HDU 4221 Greedy?(贪心)
2017-09-05 08:33
253 查看
贪心法
HDU 4221 Greedy?
提 交
参考博客
这道题读了好几遍最后用标程输出答案才理解题目的意思。 举个栗子![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif)
有n个工作,每项工作对应完成此任务需要的时间和这项任务的截止时间 ,如果超过截止时间,会被赋予一个惩罚时间(完成时间-截止时间),问你如何使得最大的惩罚时间尽可能的小
?
也就是说如果一项任务我的方案一:是每项任务都需要比截止时间延迟3时间完成
方案二:是除了最后两个任务外都按时完成,最后两个任务一个延时4个时间点一个延时2个时间点。
那么3 3 3 3 3 3 中的max:3 < 0 0 0 0 1 4 中的max:4所以方案一最优,输出 3。
好了,理解题目之后
我们想,这道题目好像和hdoj的那个今年暑假不AC的题目,是按照结束时间排个序,但是这道题就不是那么好想了,就是知道答案有的时候也想不明白。
这里推到一下证明过程哈
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif)
(这里先写出来,感觉不能自圆其说,时而糊涂时而明白,等彻底想明白再更新下吧)
对于两个活动1、2,假设D1<D2,以起始时间为0为例。
如果先安排活动1,则扣分最大值为max(C1-D1,C1+C2-D2)。
如果先安排活动2,则扣分最大值为max(C2-D2,C1+C2-D1)。
最后结果就是输出min( max(C1-D1,C1+C2-D2), max(C2-D2,C1+C2-D1) )对吧?
也就是说先找出来最大值,再取反,~(max( max(C1-D1,C1+C2-D2), max(C2-D2,C1+C2-D1) ))
显然 (谁再说显然,我打死他
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/knock.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/knock.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/knock.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/curse.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/crazy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/angry.gif)
) :
C1+C2-D1 > C1 - D1,
C1+C2-D2 > C2 - D2,
所以变成~max( max(C1-D1,C1+C2-D2),
max(C2-D2,C1+C2-D1) )=》~max( (C1+C2-D2),
C1+C2-D1) )
假设D1< D2
则 C1+C2-D1是最大值,再反一下,就是
C1+C2-D2,也就是最小值了吧,也就是答案。那么回头看是结束时间 先小后大的 这个顺序,所以按照结束时间排序啦。。。感觉怪怪的。。。
上代码:
#include<cstdio> #include<iostream> #include<algorithm> #define N 100500 #include<cstring> using namespace std; int n; int m; //int a [2]; //int b [2]; //int temp [2]; struct Ndoe { int stra; int end; } str ; //bool comp(Ndoe a,Ndoe b) //{ // return a.end < b.end; //} bool comp(Ndoe a, Ndoe b) { if(a.end==b.end) return a.stra>b.stra; return a.end<b.end; } int main() { // freopen("in.txt", "r", stdin); memset(str, 0, sizeof(str)); scanf("%d", &n); for(int j = 0; j < n; j ++) { long long 4000 int sum = 0; long long int max = 0; long long int ans = 0; scanf("%d", &m); for(int i = 0; i < m; i ++) { scanf("%d%d", &str[i].stra, &str[i].end); // sum += str[i].stra; } sort(str, str + m, comp); for(int i = 0; i < m; i ++) { // printf("%d %d+\n", str[i].stra, str[i].end); sum += str[i].stra; ans = sum - str[i].end; if(max < ans) max = ans; } printf("Case %d: %lld\n", j + 1, max); } } //#include <stdio.h> //#include <algorithm> //using namespace std; //struct TT //{ // int c,d; // bool operator < (const TT& s)const // { // return d < s.d; // } //} A[100002]; //int main() //{ // freopen("in.txt", "r", stdin); // int z,n; // __int64 t,ans; // scanf("%d",&z); // for(int k=1; k<=z; k++) // { // scanf("%d",&n); // for(int i=0; i<n; i++) // scanf("%d%d",&A[i].c,&A[i].d); // sort(A,A+n); // // // // for(int i = 0; i < n; i ++) // { // printf("%d %d+\n", A[i].c, A[i].d); // } // // // // // // t = ans = 0; // for(int i=0; i<n; i++) // { // t += A[i].c; // if(ans < t - A[i].d) // ans = t - A[i].d; // } // printf("Case %d: %I64d\n",k,ans); // } // return 0; //}
NOIP 2012 贪心 高精度 国王游戏
首先分享几个挺好的博客****1****
2
3
4
此图片地址:点击跳转
思路点拨:点击跳转(最好独立思考出来,有了上到题的基础这道题就很好做了)
设i前面大臣累乘结果为sum,i(Li,Ri),j(Lj,Rj)①i在前面
ans=max(sum/Ri,sum*Li/Rj)
②j在前面
ans=max(sum/Rj,sum*Lj/Ri)
显然的,①的后一项比②的前一项大,②的后一项比①的前一项大,么只要②的后一项比①的后一项大,一定选择i在前面更优,这个条件就可以推出,Li*Ri。
思路会了,但是看这个有些打怵(没接触过高精度,感觉直接左乘右排序不就行了吗),不太敢写,写了也没地方交= =。
先放在一边。。待更新
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
。。。搞了半天这跟贪心有什么关系......
:
注意上一道题是在遍历的过程中出现max,找好max最小的处理(排序)方式,就开始遍历,途中会出现max,输出此max;
3 4
5 6
6 6
5 8
1 100
最后是左边的sum - 100;
而上一层是(sum - 1) - 8 == 》 sum - 9;
所以显然枚举到最后的不是要输出的max;
这道题也一样。
感觉这份代码有问题!!!!!!!!!高精度参考
HDU 4310 Hero
提议很好理解,就是说你的队友只剩下你一个人了,你操作你的英雄(only one)1 vs N,打败所有的敌军损失最少的血量。对于你和敌军的英雄各自都有一个HP(血量)和DSP(攻击力),损失的血量 == 攻击方英雄的攻击力,你有无限的血量,但是攻击力只有1,你挑任意一个敌方英雄攻击,与此同时你受到的损失为对方所有活着的英雄攻击力的总和 ,血量<= 0认为是死亡。
PS:你攻击血量为1的英雄,它也会攻击你,只不过在下一局它死掉不会攻击你了。
思路:
这道题很好推出来结论,一定要自己推出来哦= =好吧,
为了方便伸手党我来上图:
(字能不能写好点
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/curse.gif)
,看的大爷我眼花缭乱~~)
上代码:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int n; struct Node { double DSP; double HP; } a[30]; bool comp(Node a,Node b) { // return a.HP / a.DSP < b.HP / b.DSP; return a.DSP / a.HP > b.DSP / b.HP; } int sum[30]; int susm[30]; int main() { // freopen("in.txt", "r", stdin); while(scanf("%d", &n) != EOF) { int ans = 0; memset(a, 0, sizeof(a)); memset(sum, 0, sizeof(sum)); memset(susm, 0, sizeof(susm)); for(int i = 0; i < n; i ++) scanf("%lf%lf", &a[i].DSP, &a[i].HP); sort(a, a + n, comp); // for(int i = 0; i < n; i ++) // { // for(int j = i; j < n; j ++) // { // sum[i] += a[j].DSP; // } // sum[i] *= a[i].HP; // } for(int i = n - 1; i >= 0; i --) { sum[i] = sum[i + 1] + a[i].DSP; susm[i] = sum[i] * a[i].HP; } for(int i = 0; i < n; i ++) ans += susm[i]; printf("%d\n", ans); } }
这道题需要注意的是double类型,如果是不能整除的整数来比较的话,会出现小数,如果int会有误差,这样过排序的时候会出错
(怎么感觉像昨天放弃的国王的游戏(高精度)呢= = )
http://tool.chinaz.com/Tools/textencrypt.aspx U2FsdGVkX1+HvqfsKkaLopnRUzWZ7PWKBzrbRJm9V4wxjW4orCAdnWSZAW94spN8
Up9NhTRpLbldN9a6kDURW4hoto/knR1AXqnH32J5df8=
相关文章推荐
- Greedy? HDU - 4221 (贪心)
- Greedy? HDU - 4221 (贪心)
- Greedy? HDU - 4221 (贪心)
- Greedy? HDU - 4221 (贪心)
- Greedy? HDU - 4221 (贪心)
- Greedy? HDU - 4221 (贪心)
- HDU 4221 Greedy?(贪心)
- HDU 4221 Greedy?(贪心啊啊)
- 贪心--HDU - 4221 Greedy?
- Greedy? HDU - 4221 (贪心)
- HDU 4221 Greedy?(贪心)
- HDU 4221 Greedy?(贪心)
- HDU-4221 Greedy? 贪心 从元素的相对位置开始考虑
- Greedy? HDU - 4221 (贪心)
- HDU 4221 贪心
- hdu 4221 贪心
- HDU 4221 Greedy?
- HDU--4221--Greedy?
- HDU 4221 Greedy?
- HDU 4221 Greedy?