打地鼠游戏(贪心+优先队列使用)

万遍红中带点绿,不过学会了一些有技巧吧。

就是会用那个优先队列了。

https://blog.csdn.net/stand1210/article/details/52464922

这个题就是贪心。

弄一个结构体,在把时间排序一下。

分两部分:

  1. 第一种可能:未雨绸缪  ( t < p [ i ] . t )
  2. 第二种可能:抉择( t > = p [ i ] . t )

讨论两种情况即可:

但是里面用到了优先队列进行维护:

priority_queue< int, vector <int > ,  greater < int > >que; 升序 top:Min

priority_queue< int >que ;降序 top:Max

扫描二维码关注公众号,回复: 2397123 查看本文章

贴上代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef struct node{
    ll T,x;
    node(ll t=0,ll X=0):T(t),x(X){}
    friend bool operator < (const node & p1, const node & p2){
        return p1.T<p2.T;
    }
}node;
node p[100005];
int main()
{
    priority_queue< ll ,vector<ll> , greater<ll> >que;
    ll n;
    scanf("%lld",&n);
    ll sumT=-1,sum=0;
    for(int i=0;i<n;i++){
        scanf("%lld",&p[i].T);
        sumT=max(sumT,p[i].T);
    }
    for(int i=0;i<n;i++){
        scanf("%lld",&p[i].x);
    }
    sort(p,p+n);
    int cnt=0,t=0;
    for(int i=0;i<n;i++){
        if(t>=p[i].T){
            ll tmp=que.top();
            if(tmp<p[i].x){
                sum+=p[i].x-tmp;
                que.push(p[i].x);
                que.pop();
            }
        }else{
            t++;
            que.push(p[i].x);
            sum+=p[i].x;
        }
    }
    printf("%lld\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Z_sea/article/details/81192714