• B. Mike and Feet Codeforces Round #305 (Div. 1) (并查集)


    B. Mike and Feet
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.

    A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.

    Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.

    Input

    The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears.

    The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears.

    Output

    Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.

    蒟蒻不会单调队列的解法..等下好好研究下...

    并查集的思路就是

    先排序,由大到小,然后分别考虑每个位置的前后左右是否被联通,如果已经被联通,那么肯定比现在的数字要大;

     1 #include <algorithm>
     2 #include <stack>
     3 #include <istream>
     4 #include <stdio.h>
     5 #include <map>
     6 #include <math.h>
     7 #include <vector>
     8 #include <iostream>
     9 #include <queue>
    10 #include <string.h>
    11 #include <set>
    12 #include <cstdio>
    13 #define FR(i,n) for(int i=0;i<n;i++)
    14 #define MAX 2005
    15 #define mkp pair <int,int>
    16 using namespace std;
    17 #include <bits/stdc++.h>
    18 const int maxn = 5e5 + 40;
    19 typedef long long ll;
    20 const int  inf = 0x3fffff;
    21 void read(int &x) {
    22     char ch; bool flag = 0;
    23     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    24     for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
    25     x *= 1 - 2 * flag;
    26 }
    27 
    28 //ll val[maxn],id[maxn];
    29 
    30 struct inof{
    31    int id,val;
    32 }p[maxn];
    33 bool cmp(inof a,inof b)
    34 {
    35     return a.val>b.val;
    36 }
    37 
    38 int res[maxn],top=0;
    39 int Rank[maxn],fa[maxn];
    40 int vis[maxn];
    41 
    42 
    43 int fin(int x)
    44 {
    45     if(x==fa[x])return x;
    46     else {
    47         return fa[x]=fin(fa[x]);
    48     }
    49 }
    50 int main() {
    51     int n;
    52     read(n);
    53     for(int i=1;i<=n;i++){
    54         read(p[i].val);
    55         p[i].id=i;
    56         fa[i]=i;
    57         Rank[i]=1;
    58     }
    59     sort(p+1,p+n+1,cmp);
    60     for(int i=1;i<=n;i++){
    61         int id=p[i].id;
    62         vis[id]=1;
    63         if(id!=n){
    64             if(vis[id+1]){
    65                 int x=fin(id+1);
    66                 fa[x]=id;
    67                 Rank[id]+=Rank[x];
    68             }
    69         }
    70         if(id!=1){
    71             if(vis[id-1]){
    72                 int x=fin(id-1);
    73                 fa[x]=id;
    74                 Rank[id]+=Rank[x];
    75             }
    76         }
    77         for(int j=top;j<Rank[id];j++)res[top++]=p[i].val;
    78        // top=Rank[id];
    79     }
    80     for(int i=0;i<top;i++)printf("%d ",res[i]);
    81     return 0;
    82 }
  • 相关阅读:
    排序算法 之 冒泡排序 插入排序 希尔排序 堆排序
    DataStructure之线性表以及其实现
    使用可重入函数进行更安全的信号处理
    内存经济学
    电脑通用技能
    循环套餐的逻辑
    占用了多少内存
    索引的用法
    电脑的眼缘
    字符串积木
  • 原文地址:https://www.cnblogs.com/DreamKill/p/9452639.html
Copyright © 2020-2023  润新知