解题思路:我关于这道题的解题思路很简单,就是暴力模拟。大家看我下面的代码应该能明白,只是感觉这样写能让更多的人能理解这道题。当然啦,这道题按道理说应该用二分做是最好的,时间复杂度就低很多了。由于本人太菜,就是一个国优菜鸡,所以就不提供二分的做法了。下面大家欣赏一下我的代码吧!(*^▽^*)
参考代码:
#include <bits/stdc++.h> using namespace std; #define maxn 210000 #define re register int a[maxn],b[maxn],n; long long int m,ans=0; //考虑每样数字对应最多写空白卡牌的情况 inline int fun2(int t){ if(b[t]!=0){ b[t]--; return 1; } else return 0; } inline int fun1(){ for(re int i=1;i<=n;i++){ if(a[i]!=0) a[i]--; // 用已有的卡牌去凑套牌(如果当前卡牌的数量不为0) else{ if(m==0) return 0; if(fun2(i)) m--; // 调用fun2函数,同时把已使用的一张空白牌减掉 else return 0; } } return 1; } int main(){ //以下三句话就是让cin,cout加速输入输出,增加时间效率 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for(re int i=1;i<=n;i++) cin >> a[i]; for(re int i=1;i<=n;i++) cin >> b[i]; while(true){ if(!fun1()) break; //如果返回值为0证明已经凑不出卡牌了,强制退出程序 ans++; } cout << ans << endl;//最后输出答案的值即可 }
0.0分
24 人评分