CF447B DZY Loves Strings 题解
Content
有一个长度为 \(n\) 的仅含小写字母的字符串 \(s\) 以及 26 个英文小写字母的价值 \(W_\texttt{a},W_\texttt{b},...,W_\texttt{z}\),请求出在后面插入 \(k\) 个小写字母后所能够获得的最大价值。
对于一个长度为 \(x\) 的字符串 \(s'\),它的价值为 \(\sum\limits_{i=1}^x i\times W_{s'_i}\)。
数据范围:\(1\leqslant n\leqslant 1000,0\leqslant k\leqslant 1000,0\leqslant W_{\texttt{a}\sim\texttt{z}}\leqslant 1000\)。
Solution
没插入前的 \(s\) 的价值很好算,直接按照题目要求加就好了。
至于插入后的就要看字母价值中最大的那个了,因为根据样例来看,这道题是允许你重复加入同样的小写字母,所以我们找到最大的字母价值然后再插入 \(k\) 次,注意,插入的字母位置是从 \(n+1\) 标号到 \(n+k\) 的,所以通过等差数列求和公式可得插入后的字符的总价值为 \(\dfrac{(n+1+n+k)\times k\times\max\{W_{\texttt{a}\sim\texttt{z}}\}}{2}=\dfrac{(2n+k+1)\times k\times\max\{W_{\texttt{a}\sim\texttt{z}}\}}{2}\)。
最后将插入的字符的价值加入到原来的价值里面就是我们想要的答案了。
Code
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstring>
using namespace std;
string s;
long long k, val[27], maxi, ans;
int main() {
cin >> s;
scanf("%d", &k);
for(int i = 1; i <= 26; ++i) {scanf("%d", &val[i]); maxi = max(maxi, val[i]);}
long long len = s.size();
for(long long i = 0; i < len; ++i) ans += (i + 1) * val[s[i] - 'a' + 1];
ans += (2 * len + k + 1) * k * maxi / 2;
printf("%lld", ans);
return 0;
}
//貌似不需要开 long long(
CF447B DZY Loves Strings 题解的更多相关文章
- CF447B DZY Loves Strings 贪心
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...
- Codeforces Round #254 (Div. 1) D - DZY Loves Strings
D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...
- Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力
D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...
- [CodeForces - 447B] B - DZY Loves Strings
B - DZY Loves Strings DZY loves collecting special strings which only contain lowercase letters. For ...
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF444E. DZY Loves Planting
题目链接 CF444E. DZY Loves Planting 题解 可以..二分网络流 可是 考虑边从小到大排序 考虑每条边能否成为答案 用并查集维护节点之间的联通性 对于一条边来说,如果这条边可以 ...
- CF 447B(DZY Loves Strings-贪心)
B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- DZY Loves Math 系列详细题解
BZOJ 3309: DZY Loves Math I 题意 \(f(n)\) 为 \(n\) 幂指数的最大值. \[ \sum_{i = 1}^{a} \sum_{j = 1}^{b} f(\gcd ...
- 【题解】DZY Loves Chinese
[题解]DZY Loves Chinese II 不吐槽这题面了... 考虑如何维护图的连通性,如果把图的变成一颗的\(dfs\)生成树,那么如果把一个节点的父边和他接下来所有的返祖边删除,那么我们就 ...
随机推荐
- 解决WSL2中Vmmem内存占用过大问题教程
Windows的linux子系统最大占用可到本机器的80%,所以必须限制一下它的性能来达到优化目的 按下Windows + R 键,输入 %UserProfile% 并运行进入用户文件夹 新建文件 . ...
- HTML四种常见的定位-相对定位
相对定位 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset=&q ...
- 【AGC板刷记录】
这个帖子,是在自己学知识点累了的时候就看看\(AGC\)的题目来休息. 而且白天上课可以做( AGC-001 \(A\ BBQ Easy\) 考虑从小到大排,相邻两个取为一对. BBQ Easy #i ...
- AtCoder Regular Contest 127 题解
sb atcoder 提前比赛时间/fn/fn/fn--sb atcoder 还我 rating/zk/zk/zk A 签到题,枚举位数 \(+\) 前导 \(1\) 个数然后随便算算贡献即可,时间复 ...
- 手写Bitset优化
一种优化方法,具体例子可以看这里 这里只是存一下手写Bitset的板子 struct Bitset { unsigned a[1600]; void reset() { memset(a,0,size ...
- DirectX12 3D 游戏开发与实战第五章内容
渲染流水线 学习目标: 了解用于在2D图像中表现出场景立体感和空间深度感等真实效果的关键因素 探索如何用Direct3D表示3D对象 学习如何建立虚拟摄像机 理解渲染流水线,根据给定的3D场景的几何描 ...
- [Linux] 非root安装GCC9.1.0
说明 一般Linux系统自带或公共的GCC版本都很低,如目前我们的服务器版本的GCC还停留在gcc-4.9.3,而官网已到达9.2版本(下载http://ftp.gnu.org/gnu/gcc/) , ...
- Python——MacBook Pro中安装pip
1.系统已有python2和python3,如何检查MacBook Pro系统是否安装的有pip? 看到terminal的提示没有,有提示pip的,下面的提示,说明pip安装了. 要查看pip3是否安 ...
- exit(0) exit(1) return() 3个的区别
exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: return():返回函数,若在主函数中,则会退出函数并返回一值. 详细说: 1. return返回函数值,是关键字 ...
- 23-Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...