poj3617 贪心
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.
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
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.
Sample Input
6 A C D B C B
Sample Output
ABCBCD
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; #define MAX 2000int n; char s[MAX + 1];void solve() {int a = 0, b = n - 1;int ans = 1;while(a <= b) {bool left = false;for(int i = 0; a + i <= b; i++) {if(s[a + i] < s[b - i]){left = true;break;}else if(s[a + i] > s[b - i])break;} if(left) putchar(s[a++]);else putchar(s[b--]);if(ans%80 == 0){cout << endl;}ans++; }putchar('\n'); }int main() { cin >> n;char temp;for(int i = 0; i < n;) {scanf("%c", &temp);if(temp != '\n') {s[i] = temp;i++;}} solve();return 0; }
转载于:https://www.cnblogs.com/bearcarl/p/8544372.html
poj3617 贪心相关推荐
- 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262
POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...
- 挑战程序设计竞赛笔记-贪心算法
2.2 贪心算法 贪心算法的精髓在于,遵循某种规则,不断地选取当前最优解. 硬币问题 假设有 1 元,5元,10元,50元,100元,500元的硬币C1,C5,C10,C50,C500枚,现在需要凑出 ...
- 【挑战程序设计竞赛】- 2.2贪心法(硬币最少、区间覆盖、字典序最小、标记最少、木板切割)
四年前犯的错再做一遍还是会犯. 四年前不看presentation要求,四年后依然PE. 四年前忘记longlong,四年后还是会忘. 2.2 贪心法 核心思想:不断选取最优策略. 例题1-硬币:有1 ...
- 算法设计与分析第3章 贪心算法
第4章 贪心算法 贪心算法总是作出在当前看来最好的选择.也就是说贪心算法并不从整体最优考虑,它所作出的选择只是在某种意义上的局部最优选择. 贪心算法的基本要素 1.贪心选择性质 所谓贪心选择性质是指所 ...
- 贪心算法单源点最短路径例题c语言源代码,Dijkstra算法是解单源最短路径问题的一个贪心算法...
问题描述 给定一个带权有向图 G=(V,E) ,其中每条边的权是一个非负实数. 另外,还给定 V 中的一个项点,称为源. 现在我们要计算从源到所有其他各项点的最短路径长度. 这里的长度是指路上各边权之 ...
- Too Many Segments CF595D 贪心乱搞
传送门! 比赛的时候没有时间写了,看看了看大佬的代码,学习学习. 一开始实验室大佬说是用差分写的,但是看了代码发现打cf的人大家都是stl狂魔! 贪心思路:区间按照左端点排序,从1~2e5遍历每一个点 ...
- Roundgod and Milk Tea 贪心
这个题好像可以用一种类似与置换的贪心方法来做~ sum记为剩余奶茶,一开始sum等于奶茶和 ans记录已经喝的奶茶数 不用排序,我们就从前往后直接处理,考虑的是每一个班最多可以喝多少杯奶茶 就是从剩余 ...
- 贪心算法简单实践 -- 分糖果、钱币找零、最多区间覆盖、哈夫曼编解码
1. 贪心算法概览 贪心算法是一种算法思想.希望能够满足限制的情况下将期望值最大化.比如:Huffman编码,Dijkstra单源最短路径问题,Kruskal最小生成树 等问题都希望满足限制的情况下用 ...
- 贪心:expedition 最优加油方法
已知一条公路上,有一个起点与一个终点,这之间有n个加油站;已知从这n个加 油站到终点的距离d与各个加油站可以加油的量l,起点位置至终点的距离L与起 始时刻油箱中汽油量P;假设使用1个单位的汽油即走1个 ...
最新文章
- 怎样训练左右手协调_BF#拳击教学#自学课程:专项辅助训练【墙靶训练法+路训】...
- Android .classpath文件的作用
- Maven实战:Pom.xml详解
- 【大总结1】数据结构与传统算法总结
- Java管理商品信息
- python语言训练教程_PYTHON零基础快乐学习之旅(K12实战训练)
- 随机效应估算与固定效应估算_【机器学习】随机生存森林
- python运行脚本命令行选项
- 纯java生成验证码 java生成图片并保存
- 被3整除判断准则的证明
- Low Power概念介绍<Level Shifter>
- AsyncTask与Thread+Handler的区别、AsyncTask的使用
- cryptoJs 前端用法
- 做善良公社项目的经历和感受——善良公社
- 琐事记 - 2015/10/28
- CentOS 7账号密码忘了怎么办?
- 令人困惑的fitsSystemWindows
- CVPR 2020 | 《Learning to Transfer Texture from Clothing Images to 3D Humans》 论文解读
- Lora和LoraWAN
- java程序会发生内存泄露吗及内存泄漏场景