Problem A. Yogurt Google Kickstart Round E 2018
阿新 • • 發佈:2018-11-09
題意:給定N份酸奶,每份都有一個截止日期A[i],過期的酸奶 不能喝。每天最多隻能喝K個。問什麼策略可以喝到最多份酸奶。
貪心即可。越快過期的酸奶越早喝最好,但是需要排除當天已經過期的酸奶。例如截止日期a<b,先喝a後喝b,b剩餘的可等到時間比a長,所以多喝一份的可能性更大。極端情況:如果a當天過期,b第二天過期,先喝a可以喝兩份,而先喝b只能喝一份。
#include<iostream> #include<stdio.h> #include<cstdio> #include<string> #include<cmath> #include<stdlib.h> #include<algorithm> #include<string.h> #include<cstring> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; const int maxn=5010; int T; int N; int K; int A[maxn]; int ans; int vis[maxn]; int main() { // freopen("input.txt","r",stdin); freopen("A-large.in","r",stdin); freopen("A.txt","w",stdout); clock_t START_TIME; clock_t FINISH_TIME; START_TIME=clock(); scanf("%d",&T); for(int ca=1;ca<=T;ca++) { memset(A,0,sizeof(A)); memset(vis,0,sizeof(vis)); scanf("%d %d",&N,&K); ans=0; for(int i=0;i<N;i++) { scanf("%d",&A[i]); } sort(A,A+N); int idx=0; for(int i=0;i<N;i++) { int cnt=0; while(cnt<K) { if(vis[idx]==1) { idx++; } else if(A[idx]-i-1<0) { idx++; } else { cnt++; vis[idx++]=1; ans++; } if(idx>=N) { break; } } if(idx>=N) { break; } } printf("Case #%d: %d\n",ca,ans); cerr<<"finish case "<<ca<<endl; } FINISH_TIME=clock(); cerr<<1.0*(FINISH_TIME-START_TIME)/CLOCKS_PER_SEC <<" (s) "<<endl; return 0; }