Skip to content

Commit 4ab547f

Browse files
committed
CodeForces and FenwickTree solutions.
1 parent e51dcea commit 4ab547f

12 files changed

+680
-0
lines changed

Diff for: Binary Search/CodeForces - C_Poisoned_Dagger.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
4+
using namespace std;
5+
6+
// [start,end)
7+
ll binary_search(function<bool(ll)> func, ll start, ll end) {
8+
/* func:int ->bool
9+
returns smallest int x where func(x) evaluates to true, searches in [start,end), it is assumed the values are false, .. , false, true ...
10+
*/
11+
12+
if (end <= start) return end; // has to be here, otherwise func(end-1) in next line could be a problem
13+
if (!func(end-1)) return end;
14+
while (end - start > 1) {
15+
ll mid = (start + end) / 2;
16+
if (func(mid)) end = mid; else start = mid;
17+
}
18+
19+
return (func(start) ? start : end);
20+
};
21+
22+
void testCase() {
23+
ll n, h;
24+
cin >> n >> h;
25+
26+
vector<ll> a(n);
27+
for (int i = 0; i < n; i++) {
28+
cin >> a[i];
29+
}
30+
31+
a.push_back((ll)2e18);
32+
n++;
33+
34+
ll k = binary_search([&](ll K) {
35+
ll damage = h;
36+
for (int i = 1; i < n; i++) {
37+
damage = damage - min(K, a[i] - a[i-1]);
38+
if (damage <= 0) break;
39+
}
40+
41+
return (damage <= 0);
42+
}, 0LL, h * 2);
43+
44+
ll damage = h;
45+
for (int i = 1; i < n; i++) {
46+
damage = damage - min(k-1, a[i] - a[i-1]);
47+
if (damage <= 0) break;
48+
}
49+
50+
if (damage <= 0) {
51+
cout << k - 1 << endl;
52+
} else {
53+
cout << k << endl;
54+
}
55+
}
56+
57+
int main() {
58+
int t;
59+
cin >> t;
60+
61+
while (t--) {
62+
testCase();
63+
}
64+
65+
return 0;
66+
}

Diff for: Bit Operations/D_-_AND_and_SUM.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
ll A, S;
9+
cin >> A >> S;
10+
11+
bool ok = true;
12+
bool carry = false;
13+
for (int i = 0; i < 61 && ok; i++) {
14+
if ((1LL << i) > A && (1LL << i) > S) break;
15+
if (A & (1LL << i)) {
16+
if (S & (1LL << i)) {
17+
if (!carry) ok = false;
18+
} else {
19+
if (carry) ok = false;
20+
carry = true;
21+
}
22+
} else {
23+
if (S & (1LL << i)) {
24+
if (carry) {
25+
carry = false;
26+
}
27+
}
28+
}
29+
}
30+
31+
cout << (ok && !carry ? "Yes" : "No") << endl;
32+
}
33+
34+
int main() {
35+
int t;
36+
cin >> t;
37+
38+
while (t--) {
39+
testCase();
40+
}
41+
42+
return 0;
43+
}

Diff for: CodeForces/C_Column_Swapping.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int n, m;
9+
cin >> n >> m;
10+
11+
vector<vector<int>> a(n, vector<int>(m, 0));
12+
vector<vector<int>> s(n, vector<int>(m, 0));
13+
for (int i = 0; i < n; i++) {
14+
for (int j = 0; j < m; j++) {
15+
cin >> a[i][j];
16+
s[i][j] = a[i][j];
17+
}
18+
}
19+
20+
for (int i = 0; i < n; i++) {
21+
sort(s[i].begin(), s[i].end());
22+
}
23+
24+
map<int, bool> col_needed;
25+
bool ok = true;
26+
for (int i = 0; i < n && ok; i++) {
27+
int cols = 0;
28+
for (int j = 0; j < m && ok; j++) {
29+
if (a[i][j] != s[i][j]) {
30+
cols++;
31+
col_needed[j] = true;
32+
}
33+
34+
if (cols > 2) {
35+
ok = false; break;
36+
}
37+
}
38+
}
39+
40+
if (!ok || col_needed.size() > 2) {
41+
cout << -1 << endl;
42+
return;
43+
}
44+
45+
if (col_needed.size() == 0) {
46+
cout << 1 << " " << 1 << endl;
47+
return;
48+
}
49+
50+
vector<int> sw;
51+
for (auto it: col_needed) {
52+
sw.push_back(it.first);
53+
}
54+
55+
for (int i = 0; i < n; i++) {
56+
swap(a[i][sw[0]], a[i][sw[1]]);
57+
ok &= is_sorted(a[i].begin(), a[i].end());
58+
}
59+
60+
if (!ok) {
61+
cout << -1 << endl;
62+
return;
63+
}
64+
65+
cout << sw[0] + 1 << " " << sw[1] + 1 << endl;
66+
}
67+
68+
int main() {
69+
int t;
70+
cin >> t;
71+
72+
while (t--) {
73+
testCase();
74+
}
75+
76+
return 0;
77+
}

Diff for: CodeForces/C_Double_Sort.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int n;
9+
cin >> n;
10+
11+
vector<int> a(n), b(n);
12+
for (int i = 0; i < n; i++) cin >> a[i];
13+
for (int i = 0; i < n; i++) cin >> b[i];
14+
15+
bool ok = true;
16+
vector<pair<int, int>> swaps;
17+
swaps.reserve(n * n);
18+
for (int i = 0; i < n; i++) {
19+
int min_id = i;
20+
for (int j = i + 1; j < n; j++) {
21+
if (a[j] <= a[min_id] && b[j] <= b[min_id]) {
22+
min_id = j;
23+
}
24+
}
25+
26+
if (i == min_id) continue;
27+
swap(a[i], a[min_id]);
28+
swap(b[i], b[min_id]);
29+
swaps.push_back({ i, min_id });
30+
}
31+
32+
ok &= is_sorted(a.begin(), a.end());
33+
ok &= is_sorted(b.begin(), b.end());
34+
if (!ok) {
35+
cout << -1 << endl;
36+
} else {
37+
if (swaps.empty()) {
38+
cout << 0 << endl;
39+
return;
40+
}
41+
42+
cout << swaps.size() << endl;
43+
for (int i = 0; i < swaps.size(); i++) {
44+
cout << swaps[i].first + 1 << " " << swaps[i].second + 1 << endl;
45+
}
46+
}
47+
}
48+
49+
int main() {
50+
int t;
51+
cin >> t;
52+
53+
while (t--) {
54+
testCase();
55+
}
56+
57+
cout << flush;
58+
return 0;
59+
}

Diff for: CodeForces/C_Infinite_Replacement.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
string s, t;
9+
cin >> s >> t;
10+
11+
string tmp = t;
12+
sort(tmp.begin(), tmp.end());
13+
if (tmp.front() == 'a' && t.size() >= 2) {
14+
cout << -1 << endl;
15+
return;
16+
}
17+
18+
int count = 0;
19+
for (int i = 0; i < s.size(); i++) {
20+
if (s[i] == 'a') count++;
21+
}
22+
23+
if (s.size() == count) {
24+
if (tmp.front() == 'a') {
25+
cout << 1 << endl;
26+
return;
27+
}
28+
}
29+
30+
cout << (1LL << count) << endl;
31+
}
32+
33+
int main() {
34+
int t;
35+
cin >> t;
36+
37+
while (t--) {
38+
testCase();
39+
}
40+
41+
return 0;
42+
}

Diff for: CodeForces/C_Make_Equal_With_Mod.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int n;
9+
cin >> n;
10+
vector<int> v(n);
11+
bool is_one = false;
12+
bool is_cons = false;
13+
for (int i = 0; i < n; i++) {
14+
cin >> v[i];
15+
if (v[i] == 1) is_one = true;
16+
}
17+
sort(v.begin(), v.end());
18+
for (int i = 1; i < n; i++) {
19+
if (v[i] == v[i-1] + 1) {
20+
is_cons = true;
21+
break;
22+
}
23+
}
24+
if (!is_one) {
25+
cout << "YES" << endl;
26+
} else {
27+
if (is_cons) {
28+
cout << "NO" << endl;
29+
} else {
30+
cout << "YES" << endl;
31+
}
32+
}
33+
}
34+
35+
int main() {
36+
int t;
37+
cin >> t;
38+
39+
while (t--) {
40+
testCase();
41+
}
42+
43+
return 0;
44+
}

Diff for: CodeForces/C_Pōja_Verdon.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
int main() {
8+
int n;
9+
cin >> n;
10+
int sum = 0;
11+
for (int i = 0; i < n; i++) {
12+
int a;
13+
cin >> a;
14+
sum += a;
15+
}
16+
cout << sum << endl;
17+
return 0;
18+
}

Diff for: CodeForces/C_Restoring_the_Duration_of_Tasks.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
#define endl '\n'
4+
5+
using namespace std;
6+
7+
void testCase() {
8+
int n;
9+
cin >> n;
10+
11+
vector<int> arrival(n), finish(n);
12+
for (int i = 0; i < n; i++) cin >> arrival[i];
13+
for (int i = 0; i < n; i++) cin >> finish[i];
14+
15+
int last_arrived = arrival[0];
16+
for (int i = 0; i < n; i++) {
17+
cout << finish[i] - last_arrived << " ";
18+
if (i+1<n) last_arrived = max(finish[i], arrival[i+1]);
19+
}
20+
21+
cout << endl;
22+
}
23+
24+
int main() {
25+
int t;
26+
cin >> t;
27+
28+
while (t--) {
29+
testCase();
30+
}
31+
32+
cout << flush;
33+
return 0;
34+
}

0 commit comments

Comments
 (0)