Acdream 1427 nice sequence

Source: Internet
Author: User
Tags integer numbers
Nice sequencetime limit: 12000/6000 ms (Java/others) memory limit: 128000/64000 KB (Java/others) Problem Description

Let us consider the sequence A1, A2 ,..., an of non-negative integer numbers. denote as ci, j the number of occurrences of the number I among A1, A2 ,..., AJ. we call the sequence K-nice if for all I1 <I2 and for all j the following condition is satisfied: CI1, J ≥ci2, J −k.

Given the sequence A1, A2,..., an and the number k, find its longest prefix that is k-nice.

Input the first line of the input file contains N and K (1 ≤ n ≤ 200 000, 0 ≤ k ≤ 200 000 ). the second line contains N integer numbers ranging from 0 to n. output output the greatest LSuch that the sequence A1, A2,..., Al is k-nice.Sample Input
10 10 1 1 0 2 2 1 2 2 32 01 0
Sample output
80
Sourceandrew stankevich contest 23 managermathlover problem solving: Line Segment tree... Ah... At that time, I did not know how to write complicated .... It really hurts ....
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 210000;18 struct node{19     int lt,rt,minv,maxv;20 };21 node tree[maxn<<2];22 void build(int lt,int rt,int v){23     tree[v].lt = lt;24     tree[v].rt = rt;25     tree[v].minv = tree[v].maxv = 0;26     if(lt == rt) return;27     int mid = (lt+rt)>>1;28     build(lt,mid,v<<1);29     build(mid+1,rt,v<<1|1);30 }31 void update(int k,int v,int &val){32     if(tree[v].lt == tree[v].rt){33         val = tree[v].maxv = ++tree[v].minv;34         return;35     }36     int mid = (tree[v].lt + tree[v].rt)>>1;37     if(k <= mid) update(k,v<<1,val);38     else if(k > mid) update(k,v<<1|1,val);39     tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv);40     tree[v].maxv = max(tree[v<<1].maxv,tree[v<<1|1].maxv);41 }42 int query_min(int lt,int rt,int v){43     if(tree[v].lt == lt && tree[v].rt == rt) return tree[v].minv;44     int mid = (tree[v].lt + tree[v].rt)>>1;45     if(rt <= mid) return query_min(lt,rt,v<<1);46     else if(lt > mid) return query_min(lt,rt,v<<1|1);47     else return min(query_min(lt,mid,v<<1),query_min(mid+1,rt,v<<1|1));48 }49 int query_max(int lt,int rt,int v){50     if(tree[v].lt == lt && tree[v].rt == rt) return tree[v].maxv;51     int mid = (tree[v].lt + tree[v].rt)>>1;52     if(rt <= mid) return query_max(lt,rt,v<<1);53     else if(lt > mid) return query_max(lt,rt,v<<1|1);54     else return max(query_max(lt,mid,v<<1),query_max(mid+1,rt,v<<1|1));55 }56 int n,k,val,tmp,ans;57 int main() {58     while(~scanf("%d %d",&n,&k)){59         build(0,n + 1,1);60         ans = 0;61         bool flag = true;62         for(int i = 1; i <= n; i++){63             scanf("%d",&tmp);64             update(tmp,1,val);65             if(flag && val <= query_min(0,tmp,1)+k && val >= query_max(tmp+1,n+1,1)-k) 66                 ans = i;67             else flag = false;68         }69         printf("%d\n",ans);70     }71     return 0;72 }
View code

Acdream 1427 nice sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.