题目链接:http://codeforces.com/problemset/problem/590/A

题目大意是给一个串,头和尾每次变换保持不变。

中间的a[i]变成a[i-1],a[i],a[i+1]的中位数,而且此题串是01串。

对于01串

0 0 0中位数是0

0 0 1中位数是0

0 1 1中位数是1

1 1 1中位数是1

所以

1、串中有两个相邻以上的0或者1是保持不变的。

2、会变的只有是两个1中间的0或者两个0中间的1。

但是到这里的话,虽然证明了肯定能变成稳定态,就算把相同数字分组模拟,给1010101010.....这种形式的话需要O(n)次才能稳定。自然不能模拟。。

由于条件2,发现如果两个满足1的串中间夹着0101这种间隔的串,那么这一段只有中间0101串会变化。

那么我只需要输入时分组,对于每两个满足1的串处理中间的0101串,然后取所有0101串变换的最大次数即为答案所要求。

然后考虑两个满足1的串:

两头是0或者两头是1是同一种情况:

1110101010111 ->变换4次得到1111111111111

一头是0一头是1:

111010101000->变换3次得到111111000000

然后就发现就是变换(中间串长度+1)/2次,得到的全1或0,或者一半1一半0。

然后写的时候要特殊考虑一下一开始全0或1,或者一半0一半1发情况。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxN = ;
int n, top;
struct node
{
bool val;
int num;
}s[][maxN]; void input()
{
top = -;
int u;
for (int i = ; i < n; ++i)
{
scanf("%d", &u);
if (top == - || u != s[][top].val)
{
top++;
s[][top].val = u;
s[][top].num = ;
}
else s[][top].num++;
}
} void work()
{
int run = , ttop = -, from, cnt;
for (int i = ; i <= top; i++)
{
ttop++;
s[][ttop] = s[][i];
if (i < top- && s[][i+].num == )
{
from = i;
i++;
while (s[][i].num == && i < top) i++;
if (s[][from].val == s[][i].val)
{
cnt = i-from+;
ttop++;
s[][ttop].val = s[][i].val;
s[][ttop].num = cnt-;
run = max(run, cnt/);
}
else
{
cnt = i-from+;
ttop++;
s[][ttop].val = s[][from].val;
s[][ttop].num = cnt/-;
ttop++;
s[][ttop].val = s[][i].val;
s[][ttop].num = cnt/-;
run = max(run, cnt/-);
}
i--;
}
}
printf("%d\n", run);
bool flag = false;
for (int i = ; i <= top; ++i)
{
for (int j = ; j < s[][i].num; ++j)
{
if (flag) printf(" ");
printf("%d", s[][i].val);
flag = true;
}
}
printf("\n");
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
input();
work();
}
return ;
}

ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)的更多相关文章

  1. ACM学习历程—Codeforces Round #354 (Div. 2)

    http://codeforces.com/contest/676 在allzysyz学弟和hqwhqwhq的邀请下,打了我的第三场CF... 毕竟在半夜..所以本来想水到12点就去睡觉的...结果一 ...

  2. ACM学习历程—CodeForces 601A The Two Routes(最短路)

    题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...

  3. CodeForces 590A Median Smoothing

    构造题. 答案可以o(n)构造出来.首先要发现规律.只有01交替的串才可能变化,变化规律如下: 1开头,长度为偶数(0结尾):变(len-2)/2次 变完后 前半1 后半01开头,长度为奇数(1结尾) ...

  4. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  5. ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)

    Description Let's consider one interesting word game. In this game you should transform one word int ...

  6. ACM学习历程—UESTC 1226 Huatuo's Medicine(数学)(2015CCPC L)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. ...

  7. ACM学习历程—UVALive 7147 World Cup(分类讨论 && 贪心)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  9. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

随机推荐

  1. 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双

    [BZOJ3331][BeiJing2013]压力 Description 如今,路由器和交换机构建起了互联网的骨架.处在互联网的骨干位置的核心路由器典型的要处理100Gbit/s的网络流量.他们每天 ...

  2. Lorenzo Von Matterhorn(STL_map的应用)

    Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. [postfix]添加黑名单

    最近公司员工的邮箱总是收到twoomail.com的邀请邮件,邮箱服务器还没有添加过黑名单呢,就拿它开刀吧. 在主配置文件中添加如下配置 #vi /etc/postfix/main.cf #black ...

  4. Js自定义异常

    function MyError(msg){ this.name="MyError"; this.message = msg || "自定义异常的默认消息";} ...

  5. Navicat试用期破解方法(转)

    转载网址https://blog.csdn.net/Jason_Julie/article/details/82864187 1.按步骤安装Navicat Premium,如果没有可以去官网下载:ht ...

  6. 图片加载控件glide

    基本用法 //(1)加载网络图片 tvGlide1.setText("(1)加载网络图片"); Glide.with(this).load("http://img1.im ...

  7. MOOC 数据结构 01-复杂度3 二分查找

    01-复杂度3 二分查找(20 分) 本题要求实现二分查找算法. 函数接口定义: Position BinarySearch( List L, ElementType X ); 其中List结构定义如 ...

  8. PIG执行MR时报Connection refused错误

    原因是jobhistory没有启动,其启动脚本位于hadoop/sbin目录下 启动命令如下 mr-jobhistory-daemon.sh start historyserver

  9. css判断iphoneX、iphoneXs、iphoneXs Max、iphone XR

    //iphoneX.iphoneXs @media only screen and (device-width: 375px) and (device-height: 812px) and (-web ...

  10. LINQ 学习路程 -- 查询操作 Select, SelectMany

    IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...