秀姿勢
jzoj 3464
題目大意
有n個數,每個數都有一個分組,現在問你最多去掉k個分組後,做多有多少個數是連續的同組的
輸入樣例
9 1
2
7
3
7
7
3
7
5
7
輸出樣例
4
樣例解釋
總共有9個學生,最多隻能刷一次學生。
若不刷,最長完美學生連續子序列長度爲2
若刷掉考第3門考得好的學生,則學生序列變成2 7 7 7 7 5 7,最長完美學生連續子序列長度爲4.
數據範圍
對於10%的數據:
對於30%的數據:
對於100%的數據:
解題思路
題目就是找一個連續的子串裏面包含的組數不超過k+1,這樣刪去k個後還剩一個,然後找這些子串中出現的最多的數出現的次數
我們從一開始讓數字進隊,當組數大於k+1時就從隊尾出,直到符合爲止
代碼
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int n, m, s, x, ans, a[100010];
map < int , int > p;
int main()
{
scanf("%d%d", &n, &m);
m += 1;
x = 1;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &a[i]);
if (!p[a[i]]) s++;//新的組
p[a[i]]++;
while (s > m)
{
p[a[x]]--;
if (!p[a[x]]) s--;//這個組沒了
x++;
}
ans = max(ans, p[a[i]]);//求最大值
}
printf("%d", ans);
return 0;
}