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 ...
随机推荐
- 【57】android图片印刻,阳刻,素描图效果处理
介绍我参与开发的妙趣剪纸app使用的图片处理相关的技术 关于妙趣剪纸,各大android商店都可以下载,下面贴出小米商店的链接 妙趣剪纸下载 软件效果截图 如何实现上面的图片处理效果呢 1.初始化高斯 ...
- HBase写被block的分析
一个线上集群出现莫名奇妙不能写入数据的bug,log中不断打印如下信息: 引用 2011-11-09 07:35:45,911 INFO org.apache.hadoop.hbase.regions ...
- 近期ubuntu 14.04 cpu占用高排障
近期linux使用总是cpu达到满值, 双核cpu其中一个核总是100%,另一个核正常.top之发现输入法框架fcitx满载,直接kill之,发现搜狗输入法不能用了,随即输入如下命令: fcitx f ...
- masm中list文件和宏的一些常用编译调试查看方法
我们知道使用用 ml /Fl a.asm 可以生成lst文件,但是如果不加调整,masm默认生成的lst文件是非常大的,因为它包含了很大的windows必须用到的头文件内容,为了减小lst文件大小,便 ...
- mysql海量数据的优化
转载:https://www.cnblogs.com/trying/archive/2013/08/15/3259126.html 下面是一部分比较重要的建议:1.选择正确的存储引擎以 MySQL为例 ...
- 使用 focus() 和 blur()
<html> <head> <style type="text/css"> a:active {color:green} </style& ...
- sqlplus 分析执行计划
转载 http://xm-koma.iteye.com/blog/1048451 对于oracle9i,需要手工设置plustrace角色,步骤如下: 1.在SQL>connect sys/密码 ...
- HashMap 深入分析
/** *@author annegu *@date 2009-12-02 */ Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上 ...
- sublime中安装sublimecodeintel插件
本文是基于在Windows上对sublime进行相关配置. 1.安装sublime,在官网http://www.sublimetext.com/3. 如果是在Linux系统上安装只需要输入命令直接安装 ...
- Java 包装类笔记
@(JDK)[包装类] Java 包装类笔记 当需要使用到集合的时候,如果是基础数据类型,需要转换为包装类,再使用,在JDK1.5之前,每次使用的时候,都需要手动转换为包装类(称为装箱),然后在使用的 ...