A. Most Unstable Array
作者:互联网
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|∑i=1n−1|ai−ai+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.
InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — 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≤n,m≤109) — the length of the array and its sum correspondingly.
OutputFor each test case, print the answer — the maximum possible value of ∑i=1n−1|ai−ai+1|∑i=1n−1|ai−ai+1| for the array aa consisting of nn non-negative integers with the sum mm.
地址:https://codeforces.com/contest/1353/problem/A
思路:当n=1时,ans=0,当n=2时ans=m,当n>=3时,数组为{0,m,0,0,0,0,0,0,.....} ans=2*m
int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; if (n == 1) cout << 0 << endl; else if (n == 2) cout << m << endl; else cout << m * 2 << endl; } return 0; }
标签:integers,ai,sum,value,Most,Unstable,test,Array,array 来源: https://www.cnblogs.com/yin101/p/12897118.html