C. Boats Competition
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
There are n people who want to participate in a boat competition. The weight of the i-th participant is . Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight.
So, if there are k teams ,
, ……,
, where
is the weight of the first participant of the i-th team and
is the weight of the second participant of the i-th team, then the condition
, where s is the total weight of each team, should be satisfied.
Your task is to choose such s that the number of teams people can create is the maximum possible. Note that each participant can be in no 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≤1000) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤50) — the number of participants. The second line of the test case contains n integers (1≤
≤n), where
is the weight of the i-th participant.
Output
For each test case, print one integer k: the maximum number of teams people can compose with the total weight s, if you choose s optimally.
Example
input
5 5 1 2 3 4 5 8 6 6 6 6 6 6 8 8 8 1 2 2 1 2 1 1 2 3 1 3 3 6 1 1 3 4 2 2
output
2 3 4 1 2
Note
In the first test case of the example, we can reach the optimal answer for s=6. Then the first boat is used by participants 1 and 5 and the second boat is used by participants 2 and 4 (indices are the same as weights).
In the second test case of the example, we can reach the optimal answer for s=12. Then first 6 participants can form 3 pairs.
In the third test case of the example, we can reach the optimal answer for s=3. The answer is 4 because we have 4 participants with weight 1 and 4 participants with weight 2.
In the fourth test case of the example, we can reach the optimal answer for s=4 or s=6.
In the fifth test case of the example, we can reach the optimal answer for s=3. Note that participant with weight 3 can't use the boat because there is no suitable pair for him in the list.
题解:
新的一个月做题情况,从这道1200分的题目开始。题目大意是说让你统计相加等于一个数的配对的最大值。考虑到n最大是50,我们可以放心的使用暴力的思想。相当于是设计一个双指针,s的范围是2~2*n,对于区间中的每个数都判断配对数量是多少,取最大值即为答案。
AC代码:
#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define f(i,ed,st) for(int i=ed-1;i>=st;i--)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
#define PI acos(-1)
using namespace std;
const int N=1e5+5;
int a[N];
void solve(){
int n;
cin>>n;
F(i,0,n){
cin>>a[i];
}
sort(a,a+n);
int ans=0;
F(i,2,2*n+1){
int l=0,r=n-1;
int cnt=0;
while (l<r){
if (a[l]+a[r]==i){
cnt++;
l++,r--;
}
else if (a[l]+a[r]<i){
l++;
}
else{
r--;
}
}
ans=max(cnt,ans);
}
cout<<ans<<endl;
}
int main()
{
BUFF;
int _;
cin>>_;
//_=1;
F(i,0,_){
solve();
}
return 0;
}
D. Anti-Sudoku
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard 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 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≤) — 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
input
1 154873296 386592714 729641835 863725149 975314628 412968357 631457982 598236471 247189563
output
154873396 336592714 729645835 863725145 979314628 412958357 631457992 998236471 247789563
题解:
我没想到这个题竟然会放到D题,还是1300分。其实这就是数独的一个特点,即每行每列以及每个3×3的方格中1~9只能出现一次。那他的这个变化其实就是在每行每列中选择一个元素替换就行,最简单的方法就是把所有的1(或2~9)都替换为9(或1~9,只要不是自己就行),最后在把答案输出出来就行。
AC代码:
#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define f(i,ed,st) for(int i=ed-1;i>=st;i--)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
#define PI acos(-1)
using namespace std;
void solve(){
string s[10];
F(i,0,9){
cin>>s[i];
}
F(i,0,9){
string t=s[i];
F(j,0,t.length()){
if (t[j]=='1'){
t[j]='9';
}
}
s[i]=t;
}
F(i,0,9){
cout<<s[i]<<endl;
}
}
int main()
{
BUFF;
int _;
cin>>_;
//_=1;
F(i,0,_){
solve();
}
return 0;
}
A. Vasya the Hipster
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and b blue socks.
According to the latest fashion, hipsters should wear the socks of different colors: a red one on the left foot, a blue one on the right foot.
Every day Vasya puts on new socks in the morning and throws them away before going to bed as he doesn't want to wash them.
Vasya wonders, what is the maximum number of days when he can dress fashionable and wear different socks, and after that, for how many days he can then wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.
Can you help him?
Input
The single line of the input contains two positive integers a and b (1 ≤ a, b ≤ 100) — the number of red and blue socks that Vasya's got.
Output
Print two space-separated integers — the maximum number of days when Vasya can wear different socks and the number of days when he can wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.
Keep in mind that at the end of the day Vasya throws away the socks that he's been wearing on that day.
Examples
input
3 1
output
1 1
input
2 3
output
2 0
input
7 3
output
3 2
Note
In the first sample Vasya can first put on one pair of different socks, after that he has two red socks left to wear on the second day.
题解:
最近做题有点儿乏味,重新回到800分的题目,提升下兴趣。题面意思是说让你输出穿不同颜色袜子的天数和相同袜子的天数,只需要对两种颜色判断一下谁大谁小再输出就可以了。
AC代码:
#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define f(i,ed,st) for(int i=ed-1;i>=st;i--)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
#define PI acos(-1)
using namespace std;
ll ksc(ll a,ll b,ll mod){
ll res=0;
while (b){
if (b&1){
res=(res+a)%mod;
}
a=(a+a)%mod;
b>>=1;
}
return res;
}
ll ksm(ll a,ll b,ll mod){
ll res=1;
while (b){
if (b&1){
res=(res*a)%mod;
}
a=(a*a)%mod;
b>>=1;
}
return res;
}
const int N=1e5+5;
int a[N],b[N];
void solve(){
int x,y;
cin>>x>>y;
if (x>y){
cout<<y<<" "<<(x-y)/2<<endl;
}
else{
cout<<x<<" "<<(y-x)/2<<endl;
}
}
int main()
{
BUFF;
int _;
//cin>>_;
_=1;
F(i,0,_){
solve();
}
return 0;
}
B. K-th Beautiful String
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
For the given integer n (n>2) let's write down all the strings of length n which contain n−2 letters 'a' and two letters 'b' in lexicographical (alphabetical) order.
Recall that the string s of length n is lexicographically less than string t of length n, if there exists such i (1≤i≤n), that , and for any j (1≤j<i)
. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.
For example, if n=5 the strings are (the order does matter):
- aaabb
- aabab
- aabba
- abaab
- ababa
- abbaa
- baaab
- baaba
- babaa
- bbaaa
It is easy to show that such a list of strings will contain exactly strings.
You are given n (n>2) and k (1≤k≤). Print the k-th string from the list.
Input
The input contains one or more test cases.
The first line contains one integer t (1≤t≤) — the number of test cases in the test. Then t test cases follow.
Each test case is written on the the separate line containing two integers n and k (3≤n≤,1≤k≤min(2⋅
,
)).
The sum of values n over all test cases in the test doesn't exceed .
Output
For each test case print the k-th string from the list of all described above strings of length n. Strings in the list are sorted lexicographically (alphabetically).
Example
input
7 5 1 5 2 5 8 5 10 3 1 3 2 20 100
output
aaabb aabab baaba bbaaa abb bab aaaaabaaaaabaaaaaaaa
题解:
这道1300分的题目很有质量,本质上是一个找规律的题目。首先我们可以分析出倒数第二个b,它的出现是个类似于等差数列的形式,所以可以通过k找到他是第几项,然后最后一个b是有规律的,可以从已知的1、3、6、10……这些项中倒着推,最后把答案输出出来即可。
AC代码:
#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define f(i,ed,st) for(int i=ed-1;i>=st;i--)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
#define PI acos(-1)
using namespace std;
ll ksc(ll a,ll b,ll mod){
ll res=0;
while (b){
if (b&1){
res=(res+a)%mod;
}
a=(a+a)%mod;
b>>=1;
}
return res;
}
ll ksm(ll a,ll b,ll mod){
ll res=1;
while (b){
if (b&1){
res=(res*a)%mod;
}
a=(a*a)%mod;
b>>=1;
}
return res;
}
const int N=1e5+5;
int a[N],b[N];
void solve(){
ll n,k;
cin>>n>>k;
ll i,p,q;
for (i=1;i<=n;i++){
if (i*(i+1)/2>=k){
break;
}
}
p=n-i;
q=i*(i+1)/2-k+p+1;
for (i=1;i<=n;i++){
if (i==p||i==q){
cout<<"b";
}
else{
cout<<"a";
}
}
cout<<endl;
}
int main()
{
BUFF;
int _;
cin>>_;
//_=1;
F(i,0,_){
solve();
}
return 0;
}
A. Boredom
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.
Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ) and delete it, at that all elements equal to
and
also must be deleted from the sequence. That step brings
points to the player.
Alex is a perfectionist, so he decided to get as many points as possible. Help him.
Input
The first line contains integer n (1 ≤ n ≤ ) that shows how many numbers are in Alex's sequence.
The second line contains n integers (1 ≤
≤
).
Output
Print a single integer — the maximum number of points that Alex can earn.
Examples
input
2 1 2
output
2
input
3 1 2 3
output
4
input
9 1 2 1 3 2 2 2 2 3
output
10
Note
Consider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points.
题解:
这是一个很简单的动态规划问题。首先要明确一点,对于每个数k,它所贡献的分数是k*a[k],其中a[k]为k在数组中的个数。对于数字k,我们可以选,也可以不选,取决于k-1这个数的贡献是否超过了k的贡献,因为数字k有a[k]个,那么对于dp[k]来说,它的值取决于dp[k-1]和dp[k-2]+k*a[k]的最大值就是答案。最后把dp[N]输出即为最后的结果。
AC代码:
#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define f(i,ed,st) for(int i=ed-1;i>=st;i--)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
#define PI acos(-1)
using namespace std;
ll ksc(ll a,ll b,ll mod){
ll res=0;
while (b){
if (b&1){
res=(res+a)%mod;
}
a=(a+a)%mod;
b>>=1;
}
return res;
}
ll ksm(ll a,ll b,ll mod){
ll res=1;
while (b){
if (b&1){
res=(res*a)%mod;
}
a=(a*a)%mod;
b>>=1;
}
return res;
}
const int N=1e5+5;
ll a[N];
ll dp[N];
void solve(){
int n;
cin>>n;
F(i,0,n){
int k;
cin>>k;
a[k]++;
}
M(dp);
dp[1]=a[1];
F(i,2,N){
dp[i]=max(dp[i-1],dp[i-2]+i*a[i]);
}
cout<<dp[N-1]<<endl;
}
int main()
{
BUFF;
int _;
//cin>>_;
_=1;
F(i,0,_){
solve();
}
return 0;
}
A. Sum of Round Numbers
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from 1 to 9 (inclusive) are round.
For example, the following numbers are round: 4000, 1, 9, 800, 90. The following numbers are not round: 110, 707, 222, 1001.
You are given a positive integer n (1≤n≤). Represent the number n as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number nn as a sum of the least number of terms, each of which is a round number.
Input
The first line contains an integer t (1≤t≤) — the number of test cases in the input. Then t test cases follow.
Each test case is a line containing an integer n (1≤n≤).
Output
Print t answers to the test cases. Each answer must begin with an integer k — the minimum number of summands. Next, k terms must follow, each of which is a round number, and their sum is n. The terms can be printed in any order. If there are several answers, print any of them.
Example
input
5 5009 7 9876 10000 10
output
2 5000 9 1 7 4 800 70 6 9000 1 10000 1 10
题解:
考虑到上次比赛A题被干了,这次就先放放高分题,重新回到800分的题目练一下子。题目让你输出k个加和为n的数,每个数都由d00…0这样的形式表示,很显然我们只需要对数字n的非0位操作一下就可以了。
AC代码:
#include <bits/stdc++.h>
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
#define endl '\n'
#define F(i,st,ed) for(int i=st;i<ed;i++)
#define f(i,ed,st) for(int i=ed-1;i>=st;i--)
#define M(x) memset(x,0,sizeof(x))
#define pb push_back
#define PI acos(-1)
using namespace std;
ll ksc(ll a,ll b,ll mod){
ll res=0;
while (b){
if (b&1){
res=(res+a)%mod;
}
a=(a+a)%mod;
b>>=1;
}
return res;
}
ll ksm(ll a,ll b,ll mod){
ll res=1;
while (b){
if (b&1){
res=(res*a)%mod;
}
a=(a*a)%mod;
b>>=1;
}
return res;
}
void solve(){
int n;
cin>>n;
int a[8]={0,0,0,0,0,0,0,0};
int idx=0,cnt=0;
while (n){
a[idx]=(n%10)*pow(10,idx);
if (a[idx]!=0){
cnt++;
}
n/=10;
idx++;
}
cout<<cnt<<endl;
F(i,0,8){
if (a[i]!=0){
cout<<a[i]<<" ";
}
}
cout<<endl;
}
int main()
{
BUFF;
int _;
cin>>_;
//_=1;
F(i,0,_){
solve();
}
return 0;
}