solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
n, k, l = mints()
x = 0
pz = k
for d in mints():
z = l - d
if z < 0:
print("No")
return
if z >= k:
x = 0
elif x < k:
x = max(x + 1, k - z)
elif x >= k:
x = x + 1
if x - k > z:
print("No")
return
print("Yes")
for i in range(mint()):
solve() | 8 | PYTHON3 |
def lpf(n,m=0,i=2):
while i*i<=n:
if n%i:i+=1
else:n//=i
return m-n
x=int(input())
a=x
for i in range(lpf(x,x+1),x+1):
if lpf(i,i+1)>=3:a=min(lpf(i,i+1),a)
print(a) | 7 | PYTHON3 |
for i in range(int(input())):
n=int(input())
dict={}
flag=0
for i in range(n):
s=input()
for i in s:
if i not in dict:
dict[i]=1
else:
dict[i]+=1
for i in dict:
if dict[i]%n!=0:
flag=1
break
if flag==1:
print('NO')
else:
print('YES') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long t;
cin >> t;
while (t--) {
long n, k;
string s;
cin >> n >> k;
cin >> s;
string ns = "";
for (int i = 0; i < 26; i++) {
for (int j = 0; j < n; j++) {
if (s[j] == (i + 'a')) {
ns += (i + 'a');
}
}
}
int f = 0;
for (int i = 0; i < k - 1; i++) {
if (ns[i] != ns[i + 1]) {
f = 1;
}
}
if (f == 1) {
cout << ns[k - 1] << endl;
continue;
}
int flag = 0;
for (int i = k; i < n - 1; i++) {
if (ns[i] != ns[i + 1]) {
flag = 1;
}
}
if (flag == 0) {
int tot = n - k;
int pr = 0;
if (tot == 0) {
pr = 0;
} else if (tot <= k) {
pr = 1;
} else {
pr = tot / k;
if (tot % k > 0) {
pr++;
}
}
cout << ns[0];
for (int i = 0; i < pr; i++) {
cout << ns[k];
}
cout << endl;
} else {
for (int i = k - 1; i < n; i++) {
cout << ns[i];
}
cout << endl;
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long a, b, k;
scanf("%lld%lld%lld", &a, &b, &k);
long long ans = 0;
ans = (a - b) * (k >> 1);
if (k & 1) ans += a;
printf("%lld\n", ans);
}
}
| 7 | CPP |
a = input()
a = list(a)
a = set(a)
if len(a) % 2 == 0:
print("CHAT WITH HER!")
else:
print("IGNORE HIM!")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 1912;
struct edge {
int w;
bool in;
int id;
int u, v;
bool operator<(const edge b) const {
if (w == b.w)
return in > b.in;
else
return w < b.w;
}
} e[maxn];
int n, m;
void ReadData() {
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> e[i].w >> e[i].in;
for (int i = 1; i <= m; i++) e[i].id = i;
sort(e + 1, e + 1 + m);
}
int ptr[maxn];
int used;
void fail() {
cout << -1 << endl;
return;
}
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
qu;
bool cmp(const edge a, const edge b) { return a.id < b.id; }
void Process() {
if (!e[1].in) return fail();
used = 2;
ptr[1] = 2;
ptr[2] = 2;
qu.push(pair<int, int>(2, 1));
qu.push(pair<int, int>(2, 2));
e[1].u = 1;
e[1].v = 2;
for (int i = 2; i <= m; i++) {
if (e[i].in) {
used++;
ptr[used - 1] = used;
ptr[used] = used;
e[i].u = used - 1;
e[i].v = used;
qu.push(pair<int, int>(ptr[used - 1], used - 1));
qu.push(pair<int, int>(ptr[used], used));
} else {
bool found = false;
while (((int)qu.size())) {
pair<int, int> tt = qu.top();
qu.pop();
int id = tt.second, val = tt.first;
if (ptr[id] != val) continue;
if (ptr[id] == used) continue;
ptr[id]++;
e[i].u = id;
e[i].v = ptr[id];
qu.push(pair<int, int>(ptr[id], id));
found = true;
break;
}
if (!found) return fail();
}
}
sort(e + 1, e + 1 + m, cmp);
for (int i = 1; i <= m; i++) cout << e[i].u << " " << e[i].v << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ReadData();
Process();
}
| 10 | CPP |
t = int(input())
count = 0
flag = 1;
while(t):
n = int(input())
n_aux = n
words = ""
for i in range(n):
words += input()
words = "".join(sorted(words))
nn_aux = 0
flag = 1
tam = len(words)
for j in range(0, tam, n_aux):
nn_aux += n_aux
if(tam % n_aux != 0):
flag = 0
for k in range(j, nn_aux):
if words[j] != words[k]:
flag = 0
break
if(flag == 0):
break
if(flag):
print("YES")
else:
print("NO")
t -= 1
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, t, cnt;
bool h, e, l1, l2, o;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int size = s.size();
for (__typeof(size - 1) i = 0; i <= size - 1; i += 1) {
if (s[i] == 'h') {
h = true;
}
if (h && s[i] == 'e') {
e = true;
}
if (h && e && s[i] == 'l') {
if (l1) {
l2 = true;
}
l1 = true;
}
if (h && e && l1 && l2 && s[i] == 'o') {
o = true;
}
}
if (h && e && l1 && l2 && o) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
a = (a+b)%MOD;
}
int nth_bit(int64_t num , int n) {
return (num >> n) & 1;
}
int main() {
int64_t N;
cin >> N;
int64_t dp[61][3] = {0};
dp[60][0] = 1;
for(int d = 59; d >= 0; d--) {
for(int s = 0; s <= 2; s++) {
for (int k = 0; k <= 2; k++) {
int s2 = min(2, 2*s+nth_bit(N, d) - k);
if (s2 >= 0) add(dp[d][s2], dp[d+1][s]);
}
}
}
int64_t ans = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD;
cout << ans << endl;
return 0;
} | 0 | CPP |
n = int(input())
a = [int(j) for j in input().split()]
x = a.count(200)
y = a.count(100)
if(x%2==0 and y%2==0):
print("YES")
elif(x%2==1 and y%2==0 and y>=2):
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline long long bs(long long a) { return a > 0 ? a : -a; }
int main() {
int n, x, m, y;
while (cin >> n >> x >> m >> y) {
long long ans = n + 1;
for (int i = 1; i <= m; i++) {
long long a = y - i - x;
long long b = y + i - x;
if (a * b >= 0) {
a = bs(a);
b = bs(b);
if (a > n) a = n + 1;
if (b > n) b = n + 1;
if (a < n || b < n)
ans += (bs(a - b) - 1) * 2;
else
ans += 1;
} else {
a = bs(a);
b = bs(b);
if (a > n) a = n + 1;
if (b > n) b = n + 1;
if (a < n || b < n)
ans += (bs(a - b)) * 2 - 1;
else
ans += 1;
}
}
cout << ans << endl;
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const N = 200 * 1000 + 16;
int const M = 1000 * 1000 * 1000 + 7;
int n;
pair<int, int> a[N];
int dp[N];
int z[N];
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
int x, w;
cin >> x >> w;
a[i] = {x + w, x - w};
}
sort(a, a + n);
int best = 1;
int last = a[0].first;
for (int i = 1; i < n; i += 2) {
if (last <= a[i].second) {
last = a[i].first;
best++;
}
if (i + 1 == n) break;
if (last <= a[i + 1].second) {
last = a[i + 1].first;
best++;
}
}
cout << best;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long RR() {
long long v = 0, k = 1;
char c = 0;
while ('0' > c || c > '9') {
if (c == '-') k = -1;
c = getchar();
}
while ('0' <= c && c <= '9') v = v * 10 + c - 48, c = getchar();
return v * k;
}
inline void W(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) W(x / 10);
putchar(x % 10 + 48);
}
inline void WW(long long x) {
W(x);
puts("");
}
inline void read(int &x, int &y) {
x = RR();
y = RR();
}
const int N = 2e5 + 5;
int f[N][23], dp[N];
int n, m, x, y, z, A, B, ans;
int lca(int x, int y) {
if (dp[y] > dp[x]) swap(x, y);
for (int j = 22; j >= 0; j--)
if (dp[f[x][j]] >= dp[y]) x = f[x][j];
if (x == y) return x;
for (int j = 22; j >= 0; j--)
if (f[x][j] != f[y][j]) x = f[x][j], y = f[y][j];
return f[x][0];
}
int main() {
n = RR();
dp[1] = 1;
x = RR();
dp[2] = 2;
f[2][0] = 1;
A = 1;
B = 2;
ans = 1;
W(ans);
putchar(' ');
for (int i = 3; i <= n; i++) {
x = RR();
dp[i] = dp[x] + 1;
f[i][0] = x;
for (int j = 1; j <= 22; j++) f[i][j] = f[f[i][j - 1]][j - 1];
y = dp[i] + dp[A] - dp[lca(i, A)] * 2;
z = dp[i] + dp[B] - dp[lca(i, B)] * 2;
ans = max(ans, max(y, z));
if (ans == y)
B = i;
else if (ans == z)
A = i;
W(ans);
putchar(' ');
}
}
| 9 | CPP |
import itertools
while True:
n = int(input())
if n == 0:
break
score_lst = [int(i) for i in input().split()]
dist = 1000001
for i1, i2 in itertools.combinations(score_lst, 2):
if abs(i1 - i2) < dist:
dist = abs(i1 - i2)
print(dist)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m, n, h, i, j, k, w, z, t, p, q;
cin >> t;
while (t--) {
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int mindist = INT_MAX, ans = -1;
for (i = 0; i + k < n; i++) {
if ((a[i + k] - a[i] + 1) / 2 < mindist) {
mindist = (a[i + k] - a[i] + 1) / 2;
ans = a[i] + mindist;
}
}
cout << ans << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> v;
int main() {
long long n;
cin >> n;
long long inx, iny;
for (long long i = 0; i < n; i++) {
cin >> inx >> iny;
v.push_back(make_pair(inx, iny));
}
if (n % 2 == 1) {
cout << "NO" << endl;
return 0;
}
for (long long i = 0; i + 1 < n / 2; i++) {
if (((v[i + 1].first - v[i].first) !=
(v[i + n / 2].first - v[i + 1 + n / 2].first)) ||
((v[i + 1].second - v[i].second) !=
(v[i + n / 2].second - v[i + 1 + n / 2].second))) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount
#define INF 1e16
#define mod 1000000007
int n,m;
bool used[200010];
vector<int> a;
int main(){
cin>>n>>m;
rep(i,m){
int aa;
cin>>aa;
aa--;
a.push_back(aa);
}
reverse(all(a));
vector<int> res;
rep(i,m){
if(used[a[i]])continue;
used[a[i]]=true;
res.push_back(a[i]);
}
rep(i,n)if(!used[i])res.push_back(i);
rep(i,n)cout<<res[i]+1<<endl;
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
int l, r = N;
l = max(0, (int)count(A.begin(), A.end(), 1)-1);
while (r-l > 1) {
int c = (19*l+r+18)/20;
map<int, int> mp;
for (int i = 1; i < N; i++) {
if (A[i-1] >= A[i]) {
if (c == 1) {
mp[0] = 1;
break;
}
int t = A[i];
mp[t]++;
auto it = mp.upper_bound(t);
mp.erase(it, mp.end());
while (t > 0 && mp[t] == c) {
mp[t] = 0;
mp[t-1]++;
t--;
}
}
if (mp[0] > 0) break;
}
if (mp[0] == 0) r = c;
else l = c;
}
cout << r << endl;
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a, ctr = 0;
map<int, int> M;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a;
if (M[a] == 0 && a != 0) ctr++;
M[a]++;
}
cout << ctr << "\n";
return 0;
}
| 7 | CPP |
n = int(input())
l = list(map(int, input().split()))
b = 10 ** 9
for i in range(0, len(l)):
b = min(b, l[i] / max(i, n - i - 1))
print(int(b)) | 8 | PYTHON3 |
import sys
import math as mt
import bisect
input=sys.stdin.readline
#t=int(input())
t=1
def ncr_util():
inv[0]=inv[1]=1
fact[0]=fact[1]=1
for i in range(2,300001):
inv[i]=(inv[i%p]*(p-p//i))%p
for i in range(1,300001):
inv[i]=(inv[i-1]*inv[i])%p
fact[i]=(fact[i-1]*i)%p
def solve(d1,d2):
ans1,ans2=0,0
for i in ['R','S','P']:
x=0
for j in ['R','S','P']:
if win[i]!=j:
x+=d2[j]
ans1+=min(d1[i],x)
for j in ['R','S','P']:
if win[i]==j:
ans2+=min(d1[i],d2[j])
return [n-ans1,ans2]
for _ in range(t):
n=int(input())
#n,m=(map(int,input().split()))
#n1=n
#a=int(input())
#b=int(input())
#a,b,c,r=map(int,input().split())
#x2,y2=map(int,input().split())
a1,a2,a3=map(int,input().split())
b1,b2,b3=map(int,input().split())
d1={}
d1['R']=a1
d1['S']=a2
d1['P']=a3
d2={}
d2['R']=b1
d2['S']=b2
d2['P']=b3
win={}
win['R']='S'
win['S']='P'
win['P']='R'
#n=int(input())
#s=input()
#s1=input()
#p=input()
#l=list(map(int,input().split()))
#l2=list(map(int,input().split()))
#l=str(n)
#l.sort(reverse=True)
#l2.sort(reverse=True)
#l1.sort(reverse=True)
print(*solve(d1,d2))
| 11 | PYTHON3 |
n = int(input())
for test in range(n):
m, k = map(int, input().split())
if m % 2 == 1 and k % 2 == 0:
print('NO')
elif (m % 2 == 1 and k % 2 == 1) or (m % 2 == 0 and k % 2 == 0):
l = ['1']*(k-1)
m -= k-1
if m % 2 != 1 or m <= 0:
print('NO')
else:
print('YES')
l.append(str(m))
print(' '.join(l))
elif m % 2 == 0 and k % 2 == 1:
l = ['2']*(k-1)
m -= (k-1)*2
if m % 2 != 0 or m <= 0:
print('NO')
else:
print('YES')
l.append(str(m))
print(' '.join(l))
| 8 | PYTHON3 |
import sys, os.path
from collections import*
from copy import*
import math
mod=10**9+7
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
for t in range(int(input())):
x1,y1,x2,y2=map(int,input().split())
if(x1==x2):
print(abs(y2-y1))
elif(y1==y2):
print(abs(x2-x1))
else:
print(abs(x2-x1)+1+abs(y2-y1)+1)
| 7 | PYTHON3 |
participants,cutoffrank=map(int,input().split())
pointsearned=list(reversed(sorted(list(map(int,input().split())))))
cutoffpoint=pointsearned[cutoffrank-1]
counter=0
for points in pointsearned:
if points>=cutoffpoint and points>0:
counter+=1
else:
break
print(counter) | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int main (){
int n,m;
cin>>n>>m;
vector<vector<int>> v(100005);
for(int i=0;i<n;i++){
int d,val;
cin>>d>>val;
v[d].push_back(val);
}
long long int ans=0;
priority_queue<int> q;
for(int i=1;i<=m;i++){
for(int j=0;j<v[i].size();j++) q.push(v[i][j]);
if(!q.empty()) ans+=q.top(),q.pop();
}
cout<<ans;
return 0;
} | 0 | CPP |
n=int(input())
a=[]
b=[]
for i in range(n):
a.append(int(input()))
for i in set(a):
b.append(i)
if len(b) == 2 and a.count(b[0]) == a.count(b[1]):
print("YES")
print(b[0],end=" ")
#print(end=" ")
print(b[1])
else:
print("NO")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct two_sat {
static const int MAXV = 1.1e6;
int V;
vector<int> implies[MAXV * 2];
int new_var() {
assert(V < MAXV);
return ((V++) * 2 + 1);
}
inline int n(int a) { return a ^ 1; }
void constrain(int a, int b) {
if (a < 0 || b < 0) return;
implies[a].push_back(b);
implies[n(b)].push_back(n(a));
}
int make_and(int a, int b) {
if (a < 0) return b;
if (b < 0) return a;
int v = new_var();
constrain(v, a);
constrain(v, b);
return v;
}
int make_or(int a, int b) {
if (a < 0) return b;
if (b < 0) return a;
int v = new_var();
constrain(a, v);
constrain(b, v);
return v;
}
int make_at_most_one(int a, int b) {
if (a < 0) return b;
if (b < 0) return a;
constrain(a, n(b));
return make_or(a, b);
}
bool solution[MAXV * 2];
bool vis[2][MAXV * 2];
int q[2][MAXV * 2];
bool solve() {
for (int i = 0; i < 2 * V; i++) {
solution[i] = false;
vis[0][i] = vis[1][i] = false;
}
for (int i = 0; i < V; i++) {
if (!solution[2 * i + 0] && !solution[2 * i + 1]) {
bool bad[2] = {false, false};
int qs[2] = {0, 0};
int qi[2] = {0, 0};
int qt[2] = {0, 0};
for (int z = 0; z < 2; z++) {
vis[z][2 * i + z] = true;
q[z][qt[z]++] = 2 * i + z;
}
while (true) {
if (bad[0] && bad[1]) {
return false;
}
for (int z = 0; z < 2; z++) {
if (bad[z]) continue;
if (qs[z] >= qt[z]) {
for (int ind = 0; ind < qt[z]; ind++) {
solution[q[z][ind]] = true;
}
bad[!z] = true;
goto done;
}
int v = q[z][qs[z]];
if (qi[z] == int(implies[v].size())) {
qs[z]++;
qi[z] = 0;
continue;
}
int u = implies[v][qi[z]];
assert(!solution[n(u)]);
if (vis[z][n(u)]) {
bad[z] = true;
} else if (!solution[u] && !vis[z][u]) {
vis[z][u] = true;
q[z][qt[z]++] = u;
}
qi[z]++;
}
}
done:
assert(bad[0] + bad[1] == 1);
for (int z = 0; z < 2; z++) {
for (int ind = 0; ind < qt[z]; ind++) {
vis[z][q[z][ind]] = false;
}
}
}
assert(solution[2 * i + 0] + solution[2 * i + 1] == 1);
}
return true;
}
} ts;
const int MAXN = int(1e5) + 20;
int N;
vector<int> adj[MAXN];
int depth[MAXN];
int par[MAXN][20];
int sz[MAXN];
int lca(int a, int b) {
if (depth[a] != depth[b]) {
if (depth[a] > depth[b]) swap(a, b);
assert(depth[b] > depth[a]);
int d = depth[b] - depth[a];
assert(d > 0);
for (int i = 0; (1 << i) <= d; i++) {
if (d & (1 << i)) b = par[b][i];
}
}
if (a == b) return a;
int i = 0;
while (par[a][i] != par[b][i]) i++;
assert(par[a][i] == par[b][i]);
for (; i > 0; i--) {
assert(par[a][i] == par[b][i]);
if (par[a][i - 1] != par[b][i - 1]) {
a = par[a][i - 1];
b = par[b][i - 1];
}
assert(par[a][i - 1] == par[b][i - 1]);
}
return par[a][0];
}
void dfs_lca(int cur, int prv = 0) {
depth[cur] = depth[prv] + 1;
par[cur][0] = prv;
for (int i = 0; par[cur][i] != 0; i++) {
par[cur][i + 1] = par[par[cur][i]][i];
}
sz[cur] = 1;
for (int nxt : adj[cur]) {
if (nxt == prv) continue;
dfs_lca(nxt, cur);
sz[cur] += sz[nxt];
}
}
int hld_ind[MAXN];
int hld_top[MAXN];
void dfs_hld(int cur = 1, int prv = 0, int hpar = 1, int ind = 1) {
hld_ind[cur] = ind;
hld_top[cur] = hpar;
ind++;
for (int nxt : adj[cur]) {
if (nxt == prv) continue;
if (sz[nxt] * 2 >= sz[cur]) {
dfs_hld(nxt, cur, hpar, ind);
ind += sz[nxt];
break;
}
}
for (int nxt : adj[cur]) {
if (nxt == prv) continue;
if (sz[nxt] * 2 >= sz[cur]) {
continue;
}
dfs_hld(nxt, cur, nxt, ind);
ind += sz[nxt];
}
assert(ind == hld_ind[cur] + sz[cur]);
}
const int S = 1 << 17;
int seg[S * 2];
const int MAXM = 1.1e4;
int M;
int ant_vars[MAXM];
int main() {
scanf("%d", &N);
for (int i = 0; i < N - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
depth[0] = 0;
dfs_lca(1);
hld_ind[0] = 0;
hld_top[0] = 0;
dfs_hld();
memset(seg, -1, sizeof(seg));
scanf("%d", &M);
for (int i = 0; i < M; i++) {
int v = ts.new_var();
ant_vars[i] = v;
for (int z = 0; z < 2; z++, v = ts.n(v)) {
int a[2];
scanf("%d %d", &a[0], &a[1]);
int c = lca(a[0], a[1]);
for (int y = 0; y < 2; y++) {
int cur = a[y];
while (cur != c) {
int nxt;
int top = hld_top[cur];
int l, r;
if (depth[top] > depth[c]) {
nxt = par[top][0];
l = hld_ind[top];
r = hld_ind[cur];
} else {
assert(hld_top[c] == top);
nxt = c;
l = hld_ind[c] + 1;
r = hld_ind[cur];
}
r++;
for (l += S, r += S; l < r; l /= 2, r /= 2) {
if (l & 1) {
seg[l] = ts.make_at_most_one(seg[l], v);
l++;
}
if (r & 1) {
r--;
seg[r] = ts.make_at_most_one(seg[r], v);
}
}
cur = nxt;
}
}
}
}
for (int i = S - 1; i > 0; i--) {
seg[i] =
ts.make_at_most_one(seg[i], ts.make_or(seg[2 * i + 0], seg[2 * i + 1]));
}
if (ts.solve()) {
printf("YES\n");
for (int i = 0; i < M; i++) {
printf("%c\n", ts.solution[ant_vars[i]] ? '1' : '2');
}
} else {
printf("NO\n");
}
return 0;
}
| 10 | CPP |
N = int(input())
a = map(int, input().split())
print(sum(a) - N)
| 0 | PYTHON3 |
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
#define eb emplace_back
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(ll i=(a); i<=(b); i++)
#define ROF(i, a, b) for(ll i=(a); i>=(b); i--)
#define chkmax(x, a) x = max(x, a)
#define chkmin(x, a) x = min(x, a)
ll fillable, any, na, nb;
void insert(queue<ll>& a, queue<ll>& x, ll ts, ll ti, ll dstSz) {
FOR(i, 1, ts) {
if (a.size() != dstSz) a.emplace(ti);
else x.emplace(ti);
}
}
void solve(){
ll n, nc; cin >> n >> nc; ll t;
vector<pair<ll, ll>> c; FOR(i, 1, nc) cin >> t, c.eb(t, i);
sort(all(c), greater<pair<ll, ll>>());
ll lo = 1, hi = 1000;
while (lo < hi) {
ll mi = lo + hi >> 1;
fillable = mi * mi - (mi / 2) * (mi / 2);
any = ((mi + 1) / 2) * ((mi + 1) / 2);
na = nb = ((mi + 1) / 2) * (mi / 2);
if (fillable >= n && na + any >= c[0].first) hi = mi;
else lo = mi + 1;
}
fillable = lo * lo - (lo / 2) * (lo / 2);
any = ((lo + 1) / 2) * ((lo + 1) / 2);
na = nb = ((lo + 1) / 2) * (lo / 2);
vector<vl> m(lo + 1, vl(lo+1));
queue<ll> a, b, x;
for (auto [ts, ti]: c) {
if (a.size() != na) insert(a, x, ts, ti, na);
else insert(b, x, ts, ti, nb);
}
cout << lo << endl;
FOR(i, 1, lo) {
FOR(j, 1, lo) {
if (i % 2 && j % 2 && x.size()) m[i][j] = x.front(), x.pop();
else if (i % 2 && j % 2 == 0 && a.size()) m[i][j] = a.front(), a.pop();
else if (i % 2 == 0 && j % 2 && b.size()) m[i][j] = b.front(), b.pop();
cout << m[i][j] << " ";
}
cout << "\n";
}
cout.flush();
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll T = 1;
cin >> T;
while (T--) solve();
return 0;
} | 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mx = 1e5 + 666;
const long long mod = 1e9 + 7;
const long long inf = 1e7;
const int llg = 18;
long long power(long long a, long long b) {
if (b == 0) return 1;
if (b % 2)
return a * power(a, b - 1) % mod;
else
return power(a * a % mod, b / 2);
}
long long n, cn = 1, a[mx], dis[mx], par[mx], ans = inf;
vector<long long> g[mx];
int dij() {
for (int i = 1; i < cn + 1; i++) {
fill(dis, dis + cn + 6, inf);
fill(par, par + cn + 6, -1);
queue<long long> q;
dis[i] = 0;
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : g[u]) {
if (dis[v] == inf) {
dis[v] = dis[u] + 1;
par[v] = u;
q.push(v);
}
if (v != par[u] && par[v] != u) ans = min(ans, dis[v] + dis[u] + 1);
}
}
}
if (ans == inf)
return -1;
else
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n + 1; i++) {
long long x;
cin >> x;
if (x > 0) {
a[cn] = x;
cn++;
}
}
if (cn > 128)
cout << 3;
else {
for (int i = 1; i < cn + 1; i++) {
dis[i] = inf;
par[i] = -1;
for (int j = i + 1; j < cn + 1; j++) {
long long x = a[i] & a[j];
if (x != 0) {
g[i].push_back(j);
g[j].push_back(i);
}
}
}
cout << dij();
}
}
| 8 | CPP |
n = int(input())
a = [int(e) for e in input().split()]
if 1 in a:
print('HARD')
else:
print('EASY') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
};
int n, k;
cin >> n >> k;
if (k < 3 * n) {
cout << 3 * n - k;
} else {
cout << "0";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q;
int n, m;
vector<int> AdjList[300001], re[300001];
int deg[300001];
int f[300001], p[300001];
int in[300001], out[300001];
int L[300001], R[300001], id[300011];
bool vis[300001], on_P[300001];
vector<int> P, S;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
AdjList[u].push_back(v);
re[v].push_back(u);
deg[v]++;
}
for (int i = 1; i <= n; i++)
if (!deg[i]) {
q.push(pair<int, int>(i, 1));
f[i] = 1;
}
while (!q.empty()) {
int u = q.front().first;
int x = q.front().second;
q.pop();
for (int i = 0; i < AdjList[u].size(); i++) {
int v = AdjList[u][i];
deg[v]--;
if (x + 1 > f[v]) {
f[v] = x + 1;
p[v] = u;
}
if (!deg[v]) q.push(pair<int, int>(v, f[v]));
}
}
int ed = -1, mx = 0;
for (int i = 1; i <= n; i++)
if (f[i] > mx) {
ed = i;
mx = f[i];
}
while (ed) {
on_P[ed] = true;
P.push_back(ed);
ed = p[ed];
}
reverse(P.begin(), P.end());
queue<int> cal_l, cal_r;
for (int i = 1; i <= n; i++) {
L[i] = P.size();
R[i] = -1;
}
int pre = 0;
for (int i = 1; i <= n; i++) vis[i] = false;
for (int i = P.size() - 1; i >= 0; i--) {
int st = P[i];
cal_r.push(st);
vis[st] = true;
while (!cal_r.empty()) {
int u = cal_r.front();
cal_r.pop();
out[st]++;
for (int j = 0; j < AdjList[u].size(); j++) {
int v = AdjList[u][j];
if (!vis[v] && !on_P[v]) {
vis[v] = true;
R[v] = i;
cal_r.push(v);
}
}
}
out[st] += out[pre];
pre = st;
}
pre = 0;
for (int i = 1; i <= n; i++) vis[i] = false;
for (int i = 0; i < P.size(); i++) {
int st = P[i];
cal_l.push(st);
vis[st] = true;
while (!cal_l.empty()) {
int u = cal_l.front();
cal_l.pop();
in[st]++;
for (int j = 0; j < re[u].size(); j++) {
int v = re[u][j];
if (!vis[v] && !on_P[v]) {
vis[v] = true;
L[v] = i;
cal_l.push(v);
}
}
}
in[st] += in[pre];
pre = st;
}
for (int i = 1; i <= n; i++)
if (!on_P[i] && (L[i] - 2 == R[i])) {
if (id[L[i]])
id[L[i]] = -1;
else if (!id[L[i]])
id[L[i]] = i;
}
for (int i = 1; i <= n; i++)
if (!on_P[i]) in[i] = out[i] = 1;
queue<int> CurrList;
for (int i = 1; i <= n; i++) vis[i] = false;
for (int i = P.size() - 1; i >= 0; i--) {
int u = P[i];
CurrList.push(u);
vis[u] = true;
while (!CurrList.empty()) {
int cur = CurrList.front();
CurrList.pop();
for (int j = 0; j < AdjList[cur].size(); j++) {
int v = AdjList[cur][j];
if (!on_P[v] && !vis[v]) {
vis[v] = true;
CurrList.push(v);
}
}
}
if (id[i] >= 1) {
out[id[i]] += out[u];
CurrList.push(id[i]);
vector<int> tmp;
vis[id[i]] = true;
while (!CurrList.empty()) {
int cur = CurrList.front();
CurrList.pop();
tmp.push_back(cur);
for (int j = 0; j < AdjList[cur].size(); j++) {
int v = AdjList[cur][j];
if (!on_P[v] && !vis[v]) {
vis[v] = true;
out[id[i]]++;
CurrList.push(v);
}
}
}
for (int i = 0; i < tmp.size(); i++) vis[tmp[i]] = false;
}
}
for (int i = 1; i <= n; i++) vis[i] = false;
for (int i = 0; i < P.size(); i++) {
int u = P[i];
CurrList.push(u);
vis[u] = true;
while (!CurrList.empty()) {
int cur = CurrList.front();
CurrList.pop();
for (int j = 0; j < re[cur].size(); j++) {
int v = re[cur][j];
if (!on_P[v] && !vis[v]) {
vis[v] = true;
CurrList.push(v);
}
}
}
if (id[i + 2] >= 1) {
in[id[i + 2]] += in[u];
CurrList.push(id[i + 2]);
vector<int> tmp;
vis[id[i + 2]] = true;
while (!CurrList.empty()) {
int cur = CurrList.front();
CurrList.pop();
tmp.push_back(cur);
for (int j = 0; j < re[cur].size(); j++) {
int v = re[cur][j];
if (!on_P[v] && !vis[v]) {
vis[v] = true;
in[id[i + 2]]++;
CurrList.push(v);
}
}
}
for (int i = 0; i < tmp.size(); i++) vis[tmp[i]] = false;
}
}
int res = 0;
for (int i = 1; i <= n; i++)
if ((in[i] + out[i] == n) || (in[i] + out[i] == n + 1)) res++;
cout << res;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
string a;
cin >> a;
int n = a.length(), i, j;
if (n <= 2) {
cout << a << "\n";
return 0;
}
for (i = 2; i < n;) {
for (j = 0; j < 3; j++) {
if ((a[i - j] == 'a' || a[i - j] == 'e' || a[i - j] == 'i' ||
a[i - j] == 'o' || a[i - j] == 'u'))
break;
}
if (j == 3 && !(a[i] == a[i - 1] && a[i - 1] == a[i - 2])) {
cout << a[i - 2] << a[i - 1];
cout << " ";
if (i + 2 >= n) break;
i += 2;
} else {
cout << a[i - 2];
i++;
if (i == n) cout << a[i - 2] << a[i - 1];
}
}
for (; i < n; i++) {
cout << a[i];
}
cout << "\n";
}
| 9 | CPP |
s=list(map(int,input().split(" ")))
print(4-len(set(s))) | 7 | PYTHON3 |
x1,y1,x2,y2=map(int,input().split())
if x1==x2:
y=y2-y1
print(x1+y,y1,x2+y,y2)
elif y1==y2:
x=x2-x1
print(x1,y1+x,x2,y2+x)
else:
if abs(x1-x2)==abs(y1-y2):
print(x1,y2,x2,y1)
else:
print(-1) | 7 | PYTHON3 |
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007
#2005_c
"""
n = int(input())
k = list("mcxi")
for i in range(n):
d = {"m":0,"c":0,"x":0,"i":0}
a,b = input().split()
a = list(a)
b = list(b)
a.insert(0,1)
b.insert(0,1)
for j in range(1,len(a)):
if a[j] in k:
if a[j-1] in k:
d[a[j]] += 1
else:
d[a[j]] += int(a[j-1])
for j in range(1,len(b))[::-1]:
if b[j] in k:
if b[j-1] in k:
d[b[j]] += 1
else:
d[b[j]] += int(b[j-1])
if d[b[j]] >= 10:
l = b[j]
while d[l] >= 10:
d[l] -= 10
l = k[k.index(l)-1]
d[l] += 1
for j in k:
if d[j]:
if d[j] == 1:
print(j,end = "")
else:
print(str(d[j])+j,end = "")
print()
"""
#2017_c
"""
while 1:
h, w = map(int, input().split())
if h == w == 0:
break
s = [list(map(int, input().split())) for i in range(h)]
ans = 0
for u in range(h):
for d in range(u+2,h):
for l in range(w):
for r in range(l+2,w):
m = float("inf")
for i in range(u,d+1):
m = min(m,s[i][l],s[i][r])
for i in range(l,r+1):
m = min(m,s[u][i],s[d][i])
f = 1
su = 0
for i in range(u+1,d):
for j in range(l+1,r):
su += (m-s[i][j])
if s[i][j] >= m:
f = 0
break
if not f:
break
if f:
ans = max(ans,su)
print(ans)
"""
#2016_c
"""
while 1:
m,n = map(int, input().split())
if m == n == 0:
break
ma = 7368791
d = [0]*(ma+1)
z = m
for i in range(n):
while d[z]:
z += 1
j = z
while j <= ma:
d[j] = 1
j += z
for j in range(z,ma+1):
if not d[j]:
print(j)
break
"""
#2018_c
"""
def factorize(n):
if n < 4:
return [1,n]
i = 2
l = [1]
while i**2 <= n:
if n%i == 0:
l.append(i)
if n//i != i:
l.append(n//i)
i += 1
l.append(n)
l.sort()
return l
while 1:
b = int(input())
if b == 0:
break
f = factorize(2*b)
for n in f[::-1]:
a = 1-n+(2*b)//n
if a >= 1 and a%2 == 0:
print(a//2,n)
break
"""
#2010_c
"""
import sys
dp = [100]*1000000
dp_2 = [100]*1000000
dp[0] = 0
dp_2[0] = 0
for i in range(1,181):
s = i*(i+1)*(i+2)//6
for j in range(s,1000000):
if dp[j-s]+1 < dp[j]:
dp[j] = dp[j-s]+1
if s%2:
for j in range(s,1000000):
if dp_2[j-s]+1 < dp_2[j]:
dp_2[j] = dp_2[j-s]+1
while 1:
m = int(sys.stdin.readline())
if m == 0:
break
print(dp[m],dp_2[m])
"""
#2015_c
"""
from collections import deque
while 1:
n = int(input())
if n == 0:
break
s = [input() for i in range(n)]
d = [s[i].count(".") for i in range(n)]
m = max(d)
c = [s[i][-1] for i in range(n)]
q = deque()
for i in range(1,m+1)[::-1]:
j = 0
while j < n:
for k in range(j,n):
if d[k] == i:break
else:
break
j = k
op = c[j-1]
while j < n and d[j] == i:
q.append(j)
j += 1
j = k
if op == "+":
k = 0
while q:
x = q.pop()
k += int(c[x])
c.pop(x)
d.pop(x)
n -= 1
else:
k = 1
while q:
x = q.pop()
k *= int(c[x])
c.pop(x)
d.pop(x)
n -= 1
c[j-1] = k
print(c[0])
"""
#2013_c
"""
from collections import defaultdict
def parse_expr(s,i):
i += 1
if s[i] == "[":
q = []
while s[i] != "]":
e,i = parse_expr(s,i)
q.append(e)
return (calc(q),i+1)
else:
n,i = parse_num(s,i)
return (calc([n]),i+1)
def parse_num(s,i):
m = int(s[i])
i += 1
while f_num[s[i]]:
m *= 10
m += int(s[i])
i += 1
return (m,i)
def calc(q):
if len(q) == 1:
return (q[0]+1)//2
q.sort()
return sum(q[:len(q)//2+1])
f_num = defaultdict(lambda : 0)
for i in range(10):
f_num[str(i)] = 1
n = int(input())
for i in range(n):
s = input()
print(parse_expr(s,0)[0])
"""
#2003_C
"""
while 1:
w,h = LI()
if w == h == 0:
break
s = SR(h)
dp = [[0]*w for i in range(h)]
for y in range(h):
for x in range(w):
if s[y][x].isdecimal():
dp[y][x] = max(dp[y-1][x],dp[y][x-1])*10+int(s[y][x])
ans = 0
for i in dp:
ans = max(ans,max(i))
print(ans)
"""
#2008_C
"""def parse_formula(s,i):
if s[i] == "-":
i += 1
e,i = parse_formula(s,i)
return 2-e,i
elif s[i] == "(":
i += 1
e1,i = parse_formula(s,i)
op = s[i]
i += 1
e2,i = parse_formula(s,i)
i += 1
return calc(op,e1,e2),i
else:
return int(s[i]),i+1
def calc(op,a,b):
if op == "*":
return min(a,b)
else:
return max(a,b)
while 1:
s = S()
if s[0] == ".":
break
t = []
f = defaultdict(int)
for p in range(3):
f["P"] = p
for q in range(3):
f["Q"] = q
for r in range(3):
f["R"] = r
t.append([f[s[i]] if s[i] in "PQR" else s[i] for i in range(len(s))])
ans = 0
for s in t:
if parse_formula(s,0)[0] == 2:
ans += 1
print(ans)
"""
#2011_C
"""
d = [(1,0),(-1,0),(0,1),(0,-1)]
def dfs(dep,s):
global ans
if dep == 5:
b = bfs(s)
m = 0
for i in b:
m += sum(i)
ans = max(ans, m)
else:
if dep < 4:
for i in range(6):
b = bfs(s)
dfs(dep+1,[[i+1 if b[y][x] else s[y][x] for x in range(w)] for y in range(h)])
else:
b = bfs(s)
dfs(dep+1,[[c if b[y][x] else s[y][x] for x in range(w)] for y in range(h)])
def bfs(s):
b = [[0]*w for i in range(h)]
b[0][0] = 1
q = deque([(0,0)])
while q:
y,x = q.popleft()
for dy,dx in d:
y_ = y+dy
x_ = x+dx
if 0 <= y_ < h and 0 <= x_ < w:
if not b[y_][x_] and s[y_][x_] == s[0][0]:
b[y_][x_] = 1
q.append((y_,x_))
return b
while 1:
h,w,c = LI()
if h == 0:
break
s = LIR(h)
ans = 0
dfs(0,s)
print(ans)
"""
#2012_C
def make_dice(t,f):
if t == 1:
if f == 2:
return [t,f,3,7-f,4,7-t]
if f == 3:
return [t,f,5,7-f,2,7-t]
if f == 5:
return [t,f,4,7-f,3,7-t]
if f == 4:
return [t,f,2,7-f,5,7-t]
if t == 2:
if f == 1:
return [t,f,4,7-f,3,7-t]
if f == 4:
return [t,f,6,7-f,1,7-t]
if f == 6:
return [t,f,3,7-f,4,7-t]
if f == 3:
return [t,f,1,7-f,6,7-t]
if t == 3:
if f == 1:
return [t,f,2,7-f,5,7-t]
if f == 2:
return [t,f,6,7-f,1,7-t]
if f == 6:
return [t,f,5,7-f,2,7-t]
if f == 5:
return [t,f,1,7-f,6,7-t]
if t == 4:
if f == 1:
return [t,f,5,7-f,2,7-t]
if f == 5:
return [t,f,6,7-f,1,7-t]
if f == 6:
return [t,f,2,7-f,5,7-t]
if f == 2:
return [t,f,1,7-f,6,7-t]
if t == 5:
if f == 1:
return [t,f,3,7-f,4,7-t]
if f == 3:
return [t,f,6,7-f,1,7-t]
if f == 6:
return [t,f,4,7-f,3,7-t]
if f == 4:
return [t,f,1,7-f,6,7-t]
if t == 6:
if f == 2:
return [t,f,4,7-f,3,7-t]
if f == 4:
return [t,f,5,7-f,2,7-t]
if f == 5:
return [t,f,3,7-f,4,7-t]
if f == 3:
return [t,f,2,7-f,5,7-t]
dy = [0,-1,0,1,0,0]
dx = [0,0,1,0,-1,0]
def Set(s,dice):
z,y,x = fall(s,dice,99,99)
dir = check(s,z,y,x,dice)
while dir:
s[z][y][x] = 0
dice = rotate(dice,dir)
z,y,x = fall(s,dice,y+dy[dir],x+dx[dir])
dir = check(s,z,y,x,dice)
def fall(s,dice,y,x):
for z in range(100):
if s[z][y][x] == 0:
s[z][y][x] = dice[0]
return z,y,x
def check(s,z,y,x,dice):
if z == 0:
return 0
for c in (6,5,4):
dir = dice.index(c)
if s[z][y+dy[dir]][x+dx[dir]] == 0 and s[z-1][y+dy[dir]][x+dx[dir]] == 0 :
return dir
return 0
def rotate(dice,dir):
if dir == 1:
return [dice[3],dice[0],dice[2],dice[5],dice[4],dice[1]]
if dir == 2:
return [dice[4],dice[1],dice[0],dice[3],dice[5],dice[2]]
if dir == 3:
return [dice[1],dice[5],dice[2],dice[0],dice[4],dice[3]]
else:
return [dice[2],dice[1],dice[5],dice[3],dice[0],dice[4]]
while 1:
n = I()
if n == 0:
break
s = [[[0]*200 for i in range(200)] for j in range(100)]
for i in range(n):
t,f = LI()
dice = make_dice(t,f)
Set(s,dice)
d = {0:0,1:0,2:0,3:0,4:0,5:0,6:0}
for y in range(200):
for x in range(200):
for z in range(100):
if s[z][y][x] == 0:
break
d[s[z-1][y][x]] += 1
for i in range(1,6):
print(d[i],end = " ")
print(d[6])
| 0 | PYTHON3 |
n=int(input())
l=list(map(int,input().split()))
sum1=0
avg=0.0
for i in l:
sum1=sum1+i
avg=sum1/n
print(avg) | 8 | PYTHON3 |
s = input()
for i in 'HQ9':
if i in s:
print('YES')
quit(0)
print('NO')
| 7 | PYTHON3 |
I=lambda:list(map(int,input().split()));n,m=I();l=[I()[::-1]+[i]for i in range(m)];c=[0]*-~n;a=[0]*m
for y,p,i in sorted(l):c[p]+=1;a[i]='%06d'%p+'%06d'%c[p]
for t in a:print(t) | 0 | PYTHON3 |
def find(n, l, r, x, contests, lista):
if n == 0:
s = sum(lista)
if s >= l and s <= r and max(lista)-min(lista) >= x:
return 1
else:
return 0
c = 0
for i in range(len(contests)):
c += find(n-1, l, r, x, contests[i+1:], lista+[contests[i],])
return c
n, l, r, x = list(map(int, input().split()))
contests = list(map(int, input().split()))
c = 0
for i in range(2, n+1):
c += find(i, l, r, x, contests, [])
print(c) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
class point {
public:
int x, y;
} p[100010], q[100010];
int cmp1(const point &A, const point &B) {
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
int cmp2(const point &A, const point &B) {
return A.y < B.y || (A.y == B.y && A.x < B.x);
}
int n, col[100010];
long long solve() {
copy(p, p + n, q);
sort(q, q + n, cmp2);
long long ret = 0;
for (int i = 1; i < n; i++)
ret += 2ll * min(i, n - i) * (q[i].y - q[i - 1].y);
int mid = n >> 1;
if (n & 1) {
for (int i = 0; i < n; i++) {
if (q[i].x < p[mid].x)
col[i] = 0;
else if (q[i].x > p[mid].x)
col[i] = 1;
else
col[i] = -1;
}
int it;
int cur = -1;
for (it = 0; it < mid; it++) {
if (col[it] != -1) {
if (cur != -1 && cur != col[it]) break;
cur = col[it];
}
}
if (it < mid) goto cont;
++it;
for (; it < n; it++) {
if (col[it] != -1) {
if (cur != -1 && col[it] == cur) break;
cur = !col[it];
}
}
cont:
if (!(it == n || col[mid] != -1))
ret += 2ll * max(q[mid - 1].y - q[mid].y, q[mid].y - q[mid + 1].y);
return ret;
} else {
for (int i = 0; i < n; i++) {
if (q[i].x < p[mid].x)
col[i] = 0;
else if (q[i].x > p[mid - 1].x)
col[i] = 1;
else
col[i] = -1;
}
int cur = -1, it;
for (it = 0; it < mid; it++) {
if (col[it] != -1) {
if (cur != -1 && cur != col[it]) break;
cur = col[it];
}
}
if (it < mid) goto cont2;
for (; it < n; it++) {
if (col[it] != -1) {
if (cur != -1 && cur == col[it]) break;
cur = !col[it];
}
}
cont2:
if (it != n) ret += 2ll * (q[mid - 1].y - q[mid].y);
return ret;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y);
sort(p, p + n, cmp1);
long long ans = 0;
long long cur = 0;
for (int i = 1; i < n; i++)
cur += 2ll * min(i, n - i) * (p[i].x - p[i - 1].x);
ans = cur + solve();
for (int i = 0; i < n; i++) swap(p[i].x, p[i].y);
sort(p, p + n, cmp1);
cur = 0;
for (int i = 1; i < n; i++)
cur += 2ll * min(i, n - i) * (p[i].x - p[i - 1].x);
ans = max(ans, cur + solve());
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
"""
:Created by: Peter Thomas
:Created on: 23/03/17
"""
def createLanternArray(lanterns):
outputArray = []
lanternsList = lanterns.split(" ")
for item in lanternsList:
outputArray.append(int(item))
return outputArray
def selection_sort(the_array):
"""
:param the_array:
:return:
"""
for k in range(len(the_array) - 1):
minimum = k
for j in range(k, len(the_array)):
if the_array[j] < the_array[minimum]:
minimum = j
swap(the_array, minimum, k)
def find_distance(lanterns, length):
max = 0
for k in range(len(lanterns) - 1):
if lanterns[k + 1] - lanterns[k] > max:
max = lanterns[k + 1] - lanterns[k]
if lanterns[0] * 2 > max:
max = lanterns[0] * 2
if (length - lanterns[-1])*2 > max:
max = (length - lanterns[-1]) * 2
return max
def swap(the_array, a, b):
"""
:param the_array:
:param a:
:param b:
:return:
"""
temp = the_array[a]
the_array[a] = the_array[b]
the_array[b] = temp
if __name__ == "__main__":
inString1 = input()
numberOfLanterns = int(inString1.split(" ")[0])
length = int(inString1.split(" ")[1])
lanternString = input()
lanternArray = createLanternArray(lanternString)
selection_sort(lanternArray)
max_distance = find_distance(lanternArray, length)
print(max_distance/2) | 8 | PYTHON3 |
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
/**
* hash???????????????UnionFind ????´???°???16????????????????????¨??????????????????
*/
template<int N>
struct SUnionFind {
int i2g[N];
SUnionFind() {
for (int i = 0; i < N; i++) {
i2g[i] = i;
}
}
SUnionFind(ull hs) {
for (int i = 0; i < N; i++) {
i2g[i] = hs & 0xf;
hs >>= 4;
}
}
void merge(int x, int y) {
int xg = i2g[x], yg = i2g[y];
for (int i = 0; i < N; i++) {
if (i2g[i] == yg) i2g[i] = xg;
}
}
bool same(int x, int y) {
return i2g[x] == i2g[y];
}
ull hash() {
int b[N];
memset(b, -1, sizeof(b));
int c = 0;
ull hs = 0;
for (int i = N-1; i >= 0; i--) {
hs <<= 4;
if (b[i2g[i]] == -1) {
b[i2g[i]] = c;
c++;
}
hs += b[i2g[i]];
}
return hs;
}
};
typedef SUnionFind<14> UF;
typedef pair<ull, double> P;
struct MyCmp {
bool operator()(const P &l, const P &r) const {
return l.first < r.first;
}
};
int main() {
int n, m, pp;
cin >> n >> m >> pp;
double p = pp / 100.0;
int g[14]= {};
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b; a--; b--;
if (a > b) swap(a, b);
g[a] |= (1<<b);
}
vector<P> mp;
mp.push_back(P(UF().hash(), 1.0));
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (!(g[i] & (1<<j))) continue;
mp.reserve(mp.size()*2);
int S = (int)mp.size();
for (int k = 0; k < S; k++) {
ull d; double r;
tie(d, r) = mp[k];
UF uf(d);
uf.merge(i, j);
mp[k].second *= p;
mp.push_back(P(uf.hash(), r*(1-p)));
}
if (!mp.size()) continue;
sort(mp.begin(), mp.end(), MyCmp());
int l = 0;
for (int k = 1; k < (int)mp.size(); k++) {
if (mp[l].first != mp[k].first) {
l++;
mp[l] = mp[k];
} else {
mp[l].second += mp[k].second;
}
}
l++;
mp.resize(l);
}
int nn = 0;
int S = (int)mp.size();
for (int k = 0; k < S; k++) {
P p = mp[k];
UF uf(p.first);
bool f = false;
for (int j = i+1; j < n; j++) {
if (uf.same(i, j)) {
f = true;
break;
}
}
if (!f) continue;
UF uf2;
for (int j = i+1; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (uf.same(j, k)) uf2.merge(j, k);
}
}
mp[nn] = P(uf2.hash(), p.second);
nn++;
}
mp.resize(nn);
if (!mp.size()) continue;
sort(mp.begin(), mp.end(), MyCmp());
int l = 0;
for (int k = 1; k < (int)mp.size(); k++) {
if (mp[l].first != mp[k].first) {
l++;
mp[l] = mp[k];
} else {
mp[l].second += mp[k].second;
}
}
l++;
mp.resize(l);
}
double res = 0;
for (auto p : mp) {
res += p.second;
}
printf("%.20lf\n", res);
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> woo(n);
for (int i = 0; i < n; i++) cin >> woo[i];
vector<int> rooms(n, 0);
bool no = 0;
for (int i = 0; i < n; i++) {
int num = (woo[i] + i) % n;
if (num < 0) num += n;
if (rooms[num] > 0) {
no = 1;
break;
}
rooms[num]++;
}
if (no)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
| 7 | CPP |
#include<bits/stdc++.h>
using namespace std;
#define N 100011
typedef struct{
int to,nxt;
}edge;
edge e[N<<1];
queue<int> q;
int cnt,head[N<<1],dep[N],fa[N],siz[N],vis[N],ans,a,b,n,st,ed,sta[N],top;
int p1[N],p2[N],np[N],val[N];
void add(int a,int b){
cnt++;
e[cnt].to=b;
e[cnt].nxt=head[a];
head[a]=cnt;
}
void bfs(int s){
dep[s]=1;
q.push(s);
while(!q.empty()){
int o=q.front();q.pop();
for(int i=head[o];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(!dep[v]){
dep[v]=dep[o]+1;
q.push(v);
}
}
}
}
void dfs(int u,int f){
fa[u]=f;
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(v==f) continue;
dfs(v,u);
}
}
int main(){
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i=1;i<=n-1;i++){
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
bfs(1);int mx=0;
for(int i=1;i<=n;i++)
if(mx<dep[i]){
mx=dep[i];
st=i;
}
memset(dep,0,sizeof(dep));
bfs(st);mx=0;
for(int i=1;i<=n;i++)
if(mx<dep[i]){
mx=dep[i];ed=i;
}
dfs(st,0);
int x=ed;
while(x){
vis[x]=2;
for(int i=head[x];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(v==fa[x]) continue;
if(!vis[v]) vis[v]=1;
}
x=fa[x];
}
for(int i=1;i<=n;i++)
if(!vis[i]){
printf("-1\n");
return 0;
}
for(int i=1;i<=n;i++){
if(vis[i]==1){
val[fa[i]]++;
}
}
x=ed;
while(x){
sta[++top]=val[x]+1;
x=fa[x];
}
int ccnt=0;
for(int i=1;i<=top;i++){
np[ccnt+1]=ccnt+sta[i];
for(int j=1;j<=sta[i]-1;j++)
np[ccnt+1+j]=ccnt+j;
ccnt+=sta[i];
}
for(int i=1;i<=n;i++) p1[np[i]]=i;
ccnt=0;
for(int i=top;i>=1;i--){
np[ccnt+1]=ccnt+sta[i];
for(int j=1;j<=sta[i]-1;j++)
np[ccnt+1+j]=ccnt+j;
ccnt+=sta[i];
}
for(int i=1;i<=n;i++) p2[np[i]]=i;
for(int i=1;i<=n;i++)
if(p1[i]>p2[i]){
for(int j=1;j<=n;j++)
printf("%d ",p2[j]);
printf("\n");
return 0;
}else
if(p1[i]<p2[i]){
for(int j=1;j<=n;j++)
printf("%d ",p1[j]);
printf("\n");
return 0;
}
for(int i=1;i<=n;i++)
printf("%d ",p1[i]);
printf("\n");
} | 0 | CPP |
import sys
input=sys.stdin.buffer.readline
mod=10**9+7
n,m=map(int,input().split())
*x,=map(int,input().split())
*y,=map(int,input().split())
xd=[i-j for j,i in zip(x,x[1:])]
yd=[i-j for j,i in zip(y,y[1:])]
xs=ys=0
for i,d in enumerate(xd):
xs+=d*(i+1)*(n-1-i)%mod
xs%=mod
for i,d in enumerate(yd):
ys+=d*(i+1)*(m-1-i)%mod
ys%=mod
print(xs*ys%mod) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n;
cin >> t;
while (t--) {
long long int a[51], b[51], ma = INT_MAX, mb = INT_MAX, sum = 0;
bool flag = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
ma = min(ma, a[i]);
}
for (int i = 0; i < n; i++) {
cin >> b[i];
mb = min(mb, b[i]);
}
for (int i = 0; i < n; i++) {
a[i] = a[i] - ma;
b[i] = b[i] - mb;
sum += max(a[i], b[i]);
}
cout << sum << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int I = 0x3f3f3f3f;
int main() {
int a, b, c, d, x, y, n;
a = b = c = d = -I;
cin >> n;
while (n--) {
cin >> x >> y;
a = max(a, x + y);
b = max(b, x - y);
c = max(c, y - x);
d = max(d, -x - y);
}
cout << a + b + c + d + 4;
return 0;
}
| 9 | CPP |
'''
Auther: ghoshashis545 Ashis Ghosh
College: Jalpaiguri Govt Enggineering College
'''
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('0')
file = 1
def ceil(a,b):
return (a+b-1)//b
def solve():
# for n in range(1,ii()+1):
n = ii()
p = 1
x = []
for i in range(1,n):
if gcd(i,n)==1:
x.append(i)
p *= i
p %= n
if p!=1:
x.remove(p)
print(len(x))
print(*x)
if __name__ =="__main__":
if(file):
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
| 9 | PYTHON3 |
n,m = map(int,input().split())
a = sorted([int(s) for s in input().split()])
b = [int(s) for s in input().split()]
output = []
for i in range(len(b)):
lo = 0
hi = n - 1
ans = 0
while (lo <= hi):
MiddleNum = lo + ((hi - lo)//2)
if a[MiddleNum] <= b[i]:
lo = MiddleNum +1
ans = MiddleNum + 1
else:
hi = MiddleNum - 1
output.append(ans)
print(" ".join(str(i) for i in output))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using std::abs;
using std::acos;
using std::cin;
using std::cout;
using std::endl;
using std::ios;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::reverse;
using std::runtime_error;
using std::set;
using std::sort;
using std::string;
using std::swap;
using std::vector;
const int maxn = 2e5 + 5;
const int minn = 2005;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long double pi = acos(-1.);
void solve() {
int n;
cin >> n;
vector<pair<int, int>> a(n);
vector<int> answ(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
int q = 0;
int g = 0;
for (int i = 0; i < n; i++) {
if (a[i].first >= 0) {
a[i].first++;
a[i].first *= -1;
q++;
} else if (a[i].first < 0)
q++;
}
sort(a.begin(), a.end());
if (n % 2 == 1) {
int gg = -1;
int p = 0;
int ma = -1000001;
int k = 0;
for (int i = 0; i < n; i++) {
if (a[i].first < -1) {
gg = 1;
a[i].first = abs(a[i].first) - 1;
break;
}
if (a[i].first == -1) p = i;
}
if (gg == -1) {
a[p].first = 0;
}
}
for (int i = 0; i < n; i++) {
answ[a[i].second] = a[i].first;
}
for (auto x : answ) {
cout << x << " ";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
cin >> t;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
const long long int MAXN = 2e5 + 5;
const long long int mod = 998244353;
const long long int N = 200005;
void solve() {
int n;
cin >> n;
vector<long long int> x1(n + 1), y1(n + 1), x2(n + 1), y2(n + 1);
vector<long long int> prex1(n + 2), prex2(n + 2), prey1(n + 2), prey2(n + 2),
suffx1(n + 2), suffx2(n + 2), suffy1(n + 2), suffy2(n + 2);
for (int i = 1; i <= n; i++) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
}
prex1[0] = prey1[0] = suffx1[n + 1] = suffy1[n + 1] = -1e10;
prex2[0] = prey2[0] = suffx2[n + 1] = suffy2[n + 1] = 1e10;
vector<pair<long long int, long long int>> vec1(n), vec2(n);
for (int i = 1; i <= n; i++) {
prex1[i] = max(prex1[i - 1], x1[i]);
prey1[i] = max(prey1[i - 1], y1[i]);
prex2[i] = min(prex2[i - 1], x2[i]);
prey2[i] = min(prey2[i - 1], y2[i]);
}
for (int i = n; i >= 1; i--) {
suffx1[i] = max(suffx1[i + 1], x1[i]);
suffy1[i] = max(suffy1[i + 1], y1[i]);
suffx2[i] = min(suffx2[i + 1], x2[i]);
suffy2[i] = min(suffy2[i + 1], y2[i]);
}
for (int i = 1; i <= n; i++) {
long long int x3 = max(prex1[i - 1], suffx1[i + 1]);
long long int x4 = min(prex2[i - 1], suffx2[i + 1]);
long long int y3 = max(prey1[i - 1], suffy1[i + 1]);
long long int y4 = min(prey2[i - 1], suffy2[i + 1]);
if (x3 > x4 || y3 > y4) {
continue;
} else {
cout << x3 << " " << y3 << "\n";
return;
}
}
return;
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
const int nax = 200 * 1000 + 10;
const ll INF = 1e18;
int t, n, m;
pi seg[nax];
ll pt[nax];
vi ind;
vi occ[nax];
ll dp[nax][2];
void test_case() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> pt[i];
}
for (int i = 1; i <= m; ++i) {
cin >> seg[i].first >> seg[i].second;
}
for (int i = 0; i <= n; ++i) {
occ[i].clear();
}
ind.clear();
sort(pt + 1, pt + n + 1);
sort(seg + 1, seg + m + 1);
for (int i = 1; i <= m; ++i) {
while ((int)ind.size() > 0 && seg[ind.back()].second >= seg[i].second) {
ind.pop_back();
}
ind.push_back(i);
}
int ptr = 0;
m = (int)ind.size();
for (int i = 1; i <= n; ++i) {
while (ptr < m && seg[ind[ptr]].second < pt[i]) {
occ[i - 1].push_back(ind[ptr]);
ptr++;
}
while (ptr < m && seg[ind[ptr]].first <= pt[i] &&
pt[i] <= seg[ind[ptr]].second) {
ptr++;
}
}
for (int i = ptr; i < m; ++i) {
occ[n].push_back(ind[i]);
}
pt[0] = -INF;
pt[n + 1] = INF;
for (int i = 1; i <= n + 1; ++i) {
dp[i][0] = dp[i - 1][0] + ((int)occ[i - 1].size() > 0
? abs(pt[i] - seg[occ[i - 1][0]].second)
: 0);
dp[i][1] = dp[i - 1][0] + 2 * ((int)occ[i - 1].size() > 0
? abs(pt[i] - seg[occ[i - 1][0]].second)
: 0);
int nxt = 0;
for (auto id : occ[i - 1]) {
ll tmp = min(dp[i - 1][1] + seg[id].first - pt[i - 1],
dp[i - 1][0] + 2 * (seg[id].first - pt[i - 1]));
ll d = (nxt == (int)occ[i - 1].size() - 1
? 0
: pt[i] - seg[occ[i - 1][nxt + 1]].second);
dp[i][0] = min(dp[i][0], tmp + d);
dp[i][1] = min(dp[i][1], tmp + 2 * d);
nxt++;
}
}
cout << min(dp[n + 1][0], dp[n + 1][1]) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
test_case();
}
}
| 12 | CPP |
n = int(input())
A = list(map(int,input().split()))
ans = []
for i in range(n)[::-1]:
for j in range(i+1)[::-1]:
if j+1 == A[j]:
ans.append(A.pop(j))
break
if len(ans)==n:
for a in ans[::-1]:
print(a)
else:
print(-1) | 0 | PYTHON3 |
n,m=map(int,input().split())
a={int(input()) for i in range(m)}
mod=10**9+7
x=1 if 1 not in a else 0
dp=[1,x]+[0]*(n-1)
for i in range(2,n+1):
if i in a:
continue
dp[i]=(dp[i-1]+dp[i-2])
print(dp[-1]%mod)
| 0 | PYTHON3 |
#include <iostream>
using namespace std;
int B[8][8]={{1,2,1,2,1,4,1,4},{1,2,1,4,1,4,1,2},{1,4,1,2,1,2,1,4},{1,4,1,4,1,2,1,2},{2,1,2,1,4,1,4,1},{2,1,4,1,4,1,2,1},{4,1,2,1,2,1,4,1},{4,1,4,1,2,1,2,1}};
int d[8];
int cal(int num){
int TMP=0;
for(int i=0;i<8;i++){
if(d[i]>B[num][i]){
TMP+=(d[i]-B[num][i]);
}
}
return TMP;
}
int main(){
int tmp,f;
while(cin>>d[0]){
for(int i=1;i<8;i++)cin>>d[i];
f=1;
tmp=cal(0);
for(int i=1;i<8;i++)if(tmp>cal(i))tmp=cal(i);
for(int i=0;f;i++){
if(tmp==cal(i)){
for(int j=0;j<8;j++){
if(j<7)cout<<B[i][j]<<" ";
else cout<<B[i][j]<<endl;
}
f=0;
}
}
}
return 0;
}
| 0 | CPP |
n,k=map(int,input().split())
s=n//2
if(n%2!=0):
s=s+1
if(k>s):
ele=2*(k-s)
else:
ele=2*k-1
print(ele) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, b, mod;
int v[501][501];
int main() {
cin >> n >> m >> b >> mod;
v[0][0] = 1;
for (int it = 1; it <= n; it++) {
int a;
cin >> a;
for (int j = 0; j < m; j++) {
for (int bu = 0; bu <= b - a; bu++) {
v[j + 1][bu + a] += v[j][bu];
v[j + 1][bu + a] %= mod;
}
}
}
int ans = 0;
for (int i = 0; i <= b; i++) {
ans += v[m][i];
ans %= mod;
}
cout << ans << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int32_t n;
std::cin >> n;
std::set<std::pair<int32_t, int32_t> > segments;
for (int32_t i = 0; i < n; i++) {
int32_t left, right;
std::cin >> left >> right;
auto it = segments.lower_bound({left, 0});
if (it != segments.begin()) {
it--;
if (it->second < left) it++;
}
int32_t min_left = left, max_right = right;
while (it != segments.end() &&
((it->second >= left && it->second <= right) ||
(it->first >= left && it->first <= right) ||
(it->first <= left && it->second >= right))) {
min_left = std::min(min_left, it->first);
max_right = std::max(max_right, it->second);
auto next = it;
next++;
segments.erase(it);
it = next;
}
segments.emplace(min_left, max_right);
std::cout << segments.size() << " ";
}
return 0;
}
| 16 | CPP |
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int x;
int main(){
cin>>x;
int i=x/100,j=x%100;
if(i*5<j)cout<<0<<endl;
else cout<<1<<endl;
} | 0 | CPP |
#include<iostream>
using namespace std;
int main(){
int n,i,a;cin >> n;
int s[n];
for(i=1;i<=n;i++){
cin >> a;
s[a-1] = i;
}
for(i=0;i<n;i++) cout << s[i] << " ";
cout << endl;
} | 0 | CPP |
N = int(input())
V = map(int, input().split())
C = map(int, input().split())
D = [v - c for v, c in zip(V, C) if v - c > 0]
print(sum(D)) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline int msbp(int x) { return 31 - __builtin_clz(x); }
inline int msb(int x) { return 1 << msbp(x); }
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
int n, s, ans, a[N], l[N], hi;
signed main() {
ios::sync_with_stdio(false);
cin >> n >> s;
for (int i = (1); i <= (n); ++i) {
cin >> a[i];
if (i == s && a[i]) {
++ans;
a[i] = 0;
}
hi = max(hi, a[i]);
l[a[i]]++;
}
ans += l[0] - 1;
int liars = l[0] - 1;
for (int i = (1); i <= (hi); ++i) {
if (!l[i]) {
++l[i];
if (liars)
--liars;
else {
++ans;
--l[hi];
while (l[hi] == 0) --hi;
}
}
}
cout << ans << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long max(long long i, long long j) { return i > j ? i : j; }
long long min(long long i, long long j) { return i < j ? i : j; }
int hcf(int a, int b) {
if (b == 0) return a;
return hcf(b, a % b);
}
int ncr(int n, int r) {
if (r == 0) return 1;
if (n == r) return 1;
if (r == 1) return n;
return ncr(n - 1, r) + ncr(n - 1, r - 1);
}
bool isPrime(long long n) {
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
bool sortValD(pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
}
vector<int> sieve(int n) {
vector<bool> prime(n + 1, true);
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
vector<int> v;
for (int i = 2; i < prime.size() + 2; i++) {
if (prime[i]) v.push_back(i);
}
return v;
}
int main() {
long long n, h, l, r;
cin >> n >> h >> l >> r;
vector<long long> a(n, 0);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<long long>> dp(n + 1, vector<long long>(h, 0));
for (long long i = n - 1; i >= 0; i--) {
for (long long j = 0; j < h; j++) {
long long x = (j + a[i]) % h;
long long y = (j + a[i] - 1) % h;
int A = 0, B = 0;
if (x >= l && x <= r) A = 1;
if (y >= l && y <= r) B = 1;
dp[i][j] = max(dp[i + 1][x] + A, dp[i + 1][y] + B);
}
}
cout << dp[0][0] << endl;
}
| 11 | CPP |
n = int(input())
s = input()
months1 = "31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31"
months2 = "31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31"
months3 = "31 28 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31"
months4 = "31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31"
if s in months1 or s in months2 or s in months3 or s in months4:
print("Yes")
else:
print("No") | 8 | PYTHON3 |
#include<iostream>
using namespace std;
int main()
{
long long n;
while(cin>>n && n!=0){
int ans=1;
long long count=3;
while(count<n){
ans++;
count*=3;
}
cout<<ans<<endl;
}
return 0;
} | 0 | CPP |
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <queue>
using namespace std;
class Node {
public:
vector<pair<int, int> > tos;
int c;
bool d;
friend bool operator< (const Node &n1, const Node &n2) {
return n1.c > n2.c;
}
};
int n;
vector<Node> ns;
int dijkstra(int s, int t) {
for (int i = 0; i < n; i++) {
ns[i].c = -1;
ns[i].d = false;
}
priority_queue<Node> q;
ns[s].c = 0;
q.push(ns[s]);
while (!q.empty()) {
Node node = q.top(); q.pop();
if (node.d) {
continue;
}
node.d = true;
for (vector<pair<int, int> >::iterator it = node.tos.begin(); it != node.tos.end(); it++) {
int to = it->first;
int c = node.c + it->second;
if (ns[to].c < 0 || c < ns[to].c) {
ns[to].c = c;
q.push(ns[to]);
}
}
}
return ns[t].c;
}
int main() {
int m;
while (cin >> n >> m, n|m) {
string s, p, g;
cin >> s >> p >> g;
map<string, int> st;
st[s] = 0;
st[p] = 1;
st[g] = 2;
ns.clear();
ns.resize(n);
for (int i = 0, idx = 3; i < m; i++) {
string a, b;
int d, t;
cin >> a >> b >> d >> t;
if (st.find(a) == st.end()) {
st[a] = idx++;
}
if (st.find(b) == st.end()) {
st[b] = idx++;
}
int a_i = st[a];
int b_i = st[b];
int c = d / 40 + t;
ns[a_i].tos.push_back(make_pair(b_i, c));
ns[b_i].tos.push_back(make_pair(a_i, c));
}
cout << dijkstra(st[s], st[p]) + dijkstra(st[p], st[g]) << endl;
}
} | 0 | CPP |
#include<iostream>
#include<vector>
using namespace std;
vector<int> Name;
typedef struct{int number; int point;} SCORE;
vector <int> toV(string in){
vector<int> out;
for(int i = 0; i < in.length(); i++){
int num = 0;
while(in[i] != ' '){
num *= 10;
num += in[i]-'0';
i++;
}
out.push_back(num);
}
return out;
}
int score(vector<int> V){
int j= 0;
int sum = 0;
int po;
for(int i = 1; i < V.size(); i++){
j++;
if(j == 10){
po = i;
break;
}
if(V[i] == 10) sum += 10+V[i+1]+V[i+2];
else if(V[i] + V[i+1] == 10){
i++;
sum += 10+V[i+1];
}else{
sum += V[i] + V[i+1];
i++;
}
}
if(V[po] + V[po+1] >= 10) sum += V[po] + V[po+1] + V[po+2];
else sum += V[po]+V[po+1];
return sum;
}
int main(){
int in;
while(cin >> in && in){
SCORE SC[in];
Name.clear();
string t;
getline(cin,t);
vector<int> V;
for(int i = 0; i < in; i++){
string s;
getline(cin,s);
s += ' ';
V = toV(s);
Name.push_back(V[0]);
SC[i].number = V[0];
SC[i].point = score(V);
}
for(int i = 0; i < in; i++){
for(int j = 0; j < in-i-1; j++){
if(SC[j].point < SC[j+1].point) swap(SC[j],SC[j+1]);
else if(SC[j].point == SC[j+1].point &&
SC[j].number > SC[j+1].number) swap(SC[j],SC[j+1]);
}
}
for(int i = 0; i < in; i++) cout << SC[i].number << " " << SC[i].point<< endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double EPS = 1e-7;
int INF = 1000000000;
long long INFLL = 1000000000000000000LL;
int mx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int my[8] = {0, 0, -1, 1, -1, 1, -1, 1};
long long f(long long n) {
if (n == 0) return 0;
long long ans = n;
for (long long x = 2; x * x <= n; ++x) {
if (n % x == 0) {
ans -= ans / x;
while (n % x == 0) n /= x;
}
}
if (n > 1) ans -= ans / n;
return ans;
}
int main() {
long long n, k;
cin >> n >> k;
k = (k - 1) / 2 + 1;
while (k-- && n > 1) {
n = f(n);
}
cout << n % 1000000007 << endl;
}
| 11 | CPP |
#include<iostream>
int main(){int a;std::cin>>a;std::cout<<!a;} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
if (N == 1) {
cout << "! 1" << endl;
return 0;
}
vector<int> res(N);
for (;;) {
int x = 0, y = 0;
for (int i = 0; i < N; ++i) {
if (x == 0 && res[i] == 0) {
x = i + 1;
} else if (res[i] == 0) {
y = i + 1;
break;
}
}
if (y == 0) break;
int z, w;
cout << "? " << x << " " << y << endl;
cin >> z;
cout << "? " << y << " " << x << endl;
cin >> w;
if (z == 0) {
res[y - 1] = w;
} else if (w == 0) {
res[x - 1] = z;
} else if (z > w) {
res[x - 1] = z;
} else {
res[y - 1] = w;
}
}
set<int> a;
for (int i = 1; i <= N; ++i) a.insert(i);
for (auto x : res) a.erase(x);
for (auto& x : res)
if (x == 0) x = *a.begin();
cout << "!";
for (auto x : res) cout << " " << x;
cout << endl;
}
| 9 | CPP |
infinity = 99999999999
minR1 = minR2 = infinity
maxL1 = maxL2 = -infinity
n = int(input())
for i in range(0, n):
(l, r) = map(int, input().split())
maxL1 = max(maxL1, l)
minR1 = min(minR1, r)
m = int(input())
for i in range(0, m):
(l, r) = map(int, input().split())
maxL2 = max(maxL2, l)
minR2 = min(minR2, r)
res = max(maxL2 - minR1, maxL1 - minR2);
print(max(res, 0)) | 8 | PYTHON3 |
n=int(input())
o=[0]*n
for i in range(n):
a,b=map(int,input().split())
o[i]=b-a%b
if(o[i]==b):
o[i]=0
for i in o:
print(i) | 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100003;
const long long inf = 1000000000000000LL;
int n;
long long t, ans;
long long k1, k2, k3, t1, t2, t3;
long long ay[maxn];
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
greater<pair<long long, int> > >
heap1, heap2, heap3;
queue<int> q1, q2, q3;
inline int get() {
char ch = getchar();
while (!isdigit(ch) && ch != -1) ch = getchar();
int v = ch - 48;
ch = getchar();
while (isdigit(ch)) {
v = v * 10 + ch - 48;
ch = getchar();
}
return v;
}
void work() {
int i = 1, j;
long long tmp;
t = 0;
ans = 0;
while (i <= n || heap1.size() > 0 || heap2.size() > 0 || heap3.size() > 0) {
while (i <= n && ay[i] == t) {
if (heap1.size() < k1)
heap1.push(pair<long long, int>(ay[i] + t1, i));
else
q1.push(i);
i++;
}
while (!heap1.empty() && heap1.top().first == t) {
if (heap2.size() < k2)
heap2.push(
pair<long long, int>(heap1.top().first + t2, heap1.top().second));
else
q2.push(heap1.top().second);
heap1.pop();
if (q1.size() > 0) {
heap1.push(pair<long long, int>(t + t1, q1.front()));
q1.pop();
}
}
while (!heap2.empty() && heap2.top().first == t) {
if (heap3.size() < k3)
heap3.push(
pair<long long, int>(heap2.top().first + t3, heap2.top().second));
else
q3.push(heap2.top().second);
heap2.pop();
if (q2.size() > 0) {
heap2.push(pair<long long, int>(t + t2, q2.front()));
q2.pop();
}
}
while (!heap3.empty() && heap3.top().first == t) {
ans = ((ans > heap3.top().first - ay[heap3.top().second])
? ans
: heap3.top().first - ay[heap3.top().second]);
heap3.pop();
if (q3.size() > 0) {
heap3.push(pair<long long, int>(t + t3, q3.front()));
q3.pop();
}
}
tmp = inf;
if (i <= n) tmp = ((tmp < ay[i]) ? tmp : ay[i]);
if (!heap1.empty())
tmp = ((tmp < heap1.top().first) ? tmp : heap1.top().first);
if (!heap2.empty())
tmp = ((tmp < heap2.top().first) ? tmp : heap2.top().first);
if (!heap3.empty())
tmp = ((tmp < heap3.top().first) ? tmp : heap3.top().first);
if (tmp != inf) t = tmp;
}
}
int main() {
cin >> k1 >> k2 >> k3 >> t1 >> t2 >> t3 >> n;
int i;
for (i = 1; i <= n; i++) {
ay[i] = get();
}
work();
cout << ans << endl;
return 0;
}
| 8 | CPP |
number_of_magnet=int(input())
list_of_magnet=[]
for i in range(number_of_magnet):
magnet=input()
list_of_magnet.append(magnet)
gp=1
for i in range(len(list_of_magnet)-1):
if not list_of_magnet[i+1]==list_of_magnet[i]:
gp+=1
print(gp)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, r2, c1, c2, d1, d2;
cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2;
int a, b, c, d;
a = (d1 + c1 - r2) / 2;
b = r1 - a;
c = c1 - a;
d = c2 - b;
if (a != b && a != c && a != d && b != c && b != d && c != d && a <= 9 &&
b <= 9 && c <= 9 && d <= 9 && a + d == d1 && b + c == d2 && a && b && c &&
d) {
cout << a << " " << b << endl;
cout << c << " " << d << endl;
} else
cout << -1;
return 0;
}
| 7 | CPP |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<int> result;
int used[2 * 100000 + 10];
int este[100000 + 10];
vector<vector<int>> edges(2 * 100000 + 10);
void dfs(int node) {
used[node] = 1;
result.push_back(node);
for(int son : edges[node]) {
if(!used[son]) {
dfs(son);
}
}
}
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++) {
int k;
cin >> k;
for(int j = 0; j < k; j++) {
int x;
cin >> x;
edges[x].push_back(m + i + 1);
edges[m + i + 1].push_back(x);
}
}
dfs(m + 1);
for(int i = 0; i < result.size(); i++) {
if(result[i] > m) {
este[result[i] - m] = 1;
}
}
bool ok = true;
for(int i = 1; i <= n; i++) {
if(este[i] == 0) {
ok = false;
}
}
ok ? cout << "YES" : cout << "NO";
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
int seg[1048580], mx[1048580];
void merge(int i) {
seg[i] = seg[i * 2] + seg[i * 2 + 1];
mx[i] = max(mx[i * 2 + 1], mx[i * 2] + seg[i * 2 + 1]);
}
void upd(int k, int v) {
k += 524287;
if (v == 1) {
if (++seg[k] == 1) {
mx[k] = k - 524287;
} else {
++mx[k];
}
} else {
if (--seg[k] == 0) {
mx[k] = 0;
} else {
--mx[k];
}
}
for (k >>= 1; k; k >>= 1) merge(k);
}
set<pair<int, int> > st;
int main() {
int m, k;
scanf("%d %d %d", &n, &m, &k);
for (int i = (1); i <= (k); i++) {
int x, y;
scanf("%d %d", &x, &y);
if (st.find({x, y}) == st.end()) {
upd(y + abs(x - m), 1);
st.insert({x, y});
} else {
upd(y + abs(x - m), -1);
st.erase({x, y});
}
printf("%d\n", max(0, mx[1] - n));
}
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> E[1 << 19];
void solve(int test) {
int n, m;
cin >> n >> m;
int i;
for (i = (0); i < (m); ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
E[a].push_back(b);
E[b].push_back(a);
}
vector<pair<int, int> > v(n);
for (i = (0); i < (n); ++i) {
cin >> v[i].first;
v[i].second = i;
}
sort((v).begin(), (v).end());
vector<int> next(n, 1);
vector<int> res(n, -1);
for (i = (0); i < (n); ++i) {
int a = v[i].second;
if (next[a] != v[i].first) {
cout << (-1) << endl;
return;
}
res[a] = next[a];
for (int b : E[a])
if (res[b] == -1 && next[b] == next[a]) ++next[b];
}
for (i = (0); i < (n); ++i) {
int a = v[i].second;
cout << (a + 1) << ' ';
}
}
int main() {
int T = 1, t;
for (t = (0); t < (T); ++t) solve(t);
return 0;
}
| 7 | CPP |
l=sorted(list(map(int,input().split())))
print(l[2]-l[0]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 125010;
const double pi = acos(-1);
char s[N], t[N];
int rev[N << 2], num[6][6][N << 2], fa[8];
struct cp {
double real, imag;
cp(double x = 0, double y = 0) {
real = x;
imag = y;
}
cp operator+(const cp &t) const { return cp(real + t.real, imag + t.imag); }
cp operator-(const cp &t) const { return cp(real - t.real, imag - t.imag); }
cp operator*(const cp &t) const {
return cp(real * t.real - imag * t.imag, real * t.imag + imag * t.real);
}
} A[N << 2], B[N << 2];
void swp(cp &u, cp &v) {
cp t = u;
u = v;
v = t;
}
void FFT(cp *pn, int len, int f) {
for (int i = 0; i < len; i++)
if (i < rev[i]) swp(pn[i], pn[rev[i]]);
for (int i = 1; i < len; i <<= 1) {
cp wn(cos(pi / i), f * sin(pi / i));
for (int j = 0, d = (i << 1); j < len; j += d) {
cp w(1, 0);
for (int k = 0; k < i; k++) {
cp u = pn[j + k], v = w * pn[j + k + i];
pn[j + k] = u + v, pn[j + k + i] = u - v;
w = w * wn;
}
}
}
if (f == -1) {
for (int i = 0; i < len; i++) pn[i].real /= len;
}
}
int Find(int x) {
if (x != fa[x])
return fa[x] = Find(fa[x]);
else
return x;
}
int main() {
scanf("%s%s", s, t);
int n = strlen(s), m = strlen(t);
reverse(t, t + m);
int len = 1, cnt = 0, maxn = m + n;
while (len < maxn) {
len <<= 1;
cnt++;
}
for (int i = 0; i < len; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1));
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (i == j) continue;
for (int k = 0; k < len; k++) A[k] = cp(0, 0), B[k] = cp(0, 0);
for (int k = 0; k < n; k++) {
if (s[k] - 'a' == i) A[k] = cp(1, 0);
}
for (int k = 0; k < m; k++) {
if (t[k] - 'a' == j) B[k] = cp(1, 0);
}
FFT(A, len, 1);
FFT(B, len, 1);
for (int k = 0; k < len; k++) A[k] = A[k] * B[k];
FFT(A, len, -1);
for (int k = 0; k < len; k++) num[i][j][k] = (int)(A[k].real + 0.5);
}
}
for (int i = m - 1; i < n; i++) {
int ans = 0;
for (int j = 0; j < 6; j++) fa[j] = j;
for (int j = 0; j < 6; j++) {
for (int k = 0; k < 6; k++) {
if (j == k) continue;
if (num[j][k][i] > 0) {
int p1 = Find(j), p2 = Find(k);
if (p1 != p2) fa[p1] = p2;
}
}
}
for (int j = 0; j < 6; j++) {
if (Find(j) != j) ans++;
}
printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
}
return 0;
}
| 15 | CPP |
n = int(input())
cnt = 0
for i in range(n):
x = list(map(int,input().split()))
if x.count(1) > 1:
cnt += 1
print(cnt) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 4000;
long long n, m, k, ans;
char a[maxN][maxN];
long long check1(long long r) {
long long f = 0;
long long ret = 0;
for (long long i = 0; i < m; i++) {
if (a[r][i] == '.')
f++;
else if (a[r][i] == '*') {
if (f >= k) {
ret += f - k + 1;
}
f = 0;
}
}
if (f >= k) {
ret += f - k + 1;
f = 0;
}
return ret;
}
long long check2(long long c) {
long long f = 0;
long long ret = 0;
for (long long i = 0; i < n; i++) {
if (a[i][c] == '.')
f++;
else if (a[i][c] == '*') {
if (f >= k) {
ret += f - k + 1;
}
f = 0;
}
}
if (f >= k) {
ret += f - k + 1;
f = 0;
}
return ret;
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
long long ch = 0;
for (long long i = 0; i < n; i++) {
string s;
cin >> s;
for (long long j = 0; j < m; j++) {
a[i][j] = s[j];
if (s[j] == '.') ch++;
}
}
if (k == 1) {
cout << ch;
return 0;
}
for (long long i = 0; i < n; i++) {
ans += check1(i);
}
for (long long i = 0; i < m; i++) {
ans += check2(i);
}
cout << ans << endl;
return 0;
}
| 9 | CPP |
#### import ####
import sys
import math
from collections import defaultdict
#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
return sys.stdin.readline()[:-1]
#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
read_all = [tuple(map(int, input().split())) for _ in range(N)]
return map(list,zip(*read_all))
#################
import heapq
N = I()
A = III()
B = III()
x = []
for i,b in enumerate(B):
heapq.heappush(x,(-b,i))
counter = 0
while x:
temp = heapq.heappop(x)
i = temp[1]
b = -temp[0]
a = B[(i-1)%N]
c = B[(i+1)%N]
if b == A[i]:
continue
elif b<A[i]:
print(-1)
exit()
else:
d,m = divmod(b-A[i],a+c)
if d==0:
print(-1)
exit()
elif m == 0:
B[i] = A[i]
counter += d
else:
B[i] = A[i]+m
heapq.heappush(x,(-B[i],i))
counter += d
print(counter) | 0 | PYTHON3 |
N=int(input())
A,B=map(int,input().split())
*P,=map(int,input().split())
X=Y=Z=0
for p in P:
if p<=A:
X+=1
elif B<p:
Z+=1
else:
Y+=1
print(min([X,Y,Z])) | 0 | PYTHON3 |
#include <bits/stdc++.h>
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 << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
const int N = 2002;
double f[N], p[N], q[N];
int numa[N], numb[N];
int n, a, b;
inline void calc(double ca, double cb) {
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1];
numa[i] = numa[i - 1];
numb[i] = numb[i - 1];
if (f[i - 1] + p[i] - ca > f[i])
f[i] = f[i - 1] + p[i] - ca, numa[i] = numa[i - 1] + 1,
numb[i] = numb[i - 1];
if (f[i - 1] + q[i] - cb > f[i])
f[i] = f[i - 1] + q[i] - cb, numa[i] = numa[i - 1],
numb[i] = numb[i - 1] + 1;
if (f[i - 1] + p[i] + q[i] - p[i] * q[i] - ca - cb > f[i])
f[i] = f[i - 1] + p[i] + q[i] - p[i] * q[i] - ca - cb,
numa[i] = numa[i - 1] + 1, numb[i] = numb[i - 1] + 1;
}
}
inline double check(double ca) {
double l = 0, r = 1;
int cnt = 50;
while (cnt-- > 0) {
double mid = (l + r) / 2.0;
calc(ca, mid);
if (numb[n] > b)
l = mid;
else
r = mid;
}
calc(ca, r);
return r;
}
inline void solve() {
n = read();
a = read();
b = read();
for (int i = 1; i <= n; i++) scanf("%lf", &p[i]);
for (int i = 1; i <= n; i++) scanf("%lf", &q[i]);
double l = 0, r = 1, cb;
int cnt = 50;
while (cnt-- > 0) {
double mid = (l + r) / 2.0;
cb = check(mid);
if (numa[n] > a)
l = mid;
else
r = mid;
}
calc(r, cb);
printf("%.5f\n", f[n] + r * a + cb * b);
}
int main() {
int T = 1;
while (T-- > 0) solve();
fclose(stdin);
fclose(stdout);
return 0;
}
| 11 | CPP |
#!/usr/bin/env python
n = int(input()) - 2
print(n * (n**2 + 6 * n + 11) // 3)
| 10 | PYTHON3 |
#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define pii pair<int,int>
#define fi first
#define se second
#define PB push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll,ll>
#define y1 orzkcz
using namespace std;
const int N=500005;
int n,m,deg[N];
int vis[N],q[N],rem;
int ban[N],qq[N];
int perm1[N],perm2[N];
vector<int> e[N],vq[N],E[N],f[N];
int dfs(int x,int fa){
f[x].PB(x);
for (auto i:E[x])
if (i!=fa){
int v=dfs(i,x);
if (v) f[x].PB(v);
}
if (f[x].size()==1) return x;
return 0;
}
void solve(int x){
int h=0,t=1;
qq[t]=x; vis[x]=1;
while (h!=t){
int x=qq[++h];
for (auto i:e[x]) ban[i]=1;
int nt=0;
For(i,1,*q)
if (!ban[q[i]]){
if (!vis[q[i]]){
vis[q[i]]=1;
qq[++t]=q[i];
E[q[i]].PB(x);
E[x].PB(q[i]);
}
}
else q[++nt]=q[i];
*q=nt;
for (auto i:e[x]) ban[i]=0;
}
For(i,1,t) f[qq[i]].resize(0);
if (dfs(qq[1],0))
if (f[qq[1]].size()==1)
f[E[qq[1]][0]].PB(qq[1]);
For(i,1,t) if (f[qq[i]].size()!=1){
//for (auto j:f[qq[i]]) cout<<j<<endl;
For(j,0,f[qq[i]].size()-1){
int p1=j,p2=(j+1)%f[qq[i]].size();
perm1[f[qq[i]][p1]]=rem;
perm2[f[qq[i]][p2]]=rem;
--rem;
}
}
}
void solve(){
scanf("%d%d",&n,&m);
For(i,1,n) e[i].resize(0);
For(i,1,n) E[i].resize(0);
For(i,1,m){
int x,y;
scanf("%d%d",&x,&y);
e[x].PB(y); e[y].PB(x);
}
For(i,0,n) vq[i].resize(0);
For(i,1,n) deg[i]=e[i].size(),vis[i]=0;
For(i,1,n) vq[deg[i]].PB(i);
rem=n;
for (;rem;){
int p=-1;
for (auto i:vq[rem-1])
if (deg[i]==rem-1)
p=i;
if (p==-1) break;
perm1[p]=perm2[p]=rem;
vis[p]=1; --rem;
for (auto i:e[p])
vq[--deg[i]].PB(i);
}
*q=0;
For(i,1,n) if (!vis[i]) q[++*q]=i;
For(i,1,n) if (!vis[i]) solve(i);
For(i,1,n) printf("%d ",perm1[i]); puts("");
For(i,1,n) printf("%d ",perm2[i]); puts("");
}
int main(){
int T;
scanf("%d",&T);
while (T--) solve();
} | 10 | CPP |
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define MOCK 0
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define DEBUGP(val) cerr << #val << "=" << val << "\n"
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const int N = 300;
vector<VI> trans(const vector<VI> &a) {
vector<VI> ret(N, VI(N));
REP(i, 0, N) REP(j, 0, N) ret[i][j] = a[j][i];
return ret;
}
ll calc(const vector<VI> &sig) {
ll tot = 0;
vector<VI> acc(N, VI(N + 1, 0));
REP(i, 0, N) {
REP(j, 0, N) {
acc[i][j + 1] = acc[i][j] + sig[i][j];
}
}
REP(i, 0, N) {
REP(j, 0, N) {
if (sig[i][j] == 0) continue;
REP(k, j + 1, N) {
if (sig[i][k] == 0) continue;
int dif = k - j;
if (i - dif >= 0) {
int idx = i - dif;
tot += 2 * (acc[idx][k + 1] - acc[idx][j]);
if (sig[i - dif][j] == 1) tot--;
if (sig[i - dif][k] == 1) tot--;
}
if (i + dif < N) {
int idx = i + dif;
tot += 2 * (acc[idx][k + 1] - acc[idx][j]);
if (sig[i + dif][j] == 1) tot--;
if (sig[i + dif][k] == 1) tot--;
}
}
}
}
return tot;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<string> s(h);
#if MOCK
REP(i, 0, h) s[i] = string(w, '#');
#else
REP(i, 0, h) cin >> s[i];
#endif
ll tot = 0;
REP(par, 0, 2) {
vector<VI> sig(N, VI(N, 0));
REP(i, 0, h) {
REP(j, 0, w) {
if (s[i][j] == '.') continue;
if ((i + j) % 2 == par) {
int x = (i + j - par) / 2;
int y = (i - j - par + w) / 2;
sig[x][y] = 1;
}
}
}
tot += calc(sig);
sig = trans(sig);
tot += calc(sig);
}
cout << tot / 2 << endl;
}
| 0 | CPP |
n = int(input())
l = [''] * n
for i in range(0, n, 4):
l[i] = 'a'
for i in range(1, n, 4):
l[i] = 'a'
for i in range(2, n, 4):
l[i] = 'b'
for i in range(3, n, 4):
l[i] = 'b'
print("".join(l)) | 8 | PYTHON3 |
n=int(input())
s=input()
str="EASY"
for x in s:
if x=='1':
str="HARD"
print(str) | 7 | PYTHON3 |
n,k=map(int,input().split())
p=list(map(int,input().split()))
s=sum(p[:k])
s_max=s
for i in range(k,n):
s=s-p[i-k]+p[i]
s_max=max(s_max,s)
print((s_max+k)/2) | 0 | PYTHON3 |
def main():
n = int(input())
a = list(map(int, input().split()))
l, r = [-1 for i in range(n)], [n for i in range(n)]
for i in range(n):
if i == 0:
continue
elif a[i] > a[i-1]:
l[i] = i - 1
else:
idx = l[i-1]
while idx != -1 and a[i] < a[idx]:
idx = l[idx]
l[i] = idx
for i in range(n-1, -1, -1):
if i == n-1:
continue
elif a[i] > a[i+1]:
r[i] = i + 1
else:
idx = r[i+1]
while idx != n and a[i] < a[idx]:
idx = r[idx]
r[i] = idx
ans = 0
for i in range(n):
ans += a[i] * (i-l[i]) * (r[i]-i)
print(ans)
if __name__ == '__main__':
main()
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long int M = 1e9 + 7;
const long long int mod = M;
const long long int N = 4e3 + 7;
long long int a[N], b[N], c[N];
vector<long long int> adj[N];
vector<pair<long long int, long long int> > v[N], ans;
long long int vist[N], root;
long long int val = 0, n;
void dfs(long long int src) {
for (auto x : adj[src]) {
dfs(x);
for (auto y : v[x]) v[src].push_back(y);
}
val++;
if (adj[src].size() == 0) {
if (b[src] != 0) {
cout << "NO";
exit(0);
} else {
v[src].push_back({val, src});
c[src] = val;
}
return;
}
sort(v[src].begin(), v[src].end());
if (v[src].size() < b[src]) {
cout << "NO";
exit(0);
}
long long int ins;
if (b[src] == 0)
ins = v[src][b[src]].first;
else
ins = v[src][b[src] - 1].first + 1;
for (long long int i = b[src]; i < v[src].size(); i++) {
v[src][i].first += 1;
}
v[src].push_back({ins, src});
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
if (a[i] != 0) {
adj[a[i]].push_back(i);
} else {
root = i;
}
}
dfs(root);
for (auto x : v[root]) {
ans.push_back({x.second, x.first});
}
sort(ans.begin(), ans.end());
cout << "YES\n";
for (auto x : ans) {
cout << x.second << " ";
}
}
| 8 | CPP |
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
long long extgcd(long long a, long long b, long long &x, long long &y){
if(b==0){
x=1; y=0;
return a;
}
long long d=extgcd(b,a%b,y,x);
y-=(a/b)*x;
return d;
}
int main(){
long long N;
cin>>N;
N*=2;
long long M=N;
vector<long long> d;
for(long long i=2;i*i<=M;i++){
if(M%i==0){
long long k=1;
while(M%i==0){
M/=i; k*=i;
}
d.push_back(k);
}
}
if(M!=1) d.push_back(M);
long long answer=N;
long long l=1;
for(int i=0;i<d.size();i++) l*=2;
for(long long i=0;i<l;i++){
long long dev=1;
long long judge=1;
for(int j=0;j<d.size();j++){
if((i%(judge*2))/judge==1) dev*=d[j];
judge*=2;
}
long long x,y;
extgcd(N/dev,dev,x,y);
y*=-1;
if(y>0) y%=(N/dev);
else{
y+=(abs(y)/(N/dev)+1)*(N/dev);
}
answer=min(answer,y*dev);
}
cout<<answer<<endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
void __print(int32_t x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(double x) { cout << x; }
void __print(float x) { cout << x; }
void __print(bool x) { cout << (x ? "true" : "false"); }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cout << '{';
__print(x.first);
cout << ',';
__print(x.second);
cout << '}';
}
template <typename T>
void __print(const T &x) {
long long f = 0;
cout << '{';
for (auto &i : x) cout << (f++ ? "," : ""), __print(i);
cout << "}";
}
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cout << ", ";
_print(v...);
}
long long power(long long base, long long exp) {
long long res = 1;
base %= 1000000007;
while (exp > 0) {
if (exp & 1) res = (res * base) % 1000000007;
base = (base * base) % 1000000007;
exp = exp >> 1;
}
return res;
}
long long inv(long long x) { return power(x, 1000000007 - 2) % 1000000007; }
const long long N = 1e3 + 3, M = N;
long long i, j;
long long dp[N][M];
long long ncr(long long n, long long r) {
if (r > n) return 0;
if (n == r or r == 0) return 1;
if (dp[n][r] != 0) return dp[n][r];
return dp[n][r] = ncr(n - 1, r - 1) + ncr(n - 1, r);
}
void solve() {
long long n, m, t;
cin >> n >> m >> t;
fill(&dp[0][0], &dp[0][0] + N * M, 0);
long long sm = 0;
for (i = 1; i <= m; i++) {
long long g = i, b = t - g;
if (g <= m and b <= n and b >= 4) {
sm += ncr(n, b) * ncr(m, g);
}
}
cout << sm << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
| 9 | CPP |
#include<bits/stdc++.h>
using namespace std;
int n,k,ans,i,j;
string s;
int main(){
cin>>n>>k>>s;
while(j<n){
while(j<n && s[j]=='1')++j;
ans=max(ans,j-i);
while(j<n && s[j]=='0')++j;
--k;
if(k<0){
while(i<n && s[i]=='1')++i;
while(i<n && s[i]=='0')++i;
}
ans=max(ans,j-i);
}
cout<<ans<<endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int T, n, m, k;
char c[200005];
int main() {
scanf("%d %d\n", &n, &k);
for (int i = 0; i < k; i++) {
c[i] = getchar();
}
int chg = 0;
for (int i = k; i < n; i++) {
char ch = getchar();
if (ch < c[i % k]) break;
if (ch > c[i % k]) {
chg = 1;
break;
}
}
if (chg) {
for (int i = k - 1; i >= 0; i--) {
if (c[i] == '9') {
c[i] = '0';
} else {
c[i]++;
break;
}
}
}
printf("%d\n", n);
for (int i = 0; i < n; i++) putchar(c[i % k]);
return 0;
}
| 9 | CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.