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 ...
随机推荐
- Luogu 2764 最小路径覆盖问题 / Libre 6002 「网络流 24 题」最小路径覆盖 (网络流,最大流)
Luogu 2764 最小路径覆盖问题 / Libre 6002 「网络流 24 题」最小路径覆盖 (网络流,最大流) Description 给定有向图G=(V,E).设P是G的一个简单路(顶点不相 ...
- [SDOI2009]Bill的挑战——全网唯一 一篇容斥题解
全网唯一一篇容斥题解 Description Solution 看到这个题,大部分人想的是状压dp 但是我是个蒟蒻没想到,就用容斥切掉了. 并且复杂度比一般状压低, (其实这个容斥的算法,提出来源于y ...
- python的面向对象-面向对象设计
1.面向对象设计 def school(name,addr,type):#定义一个大函数的作用就是不能让狗调用学校的方法 #学校动作 def kaoshi(school): print("[ ...
- 生成ssh-key for GIthub
在Github里,如果我们想通过ssh的方式进行身份验证,我们就需要建立ssh-key: 方法一: git GUI,点击help,选择Generate ssh key
- webpack插件自动加css3前缀
想要webpack帮忙自动加上“-webkit-”之类的css前缀,我们需要用到postcss-loader和它的插件autoprefixer 1.安装 npm i postcss-loader au ...
- Object类型的怎么判断空值
例如 Object result; 我直接这样是不行的 if(result==null) //这样是错的 ... 要这样判断 if(result == System.DBNull.Value) //这 ...
- oracle 工作笔记,不定期更新
此博客为工作时,所见技术问题的解决方案笔记,欢迎大家转载,转载请注明出处,谢谢~ 更新时间: 2017-07-12 1. clob字段值读取时,借用extractvalue或extract函数读取节点 ...
- hdu 2433 Travel
http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...
- Nginx配置项优化(转载)
(1)nginx运行工作进程个数,一般设置cpu的核心或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 grep ^processor / ...
- 【两分钟视频教程】如何使用myeclipse在mac本机运行iOS配套的服务器
如何使用myeclipse在mac本机运行iOS配套的服务器