채린씨의 티스토리
[HackerEarth] Easy - A good array(JavaScript) 본문
Problem
You are given an array A which consists of N elements. Each element of array is either zero or one. Your task is to convert the given array into a good array with a minimum cost.
Good array: In a good array, select any subarray of even length M and the sum of elements in the subarray will be M/2.
In order to transform an array to good array, you can perform the following two operations as many times as you want:
- Choose any index 1≤i≤N and if it is currently 0, then convert it to 1 for X coins.
- Choose any index 1≤i≤N and if it is currently 1, then convert it to 0 for Y coins.
Your task is to minimize the cost of transformation of an array to good array.
Input format
- The first line contains T denoting the number of test cases.
- The first line of each test case contains three space-separated integers N, X, and Y.
- The second line of each test case consists of N space-separated integers of array A.
Output format
For each test case, print one line denoting the minimum cost to transform array into a good array.
Constraints
1≤N≤$10^4$
0≤$A_{i}$≤1
1≤X,Y≤$10^9$
Sample Input/Output
Input | Output |
2 2 1 1 1 1 2 1 1 0 1 |
1 0 |
Explanation
First test case: Array is [1,1], there is only one subarray which is of even length (2), i.e. complete subarray [1,1], it is not good array because for a subarray of size M i.e 2 in our case, sum must be M/2 i.e 1. But sum is 2 for this subarray, so this does not follow property of good subarray.
So here we need convert array to either [1,0] or [0,1], cost is 1 in either case.
Second test case: Array is [0,1], there is only one subarray which is of even length (2), i.e. complete subarray [0,1], it is good array because for a subarray of size M i.e 2 in our case, sum must be M/2 i.e 1. Here sum is 2 for this subarray, so this does follow property of good subarray.
So we don't need to perform any operation.
My Code
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input; // Reading input from STDIN
});
process.stdin.on("end", function () {
main(stdin_input);
});
function main(input) {
let data = input.split("\n");
let input_cnt = 0;
let t = data[input_cnt++];
while (t--) {
let [n, x, y] = data[input_cnt++].split(" ").map((item) => parseInt(item));
let a = data[input_cnt++].split(" ");
let ans1 = 0;
for (let i = 0; i < a.length; i++) {
if (i % 2 === 1 && a[i] === "0") ans1 += x;
if (i % 2 === 0 && a[i] === "1") ans1 += y;
}
let ans2 = 0;
for (let i = 0; i < a.length; i++) {
if (i % 2 === 1 && a[i] === "1") ans2 += y;
if (i % 2 === 0 && a[i] === "0") ans2 += x;
}
console.log(Math.min(ans1, ans2));
}
}
'코딩테스트 대비' 카테고리의 다른 글
[Programmers] Level1 - 최대공약수와 최소공배수(JavaScript) (0) | 2022.04.07 |
---|---|
[HackerEarth] Easy - Empty arrays(JavaScript) (0) | 2022.04.06 |
[HackerEarth] Easy - Zoo creatures(JavaScript) (0) | 2022.04.06 |
[Programmers] Level1 - 두 정수 사이의 합(JavaScript) (0) | 2022.04.01 |
[Programmers] Level1 - 수박수박수박수박수박수?(JavaScript) (0) | 2022.04.01 |