Debug it (雙頭DP)
阿新 • • 發佈:2022-03-11
M. Debug it! time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Miamiao writes a program and submits it to the judger. The judger tells her the result of each test case by a string containing P or F. P means to pass the test and F means to fail the test. Now Huihui wants to help her debug the program. He can remove the first or the last letter inView problem1 minute or remove any other letter in 2 minutes. After removing a letter from the middle the two remaining parts are joined. Now, Huihui wants to know, how fast can he finish debugging the problem, which means he can obtain a string without any letter F? Input The first line of input contains an integer n(1≤n≤106), which means the length of the string. The second line of input contains a string s(|s|=n,si∈{F,P}), which means Miamiao's result. Output Output a number, which means the minimum time needed to debug the problem by Huihui. Example inputCopy 4 FPPF outputCopy 2
#include <bits/stdc++.h> usingView Codenamespace std; #define ri register int #define M 1000005 template <class G> void read(G &x) { x=0;char ch=getchar();int f=0; while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return ; } long long a[M],b[M]; int p[M]; int n,m; int main(){ read(n); for(ri i=1;i<=n;i++) { char c;cin>>c; if(c=='P') p[i]=1; else p[i]=2; } for(long long i=1;i<=n;i++) { if(p[i]==1) a[i]=a[i-1]; else { a[i]=min(a[i-1]+2,i); } } for(long long i=n;i>=1;i--) { if(p[i]==1) b[i]=b[i+1]; else { b[i]=min(b[i+1]+2,n-i+1); } } long long ans=1e18; for(ri i=0;i<=n;i++) { ans=min(ans,a[i]+b[i+1]); } cout<<ans; return 0; }