solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 0;
cin >> n;
int a[100];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
count++;
}
}
if (count == n) {
cout << "EASY";
} else {
cout << "HARD";
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, l, k;
long long a[1000000];
vector<int> vals[5000001];
long long stor[5000000 + 1];
const int MOD = 1000000000 + 7;
inline long long& dp(int i, int j) { return stor[i * n + j]; }
long long func(int i, int j) {
if (j < 0) return 0;
if (i == 0) return 0 + max(j + 1, 0);
if (dp(i, j) != -1) return dp(i, j);
if (j != 0)
dp(i, j) = func(i, j - 1);
else
dp(i, j) = 0;
int ni = upper_bound(vals[i - 1].begin(), vals[i - 1].end(), vals[i][j]) -
vals[i - 1].begin();
dp(i, j) = (dp(i, j) + func(i - 1, ni - 1)) % MOD;
return dp(i, j);
}
long long func2(int i, int j) {
if (j < 0) return 1;
if (i == 0) return 1 + max(j + 1, 0);
if (dp(i, j) != -1) return dp(i, j);
if (j != 0)
dp(i, j) = func2(i, j - 1);
else
dp(i, j) = 1;
int ni = upper_bound(vals[i - 1].begin(), vals[i - 1].end(), vals[i][j]) -
vals[i - 1].begin();
dp(i, j) = (dp(i, j) + func2(i - 1, ni - 1)) % MOD;
return dp(i, j);
}
int main() {
std::ios::sync_with_stdio(false);
memset(stor, -1, sizeof stor);
cin >> n >> l >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
for (long long i = 0; i < l && i < k * n; ++i)
vals[i / n].push_back(a[i % n]);
for (int i = 0; i < k; ++i) sort(vals[i].begin(), vals[i].end());
long long ans = 0;
for (int i = 0; i < k; ++i) {
if (vals[i].size() != 0)
ans = (ans + func(i, vals[i].size() - 1) * ((l / n - i) % MOD)) % MOD;
}
if (l % n != 0) {
memset(stor, -1, sizeof stor);
for (int i = 0; i < k; ++i) vals[i].clear();
int v = 0;
bool add = false;
for (long long i = max(l / n - k + 1, 0ll) * n; i < l; ++i) {
add = false;
vals[v].push_back(a[i % n]);
if ((i + 1) / n > i / n) {
++v;
add = true;
}
}
if (add) --v;
for (int i = 0; i <= v; ++i) sort(vals[i].begin(), vals[i].end());
for (int i = 0; i <= v; ++i) func2(i, vals[i].size() - 1);
ans = (ans + func2(v, vals[v].size() - 1) + MOD - 1) % MOD;
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
w = int(input())
def determine_equal(weight):
for i in range(1, weight):
for k in range(weight, 1, -1):
if k == weight - i and k == i:
return "YES"
return "NO"
result = determine_equal(w)
print(result)
| 7 | PYTHON3 |
#include <iostream>
#include <vector>
using namespace std;
void solve()
{
int N, K;
while(cin >> N >> K, N || K)
{
vector<int> blood(K);
for(int i = 0; i < K; ++i)
{
cin >> blood[i];
}
vector<int> sum(K);
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < K; ++j)
{
int require;
cin >> require;
sum[j] += require;
}
}
bool flag = true;
for(int i = 0; i < K; ++i)
{
if(sum[i] > blood[i])
{
flag = false;
break;
}
}
if(flag)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
}
int main()
{
solve();
return(0);
} | 0 | CPP |
n,d = input().split(' ')
n = int(n)-1
d = int(d)
s = input()
curr_index = 0
jump_counter = 0
while curr_index<n:
try:
curr_index = s.rindex('1',curr_index+1, curr_index+1+d)
jump_counter +=1
except ValueError:
print(-1)
break
if curr_index >=n:
print(jump_counter)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); }
const long long maxn = 2e6 + 200;
long long n, m, k, l, r;
vector<long long> arr(maxn, 0);
vector<long long> segTree(5 * maxn, 0);
void constructTree(long long low, long long high, long long position) {
if (low == high) {
segTree[position] = arr[low];
} else {
long long mid = (low + high) / 2;
constructTree(low, mid, (2 * position));
constructTree(mid + 1, high, (2 * position) + 1);
segTree[position] = (segTree[(2 * position)] + segTree[(2 * position) + 1]);
}
}
long long query(long long a, long long b, long long current) {
if (a > b) {
return 0;
}
if (a > r || b < l) {
return 0;
}
if (a >= l && b <= r) {
return segTree[current];
}
long long mid = (a + b) / 2;
return query(a, mid, (2 * current)) + query(mid + 1, b, (2 * current) + 1);
}
void update(long long a, long long b, long long current, long long pos) {
if (a > pos || b < pos) {
return;
}
if (a >= pos && b <= pos) {
segTree[current] = 0;
return;
}
long long mid = (a + b) / 2;
update(a, mid, 2 * current, pos);
update(mid + 1, b, 2 * current + 1, pos);
segTree[current] = segTree[2 * current] + segTree[2 * current + 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
vector<long long> ip;
for (long long i = 0; i < n; i++) {
long long val;
cin >> val;
ip.push_back(val);
arr[val] = 1;
}
sort(ip.begin(), ip.end());
constructTree(1, 2e6 + 1, 1);
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (arr[ip[i]] == 0) {
continue;
}
l = ip[i];
r = l + m - 1;
int curr = query(1, 2e6 + 1, 1);
if (curr < k) {
continue;
}
curr -= (k - 1);
ans += curr;
long long posi = upper_bound(ip.begin(), ip.end(), r) - ip.begin();
for (long long j = posi - 1; j >= posi - curr; j--) {
arr[ip[j]] = 1;
update(1, 2e6 + 1, 1, ip[j]);
}
}
cout << ans;
return 0;
}
| 10 | CPP |
n, k = map(int, input().split())
import sys
sys.setrecursionlimit(2000)
l = [[] for i in range(n)]
e = []
for i in range(n-1):
a, b = map(int, input().split())
l[a-1].append(b-1)
l[b-1].append(a-1)
e.append([a-1, b-1])
def first_search(first, depth):
record = 0
for i in l[first]:
temp = search(first, i, depth-1)
if record < temp:
record = temp
return record + 1
def search(before, now, depth):
if depth <= 0:
return 1
else:
ans = 1
for i in l[now]:
if before != i:
ans += search(now, i, depth-1)
return ans
ret = 0
if k%2 == 0:
for i in range(n):
temp = search(-1, i, k//2)
if temp > ret:
ret = temp
else:
for i in e:
temp = search(i[0], i[1], k//2) + search(i[1], i[0], k//2)
if temp > ret:
ret = temp
print(n-ret) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 <= x2 && y1 <= y2) {
cout << "Polycarp\n";
} else {
int vas = max(x2, y2);
int pat = x1 + y1;
if (vas < pat) {
cout << "Vasiliy\n";
} else {
cout << "Polycarp\n";
}
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(1000 * 1000 + 1, 0), b(1000 * 1000 + 3, 0);
int x = 1000 * 1000, y = 0;
for (int i = 0; i < n; i++) {
int t, q;
cin >> t >> q;
a[t] = q;
if (t < x) x = t;
if (t > y) y = t;
}
int ans = 0;
for (int i = x; i <= y + 1; i++) {
if (a[i] != 0) {
b[i + 1] = 1 + b[max(i - a[i], 0)];
} else {
b[i + 1] = b[i];
}
if (b[i + 1] > ans) ans = b[i + 1];
}
cout << n - ans;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 504;
const int M = 20100;
struct QQ {
int u, v;
} eg[N * N];
int ll[M][N], rr[M][N], ans[N];
int f(int u, int x[]) { return u == x[u] ? u : x[u] = f(x[u], x); }
void uni(int u, int v, int x[]) { x[f(u, x)] = f(v, x); }
int main() {
int n, m;
while (~scanf("%d %d", &n, &m)) {
for (int i = 1; i <= n; i++) ll[0][i] = rr[m + 1][i] = i;
for (int i = 1; i <= m; i++) {
scanf("%d %d", &eg[i].u, &eg[i].v);
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) ll[i][j] = ll[i - 1][j];
uni(eg[i].u, eg[i].v, ll[i]);
}
for (int i = m; i >= 1; i--) {
for (int j = 1; j <= n; j++) rr[i][j] = rr[i + 1][j];
uni(eg[i].u, eg[i].v, rr[i]);
}
int nQ;
scanf("%d", &nQ);
for (int i = 1; i <= nQ; i++) {
int l, r;
scanf("%d %d", &l, &r);
for (int j = 1; j <= n; j++) {
ans[j] = j;
}
for (int j = 1; j <= n; j++) {
uni(j, f(ll[l - 1][j], ll[l - 1]), ans);
uni(j, f(rr[r + 1][j], rr[r + 1]), ans);
}
int cnt = 0;
for (int j = 1; j <= n; j++)
if (f(j, ans) == j) cnt++;
printf("%d\n", cnt);
}
}
return 0;
}
| 10 | CPP |
def answer(n,k,A):
for i in range(k):
if A[i]=="?":
j=i+k
while j<n:
if A[j]!="?":
A[i]=A[j]
break
j+=k
for i in range(k):
if A[i]!="?":
j=i+k
while j<n:
if A[j]=="?":
A[j]=A[i]
elif A[i]!=A[j]:
return "NO"
j+=k
count0=0
count1=0
for i in range(n):
if i<k:
if A[i]=="0":
count0+=1
if A[i]=="1":
count1+=1
else:
if max(count0,count1)>k//2:
return "NO"
if A[i-k]=="1":
count1-=1
if A[i-k]=="0":
count0-=1
if A[i]=="0":
count0+=1
if A[i]=="1":
count1+=1
if max(count0,count1)>k//2:
return "NO"
return "YES"
t=int(input())
for i in range(t):
n,k=map(int,input().split())
s=list(input())
print(answer(n,k,s)) | 7 | PYTHON3 |
for _ in range(int(input())):
n = int(input())
p = list(map(int, input().split()))
for x in reversed(p):
print(x, end=" ")
print()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int cond = 1;
int main() {
int n, m;
vector<pair<int, int>> v1, v2;
scanf("%d%d", &n, &m);
for (int i = 0; i < (n); i++) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
v1.push_back({a + c, i});
v2.push_back({b + d, i});
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
long long result = 1230000000;
result *= result;
for (int i = 0; i < (11); i++) {
for (int j = 0; j < (11); j++) {
for (int k = 0; k < (11); k++) {
for (int l = 0; l < (11); l++) {
if (i + j + k + l == m) {
set<int> to_rem;
{
if (cond) {
cerr << "Line:" << 74 << ", "
<< "i"
<< " = " << i << ", "
<< "j"
<< " = " << j << ", "
<< "k"
<< " = " << k << ", "
<< "l"
<< " = " << l << endl;
}
};
int p1 = i;
int p2 = v1.size() - j - 1;
int p3 = k;
int p4 = v2.size() - l - 1;
for (int x = (0); x <= (p1 - 1); x++) {
to_rem.insert(v1[x].second);
}
for (int x = (p2 + 1); x <= (v1.size() - 1); x++) {
to_rem.insert(v1[x].second);
}
for (int x = (0); x <= (p3 - 1); x++) {
to_rem.insert(v2[x].second);
}
for (int x = (p4 + 1); x <= (v2.size() - 1); x++) {
to_rem.insert(v2[x].second);
}
while (p1 < v1.size() && to_rem.count(v1[p1].second)) {
p1++;
}
while (p2 > 0 && to_rem.count(v1[p2].second)) {
p2--;
}
while (p3 < v2.size() && to_rem.count(v2[p3].second)) {
p3++;
}
while (p4 > 0 && to_rem.count(v2[p4].second)) {
p4--;
}
long long be = 0;
if (p1 <= p2) {
be = v1[p2].first - v1[p1].first;
}
long long en = 0;
if (p3 <= p4) {
en = v2[p4].first - v2[p3].first;
}
if (be == 0) be = 1;
if (en == 0) en = 1;
{
if (cond) {
cerr << "Line:" << 105 << ", "
<< "p1"
<< " = " << p1 << ", "
<< "p2"
<< " = " << p2 << ", "
<< "p3"
<< " = " << p3 << ", "
<< "p4"
<< " = " << p4 << endl;
}
};
{
if (cond) {
cerr << "Line:" << 106 << ", "
<< "be"
<< " = " << be << ", "
<< "en"
<< " = " << en << endl;
}
};
result = min(result, ((be + 1) / 2) * ((en + 1) / 2));
}
}
}
}
}
cout << result << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, k;
cin >> n >> k;
long long arr[n];
for (int i = (int)(0); i <= (int)(n - 1); i++) {
cin >> arr[i];
}
long long pref[n];
pref[0] = arr[0];
for (int i = (int)(1); i <= (int)(n - 1); i++) {
pref[i] = pref[i - 1] + arr[i];
}
long long maxi = pref[n - 1] - pref[n - k - 1];
long long ans = pref[n - 1] - pref[n - 2 * k - 1];
long long a = n - 2 * k + 1, b = n - k + 1;
long long i = n - 2 * k - 1;
long long b1 = b;
while (i >= 0) {
if (maxi <= pref[i + 2 * k - 1] - pref[i + k - 1]) {
long long val = 0;
if (i == 0) {
val = pref[i + k - 1] + pref[i + 2 * k - 1] - pref[i + k - 1];
} else {
val = pref[i + k - 1] - pref[i - 1] + pref[i + 2 * k - 1] -
pref[i + k - 1];
}
if (val >= ans) {
a = i + 1;
ans = val;
maxi = pref[i + 2 * k - 1] - pref[i + k - 1];
b = i + k + 1;
b1 = i + k + 1;
} else {
maxi = pref[i + 2 * k - 1] - pref[i + k - 1];
b1 = i + k + 1;
}
} else {
long long val = 0;
if (i == 0) {
val = pref[i + k - 1] + maxi;
} else {
val = pref[i + k - 1] - pref[i - 1] + maxi;
}
if (ans <= val) {
a = i + 1;
ans = val;
b = b1;
}
}
i--;
}
cout << a << " " << b << "\n";
}
| 8 | CPP |
#include<bits/stdc++.h>
using namespace std;
#define N 300000
const int mod=1000000007;
int n,m,p,a[N],b[N],bit[N],f[N],x[N],y[N];
struct node{
int x,y;
node(int x=0,int y=0):x(x),y(y){}
bool operator < (const node &p) const{
return x<p.x||(x==p.x&&y>p.y);
}
bool operator != (const node &p) const{
return x!=p.x||y!=p.y;
}
}c[N];
void upd(int &x,int y){x=(x+y)%mod;}
void add(int k,int x){
for (;k<=p;k+=k&(-k)) upd(bit[k],x);
}
int qry(int k){
int ret=0;
for (;k;k-=k&(-k)) upd(ret,bit[k]);
return ret;
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) scanf("%d",a+i);
for (int i=1;i<=m;++i) scanf("%d",b+i);
for (int i=1,j=1;i<=n;++i){
for (;j<=m&&a[i]>b[j];++j);
if (j>1&&j<=m){
c[++p]=node(a[i]-b[j-1],b[j]-a[i]);
x[p]=a[i]-b[j-1];
y[p]=b[j]-a[i];
}
}
sort(x+1,x+p+1); sort(y+1,y+p+1);
for (int i=1;i<=p;++i)
c[i]=node(lower_bound(x+1,x+p+1,c[i].x)-x,lower_bound(y+1,y+p+1,c[i].y)-y);
sort(c+1,c+p+1);
for (int i=1;i<=p;++i)
if (c[i]!=c[i-1]) add(c[i].y,qry(c[i].y-1)+1);
printf("%d\n",(qry(p)+1)%mod);
return 0;
}
| 0 | CPP |
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
l = list(map(int,input().split()))
a1,b1 = 0,0
even,odd = [],[]
for i in range(n):
if l[i]&1:
odd.append(l[i])
else:
even.append(l[i])
even.sort(reverse=True)
odd.sort(reverse=True)
c = 0
i,j = 0,0
while i < len(even) or j < len(odd) :
if c%2 == 0:
if i >= len(even):
j+=1
elif j >= len(odd):
a1 += even[i]
i+=1
else:
if even[i] < odd[j]:
a1 += 0
j+=1
else:
a1+=even[i]
i+=1
else:
if j >= len(odd):
i+=1
elif i >= len(even):
b1 += odd[j]
j+=1
else:
if even[i] > odd[j]:
b1 += 0
i+=1
else:
b1 += odd[j]
j+=1
c+=1
if a1 == b1:
print("Tie")
elif a1 > b1:
print("Alice")
else:
print("Bob")
| 10 | PYTHON3 |
n=int(input())
e=[]
o=[]
if(n==1 or n==2):
print("No")
elif(n>2):
for i in range (1,n+1):
if(i%2==0):
e.append(i)
else:
o.append(i)
print("Yes")
print(len(e),end=" ")
for i in e:
print(i,end=" ")
print()
print(len(o),end=" ")
for i in o:
print(i,end=" ") | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool chk[1002];
vector<int> v;
int a[1002], b[1002];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, num;
cin >> n;
int k = 1;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) {
if (!chk[a[i]]) {
chk[a[i]] = 1;
b[k++] = a[i];
}
}
k--;
cout << k << "\n";
for (int i = k; i >= 1; i--) cout << b[i] << " ";
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int dp[105];
int main() {
int n;
scanf("%d", &n);
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + 4 * (i - 1);
}
printf("%d\n", dp[n]);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct compare {
bool operator()(const int64_t& a, const int64_t& b) { return a > b; }
};
bool customS(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
bool customS2(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) {
return (a.first) < (b.first);
}
class CompareDist {
public:
bool operator()(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) {
return a.first < b.first;
}
};
int64_t gcd(int64_t a, int64_t b) {
if (b == 0) return a;
return gcd(b, a % b);
}
double gcdfloat(double a, double b) {
if (a < b) return gcd(b, a);
if (fabs(b) < 0.001)
return a;
else
return (gcd(b, a - floor(a / b) * b));
}
int64_t LCA(int64_t a, int64_t b) {
while (a != b) {
if (b > a) {
swap(a, b);
}
a /= 2;
}
return a;
}
vector<int64_t> ra(int64_t n) {
vector<int64_t> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
return arr;
}
bool cmp(string s1, string s2) { return s1.length() < s2.length(); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64_t i, j, a = 0, b = 0, c = 0, d = 0, n, m, k, sum = 0, x = 0, y = 0,
z = 0, flag = 0, zero = 0, ans = 0;
string s, newS;
cin >> n >> k;
vector<pair<int64_t, int64_t> > arr(n);
for (i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;
sort(arr.begin(), arr.end(), customS);
i = 0;
vector<int> res;
while (i < n) {
j = i + 1;
while (j < n && arr[j].first == arr[i].first &&
arr[j].second == arr[i].second)
j++;
res.push_back(j - i);
i = j;
}
for (i = 0; i < res.size(); i++) {
if (i > 0) res[i] += res[i - 1];
if (res[i] >= k) {
if (i > 0)
cout << res[i] - res[i - 1] << endl;
else
cout << res[i] << endl;
break;
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
double x[(int)2e3 + 10], y[(int)2e3 + 10];
long long nCr(long long n, long long r) {
if (r == 0) return 1;
if (r == 1) return n;
if (n == r) return 1;
return nCr(n - 1, r) + nCr(n - 1, r - 1);
}
int main() {
long long n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> x[i] >> y[i];
}
if (n < 3) return cout << 0 << endl, 0;
long long ans = nCr(n, 3);
for (int i = 0; i < n; ++i) {
map<double, int> m;
for (int j = i + 1; j < n; ++j) {
if (x[i] == x[j])
m[1e9]++;
else if (y[i] > y[j])
m[(y[i] - y[j]) / (x[i] - x[j])]++;
else
m[(y[j] - y[i]) / (x[j] - x[i])]++;
}
map<double, int>::iterator it;
for (it = m.begin(); it != m.end(); ++it) {
ans -= (it->second * (it->second - 1)) / 2;
}
}
cout << ans << endl;
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long ooo = 9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N = 200005;
const double PI = acos(-1.0);
const double eps = 1e-9;
int o(int x) { return x % _p; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void file_put() {
freopen("filename.in", "r", stdin);
freopen("filename.out", "w", stdout);
}
long long Mul(long long a, long long b, long long p) {
long long ans = 0;
for (; b; b >>= 1, a += a, a -= (a >= p) * p)
if (b & 1) ans += a, ans -= (ans >= p) * p;
return ans;
}
long long Pow(long long x, long long k, long long p) {
long long ans = 1;
for (; k; k >>= 1, x = Mul(x, x, p))
if (k & 1) ans = Mul(ans, x, p);
return ans;
}
int G(long long x) {
long long ans = 0;
while (x) x /= 10, ans++;
return ans;
}
int T, Log2[] = {-1, 0, 1, 3, 2};
long long x, y, a, b, c, t, k, p, n, m = 6;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%I64d", &a);
n = G(a), a *= 1000000;
b = ((-a) & ((1ll << n + m) - 1));
if ((a + b) % 5 == 0) b += (1ll << n + m);
if (!m && !b || G(b) <= m) {
x = a + b, y = x >> n + m, t = Log2[y % 5], p = 5;
for (int i = 2; i <= n + m; i++, p *= 5)
while (Pow(2, t, p * 5) != y % (p * 5)) t += p / 5 * 4;
t += n + m;
}
printf("%I64d\n", t);
}
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long put(long long baza, long long exp) {
if (exp == 0) return 1;
long long ans = put(baza, exp / 2);
ans *= ans;
ans %= 998244353;
if (exp % 2) {
ans *= baza;
ans %= 998244353;
}
return ans;
}
long long n, m, i, cbig, csma, ans, p, big, a, b, sum;
long long v[200005];
long long s[200005];
int main() {
cin >> n >> m;
for (i = 1; i <= n; i++) cin >> v[i];
sort(v + 1, v + n + 1);
for (i = 1; i <= n; i++) {
s[i] = s[i - 1] + v[i];
s[i] %= 998244353;
}
for (i = 1; i <= m; i++) {
cin >> a >> b;
p = lower_bound(v + 1, v + n + 1, b) - v;
big = n - p + 1;
if (a > big) {
cout << "0\n";
continue;
}
cbig = 1;
cbig -= (a * put(big, 998244353 - 2)) % 998244353;
if (cbig < 0) cbig += 998244353;
csma = 1;
csma -= (a * put(big + 1, 998244353 - 2)) % 998244353;
if (csma < 0) csma += 998244353;
sum = (s[n] - s[p - 1]) % 998244353;
if (sum < 0) sum += 998244353;
ans = (sum * cbig) % 998244353;
ans += (s[p - 1] * csma) % 998244353;
if (ans >= 998244353) ans -= 998244353;
cout << ans << '\n';
}
return 0;
}
| 11 | CPP |
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
if(k==0){
printf("Yes\n");
for(int i=1;i<=b;++i)
printf("1");
for(int i=1;i<=a;++i)
printf("0");
printf("\n");
for(int i=1;i<=b;++i)
printf("1");
for(int i=1;i<=a;++i)
printf("0");
return 0;
}
if(a==0){
if(k==0){
printf("Yes\n");
for(int i=0;i<2;++i){
for(int j=1;j<=b;++j){
printf("1");
}
printf("\n");
}
}else{
printf("No");
}
return 0;
}
if(b==1){
if(k==0){
printf("Yes\n");
printf("1");
for(int i=1;i<=a;++i)
printf("0");
printf("\n");
printf("1");
for(int i=1;i<=a;++i)
printf("0");
}else{
printf("No");
}
return 0;
}
if(k>a+b-2){
printf("No");
}else{
printf("Yes\n");
int a1=1,a2=1;
a1=max(a1,b-k),a2=max(a2,b-k);//1
int b1=a+b-k-1-a1,b2=a+b-k-a1;//0
//�ǵ�b1��print 1
int c1=0,c2=0;
c1=max(c1,k+1-b),c2=c1;//0
int d1=b-a1-1,d2=b-a2;
//�ǵ�d1��print1
for(int i=1;i<=a1;++i)
printf("1");
for(int i=1;i<=b1;++i)
printf("0");
printf("1");
for(int i=1;i<=c1;++i)
printf("0");
for(int i=1;i<=d1;++i)
printf("1");
printf("0\n");
for(int i=1;i<=a2;++i)
printf("1");
for(int i=1;i<=b2;++i)
printf("0");
for(int i=1;i<=c2;++i)
printf("0");
for(int i=1;i<=d2;++i)
printf("1");
}
return 0;
}
| 10 | CPP |
from sys import stdin
input=lambda:stdin.readline().strip()
n=int(input())
o=0
i=0
sum1=0
k=n
while i<n:
sum1+=(k-1)+(k-1)*o+1
o+=1
i+=1
k-=1
print(sum1)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int L[1024], op, ed;
string Map[8];
bool Can[1024], In[1024];
vector<int> e[1024];
int main() {
for (int i = 0; i < 8; ++i) cin >> Map[i];
Map[7][0] = '.';
Map[0][7] = '.';
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j)
for (int k = 0; k < 8; ++k)
if (Map[j][k] == 'S') Can[i * 64 + j * 8 + k] = 1;
for (int j = 7; j >= 0; --j)
for (int k = 0; k < 8; ++k)
if (Map[j][k] == 'S') {
Map[j][k] = '.';
if (j < 7) Map[j + 1][k] = 'S';
}
}
for (int i = 0; i < 7; ++i)
for (int j = 0; j < 8; ++j)
for (int k = 0; k < 8; ++k)
for (int l = 0; l < 8; ++l)
for (int m = 0; m < 8; ++m)
if (!Can[i * 64 + j * 8 + k] && !Can[i * 64 + l * 8 + m + 64] &&
!Can[i * 64 + l * 8 + m])
if (abs(l - j) <= 1 && abs(k - m) <= 1)
e[i * 64 + j * 8 + k].push_back(i * 64 + l * 8 + m + 64);
for (In[L[op = ed = 0] = 56] = 1; op <= ed; ++op)
for (vector<int>::iterator v = e[L[op]].begin(); v != e[L[op]].end(); v++)
if (!In[*v]) In[L[++ed] = *v] = 1;
bool ans = 0;
for (int i = 0; i < 64; ++i)
if (In[64 * 7 + i]) ans = 1;
if (ans)
cout << "WIN" << endl;
else
cout << "LOSE" << endl;
return 0;
}
| 7 | CPP |
from sys import *
n=int(stdin.readline())
s=stdin.readline()
m1,m2,ans=0,0,0
for i in range(len(s)):
if i%2 and s[i]=="r":
m1+=1
if not (i%2) and s[i]=="b":
m2+=1
ans=max(m1,m2)
m1,m2=0,0
for i in range(len(s)):
if i%2 and s[i]=="b":
m1+=1
if not (i%2) and s[i]=="r":
m2+=1
print(min(ans,max(m1,m2))) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 110;
int K, N;
double x[SIZE], y[SIZE];
double dp[SIZE][SIZE], rang, pro;
void read() {
scanf("%d", &N);
scanf("%d%lf", &K, &pro);
pro /= 1000;
scanf("%lf%lf", &x[0], &y[0]);
for (int i = (1); i <= (N); i++) scanf("%lf%lf", &x[i], &y[i]);
}
double calc(int k) {
double a = x[k] - x[0];
a = a * a;
double b = y[k] - y[0];
b = b * b;
double ln = sqrt(a + b);
if (ln <= rang)
return 1;
else
return exp(1 - ln * ln / (rang * rang));
}
double DP() {
memset(dp, 0, sizeof(dp));
dp[1][0] = 1;
for (int i = (1); i <= (N); i++)
for (int j = (0); j <= (K); j++) {
if (dp[i][j] == 0) continue;
double pi = calc(i);
dp[i + 1][j + 1] += dp[i][j] * pi;
dp[i + 1][j] += dp[i][j] * (1 - pi);
}
double sum = 0;
for (int i = (0); i <= (K - 1); i++) sum += dp[N + 1][i];
return sum;
}
void solve() {
double l = 0.0, r = 4000.0, sum;
while (r - (1E-7) >= l) {
double mid = (l + r) / 2;
rang = mid;
if (DP() <= pro)
sum = mid, r = mid - (1E-7);
else
l = mid + (1E-7);
}
printf("%.7lf\n", sum);
}
int main() {
read();
solve();
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
int n, m, tot = 0, l = 0, r = 0, T, ans;
int a[200002], b[200002];
inline int min(int a, int b) { return a < b ? a : b; }
int main() {
scanf("%d%d", &n, &m), ans = n;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= m; ++i) scanf("%d", &b[i]), tot += b[i];
T = tot;
while (tot && (++r) <= n)
if ((--b[a[r]]) >= 0) --tot;
if (tot) return 0 & puts("-1");
while (r <= n) {
while (!tot && (++l) <= r)
if ((++b[a[l]]) > 0) ++tot;
ans = min(ans, r - l + 1);
while (tot && (++r) <= n)
if ((--b[a[r]]) >= 0) --tot;
}
printf("%d", ans - T);
return 0;
}
| 12 | CPP |
def answer():
n=int(input())
s = list(input())
i=0
h = [0,0,0, 0,0,0, 0,0,0, 0]
while i<len(s):
if s[i]=="L":
j=0
while j<len(h):
if h[j]==1:
pass
else:
h[j]=1
break
j+=1
elif s[i]=="R":
j=len(h)-1
while j>=0:
if h[j]==1:
pass
else:
h[j]=1
break
j-=1
else:
z=int(s[i])
h[z]=0
i+=1
ans=""
for x in h:
ans+=str(x)
return ans
print(answer()) | 7 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void solve() {
long long n;
cin >> n;
long long arr[n];
map<long long, long long> m;
long long psum[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (i == 0) {
if (arr[i] >= 0)
psum[i] = arr[i];
else
psum[i] = 0;
} else {
if (arr[i] >= 0)
psum[i] = arr[i] + psum[i - 1];
else
psum[i] = psum[i - 1];
}
}
long long res = -1e18;
pair<long long, long long> index;
for (int i = 0; i < n; i++) {
if (m.find(arr[i]) != m.end()) {
long long sum = psum[i];
if (arr[i] < 0) {
sum += 2 * arr[i];
sum -= psum[m[arr[i]]];
} else {
if (m[arr[i]] > 0) sum -= psum[m[arr[i]] - 1];
}
if (res <= sum) {
res = sum;
index.first = m[arr[i]];
index.second = i;
}
} else {
m[arr[i]] = i;
}
}
cout << res << " ";
vector<long long> k;
for (int i = 0; i < n; i++) {
if (i < index.first or i > index.second)
k.push_back(i + 1);
else if (i > index.first and i < index.second and arr[i] < 0)
k.push_back(i + 1);
}
cout << k.size() << "\n";
for (auto x : k) cout << x << " ";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) solve();
return 0;
}
| 7 | CPP |
t = int(input())
for i in range(0,t):
k = input()
s = input()
max = 0 ;
lst = s.split("A")
for j in range(1, len(lst)):
sub = lst[j]
if (len(sub)>max):
if(sub[0]=='P'):
max = len(sub)
print(max)
| 7 | PYTHON3 |
k=int(input('').split(' ')[1])
s=input('').split(' ')
n=[]
for i in s:
if int(i)>0 and int(i)>=int(s[k-1]):
n.append(i)
print(len(n)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline long toInt(string s) {
long v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
template <class T>
inline T sqr(T x) {
return x * x;
}
long long pw(long long x, long long y) {
long long ans = 1;
for (long long i = 0; i < y; i++) {
ans = ans * x;
}
return ans;
}
long long factorial(long long x) {
long long ans = 1;
for (int i = 1; i < x + 1; i++) {
ans = ans * i;
}
return ans;
}
long long combination(long long x, long long y) {
return factorial(x) / factorial(y) / factorial(x - y);
}
long long permutation(long long x, long long y) {
return factorial(x) / factorial(x - y);
}
string flip(string s) {
string ss;
for (int i = 0; i < s.size(); i++) {
ss = toString(s[i]) + ss;
}
return ss;
}
long long mod(long long a, long long b) {
if (a % b == 0) {
return 0;
} else {
return a % b;
}
}
int main() {
long long n;
cin >> n;
vector<long long> v(n);
long long a, b;
for (long long i = 0; i < n - 1; i++) {
cin >> a >> b;
v[a - 1]++;
v[b - 1]++;
}
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (v[i] >= 2) {
ans += combination(v[i], 2);
}
}
cout << ans << endl;
return 0;
}
| 12 | CPP |
#include<bits/stdc++.h>
using namespace std;
int n, m;
int main()
{
cin >> n >> m;
cout << (n*(n-1)+m*(m-1))/2 << endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
void turn(long long x, long long y, long long z) {
long long a = min(x, min(y, z));
long long c = max(x, max(y, z));
long long b = x + y + z - a - c;
long long add = 2 * c - a - b;
cout << add << endl;
long long pos;
cin >> pos;
if (pos == 1) {
if (x == c) {
turn(x + add, y, z);
} else {
cout << abs(z - y) << endl;
}
} else if (pos == 2) {
if (y == c) {
turn(x, y + add, z);
} else {
cout << abs(z - x) << endl;
}
} else if (pos == 3) {
if (z == c) {
turn(x, y, z + add);
} else {
cout << abs(y - x) << endl;
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long x, y, z;
cin >> x >> y >> z;
cout << "First" << endl;
turn(x, y, z);
return 0;
}
| 12 | CPP |
for t in range(int(input())):
n = int(input())
count = 0
size = 1
amo = 1
while n >= amo:
n -= amo
count += 1
size = size * 2 + 1
amo = (size * (size + 1)) // 2
print(count)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
string dp2[310][310];
int dp[310][310];
int main()
{
string s;
while (cin >> s){
if (s == "#END")return 0;
int res = 0, id = 0;
for (int k = 1; k < s.length(); ++k){
string s1 = s.substr(0, k);//从头到k的子序列
string s2 = s.substr(k);//从k往后的子序列
memset(dp, 0, sizeof dp);
for (int i = 0; i < s1.length(); ++i){
for (int j = 0; j < s2.length(); ++j){
if (s1[i] == s2[j])dp[i + 1][j + 1] = dp[i][j] + 1;
else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
}
}
if (res < dp[s1.length()][s2.length()]){
res = dp[s1.length()][s2.length()];
id = k;
}
}
//从id处分隔字符串
string s1 = s.substr(0, id);
string s2 = s.substr(id);
for (int i = 0; i <= s1.length(); ++i){
for (int j = 0; j <= s2.length(); ++j)dp2[i][j] = "";
}
for (int i = 0; i < s1.length(); ++i){
for (int j = 0; j < s2.length(); ++j){
if (s1[i] == s2[j])dp2[i + 1][j + 1] = dp2[i][j] + s1[i];
else{
if (dp2[i][j + 1].size() <= dp2[i + 1][j].size())dp2[i + 1][j + 1] = dp2[i + 1][j];
if (dp2[i][j + 1].size() > dp2[i + 1][j].size())dp2[i + 1][j + 1] = dp2[i][j + 1];
}
}
}
cout << dp2[s1.size()][s2.size()] << endl;
}
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int len = 1000010;
char s[len];
int sum[30];
int n, k, res;
int en[len];
int main() {
bool f = false;
scanf("%d%d", &n, &k);
scanf("%s", s);
res = 0;
for (int i = strlen(s) - 1; i >= 0; i--) {
int p = s[i] - 'A' + 1;
sum[p]++;
if (!en[p]) {
en[p] = i;
}
}
for (int i = 1; i <= 26; i++) {
if (sum[i]) res++;
}
if (res <= k) {
printf("NO\n");
} else {
int m = 0;
memset(sum, 0, sizeof(sum));
for (int i = 0; i < strlen(s); i++) {
int p = s[i] - 'A' + 1;
if (i < en[p] && sum[p] == 0) {
m++;
sum[p] = 1;
} else if (i == en[p] && sum[p] == 1) {
m--;
} else if (i == en[p] && sum[p] == 0) {
m++;
if (m > k) {
printf("YES\n");
return 0;
}
m--;
}
if (m > k) {
printf("YES\n");
return 0;
}
}
printf("NO\n");
}
return 0;
}
| 8 | CPP |
a, b, c, d = (int(i) for i in input().split())
print(max(min(b,d)-max(a,c),0)) | 0 | PYTHON3 |
n=int(input())
if n==1 or n==2:
print(1)
print(1)
elif n==3:
print(2)
print(1,end=" ")
print(3)
elif n==4:
print(4)
print(2,end=" ")
print(4,end=" ")
print(1,end=" ")
print(3,end=" ")
else:
print(n)
for i in range (1,n+1,2):
print(i,end=" ")
for i in range (2,n+1,2):
print(i,end=" ") | 7 | PYTHON3 |
#include <bits/stdc++.h>
const int MN = 500005, MM = 500005, MQ = 500005, MS = 1 << 20 | 7, MK = 51;
int N, M, K, Q;
int ea[MM], eb[MM], ncol[MM];
int qe[MQ], qc[MQ];
std::map<int, int> mpe;
int mpq[MK][MM], fa[MQ * 2], rk[MQ * 2], tg[MQ * 2], cnt;
int st[MM], tp;
inline void gvtx(int i, int &a, int &b) {
a = mpq[qc[i]][ea[qe[i]]], b = mpq[qc[i]][eb[qe[i]]];
}
inline int ff(int x) {
while (fa[x]) x = fa[x];
return x;
}
inline int gcol(int x, int &c) {
c = tg[x];
while (fa[x]) x = fa[x], c ^= tg[x];
return x;
}
inline void merge(int x, int y) {
int cx, cy, o;
x = gcol(x, cx), y = gcol(y, cy), o = cx ^ cy ^ 1;
if (x == y) return;
if (rk[x] < rk[y]) std::swap(x, y);
rk[x] += rk[y], fa[y] = x, tg[y] ^= o, st[++tp] = (o ? -1 : 1) * y;
}
inline void undo1() {
int o = st[tp] < 0 ? 1 : 0, y = (o ? -1 : 1) * st[tp];
rk[fa[y]] -= rk[y], tg[y] ^= o, fa[y] = 0, --tp;
}
inline void undok(int k) {
while (k--) undo1();
}
std::vector<int> vq[MS];
void Ins(int i, int l, int r, int a, int b, int x) {
if (r < a || b < l) return;
if (a <= l && r <= b) return vq[i].push_back(x);
Ins(i << 1, l, (l + r) >> 1, a, b, x);
Ins(i << 1 | 1, ((l + r) >> 1) + 1, r, a, b, x);
}
void Solve(int i, int l, int r) {
int ntp = tp, a, b;
for (auto j : vq[i])
if (qc[j]) {
gvtx(j, a, b);
merge(a, b);
}
if (l == r) {
int j = l, e = qe[j], c = qc[j];
if (ncol[e] != c) {
gvtx(j, a, b);
int ca, aa = gcol(a, ca);
int cb, bb = gcol(b, cb);
if (aa == bb && ca == cb)
qc[j] = ncol[e], puts("NO");
else
ncol[e] = c, puts("YES");
} else
puts("YES");
} else {
Solve(i << 1, l, (l + r) >> 1);
Solve(i << 1 | 1, ((l + r) >> 1) + 1, r);
}
undok(tp - ntp);
}
int main() {
scanf("%d%d%d%d", &N, &M, &K, &Q);
for (int i = 1; i <= M; ++i) scanf("%d%d", &ea[i], &eb[i]);
for (int i = 1; i <= Q; ++i) {
scanf("%d%d", &qe[i], &qc[i]);
int a = ea[qe[i]], b = eb[qe[i]];
if (!mpq[qc[i]][a]) mpq[qc[i]][a] = ++cnt;
if (!mpq[qc[i]][b]) mpq[qc[i]][b] = ++cnt;
int &lpos = mpe[qe[i]];
if (lpos) Ins(1, 1, Q, lpos + 1, i, lpos);
lpos = i;
}
for (auto p : mpe)
if (p.second < Q) Ins(1, 1, Q, p.second + 1, Q, p.second);
for (int i = 1; i <= cnt; ++i) rk[i] = 1;
Solve(1, 1, Q);
return 0;
}
| 11 | CPP |
import math
def primeFactors(n):
ans = []
# Print the number of two's that divide n
while n % 2 == 0:
ans.append(2)
n = n>>1
if int(n)!=1:
return [-1]
else:
return ans
from sys import stdin, stdout
q = int(input())
for _ in range(q):
a, b = map(int, stdin.readline().strip().split())
if a == b:
print(0)
continue
if a<b:
a, b = b, a
if a%b!=0:
print(-1)
else:
pfactors = primeFactors(a//b)
if pfactors == [-1]:
print(-1)
else:
twos = len(pfactors)
ans = 0
q, r = divmod(twos, 3)
ans += q
twos = r
q, r = divmod(twos, 2)
ans += q
twos = r
q, r = divmod(twos, 1)
ans += q
print(ans) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int dp[(1<<20)];
int pre[(1<<20)];
void dfs(int x)
{
if(!x)return ;
if(pre[x]==1)dp[x]=dp[x+x];
if(pre[x]==0)dp[x]=dp[x+x+1];
if(pre[x]==2)dp[x]=dp[x+x]+dp[x+x+1];
dfs(x/2);
return ;
}
int main()
{
int k;
cin>>k;
int n=(1<<k)-1;
string s;
cin>>s;
reverse(s.begin(),s.end());
for(int i=0;i<n;i++)if(s[i]=='?')pre[i+1]=2;else pre[i+1]=s[i]-'0';
for(int i=n+1;i<=n+n+1;i++)dp[i]=1;
for(int i=1;i<=n;i++)dfs(i);
int q;
cin>>q;
while(q--)
{
int x;
string t;
cin>>x>>t;
x=n+1-x;
int tmp=pre[x];
if(t[0]=='?')pre[x]=2;
else pre[x]=t[0]-'0';
dfs(x);
cout<<dp[1]<<endl;
}
return 0;
} | 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500 + 10;
int mapp[maxn][maxn];
int movex[4] = {0, 0, 1, -1};
int movey[4] = {1, -1, 0, 0};
char labels[4] = {'.', 'S', 'D', 'W'};
int main() {
int r, c;
while (cin >> r >> c) {
memset(mapp, -1, sizeof mapp);
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
char c;
cin >> c;
if (c == '.')
mapp[i][j] = 0;
else if (c == 'S')
mapp[i][j] = 1;
else if (c == 'D')
mapp[i][j] = 2;
else if (c == 'W')
mapp[i][j] = 3;
}
}
bool flag = false;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
if (mapp[i][j] == 1) {
for (int k = 0; k < 4; ++k) {
int curx = i + movex[k], cury = j + movey[k];
if (curx >= r || curx < 0 || cury >= c || cury < 0) continue;
if (mapp[curx][cury] == 3) {
flag = true;
break;
}
if (mapp[curx][cury] == 0) mapp[curx][cury] = 2;
}
}
}
}
if (flag)
cout << "No" << endl;
else {
cout << "Yes" << endl;
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) cout << labels[mapp[i][j]];
cout << endl;
}
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
long long n, m, a[N], p[N], f[N], l, r, ans;
inline long long read() {
long long 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;
}
inline void init() {
int now = 1;
for (int i = 2; i <= n; i++)
if (a[i + 1] - a[i] > a[now + 1] - a[now]) now = i;
r = a[now + 1] - a[now] - 1;
for (int i = 1; i <= n; i++) p[i] = a[i + now];
long long tmp = p[1];
for (int i = 1; i <= n; i++) p[i] -= tmp;
}
inline bool check(long long x) {
f[1] = 0;
for (int i = 2; i <= n; i++) {
f[i] = f[i - 1];
if (f[i - 1] >= p[i] - 1) f[i] = max(f[i], p[i] + x);
if (f[i - 1] >= p[i] - x - 1) f[i] = max(f[i], p[i]);
if (i > 2 && f[i - 2] >= p[i] - x - 1) f[i] = max(f[i], p[i - 1] + x);
}
if (f[n] >= m - x - 1) return 1;
f[2] = max(p[2], x);
for (int i = 3; i <= n; i++) {
f[i] = f[i - 1];
if (f[i - 1] >= p[i] - 1)
f[i] = max(f[i], p[i] + x);
else if (f[i - 1] >= p[i] - x - 1)
f[i] = max(f[i], p[i]);
if (f[i - 2] >= p[i] - x - 1) f[i] = max(f[i], p[i - 1] + x);
}
if (f[n] >= min(m - 1, m + p[2] - x - 1)) return 1;
return 0;
}
int main() {
m = read();
n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
a[i + n] = a[i] + m;
}
if (n == 1) {
printf("%lld\n", m - 1);
return 0;
}
init();
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid))
ans = mid, r = mid - 1;
else
l = mid + 1;
}
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
n=int(input())
p=0
for i in range(n):
a,b,c=map(int,input().split())
if a+b+c>=2:
p=p+1
print(p)
| 7 | PYTHON3 |
test = int (input())
for i in range(test):
x= input().split()
a= int(x[0])
b= int(x[1])
n= int(x[2])
count=0
while a<=n and b<=n:
if a<b:
a= a+b
else:
b=b+a
count += 1
print(count) | 7 | PYTHON3 |
import math
for _ in range(int(input())):
n = int(input()) - 1
l = 2
while math.gcd(l,n-l)!=1:
l+=1
print(l, n-l, 1)
| 8 | PYTHON3 |
n = input()
money = list(map(int, input().split()))
mx = max(money)
kings_money = 0
for each in money:
kings_money += mx - each
print(kings_money) | 7 | PYTHON3 |
import sys
import math
from sys import stdin, stdout
# TAKE INPUT
def get_ints_in_variables():
return map(int, sys.stdin.readline().strip().split())
def get_int(): return int(input())
def get_ints_in_list(): return list(
map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
def main():
# Write Your Code Here
for _ in range(get_int()):
n,a,b = get_ints_in_variables()
lb = math.ceil(n/2)-1
ans = [0 for i in range(n+1)]
# if abs(a-b) <= 1 then answer exists because two consecitive min or max can not possible
if abs(a-b) > 1 or a+b > n-2: # possible answer is a+b <= n-2{because first and last index can not make minimum or maximum}
print(-1)
# return
else:#if lb >= a and lb >= b and (a+b) <= n-2:
if a>b:
cnt = n
cnt_loop = a
i = 2
while i < n+1 and cnt_loop > 0:
ans[i] = cnt
cnt -= 1
cnt_loop -= 1
i += 2
i = 1
while i < n+1:
if ans[i] == 0:
ans[i] = cnt
cnt -= 1
i += 1
elif b > a:
cnt = 1
cnt_loop = b
i = 2
while i < n+1 and cnt_loop > 0:
ans[i] = cnt
cnt += 1
cnt_loop -= 1
i += 2
i = 1
while i < n+1:
if ans[i] == 0:
ans[i] = cnt
cnt += 1
i += 1
else:
cnt = n
cnt_loop = a
i = 2
while i < n+1 and cnt_loop > 0:
ans[i] = cnt
cnt -= 1
cnt_loop -= 1
i += 2
cnt = 1
i = 1
while i < n+1:
if ans[i] == 0:
ans[i] = cnt
cnt += 1
i += 1
for i in range(1, n+1):
print(ans[i],end=" ")
print()
# else:
# print(-1)
# calling main Function
if __name__ == "__main__":
main() | 8 | PYTHON3 |
for __ in range(int(input())):
n = int(input())
flag = True
a = [int(i) for i in input().split()]
for i in range(n):
if not(a[i]>=i or a[i] >=n-1-i):
flag = False
if flag:
if n%2==0:
if a[n//2-1] == a[n//2] and a[n//2] == n//2-1:
flag = False
if flag:
print("Yes")
else:
print("No")
| 8 | PYTHON3 |
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int H[6][6] = {
{1,5,2,3,0,4}, // North : 奥へ移動 ( y:-1 )
{3,1,0,5,4,2}, // East : 右へ移動 ( x:+1 )
{2,1,5,0,4,3}, // West : 左へ移動 ( x:-1 )
{4,0,2,3,5,1}, // South : 手前へ移動 ( y:+1 )
{0,2,4,1,3,5}, // Right : 右回りに回転 (移動なし)
{0,3,1,4,2,5} // Left : 左回りに回転 (移動なし)
};
// サイコロライブラリ
// d[0] := top, d[1] := front
// d[2] := right, d[3] := left
// d[4] := back, d[5] := bottom
enum{TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM};
struct Cube{
vector<string> d;
// コンストラクタで初期化
Cube(vector<string> v){
if( v.size() == 6 ){
d = v;
}else{
d = vector<string>(6);
}
}
Cube(){ d = vector<string>(6); }
// dirの方向に回転 (副作用なし)
Cube roll(int dir){
Cube result = (*this);
vector<string> d_(6);
for(int i = 0 ; i < 6 ; i++ ){
d_[i] = d[ H[dir][i] ];
}
result.d = d_;
return result;
}
void debug(){
for(int i=0 ; i < 6 ; i++ ){
cout << (*this).d[i] << " ";
}
cout << endl;
}
bool operator==(const Cube& c){
for(int i=0 ; i < 6 ; i++ ){
if( c.d[i] != (*this).d[i] )
return false;
}
return true;
}
bool is_same(Cube c){
Cube c_[24];
c_[0] = c;
c_[1] = c.roll(0);
c_[2] = c.roll(1);
c_[3] = c.roll(2);
c_[4] = c.roll(3);
c_[5] = (c.roll(0)).roll(0);
for(int i=6 ; i < 12 ; i++ ){
c_[i] = c_[i-6].roll(4);
}
for(int i=12 ; i < 18 ; i++ ){
c_[i] = c_[i-6].roll(4);
}
for(int i=18 ; i < 24 ; i++ ){
c_[i] = c_[i-6].roll(4);
}
for(int i=0 ; i < 24 ; i++ ){
if( c_[i] == (*this) )
return true;
}
return false;
}
};
int main(){
int n;
while( cin >> n , n ){
vector<Cube> v;
for(int i=0 ; i < n ; i++ ){
Cube c;
for(int j=0 ; j < 6 ; j++ ){
string s;
cin >> s;
c.d[j] = s;
}
bool flag = true;
for(int j=0 ; j < v.size() ; j++ ){
if( c.is_same( v[j] ) ) flag = false;
}
if( flag ){
v.push_back( c );
}
}
cout << (n - v.size()) << endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, p[400001];
struct Data {
int val, num;
} a[400001];
int tot = 0;
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
tot = 0;
int lim = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
if (i == 1 || p[i] != p[i - 1]) {
a[++tot].val = p[i];
a[tot].num = 1;
} else
a[tot].num++;
if (i == n / 2 + 1) lim = tot - 1;
}
if (lim < 3) {
puts("0 0 0");
continue;
}
int g = 0, s = 0, b = 0;
g = a[1].num;
int tp = 2;
while (g >= s && tp <= lim) {
s += a[tp++].num;
}
for (int j = tp; j <= lim; j++) b += a[j].num;
if (b == 0 || g >= b || g >= s) {
puts("0 0 0");
} else {
printf("%d %d %d\n", g, s, b);
}
}
return 0;
}
| 9 | CPP |
n,k=map(int,input().split())
second=int(0)
first=int(0)
for i in range(1,k):
if (n % i)==0: second=i
first=int(n/second)
print(first*k+second)
| 8 | PYTHON3 |
import sys
layout1 = sys.stdin.readline().strip()
layout2 = sys.stdin.readline().strip()
output = sys.stdin.readline().strip()
for c in output:
if c.isalpha():
idx = layout1.find(c.lower())
if c.islower():
print(layout2[idx], end="")
else:
print(layout2[idx].upper(), end="")
else:
print(c, end="")
print("") | 8 | PYTHON3 |
"""http://codeforces.com/problemset/problem/330/A"""
def solve(r, c, cake):
rows, collumns = set(), set()
for i, row in enumerate(cake):
for j, cell in enumerate(row):
if cell == 'S':
rows.add(i)
collumns.add(j)
# res = 0
# for i, row in enumerate(cake):
# for j, cell in enumerate(row):
# if cell == 'S':
# continue
# if i not in rows or j not in collumns:
# res += 1
# return res
return (r * c) - (len(rows) * len(collumns))
if __name__ == '__main__':
r, c = map(int, input().split())
l = []
for _ in range(r):
l.append(input())
print(solve(r, c, l))
| 7 | PYTHON3 |
x = [0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
i = int(input())
for j in range(i):
a = int(input())
if x[a]==1:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int MAXN = 200015;
int n;
int values[MAXN], cnt[MAXN];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> values[i];
values[i]--;
}
int answer = 1;
multiset<int> counts;
for (int i = 0; i < n; i++) {
if (counts.find(cnt[values[i]]) != counts.end()) {
counts.erase(counts.find(cnt[values[i]]));
}
cnt[values[i]]++;
counts.insert(cnt[values[i]]);
if (counts.size() == 1) {
answer = i + 1;
}
int lowestValue = *counts.begin();
int highestValue = *(prev(counts.end()));
int secondHighestValue = *(prev(prev(counts.end())));
if (lowestValue == secondHighestValue &&
secondHighestValue + 1 == highestValue) {
answer = i + 1;
}
int secondLowestValue = *(next(counts.begin()));
if (secondLowestValue == highestValue && lowestValue == 1) {
answer = i + 1;
}
}
cout << answer << endl;
return 0;
}
| 8 | CPP |
from sys import stdin
if __name__ == '__main__':
n = int(stdin.readline().rstrip())
p = list(map(int, stdin.readline().rstrip().split()))
def get_nexts(p):
d, e = [n for i in range(n)], [n for i in range(n)]
stack1, stack2 = [], []
for i in range(n):
while stack2 and p[stack2[-1]] < p[i]:
idx = stack2.pop()
e[idx] = i
to_append = []
while stack1 and p[stack1[-1]] < p[i]:
idx = stack1.pop()
d[idx] = i
to_append.append(idx)
while to_append:
stack2.append(to_append.pop())
stack1.append(i)
dist1, dist2 = [], []
for i in range(n):
dist1.append(d[i] - i)
dist2.append(e[i] - d[i])
return dist1, dist2
dist1, dist2 = get_nexts(p)
dist3, dist4 = get_nexts(p[::-1])
ans = 0
for i in range(n):
ans += (dist1[i] * dist4[n - 1 - i] + dist2[i] * dist3[n - 1 - i]) * p[i]
print(ans)
| 0 | PYTHON3 |
import sys
from fractions import gcd
cin=sys.stdin.buffer.readline
for _ in range(int(cin())):
x,y,a,b=map(int,cin().split())
print([-1,(y-x)//(a+b)][(y-x)%(a+b)==0]) | 7 | PYTHON3 |
for _ in range(int(input())):
n = int(input())
x = bin(n).count("1")
print(pow(2,x)) | 8 | PYTHON3 |
#include <algorithm>
#include <iostream>
#include <ostream>
void print_array(const int a[], int n) {
for (int i = 0; i < n; ++i) {
if (i > 0)
std::cout << " ";
std::cout << a[i];
}
std::cout << std::endl;
}
int main(int argc, char *argv[]) {
int n;
std::cin >> n;
int a[10];
for (int i = 0; i < n; ++i)
a[i] = i + 1;
do {
print_array(a, n);
} while (std::next_permutation(a, a + n));
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
const int N = 2e2 + 7;
const int xinc[] = {0, 0, 1, -1};
const int yinc[] = {1, -1, 0, 0};
const long double PI = 3.141592653589793;
int n, k, a[N], ans;
void solve() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
ans = INT_MIN;
for (int l = 0; l < n; l++)
for (int r = l; r < n; r++) {
vector<int> orig, temp;
for (int i = l; i <= r; i++) orig.push_back(a[i]);
for (int i = 0; i < l; i++) temp.push_back(a[i]);
for (int i = r + 1; i < n; i++) temp.push_back(a[i]);
sort((temp).begin(), (temp).end(), greater<int>());
int ns = min((int)temp.size(), k);
temp.resize(ns);
for (auto i : temp) orig.push_back(i);
sort((orig).begin(), (orig).end(), greater<int>());
orig.resize(r - l + 1);
int sum = 0;
for (auto i : orig) sum += i;
ans = max(sum, ans);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
t = 1;
while (t--) solve();
return 0;
}
| 7 | CPP |
def ii():
return int(input())
def mi():
return map(int, input().split())
def li():
return list(mi())
n = ii()
ans = 0
for i in range(2, n):
for j in range(2 * i, n + 1, i):
ans += j // i
print(ans * 4) | 10 | PYTHON3 |
n,m = map(int,input().split())
a = []
for i in range(n):
a.append(list(map(int,input().split())))
b = [0]*5
ans = []
for i in range(n):
t = 0
for j in range(m):
if t < b[j]:
t = b[j]
t += a[i][j]
b[j] = t
ans.append(b[m-1])
print(*ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(10);
long long t;
cin >> t;
while (t--) {
long long mind = 10000000000000ll;
long long x = -1;
long long n;
cin >> n;
long long k;
cin >> k;
std::vector<long long> a(n);
for (long long i = 0; i < n; ++i) cin >> a[i];
for (long long i = 0; i < n - k; ++i) {
if (((a[i + k] - a[i] + 1ll) / 2ll) < mind) {
mind = ((a[i + k] - a[i] + 1ll) / 2ll);
x = (a[i + k] + a[i]) / 2ll;
}
}
cout << x << "\n";
}
}
| 9 | CPP |
i = int(input())
x = 0
for j in range(i):
s = input()
if '--' in s:
x -= 1
elif '++' in s:
x += 1
print(x)
| 7 | PYTHON3 |
import collections, heapq, bisect, math
def gcd(a, b):
if b == 0: return a
return gcd(b, a%b)
def solve(A):
a1 = int(math.sqrt(A[0][2]*A[0][1]//A[1][2]))
out = [a1]
for i in range(1,len(A)):
out.append(A[0][i]//a1)
return ' '.join([str(a) for a in out])
q = input()
tests = []
for test in range(int(q)):
#n = input()
tests.append([int(p) for p in input().split(' ')])
print(solve(tests))
#print(solve(n,b)) | 8 | PYTHON3 |
import sys
import math
L = []
L1 = []
res = []
userIn = int(input())
for i in range(0,userIn):
userIn2 = int(input())
x = list(map(str, input().split()))
L.append(x)
for i in range(0,len(L)):
L[i].reverse()
res.append(L[i])
for i in range(0,len(res)):
print(' '.join(res[i]))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int sum = 0, t = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') t = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
sum = sum * 10 + ch - '0';
ch = getchar();
}
return sum * t;
}
int n, k;
int a[300005];
int cntl[1000005], cntr[1000005];
int s[300005];
long long ans;
void add(int &x, int y) {
x += y;
while (x >= k) x -= k;
while (x < 0) x += k;
}
void solve(int l, int r) {
if (l == r) return;
int p = ((l + r) >> 1), q = ((l + r) >> 1) + 1;
int mxl = 0, mxr = 0, sml = 0, smr = 0;
while (p >= l || q <= r) {
if (q > r || (max(mxl, a[p]) <= max(mxr, a[q]) && p >= l)) {
mxl = max(mxl, a[p]);
add(sml, a[p] % k);
s[p] = sml;
cntl[s[p]]++;
p--;
int x = (mxl - sml) % k;
if (x < 0) x += k;
ans += cntr[x];
} else {
mxr = max(mxr, a[q]);
add(smr, a[q] % k);
s[q] = smr;
cntr[s[q]]++;
q++;
int x = (mxr - smr) % k;
if (x < 0) x += k;
ans += cntl[x];
}
}
for (int i = l; i <= ((l + r) >> 1); i++) cntl[s[i]]--;
for (int i = r; i > ((l + r) >> 1); i--) cntr[s[i]]--;
solve(l, ((l + r) >> 1));
solve(((l + r) >> 1) + 1, r);
}
int main() {
n = read(), k = read();
for (int i = 1; i <= n; i++) a[i] = read();
solve(1, n);
printf("%lld\n", ans);
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 200005;
vector<int> mp[SIZE], d[SIZE];
int a[SIZE], b[SIZE], c[SIZE], t1[SIZE], t2[SIZE], fa[SIZE], fm;
int y[SIZE];
void DFS(int p, int f, int de) {
for (int i = 0; i < mp[p].size(); i++)
if (mp[p][i] != f) {
int to = mp[p][i];
a[to] = min(a[to], a[p]);
fa[to] = p;
DFS(to, p, de + 1);
}
if (y[p] == 1)
t1[p] = 1;
else if (y[p] == 2)
t2[p] = 1;
fm = max(fm, de);
d[de].push_back(p);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d%d", a + i, b + i, c + i);
if (b[i] == 1 && c[i] == 0)
y[i] = 1;
else if (b[i] == 0 && c[i] == 1)
y[i] = 2;
}
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
}
DFS(1, -1, 1);
long long cost = 0;
for (int i = fm; i >= 1; i--) {
for (int j = 0; j < d[i].size(); j++) {
int num = min(t1[d[i][j]], t2[d[i][j]]);
cost += (long long)a[d[i][j]] * num * 2;
t1[d[i][j]] -= num;
t2[d[i][j]] -= num;
t1[fa[d[i][j]]] += t1[d[i][j]];
t2[fa[d[i][j]]] += t2[d[i][j]];
}
}
if (t1[1] == 0 && t2[1] == 0)
printf("%I64d\n", cost);
else
puts("-1");
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<char> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b(n);
for (int i = 0; i < n; i++) {
if (a[i] == '0')
b[i] = 0;
else
b[i] = 1;
}
int f = 1;
for (int i = 0; i < n; i++) {
if (b[i] == 1) {
int q = 1;
int l = i - 1;
int r = i + 1;
if (l >= 0) {
if (b[l] == 1) q = 0;
}
if (r <= n - 1) {
if (b[r] == 1) q = 0;
}
f = q;
}
if (b[i] == 0) {
int q = 0;
int l = i - 1;
int r = i + 1;
if (l >= 0) {
if (b[l] == 1) q = 1;
}
if (r <= n - 1) {
if (b[r] == 1) q = 1;
}
f = q;
}
if (f == 0) break;
}
if (f)
cout << "Yes";
else
cout << "No";
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 100;
const long long inf = 1e17;
pair<long long, long long> a[N], b[N];
long long c[N], p[N];
vector<long long> v[3];
bool mark[N];
bool cmp(long long a, long long b) { return c[a] < c[b]; }
void ok(long long id) {
for (long long i = 0; i < v[id].size(); i++) {
mark[v[id][i]] = true;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
k--;
long long sum = 0;
for (long long i = 0; i < n; i++) {
cin >> p[i] >> c[i];
a[i] = make_pair(p[i], i);
b[i] = make_pair(c[i], i);
sum += c[i];
}
sort(a, a + n, greater<pair<long long, long long> >());
sort(b, b + n);
long long ans = inf;
if (a[k].first + 2 <= n) {
ans = 0;
for (long long i = 0; i < a[k].first + 2; i++) {
ans += b[i].first;
}
}
long long id = 0;
for (long long i = n - 1; i >= 0; i--) {
if (a[i].first == a[k].first) id = i;
if (a[k].first == p[i]) v[1].push_back(i);
if (a[k].first + 1 == p[i]) v[0].push_back(i);
if (a[k].first - 1 == p[i]) v[2].push_back(i);
}
long long x = k - id;
if (a[k].first + 1 <= n) {
long long tmp = 0, x2 = x + v[0].size();
ok(0);
ok(1);
long long h = a[k].first + 1;
h = n - h;
for (long long i = n - 1; i >= 0; i--) {
long long id = b[i].second;
if (h == 0) break;
if (mark[id] && x2 == 0) continue;
x2 -= mark[id];
tmp += c[id];
h--;
}
if (h == 0) {
ans = min(ans, sum - tmp);
}
}
memset(mark, 0, sizeof(mark));
if (a[k].first <= n) {
long long tmp = 0, x2 = x;
ok(1);
ok(2);
long long h = a[k].first;
h = n - h;
for (long long i = n - 1; i >= 0; i--) {
long long id = b[i].second;
if (h == 0) break;
if (mark[id] && x2 == 0) continue;
x2 -= mark[id];
tmp += c[id];
h--;
}
if (h == 0) {
ans = min(ans, sum - tmp);
}
}
if (ans == inf) {
cout << "-1"
<< "\n";
} else {
cout << ans << "\n";
}
return 0;
}
| 9 | CPP |
r, c = list(map(int, input().split()))
l = []
a = []
for i in range(r):
l.append(input())
cnt = 0
for ix, row in enumerate(l):
if 'S' not in row:
cnt += c
a.append(ix)
i = j = 0
while j < c:
cn = i = 0
while i < r:
if i in a:
i += 1
continue
if l[i][j] == 'S':
break
else:
cn += 1
i += 1
else:
cnt += cn
j += 1
print(cnt)
| 7 | PYTHON3 |
s=int(input())
for i in range(s):
p=int(input())
q=list(map(int,input().split()))
l=list(dict.fromkeys(q))
for j in l:
if j in q:
print(j,end=" ")
print() | 8 | PYTHON3 |
n = int(input())
ls = list(map(int, input().split()))
d = {1: 0, 2: 0}
for i in ls: d[i]+=1
cnt = min(d[1], d[2])
d[1], d[2] = d[1]-cnt, d[2]-cnt
if d[1]!=0: cnt+=d[1]//3
print(cnt)
| 7 | PYTHON3 |
n=int(input())
l=list(map(int,input().split()))
for i in l:
x=i%14
if i>14 and (x>0 and x<7):
print("YES")
else:
print("NO")
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int t[100000], x[100000], w[100000];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d%d", &t[i], &x[i]);
int ind = 0, tsum = 0;
while (1) {
map<int, vector<int> > mp;
if (ind * m >= n) break;
for (int i = ind * m; i < (ind + 1) * m && i < n; i++) {
mp[x[i]].push_back(i);
tsum = max(tsum, t[i]);
}
ind++;
int prev = 0;
for (map<int, vector<int> >::iterator iter = mp.begin(); iter != mp.end();
iter++) {
tsum += iter->first - prev;
prev = iter->first;
for (int i = 0; i < iter->second.size(); i++) w[iter->second[i]] = tsum;
tsum += 1 + iter->second.size() / 2;
}
tsum += prev;
}
for (int i = 0; i < n; i++) printf("%d ", w[i]);
}
| 9 | CPP |
# Karun Abhayan
t = int(input())
for _ in range(t):
s = input()
team1 = 0
team2 = 0
kl2 = 5 #kicks left for each team
flag = 0
min_needed = [] # min. needed for team1, min. for team2
for i in range(10):
if team1 > team2+kl2:
flag = 1
break
else:
if not i%2:
if s[i] == '1' or s[i] == '?':
team1 += 1
else:
if s[i] == '1':
team2 += 1
kl2 -= 1
if flag:
min_needed.append(i)
else:
min_needed.append(i+1)
team1 = 0
team2 = 0
kl1 = 5
flag = 0
for i in range(10):
if team2 > team1+kl1:
flag = 1
break
else:
if i%2:
if s[i] == '1' or s[i] == '?':
team2 += 1
else:
if s[i] == '1':
team1 += 1
kl1 -= 1
if flag:
min_needed.append(i)
else:
min_needed.append(i+1)
print(min(min_needed))
| 9 | PYTHON3 |
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define DEBUGP(val) cerr << #val << "=" << val << "\n"
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
while (cin >> n && n) {
VI a(n);
REP(i, 0, n) cin >> a[i];
int st = -1;
int len = 0;
vector<PI> cont;
REP(i, 0, n) {
if (a[i] == st + len) {
len += 1;
} else {
if (st >= 0)
cont.push_back(PI(st, len));
st = a[i];
len = 1;
}
}
cont.push_back(PI(st, len));
REP(i, 0, cont.size()) {
if (cont[i].second == 1) {
cout << cont[i].first;
} else {
cout << cont[i].first << "-" << cont[i].first + cont[i].second - 1;
}
cout << (i + 1 == cont.size() ? "\n" : " ");
}
}
}
| 0 | CPP |
#include "bits/stdc++.h"
using namespace std;
/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1369
??????????????????
???ICPC??\??´?????????????????£???????????¨????????°?????????????????????????????????????????°???????????????????????¢???
????£??????????????????????????????§???????????¶?????????????????????????????????????????????????????????????????????????????¢??????
???????°???\???????????????1????????????????????????????????????????§??????§??????????????????????????¢???????¨?????????????
????????????????????°???????£??????????????????????????????¨??????????????¨????¨±?????????????????°???????????????
?????????????????¢???????????????????????????, ????£???????????????????????????????????????????????????????????????°?????????
??????????????????????????????????????¨?????°??????????????????????£?????????????????????\????????????????????¨?¢???????????????¨??????
??????????????¢?????°????????????????????¢??????????????????????????£??????????????????
*/
int main() {
cin.tie(0); ios::sync_with_stdio(false);
int n, m; cin >> n >> m;
vector<int> line(200000, 0);
vector<int> min_line(n, 0), max_line(n, 0); // min~max_line[i] = i????????????????????°?????§??????????????????or???????????????
for (int i = 0; i < m;i++) {
int x, y; cin >> x >> y;
line[x] = y;
}
for (int i = 0; i < n;i++) {
min_line[i] = max_line[i] = i;
}
for (int i = 0; i < 200000;i++) {
if (line[i] == 0)continue;
min_line[line[i] - 1] = min_line[line[i]] = min(min_line[line[i] - 1], min_line[line[i]]);
max_line[line[i] - 1] = max_line[line[i]] = max(max_line[line[i] - 1], max_line[line[i]]);
}
cout << max_line[0] - min_line[0] + 1;
for (int i = 1; i < n;i++) {
cout << " " << max_line[i] - min_line[i] + 1;
}
cout << endl;
} | 0 | CPP |
def my_func(string):
gens = "ACTG"
ans = 0
for i in range(4):
ans += min((ord(string[i]) - ord(gens[i]) + 26) % 26, (ord(gens[i]) - ord(string[i]) + 26) % 26)
return ans
n = int(input())
genome = input()
temp = my_func(genome[0:4])
for i in range(n-3):
temp = min(temp, my_func(genome[i:i+4]))
print(temp)
| 7 | PYTHON3 |
def rowmatch(grid,n,s):
return grid[n] == s
def colmatch(grid,n,s):
return [grid[i][n] for i in range(3)] == list(s)
def diagmatch(grid,s):
return [grid[i][i] for i in range(3)] == list(s) or [grid[i][2-i] for i in range(3)] == list(s)
def gridcontains(grid,s):
for i in range(3):
if rowmatch(grid,i,s):
return True
if colmatch(grid,i,s):
return True
if diagmatch(grid,s):
return True
return False
def charcount(grid,char):
return sum([len([x for x in grid[i] if x == char]) for i in range(3)])
def xcount(grid):
return charcount(grid,'X')
def ocount(grid):
return charcount(grid,'0')
def xwin(grid):
return gridcontains(grid,'XXX')
def owin(grid):
return gridcontains(grid,'000')
def f(grid):
if xwin(grid):
if owin(grid) or xcount(grid) - ocount(grid) != 1:
return('illegal')
else:
return('the first player won')
if owin(grid):
if xcount(grid) - ocount(grid) != 0:
return('illegal')
else:
return('the second player won')
if xcount(grid) == 5 and ocount(grid) == 4:
return('draw')
if xcount(grid) - ocount(grid) == 1:
return('second')
if xcount(grid) - ocount(grid) == 0:
return('first')
else:
return('illegal')
grid = [input() for i in range(3)]
print(f(grid)) | 9 | PYTHON3 |
n=int(input())
answer=[]
for i in range(n):
t=''
s=input()
if len(s)>10:
t=t+s[0]
t=t+str(len(s)-2)
t=t+s[len(s)-1]
answer.append(t)
else:
answer.append(s)
print(' \n'.join(answer))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
double c;
int d;
bool operator<(const node& p) const { return c < p.c; }
};
node r[1001];
bool v[1001] = {false};
int main() {
int n, k, m, x;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> r[i].c;
r[i].d = i;
}
cin >> m;
int card = 0;
double sum = 0.0;
double Min = 10000000000.0, Max = 0.0;
for (int i = 0; i < m; i++) {
sum = 0.0;
for (int j = 0; j < n / k; j++) {
cin >> x;
if (j == 0 && v[x - 1] == false) {
card++;
}
sum += r[x - 1].c;
v[x - 1] = true;
}
Max = max(Max, sum);
Min = min(Min, sum);
}
if (card < k) {
sort(r, r + n);
int cunt = 0;
sum = 0;
for (int i = 0; i < n; i++) {
if (v[r[i].d] == false) {
sum += r[i].c;
cunt++;
if (cunt == n / k) {
Min = min(Min, sum);
break;
}
}
}
cunt = 0;
sum = 0;
for (int i = n - 1; i >= 0; i--) {
if (v[r[i].d] == false) {
sum += r[i].c;
cunt++;
if (cunt == n / k) {
Max = max(Max, sum);
break;
}
}
}
}
int u = n / k;
printf("%.10llf %.10llf\n", Min / u, Max / u);
return 0;
}
| 8 | CPP |
for t in range(1):
n,q=map(int,input().split())
time=[0]*n
for i in range(q):
t,k,d=map(int,input().split())
count=0
pos=0
for j in range(n):
if time[j]<=t:
pos+=j+1
count+=1
if count==k:
break
if count<k:
print(-1)
continue
changed=0
for j in range(n):
if time[j]<=t:
time[j]=t+d
changed+=1
if changed==count:
break
print(pos) | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> x(m);
for (auto& e : x) cin >> e;
sort(x.begin(), x.end());
vector<int> d(m-1);
for (int i=0; i<m-1; i++) d[i] = x[i+1] - x[i];
sort(d.begin(), d.end());
cout << (n >= m ? 0 : accumulate(d.begin(), d.begin() + m-n, 0)) << endl;
} | 0 | CPP |
while True:
n = int(input())
if n == 0:
break
ls = []
for i in range(n):
v = list(map(int,input().split()))
ls.append((sum(v[1:]),v[0]))
ls.sort()
ls.reverse()
print(ls[0][1],ls[0][0])
| 0 | PYTHON3 |
#include <bits/stdc++.h>
template <typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
template <typename T>
T max_4(T a, T b, T c, T d) {
return std::max(std::max(a, b), std::max(c, d));
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
static int x[5], y[5], d[5];
for (int i = 0; i < 4; ++i) scanf("%d%d", x + i, y + i);
static int u[5], v[5];
int ans = 1 << 30;
auto update = [&](int p[5], int x1, int x2, int y1, int y2) {
int cur = max_4(abs(x[p[0]] - x1) + abs(y[p[0]] - y1),
abs(x[p[1]] - x1) + abs(y[p[1]] - y2),
abs(x[p[2]] - x2) + abs(y[p[2]] - y1),
abs(x[p[3]] - x2) + abs(y[p[3]] - y2));
if (cur < ans)
ans = cur, u[p[0]] = x1, v[p[0]] = y1, u[p[1]] = x1, v[p[1]] = y2,
u[p[2]] = x2, v[p[2]] = y1, u[p[3]] = x2, v[p[3]] = y2;
};
for (d[0] = 0; d[0] < 2; ++d[0])
for (d[1] = 0; d[1] < 2; ++d[1])
for (d[2] = 0; d[2] < 2; ++d[2])
for (d[3] = 0; d[3] < 2; ++d[3]) {
static std::set<int> stx, sty;
stx.clear(), sty.clear();
for (int i = 0; i < 4; ++i)
d[i] == 0 ? stx.insert(x[i]) : sty.insert(y[i]);
if (stx.size() > 2 || sty.size() > 2) continue;
if (stx.size() < 1 || sty.size() < 1) continue;
int x1 = -1, x2 = -1;
int y1 = -1, y2 = -1;
if (stx.size() >= 1) x1 = *stx.begin();
if (sty.size() >= 1) y1 = *sty.begin();
if (stx.size() == 2) x2 = *(++stx.begin());
if (sty.size() == 2) y2 = *(++sty.begin());
auto calc = [&](int x1, int x2, int y1, int y2) {
if (x2 - x1 != y2 - y1) return;
static int p[5];
for (int i = 0; i < 4; ++i) p[i] = i;
do {
if (x[p[0]] != x1 && y[p[0]] != y1) continue;
if (x[p[1]] != x1 && y[p[1]] != y2) continue;
if (x[p[2]] != x2 && y[p[2]] != y1) continue;
if (x[p[3]] != x2 && y[p[3]] != y2) continue;
update(p, x1, x2, y1, y2);
} while (std::next_permutation(p, p + 4));
};
if (x2 != -1 && y2 != -1)
calc(x1, x2, y1, y2);
else {
if (x2 == -1)
calc(x1, x1 + (y2 - y1), y1, y2),
calc(x1 - (y2 - y1), x1, y1, y2);
if (y2 == -1)
calc(x1, x2, y1, y1 + (x2 - x1)),
calc(x1, x2, y1 - (x2 - x1), y1);
}
}
static int p[5], q[5];
for (int i = 0; i < 4; ++i) p[i] = i;
std::sort(p, p + 4, [](int a, int b) {
return x[a] != x[b] ? x[a] < x[b] : y[a] < y[b];
});
if (x[p[0]] == x[p[1]] && x[p[2]] == x[p[3]] && x[p[0]] != x[p[2]]) {
int x1 = x[p[0]], x2 = x[p[2]];
for (int i = 0; i < 4; ++i) q[i] = y[p[i]] - (i & 1 ? x2 - x1 : 0);
std::sort(q, q + 4);
q[0] = (q[0] + q[3]) / 2;
update(p, x1, x2, q[0], q[0] + (x2 - x1));
}
for (int i = 0; i < 4; ++i) p[i] = i;
std::sort(p, p + 4, [](int a, int b) {
return y[a] != y[b] ? y[a] < y[b] : x[a] < x[b];
});
if (y[p[0]] == y[p[1]] && y[p[2]] == y[p[3]] && y[p[0]] != y[p[2]]) {
int y1 = y[p[0]], y2 = y[p[2]];
for (int i = 0; i < 4; ++i) q[i] = x[p[i]] - (i & 1 ? y2 - y1 : 0);
std::sort(q, q + 4);
q[0] = (q[0] + q[3]) / 2;
std::swap(p[1], p[2]);
update(p, q[0], q[0] + (y2 - y1), y1, y2);
}
if (ans == 1 << 30)
printf("-1\n");
else {
printf("%d\n", ans);
for (int i = 0; i < 4; ++i) printf("%d %d\n", u[i], v[i]);
}
}
return 0;
}
| 10 | CPP |
// C - Sequence
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N;
vector<ll> A(100000);
ll counter(int sign){//sign: (+)start -> 1, (-)start -> -1
ll c = 0;
for(ll s=0, i=0; i<N; ++i, sign*=-1){
s += A[i];
if(sign*s<=0){ c += abs(s) + 1; s = sign; }
}
return c;
}
int main(){
cin>>N;
for(int i=0; i<N; ++i) cin>>A[i];
ll a = counter(1), b = counter(-1);
cout<< (a<b?a:b) <<endl;
} | 0 | CPP |
t = int(input())
for _ in range(t):
x,y,n = map(int, input().split())
m = n % x
diff = y-m
result = n + diff
if result > n: result -= x
print(result) | 7 | PYTHON3 |
'''
import sys
import math
import bisect
n=int(input())
arr=list(map(int,input().split()))
n,m=map(int,input().split())
t=sys.stdin.buffer.readline()
sys.stdin=open("input.txt")
sys.stdout=open("output.txt", 'w')
sys.stdout.write("Yes" + '\n')
s="abcdefghijklmnopqrstuvwxyz"
mod=1000000007
mod=998244353
vow=['a','e','i','o','u']
t=[[0 for i in range(n)]for j in range(n)]
pow(4,7)
def gcd(x,y):
while y:
x,y=y%x,x
return y'''
import bisect
n,m=map(int,input().split())
c=list(map(int,input().split()))
p=list(map(int,input().split()))
c=sorted(list(set(c)))
ans1=0
ans2=0
#ans3=10**9
an=0
for i in c:
var=bisect.bisect_left(p,i)
#print("var",var)
#print(var)
if var==0:
ans1=max(ans1,p[0]-c[0])
if var==m:
ans2=max(ans2,i-p[m-1])
else:
ans3=min(i-p[var-1],p[var]-i)
an=max(ans3,an)
#print('an',an)
print(max(ans1,ans2,an))
| 9 | PYTHON3 |
import sys
import math
from itertools import permutations
input = sys.stdin.readline
n=int(input())
arr=list(map(int,input().split()))
index=[]
for i in range(n):
index.append(i+1)
res = sorted(zip(arr,index))
#print(res)
num=[]
count=[]
for i in range(1,n):
flag1=True
for j in range(len(num)):
if res[i][0]-res[i-1][0]==num[j][0]:
num[j][1]+=1
flag1=False
if flag1:
num.append([res[i][0]-res[i-1][0],1])
if len(num)>3:
print(-1)
break
#print(num)
if len(num)==1:
print(res[0][1])
elif len(num)<=3:
star=[]
for j in range(1,n):
for k in range(len(num)):
if num[k][0]==res[j][0]-res[j-1][0] and num[k][1]<=2:
star.append(j)
#print("star",star)
f=True
for j in star:
ans=[]
flag=True
for k in range(1,n):
# print("xx",res[k][0]-res[k-1][0])
if k!=j and k!=j+1 and res[k][0]-res[k-1][0] not in ans:
ans.append(res[k][0]-res[k-1][0])
elif k!=j and k==j+1 and res[k][0]-res[k-2][0] not in ans:
ans.append(res[k][0]-res[k-2][0])
elif k==j and j!=n-1 and res[k+1][0]-res[k-1][0] not in ans:
ans.append(res[k+1][0]-res[k-1][0])
if len(ans)>1:
flag=False
break
# print("ans",ans)
if flag:
print(res[j][1])
f=False
break
if f:
f1=True
diff=res[2][0]-res[1][0]
for j in range(2,n):
if res[j][0]-res[j-1][0]!=diff:
f1=False
break
if f1:
print(res[0][1])
else:
print(-1) | 10 | PYTHON3 |
s = ["vaporeon","jolteon","flareon","espeon","umbreon","leafeon","glaceon","sylveon"]
d = []
n = int(input())
a = input()
for i in range(8):
if len(s[i])==n:
b = s[i]
cnt = 0
for j in range(n):
if a[j]==".":
pass
elif a[j]!=b[j]:
cnt+=1
if cnt==0:
d.append(s[i])
print(*d)
| 7 | PYTHON3 |
x=""
l=int(input())
while len(x)!=l:
x=input()
c=0
for i in range(0,len(x)-1):
if x[i]==x[i+1]:
c+=1
print(c)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m;
struct Edge {
int u, v, id, vis;
Edge() {}
Edge(int u, int v, int id) : u(u), v(v), id(id) { vis = 0; }
};
vector<Edge> e[2];
int fa[N];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
vector<int> ans;
int cnt[2];
bool judge() {
if (n % 2 == 0) return false;
for (int f = 0; f < 2; f++) {
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 0; i < (int)(e[!f]).size(); i++) {
Edge x = e[!f][i];
int pu = find(x.u);
int pv = find(x.v);
if (pu != pv) fa[pu] = pv;
}
for (int i = 0; i < (int)(e[f]).size(); i++) {
Edge x = e[f][i];
int pu = find(x.u);
int pv = find(x.v);
if (pu != pv) {
fa[pu] = pv;
ans.push_back(x.id);
e[f][i].vis = 1;
cnt[f]++;
}
}
}
if (cnt[0] > n / 2 || cnt[1] > n / 2) return false;
for (int i = 1; i <= n; i++) fa[i] = i;
for (int f = 0; f < 2; f++) {
for (int i = 0; i < (int)(e[f]).size(); i++) {
if (e[f][i].vis) {
int pu = find(e[f][i].u);
int pv = find(e[f][i].v);
if (pu != pv)
fa[pu] = pv;
else
return false;
}
}
}
for (int f = 0; f < 2; f++) {
for (int i = 0; i < (int)(e[f]).size(); i++) {
int pu = find(e[f][i].u);
int pv = find(e[f][i].v);
if (pu != pv) {
cnt[f]++;
fa[pu] = pv;
ans.push_back(e[f][i].id);
if (cnt[f] == n / 2) break;
}
}
}
return cnt[0] == n / 2 && cnt[1] == n / 2;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
char tp[2];
scanf("%d%d%s", &u, &v, tp);
e[tp[0] == 'S'].push_back(Edge(u, v, i));
}
if (!judge())
printf("-1\n");
else {
printf("%d\n", (int)(ans).size());
for (int i = 0; i < (int)(ans).size(); i++) printf("%d ", ans[i]);
printf("\n");
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n,m,i,x,da;
struct jgt
{
int b;
int e;
}a[1000005];
bool cmp(jgt a,jgt b)
{
if (a.e!=b.e)
{
return a.e<b.e;
}
else
{
return a.b<b.b;
}
}
int main()
{
cin>>n>>m;
for (i=1;i<=m;i++)
{
cin>>a[i].b>>a[i].e;
}
sort(a+1,a+1+m,cmp);
for (i=1;i<=m;i++)
{
if (a[i].b>=x)
{
x=a[i].e;
da+=1;
}
}
cout<<da;
return 0;
} | 0 | CPP |
#include <iostream>
using namespace std;
int main (void){
long long int x,y;
cin >> x >> y;
if(x-y<=1&&x-y>=-1) cout << "Brown" << endl;
else cout << "Alice" << endl;
return 0;
} | 0 | CPP |
n,m=map(int, input().split())
s=" "
for i in range(1,n+1) :
if (i%2)!=0 :
s="#"*m
elif (i%2)==0 :
if (i%4)==0 :
s="#" + "."*(m-1)
else :
s="."*(m-1) + "#"
print(s) | 7 | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.