zoukankan      html  css  js  c++  java
  • Codeforces 1454F F. Array Partition (思维 单调栈 离散化)

    F. Array Partition
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an array aa consisting of nn integers.

    Let min(l,r)min(l,r) be the minimum value among al,al+1,,aral,al+1,…,ar and max(l,r)max(l,r) be the maximum value among al,al+1,,aral,al+1,…,ar.

    Your task is to choose three positive (greater than 00) integers xx, yy and zz such that:

    • x+y+z=nx+y+z=n;
    • max(1,x)=min(x+1,x+y)=max(x+y+1,n)max(1,x)=min(x+1,x+y)=max(x+y+1,n).

    In other words, you have to split the array aa into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition).

    Among all such triples (partitions), you can choose any.

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t21041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.

    The first line of the test case contains one integer nn (3n21053≤n≤2⋅105) — the length of aa.

    The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109), where aiai is the ii-th element of aa.

    It is guaranteed that the sum of nn does not exceed 21052⋅105 (n2105∑n≤2⋅105).

    Output

    For each test case, print the answer: NO in the only line if there is no such partition of aa that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers xx, yy and zz (x+y+z=nx+y+z=n) in the second line.

    If there are several answers, you can print any.

    Example
    input
    Copy
    6
    11
    1 2 3 3 3 4 4 3 4 2 1
    8
    2 9 1 7 3 9 4 1
    9
    2 1 4 2 4 3 3 1 2
    7
    4 2 1 1 4 1 4
    5
    1 1 1 1 1
    7
    4 3 4 3 3 3 4
    
    output
    Copy
    YES
    6 1 4
    NO
    YES
    2 5 2
    YES
    4 1 2
    YES
    1 1 3
    YES
    2 1 4

     1 //2021-03-12 10:29:22
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4 
     5 const int N = 2e5 + 50;
     6 int a[N], b[N];
     7 
     8 int st[N], Lmax[N], Rmax[N], Lmin[N], Rmin[N];
     9 void getLmax(int a[], int n){
    10     int head = 0; st[head] = 0;
    11     for(int i = 1; i <= n; i++){
    12         while(head && a[i] >= a[st[head]]) head--;
    13         Lmax[i] = st[head];
    14         st[++head] = i;
    15     }
    16 }
    17 
    18 void getRmax(int a[], int n){
    19     int head = 0;
    20     st[head] = n + 1;
    21     for(int i = n; i >= 1; i--){
    22         while(head && a[i] >= a[st[head]]) head--;
    23         Rmax[i] = st[head];
    24         st[++head] = i;
    25     }
    26 }
    27 
    28 void getLmin(int a[], int n){
    29     int head = 0;
    30     st[head] = 0;
    31     for(int i = 1; i <= n; i++){
    32         while(head && a[i] <= a[st[head]]) head--;
    33         Lmin[i] = st[head];
    34         st[++head] = i;
    35     }
    36 }
    37 vector<int> v[N];
    38 
    39 void getRmin(int a[], int n){
    40     int head = 0;
    41     st[head] = n + 1;
    42     for(int i = n; i >= 1; i--){
    43         while(head && a[i] <= a[st[head]]) head--;
    44         Rmin[i] = st[head];
    45         st[++head] = i;
    46     }
    47 }
    48 
    49 int main(){
    50     int T;
    51     scanf("%d", &T);
    52     while(T--){
    53         memset(a, 0, sizeof(a));
    54         memset(b, 0, sizeof(b));
    55         int n;
    56         scanf("%d", &n);
    57         for(int i = 1; i <= n; i++){
    58             scanf("%d", &a[i]);
    59             b[i] = a[i];
    60             v[i].clear();
    61         }
    62         sort(b+1, b+n+1);
    63         for(int i = 1; i <= n; i++)
    64             a[i] = lower_bound(b+1, b+n+1, a[i]) - b;
    65         getLmax(a, n);
    66         getRmax(a, n);
    67         getLmin(a, n);
    68         getRmin(a, n);
    69         for(int i = 1; i <= n; i++){
    70             v[a[i]].push_back(i);
    71         }
    72         int ok = 0;
    73         for(int i = 1; i <= n; i++){
    74             int m = v[i].size();
    75             if(m < 3) continue;
    76             if(Lmax[v[i][0]] == 0 && Rmax[v[i][m-1]] == n+1){
    77                 for(int j = 1; j < m-1; j++){
    78                     int pos = v[i][j];
    79                     if(Lmin[pos] < Rmax[v[i][0]] && Rmin[pos] > Lmax[v[i][m-1]]){
    80                         ok = 1;
    81                         printf("YES
    ");
    82                         int ansl = min(pos-1, Rmax[v[i][0]]-1);
    83                         //int ansl = Rmax[v[i][0]]-1;
    84                         int ansr = n - max(pos+1, Lmax[v[i][m-1]]+1) + 1;
    85                         //int ansr = n - Lmax[v[i][m-1]];
    86                         printf("%d %d %d
    ", ansl, n-ansl-ansr, ansr);
    87                         break;
    88                     }
    89                 }
    90                 if(ok) break;
    91             }
    92         }
    93         if(!ok) printf("NO
    ");
    94 
    95 
    96     }
    97 
    98     return 0;
    99 }
  • 相关阅读:
    安装 Cacti 监控
    增加yum源方式 安装升级 Mysql
    Yum
    Cacti 抓取数据方式 安装spine
    Linux 目录解析
    Linux 发行版本简述
    php 安装扩展插件实例-ftp.so
    Crontab 计划任务
    文本处理命令 cat more less cut wc sort uniq
    grep命令
  • 原文地址:https://www.cnblogs.com/sineagle/p/14523174.html
Copyright © 2011-2022 走看看