Codeforces 190D Non-Secret Cypher【思维+RMQ+二分】
来源:互联网 发布:linux u盘启动 windows 编辑:程序博客网 时间:2024/05/16 14:34
Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the information the berlanders needed so much.
The captured enemy had an array of positive integers. Berland intelligence have long been aware of the flatland code: to convey the message, which contained a number m, the enemies use an array of integers a. The number of its subarrays, in which there are at least kequal numbers, equals m. The number k has long been known in the Berland army so General Touristov has once again asked Corporal Vasya to perform a simple task: to decipher the flatlanders' message.
Help Vasya, given an array of integers a and number k, find the number of subarrays of the array of numbers a, which has at least k equal numbers.
Subarray a[i... j] (1 ≤ i ≤ j ≤ n) of array a = (a1, a2, ..., an) is an array, made from its consecutive elements, starting from the i-th one and ending with the j-th one: a[i... j] = (ai, ai + 1, ..., aj).
The first line contains two space-separated integers n, k (1 ≤ k ≤ n ≤ 4·105), showing how many numbers an array has and how many equal numbers the subarrays are required to have, correspondingly.
The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — elements of the array.
Print the single number — the number of such subarrays of array a, that they have at least k equal integers.
Please do not use the %lld specifier to read or write 64-bit integers in С++. In is preferred to use the cin, cout streams or the %I64dspecifier.
4 21 2 1 2
3
5 31 2 1 1 3
2
3 11 1 1
6
In the first sample are three subarrays, containing at least two equal numbers: (1,2,1), (2,1,2) and (1,2,1,2).
In the second sample are two subarrays, containing three equal numbers: (1,2,1,1,3) and (1,2,1,1).
In the third sample any subarray contains at least one 1 number. Overall they are 6: (1), (1), (1), (1,1), (1,1) and (1,1,1).
题目大意:
给出长度为N的一个序列,让我们统计有多少个满足条件的子区间,一个子区间需要满足的条件是:某个数字出现的次数大于等于k次。
思路:
①我们维护一个数组nex【i】,表示从当前位子开始,第k次出现a【i】这个数的位子。
②然后我们O(n)枚举起点,然后二分一个终点,对于当前枚举的区间【L,R】,如果其中存在一个位子i,使得nex【i】<=R,那么当前区间就一定包含某个数出现的次数大于等于k次,那么我们希望找到区间【L,R】内nex【i】的最小值,所以这里预处理出一个ST表即可。
时间复杂度O(nlogn);
Ac代码:
#include<stdio.h>#include<string.h>#include<math.h>#include<vector>#include<map>using namespace std;int a[450000];vector<int>mp[450000];int num[450000];int nex[450000];int minn[400005][42];int n,k;void ST(){ int len=floor(log10(double(n))/log10(double(2))); for(int j=1;j<=len;j++) { for(int i=1;i<=n+1-(1<<j);i++) { minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]); } }}int getminn(int a,int b){ int len= floor(log10(double(b-a+1))/log10(double(2))); return min(minn[a][len], minn[b-(1<<len)+1][len]);}int main(){ while(~scanf("%d%d",&n,&k)) { int cnt=0; map<int,int>s; for(int i=1;i<=n;i++)mp[i].clear(); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(s[a[i]]==0)s[a[i]]=++cnt; a[i]=s[a[i]]; mp[a[i]].push_back(i); num[i]=mp[a[i]].size()-1; } for(int i=1;i<=n;i++) { nex[i]=0x3f3f3f3f; if(mp[a[i]].size()>num[i]+k-1) { nex[i]=mp[a[i]][num[i]+k-1]; } } for(int i=1;i<=n;i++)minn[i][0]=nex[i]; ST(); long long int output=0; for(int i=1;i<=n;i++) { int pos=-1; int l=i; int r=n; while(r-l>=0) { int mid=(l+r)/2; if(getminn(i,mid)<=mid) { pos=mid; r=mid-1; } else l=mid+1; } if(pos==-1)continue; output+=(n-pos+1); } printf("%lld\n",output); }}
- Codeforces 190D Non-Secret Cypher【思维+RMQ+二分】
- Codeforces 190D Non-Secret Cypher
- CodeForces Round #120 (190D) - Non-Secret Cypher
- codeforces 190D Non-Secret Cypher(two pointers)
- codeforces 190D Non-Secret Cypher 双指针
- D. Non-Secret Cypher
- CF 190D Non-Secret Cypher
- CF 190D. Non-Secret Cypher
- Codeforces Round #120 (Div. 2) / 190D Non-Secret Cypher (计数&two pointers)
- Codeforces Round #120(190D) Non-Secret Cypher(尺取法)
- codeforces 689D (二分 RMQ)
- Codeforces 689D RMQ+二分
- cf 120 c,d,e STL Non-Secret Cypher Counter Attack
- Codeforces 689D Friends and Subsequences【思维+二分+RMQ】套路题
- Codeforces 359D Pair of Numbers【思维+二分+区间Gcd+区间最小值RMQ】
- CodeForces 672D (思维+二分)
- Codeforces 620D Lipshitz Sequence RMQ+二分
- codeforces 731D (DP 二分 二维RMQ)
- SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
- 面试总结
- 203
- HandlerThread实现数字时钟
- String的常用方法
- Codeforces 190D Non-Secret Cypher【思维+RMQ+二分】
- BillPrint
- 静态工厂方法
- 道格拉斯-普克算法(Douglas–Peucker algorithm)---多边形逼近算法
- 高级算法日记10:图(3)
- 西子湖畔
- Lua_ uLua基础_015
- AtomicInteger类保证线程安全的用法
- 1007素数对猜想(模拟)