zoukankan      html  css  js  c++  java
  • NUC_TeamTEST_B(贪心)

    B - B
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.

    Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?

    Input

    The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

    Output

    Print a single integer – the answer to the problem.

    Sample Input

    Input
    15 10
    DZFDFZDFDDDDDDF
    Output
    82
    Input
    6 4
    YJSNPI
    Output
    4

    Hint

    In the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.

     中间超int啊,不难的贪心。CodeForces,DIV2的A题
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int max_size = 100000+10;
    
    int main()
    {
        int n, k;
        char str[max_size];
        long long  arry[26];
        while(scanf("%d %d", &n, &k) == 2)
        {
            getchar();
            memset(arry, 0, sizeof(arry));
            scanf("%s", str);
            int len = strlen(str);
            int i;
            for(i = 0; i < len; ++i)
            {
                arry[str[i]-'A']++;
            }
            i = 25;
            sort(arry, arry+26);
            //printf("%d
    ", arry[25]);
            long long sum = 0;
            long long now = k;
            do{
                if(now > arry[i])
                {
                    sum += arry[i] * arry[i];
                    now -= arry[i];
                    //printf("%d
    ", sum);
                    i--;
                }else if(now <= arry[i]){
                    sum += now*now;          ///这个比较坑,没考虑到超int
                    break;
                }
            }while(now != 0);
            cout << sum << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    华为移动HG8546M光猫路由器通过lan口再连接路由器
    Windows下使用Dev C++ 编写dll与使用dll(二)C++项目下的dll
    Windows下使用Dev C++ 编写dll与使用dll(一)C项目下的dll
    易语言之dll文件的编写与引入
    易语言之编写模块与引入模块
    Element ui 分页记录选中框
    MUI poppicker.js 增加搜索框
    element el-date-picker 去除自带的T格式
    element el-input小数保留两位小数,整数字符串去空格
    nginx vue三级目录配置
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4052489.html
Copyright © 2011-2022 走看看