题意:

给出一个数组,删除大于等于k的数字,使得其满足以下条件:

1.剩余的连续的段,每一段的长度相等;

2.在满足第一个条件的情况下,段数尽可能多;

3.在满足前两个条件的情况下,k取最小的。

求k。

思路:

一开始整个数组可以看成完整的一段,这是对应的k是最大的数字 + 1。

用一个set sd维护删除的数字。

从大到小枚举删除的数字,每次删除一个数字,都可以看成是删除一个长的段,然后添加两个小的段,在sd中找出这个数字的位置cur的前驱pre和后继sub,要删除的段就是前驱到后继这一段,然后新添加两段就是cur到pre 以及 sub到cur。把cur添加到删除数字的集合中。

用一个map维护<线段长度,数量>来表示现在段数的情况,容易知道当map的size为1时,表明所有线段的长度相同,就可以更新答案了。

更新答案的时候,首先考虑线段数量是否更多,再考虑k是否可以变小。

假设当前删除的数字是tmp,那么是不是满足条件之后就直接把答案更新为tmp呢?

不是,假设当前数字是x,比它小的第一个数字是y,显然y + 1这个数字也可以满足条件,而且y + 1 <= x是显然的,所以答案应该更新为y + 1。

感谢mzjj教我!

代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <map>
  5. #include <set>
  6. using namespace std;
  7. typedef long long ll;
  8. typedef pair<int,int> pii;
  9. map<int,int> mmp;
  10. set<pii> s;
  11. set<int> sd;
  12. int main()
  13. {
  14. int n;
  15. scanf("%d",&n);
  16. for (int i = ;i <= n;i++)
  17. {
  18. int x;
  19. scanf("%d",&x);
  20. s.insert(pii(x,i));
  21. }
  22. sd.insert();
  23. sd.insert(n+);
  24. mmp[n]++;
  25. int ans = s.rbegin() -> first + ;
  26. int cnt = ;
  27. for (int i = ;i < n - ;i++)
  28. {
  29. auto it = s.rbegin();
  30. int cur = it -> second;
  31. s.erase(*it);
  32. int pre = *(--sd.lower_bound(cur));
  33. int sub = *(sd.upper_bound(cur));
  34. //printf("%d %d %d\n",pre,sub,cur);
  35. if (sub-pre- > )
  36. {
  37. mmp[sub-pre-]--;
  38. if (mmp[sub-pre-] == )
  39. {
  40. mmp.erase(sub-pre-);
  41. }
  42. }
  43. sd.insert(cur);
  44. if (cur-pre- > ) mmp[cur-pre-]++;
  45. if (sub-cur- > ) mmp[sub-cur-]++;
  46. if (mmp.size() == )
  47. {
  48. //int len = mmp.begin() -> first;
  49. int num = mmp.begin() -> second;
  50. //printf("%d *\n",len);
  51. if (num >= cnt)
  52. {
  53. cnt = num;
  54. ans = s.rbegin() -> first + ;
  55. }
  56. }
  57. }
  58. printf("%d",ans);
  59. return ;
  60. }

codeforces 982D Shark的更多相关文章

  1. Codeforces Codeforces Round #484 (Div. 2) D. Shark

    Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D ...

  2. Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks dp+矩阵加速

    题目链接: http://codeforces.com/problemset/problem/621/E E. Wet Shark and Blocks time limit per test2 se ...

  3. 【矩阵乘法优化dp】[Codeforces 621E] Wet Shark and Blocks

    http://codeforces.com/problemset/problem/621/E E. Wet Shark and Blocks time limit per test 2 seconds ...

  4. 【CodeForces 621A】Wet Shark and Odd and Even

    题 Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wan ...

  5. 【CodeForces 621C】Wet Shark and Flowers

    题 There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such tha ...

  6. Codeforces 612B. Wet Shark and Bishops 模拟

    B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  7. 【38.24%】【codeforces 621E】 Wet Shark and Blocks

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. CodeForces 982 D Shark

    Shark 题意:一个研究员观察了一条鲨鱼n天的运动,然后这条鲨鱼他只会往前走,不会回到去过的地方,现在有一个k,,如果鲨鱼当天游过的距离 >= k, 代表的鲨鱼在这天会前往下一个地点,现在求鲨 ...

  9. CODEFORCEs 621E. Wet Shark and Blocks

    E. Wet Shark and Blocks time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. cocos2d-x项目与vs2013编译

    cocos2d-x项目与vs2013编译 2014-12-17 cheungmine 因为C++11引入了众多开源软件的特性,导致cocos2d-x r3.3项目无法用 vs2010编译. 所以安装了 ...

  2. linux 网络不通问题排查

    基本的排错步骤(从上往下)ping 127.0.0.1ping的通说明tcp协议栈没有问题ping 主机地址 ping的通说明网卡没有问题ping 路由器默认网关 ping的通说明包可以到达路由器最后 ...

  3. Auto Create Editable Copy Font(Unity3D开发之二十二)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/48318879 ...

  4. Linux其他常见压缩备份工具 - dd,cpio

    dd dd 可以读取磁碟装置的内容(几乎是直接读取磁区"sector"),然后将整个装置备份成一个文件呢!真的是相当的好用啊- dd 的用途有很多啦-但是我们仅讲一些比较重要的选项 ...

  5. 软件工程师 Book

    一.软件工程师  --Clean Code<代码整洁之道>  --Implementation Patterns<实现模式>  --Code Complete<代码大全& ...

  6. Eclipse配置SpringBoot

    从这一博客开始学习SpringBoot,今天学习Eclipse配置SpringBoot.Eclipse导入SpringBoot有两种方式,一种是在线一个是离线方式. 一.在线安装 点击Eclipse中 ...

  7. Integer 和int 比较

    在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...

  8. DB2许可证文件

    与 DB2® 数据库产品相关联的许可证文件有两种类型: 基本许可证密钥和 完整许可证密钥.这些许可证密钥以纯文本格式存储,通常称为 许可证文件或 许可证权利证书. "基本"许可证未 ...

  9. JAVA 综合面试题

    JAVA 综合面试题 2007-08-12 目录 TOC \o "1-3" \h \z \u Java面试题整理 9 Java面向对象 9 1. super()与this()的区别 ...

  10. Hibernate中的对象有三种状态

    Hibernate中的对象有三种状态: 瞬时状态 (Transient),持久状态 (Persistent), 1. 脱管状态 (Detached) 1. 1. 瞬时状态 (Transient) 由  ...