답안 #412942

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412942 2021-05-27T20:28:03 Z amoo_safar Sparklers (JOI17_sparklers) C++17
100 / 100
169 ms 10984 KB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

int n, k;
ll X[N];
ll psA[N], psB[N];

bool Valid(vector<ll> psA, vector<ll> psB){
	int sa = psA.size(), sb = psB.size();
	int pa = 0, pb = 0;
	if(psA[sa - 1] + psB[sb - 1] < 0)
		return false;
	
	int da = 0, db = 0;

	while((pa < sa - 1) || (pb < sb - 1)){
		ll nw = psA[pa] + psB[pb];
		if(nw < 0) return false;
		if(pa == sa - 1){
			pb ++;
			continue;
		}
		if(pb == sb - 1){
			pa ++;
			continue;
		}
		// int des = pa;
		da = max(da, pa + 1);
		int jmp = 0;
		for(; da < sa; da ++){
			if(psA[da] + psB[pb] < 0) break;
			if(psA[pa] <= psA[da]){
				jmp = 1;
				break;
			}
		}
		if(jmp){
			pa = da;
			continue;
		}
		int des = pb;
		for(int nx = pb + 1; nx < sb; nx ++){
			if(psA[pa] + psB[nx] < 0) break;
			if(psB[pb] <= psB[nx]){
				des = nx;
				break;
			}
		}
		if(des > pb){
			pb = des;
			continue;
		}
		////
		return false;
	}
	return true;
}

bool Check(ll T){
	if(T > 1000000000)
		return true;
	
	vector<ll> A = {T}, B = {};
	
	for(int i = k + 1; i <= n; i++){
		A.pb(X[i - 1] - X[i]);
		A.pb(T);
	}
	for(int i = k - 1; i >= 1; i--){
		B.pb(X[i] - X[i + 1]);
		B.pb(T);
	}
	psA[0] = 0;
	for(int i = 1; i <= (int) A.size(); i++)
		psA[i] = psA[i - 1] + A[i - 1];
	psB[0] = 0;
	for(int i = 1; i <= (int) B.size(); i++)
		psB[i] = psB[i - 1] + B[i - 1];

	int idx = max_element(psA, psA + A.size() + 1) - psA;
	vector<ll> la, ra;
	for(int i = 0; i <= idx; i++) la.pb(psA[i]);
	for(int i = idx; i <= (int) A.size(); i++) ra.pb(psA[i]);
	
	idx = max_element(psB, psB + B.size() + 1) - psB;
	vector<ll> lb, rb;
	for(int i = 0; i <= idx; i++) lb.pb(psB[i]);
	for(int i = idx; i <= (int) B.size(); i++) rb.pb(psB[i]);

	reverse(all(ra));
	reverse(all(rb));
	return Valid(la, lb) && Valid(ra, rb);
}


int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll T;
	cin >> n >> k >> T;
	for(int i = 1; i <= n; i++)
		cin >> X[i];

	int mid, L = -1, R = 1000000000;
	while(L + 1 < R){
		mid = (L + R) >> 1;
		if(Check(2ll * T * mid))
			R = mid;
		else L = mid;
	}
	cout << R << '\n';
	return 0;
}

Compilation message

sparklers.cpp: In function 'bool Valid(std::vector<long long int>, std::vector<long long int>)':
sparklers.cpp:32:14: warning: unused variable 'db' [-Wunused-variable]
   32 |  int da = 0, db = 0;
      |              ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 428 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 428 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 428 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 86 ms 6688 KB Output is correct
42 Correct 5 ms 600 KB Output is correct
43 Correct 22 ms 2132 KB Output is correct
44 Correct 142 ms 9620 KB Output is correct
45 Correct 136 ms 9296 KB Output is correct
46 Correct 130 ms 10416 KB Output is correct
47 Correct 134 ms 10296 KB Output is correct
48 Correct 137 ms 9232 KB Output is correct
49 Correct 158 ms 9856 KB Output is correct
50 Correct 142 ms 9848 KB Output is correct
51 Correct 132 ms 10512 KB Output is correct
52 Correct 157 ms 10984 KB Output is correct
53 Correct 160 ms 9788 KB Output is correct
54 Correct 166 ms 10884 KB Output is correct
55 Correct 133 ms 9224 KB Output is correct
56 Correct 135 ms 10796 KB Output is correct
57 Correct 161 ms 9880 KB Output is correct
58 Correct 136 ms 9452 KB Output is correct
59 Correct 130 ms 7704 KB Output is correct
60 Correct 168 ms 10972 KB Output is correct
61 Correct 164 ms 10752 KB Output is correct
62 Correct 131 ms 10652 KB Output is correct
63 Correct 161 ms 10832 KB Output is correct
64 Correct 124 ms 8900 KB Output is correct
65 Correct 135 ms 9376 KB Output is correct
66 Correct 129 ms 9088 KB Output is correct
67 Correct 121 ms 9544 KB Output is correct
68 Correct 169 ms 10956 KB Output is correct
69 Correct 166 ms 10932 KB Output is correct