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;
}
Perishable Roads
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18;
const int N = 2010;
int a[N][N];
long long d[N];
bool used[N];
int main() {
// reading input data
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
a[i][i] = 0;
for (int j = i + 1; j < n; j++) {
scanf("%d", a[i] + j);
a[j][i] = a[i][j];
}
}
// finding minimum edge weight
int min_edge = a[0][1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) {
min_edge = min(min_edge, a[i][j]);
}
}
}
// subtracting minimum edge weight from all edge weights
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) {
a[i][j] -= min_edge;
}
}
}
// initializing distance array
for (int i = 0; i < n; i++) {
d[i] = inf;
for (int j = 0; j < n; j++) {
if (i != j) {
d[i] = min(d[i], 2LL * a[i][j]);
}
}
used[i] = false;
}
// running Dijkstra
for (int it = 0; it < n; it++) {
long long min_d = inf;
int ver = -1;
for (int i = 0; i < n; i++) {
if (!used[i] && d[i] < min_d) {
min_d = d[i];
ver = i;
}
}
used[ver] = true;
for (int i = 0; i < n; i++) {
d[i] = min(d[i], d[ver] + a[ver][i]);
}
}
// printing result
for (int i = 0; i < n; i++) {
cout << (d[i] + (long long) min_edge * (n - 1)) << endl;
}
return 0;
}
Blog Post Rating
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
inline int signum(int x) {
return (x < 0 ? -1 : (x > 0 ? 1 : 0));
}
const int inf = (int) 1e9;
const int N = 500010;
pair <int, int> a[N];
int pos[N];
pair <int, int> mn[4 * N];
int add[4 * N];
void push(int x) {
add[x + x] += add[x];
mn[x + x].first += add[x];
add[x + x + 1] += add[x];
mn[x + x + 1].first += add[x];
add[x] = 0;
}
void pull(int x) {
mn[x] = min(mn[x + x], mn[x + x + 1]);
}
void build(int x, int l, int r) {
add[x] = 0;
if (l == r) {
mn[x] = make_pair(a[l].first, l);
return;
}
int y = (l + r) >> 1;
build(x + x, l, y);
build(x + x + 1, y + 1, r);
pull(x);
}
void modify(int x, int l, int r, int ll, int rr, int v) {
if (ll <= l && r <= rr) {
add[x] -= v;
mn[x].first -= v;
return;
}
push(x);
int y = (l + r) >> 1;
if (ll <= y) {
modify(x + x, l, y, ll, rr, v);
}
if (rr > y) {
modify(x + x + 1, y + 1, r, ll, rr, v);
}
pull(x);
}
pair <int, int> find_min(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return mn[x];
}
push(x);
int y = (l + r) >> 1;
pair <int, int> res = make_pair(inf, -1);
if (ll <= y) {
res = min(res, find_min(x + x, l, y, ll, rr));
}
if (rr > y) {
res = min(res, find_min(x + x + 1, y + 1, r, ll, rr));
}
pull(x);
return res;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].first);
a[i].second = i;
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
pos[a[i].second] = i;
}
build(1, 0, n - 1);
set <int> minus, zero, plus;
for (int id = 0; id < n; id++) {
int i = pos[id];
auto p = find_min(1, 0, n - 1, i, i);
int vote = signum(p.first);
if (vote == 0) {
zero.insert(i);
} else {
if (vote == -1) {
minus.insert(i);
modify(1, 0, n - 1, i, n - 1, -1);
int last_minus = *(--minus.end());
if (a[last_minus].first == 1 - (int) minus.size()) {
minus.erase(last_minus);
modify(1, 0, n - 1, last_minus, n - 1, 1);
zero.insert(last_minus);
} else {
if (!zero.empty()) {
int first_zero = *(zero.begin());
zero.erase(first_zero);
plus.insert(first_zero);
modify(1, 0, n - 1, first_zero, n - 1, 1);
}
}
} else {
plus.insert(i);
modify(1, 0, n - 1, i, n - 1, 1);
auto q = find_min(1, 0, n - 1, i, n - 1);
if (q.first == -1) {
int pos = q.second;
plus.erase(pos);
modify(1, 0, n - 1, pos, n - 1, -1);
zero.insert(pos);
}
}
}
printf("%d\n", (int) plus.size() - (int) minus.size());
}
cerr << "time = " << clock() << " ms" << endl;
return 0;
}
Test Data Generation
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;
template<typename T>
using pair2 = pair<T, T>;
#ifdef WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
const long double pi = acos((ld)-1.0);
const int maxn = 1 << 16;
typedef complex<double> comp;
comp oddc[maxn], evenc[maxn], tmp[maxn];
comp w[maxn], invw[maxn];
ll odd[maxn], even[maxn];
int oldodd[maxn], oldeven[maxn];
int btr[maxn];
int n, A, mod;
int st2;
int answer;
inline int bitrev(int x)
{
return btr[x];
}
void fft(comp *a, int n, comp *w, comp *res)
{
for (int i = 0; i < n; i++) res[bitrev(i)] = a[i];
for (int len = 2; len <= n; len *= 2)
{
int step = n / len;
for (int i = 0; i < n; i += len)
{
int curw = 0;
for (int j = 0; j < len / 2; j++)
{
comp x = res[i + j];
comp y = res[i + len / 2 + j] * w[curw];
res[i + j] = x + y;
res[i + len / 2 + j] = x - y;
curw += step;
}
}
}
}
void preparefft(int n)
{
for (int i = 0; i < n; i++)
{
int cur = i;
for (int j = 0; (1 << j) < n; j++)
{
btr[i] = (btr[i] << 1) | (cur & 1);
cur >>= 1;
}
}
for (int i = 0; i < n; i++)
{
w[i] = comp(cos(2 * pi / n * i), sin(2 * pi / n * i));
invw[i] = w[i];
}
reverse(invw + 1, invw + n);
}
void conv(ll *from, comp *to)
{
for (int i = n + 1; i < st2; i++) tmp[i] = 0;
for (int i = 0; i <= n; i++) tmp[i] = from[i];
fft(tmp, st2, w, to);
}
void convback(comp *from, ll *to)
{
fft(from, st2, invw, tmp);
for (int i = 0; i < st2; i++) tmp[i] /= st2;
for (int i = 0; i <= n; i++)
{
to[i] = (ll)(tmp[i].real() + 0.5);
assert(abs(tmp[i].imag()) < 0.1);
}
}
void addans()
{
for (int i = 1; i <= n; i += 2) answer = (answer + odd[i]) % mod;
}
void go(int A)
{
if (A == 1)
{
even[0] = 1;
odd[0] = 0;
odd[1] = 1;
addans();
return;
}
go(A / 2);
for (int i = 0; i <= n; i++) oldodd[i] = odd[i];
for (int i = 0; i <= n; i++) oldeven[i] = even[i];
conv(even, evenc);
conv(odd, oddc);
for (int i = 0; i < st2; i++)
{
if ((A / 2) % 2 == 0)
{
tie(oddc[i], evenc[i]) = make_pair((oddc[i] + evenc[i]) * oddc[i], (oddc[i] + evenc[i]) * (evenc[i] - (comp)1));
} else
{
tie(evenc[i], oddc[i]) = make_pair((oddc[i] + evenc[i]) * oddc[i], (oddc[i] + evenc[i]) * (evenc[i] - (comp)1));
}
}
convback(oddc, odd);
convback(evenc, even);
for (int i = n + 1; i <= 2 * n; i++)
{
odd[i] = 0;
even[i] = 0;
}
for (int i = 0; i <= n; i++)
{
odd[i] = (odd[i] + oldodd[i]) % mod;
even[i] = (even[i] + oldeven[i]) % mod;
}
if (A % 2 == 1)
{
for (int i = n; i >= 1; i--)
{
odd[i] = (odd[i] + odd[i - 1] + even[i - 1]) % mod;
}
}
addans();
}
int main()
{
cin >> n >> A >> mod;
if (A == 1)
{
cout << 0 << endl;
return 0;
}
st2 = 0;
while ((1 << st2) <= n) st2++;
st2++;
st2 = 1 << st2;
preparefft(st2);
go(A / 2);
cout << answer << endl;
return 0;
}