题目链接:http://codeforces.com/problemset/problem/558/B
题意:
一个序列的美丽程度与其中某个数重复次数的最大值有关。求最短的子序列(连续的一段)使得其美丽程度与原序列相等。
1 #include <iostream> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <set> 7 #include <queue> 8 #include <stdbool.h> 9 10 #define LL long long 11 using namespace std; 12 const int maxn = 1e5 + 10; 13 14 15 struct Node{ 16 int val; 17 int pos; 18 }arr[maxn]; 19 20 bool cmp(Node a,Node b){ 21 if (a.val != b.val){ 22 return a.val < b.val; 23 } 24 else{ 25 return a.pos < b.pos; 26 } 27 } 28 29 30 int main(){ 31 int n; 32 scanf("%d",&n); 33 for (int i=1;i<=n;i++){ 34 scanf("%d",&arr[i].val); 35 arr[i].pos = i; 36 } 37 sort(arr+1,arr+1+n,cmp); 38 int ansl,ansr; 39 int start = 1; 40 int len = 0,cnt = 1; 41 for (int i=2;i<=n;i++){ 42 if (arr[i].val == arr[i-1].val){ 43 cnt++; 44 } 45 else{ 46 if (cnt > len){ 47 ansl = arr[start].pos; 48 ansr = arr[i-1].pos; 49 len = cnt; 50 } 51 else if (cnt == len){ 52 if (arr[i-1].pos-arr[start].pos+1 < ansr-ansl+1){ 53 ansl = arr[start].pos; 54 ansr = arr[i-1].pos; 55 } 56 } 57 start = i; 58 cnt = 1; 59 } 60 } 61 if (cnt > len){ 62 ansl = arr[start].pos; 63 ansr = arr[n].pos; 64 } 65 else if (cnt == len){ 66 if (arr[n].pos-arr[start].pos+1 < ansr-ansl+1){ 67 ansl = arr[start].pos; 68 ansr = arr[n].pos; 69 } 70 } 71 printf("%d %d ",ansl,ansr); 72 return 0; 73 }