码迷,mamicode.com
首页 > 其他好文 > 详细

可以反悔的贪心

时间:2019-10-13 17:21:01      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:code   开始   https   std   return   www   http   problem   uil   

第一次遇到这种题,但是我觉得还是不严谨,希望能御剑有同感的朋友交流一下。

https://www.luogu.org/problem/P4053

我写的代码是这样的:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
inline long long read() {
    char ch = getchar(); long long x = 0, f = 1;
    while(ch < 0 || ch > 9) {
        if(ch == -) f = -1;
        ch = getchar();
    } while(0 <= ch && ch <= 9) {
        x = x * 10 + ch - 0;
        ch = getchar();
    } return x * f;
}
struct build {
    ll a;
    ll t;

} datas [150001];
inline bool cmp (const build &a,const build &b) {
    return a.t < b.t;
}
priority_queue <int> q;
signed main(){
#ifndef ONLINE_JUDGE
    freopen ("shit.txt","r",stdin);
#endif 
#ifdef ONLINE_JUDGE
#endif
    int n = read ();
    for (int i = 1;i <= n;++ i) {
        datas [i].a = read ();
        datas [i].t = read ();
    }
    sort (datas + 1,datas + 1 + n,cmp);
    //开始贪心
    int nowt = 0;
    ll ans = 0,nowa = 0;
    for (int i = 1;i <= n;++ i) {
        //其实就是bfs 
        if (nowt + datas [i].a > datas [i].t) {
            //不满足 弄出来一个修复时间最长的然后替换 
            if (!q.empty () && q.top () > datas [i].a && nowt - q.top () + datas [i].a <= datas [i].t) {
                nowt -= q.top ();
                q.pop ();
                q.push (datas [i].a);
                nowt += datas [i].a;
            }
        }else {    //那就直接建造
            ++ ans;
            nowt += datas [i].a;
            q.push (datas [i].a); 
        }
    }
    cout << ans << endl;
    return 0;
}

但是我觉得还是很玄学。也说不出来为啥2333

可以反悔的贪心

标签:code   开始   https   std   return   www   http   problem   uil   

原文地址:https://www.cnblogs.com/dorbmon/p/11666918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!