2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred) E. Packmen

题意:p得走到*,同时出发,可转弯,只需要有p走过就行,问最短时间

思路:二分时间,然后判断该p在该时间能走完多少个*,再看所有p走完是否*都被走过

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+100;
 4 
 5 vector<int  > p,star;
 6 string ch;
 7 int n;
 8 
 9 bool hh(int q,int ll,int  rr ,int time){
10     int xx=min(abs(q-ll)+abs(ll-rr),abs(q-rr)+abs(ll-rr));
11     if(xx>time) return false;
12     return true;
13 }
14 
15 bool check(int time){
16     int ll=0;
17     for(int i=0;i<p.size();i++){
18         int rr=ll;
19         while(rr<star.size()&&hh(p[i],star[ll],star[rr],time))
20             rr++;
21         ll=rr;
22     }
23     if(ll==star.size()) return true;
24     return false;
25 }
26 int main(){
27     int n;
28     scanf("%d",&n);
29     cin>>ch;
30     for(int i=0;i<ch.size();i++){
31         if(ch[i]=='P') p.push_back(i);
32         else if(ch[i]=='*') star.push_back(i);
33     }
34     int l=0,r=1e9,mid,ans;
35     while(l<=r){
36         mid=(l+r)>>1;
37         if(check(mid)){
38             ans=mid;
39             r=mid-1;
40         }
41         else l=mid+1;
42     }
43     cout<<ans<<endl;
44 }

 

posted on 2017-09-20 19:22  hhhhx  阅读(146)  评论(0编辑  收藏  举报

导航