solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
int n, u, v, k, a[2010], t, A, I;
long long int dp[2010], res, mod = 1000000007;
vector<int> to[2010];
bool vis[2010];
long long int DFS(int node) {
if (dp[node] != -1) return dp[node];
long long int ret = 1;
for (int i = 0; i < to[node].size(); i++) {
int T = to[node][i];
if (vis[T] == 0 && a[T] <= A + k && a[T] >= A &&
((a[T] != a[I]) || (a[T] == a[I] && I < T))) {
vis[T] = 1;
ret *= (DFS(T) + 1), ret %= mod;
vis[T] = 0;
}
}
return (dp[node] = ret);
}
int main() {
scanf("%d %d", &k, &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n - 1; i++) {
scanf("%d %d", &u, &v);
to[u].push_back(v), to[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
memset(dp, -1, sizeof dp);
A = a[i], I = i, vis[i] = 1;
res += DFS(i), res %= mod;
vis[i] = 0;
}
printf("%I64d", res);
return 0;
}
| 2,100 | CPP |
#include <bits/stdc++.h>
int faster_in() {
int r = 0, c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') return -faster_in();
for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
;
return r;
}
using namespace std;
const int INF = int(1e9 + 7);
double s;
double a, b, c;
double EPS = 1e-11;
double f(double x, double y, double z) {
return a * log(x) + b * log(y) + c * log(z);
}
double ternary(double x, double &y) {
double lo = 0.0, hi = s - x;
for (int k = 0; k < 200; k++) {
double L, R;
L = (lo * 2.0 + hi) / 3.0;
R = (lo + 2.0 * hi) / 3.0;
double lRes = f(x, L, s - x - L);
double rRes = f(x, R, s - x - R);
if (lRes > rRes)
hi = R;
else
lo = L;
}
y = lo;
return f(x, y, s - x - y);
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
cin >> a >> b >> c;
pair<double, int> v[3];
double res[3];
v[0] = make_pair(a, 0);
v[1] = make_pair(b, 1);
v[2] = make_pair(c, 2);
sort(v, v + 3);
a = v[2].first;
b = v[1].first;
c = v[0].first;
if (a == 0) {
cout << "0 0 0";
return 0;
}
if (b == 0) {
res[v[2].second] = s;
res[v[1].second] = 0;
res[v[0].second] = 0;
cout << fixed << setprecision(7) << res[0] << ' ' << res[1] << ' ' << res[2]
<< '\n';
return 0;
}
double x, y;
double lo = 0.0, hi = s;
for (int k = 0; k < 200; k++) {
double L, R;
L = (lo * 2.0 + hi) / 3.0;
R = (lo + 2.0 * hi) / 3.0;
if (ternary(L, y) > ternary(R, y)) {
hi = R;
ternary(L, y);
} else {
lo = L;
ternary(R, y);
}
}
x = lo;
res[v[2].second] = x;
res[v[1].second] = y;
res[v[0].second] = s - x - y;
cout << fixed << setprecision(11) << res[0] << ' ' << res[1] << ' ' << res[2]
<< '\n';
return 0;
}
| 1,800 | CPP |
n = int(input())
room = set()
cap = 0
for i in range(n):
typ, r = input().split()
if typ == "+":
room.add(r)
else:
if r in room:
room.remove(r)
else:
cap += 1
cap = max(cap, len(room))
print(cap)
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
bool f[105][205][55][2];
char s[105];
int abs(int x) { return x > 0 ? x : -x; }
int main() {
int n, m, i, j, k, l;
scanf("%s%d", s, &m);
n = strlen(s);
int cnt = 0;
for (i = 0; i < n; i++) {
if (s[i] == 'T') cnt++;
}
if (m >= cnt) {
if (m % 2 == cnt % 2)
printf("%d\n", n);
else
printf("%d\n", n - 1);
return 0;
}
f[0][100][0][0] = true;
for (i = 0; i < n; i++) {
for (j = 0; j < 201; j++) {
for (k = 0; k <= m; k++) {
for (l = 0; l < 2; l++) {
if (!f[i][j][k][l]) continue;
if (s[i] == 'F') {
if (!l) {
f[i + 1][j + 1][k][0] = true;
f[i + 1][j][k + 1][1] = true;
} else {
f[i + 1][j - 1][k][1] = true;
f[i + 1][j][k + 1][0] = true;
}
} else {
if (!l) {
f[i + 1][j][k][1] = true;
f[i + 1][j + 1][k + 1][0] = true;
} else {
f[i + 1][j][k][0] = true;
f[i + 1][j - 1][k + 1][1] = true;
}
}
}
}
}
}
int r = 0;
for (i = 0; i < 201; i++) {
for (k = 0; k < 2; k++) {
if (!f[n][i][m][k]) continue;
if (r < abs(i - 100)) r = abs(i - 100);
}
}
printf("%d\n", r);
return 0;
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, h, k, t, d, vnut, x;
char zn;
cin >> n;
k = 0;
long long mas[n];
for (long long i = 0; i < n; i++) {
cin >> mas[i];
}
while (k <= n - 1) {
if (mas[k] > mas[k + 1]) {
vnut = mas[k + 1];
mas[k + 1] = mas[k];
mas[k] = vnut;
cout << k + 1 << " " << k + 2 << endl;
k = 0;
} else
k++;
}
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 + 10, MAXE = 200 * 200 + 10;
vector<pair<int, int> > vec[MAXN];
bool mark[MAXE], mark2[MAXN];
pair<int, int> e[MAXE];
void dfs(int v) {
mark2[v] = true;
for (int i = 0; i < (int)vec[v].size(); i++) {
if (!mark[vec[v][i].second]) {
mark[vec[v][i].second] = true;
e[vec[v][i].second] = make_pair(v, vec[v][i].first);
dfs(vec[v][i].first);
}
}
}
int main() {
int o;
cin >> o;
for (int _ = 0; _ < o; _++) {
int n, m;
cin >> n >> m;
for (int i = 0; i <= n; i++) {
vec[i].clear();
mark2[i] = false;
}
for (int i = 0; i <= n + m; i++) mark[i] = false;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
vec[a].push_back(make_pair(b, i));
vec[b].push_back(make_pair(a, i));
}
int cnt = m;
for (int i = 0; i < n; i++)
if ((int)vec[i].size() % 2 != 0) {
vec[i].push_back(make_pair(n, cnt));
vec[n].push_back(make_pair(i, cnt));
cnt++;
}
for (int i = 0; i < n; i++)
if (!mark2[i]) dfs(i);
int ans = 0;
for (int i = 0; i < n; i++) {
if ((int)vec[i].size() == 0 || vec[i][(int)vec[i].size() - 1].first != n)
ans++;
}
cout << ans << endl;
for (int i = 0; i < m; i++)
cout << e[i].first + 1 << " " << e[i].second + 1 << endl;
}
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000003;
int dp[2002][2002];
string parse(const string& exp) {
string res;
bool first = true;
for (int i = 0; i < (((int)(exp).size())); ++i) {
if (!isdigit(exp[i])) {
if (first && (((exp[i]) == '*') || ((exp[i]) == '/'))) throw 1;
res += i ? "*" : "+";
i++;
}
for (; i < ((int)(exp).size()) && !isdigit(exp[i]); i++) {
if (!(((exp[i]) == '*') || ((exp[i]) == '/')))
res += "+";
else
throw 1;
}
if (i == ((int)(exp).size())) throw 1;
while (i + 1 < ((int)(exp).size()) && isdigit(exp[i + 1])) ++i;
first = false;
}
return res;
}
int main() {
string exp;
cin >> exp;
string simple;
try {
simple = parse(exp);
} catch (...) {
cout << 0 << endl;
return 0;
}
if (simple.empty()) simple = "+";
const int n = count((simple).begin(), (simple).end(), '*') + 1;
for (int i = 0; i < n; i++) {
dp[0][i] = 1;
}
for (int a = 1; a < ((int)(simple).size()); a++) {
for (int b = 0; b < n; b++) {
int& res = dp[a][b];
res = dp[a - 1][b + (simple[a - 1] == '*' ? 1 : 0)];
if (b) res += dp[a][b - 1], res %= mod;
}
}
cout << ((simple[((int)(simple).size()) - 1] == '*')
? dp[((int)(simple).size()) - 1][1]
: dp[((int)(simple).size()) - 1][0])
<< endl;
return 0;
}
| 2,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long c(long long n, long long m) {
if (n < m) return 0;
if (n == 1) return 1;
if (m == 0 || m == n) return 1;
if (m == 1 || m == n - 1) return n;
long long x = 1;
long long t = 1;
for (long long i = 0; i < m; i++) {
x = x * (n - i);
while (x % t == 0 && t <= m) {
x = x / t;
t++;
}
}
return x;
}
long long dgts(long long n) {
long long n1 = n;
long long x = 0;
while (n1 > 0) {
n1 = n1 / 2;
x++;
}
return x;
}
long long cnt(long long n, long long m) {
if (m == 0) return 1;
if (m == 1) return dgts(n);
if (n == 0) return 0;
if (m > dgts(n)) return 0;
long long x = dgts(n) - 1;
long long y = n - pow(2, x);
return c(x, m) + cnt(y, m - 1);
}
long long goodcnt(long long m, long long n) {
if (m == 0) return n + 1;
long long a = dgts(m) - 1;
long long b = dgts(n) - 1;
long long ans = pow(3, a) * pow(2, b - a);
long long x = m - pow(2, a);
long long y = n - pow(2, b);
long long k;
if (a == b) {
for (long long i = 0; i < b + 1; i++) {
k = cnt(x, i);
k = k * pow(2, b - i);
ans = ans + k;
}
for (long long i = 0; i < a + 1; i++) {
k = cnt(y, i);
k = k * pow(2, a - i);
ans = ans + k;
}
return ans;
}
for (long long i = 0; i < b; i++) {
k = cnt(x, i);
k = k * pow(2, b - i - 1);
ans = ans + k;
}
if (m <= y) return ans + goodcnt(m, y);
return ans + goodcnt(y, m);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
long long t, l, r, ans;
cin >> t;
for (long long i = 0; i < t; i++) {
cin >> l >> r;
if (l == 0)
ans = goodcnt(r, r);
else
ans = goodcnt(r, r) - 2 * goodcnt(l - 1, r) + goodcnt(l - 1, l - 1);
cout << ans << endl;
}
return 0;
}
| 2,300 | CPP |
n=int(input())
s=input()
t=0
for i in range(n):
if i!=n-1:
if s[i]==s[i+1]:
t+=1
print(t)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, maxx = 0, a1, a2, pref = 0, ans = 1, t = 0;
scanf("%d", &n);
vector<int> a(n), r(n, 0), x(n, 0);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) {
if (a[i] > maxx) {
r[i] = 1;
maxx = a[i];
}
pref += r[i];
}
a1 = a[0];
a2 = 0;
for (int i = 1; i < n; i++)
if (a[i] > a1) {
a2 = a1;
a1 = a[i];
t = i;
} else if (a[i] > a2) {
a2 = a[i];
if (r[i] == 0) x[t]++;
}
maxx = 0;
for (int i = 0; i < n; i++) {
int t = pref - r[i] + x[i];
if (t > maxx) {
maxx = t;
ans = a[i];
}
if (t == maxx) ans = min(ans, a[i]);
}
printf("%d", ans);
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
const unsigned long long maxa = 1e17;
unsigned long long a[100];
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
memset(a, 0, sizeof(a));
unsigned long long n, k;
cin >> n >> k;
int ok = 1;
for (int i = 0; i < n; i++) {
unsigned long long tmp;
cin >> tmp;
for (int c = log(maxa) / log(k); c >= 0; c--) {
while (tmp >= (unsigned long long)pow(k, (unsigned long long)c))
tmp -= (unsigned long long)pow(k, (unsigned long long)c), a[c]++;
}
}
for (long long i : a)
if (i > 1) {
ok = 0;
break;
}
if (ok)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 1,400 | CPP |
z=input
mod = 10**9 + 7
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
def lcd(xnum1,xnum2):
return (xnum1*xnum2//gcd(xnum1,xnum2))
################################################################################
"""
n=int(z())
for _ in range(int(z())):
x=int(z())
l=list(map(int,z().split()))
n=int(z())
l=sorted(list(map(int,z().split())))[::-1]
a,b=map(int,z().split())
l=set(map(int,z().split()))
led=(6,2,5,5,4,5,6,3,7,6)
vowel={'a':0,'e':0,'i':0,'o':0,'u':0}
color-4=["G", "GB", "YGB", "YGBI", "OYGBI" ,"OYGBIV",'ROYGBIV' ]
"""
###########################---START-CODING---###############################################
"""
3
4
1 7 6 5
5
1 2 3 4 5
2
0 -4
"""
for _ in range(int(z())):
n,k=map(int,z().split())
l=list(map(int,z().split()))
d=[0]*100
for i in range(n):
x=l[i]
c=0
while x>0:
d[c]+=x%k
c+=1
x=x//k
if max(d)>1:
print('No')
else:
print('yEs')
| 1,400 | PYTHON3 |
x = input()
A = input()
setA = A.split()
y = input()
B = input()
setB = B.split()
a = list(map(int, setA))
b = list(map(int, setB))
a.sort(reverse = True)
b.sort(reverse = True)
p = []
p.append(a[0])
p.append(b[0])
print(*p) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int l, r, a;
cin >> l >> r >> a;
while (a > 0) {
if (l < r) {
l++;
a--;
} else {
r++;
a--;
}
}
int ans = min(l, r) * 2;
cout << ans;
return 0;
}
| 800 | CPP |
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
OnLine = lambda A, B, C, x, y: A*x+B*y+C==0
def OffLine(A, B, C, nodes):
res = []
for i in nodes:
if not OnLine(A, B, C, i[0], i[1]): res.append(i)
return res
flag = False
if n<5:
print('YES')
raise SystemExit
for comb in __import__('itertools').combinations([0, 1, 2], 2):
i, j = comb[0], comb[1]
b = OffLine(a[j][1]-a[i][1], a[i][0]-a[j][0], a[j][0]*a[i][1]-a[i][0]*a[j][1], a)
if len(b)<3:
print('YES')
raise SystemExit
if not OffLine(b[j][1]-b[i][1], b[i][0]-b[j][0], b[j][0]*b[i][1]-b[i][0]*b[j][1], b):
print('YES')
raise SystemExit
print('NO')
| 2,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, n, i, min1, min2, max1, max2, a[10000], b[10000];
cin >> n;
t = n;
while (n--) {
cin >> a[i] >> b[i];
i++;
}
max1 = a[0];
max2 = b[0];
min1 = a[0];
min2 = b[0];
for (i = 0; i < t; i++) {
if (a[i] > max1) max1 = a[i];
if (b[i] > max2) max2 = b[i];
if (a[i] <= min1) min1 = a[i];
if (b[i] <= min2) min2 = b[i];
}
max1 = max1 - min1;
max2 = max2 - min2;
if (max1 >= max2)
cout << max1 * max1;
else
cout << max2 * max2;
return 0;
}
| 1,300 | CPP |
t=int(input())
for _ in range(t):
b,p,f=map(int,input().split())
h,c=map(int,input().split())
b=b//2
pro=0
if h>c:
if p < b:
b=b-p
pro+=h*p
else:
pro+=h*b
b=0
if b!=0:
if f>b:
pro+=c*b
else:
pro+=c*f
else:
if f < b:
b=b-f
pro+=c*f
else:
pro+=c*b
b=0
if b!=0:
if p>b:
pro+=h*b
else:
pro+=h*p
print(pro)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first)
return a.second < b.second;
else
return a.first > b.first;
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<pair<int, int> > v(n);
for (int i = 0; i < n; i++) {
int sum = 0, x;
for (int j = 0; j < 4; j++) cin >> x, sum += x;
v[i] = {sum, i};
}
sort(v.begin(), v.end(), cmp);
int ret = 0;
for (int i = 0; i < n; i++)
if (v[i].second == 0) ret = i + 1;
cout << ret << endl;
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s1;
string s2;
int sum = 0;
cin >> n;
cin >> s1;
cin >> s2;
for (int i = 0; i < n; i++) {
int cha1;
cha1 = abs(s1[i] - s2[i]);
if (cha1 < 5)
sum += cha1;
else
sum += 10 - cha1;
}
cout << sum << endl;
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, head[200005], ne, st[200005], tp, deep[200005], fa[200005], vis[200005],
ans[200005], cnt;
struct edge {
int t, next, v;
} e[200005 * 2];
void Init(int u, int v, int w) {
e[++ne].t = v;
e[ne].next = head[u];
head[u] = ne;
e[ne].v = w;
}
void Insert(int u, int v, int w) {
Init(u, v, w);
Init(v, u, w);
}
void Dfs(int u, int f) {
int cnt = 0;
fa[u] = f;
for (int k = head[u]; k; k = e[k].next) {
int v = e[k].t;
if (f == v) continue;
cnt++;
deep[v] = deep[u] + 1;
Dfs(v, u);
if (e[k].v == 2) st[++tp] = v;
}
}
void Up_date(int x) {
vis[x] = 1;
while (fa[x] != 0 && !vis[fa[x]]) {
x = fa[x];
vis[x] = 1;
}
}
bool cmp(int x, int y) { return deep[x] > deep[y]; }
int main() {
n = read();
memset(st, 0, sizeof(st));
tp = 0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n - 1; i++) {
int x = read(), y = read(), z = read();
Insert(x, y, z);
}
Dfs(1, 0);
sort(st + 1, st + 1 + tp, cmp);
for (int i = 1; i <= tp; i++) {
if (!vis[st[i]]) {
ans[++cnt] = st[i];
Up_date(st[i]);
}
}
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
puts("");
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
const double EPS = 1e-9;
const int MOD = 1000003;
const int INF = 2000 * 1000 * 1000;
const int MAX = 4000000;
using namespace std;
int popcount(int n) {
bitset<32> b(n);
return b.count();
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) {
int temp = gcd(a, b);
return temp ? (a / temp * b) : 0;
}
template <typename T>
T modpow(T base, T exp, T modulus) {
base %= modulus;
T result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % modulus;
base = (base * base) % modulus;
exp >>= 1;
}
return result;
}
inline bool isInteger(const std::string& s) {
if (s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+')))
return false;
char* p;
strtol(s.c_str(), &p, 10);
return (*p == 0);
}
int idx[10005];
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
idx[v[i]] = i + 1;
}
sort((v).begin(), (v).end());
if (n == 1)
cout << "Exemplary pages." << endl;
else if (n == 2) {
if (v[0] == v[1])
cout << "Exemplary pages." << endl;
else if ((v[0] + v[1]) % 2)
cout << "Unrecoverable configuration." << endl;
else
cout << (v[1] - v[0]) / 2 << " ml. from cup #" << idx[v[0]] << " to cup #"
<< idx[v[1]] << "." << endl;
} else {
bool f = 1;
for (int i = 1; i < n - 2; i++) {
if (v[i] != v[i + 1]) {
f = 0;
break;
}
}
if (f && v[0] == v[1] && v[1] == v[n - 1])
cout << "Exemplary pages." << endl;
else if (f && (v[1] - v[0] == v[n - 1] - v[1])) {
cout << v[1] - v[0] << " ml. from cup #" << idx[v[0]] << " to cup #"
<< idx[v[n - 1]] << "." << endl;
} else {
cout << "Unrecoverable configuration." << endl;
}
}
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
const int maxm = 1000100;
const int INF = 0x3f3f3f3f;
const int mod = 100007;
int n, m;
int a[maxn];
long long sum[maxn];
vector<int> g[maxn];
struct Part {
int v;
int num;
} pp[maxn];
bool cmp(Part a, Part b) { return a.v > b.v; }
int main() {
int i, j;
int k;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pp[i].v = a[i];
pp[i].num = i;
}
for (i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
sum[x] += a[y];
sum[y] += a[x];
}
sort(pp + 1, pp + n + 1, cmp);
long long ans = 0;
for (i = 1; i <= n; i++) {
int tp = pp[i].v;
int tx = pp[i].num;
ans += sum[tx];
for (j = 0; j < g[tx].size(); j++) {
int gg = g[tx][j];
sum[gg] -= tp;
}
}
printf("%I64d\n", ans);
return 0;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k, ans, t, r, i, s;
cin >> n >> k;
i = n * (k - 1) / k;
while (true) {
r = 0;
t = 1;
while (i / t > 0) {
r += i / t;
t *= k;
}
if (r >= n) {
cout << i;
return 0;
} else
i++;
}
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, ans, items[100005];
long long sum[100005];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", items + i);
sum[i] = items[i] + sum[i - 1];
}
for (int i = 1; i < n; ++i) ans += ((sum[n] - sum[i]) == sum[i]);
printf("%d\n", ans);
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[60];
double ans = 0, b[110], c[110][110], f[110][110][110];
int main() {
scanf("%d%d", &n, &m);
b[0] = 1;
for (int i = 1; i <= n; i++) b[i] = b[i - 1] / m;
for (int i = 0; i <= n + m; i++) c[i][0] = c[i][i] = 1;
for (int i = 1; i <= n + m; i++)
for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
f[0][0][0] = 1;
for (int i = 1; i <= m; i++) {
int num;
f[i][0][0] = 1;
scanf("%d", &num);
for (int j = 1; j <= n; j++)
for (int k = 1; k <= j; k++) {
int l = max((k - 1) * num + 1, 0), r = k * num;
for (int p = 0; p < l && p <= j; p++)
f[i][j][k] += f[i - 1][j - p][k] * c[n - j + p][p];
for (int p = l; p <= r && p <= j; p++)
for (int t = 0; t <= k; t++)
f[i][j][k] += f[i - 1][j - p][t] * c[n - j + p][p];
}
}
for (int i = 1; i <= n; i++) ans += f[m][n][i] * i;
for (int i = 1; i <= n; i++) ans /= 1.0 * m;
printf("%.9lf", ans);
return 0;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int from;
int to;
int capacity;
int cost;
int flow;
Edge() {}
Edge(int from, int to, int capacity, int cost)
: from(from), to(to), capacity(capacity), cost(cost), flow(0) {}
};
vector<vector<int>> g;
vector<Edge> edges;
vector<int> first;
int find_min_cost_path() {
int n = g.size();
vector<int> dist(n, 2e9 + 1);
dist[0] = 0;
vector<pair<int, int>> prev(n, {-1, 1e9});
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < edges.size(); ++j) {
auto& e = edges[j];
if (e.capacity - e.flow > 0 && dist[e.to] > dist[e.from] + e.cost) {
dist[e.to] = dist[e.from] + e.cost;
prev[e.to] = {j, min(prev[e.from].second, e.capacity - e.flow)};
if (prev[e.to].second == 0) {
cout << 1 << endl;
}
}
}
}
if (dist[n - 1] > 1e9) {
return -1e9;
}
int cv = n - 1;
vector<bool> used(n);
used[cv] = true;
int d = prev[cv].second;
int cost = 0;
while (true) {
if (prev[cv].first == -1) {
cout << 1 << endl;
}
cost += d * edges[prev[cv].first].cost;
edges[prev[cv].first].flow += d;
edges[prev[cv].first ^ 1].flow -= d;
cv = edges[prev[cv].first].from;
if (cv == 0 || used[cv]) {
used[cv] = true;
break;
}
used[cv] = true;
}
if (used[0]) {
return cost;
}
return -2e9;
}
int min_cost_max_flow() {
int cost = 0;
while (true) {
auto res = find_min_cost_path();
if (res == -1e9) {
break;
}
cost += res;
if (res == -2e9) {
break;
}
}
return cost;
}
int ctoi(char c) {
if ('a' <= c && c <= 'z') {
return c - 'a';
}
return c - 'A' + 26;
}
void add_edge(int from, int to, int capacity, int cost) {
edges.emplace_back(from, to, capacity, cost);
edges.emplace_back(to, from, 0, -cost);
g[from].push_back(edges.size() - 2);
g[to].push_back(edges.size() - 1);
}
void make_graph(string& a, string& b, int k) {
int n = a.length();
g.resize(2 * k + 5);
int sum = 0;
map<pair<int, int>, int> map1;
for (int i = 0; i < n; ++i) {
map1[{ctoi(a[i]), ctoi(b[i])}]++;
}
for (auto item : map1) {
sum += item.second;
add_edge(item.first.first + 3, item.first.second + k + 3, 1, -item.second);
}
for (int i = 0; i < k; ++i) {
add_edge(2, i + 3, 1, 0);
add_edge(i + 3 + k, 2 * k + 3, 1, 0);
}
add_edge(1, 2, sum, 0);
add_edge(2 * k + 3, 2 * k + 4, sum, 0);
add_edge(1, 2 * k + 4, sum, 0);
add_edge(0, 1, sum, 0);
}
char itoc(int x) {
if (x <= 25) {
return 'a' + x;
}
return 'A' + x - 26;
}
vector<char> get_swap(int k) {
map<int, char> change;
set<char> rest;
for (int i = 0; i < k; ++i) {
rest.insert(itoc(i));
}
for (auto& e : edges) {
if (e.cost < 0 && e.flow == 1) {
char val = itoc(e.to - 3 - k);
change[e.from - 3] = val;
rest.erase(val);
}
}
vector<char> res(k, -1);
for (auto item : change) {
res[item.first] = item.second;
}
for (auto& i : res) {
if (i == -1) {
i = *rest.begin();
rest.erase(rest.begin());
}
}
return res;
}
int main() {
int n, k;
cin >> n >> k;
string a, b;
cin >> a >> b;
g.resize(n);
first.resize(n);
make_graph(a, b, k);
cout << -min_cost_max_flow() << endl;
auto res = get_swap(k);
for (char c : res) {
cout << c;
}
cout << endl;
}
| 2,300 | CPP |
import sys
if __name__ == '__main__':
num_computers = int(input())
if num_computers < 1 or num_computers > 1000:
sys.exit()
input_inf = input().split()
computers = [int(i) for i in input_inf]
new_computers = []
num_information = 0
num_turns = 0
current_computer = 0
while num_computers != num_information:
for i in computers:
if num_information >= i:
num_information += 1
else:
new_computers.append(i)
if len(new_computers) != 0:
computers.clear()
new_computers.reverse()
computers.extend(new_computers)
new_computers.clear()
num_turns += 1
print(num_turns)
| 1,200 | PYTHON3 |
n,m=map(int,input().split())
i=0
while(i!=10**9):
n=n*3
m=m*2
if n>m:
print(i+1)
break
i=i+1
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long n, x;
signed main() {
long long t = 1;
while (t--) {
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
bool even_found = 0, odd_found = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
if (a[i] % 2)
odd_found = 1;
else
even_found = 1;
}
if (odd_found && even_found) {
sort(a.begin(), a.end());
}
for (long long i = 0; i < n; i++) {
cout << a[i] << " ";
}
}
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long w, m, k;
cin >> w >> m >> k;
long long sn = int(floor(log10(m))) + 1;
long long len = 0;
while (w > 0) {
if (w < (sn * k)) break;
long long n = pow(10, sn);
long long a = n - 1;
long long l1 = a - m + 1;
long long z = w / (sn * k);
if (z >= l1) {
w -= l1 * sn * k;
sn++;
len += l1;
m = n;
} else {
len += z;
break;
}
}
cout << len << endl;
}
| 1,600 | CPP |
from sys import stdin
n, n1, n2 = map(int, stdin.readline().split())
a = sorted(map(int, stdin.readline().split()), key=lambda x: -x)
mn = min(n1, n2)
mx = max(n1, n2)
# print(mn, mx)
# print(a)
# print(sum(a[:mn]) / mn, sum(a[mn:mn+mx]) / mx)
print((sum(a[:mn]) / mn) + (sum(a[mn:mn+mx]) / mx))
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
struct MI {
private:
char bb[1 << 14];
FILE* f;
char *bs, *be;
char e;
bool o, l;
public:
MI() : f(stdin), bs(0), be(0) {}
MI(FILE* f) : f(f), bs(0), be(0) {}
inline operator bool() { return !l; }
inline char get() {
if (o) {
o = 0;
return e;
}
if (bs == be) be = (bs = bb) + fread(bb, 1, sizeof(bb), f);
if (bs == be) {
l = 1;
return -1;
};
return *bs++;
}
inline void unget(char c) {
o = 1;
e = c;
}
template <class T>
inline T read() {
T r;
*this > r;
return r;
}
template <class T>
inline MI& operator>(T&);
};
template <class T>
struct Q {
const static bool U = T(-1) >= T(0);
inline void operator()(MI& t, T& r) const {
r = 0;
char c;
bool y = 0;
if (U)
for (;;) {
c = t.get();
if (c == -1) goto E;
if (isdigit(c)) break;
}
else
for (;;) {
c = t.get();
if (c == -1) goto E;
if (c == '-') {
c = t.get();
if (isdigit(c)) {
y = 1;
break;
};
} else if (isdigit(c))
break;
;
};
for (;;) {
if (c == -1) goto E;
if (isdigit(c))
r = r * 10 + (c ^ 48);
else
break;
c = t.get();
}
t.unget(c);
E:;
if (y) r = -r;
}
};
template <>
struct Q<char> {
inline void operator()(MI& t, char& r) {
int c;
for (;;) {
c = t.get();
if (c == -1) {
r = -1;
return;
};
if (!isspace(c)) {
r = c;
return;
};
}
}
};
template <class T>
inline MI& MI::operator>(T& t) {
Q<T>()(*this, t);
return *this;
}
template <class T>
std::ostream& operator<(std::ostream& out, const T& t) {
return out << t;
}
using std::cout;
MI cin;
const int $n = 2005;
int n, fa[$n], v[$n], deg[$n], rt, ans[$n], siz[$n], dep[$n], tp;
std::vector<int> outs[$n];
struct cmp_t {
inline bool operator()(int x, int y) const { return dep[x] > dep[y]; }
};
std::priority_queue<int, std::vector<int>, cmp_t> q;
void dfs(int x) {
siz[x] = 1;
dep[x] = dep[fa[x]] + 1;
for (int v : outs[x]) dfs(v), siz[x] += siz[v];
}
int main() {
cin > n;
for (int __r = (n), i = (1); i <= __r; ++i) {
cin > fa[i] > v[i];
if (!fa[i]) rt = i;
outs[fa[i]].push_back(i);
++deg[fa[i]];
}
dfs(rt);
for (int __r = (n), i = (1); i <= __r; ++i)
if (v[i] >= siz[i]) return cout < "NO\n", 0;
for (int __r = (n), i = (1); i <= __r; ++i)
if (!v[i]) q.push(i);
while (!q.empty()) {
const auto x = q.top();
q.pop();
ans[x] = ++tp;
for (int i = fa[x]; i; i = fa[i])
if (!--v[i]) q.push(i);
}
for (int __r = (n), i = (1); i <= __r; ++i)
if (!ans[i]) return cout < "NO\n", 0;
cout < "YES\n";
for (int __r = (n), i = (1); i <= __r; ++i) cout < ans[i] < " \n"[i == n];
}
| 1,800 | CPP |
for _ in range(int(input())):
a, b, n, S = map(int, input().split())
kBigCoins = S//n
if a >= kBigCoins:
if S - kBigCoins*n <= b:
print("YES")
else:
print("NO")
else:
if S-(a*n) <= b:
print("YES")
else:
print("NO")
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, v[200001] = {0}, i, minn = 1000000001;
char s[200001];
bool ok = false;
cin >> n;
cin.get();
cin.getline(s, 200001);
for (i = 0; i < n; i++) {
cin >> v[i];
}
for (i = 0; i < n - 1; i++) {
if (s[i] == 'R' && s[i + 1] == 'L') {
if (minn > (v[i + 1] - v[i]) / 2) {
minn = (v[i + 1] - v[i]) / 2;
ok = true;
}
}
}
if (ok == true)
cout << minn;
else
cout << -1;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, k, m, shu[6000];
int main() {
scanf("%lld%lld", &n, &k);
long long imax = 0;
queue<long long> q;
for (long long i = 1; i <= n; i++) {
scanf("%lld", &shu[i]);
q.push(shu[i]);
imax = max(imax, shu[i]);
}
if (n <= k) {
printf("%d\n", imax);
} else {
long long winer = q.front();
q.pop();
long long summ = 0;
while (summ < k) {
long long bettle = q.front();
q.pop();
if (winer >= bettle) {
summ = summ + 1;
q.push(bettle);
} else {
summ = 1;
q.push(winer);
winer = bettle;
}
}
printf("%lld\n", winer);
}
return 0;
}
| 1,200 | CPP |
(n, w), a = map(int, input().split()), list(map(int, input().split()))
l, r = -1, w + 1
while r - l > 1:
m = (r + l) // 2
now = m + 1 - 1
can = True
for i in range(n):
now += a[i]
can &= now >= 0
if not can:
l = m
else:
r = m
if r <= 0:
if r < 0:
print(0)
exit(0)
else:
now = 0
if sum(a) > w:
print(0)
exit(0)
min_can = r
l, r = 0, w + 1
while r - l > 1:
m = (r + l) // 2
can = True
now = m + 1 - 1
for i in range(n):
now += a[i]
can &= now <= w
if not can:
r = m
else:
l = m
max_can = max(l, 0)
print(max(max_can - min_can + 1, 0)) | 1,400 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const int maxn = 100000 + 10;
const int mod = 1e9 + 7;
int n, m, w;
pair<int, int> que[600];
int lim[1200];
int check(int len) {
vector<int> edg;
edg.push_back(1), edg.push_back(n);
int L = inf, R = -1, U = inf, D = -1;
for (int i = 0; i < w; i++) {
int u = que[i].first - len;
int v = que[i].first + len - 1;
if (u >= 1 && u <= n) edg.push_back(u);
if (u + 1 >= 1 && u + 1 <= n) edg.push_back(u + 1);
if (v >= 1 && v <= n) edg.push_back(v);
if (v + 1 >= 1 && v + 1 <= n) edg.push_back(v + 1);
}
sort(edg.begin(), edg.end());
int q = unique(edg.begin(), edg.end()) - edg.begin();
for (int i = 0; i < q; i++) {
vector<pair<int, int> > sub;
for (int j = 0; j < w; j++) {
int ll = que[j].second - len + 1;
int rr = que[j].second + len;
int u = que[j].first - len + 1;
int v = que[j].first + len - 1;
if (edg[i] >= u && edg[i] <= v) {
sub.push_back(make_pair(max(ll, 1), -1));
sub.push_back(make_pair(min(rr, m + 1), 1));
}
}
sub.push_back(make_pair(m + 1, 1));
sort(sub.begin(), sub.end());
int sum = 0;
int pre = 1;
if (sub.size() <= 1) {
L = min(L, 1);
R = max(R, m);
U = min(U, edg[i]);
D = max(D, edg[i]);
}
for (int j = 0; sub[j].first <= m;) {
while (sub[j].first == pre) sum += sub[j].second, j++;
if (sum == 0) {
L = min(L, pre);
R = max(R, sub[j].first - 1);
U = min(U, edg[i]);
D = max(D, edg[i]);
}
pre = sub[j].first;
}
}
if (R - L + 2 <= 2 * len && D - U + 2 <= 2 * len) return 1;
return 0;
}
int main() {
scanf("%d%d%d", &n, &m, &w);
for (int i = 0; i < w; i++) {
scanf("%d%d", &(que[i].first), &que[i].second);
}
sort(que, que + w);
int l = 1, r = max(n, m);
int ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, r = mid - 1;
else
l = mid + 1;
}
cout << ans - 1 << endl;
return 0;
}
| 2,400 | CPP |
n,k,t=map(int,input().split())
if t<=k:
print(t)
elif t>k and t<=n:
print(k)
else:
print(abs((t-n)-k))
| 800 | PYTHON3 |
#https://codeforces.com/contest/1077/problem/A
#le entrada
n = int(input())
l = []
for i in range(0, n, 1):
l.append(list(map(int, input().split())))
for i in range(0, n, 1):
dist = 0
if(l[i][2] % 2 == 0):
dist = (l[i][0] - l[i][1])*((l[i][2])//2)
else:
dist = l[i][0]
l[i][2] = l[i][2] - 1
dist = dist + (l[i][0] - l[i][1])*((l[i][2])//2)
print(int(dist))
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
if (a == b) {
cout << a << endl;
} else {
cout << 1 << endl;
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
const long N = 105;
std::vector<long> a;
int main() {
long n;
scanf("%ld", &n);
long ciel(0), total(0);
for (long p = 0; p < n; p++) {
long k;
scanf("%ld", &k);
for (long q = 0; q < k; q++) {
long x;
scanf("%ld", &x);
total += x;
if (q < k / 2) {
ciel += x;
}
if (k & 1 && q == k / 2) {
a.push_back(x);
}
}
}
sort(a.rbegin(), a.rend());
for (long p = 0; p < a.size(); p += 2) {
ciel += a[p];
}
printf("%ld %ld\n", ciel, total - ciel);
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 10;
const long long inf = 1ll << 60;
const long long mod = 1e9 + 7;
void GG() {
cout << "0\n";
exit(0);
}
long long mpow(long long a, long long n, long long mo = mod) {
long long re = 1;
while (n > 0) {
if (n & 1) re = re * a % mo;
a = a * a % mo;
n >>= 1;
}
return re;
}
long long inv(long long b, long long mo = mod) {
if (b == 1) return b;
return (mo - mo / b) * inv(mo % b, mo) % mo;
}
const int maxn = 1e6 + 5;
long long fac[maxn], ifac[maxn];
void build() {
fac[0] = ifac[0] = 1;
for (int i = 1; i < maxn; ++i)
fac[i] = fac[i - 1] * (long long)i % mod, ifac[i] = inv(fac[i]);
}
long long C(int a, int b) {
if (a == -1 && b == -1) return 1;
if (b > a || b < 0 || a < 0) return 0;
return fac[a] * ifac[b] % mod * ifac[a - b] % mod;
}
long long first(int L, int c00, int c01, int c10, int c11, bool bef = 0) {
if (c00 < 0 || c01 < 0 || c10 < 0 || c11 < 0) return 0;
++L;
int n1 = c01 + c11 + (bef == 1), n0 = c00 + c10 + (bef == 0);
if (L != n1 + n0) {
;
return 0;
}
int chg = bef + c01 - c10;
if (chg < 0 || chg > 1) {
return 0;
}
int por1 = c01 + (bef == 1), por0 = c10 + (bef == 0);
long long CC = C(n1 - 1, por1 - 1) * C(n0 - 1, por0 - 1) % mod;
;
return CC;
}
long long gt(vector<int> &x, array<array<int, 2>, 2> c) {
int n = (int)x.size();
long long re = 0;
if (c == array<array<int, 2>, 2>{{{0, 0}, {0, 0}}} && (int)x.size() > 1) {
re++;
}
{
array<array<int, 2>, 2> b = c;
for (int i = 1; i < n; ++i) {
if (x[i] == 1) {
b[x[i - 1]][0]--;
re = re + first(n - i - 1, b[0][0], b[0][1], b[1][0], b[1][1], 0);
re %= mod;
b[x[i - 1]][0]++;
}
b[x[i - 1]][x[i]]--;
}
}
{
for (int L = n - 2; L > 0; --L) {
re += first(L, c[0][0], c[0][1], c[1][0], c[1][1], 1);
re %= mod;
}
};
return re;
}
bool isok(vector<int> &x, array<array<int, 2>, 2> c) {
for (int i = 1; i < (int)x.size(); ++i) {
c[x[i - 1]][x[i]]--;
}
array<array<int, 2>, 2> d = {{{0, 0}, {0, 0}}};
return c == d;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
build();
string ra, rb;
cin >> ra >> rb;
vector<int> a, b;
for (char c : ra) a.push_back(c - '0');
for (char c : rb) b.push_back(c - '0');
array<array<int, 2>, 2> c;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) cin >> c[i][j];
long long re = gt(b, c) - gt(a, c);
;
re += isok(b, c);
re = (re + 2 * mod) % mod;
cout << re << '\n';
}
| 2,400 | CPP |
# -*- coding: utf-8 -*-
import sys
def main():
n = int(input())
for i in range(2 * n + 1):
for j in range(2 * n + 1):
t = n - (abs(n - i) + abs(n - j))
if t < 0:
print(' ', end=' ')
elif t == 0 and j >= n:
print(0, end='')
break
else:
print(t, end=' ')
print()
if __name__ == '__main__':
main() | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int matrix[1000][1000];
int ultim_luat[1000][1000];
pair<short int, short int> luat[2][100000];
int c[2];
int main() {
ios_base ::sync_with_stdio(0);
int n;
cin >> n;
matrix[500][500] = n;
luat[0][0] = {500, 500};
c[0] = 1;
for (int q(0), j(1); c[q]; q = 1 - q, j++) {
c[1 - q] = 0;
for (int i(0); i < c[q]; i++) {
int x(luat[q][i].first), y(luat[q][i].second);
ultim_luat[x][y] = 0;
int div(matrix[x][y] / 4);
matrix[x][y] -= 4 * div;
if (div == 0) continue;
for (int l(-1); l <= 1; l++) {
for (int p(-1); p <= 1; p++) {
if (l != 0 && p != 0) continue;
if (l == p) continue;
matrix[x + l][y + p] += div;
if (matrix[x + l][y + p] >= 4 && !ultim_luat[x + l][y + p]) {
ultim_luat[x + l][y + p] = 1;
luat[1 - q][c[1 - q]++] = {x + l, y + p};
}
}
}
}
}
int t, a, b;
cin >> t;
while (t--) {
cin >> a >> b;
a += 500, b += 500;
if (a > 1000 || a < 0 || b > 1000 || b < 0)
cout << "0\n";
else
cout << matrix[a][b] << '\n';
}
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, i, num, val;
cin >> n;
for (i = 1; i <= n; i++) {
val = (pow(2, i) - 1) * (pow(2, i - 1));
if (n % val == 0) {
num = val;
}
}
cout << num;
return 0;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
vector<int> x(n), y(n);
for (int& xi : x) cin >> xi;
for (int& yi : y) cin >> yi;
for (long long i = 0, px = 0, py = 0, qx, qy; i < n; ++i, px = qx, py = qy) {
qx = max(0LL, x[i] + px - y[i] * k);
qy = max(0LL, y[i] + py - x[i] * k);
if (qx > k || qy > k) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
| 2,500 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
int n, s, x[200], y[200];
signed main() {
scanf("%d%d", &n, &s);
for (int i = 2; i <= n + 1; i++) {
scanf("%d%d", x + i, y + i);
}
if (x[2] >= 0 && y[2] >= 0) {
int now = x[2] * 60 + y[2];
if (now >= s + 1) return 0 * printf("0 0\n");
}
x[n + 2] = y[n + 2] = 0x3f3f3f3f;
for (int i = 3; i <= n + 2; i++) {
int a = x[i - 1] * 60 + y[i - 1];
int b = x[i] * 60 + y[i];
if (b - a >= 2 * s + 2) {
int c = x[i - 1], d = y[i - 1];
d += s + 1;
c += d / 60;
d %= 60;
return 0 * printf("%d %d\n", c, d);
}
}
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int s[4];
cin >> s[0] >> s[1] >> s[2] >> s[3];
int howMany = 0;
int equalShoes = 0;
int colors = 0;
for (int i = 0; i < 4; i++) {
bool found = false;
for (int j = 0; j < 4; j++) {
if (i == j) {
continue;
}
if ((s[i] == s[j]) && s[j] != 0) {
s[j] = 0;
if (!found) {
++colors;
++equalShoes;
found = true;
}
++equalShoes;
}
}
}
if (equalShoes == 2) {
howMany = 1;
} else if (equalShoes == 3) {
howMany = 2;
} else if (equalShoes == 4) {
if (colors == 1) {
howMany = 3;
} else {
howMany = 2;
}
}
cout << howMany << endl;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
void shoot() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
shoot();
long long i, n, sum = 0;
long long hash[301];
string s;
memset(hash, 0, sizeof(hash));
cin >> s;
n = s.size();
for (i = 0; i < n; i++) {
hash[(int)s[i]]++;
}
sum = n;
for (i = 0; i <= 300; i++) {
sum += hash[i] * (hash[i] - 1);
}
cout << sum;
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5 + 10;
const int MAX_M = 5e5 + 10;
const int INF8 = 0x3f;
const int INF32 = 0x3f3f3f3f;
int tr[MAX_N << 2];
void modify(int t, int lb, int rb, int k, int v) {
if (rb - lb == 1) return tr[t] = v, void();
int mid = (lb + rb) >> 1;
if (k < mid)
modify(t << 1, lb, mid, k, v);
else
modify(t << 1 | 1, mid, rb, k, v);
return tr[t] = min(tr[t << 1], tr[t << 1 | 1]), void();
}
int min_query(int t, int lb, int rb, int ql, int qr) {
if (ql <= lb && rb <= qr) return tr[t];
if (rb <= ql || qr <= lb) return INF32;
int mid = (lb + rb) >> 1;
return min(min_query(t << 1, lb, mid, ql, qr),
min_query(t << 1 | 1, mid, rb, ql, qr));
}
void discretize(int *lb_, int *rb_) {
int len_ = rb_ - lb_, sz_, *cp_ = new int[len_];
memcpy(cp_, lb_, len_ * sizeof(int));
sort(cp_, cp_ + len_);
sz_ = unique(cp_, cp_ + len_) - cp_;
for (int i = 0; i < len_; i++)
*(lb_ + i) = lower_bound(cp_, cp_ + sz_, *(lb_ + i)) - cp_ + 1;
return;
}
struct QUERY {
int l, r, qid;
} qry[MAX_M];
int pre[MAX_N], nxt[MAX_N], last[MAX_N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, a[MAX_N];
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> qry[i].l >> qry[i].r, qry[i].qid = i;
discretize(a + 1, a + 1 + n);
memset(pre, ~INF8, sizeof(pre));
for (int i = 1; i <= n; i++) {
if (last[a[i]] != 0) {
pre[i] = last[a[i]];
nxt[last[a[i]]] = i;
}
last[a[i]] = i;
}
for (int i = 1; i <= n; i++) modify(1, 1, n + 1, i, i - pre[i]);
sort(qry, qry + m, [](QUERY a, QUERY b) { return a.l < b.l; });
int ptr = 1, ans[MAX_M];
for (int i = 0; i < m; i++) {
while (ptr < qry[i].l) {
modify(1, 1, n + 1, nxt[ptr], INF32);
ptr++;
}
ans[qry[i].qid] = min_query(1, 1, n + 1, qry[i].l, qry[i].r + 1);
}
for (int i = 0; i < m; i++) {
if (ans[i] >= INF32)
cout << "-1\n";
else
cout << ans[i] << '\n';
}
return 0;
}
| 2,000 | CPP |
n,m=map(int,input().split())
a=[[] for i in range(n)]
for i in range(n):
a[i]=list(input())
hor,ver=[[0]*2005 for i in range(2005)],[[0]*2005 for i in range(2005)]
for i in range(0,n+1):
for j in range(0,m+1):
if i==0 or j==0:
ver[i][j]=hor[i][j]=0
else:
ver[i][j]=ver[i-1][j]+ver[i][j-1]-ver[i-1][j-1]
hor[i][j]=hor[i-1][j]+hor[i][j-1]-hor[i-1][j-1]
if i<n and a[i-1][j-1]=='.' and a[i][j-1]=='.':ver[i][j]+=1
if j<m and a[i-1][j-1] == '.' and a[i - 1][j] == '.':hor[i][j]+=1
for _ in range(int(input())):
xb,yb,xa,ya=map(int,input().split())
ans=0
ans += hor[xa][ya - 1] - hor[xb - 1][ya - 1] - hor[xa][yb - 1] + hor[xb - 1][yb - 1]
ans += ver[xa - 1][ya] - ver[xa - 1][yb - 1] - ver[xb - 1][ya] + ver[xb - 1][yb - 1]
print(ans)
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<string> res;
void Paint(int x, int y, int height, int open) {
res[y][x] = '+';
res[y + height + 1][x] = '+';
for (int i = y + 1; i < y + height + 1; i++) {
res[i][x] = '|';
}
if (open) {
res[y][x + open] = '-';
res[y + height + 1][x + open] = '-';
}
}
int main() {
int n;
string s;
cin >> n >> s;
int max_depth = 0;
int len = 0;
for (int i = 0, depth = 0; i < s.size(); i++) {
if (s[i] == '[') {
len++;
depth++;
max_depth = max(depth, max_depth);
} else {
len++;
if (s[i - 1] == '[') {
len += 3;
}
depth--;
}
}
res.resize(max_depth * 2 + 1, string(len, ' '));
for (int i = 0, pos = 0, depth = 0; i < s.size(); i++, pos++) {
if (s[i] == '[') {
depth++;
int height = (max_depth - depth) * 2 + 1;
Paint(pos, depth - 1, height, 1);
} else {
if (s[i - 1] == '[') {
pos += 3;
}
int height = (max_depth - depth) * 2 + 1;
Paint(pos, depth - 1, height, -1);
depth--;
}
}
for (int i = 0; i < res.size(); i++) {
cout << res[i] << endl;
}
}
| 1,400 | CPP |
s = 0
for a in range(0, int(input())):
x,y,z = map(int, input().split())
if (x+y+z) >= 2:
s+=1
print(s) | 800 | PYTHON3 |
"""
t=int(input())
for _ in range(t):
#n=int(input())
n,k=list(map(int,input().split()))
if n<k:
print("NO")
continue
if n%2==1 and k%2==1:
print("YES")
print(*([1]*(k-1)+[n-k+1]))
elif n%2==1:
print("NO")
else:
arr=[2]*(k-1)+[n-k-k+2]
if n-2*k+2<0:
print("NO")
else:
print("YES")
print(*arr)
"""
import math
#123 567 91011 131415
#123 456 789 101112
#12 45 78 1011
#12 34 56 78
t=int(input())
for _ in range(t):
#n=int(input())
n,k=list(map(int,input().split()))
#arr=[i for i in range(1,n)]
vv=math.ceil(k/(n-1))
val=k%(n-1)
if val==0:
val=n-1
#print(val)
print(1+n*(vv-1)+val-1)
| 1,200 | PYTHON3 |
x = 0
n = int(input())
i = 0
while i < n:
a = input()
if a == '++X' or 'X++' == a:
x += 1
else:
x -= 1
i += 1
print(x)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct less_key {
bool operator()(const pair<int64_t, int64_t>& p1,
const pair<int64_t, int64_t>& p2) {
return p1.first < p2.first ||
(p1.first == p2.first && p1.second > p2.second);
}
};
struct pair_hash {
std::size_t operator()(const pair<int64_t, int64_t>& k) const {
return static_cast<size_t>(k.first ^ k.second);
}
};
int64_t n;
char a[202];
int64_t x;
int64_t y;
int64_t cx;
int64_t cy;
void move(char c, int64_t& x, int64_t& y) {
switch (c) {
case 'U':
y++;
break;
case 'D':
y--;
break;
case 'L':
x--;
break;
case 'R':
x++;
break;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int64_t i = 1; i <= n; i++) {
cin >> a[i];
}
int64_t S = 0;
for (int64_t i = 0; i <= n; i++) {
x = cx;
y = cy;
for (int64_t j = i + 1; j <= n; j++) {
move(a[j], x, y);
if (cx == x && cy == y) {
S++;
}
}
move(a[i], cx, cy);
}
cout << S;
return 0;
}
| 1,000 | CPP |
import sys
input = sys.stdin.readline
def solve(n, m):
CHAR = ['B', 'W']
index = 0
for ii in range(n):
row = ""
for jj in range(m):
if ii == 0 and jj == 1:
row += CHAR[index%2] if (n*m)%2 == 1 else 'B'
else:
row += CHAR[index%2]
index += 1
print(row)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
[n, m] = map(int, input().split())
solve(n, m)
'''
BBBW
WBWB
BWBW
''' | 1,000 | PYTHON3 |
#!/usr/bin/python3
Na, Nb = map(int, input().split())
k, m = map(int, input().split())
assert 1 <= k <= Na
assert 1 <= m <= Nb
a = list(map(int, input().split()))
b = list(map(int, input().split()))
assert len(a) == Na
assert len(b) == Nb
print('YES' if a[k-1] < b[-m] else 'NO') | 900 | PYTHON3 |
n, m = [int(i) for i in input().split()]
sequence = list(map(int, input().split()))
l_list = [int(input()) - 1 for _ in range(m)]
max_num = max(sequence)
f = [0] * (max_num + 1)
k = 0
d = [0] * len(sequence)
for i in range(len(sequence) - 1, -1, -1):
index = sequence[i]
if f[index] == 0:
f[index] = 1
k += 1
d[i] = k
for i in l_list:
print(d[i])
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 22;
const long long inf = 1LL << 60;
int n, m;
long long dp[maxn << 1][maxn];
long long a[maxn << 1], b[maxn << 1];
void init() {
for (int i = 0; i <= (n << 1); ++i) {
for (int j = 0; j <= n; ++j) {
dp[i][j] = inf;
}
}
dp[0][0] = 0;
}
long long get_start(const int m) {
init();
for (int k = 1; k <= n; ++k) {
for (int i = ((k + 1) >> 1); i <= min(k, m); ++i) {
dp[k][i] = min(dp[k - 1][i - 1] + a[k], dp[k - 1][i] + b[k]);
}
}
return dp[n][m];
}
long long get_end(const int m) {
init();
for (int k = 1; k <= n; ++k) {
for (int i = 0; i <= min(k, m); ++i) {
if (n - k < ((m - i) << 1)) {
continue;
}
if (i == 0) {
dp[k][i] = dp[k - 1][i] + b[k];
} else {
dp[k][i] = min(dp[k - 1][i - 1] + a[k], dp[k - 1][i] + b[k]);
}
}
}
return dp[n][m];
}
long long solve(const int m) {
init();
for (int k = 1; k <= (n << 1); ++k) {
for (int i = 0; i <= min(k, n); ++i) {
if ((i << 1) < (k - m)) {
continue;
}
if (i == 0) {
dp[k][i] = dp[k - 1][i] + b[k];
} else {
dp[k][i] = min(dp[k - 1][i - 1] + a[k], dp[k - 1][i] + b[k]);
}
}
}
return dp[n << 1][n];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%I64d", &a[i]);
}
for (int i = 1; i <= n; ++i) {
scanf("%I64d", &b[i]);
}
for (int i = 1; i <= n; ++i) {
a[n + i] = a[i];
b[n + i] = b[i];
}
long long ans = inf;
for (int i = ((n + 1) >> 1); i <= n; ++i) {
long long s = get_start(i);
long long w = solve((i << 1) - n);
long long e = get_end(n - i);
ans = min(ans, s + e + ((m >> 1) - 1) * w);
}
printf("%I64d\n", ans);
return 0;
}
| 2,500 | CPP |
# -*- coding: utf-8 -*-
# @Time : 2021/1/12 10:14 上午
# @Author : qu
# @Email : [email protected]
# @File : A. Fly.py
from sys import stdin
EPS = 1e-6
n = int(stdin.readline())
m = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
b.append(b[0])
def check(f):
fuel_left = f
total_weight = float(m + fuel_left)
for i in range(n):
cost = total_weight / a[i]
fuel_left = fuel_left - cost
total_weight = total_weight - cost
cost = total_weight / b[i + 1]
fuel_left = fuel_left - cost
total_weight = total_weight - cost
if fuel_left < 0:
return False
return True
def binary_search(left, right):
mid = (left + right) / 2
if abs(left - right) < EPS:
return mid
if check(mid):
return binary_search(left, mid)
else:
return binary_search(mid, right)
res = binary_search(0, 1e9 + 1)
if res - 1e9 > EPS:
print(-1)
else:
print("%.10f" % res)
| 1,500 | PYTHON3 |
n,m=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
for i in range(1,n):
l1[i]+=l1[i-1]
for i in range(m):
beg=0
last=n-1
while beg<=last:
mid=(beg+last)//2
if l2[i]<=l1[mid]:
if mid==0:
print(mid+1,l2[i])
break
elif l2[i]>l1[mid-1]:
print(mid+1,l2[i]-l1[mid-1])
break
else:
last=mid-1
else:
beg=mid+1
| 1,000 | PYTHON3 |
a,b,c = map(int,input().split())
x,y,z = map(int,input().split())
print('NO') if a>x or a+b>x+y or a+b+c>x+y+z else print('YES')
| 800 | PYTHON3 |
numberSushi = int(input())
sushiRow = input().split(" ")
for i in range(numberSushi):
sushiRow[i] = int(sushiRow[i])
sequenceSizes = [0] * (numberSushi + 2)
currentSushiType = sushiRow[0]
currentSequenceIndex = 1
for i in range(numberSushi):
if sushiRow[i] == currentSushiType:
sequenceSizes[currentSequenceIndex] += 1
else:
if currentSushiType == 1:
currentSushiType = 2
else:
currentSushiType = 1
currentSequenceIndex += 1
sequenceSizes[currentSequenceIndex] += 1
answer = 2
i = 1
while sequenceSizes[i] != 0:
atLeft = sequenceSizes[i - 1]
atRight = sequenceSizes[i + 1]
if sequenceSizes[i] >= atLeft and sequenceSizes[i] >= atRight:
answer = max(answer, max(atLeft, atRight) * 2)
i += 1
print(answer)
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1000;
int n, x, y;
pair<int, int> t[N];
long double a, b;
long double pi = acos(-1);
long double dis(int x, int y) {
return sqrtl(
(long double)(t[x].first - t[y].first) * (t[x].first - t[y].first) +
(long double)(t[x].second - t[y].second) * (t[x].second - t[y].second));
}
long double wek(int a, int b, int c, int d) {
return (long double)a * d - (long double)b * c;
}
int main() {
a = 1010100110011001;
scanf("%d%d%d", &n, &x, &y);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &t[i].first, &t[i].second);
t[i].first -= x;
t[i].second -= y;
a = min(a, sqrtl((((long double)t[i].first * t[i].first) +
((long double)t[i].second * t[i].second))));
b = max(b, sqrtl(((long double)t[i].first * t[i].first) +
((long double)t[i].second * t[i].second)));
}
for (int i = 1; i <= n; ++i) {
int j = i % n + 1;
long double qw = abs(wek(-t[i].first, -t[i].second, t[j].first - t[i].first,
t[j].second - t[i].second));
if (dis(i, 0) * dis(i, 0) < dis(j, 0) * dis(j, 0) + dis(i, j) * dis(i, j))
if (dis(j, 0) * dis(j, 0) < dis(i, 0) * dis(i, 0) + dis(i, j) * dis(i, j))
a = min(a, qw / dis(i, j));
}
printf("%.10Lf\n", (b * b - a * a) * pi);
return 0;
}
| 1,900 | CPP |
s = input()
str1 = 'AYOEUIayoeui'
s_new = ''
for i in s:
if i not in str1:
s_new += '.'
s_new += i
print(s_new.lower())
| 1,000 | PYTHON3 |
import sys
import math
stra=str(input())
sym=[(">"),("<"),("+"),("-"),("."),(","),("["),("]")]
val=[(8),(9),(10),(11),(12),(13),(14),(15)]
abc=stra[::-1]
#print(abc)
suma,k=0,0
for i in range(len(abc)):
#print(val[sym.index(abc[i])])
suma+=val[sym.index(abc[i])]*((int)(pow(2,k)))
k+=4
print(suma%(1000003)) | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int x, y, r, x2, y2, r2;
int main() {
cin >> x >> y >> r;
cin >> x2 >> y2 >> r2;
int maxr = max(r, r2);
int minr = min(r, r2);
double d = sqrt((1.0 * x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
if (maxr - minr >= d)
cout << setprecision(18) << fixed << (1.0 * maxr - d - minr) / 2;
if (r + r2 >= d) return cout << 0, 0;
cout << setprecision(18) << fixed << (1.0 * d - 1.0 * r - 1.0 * r2) / 2;
return 0;
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, f, k;
scanf("%d %d %d %d", &a, &b, &f, &k);
int cur1 = b - f;
int c = 2 * (a - f);
int d = 2 * f;
int count = 0;
long long dist = (long long)k * a - f;
int i = 1;
if (cur1 >= 0) {
for (; i <= k; i++) {
if (i & 1) {
if (cur1 < c && cur1 < dist) {
cur1 = b;
count++;
}
if (dist - cur1 > 0 && cur1 - c < 0) {
break;
}
dist -= c;
cur1 -= c;
} else {
if (cur1 < d && cur1 < dist) {
cur1 = b;
count++;
}
if (dist - cur1 > 0 && cur1 - d < 0) {
break;
}
dist -= d;
cur1 -= d;
}
}
}
if (i == k + 1) {
printf("%d\n", count);
} else {
printf("-1\n");
}
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
os << '[';
string sep;
for (const T& el : v) os << sep << el, sep = ", ";
return os << ']';
}
template <typename S, typename T>
ostream& operator<<(ostream& os, pair<S, T> p) {
return os << '(' << p.first << ',' << p.second << ')';
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
cerr << ' ' << H;
dbg_out(T...);
}
void player_2(int n) {
cout << "Second" << endl;
vector<vector<int> > edgelist(n);
vector<pair<int, int> > raw_numbers(n, make_pair(-1, -1));
vector<pair<int, int> > pairs_with_mod(n, make_pair(-1, -1));
for (int i = 1; i < 2 * n + 1; i++) {
int p;
cin >> p;
p--;
if (raw_numbers[p].first < 0) {
raw_numbers[p].first = i;
} else {
raw_numbers[p].second = i;
}
if (pairs_with_mod[i % n].first < 0) {
pairs_with_mod[i % n].first = p;
} else {
pairs_with_mod[i % n].second = p;
}
};
vector<vector<int> > cycles;
vector<bool> used_mods(n, false);
for (int i = 0; i < n; i++) {
if (used_mods[i]) continue;
vector<int> cyc;
int tail_pair = pairs_with_mod[i].first;
int head_pair = pairs_with_mod[i].second;
int curr_mod = i;
while (true) {
;
cyc.push_back(head_pair);
if (raw_numbers[head_pair].second % n == curr_mod)
raw_numbers[head_pair] = make_pair(raw_numbers[head_pair].second,
raw_numbers[head_pair].first);
if (head_pair == tail_pair) break;
curr_mod = raw_numbers[head_pair].second % n;
used_mods[curr_mod] = true;
pair<int, int>& mod_pairs = pairs_with_mod[curr_mod];
if (mod_pairs.second == head_pair)
mod_pairs = make_pair(mod_pairs.second, mod_pairs.first);
head_pair = mod_pairs.second;
}
cycles.push_back(cyc);
};
;
int mod2 = 0;
for (int i = 0; i < n; i++) {
mod2 = (mod2 + raw_numbers[i].first) % 2;
}
if (mod2) {
for (vector<int>& cyc : cycles) {
if (cyc.size() % 2) {
for (int p : cyc) {
raw_numbers[p] =
make_pair(raw_numbers[p].second, raw_numbers[p].first);
}
break;
}
}
}
for (int i = 0; i < n; i++) {
cout << raw_numbers[i].first << ' ';
}
cout << endl;
}
void player_1(int n) {
cout << "First" << endl;
for (int j = 0; j < 2; j++) {
for (int i = 1; i < n + 1; i++) {
cout << i << ' ';
}
}
cout << '\n';
}
void run() {
int n;
cin >> n;
if (n % 2) {
player_2(n);
} else {
player_1(n);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
run();
}
| 2,800 | CPP |
n = int(input())
c = 0
while(n > 0):
if(n > 5):
n = n - 5
c += 1
else:
if((n % 2 == 0) or (n % 3 == 0) or (n % 4 == 0) or (n % 5 == 0)):
c += 1
n = 0
else:
c += 1
n = 0
print(c) | 800 | PYTHON3 |
import sys
I = sys.stdin.buffer.readline
for i in range(int(I())):
I(); print(len(set(I().split()))) | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxx = 1e18;
long long nmax;
char s1[100000] =
" What are you doing at the end of the world? Are you busy? Will you save "
"us?";
char s2[100000] =
" What are you doing while sending \"\"? Are you busy? Will you send \"\"?";
long long f[111000];
char gg(long long x, long long y) {
if (x == 0) return s1[y];
if (y <= 34) return s2[y];
if (y <= 34 + f[x - 1] || x > nmax) return gg(x - 1, y - 34);
if (y <= 34 + f[x - 1] + 32) return s2[y - f[x - 1]];
if (y <= 34 + 2 * f[x - 1] + 32) return gg(x - 1, y - 34 - f[x - 1] - 32);
return s2[y - f[x - 1] - f[x - 1]];
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long q, x, y, i;
f[0] = 75;
for (i = 1; i <= 100; i++) {
f[i] = 2 * f[i - 1] + 68;
if (f[i] > maxx) break;
}
nmax = i;
cin >> q;
for (i = 1; i <= q; i++) {
cin >> x >> y;
if (y > f[x] && x <= nmax) {
cout << ".";
continue;
}
cout << gg(x, y);
}
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[1010], b[1010], c[1010], d[1010];
int i, j, n, sum;
while (~scanf("%d", &n)) {
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (i = 1; i <= n; i++) {
scanf("%d", &b[i]);
}
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
sum = a[1] + b[1];
for (i = 1; i <= n; i++) {
c[i] = a[i];
d[i] = b[i];
if (sum < c[i] + d[i]) {
sum = c[i] + d[i];
}
for (j = i + 1; j <= n; j++) {
c[j] = c[j - 1] | a[j];
d[j] = d[j - 1] | b[j];
if (sum < c[j] + d[j]) {
sum = c[j] + d[j];
}
}
}
printf("%d\n", sum);
}
return 0;
}
| 900 | CPP |
n, b = map(int, input().split())
def factors(n):
i = 2
wyn = []
while i*i <= n:
kro = 0
while n % i == 0:
kro += 1
n //= i
if kro > 0:
wyn.append((i, kro))
i += 1
return wyn + ([(n, 1)] if n > 1 else [])
def cnt(p):
(x, kro) = p
wyn = 0
pom = x
while pom <= n:
wyn += n // pom
pom *= x
return wyn // kro
print(min(map(cnt, factors(b))))
| 1,700 | PYTHON3 |
t= int(input())
i=1
for i in range(t):
n,k = map(int, input().split())
for d in range(2,n+1):
if(n%d == 0):
break
print(n + d + (k-1)*2) | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, t;
string s;
void cascade() {
for (int i = (int)s.size() - 1; i > 0; i--) {
if (s[i] == '.') continue;
if (s[i] > '9') {
s[i] = '0';
if (s[i - 1] == '.')
s[i - 2]++;
else
s[i - 1]++;
}
}
if (s[0] > '9') {
s[0] = '0';
s = string("1") + s;
}
}
void rem_zero() {
int i = (int)s.size() - 1;
while (s[i] == '0') i--;
if (s[i] == '.') i--;
s = s.substr(0, i + 1);
}
int main() {
cin >> n >> t >> s;
auto it = s.begin();
while (*it != '.') ++it;
++it;
while (it != s.end() && *it < '5') ++it;
if (it == s.end()) {
cout << s << endl;
exit(0);
}
while (t-- && *it >= '4' && *it != '.') --it;
for (auto jt = it + 1; jt != s.end(); ++jt) {
*jt = '0';
}
if (*it == '.') --it;
(*it)++;
cascade();
rem_zero();
cout << s << endl;
}
| 1,700 | CPP |
import math
t=int(input())
for _ in range(t):
n=int(input())
print(n//2) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline void fre1() { freopen("input.txt", "r", stdin); }
inline void fre2() { fclose(stdin); }
const int MAXN = +5;
const double EPS = 1e-8;
int main() {
long long d, k, a, b, t;
scanf("%I64d%I64d%I64d%I64d%I64d", &d, &k, &a, &b, &t);
if (d <= k) {
printf("%I64d\n", d * a);
return 0;
}
d -= k;
long long ans = k * a;
long long ti = d / k + (d % k == 0 ? 0 : 1);
long long last = d - (ti - 1) * k;
ans += min(last * a + t, last * b);
d -= last;
--ti;
ans += ti * min(a * k + t, b * k);
printf("%I64d\n", ans);
}
| 1,900 | CPP |
x = int(input())
l = []
c=0
for i in range(x):
y = input()
l.append(y)
for i in l:
if i == '++X' or i=='X++':
c+=1
else:
c-=1
print(c)
| 800 | PYTHON3 |
n= (input())
if int(n)%4==0 or int(n)%7==0 or int(n)%47==0:
print("YES")
else:
count =0
for i in '01235689':
if i in n:
count+=1
if count > 0:
print("NO")
else:
print("YES")
| 1,000 | PYTHON3 |
import random
n = int(input())
d = {}
for _ in range(n-1):
u, v = map(int, input().split())
if u not in d:
d[u] = [v]
else:
d[u].append(v)
if v not in d:
d[v] = [u]
else:
d[v].append(u)
c = [0]+list(map(int, input().split()))
start = random.randint(1, n)
stack = [(start, start)]
picked = -1
parent = []
visited = set()
while stack:
p, parent = stack.pop()
visited.add(p)
if c[p] != c[parent] and parent != picked:
if picked != -1:
print('NO')
break
else:
picked = p
for x in d[p]:
if x not in visited:
stack.append((x, p))
else:
print('YES')
if picked == -1:
picked = 1
print(picked) | 1,600 | PYTHON3 |
x = input()
y = input()
for i in range(len(x)):
if ord(x[i])<ord(y[i]):
print(-1)
exit(0)
print(y) | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void yala_bena() {
cin.tie(0);
std::ios::sync_with_stdio(0);
}
const int m_r = 2000006;
double pii = 3.1415926536;
int main() {
yala_bena();
string s[9];
for (long long i = 1; i <= 8; ++i) cin >> s[i];
long long ma = 100;
long long mb = 100, j, i;
for (j = 0; j < 8; j++) {
for (i = 1; i <= 8; i++) {
if (s[i][j] == 'W') {
ma = min(ma, i - 1);
break;
} else if (s[i][j] == 'B') {
break;
}
}
for (long long i = 8; i > 0; i--) {
if (s[i][j] == 'B') {
mb = min(mb, 8 - i);
break;
} else if (s[i][j] == 'W')
break;
}
}
if (ma <= mb)
cout << "A" << endl;
else
cout << "B" << endl;
}
| 1,200 | CPP |
for x in range(int(input())):
liczba = int(input())
if liczba % 4 == 0:
print("YES")
else:
print("NO") | 800 | PYTHON3 |
from sys import stdin
input=stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
s=input()
t=set()
y=0
x=0
dc={0:0}
m=10**18
ans=(-1,-1)
for i in range(n):
if s[i]=="L":
x-=1
elif s[i]=="R":
x+=1
elif s[i]=="U":
y+=1
else:
y-=1
if y*10**9+x in dc:
if m>i+1-dc[y*10**9+x]:
m=i+1-dc[y*10**9+x]
ans=(dc[y*10**9+x]+1,i+1)
dc[y*10**9+x]=i+1
if m==10**18:
print(-1)
else:
print(*ans) | 1,500 | PYTHON3 |
import sys, threading
sys.setrecursionlimit(10 ** 6)
scan = lambda: map(int, input().split())
string = input()
a, ab, aba = 0, 0, 0
size = 0
for i in string:
size +=1
if i == 'a':
a += 1
aba = max(aba+1, a, ab+1)
else:
ab = max(ab+1, a+1)
print(max(a, ab, aba))
| 1,500 | PYTHON3 |
from collections import *
n=int(input())
l=[*map(int,input().split())]
dic=Counter(l)
dic=OrderedDict(sorted(dic.items(),key=lambda x:x[0],reverse=True))
#print(dic)
s=set()
for i in dic:
if(dic[i]==1):
if(i+1 not in s):
s.add(i+1)
else:
if(i not in s):
s.add(i)
elif(i-1>0 and i-1 not in s):
s.add(i-1)
elif(dic[i]==2):
if(i in s):
if(i-1>0 and i-1 not in s):
s.add(i-1)
s.add(i+1)
else:
s.add(i)
if(i+1 not in s):
s.add(i+1)
elif(i-1>0):
s.add(i-1)
else:
s.add(i)
s.add(i+1)
if(i-1>0):
s.add(i-1)
#print(s,i,dic[i])
print(len(s))
#print(s) | 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct fastio {
char s[100000];
int it, len;
fastio() { it = len = 0; }
inline char get() {
if (it < len) return s[it++];
it = 0;
len = fread(s, 1, 100000, stdin);
if (len == 0)
return EOF;
else
return s[it++];
}
bool notend() {
char c = get();
while (c == ' ' || c == '\n') c = get();
if (it > 0) it--;
return c != EOF;
}
} _buff;
inline long long getnum() {
long long r = 0;
bool ng = 0;
char c;
c = _buff.get();
while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
if (c == '-') ng = 1, c = _buff.get();
while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
if (x < 0) putchar('-'), x = -x;
register short a[20] = {}, sz = 0;
while (x) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
char c = _buff.get();
while (c <= 32) c = _buff.get();
return c;
}
long long qpow(long long x, long long k) {
return k == 0 ? 1
: 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;
}
const int maxn = 11011;
const int maxm = 6011;
const int maxc = 211;
int head[maxn], deg[maxn], nxt[maxm * 2], to[maxm * 2], tot = 1;
vector<pair<int, int> > con[maxn];
void add_edge(int x, int y, int id) {
nxt[++tot] = head[x];
head[x] = tot;
to[tot] = y;
deg[x]++;
swap(x, y);
nxt[++tot] = head[x];
head[x] = tot;
to[tot] = y;
deg[x]++;
}
int match[maxn][maxc];
int n, n1, n2, m, k;
int ans[maxm], lst[maxn];
int find_color(int x) {
for (int i = 1; i <= k; i++)
if (!match[x][i]) return i;
assert(false);
}
void aug(int e, int c, int pc) {
int x = to[e ^ 1], y = to[e];
if (match[y][c])
aug(match[y][c], pc, c);
else
match[y][pc] = 0;
match[x][c] = e;
match[y][c] = e ^ 1;
ans[e >> 1] = c;
}
void solve() {
for (int x = 1; x <= n; x++) {
for (int e = head[x]; e; e = nxt[e]) {
int y = to[e];
if (ans[e >> 1]) continue;
int c = find_color(x);
int pc = find_color(y);
aug(e, c, pc);
}
}
}
int main() {
n1 = getnum(), n2 = getnum();
m = getnum(), k = getnum();
n = n1 + n2;
for (int i = 1; i <= n; i++) lst[i] = i;
for (int i = 1; i <= m; i++) {
int x, y;
x = getnum(), y = getnum();
y += n1;
add_edge(lst[x], lst[y], i);
if (deg[lst[x]] >= k) lst[x] = ++n;
if (deg[lst[y]] >= k) lst[y] = ++n;
}
solve();
int cnt = 0;
for (int i = 1; i <= n1 + n2; i++) cnt += deg[lst[i]] > 0;
putnum(cnt), putchar('\n');
for (int i = 1; i <= m; i++) putnum(ans[i]), putchar(' ');
return 0;
}
| 3,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
i >> p.first >> p.second;
return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
for (auto& v : t) {
i >> v;
}
return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
o << p.first << ' ' << p.second;
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
if (t.empty()) o << '\n';
for (size_t i = 0; i < t.size(); ++i) {
o << t[i] << " \n"[i == t.size() - 1];
}
return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
unsigned int logceil(long long first) {
return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0;
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
hash<T> t;
hash<U> u;
size_t operator()(const pair<T, U>& p) const {
return t(p.first) ^ (u(p.second) << 7);
}
};
} // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
l = m + 1;
r = m;
} else {
h = m - 1;
}
}
return r;
}
template <typename F>
double bshd(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
l = m;
} else {
h = m;
}
}
return (l + h) / 2;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
h = m - 1;
r = m;
} else {
l = m + 1;
}
}
return r;
}
template <typename F>
double bsld(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
h = m;
} else {
l = m;
}
}
return (l + h) / 2;
}
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
public:
vector2() {}
vector2(size_t a, size_t b, T t = T())
: vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector2<T>> {
public:
vector3() {}
vector3(size_t a, size_t b, size_t c, T t = T())
: vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T>
class vector4 : public vector<vector3<T>> {
public:
vector4() {}
vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
: vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T>
class vector5 : public vector<vector4<T>> {
public:
vector5() {}
vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
: vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};
class A2AddOnATreeRevolution {
public:
int N, L;
vector<vector<std::pair<int, int>>> E;
vector<vector<int>> Ans;
map<std::pair<int, int>, int> X;
bool fail;
void put(int a, int b, int c) { X[{min(a, b), max(a, b)}] += c; }
int get(int a, int b) { return X[{min(a, b), max(a, b)}]; }
void add(int a, int b, int first) {
add(a, -1, b, first);
Ans.push_back({a + 1, b + 1, first});
}
bool add(int u, int p, int b, int first) {
if (u == b) return true;
for (std::pair<int, int> v : E[u]) {
if (v.first != p) {
put(u, v.first, first);
if (add(v.first, u, b, first)) return true;
put(u, v.first, -first);
}
}
return false;
}
int leaf(int u, int p) {
if (E[u].size() == 1) {
return u;
}
for (std::pair<int, int> v : E[u]) {
if (v.first != p) return leaf(v.first, u);
}
}
void process(int u, int v, int val) {
if (val == 0) return;
vector<int> Children;
for (std::pair<int, int> w : E[v])
if (w.first != u) Children.push_back(w.first);
if (Children.size() == 0) {
add(L, v, val);
} else if (Children.size() == 1) {
fail = true;
} else {
if (val % 2 == 1) {
fail = true;
} else {
int l1 = leaf(Children[0], v);
int l2 = leaf(Children[1], v);
add(L, l1, val / 2);
add(L, l2, val / 2);
add(l1, l2, -val / 2);
}
}
}
void dfs(int u, int p) {
for (std::pair<int, int> v : E[u]) {
if (v.first != p) {
dfs(v.first, u);
process(u, v.first, v.second - get(u, v.first));
}
}
}
void solve(istream& cin, ostream& cout) {
cin >> N;
E.resize(N);
for (int i = 0; i < N - 1; ++i) {
int u, v, val;
cin >> u >> v >> val;
--u;
--v;
E[u].push_back({v, val});
E[v].push_back({u, val});
}
L = 0;
while (E[L].size() > 1) L++;
fail = false;
dfs(L, -1);
if (fail) {
cout << "NO\n";
} else {
cout << "YES\n" << Ans.size() << '\n';
for (auto ans : Ans) cout << ans;
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
A2AddOnATreeRevolution solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| 2,500 | CPP |
n, m = map(int, input().split())
output = 0
for i in range(n):
win = list(map(int, input().split()))
for j in range(0, 2 * m, 2):
if win[j] + win[j + 1] > 0:
output += 1
print(output) | 800 | PYTHON3 |
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 5 20:07:42 2020
@author: DELL
"""
n=int(input())
m=list(map(int,input().split()))
d=0
o=False
import math
y=((m[-1])-(m[0]))
y=math.fabs(y)
for i in range(n):
if i!=n-1:
u=m[i+1]-m[i]
a=math.fabs(u)
if a<y:
o=True
d=i
y=a
if o:
print(d+1,end=' ')
print(d+2)
else:
print(n,end=' ')
print(1)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<long> a(10005, 0);
int main() {
int t;
cin >> t;
while (t--) {
int a, b, c, ans = 0;
cin >> a >> b >> c;
while (c > 1 && b > 0) {
c -= 2;
b--;
ans += 3;
}
while (a > 0 && b > 1) {
b -= 2;
a -= 1;
ans += 3;
}
cout << ans << endl;
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int check[260][260];
char a[110];
int main() {
int n;
scanf("%d", &n);
scanf("%s", a + 1);
for (int i = 1; i < n; i++) {
check[a[i] - 'A'][a[i + 1] - 'A']++;
}
int maxx = 0;
int ansi, ansj;
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
if (maxx < check[i][j]) {
maxx = check[i][j];
ansi = i;
ansj = j;
}
}
}
printf("%c%c", ansi + 'A', ansj + 'A');
return 0;
}
| 900 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
using namespace std;
bool isvowel(char c) {
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
return 1;
return 0;
}
const double eps = 0.000001;
const long double pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const long long MOD = 1e18 + 9;
const long long INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 3e5 + 123;
const int PRI = 555557;
const int pri = 997;
const int Magic = 2101;
int n, m, k;
int a[N];
int Sz = 1;
int p[N];
map<int, pair<int, int> > dp;
inline void Solve() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
int mx, pos;
mx = pos = 0;
for (int i = 1; i <= n; i++) {
pair<int, int> cur = dp[a[i] - 1];
p[i] = cur.second;
cur.first++;
dp[a[i]] = make_pair(cur.first, i);
if (cur.first > mx) pos = i, mx = cur.first;
}
vector<int> ans;
while (pos) {
ans.push_back(pos);
pos = p[pos];
}
reverse(ans.begin(), ans.end());
printf("%d ", ans.size());
for (auto it : ans) printf("%d ", it);
}
int main() {
int tests = 1;
while (tests--) {
Solve();
}
return 0;
}
| 1,700 | CPP |
R = lambda: map(int, input().split())
n = int(input())
cnts = [0] * 50
arr = [8,15,16,23,42,4]
for x in R():
if x == 4:
cnts[x] += 1
elif x in arr:
idx = arr.index(x)
cnts[arr[idx]] = min(cnts[arr[idx]] + 1, cnts[arr[idx - 1]])
print(n - 6 * cnts[42]) | 1,300 | PYTHON3 |
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
try:
fake=max([l[x+1]-l[x] for x in range(len(l)-1)])
print (max((fake/2),max(l[0],k-l[-1])))
except:
print (max(l[0],k-l[0]))
| 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ans;
vector<int> power2;
vector<vector<int>> graph;
bool check(vector<vector<int>> &graph) {
for (vector<int> vi : graph) {
if (vi.size() > 4) return false;
}
return true;
}
void dfs(int node, int x, int y, int parent, int depth, int lastD) {
ans[node] = pair<int, int>(x, y);
int direction = 0;
for (int to : graph[node]) {
if (to == parent) continue;
if (direction == lastD) direction++;
if (direction == 0) {
dfs(to, x, y + power2[depth], node, depth - 1, 1);
direction++;
} else if (direction == 1) {
dfs(to, x, y - power2[depth], node, depth - 1, 0);
direction++;
} else if (direction == 2) {
dfs(to, x - power2[depth], y, node, depth - 1, 3);
direction++;
} else if (direction == 3) {
dfs(to, x + power2[depth], y, node, depth - 1, 2);
direction++;
}
}
}
int main() {
int n;
cin >> n;
graph = vector<vector<int>>(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
ans = vector<pair<int, int>>(n);
power2 = vector<int>(31);
power2[0] = 1;
for (int i = 1; i < power2.size(); i++) {
power2[i] = 2 * power2[i - 1];
}
if (check(graph)) {
cout << "YES" << endl;
dfs(0, 0, 0, -1, 30, -1);
for (int i = 0; i < n; i++) {
cout << ans[i].first << " " << ans[i].second << endl;
}
} else
cout << "NO" << endl;
}
| 2,000 | CPP |
for _ in " "*int(input()):
n=int(input())
print(n//2+1) | 800 | PYTHON3 |
t = int(input())
for _ in range(t):
n = int(input())
s = input()
ctr = 0
for i in range(n-1):
if s[i] == s[i+1]: ctr += 1
print((ctr+1) // 2) | 1,200 | PYTHON3 |
Subsets and Splits