小蒟蒻-训练课-第六次——唐果

· · 个人记录

CF1073D Berland Fair

呜呜呜,差一步第二。
首先,建议加强数据,因为我隔壁的snowane纯暴力过了,我后面也试了,直接模拟就能过,分享下水AC代码(差一点水过) :::success[水]

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,t;
int a[200005];
signed main()
{
    cin >> n >> t;
    int sum=0,res=0;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
        sum+=a[i];
    }
    while(1)
    {
        int cnt=0;//我赛时位置定到外面了,就错了,要是写这里妥妥第二
        for(int i=1;i<=n;i++)
        {
            if(t>=a[i])
            {
                t-=a[i];
                res++;
                cnt++;
            }
        }
        if(!cnt) break;
    } 
    cout << res;
    return 0;
} 

::: 一开始,我就想,这么大的数据肯定有类似循环节的做法,不出所料,我写了,但是有点不对。我们发现,每次出现新增不能买的物品时,就会有一种新的购买方案,而这个购买方案会循环执行几次,知道再次出现新增不能买的物品时,又会有新的方案,那么,按这个数据,每有一种新的方案,我们就记录一次要多少钱,一次买了多少个,在用现在剩的钱数除以多少钱,就能算出这个方案要执行多少次,减去钱数,加上购买的物品数即可。(思路代码都很简单)

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,t;
int a[200005];
signed main()
{
    cin >> n >> t;
    int res=0;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    while(1)
    {
        int cnt=0,sum=0,tmp=t;
        for(int i=1;i<=n;i++)
        {
            if(tmp>=a[i])
            {
                tmp-=a[i];
                sum+=a[i];
                cnt++;
            }
        }
        if(cnt==0) break;
        int k=t/sum;
        res+=cnt*k;
        t-=k*sum;
    } 
    cout << res;
    return 0;
} 

时间复杂度:O(n·logT):
设当前方案买一圈的钱数为cost。