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 ...
随机推荐
- Media Player Classic - HC 源代码分析 7:详细信息选项卡(CPPageFileInfoDetails)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...
- Developing RIA Web Applications with Oracle ADF
Developing RIA Web Applications with Oracle ADF Purpose This tutorial shows you how to build a ric ...
- IDE
IDE(Integrated Development Environment,集成开发环境).DE集成开发环境(简称IDE)软件是用于程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用 ...
- 【51】java设计模式-工厂设计模式剖析
工厂设计设计模式的分类: 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Factory):不利于产生系列产品: 2)工厂方法模式(Factory Method) ...
- application之OnLowMemory()和 OnTrimMemory(level)讲解
1. OnLowMemory OnLowMemory是Android提供的API,在系统内存不足,所有后台程序(优先级为background的进程,不是指后台运行的进程)都被杀死时,系统会调用OnLo ...
- OpenCV——PS 图层混合算法 (三)
具体的算法原理可以参考 PS图层混合算法之三(滤色, 叠加, 柔光, 强光) // PS_Algorithm.h #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ ...
- LeetCode(37)-Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- jasper(二):制作饼状图和柱状图
在新建一个框架之后 我们也是要执行 add dataset,来添加一个链接数据库的语句,因为这是个饼状图,所以要用group by 全部放入右边的框架 点完成 接下来,就是要创建饼状图,就要点击 窗口 ...
- spiral matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- Spring 框架的优点及缺点
首先Spring 是一个框架,使用Spring并不代表代码质量的提高,就像盖房子选择用上海的地皮还是北京的地皮一样,房子质量与土地所在的城市无关,与房子的具体设计方案和选料有关. 使用Spring 等 ...