Best Cow Line - POJ 3617 水题
2014-10-27 10:37
281 查看
Best Cow Line
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
Sample Output
题意:组成一个字符串,每次可以取最上面的或是最下面的字符,使得字典序最小。
思路:从两端开始比较,找最小的,相同的话就继续往下找。
AC代码如下:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10538 | Accepted: 3169 |
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
题意:组成一个字符串,每次可以取最上面的或是最下面的字符,使得字典序最小。
思路:从两端开始比较,找最小的,相同的话就继续往下找。
AC代码如下:
#include<cstdio> #include<cstring> using namespace std; char s1[2010][2],s2[2010]; int main() { int n,m,i,j,k,l,r; bool left; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) scanf("%s",s1[i]); l=1;r=n;m=0; while(l<=r) { for(i=0;i*2<=r-l;i++) { if(s1[l+i][0]<s1[r-i][0]) { left=1; break; } else if(s1[l+i][0]>s1[r-i][0]) { left=0; break; } } if(left) s2[++m]=s1[l++][0]; else s2[++m]=s1[r--][0]; } for(i=1;i<=n;i++) { printf("%c",s2[i]); if(i%80==0) printf("\n"); } } }
相关文章推荐
- POJ 3617:Best Cow Line
- POJ 3617 Best Cow Line 贪心
- poj 3617 Best Cow Line(取字典序最小的一种排序)
- POJ 3617 Best Cow Line(贪心+最小字典序)
- P2870 (poj 3617) [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 可爱的贪心
- POJ 3617 Best Cow Line
- POj 3617 Best Cow Line
- POJ 3617 Best Cow Line
- poj 3623 Best Cow Line, Gold (题意和3617一样只要代码中的范围改一下)
- POJ-3617-Best Cow Line(贪心算法+Java)
- POJ 3617 Best Cow Line (贪心)
- poj 3617 Best Cow Line
- POJ 3617 Best Cow Line(字典序最小问题)
- poj 3617 Best Cow Line(简单贪心)
- POJ_3617_Best Cow Line
- POJ——3617 Best Cow Line
- POJ 3617 best cow line 贪心法
- Best Cow Line(POJ 3617) 贪心
- POJ 3617 Best Cow Line (贪心)
- POJ 3617 Best Cow Line