solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)
#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)
#define ll long long
#define pii pair<int, int>
#define db double
#define x first
#define y second
#define ull unsigned long long
#define sz(a) ((int) (a).size())
#define vi vector<int>
using namespace std;
const int N = 233;
int n, tot, v[N];
set < int > S[N];
void merge(int x, int y) {
for(int k : S[y]) S[x].insert(k);
S[y].clear();
}
int getin() {
int k;
return cin >> k, k;
}
bool check(int x, int y) {
return cout << 1 << " " << x << " " << y << endl, getin();
}
bool all[N][N], vis[N];
int rmain() {
memset(all, 0, sizeof(all)), memset(vis, 0, sizeof(vis)), memset(v, 0, sizeof(v));
L(i, 1, tot) S[i].clear();
tot = 0;
cin >> n;
L(i, 0, n - 1) {
int l = 1, r = tot + 1;
while(l < r) {
int mid = (l + r) >> 1;
if(mid == tot + 1 || check(i, v[mid])) r = mid;
else l = mid + 1;
}
R(i, tot, l) v[i + 1] = v[i];
++tot, v[l] = i;
}
L(i, 1, tot) S[i].insert(v[i]);
int now = tot;
while(now > 1) {
for(int v : S[now]) if(!vis[v]) {
cout << 2 << " " << v << " ";
int cnt = 0;
L(i, 1, now - 1) cnt += sz(S[i]);
cout << cnt << " ";
L(i, 1, now - 1) for(int u : S[i]) cout << u << " ";
cout << endl;
if(getin()) {
merge(now - 1, now);
break;
}
vis[v] = true;
}
--now;
}
L(i, 1, tot) L(j, i, tot) for(int u : S[i]) for(int v : S[j]) all[u][v] = true;
cout << 3 << endl;
L(i, 0, n - 1) {
L(j, 0, n - 1) cout << all[i][j];
cout << endl;
}
return getin(), 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--) rmain();
return 0;
} | 11 | CPP |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
int main () {
int h, w; cin >> h >> w;
string s[100];
rep(i,h) cin >> s[i];
rep(i,h)rep(j,w){
if (s[i][j] == '#') continue;
s[i][j] = '0';
for(int dx = -1; dx<=1; dx++) {
for(int dy= -1; dy<=1; dy++) {
int nx=i+dx, ny=j+dy;
if(nx >= 0 && nx < h && ny >= 0 && ny < w) {
if(s[nx][ny] == '#') s[i][j]++;
}
}
}
}
rep(i,h) cout << s[i] << endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int s, i, u, diff = 0;
cin >> s >> i >> u;
if (s <= i) diff = i - s + 1;
if (u < diff) {
cout << 0 << endl;
continue;
}
s += diff;
u -= diff;
int st = 0, e = u, mid, ans = 0;
while (st <= e) {
mid = st + (e - st) / 2;
int f = u - mid;
if (f + s > mid + i)
st = mid + 1, ans = mid;
else
e = mid - 1;
}
cout << ans + 1 << endl;
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
void RD(int &x) { scanf("%d", &x); }
void RD(long long &x) { scanf("%I64d", &x); }
void RD(unsigned long long &x) { scanf("%I64u", &x); }
void RD(unsigned int &x) { scanf("%u", &x); }
void RD(double &x) { scanf("%lf", &x); }
void RD(int &x, int &y) { scanf("%d%d", &x, &y); }
void RD(long long &x, long long &y) { scanf("%I64d%I64d", &x, &y); }
void RD(unsigned int &x, unsigned int &y) { scanf("%u%u", &x, &y); }
void RD(double &x, double &y) { scanf("%lf%lf", &x, &y); }
void RD(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); }
void RD(long long &x, long long &y, long long &z) {
scanf("%I64d%I64d%I64d", &x, &y, &z);
}
void RD(unsigned int &x, unsigned int &y, unsigned int &z) {
scanf("%u%u%u", &x, &y, &z);
}
void RD(double &x, double &y, double &z) { scanf("%lf%lf%lf", &x, &y, &z); }
void RD(char &x) { x = getchar(); }
void RD(char *s) { scanf("%s", s); }
void RD(string &s) { cin >> s; }
void PR(int x) { printf("%d\n", x); }
void PR(long long x) { printf("%I64d\n", x); }
void PR(unsigned int x) { printf("%u\n", x); }
void PR(unsigned long long x) { printf("%I64u\n", x); }
void PR(double x) { printf("%.10lf\n", x); }
void PR(char x) { printf("%c\n", x); }
void PR(char *x) { printf("%s\n", x); }
void PR(string x) { cout << x << endl; }
const long long inf = ((long long)1) << 60;
const int mod = 1000000007;
const double dinf = 1e50;
const int INF = 1000000000;
const int N = 100005;
int n, m, a[2][N], ans[N];
int s1[N], s2[N];
int main() {
RD(n, m);
int i, j, k;
for (i = 0; i < 2; i++)
for (j = 0; j < n; j++) RD(k), a[i][k]++;
int e = 0, top = 0, tail = 0;
for (i = 0; i < m; i++) {
while (a[0][i]--) s1[++top] = i;
k = m - 1 - i;
while (a[1][k]--) {
if (top)
ans[++e] = s1[top--] + k;
else
s2[++tail] = k;
}
}
k = 0;
while (top) ans[++e] = s1[top--] + s2[++k] - m;
sort(ans + 1, ans + n + 1);
for (i = n; i > 1; i--) printf("%d ", ans[i]);
PR(ans[i]);
}
| 11 | CPP |
w=int(input())
if w==2: print("NO")
else:
print("YES" if (w-2)%2==0 and w%2==0 else "NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
long long INTMAX = 1000000007;
using namespace std;
void solve() {
long long n, s;
cin >> n >> s;
long long div = n / 2;
div++;
long long ans = s / div;
cout << ans << "\n";
}
signed main() {
long long t;
cin >> t;
for (long long i1 = 0; i1 < t; i1++) {
solve();
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int cnt[5005];
int dom[5005];
int a[5005];
int main() {
int n, i, j, best;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (i = 1; i <= n; i++) {
memset(cnt, 0, sizeof cnt);
best = a[i];
dom[best]++;
cnt[a[i]]++;
for (j = i + 1; j <= n; j++) {
cnt[a[j]]++;
if (cnt[a[j]] > cnt[best]) {
dom[a[j]]++;
best = a[j];
} else if (cnt[a[j]] == cnt[best] && a[j] < best) {
dom[a[j]]++;
best = a[j];
} else
dom[best]++;
}
}
for (i = 1; i <= n; i++) printf("%d%c", dom[i], i == n ? '\n' : ' ');
}
| 7 | CPP |
#include <iostream>
#include <map>
using namespace std;
map<long long int, long long int>m;
int main(){
int n;
cin>>n;
long long int x=0, a=0, ans=0;
m[0]=1;
for(int i=1;i<=n;i++){
cin>>x;
a=a+x;
ans-=m[a]*(m[a]-1)/2;
m[a]+=1ll;
ans+=m[a]*(m[a]-1)/2;
}
cout<<ans;
} | 0 | CPP |
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main() {
int n,i,k,p,cn;
double x[100],y[100],dx,dy,sx,sy,a,b,c,d,e;
while(cin >> n) {
if (n==0) break;
for (i=0;i<n;i++) scanf("%lf,%lf",&x[i],&y[i]);
sy=-1000;
for (i=0;i<n;i++) if (y[i]>sy) { sy=y[i]; k=i;}
dx=x[k]-10; dy=y[k]; sx=x[k]; sy=y[k]; cn=0;
do {
e=2;
for (i=0;i<n;i++) {
if (!((x[i]==dx && y[i]==dy) || (x[i]==sx && y[i]==sy))) {
c=sqrt((sx-dx)*(sx-dx)+(sy-dy)*(sy-dy));
b=sqrt((x[i]-sx)*(x[i]-sx)+(y[i]-sy)*(y[i]-sy));
a=sqrt((x[i]-dx)*(x[i]-dx)+(y[i]-dy)*(y[i]-dy));
d=(b*b+c*c-a*a)/(2*b*c);
if (d<e) { e=d; p=i; }
}
}
dx=sx; dy=sy; sx=x[p]; sy=y[p];
cn++;
} while(!(sx==x[k] && sy==y[k]));
cout << n-cn << endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100001, M = 37;
long long q[N];
int v[M][N];
vector<long long> a;
vector<long long> b;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> q[i];
int k = n / 2;
int l = n - k;
int B = (1 << k);
for (int i = 0; i < B; i++) {
long long ans = 0;
for (int j = 0; j < k; j++)
if ((i & (1 << j))) ans += q[j];
ans %= m;
a.push_back(ans);
}
sort(a.begin(), a.end());
B = (1 << l);
for (int i = 0; i < B; i++) {
long long ans = 0;
for (int j = 0; j < l; j++)
if ((i & (1 << (j)))) ans += q[j + k];
ans %= m;
b.push_back(ans);
}
long long ans = (a.back() + b.back()) % m;
sort(b.begin(), b.end());
for (int i = 0; i < a.size(); i++) {
int l = -1, r = b.size();
while (r - l > 1) {
int mid = (r + l) / 2;
if (a[i] + b[mid] >= m)
r = mid;
else
l = mid;
}
if (l != -1) ans = max(ans, b[l] + a[i]);
}
cout << ans;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e6 + 1e3 + 7;
int T, n, a[N], vis[N];
pair<int, int> f[N][2];
int last[N][2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], vis[i] = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < 2; j++) f[i][j] = {1e9, 1e9};
f[0][0] = f[0][1] = {-1e9, -1e9};
for (int i = 1; i <= n; i++) {
pair<int, int> dp1 = f[i - 1][0], dp2 = f[i - 1][1];
if (-a[i] > dp1.first)
dp1.first = -a[i];
else if (-a[i] > dp1.second)
dp1.second = -a[i];
else
dp1 = {1e9, 1e9};
if (-a[i] > dp2.first)
dp2.first = -a[i];
else if (-a[i] > dp2.second)
dp2.second = -a[i];
else
dp2 = {1e9, 1e9};
if (dp1 < dp2)
f[i][0] = dp1, last[i][0] = 0;
else
f[i][0] = dp2, last[i][0] = 1;
dp1 = f[i - 1][0], dp2 = f[i - 1][1];
if (a[i] > dp1.first)
dp1.first = a[i];
else if (a[i] > dp1.second)
dp1.second = a[i];
else
dp1 = {1e9, 1e9};
if (a[i] > dp2.first)
dp2.first = a[i];
else if (a[i] > dp2.second)
dp2.second = a[i];
else
dp2 = {1e9, 1e9};
if (dp1 < dp2)
f[i][1] = dp1, last[i][1] = 0;
else
f[i][1] = dp2, last[i][1] = 1;
}
int ok = 0;
if (f[n][0].first <= n && f[n][0].second <= n) {
ok = 1;
int q = 0;
for (int i = n; i >= 1; i--) {
if (!q) a[i] *= -1;
q = last[i][q];
}
} else if (f[n][1].first <= n && f[n][1].second <= n) {
ok = 1;
int q = 1;
for (int i = n; i >= 1; i--) {
if (!q) a[i] *= -1;
q = last[i][q];
}
}
if (ok) {
cout << "YES\n";
for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n];
} else
cout << "NO\n";
}
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long dp[2008][2008], tot[2008], n, k;
int MOD = 1e9 + 7;
int main() {
string s;
cin >> n >> k >> s;
dp[0][0] = tot[0] = 1;
for (int id = 1; id <= n; id++) {
int small = s[id - 1] - 'a', large = 'z' - s[id - 1];
for (int sum = 0; sum <= k; sum++) {
for (int pre = 1; pre <= id; pre++) {
if (sum - pre * (n - id + 1) < 0) break;
dp[id][sum] =
(dp[id][sum] + dp[id - pre][sum - pre * (n - id + 1)] * large) %
MOD;
}
}
for (int sum = 0; sum <= k; sum++) {
dp[id][sum] = (dp[id][sum] + tot[sum] * small) % MOD;
tot[sum] = (tot[sum] + dp[id][sum]) % MOD;
}
}
cout << tot[k] << endl;
return 0;
}
| 11 | CPP |
n, k = map(int, input().split())
mod = 10 ** 9 + 7
count = [pow(k // max(i, 1), n, mod) for i in range(k + 1)]
for i in range(k, 0, -1):
for j in range(2 * i, k + 1, i):
count[i] -= count[j]
sum_ = 0
for i in range(k + 1):
sum_ = (sum_ + i * count[i]) % mod
print(sum_) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, i, flag = 0, n, a, b;
t = 1;
while (t--) {
cin >> n >> a >> b;
flag = 0;
for (i = 0; i <= (n) / a; i++) {
if ((n - (a * i)) % b == 0) {
cout << "YES" << endl;
cout << i << " " << (n - a * i) / b << endl;
flag = 1;
break;
}
}
if (flag != 1) cout << "NO" << endl;
}
}
| 8 | CPP |
N = int(input())
V = list(map(int, input().split()))
C = list(map(int, input().split()))
ans = sum([v-c for v, c in zip(V, C) if v-c > 0])
print(ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long a[1000005];
long long tot;
struct que {
long long x, y, z;
} t[1000005];
int main() {
long long tt;
scanf("%lld", &tt);
while (tt--) {
long long n;
tot = 0;
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (long long i = 1; i <= n; i++) {
long long mi = i;
for (long long j = i + 1; j <= n; j++)
if (a[mi] > a[j]) mi = j;
long long k = a[mi];
for (long long j = mi - 1; j >= i; j--) a[j + 1] = a[j];
a[i] = k;
if (mi != i) t[++tot].x = i, t[tot].y = mi, t[tot].z = mi - i;
}
printf("%lld\n", tot);
for (long long i = 1; i <= tot; i++)
printf("%lld %lld %lld\n", t[i].x, t[i].y, t[i].z);
}
return 0;
}
| 8 | CPP |
#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
vector<string> res;
for(int i=0,s=0;i<SIZE(str);i++){
if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
}
return res;
}
int N;
int bs[100001];
int main(){
while(cin>>N&&N){
int sum=0;
for(int i=0;i<N;i++){
cin>>bs[i];
sum+=bs[i];
}
bool ok=false;
int k=0;
for(k=1;k*(k+1)/2<=sum;k++){
if(k*(k+1)/2==sum){
ok=true;
break;
}
}
int sz=N;
if(!ok)cout<<-1<<endl;
else{
int iter=0;
for(iter=0;iter<10000;iter++){
ok=true;
// check
for(int i=0;i<k;i++)
if(bs[i]!=i+1)ok=false;
if(ok)break;
// remake
int cnt=0;
int val=0;
for(int i=0;i<sz;i++){
if(bs[i]==0)cnt++;
else{
bs[i]--;
val++;
}
}
int pos=0;
for(int i=0;i<sz;i++)
if(bs[i]!=0)bs[pos++]=bs[i];
bs[pos++]=val;
sz=pos;
}
if(iter==10000)cout<<-1<<endl;
else cout<<iter<<endl;
}
}
return 0;
} | 0 | CPP |
t = input()
H = input().split()
for c in H:
if c[0] == t[0] or c[1] == t[1]:
print("YES")
quit()
print("NO")
| 7 | PYTHON3 |
#include<iostream>
using namespace std;
int main(){
int m,a,b,ans;
int p[201];
cin >> m >> a >> b;
while(m != 0 || a != 0 || b != 0){
for(int i = 0;i < m;i++){
cin >> p[i];
}
for(int i = a;i <= b;i++){
if(i == a) ans = a;
else if(p[i-1]-p[i] >= p[ans-1]-p[ans]) ans = i;
}
cout << ans << endl;
cin >> m >> a >> b;
}
return (0);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,n,i,x;
cin>>a>>b>>n;
x=max(a,b);
cout<<(n-1)/x+1;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
#pragma optimize("O3")
int V[5+(1<<18)];
int W[5+(1<<18)];
int dp[5+(1<<10)][100005];
const int INF = 1012345678;
int S, K;
int rec(int v, int L_rem, int bonus_value){
if(L_rem < 0){
return -INF;
}else if(v < (1<<K)){
return bonus_value + dp[v][L_rem];
}
return max(rec(v>>1, L_rem, bonus_value), rec(v>>1, L_rem-W[v], bonus_value+V[v]));
}
int main(){
int N;
scanf("%d", &N);
for(int i = 1; i <= N; i ++){
scanf("%d%d", &V[i], &W[i]);
}
int Q;
scanf("%d", &Q);
K = log2(N*8)/2;
//printf("1<<K=%d\n", 1<<K);
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= (1<<K); i ++){
for(int j = 0; j <= 100000; j ++){
int temp = dp[i>>1][j];
if(j >= W[i]){
temp = max(temp, V[i]+dp[i>>1][j-W[i]]);
}
dp[i][j] = temp;
}
}
while(Q --){
int v, L;
scanf("%d%d", &v, &L);
printf("%d\n", rec(v, L, 0));
}
return 0;
}
| 0 | CPP |
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 7 17:20:59 2020
@author: user
"""
def solve(n):
if(n==1):
print(0)
elif(n%2==0):
print(n//2)
else:
print(n)
n=int(input())
solve(n+1) | 7 | PYTHON3 |
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
sieve = [0]*(10**6+1)
primes = [0]*(10**6+1)
for i in range(2,10**6+1):
if not sieve[i]:
primes[i] += 1
if i*i <= 10**6:
primes[i*i] -= 1
for x in range(i*i,10**6+1,i):
sieve[x] = 1
primes[1] = 1
for i in range(1,10**6+1):
primes[i] += primes[i-1]
_ = input()
for i in map(int,input().split()):
print(primes[i])
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main() | 16 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const int mod = 1000000007;
int lowbit(int x) { return x & (-x); }
int fast_power(int a, int b) {
int x;
for (x = 1; b; b >>= 1) {
if (b & 1) x = 1ll * x * a % mod;
a = 1ll * a * a % mod;
}
return x % mod;
}
int main() {
int a, b, c, n;
scanf("%d%d%d", &a, &b, &n);
if (n == 1) {
printf("%d\n", b);
return 0;
}
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
printf("%d\n", c);
return 0;
}
| 13 | CPP |
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
answer="NO"
for i in range(n-1):
if(a[i]<=a[i+1]):
answer="YES"
break;
print(answer) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
set<int> dat;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
int tmp;
scanf("%d", &tmp);
dat.insert(tmp);
}
for (int i = 1; m >= i; i++) {
if (dat.find(i) == dat.end()) {
m -= i;
ans.push_back(i);
}
}
printf("%d\n", (int)ans.size());
for (int d : ans) {
printf("%d ", d);
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int bears[505][505];
int ans[505];
int m, n, q;
void calc(int i) {
ans[i] = 0;
int curr = 0;
for (int j = 0; j < n; j++) {
if (bears[i][j])
curr++;
else
curr = 0;
ans[i] = max(ans[i], curr);
}
}
int main() {
cin >> m >> n >> q;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) cin >> bears[i][j];
calc(i);
}
while (q--) {
int x, y;
cin >> x >> y;
bears[x - 1][y - 1] ^= 1;
int a = 0;
calc(x - 1);
for (int i = 0; i < m; i++) a = max(ans[i], a);
cout << a << endl;
}
return 0;
}
| 8 | CPP |
x, y, z = [int(_) for _ in input().split()]
print((x-z)//(y+z)) | 0 | PYTHON3 |
#include <iostream>
int main()
{
char c='a';
scanf("%c", &c);
printf("%c", c+1);
} | 0 | CPP |
for _ in range(int(input())):
n = int(input())
ls = list(map(int, input().split()))
ar = []
for i in range(n):
ar.append(ls[i])
ar.sort()
if n == 1:
print('YES')
continue
mn = ar[0]
f = True
for i in range(n):
if ar[i] == ls[i]:
continue
else:
if ls[i] % mn == 0:
continue
else:
f = False
break
if f:
print('YES')
else:
print('NO')
| 9 | PYTHON3 |
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
const LL N=1000005;
const LL MOD=998244353;
LL JC[N],inv[N];
LL Pow (LL x,LL y)
{
if (y==1) return x;
LL lalal=Pow(x,y>>1);
lalal=lalal*lalal%MOD;
if (y&1) lalal=lalal*x%MOD;
return lalal;
}
LL n,m;
LL C (LL x,LL y) {return JC[x]*inv[y]%MOD*inv[x-y]%MOD;}
LL calc (LL x,LL y) {return C(x+y,x);}
void solve ()
{
LL ans=0;
for (LL u=1;u<=min(n,m);u++)
ans=(ans+calc(u,u)*calc(n-u,m-u)%MOD)%MOD;
ans=ans*Pow(calc(n,m),MOD-2)%MOD;
ans=ans*Pow(2,MOD-2)%MOD;
ans=(ans+max(n,m))%MOD;
printf("%lld\n",ans);
}
int main()
{
JC[0]=1;for (LL u=1;u<=1000000;u++) JC[u]=JC[u-1]*u%MOD;
inv[1000000]=Pow(JC[1000000],MOD-2);
for (LL u=999999;u>=0;u--) inv[u]=inv[u+1]*(u+1)%MOD;
scanf("%lld%lld",&n,&m);
solve();
return 0;
} | 0 | CPP |
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
static const int INF = 1000000010;
int main(){
ios_base::sync_with_stdio(false);
while(true){
int n;
cin >> n;
if(n == 0){ break; }
vector<int> a(n), b(n);
for(int i = 0; i < n; ++i){
string s;
cin >> s;
if(s == "x"){
b[i] = 1;
}else{
a[i] = atoi(s.c_str());
}
}
int lower = -INF, upper = INF;
for(int i = 0; i < n; ++i){
if(b[i] == 0){
if(i + 1 >= n || b[i + 1]){ continue; }
if(i % 2 == 0){
if(a[i] >= a[i + 1]){
lower = INF; upper = -INF;
}
}else{
if(a[i] <= a[i + 1]){
lower = INF; upper = -INF;
}
}
continue;
}
if(i + 1 < n && b[i + 1]){
lower = INF; upper = -INF;
}
if(i % 2 == 0){
for(int j = -1; j <= 1; j += 2){
const int k = i + j;
if(k < 0 || n <= k || b[k]){ continue; }
upper = min(upper, a[k] - 1);
}
}else{
for(int j = -1; j <= 1; j += 2){
const int k = i + j;
if(k < 0 || n <= k || b[k]){ continue; }
lower = max(lower, a[k] + 1);
}
}
}
if(lower > upper){
cout << "none" << endl;
}else if(lower == upper){
cout << lower << endl;
}else{
cout << "ambiguous" << endl;
}
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, ans;
char s[701];
int a[701], f[701][701][10][2];
int main() {
scanf("%s", s + 1), n = strlen(s + 1);
int i, j, k, l, p;
for (i = 1; i <= n; i++) a[i] = s[i] ^ 48;
for (i = 0; i <= 9; i++) f[0][0][i][0] = 1;
for (i = 1; i <= n; i++)
for (j = 0; j < i; j++)
for (k = 1; k <= 9; k++)
for (l = 0; l < 2; l++)
for (p = 0; p <= (l ? 9 : a[i]); p++)
f[i][j + (p >= k)][k][l | (p < a[i])] =
(f[i][j + (p >= k)][k][l | (p < a[i])] + f[i - 1][j][k][l]) %
mod;
for (k = 1; k <= 9; k++)
for (i = p = 1; i <= n; i++, p = (10ll * p + 1) % mod)
ans = (ans + 1ll * p * (f[n][i][k][0] + f[n][i][k][1]) % mod) % mod;
printf("%d", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
int n, q;
long long spex;
long long a[MAX_N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &a[i]);
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
a[i] += a[i - 1];
spex += a[i] * (i != n);
}
for (scanf("%d", &q); q--;) {
long long k, s = 0;
scanf("%I64d", &k);
if (k == 1) {
printf("%I64d\n", spex);
continue;
}
for (long long step = 0, i = n, mul = 1; i > 0;
i -= mul, mul *= k, step++) {
long long j = max(0ll, i - mul);
s += (a[i] - a[j]) * step;
}
printf("%I64d\n", s);
}
}
| 10 | CPP |
#include <bits/stdc++.h>
typedef long long T[52][52];
long long N;
int r, M, K, m = 1000000007;
T A, B;
void mul(T A, T B) {
T C = {};
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
for (int k = 0; k < M; k++) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % m;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) A[i][j] = C[i][j];
}
int main() {
scanf("%I64d%d%d", &N, &M, &K);
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) A[i][j] = i == j, B[i][j] = 1;
char x, y;
for (int i = 0; i < K; i++) {
scanf(" %c%c", &x, &y);
B[x - (x > 96 ? 97 : 39)][y - (y > 96 ? 97 : 39)] = 0;
}
for (N--; N; N >>= 1) {
if (N & 1) mul(A, B);
mul(B, B);
}
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++) r = (r + A[i][j]) % m;
printf("%d\n", r);
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int vis[1005], n, root = 1;
vector<int> edge[1005];
int ask(int x, int y) {
int num;
cout << "? " << x << " " << y << endl;
cin >> num;
return num;
}
int dfs(int u) {
vis[u] = 1;
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
if (vis[v]) continue;
return dfs(v);
}
return u;
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
while (true) {
int x = dfs(root);
if (x == root) break;
int y = dfs(root);
root = ask(x, y);
}
cout << "! " << root << endl;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n;
pair<int, int> arr[N];
int mxArr[N];
int mnArr[N];
int main() {
cin >> n;
for (int l, i = 0; i < n; i++) {
cin >> l;
int mx = INT_MIN;
int mn = INT_MAX;
bool bo = false;
for (int num, j = 0; j < l; j++) {
cin >> num;
bo += mn < num;
mx = max(mx, num);
mn = min(mn, num);
}
if (bo) mx = INT_MAX, mn = INT_MIN;
arr[i] = {mx, mn};
}
sort(arr, arr + n);
for (int i = 0; i < n; i++) mxArr[i] = arr[i].first, mnArr[i] = arr[i].second;
long long ans = 0;
for (int i = 0; i < n; i++) {
int upBoIdx = upper_bound(mxArr, mxArr + n, mnArr[i]) - mxArr;
ans += n - upBoIdx;
}
cout << ans;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
map<int, int> mp;
for (int i = 0; i < n; i++) {
int q;
cin >> q;
mp[q]++;
}
int last = 0;
for (auto &i : mp) {
if (i.second > 1) {
last = i.second / 2;
i.second %= 2;
mp[i.first + 1] += last;
}
}
int cnt = 0;
for (auto i : mp) {
cnt += (i.second == 1);
}
auto it = mp.end();
it--;
int maxx = it->first;
cout << maxx - cnt + 1 << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int f[27];
int main() {
int n, i, j;
cin >> n;
char c;
int flag = 0;
for (i = 0; i < n; i++) {
cin >> c;
f[c - 'a']++;
}
if (n > 26 || flag == 1)
cout << -1 << endl;
else {
int ans = 0;
for (i = 0; i <= 26; i++) {
if (f[i] >= 2) ans += f[i] - 1;
}
cout << ans << endl;
}
return 0;
}
| 8 | CPP |
n,m= map(int, input().split())
s = input()
t = input()
if '*' not in s:
if s==t:
print("YES")
else:
print("NO")
else:
ind = s.index('*')
if s[0:ind]+s[ind+1:] == t:
print("YES")
else:
tmp = s[0:ind]+t[ind:(m-n+ind+1)]+s[ind+1:]
#print(tmp)
if tmp == t:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
char arr[200111];
char a[200111];
char b[200111];
int n;
char s1[200111];
int L1 = 0;
char s2[200111];
int L2 = 0;
bool Normal() {
int i;
for (i = 1; i <= L1; i++) {
if (s1[i] < s2[i])
return true;
else if (s1[i] > s2[i])
return false;
}
return true;
}
void MergeSort(int L, int R) {
int i;
int mid;
if ((L + R) % 2 == 0) return;
mid = (L + R) / 2;
MergeSort(L, mid);
MergeSort(mid + 1, R);
L1 = 0;
L2 = 0;
for (i = L; i <= mid; i++) {
L1++;
s1[L1] = arr[i];
}
for (i = mid + 1; i <= R; i++) {
L2++;
s2[L2] = arr[i];
}
if (Normal()) {
for (i = L; i <= mid; i++) {
arr[i] = s1[i - L + 1];
}
for (i = mid + 1; i <= R; i++) {
arr[i] = s2[i - (mid + 1) + 1];
}
} else {
for (i = L; i <= mid; i++) {
arr[i] = s2[i - L + 1];
}
for (i = mid + 1; i <= R; i++) {
arr[i] = s1[i - (mid + 1) + 1];
}
}
return;
}
int main() {
int i;
scanf("%s", a + 1);
n = strlen(a + 1);
for (i = 1; i <= n; i++) {
arr[i] = a[i];
}
MergeSort(1, n);
for (i = 1; i <= n; i++) {
a[i] = arr[i];
}
scanf("%s", b + 1);
for (i = 1; i <= n; i++) {
arr[i] = b[i];
}
MergeSort(1, n);
for (i = 1; i <= n; i++) {
b[i] = arr[i];
}
for (i = 1; i <= n; i++) {
if (a[i] != b[i]) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}
| 8 | CPP |
def consonant(s):
s = s.lower()
dot = '.'
ans = ''
for i in s:
if i not in ['a', 'e', 'i', 'o', 'u', 'y']:
ans = ans + dot
ans = ans + i
return ans
if __name__ == '__main__':
s = input()
ans = consonant(s)
print(ans) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void rd(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
x *= f;
}
const long long inf = 2e18;
const int maxn = 1e5 + 5;
long long a[maxn], b[maxn], c[maxn];
int n, q;
int main() {
rd(n);
for (int i = (1); i <= (n); ++i) rd(a[i]);
sort(a + 1, a + n + 1);
for (int i = (2); i <= (n); ++i) b[i] = a[i] - a[i - 1];
b[1] = inf;
sort(b + 1, b + n + 1);
for (int i = (1); i <= (n); ++i) c[i] = c[i - 1] + b[i];
rd(q);
while (q--) {
long long l, r;
rd(l), rd(r);
long long k = r - l + 1;
l = 0;
r = n;
while (r - l > 1) {
int mid = l + r >> 1;
if (b[mid] < k)
l = mid;
else
r = mid;
}
printf("%lld ", c[l] + (long long)(n - l) * k);
}
return 0;
}
| 10 | CPP |
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> S;
int c;
while(cin>>c)
{
if(c==0)
cout<<S.back()<<endl,
S.pop_back();
else
S.push_back(c);
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
char m[55][55];
int dp[55][55][55][55];
int n;
int dfs(int a, int b, int c, int d) {
if (dp[a][b][c][d] != -1) return dp[a][b][c][d];
if (a == c)
if (b == d) return (m[c][d] == '#');
dp[a][b][c][d] = 1 + max(c - a, d - b);
int j = a;
while (j < c) {
dp[a][b][c][d] = min(dp[a][b][c][d], dfs(a, b, j, d) + dfs(j + 1, b, c, d));
j++;
}
int k = b;
while (k < d) {
dp[a][b][c][d] = min(dp[a][b][c][d], dfs(a, b, c, k) + dfs(a, k + 1, c, d));
k++;
}
return dp[a][b][c][d];
}
int main() {
memset(dp, -1, sizeof(dp));
cin >> n;
int i = 0;
while (i < n) {
scanf("%s", m[i]);
i++;
}
printf("%d\n", dfs(0, 0, n - 1, n - 1));
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool go(const map<long long, int>& all, int x,
multiset<long long, greater<long long>> trash) {
multiset<long long, greater<long long>> ends;
for (const auto& p : all) {
if (p.second < x) {
for (int i = p.second; i < x; ++i) {
ends.insert(p.first);
}
x = p.second;
} else {
for (int i = x; i < p.second; ++i) {
trash.insert(p.first);
}
}
}
if (trash.size() > ends.size()) {
return 0;
}
while (!trash.empty()) {
if (*ends.begin() >= *trash.begin()) {
trash.erase(trash.begin());
ends.erase(ends.begin());
} else {
return 0;
}
}
return 1;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout.setf(ios_base::fixed);
cout.precision(28);
int n;
cin >> n;
map<long long, int> all;
multiset<long long, greater<long long>> trash;
long long z = 1;
for (int i = 0; i < 50; ++i) {
all[z] = 0;
z <<= 1;
}
for (int i = 0; i < n; ++i) {
long long x;
cin >> x;
if (all.count(x)) {
++all[x];
} else {
trash.insert(x);
}
}
if (all[1] == 0 || !go(all, all[1], trash)) {
cout << "-1\n";
return 0;
}
int L = 0, R = all[1], M;
while (R - L > 1) {
M = (L + R) / 2;
if (go(all, M, trash)) {
R = M;
} else {
L = M;
}
}
int zz = all[1];
for (int i = R; i <= zz; ++i) {
cout << i << ' ';
}
cout << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int nums[1000005], pos[1000005], idx;
long long int diff[1000005], deviation[1000005], initialDeviation, result;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &nums[i]);
initialDeviation += abs(nums[i] - i);
pos[nums[i]] = i;
}
result = initialDeviation;
idx = 0;
for (int i = 1; i <= n; i++) {
if (pos[i] <= i) {
int shift = i - pos[i] + 1;
diff[1]--;
diff[shift] += 2;
shift = n - pos[i] + 1;
diff[shift]--;
int change = 2 * i - n - 1;
diff[shift] += change;
diff[shift + 1] -= change;
diff[shift + 1]--;
diff[n]++;
} else {
int shift = n - pos[i] + 1;
diff[1]++;
diff[shift]--;
int change = 2 * i - n - 1;
diff[shift] += change;
diff[shift + 1] -= change;
diff[shift + 1]--;
diff[shift + 1 + i - 1] += 2;
diff[n]--;
}
}
for (int i = 1; i <= n; i++) diff[i] = diff[i - 1] + diff[i];
deviation[0] = initialDeviation;
for (int i = 1; i < n; i++) deviation[i] = deviation[i - 1] + diff[i];
for (int i = 1; i < n; i++) {
if (deviation[i] < result) {
result = deviation[i];
idx = i;
}
}
printf("%lld %d\n", result, idx);
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
for(int i=1; i <= s.size(); i++){
if((i%2==1 && s.at(i-1) == 'L') || (i%2==0 && s.at(i-1) == 'R')){
cout<<"No";
return 0;
}
}
cout<<"Yes";
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
int n, K, i, j, k, l, a[N], c[N], lst[N], head[N], adj[N], nxt[N], cap[N],
flow[N], cost[N], q[N], bg, ed, dis[N], w[N], ans, p[N], pre[N];
bool v[N];
void addedge(int u, int v, int w, int c) {
adj[++l] = v;
nxt[l] = head[u];
head[u] = l;
cap[l] = w;
cost[l] = c;
adj[++l] = u;
nxt[l] = head[v];
head[v] = l;
cost[l] = -c;
}
bool spfa() {
int i;
for (i = 1; i <= n * 2 + 2; ++i) dis[i] = 1 << 30, v[i] = false;
q[bg = ed = 1] = n * 2 + 1;
w[n * 2 + 1] = 1 << 30;
dis[n * 2 + 1] = 0;
v[n * 2 + 1] = true;
while (bg <= ed) {
v[q[bg]] = false;
for (i = head[q[bg]]; i; i = nxt[i])
if (flow[i] < cap[i] && dis[q[bg]] + cost[i] < dis[adj[i]]) {
dis[adj[i]] = dis[q[bg]] + cost[i];
w[adj[i]] = min(w[q[bg]], cap[i] - flow[i]);
p[adj[i]] = q[bg];
pre[adj[i]] = i;
if (!v[adj[i]]) {
v[adj[i]] = true;
q[++ed] = adj[i];
}
}
++bg;
}
if (dis[n * 2 + 2] == 1 << 30) return false;
ans += dis[n * 2 + 2] * w[n * 2 + 2];
i = n * 2 + 2;
while (i != n * 2 + 1) {
flow[pre[i]] += w[n * 2 + 2];
flow[pre[i] & 1 ? pre[i] + 1 : pre[i] - 1] -= w[n * 2 + 2];
i = p[i];
}
return true;
}
int main() {
scanf("%d%d", &n, &K);
for (i = 1; i <= n; ++i) scanf("%d", a + i);
for (i = 1; i <= n; ++i) scanf("%d", c + i);
for (i = 1; i <= n; ++i) {
addedge(2 * n + 1, i, 1, c[a[i]]);
addedge(n + i, 2 * n + 2, 1, 0);
addedge(i, n + i, 1, 0);
}
for (i = 1; i < n; ++i) addedge(i, i + 1, K - 1, 0);
for (i = 1; i <= n; ++i) {
if (lst[a[i]]) addedge(i - 1, n + lst[a[i]], 1, -c[a[i]]);
lst[a[i]] = i;
}
while (spfa())
;
printf("%d", ans);
return 0;
}
| 9 | CPP |
"""
Author: Q.E.D
Time: 2020-06-23 09:33:37
"""
T = int(input())
for _ in range(T):
n = int(input())
s = input()
l = -1
for i, x in enumerate(s):
if x == '0':
l = i
else:
break
ans = []
for i in range(l + 1):
ans.append(s[i])
if s[n - 1] == '0':
if l != n - 1:
ans.append(s[n - 1])
else:
r = n
for i, x in enumerate(s[::-1]):
if x == '1':
r = n - i - 1
else:
break
if l != r - 1:
ans.append(s[r - 1])
for i in range(r, n):
ans.append(s[i])
print(''.join(ans))
| 8 | PYTHON3 |
def dfs(tree, cat_map, node, max_consec_vertices):
stack = list()
stack.append((node, 0))
visited_set = set()
path_sum = 0
while len(stack) > 0:
v, cat_count = stack.pop()
updated_cat_count = cat_count + 1 if cat_map[v] == 1 else 0
if updated_cat_count > max_consec_vertices:
continue # ignore v as the path is dead
if v not in visited_set:
visited_set.add(v)
is_leaf = 1
for w in tree[v]:
if w not in visited_set:
stack.append((w, updated_cat_count))
is_leaf = 0
path_sum += is_leaf
return path_sum
def run_alg():
first_line_data = input().split(' ')
number_of_vertices = int(first_line_data[0])
max_consec_vertices = int(first_line_data[1])
tree = {}
# initialize tree
k = 0
while k < number_of_vertices:
tree[k] = set()
k += 1
cat_map = {}
second_line_data = input().split(' ')
for i in range(number_of_vertices):
cat_map[i] = int(second_line_data[i])
for _ in range(number_of_vertices - 1):
data = input().split(' ')
left_vertex = int(data[0])
right_vertex = int(data[1])
tree[left_vertex - 1].add(right_vertex - 1)
tree[right_vertex - 1].add(left_vertex - 1)
print(dfs(tree, cat_map, 0, max_consec_vertices))
if __name__ == '__main__':
run_alg()
| 9 | PYTHON3 |
t = int(input())
if t > 2 and t % 2 == 0:
print ('YES')
else:
print ('NO') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x,y;
cin>>x>>y;
cout<<x+1-y<<endl;
} | 0 | CPP |
from collections import defaultdict
import math
x,y,z=map(int,input().split())
if(x>=y+z):
print(x-y-z+1)
elif(y>=x+z):
print(y-x-z+1)
elif(z>=y+x):
print(z-y-x+1)
else:
print("0") | 7 | PYTHON3 |
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PB push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, pair<int, int> > P2;
static const int INF = 1ll<<60;
static const int dx[] = { 1, -1, 0, 0, };
static const int dy[] = { 0, 0, 1, -1 };
static const int mod = 1000000007;
int N,M;
int v[3005],h[3005];
int t[3005],s[3005];
//?????¨ //??????
int imos[3005];
int dp[1005][3005];
P1 min_pair(P1 a,P1 b){
if(a.first<b.first)return a;
else if(a.first>b.first)return b;
else {
if(a.second<b.second)return a;
else if(a.second>b.second)return b;
else return a;
}
}
void solve(){
for(int i=0;i<1005;++i)for(int j=0;j<3005;++j)dp[i][j]=-INF;
dp[0][0]=0;
for(int i=0;i<M;++i){
for(int j=0;j<=N;++j){
int cost;
if(j)cost=imos[j-1];
else cost=0;
if(cost>t[i])continue;
if(j>0){
dp[i+1][j]=max(dp[i][j-1]+h[j-1],dp[i+1][j]);
}
if(j>1){
dp[i+1][j]=max(dp[i+1][j-1]+h[j-1]+abs(h[j-2]-h[j-1]),dp[i+1][j]);
}
}
for(int j=0;j<=N;++j)dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
for(int j=0;j<=N;++j){
if(dp[i+1][j]<s[i])dp[i+1][j]=-INF;
}
}
for(int i=0;i<=N;++i){
if(dp[M][i]!=-INF){
if(!i)cout<<t[M-1]<<endl;
else cout<<t[M-1]-imos[i-1]<<endl;
return ;
}
}
cout<<-1<<endl;
}
signed main(){
cin>>N>>M;
for(int i=0;i<N;++i)cin>>v[i]>>h[i];
for(int i=0;i<M;++i)cin>>t[i]>>s[i];
for(int i=0;i<N;++i){
if(!i){
imos[i]=v[i];
}
else imos[i]=imos[i-1]+v[i];
}
solve();
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }
const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 }; // 4近傍(右, 下, 左, 上)
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };
using namespace std;
const int MOD = 1000000000 + 7;
enum {
NOTFOUND = 0xFFFFFFFFFFFFFFFFLLU
};
class SuccinctBitVector {
private:
const uint64_t size; // ビットベクトルのサイズ
const uint32_t blockBitNum = 64;
std::vector<uint64_t> B; // ビットベクトル
const uint32_t LEVEL_L = 65536;
const uint32_t LEVEL_S = 256;
std::vector<uint64_t> L; // 大ブロック
std::vector<uint16_t> S; // 小ブロック
uint64_t numOne = 0; // 1bitの数
public:
explicit SuccinctBitVector(uint64_t n) : size(n) {
const uint64_t s = (n + blockBitNum - 1) / blockBitNum + 1; // ceil(n, blockSize)
this->B.assign(s, 0);
this->L.assign(n / LEVEL_L + 1, 0);
this->S.assign(n / LEVEL_S + 1, 0);
}
// B[pos] = bit
void setBit(uint64_t bit, uint64_t pos){
assert(bit == 0 or bit == 1);
assert(pos < this->size);
const uint64_t blockPos = pos / blockBitNum;
const uint64_t offset = pos % blockBitNum;
if (bit == 1) { B.at(blockPos) |= (1LLU << offset); }
else { B.at(blockPos) &= (~(1LLU << offset)); }
}
// B[pos]
uint64_t access(uint64_t pos) {
assert(pos < this->size);
const uint64_t blockPos = pos / blockBitNum;
const uint64_t offset = pos % blockBitNum;
return ((B.at(blockPos) >> offset) & 1);
}
void build(){
uint64_t num = 0;
for (uint64_t i = 0; i <= size; i++){
if (i % LEVEL_L == 0) {
L.at(i / LEVEL_L) = num;
}
if (i % LEVEL_S == 0) {
S.at(i / LEVEL_S) = num - L.at(i / LEVEL_L);
}
if (i != size and i % blockBitNum == 0) {
num += this->popCount(this->B.at(i / blockBitNum));
}
}
this-> numOne = num;
}
// B[0, pos)のbitの数
uint64_t rank(uint64_t bit, uint64_t pos) {
assert(bit == 0 or bit == 1);
assert(pos <= this->size);
if (bit) { return rank1(pos); }
else { return pos - rank1(pos); }
}
// rank番目のbitの位置 + 1(rankは1-origin)
uint64_t select(const uint64_t bit, const uint64_t rank) {
assert(bit == 0 or bit == 1);
assert(rank > 0);
if (bit == 0 and rank > this->size - this-> numOne) { return NOTFOUND; }
if (bit == 1 and rank > this-> numOne) { return NOTFOUND; }
// 大ブロックL内を検索
uint64_t large_idx = 0;
{
uint64_t left = 0;
uint64_t right = L.size();
while (right - left > 1) {
uint64_t mid = (left + right) / 2;
uint64_t r = L.at(mid);
r = (bit) ? r : mid * LEVEL_L - L.at(mid);
if (r < rank) {
left = mid;
large_idx = mid;
} else {
right = mid;
}
}
}
// 小ブロックS内を検索
uint64_t small_idx = (large_idx * LEVEL_L) / LEVEL_S;
{
uint64_t left = (large_idx * LEVEL_L) / LEVEL_S;
uint64_t right = std::min(((large_idx + 1) * LEVEL_L) / LEVEL_S, S.size());
while (right - left > 1) {
uint64_t mid = (left + right) / 2;
uint64_t r = L.at(large_idx) + S.at(mid);
r = (bit) ? r :mid * LEVEL_S - r;
if (r < rank) {
left = mid;
small_idx = mid;
} else {
right = mid;
}
}
}
// Bをブロック単位で順番に探索
uint64_t rank_pos = 0;
{
const uint64_t begin_block_idx = (small_idx * LEVEL_S) / blockBitNum;
uint64_t total_bit = L.at(large_idx) + S.at(small_idx);
if (bit == 0) {
total_bit = small_idx * LEVEL_S - total_bit;
}
for (uint64_t i = 0;; ++i) {
uint64_t b = popCount(B.at(begin_block_idx + i));
if (bit == 0) {
b = blockBitNum - b;
}
if (total_bit + b >= rank) {
uint64_t block = (bit) ? B.at(begin_block_idx + i) : ~B.at(begin_block_idx + i);
rank_pos = (begin_block_idx + i) * blockBitNum + selectInBlock(block, rank - total_bit);
break;
}
total_bit += b;
}
}
return rank_pos + 1;
}
uint64_t getNumOne() const {
return numOne;
}
void debug() {
std::cout << "LEVEL_L(" << L.size() << ")" << std::endl;
for (uint64_t i = 0 ; i < L.size(); ++i) {
std::cout << L.at(i) << ", ";
}
std::cout << std::endl;
std::cout << "LEVEL_S(" << S.size() << ")" << std::endl;
for (uint64_t i = 0 ; i < S.size(); ++i) {
std::cout << S.at(i) << ", ";
}
std::cout << std::endl;
}
private:
uint64_t rank1(uint64_t pos) {
uint64_t rank = L.at(pos / LEVEL_L) + S.at(pos / LEVEL_S);
const uint64_t begin = (pos / LEVEL_S) * LEVEL_S / blockBitNum;
const uint64_t end = pos / blockBitNum;
for (uint64_t i = begin; i < end; ++i) {
rank += popCount(this->B.at(i));
}
const uint64_t remain = (pos % LEVEL_S) % blockBitNum;
rank += popCount(this->B.at(end) & ((1LLU << remain) - 1));
return rank;
}
uint64_t popCount(uint64_t x) {
x = (x & 0x5555555555555555ULL) + ((x >> 1) & 0x5555555555555555ULL);
x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
x = x + (x >> 8);
x = x + (x >> 16);
x = x + (x >> 32);
return x & 0x7FLLU;
}
uint64_t selectInBlock(uint64_t x, uint64_t rank) {
uint64_t x1 = x - ((x & 0xAAAAAAAAAAAAAAAALLU) >> 1);
uint64_t x2 = (x1 & 0x3333333333333333LLU) + ((x1 >> 2) & 0x3333333333333333LLU);
uint64_t x3 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0FLLU;
uint64_t pos = 0;
for (;; pos += 8){
uint64_t rank_next = (x3 >> pos) & 0xFFLLU;
if (rank <= rank_next) break;
rank -= rank_next;
}
uint64_t v2 = (x2 >> pos) & 0xFLLU;
if (rank > v2) {
rank -= v2;
pos += 4;
}
uint64_t v1 = (x1 >> pos) & 0x3LLU;
if (rank > v1){
rank -= v1;
pos += 2;
}
uint64_t v0 = (x >> pos) & 0x1LLU;
if (v0 < rank){
rank -= v0;
pos += 1;
}
return pos;
}
};
class WaveletMatrix {
private:
std::vector<SuccinctBitVector> bit_arrays;
std::vector<uint64_t> begin_one; // 各bitに着目したときの1の開始位置
std::map<uint64_t, uint64_t> begin_alphabet; // 最後のソートされた配列で各文字の開始位置
uint64_t n; // 与えられた配列のサイズ
uint64_t num_of_alphabet; // 文字数
uint64_t num_of_bit; // 文字を表すのに必要なbit数
public:
WaveletMatrix () {
}
void build(const std::vector<uint64_t> &array) {
assert(array.size() > 0);
n = array.size();
num_of_alphabet = *max_element(array.begin(), array.end()) + 1;
num_of_bit = get_num_of_bit(num_of_alphabet);
if (num_of_bit == 0) {
num_of_bit = 1;
}
for (uint64_t i = 0; i < num_of_bit; ++i) {
SuccinctBitVector sv(n);
bit_arrays.push_back(sv);
}
this->begin_one.resize(num_of_bit);
std::vector<uint64_t> v(array);
for (uint64_t i = 0; i < num_of_bit; ++i) {
std::vector<uint64_t> temp;
// 0をtempにいれてく
for (uint64_t j = 0; j < v.size(); ++j) {
uint64_t c = v.at(j);
uint64_t bit = (c >> (num_of_bit - i - 1)) & 1; // 上からi番目のbit
if (bit == 0) {
temp.push_back(c);
bit_arrays.at(i).setBit(0, j);
}
}
this->begin_one.at(i) = temp.size();
// 1をtempにいれてく
for (uint64_t j = 0; j < v.size(); ++j) {
uint64_t c = v.at(j);
uint64_t bit = (c >> (num_of_bit - i - 1)) & 1; // 上からi番目のbit
if (bit == 1) {
temp.push_back(c);
bit_arrays.at(i).setBit(1, j);
}
}
bit_arrays.at(i).build();
v = temp;
}
// ソートされた配列内での各文字の位置を取得
for (int i = v.size() - 1; i >= 0; --i) {
this->begin_alphabet[v.at(i)] = i;
}
}
// v[pos]
uint64_t access(uint64_t pos) {
if (pos >= this->n) { return NOTFOUND; }
uint64_t c = 0;
for (uint64_t i = 0; i < bit_arrays.size(); ++i) {
uint64_t bit = bit_arrays.at(i).access(pos); // もとの数値がのi番目のbit
c = (c <<= 1) | bit;
pos = bit_arrays.at(i).rank(bit, pos);
if (bit) {
pos += this->begin_one.at(i);
}
}
return c;
}
// i番目のcの位置 + 1を返す。rankは1-origin
uint64_t select(uint64_t c, uint64_t rank) {
assert(rank > 0);
if (c >= num_of_alphabet) {
return NOTFOUND;
}
if (this->begin_alphabet.find(c) == this->begin_alphabet.end()) {
return NOTFOUND;
}
uint64_t index = this->begin_alphabet.at(c) + rank;
for (uint64_t i = 0; i < bit_arrays.size(); ++i){
uint64_t bit = ((c >> i) & 1); // 下からi番目のbit
if (bit == 1) {
index -= this->begin_one.at(num_of_bit - i - 1);
}
index = this->bit_arrays.at(num_of_bit - i - 1).select(bit, index);
}
return index;
}
// v[begin_pos, end_pos)で最大値のindexを返す
uint64_t maxRange(uint64_t begin_pos, uint64_t end_pos) {
return quantileRange(begin_pos, end_pos, end_pos - begin_pos - 1);
}
// v[begin_pos, end_pos)で最小値のindexを返す
uint64_t minRange(uint64_t begin_pos, uint64_t end_pos) {
return quantileRange(begin_pos, end_pos, 0);
}
// v[begin_pos, end_pos)でk番目に小さい数値のindexを返す(kは0-origin)
// つまり小さい順に並べてk番目の値
uint64_t quantileRange(uint64_t begin_pos, uint64_t end_pos, uint64_t k) {
if ((end_pos > n || begin_pos >= end_pos) || (k >= end_pos - begin_pos)) {
return NOTFOUND;
}
uint64_t val = 0;
for (uint64_t i = 0; i < num_of_bit; ++i) {
uint64_t num_of_zero_begin = bit_arrays.at(i).rank(0, begin_pos);
uint64_t num_of_zero_end = bit_arrays.at(i).rank(0, end_pos);
uint64_t num_of_zero = num_of_zero_end - num_of_zero_begin; // beginからendまでにある0の数
uint64_t bit = (k < num_of_zero) ? 0 : 1; // k番目の値の上からi番目のbitが0か1か
if (bit) {
k -= num_of_zero;
begin_pos = this->begin_one.at(i) + begin_pos - num_of_zero_begin;
end_pos = this->begin_one.at(i) + end_pos - num_of_zero_end;
}
else {
begin_pos = num_of_zero_begin;
end_pos = num_of_zero_begin + num_of_zero;
}
val = ((val << 1) | bit);
}
uint64_t rank = begin_pos + k - this->begin_alphabet.at(val) + 1;
return select(val, rank) - 1;
}
// v[0, pos)のcの数
uint64_t rank(uint64_t c, uint64_t pos) {
assert(pos < n);
if (c >= num_of_alphabet) {
return 0;
}
if (this->begin_alphabet.find(c) == this->begin_alphabet.end()) {
return 0;
}
for (uint64_t i = 0; i < num_of_bit; ++i) {
uint64_t bit = (c >> (num_of_bit - i - 1)) & 1; // 上からi番目のbit
pos = bit_arrays.at(i).rank(bit, pos); // cのi番目のbitと同じ数値の数
if (bit) {
pos += this->begin_one.at(i);
}
}
uint64_t begin_pos = this->begin_alphabet.at(c);
return pos - begin_pos;
}
// v[0, pos)でcより小さい文字の数
uint64_t rankLessThan(uint64_t c, uint64_t pos) {
uint64_t rank_less_than = 0, rank_more_than = 0, rank = 0;
rankAll(c, 0, pos, rank, rank_less_than, rank_more_than);
return rank_less_than;
}
// v[0, pos)でcより大きい文字の数
uint64_t rankMoreThan(uint64_t c, uint64_t pos) {
uint64_t rank_less_than = 0, rank_more_than = 0, rank = 0;
rankAll(c, 0, pos, rank, rank_less_than, rank_more_than);
return rank_more_than;
}
// v[begin_pos, end_pos)でcと同じ値の数、cより小さい値の数、cより大きい値の数を求める
void rankAll(uint64_t c, uint64_t begin_pos, uint64_t end_pos, uint64_t &rank, uint64_t &rank_less_than, uint64_t &rank_more_than) {
if (c >= num_of_alphabet || begin_pos >= n || end_pos > n) {
rank_less_than = NOTFOUND;
rank_more_than = NOTFOUND;
rank = NOTFOUND;
return;
}
rank_less_than = 0;
rank_more_than = 0;
rank = 0;
if (begin_pos >= end_pos) {
return;
}
std::vector<uint64_t> more_and_less(2, 0);
for (uint64_t i = 0; i < num_of_bit; ++i) {
const uint64_t bit = (c >> (num_of_bit - i - 1)) & 1;
const uint64_t range_bits = end_pos - begin_pos;
const uint64_t begin_zero = bit_arrays.at(i).rank(0, begin_pos);
const uint64_t end_zero = bit_arrays.at(i).rank(0, end_pos);
if (bit) {
begin_pos += this->begin_one.at(i) - begin_zero;
end_pos += this->begin_one.at(i) - end_zero;
} else {
begin_pos = begin_zero;
end_pos = end_zero;
}
more_and_less.at(bit) += range_bits - (end_pos - begin_pos);
}
rank_less_than = more_and_less.at(1);
rank_more_than = more_and_less.at(0);
rank = end_pos - begin_pos;
}
// T[s1, e1)で出現回数が多い順にk個の値を返す
std::vector<std::pair<uint64_t, uint64_t>> topk(uint64_t s, uint64_t e, uint64_t k) {
assert(s < e);
std::vector<std::pair<uint64_t, uint64_t>> result;
auto c = [](const std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &l, const std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t> &r) {
// width
if (std::get<0>(l) != std::get<0>(r)) {
return std::get<0>(l) < std::get<0>(r);
}
// depth
if (std::get<3>(l) != std::get<3>(r)) {
return std::get<3>(l) > std::get<3>(r);
}
// value
if (std::get<4>(l) != std::get<4>(r)) {
return std::get<4>(l) > std::get<4>(r);
}
return true;
};
std::priority_queue<std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>, std::vector<std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>>, decltype(c)> que(c); // width, left, right, depth, value
que.push(std::make_tuple(e - s, s, e, 0, 0));
while (not que.empty()) {
auto element = que.top(); que.pop();
uint64_t width, left, right, depth, value;
std::tie(width, left, right, depth, value) = element;
if (depth >= this->num_of_bit) {
result.emplace_back(std::make_pair(value, right - left));
if (result.size() >= k) {
break;
}
continue;
}
// 0
const uint64_t left0 = this->bit_arrays.at(depth).rank(0, left);
const uint64_t right0 = this->bit_arrays.at(depth).rank(0, right);
if (left0 < right0) {
que.push(std::make_tuple(right0 - left0, left0, right0, depth + 1, value));
}
// 1
const uint64_t left1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, left);
const uint64_t right1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, right);
if (left1 < right1) {
que.push(std::make_tuple(right1 - left1, left1, right1, depth + 1, value | (1 << num_of_bit - depth - 1)));
}
}
return result;
};
// T[s1, e1)とT[s2, e2)に共通して出現する要素を求める
std::vector<std::tuple<uint64_t, uint64_t, uint64_t>> intersect(uint64_t _s1, uint64_t _e1, uint64_t _s2, uint64_t _e2) {
assert(_s1 < _e1);
assert(_s2 < _e2);
std::vector<std::tuple<uint64_t, uint64_t, uint64_t>> intersection;
std::queue<std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>> que; // s1, e1, s2, e2, depth, value
que.push(std::make_tuple(_s1, _e1, _s2, _e2, 0, 0));
while (not que.empty()) {
auto e = que.front(); que.pop();
uint64_t s1, e1, s2, e2, depth, value;
std::tie(s1, e1, s2, e2, depth, value) = e;
if (depth >= this->num_of_bit) {
intersection.emplace_back(std::make_tuple(value, e1 - s1, e2 - s2));
continue;
}
// 0
uint64_t s1_0 = this->bit_arrays.at(depth).rank(0, s1);
uint64_t e1_0 = this->bit_arrays.at(depth).rank(0, e1);
uint64_t s2_0 = this->bit_arrays.at(depth).rank(0, s2);
uint64_t e2_0 = this->bit_arrays.at(depth).rank(0, e2);
if (s1_0 != e1_0 and s2_0 != e2_0) {
que.push(std::make_tuple(s1_0, e1_0, s2_0, e2_0, depth + 1, value));
}
// 1
uint64_t s1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s1);
uint64_t e1_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e1);
uint64_t s2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, s2);
uint64_t e2_1 = this->begin_one.at(depth) + this->bit_arrays.at(depth).rank(1, e2);
if (s1_1 != e1_1 and s2_1 != e2_1) {
que.push(std::make_tuple(s1_1, e1_1, s2_1, e2_1, depth + 1, value | (1 << num_of_bit - depth - 1)));
}
}
return intersection;
};
private:
uint64_t get_num_of_bit(uint64_t x) {
if (x == 0) return 0;
x--;
uint64_t bit_num = 0;
while (x >> bit_num) {
++bit_num;
}
return bit_num;
}
};
class SuffixArray {
int n, k;
std::vector<int> rank;
std::vector<int> tmp;
public:
const std::string s;
std::vector<uint64_t> sa;
std::vector<int> lcp;
public:
SuffixArray(const std::string &s) : n(s.size()), s(s) {
sa.assign(n + 1, 0);
lcp.assign(n, 0);
rank.assign(n + 1, 0);
tmp.assign(n + 1, 0);
}
void build() {
this->construct_sa();
this->construct_lcp();
}
// sがpatternを含んでいるか
bool contain(const string &pattern) {
int left = 0, right = n;
while (right - left > 1) {
int c = (left + right) / 2;
// Sのsa[c]文字目から|T|文字とTを比較
if (s.compare(sa[c], pattern.length(), pattern) < 0) {
left = c;
}
else {
right = c;
}
}
return s.compare(sa[right], pattern.length(), pattern) == 0;
}
// sa上でのpatternの範囲(閉区間)
pair<int, int> range(const string &pattern) {
return make_pair(lower_bound(pattern), upper_bound(pattern));
};
// sa上で最初にpatternが現れる位置
int lower_bound(const string &pattern) {
int left = -1, right = n;
while (right - left > 1) {
int c = (left + right) / 2;
int ret = s.compare(sa[c], pattern.length(), pattern);
// patternが大きい
if (ret >= 0) {
right = c;
}
else {
left = c;
}
}
if (s.compare(sa[right], pattern.length(), pattern) == 0) {
return right;
}
return -1;
}
// sa上で最後にpatternが現れる位置
int upper_bound(const string &pattern) {
int left = -1, right = n + 1;
while (right - left > 1) {
int c = (left + right) / 2;
int ret = s.compare(sa[c], pattern.length(), pattern);
// patternが小さい
if (ret <= 0) {
left = c;
}
else {
right = c;
}
}
if (s.compare(sa[left], pattern.length(), pattern) == 0) {
return left;
}
return -1;
}
void debug() {
std::cout << "idx lcp sa sa[i]" << std::endl;
for (int i = 0; i < n; ++i) {
std::cout << i << " " << lcp[i] << " " << sa[i] << " " << s.substr(sa[i]) << std::endl;
}
}
private:
struct compare {
compare(const SuffixArray &sa) : sa(sa) {}
const SuffixArray &sa;
// (rank[i], rank[i + k])と(rank[j], rank[j + k])を比較
bool operator()(const int &i, const int &j) {
if (sa.rank[i] != sa.rank[j]) {
return sa.rank[i] < sa.rank[j];
}
else {
int ri = i + sa.k <= sa.n ? sa.rank[i + sa.k] : -1;
int rj = j + sa.k <= sa.n ? sa.rank[j + sa.k] : -1;
return ri < rj;
}
}
};
void construct_sa() {
for (int i = 0; i <= n; i++) {
sa[i] = i;
rank[i] = i < n ? s[i] : -1;
}
// k文字についてソートされているところから、2k文字でソートする
for (k = 1; k <= n; k *= 2) {
sort(sa.begin(), sa.end(), compare(*this));
// いったんtmpに次のランクを計算し、それからrankに移す
tmp[sa[0]] = 0;
for (int i = 1; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - 1]] + (compare(*this).operator()(sa[i - 1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; i++) {
rank[i] = tmp[i];
}
}
}
void construct_lcp() {
for (int i = 0; i <= n; i++) {
rank[sa[i]] = i;
}
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n; i++) {
// 文字列中での位置iの接尾辞と、接尾辞配列中でその1つ前の接尾辞のLCPを求める
int j = sa[rank[i] - 1];
// hを先頭の分1減らし、後ろが一致しているだけ増やす
if (h > 0) {
h--;
}
for (; j + h < n and i + h < n; h++) {
if (s[j + h] != s[i + h]) {
break;
}
}
lcp[rank[i] - 1] = h;
}
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string S;
cin >> S;
SuffixArray sa(S);
sa.build();
WaveletMatrix wm;
wm.build(sa.sa);
int M;
cin >> M;
FOR(i, 0, M) {
string X, Y;
cin >> X >> Y;
auto x_range = sa.range(X);
auto y_range = sa.range(Y);
if (x_range.first == -1 or x_range.second == -1 or y_range.first == -1 or y_range.second == -1) {
print(0);
continue;
}
int x_begin = sa.sa[wm.minRange(x_range.first, x_range.second + 1)];
int x_end = x_begin + X.size() - 1;
int y_begin = sa.sa[wm.maxRange(y_range.first, y_range.second + 1)];
int y_end = y_begin + Y.size() - 1;
if (y_end < x_end) {
print(0);
continue;
}
print(y_end - x_begin + 1);
}
return 0;
}
| 0 | CPP |
n=int(input())
ans=0
for i in range(n):
q,w,e=map(int,input().split())
x=q+w+e
if x>=2:
ans+=1
print(ans) | 7 | PYTHON3 |
#include<cstdio>
int main(){
int n;
char a[10];
int ans = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%s", a);
if(a[0] == 'Y') ans = 1;
}
if(ans) puts("Four");
else puts("Three");
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)2e9 + 10;
const int mod = (int)1e9 + 7;
const int N = (int)1e6 + 10;
const long long LLINF = (long long)8e18 + 10;
const double pi = acos(-1.0);
template <typename T1, typename T2>
bool umin(T1 &x, const T2 &y) {
if (x > y) return x = y, true;
return false;
}
template <typename T1, typename T2>
bool umax(T1 &x, const T2 &y) {
if (x < y) return x = y, true;
return false;
}
template <typename T>
T getint() {
char c = getchar();
T p = 1, x = 0;
while (c == ' ' || c == '\n') c = getchar();
if (c == '-') p = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * p;
}
struct edge {
int x, y, cap, flow;
edge(int X, int Y, int CAP, int FLOW) {
x = X;
y = Y;
cap = CAP;
flow = FLOW;
}
};
vector<edge> e;
vector<int> g[N];
int n, m, maxn;
int tox[N], toy[N];
int d[N], ptr[N];
void add(int x, int y, int cap) {
g[x].push_back(e.size());
e.push_back(edge(x, y, cap, 0));
g[y].push_back(e.size());
e.push_back(edge(y, x, 0, 0));
}
bool bfs(int st, int fi) {
queue<int> q;
q.push(st);
for (int i = 1; i <= maxn; ++i) {
d[i] = INF;
ptr[i] = 0;
}
d[st] = 0;
while (q.size()) {
int v = q.front();
q.pop();
for (int i = 0; i < g[v].size(); ++i) {
int id = g[v][i], u = e[id].y;
if (e[id].flow < e[id].cap && umin(d[u], d[v] + 1)) q.push(u);
}
}
return d[fi] != INF;
}
int dfs(int v, int fi, int flow) {
if (v == fi) return flow;
if (flow == 0) return 0;
for (; ptr[v] < g[v].size(); ++ptr[v]) {
int id = g[v][ptr[v]], u = e[id].y;
if (e[id].flow < e[id].cap && d[u] == d[v] + 1) {
int push = dfs(u, fi, min(flow, e[id].cap - e[id].flow));
if (push) {
e[id].flow += push;
e[id ^ 1].flow -= push;
return push;
}
}
}
return 0;
}
int dinic(int st, int fi) {
int res = 0;
while (bfs(st, fi)) {
while (int pushed = dfs(st, fi, INF)) res += pushed;
}
return res;
}
void init(int sz) {
for (int i = 0; i < n; ++i) {
e[i * 2].cap = sz;
}
for (int i = 0; i < e.size(); ++i) e[i].flow = 0;
}
int main() {
srand(time(NULL));
n = getint<int>();
m = getint<int>();
maxn = n + m + 2;
for (int i = 1; i <= n; ++i) {
add(1, i + 1, 0);
}
for (int i = 1; i <= m; ++i) {
int x = getint<int>(), y = getint<int>();
add(x + 1, i + n + 1, 1);
add(y + 1, i + n + 1, 1);
tox[i] = x;
toy[i] = y;
}
for (int i = 1; i <= m; ++i) {
add(i + n + 1, maxn, 1);
}
int l = -1, r = n;
while (r - l > 1) {
int mid = (l + r) / 2;
init(mid);
if (dinic(1, maxn) >= m)
r = mid;
else
l = mid;
}
init(r);
dinic(1, maxn);
printf("%d\n", r);
for (int i = 0; i < e.size(); i += 2) {
if (e[i].x == 1 || e[i].y == maxn) continue;
int tx = tox[e[i].y - n - 1], ty = toy[e[i].y - n - 1];
if (e[i].flow) printf("%d %d\n", e[i].x - 1, (tx == e[i].x - 1 ? ty : tx));
}
}
| 16 | CPP |
a, b = [int(i) for i in input().split()]
print(a-b)
| 0 | PYTHON3 |
print(3*((int(input())**2))) | 0 | PYTHON3 |
n, h, a, b, k = list(map(int, input().split()))
def ans(t1, f1, t2, f2):
res = abs(t2 - t1)
if res == 0:
res += abs(f2 - f1)
else:
ff = -1
if f1 > b:
res += f1 - b
ff = b
elif f1 < a:
res += a - f1
ff = a
else:
ff = f1
res += abs(ff - f2)
return res
for i in range(k):
t1, f1, t2, f2 = list(map(int, input().split()))
print(ans(t1, f1, t2, f2))
| 7 | PYTHON3 |
tt=[0]*24*60
n=int(input())
k=0
for i in range(n):
h,m=map(int,input().split())
tt[h*60+m]+=1
if tt[h*60+m]>k:
k=tt[h*60+m]
print(k) | 7 | PYTHON3 |
a, b, c = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
ans0 = round(abs(x1 - x2) + abs(y1 - y2), 9)
if a * b ==0:
print(ans0)
raise SystemExit
x11 = (-c - b * y1) / a
y12 = (-c - a * x1) / b
x21 = (-c - b * y2) / a
y22 = (-c - a * x2) / b
ans1 = abs(x1 - x11) + abs(x21 - x2) + ((y2 - y1)**2 + (x21 - x11)**2)**0.5
ans2 = abs(y1 - y12) + abs(x21 - x2) + ((y2 - y12)**2 + (x21 - x1)**2)**0.5
ans3 = abs(y1 - y12) + abs(y2 - y22) + ((x1 - x2)**2 + (y12 - y22)**2)**0.5
ans4 = abs(x1 - x11) + abs(y22 - y2) + ((x11 - x2)**2 + (y1 - y22)**2)**0.5
ans0 = min(ans0, ans1, ans2, ans3, ans4)
print(round(ans0, 10))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}
typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;
const int inf = 1LL << 55;
const int mod = 1e9 + 7;
int H, N, P, M, K;
vint line;
vector<vint> amida;
double dp[2][128][128];
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> H >> N >> P >> M >> K;
--P;
resz(line, H, -1);
rep(i, M) {
int a, b;
cin >> a >> b;
line[H-a] = --b;
}
memset(dp, 0, sizeof(dp));
double (*curr)[128] = dp[0];
double (*next)[128] = dp[1];
curr[0][P] = 1;
rep(i, H) {
rep(k, K+1) rep(j, N) next[k][j] = 0;
if(~line[i]) { // ???????????????????????????????????????????¢???????swap
rep(k, K+1) rep(j, N) next[k][j] = curr[k][j];
rep(k, K+1) {
next[k][line[i]] = curr[k][line[i]+1];
next[k][line[i]+1] = curr[k][line[i]];
}
swap(curr, next);
continue;
}
int cnt = 0; // ??????????????????????±??????£???????????????????????°
reps(j, i, H) if(line[j] == -1) cnt++;
if(cnt == 0) {
rep(k, K+1) rep(j, N) next[k][j] += curr[k][j];
continue;
}
rep(k, K+1) {
double p = (double)(K-k)/cnt; // ??????cnt?????????????????????(K-k)????????¬???????????°??????
rep(j, N) next[k][j] += curr[k][j]*(1-p); // ??????i????????°??????????????°??????????????????
}
rep(k, K) {
double p = (double)(K-k)/cnt; // ??????cnt?????????????????????(K-k)????????¬???????????°??????
rep(j, N) {
double x = 0;
if(j > 0) {
next[k+1][j-1] += curr[k][j]*p/(N-1); // ??????i????????°????????????????£?j-1&j????????????????¢???????p*1/(N-1)
x += 1.0/(N-1);
}
if(j < N-1) {
next[k+1][j+1] += curr[k][j]*p/(N-1); // ?£?j&j+1
x += 1.0/(N-1);
}
next[k+1][j] += curr[k][j]*p*(1-x); // ??????i????????°?????????(1-x)????¢??????§?£?j????????¢??????????????????????????????
}
}
swap(curr, next);
}
/*
rep(i, H+1) {
rep(k, K+1) {
rep(j, N) cout<<dp[i][k][j]<<" ";
cout<<endl;
}
cout<<endl;
}
*/
double ans = 0;
rep(i, N) chmax(ans, curr[K][i]);
cout << ans << endl;
return 0;
} | 0 | CPP |
// ※※※ 解答不能 ※※※
// ksun48氏
// https://atcoder.jp/contests/agc041/submissions/9192801
#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
int main(){
int n, m;
scanf("%d %d", &n, &m);
int T;
scanf("%d", &T);
vector<int> x(m), y(m);
rep(i, m){
scanf("%d %d", &x[i], &y[i]);
x[i]--; y[i]--;
}
if(T == 1){
using B = bitset<50000>;
vector<B> cur(n);
rep(i, n) cur[i][i].flip();
rep(i, m){
cur[x[i]] |= cur[y[i]];
cur[y[i]] |= cur[x[i]];
}
rep(i, n){
if((int)cur[i].count() == n){
vector<int> where(n, 0);
where[i] = 1;
string st(m, '.');
repr(j, m - 1, 0){
st[j] = "^v"[where[x[j]] < where[y[j]]];
where[x[j]] |= where[y[j]];
where[y[j]] |= where[x[j]];
}
printf("%s\n", st.c_str());
return 0;
}
}
puts("-1");
}else if(T == 2){
if(n == 2){
puts("-1");
return 0;
}
string st(m, '^');
vector<int> reach(3, 1);
int last = -1;
rep(i, m){
if(y[i] >= 3) continue;
int other = 3 - x[i] - y[i];
if(!reach[other]){
assert(last >= 0);
st[last] ^= ('^' ^ 'v');
reach[other] = true;
}
st[i] = '^';
reach[x[i]] = true;
reach[y[i]] = false;
last = i;
}
printf("%s\n", st.c_str());
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
int main() {
int n, i, f;
scanf("%d", &n);
i = (n + 1) / 3;
f = i / 12;
i = i % 12;
printf("%d %d\n", f, i);
return 0;
}
| 7 | CPP |
import bisect
n = int(input())
a = sorted(list(map(int, input().split())))
cnt = {}
for x in a:
if x in cnt:
cnt[x] += 1
else:
cnt[x] = 1
ans = 0
for i in range(n - 1, -1, -1):
for d in range(30, -1, -1):
k = bisect.bisect_left(a, (1 << d) - a[i])
if k < n and a[i] + a[k] == (1 << d) and (a[i] == a[k] and cnt[a[i]] >= 2 or a[i] != a[k] and cnt[a[i]] >= 1 and cnt[a[k]] >= 1):
ans += 1
cnt[a[i]] -= 1
cnt[a[k]] -= 1
print(ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline void file() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (0) {
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
}
}
const int PX[8] = {1, 0, -1, 0, 1, 1, -1, -1},
PY[8] = {0, 1, 0, -1, -1, 1, 1, -1}, N = 1e5 + 10, INF = 1e9,
MOD = 1e9 + 7;
const long long INFL = 1e18, MODL = 1e9 + 7;
const long double EPS = 1e-9;
inline int rnd(int l = 0, int r = INF) {
unsigned ans = rand();
ans = (ans << 8) ^ rand();
ans = (ans << 8) ^ rand();
ans = (ans << 8) ^ rand();
ans %= r - l + 1;
return int(ans + l);
}
int n, sz, x, MIN_L, MAX_R;
pair<int, int> t[4 * N];
int sum[N], v[N];
int get(int p) {
int v, l, r, mid, cnt;
v = 1;
l = 1;
r = sz;
cnt = 0;
while (1) {
if (t[v].first + !!cnt > 1) return -1;
if (t[v].first) cnt = t[v].second;
if (l == r) return cnt;
mid = (l + r) / 2;
if (p > mid) {
l = mid + 1;
v = v * 2 + 1;
} else {
r = mid;
v = v * 2;
}
}
}
void add_seg(int v, int l, int r, int x) {
if (l >= MIN_L && r <= MAX_R) {
t[v].second = x;
t[v].first++;
return;
}
if (r < MIN_L || l > MAX_R) return;
int mid = (l + r) / 2;
add_seg(v * 2, l, mid, x);
add_seg(v * 2 + 1, mid + 1, r, x);
}
inline int solve() {
int res = 0;
int ans = 1;
for (int i = 1; i <= n; ++i) {
int now, mx;
now = mx = 0;
for (int j = 1; j < i; ++j)
if (v[j] > mx) {
mx = v[j];
++now;
}
for (int j = i + 1; j <= n; ++j)
if (v[j] > mx) {
mx = v[j];
++now;
}
if (now == res) {
res = now;
ans = min(ans, v[i]);
}
if (now > res) {
res = now;
ans = v[i];
}
}
return ans;
}
int main() {
file();
cin >> n;
for (sz = 1; sz < n; sz *= 2)
;
for (int i = 1; i <= n; ++i) {
cin >> x;
v[i] = x;
int s = get(x);
if (s > 0) ++sum[s];
if (s == 0) --sum[x];
MIN_L = 1;
MAX_R = x;
add_seg(1, 1, sz, x);
}
x = 1;
for (int i = 1; i <= n; ++i)
if (sum[i] > sum[x]) x = i;
cout << x;
}
| 9 | CPP |
n = int(input())
print(3*(n**2)) | 0 | PYTHON3 |
#include <stdio.h>
int n, m, i, j, k;
int main(){
scanf("%d", &n);
printf("%d\n", (1000-n%1000)%1000);
return 0;
} | 0 | CPP |
# Why do we fall ? So we can learn to pick ourselves up.
n,k,m = map(int,input().split())
aa = [int(i) for i in input().split()]
aa.sort()
ss = sum(aa)
maxi = (ss+min(n*k,m))/n
for i in range(1,min(m+1,n)):
ss -= aa[i-1]
left = m-i
can_add = (n-i)*k
to_add = min(left,can_add)
maxi = max(maxi,(ss+to_add)/(n-i))
print(maxi)
"""
4 2 6
1 3 2 3
9 2 8
74578 2746 96295 86884 21198 28655 22503 7868 47942
"""
| 8 | PYTHON3 |
t=int(input())
Ans=[]
for i in range(0,t):
n=int(input())
A=list(map(int,input().split()))
temp=sum(A)/n
temp1=round(temp)
if temp1>temp:
Ans.append(temp1)
elif temp==temp1:
Ans.append(temp1)
else:
Ans.append(temp1+1)
for i in Ans:
print(i) | 7 | PYTHON3 |
tc =int(input())
for _ in range(tc):
n,x=arr=list(map(int,input().split()))
l=list(map(int,input().split()))
odd = 0
for a in l:
if a % 2 == 1:
odd += 1
if odd == 0 or (odd == n and x % 2 == 0) or (x == n and odd % 2 == 0):
print("NO")
else:
print("YES") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
long long n, k, m, ans;
long long a[N], b[N];
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = n; i; i--) b[i] = b[i + 1] + a[i];
sort(b + 2, b + n + 1);
for (int i = n; i > n - k + 1; i--) ans += b[i];
cout << ans + b[1];
}
| 9 | CPP |
N = int(input())
sq = int(N**0.5)
if(N==sq**2):
print(sq*2)
else:
if(sq+sq**2>=N):
print(2*sq+1)
else:
print(2*sq+2)
| 8 | PYTHON3 |
n = map(int,input().split())
l = list(map(int,input().split()))
l =(sorted(l))
#print(l)
for i in l:
print(i,end=" ")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, x, y, c[400];
int main() {
scanf("%d", &n);
int sum = 0, p = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
sum += c[i];
}
scanf("%d%d", &x, &y);
for (int i = 1; i <= n; i++) {
p += c[i];
if (p >= x && p <= y && sum - p <= y && sum - p >= x) {
printf("%d\n", i + 1);
return 0;
}
}
printf("%d\n", 0);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[111], x[111], saven;
long double f[2][200111], sum[2][200111];
int main() {
ios ::sync_with_stdio(0);
cin.tie(0);
cout << (fixed) << setprecision(12);
while (cin >> n >> m) {
int sumA = 0;
for (int i = (1), _b = (n); i <= _b; ++i) {
int x;
cin >> x;
--x;
a[i] = x;
sumA += x;
}
if (m == 1) {
cout << 1.0 << endl;
continue;
}
for (int i = (1), _b = (n); i <= _b; ++i) {
int cur = i % 2;
memset(f[cur], 0, sizeof f[cur]);
if (i == 1) {
for (int val = 0, _a = (m * i); val < _a; ++val)
if (val < m && val != a[i]) f[cur][val] = 1;
} else {
for (int val = 0, _a = (m * i); val < _a; ++val) {
f[cur][val] = sum[1 - cur][val];
if (val >= m) f[cur][val] -= sum[1 - cur][val - m];
if (val >= a[i]) f[cur][val] -= f[1 - cur][val - a[i]];
}
}
for (int val = 0, _a = (m * (i + 1)); val < _a; ++val) {
sum[cur][val] = f[cur][val];
if (val > 0) sum[cur][val] += sum[cur][val - 1];
}
}
long double t = sum[n % 2][sumA - 1];
for (int turn = 0, _a = (n); turn < _a; ++turn) t /= (m - 1);
cout << t * (m - 1.0) + 1 << endl;
}
}
| 9 | CPP |
import sys
sys.setrecursionlimit(2000)
from collections import Counter
# sys.stdin.readline()
from math import ceil
if __name__ == "__main__":
# single variables
t = [int(val) for val in input().split()][0]
for test in range(t):
x = [int(val) for val in input().split()][0]
print(int(ceil(float(x)/7.)))
| 7 | PYTHON3 |
t = int(input())
for k in range(t):
ans = 0
n = int(input())
arr = []
arr.append(str(input().strip()))
arr.append(str(input().strip()))
i, rch, cr = 0, 0, 0
while i < n:
if arr[cr][i] == '1' or arr[cr][i] == '2':
if rch == 0:
i += 1
else:
break
else:
if rch == 0:
rch = 1
if cr == 0:
cr = 1
else:
cr = 0
else:
rch = 0
i += 1
if i == n and cr == 1:
print("YES")
ans = 1
if ans == 0:
print("NO") | 9 | PYTHON3 |
n=int(input())
a = [0] * 2
b = [0] * 2
for i in range(n):
t, x, y = map(int, input().split())
if t-1:
b[0]+=x
b[1]+=y
else:
a[0]+=x
a[1]+=y
if a[0]>=a[1]:
print('LIVE')
else:
print('DEAD')
if b[0]>=b[1]:
print('LIVE')
else:
print('DEAD')
| 7 | PYTHON3 |
s=input()
a,b,c,d,e=[x for x in input().split()]
if((s[0] in a) or (s[1] in a) or (s[0] in b) or (s[1] in b) or s[0] in c or s[1] in c or s[0] in d or s[1] in d or s[0] in e or s[1] in e ):
print('YES')
else:
print('NO') | 7 | PYTHON3 |
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
int main()
{
int N;
cin >> N;
vector<int> dists(N), ids(N), cnts(N);
for (int i = 0; i < N; i++) {
int id, dist, c;
cin >> id >> dist >> c;
dists[i] = dist;
cnts[i] = 20*c;
ids[i] = id;
}
vector<int> ws(1<<N, 0);
for (int s = 0; s < (1<<N); s++) {
for (int i = 0; i < N; i++) {
if (s & (1<<i)) {
ws[s] += cnts[i];
}
}
}
vector<vector<int> > prev(1<<N, vector<int>(N));
vector<vector<double> > dp(1<<N, vector<double>(N, numeric_limits<double>::max()));
for (int i = 0; i < N; i++) {
dp[1<<i][i] = 0;
prev[1<<i][i] = -1;
}
for (int s = 0; s < (1<<N); s++) {
const double v = 2000.0/(70.0+ws[s]);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!(s & (1<<i)) || s & (1<<j)) {
continue;
}
const double t = dp[s][i] + abs(dists[i]- dists[j]) / v;
if (dp[s|(1<<j)][j] > t) {
dp[s|(1<<j)][j] = t;
prev[s|(1<<j)][j] = i;
}
}
}
}
int last = -1;
double m = numeric_limits<double>::max();
for (int i = 0; i < N; i++) {
if (dp[(1<<N)-1][i] < m) {
m = dp[(1<<N)-1][i];
last = i;
}
}
vector<int> ans;
for (int s = (1<<N)-1, i = last; i >= 0;) {
ans.push_back(i);
const int j = prev[s][i];
s &= ~(1<<i);
i = j;
}
reverse(ans.begin(), ans.end());
for (int i = 0; i < N; i++) {
if (i != 0) {
cout << ' ';
}
cout << ids[ans[i]];
}
cout << endl;
return 0;
} | 0 | CPP |
a,b,c,d = map(int, input().split())
if (a+b == c+d) or (a+d == b+c) or (a+c == b+d):
print("YES")
elif (a+b+c == d) or (a+b+d == c) or (b+c+d == a) or (a+c+d == b):
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
char s[105];
int n;
int getint() {
char ch;
int f = 1;
while (!isdigit(ch = getchar()))
if (ch == '-') f = -1;
int x = ch - 48;
while (isdigit(ch = getchar())) x = x * 10 + ch - 48;
return x * f;
}
bool check() {
int rt = 0;
for (int i = 1; i <= n; ++i)
if (s[i] == '1') {
for (int j = i + 1; j <= n; ++j) rt += (s[j] == '0');
if (rt >= 6)
return 1;
else
return 0;
}
return 0;
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
check() ? puts("yes") : puts("no");
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T& a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T& a, T b) {
return a < b ? (a = b, true) : false;
}
struct dsu {
vector<int> e;
dsu(int n) : e(n, -1) {}
int size(int x) { return -e[get(x)]; }
bool same_set(int a, int b) { return get(a) == get(b); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b];
e[b] = a;
return true;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<tuple<int, int, int>> edges;
for (int i = 0; i < M; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
edges.emplace_back(w, u, v);
}
int s = get<1>(edges[0]);
int d = get<2>(edges[0]);
sort(edges.begin() + 1, edges.end());
priority_queue<pair<int, int>> heap;
for (int i = 1; i < M; ++i) heap.emplace(-get<0>(edges[i]), i);
dsu uf(N);
int ans = 1e9;
while (!heap.empty()) {
int id = heap.top().second;
heap.pop();
auto [w, u, v] = edges[id];
uf.unite(u, v);
if (uf.same_set(s, d)) {
uin(ans, w);
break;
}
}
cout << ans << '\n';
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
map<int, int> mp;
set<int> type;
for (int i = 0; i < n; i++) {
cin >> a[i];
type.insert(a[i]);
mp[a[i]] += 1;
}
int ans = 0;
for (int i = 0; i < n; i++) {
int x = mp[a[i]];
int y = type.size();
ans = max(ans, min(x - 1, y));
ans = max(ans, min(x, y - 1));
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
for (int tt = 0; tt < t; tt++) solve();
return 0;
}
| 9 | CPP |
for _ in range(int(input())):
n = int(input())
a=[]
start = 4*n
x=0
for i in range(n):
a.append(start-x)
x+=2
print(*a)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void mymax(T &_a, T _b) {
if (_a < _b) _a = _b;
}
template <typename T>
void mymin(T &_a, T _b) {
if (_a > _b) _a = _b;
}
template <typename T>
void print(T _x) {
for (auto _it = _x.begin(); _it != _x.end(); _it++) cout << *_it << " ";
cout << endl;
}
void print(int _x) { printf("%d ", _x); }
void print(long long _x) { printf("%lld ", _x); }
int n, m;
int juji[45][45];
int ans = (1000000000);
bool check(int jie) {
for (int i = 1; i <= jie; i++)
for (int j = 1; j <= m; j++)
if (juji[j][i] == 0) return 0;
return 1;
}
void dfs(int x, int y, int nowjuji) {
if (y == n + 1) {
if (check(n)) {
ans = min(ans, nowjuji);
}
return;
}
if (nowjuji > (m * y) / 3) return;
if (nowjuji >= ans) return;
if (x == m) {
if (y - 2 >= 1 && !check(y - 2)) return;
dfs(1, y + 1, nowjuji);
} else
dfs(x + 1, y, nowjuji);
juji[x - 1][y]++;
juji[x][y - 1]++;
juji[x + 1][y]++;
juji[x][y + 1]++;
juji[x][y]++;
if (x == m)
dfs(1, y + 1, nowjuji + 1);
else
dfs(x + 1, y, nowjuji + 1);
juji[x - 1][y]--;
juji[x][y - 1]--;
juji[x + 1][y]--;
juji[x][y + 1]--;
juji[x][y]--;
}
int main() {
memset(juji, 0, sizeof(juji));
scanf("%d %d", &n, &m);
if (n < m) swap(n, m);
dfs(1, 1, 0);
printf("%d\n", n * m - ans);
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
long long n, k;
cin >> n >> k;
long long t = 4 * 60 - k;
long long r = 0;
for (int i = 1; i <= n; i++) {
t -= i * 5;
if (t < 0) {
cout << (i - 1) << endl;
return 0;
}
}
cout << n << endl;
return 0;
}
| 7 | CPP |
k,n,s,p=map(int,input().split())
v=k*n
x=s*p
if n%s==0:
d=n//s
else:
d=n//s+1
d=d*k
if d%p==0:
d=d//p
else:
d=d//p+1
print(d)
| 7 | PYTHON3 |
t = int(input())
for i in range(t):
n = int(input())
for j in range(1,n+1):
print(((2*n - (j * 2)) // 2) * "(" + j * "()" + ((2*n - (j * 2)) // 2) * ")")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int K = 20;
const int MX = (1 << 20) + 5;
string s;
vector<int> Set;
bool vst[MX];
int dp[MX];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> s;
int n = s.size();
for (int i = 0; i < n; ++i) {
int r = min(n, i + K);
int mask = 0;
for (int j = i; j < r; ++j) {
int add = 1 << (s[j] - 'a');
if (mask & add) break;
mask ^= add;
if (!vst[mask]) {
Set.push_back(mask);
vst[mask] = true;
}
}
}
int res = 0, total = (1 << K) - 1;
for (int mask : Set) dp[mask] = __builtin_popcount(mask);
for (int i = 0; i <= total; ++i)
for (int j = 0; j < K; ++j)
if (i >> j & 1) dp[i] = max(dp[i], dp[i ^ (1 << j)]);
for (int mask : Set) res = max(res, dp[mask] + dp[mask ^ total]);
cout << res;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int LIM = 60;
long long sum(long long l, long long r) {
long long sum1 = l + r;
long long sum2 = r - l + 1;
if (sum1 % 2 == 0) {
sum1 /= 2;
} else {
sum2 /= 2;
}
sum1 %= mod, sum2 %= mod;
return (sum1 * sum2) % mod;
}
struct State {
long long path;
long long vid;
long long l, r;
State() {}
State(long long p, long long v, long long l, long long r)
: path(p), vid(v), l(l), r(r) {}
long long len() { return r - l + 1; }
pair<long long, pair<long long, long long> > get_len(long long targetlen) {
State cur(path, vid, l, r);
while (cur.len() != targetlen) {
cur = cur.parent();
}
return make_pair(cur.path, make_pair(cur.l, cur.r));
}
State parent() {
long long nvid = vid / 2;
long long npath = path / 2;
long long clen = len();
if (vid % 2 == 0) {
return State(npath, nvid, l, r + clen);
} else {
return State(npath, nvid, l - clen, r);
}
}
int dep() {
long long clen = len();
int ret = 0;
while (clen < (1LL << LIM)) {
++ret;
clen *= 2;
}
return ret;
}
pair<long long, long long> merge(long long nl, long long nr,
long long npath) {
long long merged = path ^ (npath);
long long extra = merged << (LIM - dep());
return make_pair(extra, extra + len() - 1);
}
};
vector<State> current;
void fill_state(long long v, long long l, long long r, long long path,
long long tl, long long tr) {
if (l == tl && r == tr) {
current.push_back(State(path, v, l, r));
return;
}
long long mid = (l + r) / 2;
if (tl > mid) {
fill_state(2 * v + 1, mid + 1, r, path * 2 + 1, tl, tr);
} else if (tr <= mid) {
fill_state(2 * v, l, mid, path * 2, tl, tr);
} else {
fill_state(2 * v, l, mid, path * 2, tl, mid);
fill_state(2 * v + 1, mid + 1, r, path * 2 + 1, mid + 1, tr);
}
}
bool cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
return a.second < b.second;
}
vector<pair<long long, long long> > ret;
void merge(vector<State>& a, vector<State>& b) {
for (long long i = LIM; i >= 0; --i) {
vector<State> cur;
for (auto& s : a) {
if (s.len() == (1LL << i)) {
cur.push_back(s);
}
}
set<pair<long long, pair<long long, long long> > > st;
for (auto& s : b) {
if (s.len() > (1LL << i)) {
continue;
}
auto v = s.get_len(1LL << i);
st.insert(v);
}
for (auto& s : cur) {
for (auto& p : st) {
ret.push_back(s.merge(p.second.first, p.second.second, p.first));
}
}
}
}
void solve() {
int n;
cin >> n;
current.clear();
int x1 = -1, y1 = -1;
for (int i = 0; i < n; ++i) {
long long x, y;
cin >> x >> y;
x1 = x, y1 = y;
fill_state(1, 0, (1LL << LIM) - 1, 0, x, y);
}
vector<State> a = current;
current.clear();
int m;
cin >> m;
int x2 = -1, y2 = -1;
for (int i = 0; i < m; ++i) {
long long x, y;
cin >> x >> y;
x2 = x, y2 = y;
fill_state(1, 0, (1LL << LIM) - 1, 0, x, y);
}
vector<State> b = current;
merge(a, b);
merge(b, a);
ret.reserve(5000000);
sort(ret.begin(), ret.end());
long long res = 0;
long long cl = ret[0].first;
long long cr = ret[0].second;
for (int i = 1; i < ret.size(); ++i) {
if (ret[i].first > cr) {
res += sum(cl, cr);
res %= mod;
cl = ret[i].first;
cr = ret[i].second;
} else {
cr = max(cr, ret[i].second);
}
}
res += sum(cl, cr);
res %= mod;
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
t = 1;
for (int tt = 0; tt < t; ++tt) {
solve();
}
return 0;
}
| 12 | CPP |
#include <iostream>
#include <vector>
using namespace std;
class TETORIS {
public:
int N;
vector<vector<int> > bd;
TETORIS() : N(0) {}
void put(int len, int pos) {
int k;
for (k=bd.size()-1; k>=0; --k) {
for (int i=0; i<len; ++i) {
if (bd[k][pos+i]) goto brk;
}
} brk:;
++k;
if ((int)bd.size() == k) bd.push_back(vector<int>(5, 0));
for (int i=0; i<len; ++i) {
bd[k][pos+i] = 1;
}
N += len;
}
void update() {
for (int i=bd.size()-1; i>=0; --i) {
if (bd[i][0] && bd[i][1] && bd[i][2] && bd[i][3] && bd[i][4]) {
bd.erase(bd.begin()+i);
N -= 5;
}
}
}
};
int main() {
int n;
while (cin >> n, n) {
TETORIS tet;
int d, p, q;
for (int i=0; i<n; ++i) {
cin >> d >> p >> q;
--q;
if (d == 1) {
tet.put(p, q);
tet.update();
} else {
for (int j=0; j<p; ++j) tet.put(1, q);
tet.update();
}
}
cout << tet.N << endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
struct data{
string name;
double rate;
};
bool cmp(data d1, data d2){
if(fabs(d1.rate - d2.rate) < 1e-8){
return d1.name < d2.name;
}
return d1.rate > d2.rate;
}
signed main(){
int n;
while(cin >> n, n){
string l;
double p, a, b, c, d, e, f, s, m;
vector<data> memo(n);
rep(i, 0, n){
cin >> l >> p >> a >> b >> c >> d >> e >> f >> s >> m;
double tmp = (f*m*s - p) / (a + b + c + m*(d + e));
memo[i].name = l;
memo[i].rate = tmp;
}
sort(all(memo), cmp);
rep(i, 0, n){
cout << memo[i].name << endl;
}
cout << "#" << endl;
}
} | 0 | CPP |
def countStr(string, str_len):
l_len, l_sum = 0, 0
sw_idx, ew_idx = -1, -1
l_count = {}
l_flag, sw_flag = False, False
for i in range(str_len):
s = string[i]
if (sw_flag and s == 'L'):
l_flag = True
l_len += 1
elif (s == 'W'):
if l_flag:
l_sum += l_len
l_count[l_len] = l_count.get(l_len, 0) + 1
l_len = 0
l_flag = False
if not sw_flag:
sw_flag = True
sw_idx = i
ew_idx = i
return l_sum, l_count, sw_idx, ew_idx
def reslove(wl_str, n, k):
l_sum, l_count, sw_idx, ew_idx = countStr(wl_str, n)
L_left_len = sw_idx
L_right_len = n - ew_idx - 1
L_all_len = L_left_len + l_sum + L_right_len
if sw_idx < 0:
if k == 0 or n == 0:
return 0
w = k if n > k else n
return 2 * w - 1
if L_all_len <= k:
return 2 * n - 1
if l_sum < k:
w = n - L_all_len + k
return 2 * w - 1
lens = list(l_count.keys())
lens.sort()
w = n - L_all_len
bp = sum(l_count.values()) + 1
for len in lens:
len_ct = l_count[len]
l_ct = len * len_ct
if l_ct <= k:
bp -= len_ct
w += l_ct
k -= l_ct
else:
filled = k // len
bp -= filled
w += k
break
return w * 2 - bp
t = int(input())
for i in range(t):
n, k = map(int, input().split())
wl_str = input()
print(reslove(wl_str, n, k))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60000000")
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = 3.1415926535897932384626433832795;
template <class T>
T sqr(T x) {
return x * x;
}
int n;
double a, d;
int main() {
cin >> n >> a >> d;
double max_t = 0;
for (int i = 0; i < (int)(n); i++) {
double t, v;
scanf("%lf%lf", &t, &v);
double tm;
if (v * v / a / 2. >= d)
tm = sqrt(2 * d / a);
else
tm = v / a + (d - (v * v / a / 2.)) / v;
tm += t;
if (tm <= max_t) tm = max_t;
max_t = tm;
printf("%.10lf\n", max_t);
}
return 0;
}
| 7 | CPP |
t=int(input())
while t:
t-=1
w=int(input())
if(w==1 or w==4 or w==2):
print(-1)
elif(w%3==0):
print(w//3,0,0)
elif(w%3==1):
print((w//3)-2,0,1)
else:
print(w//3-1,1,0) | 7 | PYTHON3 |
Subsets and Splits