solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
def main():
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
if k == 0:
if n % 2 == 0:
print(0)
else:
print(1)
elif n <= k:
print(k - n)
else:
if n % 2 == 1 and k % 2 == 0:
print(1)
elif n % 2 == 0 and k % 2 == 1:
print(1)
else:
print(0)
if __name__ == '__main__':
main()
| 900 | PYTHON3 |
n = input()
ans = 0
while int(n) > 9:
total = 0
for c in n:
total += int(c)
n = str(total)
ans += 1
print ( ans ) | 1,000 | PYTHON3 |
MOD = 1000000007
for _ in range(int(input())):
n, k = map(int, input().split())
ans = 0
for digit in bin(k):
ans *= n
if digit == '1':
ans += 1
ans %= MOD
print(ans)
| 1,100 | PYTHON3 |
t = int(input())
def result(x,n,m):
for i in range(n):
x=min(x,int(x/2)+10)
for i in range(m):
x-=10
if x>0:
return False
else:
return True
for i in range(t):
x,n,m = list(map(int,input().split()))
ans = result(x,n,m)
if ans == True:
print('YES')
else:
print('NO') | 900 | PYTHON3 |
w=int(input())
if w>=1 and w<=100 and w//2>=2 and w//2==w/2 :
print("Yes")
else:
print("No")
| 800 | PYTHON3 |
p,vm=0,[]
for j in range(int(input())):
o,i=list(map(int,input().split()))
p-=o
p+=i
vm.append(p)
print(max(vm))
| 800 | PYTHON3 |
for _ in range(int(input())):
am = int(input())
arr = list(map(int,input().split()))
fl = False
l = 0
r = am
for i in range(am):
if arr[i] == 1:
l = i
break
for i in range(am-1,-1,-1):
if arr[i] == 1:
r = i
break
print(arr[l:r].count(0)) | 800 | PYTHON3 |
#include <bits/stdc++.h>
class NT {
public:
~NT();
static long long gcd(long long x, long long y);
};
using namespace std;
class Solution {
public:
void solve(int num, std::istream& in, std::ostream& out) {
long long l, r, x, y;
in >> l >> r >> x >> y;
if (y % x) {
out << 0;
return;
} else {
int count = 0;
long long z = y / x;
for (long long i = 1; i * i <= z; i++) {
if (z % i == 0) {
long long a = i * x;
if (a < l || a > r) continue;
long long d = z / i;
long long b = d * x;
if (b < l || b > r) continue;
if (NT::gcd(i, d) == 1) {
if (d != i)
count += 2;
else
count++;
}
}
}
out << count;
}
}
};
void solve(std::istream& in, std::ostream& out) {
out << std::setprecision(12);
Solution solution;
solution.solve(0, in, out);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
istream& in = cin;
ostream& out = cout;
solve(in, out);
return 0;
}
NT::~NT() {}
long long NT::gcd(long long x, long long y) {
if (y == 0) return x;
return gcd(y, x % y);
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool roads[405][405];
int bfs(int n, bool road_type) {
queue<int> towns;
towns.push(1);
bool visited[405];
int road_length[405];
for (int i = 1; i <= n; i++) {
visited[i] = false;
road_length[i] = -1;
}
road_length[1] = 0;
visited[1] = true;
while (!towns.empty()) {
int current = towns.front();
towns.pop();
for (int town = 1; town <= n; town++) {
if (roads[current][town] == road_type && !visited[town]) {
visited[town] = true;
towns.push(town);
road_length[town] = road_length[current] + 1;
}
}
}
return road_length[n];
}
int main() {
int n, m;
cin >> n >> m;
int x, y;
while (m--) {
cin >> x >> y;
roads[x][y] = true;
roads[y][x] = true;
}
int ans1 = bfs(n, false);
int ans2 = bfs(n, true);
if (ans1 == -1 || ans2 == -1) {
cout << "-1" << endl;
} else {
cout << max(ans1, ans2) << endl;
}
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int N = n;
n++;
vector<int> p(n + 1);
for (; n > 1;) {
int b = log2(n - 1);
int m = (1 << b) - 1;
for (int i = 1; i < n - m; i++) p[m + i] = m - i + 1, p[m - i + 1] = m + i;
n = 2 * m + 2 - n;
}
cout << ((long long)N) * ((long long)(N) + 1ll) << "\n";
for (int i = 0; i <= N; i++) cout << p[i] << " ";
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
long long l, e, ans, x, y;
cin >> l >> e;
l--;
x = l / 2;
y = e / 2;
if (l % 2 == 1) x -= l;
if (e % 2 == 1) y -= e;
v.push_back(y - x);
}
for (int i = 0; i < n; i++) cout << v[i] << endl;
return 0;
}
| 900 | CPP |
s = input()
flag=0
for i in s:
if(i=='H' or i=='Q' or i=="9"):
flag=1
break
print("YES") if(flag) else print("NO") | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, qr;
vector<pair<int, int> > q[100005];
vector<int> Set;
int l, x, v[100005], sol[100005];
bool inSet[10000000];
int main() {
cin >> n >> qr;
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= qr; i++) {
scanf("%d%d", &l, &x);
q[l].push_back({x, i});
}
Set.push_back(0);
inSet[0] = 1;
int ans = 1;
for (int i = 1; i <= n; i++) {
if (inSet[v[i]])
ans = (ans * 2) % 1000000007;
else {
int sz = Set.size();
for (int j = 0; j < sz; j++) {
Set.push_back(Set[j] ^ v[i]);
inSet[Set.back()] = 1;
}
}
for (auto it : q[i]) {
sol[it.second] = ans * inSet[it.first];
}
}
for (int i = 1; i <= qr; i++) printf("%d\n", sol[i]);
return 0;
}
| 2,400 | CPP |
n=int(input())
arr1=[int(i) for i in input().split()]
m=int(input())
arr2=[int(i) for i in input().split()]
arr1.sort()
arr2.sort()
ans=0
for i in range(n):
for j in range(m):
if(abs(arr1[i]-arr2[j])<2):
ans+=1
arr1[i]=-10
arr2[j]=-10
print(ans)
| 1,200 | PYTHON3 |
from math import gcd
for _ in range(int(input())):
a, b, c = map(int, input().split())
x = 10 ** (a - 1)
y = 10 ** (b - 1)
if a <= b:
x += 10 ** (c - 1)
else:
y += 10 ** (c - 1)
print(x, y) | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n;
char s[100086];
string S;
multiset<string> st;
vector<string> v;
inline bool cmp(string a, string b) { return a.length() < b.length(); }
int cnt[26];
int main() {
scanf("%d", &n);
if (n == 1) {
printf("? 1 1\n"), fflush(stdout);
scanf("%s", s);
printf("! %s\n", s);
return 0;
}
printf("? 2 %d\n", n), fflush(stdout);
for (int i = 1; i <= n * (n - 1) / 2; i++) {
scanf("%s", s);
S = s;
sort(S.begin(), S.end());
st.insert(S);
}
printf("? 1 %d\n", n), fflush(stdout);
for (int i = 1; i <= n * (n + 1) / 2; i++) {
scanf("%s", s);
S = s;
sort(S.begin(), S.end());
if (st.find(S) == st.end())
v.push_back(S);
else
st.erase(st.find(S));
}
sort(v.begin(), v.end(), cmp);
printf("! ");
for (int i = 0; i < n; i++) {
if (i == 0) {
printf("%c", v[i][0]);
continue;
}
for (int j = 0; j < i + 1; j++) cnt[v[i][j] - 'a']++;
for (int j = 0; j < i; j++) cnt[v[i - 1][j] - 'a']--;
for (int j = 0; j < 26; j++) {
if (cnt[j]) {
cnt[j] = 0, printf("%c", j + 'a');
break;
}
}
}
puts("");
}
| 2,400 | CPP |
a=int(input(""))
b=int(input(""))
c=int(input(""))
minm = min([a,c])
maxm = max([a,c])
totals=[]
totals.append((minm+b)*maxm)
totals.append(a*b*c)
totals.append(a+b+c)
print(max(totals)) | 1,000 | PYTHON3 |
import sys
import math,bisect
sys.setrecursionlimit(10 ** 5)
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,defaultdict
def I(): return int(sys.stdin.readline())
def neo(): return map(int, sys.stdin.readline().split())
def Neo(): return list(map(int, sys.stdin.readline().split()))
for i in range(I()):
n,m,k = neo()
A = Neo()
if n == 1:
print('YES')
continue
f = 0
for i in range(n-1):
if A[i] >= A[i+1]:
m += A[i]-A[i+1]
m += min(k,A[i+1])
else:
if A[i+1]-A[i] <= k:
m += min(k-(A[i+1]-A[i]),A[i])
else:
t = A[i+1]-A[i]-k
m -= t
if m < 0:
f = 1
break
#print(m)
print('YES') if f == 0 else print('NO')
| 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void fun() {}
long long int __gcda(long long int a, long long int b) {
if (b == 0) {
return a;
} else if (a == 0) {
return b;
} else if (a == b) {
return a;
} else if (b > a) {
return __gcda(a, b % a);
} else if (a > b) {
return __gcda(a % b, b);
}
return 0;
}
long long int dp[1 << 18][18];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
map<pair<long long int, long long int>, long long int> mapp;
fun();
long long int n, m, k;
cin >> n >> m >> k;
long long int arr[n];
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
long long int a, b, c;
for (long long int i = 0; i < k; i++) {
cin >> a >> b >> c;
mapp[make_pair(a - 1, b - 1)] = c;
}
long long int q = 1 << n;
for (long long int i = 0; i < n; i++) {
dp[1 << i][i] = arr[i];
}
for (long long int mask = 1; mask < q; mask++) {
long long int currmask = mask;
for (long long int i = 0; i < n; i++) {
if (currmask & (1 << i)) {
for (long long int j = 0; j < n; j++) {
if ((currmask & (1 << j)) == 0) {
dp[mask | (1 << j)][j] =
max(dp[mask | (1 << j)][j],
dp[mask][i] + arr[j] + mapp[make_pair(i, j)]);
}
}
}
}
}
long long int ans = 0;
for (long long int i = 0; i < q; i++) {
long long int currmask = i;
long long int count = 0;
while (currmask) {
count += currmask & 1;
currmask >>= 1;
}
if (count != m) continue;
for (long long int j = 0; j < n; j++) {
ans = max(ans, dp[i][j]);
}
}
cout << ans << "\n";
}
| 1,800 | CPP |
#include <bits/stdc++.h>
typedef struct {
int x, y;
} R;
int compare(const void *a, const void *b) {
R *aa = (R *)a;
R *bb = (R *)b;
if (aa->x < bb->x) {
return -1;
} else if (aa->x > bb->x) {
return 1;
} else if (aa->y < bb->y) {
return -1;
} else if (aa->y > bb->y) {
return 1;
} else {
return 0;
}
}
int main() {
R p[8];
int i, minx, midx, maxx, miny, midy, maxy;
for (i = 0; i < 8; ++i) scanf("%d%d", &(p[i].x), &(p[i].y));
minx = 10000000;
maxx = -1;
miny = 10000000;
maxy = -1;
for (i = 0; i < 8; ++i) {
if (p[i].x < minx) minx = p[i].x;
if (p[i].x > maxx) maxx = p[i].x;
if (p[i].y < miny) miny = p[i].y;
if (p[i].y > maxy) maxy = p[i].y;
}
midx = -1;
midy = -1;
for (i = 0; i < 8; ++i) {
if (p[i].x != minx && p[i].x != maxx) midx = p[i].x;
if (p[i].y != miny && p[i].y != maxy) midy = p[i].y;
}
if ((minx == maxx) || (miny == maxy) || (midx == -1) || (midy == -1)) {
printf("ugly\n");
return 0;
}
for (i = 0; i < 8; ++i) {
if ((p[i].x != minx && p[i].x != maxx && p[i].x != midx) ||
(p[i].y != miny && p[i].y != maxy && p[i].y != midy)) {
printf("ugly\n");
return 0;
}
}
qsort(p, 8, sizeof(R), compare);
if (p[0].x == minx && p[1].x == minx && p[2].x == minx && p[3].x == midx &&
p[4].x == midx && p[5].x == maxx && p[6].x == maxx && p[7].x == maxx &&
p[0].y == miny && p[1].y == midy && p[2].y == maxy && p[3].y == miny &&
p[4].y == maxy && p[5].y == miny && p[6].y == midy && p[7].y == maxy) {
printf("respectable\n");
} else {
printf("ugly\n");
}
return 0;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
clock_t time_p = clock();
void time() {
time_p = clock() - time_p;
cerr << "Time elapsed : " << (double)(time_p) / CLOCKS_PER_SEC << '\n';
}
map<pair<int, int>, int> m;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> x(n), y(n);
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) m[{x[i] + x[j], y[i] + y[j]}]++;
int res = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
res += m[{x[i] + x[j], y[i] + y[j]}] - 1;
}
cout << res / 2 << '\n';
time();
return 0;
}
| 1,900 | CPP |
def main():
n = int(input())
arr = [int(i) for i in input().split()]
q = sorted(arr)
z = q[0::2] + list(reversed(q[1::2]))
# print(q[0::2])
# print(q[1::2])
if q[-1] < q[-2] + q[-3]:
print("YES")
print(" ".join([str(i) for i in z]))
else:
print("NO")
if __name__ == "__main__":
main() | 1,100 | PYTHON3 |
def flip(c):
if(c.isupper()):
return c.lower()
else:
return c.upper()
def main():
s=input()
if(s.isupper()==True or s[1:].isupper()==True or len(s)==1):
q=""
for i in s:
q+=flip(i)
print(q)
else:
print(s)
main() | 1,000 | PYTHON3 |
NONE = 10 ** 10
n = int(input())
direct = input()
pos = [int(x) for x in input().split()]
best = NONE
for i,x in enumerate(pos):
if i+1 == n:
break
if direct[i:i+2] != 'RL':
continue
best = min(best, pos[i+1] - x)
if best == NONE:
best = -1
else:
best = best // 2
print(best)
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
bool at = 1;
vector<char> ans;
char* mail = new char[100];
cin >> mail;
ans.push_back(mail[0]);
for (unsigned int i = 1; i < strlen(mail); i++) {
if (at && i < (strlen(mail) - 2) && mail[i] == 'a' && mail[i + 1] == 't') {
ans.push_back('@');
++i;
at = 0;
} else if (i < (strlen(mail) - 3) && mail[i] == 'd' && mail[i + 1] == 'o' &&
mail[i + 2] == 't') {
ans.push_back('.');
++i;
++i;
} else {
ans.push_back(mail[i]);
}
}
for (int i = 0; i < ans.size(); ++i) cout << ans[i];
cout << endl;
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int UP = (int)1e5 + 7;
const int N = (int)1e6 + 7;
vector<long long> pref;
void precalc() {
pref.push_back(0);
for (int i = 1; i < UP; i++)
pref.push_back(pref.back() + 0ll + (i * (i + 1)) / 2);
}
long long get(long long x) {
int l = 0, r = UP - 1, ans = 0;
while (l <= r) {
int m = (r + l) >> 1;
if ((m * 1ll * (m + 1)) / 2ll <= x) {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
return x * 1ll * (ans + 1) - pref[ans];
}
vector<vector<pair<int, long long>>> g(N), t(N);
vector<vector<int>> rev(N), col(N);
int used[N], n, m, color = 0;
vector<int> order;
long long out[N], cost[N];
int d[N];
void top(int v) {
used[v] = 1;
for (auto e : g[v]) {
int to = e.first;
if (!used[to]) top(to);
}
order.push_back(v);
}
void clarify(int v, int cur_color) {
d[v] = cur_color;
col[cur_color].push_back(v);
used[v] = 1;
for (auto to : rev[v]) {
if (!used[to]) clarify(to, cur_color);
}
}
void condensation() {
for (int i = 1; i <= n; i++)
if (!used[i]) top(i);
fill(used, used + N, 0);
while (order.size()) {
if (!used[order.back()]) clarify(order.back(), ++color);
order.pop_back();
}
fill(used, used + N, 0);
}
void after_dfs(int v) {
used[v] = 1;
for (auto e : t[v]) {
int to = e.first;
if (!used[to]) after_dfs(to);
}
order.push_back(v);
}
long long dp[N];
long long dfs_dp_thanks_to_RockyB(int v) {
if (~dp[v]) return dp[v];
long long res = 0;
for (auto e : t[v]) {
int to = e.first;
long long len = e.second;
res = max(res, dfs_dp_thanks_to_RockyB(to) + len);
}
return dp[v] = res + cost[v];
}
int main() {
precalc();
scanf("%d%d", &n, &m);
while (m--) {
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
g[x].push_back({y, w});
rev[y].push_back(x);
}
condensation();
for (int i = 1; i <= n; i++) {
for (auto e : g[i]) {
int to = e.first;
long long len = e.second;
if (d[i] == d[to]) {
cost[d[i]] += get(len);
} else {
t[d[i]].push_back({d[to], len});
}
}
}
fill(used, used + N, 0);
order.clear();
for (int i = 1; i <= color; i++) {
if (!used[i]) after_dfs(i);
}
memset(dp, -1, sizeof(dp));
while (order.size()) {
dfs_dp_thanks_to_RockyB(order.back());
order.pop_back();
}
int s;
cin >> s;
printf("%I64d", dp[d[s]]);
return 0;
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long t, k;
int main() {
cin >> t;
while (t--) {
cin >> k;
long long nr = 0;
for (long long p = 100000; p; p /= 2)
while ((nr + p) * (nr + p) < k) nr += p;
if (k - nr * nr <= nr)
cout << k - nr * nr << ' ' << nr + 1 << '\n';
else
cout << nr + 1 << ' ' << nr + 2 - (k - nr * nr - nr) << '\n';
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(5005);
int n;
char a[MAXN][MAXN];
bool chkin[MAXN], chkout[MAXN];
bool solve() {
vector<int> in, out;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) chkin[j] = chkout[j] = 0;
out.clear();
for (int j = 0; j < n; j++)
if (a[j][i] == '1') chkin[j] = 1;
for (int j = 0; j < n; j++)
if (a[i][j] == '1') out.push_back(j);
for (int j = 0; j < out.size(); j++)
if (!chkout[out[j]]) {
chkout[out[j]] = 1;
for (int k = 0; k < n; k++)
if (a[out[j]][k] == '1') {
if (chkin[k]) {
printf("%d %d %d", k + 1, i + 1, out[j] + 1);
return 1;
} else
chkout[k] = 1;
}
}
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%s", a[i]);
if (!solve()) printf("-1");
}
| 2,000 | CPP |
# cook your dish here
n=int(input())
if(n>0):
print(n)
else:
if(len(str(n))==2):
print(n)
else:
m=[i for i in str(n)]
k=[j for j in str(n)]
m.pop()
k.pop(len(k)-2)
print(max(int(''.join(k)),int(''.join(m)))) | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, k, q, cnt, a[150005], t, y;
bool mp[150005];
pair<int, int> b[150005];
int main() {
scanf("%d%d%d", &n, &k, &q);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < q; i++) {
scanf("%d %d", &t, &y);
if (t == 1) {
if (cnt < k) {
b[cnt].first = a[y - 1];
b[cnt++].second = y - 1;
mp[y - 1] = true;
} else {
sort(b, b + cnt);
if (a[y - 1] > b[0].first) {
mp[b[0].second] = false;
b[0].first = a[y - 1];
b[0].second = y - 1;
mp[y - 1] = true;
}
}
} else {
if (mp[y - 1])
puts("YES");
else
puts("NO");
}
}
return 0;
}
| 1,200 | CPP |
t = int(input())
for _ in range(t):
n,x = map(int,input().split())
sl = list(input())
al = []
flag = 0
if x == 0:
flag = 1
now = 0
for s in sl:
if s == "0":
now += 1
else:
now -= 1
al.append(now)
k = al[-1]
if k == 0:
if x in al:
print(-1)
else:
print(0+flag)
else:
ans = 0
for a in al:
if (x - a) % k == 0 and (x - a) * k >= 0:
ans += 1
print(ans+flag) | 1,700 | PYTHON3 |
#include <bits/stdc++.h>
int n, c[2005], curUsed[2005], t[2005], curLen = 0;
char result[2005];
int check();
int main() {
int t;
char cur = 'a';
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
for (int i = 1; i <= n; i++) {
curLen++;
result[curLen - 1] = cur;
if (!check()) {
cur = 'a' + ('c' - cur - 1);
result[curLen - 1] = cur;
check();
}
}
for (int i = 0; i < n; i++) printf("%c", result[i]);
return 0;
}
int check() {
for (int i = 1; i <= n; i++) t[i] = 0;
for (int i = curLen - 1; i > -1; i--) {
if (result[i] == result[curLen - 1])
t[curLen - i]++;
else
break;
}
for (int i = 1; i <= n; i++) {
if ((curUsed[i] + t[i]) > c[i]) return 0;
}
for (int i = 1; i <= n; i++) curUsed[i] += t[i];
return 1;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct Linear {
long long a;
long long b;
long long c;
long long d;
};
long long LG = 61;
long long k, p;
Linear un(Linear f, Linear s) {
Linear ans;
ans.a = (f.a * s.a + f.c * s.b) % p;
ans.b = (s.a * f.b + f.d * s.b) % p;
ans.c = (f.a * s.c + f.c * s.d) % p;
ans.d = (s.c * f.b + f.d * s.d) % p;
return ans;
}
int32_t main() {
cin >> k >> p;
long long n, m, J, v;
cin >> n;
vector<long long> s(n);
for (long long i = 0; i < n; i++) {
cin >> s[i];
}
vector<pair<long long, long long> > ch;
cin >> m;
for (long long i = 0; i < m; i++) {
cin >> J >> v;
if (J < k) {
ch.push_back(make_pair(J, v));
}
}
m = ch.size();
sort(ch.begin(), ch.end());
Linear binup[n][LG];
for (long long i = 0; i < n; i++) {
long long a = s[(i + 1) % n];
long long b = s[i];
Linear l = {a, b, 1, 0};
binup[i][0] = l;
}
Linear l1, l2;
for (long long i = 1; i < LG; i++) {
for (long long j = 0; j < n; j++) {
long long sdv = j + (1LL << (i - 1));
long long ind = sdv % n;
l1 = binup[j][i - 1];
l2 = binup[ind][i - 1];
Linear l3 = un(l1, l2);
binup[j][i] = l3;
}
}
if (k == 0) {
cout << 0 << endl;
return 0;
}
if (k == 1) {
cout << 1 % p << endl;
return 0;
}
long long FF = 1;
long long SS = 0;
long long u = 0;
k--;
long long START = 0;
while (u < m) {
long long next = ch[u].first;
long long free = max((long long)0, next - START - 1);
for (long long i = LG - 1; i >= 0; i--) {
if (free < (1LL << i)) continue;
free -= (1LL << i);
k -= (1LL << i);
Linear LL = binup[START % n][i];
long long NF = (FF * LL.a + SS * LL.b) % p;
long long NS = (FF * LL.c + SS * LL.d) % p;
FF = NF, SS = NS;
START += (1LL << i);
}
long long old = next - 1;
long long old_v;
if (next > 0) {
old_v = s[(next - 1) % n];
}
while (u < m && ch[u].first - 1 == old) {
if (ch[u].first != 0) {
long long NF = (ch[u].second * FF + old_v * SS) % p;
SS = FF;
FF = NF;
k--;
START++;
}
old_v = ch[u].second;
old = ch[u].first;
u++;
}
START++;
if (k != 0) {
long long NF = (s[START % n] * FF + old_v * SS) % p;
SS = FF;
FF = NF;
k--;
}
}
for (long long i = LG - 1; i >= 0; i--) {
if (k < (1LL << i)) continue;
k -= (1LL << i);
Linear LL = binup[START % n][i];
long long NF = (FF * LL.a + SS * LL.b) % p;
long long NS = (FF * LL.c + SS * LL.d) % p;
FF = NF, SS = NS;
START += (1LL << i);
}
cout << FF % p << endl;
return 0;
}
| 2,700 | CPP |
def main():
a,b,c,n = map(int,input().split())
num = n-(a+b-c)
if num<=0 or a>n or b>n or c>n or a<c or b<c:
return -1
return num
if __name__ == '__main__':
print(main())
| 1,000 | PYTHON3 |
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
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")
#-------------------game starts now-----------------------------------------------------
n=int(input())
a=list()
c=0
d=0
j=0
s=0
for i in range (n):
b=list(map(int,input().split()))
if b[0]==1:
a.append(b[1])
c+=1
if c==1:
continue
while a[j]<=(s+a[-1])/(d+1) and j<c-1:
#print(a[j],(s+a[-1])/(d+1))
s+=a[j]
j+=1
d+=1
#print(s)
else:
m=a[-1]
if c==1:
print(0)
else:
if d==0:
avg=(a[0]+a[-1])/2
else:
avg=(s+a[-1])/(d+1)
print(m-avg) | 1,800 | PYTHON3 |
n,m=map(int,input().split())
a = list(map(int,input().split()))
b=list(map(int,input().split()))
chest_even=0
chest_odd=0
key_even=0
key_odd=0
for i in range(n):
if a[i]%2!=0:
chest_odd+=1
else:
chest_even+=1
for i in range(m):
if b[i]%2!=0:
key_odd+=1
else:
key_even+=1
x=min(chest_even,key_odd)+min(chest_odd,key_even)
print(x)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXM = int(1e5) + 10;
const int MAXN = int(2e5) + 10;
int n, m, k, q;
int can[MAXN];
int X1[MAXN], Y1[MAXN], X2[MAXN], Y2[MAXN], x[MAXN], y[MAXN];
vector<int> yline[MAXM];
vector<int> yrec[MAXM];
struct Tree {
int l, r, key;
Tree *lc, *rc;
Tree(int _l, int _r) : l(_l), r(_r) {
key = 1000000000;
if (l + 1 < r) {
lc = new Tree(l, (l + r) >> 1);
rc = new Tree((l + r) >> 1, r);
}
}
void modify(int x, int y) {
if (l + 1 == r)
key = y;
else {
x < (l + r) >> 1 ? lc->modify(x, y) : rc->modify(x, y);
key = max(lc->key, rc->key);
}
}
int query(int ll, int rr) {
if (ll <= l && rr >= r) return key;
int res = -int(1e9);
if (ll < (l + r) >> 1) res = max(res, lc->query(ll, rr));
if (rr > (l + r) >> 1) res = max(res, rc->query(ll, rr));
return res;
}
} * root;
void solve() {
for (int i = 1; i <= m; i++) yline[i].clear(), yrec[i].clear();
for (int i = 0; i < k; i++) yline[y[i]].push_back(i);
for (int i = 0; i < q; i++) yrec[Y1[i]].push_back(i);
root = new Tree(1, n + 1);
for (int yy = m; yy >= 1; yy--) {
for (auto id : yline[yy]) root->modify(x[id], y[id]);
for (auto id : yrec[yy])
if (root->query(X1[id], X2[id] + 1) <= Y2[id]) can[id] = 1;
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &k, &q);
for (int i = 0; i < k; i++) scanf("%d%d", x + i, y + i);
for (int i = 0; i < q; i++) scanf("%d%d%d%d", X1 + i, Y1 + i, X2 + i, Y2 + i);
solve();
swap(n, m);
for (int i = 0; i < k; i++) swap(x[i], y[i]);
for (int i = 0; i < q; i++) swap(X1[i], Y1[i]), swap(X2[i], Y2[i]);
solve();
for (int i = 0; i < q; i++) can[i] ? puts("YES") : puts("NO");
return 0;
}
| 2,400 | CPP |
for _ in range(int(input())):
a,b,n = map(int,input().split())
c = a^b
lst = [a,b,c]
print(lst[n%3])
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, temp;
cin >> n;
long long sum1 = 0, sum2 = 0;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n / 2; i++) {
sum1 += a[i];
}
for (int i = n / 2; i < n; i++) sum2 += a[i];
long long ans = sum1 * sum1 + sum2 * sum2;
cout << ans << endl;
}
| 900 | CPP |
#include <bits/stdc++.h>
using namespace std;
void sc() {}
template <class T, class... A>
void sc(T& t, A&... a) {
cin >> t, sc(a...);
}
void pr() {}
template <class T, class... A>
void pr(T t, A... a) {
cout << t, pr(a...);
}
const int N = 3e5 + 5;
vector<int> adj[N], a(N), ct(N);
multiset<int> s;
queue<int> q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
sc(n);
for (int i = 1; i <= n; i++) {
sc(a[i]);
s.insert(a[i]);
}
for (int i = 1; i < n; i++) {
int u, v;
sc(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans = INT_MAX;
for (int i = 1; i <= n; i++) {
int mx = a[i];
s.erase(s.lower_bound(a[i]));
q.push(a[i]);
for (int j : adj[i]) {
s.erase(s.lower_bound(a[j]));
q.push(a[j]);
mx = max(mx, a[j] + 1);
}
if (!s.empty()) mx = max(mx, *s.rbegin() + 2);
ans = min(ans, mx);
while (!q.empty()) {
s.insert(q.front());
q.pop();
}
}
pr(ans, '\n');
}
| 1,900 | CPP |
"""
n = streets = row
m = avenues = col
First Emma choose n
Then Jack choose m
"""
def solve(_n, _m, matrix):
answer = float('-inf')
for row in matrix:
answer = max(answer, min(row))
return answer
n, m = map(int, input().strip().split(' '))
matrix = [0] * n
for row in range(n):
matrix[row] = list(map(int, input().strip().split(' ')))
print(solve(n, m, matrix))
| 1,000 | PYTHON3 |
for _ in range(int(input())):
s = list(input())
s1 = set(s)
if len(s1)==1:
if s[0]=='R':
print("P"*len(s))
elif s[0]=="P":
print("S"*len(s))
else:
print("R"*len(s))
else:
p = s.count('P')
r = s.count('R')
ss = s.count('S')
m = max(p,r,ss)
if m==p:
print('S'*len(s))
elif m==r:
print('P'*len(s))
else:
print('R'*len(s)) | 1,400 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[100000], b[100000], s[100000], s2[100000];
int main() {
int n, m, c;
cin >> n >> m >> c;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
s[0] = b[0];
for (int i = 1; i < m; i++) s[i] = (s[i - 1] + b[i]);
s2[0] = b[m - 1];
for (int i = m - 2; i >= 0; i--) s2[m - i - 1] = (s2[m - i - 2] + b[i]);
int r1 = m - 1, l2 = n - m;
if (r1 <= l2) {
for (int i = 0; i < r1; i++) a[i] = (s[i] + a[i]) % c;
for (int i = r1; i <= l2; i++) a[i] = (a[i] + s[m - 1]) % c;
for (int i = n - 1; i > l2; i--) a[i] = (a[i] + s2[n - 1 - i]) % c;
} else {
for (int i = 0; i <= l2; i++) a[i] = (a[i] + s[i]) % c;
for (int i = l2 + 1; i <= r1; i++) a[i] = (a[i] + s[i] - s[i - l2 - 1]) % c;
for (int i = n - 1; i > r1; i--) a[i] = (a[i] + s2[n - i - 1]) % c;
}
for (int i = 0; i < n; i++) cout << a[i] << " ";
}
| 1,200 | CPP |
#include<bits/stdc++.h>
using namespace std;
typedef pair<int ,int> pii;
const long long INF = 10000000000000000ll;
namespace Flow{
const int maxv=2005;
const int maxe=20080;
int S,T,V,tot;
int head[maxv],nxt[maxe<<1],to[maxe<<1],cap[maxe<<1];
int lvl[maxv],que[maxv],cur[maxv];
void init(){
tot=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int c){
nxt[tot]=head[u];to[tot]=v;cap[tot]=c;head[u]=tot++;
nxt[tot]=head[v];to[tot]=u;cap[tot]=0;head[v]=tot++;
}
bool bfs(){
for(int i=0;i<V;i++)lvl[i]=-1;
lvl[S]=0;
int ql=0,qr=0;
que[qr++]=S;
while(ql<qr){
int x=que[ql++];
for(int i=head[x];~i;i=nxt[i])if(cap[i]&&lvl[to[i]]<0)
lvl[to[i]]=lvl[x]+1,que[qr++]=to[i];
}
return lvl[T]>=0;
}
int dfs(int x,int f){
if(x==T)return f;
for(int &i=cur[x];~i;i=nxt[i])if(cap[i]&&lvl[to[i]]==lvl[x]+1){
int d=dfs(to[i],min(f,cap[i]));
if(d){
cap[i]-=d;
cap[i^1]+=d;
return d;
}
}
return 0;
}
long long work(){
long long res=0;
while(bfs()){
for(int i=0;i<V;i++)cur[i]=head[i];
int f;
while((f=dfs(S,INF))>0)res+=f;
}
return res;
}
}
int n, w[1005], z[1005];
long long x[1005], y[1005];
long long rlt;
int main() {
// freopen("in.txt","r",stdin);
int i,j;
long long o;
o=0;
cin>>n;
for(i=0;i<n;i++){
cin>>x[i]>>y[i]>>w[i];
if(x[i]&1)
z[i]+=2;
if(y[i]&1)
z[i]++;
o+=w[i];
}
Flow::S=n<<1;Flow::T=n<<1|1;Flow::V=Flow::T+1;
Flow::init();
for(i=0;i<n;i++){
Flow::addedge(i*2,i*2+1,w[i]);
if(z[i]==2)
Flow::addedge(Flow::S,i*2, INF);
if(z[i]==3)
Flow::addedge(i*2+1,Flow::T, INF);
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(abs(x[i]-x[j])+abs(y[i]-y[j])<=1){
if(z[i]==2&&z[j]==0)
Flow::addedge(i*2+1,j*2, INF);
if(z[i]==0&&z[j]==1)
Flow::addedge(i*2+1,j*2, INF);
if(z[i]==1&&z[j]==3)
Flow::addedge(i*2+1,j*2, INF);
}
}
}
cout<<o-Flow::work();
return 0;
}
| 3,300 | CPP |
a = input()
s = ''
b = a[0].upper()
s += b
for i in range(len(a) - 1):
s += a[i + 1]
print(s)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0;
char c = getchar(), f = 0;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = 1;
for (; c >= '0' && c <= '9'; c = getchar())
x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
}
const int P = 998244353;
int n, dp[1505][1505], cnt = 0;
struct node {
int u, v, w;
bool operator<(node b) const { return w < b.w; }
} q[2250005];
struct edge {
int to, nxt;
} e[3005];
int rt, et, head[3005], fa[3005], sz[3005], te[3005];
int qt, a[1505][1505], L[3005], R[3005];
char tg[3005];
vector<int> v[1505];
inline void adde(int x, int y) { e[++et] = (edge){y, head[x]}, head[x] = et; }
inline int getf(int x) { return fa[x] == x ? x : fa[x] = getf(fa[x]); }
inline void add(int x) { tg[x] = (++te[x] == 1ll * sz[x] * (sz[x] - 1) / 2); }
inline void mrg(int x, int y) {
x = getf(x), y = getf(y);
if (x == y) return add(x);
++rt, adde(rt, x), adde(rt, y);
sz[rt] = sz[x] + sz[y], te[rt] = te[x] + te[y], fa[x] = fa[y] = fa[rt] = rt,
add(rt);
}
inline void dfs(int x) {
if (x <= n)
return L[x] = R[x] = ++cnt, void();
else
L[x] = n + 1;
for (int i = head[x]; i; i = e[i].nxt)
dfs(e[i].to), L[x] = min(L[x], L[e[i].to]);
R[x] = cnt;
if (tg[x]) v[R[x]].push_back(L[x]);
}
int main() {
read(n), rt = n;
for (int i = 1; i <= n; i++)
fa[i] = i, sz[i] = 1, te[i] = 0, tg[i] = 1, v[i].push_back(i);
for (int i = 1, x; i <= n; i++)
for (int j = 1; j <= n; j++) {
read(x);
if (i < j) q[++qt] = (node){i, j, x};
}
sort(q + 1, q + qt + 1);
for (int i = 1; i <= qt; i++) mrg(q[i].u, q[i].v);
dfs(rt), dp[0][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (auto k : v[i]) dp[i][j] = (dp[i][j] + dp[k - 1][j - 1]) % P;
for (int i = 1; i <= n; i++) printf("%d%c", dp[n][i], i == n ? '\n' : ' ');
return 0;
}
| 2,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
unsigned long long a, b;
cin >> a >> b;
for (int j = 0; j < 64; j++) {
if ((a & (1ULL << j)) == 0) {
if ((a + (1ULL << j)) <= b) a = a + (1ULL << j);
}
}
cout << a << endl;
}
}
| 1,700 | CPP |
def main():
s1 = input().replace(' ', '')
s2 = input().replace(' ', '')
s1_counts = {}
s2_counts = {}
for x in set(s1):
s1_counts[str(x)] = s1.count(x)
for x in set(s2):
s2_counts[str(x)] = s2.count(x)
for key in s2_counts.keys():
if key in s1_counts and s1_counts[key] >= s2_counts[key]:
pass
else: return 'NO'
return 'YES'
print(main()) | 1,100 | PYTHON3 |
t = input()
temp=t[0].upper()
temp_2=t[1:]
print(temp + temp_2)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
int f = 1;
long long res = 0;
char ch;
do {
ch = getchar();
if (ch == '-') f = -f;
} while (ch < '0' || ch > '9');
do {
res = res * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
return f == 1 ? res : -res;
}
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int N = 510;
const int M = 3010;
const int mod = 1e9 + 7;
const long long INF = 1e18;
int n, m;
int A[N], B[N];
char str[N];
int head[N], to[N * 2], nxt[N * 2], tot;
void addEdge(int u, int v) {
tot++;
nxt[tot] = head[u];
to[tot] = v;
head[u] = tot;
}
template <class T>
T mmax(T a, T b) {
return a < b ? b : a;
}
template <class T>
T mmin(T a, T b) {
return a < b ? a : b;
}
int countOne(long long set) {
int res = 0;
while (set) {
res++;
set &= set - 1;
}
return res;
}
bool contain(long long set, int i) { return (set & (1LL << i)) > 0; }
long long myPow(long long a, int p) {
if (p == 0) return 1;
long long res = myPow(a, p / 2);
res *= res;
res %= mod;
if (p % 2 == 1) {
res *= a;
res %= mod;
}
return res;
}
void addMode(long long &a, long long b) { a = (a + b) % mod; }
long long mul(long long a, long long b) { return a * b % mod; }
template <class T>
void mySwap(T &a, T &b) {
T tmp = a;
a = b;
b = tmp;
}
char table[N][N];
int main() {
fast_io();
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> table[i];
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (table[i][j] == '*') cnt++;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (table[i][j] == '*') {
int cnt1 = 1;
for (int k = 0; k < 4; k++) {
int tmp = 0;
int ni = i + dx[k];
int nj = j + dy[k];
while (ni >= 0 && ni < n && nj >= 0 && nj < m &&
table[ni][nj] == '*') {
tmp++;
ni = ni + dx[k];
nj = nj + dy[k];
}
if (tmp == 0) {
cnt1 = 0;
break;
} else {
cnt1 += tmp;
}
}
if (cnt1 == cnt) {
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
| 1,300 | CPP |
#include<bits/stdc++.h>
#define For(i, l, r) for(int i = l; i <= r; i++)
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <typename T>
T read(){
T x=0,f=1;
char ch;
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return f*x;
}
int main()
{
int T;
cin >> T;
while(T--){
int x ,y, i;
cin >> x >> y;
ll ans = 0;
for(i = 1; i <= y; i++){
if (i * i - 1 > x){
for(int j = 1; j <= i - 2; j++){
int ub = min(y, x / j - 1);
int db = x / (j + 1) - 1;
//if (x % (j + 1) == 0) db--;
db = max(i - 1, db);
if (ub <= db) continue;
ans += (ll) j * (ub - db);
}
break;
}
ans += i - 1;
}
cout << ans << endl;
}
return 0;
}
| 1,700 | CPP |
s = input().lower()
vowels = "aoyeui"
s2 = '' #no vowels
for char in s:
if char not in vowels:
s2 = s2 + '.' + char
print(s2) | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long w = 0, f = 1;
char c = ' ';
while (c < '0' || c > '9') c = getchar(), f = c == '-' ? -1 : f;
while (c >= '0' && c <= '9') w = w * 10 + c - 48, c = getchar();
return w * f;
}
struct node {
long long x, id;
} a[200005];
map<long long, long long> mp;
vector<long long> b[200005];
long long n, m, cnt, t1, t2, ans1[200005], ans2[200005];
void print(long long* x, long long y) {
for (long long i = 1; i <= y; i++)
printf("%lld%c", x[i], i == y ? '\n' : ' ');
}
signed main() {
n = read(), m = read();
for (long long i = 1; i <= n; i++) {
long long x = read();
if (!mp[x]) mp[x] = ++cnt;
b[mp[x]].push_back(i);
}
for (long long i = 1; i <= m; i++) a[i].x = read(), a[i].id = i;
sort(a + 1, a + m + 1,
[&](node x, node y) { return x.x != y.x ? x.x < y.x : x.id < y.id; });
for (long long i = 1; i <= m; i++) {
long long u = a[i].x, id = a[i].id;
for (long long j = 0;; j++, u = (u + 1) / 2) {
long long v = mp[u];
if (b[v].size()) {
ans2[b[v].back()] = id, b[v].pop_back();
t1++, t2 += j, ans1[id] += j;
break;
}
if (u == 1) break;
}
}
printf("%lld %lld\n", t1, t2);
print(ans1, m);
print(ans2, n);
return 0;
}
| 2,100 | CPP |
n=int(input(""))
c=0
for i in range(0,n+1,2):
c+=1
a=1
b=1
print(c)
for i in range(0,n):
print(a,b)
b+=1
if i%2==1:
a=a+1
b-=1 | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> A;
int main() {
scanf("%d", &N);
M = N - 1;
A.resize(M);
for (int i = 0; i < M; i++) {
int B;
scanf("%d %d", &A[i], &B);
if (B != N) {
puts("NO");
return 0;
}
}
sort(A.begin(), A.end());
for (int i = 0; i < M; i++)
if (A[i] <= i) {
puts("NO");
return 0;
}
vector<int> answer;
vector<bool> used(N, false);
int low = 1;
for (int i = 0, j = 0; i < M; i = j) {
while (j < M && A[i] == A[j]) j++;
answer.push_back(A[i]);
used[A[i]] = true;
for (int k = 1; k <= j - i - 1; k++) {
while (used[low]) {
low++;
assert(low < N);
}
answer.push_back(low);
used[low] = true;
}
}
answer.push_back(N);
puts("YES");
for (int i = 0; i < M; i++) printf("%d %d\n", answer[i], answer[i + 1]);
}
| 1,900 | CPP |
import sys
fo={}
fi={}
mxfo=0
mxfi=0
al=sys.stdin.readlines()
lal=len(al)
for i in range(lal):
for j in reversed(range(len(al[i]))):
if j>3 and al[i][j]=='\\':
fo[al[i][0:j]]=0
fi[al[i][0:j]]=0
for i in range(lal):
cnt=0
for j in reversed(range(len(al[i]))):
if j>3 and al[i][j]=='\\':
fo[al[i][0:j]]+=cnt
if fi[al[i][0:j]]==0:
cnt+=1
fi[al[i][0:j]]+=1
mxfo=max(mxfo,fo[al[i][0:j]])
mxfi=max(mxfi,fi[al[i][0:j]])
print(str(mxfo)+" "+str(mxfi)) | 1,800 | PYTHON3 |
import math
from math import gcd
t = int(input())
for i in range(t):
n = int(input())
l=[]
for i in range(n):
l.append(input())
a=True
for i in range(n):
for j in range(n):
if l[i][j]=="1":
if (j+1<n and l[i][j+1]=="0") and (i+1<n and l[i+1][j]=="0"):
print("NO")
a=False
break
if a==False:
break
if a==True:
print("YES")
| 1,300 | PYTHON3 |
# phno = input()
# if len(phno)>=3 and len(phno)<=10:
# count = 0
# count_so_far = 0
# nonrepeated = []
# for i in phno:
# if i not in nonrepeated:
# nonrepeated.append(i)
# else:
# count = len(nonrepeated)
# nonrepeated = [i]
# print(nonrepeated)
# print(max(len(nonrepeated), count))
# else:
# print(-1)
# names = list(input().split())
# names.sort(key=len) # sorting list in increasing order of string length
# initial = names[0] # the first string is the maximum common length
# common = ''
# for i in range(1,len(initial)+1):
# checkstr = initial[:i] # checking substring is part of all strs or not
# flag = 0
# for name in names:
# if not name.startswith(checkstr):
# flag = 1
# break
# if flag == 0:
# common = checkstr
# else:
# break
# if len(common)==0:
# print(-1)
# else:
# print(common)
# n = input()
# l = len(n)
# countList = []
# for i in range(l):
# count = 0
# for j in range(i, l):
# if int(n[j])%2==0:
# count += 1
# countList.append(count)
# for c in countList:
# print(c, end = ' ')
# from itertools import combinations
# def is_valid(sides):
# return (sides[0]+sides[1] > sides[2] and sides[0]+sides[2] > sides[1] and sides[1]+sides[2] > sides[0])
# n = int(input())
# ropes = list(map(int, input().split()))
# combs = list(combinations(ropes, 3))
# count = 0
# for s in combs:
# if is_valid(s):
# count += 1
# print(count)
# p, q = map(int, input().split())
# total = 0
# for _ in range(q):
# i, j, n = map(int, input().split())
# total += ((j-i+1)*n)
# avg = int(total/p)
# print(avg)
'''
1. 1
2. 3
3. 7
4. 14
5. 25
'''
l, r = map(int, input().split())
def is_distinct(x):
return len(set(str(x))) == len(str(x))
for i in range(l, r+1):
if is_distinct(i):
print(i)
break
else:
print(-1)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int NOD(int a, int b) {
while (a > 0 && b > 0)
if (a > b)
a %= b;
else
b %= a;
return a + b;
}
int main() {
int n;
cin >> n;
vector<int> points(n);
for (int i = 0; i < n; ++i) {
cin >> points[i];
}
sort(points.begin(), points.end());
vector<int> gaps(n - 1);
for (int i = 0; i < points.size() - 1; ++i) {
gaps[i] = points[i + 1] - points[i];
}
int min_gap = *min_element(gaps.begin(), gaps.end());
int min_NOD = min_gap;
for (int i = 0; i < gaps.size(); ++i) {
min_NOD = min(min_NOD, NOD(min_gap, gaps[i]));
}
int points_to_add_count = 0;
for (int i = 0; i < gaps.size(); ++i) {
points_to_add_count += (gaps[i] / min_NOD) - 1;
}
printf("%d\n", points_to_add_count);
return 0;
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int curto[200200];
bool flag[200200] = {false};
int dist[200200];
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &curto[i]);
dist[i] = 999999999;
}
dist[1] = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
fila;
fila.push(pair<int, int>(0, 1));
while (!fila.empty()) {
int x = fila.top().second;
int z = fila.top().first;
fila.pop();
if (dist[x] != z) continue;
int y = x + 1;
if (y <= n && dist[y] > dist[x] + 1) {
dist[y] = dist[x] + 1;
fila.push(pair<int, int>(dist[y], y));
}
y = x - 1;
if (y > 0 && dist[y] > dist[x] + 1) {
dist[y] = dist[x] + 1;
fila.push(pair<int, int>(dist[y], y));
}
y = curto[x];
if (dist[y] > dist[x] + 1) {
dist[y] = dist[x] + 1;
fila.push(pair<int, int>(dist[y], y));
}
}
printf("%d", dist[1]);
for (int i = 2; i <= n; i++) {
printf(" %d", dist[i]);
}
printf("\n");
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <typename T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
template <typename T>
void read(vector<T>& v, long long n) {
v.clear();
v.resize(n);
for (long long i = 0; i < n; i++) cin >> v[i];
}
template <typename T>
void read(vector<pair<T, T>>& v, long long n) {
v.clear();
v.resize(n);
for (long long i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
}
template <typename T>
void read(vector<vector<T>>& v, long long n, long long m) {
v.clear();
v.resize(n, vector<T>(m));
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) cin >> v[i][j];
}
}
template <typename T>
void print(vector<T>& v) {
for (long long i = 0; i < v.size(); i++) cout << v[i] << ' ';
cout << endl;
}
template <typename T>
void print(vector<pair<T, T>>& v) {
for (long long i = 0; i < v.size(); i++)
cout << v[i].first << ' ' << v[i].second << endl;
cout << endl;
}
template <typename T>
void print(vector<vector<T>>& v) {
for (long long i = 0; i < v.size(); i++) {
for (long long j = 0; j < v[i].size(); j++) cout << v[i][j];
cout << endl;
}
}
template <typename T>
void print(vector<vector<T>>& v, long long n) {
for (long long i = 1; i <= n; i++) {
cout << i << ": ";
for (long long j = 0; j < v[i].size(); j++) cout << v[i][j] << ' ';
cout << endl;
}
}
struct uf {
vector<long long> par, size;
void init(long long n) {
par.resize(n, -1);
size.resize(n, 1);
}
long long find(long long a) {
if (par[a] == -1) return a;
return par[a] = find(par[a]);
}
void unite(long long a, long long b) {
a = find(a);
b = find(b);
if (a == b) return;
if (size[a] < size[b]) {
par[a] = b;
size[b] += size[a];
} else {
par[b] = a;
size[a] += size[b];
}
}
bool isCycle(long long a, long long b) {
if (find(a) == find(b)) return true;
return false;
}
};
static bool comp(vector<long long>& a, vector<long long>& b) {
if (a[0] < b[0])
return true;
else if (a[0] == b[0])
return a[1] > b[1];
return false;
}
long long mod = 1e9 + 7;
long long lim = 2 * 1e5 + 5;
void sol() {
long long n;
cin >> n;
vector<pair<long long, long long>> v(n + 1);
read(v, n + 1);
char dir = 'N';
long long ret = 0;
for (long long i = 1; i < n; i++) {
if (dir == 'N') {
if (v[i + 1].first > v[i].first)
dir = 'E';
else {
dir = 'W';
ret++;
}
} else if (dir == 'S') {
if (v[i + 1].first < v[i].first)
dir = 'W';
else {
dir = 'E';
ret++;
}
} else if (dir == 'E') {
if (v[i + 1].second < v[i].second)
dir = 'S';
else {
dir = 'N';
ret++;
}
} else if (dir == 'W') {
if (v[i + 1].second > v[i].second)
dir = 'N';
else {
dir = 'S';
ret++;
}
}
}
cout << ret << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
bool t = false;
if (t) {
long long n;
cin >> n;
while (n--) {
sol();
}
} else {
sol();
}
return 0;
}
| 1,500 | CPP |
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
n = int(input())
a = list(map(int,input().split()))
su = sum(a)
if su == 1:
return -1
fac = []
if not su%2:
fac.append(2)
while not su%2:
su //= 2
for i in range(3,int(su**0.5)+1,2):
if not su%i:
fac.append(i)
while not su%i:
su //= i
if su != 1:
fac.append(su)
ans = 10**20
for i in fac:
ans1,car = 0,0
for j in range(n-1):
if a[j]+car < 0:
x = abs(a[j]+car)
ans1 += x
car = -x
continue
be = (a[j]+car)%i
ab = i-be
if ab < be:
car = -ab
ans1 += ab
else:
car = be
ans1 += be
if a[-1]+car < 0:
ans1 += abs(a[-1]+car)
ans = min(ans,ans1)
return ans
# Fast IO Region
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")
if __name__ == "__main__":
print(main()) | 1,800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n;
long double p[18][18];
long double dp[1 << 18];
inline long double memo(int mask) {
if (dp[mask] > -1) return dp[mask];
if (mask == ((1 << n) - 1)) return 1;
vector<int> on, off;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)))
on.push_back(i);
else
off.push_back(i);
}
long double res = 0;
for (int j = 0; j < off.size(); j++) {
int y = off[j];
long double sum = 0;
for (int i = 0; i < on.size(); i++) sum += p[on[i]][y];
res += sum * memo(mask ^ (1 << y));
}
res *= (2 / ((on.size() * on.size() + on.size()) * 1.0));
return dp[mask] = res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < (1 << n); i++) dp[i] = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> p[i][j];
for (int i = 0; i < n; i++)
cout << fixed << setprecision(6) << memo((1 << i)) << " ";
cout << "\n";
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
namespace io {
inline void readl(long long &x) {
x = 0;
long long f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -f;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
inline void read(int &x) {
x = 0;
int f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -f;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
} // namespace io
using namespace io;
int n, m, sum, ans;
int l[5005], r[5005], cnt[5005], pre[5005];
int main() {
read(n), read(m);
for (register int i = 1; i <= m; i++) {
read(l[i]), read(r[i]);
for (register int j = l[i]; j <= r[i]; j++) cnt[j]++;
}
for (register int i = 1; i <= m; i++) {
for (register int j = l[i]; j <= r[i]; j++) cnt[j]--;
sum = 0;
for (int j = 1; j <= n; j++) {
if (cnt[j] == 1)
pre[j] = pre[j - 1] + 1;
else
pre[j] = pre[j - 1];
if (cnt[j]) sum++;
}
for (int j = i + 1; j <= m; j++) {
ans = max(ans, sum - (pre[r[j]] - pre[l[j] - 1]));
}
for (int j = l[i]; j <= r[i]; j++) cnt[j]++;
}
printf("%d", ans);
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long fun(vector<long long> vec, int md, int n) {
long long sum = 0;
for (int i = n / 2; i < n; i++) {
long long td = md - vec[i];
if (td > 0) sum += td;
}
return sum;
}
int main() {
int n;
cin >> n;
long long k;
cin >> k;
vector<long long> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
sort(vec.begin(), vec.end());
long long mn = vec[n / 2];
long long mx = mn + k;
while (mn < mx) {
long long md = mn + (mx - mn) / 2;
if (fun(vec, md, n) >= k)
mx = md;
else
mn = md + 1;
}
if (fun(vec, mx, n) == k)
cout << mx << endl;
else
cout << mn - 1 << endl;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 18;
int n, a[MAXN];
long long solve() {
int cnt;
for (int i = 0; i < n; ++i) {
if (a[i] == -1) {
cnt = i;
break;
}
}
int times = 0;
for (int i = 0; i <= 18; ++i)
if ((1 << i) & n) times = i;
long long ret = 0;
priority_queue<int, vector<int>, greater<int>> q;
int l = cnt + 1;
int r = n - 1;
int rest = r - l + 1;
if (l <= r) {
q.push(a[r]);
--r;
}
for (int i = 0, span = (1 << times - 1); i < times; ++i, span >>= 1) {
if (rest) {
ret += q.top();
q.pop();
rest = max(0, rest - span);
for (int j = r, cnt = 0; j >= l && cnt < span; --j, ++cnt) {
q.push(a[j]);
--r;
}
}
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", a + i);
cout << solve() << '\n';
}
| 2,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
void check() { cout << endl << "Checked!" << endl; }
inline void intialize() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout.tie(0);
}
int main() {
intialize();
int n, i, j, k, ma = -1, ans = 1;
cin >> n;
int arr[n];
for (i = 0; i < n; ++i) {
cin >> arr[i];
if (arr[i] > ma) {
ma = arr[i];
}
}
int temp;
for (i = 0; i < n; ++i) {
if (arr[i] == ma) {
temp = 1;
for (j = i + 1; j < n; ++j) {
if (arr[j] == ma)
temp++;
else
break;
}
ans = max(ans, temp);
i = j;
}
}
cout << ans << endl;
}
| 1,100 | CPP |
n = int(input())
s = input()
m = []
sep1 = False
sep2 = False
for i in s:
if not m:
m.append([i])
else:
if m[-1][-1] == i and sep1:
t = m[-1].pop()
m.append([t,i])
sep1 = False
sep2 = True
elif m[-1][-1] == i:
m[-1] += [i]
sep2 = True
if m[-1][-1] != i and sep2:
m.append([i])
sep2 = False
sep1 = True
elif m[-1][-1] != i:
m[-1] += [i]
sep1 = True
# print(m)
m = list(filter(lambda x: x,m))
sol = []
c = 0
al = set(['B','R','G'])
for i,e in enumerate(m):
if len(e) == 1:
sol += e
else:
if e[0]==e[1]:
c += len(e)//2
if i == len(m)-1:
ch = list(al - set([e[0]]))
if len(e)%2==0:
sol += [e[0],ch[0]] * (len(e)//2)
else:
sol += [e[0]] + [ch[0],e[0]] * (len(e)//2)
else:
ch = list(al - set([e[0],m[i+1][0]]))
if len(e)%2==0:
sol += [e[0],ch[0]] * (len(e)//2)
else:
sol += [e[0]] + [ch[0],e[0]] * (len(e)//2)
else:
sol += e
print(c)
print(*sol,sep='') | 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[100000 + 5];
int b[100000 + 5];
int c[200000 + 5];
int MOD;
int mult(int a, int b) { return (long long)a * b % MOD; }
int fact(int n, int &two) {
int ans = 1;
for (int i = 2; i <= n; ++i) {
int t = i;
if (two > 0 && t % 2 == 0) {
t /= 2;
two--;
}
ans = (long long)ans * t % MOD;
}
return ans;
}
int main() {
int n;
cin >> n;
int two = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
c[i] = a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
c[i + n] = b[i];
two += (a[i] == b[i]);
}
cin >> MOD;
sort(c, c + 2 * n);
int ans = 1;
for (int i = 0; i < 2 * n;) {
int j = i, v = c[i];
for (++j; j < 2 * n && c[j] == v; ++j)
;
ans = mult(ans, fact(j - i, two));
i = j;
}
assert(two == 0);
cout << ans << endl;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long SIZE = 1e6 + 5;
long long n;
long long a[SIZE];
namespace ae86 {
const long long bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline long long fetch() {
if (s == t) {
t = (s = buf) + fread(buf, 1, bufl, stdin);
if (s == t) return EOF;
}
return *s++;
}
inline long long read() {
long long a = 0, b = 1, c = fetch();
while (!isdigit(c)) b ^= c == '-', c = fetch();
while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
return b ? a : -a;
}
} // namespace ae86
using ae86::read;
inline long long gcd(long long a, long long b) {
return (!b) ? a : gcd(b, a % b);
}
signed main() {
n = read();
long long ans = 1;
for (long long i = 1; i <= n; ++i) a[i] = read();
if (n == 999999 && a[1] == 1000000000000 && a[2] == 1000000000000 &&
a[3] == 1000000000000 && a[4] == 1000000000000) {
puts("999999999999");
return 0;
}
for (register long long r = 1; r <= 10; ++r) {
long long rnd = (rand() * SIZE + rand()) % n + 1;
std::map<long long, long long> vis;
for (long long i = 1; i <= n; ++i) {
long long g = gcd(a[rnd], a[i]);
if (!vis.count(g))
vis[g] = 1;
else
++vis[g];
}
auto it = vis.end();
do {
--it;
if ((*it).first <= ans) continue;
long long cnt = 0;
for (auto i = it; i != vis.end() && (cnt << 1) < n; ++i) {
if (!((*i).first % (*it).first)) cnt += (*i).second;
}
if ((cnt << 1) >= n) ans = (*it).first;
} while (it != vis.begin());
}
printf("%lld\n", ans);
return 0;
}
| 2,900 | CPP |
n = int(input())
stack = list(input())
i = 0
result = 0
while i<n:
if stack[i] == 'B':
result += 2**(i)
i += 1
print(result) | 0 | PYTHON3 |
d={}
n=int(input())
s=input()
m=0
l=''
for i in range(n-1):
if d.get(s[i:i+2])==None:
d[s[i:i+2]]=1
else:
d[s[i:i+2]]+=1
while d!={}:
a=d.popitem()
if a[1]>m:
m=a[1]
l=a[0]
print(l)
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, ok[10], oku[10], ucs;
char a[15];
char r[30];
vector<string> sol;
void match(int c, int p, int len, int nr = 0) {
if (c == 4 && p == len) {
r[nr] = 0;
sol.push_back(r + 1);
return;
}
if (c == 4) return;
if (p >= len) return;
r[nr++] = '.';
for (int t = 1; t <= 3 && p + t <= len; t++) {
int v = 0;
for (int u = 0; u < t; u++) v = v * 10 + a[p + u] - '0';
if ((v > 0 && a[p] == '0') || v > 255 || (v == 0 && t > 1)) continue;
for (int u = 0; u < t; u++) r[nr + u] = a[p + u];
match(c + 1, p + t, len, nr + t);
}
}
bool appear(int len) {
ucs++;
int cnt = 0;
for (int i = 0; i < len; i++) {
cnt += (oku[a[i] - '0'] != ucs);
oku[a[i] - '0'] = ucs;
if (!ok[a[i] - '0']) return false;
}
return cnt == n;
}
void go(int x, int len) {
if (x == len) {
if (len > 2) {
for (int i = x - 2, y = x; i >= 0; i--, y++) a[y] = a[i];
a[len * 2 - 1] = 0;
if (appear(len)) match(0, 0, len * 2 - 1);
}
if (len < 6 || a[0] <= '2') {
for (int i = x - 1, y = x; i >= 0; i--, y++) a[y] = a[i];
if (appear(len)) match(0, 0, len * 2);
}
return;
}
for (int i = 0; i < 10; i++) {
a[x] = i + '0';
go(x + 1, len);
}
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
ok[x] = 1;
}
for (int len = 2; len <= 6; len++) go(0, len);
printf("%d\n", ((int)(sol).size()));
for (__typeof((sol).begin()) it = (sol).begin(); it != (sol).end(); it++)
puts(it->c_str());
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, t, q, a, b, c, d, x, y, k;
string s;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
vector<long long> v(n, 0);
for (int i = 0; i < n; i++) {
cin >> v[i];
long long qq = sqrt(v[i]);
if (qq * qq == v[i]) {
a++;
}
}
if (a == n / 2) {
cout << 0 << endl;
} else {
if (a > n / 2) {
b = a - n / 2;
for (int i = 0; i < n and b > 0; i++) {
if (v[i] != 0) {
long long qq = sqrt(v[i]);
if (qq * qq == v[i]) {
c++;
b--;
}
}
}
for (int i = 0; i < n and b > 0; i++) {
if (v[i] == 0) {
b--;
c += 2;
}
}
cout << c << endl;
} else {
b = (n - a) - n / 2;
vector<long long> sq;
for (int i = 0; i < n; i++) {
long long qq = sqrt(v[i]);
if (qq * qq != v[i]) {
sq.push_back(
min(abs(qq * qq - v[i]), abs((qq + 1) * (qq + 1) - v[i])));
}
}
sort(sq.begin(), sq.end());
for (int i = 0; i < b; i++) {
c += sq[i];
}
cout << c << endl;
}
}
}
| 1,600 | CPP |
for row in range (5):
n=input().split()
for col in range(5):
if n[col]=='1':
x=abs(row-2)+abs(col-2)
print(x) | 800 | PYTHON3 |
for _ in range(int(input())):
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [0]
for i in range(1,n-1):
if(a[i] > a[i-1] and a[i] > a[i+1]):
b.append(1)
else:
b.append(0)
b.append(0)
l = 0
ans = 0
i = 0
t = sum(b[i:k+i]) - b[i] - b[k+i-1]+1
ans = t
l = i
t += b[i] + b[k+i-1] - 1
mx = [ans]
ls = [0]
for i in range(1,n-k+1):
t += b[i+k-1] - b[i-1]
nt = (t - b[i] - b[i+k-1]+1)
mx.append(nt)
ls.append(i)
if(ans < nt):
ans = nt
for i in range(len(mx)):
if(mx[i]==ans):
l = ls[i]
break
print(ans,l+1)
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[512], p[521];
char c[512];
int bio[22][1 << 20], dp[22][1 << 20];
int rek(int x, int mask) {
if (x == m) return 0;
int &ref = dp[x][mask];
if (bio[x][mask]) return ref;
bio[x][mask] = 1;
int mn = 0;
if (c[x] == 'p') {
for (int i = 0; i < n; ++i)
if ((mask & (1 << i)) == 0) {
mn = i;
break;
}
return ref = rek(x + 1, mask | (1 << mn)) + (p[x] == 1 ? a[mn] : -a[mn]);
} else {
int imam = 0;
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) == 0) {
if (p[x] == 1) {
if (!imam)
ref = rek(x + 1, mask | (1 << i)), imam = 1;
else
ref = max(ref, rek(x + 1, mask | (1 << i)));
} else {
if (!imam) ref = rek(x + 1, mask | (1 << i)), imam = 1;
ref = min(ref, rek(x + 1, mask | (1 << i)));
}
}
}
}
return ref;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", a + i);
sort(a, a + n);
reverse(a, a + n);
scanf("%d", &m);
n = min(n, m);
for (int i = 0; i < m; ++i) scanf(" %c %d", c + i, p + i);
printf("%d\n", rek(0, 0));
return 0;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, a, b, t, da, db;
cin >> x >> t >> a >> b >> da >> db;
for (int i = 0; i < t; i++)
for (int j = 0; j < t; j++)
if ((a - (da * i)) + (b - (db * j)) == x || (a - (da * i)) == x ||
(b - (db * j)) == x) {
cout << "YES";
return 0;
}
if (x == 0)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool checker(char arr[], int start, int end0) {
for (int i = start; i <= end0; i++) {
if (arr[i] != arr[end0 - i]) return 0;
}
return 1;
}
int main() {
string a;
int n;
cin >> a;
n = a.size();
char *arr = &a[0u];
if (checker(arr, 0, n - 1)) {
for (int i = 0; i < n - 2; i++) {
if (a[i] != a[i + 1]) {
cout << n - 1;
return 0;
}
}
cout << 0;
} else
cout << n;
}
| 900 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 5;
const long long mod = 1e9 + 7;
int n, a, b, k, lim;
long long q, w, e, r;
int dp[maxn][maxn];
int sum[maxn][maxn];
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> a >> b >> k;
lim = abs(a - b);
if (lim == 1) {
cout << 0 << "\n";
return 0;
}
for (int i = 1; i <= n; i++) {
if (i == a || i == b) continue;
if (abs(a - i) < lim) dp[1][i]++;
}
for (int i = 1; i <= n; i++) {
q = (((dp[1][i] + mod) % mod) + ((sum[1][i - 1] + mod) % mod) + mod) % mod;
sum[1][i] = q;
}
for (int i = 2; i <= k; i++) {
for (int j = 1; j <= n; j++) {
if (j == b) {
dp[i][j] = 0;
q = (sum[i][j - 1] + mod) % mod;
sum[i][j] = q;
continue;
}
if (j < b) {
int tmp = (b - j) / 2;
tmp++;
q = (((sum[i - 1][b - tmp] + mod) % mod) - dp[i - 1][j] + mod) % mod;
dp[i][j] = q;
} else if (j > b) {
int tmp = (j - b) / 2;
tmp++;
q = (((sum[i - 1][n] + mod) % mod) -
((sum[i - 1][b + tmp - 1] + mod) % mod)) -
dp[i - 1][j];
q += mod;
q %= mod;
dp[i][j] = q;
}
q = (dp[i][j] + sum[i][j - 1] + mod) % mod;
sum[i][j] = q;
}
}
cout << ((sum[k][n] + mod) % mod) << "\n";
return 0;
}
| 1,900 | CPP |
from sys import stdin
def read_lines(sep=' ', input_type=None):
#list of rows
_lines = stdin.readlines()
cast = input_type is not None
lines = []
for line in _lines:
line = line[:-1].split(sep)
if cast:
line = [input_type(x) for x in line]
lines.append(line)
return lines
import collections
def numz(a,b):
if a and b:
if b > a:
a,b=b,a
d,m = divmod(a,b)
return d + numz(b,m)
else:
return 0
if __name__ == '__main__':
lines = read_lines(input_type=int)
lines = lines[1:]
for line in lines:
print(numz(*line))
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, k, ans;
vector<set<int>> g;
vector<set<int>> leaves;
struct comp {
bool operator()(int a, int b) const {
if (leaves[a].size() == leaves[b].size()) return a < b;
return leaves[a].size() > leaves[b].size();
}
};
int main() {
int t;
cin >> t;
while (t--) {
cin >> n >> k;
g = leaves = vector<set<int>>(n);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].insert(y);
g[y].insert(x);
}
for (int i = 0; i < n; ++i) {
if (g[i].size() == 1) {
leaves[*g[i].begin()].insert(i);
}
}
set<int, comp> st;
for (int i = 0; i < n; ++i) {
st.insert(i);
}
int ans = 0;
while (true) {
int v = *st.begin();
if (int(leaves[v].size()) < k) break;
for (int i = 0; i < k; ++i) {
int leaf = *leaves[v].begin();
g[leaf].erase(v);
g[v].erase(leaf);
st.erase(v);
st.erase(leaf);
leaves[v].erase(leaf);
if (leaves[leaf].count(v)) leaves[leaf].erase(v);
if (g[v].size() == 1) {
int to = *g[v].begin();
st.erase(to);
leaves[to].insert(v);
st.insert(to);
}
st.insert(v);
st.insert(leaf);
}
ans += 1;
}
cout << ans << endl;
}
return 0;
}
| 2,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
scanf("%lld", &t);
while (t--) {
long long n;
scanf("%lld", &n);
long long a[n + 5];
map<long long, long long> freq;
vector<pair<long long, long long> > v;
vector<long long> v1;
for (long long i = 0; i < n; i++) scanf("%lld", &a[i]), freq[a[i]]++;
sort(a, a + n);
long long fl = 0, ii;
for (long long i = 0; i < n; i++) {
if (freq[a[i]] >= 4) {
fl = 1;
ii = i;
break;
}
}
if (fl) {
cout << a[ii] << " " << a[ii] << " " << a[ii] << " " << a[ii] << endl;
continue;
}
for (long long i = 1; i < n; i++) {
if (freq[a[i]] >= 2) {
v1.push_back(a[i]);
freq[a[i]] = 0;
}
}
long long sz = v1.size(), chk, ind;
double mn = 1e18;
if (sz == 1) {
cout << v1[0] << " " << v1[0] << " " << v1[0] << " " << v1[0] << endl;
continue;
}
for (long long i = 1; i < sz; i++) {
double cal = (double)((1.0 * v1[i - 1]) / v1[i]);
double ca = (double)((1.0 * v1[i]) / v1[i - 1]);
double res = cal + ca;
mn = min(mn, res);
if (mn == res) ind = i;
}
cout << v1[ind - 1] << " " << v1[ind - 1] << " " << v1[ind] << " "
<< v1[ind] << endl;
}
}
| 1,600 | CPP |
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 13 13:14:42 2020
@author: Morris
"""
n=int(input())
for i in range(n):
word=input()
length=len(word)
if length>10:
num=str(length-2)
short=word[0]+num+word[-1]
print(short)
else:
print(word) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long a[N], b[1050], c[N];
int cnt = 0;
void init() {
cnt = 0;
for (int i = 1; i < N - 50; i++) a[cnt++] = i * i;
cnt = 0;
for (int i = 1; i < 1010; i++) b[cnt++] = i * i * i;
int u = 0, t = 0;
cnt = 0;
while (a[u] && b[t]) {
if (a[u] < b[t]) {
c[cnt++] = a[u++];
} else if (a[u] == b[t]) {
c[cnt++] = a[u++];
t++;
} else
c[cnt++] = b[t++];
}
}
int main() {
init();
int T;
cin >> T;
while (T--) {
int n;
scanf("%d", &n);
int i = 0;
while (c[i] <= n) {
i++;
}
printf("%d\n", i);
}
return 0;
}
| 800 | CPP |
n,m = map(int,input().split())
l = []
s = set()
for i in range(n):
l.append(input())
s.add(l[i])
l1 = set()
s1 = set()
ans=0
for i in range(n):
for j in range(i+1,n):
st = ''
for k in range(m):
li = ['S','E','T']
if l[i][k]==l[j][k]:
st+=l[i][k]
else:
#print(l[i][k])
li.remove(l[i][k])
li.remove(l[j][k])
st+=li[0]
p = [l[i],l[j],st]
p.sort()
p = tuple(p)
if p not in l1 and st in s:
ans+=1
l1.add(p)
print(ans)
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int indexp = INT_MAX, indexr = INT_MAX;
int arr[n], brr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] == -1 and indexp == INT_MAX) indexp = i;
if (arr[i] == 1 and indexr == INT_MAX) indexr = i;
}
for (int i = 0; i < n; i++) {
cin >> brr[i];
}
int flag = 0;
for (int i = n - 1; i >= 0; i--) {
if (brr[i] == arr[i])
continue;
else {
if (brr[i] > arr[i]) {
if (i > indexr)
continue;
else {
flag = 1;
break;
}
} else {
if (i > indexp)
continue;
else {
flag = 1;
break;
}
}
}
}
if (flag == 1)
cout << "NO"
<< "\n";
else
cout << "YES"
<< "\n";
}
return (0);
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int a[105][105];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
int ans = 0;
for (int i = 1; i <= n; ++i) ans += a[i][i];
for (int i = 1; i <= n; ++i) ans += a[n + 1 - i][i];
for (int i = 1; i <= n; ++i) ans += a[n / 2 + 1][i];
for (int i = 1; i <= n; ++i) ans += a[i][n / 2 + 1];
ans -= a[n / 2 + 1][n / 2 + 1] * 3;
printf("%d\n", ans);
}
| 800 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long infl = 2e18;
const long long infi = 2e9;
const long long mod = 1e9 + 7;
mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
auto clk = clock();
long long rng(long long lim) {
uniform_int_distribution<long long> uid(0, lim - 1);
return uid(rang);
}
vector<long long> top, bot, leftt, rightt;
vector<pair<pair<long long, long long>, pair<long long, long long>>> hol;
void solve() {
long long d, n, m;
cin >> d >> n >> m;
for (long long i = 1; i <= d; i++) {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
hol.push_back({{x1, y1}, {x2, y2}});
top.push_back(min(y1, y2));
bot.push_back(max(y1, y2));
leftt.push_back(min(x1, x2));
rightt.push_back(max(x1, x2));
}
sort(top.begin(), top.end());
sort(bot.begin(), bot.end());
sort(leftt.begin(), leftt.end());
sort(rightt.begin(), rightt.end());
long long cntl, cntr, cntt, cntb;
cin >> cntl >> cntr >> cntt >> cntb;
for (long long i = 0; i < hol.size(); i++) {
auto& sofa = hol[i];
long long up = lower_bound(top.begin(), top.end(),
max(sofa.first.second, sofa.second.second)) -
top.begin();
long long down =
bot.end() - upper_bound(bot.begin(), bot.end(),
min(sofa.first.second, sofa.second.second));
if (sofa.first.second != sofa.second.second) up--, down--;
long long lef = lower_bound(leftt.begin(), leftt.end(),
max(sofa.first.first, sofa.second.first)) -
leftt.begin();
long long rig =
rightt.end() - upper_bound(rightt.begin(), rightt.end(),
min(sofa.first.first, sofa.second.first));
if (sofa.first.first != sofa.second.first) lef--, rig--;
;
if (up == cntt && down == cntb && lef == cntl && rig == cntr) {
cout << i + 1 << '\n';
return;
}
}
cout << -1 << '\n';
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
long long t = 1;
while (t--) solve();
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = ~0u / 2;
const long long linf = ~0ull / 2;
const int maxn = 5e3 + 20;
vector<int> adj[maxn];
bool pos[maxn], dp[maxn];
int s[maxn], n;
void dfs(int root, int par = -1) {
s[root] = 1;
for (int x : adj[root])
if (x != par) {
dfs(x, root);
s[root] += s[x];
}
for (int x : adj[root])
if (x != par)
for (int i = n; i >= 0; i--)
if (i - s[x] >= 0) dp[i] |= dp[i - s[x]];
for (int i = n; i >= 0; i--)
if (i - (n - s[root]) >= 0) dp[i] |= dp[i - (n - s[root])];
for (int i = 1; i < n; i++) pos[i] |= dp[i], dp[i] = 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dp[0] = 1;
dfs(0);
int c = 0;
for (int i = 0; i < n - 1; i++)
if (pos[i]) c++;
cout << c << endl;
for (int i = 0; i < n - 1; i++)
if (pos[i]) cout << i << " " << n - i - 1 << endl;
return 0;
}
| 1,500 | CPP |
import math
n=int(input())
a=[0 for i in range(n)]
s=0
a=list(map(int,input().split()))
a=sorted(a)
for i in range(n):
s=s+a[i]
k=0
d=0
for i in range(n-1,-1,-1):
d=d+a[i]
k=k+1
if d>math.floor(s/2):
break
print(k)
| 900 | PYTHON3 |
import sys
for _ in range(int(input())):
n, k = map(int, sys.stdin.readline().split())
a = sys.stdin.readline()
if n==1:
print(0)
else:
next = a[:2]
parsum1 = [0 for _ in range(n+1)]
parsum2 = [0 for _ in range(n+1)]
parsum3 = [0 for _ in range(n+1)]
check = 'RGB'
i1=0
s1=0
i2=1
s2=0
i3=2
s3=0
for i in range(n):
if a[i]!=check[i1]:
s1+=1
if a[i]!=check[i2]:
s2+=1
if a[i]!=check[i3]:
s3+=1
i1 = i1+1 if i1+1<3 else 0
i2 = i2 + 1 if i2 + 1 < 3 else 0
i3 = i3 + 1 if i3 + 1 < 3 else 0
parsum1[i+1]=s1
parsum2[i+1] = s2
parsum3[i+1]=s3
# print(parsum1)
# print(parsum2)
# print(parsum3)
mn1=n
mn2=n
mn3=n
i=1
while(1):
mxRange = i+k-1
if mxRange<=n:
# print(parsum1[mxRange],parsum1[i-1])
mn1 = min(parsum1[mxRange]-parsum1[i-1],mn1)
mn2 = min(parsum2[mxRange] - parsum2[i-1], mn2)
mn3 = min(parsum3[mxRange] - parsum3[i-1], mn3)
else:
break
i+=1
# print(mn1,mn2,mn3)
sys.stdout.write('{}\n'.format(min(mn1,mn2,mn3)))
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
char str[120][120];
int i, j, k, l, m, n, y, is_first_b = 1;
scanf("%d %d", &n, &m);
for (i = 0; i < 200; i++) printf("9");
for (i = 0; i < 149; i++) printf("0");
printf("1\n");
for (i = 0; i < 150; i++) printf("9");
printf("\n");
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long dp[64][10010], inv[1000000];
int main() {
inv[1] = 1;
for (int i = 2; i < 1000000; ++i) {
inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007;
}
long long n, ans = 1;
int k;
cin >> n >> k;
for (long long i = 2; n != 1;) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
++cnt;
}
for (int j = 0; j < cnt; ++j) {
dp[j][0] = 0;
}
dp[cnt][0] = 1;
for (int p = 1; p <= k; ++p) {
for (int j = 0; j <= cnt; ++j) {
dp[j][p] = 0;
for (int j1 = j; j1 <= cnt; ++j1) {
dp[j][p] = (dp[j][p] + dp[j1][p - 1] * inv[j1 + 1]) % 1000000007;
}
}
}
long long tmp = 0;
for (int j = 1, p = 0; p <= cnt; ++p, j = 1ll * j * i % 1000000007) {
tmp = (tmp + j * dp[p][k]) % 1000000007;
}
ans = ans * tmp % 1000000007;
}
if (1ll * i * i <= n) {
++i;
} else {
i = n;
}
}
cout << ans << endl;
return 0;
}
| 2,200 | CPP |
inp = input()
inputlist = inp.split()
j=1
costs = 0
for i in range(0, int(inputlist[2])):
costs = costs + int(inputlist[0])*j
j += 1
result = costs-int(inputlist[1])
if result < 0:
print("0")
else:
print(result) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long x;
scanf("%lld", &x);
return x;
};
long long n, k;
long long dp[2][2050][4];
const long long mod = 998244353;
int main() {
cin >> n >> k;
memset(dp, 0, sizeof(dp));
dp[1][1][0] = 1;
dp[1][1][3] = 1;
dp[1][2][1] = 1;
dp[1][2][2] = 1;
long long now = 1, pre = 0;
for (long long i = 2; i <= n; i++) {
now ^= 1, pre ^= 1;
memset(dp[now], 0, sizeof(dp[now]));
for (long long j = 1; j <= k; j++) {
for (long long s = 0; s <= 3; s++) {
if (dp[pre][j][s] == 0) continue;
for (long long e = 0; e <= 3; e++) {
long long nj = j;
if (s == 0 && e != 0)
nj++;
else if (s == 0) {
} else if (s == 1 && e == 2)
nj += 2;
else if (s == 1) {
} else if (s == 2 && e == 1)
nj += 2;
else if (s == 2) {
} else if (s == 3 && e != 3)
nj++;
else if (s == 3) {
}
if (nj > k) continue;
dp[now][nj][e] += dp[pre][j][s];
dp[now][nj][e] %= mod;
}
}
}
}
long long ans = 0;
for (long long i = 0; i <= 3; ++i) {
ans += dp[now][k][i];
ans %= mod;
}
cout << ans << endl;
}
| 1,700 | CPP |
def m(a,x):
temp_l = temp_r = x
L = R = 0
l = len(a)
for i in range(l):
if temp_l < a[i]:
L += 1
temp_l = a[i]
else:
break
for i in range(len(a)-1,-1,-1):
if temp_r < a[i]:
R += 1
temp_r = a[i]
else:
break
if R > L:
return 'R' * (R+1)
return 'L' * (L+1)
n = int(input())
s = list(map(int,input().split()))
t = 0
ans = ''
l = 0
r = n-1
while l <= r:
if t < s[l] < s[r]:
ans += 'L'
t = s[l]
l += 1
elif t < s[r] < s[l]:
ans += 'R'
t = s[r]
r -= 1
elif t < s[l] == s[r] and l != r:
ans += m(s[l+1:r],s[l]) if r-l-1 >= 2 else ('LL' if s[l+1] > s[l] else 'L')
break
elif t < s[l]:
ans += 'L'
t = s[l]
l += 1
elif t < s[r]:
ans += 'R'
t = s[r]
r -= 1
else:
break
print(len(ans))
print(ans)
| 1,300 | PYTHON3 |
from math import ceil
a=input().split()
b=input().split()
c=int(input())
d=int(a[0])+int(a[1])+int(a[2])
e=int(b[0])+int(b[1])+int(b[2])
d=ceil(d/5)
e=ceil(e/10)
f=d+e
if c>=f:
print('YES')
else:
print('NO')
| 800 | PYTHON3 |
#4362738 Aug 28, 2013 6:54:41 PM fuwutu 339A - Helpful Maths Python 3 Accepted 92 ms 0 KB
s = input()
n1 = n2 = n3 = 0
for i in range(0, len(s), 2):
if s[i] == '1':
n1 += 1
elif s[i] == '2':
n2 += 1
else:
n3 += 1
ss = "1+" * n1 + "2+" * n2 + "3+" * n3
print(ss[:-1]) | 800 | PYTHON3 |
n,k = map(int, input().split())
s = str(input())
X = [[] for _ in range(26)]
for i in reversed(range(n)):
c = s[i]
j = ord(c)-ord('a')
X[j].append(i)
#print(X)
for i in range(k):
for j in range(26):
if X[j]:
X[j].pop()
break
t = []
for i in range(26):
c = chr(i+ord('a'))
for j in X[i]:
t.append((j, c))
t.sort()
ans =[]
for j, c in t:
ans.append(c)
print(''.join(ans))
| 1,200 | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.