nyoj 364——田忌赛马——————【贪心】

田忌赛马

时间限制:3000 ms  |  内存限制:65535 KB

难度:3

描述
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.

输入
The input consists of many test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses.
输出
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

样例输入
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
样例输出
200
0
0

题目大意:给田忌n匹马,给国王n匹马,输一轮减少200块钱,赢一轮增加200块钱,平局不奖惩。问最后最多能得到多少钱。

解题思路:如果田忌的慢马能赢国王的,就赢。如果比国王的慢,就拉国王的最快的马比,反正输,不如输的更有价值,为后边的马减小阻力。如果慢马一样快,首先看田忌的最快的马是不是比国王最快的马快,如果是就先让最快的马赢一局。如果不是,就让田忌的最慢的马跟国王的最快的马比较是不是相等,如果不是,那么就输一局;如果是,就让田忌最慢的马跟国王最快的马平局。
#include<bits/stdc++.h>
using namespace std;
int tj[1100],king[1100];
int main(){
    int t,i,j,k,tmp,sum,cnt,n,m,tslow,tfast,kslow,kfast;
    while(scanf("%d",&n)!=EOF){
        memset(tj,0,sizeof(tj));
        memset(king,0,sizeof(king));
        for(i=0;i<n;i++){
            scanf("%d",&tj[i]);
        }
        for(i=0;i<n;i++){
            scanf("%d",&king[i]);
        }
        sort(tj,tj+n);
        sort(king,king+n);
        tslow=kslow=0;
        tfast=kfast=n-1;
        m=k=0;
        while(m<n){
            if(tj[tslow]>king[kslow]){  //田忌慢马比国王慢马快
                tslow++;
                kslow++;
                k++;
            }else if(tj[tslow]<king[kslow]){//田忌慢马比国王慢马慢
                tslow++;
                kfast--;
                k--;
            }else{//两人慢马同速
                if(tj[tfast]>king[kfast]){//田忌快马比国王快马快
                    tfast--;
                    kfast--;
                    k++;
                }else {//田忌快马慢于或等于国王快马
                    if(tj[tslow]<king[kfast]){//田忌慢马比国王快马慢
                        kfast--;
                        tslow++;
                        k--;
                    }else if(tj[tslow]==king[kfast]){//田忌慢马等于国王快马
                        tslow++;
                        kfast--;
                    }
                }
            }
            m++;
        }
        cout<<k*200<<endl;
    }
    return 0;
}
/*
5
8 6 5 1 3
9 7 6 4 2
*/

  


时间: 2024-10-06 01:03:57

nyoj 364——田忌赛马——————【贪心】的相关文章

NYOJ 364 田忌赛马

田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 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

hdu 1052(田忌赛马 贪心算法,sort排序)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18155    Accepted Submission(s): 5281 Problem Description Here is a famous story in Chinese history. "That was about

POJ 3111 K Best &amp;&amp;NYOJ 914 (二分+ 贪心,最大化平均值)

链接:NYOJ:click here, POJ:click here 题意:(最大化平均值,挑战编程P143) 有n个物品的重量和价值分别是w[i]和v[i],从中选出K个物品使得单位重量的价值最大.(1<=k<=n<=10^41<=w[i],v[i]<=10^6) 一般想到的是按单位价值对物品排序,然后贪心选取,但是这个方法是错误的,比如对nyoj的例题来说,从大到小地进行选取,输入的结果是5/7=0.714对于有样例不满足.我们一般用二分搜索来做(其实这就是一个01分数规

[BZOJ 1034][ZJOI2008]泡泡堂BNB(类田忌赛马贪心)

http://www.lydsy.com:808/JudgeOnline/problem.php?id=1034 我会说这就是改版POJ的那道Tianji the horse racing么... 不过这个题的游戏规则略有些不同,赢了得2分,平了得1分,输了不扣分,所以贪心过程和POJ的那题略有一点不同,具体看代码吧. #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stri

hdu1052(田忌赛马 贪心)

好坑的一道题,不过确实是贪心的一道好题,想了好久一直无法解决平局的情况.  参考了别人的思路,然后结合了自己的想法,总算是想出来了. 这题有些步骤是必须要执行的,有四个步骤 一.当期状态田忌的最慢的马比对手最慢的马快,那么就直接比赢了这一盘,因为对手最慢的马比田忌所有马都慢,田忌为了后面的情况最优就用最小的代价来赢得这一个必胜的局. 二.当前状态田忌最慢的马比对手最慢的马慢,说明田忌最慢的马比对手所有的马慢,对于这个必败的情况田忌自然会用这匹马去耗对手最快的马,所以这局用最慢的马去和对手最快的马

[Poj2287][Tyvj1048]田忌赛马 (贪心+DP)

瞎扯 很经典的一道题 考前才打 我太菜了QAQ 就是先贪心排序了好 然后在DP 这样比直接DP更容易理解 (其实这题做法还有很多) 代码 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define N 1005 5 using namespace std; 6 int n,money,t[N],q[N]; 7 int f[N][N]; 8 bool cmp(int a,int b) 9 {

NYOJ~106~背包问题~贪心算法~

背包问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w<=10):如果给你一个背包它能容纳的重量为m(10<=m<=20),你所要做的就是把物品装到背包里,使背包里的物品的价值总和最大. 输入 第一行输入一个正整数n(1<=n<=5),表示有n组测试数据: 随后有n测试数据,每组测试数据的第一行有两个正整数s,m(1<=s<=10

C语言贪心(2)___田忌赛马(Hdu 1052)

Problem Description 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 t

HDU ACM 1052 Tian Ji -- The Horse Racing 贪心

#include<iostream> #include<algorithm> using namespace std; int main() //贪心 { int n,money,i,j,i1,j1; int a[1005],b[1005]; while(cin>>n && n) { for(i=0;i<n;i++) cin>>a[i]; for(i=0;i<n;i++) cin>>b[i]; sort(a,a+n);