Skip to content

Commit 58ef2ec

Browse files
add gym solutions
1 parent 94d7c85 commit 58ef2ec

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
#define debug(x) cout << #x << " = "; cout << x << '\n';
3+
using namespace std;
4+
typedef long long ll;
5+
const int ARRAY_SIZE = (int) 2e6+3;
6+
7+
void idea() {
8+
int n;
9+
cin >> n;
10+
vector <int> a(n);
11+
int ans = 0;
12+
for(int &in : a) {
13+
cin >> in;
14+
if(in == 0){
15+
ans--;
16+
}
17+
else {
18+
ans++;
19+
}
20+
}
21+
if(n >= 3){
22+
if(a[0] == 1 and a[1] == 1 and a[2] == 1){
23+
++ans;
24+
}
25+
for(int i = 3; i < n; i++) {
26+
if(a[i - 2] == 1 and a[i - 1] == 1 and a[i] == 1) {
27+
++ans;
28+
}
29+
}
30+
}
31+
cout << ans << '\n';
32+
}
33+
34+
int main() {
35+
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
36+
int T = 1;
37+
// cin >> T;
38+
for(int C = 1; C <= T; C++) {
39+
// cout << "Case " << C << ": " << '\n';
40+
idea();
41+
}
42+
return 0;
43+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
#define debug(x) cout << #x << " = "; cout << x << '\n';
3+
using namespace std;
4+
typedef long long ll;
5+
const int ARRAY_SIZE = (int) 2e6+3;
6+
7+
void idea() {
8+
int n, x;
9+
cin >> n >> x;
10+
vector<int> ar(n);
11+
12+
for (int i = 0; i < n; i++)
13+
cin >> ar[i];
14+
15+
// Sort the array of beauties
16+
sort(ar.begin(), ar.end());
17+
18+
int l = 0; // leftmost pointer
19+
int r = n - 1; // rightmost pointer
20+
bool f = true; // flag to check if a valid arrangement exists
21+
22+
for (int i = 1; i < n; i++) {
23+
if (!f) break;
24+
if (ar[i] + ar[i - 1] == x) {
25+
if (ar[i - 1] != ar[l]) {
26+
swap(ar[l], ar[i - 1]);
27+
l += 2;
28+
}
29+
else if (ar[i] != ar[r]) {
30+
swap(ar[r], ar[i]);
31+
r -= 2;
32+
}
33+
else {
34+
f = false;
35+
}
36+
}
37+
}
38+
39+
f = true;
40+
// Second check to ensure no two adjacent elements sum to x
41+
for (int i = 1; i < n; i++) {
42+
if (ar[i] + ar[i - 1] == x) {
43+
f = false;
44+
break;
45+
}
46+
}
47+
48+
// Output the result
49+
if (f) {
50+
for (int i = 0; i < n; i++)
51+
cout << ar[i] << " ";
52+
cout << '\n';
53+
} else {
54+
cout << "*" << '\n';
55+
}
56+
}
57+
58+
int main() {
59+
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
60+
int T = 1;
61+
// cin >> T;
62+
for(int C = 1; C <= T; C++) {
63+
// cout << "Case " << C << ": " << '\n';
64+
idea();
65+
}
66+
return 0;
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
#define debug(x) cout << #x << " = "; cout << x << '\n';
3+
using namespace std;
4+
typedef long long ll;
5+
const int ARRAY_SIZE = (int) 2e6+3;
6+
7+
void idea() {
8+
int n, q;
9+
cin >> n >> q;
10+
vector <int> a(n + 1, 0);
11+
for(int i = 1; i <= n; i++){
12+
cin >> a[i];
13+
}
14+
vector <int> alice(n + 1, 0), bob(n + 1, 0), count_one(n + 1, 0);
15+
16+
for(int i = 1; i <= n; i++){
17+
int x = a[i];
18+
if((x & (~(x - 1))) == x){
19+
alice[i] += x;
20+
}
21+
if(x & 1){
22+
bob[i] += x;
23+
}
24+
if(x == 1){
25+
count_one[i] += x;
26+
}
27+
alice[i] += alice[i - 1];
28+
bob[i] += bob[i - 1];
29+
count_one[i] += count_one[i - 1];
30+
}
31+
32+
while(q--){
33+
int L, R;
34+
cin >> L >> R;
35+
36+
ll A = alice[R] - alice[L- 1];
37+
ll B = bob[R] - bob[L - 1];
38+
ll one = count_one[R] - count_one[L - 1];
39+
40+
A -= one;
41+
B -= one;
42+
43+
if(one & 1ll){
44+
A += (one + 1) / 2;
45+
B += (one / 2);
46+
}
47+
else{
48+
A += (one / 2);
49+
B += (one / 2);
50+
}
51+
52+
if(A > B) cout << 'A' << '\n';
53+
else if(B > A) cout << 'B' << '\n';
54+
else cout << 'E' << '\n';
55+
}
56+
}
57+
58+
int main() {
59+
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
60+
int T = 1;
61+
// cin >> T;
62+
for(int C = 1; C <= T; C++) {
63+
// cout << "Case " << C << ": " << '\n';
64+
idea();
65+
}
66+
return 0;
67+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
#define debug(x) cout << #x << " = "; cout << x << '\n';
3+
using namespace std;
4+
typedef long long ll;
5+
const int ARRAY_SIZE = (int) 2e6+3;
6+
7+
void idea() {
8+
ll n;
9+
cin >> n;
10+
vector <ll> a(n);
11+
for(ll &in : a) cin >> in;
12+
if(n == 1){
13+
cout << 0 << '\n';
14+
return;
15+
}
16+
17+
sort(a.rbegin(), a.rend());
18+
19+
ll x = a[0], y, z;
20+
ll profit = 0;
21+
for(ll ii = 0; ii < n; ii++){
22+
y = a[ii];
23+
for(ll jj = 0; jj < n; jj++) {
24+
z = a[jj];
25+
ll sell = x * x + y * y + z * z;
26+
ll buy = x * y + y * x + z * x;
27+
profit = max(profit, sell - buy);
28+
}
29+
}
30+
cout << profit << '\n';
31+
}
32+
33+
int main() {
34+
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
35+
int T = 1;
36+
// cin >> T;
37+
for(int C = 1; C <= T; C++) {
38+
// cout << "Case " << C << ": " << '\n';
39+
idea();
40+
}
41+
return 0;
42+
}

0 commit comments

Comments
 (0)