結果

問題 No.2018 X-Y-X
ユーザー chineristACchineristAC
提出日時 2022-07-22 22:32:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,272 bytes
コンパイル時間 3,431 ms
コンパイル使用メモリ 186,748 KB
実行使用メモリ 6,700 KB
最終ジャッジ日時 2023-09-17 11:01:38
合計ジャッジ時間 5,164 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 6 ms
5,212 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 12 ms
5,640 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 4 ms
4,380 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #


#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <bitset>
#include <atcoder/all>

using namespace std;
using namespace atcoder;



#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;




template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<endl;
}

template<class T>
ostream& operator<<(ostream& os, const vec<T>& A){
  os << "{";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ' ';
    }
  }
  os<<"}";
  return os;
}

template<class T,class U>
ostream& operator<<(ostream& os, const pair<T,U>& A){
  os << "(" << A.first <<", " << A.second << ")";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const deque<T>& A){
  os << "deque({";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ' ';
    }
  }
  os<<"})";
  return os;
}

int pow(int a, int b, int mod){
  int res = 1;
  while (b){
    if (b&1){
      res = res * a % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

using mint = modint998244353;

const mint i10 = mint(10).inv();

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n;
  cin>>n;
  vec<int> A(n);
  rep(i,n){
    char s;
    cin>>s;
    A[i] = abs(s-'A'-(i/2)) % 2;
  }
  vec<int> B(n);
  rep(i,n){
    char s;
    cin>>s;
    B[i] = abs(s-'A'-(i/2)) % 2;
  }

  if (A[0]!=B[0]){
    cout<< -1 << endl;
    return 0;
  }

  vec<int> RA;
  int c = A[0], k = 1;
  for (int i=1;i<n;i++){
    if (c==A[i]){
      k++;
    }
    else{
      RA.pb(k);
      c = A[i], k = 1;
    }
  }
  RA.pb(k);

  vec<int> RB;
  c = B[0], k = 1;
  for (int i=1;i<n;i++){
    if (c==B[i]){
      k++;
    }
    else{
      RB.pb(k);
      c = B[i], k = 1;
    }
  }
  RB.pb(k);

  if (RA.size()!=RB.size()){
    cout << -1 << endl;
    return 0;
  }

  int res = 0;
  vec<int> tmp_minus;
  vec<int> tmp_plus;
  rep(i,RA.size()){
    if (RA[i] > RB[i]){
      int t = RA[i] - RB[i];
      while (tmp_minus.size()){
        int j = tmp_minus[tmp_minus.size()-1];
        tmp_minus.pop_back();
        res += i - j;
        t -= 1;
      }
      rep(_,t){
        tmp_plus.pb(i);
      }
    }
    else{
      int t = RB[i] - RA[i];
      while (tmp_plus.size()){
        int j = tmp_plus[tmp_plus.size()-1];
        tmp_plus.pop_back();
        res += i - j;
        t -= 1;
      }
      rep(_,t){
        tmp_minus.pb(i);
      }

    }
  }

  cout << res << endl;

  

  
  

  
}
0