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的更多相关文章

  1. 【set】【multiset】Codeforces Round #484 (Div. 2) D. Shark

    题意:给你一个序列,让你找一个k,倘若把大于等于k的元素都标记为不可用,那么剩下的所有元素形成的段的长度相同,并且使得段的数量尽量大.如果有多解,输出k尽量小的. 把元素从大到小排序插回原位置,用一个 ...

  2. Codeforces Codeforces Round #484 (Div. 2) E. Billiard

    Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/proble ...

  3. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  6. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

随机推荐

  1. Docker中使用Tomcat并部署war工程

    准备 首先从远程仓库拉取Tomcat镜像到本地. docker pull tomcat 使用images命令查看是否拉取成功. 创建镜像文件并将war包上传到同级目录下.(本文是在/usr/local ...

  2. neo4j配置(转)

    我的neo4j配置 # 修改第9行,去掉#,修改数据库名 dbms.active_database=wkq_graph.db # 修改第12行,去掉#.修改路径,改成绝对路径 dbms.directo ...

  3. 软件工程小组讨论设计NABCD

    项目名称:失物招领平台 项目工作小组:冰淇淋队 项目简介:目前同学们丢了东西都qq空间转发或者某个特定的qq群发消息,qq空间转发浪费了别人的时间,qq群发消息也浪费了别人的时间.怎么样才能浪费最少的 ...

  4. Win10 远程桌面连接出现“要求的函数不受支持”的解决办法之修改注册表

    问题起因 笔者自己在阿里云上搞服务器,有一台 Windows Server 必须通过远程桌面连接来管理,由于没能完全关掉 Win10 自带的烦人的系统更新,导致昨天安装完更新后出现了连接远程桌面时“要 ...

  5. html入门第二天。

    二·1.图片与多媒体:-------------- img标签(重中之重): 网页中的图片展示就是用的img标签实现,img元素相网页中嵌入一幅图形,行内标签,单标签. 基础语句:<img sr ...

  6. 操作CSS样式公共方法库

    项目中常用的一些方法,我们都封装到公共方法库 let utils = (function () { //=>获取元素的样式 let getCss = function (curEle, attr ...

  7. Unity Awards 2018最佳资源

    好的工具与资源,将帮助你的开发,达到事办功倍,今天我们将为大家介绍荣获Unity Awards 2018最佳资源的获奖作品. 最佳艺术工具:Aura - Volumetric Lighting Aur ...

  8. 关于HTML5中的sessionStorage的会话级缓存使用

    sessionStorage作为HTML5的Web Storage的两种存储方式之一.    用于本地存储一个会话(session)中的数据,这些数据只有在同一个会话中的页面才能访问并且当会话结束后数 ...

  9. java集合类,HashMap,ArrayList

    集合类 Collection LinkedList.ArrayList.HashSet是非线程安全的, Vector是线程安全的; ArrayXxx:底层数据结构是数组,连续存放,所以查询快,增删慢. ...

  10. Vue的从入门到放弃

    此贴仅记录vue学习路程中遇见的大大小小,形形色色的问题 1.  vue自动打开浏览器配置: 当使用vue 脚手架搭建项目后启动npm run dev,会出现 但是不会自动打开浏览器的,这时候去con ...