hdu 1789 Doing Homework again (Greedy)
继续贪心。经典贪心算法,如果数据比较大就要用线段树来维护了。
思路很简单,只要按照代价由大到小排序,然后靠后插入即可。RE了一次,是没想到deadline可以很大。如果deadline比任务总量要大,显然这个任务是能做的,直接加上去。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
——written by Lyon