# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
865035 | guagua0407 | Lamps (JOI19_lamps) | C++17 | 281 ms | 102420 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;
int a=0,b=0;
for(int i=0;i<n;i++){
a+=(1<<(n-i-1))*(A[i]-'0');
}
for(int i=0;i<n;i++){
b+=(1<<(n-i-1))*(B[i]-'0');
}
vector<int> dp(1<<n,1e9);
vector<int> from(1<<n,-1);
dp[a]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,a});
while(!pq.empty()){
auto v=pq.top();
//cout<<v.s<<'\n';
pq.pop();
if(v.f!=dp[v.s]) continue;
for(int j=0;j<n;j++){
{
int val=v.s;
for(int k=j;k<n;k++){
if(val&(1<<k)) val^=(1<<k);
if(dp[val]>dp[v.s]+1){
dp[val]=dp[v.s]+1;
pq.push({dp[val],val});
}
}
}
{
int val=v.s;
for(int k=j;k<n;k++){
if(!(val&(1<<k))) val^=(1<<k);
if(dp[val]>dp[v.s]+1){
dp[val]=dp[v.s]+1;
pq.push({dp[val],val});
}
}
}
{
int val=v.s;
for(int k=j;k<n;k++){
val^=(1<<k);
if(dp[val]>dp[v.s]+1){
dp[val]=dp[v.s]+1;
pq.push({dp[val],val});
}
}
}
}
}
cout<<dp[b]<<'\n';
/*while(b!=-1){
for(int i=n-1;i>=0;i--){
if(b&(1<<i)) cout<<1;
else cout<<0;
}
cout<<'\n';
b=from[b];
}*/
return 0;
}
//maybe its multiset not set
/*
16
0011001001000110
1101100100010101
*/
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... |