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;
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9175893.html

时间: 2024-10-07 18:00:54

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

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

题意:给你一个序列,让你找一个k,倘若把大于等于k的元素都标记为不可用,那么剩下的所有元素形成的段的长度相同,并且使得段的数量尽量大.如果有多解,输出k尽量小的. 把元素从大到小排序插回原位置,用一个set维护前驱后继,相当于删除一个原有的段,然后将这个段切成两半,产生两个新的段.维护这次操作后所有段的长度以及各种长度的出现次数(用multiset),一旦合法,就尝试更新答案. #include<cstdio> #include<algorithm> #include<set

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 Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1