Codeforces 165C Another Problem on Strings 【二分】

本文介绍Codeforces165C题目的解题思路及AC代码实现,该题目要求找出给定二进制字符串中包含恰好k个字符'1'的所有子串的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:Codeforces 165C Another Problem on Strings

C. Another Problem on Strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A string is binary, if it consists only of characters “0” and “1”.

String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string “010” has six substrings: “0”, “1”, “0”, “01”, “10”, “010”. Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.

You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters “1”.

Input
The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.

Output
Print the single number — the number of substrings of the given string, containing exactly k characters “1”.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples
input
1
1010
output
6
input
2
01010
output
4
input
100
01010
output
0
Note
In the first sample the sought substrings are: “1”, “1”, “10”, “01”, “10”, “010”.

In the second sample the sought substrings are: “101”, “0101”, “1010”, “01010”.

题意:问你严格有k个1的子串数目。

思路:预处理前缀和,然后二分。

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e6 +10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
char str[MAXN];
int sum[MAXN];
int Find(int l, int r, int p, int v) {
    int ans = r + 1;
    while(r >= l) {
        int mid = (l + r) >> 1;
        if(sum[mid] - sum[p-1] >= v) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    return ans;
}
int main()
{
    int k;
    while(scanf("%d", &k) != EOF) {
        scanf("%s", str+1);
        int len = strlen(str+1); sum[0] = 0;
        for(int i = 1; i <= len; i++) {
            sum[i] = sum[i-1] + (str[i] == '1');
        }
        LL ans = 0;
        for(int i = 1; i <= len; i++) {
            int s = Find(i, len, i, k);
            if(s == len + 1) continue;
            int t = Find(i, len, i, k+1);
            ans += t - s;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
### 关于 Codeforces 上二项装箱问题 #### 二项装箱问题概述 二项装箱问题是经典的组合优化问题之一,在计算机科学领域具有重要意义。该类问题通常涉及将一组不同大小的对象放入固定容量的容器中,目标是最小化使用的容器数量[^1]。 对于特定平台上的挑战实例,如Codeforces中的二项装箱问题,其核心在于设计高效算法来解决这一NP难问题。尽管找到最优解可能非常复杂,但存在多种启发式方法可以提供接近最佳的结果,并且这些方法能够在合理的时间内执行完毕。 #### 解决方案策略 一种常见的处理方式是采用贪心算法,即总是尝试把当前最大的未分配物品放置到第一个能够容纳它的箱子中;如果没有任何现有箱子能放下这件物品,则创建一个新的箱子用于装载它。这种方法简单易懂,但在某些情况下可能会导致次优解。 更复杂的近似算法包括首次适应下降(First Fit Decreasing, FFD),此技术首先按照降序排列所有项目尺寸,之后应用首次适配原则(FD)。FFD已被证明能在多项式时间内给出不超过理想最小值11/9倍数加四的解法质量保证[^2]。 此外还有其他高级求解途径比如动态规划、分支限界以及遗传算法等,它们各自适用于不同的应用场景并提供了不同程度上的性能改进。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> items(n); for(int i = 0; i < n; ++i){ cin >> items[i]; } sort(items.begin(), items.end(), greater<int>()); const int bin_capacity = 1000; // 假设每个bin的最大容量为1000单位体积 vector<int> bins; for(auto item : items){ bool placed = false; for(auto& b : bins){ if(b + item <= bin_capacity){ b += item; placed = true; break; } } if(!placed){ bins.push_back(item); } } cout << "Minimum number of bins required is: " << bins.size(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值