首页 > 其他分享 >POJ2287 Tian Ji -- The Horse Racing

POJ2287 Tian Ji -- The Horse Racing

时间:2022-08-15 21:58:39浏览次数:78  
标签:horses king Horse POJ2287 -- Tian int Ji 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 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?
img

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.

Input

The input consists of up to 50 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. The input ends with a line that has a single `0' after the last test case.

Output

For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

Sample Input

3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0

Sample Output

200
0
0

Source

Shanghai 2004

题解

知识点:贪心,区间dp。

首先是一个贪心的结论,双方从大到小排列,按qiwang马从大到小比,tianji比不过就用自己最小的马,比得过就用最大的马。

因为如果tianji最大的马都不能比过的qiwang最大的马时,肯定拿当前最小的马比;如果能比过,那么直接比就行,不需要找到最小能比过的马,因为能比过对方最大的马那意味着后面的马都能比过,取谁都一样。

随后就是个区间dp,设 \(dp[i][j]\) 为用区间 \([i,j]\) 的马比qiwang \([n-(j-i),n]\) 的马,赢一次加一,输一次减一,平均不动。

时间复杂度 \(O(n^2)\)

空间复杂度 \(O(n^2)\)

代码

#include <iostream>
#include <algorithm>


using namespace std;

int a[1007], b[1007], dp[1007][1007];

int cmp(int i, int j) {
    return a[i] == b[j] ? 0 : a[i] < b[j] ? -1 : 1;
}

bool ccmp(int a, int b) {
    return a > b;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n;
    while (cin >> n, n) {
        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, ccmp);
        sort(b + 1, b + n + 1, ccmp);
        for (int i = 1;i <= n;i++) dp[i][i] = cmp(i, n);
        for (int l = 2;l <= n;l++) {
            for (int i = 1;i <= n - l + 1;i++) {
                int j = i + l - 1;
                dp[i][j] = max(dp[i + 1][j] + cmp(i, n - l + 1), dp[i][j - 1] + cmp(j, n - l + 1));
            }
        }
        cout << dp[1][n] * 200 << '\n';
    }
    return 0;
}

标签:horses,king,Horse,POJ2287,--,Tian,int,Ji,problem
From: https://www.cnblogs.com/BlankYang/p/16589770.html

相关文章

  • ITK 空洞填充
       1、GrayscaleFillholeImageFilter填充孔洞,不对边缘造成影响(无平滑边缘效果)itk提供了两个的补洞类:itkGrayscaleFillholeImageFilter(针对灰度图像),itkB......
  • Day02 基本的Dos命令
    基本的DOS指令打开CMD的方式开始+系统+命令提示符win键+R输入cmd打开控制台(推荐使用)在任意的文件夹下面,按住shift键+鼠标右键点击,在此处打开命令行窗口资源管理器的......
  • 【MySQL】第2回 字符编码和字段类型
    目录1.字符编码与配置文件1.1\S1.2my.ini2.数据库存储引擎2.1定义2.2需要掌握的存储引擎2.3不同存储引擎之间底层文件的区别3.创建表的完整语法4.MySQL字段类型4.......
  • 7-11 关键活动
    假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的......
  • 35.概念/意义一致原则
    主语和谓语动词的一致关系并非只取决于语法上的单、复数形式,还取决于主语的单、复数意义。有时主语在语法形式上并非复数,但含有复数意义,其后的谓语动词应用复数形式;反之,主......
  • PHP CGI漏洞利用
    PHPCGI漏洞利用今日复现一个早期的漏洞,PHPCGI漏洞,代号为cve:2012-1823,该漏洞暂时只能攻击linux系统,属于linux漏洞使用工具:攻击机kalilinux、靶机metasploitablelinux......
  • 我和我最后的倔强
    我和我最后的倔强用这个作为这个博文的主题,此时的我觉得我还能抢救一下。原因如下:1.资金压力:目前口袋里还有不到2万块钱,如果年后找不到工作就得回家,不过朋友会建议我搬......
  • FFT学习笔记
    -1.前置知识基础的复数知识。0.什么是多项式乘法众所周知,多项式本质是一种特殊的函数,可以表示为自变量的若干次幂之和,即\[F(x)=\sum_{i=0}c_i\cdotx^i\]其中\(c_i......
  • StringBuffer和StringBuilder
    StringBuffer线程安全;执行效率:StringBuilder> StringBuffer>String ......
  • [2013年NOIP提高组] 积木大赛
    [2013年NOIP提高组]积木大赛思路:只需要进行n次循环,(两个数之间,如果后面的数大于前面的)用后面的数-前面的差值累加,就能得出操作次数啦~代码如下:#include<iostream>#incl......