【Codeforces 1073D】Berland Fair

【链接】 我是链接,点我呀:)
【题意】


题意

【题解】


我们可以从左到右枚举一轮。
定义一个cost表示这一轮花费的钱数
如果cost+a[i]<=T那么就可以买它,并且买下它(模拟题目要求)
那么我们累计这一轮可以买下的数量cnt
则显然我们不用每一层都这么枚举
直接累加答案就好
ans = ans + T/cost cnt;
然后令T=T%cost就好了
因为newT = beforeT%cost
所以newT = beforeT-x
cost <cost
cost(1+x)>beforeT
cost>beforeT/(1+x)
xcost>xbeforeT/(1+x)
xcost>beforeT/(1/x+1)
x>=1
显然x越大,右边越来越大
则x
cost>beforeT/2
则newT<=beforeT/2
那么T的值每次都会除2.
所以复杂度就是O(n*log2T)的了

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5;
const long long M = 15e6;

int n;
ll T,ans = 0;
int a[N+10];

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n >> T;
    for (int i = 1;i <= n;i++) cin >> a[i];
    while (1){
        ll sum = 0;
        int cnt = 0;
        for (int i = 1;i <= n;i++){
            if (sum+a[i]>T){
                continue;
            }else{
                cnt++;
                sum = sum + a[i];
            }
        }
        if (cnt==0) break;
        ans = ans + T/sum*cnt;
        T = T%sum;
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值