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高级特性——切片(Slice)

    摘录廖雪峰网站 定义一个list: L = ['haha','xixi','hehe','heihei','gaga'] 取其前三个元素: >>> L[0],L[1],L[2] (' ...

  2. [springMvc]常见配置

    [springMvc]常见配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  3. js Set对象

    1.将数组转换成Set对象 let arr1 = new Set([1,2,3,4]) console.log(arr1) //{1,2,3,4} 2.数组去重 let arr2 = new Set( ...

  4. js中关于带数字类型参数传参丢失首位数字0问题

    最近在项目中遇到一个问题,js中传带有数字的参数时,如果参数开头有数字0,会把0给去掉. 例如: 方法abc(0123456,789); 方法abc中获取的参数0123456就会变为123456. 原 ...

  5. 日常工作中VBA代码积累

    1.超链接地址提取 Function GetURL(rng As Range) As String On Error Resume Next GetURL = rng.Hyperlinks(1).Ad ...

  6. Nginx基础知识点总结和优化项

    1.什么是Nginx? Nginx是一个高性能的HTTP和反向代理服务器,常用于做负载均衡服务器 2.为什么要用Nginx?跨平台.配置简单非阻塞.高并发连接:处理2-3万并发连接数,官方监测能支持5 ...

  7. Weather with you主题说明

    使用前请确保拥有js权限!!! 源代码: css: /*广告去死*/ #ad_t2 { display: none !important; } #i-amphtml-fill-content { di ...

  8. python与数据库交互的模块pymysql

    一.Mysql 1.前提 pip install pymysql import pymysql 2.详情 Connection对象 =====>用于连接数据库 用于建立与数据库的连接 创建对象: ...

  9. win10 安装cuda和cudnn

    首先通过nvidia-smi 查看自己的显卡驱动对应的cuda版本. 参考:https://blog.csdn.net/qq_40212975/article/details/89963016 再去官 ...

  10. 【使用篇二】SpringBoot整合SpringDataJPA(18)

    一.pom.xml添加依赖 <dependencies> <!--web--> <dependency> <groupId>org.springfram ...