Acdream 1427 Nice Sequence

Nice Sequence

Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (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 l such that the sequence a1, a2,..., al is k-nice.

Sample Input

10 1
0 1 1 0 2 2 1 2 2 3
2 0
1 0

Sample Output

8
0

Source

Andrew Stankevich Contest 23

Manager

mathlover

解题:线段树。。。哎。。。当时不知怎么写复杂了。。。。真是蛋疼。。。。

 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 long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 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 }

时间: 2024-12-08 21:52:54

Acdream 1427 Nice Sequence的相关文章

acd - 1427 - Nice Sequence(线段树)

题意:一个由n个数组成的序列(序列元素的范围是[0, n]),求最长前缀 j ,使得在这个前缀 j 中对于任意的数 i1 < i2,都满足任意的 m <= j,i1 在前 m 个数里出现的次数 >= i2 在前 m 个数里出现的次数 - k (1 ≤ n ≤ 200 000, 0 ≤ k ≤ 200 000). 题目链接:http://acdream.info/problem?pid=1427 -->>第一个前缀 j 不满足,那么后面的前缀一定不满足(因为前缀 j 不满足).

LightOJ 题目1427 - Substring Frequency (II)(AC自己主动机)

1427 - Substring Frequency (II) PDF (English) Statistics Forum Time Limit: 5 second(s) Memory Limit: 128 MB A string is a finite sequence of symbols that are chosen from an alphabet. In this problem you are given a string T and n queries each with a

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变成1,1变成0 3 a b 查询[a,b]区间1的个数 4 a b 查询[a,b]区间连续1最长的长度 思路:线段树线段合并.须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0.1.右边最长0.1,区间最长0.1,然后区间合并去搞就可以 代码: #include <cstdi

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

HDOJ 5147 Sequence II 树状数组

树状数组: 维护每一个数前面比它小的数的个数,和这个数后面比他大的数的个数 再枚举每个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a seq

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav