【USACO 2017 December Gold】Haybale Feast

Description
额
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
简单讲一下。
经过思考,我们可以发现一个性质:对于一个合法的子序列,固定左端点,无论右端点怎么往右拓展,都无法更新我们的答案。因为往右一个位,这个数如果比原子序列的最大值大,就无法更新我们的答案,因为是要最小值。如果比他小,那么更不可能更新答案。所以我们只需要枚举左端点,二分一个最近的合法的右端点,
T2
Haybale Feast
Description
额
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
简单讲一下。
经过思考,我们可以发现一个性质:对于一个合法的子序列,固定左端点,无论右端点怎么往右拓展,都无法更新我们的答案。因为往右一个位,这个数如果比原子序列的最大值大,就无法更新我们的答案,因为是要最小值。如果比他小,那么更不可能更新答案。所以我们只需要枚举左端点,二分一个最近的合法的右端点,然后在log(n)的时间内求出区间最大值。

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int N=100005;
int n,a[N],tree[4*N],ans=(1e4)*N;
ll m,s[N];
void build(int now,int l,int r) {
	if(l==r) {
		tree[now]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(now<<1,l,mid);
	build((now<<1)|1,mid+1,r);
	tree[now]=max(tree[now<<1],tree[(now<<1)|1]);
}
int query(int now,int l,int r,int x,int y) {
	if(l>y)return 0;
	if(r<x)return 0;
	if(l>=x&&r<=y)return tree[now];
	int mid=(l+r)>>1;
	return max(query(now<<1,l,mid,x,y),query((now<<1)|1,mid+1,r,x,y));
}
int main() {
	freopen("hayfeast.in","r",stdin);
	freopen("hayfeast.out","w",stdout);
	scanf("%d%lld",&n,&m);
	for(int i=1;i<=n;i++) {
		int x;
		scanf("%d%d",&x,&a[i]);
		s[i]=s[i-1]+x;
	}
	build(1,1,n);
	for(int i=1;i<=n;i++) {
		if(s[n]-s[i-1]<m)break;
		int l=i,r=n,ret=0;
		while(l<=r) {
			int mid=(l+r)>>1;
			if(s[mid]-s[i-1]>=m)ret=mid,r=mid-1;
			else l=mid+1;
		}
		int k=query(1,1,n,i,ret);
		if(k<ans)ans=k;
	}
	printf("%d",ans);
}

猜你喜欢

转载自blog.csdn.net/MZHjr/article/details/106531826