codeforces-1037D Berland Fair

题目链接:点我点我w~

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

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

  • at first, he visits booth number 1;
  • 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 n and T () — the number of booths at the fair and the initial amount of burles Polycarp has.

The second line contains n integers () — the price of the single candy at booth number i.

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

 

  1. Booth 1, buys no candy, not enough money;
  2. Booth , buys candy for 2.

No candy can be bought later. The total number of candies bought is 10.

In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount.

题意:转圈买糖果,买的起就买,买不起就跳过,一直到啥都买不起,求能买到的糖数。

直接模拟会TLE:

代码如下:

#include<iostream>
#include<climits>
using namespace std;
typedef long long ll;
ll a[300000];
int main()
{
  int n;
    ll money;
    scanf("%d%lld",&n,&money);
  scanf("%lld",&a[1]);
  ll minn=a[1];
    for(int i=2;i<=n;i++) 
    {
        scanf("%lld",&a[i]);
        minn=min(minn,a[i]);
    }

    ll ans=0;
    while(money>=minn)
    {
        for(int i=1;i<=n;i++) 
         if(money>=a[i])
         {
             money-=a[i];
       ans++;
         }
    }
    printf("%lld\n",ans);
     return 0;

}

换个思路:有多个圈数得到的糖果数量是一样的,取模即可优化(PS一定要完成本圈之后进行取模)

#include<iostream>
#include<climits>
using namespace std;
typedef long long ll;
ll a[300000];
int main()
{
  int n;
    ll money;
    scanf("%d%lld",&n,&money);
  scanf("%lld",&a[1]);
  ll minn=a[1];
    for(int i=2;i<=n;i++) 
    {
        scanf("%lld",&a[i]);
        minn=min(minn,a[i]);
    }

    ll ans=0;
    while(money>=minn)
    {
        for(int i=1;i<=n;i++) 
         if(money>=a[i])
         {
             money-=a[i];
       ans++;
         }
    }
    printf("%lld\n",ans);
     return 0;

}

 

 

发表评论

电子邮件地址不会被公开。