贪心 洛谷P2870 Best Cow Line, Gold
来源:互联网 发布:mac其他怎么清理 编辑:程序博客网 时间:2024/05/17 13:14
题目描述
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.
输入输出样例
6ACDBCB
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 Best Cow Line, Gold
- P2870 (poj 3617) [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 可爱的贪心
- 后缀数组——洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
- poj3623 Best Cow Line, Gold(贪心)
- poj 3623 Best Cow Line, Gold(贪心)
- POJ 3623 Best Cow Line, Gold(贪心)
- [贪心+后缀数组] poj 3623 Best Cow Line, Gold
- POJ 3623 Best Cow Line, Gold(贪心)
- POJ3623 Best Cow Line,Gold
- POJ3623-Best Cow Line, Gold
- PKU 3623 Best Cow Line, Gold
- POJ-3623-Best Cow Line, Gold
- poj 3623 Best Cow Line, Gold
- POJ 3623 Best Cow Line, Gold(模拟)
- POJ 3623 Best Cow Line, Gold(模拟)
- POJ3623:Best Cow Line, Gold(后缀数组)
- POJ 3623Best Cow Line, Gold
- POJ3623 Best Cow Line, Gold 【后缀数组】
- KMP UVA1328 Period
- 树形DP 洛谷P2014 选课
- 倍增LCA NOIP2013 货车运输
- 快速幂 NOIP2013 转圈游戏
- 贪心 NOIP2013 积木大赛
- 贪心 洛谷P2870 Best Cow Line, Gold
- 二分图 洛谷P2055 [ZJOI2009]假期的宿舍
- 动态规划入门 洛谷P1108 低价购买
- 状压DP 洛谷P2704 炮兵阵地
- 区间DP NOIP2006 能量项链
- 动态规划入门 UVA12563 Jin Ge Jin Qu hao
- 区间DP UVA1052 A Spy in the Metro
- Manacher HDU3068 最长回文
- AC自动机 HDU2222 Keywords Search