#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAX = 4e6; ll dp[MAX]; int main () { ios_base::sync_with_stdio(0); ll n, k; cin >> n >> k; dp[0] = 0; ll prev = 0; ll a = 1; ll res = 1e9; ll v; while (a <= n) { for (int i = 0; i < a; ++i) { ll id = a * (a + 1) / 2 - a; cin >> v; ll id2 = (a - 1) * a / 2 - a + 1; ll man = 0; if (id + i - a < id && id + i - a >= id2) { man++; dp[id + i] += dp[id + i - a] + 1; } if (id + i - a + 1 < id && id + i - a + 1 >= id2) { man++; dp[id + i] += dp[id + i - a + 1] + 1; } if (man == 2 && id + i - 2 * (a - 1) < id && id + i - 2*(a - 1) >= 0) { dp[id + i] -= (dp[id + i - 2*(a - 1)] + 1); } if (dp[id + i] <= k && v < res) { res = v; } } a++; } cout << res; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAX = 4e6; ll dp[MAX]; int main () { ios_base::sync_with_stdio(0); ll n, k; cin >> n >> k; dp[0] = 0; ll prev = 0; ll a = 1; ll res = 1e9; ll v; while (a <= n) { for (int i = 0; i < a; ++i) { ll id = a * (a + 1) / 2 - a; cin >> v; ll id2 = (a - 1) * a / 2 - a + 1; ll man = 0; if (id + i - a < id && id + i - a >= id2) { man++; dp[id + i] += dp[id + i - a] + 1; } if (id + i - a + 1 < id && id + i - a + 1 >= id2) { man++; dp[id + i] += dp[id + i - a + 1] + 1; } if (man == 2 && id + i - 2 * (a - 1) < id && id + i - 2*(a - 1) >= 0) { dp[id + i] -= (dp[id + i - 2*(a - 1)] + 1); } if (dp[id + i] <= k && v < res) { res = v; } } a++; } cout << res; } |