제출 #1337157

#제출 시각아이디문제언어결과실행 시간메모리
1337157SmuggingSpunLamps (JOI19_lamps)C++20
100 / 100
213 ms2784 KiB
#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
template<class T>void maximize(T& a, T b){
  if(a < b){
    a = b;
  }
}
template<class T>void minimize(T& a, T b){
  if(a > b){
    a = b;
  }
}
int n;
string a, b;
namespace sub1{
  void solve(){
    int mask = 0;
    for(int i = 0; i < n; i++){
      if(a[i] == 1){
        mask |= 1 << i;
      }
    }
    queue<int>q;
    vector<int>f(1 << n, -1);
    q.push(mask);
    f[mask] = 0;
    while(!q.empty()){
      mask = q.front();
      q.pop();
      for(int l = 0; l < n; l++){
        int zmask = mask;
        for(int r = l; r < n; r++){
          if(f[zmask |= 1 << r] == -1){
            f[zmask] = f[mask] + 1;
            q.push(zmask);
          }
        }
        zmask = mask;
        for(int r = l; r < n; r++){
          if(mask >> r & 1){
            zmask ^= 1 << r;
          }
          if(f[zmask] == -1){
            f[zmask] = f[mask] + 1;
            q.push(zmask);
          }
        }
        zmask = mask;
        for(int r = l; r < n; r++){
          if(f[zmask ^= 1 << r] == -1){
            f[zmask] = f[mask] + 1;
            q.push(zmask);
          }
        }
      }
    }
    mask = 0;
    for(int i = 0; i < n; i++){
      if(b[i] == 1){
        mask |= 1 << i;
      }
    }
    cout << f[mask];
  }
}
namespace sub3{
  void solve(){
    b += char(0);
    int ans = 0;
    for(int i = 0; i < n; i++){
      if(b[i] == 1 && b[i + 1] == 0){
        ans++;
      }
    }
    cout << ans;
  }
}
namespace sub24{
  const int INF = 1e9;
  vector<vector<int>>perm(1, vector<int>()), match;
  vector<bool>to[2];
  int lcs(vector<int>a, vector<int>b){
    if(a.empty() || b.empty()){
      return 0;
    }
    vector<int>f(b.size(), 0), nf;
    for(int i = 0; i < a.size(); i++){
      nf = f;
      for(int j = 0; j < b.size(); j++){
        maximize(nf[j], j == 0 ? 0 : nf[j - 1]);
        if(a[i] == b[j]){
          maximize(nf[j], j == 0 ? 1 : f[j - 1] + 1);
        }
      }
      swap(f, nf);
    }
    return f.back();
  }
  void solve(){
    for(int mask = 1; mask < 8; mask++){
      vector<int>p;
      for(int i = 0; i < 3; i++){
        if(mask >> i & 1){
          p.push_back(i);
        }
      }
      do{
        perm.push_back(p);
      } while(next_permutation(p.begin(), p.end()));
    }
    for(int i = 0; i < perm.size(); i++){
      for(int j = 0; j < 2; j++){
        bool x = j;
        for(int& k : perm[i]){
          if(k == 0){
            x = false;
          }
          else if(k == 1){
            x = true;
          }
          else{
            x = !x;
          }
        }
        to[j].push_back(x);
      }
    }
    match.assign(perm.size(), vector<int>(perm.size()));
    for(int i = 0; i < perm.size(); i++){
      for(int j = 0; j < perm.size(); j++){
        match[i][j] = lcs(perm[i], perm[j]);
      }
    }
    vector<int>f(perm.size(), INF), nf(perm.size());
    for(int i = f[0] = 0; i < n; i++){
      fill(nf.begin(), nf.end(), INF);
      for(int j = 0; j < perm.size(); j++){
        if(to[a[i]][j] == b[i]){
          for(int k = 0; k < perm.size(); k++){
            minimize(nf[j], f[k] + int(perm[j].size()) - match[k][j]);
          }
        }
      }
      swap(f, nf);
    }
    cout << *min_element(f.begin(), f.end());
  }
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
  cin >> n >> a >> b;
  for(int i = 0; i < n; i++){
    a[i] -= '0';
    b[i] -= '0';
  }
  if(n <= 18){
    sub1::solve();
  }
  else if(find(a.begin(), a.end(), 1) == a.end()){
    sub3::solve();
  }
  else{
    sub24::solve();
  }
}

컴파일 시 표준 에러 (stderr) 메시지

lamp.cpp: In function 'int main()':
lamp.cpp:153:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...