solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
n = int(input())
db = []
db_count = {}
for i in range(n):
name = input()
db.append(name)
db_count[name] = 0
for name in db:
if db_count[name] == 0:
print('OK')
else:
print(name+str(db_count[name]))
db_count[name] += 1 | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int h;
cin >> h;
int A[100005];
int f = 0;
cin >> A[0];
for (int i = 1; i <= h; i++) {
cin >> A[i];
if (A[i] > 1 && A[i - 1] > 1) f = 1;
}
if (!f)
cout << "perfect\n";
else {
cout << "ambiguous\n";
int p = 0, t;
for (int i = 0; i <= h; i++) {
t = A[i];
while (t--) cout << p << " ";
p += A[i];
}
cout << endl;
f = 1;
p = 1;
cout << "0 ";
for (int i = 1; i <= h; i++) {
t = A[i];
if (f && A[i - 1] > 1 && A[i] > 1) {
t = A[i];
t--;
cout << p - 1 << " ";
while (t--) cout << p << " ";
p += A[i];
f = 0;
} else {
t = A[i];
while (t--) cout << p << " ";
p += A[i];
}
}
cout << endl;
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b;
cin >> n >> a >> b;
cout << n - max(a + 1, n - b) + 1;
return 0;
}
| 7 | CPP |
def main():
d={}
nums=[None]*31
for i in range(31):
nums[i]=2**i
d[nums[i]]=0
n,q=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
d[a[i]]+=1
for i in range(31):
if d[nums[i]]==0:
del d[nums[i]]
keys=sorted(list(d.keys()))[::-1]
leng=len(keys)
ans=[0]*q
for i in range(q):
val=int(input())
for j in range(leng):
if keys[j]<=val:
coin=min(d[keys[j]],val//keys[j])
ans[i]+=coin
val-=keys[j]*coin
if val==0:
break
else:
ans[i]=-1
print('\n'.join(list(map(str,ans))))
main() | 10 | PYTHON3 |
x, y, z = list(int(a) for a in input().split())
ox = x%z
oy = y%z
x //= z
y //= z
if ox < oy:
y += (ox+oy)//z
else:
x += (ox+oy)//z
print(x+y, 0 if ox+oy < z else z-max(ox, oy)) | 7 | PYTHON3 |
n,x,y=map(int,input().split(" "))
c=list()
for i in range(0,n):
a,b=map(int,input().split(" "))
if (x-a)!=0:
m=(y-b)/(x-a)
elif (x-a)==0 and (y-b)==0:
m=1
elif (x-a)==0 and (y-b)!=0:
m=100000
c.append(m)
c=set(c)
print(len(c)) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const long long inf = 1e9;
const long long mod = 1e9 + 7;
const int maxn = 1e5 + 10;
char str[maxn], s[maxn];
void print(int i) {
if (i == 0)
printf("tokitsukaze");
else if (i == 1)
printf("quailty");
else
printf("once again");
exit(0);
}
int main() {
int n, siz, len, i, j;
bool vis;
scanf("%d%d%s", &n, &siz, str);
len = strlen(str);
vis = 1, j = -1;
for (i = 0; i < len; i++)
if (str[i] == '0') {
if (j == -1)
j = i;
else if (i >= j + siz)
vis = 0;
}
j = i;
if (vis) print(0);
vis = 1, j = -1;
for (i = 0; i < len; i++)
if (str[i] == '1') {
if (j == -1)
j = i;
else if (i >= j + siz)
vis = 0;
}
j = i;
if (vis) print(0);
if (n <= siz + 1) print(0);
for (i = 0; i < len; i++)
if (str[i] != str[0]) break;
for (j = len - 1; j >= 0; j--)
if (str[j] != str[len - 1]) break;
if (j - 1 + 1 > siz || (len - 2) - i + 1 > siz || (i - 1) - 0 + 1 > siz ||
(len - 1) - (j + 1) + 1 > siz)
print(2);
else
print(1);
return 0;
}
| 9 | CPP |
for _ in range(int(input())):
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
x,y=min(a),min(b)
res=0
for i in range(n):
v1,v2=a[i]-x,b[i]-y
temp=min(v1,v2)
res+=temp
v1-=temp
v2-=temp
res+=v1+v2
print(res) | 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using ll = long long;
int mod = 1000000000+7;
int main(){
int d;
cin>>d;
int c[26];
rep(i,26)cin>>c[i];
//int lastd[26]={};
int s[d][26];
rep(i,d){
rep(j,26){
cin>>s[i][j];
}
}
rep(i,d){
cout<<1;
if(i!=d-1)cout<<endl;
}
return 0;
}
| 0 | CPP |
t=int(input())
for i in range(t):
n=input()
cc,cv=0,0
s=''
k=len(n)*2
m=k//2
for j in range(len(n)):
if(n[j]=='0'):
cc+=1
else:
cv+=1
if(cc==0):
s+='1'*k
elif(cv==0):
s+='0'*k
else:
while(m>0):
s+='1'
s+='0'
m-=1
print(s) | 8 | PYTHON3 |
a=int(input())
for i in range(a):
target=input()
if len(target) > 10:
print(target[0]+str(len(target)-2)+target[-1])
else:
print(target) | 7 | PYTHON3 |
str=input()
x=len(set(str.split()))
print(4-x) | 7 | PYTHON3 |
n = int(input())
a = list(map(int, input().split()))
ans = [1]
num = a[0]
for i in range(1, n):
if a[0] >= a[i]*2:
ans.append(i+1)
num += a[i]
if num > sum(a) / 2:
print(len(ans))
print(' '.join(str(ans[i]) for i in range(len(ans))))
else:
print(0)
| 7 | PYTHON3 |
#122A
n=int(input())
a=[]
p=0
def luckynum(i):
e=i
t=0
c=0
while(i!=0):
d=i%10
i=i//10
t+=1
if(d==4 or d==7):
c+=1
if(t==c):
return e
else:
return 1
for i in range(1,n+1):
k=luckynum(i)
if(k!=1):
a.append(k)
for i in range(len(a)):
if(n%a[i]==0):
p+=1
if(p>0):
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int n, i, k, t, temp, flag;
int sum;
int total;
int a[10000];
int x[10000];
scanf("%d", &n);
total = 0;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
total = total + a[i];
}
sum = a[1];
int target;
target = total / 2;
target = target + 1;
t = 1;
flag = 0;
k = 0;
if (sum >= target) {
flag = 4;
printf("1\n");
printf("1\n");
goto z;
}
k = 1;
t = 1;
x[t] = 1;
t++;
for (i = 2; i <= n; i++) {
temp = 2 * a[i];
if (a[1] >= temp) {
sum = sum + a[i];
k++;
x[t] = i;
t++;
}
if (sum >= target) {
flag = 1;
break;
}
}
if (flag == 1) {
printf("%d\n", k);
for (i = 1; i < t; i++) {
printf("%d ", x[i]);
}
}
if (flag == 0) {
printf("0\n");
}
z:
return 0;
}
| 7 | CPP |
S = input()
print('Yes' if S[0] == S[2] else 'No')
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
char s[1000050];
bool bo[1000050];
int main() {
scanf(" %s", s);
int n = strlen(s);
int ans = 0;
for (int i = 0; i < n; ++i) {
if (s[(i - 1 + n) % n] != s[i] && s[i] == s[(i + 1) % n]) {
ans++;
}
}
int flag = 0;
for (int i = 0; i < n; ++i) {
if (s[(i - 1 + n) % n] != s[i] && s[i] != s[(i + 1) % n] &&
s[(i + 2) % n] != s[(i + 1) % n]) {
if (i == 0) flag = 1;
if (flag == 1 && i == n - 1) continue;
ans++;
i++;
}
}
printf("%d\n", ans);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 998244353;
vector<int> g[N];
long long fact[N];
long long dfs(int u, int p, bool circle);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 2; i <= n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
fact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = (i * fact[i - 1]) % mod;
}
long long ans = dfs(1, 0, true) * n;
ans %= mod;
cout << ans << endl;
}
long long dfs(int u, int p, bool circle) {
int ch = 0;
long long ans = 1;
for (auto &v : g[u]) {
if (v != p) {
ans *= dfs(v, u, false);
ans %= mod;
++ch;
}
}
ans *= (circle ? fact[ch] : fact[ch + 1]);
ans %= mod;
return ans;
}
| 8 | CPP |
s=input()
s=s.split('WUB')
for i in s:
if len(i)!=0:
print(i,end=' ') | 7 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
class BipartiteMatcher {
vector<vector<long long int>> G;
vector<long long int> L, R, Viz;
public:
void init(long long int n, long long int m) {
G.clear(), L.clear(), R.clear(), Viz.clear();
G.resize(n), L.resize(n, -1), R.resize(m, -1), Viz.resize(n, 0);
}
void AddEdge(long long int a, long long int b) { G[a].push_back(b); }
bool Match(long long int node) {
if (Viz[node]) return 0;
Viz[node] = 1;
for (auto vec : G[node]) {
if (R[vec] == -1 || Match(R[vec])) {
L[node] = vec;
R[vec] = node;
return 1;
}
}
return 0;
}
long long int Solve() {
bool ok = 1;
while (ok) {
ok = 0;
fill(Viz.begin(), Viz.end(), 0);
for (long long int i = 0; i < L.size(); i++) {
if (L[i] == -1) ok |= Match(i);
}
}
long long int ret = 0;
for (long long int i = 0; i < L.size(); i++) {
ret += (L[i] != -1);
}
return ret;
}
};
void solve() {
long long int n, m;
cin >> n >> m;
using T = tuple<long long int, long long int, long long int>;
vector<T> edge(m);
for (long long int i = 0; i < m; i++) {
long long int u, v, d;
cin >> u >> v >> d;
u--;
v--;
edge[i] = {d, u, v};
}
sort((edge).begin(), (edge).end());
long long int low = 0;
long long int high = m + 1;
while (high - low > 1) {
long long int mid = (high + low) / 2;
BipartiteMatcher B;
B.init(n, n);
for (long long int i = 0; i < mid; i++) {
long long int u = get<1>(edge[i]);
long long int v = get<2>(edge[i]);
B.AddEdge(u, v);
}
long long int cnt = B.Solve();
if (cnt == n) {
high = mid;
} else {
low = mid;
}
}
if (low >= m) {
cout << -1 << '\n';
return;
}
cout << get<0>(edge[low]) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
struct Node {
char type;
long long idx;
long long p;
vector<long long> chd;
long long s;
long long on;
long long r;
};
long long dfs(Node& node, vector<Node>& nls) {
node.on = 1;
if (node.type == '*') {
node.s = 1;
return 1;
}
if (node.type == 'P') {
long long sum = 0;
for (auto c : node.chd) {
sum += dfs(nls[c], nls);
}
node.s = sum;
return sum;
} else {
long long mmin = 80001;
for (auto c : node.chd) {
mmin = min(mmin, dfs(nls[c], nls));
}
int find = 0;
for (auto c : node.chd) {
if (find == 0 && nls[c].s == mmin) {
find = 1;
nls[c].on = 1;
} else {
nls[c].on = 0;
}
}
node.s = mmin;
return mmin;
}
}
void dfs2(Node& node, vector<Node>& nls, long long v) {
if (node.on == 0) {
return;
}
if (node.type == '*') {
node.r = v;
return;
}
for (auto c : node.chd) {
dfs2(nls[c], nls, v);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
vector<Node> nls;
nls.push_back(Node());
long long r;
cin >> r;
char c;
int n = 0;
int cnt = 1;
long long par = 0;
do {
cin >> c;
if (c == '(') {
++par;
Node node;
node.idx = cnt;
node.r = 0;
node.p = n;
nls.push_back(node);
nls[n].chd.push_back(cnt);
n = cnt;
++cnt;
} else if (c == ')') {
--par;
n = nls[n].p;
} else if (c == 'P') {
nls[n].type = c;
} else if (c == 'S') {
nls[n].type = c;
} else if (c == '*') {
Node node;
node.idx = cnt;
node.r = 0;
node.p = n;
node.type = c;
nls.push_back(node);
nls[n].chd.push_back(cnt);
++cnt;
}
} while (par);
long long sum = dfs(nls[0], nls);
dfs2(nls[0], nls, r * sum);
cout << "REVOLTING ";
for (int j = 1; j < cnt; ++j) {
if (nls[j].type == '*') {
cout << nls[j].r << ' ';
}
}
cout << "\n";
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
const int N=505;
int ans[N][N];
int main()
{
int n;
scanf("%i",&n);
int l;
for(l=0;;l++)
{
vector<int> v[2];
for(int i=0;i<n;i++) v[i>>l&1].pb(i+1);
if(v[1].empty()) break;
for(int i:v[0]) for(int j:v[1]) ans[min(i,j)][max(i,j)]=l+1;
}
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++) printf("%i ",ans[i][j]);
printf("\n");
}
return 0;
} | 0 | CPP |
#include<bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;
struct mf{
struct edg{ int pos, cap, rev; };
vector<edg> gph[MAXN];
void clear(){ for(int i=0; i<MAXN; i++) gph[i].clear(); }
void add_edge(int s, int e, int x){
gph[s].push_back({e, x, (int)gph[e].size()});
gph[e].push_back({s, 0, (int)gph[s].size()-1});
}
int dis[MAXN], pnt[MAXN];
bool bfs(int src, int sink){
memset(dis, 0, sizeof(dis));
memset(pnt, 0, sizeof(pnt));
queue<int> que;
que.push(src);
dis[src] = 1;
while(!que.empty()){
int x = que.front();
que.pop();
for(auto &e : gph[x]){
if(e.cap > 0 && !dis[e.pos]){
dis[e.pos] = dis[x] + 1;
que.push(e.pos);
}
}
}
return dis[sink] > 0;
}
int dfs(int x, int sink, int f){
if(x == sink) return f;
for(; pnt[x] < gph[x].size(); pnt[x]++){
edg e = gph[x][pnt[x]];
if(e.cap > 0 && dis[e.pos] == dis[x] + 1){
int w = dfs(e.pos, sink, min(f, e.cap));
if(w){
gph[x][pnt[x]].cap -= w;
gph[e.pos][e.rev].cap += w;
return w;
}
}
}
return 0;
}
lint match(int src, int sink){
lint ret = 0;
while(bfs(src, sink)){
int r;
while((r = dfs(src, sink, 2e9))) ret += r;
}
return ret;
}
}mf;
struct disj{
int pa[MAXN];
void init(int n){
iota(pa, pa + n + 1, 0);
}
int find(int x){
return pa[x] = (pa[x] == x ? x : find(pa[x]));
}
bool uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
pa[q] = p; return 1;
}
}AD, BD;
int n, A[MAXN], B[MAXN];
int main(){
scanf("%d",&n);
AD.init(n);
BD.init(n);
for(int i=0; i<n; i++){
scanf("%d",&A[i]);
AD.uni(i, A[i]);
}
for(int i=0; i<n; i++){
scanf("%d",&B[i]);
BD.uni(i, B[i]);
}
for(int i=0; i<n; i++){
if(A[i] == i){
mf.add_edge(2 * n, i, 1e9);
}
if(B[i] == i){
mf.add_edge(i + n, 2 * n + 1, 1e9);
}
if(A[i] == B[i]){
int ca = AD.find(i);
int cb = BD.find(i);
mf.add_edge(cb + n, ca, 1);
}
int ca = AD.find(i);
int cb = BD.find(i);
mf.add_edge(ca, cb + n, 1);
}
cout << -mf.match(2 * n, 2 * n + 1) + n << endl;
}
| 0 | CPP |
x=int(input())
a=list(map(int,input().split()))
y=max(a)
counter=0
for i in a:
if i!=y:
counter+=y-i
print(counter) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 7;
long long sz[maxn], F[maxn], G[maxn], n, ans = 0;
vector<pair<long long, long long> > g[maxn];
bool l[maxn];
bool check(long long w) {
while (w != 0) {
if (w % 10 != 4 && w % 10 != 7) return 0;
w = w / 10;
}
return 1;
}
void cal_sz(long long u, long long p) {
sz[u] = 1;
for (auto v : g[u]) {
if (v.first == p) continue;
cal_sz(v.first, u);
sz[u] += sz[v.first];
}
}
void dfs(long long u, long long p) {
long long ll = 0;
for (auto v : g[u]) {
if (v.first == p) continue;
ll++;
dfs(v.first, u);
if (v.second == 0) {
F[u] += F[v.first];
} else
F[u] += sz[v.first];
}
}
void dfs1(long long u, long long p) {
for (auto v : g[u]) {
if (v.first == p) continue;
if (!v.second)
G[v.first] = G[u] + F[u] - F[v.first];
else
G[v.first] = n - sz[v.first];
dfs1(v.first, u);
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long u, v, w, i = 1; i < n; i++) {
cin >> u >> v >> w;
w = check(w);
g[u].push_back(pair<long long, long long>(v, w));
g[v].push_back(pair<long long, long long>(u, w));
}
cal_sz(1, 1);
dfs(1, 1);
dfs1(1, 1);
for (long long i = 1; i <= n; i++) {
ans += F[i] * (F[i] - 1) + G[i] * (G[i] - 1) + 2 * F[i] * G[i];
}
cout << ans;
}
| 9 | CPP |
t = int(input())
tasks = []
for i in range(t):
tasks.append(list(map(int, input().split())))
for i in range(t):
n, x, y = tasks[i]
if n == 2:
print(x, y)
else:
#print(x, y) #DEBUG
dividers = [j for j in range(1, y - x + 1) if (y - x) % j == 0] #проверенно
j = 0
while ((y - x) // dividers[j]) - 1 > (n - 2): j += 1
step = dividers[j]
answer = []
#answer.append(x)
#answer.append(y)
#n -= 2
#z = x + step
z = x
while z <= y:
answer.append(z)
n -= 1
z += step
z = x - step
while z > 0 and n > 0:
answer.append(z)
n -= 1
z -= step
z = y + step
while n > 0:
answer.append(z)
n -= 1
z += step
print(' '.join(map(str, answer)))
| 9 | PYTHON3 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int main() {
unsigned long long n;
int q;
cin >> n >> q;
for (int i = 0; i < q; i++) {
unsigned long long x;
cin >> x;
x--;
unsigned long long l = 1, r = n - 1;
while (x & 1 && x != 2 * (n - 1) - 1) {
unsigned long long tr = (r - l + 1) / 2 + r;
unsigned long long tl = r + (int)(r % 2 == 0);
if (x != r) x = r + (x - l) / 2 + 1;
r = tr;
l = tl;
if (l > r) x = 2 * (n - 1) - 1;
}
if (x == 2 * (n - 1) - 1) x++;
cout << x / 2 + 1 << '\n';
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
#define ll long long
#define dro long double
#define ii pair<int,int>
#define F first
#define S second
#define fr(i,l,n) for(int i=l; i<n; i++)
#define al(fu) (fu).begin(), (fu).end()
#define alr(fu) (fu).rbegin(), (fu).rend()
#define skoree ios::sync_with_stdio(0); cin.tie(0)
#define br cout<<"\n"
#define pr(c) cout<<c<<"\n"
#define pr2(c1,c2) cout<<c1<<' '<<c2<<"\n"
#define pr3(c1,c2,c3) cout<<c1<<' '<<c2<<' '<<c3<<"\n"
#define prv(a) {fr(qz,0,a.size()) cout<<a[qz]<<' '; br;}
#define prm(a,n) {fr(qz,0,n) cout<<a[qz]<<' '; br;}
#define prw(a,n,m) {fr(i,0,n) {fr(j,0,m) cout<<a[i][j]<<' '; br;}}
#define danet(b) cout<<(b?"YES":"NO")<<"\n"
#define Danet(b) cout<<(b?"Yes":"No")<<"\n"
using namespace std;
const int nh=101, m7=1000000007, m99=998244353;
const int ogo=m7,nhl=19,mh=nh; const double eps=1e-9;
int main(){
skoree;
int n; cin>>n;
vector<int> a(n);
fr(i,0,n){
char qc; cin>>qc;
if(qc=='+') cin>>a[i];
}
ll c=0;
ll d[n+1][n];
fr(h,0,n) if(a[h]){
int x=a[h];
memset(d,0,sizeof(d)); d[0][0]=1;
fr(i,0,n) fr(j,0,n){
d[i+1][j]=d[i][j];
if(i==h) continue;
if(!a[i]){
if(j<n-1) d[i+1][j] = (d[i+1][j]+d[i][j+1])%m99;
if(j==0 && i<h) d[i+1][j] = (d[i+1][j]+d[i][j])%m99;
}
else if(a[i]>x || a[i]==x&&i>h) d[i+1][j] = (d[i+1][j]+d[i][j])%m99;
else if(j) d[i+1][j] = (d[i+1][j]+d[i][j-1])%m99;
}
c = (c+accumulate(d[n],d[n]+n,0LL)%m99*x%m99)%m99;
}
pr(c);
return 0;
}
/*
*/
| 10 | CPP |
from sys import stdin
input = stdin.readline
n, v = list(map(int, input().split()))
v = min(n, v)
print(int(v + (2+n-v)*(n-v-1)/2)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int readl() {
int x = 0, fg = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') fg = -fg;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return fg * x;
}
int n, cnt = 0, a[1000100], f[1000100];
long long sz[1000100];
struct nd {
int u, v;
long long val, valn;
} c[1000100];
vector<int> vec[1000100];
int find(int x) {
if (f[x] != x) f[x] = find(f[x]);
return f[x];
}
int cmp(nd xx, nd yy) { return xx.val < yy.val; }
int cmp1(nd xx, nd yy) { return xx.valn > yy.valn; }
int main() {
n = readl();
for (int i = 1; i <= n; i++) a[i] = readl();
int x, y;
for (int i = 1; i < n; i++) {
x = readl();
y = readl();
c[i].u = x;
c[i].v = y;
c[i].val = max(a[x], a[y]);
c[i].valn = min(a[x], a[y]);
}
long long ans = 0;
sort(c + 1, c + n, cmp);
for (int i = 1; i <= n + 1; i++) {
f[i] = i;
sz[i] = 1;
}
for (int i = 1; i < n; i++) {
int fu = find(c[i].u), fv = find(c[i].v);
ans += sz[fu] * sz[fv] * c[i].val;
f[fu] = fv;
sz[fv] += sz[fu];
}
sort(c + 1, c + n, cmp1);
for (int i = 1; i <= n + 1; i++) {
f[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= n; i++) {
int fu = find(c[i].u), fv = find(c[i].v);
ans -= sz[fu] * sz[fv] * c[i].valn;
f[fu] = fv;
sz[fv] += sz[fu];
}
printf("%lld", ans);
return 0;
}
| 12 | CPP |
mod=10**9+7
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
dp=[[[0,0] for j in range(2)] for i in range(m)]
dp[0]=[[1,0],[1,0]]
for i in range(m-1):
for j in range(2):
for k in range(2):
if j==0 and k==0:
dp[i+1][j][1]+=dp[i][j][k]
dp[i+1][j+1][0]+=dp[i][j][k]
elif j==1 and k==0:
dp[i+1][j][1]+=dp[i][j][k]
dp[i+1][j-1][0]+=dp[i][j][k]
elif j==0 and k==1:
dp[i+1][j+1][0]+=dp[i][j][k]
else:
dp[i+1][j-1][0]+=dp[i][j][k]
for j in range(2):
for k in range(2):
dp[i+1][j][k]%=mod
ans=-2
for j in range(2):
for k in range(2):
ans+=dp[m-1][j][k]
ans%=mod
dp2=[[[0,0] for j in range(2)] for i in range(n)]
dp2[0]=[[1,0],[1,0]]
for i in range(n-1):
for j in range(2):
for k in range(2):
if j==0 and k==0:
dp2[i+1][j][1]+=dp2[i][j][k]
dp2[i+1][j+1][0]+=dp2[i][j][k]
elif j==1 and k==0:
dp2[i+1][j][1]+=dp2[i][j][k]
dp2[i+1][j-1][0]+=dp2[i][j][k]
elif j==0 and k==1:
dp2[i+1][j+1][0]+=dp2[i][j][k]
else:
dp2[i+1][j-1][0]+=dp2[i][j][k]
for j in range(2):
for k in range(2):
dp2[i+1][j][k]%=mod
for j in range(2):
for k in range(2):
ans+=dp[n-1][j][k]
ans%=mod
print(ans) | 7 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int n, m, i, j, a[100009], count = 0, b = 0, c, d = 0, e[100009], q;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
e[i] = a[i];
}
for (i = 0; i < n - 1; i++) {
if ((a[i] > a[i + 1]) && count == 0) {
count++;
m = a[i];
b = i;
} else if ((a[i] > a[i + 1]) && count > 0)
count++;
else if ((count > 0) && (a[i] < a[i + 1])) {
c = a[i];
d = i;
break;
}
if (i == n - 2) {
if (count > 0) {
c = a[i + 1];
d = i + 1;
}
break;
}
}
q = 0;
for (i = b; i <= d; i++) {
e[i] = a[d - q];
q++;
}
q = 0;
for (i = 0; i < n - 1; i++) {
if (e[i] > e[i + 1]) {
q = 1;
break;
}
}
if (q == 1)
printf("no");
else if (count == 0)
printf("yes\n1 1");
else
printf("yes\n%d %d", b + 1, d + 1);
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void s() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
s();
long long n;
cin >> n;
long long sa = 0, sg = 0;
long long a[n], g[n];
string res = "";
for (long long i = 0; i < n; i++) {
cin >> a[i] >> g[i];
if (abs(sa - sg) > 500) {
res += "G";
sg += g[i];
} else {
sa += a[i];
if (abs(sa - sg) > 500) {
sa -= a[i];
sg += g[i];
res += "G";
continue;
}
res += "A";
}
}
if (abs(sa - sg) > 500)
cout << -1 << "\n";
else
cout << res << "\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
char s[maxn];
int main() {
int _;
scanf("%d", &_);
while (_--) {
int n;
scanf("%d", &n);
scanf("%s", s + 1);
int flag = 0;
for (int i = n; i >= 1; i--) {
if (s[i] == '0') {
flag = i;
break;
}
}
if (!flag) {
printf("%d %d %d %d\n", 1, n - 1, 2, n);
} else {
if (flag > n / 2) {
printf("%d %d %d %d\n", 1, flag, 1, flag - 1);
} else {
printf("%d %d %d %d\n", flag, n, flag + 1, n);
}
}
}
return 0;
}
| 9 | CPP |
import sys
import math
#to read string
get_string = lambda: sys.stdin.readline().strip()
#to read list of integers
get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) )
#to read non spaced string and elements are integers to list of int
get_intList_from_str = lambda: list(map(int,list(sys.stdin.readline().strip())))
#to read non spaced string and elements are character to list of character
get_charList_from_str = lambda: list(sys.stdin.readline().strip())
#get word sepetared list of character
get_char_list = lambda: sys.stdin.readline().strip().split()
#to read integers
get_int = lambda: int(sys.stdin.readline())
#to print faster
pt = lambda x: sys.stdout.write(str(x))
#--------------------------------WhiteHat010--------------------------------#
a,b = get_int_list()
x,y,z = get_int_list()
yellow = x*(2) + y*(1)
blue = y*(1) + z*(3)
print( max(0,yellow-a)+max(0,blue-b) ) | 7 | PYTHON3 |
s = input()
lower_count = sum(1 for c in s if c.islower())
upper_count = sum(1 for c in s if c.isupper())
if (upper_count > lower_count):
print(s.upper())
else:
print(s.lower())
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200020;
int n, m, q;
vector<int> v[maxn];
int up[maxn];
bool mark[maxn];
stack<int> st;
int h[maxn];
set<int> s[maxn], t[maxn];
bool FLAG = 0;
int po[maxn];
int merge(int a, int b) {
if (a == b) return a;
if (t[a].size() + s[a].size() > t[b].size() + s[b].size()) swap(a, b);
while (s[a].size()) {
int k = *s[a].begin();
s[a].erase(k);
if (t[b].find(k) != t[b].end())
t[b].erase(k);
else
s[b].insert(k);
}
while (t[a].size()) {
int k = *t[a].begin();
t[a].erase(k);
if (s[b].find(k) != s[b].end())
s[b].erase(k);
else
t[b].insert(k);
}
return b;
}
void dfs(int x, int par) {
if (FLAG) return;
mark[x] = 1;
st.push(x);
h[x] = h[par] + 1;
up[x] = h[x];
bool didim = 0;
for (int i = 0; i < v[x].size(); i++)
if (v[x][i] != par || didim) {
if (!mark[v[x][i]]) dfs(v[x][i], x);
up[x] = min(up[x], up[v[x][i]]);
po[x] = merge(po[x], po[v[x][i]]);
} else
didim = 1;
if (up[x] == h[x])
if (s[po[x]].size() && t[po[x]].size()) FLAG = 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= n; i++) po[i] = i;
while (q--) {
int x, y;
cin >> x >> y;
s[x].insert(q);
t[y].insert(q);
}
for (int i = 1; i <= n; i++) {
if (!mark[i]) dfs(i, 0);
if (s[po[i]].size() || t[po[i]].size()) FLAG = 1;
}
if (FLAG)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
unsigned long long x;
stack<pair<unsigned long long, unsigned long long> > q;
vector<pair<unsigned long long, unsigned long long> > sol;
int main() {
cin >> x;
x *= 6;
for (unsigned long long n = 1;; ++n) {
unsigned long long k = 2 * n * n * n;
if (k > x) break;
k += 3 * n * n;
if (k > x) break;
k += n;
if (k > x) break;
unsigned long long w = 3 * n * (n + 1);
if ((x - k) % w == 0) {
unsigned long long m = (x - k) / w + n;
sol.push_back(make_pair(n, m));
q.push(make_pair(m, n));
}
}
while (!q.empty()) {
if (q.top().first != q.top().second)
sol.push_back(make_pair(q.top().first, q.top().second));
q.pop();
}
cout << sol.size() << endl;
for (int i = 0; i < sol.size(); ++i) {
cout << sol[i].first << ' ' << sol[i].second << endl;
}
return 0;
}
| 10 | CPP |
a=int(input())
b=list(map(int,input().split(" ")))
if(sum(b)>0):
print("HARD")
else:
print("EASY") | 7 | PYTHON3 |
import sys
import string
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
t = ri()
for _ in range(t):
A = []
for z in range(9):
A.append([int(x) for x in input()])
change = [(0,0),(1,3),(2,6),(3,1),(4,4),(5,7),(6,2),(7,5),(8,8)]
for i,j in change:
if A[i][j]==9:
A[i][j]=1
else:
A[i][j]+=1
for a in A:
print(int("".join(map(str, a))) ) | 10 | PYTHON3 |
t = int(input())
for i in range(t):
n,k = map(int, input().split())
arr = list(map(int, input().split()))
if sum(arr) == 0:
print(0)
continue
newarr = sorted(arr, reverse=True)
for j in range(1,k+1):
newarr[0] += newarr[j]
newarr[j] = 0
print(newarr[0] - sorted(newarr)[0])
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
for (int q1 = 0; q1 < q; q1++) {
int n, k;
cin >> n >> k;
int ans = n / k;
int ff = n % k;
ans *= k;
if (ff <= k / 2)
ans += ff;
else
ans += k / 2;
cout << ans << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, p, k;
scanf("%d%d%d", &n, &p, &k);
int st = p - k;
int lt = p + k;
if (st < 1) st = 1;
if (lt > n) lt = n;
if (st > 1) printf("<< ");
for (int i = st; i <= lt; i++) {
if (i == p)
printf("(%d) ", i);
else
printf("%d ", i);
}
if (lt < n) printf(">>\n");
return 0;
}
| 7 | CPP |
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const double eps=1e-5;
int a[maxn],n,k;
bool check(double mval)
{
int cnt=0;
for(int i=1;i<=n;i++)
cnt+=floor(a[i]/mval);
return cnt<=k;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
double l=0,r=1e9;
while(r-l>eps)
{
double mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid;
}
printf("%d\n",(int)round(l+0.5));
} | 0 | CPP |
#!/usr/bin/env python3
d, L, v1, v2 = [int(x) for x in input().split()]
print((L - d)/(v1 + v2))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int x = s.find('0');
int n = s.size();
if (x != -1) {
s[x] = '#';
for (int i = 0; i < n; ++i)
if (s[i] != '#') cout << s[i];
} else
for (int i = 0; i < n - 1; ++i) cout << s[i];
return 0;
}
| 9 | CPP |
import math, collections, sys
input = sys.stdin.readline
def case():
n, d = map(int, input().split())
a = list(map(int, input().split()))
for i in range(1, n):
while a[i]:
if d >= i:
a[i]-=1
a[0]+=1
d-=i
else:
break
print(a[0])
for _ in range(int(input())):
case() | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
struct node {
int l, r;
int sum[3];
} segTree[maxn << 2];
int a, b;
void pushup(int num) {
for (int i = 0; i < 3; i++) {
segTree[num].sum[i] =
segTree[num << 1].sum[i] + segTree[num << 1 | 1].sum[i];
}
}
void build(int num, int l, int r) {
segTree[num].l = l;
segTree[num].r = r;
for (int i = 0; i < 3; i++) {
segTree[num].sum[i] = 0;
}
if (l == r) {
return;
}
int mid = (l + r) >> 1;
build(num << 1, l, mid);
build(num << 1 | 1, mid + 1, r);
}
void update(int num, int pos, int v) {
if (segTree[num].l == segTree[num].r) {
int add = segTree[num].sum[2] + v;
segTree[num].sum[0] = min(b, add);
segTree[num].sum[1] = min(a, add);
segTree[num].sum[2] = add;
return;
}
int mid = (segTree[num].l + segTree[num].r) >> 1;
if (pos <= mid) {
update(num << 1, pos, v);
} else {
update(num << 1 | 1, pos, v);
}
pushup(num);
}
int query(int num, int l, int r, int ty) {
if (l > r) {
return 0;
}
if (l == segTree[num].l && r == segTree[num].r) {
return segTree[num].sum[ty];
}
int mid = (segTree[num].l + segTree[num].r) >> 1;
if (r <= mid) {
return query(num << 1, l, r, ty);
} else if (l > mid) {
return query(num << 1 | 1, l, r, ty);
} else {
return query(num << 1, l, mid, ty) + query(num << 1 | 1, mid + 1, r, ty);
}
}
int main(void) {
int n, k, q;
scanf("%d%d%d%d%d", &n, &k, &a, &b, &q);
build(1, 1, n);
while (q--) {
int op;
scanf("%d", &op);
if (op == 1) {
int d, a;
scanf("%d%d", &d, &a);
update(1, d, a);
} else {
int p;
scanf("%d", &p);
printf("%d\n", query(1, 1, p - 1, 0) + query(1, p + k, n, 1));
}
}
return 0;
}
| 10 | CPP |
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;
int dp[1000][1000];
int main() {
int n, x;
cin >> n >> x;
int cnt = 0;
map<string, int> mp;
vector<string> a;
for (int i = 0; i < n; ++i) {
vector<int> v;
int m;
cin >> m;
for (int j = 0; j < m; ++j) {
string s;
cin >> s;
if(!mp.count(s)){
v.emplace_back(cnt);
a.emplace_back(s);
mp[s] = cnt++;
}else {
v.emplace_back(mp[s]);
}
}
for (int j = 0; j < m; ++j) {
for (int k = 0; k < m; ++k) {
if(j != k){
dp[v[j]][v[k]]++;
}
}
}
}
vector<pair<string, string>> ans;
for (int j = 0; j < cnt; ++j) {
for (int k = 0; k < cnt; ++k) {
if(a[j] < a[k] && dp[j][k] >= x){
ans.emplace_back(a[j], a[k]);
}
}
}
cout << ans.size() << "\n";
sort(ans.begin(),ans.end());
for (auto &&l : ans) {
cout << l.first << " " << l.second << "\n";
}
return 0;
}
| 0 | CPP |
for x in range(int(input())):
n=int(input())
lst=list()
for i in range(1,n+1):
lst.append(i)
print(*lst)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct Amulet {
int Ele[4];
};
bool isEqual(Amulet A, Amulet B) {
for (int i = 0; i < 4; i++) {
if (A.Ele[0] == B.Ele[i]) {
if (A.Ele[1] == B.Ele[(i + 1) % 4]) {
if (A.Ele[2] == B.Ele[(i + 2) % 4]) {
if (A.Ele[3] == B.Ele[(i + 3) % 4]) {
return true;
}
}
}
}
}
return false;
}
bool checkAInV(vector<Amulet> &V, Amulet A) {
for (int i = 0; i < V.size(); i++) {
if (isEqual(A, V[i])) {
return false;
}
}
V.push_back(A);
return true;
}
int main() {
vector<Amulet> V;
int n;
cin >> n;
int tmp1, tmp2;
string S;
Amulet A;
for (int i = 1; i < n; i++) {
cin >> tmp1 >> tmp2;
cin >> S;
A.Ele[0] = tmp1 / 10;
A.Ele[1] = tmp1 % 10;
A.Ele[2] = tmp2 % 10;
A.Ele[3] = tmp2 / 10;
checkAInV(V, A);
}
cin >> tmp1 >> tmp2;
A.Ele[0] = tmp1 / 10;
A.Ele[1] = tmp1 % 10;
A.Ele[2] = tmp2 % 10;
A.Ele[3] = tmp2 / 10;
checkAInV(V, A);
cout << V.size();
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, k, x, y, ans, t[10], sz[200010], f[200010][5];
vector<int> g[200010];
void dfs1(int u, int p) {
sz[u] = 1;
for (int v : g[u]) {
if (v != p) {
dfs1(v, u);
sz[u] += sz[v];
f[u][0] += f[v][k - 1] + sz[v];
for (int i = 1; i < k; i++) {
f[u][i] += f[v][i - 1];
}
}
}
}
void dfs2(int u, int p) {
memset(t, 0, sizeof(t));
if (p) {
t[0] = f[p][0] - f[u][k - 1] - sz[u];
for (int i = 1; i < k; i++) {
t[i] = f[p][i] - f[u][i - 1];
}
}
f[u][0] += t[k - 1] + n - sz[u];
for (int i = 1; i < k; i++) {
f[u][i] += t[i - 1];
}
for (int v : g[u]) {
if (v != p) {
dfs2(v, u);
}
}
}
int main() {
cin >> n >> k;
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(1, 0);
dfs2(1, 0);
for (int i = 1; i <= n; i++) {
ans += f[i][0];
}
cout << ans / 2;
return 0;
}
| 10 | CPP |
def extra_let(string):
extra = 0
prev = string[0]
for i in string[1:]:
if i == prev:
extra += 1
prev = i
return extra
def main():
n = int(input())
row = input()
print(extra_let(row))
if __name__ == '__main__':
main()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
#define MAXN 100000
long long B[MAXN], C[MAXN];
typedef tuple<pair<long long, int>, int, int> T;
vector<T> E;
int Rank[MAXN], Par[MAXN], Boss[MAXN];
vector<int> Kids[MAXN]; int Root;
typedef pair<long long, long long> P;
void init() {
for(int v = 0; v < N; ++v) {
Rank[v] = 0; Par[v] = Boss[v] = v;
}
}
int find(int v) {
if(Par[v] == v) return v;
else return Par[v] = find(Par[v]);
}
void unite(int u, int v) {
u = find(u); v = find(v);
if(u == v) return;
if(Rank[u] > Rank[v]) swap(u, v);
Par[u] = v;
if(Rank[u] == Rank[v]) ++Rank[v];
if(make_pair(C[Boss[u]], Boss[u])
> make_pair(C[Boss[v]], Boss[v])) {
Kids[Boss[u]].push_back(Boss[v]);
Boss[v] = Boss[u];
} else {
Kids[Boss[v]].push_back(Boss[u]);
}
Root = Boss[v];
}
P dp(int v) {
long long b = B[v];
vector<P> ps;
for(int w : Kids[v]) {
auto p = dp(w);
ps.push_back(p); b += p.second;
}
long long q = C[v] + b;
for(int i = 0; i < (int)ps.size(); ++i) {
q = min(q, max(ps[i].first, C[v]) + b - ps[i].second);
}
return {q, b};
}
int main() {
cin >> N >> M;
for(int v = 0; v < N; ++v) {
long long a, b; cin >> a >> b;
B[v] = b; C[v] = max(a - b, 0LL);
}
for(int e = 0; e < M; ++e) {
int u, v; cin >> u >> v; --u; --v;
E.emplace_back(
max(make_pair(C[u], u), make_pair(C[v], v)),
u, v);
}
sort(E.begin(), E.end());
init();
for(int e = 0; e < M; ++e) {
unite(get<1>(E[e]), get<2>(E[e]));
}
cout << dp(Root).first << endl;
return 0;
}
| 0 | CPP |
N = int(input())
a = N%1000
print(abs(1000-a)%1000) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
;
long long q;
cin >> q;
while (q--) {
long long n;
cin >> n;
if (n == 2)
cout << 2 << '\n';
else {
if (n % 2 != 0)
cout << 1 << '\n';
else
cout << 0 << '\n';
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool isprime(unsigned long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (unsigned long long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long int gcd(long long int a, long long int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long int exp(long long int value, long long int power) {
long long int result = 1;
while (power > 0) {
if (power & 1LL) {
result *= value;
result %= 1000000007;
}
value *= value;
value %= 1000000007;
power /= 2LL;
}
return result;
}
long long int lcm(long long n1, long long n2) {
long long max = (n1 > n2) ? n1 : n2;
do {
if (max % n1 == 0 && max % n2 == 0) {
return max;
} else
++max;
} while (true);
}
int isprime(long long n) {
if (n == 1) return 0;
if (n <= 3) return 1;
if (n % 2 == 0 || n % 3 == 0) return 0;
for (long long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return 0;
}
return 1;
}
long long a[1000][1000];
long long b[1000005];
long long c[1000005];
long long d[1000005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long int min_, max_, ans, n, sum, f, j, cnt, len, row, col, p, q, r, t,
i, k, l, m, x, y, z;
cin >> k >> p;
sum = 0;
for (i = 1; k--; i++) {
x = i;
y = i;
while (x) {
y = y * 10 + x % 10;
x /= 10;
}
sum = ((sum % p) + (y % p)) % p;
}
cout << sum;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
int AB, BA;
bool flag = false;
cin >> s;
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] == 'A' && s[i + 1] == 'B') {
AB = i;
break;
}
}
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'A') {
BA = i;
break;
}
}
for (int i = AB + 2; i < s.size() - 1; i++) {
if (s[i] == 'B' && s[i + 1] == 'A') {
flag = true;
cout << "YES\n";
return 0;
}
}
if (!flag)
for (int i = BA + 2; i < s.size() - 1; i++) {
if (s[i] == 'A' && s[i + 1] == 'B') {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
return 0;
}
| 7 | CPP |
N = int(input())
years = [int(i) for i in input().strip().split()]
print(int(sum(years)/N)) | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mxn=1e5+5;
ll n, m, l[mxn], r[mxn], cl, cr, ans;
priority_queue<ll>pq_left;
priority_queue<ll, vector<ll>, greater<ll> >pq_right;
int main(){
scanf("%lld", &n);
for(ll i=0; i<n; i++){
scanf("%lld%ld", &l[i], &r[i]);
if(i==0){
pq_left.push(l[i]);
pq_right.push(l[i]);
cl=cr=0;
continue;
}
ll last=r[i-1]-l[i-1], cur=r[i]-l[i];
cl-=cur; cr+=last;
ll lf=pq_left.top()+cl, rg=pq_right.top()+cr;
if(l[i]<lf){
ans+=lf-l[i];
pq_right.push(lf-cr);
pq_left.push(l[i]-cl);
pq_left.push(l[i]-cl);
pq_left.pop();
}else if(l[i]>rg){
ans+=l[i]-rg;
pq_left.push(rg-cl);
pq_right.push(l[i]-cr);
pq_right.push(l[i]-cr);
pq_right.pop();
}else{
pq_left.push(l[i]-cl);
pq_right.push(l[i]-cr);
}
}
printf("%lld\n", ans); return 0;
} | 0 | CPP |
# -*- coding: utf-8 -*-
from math import ceil
t=int(input())
for i in range(t):
x=input()
x1=x.split()
a=int(x1[0])
b=int(x1[1])
if b<a:
temp=b
b=a
a=temp
a1=max(a+a,b)
a2=max(a+b,b)
print((min(a1,a2))**2) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int intcmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; }
int n, m, k;
vector<pair<int, int>> v1v2index[200010];
int ea[200010], eb[200010];
int d[200010];
int curidx;
bool deleted[200010];
int delcnt[200010];
int delnow;
void dfs(int v) {
if (deleted[v] || d[v] >= k) {
return;
}
deleted[v] = true;
++delnow;
for (auto [u, idx] : v1v2index[v]) {
if (idx >= curidx) {
continue;
}
--d[u];
dfs(u);
}
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
v1v2index[a].push_back(make_pair(b, i));
v1v2index[b].push_back(make_pair(a, i));
ea[i] = a;
eb[i] = b;
d[a]++;
d[b]++;
}
curidx = m;
for (int i = 1; i < n + 1; i++) {
dfs(i);
}
for (curidx = m - 1; curidx >= 0; curidx--) {
delcnt[curidx] = delnow;
if (deleted[ea[curidx]] || deleted[eb[curidx]]) {
continue;
}
d[ea[curidx]]--;
d[eb[curidx]]--;
dfs(ea[curidx]);
dfs(eb[curidx]);
}
for (int i = 0; i < m; ++i) {
printf("%d\n", n - delcnt[i]);
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s,t;
cin>>s>>t;
int n=t.size();
t.erase(n-1,1);
if(s==t)
cout<<"Yes";
else cout<<"No";
} | 0 | CPP |
from itertools import accumulate
n=int(input())
d={0:-1}
a=0
for i,b in enumerate(accumulate(map(lambda c:2*int(c)-1,input()))):
d[b]=x=d.get(b,i)
a=max(a,i-x)
print(a)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
#define rhl (1000000007)
int f[5005][5005],n,m,ans;
char s[5005];
int main(){
std::cin>>n,scanf("%s",s),m=strlen(s),f[0][0]=ans=1;
for (int i=0;i<n;++i)
for (int j=0;j<n;++j)
(f[i+1][std::max(0,j-1)]+=f[i][j])%=rhl,(f[i+1][j+1]+=2LL*f[i][j])%=rhl;
for (int i=0;i<m;++i) ans=1LL*ans*500000004%rhl;
std::cout<<1LL*ans*f[n][m]%rhl; return 0;
}
| 0 | CPP |
MAX_SIZE = 100 * 101 // 2
prime = [True] * (MAX_SIZE + 1)
def seive():
p = 2
while p * p <= MAX_SIZE:
if prime[p]:
for i in range(p * p, MAX_SIZE, p):
prime[i] = False
p += 1
seive()
for _ in range(int(input())):
n = int(input())
c = 0
while prime[c] or not prime[n - 1 + c]:
c += 1
l = [1] * n
for i in range(n):
l[i] = c
print(*l)
l[i] = 1
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, L, brk, cnt = 0;
cin >> n >> L >> brk;
long long t = 0;
for (long long i = 0; i < n; i++) {
long long x, y;
cin >> x >> y;
cnt = cnt + (x - t) / brk;
t = x + y;
if (i == n - 1) cnt += (L - t) / brk;
}
if (n == 0) cnt = L / brk;
cout << cnt;
return 0;
}
| 7 | CPP |
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef double db;
db a,p,ans;
int i,j,k,l,t,n,m;
int main(){
scanf("%d%lf%lf",&n,&a,&p);
a-=p;
t=2*n;
l=n;
fo(i,1,n){
ans+=(db)(a*t+p*t*(t+1)/2)/t;
a=a+a/l+3*p/(2*l);
p=p+2*p/l;
l--;
t-=2;
}
printf("%.15lf\n",ans);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
double fRand(double fMin, double fMax) {
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
string s;
int n, k;
bool dp[5005][5005];
int main() {
cin >> s;
int n = s.length(), k = log2(n);
dp[0][0] = true;
for (int len = (0); len <= (n - (1 << k)); ++len) {
for (int mask = (0); mask <= ((1 << k) - 1); ++mask) {
int i = len + mask;
for (int j = (0); j <= (k - 1); ++j)
if (((mask >> j) & 1) == 0)
dp[i + (1 << j)][mask | (1 << j)] |= dp[i][mask];
}
char minC = 'z';
for (int mask = (0); mask <= ((1 << k) - 1); ++mask)
if (dp[len + mask][mask]) minC = min(minC, s[len + mask]);
for (int mask = (0); mask <= ((1 << k) - 1); ++mask)
if (s[len + mask] == minC)
dp[len + mask + 1][mask] |= dp[len + mask][mask];
printf("%c", minC);
}
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int arr[10][10];
int main() {
int row, col, n, num, r, c;
cin >> row >> col >> n >> num;
for (int i = 0; i < n; i++) {
cin >> r >> c;
arr[r - 1][c - 1] = 1;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i) arr[i][j] += arr[i - 1][j];
if (j) arr[i][j] += arr[i][j - 1];
if (i && j) arr[i][j] -= arr[i - 1][j - 1];
}
}
int sum = 0, ans = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k <= i; k++) {
for (int m = 0; m <= j; m++) {
sum = arr[i][j];
if (k) sum -= arr[k - 1][j];
if (m) sum -= arr[i][m - 1];
if (m && k) sum += arr[k - 1][m - 1];
if (sum >= num) {
ans++;
}
}
}
}
}
cout << ans << endl;
return 0;
}
| 7 | CPP |
#include <cstdio>
#include <cmath>
#include <climits>
#include <algorithm>
using namespace std;
class Point {
public:
double x, y;
Point (double x = 0, double y = 0) : x(x), y(y) { }
Point operator + (Point p) { return Point(x + p.x, y + p.y); }
Point operator - (Point p) { return Point(x - p.x, y - p.y); }
Point operator * (double a) { return Point(a * x, a * y); }
Point operator / (double a) { return Point (x / a, y / a); }
};
struct Segment {
Point p1, p2;
};
double dot(Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
double norm(Point a) {
return a.x * a.x + a.y * a.y;
}
int main()
{
Segment s;
double x1, y1, x2, y2, xq, yq, r;
int q;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
s.p1 = Point(x1, y1);
s.p2 = Point(x2, y2);
Point base = s.p2 - s.p1;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%lf %lf", &xq, &yq);
Point p = Point(xq, yq);
r = dot(p - s.p1, base) / norm(base);
Point res = s.p1 + base * r;
printf("%.10lf %.10lf\n", res.x, res.y);
}
return 0;
}
| 0 | CPP |
k=int(input())
l=int(input())
m=int(input())
n=int(input())
d=int(input())
count=0
for i in range(1,d+1):
if(i%k==0 or i%l==0 or i%m==0 or i%n==0):
count=count+1
if(count>=d):
print(d)
else:
print(count)
| 7 | PYTHON3 |
import re
print("YES" if re.search(r'0{7,}|1{7,}', input()) else "NO")
| 7 | PYTHON3 |
ii = lambda: int(input())
n = ii()
ret = 0
cur = 0
for i in range(n):
s = list(map(int, input().split()))
cur += s[1] - s[0]
ret = max(ret, cur)
print(ret)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int f[123456];
int a[123456];
int t[123456];
int query(int l, int r) {
int res = f[r];
if (l > 0) {
res -= f[l - 1];
}
return res;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
int s = 0;
for (int i = 0; i < n; i++) {
scanf("%d", t + i);
f[i] = (t[i] ? 0 : a[i]);
if (i > 0) {
f[i] += f[i - 1];
}
if (t[i]) {
s += a[i];
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
int r = min(n - 1, i + k - 1);
ans = max(ans, query(i, r));
}
printf("%d\n", ans + s);
return 0;
}
| 8 | CPP |
# Aim for the moon. If you miss, you may hit a star. W. Clement Stone
# by : Blue Edge - Create some chaos
import math
for _ in range(int(input())):
n,k=map(int,input().split())
s=input()
a=list(s)
a.sort()
b=list(set(a))
b.sort()
ans=a[k-1]
if ans!=b[0]:
print( ans )
# continue
else:
try:
c=a[k]
if c!=b[-1]:
print( ans + "".join(a[k:]) )
# continue
else:
print( ans + c*math.ceil((n-k)/k) )
except:
print(ans)
| 9 | PYTHON3 |
#include<iostream>
using namespace std;
int main(){
int m, n, k;
cin >> m >> n >> k;
int J[m+1][n+1], O[m+1][n+1], I[m+1][n+1];
for(int i = 0; i <= m; i++){
for(int j = 0; j <= n; j++){
J[i][j] = 0;
O[i][j] = 0;
I[i][j] = 0;
}
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
char c;
cin >> c;
if(c == 'J') J[i][j]++;
else if(c == 'O') O[i][j]++;
else I[i][j]++;
}
}
for(int i = 1; i <= m; i++){
for(int j = 0; j <= n; j++){
J[i][j] += J[i-1][j];
O[i][j] += O[i-1][j];
I[i][j] += I[i-1][j];
}
}
for(int i = 0; i <= m; i++){
for(int j = 1; j <= n; j++){
J[i][j] += J[i][j-1];
O[i][j] += O[i][j-1];
I[i][j] += I[i][j-1];
}
}
for(int i = 0; i < k; i++){
int a, b, c, d;
cin >> a >> b >> c >> d;
int ans_j , ans_o, ans_i;
ans_j = J[c][d] - J[a-1][d] - J[c][b-1] + J[a-1][b-1];
ans_o = O[c][d] - O[a-1][d] - O[c][b-1] + O[a-1][b-1];
ans_i = I[c][d] - I[a-1][d] - I[c][b-1] + I[a-1][b-1];
cout << ans_j << " " << ans_o << " " << ans_i << endl;
}
}
| 0 | CPP |
arr = [int(i) for i in list(input().split())]
n, l, r = arr[0], arr[1], arr[2]
ans1 = ((1 << l) - 1) + (n-l)
ans2 = ((1 << r) - 1) + (1 << (r-1))*(n-r)
print(ans1, ans2) | 8 | PYTHON3 |
x=input()
if "l" and "s" and "r" and "B" in x and x.count("a")>=2 and x.count("u")>=2:
print(min(x.count("r"),x.count("l"),x.count("s"),x.count("B"),int((x.count("u")/2)),int((x.count("a")/2)),x.count("b")))
else:
print(0) | 7 | PYTHON3 |
c=0
l=[list(map(int,input().split())) for i in range(int(input()))]
for i in l:
if sum(i)>=2:
c+=1
print(c)
| 7 | PYTHON3 |
t = int(input())
def min_xodlar_soni(n):
k2 = 0
k3 = 0
while(n%2 == 0):
n = n//2
k2 += 1
while(n%3 == 0):
n = n//3
k3 += 1
if(k2>k3 or n!=1):
return -1
else:
return (k3-k2)+k3
natija = []
for i in range(t):
n = int(input())
natija.append(min_xodlar_soni(n))
for i in range(t):
print(natija[i])
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[2000][501];
struct Trie {
struct Node {
int c, depth;
int next[26];
Node() : c(0), depth(0) { memset(next, -1, sizeof(next)); }
} P[1000000];
int sz;
vector<int> dp[1000000];
void init() {
sz = 0;
P[sz++] = Node();
}
void insert(char* s) {
int p = 0;
for (char* i = s; *i != '\0'; ++i) {
int c = *i - 'a';
if (P[p].next[c] == -1) {
P[p].next[c] = sz;
P[sz] = Node();
P[sz++].depth = P[p].depth + 1;
}
p = P[p].next[c];
}
++P[p].c;
}
void dfs(int u) {
int& total = P[u].c;
for (int i = 0; i < 26; ++i) {
if (P[u].next[i] != -1) {
dfs(P[u].next[i]);
total += P[P[u].next[i]].c;
}
}
total = min(total, m);
dp[u] = vector<int>(total + 1, 0);
for (int i = 0; i < 26; ++i) {
if (P[u].next[i] != -1) {
int t = P[u].next[i], s = (int)dp[t].size();
for (int j = total; j >= 0; --j) {
for (int k = min(total - j, s - 1); k >= 0; --k) {
dp[u][j + k] = max(dp[u][j + k], dp[u][j] + dp[t][k]);
}
}
}
}
if (u != 0) {
for (int i = 0; i <= total; ++i) {
dp[u][i] += i * (i - 1) / 2;
}
}
}
void travel(int u) {
printf("u = %d\n", u);
for (int i = 0; i < 26; ++i) {
if (P[u].next[i] != -1) {
printf(" %c -> %d\n", i + 'a', P[u].next[i]);
}
}
for (int i = 0; i < 26; ++i) {
if (P[u].next[i] != -1) {
travel(P[u].next[i]);
}
}
}
void gao() {
dfs(0);
printf("%d\n", dp[0][m]);
}
} trie;
int main() {
trie.init();
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%s", s[i]);
trie.insert(s[i]);
}
trie.gao();
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
double n, m;
double Ans;
int main() {
cin >> n >> m;
if (n == (double)1.0 && m == (double)1.0) {
Ans = (double)1.0;
} else {
Ans = (double)((double)1 / n + (double)(n - 1) / n * (m - 1) / (m * n - 1));
}
printf("%.16lf\n", Ans);
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int md = (2 << 30);
const long long oo = 1e14;
const double pi = acos(-1);
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
const int N = 1e4 + 5;
bool pr[N];
void Build() {
memset(pr, 1, sizeof pr);
for (int i = 2; i < N; ++i)
if (pr[i])
for (int j = i + i; j < N; j += i) pr[j] = 0;
}
bool Check(int x) {
int cnt = 0, trm = sqrt(x) + 2;
for (int i = 2; i <= x && cnt < 3; ++i)
if (!(x % i) && pr[i]) ++cnt;
return cnt >= 3;
}
int main() {
int a, b;
scanf("%d%d", &a, &b);
deque<int> dq;
int trm = sqrt(max(a, b)) + 5;
dq.push_back(1);
if (!(a % b)) dq.push_back(b);
if (!(b % a)) dq.push_back(a);
for (int i = 2; i <= trm; ++i) {
if (!(a % i) && !(b % i)) dq.push_back(i);
if (!(b % (a / i)) && !(a % i)) dq.push_back(a / i);
if (!(a % (b / i)) && !(b % i)) dq.push_back(b / i);
}
sort(dq.begin(), dq.end());
int n, l, r;
scanf("%d", &n);
while (n--) {
scanf("%d%d", &l, &r);
deque<int>::iterator it = lower_bound(dq.begin(), dq.end(), r);
if (it == dq.end() || ((*it) > r) && it != dq.begin()) --it;
l <= (*it) && (*it) <= r ? printf("%d\n", (*it)) : puts("-1");
}
}
| 9 | CPP |
s=input()
s=s.lower()
m=max(s)
print(m*s.count(m)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
short xxx[1 << 26];
char s[1111111];
int m, n;
char w[33];
int res[11111];
int mask[11111];
struct Res {
vector<pair<int, int> > beg;
vector<pair<int, int> > end;
int all;
int from;
int to;
};
Res solve(int l, int r) {
Res rr;
rr.from = l;
rr.to = r;
if (r == l) {
rr.all = 1 << (s[l] - 'a');
} else {
int end = (l + r) / 2;
Res t1 = solve(l, end);
Res t2 = solve(end + 1, r);
rr.all = t1.all | t2.all;
rr.beg = t1.beg;
rr.beg.push_back(pair<int, int>(t1.all, end));
for (int i = (0); i < (t2.beg.size()); i++) {
int nmask = t2.beg[i].first | t1.all;
if (nmask != rr.all) {
if (rr.beg.back().first != nmask)
rr.beg.push_back(pair<int, int>(nmask, t2.beg[i].second));
else
rr.beg.back().second = t2.beg[i].second;
}
}
while (rr.beg.size() && rr.beg.back().first == rr.all) rr.beg.pop_back();
rr.end = t2.end;
rr.end.push_back(pair<int, int>(t2.all, end + 1));
for (int i = (0); i < (t1.end.size()); i++) {
int nmask = t1.end[i].first | t2.all;
if (nmask != rr.all) {
if (rr.end.back().first != nmask)
rr.end.push_back(pair<int, int>(nmask, t1.end[i].second));
else
rr.end.back().second = t1.end[i].second;
}
}
while (rr.end.size() && rr.end.back().first == rr.all) rr.end.pop_back();
vector<int> v;
for (int i = (0); i < (t1.end.size() + 1); i++)
for (int j = (0); j < (t2.beg.size() + 1); j++) {
int nmask = (i == t1.end.size() ? 0 : t1.end[i].first) |
(t2.beg.size() == j ? 0 : t2.beg[j].first);
if (nmask != rr.all && (nmask & t1.all) != t1.all &&
(nmask & t2.all) != t2.all) {
v.push_back(nmask);
}
}
sort((v).begin(), (v).end()),
(v).erase(unique((v).begin(), (v).end()), (v).end());
for (int i = (0); i < (v.size()); i++) res[xxx[v[i]]]++;
}
int mask = 1 << (s[l] - 'a');
for (int i = (0); i < (rr.beg.size()); i++) {
if (rr.beg[i].first != mask)
while (1)
;
mask |= (1 << (s[rr.beg[i].second + 1] - 'a'));
}
while (mask != rr.all)
;
mask = 1 << (s[r] - 'a');
for (int i = (0); i < (rr.end.size()); i++) {
mask |= (1 << (s[rr.end[i].second - 1] - 'a'));
}
while (mask != rr.all)
;
return rr;
}
int main() {
scanf(" %s", s);
n = strlen(s);
cin >> m;
for (int i = (0); i < (m); i++) {
scanf(" %s", w);
int tt = strlen(w);
int mm = 0;
for (int j = (0); j < (tt); j++) mm |= (1 << (w[j] - 'a'));
mask[i] = mm;
xxx[mm] = i + 1;
}
Res t = solve(0, n - 1);
res[xxx[t.all]]++;
for (int i = (0); i < (t.beg.size()); i++) res[xxx[t.beg[i].first]]++;
for (int i = (0); i < (t.end.size()); i++) res[xxx[t.end[i].first]]++;
for (int i = (0); i < (m); i++) printf("%d\n", res[xxx[mask[i]]]);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
string s;
int T;
pair<char, char> filla(char ch) {
char res1, res2;
if (ch == 'a') {
res1 = 'b';
res2 = 'b';
} else if (ch == 'z') {
res1 = 'y';
res2 = 'y';
} else {
res1 = ch + 1;
res2 = ch - 1;
}
return make_pair(res1, res2);
}
int main() {
cin >> T;
bool np = false;
for (int t = 0; t < T; t++) {
int n;
cin >> n;
cin >> s;
for (int i = 0; i * 2 < n; i++) {
pair<char, char> c1 = filla(s[i]);
pair<char, char> c2 = filla(s[n - i - 1]);
bool f = false;
if (c1.first == c2.first || c1.first == c2.second ||
c1.second == c2.first || c1.second == c2.second)
f = true;
else
f = false;
if (!f) {
cout << "NO" << endl;
np = true;
break;
}
}
if (!np) cout << "YES" << endl;
np = false;
}
return 0;
}
| 7 | CPP |
for _ in[0]*int(input()):
n=int(input())
a=[*range(2,n+1,2)]
b=[*range(1,n,2)]
b[-1]+=n//2
print(['YES\n'+' '.join(map(str,a+b)),'NO'][n%4>0])
| 8 | PYTHON3 |
n,m=map(int,input().split())
l=[]
for _ in range(m):
arr=[int(ele) for ele in input().split()]
arr.reverse()
l.append(arr)
l.sort()
l.reverse()
ans,sum=0,0
for i in range(len(l)):
temp=l[i][1]
sum+=temp
if sum>=n:
temp=temp-(sum-n)
ans=ans+(temp*l[i][0])
if sum>=n:break
print(ans) | 8 | PYTHON3 |
x=input()
y=input()
z=input()
ran=[]
dom=[]
for i in range(0,len(x)):
ran.append(x[i])
for i in range(0,len(y)):
ran.append(y[i])
for i in range(0,len(z)):
dom.append(z[i])
dom.sort()
ran.sort()
if ran==dom:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int N;
vector<int> V[100100];
queue<int> Q;
int B[100100], D[100100];
int main() {
scanf("%d", &N);
for (int i = 1; i < N; ++i) {
int a, b;
scanf("%d%d", &a, &b);
V[a].push_back(b);
V[b].push_back(a);
}
double res = 1.0;
Q.push(1);
D[1] = B[1] = 1;
while (!Q.empty()) {
int v = Q.front();
Q.pop();
int sz = V[v].size();
for (int i = 0; i < sz; ++i) {
int nv = V[v][i];
if (!B[nv]) {
B[nv] = 1;
D[nv] = D[v] + 1;
Q.push(nv);
res = (res * D[nv] + static_cast<double>(1)) / D[nv];
}
}
}
cout.setf(ios::fixed);
cout.precision(13);
cout.setf(ios::showpoint);
cout << res << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c = 0, k = 0;
scanf("%d", &n);
int a[n + 5], b[n + 5];
for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]);
for (int i = 0; i < n; i++) {
if (a[i] > 0)
c++;
else
k++;
}
if (c >= 2 && k >= 2)
printf("No\n");
else
printf("Yes\n");
}
| 7 | CPP |
while True:
n = int(input())
if n == 0:
break
else:
s = list(map(int, input().split(' ')))
times = 0
old_nums = [-1] * n
while old_nums != s:
new_nums = []
for i in s:
new_nums.append(s.count(i))
old_nums = s[::]
s = new_nums[::]
times += 1
print(times - 1)
print(' '.join(map(str, s))) | 0 | PYTHON3 |
# -*- coding: utf-8 -*-
import math
import sys
from collections import defaultdict
buff_readline = sys.stdin.buffer.readline
readline = sys.stdin.readline
INF = 2**60
class RRH():
class RH():
def __init__(self, s, base, mod):
self.base = base
self.mod = mod
self.rev = pow(base, mod-2, mod)
l = len(s)
self.h = h = [0]*(l+1)
tmp = 0
for i in range(l):
num = ord(s[i])
tmp = (tmp*base + num) % mod
h[i+1] = tmp
self.pw = pw = [1]*(len(s)+1)
v = 1
for i in range(l):
pw[i+1] = v = v * base % mod
def calc(self, l, r):
return (self.h[r] - self.h[l] * self.pw[r-l]) % self.mod
@staticmethod
def gen(a, b, num):
import random
result = set()
while 1:
while 1:
v = random.randint(a, b)//2*2+1
if v not in result:
break
for x in range(3, int(math.sqrt(v))+1, 2):
if v % x == 0:
break
else:
result.add(v)
if len(result) == num:
break
return result
def __init__(self, s, rand=False, num=5):
if rand:
bases = RRH.gen(2, 10**3, num)
else:
assert num <= 10
bases = [641, 103, 661, 293, 547, 311, 29, 457, 613, 599][:num]
MOD = 10**9+7
self.rhs = [self.RH(s, b, MOD) for b in bases]
def calc(self, l, r):
return tuple(rh.calc(l, r) for rh in self.rhs)
class UnionFind():
def __init__(self):
self.__table = {}
self.__size = defaultdict(lambda: 1)
self.__rank = defaultdict(lambda: 1)
def __root(self, x):
if x not in self.__table:
self.__table[x] = x
elif x != self.__table[x]:
self.__table[x] = self.__root(self.__table[x])
return self.__table[x]
def same(self, x, y):
return self.__root(x) == self.__root(y)
def union(self, x, y):
x = self.__root(x)
y = self.__root(y)
if x == y:
return False
if self.__rank[x] < self.__rank[y]:
self.__table[x] = y
self.__size[y] += self.__size[x]
else:
self.__table[y] = x
self.__size[x] += self.__size[y]
if self.__rank[x] == self.__rank[y]:
self.__rank[x] += 1
return True
def size(self, x):
return self.__size[self.__root(x)]
def num_of_group(self):
g = 0
for k, v in self.__table.items():
if k == v:
g += 1
return g
def slv(S, T):
lt = len(T)
ls = len(S)
SS = S * (-(-lt // ls) * 2 + 1)
srh = RRH(SS)
th = RRH(T).calc(0, len(T))
ok = []
for i in range(ls+lt):
if srh.calc(i, i+lt) == th:
ok.append(i)
ok = set(ok)
uf = UnionFind()
for i in ok:
if (i + lt) in ok:
if not uf.union(i, (i+lt) % ls):
return -1
ans = 0
for i in ok:
ans = max(ans, uf.size(i))
return ans
def main():
S = input()
T = input()
print(slv(S, T))
if __name__ == '__main__':
main()
| 0 | PYTHON3 |
a,b,c =map(int,input().split())
gp = sorted([b,c])
_=input()
#bank notes position
bn=list(map(int,input().split()))
if a < gp[0]:
bn = [item for item in bn if item < gp[0]]
elif a>gp[1]:
bn = [item for item in bn if item > gp[1]]
else:
bn = [item for item in bn if item < gp[1] and item > gp[0]]
print(len(bn)) | 7 | PYTHON3 |
import io,os
import bisect
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def main(t):
m,n = map(int,input().split())
arr = list(map(int,input().split()))
temp = []
for i in range(n):
temp.append([-arr[i],i])
already = []
temp = sorted(temp)
ans = 0
for i in range(n):
ans += temp[i][1] - bisect.bisect(already,temp[i][1])
bisect.insort(already,temp[i][1])
print(ans)
T = int(input())
t = 1
while t<=T:
main(t)
t += 1
| 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
char a[110];
int main() {
int n;
scanf("%d", &n);
cin >> a;
int t = n / 11;
int m = 0;
for (int i = 0; i < n; i++) {
if (a[i] == '8') m++;
}
t = min(t, m);
printf("%d", t);
return 0;
}
| 7 | CPP |
class BinaryTree():
def __init__(self):
self.root = None
def insert(self, insert_node):
p = None
r = self.root
while r:
p = r
if insert_node.value < r.value:
r = r.left
else:
r = r.right
if not p:
self.root = insert_node
elif insert_node.value < p.value:
p.left = insert_node
else:
p.right = insert_node
def find(self, find_value):
r = self.root
while r:
if find_value == r.value:
return "yes"
if find_value < r.value:
r = r.left
else:
r = r.right
return "no"
def delete(self, delete_value):
flag = None
p = None
r = self.root
while True:
if delete_value == r.value:
if r.left == None and r.right == None:
if flag == "l":
p.left = None
else:
p.right = None
return
elif r.left == None:
if flag == "l":
p.left = r.right
else:
p.right = r.right
return
elif r.right == None:
if flag == "l":
p.left = r.left
else:
p.right = r.left
return
else:
min_node = r.right
while min_node:
p = min_node
min_node = min_node.left
swap_value = p.value
self.delete(swap_value)
r.value = swap_value
return
elif delete_value < r.value:
p = r
r = r.left
flag = "l"
else:
p = r
r = r.right
flag = "r"
class Node():
def __init__(self, value, left, right):
self.value = value
self.left = left
self.right = right
def pre_order_search(self):
l = self.left
r = self.right
print("",self.value, end="")
if l:
l.pre_order_search()
if r:
r.pre_order_search()
def in_order_search(self):
l = self.left
r = self.right
if l:
l.in_order_search()
print("",self.value, end="")
if r:
r.in_order_search()
m = int(input())
tree = BinaryTree()
for i in range(m):
inp = input().split()
if inp[0] == "insert":
tree.insert(Node(int(inp[1]), None, None))
elif inp[0] == "find":
print(tree.find(int(inp[1])))
elif inp[0] == "delete":
tree.delete(int(inp[1]))
else:
tree.root.in_order_search()
print()
tree.root.pre_order_search()
print() | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
pair<int, pair<int, int> > g[1000];
pair<int, pair<int, int> > t[1000];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> g[i].second.first >> g[i].first;
g[i].second.second = i + 1;
}
sort(g, g + n);
cin >> m;
for (int i = 0; i < m; i++) {
cin >> t[i].first;
t[i].second.first = -1;
t[i].second.second = i + 1;
}
sort(t, t + m);
int r = 0, sum = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
if (g[i].second.first <= t[j].first && t[j].second.first == -1) {
r++;
sum += g[i].first;
t[j].second.first = g[i].second.second;
t[j].first = t[j].second.second;
break;
}
}
}
cout << r << " " << sum << endl;
for (int i = 0; i < m; i++) {
if (t[i].second.first != -1) {
cout << t[i].second.first << " " << t[i].first << endl;
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b;
while (scanf("%lld%lld", &a, &b) != -1) {
long long t, t1;
long long sum = 0;
long long k1, k2;
for (t1 = 1; t1 < b; t1++) {
if (a % 2 == 0) {
k1 = a / 2;
k2 = (a * t1 + t1);
} else {
k1 = (a * t1 + t1) / 2;
k2 = a;
}
long long p = ((((((k1 % 1000000007) * (k2 % 1000000007)) % 1000000007) %
1000000007) *
(b % 1000000007)) %
1000000007 +
(((a % 1000000007) * (t1 % 1000000007)) % 1000000007)) %
1000000007;
sum = sum % 1000000007;
sum = (p + sum) % 1000000007;
}
cout << sum << endl;
}
}
| 7 | CPP |