zoukankan      html  css  js  c++  java
  • CodeForce edu round 53 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 (1n21051≤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.

    之前用线段树做的。超时了很难受。因为没有有效的处理无效的点。之后借鉴了网上的答案。改成链表去做。链表有些不熟悉了。比如删除结点那里都搞错了。

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #define ll long long
    //#define local
    
    using namespace std;
    
    const int MOD = 1e9+7;
    const int inf = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    const int maxn = 2e5+10;
    
    ll a[maxn];
    ll t;
    int n;
    int nex[maxn];
    int pre[maxn];
    
    int main() {
    #ifdef local
        if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!
    ");
    #endif
        
        scanf("%d%lld", &n , &t);
        int num = 1;
        for (int i = 0; i < n; ++i) {
            ll tmp;
            scanf("%lld", &tmp);
            if (tmp > t) continue;
            a[num++] = tmp;
        }
        n = num-1;
        if (n == 0) {
            printf("0
    ");
            return 0;
        }
        for (int i = 0; i < n; ++i) {
            nex[i] = i+1;
        }
        nex[n] = -1;
        for (int i = 1; i <= n; ++i)
            pre[i] = i-1;
        ll cnt = 0;
        while (nex[0] != -1) {
            ll sum = 0;
            int num = 0;
            ll rem = t;
            for (int i = nex[0]; i != -1; i = nex[i]) {
                if (a[i] <= rem) {
                    sum += a[i];
                    num++;
                    rem -= a[i];
                } else {
                    nex[pre[i]] = nex[i];
                    pre[nex[i]] = pre[i];
                }
            }
            if (sum == 0) break;
            cnt += t/sum*num;
            t %= sum;
        }
        printf("%lld
    ", cnt);
    #ifdef local
        fclose(stdin);
    #endif
        return 0;
    }

     

  • 相关阅读:
    11 Apr 18 类与对象 对象的使用 一切皆对象
    10 Apr 18 项目构架 hashlib模块 subprocess模块 configparser模块 面向对象的编程 类与对象
    9 Apr 18 shelve模块 xml模块 re模块
    8 Apr 18 包的使用 常用模块
    定义一个Student类,包含名字一个数据成员,定义无参构造函数、有参构造函数、拷贝构造函数、析构函数及对于名字的封装函数,在main函数中实例化Student对象,并访问相关函数,观察运行结果。
    皮马印第安人糖尿病数据集免费下载分享
    机器学习之模型训练(二)皮马印第安人糖尿病数据集
    anaconda的虚拟环境sklearn中如何安装pandas
    人工智能之模型评估之参数选择
    人工智能之模型评估之数据分离
  • 原文地址:https://www.cnblogs.com/lecoz/p/9860966.html
Copyright © 2011-2022 走看看