Skip to content

Instantly share code, notes, and snippets.

@shhyou
Created April 25, 2013 14:12
Show Gist options
  • Save shhyou/5460008 to your computer and use it in GitHub Desktop.
Save shhyou/5460008 to your computer and use it in GitHub Desktop.
TIOJ 1652, Alchemy. (98建中校內資訊能力競賽(prob3)) http://218.210.35.237:8080/JudgeOnline/showproblem?problem_id=1652
#include <cstdio>
#include <cstring>
#include <algorithm>
char l_exist[10000], r_exist[10000];
int n, m, val[10000], seq[10000];
int main() {
int i, j, k, diff, ans = 0;
scanf("%d", &n);
for (i=0; i<n; i++) {
scanf("%d", seq+i);
val[i] = seq[i];
}
std::sort(val, val+n);
for (i=m=1; i<n; i++)
if (val[i-1] != val[i])
val[m++] = val[i];
for (i=0; i<n; i++) {
seq[i] = std::lower_bound(val, val+m, seq[i]) - val;
}
for (i=0; i<n; i++) {
memset(l_exist, 0, sizeof(l_exist));
memset(r_exist, 0, sizeof(r_exist));
diff = 0;
for (j=i,k=i+1; j>=0 && k<n; j--,k++) {
if (!l_exist[seq[j]]) {
if (r_exist[seq[j]]) diff--;
else diff++;
l_exist[seq[j]] = 1;
}
if (!r_exist[seq[k]]) {
if (l_exist[seq[k]]) diff--;
else diff++;
r_exist[seq[k]] = 1;
}
if (diff==0 && k-j+1>ans) ans = k-j+1;
}
}
printf("%d\n", ans);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment