POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line
Time Limit: 1000MS Memory Limit: 65536K
|
【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. |
FJ打算带他的N(1 ≤ N ≤ 2,000)头奶牛去参加"Farmer of the Year"比赛。在这个比赛中每个农夫都会把他们的奶牛排成一队,然后经过评审团。 比赛方今年采用了一种新的登记方案:每头牛的出场都以名字首字母进行简要登记(换句话说,如果FJ带了Bessie、Sylvia和Dora参加,那么他只要登记成BSD)。登记结束后,每组评判根据奶牛名字首字母串字典序升序评判。 FJ今年事特多又得赶回农场,想早点完事。因此他决定在登记前把已经排好队的奶牛重排一遍。 FJ找了块地给比赛的奶牛排新队伍。接着他不断把第一头或最后一头牛从旧(或者剩下的)队伍转移到新队伍的尾部。搞定后,FJ会用这个新队伍登记。 给你这群奶牛的大写字母,找出上述方法排列后字典序最小的字符串。 |
|
【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 |
* 第1行: 一个整数: N * 第2..N+1行: 第i+1行包含表示第i个奶牛初始位置的一个大写字母('A'..'Z') |
|
【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. |
输出所能构造的最小字典序字符串。每行(最后一行不用管)包含80头奶牛的大写字母('A'..'Z')。 |
|
【Sample Input - 输入样例】 |
【Sample Output - 输出样例】 |
|
6 A C D B C B |
ABCBCD |
【题解】
贪心法,取字典序最小的元素。
输出时每次从旧队伍的头/尾取出较小的元素,如果字典序相同,就看看哪一边能更快地输出字典序较小的元素。
【代码 C++】
#include<cstdio>
char data[];
int i;
bool cmp(int L, int R){
if (data[L] == data[R]){
while (data[L] == data[R] && L < R) ++L, --R;
}
return data[L] < data[R];
}
void opt(char now){
if (i == ) i = , puts("");
putchar(now), ++i;
}
int main(){
int L, R, n;
scanf("%d", &n);
for (i = ; i < n; ++i) getchar(), data[i] = getchar();
for (i = L = , R = n - ; L <= R;){
if (cmp(L, R)) opt(data[L++]);
else opt(data[R--]);
}
return ;
}
POJ 3617 Best Cow Line(最佳奶牛队伍)的更多相关文章
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- poj 3617 Best Cow Line (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- poj 3617 Best Cow Line
http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...
- POJ 3617 Best Cow Line (模拟)
题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...
- poj 3617 Best Cow Line 解题报告
题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...
- POJ 3617 Best Cow Line (字典序最小问题 & 贪心)
原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...
随机推荐
- 【MFC三天一个游戏】之 局域网黑白棋
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611 花了三天上班时间,妈的上班写就是不能静下心来,擦,要防BOSS巡山.... 以前也写过 ...
- tomcat监控脚本
工作所需,匆匆忙忙写了个监控tomcat的shell脚本,大概思路是这样的:先检测tomcat进程是否存在,如果不存在就启动,如果进程存在,检测页面返回码状态,如果是200就是正常,如果不是就重启. ...
- 非常好!!!【从头开始写操作系统系列】实现一个-GDT(1)【转】
转自:http://blog.csdn.net/luoyhang003/article/details/47338019 权声明:本文为博主原创文章,未经博主允许不得转载.(文章来源:http://b ...
- 将Nagios监控信息存入Mysql
一.NDOUtils安装需求: nagios:安装方法:http://www.cnblogs.com/Richardzhu/p/3340638.html mysql:源码安装方法:http://www ...
- HDU 1827:Summer Holiday(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=1827 思路:强连通分量缩点后找入度为0的点,然后对于属于该强连通分量的找一个最小耗费的入口. #include ...
- 3G中的A-GPS移动定位技术
位置业务(LBS,Location Based Service)是指移动网络通过特定的定位技术来获取移动终端的位置信息,从而为终端用户提供附加服务的一种增值业务,可广泛应用于紧急救援.导航追踪.运输调 ...
- 利用Session实现一次验证码
Session可避免表单的重复提交:实现一次表单提交,可避免恶意提交: 1.首先建立一个Servlet类:ValidateColorServlet,里边有获取验证码的方法,并且验证码是大小写区分: p ...
- 算法心得2:关于k个最小和问题的思考
问题描述如下: 有k个整数数组,各包含k个元素.在每个数组中取一个元素加起来,可以得到k^k个和.求这些和中最小的k个值(重复计算的算多次). 如果同时考虑这k个数组的取值情况,其复杂程度不言而喻,并 ...
- 用python requests库写一个人人网相册爬虫
担心人人网会黄掉,写个爬虫,把我的相册照片都下载下来.代码如下: # -*- coding: utf-8 -*- import requests import json import os def m ...
- HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...