poj 3623(贪心)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 6038 | Accepted: 2087 |
Description
FJ is about to take his N (1 ≤ N ≤ 30,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<stdio.h>
#include<string.h>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int N;
cin>>N;
char in[N];
char out[N+2];
int i=0;char c;
while(cin>>c){in[i++]=c;}
in[i]='\0';
int pos1=0;int pos2=strlen(in)-1;int pos=0;
while(pos1<=pos2)
{
if(in[pos1]<in[pos2])out[pos++]=in[pos1++];
else if(in[pos1]>in[pos2])out[pos++]=in[pos2--];
else //如果in[pos1]==in[pos2]
{
int t1=pos1;int t2=pos2;
while(in[t1]==in[t2]&&t1<=t2)
{
t1++;t2--;
}
if(in[t1]==in[t2])out[pos++]=in[pos1++];
else if(in[t1]<in[t2])out[pos++]=in[pos1++];
else out[pos++]=in[pos2--];
}
}
out[pos]='\0';
int cnt=0;
while(out[cnt]!='\0')
{
cout<<out[cnt];cnt++;
if(cnt%80==0)cout<<endl;
}
return 0;
}
poj 3623(贪心)的更多相关文章
- poj 3623 Best Cow Line, Gold 后缀数组 + 贪心
题目链接 题目描述 对于一个给定的字符串,可以从左右两端取字符,依次排列构成一个新的字符串. 求可能构成的字符串中字典序 最小的一个. 例:ACDBCB -> ABCBCD 思路 参考自 xue ...
- POJ - 1017 贪心训练
Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59725 Accepted: 20273 Descrip ...
- 【POJ 3623】 Best Cow Line, Gold (后缀数组)
[题意] [分析] 后缀数组水题,嗯,不认真看输出像我一样就会被坑.. #include<cstdio> #include<cstdlib> #include<cstri ...
- POJ 2376 贪心
题意:FJ希望它的牛做一些清洁工作.有N只牛和T个时间段,每只牛可以承担一段时间内的工作.FJ希望让最小数量的牛覆盖整个T,求出其数量.若无法覆盖整个T,则输出-1. 分析:首先要注意T表示T个时间段 ...
- poj 1328 贪心
/* 贪心.... 处理处每个点按照最大距离在x轴上的映射 然后我们就有了一些线段 目的是选取尽量少的点 使得每个线段内都有点出现 我们按照左端点排序 然后逐一处理 假设第一个雷达安在第一个线段的右端 ...
- Yogurt factory(POJ 2393 贪心 or DP)
Yogurt factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8205 Accepted: 4197 De ...
- Cleaning Shifts(POJ 2376 贪心)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15143 Accepted: 3875 ...
- POJ - 1456 贪心+并查集
做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可.时间复杂度O(n * d)当d都为10000时,很容易超时.由于这题数据比较水,所有贪心未超时. AC代码 #include ...
- poj supermaket (贪心)
http://poj.org/problem?id=1456 #include<cstring> #include<iostream> #include<algorith ...
随机推荐
- Spring MVC的异步模式(ResponseBodyEmitter、SseEmitter、StreamingResponseBody) 高级使用篇
DeferredResult高级使用 上篇博文介绍的它的基本使用,那么本文主要结合一些特殊的使用场景,来介绍下它的高级使用,让能更深刻的理解DeferredResult的强大之处. 它的优点也是非常明 ...
- Java代码执行顺序及多态体现
/** * Description: * 基类的引用变量可以只想基类的实例对象也可指向其子类的事来对象 * 接口的引用变量也可以指向实现类的实例对象 * 程序调用的方法在运行期才动态绑定 * 绑定指将 ...
- VMware Workstation改动存储位置之后,软件变成全英文,修改成中文的方法
今天想改动一下VMware Workstation的位置 改动之后打开软件,本来的中文改成了英文,整了很长时候,最后发现是因为改动一下位置之后,虽然zh_CN语言包还在,但是Vmware找不到本来的 ...
- PreparedStatement 以及事务的注意事项
a).PreparedStatement 可以进行批量操作,但是与Statement有一定的区别 1. Statement可以进行不同sql语句的批量操作 即可以同时进行 crud 操作. Strin ...
- Eclipse 新建.jsp页面后,页面头部标签报错的解决方法
Eclipse 新建.jsp页面后,页面头部标签报错的解决方法 1.报错地方: 2.解决方法: .jsp页面右键==>BUild Path ==>Configure Build Path. ...
- CodeForce20C
这是一个裸的最短路的模板题,但是它需要输出路径. 用dijkstra的话首先敲一个最短路的板子,其次开一个数组p[]来记录路径,但是怎么存呢?我们需要记录每一个点的前驱,因为如果记录后边的话,一个点可 ...
- 如何用纯 CSS 创作一个慧星拖尾效果的 loader 动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/YLRLaM 可交互视频教 ...
- hadoop批量命令脚本xcall.sh及jps找不到命令解决
1.xcall.sh批量命令脚本: #!/bin/bash params=$@ i=128 for (( i=128 ; i <= 131 ; i = $i + 1 )) ; do echo = ...
- Kong/Konga - Docker容器化安装
1.0 安装kong + postgresDB docker network create kong-net docker pull postgres:latest docker run -d --n ...
- JavaScript快速排序法实现数组排序
大致分三步: 1.找基准(一般是以中间项为基准) 2.遍历数组,小于基准的放在left,大于基准的放在right 3.递归 var arr = [10,8,6,9,1,7,1,13,5,1,9]; / ...