Skip to content

Commit f348dcf

Browse files
committed
🚀 05-Apr-2021
1 parent c36a5fe commit f348dcf

18 files changed

+1306
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
You are given a positive integer n, it is guaranteed that n is even (i.e. divisible by 2).
3+
4+
You want to construct the array a of length n such that:
5+
6+
The first n2 elements of a are even (divisible by 2);
7+
the second n2 elements of a are odd (not divisible by 2);
8+
all elements of a are distinct and positive;
9+
the sum of the first half equals to the sum of the second half (?i=1n2ai=?i=n2+1nai).
10+
If there are multiple answers, you can print any. It is not guaranteed that the answer exists.
11+
12+
You have to answer t independent test cases.
13+
14+
Input
15+
The first line of the input contains one integer t (1=t=104) — the number of test cases. Then t test cases follow.
16+
17+
The only line of the test case contains one integer n (2=n=2·105) — the length of the array. It is guaranteed that that n is even (i.e. divisible by 2).
18+
19+
It is guaranteed that the sum of n over all test cases does not exceed 2·105 (?n=2·105).
20+
21+
Output
22+
For each test case, print the answer — "NO" (without quotes), if there is no suitable answer for the given test case or "YES" in the first line and any suitable array a1,a2,…,an (1=ai=109) satisfying conditions from the problem statement on the second line.
23+
24+
Example
25+
inputCopy
26+
5
27+
2
28+
4
29+
6
30+
8
31+
10
32+
outputCopy
33+
NO
34+
YES
35+
2 4 1 5
36+
NO
37+
YES
38+
2 4 6 8 1 3 5 11
39+
NO
40+
*/
41+
42+
#include<bits/stdc++.h>
43+
using namespace std;
44+
45+
int main(){
46+
ios_base::sync_with_stdio(false);
47+
cin.tie(NULL);
48+
49+
int t;
50+
cin >> t;
51+
52+
while (t--) {
53+
int n;
54+
cin >> n;
55+
56+
if ((n / 2) % 2) {
57+
cout << "NO\n";
58+
} else {
59+
cout << "YES\n";
60+
int sum = 0;
61+
for (int i = 2; i <= n; i += 2) {
62+
cout << i << " ";
63+
sum += i;
64+
}
65+
for (int i = 1; i < n - 1; i += 2) {
66+
cout << i << " ";
67+
sum -= i;
68+
}
69+
cout << sum << "\n";
70+
71+
}
72+
}
73+
74+
return 0;
75+
}
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Phoenix has n coins with weights 21,22,…,2n. He knows that n is even.
3+
4+
He wants to split the coins into two piles such that each pile has exactly n2 coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weights in the second pile. Help Phoenix minimize |a-b|, the absolute value of a-b.
5+
6+
Input
7+
The input consists of multiple test cases. The first line contains an integer t (1=t=100) — the number of test cases.
8+
9+
The first line of each test case contains an integer n (2=n=30; n is even) — the number of coins that Phoenix has.
10+
11+
Output
12+
For each test case, output one integer — the minimum possible difference of weights between the two piles.
13+
14+
Example
15+
inputCopy
16+
2
17+
2
18+
4
19+
outputCopy
20+
2
21+
6
22+
Note
23+
In the first test case, Phoenix has two coins with weights 2 and 4. No matter how he divides the coins, the difference will be 4-2=2.
24+
25+
In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16. It is optimal for Phoenix to place coins with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)-(4+8)=6.
26+
*/
27+
28+
29+
#include<bits/stdc++.h>
30+
using namespace std;
31+
32+
int main(){
33+
ios_base::sync_with_stdio(false);
34+
cin.tie(NULL);
35+
36+
int t;
37+
cin >> t;
38+
39+
while (t--) {
40+
int n;
41+
cin >> n;
42+
43+
long long res = 0;
44+
45+
res += pow(2, n);
46+
47+
for (int i = 1; i <= n / 2 - 1; i++) res += pow(2, i);
48+
49+
for (int i = n / 2; i < n; i++) res -= pow(2, i);
50+
51+
cout << res << endl;
52+
53+
}
54+
55+
56+
return 0;
57+
}
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
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.
3+
4+
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.
5+
6+
You have to answer t independent test cases.
7+
8+
Input
9+
The first line of the input contains one integer t (1=t=104) — the number of test cases. Then t test cases follow.
10+
11+
The only line of the test case contains two integers n and m (1=n,m=109) — the length of the array and its sum correspondingly.
12+
13+
Output
14+
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.
15+
16+
Example
17+
inputCopy
18+
5
19+
1 100
20+
2 2
21+
5 5
22+
2 1000000000
23+
1000000000 1000000000
24+
outputCopy
25+
0
26+
2
27+
10
28+
1000000000
29+
2000000000
30+
Note
31+
In the first test case of the example, the only possible array is [100] and the answer is obviously 0.
32+
33+
In the second test case of the example, one of the possible arrays is [2,0] and the answer is |2-0|=2.
34+
35+
In the third test case of the example, one of the possible arrays is [0,2,0,3,0] and the answer is |0-2|+|2-0|+|0-3|+|3-0|=10.
36+
*/
37+
38+
39+
40+
#include<bits/stdc++.h>
41+
using namespace std;
42+
43+
int main(){
44+
ios_base::sync_with_stdio(false);
45+
cin.tie(NULL);
46+
47+
int t;
48+
cin >> t;
49+
50+
while (t--) {
51+
int n, m;
52+
cin >> n >> m;
53+
54+
if (n == 1) cout << 0 << endl;
55+
else if (n == 2) cout << m << endl;
56+
else cout << 2 * m << endl;
57+
}
58+
59+
return 0;
60+
}
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also given an integer k.
3+
4+
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).
5+
6+
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).
7+
8+
You have to answer t independent test cases.
9+
10+
Input
11+
The first line of the input contains one integer t (1=t=200) — the number of test cases. Then t test cases follow.
12+
13+
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.
14+
15+
Output
16+
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.
17+
18+
Example
19+
inputCopy
20+
5
21+
2 1
22+
1 2
23+
3 4
24+
5 5
25+
5 5 6 6 5
26+
1 2 5 4 3
27+
5 3
28+
1 2 3 4 5
29+
10 9 10 10 9
30+
4 0
31+
2 2 4 3
32+
2 4 2 3
33+
4 4
34+
1 2 2 1
35+
4 4 5 4
36+
outputCopy
37+
6
38+
27
39+
39
40+
11
41+
17
42+
Note
43+
In the first test case of the example, you can swap a1=1 and b2=4, so a=[4,2] and b=[3,1].
44+
45+
In the second test case of the example, you don't need to swap anything.
46+
47+
In the third test case of the example, you can swap a1=1 and b1=10, a3=3 and b3=10 and a2=2 and b4=10, so a=[10,10,10,4,5] and b=[1,9,3,2,9].
48+
49+
In the fourth test case of the example, you cannot swap anything.
50+
51+
In the fifth test case of the example, you can swap arrays a and b, so a=[4,4,5,4] and b=[1,2,2,1].
52+
*/
53+
54+
55+
#include<bits/stdc++.h>
56+
using namespace std;
57+
58+
int main(){
59+
ios_base::sync_with_stdio(false);
60+
cin.tie(NULL);
61+
62+
int t;
63+
cin >> t;
64+
65+
while (t--) {
66+
int n, k;
67+
cin >> n >> k;
68+
69+
vector <int> a(n), b(n);
70+
71+
for (int i = 0; i < n; i++) cin >> a[i];
72+
for (int i = 0; i < n; i++) cin >> b[i];
73+
74+
sort(a.begin(), a.end());
75+
sort(b.begin(), b.end(), greater<int>());
76+
77+
int i = 0;
78+
while (k--) {
79+
if (a[i] > b[i]) break;
80+
swap(a[i], b[i]);
81+
i++;
82+
}
83+
84+
int sum = 0;
85+
86+
for (auto &x: a) sum += x;
87+
88+
cout << sum << endl;
89+
}
90+
91+
return 0;
92+
}
93+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Find the minimum area of a square land on which you can place two identical rectangular a×b houses. The sides of the houses should be parallel to the sides of the desired square land.
3+
4+
Formally,
5+
6+
You are given two identical rectangles with side lengths a and b (1=a,b=100) — positive integers (you are given just the sizes, but not their positions).
7+
Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square.
8+
Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding.
9+
10+
The picture shows a square that contains red and green rectangles.
11+
Input
12+
The first line contains an integer t (1=t=10000) —the number of test cases in the input. Then t test cases follow.
13+
14+
Each test case is a line containing two integers a, b (1=a,b=100) — side lengths of the rectangles.
15+
16+
Output
17+
Print t answers to the test cases. Each answer must be a single integer — minimal area of square land, that contains two rectangles with dimensions a×b.
18+
19+
Example
20+
inputCopy
21+
8
22+
3 2
23+
4 2
24+
1 1
25+
3 1
26+
4 7
27+
1 3
28+
7 4
29+
100 100
30+
outputCopy
31+
16
32+
16
33+
4
34+
9
35+
64
36+
9
37+
64
38+
40000
39+
*/
40+
41+
42+
43+
#include<bits/stdc++.h>
44+
using namespace std;
45+
46+
int main(){
47+
ios_base::sync_with_stdio(false);
48+
cin.tie(NULL);
49+
50+
int t;
51+
cin >> t;
52+
53+
while (t--) {
54+
int a, b;
55+
cin >> a >> b;
56+
int res = min(max(2 * a, b), max(2 * b, a));
57+
cout << res * res << endl;
58+
}
59+
60+
return 0;
61+
}
62+

0 commit comments

Comments
 (0)