solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 9 22:40:52 2020
@author: xiaopp
"""
tcases = int(input())
for case in range(tcases):
num_s = int(input().strip())
a = [int(x) for x in input().split(' ')]
a = sorted(a)
print(abs(a[num_s - 1] - a[num_s])) | 8 | PYTHON3 |
import collections
def solution(n, a):
a.sort()
x1 = a[n-1]*a[n-2]*a[n-3]*a[n-4]*a[n-5]
x2 = a[n-1]*a[n-2]*a[n-3]*a[0]*a[1]
x3 = a[n-1]*a[0]*a[1]*a[2]*a[3]
return max(x1, x2, x3)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split(' ')))
print(solution(n, a))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const int mod = 998244353;
const int N = 2e5 + 5;
inline long long read() {
long long x = 0;
bool f = true;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = false;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? x : -x;
}
long long gcd(long long m, long long n) { return n == 0 ? m : gcd(n, m % n); }
long long lcm(long long m, long long n) { return m * n / gcd(m, n); }
int main() {
int T;
cin >> T;
while (T--) {
queue<char> q;
string s;
cin >> s;
int res = 0;
map<pair<int, int>, set<pair<int, int>>> mp;
pair<int, int> cur = {0, 0};
for (int i = 0; i < s.size(); i++) {
pair<int, int> t = cur;
if (s[i] == 'N')
cur.second++;
else if (s[i] == 'S')
cur.second--;
else if (s[i] == 'E')
cur.first++;
else if (s[i] == 'W')
cur.first--;
if (mp[t].count(cur)) {
res++;
} else {
mp[t].insert(cur);
mp[cur].insert(t);
res += 5;
}
}
cout << res << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long po = 90000011;
long long Hasz(vector<long long>& H, vector<long long>& P, int p, int k) {
return (H[p] - P[k - p + 1] * H[k + 1]);
}
int main() {
ios_base::sync_with_stdio(0);
string nap, p, k;
cin >> nap >> p >> k;
int dl = nap.size();
vector<long long> P(2010);
P[0] = 1;
for (int i = 1; i < 2010; ++i) P[i] = P[i - 1] * po;
vector<long long> H(dl + 1, 0);
for (int i = dl - 1; i >= 0; --i) H[i] = H[i + 1] * po + nap[i];
vector<long long> hp(p.size() + 1, 0);
for (int i = p.size() - 1; i >= 0; --i) hp[i] = hp[i + 1] * po + p[i];
vector<long long> hk(k.size() + 1, 0);
for (int i = k.size() - 1; i >= 0; --i) hk[i] = hk[i + 1] * po + k[i];
long long Hp = Hasz(hp, P, 0, p.size() - 1);
long long Hk = Hasz(hk, P, 0, k.size() - 1);
if (dl < p.size() || dl < k.size()) {
cout << "0";
return 0;
}
vector<long long> wystp;
for (int i = 0; i <= dl - p.size(); ++i) {
long long t = Hasz(H, P, i, i + p.size() - 1);
if (t == Hp) wystp.push_back(i);
}
vector<long long> wystk;
for (int i = 0; i <= dl - k.size(); ++i) {
long long t = Hasz(H, P, i, i + k.size() - 1);
if (t == Hk) wystk.push_back(i);
}
int pocz = 0, kon = 0, ost = 0;
vector<long long> zb;
while (pocz < wystp.size()) {
kon = ost;
while (kon < wystk.size() && wystk[kon] < wystp[pocz]) ++kon;
ost = kon;
while (kon < wystk.size()) {
if (wystp[pocz] + p.size() - 1 <= wystk[kon] + k.size() - 1) {
zb.push_back(Hasz(H, P, wystp[pocz], wystk[kon] + k.size() - 1));
}
++kon;
}
++pocz;
}
if (zb.size() == 0) {
cout << "0";
return 0;
}
sort(zb.begin(), zb.end());
zb.push_back(zb[zb.size() - 1] - 1);
long long il = 0;
for (int i = 0; i < zb.size() - 1; ++i)
if (zb[i] != zb[i + 1]) ++il;
cout << il;
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int t1 = 1; t1 <= t; t1++) {
long long n, a, b;
cin >> n >> a >> b;
if (a + b == n)
cout << max(a, b) + 1 << endl;
else {
if (a == n && b == n)
cout << 1 << endl;
else
cout << max(n - a + 1, n - b + 1) << endl;
}
}
}
| 7 | CPP |
#include <bits/stdc++.h>
int F() {
register int a = 0, c;
while (c = getchar(), !isdigit(c))
;
for (; isdigit(c); c = getchar()) a = a * 10 + c - '0';
return a;
}
struct Edge {
int x, y;
Edge *rnext;
bool operator<(const Edge &o) const { return x < o.x || x == o.x && y < o.y; }
};
struct queue {
int arr[3003], head, tail;
bool empty() const { return head == tail; }
int front() const { return arr[head]; }
void clear() { head = tail = 0; }
void push(register int x) { arr[tail++] = x; }
void pop() { ++head; }
};
char vis[3003];
int n, outdeg[3003], qs[400000], qk[400000], qhead[3003], qnext[400000],
ans[400000], adv[13][3003], *nxt = adv[0];
struct Edge e[5000], *beg[3003], **end = beg + 1, *rhead[3003];
struct queue q;
void run(register int dst) {
register int i, j, k;
register struct Edge *it;
memset(vis + 1, 0, n);
q.clear();
vis[dst] = 1;
q.push(dst);
while (!q.empty()) {
for (it = rhead[q.front()]; it; it = it->rnext) {
if (!vis[it->x]) {
vis[it->x] = 1;
q.push(it->x);
}
}
q.pop();
}
nxt[dst] = -1;
for (i = 1; i <= n; ++i) {
if (vis[i] && i != dst) {
for (it = beg[i]; !vis[it->y]; ++it)
;
nxt[i] = it->y;
}
}
for (i = 1; i <= 12; ++i) {
for (j = 1; j <= n; ++j)
adv[i][j] = ~adv[i - 1][j] ? adv[i - 1][adv[i - 1][j]] : -1;
}
for (i = 1; i <= n; ++i)
if (~adv[12][i]) vis[i] = 2;
for (i = qhead[dst]; ~i; i = qnext[i]) {
if (vis[qs[i]] != 1) {
ans[i] = -1;
continue;
}
for (j = qs[i], k = qk[i] - 1; k; k &= k - 1) {
j = adv[__builtin_ctz(k)][j];
if (j == -1) break;
}
ans[i] = j;
}
}
int main() {
register int m, q, i, j;
register struct Edge *it;
n = F(), m = F(), q = F();
memset(qhead + 1, -1, n * sizeof(int));
for (i = 0; i < m; ++i) ++outdeg[e[i].x = F()], e[i].y = F();
std::sort(e, e + m);
for (i = 1, it = e; i <= n; ++i) {
beg[i] = it;
for (j = 0; j < outdeg[i]; ++j, ++it) {
it->rnext = rhead[it->y];
rhead[it->y] = it;
}
}
end[n] = it;
for (i = 0; i < q; ++i) {
qs[i] = F(), j = F(), qk[i] = F();
qnext[i] = qhead[j], qhead[j] = i;
}
for (i = 1; i <= n; ++i) run(i);
for (i = 0; i < q; ++i) printf("%d\n", ans[i]);
return 0;
}
| 12 | CPP |
for _ in range(int(input())):
n = int(input())
s = input()
cnt = 1
while cnt:
cnt = s.count('()')
s = s.replace('()', '')
print(len(s) // 2)
| 9 | PYTHON3 |
n = [int(x) for x in input().split(' ')]
s = max(n)
n.remove(s)
a = int((n[0] + n[1] - n[2]) / 2)
b = int((n[0] - n[1] + n[2]) / 2)
c = int((n[0] - n[1] - n[2]) / -2)
print(a, b, c)
| 7 | PYTHON3 |
#!/usr/bin/python3
def solve(n, t):
x0 = 1;
for i in range(1, n):
x0 *= 10;
for x in range(x0, 10 * x0):
if (x % t == 0):
return x
return -1
nums = list(map(int, input().split()))
print(solve(nums[0], nums[1]))
| 7 | PYTHON3 |
a = [int(x) for x in input().split()]
a.sort()
print(a[2] - a[1] + a[1] - a[0]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using i64 = long long;
template <class T>
T pow(T x, T n, T mod) {
T ret = 1;
while (n > 0) {
if ((n & 1) == 1) ret = (ret * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return ret;
}
template <class T>
T inverse(T x, T p) {
return pow(x, p - 2, p);
}
int main() {
i64 n;
const i64 mod = 1000000007;
scanf("%lld", &n);
std::vector<i64> a(n + 1);
std::set<i64> tbd;
for (int i = 1; i <= n; i++) tbd.insert(i);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] > 0) tbd.erase(a[i]);
}
int cnt = 0, al = tbd.size();
for (auto p : tbd) {
if (a[p] == -1) cnt++;
}
std::vector<long long> fact(n + 1), inv(n + 1);
fact[0] = 1;
for (auto i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
inv[n] = inverse(fact[n], mod);
for (auto i = n; i > 0; i--) inv[i - 1] = inv[i] * i % mod;
i64 ret = 0;
for (int i = 0; i <= cnt; i++) {
i64 tmp =
fact[cnt] * inv[i] % mod * inv[cnt - i] % mod * fact[al - i] % mod;
if (i % 2)
ret = (ret + mod - tmp) % mod;
else
ret = (ret + tmp) % mod;
}
printf("%lld\n", ret);
return 0;
}
| 9 | CPP |
#include<bits/stdc++.h>
using namespace std;
const int M = 1000000007;
typedef pair<int, int> P;
struct edge { int to, cap, cost, rev; };
int V;
vector<edge> G[210];
int h[210];
int dist[210];
int prevv[210], preve[210];
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back((edge){ to, cap, cost, (int)G[to].size() });
G[to].push_back((edge){ from, 0, -cost, (int)G[from].size() - 1 });
}
int min_cost_flow(int s, int t, int f) {
int res = 0;
fill(h, h + V, 0);
while (f > 0) {
priority_queue<P, vector<P>, greater<P> > q;
fill(dist, dist + V, M);
dist[s] = 0;
q.push(P(0, s));
while (!q.empty()) {
P p = q.top();
q.pop();
int v = p.second;
if (dist[v] < p.first) continue;
for (int i = 0; i < (int)G[v].size(); ++i) {
edge &e = G[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
q.push(P(dist[e.to], e.to));
}
}
}
if (dist[t] == M) return -1;
for (int v = 0; v < V; ++v)
h[v] += dist[v];
int d = f;
for (int v = t; v != s; v = prevv[v])
d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += d * h[t];
for (int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
int n;
cin >> n;
vector<vector<int>> wc(n, vector<int>(n));
vector<vector<int>> ec(n, vector<int>(n));
vector<vector<int>> cost(n, vector<int>(n));
vector<string> s(n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> wc[i][j];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> ec[i][j];
for (int i = 0; i < n; ++i)
cin >> s[i];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int c = (s[i][j] == 'o' ? 0 : wc[i][j]);
for (int k = 0; k < n; ++k)
if (k != j && s[i][k] == 'o')
c += ec[i][k];
cost[i][j] = c;
}
}
V = n * 2 + 2;
for (int i = 0; i < n; ++i) {
add_edge(n * 2, i, 1, 0);
add_edge(n + i, n * 2 + 1, 1, 0);
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
add_edge(i, n + j, 1, cost[i][j]);
int mc = min_cost_flow(n * 2, n * 2 + 1, n);
vector<vector<int>> cir(n, vector<int>(n, 0));
for (int i = 0; i < n; ++i)
for (edge& e : G[i])
if (e.to >= n && e.to < n * 2 && e.cap == 0)
cir[i][e.to - n] = 1;
vector<string> ans;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s[i][j] == '.' && cir[i][j])
ans.push_back(to_string(i + 1) + " " + to_string(j + 1) + " write\n");
if (s[i][j] == 'o' && !cir[i][j])
ans.push_back(to_string(i + 1) + " " + to_string(j + 1) + " erase\n");
}
}
cout << mc << "\n" << ans.size() << "\n";
for (string i : ans)
cout << i;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
map<int, string> result;
map<string, int> result1;
char to_lowercase(char c) {
if (c >= 'A' && c <= 'Z') return c + 32;
return c;
}
vector<int> v[300];
int level[300];
bool vis[300];
void bfs(int s) {
memset(vis, false, sizeof vis);
queue<int> q;
q.push(s);
level[s] = max(0, level[s]);
vis[s] = true;
while (!q.empty()) {
int p = q.front();
q.pop();
for (int i = 0; i < v[p].size(); i++) {
if (vis[v[p][i]] == false) {
level[v[p][i]] = level[p] + 1;
q.push(v[p][i]);
vis[v[p][i]] = true;
}
}
}
}
int n;
string s1, s2, s3;
int t = -1;
int t1, t2;
int main() {
std::locale loc;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s1 >> s2 >> s3;
for (char &c : s1) {
c = to_lowercase(c);
}
for (char &c : s3) {
c = to_lowercase(c);
}
if (result1.find(s1) == result1.end()) {
t++;
result[t] = s1;
result1[s1] = t;
}
if (result1.find(s3) == result1.end()) {
t++;
result[t] = s3;
result1[s3] = t;
}
v[result1[s3]].push_back(result1[s1]);
}
int maxi = 0;
for (int o = 0; o < 300; o++) {
bfs(o);
}
for (int q = 0; q < 300; q++) {
maxi = max(maxi, level[q] + 1);
}
cout << maxi << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
ll a[n];
int b[n], c[n];
for (int i = 0; i < (n); i++) cin >> a[i] >> b[i] >> c[i];
vector<vector<int>> g(n);
for (int i = 0; i < (n - 1); i++) {
int u, v;
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
int valid = 0;
for (int i = 0; i < (n); i++) valid += b[i] - c[i];
if (valid != 0) {
cout << -1 << endl;
return 0;
}
ll cnt[n][2], dp[n];
for (int i = 0; i < (n); i++) cnt[i][0] = 0, cnt[i][1] = 0, dp[i] = LINF;
multiset<ll> st;
ll ans = 0;
function<void(int, int)> dfs = [&](int pre, int now) {
st.insert(a[now]);
for (auto to : g[now])
if (to != pre) {
dfs(now, to);
cnt[now][0] += cnt[to][0];
cnt[now][1] += cnt[to][1];
}
if (b[now] == 0 and c[now] == 1) cnt[now][0]++;
if (b[now] == 1 and c[now] == 0) cnt[now][1]++;
st.erase(st.find(a[now]));
auto ite = st.lower_bound(a[now]);
if (ite == st.begin()) {
ll tmp = min(cnt[now][0], cnt[now][1]);
ans += 2 * tmp * a[now];
cnt[now][0] -= tmp;
cnt[now][1] -= tmp;
}
};
dfs(-1, 0);
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <cstdio>
#include <iostream>
#include <queue>
#define INF 1e9
struct Edge {
int to, cost;
};
struct Node {
int now, cost, pCost;
};
bool operator <(const Node& n1, const Node& n2) {
return n1.cost < n2.cost;
}
bool operator >(const Node& n1, const Node& n2) {
return n1.cost > n2.cost;
}
using namespace std;
int main() {
int n;
while (cin >> n, n) {
vector<Edge> edge[100];
int m; cin >> m;
for (int i = 0; i < m; ++i) {
int a, b, d;
scanf("%d,%d,%d", &a, &b, &d);
int c = d / 100 - 1;
edge[a].push_back( (Edge){ b, c } );
edge[b].push_back( (Edge){ a, c } );
}
priority_queue<Node, vector<Node>, greater<Node> > Q; Q.push( (Node){ 0, 0, 0 } );
bool f[100] = {0};
int ans = 0;
while ( !Q.empty() ) {
Node node = Q.top(); Q.pop();
if (f[node.now]) continue;
f[node.now] = true;
ans += node.pCost;
for (int i = 0; i < edge[node.now].size(); ++i) {
Edge& e = edge[node.now][i];
Q.push( (Node){ e.to, e.cost, e.cost } );
}
}
cout << ans << endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin >> a>>b>>c;
int m=max({a,b,c});
cout << a+b+c+m*9;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 55;
char a[maxn];
int n;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n;
cin >> a;
int f = 0, e = 0, l = 0;
bool flag = false;
for (int i = 0; i < n - 1; i++) {
int na = 0, nb = 0;
for (int j = i; j < n; j++) {
if (a[j] == 'a')
na++;
else
nb++;
if (na == nb && na >= 1 && na * 2 > l) {
f = i + 1;
e = j + 1;
l = e - f;
flag = true;
}
}
}
if (f != 0)
cout << f << " " << e << "\n";
else {
cout << -1 << " " << -1 << "\n";
}
}
return 0;
}
| 7 | CPP |
a, b = map(int, input().split())
c=0
if a>=1 and b>=1 and a<=100 and b<=100:
while True:
x=a*3
y=b*2
c+=1
if x>y:
print(c)
break
else:
a=x
b=y | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int a,b,c,sum,i,gqt[2006811];
int main()
{
cin>>a>>b>>c;
for(i=1;i<=a;i++)
{
cin>>gqt[i];
if(gqt[i]<b||gqt[i]>=c)
sum++;
}
cout<<sum<<endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int man = 2e5 + 10;
template <typename T>
inline T read() {
T sum = 0, fl = 1;
int ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
return sum * fl;
}
template <typename T>
inline void write(T x) {
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
template <typename T>
T gcd(T a, T b) {
return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T exgcd(T a, T b, T &g, T &x, T &y) {
if (!b) {
g = a, x = 1, y = 0;
} else {
exgcd(b, a % b, g, y, x);
y -= x * (a / b);
}
}
const long long mod = 1e9 + 7;
long long cnt = 0;
struct no {
long long v, w;
no(long long v, long long w) {
this->v = v;
this->w = w;
}
};
vector<no> sp[10 * man];
long long tr[man * 10][2];
void add(long long u, long long v, long long w) { sp[u].push_back(no(v, w)); }
void build1(long long &t, long long l, long long r) {
if (l == r) {
t = l;
return;
}
t = ++cnt;
long long m = l + r >> 1;
build1(tr[t][0], l, m);
build1(tr[t][1], m + 1, r);
add(t, tr[t][0], 0);
add(t, tr[t][1], 0);
}
void build2(long long &t, long long l, long long r) {
if (l == r) {
t = l;
return;
}
t = ++cnt;
long long m = l + r >> 1;
build2(tr[t][0], l, m);
build2(tr[t][1], m + 1, r);
add(tr[t][0], t, 0);
add(tr[t][1], t, 0);
}
void add(long long t, long long l, long long r, long long L, long long R,
long long u, long long w, long long op) {
if (L <= l && r <= R) {
if (2 == op)
add(u, t, w);
else
add(t, u, w);
return;
}
long long m = l + r >> 1;
if (L <= m) add(tr[t][0], l, m, L, R, u, w, op);
if (R > m) add(tr[t][1], m + 1, r, L, R, u, w, op);
}
struct node {
long long dis;
long long v;
node(long long dis = 0, long long v = 0) {
this->dis = dis;
this->v = v;
}
bool operator<(const node &a) const { return dis > a.dis; }
};
long long dis[man * 10];
long long inf = 0x3f3f3f3f3f3f3f3f;
bool vis[man * 10];
void dij(long long s) {
memset(dis, 0x3f3f3f3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
priority_queue<node> q;
q.push(node(0, s));
dis[s] = 0;
while (q.size()) {
long long u = q.top().v;
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (long long i = 0; i < sp[u].size(); i++) {
long long v = sp[u][i].v;
long long w = sp[u][i].w;
if (!vis[v] && dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
q.push(node(dis[v], v));
}
}
}
}
signed main() {
long long n, q, s;
scanf("%lld%lld%lld", &n, &q, &s);
cnt = n;
long long rt1, rt2;
build1(rt1, 1, n);
build2(rt2, 1, n);
for (long long i = 1; i <= q; i++) {
long long u, v, w;
long long op;
scanf("%lld", &op);
if (1 == op) {
scanf("%lld%lld%lld", &u, &v, &w);
add(u, v, w);
} else if (2 == op) {
long long l, r;
scanf("%lld%lld%lld%lld", &u, &l, &r, &w);
add(rt1, 1, n, l, r, u, w, 2);
} else if (3 == op) {
long long l, r;
scanf("%lld%lld%lld%lld", &u, &l, &r, &w);
add(rt2, 1, n, l, r, u, w, 3);
}
}
dij(s);
for (long long i = 1; i <= n; i++) {
printf(i == n ? "%lld\n" : "%lld ", dis[i] == inf ? -1 : dis[i]);
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long T, n, a[2002], dp[2002][2002], res, d[2002][2002], h, l, r;
string S;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> h >> l >> r;
for (int i = 1; i <= n; i++) cin >> a[i];
d[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < h; j++) {
;
+d[i - 1][((j + h + h) - (a[i] - 1)) % h];
+dp[i - 1][((j + h + h) - (a[i] - 1)) % h];
if (d[i - 1][((j + h + h) - a[i]) % h] != 0) {
dp[i][j] = dp[i - 1][((j + h + h) - a[i]) % h];
if (j >= l && j <= r) {
dp[i][j]++;
}
res = max(res, dp[i][j]);
d[i][j] = 1;
}
if (d[i - 1][((j + h + h) - (a[i] - 1)) % h] != 0 &&
dp[i - 1][((j + h + h) - (a[i] - 1)) % h] >= dp[i][j]) {
dp[i][j] = dp[i - 1][((j + h + h) - (a[i] - 1)) % h];
if (j >= l && j <= r) {
dp[i][j]++;
}
res = max(res, dp[i][j]);
d[i][j] = 1;
}
}
}
cout << res;
return 0;
}
| 11 | CPP |
n, k = map(int, input().strip().split())
not_possible = False
alice_only, bob_only, both = [], [], []
alice_likes, bob_likes = 0, 0
total_time = 0
for _ in range(n):
t, alice, bob = map(int, input().strip().split())
alice_likes += alice
bob_likes += bob
if alice + bob == 2:
both.append(t)
elif alice:
alice_only.append(t)
elif bob:
bob_only.append(t)
if alice + bob > 0:
total_time += t
if alice_likes < k or bob_likes < k:
not_possible = True
alice_remove = alice_likes - k
bob_remove = bob_likes - k
both_remove = min(alice_remove, bob_remove)
alice_remove -= both_remove
bob_remove -= both_remove
both.sort(reverse=True)
alice_only.sort(reverse=True)
bob_only.sort(reverse=True)
a, b = 0, 0
while a < len(alice_only) and alice_remove > 0:
total_time -= alice_only[a]
a += 1
alice_remove -= 1
while b < len(bob_only) and bob_remove > 0:
total_time -= bob_only[b]
b += 1
bob_remove -= 1
alice_only = alice_only[a:]
bob_only = bob_only[b:]
ab, o = 0, 0
while both_remove > 0:
curr_ab, curr_both = 0, 0
if ab < len(alice_only) and ab < len(bob_only):
curr_ab = alice_only[ab] + bob_only[ab]
if o < len(both):
curr_both = both[o]
if curr_ab + curr_both == 0:
break
if curr_ab > curr_both:
total_time -= curr_ab
ab += 1
else:
total_time -= curr_both
o += 1
both_remove -= 1
alice_remove += both_remove
bob_remove += both_remove
a, b = ab, ab
while a < len(alice_only) and alice_remove > 0:
total_time -= alice_only[a]
a += 1
alice_remove -= 1
while b < len(bob_only) and bob_remove > 0:
total_time -= bob_only[b]
b += 1
bob_remove -= 1
if not_possible:
print(-1)
else:
print(total_time) | 11 | PYTHON3 |
# cook your dish here
k=int(input())
month=list(map(int,input().rstrip().split()))
month.sort(reverse=True)
#print(month)
count=0
numm=-1
for i in range (12):
if count<k and k>0:
count+=month[i]
numm=i
#break
if count >=k:
print(numm+1)
else:
print(-1)
#print(numm+1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long val[30];
int main() {
string a;
long long k;
cin >> a >> k;
long long mx = -1;
for (int i = 1; i <= 26; i++) {
long long value;
cin >> value;
val[i] = value;
mx = max(value, mx);
}
long long fuc = 0;
for (int i = 0; i < (int)a.size(); i++) {
fuc += (i + 1) * val[a[i] - 'a' + 1];
}
for (int i = (int)a.size(); i < (int)a.size() + k; i++) {
fuc += (i + 1) * mx;
}
cout << fuc << endl;
return 0;
}
| 8 | CPP |
n, m = map(int, input().split())
arr = [num for num in sorted([int(i) for i in input().split()]) if num < 0]
profit=0
i=0
while(i<m and i<len(arr)):
profit+=abs(arr[i])
i+=1
print(profit)
| 8 | PYTHON3 |
p=1
for i in range(1,int(input())+1):p=p*i%(10**9+7)
print(p) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MAXN = 2e5 + 500;
const int MAXM = 1e5 + 5;
const long long mod = 1e9 + 7;
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d %d", &x, &y);
}
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
printf("0");
else
printf("1");
}
printf("\n");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
struct point {
double tt, t;
int id;
};
point pp[101000];
int cmp(point a, point b) { return a.t < b.t; }
int cmp2(point a, point b) { return a.id < b.id; }
int main() {
int n;
double a, d;
scanf("%d%lf%lf", &n, &a, &d);
for (int i = 0; i < n; i++) {
double t, v;
scanf("%lf%lf", &t, &v);
double jiashi = v / a;
double x = 1.0 / 2.0 * a * jiashi * jiashi;
if (x >= d)
pp[i].tt = sqrt(2.0 * d / a) + t;
else {
pp[i].tt = jiashi + (d - x) / v + t;
}
pp[i].t = t;
pp[i].id = i;
}
sort(pp, pp + n, cmp);
double maxx = pp[0].tt;
for (int i = 1; i < n; i++) {
pp[i].tt = maxx = max(maxx, pp[i].tt);
}
sort(pp, pp + n, cmp2);
for (int i = 0; i < n; i++) {
printf("%.4lf\n", pp[i].tt);
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int n;
int x[maxn], y[maxn];
int id[maxn];
bool ok[maxn];
int l[maxn], r[maxn];
long long ans;
void work(bool k) {
memset(ok, 0, sizeof(ok));
int L = 1e9, R = -1e9;
for (int i = 1; i <= n; ++i) {
int xx = x[id[i]], yy = y[id[i]];
if ((abs(xx + yy) & 1) != k) continue;
ok[i] = 1;
L = min(L, yy - xx), R = max(R, yy - xx);
l[i] = L, r[i] = R;
}
L = 1e9, R = -1e9;
for (int i = n; i >= 1; --i) {
int xx = x[id[i]], yy = y[id[i]];
if (!ok[i]) continue;
L = min(L, yy - xx), R = max(R, yy - xx);
int j = i - 1;
while (!ok[j] && j) --j;
if (!ok[j]) break;
int tmp1 = (xx + yy - x[id[j]] - y[id[j]]) / 2;
r[j] = min(r[j], R), l[j] = max(l[j], L);
if (l[j] >= r[j]) continue;
int tmp2 = (r[j] - l[j]) / 2;
ans += (long long)tmp1 * tmp2;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", x + i, y + i);
id[i] = i;
}
sort(id + 1, id + n + 1,
[](const int i, const int j) { return x[i] + y[i] < x[j] + y[j]; });
work(0);
work(1);
printf("%I64d\n", ans);
return 0;
}
| 10 | CPP |
T = int(input())
for t in range(T):
x, y, n = (int(i) for i in input().split())
a = n % x
if a == y:
print(n)
elif n - a + y <= n:
print(n - a + y)
else:
print(n - a + y - x)
| 7 | PYTHON3 |
_ = int(input())
mini = - 10**9-1
maxi = 10**9+1
for i in range(_):
s, n, a = input().split()
n = int(n)
# print('\n---')
# print(s, n, a)
# print('----')
if a == 'Y':
if s == '>=':
mini = max(mini,n)
elif s == '>':
mini = max(mini,n + 1)
elif s == '<=':
maxi = min(maxi, n)
elif s == '<':
maxi = min(maxi, n - 1)
else:
if s == '>=':
maxi = min(maxi, n-1)
elif s == '>':
maxi = min(maxi, n)
elif s == '<=':
mini = max(mini, n+1)
elif s == '<':
mini = max(mini, n)
# print(mini, maxi, '\n--------')
if maxi >= mini:
print(mini)
else:
print('Impossible')
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5;
const long double PI = 3.14159265358979323846;
const long double eps = 1e-11;
const long long INF = 1.5e18;
const long long inf = 1e9;
const long long M = 998244353;
const string IO[2]{"NO\n", "YES\n"};
const string Io[2]{"No\n", "Yes\n"};
const string io[2]{"no\n", "yes\n"};
const string bads = "-1\n";
template <class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
in >> a.first >> a.second;
return in;
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2>& a) {
out << a.first << ' ' << a.second;
return out;
}
template <class T>
istream& operator>>(istream& in, vector<T>& a) {
for (auto& el : a) {
in >> el;
}
return in;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& a) {
for (auto& el : a) {
out << el << ' ';
}
return out;
}
void solve() {
long long n, m, k;
cin >> n >> m >> k;
long long ssize = n / m;
long long bsize = (n + m - 1) / m;
long long cnt = n % m;
vector<vector<pair<long long, long long> > > el(k);
long long start = 0, last_start = 0;
for (long long i = 0; i < k; ++i) {
start = last_start;
for (long long j = 0; j < cnt; ++j) {
el[i].push_back({start, (start + bsize) % n});
start += bsize;
start %= n;
last_start = start;
}
for (long long j = 0; j < m - cnt; ++j) {
el[i].push_back({start, (start + ssize) % n});
start += ssize;
start %= n;
}
}
for (long long i = 0; i < k; ++i) {
for (auto block : el[i]) {
vector<long long> ans;
long long i = block.first;
bool ok = 1;
while (i != block.second || ok) {
ok = 0;
ans.push_back(i++ + 1);
i %= n;
}
cout << ans.size() << ' ' << ans << '\n';
}
}
cout << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long t = 1;
cin >> t;
while (t--) {
solve();
}
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct seg {
int l, r;
} p[200090];
bool cmp(seg a, seg b) { return a.r < b.r; }
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, w;
scanf("%d%d", &x, &w);
p[i].l = x - w;
p[i].r = x + w;
}
sort(p, p + n, cmp);
int R = -2000000009;
int ans = 0;
for (int i = 0; i < n; i++) {
if (p[i].l >= R) {
ans++;
R = max(R, p[i].r);
}
}
cout << ans << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int infi = 1e9 + 7;
const long long infl = 1e18 + 7;
int ar[101];
bitset<101> dp0[10500];
bitset<101> dp[10500];
bitset<101> ndp[10500];
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < (n); ++i) cin >> ar[i];
vector<pair<int, int> > tmp;
sort(ar, ar + n);
int lst = ar[0];
int sum = ar[0];
int cnt = 1;
for (int i = 1; i < n; ++i) {
sum += ar[i];
if (lst == ar[i])
++cnt;
else {
tmp.emplace_back(lst, cnt);
lst = ar[i];
cnt = 1;
}
}
tmp.emplace_back(lst, cnt);
if (tmp.size() <= 2) {
cout << n;
return 0;
}
for (auto cur : tmp) {
for (int cnt = 1; cnt <= cur.second; ++cnt) {
int second = cnt * cur.first;
for (int w = 0; w < (10500); ++w) {
ndp[w + second] |= dp0[w] << cnt;
ndp[w + second] |= dp[w] << cnt;
}
}
for (int cnt = 1; cnt <= cur.second; ++cnt) {
dp0[cnt * cur.first][cnt] = 1;
}
for (int i = 0; i < (10500); ++i) dp[i] |= ndp[i];
}
int ans = 0;
for (auto cur : tmp) {
for (int cnt = 1; cnt <= cur.second; ++cnt) {
int second = cnt * cur.first;
if (dp[second][cnt]) continue;
ans = max(ans, cnt);
if (tmp.size() == 2 && cnt == cur.second) ans = n;
}
}
cout << ans;
return 0;
}
| 8 | CPP |
import sys
import heapq
def _i(): return int(sys.stdin.readline().strip())
def _ia(): return map(int, sys.stdin.readline().strip().split())
def reward(n, d):
s = []
for i in range(n):
for item in d.get(i, []):
heapq.heappush(s, item)
for _ in range(len(s), i+1, -1):
_ = heapq.heappop(s)
return sum(s)
def test():
n = _i()
ll, rr = {}, {}
mn = ln = rn = 0
for i in range(n):
k, l, r = _ia()
mn += min(l, r)
if l >= r:
ll[k-1] = ll.get(k-1, []) + [l-r]
ln += 1
else:
rr[n-k-1] = rr.get(n-k-1, []) + [r-l]
rn += 1
return mn + reward(n, ll) + reward(n, rr)
def main():
t = _i()
ret = [test() for _ in range(t)]
return "\n".join(map(str, ret))
if __name__ == "__main__":
print(main())
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int t;
vector<int>edge[55],edge2[55];
int n;
int up[55],up2[55];
int beg[55],en[55],beg2[55],en2[55],nxt,nxt2;
void dfs(int v,int u){
up[v] = u; beg[v] = nxt++;
for(int i=0;i<edge[v].size();i++){
if(edge[v][i] == u) continue;
dfs(edge[v][i],v);
}
en[v] = nxt-1;
}
void dfs2(int v,int u){
up2[v] = u; beg2[v] = nxt2++;
for(int i=0;i<edge2[v].size();i++){
if(edge2[v][i] == u) continue;
dfs2(edge2[v][i],v);
}
en2[v] = nxt2-1;
}
bool need[55];
bool used[55];
void go(int v,int u){
used[v] = 1;
for(int i=0;i<edge[v].size();i++){
if(edge[v][i] == u) continue;
int d = POSL(edge2[v],edge[v][i]);
if(d == edge2[v].size() || edge2[v][d] != edge[v][i]) continue;
go(edge[v][i],v);
}
}
vector<int>edge_tp[55];
void _dfs(int v,int u){
for(int i=0;i<edge[v].size();i++){
if(edge[v][i] == u) continue;
if(!used[v] && !used[edge[v][i]]){
edge_tp[v].pb(edge[v][i]);
}
_dfs(edge[v][i],v);
}
}
void _dfs2(int v,int u){
for(int i=0;i<edge2[v].size();i++){
if(edge2[v][i] == u) continue;
if(!used[v] && !used[edge2[v][i]]){
edge_tp[edge2[v][i]].pb(v);
}
_dfs2(edge2[v][i],v);
}
}
bool find_cycle(){
int in[55]={};
for(int i=1;i<=n;i++){
for(int j=0;j<edge_tp[i].size();j++){
in[edge_tp[i][j]]++;
}
}
queue<int>que;
for(int i=1;i<=n;i++){
if(!in[i]) que.push(i);
}
while(!que.empty()){
int q = que.front(); que.pop();
for(int i=0;i<edge_tp[q].size();i++){
in[edge_tp[q][i]]--;
if(in[edge_tp[q][i]] == 0){
que.push(edge_tp[q][i]);
}
}
}
for(int i=1;i<=n;i++) if(in[i]) return true;
return false;
}
int sol2(int v){
nxt = 0; nxt2 = 0;
for(int i=1;i<=n;i++){
SORT(edge[i]);
SORT(edge2[i]);
}
dfs(v,-1);
dfs2(v,-1);
for(int i=1;i<=n;i++){
used[i] = 0;
}
go(v,-1);
for(int i=1;i<=n;i++){
edge_tp[i].clear();
}
_dfs(v,-1);
_dfs2(v,-1);
if(find_cycle()) return INF;
int ret = 0;
for(int i=1;i<=n;i++){
ret += (!used[i]);
}
return ret;
}
void sol(){
cin>>n;
for(int i=0;i<n-1;i++){
int a,b; cin>>a>>b;
edge[a].pb(b);
edge[b].pb(a);
}
for(int i=0;i<n-1;i++){
int a,b; cin>>a>>b;
edge2[a].pb(b);
edge2[b].pb(a);
}
int ans = INF;
for(int v=1;v<=n;v++){
if(edge[v].size() > 1) continue;
ans = min(ans,sol2(v));
if(ans <= 1) continue;
int to = edge[v][0];
for(int u=1;u<=n;u++){
if(v == u) continue;
if(to == u) continue;
edge[v].clear();
for(int j=0;j<edge[to].size();j++){
if(edge[to][j] == v){
swap(edge[to][j],edge[to][edge[to].size()-1]);
edge[to].pop_back();
break;
}
}
edge[v].pb(u);
edge[u].pb(v);
ans = min(ans,sol2(v)+1);
edge[v].clear();
for(int j=0;j<edge[u].size();j++){
if(edge[u][j] == v){
swap(edge[u][j],edge[u][edge[u].size()-1]);
edge[u].pop_back();
break;
}
}
edge[v].pb(to);
edge[to].pb(v);
}
}
if(ans > 1e7) ans = -1;
cout << ans << endl;
}
int main(){
cin>>t;
rep(i,t){
rep(j,55) edge[j].clear();
rep(j,55) edge2[j].clear();
sol();
}
} | 0 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const int INF = 1000000007;
int fr[20000];
long long int mod = 1000000007;
long long int binpow(long long int a, long long int b) {
if (!b) return 1;
if (b & 1) {
long long int vi = binpow(a, b - 1);
return (vi * a) % mod;
}
long long int vi = binpow(a, b / 2);
return (vi * vi) % mod;
}
vector<long long int> fac;
long long int ce(long long int n, long long int k) {
return (((fac[n] * binpow(fac[n - k], mod - 2)) % mod) *
binpow(fac[k], mod - 2)) %
mod;
}
int main() {
long long int n;
cin >> n;
vector<int> op(n);
memset(fr, -1, sizeof fr);
for (long long int i = 0; i < n; i++) {
cin >> op[i];
if (op[i] > 0) op[i]--;
if (op[i] != -1) {
fr[op[i]] = 1;
if (fr[i] == -1) fr[i] = 0;
}
}
long long int all = 0;
long long int fail = 0;
for (long long int i = 0; i < n; i++) {
if (fr[i] == -1) {
fail++;
}
if (fr[i] == 0) {
all++;
}
}
fac.resize(n + 1);
fac[0] = 1;
for (long long int i = 1; i <= n; i++) {
fac[i] = (fac[i - 1] * i) % mod;
}
long long int mn = fac[all];
long long int k = all + fail;
long long int ans = fac[k];
for (long long int i = 1; i <= fail; i++) {
long long int val = (ce(fail, i) * fac[k - i]) % mod;
if (i & 1) {
ans = (ans + mod - val) % mod;
} else
ans = (ans + val) % mod;
}
cout << ans;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
long long int bini = 0, bnew = 0, ans = 0, l;
cin >> s;
l = s.length();
while (l >= 0) {
if (s[l] == 'b') {
bini = (bini % 1000000007 + 1 % 1000000007) % 1000000007;
} else if (s[l] == 'a') {
ans = (ans % 1000000007 + bini % 1000000007) % 1000000007;
bini = ((bini % 1000000007) * 2 % 1000000007) % 1000000007;
}
l--;
}
ans = ans % 1000000007;
cout << ans;
return 0;
}
| 8 | CPP |
// Author: Vinay Khilwani
// Language: C++
// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB, CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank
// Never Stop Trying.
// Trying to be Better than Myself.
// while(true)
// {
// if(AC)
// {
// break;
// }
// else if(Contest Over)
// {
// Try.
// Check out Editorial.
// Understand.
// Find out your Mistake.
// Learn the topic (if new).
// Solve Problems on that topic (if new).
// Upsolve that problem.
// break;
// }
// else
// {
// Try.
// Use Pen-Paper.
// Find errors, edge cases, etc.
// continue;
// }
// }
// Optimizations
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// Debugging
#define dbg(a) cerr<<a<<"\n";
#define debug_a(a) for(auto x:a) {cerr<<x<<" ";} cerr<<"\n";
#define debug_b(a) for(auto x:a) {cerr<<"["<<x.first<<", "<<x.second<<"]"<<"\n";} cerr<<"\n";
#define debug_c(a) for(auto x:a) {debug_a(x)} cerr<<"\n";
#define debug_d(a) for(auto x:a) {debug_b(x)} cerr<<"\n";
#define debug_e(a) cerr<<"["<<a.first<<", "<<a.second<<"]"<<"\n";
// Defines
#define fast ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define loop(i,a,n) for(int i=a; i<n; i++)
#define rloop(i,a,n) for(int i=a; i>=n; i--)
#define fr(i,a,n,b) for(int i=a; i<n; i+=b)
#define rfr(i,a,n,b) for(int i=a; i>=n; i-=b)
#define IN cin>>
#define OUT cout<<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(),(a).end()
#define each(a,b) for(auto &a:b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) for(auto x:a) {OUT x<<" ";} OUT endl;
#define read(a) for(auto &x:a) {IN x;}
// #define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vi=vector<int>;
const ll mod=(ll)(1e9)+7LL;
const ll M=998244353LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const ld pi=acos(-1);
// General Functions
ll gcd(ll a, ll b)
{
return (b?gcd(b,a%b):a);
}
ll P(ll B, ll power, ll modulo) //Fast Power
{
ll ans=1LL;
while(power>0LL)
{
if(power%2LL==1LL)
{
ans=(ans*B)%modulo;
}
B=(B*B)%modulo;
power/=2LL;
}
return ans;
}
bool isPrime(ll n)
{
if(n<=1LL)
{
return false;
}
if(n<=3LL)
{
return true;
}
if(n%2==0LL || n%3==0LL)
{
return false;
}
for(ll i=5LL; (i*i)<=n; i+=6LL)
{
if(n%i==0LL || n%(i+2LL)==0LL)
{
return false;
}
}
return true;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r)
{
uniform_int_distribution<ll> uid(l,r);
return uid(rng);
}
void vok()
{
fast
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
}
// Global Variables
const int mxN=int(1e5)+100;
// Solver Function(s)
void solve()
{
int n,k;
IN n>>k;
vi p(n);
read(p)
vi in_deg(n+1,0);
vi g[n+1];
set<int> ss;
loop(i,0,k)
{
int a,b;
IN a>>b;
g[a].pb(b);
ss.insert(a);
ss.insert(b);
in_deg[b]++;
}
loop(i,0,n)
{
if(p[i]==i+1)
{
OUT 0<<nl;
return;
}
}
queue<int> q;
loop(i,1,n+1)
{
if(in_deg[i]>1 || sz(g[i])>1)
{
OUT 0<<nl;
return;
}
}
loop(i,1,n+1)
{
if(sz(g[i])==1 && in_deg[i]==0)
{
q.push(i);
}
}
vector<vi> v;
while(!q.empty())
{
auto curr=q.front();
q.pop();
vi temp;
while(sz(g[curr])>0)
{
ss.erase(curr);
temp.pb(curr);
curr=g[curr][0];
}
ss.erase(curr);
temp.pb(curr);
v.pb(temp);
}
if(sz(ss))
{
OUT 0<<nl;
return;
}
each(x,v)
{
set<int> ps(all(x));
loop(i,0,sz(x))
{
ps.erase(x[i]);
if(p[x[i]-1] && ps.find(p[x[i]-1])!=ps.end())
{
OUT 0<<nl;
return;
}
}
}
map<int,vi> mm;
int ind=0;
int group=n+1;
vi gr(n+1,-1);
vi mapping(n+1);
each(x,v)
{
each(y,x)
{
gr[y]=group;
mapping[y]=ind;
}
group++;
mm[ind++]=x;
}
loop(i,1,n+1)
{
if(gr[i]==-1)
{
mapping[i]=ind;
mm[ind++].pb(i);
}
}
// each(x,mm)
// {
// OUT x.first<<nl;
// write(x.second)
// }
// write(mapping)
in_deg=vi(sz(mm),0);
loop(i,1,n+1)
{
g[i].clear();
}
loop(i,0,n)
{
if(p[i])
{
if(mapping[i+1]==mapping[p[i]])
{
continue;
}
g[mapping[p[i]]].pb(mapping[i+1]);
in_deg[mapping[i+1]]++;
}
}
// write(in_deg)
loop(i,0,sz(mm))
{
if(in_deg[i]==0)
{
q.push(i);
}
}
vi top_sort;
int cnt=0;
while(!q.empty())
{
auto curr=q.front();
q.pop();
top_sort.pb(curr);
each(x,g[curr])
{
if(--in_deg[x]==0)
{
q.push(x);
}
}
cnt++;
}
if(cnt!=sz(mm))
{
OUT 0<<nl;
return;
}
each(x,top_sort)
{
each(y,mm[x])
{
OUT y<<" ";
}
}
OUT nl;
}
// Main Function
int main()
{
vok();
int t=1;
// IN t;
while(t--)
{
solve();
}
return 0;
} | 11 | CPP |
n = int(input())
count = 0
m = 1
flag = 0
lst = [int(x) for x in input().split()]
for i in range(n):
if lst[i] == 0:
flag = 1
count += 1
else:
count += abs(lst[i]) - 1
lst[i] = -1 if lst[i] < 0 else 1
m = m*lst[i]
if m == -1 and flag == 0:
count += 2
print(count) | 8 | PYTHON3 |
import functools
import heapq as hp
import collections
import bisect
import math
def unpack(func=int):
return map(func, input().split())
def l_unpack(func=int):
"""list unpack"""
return list(map(func, input().split()))
def s_unpack(func=int, rev=False):
"""sorted list unpack"""
return sorted(map(func, input().split()), reverse=rev)
def ml_unpack(n): # multiple line unpack
"""list of n integers passed on n line, one on each"""
return [int(input()) for i in range(n)]
def range_n():
return range(int(input()))
def getint():
return int(input())
def counter(a):
d = {}
for x in a:
if x in d:
d[x] += 1
else:
d[x] = 1
return d
def main():
n = getint()
a = input()
ans = []
for e in a:
if e in {'0', '1'}:
continue
if e == '4':
ans.append(2)
ans.append(2)
ans.append(3)
elif e == '6':
ans.append(3)
ans.append(5)
elif e == '8':
ans.append(2)
ans.append(2)
ans.append(2)
ans.append(7)
elif e == '9':
ans.append(3)
ans.append(3)
ans.append(2)
ans.append(7)
else:
ans.append(int(e))
ans.sort(reverse=True)
print(*ans, sep='')
main()
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int a, b;
cin >> a >> b >> s;
for (int n = 0; n < b; n++) {
for (int i = 0; i < a; i++) {
if (s[i] == 'B' && s[i + 1] == 'G') {
s[i] = 'G';
s[i + 1] = 'B';
i++;
}
}
}
cout << s;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& a) {
return out << "(" << a.first << ", " << a.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); ++i) {
if (v[i] != 1000000001)
os << v[i];
else
os << "INF";
if (i != v.size() - 1) os << ", ";
}
os << "]\n";
return os;
}
const int N = 3e5 + 10;
vector<int> adj[N] = {}, adj2[N] = {};
vector<int> vis(N);
pair<bool, int> dfs(int u = 0, int p = -1) {
vis[u] = 1;
pair<bool, int> temp = make_pair(false, -1);
for (auto v : adj[u]) {
if (v - p) {
if (vis[v] == 1) {
temp = make_pair(true, v);
adj2[v].push_back(u);
} else if (!vis[v]) {
pair<bool, int> p = dfs(v, u);
if (p.first) {
adj2[p.second].push_back(u);
temp = p;
}
}
}
}
vis[u] = 2;
if (!temp.first || u == temp.second) temp = make_pair(false, -1);
return temp;
}
long long int f(vector<long long int>& pref, int l, int r) {
if (r < 0) return 0;
if (l > 0)
return pref[r] - pref[l - 1];
else
return pref[r];
}
int main() {
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
};
int n, m;
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<pair<int, int> > w;
vector<int> next(N);
vector<long long int> pref(N);
for (int i = 0; i < n; i++) {
if (!vis[i]) dfs(i);
}
for (int i = 0; i < n; i++) {
vector<int> v = adj2[i];
if (v.size() == 0) continue;
int max1 = -1, min1 = 1000000001;
for (int i = 0; i < v.size(); i++) {
max1 = max(max1, v[i]);
min1 = min(min1, v[i]);
}
w.push_back(make_pair(min1, max1));
}
sort(w.begin(), w.end());
for (int i = 0; i < n; i++) next[i] = n;
int right = n;
for (int i = w.size() - 1; i >= 0; i--) {
next[w[i].first] = w[i].second;
}
for (int i = n - 1; i >= 0; i--) {
right = min(next[i], right);
next[i] = right;
pref[i] = right - i;
}
for (int i = 1; i < n; i++) pref[i] += pref[i - 1];
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
a--;
b--;
int beg = a, end = b;
long long int ans = a - 1;
while (beg <= end) {
int mid = (beg + end) >> 1;
if (next[mid] <= b) {
ans = mid;
beg = mid + 1;
} else
end = mid - 1;
}
long long int count = f(pref, a, ans) + ((b - ans) * (b - ans + 1)) / 2;
cout << count << endl;
}
return 0;
}
| 9 | CPP |
A = []
for i in range(5):
s = list(map(int, input().split()))
A.append(s)
for i in range(5):
for j in range(5):
if A[i][j] == 1:
x = int(i)
y = int(j)
break
print(abs(2-x) + abs(2-y))
| 7 | PYTHON3 |
#define NDEBUG
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
constexpr double EPS = 1e-8;
struct point {
double x, y;
point(double x_, double y_):x(x_), y(y_) {}
inline point operator+(const point &p) const {
return point(x + p.x, y + p.y);
}
inline point operator-(const point &p) const {
return point(x - p.x, y - p.y);
}
inline point operator*(double s) const {
return point(x * s, y * s);
}
};
inline double dot(const point &a, const point &b) {
return a.x * b.x + a.y * b.y;
}
inline double cross(const point &a, const point &b) {
return a.x * b.y - a.y * b.x;
}
inline double norm(const point &p) {
return p.x * p.x + p.y * p.y;
}
struct line {
point a, b;
line(const point &a_, const point &b_):a(a_), b(b_) {}
};
inline point project(const line &l, const point &p) {
const point diff = l.b - l.a;
return l.a + diff * (dot(p - l.a, diff) / norm(diff));
}
inline point reflect(const line &l, const point &p) {
return p + (project(l, p) - p) * 2.0;
}
inline point crosspoint(const line &l, const line &m) {
const double tmp = cross(l.b - l.a, m.b - m.a);
if(abs(tmp) < EPS) return l.a;
return m.a + (m.b - m.a) * (cross(l.b - l.a, l.a - m.a) / tmp);
}
typedef pair<bool, int> result; // is point, idx
vector<point> points;
vector<line> lines;
result parse(string::const_iterator &);
int number(string::const_iterator &it) {
int sign = 1;
while(*it == '-') {
sign = -1;
++it;
}
int res = 0;
while(isdigit(*it)) {
res = res * 10 + *it - '0';
++it;
}
return sign * res;
}
result factor(string::const_iterator &it) {
assert(*it == '(');
++it;
if(*it == '-' || isdigit(*it)) {
const double x = number(it);
assert(*it == ',');
++it;
const double y = number(it);
assert(*it == ')');
++it;
points.emplace_back(x, y);
return make_pair(true, points.size() - 1);
}
else {
const result res = parse(it);
assert(*it == ')');
++it;
return res;
}
}
result parse(string::const_iterator &it) {
result res = factor(it);
while(*it == '@') {
++it;
result tmp = factor(it);
switch(res.first + tmp.first) {
case 2: // point@point
{
lines.emplace_back(points[res.second], points[tmp.second]);
res = make_pair(false, lines.size() - 1);
break;
}
case 1: // line@point
{
if(res.first) swap(res, tmp);
points.emplace_back(reflect(lines[res.second], points[tmp.second]));
res = make_pair(true, points.size() - 1);
break;
}
case 0: // line@line
{
points.emplace_back(crosspoint(lines[res.second], lines[tmp.second]));
res = make_pair(true, points.size() - 1);
break;
}
default: assert(false);
}
}
return res;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(10);
for(string s; cin >> s && s != "#";) {
points.clear();
lines.clear();
string::const_iterator it = s.cbegin();
const result ans = parse(it);
const point p = points[ans.second];
cout << p.x << ' ' << p.y << endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<int> res;
int cnt = 0;
char c1, c2;
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt = 0;
else
cnt++;
if (cnt >= 3) {
if (s[i] == c1 && c1 == c2)
cnt--;
else {
res.push_back(i);
cnt = 1;
}
}
c1 = c2;
c2 = s[i];
}
res.push_back(s.length());
int st = 0;
for (int i = 0; i < res.size(); i++) {
cout << s.substr(st, res[i] - st) << " ";
st = res[i];
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
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>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
int main(){
int N, M;
cin >> N >> M;
int A[N], B[N], C[N];
rep(i, N) cin >> A[i] >> B[i] >> C[i];
int D[M], E[M], F[M];
rep(i, M) cin >> D[i] >> E[i] >> F[i];
map<int, int> mpx, mpy;
mpx[-inf]++, mpx[inf]++, mpy[-inf]++, mpy[inf]++;
mpx[0]++, mpy[0]++;
rep(i, N){
mpx[A[i]]++, mpx[B[i]]++, mpy[C[i]]++;
}
rep(i, M){
mpx[D[i]]++, mpy[E[i]]++, mpy[F[i]]++;
}
int n = 0, m = 0;
for(auto &e: mpx) e.second = n++;
for(auto &e: mpy) e.second = m++;
int xs[n], ys[m];
for(auto &e: mpx) xs[e.second] = e.first;
for(auto &e: mpy) ys[e.second] = e.first;
bool es[n-1][m-1][4];
rep(i, n-1) rep(j, m-1) rep(k, 4) es[i][j][k] = true;
rep(i, N){
int a = mpx[A[i]], b = mpx[B[i]], c = mpy[C[i]];
rep2(j, a, b-1){
es[j][c-1][3] = false;
es[j][c][1] = false;
}
}
rep(i, M){
int d = mpx[D[i]], e = mpy[E[i]], f = mpy[F[i]];
rep2(j, e, f-1){
es[d-1][j][2] = false;
es[d][j][0] = false;
}
}
int ex[] = {-1, 0, 1, 0}, ey[] = {0, -1, 0, 1};
ll dx[n-1], dy[m-1];
rep(i, n-1) dx[i] = xs[i+1]-xs[i];
rep(i, m-1) dy[i] = ys[i+1]-ys[i];
int sx = mpx[0], sy = mpy[0];
ll ans = 0;
bool used[n-1][m-1];
fill(used[0], used[n-1], false);
queue<pii> que;
que.push(pii(sx, sy));
while(!que.empty()){
int x = que.front().first, y = que.front().second;
que.pop();
if(used[x][y]) continue;
if(x == 0 || x == n-2 || y == 0 || y == m-2){
cout << "INF" << endl;
return 0;
}
used[x][y] = true;
ans += dx[x]*dy[y];
rep(k, 4){
int nx = x+ex[k], ny = y+ey[k];
if(!es[x][y][k] || used[nx][ny]) continue;
que.push(pii(nx, ny));
}
}
cout << ans << endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
void SolveA() {
long long p;
long long q;
cin >> p >> q;
size_t vSize = 10000000;
vector<int> isPrime(vSize, 1);
isPrime[0] = isPrime[1] = 0;
for (size_t i = 2; i < vSize; ++i) {
if (isPrime[i]) {
size_t cur = 2 * i;
while (cur < vSize) {
isPrime[cur] = 0;
cur += i;
}
}
}
vector<size_t> d(10, 0);
size_t nd = 1;
long long nPrime = 0;
long long nPal = 0;
size_t ans = 0;
for (size_t i = 1; i < vSize; ++i) {
if (isPrime[i]) ++nPrime;
size_t curD = 0;
size_t carry = 1;
while (curD < nd || carry > 0) {
size_t newD = (carry + d[curD]) % 10;
carry = (carry + d[curD]) / 10;
d[curD] = newD;
++curD;
}
nd = max(curD, nd);
bool pal = true;
for (size_t j = 0; j < nd / 2; ++j) pal = pal && (d[j] == d[nd - j - 1]);
if (pal) ++nPal;
if (q * nPrime <= p * nPal) ans = i;
}
if (ans == 0)
cout << "Palindromic tree is better than splay tree";
else
cout << ans;
}
void SolveB() {
size_t n;
cin >> n;
size_t MOD = 1000000007;
vector<vector<size_t> > C(n + 1, vector<size_t>(n + 1, 0));
C[0][0] = 1;
for (size_t i = 1; i <= n; ++i) {
C[i][0] = 1;
for (size_t j = 1; j <= i; ++j) {
C[i][j] = C[i - 1][j - 1];
if (j < i) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
}
}
vector<unsigned long long> F(n + 1);
F[0] = 1;
for (size_t i = 1; i <= n; ++i) {
F[i] = 0;
for (size_t j = 0; j < i; ++j)
F[i] = (F[i] + C[i - 1][j] * F[i - 1 - j]) % MOD;
}
unsigned long long ans = 0;
for (size_t i = 1; i <= n; ++i) ans = (ans + F[n - i] * C[n][i]) % MOD;
cout << ans;
}
int main() {
SolveB();
return 0;
}
| 8 | CPP |
t = int(input())
for i in range (t):
n, k = input().split()
last = n
k = int(k) - 1
for j in range (k):
temp = last
temp = sorted(temp)
if(temp[0] == '0'):
break
last = str(int(last) + (int(temp[0]) * int(temp[len(temp)-1])))
print(last) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200007;
template <class T>
struct fenwick_tree {
vector<T> a;
int n;
void initialize(int k) {
n = k;
a.assign(n + 1, 0);
}
void update(int pos, T val) {
for (; pos <= n; pos += pos & (-pos)) {
a[pos] += val;
}
}
T query(int pos) {
T ans = 0;
for (; pos >= 1; pos -= pos & (-pos)) {
ans += a[pos];
}
return ans;
}
T query(int l, int r) { return query(r) - query(l - 1); }
};
int n, q, arr[N], first[N], last[N];
fenwick_tree<long long> it;
vector<int> pos[N];
int zeros_cnt;
pair<int, int> interval[N];
int ans[N];
vector<int> add_time[N], delete_time[N];
set<int, greater<int> > s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, j;
scanf("%d %d", &n, &q);
for (i = 1; i <= q; i++) {
first[i] = last[i] = -1;
}
for (i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
last[arr[i]] = i;
pos[arr[i]].push_back(i);
if (arr[i] == 0) ++zeros_cnt;
}
if (zeros_cnt == n) {
printf("YES\n");
for (i = 1; i <= n; i++) {
if (i > 1) printf(" ");
printf("%d", q);
}
printf("\n");
return 0;
}
if (pos[q].empty()) {
if (zeros_cnt == 0) {
printf("NO\n");
return 0;
}
for (i = 1; i <= n; i++) {
if (arr[i] == 0) {
arr[i] = q;
first[q] = last[q] = i;
pos[q].push_back(i);
break;
}
}
}
for (i = n; i >= 1; i--) {
first[arr[i]] = i;
}
it.initialize(n);
for (i = 1; i <= n; i++) {
if (arr[i] == 0) {
it.update(i, 1);
}
}
for (i = q; i >= 1; i--) {
if (pos[i].empty()) {
interval[i] = make_pair(0, 0);
continue;
}
for (j = 0; j < (int)(pos[i].size()); j++) {
it.update(pos[i][j], 1);
}
if (it.query(first[i], last[i]) != last[i] - first[i] + 1) {
printf("NO\n");
return 0;
}
add_time[first[i]].push_back(i);
delete_time[last[i] + 1].push_back(i);
}
printf("YES\n");
for (i = 1; i <= n; i++) {
for (j = 0; j < (int)(delete_time[i].size()); j++) {
s.erase(delete_time[i][j]);
}
for (j = 0; j < (int)(add_time[i].size()); j++) {
s.insert(add_time[i][j]);
}
if (!s.empty()) {
ans[i] = *s.begin();
}
}
for (i = 2; i <= n; i++) {
if (ans[i - 1] != 0 && ans[i] == 0) {
ans[i] = ans[i - 1];
}
}
for (i = n - 1; i >= 1; i--) {
if (ans[i + 1] != 0 && ans[i] == 0) {
ans[i] = ans[i + 1];
}
}
for (i = 1; i <= n; i++) {
if (i > 1) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
return 0;
}
| 10 | CPP |
n, k = map(int, input().split())
A = list(map(int, input().split()))
s = sum(A)
def md(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
divisors.sort(reverse=True)
return divisors
P = md(s)
def test(x):
L = sorted([a%x for a in A])
M = sum(L)
c = M//x
for _ in range(c):
M -= L.pop()
return M <= k
for p in P:
if test(p):
print(p)
break | 0 | PYTHON3 |
b=int(input())
a=input()
done=0
for i in range(len(a)-1):
if not a[i]==a[i+1]:
print("YES")
print(a[i]+a[i+1])
done=1
break
if done==0:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int n, m, rt, sz[MAXN], d[MAXN];
vector<int> G[MAXN];
vector<pair<int, int> > son;
bool f[MAXN];
void get_sz(int u, int fa) {
sz[u] = 1;
for (int v : G[u])
if (v != fa) {
get_sz(v, u);
sz[u] += sz[v];
}
}
int get_rt(int u, int fa) {
for (int v : G[u])
if (v != fa) {
if (sz[v] * 2 >= n) return get_rt(v, u);
}
return u;
}
void dfs(int u, int fa, int I) {
printf("%d %d %d\n", fa, u, ((d[u] = ++m) - d[fa]) * I);
for (int v : G[u])
if (v != fa) dfs(v, u, I);
}
int main() {
scanf("%d", &n);
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
get_sz(1, 0);
rt = get_rt(1, 0);
get_sz(rt, 0);
for (int v : G[rt]) son.push_back(make_pair(sz[v], v));
sort(son.begin(), son.end());
int cur = 0;
for (pair<int, int> p : son) {
if (cur + p.first <= n * 2 / 3) {
dfs(p.second, rt, 1);
f[p.second] = true;
cur += p.first;
}
}
m = 0;
for (pair<int, int> p : son)
if (!f[p.second]) dfs(p.second, rt, cur + 1);
return 0;
}
| 10 | CPP |
n=int(input())
l=[int(i) for i in input().split()]
p=[1]
for i in range(1,len(l)):
if(l[i]>l[i-1]):
p.append(p[-1]+1)
else:
p.append(1)
print(max(p)) | 7 | PYTHON3 |
for x in range(int(input())):
a = int(input())
l = []
if a>2:
for x in range(1,a//2+1):
l.append(x*2)
for i in range(1,a-1,2):
f=i
l.append(i)
if (f+2+a//2)%2==0:
print("NO")
else:
print("YES")
print(*l,f+2+a//2)
else:
print("NO") | 8 | PYTHON3 |
"""
アンバランス
・長さが2以上
→そのまま
・過半数の文字が同じ
→abcaa とか aabacda aa とか
ある2文字や3文字の区間を見た時に
・2文字:両方一致
・3文字:3つ中二つが一致
ならアンバランス
"""
S = input()
N = len(S)
for i in range(N-1):
if len(set(S[i:i+2])) == 1:
print(i+1, i+2)
exit()
for i in range(N-2):
if len(set(S[i:i+3])) <= 2:
print(i+1, i+3)
exit()
print(-1, -1) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long t, x, ans, len;
const long long mod = 1e9 + 7;
string s;
signed main() {
scanf("%lld", &t);
while (t--) {
scanf("%lld", &x);
cin >> s;
ans = s.length();
for (long long i = 0; i < x; i++)
if (s.length() < x) {
len = s.length();
for (long long j = 0; j <= s[i] - '2'; j++)
for (long long k = i + 1; k < len; k++) s += s[k];
ans = s.length();
} else
ans = (ans + (ans - i - 1 + mod) * (s[i] - '1')) % mod;
printf("%d\n", ans);
}
return 0;
}
| 7 | CPP |
a=list(input())
b=list(input())
c=[]
for i in range(0,len(a)):
if a[i]==b[i]:
c.append('0')
else:
c.append('1')
print(''.join(c))
| 7 | PYTHON3 |
formatLen, toCheckLen = [int(x) for x in input().split(" ")]
format = input()
toCheck = input()
if "*" not in format:
if format == toCheck:
print("YES")
else:
print("NO")
exit()
if not toCheck.isalpha():
print("NO")
exit()
start, end = format.split("*")
if toCheckLen - len(end) <= len(start) - 1:
print("NO")
exit()
if not toCheck.startswith(start) or not toCheck.endswith(end):
print("NO")
exit()
print("YES")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MN = 2e5 + 10;
int n, arr[MN];
long long ans;
long long a[MN], b[MN];
vector<pair<long long, long long> > vec;
int id[MN];
int sz;
bool cmp(const int &x, const int &y) { return a[x] < a[y]; }
bool check(long long x, int id1, int id2) {
return (a[id1] * x + b[id1]) >= (a[id2] * x + b[id2]);
}
long long calc(int id1, int id2) {
return (b[id1] - b[id2] + (a[id2] - a[id1] - 1)) / (a[id2] - a[id1]);
}
void solve(int l, int r) {
if (r - l == 1) {
ans = max(ans, 1ll * arr[l]);
return;
}
int mid = l + r >> 1;
solve(l, mid), solve(mid, r);
sz = 0;
for (int i = mid; i < r; ++i) {
if (!sz)
a[sz] = arr[i], b[sz] = arr[i];
else
a[sz] = a[sz - 1] + arr[i], b[sz] = b[sz - 1] + 1ll * (sz + 1) * arr[i];
++sz;
}
vec.clear();
for (int i = 0; i < sz; ++i) id[i] = i;
sort(id, id + sz, cmp);
for (int i = 0; i < sz; ++i) {
int x = id[i];
while (!vec.empty() && check(vec.back().first, x, vec.back().second))
vec.pop_back();
if (vec.empty())
vec.push_back({0, x});
else {
if (a[vec.back().second] == a[x]) continue;
vec.push_back({calc(vec.back().second, x), x});
}
}
bool fl = (l == 0 && r == n);
long long sm1 = 0, sm2 = 0;
int cnt = 0, pnt = 0;
for (int i = mid - 1; i >= l; --i) {
++cnt;
sm1 += arr[i];
sm2 += sm1;
while (pnt < ((int)(vec).size()) && vec[pnt].first <= cnt) ++pnt;
long long ind = vec[pnt - 1].second;
ans = max(ans, sm2 + a[ind] * cnt + b[ind]);
}
}
int main() {
ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> arr[i];
solve(0, n);
cout << ans << '\n';
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353;
int n, tot = 1;
map<int, int> L, R;
set<int> S;
int main() {
scanf("%d", &n);
for (int i = 1, a; i <= n; i++) {
scanf("%d", &a);
if (!L.count(a)) L[a] = i;
R[a] = i;
S.insert(i);
}
for (auto X : L) {
auto l = S.upper_bound(X.second);
auto r = S.upper_bound(R[X.first]);
S.erase(l, r);
}
for (int i = 0; i < (int)S.size() - 1; i++) tot = tot * 2 % Mod;
return !printf("%d", tot);
}
| 11 | CPP |
#include <iostream>
#define MUL(a, b) (((a) * (b)) % 1000000007)
using namespace std;
int main(){
int p[100001] = {3};
long long int ans = 1;
int a, n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a;
ans = MUL(p[a], ans);
p[a] --; p[a+1] ++;
}
cout << ans << endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2000;
const int MV = 1e5;
const int MOD = 1e9 + 7;
const int INF = 2e9;
int MA = -INF, MI = INF, ANS;
int n, m, r, c;
bool row[MAX + 10], col[MAX + 10];
long double dp[MAX + 10][MAX + 10];
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
r = n, c = n;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (!row[x]) row[x] = 1, r--;
if (!col[y]) col[y] = 1, c--;
}
for (int i = 1; i <= n; i++)
dp[i][0] = dp[i - 1][0] + (double(1) * n / i), dp[0][i] = dp[i][0];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][j - 1] * i * j;
dp[i][j] += dp[i - 1][j] * i * (n - j);
dp[i][j] += dp[i][j - 1] * (n - i) * j;
dp[i][j] /= n * n;
dp[i][j] = dp[i][j] + 1;
dp[i][j] /= 1.00 - double(double((n - i) * (n - j)) / double(n * n));
}
}
cout << fixed << setprecision(10) << dp[r][c] << "\n";
return 0;
}
| 8 | CPP |
n = int(input())
a = input()
A = []
for i in a:
i = int(i)
if i == 2:
A.append(2)
elif i == 3:
A.append(3)
elif i == 4:
A.append(2)
A.append(2)
A.append(3)
elif i == 5:
A.append(5)
elif i == 6:
A.append(3)
A.append(5)
elif i == 7:
A.append(7)
elif i == 8:
A.append(2)
A.append(2)
A.append(2)
A.append(7)
elif i == 9:
A.append(3)
A.append(3)
A.append(2)
A.append(7)
A.sort(key=None, reverse=True)
#print(A)
s = ''
for k in A:
s = s+str(k)
print(s)
| 9 | PYTHON3 |
import sys
n, k = sys.stdin.readline().strip().split()
n = int(n)
k = int(k)
x = sys.stdin.readline().strip()
X = [0] * n
for i in range (0, n):
X[i] = int(x[i])
v = 0
w = 0
for i in range (0, n):
if v == 0 and (X[i] > X[i % k]):
v = -1
elif v == 0 and (X[i] < X[i % k]):
v = 1
ans = []
if v == -1:
i = k - 1
while X[i] == 9:
i = i - 1
while i < k:
X[i] = (X[i] + 1) % 10
i = i + 1
for i in range (0, n):
ans.append(str(X[i % k]))
print(n)
print("".join(ans))
| 7 | PYTHON3 |
#include <iostream>
using namespace std;
int main() {
int h, a;
cin >> h >> a;
cout << (h - 1) / a + 1 << endl;
} | 0 | CPP |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
const int MAX_V=1001;
struct edge{
int from,to;
string cost;
};
edge es[MAX_V];
string d[MAX_V];
int V,E;
string INF;
bool bellman_ford(int s,int g){
for(int i=0;i<V;i++)d[i]=INF;
d[s]="";
int cnt=0;
while(1){
bool update=false;
bool gupdate=false;
for(int i=0;i<E;i++){
edge &e=es[i];
if(d[e.from]!=INF&&d[e.to]>e.cost+d[e.from]){
d[e.to]=e.cost+d[e.from];
update=true;
if(e.to==g)gupdate=true;
}
}
if(!update)break;
cnt++;
// S[ðXVµÂïéÌÂHª¶Ý
if(cnt>=V&&gupdate)return false;
else if(cnt>=10*V)return true;
}
return true;
}
int N,A,S,G;
int main(){
for(int i=0;i<1000;i++)INF+='z';
while(cin>>N>>A>>S>>G&&(N|A|S|G)){
E=A;V=N;
for(int i=0;i<A;i++){
edge e;
cin>>e.to>>e.from>>e.cost;
es[i]=e;
}
bool res=bellman_ford(G,S);
if(!res||d[S]==INF)cout<<"NO"<<endl;
else cout<<d[S]<<endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool readInteger(T& x) {
char c, r = 0, n = 0;
x = 0;
while (true) {
c = getchar();
if (c < 0 && !r)
return 0;
else if (c == '-' && !r)
n = 1;
else if (c >= '0' && c <= '9')
x = x * 10 + c - '0', r = 1;
else if (r)
break;
}
if (n) x = -x;
return 1;
}
template <typename T>
vector<T> readIntegral(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) readInteger(ret[i]);
return ret;
}
auto readInts = readIntegral<int>;
auto readLongs = readIntegral<long long>;
template <typename T, T MOD>
struct ModInt {
T value;
ModInt() : value(0) {}
ModInt(T x) {
x %= MOD;
if (x < 0) x += MOD;
value = x;
}
public:
ModInt& operator+=(ModInt x) {
value += x.value;
if (value >= MOD) value -= MOD;
return *this;
}
ModInt& operator-=(ModInt x) {
value -= x.value;
if (value < 0) value += MOD;
return *this;
}
ModInt& operator*=(ModInt x) {
value *= x.value;
value %= MOD;
return *this;
}
ModInt& operator/=(ModInt x) {
x.invert();
return *this *= x;
}
ModInt operator+(ModInt x) const {
ModInt o = *this;
o += x;
return o;
}
ModInt operator-(ModInt x) const {
ModInt o = *this;
o -= x;
return o;
}
ModInt operator*(ModInt x) const {
ModInt o = *this;
o *= x;
return o;
}
ModInt operator/(ModInt x) const {
ModInt o = *this;
o /= x;
return o;
}
bool operator==(ModInt x) const { return value == x.value; }
bool operator!=(ModInt x) const { return !(*this == x); }
ModInt operator-() const { return ModInt(0) - *this; }
ModInt operator^(long long x) const {
ModInt ret = 1 % MOD;
ModInt mul = *this;
while (x) {
if (x & 1) ret *= mul;
mul *= mul;
x >>= 1;
}
return ret;
}
ModInt& operator^=(long long x) { return *this = *this ^ x; }
private:
void invert() { *this ^= MOD - 2; }
public:
void answer() { std::cout << value << std::endl; }
};
const int MX = 405;
vector<int> E[MX];
int D[MX][MX];
int n, m;
template <typename T>
void uniquify(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
vector<pair<int, double>> process(vector<pair<int, double>>& v) {
vector<pair<int, double>> ret;
sort(v.begin(), v.end());
for (auto& pr : (v)) {
if (ret.empty() || pr.first != ret.back().first) {
ret.push_back(pr);
} else {
ret.back().second += pr.second;
}
}
return ret;
}
double dpr[MX];
int stamp[MX];
double f(int x) {
double base = 1.0 / n;
vector<double> pdist(n);
vector<int> countAt(n);
for (int i = (0); i < int(n); ++i) pdist[D[x][i]] += base;
for (int i = (0); i < int(n); ++i) ++countAt[D[x][i]];
vector<vector<pair<int, double>>> possLoc(n);
for (int i = (0); i < int(n); ++i)
for (auto& o : (E[i])) possLoc[D[x][i]].push_back({o, base / E[i].size()});
double tot = 0;
for (int i = (0); i < int(n); ++i) {
if (countAt[i] == 0) continue;
double catchChance = 1.0 / countAt[i];
possLoc[i] = process(possLoc[i]);
for (auto& pr : (possLoc[i])) pr.second /= pdist[i];
for (int j = (0); j < int(n); ++j) stamp[j] = -1;
for (int j = (0); j < int(n); ++j) {
double ccatch = 0;
for (auto& pr : (possLoc[i])) {
auto& q = dpr[D[j][pr.first]];
if (stamp[D[j][pr.first]] != j) {
stamp[D[j][pr.first]] = j;
q = 0;
}
auto r = pr.second;
if (r > q) {
ccatch += r - q;
q = r;
}
}
catchChance = max(catchChance, ccatch);
}
tot += pdist[i] * catchChance;
}
return tot;
}
int main() {
for (int i = (0); i < int(MX); ++i)
for (int j = (0); j < int(MX); ++j) D[i][j] = ((int)1e9 + 10);
readInteger(n);
readInteger(m);
for (int i = (0); i < int(n); ++i) D[i][i] = 0;
for (int k = (0); k < int(m); ++k) {
int a, b;
readInteger(a);
readInteger(b);
--a;
--b;
D[a][b] = min(D[a][b], 1);
D[b][a] = min(D[b][a], 1);
E[a].push_back(b);
E[b].push_back(a);
}
for (int i = (0); i < int(n); ++i)
for (int j = (0); j < int(n); ++j)
for (int k = (0); k < int(n); ++k)
D[j][k] = min(D[j][k], D[j][i] + D[i][k]);
double ans = 0;
for (int i = (0); i < int(n); ++i) ans = max(ans, f(i));
printf("%.10f\n", ans);
}
| 10 | CPP |
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 9 07:48:54 2019
@author: LV
"""
for i in range(int(input())):
s = input()
print([s, s[0] + str(len(s) - 2) + s[-1]][len(s) > 10]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 103;
int n, m, k;
bool cantCheckRow[N];
bool isLocked[N];
int vec[N];
int arr[N][N];
int fre[N];
int main() {
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) scanf("%d", &arr[i][j]);
for (int col = 0; col < m; ++col) {
memset(fre, 0, sizeof(fre));
for (int row = 0; row < n; ++row) {
if (cantCheckRow[row]) continue;
if (arr[row][col] == 0) continue;
++fre[arr[row][col]];
}
for (int i = 0; i < N; ++i) isLocked[i] |= (fre[i] >= 2);
for (int row = 0; row < n; ++row) {
if (cantCheckRow[row]) continue;
if (arr[row][col] == 0) continue;
if (isLocked[arr[row][col]]) {
vec[row] = col + 1;
cantCheckRow[row] = true;
}
}
}
for (int i = 0; i < n; ++i) printf("%d\n", vec[i]);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, s1, s2, t, t1, t2;
cin >> s1 >> s2 >> t1 >> t2;
swap(s2[0], s2[1]);
swap(t2[0], t2[1]);
s = s1 + s2;
t = t1 + t2;
s.erase(s.find('X'), 1);
t.erase(t.find('X'), 1);
s = s + s;
cout << (s.find(t) != -1 ? "YES" : "NO");
return 0;
}
| 7 | CPP |
#include<iostream>
using namespace std;
int main(){
int a, b, k;
cin >>a>>b>>k;
for(int i=0; i<k; i++){
if(i%2==0){
if(a%2==1)
a--;
int h =a/2;
a-=h;
b+=h;
}
else{
if(b%2==1)
b--;
int h=b/2;
a+=h;
b-=h;
}
}
cout<<a<<" "<<b<<endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 500100;
string second[maxn];
int dp[30][30];
int ans = 0;
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> second[i];
}
for (int i = 0; i < n; i++) {
int cl = second[i].size();
for (int j = 0; j < 26; j++) {
if (dp[j][second[i][0] - 'a'])
dp[j][second[i][cl - 1] - 'a'] =
max(dp[j][second[i][cl - 1] - 'a'], dp[j][second[i][0] - 'a'] + cl);
}
dp[second[i][0] - 'a'][second[i][cl - 1] - 'a'] =
max(dp[second[i][0] - 'a'][second[i][cl - 1] - 'a'], cl);
}
for (int i = 0; i < 26; i++) {
ans = max(ans, dp[i][i]);
}
cout << ans << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long a[52], ans[52];
int main() {
cin >> n >> k;
a[0] = a[1] = 1;
a[2] = 2;
for (long long i = 3; i <= n; i++) a[i] = a[i - 1] + a[i - 2];
long long pos = 1;
while (pos <= n) {
if (k <= a[n - pos]) {
ans[pos] = pos;
pos++;
} else {
ans[pos] = pos + 1;
ans[pos + 1] = pos;
k -= a[n - pos];
pos += 2;
}
}
for (long long i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
bool count(long long n, vector<long long> &v) {
long long add = 0;
for (long long i = 0; i < n; i++) {
if (v[i] >= n) {
add += v[i] / n;
v[i] = (v[i] % n) - v[i] / n;
}
}
for (long long i = 0; i < n; i++) {
v[i] += add;
}
ans += add;
return add;
}
int main() {
long long n; cin >> n;
vector<long long> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
while (count(n, v));
cout << ans << endl;
return 0;
} | 0 | CPP |
s = " " + input()
dp = [0] * len(s)
for i in range(1, len(s)) :
sum = 0
for j in range(i, 0, -1) :
sum += int(s[j])
if(sum % 3 == 0) :
dp[i] = max(dp[j - 1] + 1, 1)
break
dp[i] = max(dp[i], dp[i - 1])
print(dp[-1])
| 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
#ifdef Adrian
#include "debug.h"
#else
#define debug(...) 9999
#endif
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef complex<int> point;
#define F first
#define S second
#define ii pair<ll,ll>
typedef pair<ll, ii> iii;
int main() {
#ifdef Adrian
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n, m;
cin>>n>>m;
vector<vector<ii>> g(n);
for(int i=0; i<m; i++)
{
int x, y, z;
cin>>x>>y>>z;
--x;--y;
g[x].push_back({y, z});
g[y].push_back({x, z});
}
const ll oo = 1e18;
vector<vector<ll>> d(n,
vector<ll>(51, oo));
d[0][0] = 0;
priority_queue<iii> pq;
pq.push({0, {0, 0}});
while(!pq.empty())
{
ll w = -pq.top().F;
int c = pq.top().S.F;
int l = pq.top().S.S;
pq.pop();
if(l == 0)
{
for(auto v:g[c])
if(w < d[v.F][v.S])
{
d[v.F][v.S] = w;
pq.push({-d[v.F][v.S], {v.F, v.S}});
}
} else
{
for(auto v:g[c])
{
ll t = l + v.S;
t *= t;
if(w + t < d[v.F][0])
{
d[v.F][0] = w + t;
pq.push({-d[v.F][0], {v.F, 0}});
}
}
}
}
for(int i=0; i<n; i++)
{
if(d[i][0] < oo) cout<<d[i][0];
else cout<<-1;
cout<<" \n"[i == n - 1];
}
return 0;
}
| 11 | CPP |
N = int(input())
M = ["S", "H", "C", "D"]
sr = set()
for _ in range(N):
s, r = input().split()
sr.add(s+r)
for m in M:
for i in range(1, 14):
mi = m+str(i)
if mi not in sr:
print(m, i)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, g[160], f[160][160][160], a[160];
char ch[160];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
scanf("%s", ch + 1);
memset(f, -63, sizeof(f));
for (int i = 1; i <= n; ++i) f[i + 1][i][0] = 0, f[i][i][1] = 0;
for (int j = 1; j <= n; ++j)
for (int i = j; i >= 1; --i)
for (int k = 0; k <= j - i + 1; ++k) {
if (k >= 2 && ch[i] == ch[j])
f[i][j][k] = max(f[i][j][k], f[i + 1][j - 1][k - 2]);
for (int l = i; l < j; ++l) {
f[i][j][k] = max(f[i][j][k], f[i][l][k] + f[l + 1][j][0]);
f[i][j][k] = max(f[i][j][k], f[i][l][0] + f[l + 1][j][k]);
}
if (a[k] != -1) f[i][j][0] = max(f[i][j][0], f[i][j][k] + a[k]);
}
for (int i = 1; i <= n; ++i) {
g[i] = g[i - 1];
for (int j = 1; j <= i; ++j) g[i] = max(g[i], g[j - 1] + f[j][i][0]);
}
printf("%d\n", g[n]);
return 0;
}
| 10 | CPP |
#include<bits/stdc++.h>
using namespace std;
int n;
int a,b,c;
int ans[100002];
vector<pair<int,int> >vec[100002];
int slv(int now,int col){
ans[now]=col;
for(int i=0;i<vec[now].size();i++){
int next=vec[now][i].first , dir=vec[now][i].second;
if(ans[next]!=-1)continue;
if(dir%2==1){
slv(next,col^1);
}
else{
slv(next,col);
}
}
return(0);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)ans[i]=-1;
for(int i=1;i<n;i++){
cin>>a>>b>>c;
vec[a].push_back(make_pair(b,c));
vec[b].push_back(make_pair(a,c));
}
slv(1,0);
for(int i=1;i<=n;i++){
cout<<ans[i]<<endl;
}
return(0);
} | 0 | CPP |
n = int(input())
coins = [int(x) for x in input().split()]
coins.sort(reverse=True)
half = sum(coins) // 2;
added = 0
counter = 0
for i in coins:
added += i
counter += 1
if added > half:
print(counter)
break | 7 | PYTHON3 |
t = int(input())
for i in range(t):
s = input()
if len(s) > 10:
print(f"{s[0]}{len(s)-2}{s[-1]}")
else:
print(s)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int mp_open[500005] = {0}, mp_close[500005] = {0}, khali = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int ans = 0;
int n;
cin >> n;
string x;
stack<char> st1;
for (int i = 0; i < n; ++i) {
cin >> x;
int l = x.length();
for (int i = 0; i < l; ++i) {
if (st1.size() == 0)
st1.push(x[i]);
else if (st1.top() == '(' && x[i] == ')') {
st1.pop();
} else
st1.push(x[i]);
}
int ctropen = 0, ctrclose = 0;
while (!st1.empty()) {
if (st1.top() == '(')
ctropen++;
else
ctrclose++;
st1.pop();
}
if (ctropen > 0 && ctrclose > 0) continue;
if (ctropen > 0) {
if (mp_close[ctropen] > 0) {
mp_close[ctropen]--;
ans++;
} else
mp_open[ctropen]++;
}
if (ctrclose > 0) {
if (mp_open[ctrclose] > 0) {
mp_open[ctrclose]--;
ans++;
} else
mp_close[ctrclose]++;
}
if (ctropen == 0 && ctrclose == 0) khali++;
}
ans = ans + khali / 2;
cout << ans << '\n';
return 0;
}
| 9 | CPP |
from sys import stdin
z=[]
a=int(stdin.readline());gh={}
k=list(map(int,stdin.readline().split()))
i=0;_i,_o=0,0
while i<a:
while i<a:
if k[i] not in gh:
if k[i]<0:
if -k[i] not in gh:exit(print(-1))
_o+=1;gh[k[i]]=1
else:
gh[k[i]]=1
_i+=1
else:exit(print(-1))
i+=1
if _i==_o:z.append(len(gh));_i,_o=0,0;gh={};break
if _i!=_o:exit(print(-1))
print(len(z))
print(*z) | 8 | PYTHON3 |
n = int(input())
b = list(map(int, input().split()))
a = [0]
last = 0
min_value = 0
for x in b:
last += x
a.append(last)
if last < min_value:
min_value = last
for i in range(len(a)):
a[i] += -min_value + 1
temp = a.copy()
temp.sort()
check = True
for index, value in enumerate(temp):
if index > 0 and value != temp[index - 1] + 1:
check = False
break
if check:
for x in a:
print(x, end=' ')
else:
print(-1)
| 9 | PYTHON3 |
import sys
import bisect
from collections import deque
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
n = int(ri())
a = Ri()
print(n+1)
for i in range(n-1,-1,-1):
temp = a[i]%n
dif = -1
if i > temp:
dif = i-temp
else:
dif = n-(temp-i)
print(1,i+1,dif)
for j in range(i):
a[j] = a[j]+dif
print(2,n,n)
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t;
scanf("%d", &n);
int r = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%d", &t);
if (i == j) r ^= t;
}
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; ++i) {
scanf("%d", &t);
if (t == 3)
printf("%d", r);
else {
scanf("%d", &t);
r = 1 - r;
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
string str;
int main() {
while (cin >> str) {
if (str[0] == 's') {
int x, y;
for (x = 0, y = 1;; x = y, y *= 2) {
cout << '?' << " " << x << " " << y << endl;
cin >> str;
if (str == "x") break;
}
while (x < y) {
int mid = (x + y) >> 1;
cout << '?' << " " << mid << " " << y << endl;
fflush(stdout);
cin >> str;
if (mid + 1 == y) {
cout << "! ";
if (str == "x")
cout << y << endl;
else
cout << mid << endl;
break;
}
if (str == "x") {
x = mid;
}
if (str == "y") {
y = mid;
}
}
}
}
return 0;
}
| 10 | CPP |
from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value(): return tuple(map(int, input().split())) # multiple values
def arr(): return [int(i) for i in input().split()] # array input
def sarr(): return [int(i) for i in input()] #array from string
def starr(): return [str(x) for x in input().split()] #string array
def inn(): return int(input()) # integer input
def svalue(): return tuple(map(str, input().split())) #multiple string values
def parr(): return [(value()) for i in range(n)] # array of pairs
mo = 1000000007
# ----------------------------CODE------------------------------#
for _ in range(inn()):
p=input()
h=input()
d=defaultdict(int)
for i in p:
d[i]+=1
p=sorted(p)
ans='NO'
n=len(p)
for i in range(len(h)):
if(h[i] in d):
s=h[i:i+n]
s=sorted(s)
if(s==p):
ans='YES'
break
print(ans)
| 7 | PYTHON3 |
from sys import stdin
n,m,k=map(int,stdin.readline().split())
l=[]
for i in range(m+1):
l.append(int(stdin.readline().rstrip()))
c=0
for i in range(m):
p=l[i]^l[m]
b=bin(p).replace("0b","")
if b.count("1")<=k:
c+=1
print(c) | 8 | PYTHON3 |
n=int(input())
pO=list(map(int,input().split(" ")))
result=0.0;i=0
while(i<n):
result+=(pO[i]/100)
i+=1
print('{0:.12f}'.format((result/n)*100))
| 8 | PYTHON3 |
for _ in range(int(input())):
a,b,n=map(int,input().split())
s=c=f=0
while(a<=n):
s=min(a,b)
c=max(a,b)
b=c
a=c+s
f+=1
print(f) | 7 | PYTHON3 |
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX_N = 2e5 + 6;
vector<int> v[MAX_N];
int main() {
int n,m;
scanf("%d %d",&n,&m);
for (int i=1;n>=i;i++) {
int x,y;
scanf("%d %d",&x,&y);
v[x].push_back(y);
}
for (int i=0;m+1>=i;i++) {
sort(v[i].begin(),v[i].end());
}
priority_queue<int,vector<int>,greater<int> > pq;
int nxt=0;
for (int i=0;m>=i;i++) {
if (i==0) {
for (int j:v[i]) {
pq.push(j);
}
}
else {
if (v[i].size() == 0) {
if (!pq.empty() && pq.top()<=i) pq.pop();
else {
if (nxt < i) nxt=i;
while (v[nxt].size() == 0 && nxt<=m+1) nxt++;
if (v[nxt].size() == 0) continue;
else v[nxt].pop_back();
}
}
else {
for (int j=0;v[i].size()-1>j;j++) {
pq.push(v[i][j]);
}
}
}
}
printf("%d\n",pq.size());
}
| 0 | CPP |
#include<iostream>
using namespace std;
int main()
{
int n,m,t,i;
cin >> n >> m;
if(n%2==1){
for(i=1;i<=m;i++)
cout << i << " " << n-i << endl;
}else{
t=n/2;
for(i=1;i<=t/2&&m>0;i++,m--)
cout << i << " " << t+1-i << endl;
for(i=1;i<=(t-1)/2&&m>0;i++,m--)
cout << t+i << " " << n-i << endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long fact[1000001] = {0};
void preFact() {
fact[0] = 1;
fact[1] = 1;
for (long long i = 2; i < 1000001; i++) {
fact[i] = (fact[i - 1] * (i % 1000003)) % 1000003;
}
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long setBitNumber(long long n) {
if (n == 0) return 0;
long long msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return (msb);
}
long long binaryExpon(long long a, long long b) {
long long ans = 1;
while (b > 0) {
if (b % 2 == 1) {
ans *= (a % 1000003) % 1000003;
ans = ans % 1000003;
}
a *= (a % 1000003) % 1000003;
a = a % 1000003;
b = b >> 1;
}
return ans % 1000003;
}
long long nCr(long long n, long long r) {
if (n < r) {
return 0;
} else {
long long ans = fact[n];
ans = ans * (binaryExpon(fact[r], (1000003 - 2)) % 1000003) % 1000003;
ans = ans * (binaryExpon(fact[n - r], (1000003 - 2)) % 1000003) % 1000003;
return ans;
}
}
long long __gcd(long long a, long long b) {
if (b == 0) {
return a;
}
return __gcd(a % b, b);
}
void solve() {
long long p;
cin >> p;
;
long long q;
cin >> q;
;
if (p % q != 0 || p < q) {
cout << p << "\n";
;
return;
} else {
vector<long long> factors;
long long qq = q;
for (long long i = 2; i * i <= q; i++) {
if (qq % i == 0) {
factors.push_back(i);
while (qq % i == 0) {
qq /= i;
}
}
}
if (qq > 1) {
factors.push_back(qq);
}
long long finalans = -1;
long long curr;
for (auto x : factors) {
long long pp = p;
while (pp % q == 0) {
pp /= x;
}
finalans = ((finalans > pp) ? finalans : pp);
}
cout << finalans << "\n";
;
}
}
int main() {
ios_base::sync_with_stdio(false);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
cin.tie(0);
cout.tie(0);
preFact();
long long t;
cin >> t;
;
while (t--) {
solve();
}
}
| 7 | CPP |
for _ in range(int(input())):
# n=int(input())
n,q=map(int,input().split())
l=list(map(int,input().split()))
ans=0
f=0
ind=0
if(n==1):
print(l[0])
else:
while(ind<n-1):
if(f==0):
while(ind<n-1 and l[ind+1]>=l[ind]):
ind+=1
ans+=l[ind]
f=1
if(ind==n-1):
break
else:
while(ind<n-1 and l[ind+1]<=l[ind]):
ind+=1
f=0
if(ind==n-1):
break
else:
ans-=l[ind]
print(ans)
| 9 | PYTHON3 |
try:
t=int(input())
for _ in range(t):
n=int(input())
if n==1:
print(0)
else:
start=8
if n==3:
print(start)
else:
i=5
mul=2
while i<=n:
curr=2*i+2*(i-2)
start=start+curr*mul
mul=mul+1
i=i+2
print(start)
except:
pass
| 9 | PYTHON3 |
n = int(input())
h = [[] for _ in range(2)]
h[0] = list(map(int, input().split()))
h[1] = list(map(int, input().split()))
h[0].insert(0, 0)
h[1].insert(0, 0)
dp = [[0 for __ in range(2)] for _ in range(n + 1)]
best_edge = [-1] * (n + 1)
dp[0][0] = h[0][0]
dp[0][1] = h[1][0]
dp[1][0] = dp[0][0] + h[0][1]
dp[1][1] = dp[0][1] + h[1][1]
for i in range(2, n + 1):
dp[i][0] = max(dp[i-1][1] + h[0][i], dp[i-2][1] + h[0][i])
dp[i][1] = max(dp[i-1][0] + h[1][i], dp[i-2][0] + h[1][i])
print(max(dp[n]))
| 9 | PYTHON3 |
t=int(input())
for _ in range(t):
x,n,m=map(int,input().split())
c=0
while x>20 and c<n:
x=(x//2)+10
#print(x)
c+=1
#print(x,"p")
s=0
while x>0 and s<m:
x=x-10
#print(x)
s+=1
if x<=0:
print("YES")
else:
print("NO")
| 8 | PYTHON3 |
n=int(input())
for i in range(n):
x=input()
y=input()
x=x[::-1]
y=y[::-1]
q=y.find('1')
k=x[q:].find('1')
print(k) | 7 | PYTHON3 |
Subsets and Splits