H Traveling on the Axis   

作者: 浙江大学竞赛命题组
单位: ACMICPC
时间限制: 500 ms
内存限制: 64 MB
代码长度限制: 32 KB

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 t​i​​ (t​i​​∈{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:

  1. 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.
  2. 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, s​i​​∈{‘0’,‘1’} for all 1≤i≤∣s∣), indicating the types of the traffic lights. If s​i​​=‘0’, the traffic light at point (i−0.5) is of type 0 and is initially red; If s​i​​=‘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 10​6​​.

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.


 题目思路:

  1. 题目解释的样例,见样例
  2. 题目做的时候,起初用的三重循环,10^5d的单重循环量,三重循环枚举每种情况的每个起点和终点——直接炸了!想了想,可以进行优化!优化时,可以计算当前区间在作为起点的过程中,其实可以被计算len-k次,这样复杂度直接降为了10^10,心里捉摸了一下跑了一个裸的两重循环——在本地也炸了!阔怕!
  3. len表示本题的数字串长度!
  4. 左想右想,发现一重循环才是正解!瞎猜了一阵子无果!发现每次跑循环计算第k(k从0到len-1)个区间的时候,可以一次性把全部计算出来!即当前区间遍历的次数等于把该区间当做第一个起点的情况个数(len-k)个,再加上之前的区间的为起点并且覆盖到第k个区间的个数(设为num),num怎么求呢!
  5. num仔细根据样例推推发现,每轮走到的行数都是该列数字所对应的行数“1”上,然后从该列行数作为起点的也必须是从“1”出发的!这会有简单的回合!开一个f1和f2分别进行记录!
  6. 其余的见代码的注释吧!手推样例还是很重要的!

真数据:

真测试输入

真测试输出

感兴趣的可以试试上面的数据,应该没问题!

代码;

#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-【思维模拟题目】的更多相关文章

  1. The 2018 ACM-ICPC Asia Qingdao Regional Contest(部分题解)

    摘要: 本文是The 2018 ACM-ICPC Asia Qingdao Regional Contest(青岛现场赛)的部分解题报告,给出了出题率较高的几道题的题解,希望熟悉区域赛的题型,进而对其 ...

  2. The 2018 ACM-ICPC Asia Qingdao Regional Contest

    The 2018 ACM-ICPC Asia Qingdao Regional Contest 青岛总体来说只会3题 C #include<bits/stdc++.h> using nam ...

  3. 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 ...

  4. 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 ...

  5. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online J - Press the Button(思维)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4056 题意 有一个按钮.一个灯.一个计时器和一个计数器,每按一次按钮,计时 ...

  6. 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 ...

  7. 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 ...

  8. 2018-2019, ICPC, Asia Yokohama Regional Contest 2018 K

    传送门:https://codeforces.com/gym/102082/attachments 题解: 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ...

  9. ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...

随机推荐

  1. 南柯33的Python学习笔记第(一)部分

    Python基础 1.Python简介 1.1 Python是什么编程语言 从编程语言的几个方向来说 编译型和解释型 什么是编译型?什么是解释型? 编译型:就是把源代码一下全部都编译成二进制文件(优点 ...

  2. Linux IO的五种模型 ongoing

    服务器端编程经常需要构造高性能的IO模型,常见的IO模型: 阻塞I/O模型  (Blocking IO) ------------(同步)(阻塞) 非阻塞I/O模型 (Non-Blocking IO) ...

  3. 如何安装Oracle--新手安装Oracle教程

    1. 将win32_11gR2_database_1of2.zip与win32_11gR2_database_1of2.zip 解压到当前目录 PS:选中两个压缩包后右键解压到当前文件夹:必须同时解压 ...

  4. 长乐培训Day1

    T1 魔法照片 题目 [题目描述] 如果你看过<哈利·波特>,你就会知道魔法世界里的照片是很神奇的.也许是因为小魔法师佳佳长的太帅,很多人都找他要那种神奇的魔法照片, 而且还都要佳佳和他的 ...

  5. PAT甲级 图 相关题_C++题解

    图 PAT (Advanced Level) Practice 用到图的存储方式,但没有用到图的算法的题目 目录 1122 Hamiltonian Cycle (25) 1126 Eulerian P ...

  6. time() 函数时间不同步问题

    1.时区设置问题 处理方法:编辑php.ini  搜索 “timezone” 改写为 PRC 时区 2.服务器时间不同步 处理方法:设置服务器时间和本地时间进行同步

  7. 硬件实现IIC协议读取EEPROM

    我TMD也是服了,反正我板子搞了半天也不成功我也不知道为什么,野火STM32-MINI,一直卡EV5,不管了 先代码沾上 工程目录(板子为野火STM32 MINI) 串口相关代码: bsp_usart ...

  8. SAS学习笔记45 宏系统选项及其他

    关于宏的系统选项 MCOMPILENOTE=NONE|NOAUTOCALL|ALL 该系统选项控制是否在日志当中显示宏程序编译时的信息,默认值为NONE,也就是不显示.其中NOAUTOCALL针对的是 ...

  9. c++学习---const 和 string

    当在两个文件定义了同名的const变量时,相对于定义了两个独立的变量 想要在一个文件中定义一个const变量并在其他文件中使用他:不管时声明还是定义,都加上extern关键字 因为const对象一经初 ...

  10. (三)Redis之数据结构概念以及数据结构之字符串

    一.数据结构 五种数据类型: 字符串(String) 字符串列表(list) 有序字符串集合(sorted set) 哈希(hash) 字符串集合(set) 二.数据结构之字符串 二进制安全的,存入和 ...