Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46960 | Accepted: 11949 |
Description
FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.
The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows' names.
FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.
FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.
Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line
Output
The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.
Sample Input
6 A C D B C B
Sample Output
ABCBCD
Source
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 const int max_n=2000+20; 7 8 int n; 9 char a[max_n]; 10 11 void solve() 12 { 13 int left=0,right=n-1,cnt=0; 14 while(left<=right) 15 { 16 ++cnt; 17 bool flag=true; 18 19 for(int i=0;i<=(right-left)/2;++i) 20 { 21 if(a[left+i]<a[right-i]) 22 { 23 break; 24 } 25 else if(a[left+i]>a[right-i]) 26 { 27 flag=false; 28 break; 29 } 30 } 31 32 if(flag) 33 { 34 putchar(a[left]); 35 ++left; 36 } 37 else 38 { 39 putchar(a[right]); 40 --right; 41 } 42 43 if(cnt%80==0) 44 { 45 putchar(' '); 46 } 47 } 48 } 49 50 int main() 51 { 52 scanf("%d",&n); 53 for(int i=0;i<n;++i) 54 { 55 scanf("%s",&a[i]); 56 } 57 //a[n]=' '; 58 //cout<<a<<endl; 59 solve(); 60 return 0; 61 }