Skip to content

Instantly share code, notes, and snippets.

@shininglion
Created August 31, 2019 07:07
Show Gist options
  • Save shininglion/b66964885463b18f2742d968a6999c1a to your computer and use it in GitHub Desktop.
Save shininglion/b66964885463b18f2742d968a6999c1a to your computer and use it in GitHub Desktop.
Accepted implementation for the problem 1208B in codeforces
/*
* =====================================================================================
*
* Filename: 1208B.cpp
*
* Description: codeforces 1208B: Uniqueness
*
* Version: 1.0
* Created: 2019年08月31日
* Revision: none
* Compiler: gcc
*
* Author: lionking
* Organization: None
*
* =====================================================================================
*/
// possible cases:
// case 1:
// 10
// 7 7 7 1 2 3 4 5 6 7
// answer: 3 (first 3 digits: 7 7 7)
//
// case 2:
// 10
// 1 2 3 4 3 4 5 6 7 1
// answer: 4 (first 4 digits: 1 2 3 4)
//
// case 3:
// 9
// 1 9 2 9 0 1 5 5 3
// answer: 4 (digit sequence: 9 0 1 5)
//
// case 4:
// 3
// 1 2 3
// answer: 0 (no digit needs to be removed)
//
// case 5:
// 10
// 1 9 2 9 0 1 5 5 3 9
// answer: 6 (digit sequence: 9 2 9 0 1 5)
#include <cstdio>
#include <unordered_set>
using namespace std;
int main()
{
constexpr int NUM_SIZE = 2001;
int n, num[NUM_SIZE];
unordered_set<int> exist;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", num+i);
}
int lidx = 1;
for (; (lidx <= n) && (exist.count(num[lidx]) == 0); ++lidx) {
exist.emplace(num[lidx]);
}
auto result = n;
int ridx = n;
for (--lidx; lidx >= 0; --lidx) {
for (; (ridx > 0) && (exist.count(num[ridx]) == 0); --ridx) {
exist.emplace(num[ridx]);
}
result = min(result, ridx - lidx);
exist.erase(num[lidx]);
}
printf("%d\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment