#include <iostream> #include <algorithm> using namespace std; struct Piramid { const int height; const int k; int **piramid; Piramid(const int& h, const int& k) :height(h), k(k) { piramid = new int*[height]; for (int i = 0; i < height; ++i) { piramid[i] = new int[i + 1]; for (int j = 0; j < i + 1; ++j) cin >> piramid[i][j]; } } ~Piramid() { for (int i = 0; i < height; ++i) delete piramid[i]; delete piramid; } int FindBestWine() { int bestWine = 0x7FFFFFFF; for (int i = 1; i <= height; ++i) { for (int j = 1; j <= i; ++j) { if (j * (i - j + 1) - 1 <= k) { if (piramid[i-1][j-1] < bestWine) bestWine = piramid[i-1][j-1]; } } } return bestWine; } }; int main() { int h, k; cin >> h >> k; Piramid P(h, k); cout << P.FindBestWine(); return 0; }
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 | #include <iostream> #include <algorithm> using namespace std; struct Piramid { const int height; const int k; int **piramid; Piramid(const int& h, const int& k) :height(h), k(k) { piramid = new int*[height]; for (int i = 0; i < height; ++i) { piramid[i] = new int[i + 1]; for (int j = 0; j < i + 1; ++j) cin >> piramid[i][j]; } } ~Piramid() { for (int i = 0; i < height; ++i) delete piramid[i]; delete piramid; } int FindBestWine() { int bestWine = 0x7FFFFFFF; for (int i = 1; i <= height; ++i) { for (int j = 1; j <= i; ++j) { if (j * (i - j + 1) - 1 <= k) { if (piramid[i-1][j-1] < bestWine) bestWine = piramid[i-1][j-1]; } } } return bestWine; } }; int main() { int h, k; cin >> h >> k; Piramid P(h, k); cout << P.FindBestWine(); return 0; } |