Problem:
F: Swap
問題
長さ $N$ の文字列 $S,\ T$ が与えられます．$S,\ T$ はそれぞれ 'o' , '.' の二種類の文字だけで構成されています．
  あなたは $S$ に対して，以下の操作を行うことができます．
以下の条件を全て満たす整数対 $(l, r)$ を選択する．その後，$S[l]$ と $S[l + 1]，S[r - 1]$ と $S[r]$ をそれぞれスワップする．
$1 \leq l, r \leq N$
$r - l \geq 3$
$S[l] = S[r] =$ '.'
$S[l + 1] = S[l + 2] = \dots  = S[r - 1] =$ 'o'
何回か操作を繰り返したあと(0回でも可)，文字列 S を T に変形することが可能か判定してください．
制約
$1 \leq N \leq 100000$
$|S| = |T| = N \ \ \ \ \ \ \ |S|$ , $|T|$ は文字列の長さ
$S$ , $T$ は 'o', '.' の二種類の文字からのみ構成される。
入力形式
入力は以下の形式で与えられる。
$N$
$S$
$T$
出力
操作を何回か適用後( $0$ 回でも可)， $S$ を $T$ に変形することが可能ならば Yes，どのように操作しても不可能ならば No を出力してください．また、末尾に改行も出力してください．
サンプル
サンプル入力 1
8
.oo.ooo.
o.o.oo.o
サンプル出力 1
Yes
以下の手順で操作を行うことで達成できます．
$S$ = ".oo.ooo." : $(l, r) = (1, 4)$ を選択する．
$S$ = "o..oooo." : $(l, r) = (4, 9)$ を選択する．
$S$ = "o.o.oo.o" : $T$ と一致し，目的を達成．
サンプル入力 2
7
.ooooo.
oo.o.oo
サンプル出力 2
Yes
以下の手順で操作を行うことで達成できます．
$S$ = ".ooooo." : $(l, r) = (1, 7)$ を選択する．
$S$ = "o.ooo.o" : $(l, r) = (2, 6)$ を選択する．
$S$ = "oo.o.oo" : $T$ と一致し，目的を達成．
サンプル入力 3
6
o.o.o.
ooo...
サンプル出力 3
No
操作を適用できる $(l, r)$ が存在しないため，$S$ を変形して $T$ にすることができません．
サンプル入力 4
9
.oo.oooo.
.oo.oooo.
サンプル出力 4
Yes
$1$ 回も変形させずに目的を達成できます
サンプル入力 5
11
.oooo.oooo.
oo.oo.oo.oo
サンプル出力 5
Yes

Submitted solutions:

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    auto f = [](string s) -> vector<int> {
        int cnt = 0;
        vector<int> v;
        for (char c : s) {
            if (c == 'o') cnt += 1;
            else v.push_back(cnt);
        }
        return v;
    };
    auto a = f(s), b = f(t);
    if (a.size() != b.size() or accumulate(a.begin(), a.end(), 0LL) != accumulate(b.begin(), b.end(), 0LL)) {
        cout << "No" << endl;
        return 0;
    }
    n = a.size();
    vector<int> op(n + 2), dif(n + 2);
    for (int i = 0; i + 1 < n; ++i) {
        int pos = a[i] - (i ? op[i] : 0);
        op[i + 1] = b[i] - pos;
        dif[i + 1] = a[i + 1] - a[i];
        if (op[i + 1] < 0) {
            cout << "No" << endl;
            return 0;
        }
    }
    for (int i = 0; i < 3000; ++i) {
        for (int j = 1; j <= n; ++j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            dif[j - 1] += mi;
            dif[j + 1] += mi;
        }
        for (int j = n; j >= 1; --j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            dif[j - 1] += mi;
            dif[j + 1] += mi;
        }
    }
    for (int i = 0; i < n; ++i) {
        if (op[i]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}
accuracy: 0.6029411764705882
time_ms: 1050
memory_kb: 4280
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    auto f = [](string s) -> vector<int> {
        int cnt = 0;
        vector<int> v;
        for (char c : s) {
            if (c == 'o') cnt += 1;
            else v.push_back(cnt);
        }
        return v;
    };
    auto a = f(s), b = f(t);
    if (a.size() != b.size() or
        accumulate(a.begin(), a.end(), 0LL) != accumulate(b.begin(), b.end(), 0LL)) {
        cout << "No" << endl;
        return 0;
    }
    n = a.size();
    vector<int> op(n), dif(n);
    for (int i = 0; i + 1 < n; ++i) {
        int pos = a[i] - (i ? op[i - 1] : 0);
        op[i] = b[i] - pos;
        dif[i] = a[i + 1] - a[i];
        if (op[i] < 0) {
            cout << "No" << endl;
            return 0;
        }
    }
    for (int i = 0; i < 3000; ++i) {
        for (int j = 0; j < n; ++j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            if (j) dif[j - 1] += mi;
            if (j + 1 < n) dif[j + 1] += mi;
        }
        for (int j = n - 1; j >= 0; --j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            if (j) dif[j - 1] += mi;
            if (j + 1 < n) dif[j + 1] += mi;
        }
    }
    for (int i = 0; i < n; ++i) {
        if (op[i]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}
accuracy: 0.6029411764705882
time_ms: 1120
memory_kb: 4240
final_rank: 6

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    auto f = [](string s) -> vector<int> {
        int cnt = 0;
        vector<int> v;
        for (char c : s) {
            if (c == 'o') cnt += 1;
            else v.push_back(cnt);
        }
        return v;
    };
    auto a = f(s), b = f(t);
    if (a.size() != b.size() or
        accumulate(a.begin(), a.end(), 0LL) != accumulate(b.begin(), b.end(), 0LL)) {
        cout << "No" << endl;
        return 0;
    }
    n = a.size();
    vector<int> op(n), dif(n);
    for (int i = 0; i + 1 < n; ++i) {
        int pos = a[i] - (i ? op[i - 1] : 0);
        op[i] = b[i] - pos;
        dif[i] = a[i + 1] - a[i];
        if (op[i] < 0) {
            cout << "No" << endl;
            return 0;
        }
    }
    for (int i = 0; i < 2000; ++i) {
        for (int j = 0; j < n; ++j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            if (j) dif[j - 1] += mi;
            if (j + 1 < n) dif[j + 1] += mi;
        }
        for (int j = n - 1; j >= 0; --j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            if (j) dif[j - 1] += mi;
            if (j + 1 < n) dif[j + 1] += mi;
        }
    }
    for (int i = 0; i < n; ++i) {
        if (op[i]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}
accuracy: 0.6029411764705882
time_ms: 740
memory_kb: 4232
final_rank: 4

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    auto f = [](string s) -> vector<int> {
        int cnt = 0;
        vector<int> v;
        for (char c : s) {
            if (c == 'o') cnt += 1;
            else v.push_back(cnt);
        }
        return v;
    };
    auto a = f(s), b = f(t);
    if (a.size() != b.size() or
        accumulate(a.begin(), a.end(), 0LL) != accumulate(b.begin(), b.end(), 0LL)) {
        cout << "No" << endl;
        return 0;
    }
    n = a.size();
    vector<int> op(n), dif(n);
    for (int i = 0; i + 1 < n; ++i) {
        int pos = a[i] - (i ? op[i - 1] : 0);
        op[i] = b[i] - pos;
        dif[i] = a[i + 1] - a[i];
        if (op[i] < 0) {
            cout << "No" << endl;
            return 0;
        }
    }
    for (int i = 0; i < 1000; ++i) {
        for (int j = 0; j < n; ++j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            if (j) dif[j - 1] += mi;
            if (j + 1 < n) dif[j + 1] += mi;
        }
        for (int j = n - 1; j >= 0; --j) {
            int mi = min(dif[j] / 2, op[j]);
            op[j] -= mi, dif[j] -= mi * 2;
            if (j) dif[j - 1] += mi;
            if (j + 1 < n) dif[j + 1] += mi;
        }
    }
    for (int i = 0; i < n; ++i) {
        if (op[i]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}
accuracy: 0.6029411764705882
time_ms: 370
memory_kb: 4280
final_rank: 1

code_snippet: #include<algorithm>
#include<iostream>
#include<string>
#include<vector>
int main(int, char**)
{
	int dummy = 0;
	std::cin >> dummy;
	std::string buff_s;
	std::string buff_t;
	std::cin >> buff_s >> buff_t;

	int place1_s = 0;
	int place2_s = 0;
	int place1_t = 0;

	auto search = [](const std::string& str, const int start, const int delta)->int
	{
		for (int index = start; str.length() > index; index += delta)
		{
			if ('.' == str[index])
			{
				return index;
			}
		}
		return -1;
	};
	while (buff_s != buff_t)
	{
		place1_s = search(buff_s, static_cast<int>(buff_s.length()) - 1, -1);
		place1_t = search(buff_t, static_cast<int>(buff_t.length()) - 1, -1);
		if (place1_s < place1_t)
		{
			break;
		}

		place1_s = search(buff_s, 0, 1);
		place1_t = search(buff_t, 0, 1);
		if (place1_s > place1_t || (-1 == place1_s || -1 == place1_t))
		{
			break;
		}
		if (place1_s < place1_t)
		{
			int desire = place1_t - place1_s;
			place2_s = search(buff_s, place1_s + 1, 1);
			while (-1 != place2_s && 3 > place2_s - place1_s)
			{
				desire = place1_s + desire * 2 + 1 - place2_s;
				place1_s = place2_s;
				place2_s = search(buff_s, place1_s + 1, 1);
			}
			if (-1 == place2_s)
			{
				break;
			}

			const int delta = (std::min)(desire, (place2_s - place1_s - 1) / 2);
			buff_s[place1_s] = 'o';
			buff_s[place1_s + delta] = '.';
			buff_s[place2_s] = 'o';
			buff_s[place2_s - delta] = '.';
			continue;
		}
		if (place1_s == place1_t)
		{
			buff_s = buff_s.substr(place1_s + 1);
			buff_t = buff_t.substr(place1_t + 1);
		}
	}

	std::cout << (buff_s == buff_t ? "Yes" : "No") << std::endl;
}
accuracy: 0.5147058823529411
time_ms: 170
memory_kb: 3368
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);i++)

int n;
char s[125252];
char t[125252];

int sim[125252];
int cm;
int cnt[125252];
int pek[125252];
int poi[125252];

int main(){
  scanf("%d%s%s",&n,s,t);
  if(count(s,s+n,'.') != count(t,t+n,'.')){
    puts("No");
    return 0;
  }
  // simulate
  {
    cm = 1;
    int po = 0;
    int pk = 0;
    while(true){
      sim[0]-=2;
      pk = -sim[0];
      po += 1;
      sim[1]++;
      int i = 1;
      while(sim[i]==2){
        sim[i] -= 2;
        sim[i-1]++;
        sim[i+1]++;
        i++;
      }
      if(sim[cm]==1){
        cnt[cm] = -sim[0];
        pek[cm] = pk;
        poi[cm] = po;
        if(cnt[cm++]>100000){
          break;
        }
      }
    }
  }
  // solve
  vector<int> si, ti;
  si.push_back(0);
  ti.push_back(0);
  REP(i,n)if(s[i]=='o'){
    si.back()++;
  }else{
    si.push_back(0);
  }
  REP(i,n)if(t[i]=='o'){
    ti.back()++;
  }else{
    ti.push_back(0);
  }
  n = si.size();
  if(si.front() > ti.front() || si.back() > ti.back()){
    puts("No");
    return 0;
  }
  REP(i,n-1){
    if(si[i]==ti[i])continue;
    if(si[i]>ti[i]){
      puts("No");
      return 0;
    }
    // REP(j,n)printf("%d,",si[j]);puts("");
    while(si[i] < ti[i]){
      int j = 0;
      while(true){
        j++;
        int need = pek[j];
        if(i+j>=n-1 || need>100000){
          puts("No");
          return 0;
        }else if(si[i+j] >= need){
          break;
        }
      }
      si[i+j] -= cnt[j];
      si[i+j+1] += poi[j];
      if(j>1)si[i+1]--;
      while(j>0){
        j--;
        si[i+j]++;
      }
      // REP(j,n)printf("%d,",si[j]);puts("");
    }
  }
  puts("Yes");
  return 0;
}
accuracy: 0.07352941176470588
time_ms: 10
memory_kb: 3268
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);i++)

int n;
char s[125252];
char t[125252];

int sim[125252];
int cm;
int cnt[125252];
int pek[125252];
int poi[125252];

int main(){
  scanf("%d%s%s",&n,s,t);
  if(count(s,s+n,'.') != count(t,t+n,'.')){
    puts("No");
    return 0;
  }
  // simulate
  {
    cm = 1;
    int po = 0;
    int pk = 0;
    while(true){
      sim[0]-=2;
      pk = -sim[0];
      po += 1;
      sim[1]++;
      int i = 1;
      while(sim[i]==2){
        sim[i] -= 2;
        sim[i-1]++;
        sim[i+1]++;
        i++;
      }
      if(sim[cm]==1){
        cnt[cm] = -sim[0];
        pek[cm] = pk;
        poi[cm] = po;
        if(cnt[cm++]>100000){
          break;
        }
      }
    }
  }
  // solve
  vector<int> si, ti;
  si.push_back(0);
  ti.push_back(0);
  REP(i,n)if(s[i]=='o'){
    si.back()++;
  }else{
    si.push_back(0);
  }
  REP(i,n)if(t[i]=='o'){
    ti.back()++;
  }else{
    ti.push_back(0);
  }
  n = si.size();
  if(si.front() > ti.front() || si.back() > ti.back()){
    puts("No");
    return 0;
  }
  REP(i,n-1){
    if(si[i]==ti[i])continue;
    if(si[i]>ti[i]){
      puts("No");
      return 0;
    }
    // REP(j,n)printf("%d,",si[j]);puts("");
    while(si[i] < ti[i]){
      int j = 0;
      while(true){
        j++;
        int need = pek[j];
        if(i+j>=n-1 || need>100000){
          puts("No");
          return 0;
        }else if(si[i+j] >= need){
          break;
        }
      }
      si[i+j] -= pek[j];
      si[i+j+1] += poi[j];
      if(j>1)si[i+1]--;
      while(j>0){
        j--;
        si[i+j]++;
      }
      // REP(j,n)printf("%d,",si[j]);puts("");
    }
  }
  puts("Yes");
  return 0;
}
accuracy: 0.07352941176470588
time_ms: 10
memory_kb: 3272
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  string s,t;
  cin>>s>>t;
  Int m=1;
  for(Int i=0;i<n;i++) m+=s[i]=='.';

  auto NO=[](){cout<<"No"<<endl;exit(0);};
  {
    Int tmp=1;
    for(Int i=0;i<n;i++) tmp+=t[i]=='.';
    if(m!=tmp) NO();
  }

  vector<Int> a,b;
  a.emplace_back(0);
  b.emplace_back(0);
  for(Int i=0;i<n;i++){
    if(s[i]=='.') a.emplace_back();
    else a.back()++;
    if(t[i]=='.') b.emplace_back();
    else b.back()++;
  }

  vector<Int> c(m),d(a);
  c[0]=0;
  for(Int i=1;i+1<m;i++){
    c[i]=b[i-1]-a[i-1];
    a[i-1]+=c[i];
    a[i]-=c[i]*2;
    a[i+1]+=c[i];
    if(c[i]<0) NO();
  }
  if(c[m-1]!=0) NO();
  for(Int i=0;i<m;i++) if(a[i]!=b[i]) NO();
  
  if(0){
    for(Int i=0;i<m;i++) cout<<setw(4)<<a[i];
    cout<<endl;
    for(Int i=0;i<m;i++) cout<<setw(4)<<b[i];
    cout<<endl;
  }
  
  if(0){
    for(Int i=0;i<m;i++) cout<<setw(4)<<c[i];
    cout<<endl;
    for(Int i=0;i<m;i++) cout<<setw(4)<<d[i]-b[i];
    cout<<endl;
  }

  using P = pair<Int, Int>;
  priority_queue<P> pq;

  auto calc=
    [&](Int thr){
      for(Int i=0;i<m;i++){
	Int k=min(d[i]/2,c[i]);
	if(k>thr) pq.emplace(k,i);
      }  
  
      while(!pq.empty()){
	Int x,i;
	tie(x,i)=pq.top();pq.pop();
	assert(0<=i-1&&i+1<m);

	{
	  Int k=min(d[i]/2,c[i]);
	  if(k!=x) continue;
	}
	
	//right: i+1
	{
	  while(1){
	    Int k=min(d[i]/2,c[i]);
	    if(!k) break;

	    c[i]-=k;
	    d[i-1]+=k;
	    d[i]-=k*2;
	    d[i+1]+=k;

	    Int nk=min(d[i+1]/2,c[i+1]);
	    if(nk){
	      c[i+1]-=nk;
	      d[i]+=nk;
	      d[i+1]-=nk*2;
	      d[i+2]+=nk;
	      assert(i+2<m);
	    }
	  }
	}
	
	//left: i-1
	{
	  while(1){
	    Int k=min(d[i]/2,c[i]);
	    if(!k) break;
	    c[i]-=k;
	    d[i-1]+=k;
	    d[i]-=k*2;
	    d[i+1]+=k;
	    
	    Int nk=min(d[i-1]/2,c[i-1]);
	    if(nk){
	      c[i-1]-=nk;
	      d[i]+=nk;
	      d[i-1]-=nk*2;
	      d[i-2]+=nk;
	      assert(0<=i-2);
	    }
	  }
	}
	
	for(Int j=i-1;j<=i+1;j++){
	  Int k=min(d[j]/2,c[j]);
	  if(k>thr) pq.emplace(k,j);
	}
      }
    };
  
  //calc(1000);
  //calc(100);
  //calc(10);
  calc(0);
  calc(0);
  calc(0);
  calc(0);
  
  for(Int i=0;i<m;i++) if(d[i]!=b[i]) NO();
  cout<<"Yes"<<endl;
  return 0;
}
accuracy: 0.6029411764705882
time_ms: 10
memory_kb: 5216
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  string s,t;
  cin>>s>>t;
  Int m=1;
  for(Int i=0;i<n;i++) m+=s[i]=='.';

  auto NO=[](){cout<<"No"<<endl;exit(0);};
  {
    Int tmp=1;
    for(Int i=0;i<n;i++) tmp+=t[i]=='.';
    if(m!=tmp) NO();
  }

  vector<Int> a,b;
  a.emplace_back(0);
  b.emplace_back(0);
  for(Int i=0;i<n;i++){
    if(s[i]=='.') a.emplace_back();
    else a.back()++;
    if(t[i]=='.') b.emplace_back();
    else b.back()++;
  }

  vector<Int> c(m),d(a);
  c[0]=0;
  for(Int i=1;i+1<m;i++){
    c[i]=b[i-1]-a[i-1];
    a[i-1]+=c[i];
    a[i]-=c[i]*2;
    a[i+1]+=c[i];
    if(c[i]<0) NO();
  }
  if(c[m-1]!=0) NO();
  for(Int i=0;i<m;i++) if(a[i]!=b[i]) NO();
  
  if(0){
    for(Int i=0;i<m;i++) cout<<setw(4)<<a[i];
    cout<<endl;
    for(Int i=0;i<m;i++) cout<<setw(4)<<b[i];
    cout<<endl;
  }
  
  if(0){
    for(Int i=0;i<m;i++) cout<<setw(4)<<c[i];
    cout<<endl;
    for(Int i=0;i<m;i++) cout<<setw(4)<<d[i]-b[i];
    cout<<endl;
  }

  using P = pair<Int, Int>;
  priority_queue<P> pq;

  auto calc=
    [&](Int thr){
      for(Int i=0;i<m;i++){
	Int k=min(d[i]/2,c[i]);
	if(k>thr) pq.emplace(k,i);
      }  
  
      while(!pq.empty()){
	Int x,i;
	tie(x,i)=pq.top();pq.pop();
	assert(1<=i&&i+1<m);

	{
	  Int k=min(d[i]/2,c[i]);
	  if(k!=x) continue;
	}
	
	//right: i+1
	{
	  while(1){
	    Int k=min(d[i]/2,c[i]);
	    if(!k) break;

	    c[i]-=k;
	    d[i-1]+=k;
	    d[i]-=k*2;
	    d[i+1]+=k;

	    Int nk=min(d[i+1]/2,c[i+1]);
	    if(nk){
	      c[i+1]-=nk;
	      d[i]+=nk;
	      d[i+1]-=nk*2;
	      d[i+2]+=nk;	      
	    }
	  }
	}
	
	//left: i-1
	{
	  while(1){
	    Int k=min(d[i]/2,c[i]);
	    if(!k) break;
	    c[i]-=k;
	    d[i-1]+=k;
	    d[i]-=k*2;
	    d[i+1]+=k;
	    
	    Int nk=min(d[i-1]/2,c[i-1]);
	    if(nk){
	      c[i-1]-=nk;
	      d[i]+=nk;
	      d[i-1]-=nk*2;
	      d[i-2]+=nk;   
	    }
	  }
	}
	
	
	for(Int j=i-1;j<=i+1;j++){
	  Int k=min(d[j]/2,c[j]);
	  if(k>thr) pq.emplace(k,j);
	}
      }
    };
  
  //calc(1000);
  //calc(100);
  //calc(10);
  calc(0);
  calc(0);
  calc(0);
  calc(0);
  
  for(Int i=0;i<m;i++) if(d[i]!=b[i]) NO();
  cout<<"Yes"<<endl;
  return 0;
}
accuracy: 0.6029411764705882
time_ms: 10
memory_kb: 5364
final_rank: 3