地鼠游戏——有趣的贪心
一看到就写了个(STL)优先队列,然后从第一秒开始贪,结果样例就错了。后来改成了从最后一秒贪起,还是过了。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdio> 4 #include<queue> 5 using namespace std; 6 const int N=128; 7 struct node{ 8 int num,val; 9 bool operator < (const node oth) const {return val<oth.val;} 10 }; 11 12 int n,val[N],t[N]; 13 int res; 14 bool hit[N]; 15 priority_queue<node> q[N]; 16 int main(){ 17 cin>>n; 18 for(int i=1;i<=n;i++)cin>>t[i]; 19 for(int i=1;i<=n;i++)cin>>val[i]; 20 for(int i=1;i<=n;i++) 21 for(int j=1;j<=t[i];j++) 22 q[j].push((node){i,val[i]}); 23 for(int i=n;i>=1;i--){ 24 bool out=false; 25 while(!out&&!q[i].empty()){ 26 node x=q[i].top();q[i].pop(); 27 if(hit[x.num])continue; 28 else{ 29 out=true; 30 hit[x.num]=true; 31 res+=x.val; 32 } 33 } 34 } 35 cout<<res<<endl; 36 return 0; 37 }
CodeVS 10ms

浙公网安备 33010602011771号