solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
n,m,v,p = map(int,readline().split())
a = [int(i) for i in readline().split()]
a.sort(reverse=True)
ans = p
v -= (p-1)
g = a[p-1]
L = 1
use = m-g
for i in range(p,n):
mar = g-a[i]
V = v-(n-1-i)
if mar > m:
break
if V <= 1:
if mar <= m:
ans += 1
L += 1
use += m-a[i]
else:
break
else:
if use+a[i]*L >= (V-1)*m:
ans += 1
L += 1
use += m-a[i]
else:
break
print(ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
return (a * b) / gcd(a, b);
}
using namespace std;
int n, vec[305], visited[305];
char ar[305][305];
vector<set<int> > ans(305);
void dfs(int node, int color) {
visited[node] = color;
ans[color].insert(vec[node]);
for (int i = 1; i <= n; i++) {
if (ar[node][i] == '1' && !visited[i]) {
dfs(i, color);
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> vec[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> ar[i][j];
int c = 0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
dfs(i, ++c);
}
}
for (int i = 1; i <= n; i++) {
cout << *ans[visited[i]].begin() << " ";
ans[visited[i]].erase(ans[visited[i]].begin());
}
return 0;
}
| 8 | CPP |
from itertools import combinations_with_replacement
n,m,q = map(int,input().split())
abcd = [list(map(int,input().split())) for _ in range(q)]
ans = 0
for l in combinations_with_replacement(range(m), n):
sum = 0
for a,b,c,d in abcd:
if l[b-1] - l[a-1] == c:
sum += d
ans = max(ans, sum)
print(ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
vector<vector<ii>> t;
int n, tt;
vector<int> lo, hi, bit;
vector<pair<long long, int>> p, a;
void dfs(int u = 0) {
lo[u] = hi[u] = tt++;
p[u].second = u;
for (auto [v, w] : t[u]) {
p[v].first = w + p[u].first;
dfs(v);
hi[u] = max(hi[u], hi[v]);
}
}
void upd(int k, int x) {
for (++k; k <= n; k += k & -k) bit[k] += x;
}
int query(int k) {
int a = 0;
for (++k; k > 0; k -= k & -k) a += bit[k];
return a;
}
void solve() {
cin >> n;
vector<int> cnt(n, 0);
bit.assign(n + 1, 0);
t.resize(n);
lo.resize(n);
hi.resize(n);
p.assign(n, make_pair(0LL, 0));
a.resize(n);
for (int i = 0; i < n; ++i) cin >> a[i].first;
for (int i = 1; i < n; ++i) {
int pai, w;
cin >> pai >> w;
t[pai - 1].push_back({i, w});
}
dfs();
for (int i = 0; i < n; ++i) {
a[i].first -= p[i].first;
a[i].second = i;
p[i].first *= -1;
}
sort(a.rbegin(), a.rend());
sort(p.rbegin(), p.rend());
for (int i = 0, j = 0; j < n; ++j) {
for (; i < n && a[i].first >= p[j].first; ++i) upd(lo[a[i].second], 1);
cnt[p[j].second] = query(hi[p[j].second]) - query(lo[p[j].second]);
}
for (int v = 0; v < n; ++v) cout << cnt[v] << ' ';
cout << '\n';
}
signed main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void calc(const string& a, const string& b, vector<int>& res) {
int na = a.size(), nb = b.size();
res.resize(na + 1, true);
for (int i = 0; i < na; ++i) {
for (int j = 0; j < nb; ++j) {
if (i + j >= na) break;
if (a[i + j] != b[j] && a[i + j] != '?' && b[j] != '?') {
res[i] = false;
break;
}
}
}
}
int solve(const string& a, const string& b, const string& c) {
int na = a.size(), nb = b.size(), nc = c.size();
vector<int> dp_ab, dp_bc, dp_ac;
calc(a, b, dp_ab);
calc(b, c, dp_bc);
calc(a, c, dp_ac);
int ans = na + nb + nc;
for (int i = 0; i <= 4000; ++i) {
if (!dp_ab[min(i, na)]) continue;
for (int j = 0; j <= 4000; ++j) {
if (!dp_bc[min(j, nb)] || !dp_ac[min(i + j, na)]) continue;
ans = min(ans, max({na, i + nb, i + j + nc}));
}
}
return ans;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
array<string, 3> vs;
for (int i = 0; i < 3; ++i) {
cin >> vs[i];
}
vector<int> ids = {0,1,2};
int ans = 10000;
do {
ans = min(ans, solve(vs[ids[0]], vs[ids[1]], vs[ids[2]]));
} while (next_permutation(ids.begin(), ids.end()));
cout << ans << "\n";
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long B = 131;
const long long P = 1e9 + 9;
int n, m;
char s[200000 + 10];
long long h[26][200000 + 10];
long long t[200000 + 10];
long long add(long long a, long long b) { return (a + b) % P; }
long long sub(long long a, long long b) {
long long c = (a - b) % P;
if (c < 0) c += P;
return c;
}
long long mul(long long a, long long b) { return a * b % P; }
bool calc(int x, int y, int len) {
long long a[26], b[26];
for (int i = 0; i < 26; i++) {
a[i] = sub(h[i][x + len - 1], mul(x > 0 ? h[i][x - 1] : 0, t[len]));
b[i] = sub(h[i][y + len - 1], mul(y > 0 ? h[i][y - 1] : 0, t[len]));
}
sort(a, a + 26);
sort(b, b + 26);
for (int i = 0; i < 26; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
void solve() {
t[0] = 1;
for (int i = 1; i < n; i++) {
t[i] = mul(t[i - 1], B);
}
for (int i = 0; i < 26; i++) {
h[i][0] = (s[0] == i + 'a');
for (int j = 1; j < n; j++) {
h[i][j] = add(mul(h[i][j - 1], B), s[j] == i + 'a');
}
}
for (int x, y, len; m--;) {
scanf("%d %d %d", &x, &y, &len);
puts(calc(x - 1, y - 1, len) ? "YES" : "NO");
}
}
int main() {
scanf("%d %d %s", &n, &m, s);
solve();
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 50;
long long c[MAXN];
long long minSum(long long n, long long k) {
long long res = 0, cur = 1, left = n, d = 1;
while (left > 0) {
cur = min(left, cur);
res += d * cur;
left -= cur;
d++;
cur *= k;
}
return res;
}
int main() {
long long n, s;
scanf("%lld%lld", &n, &s);
if (s < 2 * n - 1 || s > n * (n + 1) / 2) {
printf("No\n");
return 0;
} else {
printf("Yes\n");
}
long long k = 1;
while (minSum(n, k) > s) k++;
if (k == 1) {
for (int i = 1; i <= n - 1; i++) printf("%d ", i);
return 0;
}
for (int i = 1; i <= n; i++) c[i] = 1;
int pos = 2;
long long left = n * (n + 1) / 2 - s;
for (int i = n; i > 0 && left > 0; i--) {
while (c[pos] >= k * c[pos - 1]) pos++;
if (i - pos <= left) {
c[i]--;
c[pos]++;
left -= (i - pos);
} else {
c[i]--;
c[i - left]++;
break;
}
}
int l = 1, r = 1;
for (int i = 2; i <= n; i++) {
int nl = r + 1, nr = nl + c[i] - 1;
int cur = l;
for (int j = 0; j < c[i] / k; j++) {
for (int t = 0; t < k; t++) printf("%d ", cur);
cur++;
}
for (int j = 0; j < c[i] % k; j++) printf("%d ", cur);
l = nl, r = nr;
}
}
| 9 | CPP |
n=int(input())
for i in range (n):
a=input()
word=tuple(a)
if len(word)>10:
print(word[0]+str(len(word)-2)+word[-1])
else:
print(a) | 7 | PYTHON3 |
count = int(input())
high = list(map(int, input().split(' ')))
value = 0
value_res = 0
for i in range(count):
longer = high[i]
j = i
while j < count-1 :
if longer > high[j+1]:
value +=1
longer = high[j+1]
elif longer == high[j+1]:
value += 1
else:
break
j +=1
longer = high[i]
j = i
while j > 0:
if longer > high[j-1]:
value +=1
longer = high[j-1]
elif longer == high[j-1]:
value += 1
else:
break
j -=1
if value+1 > value_res:
value_res = value+1
value = 0
print(value_res) | 8 | PYTHON3 |
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split(" ")))
if l[-1] == 0:
for i in range(1, n + 2):
print(i, end=" ")
print()
else:
if l[0] == 1:
print(n + 1, end=" ")
for i in range(1, n + 1):
print(i, end=" ")
print()
else:
idx = -1
for i in range(n - 1):
if l[i] == 0 and l[i + 1] == 1:
idx = i + 1
break
if idx >= 0:
for i in range(1, idx + 1):
print(i, end=" ")
print(n + 1, end=" ")
for i in range(idx + 1, n + 1):
print(i, end=" ")
print()
else:
print(-1)
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long INF = (1ULL << 50);
struct edge {
int src, dst;
long long capacity;
int rev;
long long residue;
};
struct graph {
int n;
vector<vector<edge>> adj;
graph(int n = 0) : n(n), adj(n) {}
void add_edge(int src, int dst, long long capacity) {
adj[src].push_back({src, dst, capacity, (int)adj[dst].size()});
adj[dst].push_back({dst, src, capacity, (int)adj[src].size() - 1});
}
vector<int> level, iter;
long long augment(int u, int t, long long cur) {
if (u == t) return cur;
for (int &i = iter[u]; i < adj[u].size(); ++i) {
edge &e = adj[u][i];
if (e.residue > 0 && level[u] < level[e.dst]) {
long long f = augment(e.dst, t, min(cur, e.residue));
if (f > 0) {
e.residue -= f;
adj[e.dst][e.rev].residue += f;
return f;
}
}
}
return 0;
}
int bfs(int s, int t) {
level.assign(n, -1);
level[s] = 0;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
if (u == t) break;
for (auto &e : adj[u]) {
if (e.residue > 0 && level[e.dst] < 0) {
Q.push(e.dst);
level[e.dst] = level[u] + 1;
}
}
}
return level[t];
}
long long max_flow(int s, int t) {
for (int u = 0; u < n; ++u)
for (auto &e : adj[u]) e.residue = e.capacity;
long long flow = 0;
int itera = 0;
while (bfs(s, t) >= 0) {
iter.assign(n, 0);
for (long long f; (f = augment(s, t, INF)) > 0;) flow += f;
}
return flow;
}
vector<edge> tree;
vector<int> parent;
void gomory_hu() {
tree.clear();
parent.clear();
parent.resize(n);
for (int i = 0; i < n; ++i) parent[i] = 0;
for (int u = 1; u < n; ++u) {
tree.push_back({u, parent[u], max_flow(u, parent[u])});
for (int v = u + 1; v < n; ++v)
if (level[v] >= 0 && parent[v] == parent[u]) parent[v] = u;
}
}
};
struct ed {
int u;
long long cap;
};
vector<ed> par;
struct ted {
int u;
long long cap;
int rev;
bool tak;
};
vector<vector<ted>> adj;
int n;
vector<bool> vis;
long long dfs(int x) {
vis[x] = 1;
long long ans = INF;
for (int i = 0; i < (int)adj[x].size(); ++i) {
ted nxt = adj[x][i];
if (!nxt.tak && !vis[nxt.u]) {
ans = min(ans, nxt.cap);
ans = min(ans, dfs(nxt.u));
}
}
return ans;
}
bool f = 0;
pair<int, int> dfs2(int x, long long val) {
vis[x] = 1;
pair<int, int> ans = make_pair(-1, -1);
for (int i = 0; i < (int)adj[x].size(); ++i) {
ted nxt = adj[x][i];
if (!nxt.tak && !vis[nxt.u]) {
if (!f && nxt.cap == val) {
f = 1;
ans = make_pair(x, nxt.u);
adj[x][i].tak = 1;
adj[nxt.u][adj[x][i].rev].tak = 1;
}
pair<int, int> maybe = dfs2(nxt.u, val);
if (maybe.first != -1) ans = maybe;
}
}
return ans;
}
vector<int> perm(int x) {
vis.resize(n);
for (int i = 0; i < n; ++i) vis[i] = 0;
long long val = dfs(x);
if (val >= INF) return {x};
for (int i = 0; i < n; ++i) vis[i] = 0;
f = 0;
pair<int, int> tr = dfs2(x, val);
vector<int> a1 = perm(tr.first), a2 = perm(tr.second);
vector<int> ans = a1;
for (int i = 0; i < (int)a2.size(); ++i) ans.push_back(a2[i]);
return ans;
}
int main() {
int m;
cin >> n >> m;
graph g(n);
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
g.add_edge(a - 1, b - 1, c);
}
g.gomory_hu();
vector<edge> tree = g.tree;
vector<int> parent = g.parent;
par.resize(n);
par[0] = {-1, 0};
adj.resize(n);
long long ans = 0;
for (int i = 0; i < (int)tree.size(); ++i) {
if (parent[tree[i].src] == tree[i].dst) {
par[tree[i].src] = {tree[i].dst, tree[i].capacity};
} else {
par[tree[i].dst] = {tree[i].src, tree[i].capacity};
}
cerr << tree[i].src << " " << tree[i].dst << " " << tree[i].capacity
<< endl;
ans += tree[i].capacity;
int a = adj[tree[i].src].size();
int b = adj[tree[i].dst].size();
adj[tree[i].src].push_back({tree[i].dst, tree[i].capacity, b, 0});
adj[tree[i].dst].push_back({tree[i].src, tree[i].capacity, a, 0});
}
vector<int> p = perm(0);
cout << ans << endl;
for (int i = 0; i < n; ++i) cout << p[i] + 1 << " ";
cout << endl;
return 0;
}
| 11 | CPP |
a = int(input())
for i in range(0,a):
a,b,c,d = map(int,input().split())
print(a,c,c)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int A[100000 + 5], frec[100000 + 5];
int main() {
int n;
long long sum = 0LL;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> A[i], sum += A[i];
}
if (abs(sum) % 3 != 0)
puts("0");
else {
long long S = sum / 3, acum = 0LL;
frec[n] = 0;
for (int i = n - 1; i >= 0; i--) {
acum += A[i];
frec[i] = frec[i + 1];
if (acum == S) frec[i]++;
}
long long ways = 0LL;
acum = 0LL;
for (int i = 0; i < n; ++i) {
acum += A[i];
if (acum == S && i + 2 < n) ways += frec[i + 2];
}
cout << ways << endl;
}
}
| 9 | CPP |
n=int(input())
c=0
while n>0:
if n>=100:
n-=100
elif n>=20 and n<100:
n-=20
elif n>=10 and n<20:
n -= 10
elif n>=5 and n<10:
n-=5
elif n>=1 and n<5:
n-=1
c+=1
print(c) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T min(T a, T b, T c, T d) {
return min(a, min(b, min(c, d)));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
template <class T>
T max(T a, T b, T c, T d) {
return max(a, max(b, max(c, d)));
}
bool cmp(const int& a, const int& b) { return a > b; }
unsigned long long __Gcd(unsigned long long a, unsigned long long b) {
return b ? __Gcd(b, a % b) : a;
}
unsigned long long a[1000007];
int main() {
unsigned long long p, q;
cin >> p >> q;
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i]);
unsigned long long _p = 0LL;
unsigned long long _q = 1LL;
for (int i = (n); i >= (1); --i) {
_p += a[i] * _q;
if (log(a[i]) + log(_q) > log((unsigned long long)1e18)) {
puts("NO");
return 0;
}
unsigned long long GCD = __Gcd(_p, _q);
swap(_p, _q);
_p /= GCD;
_q /= GCD;
}
swap(_p, _q);
unsigned long long __GCD = __Gcd(p, q);
p /= __GCD;
q /= __GCD;
if (_p == p && _q == q)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
str1=input()
str1=str1.lower()
list1=[]
list2=['a','e','i','o','u','y']
for char in str1:
if char not in list2:
list1.append('.')
list1.append(char)
str2="".join(list1)
print(str2) | 7 | PYTHON3 |
"""
Phone Numbers
"""
n = int(input())
nString = input()
eights = nString.count('8')
strings = n //11
print(strings if(strings <= eights) else eights) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 1e5 + 5;
template <class T>
inline bool Read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
void Out(int a) {
if (a < 0) {
putchar('-');
a = -a;
}
if (a >= 10) Out(a / 10);
putchar(a % 10 + '0');
}
double num[110000];
int main() {
int a, b, c;
scanf("%d%d%d", &(a), &(b), &(c));
for (int(i) = (0); (i) < (a); ++(i)) {
Read(num[i]);
}
double res = 0;
sort(num, num + a, greater<int>());
int b1, c1;
c1 = max(b, c);
b1 = min(b, c);
for (int(i) = (0); (i) < (b1); ++(i)) {
res += (num[i] / b1);
}
for (int(i) = (b1); (i) < (b1 + c1); ++(i)) {
res += (num[i] / c1);
}
printf("%.8f\n", res);
}
| 8 | CPP |
# @Date : 2016-08-22 19:21:32
# @Problem :
import unittest
from random import randint, shuffle
from sys import maxsize
class StressTest(unittest.TestCase):
known_values = (
# A, expected
("e4", 8),
)
def test_known_cases(self):
for A, expected in self.known_values:
self.assertEqual(expected, NaiveSolution().kingMoves(A))
def test_all_cases(self):
while True:
break
class NaiveSolution:
def kingMoves(self, A):
cols = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7}
rows = {'8':0, '7':1, '6':2, '5':3, '4':4, '3':5, '2':6, '1':7}
r, c = (rows[A[1]], cols[A[0]])
count = 0
# top left diagonal
if r-1 >= 0 and c-1 >= 0: count += 1
# top right diagonal
if r-1 >= 0 and c+1 <= 7: count += 1
# bottom left diagonal
if r+1 <= 7 and c-1 >= 0: count += 1
# bottom right diagonal
if r+1 <= 7 and c+1 <= 7: count += 1
# top
if r-1 >= 0: count += 1
# bottom
if r+1 <= 7: count += 1
# left
if c-1 >= 0: count += 1
# right
if c+1 <= 7: count += 1
return count
class BestSolution:
pass
if __name__ == '__main__':
#unittest.main()
ans = NaiveSolution().kingMoves(input().strip())
print(ans)
| 7 | PYTHON3 |
import sys
input=sys.stdin.readline
'''import io, os
import bisect
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline'''
power=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32]
t=int(input())
for i in range(t):
n=int(input())
arr=[int(i) for i in input().split()]
alt=[arr[0]]
for j in range(1,len(arr)):
if arr[j]>0 and alt[-1]<0 or arr[j]<0 and alt[-1]>0:
alt.append(arr[j])
elif arr[j]>0 and alt[-1]>0 :
alt[-1]=max(alt[-1],arr[j])
elif arr[j]<0 and alt[-1]<0:
alt[-1]=max(alt[-1],arr[j])
print(sum(alt))
| 9 | PYTHON3 |
s = str(input())
if 'h' in s:
a1 = int(s.find('h'))
if s.find('e', a1) > a1:
a1 = int(s.find('e', a1))
if s.find('l', a1) > a1:
a1 = int(s.find('l', a1))
if s.find('l', (a1+1)) > a1:
a1 = int(s.find('l', a1) )
if s.find('o', a1) > a1:
a1 = int(s.find('o', a1))
print('YES')
else:
print('NO')
else:
print('NO')
else:
print('NO')
else:
print('NO')
else:
print('NO') | 7 | PYTHON3 |
// D - Maximum Average Sets
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using DBL = double;
#define allr(x) (x).rbegin(),(x).rend()
#define rp(i,s,e) for(ll i=(s);i<(e);++i)
#define rpz(i,e) rp(i,0,e)
#define MAX 50
vvl nCr(MAX+1, vl(MAX+1, 0));
void nCr_table_create(ll N){
rp(n, 0, N+1) rp(r, 0, n+1){
if(r==0 || n==r) nCr[n][r] = 1;
else nCr[n][r] = nCr[n-1][r-1] + nCr[n-1][r];
}
}
int main(){
int N,A,B; cin>>N>>A>>B;
vl V(N); rpz(i, N) cin>>V[i];
sort(allr(V));
nCr_table_create(N);
DBL maxave = 0.0;
rpz(i, A) maxave += V[i];
maxave /= A;
int n = 0, r = 0;
rpz(i, N){
if(V[A-1] == V[i]){
n++;
if(i < A) r++;
}
}
ll maxcnt = 0;
// if(r == A) rp(r, A, B+1) maxcnt += nCr[n][r];
if(r == A) rp(r, A, min(n, B)+1) maxcnt += nCr[n][r];
else maxcnt = nCr[n][r];
printf("%.6f\n", maxave);
cout<< maxcnt <<endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int MIN(int a, int b) { return min(a, b); }
int MAX(int a, int b) { return max(a, b); }
int N, UP;
char in[25][25];
vector<vector<pair<int, int> > > base;
vector<vector<char> > cbase;
int go[45][30][25];
bool sosedi(pair<int, int> a, pair<int, int> b) {
return abs(a.first - b.first) + abs(a.second - b.second) == 1;
}
int* dp[45];
int main() {
scanf("%d", &N);
UP = 2 * N - 1;
for (int i = 0; i < N; ++i) {
scanf("%s", &in[i]);
}
for (int i = 0; i < N; ++i) {
base.push_back(vector<pair<int, int> >());
cbase.push_back(vector<char>());
for (int y = 0, x = i; x >= 0; --x, ++y)
base.back().push_back(make_pair(x, y)), cbase.back().push_back(in[x][y]);
}
for (int j = 1; j < N; ++j) {
base.push_back(vector<pair<int, int> >());
cbase.push_back(vector<char>());
for (int y = j, x = N - 1; y < N; --x, ++y)
base.back().push_back(make_pair(x, y)), cbase.back().push_back(in[x][y]);
}
for (int i = 0; i + 1 < ((int)(base).size()); ++i)
for (int j = 0; j < ((int)(base[i]).size()); ++j)
for (char c = 'a'; c <= 'z'; ++c)
for (int nbr = 0; nbr < ((int)(base[i + 1]).size()); ++nbr)
if (cbase[i + 1][nbr] == c && sosedi(base[i][j], base[i + 1][nbr]))
go[i][c - 'a'][j] |= (1 << nbr);
dp[((int)(base).size()) - 1] = new int[2];
dp[((int)(base).size()) - 1][1] = 0;
for (int cur = ((int)(base).size()) - 1; cur > 0; --cur) {
dp[cur - 1] = new int[1 << ((int)(base[cur - 1]).size())];
typeof(&MIN) relax;
if ((cur & 1) == 0) {
relax = &MAX;
fill(dp[cur - 1], dp[cur - 1] + (1 << ((int)(base[cur - 1]).size())),
-1000000000);
} else {
relax = &MIN;
fill(dp[cur - 1], dp[cur - 1] + (1 << ((int)(base[cur - 1]).size())),
1000000000);
}
int RS = ((int)(base[cur - 1]).size()), UPTO = (1 << RS);
for (int msk = 1; msk < UPTO; ++msk) {
int& VAL = dp[cur - 1][msk];
for (char put = 0; put < 26; ++put) {
int nmsk = 0, nval = 0;
if (put == 0)
++nval;
else if (put == 1)
--nval;
int cmsk = msk, i;
int* sft = go[cur - 1][put];
for (i = __builtin_ctz(cmsk); cmsk != 0;
cmsk ^= (1 << i), i = __builtin_ctz(cmsk))
nmsk |= sft[i];
if (nmsk != 0 && abs(dp[cur][nmsk]) != 1000000000)
VAL = relax(VAL, nval + dp[cur][nmsk]);
}
}
}
int val = dp[0][1];
if (in[0][0] == 'a') ++val;
if (in[0][0] == 'b') --val;
if (val > 0)
puts("FIRST");
else if (val < 0)
puts("SECOND");
else
puts("DRAW");
return 0;
}
| 8 | CPP |
n = int(input())
l = sorted([int(i)for i in input().split()])
if max(l) % 2 == 0:
SUM_SR =((max(l)+1)//2)*2 + 1
else:
SUM_SR = ((max(l) + 1) // 2) * 2
i = 0
while i < len(l)-1:
SUM_SR -= l[i]
i+=1
print(SUM_SR) | 8 | PYTHON3 |
from collections import deque
n, d, a = map(int, input().split())
xh = sorted([list(map(int, input().split())) for _ in range(n)])
ans = 0
dam_sum = 0
q = deque()
for i, (x, h) in enumerate(xh):
while q and q[0][0] < x:
ran, dam = q.popleft()
dam_sum -= dam
h -= dam_sum
if h <= 0:
continue
c = -(-h//a)
ans += c
dam_sum += c*a
q.append((x+d*2, c*a))
print(ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void BoostIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
long long const maxn = 100005;
double pi = 3.1415926535897932;
double eps = 1e-7;
long long mod = 1e9 + 7;
long long Pow(long long a, long long b) {
if (b == 0) return 1;
if (b % 2 == 0) {
return Pow(a, b / 2) * Pow(a, b / 2);
} else {
return Pow(a, b / 2) * Pow(a, b / 2) * a;
}
}
struct My {
long long a;
long long b;
long long c;
};
int32_t main() {
BoostIO();
long long n, l, r;
cin >> n >> l >> r;
vector<long long> ost(3);
while (l <= r && l % 3 != 0) {
++ost[l % 3];
++l;
}
while (r >= l && r % 3 != 2) {
++ost[r % 3];
--r;
}
long long k = (r - l + 1) / 3;
for (long long i = 0; i < 3; ++i) {
ost[i] += k;
}
vector<vector<long long>> dp(n + 1, vector<long long>(3));
dp[1][0] = ost[0];
dp[1][1] = ost[1];
dp[1][2] = ost[2];
for (long long i = 2; i <= n; ++i) {
dp[i][0] =
((dp[i - 1][0] * ost[0] % mod + dp[i - 1][1] * ost[2] % mod) % mod +
(dp[i - 1][2] * ost[1]) % mod) %
mod;
dp[i][1] =
((dp[i - 1][0] * ost[1] % mod + dp[i - 1][1] * ost[0] % mod) % mod +
(dp[i - 1][2] * ost[2]) % mod) %
mod;
dp[i][2] =
((dp[i - 1][0] * ost[2] % mod + dp[i - 1][1] * ost[1] % mod) % mod +
(dp[i - 1][2] * ost[0]) % mod) %
mod;
}
cout << dp[n][0] << endl;
return 0;
}
| 9 | CPP |
n,m=map(int,input().split())
arr=[int(x) for x in input().split()]
k=0
arr2=[]
sumb=0
for i in range(n):
arr2.sort(reverse=True)
suma=sumb
tot=0
j=0
len1=i
#print(arr)
#print(arr2)
while suma+arr[i]>m and j<len1:
suma-=arr2[j]
j+=1
tot+=1
if j==len1:
break
arr2.append(arr[i])
sumb+=arr[i]
print(tot,end=" ")
print() | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<bool> vb;
typedef vector<pair<int,int>> vpi;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define pi pair<int, int>
#define mp make_pair
#define F first
#define S second
int n, x;
ll MOD = 998244353, ans;
vll a, two;
char c;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
a.rsz(n);
F0R(i, n) {
cin >> c;
if (c=='-') a[i]=0;
else cin >> a[i];
}
two.rsz(n+1);
two[0]=1;
FOR(i, 1, n+1) two[i]=(2*two[i-1])%MOD;
ans=0;
F0R(i, n) if (a[i]>0) {
// cout << "dp on " << a[i] << endl;
vll dp(n+1, 0);
dp[0]=1;
F0R(j, n) {
if (a[j]==0) {
if (j<i) dp[0]=(2*dp[0])%MOD;
F0R(k, n) dp[k] = (dp[k]+dp[k+1])%MOD;
}
else {
if (a[j]<a[i] || (a[j]==a[i] && j<i)) R0F(k, n) dp[k+1] = (dp[k+1]+dp[k])%MOD;
else if (j != i) F0R(k, n+1) dp[k]=(2*dp[k])%MOD;
}
// F0R(k, n+1) cout << dp[k] << ' '; cout << endl;
}
F0R(j, n+1) ans = (ans + a[i]*dp[j])%MOD;
}
cout << ans << endl;
}
| 10 | CPP |
#include <bits/stdc++.h>
#pragma GCC target("sse4.2")
using namespace std;
bool home = 1;
signed realMain();
signed main() {
home = 0;
if (home) {
freopen("tony_stark", "r", stdin);
} else {
ios::sync_with_stdio(0);
cin.tie(0);
}
realMain();
}
const int M = (int)1e9 + 7;
int add(int a, int b) {
a += b;
if (a >= M) return a - M;
if (a < 0) return a + M;
return a;
}
int mul(int a, int b) { return a * (long long)b % M; }
int pw(int a, int b) {
int r = 1;
while (b) {
if (b & 1) r = mul(r, a);
a = mul(a, a);
b /= 2;
}
return r;
}
int dv(int a, int b) { return mul(a, pw(b, M - 2)); }
const int N = (int)1e5 + 7;
const int T = 1;
int q, n, fact[N], ifact[N], memo[T][N], p25[N], p26[N], now[N];
string s;
int comb(int n, int k) { return mul(fact[n], mul(ifact[k], ifact[n - k])); }
void job() {
for (int len = 1; len < N; len++) {
if (len - n < 0)
now[len] = 0;
else
now[len] =
add(mul(26, now[len - 1]), mul(comb(len - 1, n - 1), p25[len - n]));
}
}
signed realMain() {
p25[0] = 1;
p26[0] = 1;
for (int i = 1; i < N; i++) p25[i] = mul(p25[i - 1], 25);
for (int i = 1; i < N; i++) p26[i] = mul(p26[i - 1], 26);
fact[0] = 1;
for (int i = 1; i < N; i++) fact[i] = mul(fact[i - 1], i);
ifact[N - 1] = dv(1, fact[N - 1]);
for (int i = N - 2; i >= 0; i--) ifact[i] = mul(ifact[i + 1], i + 1);
for (n = 1; n < T; n++) {
job();
for (int j = 0; j < N; j++) {
memo[n][j] = now[j];
}
}
cin >> q >> s;
n = (int)s.size();
job();
while (q--) {
int t;
cin >> t;
if (t == 1) {
cin >> s;
n = (int)s.size();
job();
} else {
int len;
cin >> len;
cout << now[len] << "\n";
if (n < T) {
assert(now[len] == memo[n][len]);
}
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int len = 400;
const int MOD = 1e9 + 7;
const int C = 150;
const long long MAXK = 1e4;
const long long MAXH = 10004205361450474;
long long dp[6][MAXK + 1];
long long f(int q, long long l);
vector<long long> gena(int q, long long l) {
long long k = min(l, MAXK);
vector<long long> a = {f(q - 1, l)};
for (int i = 1; i < k; ++i) a.push_back(min(MAXH, f(q - 1, a.back() + 1)));
return a;
}
long long f(int q, long long l) {
if (q == 0) return l;
int k = min(l, MAXK);
if (dp[q][k]) return dp[q][k] - k + l;
vector<long long> a = gena(q, l);
long long ans = f(q - 1, a.back() + 1);
dp[q][k] = ans - l + k;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
long long l = 1;
for (int q = 5; q > 0; --q) {
vector<long long> a = gena(q, l);
cout << a.size() << ' ';
for (long long val : a) cout << val << ' ';
cout << endl;
int type;
cin >> type;
if (type == -1)
return 0;
else if (type > 0)
l = a[type - 1] + 1;
}
return 0;
}
| 13 | CPP |
# coding: utf-8
q = int(input())
response = []
while q > 0:
n, r = map(int, input().split())
monsters = sorted(set(map(int, input().split())))
# print(monsters)
c = 0
while len(monsters) != 0 and monsters[-1] > r * c:
monsters.pop()
c += 1
response.append(c)
q -= 1
for r in response:
print(r)
| 8 | PYTHON3 |
#import<iostream>
int main(){int a;std::cin>>a;std::cout<<1110-a;} | 0 | CPP |
def find_prime_in_range(a, b):
for p in range(a, b):
for i in range(2, p):
if p % i == 0:
break
else:
return p
return None
n,m = map(int,input().split())
a = find_prime_in_range(n+1, 2*n)
if m==a:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
int main(){
int N, a, b;
while(cin >> N >> a >> b){
--a; --b;
vector<long long> x(N), y(N);
set<int> rest;
for(int i=0;i<N;i++){
if(i != a) rest.insert(i);
}
for(int i=0;i<N;i++){
long long p, q; cin >> p >> q;
x[i] = p-q;
y[i] = p+q;
}
int targetD = max(abs(x[a]-x[b]), abs(y[a]-y[b]));
map<long long, set< pair<int, int> > > h, v;
map<long long, vector<int> > hAll, vAll;
for(int i=0;i<N;i++){
h[x[i]].insert(make_pair(y[i], i));
v[y[i]].insert(make_pair(x[i], i));
hAll[x[i]].push_back(y[i]);
vAll[y[i]].push_back(x[i]);
}
for(auto it=hAll.begin();it!=hAll.end();it++){
sort(it->second.begin(), it->second.end());
}
for(auto it=vAll.begin();it!=vAll.end();it++){
sort(it->second.begin(), it->second.end());
}
long long res = 0;
queue<int> qu; qu.push(a);
while(!qu.empty()){
int pos = qu.front(); qu.pop();
auto checkH = [&](int checkX, int centerY){
auto itF = lower_bound(hAll[checkX].begin(), hAll[checkX].end(), centerY-targetD);
auto itE = lower_bound(hAll[checkX].begin(), hAll[checkX].end(), centerY+targetD+1);
if(itF == itE) return;
res += distance(itF, itE);
auto itF2 = h[checkX].lower_bound(make_pair(centerY-targetD, 0));
auto itE2 = h[checkX].lower_bound(make_pair(centerY+targetD+1, 0));
for(auto it=itF2;it!=itE2;it++){
auto it2 = rest.find(it->second);
if(it2 != rest.end()){
qu.push(it->second);
rest.erase(it2);
}
}
h[checkX].erase(itF2, itE2);
};
auto checkV = [&](int centerX, int checkY){
auto itF = lower_bound(vAll[checkY].begin(), vAll[checkY].end(), centerX-targetD+1);
auto itE = lower_bound(vAll[checkY].begin(), vAll[checkY].end(), centerX+targetD);
if(itF == itE) return;
res += distance(itF, itE);
auto itF2 = v[checkY].lower_bound(make_pair(centerX-targetD+1, 0));
auto itE2 = v[checkY].lower_bound(make_pair(centerX+targetD, 0));
for(auto it=itF2;it!=itE2;it++){
auto it2 = rest.find(it->second);
if(it2 != rest.end()){
qu.push(it->second);
rest.erase(it2);
}
}
itF2 = v[checkY].lower_bound(make_pair(centerX-targetD, 0));
itE2 = v[checkY].lower_bound(make_pair(centerX+targetD+1, 0));
v[checkY].erase(itF2, itE2);
};
checkH(x[pos]-targetD, y[pos]);
checkH(x[pos]+targetD, y[pos]);
checkV(x[pos], y[pos]-targetD);
checkV(x[pos], y[pos]+targetD);
}
cout << res/2 << endl;
}
}
| 0 | CPP |
n = int(input())
D = []
for _ in range(n):
D.append(tuple(map(int, input().split())))
D.sort()
date = 0
for d in D:
if date <= min(d[0], d[1]):
date = min(d[0], d[1])
else:
date = d[0]
print(date)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
pair<int, int> p[1000006];
pair<int, int> H[1000006];
vector<int> v[1000006];
map<pair<int, int>, int> M;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
p[0] = {1, 1};
for (int i = 1; i <= n; i++) {
p[i].first = 1LL * p[i - 1].first * 5 % 1000000007;
p[i].second = 1LL * p[i - 1].second * 5 % 31183;
}
for (int i = 1; i <= n; i++) {
for (auto it : v[i]) {
H[i].first = (H[i].first + p[it].first) % 1000000007;
H[i].second = (H[i].second + p[it].second) % 31183;
}
M[{H[i].first, H[i].second}]++;
}
long long ans = 0;
for (auto it : M) {
ans = ans + 1LL * it.second * (it.second - 1) / 2;
}
for (int i = 1; i <= n; i++) {
for (auto it : v[i]) {
if (i >= it) continue;
pair<int, int> a, b;
a.first = (H[it].first - p[i].first + 1000000007) % 1000000007;
a.second = (H[it].second - p[i].second + 31183) % 31183;
b.first = (H[i].first - p[it].first + 1000000007) % 1000000007;
b.second = (H[i].second - p[it].second + 31183) % 31183;
if (a == b) ans++;
}
}
cout << ans << '\n';
return 0;
}
| 9 | CPP |
m, d = map(int, input().split())
ans = 0
for i in range(1, d+1):
a, b = divmod(i, 10)
if a >= 2 and b >= 2:
if 1 <= a*b <= m:
ans += 1
print(ans)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
using namespace std;
const int N = 1e5 + 5;
long long a[N];
int main() {
int n, m, l;
scanf("%d %d %d", &n, &m, &l);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (a[i] > l) {
if (i == 1 || a[i - 1] <= l) cnt++;
}
}
int ty, p, d;
while (m--) {
scanf("%d", &ty);
if (ty == 0)
printf("%d\n", cnt);
else {
scanf("%d %d", &p, &d);
if (a[p] <= l) {
a[p] += d;
if (a[p] > l) {
if (p == 1 || a[p - 1] <= l) {
if (p == n || a[p + 1] <= l) cnt++;
}
if (p != 1 && p != n && a[p - 1] > l && a[p + 1] > l) cnt--;
}
} else
a[p] += d;
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long dv=998244353;
long long n,cr,dp[369][669],ps[2][669],tmp[669],pwk;
vector<long long> al[369];
bitset<369> vtd;
long long pw(long long x,long long y)
{
if(!y)
{
return 1;
}
pwk=pw(x,y/2);
pwk=pwk*pwk%dv;
if(y%2)
{
pwk=pwk*x%dv;
}
return pwk;
}
void dfs(long long x)
{
long long i,j,ii,sz=al[x].size(),l;
vtd[x]=1;
for(i=0;i<=cr*2+1;i++)
{
dp[x][i]=0;
}
dp[x][0]=1;
dp[x][cr+1]=1;
for(i=0;i<sz;i++)
{
l=al[x][i];
if(!vtd[l])
{
dfs(l);
for(j=cr*2+1;j;j--)
{
dp[l][j]=dp[l][j-1];
}
dp[l][0]=0;
for(ii=0;ii<2;ii++)
{
for(j=0;j<=cr*2+1;j++)
{
ps[ii][j+1]=(ps[ii][j]+dp[x][j])%dv;
}
swap(x,l);
}
for(j=0;j<=cr*2+1;j++)
{
tmp[j]=(dv-dp[x][j]*dp[l][j]%dv)%dv;
for(ii=0;ii<2;ii++)
{
tmp[j]=(tmp[j]+dp[x][j]*(ps[!ii][max(cr*2+2-j,j+1)]+dv-ps[!ii][min(j,cr*2+2-j)]))%dv;
swap(x,l);
}
}
for(j=0;j<=cr*2+1;j++)
{
dp[x][j]=tmp[j];
}
}
}
}
int main()
{
long long i,j,k,l,sm,z=0;
scanf("%lld",&n);
for(i=0;i<n-1;i++)
{
scanf("%lld%lld",&k,&l);
al[k].push_back(l);
al[l].push_back(k);
}
l=0;
for(i=0;i<n;i++)
{
cr=i;
vtd.reset();
dfs(1);
sm=0;
for(j=0;j<=i;j++)
{
sm=(sm+dp[1][j])%dv;
}
z=(z+(sm+dv-l)*(i+dv-1))%dv;
l=sm;
}
z=(z+n)%dv;
z=z*pw(pw(2,n),dv-2)%dv;
printf("%lld\n",z);
}
| 12 | CPP |
n,k = [int(x) for x in input().split()]
car = list(input())
d = {}
for i in range(n):
d[car[i]] = d.get(car[i],0)+1
sortedd = sorted(d.items(),key=lambda x:x[1])
coins = 0
while k>0:
x = sortedd[len(sortedd)-1][1]
if x>k:
coins+=k*k
k = 0
else:
k = k-x
coins+=(x)*x
# print(coins,x)
sortedd.pop()
print(coins)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, ans;
cin >> n >> a >> b;
cout << n - max(a + 1, n - b) + 1;
}
| 7 | CPP |
#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
const int MX = 500001;
int n, l[MX], r[MX], lf[MX], depth[MX];
ll dp[2][MX];
inline void rec(int c, int d = 0){
//if(c == n) return;
//rec(l[c], d + 1); rec(r[c], d + 1);
lf[c] += lf[l[c]] + lf[r[c]];
dp[0][c] += dp[0][l[c]] + lf[l[c]];
dp[0][c] += dp[0][r[c]] + lf[r[c]];
dp[1][c] = min(lf[l[c]] + dp[0][l[c]] + dp[1][r[c]], lf[r[c]] + dp[1][l[c]] + dp[0][r[c]]) + 1;
dp[1][c] = min(dp[1][c], 2 + d + dp[1][l[c]] + dp[1][r[c]]);
}
int main(){
cin >> n;
rep(i, n - 1) cin >> l[i + 1] >> r[i + 1];
vi ord;
queue<pi> q;
q.push(mp(1, 0));
while(!q.empty()){
int c = q.front().first, d = q.front().second; q.pop();
ord.pb(c);
depth[c] = d;
if(l[c] < n) q.push(mp(l[c], d + 1));
if(r[c] < n) q.push(mp(r[c], d + 1));
}
reverse(all(ord));
lf[n] = 1;
rep(i, n - 1) rec(ord[i], depth[ord[i]]);
cout << dp[1][1] << endl;
/*
rep(i, n) cerr<<dp[0][i + 1] << " " << dp[1][i + 1] << endl;
rep(i, n) dbg(lf[i + 1]);
*/
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 17;
vector<pair<int, int>> ve, vec[N];
bool f[N];
int dis[N], h[N], par[N], a[N], b[N], wi[N], ans[N], pos[N];
int root(int x) {
while (par[x] != 0) {
x = par[x];
}
return x;
}
void merge(int x, int y) {
x = root(x);
y = root(y);
vec[x].clear();
vec[y].clear();
f[x] = false;
f[y] = false;
if (x == y) {
return;
}
if (h[x] > h[y]) {
par[y] = x;
} else {
par[x] = y;
if (h[x] == h[y]) {
h[y]++;
}
}
}
void add(int x, int y, int z) {
if (x == y) {
return;
}
ans[z] = 1;
vec[x].push_back({y, z});
vec[y].push_back({x, z});
}
void dfs(int x, int y, int hi) {
f[x] = true;
dis[x] = hi;
for (auto u : vec[x]) {
if (!f[u.first]) {
dfs(u.first, u.second, hi + 1);
dis[x] = min(dis[x], dis[u.first]);
} else if (u.second != y) {
dis[x] = min(dis[x], dis[u.first]);
}
}
if (dis[x] == hi) {
ans[y] = 2;
}
}
bool migam(int &aa, int &bb) { return wi[aa] < wi[bb]; }
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> wi[i];
pos[i] = i;
}
sort(pos + 1, pos + m + 1, migam);
for (int i = 1; i <= m;) {
int j;
if (i == 1) {
}
for (j = i; wi[pos[j]] == wi[pos[i]]; j++) {
add(root(a[pos[j]]), root(b[pos[j]]), pos[j]);
}
for (j = i; wi[pos[j]] == wi[pos[i]]; j++) {
int k = root(a[pos[j]]);
if (!f[k]) {
dfs(k, 0, 0);
}
}
for (j = i; wi[pos[i]] == wi[pos[j]]; j++) {
merge(a[pos[j]], b[pos[j]]);
}
i = j;
}
for (int i = 1; i <= m; i++) {
if (!ans[i]) {
cout << "none" << endl;
} else if (ans[i] == 1) {
cout << "at least one" << endl;
} else {
cout << "any" << endl;
}
}
return 0;
}
| 10 | CPP |
n = int(input())
A = list(map(int,input().split(" ")))
for i in range(0,n,1):
if A[i] % 2 == 0:
A[i] -= 1
print(A[i],end = ' ')
else:
print(A[i],end = ' ') | 7 | PYTHON3 |
/*Lucky_Glass*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=55;
typedef long long ll;
template<class T>inline T Read(T &r){
int b=1,c=getchar();r=0;
while(c<'0' || '9'<c) b=c=='-'? -1:b,c=getchar();
while('0'<=c && c<='9') r=(r<<1)+(r<<3)+(c^'0'),c=getchar();
return r*=b;
}
#define cmin(a,b) (a=min(a,b))
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,m;
ll cst[N],lim[N][2],f[N][N][2][2][N],g[N][N][N][2],h[N][2];
ll sta[N][N][2];
ll F(int L,int R,int bl,int br,int H){
if(H>=m) return L<=R? INF:0;
if(~f[L][R][bl][br][H]) return f[L][R][bl][br][H];
ll &u=f[L][R][bl][br][H],lasv;
u=INF;
if(L==1 || R==n) cmin(u,F(L,R,bl,br,H+1));
else cmin(u,F(L,R,bl,br,H+1)+((lim[L-1][bl]^lim[R+1][br])>>H&1)*cst[H]);
for(int i=L-1;i<=R;i++) g[L][R][i][0]=g[L][R][i][1]=INF;
g[L][R][L-1][bl]=0;
for(int i=L;i<=R;i++)
for(int p=0;p<2;p++){
if(sta[i][H][p]==-1) continue;
for(int j=L-1;j<i;j++)
for(int q=0;q<2;q++){
if(g[L][R][j][q]==INF) continue;
if(j==L-1){
lasv=j>=1? lim[L-1][bl]:sta[i][H][p];
cmin(g[L][R][i][p],g[L][R][j][q]+F(j+1,i-1,q,p,H+1)+((lasv^sta[i][H][p])>>H&1)*cst[H]);
}
else cmin(g[L][R][i][p],g[L][R][j][q]+F(j+1,i-1,q,p,H+1)+((sta[j][H][q]^sta[i][H][p])>>H&1)*cst[H]);
}
if(R+1<=n) lasv=lim[R+1][br];
else lasv=sta[i][H][p];
cmin(u,g[L][R][i][p]+F(i+1,R,p,br,H+1)+((sta[i][H][p]^lasv)>>H&1)*cst[H]);
}
return u;
}
int main(){
//freopen("input.in","r",stdin);
memset(sta,-1,sizeof sta);
memset(f,-1,sizeof f);
Read(n),Read(m);
for(int i=1;i<=n;i++){
Read(lim[i][0]),Read(lim[i][1]);
if(lim[i][1]-lim[i][0]<=1){
sta[i][0][0]=lim[i][0];
sta[i][0][1]=lim[i][1];
}
else{
for(int j=m-1;j>=0;j--)
if((lim[i][0]^lim[i][1])>>j&1){
for(int k=j-1;k>=0;k--){
if(!(lim[i][0]>>k&1))
sta[i][k][0]=lim[i][0]^(1ll<<k);
if(lim[i][1]>>k&1)
sta[i][k][1]=lim[i][1]^(1ll<<k);
}
break;
}
}
}
for(int i=0;i<m;i++) Read(cst[i]);
for(int i=1;i<=n+1;i++) h[i][0]=h[i][1]=INF;
for(int i=1;i<=n+1;i++){
for(int j=0;j<i;j++){
cmin(h[i][0],h[j][0]+F(j+1,i-1,0,0,0));
cmin(h[i][0],h[j][1]+F(j+1,i-1,1,0,0));
cmin(h[i][1],h[j][0]+F(j+1,i-1,0,1,0));
cmin(h[i][1],h[j][1]+F(j+1,i-1,1,1,0));
}
}
printf("%lld\n",h[n+1][0]);
return 0;
} | 11 | CPP |
test = int(input())
list = list(map(int, input().split()))
list.sort()
a = list[-1] - list[0]
b = a//2 if a%2 == 0 else a
extra = b+list[0]
flag = False
for i in list[1:]:
if i+b != extra and i-b != extra and i!=extra:
flag = True
break
if flag:
print(-1)
elif a % 2 == 0:
print(a//2)
else:
print(a) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (scanf("%d", &n) == 1) {
vector<int> a(n);
vector<long long> c(n);
vector<long long> A(n);
for (int i = (0); i <= ((n)-1); ++i) scanf("%d", &a[i]);
sort((a).begin(), (a).end());
long long s = 0, SA = 0;
for (int i = (0); i <= ((n)-1); ++i)
s += a[i], c[i] = s, A[i] = (long long)a[i] * (long long)(n - 1 - i),
SA += A[i];
int q;
scanf("%d", &q);
for (int i = (0); i <= ((q)-1); ++i) {
int Q;
scanf("%d", &Q);
if (Q > 1) {
long long C = 0;
long long R = n - 1;
long long ki = Q;
while (R > 0) {
C += c[R - 1];
R -= ki;
ki *= Q;
}
cout << C << endl;
} else {
cout << SA << endl;
}
}
}
return 0;
}
| 10 | CPP |
t = int(input())
for _ in range(t):
n = int(input())
s = input()
print((n-s.count('10') - s.count('01')) // 2)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 16;
int n, c[N], a[N], b[N], dp[1 << N][2][N * N + 10];
int main() {
scanf("%d", &n);
int R = 0, B = 0;
for (int i = 0; i < n; i++) {
char s[2];
scanf("%s", s);
scanf("%d %d", &a[i], &b[i]);
R += max(0, a[i] - n), B += max(0, b[i] - n);
a[i] = min(a[i], n), b[i] = min(b[i], n);
c[i] = (s[0] == 'R');
}
int tot = 1 << n, up = n * n;
memset(dp, 0x3f3f3f3f, sizeof(dp));
if (R > B)
dp[0][0][min(up, R - B)] = 0;
else
dp[0][1][min(up, B - R)] = 0;
for (int i = 1; i < tot; i++) {
int red = 0, blue = 0;
for (int j = 0; j < n; j++) {
if ((1 << j) & i) {
if (c[j])
red++;
else
blue++;
}
}
for (int j = 0; j < n; j++) {
if (!((1 << j) & i)) continue;
int p = i ^ (1 << j), pred = red, pblue = blue;
if (c[j])
pred--;
else
pblue--;
int r_re = max(0, a[j] - pred), b_re = max(0, b[j] - pblue);
for (int k = 0; k <= up; k++) {
if (dp[p][1][k] < 0x3f3f3f3f) {
int add = max(0, max(r_re - k, b_re)), ar = k + add - r_re,
br = add - b_re;
ar = min(ar, up), br = min(br, up);
if (!ar) dp[i][0][br] = min(dp[i][0][br], dp[p][1][k] + add + 1);
if (!br) dp[i][1][ar] = min(dp[i][1][ar], dp[p][1][k] + add + 1);
}
if (dp[p][0][k] < 0x3f3f3f3f) {
int add = max(0, max(r_re, b_re - k)), ar = add - r_re,
br = k + add - b_re;
ar = min(ar, up), br = min(br, up);
if (!ar) dp[i][0][br] = min(dp[i][0][br], dp[p][0][k] + add + 1);
if (!br) dp[i][1][ar] = min(dp[i][1][ar], dp[p][0][k] + add + 1);
}
}
}
}
int ans = 0x3f3f3f3f;
R = max(R, B);
for (int i = 0; i <= up; i++) {
for (int w = 0; w < 2; w++) {
ans = min(ans, dp[tot - 1][w][i] + R);
}
}
printf("%d\n", ans);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const int MAX = 4007;
long long int ans[MAX], d[MAX];
long long int e[MAX][MAX];
int main() {
long long int n;
cin >> n;
for (int i = 1; i < MAX; i++) {
e[i][i] = 1;
e[i][0] = 1;
e[i][1] = i;
}
for (int i = 2; i < MAX; i++) {
for (int j = 2; j < i; j++) {
e[i][j] = (e[i - 1][j] + e[i - 1][j - 1]) % MOD;
}
}
ans[1] = 1;
d[1] = 1;
d[0] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
ans[i] = (ans[i] + (e[i][j] * d[i - j])) % MOD;
}
for (int j = 0; j <= i - 1; j++) {
d[i] = (d[i] + e[i - 1][j] * d[i - j - 1]) % MOD;
}
}
cout << ans[n] << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
int A[200005];
int block = 450;
vector<int> L[200005];
int vs[400005];
vector<pair<int, int> > Len;
vector<int> Exit[200005];
vector<int> T;
int dp[200005];
int lo;
int force(int D, int V) {
T.clear();
int bal = 0;
vs[bal + n] = 0;
T.push_back(bal);
int ans = 0;
for (int i = 1; i <= n; i++) {
if (A[i] == D)
bal++;
else if (A[i] == V)
bal--;
if (vs[bal + n] >= 0) {
ans = max(ans, i - vs[bal + n]);
}
if (vs[bal + n] == -1) {
T.push_back(bal);
vs[bal + n] = i;
}
}
for (int v : T) {
vs[v + n] = -1;
}
return ans;
}
void smallForce(int D, int V, int len) {
int N = L[V].size();
for (int i = 0; i + len - 1 < N; i++) {
int l = i, r = i + len - 1;
Len.push_back(make_pair(L[V][i], L[V][r]));
}
}
int solve(int D, int len) {
int ans = 0;
int bal = 0;
for (int i = 0; i <= L[D].size(); i++) dp[i] = -1;
lo = 0;
for (auto it : Len) {
int a = it.first, b = it.second;
Exit[b].push_back(a);
}
dp[0] = 0;
for (int i = 1; i <= n; i++) {
if (A[i] == D) bal++;
for (int a : Exit[i]) {
lo = max(lo, a);
}
if (bal >= len) {
int ll = dp[bal - len] + 1;
if (lo >= ll) {
ans = max(ans, i - ll + 1);
}
}
if (dp[bal] == -1) dp[bal] = i;
}
for (auto it : Len) {
Exit[it.second].clear();
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
L[A[i]].push_back(i);
}
int D = 1;
for (int i = 1; i <= n; i++) {
if (L[i].size() > L[D].size()) D = i;
}
memset(vs, -1, sizeof(vs));
int ans = 0;
for (int i = 1; i <= n; i++) {
if (D == i) continue;
if (L[i].size() >= block) {
ans = max(ans, force(D, i));
}
}
for (int len = 1; len < block; len++) {
Len.clear();
for (int j = 1; j <= n; j++) {
if (j == D) continue;
if (L[j].size() >= len) {
smallForce(D, j, len);
}
}
ans = max(ans, solve(D, len));
}
printf("%d\n", ans);
return 0;
}
| 10 | CPP |
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++)
#define MAX 100
class State {
public:
int p, b, cost;
State(int p = 0, int b = 0, int cost = 0) :
p(p), b(b), cost(cost) {
}
};
int n, B[MAX][2];
int bfs() {
bool V[MAX][2];
queue<State> Q;
rep(i, n)
rep(j, 2)
V[i][j] = false;
int s0 = 0, s1 = 0;
if (B[s0][0] == 1)
while (s0 + 1 < n && B[s0 + 1][0] == 1)
s0++;
if (B[s1][1] == 1)
while (s1 + 1 < n && B[s1 + 1][1] == 1)
s1++;
V[s0][0] = V[s1][1] = true;
Q.push(State(s0, 0, 0));
Q.push(State(s1, 1, 0));
State u, v;
while (!Q.empty()) {
u = Q.front();
Q.pop();
if (u.p == n - 1)
return u.cost;
for (int r = 0; r < 3; r++) {
int np = u.p + r;
int nb = (u.b + 1) % 2;
if (np >= n)
continue;
if (B[np][nb] == 2)
while (B[np][nb] == 2)
np--;
if (B[np][nb] == 1)
while (np + 1 < n && B[np + 1][nb] == 1)
np++;
if (V[np][nb])
continue;
V[np][nb] = true;
Q.push(State(np, nb, u.cost + 1));
}
}
return -1;
}
int main() {
while( cin >> n ) {
if ( n == 0 ) break;
rep(i, n) cin >> B[i][0];
rep(i, n) cin >> B[i][1];
int cost = bfs();
if ( cost < 0 ) cout << "NA" << endl;
else cout << cost << endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
const int N = 2e5 + 5;
int n, k, b, c;
long long a[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k >> b >> c;
b = min(b, 5 * c);
for (int i = (0); i < (n); i++) {
cin >> a[i];
a[i] += 1e9 + 5;
}
sort(a, a + n);
long long ans = LLONG_MAX;
for (int m = (0); m < (5); m++) {
long long sum = 0;
priority_queue<long long> Q;
for (int i = (0); i < (n); i++) {
int d = ((m - a[i]) % 5 + 5) % 5;
long long x = d * c - ((a[i] + d) / 5) * b;
sum += x;
Q.push(x);
if (((int)(Q).size()) > k) {
sum -= Q.top();
Q.pop();
}
if (((int)(Q).size()) == k) {
ans = min(ans, sum + ((a[i] + d) / 5) * b * k);
}
}
}
cout << ans << '\n';
}
| 10 | CPP |
# https://codeforces.com/problemset/problem/119/A
from math import gcd
a, b, heap = [int(i) for i in input().split(" ")]
simonMove = True
while True:
stonesToTake = gcd(a if simonMove else b, heap)
simonMove = not simonMove
if stonesToTake > heap:
break
else:
heap = heap - stonesToTake
print("0" if simonMove else "1")
| 7 | PYTHON3 |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)
int main(){
int n,m,t;
cin>>n>>m>>t;
vector<int> a(n);
rep(i,n)cin>>a[i];
int ans = 0;
ans+=a[0]-m;
ans+=max(0,t-m-a[a.size()-1]);
rep(i,a.size()-1){
if(a[i+1]-a[i]>2*m)ans+=a[i+1]-a[i]-2*m;
}
cout<<ans<<endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define CHMIN(a,b) (a)=min((a),(b))
#define CHMAX(a,b) (a)=max((a),(b))
typedef long long ll;
int N,A,B;
int a[830], b[830];
int c[830];
bool g[830][830];
int main(){
scanf("%d%d%d",&N,&A,&B);
REP(i,N)scanf("%d%d",a+i,b+i);
REP(i,N)c[i] = a[i]-b[i];
int ans = 0;
vector<int> pos, neg;
REP(i,N){
if(abs(c[i])<=A || (B<=abs(c[i]) && abs(c[i])<=2*A)){
ans++;
}else{
(c[i]<0 ? neg : pos).push_back(c[i]);
}
}
int n = pos.size(), m = neg.size();
REP(i,n)REP(j,m){
int x = pos[i] + neg[j];
if(abs(x)<=A || (B<=abs(x) && abs(x)<=2*A)){
g[i][n+j] = true;
}else{
g[i][n+j] = false;
}
}
int s = n+m;
int t = n+m+1;
REP(i,n)g[s][i] = true;
REP(j,m)g[n+j][t] = true;
while(true){
vector<int> bef(n+m+2, -1);
vector<bool> used(n+m+2, false);
stack<int> S;
S.push(s);
used[s] = true;
while(!S.empty()){
int p = S.top(); S.pop();
REP(to,n+m+2)if(!used[to]){
if(!g[p][to])continue;
used[to] = true;
bef[to] = p;
S.push(to);
}
}
if(!used[t])break;
int cur = t;
while(cur != s){
int b = bef[cur];
g[b][cur] = false;
g[cur][b] = true;
cur = b;
}
ans++;
}
printf("%d\n",ans);
return 0;
}
| 0 | CPP |
import math
def gcd(a,b):
return math.gcd(a,b)
for _ in range(int(input())):
n,k=map(int,input().split())
l=list(map(int,input().split()))
g=0
l.sort()
for i in range(1,n):
g=gcd(g,l[i]-l[0])
if (k-l[0])%g==0:
print('YES')
else:
print('NO') | 7 | PYTHON3 |
from math import factorial as f
a,b = [int(x) for x in input().split()]
print(f(min(a,b))) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> e[100010];
bool vis[100010];
double ans = 0;
void dfs(long long x, double p, long long l) {
vis[x] = true;
long long y = 0;
for (auto z : e[x])
if (!vis[z]) y++;
if (y == 0) ans += p * l;
for (auto z : e[x])
if (!vis[z]) dfs(z, p / y, l + 1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long i, j, k, t, x, y, p, q, n, m, f, tt, l, d, r;
cin >> n;
double dd = 1.0;
for (i = 1; i <= n - 1; i++) {
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
for (i = 1; i <= n; i++) vis[i] = false;
dfs(1, dd, 0);
cout << fixed << setprecision(10) << ans;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using ll = long long;
constexpr ll inf = 2147483647;
char buf[4194304];
inline ll Read() {
static char *p = buf;
while (*p < '0') {
++p;
}
ll ans = *p ^ 48;
while (*(++p) >= '0') {
ans = ans * 10 + (*p ^ 48);
}
return ans;
}
inline int Min(const int a, const int b) { return a < b ? a : b; }
struct Node {
int data;
Node *lc, *rc;
Node() : data(inf), lc(nullptr), rc(nullptr) {}
~Node() {
delete lc;
delete rc;
}
inline void Update(const int l, const int r, const int pos, const int val) {
data = val;
if (l < r) {
const int mid = (l + r) / 2;
if (pos <= mid) {
if (lc == nullptr) {
lc = new Node;
}
lc->Update(l, mid, pos, val);
} else {
if (rc == nullptr) {
rc = new Node;
}
rc->Update(mid + 1, r, pos, val);
}
}
return;
}
inline int Query(const int l, const int r, const int L, const int R) const {
if (l == L && R == r) {
return data;
}
const int mid = (l + r) / 2;
if (R <= mid) {
return lc == nullptr ? inf : lc->Query(l, mid, L, R);
}
if (mid < L) {
return rc == nullptr ? inf : rc->Query(mid + 1, r, L, R);
}
return Min((lc == nullptr ? inf : lc->Query(l, mid, L, mid)),
(rc == nullptr ? inf : rc->Query(mid + 1, r, mid + 1, R)));
}
};
ll a[100001], f[100001];
int main() {
fread(buf, 1, 4194304, stdin);
const ll n = Read(), g = Read(), r = Read(), turn = g + r, size = turn - 1;
for (ll i = 1; i <= n; ++i) {
a[i] = Read() + a[i - 1];
}
Node *tr = new Node;
for (ll i = n; i >= 1; --i) {
tr->Update(0, size, a[i] % turn, i);
const ll &val = a[i - 1], l = (g + val) % turn, r = (size + val) % turn;
const int upd =
(l <= r ? tr->Query(0, size, l, r)
: Min(tr->Query(0, size, l, size), tr->Query(0, size, 0, r)));
f[i] = (upd == inf ? a[n] - val
: ((a[upd] - val) / turn + 1) * turn +
(upd < n ? f[upd + 1] : 0));
}
const ll add = Read();
ll T = Read();
while (T--) {
const ll val = Read(), l = ((g - val) % turn + turn) % turn,
r = size - val % turn,
upd = (l <= r ? tr->Query(0, size, l, r)
: Min(tr->Query(0, size, l, size),
tr->Query(0, size, 0, r)));
printf("%lld\n", (upd == inf ? a[n] + val
: ((a[upd] + val) / turn + 1) * turn +
(upd < n ? f[upd + 1] : 0)) +
add);
}
delete tr;
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int const INF = (int)1e9 + 1e3;
long long const INFL = (long long)1e18 + 1e6;
mt19937 tw(9450189);
uniform_int_distribution<long long> ll_distr;
long long rnd(long long a, long long b) {
return ll_distr(tw) % (b - a + 1) + a;
}
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> graph(n);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
graph[a].push_back(b);
}
for (int i = 0; i < n; ++i) {
sort(graph[i].begin(), graph[i].end());
graph[i].erase(unique(graph[i].begin(), graph[i].end()), graph[i].end());
}
vector<bool> used(n);
vector<int> order;
function<void(int)> dfs = [&](int v) {
used[v] = true;
for (int to : graph[v]) {
if (!used[to]) {
dfs(to);
}
}
order.push_back(v);
};
for (int i = 0; i < n; ++i) {
if (!used[i]) {
dfs(i);
}
}
vector<vector<int>> rgraph(n);
for (int i = 0; i < n; ++i) {
for (int to : graph[i]) {
rgraph[to].push_back(i);
}
}
reverse(order.begin(), order.end());
vector<int> anscnt(n);
for (int q = 0; q < 2; ++q) {
vector<int> out(n);
vector<int> out2(n);
int cnt = 0, cnt2 = 0;
long long sum_bad = 0;
set<int> bads;
for (int v : order) {
cnt += 1;
++cnt2;
sum_bad += v;
for (int from : rgraph[v]) {
out[from]++;
if (out[from] == 1) {
--cnt;
sum_bad -= from;
for (int from2 : rgraph[from]) {
out2[from2]++;
if (out2[from2] == 1) {
--cnt2;
}
}
}
}
if (cnt == 1) {
anscnt[v] += 0;
} else if (cnt == 2) {
int cnt3 = cnt2;
for (int from : rgraph[v]) {
if (out2[from] == 0) {
--cnt3;
}
}
assert(cnt3 >= 2);
if (cnt3 == 2) {
anscnt[v] += 1;
} else {
anscnt[v] += 123;
}
} else {
anscnt[v] += 123;
}
}
reverse(order.begin(), order.end());
swap(graph, rgraph);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (anscnt[i] <= 1) {
++ans;
}
}
cout << ans << "\n";
}
int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_count = 1;
for (int test = 1; test <= test_count; ++test) {
solve();
}
}
| 12 | CPP |
if __name__ == "__main__":
n , b , p = map( int , input().split() )
res2 = n*p
res1 = 0
while n > 1:
res1 += (n//2)*(2*b+1)
n -= n//2
print( res1 , res2 ) | 7 | PYTHON3 |
N=int(input())
p=[list(map(str,input().split())) for _ in range(N)]
X=input()
flag=0
cnt=0
for i in range(N):
if flag==1:
cnt=cnt+int(p[i][1])
if p[i][0]==X:
flag=1
print(cnt) | 0 | PYTHON3 |
#include "bits/stdc++.h"
using namespace std;
map<char, set<int>>mp;
string test = "uids";
set<int>calc(const set<int>&l, const set<int>&r,char c) {
set<int>ans;
if (c == 'u') {
ans = l;
for (auto n : r)ans.emplace(n);
}
else if (c == 'i') {
for (auto&&n : l) {
if (r.find(n)!=r.end())ans.emplace(n);
}
}
else if (c == 'd') {
for (auto&&n : l) {
if (r.find(n) == r.end())ans.emplace(n);
}
}
else if (c == 's') {
for (auto &&n : l) {
if (r.find(n) == r.end())ans.emplace(n);
}
for (auto&&n : r) {
if (l.find(n) == l.end())ans.emplace(n);
}
}
return ans;
}
string st; int a;
set<int>expr();
set<int>exp() {
if (st[a] == '(') {
a++;
auto n= expr();
return n;
}else if (st[a] == 'c') {
a++;
auto n = exp();
n = calc(mp['U'], n, 'd');
return n;
}
else {
char c(st[a]);
assert(mp.find(c) != mp.end());
set<int>n(mp[c]);
a++;
return n;
}
}
set<int>expr() {
set<int>l(exp());
while (1) {
if (a == st.size() || st[a] == ')') {
a++;
return l;
}
else {
char op = st[a];
assert(test.find(op) != string::npos);
a++;
set<int>r(exp());
l = calc(l, r, op);
}
}
}
int main() {
char c; int n;
while (cin >> c >> n) {
if (c == 'R') {
set<int>v;
for (auto m : mp) {
for (auto n : m.second) {
v.insert(n);
}
}
mp['U'] = v;
cin >> st;
auto ans = expr();
for (auto aa : ans) {
cout << aa;
if (*prev(ans.end()) == aa) {
cout << endl;
}
else cout << " ";
}
if (ans.empty()) {
cout << "NULL" << endl;
}
mp.clear();
a = 0;
}
else {
for (int i = 0; i < n; ++i) {
int a; cin >> a;
mp[c].emplace(a);
}
}
}
return 0;
} | 0 | CPP |
from itertools import combinations, permutations
from sys import path_hooks, stdin, stdout
from collections import Counter,deque
import math
from copy import deepcopy
import random
import heapq
import sys
sys.setrecursionlimit(10**5)
def mapinput():
return map(int, stdin.readline().split())
def strinput():
return stdin.readline().strip()
def listinput():
return list(map(int,stdin.readline().split()))
def intinput():
return int(stdin.readline().strip())
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
def SieveOfEratosthenes(n):
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
ans = []
for p in range(n + 1):
if prime[p]:
ans.append(p)
return ans
coprime1 = {6: 100003, 5: 10007, 4: 1009, 3: 101, 2: 11, 1: 2, 7: 1000003, 8: 10000019, 9: 100000007}
coprime2 = {8:10000169,9:100000049,7:1000033,6:100019,5:10009,4:1013,3:103,2:13,1:3}
def writ(ss):
stdout.write(str(ss) + "\n")
mod = 998244353
def sumn(n):
return (n*(n+1))//2
def perm(lis):
if len(lis) < 2:
return lis
#print(lis)
anss = []
for i in range(len(lis)):
ele = lis[i]
liss = lis[:i] + lis[i+1:]
for j in perm(liss):
anss.append( ele+j )
return anss
for test in range(intinput()):
def solve():
#n = intinput()
a , b = mapinput()
if a + b & 1 == 0:
n = (a + b) >> 1
aa = abs( (n & 1) - (a & 1) )
ans = deque([i for i in range( aa , a + b + 1 , 2 )])
diff = abs(a - b)>> 2
for i in range(diff):
ans.popleft()
ans.pop()
else:
ans = deque([ i for i in range(a+b+1)])
diff = abs( a - b) >> 1
for i in range(diff):
ans.popleft()
ans.pop()
print(len(ans))
print(*ans)
solve()
| 8 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
std::array<int, 4> c[2];
int r(0);
double d;
for (int i(0); i < 2; ++i)
std::scanf("%d%d%d%d", &c[i][0], &c[i][1], &c[i][2], &c[i][3]);
d = std::sqrt((c[0][0] - c[1][0]) * (c[0][0] - c[1][0]) +
(c[0][1] - c[1][1]) * (c[0][1] - c[1][1]));
if (c[0][3] < c[1][3]) std::swap(c[0], c[1]);
if (d <= c[0][3]) {
if (d + c[1][3] <= c[0][2]) {
r = 4;
} else if (d + c[1][2] <= c[0][2]) {
r = 1;
r += d + c[1][3] <= c[0][3];
} else {
if (d + c[1][3] <= c[0][3]) {
if (c[0][2] + c[1][3] <= d)
r = 2;
else if (d < c[1][2] && c[0][2] <= c[1][2] - d)
r = 2;
else
r = 1;
} else {
r += c[0][2] + c[1][3] <= d;
r += d < c[1][2] && c[0][2] <= c[1][2] - d;
r += c[1][2] + c[0][3] <= d;
r += d < c[0][2] && c[1][2] <= c[0][2] - d;
}
}
} else {
if (c[0][3] + c[1][3] <= d) {
r = 4;
} else {
r += c[0][2] + c[1][3] <= d;
r += c[0][3] + c[1][2] <= d;
}
}
std::printf("%d\n", r);
return 0;
}
| 8 | CPP |
D=input("")
D=D.split()
M=int(D[0])
N=int(D[1])
if(M%2)==0:
X=M//2
Res=X*N
else:
XP=M//2
Y=N
X=N//2
Res=(XP*Y)+X
print(Res)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
int n, a[200004], sa, s0, cnt = 0;
vector<int> bad;
bool b[200004];
void delet() {
int nn = bad.size();
for (int i = (0); i <= (nn - 1); ++i) {
if (cnt == n - 1) break;
if (nn - i == 1) {
cout << "2 " << bad[i] << '\n';
b[bad[i]] = 0;
} else {
b[bad[i]] = 0;
cout << "1 " << bad[i] << ' ' << bad[i + 1] << '\n';
}
cnt++;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = (1); i <= (n); ++i) {
cin >> a[i];
if (a[i] < 0) {
sa++;
}
if (a[i] == 0) {
s0++;
bad.push_back(i);
}
}
int mnam = -1e9 - 1, mnid;
if (sa & 1)
for (int i = (1); i <= (n); ++i)
if (a[i] < 0 && a[i] > mnam) {
mnam = a[i];
mnid = i;
}
if (sa & 1) bad.push_back(mnid);
fill(b, b + 2 + n, 1);
delet();
int tem = 1;
if (!b[1]) tem = 0;
for (int i = (2); i <= (n); ++i) {
if (cnt == n - 1) break;
if (b[i]) {
if ((tem == 0 && i == 2) || (!b[i - 1])) {
continue;
}
cout << "1 " << i - 1 << ' ' << i << '\n';
cnt++;
} else {
int t1 = i - 1, t2 = i + 1;
while (!b[t1]) t1++;
while (!b[t2]) t2++;
if (t1 == t2) {
i = t1;
continue;
}
if (t2 > n) continue;
cout << "1 " << t1 << ' ' << t2 << '\n';
cnt++;
}
}
}
| 9 | CPP |
n = int(input())
if n == 0:
print(1)
quit()
n%=4
if n == 0:
print(6)
elif n == 3:
print(2)
elif n == 2:
print(4)
elif n == 1:
print(8) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[510][510];
int main() {
a[1][1] = 8;
a[1][2] = 7;
a[1][3] = 6;
a[1][4] = -1;
a[2][1] = 5;
a[2][2] = 1;
a[2][3] = 2;
a[2][4] = 0;
a[3][1] = 4;
a[3][2] = 9;
a[3][3] = 3;
a[3][4] = -2;
a[4][1] = -6;
a[4][2] = -5;
a[4][3] = -4;
a[4][4] = -3;
int now = -7;
for (int i = 5; i <= 500; i++) {
if (i % 2) {
for (int j = 1; j <= i; j++) a[i][j] = now--;
for (int j = i - 1; j >= 1; j--) a[j][i] = now--;
} else {
for (int j = 1; j <= i; j++) a[j][i] = now--;
for (int j = i - 1; j >= 1; j--) a[i][j] = now--;
}
}
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
if (n == 1 || n == 2)
cout << -1 << endl;
else {
int val = n * n - a[3][2];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << a[i][j] + val << " ";
}
cout << endl;
}
}
return 0;
}
| 11 | CPP |
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string s;
int num, a, b, i;
cin >> num >> a >> b >> s;
a += b;
for (i = 0; i < num; i++) {
if (s[i] == 'a') {
if (a > 0) {
a--;
printf("Yes\n");
continue;
}
}
else if (s[i] == 'b') {
if (a > 0 && b > 0) {
a--;
b--;
printf("Yes\n");
continue;
}
}
printf("No\n");
}
return 0;
} | 0 | CPP |
A,B,C,D = map(int, input().split())
CC=B//C-(A-1)//C
DD=B//D-(A-1)//D
import fractions
F=(C*D)//fractions.gcd(C,D)
FF=B//F-(A-1)//F
print(B-A+1-CC-DD+FF)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
set<long long int> s;
multiset<long long int> m;
void add(long long int x) {
s.insert(x);
auto it = s.find(x);
if (it != s.begin()) {
m.insert((*it) - (*prev(it)));
}
if (next(it) != s.end()) {
m.insert((*next(it)) - (*it));
}
if (next(it) != s.end() && it != s.begin()) {
m.erase(m.find((*next(it)) - (*prev(it))));
}
}
void remove(long long int x) {
auto it = s.find(x);
if (it != s.begin()) {
m.erase(m.find((*it) - (*prev(it))));
}
if (next(it) != s.end()) {
m.erase(m.find(*next(it) - (*it)));
}
if (next(it) != s.end() && it != s.begin()) {
m.insert((*next(it)) - (*prev(it)));
}
s.erase(x);
}
void query() {
if (s.empty()) {
cout << 0 << '\n';
return;
}
if (m.empty()) {
cout << 0 << '\n';
return;
}
long long int ans = (*s.rbegin()) - (*s.begin()) - (*m.rbegin());
cout << ans << '\n';
}
void mainSolve() {
long long int n;
cin >> n;
long long int q;
cin >> q;
for (int i = 0; i < n; i++) {
long long int a;
cin >> a;
add(a);
}
query();
while (q--) {
long long int a;
cin >> a;
long long int b;
cin >> b;
if (a == 0)
remove(b);
else
add(b);
query();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
while (t--) {
mainSolve();
}
return 0;
}
| 10 | CPP |
testCases = int(input())
for i in range(testCases):
number = int(input())
p = [int(i) for i in input().split()]
vertices = [None] * number
for j in range(number):
if vertices[j] is None:
k = j
vertices[j] = [j]
while j != p[k] - 1:
vertices[j].append(p[k] - 1)
vertices[p[k] - 1] = vertices[j]
k = p[k] - 1
print(*[len(v) for v in vertices]) | 8 | PYTHON3 |
import random
import math
import sys
LI = lambda: list(map(int,input().split()))
MI = lambda: map(int,input().split())
yes = lambda: print("Yes")
no = lambda: print("No")
I = lambda: list(input())
J = lambda x: "".join(x)
II = lambda: int(input())
SI = lambda: input()
#---khan17---template
t = II()
for q in range(t):
n = II()
a = [0]*n
temp = 1
for i in range(n):
start = 0
end = 0
maxS = 0
maxE = 0
l = []
for j in range(n):
if a[j]!=0:
if end-start>maxE-maxS:
maxS = start
maxE = end
l = []
elif end-start==maxE-maxS:
l.append((start+end-1)//2)
start = end+1
end = end+1
else:
end+=1
if end-start>maxE-maxS:
maxS = start
maxE = end
l = []
elif end-start==maxE-maxS:
l.append((start+end-1)//2)
if maxS == maxE:
break
a[(maxS+maxE-1)//2] = temp
temp+=1
for j in l:
a[j] = temp
temp+=1
for i in range(n):
if a[i] == 0:
a[i] = temp
temp+=1
print(*a)
| 10 | PYTHON3 |
'''input
2
3 5
'''
n = int(input())
s = sum(map(int, input().split()))
t = 0
for x in range(1, 6):
if ((x + s) % (n + 1)) - 1 != 0:
t += 1
print(t) | 7 | PYTHON3 |
n = int(input())
counts = [0] * 200001
incorrect = False
for i in input().split():
counts[int(i)] += 1
if counts[int(i)] > 2:
incorrect = True
break
if incorrect:
print('NO')
else:
print('YES')
up = []
down = []
for i, c in enumerate(counts):
if c == 2:
up.append(i)
if c:
down.append(i)
print(len(up))
for i in up:
print(i, end=' ')
print()
print(len(down))
for i in reversed(down):
print(i, end=' ') | 9 | PYTHON3 |
#include <iostream>
#include <string>
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
using namespace std;
typedef long long ll;
const int TLE = 1000000000;
ll pow(ll x, int n){
ll res = 1;
rep(i, n){
res *= x;
if(res > TLE) return -1;
}
return res;
}
ll n, T;
string s;
int solve(){
ll res = 0;
rep(i, s.size()){
if('0' <= s[i] && s[i] <= '9'){
if(pow(n, s[i]-'0') == -1) return -1;
res += pow(n, s[i]-'0');
}
if(res > TLE) return -1;
}
res *= T;
return res>TLE?-1:res;
}
int main(){
cin >> n >> T >> s;
int ans = solve();
if(~ans) cout << ans << endl;
else cout << "TLE" << endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int n, k, m, a, rk[110];
struct NODE {
int idx;
int cnt;
int last;
} node[110];
bool cmp(NODE x, NODE y) {
if (x.cnt == y.cnt) {
return x.last < y.last;
}
return x.cnt > y.cnt;
}
bool yes(int x) {
if (x > k) return false;
if (!node[x].cnt) return false;
if (n == k) return true;
int sum = 0;
for (int i = x + 1; i <= k + 1; i++) {
sum += node[x].cnt + 1 - node[i].cnt;
}
return sum > m - a;
}
bool no(int x) {
int tmp = node[x].cnt + m - a;
if (tmp == 0) return true;
if (x <= k) return false;
for (int i = 1; i <= k; i++) {
if (tmp > node[i].cnt) {
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k >> m >> a;
for (int i = 1; i <= n; i++) {
node[i].idx = i;
}
for (int i = 1, x; i <= a; i++) {
cin >> x;
node[x].cnt++;
node[x].last = i;
}
sort(node + 1, node + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
rk[node[i].idx] = i;
}
for (int i = 1; i <= n; i++) {
if (yes(rk[i]))
cout << 1;
else if (no(rk[i]))
cout << 3;
else
cout << 2;
if (i != n)
cout << " ";
else
cout << endl;
}
return 0;
}
| 12 | CPP |
s = input()
l = list(s)
if l[0] >= 'a' and l[0] <= 'z' :
r = ord(s[0]) - ord('a') + ord('A')
l[0] = chr(r)
s = ''.join(l)
print(s)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const double eps(1e-8);
int n;
double ans;
vector<int> v[110000];
void dfs(int i, int f, int depth) {
ans += 1.0 / depth;
for (int j = 0; j < (v[i].size()); ++j) {
if (v[i][j] == f) continue;
dfs(v[i][j], i, depth + 1);
}
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n - 1); ++i) {
int a, b;
scanf("%d %d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
ans = 0;
dfs(1, 0, 1);
printf("%.10lf\n", ans);
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10, INF = 1e10;
int n, m;
vector<int> adj[MAXN];
pair<int, int> dfs(int u, int h) {
if (adj[u].empty()) return m++, make_pair(1, 1);
pair<int, int> ans;
if (h % 2)
ans = make_pair(0, INF);
else
ans = make_pair(INF, 0);
for (int i = 0; i < (int)adj[u].size(); i++) {
pair<int, int> p = dfs(adj[u][i], h + 1);
if (h % 2)
ans.first += p.first, ans.second = min(ans.second, p.second);
else
ans.first = min(ans.first, p.first), ans.second += p.second;
}
return ans;
}
int main() {
cin >> n;
int u, v;
for (int i = 0; i < n - 1; i++) cin >> u >> v, u--, v--, adj[u].push_back(v);
pair<int, int> ans = dfs(0, 0);
return cout << m - ans.first + 1 << " " << ans.second << endl, 0;
}
| 11 | CPP |
n=int(input())
s=input()
r=s.count(".")
l=0
c=0
ans=r
for i in s:
if i=="#":
l+=1
else:
r-=1
if ans>l+r:
ans=l+r
print(ans) | 0 | PYTHON3 |
a=input()
b=input()
i=0
ans=''
while i<len(a):
if a[i]==b[i]:
ans=ans+'0'
else :
ans=ans+'1'
i+=1
print(ans)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct node {
int t, l, r;
node(int t = 0, int l = 0, int r = 0) : t(t), l(l), r(r) {}
friend bool operator<(node a, node b) {
if (a.r == b.r) {
if (a.l == b.l) return a.t == 1;
return a.l < b.l;
}
return a.r < b.r;
}
} p[maxn];
vector<node> v1, v0;
int ans[maxn];
int n, m;
int f[maxn];
void init() {
for (int i = 1; i <= n; i++) f[i] = i;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
cin >> n >> m;
init();
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &p[i].t, &p[i].l, &p[i].r);
if (p[i].t == 1)
v1.push_back(p[i]);
else
v0.push_back(p[i]);
}
sort(v1.begin(), v1.end());
for (int i = 0; i < v1.size(); i++) {
for (int j = v1[i].l + 1; j <= v1[i].r; ++j) f[j] = j - 1;
}
bool ok = 1;
sort(v0.begin(), v0.end());
for (auto v : v0) {
bool tmp = 0;
for (int i = v.l + 1; i <= v.r; i++) {
if (find(i) != find(i - 1)) {
tmp = 1;
break;
}
}
if (!tmp) {
ok = 0;
break;
}
}
if (!ok) {
cout << "NO";
return 0;
}
int lim = 1e6;
cout << "YES\n";
ans[1] = lim;
for (int i = 2; i <= n; i++) {
if (find(i) == find(i - 1)) {
ans[i] = ans[i - 1] + 1;
} else
ans[i] = ans[i - 1] - 1;
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;
template <typename... Args>
void readln(Args&... args) {
((cin >> args), ...);
}
template <typename... Args>
void writeln(Args... args) {
((cout << args << " "), ...);
cout << '\n';
}
void solve() {
int n, m;
readln(n, m);
;
vector<vint> a(n + 1, vint(m + 1));
for (int i = 1; i <= n; i++) {
string s;
readln(s);
;
for (int j = 1; j <= m; j++) a[i][j] = (s[j - 1] == '1');
}
auto cs = a;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) cs[j][i] += cs[j - 1][i];
}
const auto F = [&](int s, int e, int j) {
return (e - s - 1) - (cs[e - 1][j] - cs[s][j]);
};
const auto G = [&](int s, int e, int j) {
return !a[s][j] + !a[e][j] + (cs[e - 1][j] - cs[s][j]);
};
int ans = n * m;
vint b(m + 1), s(m + 1);
for (int i = 1; i <= n; i++) {
for (int j = i + 4; j <= n; j++) {
for (int k = 1; k <= m; k++) s[k] = s[k - 1] + G(i, j, k);
for (int k = m; k >= 1; k--)
b[k] = min(k == m ? int(1e9) : b[k + 1], s[k - 1] + F(i, j, k));
for (int k = 1; k + 3 <= m; k++)
ans = min(ans, b[k + 3] + F(i, j, k) - s[k]);
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
readln(t);
;
while (t--) solve();
return 0;
}
| 7 | CPP |
Size = list(map(int, input().split()))
X = []
for i in range(Size[0]):
X.append(input())
for i in range(len(X)):
for j in range(len(X[i])):
if X[i][j] == 'W':
# Left
if j > 0:
if X[i][j - 1] == 'S':
print('NO')
exit()
# Right
if j < Size[1] - 1:
if X[i][j + 1] == 'S':
print('NO')
exit()
"UP"
if i > 0:
if X[i - 1][j] == 'S':
print('NO')
exit()
# Down
if i < Size[0] - 1:
if X[i + 1][j] == 'S':
print('NO')
exit()
print("YES")
for i in X:
print(i.replace('.', 'D'))
| 7 | PYTHON3 |
#http://codeforces.com/contest/405/problem/A
def GravitySort():
n = int(input())
columns = [ *map( int, input().strip().split() ) ]
return sorted(columns)
if __name__ == '__main__':
a = GravitySort()
print(*a)
| 7 | PYTHON3 |
n=input()
s=input()
d=[]
j=0
for i in s:
if i not in d:
d.append(i)
else:
j+=1
if len(d)+j >26:
print(-1)
else :
print(j)
| 8 | PYTHON3 |
from collections import Counter
n, m = map(int, input().split())
a = list(Counter(map(int, input().split())).items())
b = list(Counter(map(int, input().split())).items())
assert(len(a)==len(b))
k = len(a)
a.sort()
b.sort()
ans = m
for i in range(k):
tmp = (b[i][0]-a[0][0])%m
for j in range(k):
if b[(j+i)%k][1]!=a[j][1] or (b[(j+i)%k][0]-a[j][0])%m!=tmp:
break
else:
ans = min((b[i][0]-a[0][0])%m, ans)
print(ans) | 8 | PYTHON3 |
from sys import stdin
n, q = map(int, input().split())
cnt = [0]*n
root = [[] for i in range(n)]
for i in range(n-1):
a, b = map(int, stdin.readline().split())
root[a-1].append(b-1)
for i in range(q):
p, x = map(int, stdin.readline().split())
cnt[p-1] += x
tmparr = [0]
while tmparr:
tmpi = tmparr.pop()
for i in root[tmpi]:
cnt[i] += cnt[tmpi]
tmparr += root[tmpi]
print(*cnt) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
} A[40005], B[40005], CA[40005], CB[40005], S[40005];
int tot;
node operator+(node a, node b) { return node{a.x + b.x, a.y + b.y}; }
node operator-(node a, node b) { return node{a.x - b.x, a.y - b.y}; }
int operator*(node a, node b) { return a.x * b.x + a.y * b.y; }
int operator/(node a, node b) { return a.x * b.y - a.y * b.x; }
bool operator<(node a, node b) {
if (a.x == b.x) return a.y > b.y;
return a.x < b.x;
}
void convex_hull(node p[], node c[], int n, int &tp) {
sort(p + 1, p + n + 1);
tp = 0;
for (int i = 1; i <= n; i++) {
while (tp > 1 && (c[tp] - p[i]) / (c[tp - 1] - p[i]) >= 0) tp--;
c[++tp] = p[i];
}
int k = tp--;
for (int i = n; i >= 1; i--) {
while (tp > k && (c[tp] - p[i]) / (c[tp - 1] - p[i]) >= 0) tp--;
c[++tp] = p[i];
}
tp--;
}
void getedge(node a[], int l, int r) {
S[++tot] = node{l, r};
if (l + 1 == r) return;
int i, j;
double Max = -1e100;
int mid = 0;
for (int k = l + 1; k < r; ++k) {
double d = (double)((a[k] - a[l]) * (a[k] - a[r])) /
((a[l] - a[k]) / (a[l] - a[r]));
if (d > Max) mid = k, Max = d;
}
getedge(a, l, mid);
getedge(a, mid, r);
}
bool solve(node a[], node b[], int na, int nb) {
tot = 0;
getedge(a, 1, na);
for (int i = 1, j; i <= tot; i++) {
node pa = a[S[i].x], pb = a[S[i].y];
double lp = -1e100, rp = 1e100;
for (j = 1; j <= na; j++) {
int ta = (a[j] - pa) * (a[j] - pb);
int tb = (pa - a[j]) / (pa - pb);
if (tb == 0) continue;
double r = (double)ta / tb;
if (tb < 0)
rp = min(rp, r);
else if (tb > 0)
lp = max(lp, r);
}
for (j = 1; j <= nb; j++) {
int ta = (b[j] - pa) * (b[j] - pb);
int tb = (pa - b[j]) / (pa - pb);
if (tb == 0) {
if (ta <= 0)
break;
else
continue;
}
double r = (double)ta / tb;
if (tb > 0)
rp = min(rp, r);
else if (tb < 0)
lp = max(lp, r);
if (lp >= rp) break;
}
if (j > nb && lp < rp) return 1;
}
return 0;
}
int main() {
int n, m, cn, cm;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d%d", &A[i].x, &A[i].y);
for (int i = 1; i <= m; i++) scanf("%d%d", &B[i].x, &B[i].y);
if (n == 1 || m == 1) return puts("YES"), 0;
convex_hull(A, CA, n, cn);
convex_hull(B, CB, m, cm);
if (solve(CA, B, cn, m) || solve(CB, A, cm, n))
puts("YES");
else
puts("NO");
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int OO = 1e9;
const double EPS = 1e-9;
int n, k, x;
set<int> distinctX;
set<pair<int, int> > scenarios;
map<int, int> fsOccur, lstOccur;
int main() {
ios::sync_with_stdio(false);
cout.precision(10);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
if (i > 1) scenarios.insert({i, i - 1});
if (i < n) scenarios.insert({i, i + 1});
}
for (int i = 0; i < k; ++i) {
cin >> x;
distinctX.insert(x);
if (fsOccur.find(x) == fsOccur.end()) fsOccur[x] = i;
lstOccur[x] = i;
}
int ans = n - int(distinctX.size());
ans += 2 * n - 2;
for (auto& scenario : scenarios)
if (distinctX.find(scenario.first) != distinctX.end() &&
distinctX.find(scenario.second) != distinctX.end() &&
fsOccur[scenario.first] < lstOccur[scenario.second])
--ans;
cout << ans << '\n';
return 0;
}
| 9 | CPP |
a = input()
h = a.find('h')
e = a.find('e',h)
l = a.find('l',e)
l2 = a.find('l',l+1)
o = a.find('o',l2)
if h<e and e<l and l<l2 and l2<o:
print('YES')
else:
print('NO') | 7 | PYTHON3 |
s=input()
ss=input()
if s[::-1]==ss:
print('YES')
else:
print('NO') | 7 | PYTHON3 |
n=int(input())
d={}
for name in range(n):
s=input()
if not s in d:
d[s]=0
print('OK')
else:
d[s]+=1
print(s+str(d[s]))
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, xx;
map<int, int> ori, ad;
int a[100005];
inline int rd() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return x;
}
int main() {
n = rd();
xx = rd();
int ans = 0x3f3f3f3f;
for (int i = 1; i <= n; ++i) {
a[i] = rd();
ori[a[i]]++;
ad[(a[i] & xx)]++;
}
for (int i = 1; i <= n; ++i) {
if (ori[a[i]] > 1) {
ans = min(ans, 0);
}
ori[a[i]]--;
if (ori[(a[i] & xx)]) {
ans = min(ans, 1);
}
ori[a[i]]++;
if (ad[(a[i] & xx)] > 1) {
ans = min(ans, 2);
}
}
if (ans < 0x3f3f3f3f)
printf("%d\n", ans);
else
puts("-1");
return 0;
}
| 8 | CPP |
for _ in range(int(input())):
n=int(input())
st=input()
operation=0
flip=0
for i in range(n):
if st[i]=="(":
flip+=1
else:
flip-=1
if flip<0:
operation+=1
flip=0
print(operation) | 9 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> a(n, std::vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
char ch;
std::cin >> ch;
a[i][j] = ch - '0';
}
}
std::vector<std::tuple<int, int, int, int, int, int>> ans;
auto change = [&](int x1, int y1, int x2, int y2, int x3, int y3) {
ans.emplace_back(x1, y1, x2, y2, x3, y3);
a[x1][y1] ^= 1;
a[x2][y2] ^= 1;
a[x3][y3] ^= 1;
};
for (int i = n - 1; i >= 2; --i) {
for (int j = 0; j < m; ++j) {
if (a[i][j] == 1) {
if (j > 0) {
change(i, j, i - 1, j, i - 1, j - 1);
} else {
change(i, j, i - 1, j, i - 1, j + 1);
}
}
}
}
for (int i = m - 1; i >= 2; --i) {
if (a[0][i] == 1) change(0, i, 0, i - 1, 1, i - 1);
if (a[1][i] == 1) change(1, i, 1, i - 1, 0, i - 1);
}
if (a[1][1] == 1) change(0, 1, 1, 0, 1, 1);
if (a[0][0] + a[0][1] + a[1][0] == 3) {
change(0, 0, 0, 1, 1, 0);
} else if (a[0][0] + a[0][1] == 2) {
change(0, 0, 1, 0, 1, 1);
change(0, 1, 1, 0, 1, 1);
} else if (a[0][0] + a[1][0] == 2) {
change(0, 0, 0, 1, 1, 1);
change(1, 0, 0, 1, 1, 1);
} else if (a[1][0] + a[0][1] == 2) {
change(1, 0, 0, 0, 1, 1);
change(0, 1, 0, 0, 1, 1);
} else if (a[0][0] == 1) {
change(0, 0, 0, 1, 1, 0);
change(0, 0, 0, 1, 1, 1);
change(0, 0, 1, 1, 1, 0);
} else if (a[0][1] == 1) {
change(0, 1, 0, 0, 1, 1);
change(0, 1, 0, 0, 1, 0);
change(0, 1, 1, 0, 1, 1);
} else if (a[1][0] == 1) {
change(1, 0, 1, 1, 0, 0);
change(1, 0, 1, 1, 0, 1);
change(1, 0, 0, 1, 0, 0);
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) assert(a[i][j] == 0);
std::cout << ans.size() << "\n";
for (auto [a, b, c, d, e, f] : ans)
std::cout << a + 1 << " " << b + 1 << " " << c + 1 << " " << d + 1 << " "
<< e + 1 << " " << f + 1 << "\n";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N; cin >> N;
vector<int> MY(3*N);
for(int i=0;i<3*N;i++){
cin >> MY[i];
}
sort(MY.rbegin(),MY.rend());
long long ans=0;
for(int i=1;i<2*N;i+=2){
ans+=MY[i];
}
cout << ans << endl;
}
| 0 | CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.