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. Vue3 相比 vue2

    Vue3 使用Proxy替代了defineProperty. Proxy 相比于 defineProperty 的优势 Object.defineProperty() 的问题主要有三个: 不能监听数组 ...

  2. iOS-AES算法总结

    AESCipher.h #import <Foundation/Foundation.h> @interface AESCipher : NSObject /** 加密算法 @param ...

  3. Windows 10系统快捷键

    虚拟桌面 创建新的虚拟桌面:Win + Ctrl + D 关闭当前虚拟桌面:Win + Ctrl + F4 切换虚拟桌面:Win + Ctrl +左/右 任务视图:Win + Tab Win10常用W ...

  4. js 数组去重、去空(收藏)

    function unique (arr) { return Array.from(new Set(arr)) } var arr = [1,1,'true','true',true,true,15, ...

  5. ${__setProperty(row,rowNum)};不能在import XXX后面使用;

    如下 ${__javaScript只能用一次调用 excel.CWResultFile.CWOutputFile.wOutputFile("/Users/iot/1.xls", & ...

  6. go hello world第一个程序

    main 函数所在的包名必须使用main import "fmt"  导入包fmt  fmt包包含了Println方法的定义 func main() 程序运行入口方法和c语言相似 ...

  7. Spring (1)框架

    Spring第一天笔记   1. 说在前面 怎样的架构的程序,我们认为是一个优秀的架构? 我们考虑的标准:可维护性好,可扩展性好,性能. 什么叫可扩展性好? 答:就是可以做到,不断的增加代码,但是可以 ...

  8. Qt中容器类应该如何存储对象(最好使用对象指针类型,如:QList<TestObj*>,而不要使用 QList<TestObj> 这样的定义,建议采用 智能指针QSharedPointer)

    Qt提供了丰富的容器类型,如:QList.QVector.QMap等等.详细的使用方法可以参考官方文档,网上也有很多示例文章,不过大部分文章的举例都是使用基础类型:如int.QString等.如果我们 ...

  9. MySQL 子查询(三) 派生表、子查询错误

    From MySQL 5.7 ref:13.2.10.8 Derived Tables 八.派生表 派生表是一个表达式,用于在一个查询的FROM子句的范围内生成表. 例如,在一个SELECT查询的FR ...

  10. 美化linux客户端zsh和oh-my-zsh

    linuxbashzshoh-my-zsh 一.安装zsh 二.安装oh-my-zsh 一.安装zsh 安装 zsh yum -y install zsh 替换默认shell chsh -s /bin ...