【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing
田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧
Descriptions:
田忌和齐王赛马,他们各有n匹马,依次派出一匹马比赛,赢了加200,输了减200,平局不加钱,问如何安排马的出场顺序,使得田忌赢的钱最多
Input
输入最多包含 50 组测试数据。对于每组测试数据,第一行包括一个正整数 n (n ≤ 1000),表示每一方的马的数目。第二行中的 n 个整数,表示田忌的马的速度。第三行中的 n 个整数,表示齐王的马的速度。在最后一组测试数据之后,是只包含单个 0 的一行。
Output
对于每组测试数据,输出包含单个数的一行,表示田忌将从齐王那里赢得银两的最大值。
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
题目链接:
https://vjudge.net/problem/OpenJ_Bailian-2287
题解都在代码上
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
int n;
int a[];
int b[];
int main()
{
while(cin>>n,n)
{
ME0(a);
ME0(b);
int sum=;
for(int i=; i<n; i++)
cin>>a[i];//田忌的马
for(int i=; i<n; i++)
cin>> b[i];//齐王的马
sort(a,a+n);//排序
sort(b,b+n);
int l1=;
int l2=;
int r1=n-;
int r2=n-;
while(l1<=r1)
{
//田忌最快的马比齐王最快的马快,则两者比
if(a[r1]>b[r2])
{
sum+=;
--r1;
--r2;
}
//田忌最快的马比齐王最快的马慢,则用田忌最慢的马和齐王最快的马比
if(a[r1]<b[r2])
{
sum-=;
++l1;
--r2;
}
//速度相等
if(a[r1]==b[r2])
{
//田忌最慢的马比齐王最慢的马快,则这两匹马比
if(a[l1]>b[l2])
{
sum+=;
++l1;
++l2;
}//否则,用田忌最慢的马和齐王最快的马比
else
{
if(a[l1]<b[r2])//田忌最慢的马比齐王最快的马快
sum-=;//加200
++l1;//否则平局
--r2;
}
}
}
cout<<sum<<endl;
}
}
【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)的更多相关文章
- POJ-2287.Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 17662 Acc ...
- 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 ...
- POJ 2287 Tian Ji -- The Horse Racing(贪心)
题意:田忌和齐王有n匹马,进行n局比赛,每局比赛输者给胜者200,问田忌最多能得多少钱. 分析:如果田忌最下等的马比齐王最下等的马好,是没必要拿最下等的马和齐王最好的马比的.(最上等马同理) 因此,如 ...
- (贪心5.1.2)POJ 2287 Tian Ji -- The Horse Racing
/* * POJ_2287.cpp * * Created on: 2013年10月9日 * Author: Administrator */ #include <iostream> #i ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)
题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...
- UVaLive 3266 Tian Ji -- The Horse Racing (贪心)
题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...
- HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论
题目链接:https://cn.vjudge.net/problem/HDU-1052 题意 田忌赛马问题扩展版 给n匹马,马的能力可以相同 问得分最大多少 思路 贪心做得还是太少,一开始一点思虑都没 ...
- UVa LA 3266 - Tian Ji -- The Horse Racing 贪心,不只处理一端,也处理另一端以理清局面 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 【贪心】[hdu1052]Tian Ji -- The Horse Racing(田忌赛马)[c++]
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 什么是 AQS ?
1.什么是AQS? AQS是英文单词AbstractQueuedSynchronizer的缩写,翻译过来就是队列同步器. 它是构建锁或者其他同步组件的基础框架(如ReentrantLock.Reent ...
- mybatis入门(八)
mybatis入门---更新和删除 <!-- 删除用户 --> <delete id="deleteUser" parameterType="java. ...
- charles刷分微信跳一跳小程序对https的理解
以前以为只要安装了https 客户端与服务端的数据会被加密就安全了 事实上 只要任意一款抓包工具 并伪造证书 就可以解密这个被所谓https加密的数据 如 可以下载charles的根证书 作为伪 ...
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或
题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...
- html页面表格导出到excel总结
转载:http://www.cnblogs.com/liuguanghai/archive/2012/12/31/2840262.html <table id="tableExcel& ...
- BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每 ...
- CI核心文件分析之基准测试类 (Benchmark.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter * * ...
- RequireJS 加载 js 执行顺序
初次接触RequireJS 对文档理解不很透彻,自己通过测试测到的执行顺序: 文档结构: |-amaze | -js | -amazeui.js | -jquery.min.js | -main.js ...
- codeforces 701A A. Cards(水题)
题目链接: A. Cards 题意: 问两个数的和相同,怎么组合; AC代码: #include <iostream> #include <cstdio> #include & ...
- BZOJ-4488:最大公约数(GCD)
给定一个长度为 N 的正整数序列Ai对于其任意一个连续的子序列{Al,Al+1...Ar},我们定义其权值W(L,R )为其长度与序列中所有元素的最大公约数的乘积,即W(L,R) = (R-L+1) ...