2025“钉耙编程”中国大学生算法设计暑期联赛(5)——"合理"避税 · znzryb
## 思路讲解
核心就是知道可以发多少个月,就可以知道总共可以发多少个人次,一个人最多可以发多少钱。
```cpp
auto check=[&](ll mid){
// 我们一共可以给mid*P个人次发钱。
ll chance=mid*P;
vpll rem;
ll ans=0;
FOR(i,1,N){
ll a=A[i-1];
ll ci=min({a/K,mid,chance});
chance-=ci;
ans+=ci*K;
ll r=a-K*ci;
if(r>0) rem.EB(r,mid-ci);
}
if(chance<=0){
if(ans>=M){
return true;
}else{
return false;
}
}
sort(all(rem),greater<>());
for(auto [v,ci]:rem){
if(chance==0) break;
if(ci>0) ans+=v,chance--;
}
if(ans>=M){
return true;
}else{
return false;
}
};
```
## AC代码
[https://acm.hdu.edu.cn/contest/view-code?cid=1176&rid=8395](https://acm.hdu.edu.cn/contest/view-code?cid=1176&rid=8395)
::: 源代码
```cpp
// by Gospel_rock
// from template_cpp
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define pb push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define lson(var) (var<<1)
#define rson(var) ((var<<1)+1)
#define fsp(x) fixed<<setprecision(x)
using namespace std;
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
// using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using vdb = vector<DB>;using vc=vector<char>;
using tll = tuple<ll,ll,ll>;
using vpll = vector<pll>;using vtll = vector<tll>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+9; // 1e18+9也是素数
constexpr ll mod=(ll)1e9+7; // 998244353;
constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT,P;
inline void __();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>lT;
while(lT--)
__();
return 0;
}
/*
*/
inline void __(){
cin>>N>>M>>K>>P;
vll A(N);
FOR(i,1,N){
cin>>A[i-1];
}
sort(all(A),greater<>());
auto check=[&](ll mid){
// 我们一共可以给mid*P个人次发钱。
ll chance=mid*P;
vpll rem;
ll ans=0;
FOR(i,1,N){
ll a=A[i-1];
ll ci=min({a/K,mid,chance});
chance-=ci;
ans+=ci*K;
ll r=a-K*ci;
if(r>0) rem.EB(r,mid-ci);
}
if(chance<=0){
if(ans>=M){
return true;
}else{
return false;
}
}
sort(all(rem),greater<>());
for(auto [v,ci]:rem){
if(chance==0) break;
if(ci>0) ans+=v,chance--;
}
if(ans>=M){
return true;
}else{
return false;
}
};
ll low=1,hig=M/K+(ll)1e4;
while(low<hig){
ll mid=low+hig>>1;
if(check(mid)){
hig=mid;
}else{
low=mid+1;
}
}
cout<<low<<"\n";
}
/*
AC
https://acm.hdu.edu.cn/contest/view-code?cid=1176&rid=8395
*/
```
:::
## 心路历程(WA,TLE,MLE……)