CF1353A.Most Unstable Array

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two integers nn and mm. You have to construct the array aa of length nn consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly mm and the value ∑i=1n−1∣ai−ai+1∣\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}| is the maximum possible. Recall that ∣x∣|x| is the absolute value of xx.

In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array a=[1,3,2,5,5,0]a=[1, 3, 2, 5, 5, 0] then the value above for this array is ∣1−3∣+∣3−2∣+∣2−5∣+∣5−5∣+∣5−0∣=2+1+3+0+5=11|1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11. Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.

You have to answer tt independent test cases.

给你两个整数 nn 和 mm。你需要构造一个长度为 nn 的数组 aa,其中每个元素均为非负整数(即大于或等于零的整数),使得该数组所有元素之和恰好为 mm,并且表达式 ∑i=1n−1∣ai−ai+1∣\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}| 的值达到最大可能。注意,∣x∣|x| 表示 xx 的绝对值。

换言之,你需要最大化相邻(连续)元素之间绝对差值的总和。例如,若数组 a=[1,3,2,5,5,0]a=[1, 3, 2, 5, 5, 0],则上述表达式的值为 ∣1−3∣+∣3−2∣+∣2−5∣+∣5−5∣+∣5−0∣=2+1+3+0+5=11|1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11。注意,该例子并未展示最优解,仅用于说明如何对某个数组计算该目标值。

你需要回答 tt 个相互独立的测试用例。

输入格式

The first line of the input contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Then tt test cases follow.

The only line of the test case contains two integers nn and mm (1≤n,m≤1091 \le n, m \le 10^9) — the length of the array and its sum correspondingly.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例仅一行,包含两个整数 nn 和 mm(1≤n,m≤1091 \le n, m \le 10^9),分别表示数组的长度及其元素之和。

输出格式

For each test case, print the answer — the maximum possible value of ∑i=1n−1∣ai−ai+1∣\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}| for the array aa consisting of nn non-negative integers with the sum mm.

对于每个测试用例,输出答案——即由 nn 个非负整数组成、且元素之和为 mm 的数组 aa 所能取到的 ∑i=1n−1∣ai−ai+1∣\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}| 的最大可能值。

输入输出样例

  • 输入#1

    5
    1 100
    2 2
    5 5
    2 1000000000
    1000000000 1000000000

    输出#1

    0
    2
    10
    1000000000
    2000000000

说明/提示

In the first test case of the example, the only possible array is [100][100] and the answer is obviously 00.

In the second test case of the example, one of the possible arrays is [2,0][2, 0] and the answer is ∣2−0∣=2|2-0| = 2.

In the third test case of the example, one of the possible arrays is [0,2,0,3,0][0, 2, 0, 3, 0] and the answer is ∣0−2∣+∣2−0∣+∣0−3∣+∣3−0∣=10|0-2| + |2-0| + |0-3| + |3-0| = 10.

在示例的第一个测试用例中,唯一可能的数组是 [100][100],答案显然为 00。

在示例的第二个测试用例中,一个可能的数组是 [2,0][2, 0],答案为 ∣2−0∣=2|2-0| = 2。

在示例的第三个测试用例中,一个可能的数组是 [0,2,0,3,0][0, 2, 0, 3, 0],答案为 ∣0−2∣+∣2−0∣+∣0−3∣+∣3−0∣=10|0-2| + |2-0| + |0-3| + |3-0| = 10。

输入解题思路,AI测评打分。不知道怎么写?

首页