solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
int t, n, m;
vector<pair<int, int> > graph[102];
int dp[102][102][27];
int dfs(int u, int v, int c) {
if (dp[u][v][c] != -1) return dp[u][v][c];
for (pair<int, int> e : graph[u]) {
if (e.second >= c) {
if (!dfs(v, e.first, e.second)) {
return dp[u][v][c] = 1;
}
}
}
return dp[u][v][c] = 0;
}
void solve() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int c = 0; c < 26; c++) {
dp[i][j][c] = -1;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << (dfs(i, j, 0) == 1 ? "A" : "B");
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
int u, v;
char w;
for (int i = 0; i < m; i++) {
cin >> u;
cin >> v;
cin >> w;
graph[u].push_back(make_pair(v, w - 'a'));
}
solve();
return 0;
}
| 1,700 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e5 + 5;
inline long long power(long long a, long long b) {
long long x = 1;
a = a % mod;
while (b) {
if (b & 1) x = (x * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return x;
}
inline long long inv(long long a) { return power(a, mod - 2); }
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int n, m;
string a[20];
int sum[20][2];
int dp[20][2];
void solve() {
int i, j, flag = 0;
i = n - 1;
while (i > -1 && sum[i][1] == -1) {
i--;
flag++;
}
if (i == -1) return;
dp[i][1] = sum[i][1] + flag;
dp[i][0] = sum[i][1] + sum[i][1] - sum[i][0] + flag;
flag = 0;
for (i = i - 1; i > -1; i--) {
if (sum[i][1] == -1) {
flag++;
continue;
}
dp[i][1] =
min(dp[i + 1 + flag][1] + m + 1 - sum[i + 1 + flag][1] + 1 + flag + m +
1 - sum[i][0] + sum[i][1] - sum[i][0],
dp[i + 1 + flag][1] + sum[i + 1 + flag][1] + 1 + flag + sum[i][1]);
dp[i][1] = min(dp[i][1], dp[i + 1 + flag][0] + sum[i + 1 + flag][0] + 1 +
flag + sum[i][1]);
dp[i][1] =
min(dp[i][1], dp[i + 1 + flag][0] + m + 1 - sum[i + 1 + flag][0] + 1 +
flag + m + 1 - sum[i][0] + sum[i][1] - sum[i][0]);
dp[i][0] = min(dp[i + 1 + flag][1] + m + 1 - sum[i + 1 + flag][1] + 1 +
flag + m + 1 - sum[i][0],
dp[i + 1 + flag][1] + sum[i + flag + 1][1] + flag + 1 +
sum[i][1] + sum[i][1] - sum[i][0]);
dp[i][0] = min(dp[i][0], dp[i + 1 + flag][0] + sum[i + 1 + flag][0] + 1 +
flag + sum[i][1] + sum[i][1] - sum[i][0]);
dp[i][0] =
min(dp[i][0], dp[i + 1 + flag][0] + m + 1 - sum[i + 1 + flag][0] + 1 +
flag + m + 1 - sum[i][0]);
flag = 0;
}
}
int main() {
ios_base::sync_with_stdio(false);
int t = 1;
while (t--) {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0;
int x = 0, y = 0, i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m + 2; j++) {
if (a[i][j] == '1') break;
}
sum[i][0] = j;
for (j = m + 1; j > -1; j--) {
if (a[i][j] == '1') break;
}
sum[i][1] = j;
}
solve();
i = 0;
while (i < n && sum[i][0] == m + 2) i++;
if (i == n)
cout << "0\n";
else
cout << min(dp[i][0], dp[i][1]) << endl;
}
return 0;
}
| 1,600 |
CPP
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("popcnt")
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(2 * n);
vector<int> stck;
for (int i = 0; i < 2 * n; i++) {
char sym;
cin >> sym;
if (sym == '+') {
stck.push_back(i);
} else {
cin >> a[i];
if (stck.empty()) {
cout << "NO"
<< "\n";
return 0;
}
a[stck.back()] = a[i];
a[i] *= -1;
stck.pop_back();
}
}
set<int> myset;
for (int i = 0; i < 2 * n; i++) {
if (a[i] > 0) {
myset.insert(a[i]);
} else {
if (*myset.begin() == -a[i]) {
myset.erase(*myset.begin());
} else {
cout << "NO"
<< "\n";
return 0;
}
}
}
cout << "YES"
<< "\n";
for (int i = 0; i < 2 * n; i++) {
if (a[i] > 0) cout << a[i] << " ";
}
cout << "\n";
}
| 1,700 |
CPP
|
for _ in [0]*int(input()):
n = int(input())
s = list(map(int, input().split()))
max_len = [1]*n
for i in range(n):
mul = 2*(i+1)
while mul <= n:
if s[mul-1] > s[i]:
max_len[mul-1] = max(max_len[mul-1], max_len[i] +1)
mul += (i+1)
print(max(max_len))
| 1,400 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T& x) {
short f = 1;
char ch = getchar();
x = 0;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= f;
}
long long head[1000010], ver[1000010], Next[1000010], edge[1000010], tot;
void add(long long x, long long y, long long z) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
edge[tot] = z;
}
long long n, m;
long long x, y, z;
long long d[1000010];
bool v[1000010];
priority_queue<pair<long long, long long> > q;
void DJ(long long x) {
memset(v, 0, sizeof v);
memset(d, 0x3f, sizeof d);
d[x] = 0;
q.push(make_pair(0, x));
while (!q.empty()) {
x = q.top().second, q.pop();
if (v[x]) continue;
v[x] = 1;
for (long long i = head[x]; i; i = Next[i]) {
long long y = ver[i], z = edge[i];
if (d[y] > d[x] + z) {
d[y] = d[x] + z;
q.push(make_pair(-d[y], y));
}
}
}
}
signed main() {
read(n), read(m);
while (m--) {
read(x), read(y), read(z);
z <<= 1ll;
add(x, y, z), add(y, x, z);
}
for (long long i = 1; i <= n; i++) {
read(x);
add(0, i, x);
}
DJ(0);
for (long long i = 1; i <= n; i++) printf("%lld ", d[i]);
}
| 2,000 |
CPP
|
for i in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
print(n if a.count(a[0])==n else 1)
| 800 |
PYTHON3
|
n = int(input())
drinks = list(map(int, input().split()))
print(round(sum(drinks) / n, 10))
| 800 |
PYTHON3
|
n = eval(input())
for i in range(n):
ans = ''
k =''
s = input().split(':')
c = 0
m = 0
for g in range(len(s)):
if s[g] != '':
c += 1
if c == 8:
for j in range(c):
s[j] = (4-len(s[j]))*'0' + s[j]
ans += s[j] +':'
else:
k = (8-c)*'0000:'
for j in range(len(s)):
if s[j] == '':
ans += k
k = ''
m = 1
else:
s[j] = (4-len(s[j]))*'0' + s[j]
ans += s[j] +':'
ans += k
ans = ans[:-1]
print(ans)
| 1,500 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, hmin = INT_MAX, hmax = 0, bmin = INT_MAX, bmax = 0, c = 0;
char arr[100][100];
cin >> n >> m;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) cin >> arr[i][j];
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr[i][j] == 'B') {
c++;
if (i < hmin) {
hmin = i;
}
if (i > hmax) {
hmax = i;
}
if (j < bmin) {
bmin = j;
}
if (j > bmax) {
bmax = j;
}
}
}
}
int max, min;
if (hmax - hmin > bmax - bmin)
max = hmax - hmin + 1;
else
max = bmax - bmin + 1;
if (n > m)
min = m;
else
min = n;
if (c == 0)
cout << "1" << endl;
else {
if (min < max)
cout << "-1" << endl;
else
cout << max * max - c << endl;
}
return 0;
}
| 1,300 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000;
const int INF = 0x3f3f3f3f;
bool occur[20];
int main(int argc, char const *argv[]) {
long long n, k;
while (cin >> n >> k) {
memset(occur, 0, sizeof occur);
while (n--) {
long long te, sum = 0, digit = 1;
for (long long i = 1; i <= k; i++) {
scanf("%lld", &te), sum += te * digit;
digit *= 2;
}
occur[sum] = 1;
}
long long lim = 1;
for (long long i = 1; i <= k; ++i) lim *= 2;
bool find = 0;
if (occur[0] == 1) find = 1;
for (int i = 0; i < lim; ++i) {
if (occur[i])
for (int j = i + 1; j < lim; ++j)
if (occur[j] && (i & j) == 0) find = 1;
}
if (find)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
| 1,500 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > g[511];
bool del[511] = {0};
int main() {
int N, m;
cin >> N >> m;
double ans = 0;
int x[N];
for (int i = 0; i < N; i++) cin >> x[i];
int u, v, l;
for (int i = 0; i < m; i++) {
cin >> u >> v >> l;
u--;
v--;
ans = max(ans, (double)(x[u] + x[v]) / (double)l);
}
printf("%.10f", ans);
}
| 1,600 |
CPP
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int INF = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 1e6 + 7;
signed main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a == c || b == d) {
cout << abs(a - c) + abs(b - d) << endl;
} else {
cout << abs(a - c) + abs(b - d) + 2 << endl;
}
}
return 0;
}
| 800 |
CPP
|
def foo():
n,m = map(int,input().split());
a = list(map(int,input().split()))
b = list(map(int,input().split()))
for e in a:
if e in b:
print("YES")
print(1, end=" ")
print(e)
return
print("NO")
t = int(input())
for _ in range(t):
foo()
| 800 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double n, m;
vector<double> C(5);
cin >> n >> m;
for (int i = 1; i <= 5; i++) {
int z = 5 - i;
C[i - 1] = 0;
if (z == 0) z = 5;
for (double j = z; j <= m; j += 5) {
C[i - 1]++;
}
}
long long R = 0;
for (double i = 1; i <= n; i++) {
if (i > 5) {
i = 1;
n -= 5;
}
R += C[i - 1];
}
cout << R << endl;
return 0;
}
| 1,100 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
class edge {
public:
int s, e, l, r;
edge(int s1 = 0, int e1 = 0, int l1 = 0, int r1 = 0) {
s = s1, e = e1, l = l1, r = r1;
}
bool operator<(const edge &t) const { return l > t.l; }
} E[2000010];
vector<edge> g[1000010];
int n, m, s[500010], e[500010], l[500010], r[500010];
priority_queue<edge> q;
int L[1000010];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
scanf("%d%d%d%d", &s[i], &e[i], &l[i], &r[i]), r[i]--;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= m; i++) {
E[i * 4 - 3] = edge(s[i] * 2 - 1, e[i] * 2, l[i] + (l[i] % 2 == 0),
r[i] - (r[i] % 2 == 0));
E[i * 4 - 2] =
edge(e[i] * 2, s[i] * 2 - 1, l[i] + l[i] % 2, r[i] - r[i] % 2);
E[i * 4 - 1] =
edge(s[i] * 2, e[i] * 2 - 1, l[i] + l[i] % 2, r[i] - r[i] % 2);
E[i * 4] = edge(e[i] * 2 - 1, s[i] * 2, l[i] + (l[i] % 2 == 0),
r[i] - (r[i] % 2 == 0));
}
for (int i = 1; i <= m * 4; i++) q.push(E[i]);
memset(L, 255, sizeof(L));
L[2] = 0;
while (!q.empty()) {
edge fr = q.top();
q.pop();
if (fr.l > fr.r) continue;
if (L[fr.s] >= fr.l) {
if (fr.e == n * 2 || fr.e == n * 2 - 1) {
printf("%d\n", fr.l + 1);
return 0;
}
L[fr.e] = max(L[fr.e], fr.r + 1);
for (edge x : g[fr.e]) {
edge tmp = x;
tmp.l = fr.l + 1;
q.push(tmp);
}
g[fr.e].clear();
} else
g[fr.s].push_back(fr);
}
puts("-1");
return 0;
}
| 3,200 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
void coutv(vector<long long> v) {
for (auto it : v) cout << it << " ";
cout << endl;
}
void couts(set<int> v) {
for (auto it : v) cout << it << " ";
cout << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
bool f = false;
int n;
cin >> n;
vector<long long> v(n), ans(n);
set<int> s;
for (auto &i : v) cin >> i;
for (int i = 1; i <= n; i++) s.insert(i);
ans[0] = v[0];
s.erase(v[0]);
for (int i = 1; i < n; i++) {
if (v[i] == v[i - 1]) {
if (*s.begin() > v[i]) {
f = true;
break;
}
ans[i] = *s.begin();
s.erase(s.begin());
} else {
ans[i] = v[i];
s.erase(v[i]);
}
}
if (f)
cout << "-1\n";
else
coutv(ans);
}
}
| 1,200 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int n;
map<int, int> mp;
int x, ans;
int a[400086];
int sum;
vector<int> v;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
mp[x]++;
}
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
a[it->second]++;
}
for (int i = n; i; i--) a[i] += a[i + 1];
sum = n;
for (int i = n; i; i--) {
sum -= a[i + 1];
if (sum / i >= i) {
if (sum / i * i > ans) {
ans = sum / i * i;
x = i;
}
}
}
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
if (it->second < x) continue;
for (int i = 1; i <= it->second && v.size() < ans && i <= x; i++)
v.push_back(it->first);
if (v.size() == ans) break;
}
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
if (it->second >= x) continue;
for (int i = 1; i <= it->second && v.size() < ans && i <= x; i++)
v.push_back(it->first);
if (v.size() == ans) break;
}
printf("%d\n%d %d\n", ans, x, ans / x);
for (int i = 1; i <= x; i++) {
int now = i, val = i;
for (int j = 1; j <= ans / x; j++) {
a[now] = v[val - 1];
now++;
if (now > ans / x) now = 1;
val += x;
}
for (int j = 1; j <= ans / x; j++) printf("%d ", a[j]);
puts("");
}
}
| 2,300 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int p[200005], s[200005], a[200005], b[200005];
map<int, vector<int> > mp;
vector<int> v[200005];
struct sss {
int id, s;
} ss[200005];
bool cmp(sss x, sss y) { return x.s < y.s; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, sr = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> p[i];
mp[p[i]].push_back(i);
}
for (int i = 0; i < m; i++) {
cin >> ss[i].s;
ss[i].id = i;
}
sort(ss, ss + m, cmp);
int c = 0, u = 0;
for (int i = 0; i < m; i++) {
int x = ss[i].s, y = 0, z;
while (x) {
if (mp[x].size()) {
a[ss[i].id] = y;
b[mp[x].back()] = ss[i].id + 1;
c++;
u += y;
mp[x].pop_back();
break;
}
if (x == 1) break;
y++;
x = (x + 1) / 2;
}
}
cout << c << ' ' << u << endl;
for (int i = 0; i < m - 1; i++) cout << a[i] << ' ';
cout << a[m - 1] << endl;
for (int i = 0; i < n - 1; i++) cout << b[i] << ' ';
cout << b[n - 1] << endl;
}
| 2,100 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 100;
long long n, m, x, y, cnt, vis, s;
pair<long long, long long> p[N];
vector<long long> v, ans;
bool check() {
cnt = 0;
for (int i = 1; i < m; i++) cnt += p[i].first;
if ((p[0].first > cnt && cnt * 2 + 1 < n) || cnt + p[0].first < n ||
(n % 2 == 1 && m == 2))
return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
long long nn = n;
for (int i = 0; i < m; i++) {
cin >> p[i].first;
p[i].second = i + 1;
}
sort(p, p + m, greater<pair<int, int>>());
if (!check()) return cout << -1, 0;
if (p[m - 1].first * 2 > n && n % 2 == 1)
v.push_back(p[1].second), p[1].first--;
else
v.push_back(p[m - 1].second), p[m - 1].first--;
n--, cnt = 0, y = m - 1;
while (n) {
n--, cnt++;
if (cnt % 2 == 1) {
if (p[x].first == 0) x++;
if (x != y) v.push_back(p[x].second), p[x].first--;
} else {
if (p[y].first == 0) y--;
if (x != y) v.push_back(p[y].second), p[y].first--;
}
}
if (v.size() != nn) {
for (int i = 0; i < m; i++)
if (p[i].first != 0) vis = i;
if (v[v.size() - 1] != p[vis].second)
v.push_back(p[vis].second), p[vis].first--;
if (p[vis].first) {
for (auto u : v) {
if (s % 2 == 1 && p[vis].first)
ans.push_back(p[vis].second), p[vis].first--;
ans.push_back(u), s++;
}
for (int i = 0; i < nn; i++) cout << ans[i] << " ";
return 0;
}
}
for (int i = 0; i < nn; i++) cout << v[i] << " ";
return 0;
}
| 2,100 |
CPP
|
n,m=map(int,input().split())
a=0
if n>=m:
two=int(n/2)
if n%2==0: one=0
else: one=1
if (two+one)%m==0:
a=two+one
else:
nn=0
for x in range(two):
nn+=1
if (two+one+nn)%m==0:
a=two+one+nn
break
print(a) if a>0 else print(-1)
| 1,000 |
PYTHON3
|
#import sys
#sys.stdin = open('a_in.txt')
T = int(input())
for _ in range(T):
n, a, b = map(int, input().split())
white, black = map(int, input().split())
if a + b < white * 2 or (2 * n - 2 * black) < a + b:
print("NO")
elif a + b == white * 2 or (2 * black) == (2 * n - a - b):
if (a % 2) == (b % 2):
print("YES")
else:
print("NO")
else:
print("YES")
| 800 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, M = 1e4 + 5, OO = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
long long pre[N];
int main() {
string s;
cin >> s;
int m;
cin >> m;
while (m--) {
int x;
cin >> x;
pre[x - 1]++;
pre[s.length() - x]++;
}
partial_sum(pre, pre + N, pre);
for (int i = 0; i < ((s.length() + 1) / 2); ++i) {
if (pre[i] % 2) swap(s[i], s[(s.length() - (i + 1))]);
}
cout << s << endl;
return 0;
}
| 1,400 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int dp[40005][31];
bool vis[40005][31];
int a, b;
long long n;
inline long long qpow(int a, int b) {
long long res = 1ll, tp = a;
while (b) {
if (b & 1) {
res *= tp;
}
tp *= tp;
b >>= 1;
}
return res;
}
int cal(int a, int b) {
if (qpow(a, b) >= n) {
return 1;
}
if (a == 1 && qpow(2, b) >= n) return 2;
if (b == 1 && qpow(a, 2) >= n) {
long long t = n - ((long long)(a));
if (t & 1ll) return 0;
return 1;
}
if (vis[a][b]) return dp[a][b];
vis[a][b] = 1;
if (cal(a + 1, b) == 0) {
return dp[a][b] = 1;
}
if (cal(a, b + 1) == 0) {
return dp[a][b] = 1;
}
if (cal(a + 1, b) == 2) {
return dp[a][b] = 2;
}
if (cal(a, b + 1) == 2) {
return dp[a][b] = 2;
}
return dp[a][b] = 0;
}
int main() {
scanf("%d%d%lld", &a, &b, &n);
int tp = cal(a, b);
if (tp == 0) {
puts("Stas");
return 0;
}
if (tp == 1) {
puts("Masha");
return 0;
}
if (tp == 2) {
puts("Missing");
return 0;
}
}
| 2,000 |
CPP
|
for _ in range(int(input())):
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=a.index(min(a));s=0
for i in range(n):
if i==b or a[i]>=k:
continue
s+=abs(k-a[i])//a[b]
print(s)
| 800 |
PYTHON3
|
import sys
def check(a):
temp = inp[0]
for i in a:
if i=='1': temp*=2
if i=='2': temp=temp*10+1
if temp == inp[1]:
return True
else:
return False
def printt(a):
print('YES')
print(len(a)+1)
temp = inp[0]
print(temp,end=' ')
for i in a:
if i=='1': temp*=2
if i=='2': temp=temp*10+1
print(temp,end=' ')
def solve(a):
if check(a):
printt(a)
sys.exit()
if h(a)>inp[1]: return
solve(a+'1')
solve(a+'2')
def h(a):
temp = inp[0]
for i in a:
if i=='1': temp*=2
if i=='2': temp=temp*10+1
return temp
inp = list(map(int,input().split()))
solve('')
print('NO')
| 1,000 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
long long calc(long long num) {
string numb = to_string(num);
for (long long i = 0; i < numb.size(); i++) {
num -= numb[i] - '0';
}
return num;
}
long long LB(long long st, long long en, long long S, long long siz) {
if (st > en) {
if (st <= siz) return st;
return 0;
}
long long real_mid = st + (en - st) / 2;
long long middle = calc(real_mid);
if (S > middle) return LB(real_mid + 1, en, S, siz);
if (S <= middle) return LB(st, real_mid - 1, S, siz);
return 0;
}
void init() {
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(0);
}
int main() {
init();
long long n = 0, q = 0;
cin >> n >> q;
long long res = LB(1, n, q, n);
if (!res)
cout << 0 << endl;
else
cout << n + 1 - res << endl;
return 0;
}
| 1,600 |
CPP
|
t = int(input())
for loop in range(t):
n = int(input())
s = input()
lis = []
for k in range(1,n+1):
temp = list(s[:k-1])
if (n-k+1) % 2 == 1:
temp.reverse()
ns = s[k-1:] + "".join(temp)
lis.append([ns,k])
lis.sort()
print (lis[0][0])
print (lis[0][1])
| 1,400 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
long long int n;
cin >> n;
if (n % 2 != 0) {
cout << 0 << endl;
} else {
cout << (1 << n / 2) << endl;
}
return 0;
}
| 1,000 |
CPP
|
# problem/791/A
import math
a, b = [float(num) for num in input().split(" ")]
i = math.ceil(math.log(a/b, 2./3))
if a*(3**i) == b*(2**i):
print(i + 1)
else:
print(i)
| 800 |
PYTHON3
|
[N,K] = list(map(int,input().split()))
ids = set()
vals = []
IDS = list(map(int,input().split()))
for id_ in IDS:
if not id_ in ids:
vals.append(id_)
ids.add(id_)
if len(vals) > K:
ids.remove(vals[0])
vals = vals[1:]
print(len(vals))
print(" ".join([str(x) for x in vals[::-1]]))
| 1,000 |
PYTHON3
|
n=int(input())
l=list(map(int,input().split()))
ll=[0]
for i in range(1,n+1):
ll.append(ll[i-1]+l[i-1])
mx=ll[-1]
cou=0
for i in range(n+1):
if(i<n):
if(l[i]==0):
cou+=1
for j in range(i+1,n+1):
#print(ll[-1]-(ll[j]-ll[i])+(j-i-(ll[j]-ll[i])),end=" ")
if(ll[-1]-(ll[j]-ll[i])+(j-i-(ll[j]-ll[i]))>mx):
mx=ll[-1]-(ll[j]-ll[i])+(j-i-(ll[j]-ll[i]))
if(cou==0):
print(n-1)
else:
print(mx)
| 1,200 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void init_ios() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
const int N = (int)1e5 + 10;
int n, ile[N];
bool vis[N], flag = true;
char z[N];
void dfs(int v) {
vis[v] = true;
if (z[v] == '>')
v += ile[v];
else
v -= ile[v];
if (v < 1 || v > n) {
flag = false;
return;
}
if (!vis[v]) dfs(v);
}
int main() {
init_ios();
cin >> n;
for (int i = 1; i <= n; ++i) cin >> z[i];
for (int i = 1; i <= n; ++i) cin >> ile[i];
dfs(1);
if (flag)
cout << "INFINITE\n";
else
cout << "FINITE\n";
}
| 1,000 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string w = "hello", s;
cin >> s;
int j = 0;
for (int i = 0; i < int(s.size()); ++i)
if (s[i] == w[j]) ++j;
cout << (j == 5 ? "YES" : "NO") << endl;
return 0;
}
| 1,000 |
CPP
|
#include <bits/stdc++.h>
const int maxn = 1e5 + 10;
using namespace std;
int n, pre, suf;
string S, T;
int main() {
cin >> n;
cin >> S >> T;
for (int i = 0; i < n; i++) {
if (S[i] != T[i]) {
pre = i;
break;
}
}
for (int i = 0; i < n; i++) {
if (S[n - 1 - i] != T[n - 1 - i]) {
suf = n - 1 - i;
break;
}
}
int len = suf - pre;
if (suf == pre || (S.substr(pre, len) == T.substr(pre + 1, len) &&
S.substr(pre + 1, len) == T.substr(pre, len))) {
cout << "2\n";
return 0;
}
if (S.substr(pre, len) == T.substr(pre + 1, len) ||
S.substr(pre + 1, len) == T.substr(pre, len)) {
cout << "1\n";
return 0;
}
cout << "0\n";
}
| 1,800 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 200000;
const double EPS = 1e-9;
int n;
int d[2000][2000];
string a, b;
char buf[2000];
bool win(char a, char b) {
if (a == 'R' && b == 'S') return true;
if (a == 'S' && b == 'P') return true;
if (a == 'P' && b == 'R') return true;
return false;
}
int main() {
scanf("%d\n", &n);
gets(buf);
a = buf;
gets(buf);
b = buf;
memset(d, -1, sizeof d);
int i = 0, j = 0, cnt1 = 0, cnt2 = 0, h = 0;
for (; h < n && d[i][j] == -1; h++) {
d[i][j] = 1;
if (win(a[i], b[j])) cnt1++;
if (win(b[j], a[i])) cnt2++;
i = (i + 1) % (int)(a).size();
j = (j + 1) % (int)(b).size();
}
if (h < n) {
int mul = n / h;
cnt1 *= mul;
cnt2 *= mul;
for (int k = 0; k < int(n % h); k++) {
if (win(a[i], b[j])) cnt1++;
if (win(b[j], a[i])) cnt2++;
i = (i + 1) % (int)(a).size();
j = (j + 1) % (int)(b).size();
}
}
cout << cnt2 << " " << cnt1 << endl;
return 0;
}
| 1,300 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
long long k, l, r, p;
long long bspow(long long a, long long b, long long p) {
long long res = 1 % p;
a %= p;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
long long div(long long a, long long b, long long p) {
return a * bspow(b, p - 2, p) % p;
}
long long pt(long long a, long long b, long long p) {
if (a % p == 0) return 0;
return bspow(a, bspow(2, b, p - 1), p);
}
long long sum(long long a, long long b, long long p) {
if (b == 0) return 1;
if ((a - 1) % p == 0) return bspow(2, b, p);
return div((pt(a, b, p) + p - 1) % p, (a + p - 1) % p, p);
}
long long solve() {
if (k == 1) return 2 % p;
if (k % 2 == 0) return sum(pt(k, l, p), r - l + 1, p);
if (p == 2) return 0;
return div(sum(pt(k, l, p), r - l + 1, p), bspow(2, r - l, p), p);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
printf("%I64d\n", solve());
}
}
| 2,600 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 405;
vector<long long int> adj[maxn];
int vis[maxn], used;
int dfs(int s, vector<int> &path) {
path.push_back(s);
vis[s] = 1;
int ans = 1;
for (auto x : adj[s]) {
if (!vis[x]) {
ans += dfs(x, path);
}
}
return ans;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> on, to, th;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
vector<int> path;
int sz = dfs(i, path);
if (sz > 3) {
cout << -1;
return 0;
}
if (sz == 1) on.push_back(path[0]);
if (sz == 2) {
to.push_back(path[0]);
to.push_back(path[1]);
}
if (sz == 3) {
th.push_back(path[0]);
th.push_back(path[1]);
th.push_back(path[2]);
}
}
}
int g = on.size(), h = to.size();
if (g < h / 2) {
cout << -1;
return 0;
}
for (int i = 0; i < th.size(); i++) {
cout << th[i] << ' ';
if (i % 3 == 2) cout << endl;
}
int k = 0;
for (int i = 0; i < h; i++) {
cout << to[i] << ' ';
if (i % 2 == 1) {
cout << on[k++] << endl;
}
}
int x = 0;
while (k < g) {
if (x != 0 && x % 3 == 0) cout << endl;
cout << on[k] << ' ';
k++;
x++;
}
return 0;
}
| 1,500 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<char> s(n + 1), t(n + 1);
scanf("%s", &s[0]);
scanf("%s", &t[0]);
long long ans = 0;
long long cnt_a = 0;
long long cnt_b = 0;
bool found_diff = false;
bool big_num = false;
for (int i = 0; i < n; ++i) {
if (found_diff && !big_num) {
cnt_a = cnt_a * 2 + (s[i] == 'a' ? 1 : 0);
cnt_b = cnt_b * 2 + (t[i] == 'b' ? 1 : 0);
}
if (s[i] != t[i]) {
found_diff = true;
}
if (found_diff) {
ans += min(cnt_a + cnt_b + 2, (long long)k);
if (cnt_a + cnt_b + 2 >= k) {
big_num = true;
}
} else {
++ans;
}
}
printf("%lld\n", ans);
return 0;
}
| 2,000 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x = 0;
cin >> n;
if (n >= 100) x += n / 100;
n = n % 100;
if (n >= 20) x += n / 20;
n = n % 20;
if (n >= 10) x += n / 10;
n = n % 10;
if (n >= 5) x += n / 5;
n = n % 5;
if (n >= 1) x += n / 1;
n = n % 1;
cout << x << endl;
return 0;
}
| 800 |
CPP
|
n,h,a,b,k=map(int,input().split())
for i in range(k):
ans=0
ta,fa,tb,fb=map(int,input().split())
ans+=abs(ta-tb)+abs(fa-fb)
if ta!=tb:
if fa>b and fb>b:ans+=min(fa-b,fb-b)*2
if fa<a and fb<a:ans+=min(a-fa,a-fb)*2
print(ans)
| 1,000 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int color[100005];
int edge[100005][2];
vector<int> adj[100005];
bool visited[100005];
bool dfs(int c, int now) {
if (color[now] != c) return false;
for (int i = 0; i < adj[now].size(); i++) {
if (visited[adj[now][i]]) continue;
visited[adj[now][i]] = true;
if (!dfs(c, adj[now][i])) return false;
visited[adj[now][i]] = false;
}
return true;
}
int main() {
int n, i;
bool isPossible;
scanf("%d", &n);
for (i = 1; i <= n - 1; i++) {
scanf("%d %d", &edge[i][0], &edge[i][1]);
adj[edge[i][0]].push_back(edge[i][1]);
adj[edge[i][1]].push_back(edge[i][0]);
}
for (i = 1; i <= n; i++) {
scanf("%d", &color[i]);
}
for (i = 1; i <= n - 1; i++) {
if (color[edge[i][0]] != color[edge[i][1]]) break;
}
if (i > n - 1) {
printf("YES\n1");
return 0;
}
int root1 = edge[i][0], root2 = edge[i][1];
visited[root1] = true;
isPossible = true;
for (i = 0; i < adj[root1].size(); i++) {
visited[adj[root1][i]] = true;
if (!dfs(color[adj[root1][i]], adj[root1][i])) isPossible = false;
visited[adj[root1][i]] = false;
}
if (isPossible) {
printf("YES\n%d", root1);
return 0;
}
for (int i = 0; i <= n; i++) visited[i] = false;
visited[root2] = true;
isPossible = true;
for (i = 0; i < adj[root2].size(); i++) {
visited[adj[root2][i]] = true;
if (!dfs(color[adj[root2][i]], adj[root2][i])) isPossible = false;
visited[adj[root2][i]] = false;
}
if (isPossible) {
printf("YES\n%d", root2);
return 0;
}
printf("NO");
return 0;
}
| 1,600 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
long long int lcm(long long int a, long long int b) {
long long int c = a * b;
while (a != b) {
if (a > b)
a = a - b;
else
b = b - a;
}
return c / a;
}
int main() {
long long int n, sum = 0, max = 0, p, done = 0;
cin >> n;
if (n == 1 || n == 2) {
cout << n;
return 0;
}
if (n == 3) {
cout << 6;
return 0;
}
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= 3; j++) {
for (int k = 0; k <= 3; k++) {
sum = lcm((n - i), lcm((n - j), (n - k)));
if (i != j && j != k && i != k && max < sum) {
max = sum;
}
}
}
}
cout << max;
}
| 1,600 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long int t;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<string> v(n);
long long int tot = 0;
for (long long int i = 0; i < n; i++) tot += (s[i] == '1');
long long int ans = tot;
for (long long int i = 0; i < k; i++) {
long long int d = 0;
for (long long int j = i; j < n; j += k) {
if (s[j] == '1')
d++;
else
d--;
d = max(d, (long long)0);
ans = min(ans, tot - d);
}
}
cout << ans << endl;
}
}
| 1,900 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int inf = 1e9;
int n, m;
int a[maxn];
int dp[maxn][5][5];
int f(int x, int c1, int c2) {
if (x == m + 1) {
if (c1 == 0 && c2 == 0) return 0;
return -inf;
}
if (!dp[x][c1][c2]) {
dp[x][c1][c2] = -inf;
for (int i = 0; i <= 2; i++) {
if (a[x] - c1 - c2 - i >= 0)
dp[x][c1][c2] =
max(dp[x][c1][c2], f(x + 1, c2, i) + i + (a[x] - c1 - c2 - i) / 3);
}
}
return dp[x][c1][c2];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
a[x]++;
}
printf("%d", f(1, 0, 0));
}
| 2,200 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k, A, B;
cin >> n >> k >> A >> B;
if (k == 1) {
cout << A * (n - 1);
return 0;
}
long long ans = 0;
while (n != 1) {
if (n < k) {
cout << ans + A * (n - 1);
return 0;
}
ans += A * (n % k);
n -= n % k;
ans += min(B, A * (n - n / k));
n /= k;
}
cout << ans;
return 0;
}
| 1,400 |
CPP
|
arr=[ list( input() ) for _ in range (8) ]
cw=0
cb=0
flag = True
for i in range(0,8):
for j in range(1,8):
if ((arr[i][j]) == arr[i][j-1]) :
flag =False
if (flag == True):
print("YES")
elif(flag == False):
print("NO")
| 1,000 |
PYTHON3
|
import math
n = int(input())
string = ""
for i in range(1, 1000):
string += str(i)
print(string[n-1])
| 800 |
PYTHON3
|
#include <bits/stdc++.h>
const int z = 111111;
const double eps = 1e-9;
const int inf = int(1e9 + 7);
const long long llinf = int(1e18);
using namespace std;
bool palindrom(string& t, int n) {
for (int j = 0; j + j < n; j++) {
if (t[j] != t[n - j - 1]) {
return false;
}
}
return true;
}
bool check(string& s, int i, int n) {
string t = s.substr(i) + s.substr(0, i);
return palindrom(t, n) && s != t;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
string s;
cin >> s;
int n = s.size();
bool same = true;
for (int i = 1; i + i + 1 < n; i++) {
if (s[i] != s[0]) {
same = false;
break;
}
}
if (same) {
cout << "Impossible";
return 0;
}
for (int i = 1; i + i <= n; i++) {
if (check(s, i, n)) {
cout << 1;
return 0;
}
}
cout << 2;
return 0;
}
| 1,800 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
string s;
struct node {
node *list[26];
node() {
for (int i = 0; i < (int)26; i++) list[i] = NULL;
}
} * root;
void add() {
node *cur = root;
for (int i = 0; i < (int)s.size(); i++) {
int a = s[i] - 'a';
if (cur->list[a] == NULL) cur->list[a] = new node();
cur = cur->list[a];
}
}
bool dfs1(node *cur) {
bool ret = false;
for (int i = 0; i < (int)26; i++) {
if (cur->list[i] == NULL) continue;
ret |= (!dfs1(cur->list[i]));
}
return ret;
}
bool dfs2(node *cur) {
bool ret = false, fl = true;
for (int i = 0; i < (int)26; i++)
if (cur->list[i] != NULL) {
fl = false;
break;
}
if (fl) return true;
for (int i = 0; i < (int)26; i++) {
if (cur->list[i] == NULL) continue;
ret |= (!dfs2(cur->list[i]));
}
return ret;
}
int main() {
root = new node();
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < (int)n; i++) {
cin >> s;
add();
}
bool win = dfs1(root), lose = dfs2(root);
if (lose & win)
cout << "First" << endl;
else if (!win)
cout << "Second" << endl;
else {
if (k % 2)
cout << "First" << endl;
else
cout << "Second" << endl;
}
return 0;
}
| 1,900 |
CPP
|
n=int(input())
s=[int(x) for x in input().split()]
G=sorted(s)
c=0
for i in range(0,len(G),2):
c=c+abs(G[i+1]-G[i])
print(c)
"""L=[0]*105
G=[]
for i in range(0,len(s)):
if(L[s[i]]%2==0):
L[s[i]]=1
else:
L[s[i]]=L[s[i]]+1
for i in range(0,len(L)):
if(L[i]%2==0):
L[i]=0
for i in range(0,len(L)):
if(L[i]%2!=0):
G.append(i)
print(G)
c=0
for i in range(0,len(G),2):
c=c+abs(G[i+1]-G[i])
print(c)"""
| 800 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, h, a, sum = 0;
cin >> n;
cin >> h;
for (int i = 0; i < n; i++) {
cin >> a;
if (a <= h)
sum += 1;
else
sum += 2;
}
cout << sum;
return 0;
}
| 800 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
set<long long int> se;
for (long long int i = 1; i <= n; i++) {
if (i * i > n) break;
se.insert(i * i);
if (i * i * i <= n) se.insert(i * i * i);
}
cout << se.size() << endl;
}
int main() {
std::ios::sync_with_stdio(false);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
| 800 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
string s;
cin >> s;
long long int j = 0;
long long int n = s.size();
for (long long int i = 0; i < n; i++) {
if (s[i] == 'N') j++;
}
if (j == 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
| 800 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005;
char s[maxn];
char t[maxn];
stack<int> st;
int l[maxn], r[maxn];
int fa[maxn];
int rep[maxn];
int find(int x) { return fa[x] = (fa[x] == x ? x : find(fa[x])); }
int main() {
int n, m, p;
scanf("%d %d %d", &n, &m, &p);
scanf("%s", s);
scanf("%s", t);
for (int i = 0; i <= n; i++) {
fa[i] = i;
}
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
st.push(i);
} else {
rep[i] = st.top();
rep[st.top()] = i;
st.pop();
}
}
p--;
for (int i = 0; i < m; i++) {
if (t[i] == 'L') {
p--;
p = find(p);
} else if (t[i] == 'R') {
p++;
p = find(p);
} else {
int x = min(p, rep[p]);
int y = max(p, rep[p]);
fa[x] = y + 1;
fa[y] = x - 1;
p = find(y + 1);
if (p >= n) p = find(x - 1);
}
}
for (int i = find(0); i < n; i++, i = find(i)) {
printf("%c", s[i]);
}
puts("");
return 0;
}
| 1,700 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(20);
long long int n, h;
cin >> n >> h;
long long int sh = (h * (h + 1)) / 2;
long long int pp = (-1 + sqrt(1 + 8 * n));
if (pp <= 2 * h) {
long long int ans = 0;
long long int remain = n;
long long int kk = sqrt(n);
while (kk * (kk + 1) <= 2 * n) kk++;
kk--;
ans += kk;
remain -= (kk * (kk + 1)) / 2;
if (remain) {
ans++;
}
cout << ans;
} else if (n >= sh && n <= sh + 2 * h) {
long long int ans = h;
long long int remain = n - sh;
if (remain) {
ans += (remain / h);
remain %= h;
}
if (remain) ans += 1;
cout << ans << endl;
} else {
long long int ans = h + 1;
long long int remain = n - sh - h;
;
long long int kk =
(-1 - 2 * h + sqrt((1 + 2 * h) * (1 + 2 * h) + 4 * remain)) / 2;
;
ans += 2 * kk;
remain -= (2 * kk * h + kk * (kk + 1));
if (remain) {
ans += (remain / (h + kk + 1));
remain -= (remain / (h + kk + 1)) * (h + kk + 1);
}
if (remain) ans++;
cout << ans << endl;
};
return 0;
}
| 2,100 |
CPP
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
namespace IO_Optimization {
inline int read() {
int X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) {
X = (X << 3) + (X << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -X : X;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
} // namespace IO_Optimization
using namespace IO_Optimization;
int n, k;
double sum;
struct Node {
int num, pos;
} a[2000 + 2], b[2000 + 2];
struct Trans {
int s[2000 + 2];
} ans[2000 + 2];
int tot1, tot2;
inline bool cmp(Node x, Node y) { return x.num > y.num; }
int main() {
n = read(), k = read();
for (register int i = 1; i <= n; ++i) {
int x = read(), y = read();
if (y == 1)
a[++tot1].num = x, a[tot1].pos = i;
else
b[++tot2].num = x, b[tot2].pos = i;
}
sort(a + 1, a + 1 + tot1, cmp);
int noww = k - 1 < tot1 ? k - 1 : tot1;
for (register int i = 1; i <= noww; ++i) {
ans[i].s[++ans[i].s[0]] = a[i].pos;
sum += a[i].num * 0.5;
}
if (tot1 <= k - 1) {
for (register int i = 1; i <= tot2; ++i) {
int nowww = tot1 + i < k ? tot1 + i : k;
sum += b[i].num;
ans[nowww].s[++ans[nowww].s[0]] = b[i].pos;
}
} else {
int minn = 1e9;
for (register int i = k; i <= tot1; ++i) {
sum += a[i].num;
ans[k].s[++ans[k].s[0]] = a[i].pos;
minn = a[i].num < minn ? a[i].num : minn;
}
for (register int i = 1; i <= tot2; ++i) {
sum += b[i].num;
ans[k].s[++ans[k].s[0]] = b[i].pos;
minn = b[i].num < minn ? b[i].num : minn;
}
sum -= minn * 0.5;
}
printf("%.1lf\n", sum);
for (register int i = 1; i <= k; ++i, puts("")) {
write(ans[i].s[0]);
cout << " ";
for (register int j = 1; j <= ans[i].s[0]; ++j) {
write(ans[i].s[j]);
cout << " ";
}
}
return 0;
}
| 1,700 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int a[100010], b[100010];
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < m; ++i) {
cin >> b[i];
}
int ans = 0, tmp1 = 0, tmp2 = 0, cnt1 = 0, cnt2 = 0;
while (1) {
if (cnt1 == n && cnt2 == m) break;
if (tmp1 < tmp2) {
tmp1 += a[cnt1++];
} else {
tmp2 += b[cnt2++];
}
if (tmp1 == tmp2) {
++ans;
tmp1 = 0;
tmp2 = 0;
}
}
cout << ans << endl;
}
return 0;
}
| 1,100 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
long long n, a, k, b;
int main() {
cin >> n >> k;
a = 10e9;
b = 10e9;
for (long long i = k - 1; i > 0; i--) {
if (n % i == 0) {
if (a >= n / i) {
a = n / i;
b = i;
}
}
}
cout << a * k + b;
return 0;
}
| 1,100 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 10;
long long res;
long long a[N];
long long d, x, y;
void extendedEuclid(long long A, long long B) {
if (B == 0) {
d = A;
x = 1;
y = 0;
} else {
extendedEuclid(B, A % B);
long long temp = x;
x = y;
y = temp - (A / B) * y;
}
}
void solve() {
long long A, B, C;
cin >> A >> B >> C;
C = -1 * C;
extendedEuclid(A, B);
if (C % d != 0) {
cout << -1;
} else {
cout << x * C / d << " " << y * C / d;
}
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 1,800 |
CPP
|
def check_diverse_string(input_str):
input_str = sorted(input_str)
D = dict()
for i in input_str:
if i in D:
return "NO"
else:
D[i] = ord(i)
cor_seq = list(range(ord(input_str[0]), ord(input_str[0]) + len(input_str)))
l = []
for k,v in D.items():
l.append(v)
if l == cor_seq:
return "YES"
else:
return "NO"
n = int(input())
I = []
for i in range(n):
k=input()
I.append(k)
for i in range(n):
print(check_diverse_string(I[i]))
| 800 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, t, k, i, j, s[3005], x, y, l[3005], r[3005], to[3005],
val[3005], ans, tmp;
vector<long long> pos[3005];
void ins(long long &x, long long y) {
r[x] = y;
l[y] = x;
x = y;
}
void shan(long long x) {
l[r[x]] = l[x];
r[l[x]] = r[x];
}
int main() {
scanf("%I64d%I64d%I64d%I64d", &n, &m, &t, &k);
for ((i) = 1; (i) <= (t); (i)++) {
scanf("%I64d%I64d", &x, &y);
pos[x].push_back(y);
}
for ((i) = 1; (i) <= (n); (i)++) {
for ((j) = (i); (j) <= (n); (j)++)
for (__typeof((pos[j]).begin()) it = (pos[j]).begin();
it != (pos[j]).end(); it++)
s[*it]++;
tmp = 0;
long long lst = 0;
for ((j) = 1; (j) <= (m); (j)++)
if (s[j]) ins(lst, j);
ins(lst, m + 1);
long long p = 0, sum = 0;
for (j = r[0]; j <= m; j = r[j]) {
while (sum < k && p <= m) {
p = r[p];
sum += s[p];
}
tmp += (j - l[j]) * (m - p + 1);
to[j] = p;
val[j] = sum;
sum -= s[j];
}
for ((j) = (n); (j) >= (i); (j)--) {
ans += tmp;
for (__typeof((pos[j]).begin()) it = (pos[j]).begin();
it != (pos[j]).end(); it++) {
s[*it]--;
long long p = *it, sum = 0;
while (p && sum + s[l[p]] < k) {
p = l[p];
sum += s[p];
}
while (p <= *it) {
if ((--val[p]) < k && to[p] <= m) {
tmp -= (p - l[p]) * (r[to[p]] - to[p]);
to[p] = r[to[p]];
val[p] += s[to[p]];
}
p = r[p];
}
if (!s[*it]) shan(*it);
}
}
}
cout << ans << endl;
return 0;
}
| 3,000 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n;
long long a[3];
long long t[N];
bool used[N];
priority_queue<pair<long long, int> > q01, q[3];
int res = 0;
int get(int x) {
int res = -1;
while (!q[x].empty()) {
int u = q[x].top().second;
if (used[u]) {
q[x].pop();
continue;
}
res = u;
break;
}
return res;
}
void getboth() {
while (!q01.empty()) {
int u = q01.top().second;
if (used[u]) {
q01.pop();
continue;
}
used[u] = 1;
break;
}
}
void go() {
int f0 = get(0), f1 = get(1);
int rem = -1;
if (f0 == -1 || f1 == -1) {
getboth();
return;
}
if (f0 == f1) {
rem = f0;
used[f0] = 1;
f1 = get(1);
if (f1 == -1) {
q[1].push(make_pair(t[rem], rem));
used[rem] = 0;
getboth();
} else {
used[f1] = 1;
}
} else {
used[f0] = 1;
used[f1] = 1;
}
}
int main() {
scanf("%d", &n);
scanf("%I64d %I64d %I64d", a, a + 1, a + 2);
for (int i = 1; i <= n; i++) {
scanf("%I64d", t + i);
}
sort(t + 1, t + n + 1);
sort(a, a + 3);
if (t[n] > a[0] + a[1] + a[2]) {
cout << -1;
return 0;
}
for (int i = 1; i <= n; i++) {
if (t[i] <= a[0]) {
q[0].push(make_pair(t[i], i));
}
if (t[i] <= a[1]) {
q[1].push(make_pair(t[i], i));
}
if (t[i] <= a[2]) {
q[2].push(make_pair(t[i], i));
}
if (t[i] <= a[0] + a[1]) {
q01.push(make_pair(t[i], i));
}
}
for (int i = n; i; i--) {
if (used[i]) {
continue;
}
res++;
used[i] = 1;
if (a[2] >= t[i]) {
int pos = 2;
if (a[1] >= t[i]) {
pos = 1;
}
if (a[0] >= t[i]) {
pos = 0;
}
if (pos == 2) {
go();
} else {
for (int i = 2; i >= 0; i--) {
int f = get(i);
if (i != pos && f != -1) {
used[f] = 1;
}
}
}
continue;
}
if (a[2] + a[1] >= t[i]) {
int pos = 0;
if (a[0] + a[2] >= t[i]) {
pos = 1;
}
if (a[0] + a[1] >= t[i]) {
pos = 2;
}
int f = get(pos);
if (f != -1) {
used[f] = 1;
}
continue;
}
}
cout << res << endl;
return 0;
}
| 2,400 |
CPP
|
h = int(input())
for i in range(h):
n = int(input())
e1 = 0
e0 = 0
n = list(map(int, input().split()))
for i in range(len(n)):
if n[i] == 1:
e1 += 1
else:
e0 += 1
if e0 >= e1:
print(e0)
for i in range(e0):
print(0, end=" ")
print()
else:
if e1%2 == 0:
print(e1)
for i in range(e1):
print(1, end=" ")
print()
else:
print(e1 - 1)
for i in range(e1-1):
print(1, end=" ")
print()
| 1,100 |
PYTHON3
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
sum1=10
currnum=0
ans=[10]
mod=998244353
n=int(input())
for i in range(1,n):
#print(sum1,10**(i+1)*(i+1))
currnum=(9*i+10)*pow(10,i,mod)-sum1
sum1+=currnum
ans.append(currnum%mod)
print(" ".join(str(x) for x in ans[::-1]))
| 1,800 |
PYTHON3
|
n=int(input())
a=[*map(int,input().split())]
c=0
for i in range(1,n):
j=a.index(a[i-1],i);c+=j-i;del a[j]
print(c)
| 1,400 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[10100];
int x;
int ans;
int solve(int x) {
int ans = 0;
for (int i = 0; i < n; i++)
if (x & (1 << i)) ans++;
return ans;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; i++) scanf("%d", &a[i]);
scanf("%d", &x);
for (int i = 0; i < m; i++) {
int y = x ^ a[i];
if (solve(y) <= k) ans++;
}
printf("%d\n", ans);
return 0;
}
| 1,100 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cout.precision(12);
cin.tie(0);
int n;
cin >> n;
unordered_map<int, pair<int, int> > vals;
vals.reserve(2 * n);
for (int i = 0; i < n; ++i) {
int f, b;
cin >> f >> b;
vals[f].first++;
if (f != b) {
vals[b].second++;
}
}
int best = n + 1;
for (auto& i : vals) {
if (i.second.first + i.second.second >= (n + 1) / 2) {
best = min(best, max(0, (n + 1) / 2 - i.second.first));
}
}
if (best > n) {
best = -1;
}
cout << best << "\n";
return 0;
}
| 1,500 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int64_t n, e, o, ans, a[300001], sm[300001], cnt[300001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int64_t i = 1; i <= n; ++i) {
cin >> a[i];
sm[i] = (sm[i - 1] ^ a[i]);
if (__builtin_popcountll(sm[i]) % 2 == 0)
++e;
else
++o;
}
for (int64_t i = 1; i <= n; ++i) {
cnt[i] = __builtin_popcountll(a[i]);
ans += e;
if (cnt[i] & 1) swap(e, o);
--e;
}
for (int64_t i = 1; i <= n; ++i) {
int64_t sm = 0, mx = 0, temp;
for (int64_t j = i; j <= n; ++j) {
mx = max(mx, cnt[j]);
sm += cnt[j];
if (sm % 2 == 0 && 2 * mx > sm) --ans;
if (sm > 125) break;
}
}
cout << ans;
}
| 2,000 |
CPP
|
def counting(s,t):
n = len(s)
n1 = len(t)
count = 0
for i in range(n-n1+1):
if s[i:i+n1] == t:
count += 1
return count
for i in range(int(input())):
n = int(input())
s = input()
c1 = counting(s,'11')
c2 = counting(s,'00')
print(max(c1,c2))
| 1,200 |
PYTHON3
|
test_cases = int(input())
for i in range(test_cases) :
current_vol, desired_vol = map(int, input().split())
change_in_volume = abs(current_vol - desired_vol)
presses = change_in_volume // 5
change_in_volume = change_in_volume % 5
presses += change_in_volume // 2
change_in_volume = change_in_volume % 2
presses += change_in_volume // 1
print(presses)
"""
while change_in_volume != 0 :
if change_in_volume >= 5 :
change_in_volume -= 5
elif change_in_volume >= 2 :
change_in_volume -= 2
else :
change_in_volume -= 1
presses += 1
print(presses)
"""
| 800 |
PYTHON3
|
a, b = [str(x) for x in input().split()]
for i in range(int(b)):
if a[-1] != "0":
a = f"{(int(a) - 1)}"
elif a[-1] == "0":
a = f"{(int(a) // 10)}"
print(a)
| 800 |
PYTHON3
|
inf = 10 ** 10
def solve():
n = int(input())
food = list(map(int, input().split()))
food.sort()
dp = [[inf]*(n+100) for i in range(n+1)]
dp[0][0] = 0
for i, v in enumerate(food):
for k in range(n+100):
for m in range(k):
dp[i+1][k] = min(dp[i+1][k], dp[i][m] + abs(k - v))
ans = min(dp[n][j] for j in range(n+100))
print(ans)
return
def main():
t = int(input())
for i in range(t):
solve()
return
if __name__ == "__main__":
main()
| 1,800 |
PYTHON3
|
l1=[]
n=int(input())
l=list(map(int,input().split()))
for i in range(2*n):
l1.append([l[i],i])
l1.sort()
sd,dd=l1[0][1],l1[1][1]
for i in range(2,2*n):
if i%2:
dd+=abs(l1[i][1]-l1[i-2][1])
else:
sd+=abs(l1[i][1]-l1[i-2][1])
print(sd+dd)
| 1,200 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
char s[105][10005];
int main() {
ios::sync_with_stdio(false);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", s[i]);
int x = 0, y = 0;
int dir = 1;
long long ans = 0;
while (true) {
if (x == n - 1) {
cout << ans << endl;
return 0;
}
if (s[x + 1][y] == '.') {
x++;
ans++;
continue;
}
int ind[2] = {y - 1, y + 1};
for (int trial = 0; trial < 2 * m; trial++) {
if (s[x + 1][y] == '.') break;
if (ind[dir] >= 0 && ind[dir] < m) {
int cur = ind[dir];
if (s[x][cur] == '.') {
y = ind[dir];
ind[dir] += dir == 1 ? 1 : -1;
ans++;
} else if (s[x][cur] == '+') {
s[x][cur] = '.';
ind[dir] += dir == 1 ? 1 : -1;
ans++;
dir ^= 1;
int ny = ind[dir];
ny -= dir == 1 ? 1 : -1;
ans += abs(y - ny);
y = ny;
} else {
ans++;
dir ^= 1;
int ny = ind[dir];
ny -= dir == 1 ? 1 : -1;
ans += abs(y - ny);
y = ny;
}
} else {
ans++;
dir ^= 1;
int ny = ind[dir];
ny -= dir == 1 ? 1 : -1;
ans += abs(y - ny);
y = ny;
}
}
if (s[x + 1][y] == '.')
x++, ans++;
else {
cout << "Never" << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
| 2,000 |
CPP
|
n = int(input())
data = list(map(int, input().split()))
for i in range(n):
data[i] = [data[i], i + 1]
data.sort()
ans = 0
for i in range(1, n):
ans += abs(data[i][1] - data[i - 1][1])
print(ans)
| 1,200 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0, b = 0, ax = 0, i = 0, tol = 0, v[128] = {0};
while (scanf("%d%d", &n, &b) == 2) {
ax = 0;
for (i = 0; i < n; i++) {
scanf("%d", v + i);
if (v[i] > ax) {
ax = v[i];
}
}
tol = 0;
for (i = 0; i < n; i++) {
tol += ax - v[i];
}
if (tol > b) {
printf("-1\n");
} else {
for (i = 0; i < n; i++) {
printf("%.12lf\n", ax - v[i] + (b - tol) * 1. / n);
}
}
}
return 0;
}
| 1,100 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
const int N = 403600;
int n, s, first, a[N];
int pt[N], q[N];
pair<int, int> ans[N];
int hd[N] = {0}, ne[N], v[N];
int main() {
scanf("%d%d", &n, &s);
for (int i = (0); i < (int)(n); i++) {
scanf("%d", &first);
v[i + 1] = i;
ne[i + 1] = hd[first];
hd[first] = i + 1;
}
memset(pt, -1, sizeof(pt));
int m = 0;
for (int i = N - 1; i >= 0; i--) {
for (int j = hd[i]; j; j = ne[j]) {
a[m] = i;
q[m++] = v[j];
}
pt[i] = m - 1;
}
int cur = N - 1, na = 0;
for (int i = (0); i < (int)(n); i++) {
cur = a[i];
int bdd = i, last = pt[cur];
while (a[i]--) {
bool up = 0;
if (pt[cur] <= bdd) bdd = last;
while (cur > 0 && pt[cur] <= bdd) --cur, up = 1;
if (up) {
bdd = last;
last = pt[cur];
}
if (cur == 0) {
break;
}
ans[na++] = pair<int, int>(q[i] + 1, q[pt[cur]] + 1);
a[pt[cur]]--;
pt[cur]--;
}
}
if (na * 2 == s) {
cout << "Yes" << endl;
printf("%d\n", na);
for (int i = (0); i < (int)(na); i++)
printf("%d %d\n", ans[i].first, ans[i].second);
} else {
cout << "No" << endl;
}
return 0;
}
| 2,200 |
CPP
|
ax,ay,bx,by,tx,ty=map(int,input().split())
n=int(input())
a,b=[],[]
res=0
for i in range(n):
x, y=map(int, input().split())
lt=((tx-x)*(tx-x)+(ty-y)*(ty-y))**0.5
la=((ax-x)*(ax-x)+(ay-y)*(ay-y))**0.5
lb=((bx-x)*(bx-x)+(by-y)*(by-y))**0.5
a+=[(la-lt,i)]
b+=[(lb-lt,i)]
res+=lt
a.sort();b.sort()
res*=2
if a[0][1]==b[0][1] and n>1:
res+=min(a[0][0],b[0][0],a[0][0]+b[1][0],a[1][0]+b[0][0])
else:
if a[0][1]==b[0][1]:
res+=min(a[0][0],b[0][0])
else:
res+=min(a[0][0],b[0][0],a[0][0]+b[0][0])
print(res)
| 1,800 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
char s[10], kq[15][10];
int dem[15];
int main() {
int i, j, max1, len, so;
while (scanf("%s", &s) > 0) {
len = strlen(s);
max1 = 0;
for (i = 0; i < len; i++)
if (s[i] > max1) max1 = s[i];
max1 -= '0';
for (i = 0; i < max1; i++) {
kq[i][0] = '\0';
dem[i] = 0;
}
for (i = 0; i < len; i++) {
so = s[i] - '0';
for (j = 0; j < max1; j++) {
if (so <= 0) {
if (dem[j] == 0) continue;
kq[j][dem[j]] = '0';
kq[j][dem[j] + 1] = '\0';
dem[j]++;
} else {
kq[j][dem[j]] = '1';
kq[j][dem[j] + 1] = '\0';
dem[j]++;
so--;
}
}
}
printf("%d\n", max1);
for (i = 0; i < max1; i++) {
if (i == 0)
printf("%s", kq[i]);
else
printf(" %s", kq[i]);
}
printf("\n");
}
return 0;
}
| 1,400 |
CPP
|
for tc in range(int(input())):
n = int(input())
if n<31:
print('NO')
elif n==36 or n==40 or n==44:
print('YES')
print('6 10 15',n-31)
else:
print('YES')
print('6 10 14',n-30)
| 800 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
long long dp[101][101][11][2];
long long k1, k2;
long long solve(long long n1, long long n2, long long count, long long flag) {
if (n1 < 0 || n2 < 0) {
return 0;
}
if (n1 == 0 && n2 == 0) {
return 1;
}
if (flag != -1 && dp[n1][n2][count][flag] != -1) {
return dp[n1][n2][count][flag];
}
long long i;
long long ans = 0;
if (flag != 1) {
for (i = 1; i <= k2; i++) {
ans += solve(n1, n2 - i, i, 1);
ans %= (long long)1e8;
}
}
if (flag != 0) {
for (i = 1; i <= k1; i++) {
ans += solve(n1 - i, n2, i, 0);
ans %= (long long)1e8;
}
}
if (flag != -1) dp[n1][n2][count][flag] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n1, n2;
cin >> n1 >> n2 >> k1 >> k2;
memset(dp, -1, sizeof(dp));
cout << solve(n1, n2, 0, -1);
return 0;
}
| 1,700 |
CPP
|
n, k = input().split()
n, k = int(n), int(k)
arr = [int(x) for x in input().split()]
if n == k:
print(int(1e9))
else:
arr.sort()
if k == 0 and arr[0] == 1:
print(-1)
elif k == 0 and arr[0] > 1:
print(1)
else:
p = arr[k-1]
if p == arr[k]:
print(-1)
else:
print(p)
| 1,200 |
PYTHON3
|
from sys import stdin,stdout
# stdin = open("input.txt", "r");
# stdout = open("output.txt", "w");
t=int(stdin.readline().strip())
for _ in range(t):
n=int(stdin.readline().strip())
s=stdin.readline().strip()
all_str=[]
for k in range(n):
pol=(n-k)%2
if pol==0:
all_str.append((s[k:]+s[:k],k+1))
else:
all_str.append((s[k:]+s[:k][::-1],k+1))
all_str=sorted(all_str,key=lambda ele: (ele[0],ele[1]))
#print(all_str)
stdout.write(all_str[0][0]+"\n")
stdout.write(str(all_str[0][1])+"\n")
| 1,400 |
PYTHON3
|
#include <bits/stdc++.h>
const double EPS = 0.0000000001;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long mod3 = 1000000009;
const long long mod4 = 228228227;
const long long inf = 100000000000000;
using namespace std;
signed main() {
long long t;
cin >> t;
for (long long q = 0; q < t; q++) {
long long n, a, b;
cin >> n >> a >> b;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
long long dp[n];
dp[0] = v[0];
for (long long i = 1; i < n; i++) {
dp[i] = inf;
if ((i - b) == -1) {
dp[i] = v[i];
}
if ((i - b) >= 0) dp[i] = dp[i - b] + v[i];
dp[i] = min(dp[i], dp[i - 1] + v[i]);
}
for (long long i = n - 1; i >= 0; i--) {
if (dp[i] <= a) {
cout << i + 1 << endl;
break;
}
}
if (dp[0] > a) cout << 0 << endl;
}
}
| 1,600 |
CPP
|
n = int(input())
a = list(map(int, input().split()))
res = ""
for i in range(n):
if a[i]%2 == 1:
res += str(a[i]) + " "
else:
res += str(a[i]-1) + " "
print(res)
| 800 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
void dfs(int x, vector<vector<int> > &graph, vector<bool> &is_in_subgraph,
vector<bool> &used, int &ans) {
if (used[x]) return;
used[x] = true;
if (is_in_subgraph[x]) {
ans = x;
return;
}
for (int i = 0; i < graph[x].size(); ++i)
dfs(graph[x][i], graph, is_in_subgraph, used, ans);
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n, a, b, k, x, k2;
cin >> n;
vector<vector<int> > graph(n);
for (int i = 0; i < n - 1; ++i) {
cin >> a >> b;
--a;
--b;
graph[a].push_back(b);
graph[b].push_back(a);
}
cin >> k;
vector<bool> is_in_subgraph(n);
for (int i = 0; i < k; ++i) {
cin >> x;
--x;
is_in_subgraph[x] = true;
}
cin >> k2;
set<int> his_subgraph;
for (int i = 0; i < k2; ++i) {
cin >> x;
his_subgraph.insert(x);
}
cout << "B " << x << endl;
cout.flush();
cin >> x;
--x;
vector<bool> used(n);
int ans = -1;
dfs(x, graph, is_in_subgraph, used, ans);
cout << "A " << ans + 1 << endl;
cout.flush();
cin >> x;
if (his_subgraph.find(x) != his_subgraph.end()) {
cout << "C " << ans + 1 << endl;
cout.flush();
} else {
cout << "C " << -1 << endl;
cout.flush();
}
}
return 0;
}
| 1,900 |
CPP
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int x1;
int x2;
int y1;
int y2;
int r1;
int r2;
int R1;
int R2;
cin >> x1;
cin >> y1;
cin >> r1;
cin >> R1;
cin >> x2;
cin >> y2;
cin >> r2;
cin >> R2;
int p = 4;
float s = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if ((s - R2 < r1) &&
(((r2 > r1) && (s + r1 > r2)) || ((R2 < r1) && (s + R2 > r1)) ||
((r2 <= r1) && (R2 >= r1) && (r1 + R2 > s))))
p--;
if ((s - R1 < r2) &&
(((r1 > r2) && (s + r2 > r1)) || ((R1 < r2) && (s + R1 > r2)) ||
((r1 <= r2) && (R1 >= r2) && (r2 + R1 > s))))
p--;
if ((s - R2 < R1) &&
(((r2 > R1) && (s + R1 > r2)) || ((R2 < R1) && (s + R2 > R1)) ||
((r2 <= R1) && (R2 >= R1) && (R1 + R2 > s))))
p--;
if ((s - R1 < R2) &&
(((r1 > R2) && (s + R2 > r1)) || ((R1 < R2) && (s + R1 > R2)) ||
((r1 <= R2) && (R1 >= R2) && (R2 + R1 > s))))
p--;
cout << p;
}
| 1,900 |
CPP
|
import collections,sys
n = int(sys.stdin.readline())
g = []
sys.setrecursionlimit(2**20)
for i in range(n+1):
g.append([])
for i in range(n-1):
a,b = list(map(int,sys.stdin.readline().split(' ')))
g[a].append(b)
g[b].append(a)
leaf = {}
mx = n-1
for i in range(1,n+1,1):
if(len(g[i]) == 1):
leaf[i] = True
else:
s = i
for i in range(1,n+1,1):
l = 0
for v in g[i]:
if(v in leaf):
l+=1
if(l != 0):
mx = mx-l+1
mn = 1
l = s
dst = [0]*(n+1)
a = collections.deque([])
exp = {}
exp[l] = True
a.append(l)
o = 0
e = 0
while(a):
v = a.pop()
h = dst[v]+1
for u in g[v]:
if(u not in exp):
dst[u] = h
exp[u] = True
a.append(u)
if(u in leaf):
if(h % 2 == 1):
o += 1
else:
e += 1
if(o == 0 or e ==0):
mn = 1
else:
mn = 3
print(mn,mx)
| 1,800 |
PYTHON3
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")
def main():
t = I()
rr = []
for _ in range(t):
n,m = LI()
s = S()
p = LI_()
r = [0] * 26
t = [0] * 26
pc = collections.Counter(p)
pc[n-1] += 1
for i in range(n):
c = ord(s[i]) - ord('a')
t[c] += 1
pt = pc[i]
if pt > 0:
for j in range(26):
r[j] += t[j] * pt
rr.append(JA(r, " "))
return JA(rr, "\n")
print(main())
| 1,300 |
PYTHON3
|
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq,bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------------------------------------------------------------------------------------------------------
m,n=map(int,input().split())
l=[]
for i in range(m):
e=list(map(int,input().split()))
l.append(set(e[1:]))
for i in range(m):
for j in range(i+1,m):
if len(l[i]&l[j])==0:
print("impossible")
sys.exit(0)
print("possible")
| 2,100 |
PYTHON3
|
import sys
import math
from _collections import deque
n = int(sys.stdin.readline())
an = [int(x) for x in (sys.stdin.readline()).split()]
ar = [0] * 8
for i in range(n):
ar[an[i]] += 1
t = int(n / 3)
if(ar[1] != t):
print(-1)
exit()
c = 0
d = []
for i in range(t):
if(ar[2] > 0 and ar[4] > 0):
ar[2] -= 1
ar[4] -= 1
d.append("1 2 4")
elif(ar[2] > 0 and ar[6] > 0):
ar[2] -= 1
ar[6] -= 1
d.append("1 2 6")
elif(ar[3] > 0 and ar[6] > 0):
ar[3] -= 1
ar[6] -= 1
d.append("1 3 6")
else:
c += 1
if(c != 0):
print(-1)
else:
for i in d:
print(i)
| 1,200 |
PYTHON3
|
import sys
import math
import heapq
import collections
def inputnum():
return(int(input()))
def inputnums():
return(map(int,input().split()))
def inputlist():
return(list(map(int,input().split())))
def inputstring():
return([x for x in input()])
def inputstringnum():
return([ord(x)-ord('a') for x in input()])
def inputmatrixchar(rows):
arr2d = [[j for j in input().strip()] for i in range(rows)]
return arr2d
def inputmatrixint(rows):
arr2d = []
for _ in range(rows):
arr2d.append([int(i) for i in input().split()])
return arr2d
t = int(input())
for q in range(t):
n, s, k = inputnums()
a = inputlist()
for i in range(0, k+1):
if s-i >= 1 and not s-i in a:
print(i)
break
if s+i <= n and not s+i in a:
print(i)
break
| 1,100 |
PYTHON3
|
#include <bits/stdc++.h>
int n;
char str[(80)];
int dp[(80)][(80)][(80)][2];
int cnt_v[(80)], cnt_k[(80)], cnt_x[(80)];
std::vector<int> vec_v, vec_k, vec_x;
int main() {
scanf("%d%s", &n, str + 1);
for (int i = 1; i <= n; ++i) {
cnt_v[i] = cnt_v[i - 1];
cnt_k[i] = cnt_k[i - 1];
cnt_x[i] = cnt_x[i - 1];
switch (str[i]) {
case 'V':
vec_v.push_back(i);
++cnt_v[i];
break;
case 'K':
vec_k.push_back(i);
++cnt_k[i];
break;
default:
vec_x.push_back(i);
++cnt_x[i];
}
}
memset(dp, 0x7f, sizeof(dp));
int A = (int)vec_v.size(), B = (int)vec_k.size(), C = (int)vec_x.size();
dp[0][0][0][0] = 0;
for (int i = 0; i <= A; ++i) {
for (int j = 0; j <= B; ++j) {
for (int k = 0; k <= C; ++k) {
if (!i && !j && !k) continue;
if (i) {
int posV = vec_v[i - 1];
int costV =
std::max(0, cnt_k[posV] - j) + std::max(0, cnt_x[posV] - k);
dp[i][j][k][1] =
std::min(dp[i - 1][j][k][0], dp[i - 1][j][k][1]) + costV;
}
if (j) {
int posK = vec_k[j - 1];
int costK =
std::max(0, cnt_v[posK] - i) + std::max(0, cnt_x[posK] - k);
dp[i][j][k][0] = dp[i][j - 1][k][0] + costK;
}
if (k) {
int posX = vec_x[k - 1];
int costX =
std::max(0, cnt_v[posX] - i) + std::max(0, cnt_k[posX] - j);
dp[i][j][k][0] = std::min(
dp[i][j][k][0],
std::min(dp[i][j][k - 1][0] + costX, dp[i][j][k - 1][1] + costX));
}
}
}
}
printf("%d\n", std::min(dp[A][B][C][0], dp[A][B][C][1]));
return 0;
}
| 2,500 |
CPP
|
t=int(input())
for i in range(0,t):
n=int(input())
mid_cell=int((n-1)/2)
k=n
ans=0
for j in range(0,mid_cell):
dist_mid_cell = (mid_cell - j);
num_cells = ((4*k)-4);
k = (k-2);
ans = ans + (num_cells*dist_mid_cell);
print(ans)
| 1,000 |
PYTHON3
|
#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char *sdbg, TH h, TA... t) {
while (*sdbg != ',') {
cerr << *sdbg++;
}
cerr << "=" << h << ",";
_dbg(sdbg + 1, t...);
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
T lcm(T a, T b) {
return a * b / gcd(a, b);
}
vector<string> split(string s) {
istringstream buf(s);
istream_iterator<string> beg(buf), end;
vector<string> list(beg, end);
return list;
}
bool isvowel(char c) {
string s = "aeiouAEIOU";
if (find(s.begin(), s.end(), c) != s.end()) return true;
return false;
}
vector<long long> sieve(long long n) {
vector<bool> prime(n + 1, true);
prime[0] = prime[1] = 0;
for (int i = 2; i * i <= n; i++) {
if (prime[i])
for (int j = i * i; j <= n; j += i) {
prime[j] = 0;
}
}
vector<long long> p;
for (int i = 2; i <= prime.size(); i++)
if (prime[i]) p.push_back(i);
return p;
}
bool isprime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true && (n != 1);
}
int ncr(long long n, long long r) {
long long C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long i = 1; i <= n; i++) {
for (long long j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % 1000000007;
}
return C[r];
}
long long mod_exp(long long x, long long n, long long mod) {
if (n == 0) return 1;
if (n == 1) return x;
long long ans = mod_exp(x, n / 2, mod);
ans = (ans % mod * ans % mod) % mod;
if (n & 1) ans = (ans * x) % mod;
return ans;
}
void solve() {
int n;
cin >> n;
vector<int> v(n);
set<int> s;
for (int i = 0; i < n; i++) {
cin >> v[i];
s.insert(v[i]);
}
int a = *s.begin();
int b = *s.rbegin();
int diff = b - a;
if (s.size() > 3) {
cout << -1;
return;
}
s.erase(a);
s.erase(b);
if (s.size() == 1 && (diff % 2 == 1 || *s.begin() != a + diff / 2)) {
cout << -1;
return;
}
if (s.size() == 1 || diff % 2 == 0)
cout << diff / 2;
else
cout << diff;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| 1,200 |
CPP
|
j=input("")
k=""
i=0
while i<len(j):
if j[i]=="-":
if j[i+1]==".":
k+="1"
i+=2
elif j[i+1]=="-":
k+="2"
i+=2
else:
k+="0"
i+=1
print(k)
| 800 |
PYTHON3
|
str1=input()
str2=''
str3=''
for i in str1:
if (65<=ord(i)<91):
str3=str3+chr(ord(i)+32)
else:
str3=str3+i
for i in str3:
if (ord(i)==97 or ord(i)==101 or ord(i)==105 or ord(i)==111 or ord(i)==117 or ord(i)==121):
continue
else:
str2+=('.'+i)
print (str2)
| 1,000 |
PYTHON3
|
#include <bits/stdc++.h>
const int N = 200 + 10;
typedef long long Matrix[N][N];
int m, a[N];
long long n;
char s[N];
Matrix adj;
struct {
int next[26], fail, val;
} pool[N];
int tot = 1, root = 1;
void add(char s[], int v) {
int u = root;
for (char *it = s; *it != '\0'; ++it) {
int ch = *it - 'a';
if (!pool[u].next[ch]) pool[u].next[ch] = ++tot;
u = pool[u].next[ch];
}
pool[u].val += v;
}
void build() {
static std::queue<int> q;
q.push(root);
while (!q.empty()) {
int a = q.front();
q.pop();
pool[a].val += pool[pool[a].fail].val;
for (int i = 0; i < 26; ++i) {
int &b = pool[a].next[i];
(b ? (q.push(b), pool[b].fail) : b) =
(a == root) ? root : pool[pool[a].fail].next[i];
}
}
memset(adj, -0x3f, sizeof adj);
for (int a = 1; a <= tot; ++a) adj[a][a] = 0;
for (int a = 1; a <= tot; ++a) {
for (int i = 0; i < 26; ++i) {
int b = pool[a].next[i];
adj[a][b] = pool[b].val;
}
}
}
void mul(Matrix a, const Matrix b) {
static Matrix c;
memset(c, -0x3f, sizeof c);
for (register int i = 1; i <= tot; ++i)
for (register int k = 1; k <= tot; ++k)
for (register int j = 1; j <= tot; ++j)
c[i][j] = std::max(c[i][j], a[i][k] + b[k][j]);
memcpy(a, c, sizeof c);
}
void power(Matrix base, long long exp) {
static Matrix res;
memset(res, -0x3f, sizeof res);
res[1][1] = 0;
for (; exp; exp >>= 1) {
if (exp & 1) mul(res, base);
mul(base, base);
}
memcpy(base, res, sizeof res);
}
int main() {
scanf("%d%lld", &m, &n);
pool[root].fail = root;
for (int i = 1; i <= m; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= m; ++i) {
scanf(" %s", s);
add(s, a[i]);
}
build();
power(adj, n);
printf("%lld\n", *std::max_element(adj[1] + 1, adj[1] + tot + 1));
return 0;
}
| 2,500 |
CPP
|
s=input()
count=0
for x in range(26):
ch=chr(x+97)
if s.find(ch)!=-1:
count=count+1
else:
count=count
print(count)
| 800 |
PYTHON3
|
a=int(input())
if a%5==0:
b=a//5
print(b)
else:
b=a//5
c=b+1
print(c)
| 800 |
PYTHON3
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.