The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online - H Traveling on the Axis-【思维模拟题目】

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. 其余的见代码的注释吧!手推样例还是很重要的!


真数据:

真测试输入
4
10100101010101010101010101011110101010101
111111111111111110100101010101010101010101011110101010101
1111111111111111111
00000000000000000000000000

真测试输出

13951
41543
2470
6552

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

代码;

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<string>
#include<string.h>
#include<time.h>
#include<algorithm>
using namespace std;
const double eps=1e-7;
#define PI acos(-1.0)
#define ll long long
#define mian main
#define mem(a,b) memset(a,b,sizeof(a))

char a[100005];
int b[100005][2];
int main()
{

    int T;
    while(scanf("%d",&T)!=EOF)
    {
        while(T--)
        {
            scanf("%s",a);
            int len=strlen(a);
            for(int i=0; i<len; i++)
            {
                b[i][0]=a[i]-‘0‘;  ///原数据层
                b[i][1]=!b[i][0];  ///红绿灯交替一遍后!若红绿灯再交替一遍其实就是上一层!
            }

            int Time=0;
            int  ans=0,k=0;
            int f1=0,f2=-1;//表示当前层数0或者1

            int num1=0,num2=0;
            for(int k=0; k<len; k++)  ///暴力枚举一遍len个区间即可!!
            {
                if(f1==f2)
                {
                    f2=-1;
                    num1+=num2;
                    num2=0;
                }
                num1-=k;  ///这里是个终点,每往后走一个,会少k个前面的区间覆盖到当前区间的,必须减去
             //   printf(" (f1=%d f2=%d) ",f1,f2);
                if(f1==0) ///分配每轮的萌新层,与f1层相符就加进f1里
                {
                    num1+=len-k;
                }
                else     ///否则就新开一个f2来进行记录!
                {
                    if(f2==-1)
                        f2=0;
                    num2+=len-k;
                }

                if(b[k][f1]==1)
                {
                    ans+=1*(num1 ),f1=!f1;
                }
                else
                    ans+=2*(num1 );
                if(f2!=-1)
                {
                    if(b[k][f2]==1)
                    {
                        ans+=1*(num2 ),f2=!f2;
                    }
                    else
                        ans+=2*(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 0;
}

(有大量注释)

原文地址:https://www.cnblogs.com/zhazhaacmer/p/9676664.html

时间: 2024-11-06 07:36:03

The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online - H Traveling on the Axis-【思维模拟题目】的相关文章

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 (Gym - 102082)

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 A - Digits Are Not Just Characters 签到. B - Arithmetic Progressions 题意:从给定的集合中选出最多的数构成等差数列. 题解:数字排序后,设\(dp[i][j]\)表示等差数列最后一个数字为\(a[i]\),倒数第二个数字为\(a[j]\)的最大个数.然后对于每一位枚举 \(i\),\(lower\_bound()\)找有无合法的

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Krak&#243;w

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik's RectangleProblem B: What does the fox say?Problem C: Magical GCDProblem D: SubwayProblem E: EscapeProblem F: DraughtsProblem G: History courseProblem H: C

The 2018 ACM-ICPC Asia Qingdao Regional Contest K XOR Clique

K XOR Clique BaoBao has a sequence a?1??,a?2??,...,a?n??. He would like to find a subset S of {1,2,...,n} such that ?i,j∈S, a?i??⊕a?j??<min(a?i??,a?j??) and ∣S∣ is maximum, where ⊕ means bitwise exclusive or. Input There are multiple test cases. The

2018 ICPC Asia Jakarta Regional Contest

题目传送门 题号 A B C D E F G H I J K L 状态 Ο . . Ο . . . . Ο . . Ο Ο:当场 Ø:已补 .  :  待补 A. Edit Distance Thinking:kk pai爷 Code:kk 不能直接反转,比如"010101",直接反转后就变成"101010",右移一位,然后加个0就可以了. 所以要先统计01的数量,如果0大于1,就全变成1,1大于0,就全变成0(从数量上的改变就大于s/2了),相等的话,就看首位是0

The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

XOR Clique Press the Button Kuririn MIRACLE Traveling on the Axis Couleur Chaleur Infinite Parenthesis Sequence Pixel Art Halting Problem Red Black Tree Live Love 原文地址:https://www.cnblogs.com/Accpted/p/11298226.html

2019-2020 ICPC, Asia Jakarta Regional Contest H. Twin Buildings

As you might already know, space has always been a problem in ICPC Jakarta. To cope with this, ICPC Jakarta is planning to build two new buildings. These buildings should have a shape of a rectangle of the same size. Now, their problem is to find lan

2019-2020 ICPC, Asia Jakarta Regional Contest C. Even Path

Pathfinding is a task of finding a route between two points. It often appears in many problems. For example, in a GPS navigation software where a driver can query for a suggested route, or in a robot motion planning where it should find a valid seque

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his