Codeforces Round 858 (Div. 2)题解

这篇具有很好参考价值的文章主要介绍了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文章来源地址https://www.toymoban.com/news/detail-403303.html

代码实现

#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;
}

到了这里,关于Codeforces Round 858 (Div. 2)题解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Educational Codeforces Round 145 Div. 2 题解

    目录 A. Garland(签到) 题面翻译 思路: 代码 B. Points on Plane(数学) 题面翻译 思路: 代码 C. Sum on Subarray(构造) 题面翻译: 思路: 代码 D. Binary String Sorting 题面翻译 思路: 代码 You have a garland consisting of 4 colored light bulbs, the color of the i-th light bulb is si. Initially, all the l

    2023年04月09日
    浏览(27)
  • Educational Codeforces Round 147 div2题解

    目录 A. Matching(签到) 思路: 代码:  B. Sort the Subarray(签到) 思路: 代码: C. Tear It Apart(枚举) 思路: 代码: D. Black Cells(模拟) 思路:  代码一: 代码二:(模仿自\\\"AG\\\"佬) An integer template is a string consisting of digits and/or question marks. A positive (strictly greater than 0) in

    2023年04月21日
    浏览(26)
  • Educational Codeforces Round 134 (Div.2) D 题解

    D. Maximum AND 给定两组序列 (a) (b) ,长度为 (n) ,现有一新序列 (c) ,长度也为 (n) 。 其中, (c_i = a_i oplus b_i) 。 定义 (f(a,b) = c_1c_2……c_n) 。 现在你可以随意编排 (b) 序列的顺序,求 (f(a,b)) 的最大值。 以下位运算均是二进制。 由于按位与的运算结果是越来越小的

    2024年02月06日
    浏览(31)
  • Educational Codeforces Round 148 (Rated for Div. 2) 题解

    总结:5.21下午VP一场,死在了A题,给我wa崩溃了,浪费了差不多一个小时,BC还挺顺畅,虽然C题是在结束后不久交上去的。。。。 思路:其实思路很简单, “ The string s a palindrome ”, 题目已经说了所给的为回文字符串,所以直接判断一半 有几种字符 即可(开始的时候计算整

    2024年02月06日
    浏览(29)
  • Codeforces Round 890 (Div. 2) D. More Wrong(交互题 贪心/启发式 补写法)

    题目 t(t=100)组样例,长为n(n=2000)的序列 交互题,每次你可以询问一个区间[l,r]的逆序对数,代价是 要在的代价内问出最大元素的位置,输出其位置 思路来源 neal Codeforces Round 890 (Div. 2) supported by Constructor Institute D (交互+分治) 附加强 - 知乎 题解 赛中开题顺序大失败没看这个

    2024年02月14日
    浏览(27)
  • Codeforces Round 169 (Div. 2)C. Little Girl and Maximum Sum(差分、贪心)

    C. Little Girl and Maximum Sum 给q个[l,r]将所有这些区间里面的数相加和最大。 可以进行的操作是任意排列数组 对出现的每个区间内的位置加上1,代表权值 操作完之后求一遍前缀和,得到每个位置的权值 然后贪心的考虑,权值越大,应该分配给该位置的数越大越好这样对答案的贡

    2024年02月21日
    浏览(25)
  • Codeforces Round 916 (Div. 3)(E:贪心 F贪心dfs G tarjan+topsort +线段树优化建图)

    A:直接暴力统计每个字符的次数是否达标即可 B:直接先输出所需要的k,然后降序输出剩下的即可 C: 直接枚举最后操作到哪个位置就行了,然后贪心一直操作1到i的位置的b最大值即可 D: 先固定第一次是A 第二次是B 第三次是C 枚举B为中介点i,然后求1到i-1的A的最大值,和

    2024年01月17日
    浏览(28)
  • Codeforces Round 867 (Div. 3)

    从所有a[i]+i-1=t的选择种取个max即可 实际上就是取同符号乘积的最大值 找规律,发现结果与边长n的关系是:res = n * (n + 3) - (n - 2) ①当n为奇数时,除了1其他均无解 ②当n为偶数时,我们可以构造一个形如n,1,n - 2,3,...的数列 首先我们可以发现n必定出现在起始位置。如果n不在起

    2024年02月02日
    浏览(33)
  • Codeforces Round 868 Div 2

    要求构造一个仅包含 (1) 和 (-1) 的长度为 (n) 的数组 (a) ,使得存在 (k) 个下标对 ((i, j), i j) 满足 (a_i times a_j = 1) 。 当有 (x) 个 (1) , (y) 个 (-1) 时,其满足条件的下标对数量为 (frac{x (x - 1)}{2} + frac{y (y - 1)}{2}) 。 由于 (n) 只有 (100) ,直接枚举 (x) 即可。

    2024年02月01日
    浏览(30)
  • Codeforces Round 871 (Div. 4)

    给定n个长度为10的字符串,问其与codeforces字符串的对应下标字母不同的个数。 对于每个字符串从前往后依次和“codeforces”对应字符比较然后统计不同字母数即可 给定长度为n的数组,问连续相邻为0的最大长度 维护一个全局最大长度res,和当前连续序列的长度cnt。从前往后扫

    2024年02月03日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包