首页 > 其他分享 >题解:Pinely Round 4 (Div. 1 + Div. 2) C

题解:Pinely Round 4 (Div. 1 + Div. 2) C

时间:2024-08-01 10:41:37浏览次数:11  
标签:10 le 题解 40 偶数 测试用例 test Div Round

C. Absolute Zero

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given an array \(a\) of \(n\) integers.

In one operation, you will perform the following two-step move:

  1. Choose an integer \(x\) (\(0 \le x \le 10^{9}\)).
  2. Replace each \(a_i\) with \(|a_i - x|\), where \(|v|\) denotes the absolute value of \(v\).

For example, by choosing \(x = 8\), the array \([5, 7, 10]\) will be changed into \([|5-8|, |7-8|, |10-8|] = [3,1,2]\).

Construct a sequence of operations to make all elements of \(a\) equal to \(0\) in at most \(40\) operations or determine that it is impossible. You do not need to minimize the number of operations.

给你一个由 \(n\) 个整数组成的数组 \(a\) 。

在一次操作中,您将执行以下两步移动:

  1. 选择一个整数 \(x\) ( \(0 \le x \le 10^{9}\) )。
  2. 将每个 \(a_i\) 替换为 \(|a_i - x|\) ,其中 \(|v|\) 表示 \(v\) 的 绝对值

例如,选择 \(x = 8\) 后,数组 \([5, 7, 10]\) 将变为 \([|5-8|, |7-8|, |10-8|] = [3,1,2]\) 。

构建一个操作序列,使 \(a\) 中的所有元素在最多 \(40\) 次操作中等于 \(0\) ,或者确定这是不可能的。您不需要****尽量减少运算次数。

Input

Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the array \(a\).

The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) — the elements of the array \(a\).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

输入

每个测试包含多个测试用例。第一行包含一个整数 \(t\) ( \(1 \le t \le 10^4\) )--测试用例数。( \(1 \le t \le 10^4\) )--测试用例的数量。测试用例说明如下。

每个测试用例的第一行都包含一个整数 \(n\) ( \(1 \le n \le 2 \cdot 10^5\) ) - 数组 \(a\) 的长度。

每个测试用例的第二行包含 \(n\) 个整数 \(a_1, a_2, \ldots, a_n\) ( \(0 \le a_i \le 10^9\) ) - 数组 \(a\) 的元素。

保证所有测试用例中 \(n\) 的总和不超过 \(2 \cdot 10^5\) 。

Output

For each test case, output a single integer \(-1\) if it is impossible to make all array elements equal to \(0\) in at most \(40\) operations.

Otherwise, output two lines. The first line of output should contain a single integer \(k\) (\(0 \le k \le 40\)) — the number of operations. The second line of output should contain \(k\) integers \(x_1, x_2, \ldots, x_k\) (\(0 \le x_i \le 10^{9}\)) — the sequence of operations, denoting that on the \(i\)-th operation, you chose \(x=x_i\).

If there are multiple solutions, output any of them.

You do not need to minimize the number of operations.

输出

对于每个测试用例,如果不可能在最多 \(40\) 次操作中使所有数组元素等于 \(0\) ,则输出一个整数 \(-1\) 。

否则,输出两行。第一行输出应包含一个整数 \(k\) ( \(0 \le k \le 40\) ) - 运算次数。第二行输出应该包含 \(k\) 个整数 \(x_1, x_2, \ldots, x_k\) ( \(0 \le x_i \le 10^{9}\) ) - 运算序列,表示在 \(i\) -th 运算中,你选择了 \(x=x_i\) 。

如果有多个解,请输出其中任意一个。

无需最小化操作次数。

Example

Input

5
1
5
2
0 0
3
4 6 8
4
80 40 20 10
5
1 2 3 4 5

Output

1
5
0

3
6 1 1
7
60 40 20 10 30 25 5
-1

Note

In the first test case, we can perform only one operation by choosing \(x = 5\), changing the array from \([5]\) to \([0]\).

In the second test case, no operations are needed because all elements of the array are already \(0\).

In the third test case, we can choose \(x = 6\) to change the array from \([4, 6, 8]\) to \([2, 0, 2]\), then choose \(x = 1\) to change it to \([1, 1, 1]\), and finally choose \(x = 1\) again to change the array into \([0, 0, 0]\).

In the fourth test case, we can make all elements \(0\) by following the operation sequence \((60, 40, 20, 10, 30, 25, 5)\).

In the fifth test case, it can be shown that it is impossible to make all elements \(0\) in at most \(40\) operations. Therefore, the output is \(-1\).

在第一个测试用例中,我们只需选择 \(x = 5\) ,将数组从 \([5]\) 变为 \([0]\) ,就可以执行一次操作。

在第二个测试用例中,不需要进行任何操作,因为数组的所有元素都已经是 \(0\) 。

在第三个测试用例中,我们可以选择 \(x = 6\) 将数组从 \([4, 6, 8]\) 变为 \([2, 0, 2]\) ,然后选择 \(x = 1\) 变为 \([1, 1, 1]\) ,最后再次选择 \(x = 1\) 变为 \([0, 0, 0]\) 。

在第四个测试用例中,我们可以按照 \((60, 40, 20, 10, 30, 25, 5)\) 的操作序列将所有元素变为 \(0\) 。

在第五个测试用例中,我们可以证明不可能在最多 \(40\) 次操作中将所有元素变为 \(0\) 。因此,输出为 \(-1\) 。

题意

给你一个数组 \(a\)
你可以对数组 \(a\) 做一种操作:选择一个数字 \(x\),使a中的所有元素变成 a与x的差的绝对值
问:能否通过不超过 \(40\) 次操作,使数组的所有元素变成 \(0\)

题解

看到 40 这个数字,有没有什么感觉
刚刚好没有超过 32 啊(联想一下 2^32
那是不是可以二分呢??我们再看看
假如说一个我选择一个数 \(x\) ,使所有数字变成 \(|a_i - x|\)
那么我们可以看得出来,只要 \(a_i \le x \times 2\)
就能得到: \(|a_i - x| \le x\)
即使最初的这个数很大很大(甚至 \(1 \times 10^9\) )
我也能一层一层二分给他削减下去,直到最后为 \(0\)

注:因为数字不能超过\(1 \times 10^9\),所以我们只能从 \(2^{30}\) 开始往下减

第二个问题:怎么确认一个数组里面全部数字能不能变成 \(0\) ?
已知:

  • 奇数相减/偶数相减 得到的只会是偶数
  • 奇偶相减 得到的只会是奇数

我们来对比一下:

  • 奇数
    • 和奇数相减 偶数
    • 和偶数相减 奇数
  • 偶数
    • 和奇数相减 奇数
    • 和偶数相减 偶数

我们可以看到

  • 不管是奇数还是偶数,和奇数相减,奇偶性改变
  • 不管是奇数还是偶数,和偶数相减,奇偶性不变

也就是说,不管我怎么操作,他们的奇偶性相对而言都是不变的
而最后我们要全部变成0
说明奇偶性要一致
所以我们只要判断是不是全部是奇数或者偶数就可以了

  • 是,就可以全部变成0
  • 不是,就不可以

PS:
从这里我们又能看到,因为 \(2\) 的任意次方都是偶数,没有改变奇偶性( \(0\) 次方除外)
所以假如数组全部是偶数的话,减去一个 \(1\) 之后奇偶性改变,要再减去一个 \(1\)

代码

#include <bits/stdc++.h>
#define int long long

int bii[30];

void init() {
    bii[0] = 1;
    for(int i = 1 ; i < 30 ; i ++) bii[i] = bii[i-1] * 2;
}

void solve() {
    int n,num,n1;
    std::cin >> n;
    int ji=0,ou=0;
    for(int i = 0 ; i < n ; i ++) {
        std::cin >> num;
        if(num % 2 == 1) ji++;
        else ou++;
    }

    if(ji > 0 && ou > 0) std::cout << -1 << "\n";
    else {
        std::cout << 30 + (ou ? 1 : 0) << "\n";

        for(int i = 29 ; i >= 0 ; i --) {
            std::cout << bii[i] << " ";
        }

        if(ou > 0) std::cout << 1;

        std::cout << "\n";
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    init();

    int t;
    std::cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

标签:10,le,题解,40,偶数,测试用例,test,Div,Round
From: https://www.cnblogs.com/jiejiejiang2004/p/18336168

相关文章

  • Codeforces 11D A Simple Task 题解 [ 蓝 ] [ 状压 dp ]
    思路不难想,细节比较多。思路观察到\(n\le19\),首先想到状压dp。于是自然地定义\(dp[j][i]\)为:抵达点的状态为\(i\),且此时在点\(j\)时,简单路径的条数。注意这里是简单路径的条数,而不是环的个数,因为环的个数要在dp过程中统计。这里的dp作用就在于求简单路径条数。......
  • Codeforces Round 943 (Div. 3)A~E
    A.Maximize?题目大意:给你一个数x,你需要找到一个数y(1<=y<x),使得gcd(x,y)+y值最大,然后输出这个y思路:看范围暴力即可voidsolve(){inta,b=0,maxx=0,bj=0;cin>>a;for(inti=1;i<a;i++){b=__gcd(a,i);b+=i;if(maxx<b)......
  • [JOI 2020 Final] 火事 题解
    给一篇题解。(下面这张图是从luogu上粘贴的,因为不太会画图)其中纵坐标为\(t\),横坐标为\(a_i\)。发现同颜色块只有平行四边形和直角梯形(等腰直角三角形)两种情况。可以将直角梯形削去左下角,分成两部分考虑。等直可以直接暴力插入区间,总个数\(O(n)\)。平行四边形可以看作上......
  • [CF455D] Serega and Fun 题解
    不知道大家做没做过数列分块基础9题?插入删除操作可以用链表,线段树等数据结构都不好维护,考虑分块。对于修改操作,暴力重构受影响块的链表,发现除首尾块外,其他块都可以看作是区间左移一位,所以加头删尾即可。每个块开一个数组(绝对不能是\((un\_)map\),不然你会和我一样死的很诡异),表示......
  • HDU2024 R2 T9 题解
    考虑维护一下每个点的速度。把区间加拆成后缀加和后缀减,然后考虑后缀加。减就同理。考虑在一段后缀的目标速度增加之后,哪些时刻的加速度会变化。这里加速度必然只会变大\(1\),因此在这个时刻之后的速度都会增加\(1\),又由于目标速度也增加了\(1\),所以这个位置之后的加速度都不再......
  • CF455D Serega and Fun 题解
    Beforeit来当一回教练的题解翻译家,平衡树这种高级算法自然是不会的,所以详细讲一下STL。成为蒟蒻(也就是自己)的福音。Solution我们观察到题目要求“把第\(r\)个数插入在第\(l\)个数之前,其他数顺序不变“,使用\(deque\)的\(push\)_\(front\)和\(push\)_$back$操作可......
  • 暗区突围pc端下载失败/卡正在初始化/连接伺服务器失败/问题解决方法
    暗区突围pc端下载失败/卡正在初始化/连接伺服务器失败/问题解决方法暗区突围pc端下载失败/卡正在初始化/连接伺服务器失败/问题解决方法暗区突围也可以在电脑上游玩拉,暗区突围PC端上线在即,本次上线就是全球抢先测试了,很多小伙伴在游戏下载过程中遇到了很多问题,比如:下载失......
  • QOJ6504 Flower‘s Land 2 题解
    QOJ6504Flower'sLand2题解题目链接:QOJ6504Flower'sLand2题意:给定一个只包含\(0,1,2\)的序列,\(T\)次询问,询问有两种:区间所有数加\(1\)然后模\(3\)求一段区间能否通过每次删掉相邻两个相同的数删完(如\(1,0,0,2,2,1\)就满足条件)题解:考虑用什么方法来维护区间......
  • CF1995C Squaring 题解
    思路详解:请注意,本题解用到了非整数计算,也就是说性能可能不如整数运算,但是易于实现,追求最优解的大佬不建议观看本题解。这个题看似简单,但是由于涉及到了平方操作,不用高精度根本存不下,然后如果你要用高精度的话又会T......
  • 关于题解
    这里说一下为什么要在博客园写东西,其实有几个主要原因:首先是我一个朋友写题解的时候,因为多了一个空格被打回。然后我这个朋友就开始在这里写东西了。然后这个朋友找到了一个拿过金牌的学长,他同样之前审过题解,总之他认为只要不是题解写的完全不能看,或者在某些地方有一些事实性错......