Gym - 101350A Sherlock Bones(思维)
The great dog detective Sherlock Bones is on the verge of a new discovery. But for this problem, he needs the help of his most trusted advisor -you- to help him fetch the answer to this case.
He is given a string of zeros and ones and length N.
Let F(x, y) equal to the number of ones in the string between indices x and yinclusively.
Your task is to help Sherlock Bones find the number of ways to choose indices (i, j, k) such that i < j < k, sj is equal to 1, and F(i, j) is equal to F(j, k).
Input
The first line of input is T – the number of test cases.
The first line of each test case is an integer N (3 ≤ N ≤ 2 × 105).
The second line is a string of zeros and ones of length N.
Output
For each test case, output a line containing a single integer- the number of ways to choose indices (i, j, k).
Example
3
5
01010
6
101001
7
1101011
3
7 题意:
给定01字符串,求有多少个三元组i,j,k满足i<j<k且F(i, j)=F(j, k).其中F(x,y)是字符串下标x到y的一的个数。
下标为j的一定要是1。
思路:
先处理一下0的数量分布。比如00110就是2,0,1
假设字符串以0为开头和结尾,那么字符串便可以表示为:
a,1,b,1,c,1,d,1,e,1,f
其中字母表示0的数量。
枚举j在哪一个1上,便可以得出以下式子、
ans=a*b+ b*c+a*d+ c*d+b*e+a*f+ d*e+c*f+ e*f;
可以看出,每一个字母其实就是要与之后奇数或偶数的后缀和相乘,乘积相加就是结果。
但是这样仍然不对,因为i可k还可以指向1.
于是我们把每个字母的值加上一个1,再进行计算即可。
然而这样还是有问题。
相邻的两个字母相乘时不需要+1,于是我们再用一个for循环处理一下即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
char s[maxn];
vector<ll>v;
ll sum[maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--){int len;
scanf("%d%s",&len,s);
memset(sum,,sizeof(sum));
v.clear();
ll ans=;
for(int i=;i<len;i++){
if(s[i]==''){
ans++;
}
else{
v.push_back(ans);
ans=;
}
}
v.push_back(ans);
int sz=v.size();
for(int i=sz-;i>=;i--){
sum[i]=v[i]+sum[i+];
}
ans=;
for(int i=;i<sz;i++){
ans+=v[i]*sum[i+];
}
for(int i=;i<sz-;i++){
ans-=v[i]*v[i+]-(v[i]-)*(v[i+]-);
}
printf("%lld\n",ans);
}
return ;
}
Gym - 101350A Sherlock Bones(思维)的更多相关文章
- 组队赛Day1第一场 GYM 101350A - Sherlock Bones (DP)
[题意] 给你一个01串.f(i,j)定义为区间[i,j]内1的个数,求区间 [i,j,k] 中 f(i,j) =f(j,k) 的情况的子串总数,要求str[j]=='1'. (题意描述引自Ilook ...
- Gym 102028C - Supreme Command - [思维题][2018-2019 ACM-ICPC Asia Jiaozuo Regional Contest Problem C]
题目链接:https://codeforces.com/gym/102028/problem/C Lewis likes playing chess. Now he has n rooks on th ...
- Gym 101775C - Traffic Light - [思维题]
题目链接:http://codeforces.com/gym/101775/problem/C 题意: 给出 $N$ 个红绿灯,又给出 $N+1$ 个距离 $S_i = S_0,S_1, \cdots ...
- Gym 100801E Easy Arithmetic (思维题)
题目:传送门.(需要下载PDF) 题意:给定一个长度不超过1000的字符串表达式,向该表达式中加入'+'或'-',使得表达式的值最大,输出该表达式. 题解:比如300-456就改成300-4+56,遇 ...
- A - Arcade Game Gym - 100814A (概率思维题)
题目链接:https://cn.vjudge.net/contest/285964#problem/A 题目大意:每一次给你你一个数,然后对于每一次操作,可以将当前的数的每一位互换,如果互换后的数小于 ...
- G - WiFi Password Gym - 101608G (异或思维题+曲尺)
题目链接:https://cn.vjudge.net/contest/285962#problem/G 题目大意:给你n和m,n代表有n个数,然后让你找出一个最长的区间,使得这个区间内的所有的数的‘’ ...
- L - Looking for Taste Gym - 101991L 二进制枚举/思维
方法一:因为最多是10的六次方,所以可以直接枚举二进制上的每一位来得到最优结果. AC代码: #include<iostream> #include<stack> #inclu ...
- Gym 100851A Adjustment Office (思维)
题意:给定一个 n*n 的矩阵,然后有 m 个询问,问你每一行或者每一列总是多少,并把这一行清空. 析:这个题不仔细想想,还真不好想,我们可以根据这个题意,知道每一行或者每一列都可以求和公式来求,然后 ...
- Gym 101128A Promotions(思维 + dfs)题解
题意:给一有向图,如果A指向B,则A是B的上级.一直i要升职那么他的上级必须都升职.现在给你一个升职人数的区间[a, b],问你升职a人时几个人必被升职,b时几个人必升职,b时几个人没有可能被升职. ...
随机推荐
- JavaScript中闭包的写法和作用详解
1.什么是闭包 闭包是有权访问另一个函数作用域的变量的函数. 简单的说,Javascript允许使用内部函数---即函数定义和函数表达式位于另一个函数的函数体内.而且,这些内部函数可以访问它们所在的外 ...
- MySQL InnoDB下关于MVCC的一个问题的分析
这个是网友++C++在群里问的一个关于MySQL的问题,本篇文章实验测试环境为MySQL 5.6.20,事务隔离级别为REPEATABLE-READ ,在演示问题前,我们先准备测试环境.准备一个测 ...
- input file实现多选和限制文件上传类型
<!-- input file accept 属性设置可上传文件的类型 multiple属性设置可多文件上传--> <!-- accept 并未真正的实现限制上传文件类型,只是在 ...
- 从0开始的Python学习009参数
默认参数 对于参数有时候我们希望他是可选的,前面介绍了函数柯里化,当然还有其他的解决方案.如果不想给某些参数提供值的话,就让这写参数使用默认值.在函数定义的时候给参数赋值使用(参数,参数=值..... ...
- 使用laravel-admin后台sdk报错Failed to load resource: net::ERR_CERT_AUTHORITY_INVALID、Provisional headers are shown
报错Failed to load resource: net::ERR_CERT_AUTHORITY_INVALID请先确定自己的资源url是否可以确实访问到(地址正确与否.访问权限是否开启等) 若n ...
- Think_in_java_4th(并发学习一)
Java的并发是在顺序语言的基础上提供对线程的支持的. 并发能够更加有效的执行我们的代码,也就是更加合理的应用CPU资源. 并发程序往往CPU和内存使用率,要高于同等的非并发程序. 下面就用Think ...
- the security settings could not be applied to the database(mysql安装error)【简记】
在安装mysql时,出现“The security settings could not be applied to the database because the connection has f ...
- Eclipse启动报错,解决办法
打开log日志,发现如下错误.原因是修改了计算机用户名导致 !SESSION Thu Aug 30 08:55:41 CST 2018 -------------------------------- ...
- some settings for spacemacs golang
spacemacs 中的 golang配置 spacemacs 中的 golang layer 已经有很多默认的配置了, 但是都是针对在 GOPATH 下的配置. 如果你的项目不再默认 的 GOPAT ...
- C++面向对象程序设计之C++的初步知识
本节内容为学习谭浩强老师编写的<C++面向对象程序设计>的第1章 C++的初步知识 后的个人总结. 在正文开始之前,首先声明,我是Python程序员. 1.2.最简单的C++程序 例1 ...