Submission #1084629

#TimeUsernameProblemLanguageResultExecution timeMemory
1084629RiverFlowGarden (JOI23_garden)C++14
75 / 100
3069 ms14280 KiB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int mod = (int)1e9 + 7;

void prepare(); void main_code();

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0; prepare();
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); }
}

void prepare() {};

const int D = (int)5e3 + 2;
const int N = (int)5e5 + 9;

int p[N], q[N], r[N], s[N];

int n, m, d, cnt[D];

vector<int> ev[D];

// tim q[i] dau tien < x

vector<int> val, chain;

int len = 0;

int dist(int a, int b) {
    return a < b ? b - a - 1 : d - a - 1 + b;
}

const bool dbg=0;

struct linked_list {
    int nxt[D], pre[D];
    int max_gap = 0;
    void init() {
        int lst = -1;
        if (dbg) {
            cout << "chain: ";
            FOR(i, 0, d - 1) if (cnt[i]>0)cout<<i<<' ';
            cout<<nl;
        }
        FOR(i, 0, d - 1) {
            if (cnt[i] > 0) {
                pre[i] = lst;
                if (lst != -1) {
                    nxt[lst] = i;
                    maxi(max_gap, dist(lst, i));
                }
                lst = i;
            }
        }
        assert(lst != -1);
        FOR(i, 0, d - 1) {
            if (cnt[i] > 0) {
                nxt[lst] = i;
                pre[i] = lst;
                maxi(max_gap, dist(lst, i));
                break ;
            }
        }
        if (dbg) cout << "--maxgap: " << max_gap << nl;
    }
    void era(int v) {
        --cnt[v];
        if (cnt[v] == 0) {
            int p = pre[v], n = nxt[v];
            maxi(max_gap, dist(p, n));
            nxt[p] = n, pre[n] = p;
        }
    }
};

bool in(int a, int b, int x) {
    if (a <= b) return (x >= a) and (x <= b);
    return (x >= a) or (x <= b);
}

int solve(int x) {
    int p = (val[0] >= x ? -1 : (--lower_bound(all(val), x)) - val.begin());
    int u = (p == -1 ? val.back() : val[p]);

    if (dbg) cout << x << ' ' << u << nl;

    FOR(i, 1, m) {
        if (!in(x, u, s[i])) {
            chain.push_back(r[i]);
        }
    }
    FOR(i, 0, d - 1) cnt[i] = 0;
    for(int v : chain) ++cnt[v];

    linked_list px;
    px.init();

    while (sz(chain) > len) chain.pop_back();

    int cost = INT_MAX;

    int st = (x<=u?u-x+1:d-x+u+1);
    for(int j = st; j <= d; ++j) {
        cost = min(cost, j * (d - px.max_gap));
//        cout << px.max_gap << ' ';
        ++u;
        if (u >= d) u -= d;
        for(int p : ev[u]) {
            px.era(p);
        }
    }
//    cout << nl;
    return cost;
}

void main_code() {
    cin >> n >> m >> d;
    for(int i = 1; i <= n; ++i) cin >> p[i] >> q[i];
    for(int i = 1; i <= m; ++i) cin >> r[i] >> s[i];

    FOR(i, 1, n) val.push_back(q[i]);
    sort(val.begin(), val.end());

    FOR(i, 1, n) chain.push_back(p[i]);
    len = sz(chain);

    // [0, d - 1)
    // always mot so gia tri o truoc
    FOR(i, 1, m) {
        ev[s[i]].push_back(r[i]);
    }
    int ans = d * d;
    for(int i = 0; i < d; ++i) {
        ans = min(ans, solve(i));
    }
    cout << ans;
}


/*     Let the river flows naturally     */

Compilation message (stderr)

garden.cpp: In function 'int main()':
garden.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
garden.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...