Submission #625005

# Submission time Handle Problem Language Result Execution time Memory
625005 2022-08-09T08:54:50 Z andrei_boaca Board (CEOI13_board) C++14
50 / 100
200 ms 1804 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll a,b;
string s;
vector<ll> val,va,vb;
void multiply()
{
    ll lg=val.size();
    int last=lg%2;
    if(last==1)
        val.push_back(1);
    else
        val[lg-1]++;
}
void divide()
{
    ll lg=val.size();
    int last=lg%2;
    if(val[lg-1]==1)
        val.pop_back();
    else
        val[lg-1]--;
}
void add()
{
    ll lg=val.size();
    int last=lg%2;
    if(last==0)
    {
        if(val[lg-1]>1)
        {
            val[lg-1]--;
            val.push_back(1);
        }
        else
        {
            val.pop_back();
            lg--;
            val[lg-1]++;
        }
        return;
    }
    else
    {
        ll nr1=val[lg-1];
        ll nr0=val[lg-2];
        val.pop_back();
        val.pop_back();
        lg-=2;
        if(nr0==1)
        {
            val[lg-1]++;
            val.push_back(nr1);
        }
        else
        {
            nr0--;
            val.push_back(nr0);
            val.push_back(1);
            val.push_back(nr1);
        }
    }
}
void subtract()
{
    int lg=val.size();
    int last=lg%2;
    if(last==1)
    {
        if(val[lg-1]==1)
        {
            lg--;
            val.pop_back();
            val[lg-1]++;
        }
        else
        {
            val[lg-1]--;
            val.push_back(1);
        }
        return;
    }
    else
    {
        ll nr0=val[lg-1];
        ll nr1=val[lg-2];
        val.pop_back();
        val.pop_back();
        lg-=2;
        if(nr1==1)
        {
            val[lg-1]++;
            val.push_back(nr0);
        }
        else
        {
            nr1--;
            val.push_back(nr1);
            val.push_back(1);
            val.push_back(nr0);
        }
    }
}
ll getniv(ll x)
{
    for(ll d=0;d<=62;d++)
    {
        ll st=(1LL<<d);
        ll dr=st*2LL-1;
        if(x>=st&&x<=dr)
            return d;
    }
    return -1;
}
vector<int> dif;
void getdif()
{
    dif.clear();
    dif.shrink_to_fit();
    int t=0;
    for(int i=va.size()-1;i>=0;i--)
    {
        int x=va[i]-t-vb[i];
        t=0;
        if(x<0)
        {
            x+=2;
            t=1;
        }
        dif.push_back(x);
    }
    while(dif.back()==0)
        dif.pop_back();
    if(dif.empty())
        dif.push_back(0);
    reverse(dif.begin(),dif.end());
}
int main()
{
    cin>>s;
    val.push_back(1);
    for(char c:s)
    {
        if(c=='1')
            multiply();
        if(c=='2')
        {
            multiply();
            add();
        }
        if(c=='U')
            divide();
        if(c=='L')
            subtract();
        if(c=='R')
            add();
    }
    for(int i=0;i<val.size();i++)
    {
        if(i%2==0)
        {
            for(int j=1;j<=val[i];j++)
                va.push_back(1);
        }
        else
        {
            for(int j=1;j<=val[i];j++)
                va.push_back(0);
        }
    }
    val.clear();
    cin>>s;
    val.push_back(1);
    for(char c:s)
    {
        if(c=='1')
            multiply();
        if(c=='2')
        {
            multiply();
            add();
        }
        if(c=='U')
            divide();
        if(c=='L')
            subtract();
        if(c=='R')
            add();
    }
    for(int i=0;i<val.size();i++)
    {
        if(i%2==0)
        {
            for(int j=1;j<=val[i];j++)
                vb.push_back(1);
        }
        else
        {
            for(int j=1;j<=val[i];j++)
                vb.push_back(0);
        }
    }
    ll rez=0;
    ll ans=1e18;
    if(va.size()<vb.size())
        swap(va,vb);
    while(va.size()>vb.size())
    {
        rez++;
        va.pop_back();
    }
    bool need=0;
    for(int i=0;i<va.size();i++)
        if(va[i]!=vb[i])
        {
            if(va[i]<vb[i])
                need=1;
            break;
        }
    if(need)
        swap(va,vb);
    /*for(int i:va)
        cout<<i;
    cout<<'\n';
    for(int i:vb)
        cout<<i;
    cout<<'\n';*/
    getdif();
    while(dif.size())
    {
        if(dif.size()<=50)
        {
            getdif();
            ll x=0;
            for(int i:dif)
                x=x*2LL+i;
            ans=min(ans,x+rez);
            if(x==0)
                break;
        }
        rez+=2;
        dif.pop_back();
        va.pop_back();
        vb.pop_back();
    }
    cout<<ans;
    return 0;
}

Compilation message

board.cpp: In function 'void divide()':
board.cpp:20:9: warning: unused variable 'last' [-Wunused-variable]
   20 |     int last=lg%2;
      |         ^~~~
board.cpp: In function 'int main()':
board.cpp:160:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |     for(int i=0;i<val.size();i++)
      |                 ~^~~~~~~~~~~
board.cpp:192:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  192 |     for(int i=0;i<val.size();i++)
      |                 ~^~~~~~~~~~~
board.cpp:215:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  215 |     for(int i=0;i<va.size();i++)
      |                 ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Execution timed out 1090 ms 212 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 5 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 7 ms 404 KB Output is correct
3 Execution timed out 1081 ms 468 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 7 ms 468 KB Output is correct
3 Execution timed out 1082 ms 472 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1548 KB Output is correct
2 Correct 6 ms 1548 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1552 KB Output is correct
2 Correct 10 ms 1804 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Execution timed out 1084 ms 1780 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1548 KB Output is correct
2 Correct 7 ms 1596 KB Output is correct
3 Execution timed out 1071 ms 1548 KB Time limit exceeded
4 Halted 0 ms 0 KB -