P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
poj 3617 http://poj.org/problem?id=3617

题目描述
FJ is about to take his N (1 ≤ N ≤ 500,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.

样例

输入

A
C
D
B
C
B
输出
ABCBCD

简单的说就是从输入字符串队首队尾抽取字符串组成输出 要求输出字符串字典序最小

算法1
我们来贪心选取 队首队尾哪个字典序小 就选择哪个。
关键在队首队尾相同那么就看次一级的字母的字典序 但是要考虑诸如 aca vav aa 等边界情况。
需要处理好
吐槽下 POJ 的提交和没有错误提示。 洛谷倒是不错 就是数据量变大了 把我代码卡成TLE了

C++ 代码

#include <iostream>
#include <queue>
#include <string> using namespace std; char input[];
char output[];
int idx; int SelectCopy(int l, int r)
{
if (l >= r) return ; if (input[l] < input[r]) {
return -;
}
else if (input[l] > input[r]) {
return ;
}
else {
int copyl = l + ; int copyr = r - ;
return SelectCopy(copyl, copyr);
} return ;
} void Select(int l, int r)
{
if (l == r) { output[idx] = input[l]; idx++; return; }
if (l > r) return; if (input[l] < input[r]) {
output[idx] = input[l];
idx++; l++;
}
else if (input[l] > input[r]) {
output[idx] = input[r];
idx++; r--;
}
else {
//相等
int copyl = l + ; int copyr = r - ;
int selectidx = SelectCopy(copyl, copyr);
if (- == selectidx) {
output[idx] = input[l];
idx++; l++;
}
else if ( == selectidx) {
output[idx] = input[r];
idx++; r--;
}
} if (l <= r) {
Select(l, r);
} } int main()
{
int N;
cin >> N;
for (int i = ; i < N; i++) {
char t;
cin >> t;
input[i] = t;
} int l = ; int r = N - ; Select(l, r);
for (int i = ; i < idx; i++) {
if (i % == && i>=) cout << endl;
cout << output[i];
} return ;
} 作者:defddr
链接:https://www.acwing.com/blog/content/178/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Best Cow Line <挑战程序设计竞赛> 习题 poj 3617的更多相关文章

  1. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

  2. Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题

    King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...

  3. 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181

    POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...

  4. 挑战程序设计竞赛》P345 观看计划

                                                 <挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...

  5. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  6. 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262

    POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...

  7. poj 1852 ants 题解《挑战程序设计竞赛》

    地址  http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...

  8. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  9. 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题

    [题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...

随机推荐

  1. Python高级特性——生成器(generator)

    通过上节的学习,我们知道使用列表生成式,可以直接创建一个列表.但是,有些时候,受到内存的限制等实际情况,列表生成式无法满足.比如,一个长度为1000万的列表,普通内存根本就不够,又或者实际处理的过程中 ...

  2. P1356 数列的整除性

    dp百题进度条[2/100] 题目链接 题目描述 对于任意一个整数数列,我们可以在每两个整数中间任意放一个符号'+'或'-',这样就可以构成一个表达式,也就可以计算出表达式的值.比如,现在有一个整数数 ...

  3. Ligg.EasyWinApp-100-Ligg.EasyWinForm:一款Winform应用编程框架和UI库介绍

        本项目是一个Winform应用编程框架和UI库.通过这个该框架,不需任何代码,通过XML配置文件,搭建任意复杂的Windows应用界面,以类似Execel公式的方式实现基本的过程控制(赋值.条 ...

  4. JavaScript for 、for...of 、for...in 等 iteration 效率测试

    由于不同浏览器,不同版本性能不一,且控制台本质是是套用了一大堆eval,沙盒化程度高,所以需使用node环境测试来提高准确性 // 准备待测数组 const NUM = 1e7; let arr = ...

  5. C lang: The Command line

    Ax_command line h Ax_a command line describe The command line is in enviroment for DOS,to user opera ...

  6. 47.QT-QChart之曲线图,饼状图,条形图使用

     1.使用准备 在pro中, 添加QT+= charts 然后在界面头文件中添加头文件并声明命名空间,添加: #include <QtCharts> QT_CHARTS_USE_NAMES ...

  7. 抖音短视频教程VIP培训课程(2019实时更新中)

    抖音联盟,抖友会,抖音联盟会员,抖音联盟学员,抖音批量做号团队,工作室带队,联盟学员统一官网认证可查,统一变现渠道担保,成熟技术技术后盾,实时工作室真机实测规则,抖音情感励志书单模式2.0升级,拒绝落 ...

  8. SAP之RFC_READ_TABLE

    RFC_READ_TABLE 是SAP系统自带的RFC函数,用于读取SAP数据库表的结构和数据.使用方法如下: IMPORTINGQUERY_TABLE:读取的表名DELIMITER:输出字段(DAT ...

  9. MySQL数据库~~~~ 完整性约束

    1. not null 与 default not null : 不可空 default : 默认值 例: create table t1(id int not null default 2); 2. ...

  10. linux添加磁盘分区做为swap分区

    在部署oracle时,根据内存的不同,会对swap分区大小有不同要求,具体参考https://www.cnblogs.com/chxmtl/p/11672053.html 下面为具体操作步骤. 1.新 ...