1. 程式人生 > >Educational Codeforces Round 53 (Rated for Div. 2) D - Berland Fair

Educational Codeforces Round 53 (Rated for Div. 2) D - Berland Fair

D. Berland Fair

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nn booths, arranged in a circle. The booths are numbered 11through nn clockwise with nn being adjacent to 11. The ii-th booths sells some candies for the price of aiai burles per item. Each booth has an unlimited supply of candies.

Polycarp has decided to spend at most TT burles at the fair. However, he has some plan in mind for his path across the booths:

  • at first, he visits booth number 11;
  • if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
  • then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).

Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.

Calculate the number of candies Polycarp will buy.

Input

The first line contains two integers nn and TT (1≤n≤2⋅1051≤n≤2⋅105, 1≤T≤10181≤T≤1018) — the number of booths at the fair and the initial amount of burles Polycarp has.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the price of the single candy at booth number ii.

Output

Print a single integer — the total number of candies Polycarp will buy.

Examples

input

Copy

3 38
5 2 5

output

Copy

10

input

Copy

5 21
2 4 100 2 6

output

Copy

6

 

題意:

你有T元錢,然後這裡有一個1-n的環,環上有n個點,每一個點都有一個商店,供應無限的糖果。

然後a[i]表示第i個商店的糖果的售價。對於你從i=1的商店開始走到i=2,i=3.....,順時針走到n,再走回到1繼續走。

每到達一個點,如果你口袋裡的錢T>=a[i],那麼你就買一個糖果,然後繼續走。這樣一直走到你的錢再也不能買任何一個糖果為止。問你最後你能買到的糖果數量是多少。

 

解析:

想這道題的時候腦子一直有點亂,被他的題意給繞進去了。所以當時沒有做出來,

後來想了一個早上,發現,其實每一次你都找現在存在的環裡面,字首和第一個大於T的點,然後把這個點

從環裡面刪掉,然後繼續找。每一次找完,你都判斷一下,當前環能不能用T 走一遍,能得話就看用T能走幾遍,

然後加上答案,對應T減少。這樣每一次,都一定會有一個點被刪除,然後找第一個大於T的用二分,字首和和環的數量

用樹狀陣列維護,所以總的複雜度是O(nlognlogn)

但是....我後來看了榜上的大佬,發現用暴力直接就可以過......

每一次都用T 走一遍n個點,記錄下花費s和買的糖果數量k,然後T/s*k加上答案,對應T減少,

然後繼續這麼做下去直到當前的T<糖果的最小售價就停止。

這個複雜度我算不來...我感覺是O(n*n)的複雜度,但是我比較了一下速度竟然比我的還快....(我用了140ms,大佬的用了70ms)

O(nlognlogn)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+100;
int n;
ll a[MAXN],c[MAXN];
int num[MAXN];

int lowbit(int x)
{
	return x&(-x);
}

void add_val(int x,ll y)
{
	int i;
	for(i=x;i<=n;i+=lowbit(i))
		c[i]+=y;
}

void add_num(int x,int y)
{
	int i;
	for(i=x;i<=n;i+=lowbit(i))
		num[i]+=y;
}

ll sum_val(int x)
{
	int i;
	ll s=0;
	for(i=x;i>0;i-=lowbit(i))
	{
		s+=c[i];
	}
	return s;
}

int sum_num(int x)
{
	int i;
	int s=0;
	for(i=x;i>0;i-=lowbit(i))
	{
		s+=num[i];
	}
	return s;
}

int bin_search(int l,int r,ll T)
{
    int ans=0;
    while(l<r)
    {
        int mid=(l+r)>>1;
        if(sum_val(mid)>T) ans=mid,r=mid;
        else l=mid+1;
    }
    if(sum_val(l)>T) ans=l;
    return ans;
}


int main()
{
    ll T;
    scanf("%d%lld",&n,&T);
    for(int i=1;i<=n;i++)
    {
        ll tmp;
        scanf("%lld",&tmp);
        a[i]=tmp;
        add_val(i,tmp);
        add_num(i,1);
    }

    ll ans=0;
    int plu=n;
    while(plu&&T)
    {
        int ind=bin_search(1,n,T);
        if(ind!=0){
            add_val(ind,-a[ind]);
            add_num(ind,-1);
        }
        ll p=sum_val(n);
        plu=sum_num(n);
        if(p<=T&&p)
        {
            ans+=(T/p)*plu;
            T=T%p;
        }
    }
    printf("%lld\n",ans);
    return 0;

}

大佬版的暴力

#include<bits/stdc++.h>
using namespace std;
int a[200005];
int main(){
	int n,mn=1e9;
	long long m,ans=0;
	scanf("%d %I64d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),mn=min(mn,a[i]);
	while(m>=mn){
		int s=0;
		long long k=0;
		for(int i=1;i<=n;i++) if(m>=a[i]) m-=a[i],s++,k+=a[i];
		ans+=s+(long long)m/k*s,m%=k;
	}
	printf("%lld",ans);
	return 0;
}