310 字
2 分钟
CF2024A-Profitable-Interest-Rate-题解

CF2024A#

题意简述#

Alice有 aa 枚硬币,她想要将硬币存入银行,但是开立存款的最低金额为 bb,如果金额不足,可以花费 xx 个硬币使得存款的最低金额减少 2x2x。求存入银行的最大硬币数量。

题意分析#

显然,当 bab\le a 时,可以直接将 aa 枚硬币全部存入,所以直接输出 aa 即可。

反之,当 b>ab>a 时,我们需要花费硬币降低最低金额。不难发现,题目要求最大值,且同时符合单调性,可以使用二分答案来直接计算。答案区间表示存入银行的硬币数量 ansans,通过check判断 ansansb(aans)×2b-(a-ans)\times 2 的大小关系,如果 ansb(aans)×2ans\ge b-(a-ans)\times 2check判断为true,将左边界更新即可。

Code#

#pragma GCC optimize(1, 2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ONLINE_JUDGE
#define MULTI_CASES
#define endl '\n'
const int MaxN = 2e5+100;
const int INF = 1e9;
const int mod=212370440130137957ll;
int T=1, N, M;
int a[MaxN];
bool check(int x)
{
if(x>=M-(N-x)*2){
return 1;
}
return 0;
}
inline void Solve()
{
cin>>N>>M;
if(N>=M){
cout<<N<<endl;
}
else{
int l=0,r=N;
while(l<r){
int mid=(l+r+1)>>1;
if(check(mid)){
l=mid;
}
else{
r=mid-1;
}
}
cout<<l<<endl;
}
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
#ifdef MULTI_CASES
cin >> T;
while (T--)
#endif
Solve();
//fclose(stdin);
//fclose(stdout);
return 0;
}
CF2024A-Profitable-Interest-Rate-题解
https://blog.introl.top/posts/cf2024a-profitable-interest-rate-题解/
作者
Introl
发布于
2024-10-21
许可协议
CC BY-NC-SA 4.0