제출 #1339653

#제출 시각아이디문제언어결과실행 시간메모리
1339653adscodingCoin Collecting (JOI19_ho_t4)C++20
100 / 100
38 ms1608 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#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 __print_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 == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

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

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

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

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

const int maxn = 1e5 + 3, dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int n, sl[maxn][3], lst[3];
ll res;

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

namespace sub1
{
    map<vector<vector<int>>, int> D;

    bool valid(int x, int y)
    {
        return x >= 1 && x <= n && y >= 1 && y <= 2;
    }

    bool check(const auto &state)
    {
        FOR(i, 1, n)
            FOR(j, 1, 2)
                if (state[i][j] != 1)
                    return false;
        return true;
    }

    void solve()
    {
        vector<vector<int>> iState(n + 1, vector<int>(3, 0));
        FOR(i, 1, n)
            FOR(j, 1, 2)
                iState[i][j] = sl[i][j];

        D[iState] = res;
        queue<vector<vector<int>>> q;
        q.push(iState);

        while (q.size())
        {
            vector<vector<int>> state = q.front(); q.pop();

            if (check(state))
                return void(cout << D[state]);

            FOR(i, 1, n)
                FOR(j, 1, 2)
                {
                    if (state[i][j] < 1) continue;
                    vector<vector<int>> nState = state;
                    FOR(k, 0, 3)
                    {
                        int u = i + dx[k], v = j + dy[k];
                        if (!valid(u, v)) continue;
                        --nState[i][j];
                        ++nState[u][v];
                        if (!D.count(nState))
                        {
                            D[nState] = D[state] + 1;
                            q.push(nState);
                        }
                    }
                }
        }
    }
}

namespace AC
{
    ll F(ll x)
    {
        return x * (x + 1) / 2;
    }

    ll G(int l, int r)
    {
        return F(r) - F(l - 1);
    }

    void Lap(int i, int j)
    {
        if (lst[j] >= i - 1) return;
        int cur = min(sl[i][j], i - lst[j] - 1);
        res += G(i - lst[j] - cur, i - lst[j] - 1);
        lst[j] += cur;
        sl[i][j] -= cur;
    }

    void solve()
    {
        FOR(i, 1, n)
        {
            // Lap day nhung cai truoc do bang nhung cai cung hang
            Lap(i, 1);
            Lap(i, 2);


            if (sl[i][1] > 0)
                lst[1] = i;
            if (sl[i][2] > 0)
                lst[2] = i;
            // Lap day nhung cai truoc do bang xu o hang doi dien
            {
                int n1 = i - lst[1], n2 = i - lst[2];
                if (n1 == 0 && n2 > 0)
                {
                    int cur = min(sl[i][1] - 1, n2);
                    res += cur;
                    sl[i][1] -= cur;
                    sl[i][2] += cur;
                    Lap(i, 2);
                }
                else if (n1 > 0 && n2 == 0)
                {
                    int cur = min(sl[i][2] - 1, n1);
                    res += cur;
                    sl[i][2] -= cur;
                    sl[i][1] += cur;
                    Lap(i, 1);
                }
            }

            if (sl[i][1] > 0)
                lst[1] = i;
            if (sl[i][2] > 0)
                lst[2] = i;

            // Neu con du thi day len phia truoc
            if (sl[i][1])
            {
                sl[i + 1][1] += sl[i][1] - 1;
                res += sl[i][1] - 1;
            }
            if (sl[i][2])
            {
                sl[i + 1][2] += sl[i][2] - 1;
                res += sl[i][2] - 1;
            }

        }

        cout << res;
    }
}

void solve()
{
    cin >> n;
    FOR(i, 1, n * 2)
    {
        int x, y; cin >> x >> y;
        if (y > 2)
        {
            res += y - 2;
            y = 2;
        }
        else if (y < 1)
        {
            res += 1 - y;
            y = 1;
        }

        if (x > n)
        {
            res += x - n;
            x = n;
        }
        else if  (x < 1)
        {
            res += 1 - x;
            x = 1;
        }

        ++sl[x][y];

//        dbg(i, x, y, res);
    }

    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;
}

컴파일 시 표준 에러 (stderr) 메시지

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