codeforces round 644 div3

题目链接
A
B
C
D
E

A. Minimal Square

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Find the minimum area of a square land on which you can place two identical rectangular × houses. The sides of the houses should be parallel to the sides of the desired square land.

Formally,

You are given two identical rectangles with side lengths and (1≤,≤100) — positive integers (you are given just the sizes, but not their positions).
Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square.
Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding.

The picture shows a square that contains red and green rectangles.
Input
The first line contains an integer (1≤≤10000) —the number of test cases in the input. Then test cases follow.

Each test case is a line containing two integers , (1≤,≤100) — side lengths of the rectangles.

Output
Print answers to the test cases. Each answer must be a single integer — minimal area of square land, that contains two rectangles with dimensions ×.

Example
inputCopy
8
3 2
4 2
1 1
3 1
4 7
1 3
7 4
100 100
outputCopy
16
16
4
9
64
9
64
40000
Note
Below are the answers for the first two test cases:

题意:给你两个个边长为m和n的矩形,问需要多大一个正方形,才可以将两个矩形放下去,且正方形每条边都靠着矩形的边

题解:比较m,n的最小值min,和最大值max,比较2*min和max,取最大,即为正方形边长

代码:

#include
#include
using namespace std;
typedef long long ll;
int a,b;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int ans,c,d;
		cin>>a>>b;
		c=min(a,b);
		d=max(a,b);
		ans=max(2*c,d);
		cout<<ans*ans<<endl;
	}
	return 0;
}

B. Honest Coach

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are athletes in front of you. Athletes are numbered from 1 to from left to right. You know the strength of each athlete — the athlete number has the strength .

You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.

You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams and so that the value |max()−min()| is as small as possible, where max() is the maximum strength of an athlete from team , and min() is the minimum strength of an athlete from team .

For example, if =5 and the strength of the athletes is =[3,1,2,6,4], then one of the possible split into teams is:

first team: =[1,2,4],
second team: =[3,6].
In this case, the value |max()−min()| will be equal to |4−3|=1. This example illustrates one of the ways of optimal split into two teams.

Print the minimum value |max()−min()|.

Input
The first line contains an integer (1≤≤1000) — the number of test cases in the input. Then test cases follow.

Each test case consists of two lines.

The first line contains positive integer (2≤≤50) — number of athletes.

The second line contains positive integers 1,2,…, (1≤≤1000), where — is the strength of the -th athlete. Please note that values may not be distinct.

Output
For each test case print one integer — the minimum value of |max()−min()| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.

Example
inputCopy
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
outputCopy
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is =[2,1], =[3,2,4,3], so the answer is |2−2|=0.

题意:将一个数组分成两个,然后使得1数组的最大值,减二数组的最小值最小。

题解:即求解一个有序数组,后一个减前一个的最小值

代码:

#include
#include
using namespace std;
int a[100];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		int ans=0x3f3f3f3f;
		for(int i=2;i<=n;i++)
			ans=min(ans,a[i]-a[i-1]);
		cout<<ans<<endl;
	}
	return 0;
}

C. Similar Pairs

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
We call two numbers and similar if they have the same parity (the same remainder when divided by 2), or if |−|=1. For example, in each of the pairs (2,6), (4,3), (11,7), the numbers are similar to each other, and in the pairs (1,4), (3,12), they are not.

You are given an array of ( is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.

For example, for the array =[11,14,16,12], there is a partition into pairs (11,12) and (14,16). The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.

Input
The first line contains a single integer (1≤≤1000) — the number of test cases. Then test cases follow.

Each test case consists of two lines.

The first line contains an even positive integer (2≤≤50) — length of array .

The second line contains positive integers 1,2,…, (1≤≤100).

Output
For each test case print:

YES if the such a partition exists,
NO otherwise.
The letters in the words YES and NO can be displayed in any case.

Example
inputCopy
7
4
11 14 16 12
2
1 8
4
1 1 1 1
4
1 2 5 6
2
12 13
6
1 6 3 10 5 8
6
1 12 3 10 5 8
outputCopy
YES
NO
YES
YES
YES
YES
NO
Note
The first test case was explained in the statement.

In the second test case, the two given numbers are not similar.

In the third test case, any partition is suitable.

题意:相似数组一个二元组里头的东西相差为1,或者是对2取余后结果相同。问一个长的数组可不可以拆成若干个相似数组。

题解:计算出长数组里头的奇偶数,各有多少个,然后再对数组排序,前后作差判断有没有为1的,则说明有一组可以凑成二元组,可消化一种非二元组的情况。判断奇数个数,与偶数个数皆成对存在即可

代码:

#include
#include
using namespace std;
int a[100];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		int b=0,c=0,d=0;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
		{
			if(a[i]&1)
				b++;
			else
				c++;
		}
		for(int i=2;i<=n;i++)
			if(a[i]-a[i-1]==1)
				d=1;
		if((b%2==0 && c%2==0) || ((b-d)%2==0 && (c-d)%2==0))
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}

D. Buying Shovels

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp wants to buy exactly shovels. The shop sells packages with shovels. The store has types of packages: the package of the -th type consists of exactly shovels (1≤≤). The store has an infinite number of packages of each type.

Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly shovels?

For example, if =8 and =7, then Polycarp will buy 2 packages of 4 shovels.

Help Polycarp find the minimum number of packages that he needs to buy, given that he:

will buy exactly shovels in total;
the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from 1 to , inclusive.
Input
The first line contains an integer (1≤≤100) — the number of test cases in the input. Then, test cases follow, one per line.

Each test case consists of two positive integers (1≤≤109) and (1≤≤109) — the number of shovels and the number of types of packages.

Output
Print answers to the test cases. Each answer is a positive integer — the minimum number of packages.

Example
inputCopy
5
8 7
8 1
6 10
999999733 999999732
999999733 999999733
outputCopy
2
8
1
999999733
1
Note
The answer to the first test case was explained in the statement.

In the second test case, there is only one way to buy 8 shovels — 8 packages of one shovel.

In the third test case, you need to buy a 1 package of 6 shovels.

题意:给一个n,和一个k,问n能否由1-k的数构成。最少有由多少个这个数组成。

题解:首先这个数会小于k,其次这个数ii<=n,然后如果能整除i,且ik会大于n的话则,更新答案为i,否则更新答案为n/i;

代码:

#include
#include
#include
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k,a;
		cin>>n>>k;
		int ans=0;
		for(int i=1;i<=k&&i*i<=n;i++)
		{
			if(n%i==0)
			a=n/i;
			if(a<=k)
			{
				ans=i;
				break;
			}
			ans=a;
		}
		if(ans!=0)
			cout<<ans<<endl;
		else
			cout<<n<<endl;
	}
	return 0;
}

E. Polygon

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polygon is not only the best platform for developing problems but also a square matrix with side , initially filled with the character 0.

On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2 cannons were placed.

Initial polygon for =4.
Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.

More formally:

if a cannon stands in the row , to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (,1) and ends in some cell (,);
if a cannon stands in the column , above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1,) and ends in some cell (,).
For example, consider the following sequence of shots:

  1. Shoot the cannon in the row 2. 2. Shoot the cannon in the row 2. 3. Shoot the cannon in column 3.

You have a report from the military training on your desk. This report is a square matrix with side length consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?

Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.

Input
The first line contains an integer (1≤≤1000) — the number of test cases. Then test cases follow.

Each test case starts with a line containing an integer (1≤≤50) — the size of the polygon.

This is followed by lines of length , consisting of 0 and 1 — the polygon matrix after the training.

The total area of the matrices in all test cases in one test does not exceed 105.

Output
For each test case print:

YES if there is a sequence of shots leading to a given matrix;
NO if such a sequence does not exist.
The letters in the words YES and NO can be printed in any case.

Example
inputCopy
5
4
0010
0011
0000
0000
2
10
01
2
00
00
4
0101
1111
0101
0111
4
0100
1110
0101
0111
outputCopy
YES
NO
YES
YES
NO
Note
The first test case was explained in the statement.

The answer to the second test case is NO, since a 1 in a cell (1,1) flying out of any cannon would continue its flight further.

题意:一个发射台从左发射,从上发射,然后问下列哪种方法是发射可以形成的。

题解:
模拟判断

代码:

#include
using namespace std;
const int N=50+5;
char s[N][N];
int main()
{
	int t,n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>s[i]+1;
		int flag=1;
		for(int i=n;i>=1;i--)
		{
			int h=n-i+1;
			for(int j=h;j>=1;j--)
			{
				if(s[h][j]=='1' && !(h==n || j==n || s[h+1][j]=='1' || s[h][j+1]=='1'))
				{
					flag=0;
					break;
				}
			}
			for(int j=h;j>=1;j--)
			{
				if(s[j][h]=='1' && !(h==n || j==n || s[j+1][h]=='1' || s[j][h+1]=='1'))
				{
					flag=0;
					break;
				}
			}
		}
		if(flag)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}

你可能感兴趣的:(codeforces round 644 div3)