POJ 3617 Best Cow Line

Time Limit: 1000MS  Memory Limit: 65536K

【Description】

【题目描述】

FJ is about to take his N (1 ≤ N ≤ 2,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.

FJ打算带他的N(1 ≤ N ≤ 2,000)头奶牛去参加"Farmer of the Year"比赛。在这个比赛中每个农夫都会把他们的奶牛排成一队,然后经过评审团。

比赛方今年采用了一种新的登记方案:每头牛的出场都以名字首字母进行简要登记(换句话说,如果FJ带了Bessie、Sylvia和Dora参加,那么他只要登记成BSD)。登记结束后,每组评判根据奶牛名字首字母串字典序升序评判。

FJ今年事特多又得赶回农场,想早点完事。因此他决定在登记前把已经排好队的奶牛重排一遍。

FJ找了块地给比赛的奶牛排新队伍。接着他不断把第一头或最后一头牛从旧(或者剩下的)队伍转移到新队伍的尾部。搞定后,FJ会用这个新队伍登记。

给你这群奶牛的大写字母,找出上述方法排列后字典序最小的字符串。

【Input】

【输入】

* 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

* 第1行: 一个整数: N

* 第2..N+1行: 第i+1行包含表示第i个奶牛初始位置的一个大写字母('A'..'Z')

【Output】

【输出】

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.

输出所能构造的最小字典序字符串。每行(最后一行不用管)包含80头奶牛的大写字母('A'..'Z')。

【Sample Input - 输入样例】

【Sample Output - 输出样例】

6

A

C

D

B

C

B

ABCBCD

【题解】

贪心法,取字典序最小的元素。

输出时每次从旧队伍的头/尾取出较小的元素,如果字典序相同,就看看哪一边能更快地输出字典序较小的元素。

【代码 C++】

 1 #include<cstdio>
 2 char data[2005];
 3 int i;
 4 bool cmp(int L, int R){
 5     if (data[L] == data[R]){
 6         while (data[L] == data[R] && L < R) ++L, --R;
 7     }
 8     return data[L] < data[R];
 9 }
10 void opt(char now){
11     if (i == 80) i = 0, puts("");
12     putchar(now), ++i;
13 }
14 int main(){
15     int L, R, n;
16     scanf("%d", &n);
17     for (i = 0; i < n; ++i) getchar(), data[i] = getchar();
18     for (i = L = 0, R = n - 1; L <= R;){
19         if (cmp(L, R)) opt(data[L++]);
20         else opt(data[R--]);
21     }
22     return 0;
23 }

转载于:https://www.cnblogs.com/Simon-X/p/5309380.html

POJ 3617 Best Cow Line(最佳奶牛队伍)相关推荐

  1. 贪心  POJ - 3617 ​​​​​​​Best Cow Line

    Best Cow Line POJ - 3617 FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of ...

  2. POJ 3617 Best Cow Line 贪心算法

    Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 Descri ...

  3. POJ 3617 Best Cow Line 贪心

    不能单纯比较头尾两个字符,应该比较头尾两个字符串. //#pragma comment(linker, "/STACK:1024000000,1024000000") #inclu ...

  4. POJ 3617 Best Cow Line

    给定长度N的字符串S,要求构造一个长度为N的字符串T,起初T是一个空串,随后反复进行如下操作: 1.从字符串S的首部删除一个字符添加到T的末尾. 2.从字符串S的尾部删除一个字符添加到T的末尾. 要求 ...

  5. POJ 3618 Best Cow Line(贪心算法)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30454   Accepted: 8126 De ...

  6. 【POJ】3617 Best Cow Line (字典序 字符串)

    http://poj.org/problem?id=3617 给定长度为N(1≤N≤2000)的字符串S,要构造一个长度为N的字符串T.期初,T是一个空串,随后反复进行下列任意操作. 从S的头部删除一 ...

  7. P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希)

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希) 洛谷上这道水题丧心病狂地把数据范围加到了500000 普通的做法肯定A不了了, ...

  8. poj Best Cow Line

    Best Cow Line 题意: 就是说给你一系列字母, 要你取出字典序最小的字符串,每次只能从最上面或者最下面进行取,每次从上和从下选择最小的,若相等则向中间搜索,找到较小的. 注意: 1.先吸收 ...

  9. 3403: [Usaco2009 Open]Cow Line 直线上的牛

    3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 71  Solved: 62 ...

最新文章

  1. 基于Python的OpenCV轮廓检测聚类
  2. C#算法大全-1-Hanoi
  3. 将Numpy加速700倍——CuPy
  4. Windows Server 2008安装Memcached笔记
  5. PMP读书笔记(第2章)
  6. 西安工业学院计算机系王翊,西安文理学院艺术学院
  7. 再推新机!小米A3正式发布 售价249欧元起
  8. mysql 数据结构语句_SQL 数据结构操作语句
  9. 100-days: nineteen
  10. 翻译:神经网络 为什么要通过减去数据集的图像均值来标准化图像,而不是深度学习中的当前图像均值?
  11. 罚函数——内点罚函数算法
  12. 分享一下泛微OA与用友NC对接自动生成凭证的任务(一)
  13. Flink状态的缩放(rescale)与键组(Key Group)设计
  14. linux添加jetdirect协议,如何设置 HP JetDirect 设备的网络安全性?
  15. 这样的跳槽理由HR不排斥,赶紧记好!
  16. 三星Galaxy S20:将侧面按钮更改为电源按钮
  17. Word文档使用Mathtype如何实现公式自动居中并右对齐编号?
  18. css 背景颜色 background属性
  19. 一个简单好用的磨皮祛斑算法理论和python实现
  20. python打开zip文件_Python操作Zip文件

热门文章

  1. 比特币将来能取代黄金,成为世界货币?
  2. 将 ext_net 连接到 router - 每天5分钟玩转 OpenStack(145)
  3. 由中行IBM大型机宕机谈银行系统运维
  4. 同步/异步移动文件列表框选中的文件
  5. _VARIANT_T 到 CSTRING 转换
  6. bootstraptable控制分页_bootstrap table分页(前后端两种方式实现)
  7. 字符动图_手把手教你做一个python+matplotlib的炫酷的数据可视化动图
  8. Java知识系统回顾整理01基础05控制流程07结束外部循环
  9. 【ARTS】01_04_左耳听风-20181203~1209
  10. Linux 进程详解