题目 :https://codeforces.com/problemset/problem/1073/D
思路:把价格小于当前钱的糖果放入队列里,并记录队列里所有糖果的价格和
每次变量前:
- 1.先看当前钱够买多少轮队列里的糖果
- 2 . 遍历队列,并把仍然小于当前钱数的糖果再次加入队列
代码:
#include<cstdio>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
int main()
{
int n;
queue <int> q;
LL ans,cut = 0,sum = 0;
scanf("%d %lld",&n,&ans);
for(int i = 0; i < n; i++){
int a; scanf("%d",&a);
if(a <= ans) q.push(a),sum += a;
}
while(ans){
int nl = q.size();
bool ok = true;
if(ans >= sum && sum){
LL o = ans / sum;
cut += nl * o;
ans -= sum * o;
}
sum = 0;
while(nl--){
int p = q.front();
q.pop();
if(p <= ans) ans -= p,cut++;
if(p <= ans) q.push(p),sum += p,ok = false;
}
if(ok) break;
}
printf("%lld\n",cut);
return 0;
}