UVALive - 3266 Tian Ji -- The Horse Racing
2016-07-23 18:48
417 查看
题目大意:田忌赛马,牺牲慢马以获得最大利益,输入田忌的马速和秦王的马速,每一轮输的人要给赢的人 200,输出可以获得的奖金。
解题思路:排序,从最大的开始比较,若比不过则比较最小的,最小若还是比不过就把田忌最小的马和秦王最大的马比,输一轮。然后再从最大的开始比,直至结束。
解题思路:排序,从最大的开始比较,若比不过则比较最小的,最小若还是比不过就把田忌最小的马和秦王最大的马比,输一轮。然后再从最大的开始比,直至结束。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctype.h> using namespace std; int a[1010]; int b[1010]; int cmp(const void*a, const void*b) { return *(int*)b-*(int*)a; } int main() { int n; while(scanf("%d", &n) != EOF && n) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); qsort(a, n, sizeof(int), cmp); qsort(b, n, sizeof(int), cmp); int sum = 0; int h1 = 0, h2 = 0; int t1 = n-1, t2 = n-1; for (int i = 0; i < n; i++) { if (a[h1] > b[h2]) { sum += 200; a[h1++] = b[h2++] = 0; continue; } if (a[t1] > b[t2]) { sum += 200; a[t1--] = b[t2--] = 0; continue; } if (a[t1] < b[h2]) { sum -= 200; a[t1--] = b[h2++] = 0; continue; } } printf("%d\n", sum); } return 0; }
相关文章推荐
- UVALive 4244 Party Party Party(HDU 2779 && Sicily 1663)
- UVALive 6741 The Sacrificial Firepits
- UVALive 5870 - Smooth Visualization
- UVALive 5876 - Writings on the Wall 【KMP】
- UVALive 5878 - Shortest Leash 【计算几何】
- UVALive 5873 - Tree Inspections 【模拟】
- UVALive 6800 The Mountain of Gold (bellman_ford判负环)
- UVALive 6436 The Busiest City
- F - Buggy Sat
- E - Lazy Math Instructor
- C - Push!!
- Uvalive 6855 Banks
- Uvalive 7150 Amalgamated Artichokes
- Uvalive 4954 Lawn mower
- Uvalive 6832 Bit String Reordering
- Uvalive 6398 Low Power
- Uvalive 6929 Sums
- UVALive 5913 【NEERC 2011 K】 Kingdom Roadmap
- UVALive 6886
- UVALive 7146 Defeat the Enemy (贪心)