Submission #404814

#TimeUsernameProblemLanguageResultExecution timeMemory
404814LptN21Salesman (IOI09_salesman)C++17
100 / 100
290 ms15940 KiB
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define oo 1e9
#define eps 1e-9
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int N = 5e5+7, M=2e5+7;
const int MOD = 1e9+7;

int n, m, k, t;

struct fair{
    int t, l, m;
    int best, cbest, bbest;
    bool operator<(const fair &p) const {
        if(t==p.t) return l<p.l;
        return t<p.t;
    }
} f[N];

int fen[N][2];
// 0 : U, 1 : D
void add(int j, int i, int v) {for(;i<N;i+=i&-i) fen[i][j]=max(fen[i][j], v);}
int get(int j, int i) {
    int res=-oo;
    for(;i>0;i-=i&-i) res=max(res, fen[i][j]);
    return res;
}
int cost(int p1, int p2) { // p1 -> p2
    if(p1<p2) return (p2-p1)*k;
    return (p1-p2)*m;
}
void upd(int idx) {
    add(0, f[idx].l, f[idx].best+f[idx].l*k), add(1, N-f[idx].l, f[idx].best-f[idx].l*m);
}
int qry(int idx) {
    return f[idx].m+max(get(0, f[idx].l)-f[idx].l*k, get(1, N-f[idx].l)+f[idx].l*m);
}

signed main() {
    //freopen("test.inp", "r", stdin);
    //freopen("test.out", "w", stdout);
    //fastIO;
    scanf("%d%d%d%d", &n, &m, &k, &t);
    // m : u, k : d
    for(int i=0;i<N;i++) for(int j=0;j<2;j++) fen[i][j]=-oo;
    for(int i=1;i<=n;i++) scanf("%d%d%d", &f[i].t, &f[i].l, &f[i].m);
    sort(f+1, f+n+1);f[n+1].l=f[0].l=t, f[n+1].m=f[0].best=0;
    upd(0);
    for(int i=1;i<=n;i++) {
        if(i==n||f[i].t!=f[i+1].t) f[i].best=qry(i), upd(i);
        else {
            int l=i++, r;
            while(i<=n&&f[i].t==f[l].t) i++;
            r=--i;
            for(int j=l;j<=r;j++) f[j].bbest=f[j].best=qry(j);
            // U -> D
            f[l].cbest=f[l].bbest;
            for(int j=l+1;j<=r;j++) {
                f[j].cbest=max(f[j-1].cbest-cost(f[j-1].l, f[j].l)+f[j].m, f[j].bbest);
                f[j].best=max(f[j].best, f[j].cbest);
            }
            // D -> U
            f[r].cbest=f[r].bbest;
            for(int j=r-1;j>=l;j--) {
                f[j].cbest=max(f[j+1].cbest-cost(f[j+1].l, f[j].l)+f[j].m, f[j].bbest);
                f[j].best=max(f[j].best, f[j].cbest);
            }
            for(int j=l;j<=r;j++) upd(j);
        }
    }
    printf("%d", max(0, qry(n+1)));
    return 0;
}
/* stuff you should look for
    - int overflow, array bounds
    - special cases (n=1?)
    - do smth instead of do nothing and stay organized
    - WRITE STUFF DOWN
    - DONT JUST STICK ON ONE APPROACH
*/

Compilation message (stderr)

salesman.cpp: In function 'int main()':
salesman.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |     scanf("%d%d%d%d", &n, &m, &k, &t);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:56:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |     for(int i=1;i<=n;i++) scanf("%d%d%d", &f[i].t, &f[i].l, &f[i].m);
      |                           ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...