题目描述
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 p and 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?

输入
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|≤105, |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.

输出
For each test case output one line containing one integer, indicating the answer.

样例输入
3
101
011
11010
样例输出
12
15
43

提示
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 1 0 1 0
0 0 1 0 1
0出发: 1 3 4 5 6  19  11
1出发:  1 2 3 4  10  10
2出发:    2 3 4  9   01
3出发:      1 2  3   10
4出发:        2  2   0

首先,根据相邻两个点的情况,不难发现,如果两个相邻的红绿灯的初始状态相同,那么通过一个红绿灯之后,下一个一定会拦下,基于这个,我们先算出从0出发的所有情况。之后从不同位置出发的所有情况,不难发现每往后一个位置,都会减去最前面的数据,这是因为出发位置的改变使情况减少了,更换起点之后,应该根据起点位置,再做新的初试调整,也就是说如果修改位置后前面两个为11、10、01、00,都应该对应不同的操作,以11为例,如果调整前最前面两个为11,那么初始位置向后一一个后变成10,就减少了开始等待的两分钟,直接通过两个路口,这引起整个后续情况都少了两分钟,其余情况以此类推。

另外几个数据需要定义成long long int,不然会WA。

AC代码

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{int t;scanf("%d",&t);getchar();while(t--){string str;cin>>str;getchar();long long int time=0,ans=0;if(str[0]=='0')time=2;else if(str[0]=='1')time=1;ans+=time;for(int i=1;i<str.length();i++){if(str[i]==str[i-1])time+=2;elsetime++;ans+=time;    }long long int sum=ans;int m=str.length();for(int i=1;i<str.length();i++){if(str[i-1]=='1'&&str[i]=='1')//11ans=ans-((m-1)*2+1);else if(str[i-1]=='1'&&str[i]=='0')//10ans=ans-1;else if(str[i-1]=='0')//0Xans=ans-2*m;sum+=ans;m--;}printf("%lld\n",sum);}return 0;
}

Traveling on the Axis 模拟相关推荐

  1. Traveling on the Axis (The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4054 题意就是一个小孩走一段路,这条路上有n个红绿灯,并且告诉 ...

  2. 2021-2027全球与中国模拟监控摄像头市场现状及未来发展趋势

    2021-2027全球与中国模拟监控摄像头市场现状及未来发展趋势 2019年,全球模拟监控摄像头市场规模达到了xx亿元,预计2026年将达到xx亿元,年复合增长率(CAGR)为xx%. 本报告研究全球 ...

  3. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    zoj4047 A Live Love DreamGrid is playing the music game Live Love. He has just finished a song consi ...

  4. [matlab]Monte Carlo模拟学习笔记

    理论基础:大数定理,当频数足够多时,频率可以逼近概率,从而依靠概率与$\pi$的关系,求出$\pi$ 所以,rand在Monte Carlo中是必不可少的,必须保证测试数据的随机性. 用蒙特卡洛方法进 ...

  5. Android自动化测试之Shell脚本一——模拟触屏事件

    摘自:http://guolin.guosfamily.com/2012/02/15/android-automated-test-by-shell-touch-event/ Android自动化测试 ...

  6. 一个自带简易数据集的模拟线性分类器matlab代码——实验训练

    一个自带简易数据集的模拟线性分类器matlab代码--实验训练 %%%% Tutorial on the basic structure of using a planar decision boun ...

  7. 杨氏干涉的模拟的MATLAB仿真

    % 杨氏干涉的模拟  close all  figure('position',[217 266 694 244]);   set(gcf,'DoubleBuffer','on');  axes('p ...

  8. 计算机如何改变沟通方式,雅思阅读模拟题:计算机改变沟通方式

    下面新东方网雅思频道与大家分享一篇雅思阅读模拟题,这篇雅思阅读模拟试题的主要内容是简单介绍了计算机在改变人类的沟通方式方面的作用,还有这种改变的主要内容是什么,人们的沟通方式是如何改变的,有什么样的过 ...

  9. 2021-04-09 随机模拟—蒙特卡洛方法 Matlab代码实现

    随机模拟-蒙特卡洛方法 Matlab代码实现 蒙特卡洛方法 蒙特卡洛方法(Monte Carlo method),也称统计模拟方法,是二十世纪四十年代中期由于科学技术的发展和电子计算机的发明,而被提出 ...

最新文章

  1. 【解决问题】IDEA配置Tomcat添加Deployment时没有Artifact
  2. Springboot 配置文件的加载位置以及优先级和外部配置文件加载的优先级
  3. javaWeb(入门基础详解)
  4. 计算机硬盘修复教程,DiskGenius数据恢复教程 教你电脑硬盘数据如何恢复
  5. Pytorch register_forward_hook()简单用法
  6. weblogic部署启动时报错(weblogic.application.ModuleException)
  7. 05 基本数据类型+五大数据类型
  8. 34、linux shell,常用函数strace
  9. 人工智能产生式系统实验—动物识别系统(txt文件导入数据库)python代码实现以及关系图
  10. MySQL的登陆【数据库系统】
  11. Permission denied (publickey).../RPC failed; curl 92 HTTP/2 stream 0 was.../pack exceeds maximum all
  12. 女生适合学大数据开发吗,女生怎样学大数据开发
  13. canvas火焰效果
  14. http://jackwang1.blog.163.com/blog/static/39534478201182651610201/
  15. QTcreater学习笔记
  16. QQ、YY与webRTC回声消除效果对比分析与展望
  17. 苹果笔记本python_python实现在mac笔记本上更换桌面背景
  18. OpenCV中拆分通道、合并通道、alpha通道的讲解及实战演示(附python源码 超详细)
  19. 如何提高神经网络的泛化能力?八大要点掌握
  20. ISP Tuning—高通Chromatix6

热门文章

  1. python之import子目录文件
  2. 【VS开发】图像颜色
  3. 《三层架构之抽象工厂加反射》纠错
  4. BZOJ 3531[Sdoi2014]旅行
  5. CentOS 特殊变量($0、$1、$2、 $?、 $# 、$@、 $*)
  6. == 与 equals 的区别
  7. ABySS非root权限安装
  8. ld cannot find an existing library
  9. java总结第四次//常用类
  10. Frameset使用教程