【Maratona de Programa¸c˜ao da SBC 2019 M】 Maratona Brasileira de Popcorn 二分答案

The “Maratona Brasileira de Popcorn” is a competition that takes place annually to find out which
team is the most organized, prepared and well-trained in the art of eating popcorn. It is organized by
Brazilian Society of Popcorn Eaters (SBCp, its acronym in Portuguese), which periodically meets to
discuss the rules and format of the competition.
The competition consists of N popcorn bags placed side by side, where each bag has an arbitrary
amount of popcorn. For added fun, the competition takes place in teams, each made up of C competitors. Since the “Maratona Brasileira de Popcorn” is a serious event that values, above all, the health
of the competitors, the medical commission has imposed that each competitor may eat a maximum of
T popcorn per second to avoid possible sickness.
At its last meeting, SBCp defined two new rules for the 2019 edition:
– Each team competitor must eat a contiguous sequence of popcorn bags. It is perfectly valid that
a competitor does not eat any popcorn.
– All popcorn in the same bag must be eaten by a single competitor.
The goal of the competition is to eat all the popcorn in the shortest possible time as the C
competitors can eat in parallel and they will abide by all rules imposed by the SBCp.
Input
The first line of input contains three integer numbers N, C y T (1 ≤ N ≤ 105
, 1 ≤ C ≤ 105 and
1 ≤ T ≤ 50), representing the number of popcorn bags in the competition, the number of competitors
in the team, and the maximum amount of popcorn per second a competitor can eat. The second line
contains N integers Pi (1 ≤ Pi ≤ 104
), representing the amount of popcorn on each of the N popcorn
bags.
Output
Your program must output a single line, containing an integer number, representing The minimum
amount of seconds it takes for the team to eat all the popcorn if they organize themselves as best
possible.
Input example 1
5 3 4
5 8 3 10 7
Output example 1
4
Input example 2
3 2 1
1 5 1
Output example 2
6
Input example 3
3 2 1
1 1 5
Output example 3
5

题意:n个爆米花,c个人,每个人每秒吃t个单位,问最小吃完所有爆米花的时间

思路:

二分答案,二分最小时间mid,对于每个mid,让一个人吃掉mid*t内尽可能多的爆米花。如果当前超过了mid * t的话就多加一个人进来吃。最后看看要的人数和c比较即可。小于的话说明时间还可以再小。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
//#include<random>
#include<cstdlib>
#include<ctime>
#include<map>
#include<set>
#include<stack>
#include<queue>
#define FAST ios::sync_with_stdio(false)
#define DEV_RND ((int)rand()*RAND_MAX+rand())
#define RND(L,R) (DEV_RND%((R)-(L)+1)+(L))
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,n,a) for(int i=n-1;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define li inline
#define re register
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ld;
//typedef uniform_int_distribution<int> RNDI;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
const int Hash = 131;//13331
const int maxn = 1e5+5;
const int maxm = 1e5+5;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-7;
const double pi = acos(-1);
//int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
//li int f(int x){return x==par[x]?par[x]:par[x]=f(par[x]);}
//mt19937 eng(time(0));
//li int RND(int L,int R){RNDI rnd(L,R);return rnd(eng);}
li ll lowbit(ll x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
li ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res%MOD;}
li ll qmul(ll a,ll b,ll MOD=mod){return (a*b-(ll)((long double)a/MOD*b)*MOD+MOD)%MOD;}
li ll inv(ll x,ll p){return qpow(x,p-2,p);}
li ll jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
db f(db x){return x;}
li db sim(db l,db r){return (f(l)+4.*f((l+r)/2.)+f(r))*(r-l)/6.;}
db asr(db l,db r,db ans,db eps){db m=l+(r-l)/2.,L=sim(l,m),R=sim(m,r);return fabs(L+R-ans)<=15.*eps?L+R+(L+R-ans)/15.:asr(l,m,L,eps/2)+asr(m,r,R,eps/2);}
db asr(db l,db r,db eps){return asr(l,r,sim(l,r),eps);}
namespace IO
{
	li ll read()
	{
		ll x=0,sign=1;char c=getchar();
		for(;!isdigit(c);c=getchar()) if(c=='-')sign=-1;
		for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
		return x*sign;
	}
	template<typename T>
	li void write(T x,char t='\n')
	{
		if(x<0){x=-x;putchar('-');};
		static int sta[25];int top=0;
		do{sta[top++]=x%10;}while(x/=10);
		while(top) putchar(sta[--top]+'0');
		putchar(t);
	}
}
using namespace IO;
/*-------------head-------------*/
//
ll n,c,t;
ll a[maxn];
bool check(ll mid)
{
	ll cnt=0,sum=0;
	rep(i,0,n)
	{
		if(sum+a[i]<=t*mid)sum+=a[i];
		else ++cnt,sum=a[i];
	}
	if(sum)++cnt;
	return cnt<=c;
}
li void solve()
{
	c=read(),t=read();
	ll l=0,r=0;
	rep(i,0,n)
	{
		a[i]=read();
		l=max(l,(ll)ceil(a[i]/(double)t));
		r+=(ll)ceil(a[i]/(double)t);
	}
	while(l<=r)
	{
		ll mid=l+r>>1;
		if(check(mid))r=mid-1;
		else l=mid+1;
	}
	write(l);
	//puts("");
}
int main()
{
	//srand(time(0));
	//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
	//for(int QwQ=read();QwQ;QwQ--) solve();
	while(~scanf("%lld",&n)) solve();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/107516653
DE
今日推荐