测试题 2.6

/

明天学动规,今天就水下测试题,不过今天测试居然拿下了第一qwq



G - Directional Increase

 CodeForces - 1694C 

思路:观察样例可以发现当前k个数和为0时指针无法向后移动,故要保证在指针走到最后一个非0数时刚好sum为0,利用好这个条件就能做出来这题

我们有一个长度为n的数组。最初,每个元素等于0,并且在
first element. 第一要素。
 我们可以按任意顺序执行以下两种操作,任意次数(可能为零):
1.如果指针不在最后一个元素上,则将指针当前上的元素增加1。
然后把它移到下一个元素。
 2.如果指针不在第一个元素上,则将指针当前所在的元素减少1。
但还有一条规则。完成后,指针必须位于第一个元素上。
您将得到一个数组a。确定是否有可能在某些操作之后获得a。
Input 输入
第一行包含一个整数(1<t<1000),一测试用例数。对…的描述
. 下面是测试用例。
a. 每个测试用例的第一行包含一个整数(1<n<2.105),一为数组a的大小。
The second line of each test case contains n integers a1, a....,an(-109 < a; < 109) - elements of 每个测试用例的第二行包含n个整数A1,a.,an(-109<a;<109)-元素
the array. 数组。

Output

For each test case, print "Yes" (without quotes) if it's possible to obtain aa 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

InputcopyOutputcopy
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
#include<bits/stdc++.h>
using namespace std;
long long int sum=0;
int h;
 int s[200005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
            sum=0;
           int flag=0;
        int t;
        scanf("%d",&t);
        for(int i=1;i<=t;i++){
            scanf("%d",&s[i]);
        }
        for(int i=1;i<=t;i++){
            sum+=s[i];
            if(sum<=0&&i<t){flag=1;h=i+1;break;}
        }
    if(sum==0&&flag==1){
            for(h;h<=t;h++){
                if(s[h]!=0)break;
            }
   if(h>t)flag=0;
}
        if(flag==0&&sum==0)printf("YES\n");
        else printf("NO\n");
    }}

E - Paranoid String

 CodeForces - 1694B 

思路:开始想用暴力解题,但还没学动规没好的方法枚举所有子串,后面研究满足条件串的规律因为01变为1   而10变为0,即1左边的0都能去,0左边的1都能去于是只需看子串最后两位就能确定是否为偏执串,满足则有当前位置数的可能串生成,最后加上串长即可,从后向前推也避免了重复。

f 让我们调用长度为m的二进制字符串T,从1到m为偏执狂,如果我们可以获得
: 通过以任意顺序执行以下两种操作m一1次,长度1:
●Select any substring of T that is equal to 01, and then replace it with 1. 选择任何等于01的T子字符串,然后用1替换它。
●Select any substring of T that is equal to 10, and then replace it with 0. 选择等于10的任何T子字符串,然后用0替换它。
如果T=001,我们可以选择子字符串[T2T3]并执行第一个操作。所以
we obtain T= 01. 得到T=01。
 给你一个长度为1到n的二进制字符串S。找到整数对的数目。
(I,r)1 <l< r < n such that S[l ... r] (the substring of S froml to r) is a paranoid string. (i,r)1<l<r<n,使S[l].R](从S到r的子字符串)是偏执字符串。
nput 纳普特
t 第一行包含一个整数(1<t<1000)--测试用例的数量。测试描述
cases follows. 案件如下。
The first line of each test case contains a single integern(1 < n < 2.10)- the size of S. 每个测试用例的第一行包含一个整数(1<n<2.10),即S的大小。
The second line of each test case contains a binary string S of n characters S1S2...Sn.(S:=0or S; = 每个测试用例的第二行包含n个字符S1S2.Sn的二进制字符串S。(s:=0或S;=
1foreach1<i<n) 1前景1<i<n)
It is guaranteed that the sum of n over all test cases doesn't exceed2.105. 保证所有测试用例上n的和不会超过2.105。

Output 输出量
For each test case, output the number of pairs of integers(l,r)1 <l< r < n such that S[l...r] (the 对于每个测试用例,输出整数对数(l,r)1<l<r<n,使S[l.r](
substring of S froml to r) is a paranoid string. 从S到r的子字符串是一个偏执字符串。

Sample 1

InputcopyOutputcopy
5
1
1
2
01
3
100
4
1001
5
11111
1
3
4
8
5

In the first sample, S already has length 1 and doesn't need any operations. 在第一个示例中,S已经有长度1,不需要任何操作。
In the second sample, all substrings of S are paranoid. For the entire string, it's enough to perform the 在第二个样本中,S的所有子串都是偏执的。对于整个字符串,只需执行
first operation. 第一次手术
In the third sample, all substrings of S are paranoid except [S2 S3],because we can't perform any 在第三个示例中,除了[S2 S3]之外,S的所有子字符串都是偏执的,因为我们不能执行任何
operations on it, and [S1 S2S3] (the entire string). 操作,以及[S1 S2S3](整个字符串)。

#include<bits/stdc++.h>
using namespace std;
long long int sum=0;
 char s[200005];
void dfs(int l,int r)
{
    if(l>r)return ;
    if(r==l)sum++;
    else {
        if(s[r]!=s[r-1])sum++;
    }
    dfs(l+1,r);
    dfs(l,r-1);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
            sum=0;
        int t;
        scanf("%d",&t);
        getchar();
        scanf("%s",s);
        for(int i=t-1;i>0;i--){
            if(s[i]!=s[i-1])sum+=i;
        }
        if(t==1)printf("1\n");
        else
        printf("%lld\n",sum+t);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值