POJ 3617(贪心)
2017-10-09 18:32
225 查看
Best Cow Line
DescriptionFJ 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 phaseends, 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'sfinished, 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 lineOutputThe 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
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26621 | Accepted: 7215 |
6 A C D B C BSample Output
ABCBCD
题意: 给你一个长度为n的字符串,每次输出一个一个字符 只能是 头和尾的字符 ,要求是使得输出的字符 字典序
尽可能的小。
思路: 当然是贪心的思想,4000但是唯一一个不大好处理的点就是,当首尾字符串相等的时候,应该怎么处理,其实很简单,那就
比较下一个字符,,知道比较到字符不相等的地方,然后看 是首字符小还是尾字符小。。
代码:
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#define N 2005using namespace std;int n;char a;char fin;int main(){scanf("%d",&n);getchar();for(int i=0;i<n;i++){scanf("%c",&a[i]);getchar();}int cnt=0;int left=0;int right=n-1;while(left<=right){//printf("%d %d\n",left,right);int f=0;for(int i=0;i+left<=right;i++){if(a[left+i]<a[right-i]){f=1;break;}else if(a[left+i]>a[right-i]){f=0;break;}}if(f) printf("%c",a[left++]);else printf("%c",a[right--]);++cnt;if(cnt%80==0) printf("\n");}//printf("\n");return 0;}
相关文章推荐
- POJ 3617 Best Cow Line [贪心] 《挑战程序设计竞赛》2.2
- POJ 3617 Best Cow Line——贪心
- POJ 3617 (贪心)
- 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 3069 贪心
- poj-3617字典序最小 贪心
- 贪心基础题(区间调度问题+POJ_3617+POJ_3069)
- POJ 3617 Best Cow Line (贪心)
- poj 3617 Best Cow Line 【贪心】
- poj-3617字典序最小 贪心
- poj 3617 贪心
- POJ 3617 - Best Cow Line(贪心)
- POJ 3617 贪心
- poj-3617字典序最小 贪心
- POJ 3617 Best Cow Line [贪心] [字典序最小]