D - D

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 
 

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?"

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
 
 
题解:田忌赛马
三种情况

1.当田忌最快的马比齐王最快的马快时,用田忌最快的  马赢齐王最快的马

2.当田忌最快的马比齐王最快的马慢时,用田忌最慢的  马输给齐王最快的马

3.当田忌最快的马跟齐王最快的马一样快时,分情况讨  论

a.当田忌最慢的马比齐王慢快的马快时,用田忌最慢的马赢齐王最慢的马比

b.当田忌最慢的马比齐王最慢的马慢时,用田忌最慢的马和齐王最快的马比

 
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int t;
int total,co;
int a[],b[];
bool cmp(int a1,int a2)
{
return a1>a2;
}
int main()
{
while(cin>>t&&t)
{
for(int i=; i<t; i++)
cin>>a[i];
for(int i=; i<t; i++)
cin>>b[i];
sort(a,a+t,cmp);
sort(b,b+t,cmp);
total=;
co=;
int p=,q=;
int x=t-,y=t-;
for(int i=; i<t; i++)
{
co=-;
if(a[p]>b[q])
{
co=;
p++;
q++;
}
else
{
if(a[x]>b[y])
{
co=;
x--;
y--;
}
else
{
if(a[x]==b[q]) co=;
else
co=-;
x--;
q++;
}
}
total+=co;
}
cout<<total*<<endl;
}
return ;
}

D题(贪心)的更多相关文章

  1. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  2. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C ...

  3. 【构造题 贪心】cf1041E. Tree Reconstruction

    比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...

  4. CodeForces 719B Anatoly and Cockroaches (水题贪心)

    题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...

  5. 集训第四周(高效算法设计)G题 (贪心)

    G - 贪心 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Desc ...

  6. Codeforces Round #303 (Div. 2) D. Queue 水题贪心

    题目: 题意:给你n个数值,要求排列这个序列使得第k个数值的前K-1个数的和>=第k个数值的个数尽可能多: #include <iostream> #include <cstd ...

  7. 思维题+贪心——牛客多校第一场C

    /* 给定一组n维向量 A=(a1/m,a2/m,a3/m ... an/m), 求另一个n维向量 P=(p1,p2,p3...pn),满足sum{pi}=1,使得ans=sum{(ai/m-pi)^ ...

  8. CodeForces839-B. Game of the Rows-水题(贪心)

    最近太zz了,老是忘记带脑子... 补的以前的cf,发现脑子不好使...   B. Game of the Rows time limit per test 1 second memory limit ...

  9. hdu 6095 Rikka with Competition---思维题贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6095 题目大意: 任意两个人相比,相差大于K,分低的淘汰,否则两人都有可能赢,剩下的继续比,问有最多 ...

  10. 洛谷p1208 水题贪心 思想入门

    题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...

随机推荐

  1. bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Sta ...

  2. NOI题库1799 最短前缀

    1799:最短前缀 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的字串是: &quo ...

  3. 总结&计划

    最近完成的事儿: 1. 看完了<c专家编程>并且整理了读书笔记 2. 看了半章<大数据>发现这本书里面是纯纯的干货...习题需要认真做,算法需要仔细体会...不适合突击,尤其是 ...

  4. Servlet的一些细节(1)

    1.  Servlet程序必须映射到一个URL地址 由于客户端是通过URL访问web服务器资源,所以Servlet程序必须映射到一个URL地址.这个工作在web.xml文件中使用<servlet ...

  5. XPath与Xquery

    XPath 和 XQuery 在某些方面很相似.XPath 还是 XQuery 完整不可分割的一部分.这两种语言都能够从 XML 文档或者 XML 文档存储库中选择数据.本文简要介绍了 XPath 和 ...

  6. 关于连不上dc服务器的机器强行退出域的方法

    有时加入域的计算机无法连接到之前的域了,只能强制退出域了,有两个前提条件:   1.断开网络,就是拔掉网线或者禁用网卡.   2.使用本地管理员登陆.   然后命令行执行如下命令即可:   netdo ...

  7. nginx 请求负载 转发规则设置

    (1)轮询(默认) weight=5;         #本机上的Squid开启3128端口,不是必须要squid         server 192.168.8.2x:80    weight=1 ...

  8. leetcode第一刷_Interleaving String

    有关这样的字符串的题真是层出不穷啊,并且他们都有这样一个特点,就是递归的思路如此简单,但一定超时! 这个时候,dp就朝我们缓缓走来.递归超,dp搞!这道题的状态转移方程还是比較好写的,用ispart[ ...

  9. android studio c++ 自动补全

    这两天弄起来了Android ndk,可这东西的配置实在是个问题.对于Eclipse可以通过makefile进行编译,也比较成熟.但是对Android studio来说就蛋疼了,官方是想通过gradl ...

  10. 【小TIP】记录各种错误【更新中】

    最好程序一遍通过,为了提高代码能力,这里将用TIP的形式记录来犯过的错误.不断更新中. *已经转移到闪存.. [150214]WA:检查是否数组开小了. [150212]WA:如果程序中有乘号,需要留 ...