QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#382146#8058. Binary vs Ternarydnialh#RE 0ms3596kbC++173.6kb2024-04-08 03:46:532024-04-08 03:46:53

Judging History

你现在查看的是最新测评结果

  • [2024-04-08 03:46:53]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3596kb
  • [2024-04-08 03:46:53]
  • 提交

answer

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

#define FOR(i,a,b) for (int i=(int)(a); i<=(int)(b); i++)
#define F0R(i,a) for (int i=0; i<(int)(a); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define sz(v) (int)v.size()

#define FAST ios::sync_with_stdio(0); cin.tie(0);

typedef long long ll;

int main(){ FAST
  int tt;
  cin>>tt;
  auto to_bcnt_of_1s = [](string &a, int bcnt, vector<pair<int,int>> &ans){
    assert(bcnt>=2);
    int t0cnt=0;
    // step 1: remove trailing 0 of a
    int last1 = 0;
    FOR(i,0,sz(a)-1) if(a[i]=='1') last1 = i;
    FOR(i,last1+1,sz(a)-1) ans.pb(mp(i,i+1));
    t0cnt = sz(a)-last1-1; // they all become 1
    // cout<<"now ans have "<<sz(ans)<<"\n";
    // step 2: delete 0's in a
    vector<pair<int,int>> targets; // (start, cnt0)
    for(int i=0,j;i<sz(a);i=j){
      assert(a[i]=='1');
      j=i+1;
      while(j<sz(a)&&a[j]=='0') j++;
      targets.pb(mp(i,j-i-1));
    }
    targets.pop_back();
    reverse(all(targets));
    for(auto &[s,l]:targets) if(l!=0) ans.pb(mp(s+2, s+2+l));
    // cout<<"now ans have "<<sz(ans)<<"\n";
    // step 3: inc or dec count of 1s
    int acnt=0;
    for(auto &c:a) if(c=='1') acnt++;
    acnt+=t0cnt;

    if(acnt > bcnt){
      for(int i=acnt;i>=bcnt+1;i--) ans.pb(mp(1,2)),ans.pb(mp(2,4));
    }
    if(bcnt > acnt){
      FOR(i,acnt,bcnt-1) ans.pb(mp(1,2)),ans.pb(mp(1,2)),ans.pb(mp(2,3));
    }
    // cout<<"now ans have "<<sz(ans)<<"\n";
  };
  auto add_0s_between_11 = [](int s, int cnt, vector<pair<int,int>> &ans){
    if(cnt==0) return;
    ans.pb(mp(s,s+1));ans.pb(mp(s,s+2));// 11 -> 100 -> 1001
    if(cnt==1){
      ans.pb(mp(s+2,s+3));
      return;
    }
    F0R(_,cnt-2) ans.pb(mp(s,s+1)),ans.pb(mp(s,s+1));
  };
  while(tt--){
    string a,b;
    cin>>a>>b;
    if(a[sz(a)-1]!='0'&&a[sz(a)-1]!='1') a = a.substr(0,sz(a)-1);
    if(b[sz(b)-1]!='0'&&b[sz(b)-1]!='1') b = b.substr(0,sz(b)-1);
    vector<pair<int,int>> ans;
    if(a.size()==1){
      if(b.size()==1 && a[0]==b[0]) cout<<"0\n";
      else cout<<"-1\n";
      continue;
    }
    if(b.size()==1){
      if(a.size()==1 && a[0]==b[0]) cout<<"0\n";
      else cout<<"-1\n";
      continue;
    }

    int bcnt=0;
    for(auto &c:b) if(c=='1') bcnt++;
    to_bcnt_of_1s(a,max(2,bcnt),ans);
    
    if(bcnt==1){ // only first is 1 and now we have 11
      ans.pb(mp(1,2));
      assert(sz(b) >= 3);
      if(sz(b)==2) ans.pb(mp(2,3));
      else FOR(_,1,sz(b)-3) ans.pb(mp(1,2)), ans.pb(mp(1,2));
    } else { // now we have bcnt-many 1's
      // step 1: add trailing 0's
      int t0cnt=0;
      for(auto &c:b){
        if(c=='0') t0cnt++;
        else if(c=='1') t0cnt=0;
      }
      // cout<<"b has " << t0cnt << " trailing 0s\n";
      F0R(_,t0cnt) ans.pb(mp(bcnt-1,bcnt)), ans.pb(mp(bcnt-1,bcnt));
      // cout<<"now ans have "<<sz(ans)<<"\n";
      // step 2: convert each 11 to 10..01
      vector<pair<int,int>> targets; // (start, cnt0)
      for(int i=0,j;i<sz(b);i=j){
        assert(b[i]=='1');
        j=i+1;
        while(j<sz(b)&&b[j]=='0') j++;
        targets.pb(mp(i,j-i-1));
      }
      assert(sz(targets) == bcnt && targets.back().se == t0cnt);
      targets.pop_back();
      for(int i=bcnt-1;i>=1;i--){
        // now [i,i+1] is 11, want 1 + 0 * targets[i-1].se + 1
        add_0s_between_11(i,targets[i-1].se,ans);
      }
    }
    cout<<ans.size()<<"\n";
    for(auto &[l,r]:ans) cout<<l<<" "<<r<<"\n";
  }
}
/*
3
1
111
110110
1101010
1111
111111

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3596kb

input:

3
1
111
110110
1101010
1111
111111

output:

-1
12
5 6
3 4
1 2
2 4
3 4
3 4
3 4
3 5
5 6
2 3
2 4
4 5
6
1 2
1 2
2 3
1 2
1 2
2 3

result:

ok Haitang Suki (3 test cases)

Test #2:

score: -100
Runtime Error

input:

1000
11100
111
1
11110
10001
10
1011
1111
10
1110
1100
11
11010
11
110
11
1
10001
10110
10
10
11111
10000
1001
10
1
11
10111
11
10
1
100
11
10100
1
10
101
11
1100
110
11
1110
1
1001
1
11111
10
10010
10
11001
110
1010
10011
1110
10100
1001
1001
101
100
1
1001
11
101
11
101
1001
1
1
1011
1
10
10
1011
...

output:


result: