zoukankan      html  css  js  c++  java
  • CodeForces462B

    Appleman and Card Game

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

    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

     1 //2016.8.2
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 
     6 using namespace std;
     7 
     8 int ch[30];
     9 
    10 bool cmp(int a, int b)
    11 {
    12     return a>b;
    13 }
    14 
    15 int min(int a, int b)
    16 {
    17     return a > b ? b : a;
    18 }
    19 
    20 int main()
    21 {
    22     int n, k;
    23     long long ans, x;
    24     char c;
    25     while(cin >> n >> k)
    26     {
    27         memset(ch, 0, sizeof(ch));
    28         getchar();
    29         ans = 0;
    30         for(int i = 0; i < n; i++)
    31         {
    32             c = getchar();
    33             ch[c-'A']++;
    34         }
    35         sort(ch, ch+26, cmp);
    36 
    37         for(int i = 0; k > 0;i++)
    38         {
    39             x = min(k, ch[i]);
    40             ans += x*x;
    41             k -= x;
    42         }
    43         cout << ans << endl;
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    wppay免登录付费查看隐藏内容/付费资源下载
    个人网站html5雪花飘落代码JS特效下载
    HTML5 audio 如何实现播放多个MP3音频
    网站html代码解析
    vue-webpack模板升级到webpack4
    npm安装cnpm
    单个div充满屏幕的CSS方法
    vue监听滚动事件-元素固定位置显示
    HTML5中判断横屏竖屏
    The META for Mobile terminal
  • 原文地址:https://www.cnblogs.com/Penn000/p/5758137.html
Copyright © 2011-2022 走看看