Description
Farmer John is preparing a delicious meal for his cows! In his barn, he has N haybales (1≤N≤100,000). The ith haybale has a certain flavor Fi (1≤Fi≤10^9) and a certain spiciness Si (1≤Si≤10^9). The meal will consist of a single course, being a contiguous interval containing one or more consecutive haybales (Farmer John cannot change the order of the haybales). The total flavor of the meal is the sum of the flavors in the interval. The spiciness of the meal is the maximum spiciness of all haybales in the interval.
Farmer John would like to determine the minimum spiciness his single-course meal could achieve, given that it must have a total flavor of at least M (1≤M≤10^18).
Input
The first line contains the integers N and M, the number of haybales and the minimum total flavor the meal must have, respectively. The next N lines describe the N haybales with two integers per line, first the flavor F and then the spiciness S.
Output
Please output the minimum spiciness in a single course meal that satisfies the minimum flavor requirement. There will always be at least one single-course meal that satisfies the flavor requirement.
Sample Input
5 10
4 10
6 15
3 5
4 9
3 6
Sample Output
9
Solution
朴素算法
直接暴力枚举二元组(i,j)(i,j)(i,j),统计sum(fi,fi+1,fi+2,...,fj)sum(f_i,f_i+1,f_i+2,...,f_j)sum(fi,fi+1,fi+2,...,fj),若sum(fi,fi+1,fi+2,...,fj)>=Msum(f_i,f_i+1,f_i+2,...,f_j)>=Msum(fi,fi+1,fi+2,...,fj)>=M,那么用max(si,si+1,si+2,...,sj)max(s_i,s_i+1,s_i+2,...,s_j)max(si,si+1,si+2,...,sj)去更新答案,总时间复杂度为O(n3)O(n^3)O(n3)
RMQ优化
用O(1)O(1)O(1)的时间求max(si,si+1,si+2,...,sj)max(s_i,s_i+1,s_i+2,...,s_j)max(si,si+1,si+2,...,sj),总时间复杂度为O(n2)O(n^2)O(n2)
发现规律
当一个序列刚好满足sum(fi,fi+1,fi+2,...,fj)>=Msum(f_i,f_i+1,f_i+2,...,f_j)>=Msum(fi,fi+1,fi+2,...,fj)>=M时,我们不需要考虑再增长这个序列,这个刚好满足条件的序列肯定比再增长后的序列优,因为如果后面加入的数比当前的数都小或相等,他不会对答案产生任何影响,如果后面加入的数比当前的数都大,他反而会使这个序列变得不优。
所以求前缀和,枚举iii,二分出第一个满足条件的jjj,用RMQ或线段树,找到这个序列中最大的数,尝试更新答案,总时间复杂度O(nlogn)O(nlogn)O(nlogn)
Code
#include<cstdio>
#include<cmath>
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
long long m,sum[100005],f[100005];
int n,s[100005][20],r,er[20],ans;
inline int st_query(int l,int r){
int t=log(r-l+1)/log(2);
return max(s[l][t],s[r-er[t]+1][t]);
}
int main(){
freopen("hayfeast.in","r",stdin);
freopen("hayfeast.out","w",stdout);
scanf("%d%lld",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld%d",&f[i],&s[i][0]);
sum[i]=sum[i-1]+f[i];
}
er[0]=1;
for(int i=1;i<20;i++)
er[i]=er[i-1]*2;
for(int i=1;i<=log(n)/log(2);i++)
for(int j=1;j<=n-er[i]+1;j++)
s[j][i]=max(s[j][i-1],s[j+er[i-1]][i-1]);
ans=0x7fffffff;
for(int i=1;i<=n;i++){
r=i;
for(int j=log(n-i)/log(2);j>=0;j--)
if(sum[r+er[j]]-sum[i-1]<m&&r+er[j]<=n)
r+=er[j];
if(sum[r]-sum[i-1]<m)
r+=1;
if(r>n) continue;
ans=min(ans,st_query(i,r));
}
printf("%d",ans);
return 0;
}
博客详细介绍了如何使用二分查找和RMQ(Range Minimum Query)优化解决USACO 2017 December Gold竞赛中的Haybale Feast问题。内容涵盖了问题描述、输入输出格式、样例及解决方案,包括朴素算法、RMQ优化以及通过发现规律降低时间复杂度到O(nlogn)的方法。
&spm=1001.2101.3001.5002&articleId=106482947&d=1&t=3&u=fd67314cfc7f484baf6d5b06544ecb74)
530

被折叠的 条评论
为什么被折叠?



