solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
constexpr int MAX_N = 1000;
int n, m;
cin >> n >> m;
bitset<MAX_N> back;
for(int i = 0; i < m; ++i) {
int c, d;
cin >> c >> d;
if(c > d) continue;
for(int j = c; j < d; ++j) {
back[j] = true;
}
}
cout << n + 1 + back.count() * 2 << endl;
return EXIT_SUCCESS;
} | 0 | CPP |
n = int(input())
response = list(input().split( ))
if '1' in response:
print('HARD')
else:
print('EASY') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, i, a, f[5], s;
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a;
f[a % 3]++;
}
if (f[1] < f[2])
s = f[1];
else
s = f[2];
s = s + f[0] / 2;
cout << s;
}
| 9 | CPP |
v = int( input() )
gh = input().split()
dicts = {}
dicts[0] = 0
dicts[1] = 0
for i in gh:
v = int(i)
if v & 1 == 0:
dicts[0] += 1
else:
dicts[1] += 1
if dicts[0] > dicts[1]:
count = 0
for i in gh:
count += 1
v = int(i)
if v & 1 == 1 :
print(count)
break
else:
count = 0
for i in gh:
count += 1
v = int(i)
if v & 1 == 0 :
print(count)
break | 7 | PYTHON3 |
#include <bits/stdc++.h>
typedef struct queen {
int x;
int y;
int sum;
int sub;
int ind;
} Queen;
using namespace std;
int vec[100010];
Queen q[100010];
bool compara_x(Queen a, Queen b) {
if (a.x > b.x) return 1;
if (a.x == b.x)
if (a.y > b.y)
return 1;
else
return 0;
return 0;
}
bool compara_y(Queen a, Queen b) {
if (a.y > b.y) return 1;
if (a.y == b.y)
if (a.x > b.x)
return 1;
else
return 0;
return 0;
}
bool compara_sum(Queen a, Queen b) {
if (a.sum > b.sum) return 1;
if (a.sum == b.sum)
if (a.x > b.x)
return 1;
else
return 0;
return 0;
}
bool compara_sub(Queen a, Queen b) {
if (a.sub > b.sub) return 1;
if (a.sub == b.sub)
if (a.x > b.x)
return 1;
else
return 0;
return 0;
}
int main() {
int n, k;
int a, b, atak[9];
Queen aux;
while (scanf("%d %d", &n, &k) != EOF) {
for (int i = 0; i < k; i++) {
vec[i] = 0;
scanf("%d %d", &a, &b);
q[i].x = a;
q[i].y = b;
q[i].sum = a + b;
q[i].sub = a - b;
q[i].ind = i;
}
sort(q, q + k, compara_x);
aux = q[0];
for (int i = 1; i < k; i++) {
if (aux.x == q[i].x) {
vec[aux.ind]++;
vec[q[i].ind]++;
}
aux = q[i];
}
sort(q, q + k, compara_y);
aux = q[0];
for (int i = 1; i < k; i++) {
if (aux.y == q[i].y) {
vec[aux.ind]++;
vec[q[i].ind]++;
}
aux = q[i];
}
sort(q, q + k, compara_sum);
aux = q[0];
for (int i = 1; i < k; i++) {
if (aux.sum == q[i].sum) {
vec[aux.ind]++;
vec[q[i].ind]++;
}
aux = q[i];
}
sort(q, q + k, compara_sub);
aux = q[0];
for (int i = 1; i < k; i++) {
if (aux.sub == q[i].sub) {
vec[aux.ind]++;
vec[q[i].ind]++;
}
aux = q[i];
}
for (int i = 0; i < 9; i++) atak[i] = 0;
for (int i = 0; i < k; i++) atak[vec[i]]++;
for (int i = 0; i < 9; i++) {
if (i) printf(" ");
printf("%d", atak[i]);
}
printf("\n");
}
return 0;
}
| 11 | CPP |
for i in range(int(input())):
n=int(input())
print(n//2-(n+1)%2) | 7 | PYTHON3 |
def solve(i, j, lights):
lights[i][j] ^= 1
pos = [(i+1,j),(i-1,j),(i, j+1),(i,j-1)]
for val in pos:
i, j = val
if 0 <= i < 3 and 0 <=j and j <3:
lights[i][j] ^= 1
def main():
m = n = 3
a = []
lights = [[1 for x in range(m)] for y in range(n)]
for i in range(3):
a.append([int(x) for x in input().split()])
for j in range(3):
if a[i][j] % 2:
solve(i, j, lights)
for row in lights:
print (''.join([str(x) for x in row]))
main() | 7 | PYTHON3 |
s = input()
result = 0
last_ch = s[0]
last_ch_len = 1
for i in range(1, len(s)):
if s[i] == last_ch:
last_ch_len += 1
else:
if last_ch_len%2 == 0:
result += 1
last_ch_len = 1
last_ch = s[i]
#print('{} {} {} {}'.format(s[i], last_ch, last_ch_len, result))
if last_ch_len%2 == 0:
result += 1
print(result)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxm = 2e3 + 10;
const int inf = 0x3f3f3f3f;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
int f, qr;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char x) {
*oS++ = x;
if (oS == oT) flush();
}
template <typename A>
inline bool read(A &x) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (x = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
x = x * 10 + (c & 15);
x *= f;
return 1;
}
inline bool read(char &x) {
while ((x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == ' ' ||
x == '\n' || x == '\r')
;
return x != EOF;
}
inline bool read(char *x) {
while ((*x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == '\n' ||
*x == ' ' || *x == '\r')
;
if (*x == EOF) return 0;
while (!(*x == '\n' || *x == ' ' || *x == '\r' || *x == EOF))
*(++x) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
*x = 0;
return 1;
}
template <typename A, typename... B>
inline bool read(A &x, B &...y) {
return read(x) && read(y...);
}
template <typename A>
inline bool write(A x) {
if (!x) putc('0');
if (x < 0) putc('-'), x = -x;
while (x) qu[++qr] = x % 10 + '0', x /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(char x) {
putc(x);
return 0;
}
inline bool write(const char *x) {
while (*x) {
putc(*x);
++x;
}
return 0;
}
inline bool write(char *x) {
while (*x) {
putc(*x);
++x;
}
return 0;
}
template <typename A, typename... B>
inline bool write(A x, B... y) {
return write(x) || write(y...);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::putc;
using io ::read;
using io ::write;
int top[maxm][maxm], down[maxm][maxm];
int last[30];
char s[maxm][maxm];
char t[maxm];
int main() {
int n, m;
read(n, m);
for (int i = 1; i <= n; i++) {
read(t + 1);
for (int j = 1; j <= m; j++) s[i][j] = t[j];
}
for (int j = 1; j <= m; j++) {
for (int i = 0; i < 26; i++) last[i] = inf;
for (int i = 1; i <= n; i++) {
for (int k = 0; k < 26; k++) {
if (k == s[i][j] - 'a') continue;
last[k] = inf;
}
last[s[i][j] - 'a'] = min(last[s[i][j] - 'a'], i);
top[i][j] = last[s[i][j] - 'a'];
}
}
for (int j = 1; j <= m; j++) {
for (int i = 0; i < 26; i++) last[i] = 0;
for (int i = n; i; i--) {
for (int k = 0; k < 26; k++) {
if (k == s[i][j] - 'a') continue;
last[k] = 0;
}
last[s[i][j] - 'a'] = max(last[s[i][j] - 'a'], i);
down[i][j] = last[s[i][j] - 'a'];
}
}
for (int i = 1; i <= n; i++) {
top[i][0] = i + 1;
down[i][0] = i - 1;
top[i][m + 1] = i + 1;
down[i][m + 1] = i - 1;
for (int j = 1; j <= m; j++) {
if (s[i][j] == s[i][j - 1]) {
top[i][j] = max(top[i][j], top[i][j - 1] - 1);
down[i][j] = min(down[i][j], down[i][j - 1] + 1);
} else {
top[i][j] = max(top[i][j], i);
down[i][j] = min(down[i][j], i);
}
}
for (int j = m; j; j--) {
if (s[i][j] == s[i][j + 1]) {
top[i][j] = max(top[i][j], top[i][j + 1] - 1);
down[i][j] = min(down[i][j], down[i][j + 1] + 1);
} else {
top[i][j] = max(top[i][j], i);
down[i][j] = min(down[i][j], i);
}
}
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
ans += min(i - top[i][j] + 1, down[i][j] - i + 1);
}
}
printf("%lld\n", ans);
return 0;
}
| 10 | CPP |
print(len([i for i in range(1, int(input())+1) if len(str(i))%2==1])) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
vector<tuple<string,int,int>>a;
for(int i=1;i<=n;i++)
{
string s;
int p;
cin>>s>>p;
p=-p;
a.push_back(make_tuple(s,p,i));
}
sort(a.begin(),a.end());
for(int i=0;i<n;i++)
{
cout<<get<2>(a[i])<<endl;
}
} | 0 | CPP |
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353,g=3;
const int N=1e6+5;
int pow2(int x,int y) {
int mul=1;
for(;y;y>>=1,x=x*1ll*x%mod) if(y&1) mul=mul*1ll*x%mod;
return mul;
}
int n,k;
int fac[N],inv[N];
int a[N<<1],b[N<<1];
int lim,rev[N<<1],q;
void NTT(int *v,int id) {
for(int i=1;i<lim;i++) if(rev[i]<i) swap(v[rev[i]],v[i]);
for(int mid=1;mid<lim;mid<<=1) {
int w=pow2(g,(mod-1)/(mid<<1));
if(id==-1) w=pow2(w,mod-2);
for(int l=0;l<lim;l+=(mid<<1)) {
int wn=1;
for(int i=l;i<mid+l;i++,wn=wn*1ll*w%mod) {
int x=v[i],y=1ll*v[i+mid]*wn%mod;
v[i]=(x+y)%mod;
v[i+mid]=(x-y+mod)%mod;
}
}
}
if(id==-1) {
for(int i=0;i<lim;i++) v[i]=v[i]*1ll*pow2(lim,mod-2)%mod;
}
return;
}
void init() {
fac[0]=1;
for(int i=1;i<=k;i++) fac[i]=fac[i-1]*1ll*i%mod;
inv[0]=1;
for(int i=1;i<=k;i++) inv[i]=pow2(fac[i],mod-2);
for(int i=0;i<=k;i++) {
if(i&1) a[i]=mod-inv[i];
else a[i]=inv[i];
b[i]=inv[i]*1ll*pow2(i,k)%mod;
}
lim=1;
q=0;
while(lim<k+k+1) lim<<=1,q++;
for(int i=1;i<lim;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(q-1));
NTT(a,1);
NTT(b,1);
for(int i=0;i<lim;i++) a[i]=a[i]*1ll*b[i]%mod;
NTT(a,-1);
return;
}
int main() {
scanf("%d%d",&n,&k);
init();
long long ans=0,now=1;
for(int i=1;i<=k;i++) {
now=now*(n-i+1)%mod;
if(!now) break;
ans=(ans+a[i]*1ll*now%mod*pow2(n+1,n-i)%mod)%mod;
}
printf("%lld\n",ans);
return 0;
} | 12 | CPP |
# -*- coding: utf-8 -*-
'''
7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7
'''
# from helper import elapsed_time
from bisect import bisect_left, insort_left
N = int(input())
s = list(input())
Q = int(input())
# a-zまでの26文字に対して、s文字列の各文字に該当するindexを格納しておく
d = {i: [] for i in range(26)}
for i, c in enumerate(s):
d[ord(c) - ord("a")].append(i)
# @elapsed_time
def convert(d: dict, s: list, q1: int, q2: str):
if s[q1] == q2:
return d, s
didx_before = ord(s[q1]) - ord("a")
didx_after = ord(q2) - ord("a")
idx = bisect_left(d[didx_before], q1)
d[didx_before].pop(idx)
s[q1] = q2
insort_left(d[didx_after], q1)
return d, s
# @elapsed_time
def calc(d: dict, q1: int, q2: int):
ans = 0
for i in range(26):
idx = bisect_left(d[i], q1)
if d[i] and q1 <= d[i][-1] and d[i][idx] < q2 + 1:
ans += 1
return ans
for _ in range(Q):
q, q1, q2 = input().split()
q = int(q)
if q == 1:
d, s = convert(d, s, int(q1) - 1, q2)
if q == 2:
print(calc(d, int(q1) - 1, int(q2) - 1))
| 0 | PYTHON3 |
s1=input()
s2=input()
if s1.lower()> s2.lower():
print(1)
elif s1.lower()< s2.lower():
print(-1)
else :
print(0)
| 7 | PYTHON3 |
def readInts():
return [int(x) for x in input().split(' ')]
Q = int(input())
for q in range(Q):
[N, K] = readInts()
A = readInts()
B = min(A) + K
if max(A) - K > B:
B = -1
print(B)
| 8 | PYTHON3 |
import sys
from math import ceil
cin=sys.stdin.buffer.readline
for _ in range(int(cin())):
n=int(cin())
nop,nep,noq,neq=0,0,0,0
p=list(map(int,cin().split()))
m=int(cin())
q=list(map(int,cin().split()))
for el in p:
if el%2==0:
nep+=1
else:
nop+=1
for el in q:
if el%2==0:
neq+=1
else:
noq+=1
print(neq*nep+noq*nop) | 7 | PYTHON3 |
#include<iostream>
using namespace std;
int main(){
long long x;
cin>>x;
cout<<x/500*1000+x%500/5*5;
return 0;
} | 0 | CPP |
n = int(input())
l = list(map(int, input().split()))
check = True
if (n == 1 and l[0] == 0) or l.count(0) > 1 or (l.count(0) == 0 and n > 1):
check = False
print("YES" if check else "NO")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 101, M = (long long)1e18;
long long arr[N], ind = 2, f[N], g[N];
void p_f() {
arr[1] = 1;
arr[2] = 2;
while (arr[ind] <= M) {
ind++;
arr[ind] = arr[ind - 1] + arr[ind - 2];
}
}
long long get_ans(long long n) {
vector<int> v;
while (n) {
int id = lower_bound(arr, arr + ind, n) - arr;
if (arr[id] == n) {
v.push_back(id);
n = 0;
} else {
id--;
v.push_back(id);
n -= arr[id];
}
}
v.push_back(0);
reverse(v.begin(), v.end());
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
int sz = v.size();
g[0] = 1;
f[0] = 0;
for (int i = 1; i < sz; i++) {
g[i] = g[i - 1] + f[i - 1];
f[i] = g[i - 1] * ((v[i] - v[i - 1] - 1) / 2) +
f[i - 1] * ((v[i] - v[i - 1]) / 2);
}
return f[sz - 1] + g[sz - 1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
p_f();
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << get_ans(n) << '\n';
}
}
| 10 | CPP |
n = int(input())
if(n%2==0):
print(n)
else:
print(2*n) | 0 | PYTHON3 |
z=list(map(int,input().split()))
arr=[]
for i in range(z[0]) :
y=list(map(int,input().split()))
arr.append(y[0]/y[1])
print("%.8f"%(min(arr)*z[1])) | 7 | PYTHON3 |
class Dice():
def __init__(self, d):
self.d = d
self.north = [2, 6, 3, 4, 1, 5]
self.south = [5, 1, 3, 4, 6, 2]
self.east = [4, 2, 1, 6, 5, 3]
self.west = [3, 2, 6, 1, 5, 4]
self.label = [(self.d[0], self.d[1], self.d[2])]
def roll(self, direction):
if direction == 'S':
self.d = [ self.d[self.south[i]-1] for i in range(6) ]
elif direction == 'N':
self.d = [ self.d[self.north[i]-1] for i in range(6) ]
elif direction == 'E':
self.d = [ self.d[self.east[i]-1] for i in range(6) ]
elif direction == 'W':
self.d = [ self.d[self.west[i]-1] for i in range(6) ]
def rollAll(self):
for i in range(4):
for i in range(4):
self.roll('E')
self.label.append((self.d[0], self.d[1], self.d[2]))
self.roll('S')
self.roll('E')
self.roll('S')
for i in range(4):
self.roll('E')
self.label.append((self.d[0], self.d[1], self.d[2]))
self.roll('S')
self.roll('S')
for i in range(4):
self.roll('E')
self.label.append((self.d[0], self.d[1], self.d[2]))
def getLabel(self):
return set(self.label)
def getDice(self):
return self.d
n = int(input())
ll = []
for i in range(n):
d = [ int(s) for s in input().split() ]
dice = Dice(d)
dice.rollAll()
l = dice.getLabel()
if l not in ll:
ll.append(l)
if len(ll) == n:
print('Yes')
else:
print('No')
| 0 | PYTHON3 |
n = int(input())
l = list(map(int,input().split()))
best = l
swaps = []
for i in range(n):
p = list(input())
for j in range(n):
if j>i and p[j] == '1':
swaps.append([i,j])
group = []
for i in range(n):
group.append([i])
for i in range(len(swaps)):
x = swaps[i][1]
y = swaps[i][0]
while isinstance(group[x],int) == 1:
x = group[x]
while isinstance(group[y],int) == 1:
y = group[y]
if y != x:
group[y] += group[x]
group[x] = y
for i in range(n):
if isinstance(group[i],list) == 1:
group[i] = sorted(group[i])
for i in range(n):
if isinstance(group[i],list) == 1:
x = []
for j in range(len(group[i])):
x.append(l[group[i][j]])
x = list(sorted(x))
for j in range(len(group[i])):
l[group[i][j]] = x[j]
s = str(l[0])
for i in range(1,n):
s += ' '+str(l[i])
print(s)
| 8 | PYTHON3 |
def train(p):
m = 0
s = {}
for pi in p:
k = s.get(pi-1, 0) + 1
s[pi] = k
m = max(k, m)
return len(p)-m
if __name__ == '__main__':
n = int(input())
p = list(map(int, input().split()))
print(train(p))
| 9 | PYTHON3 |
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector <long long> a(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int q;
cin>>q;
while(q--)
{
int b,e;
cin>>b>>e;
reverse(a.begin()+b,a.begin()+e);
}
int j;
for(j=0;j<n-1;j++)
{
cout<<a[j]<<" ";
}
cout<<a[j]<<endl;
return 0;
}
| 0 | CPP |
l=input()
goo=list(l)
n=len(l)
i=n//2
t=''
if n%2==0:
for i in range(n):
if i%2==0:
t+=goo[-1]
goo.pop(-1)
else:
t+=goo[0]
goo.pop(0)
else:
for i in range(n):
if i%2==1:
t+=goo[-1]
goo.pop(-1)
else:
t+=goo[0]
goo.pop(0)
print(t[::-1]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int F[1010];
int A[1010];
int n;
int main() {
cin >> n;
int i;
for (i = 1; i <= n; i++) cin >> A[i];
F[0] = 0;
F[1] = A[1];
i = 2;
while (i <= n) {
F[i] = min(max(F[i - 1], F[i - 2]), A[i]);
i++;
}
cout << F[n] << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
double const pi = 3.1415926536;
int main() {
int n;
cin >> n;
int a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
for (int i = 0; i < n; i++)
for (int j = i + 2; j <= n; j++) {
reverse(&a[i], &a[j]);
int f = 0;
for (int k = 0; k < n; k++) {
if (a[k] != b[k]) f = 1;
}
reverse(&a[i], &a[j]);
if (f == 0) {
cout << i + 1 << " " << j;
return (0);
;
}
}
cout << "0 0";
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:600000000")
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
double const pi = 3.1415926535897932384626433832795;
int const inf = (int)1e9;
long long const inf64 = (long long)4e18;
const string name = "c";
const int NMAX = 101010;
int n, m, l[NMAX], r[NMAX], L[NMAX], R[NMAX], K[NMAX], ans[NMAX];
vector<pair<int, int> > ev;
set<pair<int, int> > S;
vector<pair<int, int> > toremove;
int getans(int num) {
set<pair<int, int> >::iterator it = S.lower_bound(make_pair(r[num], -1));
toremove.clear();
while (it != S.end()) {
if (K[it->second] > 0) {
for (int i = 0; i < (int)toremove.size(); i++) S.erase(toremove[i]);
return it->second;
}
toremove.push_back(*it);
++it;
}
cout << "NO" << endl;
exit(0);
}
int main() {
cin >> m;
for (int i = 0; i < (int)m; i++) {
scanf("%d%d", &l[i], &r[i]);
ev.push_back(make_pair(l[i], i + 1));
}
cin >> n;
for (int i = 0; i < (int)n; i++) {
scanf("%d%d%d", &L[i], &R[i], &K[i]);
ev.push_back(make_pair(L[i], -(i + 1)));
}
sort(ev.begin(), ev.end());
memset(ans, 255, sizeof(ans));
for (int i = 0; i < (int)ev.size(); i++) {
if (ev[i].second < 0) {
int num = (-ev[i].second) - 1;
S.insert(make_pair(R[num], num));
} else {
int num = ev[i].second - 1;
int idx = getans(num);
ans[num] = idx;
K[idx]--;
}
}
cout << "YES" << endl;
for (int i = 0; i < (int)m; i++) printf("%d ", ans[i] + 1);
return 0;
}
| 9 | CPP |
#!/usr/bin/env python
# coding: utf-8
# In[4]:
k=int(input())
l=int(input())
m=int(input())
n=int(input())
d=int(input())
# In[5]:
count=0
for i in range(1,d+1):
if i%l==0 or i%m==0 or i%k==0 or i%n==0:
count+=1
# In[6]:
print(count)
# In[ ]:
| 7 | PYTHON3 |
n, m = map(int, input().split())
c = [0] + list(map(int, input().split()))
t = {i: [] for i in set(c[1:])}
for i in range(m):
a, b = map(int, input().split())
if c[a] != c[b]:
t[c[a]].append(c[b])
t[c[b]].append(c[a])
j, k = c[1], 0
for i, s in t.items():
l = len(set(s))
if l >= k:
if l > k: j, k = i, l
elif j > i: j = i
print(j) | 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string c;
cin>>c;
regex re("[aouie]");
cout<<(regex_match(c,re)?"vowel":"consonant")<<endl;
return 0;
} | 0 | CPP |
x = int(input())
for i in range(1, x):
if i % 2 == 1:
print("I hate that",end=" ")
else:
print("I love that",end=" ")
if x % 2 == 1:
print("I hate it",end=" ")
else:
print("I love it",end=" ") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long t, r = 1, r1 = 0, r2 = 0, k = 0, a, b, c = 1, m, d = 0, n, e, f,
x = 0, g, p = 0, q = 0, y = 0, z = 0;
vector<long long> v;
vector<long long> u;
set<long long> s;
std::vector<int>::iterator it;
string s1, s2, s3, s4;
cin >> n;
while (n--) {
cin >> m;
cout << m << endl;
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0),
cout.precision(15);
int t;
cin >> t;
while (t--) {
long long int s, a, b, c;
cin >> s >> a >> b >> c;
long long int ans = s / c;
ans += (ans / a) * b;
cout << ans << '\n';
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
inline int cmp(double x, double y = 0, double tol = 1e-9) {
return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
int n, m;
int map[300][300];
int main() {
int t;
char c;
std::cin >> t;
while (t--) {
memset(map, 0, sizeof(map));
int answer = 0;
std::cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
std::cin >> c;
map[i][j] = c - '0';
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
int ok = 1, side = 0;
if (!map[i][j] || map[i - 1][j - 1])
next:
continue;
while (map[i + side][j] && map[i][j + side]) {
if (map[i + side][j - 1] || map[i - 1][j + side]) goto next;
++side;
}
if (side < 2) goto next;
for (int k = 0; k < side; ++k)
if (!map[i + side - 1][j + k] || !map[i + k][j + side - 1]) goto next;
for (int k = -1; k <= side; ++k)
if (map[i + side][j + k] || map[i + k][j + side]) goto next;
for (int k = 1; k < side - 1; ++k)
if (map[i + 1][j + k] || map[i + k][j + 1] ||
map[i + side - 2][j + k] || map[i + k][j + side - 2])
goto next;
++answer;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
int ok = 1, side = 0;
if (!map[i][j] || map[i - 1][j])
next2:
continue;
while (map[i + side][j + side] && map[i + side][j - side]) {
if (map[i + side - 1][j - side] || map[i + side + 1][j - side] ||
map[i + side - 1][j + side] || map[i + side + 1][j + side] ||
map[i + side][j - side - 1] || map[i + side][j - side + 1] ||
map[i + side][j + side - 1] || map[i + side][j + side + 1] ||
map[i + side - 1][j - side - 1] ||
map[i + side - 1][j + side + 1])
goto next2;
++side;
}
if (side < 2) goto next2;
for (int di = side - 1, dj = side - 1; di <= 2 * (side - 1); ++di, --dj)
if (!map[i + di][j - dj] || !map[i + di][j + dj]) goto next2;
for (int di = side - 1, dj = side - 1; di <= 2 * (side - 1); ++di, --dj)
if (map[i + di][j - dj + 1] || map[i + di + 1][j - dj] ||
map[i + di + 1][j - dj - 1] || map[i + di][j + dj - 1] ||
map[i + di + 1][j + dj] || map[i + di + 1][j + dj + 1])
goto next2;
for (int k = 1; k < side - 1; ++k)
if (map[i + k + 1][j - k + 1] || map[i + k + 1][j + k - 1] ||
map[i + side + k - 2][j - side + k + 2] ||
map[i + side + k - 2][j + side - k - 2])
goto next2;
++answer;
}
std::cout << answer << std::endl;
}
return 0;
}
| 9 | CPP |
for _ in range(int(input())):
a = input()
r,q = 0,0
w = len(a)
for i in range(w):
if a[i]=="-":r+=1
else:q+=1
if r>q:w+=(i+1);r-=1
print(w) | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
const bool typetest = 1;
const int N = 1e5 + 5;
int n, a[N];
int k;
string s;
string dp[N];
void Read()
{
cin >> n >> k >> s;
}
char Max(char c)
{
return (c == 'a' || c == 'a' + k - 1) ? 'a' : char(c - 1);
}
void Solve()
{
for (int i = 0; i < n; ++i)
dp[i] = char('z' + 1);
dp[0] = Max(s[0]);
if (n > 1)
{
string v = "";
v.push_back(Max(s[1]));
v.push_back(s[0]);
dp[1] = min(dp[1], v);
v.clear();
v.push_back(Max(s[0]));
v.push_back(Max(s[1]));
}
//cout << dp[0] << " " << dp[1] << '\n';
for (int i = 0; i < n; ++i)
{
// /cout << i << ": " << dp[i] << "\n";
if (i + 1 < n)
{
dp[i + 1] = min(dp[i + 1], dp[i] + Max(s[i + 1]));
dp[i + 1] = min(dp[i + 1], dp[i].substr(0, dp[i].size() - 1) + s[i + 1] + dp[i].back());
}
if (i + 2 < n)
{
dp[i + 2] = min(dp[i + 2], dp[i] + Max(s[i + 2]) + s[i + 1]);
dp[i + 2] = min(dp[i + 2], dp[i].substr(0, dp[i].size() - 1) + s[i + 2] + dp[i].back() + s[i + 1]);
}
}
cout << dp[n - 1] << "\n";
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
Read();
Solve();
}
} | 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int,int>
#define ff first
#define ss second
#define boost ios::sync_with_stdio(false);cin.tie(nullptr)
#include "string"
int32_t main(){
int n;
cin>>n;
int a[n];
int xr = 0;
for(int i=0;i<n;i++){
cin>>a[i];
if(i>1)
xr ^= a[i];
}
int s = a[0] + a[1];
if(s < xr) {
cout<<-1<<endl;
return 0;
}
if((a[0]^a[1]) == xr){
cout<<0<<endl;
return 0;
}
int A = 0,B = 0;
A = B = (s - xr)/2;
if((s-xr)%2 != 0 or A > a[0] ){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<64;i++){
if(xr&(1ll<<i) && A&(1ll<<i)){
cout<<-1<<endl;
return 0;
}
}
for(int i=64;i>=0;i--){
if(xr&(1ll<<i) && (A|(1ll<<i)) < a[0] ){
A |= (1ll<<i);
}
}
int tra = a[0] - A;
if(tra == a[0] ){
cout<<-1<<endl;
return 0;
}
cout<< tra <<endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ul=unsigned long long;
int s[500],t[500],tmp[500][500],S[500],T[500],n,bit=-1,IN;
ul ans[500][500],u[500],v[500];
void ng(){
cout<<-1;
exit(0);
}
struct P{int va,idx,ve,ne,al;};
bool operator <(const P&a,const P&b){return a.va<b.va;};
priority_queue<P>que;
#define REP(i) for(int i=0;i<n;i++)
#define U ((u[i]>>bit)&1)
#define V ((v[j]>>bit)&1)
#define F(va) {\
if(!~tmp[i][j]){\
ans[i][j]+=((ul)1<<bit)*(tmp[i][j]=va);\
if(++S[i]<n&&IN)que.push({S[i],i,'S',s[i],1});\
if(++T[j]<n&&IN)que.push({T[j],j,'T',t[j],1});\
}\
}
void AL(){
int koushin=1;
while(koushin--){
REP(i)
if(s[i]==U){
bool f=0;
REP(j)f|=tmp[i][j]==s[i];
if(f)REP(j){koushin|=tmp[i][j]<0;F(s[i]^1);}
}
REP(j)
if(t[j]==V){
bool f=0;
REP(i)f|=tmp[i][j]==t[j];
if(f)REP(i){koushin|=tmp[i][j]<0;F(t[j]^1);}
}
}
}
int main(){
cin>>n;
REP(i)cin>>s[i];
REP(i)cin>>t[i];
REP(i)cin>>u[i];
REP(i)cin>>v[i];
while(++bit<64){
memset(tmp,-1,sizeof(tmp));
memset(S,0,sizeof(S));
memset(T,0,sizeof(T));
IN=0;
REP(i)if(s[i]^U)REP(j)F(s[i]^1);
REP(j)if(t[j]^V)REP(i)F(t[j]^1);
REP(i)REP(j)if(s[i]==U&&U==t[j]&&t[j]==V)F(s[i]);
AL();
IN=1;
REP(i)if(S[i]<n)que.push({S[i],i,'S',s[i],1});
REP(j)if(T[j]<n)que.push({T[j],j,'T',t[j],1});
while(que.size()){
P p=que.top();que.pop();
if(p.ve=='S'){
int i=p.idx;
if(p.va<S[i])continue;
REP(j)
if(!~tmp[i][j]){
F(p.ne);
p.ne^=p.al;p.al=0;
}
AL();
}
else{
int j=p.idx;
if(p.va<T[j])continue;
REP(i)
if(!~tmp[i][j]){
F(p.ne);
p.ne^=p.al;p.al=0;
}
AL();
}
}
}
REP(i){
ul A=ans[i][0];
REP(j)
if(s[i])A|=ans[i][j];
else A&=ans[i][j];
if(A!=u[i])ng();
}
REP(j){
ul A=ans[0][j];
REP(i)
if(t[j])A|=ans[i][j];
else A&=ans[i][j];
if(A!=v[j])ng();
}
REP(i){
REP(j)cout<<ans[i][j]<<" ";cout<<endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 26;
struct node {
int min, min2, s, s2;
node() {
min = min2 = inf;
s = s2 = 0;
}
} A[1201011];
int flag[1201011], c[301011], b[301011];
int n, t1, t2;
long long ans = 0;
void updata(int p) {
A[p].min = min(A[p + p].min, A[p + p + 1].min);
A[p].min2 = min(A[p + p].min2, A[p + p + 1].min2);
if (max(A[p + p].min, A[p + p + 1].min) > A[p].min)
A[p].min2 = min(A[p].min2, max(A[p + p].min, A[p + p + 1].min));
A[p].s = A[p].s2 = 0;
if (A[p].min == A[p + p].min) A[p].s += A[p + p].s;
if (A[p].min == A[p + p + 1].min) A[p].s += A[p + p + 1].s;
if (A[p].min2 == A[p + p].min) A[p].s2 += A[p + p].s;
if (A[p].min2 == A[p + p + 1].min) A[p].s2 += A[p + p + 1].s;
if (A[p].min2 == A[p + p].min2) A[p].s2 += A[p + p].s2;
if (A[p].min2 == A[p + p + 1].min2) A[p].s2 += A[p + p + 1].s2;
}
void setit(int p, int x) {
flag[p] += x;
A[p].min += x;
A[p].min2 += x;
}
void down(int p) {
if (!flag[p]) return;
setit(p + p, flag[p]);
setit(p + p + 1, flag[p]);
flag[p] = 0;
}
void ins(int p, int l, int r, int x, int k) {
if (l == r) {
A[p].min = k;
A[p].s = 1;
return;
}
down(p);
int mid = (l + r) >> 1;
if (x <= mid)
ins(p + p, l, mid, x, k);
else
ins(p + p + 1, mid + 1, r, x, k);
updata(p);
}
void ins(int p, int l, int r, int x, int y, int k) {
if (x > y) return;
if (l < r) down(p), updata(p);
if (l >= x && r <= y) {
setit(p, k);
return;
}
int mid = (l + r) >> 1;
if (x <= mid) ins(p + p, l, mid, x, y, k);
if (y > mid) ins(p + p + 1, mid + 1, r, x, y, k);
updata(p);
}
int count(int p, int l, int r, int x, int y) {
if (l < r) down(p), updata(p);
if (l >= x && r <= y) {
if (A[p].min2 <= 2)
return A[p].s + A[p].s2;
else if (A[p].min <= 2)
return A[p].s;
else
return 0;
}
int mid = (l + r) >> 1, t = 0;
if (x <= mid) t = count(p + p, l, mid, x, y);
if (y > mid) t += count(p + p + 1, mid + 1, r, x, y);
updata(p);
return t;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", c + i);
for (int i = 1; i <= n; ++i) b[c[i]] = i;
ins(1, 1, n, 1, 1);
for (int i = 2; i <= n; ++i) {
ins(1, 1, n, i, 1);
t1 = c[b[i] - 1];
t2 = c[b[i] + 1];
if (b[i] > 1 && b[i] < n) {
if (max(t1, t2) < i) ins(1, 1, n, 1, min(t1, t2), -1);
if (min(t1, t2) > i)
ins(1, 1, n, 1, i - 1, 1);
else if (max(t1, t2) < i)
ins(1, 1, n, max(t1, t2) + 1, n, 1);
else
ins(1, 1, n, min(t1, t2) + 1, n, 1);
} else {
t1 = (b[i] > 1) ? c[n - 1] : c[2];
if (t1 > i)
ins(1, 1, n, 1, i - 1, 1);
else
ins(1, 1, n, t1 + 1, n, 1);
}
ins(1, 1, n, i, 1);
ans += count(1, 1, n, 1, i - 1);
}
printf("%I64d\n", ans);
return 0;
}
| 10 | CPP |
import re
n = int(input())
print(re.sub('o(go)+', '***', input())) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s = 0, j = 1, s1 = 0, b;
cin >> n;
b = n;
while (n > 0) {
n = n - j;
if (n > 0)
j++;
else
j = j + 1 - 1;
s++;
}
if (n == 0) {
cout << s << endl;
for (int k = j; k >= 1; k--) {
cout << k << " ";
}
} else {
cout << s - 1 << endl;
for (int k = 1; k <= j - 2; k++) {
cout << k << " ";
s1 = s1 + k;
}
cout << (j - 1) + (b - (s1 + j - 1));
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long a, b, k;
bool isPrime(long long x) {
if (x == 2) return true;
if (x % 2 == 0) return false;
for (long long i = 3; i * i <= x; i++) {
if (x % i == 0) return false;
}
return true;
}
long long f(long long n, long long k);
long long g(long long n, long long k) {
long long ans = n;
for (long long i = 2; i < k; i++) ans -= f(n, i);
return ans;
}
long long f(long long n, long long k) {
if (k > n || !isPrime(k)) return 0;
if (n < k * k) return 1;
return g(n / k, k);
}
int main() {
scanf(
"%I64d"
"%I64d"
"%I64d",
&a, &b, &k);
long long ans = f(b, k) - f(a - 1, k);
printf(
"%I64d"
"\n",
ans);
return 0;
}
| 10 | CPP |
//Bishmillahir Rahmanir Rahim
//sajeedreefy
#include<bits/stdc++.h>
using namespace std;
#define SZ 100005
typedef long long int ll;
#define pb push_back
#define ull unsigned long long
#define mp make_pair
#define fr(i,n) for (ll i=0;i<n;i++)
#define ff first
#define ss second
#define mod 1000000007
void fastIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
signed main()
{
fastIO();
ll n,t=1,mini,x,l,c,m,i,j,k,count,y,sum;
string st;
cin >> t;
// ll arr[SZ];
while(t--)
{
cin >> n >> x;
ll arr[n+1];
sum=0;
vector <ll> vec,sec;
fr(i,n)
{
cin >> arr[i];
sum=sum+arr[i];
vec.pb(arr[i]);
}
count=0;
sec=vec;
c=0;
while(1)
{
for(i=0;i<sec.size();i++)
{
if(sec[i]%x!=0)
{
c=1;
break;
}
else
{
sum=sum+vec[i];
sec[i]=sec[i]/x;
}
}
if(c==1)
{
break;
}
}
cout << sum << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
int q1, q2, ans, q3;
while (scanf("%d%d", &q1, &q2) != EOF) {
ans = 0;
while (q1 > 0 && q2 > 0) {
q3 = min(q1, q2);
if (q3 == q2)
q2 += 3;
else
q1 += 3;
q2 -= 2;
q1 -= 2;
if (q1 < 0 || q2 < 0) break;
ans++;
}
printf("%d\n", ans);
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
const long long inf = 1000000000ll;
const long long inf64 = inf * inf;
const long long base = inf + 7;
const double pi = acos(-1.0);
using namespace std;
template <typename T, typename P>
const ostream& operator<<(ostream& os, const pair<T, P>& a) {
os.precision(10);
os << fixed;
os << "{";
os << a.first;
os << ' ';
os << a.second;
os << "} ";
return os;
}
bool solve() {
int n, k, m;
scanf("%d %d", &n, &k);
long long ans = 0;
vector<pair<int, int> > mas;
for (int i(0); i < k; i++) {
scanf("%d", &m);
vector<int> a(m);
for (int j(0); j < m; j++) scanf("%d", &a[j]);
int pos = 0;
while (pos + 1 < m && a[pos] + 1 == a[pos + 1]) pos++;
ans += m - (pos + 1);
mas.push_back(make_pair(a[0], a[pos]));
for (int j(pos + 1); j < m; j++) mas.push_back(make_pair(a[j], a[j]));
}
sort((mas).begin(), (mas).end());
for (int i(0); i + 1 < (int)mas.size(); i++) {
ans += mas[i + 1].second - mas[i + 1].first;
ans += mas[i + 1].second - mas[i + 1].first + 1;
}
cout << ans << '\n';
return true;
}
int main() {
solve();
return 0;
}
| 7 | CPP |
import math
def pow2(g):
res = 0
while g % 2 == 0:
g //= 2
res += 1
return res if g == 1 else None
T = int(input())
for _ in range(T):
x, y = [int(a) for a in input().split()]
x2, y2 = 0, 0
while x % 2 == 0:
x //= 2
x2 += 1
while y % 2 == 0:
y //= 2
y2 += 1
#print()
if x != y:
print(-1)
else:
# x2 can be changed to x +- 1, +-2, +-3
diff = abs(x2-y2)
steps = diff // 3 + int((diff%3) != 0)
print(steps)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 10;
vector<int> res;
int a[N], n2, n1, n3, n, sumup, sumdown, f[N];
int main() {
std::ios::sync_with_stdio(false);
cin >> n1 >> n2 >> n3;
for (int i = 1; i <= n1 + n2 + n3; i++) cin >> a[i];
sort(a + 1, a + n1 + 1);
sort(a + n1 + 1, a + n1 + n2 + 1);
sort(a + n1 + n2 + 1, a + n1 + n2 + n3 + 1);
for (int i = 1; i <= n1 + n2 + n3; i++) {
if (res.empty() || res.back() < a[i])
res.push_back(a[i]);
else {
int pos = lower_bound(res.begin(), res.end(), a[i]) - res.begin();
res[pos] = a[i];
}
}
cout << n1 + n2 + n3 - res.size();
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int inter(int l1, int r1, int l2, int r2) {
return max(0, min(r1, r2) - max(l1, l2) + 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
int l, r;
cin >> l >> r;
a[i] = {l, r};
};
sort(a.begin(), a.end());
int ans = 0;
vector<int> l(n), r(n);
l[n - 1] = a[n - 1].first, r[n - 1] = a[n - 1].second;
for (int i = n - 2; i >= 0; --i) {
l[i] = max(l[i + 1], a[i].first);
r[i] = min(r[i + 1], a[i].second);
}
int L = 0, R = 1e9;
for (int i = 0; i < n - 1; ++i) {
ans = max(ans, a[i].second - a[i].first + 1 + inter(L, R, l[i + 1], r[i + 1]));
L = max(L, a[i].first);
R = min(R, a[i].second);
ans = max(ans, max(0, R - L + 1) + max(0, r[i + 1] - l[i + 1] + 1));
}
cout << ans;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[2*100001], b[2*100001];
int main() {
int n, m = 0; cin >> n;
for(int i=1; i<=n; ++i) cin >> a[i];
for(int i=n; i>0; --i){
int sum = 0;
for(int k=i; k<=n; k+=i) sum += b[k];
if(a[i] != sum%2) {
b[i] = 1;
++m;
}
}
cout << m << endl;
for(int i=1; i<=n; ++i) if(b[i] == 1) cout << i << endl;
}
| 0 | CPP |
"""
NTC here
"""
from sys import setcheckinterval, stdin
setcheckinterval(1000)
# print("Case #{}: {} {}".format(i, n + m, n * m))
iin = lambda: int(stdin.readline())
lin = lambda: list(map(int, stdin.readline().split()))
def eqation(a,b):
for i in range(26):
if a[i]>b[i]:
return False
return True
n=iin()
s=input()
m=iin()
a=[input() for i in range(m)]
sa=[[0 for i in range(26)] for j in range(n)]
for i in range(n):
sa[i][ord(s[i])-ord('a')]+=1
for i in range(1,n):
for j in range(26):
sa[i][j]+=sa[i-1][j]
sa1=[[0 for i in range(26)] for j in range(m)]
for i in range(m):
for j,k in enumerate(a[i]):
sa1[i][ord(k)-ord('a')]+=1
for i in range(m):
l,r=0,n-1
mn=0
ans=n-1
while l<=r:
m=l+r
m//=2
if eqation(sa1[i],sa[m]):
r=m-1
ans=min(ans,m)
else:
l=m+1
print(ans+1)
| 8 | PYTHON3 |
T=int(input())
for _ in range(T):
n=int(input())
a=input().split()
for i in range(n):
a[i]=int(a[i])
a.sort()
mini=a[1]-a[0]
for i in range(1, n):
if a[i]-a[i-1]<mini:
mini=a[i]-a[i-1]
if mini==0:
break
print(mini) | 8 | PYTHON3 |
if __name__ == "__main__":
n,x= map(int, input().split())
Nums = list(map(int, input().split()))
step=0
if Nums.count(x)>0:
Nums.sort()
step=x-Nums.index(x)+1
else:
Nums.append(x)
Nums.sort()
step=x-Nums.index(x)
print(step)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10000000")
using namespace std;
template <typename T>
inline T abs(T a) {
return ((a < 0) ? -a : a);
}
template <typename T>
inline T sqr(T a) {
return a * a;
}
const int INF = (int)1E9 + 7;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
int d[30][110][110], k;
int cost[30][30];
string s;
int solve(char c, int leng, int k) {
if (leng == 1) return 0;
if (d[c - 'a'][leng][k] == -1) {
int& ans = d[c - 'a'][leng][k] = -INF;
int npos = int((s).size()) - leng + 1;
for (char nc = 'a'; nc <= 'z'; nc++) {
if (nc != s[npos] && k > 0) {
ans = max(ans, solve(nc, leng - 1, k - 1) + cost[c - 'a'][nc - 'a']);
}
if (nc == s[npos])
ans = max(ans, solve(nc, leng - 1, k) + cost[c - 'a'][nc - 'a']);
}
}
return d[c - 'a'][leng][k];
}
int main() {
memset(d, -1, sizeof d);
cin >> s >> k;
int n;
cin >> n;
for (int i = 0; i < int(n); ++i) {
char u, v;
scanf(" %c %c ", &u, &v);
u -= 'a', v -= 'a';
scanf("%d", &cost[u][v]);
}
int maxV = -INF;
for (char c = 'a'; c <= 'z'; c++) {
if (s[0] != c && k > 0) {
maxV = max(maxV, solve(c, int((s).size()), k - 1));
}
if (s[0] == c) maxV = max(maxV, solve(c, int((s).size()), k));
}
cout << maxV << endl;
return 0;
}
| 9 | CPP |
def ryad(n):
if n % 4 == 1 or n % 4 == 2:
n=n+2
rd=n//2-1
return rd
c=input()
n=int(c[0:len(c)-1])
s=c[-1]
obsl="fedabc"
x=ryad(n)
time=x*6+x//2*3+x-x//2+obsl.index(s)+1
print(time)
| 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fast_in_out ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ln "\n"
#define sc scanf
#define pr printf
const double EPS = (double) 1e-9;
const double pii = acos(-1);
const int mod = 1000000007;
const int N = (int) 1e5;
ll arr[N + 1];
void prefixSum()
{
for(int i = 1; i <= N; i++)
{
arr[i] += arr[i - 1];
}
}
int main()
{
fast_in_out;
map <char, int> mp;
char ch = 'a';
int num = 1;
while(ch <= 'z')
{
mp.insert({ch, num});
ch++;
num++;
}
int n, q;
cin >> n >> q;
string s;
cin >> s;
for(int i = 0; i < n; i++)
{
arr[i] = mp[s[i]];
}
prefixSum();
for(int i = 0; i < q; i++)
{
int l, r;
cin >> l >> r;
l--;
r--;
ll ans = arr[r];
if(l)
{
ans -= arr[l - 1];
}
cout << ans << ln;
}
}
| 8 | CPP |
#include<bits/stdc++.h>
#define lol(i,n) for(int i=0;i<n;i++)
using namespace std;
int d[1010][1010],h,w,a;
int main(){
while(cin>>h>>w>>a&&h){
int x=0,y=0;
memset(d,0,sizeof(d));
d[0][0]=a;
lol(i,h)lol(j,w){
cin>>a;
d[i+1][j]+=d[i][j]/2+d[i][j]%2*(1-a);
d[i][j+1]+=d[i][j]/2+d[i][j]%2*a;
d[i][j]+=a;
}
while(x<w&&y<h){
if(d[y][x]%2)y++;
else x++;
}
cout<<y+1<<" "<<x+1<<endl;
}
return 0;
}
| 0 | CPP |
#include <iostream>
using namespace std;
int main()
{
int A, B, C;
cin >> A >> B >> C;
cout << A * B / 2;
} | 0 | CPP |
#include <iostream>
#include <map>
#include <string>
int main(int argc, char *argv[]) {
int q;
std::cin >> q;
std::map<std::string, long> m;
for (int i = 0; i < q; ++i) {
int c;
std::string key;
std::cin >> c >> key;
switch (c) {
case 0:
int x;
std::cin >> x;
m[key] = x;
break;
case 1:
if (m.find(key) != m.end())
std::cout << m.at(key) << std::endl;
else
std::cout << 0 << std::endl;
break;
case 2:
m.erase(key);
break;
case 3:
std::string r;
std::cin >> r;
auto begin = m.lower_bound(key), end = m.upper_bound(r);
for (auto i = begin; i != end; ++i) {
std::cout << i->first << " " << i->second << std::endl;
}
break;
}
}
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 10;
const int INF = 1 << 30;
int p[maxn], f1[maxn], f2[maxn], t[maxn];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
for (int i = 1; i <= n; i++) t[i] = i == 1 ? 0 : INF;
for (int i = n; i >= 1; i--) {
if (f1[i + 1] || p[i] == 100) f1[i] = 1;
if (f2[i + 1] || p[i] > 0) f2[i] = 1;
}
int ans = 0;
bool ok = 0;
for (int i = 1; i <= n; i++)
if (t[i] <= k) {
if (p[i] != 100 && f2[i + 1]) {
t[i + 1] = min(t[i + 1], t[i] + 1);
}
ans++;
if (p[i] > 0) {
if (f1[i + 1]) {
t[i + 2] = min(t[i + 2], t[i] + 1);
if (i + 1 == n) ok = 1;
} else {
ans += min(k - t[i], n - i);
for (int j = i + 1; j <= n; j++)
if (f2[j] && t[i] + j - i <= k) {
if (j == n) ok = 1;
t[j + 1] = min(t[j + 1], t[i] + j - i);
if (p[i] != 100) t[j] = min(t[j], t[i] + j - i);
}
}
} else {
if (f2[i + 1]) t[i + 1] = min(t[i + 1], t[i] + 1);
}
}
if (ok) ans++;
printf("%d\n", ans);
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct pt {
long long x, y;
};
struct seg {
pt s, f;
};
seg a[3];
inline bool eq(pt a, pt b) { return a.x == b.x && a.y == b.y; }
long long cp(seg a1, seg b1) {
pt a = {a1.f.x - a1.s.x, a1.f.y - a1.s.y};
pt b = {b1.f.x - b1.s.x, b1.f.y - b1.s.y};
return (long long)a.x * b.x + (long long)a.y * b.y;
}
long long dp(seg a1, seg b1) {
pt a = {a1.f.x - a1.s.x, a1.f.y - a1.s.y};
pt b = {b1.f.x - b1.s.x, b1.f.y - b1.s.y};
return (long long)a.x * b.y - (long long)a.y * b.x;
}
double len(pt a, pt b) {
return sqrt((double)(a.x - b.x) * (a.x - b.x) +
(double)(a.y - b.y) * (a.y - b.y));
}
bool in(pt a1, seg b1) {
seg a, b;
b.s = a.s = a1;
a.f = b1.s, b.f = b1.f;
if (dp(a, b) == 0 && cp(a, b) < 0)
return 1;
else
return 0;
}
bool check(seg a, seg b, seg c) {
if (eq(a.s, b.f)) swap(b.f, b.s);
if (eq(a.f, b.s)) swap(a.f, a.s);
if (eq(a.f, b.f)) swap(a.f, a.s), swap(b.f, b.s);
if (!eq(a.s, b.s)) return 0;
if (cp(a, b) < 0 || dp(a, b) == 0) return 0;
if (in(c.f, a) && in(c.s, b)) swap(c.f, c.s);
if (!in(c.s, a) || !in(c.f, b)) return 0;
if ((double)abs(len(c.s, a.s)) / abs(len(a.s, a.f)) < 0.2 - 1E-10) return 0;
if ((double)abs(len(c.s, a.f)) / abs(len(a.s, a.f)) < 0.2 - 1E-10) return 0;
if ((double)abs(len(c.f, b.s)) / abs(len(b.s, b.f)) < 0.2 - 1E-10) return 0;
if ((double)abs(len(c.f, b.f)) / abs(len(b.s, b.f)) < 0.2 - 1E-10) return 0;
return 1;
}
int main(void) {
int t;
cin >> t;
for (int curt = 0; curt < t; curt++) {
bool ans = 0;
for (int i = 0; i < 3; i++)
scanf("%I64d%I64d%I64d%I64d", &a[i].s.x, &a[i].s.y, &a[i].f.x, &a[i].f.y);
for (int i = 0; i < 3; i++)
for (int j = i + 1; j < 3; j++)
if (3 - i - j != i && 3 - i - j != j)
if (check(a[i], a[j], a[3 - i - j])) ans = 1;
if (ans)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 8 | CPP |
import math
import os
import random
import re
import sys
if __name__ == '__main__':
a = list(map(int, input().rstrip().split()))
n=int(a[0])
m=int(a[1])
a=int(a[2])
c,d=0,0
if(n%a==0):
c=n//a
else:
c=(n//a)+1
if(m%a==0):
d=m//a
else:
d=(m//a)+1
t=c*d
print(t)
| 7 | PYTHON3 |
import random, math, sys
from copy import deepcopy as dc
from bisect import bisect_left, bisect_right
from collections import Counter
input = sys.stdin.readline
# Function to take input
def input_test():
n, m, k = map(int, input().strip().split(" "))
grid = []
for i in range(n):
grid.append(list(input().strip()))
def dfsUtil(i, j):
nonlocal n, m, k, grid
adj = [
[0, 1], [0, -1], [1, 0], [-1, 0]
]
stack = [[i, j]]
while stack:
if k <= 0:
return
i, j = stack[-1][0], stack[-1][1]
stack.pop()
for kj in adj:
ni, nj = i + kj[0], j+ kj[1]
if 0 <= ni < n and 0 <= nj < m and grid[ni][nj] == ".":
if k <= 0:
return
grid[ni][nj] = "Y"
k -= 1
# print(i, j, "=>", ni, nj, "K = ", k)
stack.append([ni, nj])
li, lj = 0, 0
s = 0
for i in range(n):
for j in range(m):
if grid[i][j] == ".":
s += 1
li, lj = i+1-1, j+1-1
k = s - k - 1
grid[li][lj] = "Y"
dfsUtil(li, lj)
# print(grid)
for i in range(n):
for j in range(m):
if grid[i][j] == "Y":
grid[i][j] = "."
elif grid[i][j] == ".":
grid[i][j] = "X"
for row in grid:
print("".join(row))
# Function to test my code
def test():
pass
input_test()
# test() | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string ss, sk = "";
cin >> ss;
int len = ss.length() - 1;
while (ss[len] == '0') len--;
if (len < 0) {
cout << "YES" << endl;
return 0;
}
for (int i = 0; i <= len; i++) {
if (ss[i] != ss[len - i]) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}
| 7 | CPP |
nk=input().split()
n=int(nk[0])
for i in range(int(nk[1])):
if str(n)[-1]=="0":
n=int(n/10)
else:
n-=1
print(n) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void print(T x, char let) {
print(x), putchar(let);
}
const int N = 3e3 + 5;
int t, n, a[N], d[N][N][2];
vector<int> G[N];
int main() {
t = read();
while (t--) {
n = read();
for (int i = 1; i <= n; i++) G[i].clear();
for (int i = 1; i <= n; i++) {
a[i] = read();
G[a[i]].push_back(i);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 0; k <= 1; k++) {
d[i][j][k] = 1e5;
if (j <= i) d[i][j][k] = 0;
}
for (int l = 2; l <= n; l++) {
for (int i = 1; i <= n - l + 1; i++) {
int j = i + l - 1;
int x = a[i];
if (a[i + 1] == a[i])
d[i][j][0] = min(d[i][j][0], d[i + 1][j][0]);
else
d[i][j][0] = min(d[i][j][0], d[i + 1][j][0] + 1);
if (a[j] == a[i])
d[i][j][0] = min(d[i][j][0], d[i + 1][j][1]);
else
d[i][j][0] = min(d[i][j][0], d[i + 1][j][1] + 1);
if (a[j] == a[i])
d[i][j][1] = min(d[i][j][1], d[i + 1][j][1]);
else
d[i][j][1] = min(d[i][j][1], d[i + 1][j][1] + 1);
int k;
for (k = 0; k < G[x].size(); k++)
if (G[x][k] == i) break;
for (int y = k + 1; y < G[x].size(); y++) {
int q = G[x][y];
if (q < j) {
if (a[q + 1] == x)
d[i][j][0] = min(d[i][j][0], d[i][q][0] + d[q + 1][j][0]);
else
d[i][j][0] = min(d[i][j][0], d[i][q][0] + d[q + 1][j][0] + 1);
if (a[j] == x)
d[i][j][0] = min(d[i][j][0], d[i][q][0] + d[q + 1][j][1]);
else
d[i][j][0] = min(d[i][j][0], d[i][q][0] + d[q + 1][j][1] + 1);
if (a[j] == a[i])
d[i][j][1] = min(d[i][j][1], d[i][q][0] + d[q + 1][j][1]);
else
d[i][j][1] = min(d[i][j][1], d[i][q][0] + d[q + 1][j][1] + 1);
if (a[j] == a[q])
d[i][j][1] = min(d[i][j][1], d[i][q][1] + d[q + 1][j][1]);
else
d[i][j][1] = min(d[i][j][1], d[i][q][1] + d[q + 1][j][1] + 1);
} else if (q == j) {
if (a[i + 1] == a[i])
d[i][j][0] = min(d[i][j][0], d[i + 1][j - 1][0]);
else
d[i][j][0] = min(d[i][j][0], d[i + 1][j - 1][0] + 1);
if (a[i + 1] == a[i])
d[i][j][1] = min(d[i][j][1], d[i + 1][j - 1][0]);
else
d[i][j][1] = min(d[i][j][1], d[i + 1][j - 1][0] + 1);
if (a[j - 1] == a[i])
d[i][j][0] = min(d[i][j][0], d[i + 1][j - 1][1]);
else
d[i][j][0] = min(d[i][j][0], d[i + 1][j - 1][1] + 1);
if (a[j - 1] == a[i])
d[i][j][1] = min(d[i][j][1], d[i + 1][j - 1][1]);
else
d[i][j][1] = min(d[i][j][1], d[i + 1][j - 1][1] + 1);
}
}
}
}
cout << min(d[1][n][0], d[1][n][1]) << endl;
}
return 0;
}
| 9 | CPP |
n = int(input())
nums = list(map(int, input().split()))
nums.sort()
for idx in range(len(nums)-1, -1, -1):
if nums[idx] < 0:
print(nums[idx])
break
intSqrt = int(pow(nums[idx], 0.5))
if intSqrt*intSqrt != nums[idx]:
print(nums[idx])
break
| 7 | PYTHON3 |
#include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
signed main(){
int n;
cin>>n;
rep(i,0,n){
string s;
cin>>s;
int ans=0;
int sz=s.size();
if(sz<6){
ans=-1;
}else if(s[0]=='>'&&s[1]=='\''&&s[sz-1]=='~'&&sz%2==0&&s[sz/2]=='#'){
rep(i,2,sz-1){
if(i!=sz/2&&s[i]!='='){
ans=-1; break;
}
if(i==sz-2) ans=1;
}
}else if(s[0]=='>'&&s[1]=='^'&&s[sz-2]=='~'&&s[sz-1]=='~'&&sz%2==0){
rep(i,2,sz-2){
if(i%2==0&&s[i]!='Q'){
ans=-1; break;
}if(i%2&&s[i]!='='){
ans=-1; break;
}
if(i==sz-3) ans=2;
}
}else{
ans=-1;
}
if(ans==-1) o("NA");
else if(ans==1) o("A");
else if(ans==2) o("B");
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxi = 54321;
int papa[maxi];
int from[maxi], to[maxi];
int range[maxi];
bool visited[maxi];
void init(int n) {
for (int i = 0; i <= n; i++) {
papa[i] = i;
}
}
int get(int x) { return (x == papa[x] ? x : papa[x] = get(papa[x])); }
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> from[i] >> to[i];
}
init(n);
int k = 0;
for (int i = 1; i <= m; i++) {
int x = get(from[i]);
int y = get(to[i]);
if (x != y) {
range[k++] = i;
visited[i] = true;
papa[x] = y;
}
}
init(n);
for (int i = m; i >= 1; i--) {
int x = get(from[i]);
int y = get(to[i]);
if (x != y) {
if (!visited[i]) {
visited[i] = true;
range[k++] = i;
}
papa[x] = y;
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
init(n);
for (int i = 0; i < k; i++) {
if (range[i] < l || range[i] > r) {
int x = get(from[range[i]]);
int y = get(to[range[i]]);
if (x != y) {
papa[x] = y;
}
}
}
int ans(0);
for (int i = 1; i <= n; i++) {
if (papa[i] == i) ans++;
}
cout << ans << endl;
}
}
| 10 | CPP |
#import bisect
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
class Sortedlist:
def __init__(self, iterable=[], _load=200):
"""Initialize sorted list instance."""
values = sorted(iterable)
self._len = _len = len(values)
self._load = _load
self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
self._list_lens = [len(_list) for _list in _lists]
self._mins = [_list[0] for _list in _lists]
self._fen_tree = []
self._rebuild = True
def _fen_build(self):
"""Build a fenwick tree instance."""
self._fen_tree[:] = self._list_lens
_fen_tree = self._fen_tree
for i in range(len(_fen_tree)):
if i | i + 1 < len(_fen_tree):
_fen_tree[i | i + 1] += _fen_tree[i]
self._rebuild = False
def _fen_update(self, index, value):
"""Update `fen_tree[index] += value`."""
if not self._rebuild:
_fen_tree = self._fen_tree
while index < len(_fen_tree):
_fen_tree[index] += value
index |= index + 1
def _fen_query(self, end):
"""Return `sum(_fen_tree[:end])`."""
if self._rebuild:
self._fen_build()
_fen_tree = self._fen_tree
x = 0
while end:
x += _fen_tree[end - 1]
end &= end - 1
return x
def _fen_findkth(self, k):
"""Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`)."""
_list_lens = self._list_lens
if k < _list_lens[0]:
return 0, k
if k >= self._len - _list_lens[-1]:
return len(_list_lens) - 1, k + _list_lens[-1] - self._len
if self._rebuild:
self._fen_build()
_fen_tree = self._fen_tree
idx = -1
for d in reversed(range(len(_fen_tree).bit_length())):
right_idx = idx + (1 << d)
if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
idx = right_idx
k -= _fen_tree[idx]
return idx + 1, k
def _delete(self, pos, idx):
"""Delete value at the given `(pos, idx)`."""
_lists = self._lists
_mins = self._mins
_list_lens = self._list_lens
self._len -= 1
self._fen_update(pos, -1)
del _lists[pos][idx]
_list_lens[pos] -= 1
if _list_lens[pos]:
_mins[pos] = _lists[pos][0]
else:
del _lists[pos]
del _list_lens[pos]
del _mins[pos]
self._rebuild = True
def _loc_left(self, value):
"""Return an index pair that corresponds to the first position of `value` in the sorted list."""
if not self._len:
return 0, 0
_lists = self._lists
_mins = self._mins
lo, pos = -1, len(_lists) - 1
while lo + 1 < pos:
mi = (lo + pos) >> 1
if value <= _mins[mi]:
pos = mi
else:
lo = mi
if pos and value <= _lists[pos - 1][-1]:
pos -= 1
_list = _lists[pos]
lo, idx = -1, len(_list)
while lo + 1 < idx:
mi = (lo + idx) >> 1
if value <= _list[mi]:
idx = mi
else:
lo = mi
return pos, idx
def _loc_right(self, value):
"""Return an index pair that corresponds to the last position of `value` in the sorted list."""
if not self._len:
return 0, 0
_lists = self._lists
_mins = self._mins
pos, hi = 0, len(_lists)
while pos + 1 < hi:
mi = (pos + hi) >> 1
if value < _mins[mi]:
hi = mi
else:
pos = mi
_list = _lists[pos]
lo, idx = -1, len(_list)
while lo + 1 < idx:
mi = (lo + idx) >> 1
if value < _list[mi]:
idx = mi
else:
lo = mi
return pos, idx
def add(self, value):
"""Add `value` to sorted list."""
_load = self._load
_lists = self._lists
_mins = self._mins
_list_lens = self._list_lens
self._len += 1
if _lists:
pos, idx = self._loc_right(value)
self._fen_update(pos, 1)
_list = _lists[pos]
_list.insert(idx, value)
_list_lens[pos] += 1
_mins[pos] = _list[0]
if _load + _load < len(_list):
_lists.insert(pos + 1, _list[_load:])
_list_lens.insert(pos + 1, len(_list) - _load)
_mins.insert(pos + 1, _list[_load])
_list_lens[pos] = _load
del _list[_load:]
self._rebuild = True
else:
_lists.append([value])
_mins.append(value)
_list_lens.append(1)
self._rebuild = True
def discard(self, value):
"""Remove `value` from sorted list if it is a member."""
_lists = self._lists
if _lists:
pos, idx = self._loc_right(value)
if idx and _lists[pos][idx - 1] == value:
self._delete(pos, idx - 1)
def remove(self, value):
"""Remove `value` from sorted list; `value` must be a member."""
_len = self._len
self.discard(value)
if _len == self._len:
raise ValueError('{0!r} not in list'.format(value))
def pop(self, index=-1):
"""Remove and return value at `index` in sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
value = self._lists[pos][idx]
self._delete(pos, idx)
return value
def bisect_left(self, value):
"""Return the first index to insert `value` in the sorted list.
if value > bigger element, it returns len
"""
pos, idx = self._loc_left(value)
return self._fen_query(pos) + idx
def bisect_right(self, value):
"""Return the last index to insert `value` in the sorted list."""
pos, idx = self._loc_right(value)
return self._fen_query(pos) + idx
def count(self, value):
"""Return number of occurrences of `value` in the sorted list."""
return self.bisect_right(value) - self.bisect_left(value)
def __len__(self):
"""Return the size of the sorted list."""
return self._len
def __getitem__(self, index):
"""Lookup value at `index` in sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
return self._lists[pos][idx]
def __delitem__(self, index):
"""Remove value at `index` from sorted list."""
pos, idx = self._fen_findkth(self._len + index if index < 0 else index)
self._delete(pos, idx)
def __contains__(self, value):
"""Return true if `value` is an element of the sorted list."""
_lists = self._lists
if _lists:
pos, idx = self._loc_left(value)
return idx < len(_lists[pos]) and _lists[pos][idx] == value
return False
def __iter__(self):
"""Return an iterator over the sorted list."""
return (value for _list in self._lists for value in _list)
def __reversed__(self):
"""Return a reverse iterator over the sorted list."""
return (value for _list in reversed(self._lists) for value in reversed(_list))
def __repr__(self):
"""Return string representation of sorted list."""
return 'SortedList({0})'.format(list(self))
n,q = map(int,input().split())
arr = list(map(int,input().split()))
arr = sorted(arr)
d = [0]
for i in range(n-1):
d.append(arr[i+1]-arr[i])
sorted_arr = Sortedlist(arr)
sorted_d = Sortedlist(d)
#print(sorted_arr)
#print(sorted_arr[-1]-sorted_arr[0])
print(sorted_arr[-1]-sorted_arr[0]-sorted_d[-1])
#print("---",arr,d)
for j in range(q):
op, x = map(int,input().split())
if op==0:
loc = sorted_arr.bisect_left(x)
frontd,reard = 0,0
if 0<loc<len(sorted_arr): frontd = sorted_arr[loc] - sorted_arr[loc-1]
if loc<len(sorted_arr)-1: reard = sorted_arr[loc+1] - sorted_arr[loc]
sorted_arr.pop(loc)
if frontd>0:
loc1 = sorted_d.bisect_left(frontd)
sorted_d.pop(loc1)
if reard>0:
loc2 = sorted_d.bisect_left(reard)
sorted_d.pop(loc2)
if frontd>0 and reard>0:
sorted_d.add(frontd+reard)
else:
loc = sorted_arr.bisect_left(x)
frontd,reard = 0,0
if loc>0: frontd = x - sorted_arr[loc-1]
if loc<len(sorted_arr): reard = sorted_arr[loc] - x
sorted_arr.add(x)
if frontd>0:
sorted_d.add(frontd)
if reard>0:
sorted_d.add(reard)
if frontd>0 and reard>0:
loct = sorted_d.bisect_left(frontd+reard)
sorted_d.pop(loct)
if len(sorted_arr)>0 and len(sorted_d)>0:
print(sorted_arr[-1]-sorted_arr[0]-sorted_d[-1])
else:
print(0)
# print(sorted_arr)
# print(sorted_d)
| 10 | PYTHON3 |
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <functional>
using namespace std;
typedef vector<int>::iterator viter;
const int N = 400010;
int _w;
int n, m, ans;
vector<int> right[N];
priority_queue<int, vector<int>, greater<int> > pq;
int main() {
_w = scanf( "%d%d", &n, &m );
for( int i = 0; i < n; ++i ) {
int l, r;
_w = scanf( "%d%d", &l, &r );
l += m, swap(l, r);
if( l <= r ) right[l].push_back(r);
}
for( int i = 1; i <= m+m; ++i ) {
for( viter it = right[i].begin(); it != right[i].end(); ++it )
pq.push(*it);
while( !pq.empty() && pq.top() < i )
pq.pop();
if( !pq.empty() )
++ans, pq.pop();
}
ans = min(ans, m);
printf( "%d\n", n-ans );
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int cnt = 0;
for (int i = 0; i < n; i++) {
int m;
cin >> m;
int h = 0;
while (m) {
int last = m % 10;
if (last == 4 || last == 7) {
h++;
}
m = m / 10;
}
if (h <= k) {
cnt++;
}
}
cout << cnt << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
const int INF = 1e9;
int n;
char s[maxn];
int P, M;
struct node {
int l, r;
int left, right;
} Tree[maxn];
int cur;
vector<int> stk;
void addNode(int x, bool flag) {
++cur;
Tree[cur].l = x;
if (!stk.empty()) {
int par = stk.back();
if (!Tree[par].left)
Tree[par].left = cur;
else {
assert(!Tree[par].right);
Tree[par].right = cur;
}
}
if (flag)
stk.push_back(cur);
else
Tree[cur].r = x;
}
void buildTree() {
for (int i = 0; i < n; ++i) {
if (s[i] == '(' || (s[i] != '?' && s[i] != ')'))
addNode(i, s[i] == '(');
else if (s[i] == ')') {
int v = stk.back();
Tree[v].r = i;
stk.pop_back();
}
}
}
long long dp[2][maxn][105];
bool mrk[2][maxn][105];
bool flag;
long long f(int type, int v, int op) {
if (!mrk[type][v][op]) {
mrk[type][v][op] = true;
if (!type) {
if (Tree[v].l == Tree[v].r)
return dp[type][v][op] = (!op) ? (s[Tree[v].l] - '0') : (-INF);
dp[type][v][op] = -INF;
int L = Tree[v].left;
int R = Tree[v].right;
if (op) {
for (int x = 0; x <= 1; ++x)
for (int y = 0; y <= 1; ++y)
for (int i = 0; i <= op - 1; ++i) {
if (abs(f(x, L, i)) == INF || abs(f(y, R, op - i - 1)) == INF)
continue;
if (flag)
dp[type][v][op] =
max(dp[type][v][op], f(x, L, i) - f(y, R, op - i - 1));
else
dp[type][v][op] =
max(dp[type][v][op], f(x, L, i) + f(y, R, op - i - 1));
}
}
for (int x = 0; x <= 1; ++x)
for (int y = 0; y <= 1; ++y)
for (int i = 0; i <= op; ++i) {
if (abs(f(x, L, i)) == INF || abs(f(y, R, op - i)) == INF) continue;
if (flag)
dp[type][v][op] =
max(dp[type][v][op], f(x, L, i) + f(y, R, op - i));
else
dp[type][v][op] =
max(dp[type][v][op], f(x, L, i) - f(y, R, op - i));
}
} else {
if (Tree[v].l == Tree[v].r)
return dp[type][v][op] = (!op) ? (s[Tree[v].l] - '0') : (INF);
dp[type][v][op] = INF;
int L = Tree[v].left;
int R = Tree[v].right;
if (op) {
for (int x = 0; x <= 1; ++x)
for (int y = 0; y <= 1; ++y)
for (int i = 0; i <= op - 1; ++i) {
if (abs(f(x, L, i)) == INF || abs(f(y, R, op - i - 1)) == INF)
continue;
if (flag)
dp[type][v][op] =
min(dp[type][v][op], f(x, L, i) - f(y, R, op - i - 1));
else
dp[type][v][op] =
min(dp[type][v][op], f(x, L, i) + f(y, R, op - i - 1));
}
}
for (int x = 0; x <= 1; ++x)
for (int y = 0; y <= 1; ++y)
for (int i = 0; i <= op; ++i) {
if (abs(f(x, L, i)) == INF || abs(f(y, R, op - i)) == INF) continue;
if (flag)
dp[type][v][op] =
min(dp[type][v][op], f(x, L, i) + f(y, R, op - i));
else
dp[type][v][op] =
min(dp[type][v][op], f(x, L, i) - f(y, R, op - i));
}
}
}
return dp[type][v][op];
}
long long solve() {
n = strlen(s);
if (n == 1) return s[0] - '0';
if (P > M) {
flag = true;
swap(P, M);
}
buildTree();
return f(0, 1, P);
}
int main() {
scanf(" %s %d %d", s, &P, &M);
printf("%lld\n", solve());
return 0;
}
| 11 | CPP |
n,m=map(int,input().split())
ans=0
for i in range(1,m+1):
for j in range(1,m+1):
if ( i*i %m + j*j % m) % m ==0:
ans+=(((n-i)//m+1)*((n-j)//m+1))
print(ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int j, n, m, ans;
string s[120];
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n - 1; i++) {
if (s[i] > s[i + 1]) {
for (j = 0; j < m; j++) {
if (s[i][j] > s[i + 1][j]) {
break;
}
}
for (int k = 0; k < n; k++) {
s[k].erase(j, 1);
}
ans++;
m--;
i = -1;
}
}
printf("%d\n", ans);
return 0;
}
| 9 | CPP |
h, w = map(int, input().split())
a = [input() for _ in range(h)]
s = sum([1 if a[i][j] == "#" else 0 for j in range(w) for i in range(h)])
print("Possible") if s == h + w - 1 else print("Impossible")
| 0 | PYTHON3 |
for i in range(int(input())):
i=input().split()
e=i[0]
k=int(i[1])
c=0
while c<k-1 and "0" not in e:
el=[int(i) for i in e]
e=int(e)+min(el)*max(el)
c+=1
e=str(e)
print("".join([str(i) for i in e]))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
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;
}
struct Segment {
int a, l;
bool operator<(const Segment &rhs) const { return a < rhs.a; }
} t[105];
int n;
int f[105][105][2];
int main() {
n = read();
for (int i = 1; i <= n; i++) {
t[i].a = read();
t[i].l = read();
}
sort(t + 1, t + n + 1);
t[0].a = -1145141919;
int ans = -1145141919;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= i; j++) {
for (int k = 0; k < 2; k++) {
int l = t[j].a + t[j].l * k;
int maxlen = -1145141919;
int x = 0, y = 0;
for (int o = i + 1; o <= n; o++) {
for (int p = 0; p < 2; p++) {
int l1 = t[o].a + t[o].l * p;
if (l1 > maxlen) {
maxlen = l1;
x = o;
y = p;
}
f[o][x][y] =
max(f[o][x][y], f[i][j][k] + min(t[o].l, l1 - l) + maxlen - l1);
}
}
}
}
}
for (int j = 0; j <= n; j++) {
for (int k = 0; k < 2; k++) ans = max(ans, f[n][j][k]);
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
const double EPS = 1e-9;
const int N = 1e6 + 5;
const long long mod = 1e9 + 7;
const int oo = 1e9;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int n, m;
char g[501][501];
bool vis[501][501];
vector<pair<char, pair<int, int>>> ans;
void dfs(int i, int j, bool p) {
vis[i][j] = 1;
ans.push_back({'B', {i, j}});
for (int k = 0; k <= 3; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == '.' &&
!vis[nx][ny]) {
dfs(nx, ny, 1);
}
}
if (p) {
ans.push_back({'D', {i, j}});
ans.push_back({'R', {i, j}});
}
}
int main() {
cin >> n >> m;
for (int i = 0; i <= n - 1; i++)
for (int j = 0; j <= m - 1; j++) cin >> g[i][j];
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= m - 1; j++) {
if (!vis[i][j] && g[i][j] == '.') dfs(i, j, 0);
}
}
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second.first + 1 << " "
<< ans[i].second.second + 1 << "\n";
}
}
| 10 | CPP |
for i in range(int(input())):
x,y,n=map(int,input().split())
a2=n%x
if(n-a2+y<=n):
print(n-a2+y)
else:
print(n-a2-x + y)
| 7 | PYTHON3 |
results = []
for _ in range(int(input())):
a, b = map(int, input().split())
if a % b:
results.append(str(b - a % b))
else:
results.append('0')
print('\n'.join(results))
| 10 | PYTHON3 |
n = int(input())
num = list(str(n))
flag = 0
if len(num)==1:
if num[0]=='9':
flag = 1
print(9)
if flag==0:
if num[0]=='9':
x=1
else:
x=0
for i in range(x,len(num)):
if int(num[i])>=5:
num[i]=str(9-int(num[i]))
print(''.join(num)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void show(T a, int n) {
for (int i = 0; i < n; ++i) cout << a[i] << ' ';
cout << endl;
}
template <class T>
void show(T a, int r, int l) {
for (int i = 0; i < r; ++i) show(a[i], l);
cout << endl;
}
const int N = 1 << 10;
const int M = 5000;
const int oo = 10000 * 10000 * 10;
struct Point {
double x, y;
} p[N];
struct Cir {
double r;
double x, y;
} cir[N];
bool cmp(Cir a, Cir b) { return a.r < b.r; }
inline double dis2(double x, double y, double xx, double yy) {
return (x - xx) * (x - xx) + (y - yy) * (y - yy);
}
int n, m;
int dex[N];
bool visited[N];
vector<int> e[N];
int d[N][N];
int que[N];
void bfs(int s, int d[]) {
int i, j, k, u, v;
memset(visited, 0, sizeof(visited));
int head = 0, tail = 0;
for (i = 0; i < m + 1; ++i) d[i] = oo;
que[tail++] = s;
d[s] = 0;
visited[s] = true;
while (head != tail) {
u = que[head++];
for (i = 0; i < e[u].size(); ++i) {
int v = e[u][i];
if (!visited[v]) {
d[v] = d[u] + 1;
visited[v] = true;
que[tail++] = v;
}
}
}
}
void solve() {
int i, j, k;
sort(cir, cir + m, cmp);
memset(visited, 0, sizeof(visited));
for (i = 0; i < m; ++i) {
for (j = i + 1; j < m; ++j) {
if (dis2(cir[i].x, cir[i].y, cir[j].x, cir[j].y) < cir[j].r * cir[j].r) {
visited[i] = true;
e[i].push_back(j);
e[j].push_back(i);
break;
}
}
}
for (i = 0; i < m; ++i)
if (!visited[i]) {
e[i].push_back(m);
e[m].push_back(i);
}
for (i = 0; i < m + 1; ++i) {
bfs(i, d[i]);
}
for (i = 0; i < n; ++i) {
dex[i] = m;
for (j = 0; j < m; ++j) {
if (dis2(p[i].x, p[i].y, cir[j].x, cir[j].y) < cir[j].r * cir[j].r) {
dex[i] = j;
break;
}
}
}
}
int main() {
int i, j, cas = 0;
int q;
scanf("%d %d %d", &n, &m, &q);
for (i = 0; i < n; ++i) scanf("%lf %lf", &p[i].x, &p[i].y);
for (i = 0; i < m; ++i) scanf("%lf %lf %lf", &cir[i].r, &cir[i].x, &cir[i].y);
solve();
while (q--) {
int u, v;
scanf("%d %d", &u, &v);
u = dex[u - 1];
v = dex[v - 1];
printf("%d\n", d[u][v]);
}
return 0;
}
| 10 | CPP |
n,p=map(int, input().split())
l=list(map(int, input().split()))
if n==1:
print(max(l[0],p-l[0]))
else:
l=list(set(l))
l.sort()
x=len(l)
if x==1:
print(max(l[0],p-l[0]))
else:
t=(l[1]-l[0])/2
for i in range(2,x):
if (l[i]-l[i-1])/2>t:
t=(l[i]-l[i-1])/2
if t>max(l[0],p-l[x-1]):
print(t)
else:
print(max(l[0],p-l[x-1]))
| 8 | PYTHON3 |
n, k = map(int, input().split())
if k <= (n+1)//2:
print((2*k) - 1)
else:
print((k-((n+1)//2))*2)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T INF(T a) {
a = numeric_limits<T>::max();
return a;
}
template <typename T>
T mINF(T a) {
a = numeric_limits<T>::min();
return a;
}
class node {
public:
int st;
list<int> nb;
};
int main() {
int n, a1, a2;
cin >> n;
vector<node> g(n);
int maxs = mINF(int());
for (int i = 0; i < (n); i++) {
scanf("%d", &a1);
g[i].st = a1;
maxs = max(a1, maxs);
}
int nm = 0;
int nm1 = 0;
for (int i = 0; i < (n); i++) {
if (g[i].st == maxs) nm++;
if (g[i].st == maxs - 1) nm1++;
}
for (int i = 0; i < (n - 1); i++) {
scanf("%d %d", &a1, &a2);
a1--, a2--;
g[a1].nb.push_back(a2);
g[a2].nb.push_back(a1);
}
int add = 2;
for (int i = 0; i < (n); i++) {
int nmcur = 0, nm1cur = 0;
bool unicomax = false;
if (g[i].st == maxs)
nmcur++, unicomax = true;
else if (g[i].st == maxs - 1)
nm1cur++;
for (__typeof((g[i].nb).begin()) it = (g[i].nb).begin();
it != (g[i].nb).end(); it++) {
if (g[*it].st == maxs) {
nmcur++;
} else if (g[*it].st == maxs - 1) {
nm1cur++;
}
}
if (nm == nmcur) {
if (unicomax and nm == 1 and nm1 == nm1cur) {
add = 0;
} else {
add = min(add, 1);
}
} else {
add = min(add, 2);
}
}
cout << maxs + add << endl;
return 0;
}
| 9 | CPP |
import math
def list_primes_upto(upto :int):
pcl = [True for x in range(upto+1)]
primes_list = []
if upto >= 2: primes_list.append(2)
if upto >= 3: primes_list.append(3)
for i in range(6, upto + 2, 6):
if i-1 <= upto and pcl[i-1]:
primes_list.append(i-1)
for k in range((i-1)**2, upto+1, (i-1)*2):
pcl[k] = False
if i+1 <= upto and pcl[i+1]:
primes_list.append(i+1)
for k in range((i+1)**2, upto+1, (i+1)*2):
pcl[k] = False
return primes_list
t = int(input())
tests = []
for q in range(t):
tests.append([int(x) for x in input().split()])
check_limit = 0
for a, b in tests:
if a-b == 1:
check_limit = max(check_limit, a+b)
primes = list_primes_upto(int(math.sqrt(check_limit))+1)
for a, b in tests:
yes = True
if a-b != 1:
yes = False
if yes:
apb = a+b
is_apb_prime = True
for p in primes:
if not is_apb_prime or p >= apb:
break
is_apb_prime = is_apb_prime and (apb % p != 0)
yes = yes and is_apb_prime
print("YES" if yes else "NO")
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int t, n, m;
int nxt[1000000];
int cnt[1000000];
int q[1000000];
int b[1000000];
int dp[2][1000000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> m;
string s;
for (int i = 0; i < n; ++i) {
cin >> s;
for (int j = 0; j < m; ++j) {
b[i * m + j] = s[j] == '0';
}
}
for (int i = 0; i < n; ++i) {
cin >> s;
for (int j = 0; j < m; ++j) {
int nx = -1;
if (s[j] == 'U') {
nx = (i - 1) * m + j;
}
if (s[j] == 'D') {
nx = (i + 1) * m + j;
}
if (s[j] == 'L') {
nx = i * m + j - 1;
}
if (s[j] == 'R') {
nx = i * m + j + 1;
}
nxt[i * m + j] = nx;
}
}
int sz = n * m;
for (int i = 0; i < sz; ++i) {
dp[0][i] = nxt[i];
}
for (int i = 0; i < 20; ++i) {
for (int j = 0; j < sz; ++j) {
dp[1][j] = dp[0][dp[0][j]];
}
for (int j = 0; j < sz; ++j) {
dp[0][j] = dp[1][j];
}
}
fill(cnt, cnt + sz, 0);
fill(q, q + sz, 0);
for (int i = 0; i < sz; ++i) {
++cnt[dp[0][i]];
q[dp[0][i]] += b[i];
}
int ans1 = 0, ans2 = 0;
for (int i = 0; i < sz; ++i) {
ans1 += (int)(cnt[i] > 0);
ans2 += (int)(q[i] > 0);
}
cout << ans1 << ' ' << ans2 << endl;
}
return 0;
}
| 12 | CPP |
s = input()
res = set()
for i in range(len(s)):
l = i + len(s)
sc = s[i:l] + s[0:i]
res.add(sc)
print(len(res)) | 7 | PYTHON3 |
m, r = map(int, input().split())
answer = 0
from math import sqrt
def fef(vev):
if vev == 0:
return 0
if vev <= 1:
return 2 * r + sqrt(2) * r
curr = 2 * r + sqrt(2) * r
curr += (vev - 1) * (2 * r + sqrt(2) *2 * r)
curr += r * (vev - 1) * (vev - 2)
return curr
for i in range(m):
answer += fef(i)
answer += fef(m - 1- i)
answer += 2 * r
print(answer / (m ** 2))
| 8 | PYTHON3 |
/*long long or int&the MAX_INT or MIN_INT*/
/*the size of a[]&inline*/
/*xx 396*/
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
template<class T>inline void checkmin(T &a,const T &b){if(b<a)a=b;}
template<class T>inline void checkmax(T &a,const T &b){if(b>a)a=b;}
vector<vector<int> >a;
int n,m,k=0,tot=0;
void print(vector<vector<int> >v){
cout<<"Yes\n";
for(int i=0;i<(int)v[0].size();i++)cout<<v[0][i]<<" ";
}
void solve1(){
for(int i=1;i<n;i++)for(int j=0;j<m;j++)a[i][j]=a[0][j];
print(a);
}
bool ok(int id1,int id2,int id3,vector<vector<int> >&v){
v[0][id2]=v[k][id2];v[k][id3]=v[0][id3];
vector<int>now;
for(int i=1;i<n;i++){
int cnt=0;
for(int j=0;j<m;j++){
if(j==id1)continue;
if(v[0][j]!=v[i][j])cnt++;
}
if(cnt>2)return false;
if(cnt==2)now.pb(a[i][id1]);
}
bool flag=true;
if((int)now.size()==0)return true;
int val=now[0];
for(int i=0;i<(int)now.size();i++)if(val!=now[i])flag=false;
v[0][id1]=val;
// if(flag==true)cout<<id1<<" "<<id2<<" "<<id3<<" "<<val<<endl;
if(!flag)return false;
else return true;
}
void solve2(){
int p[3],id=0;
for(int i=0;i<m;i++)if(a[0][i]!=a[k][i])p[id++]=i;
for(int i=0;i<3;i++)for(int j=0;j<3;j++)if(i!=j){
int k=3-i-j;
vector<vector<int> >b=a;
if(ok(p[k],p[i],p[j],b)){
print(b);
return;
}
}
printf("No\n");
}
bool ok(int p1,int p2,int p3,int p4,vector<vector<int> >&v){
// cout<<p1<<" "<<p2<<" "<<p3<<" "<<p4<<endl;
v[k][p1]=v[0][p1];v[k][p2]=v[0][p2];v[0][p3]=v[k][p3];v[0][p4]=v[k][p4];
// for(int i=0;i<m;i++)cout<<v[0][i]<<" ";cout<<endl;
for(int i=0;i<n;i++){
int cnt=0;
for(int j=0;j<m;j++){
if(v[i][j]!=v[0][j])cnt++;
}
if(cnt>2)return false;
}
return true;
}
void solve3(){
int p[4],id=0;
for(int i=0;i<m;i++)if(a[0][i]!=a[k][i])p[id++]=i;
// for(int i=0;i<4;i++)cout<<p[i]<<" ";cout<<endl;
for(int i=0;i<4;i++)for(int j=i+1;j<4;j++)for(int k=0;k<4;k++)if(i!=k&&j!=k){
vector<vector<int> >b=a;
if(ok(p[i],p[j],p[k],p[6-i-j-k],b)){
print(b);
return;
}
}
printf("No\n");
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
vector<int>v;
for(int j=0;j<m;j++){
int x;cin>>x;v.pb(x);
}
a.pb(v);
}
for(int i=1;i<n;i++){
int cnt=0;
for(int j=0;j<m;j++)if(a[i][j]!=a[0][j])cnt++;
if(cnt>4){
cout<<"No\n";
return 0;
}
if(cnt>tot){
tot=cnt;
k=i;
}
}
if(tot<=2)solve1();
if(tot==3)solve2();
if(tot==4)solve3();
return 0;
} | 11 | CPP |
n=int(input())
ar=[]
for k in range(n):
a,b=map(int,input().split())
ar.append([a,b])
ar.sort(key=lambda x: x[0])
flag=False
for i in range(n-1):
if ar[i][0]<ar[i+1][0] and ar[i][1]>ar[i+1][1]:
flag=True
break
if flag==True:
print("Happy Alex")
else:
print("Poor Alex") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long a[30], n;
vector<long long> xs, ys;
long long calc(long long x) {
int r = ys.size() - 1;
long long ret = 0, s;
for (int i = 0; i < xs.size(); i++) {
s = xs[i];
if (s > x) break;
while (ys[r] > x / s) r--;
ret = ret + r + 1;
}
return ret;
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n / 2; i++) swap(a[i], a[n - i + 1]);
long long mx = 1e18, s;
xs.clear();
ys.clear();
xs.push_back(1);
ys.push_back(1);
for (int t = 1; t <= n / 2; t++) {
s = a[t];
for (int i = 0; i < xs.size(); i++)
if (s <= mx / xs[i]) xs.push_back(xs[i] * s);
}
for (int t = n / 2 + 1; t <= n; t++) {
s = a[t];
for (int i = 0; i < ys.size(); i++)
if (s <= mx / ys[i]) ys.push_back(ys[i] * s);
}
sort(xs.begin(), xs.end());
sort(ys.begin(), ys.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
long long l = 1, r = 1e18;
long long k;
scanf("%lld", &k);
while (l <= r) {
long long mid = (l + r) / 2;
if (calc(mid) < k)
l = mid + 1;
else
r = mid - 1;
}
printf("%lld\n", l);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a[10100], v[1001000];
int p[10100], nx[10100], ans = 1;
int solve(int x, int y, int z) {
int t = x;
while (t <= y) t = nx[t];
if (t <= n) return solve(y, t, z + 1);
return z;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
p[i] = v[a[i]];
v[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
nx[i] = n + 1;
for (int j = i + 1; j <= n; j++)
if (a[j] == a[i]) {
nx[i] = j;
break;
}
}
for (int i = 1; i <= n; i++)
if (!p[i])
for (int j = i + 1; j <= n; j++) {
if (p[j] > i) continue;
ans = max(ans, solve(i, j, 2));
}
cout << ans << endl;
}
| 9 | CPP |
s=input()
for _ in range(int(input())):
o=input().split()
a,b=map(int,o[1:3]);b+=1
c=o[0][2]
if c=='p':s=s[:a]+o[3]+s[b:]
elif c=='i':print(s[a:b])
elif c=='v':s=s[:a]+s[a:b][::-1]+s[b:]
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, i;
cin >> n;
vector<long long> v, p, a;
for (i = 0; i < n; i++) {
long long temp;
cin >> temp;
v.push_back(temp);
}
long long cur = v[0];
long long cnt = 1;
for (i = 1; i < n; i++) {
if (v[i] == cur)
cnt++;
else {
p.push_back(cnt);
cnt = 1;
cur = v[i];
}
}
if (cur > 0) p.push_back(cnt);
long long sum = 0;
for (i = 0; i < int(p.size()); i++) {
if (sum + p[i] <= (n / 2)) {
a.push_back(p[i]);
sum += p[i];
} else
break;
}
if (a.size() < 3)
cout << 0 << " " << 0 << " " << 0 << endl;
else {
long long g = a[0];
long long n1 = a.size();
long long b = 0;
long long ind;
for (i = n1 - 1; i >= 1; i--) {
if (b + a[i] <= g)
b += a[i];
else {
b += a[i];
ind = i - 1;
break;
}
}
if (b <= g)
cout << 0 << " " << 0 << " " << 0 << endl;
else {
long long s = 0;
for (i = 1; i <= ind; i++) s += a[i];
if (s <= g)
cout << 0 << " " << 0 << " " << 0 << endl;
else
cout << g << " " << s << " " << b << endl;
}
}
}
return 0;
}
| 7 | CPP |
# -*- coding: utf-8 -*-
"""
Created on Wed May 23 04:45:33 2018
@author: USER
"""
a=int(input())
b=(a//2)*(a//2+1)
c=(a*(a+1))//2
d=c-b
f=b-d
print(f) | 7 | PYTHON3 |
// Author:- Pratik Kinger
// Birla Institute of Technology
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define all(a) (a).begin(), (a).end()
#define fi first
#define si second
#define sz(x) (ll) x.size()
#define endl '\n'
#define mod 1000000007
#define mset(m, v) memset(m, v, sizeof(m))
using namespace std;
const ll MOD = 1000000000 + 7;
//------------------------------CODE--HERE--------------------------
//----------global--variables-------------------
//------------end-------------------------------
bool check_palind(string s)
{
int n = s.length();
for(int i = 0; i<n/2; i++)
{
if(s[i] != s[n - i - 1])
{
return false;
}
if(s[i] == '?' || s[n - i - 1] == '?')
return false;
}
return true;
}
void solve()
{
int a, b;
cin>>a>>b;
string s;
cin>>s;
int n = a + b;
for(int i = 0; i<n; i++)
{
if(s[i] == '?' && s[n - i - 1] != '?')
{
s[i] = s[n - i - 1];
}
}
// cout<<s<<endl;
int needed_0 = 0, needed_1 = 0;
int coun_0 = 0, coun_1 = 0, coun_q = 0;
for(int i = 0; i<n; i++)
{
if(s[i] == '0')
coun_0++;
if(s[i] == '1')
coun_1++;
}
coun_q = n - coun_0 - coun_1;
needed_0 = a - coun_0;
needed_1 = b - coun_1;
if(needed_0 < 0 || needed_1 < 0)
{
cout<<-1<<endl;
return;
}
if(n%2 == 1 && s[n/2] == '?')
{
if(needed_0%2 == 0 && needed_1%2 == 0)
{
cout<<-1<<endl;
return;
}
else if(needed_0%2 == 1)
{
s[n/2] = '0';
needed_0--;
}
else if(needed_1%2 == 1)
{
s[n/2] = '1';
needed_1--;
}
}
if(needed_0%2 != 0 || needed_1%2 != 0)
{
cout<<-1<<endl;
return;
}
for(int i = 0; i<n; i++)
{
if(s[i] == '?')
{
if(needed_0 > 1)
{
s[i] = '0';
s[n - i - 1] = '0';
needed_0 -= 2;
}
else if(needed_1 > 1)
{
s[i] = '1';
s[n - i - 1] = '1';
needed_1 -= 2;
}
}
}
if(check_palind(s))
cout<<s<<endl;
else
cout<<-1<<endl;
}
//------------------------------
int main()
{
//FAST INPUT OUTPUT
FIO;
//INPUT OUTPUT
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
} | 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> arr[150];
long long n, k, dp[105][105], ans = 0;
string s;
long long solve(int indx, long long len) {
if (!len) return 1;
if (indx == n) return 1e18;
if (dp[indx][len] != -1) return dp[indx][len];
bool vis[150] = {};
long long ret = 0;
for (int i = indx; i < n; i++) {
if (!vis[s[i]]) {
vis[s[i]] = 1;
long long temp = solve(i + 1, len - 1);
if (temp >= 1e18)
break;
else
ret += temp;
}
}
return dp[indx][len] = ret;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> k >> s;
for (int i = 0; i < n; i++) {
arr[s[i]].push_back(i);
}
for (int i = n; i >= 0 && k > 0; i--) {
long long tmp = solve(0, i);
ans += (n - i) * min(k, tmp);
k -= min(k, tmp);
}
if (!k)
cout << ans << '\n';
else
cout << -1 << '\n';
return 0;
}
| 11 | CPP |