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

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

把元素从大到小排序插回原位置,用一个set维护前驱后继,相当于删除一个原有的段,然后将这个段切成两半,产生两个新的段。维护这次操作后所有段的长度以及各种长度的出现次数(用multiset),一旦合法,就尝试更新答案。

#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
typedef pair<int,int> Point;
set<int>S;
multiset<int>S2;
int n;
Point a[100005];
int all,ans,maxnum;
int main(){
	scanf("%d",&n);
	int x;
	S2.insert(n);
	maxnum=1;
	for(int i=1;i<=n;++i){
		scanf("%d",&x);
		a[i].first=x;
		a[i].second=i;
	}
	sort(a+1,a+n+1);
	ans=a[n].first+1;
	S.insert(0);
	S.insert(n+1);
	for(int i=n;i>=1;--i){
		S.insert(a[i].second);
		int y=*S.upper_bound(a[i].second);
		set<int>::iterator it=S.lower_bound(a[i].second); --it;
		int x=*it;
		S2.erase(S2.find(y-x-1));
		if(y-a[i].second>1){
			S2.insert(y-a[i].second-1);
		}
		if(a[i].second-x>1){
			S2.insert(a[i].second-x-1);
		}
		if(!S2.empty()){
			multiset<int>::iterator jt=S2.end(); --jt;
			if((*S2.begin())==(*jt)){
				if(S2.size()>maxnum || (S2.size()==maxnum && a[i-1].first+1<ans)){
					maxnum=S2.size();
					ans=a[i-1].first+1;
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

原文地址:https://www.cnblogs.com/autsky-jadek/p/9055525.html

时间: 2024-08-01 14:16:22

【set】【multiset】Codeforces Round #484 (Div. 2) D. Shark的相关文章

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

Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)

Codeforces Round #484 (Div. 2) B. Bus of Characters B. Bus of Characters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output In the Bus of Characters there are nn rows of seat, each having 22

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

Codeforces Codeforces Round #484 (Div. 2) E. Billiard 题目连接: http://codeforces.com/contest/982/problem/E Description Consider a billiard table of rectangular size $n \times m$ with four pockets. Let's introduce a coordinate system with the origin at t

Codeforces Round #484 (Div. 2) A. Row

A. Row time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You're given a row with nn chairs. We call a seating of people "maximal" if the two following conditions hold: There are no neigh

Codeforces Round #484 (Div. 2)

Link A.B 签到 C 分析 赛时wa7起不来 D 题意 分析 原文地址:https://www.cnblogs.com/Deadline/p/9058253.html

Codeforces Round #257 div.2 D or 450D Jzzhu and Cities【最短路】

Codeforces Round #257 div.2 D or 450D Jzzhu and Cities[最短路] 题目链接:点击打开 题目大意: 在一个国家中有n个城市(城市编号1~n),m条公路和k条铁路,编号为1的城市为首都,为了节约,不需要的铁路需要关闭,问在保证首都到其余所有城市的最短路不变的条件下,最多有多少条铁路是不需要的. 解法: 这个题比较麻烦,保证首都到其余城市的最短路不变,要求出最多有多少条铁路是不需要的,那肯定是从最短路的代码上下手了,我们首先考虑dijkstra算法

Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】

简易字符串匹配,题意不难 1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 int main(){ 9 int i, j, k, t, n; 10 int num, flag, ans; 11 char a[300]; 12 sc

Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】

一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7

Codeforces Round #286 div.1 D 506D D. Mr. Kitayuta&#39;s Colorful Graph【并查集】

题目链接:http://codeforces.com/problemset/problem/506/D 题目大意: 给出n个顶点,m条边,每条边上有一个数字,代表某个颜色.不同数字代表不同的颜色.有很多个询问,每个询问问有多少条纯种颜色的路径使得某两个点联通. 分析: 这个题一看就想用并查集来搞,每种颜色用一个并查集处理.对于输入的每条边,我们只需要将这两个点在这条边的颜色对应的并查集中合并就好了.查询的时候,我们只需要检测这两个点在多少个颜色对应的并查集中是在统一集合中的,那么就有多少条纯种颜