# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1000940 | toilanvd | Salesman (IOI09_salesman) | C++14 | 532 ms | 65536 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll INF= 1000000000000005LL, cons= 500001;
ll n, upcost, downcost, sta;
vector<ii> a[500005];
ll g[500005][2], f[500005][2], s[500005];
ll d[2][2000505];
void update(ll type, ll k, ll p, ll q, ll x, ll z){
if(p>q || p>x || q<x) return;
if(p==x && x==q){
d[type][k]= z;
return;
}
update(type,2*k,p,(p+q)/2,x,z);
update(type,2*k+1,(p+q)/2+1,q,x,z);
d[type][k]= max(d[type][2*k], d[type][2*k+1]);
}
ll get(ll type, ll k, ll p, ll q, ll x, ll y){
if(p>q || p>y || q<x) return -INF;
if(x<=p && q<=y) return d[type][k];
return max(get(type,2*k,p,(p+q)/2,x,y),get(type,2*k+1,(p+q)/2+1,q,x,y));
}
int main(){
//freopen("salesman.018.in","r",stdin);
//freopen("salesman.out","w",stdout);
ios_base::sync_with_stdio(false);
cin >> n >> upcost >> downcost >> sta;
a[0].push_back(ii(sta,0)); a[cons].push_back(ii(sta,0));
for(ll i=1;i<=n;i++){
ll x, y, z; cin >> x >> y >> z;
a[x].push_back(ii(y,z));
}
for(ll i=1;i<=5*cons;i++){d[0][i]= -INF; d[1][i]= -INF;}
update(0,1,1,cons,sta,upcost*sta);
update(1,1,1,cons,sta,-downcost*sta);
for(ll i=cons-1;i>=0;i--){
if(a[i].empty()) continue;
sort(a[i].begin(),a[i].end());
ll maxi= -INF;
for(ll j=0;j<(ll)a[i].size();j++){
s[j+1]= s[j]+a[i][j].y;
maxi= max(maxi, -s[j]+(upcost+downcost)*a[i][j].x);
g[j+1][0]= maxi+(s[j+1]-(upcost+downcost)*a[i][j].x);
}
maxi= -INF;
for(ll j=(ll)a[i].size()-1;j>=0;j--){
maxi= max(maxi, s[j+1]-(upcost+downcost)*a[i][j].x);
g[j+1][1]= maxi+(-s[j]+(upcost+downcost)*a[i][j].x);
}
for(ll j=0;j<(ll)a[i].size();j++){
f[j+1][1]= get(0,1,1,cons,1,a[i][j].x)-upcost*a[i][j].x;
f[j+1][1]= max(f[j+1][1], get(1,1,1,cons,a[i][j].x,cons)+downcost*a[i][j].x);
}
maxi= -INF;
for(ll j=0;j<(ll)a[i].size();j++){
maxi= max(maxi, f[j+1][1]+upcost*a[i][j].x-s[j]);
f[j+1][0]= maxi+(-upcost*a[i][j].x+s[j]+g[j+1][1]);
}
maxi= -INF;
for(ll j=(ll)a[i].size()-1;j>=0;j--){
maxi= max(maxi, f[j+1][1]-downcost*a[i][j].x+s[j+1]);
f[j+1][0]= max(f[j+1][0], maxi+(downcost*a[i][j].x-s[j+1]+g[j+1][0]));
}
for(ll j=0;j<(ll)a[i].size();j++){
update(0,1,1,cons,a[i][j].x,f[j+1][0]+upcost*a[i][j].x);
update(1,1,1,cons,a[i][j].x,f[j+1][0]-downcost*a[i][j].x);
}
}
cout << f[1][0] << "\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |