视频讲解~~
Codeforces Round 961 (Div. 2) A - C_哔哩哔哩_bilibili
看视频噢
#include<bits/stdc++.h>
using namespace std;
int solve(int n, int k)
{
if (k == 0)
return 0;
if (n >= k)
return 1;
k -= n;
int cnt = 1;
for (int i = n - 1; i >= 1; i--)
{
if (i >= k)
return cnt + 1;
cnt++, k -= i;
if (i >= k)
return cnt + 1;
cnt++, k -= i;
}
return -1;
}
int main()
{
int T;
cin >> T;
while (T--)
{
int n, k;
cin >> n >> k;
cout << solve(n, k) << endl;
}
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int T;
cin >> T;
while (T--)
{
int n, m;
cin >> n >> m;
vector<int> a(n);
for (auto &i : a)
cin >> i;
sort(a.begin(), a.end());
int sum = 0, ans = 0;
for (int l = 0, r = 0; r < n; r++)
{
sum += a[r];
while (l < r && !(sum <= m && a[r] - a[l] <= 1))
sum -= a[l], l++;
if (sum <= m && a[r] - a[l] <= 1)
ans = max(ans, sum);
}
cout << ans << endl;
}
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int T;
cin >> T;
while (T--)
{
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n);
for (auto &i : a)
cin >> i.first;
for (auto &i : a)
cin >> i.second;
sort(a.begin(), a.end());
int ans = 0;
for (auto [i, j] : a)
ans = max(ans, i * min(j, (m / i)));
for (int i = 1; i < n; i++)
{
if (a[i - 1].first + 1 < a[i].first)
continue;
int cur = m;
int small = min(a[i - 1].second, cur / a[i - 1].first);
cur -= small * a[i - 1].first;
int big = min(a[i].second, cur / a[i].first);
cur -= big * a[i].first;
int temp = min(small, a[i].second - big);
int add = min(cur, temp);
cur -= add;
ans = max(ans, m - cur);
}
cout << ans << endl;
}
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
bool ok(vector<int> &a)
{
int n = a.size();
for (int i = 1; i < n; i++)
if (a[i] == 1 && a[i - 1] > 1)
return false;
return true;
}
int cal(int a, int b, int c)
{
if (a == 1 || c == 1)
return 0;
double it = log2(a) / log2(c);
int x = ceil(b + log2(it));
return max(0LL, x);
}
signed main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
vector<int> a(n), b(n, 0);
for (auto &i : a)
cin >> i;
if (!ok(a))
{
cout << -1 << endl;
continue;
}
int cnt = 0;
for (int i = 1; i < n; i++)
b[i] = cal(a[i - 1], b[i - 1], a[i]), cnt += b[i];
cout << cnt << endl;
}
}