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
| #include <iostream> #include <vector> using namespace std;
void find_max_height(int* height, int n, int k, int h, int height_now, int times_now); bool binary_find(const int* height, int begin, int end, int target); int max_height = 0;
int main() { std::cout << "Hello, World!" << std::endl; int n, k, h; cin >> n >> k >> h; int height[n]; for(int i =0; i < n; i++) { cin >> height[i]; } find_max_height(height, n, k, h, 0, 0); cout << max_height; return 0; }
void find_max_height(int* height, int n, int k, int h, int height_now, int times_now) { if(height_now > max_height) max_height = height_now; if(times_now < k && height_now >=0) { for(int i = height_now - h; i <= height_now + h; i++) { if(binary_find(height, 0, n-1, i)) {
find_max_height(height, n, k, h, i * 2 - height_now, times_now + 1); } } } } bool binary_find(const int* height, int begin, int end, int target) { while(begin <= end) { int mid = (begin + end) / 2; if(height[mid] < target) begin = mid + 1; else if(height[mid] > target) end = mid - 1; else return true; } return false; }
|