Codeforces 853A Planning 【贪心】
来源:互联网 发布:双色球走势数据分析 编辑:程序博客网 时间:2024/06/07 17:58
题目大意
由于不可抗力什么的所有航班都要推迟k分钟,啊但是每架飞机都已经计划好原来的起飞计划了Orz(输入顺序就是原起飞计划),于是Helen就需要搞一个新的起飞计划。
飞机C每推迟一分钟(相对于原计划)就会花费C_i块钱,并且新的计划中,不允许某架飞机的起飞时间比原计划还要早,而且而且不允许同一分钟内起飞两架飞机
现在求一个最优方案使得花费最少,并且打印出这个方案来。
一个贪心的想法是优先考虑那些花费最高的飞机,使得这些飞机的延误时间最短,从而尽量降低花费,从k+1至k+n开始模拟,每个时刻i能够起飞的只有前i架飞机,用堆处理最大花费的那架。
还有就是要开longlong
ans += (long long) (i-x.ti) * x.w;
这一段有必要说一下,(i-x.ti)不会爆int
((i-x.ti)*x.w)会爆int
所以要用(longlong)强制转换(i-x.ti)而不是整个表达式,longlong转换的优先级高于加减乘除
然后longlong型的i-x.ti与x.w相乘,答案也是longlong型
#include <queue>#include <cstdio>#include <iostream>#include <algorithm>const int maxn = 300010;inline void read(int &x) { x = 0; char ch = getchar(); while(ch<'0'||ch>'9') ch = getchar(); while(ch>='0'&&ch<='9') x = x*10+ch-'0', ch = getchar(); return;}int n,c[maxn],k,work[maxn];long long ans;bool time[maxn];struct st{ int ti, w; bool operator < (const st &a) const{ if(a.w==w) { return a.ti > ti; } return a.w > w; }};using namespace std;priority_queue <st> q;int main() { read(n); read(k); for(int i=1; i<=n; i++) read(c[i]); for(int i=1; i<=k; i++) q.push((st){i,c[i]}); for(int i=k+1; i<=k+n; i++) { if(i<=n) q.push((st){i,c[i]}); st x = q.top(); q.pop(); ans += (long long) (i-x.ti) * x.w; //强制转换i-x.ti为longlong之后再与x.w相乘 work[x.ti] = i; } printf("%lld\n",ans); for(int i=1; i<=n; i++) printf("%d ",work[i]); return 0;}
阅读全文
0 0
- Codeforces 853A Planning 【贪心】
- (codeforces 853A)Planning 贪心
- 【贪心+并查集】Codeforces 853A Planning
- Codeforces 853A Planning
- Codeforces 854C. Planning 贪心
- Codeforces Round #433 C. Planning(贪心)
- Codeforces 854 C Planning(优先队列+贪心)
- Codeforces 854C Planning【贪心+并查集】
- Codeforces Round #433 (Div. 2) C. Planning(贪心)
- Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
- Codeforces Round #433 (Div. 2)-贪心-C. Planning
- Planning CodeForces
- Planning CodeForces
- A. Dragons codeforces+贪心
- CodeForces 288A--贪心
- Codeforces 556A 贪心
- Codeforces 853A Round#433 Div2C & Div1A Planning:优先队列或堆或排序
- Planning(贪心)
- HNU OJ题库 1001B图像旋转问题
- Vue监听一个数组id是否与另一个数组id相同
- 7-34 任务调度的合理性(25 分)
- TCP协议简介
- JavaWeb学习总结(三)——Tomcat服务器学习和使用(二)
- Codeforces 853A Planning 【贪心】
- BZOJ 4569 萌萌哒 (并查集)
- TeeChart.ocx在32位机和64位机上的的安装方法
- Matlab数字图像处理的基础
- 628. Maximum Product of Three Numbers
- javaweb学习总结(四)——Http协议
- pycharm控制台项目文件名中文字体乱码显示口口口问题解决
- Android APK签名
- Problem F: 求余数