# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
404858 | LptN21 | Salesman (IOI09_salesman) | C++14 | 296 ms | 16072 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 b, bb, cb;
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 : UP, 1 : DOWN
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;}
void upd(int idx) {
add(0, f[idx].l, f[idx].b+f[idx].l*k), add(1, N-f[idx].l, f[idx].b-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);
// u : m, d : k
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[0].b=f[n+1].m=0, f[0].l=f[n+1].l=t;upd(0);
for(int i=1;i<=n;i++) {
if(i==n||f[i].t!=f[i+1].t) f[i].b=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].bb=f[j].b=qry(j);
// U -> D
f[l].cb=f[l].bb;
for(int j=l+1;j<=r;j++) {
f[j].cb=max(f[j-1].cb-(f[j].l-f[j-1].l)*k+f[j].m, f[j].bb);
f[j].b=max(f[j].cb, f[j].b);
}
// D -> U
f[r].cb=f[r].bb;
for(int j=r-1;j>=l;j--) {
f[j].cb=max(f[j+1].cb-(f[j+1].l-f[j].l)*m+f[j].m, f[j].bb);
f[j].b=max(f[j].cb, f[j].b);
}
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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |