zoukankan      html  css  js  c++  java
  • The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    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 (i0.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 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 (1s10​^5​​, s=n, si​​{‘0’,‘1’} for all 1is∣), indicating the types of the traffic lights. If si​​=‘0’, the traffic light at point (i0.5) is of type 0 and is initially red; If si​​=‘1’, the traffic light at point (i0.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​​.

    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
    ",j,ans);
                    //    printf("]%d
    ",ans);
               //     printf("--> (f1=%d f2=%d)
    ",f1,f2);
                //    printf("num1=%d num2=%d Time=%d &ans=%d
    ",num1,num2,++Time,ans);
                }
                printf("%d
    ",ans);
            }
    
        }
    
        return 0;
    }
    View Code(有大量注释)
  • 相关阅读:
    05-java学习-循环结构
    04-java学习-选择结构
    03-java学习-基本数据类型-运算符-键盘接收用户输入
    A02-java学习-classpath配置-标识符-java变量类型
    A01-java学习环境准备
    20190215面试-C#操作外设-多线程-shocket
    装饰者模式
    状态模式
    DllImport学习
    网络编程(一)----基础知识、数据流套接字
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/9676664.html
Copyright © 2011-2022 走看看