Submission #53549

# Submission time Handle Problem Language Result Execution time Memory
53549 2018-06-30T07:52:02 Z 윤교준(#1420) Salesman (IOI09_salesman) C++11
60 / 100
1000 ms 47280 KB
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
//#define rf(x) (x)=0;while(*p<48)im=*p=='-';while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);if(im)(x)=-(x);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define cb(x) (x)=(!(x))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
#define INFST (0x7f7f7f7f)
#define INFLLST (0x7f7f7f7f7f7f7f7fll)
using namespace std;
typedef long long ll;
typedef __int128_t lll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
typedef complex<ld> base;
const ld EPS = (ld)1e-7;
const ld PI = acos(0) * 2;
bool isZero(const ld& x) { return abs(x) <= EPS; }
int sign(const ld& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }
ll gcd(ll a, ll b) { for(;b;a%=b,swap(a,b)){} return abs(a); }
pll operator + (const pll& a, const pll& b) { return pll(a.first+b.first, a.second+b.second); }
pll operator - (const pll& a, const pll& b) { return pll(a.first-b.first, a.second-b.second); }
pll operator * (const pll& a, const ll& b) { return pll(a.first*b, a.second*b); }
ll operator * (const pll& a, const pll& b) { return a.first*b.second - b.first*a.second; }
ll ccw(const pll& a, const pll& b, const pll& c) { return a*b + b*c + c*a; }
int rd(int s, int e) { return rand()%(e-s+1) + s; }
void fg(vector<int> G[], int a, int b) { G[a].pb(b); G[b].pb(a); }
void fg(vector<pii> G[], int a, int b, int c) { G[a].pb({b, c}); G[b].pb({a, c}); }

const int MAXN = 500005;
const int MAXX = 500005;
const int MX = 524288;

struct SEG {
	SEG() { init(); }
	int d[MX*2];
	void init() { fill(d, d+MX*2, -INF); }
	void upd(int x, int r) {
		x += MX; d[x] = r;
		for(x /= 2; x; x /= 2)
			d[x] = max(d[x*2], d[x*2+1]);
	}
	int get(int s, int e) {
		int r = -INF; for(s += MX, e += MX; s <= e; s /= 2, e /= 2) {
			if(s&1) upmax(r, d[s++]);
			if(~e&1) upmax(r, d[e--]);
		} return r;
	}
};

struct NOD {
	NOD(int d = 0, int x = 0, int c = 0) : d(d), x(x), c(c) {}
	int d, x, c;

	bool operator < (const NOD &t) const {
		if(d != t.d) return d < t.d;
		return x < t.x;
	}
};

SEG segu, segd, segiu, segid;
SEG segs1, sege1, segs2, sege2;

int dp[MAXN];

NOD nod[MAXN];
int SM[MAXN];

int SI[MAXX], EI[MAXX];

int N, CU, CD, SX;

int main() {
	scanf("%d%d%d%d", &N, &CU, &CD, &SX);
	for(int i = 1; i <= N; i++) {
		int d, x, c;
		scanf("%d%d%d", &d, &x, &c);
		nod[i] = NOD(d, x, c);
	}
	nod[N+1] = NOD(0, SX, 0);
	nod[N+2] = NOD(MAXX-1, SX, 0);

	sort(nod+1, nod+N+3);
	N += 2;

	fill(SI, SI+MAXX, INF);
	fill(EI, EI+MAXX, -INF);
	for(int i = 1; i <= N; i++) {
		upmin(SI[nod[i].d], i);
		upmax(EI[nod[i].d], i);
		SM[i] = SM[i-1] + nod[i].c;
	}

	segu.upd(SX, -CU * SX);
	segd.upd(SX, CD * SX);

	for(int t = 1, s, e; t < MAXX; t++) {
		s = SI[t]; e = EI[t];
		if(s > e) continue;
		//printf("t=%d : %d %d\n", t, s, e);

		for(int i = s; i <= e; i++) {
			int &ret = dp[i];
			if(SX <= nod[i].x) ret = nod[i].c - CD * (nod[i].x - SX);
			else ret = nod[i].c - CU * (SX - nod[i].x);
			upmax(ret, segu.get(nod[i].x+1, MAXX-1) + CU*nod[i].x + nod[i].c);
			upmax(ret, segd.get(0, nod[i].x-1) - CD*nod[i].x + nod[i].c);
			segu.upd(nod[i].x, ret - CU*nod[i].x);
			segd.upd(nod[i].x, ret + CD*nod[i].x);
		}

		for(int i = s; i <= e; i++) {
			segiu.upd(i, dp[i] - SM[i-1] - CU*nod[i].x);
			segid.upd(i, dp[i] - SM[i] + CD*nod[i].x);

			segs1.upd(i, dp[i] - SM[i] + CD*nod[i].x);
			sege1.upd(i, SM[i] - CD*nod[i].x - CU*nod[i].x);

			segs2.upd(i, -SM[i-1] + CU*nod[i].x + CD*nod[i].x);
			sege2.upd(i, dp[i] + SM[i-1] - CU*nod[i].x);
		}

		for(int i = s; i <= e; i++) {
			int &ret = dp[i];
			upmax(ret, segiu.get(i+1, e) + SM[i-1] + CU*nod[i].x);
			upmax(ret, segid.get(s, i-1) - SM[i] - CD*nod[i].x);

			upmax(ret, segs1.get(s, i-1) + sege1.get(i+1, e) + CU*nod[i].x);
			upmax(ret, segs2.get(s, i-1) + sege2.get(i+1, e) - CD*nod[i].x);
		}

		for(int i = s; i <= e; i++) {
			segu.upd(nod[i].x, dp[i] - CU*nod[i].x);
			segd.upd(nod[i].x, dp[i] + CD*nod[i].x);
		}
	}

/*
	for(int i = 1; i <= N; i++)
		printf("%d : x=%d, c=%d :: %d\n", i, nod[i].x, nod[i].c, dp[i]);
*/

	cout << dp[N] << endl;
	return 0;
}

Compilation message

salesman.cpp: In function 'int main()':
salesman.cpp:90:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &N, &CU, &CD, &SX);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &d, &x, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 37 ms 42872 KB Output is correct
2 Correct 37 ms 43132 KB Output is correct
3 Correct 37 ms 43164 KB Output is correct
4 Correct 39 ms 43164 KB Output is correct
5 Correct 42 ms 43248 KB Output is correct
6 Correct 73 ms 43280 KB Output is correct
7 Correct 119 ms 43636 KB Output is correct
8 Correct 193 ms 43912 KB Output is correct
9 Correct 270 ms 44364 KB Output is correct
10 Correct 469 ms 45572 KB Output is correct
11 Correct 530 ms 45648 KB Output is correct
12 Correct 671 ms 46384 KB Output is correct
13 Correct 675 ms 46384 KB Output is correct
14 Correct 840 ms 47144 KB Output is correct
15 Correct 810 ms 47144 KB Output is correct
16 Correct 857 ms 47144 KB Output is correct
17 Incorrect 36 ms 47144 KB Output isn't correct
18 Incorrect 36 ms 47144 KB Output isn't correct
19 Incorrect 38 ms 47144 KB Output isn't correct
20 Incorrect 41 ms 47144 KB Output isn't correct
21 Incorrect 40 ms 47144 KB Output isn't correct
22 Incorrect 52 ms 47144 KB Output isn't correct
23 Incorrect 44 ms 47144 KB Output isn't correct
24 Incorrect 43 ms 47144 KB Output isn't correct
25 Incorrect 240 ms 47144 KB Output isn't correct
26 Incorrect 403 ms 47144 KB Output isn't correct
27 Incorrect 697 ms 47144 KB Output isn't correct
28 Incorrect 697 ms 47144 KB Output isn't correct
29 Incorrect 985 ms 47276 KB Output isn't correct
30 Execution timed out 1072 ms 47280 KB Time limit exceeded