CodeForces 165C - Another Problem on Strings
来源:互联网 发布:美工外包注意事项 编辑:程序博客网 时间:2024/05/16 14:30
题意:给定一个长度为 n (1 <= n <= 10^6 ) 的01串,求有多少连续子序列中包含 k 个 1。
从前往后记录到第 i 个元素存在了多少个 1,然后从前往后找个数 >= k 的元素,再找 - k 与 - k + 1 之间与多少个元素加入结果即可。
#include<cstdio>#include<cstring>#include<cctype>#include<cstdlib>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<list>typedef long long ll;typedef unsigned long long llu;const int MAXN = 100 + 10;const int MAXT = 1000000 + 10;const int INF = 0x7f7f7f7f;const double pi = acos(-1.0);const double EPS = 1e-6;using namespace std;int n, k, num[MAXT];char s[MAXT];int main(){ scanf("%d%s", &k, s + 1); n = strlen(s + 1); num[0] = 0; for(int i = 1; i <= n; ++i){ num[i] = num[i - 1]; if(s[i] == '1') ++num[i]; } num[n + 1] = num[n] + 1; llu ans = 0; for(int i = 1; i <= n; ++i) if(num[i] >= k){ int c1 = lower_bound(num, num + i, num[i] - k) - num; int c2 = upper_bound(num, num + i, num[i] - k) - num; ans += llu(c2 - c1); } printf("%I64d\n", ans); return 0;}
0 0
- CodeForces 165C - Another Problem on Strings
- CodeForces 165C - Another Problem on Strings
- codeforces-165C-Another Problem on Strings
- Codeforces 165C Another Problem on Strings 【二分】
- codeforces 165C Another Problem on Strings 双指针
- CodeForces 165C Another Problem on Strings(公式推导)
- Another Problem on Strings CodeForces
- codeforce 165C Another Problem on Strings
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- Codeforces Round #112 (Div. 2) C. Another Problem on Strings
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- Codeforces 165C Another Problem On Strings 尺取 Or 前缀和
- codeforces 165-C. Another Problem on Strings(计数+尺取)
- codeforces 之 Another Problem on Strings
- Another Problem on Strings
- codeforces Another Problem on Strings 记录一个不慎
- cf165c Another Problem on Strings
- C. Another Problem on Strin
- R语言笔记 持续更新
- Dockerfile-centos:tengine
- Highcharts(四)之数据列
- 12期 8月期刊自荐
- 1020. 月饼 (25) -- 做题记录
- CodeForces 165C - Another Problem on Strings
- 前言
- Java的基础知识2——数组
- Highcharts(五)之颜色
- Windows CMD命令 (自用)
- Apache + Tomcat
- android----菜单
- Highcharts(六)之提示框
- Highcharts(七)之版权信息