zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    致歉
    [公告]博客园正在对网站程序进行性能优化
    [公告]调整默认发布选项
    网站情况继续汇报
    定制“Server Too Busy”错误信息
    可恶的垃圾广告
    博客园分站服务器故障
    很值得期待—SharePoint "V3.0"新特性
    安装Vistual Studio 2005的小问题
    安装智能陈桥五笔时请小心
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/7577640.html
Copyright © 2011-2022 走看看