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排序,然后想从左到右不断更新,其实这是错的...因为空中是个圆弧. 后 ...
随机推荐
- DedeCms 5.7友情链接模块注入漏洞
漏洞版本: DedeCms 5.7 漏洞描述: DedeCms基于PHP+MySQL的技术开发,是目前国内应用最广泛的php类CMS系统. DedeCms 5.7前台提交友情链接处,可以插入恶意JS代 ...
- c#集合类的线程安全
即位于System.Collections命名空间下的集合,如Hashtable,ArrayList,Stack,Queue等.其均提供了线程同步的一个实现 集合线程同步的问题 public clas ...
- tcxtreelist 控制单元格变颜色,或者行变颜色
如果控制单元格变颜色,只需要把注释的放开就可以了, 也就是判断当前列,是否是你想让变颜色的列. 如果想整行变颜色, 则只需要注释下面的就可以了. procedure TfrmwpOrderSendin ...
- [转]优化数据库大幅度提高Oracle的性能
几个简单的步骤大幅提高Oracle性能--我优化数据库的三板斧. 数据库优化的讨论可以说是一个永恒的主题.资深的Oracle优化人员通常会要求提出性能问题的人对数据库做一个statspack,贴出数据 ...
- 【解决】Oracle服务器ip地址被占用
数据库服务器ip地址被占用,怎么破?! 服务器: 1.改服务器ip: 2.改tnsnames.ora里配置的Oracle数据库ip: 3.重启Oracle服务: 客户端: 1.改tnsnames.or ...
- 【荐】Redis学习资料汇总
Redis学习手册(目录) - Stephen_Liu - 博客园 Redis 命令参考 — Redis 命令参考 Redis_php 学习 - 简单--生活 - 博客园
- 【三支火把】---C文件学习
---恢复内容开始--- 又看了一遍文件的知识点了,断断续续已经看了2-3遍,也就这次花了点时间做了一下总结,以后我想都不会再去翻书了,哈哈. 1. 基于缓冲区的文件操作2. 打开关闭文件3. 单个字 ...
- C#- WinForm获取 当前执行程序路径的几种方法
1.获取和设置当前目录的完全限定路径.string str = System.Environment.CurrentDirectory;Result: C:xxxxxx 2.获取启动了应用程序的可执行 ...
- JS/JQ综合总结
总结 js部分 一 语法结构 1 区分大小写 2注意 //单行 /*多行注释*/ 3子面量(直接量 literal) 12//数字 5.8//小数 "hello"字符串 true ...
- window nginx 启动无提示错误,却没有listen 80port
一直使用虚拟机来使用web+hostonly方式; 今天为了測试一个php平台的window系统兼容性, 在官方下载了window-nginx 1.9.1版本号; 解压到文件夹, 执行nginx.ex ...