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

1163 最高的奖励 贪心 + 并查集

时间:2017-01-30 22:50:38      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:www   class   记录   and   分享   tor   sort   mat   时间   

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163

首先如果时间大于n,也就是相当于不限时了,因为完成前n - 1项任务需要时间n - 1,不影响我的第n项。

首先按价值排序,然后这个价值安排在它准备过期的那一天,如果被占据了,就找下一天,就能优先吃到了最大值。而且你吃其他也是耗费一个空间,所以吃个更大的是最优的。

然后可以用并查集来维护,记录topre[i]表示第i个位置的上一个空位是谁。

就能加速找到了。

不然有数据卡的,就是全部都是49999 然后权值是1--50000

 

技术分享
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 50000 + 20;
struct node {
    int tim, val;
    int id;
    bool operator < (const struct node & rhs) const {
        return val < rhs.val;
    }
}a[maxn];
int topre[maxn];
bool pos;
int tofind(int t) {
    if (t == 0) return 0;
    if (topre[t] == t) {
        topre[t] = t - 1;
        pos = true;
        return t - 1;
    }
    topre[t] = tofind(topre[t]);
}
void work() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d%d", &a[i].tim, &a[i].val);
    }
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; ++i) topre[i] = i;
    LL ans = 0;
    for (int i = n; i >= 1; --i) {
        if (a[i].tim >= n) {
            ans += a[i].val;
            continue;
        }
        pos = false;
        tofind(a[i].tim);
//        cout << pos << endl;
        if (pos != false) ans += a[i].val;
    }
    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}
View Code

 

1163 最高的奖励 贪心 + 并查集

标签:www   class   记录   and   分享   tor   sort   mat   时间   

原文地址:http://www.cnblogs.com/liuweimingcprogram/p/6358456.html

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