标签:location head lin end stream appear original already des
FJ is about to take his N (1 ≤ N ≤ 30,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.
每次只能从两边取,要求取出来之后字典序最小
输入格式:
Line 1: A single integer: N
输出格式:
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.
6 A C D B C B
ABCBCD
代码很丑勿喷
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n; 7 int head,tail,cnt; 8 char in[30010]; 9 int main(){ 10 cin>>n; 11 for(int i=1;i<=n;i++) cin>>in[i]; 12 head=1; 13 tail=n; 14 for(int i=1;i<=n;i++){ 15 if(in[head]==in[tail]){ 16 int hhead=head,ttail=tail; 17 bool check=0; 18 while(hhead<=ttail){ 19 if(cnt==80){ 20 printf("\n"); 21 cnt=0; 22 } 23 if(in[hhead]<in[ttail]){ 24 printf("%c",in[head]); 25 cnt++; 26 head++; 27 check=1; 28 break; 29 } 30 else if(in[hhead]>in[ttail]){ 31 printf("%c",in[tail]); 32 cnt++; 33 tail--; 34 check=1; 35 break; 36 } 37 38 hhead++; 39 ttail--; 40 } 41 if(!check){ 42 if(cnt==80){ 43 printf("\n"); 44 cnt=0; 45 } 46 printf("%c",in[head]); 47 cnt++; 48 head++; 49 50 } 51 } 52 else { 53 if(cnt==80){ 54 printf("\n"); 55 cnt=0; 56 } 57 if(in[head]<in[tail]){ 58 printf("%c",in[head]); 59 cnt++; 60 head++; 61 } 62 else{ 63 printf("%c",in[tail]); 64 cnt++; 65 tail--; 66 } 67 68 } 69 } 70 if(cnt) printf("\n"); 71 return 0; 72 }
贪心 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
标签:location head lin end stream appear original already des
原文地址:http://www.cnblogs.com/sdfzxh/p/6906502.html