#include <iostream> #include <vector> #include <set> using namespace std; vector<vector<int>> values; vector<vector<int>> parent_count; int main() { ios_base::sync_with_stdio(0); int n,k; cin >> n >> k; for (int row=0; row < n; row ++) { vector<int> r; vector<int> rpc; for (int col=0; col < row + 1; col ++) { int v; cin >> v; r.push_back(v); if (row == 0) { rpc.push_back(1); } else { int pc; if (row == col) pc = row; else pc = parent_count[row-1][col] + col; pc++; rpc.push_back(pc); //cout << pc << " "; } } //cout << endl; parent_count.push_back(rpc); values.push_back(r); } int best = 1e9; pair<int, int> best_pos; for (int row=0; row < n; row ++) { for (int col=0; col < row + 1; col ++) { if (parent_count[row][col] <= k) { if (values[row][col] < best) { best = values[row][col]; best_pos = make_pair(row, col); } } } } cout << best << 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 | #include <iostream> #include <vector> #include <set> using namespace std; vector<vector<int>> values; vector<vector<int>> parent_count; int main() { ios_base::sync_with_stdio(0); int n,k; cin >> n >> k; for (int row=0; row < n; row ++) { vector<int> r; vector<int> rpc; for (int col=0; col < row + 1; col ++) { int v; cin >> v; r.push_back(v); if (row == 0) { rpc.push_back(1); } else { int pc; if (row == col) pc = row; else pc = parent_count[row-1][col] + col; pc++; rpc.push_back(pc); //cout << pc << " "; } } //cout << endl; parent_count.push_back(rpc); values.push_back(r); } int best = 1e9; pair<int, int> best_pos; for (int row=0; row < n; row ++) { for (int col=0; col < row + 1; col ++) { if (parent_count[row][col] <= k) { if (values[row][col] < best) { best = values[row][col]; best_pos = make_pair(row, col); } } } } cout << best << endl; } |