hdu题面

解题思路

首先我们要选一个起点,这个起点应该在第一个区间内,然后再看第二个区间在左边还是右边以便移动,但转念一想,我们可以把起点直接选在前一堆区间的交集上,于是思路就有了——依次把所有区间取交集,如果没有交集就搞一个新的区间,之后的接着取交集,得到一堆合并出来的区间。然后就在合并的区间上移动就好。

有一个细节要注意。举个例子,如果现在位置在\(3\),之后的两个目标区间依次是\([6,7]\)、\([9,10]\),那么我们如果经过两次移动(先1后2)到达第一个区间的左端点\(6\),下一步就要再移动两次才能到达\(9\),所以第一步不妨用相同的代价(两次移动)多走点,走到7,那么第二步到\(9\)就只用移动一次就好。

比赛时合并区间写的有问题,WA*7,最后两题惨淡收场……

源代码

  1. #include<cstdio>
  2. #include<algorithm>
  3. int T,n;
  4. struct D{
  5. int a,b;
  6. }p[1010];
  7. int num;
  8. int l,r;
  9. int main()
  10. {
  11. scanf("%d",&T);
  12. while(T--)
  13. {
  14. num=0;
  15. scanf("%d%d%d",&n,&l,&r);
  16. for(int i=1,a,b;i<n;i++)
  17. {
  18. scanf("%d%d",&a,&b);
  19. if(a>r||b<l)
  20. {
  21. p[num++]={l,r};
  22. l=a;
  23. r=b;
  24. }
  25. else
  26. {
  27. l=std::max(l,a);
  28. r=std::min(b,r);
  29. }
  30. if(i==n-1) p[num++]={l,r};//比赛时这句话被我写到循环外面去了……
  31. }
  32. int pos;
  33. if(p[0].b<p[1].a) pos=p[0].b;
  34. else pos=p[0].a;
  35. long long ans=0;
  36. for(int i=1;i<num;i++)
  37. {
  38. if(pos<p[i].a)//向右走
  39. {
  40. int delta=p[i].a-pos;
  41. pos=p[i].a;
  42. ans+=delta>>1;
  43. if(delta&1)
  44. {
  45. ans++;
  46. if(i+1<num&&pos+1<=p[i].b&&p[i+1].a>pos) pos++;
  47. }
  48. }
  49. else//向左走
  50. {
  51. int delta=pos-p[i].b;
  52. pos=p[i].b;
  53. ans+=delta>>1;
  54. if(delta&1)
  55. {
  56. ans++;
  57. if(i+1<num&&pos-1>=p[i].a&&p[i+1].b<pos) pos--;
  58. }
  59. }
  60. }
  61. printf("%lld\n",ans);
  62. }
  63. return 0;
  64. }

HDU 6669 Game的更多相关文章

  1. 2019 年百度之星·程序设计大赛 - 初赛一Game HDU 6669 (实现,贪心)

    Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  2. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  5. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  7. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

随机推荐

  1. ASP.NET Core中使用EasyCaching作为缓存抽象层

    ⒈是什么? 和CacheManager差不多,两者的定位和功能都差不多. EasyCaching主要提供了下面的几个功能 统一的抽象缓存接口 多种常用的缓存Provider(InMemory,Redi ...

  2. HDU 6662 Acesrc and Travel 换根DP,宇宙最傻记录

    #include<bits/stdc++.h> typedef long long ll; using namespace std; const int maxn=1e6+50; cons ...

  3. 从入门到自闭之Python高阶函数

    高阶函数:内部帮忙做了一个for循环 filter:筛选过滤 语法: filter(function,iterable) function: 1.指定过滤规则(函数的内存地址) 2.用来筛选的函数,在 ...

  4. 纯H5 AJAX文件上传加进度条功能

    上传代码js部分 //包上传 $('.up_apk').change(function () { var obj = $(this); var form_data = new FormData(); ...

  5. springboot2整合zookeeper集成curator

    步骤: 1- pom.xml <dependency> <groupId>org.apache.curator</groupId> <artifactId&g ...

  6. 第三章 联接查询 T-SQL语言基础

    联接查询 sql server 2008支持四种表运算符----JOIN,APPLY,PIVOT,UNPIVOT. JOIN表运算符是ANSI标准,而APPLY,PIVOT,UNPIVOT是T-SQL ...

  7. tomcat进行压测时,cpu占用90%

    1.top 命令查看占用cpu高的进程,pid=15019 2.查看该进程下所有占用cppu高的线程 top -Hp pid   即:top -Hp 15019 得到pid 3.获取15030的16进 ...

  8. 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 ...

  9. 网速监控-nload

    用来监控系统网卡实时网速的. 安装 yum install nload -y # 或 apt install nload -y 使用 # 直接运行默认监控第一个网卡, 使用上下方向键来切换网卡. nl ...

  10. PyTorch安装问题解决

    现在caffe2被合并到了PyTorch中 git clone https://github.com/pytorch/pytorch pip install -r requirements.txtsu ...