Submission #73324

# Submission time Handle Problem Language Result Execution time Memory
73324 2018-08-28T07:19:24 Z 윤교준(#2271) Radio (Balkan15_RADIO) C++11
60 / 100
2000 ms 9680 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 pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#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 INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ_P;
typedef priority_queue<int, vector<int>, greater<int>> PQ_M;
static unsigned char str[5500055], *p=str;

const bool debug = 0;
const int MAXN = 100005;

struct EVT {
	EVT(int x, int idx) : x(x), idx(idx) {}
	int x, idx;

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

PQ_M AI, BI, CI, EAI, EBI, ECI;
PQ_P AO, BO, CO, EAO, EBO, ECO;

vector<EVT> EV;
vector<int> XV;

int A[MAXN], B[MAXN], C[MAXN];

ll Ans = INFLL, Sum, SumA, SumC;
int N, K;

inline void prop_M(PQ_M &A, PQ_M &EA) {
	for(; !EA.empty() && !A.empty();) {
		if(A.top() != EA.top()) break;
		A.pop();
		EA.pop();
	}
}
inline void prop_P(PQ_P &B, PQ_P &EB) {
	for(; !EB.empty() && !B.empty();) {
		if(B.top() != EB.top()) break;
		B.pop();
		EB.pop();
	}
}

inline void erase(PQ_M &A, PQ_P &B, PQ_M &EA, PQ_P &EB, int x) {
	prop_M(A, EA); prop_P(B, EB);
	if(!A.empty() && A.top() <= x) {
		if(A.top() == x) A.pop();
		else EA.push(x);
		Sum -= x;
		return;
	}
	if(B.top() == x) B.pop();
	else EB.push(x);
}
inline void goup(PQ_M &A, PQ_P &B, PQ_M &EA, PQ_P &EB) {
	for(int t;;) {
		prop_M(A, EA); prop_P(B, EB);
		if(B.empty()) break;
		t = B.top();
		if(A.empty() || t <= A.top()) break;
		B.pop();
		A.push(t);
		Sum += t;
	}
}
inline void goup(PQ_M &A, PQ_P &B, PQ_M &EA, PQ_P &EB, ll cut, ll dx) {
	for(ll x;;) {
		prop_P(B, EB);
		if(B.empty()) break;
		x = ll(B.top()) + dx;
		if(x <= cut) break;
		x -= dx;
		B.pop();
		A.push(x);
		Sum += x;
	}
}

void f(int X) {
	prop_P(AO, EAO);
	prop_P(BO, EBO);
	prop_P(CO, ECO);
	ll at = (AO.empty() ? -INFLL : AO.top()) + X;
	ll bt = BO.empty() ? -INFLL : BO.top();
	ll ct = (CO.empty() ? -INFLL : CO.top()) - X;
	ll mxt = max({at, bt, ct});
	if(mxt == at) {
		at -= X;
		AI.push(at);
		AO.pop();
		Sum += at;
	} else if(mxt == bt) {
		BI.push(bt);
		BO.pop();
		Sum += bt;
	} else {
		ct += X;
		CI.push(ct);
		CO.pop();
		Sum += ct;
	}
}
void g(int X) {
	prop_M(AI, EAI);
	prop_M(BI, EBI);
	prop_M(CI, ECI);
	ll l = INFLL;
	if(!AI.empty()) upmin(l, ll(AI.top()) + X);
	if(!BI.empty()) upmin(l, ll(BI.top()));
	if(!CI.empty()) upmin(l, ll(CI.top()) - X);
	goup(AI, AO, EAI, EAO, l, X);
	goup(BI, BO, EBI, EBO, l, 0);
	goup(CI, CO, ECI, ECO, l, -X);
}
void h(int X) {
	prop_M(AI, EAI);
	prop_M(BI, EBI);
	prop_M(CI, ECI);
	ll at = (AI.empty() ? INFLL : AI.top()) + X;
	ll bt = BI.empty() ? INFLL : BI.top();
	ll ct = (CI.empty() ? INFLL : CI.top()) - X;
	ll mnt = min({at, bt, ct});
	if(mnt == at) {
		at -= X;
		AI.pop();
		AO.push(at);
		Sum -= at;
	} else if(mnt == bt) {
		BI.pop();
		BO.push(bt);
		Sum -= bt;
	} else {
		ct += X;
		CI.pop();
		CO.push(ct);
		Sum -= ct;
	}
}

int main() {
	fread(str, 1, 5500055, stdin);

	rf(N) rf(K) K = N-K;
	for(int i = 1; i <= N; i++) { rf(A[i]) rf(B[i]) rf(C[i]) }

	XV.eb(-INF); XV.eb(INF);
	for(int i = 1; i <= N; i++) {
		XV.eb(A[i] - B[i]);
		XV.eb(A[i] + B[i]);
		EV.eb(A[i]-B[i], -i);
		EV.eb(A[i]+B[i], i);
	}
	sorv(XV); univ(XV);
	sorv(EV);

	if(debug) {
		for(int i = 1; i <= N; i++) printf("%d ; %d %d %d\n", i, A[i], B[i], C[i]);
		printf("XV : "); for(int v : XV) printf("%d ", v); puts("");
		printf("EV : "); for(auto &v : EV) printf("(%d,%d) ", v.x, v.idx); puts("");
		puts("");
	}

	for(int i = 1; i <= N; i++) {
		CO.push(A[i]-B[i]+C[i]);
		SumC += A[i]-B[i];
	}
	for(int xvi = 0, evi = 0, xvn = sz(XV), evn = sz(EV); xvi < xvn; xvi++) {
		const int X = XV[xvi];
		for(int idx, t; evi < evn && EV[evi].x <= X; evi++) {
			idx = EV[evi].idx;
			if(idx < 0) {
				idx = -idx;
				t = A[idx]-B[idx]+C[idx];
				erase(CI, CO, ECI, ECO, t);
				t = C[idx];
				BO.push(t);
				SumC -= A[idx]-B[idx];
			} else {
				t = C[idx];
				erase(BI, BO, EBI, EBO, t);
				t = -A[idx]-B[idx]+C[idx];
				AO.push(t);
				SumA += -A[idx]-B[idx];
			}
		}
		goup(AI, AO, EAI, EAO);
		goup(BI, BO, EBI, EBO);
		goup(CI, CO, ECI, ECO);
		if(sz(AI) == sz(EAI) && sz(BI) == sz(EBI) && sz(CI) == sz(ECI)) f(X);
		g(X);
		for(int cnt = sz(AI) + sz(BI) + sz(CI) - sz(EAI) - sz(EBI) - sz(ECI); cnt < K; cnt++) f(X);
		for(int cnt = sz(AI) + sz(BI) + sz(CI) - sz(EAI) - sz(EBI) - sz(ECI); K < cnt; cnt--) h(X);

		{
			ll ret = SumA + SumC - Sum;
			ret += ll(X) * (sz(AO) - sz(EAO));
			ret -= ll(X) * (sz(CO) - sz(ECO));
			if(ret < Ans) Ans = ret;

			if(debug) {
				printf("X = %d, xvi=%d, evi=%d :: ret=%lld, Sum=%lld\n", X, xvi, evi, ret, Sum);
			}
		}
	}

	cout << Ans << endl;
	return 0;
}

Compilation message

code1.cpp: In function 'int main()':
code1.cpp:159:7: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  fread(str, 1, 5500055, stdin);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 408 KB Output is correct
4 Correct 2 ms 520 KB Output is correct
5 Correct 2 ms 520 KB Output is correct
6 Correct 2 ms 520 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 536 KB Output is correct
2 Correct 3 ms 544 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 3 ms 592 KB Output is correct
5 Correct 3 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 596 KB Output is correct
2 Correct 2 ms 720 KB Output is correct
3 Correct 4 ms 720 KB Output is correct
4 Correct 3 ms 720 KB Output is correct
5 Correct 3 ms 720 KB Output is correct
6 Correct 3 ms 720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1132 KB Output is correct
2 Correct 23 ms 2404 KB Output is correct
3 Correct 56 ms 4704 KB Output is correct
4 Correct 130 ms 8248 KB Output is correct
5 Correct 109 ms 8248 KB Output is correct
6 Correct 150 ms 9680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9680 KB Output is correct
2 Correct 22 ms 9680 KB Output is correct
3 Correct 449 ms 9680 KB Output is correct
4 Execution timed out 2045 ms 9680 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9680 KB Output is correct
2 Correct 1316 ms 9680 KB Output is correct
3 Execution timed out 2027 ms 9680 KB Time limit exceeded
4 Halted 0 ms 0 KB -