P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
我比赛的时候A了,luogu上25分,QAQ,又憨又傻的200+代码,我为什么要干电脑干的事情,无语了。
如果左边<右边,取左
如果右边<左边,取右
如果相等,就向中间找,直到找到第一个不同的,然后给电脑指明下一步是取队首或队尾取就行了。
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.18
using namespace std;
int n;
int l,r;
int cnt;
char a[],b[];
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void deal(int &x,int &y)
{
int l=x,r=y;
while(a[l]==a[r]&&l<r)
l++,r--;
if(a[l]<a[r])
{
b[++cnt]=a[x];
x++;
}
else
{
b[++cnt]=a[y];
y--;
}
} int main()
{
in(n);
For(i,,n)
cin>>a[i];
l=,r=n;
while(l<=r)
{
if(a[l]<a[r])
{
b[++cnt]=a[l];
l++;
}
else
if(a[l]>a[r])
{
b[++cnt]=a[r];
r--;
}
else
{
if(l!=r)
deal(l,r);
else
{
b[++cnt]=a[r];
r--;
}
}
}
For(i,,cnt)
{
p(b[i]);
if(i%==)
p('\n');
}
return ;
}
憨代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.18
using namespace std;
int n;
int l,r;
int cnt;
char a[],b[];
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void deal(int &x,int &y)
{
int l=x,r=y;
char now=a[l];
while(a[l]==a[r]&&l<r)
{
l++,r--;
/* if(a[l]==a[r]&&a[l]!=now)
now=a[l];*/
}
if(l==r)
{
if(a[l]<now)
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
else
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
}
else
if(a[l]>=now&&a[r]>=now)
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
if(a[l]<=now&&a[r]<=now)
{
if(l-x>y-r)
{
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
if(l-x<y-r)
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
else
if(l-x==y-r)
{
if(a[l]>a[r])
{
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
}
}
else
if(a[l]>=now&&now>=a[r])
{
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
if(a[l]<=now&&now>=a[r])
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
} int main()
{
in(n);
For(i,,n)
cin>>a[i];
l=,r=n;
while(l<=r)
{
if(a[l]<a[r])
{
b[++cnt]=a[l];
l++;
}
else
if(a[l]>a[r])
{
b[++cnt]=a[r];
r--;
}
else
{
if(l!=r)
deal(l,r);
else
{
b[++cnt]=a[r];
r--;
}
}
}
For(i,,cnt)
{
p(b[i]);
if(i%==)
p('\n');
}
return ;
}
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold的更多相关文章
- P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 解题报告
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题意 给一个字符串,每次可以从两边中的一边取一个字符,要求取出的字符串字典序最小 可以Hash+二分 也可以S ...
- 洛谷P2870 - [USACO07DEC]最佳牛线Best Cow Line
Portal Description 给出一个字符串\(s(|s|\leq3\times10^4)\),每次从\(s\)的开头或结尾取出一个字符接在新字符串\(s'\)的末尾.求字典序最小的\(s'\ ...
- 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
思路大概和其他的题解一样: 从当前字符串最前面,最后面选一个字典序较小的然后拉到一个新的字符串序列中,如果相同就一直往中间扫描直到发现不同为止(一个字符如果被选中之后那么就不可以再次选择了),所以我们 ...
- luogu2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...
- [luoguP2870] [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(后缀数组)
传送门 数据小的话贪心就行. 可以把这个串翻转再接到后面,再求后缀数组,求出 rank 数组就很简单了. ——代码 #include <cstdio> #include <iostr ...
- POJ3623 Best Cow Line, Gold 【后缀数组】
最好的牛线,金 时间限制: 5000MS 内存限制: 65536K 提交总数: 5917 接受: 2048 描述 FJ即将把他的ñ(1≤ ñ ≤30,000)头牛竞争一年一度的"年度 ...
- POJ3623:Best Cow Line, Gold(后缀数组)
Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year&qu ...
- HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)
题目 POJ 3617 和 这道题题目一样,只是范围稍稍再小一点. //模拟试试 #include<stdio.h> #include<string.h> #include&l ...
- 【POJ 3623】 Best Cow Line, Gold (后缀数组)
[题意] [分析] 后缀数组水题,嗯,不认真看输出像我一样就会被坑.. #include<cstdio> #include<cstdlib> #include<cstri ...
随机推荐
- 【洛谷P3411】字串变换
题解:普通的 BFS 没什么可说的,字符串处理是这道题的难点,同时需要注意哈希判重. 另外,对于 \(string\) 类来说,学到了一个 push_back((char)) 操作. c++strin ...
- plsql批量导入sql文件
背景:有时候在两个数据库之间导入导出数据,不可避免的需要进行sql文件的批量导入,一个个导入效率太低,所以可以考虑批量导入的办法进行导入. 操作步骤 1.假设有三个sql脚本,分别为aa.sql,bb ...
- 数组初始化 memset fill
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #in ...
- Hadoop生态圈-Flume的主流Sinks源配置
Hadoop生态圈-Flume的主流Sinks源配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是配置的是Flume主流的Sinks,想要了解更详细的配置信息请参考官 ...
- Python中的列表解析和生成器表达式
Python中的列表解析和生成器表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.列表解析案例 #!/usr/bin/env python #_*_coding:utf-8 ...
- C# 获取文件图标
今天突然想到一个问题,如何去获取一个文件的关联图标呢?于是就上网搜索了一下.现总结如下: 首先明确问题:获取一个文件的关联图标或者是某个类型文件的显示图标. 在网上搜了一圈,发现方法还是比较多的,但是 ...
- vue-router基本概念及使用
index.html: <!DOCTYPE html> <html> <head> <title></title> <meta cha ...
- Hadoop源码阅读-HDFS-day1
HDFS声明及构造函数 @InterfaceAudience.Private @InterfaceStability.Evolving public class Hdfs extends Abstra ...
- Spark记录-Scala语句(运算符-if-for-while-try-模式匹配)
Scala条件运算符 Scala条件运算符在下表中列出. 运算符 操作 描述 && 与 运算符左侧和右侧的值为true.仅当左侧为真时,右侧才被计算. || 或 左侧或右侧的至少一个值 ...
- Spring RedisTemplate操作-事务操作(9)
@Autowired @Qualifier("redisTemplate") private RedisTemplate<String, String> stringr ...