Submission #1332423

#TimeUsernameProblemLanguageResultExecution timeMemory
1332423adscodingLamps (JOI19_lamps)C++20
0 / 100
1095 ms10460 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __prine_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        cerr << "  ,  ";
        ++s;
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = {0, (__prine_one(s, args), 0)...};
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, const X &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, const X &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

const int maxn = 1e6 + 3;
int n; vector<int> iState, fState;

// --------------------------------------------------------------------------------------------

namespace sub1
{
    map<vector<int>, int> ID;
    vector<vector<int>> states;
    vector<int> D;
    int tid = 0;
    queue<int> q;

    void Add(vector<int> &s, int dprev)
    {
        if (ID.count(s)) return;
        ID[s] = ++tid;
        D.push_back(dprev + 1);
        states.push_back(s);
        q.push(tid);
    }

    void solve()
    {
        ID[iState] = 0;
        D.push_back(0);
        states.push_back(iState);
        q.push(0);

        if (iState == fState) return cout << 0, void();

        while (q.size())
        {
            int u = q.front(); q.pop();

            // Type 1
            {
                FOR(i, 0, n - 1)
                {
                    vector<int> nState = states[u];
                    FOR(j, i, n - 1)
                    {
                        nState[j] = 0;
                        Add(nState, D[u]);
                        if (nState == fState) return cout << D[tid], void();
                    }
                }
            }

            // Type 2
            {
                FOR(i, 0, n - 1)
                {
                    vector<int> nState = states[u];
                    FOR(j, i, n - 1)
                    {
                        nState[j] = 1;
                        Add(nState, D[u]);
                        if (nState == fState) return cout << D[tid], void();
                    }
                }
            } 

            // Type 3
            {
                FOR(i, 0, n - 1)
                {
                    vector<int> nState = states[u];
                    FOR(j, i, n - 1)
                    {
                        nState[j] ^= 1;
                        Add(nState, D[u]);
                        if (nState == fState) return cout << D[tid], void();
                    }
                }
            }
        }
    }
}

namespace AC
{
    void solve()
    {   
        int res = 0;
        FOR(i, 0, n - 1)
        {
            if (iState[i] == fState[i]) continue;
            int j0 = i - 1, j1 = i - 1, jflip = i - 1, nxj0 = i - 1, nxj1 = i - 1;
            int cur0 = 0, cur1 = 0, curflip = 0;
            while (j0 + 1 < n && (fState[j0 + 1] == 0 || iState[j0 + 1] == 0))
            {
                ++j0;
                cur0 += iState[j0] != 0;
            }
            while (j1 + 1 < n && (fState[j1 + 1] == 1 || iState[j1 + 1] == 1))
            {
                ++j1;
                cur1 += iState[j1] != 1;
            }


            while (j0 + 1 < n && (fState[nxj0 + 1] == 0)) ++nxj0;
            while (j1 + 1 < n && (fState[nxj1 + 1] == 1)) ++nxj1;
            while (jflip + 1 < n && iState[jflip + 1] != fState[jflip + 1]) ++jflip, ++curflip;
            int nxt = i - 1;
            if (curflip >= cur0 && curflip >= cur1)
            {
                FOR(j, i, jflip)
                    iState[j] ^= 1;
                nxt = jflip;
            }
            else if (cur0 >= curflip && cur0 >= cur1)
            {
                FOR(j, i, j0)
                    iState[j] = 0;
                nxt = nxj0;
            }
            else
            {
                FOR(j, i, j1)
                    iState[j] = 1;
                nxt = nxj1;
            }
            ++res;
            // dbg(i, cur0, cur1, curflip, nxt);
            i = nxt;
        }

        cout << res;
    }
}

void solve()
{
    cin >> n;
    FOR(i, 1, n)
    {
        char ch; cin >> ch;
        iState.push_back(ch - '0');
    }
    FOR(i, 1, n)
    {
        char ch; cin >> ch;
        fState.push_back(ch - '0');
    }

    // if (n <= 18) return sub1 :: solve(), void();
    AC :: solve();
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

lamp.cpp: In function 'int main()':
lamp.cpp:229:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  229 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lamp.cpp:230:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  230 |         freopen(TASK".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...