codeforce #322C Developing Skills (优先队列)

本文介绍了一道CodeForces C题目的解题思路及实现方法。通过使用优先队列来选择最有价值提升的技能,以最大化玩家角色的总得分。此算法复杂度为O(k*logn),适用于技能提升策略问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意:玩游戏,你的角色有n项技能,各项技能的值为a1,a2...an(0 <= a <= 100),总分为 a1/10 + a2/10 + ... + an / 10(a/10向下取整)。现在你有k分可以用来增加技能值,问,能获得的最大总分是多少。不一定要用完k分。

思路:如果一项技能值ai % 10 最小,那么把分加给它效果最明显。所以用一个结构保存各项技能的 ai 和 ai % 10,每次加分取出 ai % 10 小的加分,用优先队列 ai % 10 小的排在前面。复杂度O(k*logn)。


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <queue>
#define INF 0x7fffffff
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct skill
{
    int x, l;
    bool operator < (const skill& rhs) const
    {
        return l > rhs.l;
    }
}cur;

priority_queue<skill> que;


int main()
{
    #ifdef LOCAL
    freopen("data.in", "r", stdin);
    #endif

    int n, k;
    while(scanf("%d%d", &n, &k) != EOF)
    {
        while(!que.empty()) que.pop();
        int x, ans = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &x);
            ans += x / 10;
            cur.x = x; cur.l = 10 - x % 10;
            if(x < 100) que.push(cur);
        }
        while(k && !que.empty())
        {
            cur = que.top();
            que.pop();
            //printf("cur:%d %d k=%d\n", cur.x, cur.l, k);
            if(cur.l <= k)
            {
                ans++; k -= cur.l;
                cur.x += cur.l; cur.l = 10;
                if(cur.x < 100)
                    que.push(cur);
            }
            else
                break;
        }
        printf("%d\n", ans);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值