1. 程式人生 > >貪心 洛谷P2870 [USACO07DEC]最佳牛線,黃金Best Cow Line, Gold

貪心 洛谷P2870 [USACO07DEC]最佳牛線,黃金Best Cow Line, Gold

勿噴 bsp bool lines nis origin ble 代碼 letter

[USACO07DEC]最佳牛線,黃金Best Cow Line, Gold

題目描述

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

  • Lines 2..N+1: Line i+1 contains a single initial (‘A‘..‘Z‘) of the cow in the ith position in the original line

輸出格式:

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.

輸入輸出樣例

輸入樣例#1:
6
A
C
D
B
C
B
輸出樣例#1:
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