版权声明:欢迎转载,若转载,请标明出处,如有错误,请指点,也欢迎大佬们给出优化方法 https://blog.csdn.net/Charles_Zaqdt/article/details/89066341
题目链接:https://codeforces.com/contest/1119/problem/A
题意是输入n个数,然后找出两个不同的数使得他们之间的距离最大,输出这个最大的距离。
我的思路就是记录当前数的最左端和最右端的位置,然后分情况讨论一下就好了。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int n;
map<int,int> ma1,ma2;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
if(ma1[x] == 0) ma1[x] = i;
ma2[x] = i;
}
int ans = 0;
for(int i=1;i<=n;i++){
if(ma1[i] == ma2[i] && ma1[i] == 0) continue;
if(ma1[i] == 1 && ma2[i] == n) continue;
if(ma1[i] != 1 && ma2[i] != n){
ans = max(ans, max(ma2[i] - 1, n - ma1[i]));
continue;
}
if(ma1[i] == 1 || ma2[i] == n){
ans = max(ans, n - 1);
continue;
}
}
printf("%d\n", ans);
return 0;
}