题意:

给出一个数组,删除大于等于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的更多相关文章

  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. /dev、/sys/dev 和/sys/devices 和udev的关系

    /dev,设备文件存储目录,应用程序通过对这些文件的读写和控制,可以访问实际的设备: /sys/devices目录,按照设备挂接的总线类型,组织成层次结构,保存了系统所有的设备:是文件系统管理设备的最 ...

  2. ITU-T G.1081 IPTV性能监测点 (Performance monitoring points for IPTV)

    ITU-T 建议书 G.1081 IPTV性能监测点 Performance monitoring points for IPTV Summary Successful deployment of I ...

  3. 解决winform窗体闪烁问题

    如果你在Form中绘图的话,不论是不是采用的双缓存,都会看到图片在更新的时候都会不断地闪烁,解决方法就是在这个窗体的构造函数中增加以下三行代码: 请在构造函数里面底下加上如下几行: SetStyle( ...

  4. C语言之插入排序

    插入法排序的要领就是每读入一个数立即插入到最终存放的数组中,每次插入都使得该数组有序. 上代码: #include <stdio.h> #include <stdlib.h> ...

  5. Erlang cowboy routing 路由

    Erlang cowboy routing 路由 本文译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/routing/ Routing 默认情况下,C ...

  6. 部署与管理ZooKeepe

    1.部署 本章节主要讲述如何部署ZooKeeper,包括以下三部分的内容: 1. 系统环境 2. 集群模式的配置 3. 单机模式的配置 系统环境和集群模式配置这两节内容大体讲述了如何部署一个能够用于生 ...

  7. leetCode之旅(5)-博弈论中极为经典的尼姆游戏

    题目介绍 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

  8. apt-get Ubuntu本地ISO镜像入源

    转自http://blog.csdn.net/binchel/article/details/21486999 在没有网络的情况下,本地镜像源不实为一个上等的权宜之计! 目前linux的两大主流包管理 ...

  9. navicat for mysql远程连接ubuntu服务器的mysql数据库

    经常玩服务器上的mysql数据库,但是基于linux操作Mysql多有不便,于是就想着使用GUI工具来远程操作mysql数据库.已经不是三次使用navicat-for-mysql了,但是每次连接远程服 ...

  10. Oracle常用数据库对象(片段)

    1:用户和权限 1.1 用户的创建 a)语法---    create user 用户名  identified by 密码: b)创建用户abcd,并设定密码为abcd;---注意:操作数据库对象是 ...