http://codeforces.com/problemset/problem/847/E
1 second
256 megabytes
standard input
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.
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.
Print minimum possible time after which Packmen can eat all asterisks.
7
*..P*P*
3
10
.**PP.*P.*
2
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+;
const int inf=0x3f3f3f3f;
ll n;
string a;
bool judge(ll t)
{
int now=-,pos=-;//pos到不了,p能够到的最右的地方
for(int i=;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))/);
}
}
else
{
now=i+t;
}
}
}
return now>=pos;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin>>n;
cin>>a;
ll l=,r=inf;
while(r-l>)
{
ll mid=(r+l)>>;
if(judge(mid))
{
r=mid;
}
else
{
l=mid+;
}
// cout<<l<<' '<<r<<endl;
}
cout<<(l+r)/<<endl;
return ;
}
最新文章
- 64位win7下安装SQL Server 2008(图文解说版)
- maven工程直接部署在tomcat上
- 一款jQuery特效编写的大度宽屏焦点图切换特效
- sql 自定义函数--固定格式字符转时间类型
- linux管理网络连接指令
- Topo软件
- Linux ALSA声卡驱动之二:声卡的创建
- JS中有关正则表达式的一些常见应用
- 简单的C语言编译器--语法分析器
- Node.js系列文章:编写自己的命令行界面程序(CLI)
- Java 中的纤程库 – Quasar
- Python:python抓取豆瓣电影top250
- 海纳百川而来的一篇相当全面的Java NIO教程
- Android测试(一)——Apk文件结构以及Android组件介绍
- gitlab入门
- SWD Connect/Transfer Source Code
- 自定义 tableviewheader 高度显示不正常
- tensorflow的日常Demo
- SaltStack系列(四)之实例编写
- JVM启动报错: Could not reserve enough space for object heap error