solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
long long minn(long long a, long long b) { return (a > b) ? b : a; }
long long xxx(long long a, long long b, long long c) {
if (a < b) {
if (a < c)
return a;
else
return c;
} else {
if (b < c)
return b;
else
return c;
}
}
int main() {
long long n, a, b, c;
scanf("%lld%lld%lld%lld", &n, &a, &b, &c);
if (n % 4 == 0)
printf("%d", 0);
else if (n % 4 == 2) {
long long r = xxx(2 * a, b, 2 * c);
printf("%lld", r);
} else if (n % 4 == 3) {
long long r = xxx(a, 3 * c, b + c);
printf("%lld", r);
} else {
long long r = xxx(3 * a, a + b, c);
printf("%lld", r);
}
return 0;
}
| 1,300 | CPP |
n=int(input())
c=[]
for i in range(n):
m,n=map(int,input().split())
x=min(m,n)
if 2*x>max(m,n):
c.append((2*x)**2)
else:
c.append(max(m,n)**2)
for i in c:
print(i) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long fac[200111];
long long powmod(long long n, long long p) {
long long res = 1, a = n;
for (; p; p >>= 1) {
if (p & 1) res = res * a % 1000000007ll;
a = a * a % 1000000007ll;
}
return res;
}
long long Cal(int x, int y) {
return fac[x + y] *
powmod(fac[x] * fac[y] % 1000000007ll, 1000000007ll - 2ll) %
1000000007ll;
}
pair<int, int> value[2111];
long long f[2111];
int main() {
int n, h, w;
fac[0] = 1;
for (int i = (1), _b = (200000); i <= _b; ++i)
fac[i] = fac[i - 1] * i % 1000000007ll;
scanf("%d%d%d", &h, &w, &n);
for (int i = (1), _b = (n); i <= _b; ++i)
scanf("%d%d", &(value[i].first), &(value[i].second));
value[++n] = make_pair(h, w);
sort(value + 1, value + n + 1);
for (int i = (1), _b = (n); i <= _b; ++i) {
f[i] = Cal(value[i].first - 1, value[i].second - 1);
for (int j = (1), _b = (i - 1); j <= _b; ++j)
if (value[i].first >= value[j].first &&
value[i].second >= value[j].second)
f[i] = (f[i] - Cal(value[i].first - value[j].first,
value[i].second - value[j].second) *
f[j]) %
1000000007ll;
}
(f[n] += 1000000007ll) %= 1000000007ll;
printf("%I64d", f[n]);
return 0;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
int n;
vector<int> g[N];
int sz[N];
void dfsz(int v, int p) {
sz[v] = 1;
for (int to : g[v]) {
if (to != p) {
dfsz(to, v);
sz[v] += sz[to];
}
}
}
double ans[N];
void bfs() {
queue<int> q;
vector<int> used(n + 123, false);
q.push(1);
used[1] = true;
ans[1] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
if (used[v]) continue;
ans[v] = ans[u] + 1.0 + (sz[u] - sz[v] - 1) / 2.0;
used[v] = true;
q.push(v);
}
}
}
int main() {
scanf("%d", &n);
for (int v = 2; v <= n; v++) {
int p;
scanf("%d", &p);
g[v].push_back(p);
g[p].push_back(v);
}
dfsz(1, -1);
bfs();
for (int v = 1; v <= n; v++) {
printf("%.10lf\n", ans[v]);
}
printf("\n");
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, q, i, t, ct, f, j;
cin >> n >> k >> q;
int a[n + 1];
set<int> v;
set<int>::reverse_iterator b;
set<int>::iterator b1;
for (i = 1; i <= n; i++) cin >> a[i];
while (q--) {
cin >> t >> j;
if (t == 1) {
v.insert(a[j]);
} else {
ct = k;
f = 0;
for (b = v.rbegin(); b != v.rend() && ct--; b++) {
if (*b == a[j]) {
f = 1;
break;
}
}
if (f)
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
}
| 1,200 | CPP |
t = int(input())
for test in range(t):
n,x = map(int,input().split())
a = list(map(int,input().split()))
odd = 0
even = 0
for i in range(n):
if a[i]%2==0: even+=1
else: odd+=1
if odd%2==0:
mx = even + odd-1
else:
mx = even + odd
if odd==0: print("No")
else:
if x%2==0 and even==0: print("No")
else:
if mx>=x: print("Yes")
else: print("No")
| 1,200 | PYTHON3 |
def peredachi():
n = int(input())
lst_n = [int(x) for x in input().split()]
n = int(input())
lst_m = [int(x) for x in input().split()]
all_ = []
for i in lst_n:
for j in lst_m:
if j%i == 0:
all_.append(j/i)
max = all_[0]
num_max = 0
for i in all_:
if i == max:
num_max += 1
if i > max:
max = i
num_max = 1
return num_max
if __name__ == '__main__':
print(peredachi()) | 900 | PYTHON3 |
#n = int(input())
#n, m = map(int, input().split())
s = input()
#c = list(map(int, input().split()))
l = 0
for i in range(len(s) - 1, -1, -1):
if l < 6:
if s[i] == '0':
l += 1
else:
if s[i] == '1':
l += 1
break
if l == 7:
print('yes')
else:
print('no') | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int out[100010], deg[100010];
queue<pair<int, int> > q[100010];
bool vis[100010 * 3];
struct Node {
int a, b;
} ans[3 * 100010];
int cnt;
void dfs(int u) {
int i, v;
pair<int, int> now;
while (!q[u].empty()) {
now = q[u].front();
q[u].pop();
v = now.first;
if (vis[now.second]) continue;
vis[now.second] = 1;
dfs(v);
ans[++cnt].a = u;
ans[cnt].b = v;
}
}
int main() {
int n, m, i, a, b;
scanf("%d%d", &n, &m);
int num = 0;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
q[a].push(make_pair(b, ++num));
q[b].push(make_pair(a, num));
deg[a]++;
deg[b]++;
}
int last = -1;
for (int i = 1; i <= n; i++)
if (deg[i] & 1) {
if (last == -1)
last = i;
else {
q[last].push(make_pair(i, ++num));
q[i].push(make_pair(last, num));
last = -1;
deg[last]++;
deg[i]++;
}
}
if (num & 1) q[1].push(make_pair(1, ++num));
dfs(1);
cout << cnt << endl;
for (int i = 1; i <= cnt; i++) {
if (i & 1)
printf("%d %d\n", ans[i].a, ans[i].b);
else
printf("%d %d\n", ans[i].b, ans[i].a);
}
return 0;
}
| 2,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxl = 5 * 100 + 5;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
char c[4] = {'R', 'D', 'L', 'U'};
char op[4] = {'L', 'U', 'R', 'D'};
bool a[maxl][maxl];
int n;
int xs, ys;
string ans;
bool marked[maxl][maxl];
void dfs(int x, int y) {
marked[x][y] = true;
marked[x][y] = true;
if (!a[x][y]) a[x][y] = true, ans.push_back('1');
for (int i = 0; i < 4; i++) {
bool flag = false;
for (int j = 1; j < n; j++) {
int nx = x + dx[i] * j, ny = y + dy[i] * j;
if (nx < 0 || nx >= n) break;
if (ny < 0 || ny >= n) break;
flag |= a[nx][ny];
}
if (!flag) continue;
int nx = x + dx[i], ny = y + dy[i];
if (!marked[nx][ny]) {
ans.push_back(c[i]);
dfs(nx, ny);
ans.push_back(op[i]);
}
}
if (a[x][y]) a[x][y] = false, ans.push_back('2');
}
int main() {
cin >> n >> xs >> ys;
xs--;
ys--;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> a[i][j];
dfs(xs, ys);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i][j]) {
cout << "NO" << endl;
return 0;
}
cout << "YES\n" << ans << endl;
}
| 2,400 | CPP |
n=input()
n1=n.split()
n=int(n1[0])
k=int(n1[1])
str1=""
str2=""
if(k==0 or k==n):
str1="0 "
else:
str1="1 "
if(k==0):
str2="0"
else:
if(k<=n/3):
str2=str(2*k)
else:
str2=str(n-k)
print(str1+str2) | 1,200 | PYTHON3 |
T=int(input())
while T:
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
b=[]
c=[]
if a[0]!=0:
print("0")
else:
b = list(set(a))
b.sort()
f=x=y=0
for i in range(len(b)):
if i not in b:
x=i
f=1
break
if f==0:
x = b[-1]+1
for i in b:
a.remove(i)
f=0
for i in range(len(a)):
if i not in a:
y=i
f=1
break
if f==0:
if(len(a)>0):
y = a[-1]+1
print(x+y)
T-=1 | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long p, y;
bool prime(long long x) {
for (long long k = 2; k <= p && k * k <= x; k++) {
if (x % k == 0) return 0;
}
return 1;
}
int main() {
cin >> p >> y;
for (long long i = y; i > p; i--) {
if (prime(i)) {
cout << i << endl;
return 0;
}
}
cout << "-1" << endl;
return 0;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
vector<int> G[maxn];
vector<int> inv[maxn];
int n, m;
int vis[2][maxn];
int in[maxn];
inline int topo_sort(vector<int>* G, int* in) {
stack<int> S;
for (auto i = (0); i < (n); ++i) {
if (in[i] == 0) S.push(i);
}
int cnt = 0;
for (; !S.empty();) {
int u = S.top();
S.pop();
++cnt;
for (auto v : G[u]) {
if (--in[v] == 0) S.push(v);
}
}
return cnt == n;
}
inline void dfs(int u, vector<int>* g, int* vis) {
if (vis[u]) return;
vis[u] = 1;
for (auto v : g[u]) {
if (vis[v]) continue;
dfs(v, g, vis);
}
}
inline void solve() {
int ans = 0;
string o;
for (auto i = (0); i < (n); ++i) {
if (!vis[0][i] && !vis[1][i]) {
o.push_back('A');
ans++;
} else
o.push_back('E');
dfs(i, G, vis[0]);
dfs(i, inv, vis[1]);
}
printf("%d\n%s\n", ans, o.c_str());
}
int main(void) {
scanf("%d", &(n)), scanf("%d", &(m));
for (auto i = (0); i < (m); ++i) {
int u, v;
scanf("%d", &(u)), scanf("%d", &(v));
u--, v--;
G[u].push_back(v);
inv[v].push_back(u);
in[v]++;
}
if (!topo_sort(G, in)) return puts("-1"), 0;
solve();
return 0;
}
| 2,600 | CPP |
if __name__ == '__main__':
ans = 0
to_add = 1
user_input = tuple(input())
for char in user_input:
if char == "b":
ans += to_add - 1
ans %= 1000000007
else:
to_add *= 2
to_add %= 1000000007
print(ans)
| 1,400 | PYTHON3 |
s = input()
n_lower = 0
for l in list(s):
if l.islower():
n_lower += 1
if n_lower >= len(list(s)) - n_lower:
print(s.lower())
else:
print(s.upper())
| 800 | PYTHON3 |
n = int(input())
i = 0
h = 0
while i < n:
a, b, c = map(int, input().split())
if a + b + c >= 2:
h = h + 1
i = i + 1
print(h) | 800 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int a, b, c, ara[12], ar[15], i, j, k, n = 4, temp, temp1, temp2;
for (i = 0; i < n; i++) {
scanf("%d", &ara[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (ara[i] < ara[j]) {
temp = ara[i];
ara[i] = ara[j];
ara[j] = temp;
}
}
}
if (ara[0] == ara[1] == ara[2] == ara[3]) {
k = ara[0] - ara[1];
printf("%d %d %d", k, k, k);
} else {
int l = ara[0] - ara[3];
int m = ara[0] - ara[2];
int t = ara[0] - ara[1];
printf("%d %d %d", l, m, t);
}
return 0;
}
| 800 | CPP |
n,m = [int(x) for x in input().split()]
a = [[int(x) for x in input().split()] for i in range(m) ]
#print(a)
firstStage = [1]*m
eachperson = {}
for i in range(1,n+1):
eachperson[i] = 0
#print(firstStage)
mav1 = 1
mav2 = 0
#print(type(eachperson),eachperson)
for i in range(m):
maxV1 = 0
for j in range(n):
if a[i][j] > maxV1:
maxV1 = a[i][j]
firstStage[i] = j+1
eachperson[ firstStage[i] ] += 1
for x in eachperson:
if eachperson[x] > mav2:
mav1 = x
mav2 = eachperson[x]
#print(eachperson)
print(mav1) | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const long long infll = (long long)1.01e18;
const long double eps = 1e-9;
const long double pi = acos((long double)-1);
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x; }
const int mod = (int)1e9 + 7;
int mul(int a, int b) { return (long long)a * b % mod; }
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
const int maxn = (int)5e5 + 5;
int f[maxn];
void precalc() {
f[0] = 1;
for (int i = 1; i < maxn; i++) {
f[i] = mul(f[i - 1], i);
}
}
int n;
vector<int> g[maxn];
int p[maxn];
bool read() {
if (scanf("%d", &n) < 1) {
return false;
}
for (int i = 0; i < n; i++) {
g[i].clear();
}
for (int i = 0; i < n - 1; i++) {
int v, u;
scanf("%d%d", &v, &u);
v--;
u--;
g[v].push_back(u);
g[u].push_back(v);
}
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
p[i]--;
}
return true;
}
const int logn = 20;
int pr[maxn][logn];
int q[maxn];
int used[maxn];
int tin[maxn], tout[maxn], t;
void dfs0(int v, int p) {
tin[v] = t++;
pr[v][0] = (p == -1 ? v : p);
for (int i = 1; i < logn; i++) {
pr[v][i] = pr[pr[v][i - 1]][i - 1];
}
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == p) {
continue;
}
dfs0(u, v);
}
tout[v] = t;
}
bool anc(int v, int u) { return tin[v] <= tin[u] && tout[u] <= tout[v]; }
int get(int v, int u) {
if (v == u) {
return v;
}
for (int i = logn - 1; i >= 0; i--) {
if (!anc(pr[v][i], u)) {
v = pr[v][i];
}
}
return v;
}
int ls[maxn], rs[maxn];
int nxt[maxn];
int res;
bool dfs(int v, int pp) {
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == pp) {
continue;
}
if (!dfs(u, v)) {
return false;
}
}
auto &l = ls[v];
auto &r = rs[v];
l = -1;
r = -1;
int lid = -1;
if (p[v] != -1 && !anc(v, p[v])) {
r = p[v];
}
if (q[v] != -1 && !anc(v, q[v])) {
l = q[v];
lid = v;
}
nxt[v] = (p[v] == -1 || !anc(v, p[v]) ? p[v] : get(p[v], v));
int k = 0;
if (q[v] == -1 || !anc(v, q[v])) {
k++;
}
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == pp) {
continue;
}
if (ls[u] != -1 && !anc(v, ls[u])) {
if (l == -1) {
l = ls[u];
lid = u;
}
if (l != ls[u]) {
return false;
}
}
if (rs[u] != -1 && !anc(v, rs[u])) {
if (r == -1) {
r = rs[u];
}
if (r != rs[u]) {
return false;
}
}
if (ls[u] == -1 || !anc(v, ls[u])) {
k++;
}
nxt[u] = (rs[u] == -1 || !anc(v, rs[u]) ? rs[u] : get(rs[u], v));
}
{
used[v] = false;
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == pp) {
continue;
}
used[u] = false;
}
if (k == 0) {
if (pp != -1) {
return false;
}
int w = v;
while (w != -1 && !used[w] && anc(v, w)) {
used[w] = true;
w = nxt[w];
}
if (w == -1 || !anc(v, w)) {
return false;
}
if (!used[v]) {
return false;
}
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == pp) {
continue;
}
if (!used[u]) {
return false;
}
}
return true;
}
if (lid != -1) {
int w = lid;
while (w != -1 && !used[w] && anc(v, w)) {
used[w] = true;
w = nxt[w];
}
if (w != -1) {
if (anc(v, w)) {
return false;
}
if (!used[v]) {
return false;
}
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == pp) {
continue;
}
if (!used[u]) {
return false;
}
}
return true;
}
}
if (!used[v] && (q[v] == -1 || !anc(v, q[v]))) {
int w = v;
while (w != -1 && !used[w] && anc(v, w)) {
used[w] = true;
w = nxt[w];
}
if (w != -1 && anc(v, w)) {
return false;
}
}
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == pp) {
continue;
}
if (!used[u] && (ls[u] == -1 || !anc(v, ls[u]))) {
int w = u;
while (w != -1 && !used[w] && anc(v, w)) {
used[w] = true;
w = nxt[w];
}
if (w != -1 && anc(v, w)) {
return false;
}
}
}
if (!used[v]) {
return false;
}
for (int i = 0; i < ((int)(g[v]).size()); i++) {
int u = g[v][i];
if (u == pp) {
continue;
}
if (!used[u]) {
return false;
}
}
}
if (k == 1) {
return true;
}
if (pp == -1) {
if (k) {
k--;
res = mul(res, f[k]);
}
return res;
}
if (l != -1) {
k--;
}
if (r != -1) {
k--;
}
res = mul(res, f[k]);
return true;
}
void solve() {
for (int i = 0; i < n; i++) {
q[i] = -1;
}
for (int i = 0; i < n; i++) {
if (p[i] != -1) {
q[p[i]] = i;
}
}
t = 0;
dfs0(0, -1);
res = 1;
if (!dfs(0, -1)) {
res = 0;
}
printf("%d\n", res);
}
int main() {
precalc();
while (read()) {
solve();
}
return 0;
}
| 3,300 | CPP |
from bisect import bisect_left
for _ in range(int(input())):
n = int(input())
c = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
dp = [-1, 2 + abs(a[1] - b[1]) + c[1] - 1]
for i in range(2, n):
if a[i] == b[i]:
dp.append(2 + c[i] - 1)
else:
dp.append(max(dp[-1] + 2 + c[i] + min(a[i], b[i]) - 1 - max(a[i], b[i]), c[i] + 1 + abs(a[i] - b[i])))
# print(dp)
print(max(dp))
| 1,600 | PYTHON3 |
__copyright__ = ''
__author__ = 'Son-Huy TRAN'
__email__ = "[email protected]"
__doc__ = ''
__version__ = '1.0'
def is_definitely_unlucky(n: int, ticket: str) -> bool:
digits = list(ticket)
left = sorted(digits[:n])
right = sorted(digits[n:])
if left[0] == right[0]:
return False
condition = left[0] < right[0]
for i in range(1, n):
if (left[i] == right[i]) or (left[i] < right[i]) != condition:
return False
return True
def main() -> int:
n = int(input())
ticket = input()
print('YES' if is_definitely_unlucky(n, ticket) else 'NO')
return 0
if __name__ == '__main__':
exit(main()) | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int s[40][40];
void init() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &s[i][j]);
m = n / 2 + 1;
}
int t[40][40];
int findMax(int bt) {
int piv = 0, rst = 0;
for (int i = 0; i < m; i++) t[i][m - 1] = (bt & (1 << i)) ? 1 : 0;
for (int i = m; i < n; i++) t[i][m - 1] = t[i - m][m - 1] ^ t[m - 1][m - 1];
for (int i = 0; i < n; i++) piv += (t[i][m - 1] ? -s[i][m - 1] : s[i][m - 1]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m - 1; j++) {
if (t[i][m - 1])
t[i][j] = s[i][j] - s[i][j + m];
else
t[i][j] = s[i][j] + s[i][j + m];
}
}
for (int j = 0; j < m - 1; j++) {
int t1 = (t[m - 1][m - 1]) ? s[m - 1][j] - s[m - 1][j + m]
: s[m - 1][j] + s[m - 1][j + m];
int ac1 = -t1, ac2 = t1;
for (int i = 0; i < m - 1; i++) {
ac1 += abs(t[i][j] - t[i + m][j]);
ac2 += abs(t[i][j] + t[i + m][j]);
}
rst += max(ac1, ac2);
}
return piv + rst;
}
void process() {
int mx = findMax(0);
for (int i = 1; i < (1 << m); i++) {
mx = max(mx, findMax(i));
}
printf("%d\n", mx);
}
int main() {
init();
process();
return 0;
}
| 2,900 | CPP |
n=int(input())
lr=list(map(int,input().split()))
lc=list(map(int,input().split()))
sr=sum(lr)
m1=max(lc)
lc.remove(m1)
m2=max(lc)
mc=m1+m2
if mc>=sr:
print("YES")
else:
print("NO") | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool home = false;
const int N = (int)1e6 + 7;
const int INF = (int)1e9;
int n;
int a[N][2];
int dp[N][2][2];
vector<int> par[N][2][2];
int main() {
if (!home) {
ios::sync_with_stdio(0);
cin.tie(0);
}
int tests;
cin >> tests;
while (tests--) {
cin >> n;
for (int i = 0; i <= n + 1; i++) {
dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = INF;
par[i][0][0] = par[i][0][1] = par[i][1][0] = par[i][1][1] = {-1, -1, -1};
}
for (int i = 1; i <= n; i++) {
cin >> a[i][0];
a[i][1] = -a[i][0];
}
function<void(int, int, int, int, int, int)> upd = [&](int i, int m1,
int m2, int c1,
int c2, int cur) {
assert(m1 == a[i][0] || m1 == a[i][1] || m2 == a[i][0] || m2 == a[i][1]);
if (m1 == a[i][0] || m1 == a[i][1]) {
if (m2 < dp[i][0][(m1 == a[i][1])]) {
dp[i][0][(m1 == a[i][1])] = m2;
par[i][0][(m1 == a[i][1])] = {c1, c2, cur};
}
} else {
if (m1 < dp[i][1][(m2 == a[i][1])]) {
dp[i][1][(m2 == a[i][1])] = m1;
par[i][1][(m2 == a[i][1])] = {c1, c2, cur};
}
}
};
upd(1, a[1][1], -INF, 0, 0, 0);
for (int i = 1; i < n; i++) {
for (int pos = 0; pos <= 1; pos++) {
for (int who = 0; who <= 1; who++) {
int m1 = a[i][who], m2 = dp[i][pos][who];
if (pos) {
swap(m1, m2);
}
for (int t = 0; t <= 1; t++) {
int x = a[i + 1][t];
if (x > m1) {
upd(i + 1, x, m2, pos, who, x);
} else {
if (x > m2) {
upd(i + 1, m1, x, pos, who, x);
}
}
}
}
}
}
bool good = false;
int p, w;
for (int pos = 0; pos <= 1; pos++) {
for (int who = 0; who <= 1; who++) {
if (dp[n][pos][who] != INF) {
good = true;
p = pos;
w = who;
}
}
}
if (!good) {
cout << "NO\n";
continue;
}
vector<int> sol;
for (int i = n; i >= 2; i--) {
sol.push_back(par[i][p][w][2]);
int p2, w2;
p2 = par[i][p][w][0];
w2 = par[i][p][w][1];
assert(p2 != -1);
assert(w2 != -1);
p = p2;
w = w2;
}
sol.push_back(a[1][1]);
cout << "YES\n";
reverse(sol.begin(), sol.end());
for (auto &x : sol) {
cout << x << " ";
}
cout << "\n";
}
return 0;
}
| 2,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[1000001];
int dp[1000001];
int dp1[1000001];
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cout << 1 << " ";
}
cout << endl;
}
}
| 800 | CPP |
total = int(input()) + 1
current = sum(map(int, input().split()))
print(sum(((current + i) % total != 1) for i in range(1, 6)))
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long t, n, i, j, k;
int main() {
cin >> n >> k;
long long a[n];
set<int> s;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
deque<long long> l;
for (i = 1; i <= n; i++) {
if (l.size() < k) {
if (s.find(a[i]) == s.end()) {
a[i] = abs(a[i]);
l.push_front(a[i]);
s.insert(a[i]);
}
} else {
if (s.find(a[i]) == s.end()) {
s.erase(l.back());
l.pop_back();
l.push_front(a[i]);
s.insert(a[i]);
}
}
}
cout << l.size() << endl;
for (auto it : l) {
cout << it << " ";
}
}
| 1,000 | CPP |
#include <bits/stdc++.h>
template <typename _T>
inline bool read(_T& x) {
x = 0;
_T y = 1;
char c = getchar();
while ((c < '0' || '9' < c) && c != EOF) {
if (c == '-') y = -1;
c = getchar();
}
if (c == EOF) return false;
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
x *= y;
return true;
}
int N, ts, top;
long long a[600007 << 1], tag[600007 << 2], sum[600007 << 2];
long long res;
struct seg {
long long l, r, h, v;
bool operator<(const seg& rhs) const { return h < rhs.h; }
} s[600007];
inline void update(int x, int l, int r) {
if (tag[x])
sum[x] = a[r + 1] - a[l];
else
sum[x] = sum[x << 1] + sum[x << 1 | 1];
}
void modify(int x, int l, int r, int ql, int qr, long long val) {
if (ql <= l && r <= qr) return tag[x] += val, update(x, l, r);
int mid = l + r >> 1;
if (ql <= mid) modify(x << 1, l, mid, ql, qr, val);
if (mid < qr) modify(x << 1 | 1, mid + 1, r, ql, qr, val);
update(x, l, r);
}
int main() {
read(N);
int a1, a2, b1, b2;
for (int i = 1; i <= N; ++i) {
read(a1), read(b1), read(a2), read(b2);
if (a1 > a2) std::swap(a1, a2);
if (b1 > b2) std::swap(b1, b2);
a2++, b2++;
s[++ts] = {b1, b2, a1, 1};
s[++ts] = {b1, b2, a2, -1};
a[++top] = b1, a[++top] = b2;
}
std::sort(a + 1, a + top + 1);
top = std::unique(a + 1, a + top + 1) - a - 1;
std::sort(s + 1, s + ts + 1);
int l, r;
for (int i = 1; i <= ts - 1; ++i) {
l = std::lower_bound(a + 1, a + top + 1, s[i].l) - a;
r = std::lower_bound(a + 1, a + top + 1, s[i].r) - a - 1;
modify(1, 1, top, l, r, s[i].v);
res += sum[1] * (s[i + 1].h - s[i].h);
}
printf("%lld\n", res);
return 0;
}
| 2,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<pair<long long, long long>> bish(n);
for (long long i = 0; i < (n); ++i) {
cin >> bish[i].first;
cin >> bish[i].second;
}
map<long long, long long> left;
map<long long, long long> right;
for (long long i = 0; i < (n); ++i) {
pair<long long, long long> temp = bish[i];
temp.second -= temp.first;
left[temp.second]++;
temp = bish[i];
temp.second -= 1000 - temp.first;
right[temp.second]++;
}
long long result = 0;
for (pair<long long, long long> p : left) {
result += p.second * (p.second - 1) / 2;
}
for (pair<long long, long long> p : right) {
result += p.second * (p.second - 1) / 2;
}
cout << result;
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vii = vector<ii>;
using vvii = vector<vii>;
const int INF = 2000000000;
const ll LLINF = 9000000000000000000;
template <class T, T (*op)(T, T), T ident>
struct SegmentTree {
int n;
vector<T> tree;
SegmentTree(vector<T> &init) : n(init.size()), tree(2 * n, ident) {
copy(init.begin(), init.end(), tree.begin() + n);
for (int j = n - 1; j > 0; --j) tree[j] = op(tree[2 * j], tree[2 * j + 1]);
}
void update(int i, T val) {
for (tree[i + n] = val, i = (i + n) / 2; i > 0; i /= 2)
tree[i] = op(tree[2 * i], tree[2 * i + 1]);
}
T query(int l, int r) {
T lhs = T(ident), rhs = T(ident);
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) lhs = op(lhs, tree[l++]);
if (!(r & 1)) rhs = op(tree[r--], rhs);
}
return op(l == r ? op(lhs, tree[l]) : lhs, rhs);
}
};
ll mmin(ll l, ll r) { return l < r ? l : r; }
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
ll D, N;
int M;
cin >> D >> N >> M;
vii XP(M + 1, {0LL, 0LL});
for (int i = 1; i <= M; ++i) cin >> XP[i].first >> XP[i].second;
++M;
++M;
XP.push_back({D, 0LL});
assert(M == int(XP.size()));
sort(XP.begin(), XP.end());
vi X(M, 0LL), P(M, 0LL);
for (int i = 0; i < M; ++i) {
X[i] = XP[i].first;
P[i] = XP[i].second;
}
bool pos = true;
for (int i = 1; i < M; ++i) pos = pos && X[i] - X[i - 1] <= N;
if (!pos) {
cout << -1 << endl;
return 0;
}
SegmentTree<ll, mmin, LLINF> st(P);
int p = 0;
ll fuel = N, cost = 0LL;
while (p != M - 1) {
int l = p + 1, r = M - 1;
while (l < r) {
int m = (l + r + 1) / 2;
if (X[m] - X[p] > N)
r = m - 1;
else
l = m;
}
int R = l;
ll mn = st.query(p + 1, R);
if (mn < P[p]) {
l = p + 1;
r = R;
while (l < r) {
int m = (l + r) / 2;
if (st.query(p + 1, m) < P[p])
r = m;
else
l = m + 1;
}
cost += max(0LL, X[l] - X[p] - fuel) * P[p];
fuel += max(0LL, X[l] - X[p] - fuel);
} else {
l = p + 1;
r = R;
while (l < r) {
int m = (l + r) / 2;
if (st.query(p + 1, m) > mn)
l = m + 1;
else
r = m;
}
cost += (N - fuel) * P[p];
fuel = N;
}
assert(p < l);
fuel -= X[l] - X[p];
p = l;
}
cout << cost << endl;
return 0;
}
| 2,200 | CPP |
n = int(input())
heights = [int(x) for x in input().split(' ')]
if n == 1:
print(n)
else:
maximum_possible = 0
for i in range(n):
current_possible = 0
if i > 0:
for k in reversed(range(i)):
if heights[k] <= heights[k + 1]:
current_possible = current_possible + 1
else:
break
if i < n - 1:
for j in range(i + 1, n):
if heights[j] <= heights[j - 1]:
current_possible = current_possible + 1
else:
break
if current_possible + 1 > maximum_possible:
maximum_possible = current_possible + 1
print (maximum_possible)
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n;
std::string a;
std::cin >> a >> n;
auto maxlen = a.size();
std::vector<std::string> b(n);
for (auto& x : b) {
std::cin >> x;
maxlen = std::max(maxlen, x.size());
}
std::array<int, 10> cost;
for (auto& x : cost) std::cin >> x;
auto const l = int(maxlen + 1);
auto lead = int(l - a.size());
assert(a[0] != '0');
int const nilmark = '0' - 1;
a.insert(begin(a), lead, nilmark);
for (auto& x : b) {
assert(x[0] != '0');
x.insert(begin(x), l - x.size(), nilmark);
}
std::vector<std::vector<int>> dp(l + 1, std::vector<int>(n + 1, -1));
dp[l][0] = 0;
std::vector<int> ind(n);
std::iota(begin(ind), end(ind), 0);
for (int i = l; i; --i) {
if (i < l)
std::stable_sort(begin(ind), end(ind), [&](int x, int y) {
return std::max('0', b[x][i]) > std::max('0', b[y][i]);
});
char const na = std::max('0', a[i - 1]);
bool anil = a[i - 1] == nilmark;
std::array<int, 10> cn{}, cc{};
for (auto const& x : b) {
auto c = x[i - 1];
if (not anil or c != nilmark) ++cn[std::max(0, c - '0')];
}
for (int ncarry = 0;;) {
auto costi = dp[i][ncarry];
if (costi >= 0) {
auto const process = [&](int digit) {
int newcost = costi, new_ncarry = 0;
auto const proc2 = [&](int newdigit, int cnt) {
if (newdigit >= 10) {
newdigit -= 10;
new_ncarry += cnt;
}
newcost += cost[newdigit] * cnt;
};
for (int k = 0; k < 10; ++k) {
proc2(k + digit, cn[k]);
proc2(k + digit + 1, cc[k]);
}
dp[i - 1][new_ncarry] = std::max(dp[i - 1][new_ncarry], newcost);
};
if (na == '?')
for (int digit = (i - 1 == lead ? 1 : 0); digit < 10; ++digit)
process(digit);
else
process(na - '0');
}
if (ncarry == n) {
assert(std::all_of(begin(cn), end(cn), [](int x) { return x == 0; }));
break;
}
{
auto c = b[ind[ncarry]][i - 1];
if (not anil or c != nilmark) --cn[std::max(0, c - '0')];
}
++cc[std::max(0, b[ind[ncarry]][i - 1] - '0')];
++ncarry;
}
}
std::cout << dp[0][0] << '\n';
}
| 3,000 | CPP |
def main():
sum = input().split("+")
print("+".join(sorted(sum)))
if __name__ == "__main__":
main() | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int back[1000001];
int res[1000001];
int cnt[1000001];
int cnt2[1000001];
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
int s = 0;
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
if (a == 0)
s = b;
else {
back[a] = b;
}
cnt[a]++;
cnt2[b]++;
}
int f = 0;
for (int i = 1; i <= 1000000; ++i) {
if (cnt[i] + cnt2[i] == 1 && cnt[i] == 1) {
f = i;
break;
}
}
cout << f << " " << s;
for (int i = 1; i < (n + 1) / 2; ++i) {
if (back[f]) {
cout << " " << back[f];
f = back[f];
}
if (back[s]) {
cout << " " << back[s];
s = back[s];
} else
break;
}
cout << "\n";
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
char s[4][123];
int main(int argc, char const *argv[]) {
int n;
std::cin >> n;
if (n & 1) {
for (int i = 1, j = 0; i < n; i += 2, j++) {
if (j & 1) {
s[0][i] = s[0][i + 1] = 'b';
s[1][i] = s[1][i + 1] = 'c';
} else {
s[0][i] = s[0][i + 1] = 'd';
s[1][i] = s[1][i + 1] = 'e';
}
}
for (int i = 0, j = 0; i < n; i += 2, j++) {
if (j & 1) {
s[2][i] = s[2][i + 1] = 'f';
s[3][i] = s[3][i + 1] = 'g';
} else {
s[2][i] = s[2][i + 1] = 'h';
s[3][i] = s[3][i + 1] = 'i';
}
}
s[0][0] = s[1][0] = 'a';
s[2][n - 1] = s[3][n - 1] = 'z';
} else {
for (int i = 1, j = 0; i < n; i += 2, j++) {
if (j & 1) {
s[1][i] = s[1][i + 1] = 'b';
s[2][i] = s[2][i + 1] = 'c';
} else {
s[1][i] = s[1][i + 1] = 'd';
s[2][i] = s[2][i + 1] = 'e';
}
}
for (int i = 0, j = 0; i < n; i += 2, j++) {
if (j & 1) {
s[0][i] = s[0][i + 1] = 'f';
s[3][i] = s[3][i + 1] = 'g';
} else {
s[0][i] = s[0][i + 1] = 'h';
s[3][i] = s[3][i + 1] = 'i';
}
}
s[2][0] = s[1][0] = 'a';
s[2][n - 1] = s[1][n - 1] = 'z';
}
s[0][n] = s[1][n] = 0;
s[2][n] = s[3][n] = 0;
for (int i = 0; i < 4; i++) {
std::cout << s[i] << '\n';
}
return 0;
}
| 1,300 | CPP |
for _ in range(int(input())):
l=input().split()
if l[1]=='1':
print(l[0])
else:
l[1]=int(l[1])
x=int(l[0])
for i in range(2,l[1]+1):
x=x+int(min(list(str(x))))*int(max(list(str(x))))
if '0' in str(x):
break
print(x)
| 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 3;
const long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
int a[4] = {1, 2, 3, 5};
cout << k * (6 * (n - 1) + 5) << "\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
cout << k * (6 * i + a[j]) << " ";
}
cout << "\n";
}
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10, mod = 998244353;
struct node {
long long l, r, cnt;
node() { l = r = cnt = 0; }
};
struct ordered_set {
node seg[N << 2];
ordered_set() { build(1, 0, N); }
void build(long long v, long long l, long long r) {
seg[v].l = l;
seg[v].r = r;
seg[v].cnt = 0;
if (l + 1 == r) return;
long long mid = (l + r) / 2;
build(2 * v, l, mid);
build(2 * v + 1, mid, r);
}
long long get(long long v, long long l, long long r) {
if (r <= seg[v].l || seg[v].r <= l) return 0;
if (l <= seg[v].l && seg[v].r <= r) return seg[v].cnt;
return get(2 * v, l, r) + get(2 * v + 1, l, r);
}
void update(long long v) { seg[v].cnt = seg[2 * v].cnt + seg[2 * v + 1].cnt; }
void change(long long v, long long l, long long x) {
if (seg[v].l + 1 == seg[v].r) {
seg[v].cnt += x;
return;
}
if (l < seg[2 * v].r)
change(2 * v, l, x);
else
change(2 * v + 1, l, x);
update(v);
}
long long find(long long v, long long x) {
if (seg[v].l + 1 == seg[v].r) return seg[v].l;
if (x <= seg[2 * v].cnt)
return find(2 * v, x);
else
return find(2 * v + 1, x - seg[2 * v].cnt);
}
void insert(long long v) { change(1, v, 1); }
void del(long long x) { change(1, x, -1); }
long long indx(long long v) { return get(1, 0, v) + 1; }
long long find_by_order(long long v) { return find(1, v); }
long long size() { return seg[1].cnt; }
void clear() { build(1, 0, N); }
} st;
long long n, ans, lft, rgh, total, ar[N], mark[N];
long long pw(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return res;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> ar[i];
if (ar[i] == -1)
rgh++, total++;
else
mark[ar[i]] = 1;
}
for (long long i = 1; i <= n; i++)
if (!mark[i]) st.insert(i);
for (long long i = 0; i < n; i++) {
if (ar[i] == -1) {
lft++;
rgh--;
continue;
}
st.insert(ar[i]);
long long indx = st.indx(ar[i]);
long long tmp = (lft * (st.size() - indx)) % mod;
tmp = (tmp * pw(total, mod - 2)) % mod;
ans = (ans + tmp) % mod;
tmp = (rgh * (indx - 1)) % mod;
tmp = (tmp * pw(total, mod - 2)) % mod;
ans = (ans + tmp) % mod;
st.del(ar[i]);
}
long long tmp = (total * (total - 1)) % mod;
tmp = (tmp * pw(4LL, mod - 2)) % mod;
ans = (ans + tmp) % mod;
st.clear();
for (long long i = 0; i < n; i++) {
if (ar[i] == -1) continue;
st.insert(ar[i]);
long long indx = st.indx(ar[i]);
ans = (ans + (st.size() - indx)) % mod;
}
ans %= mod;
cout << ans;
}
| 2,300 | CPP |
input();l = ''.join(input().split())
ctr = 0
ans = 0
skip = False
for i in range(len(l)-2):
if (i >= 2 and not skip) or i == len(l)-3:
if l[i:i+3] == '101' and l[i-2:i+1] != '101':
ctr = 1
skip = True
continue
elif l[i:i+3] == '101' and l[i-2:i+1] == '101':
ctr += 1
skip = True
continue
else:
ans += -(-ctr//2)
# print(ctr)
ctr = 0
elif i < 2 and not skip:
if l[i:i+3] == '101':
ctr += 1
skip = True
continue
if i == len(l)-3:
ans += ctr
ctr = 0
skip = False
# print(ctr,'out')
print(ans -(-ctr//2)) | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 5e5 + 10;
int n, top, stk[N], a[N];
char s[N];
long long pre[N];
inline long long calc2(int l, int r) {
if (l > r) return 0;
return 1ll * (l + r) * (r - l + 1) / 2;
}
inline long long calc(int l, int r) {
if (l == 0) return 0;
return 1ll * a[r] * (r - l) + calc2(1, a[l] - a[r] - 1);
}
int main() {
scanf("%d%s", &n, s + 1);
int l = 0;
long long ans = 0;
for (register int i = (1); i <= (n); i++) {
if (s[i] == '1') {
if (!l) {
a[l = i] = 1, stk[++top] = i,
pre[top] = pre[top - 1] + calc(stk[top - 1], stk[top]);
} else {
a[l]++, pre[top] = pre[top - 1] + calc(stk[top - 1], stk[top]);
}
while (top > 1 && a[stk[top]] >= a[stk[top - 1]]) {
stk[top - 1] = stk[top];
--top, pre[top] = pre[top - 1] + calc(stk[top - 1], stk[top]);
}
} else
l = 0;
ans += pre[top];
ans += calc2(1, a[stk[top]] - 1);
ans += 1ll * a[stk[1]] * stk[1];
}
printf("%lld\n", ans);
}
| 2,400 | CPP |
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
ans = n//k*k
if n%k > int(k/2):
ans += int(k/2)
else:
ans = n
print(ans)
| 900 | PYTHON3 |
def toBase26(x):
result = []
while x > 0:
result.append(chr((x-(x-1)//26*26 + 64)))
x = (x-1)//26
return result
def fromBase26(s):
result = 0
l = len(s)
for i in range(l):
result += (ord(s[l-i-1])-64)*26**i
return result
n = int(input())
strList = []
for i in range(n):
strList.append(input())
for line in strList:
i = line[2:].find('C')
if line[1].isdigit() and i != -1:
row = int(line[1:i+2])
col = toBase26(int(line[i+3:]))
l = len(col)
for i in range(l): print(col[l-i-1], sep='', end='')
print(row)
else:
i = 0
while line[i].isalpha():
i += 1
col = fromBase26(line[:i])
row = int(line[i:])
print('R{0}C{1}'.format(row, col))
| 1,600 | PYTHON3 |
n, m = map(int, input().split())
points = list(zip(map(int, input().split()), range(n)))
points.sort()
res = []
curr = 0
for point in points:
res.append((point[1], (curr + 1) % 2))
curr += 1
res.sort()
for point in res:
print(point[1], end=' ') | 1,600 | PYTHON3 |
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
for i in a[::-1]:
print(i, end = " ")
print() | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500100, maxm = 4 * maxn;
int n, u, v, tot, ans;
vector<int> g[maxn], f[maxn];
int fa[maxm], x[maxm], y[maxm], son[maxm][2], MAX[maxm], val[maxm], rev[maxm],
size[maxm];
inline void down(int x) {
if (!x) return;
if (rev[x]) {
swap(son[x][0], son[x][1]);
if (son[x][0]) rev[son[x][0]] ^= 1;
if (son[x][1]) rev[son[x][1]] ^= 1;
}
rev[x] = 0;
}
inline void update(int x) {
down(son[x][0]), down(son[x][1]);
size[x] = size[son[x][0]] + size[son[x][1]] + 1;
MAX[x] = x;
if (son[x][0] && val[MAX[son[x][0]]] > val[MAX[x]]) MAX[x] = MAX[son[x][0]];
if (son[x][1] && val[MAX[son[x][1]]] > val[MAX[x]]) MAX[x] = MAX[son[x][1]];
}
inline bool truefa(int x) {
return (fa[x] && (x == son[fa[x]][0] || x == son[fa[x]][1]));
}
inline void rotate(int x) {
int y = fa[x], z = fa[y], k = x == son[y][1];
if (truefa(y)) son[z][y == son[z][1]] = x;
fa[x] = z;
if (son[x][k ^ 1]) fa[son[x][k ^ 1]] = y;
son[y][k] = son[x][k ^ 1];
son[x][k ^ 1] = y, fa[y] = x;
update(y);
}
inline void splay(int x) {
while (truefa(x)) {
int y = fa[x], z = fa[y];
if (!truefa(y))
down(y), down(x), rotate(x);
else {
down(z), down(y), down(x);
if ((x == son[y][0]) ^ (y == son[z][0]))
rotate(x), rotate(x);
else
rotate(y), rotate(x);
}
}
down(x), update(x);
}
inline int access(int x) {
int p = x, q = 0;
for (; p; p = fa[q]) splay(p), son[p][1] = q, update(q = p);
return q;
}
inline int findroot(int x) {
for (x = access(x); down(x), update(x), son[x][0]; x = son[x][0])
;
splay(x);
return x;
}
inline void makeroot(int x) {
int p = access(x);
rev[p] ^= 1;
splay(x);
}
inline void link(int x, int y) {
if (findroot(x) == findroot(y)) return;
makeroot(x);
fa[x] = y;
access(x);
}
inline void cut(int x, int y) {
if (findroot(x) != findroot(y)) return;
makeroot(x);
access(y);
splay(y);
fa[son[y][0]] = 0, son[y][0] = 0;
update(y);
}
inline int get(int x, int y) {
makeroot(x);
access(y);
splay(x);
return MAX[x];
}
char ch;
void read(int &rt) {
rt = 0;
ch = getchar();
while (ch > '9' || ch < '0') ch = getchar();
while (ch <= '9' && ch >= '0') rt = rt * 10 + ch - '0', ch = getchar();
}
int main() {
read(n);
for (int i = 1; i < n; i++) {
read(u), read(v);
if (u > v) swap(u, v);
g[u].push_back(v);
}
for (int i = 1; i < n; i++) {
read(u), read(v);
if (u > v) swap(u, v);
f[u].push_back(v);
}
tot = n;
for (int i = 1; i <= n; i++) MAX[i] = i;
for (int i = 1; i <= n; i++) {
sort(g[i].begin(), g[i].end()), sort(f[i].begin(), f[i].end());
for (int j = 0, k = 0; j < (int)g[i].size(); j++) {
while (k + 1 < (int)f[i].size() && f[i][k] < g[i][j]) k++;
x[++tot] = i, y[tot] = g[i][j];
if (f[i].size() == 0 || f[i][k] != g[i][j]) val[tot] = 1, ans++;
MAX[tot] = tot;
link(i, tot), link(tot, g[i][j]);
}
}
cout << ans << endl;
for (int i = 1; i <= n; i++) {
for (int j = 0, k = 0; k < (int)f[i].size(); k++) {
while (j + 1 < (int)g[i].size() && g[i][j] < f[i][k]) j++;
if (g[i].size() > 0 && f[i][k] == g[i][j]) continue;
u = i, v = f[i][k];
int t = get(u, v);
cut(t, x[t]), cut(t, y[t]);
x[++tot] = u, y[++tot] = v;
val[tot] = 0, MAX[tot] = tot;
link(u, tot), link(v, tot);
printf("%d %d %d %d\n", x[t], y[t], u, v);
}
}
return 0;
}
| 3,200 | CPP |
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 14 12:40:58 2019
@author: Hamadeh
"""
class cinn:
def __init__(self):
self.x=[]
def cin(self,t=int):
if(len(self.x)==0):
a=input()
self.x=a.split()
self.x.reverse()
return self.get(t)
def get(self,t):
return t(self.x.pop())
def clist(self,n,t=int): #n is number of inputs, t is type to be casted
l=[0]*n
for i in range(n):
l[i]=self.cin(t)
return l
def clist2(self,n,t1=int,t2=int,t3=int,tn=2):
l=[0]*n
for i in range(n):
if(tn==2):
a1=self.cin(t1)
a2=self.cin(t2)
l[i]=(a1,a2)
elif (tn==3):
a1=self.cin(t1)
a2=self.cin(t2)
a3=self.cin(t3)
l[i]=(a1,a2,a3)
return l
def clist3(self,n,t1=int,t2=int,t3=int):
return self.clist2(self,n,t1,t2,t3,3)
def cout(self,i,ans=''):
if(ans==''):
print("Case #"+str(i+1)+":", end=' ')
else:
print("Case #"+str(i+1)+":",ans)
def printf(self,thing):
print(thing,end='')
def countlist(self,l,s=0,e=None):
if(e==None):
e=len(l)
dic={}
for el in range(s,e):
if l[el] not in dic:
dic[l[el]]=1
else:
dic[l[el]]+=1
return dic
def talk (self,x):
print(x,flush=True)
def dp1(self,k):
L=[-1]*(k)
return L
def dp2(self,k,kk):
L=[-1]*(k)
for i in range(k):
L[i]=[-1]*kk
return L
def isprime(self,n):
if(n==1 or n==0):
return False
for i in range(2,int(n**0.5+1)):
if(n%i==0):
return False
return True
def factors(self,n):
from functools import reduce
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
def nthprime(self,n):
#usable up to 10 thousand
i=0
s=2
L=[]
while(i<n):
while(not self.isprime(s)):
s+=1
L.append(s)
s+=1
i+=1
return L
def matrixin(self,m,n,t=int):
L=[]
for i in range(m):
p=self.clist(n,t)
L.append(p)
return L
def seive(self,k):
#1000000 tops
n=k+1
L=[True]*n
L[1]=False
L[0]=False
for i in range(2,n):
if(L[i]==True):
for j in range(2*i,n,i):
L[j]=False
return L
def seiven(self,n,L):
i=0
for j in range(len(L)):
if(L[j]==True):
i+=1
if(i==n):
return j
def matrixin2(self,m,t=int):
L=[]
for i in range(m):
iny=self.cin(str)
lsmall=[]
for el in iny:
lsmall.append(t(el))
L.append(lsmall)
return L
c=cinn()
n=c.cin()
dol=c.cin()
eu=c.cin()
mini=100000000000000000000000000
tempn=n
for i in range(0,dol*eu+1):
tempn=n
tempn-=i*dol
if(tempn<0):
break
mini=min(mini,tempn%(eu*5))
print(mini)
| 1,400 | PYTHON3 |
n=int(input())
last='3'
lists=1
for i in range(n):
s=input()
first=s[0]
if last == first :
lists=lists+1
last=s[1]
print(lists)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int t = 1;
while (t--) solve();
return 0;
}
void solve() {
int n, m;
cin >> n >> m;
int d = n, u = 1;
while (u <= d) {
if (d != u) {
for (int i = 1; i <= m; ++i) {
cout << u << ' ' << i << '\n';
cout << d << ' ' << m - i + 1 << '\n';
}
} else {
int l = 1, r = m;
while (l <= r) {
if (l == r) {
cout << u << ' ' << l << '\n';
return;
}
cout << u << ' ' << l << ' ' << u << ' ' << r << '\n';
++l, --r;
}
}
++u;
--d;
}
}
| 1,800 | CPP |
for _ in range(int(input())):
n=int(input())
l=[int(x) for x in input().split()]
temp="First"
if l.count(1)==n:
if n%2==0:
print("Second")
else:
print("First")
else:
for i in range(n):
if l[i]!=1:
break
else:
if i%2==0:
temp="Second"
else:
temp="First"
print(temp)
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 1000 + 2;
int mx[MAXN + 10][2];
int val[MAXN + 10];
int par[MAXN + 10];
int n;
void set_val(int v) {
int newVal = max(mx[v][0], mx[v][1] + 1);
if (v == 0 || newVal == val[v]) return;
val[v] = newVal;
if (mx[par[v]][1] < val[v]) {
mx[par[v]][1] = val[v];
if (mx[par[v]][0] < val[v]) swap(mx[par[v]][0], mx[par[v]][1]);
set_val(par[v]);
}
}
int main() {
cin >> n;
n++;
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
p--;
par[i] = p;
set_val(i);
printf("%d ", mx[0][0]);
}
printf("\n");
return 0;
}
| 2,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
const double EPS = 1e-8;
const double PI = acos(-1.0);
int popcount(int n) { return __builtin_popcount(n); }
int popcount(long long n) { return __builtin_popcountll(n); }
template <class T>
int SIZE(T a) {
return a.size();
}
template <class T>
string IntToString(T num) {
string res;
stringstream ss;
ss << num;
return ss.str();
}
template <class T>
T StringToInt(string str) {
T res = 0;
for (int i = 0; i < SIZE(str); i++) res = (res * 10 + str[i] - '0');
return res;
}
template <class T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <class T>
void PrintSeq(T &a, int sz) {
for (int i = 0; i < sz; i++) {
cout << a[i];
if (sz == i + 1)
cout << endl;
else
cout << ' ';
}
}
bool EQ(double a, double b) { return abs(a - b) < EPS; }
bool EQV(complex<double> a, complex<double> b) {
return EQ(a.real(), b.real()) & EQ(a.imag(), b.imag());
}
void fastStream() {
cin.tie(0);
std::ios_base::sync_with_stdio(0);
}
vector<string> split(string str, char del) {
vector<string> res;
for (int i = 0, s = 0; i < SIZE(str); i++) {
if (str[i] == del) {
if (i - s != 0) res.push_back(str.substr(s, i - s));
s = i + 1;
} else if (i == SIZE(str) - 1) {
res.push_back(str.substr(s));
}
}
return res;
}
int main() {
string str;
cin >> str;
int pos = -1;
for (int i = 0; i < (int)str.size(); i++) {
if (str[i] == '0') {
pos = i;
break;
}
}
if (pos == -1)
cout << str.substr(1) << endl;
else {
cout << str.substr(0, pos) + str.substr(pos + 1) << endl;
}
return 0;
}
| 1,100 | CPP |
#----Kuzlyaev-Nikita-Codeforces-----
#------------03.04.2020-------------
alph="abcdefghijklmnopqrstuvwxyz"
#-----------------------------------
n,a,b=map(int,input().split())
h=list(map(int,input().split()))
h.sort()
r=h[b]-h[b-1]
print(r) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
struct UF {
int n;
vector<int> par, sz;
vector<int> ops;
void init(int _n) {
n = _n;
par = sz = vector<int>(n);
for (int i = (0); i < (n); ++i) par[i] = i, sz[i] = 1;
ops.clear();
}
int gettime() { return ((int)(ops).size()); }
void restore(int time) {
while (((int)(ops).size()) > time) {
int b = ops[((int)(ops).size()) - 1];
ops.pop_back();
int a = par[b];
par[b] = b, sz[a] -= sz[b];
}
}
int find(int a) {
if (par[a] == a) return a;
return find(par[a]);
}
void unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
ops.push_back(b);
par[b] = a, sz[a] += sz[b];
}
bool areconnected(int a, int b) { return find(a) == find(b); }
};
const int MAXN = 200000;
const int MAXQ = 200000;
const int MAXID = 2 * MAXQ;
int n, nq;
int qkind[MAXQ], qx[MAXQ], qy[MAXQ];
bool qans[MAXQ];
int qid[MAXQ][2];
int nid;
map<pair<int, int>, int> mp;
int qactid[MAXQ], qactx[MAXQ], qacty[MAXQ];
bool inside[MAXID];
int cnt[MAXID];
UF uf;
void rec(int l, int r, const vector<int> &alive, int &last) {
if (l == r) {
int idx = l;
qactid[idx] = qid[idx][last];
qactx[idx] = (qx[idx] + last) % n, qacty[idx] = (qy[idx] + last) % n;
if (qkind[idx] == 2) {
int time = uf.gettime();
for (int i = (0); i < (((int)(alive).size())); ++i) {
int idx = alive[i], x = qactx[idx], y = qacty[idx], id = qactid[idx];
uf.unite(x, y);
}
qans[idx] = uf.areconnected(qactx[idx], qacty[idx]);
last = qans[idx] ? 1 : 0;
uf.restore(time);
}
} else {
int time = uf.gettime();
for (int i = (l); i <= (r); ++i)
if (qkind[i] == 1)
for (int v = (0); v < (2); ++v) inside[qid[i][v]] = true;
vector<int> lalive;
for (int i = (0); i < (((int)(alive).size())); ++i) {
int idx = alive[i], x = qactx[idx], y = qacty[idx], id = qactid[idx];
if (inside[id])
lalive.push_back(idx);
else
uf.unite(x, y);
}
for (int i = (l); i <= (r); ++i)
if (qkind[i] == 1)
for (int v = (0); v < (2); ++v) inside[qid[i][v]] = false;
int m = l + (r - l) / 2;
rec(l, m, lalive, last);
for (int i = (0); i < (((int)(lalive).size())); ++i) {
int idx = lalive[i], id = qactid[idx];
++cnt[id];
}
for (int i = (l); i <= (m); ++i)
if (qkind[i] == 1) {
int idx = i, id = qactid[idx];
++cnt[id];
}
vector<int> ralive;
for (int i = (0); i < (((int)(lalive).size())); ++i) {
int idx = lalive[i], id = qactid[idx];
if (cnt[id] % 2 == 1) ralive.push_back(idx);
cnt[id] = 0;
}
for (int i = (l); i <= (m); ++i)
if (qkind[i] == 1) {
int idx = i, id = qactid[idx];
if (cnt[id] % 2 == 1) ralive.push_back(idx);
cnt[id] = 0;
}
rec(m + 1, r, ralive, last);
uf.restore(time);
}
}
void solve() {
nid = 0;
mp.clear();
for (int i = (0); i < (nq); ++i)
for (int v = (0); v < (2); ++v) {
int x = (qx[i] + v) % n, y = (qy[i] + v) % n;
if (x > y) swap(x, y);
pair<int, int> key = make_pair(x, y);
if (mp.count(key))
qid[i][v] = mp[key];
else
qid[i][v] = nid++, mp[key] = qid[i][v];
}
int last = 0;
for (int i = (0); i < (nid); ++i) inside[i] = false, cnt[i] = false;
uf.init(n);
rec(0, nq - 1, vector<int>(), last);
}
void run() {
scanf("%d%d", &n, &nq);
for (int i = (0); i < (nq); ++i) {
scanf("%d%d%d", &qkind[i], &qx[i], &qy[i]), --qx[i], --qy[i];
}
solve();
for (int i = (0); i < (nq); ++i)
if (qkind[i] == 2) printf("%d", qans[i] ? 1 : 0);
puts("");
}
int main() {
run();
return 0;
}
| 2,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5 + 5;
int a[sz], sbz[sz], cnt[sz], bit[sz], ans[sz];
vector<int> g[sz], qk[sz], qid[sz];
bitset<sz> heavy;
void fadd(int x, int f) {
while (x) {
bit[x] += f;
x -= x & (-x);
}
}
int quer(int x) {
int res = 0;
while (x < sz) {
res += bit[x];
x += x & (-x);
}
return res;
}
void add(int u, int p, int f) {
int &w = cnt[a[u]];
fadd(w, -1);
w += f;
fadd(w, +1);
for (int v : g[u])
if (v ^ p && !heavy[v]) add(v, u, f);
}
void dfs(int u = 1, int p = 0, bool keep = 0) {
int mx = 0, bigc = 0;
for (int v : g[u])
if (v ^ p && sbz[v] > mx) mx = sbz[v], bigc = v;
for (int v : g[u])
if (v ^ p && v ^ bigc) dfs(v, u);
if (bigc) heavy[bigc] = 1, dfs(bigc, u, 1);
add(u, p, 1);
for (int i = 0; i < qk[u].size(); i++) {
ans[qid[u][i]] = quer(qk[u][i]);
}
heavy[bigc] = 0;
if (!keep) add(u, p, -1);
}
int getsbz(int u = 1, int p = 0) {
sbz[u] = 1;
for (int v : g[u])
if (v ^ p) sbz[u] += getsbz(v, u);
return sbz[u];
}
int main() {
int n, q;
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
getsbz();
for (int i = 1; i <= q; i++) {
int v, k;
scanf("%d %d", &v, &k);
qk[v].push_back(k);
qid[v].push_back(i);
}
dfs();
for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
| 2,400 | CPP |
def problem(a, b):
ans = 0
if a == b: return ans
max_ = max(a, b)
min_ = min(a, b)
temp = max_ - min_
ans = temp // 10
if temp % 10 != 0:
ans += 1
return ans
testcases = int(input())
for i in range(testcases):
a, b = list(map(int, input().split()))
res = problem(a, b)
print(res) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int nm = 0, fh = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') fh = -1;
for (; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * fh;
}
int n, tot, to[100020 << 1], nt[100020 << 1], fs[100020];
int col[100020], T[100020], sz[100020], fa[100020], mxs[100020], mx, dt;
long long ans[100020], sum;
inline void link(int u, int v) { to[++tot] = v, nt[tot] = fs[u], fs[u] = tot; }
inline void dfs(int u, int last = 0) {
sz[u] = 1, fa[u] = last;
for (int i = fs[u]; i; i = nt[i])
if (to[i] ^ last) {
dfs(to[i], u), sz[u] += sz[to[i]];
if (sz[to[i]] > sz[mxs[u]]) mxs[u] = to[i];
}
}
inline void chg(int u, int Lim) {
T[col[u]] += dt;
if (T[col[u]] > mx)
mx = T[col[u]], sum = col[u];
else if (T[col[u]] == mx)
sum += (long long)col[u];
for (int i = fs[u]; i; i = nt[i])
if ((to[i] ^ fa[u]) && (to[i] ^ Lim)) chg(to[i], Lim);
}
inline void dfst(int u, int flag) {
for (int i = fs[u]; i; i = nt[i])
if ((to[i] ^ fa[u]) && (to[i] ^ mxs[u])) dfst(to[i], 1);
if (mxs[u]) dfst(mxs[u], 0);
dt = 1, chg(u, mxs[u]), ans[u] = sum;
if (flag) dt = -1, chg(u, 0), mx = 0, sum = 0ll;
}
int main() {
n = read();
for (int i = 1; i <= n; i++) col[i] = read();
for (int i = 1; i < n; i++) {
int u = read(), v = read();
link(u, v), link(v, u);
}
dfs(1), dfst(1, 0);
for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
puts("");
return 0;
}
| 2,300 | CPP |
def solve():
n=int(input())
candies=list(map(int,input().split()))
oranges=list(map(int,input().split()))
minCand=min(candies)
minOran=min(oranges)
sol=0
li=[]
for i in range(0,n):
li.append([candies[i], oranges[i]])
for l in li:
if l[0]==minCand and l[1]==minOran:
continue
elif l[0]!=minCand and l[1]!=minOran:
ldiff=l[0]-minCand
rdiff=l[1]-minOran
diff=min(ldiff,rdiff)
sol+=diff
sol+=(l[0]-diff-minCand)
sol+=(l[1]-diff-minOran)
elif l[0]!=minCand and l[1]==minOran:
diff=l[0]-minCand
sol+=diff
elif l[0]==minCand and l[1]!=minOran:
diff=l[1]-minOran
sol+=diff
print(sol)
tc=int(input())
for i in range(tc):
solve()
| 800 | PYTHON3 |
#include <bits/stdc++.h>
const int inf = 1 << 30;
const long long INF = 1ll << 61;
using namespace std;
const long long MOD = 1000000007;
int n;
map<int, int> mp;
map<int, int>::iterator it;
const int MAXN = 18000;
int C[MAXN + 1][500 + 1];
void Initial() {
int i, j;
for (i = 0; i <= MAXN; i++) {
if (i <= 500) C[0][i] = 0ll;
C[i][0] = 1ll;
}
for (i = 1; i <= MAXN; ++i) {
for (j = 1; j <= MAXN && j <= 500; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
}
}
bool isprime[1000000 + 5];
int prime[1000000 + 5];
int k;
void make_prime() {
memset(isprime, false, sizeof(isprime));
for (int i = 2; i < 100005; i++)
if (!isprime[i])
for (int j = i * 2; j < 100005; j += i) isprime[j] = true;
for (int i = 2; i < 100005; i++)
if (!isprime[i]) prime[k++] = i;
}
void divide(int x) {
int tmp = (int)sqrt(x * 1.0);
for (int i = 0; i < k; i++) {
if (prime[i] > tmp) break;
while (x % prime[i] == 0) {
mp[prime[i]]++;
x /= prime[i];
}
}
if (x != 1) mp[x]++;
}
void init() { mp.clear(); }
bool input() {
while (scanf("%d", &n) == 1) {
return false;
}
return true;
}
void cal() {
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
divide(x);
}
int ans = 1;
for (it = mp.begin(); it != mp.end(); it++) {
int tmp = it->second;
ans = (ans % MOD * C[tmp + n - 1][n - 1] % MOD) % MOD;
}
cout << ans << endl;
}
void output() {}
int main() {
Initial();
make_prime();
while (true) {
init();
if (input()) return 0;
cal();
output();
}
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int k, pw;
void calc(int *a, long long p) {
if (p == 0) return;
long long q = p / pw;
int x = p % pw;
if (x < k) {
a[0] = x, a[1] = 0;
calc(a + 2, q);
} else {
q++, x = pw - x;
int t = (x - 1) / k + 1;
int y = k * t - x;
a[0] = y, a[1] = t;
calc(a + 2, q);
}
}
int a[100];
long long p;
int main() {
scanf("%I64d %d", &p, &k);
pw = k * k;
calc(a, p);
int d = 99;
while (!a[d]) d--;
printf("%d\n", d + 1);
for (int i = 0; i <= d; i++) {
if (i) putchar(' ');
printf("%d", a[i]);
}
puts("");
}
| 2,000 | CPP |
for i in range(int(input())):
a, b = list(map(int, input().split()))
c = a-b
d = b-a
if c == d:
print(0)
if c>d and c % 2 == 0:
print(1)
if c>d and c % 2 != 0:
print(2)
if d>c and c % 2 == 0:
print(2)
if d>c and c % 2 != 0:
print(1)
'''m = 0
if c % 2 != 0:
while a != b:
a = a -''' | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10, maxg = 1010, mod = 1e9 + 7, inf = 1e9 + 10;
int a[maxn];
int dp[maxn][maxg];
bool mark[maxn][maxg];
deque<pair<int, int> > D;
void better(int pos, int rm, int x, bool is) {
if (mark[pos][rm]) return;
mark[pos][rm] = 1;
dp[pos][rm] = x;
if (is)
D.push_front({pos, rm});
else
D.push_back({pos, rm});
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie();
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a[i];
}
int g, r;
cin >> g >> r;
sort(a, a + m);
m = unique(a, a + m) - a;
for (int i = 0; i < m; i++) {
for (int j = 0; j <= g; j++) {
dp[i][j] = inf;
}
}
dp[0][g] = 0;
D.push_back({0, g});
mark[0][g] = 1;
while (int((D).size())) {
int pos = D.back().first, rm = D.back().second;
D.pop_back();
if (rm == 0) better(pos, g, dp[pos][rm] + 1, 1);
if (pos != 0 && a[pos] - a[pos - 1] <= rm)
better(pos - 1, rm - (a[pos] - a[pos - 1]), dp[pos][rm], 0);
if (pos != m - 1 && a[pos + 1] - a[pos] <= rm)
better(pos + 1, rm - (a[pos + 1] - a[pos]), dp[pos][rm], 0);
}
int ans = inf;
for (int i = 0; i <= g; i++) {
if (dp[m - 1][i] != inf) ans = min(ans, dp[m - 1][i] * (r + g) + (g - i));
}
if (ans == inf)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| 2,400 | CPP |
n = int(input(''))
words = [input('') for _ in range(n)]
for word in words:
length = len(word)
if (length > 10):
print(word[0] + str(length - 2) + word[-1])
else:
print(word)
| 800 | PYTHON3 |
p = input()
p = p.split()
r = int(p[1])
p = int(p[0])
i = 1
while 1:
if((p*i-r)%10==0) or (p*i)%10==0:
print(i)
break
i+=1
| 800 | PYTHON3 |
'''
Created on Sep 25, 2018
@author: Josh
'''
s = input()
count = 0
for i in range(len(s)):
if s[i] == "4" or s[i] == "7":
count += 1
if count == 4 or count == 7:
print("YES")
else:
print("NO") | 800 | PYTHON3 |
import sys,math,collections,itertools
from collections import defaultdict
from functools import reduce
#from itertools import permutations,combinations
def file():
sys.stdin = open('input.py', 'r')
sys.stdout = open('output.py', 'w')
def get_array():
l=list(map(int, input().split()))
return l
def get_ints():
return map(int, input().split())
def sod(n):
n,c=str(n),0
for i in n:
c+=int(i)
return c
def isPrime(n):
if (n <= 1):
return False
if (n <= 3):
return True
if (n % 2 == 0 or n % 3 == 0):
return False
i = 5
while(i * i <= n):
if (n % i == 0 or n % (i + 2) == 0):
return False
i = i + 6
return True
def getFloor(A, x):
(left, right) = (0, len(A) - 1)
floor = -1
while left <= right:
mid = (left + right) // 2
if A[mid] == x:
return A[mid]
elif x < A[mid]:
right = mid - 1
else:
floor = A[mid]
left = mid + 1
return floor
def gcd(a,b):
if (b == 0):
return a
return gcd(b, a%b)
def chk_power_of_2(n):
return n&(n-1)==0
def factors(n):
return set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
#file()
def main():
for tt in range(int(input())):
n=int(input())
l=get_array()
c=0
for i in range(n):
if(l[i]==1):
for j in range(i+1,n):
if(l[j]==1):
c+=(j-i-1)
break
print(c)
if __name__ == '__main__':
main()
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int BLOCK;
vector<pair<long long, int> > G[880];
long long add[880];
int n, q;
int search1(long long x, vector<pair<long long, int> > &tmp) {
int l = 0, r = tmp.size() - 1;
if (tmp[l].first > x || tmp[r].first < x) {
return -1;
}
while (l < r) {
int mid = (l + r) >> 1;
if (x <= tmp[mid].first) {
r = mid;
} else {
l = mid + 1;
}
}
if (tmp[l].first == x) {
return tmp[l].second;
}
return -1;
}
int search2(long long x, vector<pair<long long, int> > &tmp) {
int l = 0, r = tmp.size() - 1;
if (tmp[l].first > x || tmp[r].first < x) {
return -1;
}
while (l < r) {
int mid = (l + r + 1) >> 1;
if (x >= tmp[mid].first) {
l = mid;
} else {
r = mid - 1;
}
}
if (tmp[r].first == x) {
return tmp[r].second;
}
return -1;
}
int main() {
while (~scanf("%d%d", &n, &q)) {
for (int i = 0; i < BLOCK; i++) {
G[i].clear();
}
BLOCK = (int)sqrt(n) + 1;
memset(add, 0, sizeof(add));
for (int i = 0; i < n; i++) {
long long val;
scanf("%I64d", &val);
G[i / BLOCK].push_back(make_pair(val, i));
}
for (int i = 0; i <= (n - 1) / BLOCK; i++) {
sort(G[i].begin(), G[i].end());
}
for (int i = 0; i < q; i++) {
int op, l, r, x;
scanf("%d", &op);
if (op == 1) {
scanf("%d%d%d", &l, &r, &x);
l--, r--;
int id1 = l / BLOCK;
int id2 = r / BLOCK;
for (int j = id1 + 1; j <= id2 - 1; j++) {
add[j] += x;
}
for (int j = 0; j < G[id1].size(); j++) {
if (G[id1][j].second <= r && G[id1][j].second >= l) {
G[id1][j].first += x;
}
}
sort(G[id1].begin(), G[id1].end());
for (int j = 0; id1 != id2 && j < G[id2].size(); j++) {
if (G[id2][j].second <= r && G[id2][j].second >= l) {
G[id2][j].first += x;
}
}
sort(G[id2].begin(), G[id2].end());
} else {
scanf("%d", &x);
int ans1, ans2;
for (int j = 0; j <= (n - 1) / BLOCK; j++) {
ans1 = search1(x - add[j], G[j]);
if (~ans1) {
break;
}
}
for (int j = (n - 1) / BLOCK; j >= 0; j--) {
ans2 = search2(x - add[j], G[j]);
if (~ans2) {
break;
}
}
if (ans1 == -1 || ans2 == -1) {
printf("-1\n");
} else {
printf("%d\n", ans2 - ans1);
}
}
}
}
return 0;
}
| 2,500 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int MAXN = 101000;
const int MAXM = 500000;
const long long INF = 1e17;
struct shortestpath {
int cnt;
int head[MAXN], vis[MAXN];
long long dis[MAXN];
struct edge {
int to, nxt, w;
} e[MAXM];
void init() {
cnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w) {
e[cnt].w = w;
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
void dijkstra(int s, int n) {
memset(vis, 0, sizeof(vis));
for (int i = 0; i <= n; i++) dis[i] = INF;
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
greater<pair<long long, int> > >
que;
dis[s] = 0;
vis[s] = 1;
que.push(pair<long long, int>(0, s));
while (!que.empty()) {
pair<long long, int> p = que.top();
que.pop();
int u = p.second;
if (dis[u] < p.first) continue;
for (int i = head[u]; ~i; i = e[i].nxt) {
if (dis[e[i].to] > dis[u] + e[i].w) {
dis[e[i].to] = dis[u] + e[i].w;
que.push(pair<long long, int>(dis[e[i].to], e[i].to));
}
}
}
}
} Dij1, Dij2;
struct Tarjan {
int head[MAXN], cnt;
int low[MAXN], dfn[MAXN], sta[MAXN], bel[MAXN];
bool iscut[MAXN];
int top, idx, block, bri;
struct edge {
int from, to, nxt;
bool cut;
} e[MAXM];
void init() {
cnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v) {
e[cnt].from = u;
e[cnt].to = v;
e[cnt].nxt = head[u];
e[cnt].cut = false;
head[u] = cnt++;
}
void tarjan(int u, int pre) {
int v;
low[u] = dfn[u] = ++idx;
sta[++top] = u;
int child = 0, flag = 1;
for (int i = head[u]; ~i; i = e[i].nxt) {
v = e[i].to;
if (u == v) continue;
if (flag && v == pre) {
flag = 0;
continue;
}
if (!dfn[v]) {
child++;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
iscut[u] = true;
if (low[v] > dfn[u]) {
e[i].cut = true;
e[i ^ 1].cut = true;
}
}
} else
low[u] = min(low[u], dfn[v]);
}
if (child == 1 && pre < 0) iscut[u] = false;
if (low[u] == dfn[u]) {
block++;
do {
bel[sta[top]] = block;
} while (sta[top--] != u);
}
}
void solve(int N) {
idx = top = block = 0;
memset(dfn, 0, sizeof(dfn));
memset(iscut, 0, sizeof(iscut));
memset(bel, -1, sizeof(bel));
for (int i = 1; i <= N; i++) {
if (!dfn[i]) tarjan(i, -1);
}
}
} Tar;
int vis[MAXM];
struct Edge {
int u, v, w;
} e[MAXN];
int main() {
int n, m, s, t;
while (~scanf("%d%d%d%d", &n, &m, &s, &t)) {
Dij1.init(), Dij2.init();
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
e[i].u = u, e[i].v = v, e[i].w = w;
Dij1.addedge(u, v, w);
Dij2.addedge(v, u, w);
}
Dij1.dijkstra(s, n);
Dij2.dijkstra(t, n);
Tar.init();
memset(vis, false, sizeof(vis));
for (int i = 0; i < m; i++) {
int u = e[i].u, v = e[i].v, w = e[i].w;
if (Dij1.dis[u] + w + Dij2.dis[v] == Dij1.dis[t]) {
Tar.addedge(u, v);
Tar.addedge(v, u);
vis[i] = Tar.cnt - 1;
}
}
Tar.solve(n);
for (int i = 0; i < m; i++) {
if (vis[i] && Tar.e[vis[i]].cut == true) {
puts("YES");
continue;
}
int u = e[i].u, v = e[i].v;
long long w = Dij1.dis[t] - Dij1.dis[u] - Dij2.dis[v] - 1;
if (w <= 0)
puts("NO");
else
printf("CAN %I64d\n", e[i].w - w);
}
}
return 0;
}
| 2,200 | CPP |
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
temp=0
value = 0
last = -1
can = True
for i in range(n):
temp = b[i]-a[i]
if temp<0:
can = False
break
elif temp==0:
continue
else:
if (temp!=value and value>0) or (last!=-1 and last!=i-1):
can = False
break
value = temp
last = i
if can == True:
print("YES")
else:
print("NO")
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int pw = 131072;
int main() {
int n, m;
ios::sync_with_stdio(0);
cin >> n >> m;
if (n == 2 && m == 0) {
cout << "NO";
return 0;
} else
cout << "YES\n";
if (n == 1) {
cout << m;
return 0;
}
if (n == 2) {
cout << "0 " << m;
return 0;
}
int xuanxue = 0;
for (int i = 1; i <= n - 3; i++) cout << i << " ", xuanxue = xuanxue ^ i;
if (m == xuanxue) {
cout << pw << " " << pw * 2 << " " << (pw ^ (pw * 2));
} else
cout << "0 " << pw << " " << ((pw ^ m) ^ xuanxue);
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 20;
long long n, d, a, b, m = 0;
pair<long long, int> p[N];
vector<int> v;
int main() {
cin >> n >> d;
cin >> a >> b;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
p[i].first = (x * a) + (y * b);
p[i].second = i;
}
sort(p, p + n + 1);
for (int i = 1; i <= n; i++) {
if (m + p[i].first <= d) {
m += p[i].first;
v.push_back(p[i].second);
} else {
break;
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
return 0;
}
| 1,400 | CPP |
for i in range(int(input())):
s=input()
l=r=u=d=0
for i in range(len(s)):
if s[i]=='L':
l+=1
elif s[i]=='R':
r+=1
elif s[i]=='U':
u+=1
else:
d+=1
if min(u,d)==0 and l and r:
print(2)
print("LR")
elif min(u,d)==0 and min(l,r)==0:
print(0)
print('')
elif min(l,r)==0 and u and d:
print(2)
print('UD')
else:
l=min(l,r)
r=min(l,r)
u=min(u,d)
d=min(u,d)
ans=r*['R']+d*['D']+l*['L']+u*['U']
print(len(ans))
print(*ans,sep='') | 1,200 | PYTHON3 |
n = int(input())
num = n % 10
n = n // 10
if num <= 5:
n = n * 10
if num > 5:
n = n + 1
n = n * 10
print(n) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c, d, a, b, y, i;
cin >> n;
y = 0, a = 1e9, b = -100000000;
for (i = 0; i < n; i++) {
cin >> c >> d;
if (d == 1)
b = max(1900 - y, b);
else if (d == 2)
a = min(1900 - y, a);
y += c;
}
if (a == 1e9)
cout << "Infinity" << endl;
else if (a <= b)
cout << "Impossible" << endl;
else
cout << a - 1 + y << endl;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10000000000")
using namespace std;
const int INF = 1e9 + 10;
const int size = (1 << 2);
const int maxK = 1e4;
const int maxN = 1e5;
int t[maxN];
int main() {
int n, c;
cin >> n >> c;
int w = 1;
for (int i = 0; i < n; i++) {
cin >> t[i];
}
for (int i = 1; i < n; i++) {
if (t[i] - t[i - 1] > c) {
w = 1;
} else {
w++;
}
}
cout << w;
return 0;
}
| 800 | CPP |
n=int(input())
s=input()
si=[len(i) for i in s.split('W') if i]
print(len(si))
for i in si:
print(i,end=' ') | 800 | PYTHON3 |
n, m, x, y = map(int, input().split())
a_n = [i for i in map(int, input().split())]
b_m = [i for i in map(int, input().split())]
count = 0
result = []
i = 0
j = 0
while j < m and i < n:
if b_m[j] > a_n[i] + y:
i += 1
elif b_m[j] < a_n[i] - x:
j += 1
else:
count += 1
result.append((i+1, j+1))
i += 1
j += 1
print(count)
for i in result:
print(i[0], i[1]) | 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int SI = 3e5 + 7;
long long INF = 8e18 + 7;
int MOD = 1e9 + 7;
int T = 1;
long long n, m, k;
set<long long> se;
deque<long long> q;
void solver() {
while (q.size()) q.pop_back();
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) q.push_back(i);
while (k--) {
long long mm = n % m;
se.clear();
for (int i = 1; i <= n; i++) se.insert(i);
while (mm--) {
cout << (n + m - 1) / m << " ";
for (int i = 0; i < (n + m - 1) / m; i++) {
cout << q.front() << " ";
q.push_back(q.front());
se.erase(q.front());
q.pop_front();
}
cout << "\n";
}
mm = m - n % m;
for (int i = 1; i <= mm; i++) {
cout << n / m << " ";
for (int i = 0; i < n / m; i++)
cout << *se.begin() << " ", se.erase(se.begin());
cout << "\n";
}
}
}
int main() {
cin >> T;
while (T--) solver();
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int t;
string p, h;
bool is_shuffle(string s, string p) {
sort(s.begin(), s.end());
sort(p.begin(), p.end());
return s == p;
}
int main() {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> p >> h;
bool flag = 0;
if (h.length() >= p.length()) {
for (int i = 0; i < h.length() - p.length() + 1; ++i) {
string tmp = h.substr(i, p.length());
if (is_shuffle(tmp, p)) {
flag = 1;
break;
}
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
int n, a[MAX];
int main() {
cin >> n;
while (n--) {
int x;
cin >> x;
a[--x]++;
}
int cnt = 0;
for (int i = 0; i < 90; i++) {
if (a[i])
cnt = 0;
else
cnt++;
if (cnt == 15) {
cout << i + 1 << "\n";
return 0;
}
}
cout << 90 << "\n";
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long x = 0, f = 0, j, q = 1, i, n, m, k;
cin >> q;
while (q--) {
x = 0;
cin >> n;
vector<long long> a(n);
unordered_map<long long, long long> m;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i <= n - 1; i++) m[(i + (a[i] % n) + n) % n]++;
for (auto i : m)
if (i.second > 1) x = -1;
if (x == 0)
cout << "YES";
else
cout << "NO";
cout << "\n";
}
}
| 1,600 | CPP |
#include<bits/stdc++.h>
#define int long long
#define REP(i,a,n) for(int i=a;i<(n);i++)
#define REP_sz(i,s) for(int i=0;i<s.size();i++)
#define gcd(a,b) __gcd(a,b)
#define RE return
#define FILL(a,b) memset(a,b,sizeof(a))
#define SO(a) sort(all(a))
#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) (int)a.size()
#define V vector
#define ld long double
#define viit(a) vector<int>::iterator a
#define IT iterator
#define SET setprecision
#define FOR(i,a,n) for(int i=a;i<=(n);i++)
#define ER1(a) a.erase(a.begin())
#define ER0(a) a.erase(a.end())
#define pii pair<int,int>
#define pause system("PAUSE")
#define cls system("CLS")
#define mod1 1000000007
#define mod2 998244353
#define infmod 9223372036854775783
#define tc int t;cin>>t;while(t--)solve();
#define over(x) {cout<<x<<endl;return;}
#define intsz(x) (int)sz(to_string(x))
using namespace std;
int sum[100005][26];
void solve()
{
FILL(sum,0);
string a,b;
int n,m;
cin>>n>>m;
REP(i,0,n){
cin>>a;
REP(j,0,m)
sum[j][a[j]-'a']++;
}
REP(i,1,n){
cin>>b;
REP(j,0,m)
sum[j][b[j]-'a']--;
}
REP(i,0,m)
REP(j,0,26)
if(sum[i][j])cout<<char(j+'a');
cout<<endl;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
tc
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
long long rs = 1, cr = a;
while (b) {
if (b & 1) rs = (rs * cr) % 1000000007;
cr = (cr * cr) % 1000000007;
b >>= 1;
}
return rs;
}
long long a[1010][1010];
int p[1010 * 1010], cnt[1010 * 1010];
bool vs[1010][1010];
int par(int u) {
if (p[u] == u) return u;
return p[u] = par(p[u]);
}
void merg(int u, int v) {
if (par(u) == par(v)) return;
cnt[par(u)] += cnt[par(v)];
p[par(v)] = par(u);
}
long long k, dn;
int n, m;
void dfs(int r, int c, long long vl) {
if (vs[r][c] || dn == k || a[r][c] < vl) return;
vs[r][c] = 1;
dn += vl;
if (r > 1) dfs(r - 1, c, vl);
if (r < n) dfs(r + 1, c, vl);
if (c > 1) dfs(r, c - 1, vl);
if (c < m) dfs(r, c + 1, vl);
}
map<long long, vector<int> > M;
void solve() {
int i, j;
scanf("%d", &n);
scanf("%d", &m);
scanf("%lld", &k);
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j) {
scanf("%lld", &a[i][j]);
M[a[i][j]].push_back(i * (m + 1) + j);
}
map<long long, vector<int> >::reverse_iterator itr;
itr = M.rbegin();
long long vl, r, c, idx;
for (; itr != M.rend(); ++itr) {
vl = itr->first;
for (i = 0; i < (itr->second).size(); ++i) {
idx = (itr->second)[i];
r, c;
c = idx % (m + 1);
r = idx / (m + 1);
p[idx] = idx;
cnt[idx] = 1;
int id1 = (r - 1) * (m + 1) + c;
if (p[id1] > 0) merg(idx, id1);
id1 = (r + 1) * (m + 1) + c;
if (p[id1] > 0) merg(idx, id1);
id1 = (r) * (m + 1) + c - 1;
if (p[id1] > 0) merg(idx, id1);
id1 = (r) * (m + 1) + c + 1;
if (p[id1] > 0) merg(idx, id1);
if (k % vl == 0 && k / vl <= cnt[par(idx)]) break;
}
if (i != (itr->second).size()) {
break;
}
}
if (itr == M.rend()) {
printf("NO\n");
return;
}
printf("YES\n");
dfs(r, c, vl);
for (i = 1; i <= n; ++i) {
for (j = 1; j <= m; ++j) {
if (vs[i][j])
printf("%lld ", vl);
else
printf("0 ");
}
printf("\n");
}
}
int main() {
int t = 1;
while (t--) {
solve();
}
}
| 2,000 | CPP |
x=int(input())
emon1=input().split()
emon=[]
y=int(input())
mishu1=input().split()
mishu=[]
for i in emon1:
c=int(i)
emon.append(c)
for i in mishu1:
c=int(i)
mishu.append(c)
emon.sort()
mishu.sort()
a=0
for i in emon:
for l in mishu:
if (((i-1)==l) or ((i+1)==l) or (i==l)):
a=a+1
mishu.remove(l)
break
print(a) | 1,200 | PYTHON3 |
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
# βββββββββββββββββββββββββββββββ
for i in range(int(input())):
a,b = map(int,input().split())
if a==b:
print(0)
diff = abs(a-b)
if a<b:
if diff%2==0:
print(2) #do odd ka sum
elif diff%2==1:
print(1) # ek odd = b-a add karna hai a mei
elif a>b:
if diff%2==0:
print(1) # ek even subtract karna hai a se
if diff%2==1:
print(2) # add +1 to a and then subtract a+1-b (even) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
char s[2000000 + 5];
int deepK[2000000 + 5];
int n, k;
bool flag;
void dfs(int pos, int deep) {
if (pos > n) return;
if (s[pos] == '(') {
if (k) {
printf("(");
deepK[deep + 1] = 1;
k--;
} else
deepK[deep + 1] = 0;
dfs(pos + 1, deep + 1);
} else if (s[pos] == ')') {
if (deepK[deep]) printf(")");
dfs(pos + 1, deep - 1);
}
}
int main() {
scanf("%d%d", &n, &k);
k /= 2;
scanf("%s", s + 1);
dfs(1, 1);
}
| 1,200 | CPP |
for _ in range(int(input())):
n=int(input())
s=input()
sm=input()
a=list(s)
asm=list(sm[::-1])
i=0
l=[]
while i<n and list(sm)!=a:
if a[0]==sm[n-i-1]:
if a[0]=='0':
a[0]='1'
else:
a[0]='0'
l.append(1)
for x in range(n-i):
if a[x]=='1':
a[x]='0'
else:
a[x]='1'
a[:n-i]=a[n-i-1::-1]
l.append(n-i)
i+=1
print(len(l),*l) | 1,300 | PYTHON3 |
MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
n=ii()
s=1
for i in range(1,n):
s=s*i
print(s//(n//2)) | 1,300 | PYTHON3 |
#!/usr/bin/env python
from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
def main():
n = int(input())
if n % 2 == 0:
print('NO')
return
sol = [0] * (2 * n)
sol[0] = 1
sol[n] = 2
for i in range(1, n):
if i % 2:
sol[n - i] = (i * 2) + 1
sol[-i] = (i * 2) + 2
else:
sol[-i] = (i * 2) + 1
sol[n - i] = (i * 2) + 2
print("YES")
print(*sol)
# region fastio
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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
| 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> M;
int n, m, max = -99999;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &m);
M[m] += 1;
if (M[m] > max) {
max = M[m];
}
}
printf("%d\n", n - max);
return 0;
}
| 1,300 | CPP |
k = int(input())
l = int(input())
m = int(input())
n = int(input())
d = int(input())
counter = 0
f = [k, l, m, n]
if 1 in f:
print(d)
else:
for i in range(1, d+1):
if i % k == 0 or i % l == 0 or i % m == 0 or i % n == 0:
counter += 1
print(counter)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int k;
long long pa, pb, p1, p2, f[1005][2005], bin[2005];
long long Pow(long long x, long long y) {
long long ans = 1;
while (y) {
if (y & 1) ans = ans * x % 1000000007;
x = x * x % 1000000007;
y /= 2;
}
return ans;
}
long long ans = 0;
int main() {
k = read();
pa = read();
pb = read();
long long inv = Pow(pa + pb, 1000000007 - 2);
p1 = pa * inv % 1000000007;
p2 = pb * inv % 1000000007;
long long p3 = Pow(p2, 1000000007 - 2) % 1000000007;
f[1][0] = 1;
for (int i = 1; i <= k; i++) {
for (int j = 0; j <= k; j++)
if (f[i][j] != 0) {
if (i + j < k) {
f[i + 1][j] = (f[i + 1][j] + f[i][j] * p1 % 1000000007) % 1000000007;
f[i][i + j] = (f[i][i + j] + f[i][j] * p2 % 1000000007) % 1000000007;
} else {
if (j == k)
ans = (ans + f[i][j] * j % 1000000007) % 1000000007;
else if (j < k) {
ans =
(ans + f[i][j] * (i + j + p1 * p3 % 1000000007) % 1000000007) %
1000000007;
}
}
}
}
printf("%lld\n", ans % 1000000007);
return 0;
}
| 2,200 | CPP |
n = int(input())
A = list(map(int, input().split()))
# soln
A.sort()
t = 0
SZ = 4
while A:
c = A.pop()
while A and c < SZ:
c = c + A[0]
if A and c <= SZ:
A.pop(0)
t += 1
print(t)
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
template <class T>
int size(T &x) {
return x.size();
}
int main() {
long long n, k;
cin >> n >> k;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long psum = 0, at = 0, cnt = n;
for (int i = 0; i < n; i++) {
long long d = 0;
d += psum;
d -= (cnt - at - 1) * a[i] * at;
if (d < k) {
printf("%d\n", i + 1);
cnt--;
} else {
psum += a[i] * at;
at++;
}
}
return 0;
}
| 1,600 | CPP |
import re
for _ in range(int(input())):
s=input()
count1=0
count0=0
count=0
for i in range(len(s)):
if(s[i]=="1"):
count1+=1
else:
count0+=1
if(count1==count0):
count+=count1
count1=0
count0=0
elif(count1==1 and count0>1):
count0-=1
count1=0
count+=1
elif(count0==1 and count1>1):
count1-=1
count0=0
count+=1
if count&1:
print("DA")
else:
print("NET") | 900 | PYTHON3 |
c=0
for i in range(int(input())):
a,b=map(int,input().split())
if(b-a>=2):
c=c+1
print(c)
| 800 | PYTHON3 |
def digit(x, n):
for i in range(10):
if str(i) in str(n) and str(i) in str(x):
return True
return False
def factorize(n): # o(sqr(n))
c, ans = 1, 0
while (c * c < n):
if n % c == 0:
if digit(c, n):
ans += 1
if digit(n // c, n):
ans += 1
c += 1
if c * c == n:
if digit(c, n):
ans += 1
return ans
print(factorize(int(input())))
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
const bool debug = false;
using namespace std;
long long powmod(long long a, long long b, long long MOD) {
long long res = 1;
a %= MOD;
for (; b; b >>= 1) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
}
return res;
}
void buginfo(const char* f, ...) {
if (!debug) return;
va_list al;
va_start(al, f);
vprintf(f, al);
va_end(al);
}
const int maxn = 2e5 + 10;
struct Node {
int id, v;
} a[maxn];
bool cmp(const Node& p1, const Node& p2) { return p1.v < p2.v; }
bool cmp1(const Node& p1, const Node& p2) { return p1.v == p2.v; }
int ksunique(Node* a, int len) {
int p = 0;
for (int i = 0; i < len; ++i) {
if (a[p].v != a[i].v) {
++p;
swap(a[p], a[i]);
}
}
return ++p;
}
int n, m, cnt;
set<int> as;
int x, y;
int getodd() {
while (x <= m && as.count(x)) x += 2;
if (x > m) return -1;
x += 2;
return x - 2;
}
int getev() {
while (y <= m && as.count(y)) y += 2;
if (y > m) return -1;
y += 2;
return y - 2;
}
int main() {
scanf("%d%d", &n, &m);
x = 1, y = 2;
for (int i = 0; i < n; ++i) scanf("%d", &a[i].v);
for (int i = 0; i < n; ++i) a[i].id = i;
sort(a, a + n, cmp);
int p = ksunique(a, n);
for (int i = 0; i < n; ++i) as.insert(a[i].v);
int ev = 0, od = 0;
for (int i = 0; i < n; ++i) {
if (a[i].v & 1)
++od;
else
++ev;
}
if (abs(ev - od) & 1) return 0 * printf("-1\n");
if (ev == od) goto done;
if (ev > od) {
for (int i = n - 1; i >= 0; --i) {
if (ev == od) break;
if (a[i].v & 1) continue;
int c = getodd();
if (c == -1) return 0 * printf("-1\n");
a[i].v = c;
--ev, ++od, ++cnt;
}
} else {
for (int i = n - 1; i >= 0; --i) {
if (ev == od) break;
if (a[i].v % 2 == 0) continue;
int c = getev();
if (c == -1) return 0 * printf("-1\n");
a[i].v = c;
++ev, --od, ++cnt;
}
}
done:
sort(a, a + n, cmp);
p = ksunique(a, n);
for (int i = n - 1; i >= p; --i) {
if (a[i].v & 1) {
int c = getodd();
if (c == -1) return 0 * printf("-1\n");
a[i].v = c;
} else {
int c = getev();
if (c == -1) return 0 * printf("-1\n");
a[i].v = c;
}
++cnt;
}
sort(a, a + n, [&](const Node& p1, const Node& p2) { return p1.id < p2.id; });
printf("%d\n", cnt);
for (int i = 0; i < n; ++i) printf("%d ", a[i].v);
printf("\n");
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1000 + 5;
const int mod = 1000 * 1000 * 1000 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie();
int n, k, p, x, y;
cin >> n >> k >> p >> x >> y;
int sum = 0, s1 = 0, s2 = 0;
for (int i = 0; i < k; i++) {
int tmp;
cin >> tmp;
sum += tmp;
if (tmp >= y)
s1++;
else
s2++;
}
if (s2 >= (n + 1) / 2) {
cout << -1 << endl;
return 0;
}
int te1 = max((n + 1) / 2 - s1, 0);
int te2 = n - k - te1;
if (sum + te2 + te1 * y > x) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < te1; i++) cout << y << ' ';
for (int i = 0; i < te2; i++) cout << 1 << ' ';
cout << endl;
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, u[1001000], v[1001000], w[1001000];
vector<int> BIT[1001000];
vector<pair<int, int> > e[1001000];
void Up(int k, int u, int val) {
for (; u < (int)BIT[k].size(); u += u & -u) BIT[k][u] = max(BIT[k][u], val);
}
int Get(int k, int u) {
int ans = 0;
for (; u > 0; u -= u & -u) ans = max(ans, BIT[k][u]);
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = (1); i <= (m); ++i) {
cin >> u[i] >> v[i] >> w[i];
e[u[i]].push_back(pair<int, int>(w[i], v[i]));
e[v[i]].push_back(pair<int, int>(w[i], u[i]));
}
for (int i = (1); i <= (n); ++i) {
sort(e[i].begin(), e[i].end());
BIT[i].resize((int)e[i].size() + 10);
}
int ans = 0;
for (int i = (1); i <= (m); ++i) {
int pu =
lower_bound(e[u[i]].begin(), e[u[i]].end(), pair<int, int>(w[i], -1)) -
e[u[i]].begin();
int fv = Get(u[i], pu) + 1;
int pv =
lower_bound(e[v[i]].begin(), e[v[i]].end(), pair<int, int>(w[i], -1)) -
e[v[i]].begin();
Up(v[i], pv + 1, fv);
ans = max(ans, fv);
}
cout << ans;
}
| 2,100 | CPP |