CF 2018-10-25 D. Berland Fair

本文介绍了一种在有限预算下,于环形排列的糖果商店中购买最大数量糖果的算法策略。通过使用树状数组或Set数据结构,文章详细解释了如何高效地选择购买顺序,确保在预算范围内买到最多的糖果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 11 through 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

Note

Let's consider the first example. Here are Polycarp's moves until he runs out of money:

  1. Booth 11 , buys candy for 55 , T=33T=33 ;
  2. Booth 22 , buys candy for 22 , T=31T=31 ;
  3. Booth 33 , buys candy for 55 , T=26T=26 ;
  4. Booth 11 , buys candy for 55 , T=21T=21 ;
  5. Booth 22 , buys candy for 22 , T=19T=19 ;
  6. Booth 33 , buys candy for 55 , T=14T=14 ;
  7. Booth 11 , buys candy for 55 , T=9T=9 ;
  8. Booth 22 , buys candy for 22 , T=7T=7 ;
  9. Booth 33 , buys candy for 55 , T=2T=2 ;
  10. Booth 11 , buys no candy, not enough money;
  11. Booth 22 , buys candy for 22 , T=0T=0 .

 

题目大意:糖果商店买糖果(顺时针环形)给n,T分别表示 价值个数,金钱总额,买得起的店必须一定会买,买不起就到下一个,直到所有的都买不起,问最多可以买多少糖果。

 

这题可以用(BIT)树状数组,也可以通过Set 做。

这里熟悉一下set的用法。

注意这里 记录 前点、后点的方法,pre,nex。这样的好处就是每次O(1)时间选取下一个元素进行判断。

 

code:

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i = j; i < k; ++i)
using namespace std;
typedef long long LL;
const int N = 2e5+5;
int n;
LL T,ans,num,sum;
int pre[N],nex[N],a[N];
LL mn;
struct node{
    int id,v;
    bool operator<(const node o)const{
        return v < o.v;
    }
};
set<node> S;

/*
    Set::erase
        (1)
        iterator  erase (const_iterator position);
        (2)
        size_type erase (const value_type& val);
        (3)
        iterator  erase (const_iterator first, const_iterator last);
    Set::find
        const_iterator find (const value_type& val) const;
        iterator       find (const value_type& val);
    Set
    C++ 11 结构体赋值方式  (node){v1,v2}
    set 用迭代器取元素值  (*S.begin()).v
*/

int main()
    {
        scanf("%d%I64d",&n,&T);
        rep(i,1,n+1){
            scanf("%d",&a[i]);
            nex[i] = i%n + 1;
            S.insert((node){i,a[i]});
            sum += a[i];
        }
        rep(i,1,n+1) pre[nex[i]] = i;
        int cur = 1;
        //printf("%d\n",(*S.begin()).v);
        mn = (*S.begin()).v; // 最小值
        ans = 0;
        num = n;
        while(T >= mn){
            if(T >= sum){
                ans += T/sum*num,T %= sum;
            }
            else if(T >= a[cur]){
                T -= a[cur],++ans,cur = nex[cur];
            }
            else{  //删除 cur 对应的元素 并调整cur 前、后元素的 后、前元素位置
                --num;
                sum -= a[cur];
                pre[nex[cur]] = pre[cur];
                nex[pre[cur]] = nex[cur];
                S.erase((node){cur,a[cur]});
                cur = nex[cur];
            }
        }
        printf("%I64d\n",ans);

        return 0;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值