Codeforces Round 858 (Div. 2)题解

目录

A. Walking Master(贪心)

题面翻译

思路:

代码实现 

B. Mex Master(贪心)

题面翻译:

思路:

代码实现

C. Sequence Master(数学)

题面翻译:

思路:

代码实现


A. Walking Master(贪心)

YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the top right or the adjacent point on the left.

That is, if she is standing on point (x,y), she can either move to point (x+1,y+1) or point (x−1,y)

YunQian initially stands at point (a,b) and wants to move to point (c,d). Find the minimum number of moves she needs to make or declare that it is impossible.

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of test cases follows.

The first line and only line of each test case contain four integers a, b, c, d(−108≤a,b,c,d≤108).

Output

For each test case, if it is possible to move from point (a,b) to point (c,d), output the minimum number of moves. Otherwise, output −1

Example

input

6

-1 0 -1 2

0 0 4 5

-2 -1 1 1

-3 2 -3 2

2 -1 -1 -1

1 1 0 2

output

4
6
-1
0
3
3

Note

In the first test case, one possible way using 44 moves is (−1,0)→(0,1)→(−1,1)→(0,2)→(−1,2). It can be proven that it is impossible to move from point (−1,0)to point (−1,2) in less than 4 moves.

题面翻译

一个人从(x,y)可以走到(x+1 , y+1)和(x-1,y),问从(a,b)走到(c,d)最少几步

思路:

优先斜着走,b走到d后再横着走到c

走不到的话就输出-1

代码实现 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
    int x,y;
};
int main(){
    int t;
    cin>>t;
    while(t--){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        if(y2<y1){
            cout<<-1<<endl;
            continue;
        }
        int dy=y2-y1;
        if(x2-x1>dy){
            cout<<-1<<endl;
            continue;
        }
        int x=x1+dy;
        cout<<dy+(x-x2)<<endl;
    }
    return 0;
}

 


B. Mex Master(贪心)

You are given an array a of length n. The score of a is the MEX†† of [a1+a2,a2+a3,…,an−1+an]. Find the minimum score of a if you are allowed to rearrange elements of a in any order. Note that you are not required to construct the array a that achieves the minimum score.

†† The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1]is 0, because 0 does not belong to the array.
  • The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not.
  • The MEX of [0,3,1,2] is 4 because 0, 11, 2, and 3 belong to the array, but 4 does not.

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (2≤n≤2⋅105).

The second line of each test case contains n integers a1,a2,…,an(0≤ai≤2⋅105).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output the minimum score of a after rearranging the elements of a in any order.

Example

input

3

2

0 0

3

0 0 1

8

1 0 0 0 2 0 3 0

output

1
0
1

Note

In the first test case, it is optimal to rearrange a as [0,0], the score of this array is the MEX of [0+0]=[0], which is 1.

In the second test case, it is optimal to rearrange a as [0,1,0], the score of this array is the MEX of [0+1,1+0]=[1,1], which is 0.

题面翻译:

给你一个长为n的数列,你对这个数列进行排序,然后构造一个长为n-1新数列,每一项由排序后的相邻两项相加而成,求新数列中不存在的最小非负整数

思路:

如果0的个数少于(n+1)/2,可以构造一个不含0的数列,最小整数为0

0和1的个数小于n,可以把0放在一边,1放在另一半,构造出的数列不含1,最小整数为1,

数列全由0,1组成,用0把1隔开,最小不存在的数为2

代码实现

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[200005];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++){
            int a;
            cin>>a;
            cnt[a]++;
        }
        if(cnt[0]<=(n+1)/2){
            cout<<0<<endl;
            continue;
        }
        if(cnt[1]+cnt[0]<n){
            cout<<1<<endl;
            continue;
        }
        if(cnt[1]==0){
            cout<<1<<endl;
            continue;
        }
        cout<<2<<endl;
    }
    return 0;
}

C. Sequence Master(数学)

For some positive integer m, YunQian considers an array q of 2m (possibly negative) integers good, if and only if for every possible subsequence of q that has length m, the product of the m elements in the subsequence is equal to the sum of the m elements that are not in the subsequence. Formally, let U={1,2,…,2m} For all sets S⊆U such that |S|=m|=, ∏i∈Sqi=∑i∈U∖Sqi∏.

Define the distance between two arrays a and bboth of length k\ to be ∑i=1k|ai−bi|.

You are given a positive integer n and an array p of 2n integers.

Find the minimum distance between p and q over all good arrays q of length 2n. It can be shown for all positive integers n, at least one good array exists. Note that you are not required to construct the array q that achieves this minimum distance.

Input

The first line contains a single integer t (1≤t≤104 — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (1≤n≤2⋅105).

The second line of each test case contains 2n integers p1,p2,…,p2n (|pi|≤109).

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

Output

For each test case, output the minimum distance between p and a good q.

Example

input

4

1

6 9

2

1 2 2 1

2

-2 -2 2 2

4

-3 -2 -1 0 1 2 3 4

output

Copy

3
2
5
13

Note

In the first test case, it is optimal to let q=[6,6].

In the second test case, it is optimal to let q=[2,2,2,2].

题面翻译:

给你一个长为2*n的数列,找一个长为2*n的数列满足任意n项的和等于另外n项的积,使得这两个数列每一项之差的绝对值的和最小

思路:

md纯一个数学题

n=1的时候就是两个数的差值

n=2的时候,三种情况,全是2,全是0,3个-1和一个2,全部讨论取最小值即可

n%2=0的时候,要么全是0,要么是2n-1个-1和一个n,讨论

n%2=1的时候,全是0

代码实现

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[400005];
int main(){
    int t;
    cin>>t;
    while(t--){
        ll n,ans=LLONG_MAX;
        cin>>n;
        for(int i=0;i<2*n;i++){
            cin>>a[i];
        }
        if(n==1){
            ans=abs(a[1]-a[0]);
        }
        if(n==2){
            ll sum=0;
            for(int i=0;i<2*n;i++){
                sum+=abs(a[i]-2);
            }
            ans=sum;
        }
        if(n%2==0){
            ll sum=0;
            for(int i=0;i<2*n;i++){
                sum+=abs(a[i]+1);
            }
            for(int i=0;i<2*n;i++){
                ans=min(ans,sum-abs(a[i]+1)+abs(a[i]-n));
            }
        }
        ll sum=0;
        for(int i=0;i<2*n;i++){
            sum+=abs(a[i]);
        }
        ans=min(ans,sum);
        cout<<ans<<endl;
    }
    return 0;
}

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Auroraaaaaaaaaaaaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值