CF2208B Cyclists
思路
首先考虑若要选的那个胜利条件最开始就在最后面,该怎么选。
显然是找前面 $k-1$ 个花费最大的卡牌,将它们保留,然后将其它的卡牌挪到最后面,最后胜利条件就会位于第 $k$ 个位置,直接挪动它。
此时就回到了跟原情况等价的一种局面,可以花费一定的代价稳定地获取一次胜利条件,用剩余的花费除以一定的代价得到答案。
那如果胜利条件最开始不在最后面呢?
那就想办法将它挪动到最后面。
若开始时它在前 $k$ 个,直接挪动。
否则,留下在它前面且最大的 $k-1$ 个,挪动其它卡牌,再挪动胜利条件。
最后所有情况都到了它开始就在最后的局面,按照前面提到的方法计算次数即可。
代码
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 53 54 55 56 57 58 59 60 61 62 63
| #include<bits/stdc++.h> #define fir first #define sec second #define int long long #define pii pair<int,int> #define fep(i,s,e) for(int i=s;i<e;i++) #define pef(i,s,e) for(int i=s;i>e;i--) #define rep(i,s,e) for(int i=s;i<=e;i++) #define per(i,s,e) for(int i=s;i>=e;i--) namespace FastIO{ template<typename T>inline void read(T &x){ x=0;int f=1;char c=getchar(); for(;!isdigit(c);c=getchar())if(c=='-')f=-1; for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);x*=f; } template<typename T,typename...Args> inline void read(T &x,Args&...args){ read(x); read(args...); } template<typename T>void print(T x){ if(x<0)x=-x,putchar('-'); if(x>9)print(x/10); putchar((x%10)^48); } } using namespace std; using namespace FastIO; const int N=5005; int T,n,k,p,m,val,a[N]; signed main(){ read(T); while(T--){ int ans=0; read(n,k,p,m); rep(i,1,n){ if(i<p)read(a[i]); if(i==p)read(val); if(i>p)read(a[i-1]); } if(p<=k){ if(m>=val)ans++,m-=val; } else{ sort(a+1,a+p); fep(i,1,p){ if(i<=p-k){ m-=a[i]; } } if(m>=val)ans++,m-=val; } sort(a+1,a+n); int tot=0; rep(i,1,n-k){ tot+=a[i]; } tot+=val; ans+=m/tot; print(ans); puts(""); } }
|