Submission #425478

# Submission time Handle Problem Language Result Execution time Memory
425478 2021-06-13T04:55:30 Z Kevin_Zhang_TW Sparklers (JOI17_sparklers) C++17
0 / 100
1 ms 204 KB
#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T, class ...U> void kout (T v, U ...e) { cerr << v << ' ', kout(e...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 100010;
 
const ll inf = 1ll << 55;
 
int n, k, T;
int pos[MAX_N];
 
ll a[MAX_N];

bool valid(ll S) {

	for (int i = 1;i <= n;++i) {
		a[i] = 2 * S * T * i - pos[i];
	}

	int cl = k, cr = k, gl = min_element(a+1, a+1+k) - a, gr = max_element(a+1+k, a+1+n) - a;

	// a[l] <= a[r] for all time

	ll lv = a[cl], rv = a[cr];

	while (cl > gl || cr < gr) {
		int sl = cl, sr = cr;
		while (cl > gl && a[cl-1] <= rv)
			chmin(lv, a[--cl]);
		while (cr < gr && a[cr+1] >= lv)
			chmin(rv, a[++cr]);
		if (sl == cl && sr == cr) return false;
	}

	lv = a[1], rv = a[n];

	if (lv > rv) return false;

	cl = 1, cr = n;
	while (cl < gl || cr > gr) {
		int sl = cl, sr = cr;
		while (cl < gl && a[cl+1] <= rv)
			chmin(lv, a[++cl]);
		while (cr > gr && a[cr-1] >= lv)
			chmin(rv, a[--cr]);
		if (sl == cl && sr == cr) return false;
	}
	return true;
}
 
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> k >> T;
	for (int i = 1;i <= n;++i)
		cin >> pos[i];

	//assert(n < MAX_N);
 
	ll L = 0, R = 2 * pos[n], M;
 
	while (L < R) {
		M = L + R >> 1;
		if (valid(M))
			R = M;
		else L = M + 1;
	}
 
	cout << L << '\n';
	
}

Compilation message

sparklers.cpp: In function 'int32_t main()':
sparklers.cpp:79:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   79 |   M = L + R >> 1;
      |       ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -