# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
404811 | LptN21 | Salesman (IOI09_salesman) | C++17 | 317 ms | 24920 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (p2-p1)*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=t, f[n+1].m=0, f[0].l=t, f[0].best=0;
upd(0);
for(int i=1;i<=n;i++) {
//printf("%d %d %d\n", f[i].t, f[i].l, f[i].m);
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].l==f[i+1].l) i++;
r=i;
for(int j=l;j<=r;j++) f[i].bbest=f[i].best=qry(i);
// U -> D
f[l].cbest=f[i].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[i].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(i);
}
}
f[n+1].best=qry(n+1);
printf("%d", max(0, f[n+1].best));
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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |