Submission #342824

# Submission time Handle Problem Language Result Execution time Memory
342824 2021-01-02T23:44:02 Z Kalashnikov K blocks (IZhO14_blocks) C++17
0 / 100
1 ms 512 KB
#include <bits/stdc++.h>
 
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
 
using namespace std;
using ll = long long;
 
const int N = 2e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7 , P = 6547;

int a[N];
double dp[N];
ll cnt[N];
int n , k;
ll ans = 0;

void check(double lam) {
	dp[1] = a[1] - lam;
	cnt[1] = 1;
	set<int> st;
	st.insert(a[1]);
	ans = 0;
	for(int i = 1; i < n; i ++) {
		dp[i+1] = min(max(dp[i] , a[i+1]-lam) , dp[i] + a[i+1] - lam);
		cnt[i+1] = cnt[i] + (max(dp[i] , a[i+1]-lam) > dp[i] + a[i+1] - lam);
		if(cnt[i] == cnt[i+1]) {
			st.insert(a[i+1]);
		}
		else {
			ans += *st.rbegin();
			st.clear();
			st.insert(a[i+1]);
		}
	}
	ans += *st.rbegin();
}

void solve() {
	cin >> n >> k;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
	}
	for(int i = 0; i <= 1000; i ++) {
		check(i);
		//cout << dp[n]+cnt[n]*i << ' ' << cnt[n] << '\n';
	}
	double l = 0 , r = 1e18;
	double lam;
	for(int i = 1; i <= 200; i ++) {
		double md = (l+r) / 2;
		check(md);	
		if(cnt[n] <= k) {
			lam = md;
			l = md;
		}
		else {
			r = md;
		}
	}
	check(lam);
	cout << ans;
}
/*
*/
main() {
	file("blocks");
    ios;
    int tt = 1;
    //cin >> tt;
    while(tt --) {
        solve();
    }
    return 0;
}

Compilation message

blocks.cpp:69:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   69 | main() {
      |      ^
blocks.cpp: In function 'int main()':
blocks.cpp:4:48: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
    4 | #define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
      |                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
blocks.cpp:70:2: note: in expansion of macro 'file'
   70 |  file("blocks");
      |  ^~~~
blocks.cpp:4:77: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
    4 | #define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
      |                                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:70:2: note: in expansion of macro 'file'
   70 |  file("blocks");
      |  ^~~~
blocks.cpp: In function 'void solve()':
blocks.cpp:64:7: warning: 'lam' may be used uninitialized in this function [-Wmaybe-uninitialized]
   64 |  check(lam);
      |  ~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Incorrect 1 ms 364 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Incorrect 1 ms 364 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Incorrect 1 ms 364 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Incorrect 1 ms 364 KB Output isn't correct
9 Halted 0 ms 0 KB -