Codeforces Round #719 (Div. 3)/ Codeforces Round #720 (Div. 2)

A. Do Not Be Distracted!

题意:

一件事情一但开始,只能做完才能做别的事,当出现一件事不连续出现时,教师会怀疑

题目:

Polycarp has 26 tasks. Each task is designated by a capital letter of the Latin alphabet.

The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task.

Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice.

For example, if Polycarp solved tasks in the following order: “DDBBCCCBBEZ”, then the teacher will see that on the third day Polycarp began to solve the task ‘B’, then on the fifth day he got distracted and began to solve the task ‘C’, on the eighth day Polycarp returned to the task ‘B’. Other examples of when the teacher is suspicious: “BAB”, “AABBCCDDEEBZZ” and “AAAAZAAAAA”.

If Polycarp solved the tasks as follows: “FFGZZZY”, then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn’t have any suspicious: “BA”, “AFFFCC” and “YYYYY”.

Help Polycarp find out if his teacher might be suspicious.

Input

The first line contains an integer t (1≤t≤1000). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤50) — the number of days during which Polycarp solved tasks.

The second line contains a string of length n, consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks.

Output

For each test case output:

“YES”, if the teacher cannot be suspicious;
“NO”, otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

Example
input

5
3
ABA
11
DDBBCCCBBEZ
7
FFGZZZY
1
Z
2
AB

output

NO
NO
YES
YES
YES

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
typedef long long ll;
int t,n,m;
int main(){
    string s;
    cin>>t;
    while(t--){
        cin>>n>>s;
        int flag=0;
        int book[30];
        memset(book,0,sizeof(book));
        for(int i=0;i<n;i++){
            while(s[i]==s[i+1])
                i++;
            //cout<<<<endl;
            if(book[s[i]-'A'+1]==1){
                flag=1;
                break;
            }
            book[s[i]-'A'+1]=1;
        }
        flag==1?cout<<"NO"<<endl:cout<<"YES"<<endl;
    }
}

B. Ordinary Numbers

题意:

给你一个数n,问(1~n)中有多少个,每个数位都相等的数?

题目:

Let’s call a positive integer n ordinary if in the decimal notation all its digits are the same. For example, 1, 2 and 99 are ordinary numbers, but 719 and 2021 are not ordinary numbers.

For a given number n, find the number of ordinary numbers among the numbers from 1 to n.

Input

The first line contains one integer t (1≤t≤104). Then t test cases follow.

Each test case is characterized by one integer n (1≤n≤109).

Output

For each test case output the number of ordinary numbers among numbers from 1 to n.

Example
input

6
1
2
3
4
5
100

output

1
2
3
4
5
18

AC代码:

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n,i,j,count=0;
        cin>>n;
        for(i=1; i<=n; i=i*10+1)
        {
            for(j=1; j<=9; j++)
                if(i*j<=n){
                    count++;
                    //cout<<i<<"***"<<j<<endl;
                }

        }
        cout<<count<<endl;
    }
}

C. Not Adjacent Matrix

题意:

给你一个n*n的方格,向里面放置(1~ n ∗ n n*n nn)的数,问如何放,使得任意格子的上下左右格子的差值不为一。

题目:

We will consider the numbers a and b as adjacent if they differ by exactly one, that is, |a−b|=1.

We will consider cells of a square matrix n×n as adjacent if they have a common side, that is, for cell (r,c) cells (r,c−1), (r,c+1), (r−1,c) and (r+1,c) are adjacent to it.

For a given number n, construct a square matrix n×n such that:

Each integer from 1 to n2 occurs in this matrix exactly once;
If (r1,c1) and (r2,c2) are adjacent cells, then the numbers written in them must not be adjacent.

Input

The first line contains one integer t (1≤t≤100). Then t test cases follow.

Each test case is characterized by one integer n (1≤n≤100).

Output

For each test case, output:

-1, if the required matrix does not exist;
the required matrix, otherwise (any such matrix if many of them exist).
The matrix should be outputted as n lines, where each line contains n integers.

Example
input

3
1
2
3

output

1
-1
2 9 7
4 6 3
1 8 5

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        if(n == 2)
            cout<<"-1\n";
        else
        {
            for(int i=1; i<=n*n; i+=2)//交叉放置即可
                cout<<i<<' ';
            for(int i=2; i<=n*n; i+=2)
                cout<<i<<' ';
            cout<<endl;
        }
    }

    return 0;
}

D. Same Differences

题意:

给你一个n个整数的数组,问数组中满足i<j and a j − a i = j − i , a_{j}-a_{i} = j-i, ajai=ji.有多少个?

题目:

You are given an array a of n integers. Count the number of pairs of indices (i,j) such that i<j and a j − a i = j − i , a_{j}-a_{i} = j-i, ajai=ji.

Input

The first line contains one integer t (1≤t≤104). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2⋅105).

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case output the number of pairs of indices (i,j) such that i<j and aj−ai=j−i.

Example
input

4
6
3 5 1 4 6 6
3
1 2 3
4
1 3 3 4
6
1 6 3 4 5 6

output

1
3
3
10

分析:

让我们重写一下原始的相等性:
a j − a i = j − i , a_{j}-a_{i} = j-i, ajai=ji
a j − j = a i − i a_{j}-j = a_{i}-i ajj=aii
让我们用 b i = a i − i b_{i} = a_{i}-i bi=aii替换每个 a i a_{i} ai。 那么答案是对(i,j)的对数,使得i <j并且 b i = b j b_{i} = b_{j} bi=bj。 要计算此值,可以使用地图或排序。

AC代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        map<int,int> mp;
        cin>>n;
        long long ans=0;
        for(int i=1; i<=n; i++)
        {
            int x;
            cin>>x;
            ans+=mp[x-i];
            mp[x-i]++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

H - Arranging The Sheep

题意:

给你一个字符串,表示一排中🐏羊的站位,对羊进行移动,使得羊之间没有空隙,问最小的移动数?

题目:

You are playing the game “Arranging The Sheep”. The goal of this game is to make the sheep line up. The level in the game is described by a string of length n, consisting of the characters ‘.’ (empty space) and ‘*’ (sheep). In one move, you can move any sheep one square to the left or one square to the right, if the corresponding square exists and is empty. The game ends as soon as the sheep are lined up, that is, there should be no empty cells between any sheep.

For example, if n=6 and the level is described by the string “**.*…”, then the following game scenario is possible:

the sheep at the 4 position moves to the right, the state of the level: “.";
the sheep at the 2 position moves to the right, the state of the level: "
...";
the sheep at the 1 position moves to the right, the state of the level: ".
..";
the sheep at the 3 position moves to the right, the state of the level: ".
..";
the sheep at the 2 position moves to the right, the state of the level: "…
*.”;
the sheep are lined up and the game ends.
For a given level, determine the minimum number of moves you need to make to complete the level.

Input

The first line contains one integer t (1≤t≤104). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤106).

The second line of each test case contains a string of length n, consisting of the characters ‘.’ (empty space) and ‘*’ (sheep) — the description of the level.

It is guaranteed that the sum of n over all test cases does not exceed 106.

Output

For each test case output the minimum number of moves you need to make to complete the level.

Example
Input
5
6
**.*..
5
*****
3
.*.
3
...
10
*.*...*.**
Output

1
0
0
0
9

分析:

因为最终是让所有的羊在一起,那么处在最中间的羊,动一步,都不是最少的移动,所以对其他羊来说,就都往中间羊靠拢即可。
在这里插入图片描述

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int M=1e6+10;
int a[1000010];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int sum=0;
        for(int i=0; i<n; i++)
            if(s[i]=='*')
                a[sum++]=i;
        long long  ans=0;
        int m=sum/2;
        for(int i=0; i<sum; i++)
        	ans+=abs(a[i]-a[m])-abs(i-m);//第i只羊离中间羊的距离;
        cout<<ans<<endl;
    }
    return 0;
}

A - Nastia and Nearly Good Numbers

题意:

给你两个数A和B,问是否存在两个能够整除A,不能整除ab的数x,y,及一个x+y能够整除ab的数z。

题目:

Nastia has 2 positive integers A and B. She defines that:

The integer is good if it is divisible by A⋅B;
Otherwise, the integer is nearly good, if it is divisible by A.
For example, if A=6 and B=4, the integers 24 and 72 are good, the integers 6, 660 and 12 are nearly good, the integers 16, 7 are neither good nor nearly good.

Find 3 different positive integers x, y, and z such that exactly one of them is good and the other 2 are nearly good, and x+y=z.

Input

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

The first line of each test case contains two integers A and B (1≤A≤106, 1≤B≤106) — numbers that Nastia has.

Output

For each test case print:

“YES” and 3 different positive integers x, y, and z (1≤x,y,z≤1018) such that exactly one of them is good and the other 2 are nearly good, and x+y=z.
“NO” if no answer exists.
You can print each character of “YES” or “NO” in any case.
If there are multiple answers, print any.

Example
Input

3
5 3
13 2
7 11

Output

YES
10 50 60
YES
169 39 208
YES
28 154 182

Note

In the first test case: 60 — good number; 10 and 50 — nearly good numbers.

In the second test case: 208 — good number; 169 and 39 — nearly good numbers.

In the third test case: 154 — good number; 28 and 182 — nearly good numbers.

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
ll n,m;
int main(){
    int t;
    cin>>t;
    while(t--){
       cin>>n>>m;
       if(m==1)
            cout<<"NO"<<endl;
       else{
        cout<<"YES"<<endl;
                 printf("%lld %lld %lld\n",n,m*n,(m+1)*n);//相邻两个数互质
       }
    }
    return 0;
}

B - Nastia and a Good Array

题意:

给你一个包含n个整数的数组,现在经过操作:
1.每次选取两个整数, a i , a j a_{i},a_{j} ai,aj,以及在(1~ 2 ∗ 1 0 9 2*10^{9} 2109)中找到两个数x,y;
2.满足 m i n ( a i , a j ) = m i n ( x , y ) min(a_{i},a_{j})=min(x,y) min(ai,aj)=min(x,y)
3.最后让 a i = x , a j = y a_{i}=x,a_{j}=y ai=x,aj=y
使得最终的序列,任意相邻的两个数都互质。

题目·:

Nastia has received an array of n positive integers as a gift.

She calls such an array a good that for all i (2≤i≤n) takes place gcd(ai−1,ai)=1, where gcd(u,v) denotes the greatest common divisor (GCD) of integers u and v.

You can perform the operation: select two different indices i,j (1≤i,j≤n, i≠j) and two integers x,y (1≤x,y≤2⋅109) so that min(ai,aj)=min(x,y). Then change ai to x and aj to y.

The girl asks you to make the array good using at most n operations.

It can be proven that this is always possible.

Input

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

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

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the array which Nastia has received as a gift.

It’s guaranteed that the sum of n in one test doesn’t exceed 2⋅105.

Output

For each of t test cases print a single integer k (0≤k≤n) — the number of operations. You don’t need to minimize this number.

In each of the next k lines print 4 integers i, j, x, y (1≤i≠j≤n, 1≤x,y≤2⋅109) so that min(ai,aj)=min(x,y) — in this manner you replace ai with x and aj with y.

If there are multiple answers, print any.

Example
Input

2
5
9 6 3 11 15
3
7 5 13

Output

2
1 5 11 9
2 5 7 6
0

Note

Consider the first test case.

Initially a=[9,6,3,11,15].

In the first operation replace a1 with 11 and a5 with 9. It’s valid, because min(a1,a5)=min(11,9)=9.

After this a=[11,6,3,11,9].

In the second operation replace a2 with 7 and a5 with 6. It’s valid, because min(a2,a5)=min(7,6)=6.

After this a=[11,7,3,11,6] — a good array.

In the second test case, the initial array is already good.

分析:

由于不要求最优,我们可以找每个间隔都放置一个质数 1 e 9 + 7 1e^{9}+7 1e9+7;

AC代码:

#include<stdio.h>
#include<iostream>
using namespace std;
const int mod=1e9+7;
const int M=1e5+10;
int a[M];
int main(){
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        cout<<n/2<<endl;
        for(int i=1;i<n;i+=2){
            printf("%d %d %d %d\n",i,i+1,min(a[i],a[i+1]),mod);
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值