Class 09: Contest I
02-03-2020
“Hard work beats talent when talent doesn’t work hard”
You can find the contest here.
The solutions will be uploaded after the contest.
The upsolving contest is here.
A: Fifty-Fifty
Fifty-Fifty
Is you sort the string you dont need to do a lot of checks.
Code
#include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int main () {
ios::sync_with_stdio(false); cin.tie(0);
string s;
cin >> s;
sort(all(s));
if (s[0] == s[1] and s[2] == s[3] and s[1] != s[2]) cout << "Yes\n";
else cout << "No\n";
return (0);
}
B: Handstand 2
Handstand 2
We can fix the first and last digit and generate the number that hold the conditions.
Code
#include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <ll> vll;
int intPower (int a, int b) {
int ret = 1;
while (b--) {
ret *= a;
}
return ret;
}
int main () {
ios::sync_with_stdio(false); cin.tie(0);
int n;
cin >> n;
ll ans = 0;
for (int a = 1; a <= 9; a++) {
for (int b = 1; b <= 9; b++) {
set <int> left, right;
if (a == b and a <= n) {
left.insert(a);
right.insert(a);
}
for (int d = 0; d <= 4; d++) {
int p = intPower(10, d);
for (int center = 0; center < p; center++) {
int num1 = a * 10 * p + 10 * center + b;
int num2 = b * 10 * p + 10 * center + a;
if (num1 <= n) left.insert(num1);
if (num2 <= n) right.insert(num2);
}
}
ans += 1LL * sz(left) * sz(right);
}
}
cout << ans << '\n';
return (0);
}
C: Count Order
Count Order
We can just generate all possible permutations and find the indices of the given permutations.
Code
#include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <ll> vll;
int main () {
ios::sync_with_stdio(false); cin.tie(0);
int n;
cin >> n;
vector <int> p(n);
vector <int> q(n);
for (int& elem: p) cin >> elem;
for (int& elem: q) cin >> elem;
vector <int> perm(n);
iota(all(perm), 1);
int a = -1;
int b = -1;
int index = 1;
do {
if (perm == p) a = index;
if (perm == q) b = index;
index++;
} while (next_permutation(all(perm)));
cout << abs(a - b) << '\n';
return (0);
}
D: Next Alphabet
Next Alphabet
A char is stored as an int that represents its ascii representation and the letters “abc..z” are continuous. We can use that fact to void a lot of ifs.
Code
#include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <ll> vll;
int main () {
ios::sync_with_stdio(false); cin.tie(0);
char ch;
cin >> ch;
cout << char(ch + 1) << '\n';
return (0);
}
E: Equal Sums
Equal Sums
For a sequence \(seq_i = a_1, a_2, \dots, a_n\) let \(sum = a_1 + a_2 + \dots + a_n\).
We can store in a map \((sum - a[j]) \to [(i, j)]\)
Then if there are two sequence that holds the conditions, they must have been added to the map.
Code
#include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
int main () {
ios::sync_with_stdio(false); cin.tie(0);
int k;
cin >> k;
map <int, vpii> mp;
for (int i = 1; i <= k; i++) {
int n;
cin >> n;
vi arr(n);
for (int& elem: arr) cin >> elem;
int sum = accumulate(all(arr), 0);
for (int j = 0; j < n; j++) {
if (mp[sum - arr[j]].empty() or
mp[sum - arr[j]].back().first != i) {
mp[sum - arr[j]].pb(pii(i, j + 1));
}
}
}
for (auto row: mp) {
if (sz(row.second) >= 2) {
auto pp = row.second[0];
auto qq = row.second[1];
cout << "YES\n";
cout << pp.first << ' ' << pp.second << '\n';
cout << qq.first << ' ' << qq.second << '\n';
return (0);
}
}
cout << "NO\n";
return (0);
}
F: RGB Boxes
RGB Boxes
Fix the number of red and green boxes, then you can compute how many blue boxes you need.
Code
#include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define sz(A) int(A.size())
using namespace std;
typedef long long ll;
int main () {
ios::sync_with_stdio(false); cin.tie(0);
int r, g, b, n;
cin >> r >> g >> b >> n;
int ans = 0;
for (int i = 0; i <= n / r + 1; i++) {
for (int j = 0; j <= n / g + 1; j++) {
int k = n - i * r - j * g;
if (k >= 0 and k % b == 0) {
ans++;
}
}
}
cout << ans << endl;
return (0);
}