HDU 6669 Game
解题思路
首先我们要选一个起点,这个起点应该在第一个区间内,然后再看第二个区间在左边还是右边以便移动,但转念一想,我们可以把起点直接选在前一堆区间的交集上,于是思路就有了——依次把所有区间取交集,如果没有交集就搞一个新的区间,之后的接着取交集,得到一堆合并出来的区间。然后就在合并的区间上移动就好。
有一个细节要注意。举个例子,如果现在位置在\(3\),之后的两个目标区间依次是\([6,7]\)、\([9,10]\),那么我们如果经过两次移动(先1后2)到达第一个区间的左端点\(6\),下一步就要再移动两次才能到达\(9\),所以第一步不妨用相同的代价(两次移动)多走点,走到7,那么第二步到\(9\)就只用移动一次就好。
比赛时合并区间写的有问题,WA*7,最后两题惨淡收场……
源代码
#include<cstdio>
#include<algorithm>
int T,n;
struct D{
int a,b;
}p[1010];
int num;
int l,r;
int main()
{
scanf("%d",&T);
while(T--)
{
num=0;
scanf("%d%d%d",&n,&l,&r);
for(int i=1,a,b;i<n;i++)
{
scanf("%d%d",&a,&b);
if(a>r||b<l)
{
p[num++]={l,r};
l=a;
r=b;
}
else
{
l=std::max(l,a);
r=std::min(b,r);
}
if(i==n-1) p[num++]={l,r};//比赛时这句话被我写到循环外面去了……
}
int pos;
if(p[0].b<p[1].a) pos=p[0].b;
else pos=p[0].a;
long long ans=0;
for(int i=1;i<num;i++)
{
if(pos<p[i].a)//向右走
{
int delta=p[i].a-pos;
pos=p[i].a;
ans+=delta>>1;
if(delta&1)
{
ans++;
if(i+1<num&&pos+1<=p[i].b&&p[i+1].a>pos) pos++;
}
}
else//向左走
{
int delta=pos-p[i].b;
pos=p[i].b;
ans+=delta>>1;
if(delta&1)
{
ans++;
if(i+1<num&&pos-1>=p[i].a&&p[i+1].b<pos) pos--;
}
}
}
printf("%lld\n",ans);
}
return 0;
}
HDU 6669 Game的更多相关文章
- 2019 年百度之星·程序设计大赛 - 初赛一Game HDU 6669 (实现,贪心)
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
随机推荐
- ASP.NET Core中使用EasyCaching作为缓存抽象层
⒈是什么? 和CacheManager差不多,两者的定位和功能都差不多. EasyCaching主要提供了下面的几个功能 统一的抽象缓存接口 多种常用的缓存Provider(InMemory,Redi ...
- HDU 6662 Acesrc and Travel 换根DP,宇宙最傻记录
#include<bits/stdc++.h> typedef long long ll; using namespace std; const int maxn=1e6+50; cons ...
- 从入门到自闭之Python高阶函数
高阶函数:内部帮忙做了一个for循环 filter:筛选过滤 语法: filter(function,iterable) function: 1.指定过滤规则(函数的内存地址) 2.用来筛选的函数,在 ...
- 纯H5 AJAX文件上传加进度条功能
上传代码js部分 //包上传 $('.up_apk').change(function () { var obj = $(this); var form_data = new FormData(); ...
- springboot2整合zookeeper集成curator
步骤: 1- pom.xml <dependency> <groupId>org.apache.curator</groupId> <artifactId&g ...
- 第三章 联接查询 T-SQL语言基础
联接查询 sql server 2008支持四种表运算符----JOIN,APPLY,PIVOT,UNPIVOT. JOIN表运算符是ANSI标准,而APPLY,PIVOT,UNPIVOT是T-SQL ...
- tomcat进行压测时,cpu占用90%
1.top 命令查看占用cpu高的进程,pid=15019 2.查看该进程下所有占用cppu高的线程 top -Hp pid 即:top -Hp 15019 得到pid 3.获取15030的16进 ...
- Yii2 常用代码集合
Yii2.0 对数据库查询的一些简单的操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- 网速监控-nload
用来监控系统网卡实时网速的. 安装 yum install nload -y # 或 apt install nload -y 使用 # 直接运行默认监控第一个网卡, 使用上下方向键来切换网卡. nl ...
- PyTorch安装问题解决
现在caffe2被合并到了PyTorch中 git clone https://github.com/pytorch/pytorch pip install -r requirements.txtsu ...