• 8 4 第三场团队赛


    Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages.

    There are n stages available. The rocket must contain exactly k of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'.

    For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z' — 26 tons.

    Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.

    Input

    The first line of input contains two integers — n and k (1≤k≤n≤50) – the number of available stages and the number of stages to use in the rocket.The second line contains string s, which consists of exactly n lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.

    Output

    Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.

    Sample Input

    Input

    5 3
    xyabd

    Output

    29

    Input

    7 4
    problem

    Output

    34

    Input

    2 2
    ab

    Output

    -1

    Input

    12 1
    abaabbaaabbb

    Output

    1

    Hint

    In the first example, the following rockets satisfy the condition:

    • "adx" (weight is 1+4+24=29);
    • "ady" (weight is 1+4+25=30);
    • "bdx" (weight is 2+4+24=30);
    • "bdy" (weight is 2+4+25=31).

    Rocket "adx" has the minimal weight, so the answer is 29.

    In the second example, target rocket is "belo". Its weight is 2+5+12+15=34.

    In the third example, n=k=2, so the rocket must have both stages: 'a' and 'b'. This rocket doesn't satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1.

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <vector>
    using namespace std;
    const int MAX=1e6+10;
    typedef long long ll;
    int main()
    {
        ll n,m,i;
        char s[100],t;
        scanf("%lld%lld",&n,&m);
        getchar();
        gets(s);
        sort(s,s+n);
        ll ans=0;
        ans=s[0]-96;
        t=s[0];
        ll p=1;
        for(i=1;i<n;i++)
        {
            if(p==m)
                break;
            if(s[i]-t>1)
            {
                ans+=s[i]-96;
                t=s[i];
                p++;
            }
            if(p==m)
                break;
        }
        if(p<m)//注意不能用i>=n来判断 因为可能到最后符合条件
            printf("-1
    ");
        else
        printf("%lld
    ",ans);
        return 0;
    }
    

    Berland Football Cup starts really soon! Commentators from all over the world come to the event.

    Organizers have already built n commentary boxes. m regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.If n is not divisible by m, it is impossible to distribute the boxes to the delegations at the moment.Organizers can build a new commentary box paying aburles and demolish a commentary box paying b burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by m)?

    Input

    The only line contains four integer numbers n, m, a and b (1≤n,m≤1012, 1≤a,b≤100), where n is the initial number of the commentary boxes, m is the number of delegations to come, a is the fee to build a box and b is the fee to demolish a box.

    Output

    Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by m). It is allowed that the final number of the boxes is equal to 0.

    Sample Input

    Input

    9 7 3 8

    Output

    15
    

    Input

    2 7 3 7

    Output

    14
    

    Input

    30 6 17 19

    Output

    0
    

    Hint

    In the first example organizers can build 5

    boxes to make the total of 14 paying 3

    burles for the each of them.

    In the second example organizers can demolish 2

    boxes to make the total of 0 paying 7

    burles for the each of them.

    In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 5

    boxes.

    题意:有n个盒子  m个队伍  造一个盒子需要a元  拆一个盒子b元   规定不能剩下盒子 每个队伍得到的盒子相等  问最小花费

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <vector>
    using namespace std;
    const int MAX=1e6+10;
    typedef long long ll;
    int main()
    {
        ll n,m,a,b,i,ans1=0,ans2=0;
        scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
        ll p=n/m;
        ll q=n%m;
        if(q==0)
            printf("0
    ");
        else
        {
            ans1=q*b;
            ans2=a*(m-q);
            printf("%lld
    ",min(ans1,ans2));
        }
        return 0;
    }
    

    You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.

    You know that you have n bacteria in the Petri dish and size of the i-th bacteria is ai. Also you know intergalactic positive integer constant K.The i-th bacteria can swallow the j-th bacteria if and only if ai>aj and ai≤aj+K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria j if ai>aj and ai≤aj+K. The swallow operations go one after another.

    For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54]and K=1. The one of possible sequences of swallows is: [101,53,42,102,101−−−,55,54] → [101,53−−,42,102,55,54] → [101−−−,42,102,55,54] → [42,102,55,54−−] → [42,102,55]. In total there are 3 bacteria remained in the Petri dish.

    Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.

    Input

    The first line contains two space separated positive integers n

    and K (1≤n≤2⋅105, 1≤K≤106) — number of bacteria and intergalactic constant K.

    The second line contains n space separated integers a1,a2,…,an (1≤ai≤106) — sizes of bacteria you have.

    Output

    Print the only integer — minimal possible number of bacteria can remain.

    Sample Input

    Input

    7 1
    101 53 42 102 101 55 54

    Output

    3
    

    Input

    6 5
    20 15 10 15 20 25

    Output

    1
    

    Input

    7 1000000
    1 1 1 1 1 1 1

    Output

    7
    

    Hint

    The first example is clarified in the problem statement.

    In the second example an optimal possible sequence of swallows is: [20,15,10,15,20−−,25]→ [20,15,10,15−−,25] → [20,15,10−−,25] → [20,15−−,25] → [20−−,25] → [25].

    In the third example no bacteria can swallow any other bacteria.

    题意:当满足 ai>aj and ai≤aj+K时  aj删除  问最后剩下几个数

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <map>
    using namespace std;
    const int MAX=1e6+10;
    typedef long long ll;
    map<ll,ll> mp;
    ll a[1010000];
    int main()
    {
        ll n,k,i,m,x,sum=0,p=1;
        scanf("%lld%lld",&n,&k);
        for(i=0;i<n;i++)
            scanf("%lld",&a[i]);
        sort(a,a+n);
        for(i=1;i<n;i++)
        {
            if(a[i]==a[i-1])
                p++;
            else
            {
                if(a[i]<=a[i-1]+k)
                    sum+=p;
                p=1;
            }
        }
        printf("%lld
    ",n-sum);
        return 0;
    }
    

    Natasha is planning an expedition to Mars for n people. One of the important tasks is to provide food for each participant.The warehouse has m daily food packages. Each package has some food type ai.

    Each participant must eat exactly one food package each day. Due to extreme loads, each participant must eat the same food type throughout the expedition. Different participants may eat different (or the same) types of food.

    Formally, for each participant j Natasha should select his food type bj and each day j-th participant will eat one food package of type bj. The values bj for different participants may be different.

    What is the maximum possible number of days the expedition can last, following the requirements above?

    Input

    The first line contains two integers n and m (1≤n≤100, 1≤m≤100) — the number of the expedition participants and the number of the daily food packages available.

    The second line contains sequence of integers a1,a2,…,am(1≤ai≤100), where ai is the type of i-th food package.

    Output

    Print the single integer — the number of days the expedition can last. If it is not possible to plan the expedition for even one day, print 0.

    Sample Input

    Input

    4 10
    1 5 2 1 1 1 2 5 7 2

    Output

    2
    

    Input

    100 1
    1
    

    Output

    0
    

    Input

    2 5
    5 4 3 2 1

    Output

    1
    

    Input

    3 9
    42 42 42 42 42 42 42 42 42

    Output

    3
    

    Hint

    In the first example, Natasha can assign type 1 food to the first participant, the same type 1 to the second, type 5 to the third and type 2 to the fourth. In this case, the expedition can last for 2 days, since each participant can get two food packages of his food type (there will be used 4 packages of type 1, two packages of type 2 and two packages of type 5).

    In the second example, there are 100 participants and only 1 food package. In this case, the expedition can't last even 1 day

    题意:n个人 m个包裹 每个人只能吃一种类型的包裹 一个包裹可以多人吃 求最大生存天数

    思路:直接暴力天数

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <map>
    using namespace std;
    const int MAX=1e6+10;
    typedef long long ll;
    map<ll,ll> mp;
    ll a[1010],flag[1010]={0},b[1010];
    ll cmp(ll p,ll q)
    {
        return p>q;
    }
    int main()
    {
        ll n,m,i,ans=1;
        scanf("%lld%lld",&n,&m);
        for(i=0;i<m;i++)
        {
            scanf("%lld",&a[i]);
            flag[a[i]]++;
        }
        if(n>m)
        {
            printf("0
    ");
            return 0;
        }
        sort(flag,flag+101,cmp);
        ll t=0;
        for(i=0;i<=m;i++)
        {
            if(flag[i]!=0)
                b[t++]=flag[i];
        }
        while(1)
        {
            ll sum=0;
            for(i=0;i<t;i++)
            {
                sum+=b[i]/ans;
            }
            if(sum>=n)
                ans++;
            else
                break;
        }
        printf("%lld
    ",ans-1);
        return 0;
    }
    
  • 相关阅读:
    数据结构(一)线性表单链表试题
    虚拟研讨会:如何设计好的RESTful API?
    如何生成RestFul Api文档
    webstorm 10 设置文件的默认编码
    HAML学习
    Nodejs初阶之express
    RESTful API 简书
    解读Nodejs多核处理模块cluster
    Nginx做NodeJS应用负载均衡配置实例
    拿nodejs快速搭建简单Oauth认证和restful API server攻略
  • 原文地址:https://www.cnblogs.com/zcy19990813/p/9702718.html
Copyright © 2020-2023  润新知