#include <cmath> #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <map> #include <list> #include <time.h> #include <math.h> #include <random> #include <deque> #include <queue> #include <cassert> #include <unordered_map> #include <unordered_set> #include <iomanip> #include <bitset> #include <sstream> #include <chrono> #include <cstring> using namespace std; typedef long long ll; /* #ifdef iq mt19937 rnd(228); #else mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif */ const int N = 2005; int a[N][N]; int dp[N][N]; int main() { #ifdef iq freopen("a.in", "r", stdin); //freopen("a.out", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); int n, k; while (cin >> n >> k) { for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) dp[i][j] = 0; int ans = 3000; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { cin >> a[i][j]; if (i + 1 < n) { dp[i + 1][j] += dp[i][j] + 1; dp[i + 1][j + 1] += dp[i][j] + 1; if (i + 2 < n) { dp[i + 2][j + 1] -= dp[i][j] + 1; } } int cnt = n - i; int ok = n * (n + 1) / 2; int rest = dp[i][j]; ok -= rest; if (rest < k && ok - cnt * (cnt + 1) / 2 >= k - 1 - rest) { ans = min(ans, a[i][j]); } } } cout << ans << endl; } }
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include <cmath> #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <map> #include <list> #include <time.h> #include <math.h> #include <random> #include <deque> #include <queue> #include <cassert> #include <unordered_map> #include <unordered_set> #include <iomanip> #include <bitset> #include <sstream> #include <chrono> #include <cstring> using namespace std; typedef long long ll; /* #ifdef iq mt19937 rnd(228); #else mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif */ const int N = 2005; int a[N][N]; int dp[N][N]; int main() { #ifdef iq freopen("a.in", "r", stdin); //freopen("a.out", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); int n, k; while (cin >> n >> k) { for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) dp[i][j] = 0; int ans = 3000; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { cin >> a[i][j]; if (i + 1 < n) { dp[i + 1][j] += dp[i][j] + 1; dp[i + 1][j + 1] += dp[i][j] + 1; if (i + 2 < n) { dp[i + 2][j + 1] -= dp[i][j] + 1; } } int cnt = n - i; int ok = n * (n + 1) / 2; int rest = dp[i][j]; ok -= rest; if (rest < k && ok - cnt * (cnt + 1) / 2 >= k - 1 - rest) { ans = min(ans, a[i][j]); } } } cout << ans << endl; } } |