poj3617 best cow line(贪心题)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 32687 | Accepted: 8660 |
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<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int N;
char a[]; int ans=; void solve(int k)
{
ans=;
int p=,q=k-;
while(q>=p)
{
int left=;
for(int i=;i+p<=q;i++)
{
if(a[p+i]<a[q-i])
{
left=;
break;
}
else if(a[p+i]>a[q-i])
{
left=;
break;
}
}
if(left)
{
printf("%c",a[p++]);
ans++;
}
else
{
printf("%c",a[q--]);
ans++;
} if(ans%==)
cout<<endl;
}
printf("\n");
}
int main()
{ cin>>N;
getchar();
for(int i=;i<N;i++)
{
cin>>a[i];
} solve(N);
}
poj3617 best cow line(贪心题)的更多相关文章
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- poj 3617 Best Cow Line 贪心模拟
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42701 Accepted: 10911 D ...
- poj3617 Best Cow Line(贪心,字典序问题)
https://vjudge.net/problem/POJ-3617 这类字符串处理字典序问题经常用到贪心, 每决定输出一个字符之前,都要前后i++,j--逐个比大小,直至比出为止. #includ ...
- POJ3617 Best Cow Line【贪心】
Description 给定长度为n的字符串S,要构造一个长度为n的字符串T.起初,T是空串,随后反复进行下列任意操作: 1.从S的头部删除一个字符,加到T的尾部 2.从S的尾部删除一个字符,加 ...
- bzoj4278[ONTAK2015]Tasowanie & bzoj1692[USACO 2007Dec]队列变换(Best Cow Line) 贪心正确性证明
做法网上到处都有就不说了. 这题其实是之前做的….不过由于人太傻现在才想明白比较字典序进行贪心的正确性…. 方便起见,在两个串的最右端都加上很大但不相同的字符,避免第lcp+1个字符不存在的边界. 如 ...
- POJ 3617 Best Cow Line (贪心)
题意:给定一行字符串,让你把它变成字典序最短,方法只有两种,要么从头部拿一个字符,要么从尾部拿一个. 析:贪心,从两边拿时,哪个小先拿哪个,如果一样,接着往下比较,要么比到字符不一样,要么比完,也就是 ...
- POJ3617 Best Cow Line
其实是学习参考了算法书的代码,但仍然是我自己写的,有小差别.贪心类型. #include <iostream> using namespace std; int main() { int ...
- POJ3617 Best Cow Line 馋
虽然这个问题很简单,但非常好,由于过程是很不错的.发展思路的比较 并鼓励人们,不像有些贪心太偏,推动穷人,但恼人 鉴于长N弦S,然后又空字符串STR.每当有两个选择 1:删S增加虚假的第一要素STR于 ...
- POJ 3617 Best Cow Line ||POJ 3069 Saruman's Army贪心
带来两题贪心算法的题. 1.给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下面两个操作:1.从S的头部删除一个字符,加到T的尾部.2.从S的尾部删除一个字符,加 ...
随机推荐
- java课后思考问题(七)
1.继承条件下的构造方法调用 package parent;class Grandparent { public Grandparent() { System.out.println("Gr ...
- css文本换行的问题
今天敲代码的时候发现了一个一直都没太注意的小问题,当我在一个200px的div中写了一长串的‘f ‘时发现没有换行 但加上空格或标点符号后就能自动换行 原来浏览器把它当成了一串完整的单词,所以默认不换 ...
- 博弈论 && 题目
终于我也开始学博弈了,说了几个月,现在才学.学多点套路,不深学.(~~) 参考刘汝佳蓝书p132 nim游戏: 假设是两维的取石子游戏,每次可以在任意一堆拿任意数量个(至少一根,因为这样游戏的状态集有 ...
- java 通用查询框架Querydsl 简介
Querydsl 是一个通用的查询框架,专注于通过JavaAPI构建类型安全的SQL查询说说Querydsl的优势吧: 1. Querydsl支持代码自动完成,因为才纯Java API编写查询,因此主 ...
- C#知识点-StopWatch-计时
目录 简单介绍 基本用法 结尾 简单介绍 Stopwatch 可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.一般用来测量代码执行所用的时间或者计算性能数据,在优化代码性能上可以使 ...
- css3相关样式
1.渐变 1.1 线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向 background: linear-gradient(direction, color-stop1 ...
- AngularJS(三):重复HTML元素、数据绑定
本文也同步发表在我的公众号“我的天空” 重复HTML元素 在前端的页面编写中,我们会经常遇到重复HTML元素,譬如绘制表格.菜单等,如以下代码显示一个简单的li列表: <body> ...
- str中的join方法,fromkeys(),set集合,深浅拷贝(重点)
一丶对之前的知识点进行补充 1.str中的join方法.把列表转换成字符串 # 将列表转换成字符串,每个元素之间用_拼接 s = "_".join(["天",& ...
- keil下JLINK在线调试仿真设置,SWD连接
keil下JLINK在线调试仿真设置,以下三个步骤搞定: 有时我们编译时会遇到空间不足的情况,首先我们应该把 flash和RAM的size 设置为当前所用芯片的大小,如下我使用了一个片上flash 2 ...
- sql优化实战:从1353秒到135秒(删除索引+修改数据+重建索引)
最近在优化日结存储过程,日结存储过程中大概包含了20多个存储过程. 发现其有一个存储过程代码有问题,进一步发现结存的数据中有一个 日期字段business_date 是有问题的,这个字段对应的类型是v ...