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的额 ...
随机推荐
- JavaScript 递归遍历json串获取相关数据
递归遍历json串获取相关数据 by:授客 QQ:1033553122 1. 测试数据 // 导航菜单 [ { id: 1, parentId: 0, parentName: null, na ...
- 搭建mount+nfs远程挂载
需求背景: 192.168.10.100 源服务器 目录:/root/test 目录属主属组普通用户,权限777 192.168.10.111 目标服务器 目录:/root/test111 目录属主属 ...
- Nginx日志常见时间变量解析
$request_time 官方解释:request processing time in seconds with a milliseconds resolution; time elapsed b ...
- Map随笔:最常用的Map——HashMap
目录 Map随笔:最常用的Map--HashMap 前言: 1,HashMap的结构 2,HashMap的一些属性(JDK8) 3,HashMap的构造函数(JDK8) 4,HashMap的一些方法( ...
- SQL Server 索引分析开关
set statistics io onset statistics profile on
- [译]Vulkan教程(10)交换链
[译]Vulkan教程(10)交换链 Vulkan does not have the concept of a "default framebuffer", hence it r ...
- IT兄弟连 HTML5教程 HTML5表单 HTML5新增表单元素
HTML5有一些新的表单元素:<datalist>.<keygen>.<output>.不是所有的浏览器都支持HTML5新的表单元素,但即使浏览器不支持该表单属性, ...
- Java 创建线程的3种方法及各自优势
1. 继承 Thread 类,然后调用 start 方法. class MyThread extends Thread { //重写run方法,线程运行后,跑的就是run方法 public void ...
- ETCD:实验特性和APIs
原文地址:Experimental features and APIs 大多数情况下,etcd项目是稳定的,但我们仍在快速发展! 我们相信快速发布理念. 我们希望获得有关仍在开发和稳定中的功能的早期反 ...
- RocketMQ 升级到主从切换(DLedger、多副本)实战
目录 1.RocketMQ DLedger 多副本即主从切换核心配置参数详解 2.搭建主从同步环境 3.主从同步集群升级到DLedger 3.1 部署架构 3.2 升级步骤 3.3 验证消息发送与消息 ...