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.
样例
输入 6 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[500010]; char output[500010]; int idx; int SelectCopy(int l, int r) { if (l >= r) return 1; if (input[l] < input[r]) { return -1; } else if (input[l] > input[r]) { return 1; } else { int copyl = l + 1; int copyr = r - 1; return SelectCopy(copyl, copyr); } return 0; } 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 + 1; int copyr = r - 1; int selectidx = SelectCopy(copyl, copyr); if (-1 == selectidx) { output[idx] = input[l]; idx++; l++; } else if (1 == selectidx) { output[idx] = input[r]; idx++; r--; } } if (l <= r) { Select(l, r); } } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { char t; cin >> t; input[i] = t; } int l = 0; int r = N - 1; Select(l, r); for (int i = 0; i < idx; i++) { if (i % 80 == 0 && i>=80) cout << endl; cout << output[i]; } return 0; } 作者:defddr 链接:https://www.acwing.com/blog/content/178/ 来源:AcWing 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话