Best Cow Line <挑战程序设计竞赛> 习题 poj 3617
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的更多相关文章
- POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...
- Aizu 2249Road Construction 单源最短路变形《挑战程序设计竞赛》模板题
King Mercer is the king of ACM kingdom. There are one capital and some cities in his kingdom. Amazin ...
- 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181
POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...
- 挑战程序设计竞赛》P345 观看计划
<挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262
POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...
- poj 1852 ants 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题
[题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...
随机推荐
- C# base
using System; using System.Collections.Generic; using System.Text; namespace 继承 { class Program { st ...
- WPF数据可视化-瀑布图
实现方式一: 将数据(Point[])根据索引沿X轴使用虚拟画布进行绘制,每个数据绘制大小为1px * 1px:最终绘制出的宽度等于数据的总长度.标记并存储当前绘制的图为PreviousBitmap; ...
- Nginx的安装及配置
1.概述 Nginx是开源免费的一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.其特点是占有内存少,并发能力强,使用nginx网站用户有很多,如百 ...
- 基于titanic数据集预测titanic号旅客生还率
数据清洗及可视化 实验内容 数据清洗是数据分析中非常重要的一部分,也最繁琐,做好这一步需要大量的经验和耐心.这门课程中,我将和大家一起,一步步完成这项工作.大家可以从这门课程中学习数据清洗的基本思路以 ...
- maven 解决jar包冲突及简单使用
maven 解决jar包冲突 1.jar包冲突原因 maven中使用坐标导入jar包时会把与之相关的依赖jar包导入(导入spring-context的jar时就会把spring的整个主体导入) ,而 ...
- ArcGIS API for JavaScript小白入门
简单理解就是:通过js调用arcgis相关的方法和通过html引入css等资源来展示地图,代码如下: <!DOCTYPE html> <html> <head> & ...
- 团队项目之测试与发布(Alpha版本)
小组:BLACK PANDA 时间:2019.12.05 测试报告 1.测试找出的BUG 图片上传,文件过大会出错 用户可访问不具权限的URL 空字段导致异常 serializable反序列化时版本不 ...
- Quartznet速记
参与 https://www.cnblogs.com/lzrabbit/archive/2012/04/14/2371420.html 1.XML配置 参考:https://www.cnblogs.c ...
- oracle截取时间的年/月/日/时/分/秒
修改日期格式为年月日时分秒: alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';select to_char(sysdate,'yyy ...
- MySQL数据库:合并结果集
合并结果集 union----合并结果集 对合并后的结果集中的重复数据也会自动去重 select sName from students union select tName from Teacher ...