CodeForces 590A Median Smoothing
构造题。
答案可以o(n)构造出来。首先要发现规律。只有01交替的串才可能变化,变化规律如下:
1开头,长度为偶数(0结尾):变(len-2)/2次 变完后 前半1 后半0
1开头,长度为奇数(1结尾):变(len-1)/2次 变完后 全为1
0开头,长度为偶数(1结尾):变(len-2)/2次 变完后 前半0 后半1
0开头,长度为奇数(0结尾):变(len-1)/2次 变完后 全为0
然后就是在原串中寻找01交替串,然后按照上述规律进行变换。
#include <cstdio>
#include <cstring>
#include <vector>
#include<cmath>
#include<ctime>
#include<cstdlib>
using namespace std; const int maxn=;
int n;
int a[maxn],b[maxn]; struct X
{
int st,en;
int num;
}s[maxn];
int tot; int main()
{
while(~scanf("%d",&n))
{
for(int i=; i<=n; i++) scanf("%d",&a[i]);
a[]=a[]; a[n+]=a[n]; a[n+]=;
tot=; int pos=;
for(int i=pos;i<=n+;)
{
for(int j=i;j<=n+;j++)
{
if(a[j]==a[i]) continue;
else
{
if(j-!=i)
{
s[tot].st=i;
s[tot].en=j-;
s[tot].num=a[i];
tot++;
}
i=j;
break;
}
}
} int ans=;
for(int i=;i<=n;i++) b[i]=a[i]; for(int i=;i<tot+;i++)
{
int len=s[i+].st-s[i].en+;
if(s[i].num==)
{
if(s[i+].num==)
{
ans=max(ans,(len-)/);
for(int k=s[i].en;k<=s[i].en+len/-;k++) b[k]=;
for(int k=s[i].en+len/;k<=s[i+].st;k++) b[k]=;
}
else
{
ans=max(ans,(len-)/);
for(int k=s[i].en;k<=s[i].en+len/-;k++) b[k]=;
for(int k=s[i].en+len/;k<=s[i+].st;k++) b[k]=;
}
}
else
{
if(s[i+].num==)
{
ans=max(ans,(len-)/);
for(int k=s[i].en;k<=s[i].en+len/-;k++) b[k]=;
for(int k=s[i].en+len/;k<=s[i+].st;k++) b[k]=;
}
else
{
ans=max(ans,(len-)/);
for(int k=s[i].en;k<=s[i].en+len/-;k++) b[k]=;
for(int k=s[i].en+len/;k<=s[i+].st;k++) b[k]=;
}
}
} printf("%d\n",ans);
for(int i=; i<=n; i++) printf("%d ",b[i]);
printf("\n");
}
return ;
}
CodeForces 590A Median Smoothing的更多相关文章
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- codeforces 590A A. Median Smoothing(思维)
题目链接: A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- 【22.70%】【codeforces 591C】 Median Smoothing
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
- Codeforces Round #327 (Div. 2)C. Median Smoothing 构造
C. Median Smoothing A schoolboy named Vasya loves reading books on programming and mathematics. He ...
- Codeforces 590 A:Median Smoothing
A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- cf590A Median Smoothing
A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing
http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...
随机推荐
- ios根据颜色返回图片
+(UIImage*) createImageWithColor:(UIColor*) color { CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); ...
- NSArray的containsObject漏洞
1.NSArray中的containsObject的用法 NSMutableArray *array=[NSMutableArray array]; if(![array containsObject ...
- blob的存储与读取
对于oracle数据库的blob的存储与读取对应的是byte数组. 将blob类型数据存入数据库: String blob: byte[] byte = blob.getBytes(); entity ...
- WebForm(aspx,cs,dll之间的关系)
WebForm分为两个文件aspx和aspx.cs,aspx是页面模板,是页面描述文件,就是html的内容,和aspx结合的更好,不用像一开始那样程序员自己去填充模板,控件都是定义在aspx中,内联的 ...
- c++ map unordered_map
map operator<的重载一定要定义成const.因为map内部实现时调用operator<的函数好像是const. #include<string> #include& ...
- VC2010编译错误
1. cannot convert parameter 1 from 'const char [43]' to 'LPCWSTR' 我是看了这个之后解决问题的~ http://blog.163.com ...
- C++中的函数指针和函数对象总结
篇一.函数指针函数指针:是指向函数的指针变量,在C编译时,每一个函数都有一个入口地址,那么这个指向这个函数的函数指针便指向这个地址.函数指针的用途是很大的,主要有两个作用:用作调用函数和做函数的参数. ...
- pipe的实现
本文进行了详细的描述,http://blog.csdn.net/zhouhong1026/article/details/8151235
- 字符串编码问题(Ascii、Unicode、UCS-2、GBK、UTF-8)
1.字符编码的发展 第一阶段:ASCII阶段,(American Standard Code for Information Interchange, "美国信息交换标准码),计算机当时只支 ...
- AIX创建用户
-bash-3.2# mkuser test 创建用户3004-689 User "test" exists.-bash-3.2# finge ...