WY76. 字母卡片
描述
输入描述
输入包含两行,第一行含两个整数n,k(0<k<=n<=1,000,000)输出描述
输出仅包含一行,输出尽可能高的分数示例1
输入:
15 10 DZFDFZDFDDDDDDF
输出:
82
C++ 解法, 执行用时: 8ms, 内存消耗: 1528KB, 提交时间: 2019-11-21
#include <bits/stdc++.h> #define LL long long #define pb push_back #define endl "\n" #define FIN freopen("1.in","r",stdin) #define mem(x,v) memset(x,v,sizeof(x)) #define rep(i,a,n) for(int i=a;i<n;i++) #define per(i,a,n) for(int i=n-1;i>=a;i--) using namespace std; const int N = 26; LL n,k,ans; string s; LL ch[N]; int main() { ios::sync_with_stdio(false); while(cin>>n>>k>>s){ ans = 0; mem(ch,0); rep(i,0,n){ ch[s[i]-'A']++; } sort(ch,ch+N,greater<LL>()); LL r = 0; while(k){ if(k>ch[r]){ ans+=ch[r]*ch[r]; k-=ch[r]; r++; }else{ ans+=k*k; k = 0; } } cout<<ans<<endl; } return 0; }
C++ 解法, 执行用时: 8ms, 内存消耗: 3640KB, 提交时间: 2020-07-28
#include <bits/stdc++.h> #define LL long long #define pb push_back #define endl "\n" #define FIN freopen("1.in","r",stdin) #define mem(x,v) memset(x,v,sizeof(x)) #define rep(i,a,n) for(int i=a;i<n;i++) #define per(i,a,n) for(int i=n-1;i>=a;i--) using namespace std; const int N = 26; LL n,k,ans; string s; LL ch[N]; int main() { ios::sync_with_stdio(false); while(cin>>n>>k>>s){ ans = 0; mem(ch,0); rep(i,0,n){ ch[s[i]-'A']++; } sort(ch,ch+N,greater<LL>()); LL r = 0; while(k){ if(k>ch[r]){ ans+=ch[r]*ch[r]; k-=ch[r]; r++; }else{ ans+=k*k; k = 0; } } cout<<ans<<endl; } return 0; }