Codeforces Round #644 (Div. 3) A,C,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 a×b 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 a and b (1≤a,b≤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 t (1≤t≤10000) —the number of test cases in the input. Then t test cases follow.

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

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

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:

思路:自己做的太急了,到时浪费时间 难受

while (t--)
	{
		int a, b; cin >> a >> b;
		
			int y = max(a, b);
			int x = min(a, b);
			if (x*2>y)
				cout << 4 * x * x << endl;
			else cout << y * y << endl;
	
	}

C. Similar Pairs
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
We call two numbers x and y similar if they have the same parity (the same remainder when divided by 2), or if |x−y|=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 a of n (n 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 a=[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 t (1≤t≤1000) — the number of test cases. Then t test cases follow.

Each test case consists of two lines.

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

The second line contains n positive integers a1,a2,…,an (1≤ai≤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.
思路:自己想多了这个题,其实很好想 奇数和偶数的个数不一定相等但是他们的个数ji ou 一定是 都为奇数或者都为偶数
当他们都是偶数 输出yes
如果都为偶数 再去看他们的 ch(就是差值为1的数)存不存在
如果存在直接输出yes

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
int a[5000];
int main()
{
	int t; cin >> t;
	while (t--)
	{
		int n; cin >> n;
		int j = 0, ch = 0, ou = 0;;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			if (a[i] % 2 == 0)
				ou++;
			else
				j++;
		}
		sort(a + 1, a + 1 + n);
		for (int i = 2; i <= n; i++)
		{
			if (a[i] - a[i - 1] == 1)
			{
				ch++; i++;
			}
		}
			if (j % 2 == 0 && ou % 2 == 0)
				cout << "YES" << endl;
			else 
			{
				if (ch != 0) cout << "YES" << endl;
				else cout << "NO" << endl;

			}
		
	}

}

E题
Polygon is not only the best platform for developing problems but also a square matrix with side n, 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 2n cannons were placed.

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 i, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (i,1) and ends in some cell (i,j);
if a cannon stands in the column j, above the first row, and shoots with a 1, then the 1 starts its flight from the cell (1,j) and ends in some cell (i,j).
For example, consider the following sequence of shots:

在这里插入图片描述
You have a report from the military training on your desk. This report is a square matrix with side length n 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 t (1≤t≤1000) — the number of test cases. Then t test cases follow.

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

This is followed by n lines of length n, 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
input
5
4
0010
0011
0000
0000
2
10
01
2
00
00
4
0101
1111
0101
0111
4
0100
1110
0101
0111
output
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.

思路:既然那个炮弹能停下来(不看边界),说明它的右边或者下边一定是个1
如果都是零 那就错了
细节它输入东西的时候没空格哦,数组用char来做

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
char a[51][51];
int main()
{
	int t; cin >> t;
	while (t--)
	{
		int n; cin >> n;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >>a[i][j];
		int s = 0;
		for (int i =1;i<n;i++)
		{
			for (int j = 1;j<n; j++)
			{
			
				if (a[i][j] =='1')
				{
					if (a[i][j + 1]=='0'&&a[i + 1][j] =='0')
					{
						s = 1; break;
					}
				}
			}
			if (s == 1) break;
		}

		
		if (s == 0)
			cout << "YES" << endl;
		else cout << "NO" << endl;
		
		
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值