Codeforces Round #661 (Div. 3)题解

Problem A

题目

You are given the array a consisting of n positive (greater than zero) integers.

In one move, you can choose two indices i and j (i≠j) such that the absolute difference between ai and aj is no more than one (|ai−aj|≤1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).

Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.

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 length of a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100), where ai is the i-th element of a.

Output
For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.

Example
inputCopy
5
3
1 2 2
4
5 5 5 5
3
1 2 4
4
1 3 4 4
1
100
outputCopy
YES
YES
NO
NO
YES

思路

给你一坨数字,然后你每次可以选择两个不同数字中的最小的数字删除,前提是这两个数的差的绝对值小于2,问你最后能不能删到只有一个数。考虑一下,假设你有三个数,两两之间差值为1,我们考虑不同决策,显然我们删去中间的数是坏的,以为删掉他,只能让剩下两个的差值变小,那么我们先sort一遍,计算和相邻数的差值,如果有大于等于2的存在,那么无论如何也不会只剩一个数。

Problem C

题面

There are n people who want to participate in a boat competition. The weight of the i-th participant is wi. 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 (a1,b1), (a2,b2), …, (ak,bk), where ai is the weight of the first participant of the i-th team and bi is the weight of the second participant of the i-th team, then the condition a1+b1=a2+b2=⋯=ak+bk=s, 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 w1,w2,…,wn (1≤wi≤n), where wi 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
inputCopy
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
outputCopy
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.

思路

先开始直接开dfs搜了,但t2就tle了,复杂度还是没卡过去。那么我们考虑用map存下所有数的个数,枚举两数之和,然后统计最大值就好了。精髓在于数据量很小,只有50,所以即使我们枚举答案,那么也只需要枚举到100就好了。

Problem D

题目

You are given a binary string s consisting of n zeros and ones.

Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).

Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤2⋅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 s. The second line of the test case contains n characters '0' and '1' — the string s.

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

Output
For each test case, print the answer: in the first line print one integer k (1≤k≤n) — the minimum number of subsequences you can divide the string s to. In the second line print n integers a1,a2,…,an (1≤ai≤k), where ai is the number of subsequence the i-th character of s belongs to.

If there are several answers, you can print any.

Example
inputCopy
4
4
0011
6
111111
5
10101
8
01010000
outputCopy
2
1 2 2 1
6
1 2 3 4 5 6
1
1 1 1 1 1
4
1 1 1 1 1 2 3 4

思路

首先这个不同的字串一定是长度有1位差异的,我们选择用队列去存储,分两个队列,分别代表待匹配的0和1,那么同时对他进行计数,然后当出现一个匹配数的时候,我们出队列,并且加入另外一个队列里面。最后统计次数就好了。

代码实现

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define MT(x,i) memset(x,i,sizeof(x) )
#define rev(i,start,end) for (int i=start;i<end;i++)
#define inf 0x3f3f3f3f
#define mp(x,y) make_pair(x,y)
#define lowbit(x) (x&-x)
#define MOD 1000000007
#define exp 1e-8
#define N 1000005 
#define fi first 
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int ,int> PII;
typedef pair<int ,PII> PIII;
ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }

const int maxn=2e5+9;
string s;
int idx;
int a[maxn];
int x;

void solve () {
    int n;
    scanf ("%d",&n);
    cin>>s;

    idx=0;
    queue <int > q0,q1;
    rev (i,0,n) {
        if (s[i]=='0') {
            if (!q1.empty ()) {
                x=q1.front ();
                q1.pop ();
                a[i]=x;
                q0.push (x);
            }
            else {
                a[i]=++idx;
                q0.push (idx);
            }
        }
        else {
            if (q0.size ()) {
                x=q0.front ();
                q0.pop ();
                a[i]=x;
                q1.push (x);
            }
            else {
                a[i]=++idx;
                q1.push (idx);
            }
        }
    }
    cout<<idx<<endl;
    rev (i,0,n) cout<<a[i]<<" ";
    cout<<endl;
}

int main () {
    int t;
    cin>>t;
    while (t--) solve ();
    return 0;
}
posted @ 2020-08-16 21:37  Luglucky  阅读(192)  评论(0编辑  收藏  举报