• hdu 1789 Doing Homework again (Greedy)


    Problem - 1789

      继续贪心。经典贪心算法,如果数据比较大就要用线段树来维护了。

      思路很简单,只要按照代价由大到小排序,然后靠后插入即可。RE了一次,是没想到deadline可以很大。如果deadline比任务总量要大,显然这个任务是能做的,直接加上去。

    代码如下:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <vector>
     6 
     7 using namespace std;
     8 
     9 typedef pair<int, int> PII;
    10 typedef vector<PII> VPII;
    11 
    12 VPII rec;
    13 vector<bool> vis;
    14 
    15 inline bool cmp(PII a, PII b) { return a > b;}
    16 int insert(PII x, int n) {
    17     int t = x.second;
    18     if (t >= n) return x.first;
    19     while (t > 0 && vis[t]) t--;
    20     vis[t] = true;
    21 //    cout << "~~~ " << t << endl;
    22     return t ? x.first : 0;
    23 }
    24 
    25 int main() {
    26     int T, n;
    27     cin >> T;
    28     while (T-- && cin >> n) {
    29         int x;
    30         rec.clear();
    31         vis = vector<bool>(n + 1, false);
    32         for (int i = 0; i < n; i++) {
    33             cin >> x;
    34             rec.push_back(PII(0, x));
    35         }
    36         int sum = 0;
    37         for (int i = 0; i < n; i++) {
    38             cin >> rec[i].first;
    39             sum += rec[i].first;
    40         }
    41         sort(rec.begin(), rec.end(), cmp);
    42         int ans = 0;
    43         for (int i = 0; i < n; i++) ans += insert(rec[i], n);
    44         cout << sum - ans << endl;
    45     }
    46     return 0;
    47 }
    View Code

    ——written by Lyon

  • 相关阅读:
    缓冲式I/O
    事件轮询接口
    博弈游戏
    多任务I/O之poll函数
    好的link
    做纹理处理的。。。
    快毕业了!
    语音处理的资料
    google图像搜索原理
    install opencv in centos
  • 原文地址:https://www.cnblogs.com/LyonLys/p/hdu_1789_Lyon.html
Copyright © 2020-2023  润新知