Codeforces Round #719 (Div. 3)

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
inputCopy
5
3
ABA
11
DDBBCCCBBEZ
7
FFGZZZY
1
Z
2
AB
outputCopy
NO
NO
YES
YES
YES

如果存在已经出现的连续字母段则输出NO
所以用map标记看是否出现过连续字母

#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int p=1;
		map<char,int>m;
		int n;
		cin>>n;
		char a[100];
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		for(int i=0;i<n;i++)
		{
			if(m[a[i]]!=0&&a[i]!=a[i-1])
			{
				p=0;
				break;
			}	
			//cout<<m[a[i]];
			//cout<<a[i]<<endl;
			m[a[i]]++;
		
			/**/
		}
		if(p==1)
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
} 

用的方法比较复杂,用map做了标记,然后统计

B - Ordinary Numbers

B. Ordinary Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
6
1
2
3
4
5
100
outputCopy
1
2
3
4
5
18

这题可以看出1到9为9个普通数,10到99也为9个普通数;100到999也为9个普通数,本人比较懒,就直接列举到10^9了;

#include<iostream>
using namespace std;
 
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long n;
		cin>>n;
		if(n<=9)
		{
			cout<<n<<endl;
		}
		else if(n<=99)
		{
			cout<<n/11+9<<endl;
		}
		else if(n<=999)
		{
			cout<<n/111+18<<endl;
		}
		else if(n<=9999)
		{
			cout<<n/1111+27<<endl;
		}
		else if(n<=99999)
		{
			cout<<n/11111+36<<endl;
		}
		else if(n<=999999)
		{
			cout<<n/111111+45<<endl;
		}
		else if(n<=9999999)
		{
			cout<<n/1111111+54<<endl;
		}
		else if(n<=99999999)
		{
			cout<<n/11111111+63<<endl;
		}
		else if(n<=999999999)
		{
			cout<<n/111111111+72<<endl;
		}
		else if(n<=9999999999)
		{
			cout<<n/1111111111+81<<endl;
		}
	}
}

这题做的非常暴力,不过还是过了。

C - Not Adjacent Matrix

C. Not Adjacent Matrix
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
3
1
2
3
outputCopy
1
-1
2 9 7
4 6 3
1 8 5

n=2时,无论如何构造都会有相邻的矩阵;
其他时候只要不断加2就可以不相邻,而超过n*n的时候就从2开始不断加2;

#include<iostream>
using namespace std;
int main()
{
	int n,t;
	cin>>t;
	while(t--)
	{
		int f=1;
		cin>>n;
		if(n==2)
		cout<<-1<<endl;
		else
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++)
				{	
					if(f>n*n)
					{
						if((f-n*n)%2==0)
						cout<<f-n*n<<" ";
						else
						cout<<f-n*n+1<<" ";
					}
					else
					cout<<f<<" ";
					f+=2;
					
				}
				cout<<endl;
			}
		}
	}
}

这题先输出奇数后输出偶数,只有2不行。

D - Same Differences

D. Same Differences
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a of n integers. Count the number of pairs of indices (i,j) such that i<j and aj−ai=j−i.

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
inputCopy
4
6
3 5 1 4 6 6
3
1 2 3
4
1 3 3 4
6
1 6 3 4 5 6
outputCopy
1
3
3
10

变换公式,aj−ai=j−i→aj−j=ai−i

所以我们可以存储 ai−i 的值,计算有多少相同的ai-i,然后进行组合数计算相同个数超过2的,例如:相同个数为2那就是一种,相同个数为3的就是1+2为3种,相同个数为4的那就是1+2+3为6种依次类推为n*(n-1)/2;

#include<iostream>
#include<map>
using namespace std;
int main()
{
	long long t;
	cin>>t;
	long long a[200005];
	map<long long,long long>m;
	while(t--)
	{
		long long n;
		cin>>n;
		for(long long i=0;i<n;i++)
		{
			cin>>a[i];
			m[a[i]-i]++;
		}
		map<long long,long long>::iterator it;
		long long h=0;
		long long sum=0;
		for(it=m.begin();it!=m.end();it++)
		{
			if(it->second>=2)
			{
				h=it->second;
				sum+=h*(h-1)/2;
			}
			
		}
		cout<<sum<<endl;
		m.clear();
	}
}

一开始考虑条件漏了还有其他情况。
后来又用map做标记

这场Div. 3题意好理解,喜欢这种题,终究还是菜了,后面再慢慢补题吧。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值