Equal Beauty CodeChef SnackDown 2021 Round 1A Question The beauty of an (non-empty) array of integers is defined as the difference between its largest and smallest element. For example, the beauty of the array [2,3,4,4,6] is 6−2=4. An array A is said to be good if it is possible to partition the elements of A into two non-empty arrays B1 and B2 such that B1 and B2 have the same beauty. Each element of array A should be in exactly one array: either in B1 or in B2. For example, the array [6,2,4,4,4] is good because its elements can be partitioned into two arrays B1=[6,4,4] and B2=[2,4], where both B1 and B2 have the same beauty (6−4=4−2=2). You are given an array A of length N. In one move you can: Select an index i (1≤i≤N) and either increase Ai by 1 or decrease Ai by 1. Find the minimum number of moves required to make the array A good. Input Format The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follow. Each test
Chefland and Average Distance CodeChef SnackDown 2021 Advanced Practice Contest
Question
Chefland is a grid with rows and columns. Each cell of this grid is either empty or contains a house. The distance between a pair of houses is the Manhattan distance between the cells containing them.
For each between and inclusive, Chef wants to calculate the number of unordered pairs of distinct houses with distance equal to . Please help him!
Input
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers and .
- lines follow. For each (), the -th of these lines contains a binary string with length ; for each (), the -th character of this string is '1' if the cell in the -th row and -th column contains a house or '0' if it is empty.
Output
For each test case, print a single line containing space-separated integers. For each valid , the -th integer should denote the number of pairs with distance .
Constraints
1≤T≤3
2≤N,M≤300
Example Input
1
3 4
0011
0000
0100
Example Output
1 0 1 1 0
Program Code in C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 305;
char s[maxn];
bool a[maxn][maxn];
short f[maxn][maxn][maxn + maxn];
LL ret[maxn + maxn];
int n, m;
template <bool sgn>
LL calc () {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
bool _sgn = a[i][j];
for (int k = 0; k < i + j; ++k) {
f[i][j][k] = f[i - 1][j - 1][k];
if (_sgn && sgn) ret[i + j - k] += f[i][j][k];
f[i][j][k] = f[i - 1][j][k] + f[i][j - 1][k] - f[i][j][k];
if (_sgn && !sgn) ret[i + j - k] += f[i][j][k];
}
f[i][j][i + j] = a[i][j];
}
}
}
void solve () {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (int j = 1; j <= m; ++j) a[i][j] = s[j] == '1';
}
memset(ret, 0, sizeof(ret));
calc<0>();
for (int i = 1; i <= n; ++i) reverse(a[i] + 1, a[i] + m + 1);
calc<1>();
for (int i = 1; i <= n + m - 2; ++i) printf("%d%c", ret[i], i == n + m - 2 ? '\n' : ' ');
}
int main () {
int T;
scanf("%d", &T);
while (T--) solve();
return 0;
}
Suggested Links
More information
CodeChef is one of the largest online coding platform and SnackDown is one of it's of grandest programs held ever year. SnackDown is a global programming event that invites programmers all over the world to participate in India's most prestigious multi-round programming competition. SnackDown is open to everyone who has a knack in programming.
This question that is Chefland and Average Distance is of CodeChef SnackDown 2021 Advanced Practice Contest. Here you will get a brief explanation of the problem and after reading the explanation if you are still stuck and have no clue to understand the problem then you can visit the solution of the question given above.
Hope you learnt something from this explanation and solution. Code it guys, practice coding more and more.
Comments
Post a Comment