POJ 3617 Best Cow Line 贪心算法
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26670 | Accepted: 7226 |
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 思路:从左Left和从右Right同时开始扫,如果如果左边的值小于右边的值,则输出左边的,Left++。
如果左边的值大于右边的值,则输出右边的,Right++。
如果左边的值等于右边的值,则比较从左往右形成的字符串和从右往左形成的字符串哪个更小。输出小的那边的,改变Left或者Right。 代码:
#include <iostream>
using namespace std;
typedef long long ll; int n;
char a[]; int main() {
cin >> n;
for(int i = ;i < n; i++) cin >> a[i]; string s; int left = ;int right = n-;
while(left <= right){
int l = false;
for(int i = ;i <= right - left; i++){
if(a[left+i] < a[right-i]){
l = true;
}
if(a[left+i] != a[right-i]){
break;
}
} if(l) s += a[left++];
else s += a[right--];
}
for(int i = ;s[i]; i++){
if(i != &&i% == ) cout << endl;
cout << s[i];
} return ;
}
// writen by zhangjiuding
POJ 3617 Best Cow Line 贪心算法的更多相关文章
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- POJ 3617 Best Cow Line (贪心)
题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个. 析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是 ...
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- poj 3617 Best Cow Line (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- POJ 3617 Best Cow Line (字典序最小问题 & 贪心)
原题链接:http://poj.org/problem?id=3617 问题梗概:给定长度为 的字符串 , 要构造一个长度为 的字符串 .起初, 是一个空串,随后反复进行下列任意操作. 从 的头部删除 ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- POJ 3617 Best Cow Line (模拟)
题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...
- poj 3617 Best Cow Line
http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617 Description FJ is about t ...
随机推荐
- BZOJ 3674/BZOJ 3673 主席树
思路: 主席树维护可持久化数组 剩下的就是普通的并查集了- //By SiriusRen #include <cstdio> #include <cstring> #inclu ...
- spring《四》自动装配
byName模式<bean autowire="byName"> Spring会查找一个叫做date的bean定义. byType模式<bean autowire ...
- Chosen:Select 选择框的华丽变身
HTML Form 表单里的各种组件,例如文本输入框,textarea,按钮等,都可以通过CSS或其它技术进行美化,让它们看起来很漂亮了,唯独下拉列表选项框(select box),不管你怎么做,它摆 ...
- 一个基于React整套技术栈+Node.js的前端页面制作工具
pagemaker是一个前端页面制作工具,方便产品,运营和视觉的同学迅速开发简单的前端页面,从而可以解放前端同学的工作量.此项目创意来自网易乐得内部项目nfop中的pagemaker项目.原来项目的前 ...
- JQurey大纲
- Java中更精确的计时
我们一般的java运输计时代码是 long begintime = System.currentTimeMillis(); //运算代码 long endtinme=System.currentTim ...
- ZBrush中如何清除遮罩
在之前的学习中我们知道在ZBrush®中如何创建遮罩,在创建遮罩时怎样进行反转来选择反选遮罩,本文将详细讲解ZBrush中如何清除遮罩,当我们利用遮罩达到预期效果时就需要将遮罩清除了:或者在做了遮罩的 ...
- flex-2
1. 2. justify:整理版面 3. 4.归纳 justify-content:flex-start(默认).center.flex-end 下面还会提到剩下的两种项目在主轴上对齐方式: spa ...
- baidu练习/html/css
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- js在当前日期基础上,加1天 3天 7天 15天
需求 点击保障期的天数 根据起始时间算出结束时间 代码 //点击保障期触发的方法 periodChange(val,id){ this.activeNumperiod=val this.submitD ...