codeforces1073d Berland Fair 思维(暴力删除)

Posted mountaink

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces1073d Berland Fair 思维(暴力删除)相关的知识,希望对你有一定的参考价值。

题目传送门

  题目大意:一圈人围起来卖糖果,标号从1-n,每个位置的糖果都有自己的价格,一个人拿着钱从q开始走,能买则买,不能买则走到下一家,问最多能买多少件物品。

  思路:此题的关键是不能买则走到下一家,一旦走到下一家,我们会发现之前的这家以后无论转几圈我们都买不起,所以直接把这个店删掉就可以了。

  于是先将n当成周期,算出此时的sum,和原来的money比较,能买几个周期则买几个周期,然后遍历双向链表,不能买则删去,更新周期和sum,继续判断能不能买得起此时的周期,然后走到下一家店,直到剩下一家店。

  由于每家店最多被删去一次,所以时间复杂度是O(n)的,只要注意链表的一些细节处理就可以了。

技术分享图片
//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
inline ll read(){
    ll x=0,f=1;
    char ch=getchar();while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;}
const int maxn=2e5+10;
struct node{
    ll val;
    int pre,Next;
}a[maxn];
ll sum,money,ans;
int main(){
    int n;
    cin>>n>>money;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i].val);
        sum+=a[i].val;
        a[i].pre=i-1,a[i].Next=i+1;
    }
    a[1].pre=n,a[n].Next=1;
        if(money>=sum)
        {
            ans+=money/sum*n;
            money%=sum;
        }
        int i=1;
        while(a[i].Next!=i)
        {
            if(money>=a[i].val)
            {
                ans++;
                
                money-=a[i].val;
                if(money<=0)break;
                i=a[i].Next;
            }else{
                a[a[i].pre].Next=a[i].Next;
                a[a[i].Next].pre=a[i].pre;
                sum-=a[i].val;
                n--;
                if(money>=sum&&a[i].Next!=a[i].pre)
                {
                    ans+=money/sum*n;
                    money%=sum;
                }
                if(money<=0)break;
                i=a[i].Next;
            }
        }
        if(money>=sum)
        {
            ans+=money/sum*n;
            money%=sum;
        }
    printf("%lld
",ans);
}
View Code

 

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 (1n2?1051≤n≤2?105, 1T10181≤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 (1ai1091≤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.

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

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

 

 



以上是关于codeforces1073d Berland Fair 思维(暴力删除)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces1073d

Codeforces-B. Berland National Library(模拟)

codeforces 723D: Lakes in Berland

(KMPdp)Codeforces Educational Codeforces Round 21 G-Anthem of Berland

CodeForces 723D Lakes in Berland

模拟Codeforces 691A Fashion in Berland