Submission #1176457

#TimeUsernameProblemLanguageResultExecution timeMemory
1176457InvMODBoard (CEOI13_board)C++20
50 / 100
2 ms1220 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




void solve()
{
    string s,t; cin >> s >> t;

    auto conv = [&](const string &x) -> vector<int>{
        // child position vs dad

        vector<int> v;
        for(int i = 0; i < sz(x); i++){
            if(x[i] == '1' || x[i] == '2')
                v.push_back(x[i] - '0' - 1);
            if(x[i] == 'L' || x[i] == 'R')
                v.back() += (x[i] == 'L' ? -1 : 1);
            if(x[i] == 'U'){
                int child = v.back(); v.pop_back();
                v.back() += (child / 2);
                child -= (child / 2);
                if(child < 0) v.back()--;
            }
        }

        for(int i = sz(v) - 1; i >= 1; i--){
            int cur = v[i] / 2;
            v[i] -= cur * 2;
            if(v[i] < 0) cur--, v[i] += 2;
            v[i - 1] += cur;
        }
        return v;
    };

    vector<int> A = conv(s), B = conv(t);

    int dist = 0, ans = sz(A) + sz(B);
    for(int i = 0; i < min(sz(A), sz(B)); i++){
        dist += A[i] - B[i];
        ans = min(ans, abs(dist) + sz(A) + sz(B) - 2 * (i + 1));
        dist = dist * 2;
        if(abs(dist) > 2 * (sz(A) + sz(B))) break;
    }

    cout << ans << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message (stderr)

board.cpp: In function 'int main()':
board.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
board.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...