C. Rings(构造)

https://codeforces.com/problemset/problem/1562/C
题意
Frodo was caught by Saruman. He tore a pouch from Frodo’s neck, shook out its contents —there was a pile of different rings: gold and silver…
“How am I to tell which is the One?!” the mage howled.

“Throw them one by one into the Cracks of Doom and watch when Mordor falls!”

Somewhere in a parallel Middle-earth, when Saruman caught Frodo, he only found n rings. And the i-th ring was either gold or silver. For convenience Saruman wrote down a binary string s of n characters, where the i-th character was 0 if the i-th ring was gold, and 1 if it was silver.

Saruman has a magic function f, which takes a binary string and returns a number obtained by converting the string into a binary number and then converting the binary number into a decimal number. For example, f(001010)=10,f(111)=7,f(11011101)=221.

Saruman, however, thinks that the order of the rings plays some important role. He wants to find 2 pairs of integers (l1,r1),(l2,r2), such that:

1≤l1≤n, 1≤r1≤n, r1−l1+1≥⌊n2⌋
1≤l2≤n, 1≤r2≤n, r2−l2+1≥⌊n2⌋
Pairs (l1,r1) and (l2,r2) are distinct. That is, at least one of l1≠l2 and r1≠r2 must hold.
Let t be the substring s[l1:r1] of s, and w be the substring s[l2:r2] of s. Then there exists non-negative integer k, such that f(t)=f(w)⋅k.
Here substring s[l:r] denotes slsl+1…sr−1sr, and ⌊x⌋ denotes rounding the number down to the nearest integer.

Help Saruman solve this problem! It is guaranteed that under the constraints of the problem at least one solution exists.

input
Each test contains multiple test cases.

The first line contains one positive integer t (1≤t≤103), denoting the number of test cases. Description of the test cases follows.

The first line of each test case contains one positive integer n (2≤n≤2⋅104) — length of the string.

The second line of each test case contains a non-empty binary string of length n.

It is guaranteed that the sum of n over all test cases does not exceed 105.

output
For every test case print four integers l1, r1, l2, r2, which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively.

If there are multiple solutions, print any.

思路
本题暴力必超时,我们考虑构造,我们发现对于一个二进制数,在它后面增加一个0便是将这个数乘2,例如110(6)的两倍1100(12),111(7)的两倍1110(14),所以我们找01串中0的位置,当0出现在前n/2个数中时,我们考虑向后构造t和w相等的情况,当0出现在后n/2个数中时,我们考虑向前构造t是w两倍的情况,当没0时在n/2附件向前向后拓展成相等的串即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
char s[N];
int main()
{int t;cin>>t;while(t--){int n;cin>>n>>s+1;int pos1=-1,pos2=-1;for(int i=1;s[i];i++){if(i>n/2&&s[i]=='0') pos2=i;else if(i<=n/2&&s[i]=='0') pos1=i;}if(pos2!=-1) cout<<"1 "<<pos2<<" 1 "<<pos2-1<<endl;else if(pos1!=-1) cout<<pos1<<" "<<n<<" "<<pos1+1<<" "<<n<<endl;else if(n&1) cout<<"1 "<<n/2+1<<" "<<n/2+1<<" "<<n<<endl;else cout<<"1 "<<n/2<<" "<<n/2+1<<" "<<n<<endl;}return 0;
}

C. Rings(构造)相关推荐

  1. Cf#741-C. Rings(构造)

    C. Rings 先贴上我错误的代码: #include <bits/stdc++.h> #define closeSync ios::sync_with_stdio(0);cin.tie ...

  2. Codeforces1562 C. Rings(构造)

    题意: 解法: 如果只有1,那么[1,n-1],[2,n]是答案.只有存在0,假设某一个0的位置为i, 如果i在左半部分,那么[i,n],[i+1,n]是答案, 如果i在右半部分,那么[1,i],[1 ...

  3. cf1562 C. Rings

    cf1562 C. Rings 题意: 给你一个长度为n的01串,在01串选两个连续区间S和T,要求区间长度>=⌊n2⌋\lfloor \frac{n}{2} \rfloor⌊2n​⌋. 现在定 ...

  4. Zemax优化过程中的Rings和Arms

    1.  建立"傻瓜"评价函数时,一般选择反映像质的"总体"指标,如弥散圆或波像差等,并且要做如下考虑: 选择像质评价指标的RMS值还是Peak to Valle ...

  5. ArcGIS for Js Graphic、GraphicLayer、Geometry、(Point,Line,Polygon)、Rings(Coordinates)之间的关系

    一.名词解释 Graphic 图形,只一个展示在底图上的一个图形.构造函数Graphic(Geometry,Sysmol,Attribute,InfoTemplate) 参数一:Geometry: 几 ...

  6. Chinese Rings (九连环+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目: Problem Description Dumbear likes to play th ...

  7. LeetCode简单题之构造矩形

    题目 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 所以,现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 你设计的矩形 ...

  8. 兑换量子计算机,阅读 | 【量子计算机】构造置换量子门

    原标题:阅读 | [量子计算机]构造置换量子门 量子计算机的一个基本组成单位叫量子门(quantum gate),下面简单介绍些基本概念. 量子比特和量子态 量子计算机的信息存储单元是一种叫做量子比特 ...

  9. [ActionScript 3.0] AS向php发送二进制数据方法之——在URLRequest中构造HTTP协议发送数据...

    主类 HTTPSendPHP.as 1 package 2 { 3 import com.JPEGEncoder.JPGEncoder; 4 import com.fylib.httpRequest. ...

最新文章

  1. pyspark性能调优参数
  2. 阿里云maven镜像地址
  3. POJ1125 Floyd
  4. Nginx出现这么几个500怎么解决?
  5. CTFshow php特性 web94
  6. 主机访问虚拟机中linux上的web服务
  7. View页面间的跳转
  8. [MATLAB调试笔记]Possion方程求网格点电场强度
  9. pandas显示全部数据内容_1行Python代码就能挖掘数据!这个库太神奇啦!
  10. Learning the Vi Editor, 6th Edition学习笔记(0)
  11. Linux中sort、uniq、cut、wc命令详解
  12. java网络编程阻塞_Java网络编程由浅入深三 一文了解非阻塞通信的图文代码示例详解...
  13. spring 基于java的配置
  14. 国内现在web前端高手薪资都拿多少?
  15. HTML 动画(一)
  16. matlab连续信号傅里叶变换,连续信号的傅里叶变换及matlab显示
  17. 安装linux ubuntu11系统时,应该如何选择键盘布局,在ubuntu上创建新键盘布局需要哪些步骤?...
  18. 【Learncpp中文翻译版】【1.9、1.10、1.11】
  19. Nginx的简单使用,配置多前端,多端口【微信小程序+前后端分离跨域解决】
  20. 弦截法求方程根例题c语言,弦截法求方程根.ppt

热门文章

  1. GitHub 优秀的 Android 开源项目 淘宝技术牛p博客整理开发中最常用的GitHub上 优秀的 Android 开源项目整理(精品)...
  2. 使用poi将数据库数据表中的数据导出成excel
  3. C语言计算长方体体积的代码(代码中稍加美化了一下)
  4. 药品过5关价翻12倍 批发商抢走药品一半利润
  5. python 处理文本(.txt文件)
  6. 【数据增强】Cutout
  7. 新手建站必看的十大忠告 1
  8. 转载: 房贷的两种还款方式介绍
  9. 树木分形迭代图 matlab,基于迭代函数系统的分形植物模拟
  10. 直接插入排序:监视哨的作用