Codeforces Round #634 (Div. 3)题解

在这里插入图片描述
写在前面: 还是只有四道。。。想出了后两道解法但来不及了,主要是第四道耗时太久。下次碰到类似的要找准方法,直接写。

A. Candies and Two Sisters

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are two sisters Alice and Betty. You have n candies. You want to distribute these n candies between two sisters in such a way that:

Alice will get a (a>0) candies;
Betty will get b (b>0) candies;
each sister will get some integer number of candies;
Alice will get a greater amount of candies than Betty (i.e. a>b);
all the candies will be given to one of two sisters (i.e. a+b=n).
Your task is to calculate the number of ways to distribute exactly n candies between sisters in a way described above. Candies are indistinguishable.

Formally, find the number of ways to represent n as the sum of n=a+b, where a and b are positive integers and a>b.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of a test case contains one integer n (1≤n≤2⋅109) — the number of candies you have.

Output
For each test case, print the answer — the number of ways to distribute exactly n candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print 0.

Example
inputCopy
6
7
1
2
3
2000000000
763243547
outputCopy
3
0
0
1
999999999
381621773
Note
For the test case of the example, the 3 possible ways to distribute candies are:

a=6, b=1;
a=5, b=2;
a=4, b=3.
思路: 很简单,奇数直接除二,偶数除二减一。

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        if (n & 1)
        {
            cout << n / 2 << endl;
        }
        else
        {
            cout << n / 2 - 1 << endl;
        }
    }
 
    return 0;
}

B. Construct the String

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given three positive integers n, a and b. You have to construct a string s of length n consisting of lowercase Latin letters such that each substring of length a has exactly b distinct letters. It is guaranteed that the answer exists.

You have to answer t independent test cases.

Recall that the substring s[l…r] is the string sl,sl+1,…,sr and its length is r−l+1. In this problem you are only interested in substrings of length a.

Input
The first line of the input contains one integer t (1≤t≤2000) — the number of test cases. Then t test cases follow.

The only line of a test case contains three space-separated integers n, a and b (1≤a≤n≤2000,1≤b≤min(26,a)), where n is the length of the required string, a is the length of a substring and b is the required number of distinct letters in each substring of length a.

It is guaranteed that the sum of n over all test cases does not exceed 2000 (∑n≤2000).

Output
For each test case, print the answer — such a string s of length n consisting of lowercase Latin letters that each substring of length a has exactly b distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists.

Example
inputCopy
4
7 5 3
6 1 1
6 6 1
5 2 2
outputCopy
tleelte
qwerty
vvvvvv
abcde
Note
In the first test case of the example, consider all the substrings of length 5:

“tleel”: it contains 3 distinct (unique) letters,
“leelt”: it contains 3 distinct (unique) letters,
“eelte”: it contains 3 distinct (unique) letters.
思路: 直接用n和b,b顺序abc···,然后排进去就行。

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, a, b;
        cin >> n >> a >> b;
        string x = "";
        for (int i = 0; i < b; ++i)
            x += 'a' + i;
        string ans = "";
        int xx = n / b;
        for (int i = 0; i < xx; ++i)
            ans += x;
        for (int i = xx * b; i < n; ++i)
        {
            ans += x[i - xx * b];
        }
        cout << ans << endl;
    }
 
    return 0;
}

C. Two Teams Composing

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have n students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the i-th student skill is denoted by an integer ai (different students can have the same skills).

So, about the teams. Firstly, these two teams should have the same size. Two more constraints:

The first team should consist of students with distinct skills (i.e. all skills in the first team are unique).
The second team should consist of students with the same skills (i.e. all skills in the second team are equal).
Note that it is permissible that some student of the first team has the same skill as a student of the second team.

Consider some examples (skills are given):

[1,2,3], [4,4] is not a good pair of teams because sizes should be the same;
[1,1,2], [3,3,3] is not a good pair of teams because the first team should not contain students with the same skills;
[1,2,3], [3,4,4] is not a good pair of teams because the second team should contain students with the same skills;
[1,2,3], [3,3,3] is a good pair of teams;
[5], [6] is a good pair of teams.
Your task is to find the maximum possible size x for which it is possible to compose a valid pair of teams, where each team size is x (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of students. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤n), where ai is the skill of the i-th student. Different students can have the same skills.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — the maximum possible size x for which it is possible to compose a valid pair of teams, where each team size is x.

Example
inputCopy
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3
outputCopy
3
1
0
2
Note
In the first test case of the example, it is possible to construct two teams of size 3: the first team is [1,2,4] and the second team is [4,4,4]. Note, that there are some other ways to construct two valid teams of size 3.

思路: 计算最多重复的数值个数,计算最多不同数的个数。

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
int a[200005];
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        map<int, int>mp;
        sort(a, a + n);
        vector<int>vec;
        int msum = 0;
        int mnum = 0;
        for (int i = 0; i < n; ++i)
        {
            if (mp[a[i]] == 0)
            {
                vec.push_back(a[i]);
                mp[a[i]]++;
                if (msum < mp[a[i]])
                {
                    msum = mp[a[i]];
                }
            }
            else
            {
                mp[a[i]]++;
                if (msum < mp[a[i]])
                {
                    msum = mp[a[i]];
                }
            }
        }
        if (msum <= vec.size() - 1)
            cout << msum << endl;
        else if (msum == vec.size())
            cout << msum  - 1 << endl;
        else
            cout << vec.size() << endl;
    }
 
    return 0;
}

D. Anti-Sudoku

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a correct solution of the sudoku puzzle. If you don’t know what is the sudoku, you can read about it here.

The picture showing the correct sudoku solution:

Blocks are bordered with bold black color.

Your task is to change at most 9 elements of this field (i.e. choose some 1≤i,j≤9 and change the number at the position (i,j) to any other number in range [1;9]) to make it anti-sudoku. The anti-sudoku is the 9×9 field, in which:

Any number in this field is in range [1;9];
each row contains at least two equal elements;
each column contains at least two equal elements;
each 3×3 block (you can read what is the block in the link above) contains at least two equal elements.
It is guaranteed that the answer exists.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

Each test case consists of 9 lines, each line consists of 9 characters from 1 to 9 without any whitespaces — the correct solution of the sudoku puzzle.

Output
For each test case, print the answer — the initial field with at most 9 changed elements so that the obtained field is anti-sudoku. If there are several solutions, you can print any. It is guaranteed that the answer exists.

Example
inputCopy
1
154873296
386592714
729641835
863725149
975314628
412968357
631457982
598236471
247189563
outputCopy
154873396
336592714
729645835
863725145
979314628
412958357
631457992
998236471
247789563
思路: 方法很多,我是直接模拟。

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
int a[200005];
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string s[9];
        for (int i = 0; i < 9; ++i)
            cin >> s[i];
        s[0][1] = s[0][0];
        s[1][5] = s[1][4];
        s[2][6] = s[2][8];
        s[3][2] = s[3][1];
        s[4][3] = s[4][5];
        s[5][7] = s[5][6];
        s[6][0] = s[6][2];
        s[7][4] = s[7][3];
        s[8][8] = s[8][7];
        // cout << endl;
        for (int i = 0; i < 9; ++i)
            cout << s[i] << endl;
    }
 
    return 0;
}

E1. Three Blocks Palindrome (easy version)

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The only difference between easy and hard versions is constraints.

You are given a sequence a consisting of n positive integers.

Let’s define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [a,a,…,ax,b,b,…,by,a,a,…,ax]. There x,y are integers greater than or equal to 0. For example, sequences [], [2], [1,1], [1,2,1], [1,2,2,1] and [1,1,2,1,1] are three block palindromes but [1,2,3,2,1], [1,2,1,2,1] and [1,2] are not.

Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome.

You have to answer t independent test cases.

Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].

Input
The first line of the input contains one integer t (1≤t≤2000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2000) — the length of a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤26), where ai is the i-th element of a. Note that the maximum value of ai can be up to 26.

It is guaranteed that the sum of n over all test cases does not exceed 2000 (∑n≤2000).

Output
For each test case, print the answer — the maximum possible length of some subsequence of a that is a three blocks palindrome.

Example
inputCopy
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
outputCopy
7
2
4
1
1
3
思路: 记录每个数字的位置,计算从某个位置为止的某个数的个数。

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pf push_front
#define ft first
#define sc second
#define all(v) v.begin(), v.end()
using namespace std;
const int maxn = 201001;
signed main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> mas(n, 0);
        vector<int> pos[201];
        int cnt[n][200];
        for (int d = 0; d < n; d++) {
            cin >> mas[d];
            pos[mas[d]].pb(d);
            if (d > 0) {
                for (int i = 1; i <= 200; i++) {
                    cnt[d][i] = cnt[d - 1][i];
                }
            }
            cnt[d][mas[d]]++;
        }
        int max1 = 0;
        for (int el = 1; el <= 200; el++) {
            max1 = max(max1, (int)pos[el].size());
            for (int i = 1; i * 2 <= pos[el].size(); i++) {
                int posl = pos[el][i - 1], posr = pos[el][pos[el].size() - i] - 1;
                int mx1 = 0;
                for (int j = 1; j <= 200; j++) {
                    mx1 = max(mx1, cnt[posr][j] - cnt[posl][j]);
                }
                max1 = max(max1, 2 * i + mx1);
            }
        }
        cout << max1 << "\n";
    }
}

E2. Three Blocks Palindrome (hard version)

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The only difference between easy and hard versions is constraints.

You are given a sequence a consisting of n positive integers.

Let’s define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [a,a,…,ax,b,b,…,by,a,a,…,ax]. There x,y are integers greater than or equal to 0. For example, sequences [], [2], [1,1], [1,2,1], [1,2,2,1] and [1,1,2,1,1] are three block palindromes but [1,2,3,2,1], [1,2,1,2,1] and [1,2] are not.

Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome.

You have to answer t independent test cases.

Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤200), where ai is the i-th element of a. Note that the maximum value of ai can be up to 200.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — the maximum possible length of some subsequence of a that is a three blocks palindrome.

Example
inputCopy
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
outputCopy
7
2
4
1
1
3

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pf push_front
#define ft first
#define sc second
#define all(v) v.begin(), v.end()
using namespace std;
const int maxn = 201001;
signed main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> mas(n, 0);
        vector<int> pos[201];
        int cnt[n][200];
        for (int d = 0; d < n; d++) {
            cin >> mas[d];
            pos[mas[d]].pb(d);
            if (d > 0) {
                for (int i = 1; i <= 200; i++) {
                    cnt[d][i] = cnt[d - 1][i];
                }
            }
            cnt[d][mas[d]]++;
        }
        int max1 = 0;
        for (int el = 1; el <= 200; el++) {
            max1 = max(max1, (int)pos[el].size());
            for (int i = 1; i * 2 <= pos[el].size(); i++) {
                int posl = pos[el][i - 1], posr = pos[el][pos[el].size() - i] - 1;
                int mx1 = 0;
                for (int j = 1; j <= 200; j++) {
                    mx1 = max(mx1, cnt[posr][j] - cnt[posl][j]);
                }
                max1 = max(max1, 2 * i + mx1);
            }
        }
        cout << max1 << "\n";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值