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. 【谷歌浏览器】修改和添加Cookie

    一.使用谷歌浏览器 1.1.修改ookie 方法一:直接用开发者工具修改: 操作如图:   参考: 检查和删除 Cookie · Chrome 开发者工具中文文档 http://www.css88.c ...

  2. jira邮箱配置

    系统-邮件-外发邮件

  3. (模板)扩展kmp算法(luoguP5410)

    题目链接:https://www.luogu.org/problem/P5410 题意:有两个字符串a,b,要求输出b与a的每一个后缀的最长公共前缀.输出: 第一行有lenb个数,为b的next数组( ...

  4. wireguard使用方法

    1.翻墙访问网页:https://cryptostorm.is/wireguard.cgi 并下载客户端 2. 选者第二个并打开 3.复制publickey 4.黏贴在第二行并addkey: 5.将获 ...

  5. java 用户线程和守护线程

    在Java中通常有两种线程:用户线程和守护线程(也被称为服务线程)通过Thread.setDaemon(false)设置为用户线程通过Thread.setDaemon(true)设置为守护线程线程属性 ...

  6. SpringBoot 第一篇:HelloWorld 跑起来

    背景 金融行业从业快十年,作为银行系开发人员来说开源框架了解不多非常正常,因为银行系的运行平台,基本上不会采购小厂商集合开源框架自建的产品,竞标的产品没有几十个成功案例,你也进不了这个门槛(有关系的除 ...

  7. IOS+H5页面自定义按钮无效

    在IOS整合H5页面的时候,自定义的按钮失去效果,Android系统可以. 如图,确定和取消按钮在IOS系统无效. 解决办法是在两个按钮上加上一个style属性即可 <span class=&q ...

  8. IDEA整合Jenkins界面化管理项目构建

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/WALK_MAN_wubiao/articl ...

  9. CSS3中三种清除浮动(float)影响的方式

    float是HTML中布局的一大关键,很多难题一旦用上float都能很愉快地解决.但是凡是好用的,也容易出错.比如当子元素都为float时,其父元素会受影响,或者偶尔会发现自己某个div的高度变成了0 ...

  10. IQueryable vs. IEnumerable

    IQueryable<T> extends the IEnumerable<T> interface IEnumerable<T> is great for wor ...