A. Most Unstable Array
You are given two integers n and m. You have to construct the array a of length n 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 m and the value ∑i=1n−1|ai−ai+1| is the maximum possible. Recall that |x| is the absolute value of x.
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] then the value above for this array is |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 t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤1e4) — the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and m (1≤n,m≤1e9) — the length of the array and its sum correspondingly.
Output
For each test case, print the answer — the maximum possible value of ∑i=1n−1|ai−ai+1| for the array a consisting of n non-negative integers with the sum m.
Example
input
5
1 100
2 2
5 5
2 1000000000
1000000000 1000000000
output
0
2
10
1000000000
2000000000
题意:构造一个长度为n,各元素之和为m得数组,求数组相邻两元素差的绝对值的和的最大值。
思路:1和2特判,其余直接输出m*2
lude<bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 1000000007
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
#define cio ios::sync_with_stdio(false);
int main()
{
int t;
cin >> t;
while(t--){
ll 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;
}
B. Two Arrays And Swaps
You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also given an integer k.
In one move, you can choose two indices i and j (1≤i,j≤n) and swap ai and bj (i.e. ai becomes bj and vice versa). Note that i and j can be equal or different (in particular, swap a2 with b2 or swap a3 and b9 both are acceptable moves).
Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k such moves (swaps).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤200) — the number of test cases. Then t test cases follow.
The first line of the test case contains two integers n and k (1≤n≤30;0≤k≤n) — the number of elements in a and b and the maximum number of moves you can do. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤30), where ai is the i-th element of a. The third line of the test case contains n integers b1,b2,…,bn (1≤bi≤30), where bi is the i-th element of b.
Output
For each test case, print the answer — the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k swaps.
Example
input
5
2 1
1 2
3 4
5 5
5 5 6 6 5
1 2 5 4 3
5 3
1 2 3 4 5
10 9 10 10 9
4 0
2 2 4 3
2 4 2 3
4 4
1 2 2 1
4 4 5 4
output
6
27
39
11
17
题意:给定数组a和数组b,可以有k次操作,每次可将a中的一个元素换成b中的一个元素,求操作完成后a数组元素总和的最大值。
思路:用一个结构体数组存储两个数组元素,并标记属于a数组或者是b数组,从大到小排序,从头遍历取数即可。
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1)