Codeforces Global Round 4 B. WOW Factor (前缀和,数学)

题意:找出序列中有多少子序列是\(wow\),但是\(w\)只能用\(vv\)来表示.
题解:我们分别记录连续的\(v\)和\(o\)的个数,用\(v1\)和\(v2\)存,这里要注意前导\(o\)不能要,观察一下写出答案公式:\(ans=v1[i]*(v2[i]*(v1[i+1]+...+v1[k])+v2[i+1]*(v1[i+2]+...+v1[k])+...+v2[k-1]*v[k])+v1[i+1]*(...)+...+v1[k-1]*v2[k-1]*v1[k]\),很显然我们是不能直接算的,但是发现这个公式里面有很多连续的线性和,所以我们可以用前缀和来进行复杂度的优化,将其降到\(O(n)\).
代码:
string s;
vector<ll> v1,v2;
ll cnt1,cnt2;
ll pre1[N],pre[N]; int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>s;
bool flag=false;
for(int i=0;i<(int)s.size();++i){
if(s[i]=='v'){
cnt1++;
if(cnt2 && flag){
v2.pb(cnt2);
cnt2=0;
}
if(!flag) cnt2=0;
flag=true;
}
else{
cnt2++;
if(cnt1){
v1.pb(cnt1);
cnt1=0;
}
}
}
if(cnt1) v1.pb(cnt1); int len1=(int) v1.size();
int len2=(int) v2.size();
for(int i=len1-1;i>=0;--i){
pre1[i]=pre1[i+1]+v1[i]-1; //先对v1求前缀和.
}
for(int i=len2-1;i>=0;--i){
pre[i]=pre[i+1]+v2[i]*pre1[i+1]; //求出v2[i]*(v1[i+1]+...+v1[k])的前缀和pre[i]<-这里(...)的就是我们上一步求的前缀和pre1[i+1].
}
ll ans=0;
for(int i=0;i<len1;++i){
ans+=(v1[i]-1)*pre[i];
}
cout<<ans<<endl; return 0;
}
Codeforces Global Round 4 B. WOW Factor (前缀和,数学)的更多相关文章
- Codeforces Global Round 7 A. Bad Ugly Numbers(数学)
题意: 给你一个 n,输出一个 n 位不含 0 且不被任一位整除的正数. 思路: 构造 233 或 899. #include <bits/stdc++.h> using namespac ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
随机推荐
- Selenium WebDriver 8大定位方式
Selenium WebDriver 8大定位方式: driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- docker 数据卷的挂载和使用
容器之间的数据共享技术, Docker容器产生的数据同步到本地 卷技术 --> 目录挂载, 将容器内的目录挂载到服务器上 使用命令来挂载 -v # 可以挂载多个目录 docker run -it ...
- 关于 percona monitoring plugins插件报slave is stoped on ip地址
思路:肯定是某个item触发了触发器 去看触发器,找到 slave is stoped,如下图 看到键是mysql.running-slave ,然后去定义key的文件中查看该键对应的脚本,修改脚本. ...
- zabbix自动发现主机并注册
- 【Jboss】A RESOURCE POOL IS PERMANENTLY BROKEN!
jboss后台报错,其中有这个错误 [error] A RESOURCE POOL IS PERMANENTLY BROKEN! 查阅多方资料后发现.数据库连接配置文件中,有地方存在空格,导致服务连接 ...
- Test typora
目录 0. test 0.5 easy test 1. problem 1 2. problem 2 3. problem 3 import numpy as np import matplotlib ...
- P1140 相似基因(字符串距离,递推)
题目链接: https://www.luogu.org/problemnew/show/P1140 题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了44种核苷酸,简记作A,C,G,TA,C, ...
- 解决ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table study_record( id int(11) not null
之前一直用的好好的,突然就出现了这个错误: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha ...
- 利用sklearn进行字典&文本的特征提取
写在前面 这篇博客主要内容: 应用DictVectorizer实现对类别特征进行数值化.离散化 应用CountVectorizer实现对文本特征进行数值化 特征提取API sklearn.featur ...
- 集成多种协议、用于 USBC 端口的快充协议芯片IP2723
1. 特性 快充规格 集成 QC4/QC4+输出快充协议 - 兼容 QC2.0/QC3.0 - 支持 Class B 电压等级 集成 FCP 输出快充协议 集成 SCP 输出快充协议 集成 ...