codeforces 590A A. Median Smoothing(思维)
题目链接:
2 seconds
256 megabytes
standard input
standard output
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.
Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bnobtained by the following algorithm:
- b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.
- For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1.
The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.
In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.
Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.
Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.
The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence.
The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 1), giving the initial sequence itself.
If the sequence will never become stable, print a single number - 1.
Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space — the resulting sequence itself.
4
0 0 1 1
0
0 0 1 1
5
0 1 0 1 0
2
0 0 0 0 0 题意: 给一个01串,每次每一位的变换都是这一位相邻的左右两个然后三人表决器的结果;问经过最少多少次变换会稳定,且输出最后的状态; 思路: 可以方向如果多于两个的相同的在一起,那么这些相邻的就不会变化了,这样就把这个串分成了多个子串,然后可以发现这些字串的最后状态和其左右界有关,最少的操作数等于(r-l)/2;
具体的看代码; AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=5e5+;
int n,a[N],p[N],nex[N];
int fun(int l,int r)
{
if(a[r]==a[l])
{
for(int i=l;i<=r;i++)a[i]=a[l];
}
else
{
for(int i=l;i<=l+(r-l)/;i++)a[i]=a[l];
for(int i=l+(r-l)/+;i<=r;i++)a[i]=a[r];
}
return (r-l)/;
}
int main()
{
read(n);
Riep(n)read(a[i]),p[i]=nex[i]=i;
for(int i=;i<=n;i++)
{
if(a[i]==a[i-])p[i]=p[i-],nex[p[i]]=i;
}
int l=nex[],r=n;
int ans=;
for(int i=nex[]+;i<n;i++)
{
if(nex[i]!=i)
{
r=i;
ans=max(ans,fun(l,r));
i=nex[i];
l=i;
}
}
r=p[n];
ans=max(ans,fun(l,r));
cout<<ans<<"\n";
Riep(n)printf("%d ",a[i]);
return ;
}
codeforces 590A A. Median Smoothing(思维)的更多相关文章
- 【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) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- 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 ...
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- CodeForces 590A Median Smoothing
构造题. 答案可以o(n)构造出来.首先要发现规律.只有01交替的串才可能变化,变化规律如下: 1开头,长度为偶数(0结尾):变(len-2)/2次 变完后 前半1 后半01开头,长度为奇数(1结尾) ...
- Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing
http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...
随机推荐
- Castle学习笔记----初探IOC容器
通常IOC实现的步骤为-->建立容器-->加入组件-->获取组件-->使用组件. 1.建立容器 建立容器也就是IWindsorContainer.接着我门要向容器中注册服务,并 ...
- Castle IOC容器内幕故事(上)
主要内容 1.WindsorContainer分析 2.MicroKernel分析 3.注册组件流程 一.WindsorContainer分析 WindsorContainer是Castle的IOC容 ...
- 《Spring3.0就这么简单》第1章快速入门
问题 [如何将jsp中传递到HttpServletRequest的参数,自动装配到Java对象中] [成功] public void setUsernAme(String username) < ...
- ps:探索按钮按起落下的技巧
(从死了一次又一次终于挂掉的百度空间中抢救出来的,发表日期 2014-07-10) 先上图: 那个看上去想按下去的,那个看上去像自然地呢? 显而易见: 第一像按下去的,第二个像自然地. 原因: 渐变: ...
- CANBus Determining Network Baud Rate, Automatic bit-rate detection
http://www.canbushack.com/blog/index.php?title=determining-network-baud-rate Determining Network Bau ...
- wpa_supplicant 连接成功后,如何配置wlan0与br0 协调上网
wlan0 地址,路由配置完成后,加入两条iptables 规则. #iptables -A FORWARD -i wlan0 -o br0 -s -m state --state NEW -j AC ...
- 理解sizeof
1.sizeof返回的是字节个数,内存编址的最小单元是字节.因此,空对象,bool值占用的内存也是一个字节. 2.可以对哪些东西求sizeof ? a.对象和类型.如int a; sizeof(a), ...
- BZOJ 1061: [Noi2008]志愿者招募 费用流
1061: [Noi2008]志愿者招募 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1061 Description 申奥成功后,布布 ...
- hdu 5276 YJC tricks time 数学
YJC tricks time Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- 查看浏览记录(thinkphp)
if ((MODULE_NAME == 'Goods' || MODULE_NAME == 'News') && ACTION_NAME == 'info' && $t ...