답안 #684577

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
684577 2023-01-21T19:36:41 Z pragmatist Stove (JOI18_stove) C++17
0 / 100
1 ms 468 KB
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
 
#include<bits/stdc++.h>
 
#define sz(v) (int)v.size()
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define nl "\n"
 
using namespace std;
using pii = pair<int, int>;
 
const int N = (int)5e3 + 7; // make sure this is right
const int M = (int)1e6 + 7;
const int inf = (int)1e9 + 7;
const ll INF = (ll)3e18 + 7; 
const ll MOD = (ll)998244353; // make sure this is right
 
bool bit(int x, int i) {
	return x >> i & 1;
}
 
pii dir[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
 
int n, k, a[N];
pair<ll, int> dp[N];
 
pair<ll, int> check(int C) {
	pair<ll, int> mn = {0, 0};
	for(int i = 1; i <= n; ++i) {
		mn = min(mn, {dp[i - 1].x - a[i], dp[i - 1].y});
		dp[i].x = mn.x + C + a[i];
		dp[i].y = mn.y + 1;
	}
	return {dp[n].x + dp[n].y, dp[n].y};
}
 
void solve() {
	cin >> n >> k;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	int l = 0, r = 1e9, res = -1;
	while(l <= r) {
		int mid = (l + r) >> 1;
		if(check(mid).y <= k) {
			res = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		} 		
	}
	assert(res != -1);
	pair<ll, int> ans = check(res);
	if(ans.y == k) {
		cout << ans.x - res * ans.y << "\n";
	} else {
		pair<ll, int> nxt = check(res + 1);
		ll val1 = nxt.x - nxt.y * (res + 1), val2 = ans.x - ans.y * res;
		ll tot = val1 - val2;
		int delta = (nxt.y - ans.y);
		assert(delta != 0);
		ll d = tot / delta;
		cout << d * (k - ans.y) + val2 - delta / 2;
	}
}
 
signed main() {	
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; ++i) {
	 	//cout << "Case #" << i << ": ";
		solve();
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -