codevs 1052 地鼠游戏 贪心

来源:互联网 发布:linux vi移到行尾 编辑:程序博客网 时间:2024/05/22 17:50

题目:
http://codevs.cn/problem/1052/

今晚状态不好QAQ;

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>using namespace std;const int MAXN=10001;int tim[MAXN],cost[MAXN];int tot,ans,n;struct hh{    int tim,cost;}ma[MAXN];priority_queue<hh>q;bool operator < (hh a,hh b){    return a.cost>b.cost;}bool cmp(hh a,hh b){    return a.tim<b.tim;}void solve(){    scanf("%d",&n);    for(int i=1;i<=n;i++) scanf("%d",&ma[i].tim);    for(int i=1;i<=n;i++) scanf("%d",&ma[i].cost);    int j=1;    sort(ma+1,ma+n+1,cmp);//按时间排序;     while(true)    {        if(j==n+1) break;        if(ma[j].tim>tot)//tot为已经打了的地鼠的数量;             ans+=ma[j].cost,tot++,q.push(ma[j]);        else if(!q.empty() && ma[j].tim<=tot)//当前时间,已经打够了地鼠;         {            hh u=q.top();            if(ma[j].cost>u.cost)            {                q.pop(),ans+=ma[j].cost-u.cost;                q.push(ma[j]);            }        }        j++;    }    cout<<ans<<'\n';} int main(){    solve();    return 0;}
原创粉丝点击