HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论
题目链接:https://cn.vjudge.net/problem/HDU-1052
题意
田忌赛马问题扩展版
给n匹马,马的能力可以相同
问得分最大多少
思路
贪心做得还是太少,一开始一点思虑都没有的
这里稍微总结一下如何打开思路吧
- 从特殊位置开始考虑是否存在某种必然性,包括不限于序列首尾
- 若讨论难以进行,试着把这个讨论点展开,换个角度(或者换个特殊位置)讨论
首先排序
其次的关键是讨论尾元素是否必胜或必败,思考贪心
在一个关键是双方尾元素相同时,展开考虑首元素是否必胜或必败,思考贪心
当首尾元素各相等时,用我们最慢马比齐王最快马,这里需要认真证明,但可能考虑不到证法(这里可能需要总结规律?)。。。
提交过程
AC | 没有思路,看得以前的代码AC的:( |
代码
#include <cstdio>
#include <algorithm>
using namespace std;
int main(void){
int n, a[1020], b[1020];
while (scanf("%d", &n)==1 && n){
for (int i=0; i<n; i++) scanf("%d", &a[i]);
for (int i=0; i<n; i++) scanf("%d", &b[i]);
sort(a, a+n); sort(b, b+n);
int head=0, last=n-1, alast=n-1, i=0, cnt=0;
while (i<=alast){
if (b[i]<a[head]){
head++; cnt++; i++;
}else if (b[i]>a[head]){
head++; alast--; cnt--;
}else if (b[i]==a[head]){
if (b[alast]<a[last]){
alast--; last--; cnt++;
}else if (b[alast]>a[last]){
alast--; head++; cnt--;
}else{
if (a[head]<b[alast]) cnt--;
else if (a[head]>b[alast]) cnt++;
alast--; head++;
}
}
}printf("%d\n", cnt*200);
}
return 0;
}
Time | Memory | Length | Lang | Submitted |
---|---|---|---|---|
46ms | 1596kB | 970 | G++ | 2018-06-23 11:35:55 |
HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论的更多相关文章
- HDU 1052 Tian Ji -- The Horse Racing(贪心)
题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...
- 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的边:如果输 ...
- 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 (田忌赛马)
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(贪心)(2004 Asia Regional Shanghai)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...
- hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】
题目 这道题主要是需要考虑到各种情况:先对马的速度进行排序,然后分情况考虑: 1.当田忌最慢的马比国王最慢的马快则赢一局 2.当田忌最快的马比国王最快的马快则赢一局 3.当田忌最快的马比国王最快的马慢 ...
- 杭州电 1052 Tian Ji -- The Horse Racing(贪婪)
http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】
思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...
随机推荐
- thinkphp5 模板中截取中文字符串
TP5模板页截取中文字符串 {$vo.task_detail|mb_substr=###,0,15,'utf-8'}
- POSIX Threads
POSIX Threads, usually referred to as pthreads, is an execution model that exists independently from ...
- DDD中Dto领域驱动设计概述,摘自《NET企业级应用架构设计》
- sklearn学习6----交叉验证
1.kfold:自己分样本来交叉验证迭代 导入模块:from sklearn.model_selection import KFold 参数: KFold(n_splits=3, shuffle=Fa ...
- C语言基础 (4) 原码反码补码与数据类型
1.回顾 使用gcc编译代码 gcc hello.c -o hello windows下编译代码 C语言编译步骤: 预处理(头文件展开,干掉注释) gcc -E hello.c -o hello.i ...
- Vue 实现前进刷新,后退不刷新的效果
需求一: 在一个列表页中,第一次进入的时候,请求获取数据.点击某个列表项,跳到详情页,再从详情页后退回到列表页时,不刷新.也就是说从其他页面进到列表页,需要刷新获取数据,从详情页返回到列表页时不要刷新 ...
- Nodejs之旅開始
web前端是一个门槛低,但精通起来比較难的行业,由于它涉及的范围比較广,也许在十年前.我光靠切图,就能找到一个好的职位,可是如今,仅仅会切图.我们非常难找到自己惬意的工作,如今前端职位要求不仅是htm ...
- Windows下面使用curl
Windows下面使用curl 学习了:https://www.cnblogs.com/xing901022/p/4652624.html 下载地址:https://curl.haxx.se/down ...
- [Hyperapp] Render Text with JSX in Hyperapp
Hyperapp is an ultra lightweight (1kb), minimal, functional, JavaScript library for building UIs. It ...
- Oracle 学习笔记 13 -- 控制用户权限
数据库控制语言的功能室控制用户对数据库的存取权限. 用户对某类数据具有何种操作权限是有DBA决定的.Oracle 通过GRANT语句完毕权限的授予,通过REVOKE语句完毕对权限的收回. 权限分为系统 ...