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. 基于 Spring Cloud 开发的分布式系统,遇到爬虫、接口盗刷怎么办?
  2. 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting
  3. 你所不知道的日志异步落库
  4. 我是如何从技术转向产品的
  5. go语言goroutine的取消
  6. java调用三汇语音卡,三汇语音卡
  7. sql server 修改字段不能为空_SQL-SQL介绍
  8. JAVA无法加载此类文件,ORA-00376: 此时无法读取文件问题处理
  9. python 规则引擎 drools_Drools规则引擎详解-常用的drl实例
  10. 中心管理cms服务器_如何查找网站使用的CMS,脚本,服务器,技术
  11. Python爬虫入门教程 29-100 手机APP数据抓取 pyspider
  12. 【Hibernate教程】集合映射
  13. Android物联网应用程序开发(智慧城市)—— 购物信息的存储界面开发
  14. 通俗易懂|基于零信任理念的技术让远程办公「安全又敏捷」
  15. 时间序列的特征工程——针对Hurst指数的Python计算
  16. mysql查询工龄在20年以上_数据库查询练习
  17. Echarts icon图标 默认样式 样式总结
  18. photoshop进行景深合成
  19. c语言 显示一行中文 程序,关于C语言TurboC中显示汉字的问题程序如下(我已把hak16 爱问知识人...
  20. CentOS安装QEMU(试验成功且简单的方法)

热门文章

  1. Android 商城类应用实战之购物车附源码
  2. MongoDB基础教程
  3. b站视频-尚硅谷jQuery教程张晓飞老师-笔记
  4. 解决Eclipse中的卡死现象
  5. 谈网站域名,备案到上线
  6. 用户为什么要使用天翼LIVE?
  7. 京东“毕业”下的众生相:房贷、二胎,这失业后的日子怎么办?
  8. c语言或运算怎么没起作用,C语言学不会? 这样学习, 才有效果!
  9. C#实现数独解题及随机出题的思路及源码分享
  10. IMPL2. get_cells/get_pins等获取对象常用命令解析