NWERC 2017 Problem J. Juggling Troupe (规律+STL)

 Juggling Troupe

时间限制: 3 Sec  内存限制: 128 MB
提交: 39  解决: 8
[提交][状态][讨论版][命题人:admin]

题目描述

At the national centre for computing and advanced circus skills, technical demonstrations by students are strongly encouraged.
A troupe of n novice performers are at this very moment arrayed in a row attempting to put on a juggling show. Unfortunately, none of them are very confident in their craft, and they are struggling. Thus, as soon as an opportunity presents itself, they will try to reduce their part in the performance to make the task easier.
Whenever a juggler has more than one ball in their possession, they will throw one ball to each of their neighbours. In the case that a juggler does not have a neighbour in some direction, they will simply throw the ball offstage instead. Everybody throws their juggling balls simultaneously. The show ends when no juggler has more than one ball.
As a member of the audience, you are not impressed by this performance. However, you do wonder how many balls each of the jugglers will have left at the end of the show.

输入

The input consists of:
• One line with a string s of length n (1 ≤ n ≤ 106 ) over the characters 0, 1 and 2. The ith character in s represents the number of juggling balls initially held by the ith person.

输出

Output a string s of length n over the characters 0 and 1, the ith giving the number of juggling balls the ith person has at the end of the show.

样例输入

12100212

样例输出

10111111

提示

来源


【题意】

    根据图片 我们可以 看到 实例的 模拟运行操作;

    气球数大于 2  向左右 分,  分到边界后 就消失, 当没有大于2 存在时 结束,并输出

【思路】

    我们可以试着写一写, 发现 对于一个单独的2  一定会 分到左右两边0 的位置 L,R 变成1, 

    并且 L+R -i 的位置 变成 0,

    于是我们可以把 0 的坐标 用STL  set  存储起来, 自动排序 并调用 lower_bound 函数 在logn 级别内

    查找位置2 的左右两边0的位置,   在0-n 内 更新0的坐标,

    最后肯定都是1  然后 把set 里的 对应位置赋值为 0  就 ok了

【注意】

    @彬子哥 提醒道 对于 set 里 iterator 指针 要放到 函数里面, 随用随调用, 不要放在 全局,不然会超时,

    然 我实现的时候 竟然神奇的 没有超时, 并且 在外面要比里面快一些.。。 尴尬

    

 上面定义在外面, 下面定义在里面 

【code】 

/*
* Date:4/15/2018
* Tile: 2017 NWERC J:Juggling Troupe
* Categery: 思维,规律, STL
* Attention: 注意超时问题
*
*/
#include <iostream>
#include <bits/stdc++.h>
typedef long long ll;

const int MAXN=1e6+7;

using namespace std;

typedef set<int>::iterator It;
set<int>T;

char str[MAXN];
int main()
{
    scanf("%s",str+1);
    int len=strlen(str+1);
    T.insert(0);
    T.insert(len+1);
    for(int i=1;i<=len;i++)
        if(str[i]=='0')
            T.insert(i);

    for(int i=1;i<=len;i++)
    {
        if(str[i]=='2')
        {
            set<int>::iterator L,R;
            R=T.lower_bound(i);
            L=R;
            L--;
            int x= *R+*L - i;
            if(*L>0)
                T.erase(L);
            if(*R<=len)
                T.erase(R);
            T.insert(x);
        }
    }
    for(int i=1;i<=len;i++) str[i]='1';
    for(It it=T.begin();it!=T.end();it++)
        str[*it]='0';
    for(int i=1;i<=len;i++)
        putchar(str[i]);
    cout<<endl;
    return 0;
}

123  

posted @ 2018-04-15 23:29  Sizaif  阅读(381)  评论(0编辑  收藏  举报