LA 3266 || UVALive 3266 Tian Ji -- The Horse Racing 田忌赛马(贪心)
来源:互联网 发布:加强网络基础设施建设 编辑:程序博客网 时间:2024/05/29 16:31
大体题意:
田忌与齐王赛马,他们有n 匹马,告诉你每匹马的速度,田忌赢一场得200,输一场扣200,平局不赚不扣,求最高赚多少?
思路:
贪心思路
都给双方排个序,从最快的 马开始枚举,
如果田忌的当前的马快于齐王的马,那么这局肯定赢,直接得200。
如果慢与齐王,那么这局肯定赢不了,既然赢不了,那不如用田忌当前最慢的马去输齐王当前最快的马。
如果与齐王速度相同,那么这局也赢不了,那就看田忌当前最慢的马,如果当前最慢的马和齐王当前最快的马平速,那么不赚不得,继续循环,如果慢与齐王最快的马,那么肯定输掉200了!
模拟一下钱即可!
详细见代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1000 +10;int a[maxn],b[maxn];int main(){ int n; while(scanf("%d",&n) == 1 && n){ for (int i = 1; i <= n; ++i)scanf("%d",&a[i]); for (int i = 1; i <= n; ++i)scanf("%d",&b[i]); int p1 = 1,p2 = n,q1 = 1,q2 = n; sort(a+1,a+n+1); sort(b+1,b+n+1); int ans = 0; for (int i = 1; i <= n; ++i){ if (a[p2] > b[q2]){ ans += 200; p2--;q2--; } else if (a[p2] < b[q2]){ ans -= 200; q2--;p1++; } else { if (a[p1] > b[q1]){ ans += 200; p1++;q1++; } else { if (a[p1] < b[q2]){ ans -= 200; p1++; q2--; } else { p1++; q2--; } } } } printf("%d\n",ans); } return 0;}
0 0
- LA 3266 || UVALive 3266 Tian Ji -- The Horse Racing 田忌赛马(贪心)
- LA 3266&HDU 1052 Tian Ji -- The Horse Racing(田忌赛马,贪心)
- LA 3266 Tian Ji -- The Horse Racing (贪心 || DP)
- HDU1052 Tian Ji -- The Horse Racing 田忌赛马 贪心
- UVALive - 3266 Tian Ji -- The Horse Racing 贪心
- LA 3266 Tian Ji -- The Horse Racing
- LA 3266 Tian Ji -- The Horse Racing
- UVALive 3266 Tian Ji -- The Horse Racing
- UVALive 3266 Tian Ji -- The Horse Racing
- UVALive - 3266 Tian Ji -- The Horse Racing
- UVALive 3266Tian Ji -- The Horse Racing
- Tian Ji -- The Horse Racing(田忌赛马)
- 田忌赛马(Tian Ji -- The Horse Racing)中的动态规划以及贪心算法
- 田忌赛马(Tian Ji -- The Horse Racing)中的动态规划以及贪心算法
- Tian Ji -- The Horse Racing(田忌赛马模拟)+贪心
- 【贪心专题】HDU 1052 Tian Ji -- The Horse Racing (田忌赛马)
- hdu1052 Tian Ji -- The Horse Racing (贪心,田忌赛马)
- POJ2287 Tian Ji -- The Horse Racing(田忌赛马/贪心)
- JFreeChart设置
- RHEL6 上DHCP 服务器的搭建
- [LeetCode] String to Integer (atoi)
- 【数据结构与算法】——交换排序
- 设计模式之抽象工厂模式
- LA 3266 || UVALive 3266 Tian Ji -- The Horse Racing 田忌赛马(贪心)
- 10.Java基础:常见IO流----->打印流:PrintStream、PrintWriter
- 04-贝叶斯概率
- leetcodd之candy的前后扫描得结果
- poj 2187
- 【数据结构与算法】——插入排序
- 为什么我们要干一行爱一行
- 上拉电阻与下拉电阻的作用总结
- Codeforces #369 (Div. 2) E. ZS and The Birthday Paradox (勒让德定理+逆元)