solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
n = int(input())
arr = list(map(int,input().split()))
info = 0
num =0
count = -1
while num != n:
count += 1
for i in range(n):
if arr[i]>=0 and arr[i]<=info:
arr[i] = -1
info += 1
num += 1
arr.reverse()
print(count)
| 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std ;
const int maxn = 3e5 + 10 ;
int n , m ;
vector<int> g[maxn] ;
int dep[maxn] , fa[maxn][25] ;
int now ;
int dfn[maxn] , sz[maxn] ;
vector<pair<int , int>> s[maxn] ;
int c[maxn] ; // c[u]表示一个端点在u子树内部,另一个端点在u子树外部
map<int , int> d[maxn] ;
void dfs1(int f , int u , int deep)
{
dfn[u] = ++ now ;
sz[u] = 1 ;
dep[u] = deep ;
for(int i = 1 ; i <= 20 ; i ++)
{
int nxt = fa[u][i - 1] ;
fa[u][i] = fa[nxt][i - 1] ;
}
for(auto v : g[u])
{
if(v == f) continue ;
fa[v][0] = u ;
dfs1(u , v , deep + 1) ;
sz[u] += sz[v] ;
s[u].push_back({dfn[v] , v}) ;
}
}
int lca(int x , int y)
{
if(dep[x] < dep[y]) swap(x , y) ;
for(int i = 20 ; i >= 0 ; i --)
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i] ;
if(x == y) return x ;
for(int i = 20 ; i >= 0 ; i --)
if(fa[x][i] != fa[y][i])
x = fa[x][i] , y = fa[y][i] ;
return fa[x][0] ;
}
vector<pair<int , int>> p[maxn] ;
int cnt[maxn] ;
int change(int lc , int u)
{
pair<int , int> tt = {dfn[u] , 1000000000} ;
auto it = upper_bound(s[lc].begin() , s[lc].end() , tt) ;
it -- ;
d[lc][(*it).second] ++ ;
return (*it).second ;
}
long long cal1() //same lca
{
long long res = 0 ;
for(int i = 1 ; i <= n ; i ++)
{
int siz = p[i].size() ;
sort(p[i].begin() , p[i].end()) ;
res += 1ll * siz * (siz - 1) ;
for(int j = 0 ; j < siz ; j ++)
{
if(p[i][j].first != -1) cnt[p[i][j].first] ++ ;
if(p[i][j].second != -1) cnt[p[i][j].second] ++ ;
}
for(int j = 0 ; j < siz ; j ++)
{
int k = j ;
while(k + 1 < siz && p[i][k + 1] == p[i][k]) k ++ ;
for(int t = j ; t <= k ; t ++)
{
if(p[i][t].first == -1 && p[i][t].second == -1) continue ;
else if(p[i][t].second == -1)
{
int num = cnt[p[i][t].first] - 1 ;
res -= num ;
}
else
{
int num = cnt[p[i][t].first] - 1 ;
num += cnt[p[i][t].second] - 1 ;
num -= (k - j + 1) - 1 ;
res -= num ;
}
}
j = k ;
}
for(int j = 0 ; j < siz ; j ++)
{
if(p[i][j].first != -1) cnt[p[i][j].first] = 0 ;
if(p[i][j].second != -1) cnt[p[i][j].second] = 0 ;
}
}
return res / 2 ;
}
long long cal2(int fa , int u) //not same lca
{
long long res = 0 ;
for(auto v : g[u])
{
if(v == fa) continue ;
res += cal2(u , v) ;
int res2 = c[v] - d[u][v] ; //一个端点在v子树内部,另一个端点在u子树外部。
c[u] += res2 ;
int cc = p[u].size() - d[u][v] ;
res += 1ll * res2 * cc ;
}
return res ;
}
int main()
{
std::ios::sync_with_stdio(false) , cin.tie(0) ;
cin >> n ;
for(int i = 1 ; i <= n - 1 ; i ++)
{
int u , v ;
cin >> u >> v ;
g[u].push_back(v) ;
g[v].push_back(u) ;
}
vector<int> tmp ;
dfs1(1 , 1 , 1) ;
cin >> m ;
for(int i = 1 ; i <= m ; i ++)
{
int u , v ;
cin >> u >> v ;
int lc = lca(u , v) ;
if(u == v) p[u].push_back({-1 , -1}) ;
else if(u == lc)
{
c[v] ++ ;
tmp.push_back(v) ;
p[lc].push_back({change(lc , v) , -1}) ;
}
else if(v == lc)
{
c[u] ++ ;
tmp.push_back(u) ;
p[lc].push_back({change(lc , u) , -1}) ;
}
else
{
c[u] ++ ;
c[v] ++ ;
tmp.push_back(u) ;
tmp.push_back(v) ;
int t1 = change(lc , u) ;
int t2 = change(lc , v) ;
p[lc].push_back({min(t1 , t2) , max(t1 , t2)}) ;
}
}
long long ans = 0 ;
ans += cal1() ;
ans += cal2(1 , 1) ;
for(auto u : tmp) ans += p[u].size() ;
cout << ans << '\n' ;
return 0 ;
} | 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void solve(std::istream& in, std::ostream& out) {
int n;
in >> n;
int ans = n / 2 + n % 2 - 1;
out << ans << "\n";
}
};
void solve(std::istream& in, std::ostream& out) {
out << std::setprecision(12);
Solution solution;
solution.solve(in, out);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
istream& in = cin;
ostream& out = cout;
solve(in, out);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll fnd(ll n)
{
if(n<2050 || n%2050)
return -1;
n/=2050;
ll c=0;
while(n)
{
c+=n%10;
n/=10;
}
return c;
}
int main()
{
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
cout<<fnd(n)<<endl;
}
return 0;
} | 7 | CPP |
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
"""
pppppppppppppppppppp
ppppp ppppppppppppppppppp
ppppppp ppppppppppppppppppppp
pppppppp pppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppp pppppppp
ppppppppppppppppppppp ppppppp
ppppppppppppppppppp ppppp
pppppppppppppppppppp
"""
def cmp(a, b):
if a[1] > b[1]:
return -1
if a[1] < b[1]:
return 1
if a[0][0] < b[0][0]:
return -1
return 1
name = data()
n = int(data())
score = dd(int)
for i in range(n):
arr = sl()
if arr[1] == 'posted':
temp = arr[3][:len(arr[3])-2]
if temp == name:
score[(arr[0], temp)] += 15
elif arr[0] == name:
score[(temp, arr[0])] += 15
else:
score[(temp, name)] += 0
score[(arr[0], name)] += 0
if arr[1] == 'commented':
temp = arr[3][:len(arr[3])-2]
if temp == name:
score[(arr[0], temp)] += 10
elif arr[0] == name:
score[(temp, arr[0])] += 10
else:
score[(temp, name)] += 0
score[(arr[0], name)] += 0
if arr[1] == 'likes':
temp = arr[2][:len(arr[2]) - 2]
if temp == name:
score[(arr[0], temp)] += 5
elif arr[0] == name:
score[(temp, arr[0])] += 5
else:
score[(temp, name)] += 0
score[(arr[0], name)] += 0
score = sorted(score.items(), key=cmp_to_key(cmp))
for i in score:
if i[0][1] == name:
out(i[0][0])
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline int getint() {
static char c;
while ((c = getchar()) < '0' || c > '9')
;
int res = c - '0';
while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
return res;
}
const int MaxN = 500000;
const int MaxM = 500000;
int n, m;
struct edge {
int u, v, l, r;
edge() {}
edge(const int &_u, const int &_v, const int &_l, const int &_r)
: u(_u), v(_v), l(_l), r(_r) {}
friend inline bool operator<(const edge &lhs, const edge &rhs) {
if (lhs.l != rhs.l) return lhs.l > rhs.l;
return lhs.r > rhs.r;
}
};
priority_queue<edge> q;
int last[2][MaxN + 1];
vector<edge> adj[2][MaxN + 1];
inline void extend(int v, int l, int r) {
last[l & 1][v] = max(last[l & 1][v], r);
for (edge &e : adj[l & 1][v]) e.l = l, q.push(e);
adj[l & 1][v].clear();
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u = getint(), v = getint();
int l = getint(), r = getint() - 1;
q.emplace(u, v, l, r - ((l ^ r) & 1));
q.emplace(v, u, l, r - ((l ^ r) & 1));
q.emplace(u, v, l + 1, r - ((l ^ r ^ 1) & 1));
q.emplace(v, u, l + 1, r - ((l ^ r ^ 1) & 1));
}
last[0][1] = 0;
for (int u = 2; u <= n; ++u) last[0][u] = -1;
for (int u = 1; u <= n; ++u) last[1][u] = -1;
while (!q.empty()) {
edge e = q.top();
q.pop();
if (e.l > e.r) continue;
if (e.l > last[e.l & 1][e.u])
adj[e.l & 1][e.u].push_back(e);
else if (e.v != n)
extend(e.v, e.l + 1, e.r + 1);
else {
cout << e.l + 1 << endl;
return 0;
}
}
cout << (n == 1 ? 0 : -1) << endl;
return 0;
}
| 12 | CPP |
for t in range(int(input())):
n = int(input())
if (n==1):
a,b = map(int, input().split())
print(0)
else:
alist = []
blist = []
for i in range(n):
a,b = map(int, input().split())
alist.append(a)
blist.append(b)
print(abs(min(0,(min(blist)-max(alist)))))
| 7 | PYTHON3 |
for t in range(int(input())):
n,k=map(int,input().split())
if k>n:
print(k-n)
elif k==n:
print(0)
elif n>k:
if k%2==0:
if n%2==0:
print(0)
else:
print(1)
elif k%2==1:
if n%2==0:
print(1)
else:
print(0)
| 7 | PYTHON3 |
n = int(input())
a = []
for i in range(n):
r, g, b = map(int, input().split())
if r + g + 1 < b or g + b + 1 < r or r + b + 1 < g:
print('No')
else:
print('Yes')
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
if (false) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int n;
scanf("%d\n", &n);
vector<vector<bool> > a(n, vector<bool>(n, false));
int val;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
scanf("%d ", &val);
a[i][j] = (val > 0 ? true : false);
}
scanf("%d\n", &val);
a[i][n - 1] = (val > 0 ? true : false);
}
vector<bool> row_flipped(n, false);
vector<bool> col_flipped(n, false);
bool sum = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum = sum ^ (a[i][j] & a[j][i]);
}
}
int q;
scanf("%d\n", &q);
for (int i = 0; i < q; i++) {
int x, y;
scanf("%d", &x);
if (x < 3) {
scanf(" %d\n", &y);
sum = !sum;
} else {
printf("%d", (sum ? 1 : 0));
}
}
if (false) {
fclose(stdin);
fclose(stdout);
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, cnt = 0;
cin >> n >> m;
int a[n + 3], b[m + 3];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
int i = 0, j = 0;
long s1 = a[0], s2 = b[0];
while (i < n && j < m) {
if (s1 == s2) {
cnt++;
i++;
j++;
s1 = a[i];
s2 = b[j];
} else if (s1 > s2) {
j++;
s2 += b[j];
} else if (s1 < s2) {
i++;
s1 += a[i];
}
}
cout << cnt;
return 0;
}
| 8 | CPP |
#Author: Adisbek
n,m=map(int,input().split())
k,k2,l=1,1,1
s2=min(n,m)
for i in range(1,s2+1):
if i==s2:k=k*i
k2=k2*i
#for i in range(1,k2+1):
# if k % i==0 and k2 % i==0:l=i
print(k2)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct point {
double x, y;
void in() { scanf("%lf%lf", &x, &y); }
point() {}
point(double a, double b) : x(a), y(b) {}
} p1, p2, cp1[3], cp2[3];
struct line {
double ta, tb, tc;
void in() { scanf("%lf%lf%lf", &ta, &tb, &tc); }
line() {}
line(double a, double b, double c) : ta(a), tb(b), tc(c) {}
point getx(double y) {
double x = (-tc - tb * y) / ta;
return point(x, y);
}
point gety(double x) {
double y = (-tc - ta * x) / tb;
return point(x, y);
}
} l;
double Sqr(double a) { return a * a; }
double dist(point a, point b) { return sqrt(Sqr(a.x - b.x) + Sqr(a.y - b.y)); }
double getalldis(point pp1, point pp2) {
return dist(p1, pp1) + dist(pp1, pp2) + dist(pp2, p2);
}
double ans = 1e12;
double ABS(double a) { return a < 0 ? -a : a; }
double dir(point a, point b) { return ABS(a.x - b.x) + ABS(a.y - b.y); }
int main() {
l.in();
p1.in();
p2.in();
cp1[1] = l.getx(p1.y);
cp1[2] = l.gety(p1.x);
cp2[1] = l.getx(p2.y);
cp2[2] = l.gety(p2.x);
ans = dir(p1, p2);
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
ans = min(ans, getalldis(cp1[i], cp2[j]));
}
}
printf("%.8lf\n", ans);
return 0;
}
| 7 | CPP |
n = int(input())
tests = [ (input(), input(), input()) for _ in range(n)]
ans = []
for _, a, b in tests:
n, k = map(int, _.split())
a = list(map(int, a.split()))
b = list(map(int, b.split()))
a.sort()
b.sort()
for i in range(1, k+1):
if b[-i] > a[i-1]:
a[i-1], b[-i] = b[-i], a[i-1]
ans.append(sum(a))
[print(_) for _ in ans]
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, idx = 0;
int arr[1001][1001];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
if (n < 5) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
idx += arr[i][j];
}
}
cout << idx << endl;
} else if (n >= 5) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
idx += arr[i][j];
else if (i + j == (n - 1))
idx += arr[i][j];
else if (i == (n - 1) / 2 or j == (n - 1) / 2)
idx += arr[i][j];
}
}
cout << idx << endl;
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int INP, AM, REACHEOF;
char BUF[(1 << 12) + 1], *inp = BUF;
const long double PI = acos((long double)-1.0);
const long long MOD = 1000000009LL;
int n, w, b;
long long gt[4011], revgt[4011];
long long power(long long x, long long k) {
if (k == 0) return 1;
if (k == 1) return x % MOD;
long long mid = power(x, k >> 1);
mid = (mid * mid) % MOD;
if (k & 1)
return (mid * x) % MOD;
else
return mid;
}
long long get(int n, int k) {
return gt[n] * gt[n - 1] % MOD * revgt[k - 1] % MOD * revgt[n - k] % MOD;
}
int main() {
gt[0] = 1;
for (int i = (1), _b = (4000); i <= _b; i++) gt[i] = (gt[i - 1] * i) % MOD;
for (int i = (0), _b = (4000); i <= _b; i++) revgt[i] = power(gt[i], MOD - 2);
while (cin >> n >> w >> b) {
long long res = 0;
for (int y = (1), _b = (n - 2); y <= _b; y++)
if (b >= y && n - y <= w) {
int xz = n - y;
long long now = (n - y) - 2 + 1;
now = (now * get(w, xz)) % MOD * get(b, y) % MOD;
res = (res + now) % MOD;
}
cout << res << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int M = 30;
long long cnt[M], pref[M], fact[M];
long long solve(int p, int n);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
memset(cnt, 0, sizeof(cnt));
long long k;
cin >> k;
long long p = 2;
while (k) {
++cnt[k % p];
k /= p;
++p;
}
long long ans = solve(p - 2, p - 2);
if (cnt[0]) {
--cnt[0];
ans -= solve(p - 2, p - 3);
}
cout << ans - 1 << '\n';
}
}
long long solve(int p, int n) {
pref[0] = cnt[0];
for (int i = 1; i <= p; ++i) {
pref[i] = pref[i - 1] + cnt[i];
}
long long ans = 1;
for (int i = 1; i <= n; ++i) {
ans *= (pref[i] - (i - 1));
}
fact[0] = 1;
for (int i = 1; i <= p; ++i) {
fact[i] = (i * fact[i - 1]);
}
for (int i = 0; i <= p; ++i) {
ans /= fact[cnt[i]];
}
return ans;
}
| 17 | CPP |
import sys
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()
import bisect,string,math,time,functools,random,fractions
from bisect import*
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def I():return int(input())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def NLI(n):return [[int(i) for i in input().split()] for i in range(n)]
def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def INP():
N=8
n=random.randint(2,N)
n=6
a=[(random.randint(1,9),random.randint(1,9)) for i in range(n)]
#A=[random.randint(1,n) for i in range(m)]
return a
def Rtest(T):
case,err=0,0
for i in range(T):
inp=INP()
a1=naive(inp)
a2=solve(inp)
if a1!=a2:
print(inp)
print('naive',a1)
print('solve',a2)
err+=1
case+=1
print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
org_inp=[];g=[[] for i in range(V)]
FromStdin=True if ls==None else False
for i in range(E):
if FromStdin:
inp=LI()
org_inp.append(inp)
else:
inp=ls[i]
if len(inp)==2:a,b=inp;c=1
else:a,b,c=inp
if index==1:a-=1;b-=1
aa=(a,c);bb=(b,c);g[a].append(bb)
if not Directed:g[b].append(aa)
return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
mp=[boundary]*(w+2);found={}
for i in R(h):
s=input()
for char in search:
if char in s:
found[char]=((i+1)*(w+2)+s.index(char)+1)
mp_def[char]=mp_def[replacement_of_found]
mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
mp+=[boundary]*(w+2)
return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
rt=[0]
for i in ls:rt+=[rt[-1]+i]
return rt
def bit_combination(n,base=2):
rt=[]
for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
return rt
def gcd(x,y):
if y==0:return x
if x%y==0:return y
while x%y!=0:x,y=y,x%y
return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
if show_flg:print(*inp,end=end)
mo=10**9+7
#mo=998244353
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
alp=[chr(ord('a')+i)for i in range(26)]
#sys.setrecursionlimit(10**7)
show_flg=False
show_flg=True
ans=0
def solve(a,b):
n=a+b
rt=set()
x,y=-~n//2,n//2
for k in range(n+1):
s=x-a
t=y-b
if s+t!=0:
continue
if 0<=k+s<=2*x and 0<=k-s<=2*y and (k+s)%2==0 and (k-s)%2==0:
rt.add(k)
y,x=-~n//2,n//2
for k in range(n+1):
s=x-a
t=y-b
if s+t!=0:
continue
if 0<=k+s<=2*x and 0<=k-s<=2*y and (k+s)%2==0 and (k-s)%2==0:
rt.add(k)
return sorted(rt)
for _ in range(I()):
ans=0
a,b=LI()
ans=solve(a,b)
print(len(ans))
print(*ans)
| 8 | PYTHON3 |
t=int(input())
for case in range(t) :
n=int(input())
List=[int(j) for j in input().split(" ")]
List_1=set(List)
k=0
for i in List_1:
if i!=k:
break
else:
k+=1
List.sort()
List_3=[]
for i in range(1,n) :
if List[i]==List[i-1]:
if List[i] not in List_3:
List_3.append(List[i])
a=0
for i in List_3:
if i!=a:
break
else:
a+=1
print(a+k) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
char entry[1000010];
int last[200];
scanf("%s", entry);
for (int i = 0; i < n; i++) {
last[entry[i]] = i;
}
int visited[200];
int count = 0;
int max = -1;
for (int i = 0; i < n; i++) {
if (!visited[entry[i]]) {
count++;
visited[entry[i]] = 1;
}
max = max > count ? max : count;
if (i == last[entry[i]]) {
count--;
}
}
if (max <= k)
printf("NO\n");
else
printf("YES\n");
}
| 8 | CPP |
import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int, input().split()))
INF = 10 ** 6 - 1
ans = [INF] * n
l = 0
if a[0] == 1:
ans[0] = 0
for i in range(n - 1):
if i + 1 < a[i]:
print(-1)
exit()
if a[i] < a[i + 1]:
if i + 2 < a[i + 1]:
print(-1)
exit()
ans[i + 1] = a[i]
min_num = a[i] + 1
max_num = a[i + 1] - 1
while min_num <= max_num:
if ans[l] == INF:
ans[l] = min_num
min_num += 1
l += 1
print(*ans) | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
struct TreeNode {
int L, R;
pair<double, double> val;
} tree[maxn << 2];
int a[maxn], b[maxn], n, q;
void pushUp(int root) {
tree[root].val.first =
tree[root << 1].val.first * tree[root << 1 | 1].val.first;
tree[root].val.second =
tree[root << 1].val.second +
tree[root << 1].val.first * tree[root << 1 | 1].val.second;
}
void build(int L, int R, int root) {
tree[root].L = L;
tree[root].R = R;
if (L == R) {
return;
}
int mid = (L + R) >> 1;
build(L, mid, root << 1);
build(mid + 1, R, root << 1 | 1);
}
void update(int pos, pair<double, double> val, int root) {
int L = tree[root].L, R = tree[root].R;
if (L == R) {
tree[root].val = val;
return;
}
int mid = (L + R) >> 1;
if (pos <= mid) {
update(pos, val, root << 1);
} else {
update(pos, val, root << 1 | 1);
}
pushUp(root);
}
pair<double, double> query(int ql, int qr, int root) {
int L = tree[root].L, R = tree[root].R;
if (L >= ql && R <= qr) return tree[root].val;
int mid = (L + R) >> 1;
pair<double, double> a = make_pair(1.0, 0.0), b = make_pair(1.0, 0.0), c;
if (ql <= mid) {
a = query(ql, qr, root << 1);
}
if (qr > mid) {
b = query(ql, qr, root << 1 | 1);
}
c.first = a.first * b.first;
c.second = a.second + a.first * b.second;
return c;
}
int main() {
cin >> n >> q;
build(1, n, 1);
for (int i = 1; i <= n; i++) {
double x, y;
cin >> x >> y;
double p = x / y;
update(i, make_pair((1 - p) / p, (1 - p) / p), 1);
}
while (q--) {
int type;
cin >> type;
if (type == 1) {
int i, x, y;
scanf("%d%d%d", &i, &x, &y);
double p = double(x) / y;
update(i, make_pair((1.0 - p) / p, (1.0 - p) / p), 1);
} else {
int l, r;
scanf("%d%d", &l, &r);
pair<double, double> t = query(l, r, 1);
double p = t.second;
if (p < 1e20)
printf("%.6lf\n", 1.0 / (1 + p));
else
cout << "0.0000000" << endl;
}
}
return 0;
}
| 11 | CPP |
import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
x, y, z = map(int, input().split())
M = max(x, y, z)
if x==y==M or y==z==M or z==x==M:
print('YES')
print(M, min(x, y, z), 1)
else:
print('NO') | 7 | PYTHON3 |
a=input()
b=input()
a=a.lower()
b=b.lower()
if a<b:
ans=-1
elif a==b :
ans=0
else:
ans=1
print(ans) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
fast();
long long n;
cin >> n;
if (n % 4 == 1)
cout << '0' << " " << 'A' << endl;
else if (n % 4 == 0)
cout << '1' << " " << 'A' << endl;
else if (n % 4 == 2)
cout << '1' << " " << 'B' << endl;
else if (n % 4 == 3)
cout << '2' << " " << 'A' << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
namespace zzc {
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
const int maxn = 1e5 + 5;
const int blo = 700;
int n;
int a[maxn], pos[maxn], lst[maxn], pre[maxn], ans[maxn];
int count(int x) {
int num = 0, tmp = 1, stp = 1;
for (register int i = 2; i <= n; i++) {
if (pre[i] < stp) {
if (tmp == x) {
num++;
tmp = 0;
stp = i;
}
tmp++;
}
}
if (tmp) num++;
return num;
}
void solve(int l, int r) {
int sl = count(l), sr = count(r);
if (sl == sr) {
for (register int i = l; i <= r; i++) ans[i] = sl;
return;
}
int mid = (l + r) >> 1;
solve(l, mid);
solve(mid + 1, r);
}
void work() {
n = read();
for (register int i = 1; i <= n; i++) {
a[i] = read();
pre[i] = lst[a[i]];
lst[a[i]] = i;
}
for (register int i = 1; i <= blo; i++) ans[i] = count(i);
solve(blo + 1, n);
for (register int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
} // namespace zzc
int main() {
zzc::work();
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MX = 50005;
const int INF = 1111111111;
int C[MX], Cn;
vector<int> conV[MX];
struct Edge {
int id;
int u, v, c, t;
void in() {
scanf("%d%d%d%d", &u, &v, &c, &t);
u--, v--;
conV[u].push_back(id);
conV[v].push_back(id);
C[Cn++] = c;
}
} edge[MX];
int N, M;
vector<int> con[6 * MX], rcon[6 * MX], gcon[6 * MX];
int group[6 * MX], vis[6 * MX], Gn;
int Not(int u) { return (u + 3 * MX) % (6 * MX); }
void add(int u, int v) {
con[u].push_back(v);
con[Not(v)].push_back(Not(u));
rcon[v].push_back(u);
rcon[Not(u)].push_back(Not(v));
}
bool preprocess() {
int frq[MX] = {0}, ege[MX], egn;
int newV = MX;
for (int i = 0; i < N; i++) {
egn = 0;
for (auto it : conV[i]) {
if (frq[edge[it].c] == i + 1) ege[egn++] = it;
frq[edge[it].c] = i + 1;
}
if (egn > 1) return false;
if (egn)
for (auto it : conV[i])
if (it != ege[0] && edge[it].c == edge[ege[0]].c) ege[egn++] = it;
assert(egn == 0 || egn == 2);
if (egn) {
add(ege[0], Not(ege[1]));
add(Not(ege[0]), ege[1]);
}
int ii = 0, sz = conV[i].size();
for (auto it : conV[i]) {
add(it, newV);
if (ii < sz - 1) {
add(newV, newV + 1);
}
if (ii) {
add(it, Not(newV - 1));
}
ii++, newV++;
}
}
return true;
}
int que[6 * MX], qn;
void dfs(int u) {
vis[u] = true;
for (auto it : con[u])
if (!vis[it]) dfs(it);
que[qn++] = u;
}
void DFS(int u) {
vis[u] = true;
group[u] = Gn;
for (auto it : rcon[u])
if (!vis[it]) DFS(it);
}
void SCC() {
for (int i = 0; i < 6 * MX; i++)
if (!vis[i]) dfs(i);
for (int i = 0; i < 6 * MX; i++) vis[i] = 0;
for (int i = qn - 1; i >= 0; i--)
if (!vis[que[i]]) Gn++, DFS(que[i]);
}
int val[6 * MX], gval[6 * MX];
bool gdfs(int u) {
vis[u] = 1;
if (gval[u] != -1) {
if (!gval[u])
return false;
else
return true;
}
gval[u] = 1;
for (auto it : gcon[u]) {
if (!gdfs(it)) return false;
}
return true;
}
bool can(int Max) {
for (int i = 0; i < 6 * MX; i++) val[i] = gval[i] = -1, vis[i] = 0;
for (int i = 1; i <= M; i++)
if (edge[i].t > Max) {
if (gval[group[i]] != -1) {
if (gval[group[i]] == 1)
return false;
else
continue;
}
gval[group[i]] = 0;
if (!gdfs(group[Not(i)])) return false;
}
return true;
}
bool process() {
SCC();
for (int i = 0; i < 6 * MX; i++)
if (group[i] == group[Not(i)]) {
return false;
}
for (int i = 0; i < 6 * MX; i++) {
for (auto it : con[i]) {
if (group[i] == group[it]) continue;
gcon[group[i]].push_back(group[it]);
}
}
int st = 0, en = INF;
for (int it = 0; it < 50; it++) {
int md = st + en >> 1;
if (can(md))
en = md;
else
st = md;
}
can(en);
for (int i = 1; i <= M; i++) {
if (gval[group[i]] != -1)
val[i] = gval[group[i]];
else {
if (gval[group[Not(i)]] != -1)
val[i] = !gval[group[Not(i)]];
else {
int u = group[i], v = group[Not(i)];
val[i] = gval[u] = u > v, gval[v] = v > u;
}
}
}
int cnt = 0;
for (int i = 1; i <= M; i++) cnt += val[i];
puts("Yes");
printf("%d %d\n", en, cnt);
int flg = 0;
for (int i = 1; i <= M; i++)
if (val[i]) {
if (!flg)
flg = 1;
else
putchar(' ');
printf("%d", i);
}
puts("");
return true;
}
int main() {
scanf("%d%d", &N, &M);
for (int i = 0; i < MX; i++) edge[i].id = i;
for (int i = 1; i <= M; i++) edge[i].in();
sort(C, C + Cn);
Cn = unique(C, C + Cn) - C;
for (int i = 1; i <= M; i++)
edge[i].c = lower_bound(C, C + Cn, edge[i].c) - C;
if (!preprocess()) {
puts("No");
return 0;
}
if (!process()) {
puts("No");
return 0;
}
return 0;
}
| 10 | CPP |
import math
n = int(input())
S = list(map(int,input().split()))
M = 10**9 + 1
def merge(A, left, mid, right):
global count
n1 = mid - left
n2 = right - mid
L = A[left:left + n1]
R = A[mid:mid + n2]
L.append(M)
R.append(M)
i = 0
j = 0
for k in range(left,right):
count += 1
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
def mergeSort(A, left, right):
if left + 1 < right:
mid = (left + right) // 2
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
count = 0
mergeSort(S,0,len(S))
print(*S)
print(count) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, c[2010][2010], x[2], y[2];
long long a[4010], b[4010], ret[2];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &c[i][j]);
a[i + j] += c[i][j];
b[i - j + n] += c[i][j];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[i + j] + b[i - j + n] - c[i][j] >= ret[(i + j) & 1]) {
int v = (i + j) & 1;
ret[v] = a[i + j] + b[i - j + n] - c[i][j];
x[v] = i + 1;
y[v] = j + 1;
}
printf("%I64d\n", ret[0] + ret[1]);
printf("%d %d %d %d\n", x[0], y[0], x[1], y[1]);
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 16, mod = 998244353, g = 3;
int n, m, N = 1, l, rev[maxn], t1[maxn], t2[maxn], s[3][maxn], f[3][maxn];
int qpow(int x, int k) {
long long d = 1, t = x;
while (k) {
if (k & 1) d = d * t % mod;
t = t * t % mod, k >>= 1;
}
return d;
}
void DNT(int *F, int p) {
for (int i = 0; i < N; i++)
if (i < rev[i]) swap(F[i], F[rev[i]]);
for (int i = 1; i < N; i <<= 1) {
int w1 = qpow(g, (mod - 1) / (i << 1));
for (int j = 0; j < N; j += i << 1) {
int w = 1;
for (int k = j; k < j + i; k++) {
int t1 = F[k], t2 = 1ll * w * F[k + i] % mod;
F[k] = (t1 + t2) % mod, F[k + i] = (t1 - t2 + mod) % mod;
w = 1ll * w * w1 % mod;
}
}
}
if (!p) {
int inv = qpow(N, mod - 2);
for (int i = 0; i < N; i++) F[i] = 1ll * F[i] * inv % mod;
for (int i = 1; i <= (N >> 1); i++) swap(F[i], F[N - i]);
}
}
void NTT(int *F, int *G, int *H) {
for (int i = 0; i <= m; i++) t1[i] = F[i], t2[i] = G[i];
for (int i = m + 1; i < N; i++) t1[i] = t2[i] = 0;
DNT(t1, 1), DNT(t2, 1);
for (int i = 0; i < N; i++) t1[i] = 1ll * t1[i] * t2[i] % mod;
DNT(t1, 0);
for (int i = 0; i <= m; i++) H[i] = (H[i] + t1[i]) % mod;
}
void Mul(int x, int a, int b) {
for (int i = 0; i <= m; i++) s[x][i] = 0;
NTT(f[a], f[b], s[x]);
NTT(f[a - 1], f[b - 1], s[x] + 1);
s[x][m + 1] = 0;
}
void Add() {
for (int i = 0; i <= m; i++) {
f[0][i] = f[1][i], f[1][i] = f[2][i];
f[2][i] = (f[1][i] + (i ? (f[1][i - 1] + f[0][i - 1]) % mod : 0)) % mod;
}
}
int main() {
f[2][0] = 1;
scanf("%d%d", &n, &m);
while (N < ((m + 1) << 1)) N <<= 1, l++;
for (int i = 0; i < N; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (l - 1));
int p = 30;
while ((n & (1 << p)) == 0) p--;
for (int i = p; i >= 0; i--) {
if (n & (1 << i)) Add();
if (i) {
Mul(2, 2, 2), Mul(1, 2, 1), Mul(0, 1, 1);
for (int j = 0; j <= m; j++)
f[0][j] = s[0][j], f[1][j] = s[1][j], f[2][j] = s[2][j];
}
}
for (int i = 1; i <= m; i++) printf("%d ", f[2][i]);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int maxn = 50000;
int main() {
int n;
cin >> n;
int ans[maxn];
for (int i = 1; i <= n; i++) ans[i] = i;
set<int> para[maxn];
int m;
cin >> m;
while (m--) {
int a, b;
cin >> a >> b;
para[b].insert(a);
}
for (int i = 1; i <= n; i++) {
int pos = i, x = ans[i];
while (pos > 1 && para[x].count(ans[pos - 1])) {
swap(ans[pos], ans[pos - 1]);
--pos;
}
}
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n;
pair<int, int> arr[101010];
bool cmp(pair<int, int> &a, pair<int, int> &b) {
return (a.first - a.second) - (b.first - b.second) > 0;
}
int main() {
scanf("%lld", &n);
for (long long i = 0; i < n; i++) {
scanf("%lld%lld", &arr[i].first, &arr[i].second);
}
sort(arr, arr + n, cmp);
long long ans = 0;
for (long long i = 0; i < n; i++) {
ans += (long long)arr[i].first * i + arr[i].second * (n - i - 1);
}
printf("%lld", ans);
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
void RD(int &x) { scanf("%d", &x); }
void RD(long long &x) { scanf("%I64d", &x); }
void RD(unsigned long long &x) { scanf("%I64u", &x); }
void RD(unsigned int &x) { scanf("%u", &x); }
void RD(double &x) { scanf("%lf", &x); }
void RD(int &x, int &y) { scanf("%d%d", &x, &y); }
void RD(long long &x, long long &y) { scanf("%I64d%I64d", &x, &y); }
void RD(unsigned int &x, unsigned int &y) { scanf("%u%u", &x, &y); }
void RD(double &x, double &y) { scanf("%lf%lf", &x, &y); }
void RD(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); }
void RD(long long &x, long long &y, long long &z) {
scanf("%I64d%I64d%I64d", &x, &y, &z);
}
void RD(unsigned int &x, unsigned int &y, unsigned int &z) {
scanf("%u%u%u", &x, &y, &z);
}
void RD(double &x, double &y, double &z) { scanf("%lf%lf%lf", &x, &y, &z); }
void RD(char &x) { x = getchar(); }
void RD(char *s) { scanf("%s", s); }
void RD(string &s) { cin >> s; }
void PR(int x) { printf("%d\n", x); }
void PR(int x, int y) { printf("%d %d\n", x, y); }
void PR(long long x) { printf("%I64d\n", x); }
void PR(unsigned int x) { printf("%u\n", x); }
void PR(unsigned long long x) { printf("%I64u\n", x); }
void PR(double x) { printf("%.4lf\n", x); }
void PR(char x) { printf("%c\n", x); }
void PR(char *x) { printf("%s\n", x); }
void PR(string x) { cout << x << endl; }
void upMin(int &x, int y) {
if (x > y) x = y;
}
void upMin(long long &x, long long y) {
if (x > y) x = y;
}
void upMin(double &x, double y) {
if (x > y) x = y;
}
void upMax(int &x, int y) {
if (x < y) x = y;
}
void upMax(long long &x, long long y) {
if (x < y) x = y;
}
void upMax(double &x, double y) {
if (x < y) x = y;
}
int sgn(double x) {
if (x > 1e-6) return 1;
if (x < -1e-6) return -1;
return 0;
}
long long Gcd(long long x, long long y) {
if (y == 0) return x;
return Gcd(y, x % y);
}
long long Lcm(long long x, long long y) { return x / Gcd(x, y) * y; }
long long eular(long long n) {
long long ans = n, i;
for (i = 2; i * i <= n; i++)
if (n % i == 0) {
ans -= ans / i;
while (n % i == 0) n /= i;
}
if (n > 1) ans -= ans / n;
return ans;
}
long long exGcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long temp = exGcd(b, a % b, x, y);
long long t = x;
x = y;
y = t - a / b * y;
return temp;
}
long long gcdReverse(long long a, long long b) {
long long x, y;
exGcd(a, b, x, y);
x %= b;
if (x < 0) x += b;
return x;
}
long long myPow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
long long myPow(long long a, long long b, long long mod) {
a %= mod;
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int getInt() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
int getBitOfOne(long long x) {
int ans = 0;
while (x) ans += x & 1, x >>= 1;
return ans;
}
long long getSqrt(long long t) {
long long p = sqrt(1.0 * t);
while (p * p > t) p--;
while ((p + 1) <= t / (p + 1)) p++;
return p;
}
const int mod = 300007;
const long long inf = (((long long)1) << 60) + 5;
const double dinf = 1000000000;
const int INF = 2000000005;
const int N = 200005;
int n, m;
struct node {
int val, cost, id;
int operator<(const node &a) const {
return cost < a.cost || cost == a.cost && val > a.val;
}
};
node a[N];
int e;
int d[N];
int mp[N];
vector<int> g[N];
int b[N];
void pushUp(int t) {
while (t > 1) {
int r = t >> 1;
if (a[t] < a[r]) {
swap(a[t], a[r]);
mp[a[t].id] = t;
mp[a[r].id] = r;
t = r;
} else
break;
}
}
void pushDown(int t) {
while (1) {
int L = t * 2, R = t * 2 + 1;
int xx = t;
if (L <= e && a[L] < a[xx]) xx = L;
if (R <= e && a[R] < a[xx]) xx = R;
if (xx == t) break;
swap(a[t], a[xx]);
mp[a[t].id] = t;
mp[a[xx].id] = xx;
t = xx;
}
}
void insert(int val, int cost, int id) {
e++;
a[e].val = val;
a[e].cost = cost;
a[e].id = id;
mp[id] = e;
pushUp(e);
}
int h[N];
int main() {
RD(n, m);
int i;
for (i = 1; i <= n; i++) RD(d[i]);
for (i = 1; i <= m; i++) {
int x, y;
RD(x, y);
b[x] += d[y];
b[y] += d[x];
g[x].push_back(y);
g[y].push_back(x);
}
long long ans = 0;
for (i = 1; i <= n - 1; i++) {
int Max = -1, id;
int j;
for (j = 1; j <= n; j++)
if (!h[j] && d[j] > Max) {
Max = d[j];
id = j;
}
for (j = 0; j < g[id].size(); j++)
if (!h[g[id][j]]) {
ans += d[g[id][j]];
}
h[id] = 1;
}
PR(ans);
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
int n = s.length();
if (n == 1) {
cout << 0;
exit(0);
}
for (int i = 0; i <= (s.length() - 2); i++) {
if (s[i] == s[i + 1]) {
cout << 0 << " ";
continue;
} else if (s[i] == 'a' && s[i + 1] == 'b') {
cout << 1 << " ";
string temp = s.substr(0, i + 1);
reverse(temp.begin(), temp.end());
s.replace(0, i + 1, temp);
continue;
} else if (s[i] == 'b' && s[i + 1] == 'a') {
cout << 1 << " ";
string temp = s.substr(0, i + 1);
reverse(temp.begin(), temp.end());
s.replace(0, i + 1, temp);
}
}
string temp = s;
reverse(temp.begin(), temp.end());
if (temp <= s) {
cout << 1;
cout << endl;
} else {
cout << 0;
cout << endl;
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
vector<long long> dp;
long long gg(long long i, long long j) {
j = min(j, (long long)v.size() - 1);
long long res = dp[j];
if (i > 0) res -= dp[i - 1];
return res;
}
int main() {
int n;
cin >> n;
v.resize(n);
dp.resize(n);
for (int i = (0); i < (n); i += 1) cin >> v[i];
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
long long x = 0;
for (int i = (0); i < (n); i += 1) {
x += v[i];
dp[i] = x;
}
long long r1 = 0;
for (int i = (0); i < (n); i += 1) r1 += v[i] * i;
int q;
cin >> q;
while (q--) {
long long k;
cin >> k;
if (k == 1) {
cout << r1 << endl;
} else {
long long s = 0;
long long rame = 1;
int i = 0;
long long prnt = 0;
while (s < n) {
long long g = gg(s, s + rame - 1);
prnt += g * i;
s += rame;
i++;
rame *= k;
}
cout << prnt << endl;
}
}
return 0;
}
| 8 | CPP |
import sys
def main():
#w = int(sys.argv[1])
w= int(input())
if w >= 4 and w % 2 == 0:
print('YES')
else:
print('NO')
main() | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using llu = unsigned long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
for (int i = 0; i < n - 1; i++) {
if (s < 2) {
break;
}
s -= 2;
}
if (s < 2) {
cout << "NO\n";
} else {
cout << "YES\n";
for (int i = 0; i < n - 1; i++) {
cout << "2 ";
}
cout << s << "\n1\n";
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
inline long long add(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x + _y;
if (_ >= _mod) _ -= _mod;
return _;
}
inline long long sub(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x - _y;
if (_ < 0) _ += _mod;
return _;
}
inline long long mul(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x * _y;
if (_ >= _mod) _ %= _mod;
return _;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1ll;
long long _tmp = mypow(_a, _x / 2, _mod);
_tmp = mul(_tmp, _tmp, _mod);
if (_x & 1) _tmp = mul(_tmp, _a, _mod);
return _tmp;
}
long long mymul(long long _a, long long _x, long long _mod) {
if (_x == 0) return 0ll;
long long _tmp = mymul(_a, _x / 2, _mod);
_tmp = add(_tmp, _tmp, _mod);
if (_x & 1) _tmp = add(_tmp, _a, _mod);
return _tmp;
}
inline bool equal(double _x, double _y) {
return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
void build() {}
long long d, k, a, b, t;
void init() {
d = getint();
k = getint();
a = getint();
b = getint();
t = getint();
}
long long ans;
inline long long cal(long long tms) {
long long dst = min(d, k * tms);
long long tans = dst * a + (d - dst) * b;
if (tms) tans += (tms - 1) * t;
return tans;
}
void solve() {
ans = d * b;
long long mst = (d / k);
if (mst * k < d) mst++;
long long bl = 0, br = mst, mid1, mid2;
while (br - bl > 10) {
mid1 = bl + (br - bl) / 3;
mid2 = br - (br - bl) / 3;
long long ans1 = cal(mid1);
long long ans2 = cal(mid2);
if (ans1 < ans2) {
ans = min(ans, ans1);
br = mid2;
} else {
ans = min(ans, ans2);
bl = mid1;
}
}
for (long long i = bl; i <= br; i++) ans = min(ans, cal(i));
cout << ans << endl;
}
int main() {
build();
while (__--) {
init();
solve();
}
}
| 10 | CPP |
t = int(input())
for _ in range(0,t):
n,a,b = input().split()
n=int(n)
a=int(a)
b=int(b)
i=0
x=97
z=[]
ans=""
while(i<a):
if((a-(i%a))<b):
if(len(z)==0):
# print(i)
z.append(chr(x))
else:
# print(i)
x=x+1
z.append(chr(x))
i=i+1
else:
z.append(chr(x))
i=i+1
for i in range(0,n):
ans= ans+(z[i%a])
print(ans) | 8 | PYTHON3 |
#include<iostream>
#include<set>
using namespace std;
int N,P[1<<17],A[1<<17];
set<int>S;
int main()
{
cin>>N;
for(int i=0;i<N;i++)
{
cin>>P[i];
A[P[i]]=i;
}
S.insert(A[N]);
long ans=0;
for(int i=N-1;i;i--)
{
S.insert(A[i]);
set<int>::iterator id=S.find(A[i]);
set<int>::iterator L=id,R=id;
long B,AA,a,b;
R++;
if(R==S.end())
{
a=N-A[i];
b=0;
}
else
{
int t=*R;
a=t-A[i];
R++;
if(R==S.end())b=N-t;
else b=*R-t;
}
if(L==S.begin())
{
AA=A[i]+1;
B=0;
}
else
{
L--;
int t=*L;
AA=A[i]-t;
if(L==S.begin())B=t+1;
else
{
L--;
B=t-*L;
}
}
ans+=(AA*b+a*B)*i;
}
cout<<ans<<endl;
}
| 0 | CPP |
n,k=map(int,input().split())
n=n/2+0.5
if n>=k:
print("YES")
else:
print("NO")
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#define INF 99999999
int n, root, Ans, ans, C[maxn], mark[maxn];
int degree[maxn], dp1[maxn], dp2[maxn];
int val[maxn];
int read()
{
int x = 0, k = 1;
char c; c = getchar();
while(c < '0' || c > '9') { if(c == '-') k = -1; c = getchar(); }
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * k;
}
struct edge
{
int cnp, to[maxn * 2], last[maxn * 2], head[maxn];
edge() { cnp = 1; }
void add(int u, int v)
{
to[cnp] = v, last[cnp] = head[u], head[u] = cnp ++;
to[cnp] = u, last[cnp] = head[v], head[v] = cnp ++;
}
}E1;
void dfs(int u, int fa)
{
mark[u] = C[u];
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
if(v == fa) continue;
dfs(v, u);
if(!mark[v]) ++ degree[u], ++ degree[v];
mark[u] &= mark[v];
}
}
void dfs2(int u, int fa)
{
int mx1 = 0, mx2 = 0; bool flag = 0;
Ans += degree[u];
if((degree[u] + C[u]) & 1) val[u] = 0;
else val[u] = 2, ++ Ans;
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
if(v == fa || mark[v]) continue;
flag = 1; dfs2(v, u);
ans = max(ans, max(dp1[v] + mx2 + val[u], dp2[v] + mx1 + val[u]));
mx1 = max(dp1[v], mx1), mx2 = max(dp2[v], mx2);
}
dp1[u] = mx1 + val[u], dp2[u] = flag ? mx2 + val[u] : -INF;
}
int main()
{
n = read();
for(int i = 1; i < n; i ++)
{
int x = read(), y = read();
E1.add(x, y);
}
for(int i = 1; i <= n; i ++)
{
char c; cin >> c;
if(c == 'B') C[i] = 1;
else root = i;
}
if(!root) { puts("0"); return 0; }
dfs(root, 0); dfs2(root, 0);
printf("%d\n", Ans - ans);
return 0;
} | 0 | CPP |
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil,pi,sin,cos,acos,atan
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush,heappop,heapify
from itertools import permutations,combinations
mod = int(1e9)+7
ip = lambda : int(stdin.readline())
inp = lambda: map(int,stdin.readline().split())
ips = lambda: stdin. readline().rstrip()
out = lambda x : stdout.write(str(x)+"\n")
t = ip()
for _ in range(t):
n = ip()
arr = list(inp())
if sum(arr)%n == 0:
mean = sum(arr)//n
ans = []
for i in range(1,n):
idx = i+1
if arr[i]%idx == 0:
quot = arr[i]//idx
arr[0] += arr[i]
arr[i] = 0
query = "{} {} {}".format(idx,1,quot)
ans.append(query)
for i in range(1,n):
idx = i+1
if arr[i] == 0:
pass
else:
if arr[i]%idx != 0:
val = idx - arr[i]%idx
arr[0] -= val
arr[i] += val
query = "{} {} {}".format(1,idx,val)
ans.append(query)
quot = arr[i]//idx
arr[0] += arr[i]
arr[i] = 0
query = "{} {} {}".format(idx,1,quot)
ans.append(query)
for i in range(1,n):
idx = i+1
arr[i] += mean
arr[0] -= mean
query = "{} {} {}".format(1,idx,mean)
ans.append(query)
print(len(ans))
for i in ans:
print(i)
else:
out(-1)
| 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n;
int a[501];
int f[501][501] = {};
inline int add(int x, int y) {
x += y;
return x < mod ? x : x - mod;
}
inline int mul(int x, int y) { return 1ll * x * y % mod; }
int dp(int i, int j) {
if (f[i][j]) return f[i][j];
for (int k = i + 1; k <= j; ++k)
if (a[i + 1] < a[k + 1] || k == j)
f[i][j] = add(f[i][j], mul(dp(i + 1, k), dp(k, j)));
return f[i][j];
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), f[i][i] = 1;
cout << dp(1, n);
return 0;
}
| 12 | CPP |
#include <iostream>
#include <string>
using namespace std;
void solve()
{
string str;
while(cin >> str)
{
for(int i = 0; i < str.size(); ++i)
{
if(str[i] == '@')
{
for(int j = 0; j < str[i + 1] - '0'; ++j)
{
cout << str[i + 2];
}
i += 2;
}
else
{
cout << str[i];
}
}
cout << endl;
}
}
int main()
{
solve();
return(0);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long in() {
long long a;
scanf("%lld", &a);
return a;
}
double din() {
double a;
scanf("%lf", &a);
return a;
}
long long bigmod(long long b, long long p, long long md) {
if (p == 0) return 1;
if (p % 2 == 1) {
return ((b % md) * bigmod(b, p - 1, md)) % md;
} else {
long long y = bigmod(b, p / 2, md);
return (y * y) % md;
}
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long getRandom(long long a, long long b) {
long long ret = (long long)rand() * (long long)rand();
ret %= (b - a + 1);
ret += a;
return ret;
}
int solve(vector<int> v) {
if (v.size() == 1) return 1;
int mx = 0, bla = 0;
vector<int> vv;
for (int i = 0; i < v.size(); i++) {
if (i == v.size() - 1 || (v[i] ^ v[i + 1]) > v[i]) {
for (int j = 32; j >= 0; j--) {
if (v[i] & (1LL << j)) {
v[i] ^= (1LL << j);
break;
}
}
vv.push_back(v[i]);
mx = max(mx + 1, bla + solve(vv));
bla = 1;
vv.clear();
} else {
for (int j = 32; j >= 0; j--) {
if (v[i] & (1LL << j)) {
v[i] ^= (1LL << j);
break;
}
}
vv.push_back(v[i]);
}
}
return mx;
}
int main() {
int n = in();
vector<int> v;
for (int i = 0; i < n; i++) v.push_back(in());
sort(v.begin(), v.end());
int mx = solve(v);
printf("%lld\n", (long long)n - mx);
}
| 9 | CPP |
from collections import Counter,defaultdict,deque
#alph = 'abcdefghijklmnopqrstuvwxyz'
#from math import factorial as fact
import math
#nl = '\n'
#import sys
#input=sys.stdin.readline
#print=sys.stdout.write
#tt = int(input())
#total=0
#n = int(input())
#n,m,k = [int(x) for x in input().split()]
#n = int(input())
#l,r = [int(x) for x in input().split()]
n = int(input())
arr = [int(x) for x in input().split()]
d = {}
visited = {}
for i in range(n):
d[arr[i]] = i
visited[arr[i]] = False
res =[]
sarr = sorted(arr)
for el in sarr:
cur = []
nxt = el
while True:
if visited[nxt]:
break
visited[nxt] = True
cur.append(d[nxt])
nxt = sarr[d[nxt]]
if len(cur):
res.append(cur)
print(len(res))
for el in res:
print(len(el),end=' ')
for p in el:
print(p+1,end = ' ')
print()
| 7 | PYTHON3 |
#include<iostream>
int main(){
int a,b; std::cin >> a >> b;
std::cout << a - b + 1 << std::endl;
} | 0 | CPP |
a = b = 1
p, s = [0, 0], [0, 0]
for i in range(int(input())):
p.append(int(input()))
while p[-1] - p[a] > 89: a += 1
while p[-1] - p[b] > 1439: b += 1
s.append(min(s[-1] + 20, s[a - 1] + 50, s[b - 1] + 120))
print(' '.join(str(u - v) for u, v in zip(s[2:], s[1:]))) | 8 | PYTHON3 |
n=int(input())
a=list(map(int,input().split()))
count=0
ar=[]
for i in range(1,n):
if(a[i]>=a[i-1]):
count=count+1
else:
ar.append(count)
count=0
ar.append(count)
print(max(ar)+1)
| 7 | PYTHON3 |
s=input()
temp=s[0].upper()
#print(temp)
print(temp+s[1:]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
string s;
int up = 0, lw = 0;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] - 65 <= 26)
up++;
else
lw++;
}
if (up > lw) {
for (int i = 0; s[i] != '\0'; i++) {
s[i] = toupper(s[i]);
}
} else {
for (int i = 0; s[i] != '\0'; i++) {
s[i] = tolower(s[i]);
}
}
cout << s;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int) (x).size())
typedef long long ll;
typedef long double ld;
int n, c[200100], t[200100];
bool u[200100];
vector<pair<int, int>> r;
void sw(int i, int j) {
t[c[i]]++, t[c[j]]++;
r.push_back({i, j});
swap(c[i], c[j]);
}
void solve(int i) {
while (c[i] != i)
sw(i, c[i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(12);
cin >> n;
for (int i = 0; i < n; i++)
cin >> c[i], c[i]--;
int x = -1;
for (int i = 0; i < n; i++) {
if (c[i] == i || u[i])
continue;
int j = i;
do {
u[j] = true;
j = c[j];
} while (j != i);
if (x == -1)
x = i;
else {
sw(x, i);
sw(c[x], c[i]);
solve(c[x]);
solve(c[i]);
x = -1;
}
}
if (x != -1) {
if (c[c[x]] == x) {
int i = 0;
while (i == x || i == c[x])
i++;
sw(i, c[x]);
sw(i, x);
sw(i, c[i]);
} else {
int i = x, j = c[x];
while (c[c[i]] != i)
sw(i, c[i]);
sw(j, c[i]);
sw(i, c[i]);
sw(i, c[i]);
}
}
// cout << "Fin\n";
// for (int i = 0; i < n; i++)
// cout << c[i] << (i < n - 1 ? " " : "\n");
// for (int i = 0; i < n; i++)
// cout << t[i] << (i < n - 1 ? " " : "\n");
cout << sz(r) << "\n";
for (auto & p : r)
cout << p.first + 1 << " " << p.second + 1 << "\n";
}
| 13 | CPP |
s=list(input())
t=list(input())
r=len(t)
for ii in range(len(s)-len(t)+1):
rr=0
for jj in range(len(t)):
if s[ii+jj]!=t[jj]:
rr+=1
if rr<r:
r=rr
print(r) | 0 | PYTHON3 |
#include <bits/stdc++.h>
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const double eps = 1e-6;
using namespace std;
bool eq(const double &a, const double &b) { return fabs(a - b) < eps; }
bool ls(const double &a, const double &b) { return a + eps < b; }
bool le(const double &a, const double &b) { return eq(a, b) || ls(a, b); }
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); };
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long kpow(long long a, long long b) {
long long res = 1;
a %= mod;
if (b < 0) return 1;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct edge {
int l, r, id;
bool operator<(edge c) const {
if (l == c.l) return r > c.r;
return l < c.l;
}
} e[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < (n); ++i) {
scanf("%d%d", &e[i].l, &e[i].r);
e[i].id = i + 1;
}
sort(e, e + n);
int i = 0;
while (i < n) {
int j = i + 1;
while (1) {
if (e[j].r > e[i].r || j > n) break;
if (e[j].l >= e[i].l && e[j].r <= e[i].r)
return printf("%d %d\n", e[j].id, e[i].id), 0;
++j;
}
i = j;
}
puts("-1 -1");
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
bool cmp(long long a, long long b) { return a > b; }
long long prem[maxn], preu[maxn];
long long totm = 0, totu = 0;
long long mrr[maxn], urr[maxn];
int main() {
long long n, d, m;
scanf("%lld %lld %lld", &n, &d, &m);
long long val;
for (int i = 0; i < n; i++) {
scanf("%lld", &val);
if (val > m)
mrr[totm++] = val;
else
urr[totu++] = val;
}
sort(mrr, mrr + totm, cmp);
sort(urr, urr + totu, cmp);
prem[0] = mrr[0], preu[0] = urr[0];
for (int i = 1; i < totm; i++) prem[i] = prem[i - 1] + mrr[i];
for (int i = 1; i < n; i++) preu[i] = preu[i - 1] + urr[i];
long long ans = 0;
if (totu == n) ans = preu[totu - 1];
for (int i = 1; i <= totm; i++) {
long long delay = (i - 1) * (d + 1) + 1;
if (delay > n) break;
long long res = prem[i - 1];
ans = max(ans, preu[n - delay - 1] + res);
}
printf("%lld\n", ans);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int maxm = 200005;
const int inf = 2e9 + 3;
const int finf = -2e9;
vector<pair<int, int> > bar[maxn];
vector<int> tmp;
int a[maxn], n, k;
char s[25];
int deal(char s[], int len) {
int st = 0, flag = 1;
if (s[0] == '-') st++, flag = -1;
int ret = 0;
for (int i = st; i < len; i++) {
ret = ret * 10 + s[i] - '0';
}
return ret * flag;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = (int)1; i <= (int)n; i++) {
scanf("%s", s);
int l = strlen(s);
if (s[0] == '?')
a[i] = inf;
else
a[i] = deal(s, l);
int pos = i - (i - 1) / k * k;
bar[pos].push_back({a[i], i});
}
for (int i = 1; i <= k; i++) {
bar[i].push_back({inf + 2, 0});
}
int flag = 0;
for (int i = 1; i <= k; i++) {
int pre = -(int)2e9 - 1;
tmp.clear();
for (int j = 0; j < bar[i].size(); j++) {
int aim = bar[i][j].first;
if (aim == inf) {
tmp.push_back(bar[i][j].second);
} else {
if (aim <= pre) {
flag = 1;
break;
}
int can = aim - pre - 1;
if (can < tmp.size()) {
flag = 1;
break;
}
if (aim <= 0) {
for (int k = tmp.size() - 1, val = aim - 1; k >= 0; k--, val--) {
a[tmp[k]] = val;
}
tmp.clear();
} else {
if (pre < 0) {
int sz = tmp.size(), fir = aim - sz, gre = -(sz - 1) / 2;
if (fir > gre) {
fir = max(pre + 1, gre);
}
for (int k = 0, val = fir; k < sz; k++, val++) {
a[tmp[k]] = val;
}
tmp.clear();
} else {
for (int k = 0, val = pre + 1; k < tmp.size(); k++, val++) {
a[tmp[k]] = val;
}
tmp.clear();
}
}
pre = aim;
}
}
if (flag) break;
}
if (flag) return 0 * printf("Incorrect sequence\n");
for (int i = 1; i <= n; i++) {
printf("%d%c", a[i], i == n ? '\n' : ' ');
}
return 0;
}
| 11 | CPP |
I=lambda:map(int,input().split())
n,m=I()
a=sorted(I())
print(min(j-i for i,j in zip(a,a[n-1:]))) | 7 | PYTHON3 |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
long long A,B,l,r,ll,rr;
int W1,W2=-1;
long long read(){
long long ans=0;char c=getchar();
while (c<'0'||c>'9') c=getchar();
while (c>='0'&&c<='9') ans=(ans<<1)+(ans<<3)+(c^48),c=getchar();
return ans;
}
int main(){
A=read(),B=read();
if (A==B){printf("1");return 0;}
for (register int i=60;i>=0;i--)
if ((A&(1ll<<i))!=(B&(1ll<<i))){W1=i;break;}
A&=((1ll<<(W1+1))-1),B&=((1ll<<(W1+1))-1);
for (register int i=W1-1;i>=0;i--)
if (B&(1ll<<i)){W2=i;break;}
long long L1=A,R1=B|((1ll<<(W2+1))-1);
long long L2=(1ll<<W1)|A,R2=(1ll<<(W1+1))-1;
if (R1<L2) printf("%lld",R2-L2+1+R1-L1+1);
else printf("%lld",R2-L1+1);
return 0;
}
| 0 | CPP |
def solve():
n = int(input())
divs = []
for i in range(2, int(n**0.5) + 2):
if n % i == 0:
n //= i
divs.append(i)
if len(divs) == 2:
if n > divs[1]:
print ("YES")
print (*divs, n)
return
else:
print ("NO")
return
print ("NO")
t = int(input())
for _ in range(t):
solve()
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b != 0 ? a : gcd(b, a % b); }
long long power(long long a, long long b, long long mod) {
long long c = 1;
while (b > 0) {
if (b % 2) c *= a, c %= mod;
b /= 2;
a *= a;
a %= mod;
}
return c;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, t;
cin >> n;
long long k;
cin >> k;
long long a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long b[n];
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
long long l = 0;
long long mid, h = 20 * 1e8 + 1;
long long u = 0;
mid = h / 2;
while (h > l) {
long long j = 0;
mid = (l + h) / 2;
for (int i = 0; i < n; i++) {
j += max(a[i] * mid - b[i], 0ll);
if (j > k) {
h = mid;
break;
}
if (i == n - 1) l = mid + 1;
}
}
cout << l - 1 << endl;
return 0;
}
| 10 | CPP |
import sys
board_size = int(input())
first_row = "".join(["W" if i % 2 == 0 else "B" for i in range(board_size)]) + "\n"
second_row = "".join(["B" if i % 2 == 0 else "W" for i in range(board_size)]) + "\n"
for n in range(board_size):
if n % 2 == 0:
sys.stdout.write(first_row)
else:
sys.stdout.write(second_row)
sys.stdout.flush()
| 8 | PYTHON3 |
#include <bits/stdc++.h>
int a[10010], n, k;
double p, l = 0.0, r = 1000.0, m, s;
int main() {
scanf("%d%d", &n, &k);
p = 1.0 - k / 100.0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
while (r - l > 0.0000001) {
m = (l + r) / 2.0, s = 0.0;
for (int j = 1; j <= n; j++)
(a[j] > m) ? s += (a[j] - m)* p : s -= (m - a[j]);
(s > 0.0) ? l = m : r = m;
}
printf("%.7lf\n", m);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int p[maxn];
vector<int> pa;
int main() {
int n, f;
scanf("%d", &n);
p[1] = 1;
for (int i = 2; i <= n; i++) {
scanf("%d", &f);
p[i] = f;
}
int r = n;
while (p[r] != r) {
pa.push_back(r);
r = p[r];
}
reverse(pa.begin(), pa.end());
printf("1");
for (int i = 0; i < (int)pa.size(); i++) printf(" %d", pa[i]);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000000 + 7;
const long long alphabet = 256;
long long arr[5005], cumsum[5005];
long long dp1[5005], id1[5005];
int main() {
for (int i = 0; i < 5005; i++) dp1[i] = -1e18;
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> arr[i];
}
for (long long i = 1; i <= n; i++) cumsum[i] = cumsum[i - 1] + arr[i];
for (long long i = 1; i <= n + 1; i++) {
for (long long j = 1; j <= i; j++) {
if (dp1[i] < cumsum[j - 1] - cumsum[i - 1] + cumsum[j - 1]) {
dp1[i] = cumsum[j - 1] - cumsum[i - 1] + cumsum[j - 1];
id1[i] = j;
}
}
}
long long ans = -1e18, del0, del1, del2;
for (long long i = 1; i <= n + 1; i++) {
for (long long j = 1; j <= i; j++) {
long long temp = (cumsum[i - 1] - cumsum[j - 1]) -
(cumsum[n - 1] - cumsum[i - 1]) + dp1[j];
if (ans < temp) {
ans = temp;
del0 = id1[j], del1 = j, del2 = i;
del0--;
del1--;
del2--;
}
}
}
cout << del0 << " " << del1 << " " << del2 << endl;
}
| 9 | CPP |
def f(r1):
global r, lastNum, lPos, rPos
r.append(r1)
if r1 == 'L':
lastNum = a[lPos]
lPos += 1
else:
lastNum = a[rPos]
rPos -= 1
def onlyLeft(begin):
global a
pos = begin
while a[pos] < a[pos+1]:
pos += 1
return pos - begin + 1
def onlyRight(begin):
global a
pos = begin
while a[pos] < a[pos-1]:
pos -= 1
return begin - pos + 1
n = int(input())
a = list(map(int, input().split()))
r = []
lastNum = -1
lPos = 0
rPos = n-1
while lPos <= rPos:
if a[lPos] <= lastNum and a[rPos] <= lastNum:
break
## print(lPos, rPos, level, r)
if lPos == rPos:
if a[lPos] > lastNum:
f('L')
break
if a[lPos] == a[rPos]:
r1 = onlyLeft(lPos)
r2 = onlyRight(rPos)
if r1 > r2:
for i in range(r1):
r.append('L')
else:
for i in range(r2):
r.append('R')
break
if a[lPos] < a[rPos]:
if a[lPos] > lastNum:
f('L')
elif a[rPos] > lastNum:
f('R')
if a[lPos] > a[rPos]:
if a[rPos] > lastNum:
f('R')
elif a[lPos] > lastNum:
f('L')
print(len(r))
print(*r, sep='')
| 9 | PYTHON3 |
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().split()))
p=0
m=a[0]
for j in range(1,n):
if a[j]*a[j-1]>0:
m=max(m,a[j])
else:
p=p+m
m=a[j]
p=p+m
print(p)
| 9 | PYTHON3 |
n, t = map(int, input().split())
data = [int(i) for i in input().split()]
sums = [0]
for i in range(n):
sums.append(sums[-1] + data[i])
i, j = 1, 1
ans = 0
while j <= n and i <= n:
while i <= n and j <= n and sums[j] - sums[i-1] <= t:
j += 1
ans = max(ans, j-i)
i += 1
print(ans)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
#define inf 1000000000
#define mod 1000000007
using namespace std;
using LL = long long;
int main(){
string s;cin>>s;
if(s[s.size()-1]!='A')s+='A';
if(s[0]!='A')s.insert(s.begin(),'A');
if(s=="AKIHABARA" || s=="AKIHBARA" || s=="AKIHABRA" || s=="AKIHBRA")cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
} | 0 | CPP |
from collections import defaultdict
n,k=map(int,input().split())
typ=defaultdict(lambda:[])
for _ in range(k):
x,y=input().split()
typ[y].append(x[0])
curr=2
val=typ['a']
while(curr<n):
temp=[]
for i in val:
for j in typ[i]:
temp.append(j)
val=temp
curr+=1
print(len(val))
| 8 | PYTHON3 |
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
p=False
for i in range(1,n-1):
if a[i-1]<a[i] and a[i+1]<a[i]:
print('YES')
print(i,i+1,i+2)
p=True
break
if not(p):
print('NO')
| 7 | PYTHON3 |
a,b=map(int,input().split())
for i in range(a+1):
for j in range(a-i+1):
if i*10000+j*5000+(a-i-j)*1000==b:
print(i,j,a-i-j)
exit()
print(-1,-1,-1) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 500000, MOD = 1e9;
long long arr[M];
vector<long long> v[1000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
int sum = 0;
while (1) {
bool ok = 0;
sum = arr[0];
for (int i = 1; i < n; i++) {
if (arr[0] < arr[i]) {
ok = 1;
arr[i] -= arr[0];
}
sum += arr[i];
}
if (!ok) break;
sort(arr, arr + n);
}
cout << sum << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
template <class T>
T power(T N, T P) {
return (P == 0) ? 1 : N * power(N, P - 1);
}
template <class T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
void make_unique(vector<int>& a) {
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
}
int on(int n, int pos) { return n = n | (1 << pos); }
void off(int& n, int pos) { n = n & ~(1 << pos); }
bool check(int n, int pos) { return (bool)(n & (1 << pos)); }
const int N = 1e5 + 10;
const int M = 64;
int id, cnt[M * N], a[N * M][5];
int new_node() {
for (int i = 0; i < 3; i++) {
a[id][i] = -1;
}
cnt[id] = 0;
return id++;
}
vector<int> bit(70);
void make(long long int x) {
bit.clear();
int i = 0;
while (x) {
bit.push_back(x % 2);
x /= 2LL;
i++;
}
while (i < M - 1) {
bit.push_back(0LL);
i++;
}
reverse(bit.begin(), bit.end());
}
void add(long long int x) {
make(x);
int cur = 0, typ;
for (int i = 0; i < M; i++) {
typ = bit[i];
if (a[cur][typ] == -1) {
a[cur][typ] = new_node();
}
cur = a[cur][typ];
++cnt[cur];
}
}
void del(long long int x) {
make(x);
int cur = 0, typ;
for (int i = 0; i < M; i++) {
typ = bit[i];
cur = a[cur][typ];
--cnt[cur];
}
}
long long int query(long long int x) {
make(x);
int cur = 0, typ;
long long int ans = 0LL;
for (int i = 0; i < M; i++) {
typ = bit[i];
if (cnt[a[cur][!typ]] > 0) {
ans += (1LL << (M - i - 2));
cur = a[cur][!typ];
} else {
if (a[cur][typ] == -1) return ans;
cur = a[cur][typ];
}
}
return ans;
}
long long int arr[N];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
id = 0;
new_node();
for (__typeof(n) i = 1; i <= n; i++) {
scanf("%lld", &arr[i]);
}
long long int xr = 0LL;
for (int i = n; i >= 1; i--) {
xr ^= arr[i];
add(xr);
}
long long int mx = 0LL;
long long int nw = 0LL;
mx = max(mx, query(nw));
for (int i = 1; i <= n; i++) {
nw ^= arr[i];
mx = max(mx, query(nw));
del(xr);
xr ^= arr[i];
}
printf("%lld\n", mx);
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
struct NODE {
int val, id;
bool operator<(const NODE &B) const { return val > B.val; }
};
NODE a[MAXN];
int vis[MAXN];
int main() {
int n, k;
long long b, sum = 0;
cin >> n >> k >> b;
for (int i = 1; i <= n; i++) {
cin >> a[i].val;
a[i].id = i;
}
sort(a + 1, a + n);
int ans = n;
for (int i = 1; i < k; i++) {
sum += a[i].val;
vis[a[i].id] = 1;
ans = min(ans, a[i].id);
}
if (sum + a[k].val <= b) {
cout << n << endl;
return 0;
}
for (int i = 1; i < n; i++) {
if (!vis[a[i].id] && sum + a[i].val > b) {
ans = min(ans, a[i].id);
}
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long a[1000005], d[10005], ans;
int b[10005], n, m;
long long gcd(long long a, long long b) {
if (!a || !b) return (!a ? b : a);
if (a < b) swap(a, b);
long long f = a % 2, s = b % 2;
if (f && s) {
return gcd((a - b) / 2, b);
} else if (!f && s) {
return gcd(a / 2, b);
} else if (f && !s) {
return gcd(a, b / 2);
} else {
return (gcd(a / 2, b / 2) * 2);
}
}
void work(int x) {
m = 0;
memset(b, 0, sizeof(b));
for (int i = 1; 1ll * i * i <= a[x]; i++)
if (a[x] % i == 0) {
d[++m] = i;
d[++m] = a[x] / i;
}
sort(d + 1, d + m + 1);
m = unique(d + 1, d + m + 1) - d - 1;
for (int i = 1; i <= n; i++)
b[lower_bound(d + 1, d + m + 1, gcd(a[i], a[x])) - d]++;
for (int i = m; i && d[i] > ans; i--) {
int tmp = 0;
for (int j = i; j <= m; j++)
if (d[j] % d[i] == 0) tmp += b[j];
if (tmp * 2 >= n) {
ans = max(ans, d[i]);
return;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 1; i <= 10; i++) work(((rand() << 15) + rand()) % n + 1);
printf("%lld", ans);
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
string str;
string s[2222];
int z, n, m, nn;
int c[2222][300];
bool u[2222][111];
void erase2(int p, int x, char ch) {
int i, j, k;
c[p][ch]--;
for (i = 0; i < n; i++)
if (!u[p][i] && s[p][i] == ch) {
x--;
if (!x) {
u[p][i] = 1;
break;
}
}
}
void Erase(int x, char ch) {
int i, j, k;
k = 0;
for (i = 1; i <= m; i++) {
if (k + c[i][ch] >= x) break;
k += c[i][ch];
}
erase2(i, x - k, ch);
}
int main() {
int i, j, k;
for (z = 1; z <= 1000; z++)
;
scanf("%d\n", &m);
cin >> str;
n = str.length();
nn = n * m;
j = 0;
for (i = 1; i <= m; i++) s[i] = str;
for (i = 0; i < n; i++) c[1][str[i]]++;
for (i = 2; i <= m; i++)
for (j = 0; j < 300; j++) c[i][j] = c[i - 1][j];
int pp;
scanf("%d\n", &pp);
while (pp--) {
int X;
char ch;
scanf("%d %c\n", &X, &ch);
Erase(X, ch);
}
for (i = 1; i <= m; i++) {
for (j = 0; j < n; j++)
if (!u[i][j]) putchar(s[i][j]);
}
puts("");
return 0;
}
| 9 | CPP |
N=int(input())
xy=[list(map(int,input().split()))for _ in range(N)]
xy1=[x+y for x,y in xy]
xy2=[-x+y for x,y in xy]
ans1=max(xy1)-min(xy1)
ans2=max(xy2)-min(xy2)
print(max(ans1,ans2)) | 0 | PYTHON3 |
word = list(input())
word = word[1:]
word = word[:-1]
num = int((len(word) + 2)/3)
abc = []
for i in range(num):
abc.append(word[3 * i])
set1 = set(abc)
print(int(len(set1))) | 7 | PYTHON3 |
n,a=list(map(int, input().split(" ")))
x,sum,i,j = list(map(int, input().split(" "))),0,a-2,a
while i>=0 or j<n:
if i==-1:
sum+=x[j:].count(1)
break
if j==n:
sum+=x[:i+1].count(1)
break
if x[i] == x[j] == 1: sum += 2
i,j=i-1,j+1
print(sum if x[a-1]==0 else sum+1) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using std::max;
using std::min;
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3f;
__inline__ __attribute__((always_inline)) unsigned int rnd() {
static unsigned int seed = 416;
return seed += 0x71dad4bfu, seed ^= seed >> 5, seed += 0xc6f74d88u,
seed ^= seed << 17, seed += 0x25e6561u, seed ^= seed >> 13;
}
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {
return a >= 0 ? a : -a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {
(a < b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {
(b < a) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void read(_Tp &x) {
char ch(getchar());
bool f(false);
while (!isdigit(ch)) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
f && (x = -x);
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b,
const Args &...args) {
return a < b ? min(a, args...) : min(b, args...);
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b,
const Args &...args) {
return a < b ? max(b, args...) : max(a, args...);
}
__inline__ __attribute__((always_inline)) int read_str(char *s) {
char ch(getchar());
while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
char *tar = s;
*tar = ch, ch = getchar();
while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
*(++tar) = ch, ch = getchar();
return tar - s + 1;
}
const int N = 65;
const int mod = 1000000007;
template <typename _Tp1, typename _Tp2>
__inline__ __attribute__((always_inline)) void add(_Tp1 &a, _Tp2 b) {
(a += b) >= mod && (a -= mod);
}
template <typename _Tp1, typename _Tp2>
__inline__ __attribute__((always_inline)) void sub(_Tp1 &a, _Tp2 b) {
(a -= b) < 0 && (a += mod);
}
int n, a[N], fa[N];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int dp[1 << 15][N], sum[1 << 15];
std::pair<int, int> solve(int ___) {
std::vector<int> nd;
for (int i = 1; i <= n; ++i)
if (find(i) == ___) nd.push_back(a[i]);
std::vector<int> A, B;
for (auto a : nd) {
int cnt = 0;
for (auto b : nd)
if (a != b && !(a % b)) ++cnt;
if (!cnt)
A.push_back(a);
else
B.push_back(a);
}
if (((int)B.size()) <= 1) return std::make_pair(0, 1);
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
for (auto &&it : B) {
int qwq = 0;
for (int i = 0; i < ((int)A.size()); ++i)
if (!(it % A[i])) qwq |= 1 << i;
it = qwq, ++sum[it];
++dp[it][1];
}
for (int i = 0; i < ((int)A.size()); ++i)
for (int st = 0; st < 1 << ((int)A.size()); ++st)
if ((st >> i) & 1) sum[st] += sum[st ^ (1 << i)];
for (int st = 0; st < 1 << ((int)A.size()); ++st) {
for (int j = 1; j <= ((int)B.size()); ++j) {
if (!dp[st][j]) continue;
add(dp[st][j + 1], 1ll * dp[st][j] * (sum[st] - j) % mod);
for (auto it : B)
if ((it & st) && (st & it) != it) add(dp[st | it][j + 1], dp[st][j]);
}
}
return std::make_pair(((int)B.size()) - 1,
dp[(1 << ((int)A.size())) - 1][((int)B.size())]);
}
int C[N][N];
int main() {
for (int i = 0; i < N; ++i)
for (int j = C[i][0] = 1; j <= i; ++j)
C[i][j] = C[i - 1][j - 1], add(C[i][j], C[i - 1][j]);
read(n);
for (int i = 1; i <= n; ++i) read(a[i]), fa[i] = i;
std::sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (!(a[i] % a[j])) fa[find(i)] = find(j);
long long ans = 1, res = 0;
for (int i = 1; i <= n; ++i) {
if (fa[i] == i) {
std::pair<int, int> tmp = solve(i);
res += tmp.first;
ans = ans * C[res][tmp.first] % mod * tmp.second % mod;
}
}
printf("%lld\n", ans);
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int highest=100;
int gerden[11][11];
int main(){
int d, w;
while(cin >> d >> w){
if(d==0 && w==0) break;
for(int i=0; i<d; i++){
for(int j=0; j<w; j++){
cin >> gerden[i][j];
}
}
int ans=0;
for(int i=3; i<=d; i++){
for(int j=3; j<=w; j++){ // 縦i横jの池候補
for(int k=0; k<=d-i; k++){
for(int l=0; l<=w-j; l++){ // 池候補を縦横に動かす
int min_out=100, max_in=-1, capacity=0;
for(int m=k; m<i+k; m++){
for(int n=l; n<j+l; n++){ // 池かどうかを調べる(ついでに容量)
if(m==k || n==l || m==k+i-1 || n==l+j-1){
if(gerden[m][n]<min_out){
min_out = gerden[m][n];
}
}else{
capacity += highest - gerden[m][n];
if(gerden[m][n]>max_in){
max_in = gerden[m][n];
}
}
}
}
if(max_in < min_out){
if(capacity-((100-min_out)*(i-2)*(j-2)) > ans ){
ans = capacity - ((100-min_out)*(i-2)*(j-2));
}
}
}
}
}
}
cout<<ans<<endl;
}
return 0;
}
| 0 | CPP |
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
print(('No', 'Yes')[sum(x) >= sum(y)])
| 7 | PYTHON3 |
s = input()
n = int(input())
d = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
e = 'abcdefghijklmnopqrstuvwxyz'
for i in s:
z = ord(i) - ord('a') if ord('a') <= ord(i) and ord(i) <= ord('z') else ord(i) - ord('A')
if z < n:
print(d[z], end = '')
else:
print(e[z], end = '') | 10 | PYTHON3 |
#include <iostream>
#include <vector>
using namespace std;
long long int dp[ 1 << 16 ][ 31 ] = {};
long long int score( long long int k ) {
long long int ans = 0;
while( k > 0 ) {
ans += ( k % 2 );
k /= 2;
}
return ans;
}
int main() {
while( true ) {
long long int n, c;
cin >> n >> c;
if ( n == 0 ) break;
vector< long long int > a, b;
for ( long long int i = 0; i < n; i++ ) {
long long int k = 0;
for ( long long int j = 0; j < 16; j++ ) {
long long int in;
cin >> in;
k = k * 2 + in;
}
a.push_back( k );
}
for ( long long int i = 0; i < c; i++ ) {
long long int k = 0;
for ( long long int j = 0; j < 16; j++ ) {
long long int in;
cin >> in;
k = k * 2 + in;
}
b.push_back( k );
}
for ( long long int i = 0; i <= n; i++ ) {
for ( long long int j = 0; j < ( 1 << 16 ); j++ ) {
dp[j][i] = 0;
}
}
dp[0][0] = 1;
for ( long long int i = 0; i < n; i++ ) {
for ( long long int j = 0; j < ( 1 << 16 ); j++ ) {
if ( dp[j][i] == 0 ) continue;
long long int k = j | a[i];
for ( long long int p = 0; p < c; p++ ) {
long long int q = k & b[p];
dp[ k ^ q ][ i + 1 ] = max( dp[k^q][i+1], dp[j][i] + score(q) );
}
}
}
long long int ans = 0;
for ( long long int i = 0; i < ( 1 << 16 ); i++ ) {
ans = max( ans, dp[i][n] );
}
if ( ans == 0 ) {
cout << 0 << endl;
}else {
cout << ans - 1 << endl;
}
}
return 0;
} | 0 | CPP |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
struct CL
{
ll b,l,r,ans;
bool operator < (const CL &p)const
{
return ans > p.ans;
}
}c[MAXN];
int n;
ll X,ans;
ll getans(ll v)
{
ll sum = 0,MAX = 0;
for (int i = 1;i <= v / X;i++)
sum += c[i].ans;
for (int i = 1;i <= n;i++)
{
ll val = 0;
if (v % X >= c[i].b)
val = c[i].l * c[i].b + c[i].r * (v % X - c[i].b);
else
val = c[i].l * (v % X);
if (i <= v / X)
MAX = max(MAX,val + sum - c[i].ans + c[v / X + 1].ans);
else
MAX = max(MAX,val + sum);
}
return MAX;
}
int main()
{
scanf("%d%lld",&n,&X);
for (int i = 1;i <= n;i++)
{
scanf("%lld%lld%lld",&c[i].b,&c[i].l,&c[i].r);
ans -= c[i].l * c[i].b;
c[i].ans = c[i].l * c[i].b + c[i].r * (X - c[i].b);
}
sort(c + 1,c + n + 1);
ll low = 0,high = n * X;
while (low < high)
{
ll mid = low + high >> 1;
if (getans(mid) + ans >= 0)
high = mid;
else
low = mid + 1;
}
printf("%lld\n",low);
return 0;
} | 0 | CPP |
#include<iostream>
#include<cstdio>
#define MN 200000
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
char st[MN+5];
int f[MN+5],F[MN+5],g[MN+5],from[MN+5],dep[MN+5],n,head[MN+5],tot=0,mnok[MN+5];
int cnt=0,mx[MN+5],H[MN+5],h[MN+5],mn[MN+5];
struct edge{int to,next;}e[MN*2+5];
inline void ins(int f,int t)
{
e[++cnt]=(edge){t,head[f]};head[f]=cnt;
e[++cnt]=(edge){f,head[t]};head[t]=cnt;
}
void Pre(int x,int fa)
{
f[x]=F[x]=dep[x];H[x]=st[x]=='1';mnok[x]=int(1e9);
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fa)
{
dep[e[i].to]=dep[x]+1;
Pre(e[i].to,x);
if(f[e[i].to]>f[x]) F[x]=f[x],f[x]=f[e[i].to],from[x]=e[i].to;
else F[x]=max(F[x],f[e[i].to]);
H[x]+=H[e[i].to];
if(H[e[i].to]) mnok[x]=min(mnok[x],f[e[i].to]);
}
}
void Dfs(int x,int fa)
{
if(st[x]=='0') mn[x]=min(mnok[x]-dep[x],h[x]);
mx[x]=min(max(f[x]-dep[x],g[x])-1,f[x]-dep[x]+1);
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=fa)
{
int dis2=max((e[i].to==from[x]?F[x]:f[x])-dep[x],g[x]);
mx[x]=min(mx[x],dis2+1);
g[e[i].to]=dis2+1;
h[e[i].to]=tot>H[e[i].to]?dis2+1:int(1e9);
Dfs(e[i].to,x);
}
}
long long ans=1;
int main()
{
n=read();
for(int i=1;i<n;++i) ins(read(),read());
scanf("%s",st+1);
for(int i=1;i<=n;++i) tot+=st[i]=='1';
Pre(1,0);h[1]=int(1e9);Dfs(1,0);
for(int i=1;i<=n;++i) ans+=max(0,mx[i]-mn[i]+1);
printf("%lld\n",ans);
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[4][4];
bool check(int x) {
int r[2][4];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 4; j++) r[i][j] = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) r[0][i] += a[i][j];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) r[1][i] += a[j][i];
r[0][3] += a[0][0] + a[1][1] + a[2][2];
r[1][3] += a[0][2] + a[1][1] + a[2][0];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 4; j++)
if (r[i][j] != r[0][0]) return 0;
return 1;
}
int main() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) cin >> a[i][j];
int ans;
for (int i = 1; i < 1000 * 100 + 1; i++) {
int qq = 0;
a[0][0] = i;
int yu = a[0][0] + a[0][1] + a[0][2];
for (int k = 1; k < 3; k++) {
int oo = 0;
for (int j = 0; j < 3; j++) oo += a[k][j];
a[k][k] = yu - oo;
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (a[i][j] < 0) qq++;
if (qq > 0) continue;
if (check(i)) break;
a[0][0] = 0;
a[1][1] = 0;
a[2][2] = 0;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
| 8 | CPP |
n, m = input().split()
n, m = map(int, [n, m])
ans_list = [input() for i in range(n)]
scores_list = input()
scores_list = list(map(int, scores_list.split(' ')))
list1 = []
ans = ''
c = 0
for j in range(m):
for k in range(n):
ans += ans_list[k][j]
list1.append(ans)
ans = ''
c1 = 0
for l in list1:
a_counter = l.count('A')
b_counter = l.count('B')
c_counter = l.count('C')
d_counter = l.count('D')
e_counter = l.count('E')
c += max(a_counter, b_counter, c_counter, d_counter, e_counter) * scores_list[c1]
c1+=1
print(c) | 7 | PYTHON3 |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
int main() {
while (true) {
int n, s;
cin >> n >> s;
if (n == 0 && s == 0) break;
vector<int> r;
rep (i, n) {
int rr;
cin >> rr;
r.push_back(rr);
}
int res = 0;
sort(r.begin(), r.end());
rep (i, n) {
if (r[i] + r[i] > s) --res;
res += r.end() - upper_bound(r.begin(), r.end(), s - r[i]);
}
cout << res / 2 << endl;
}
return 0;
} | 0 | CPP |
for i in range(int(input())):
a,b=map(int,input().split())
if a!=b:
if abs(a-b)%10!=0:
print(abs(a-b)//10+1)
else:
print(abs(a-b)//10)
else:
print(0) | 7 | PYTHON3 |
a=[]
for i in range(10):
x=int(input())
a.append(x)
a.sort()
print(a[9])
print(a[8])
print(a[7])
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 201;
const int MAXM = 15;
int n, q;
int x[MAXN], y[MAXN];
int vis[MAXN], cnt[MAXN];
double ans;
double f[MAXN][MAXN][MAXM + 1];
double Base[MAXN], zy[MAXN];
vector<int> point[MAXN][MAXN];
vector<pair<int, int> > line;
inline int Read() {
int i = 0, f = 1;
char c;
for (c = getchar(); (c > '9' || c < '0') && c != '-'; c = getchar())
;
if (c == '-') f = -1, c = getchar();
for (; c >= '0' && c <= '9'; c = getchar()) i = (i << 3) + (i << 1) + c - '0';
return i * f;
}
bool check(int a, int b, int c) {
return (y[b] - y[a]) * (x[c] - x[a]) == (y[c] - y[a]) * (x[b] - x[a]);
}
int main() {
n = Read();
for (int i = 1; i <= n; ++i) x[i] = Read(), y[i] = Read();
for (int i = 1; i <= n; ++i) {
memset(vis, 0, sizeof(vis));
for (int j = 1; j <= n; ++j) {
if (i == j) continue;
if (vis[j]) continue;
cnt[i]++;
for (int k = 1; k <= n; ++k) {
if (check(i, j, k)) {
point[i][j].push_back(k);
vis[k] = 1;
}
}
line.push_back(make_pair(point[i][j][0], point[i][j][1]));
}
}
sort(line.begin(), line.end());
line.erase(unique(line.begin(), line.end()), line.end());
int siz1 = line.size();
for (int i = 0; i < siz1; ++i) {
vector<int> &vec = point[line[i].first][line[i].second];
int siz2 = vec.size();
for (int j = 0; j < siz2; ++j) {
for (int k = 0; k < siz2; ++k) {
f[vec[j]][vec[k]][0] += 1.0 / siz2 * 1.0;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
f[i][j][0] /= cnt[i];
}
}
for (int i = 1; i <= MAXM; ++i) {
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= n; ++k) {
if (f[j][k][i - 1] > 1e-6) {
for (int t = 1; t <= n; ++t) {
f[j][t][i] += f[j][k][i - 1] * f[k][t][i - 1];
}
}
}
}
}
q = Read();
while (q--) {
int t = Read(), step = Read() - 1;
memset(Base, 0, sizeof(Base));
Base[t] = 1;
for (int i = 0; i <= MAXM; ++i) {
if ((1 << i) > step) break;
if ((1 << i) & step) {
memset(zy, 0, sizeof(zy));
for (int j = 1; j <= n; ++j) {
if (Base[j] > 1e-6) {
for (int k = 1; k <= n; ++k) {
zy[k] += f[k][j][i] * Base[j];
}
}
}
memcpy(Base, zy, sizeof(zy));
}
}
ans = 0.0;
int siz = line.size();
for (int i = 0; i < siz; ++i) {
vector<int> &vec = point[line[i].first][line[i].second];
double tot = 0.0;
int siz2 = vec.size();
for (int j = 0; j < siz2; ++j) {
tot += Base[vec[j]];
}
tot /= 1.0 * siz2;
ans = max(ans, tot);
}
memset(zy, 0, sizeof(zy));
for (int i = 1; i <= n; ++i) {
if (Base[i] > 1e-6) {
for (int j = 1; j <= n; ++j) {
zy[j] += Base[i] * f[j][i][0];
}
}
}
memcpy(Base, zy, sizeof(zy));
for (int i = 1; i <= n; ++i) ans = max(ans, Base[i]);
printf("%.10lf\n", ans);
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = INT_MAX;
const long double pi = 4 * atan((long double)1);
long long dp[2002][2002];
signed main() {
ios::sync_with_stdio();
cin.tie(0);
cout.tie(0);
;
long long n, m;
cin >> n >> m;
string str;
cin >> str;
dp[1][1] = 1;
dp[0][0] = 1;
long long temp = n - m;
for (long long i = 2; i <= 2000; i++) {
if (i % 2 == 0) dp[i][0] = dp[i - 1][1];
dp[i][i] = 1;
for (long long j = 1; j <= i - 1; j++) {
if ((i - j) % 2 == 0) {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j + 1];
dp[i][j] %= mod;
} else
dp[i][j] = 0;
}
}
long long sum = 0, l = 0;
for (long long i = 0; i < str.length(); i++) {
if (str[i] == '(')
sum++;
else
sum--;
l = min(l, sum);
}
if (l < 0) l = -l;
long long ans = 0;
for (long long i = 0; i <= temp; i++) {
long long res = temp - i;
for (long long j = l; j <= i; j++) {
long long demo = j + sum;
if (demo > res) break;
ans += dp[i][j] * dp[res][demo];
ans %= mod;
}
}
cout << ans;
}
| 9 | CPP |
n, L = [int(x) for x in input().split()]
T = [int(x) for x in input().split()]
for i in range(0,n):
T[i] = (2**i,T[i])
def f(n,L,T):
if L == 0:
return 0
if n == 1:
return (((L-1)//(T[0][0]))+1)*T[0][1]
else:
best = (T[0][0],T[0][1])
for j in T:
if best[1]*j[0] > j[1]*best[0]:
best = j
cost = (((L-1)//best[0]))*best[1]
paid = (((L-1)//best[0]))*best[0]
x = best[1]
T.remove(best)
return cost+min(f(n-1,L-paid,T),x)
print(f(n,L,T)) | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1, s2;
cin >> s1 >> s2;
int h1 = (s1[0] - '0') * 10 + (s1[1] - '0');
int m1 = (s1[3] - '0') * 10 + (s1[4] - '0');
int h2 = (s2[0] - '0') * 10 + (s2[1] - '0');
int m2 = (s2[3] - '0') * 10 + (s2[4] - '0');
int hh = h2 - h1;
int mm = m2 - m1;
int diff = hh * 60 + mm;
diff = diff / 2;
int h_ans = (diff / 60) + h1;
int m_ans = (diff % 60) + m1;
if (m_ans >= 60) {
m_ans = m_ans % 60;
h_ans++;
}
if (h_ans / 10 == 0) cout << "0";
cout << h_ans << ":";
if (m_ans / 10 == 0) cout << "0";
cout << m_ans << "\n";
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
namespace fastIO {
bool IOerror = 0;
inline char nc() {
static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, 100000, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' | ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror) return;
if (ch == '-') sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
if (sign) x = -x;
}
}; // namespace fastIO
using namespace fastIO;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5 + 10;
int a[20][105];
int b[20][105];
std::mt19937 rnd(time(0));
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
a[i + n][j] = a[i][j];
}
}
int x = 1;
long long ans = 0;
while (x++ <= 4000) {
long long res = 0;
for (int j = 1; j <= m; j++) {
int st = rnd() % n + 1;
for (int i = st, k = 1; i <= st + n - 1; i++, k++) {
b[k][j] = a[i][j];
}
}
for (int i = 1; i <= n; i++) {
int maxx = 0;
for (int j = 1; j <= m; j++) maxx = max(maxx, b[i][j]);
res += maxx;
}
ans = max(ans, res);
}
printf("%lld\n", ans);
}
return 0;
}
| 11 | CPP |
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <climits>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
#define REP(i,n) FOR(i, 0, n)
const int INF = INT_MAX/10;
struct state {
int p, t;
state(int p, int t) : p(p), t(t) {};
bool operator< (const state &o)const {
return t>o.t;
}
};
int main() {
int N;
cin >> N;
vi field(N);
REP(i, N) {
cin >> field[i];
}
vi turn(N, INF);
priority_queue<state> Q;
FOR(i, 1, min(N, 6)+1) {
Q.push(state(i, 1));
turn[i] = 1;
}
Q.push(state(0, 0));
turn[0] = 0;
while(!Q.empty()) {
state st = Q.top();
Q.pop();
if(st.p == N-1) {
cout << st.t << endl;
return 0;
}
if(field[st.p] != 0) {
int np = st.p + field[st.p];
if(np >= N) {
np = N-1;
}
if(turn[np] > st.t) {
turn[np] = st.t;
Q.push(state(np, st.t));
}
} else {
FOR(d, 1, 6+1) {
int np = st.p + d;
if(np >= N) {
np = N-1;
}
if(turn[np] > st.t+1) {
turn[np] = st.t+1;
Q.push(state(np, st.t+1));
}
}
}
}
} | 0 | CPP |
n = int(input())
l = list(map(int, input().split()))
min_val = l[0]
max_val = l[0]
c = 0
for i in l[1:]:
if min_val < i:
c += 1
min_val = i
elif max_val > i:
c += 1
max_val = i
print(c) | 7 | PYTHON3 |
MOD=10**9+7
n,m=map(int,input().split())
a=[int(input()) for _ in range(m)]
dp=[0]*(n+1)
dp[0]=1
dp[1]=1 if 1 not in a else 0
a=sorted(list(set(range(2,n+1))-set(a)))
for i in a:
dp[i]=(dp[i-1]+dp[i-2])%MOD
print(dp[n]%MOD) | 0 | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.