题解:Codeforces Round 962 (Div. 3) A&B
由于A和B太简单了,我就放在一起讲了
A. Legs
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
It's another beautiful day on Farmer John's farm.
After Farmer John arrived at his farm, he counted \(n\) legs. It is known only chickens and cows live on the farm, and a chicken has \(2\) legs while a cow has \(4\).
What is the minimum number of animals Farmer John can have on his farm assuming he counted the legs of all animals?
农夫约翰的农场又迎来了美好的一天。
农夫约翰来到农场后,数了数 \(n\) 条腿。众所周知,农场里只住着鸡和牛,一只鸡有 \(2\) 条腿,而一头牛有 \(4\) 条腿。
假设约翰农场主数清了所有动物的腿,那么他的农场里至少有多少种动物?
Input
The first line contains single integer \(t\) (\(1 \leq t \leq 10^3\)) — the number of test cases.
Each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^3\), \(n\) is even).
输入
第一行包含一个整数 \(t\) ( \(1 \leq t \leq 10^3\) ) - 测试用例数。
每个测试用例包含一个整数 \(n\) ( \(2 \leq n \leq 2 \cdot 10^3\) , \(n\) 为偶数)。
Output
For each test case, output an integer, the minimum number of animals Farmer John can have on his farm.
输出
针对每个测试用例,输出一个整数,即农场主约翰的农场可以拥有的最少动物数量。
Example
Input
3
2
6
8
Output
1
2
2
题意
就是鸡兔同笼
给你腿的总数
问你里面最少有多少只生物
题解
假设兔子(四条腿的)尽量多
还能放就放鸡(两条腿的)
ans = n/4 + (n%4 ? : 1 : 0);
我的代码
#include <bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()
using i64 = long long;
int t = 1;
void solve(){
int n;
std::cin >> n;
n/=2;
std::cout << n/2 + n%2 <<"\n";
}
signed main(){
std::cin >> t;
while(t--){
solve();
}
return 0;
}
B. Scale
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Tina has a square grid with \(n\) rows and \(n\) columns. Each cell in the grid is either \(0\) or \(1\).
Tina wants to reduce the grid by a factor of \(k\) (\(k\) is a divisor of \(n\)). To do this, Tina splits the grid into \(k \times k\) nonoverlapping blocks of cells such that every cell belongs to exactly one block.
Tina then replaces each block of cells with a single cell equal to the value of the cells in the block. It is guaranteed that every cell in the same block has the same value.
For example, the following demonstration shows a grid being reduced by factor of \(3\).
Original grid
\(0\) | \(0\) | \(0\) | \(1\) | \(1\) | \(1\) |
---|---|---|---|---|---|
\(0\) | \(0\) | \(0\) | \(1\) | \(1\) | \(1\) |
\(0\) | \(0\) | \(0\) | \(1\) | \(1\) | \(1\) |
\(1\) | \(1\) | \(1\) | \(0\) | \(0\) | \(0\) |
\(1\) | \(1\) | \(1\) | \(0\) | \(0\) | \(0\) |
\(1\) | \(1\) | \(1\) | \(0\) | \(0\) | \(0\) |
Reduced grid
\(0\) | \(1\) |
---|---|
\(1\) | \(0\) |
Help Tina reduce the grid by a factor of \(k\).
蒂娜有一个行数为 \(n\) 列数为 \(n\) 的正方形网格。网格中的每个单元格要么是 \(0\) 要么是 \(1\) 。
蒂娜希望将网格缩小 \(k\) 倍(** \(k\) 是 \(n\) **的除数)。为此,蒂娜将网格分割成 \(k \times k\) 个不重叠的单元格块,这样每个单元格都正好属于一个单元格块。
然后,蒂娜将每个单元格块替换为与单元格块中单元格值相等的单个单元格。保证同一区块中的每个单元格都具有相同的值。
例如,下面的演示显示一个网格被缩小了 \(3\) 倍。
原始网格
\(0\) | \(0\) | \(0\) | \(1\) | \(1\) | \(1\) |
---|---|---|---|---|---|
\(0\) | \(0\) | \(0\) | \(1\) | \(1\) | \(1\) |
\(0\) | \(0\) | \(0\) | \(1\) | \(1\) | \(1\) |
\(1\) | \(1\) | \(1\) | \(0\) | \(0\) | \(0\) |
\(1\) | \(1\) | \(1\) | \(0\) | \(0\) | \(0\) |
\(1\) | \(1\) | \(1\) | \(0\) | \(0\) | \(0\) |
缩小网格
\(0\) | \(1\) |
---|---|
\(1\) | \(0\) |
帮助 Tina 将网格缩小 \(k\) 倍。
Input
The first line contains \(t\) (\(1 \leq t \leq 100\)) – the number of test cases.
The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n \leq 1000\), \(1 \le k \le n\), \(k\) is a divisor of \(n\)) — the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid by.
Each of the following \(n\) lines contain \(n\) characters describing the cells of the grid. Each character is either \(0\) or \(1\). It is guaranteed every \(k\) by \(k\) block has the same value.
It is guaranteed the sum of \(n\) over all test cases does not exceed \(1000\).
输入
第一行包含 \(t\) ( \(1 \leq t \leq 100\) ) - 测试用例数。
每个测试用例的第一行包含两个整数 \(n\) 和 \(k\) ( \(1 \leq n \leq 1000\) 、 \(1 \le k \le n\) ,** \(k\) 是 \(n\) 的除数**)--网格的行数和列数,以及蒂娜希望缩小网格的因子。
下面每 \(n\) 行包含 \(n\) 个描述网格单元格的字符。每个字符要么是 \(0\) 要么是 \(1\) 。保证每个 \(k\) x \(k\) 块都具有相同的值。
保证所有测试用例中 \(n\) 的总和不超过 \(1000\) 。
Output
For each test case, output the grid reduced by a factor of \(k\) on a new line.
输出
对于每个测试用例,在新的一行输出网格缩小了 \(k\) 倍的结果。
Example
Input
4
4 4
0000
0000
0000
0000
6 3
000111
000111
000111
111000
111000
111000
6 2
001100
001100
111111
111111
110000
110000
8 1
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
Output
0
01
10
010
111
100
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
题意
一个 \(n \times n\) 的 \(01\) 矩阵被均等分成了 \(k \times k\) 份
每一块里面的数保证全部相同,
求原矩阵缩小 \(k\) 倍的矩阵
题解
只要输出每一块的左上角那个数字就可以了
也就是输出(假设坐标范围是 \(0 \le i,j \lt n\) )
代码
#include <bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()
using i64 = long long;
int t = 1;
void solve(){
int n,k;
std::cin >> n >> k;
std::string s[n];
for(int i = 0 ; i < n ; i ++) {
std::cin >>s[i];
}
for(int i = 0 ; i < n ; i += k) {
for(int j = 0 ; j < n ; j += k ) {
std::cout << s[i][j];
}
std::cout << "\n";
}
}
signed main(){
std::cin >> t;
while(t--){
solve();
}
return 0;
}
posted on 2024-07-30 09:46 Jiejiejiang 阅读(11) 评论(0) 编辑 收藏 举报