ZOJ Problem Set - 3908 Number Game
Chieh
posted @ 2015年11月01日 12:49
in NO Answer No Speak
, 354 阅读
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=123456; int ai[maxn]; int Link[12345]; int bi[12345]; int T; int n,m,k; bool cmp(int a,int b) { return a>b; } void init() { scanf("%d%d%d",&n,&m,&k); memset(bi,0,sizeof(bi)); for(int i=1; i<=n; i++) { scanf("%d",&ai[i]); if(ai[i]==0)continue; bi[ai[i]]++; } Link[0]=0; bi[0]=100000000; for(int i=1; i<=10000; i++) { if(bi[i]==0)Link[i]=Link[i-1]; else Link[i]=i; } sort(ai+1,ai+1+n,cmp); } vector<int> V; int Find(int x) { if(Link[x]==x)return x; Link[x]=Find(Link[x]); return Link[x]; } void play() { V.clear(); for(int i=1; i<=n; i++) { int cz=k-ai[i]; if(ai[i]==0)continue; if(bi[ai[i]]==0)continue; bi[ai[i]]--; if(bi[ai[i]]==0)Link[ai[i]]=Link[ai[i]-1]; if(cz<=0)continue; if(cz>10000)cz=10000; int f=Find(cz); bi[f]--; if(bi[f]==0)Link[f]=Link[f-1]; V.push_back(f*ai[i]); } int p=V.size(); LL ans=0; sort(V.begin(),V.end(),cmp); for(int i=0; i<min(p,m); i++) { ans+=V[i]; } printf("%lld\n",ans); } int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }