nyoj364 田忌赛马 贪心

来源:互联网 发布:手机怎么投诉淘宝店铺 编辑:程序博客网 时间:2024/05/16 06:49
田忌赛马
时间限制: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
来源
hdu
上传者

侯飒飒


思路:

因为马的顺序不是固定的,所以可以进行先排序

1:如果田忌的最大的马大于国王最大的马,那么胜场++,就要最大的去和国王最大的去战斗

2:如果田忌的最大的马小于国王最大的马,那么胜场--,就要最小的去和国王最大的去战斗

3:如果田忌的最大的马等于国王最大的马:

3.1:如果田忌最小的马大于国王最小的马,那么胜场++。就拿田忌最小的去和国王最小的去战斗

3.2:不然就拿田忌最小的马去和国王最大的马去战斗:此时如果田忌最小的马小于国王最大的马,胜场--;

代码如下:

#include<iostream>#include<cstring>#include<stdio.h>#include<algorithm>using namespace std;bool cmp(int a,int b){    return a>b;}int a[1001];int b[1001];int main(){    int n;    while(cin>>n)    {        if(n==0)            break;                for(int i=1; i<=n; i++)cin>>a[i];        for(int i=1; i<=n; i++)cin>>b[i];        sort(a+1,a+n+1,cmp);        sort(b+1,b+n+1,cmp);        int ans=0;        int i,j,ii,jj;        for( i=1,j=1,ii=n,jj=n; i<=ii;)        {            if(a[i]>b[j])            {                ans++;                i++;                j++;            }            else if(a[i]<b[j])            {                ans--;                j++;                ii--;            }            else            {                if(a[ii]>b[jj])                {                    ans++;                    ii--;                    jj--;                }                else                {                    if(a[ii]<b[j])                    {                        ans--;                    }                    ii--;                    j++;                }            }        }        printf("%d\n",ans*200);    }}


还一种思路:dp

我们假设下列贪心策略:

如果田忌剩下的马中最强的赢不了齐王剩下的马中最强的,那就让田忌最弱的去和齐王最强的去战斗

如果田忌剩下的马中最强的赢了齐王剩下的马中最强的,那就让田忌最强的去和齐王最强的去战斗

如果田忌剩下的马中最强的和齐王剩下的马中最强的一样,那么可以选择打平比赛或者是选择一个最差的马去输掉比赛



经过贪心德思考,可以发现:如果对于齐王的马从大到小排序,然后田忌的马也是从大到小排序,可以发现,对于齐王的这匹马,田忌总是选择最强的或者最弱的马去战斗,也就是从前面取或者从后面取

那么就有了转移方程

:首先预处理cij为田忌的i和齐王的j谁赢

然后就得出f[i][j]=max(f[i-1][j]+c[n-(i-j)+1][i],f[i-1][j-1]+c[j][i]);

fij表示战斗到第i场,从头取j场的最优解

代码如下,实在看不懂的举个例子代入看下就知道了,其实就是从头取或者从尾取得问题

 #include<iostream>#include<cstring>#include<stdio.h>#include<algorithm>using namespace std;int f[1001][1001],a[1001],b[1001],c[1001][1001];bool cmp(int a,int b){    return a>b;}int main(){    int n;    while(cin>>n)    {        int ans=-999999;        for(int i=0; i<=n; i++)            for(int j=0; j<=n; j++)                f[i][j]=-999999;        for(int i=1; i<=n; i++)cin>>a[i];        for(int i=1; i<=n; i++)cin>>b[i];        sort(a+1,a+n+1,cmp);        sort(b+1,b+n+1,cmp);                for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)                if(a[i]>b[j])c[i][j]=1;                else if(a[i]==b[j])c[i][j]=0;                else c[i][j]=-1;        f[0][0]=0;        for(int i=1; i<=n; i++)            for(int j=0; j<=i; j++)                if(j>=1)f[i][j]=max(f[i-1][j]+c[n-(i-j)+1][i],f[i-1][j-1]+c[j][i]);                else f[i][j]=f[i-1][j]+c[n-(i-j)+1][i];        for(int i=0; i<=n; i++)            ans=max(f[n][i],ans);        cout<<ans*200<<endl;;    }}        



0 0
原创粉丝点击