Qualification Round 2020 - Code Jam 2020

Vestigium

Problem

Vestigium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.

The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).

An N-by-N square matrix is a Latin square if each cell contains one of N different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N values are the integers between 1 and N.

Given a matrix that contains only integers between 1 and N, we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each starts with a line containing a single integer N: the size of the matrix to explore. Then, N lines follow. The i-th of these lines contains N integers Mi,1Mi,2 ..., Mi,NMi,j is the integer in the i-th row and j-th column of the matrix.

Output

For each test case, output one line containing Case #x: k r c, where x is the test case number (starting from 1), k is the trace of the matrix, r is the number of rows of the matrix that contain repeated elements, and c is the number of columns of the matrix that contain repeated elements.

Limits

Test set 1 (Visible Verdict)

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ N ≤ 100.
1 ≤ Mi,j ≤ N, for all i, j.

Sample


Input
 

Output
 
3
4
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
4
2 2 2 2
2 3 2 3
2 2 2 3
2 2 2 2
3
2 1 3
1 3 2
1 2 3

  
Case #1: 4 0 0
Case #2: 9 4 4
Case #3: 8 0 2

  

In Sample Case #1, the input is a natural Latin square, which means no row or column has repeated elements. All four values in the main diagonal are 1, and so the trace (their sum) is 4.

In Sample Case #2, all rows and columns have repeated elements. Notice that each row or column with repeated elements is counted only once regardless of the number of elements that are repeated or how often they are repeated within the row or column. In addition, notice that some integers in the range 1 through N may be absent from the input.

In Sample Case #3, the leftmost and rightmost columns have repeated elements.

Solution

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

const int maxn = 1e2 + 10;
bool vis[maxn];
int mat[maxn][maxn];

int main()
{
    int t = read();
    for (int x = 1; x <= t; x++)
    {
        int k = 0, r = 0, c = 0;
        int n = read();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                mat[i][j] = read();
        for (int i = 1; i <= n; i++) k += mat[i][i];
        for (int i = 1; i <= n; i++)
        {
            memset(vis, false, sizeof vis);
            bool flag = false;
            for (int j = 1; j <= n; j++)
            {
                if (vis[mat[i][j]])
                {
                    flag = true;
                    break;
                }
                vis[mat[i][j]] = true;
            }
            r += flag;
        }
        for (int i = 1; i <= n; i++)
        {
            memset(vis, false, sizeof vis);
            bool flag = false;
            for (int j = 1; j <= n; j++)
            {
                if (vis[mat[j][i]])
                {
                    flag = true;
                    break;
                }
                vis[mat[j][i]] = true;
            }
            c += flag;
        }
        printf("Case #%d: %d %d %d\n", x, k, r, c);
    }
    return 0;
}

Nesting Depth

Problem

tl;dr: Given a string of digits S, insert a minimum number of opening and closing parentheses into it such that the resulting string is balanced and each digit d is inside exactly d pairs of matching parentheses.

Let the nesting of two parentheses within a string be the substring that occurs strictly between them. An opening parenthesis and a closing parenthesis that is further to its right are said to match if their nesting is empty, or if every parenthesis in their nesting matches with another parenthesis in their nesting. The nesting depth of a position p is the number of pairs of matching parentheses m such that p is included in the nesting of m.

For example, in the following strings, all digits match their nesting depth: 0((2)1)(((3))1(2))((((4))))((2))((2))(1). The first three strings have minimum length among those that have the same digits in the same order, but the last one does not since ((22)1) also has the digits 221 and is shorter.

Given a string of digits S, find another string S', comprised of parentheses and digits, such that:

  • all parentheses in S' match some other parenthesis,
  • removing any and all parentheses from S' results in S,
  • each digit in S' is equal to its nesting depth, and
  • S' is of minimum length.

Input

The first line of the input gives the number of test cases, TT lines follow. Each line represents a test case and contains only the string S.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the string S' defined above.

Limits

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ length of S ≤ 100.

Test set 1 (Visible Verdict)

Each character in S is either 0 or 1.

Test set 2 (Visible Verdict)

Each character in S is a decimal digit between 0 and 9, inclusive.

Sample


Input
 

Output
 
4
0000
101
111000
1

  
Case #1: 0000
Case #2: (1)0(1)
Case #3: (111)000
Case #4: (1)

  

The strings ()0000()(1)0(((()))1) and (1)(11)000 are not valid solutions to Sample Cases #1, #2 and #3, respectively, only because they are not of minimum length. In addition, 1)( and )(1 are not valid solutions to Sample Case #4 because they contain unmatched parentheses and the nesting depth is 0 at the position where there is a 1.

You can create sample inputs that are valid only for Test Set 2 by removing the parentheses from the example strings mentioned in the problem statement.

Solution

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    int t = read();
    for (int i = 1; i <= t; i++)
    {
        cout << "Case #" << i << ": ";
        string s;
        cin >> s;
        s = "0" + s;
        for (int i = 1; i < s.length(); i++)
        {
            for (int j = 0; j < s[i] - s[i - 1]; j++) cout << "(";
            for (int j = 0; j < s[i - 1] - s[i]; j++) cout << ")";
            cout << s[i];
        }
        for (int i = 0; i < s[s.length() - 1] - '0'; i++) cout << ")";
        cout << endl;
    }
    return 0;
}

Parenting Partnering Returns

Problem

Cameron and Jamie's kid is almost 3 years old! However, even though the child is more independent now, scheduling kid activities and domestic necessities is still a challenge for the couple.

Cameron and Jamie have a list of N activities to take care of during the day. Each activity happens during a specified interval during the day. They need to assign each activity to one of them, so that neither of them is responsible for two activities that overlap. An activity that ends at time t is not considered to overlap with another activity that starts at time t.

For example, suppose that Jamie and Cameron need to cover 3 activities: one running from 18:00 to 20:00, another from 19:00 to 21:00 and another from 22:00 to 23:00. One possibility would be for Jamie to cover the activity running from 19:00 to 21:00, with Cameron covering the other two. Another valid schedule would be for Cameron to cover the activity from 18:00 to 20:00 and Jamie to cover the other two. Notice that the first two activities overlap in the time between 19:00 and 20:00, so it is impossible to assign both of those activities to the same partner.

Given the starting and ending times of each activity, find any schedule that does not require the same person to cover overlapping activities, or say that it is impossible.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case starts with a line containing a single integer N, the number of activities to assign. Then, N more lines follow. The i-th of these lines (counting starting from 1) contains two integers Si and Ei. The i-th activity starts exactly Si minutes after midnight and ends exactly Ei minutes after midnight.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is IMPOSSIBLE if there is no valid schedule according to the above rules, or a string of exactly N characters otherwise. The i-th character in y must be C if the i-th activity is assigned to Cameron in your proposed schedule, and J if it is assigned to Jamie.

If there are multiple solutions, you may output any one of them. (See "What if a test case has multiple correct solutions?" in the Competing section of the FAQ. This information about multiple solutions will not be explicitly stated in the remainder of the 2020 contest.)

Limits

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ Si < Ei ≤ 24 × 60.

Test set 1 (Visible Verdict)

2 ≤ N ≤ 10.

Test set 2 (Visible Verdict)

2 ≤ N ≤ 1000.

Sample


Input
 

Output
 
4
3
360 480
420 540
600 660
3
0 1440
1 3
2 4
5
99 150
1 100
100 301
2 5
150 250
2
0 720
720 1440

  
Case #1: CJC
Case #2: IMPOSSIBLE
Case #3: JCCJJ
Case #4: CC

  

Sample Case #1 is the one described in the problem statement. As mentioned above, there are other valid solutions, like JCJ and JCC.

In Sample Case #2, all three activities overlap with each other. Assigning them all would mean someone would end up with at least two overlapping activities, so there is no valid schedule.

In Sample Case #3, notice that Cameron ends an activity and starts another one at minute 100.

In Sample Case #4, any schedule would be valid. Specifically, it is OK for one partner to do all activities.

Soluton

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
struct node { int beg, end, pos; };
vector<node> vec;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

bool cmp(const node& a, const node& b) { return a.beg != b.beg ? a.beg < b.beg : a.end < b.end; }

int main()
{
    int t = read();
    for (int i = 1; i <= t; i++)
    {
        cout << "Case #" << i << ": ";
        int n = read();
        string str;
        str.resize(n);
        vec.resize(n);
        for (int j = 0; j < n; j++)
        {
            vec[j].beg = read();
            vec[j].end = read();
            vec[j].pos = j;
        }
        sort(vec.begin(), vec.end(), cmp);
        int a = 0, b = 0;
        bool flag = false;
        for (int j = 0; j < n; j++)
        {
            if (a <= vec[j].beg)
            {
                a = vec[j].end;
                str[vec[j].pos] = 'C';
            }
            else if (b <= vec[j].beg)
            {
                b = vec[j].end;
                str[vec[j].pos] = 'J';
            }
            else
            {
                flag = true;
                break;
            }
        }
        cout << (flag ? "IMPOSSIBLE" : str) << endl;
    }
    return 0;
}

Indicium

Problem

Indicium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.

Latin square is an N-by-N square matrix in which each cell contains one of N different values, such that no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N values are the integers between 1 and N.

The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).

Given values N and K, produce any N-by-N "natural Latin square" with trace K, or say it is impossible. For example, here are two possible answers for N = 3, K = 6. In each case, the values that contribute to the trace are underlined.

2 1 3   3 1 2
2 1   1 2 3
1 3 2   2 3 1

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of one line containing two integers N and K: the desired size of the matrix and the desired trace.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is IMPOSSIBLE if there is no answer for the given parameters or POSSIBLE otherwise. In the latter case, output N more lines of N integers each, representing a valid "natural Latin square" with a trace of K, as described above.

Limits

Time limit: 20 seconds per test set.
Memory limit: 1GB.
N ≤ K ≤ N2.

Test set 1 (Visible Verdict)

T = 44.
2 ≤ N ≤ 5.

Test set 2 (Hidden Verdict)

1 ≤ T ≤ 100.
2 ≤ N ≤ 50.

Sample


Input
 

Output
 
2
3 6
2 3

  
Case #1: POSSIBLE
2 1 3
3 2 1
1 3 2
Case #2: IMPOSSIBLE

  

Sample Case #1 is the one described in the problem statement.

Sample Case #2 has no answer. The only possible 2-by-2 "natural Latin squares" are as follows:

1 2   2 1
2 1   1 2

These have traces of 2 and 4, respectively. There is no way to get a trace of 3.

Solution

第二个测试点 T 了

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 51;
int n, k;
int mat[maxn][maxn];
bool solved, row[maxn][maxn], col[maxn][maxn];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

void solve(int r, int c, int m)
{
    if (r == n && c == n + 1 && m == k)
    {
        solved = true;
        cout << "POSSIBLE" << endl;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cout << mat[i][j] << (j == n ? "\n" : " ");
        return;
    }
    if (r > n) return;
    if (c > n) solve(r + 1, 1, m);
    for (int i = 1; i <= n && !solved; i++)
    {
        if (!row[r][i] && !col[c][i])
        {
            row[r][i] = col[c][i] = true;
            if (r == c) m += i;
            mat[r][c] = i;
            solve(r, c + 1, m);
            row[r][i] = col[c][i] = false;
            if (r == c) m -= i;
            mat[r][c] = 0;
        }
    }
}

int main()
{
    int t = read();
    for (int i = 1; i <= t; i++)
    {
        cout << "Case #" << i << ": ";
        n = read(); k = read();
        solved = false;
        solve(1, 1, 0);
        if (!solved) cout << "IMPOSSIBLE" << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/105315120