14. Juju and Binary String
题目链接:Juju and Binary String
感觉这题的思路和代码实现都比 E 题简单,不知道怎么评到的 2700 分。
给你一个长为 nnn 的 01 串,定义其 cuteness 为 1 的个数比上总长度。让你找 kkk 个子串,使它们的长度之和为 mmm,且拼接后的 cuteness 与原串的相等。最小化 kkk 并输出方案。
设总共有 xxx 个 1,那么子串中需要 y=xmny=\dfrac{xm}{n}y=nxm 个 1。显然,如果 yyy 不是整数,那么一定构造不出来。否则肯定能构造出来,因为直接拿 yyy 个 1 和 m−ym-ym−y 个 0 就是一种方案了。
然后就是最小化 kkk,最理想的情况当然是存在一个长为 mmm 的子串正好有 yyy 个 1。第二个样例提示我们这种情况并不一定存在,但我们可以把原串连成一个环,如果我们选择的区间正好跨过了连起来的地方,kkk 就为 222。可以证明,在环上总是可以找到这样的一个子串。
先证明一个引理:环上两个相邻的长为 mmm 的子串内 1 的个数相差不会超过 111。
这是显然的,若第一个子串从 iii 处开始,则当且仅当 s[i]≠s[i+m]s[i]\neq s[i+m]s[i]=s[i+m] 时两个子串内 111 的个数才会发生变化,两者差的绝对值必为 111。
这就意味着各子串之间 111 的个数的变化是连续的,即如果存在两个子串,其中一个含有的 111 的个数小于 yyy,另一个大于 yyy,就必然存在一个子串中 111 的个数刚好是 yyy。
然后可以用反证法证明不可能所有子串内 111 的个数均小于或均大于 yyy。求和一下就行了。
那么将原串复制一遍后求个前缀和就可以找到这个子串了。
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
const int maxn = 4e5 + 5;
int n, m;
char str[maxn];
int pre[maxn];
void solve() {cin >> n >> m;cin >> (str + 1);int cnt = 0;for (int i = 1; i <= n; ++i) {if (str[i] == '1')cnt++;str[i + n] = str[i];}for (int i = 1; i <= 2 * n; ++i) {pre[i] = pre[i - 1] + (str[i] == '1');}ll tot = 1LL * cnt * m;if (tot % n != 0) {cout << -1 << endl;return;}tot /= n;for (int i = m; i < n + m; ++i) {if (pre[i] - pre[i - m] == tot) {if (i <= n) {cout << 1 << endl;cout << i - m + 1 << ' ' << i << endl;} else {cout << 2 << endl;cout << 1 << ' ' << i - n << endl;cout << n - (m - (i - n)) + 1 << ' ' << n << endl;}break;}}
}
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T = 1;cin >> T;while (T--) {solve();}
}
14. Juju and Binary String相关推荐
- Juju and Binary String(前缀和)
Juju and Binary String [Link](Problem - F - Codeforces) 题意 给你一个010101串strstrstr长度为nnn,这个串的权定为onesn(o ...
- nyoj 题目5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alpha ...
- Binary String Reconstruction CodeForces - 1352F(思维+构造)
For some binary string s (i.e. each character si is either '0' or '1'), all pairs of consecutive (ad ...
- Binary String Minimizing CodeForces - 1256D(贪心)
You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1'). I ...
- 【2019牛客暑期多校训练营(第三场)- B】Crazy Binary String(思维,01串,前缀和)
题干: 链接:https://ac.nowcoder.com/acm/contest/883/B 来源:牛客网 ZYB loves binary strings (strings that only ...
- Binary String Matching
Binary String Matching 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your t ...
- Binary String Constructing(CF-1003B)
Problem Description You are given three integers aa, bb and xx. Your task is to construct a binary s ...
- 2019牛客暑期多校训练营(第三场) B-Crazy Binary String
题目描述 ZYB喜欢二进制字符串(只包含'0'和'1'的字符串).他喜欢equal binary string(其中字符串中的"0"数和"1"数相等) ZYB想 ...
- D1. Kirk and a Binary String (easy version)
题目链接:http://codeforces.com/contest/1204/problem/D1 D1. Kirk and a Binary String (easy version) time ...
最新文章
- parquet java_Apache Parquet Java API的文档?
- 简介nandflash、norflash、ram、sram、dram、rom、eeprom、flash的区别
- 我国智能家居行业运行现状分析 标准割裂市场
- 开源 java CMS - FreeCMS2.8 自定义标签 channelSon
- could not find the main class错误
- ddos发包机原理_简单的DDOS发包学习思路大家可以在去写
- SAP CRM里note界面默认语言的决定逻辑
- 离线安装 VS2017 的正确姿势
- 计算机二级web题目(2)--HTTP基础
- android 日历仿IOS,基于Android week view仿小米和iphone日历效果
- PAT-1002 写出这个数
- 东北大学计算机 大一物理考试题,2010-2011东北大学物理考试题及答案
- ubuntu 18.04 LTS 安装SecureCRT
- 万万没想到一个普通的地区问题,竟然让我们大战了300回合
- oracle 归档模式 ASM,oracle rac启用归档模式
- 一次excel表格操作
- 简单的个人介绍网页-主页面【附代码】
- 计算机组成原理---第二章运算器和运算方法
- 追梦算法----修路
- Exchange-获取主、所有SMTP地址
热门文章
- 读懂消费贷款之中国消费金融发展史
- 冷启动问题——协同过滤(推荐系统)
- 超详细!linux系统nlg-eval安装指南
- PC端下载B站和yotube视频的简单方法: you-get
- DataFrame使用pd.sample()随机选取N行数据
- blender script mmd_tool 自动重命名刚体到合适的名字
- LVM管理体系(二)通过扩展LV扩展逻辑卷(操作步骤)
- maya mel uf8_maya中mel文件如何使用?
- JNI NDK入门详解
- mysql 5.7.31 忘记root密码怎么修改密码并实现远程登录