【算法】贪心
【题解】
反过来做更容易理解:http://blog.csdn.net/MintGreenTZ/article/details/52949289
一般写法:http://www.cnblogs.com/ciao-sora/p/5945638.html
http://blog.csdn.net/sunshinezff/article/details/49966035
#include<cstdio> #include<algorithm> #include<queue> using namespace std; const int maxn=100010; struct cyc{int time,value;}a[maxn]; priority_queue<int>q; bool cmp(cyc a,cyc b) {return a.time>b.time;} int n; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d%d",&a[i].time,&a[i].value); sort(a+1,a+n+1,cmp); long long ans=0;int mx=a[1].time; for(int i=1;i<=n;i++) { for(int j=1;j<=mx-a[i].time-1&&!q.empty();j++)ans+=q.top(),q.pop(); mx=a[i].time; q.push(a[i].value); if(i<n&&a[i+1].time==a[i].time)continue; ans+=q.top(),q.pop(); } for(int j=1;j<=mx-0-1&&!q.empty();j++)ans+=q.top(),q.pop(); printf("%lld",ans); return 0; }
时间: 2024-10-29 10:46:38