zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    centos crash debug
    go get Unknown SSL protocol error in connection to gopkg.in
    Tensorflow serving with Kubernetes
    Spring 集成 Swagger UI
    Docker Registry V2 Garbage Collection
    Docker Registry V2 with Nginx
    Zabbix磁盘性能监控
    Zabbix CPU utilization监控参数
    Windows挂载Gluster复制卷
    Redis持久化存储(三)
  • 原文地址:https://www.cnblogs.com/LyonLys/p/hdu_1789_Lyon.html
Copyright © 2011-2022 走看看