2.6总结

总结一下今天最后三道测试题。

Paranoid String

Let's call a binary string T of length m indexed from 11 to m paranoid if we can obtain a string of length 11 by performing the following two kinds of operations m−1 times in any order :
Select any substring of T that is equal to 01, and then replace it with 1.
Select any substring of T that is equal to 10, and then replace it with 0.
For example, if T= 001, we can select the substring [ T2 T3] and perform the first operation. So we obtain T= 01.
You are given a binary string S of length n indexed from 11 to n. Find the number of pairs of integers ( l, r) 1≤ lrn such that S[ lr] (the substring of S from l to r) is a paranoid string.

Input

The first line contains an integer t (1≤ t≤1000) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (1≤ n≤2⋅105) — the size of S.
The second line of each test case contains a binary string S of n characters 12… S1 S2… Sn. ( Si= 0 or Si= 1 for each 1≤ in)
It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case, output the number of pairs of integers( l, r) 1≤ lrn such that S[ lr] (the substring of S from l to r) is a paranoid string.

Sample 1

Inputcopy

Outputcopy

5

1

1

2

01

3

100

4

1001

5

11111

1

3

4

8

5

Note

In the first sample, S already has length 11 and doesn't need any operations.
In the second sample, all substrings of S are paranoid. For the entire string, it's enough to perform the first operation.
In the third sample, all substrings of S are paranoid except [ S2 S3], because we can't perform any operations on it, and [ S1 S2 S3] (the entire string).

思路

题目大意就是给定一个字符串,可以通过两种操作011或者100把字符串子串换成一个数,求有多少个这样的子串。我们可以发现, 每次替换保留的是后面一个字符, 如果每个字符是交替出现的, 1010 这样, 一定是可以变成长度为 1 的串的, 但是如果存在 100 , 011 无论怎么替换都无法消去连续的 1 或 0 所以只需要计算最后两位不相同的字符串数量即可。来自大佬的解释,代码还是挺简单的。

#include<stdio.h>
#define MAX 200010
long long int n;
char s[MAX];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
       scanf("%lld",&n);
       scanf("%s",s);
       long long int count=n;
       for(int i=1;i<n;i++)
        if(s[i]!=s[i-1])
        count+=i;
       printf("%lld\n",count);
    }
}

Directional Increase

We have an array of length n. Initially, each element is equal to 00 and there is a pointer located on the first element.

We can do the following two kinds of operations any number of times (possibly zero) in any order:

  1. If the pointer is not on the last element, increase the element the pointer is currently on by 11. Then move it to the next element.

  1. If the pointer is not on the first element, decrease the element the pointer is currently on by 11. Then move it to the previous element.

But there is one additional rule. After we are done, the pointer has to be on the first element.

You are given an array a. Determine whether it's possible to obtain a after some operations or not.

Input

The first line contains a single integer t (1≤t≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the size of array a.

The second line of each test case contains n integers 1,2,…,a1,a2,…,an (−109≤ai≤109) — elements of the array.

It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅105.

Output

For each test case, print "Yes" (without quotes) if it's possible to obtain a after some operations, and "No" (without quotes) otherwise.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

Sample 1

Inputcopy

Outputcopy

7

2

1 0

4

2 -1 -1 0

4

1 -4 3 0

4

1 -1 1 -1

5

1 2 3 4 -10

7

2 -1 1 -2 0 0 0

1

0

No

Yes

No

No

Yes

Yes

Yes

Note

In the first test case we can obtain the array after some operations, but the pointer won't be on the first element.

One way of obtaining the array in the second test case is shown below.

⟨0,0,0,0⟩→⟨1,0,0,0⟩→⟨1,−1,0,0⟩→⟨2,−1,0,0⟩→⟨2,0,0,0⟩→⟨2,0,−1,0⟩→⟨2,−1,−1,0⟩

思路

这个题目看着就好麻烦,不想解释题意,太长了。但是找到规律就会变得简单,着规律也忒难找了吧。首先我们可以把给一个非首位的数 +1 看作右移一步, 一个数 −1看作左移一步, 所以我们可以考虑计算前缀和, 当前缀和小于 0 时直接 NONO , 因为相当于从起点往左移, 由于首位只能 +1 , 所以当回到起点后, 我们一定不能再右移, 所以当 count=0 , 后面的数必须全部等于 0 , 不然就 NO

代码

#include<stdio.h>
#define MAX 200010
long long int a[MAX];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long int n,i,flag=0,flag1=0,count=0;
        scanf("%lld",&n);
        for(i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(i=1;i<=n;i++)
        {
            if(flag==1&&a[i]!=0)
            {
                printf("NO\n");
                flag1=1;
               break;
            }
            count+=a[i];
            if(count<=0)
                flag=1;
        }
        if(flag1==1)
           continue;
        if(count!=0)
            printf("NO\n");
        else
            printf("YEs\n");
    }
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值