跳到正文

题解

题解:P13463 [GCJ 2008 #1C] Text Messaging Outrage

Peruere_Arlecchino约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年7月27日
收录
2025年12月12日
更新
2026年10月13日
篇幅
约 1 分钟
题目
P13463

版本

  1. v1当前

解决这个问题,可以通过将字母按出现频率从高到低排序,并再依次分配到按键上,使第 ii 个字母位于第 (i(i% K) 个按键的第 (i/K+1)(i/K + 1) 个位置,最后要计算每个字母的频率与对应按键次数的乘积之和,就可以得到输入消息所需的最少按键次数了。 ##code:

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int N;
    cin >> N;
    for (int v = 1; v <= N; ++v) {
        int P, K, L;
        cin >> P >> K >> L;
        vector<int> c(L);
        for (int i = 0; i < L; ++i) {
            cin >> c[i];
        }
        sort(c.rbegin(), c.rend());
        long long g = 0;
        int x;
        for (x = 0; x < L; ++x) {
            int o = (x / K) + 1;
            g += (long long)c[x] * o;
        }
        cout << "Case #" << v << ": " << g << endl;
    }
    return 0;
}

求赞求过。

原文