The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online - H Traveling on the Axis-【思维模拟题目】
H Traveling on the Axis
BaoBao is taking a walk in the interval [0,n] on the number axis, but he is not free to move, as at every point (i−0.5) for all i∈[1,n], where i is an integer, stands a traffic light of type ti (ti∈{0,1}).
BaoBao decides to begin his walk from point p and end his walk at point q (both pand q are integers, and p<q). During each unit of time, the following events will happen in order:
- Let's say BaoBao is currently at point x, he will then check the traffic light at point (x+0.5). If the traffic light is green, BaoBao will move to point (x+1); If the traffic light is red, BaoBao will remain at point x.
- All the traffic lights change their colors. If a traffic light is currently red, it will change to green; If a traffic light is currently green, it will change to red.
A traffic light of type 0 is initially red, and a traffic light of type 1 is initially green.
Denote t(p,q) as the total units of time BaoBao needs to move from point p to point q. For some reason, BaoBao wants you to help him calculate

where both p and q are integers. Can you help him?
Input
There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:
The first and only line contains a string s (1≤∣s∣≤10^5, ∣s∣=n, si∈{‘0’,‘1’} for all 1≤i≤∣s∣), indicating the types of the traffic lights. If si=‘0’, the traffic light at point (i−0.5) is of type 0 and is initially red; If si=‘1’, the traffic light at point (i−0.5) is of type 1 and is initially green.
It's guaranteed that the sum of ∣s∣ of all test cases will not exceed 106.
Output
For each test case output one line containing one integer, indicating the answer.
Sample Input
3
101
011
11010
Sample Output
12
15
43
Hint
For the first sample test case, it's easy to calculate that t(0,1)=1, t(0,2)=2, t(0,3)=3, t(1,2)=2, t(1,3)=3 and t(2,3)=1, so the answer is 1+2+3+2+3+1=12.
For the second sample test case, it's easy to calculate that t(0,1)=2, t(0,2)=3, t(0,3)=5, t(1,2)=1, t(1,3)=3 and t(2,3)=1, so the answer is 2+3+5+1+3+1=15.
题目思路:
- 题目解释的样例,见样例
- 题目做的时候,起初用的三重循环,10^5d的单重循环量,三重循环枚举每种情况的每个起点和终点——直接炸了!想了想,可以进行优化!优化时,可以计算当前区间在作为起点的过程中,其实可以被计算len-k次,这样复杂度直接降为了10^10,心里捉摸了一下跑了一个裸的两重循环——在本地也炸了!阔怕!
- len表示本题的数字串长度!
- 左想右想,发现一重循环才是正解!瞎猜了一阵子无果!发现每次跑循环计算第k(k从0到len-1)个区间的时候,可以一次性把全部计算出来!即当前区间遍历的次数等于把该区间当做第一个起点的情况个数(len-k)个,再加上之前的区间的为起点并且覆盖到第k个区间的个数(设为num),num怎么求呢!
- num仔细根据样例推推发现,每轮走到的行数都是该列数字所对应的行数“1”上,然后从该列行数作为起点的也必须是从“1”出发的!这会有简单的回合!开一个f1和f2分别进行记录!
- 其余的见代码的注释吧!手推样例还是很重要的!
真数据:
真测试输入 真测试输出
感兴趣的可以试试上面的数据,应该没问题!
代码;
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<string>
#include<string.h>
#include<time.h>
#include<algorithm>
using namespace std;
const double eps=1e-;
#define PI acos(-1.0)
#define ll long long
#define mian main
#define mem(a,b) memset(a,b,sizeof(a)) char a[];
int b[][];
int main()
{ int T;
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
scanf("%s",a);
int len=strlen(a);
for(int i=; i<len; i++)
{
b[i][]=a[i]-''; ///原数据层
b[i][]=!b[i][]; ///红绿灯交替一遍后!若红绿灯再交替一遍其实就是上一层!
} int Time=;
int ans=,k=;
int f1=,f2=-;//表示当前层数0或者1 int num1=,num2=;
for(int k=; k<len; k++) ///暴力枚举一遍len个区间即可!!
{
if(f1==f2)
{
f2=-;
num1+=num2;
num2=;
}
num1-=k; ///这里是个终点,每往后走一个,会少k个前面的区间覆盖到当前区间的,必须减去
// printf(" (f1=%d f2=%d) ",f1,f2);
if(f1==) ///分配每轮的萌新层,与f1层相符就加进f1里
{
num1+=len-k;
}
else ///否则就新开一个f2来进行记录!
{
if(f2==-)
f2=;
num2+=len-k;
} if(b[k][f1]==)
{
ans+=*(num1 ),f1=!f1;
}
else
ans+=*(num1 );
if(f2!=-)
{
if(b[k][f2]==)
{
ans+=*(num2 ),f2=!f2;
}
else
ans+=*(num2 );
} // printf("&%d ",ans);
// printf("endd =%d ans_sum=%d\n",j,ans);
// printf("]%d\n",ans);
// printf("--> (f1=%d f2=%d)\n",f1,f2);
// printf("num1=%d num2=%d Time=%d &ans=%d\n",num1,num2,++Time,ans);
}
printf("%d\n",ans);
} } return ;
}
(有大量注释)
The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online - H Traveling on the Axis-【思维模拟题目】的更多相关文章
- The 2018 ACM-ICPC Asia Qingdao Regional Contest(部分题解)
摘要: 本文是The 2018 ACM-ICPC Asia Qingdao Regional Contest(青岛现场赛)的部分解题报告,给出了出题率较高的几道题的题解,希望熟悉区域赛的题型,进而对其 ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest
The 2018 ACM-ICPC Asia Qingdao Regional Contest 青岛总体来说只会3题 C #include<bits/stdc++.h> using nam ...
- ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków
ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...
- 2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred)
2019-2020 ICPC, Asia Jakarta Regional Contest (Online Mirror, ICPC Rules, Teams Preferred) easy: ACE ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online J - Press the Button(思维)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4056 题意 有一个按钮.一个灯.一个计时器和一个计数器,每按一次按钮,计时 ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online -C:Halting Problem(模拟)
C Halting Problem In computability theory, the halting problem is the problem of determining, from a ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online Solution
A Live Love 水. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; const i ...
- 2018-2019, ICPC, Asia Yokohama Regional Contest 2018 K
传送门:https://codeforces.com/gym/102082/attachments 题解: 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ...
- ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...
随机推荐
- Golang 异常/日志处理
1.xerrors 异常 xerrors 包是一个非常棒的设计,不同于往常语言如java/php,因为go的errors只是一个string类型的映射,所以内存占用空间很少.这在golang的核心库和 ...
- Andrew Ng机器学习课程14(补)
Andrew Ng机器学习课程14(补) 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 利用EM对factor analysis进行的推导还是要参看我的上一 ...
- Quartz.Net入门 - Net作业调度
背景 很多时候,项目需要在不同时刻,执行一个或很多个不同的作业. Windows执行计划这时并不能很好的满足需求了,迫切需要一个更为强大,方便管理,集群部署的作业调度框架. 介绍 Quartz一个开源 ...
- 设计模式的好书 -- ongoing
1 设计模式--可复用面向对象软件的基础 Erich Gamma. Richard Helm -- 已经下载了/baiduNetDisk Design Patterns --- Element ...
- Java程序员必会常用Linux速查手册
目錄 系统服务管理 文件管理 查看日志 压缩与解压 磁盘和网络管理 防火墙 ftp操作 软件的安装与管理 其他 系统服务管理 systemctl 输出系统中各个服务的状态: systemctl lis ...
- 【坑】Mybatis 多次逆向工程生成mapper文件
在使用 mybatis 逆向工程的时候,多次逆向工程生成的文件,是不会产生覆盖的,而是追加: 假如,你第一次逆向,发生数据库的某个字段类型错了,修改以后再次逆向,那么得到的 mapper文件,将是 2 ...
- 20190924-LeetCode解数独题目分享
解决数独 题目描述 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一个以 ...
- 基于Task的多线程
/// <summary> /// 基于Task的多线程 /// </summary> public class Tasks { public static void Task ...
- vim编辑器中的替换(转)
转1:https://www.cnblogs.com/david-wei0810/p/6385988.html 转2:https://blog.csdn.net/doubleface999/artic ...
- java实现工程配置文件敏感字段加解密
以下引自他人博客: 1. 需求背景我们在开发应用时,需要连接数据库,一般把数据库信息放在一个属性配置文件中,比如***.properties,具体的内容 #mysql的配置文件jdbc.url=jdb ...