Codeforces Round #334 (604B) More Cowbell [贪心]

来源:互联网 发布:编程语言与脚本语言 编辑:程序博客网 时间:2024/05/21 08:59
B. More Cowbell
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection.

Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.

题意:

给出N个已排序的奶牛铃铛的大小,现在把它们装到箱子里,一个箱子最多装两个铃铛,且不能铃铛的大小和不能超过箱子大小,问在使用K个箱子的前提下,问盒子最小多大。

解法:

简单贪心,算出最后有2*K-N个铃铛装1个,显然这些越大越好,直接装最后几个,剩下的两个装一起,例如 2,3,4,5,显然组合方案为(2,5)+(3,4)不断首尾拿出来装到一起即可,这样肯定是最优的(坑点,K大于N,一开始需K=min(K,N))

代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<iostream>#include<stdlib.h>#include<set>#include<map>#include<queue>#include<vector>#include<bitset>#pragma comment(linker, "/STACK:1024000000,1024000000")template <class T>bool scanff(T &ret){ //Faster Input    char c; int sgn; T bit=0.1;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    if(c==' '||c=='\n'){ ret*=sgn; return 1; }    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;    ret*=sgn;    return 1;}#define inf 1073741823#define llinf 4611686018427387903LL#define PI acos(-1.0)#define lth (th<<1)#define rth (th<<1|1)#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)#define mem(x,val) memset(x,val,sizeof(x))#define mkp(a,b) make_pair(a,b)#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define pb(x) push_back(x)using namespace std;typedef long long ll;typedef pair<int,int> pii;int a[1000100];int n,k;int main(){    scanff(n);    scanff(k);    k=min(n,k);    rep(i,1,n){        scanff(a[i]);    }    int p=n,ans=0;    rep(i,1,2*k-n){        ans=max(ans,a[p--]);    }    rep(i,1,p/2){        ans=max(ans,a[i]+a[p-i+1]);    }    printf("%d\n",ans);}




0 0
原创粉丝点击