Is it rated?
Tutorial is loading...
Code
n = int(input())
results = []
for i in range(n):
results.append(list(map(int, input().split())))
for r in results:
if r[0] != r[1]:
print("rated")
exit()
for i in range(n):
for j in range(i):
if results[i][0] > results[j][0]:
print("unrated")
exit()
print("maybe")
T-Shirt Hunt
Tutorial is loading...
More efficient code
#include <bits/stdc++.h>
using namespace std;
int main() {
int p, x, y;
cin >> p >> x >> y;
for (int s = y; ; s++) {
if (s % 50 != x % 50) {
continue;
}
bool me = false;
int i = s / 50 % 475;
for (int j = 0; j < 25; j++) {
i = (i * 96 + 42) % 475;
if (i + 26 == p) {
me = true;
break;
}
}
if (me) {
printf("%d\n", (max(0, s - x) + 50) / 100);
break;
}
}
return 0;
}
Less efficient code
p, x, y = map(int, input().split())
def check(s):
i = (s // 50) % 475
for t in range(25):
i = (i * 96 + 42) % 475
if 26 + i == p:
return True
return False
for up in range(500):
for down in range(500):
if x + 100 * up - 50 * down >= y and check(x + 100 * up - 50 * down):
print(up)
exit()
assert(False)
Success Rate
Tutorial is loading...
Binary search solution code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool check(ll x, ll y, ll p, ll q, ll t) {
return p * t >= x && q * t - p * t >= y - x;
}
void solve() {
ll x, y, p, q;
scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
ll l = -1;
ll r = (ll) 1e9;
if (!check(x, y, p, q, r)) {
printf("-1\n");
return;
}
while (r - l > 1) {
ll m = (l + r) / 2;
if (check(x, y, p, q, m)) {
r = m;
} else {
l = m;
}
}
printf("%lld\n", r * q - y);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
Formula solution code
#include <bits/stdc++.h>
using namespace std;
int main() {
int tt;
cin >> tt;
while (tt--) {
int x, y, p, q;
cin >> x >> y >> p >> q;
if (p == 0) {
cout << (x == 0 ? 0 : -1) << endl;
continue;
}
if (p == q) {
cout << (x == y ? 0 : -1) << endl;
continue;
}
int t1 = (x + p - 1) / p;
int t2 = ((y - x) + (q - p) - 1) / (q - p);
cout << (q * 1LL * max(t1, t2) - y) << endl;
}
return 0;
}
Dynamic Problem Scoring
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 123456;
const int m = 5;
const int k = 6;
int a[N][m];
int score[N];
int cost[m];
int solved[m];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", a[i] + j);
if (a[i][j] != -1) {
solved[j]++;
}
}
}
for (int bots = 0; bots <= ((1 << k) — 1) * n; bots++) {
for (int j = 0; j < m; j++) {
int total = n + bots;
int cur = solved[j];
if (a[0][j] != -1 && a[1][j] != -1 && a[0][j] > a[1][j]) {
cur += bots;
}
cost[j] = 500;
while (cost[j] < 500 * k && 2 * cur <= total) {
cur *= 2;
cost[j] += 500;
}
}
for (int i = 0; i < 2; i++) {
score[i] = 0;
for (int j = 0; j < m; j++) {
if (a[i][j] != -1) {
score[i] += cost[j] / 250 * (250 - a[i][j]);
}
}
}
if (score[0] > score[1]) {
printf("%d\n", bots);
return 0;
}
}
printf("%d\n", -1);
return 0;
}
Prairie Partition
Tutorial is loading...
Code with the second optimization
n = int(input())
a = sorted(list(map(int, input().split())))
maxe = max(a)
cnt = []
cur = 1
k = 0
i = 0
while i < n:
cnt.append(0)
while i < n and a[i] < cur:
cnt[2 * k] += 1
i += 1
cnt.append(0)
while i < n and a[i] == cur:
cnt[2 * k + 1] += 1
i += 1
k += 1
cur *= 2
cnt.append(0)
cnt.append(0)
maxe = len(cnt) - 1
maxk = cnt[1]
was = False
for l in range(maxk):
cur = 1
while cnt[cur] > 0:
cnt[cur] -= 1
cur += 2
cnt[cur] -= 1
cursum = 0
ok = True
for t in range(maxe, 0, -1):
cursum += cnt[t]
if cursum > 0:
ok = False
break
if ok:
print(l + 1, end=" ")
was = True
if not was:
print(-1)
Code with both optimizations
#include <bits/stdc++.h>
using namespace std;
const int MAX = 66;
int power[MAX + 10];
int between[MAX + 10];
int extra[MAX + 10];
int ende[MAX + 10];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long foo;
cin >> foo;
int cc = 0;
bool good = true;
while (foo > 1) {
if (foo & 1) {
good = false;
}
cc++;
foo >>= 1;
}
if (good) {
power[cc]++;
} else {
between[cc]++;
}
}
int low = 1, high = power[0] + 1;
while (low < high) {
int mid = (low + high) >> 1;
int cur = mid;
for (int i = 0; i < MAX; i++) {
extra[i] = power[i] - cur;
int new_cur = min(cur, power[i + 1]);
ende[i] = cur - new_cur;
cur = new_cur;
}
bool fail = false;
int open = 0;
for (int i = MAX - 1; i >= 0; i--) {
open += ende[i];
open -= between[i];
open -= extra[i + 1];
if (open < 0) {
fail = true;
break;
}
}
open -= extra[0];
if (open < 0) {
fail = true;
}
if (fail) {
low = mid + 1;
} else {
high = mid;
}
}
if (low > power[0]) {
cout << -1 << endl;
return 0;
}
for (int i = low; i <= power[0]; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}