【杭电OJ】--1052-田忌赛马(贪心,双端队列)
2017-02-14 22:25
357 查看
U - Tian Ji -- The Horse Racing
Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others." "Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars fromthe loser." "Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian." "Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match." "It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do youthink of Tian Ji, the high ranked official in China?"3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0Sample Output
200 0 0
思路:(1)先比较两者的好马,如果田忌的好马可以赢过齐王的好马,果断去比,高手过招,赢了才更有满
足感,自豪感嘛!
(2)再比较不好的马,如果田不好的马可以赢过齐王的不好的马,毫不犹豫去比,反正可以赢。用田
忌最弱的战胜对方最弱的,这才符合江湖规矩。
(3)如果以上都不成立,那么如果田的坏马比齐王的坏马慢,或者速度一样,就用田的坏马去和齐王
的好马比,拉齐王的好马下水,两个坏马的速度一样时,与其平局,不如让田的坏马拉齐王好马
下水。
(4)好马坏马,速度大小都是相对的,不绝对。
方法一:贪心
#include<cstdio>#include<algorithm>using namespace std;bool cmp(int A,int B){return A>B;}int t[1000+22];int q[1000+22];int main(){int n,num,t_max,t_min,q_max,q_min,i;while(scanf("%d",&n)!=EOF&&n){num=0;for(i=0;i<n;i++)scanf("%d",&t[i]);for(i=0;i<n;i++)scanf("%d",&q[i]);sort(t,t+n,cmp);sort(q,q+n,cmp);//两组马速度从大到小排序t_max=0;t_min=n-1;q_max=0;q_min=n-1; //代表速度最大最小的下标,比完一组,换下一组while(t_max<=t_min&&q_max<=q_min){if(t[t_max]>q[q_max]){num++;t_max++;q_max++;}else if(t[t_min]>q[q_min]){num++;t_min--;q_min--;}else{if(t[t_min]<q[q_max])num--;t_min--;q_max++;}}printf("%d\n",200*num);}return 0;}方法二:双端队列
#include<cstdio>#include<queue>#include<deque>#include<algorithm>using namespace std;int main(){int n,num,i,t,v1,v2;while(~scanf("%d",&n)&&n){num=0;deque<int> q1;deque<int> q2;for(i=1;i<=n;i++){scanf("%d",&t);q1.push_back(t); //表示从队尾插入数据}for(i=1;i<=n;i++){scanf("%d",&t);q2.push_back(t);}sort(q1.begin(),q1.end());//双端队列从小到大排序,两者分别表示队列的首末地址sort(q2.begin(),q2.end());for(i=1;i<=n;i++){if(q1.back()>q2.back())//比较队尾,队尾是好马,好马可以赢,果断去比{num+=200;q1.pop_back();//删除队尾元素,并不返回其值q2.pop_back();continue;//比完一组好马后,接着比下一组好马,不用进行下面内容}v1=q1.front();v2=q2.front();if(v1>v2)//比较队首,队首是笨马,笨马可以赢,果断去比{num+=200;q1.pop_front();//删除队首元素,不返回其值q2.pop_front();}else{ //田忌笨马和齐王好马比,拉齐王下水,if(q1.front()!=q2.back())//速度不一样,就是比人家低,输了num-=200;q1.pop_front();q2.pop_back();}}printf("%d\n",num);}return 0;}方法三:还是双端队列
#include<cstdio>#include<queue>#include<deque>#include<algorithm>using namespace std;int cmp(int A,int B){return A>B;}int main(){int n,num,i,t,v1,v2;while(~scanf("%d",&n)&&n){num=0;deque<int> q1;deque<int> q2;for(i=1;i<=n;i++){scanf("%d",&t);q1.push_front(t); //表示从队首插入数据}for(i=1;i<=n;i++){scanf("%d",&t);q2.push_front(t);}sort(q1.begin(),q1.end(),cmp);//双端队列从大到小排序,两者分别表示队列的首末地址sort(q2.begin(),q2.end(),cmp);for(i=1;i<=n;i++){if(q1.front()>q2.front())//比较队首,队首是好马,好马可以赢,果断去比{num+=200;q1.pop_front();//删除队首元素,并不返回其值q2.pop_front();continue;//比完一组好马后,接着比下一组好马,不用进行下面内容}v1=q1.back();v2=q2.back();if(v1>v2)//比较队尾,队尾是笨马,笨马可以赢,果断去比{num+=200;q1.pop_back();//删除队尾元素,不返回其值q2.pop_back();}else{ //田忌笨马和齐王好马比,拉齐王下水,if(q1.back()!=q2.front())//速度不一样,就是比人家低,输了num-=200;q1.pop_back();q2.pop_front();}}printf("%d\n",num);}return 0;}
相关文章推荐
- 【杭电oj】1052 - Tian Ji -- The Horse Racing(田忌赛马,贪心 & 双端队列)
- 田忌赛马 南阳oj364 HDU杭电1052【贪心】
- 杭电oj1789 Doing Homework again(贪心与队列完美碰撞)
- 【HDU】-1052-Tian Ji -- The Horse Racing(贪心,双端队列)
- 【Light-oj】-1198 - Karate Competition(贪心+双端队列)
- hdu 1052 田忌赛马(贪心)
- HDU 1052 田忌赛马(贪心)
- 【杭电oj】2111 - Saving HDU(贪心,水)
- 杭电hdu 1052 Tian Ji -- The Horse Racing 贪心
- HDU-1052-田忌赛马(贪心3)
- 【杭电oj】1702 - ACboy needs your help again!(栈和队列)
- 【杭电oj】5246 - 超级赛亚ACMer(二分 & 贪心 & STL)
- BZOJ 2457: [BeiJing2011]双端队列 贪心
- 【贪心】Bzoj 2457:[BeiJing2011]双端队列
- NYOJ 364 & HDU 1052 田忌赛马(贪心)
- 【杭电oj】5248 - 序列变换(贪心 & 二分)
- 杭电acm 1052 田忌赛马
- 【杭电oj】1873 - 看病要排队(优先队列,水)
- 【BZOJ2457】[BeiJing2011]双端队列 贪心+模拟
- hdu5380 贪心+双端队列