[USACO10FEB]吃巧克力Chocolate Eating

题目:洛谷P2985。

题目大意:有n块巧克力要吃d天,并且只能按顺序吃。一块巧克力有一个开心值,吃了就能增加开心值。一个人初始开心值为0,且每天早上开心值变为原来的一半。问如何吃巧克力才能使开心值最小的一天开心值最大(每天都按吃完巧克力后计算),且需要输出方案。

解题思路:最大化最小值问题,用二分答案。

贪心地扫描,对于一个答案,如果开心值不到这个答案,就一直吃巧克力即可。

最后输出吃的方案时也用此种贪心法。

注意如果最后巧克力没有吃完,则在最后一天全部吃掉。

C++ Code:

#include<cstdio>
#include<cctype>
int n,d,a[50005];
long long l,r,ans;
inline int readint(){
	char c=getchar();
	for(;!isdigit(c);c=getchar());
	int d=0;
	for(;isdigit(c);c=getchar())
	d=(d<<3)+(d<<1)+(c^‘0‘);
	return d;
}
bool ok(long long k){
	long long kx=0;
	int qk=1;
	for(int i=1;i<=d;++i){
		kx>>=1;
		while(kx<k&&qk<=n)kx+=a[qk++];
		if(kx<k)return false;
	}
	return true;
}
void print(long long k){
	long long kx=0;
	int qk=1;
	for(int i=1;i<=d;++i){
		kx>>=1;
		while(kx<k&&qk<=n){
			kx+=a[qk++];
			printf("%d\n",i);
		}
	}
	while(qk<=n)printf("%d\n",d),++qk;
}
int main(){
	n=readint(),d=readint();
	for(int i=1;i<=n;++i)a[i]=readint();
	l=0,r=50000000005LL,ans=0;
	while(l<=r){
		long long mid=(l+r)>>1;
		if(ok(mid))l=(ans=mid)+1;else
		r=mid-1;
	}
	printf("%lld\n",ans);
	print(ans);
	return 0;
}
时间: 2024-10-10 06:29:22

[USACO10FEB]吃巧克力Chocolate Eating的相关文章

luogu P2985 [USACO10FEB]吃巧克力Chocolate Eating

题目描述 Bessie拿到了N (1 <= N <= 50,000)块巧克力.她决定想个办法吃掉这些巧克力,使得它在吃巧克力的这段时间里,最不开心的一天尽可能的开心.并且一共吃D (1 <= D <= 50,000)天. 每块巧克力有一个开心值H_i (1 <= H_i <= 1,000,000),当某天你吃下那块巧克力时,你将获得那块巧克力的开心值.每一天的开心值是所有当天吃掉的巧克力的总开心值之和.每天晚上Bessie睡觉之后,它的开心值会减半.也就是说,比如昨天B

洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying

https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of

洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying

题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of chocolate has price P_i (1 <= P_i <= 1

洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving

题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= 25,000) of his bulls have a special cow in mind to receive a chocolate gift. Each of the bulls and cows is grazing alone in one of the farm's N (2*B <=

洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving

https://www.luogu.org/problem/show?pid=2984 题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= 25,000) of his bulls have a special cow in mind to receive a chocolate gift. Each of the bulls and cows is graz

【luogu P2984 [USACO10FEB]给巧克力Chocolate Giving】 题解

题目链接:https://www.luogu.org/problemnew/show/P2984 练习SPFA,把FJ当做起点,求出到所有牛的最短路,再把两个牛的相加. 1 #include <cstdio> 2 #include <queue> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstring> 6 using namespace std; 7 const int

BZOJ 2016: [Usaco2010]Chocolate Eating

题目 2016: [Usaco2010]Chocolate Eating Time Limit: 10 Sec  Memory Limit: 162 MB Description 贝西从大牛那里收到了N块巧克力.她不想把它们马上吃完,而是打算制定一个计划, 使得在接下来的D天里,她能够尽量地快乐.贝西的快乐指数可以用一个整数来衡量,一开始的时候是0,当她每天晚上睡觉的时候,快乐指数会减半(奇数时向下取整).贝西把她的巧克力按照收到的时间排序,并坚持按照这个顺序来吃巧克力.当她吃掉第i块巧克力的时

bzoj2016[Usaco2010]Chocolate Eating

bzoj2016[Usaco2010]Chocolate Eating 题意: n块巧克力,每次吃可以增加ai点快乐,每天早晨睡觉起来快乐值会减半,求如何使d天睡觉前的最小快乐值最大.n,d≤50000 题解: 二分快乐值,每天不够就吃.注意如果最后一天有剩余巧克力,必须将其全部吃完. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5

2016: [Usaco2010]Chocolate Eating

2016: [Usaco2010]Chocolate Eating Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 224  Solved: 87[Submit][Status][Discuss] Description 贝西从大牛那里收到了N块巧克力.她不想把它们马上吃完,而是打算制定一个计划, 使得在接下来的D天里,她能够尽量地快乐.贝西的快乐指数可以用一个整数来衡量,一开始的时候是0,当她每天晚上睡觉的时候,快乐指数会减半(奇数时向下取整).