CF322 C 优先队列+贪心

题目连接

http://codeforces.com/contest/581/problem/C

Description

Petya loves computer games. Finally a game that he’s been waiting for so long came out!

The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.

At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya’s character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero’s skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.

Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.

Input

The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya’s disposal.

The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.

Output

The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.

Sample Input

2 4
7 9

Sample Output

2

题意

给出人物属性,一共K点属性,保证最后求得值最大

题解

优先队列+贪心,优先把个位数最大的数加到10
最后的min(n * 10,ans+k/10),因为前面只取了个位数,而加的时候最多加到10,k就可以剩下很多,so ans+k/10,但是最大的数又不能超过100,此处就和10 * n关联,如果ans+k/10是小于n*10的则等同与说明,以前的操作中最小的数是小于100的即k还不够大,10 * n表示把所有的数都加到100时ans的值,这样保证了不会把数加到爆100.(其实可以等效地想,把一个数加到爆100也可以,只要小于100 * n就没问题)

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
priority_queue< int, vector<int>, less<int> > que;
int main(int argc, char const *argv[])
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
    int ans=0;
    for (int i = 0; i < n; ++i)
    {
        int t;
        cin>>t;
        ans+=t/10;
        t=t%10;
        que.push(t);
    }
    for (int i = 0; i < n; ++i)
    {
        int t=que.top();
        que.pop();
        if (10-t<k)
        {
             ans+=1;
             k-=10-t;
        }
        else{
            ans+=(t+k)/10;
        }

    }
    printf("%d\n",min(10*n,ans+k/10) );
}
    return 0;

}

自己TLE的代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
priority_queue< P, vector<P>, less<P> > Q;
int main(int argc, char const *argv[])
{
    int n,k;
    cin>>n>>k;
    for (int i = 0; i < n; ++i)
    {
        P t;
        cin>>t.second;
        t.first=t.second%10;
        Q.push(t);
    }
     P p=Q.top();
     int ans=0;
    while(k!=0&&p.second!=100){
        P q=Q.top();
        Q.pop();
        q.second++;
        k--;
        q.first=q.second%10;
        Q.push(q);
        p=Q.top();
        if(p.second==100){
            Q.pop();
            ans+=10;
            n--;
            p=Q.top();
        } 
    }
    for (int i = 0; i < n; ++i)
    {
        P q=Q.top();
        Q.pop();
        ans+=q.second/10;
    }
    printf("%d\n",ans );
    return 0;
}

Get

  • 想法设法把优先队列和pair结合在了一起,虽然爆时间了,但还是学了很多。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值