CF1208B
質問の意味:
N個のデジタル与え、連続の最小端が残りの要素は、最小間隔の長さを除去するために必要な、重複する要素を含むように、操作を削除見つけます
ソリューション:
サブセクションを削除した後、接頭辞と接尾辞が変更されないまま、おそらく0の長さは、私たちは私たちが要素を繰り返すことなく得ることができる最大値を見つけるために、接頭辞と接尾辞のいずれかの重複要素が含まれていません修正しましょう。我々は問題を解決するためにマップを使用することができます。
コード:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
#define INF 2147483647
#define N 2010
int a[N],n,ans = INF;
map<int,int> Hash;
int main() {
scanf("%d",&n);
for(int i = 1 ; i <= n ; i++)
scanf("%d",&a[i]);
int cnt = n;
while(cnt >= 1) {
if(Hash[a[cnt]] >= 1) break;
Hash[a[cnt]]++;
cnt--;
}
for (int i = 1; i <= n; i++) {
ans = min(ans, cnt - i + 1);
Hash[a[i]]++;
while (Hash[a[i]] >= 2 && cnt <= n) {
cnt++;
Hash[a[cnt]]--;
}
if (Hash[a[i]] >= 2)break;
}
printf("%d\n", ans);
system("pause");
return 0;
}