codeforces 982D Shark
题意:
给出一个数组,删除大于等于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教我!
代码:
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <map>
- #include <set>
- using namespace std;
- typedef long long ll;
- typedef pair<int,int> pii;
- map<int,int> mmp;
- set<pii> s;
- set<int> sd;
- int main()
- {
- int n;
- scanf("%d",&n);
- for (int i = ;i <= n;i++)
- {
- int x;
- scanf("%d",&x);
- s.insert(pii(x,i));
- }
- sd.insert();
- sd.insert(n+);
- mmp[n]++;
- int ans = s.rbegin() -> first + ;
- int cnt = ;
- for (int i = ;i < n - ;i++)
- {
- auto it = s.rbegin();
- int cur = it -> second;
- s.erase(*it);
- int pre = *(--sd.lower_bound(cur));
- int sub = *(sd.upper_bound(cur));
- //printf("%d %d %d\n",pre,sub,cur);
- if (sub-pre- > )
- {
- mmp[sub-pre-]--;
- if (mmp[sub-pre-] == )
- {
- mmp.erase(sub-pre-);
- }
- }
- sd.insert(cur);
- if (cur-pre- > ) mmp[cur-pre-]++;
- if (sub-cur- > ) mmp[sub-cur-]++;
- if (mmp.size() == )
- {
- //int len = mmp.begin() -> first;
- int num = mmp.begin() -> second;
- //printf("%d *\n",len);
- if (num >= cnt)
- {
- cnt = num;
- ans = s.rbegin() -> first + ;
- }
- }
- }
- printf("%d",ans);
- return ;
- }
codeforces 982D Shark的更多相关文章
- Codeforces Codeforces Round #484 (Div. 2) D. Shark
Codeforces Codeforces Round #484 (Div. 2) D. Shark 题目连接: http://codeforces.com/contest/982/problem/D ...
- 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 ...
- 【矩阵乘法优化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 ...
- 【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 ...
- 【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 ...
- Codeforces 612B. Wet Shark and Bishops 模拟
B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- 【38.24%】【codeforces 621E】 Wet Shark and Blocks
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces 982 D Shark
Shark 题意:一个研究员观察了一条鲨鱼n天的运动,然后这条鲨鱼他只会往前走,不会回到去过的地方,现在有一个k,,如果鲨鱼当天游过的距离 >= k, 代表的鲨鱼在这天会前往下一个地点,现在求鲨 ...
- 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 ...
随机推荐
- cocos2d-x项目与vs2013编译
cocos2d-x项目与vs2013编译 2014-12-17 cheungmine 因为C++11引入了众多开源软件的特性,导致cocos2d-x r3.3项目无法用 vs2010编译. 所以安装了 ...
- linux 网络不通问题排查
基本的排错步骤(从上往下)ping 127.0.0.1ping的通说明tcp协议栈没有问题ping 主机地址 ping的通说明网卡没有问题ping 路由器默认网关 ping的通说明包可以到达路由器最后 ...
- Auto Create Editable Copy Font(Unity3D开发之二十二)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/48318879 ...
- Linux其他常见压缩备份工具 - dd,cpio
dd dd 可以读取磁碟装置的内容(几乎是直接读取磁区"sector"),然后将整个装置备份成一个文件呢!真的是相当的好用啊- dd 的用途有很多啦-但是我们仅讲一些比较重要的选项 ...
- 软件工程师 Book
一.软件工程师 --Clean Code<代码整洁之道> --Implementation Patterns<实现模式> --Code Complete<代码大全& ...
- Eclipse配置SpringBoot
从这一博客开始学习SpringBoot,今天学习Eclipse配置SpringBoot.Eclipse导入SpringBoot有两种方式,一种是在线一个是离线方式. 一.在线安装 点击Eclipse中 ...
- Integer 和int 比较
在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...
- DB2许可证文件
与 DB2® 数据库产品相关联的许可证文件有两种类型: 基本许可证密钥和 完整许可证密钥.这些许可证密钥以纯文本格式存储,通常称为 许可证文件或 许可证权利证书. "基本"许可证未 ...
- JAVA 综合面试题
JAVA 综合面试题 2007-08-12 目录 TOC \o "1-3" \h \z \u Java面试题整理 9 Java面向对象 9 1. super()与this()的区别 ...
- Hibernate中的对象有三种状态
Hibernate中的对象有三种状态: 瞬时状态 (Transient),持久状态 (Persistent), 1. 脱管状态 (Detached) 1. 1. 瞬时状态 (Transient) 由 ...