HDU-1052(贪心策略)
Tian Ji -- The Horse Racing
"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.
分析:题意就是田忌赛马的扩展,田忌和国王每个人有n匹马,然后两个人每匹马两两对应匹配,谁的马速度快谁就赢一局,求怎么匹配使得田忌赢的局数尽可能多。
题目有个误解让人以为是二分图最大匹配,其实是个贪心,但对我来说不好想。分三种情况:
1.田忌最慢的马比国王最慢的马还慢,则拿去与国王最快的马PK,反正都是输,不如拿去把对方最快的比掉,则后面马赢的机会就更大。
2.田忌最慢的马比国王最慢的马快,则田忌赢一局。
3.田忌最慢的马与国王最慢的马速度相等,则不能打平,因为打平与一平一负效果等价,还不如把田忌最慢的马去与国王最快的马比,为后面的马争取更大的赢的机会。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
#define INF 100000
typedef long long ll;
const int maxn=;
int tian[maxn],king[maxn];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
while(scanf("%d",&n)){
if(n==) break;
memset(tian,,sizeof(tian));
memset(king,,sizeof(king));
for(int i=;i<n;i++)
scanf("%d",&tian[i]);
for(int i=;i<n;i++)
scanf("%d",&king[i]);
sort(tian,tian+n);
sort(king,king+n);
int win=,lose=;
int t_low=,t_best=n-,k_low=,k_best=n-;
while(t_low<=t_best){
if(tian[t_low]>king[k_low]){
win++;
t_low++;
k_low++;
}
else if(tian[t_low]<king[k_low]){
lose++;
t_low++;
k_best--;
}
else{
if(tian[t_best]>king[k_best]){
win++;
t_best--;
k_best--;
}
else{
if(tian[t_low]<king[k_best])
lose++;
t_low++;
k_best--;
}
}
}
int ans=(win-lose)*;
printf("%d\n",ans);
}
return ;
}
HDU-1052(贪心策略)的更多相关文章
- HDU 1052 贪心+dp
http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...
- HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】
算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...
- E - 不爱学习的lyb HDU - 1789(贪心策略)
众所周知lyb根本不学习.但是期末到了,平时不写作业的他现在有很多作业要做. CUC的老师很严格,每个老师都会给他一个DDL(deadline). 如果lyb在DDL后交作业,老师就会扣他的分. 现在 ...
- Tian Ji -- The Horse Racing HDU - 1052
Tian Ji -- The Horse Racing HDU - 1052 (有平局的田忌赛马,田忌赢一次得200块,输一次输掉200块,平局不得钱不输钱,要使得田忌得到最多(如果只能输就输的最少) ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- hdu 1009 贪心基础题
B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bi ...
- LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)
Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...
- HDU - 1789 贪心
贪心策略:按照分数降序排列,如果分数相同将截止时间早的排在前面.每次让作业尽量晚完成,因此需要逆序枚举判断这一天是否已经做了其他作业,如果没时间做这个作业说明不能完成,否则将这一天标记. AC代码 # ...
- poj1328 Radar Installation(贪心 策略要选好)
https://vjudge.net/problem/POJ-1328 贪心策略选错了恐怕就完了吧.. 一开始单纯地把island排序,然后想从左到右不断更新,其实这是错的...因为空中是个圆弧. 后 ...
随机推荐
- ZOJ 3790 Consecutive Blocks
大致题意就是给你一个数列,让你最多移除K个数字,使得连续的相同数字的长度最大,并求出最大长度. 我们将此序列按颜色排序,颜色相同的话按位置排序,那么排完序之后颜色相同的blocks就在一起,只是他们的 ...
- Python:变量与字符串
变量 使用dos页面进行命令的输入如下变量,进行打印: 同时,相同两个变量书写在同一行,中间用英文的“;”隔开 python中区分大小写变量 字符串 简单的说,字符串就是双引号,单引号,或者三 ...
- Content-Type伪装 - 将jsp伪装成css
一.前期理论准备 1)目的: 在jsp中动态生成css语句,然后输出给浏览器解析.渲染. 2)浏览器解析文件的依据: 页面加载后,浏览器会发起各个请求去下载各种资源. 比如下载css文件,然后根 ...
- 决策树及其python实现
剪枝 由于悲观错误剪枝 PEP (Pessimistic Error Pruning).代价-复杂度剪枝 CCP (Cost-Complexity Pruning).基于错误剪枝 EBP (Error ...
- Bzoj 1565: [NOI2009]植物大战僵尸 最大权闭合图,拓扑排序
题目: http://cojs.tk/cogs/problem/problem.php?pid=410 410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:p ...
- Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
题目 : http://www.tsinsen.com/A1517 A1517. 动态树 时间限制:3.0s 内存限制:1.0GB 总提交次数:227 AC次数:67 平均分:49. ...
- java操作pdf添加页眉条码添加水印图片
添加条码页眉以及图片水印 1. 引入jar包 1. itext-4.2.1.jar 2. itext-asian-5.2.0.jar 3. jbarcode-0.2.8.jar ...
- Hadoop学习记录(4)|MapReduce原理|API操作使用
MapReduce概念 MapReduce是一种分布式计算模型,由谷歌提出,主要用于搜索领域,解决海量数据计算问题. MR由两个阶段组成:Map和Reduce,用户只需要实现map()和reduce( ...
- HDOJ-ACM1020(JAVA)
题意:给字母计数,如果是字母后面没有相同的,原样输出,如果有则输出这个字母的个数和字母本身. import java.util.*; import java.io.*; public class Ma ...
- Native code - how to get function call stack (backtrace) programatically 附带源代码
自己根据 https://github.com/zhuowei/libcorkscrew-ndk 上的库做了一个包装库并附带使用的例子(executable 分支),具体代码在自己的代码仓库里,名字叫 ...