The minimum representation P1368

First of all we are looking for the smallest, in fact, can reverse thinking, all large are excluded can get a small detail, see the code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=3e5+5;
 4 int a[2*N],n;
 5 int main(){
 6     scanf("%d",&n);
 7     for(int i=1;i<=n;i++)
 8      scanf("%d",&a[i]),a[i+n]=a[i];
 9     int i=1,j=2,k;
10     while(i<=n&&j<=n){
11         for(k=0;a[i+k]==a[j+k]&&k<=n;k++);
12         if(a[i+k]>a[j+k])i+=k+1;
13         else j+=k+1;
14         if(i==j)i++;
15     }
16     int ans=min(i,j);
17     for(int i=0;i<n;i++)
18      printf("%d ",a[i+ans]); 
19     puts("");
20     return 0;
21 } 
Code

 

Guess you like

Origin www.cnblogs.com/coder-cjh/p/11404464.html