solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct block {
int l, r, pre, nex, cnt, f[N];
} bl[320];
int a[N], n, num, dd[N];
int solve(int k) {
int res = 1, pre = 1, cnt = 0;
memset(dd, 0, sizeof dd);
for (int i = 1; i <= n; i++) {
if (dd[a[i]])
dd[a[i]]++;
else if (cnt < k) {
dd[a[i]]++;
cnt++;
} else {
for (int j = pre; j <= i - 1; j++) dd[a[j]] = 0;
pre = i;
res++;
dd[a[i]] = cnt = 1;
}
}
return res;
}
void init(int k) {
num = bl[1].l = 1;
for (int i = 1; i <= n; i++) {
if (bl[num].f[a[i]])
bl[num].f[a[i]]++;
else if (bl[num].cnt < k) {
bl[num].f[a[i]]++;
bl[num].cnt++;
} else {
bl[num++].r = i - 1;
bl[num].l = i;
bl[num].f[a[i]] = bl[num].cnt = 1;
}
}
bl[num].r = n;
for (int i = 1; i <= num; i++) {
bl[i].pre = i - 1;
bl[i].nex = i + 1;
}
}
int bl_solve(int k) {
int res = 1;
for (int i = bl[1].nex; i <= num; i = bl[i].nex) {
int pre = bl[i].pre, nex = bl[i].nex;
for (int j = bl[i].l; j <= bl[i].r + 1; j++) {
if (j <= bl[i].r && (bl[pre].f[a[j]] || bl[pre].cnt < k)) {
if (!bl[pre].f[a[j]]) bl[pre].cnt++;
bl[pre].f[a[j]]++;
bl[i].f[a[j]]--;
if (!bl[i].f[a[j]]) bl[i].cnt--;
bl[pre].r = j;
} else {
bl[i].l = j;
break;
}
}
if (bl[i].l <= bl[i].r)
res++;
else {
bl[pre].nex = nex;
bl[nex].pre = pre;
}
}
return res;
}
int main() {
ios ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= min(n, 317); i++) cout << solve(i) << ' ';
if (n <= 317) return 0;
init(317);
for (int i = 318; i <= n; i++) cout << bl_solve(i) << ' ';
}
| 2,400 | CPP |
def isDistinct(n):
n=str(n)
n=list(map(int,n))
m=set(n)
if len(n)==len(m):
return True
else:
return False
n=int(input())
i=n+1
while (not isDistinct(i)):
i+=1
print(i)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int const maxn = 2e5 + 10;
int n, a[maxn];
int main() {
scanf("%d", &n);
int cnt[maxn], cmx = 0, cmx_a;
vector<int> id;
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
cnt[a[i]]++;
}
for (int i = 1; i <= n; i++)
if (cnt[a[i]] > cmx) {
cmx = cnt[a[i]];
cmx_a = a[i];
}
for (int i = 1; i <= n; i++)
if (a[i] == cmx_a) id.push_back(i);
printf("%d\n", n - cmx);
int L = 1;
for (auto &i : id) {
for (int j = i - 1; j >= L; j--)
printf("%d %d %d\n", (a[j] > a[i]) + 1, j, j + 1);
L = i + 1;
}
for (int j = L; j <= n; j++)
printf("%d %d %d\n", (a[j] > a[L - 1]) + 1, j, j - 1);
return 0;
}
| 1,400 | CPP |
#vk 2012
n=int(input())
a,b,c,d=map(input().split().count,['1','2','3','4'])
#print(a,b,c,d)
#print(a+b*2+c*3+d*4)
print((max(a-c,0)+b*2+3)//4+c+d) | 1,100 | PYTHON3 |
s1=input()
s2=input()
s3=input()
a=[0 for i in range(27)]
b=[0 for i in range(27)]
for i in range(len(s1)):
a[ord(s1[i])-ord('A')+1]+=1
for i in range(len(s2)):
a[ord(s2[i])-ord('A')+1]+=1
for i in range(len(s3)):
b[ord(s3[i])-ord('A')+1]+=1
bool=0
for i in range(27):
if a[i]!=b[i]:
bool=1
if bool==1:
print('NO')
else:
print('YES')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.length();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
dp[i][i] = 1;
if (i + 1 < n) {
dp[i][i + 1] = (s[i] == s[i + 1]) ? 2 : 0;
}
}
for (int len = 3; len <= n; ++len) {
for (int i = 0; i + len - 1 < n; ++i) {
int j = i + len - 1;
if (dp[i + 1][j - 1] > 0 && s[i] == s[j]) {
int m = (i + j + 1) / 2 - 1;
dp[i][j] = dp[i][m] + 1;
}
}
}
vector<int> cnt(n + 1);
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
++cnt[dp[i][j]];
}
}
int cur = 0;
vector<int> ans(n);
for (int i = n; i >= 1; --i) {
cur += cnt[i];
ans[i - 1] = cur;
}
for (int x : ans) {
printf("%d ", x);
}
puts("");
}
| 1,900 | CPP |
from functools import reduce
numberColumns, numberSquares = map(int, input().split())
totalSquareInColumn = [0 for i in range(numberColumns)]
squareColumnTargets = map(int, input().split())
squareColumnTargets = map(lambda x: x-1, squareColumnTargets)
for target in squareColumnTargets:
totalSquareInColumn[target] += 1
deletedColumns = min(totalSquareInColumn)
print(deletedColumns) | 900 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60000000")
using namespace std;
const long double PI = acos((long double)0) * 2;
struct pt {
long double x, y;
pt() : x(0), y(0) {}
pt(long double x, long double y) : x(x), y(y) {}
pt operator+(const pt& p) const { return pt(x + p.x, y + p.y); }
pt operator-(const pt& p) const { return pt(x - p.x, y - p.y); }
pt operator*(long double p) const { return pt(x * p, y * p); }
};
inline long double dist2(const pt& a, const pt& b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
inline long double dist(const pt& a, const pt& b) { return sqrt(dist2(a, b)); }
struct circle : pt {
long double r;
circle(long double x, long double y, long double r) : pt(x, y), r(r) {}
};
struct line {
long double a, b, c;
line() {}
line(const pt& p, const pt& q) {
a = p.y - q.y;
b = q.x - p.x;
c = -a * p.x - b * p.y;
norm();
}
void norm() {
long double z = sqrt(a * a + b * b);
a /= z, b /= z, c /= z;
}
pt covector() { return pt(-b, a); }
};
pt inverse(const pt& p) {
long double d = dist2(pt(0, 0), p);
return p * (1. / d);
}
circle inverse_to_circle(const circle& c) {
pt p1 = inverse(c + pt(-c.r, 0));
pt p2 = inverse(c + pt(c.r, 0));
pt cntr = (p1 + p2) * 0.5;
return circle(cntr.x, cntr.y, dist(p1, p2) / 2);
}
line inverse_to_line(const circle& c) {
pt p1 = inverse(c + pt(0, c.r));
pt p2 = inverse(c - pt(0, c.r));
return line(p1, p2);
}
pt intersect(const line& m, const line& n) {
long double zn = (m.a * n.b - m.b * n.a);
return pt(-(m.c * n.b - m.b * n.c) / zn, -(m.a * n.c - m.c * n.a) / zn);
}
long double ang(const pt& a, const pt& b) {
long double x = atan2(a.y, a.x);
long double y = atan2(b.y, b.x);
long double res = abs(x - y);
return min(res, 2 * PI - res);
}
long double get_new_radius(const circle& c) {
return c.r / (c.x * c.x + c.y * c.y - c.r * c.r);
}
int R, r, k;
void read() { scanf("%d%d%d", &R, &r, &k); }
void solve() {
circle big(R, 0, R);
circle small(r, 0, r);
circle med(R + r, 0, R - r);
line nbig = inverse_to_line(big);
line nsmall = inverse_to_line(small);
circle nmed = inverse_to_circle(med);
circle ans = nmed;
ans.y += 2 * nmed.r * k;
long double res = get_new_radius(ans);
cout.precision(20);
cout.setf(ios::fixed);
cout << res << '\n';
}
int main() {
int ts;
cin >> ts;
for (int tt = 0; tt < int(ts); ++tt) {
read();
solve();
}
}
| 2,800 | CPP |
s=input().split()
n=int(s[0])
a=int(s[1])
b=int(s[2])
a=(a+b)%n
if a==0:
a=n
print(a) | 1,000 | PYTHON3 |
g={'purple':'Power','green':'Time','orange':'Soul',"blue":"Space","red":"Reality",'yellow':"Mind"}
n=int(input().strip())
print(6-n)
c=[]
for _ in range(n):
c.append(input().strip())
gems=[]
for i in g:
if i not in c:
gems.append(g[i])
for k in gems:
print(k) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int r1, c1, r2, c2;
int main() {
cin >> r1 >> c1 >> r2 >> c2;
(r2 == r1 || c1 == c2) ? cout << 1 << " " : cout << 2 << " ";
if ((r1 + c1) % 2 != (r2 + c2) % 2)
cout << 0;
else {
((r1 + c1 == r2 + c2) || (r1 - c1 == r2 - c2)) ? cout << 1 : cout << 2;
}
cout << " ";
cout << max(abs(r1 - r2), abs(c1 - c2)) << " "
<< "\n";
}
| 1,100 | CPP |
import copy
for _ in range(int(input())) :
n,m = map(int,input().split())
l=[int(x) for x in input().split()]
a=[int(z) for z in input().split()]
a.sort()
start = a[0]-1
x=[]
for i in range (m-1) :
if a[i] != a[i+1]-1 :
for p in range (start,a[i]+1) :
for q in range (p+1,a[i]+1) :
if l[p] > l[q] :
l[p],l[q] = l[q],l[p]
start = a[i+1] -1
for p in range (start,min(a[m-1]+1,n)) :
for q in range (p+1,min(a[m-1]+1,n)) :
if l[p] > l[q] :
l[p],l[q] = l[q],l[p]
xx = copy.deepcopy(l)
xx.sort()
if l == xx :
print("YES")
else:
print("NO") | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, nq;
cin >> n >> nq;
set<int> p;
multiset<int> d;
auto add = [&](int x) {
auto it = p.insert(x).first;
auto nt = next(it);
if (it == p.begin()) {
if (nt != p.end()) d.insert(*nt - x);
} else if (nt == p.end()) {
d.insert(x - *prev(it));
} else {
auto lt = prev(it);
d.erase(d.find(*nt - *lt));
d.insert(x - *lt);
d.insert(*nt - x);
}
};
auto del = [&](int x) {
auto it = p.erase(p.find(x));
if (it == p.end()) {
if (p.rbegin() != p.rend()) d.erase(d.find(x - *p.rbegin()));
} else if (it == p.begin()) {
d.erase(d.find(*it - x));
} else {
int l = *prev(it), r = *it;
d.erase(d.find(r - x));
d.erase(d.find(x - l));
d.insert(r - l);
}
};
for (int i = 0, x; i < n; i++) {
cin >> x;
add(x);
}
auto solve = [&]() {
if (p.size() <= 2) return 0;
return *p.rbegin() - *p.begin() - *d.rbegin();
};
cout << solve() << '\n';
for (int t, x, dx; nq--;) {
cin >> t >> x;
if (t)
add(x);
else
del(x);
cout << solve() << '\n';
}
return 0;
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1 << 10 | 1;
const long long M = 19;
long long m[M][M];
long long f[N][N];
long long n, Edge, k;
long long cnt[N];
long long read() {
long long f = 1, x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 1) + (x << 3) + (s ^ '0');
s = getchar();
}
return f * x;
}
void Prepare(long long i) {
long long x = i;
while (x) {
if (x & 1) cnt[i]++;
x >>= 1;
}
}
long long mx;
void DP() {
for (long long i = 1; i < mx; i++)
for (long long j = i; j; j = (j - 1) & i)
if (f[i][j]) {
for (long long k = 1; k <= n; k++) {
if (i & (1 << (k - 1)))
for (long long v = 1; v <= n; v++)
if (m[k][v] and !(i & (1 << (v - 1)))) {
long long l = 0;
if (cnt[i] == 1)
l = i | (1 << (v - 1));
else {
l = (j & (~(1 << (k - 1))));
l |= (1 << (v - 1));
}
if (!(l >> v)) f[i | (1 << (v - 1))][l] += f[i][j];
}
}
}
}
long long ans;
signed main() {
n = read();
Edge = read();
k = read();
mx = (1 << n);
for (long long i = 1; i <= Edge; i++) {
long long u = read();
long long v = read();
m[u][v] = 1;
m[v][u] = 1;
}
for (long long i = 1; i < mx; i <<= 1) f[i][i] = 1;
for (long long i = 0; i < mx; i++) Prepare(i);
DP();
for (long long i = 0; i < mx; i++)
if (cnt[i] == k) ans += f[mx - 1][i];
cout << ans << endl;
return 0;
}
| 2,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < 100; i++) {
s += '-';
}
bool ok = false;
for (int i = 0; i < n; i++) {
if (s[i] == '*') {
for (int j = i + 1; j < n; j++) {
int dist = j - i;
if (s[j] == '*' && s[j + dist] == '*' && s[j + 2 * dist] == '*' &&
s[j + 3 * dist] == '*') {
ok = true;
}
}
}
}
if (ok)
cout << "yes\n";
else
cout << "no\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 1,300 | CPP |
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
if i<n-1:
b.append(abs(a[i]-a[i+1]))
else:
b.append(abs(a[n-1]-a[0]))
c=b.index(min(b))
if c==n-1:
print(n,end=' ')
print('1')
else:
print(c+1,end=' ')
print(c+2) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int DX[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int DY[8] = {0, 0, 1, -1, 1, -1, 1, -1};
const int intmax = 0x7fffffff;
const int mod = 1000000007;
template <class DT>
DT gcd(DT a, DT b) {
if (b) return gcd(b, a % b);
return a;
}
template <class DT>
void exgcd(DT a, DT b, DT &d, DT &x, DT &y) {
if (b)
exgcd(b, a % b, d, y, x), y -= x * (a / b);
else
d = a, x = 1, y = 0;
}
template <class DT>
inline DT inv(DT a, DT m) {
DT x, y, d;
exgcd(a, m, d, x, y);
if (d == 1) return (x + m) % m;
return -1;
}
template <class DT1, class DT2>
inline DT2 Mod(DT1 a, DT2 mod) {
if (a < 0) return (a % mod + mod) % mod;
return a % mod;
}
template <class DT, class DT1, class DT2>
inline DT mulm(DT1 a, DT2 b, DT mod) {
DT ta(Mod(a, mod)), tb(Mod(b, mod));
if (ta <= intmax && tb <= intmax) return 1ll * ta * tb % mod;
DT ret(0);
if (ta < tb) swap(ta, tb);
for (; tb; tb >>= 1) {
if (tb & 1) ret = (ret + ta) % mod;
ta = (ta + ta) % mod;
}
return ret;
}
template <class DT>
inline DT mulm(int a, int b, DT mod) {
return Mod(1ll * a * b, mod);
}
template <class DT, class DT1, class DT2>
inline DT expm(DT1 a, DT2 b, DT mod) {
assert(b >= 0);
DT ret(1), ta(Mod(a, mod));
for (; b; b >>= 1) {
if (b & 1) ret = mulm(ret, ta, mod);
ta = mulm(ta, ta, mod);
}
return ret;
}
char s1[1005], s2[1005], ns[1005];
int n;
map<string, int> mp;
map<string, int>::iterator mit;
int main() {
scanf("%s%s%d", s1, s2, &n);
int l = strlen(s1);
int ans = 1;
for (int i = 1; i <= n; i++) {
if (i & 1)
ans = (1ll * ans * (l - 1) - (l - 2) + mod) % mod;
else
ans = (1ll * ans * (l - 1)) % mod;
}
if (strcmp(s1, s2) == 0) {
int find = 0;
for (int i = 1; i < l; i++) {
for (int j = i; j < i + l; j++) ns[j - i] = s1[j % l];
if (strcmp(ns, s2) == 0) find++;
}
if (n & 1)
printf("%d\n", (1ll * find * ans + ans - 1) % mod);
else
printf("%d\n", (1ll * find * (ans - 1) + ans) % mod);
} else {
int find = 0;
for (int i = 1; i < l; i++) {
for (int j = i; j < i + l; j++) ns[j - i] = s1[j % l];
if (strcmp(ns, s2) == 0) find++;
}
if (find) {
if (n & 1)
printf("%d\n", (1ll * ans * find) % mod);
else
printf("%d\n", 1ll * find * (ans - 1) % mod);
} else
puts("0");
}
}
| 1,700 | CPP |
for t in range(int(input())):
r, g, b, w = map(int, input().split())
if r + g + b + w <= 1:
print('YES')
else:
p = True
a = r % 2 + g % 2 + b % 2 + w % 2
if a <= 1:
print('YES')
elif a == 3 or a == 4:
if r and g and b:
print('YES')
else:
print('NO')
else:
s = min(r, g, b)
a = (r - s) % 2 + (g - s) % 2 + (b - s) % 2 + ((w % 2) ^ (s % 2))
if a <= 1:
print('YES')
else:
print('NO') | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
set<char> vow = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
long long int xpow(long long int n, long long int m) {
if (m == 0) {
return 1;
} else {
long long int x = xpow(n, m / 2);
if (m & 1) {
return x * x * n;
} else {
return x * x;
}
}
}
long long int xmod(long long int a, long long int m) {
return (((a % m) + m) % m);
}
long long int xmodpow(long long int n, long long int m, long long int y) {
if (m == 0) {
return 1;
} else {
long long int x = xmodpow(n, m / 2, y);
if (m & 1) {
return xmod((xmod((xmod(x, y) * xmod(x, y)), y) * xmod(n, y)), y);
} else {
return xmod((xmod(x, y) * xmod(x, y)), y);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
cin.ignore();
vector<long long int> a;
for (int i = 0; i < n; i++) {
long long int x;
cin >> x;
cin.ignore();
a.push_back(x);
};
long long int curr = 1;
while (curr < n) {
curr *= 2;
}
long long int ans = 0;
for (int i = 0; i < n - 1; i++) {
while (i + curr >= n) {
curr /= 2;
}
ans += a[i];
a[i + curr] += a[i];
cout << setprecision(20) << ans << '\n';
}
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int kkk;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
map<string, int> v;
int n;
string s;
cin >> n;
while (n--) {
cin >> s;
for (int i = 0; i < s.size(); i++) {
string a;
a += s[i];
v[a]++;
if (i < s.size() - 1) {
a += s[i + 1];
v[a]++;
}
}
}
char c;
for (int i = 0; i < 26; i++) {
c = 'a' + i;
string ss;
ss += c;
if (v[ss] == 0) {
cout << ss;
return 0;
}
}
for (int i = 0; i < 26; i++) {
c = 'a' + i;
string ss;
ss += c;
for (int k = 0; k < 26; k++) {
c = 'a' + k;
string a = ss;
a += c;
if (v[a] == 0) {
cout << a << endl;
return 0;
}
}
}
}
| 1,500 | CPP |
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
ip = lambda : int(stdin.readline())
inp = lambda: map(int,stdin.readline().split())
def solve(set, n, sum):
subset =([[False for i in range(sum + 1)]
for i in range(n + 1)])
for i in range(n + 1):
subset[i][0] = True
for i in range(1, sum + 1):
subset[0][i]= False
for i in range(1, n + 1):
for j in range(1, sum + 1):
if j<set[i-1]:
subset[i][j] = subset[i-1][j]
if j>= set[i-1]:
subset[i][j] = (subset[i-1][j] or subset[i - 1][j-set[i-1]])
return subset[n][sum]
t = ip()
for _ in range(t):
n = ip()
nc = n
n = n<<1
arr = list(inp())
idx = n-1
check = []
while idx != 0:
val = max(arr)
c = 0
for i in arr[::-1]:
if i == val:
c += 1
break
else:
c += 1
check.append(c)
idx = arr.index(val)
arr = arr[:idx]
length = len(check)
flag = solve(check,length,nc)
if flag:
print("YES")
else:
print("NO")
| 1,800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void SolveA() {
size_t q;
cin >> q;
for (size_t i = 0; i < q; ++i) {
size_t n;
cin >> n;
size_t res = 0;
if ((n % 2)) {
if (n < 9) {
cout << "-1\n";
continue;
}
n -= 9;
++res;
}
if (n % 4) {
if (n < 6) {
cout << "-1\n";
continue;
}
n -= 6;
++res;
}
res += (n / 4);
cout << res << "\n";
}
}
int main(int argc, char **argv) {
SolveA();
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int mod = (int)1e+9 + 7;
const double pi = acos(-1.);
const int maxn = 100100;
int n, k, a[maxn];
long long b[maxn];
bool check(long long m) {
int cnt = 0;
long long ost = 0;
for (int i = 0; i < n; i++) {
b[i] = a[i] - ost;
while (b[i] > 0) {
b[i] -= m - i - 1;
if (m - i - 1 < 0) {
return (0);
}
if (++cnt > k) {
return (0);
}
}
ost = max(((b[i]) < 0 ? -(b[i]) : (b[i])) - 1, 0ll);
}
return (1);
}
int main() {
srand(time(0));
const string file = "";
if (!file.empty()) {
freopen((file + ".in").c_str(), "r", stdin);
freopen((file + ".out").c_str(), "w", stdout);
}
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
long long l = 0, r = (long long)1e17;
while (l + 1 < r) {
long long m = (l + r) >> 1;
if (check(m)) {
r = m;
} else {
l = m;
}
}
printf("%I64d", l + 1);
return (0);
}
| 2,200 | CPP |
def twins(n):
a=things[0]
del things[0]
if a>sum(things):
print(1)
else:
j=1
while j!=0:
a+=things[0]
del things[0]
j+=1
if a>sum(things):
print(j)
j=0
n=int(input())
things = [int(n) for n in input().split()]
things = sorted(things)
things.reverse()
twins(n)
| 900 | PYTHON3 |
l=[]
for i in range(int(input())):
s=input()
print('YES' if s in l else 'NO')
l.append(s)
#trying again because it threw me a syntax error
| 800 | PYTHON3 |
n = int(input())
results = []
for i in range(n):
m = int(input())
results.append([i for i in range(1, m + 1)])
for result in results:
print(len(result))
for value in result:
print(value, end=' ')
print()
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long int n, x, y;
int sol(long long int l, long long int r, long long int a) {
if (l == r) return 3;
long long int mid = (l + r) / 2;
long long int shots = mid / y + mid / x;
if (shots == a) {
long long int first = y * (mid / y);
long long int second = x * (mid / x);
if (first > second) return 1;
if (first < second) return 2;
if (first == second) return 3;
}
if (shots < a)
return sol(mid + 1, r, a);
else
return sol(l, mid, a);
}
int main() {
scanf("%I64d %I64d %I64d", &n, &x, &y);
for (int i = 0; i < n; ++i) {
long long int a;
scanf("%I64d", &a);
int ans = sol(0, x * y, a % (x + y));
if (ans == 1) printf("Vanya\n");
if (ans == 2) printf("Vova\n");
if (ans == 3) printf("Both\n");
}
return 0;
}
| 1,800 | CPP |
n = int(input())
se2sf = 0
sf2se = 0
lst = None
for ch in input():
if lst is None:
lst = ch
else:
if lst=='S' and ch=='F':
se2sf+=1
if lst=='F' and ch=='S':
sf2se +=1
lst = ch
print('YES' if se2sf>sf2se else 'NO')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000;
int n, t[30][MAX + 1], color[30][MAX + 1];
map<char, int> M;
long long bin_searc(long long n) {
long long a = 1, b = 1.e8;
while (true) {
if (a >= b || b - a == 1) return b;
long long m = (a + b) / 2;
if (m * (m + 1) / 2 < n) {
a = m;
} else {
b = m;
}
}
return b;
}
int main() {
long long n;
cin >> n;
long long t = bin_searc(n);
if (t * (t + 1) / 2 > n) {
t -= 1;
}
n -= t * (t + 1) / 2;
if (n == 0)
cout << t << endl;
else
cout << n << endl;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char **argv) {
int n;
char l, c;
cin >> n;
vector<string> s(n);
string w, h;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
cin >> w;
cin >> l;
h = w;
for (int i = 0; i < n; i++) {
for (int j = 0; j < ((int)(w).size()) - ((int)(s[i]).size()) + 1; j++) {
bool fl = 1;
for (int k = 0; k < ((int)(s[i]).size()); k++) {
int d = (int)s[i][k] - (int)w[j + k];
if (d % ('A' - 'a')) fl = 0;
}
if (fl) {
for (int k = 0; k < ((int)(s[i]).size()); k++) {
c = l;
if (w[j + k] == l || w[j + k] == l + 'A' - 'a') {
c = 'a';
if (c == l) c++;
}
if (w[j + k] >= 'A' && w[j + k] <= 'Z')
h[j + k] = c + 'A' - 'a';
else
h[j + k] = c;
}
}
}
}
cout << h << endl;
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int head[N], nxt[N * 2], to[N * 2], cost[N * 2], dir[N * 2], ne = 0;
int f[N];
void addedge(int a, int b, int c, int d) {
nxt[ne] = head[a];
to[ne] = b;
cost[ne] = c;
dir[ne] = d;
head[a] = ne++;
}
int ans[N];
int main() {
int n, m;
scanf("%d %d", &n, &m);
memset(head, -1, sizeof head);
int a, b, c, d;
for (int i = 0; i < m; ++i) {
scanf("%d %d %d", &a, &b, &c);
addedge(a, b, c, 0);
addedge(b, a, c, 1);
f[a] += c;
f[b] += c;
}
memset(ans, -1, sizeof ans);
for (int i = 1; i <= n; ++i) f[i] /= 2;
queue<int> q;
q.push(1);
while (q.size()) {
int idx = q.front();
q.pop();
for (int i = head[idx]; ~i; i = nxt[i]) {
int id = i / 2;
if (ans[id] == -1) {
ans[id] = dir[i];
f[to[i]] -= cost[i];
if (to[i] != n && f[to[i]] == 0) q.push(to[i]);
}
}
}
for (int i = 0; i < m; ++i) {
printf("%d\n", ans[i]);
}
}
| 2,100 | CPP |
s = input()
unique = []
for char in s:
if char not in unique:
unique.append(char)
if len(unique) % 2 == 0:
print("CHAT WITH HER!")
else:
print("IGNORE HIM!")
| 800 | PYTHON3 |
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
x=0
y=0
for i in range(n):
if i%2==0 and l[i]%2==1:
x+=1
elif i%2==1 and l[i]%2==0:
y+=1
if x==y:
print(x)
else:
print(-1)
| 800 | PYTHON3 |
n=int(input())
if n%2==0:
print(4,int(n-4))
else:
print(9,int(n-9))
| 800 | PYTHON3 |
n=int(input())
a=list(map(int,input().split()))
b=range(1,n+1)
za=[x for x,y in sorted(zip(a,b))]
zb=[y for x,y in sorted(zip(a,b))]
s=input()
a=[]
j=0
for i in range(2*n):
if(s[i]=='0'):
a.append(zb[j])
print(zb[j],end=' ')
j+=1
else:
print(a.pop(),end=' ')
| 1,300 | PYTHON3 |
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (111)
int a[maxn][maxn], xor_[maxn << 1];
signed main()
{
int t; cin >> t; while(t--) {
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%lld", &a[i][j]);
memset(xor_, 0, sizeof(xor_));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
xor_[i + j] ^= a[i][j];
}
}
int ok = 1;
for(int i = 0; i < maxn * 2; i++)
if(xor_[i]) {ok = 0; break;}
cout << (ok? "Jeel": "Ashish") << endl;
}
return 0;
} | 2,700 | CPP |
import math
def main():
t = int(input())
for i in range(t):
n,k = map(int, input().split())
p = math.ceil(k / (n - 1)) - 1
print(k + p)
if __name__ == "__main__":
main()
| 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcount(s);
}
template <class T>
T gcd(T a, T b) {
T r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
vector<pair<int, int> > V[200005];
int n, m;
vector<int> RR[2], res;
int color[200005];
int que[200005], size;
bool cal(int id) {
memset(color, -1, sizeof(color));
RR[id].clear();
for (int i = (1); i <= (n); ++i)
if (color[i] == -1) {
size = 0;
que[size++] = i;
int mau = 0;
color[i] = 1;
for (int t = 0; t < (size); ++t) {
int u = que[t];
mau += color[u];
for (int j = 0; j < (((int)(V[u]).size())); ++j) {
int v = V[u][j].first;
int c = V[u][j].second;
if (color[v] != -1) {
if (color[u] ^ color[v] ^ id ^ c) {
return 0;
}
} else {
color[v] = color[u] ^ id ^ c;
que[size++] = v;
}
}
}
for (int t = 0; t < (size); ++t) {
int u = que[t];
if (mau > size / 2) {
if (!color[u]) RR[id].push_back(u);
} else {
if (color[u]) RR[id].push_back(u);
}
}
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int run = 0; run < (m); ++run) {
int u, v;
char ch;
cin >> u >> v >> ch;
int id = (ch == 'R' ? 1 : 0);
V[u].push_back(make_pair(v, id));
V[v].push_back(make_pair(u, id));
}
if (cal(0)) {
if (cal(1)) {
if (((int)(RR[0]).size()) < ((int)(RR[1]).size())) {
res = RR[0];
} else
res = RR[1];
} else
res = RR[0];
} else {
if (cal(1))
res = RR[1];
else {
cout << -1 << endl;
return 0;
}
}
cout << ((int)(res).size()) << endl;
for (int i = 0; i < (((int)(res).size())); ++i) cout << res[i] << " ";
return 0;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const int SITO_MAX = 1000000;
int f[SITO_MAX + 1];
vector<int> prosti;
template <class T>
T gcd(T a, T b) {
T t;
while (a) {
t = a;
a = b % a;
b = t;
}
return b;
}
struct sito {
sito() {
for (int i = 2; i <= SITO_MAX; i++) {
if (f[i] == 0) {
f[i] = i;
prosti.push_back(i);
}
int j = 0;
while (j < (int)prosti.size()) {
if (prosti[j] > f[i]) {
break;
}
int x = i * prosti[j];
if (x > SITO_MAX) {
break;
}
f[x] = prosti[j];
j++;
}
}
}
} sito_obj_983431;
vector<pair<long long, int>> factor(long long x) {
vector<pair<long long, int>> v;
for (int p : prosti) {
if (x % p == 0) {
int c = 0;
while (x % p == 0) {
x /= p;
c++;
}
v.push_back({p, c});
}
}
if (x > 1) {
v.push_back({x, 1});
}
return v;
}
int n, m;
long long p[11], k, a[1000005], g;
int e[1000005];
long long gg[1000005][11];
bool cmp_eid(int i, int j) { return e[i] < e[j]; }
map<long long, basic_string<int>> mp;
basic_string<int> choose_m(basic_string<int> a, basic_string<int> b) {
basic_string<int> out(2 * m, 0);
int elems =
merge(a.begin(), a.end(), b.begin(), b.end(), out.begin(), cmp_eid) -
out.begin();
elems = unique(out.begin(), out.begin() + elems) - out.begin();
elems = min(elems, m);
out.resize(elems);
return out;
}
basic_string<int> masks[1 << 11];
basic_string<int> inv_masks[1000005];
long long dp[2][1 << 11][12];
long long mul(long long x, long long y) {
if (x * 1.0 * y > 1e18) return 1e18;
return x * y;
}
void mn(long long& x, long long y) { x = min(x, y); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
g = gcd(g, a[i]);
}
for (int i = 0; i < n; i++) cin >> e[i];
if (g == 1) {
cout << "0\n";
return 0;
}
{
auto v = factor(g);
for (auto ee : v) {
p[m++] = ee.first;
}
}
for (int i = 0; i < n; i++) {
long long tmp = a[i], prod = 1;
for (int j = 0; j < m; j++) {
long long p = ::p[j];
gg[i][j] = 1;
while (tmp % p == 0) {
tmp /= p;
prod *= p;
gg[i][j] *= p;
}
}
mp[prod] += i;
}
for (auto& ee : mp) {
sort(ee.second.begin(), ee.second.end(), cmp_eid);
int sz = min((int)ee.second.size(), m);
ee.second.resize(sz);
auto alpha = gg[ee.second[0]];
for (int mask = 0; mask < (1 << m); mask++) {
long long prod = 1;
for (int i = 0; i < m; i++)
if (mask & (1 << i)) prod *= alpha[i];
if (prod <= k) {
for (int x : ee.second) inv_masks[x] += mask;
}
}
}
auto ol = dp[0], nu = dp[1];
memset(ol, 63, sizeof(dp[0]));
ol[0][0] = 0;
int fm = (1 << m) - 1;
for (int i = 0; i < n; i++) {
if (inv_masks[i].size()) {
memset(nu, 63, sizeof(dp[0]));
for (int diff_mask : inv_masks[i]) {
for (int mask = fm - diff_mask; mask;
mask = (mask - 1) & (fm - diff_mask)) {
for (int br = 0; br < m; br++) {
mn(nu[mask | diff_mask][br + 1], ol[mask][br] + e[i]);
}
}
for (int br = 0; br < m; br++) {
mn(nu[0 | diff_mask][br + 1], ol[0][br] + e[i]);
}
}
for (int mask = 0; mask <= fm; mask++) {
for (int br = 0; br <= m; br++) {
mn(nu[mask][br], ol[mask][br]);
}
}
swap(ol, nu);
}
}
long long sol_all = 123123123123123123ll;
for (int i = 1; i <= m; i++) {
mn(sol_all, mul(i, ol[fm][i]));
}
if (sol_all == 123123123123123123ll) sol_all = -1;
cout << sol_all << '\n';
}
| 3,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int Pow(long long int a, long long int b, long long int md,
long long int ans = 1) {
for (; b; b >>= 1, a = a * a % md)
if (b & 1) ans = ans * a % md;
return ans;
}
const long long int MAXN = 1.5e5 + 10;
const long long int INF = 8e18;
const long long int MOD = 1e9 + 7;
long long int M[MAXN << 2], C[MAXN << 2], lazy[2][MAXN << 2], A[MAXN << 2],
P[MAXN], R[MAXN], n, q;
stack<long long int> mn, mx;
vector<pair<long long int, long long int> > vec[MAXN];
void merge(long long int id) {
M[id] = min(M[id << 1], M[id << 1 | 1]);
C[id] = 0;
A[id] = A[id << 1] + A[id << 1 | 1];
if (M[id] == M[id << 1]) C[id] += C[id << 1];
if (M[id] == M[id << 1 | 1]) C[id] += C[id << 1 | 1];
}
void build(long long int id = 1, long long int l = 1, long long int r = n + 1) {
if (r - l == 1) {
M[id] = l - 1;
C[id] = 1;
return;
}
long long int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid, r);
merge(id);
}
void shift(long long int id, long long int l, long long int r) {
M[id] += lazy[0][id];
if (r - l > 1) {
lazy[0][id << 1] += lazy[0][id];
lazy[0][id << 1 | 1] += lazy[0][id];
}
lazy[0][id] = 0;
A[id] += C[id] * lazy[1][id];
if (r - l > 1) {
if (M[id << 1] + lazy[0][id << 1] == M[id]) lazy[1][id << 1] += lazy[1][id];
if (M[id << 1 | 1] + lazy[0][id << 1 | 1] == M[id])
lazy[1][id << 1 | 1] += lazy[1][id];
}
lazy[1][id] = 0;
}
void update(long long int ql, long long int qr, long long int val,
long long int id = 1, long long int l = 1,
long long int r = n + 1) {
shift(id, l, r);
if (qr <= l || r <= ql) return;
if (ql <= l && r <= qr) {
lazy[0][id] += val;
shift(id, l, r);
return;
}
long long int mid = (l + r) >> 1;
update(ql, qr, val, id << 1, l, mid);
update(ql, qr, val, id << 1 | 1, mid, r);
merge(id);
}
long long int get(long long int ql, long long int qr, long long int id = 1,
long long int l = 1, long long int r = n + 1) {
shift(id, l, r);
if (qr <= l || r <= ql) return 0;
if (ql <= l && r <= qr) return A[id];
long long int mid = (l + r) >> 1;
return get(ql, qr, id << 1, l, mid) + get(ql, qr, id << 1 | 1, mid, r);
}
int main() {
scanf("%lld", &n);
for (long long int i = 1; i <= n; i++) {
scanf("%lld", &P[i]);
}
scanf("%lld", &q);
build();
for (long long int i = 1; i <= q; i++) {
long long int l, r;
scanf("%lld%lld", &l, &r);
vec[r].push_back({l, i});
}
for (long long int i = 1; i <= n; i++) {
long long int l = 1, r = i, x;
while ((long long int)mn.size() && P[mn.top()] > P[i]) {
x = P[mn.top()] - P[i];
mn.pop();
l = 1;
if ((long long int)mn.size()) l = mn.top() + 1;
update(l, r, x);
r = l;
}
r = i, l = 1;
while ((long long int)mx.size() && P[mx.top()] < P[i]) {
x = -P[mx.top()] + P[i];
mx.pop();
l = 0;
if ((long long int)mx.size()) l = mx.top() + 1;
update(l, r, x);
r = l;
}
mn.push(i);
mx.push(i);
if (M[1] == 0) lazy[1][1]++;
for (pair<long long int, long long int> &j : vec[i])
R[j.second] = get(j.first, i + 1);
update(1, n + 1, -1);
}
for (long long int i = 1; i <= q; i++) printf("%lld\n", R[i]);
return 0;
}
| 3,000 | CPP |
n , t = map(int , input().split())
ans = (10**(n-1-(t>9))) ## to handle if t ==0
print([ ans * t,-1][ans < 1]) | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long n, m, a, b, k, jc[100005], ny[100005], pre[1503];
long long f[1503][1503], s[1503][1503], p[1503];
inline long long ksm(long long x, long long y) {
if (y < 0) {
return 0;
}
long long ans = 1;
for (; y; y >>= 1) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod;
}
return ans;
}
inline long long C(long long n, long long m) {
if (n < m) {
return 0;
}
return jc[n] * ny[m] % mod * ny[n - m] % mod;
}
int main() {
cin >> n >> m >> a >> b >> k;
a = a * ksm(b, mod - 2) % mod;
jc[0] = jc[1] = ny[0] = ny[1] = 1;
for (int i = 2; i <= k; i++) {
jc[i] = jc[i - 1] * i % mod, ny[i] = (mod - mod / i) * ny[mod % i] % mod;
}
for (int i = 2; i <= k; i++) {
ny[i] = ny[i - 1] * ny[i] % mod;
}
for (int i = 1; i <= m; i++) {
p[i] =
ksm(a, i - 1) * ksm(1 + mod - a, k - i + 1) % mod * C(k, i - 1) % mod;
pre[i] = (pre[i - 1] + p[i]) % mod;
}
f[0][m] = s[0][m] = 1;
for (int i = 1; i <= n; i++) {
int w = 0;
for (int j = 1; j <= m; j++) {
(w += p[j] * (s[i - 1][m] + mod - s[i - 1][j - 1]) % mod) %= mod;
f[i][j] = (w + mod - s[i - 1][m - j] * pre[j] % mod) * p[m - j + 1] % mod;
s[i][j] = (s[i][j - 1] + f[i][j]) % mod;
}
}
cout << s[n][m] << endl;
return 0;
}
| 3,100 | CPP |
#include <bits/stdc++.h>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int flag, a, b, c;
flag = 1;
if (n % 2 == 0) {
a = 2;
flag = 0;
} else
for (a = 3; a * a * a <= n; a += 2)
if (n % a == 0) {
flag = 0;
break;
}
if (!flag) {
int flag2 = 0;
n /= a;
flag = 1;
if (n % 2 == 0) flag2 = 1;
for (b = a; b * b <= n;) {
if ((n % b == 0) && (a != b)) {
flag = 0;
break;
}
b++;
}
if (!flag) {
n /= b;
flag = 1;
if ((n != a) && (n != b) && (n >= 2)) flag = 0;
}
}
if (flag)
printf("NO\n");
else
printf("YES\n%d %d %d\n", a, b, n);
}
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool solve(int k, int rem, vector<vector<vector<int>>> &dp, vector<int> &sol) {
if (dp[k][rem].size() != 0) {
if (dp[k][rem][0] == -1) return false;
return true;
}
if (rem == 0) {
if (k != 0) {
vector<int> t;
t.push_back(-1);
dp[k][rem] = t;
return false;
} else {
dp[k][rem] = sol;
return true;
}
}
if (k == 0) {
dp[k][rem] = sol;
return true;
}
bool f = false;
for (int i = 2; i <= 317; i++) {
int v = (i * (i - 1)) / 2;
if (v <= k) {
sol.push_back(i);
f = solve(k - v, rem - 1, dp, sol);
if (f) {
dp[k][rem] = sol;
return true;
}
sol.pop_back();
}
}
vector<int> t;
t.push_back(-1);
dp[k][rem] = t;
return false;
}
int main() {
int k;
cin >> k;
vector<vector<vector<int>>> dp(100002,
vector<vector<int>>(27, vector<int>()));
vector<int> sol;
solve(k, 26, dp, sol);
string ans = "";
for (int i = 0; i < sol.size(); i++) {
string temp = "";
temp += char('a' + i);
string tt = "";
for (int j = 0; j < sol[i]; j++) {
tt.append(temp);
}
ans.append(tt);
}
if (k == 0)
cout << "a" << endl;
else
cout << ans << endl;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1, -2, 2, 0, 0},
dy[] = {1, 0, 0, -1, -1, 1, 1, -1, 0, 0, -2, 2};
using namespace std;
vector<vector<int> > g(2005, vector<int>(0));
vector<vector<int> > d(2005, vector<int>(0));
vector<int> nodes;
set<int> dis;
int visited[2005];
void dfs(int cur) {
visited[cur] = 1;
nodes.push_back(cur);
for (int i = 0; i < d[cur].size(); i++) dis.insert(d[cur][i]);
for (int i = 0; i < g[cur].size(); i++) {
if (!visited[g[cur][i]]) dfs(g[cur][i]);
}
}
int main() {
int n, m, k;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> k;
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
d[a].push_back(b);
d[b].push_back(a);
}
int mx = 0;
for (int i = 0; i < n + 1; i++) {
if (!i) continue;
memset(visited, 0, sizeof(visited));
;
dis.clear();
nodes.clear();
dfs(i);
bool ok = 1;
for (int i = 0; i < nodes.size(); i++) {
if (dis.find(nodes[i]) != dis.end()) {
ok = 0;
break;
}
}
if (ok) mx = max(mx, (int)nodes.size());
}
cout << mx << endl;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> G[2 * 100000 + 5];
signed long long int S, X;
int fdp[60][3];
signed long long int dp[60][3];
bool checkBit(signed long long int x, int b) { return (x & (1LL << b)); }
signed long long int rec(int b, int c) {
if (b == 60) {
if (c == 0) return 1;
return 0;
}
if (fdp[b][c]) return dp[b][c];
fdp[b][c] = 1;
dp[b][c] = 0;
for (auto m : {0, 1}) {
for (auto n : {0, 1}) {
bool s = checkBit(m + n + c, 0);
bool new_c = checkBit(m + n + c, 1);
bool x = m ^ n;
if (s == checkBit(S, b) and x == checkBit(X, b)) {
dp[b][c] += rec(b + 1, new_c);
}
}
}
return dp[b][c];
}
int main() {
cin >> S >> X;
cout << rec(0, 0) - (S == X ? 2 : 0);
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct pnt {
int x, y;
pnt(int x = 0, int y = 0) : x(x), y(y) {}
bool operator<(const pnt &p) const { return x == p.x ? y < p.y : x < p.x; }
bool operator==(const pnt &p) const { return x == p.x && y == p.y; }
};
const int inf = 1e9;
int ans;
pnt p[4], rlt[4];
void check(pnt *q) {
sort(q, q + 4);
while (1) {
bool flag = 0;
int cur = -1;
for (int i = 0; i < 4; i++) {
if (q[i].x != p[i].x && q[i].y != p[i].y) {
flag = 1;
break;
}
cur = max(cur, abs(p[i].x - q[i].x) + abs(p[i].y - q[i].y));
}
if (!flag) {
if (cur < ans) {
ans = cur;
for (int i = 0; i < 4; i++) rlt[i] = q[i];
}
}
if (!next_permutation(q, q + 4)) break;
}
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void solve() {
for (int i = 0; i < 4; i++) cin >> p[i].x >> p[i].y;
vector<pnt> u;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
u.push_back(pnt(p[i].x, p[j].y));
}
sort(u.begin(), u.end());
u.erase(unique(u.begin(), u.end()), u.end());
vector<int> a;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
a.push_back(abs(p[i].x - p[j].x));
a.push_back(abs(p[i].y - p[j].y));
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
vector<pnt> v;
vector<int> dist;
for (int i = 0; i < a.size(); i++)
for (int j = i; j < a.size(); j++) {
int d = a[j] - a[i];
dist.push_back(d >> 1);
dist.push_back(d + 1 >> 1);
}
sort(dist.begin(), dist.end());
dist.erase(unique(dist.begin(), dist.end()), dist.end());
for (int k = 0; k < u.size(); k++) {
for (int h = 0; h < 4; h++) {
for (int r = 0; r < dist.size(); r++)
v.push_back(pnt(u[k].x + dist[r] * dx[h], u[k].y + dist[r] * dy[h]));
}
}
ans = inf;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 0; i < v.size(); i++) {
for (int j = 1; j < a.size(); j++) {
pnt q0[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y + a[j]),
pnt(v[i].x + a[j], v[i].y),
pnt(v[i].x + a[j], v[i].y + a[j])};
check(q0);
pnt q1[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y - a[j]),
pnt(v[i].x + a[j], v[i].y),
pnt(v[i].x + a[j], v[i].y - a[j])};
check(q1);
pnt q2[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y + a[j]),
pnt(v[i].x - a[j], v[i].y),
pnt(v[i].x - a[j], v[i].y + a[j])};
check(q2);
pnt q3[4] = {pnt(v[i].x, v[i].y), pnt(v[i].x, v[i].y - a[j]),
pnt(v[i].x - a[j], v[i].y),
pnt(v[i].x - a[j], v[i].y - a[j])};
check(q3);
}
}
if (ans == inf) {
puts("-1");
} else {
printf("%d\n", ans);
for (int i = 0; i < 4; i++) printf("%d %d\n", rlt[i].x, rlt[i].y);
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
| 3,000 | CPP |
# link : https://codeforces.com/problemset/problem/1279/C
for t in range(int(input())):
n, m = map(int, input().split())
a = [x - 1 for x in list(map(int, input().split()))]
b = [x - 1 for x in list(map(int, input().split()))]
pos = a[:]
for i in range(n):
pos[a[i]] = i
lst = -1
res = m
for i in range(m):
if pos[b[i]] > lst:
res += 2 * (pos[b[i]] - i)
lst = pos[b[i]]
print(res) | 1,400 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int node[100005][2];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) scanf("%d%d", &node[i][0], &node[i][1]);
int ans = node[0][1] + node[0][0] - 1;
bool flag = 0;
for (int i = 1; i < m; i++) {
int d_val = node[i][0] - node[i - 1][0];
if (abs(node[i][1] - node[i - 1][1]) > d_val) {
flag = 1;
break;
}
ans = max(ans, node[i][1]);
ans = max(ans, max(node[i][1], node[i - 1][1]) +
(d_val - abs(node[i][1] - node[i - 1][1])) / 2);
}
int syday = n - node[m - 1][0];
ans = max(ans, node[m - 1][1] + syday);
if (flag)
cout << "IMPOSSIBLE" << endl;
else
cout << ans << endl;
return 0;
}
| 1,600 | CPP |
def main():
x = int(input())
l = squares(x)
last = l[len(l) - 1]
if last[0] != last[1]:
print(2 * len(l))
else:
print(2 * len(l) - 1)
for pair in l:
print(pair[0], pair[1])
if last[0] != last[1]:
for pair in l[::-1]:
print(pair[1], pair[0])
else:
revL = l[::-1]
for pair in revL[1:]:
print(pair[1], pair[0])
def squares(x):
if x == 1:
return [(1, 1)]
result = []
cubeRoot = int((6 * x)**(1/3))
n = 1
while n <= cubeRoot:
#m = (y / (n * (n + 1)) + n - 1) / 3
y = 6 * x
temp = n * (n + 1)
if y % temp == 0:
y //= temp
y += n - 1
if y % 3 == 0:
m = y // 3
if m >= n:
result += [(n, m)]
n += 1
return result
#main()
main()
| 1,900 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int n;
int j = 0;
scanf("%d", &n);
int arr[n];
for (int i = 2; i <= n; i++) {
arr[i] = 0;
}
for (int i = 2; i <= n; i++) {
if (arr[i] == 0) {
j = i + i;
} else {
continue;
}
while (j <= n) {
arr[j] += 1;
j += i;
}
}
int count = 0;
for (int i = 2; i <= n; i++) {
if (arr[i] == 2) {
count++;
}
}
printf("%d\n", count);
return 0;
}
| 900 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long a[100001], n, a2, a3, i, a4;
int main() {
cin >> n;
a2 = n;
for (i = 1; i <= n; i++) cin >> a[i];
sort(a, a + n + 1);
while (a2) {
a3 = 0;
for (i = 1; i <= n; i++) {
if (a[i] >= a3) {
a2--;
a3++;
a[i] = -1;
}
}
a4++;
}
cout << a4 << endl;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[200000], ndiff[200000];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
ndiff[n - 1] = n;
for (int i = n - 2; i >= 0; i--) {
if (a[i + 1] != a[i])
ndiff[i] = i + 1;
else
ndiff[i] = ndiff[i + 1];
}
int l, r, x;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &l, &r, &x);
if (a[l - 1] != x)
printf("%d\n", l);
else if (ndiff[l - 1] < r)
printf("%d\n", ndiff[l - 1] + 1);
else
printf("-1\n");
}
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long n, m;
cin >> n >> m;
vector<bool> ok(n, false);
vector<long long> deg(n, 0);
vector<vector<long long>> g(n);
while (m--) {
long long x, y;
cin >> x >> y;
x--, y--;
deg[x]++, deg[y]++;
g[x].push_back(y);
g[y].push_back(x);
}
long long best = max_element(deg.begin(), deg.end()) - deg.begin();
vector<pair<long long, long long>> ans;
queue<long long> q;
q.push(best);
ok[best] = true;
while (!q.empty()) {
long long now = q.front();
q.pop();
for (auto& j : g[now]) {
if (!ok[j]) {
ok[j] = true;
ans.push_back({now, j});
q.push(j);
}
}
}
for (const auto& i : ans) {
cout << i.first + 1 << " " << i.second + 1 << endl;
}
return 0;
}
| 1,600 | CPP |
from sys import stdin
from math import sqrt
input = stdin.readline
for _ in range(int(input())):
n = int(input())
arr = set([0])
s = int(sqrt(n))+1
for i in range(1, s):
if n//i not in arr:
arr.add(n//i)
arr.add(i)
print(len(arr))
arr = list(arr)
arr.sort()
print(*arr)
| 1,400 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& os, vector<long long>& x) {
for (long long i = 0; i < x.size(); i++) os << x[i] << ' ';
return os;
}
ostream& operator<<(ostream& os, pair<long long, long long> x) {
cout << x.first << ' ' << x.second << ' ';
return os;
}
ostream& operator<<(ostream& os, vector<pair<long long, long long> >& x) {
for (long long i = 0; i < x.size(); i++) os << x[i] << '\n';
return os;
}
vector<pair<long long, long long> > changed;
bool mark[500005];
struct dsu {
long long par[500005];
dsu() {
for (long long i = 1; i <= 500005 - 5; i++) par[i] = i;
}
long long find(long long x, bool cache = false) {
if (cache) {
if (!mark[x]) {
mark[x] = true;
changed.push_back(make_pair(x, par[x]));
}
}
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
bool uni(long long x, long long y, bool cache = false) {
x = find(x, cache);
y = find(y, cache);
if (x != y) {
if (cache) {
if (!mark[x]) {
mark[x] = true;
changed.push_back(make_pair(x, par[x]));
}
}
par[x] = y;
return true;
}
return false;
}
} d;
struct data {
long long u, v, c;
data(long long _u, long long _v, long long _c) : u(_u), v(_v), c(_c){};
};
bool byPrice(data x, data y) { return x.c < y.c; }
long long n, m, q;
vector<data> edge;
vector<data> oredge;
vector<vector<vector<long long> > > query(500005);
vector<vector<pair<long long, long long> > > withW(500005);
vector<vector<long long> > tmp(500005);
bool ans[500005];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i = 1; i <= m; i++) {
long long u, v, c;
cin >> u >> v >> c;
edge.push_back(data(u, v, c));
withW[c].push_back(make_pair(u, v));
}
cin >> q;
for (long long i = 1; i <= q; i++) {
long long num;
cin >> num;
vector<long long> zz;
while (num--) {
long long t;
cin >> t;
t--;
zz.push_back(t);
tmp[edge[t].c].push_back(t);
}
for (long long j = 0; j < zz.size(); j++) {
long long c = edge[zz[j]].c;
if (tmp[c].empty()) continue;
tmp[c].push_back(i);
query[c].push_back(tmp[c]);
tmp[c].clear();
}
}
memset(ans, 1, sizeof(ans));
for (long long w = 1; w <= 500000; w++) {
for (long long i = 0; i < query[w].size(); i++) {
for (long long j = 0; j < query[w][i].size() - 1; j++) {
long long num = query[w][i][j];
assert(edge[num].c == w);
long long u = edge[num].u;
long long v = edge[num].v;
d.find(u);
d.find(v);
}
}
for (long long i = 0; i < query[w].size(); i++) {
long long which = query[w][i].back();
bool ok = true;
for (long long j = 0; j < query[w][i].size() - 1; j++) {
long long num = query[w][i][j];
long long u = edge[num].u;
long long v = edge[num].v;
if (!d.uni(u, v, 1)) {
ok = false;
break;
}
}
if (!ok) {
ans[which] = false;
}
for (long long j = 0; j < changed.size(); j++) {
d.par[changed[j].first] = changed[j].second;
mark[changed[j].first] = false;
}
changed.clear();
}
for (long long i = 0; i < withW[w].size(); i++) {
long long u = withW[w][i].first;
long long v = withW[w][i].second;
d.uni(u, v);
}
}
for (long long i = 1; i <= q; i++) {
if (ans[i] == 1)
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
}
| 2,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 8e3 + 5;
int nxt[MAXN], p[MAXN][MAXN], dp[MAXN];
int N, len;
char s[MAXN], S[MAXN];
inline int calc(int x) {
int ret = 0;
while (x) x /= 10, ret++;
return ret;
}
inline void KMP(int st) {
len = N - st + 1;
for (int i = 1; i <= len; i++) s[i] = S[st + i - 1];
int j = 0;
for (int i = 2; i <= len; i++) {
while (j && s[i] != s[j + 1]) j = nxt[j];
if (s[i] == s[j + 1]) j++;
nxt[i] = j;
}
for (int i = 1; i <= len; i++) {
int tmp = i - nxt[i];
if (i % tmp) tmp = i;
p[st][st + i - 1] = tmp + calc(i / tmp);
}
}
int main() {
scanf("%s", S + 1);
N = strlen(S + 1);
for (int i = 1; i <= N; i++) KMP(i);
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= N; i++)
for (int j = 0; j < i; j++) dp[i] = min(dp[i], dp[j] + p[j + 1][i]);
printf("%d\n", dp[N]);
return 0;
}
| 2,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int nax = 30001;
long long dp[nax][600], gems[nax];
int d, OFFSET;
long long rec(int pos, int jmp) {
if (pos >= nax || jmp <= 0) return 0;
long long &ans = dp[pos][jmp - OFFSET];
if (ans != -1) return ans;
return ans =
gems[pos] + max({rec(pos + jmp + 1, jmp + 1), rec(pos + jmp, jmp),
rec(pos + jmp - 1, jmp - 1)});
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(gems, 0, sizeof gems);
int n, p;
cin >> n >> d;
OFFSET = max(0, d - 250);
for (int i = 0; i < int(n); i++) cin >> p, gems[p]++;
memset(dp, -1, sizeof dp);
cout << rec(d, d) << "\n";
}
| 1,900 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
static long long gcd(long long x, long long y) {
return y == 0 ? x : gcd(y, x % y);
}
static long long pwm(long long x, long long n, long long m) {
long long r = 1;
while (n) {
if (n & 1) r = r * x % m;
x = x * x % m;
n >>= 1;
}
return r;
}
int solve() {
int n, m, q, t, a, b, c, d;
set<tuple<int, int, int, int> > s;
scanf("%d%d%d", &n, &m, &q);
while (q--) {
scanf("%d%d%d%d%d", &t, &a, &b, &c, &d);
if (t == 1)
s.insert(std::make_tuple(a, b, c, d));
else if (t == 2)
s.erase(std::make_tuple(a, b, c, d));
else {
bool o = 0;
for (const auto& i : s) {
int a_ = std::get<0>(i), b_ = std::get<1>(i), c_ = std::get<2>(i),
d_ = std::get<3>(i);
o |= (a_ <= a && a <= c_ && b_ <= b && b <= d_) ^
(a_ <= c && c <= c_ && b_ <= d && d <= d_);
}
puts(o ? "No" : "Yes");
}
}
return 0;
}
int main(int argc, char* argv[]) {
::std::ios::sync_with_stdio(false);
::std::cin.tie(0);
::std::cout.tie(0);
int t = 1;
while (t--) solve();
}
| 2,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 500 + 9;
int a[maxn], dp[maxn][maxn];
void input() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
}
void count() {
for (int len = 1; len <= n; len++)
for (int i = 0; i <= n - len; i++) {
int j = len + i;
dp[i][j] = j - i;
for (int k = i + 1; k <= j - 1; k++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
if (a[i] == a[j - 1]) {
dp[i][j] = min(dp[i][j], max(1, dp[i + 1][j - 1]));
}
}
cout << dp[0][n] << endl;
}
int main() {
input();
count();
}
| 1,900 | CPP |
#include <bits/stdc++.h>
int main() {
int n, a, b, c, np, nf;
scanf("%d%d%d%d", &a, &b, &c, &n);
np = a + b - c;
nf = n - np;
if (c >= 0 && a >= c && b >= c && np <= n && np != n) {
printf("%d", nf);
} else {
printf("-1");
}
}
| 1,000 | CPP |
s = input()
if len(s)<=2:
print('YES')
else:
flag = True
for i in range(2,len(s)):
t = ord(s[i-1])-ord('A')+1+ord(s[i-2])-ord('A')
while(t>26):
t-=26
if t!=ord(s[i])-ord('A')+1:
flag = False
break
if flag:
print("YES")
else:
print("NO") | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
int f = 0;
x = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) f |= (ch == '-');
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
if (f) x = -x;
}
const int N = 100005, M = 50, inf = 2e9 + 9;
struct list {
int *a, n;
void push_back(int x) {
if ((n & -n) == n) {
a = (int *)realloc(a, (n * 2 + 1) * (sizeof n));
}
a[n++] = x;
}
} li[N];
int R[N], p[N], f[N], g[N], x[N], ans[N];
int mn[N][18], mx[N][18], lg[N];
int n, m, w, Q;
pair<int, int> q[N];
int value(int l, int r) {
if (r > n) return inf;
int t = lg[r - l + 1];
return max(mx[l][t], mx[r - (1 << t) + 1][t]) -
min(mn[l][t], mn[r - (1 << t) + 1][t]);
}
void init() {
read(n), read(w), read(Q);
m = max(1, (int)(1.5 * pow(n, 1. / 3))), lg[0] = -1;
for (int i = (1); i <= (n); i++) {
read(x[i]), lg[i] = lg[i >> 1] + 1;
mn[i][0] = mx[i][0] = x[i];
}
for (int j = (1); j <= (17); j++)
for (int i = (1); i <= (n - (1 << j) + 1); i++) {
mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
}
for (int i = (1); i <= (Q); i++) {
read(q[i].first);
q[i].first = w - q[i].first;
q[i].second = i;
}
sort(q + 1, q + Q + 1);
for (int i = (n); i >= (1); i--) {
if (i == n || i % m == 0) {
R[i] = i, p[i] = i + 1;
f[i] = 0, g[i] = i;
} else {
R[i] = R[i + 1], p[i] = i + 1;
f[i] = f[p[i]] + 1, g[i] = g[p[i]];
}
int las = 0;
for (int j = (i + 1); j <= (R[i] + 1); j++) {
int d = lower_bound(q + 1, q + Q + 1, make_pair(value(i, j), 0)) - q;
if (las < d) li[d].push_back(i), las = d;
}
}
}
int solve(int step, int id) {
for (int i = 0; i < (li[id].n); i++) {
int k = li[id].a[i];
while (p[k] <= k + m * m && value(k, p[k]) <= step) p[k]++;
if (p[k] > R[k])
f[k] = 0, g[k] = k;
else
f[k] = f[p[k]] + 1, g[k] = g[p[k]];
for (int j = k - 1; R[j] == R[k]; j--)
if (p[j] <= R[j]) f[j] = f[p[j]] + 1, g[j] = g[p[j]];
}
int res = 0, k = 1;
while (k <= n) {
if (p[k] <= R[k]) res += f[k], k = g[k];
assert(p[k] > R[k]);
while (p[k] <= k + m * m && value(k, p[k]) <= step) p[k]++;
if (p[k] <= k + m * m)
res++, k = p[k];
else {
int l = k, r = n + 1, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (value(k, mid) <= step)
l = mid;
else
r = mid - 1;
}
res++, k = l + 1;
}
}
return res;
}
int main() {
init();
for (int i = (1); i <= (Q); i++) ans[q[i].second] = solve(q[i].first, i);
for (int i = (1); i <= (Q); i++) printf("%d\n", ans[i] - 1);
return 0;
}
| 3,400 | CPP |
testCases = int(input())
for i1 in range(testCases):
used = [False]*26
s = input()
t = s[0]
used[ord(s[0]) - ord('a')] = True
pos = 0
result = 'YES'
for i2 in range(1, len(s), 1):
if used[ord(s[i2]) - ord('a')]:
if pos > 0 and t[pos - 1] == s[i2]:
pos -= 1
elif pos < len(t) - 1 and t[pos + 1] == s[i2]:
pos += 1
else:
result = 'NO'
break
else:
if pos == 0:
t = s[i2] + t
elif pos == len(t) - 1:
t += s[i2]
pos += 1
else:
result = 'NO'
break
used[ord(s[i2]) - ord('a')] = True
for i2 in range(26):
if not used[i2]:
t += chr(i2 + ord('a'))
print(result)
if result == 'YES':
print(t) | 1,600 | PYTHON3 |
n, m, k = map(int, input().split())
B = list(map(int, input().split()))
D = sorted(b1 - b0 - 1 for b1, b0 in zip(B[1:], B))
ans = n + sum(D[:max(0, n - k)])
print(ans)
| 1,400 | PYTHON3 |
def tri(lis):
l = [int(i) for i in lis.split()]
b, c = l[1], l[2]
print(b, c, c)
n=int(input())
for i in range(n):
l=input()
tri(l)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a1[101000];
long long sum;
void find_limit(int tmp, long long& l, long long& r, int p) {
l = r = 0;
int i;
for (i = p - 1; i; i--) {
if ((a1[i] & tmp) == 0) break;
l++;
}
for (i = p + 1; i <= n; i++) {
if ((a1[i] & tmp) == 0) break;
r++;
}
}
void solve(int p, int new_value, int origin_value) {
int i;
long long cont_left = 0, cont_right = 0;
for (i = 0; (1 << i) < 101000; i++) {
int tmp = (1 << i);
if ((new_value & tmp) == (origin_value & tmp)) continue;
find_limit(tmp, cont_left, cont_right, p);
long long sub_value =
(cont_left * cont_right + cont_left + cont_right + 1) * (long long)tmp;
if (new_value & tmp)
sum += sub_value;
else
sum -= sub_value;
}
}
void build() {
sum = 0;
int i, j;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%d", &a1[i]);
for (i = 0; (1 << i) < 101000; i++) {
int tmp = (1 << i);
long long num = 0;
for (j = 1; j <= n; j++) {
if (a1[j] & tmp)
num++;
else {
if (num) sum += (num * (num + 1) * (long long)tmp / 2);
num = 0;
}
}
if (num) sum += (num * (num + 1) * (long long)tmp / 2);
}
}
int main() {
build();
int p, v, i;
for (i = 0; i < m; i++) {
scanf("%d%d", &p, &v);
solve(p, v, a1[p]);
a1[p] = v;
printf("%I64d\n", sum);
}
return 0;
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
int a[maxn];
int pos[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
int l = 2e5;
int r = l - 1;
while (t--) {
char m;
int n;
cin >> m >> n;
if (m == 'L') {
--l;
a[l] = n;
pos[n] = l;
} else if (m == 'R') {
++r;
a[r] = n;
pos[n] = r;
} else {
cout << min(r - pos[n], pos[n] - l) << endl;
}
}
return 0;
}
| 1,400 | CPP |
x, y = map(int, input().split())
print("YES\n", "\n".join('{} {}'.format(str(i), str(i+1)) for i in range(x, y+1, 2)), sep='')
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int tc;
ll a, b, INF = 1e9;
int main() {
scanf("%d", &tc);
while (tc--) {
scanf("%lld %lld", &a, &b);
if (a % b) {
printf("%lld\n", a);
continue;
}
ll A = a, B = b;
ll mx = 0;
for (ll i = 2; i * i <= INF; i++) {
if (i == 1) break;
if (b % i == 0) {
int c1 = 0, c2 = 0;
while (b % i == 0) b /= i, c1++;
while (a % i == 0) a /= i, c2++;
int c = c2 - (c1 - 1);
ll g = 1;
for (int j = 1; j <= c; j++) g *= i;
mx = max(mx, A / g);
}
}
if (b > 1) {
int c1 = 1, c2 = 0;
while (a % b == 0) a /= b, c2++;
int c = c2 - (c1 - 1);
ll g = 1;
for (int j = 1; j <= c; j++) g *= b;
mx = max(mx, A / g);
}
printf("%lld\n", mx);
}
}
| 1,500 | CPP |
# 20191231 19:04 ~ 19:09
t = int(input())
for T in range(t):
n, k1, k2 = map(int, input().split())
ans = "NO"
if n in list(map(int, input().split())):
ans = "YES"
input()
print(ans) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, n, m, k;
int main() {
scanf("%d%d", &n, &k);
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
greater<pair<long long, int>>>
q;
queue<pair<long long, int>> sq;
for (int _n((n)-1), i(0); i <= _n; i++) {
scanf("%d%d", &a, &b);
q.push(make_pair(a, b));
}
c = k;
while (!q.empty()) {
pair<long long, int> t = q.top();
q.pop();
if (t.second == -1) {
if (!sq.empty()) {
pair<long long, int> tt = sq.front();
sq.pop();
q.push(make_pair(t.first + tt.second, -1));
printf("%I64d\n", t.first + tt.second);
} else
++c;
} else {
if (!c)
sq.push(t);
else {
--c;
q.push(make_pair(t.first + t.second, -1));
printf("%I64d\n", t.first + t.second);
}
}
}
}
| 1,600 | CPP |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef NOOBxCODER
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#define NOOBxCODER 0
#endif
cin>>t;
//t=1;
while(t--){
ll count=0;
cin>>n;
if(n<20){
m=n;
while(n>1){
n= (n+1)/2;
count++;
}
n=m;
cout<<n-3+count<<endl;
fo1(i,n-3){
cout<<i+2<<" "<<n<<endl;
}
fo(i,count)cout<<n<<" "<<2<<endl;
}
else{
m=n;
while(n>1){
n= (n+15)/16;
count++;
}
n=m;
cout<<n-5+4+count<<endl;
fo1(i,n-3){
if(i!=14 && i!=2)cout<<i+2<<" "<<n<<endl;
}
fo(i,count)cout<<n<<" "<<16<<endl;
fo(i,2)cout<<16<<" "<<4<<endl;
fo(i,2)cout<<4<<" "<<2<<endl;
}
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 1,700 | CPP |
#!/usr/bin/env python3
from sys import stdin as cin
from itertools import accumulate
lmap = lambda f, it: list(map(f, it))
laccumulate = lambda it, *f: list(accumulate(it, *f))
def min_xor(s):
if len(s) % 2 == 1:
return -1
s.sort(reverse=True)
for n in s[1:]:
k = s[0] ^ n
t = [(k ^ x) for x in s]
if s == sorted(t, reverse=True):
return k
return -1
def main():
t = int(next(cin).strip())
for i in range(t):
next(cin)
s = lmap(int, next(cin).strip().split())
print(min_xor(s))
main()
| 1,200 | PYTHON3 |
x, k = [int(a) for a in input().strip().split()]
def binpow(x, k, mod):
res = 1
while k > 0:
if k & 1:
res = ( res * x ) % mod
x = ( x * x) % mod
k >>= 1
return res
if x == 0:
print(0)
exit()
mod = int(1e9 + 7)
k2 = binpow(2, k, mod)
res = ( k2 * (2 * x - 1) + 1) % mod
res %= mod
print(int(res)) | 1,600 | PYTHON3 |
try:
y, k, n = map(int,input().split())
flg = 0
if(y >= n ):
print(-1)
else:
mult = 1
while(k*mult <= n):
check = k * mult
if(check-y > 0):
print(check-y, end= " ")
flg = 1
mult += 1
else:mult += 1
if(flg == 0):print(-1)
except:
pass | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using pll = pair<long long, long long>;
using vl = vector<long long>;
using vpll = vector<pll>;
using mll = map<long long, long long>;
using mcl = map<char, long long>;
using msl = map<string, long long>;
using sl = set<long long>;
using sc = set<char>;
using dl = deque<long long>;
const int N = 1e6 + 5;
long long mod = 1e9 + 7;
vl adj[N];
vpll adjc[N];
long long vis[N];
long long arr[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long a, b, c, d, n, m, p, x, y, z, i, j, k,
f = 0, tc, cnt = 0, sum = 0, mul = 1, mi = 1e18, ma = -1e18;
string str;
char ch;
double db;
long long l, r;
cin >> tc;
while (tc--) {
cin >> n;
sum = 0;
cnt = 0;
f = 0;
for (i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] != -1) {
f = 1;
}
}
if (f == 0) {
cout << 0 << " " << 0 << "\n";
continue;
}
vl vt;
for (i = 0; i < n - 1; i++) {
if (i == 0) {
if (arr[i] != -1 && arr[i + 1] == -1) {
vt.push_back(arr[i]);
}
continue;
}
if (arr[i] != -1 && arr[i - 1] == -1 || arr[i] != -1 && arr[i + 1] == -1)
vt.push_back(arr[i]);
}
if (arr[i] != -1 && arr[i - 1] == -1) vt.push_back(arr[i]);
sort((vt).begin(), (vt).end());
x = int((vt).size());
long long ans;
ans = (vt[x - 1] + vt[0]) / 2;
ma = 0;
for (i = 0; i < n; i++) {
if (arr[i] == -1) arr[i] = ans;
}
for (i = 0; i < n - 1; i++) {
ma = max(ma, abs(arr[i] - arr[i + 1]));
}
cout << ma << " " << ans << "\n";
}
return 0;
}
| 1,500 | CPP |
#input=__import__('sys').stdin.readline
n,x,y = map(int,input().split())
s = list(input())
if s.count('0')==0:
print(0)
exit()
s1=s[:]
grp=0
for i in range(1,n):
if s[i]=='1' and s[i-1]=='0':
grp+=1
if s[-1]=='0':
grp+=1
ans = min((grp-1)*x +y , grp*y)
print(ans)
| 1,500 | PYTHON3 |
n, q, k = map(int, input().split())
x = [int(w) for w in input().split()]
c = [0]*(n)
b = 0
for i in range(n):
l = 0
r = k+1
if(i > 0):
l = x[i-1]
if(i < n-1):
r = x[i+1]
b += r-l-2
c[i] = b
for i in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
if(l == r):
print(k-1)
else:
ans = c[r-1]
ans -= c[l]
ll, lr = 0, x[l+1]
ans += lr-ll-2
rl, rr = x[r-1], k+1
ans += rr-rl-2
print(ans) | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n[5], t[5];
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q[5];
int main() {
int k;
cin >> k;
for (int i = 0; i < 3; i++) {
cin >> n[i];
for (int j = 0; j < n[i]; j++) q[i].push(pair<int, int>(0, i));
}
for (int i = 0; i < 3; i++) cin >> t[i];
int ans = 0;
for (int i = 0; i < k; i++) {
int tme = 0;
int sum = 0;
for (int j = 0; j < 3; j++) {
tme = max(tme, q[j].top().first - sum);
sum += t[j];
}
sum = 0;
for (int j = 0; j < 3; j++) {
int l = q[j].top().second;
q[j].pop();
sum += t[j];
q[j].push(pair<int, int>(tme + sum, l));
}
ans = tme;
}
cout << ans + t[0] + t[1] + t[2] << endl;
}
| 1,900 | CPP |
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0; i<t; i++)
{
int s=0,n,x,A[100];
cin>>n>>x;
for(int j=0; j<n; j++)
{cin>>A[j];
s+=A[j];}
if(s==x)
cout<<"NO"<<endl;
if(s<x)
{cout<<"YES"<<endl;
for(int k=0; k<n; k++)
cout<<A[k]<<" ";
cout<<endl;}
if(s>x)
{int v=0,s1=0;
bool b=0;
for(int k=0; k<n; k++)
{s1+=A[k];
if(s1==x)
{v=A[k];
break;}}
cout<<"YES"<<endl;
for(int q=0; q<n; q++)
{if(A[q]==v)
{b=1;
continue;}
cout<<A[q]<<" ";
if(b)
{b=0;
cout<<v<<" ";}}
cout<<endl;
}
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
int cnt = k, ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0')
cnt++;
else {
ans += (cnt - k) / (k + 1);
cnt = 0;
}
}
ans += cnt / (k + 1);
cout << ans << endl;
}
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j;
string W_s, W_key, keyboard = "qwertyuiopasdfghjkl;zxcvbnm,./";
cin >> W_key;
cin >> W_s;
if (W_key == "R") {
for (i = 0; i < W_s.length(); i++) {
for (j = 0; i < W_s.length(); j++) {
if (keyboard[j] == W_s[i]) {
cout << keyboard[j - 1];
break;
}
}
}
} else {
for (i = 0; i < W_s.length(); i++) {
for (j = 0; i < W_s.length(); j++) {
if (keyboard[j] == W_s[i]) {
cout << keyboard[j + 1];
break;
}
}
}
}
}
| 900 | CPP |
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes() {
bool prime[10000001];
memset(prime, true, sizeof(prime));
for (long long int p = 2; p * p <= 10000000; p++) {
if (prime[p] == true) {
for (long long int i = p * p; i <= 10000000; i += p) {
prime[i] = false;
}
}
}
}
long long int binpow(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) res = (res * a);
a = (a * a);
b >>= 1;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (b % a == 0) return a;
return gcd(b % a, a);
}
long long int nCrModpDP(long long int n, long long int r, long long int p) {
long long int C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long int i = 1; i <= n; i++) {
for (long long int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
long long int nCrModp(long long int n, long long int r, long long int p) {
if (r == 0) return 1;
long long int ni = n % p, ri = r % p;
return (nCrModp(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long int j, i, n, t, l, r, c, h, k, m, maxi, d, x;
cin >> n;
string s;
cin >> s;
m = 0;
x = 0;
for (i = 0; i < n; i++) {
if ((s[i] - '0') != i % 2) m++;
}
for (i = 0; i < n; i++) {
if ((s[i] - '0') != (i + 1) % 2) x++;
}
cout << min(m, x);
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX;
const double PI = 3.14159265;
int dpx[4] = {0, 1, 0, -1};
int dpy[4] = {1, 0, -1, 0};
vector<long long> sc;
void rec(long long N) {
sc.push_back(N);
if (N <= 10e9) {
rec(N * 10 + 4);
rec(N * 10 + 7);
}
}
int main() {
ios::sync_with_stdio(false);
long long res = 0;
long long pos = 0, l, r;
rec(4);
rec(7);
sort(sc.begin(), sc.end());
cin >> l >> r;
while (l <= r) {
while (l > sc[pos]) {
pos++;
}
res += sc[pos] * min(sc[pos] - l + 1, r - l + 1);
l = sc[pos] + 1;
}
cout << res;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
const int N = 2e6, inf = 1e9 + 7;
using namespace std;
int n, m, a[N], b[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= m; ++i) scanf("%d", &b[i]);
sort(a + 1, a + n + 1);
sort(b + 1, b + m + 1);
if (a[n] > b[1]) {
puts("-1");
return 0;
}
long long ans = 0;
if (a[n] == b[1]) {
for (int i = 1; i <= m; ++i) ans += b[i];
for (int i = 1; i <= n - 1; ++i) ans += (long long)a[i] * m;
} else {
if (n == 1) {
puts("-1");
return 0;
}
for (int i = 1; i <= m; ++i) ans += b[i];
for (int i = 1; i <= n - 1; ++i) ans += (long long)a[i] * m;
ans += a[n] - a[n - 1];
}
printf("%lld\n", ans);
return 0;
}
| 1,500 | CPP |
n=int(input())
a=[int(x) for x in input().split()]
b=a[::]
c=[]
d=[]
a.sort()
for i in range(n):
if(b[i]!=a[i]):
c.append(int(i))
if(len(c)==0):
print('yes')
print('1','1')
#elif(c[len(c)-1]!=c[0]+len(c)-1):
# print('no')
else:
d=b[:c[0]:] + b[c[len(c)-1]:c[0]-1:-1] + b[c[len(c)-1]+1:n:] if(c[0]>0) else b[:c[0]:] + b[c[len(c)-1]::-1] + b[c[len(c)-1]+1:n:]
if(d!=a):
print('no')
else:
print('yes')
print(c[0]+1,c[len(c)-1]+1)
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, tot = 0, tmp, x = 0;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> tmp;
tot += tmp;
if (tmp & 1) {
if (!x) {
x = tmp;
} else {
x = min(x, tmp);
}
}
}
if (tot & 1) {
tot -= x;
}
cout << tot;
}
| 900 | CPP |
from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
##########################################################
#for _ in range(int(input())):
# from collections import deque
from collections import Counter
# ls=list(map(int,input().split()))
# for i in range(m):
# for i in range(int(input())):
# arr = list(map(int, input().split()))
# for i in range(int(input())):
import math
# a,b= map(int, input().split())
for _ in range(int(input())):
b,p,f= map(int, input().split())
h,c=map(int, input().split())
if h>c:
hh=min(b//2,p)
pp=hh*h
cc=min((b-hh*2)//2,f)
pp+=cc*c
else:
cc=min(b//2,f)
pp=cc*c
hh=min((b-cc*2)//2,p)
pp+=hh*h
print(pp)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct Hash {
int num[2];
void set(int x, int y) { num[0] = x, num[1] = y; }
Hash operator*(const Hash &A) const {
Hash res;
res.num[0] = (1LL * num[0] * A.num[0]) % 1000000007;
res.num[1] = (1LL * num[1] * A.num[1]) % 1000000009;
return res;
}
Hash operator+(const Hash &A) const {
Hash res;
res.num[0] = (num[0] + A.num[0]) % 1000000007;
res.num[1] = (num[1] + A.num[1]) % 1000000009;
return res;
}
bool operator==(const Hash &A) const {
return A.num[0] == num[0] && A.num[1] == num[1];
}
} based[100005], num[10][100005];
struct node {
int L, R, x;
int len() { return (R - L + 1); }
Hash num;
} tree[100005 << 2];
char str[100005];
void down(int p) {
int &t = tree[p].x;
if (t == -1) return;
tree[2 * p].x = t;
tree[2 * p].num = num[t][tree[2 * p].len()];
tree[2 * p + 1].x = t;
tree[2 * p + 1].num = num[t][tree[2 * p + 1].len()];
t = -1;
}
void up(int p) {
tree[p].num =
tree[2 * p].num * based[tree[2 * p + 1].len()] + tree[2 * p + 1].num;
}
void build(int L, int R, int p) {
tree[p].L = L, tree[p].R = R;
tree[p].x = -1;
if (L == R) {
tree[p].num.set(str[L] - '0', str[L] - '0');
return;
}
int mid = (L + R) >> 1;
build(L, mid, 2 * p);
build(mid + 1, R, 2 * p + 1);
up(p);
}
void update(int L, int R, int c, int p) {
if (tree[p].L == L && R == tree[p].R) {
tree[p].x = c;
tree[p].num = num[c][R - L + 1];
return;
}
down(p);
int mid = (tree[p].L + tree[p].R) >> 1;
if (R <= mid)
update(L, R, c, 2 * p);
else if (L > mid)
update(L, R, c, 2 * p + 1);
else {
update(L, mid, c, 2 * p);
update(mid + 1, R, c, 2 * p + 1);
}
up(p);
}
Hash query(int L, int R, int p) {
if (tree[p].L == L && tree[p].R == R) {
return tree[p].num;
}
down(p);
int mid = (tree[p].L + tree[p].R) >> 1;
if (R <= mid)
return query(L, R, 2 * p);
else if (L > mid)
return query(L, R, 2 * p + 1);
else {
return query(L, mid, 2 * p) * based[R - mid] + query(mid + 1, R, 2 * p + 1);
}
}
int main() {
int n, m, k, i, j, a, b, c;
scanf("%d %d %d", &n, &m, &k);
m += k;
scanf("%s", str + 1);
Hash tmp;
tmp.set(233, 233);
based[0].set(1, 1);
for (i = 1; i <= n; i++) based[i] = based[i - 1] * tmp;
Hash one;
for (i = 0; i < 10; i++) {
num[i][0].set(0, 0);
one.set(i, i);
for (j = 1; j <= n; j++) {
num[i][j] = num[i][j - 1] * tmp + one;
}
}
build(1, n, 1);
while (m-- && scanf("%d %d %d %d", &k, &a, &b, &c)) {
if (k == 1) {
update(a, b, c, 1);
} else {
if (b - a + 1 == c)
puts("YES");
else {
puts(query(a, b - c, 1) == query(a + c, b, 1) ? "YES" : "NO");
}
}
}
return 0;
}
| 2,500 | CPP |
i=int(input())
d={}
b=[]
c=[]
for y in range(0,i):
a=input()
if(a not in d):
b.append('OK')
d[a]=0
else:
d[a]=d[a]+1;
d[a+str(d[a])]=0;
b.append(a+str(d[a]))
"""if(a in b):
for x in range(1,len(b)+1):
if(a+str(x) not in b):
b.append(a+str(x))
c.append(a+str(x))
break
else:
c.append('OK')
b.append(a)"""
for i in b:
print(i)
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 9;
const long long MOD = 1e9 + 7;
long long dp[MAX], dp2[MAX];
vector<int> g[MAX];
void dfs1(int v) {
dp[v] = 1;
for (auto u : g[v]) {
dfs1(u);
dp[v] = dp[v] * (dp[u] + 1) % MOD;
}
return;
}
void dfs2(int v) {
long long p1[g[v].size()];
long long p2[g[v].size()];
for (int i = 0; i < g[v].size(); i++) {
int u = g[v][i];
p1[i] = ((i == 0) ? (dp[u] + 1) : p1[i - 1] * (dp[u] + 1) % MOD);
}
for (int i = g[v].size() - 1; i >= 0; i--) {
int u = g[v][i];
p2[i] =
((i == g[v].size() - 1) ? (dp[u] + 1) : p2[i + 1] * (dp[u] + 1) % MOD);
}
for (int i = 0; i < g[v].size(); i++) {
int u = g[v][i];
long long m = ((i == g[v].size() - 1) ? 1 : p2[i + 1]) *
((i == 0) ? 1 : p1[i - 1]) % MOD;
m = m * dp2[v] % MOD;
dp2[u] = m + 1;
dfs2(u);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
p--;
g[p].push_back(i + 1);
}
dp2[0] = 1;
dfs1(0);
dfs2(0);
for (int i = 0; i < n; i++) {
cout << dp[i] * max(dp2[i], (long long)1) % MOD << " ";
}
cout << "\n";
}
| 2,300 | CPP |
from sys import stdin
input=stdin.readline
for _ in range(int(input())):
a,b,c=map(int,input().split())
if a==0 and c==0:
if b==1:print('01')
else:
s='01'*((b+1)//2)
if b%2==0:s='1'+s
print(s)
elif a and b and c:b-=1;print('1'*(b%2)+'0'*(a+1)+'1'*(c+1)+'01'*(b//2))
elif b and c and not a:print('0'*(b%2)+'1'*(c+1)+'01'*(b//2))
elif a and b and not c:print('1'*(b%2)+'0'*(a+1)+'10'*(b//2))
elif c and not b and not a:print('1'*(c+1))
elif a and not b and not c:print('0'*(a+1)) | 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 150005;
int n, m, h;
struct Tnode {
int id, val, maxV, add;
Tnode *f, *son[2];
} tree[maxN], *root, *nul;
int s[maxN], c[maxN], r[maxN];
int trees;
Tnode *newnode(int id, int val) {
trees++;
tree[trees].id = id;
tree[trees].val = tree[trees].maxV = val;
tree[trees].son[0] = tree[trees].son[1] = tree[trees].f = nul;
return tree + trees;
}
void updata(Tnode *root) {
root->maxV = root->val;
if (root->son[0] != nul) root->maxV = max(root->maxV, root->son[0]->maxV);
if (root->son[1] != nul) root->maxV = max(root->maxV, root->son[1]->maxV);
}
void down(Tnode *root) {
if (root->add == 0) return;
if (root->son[0] != nul) {
root->son[0]->add += root->add;
root->son[0]->val += root->add;
root->son[0]->maxV += root->add;
}
if (root->son[1] != nul) {
root->son[1]->add += root->add;
root->son[1]->val += root->add;
root->son[1]->maxV += root->add;
}
root->add = 0;
}
void Readln() {
scanf("%d%d%d\n", &n, &m, &h);
for (int i = 1; i <= m; i++) {
int val;
scanf("%d", &val);
r[i] = h - val;
}
for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
}
bool cmp(int a, int b) { return a > b; }
void rot(Tnode *u, int t) {
Tnode *v = u->f, *w = v->f;
if (w != nul) {
if (w->son[0] == v) w->son[0] = u;
if (w->son[1] == v) w->son[1] = u;
}
u->f = w;
v->son[t ^ 1] = u->son[t];
if (u->son[t] != nul) u->son[t]->f = v;
v->f = u;
u->son[t] = v;
updata(v);
}
void splay(Tnode *u, Tnode *fa) {
while (u->f != fa && u->f != nul) {
Tnode *v = u->f;
Tnode *w = v->f;
down(w);
down(v);
down(u);
if (w == nul) {
rot(u, v->son[0] == u);
break;
}
bool p = (v->son[0] == u), q = (w->son[0] == v);
if (p ^ q) {
rot(u, p);
rot(u, q);
} else {
rot(v, q);
rot(u, p);
}
}
if (fa == nul) root = u;
}
Tnode *findpred(int id) {
Tnode *now = root, *ret = nul;
while (now != nul) {
down(now);
if (now->id >= id) {
ret = now;
now = now->son[1];
} else
now = now->son[0];
}
return ret;
}
Tnode *inittree(Tnode *fa, int lef, int rig) {
if (lef > rig) return nul;
int mid = (lef + rig) / 2;
Tnode *now = newnode(r[c[mid]], c[mid]);
now->f = fa;
if (lef == rig) return now;
now->son[0] = inittree(now, lef, mid - 1);
now->son[1] = inittree(now, mid + 1, rig);
updata(now);
return now;
}
void addtree(int id, int val) {
Tnode *pre = findpred(id + 1);
if (pre == nul) {
root->add += val;
root->maxV += val;
root->val += val;
return;
}
splay(pre, nul);
if (root->son[1] == nul) return;
root->son[1]->add += val;
root->son[1]->maxV += val;
root->son[1]->val += val;
updata(root);
}
void Solve() {
sort(r + 1, r + 1 + m, cmp);
int tm = 0;
for (int i = 1; i <= m; i++)
if (r[i] != r[i + 1] && r[i] > 0) {
tm++;
c[tm] = i;
}
nul = newnode(0, -1000000);
root = inittree(nul, 1, tm);
for (int i = 1; i <= m; i++) addtree(s[i], -1);
int ans = 0;
if (root->maxV <= 0) ans++;
for (int i = m + 1; i <= n; i++) {
addtree(s[i - m], 1);
addtree(s[i], -1);
if (root->maxV <= 0) ans++;
}
printf("%d\n", ans);
}
int main() {
Readln();
Solve();
return 0;
}
| 2,600 | CPP |
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 17 19:40:00 2020
@author: shubham gupta
"""
for _ in range(int(input())):
print(' '.join(['1']*(int(input())))) | 800 | PYTHON3 |
nm=input().split()
tasks=list(map(lambda x: int(x),input().split()))
current_place=1
time=0
for x in tasks:
if(x>current_place):
time+=x-current_place
current_place=x
if(x<current_place):
time+=int(nm[0])-current_place+x
current_place=x
print(time)
| 1,000 | PYTHON3 |
n = int(input())
if n %2 ==0:
print(int(n/2))
else:
print(int(n/2)-n) | 800 | PYTHON3 |
def ans():
N=int(input())
for i in range(N):
A=int(input())
arr=input().split()
a=0
b=0
for j in range(A):
if arr[j]=='1':
a+=1
elif arr[j]=='0':
b+=1
if a>b:
if (len(arr)//2)%2==0:
print((len(arr))//2)
for k in range((len(arr))//2):
if k!=(len(arr))//2-1:
print(1,end=' ')
else:
print(1)
else:
print((len(arr))//2+1)
for k in range((len(arr))//2):
if k!=(len(arr))//2-1:
print(1,end=' ')
else:
print(1,end=' ')
print(1)
else:
print((len(arr))//2)
for k in range((len(arr))//2):
if k!=(len(arr))//2-1:
print(0,end=' ')
else:
print(0)
ans()
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
vector<long long int> v;
cin >> n;
for (long long int i = 0; i < n; i++) {
long long int a;
cin >> a;
v.push_back(a);
}
long long int s = 1, k = 1;
for (long long int i = 0; i < n - 1; i++) {
if ((v[i] * 2) >= v[i + 1])
k++;
else
k = 1;
s = max(s, k);
}
cout << s << endl;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int ar[100001];
bool vis[100001];
vector<vector<int> > all(100001);
bool flag = 0;
void dfs(int s, int k) {
int t;
vis[s] = 1;
if (all[s].size() > 0) {
ar[s] = k;
}
for (int i = 0; i < all[s].size(); i++) {
if (vis[all[s][i]]) {
if (ar[all[s][i]] == k) {
flag = 1;
return;
}
} else {
if (k == 1) {
t = 2;
} else {
t = 1;
}
dfs(all[s][i], t);
if (flag) {
return;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int u, v, x, y;
cin >> n >> m;
cin >> u >> v;
all[u].push_back(v);
all[v].push_back(u);
for (int i = 1; i < m; i++) {
cin >> x >> y;
all[x].push_back(y);
all[y].push_back(x);
}
ar[u] = 1;
ar[v] = 2;
dfs(u, 1);
if (flag) {
cout << -1;
return 0;
}
dfs(v, 2);
if (flag) {
cout << -1;
return 0;
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, 1);
if (flag) {
cout << -1;
return 0;
}
}
}
vector<int> A;
vector<int> B;
for (int i = 1; i <= n; i++) {
if (ar[i] == 1) {
A.push_back(i);
} else if (ar[i] == 2) {
B.push_back(i);
}
}
cout << A.size() << endl;
for (int i = 0; i < A.size(); i++) {
cout << A[i];
if (i != A.size() - 1) {
cout << " ";
}
}
cout << endl;
cout << B.size() << endl;
for (int i = 0; i < B.size(); i++) {
cout << B[i];
if (i != B.size() - 1) {
cout << " ";
}
}
return 0;
}
| 1,500 | CPP |