3 solutions
-
1
可以理解为把坑填平
填坑消耗的万能卡<=目标高度(4)时可以由经过平移变化,使每一层至多有一张万能卡
样例
4 5 2 3 4 5
样例 填坑后 平移变化 0 0 0 1 0 0 0 1 0 0 0 1 0 0 1 1 x x 1 1 x 1 1 1 0 1 1 1-->x 1 1 1--->x 1 1 1 1 1 1 1 1 1 1 1 x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#include <bits/stdc++.h> using namespace std; #define int long long int n, m; bool check(int x,vector<int>&a){ int diff = 0;//diff为消耗的万能卡,图中的x for (int i = 1; i <= n; i++) { if (a[i] < x)diff += x - a[i]; if (diff > m || diff > x){ return false; } } return true; } signed main() { cin >> n >> m; vector<int>num(n+1); for (int i = 1; i <= n; i++)cin >> num[i]; sort(num.begin() + 1, num.end()); int l = num[1], r = num[n]; //对目标高度二分 while (l < r) { int mid = l + (r - l + 1) / 2; if (check(mid, num))l = mid; else r = mid - 1; } cout << l; return 0; }
Information
- ID
- 7013
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 4
- Tags
- (None)
- # Submissions
- 174
- Accepted
- 11
- Uploaded By