HRBU_20211006训练

A - Nearest Interesting Number
题意:
如果一个数字的各数位和可以被4整除,则这个数是有趣的。给定的数字aa找到最近的更大或相等的感兴趣的数字
题解
暴力就行,从a,开始,判断数位和是否整除4,是输出,否a++;

#include<iostream>
using namespace std;
int main()
{
    int a,s=0,d;
    cin>>a;
    for(int i=a;i<100005;++i)
    {s=0;
    d=i;
        while(d!=0)
        {
           s+=d%10;
           d=d/10;
        }
        if(s%4==0)
        {
            cout<<i<<endl;
            break;
        }
    }

}

B - Equalize Prices
题意
商店里有n种产品。第i个产品的价格是ai。店主想使所有产品的价格相同。相同价格为B,B要满足 | ai−B |≤k,输出满足条件的B的最大值,找不到-1.

题解
找到最小值a,最大值b,有2种情况1,b-a>2k 输出-1 2,输出a+k

#include<iostream>
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n,a[100005],k,s,d;
        cin>>n>>k;
        for(int i=0;i<n;++i)
        {cin>>a[i];
            if(i==0)
            {
                s=a[i];
                d=a[i];
            }
            else {
                if(s<a[i])
                    s=a[i];
                if(d>a[i])
                    d=a[i];
            }
        }
        if(s-d>2*k)
            cout<<-1<<endl;
        else cout<<d+k<<endl;
    }
}

C - Computer Game
题意(真的很难理解)
电量大于a,直接玩,大于b边充电边玩,小于等于a,b无法完成,给n,k,a,b,总电量,回合数
问是否能完成,否输出-1,是输出直接玩的最大回合数
题解
首先n/b<=k 输出-1, n/a>k输出k,中间的 用b完成k后看剩余电量能支持几回合

#include<iostream>
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n,k,a,b,s=0,f;
        cin>>n>>k>>a>>b;
        if(n/b<k||n/b==k&&n%b==0)
            cout<<-1<<endl;
        else if(n/a==k&&n%a!=0||n/a>k)
        {
            cout<<k<<endl;
        }else{

        n=n-b*k;
        s=a-b;
        f=0;
        int d=0;
        if(n%s==0)
        {
            f=n/s-1;
        }
        else f=n/s;
        cout<<f<<endl;
        }
    }
}

D - Candy Box (easy version)
This problem is actually a subproblem of problem G from the same contest.
There are nn candies in a candy box. The type of the ii-th candy is a_ia
i (1 \le a_i \le n1≤a i ≤n).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 11 and two candies of type 22 is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
You have to answer qq independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains one integer qq (1 \le q \le 2 \cdot 10^51≤q≤2⋅10
5
) — the number of queries. Each query is represented by two lines.
The first line of each query contains one integer nn (1 \le n \le 2 \cdot 10^51≤n≤2⋅10 5) — the number of candies.
The second line of each query contains nn integers a_1, a_2, \dots, a_na
1 ,a 2​ ,…,a n(1 \le a_i \le n1≤ai ≤n), where a_ia i is the type of the ii-th candy in the box.
It is guaranteed that the sum of nn over all queries does not exceed 2 \cdot 10^52⋅10
5
Output
For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.

Example
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
Note
In the first query, you can prepare a gift with two candies of type 88 and one candy of type 55, totalling to 33 candies.

Note that this is not the only possible solution — taking two candies of type 44 and one candy of type 66 is also valid.
题意
给一个q,q次查询,给一个n,盒子里糖果数量为n,n个数,ai,第i个糖果的类型。在已经拥有的糖果中,找出可以组成一个礼物的最大数量。必须满足下列限制,同一个礼物中每种糖果出现的 次数不能相同。
题解
简单的贪心,统计每个糖果出现的次数,然后题目就变成了,在这一堆数中找满足条件的最大值,条件为每个数字只能出现一次.这样的话从大到小排序,然后加起来,如果等于或大于上一个,就减一.

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       long long  n,a[200005],s=0,m;
       cin>>n;
          for(int i=0; i<=n; i++)
            a[i] = 0;
       for(int i=1;i<=n;i++)
       {
           int c;
           scanf("%d",&c);
           a[c]++;

       }
       sort(a+1,a+n+1,greater<int>());

      s +=a[1];
         m = a[1];
        for(int i=2; i<=n; i++)
        {
            if(a[i]==0||m==0)
                break;
            if(a[i]>=m)
            {
                if(m==0)
                    break;
                s +=m-1;
               m = m-1;
            }
            else
            {
                s +=a[i];
               m = a[i];
            }

        }
printf("%lld\n",s);
       }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值