Skip to content

Commit 3cf0ef4

Browse files
committed
🚀 16-Aug-2021
1 parent 1da2a5f commit 3cf0ef4

21 files changed

+1802
-49
lines changed

Diff for: competitive programming/codechef/COPR16G.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
The easiest problem of the lot !
3+
4+
You are provided with coins of denominations "a" and "b". You are required to find the least value of n, such that all currency values greater than or equal to n can be made using any number of coins of denomination "a" and "b" and in any order. If no such number exists, print "-1" in that case.
5+
6+
Input Constraints:
7+
1 <= t <= 106
8+
1 <= a <= 109
9+
1 <= b <= 109
10+
Input Format:
11+
The first line contains t, the number of test cases.
12+
13+
Each of the next t lines contains 2 integers a & b, as specified in the question.
14+
15+
Output Format:
16+
For each test case, print the required answer
17+
18+
Example
19+
Input:
20+
1
21+
4 7
22+
6 1
23+
2 4
24+
25+
Output:
26+
18
27+
0
28+
-1
29+
NOTE:Prefer fast input/output methods
30+
*/
31+
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+
long long a, b;
47+
cin >> a >> b;
48+
49+
int g = __gcd(a, b);
50+
51+
if (g > 1) cout << -1 << "\n";
52+
else cout << a * b - a - b + 1 << "\n"; // using chicken mcnugget theorem
53+
}
54+
55+
return 0;
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
You have a given integer n. Find the number of ways to fill all 3×n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.
3+
4+
This picture describes when n=4. The left one is the shape and the right one is 3×n tiles.
5+
Input
6+
The only line contains one integer n (1=n=60) — the length.
7+
8+
Output
9+
Print the number of ways to fill.
10+
11+
Examples
12+
inputCopy
13+
4
14+
outputCopy
15+
4
16+
inputCopy
17+
1
18+
outputCopy
19+
0
20+
Note
21+
In the first example, there are 4 possible cases of filling.
22+
23+
In the second example, you cannot fill the shapes in 3×1 tiles
24+
*/
25+
26+
27+
28+
29+
30+
31+
32+
#include<bits/stdc++.h>
33+
using namespace std;
34+
35+
void solve() {
36+
int n;
37+
cin >> n;
38+
39+
if (n % 2) cout << 0 << "\n";
40+
else cout << (int)pow(2, n / 2) << "\n";
41+
}
42+
43+
int main(){
44+
ios_base::sync_with_stdio(false);
45+
cin.tie(NULL);
46+
47+
int t = 1;
48+
//cin >> t;
49+
50+
while (t--) solve();
51+
52+
return 0;
53+
}
+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
Alice and Bob are playing a game on a matrix, consisting of 2 rows and m columns. The cell in the i-th row in the j-th column contains ai,j coins in it.
2+
3+
Initially, both Alice and Bob are standing in a cell (1,1). They are going to perform a sequence of moves to reach a cell (2,m).
4+
5+
The possible moves are:
6+
7+
Move right — from some cell (x,y) to (x,y+1);
8+
Move down — from some cell (x,y) to (x+1,y).
9+
First, Alice makes all her moves until she reaches (2,m). She collects the coins in all cells she visit (including the starting cell).
10+
11+
When Alice finishes, Bob starts his journey. He also performs the moves to reach (2,m) and collects the coins in all cells that he visited, but Alice didn't.
12+
13+
The score of the game is the total number of coins Bob collects.
14+
15+
Alice wants to minimize the score. Bob wants to maximize the score. What will the score of the game be if both players play optimally?
16+
17+
Input
18+
The first line contains a single integer t (1=t=104) — the number of testcases.
19+
20+
Then the descriptions of t testcases follow.
21+
22+
The first line of the testcase contains a single integer m (1=m=105) — the number of columns of the matrix.
23+
24+
The i-th of the next 2 lines contain m integers ai,1,ai,2,…,ai,m (1=ai,j=104) — the number of coins in the cell in the i-th row in the j-th column of the matrix.
25+
26+
The sum of m over all testcases doesn't exceed 105.
27+
28+
Output
29+
For each testcase print a single integer — the score of the game if both players play optimally.
30+
31+
Example
32+
inputCopy
33+
3
34+
3
35+
1 3 7
36+
3 5 1
37+
3
38+
1 3 9
39+
3 5 1
40+
1
41+
4
42+
7
43+
outputCopy
44+
7
45+
8
46+
0
47+
Note
48+
The paths for the testcases are shown on the following pictures. Alice's path is depicted in red and Bob's path is depicted in blue.
49+
50+
51+
52+
53+
54+
55+
56+
// o(m) accepted
57+
58+
#include<bits/stdc++.h>
59+
using namespace std;
60+
61+
void solve() {
62+
int m;
63+
cin >> m;
64+
65+
vector <vector<int>> v(2, vector<int> (m));
66+
67+
for (int i = 0; i < 2; i++) {
68+
for (int j = 0; j < m; j++) {
69+
cin >> v[i][j];
70+
}
71+
}
72+
73+
vector <vector<int>> psum(2, vector<int> (m));
74+
75+
psum[0][0] = v[0][0];
76+
psum[1][0] = v[1][0];
77+
78+
for (int i = 0; i < 2; i++) {
79+
for (int j = 1; j < m; j++) {
80+
psum[i][j] = psum[i][j - 1] + v[i][j];
81+
}
82+
}
83+
84+
int res = INT_MAX;
85+
86+
for (int i = 0; i < m; i++) {
87+
// 0th row
88+
int up = psum[0][m - 1] - psum[0][i];
89+
90+
91+
// 1st row
92+
int down = 0;
93+
94+
if (i - 1 >= 0) down = psum[1][i - 1];
95+
96+
res = min(res, max(up, down));
97+
}
98+
99+
cout << res << "\n";
100+
}
101+
102+
int main(){
103+
ios_base::sync_with_stdio(false);
104+
cin.tie(NULL);
105+
106+
int t = 1;
107+
cin >> t;
108+
109+
while (t--) solve();
110+
111+
return 0;
112+
}
113+
114+
115+
116+
117+
118+
119+
120+
// o(m^2) TLE
121+
122+
#include<bits/stdc++.h>
123+
using namespace std;
124+
125+
void solve() {
126+
int m;
127+
cin >> m;
128+
129+
vector <vector<int>> v(2, vector<int> (m));
130+
131+
for (int i = 0; i < 2; i++) {
132+
for (int j = 0; j < m; j++) {
133+
cin >> v[i][j];
134+
}
135+
}
136+
137+
int res = INT_MAX;
138+
139+
for (int i = 0; i < m; i++) {
140+
int up = 0;
141+
142+
// 0th row
143+
for (int j = i + 1; j < m; j++) up += v[0][j];
144+
145+
int down = 0;
146+
147+
// 1st row
148+
for (int j = 0; j < i; j++) down += v[1][j];
149+
150+
res = min(res, max(up, down));
151+
}
152+
153+
cout << res << "\n";
154+
}
155+
156+
int main(){
157+
ios_base::sync_with_stdio(false);
158+
cin.tie(NULL);
159+
160+
int t = 1;
161+
cin >> t;
162+
163+
while (t--) solve();
164+
165+
return 0;
166+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Ezzat has an array of n integers (maybe negative). He wants to split it into two non-empty subsequences a and b, such that every element from the array belongs to exactly one subsequence, and the value of f(a)+f(b) is the maximum possible value, where f(x) is the average of the subsequence x.
3+
4+
A sequence x is a subsequence of a sequence y if x can be obtained from y by deletion of several (possibly, zero or all) elements.
5+
6+
The average of a subsequence is the sum of the numbers of this subsequence divided by the size of the subsequence.
7+
8+
For example, the average of [1,5,6] is (1+5+6)/3=12/3=4, so f([1,5,6])=4.
9+
10+
Input
11+
The first line contains a single integer t (1=t=103)— the number of test cases. Each test case consists of two lines.
12+
13+
The first line contains a single integer n (2=n=105).
14+
15+
The second line contains n integers a1,a2,…,an (-109=ai=109).
16+
17+
It is guaranteed that the sum of n over all test cases does not exceed 3·105.
18+
19+
Output
20+
For each test case, print a single value — the maximum value that Ezzat can achieve.
21+
22+
Your answer is considered correct if its absolute or relative error does not exceed 10-6.
23+
24+
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if |a-b|max(1,|b|)=10-6.
25+
26+
Example
27+
inputCopy
28+
4
29+
3
30+
3 1 2
31+
3
32+
-7 -6 -6
33+
3
34+
2 2 2
35+
4
36+
17 3 5 -3
37+
outputCopy
38+
4.500000000
39+
-12.500000000
40+
4.000000000
41+
18.666666667
42+
Note
43+
In the first test case, the array is [3,1,2]. These are all the possible ways to split this array:
44+
45+
a=[3], b=[1,2], so the value of f(a)+f(b)=3+1.5=4.5.
46+
a=[3,1], b=[2], so the value of f(a)+f(b)=2+2=4.
47+
a=[3,2], b=[1], so the value of f(a)+f(b)=2.5+1=3.5.
48+
Therefore, the maximum possible value 4.5.
49+
In the second test case, the array is [-7,-6,-6]. These are all the possible ways to split this array:
50+
51+
a=[-7], b=[-6,-6], so the value of f(a)+f(b)=(-7)+(-6)=-13.
52+
a=[-7,-6], b=[-6], so the value of f(a)+f(b)=(-6.5)+(-6)=-12.5.
53+
Therefore, the maximum possible value -12.5.
54+
*/
55+
56+
57+
58+
59+
60+
61+
#include<bits/stdc++.h>
62+
using namespace std;
63+
64+
void solve() {
65+
long long n;
66+
cin >> n;
67+
68+
vector <long long> v(n);
69+
70+
for (auto &x: v) cin >> x;
71+
72+
int maxi = *max_element(v.begin(), v.end());
73+
74+
long long sum = 0;
75+
76+
for (auto &x: v) sum += x;
77+
78+
sum -= maxi;
79+
80+
double res = double(maxi) + double(sum) / (n - 1);
81+
82+
cout << setprecision(9) << fixed << res << "\n";
83+
84+
}
85+
86+
int main(){
87+
ios_base::sync_with_stdio(false);
88+
cin.tie(NULL);
89+
90+
int t = 1;
91+
cin >> t;
92+
93+
while (t--) solve();
94+
95+
return 0;
96+
}

0 commit comments

Comments
 (0)