ZOJ - 4054 Traveling on the Axis(ACM-ICPC 2018 青岛赛区网络预赛 H)(思维)

版权声明:Why is everything so heavy? https://blog.csdn.net/lzc504603913/article/details/82740886

Traveling on the Axis


Time Limit: 1 Second      Memory Limit: 65536 KB


BaoBao is taking a walk in the interval  on the number axis, but he is not free to move, as at every point  for all , where  is an integer, stands a traffic light of type  ().

BaoBao decides to begin his walk from point  and end his walk at point  (both  and  are integers, and ). During each unit of time, the following events will happen in order:

  1. Let's say BaoBao is currently at point , he will then check the traffic light at point . If the traffic light is green, BaoBao will move to point ; If the traffic light is red, BaoBao will remain at point .

  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  as the total units of time BaoBao needs to move from point  to point . For some reason, BaoBao wants you to help him calculate

where both  and  are integers. Can you help him?

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first and only line contains a string  (, ,  for all ), indicating the types of the traffic lights. If , the traffic light at point  is of type 0 and is initially red; If , the traffic light at point  is of type 1 and is initially green.

It's guaranteed that the sum of  of all test cases will not exceed .

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 , , , ,  and , so the answer is .

For the second sample test case, it's easy to calculate that , , , ,  and , so the answer is .


Author: WENG, Caizhi
Source: The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

Submit    Status

题意:走红绿灯,红绿灯每秒变一次,问走到最后要多少秒。求的是从任一点走到最后的和。

解题思路:首先先预处理从开始走到每一个点所需要的时间,然后求一个后缀和。对于任一点,它第一个红绿灯所需要的时间要么是1s要么是2s,所以根据这个判断,用后缀和减去多出来的时间就好了。

#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <queue>
using namespace std;
const int MAXN = 100010;
const int INF = 0x3f3f3f3f;
typedef long long ll;

int a[MAXN];
ll sum[MAXN];
char str[MAXN];
int main()
{
    int TT;
    scanf("%d",&TT);
    while (TT--)
    {
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        scanf("%s",str);
        int len=strlen(str);

        ll cur=0;
        bool cha=0;
        for(int i=0;i<len;i++){
            if(str[i]=='1'&&cha==0){
                cur++;
                a[i+1]=cur;
                cha=1;
                continue;
            }
            if(str[i]=='1'&&cha==1){
                cur++;
                cur++;
                a[i+1]=cur;
                continue;
            }
            if(str[i]=='0'&&cha==0){
                cur++;
                cur++;
                a[i+1]=cur;
                continue;
            }
            if(str[i]=='0'&&cha==1){
                cur++;
                a[i+1]=cur;
                cha=0;
                continue;
            }
        }

        for(int i=len;i>=1;i--)
            sum[i]=sum[i+1]+a[i];
        
        ll ans=0;
        for(int i=1;i<=len;i++){

            if(str[i-1]=='0'){
                ll sub=a[i]-2;
                ans+=sum[i]-sub*(len-i+1);
            }
            else{
                ll sub=a[i]-1;
                ans+=sum[i]-sub*(len-i+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/82740886