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

来源:互联网 发布:linux vi 删除字符 编辑:程序博客网 时间:2024/06/14 16:50

题目: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 1000000007using 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;}


0 0
原创粉丝点击