# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
865028 | guagua0407 | Lamps (JOI19_lamps) | C++17 | 179 ms | 133028 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s+ ".out").c_str(), "w", stdout);
}
const int mxn=1e6+5;
int a[mxn];
int dp[mxn];
vector<int> st[mxn],en[mxn];
int main() {_
int n;
cin>>n;
string A,B;
cin>>A>>B;
map<pair<char,char>,int> mp;
mp[{'0','0'}]=1;
mp[{'1','1'}]=2;
mp[{'1','0'}]=3;
mp[{'0','1'}]=4;
for(int i=0;i<n;i++){
a[i+1]=mp[{A[i],B[i]}];
}
vector<pair<int,int>> vec;
{
int cnt=0;
for(int i=1;i<=n;i++){
if(a[i]!=1 and a[i]!=3){
if(cnt>0){
vec.push_back({i-1-cnt+1,i-1});
}
cnt=0;
}
else{
cnt++;
}
}
if(cnt>0){
vec.push_back({n-cnt+1,n});
}
}
{
int cnt=0;
for(int i=1;i<=n;i++){
if(a[i]!=2 and a[i]!=4){
if(cnt>0){
vec.push_back({i-1-cnt+1,i-1});
}
cnt=0;
}
else{
cnt++;
}
}
if(cnt>0){
vec.push_back({n-cnt+1,n});
}
}
{
int cnt=0;
for(int i=1;i<=n;i++){
if(a[i]!=3 and a[i]!=4){
if(cnt>0){
vec.push_back({i-1-cnt+1,i-1});
}
cnt=0;
}
else{
cnt++;
}
}
if(cnt>0){
vec.push_back({n-cnt+1,n});
}
}
for(int i=0;i<vec.size();i++){
st[vec[i].f].push_back(i);
en[vec[i].s+1].push_back(i);
}
multiset<int> S;
for(int i=1;i<=n;i++){
for(auto v:st[i]){
S.insert(i);
}
for(auto v:en[i]){
S.erase(S.find(vec[v].f));
}
assert(!S.empty());
dp[i]=dp[i-1]+(a[i]==3 or a[i]==4);
for(auto v:S){
dp[i]=min(dp[i],dp[v-1]+1);
}
}
/*for(auto v:vec){
cout<<v.f<<' '<<v.s<<'\n';
}
for(int i=1;i<=n;i++){
cout<<dp[i]<<' ';
}
cout<<'\n';*/
cout<<dp[n]<<'\n';
return 0;
}
//maybe its multiset not set
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |