【杭电OJ】--1052-田忌赛马(贪心,双端队列)
来源:互联网 发布:北京大学软件工程硕士 编辑:程序博客网 时间:2024/06/06 02:28
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 from the 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 you think of Tian Ji, the high ranked official in China?"
Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...
However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.
In this problem, you are asked to write a program to solve this special case of matching problem.
"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 from the 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 you think of Tian Ji, the high ranked official in China?"
Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...
However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.
In this problem, you are asked to write a program to solve this special case of matching problem.
392 83 7195 87 74220 2020 20220 1922 180
20000
思路:(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; }
0 0
- 【杭电OJ】--1052-田忌赛马(贪心,双端队列)
- 【杭电oj】1052 - Tian Ji -- The Horse Racing(田忌赛马,贪心 & 双端队列)
- 田忌赛马 南阳oj364 HDU杭电1052【贪心】
- 杭电1052 田忌赛马
- 杭电acm 1052 田忌赛马
- hdu 1052 田忌赛马(贪心)
- HDU 1052(贪心 田忌赛马)
- HDU 1052 田忌赛马(贪心)
- 田忌赛马(贪心)
- hrbust 哈理工oj 1570 田忌赛马【贪心】
- 杭电1052——田忌赛马
- 杭电OJ1052 田忌赛马
- HDUOJ-1052(poj-2287)(田忌赛马)(贪心)
- NYOJ 364 & HDU 1052 田忌赛马(贪心)
- HDU 1052 田忌赛马 (贪心_好题)
- NYOJ 364 田忌赛马 (贪心)
- POJ2287--贪心(田忌赛马)
- NYOJ-364 田忌赛马(贪心)
- 数学函数
- 学习C++:实践者的方法
- poj2245枚举排列(DFS)
- 开车旅行
- 程序员们的爱情表白书
- 【杭电OJ】--1052-田忌赛马(贪心,双端队列)
- SpringMVC的几种返回方式(get请求)
- adb server version (32) doesn't match this client (36); killing...
- mysql之触发器
- rep movsd ,N - move all of our dwords--【解决办法】
- 使用GridView控件分页显示数据
- 深入剖析Java中的装箱和拆箱
- 关于linux stat指令以及怎么用touch来更改三个文件属性中的时间
- T-SQL 数据语言操作(运算符),笔记