田忌赛马

时间限制: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 贪心算法
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std; int main(){
int n;
while(cin >> n){
vector<int> tian(n),king(n);
for(int i = ; i < n ; ++ i ) cin >> tian[i];
for(int i = ; i < n ; ++ i ) cin >> king[i];
sort(tian.begin(),tian.end());
sort(king.begin(),king.end());
int leftTian = ,leftKing = , rightTian = tian.size()-, rightKing = king.size()-, sum = ;
while(leftTian <= rightTian){
if(tian[rightTian] > king[rightKing]){
rightTian--;rightKing--;
sum += ;
}else if(tian[leftTian] > king[leftKing]){
leftTian++;leftKing++;
sum += ;
}else{
if(tian[leftTian] < king[rightKing]) sum-=;
leftTian++;rightKing--;
}
}
cout<<sum<<endl;
}
}

 

ACM 田忌赛马的更多相关文章

  1. nyoj 364 田忌赛马(贪心)

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  2. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  3. HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. D - D 田忌赛马

    D - D    田忌赛马   解题报告 hdu 1052 Tian Ji -- The Horse Racing 链接:http://acm.hust.edu.cn/vjudge/contest/v ...

  5. HDOJ-1052 田忌赛马(贪心)

    田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述: Here is a famous story in Chinese history. "That was ...

  6. ACM课程总结

    当我还是一个被P哥哥忽悠来的无知少年时,以为编程只有C语言那么点东西,半个学期学完C语言的我以为天下无敌了,谁知自从有了杭电练习题之后,才发现自己简直就是渣渣--咳咳进入正题: STL篇: 成长为一名 ...

  7. nyoj 364——田忌赛马——————【贪心】

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  8. hdu1052 田忌赛马 —— 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 错误代码: #include<stdio.h>//田忌赛马,错误版 #include ...

  9. 牛人的ACM经验 (转)

    一:知识点     数据结构:       1,单,双链表及循环链表       2,树的表示与存储,二叉树(概念,遍历)二叉树的                    应用(二叉排序树,判定树,博弈 ...

随机推荐

  1. MVC学习笔记---MVC框架执行顺序

    一.把路由添加到路由表, 二.注册ControllerBuilder(老板)和默认工厂(DefaultControllerFactory) 2.1默认工厂获取可以创建的Controller. 三.由于 ...

  2. Java 内存区域和GC机制

    目录 Java垃圾回收概况 Java内存区域 Java对象的访问方式 Java内存分配机制 Java GC机制 垃圾收集器 Java垃圾回收概况 Java GC(Garbage Collection, ...

  3. 七牛---以一个七牛上传的实例小结下AJAX跨域【转】

    http://blog.csdn.net/netdxy/article/details/50699842 使用七牛过程中,很多用户或多或少遇到跨域的问题,这篇文章主要介绍下跨域的概念来看什么情况下会出 ...

  4. 你可能不知道的java、python、JavaScript以及jquary循环语句的区别

    一.概述 java循环语句分为四种形式,分别是 while, do/while, for, foreach: python中循环语句有两种,while,for: JavaScript中循环语句有四种, ...

  5. oracle 11g r1 RAC增加新节点

    在一套两节点的rac上增加一个新的节点,详细的操作记录如下:   已有节点RAC1,RAC2 一,环境及版本: 公司环境:Vmware Esxi 5.5  操作系统:Redhat 5.8 x86_64 ...

  6. OpenMesh 删除网格顶点

    OpenMesh 提供了 delete_vertex() 函数来实现从网格中删除顶点,在删除掉顶点的同时,所有与该顶点相连的边也同时被删除. OpenMesh 官方文档 中给的顶点删除函数声明如下: ...

  7. Sp EF输出 临时表

    -- ============================================= -- Author: <Author,,Name> -- Create date: < ...

  8. SQLserver删除某数据库中所有表 方法 二

    方便删除数据库中所有的数据表,清空数据库,有些有约束,不能直接delete,需要先删除库中的约束,代码如下: --删除所有约束DECLARE c1 cursor for select 'alter t ...

  9. background为圆角的表框,dp转Px,Px转dp

    圆角边框<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="ht ...

  10. FastMM 定位内存泄露的代码位置

    FastMM 定位内存泄露的代码位置 开源的FastMM,使用很简单,在工程的第一行引用FastMM4即可(注意,一定要在第一个Uses的位置),可以在调试程序时提示内存泄露情况,还可以生成报告. 在 ...