hdoj1052 Tian Ji -- The Horse Racing(贪心算法+2)
来源:互联网 发布:网络借贷平台哪个方便 编辑:程序博客网 时间:2024/05/16 14:51
田忌赛马,关键在于比较的次序。
首先先比较两个人最慢的马,如果田忌的马快就直接赢下一分,count++
如果更慢的话就用这匹慢马去与大王最快的马比赛,count--
如果相等的话,再比较两个人最快的马,如果田忌的马更快,count++
反之就用田忌的慢马去消耗大王这匹快马,count--
如果仍相等,最后用田忌最慢的马与大王最快的马比较,这时因为大王和田忌最快的马是一样快的,
所以这时只存在田忌最慢的马比大王最快的马慢和田忌大王的马都一样快这两种情况,前一种情况count--,后一种count不变。
代码如下:
#include<stdio.h>#include<string>#include<algorithm>using namespace std;int a[1005],b[1005];int n;int main(){int count,s1,s2,e1,e2;while(~scanf("%d",&n)&&n!=0){count=0;for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++)scanf("%d",&b[i]);sort(a+1,a+1+n);sort(b+1,b+1+n);for(s1=1, s2=1, e1=n, e2=n;s1<=e1&&s2<=e2;){if(a[s1]>b[s2]){count++;s1++;s2++;}else if(a[s1]<b[s2]){count--;s1++;e2--;}else if(a[e1]>b[e2]){count++;e1--;e2--;}else if(a[e1]<b[e2]){count--;s1++;e2--;}else if(a[s1]<b[e2]){count--;s1++;e2--;}else{s1++;e2--;}} printf("%d\n",200*count);}return 0;}
阅读全文
0 0
- hdoj1052 Tian Ji -- The Horse Racing(贪心算法+2)
- 【hdoj1052】Tian Ji -- The Horse Racing(贪心)
- hdu 1052 Tian Ji -- The Horse Racing 贪心算法
- Tian Ji -- The Horse Racing hdu1052 贪心算法
- hdu 1052 Tian Ji -- The Horse Racing 贪心算法
- hdu :Tian Ji -- The Horse Racing 之贪心算法
- HDU 1052 Tian Ji -- The Horse Racing(贪心算法)
- HDU hdu 1052 Tian Ji -- The Horse Racing 贪心算法
- hdoj 1052Tian Ji -- The Horse Racing【贪心算法】
- hdu 1052 Tian Ji -- The Horse Racing(贪心算法)
- HDU1052 Tian Ji -- The Horse Racing(贪心)
- HDU1052 Tian Ji -- The Horse Racing【贪心】
- zoj2397 Tian Ji -- The Horse Racing 贪心
- hdu1052 Tian Ji -- The Horse Racing 贪心
- HDU1052 Tian Ji -- The Horse Racing 贪心
- Tian Ji -- The Horse Racing(贪心)
- [HDU1052]Tian Ji -- The Horse Racing[贪心]
- HDU1052 Tian Ji -- The Horse Racing(贪心)
- codeforces 144A Arrival of the General
- Vim 快速上手
- Ubuntu下sublime安装完package control后Preference下没有出现的问题
- 安卓事件传递机制
- 倒计时
- hdoj1052 Tian Ji -- The Horse Racing(贪心算法+2)
- 为兼容IE8 使用layer弹框简记
- Ajax的基础知识
- 控制随机数生成概率的一种解决方法
- Lintcode——两数组的交 II
- 数据白化
- win10 wifi断开连接
- Java 网络编程
- Java并发库