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 ...
随机推荐
- POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)
POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...
- [学习笔记]插头dp
基于连通性的状压dp 巧妙之处:插头已经可以表示内部所有状态了. 就是讨论麻烦一些. 简介 转移方法:逐格转移,分类讨论 记录状态方法:最小表示法(每次要重新编号,对于一类没用“回路路径”之类的题,可 ...
- 【洛谷P1126】机器人搬重物
题目大意:给定一个 N 行,M 列的地图,一个直径为 1.6 的圆形机器人需要从起点走到终点,每秒钟可以实现:向左转,向右转,向前 1-3 步.求从起点到终点最少要多长时间. 题解:相比于普通的走迷宫 ...
- Java 使用 Enum 实现单例模式
在这篇文章中介绍了单例模式有五种写法:懒汉.饿汉.双重检验锁.静态内部类.枚举.如果涉及到反序列化创建对象时推荐使用枚举的方式来实现单例,因为Enum能防止反序列化时重新创建新的对象.本文介绍 Enu ...
- cookie工具包
package com.taotao.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLDec ...
- webpack打包提取css到独立文件
将本来镶嵌在bundle.js的css转到外面来,我们需要用到一个插件:extract-text-webpack-plugin 使用方法: 1.安装 npm i extract-text-webpac ...
- Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) F 构造
http://codeforces.com/contest/967/problem/F 题目大意: 有n个点,n*(n-1)/2条边的无向图,其中有m条路目前开启(即能走),剩下的都是关闭状态 定义: ...
- jquery的几种ajax方式对比
jquery的几种ajax方式对比 jquery的ajax方式有如下几种: 1. $.post(url,params,callback); 2. $.getJSON(url,params,ca ...
- innodb和myisam数据库文件存储详解以及mysql表空间
数据库常用的两种引擎有Innodb和Myisam,关于二者的区别参考:https://www.cnblogs.com/qlqwjy/p/7965460.html 1.关于数据库的存储在两种引擎的存储是 ...
- tensorboard遇到的坑
<ul><li>No graph definition files were found.</li></ul> <p>启动命令 tensor ...