• http://codeforces.com/problemset/problem/847/E


    E. Packmen
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A game field is a strip of 1 × n square cells. In some cells there are Packmen, in some cells — asterisks, other cells are empty.

    Packman can move to neighboring cell in 1 time unit. If there is an asterisk in the target cell then Packman eats it. Packman doesn't spend any time to eat an asterisk.

    In the initial moment of time all Packmen begin to move. Each Packman can change direction of its move unlimited number of times, but it is not allowed to go beyond the boundaries of the game field. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.

    Your task is to determine minimum possible time after which Packmen can eat all the asterisks.

    Input

    The first line contains a single integer n (2 ≤ n ≤ 105) — the length of the game field.

    The second line contains the description of the game field consisting of n symbols. If there is symbol '.' in position i — the cell i is empty. If there is symbol '*' in position i — in the cell i contains an asterisk. If there is symbol 'P' in position i — Packman is in the cell i.

    It is guaranteed that on the game field there is at least one Packman and at least one asterisk.

    Output

    Print minimum possible time after which Packmen can eat all asterisks.

    Examples
    input
    7
    *..P*P*
    output
    3
    input
    10
    .**PP.*P.*
    output
    2
    Note

    In the first example Packman in position 4 will move to the left and will eat asterisk in position 1. He will spend 3 time units on it. During the same 3 time units Packman in position 6 will eat both of neighboring with it asterisks. For example, it can move to the left and eat asterisk in position 5 (in 1 time unit) and then move from the position 5 to the right and eat asterisk in the position 7 (in 2 time units). So in 3 time units Packmen will eat all asterisks on the game field.

    In the second example Packman in the position 4 will move to the left and after 2 time units will eat asterisks in positions 3 and 2. Packmen in positions 5 and 8 will move to the right and in 2 time units will eat asterisks in positions 7 and 10, respectively. So 2 time units is enough for Packmen to eat all asterisks on the game field.

     题解:二分时间,判断时用now代表P能够在时间t内到达的最有方的点,pos代表最右方还没到的带点,然后通过这两点位置关系看是否满足条件。

    #include<bits/stdc++.h>
    #define pb push
    #define ll long long
    #define PI 3.14159265
    using namespace std;
    const int maxn=1e5+5;
    const int inf=0x3f3f3f3f;
    ll n;
    string a;
    bool judge(ll t)
    {
        int now=-1,pos=-2;//pos到不了,p能够到的最右的地方 
        for(int i=0;i<n;i++)
        {
            if(a[i]=='*')
            {
                if(now<i&&now>=pos)pos=i;
            }
            else if(a[i]=='P')
            {
                if(now<pos)
                {
                    if(i-pos>t)return false;
                    else
                    {
                        now=max(t-(i-pos)+pos,i+(t-(i-pos))/2);
                    }
                }
                else
                {
                    now=i+t;
                 } 
            }
        }
        return now>=pos;
    }
    int main()
    {    
        std::ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin>>n;
        cin>>a;
        ll l=0,r=inf;
        while(r-l>0)
        {
            ll mid=(r+l)>>1;
            if(judge(mid))
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        //    cout<<l<<' '<<r<<endl;
        }
        cout<<(l+r)/2<<endl; 
        return 0;
    }
  • 相关阅读:
    互动教程,让你5分钟掌握 Flexbox 布局模式
    Fixed Responsive Nav – 响应式的单页网站导航插件
    创意无限!一组网页边栏过渡动画【附源码下载】
    12款界面精美的 HTML5 & CSS3 网站模板
    Twproject Gantt – 开源的 JavaScript 甘特图组件
    真是好东西!一组动感的页面加载动画效果
    Method Draw – 很好用的 SVG 在线编辑器
    CSS Vocabulary – CSS 词汇表,你都掌握了吗?
    前端精选文摘:BFC 神奇背后的原理
    Vis.js – 基于浏览器的动态 JavaScript 可视化库
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/7577640.html
Copyright © 2020-2023  润新知