D题(贪心)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
"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
Output
Sample Input
Sample Output
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题(贪心)的更多相关文章
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- C#LeetCode刷题-贪心算法
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C ...
- 【构造题 贪心】cf1041E. Tree Reconstruction
比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...
- CodeForces 719B Anatoly and Cockroaches (水题贪心)
题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
- 集训第四周(高效算法设计)G题 (贪心)
G - 贪心 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Desc ...
- Codeforces Round #303 (Div. 2) D. Queue 水题贪心
题目: 题意:给你n个数值,要求排列这个序列使得第k个数值的前K-1个数的和>=第k个数值的个数尽可能多: #include <iostream> #include <cstd ...
- 思维题+贪心——牛客多校第一场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)^ ...
- CodeForces839-B. Game of the Rows-水题(贪心)
最近太zz了,老是忘记带脑子... 补的以前的cf,发现脑子不好使... B. Game of the Rows time limit per test 1 second memory limit ...
- hdu 6095 Rikka with Competition---思维题贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6095 题目大意: 任意两个人相比,相差大于K,分低的淘汰,否则两人都有可能赢,剩下的继续比,问有最多 ...
- 洛谷p1208 水题贪心 思想入门
题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...
随机推荐
- 外星人的供给站 (区间覆盖 t贪心)
/** 区间覆盖问题 分析: 每个点可以确定两个圆心 圆心的范围形成 一个区间 在这个区间上以任意一点画圆便可将此点 包含在内 如果有两个点所确定的区间相交了 说明这两个点可以用一个圆包含在内 即用一 ...
- 二次战CPP链表
Felling By Ruiy: Pre-learnning link list knowloages 熟悉 指针相关操作应用+结构体数据类型应用,且能简单融合使用,堆内存(内存泄露)->类似于 ...
- THOMAS MASON--英国顶级衬衫面料品牌
欧尚时光 THOMAS MASON--英国顶级衬衫面料品牌 2014-01-26 欧尚时光 今天为大家介绍英国顶级衬衫面料厂商:THOMAS MASON(托马斯·梅森).THOMAS MAS ...
- CentOS6.5 mini开启网络
1.编辑network配置 vim /etc/sysconfig/network-scripts/ifcfg-eth0 1 2 3 4 5 6 7 DEVICE=eth0 HWADDR=00:0C:2 ...
- 351. Android Unlock Patterns
这个题我真是做得想打人了卧槽. 题目不难,就是算组合,但是因为是3乘3的键盘,所以只需要从1和2分别开始DFS,结果乘以4,再加上5开始的DFS就行了. 问题是这个傻逼题目的设定是,从1到8不需要经过 ...
- 利用jks2pfx转换keystore格式的证书为pfs格式(含秘钥和证书的形式)
利用java语言写的openssl转换证书格式工具,使用方法如下所示: Java KeyStore文件转换为微软的.pfx文件和OpenSSL的PEM格式文件(.key + .crt)运行方式:JKS ...
- 深入理解java垃圾回收算法
Java虚拟机的内存区域中,程序计数器.虚拟机栈和本地方法栈三个区域是线程私有的,随线程生而生,随线程灭而灭:栈中的栈帧随着方法的进入和退出而进行入栈和出栈操作,每个栈帧中分配多少内存基本上是在类结构 ...
- java_method_Log输出日志的方法
package cn.com.qmhd.tools; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigura ...
- lvchange的available參数
available參数在man info help中均无此參数,事实上參数为:activate 写此此.值得用的人注意. available 參数实为: -a, --activate [a|e|l] ...
- log4j配置文件详解---转
使用步骤注意: 1. 从http://logging.apache.org/log4j/1.2/ 下载文件 2. 在src目录下加入log4j.properties,将jar包放入build -pat ...