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的更多相关文章

  1. P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 解题报告

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题意 给一个字符串,每次可以从两边中的一边取一个字符,要求取出的字符串字典序最小 可以Hash+二分 也可以S ...

  2. 洛谷P2870 - [USACO07DEC]最佳牛线Best Cow Line

    Portal Description 给出一个字符串\(s(|s|\leq3\times10^4)\),每次从\(s\)的开头或结尾取出一个字符接在新字符串\(s'\)的末尾.求字典序最小的\(s'\ ...

  3. 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

    思路大概和其他的题解一样: 从当前字符串最前面,最后面选一个字典序较小的然后拉到一个新的字符串序列中,如果相同就一直往中间扫描直到发现不同为止(一个字符如果被选中之后那么就不可以再次选择了),所以我们 ...

  4. luogu2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  5. [luoguP2870] [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(后缀数组)

    传送门 数据小的话贪心就行. 可以把这个串翻转再接到后面,再求后缀数组,求出 rank 数组就很简单了. ——代码 #include <cstdio> #include <iostr ...

  6. POJ3623 Best Cow Line, Gold 【后缀数组】

    最好的牛线,金 时间限制: 5000MS   内存限制: 65536K 提交总数: 5917   接受: 2048 描述 FJ即将把他的ñ(1≤ ñ ≤30,000)头牛竞争一年一度的"年度 ...

  7. 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 ...

  8. HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)

    题目 POJ 3617 和 这道题题目一样,只是范围稍稍再小一点. //模拟试试 #include<stdio.h> #include<string.h> #include&l ...

  9. 【POJ 3623】 Best Cow Line, Gold (后缀数组)

    [题意] [分析] 后缀数组水题,嗯,不认真看输出像我一样就会被坑.. #include<cstdio> #include<cstdlib> #include<cstri ...

随机推荐

  1. Python基础学习(三)

    了解了Python的基础使用,接下来继续练手廖雪峰老师的教学案例. 一.变量可以指向函数 说明,一个函数可以赋值给一个变量,该变量就会具有该函数的功能,举例: gg = abs print( gg(- ...

  2. 位运算的一种应用 和 hiho1516过河解题报告

    初始i=s 每次:i=(i-1) & s 直到i=0 etc.11000100000100000000 10000=10001 & 1100001000=01111 & 110 ...

  3. JS中的继承链

    我们首先定义一个构造函数Person,然后定义一个对象p,JS代码如下: function Person(name) { this.name = name; } var p = new Person( ...

  4. Codeforces 314 E. Sereja and Squares

    http://codeforces.com/contest/314/problem/E 题意: 原本有一个合法的括号序列 擦去了所有的右括号和部分左括号 问有多少种填括号的方式使他仍然是合法的括号序列 ...

  5. C标准库函数中复杂的函数声明

    <signal.h> 中有一个复杂的函数声明.很叫人费解. void (*signal(int sig, void (*handler)(int)))(int); 我们按照向右看向左看的黄 ...

  6. import和require的区别

    node编程中最重要的思想就是模块化,import和require都是被模块化所使用. 遵循规范 require 是 AMD规范引入方式 import是es6的一个语法标准,如果要兼容浏览器的话必须转 ...

  7. Android的layout_weight和weightSum

    先看一下weightSum属性的功能描述:定义weight总和的最大值.如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值.把weightSum的定义搁在这里,先去看 ...

  8. Javascript - 操作符

    操作符(Operator) void 如果void后是数字,就返回NAN,否则返回Undefined. alert(void "hello");//跟的字符 print undef ...

  9. MapReduce--shuffle

    一.Shuffle简介 Shuffle的正常意思是洗牌或弄乱,shuffle的大致范围就是把map task的输出结果有效的传送到Reduce端.也可以这样理解,shuffle描述着数据从map ta ...

  10. expect学习笔记及实例详解【转】

    1. expect是基于tcl演变而来的,所以很多语法和tcl类似,基本的语法如下所示:1.1 首行加上/usr/bin/expect1.2 spawn: 后面加上需要执行的shell命令,比如说sp ...