HDU1789
一、题目描述

二、解题思路
这题是一个贪心问题,但是不是简单贪心就行了,对于每一次我如果天数不满足期限,我应当判断一下以前天数是否有最大罚时超过他的,如果有我们比较一下以前的最大罚时和当前罚时。
如果大于的话我们就选择把当前罚时加入优先队列,否则的话我们就把答案加上以前的最大罚时。再把以前那个最大罚时出队。
三、代码实现
1 #include <iostream> 2 #include <queue> 3 #include <vector> 4 #include <cstring> 5 #include <string> 6 #include <map> 7 #include <cmath> 8 #include <algorithm> 9 #include <set> 10 #include <stack> 11 #include <cstdio> 12 #include <climits> 13 #define PII pair<int,int> 14 #define rep(i,z,n) for(int i = z;i <= n; i++) 15 #define per(i,n,z) for(int i = n;i >= z; i--) 16 #define ll long long 17 #define db double 18 #define vi vector<int> 19 #define debug(x) cerr << "!!!" << x << endl; 20 using namespace std; 21 inline ll read() 22 { 23 ll s,r; 24 r = 1; 25 s = 0; 26 char ch = getchar(); 27 while(ch < '0' || ch > '9'){ 28 if(ch == '-') 29 r = -1; 30 ch = getchar(); 31 } 32 while(ch >= '0' && ch <= '9'){ 33 s = (s << 1) + (s << 3) + (ch ^ 48); 34 ch = getchar(); 35 } 36 return s * r; 37 } 38 inline void write(ll x) 39 { 40 if(x < 0) putchar('-'),x = -x; 41 if(x > 9) write(x / 10); 42 putchar(x % 10 + '0'); 43 } 44 45 struct node{ 46 int day; 47 int fa; 48 }e[1010]; 49 bool cmp(node a,node b) 50 { 51 if(a.day < b.day) 52 return true; 53 else if(a.day == b.day) 54 return a.fa > b.fa; 55 else 56 return false; 57 } 58 //重构优先队列比较函数 59 struct cmp2{ 60 bool operator()(node a,node b){ 61 return a.fa > b.fa; 62 } 63 }; 64 int main() 65 { 66 int t; 67 cin >> t; 68 while(t--){ 69 int n; 70 cin >> n; 71 for(int i = 1;i <= n;i++) 72 cin >> e[i].day; 73 for(int i = 1;i <= n;i++) 74 cin >> e[i].fa; 75 sort(e + 1,e + 1 + n,cmp); 76 priority_queue <node,vector<node>,cmp2> que; 77 int ans = 0; 78 int day = 1; 79 for(int i = 1;i <= n;i++){ 80 //如果当前作业限制天数小于做作业的天数 81 //那么就比较以前罚时最多的和当前罚时 82 if(e[i].day < day){ 83 if(que.top().fa < e[i].fa){ 84 ans += que.top().fa; 85 que.pop(); 86 que.push(e[i]); 87 }else{ 88 ans += e[i].fa; 89 } 90 }else{ 91 que.push(e[i]); 92 day++; 93 } 94 } 95 cout << ans << endl; 96 } 97 return 0; 98 }
本文来自博客园,作者:{scanner},转载请注明原文链接:{https://home.cnblogs.com/u/scannerkk/}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】