Submission #119789

# Submission time Handle Problem Language Result Execution time Memory
119789 2019-06-22T10:26:34 Z win11905 Salesman (IOI09_salesman) C++11
100 / 100
938 ms 44820 KB
#include <bits/stdc++.h>
#define long long long
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define x first
#define y second
using namespace std;

const long INF = 1e12;

const int N = 1<<19;

int n, u, d, s;
long t1[N<<1], t2[N<<1];

void update(long t[], int x, long v) {
    x += N;
    for(t[x] = max(t[x], v); x != 1; x >>= 1) t[x>>1] = max(t[x], t[x^1]);
}

long query(long t[], int l, int r) {
    long mx = -INF;
    for(l += N, r += N+1; l < r; l >>= 1, r >>= 1) {
        if(l & 1) mx = max(mx, t[l++]);
        if(r & 1) mx = max(mx, t[--r]);
    }
    return mx;
}

long p;

void updcost(long a, long b) {
    update(t1, a, b - p * a);
    update(t2, a, b + p * a);
}

long getcost(long a) {
    long vl = (query(t1, a, N-1) + p * a);
    long vr = (query(t2, 0, a) - p * a);
    return max(vl, vr);
}

long dp1[N], dp2[N], pref[N], suff[N], cost[N];
vector<pii> Mp[N];

int main() {
    fill_n(t1, N<<1, -INF);
    fill_n(t2, N<<1, -INF);
    scanf("%d %d %d %d", &n, &u, &d, &s);
    p = u+d;
    for(int i = 0, a, b, c; i < n; ++i) {
        scanf("%d %d %d", &a, &b, &c);
        Mp[a].emplace_back(b, c << 1);
    }
    updcost(s, 0);
    for(int i = 0; i < N; ++i) if(Mp[i].size()) { // pos, cost
        vector<pii> &now = Mp[i];
        int n = now.size();
        sort(all(now));
        for(int i = 0; i < n; ++i) cost[i] = getcost(now[i].x) + now[i].y;
        dp1[0] = pref[0] = now[0].y;
        for(int i = 1; i < n; ++i) {
            dp1[i] = max(dp1[i-1] - 2 * p * (now[i].x - now[i-1].x), 0ll) + now[i].y;
            pref[i] = pref[i-1] + now[i].y - p * (now[i].x - now[i-1].x);
        }
        dp2[n-1] = suff[n-1] = now[n-1].y;
        for(int i = n-2; i >= 0; --i) {
            dp2[i] = max(dp2[i+1] - 2 * p * (now[i+1].x - now[i].x), 0ll) + now[i].y;
            suff[i] = suff[i+1] + now[i].y - p * (now[i+1].x - now[i].x); 
        }
        long premax = -INF;
        for(int i = 0; i < n; ++i) {
            if(i) cost[i] = max(cost[i], dp2[i] - now[i].y + pref[i] + premax);
            premax = max(premax, dp1[i] - pref[i] + getcost(now[i].x));
        }
        premax = -INF;
        for(int i = n-1; i >= 0; --i) {
            if(i != n-1) cost[i] = max(cost[i], dp1[i] - now[i].y + suff[i] + premax);
            premax = max(premax, dp2[i] - suff[i] + getcost(now[i].x));
        }
        for(int i = 0; i < n; ++i) {
            updcost(now[i].x, cost[i]);
            // cerr << now[i].x << " " << now[i].y << " -> " << cost[i] << endl;
        }
    }
    printf("%lld\n", getcost(s) >> 1);

}

Compilation message

salesman.cpp: In function 'int main()':
salesman.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d %d", &n, &u, &d, &s);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 29056 KB Output is correct
2 Correct 26 ms 29048 KB Output is correct
3 Correct 25 ms 29056 KB Output is correct
4 Correct 27 ms 29184 KB Output is correct
5 Correct 29 ms 29312 KB Output is correct
6 Correct 58 ms 29688 KB Output is correct
7 Correct 110 ms 30584 KB Output is correct
8 Correct 198 ms 32320 KB Output is correct
9 Correct 279 ms 33784 KB Output is correct
10 Correct 535 ms 38648 KB Output is correct
11 Correct 573 ms 38524 KB Output is correct
12 Correct 753 ms 41648 KB Output is correct
13 Correct 768 ms 41580 KB Output is correct
14 Correct 929 ms 44784 KB Output is correct
15 Correct 891 ms 44820 KB Output is correct
16 Correct 938 ms 44792 KB Output is correct
17 Correct 25 ms 29056 KB Output is correct
18 Correct 27 ms 29048 KB Output is correct
19 Correct 27 ms 29176 KB Output is correct
20 Correct 30 ms 29184 KB Output is correct
21 Correct 28 ms 29304 KB Output is correct
22 Correct 32 ms 29184 KB Output is correct
23 Correct 33 ms 29176 KB Output is correct
24 Correct 31 ms 29184 KB Output is correct
25 Correct 163 ms 30344 KB Output is correct
26 Correct 298 ms 32340 KB Output is correct
27 Correct 487 ms 35276 KB Output is correct
28 Correct 567 ms 34572 KB Output is correct
29 Correct 704 ms 34332 KB Output is correct
30 Correct 680 ms 37296 KB Output is correct