답안 #551221

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551221 2022-04-20T06:00:34 Z Sohsoh84 쌀 창고 (IOI11_ricehub) C++17
100 / 100
51 ms 3224 KB
#include "ricehub.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define X			first
#define Y			second
#define sep			' '
#define debug(x)		cerr << #x << ": " << x << endl;

const ll MAXN = 1e6 + 10;

inline pll operator+ (const pll& a, const pll& b) {
	return {a.X + b.X, a.Y + b.Y};
}

int n;
vector<ll> vec;

namespace median {
	pll fen[MAXN], tot = {0, 0};

	inline void update(int ind, pll val) {
		tot = tot + val;
		for (++ind; ind < MAXN; ind += ind & -ind)
			fen[ind] = fen[ind] + val;
	}

	inline pll query(int ind) {
		pll ans = {0, 0};
		for (++ind; ind > 0; ind -= ind & -ind)
			ans = ans + fen[ind];
		return ans;
	}

	inline ll median() {
		int l = 0, r = int(vec.size()) - 1;
		while (l < r) {
			int mid = (l + r) >> 1;
			if (query(mid).Y < (tot.Y + 1) / 2) l = mid + 1;
			else r = mid;
		}

		return l;
	}

	inline ll cost() {
		int ind = median();
		ll ans = 0;

		pll e = query(ind);
		ans += e.Y * vec[ind] - e.X;

		e = tot + pll(-e.X, -e.Y);
		ans += e.X - e.Y * vec[ind];
		return ans;
	}
}	

int besthub(int R, int L, int X[], long long B) {	
	n = R;
	for (int i = 0; i < n; i++)
		vec.push_back(X[i]);

	int ans = 0, l = 0;
	for (int r = 0; r < n; r++) {
		median::update(r, {vec[r], 1});
		
	//	cerr << l + 2 << sep << r + 2 << sep << median::cost() << endl;
	//	break;
		while (median::cost() > B) {
			median::update(l, {-vec[l], -1});
			l++;
		}

		ans = max(ans, r - l + 1);
	}

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 3 ms 468 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 3 ms 468 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 1040 KB Output is correct
2 Correct 14 ms 1032 KB Output is correct
3 Correct 47 ms 3136 KB Output is correct
4 Correct 46 ms 3096 KB Output is correct
5 Correct 39 ms 1796 KB Output is correct
6 Correct 33 ms 1792 KB Output is correct
7 Correct 39 ms 3120 KB Output is correct
8 Correct 43 ms 3224 KB Output is correct
9 Correct 33 ms 1740 KB Output is correct
10 Correct 35 ms 1780 KB Output is correct
11 Correct 46 ms 3204 KB Output is correct
12 Correct 51 ms 3196 KB Output is correct
13 Correct 39 ms 1824 KB Output is correct
14 Correct 36 ms 1844 KB Output is correct
15 Correct 31 ms 2568 KB Output is correct
16 Correct 32 ms 2608 KB Output is correct
17 Correct 38 ms 2996 KB Output is correct
18 Correct 49 ms 3004 KB Output is correct
19 Correct 46 ms 3136 KB Output is correct
20 Correct 39 ms 3076 KB Output is correct