Codeforces Codeforces Round #484 (Div. 2) D. Shark
Codeforces Codeforces Round #484 (Div. 2) D. Shark
题目连接:
http://codeforces.com/contest/982/problem/D
Description
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.
Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $k$ that if the shark in some day traveled the distance strictly less than $k$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $k$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $k$.
The shark never returned to the same location after it has moved from it. Thus, in the sequence of $n$ days we can find consecutive nonempty segments when the shark traveled the distance less than $k$ in each of the days: each such segment corresponds to one location. Max wants to choose such $k$ that the lengths of all such segments are equal.
Find such integer $k$, that the number of locations is as large as possible. If there are several such $k$, print the smallest one.
Sample Input
8
1 2 7 3 4 8 5 6
Sample Output
6
25 1 2 3 14 36
题意
给定一个k,所有严格小于k的为0,大于等于k的为1,由此产生新序列。
对于新的序列
1.要保证所有连续为1的长度相等
2.满足1情况下,尽可能段数更多
3.满足2的k尽可能小
Creating a new sequence, for each element replace by 0, if \(x<k\); otherwise 1.
If the new sequence is valid, it must fit these condition:
1.The length of every consecutive nonempty segments which is made of 1 are same.
2.The number of consecutive segments is maximum possible satisfying the first condition,
3.K is smallest possible satisfying the first and second conditions.
题解:
用优先队列来枚举k,用并查集维护线段,然后判断线段是否改变了答案
Use priority_queue to enumeratioin k. Use Disjoint set union to maintain segment, then judge the new segment changing the answer or not.
代码
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100010];
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii> > q;
int fa[100010];
int sz[100010];
int sumduan;
int bigduan;
int maxduan;
int ans;
int ansduan;
int find(int k) {
return fa[k] == k ? k : fa[k] = find(fa[k]);
}
void unionfa(int q, int w) {
if (w > q) swap(q, w);
fa[q] = w = find(w);
sz[w] += sz[q];
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sz[i] = 1;
fa[i] = i;
q.push(make_pair(a[i], i));
}
memset(a, 0, sizeof a);
for (int i = 1; i <= n; i++) {
int o = q.top().first;
int x = q.top().second;
q.pop();
a[x] = 1;
if (a[x - 1] && a[x + 1]) {
unionfa(x, x - 1);
unionfa(x, x + 1);
sumduan--;
} else if (a[x - 1]) {
unionfa(x, x - 1);
} else if (a[x + 1]) {
unionfa(x, x + 1);
} else {
sumduan++;
}
int f = find(x);
if (sz[f] > maxduan) {
maxduan = sz[f];
bigduan = 1;
} else if (sz[f] == maxduan) {
bigduan++;
}
if (bigduan == sumduan) {
if (sumduan > ansduan) {
ansduan = sumduan;
ans = o;
}
}
}
cout << ans+1 << endl;
}
Codeforces Codeforces Round #484 (Div. 2) D. Shark的更多相关文章
- 【set】【multiset】Codeforces Round #484 (Div. 2) D. Shark
题意:给你一个序列,让你找一个k,倘若把大于等于k的元素都标记为不可用,那么剩下的所有元素形成的段的长度相同,并且使得段的数量尽量大.如果有多解,输出k尽量小的. 把元素从大到小排序插回原位置,用一个 ...
- Codeforces Codeforces Round #484 (Div. 2) E. Billiard
Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- 学习TestNG,乍暖还寒冷时
时间:2019年2月23日,农历正月十九,星期六,天气晴,略有阳光,但下午三点多就不见阳光了. 地点:上海 昨夜雨疏风骤,浓睡不消残酒.试问卷帘人,却道海棠依旧.知否?知否?应是绿肥红瘦 是的,魔都上 ...
- CentOS7下Mysql 5.6.30安装与配置
环境:centos 7 x64 先下载mysql安装包 打开 http://dev.mysql.com/downloads/mysql/ 选择 linux - Generic 再选择 下载完毕后,得 ...
- JUnit源码分析 - 扩展 - 自定义Rule
JUnit Rule简述 Rule是JUnit 4.7之后新加入的特性,有点类似于拦截器,可以在测试类或测试方法执行前后添加额外的处理,本质上是对@BeforeClass, @AfterClass, ...
- POJ-3660.Cow Contest(有向图的传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17797 Accepted: 9893 De ...
- L3-021 神坛(极角排序求三角形最小面积)
在古老的迈瑞城,巍然屹立着 n 块神石.长老们商议,选取 3 块神石围成一个神坛.因为神坛的能量强度与它的面积成反比,因此神坛的面积越小越好.特殊地,如果有两块神石坐标相同,或者三块神石共线,神坛的面 ...
- CentOS7+CDH5.14.0安装CDH错误排查: HiveServer2 该角色的进程已退出。该角色的预期状态为已启动
错误提示: HiveServer2 该角色的进程已退出.该角色的预期状态为已启动 解决办法:出现此问题应该是内存不足造成的,重启相应的组件即可.比如Hive报错,重启Hive,YARN报错,重启YAR ...
- HDU 1522 Marriage is Stable 稳定婚姻匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1522 #include<bits/stdc++.h> #define INF 0x3f3f3f3f ...
- java调用本地播放器播放视频文件。调用本地播放器不能播放指定文件的说明。
public class OpenExe extends HttpServlet { //打开本地播放器并播放视频 public static void openExe(String file) { ...
- FastDFS防盗链
FastDFS扩展模块内置了通过token来实现防盗链的功能.开启防盗链后,访问文件是需要在url中加两个参数:token和ts.ts为时间戳,token为系统根据时间戳和密码生成的信物.为了系统的安 ...
- 机器学习之支持向量机(SVM)学习笔记
支持向量机是一种二分类算法,算法目的是找到一个最佳超平面将属于不同种类的数据分隔开.当有新数据输入时,判断该数据在超平面的哪一侧,继而决定其类别. 具体实现思路: 训练过程即找到最佳的分隔超平面的过程 ...