Skip to content

Commit fcf5e2d

Browse files
committed
🚀 07-May-2021
1 parent f348dcf commit fcf5e2d

10 files changed

+778
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
A penguin Rocher has n sticks. He has exactly one stick with length i for all 1=i=n.
3+
4+
He can connect some sticks. If he connects two sticks that have lengths a and b, he gets one stick with length a+b. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections.
5+
6+
He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create?
7+
8+
Input
9+
The input consists of multiple test cases. The first line contains a single integer t (1=t=1000) — the number of test cases. Next t lines contain descriptions of test cases.
10+
11+
For each test case, the only line contains a single integer n (1=n=109).
12+
13+
Output
14+
For each test case, print a single integer — the answer to the problem.
15+
16+
Example
17+
inputCopy
18+
4
19+
1
20+
2
21+
3
22+
4
23+
outputCopy
24+
1
25+
1
26+
2
27+
2
28+
Note
29+
In the third case, he can connect two sticks with lengths 1 and 2 and he will get one stick with length 3. So, he will have two sticks with lengths 3.
30+
31+
In the fourth case, he can connect two sticks with lengths 1 and 3 and he will get one stick with length 4. After that, he will have three sticks with lengths {2,4,4}, so two sticks have the same length, and one stick has the other length.
32+
*/
33+
34+
35+
#include<bits/stdc++.h>
36+
using namespace std;
37+
38+
int main(){
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
42+
int t;
43+
cin >> t;
44+
45+
while (t--) {
46+
int n;
47+
cin >> n;
48+
49+
if (n % 2) cout << (long long)ceil(n / 2.0) << endl;
50+
else cout << (long long)n / 2 << endl;
51+
}
52+
53+
return 0;
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
A permutation of length n is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4,3,5,1,2], [3,2,1] are permutations, and [1,1], [0,1], [2,2,1,4] are not.
3+
4+
There was a permutation p[1…n]. It was merged with itself. In other words, let's take two instances of p and insert elements of the second p into the first maintaining relative order of elements. The result is a sequence of the length 2n.
5+
6+
For example, if p=[3,1,2] some possible results are: [3,1,2,3,1,2], [3,3,1,1,2,2], [3,1,3,1,2,2]. The following sequences are not possible results of a merging: [1,3,2,1,2,3], [3,1,2,3,2,1], [3,3,1,2,2,1].
7+
8+
For example, if p=[2,1] the possible results are: [2,2,1,1], [2,1,2,1]. The following sequences are not possible results of a merging: [1,1,2,2], [2,1,1,2], [1,2,2,1].
9+
10+
Your task is to restore the permutation p by the given resulting sequence a. It is guaranteed that the answer exists and is unique.
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=400) — the number of test cases. Then t test cases follow.
16+
17+
The first line of the test case contains one integer n (1=n=50) — the length of permutation. The second line of the test case contains 2n integers a1,a2,…,a2n (1=ai=n), where ai is the i-th element of a. It is guaranteed that the array a represents the result of merging of some permutation p with the same permutation p.
18+
19+
Output
20+
For each test case, print the answer: n integers p1,p2,…,pn (1=pi=n), representing the initial permutation. It is guaranteed that the answer exists and is unique.
21+
22+
Example
23+
inputCopy
24+
5
25+
2
26+
1 1 2 2
27+
4
28+
1 3 1 4 3 4 2 2
29+
5
30+
1 2 1 2 3 4 3 5 4 5
31+
3
32+
1 2 3 1 2 3
33+
4
34+
2 3 2 4 1 3 4 1
35+
outputCopy
36+
1 2
37+
1 3 4 2
38+
1 2 3 4 5
39+
1 2 3
40+
2 3 4 1
41+
*/
42+
43+
44+
45+
#include<bits/stdc++.h>
46+
using namespace std;
47+
48+
int main(){
49+
ios_base::sync_with_stdio(false);
50+
cin.tie(NULL);
51+
52+
int t;
53+
cin >> t;
54+
55+
while (t--) {
56+
int n;
57+
cin >> n;
58+
59+
unordered_set <int> s;
60+
61+
for (int i = 0; i < 2 * n; i++) {
62+
int a;
63+
cin >> a;
64+
65+
if (s.find(a) == s.end()) {
66+
s.insert(a);
67+
cout << a << " ";
68+
}
69+
}
70+
71+
cout << endl;
72+
}
73+
74+
return 0;
75+
}
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Phoenix has collected n pieces of gold, and he wants to weigh them together so he can feel rich. The i-th piece of gold has weight wi. All weights are distinct. He will put his n pieces of gold on a weight scale, one piece at a time.
3+
4+
The scale has an unusual defect: if the total weight on it is exactly x, it will explode. Can he put all n gold pieces onto the scale in some order, without the scale exploding during the process? If so, help him find some possible order.
5+
6+
Formally, rearrange the array w so that for each i (1=i=n), ?j=1iwj?x.
7+
8+
Input
9+
The input consists of multiple test cases. The first line contains an integer t (1=t=1000) — the number of test cases.
10+
11+
The first line of each test case contains two integers n and x (1=n=100; 1=x=104) — the number of gold pieces that Phoenix has and the weight to avoid, respectively.
12+
13+
The second line of each test case contains n space-separated integers (1=wi=100) — the weights of the gold pieces. It is guaranteed that the weights are pairwise distinct.
14+
15+
Output
16+
For each test case, if Phoenix cannot place all n pieces without the scale exploding, print NO. Otherwise, print YES followed by the rearranged array w. If there are multiple solutions, print any.
17+
18+
Example
19+
inputCopy
20+
3
21+
3 2
22+
3 2 1
23+
5 3
24+
1 2 3 4 8
25+
1 5
26+
5
27+
outputCopy
28+
YES
29+
3 2 1
30+
YES
31+
8 1 2 3 4
32+
NO
33+
Note
34+
In the first test case, Phoenix puts the gold piece with weight 3 on the scale first, then the piece with weight 2, and finally the piece with weight 1. The total weight on the scale is 3, then 5, then 6. The scale does not explode because the total weight on the scale is never 2.
35+
36+
In the second test case, the total weight on the scale is 8, 9, 11, 14, then 18. It is never 3.
37+
38+
In the third test case, Phoenix must put the gold piece with weight 5 on the scale, and the scale will always explode
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 n, x;
55+
cin >> n >> x;
56+
57+
queue <int> q;
58+
59+
int sum = 0;
60+
61+
for (int i = 0; i < n; i++) {
62+
int ch;
63+
cin >> ch;
64+
q.push(ch);
65+
sum += ch;
66+
}
67+
68+
if (sum == x) {
69+
cout << "NO" << endl;
70+
} else {
71+
sum = x;
72+
cout << "YES" << endl;
73+
int first = q.front();
74+
q.pop();
75+
76+
if (first == x) {
77+
q.push(first);
78+
} else {
79+
sum -= first;
80+
cout << first << " ";;
81+
}
82+
83+
while (!q.empty()) {
84+
int tmp = q.front();
85+
q.pop();
86+
if (tmp == sum) q.push(tmp);
87+
else {
88+
cout << tmp << " ";
89+
sum -= tmp;
90+
}
91+
}
92+
cout << endl;
93+
}
94+
}
95+
96+
return 0;
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Phoenix is playing with a new puzzle, which consists of n identical puzzle pieces. Each puzzle piece is a right isosceles triangle as shown below.
3+
4+
A puzzle piece
5+
The goal of the puzzle is to create a square using the n pieces. He is allowed to rotate and move the pieces around, but none of them can overlap and all n pieces must be used (of course, the square shouldn't contain any holes as well). Can he do it?
6+
7+
Input
8+
The input consists of multiple test cases. The first line contains an integer t (1=t=104) — the number of test cases.
9+
10+
The first line of each test case contains an integer n (1=n=109) — the number of puzzle pieces.
11+
12+
Output
13+
For each test case, if Phoenix can create a square with the n puzzle pieces, print YES. Otherwise, print NO.
14+
15+
Example
16+
inputCopy
17+
3
18+
2
19+
4
20+
6
21+
outputCopy
22+
YES
23+
YES
24+
NO
25+
*/
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+
int a = sqrt(n / 2);
44+
int b = sqrt(n / 4);
45+
46+
a * a * 2 == n || b * b * 4 == n ? cout << "YES\n" : cout << "NO\n";
47+
48+
}
49+
50+
return 0;
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
You have r red and b blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet:
3+
4+
has at least one red bean (or the number of red beans ri=1);
5+
has at least one blue bean (or the number of blue beans bi=1);
6+
the number of red and blue beans should differ in no more than d (or |ri-bi|=d)
7+
Can you distribute all beans?
8+
9+
Input
10+
The first line contains the single integer t (1=t=1000) — the number of test cases.
11+
12+
The first and only line of each test case contains three integers r, b, and d (1=r,b=109; 0=d=109) — the number of red and blue beans and the maximum absolute difference in each packet.
13+
14+
Output
15+
For each test case, if you can distribute all beans, print YES. Otherwise, print NO.
16+
17+
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
18+
19+
Example
20+
inputCopy
21+
4
22+
1 1 0
23+
2 7 3
24+
6 1 4
25+
5 4 0
26+
outputCopy
27+
YES
28+
YES
29+
NO
30+
NO
31+
Note
32+
In the first test case, you can form one packet with 1 red and 1 blue bean. The absolute difference |1-1|=0=d.
33+
34+
In the second test case, you can form two packets: 1 red and 4 blue beans in the first packet and 1 red and 3 blue beans in the second one.
35+
36+
In the third test case, since b=1, you can form only one packet with 6 red and 1 blue beans. The absolute difference |6-1|=5>d.
37+
38+
In the fourth test case, since d=0 so each packet should contain the same number of red and blue beans, but r?b.
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 r, b, d;
55+
cin >> r >> b >> d;
56+
57+
if (r == b) cout << "YES\n";
58+
else if (r > b) {
59+
int tmp = ceil(float(r) / b);
60+
tmp -= 1;
61+
if (tmp <= d) cout << "YES\n";
62+
else cout << "NO\n";
63+
} else {
64+
int tmp = ceil(float(b) / r);
65+
tmp -= 1;
66+
if (tmp <= d) cout << "YES\n";
67+
else cout << "NO\n";
68+
}
69+
}
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)