Codeforces Round #634 (Div. 3)

Codeforces Round #634 (Div. 3)

A

A. Candies and Two Sisters(题目链接)
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 𝑛 candies. You want to distribute these 𝑛 candies between two sisters in such a way that:

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

Formally, find the number of ways to represent 𝑛 as the sum of 𝑛=𝑎+𝑏, where 𝑎 and 𝑏 are positive integers and 𝑎>𝑏.

You have to answer 𝑡 independent test cases.

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

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

Output
For each test case, print the answer — the number of ways to distribute exactly 𝑛 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:

𝑎=6, 𝑏=1;
𝑎=5, 𝑏=2;
𝑎=4, 𝑏=3.

题意:分糖果,两个人分糖果,总共有n颗糖果,分给第一个人a颗,第二个人b颗,且a>b, a+b=n,问总共有多少种分法。

题解:如果>2的话,为奇数的话输出n/2,为偶数输出n/2-1

代码:

#include<bits/stdc++.h>
#define sc(x) scanf("%lld",&x);
#define pf printf
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define dep(i,e,s) for(int i=e;i>=s;i--)
using namespace std;
typedef long long ll;
const int inf=~0u>>2;
const int maxn=1e5+7;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int main()
{
	SIS;
	//¼ÓËÙcin/cout
	int t;
	ll n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		if(n==1 || n==0 || n==2)
			cout<<0<<endl;
		else
		{
			if(n&1)
				cout<<n/2<<endl;
			else
				cout<<n/2-1<<endl;
		}
	}
	return 0;
}

B

B. Construct the String(题目链接)
B. Construct the String
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given three positive integers 𝑛, 𝑎 and 𝑏. You have to construct a string 𝑠 of length 𝑛 consisting of lowercase Latin letters such that each substring of length 𝑎 has exactly 𝑏 distinct letters. It is guaranteed that the answer exists.

You have to answer 𝑡 independent test cases.

Recall that the substring 𝑠[𝑙…𝑟] is the string 𝑠𝑙,𝑠𝑙+1,…,𝑠𝑟 and its length is 𝑟−𝑙+1. In this problem you are only interested in substrings of length 𝑎.

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

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

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

Output
For each test case, print the answer — such a string 𝑠 of length 𝑛 consisting of lowercase Latin letters that each substring of length 𝑎 has exactly 𝑏 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长度的字符串,要使得每a范围内只出现b个相同的字符。
输入n a b让你构造

题解:前b个字符不同,然后a-b个重复最后一个,然后之后每次接着上面循环即可。
如 7 5 3
abccc a b即为答案。
如 8 5 3
abcccabc即为答案。

代码:模拟上述步骤即可。

#include<bits/stdc++.h>
#define sc(x) scanf("%lld",&x);
#define pf printf
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define dep(i,e,s) for(int i=e;i>=s;i--)
using namespace std;
typedef long long ll;
const int inf=~0u>>2;
const int maxn=1e5+7;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int main()
{
	SIS;
	//¼ÓËÙcin/cout
	int t,n,a,b;
	cin>>t;
	while(t--)
	{
		cin>>n>>a>>b;
		string s;
		for(int i=0;i<b;i++)
		{
			char c='a'+i;
			s=s+c;
		}
		for(int i=0;i<a-b;i++)
		{
			char c='a'+b-1;
			s=s+c;
		}
		for(int i=0;i<n-a;i++)
		{
			s=s+s[i];
		}
		cout<<s<<endl;
	}
	return 0;
}

C

C. Two Teams Composing(题目链接)
C. Two Teams Composing
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have 𝑛 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 𝑖-th student skill is denoted by an integer 𝑎𝑖 (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 𝑥 for which it is possible to compose a valid pair of teams, where each team size is 𝑥 (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 𝑡 independent test cases.

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

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

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

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

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.

贪心题:
题意:给你一组序列,你要组成两个集合,集合一中所有的元素不同,集合二中所有的元素相同,且这两个集合的元素总个数得相同,问你集合最大元素总个数是多少。

题解:用个map容器记录下每个数字出现的次数m,m记录个数最多的元素。然后记录下不同个数字母的总个数cnt,然后答案即为
max(min(m,cnt-1),min(m-1,cnt))
可以理解一下下上述两个状态,
min(m,cnt-1) 状态即为这m个元素构成了一个集合,剩下cnt-1种元素构成了一种元素。取min
min(m-1,cnt) 状态即为从m个元素抽出一个来,然后剩下m-1个元素构成一个集合,然后cnt个元素构成一个集合。取min

代码:

#include<bits/stdc++.h>
#define sc(x) scanf("%lld",&x);
#define pf printf
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define dep(i,e,s) for(int i=e;i>=s;i--)
using namespace std;
typedef long long ll;
const int inf=~0u>>2;
const int maxn=1e5+7;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int main()
{
	SIS;
	//¼ÓËÙcin/cout
	int t,n,a;
	cin>>t;
	while(t--)
	{
		cin>>n;
		map<int,int> mp;
		for(int i=1;i<=n;i++)
		{
			cin>>a;
			mp[a]++;
		}
		int cnt=0,m=0;
		for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
		{
			cnt++;
			m=max(m,it->second);
		}
		cout<<max(min(cnt-1,m),min(cnt,m-1))<<endl;;
	}
	return 0;
}

D

D. Anti-Sudoku(题目链接)
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≤𝑖,𝑗≤9 and change the number at the position (𝑖,𝑗) 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 𝑡 independent test cases.

Input
The first line of the input contains one integer 𝑡 (1≤𝑡≤104) — the number of test cases. Then 𝑡 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

题意:
给你个9x9的数独块,数独块是已经成功了的数独块。现在要你搞反数独。
即每3x3块之间得有相同的元素,然后每一行,每一列都得有相同的元素
请你构造这样的块。

题解:额,这个直接把每行的一个元素该成另一个元素就好了吧。我把每行的9改为了8,ac了。
找元素用find就行了

代码:

#include<bits/stdc++.h>
#define sc(x) scanf("%lld",&x);
#define pf printf
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define dep(i,e,s) for(int i=e;i>=s;i--)
using namespace std;
typedef long long ll;
const int inf=~0u>>2;
const int maxn=1e5+7;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
string s[10];
int main()
{
	SIS;
	//¼ÓËÙcin/cout
	int t;
	cin>>t;
	while(t--)
	{
		for(int i=1;i<=9;i++)
		{
			cin>>s[i];
			s[i][s[i].find('9')]='8';
		}
		for(int i=1;i<=9;i++)
			cout<<s[i]<<endl;
	}
	return 0;
}

E1 E2

E1. Three Blocks Palindrome
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 𝑎 consisting of 𝑛 positive integers.

Let’s define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are 𝑎 and 𝑏, 𝑎 can be equal 𝑏) and is as follows: [𝑎,𝑎,…,𝑎𝑥,𝑏,𝑏,…,𝑏𝑦,𝑎,𝑎,…,𝑎𝑥]. There 𝑥,𝑦 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 𝑎 that is a three blocks palindrome.

You have to answer 𝑡 independent test cases.

Recall that the sequence 𝑡 is a a subsequence of the sequence 𝑠 if 𝑡 can be derived from 𝑠 by removing zero or more elements without changing the order of the remaining elements. For example, if 𝑠=[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 𝑡 (1≤𝑡≤2000) — the number of test cases. Then 𝑡 test cases follow.

The first line of the test case contains one integer 𝑛 (1≤𝑛≤2000) — the length of 𝑎. The second line of the test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤26), where 𝑎𝑖 is the 𝑖-th element of 𝑎. Note that the maximum value of 𝑎𝑖 can be up to 26.

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

Output
For each test case, print the answer — the maximum possible length of some subsequence of 𝑎 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

这两题如果代码够优化不是很暴力的话,可以稍改一点点,同时过。

题意:就是给你一个序列,然后你可以删除一些元素。然后使得它成为一个三元回文序列。[ x , y ,x ] x包含很多元素,y包含很多元素,x又包含很多元素。问你三元回文序列的最长长度为多少。

题解:
对每个元素出现次数,即位置做好统计。
然后开始枚举数字1,出现的次数,二分查找,最左边和最右边出现这个数字的位置。然后这个数字之间的数字再做统计,就能计算出来最大的长度了,有点像dp。。但我感觉我写的是暴力。。

代码:

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

int a[2010];
int b[30][2010];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=26;i++) b[i][0]=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=26;j++){
                if(a[i]==j) b[j][i]=b[j][i-1]+1;
                else b[j][i]=b[j][i-1];
            }
        }
        int ans=1;
        for(int i=1;i<=26;i++){     //ö¾Ù26¸öÊý
            for(int j=1;j<=b[i][n]/2;j++){      //ö¾Ù×óÓÒ¸÷Óм¸¸ö¸ÃÊý
                int cnt=j*2;
                int l=lower_bound(b[i],b[i]+n,j)-b[i];      //¶þ·Ö²éÕÒ×î×ó±ßµÚj´Î³öÏÖÕâ¸öÊýµÄλÖÃ
                int r=lower_bound(b[i],b[i]+n,b[i][n]-j+1)-b[i];    //¶þ·Ö²éÕÒ×îÓұߵÚj´Î³öÏÖÕâ¸öÊýµÄλÖÃ
                for(int k=1;k<=26;k++){         //ö¾ÙÖмäµÄÊý×Ö£»
                    ans=max(ans,cnt+b[k][r-1]-b[k][l]);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值