solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N], n;
long long ans = 1e18, b[N];
void cal(long long x) {
for (int i = 1; i <= n; i++) {
b[i] = a[i];
}
long long sum1 = 0, sum2 = 0, mn = 0, pos = 0;
for (long long i = 1; i <= n;) {
if (sum1 + b[i] <= x) {
sum1 += b[i];
sum2 += b[i] * i;
if (pos) {
mn += b[i] * (i - pos);
} else if (sum1 >= x / 2 + x % 2) {
pos = i;
mn += sum1 * i - sum2;
}
i++;
} else if (sum1 + b[i] > x) {
if (pos) {
mn += (x - sum1) * (i - pos);
} else {
mn += sum1 * i - sum2;
}
b[i] -= x - sum1;
sum1 = 0;
sum2 = 0;
pos = 0;
}
}
ans = min(ans, mn);
}
int main() {
scanf("%d", &n);
long long sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
if (sum == 1) {
printf("-1");
return 0;
}
for (int i = 2; i <= sqrt(sum); i++) {
if (sum % i == 0) {
cal(i);
cal(sum / i);
}
}
cal(sum);
printf("%lld", ans);
}
| 11 | CPP |
n = int(input())
l = [0] * 10
for i in input().strip():
if i == "L":
for j in range (10):
if l[j] == 0:
l[j] = 1
break
elif i == "R":
for j in range (9 , -1, -1):
if l[j] == 0:
l[j] = 1
break
else:
l[int(i)] = 0
print("".join(map(str, l))) | 7 | PYTHON3 |
#-------------Program--------------
#----Kuzlyaev-Nikita-Codeforces----
#-------------Training-------------
#----------------------------------
n,k=map(int,input().split())
s=str(input())
alph="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
arr=[0]*26
for i in range(n):
arr[alph.index(s[i])]+=1
print(k*min(arr[0:k])) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n;
cin >> n;
long long a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> v;
v.push_back(a[0]);
for (i = 1; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
v.push_back(a[i]);
} else {
v.push_back(a[i + 1]);
i++;
}
}
if (v[v.size() - 1] != a[n - 1]) v.push_back(a[n - 1]);
long long min = INT_MAX;
for (i = 0; i < v.size(); i++) {
if (min > v[i]) min = v[i];
}
cout << min << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, x[200001], y[200001], tag[200001], fi[200001 << 1], nt[200001 << 1],
go[200001 << 1], root;
int cnt, top, low[200001], dfn[200001], id[200001], deg[200001], st[200001],
ans, cntx, sum, maxx;
void add(int u, int v) {
nt[++cnt] = fi[u];
fi[u] = cnt;
go[cnt] = v;
}
void tarjan(int u, int fa) {
dfn[u] = ++cnt;
low[u] = dfn[u];
st[++top] = u;
int t = 0;
for (int i = fi[u]; i; i = nt[i]) {
int v = go[i];
if (v == fa && !t) {
t++;
continue;
}
if (dfn[v]) {
low[u] = min(low[u], low[v]);
continue;
}
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
if (low[u] == dfn[u]) {
int y;
while (y = st[top--]) {
tag[y] = u;
if (y == u) break;
}
}
}
void dfs1(int u, int dep) {
dfn[u] = 1;
for (int i = fi[u]; i; i = nt[i]) {
int v = go[i];
if (dfn[v]) continue;
dfs1(v, dep + 1);
}
if (dep > maxx) root = u, maxx = dep;
}
void dfs2(int u, int dep, int fa) {
for (int i = fi[u]; i; i = nt[i]) {
int v = go[i];
if (v == fa) continue;
dfs2(v, dep + 1, u);
}
if (dep >= maxx) maxx = dep;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &x[i], &y[i]);
add(x[i], y[i]);
add(y[i], x[i]);
}
for (int i = 1; i <= n; i++) {
top = 0;
cnt = 0;
if (!dfn[i]) tarjan(i, 0);
}
memset(fi, 0, sizeof fi);
cnt = 0;
top = 0;
for (int i = 1; i <= n; i++)
if (tag[i] == i) id[i] = ++top;
for (int i = 1; i <= m; i++) {
if (tag[x[i]] != tag[y[i]]) {
add(id[tag[x[i]]], id[tag[y[i]]]), add(id[tag[y[i]]], id[tag[x[i]]]);
deg[id[tag[x[i]]]]++;
deg[id[tag[y[i]]]]++;
}
}
for (int i = 1; i <= top; i++) {
if (deg[i] == 1) ans++;
}
memset(dfn, 0, sizeof dfn);
for (int i = 1; i <= top; i++) {
if (!dfn[i] && deg[i] == 0) sum++, ans++;
if (!dfn[i] && deg[i] != 0) {
sum++;
dfs1(i, 1), dfs2(root, 1, 0);
root = 0;
ans += maxx - 2;
maxx = 0;
}
}
sum--;
ans = sum + n - ans;
cout << ans << endl;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n], b[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
long long x = *min_element(a, a + n);
long long y = *min_element(b, b + n);
long long sum = 0;
for (int i = 0; i < n; i++) {
a[i] = abs(a[i] - x);
b[i] = abs(b[i] - y);
sum += max(a[i], b[i]);
}
cout << sum << "\n";
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(void){
std::map<double, int> map4;
int p;
double t;
std::map<double, int> map1;
for (int i=0;i<8;i++) {
cin>>p>>t;
map1.insert(make_pair(t,p));
}
auto itr1=map1.begin();
printf("%d",itr1->second);
cout<<" ";
printf("%.2f",itr1->first);
cout<<endl;
itr1=next(itr1,1);
printf("%d",itr1->second);
cout<<" ";
printf("%.2f",itr1->first);
cout<<endl;
std::map<double, int> map2;
for (int j=0;j<8;j++) {
cin>>p>>t;
map2.insert(make_pair(t,p));
}
auto itr2=map2.begin();
printf("%d",itr2->second);
cout<<" ";
printf("%.2f",itr2->first);
cout<<endl;
itr2=next(itr2,1);
printf("%d",itr2->second);
cout<<" ";
printf("%.2f",itr2->first);
cout<<endl;
std::map<double, int> map3;
for (int j=0;j<8;j++) {
cin>>p>>t;
map3.insert(make_pair(t,p));
}
auto itr3=map3.begin();
printf("%d",itr3->second);
cout<<" ";
printf("%.2f",itr3->first);
cout<<endl;
itr3=next(itr3,1);
printf("%d",itr3->second);
cout<<" ";
printf("%.2f",itr3->first);
cout<<endl;
for (int k=0;k<6;k++) {
itr1=next(itr1,1);
map4.insert(make_pair(itr1->first,itr1->second));
itr2=next(itr2,1);
map4.insert(make_pair(itr2->first,itr2->second));
itr3=next(itr3,1);
map4.insert(make_pair(itr3->first,itr3->second));
}
auto itr4=map4.begin();
printf("%d",itr4->second);
cout<<" ";
printf("%.2f",itr4->first);
cout<<endl;
itr4=next(itr4,1);
printf("%d",itr4->second);
cout<<" ";
printf("%.2f",itr4->first);
cout<<endl;
}
| 0 | CPP |
import sys
t=int(input())
for i in range(t):
mine1=sys.maxsize
mine2=sys.maxsize
n,s,k=map(int,input().split())
arr=list(map(int,input().split()))
for i in range(s,0,-1):
if(i not in arr):
mine1=s-i
break
for i in range(s+1,n+1):
if(i not in arr):
mine2=i-s
break
print(min(mine1,mine2))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int DP[2][50][50];
int len[2];
int convert[50][3];
int rec[50][50];
int recur(int a, int b) {
if (a == len[0] && b == len[1]) return 0;
if (a == len[0] || b == len[1]) return 100;
if (rec[a][b] != -1) return rec[a][b];
int res = 100;
for (int i = a; i < len[0]; i++)
for (int j = b; j < len[1]; j++) {
if (DP[0][a][i] & DP[1][b][j]) {
res = min(recur(i + 1, j + 1) + 1, res);
}
}
rec[a][b] = res;
return res;
}
int main() {
memset(DP, 0, sizeof(DP));
memset(rec, -1, sizeof(rec));
for (int i = 0; i < 2; i++) {
string str;
cin >> str;
len[i] = str.length();
for (int j = 0; j < str.length(); j++) DP[i][j][j] = (1 << (str[j] - 'a'));
}
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
convert[i][0] = (1 << (str[0] - 'a'));
convert[i][1] = (1 << (str[3] - 'a'));
convert[i][2] = (1 << (str[4] - 'a'));
}
for (int i = 0; i < 2; i++)
for (int j = 0; j < len[i]; j++)
for (int k = 0; k + j < len[i]; k++)
for (int l = 0; l < j; l++)
for (int m = 0; m < n; m++) {
if ((DP[i][k][k + l] & convert[m][1]) &&
(DP[i][l + k + 1][j + k] & convert[m][2])) {
DP[i][k][j + k] |= convert[m][0];
}
}
int res = recur(0, 0);
if (res >= 100)
cout << "-1";
else
cout << res;
}
| 11 | CPP |
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split(' ')))
x = sorted(set(arr))
x = list(reversed(x))
if arr == x and len(arr) == len(x):
print('NO')
else:
print('YES') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
struct mint {
int x;
mint(int x) : x(x) {}
friend mint operator+(const mint &a, const mint b) {
const int x = a.x + b.x;
return mint(x >= M ? x - M : x);
}
friend mint operator-(const mint &a, const mint b) {
const int x = a.x - b.x;
return mint(x < 0 ? x + M : x);
}
friend mint operator*(const mint &a, const mint b) {
return mint(1ll * a.x * b.x % M);
}
};
int Pw(int a, int x = M - 2, int res = 1) {
for (; x; x >>= 1, a = (mint(a) * a).x) {
if (x & 1) {
res = (mint(res) * a).x;
}
}
return res;
}
const int N = 5e5;
struct par {
int u, v;
};
long long c[N];
int te[N];
map<long long, vector<par>> mp;
int R(int u) { return u == te[u] ? u : te[u] = R(te[u]); }
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, ne, nk;
cin >> n >> ne >> nk;
for (int u = (0); u < (n); ++u) {
cin >> c[u], te[u] = u;
}
for (int i = (0); i < (ne); ++i) {
int u, v;
cin >> u >> v, --u, --v;
mp[c[u] ^ c[v]].push_back({u, v});
}
int res = ((mint(Pw(2, nk)) - int((mp).size())) * Pw(2, n)).x;
for (const auto &C : mp) {
int nc = n;
for (const par &p : C.second) {
if (R(p.u) == R(p.v)) {
continue;
}
nc -= 1, te[R(p.u)] = R(p.v);
}
res = (res + mint(Pw(2, nc))).x;
for (const par &p : C.second) {
te[p.u] = p.u, te[p.v] = p.v;
}
}
cout << res << '\n';
return 0;
}
| 9 | CPP |
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
def value():
return map(int,input().split())
def array():
return [int(i) for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
s=input()
try:
ind=s.index('AB')
except:
print("NO")
exit()
if('BA' in s[ind+2:]):
print("YES")
exit()
try:
ind=s.index('BA')
except:
print("NO")
exit()
if('AB' in s[ind+2:]):
print("YES")
else:
print("NO")
| 7 | PYTHON3 |
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
using namespace std;
using ll = long long;
const ll INF = 2e9;
vector <vector<int>> gp;
vector<int> vis;
vector<int> dist;
vector<int> v;
string fibWord(int n)
{
string Sn_1 = "0";
string Sn = "01";
string tmp;
for (int i = 2; i <= n; i++)
{
tmp = Sn;
Sn += Sn_1;
Sn_1 = tmp;
}
return Sn;
}
void solution() {
int n; cin >> n;
cout << 2 - n * n << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int T; cin >> T;
//while (T--){
solution();
//}
} | 12 | CPP |
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;
typedef int Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) :
src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
vector<Weight> dijkstra(const Graph &g, int s) {
vector<Weight> dist(g.size(), INF);
dist[s] = 0;
priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight"
for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {
Edge e = Q.top(); Q.pop();
if (dist[e.dst] < e.weight) continue;
FOR(f,g[e.dst]) {
if (dist[f->dst] > e.weight+f->weight) {
dist[f->dst] = e.weight+f->weight;
Q.push(Edge(f->src, f->dst, e.weight+f->weight));
}
}
}
return dist;
}
int main() {
int n,m;
while(cin>>n>>m,n||m) {
Graph g(n*3);
REP(i,m) {
int a, b, c;
cin >> a >> b >> c;
a--; b--;
int a1 = a*3, b1 = b*3;
int a2 = a1+1, b2 = b1+1;
int a3 = a2+1, b3 = b2+1;
g[a1].push_back(Edge(a1,b1,c));
g[b1].push_back(Edge(b1,a1,c));
g[a1].push_back(Edge(a1,b2,0));
g[b1].push_back(Edge(b1,a2,0));
g[a2].push_back(Edge(a2,b3,0));
g[b2].push_back(Edge(b2,a3,0));
g[a3].push_back(Edge(a3,b3,c));
g[b3].push_back(Edge(b3,a3,c));
}
vector<int> dist = dijkstra(g,0);
int t = (n-1)*3;
cout << min(dist[t], dist[t+2]) << endl;
}
} | 0 | CPP |
#take two values sap by space call them n and k.
#n means no of participants and k is the score of kth participant to compare to all the n's
n,kthPlace = input().split(" ")
n,kthPlace = int(n),int(kthPlace)
if(1<=kthPlace<=n<=50):
participants = [(int(i)) for i in input().split(" ")]
compareValue = participants[kthPlace-1]
#working for the solution
count = 0
for score in participants:
if (score,compareValue)==(0,0):
continue
if score>=compareValue:
count+=1
# if(compareValue)==0 and count>0:
# count+=1
print(count)
| 7 | PYTHON3 |
a=[int(input()) for i in range(6)]
s=sum(a[0:4])-min(a[0:4])+max(a[4],a[5])
print(s)
| 0 | PYTHON3 |
input()
A = list(map(int, input().split())); A.sort()
s = 0
l = 0
for i in range(len(A)):
if s * 2 < A[i]:
l = i
s += A[i]
print(len(A) - l)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
template <class T>
T sqr(T x) {
return x * x;
}
template <class T>
T lcm(T a, T b) {
return a / __gcd(a, b) * b;
}
template <class T>
T minimize(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
T maximize(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
using namespace std;
int v1, v2, n, d;
int res = 0;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> v1 >> v2 >> n >> d;
for (int i = 1; i <= n; ++i) {
if (v1 <= v2) {
res += v1;
v1 += d;
} else {
res += v2;
v2 += d;
}
}
cout << res;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2 * 1e5 + 9;
const int MOD = 1e9 + 7;
const double epsilon = 1e-8;
bool equal(double a, double b) {
a = b - a;
return fabs(a) <= epsilon;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first > b.first;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, a[maxN], pre[maxN], s, e;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
cin >> s >> e;
pre[1] = a[1];
for (int i = 2; i <= 2 * n; i++) {
pre[i] = a[i] + pre[i - 1];
}
int best = 0, id;
e--;
for (int i = 1; i <= n; i++) {
int val = pre[n + e - i + 1] - pre[n + s - 1 - i + 1];
if (val > best) {
best = val;
id = i;
}
}
cout << id << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int n;
int a[N], F[N];
int main() {
int i, mymax = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= n; i++) F[a[i]] = F[a[i] - 1] + 1;
for (i = 1; i <= n; i++) mymax = max(mymax, F[i]);
cout << n - mymax;
return 0;
}
| 9 | CPP |
number,height=map(int,input().split())
heigh=list(map(int,input().split()))
width=0
for i in range(number):
if heigh[i]<=height:
width+=1
else:
width+=2
print(width) | 7 | PYTHON3 |
def zip_sorted(a,b):
# sorted by a
a,b = zip(*sorted(zip(a,b)))
# sorted by b
sorted(zip(a, b), key=lambda x: x[1])
return a,b
t = int(input())
for t1 in range(t):
n = int(input())
s = list(str(input()))
a = []
b = []
sum1 = []
sum2 = []
for k3 in range(len(s)):
if s[k3]=='>':
a.append(1)
b.append(0)
elif s[k3]=='-':
a.append(1)
b.append(1)
else:
a.append(0)
b.append(1)
if (sum(a)==n) or (sum(b)==n):
print(n)
else:
count = 0
for k3 in range(len(s)):
if k3!=0:
if s[k3]=='-' or s[k3-1]=='-':
count = count + 1
else:
if s[k3]=='-' or s[-1]=='-':
count = count + 1
print(count)
'''
for i in range(n):
for j in range(n):
for k1 in range(len(a)):
for k2 in range(len(a)):
for k3 in range(len(a)):
''' | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * asin(1.0);
const int INF = 1000000000;
const double EPS = 1e-10;
inline int cmp(double x, double y = 0, double tol = EPS) {
return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
int main() {
long long n, m, k;
while (cin >> n >> m >> k) {
if ((n - 1) + (m - 1) < k)
cout << "-1" << endl;
else {
long long a = min(n - 1, k);
long long b = k - a;
long long x = (n / (a + 1)) * (m / (b + 1));
b = min(m - 1, k);
a = k - b;
long long y = (n / (a + 1)) * (m / (b + 1));
cout << max(x, y) << endl;
}
}
return 0;
}
| 7 | CPP |
n = int(input())
a = list(map(int, input().split()))
m = 0
for i in range(n):
for j in range(i+1, n+1):
x = sum(a[:i]) + (j-i) - sum(a[i:j]) + sum(a[j:])
m = max(x, m)
print(m)
| 7 | PYTHON3 |
t=int(input())
for _ in range(t):
x,y,n=list(map(int,input().split()))
if n- (n%x)+y <=n:
print(n- (n%x)+y )
else:
print(n- (n%x)+y -x)
| 7 | PYTHON3 |
#map(int,input().split())
n=int(input())
s=list(input())
l=[0]*n
ct=0
for i in range(n):
if s[i]=='8':
l[ct]=i
ct+=1
if ct==0:
print('NO')
else:
if ct <= (n - 11)//2:
print('NO')
else:
ct = (n-11)//2
x = l[ct]
if x <= ct + (n-10)//2:
print('YES')
else:
print('NO') | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[5050];
int b[5050][5050];
int n;
void build() {
for (int i = 1; i <= n; i++) b[1][i] = a[i];
for (int k = 2; k <= n; k++) {
for (int i = 1; i <= n - k + 1; i++)
b[k][i] = b[k - 1][i] ^ b[k - 1][i + 1];
}
for (int i = 1; i <= n; i++)
for (int k = 2; k <= n + 1 - i; k++) b[k][i] = max(b[k - 1][i], b[k][i]);
}
int le[101010], ri[101010], ans[101010];
vector<int> v[5050];
bool cmp(int i, int j) { return le[i] > le[j]; }
void solve() {
for (int R = 1; R <= n; R++) {
sort(v[R].begin(), v[R].end(), cmp);
int L = R;
int now = 0;
for (int i = 0; i < v[R].size(); i++) {
int to = le[v[R][i]];
while (L >= to) now = max(now, b[R - L + 1][L]), L--;
ans[v[R][i]] = now;
}
}
}
int main() {
int q;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build();
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d %d", &le[i], &ri[i]);
v[ri[i]].push_back(i);
}
solve();
for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}
| 8 | CPP |
#include <iostream>
using namespace std;
int main(){
int n;
int cnt[6] = {0};
double a;
cin >> n;
while(n--){
cin >> a;
if(a < 165) cnt[0]++;
else if(a < 170) cnt[1]++;
else if(a < 175) cnt[2]++;
else if(a < 180) cnt[3]++;
else if(a < 185) cnt[4]++;
else cnt[5]++;
}
for(int i = 0 ; i < 6 ; i++){
cout << i+1 << ":";
for(int j = 0 ; j < cnt[i] ; j++){
cout << "*";
}
cout << endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
using namespace std;
long long fac[maxn];
long long inv[maxn];
long long sum[maxn];
char s[maxn];
long long a[maxn];
int n, k;
long long ans;
long long ten;
long long mod_pow(long long x, long long n) {
x %= mod;
long long res = 1;
while (n > 0) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
void init() {
inv[0] = 1;
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = (fac[i - 1] * i) % mod;
inv[i] = mod_pow(fac[i], mod - 2) % mod;
}
}
long long C(long long n, long long m) {
if (n < 0 || m < 0) return 0;
if (m > n) return 0;
return (((fac[n] * inv[m]) % mod) * inv[n - m]) % mod;
}
int main() {
cin >> n >> k;
init();
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) a[i] = s[i] - '0';
ten = 1;
for (int i = n - 1; i >= 1; i--) {
sum[i] = (sum[i + 1] + ten * C(i - 1, k - 1)) % mod;
ten = (ten * 10) % mod;
}
ten = 1;
for (int i = n; i >= k + 1; i--) {
sum[i] = (sum[i] + C(i - 1, k) * ten) % mod;
ten = (ten * 10) % mod;
}
ans = 0;
for (int i = 1; i <= n; i++) ans = (ans + sum[i] * a[i]) % mod;
cout << ans << endl;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
class DS {
public:
DS(int rows, int cols) : _rows(rows), _cols(cols), _ans(0) {
int n = rows + cols;
_p.resize(n, -1);
_r.resize(n, 0);
_nr.resize(n, 0);
_nc.resize(n, 0);
for (int i = 0; i < rows; ++i) _nr[i] = 1;
for (int i = 0; i < cols; ++i) _nc[rows + i] = 1;
}
long long ans() { return _ans; }
int find(int i) {
while (_p[i] >= 0) i = _p[i];
return i;
}
void merge(int r, int c) {
int i = find(r);
int j = find(_rows + c);
if (i == j) return;
if (_r[i] > _r[j]) swap(i, j);
_rb.push_back({i, j, _r[j]});
_p[i] = j;
if (_r[i] == _r[j]) ++_r[j];
_ans += (long long)_nr[i] * _nc[j] + (long long)_nc[i] * _nr[j];
_nr[j] += _nr[i];
_nc[j] += _nc[i];
}
int current_snapshot() { return _rb.size(); }
void rollback(int snapshot) {
while (_rb.size() > snapshot) {
int i = _rb.back().i;
int j = _rb.back().j;
int r = _rb.back().r;
_rb.pop_back();
_p[i] = -1;
_r[j] = r;
_nr[j] -= _nr[i];
_nc[j] -= _nc[i];
_ans -= (long long)_nr[i] * _nc[j] + (long long)_nc[i] * _nr[j];
}
}
private:
int _rows, _cols;
vector<int> _p, _r, _nr, _nc;
struct rb_t {
int i, j, r;
};
vector<rb_t> _rb;
long long _ans;
};
class Solution {
public:
Solution(vector<pair<int, int>> const& p, int mx, int my)
: _n(p.size()), _p(p), _seg(4 * _n), _ds(mx + 1, my + 1) {}
void solve() {
map<pair<int, int>, int> t;
for (int i = 0; i < _n; ++i) {
auto it = t.find(_p[i]);
if (it == t.end()) {
t[_p[i]] = i;
continue;
}
int j = it->second;
t.erase(it);
add_pt(0, 0, _n, j, i, _p[i]);
}
for (const auto& ti : t) {
add_pt(0, 0, _n, ti.second, _n, ti.first);
}
dfs(0, 0, _n);
}
private:
void add_pt(int u, int a, int b, int A, int B, const pair<int, int>& pt) {
if (a >= B || A >= b) return;
if (A <= a && b <= B) {
_seg[u].push_back(pt);
return;
}
int c = (a + b) >> 1;
add_pt(2 * u + 1, a, c, A, B, pt);
add_pt(2 * u + 2, c, b, A, B, pt);
}
void dfs(int u, int a, int b) {
int snapshot = _ds.current_snapshot();
for (const pair<int, int>& pt : _seg[u]) _ds.merge(pt.first, pt.second);
if (a + 1 == b) {
cout << _ds.ans() << " ";
} else {
int c = (a + b) >> 1;
dfs(2 * u + 1, a, c);
dfs(2 * u + 2, c, b);
}
_ds.rollback(snapshot);
}
private:
int _n;
vector<pair<int, int>> const& _p;
vector<vector<pair<int, int>>> _seg;
DS _ds;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
vector<pair<int, int>> p(q);
int mx = 0, my = 0;
for (pair<int, int>& pi : p) {
cin >> pi.first >> pi.second;
mx = max(mx, pi.first);
my = max(my, pi.second);
}
Solution(p, mx, my).solve();
return 0;
}
| 12 | CPP |
t = int(input())
if t == 1:
print("9 8")
else :
print("{} {}".format(t*3,t*2)) | 7 | PYTHON3 |
#include<iostream>
using namespace std;
int main() {
int n, m, x, y;
int a[105] = {0};
cin>>n>>m;
for (int i = 0; i < m; i++) {
cin>>x;
for (int j = 0; j < x; j++) {
cin>>y;
a[y] = 1;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++)
if (!a[i]) cnt++;
cout<<cnt;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const int inf = (int)1e9;
int n, m, q;
vector<int> g[N];
int used[N];
int col[N], c;
int dp[N];
int fup[N], tin[N], tout[N], up[20][N], timer;
void go(int v, int p) {
tin[v] = fup[v] = ++timer;
used[v] = 1;
up[0][v] = p;
for (int lvl = 1; lvl < 20; lvl++) up[lvl][v] = up[lvl - 1][up[lvl - 1][v]];
for (int i = 0; i < (int((g[v]).size())); i++) {
int to = g[v][i];
if (p == to) continue;
if (used[to]) {
fup[v] = min(fup[v], tin[to]);
} else {
go(to, v);
fup[v] = min(fup[v], fup[to]);
}
}
tout[v] = ++timer;
}
void calc(int v, int p, int w, int color) {
used[v] = 1;
dp[v] = w;
col[v] = color;
for (int i = 0; i < (int((g[v]).size())); i++) {
int to = g[v][i];
if (p == to || used[to]) continue;
int nc;
if (fup[to] < tin[v])
nc = col[v];
else
nc = ++c;
calc(to, v, dp[v] + (col[v] != nc), nc);
}
}
bool is_ancestor(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b) {
if (is_ancestor(a, b)) return a;
if (is_ancestor(b, a)) return b;
for (int i = 19; i >= 0; i--)
if (!is_ancestor(up[i][a], b)) a = up[i][a];
return up[0][a];
}
int dist(int a, int b) {
int pr = b;
for (int i = 19; i >= 0; i--)
if (!is_ancestor(up[i][pr], a)) pr = up[i][pr];
return dp[b] - dp[a] + (col[a] == col[pr]);
}
int get(int a, int b) {
if (is_ancestor(a, b)) return dist(a, b);
if (is_ancestor(b, a)) return dist(b, a);
int l = lca(a, b);
return dist(l, a) + dist(l, b);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
tin[0] = 0, tout[0] = inf;
cin >> n >> m >> q;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
go(1, 0);
memset(used, 0, sizeof used);
calc(1, 0, 0, 0);
for (int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y;
cout << get(x, y) << "\n";
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
int IT_MAX = 1 << 17;
const long long MOD = 1000000007;
const int INF = 0x1f3f3f3f;
const long long LL_INF = 1034567890123456789ll;
const double PI = acos(-1);
const double ERR = 1E-11;
multiset<int> Sx;
int in[100050];
int main() {
int N, K, i, j;
scanf("%d %d", &N, &K);
for (i = 1; i <= N; i++) scanf("%d", &in[i]);
int ans = -1;
vector<int> Va;
for (i = 1, j = 1; i <= N; i++) {
while (j <= N) {
if (Sx.empty()) {
Sx.insert(in[j++]);
continue;
}
auto it1 = Sx.begin();
auto it2 = Sx.end();
it2--;
if (abs(in[j] - *it1) > K || abs(in[j] - *it2) > K) break;
Sx.insert(in[j++]);
}
if (ans < j - i) {
ans = j - i;
Va.clear();
}
if (ans == j - i) Va.push_back(i);
Sx.erase(Sx.lower_bound(in[i]));
}
printf("%d %d\n", ans, (int)Va.size());
for (auto it : Va) printf("%d %d\n", it, it + ans - 1);
return 0;
}
| 11 | CPP |
for t in range(int(input())):
n, k=map(int,input().split())
lis = list(map(int,input().split()))
s = set(lis)
Max = max(lis)
min = 0
for i in range(Max+2):
if i not in s:
min=i
break
if min==Max+1:
print(n+k)
continue
if k>0:
s.add((Max+min+1)//2)
print(len(s)) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int colcnt[1009];
int dp[1009][1009][3];
long dots, hashs, n, m, x, y;
int solve(int col, int width, int last) {
if (col == m) {
if (width > y || width < x) {
return 1000000000;
} else {
return 0;
}
}
if (dp[col][width][last] != -1) {
return dp[col][width][last];
}
long dots = 1000000000;
if (col == 0 || (last == 0 && width + 1 <= y) || (last == 1 && width >= x)) {
dots = n - colcnt[col] + solve(col + 1, last == 1 ? 1 : width + 1, 0);
}
long hashes = 1000000000;
if (col == 0 || (last == 1 && width + 1 <= y) || (last == 0 && width >= x)) {
hashes = colcnt[col] + solve(col + 1, last == 1 ? width + 1 : 1, 1);
}
return dp[col][width][last] = min(hashes, dots);
}
int main() {
n, m, x, y;
cin >> n >> m >> x >> y;
char c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> c;
if (c == '.') {
colcnt[j]++;
}
}
}
memset(dp, -1, sizeof(dp));
int ans = solve(0, 0, 0);
cout << ans << endl;
return 0;
}
| 9 | CPP |
n = int(input())
c = n
result = 0
l = []
if 2 <= n <= 30:
while c > 0:
hi,ai = map(int,input().split())
c -= 1
if 0 <= hi <= 1000 and 0 <= ai <= 1000:
l.append(hi)
l.append(ai)
l_1 = []
l_2 = []
len = len(l)
while len > 0:
l_1.append(l[0])
l.pop(0)
l_2.append(l[0])
l.pop(0)
len -= 2
for i in l_1:
for j in l_2:
if i == j:
result += 1
print(result) | 7 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
long long int n, k;
scanf("%lld %lld", &n, &k);
long long int i, num[n + 10];
for (i = 0; i < n; i++) {
scanf("%1lld", &num[i]);
}
if (k > 0) {
for (i = 0; i < n - 1; i++) {
if (num[i] == 4) {
if (num[i + 1] == 7) {
if (i % 2 == 0) {
num[i] = 4;
num[i + 1] = 4;
k--;
} else {
num[i] = 7;
num[i + 1] = 7;
k--;
if (num[i - 1] == 4 && i > 0) {
if (k % 2 == 0) {
k = 0;
} else {
num[i] = 4;
k = 0;
}
}
}
}
}
if (k == 0) {
break;
}
}
}
for (i = 0; i < n; i++) printf("%lld", num[i]);
if (n == 0)
;
else
printf("\n");
}
| 10 | CPP |
x = int(input())
for i in range(x):
string = str(input())
result = ""
maxNum = -1
if(string.rfind("po")>=0):
num = string.rfind("po")
if(maxNum<num):
maxNum=num
result = "FILIPINO"
if(string.rfind("desu")>=0):
num = string.rfind("desu")
if(maxNum<num):
maxNum=num
result = "JAPANESE"
if(string.rfind("masu")>=0):
num = string.rfind("masu")
if(maxNum<num):
maxNum=num
result = "JAPANESE"
if(string.rfind("mnida")>=0):
num = string.rfind("mnida")
if(maxNum<num):
maxNum=num
result = "KOREAN"
print(result) | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
bool used[3];
string s;
bool check(){
while(1){
memset(used,0,sizeof(used));
int flag=0;
for(int i=0;i<s.size();i++)
if(i<s.size()-2&&s[i]+1==s[i+1]&&s[i+1]+1==s[i+2]){
i+=2;
flag=1;
}
else used[s[i]-'A']=true;
if(used[0]+used[1]+used[2]==2&&flag){
string t;
for(int i=0;i<3;i++){
if(used[i])continue;
for(int j=0;j<s.size();j++)
if(j<s.size()-2&&s[j]+1==s[j+1]&&s[j+1]+1==s[j+2]){
t+=i+'A';
j+=2;
}else t+=s[j];
}
s=t;
}
else if(s=="ABC")return true;
else break;
}
return false;
}
int main(){
cin>>s;
if(check())cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
} | 0 | CPP |
import itertools
N = input()
S = input()
G = itertools.groupby(S)
print(len(list(G)))
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
string s[11000];
int n, k, x, a[110000];
string str;
struct node {
int x, y;
} p[110000];
int main() {
scanf("%d%d%d", &n, &k, &x);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
p[0].x = a[0], p[0].y = a[n - 1];
int indx = 0, y = -1;
for (int i = 0; i < n; i++) str += a[i];
s[indx] = str;
while (1) {
for (int i = 0; i < n; i += 2) a[i] ^= x;
sort(a, a + n);
str.clear();
for (int i = 0; i < n; i++) str += a[i];
int flag = 1;
for (int i = 0; i < indx; i++) {
if (s[i] == str) {
y = i;
flag = 0;
break;
}
}
if (!flag)
break;
else {
s[++indx] = str;
p[indx].x = a[0], p[indx].y = a[n - 1];
}
}
if (k < y)
printf("%d %d\n", p[k].y, p[k].x);
else {
k -= y;
printf("%d %d\n", p[(k % (indx - y + 1)) + y].y,
p[(k % (indx - y + 1)) + y].x);
}
return 0;
}
| 9 | CPP |
/*************************************************************************
> File Name: c.cpp
> Author: whaleshark
> Mail: [email protected]
> Created Time: 三 7/28 15:30:37 2021
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
template<typename T>void Read(T &x){x=0;char ch=getchar();LL f=1;while(!isdigit(ch)){if(ch=='-')f*=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}x*=f;}
template<typename T,typename... Args>void Read(T &x,Args&... args){Read(x);Read(args...);}
inline int64_t QuickPow(LL base,LL n,LL Mod=0){LL ret(1);while(n){if(n&1){ret*=base;if(Mod)ret%=Mod;}base*=base;if(Mod)base%=Mod;n>>=1;}return Mod?ret%Mod:ret;}
inline int64_t __inv(LL x,LL p){return QuickPow(x,p-2,p);}
#define REP(i, a, n) for (int i = a; i <= n; ++i)
#define PER(i, n, a) for (int i = n; i >= a; --i)
const int kMax = 1e5 + 10;
const int kMod = 1e9 + 7;
LL gcd(LL a,LL b){
return b == 0?a:gcd(b,a%b);
}
LL lcm(LL a,LL b){
return a/gcd(a,b)*b;
}
void test(int num){
for(int i = 2;i <= num;i++){
if(num%i != 0){
cout<<num<<" "<<i<<'\n';
return;
}
}
cout<<num<<" "<<num+1<<'\n';
}
void solve(){
LL n;
cin>>n;
// for(int i = 1;i <= n;i++)test(i);
LL ans = 1,nxt = 1,res = 0;
for(int i = 1;;i++){
ans = lcm(ans,i);
if(ans > n)break;
nxt = lcm(ans,i+1);
res = (res + (i+1)*(n/ans-n/nxt)%kMod)%kMod;
}
cout<<res<<'\n';
}
int main(){
int T(1);
Read(T);
while(T--)solve();
return 0;
}
| 9 | CPP |
n, dist = map(int, input().split())
locs = list(map(int, input().split()))
def f(A):
c = 0
for i, k in enumerate(A):
if i == 0 or k - dist >= A[i - 1] + dist:
c += 1
if i == len(A) - 1 or k + dist < A[i + 1] - dist:
c += 1
return c
ans = f(locs)
print(ans)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const long long MXLL = 9223372036854775807;
const int Sz = 1110111;
using namespace std;
inline void Read_rap() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
}
int n;
char dir[Sz];
int d[Sz];
bool used[Sz];
void dfs(int pos) {
if (pos > n || pos < 1) {
cout << "FINITE";
exit(0);
}
if (used[pos]) {
cout << "INFINITE";
exit(0);
}
used[pos] = 1;
dfs(pos + (dir[pos] == '>' ? +1 : -1) * d[pos]);
}
int main() {
Read_rap();
cin >> n;
for (int i = 1; i <= n; i++) cin >> dir[i];
for (int i = 1; i <= n; i++) cin >> d[i];
dfs(1);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
void arquivo() {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
const int N = 5000100;
int n;
long long a, b;
int v[N];
bool memo[N][3];
long long pd[N][3];
int atPrime;
long long func(int pos, int op) {
if (pos == n) return 0LL;
if (memo[pos][op]) return pd[pos][op];
long long ret = 1000000000000000001LL;
if (!op) {
ret = func(pos + 1, 1) + a;
if (v[pos] % atPrime == 0) ret = min(ret, func(pos + 1, 0));
if ((v[pos] + 1) % atPrime == 0) ret = min(ret, func(pos + 1, 0) + b);
if ((v[pos] - 1) % atPrime == 0) ret = min(ret, func(pos + 1, 0) + b);
} else if (op == 1) {
ret = func(pos + 1, 1) + a;
if (v[pos] % atPrime == 0) ret = min(ret, func(pos + 1, 2));
if ((v[pos] + 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b);
if ((v[pos] - 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b);
} else {
if (v[pos] % atPrime == 0) ret = min(ret, func(pos + 1, 2));
if ((v[pos] + 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b);
if ((v[pos] - 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b);
}
memo[pos][op] = 1;
return pd[pos][op] = ret;
}
void fatora(long long x, set<int> &all) {
for (long long i = 2; i * i <= x; ++i) {
if (x % i == 0) {
all.insert(i);
while (x % i == 0) x /= i;
}
}
if (x > 1) all.insert(x);
}
int main() {
scanf("%d %lld %lld", &n, &a, &b);
set<int> all;
long long x;
for (int i = 0; i < n; ++i) {
scanf("%d", v + i);
}
for (int i = 0; i < 1; ++i) {
x = v[i];
fatora(x, all);
x = v[i] + 1;
fatora(x, all);
x = v[i] - 1;
fatora(x, all);
}
for (int i = n - 1; i < n; ++i) {
x = v[i];
fatora(x, all);
x = v[i] + 1;
fatora(x, all);
x = v[i] - 1;
fatora(x, all);
}
long long ans = (long long)(n - 1LL) * a;
for (set<int>::iterator it = all.begin(); it != all.end(); it++) {
atPrime = *it;
if (atPrime == 1) continue;
memset(memo, 0, sizeof memo);
for (int j = n - 1; j >= 0; --j) {
func(j, 0);
func(j, 1);
func(j, 2);
}
ans = min(ans, func(0, 0));
}
printf("%lld\n", ans);
return 0;
}
| 10 | 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 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(int x, int y) { printf("%d %d\n", x, y); }
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("%.6lf\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 int mod = 1000000007;
const long long inf = ((long long)1) << 60;
const int INF = 100000000;
const int N = 1000005;
vector<pair<int, int> > V;
long long Pow[N];
int n, m, K;
void init() {
Pow[0] = 1;
int i;
for (i = 1; i <= N - 1; i++) Pow[i] = Pow[i - 1] * 2 % mod;
}
int main() {
init();
while (scanf("%d", &n) != -1) {
RD(m, K);
V.clear();
int i, x, y, cnt, flag = 1;
for (i = 1; i <= m; i++) {
RD(x, y);
if (y - x != 1 && y - x != K + 1) flag = 0;
if (y - x == K + 1) V.push_back(make_pair(x, y));
}
if (!flag) {
puts("0");
continue;
}
sort(V.begin(), V.end());
if (!K || K >= n - 1) {
puts("1");
continue;
}
for (i = 0; i < V.size(); i++)
if (!(V[i].first >= V[0].first && V[i].first < V[0].second)) {
break;
}
if (i < V.size()) {
puts("0");
continue;
}
long long ans = 0;
if (!V.size()) ans++;
int t = V.size();
for (i = 1; i <= n; i++) {
x = i;
y = i + K + 1;
cnt = t;
if (y > n || t && x > V[0].first) break;
if (t && !(x <= V[0].first && V[t - 1].first < y)) continue;
if (t && x == V[0].first) cnt--;
y = ((y) < (n - K) ? (y) : (n - K));
ans += Pow[y - x - 1 - cnt];
ans %= mod;
}
PR(ans);
}
return 0;
}
| 10 | CPP |
import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
except:
pass
input = lambda: sys.stdin.buffer.readline().rstrip()
r,g,b=map(int,input().split())
print(max(((r+1)//2-1)*3+30,((g+1)//2-1)*3+31,((b+1)//2-1)*3+32)) | 7 | PYTHON3 |
n, a = int(input()), list(map(int, input().split()))
sum = 0
for i in range (0, n):
sum += a[i];
sum1 = 0
i = 0
while sum1 < sum:
sum1 += a[i]
sum -= a[i]
i += 1
print (i)
| 7 | PYTHON3 |
from sys import stdin,stdout
import math
# stdin = open("input.txt", "r");
# stdout = open("output.txt", "w");
n,m,k=stdin.readline().strip().split(' ')
n,m,k=int(n),int(m),int(k)
power =list(map(int,stdin.readline().strip().split(' ')))
school =list(map(int,stdin.readline().strip().split(' ')))
selected=list(map(int,stdin.readline().strip().split(' ')))
max_power={}
taken={}
for i in range(len(power)):
if school[i] in max_power:
max_power[school[i]]=max(power[i],max_power[school[i]])
else:
max_power[school[i]]=power[i]
taken[school[i]]=0
ans=0;
for i in selected:
if power[i-1]==max_power[school[i-1]]:
if taken[school[i-1]]==0:
taken[school[i-1]]=1
else:
ans+=1
else:
ans+=1
stdout.write(str(ans)+"\n"); | 7 | PYTHON3 |
n,m = map(int,input().split())
q = [list(map(int,input().split())) for i in range(m)]
for i in range(0,1000):
ans = list(str(i))
alf = 0
if len(ans) != n:
continue
for j in range(m):
if int(ans[q[j][0]-1]) == q[j][1]:
alf += 1
if alf == m:
print(i)
exit()
print(-1)
| 0 | PYTHON3 |
n = int(input())
l1 = [int(x) for x in input().split()]
l2 = [int(x) for x in l1 if x%2]
l3 = [int(x) for x in l1 if x%2==0]
ans = sum(l3)
if len(l2)>=2:
if len(l2)%2:
l2.remove(min(l2))
ans+=sum(l2)
print(ans) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<double> A[26], B[26], cum[26], cum1[26];
char p[200005];
char q[200005];
int search1(long long x, int id) {
int high = int((A[id]).size()) - 1, low = 1;
while (low <= high) {
int mid((high + low) / 2);
if (A[id][mid] > x)
high = mid - 1;
else {
low = mid + 1;
}
}
return high;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> (p + 1) >> (q + 1);
for (int i(0), _n(26); i < _n; ++i) {
A[i].push_back(0);
B[i].push_back(0);
cum[i].push_back(0);
cum1[i].push_back(0);
}
for (int i(1), _b(n + 1); i < _b; ++i) {
A[p[i] - 'A'].push_back(i);
B[p[i] - 'A'].push_back(n - i + 1);
cum[p[i] - 'A'].push_back((i));
cum1[p[i] - 'A'].push_back((n - i + 1));
}
for (int i(0), _n(26); i < _n; ++i) {
for (int j(1), _b(int((A[i]).size())); j < _b; ++j)
cum[i][j] += cum[i][j - 1];
for (int j(1), _b(int((A[i]).size())); j < _b; ++j)
cum1[i][j] += cum1[i][j - 1];
}
double ans = 0;
for (int i(1), _b(n + 1); i < _b; ++i) {
int id = q[i] - 'A';
int u = search1(i, id);
ans += cum[id][u] * (n - i + 1) +
(cum1[id][int((cum1[id]).size()) - 1] - cum1[id][u]) * (i);
}
cout.precision(10);
cout.setf(ios::fixed);
cout << 6.0 * ans / n / (1.0 + n) / (2.0 * n + 1);
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
string s, t;
cin >> s >> t;
string cs = s;
string ct = t;
sort(cs.begin(), cs.end());
sort(ct.begin(), ct.end());
if (cs != ct) {
cout << -1 << '\n';
continue;
}
int l = 0;
for (int i = 0; i < n; ++i) {
int cl = 0;
for (int j = 0; j < n; ++j) {
if (t[i + cl] == s[j]) {
cl++;
}
}
l = max(l, cl);
}
cout << (n - l) << '\n';
}
cout << endl;
return 0;
}
| 11 | CPP |
import sys
from collections import deque
from heapq import heapify, heappop, heappush
from math import sqrt
def decide_next_dst(cost, order, current_id, store_cost, store_time, wf, T):
if current_id == 0:
d = 0
c = 0
for key in order:
if cost[key][1] / wf[0][key] > c:
d = key
c = cost[key][1] / wf[0][key]
return d
d = 0
c = 0
for t in store_time:
c += (T - t) ** 2
c /= (wf[current_id][0]) ** 2
for key in order:
if cost[key][1] * (10000 / wf[current_id][key]) > c:
d = key
c = cost[key][1] * (10000 / wf[current_id][key])
return d
def solve():
inf = 10000000000
input = sys.stdin.readline
#fin = open("sampleinput.txt", "r")
#input = fin.readline
V, E = map(int, input().split())
edge = dict()
wf = dict()
next_id = dict() #iからjに行くときにiの次に訪れる地点
for i in range(V):
edge[i] = dict()
wf[i] = dict()
next_id[i] = dict()
for j in range(V):
edge[i][j] = (0 if i == j else inf)
next_id[i][j] = j
wf[i][j] = (0 if i == j else inf)
for _ in range(E):
u, v, d = map(int, input().split())
edge[u - 1][v - 1] = d
edge[v - 1][u - 1] = d
wf[u - 1][v - 1] = d
wf[v - 1][u - 1] = d
#全頂点最短経路と目的の頂点に向かうとき次に行くべき頂点の復元
for k in range(V):
for i in range(V):
for j in range(V):
if wf[i][j] > wf[i][k] + wf[k][j]:
wf[i][j] = wf[i][k] + wf[k][j]
next_id[i][j] = next_id[i][k]
T = int(input())
order = set()
stuck_order = set()
command = [None] * T
heading = 0 #次に向かう地点
dist_left = 0 #次に向かう地点までの残り距離
final_dist = 0
stuck_cost = [0 for _ in range(V)]
cost = [[0, 0] for _ in range(V)]
driver_hold = 0
store_hold = 0
store_time = set()
visited = [1] * V
for t in range(T):
N = int(input()) #注文の発生
if N == 1:
new_id, dst = map(int, input().split())
stuck_order |= {dst - 1}
stuck_cost[dst - 1] += 1
store_hold += 1
store_time |= {t}
if dist_left > 0: #移動中の場合そのまま移動を続ける
command[t] = heading + 1
dist_left -= 1
else:
if heading == 0: #店にいるときの処理
if store_hold == driver_hold == 0:
command[t] = -1
continue
else:
order |= stuck_order
for key in order:
cost[key][1] += 0.5 * cost[key][0] + stuck_cost[key]
cost[key][0] += stuck_cost[key]
driver_hold += stuck_cost[key]
stuck_cost[key] = 0
stuck_order = set()
store_hold = 0
store_time = set()
if heading in order and heading > 0: #顧客のいる場所で荷物を積み下ろすとき
order -= {heading}
driver_hold -= cost[heading][0]
cost[heading] = [0, 0]
visited[heading] += 1
current_id = heading #現在地の更新
if len(order) > 0: #まだ配達すべき荷物があるとき
if current_id == final_dist: #目的地に到着したときは残りの荷物で先に運ぶべき荷物を選ぶ
final_dist = decide_next_dst(cost, order, current_id, store_hold, store_time, wf, t)
else: final_dist = 0 #荷物が無いので店に戻る
heading = next_id[current_id][final_dist]
dist_left = edge[current_id][heading] - 1
command[t] = heading + 1
for i in range(T): print(command[i])
#out = open(str(V) + "-" + str(E) + "-out.txt", "w")
#for i in range(T):
#print(command[i], file = out, end ="\n")
#out.close
return 0
if __name__ == "__main__":
solve()
| 0 | PYTHON3 |
from sys import stdin
input = stdin.readline
n = int(input())
arr, cnt, curr_len, curr_sum = [0] * (n+1), [0] * (n+1), 1, 0
for _ in range(n):
s = input()
if s[0] == '1':
_, x, y = map(int, s.split())
curr_sum += x*y
cnt[x-1] += y
elif s[0] == '2':
_, x = map(int, s.split())
curr_sum += x
arr[curr_len] = x
curr_len += 1
else:
curr_len -= 1
curr_sum -= arr[curr_len] + cnt[curr_len]
cnt[curr_len-1] += cnt[curr_len]
cnt[curr_len] = 0
print('%.9f' % (curr_sum / curr_len))
| 7 | PYTHON3 |
from sys import stdin
n = int(stdin.readline().rstrip())
l = []
l2 = []
for i in range(n):
a, b = map(int, stdin.readline().rstrip().split(" "))
l.append([a,b])
l2.append([b,a])
l.sort()
l2.sort()
c = 0
x = 0
for i in range(1,n):
if l[i][0]==l[i-1][0]:
x+=1
else:
c+= (x*(x+1)//2)
x = 0
if x:
c+= (x*(x+1)//2)
x = 0
for i in range(1,n):
if l2[i][0] == l2[i - 1][0]:
x += 1
else:
c+= (x*(x+1)//2)
x = 0
if x:
c+= (x*(x+1)//2)
x = 0
rem = 0
for i in range(1,n):
if l[i]==l[i-1]:
x+=1
else:
rem += x*(x+1)//2
x = 0
if x:
rem +=(x*(x+1)//2)
print(c-rem) | 7 | PYTHON3 |
a=int(input())
b=list(map(int,input().split()))
b.sort()
if a%2==0:
kek=a//2
else:
kek=(a+1)//2
print(b[kek-1]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, p;
long long a[1005][1005];
long long res = -0x3f3f3f3f3f3f3f3f;
priority_queue<long long> rq;
priority_queue<long long> cq;
long long sum_r[1005];
long long sum_c[1005];
long long r[1005 * 1005];
long long c[1005 * 1005];
void Init() {
scanf("%I64d%I64d%I64d%I64d", &n, &m, &k, &p);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%I64d", &a[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
sum_r[i] += a[i][j];
}
rq.push(sum_r[i]);
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++) {
sum_c[j] += a[i][j];
}
cq.push(sum_c[j]);
}
}
void Solve() {
for (int i = 1; i <= k; i++) {
long long cc = cq.top();
cq.pop();
c[i] = c[i - 1] + cc;
cc -= n * p;
cq.push(cc);
}
for (int i = 1; i <= k; i++) {
long long rr = rq.top();
rq.pop();
r[i] = r[i - 1] + rr;
rr -= m * p;
rq.push(rr);
}
for (int i = 0; i <= k; i++) {
res = ((res) > (r[i] + c[k - i] - p * i * (k - i))
? (res)
: (r[i] + c[k - i] - p * i * (k - i)));
}
}
void Print() { printf("%I64d\n", res); }
int main() {
Init();
Solve();
Print();
return 0;
}
| 8 | CPP |
from sys import stdin,stdout
from heapq import heapify,heappush,heappop,heappushpop
from collections import defaultdict as dd, deque as dq,Counter as C
from bisect import bisect_left as bl ,bisect_right as br
from itertools import combinations as cmb,permutations as pmb
from math import factorial as f ,ceil,gcd,sqrt,log,inf
mi = lambda : map(int,input().split())
ii = lambda: int(input())
li = lambda : list(map(int,input().split()))
mati = lambda r : [ li() for _ in range(r)]
lcm = lambda a,b : (a*b)//gcd(a,b)
MOD=10**9+7
def soe(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return(prime)
def ncr(n,r):
p,k=1,1
if (n - r < r):
r = n - r
if r!=0:
while(r):
p,k=p*n,k*r
m=gcd(p,k)
p,k=p//m,k//m
n,r=n-1,r-1
else:
p=1
return(p)
def solve():
arr=[int(x) for x in input()]
d={}
for x in arr:
if x==4 or x==7:
try:
d[x]+=1
except:
d[x]=1
if len(d)==0:
print(-1)
elif len(d)==1:
print(*list(d.keys()))
else:
if d[4]>=d[7]:
print(4)
else:
print(7)
for _ in range(1):
solve() | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> m[101];
int a[101];
int main() {
int n, sum = 0, target;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum += a[i];
m[a[i]].push_back(i);
}
target = (2 * sum) / n;
int pos, pos2;
for (int i = 1; i <= n; i++) {
int ans = 0;
for (int j = 0; j < m[a[i]].size(); j++) {
if (m[a[i]][j] == i) {
ans = 1;
break;
}
}
if (ans == 0) continue;
if (m[target - a[i]].size() > 0) {
if (target == 2 * a[i]) {
if (m[a[i]][m[a[i]].size() - 1] == i) m[a[i]].pop_back();
if (m[a[i]].size() == 0) continue;
printf("%d %d\n", i, m[a[i]][m[a[i]].size() - 1]);
m[a[i]].pop_back();
if (m[a[i]].size() == 0) continue;
for (int j = 0; j < m[a[i]].size(); j++) {
if (m[a[i]][j] == i) {
pos2 = j;
break;
}
}
m[a[i]].erase(m[a[i]].begin() + pos2);
continue;
}
printf("%d %d\n", i, m[target - a[i]][m[target - a[i]].size() - 1]);
m[target - a[i]].pop_back();
for (int j = 0; j < m[a[i]].size(); j++) {
if (m[a[i]][j] == i) {
pos2 = j;
break;
}
}
m[a[i]].erase(m[a[i]].begin() + pos2);
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
long long N, M;
const long long MAXN = 5000;
const long long INF = 2147483600;
const long long Mod = 998244353LL;
long long a[2010][2010];
long long g[2010][2010];
namespace BIT {
long long c[3][MAXN + 1];
inline void Add(long long n, long long k, long long x) {
for (; k <= N; k += (k & (-k))) c[n][k] += x;
}
inline long long Query(long long n, long long k) {
long long ans = 0;
for (; k; k -= (k & (-k))) ans += c[n][k];
return ans;
}
} // namespace BIT
using namespace BIT;
long long f[2010], fac[2010];
bool vis[2010];
long long vall[2010];
int main() {
N = read();
for (long long i = 1; i <= N; i++) {
for (long long j = 1; j <= N; j++) a[i][j] = read();
}
g[0][0] = 1;
g[1][0] = 0;
for (long long i = 2; i <= N; i++) {
g[i][0] = 1LL * (i - 1) * (g[i - 1][0] + g[i - 2][0]) % Mod;
}
for (long long i = 1; i <= N; i++) {
for (long long j = 1; j <= i; j++)
g[i][j] = (g[i - 1][j - 1] + g[i][j - 1]) % Mod;
}
long long Fn = g[N][0];
f[0] = 1;
long long rak = 0;
for (long long i = 1; i <= N; i++) f[i] = f[i - 1] * Fn % Mod;
fac[0] = 1;
long long ans = 0;
for (long long i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % Mod;
for (long long i = 1; i <= N; i++)
rak = (rak + (a[1][i] - 1 - Query(0, a[1][i])) * fac[N - i] % Mod) % Mod,
Add(0, a[1][i], 1);
(ans += rak * f[N - 1] % Mod) %= Mod;
for (long long i = 2; i <= N; i++) {
for (long long j = 1; j <= N; j++)
c[0][j] = c[1][j] = c[2][j] = 0, vall[j] = vis[j] = 0;
long long cnt = 0;
for (long long j = 1; j <= N; j++) {
if (vall[a[i - 1][j]]) ++cnt;
long long d1 = a[i][j] - 1 - Query(0, a[i][j] - 1) +
Query(1, a[i][j] - 1) -
(a[i - 1][j] < a[i][j] && !vall[a[i - 1][j]]);
long long a1 = a[i][j] - 1 - Query(2, a[i][j] - 1) -
(a[i - 1][j] < a[i][j] && !vis[a[i - 1][j]]) - d1;
(ans += g[N - j][j - cnt] * d1 % Mod * f[N - i] % Mod) %= Mod;
if (2 * j != cnt)
(ans += g[N - j][j - cnt - 1] * a1 % Mod * f[N - i] % Mod) %= Mod;
if (vall[a[i][j]]) Add(1, a[i][j], 1);
if (vall[a[i - 1][j]]) Add(1, a[i - 1][j], 1);
vall[a[i][j]]++;
vall[a[i - 1][j]]++;
Add(0, a[i][j], 1);
Add(0, a[i - 1][j], 1);
Add(2, a[i][j], 1);
vis[a[i][j]] = true;
if (vall[a[i][j]] == 2) ++cnt;
}
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
## necessary imports
import sys
input = sys.stdin.readline
from math import ceil, floor, factorial;
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp
## gcd function
def gcd(a,b):
if a == 0:
return b
return gcd(b%a, a)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0):
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x:
lo = mid+1
else:
hi = mid
return lo
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e6 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve()
def factoriazation(x):
ret = {};
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1;
x = x//spf[x]
return ret
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
## taking integer array input
def int_array():
return list(map(int, input().strip().split()))
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
n = int(input()); a = sorted(int_array());
x = a[-1]; y = a.copy(); ns = set();
for i in a:
if x % i == 0 and i not in ns:
y.remove(i); ns.add(i);
y = max(y);
print(x, y); | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
char s[N], t[N];
int n, m;
int ch[N][26], lps[N];
void calcLps() {
memset(lps, 0, sizeof(lps));
int i = 1;
int len = 0;
lps[0] = 0;
while (i < m) {
if (t[i + 1] == t[len + 1]) {
len++;
lps[i] = len;
i++;
} else {
if (len == 0) {
lps[i] = 0;
i++;
} else {
len = lps[len - 1];
}
}
}
}
int main() {
scanf("%s", s + 1);
scanf("%s", t + 1);
n = strlen(s + 1), m = strlen(t + 1);
for (int i = 1; i <= m; i++) ch[i - 1][t[i] - 'a'] = i;
calcLps();
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 26; j++) {
if (!ch[i][j]) {
ch[i][j] = ch[lps[i - 1]][j];
}
}
}
vector<vector<int> > dp(n + 1, vector<int>(m + 1, -1));
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
if (dp[i][j] == -1) continue;
for (int c = s[i + 1] == '?' ? 0 : (s[i + 1] - 'a');
c < (s[i + 1] == '?' ? 26 : (s[i + 1] - 'a' + 1)); c++) {
dp[i + 1][ch[j][c]] =
max(dp[i + 1][ch[j][c]], dp[i][j] + (ch[j][c] == m));
}
}
}
int ans = 0;
for (int i = 0; i <= m; i++) {
ans = max(ans, dp[n][i]);
}
printf("%d\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, s = 0, c = 0, i, k;
cin >> n;
for (i = 0; i < n; i++) {
cin >> k;
if (k == 0) c++, k++;
s = s + k;
}
if (s == 0) c++;
cout << c << endl;
}
}
| 7 | CPP |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll TLE = 1000000000;
int main(){
ll n,t;
cin >> n >> t;
string s;
cin >> s;
map<int,int> deg;
for(int i=2;i<(int)s.size();i+=4){
deg[s[i]-'0']++;
}
ll ans = 0;
for(auto p : deg){
ll tmp = 1;
for(int i=0;i<p.first;i++){
tmp *= n;
if(tmp>TLE){
ans = TLE+1;
break;
}
}
if(p.second*tmp>TLE){
ans = TLE+1;
break;
}
ans += p.second*tmp;
if(ans>TLE)break;
}
if(ans>TLE || ans*t>TLE)cout << "TLE" << endl;
else cout << ans*t << endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, sum = 0;
cin >> n;
int A[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
if (i == n / 2 || j == n / 2 || i == j || i + j == n - 1) {
sum += A[i][j];
}
}
}
cout << sum;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
const int MAX_N = (int)2e5 + 5;
vector<int> g[MAX_N << 1];
bool used[MAX_N << 1];
int scc[MAX_N << 1], n;
map<ii, int> res;
void dfs(vector<int> g[], int v, const int& id) {
static int tme = 0;
used[v] = true;
scc[v] = id;
for (int& x : g[v])
if (!used[x]) dfs(g, x, id);
}
int main() {
int k, l, p, q;
scanf("%d%d%d", &n, &k, &l);
for (int i = 0; i < k; i++) {
scanf("%d%d", &p, &q);
g[p].push_back(q);
g[q].push_back(p);
}
for (int i = 0; i < l; i++) {
scanf("%d%d", &p, &q);
g[p + n].push_back(q + n);
g[q + n].push_back(p + n);
}
for (int i = 1, id = 0; i <= (n << 1); i++)
if (!used[i]) dfs(g, i, ++id);
for (int i = 1; i <= n; i++)
res[{scc[i], scc[i + n]}]++;
for (int i = 1; i <= n; i++) printf("%d ", res[{scc[i], scc[i + n]}]);
} | 0 | CPP |
s = list(input())
print (s.count('2')) | 0 | PYTHON3 |
n, m, Q = map(int, input().split())
d = [[0 for i in range(n + 1)] for i in range(n + 1)]
for i in range(m):
l, r = map(int, input().split())
d[l][r] += 1
for i in range(n + 1):
for j in range(n):
d[i][j + 1] += d[i][j]
for i in range(n + 1):
for j in range(n):
d[n - 1 - j][i] += d[n - j][i]
for i in range(Q):
p, q = map(int, input().split())
print(d[p][q]) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt;
cin >> tt;
while (tt--) {
long long a, b, c, d, e, f, i, j, k, m, n, o, x, y;
cin >> n;
long long ar[n + 1], ab[n + 1];
for (i = 1; i <= n; i++) cin >> ar[i];
vector<int> v;
for (i = 1; i <= n; i++) {
cin >> a;
ab[i] = a;
if (a == 0) v.push_back(ar[i]);
}
sort(v.rbegin(), v.rend());
long long start = 0;
for (i = 1; i <= n; i++) {
if (ab[i] == 0) ar[i] = v[start++];
}
long long sum = 0, ans = 0;
for (i = 1; i <= n; i++) {
sum += ar[i];
if (ar[i] < 0) ans = i;
}
for (i = 1; i <= n; i++) cout << ar[i] << " ";
cout << "\n";
}
}
| 8 | CPP |
i = int(input())
for j in range(i):
n, x, a, b = list(map(int, input().split()))
print(min(n - 1, abs(a - b) + x))
| 7 | PYTHON3 |
K,A,B = map(int,input().split())
if A + 2 < B:
K2 = K - A + 1
print( K2 // 2 * ( B - A ) + K2 % 2 + A)
else:
print( 1 + K )
| 0 | PYTHON3 |
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
mod = 10 ** 9 + 7
Y = 0
for i in range(1, m + 1):
Y += (i - 1) * y[i - 1] - (m - i) * y[i - 1]
Y %= mod
X = 0
for i in range(1, n + 1):
X += (i - 1) * x[i - 1] - (n - i) * x[i - 1]
X %= mod
print(X * Y % mod)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int f[2006];
int find(int x) {
if (x == f[x])
return x;
else {
f[x] = find(f[x]);
return f[x];
}
}
struct E {
int x, y;
long long val;
} ed2[2006], ed[3000006];
bool cmp(E aa, E bb) { return aa.val < bb.val; }
long long c[2006], k[2006];
vector<int> p1;
vector<pair<int, int> > p2;
int main() {
int n, cnt = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> ed2[i].x >> ed2[i].y;
}
f[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> c[i];
f[i] = i;
}
for (int i = 1; i <= n; i++) {
cin >> k[i];
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
ed[cnt].x = i;
ed[cnt].y = j;
ed[cnt].val =
(k[i] + k[j]) * (abs(ed2[i].x - ed2[j].x) + abs(ed2[i].y - ed2[j].y));
cnt++;
}
}
for (int i = 1; i <= n; i++) {
ed[cnt].x = 0;
ed[cnt].y = i;
ed[cnt].val = c[i];
cnt++;
}
sort(ed, ed + cnt, cmp);
long long ans = 0;
for (int i = 0; i < cnt; i++) {
int x = find(ed[i].x);
int y = find(ed[i].y);
if (x != y) {
f[x] = y;
ans += ed[i].val;
if (ed[i].x == 0 || ed[i].y == 0) {
p1.push_back(ed[i].x + ed[i].y);
} else
p2.push_back(make_pair(ed[i].x, ed[i].y));
}
}
cout << ans << endl;
int sz = p1.size();
cout << sz << endl;
for (int i = 0; i < sz; i++) {
printf("%d%c", p1[i], " \n"[i == sz - 1]);
}
sz = p2.size();
cout << sz << endl;
for (int i = 0; i < sz; i++) {
printf("%d %d\n", p2[i].first, p2[i].second);
}
return 0;
}
| 10 | CPP |
q = int(input())
A = []
ans = []
for i in range(q):
q = str(input())
if q[0] == "0": #pushBack
A.append(int(q[2:]))
elif q[0] == "1": #randamAccess
# ans.append(A[int(q[2:])])
print(A[int(q[2:])])
else: #popBack
A.pop()
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, tp, g[100005][2], cnt[100005];
char s[100005];
struct node {
int a, b;
node operator+(const node &x) const { return (node){a + x.a, b + x.b}; }
bool operator>(const node &x) const {
return a > x.a || (a == x.a && b < x.b);
}
} f[100005];
node Max(node a, node b) { return a > b ? a : b; }
int main() {
scanf("%d %s%d", &n, s + 1, &m);
tp = (!(m & 1));
g[0][0] = n + 1;
g[0][1] = n + 1;
for (int i = 1; i <= n; i++) {
cnt[i] = cnt[i - 1] + (s[i] == '?');
g[i][0] = n + 1;
g[i][1] = n + 1;
if (s[i] != 'b') g[i][0] = min(g[i - 1][1], i);
if (s[i] != 'a') g[i][1] = g[i - 1][0];
}
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1];
if (g[i][tp] <= i - m + 1)
f[i] = Max(f[i], f[i - m] + (node){1, cnt[i] - cnt[i - m]});
}
printf("%d\n", f[n].b);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long a[200009];
long long b[200009];
map<long long, long long> mp;
map<long long, long long> mp1;
vector<long long> vc;
int main() {
cin.sync_with_stdio(false);
long long i, j, k, l, c, v, n, m, r, t;
long long x1, x2, y1, y2, x3, x4, x5, x6;
string s, s1, s2, s3, ss[10009];
while (cin >> n) {
cin >> s;
c = 0;
for (i = 0; i < n; i++) {
}
j = 1;
for (i = 0; i < n; i++) {
if ((s[i] - 48) % 2 == 0) {
c += j;
}
j++;
}
cout << c;
}
}
| 7 | CPP |
# https://codeforces.com/contest/1353/problem/E
import sys
import os
import heapq
try:
path = "./file/input.txt"
if os.path.exists(path):
sys.stdin = open(path, 'r')
# sys.stdout = open(r"./file/output.txt", 'w')
except:
pass
t = int(input())
def printd(value):
# print(value)
pass
for _ in range(t):
n = int(input())
arr = list(map(int, input().split(" ")))
arr.sort()
result = 20000
for i in range(1, n):
value = arr[i] - arr[i - 1]
if value < result:
result = value
print(result)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
long long mod_pow(long long x, long long n){
if(n == 0) return 1;
long long res = mod_pow(x*x % mod, n / 2);
if(n & 1) res = res*x % mod;
return res;
}
const int MAX_NUM = 200010;
long long fact[MAX_NUM], inv[MAX_NUM];
long long comb(long long n, long long k){
if(n < k || n < 0 || k < 0) return 0;
if(fact[0] == 0){
fact[0] = 1; inv[0] = 1;
for(int i = 1; i < MAX_NUM; i++){
fact[i] = fact[i - 1] * i % mod;
inv[i] = mod_pow(fact[i], mod - 2);
}
}
return (((fact[n] * inv[k]) % mod) * inv[n - k]) % mod;
}
long long hcomb(long long n, long long k){
if(n == 0 && k == 0) return 1;
return comb(n + k - 1, k);
}
signed main(){
int K, N;
cin >> K >> N;
for(int i = 2; i <= 2 * K; i++){
int ans = 0;
int n = min(i, 2 * K + 2 - i) / 2;
for(int j = 1; j <= n; j++){
ans = (ans + (j % 2 ? 1 : -1) * hcomb(K, N - 2 * j) * comb(n, j) % mod + mod) % mod;
}
ans = (hcomb(K, N) - ans + mod) % mod;
cout << ans << endl;
}
} | 0 | CPP |
N,X,Y=map(int, input().split())
ans=[0]*(N-1)
for i in range(1,N):
for j in range(i+1, N+1):
ans[min(j-i, abs(j-Y)+abs(i-X)+1)-1]+=1
for a in ans:
print(a) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
set<int> banx, bany;
int main() {
int n, m, x, y;
cin >> n >> m;
for (int i(0); i != m; ++i) {
cin >> x >> y;
if (x != 1 && x != n) banx.insert(x);
if (y != 1 && y != n) bany.insert(y);
}
if (n & 1) {
int tmp(0);
if (banx.count(n / 2 + 1) || bany.count(n / 2 + 1)) ++tmp;
cout << (n - 1) * 2 - 3 + tmp - banx.size() - bany.size() << '\n';
} else
cout << n * 2 - 4 - banx.size() - bany.size() << '\n';
return 0;
}
| 8 | CPP |
#include<iostream>
#include<algorithm>
#define r(i,n) for(int i=0;i<n;i++)
#define z 100
using namespace std;
int n,a,b,q[z],l[z],la,be,an,ct[z],c[z][z];bool v[z],s[z];void d(int x,int len){if(len>be){la=x;be=len;}v[x]=1,s[x]=1;for(int i=0;i<ct[x];i++)if(!v[c[x][i]]){d(c[x][i],len+1);v[c[x][i]]=0;}}int main(){while(1){cin>>n;if(!n)break;r(i,z)ct[i]=0;r(i,n){cin>>a>>b;a--,b--;c[a][ct[a]]=b,c[b][ct[b]]=a;ct[a]++,ct[b]++;}r(i,z)s[i]=0;an=0;for(int i=0;i<z;i++)if(!s[i]){r(i,z)v[i]=0;be=0;d(i,1);r(i,z)v[i]=0;be=0;d(la,1);an=max(an,be);}cout<<an<<endl;}return 0;} | 0 | CPP |
#!/usr/bin/env python3
from typing import List
def get_input():
try:
text = input()
text = text.strip()
if len(text) == 0:
return None
else:
return text
except EOFError:
return None
def min_steps(xs: List[int], ys: List[int]):
x_min = min(xs)
y_min = min(ys)
xs_d = [x - x_min for x in xs]
ys_d = [y - y_min for y in ys]
return sum(max(x_d, y_d) for x_d, y_d in zip(xs_d, ys_d))
total_cases = int(get_input())
for _ in range(total_cases):
get_input()
xs = list(map(int, get_input().split(" ")))
ys = list(map(int, get_input().split(" ")))
print(min_steps(xs, ys))
| 8 | PYTHON3 |
from sys import stdin, stdout
from collections import defaultdict, Counter
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
def main():
T = int(rl())
numchar = lambda c: ord(c) - 97
for _ in range(T):
n, m = (int(x) for x in rll())
s = rll()[0]
P = [int(x) for x in rll()]
freqs = [0] * 26
for c in s:
freqs[numchar(c)] += 1
prefix = [0 for _ in range(len(s)+1)]
for x in P:
prefix[x] -= 1
curr_sum = 0
for i, char in enumerate(s):
curr_sum -= prefix[i]
freqs[numchar(char)] += m - curr_sum
ans = " ".join(str(x) for x in freqs)
stdout.write(str(ans));stdout.write("\n")
stdout.close()
if __name__ == "__main__":
main() | 9 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int N;
string S;
void dfs(int a,int b)
{
if(a==N)
{
puts(S.c_str());
return ;
}
for(int i=0;i<=b+1;i++)
{
S[a]='a'+i;
dfs(a+1,max(b,i));
}
return ;
}
int main()
{
cin>>N;
dfs(0,-1);
return 0;
} | 0 | CPP |
s=input()
k=0
s1=''
s2=['','','']
for a in s:
if a=='a' or a=='e' or a=='i' or a=='o' or a=='u':
s1+=a
k=0
else:
if k+1==3:
s2[2]=a
if (s2[0]!=s2[1] or s2[0]!=s2[2]):
s1=s1+' '+a
k=1
s2[0]=a
else:
k=2
s1+=a
else:
s2[k] = a
k+=1
s1+=a
print(s1) | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define M 1000005
#define ll long long
ll n, p, ans = 1;
bool dd[M];
vector<ll> v;
int main(){
cin >> n >> p;
if (n == 1){
cout << p;
return 0;
}
for (int i = 2; i < M; i++){
if (!dd[i]){
v.push_back(i);
for (int j = 2*i; j < M; j += i) dd[j] = true;
}
}
for (int i = 0; i < v.size(); i++){
ll cnt = 0;
while (p > 0 && p%v[i] == 0) cnt++, p /= v[i];
if (cnt > 0) ans *= pow(v[i], cnt/n);
}
cout << ans;
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:32777216")
using namespace std;
const long double pi = acos(-1.0);
const int INF = 1000000000;
const int MOD = 1000000007;
const double EPS = 1e-7;
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
int main() {
int n;
cin >> n;
long long r = 0;
vector<int> a(n);
for (int(i) = (0); (i) < (n); ++(i)) {
cin >> a[i];
r += abs(a[i]);
}
sort((a).begin(), (a).end());
long long s = 0;
for (int(i) = (1); (i) < (a.size()); ++(i)) {
s += 1LL * i * (a[i] - a[i - 1]);
r += 2 * s;
}
long long p = n;
long long g = gcd(r, p);
cout << r / g << ' ' << p / g << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(kbrni,n)cout<<" "<<a[kbrni];cout<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define svecp(v) cout<<#v<<":";each(kbrni,v)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
using namespace std;
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
struct RollingHash {
static const ll mo0=1000000007,mo1=1000000009;
static ll mul0,mul1,mul2,mul3; static vector<ll> pmo[4];
static const ll add0=1000010007,add1=1003333331;
vector<string> s; int rw,cl; vector<vector<ll> > hash_[2];
void init(vector<string> s) {
this->s=s; rw=(int)s.size(); cl=(int)s[0].size();
hash_[0].resize(rw+1,vector<ll>(cl+1,0)),hash_[1].resize(rw+1,vector<ll>(cl+1,0));
if(!mul0) mul0=10009+(((ll)&mul0)>>5)%259,mul1=10007+(((ll)&mul1)>>5)%257;
if(!mul2) mul2=10039+(((ll)&mul2)>>5)%289,mul3=10037+(((ll)&mul3)>>5)%287;
if(pmo[0].empty()) pmo[0].pb(1),pmo[1].pb(1),pmo[2].pb(1),pmo[3].pb(1);
rep(i,rw)rep(j,cl) hash_[0][i+1][j+1]=(hash_[0][i+1][j]*mul0+add0+s[i][j])%mo0; //hash_[0][i][j]はインデックス0~i-1,0~j-1までの文字列のハッシュ値
rep(i,rw)rep(j,cl) hash_[1][i+1][j+1]=(hash_[1][i+1][j]*mul1+add1+s[i][j])%mo1;
rep(j,cl)rep(i,rw) hash_[0][i+1][j+1]=(hash_[0][i][j+1]*mul2+hash_[0][i+1][j+1])%mo0;
rep(j,cl)rep(i,rw) hash_[1][i+1][j+1]=(hash_[1][i][j+1]*mul3+hash_[1][i+1][j+1])%mo1;
}
ll comp(int u,int l,int d,int r,int id,const ll mod){
return (hash_[id][d+1][r+1]+(mod-hash_[id][d+1][l]*pmo[id][r+1-l]%mod)
+(mod-hash_[id][u][r+1]*pmo[id+2][d+1-u]%mod)+(hash_[id][u][l]*pmo[id][r+1-l]%mod*pmo[id+2][d+1-u]%mod))%mod;
}
pair<ll,ll> hash(int u,int l,int d,int r) { //文字列sのインデックスu~d,l~rまでの部分文字列のハッシュ値
if(l>r||u>d) return make_pair(0,0);
while((int)pmo[0].size()<r+2) pmo[0].pb(pmo[0].back()*mul0%mo0), pmo[1].pb(pmo[1].back()*mul1%mo1);
while((int)pmo[2].size()<d+2) pmo[2].pb(pmo[2].back()*mul2%mo0), pmo[3].pb(pmo[3].back()*mul3%mo1);
return make_pair(comp(u,l,d,r,0,mo0),comp(u,l,d,r,1,mo1));
}
pair<ll,ll> hash(vector<string> s) { init(s); return hash(0,0,(int)s.size()-1,(int)s[0].size()-1); } //文字列s全体のハッシュ値
static pair<ll,ll> rconcat(pair<ll,ll> L,pair<ll,ll> R,int len) { //Lの右にRを結合する(lenはRの横方向の長さ)
while((int)pmo[0].size()<len+2) pmo[0].pb(pmo[0].back()*mul0%mo0), pmo[1].pb(pmo[1].back()*mul1%mo1);
return make_pair((R.first + L.first*pmo[0][len])%mo0,(R.second + L.second*pmo[1][len])%mo1);
}
static pair<ll,ll> cconcat(pair<ll,ll> L,pair<ll,ll> R,int len) { //Lの下にRを結合する(lenはRの縦方向の長さ)
while((int)pmo[2].size()<len+2) pmo[2].pb(pmo[2].back()*mul2%mo0), pmo[3].pb(pmo[3].back()*mul3%mo1);
return make_pair((R.first + L.first*pmo[2][len])%mo0,(R.second + L.second*pmo[3][len])%mo1);
}
};
vector<ll> RollingHash::pmo[4]; ll RollingHash::mul0,RollingHash::mul1,RollingHash::mul2,RollingHash::mul3;
void contain(RollingHash& arg1,vector<string>& arg2,vector<P>& ans)
{
int r = (int)arg2.size(),c = (int)arg2[0].size();
if(arg1.rw < r || arg1.cl < c){
return;
}
RollingHash rh;
pair<ll,ll> hs = rh.hash(arg2);
rep(i,arg1.rw-r+1){
rep(j,arg1.cl-c+1){
if(arg1.hash(i,j,i+r-1,j+c-1) == hs){
ans.pb(P(i,j));
}
}
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n,m;
cin >> n >> m;
vs s(n);
rep(i,n){
cin >> s[i];
}
RollingHash rh;
rh.init(s);
cin >> n >> m;
vs t(n);
rep(i,n){
cin >> t[i];
}
vp ans;
contain(rh,t,ans);
rep(i,len(ans)){
cout << ans[i].fi << " " << ans[i].se << "\n";
}
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int IT_MAX = 1 << 19;
int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double ERR = 1e-10;
typedef struct p1 {
int x, y, z;
} p;
bool cmp(long long a, long long b) { return a > b; }
long long gcd(long long m, long long n) {
while (m > 0) {
long long c = n % m;
n = m;
m = c;
}
return n;
}
double qpow(double a, long long b) {
double res = 1;
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
long long C(long long n, long long x) {
long long ans = 1;
for (long long i = n, j = 1; i >= n - x + 1; i--, j++) {
ans *= i;
ans /= j;
}
return ans;
}
int main() {
int n;
scanf("%d", &n);
;
if (n == 1) {
cout << 1 << " " << 0;
return 0;
}
vector<int> f;
set<int> c;
int ans = 1;
for (int i = 2; i <= n; i++) {
int cnt = 0;
while (n % i == 0) {
n /= i;
f.push_back(i);
cnt++;
if (cnt == 1) ans *= i;
}
if (cnt != 0) c.insert(cnt);
}
int cnt = 0;
int mmax = -1;
if (c.size() == 1) {
set<int>::iterator it = c.begin();
int a = *it;
if ((a & (a - 1)) != 0) cnt++;
} else {
cnt++;
}
for (set<int>::iterator it = c.begin(); it != c.end(); it++) {
int tmp = *it;
mmax = max(tmp, mmax);
}
if ((mmax & (mmax - 1)) == 0) cnt--;
while (mmax) {
mmax /= 2;
cnt++;
}
cout << ans << " " << cnt;
return 0;
}
| 8 | CPP |
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL n,a,b,c,d;
int main(){
scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d);
bool can=0;
for(int i=0;i<=n;i++)
if(c*(n-1-i)-d*i<=a-b&&a-b<=d*(n-1-i)-c*i){can=1;break;}
printf("%s",can?"YES":"NO");
return 0;
} | 0 | CPP |
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque
H, W, K = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
X = [1] * (W + 2)
for _ in range(H):
X += [1] + [1 if a == "@" else 0 for a in input()] + [1]
X += [1] * (W + 2)
H, W = H+2, W+2
s, t = x1 * W + y1, x2 * W + y2
def BFS(i0=0):
Q = deque([(i0, 3)])
D = [-1] * (H * W)
D[i0] = 0
while Q:
x, t = Q.popleft()
if t & 1:
for d in (1, -1):
for y in range(x + d, x + d * K, d):
if X[y]: break
if 0 <= D[y] <= D[x]: break
D[y] = D[x] + 1
Q.append((y, 2))
else:
y = x + d * K
if X[y] == 0:
if D[y] == -1:
D[y] = D[x] + 1
Q.append((y, 3))
if t & 2:
for d in (W, -W):
for y in range(x + d, x + d * K, d):
if X[y]: break
if 0 <= D[y] <= D[x]: break
D[y] = D[x] + 1
Q.append((y, 1))
else:
y = x + d * K
if X[y] == 0:
if D[y] == -1:
D[y] = D[x] + 1
Q.append((y, 3))
return D
print(BFS(s)[t]) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int nodes, q, arr[200005], total[1000005] = {}, blok, batas1, batas2, kiri,
kanan;
unsigned long long jawaban = 0, ans[200005];
struct apapun1 {
int kiri, kanan, indeks;
friend bool operator<(apapun1 a, apapun1 b) {
if (a.kiri / blok == b.kiri / blok) return (a.kanan < b.kanan);
return (a.kiri / blok < b.kiri / blok);
}
} coba1, temp1;
vector<apapun1> query;
void pairing1(int kiri, int kanan, int indeks) {
coba1.kiri = kiri;
coba1.kanan = kanan;
coba1.indeks = indeks;
}
void add(int indeks) {
jawaban -= (long long)total[arr[indeks]] * total[arr[indeks]] * arr[indeks];
total[arr[indeks]]++;
jawaban += (long long)total[arr[indeks]] * total[arr[indeks]] * arr[indeks];
}
void remove(int indeks) {
jawaban -= (long long)total[arr[indeks]] * total[arr[indeks]] * arr[indeks];
total[arr[indeks]]--;
jawaban += (long long)total[arr[indeks]] * total[arr[indeks]] * arr[indeks];
}
int main() {
scanf("%d%d", &nodes, &q);
blok = ceil(sqrt(nodes));
for (int i = 1; i <= nodes; i++) {
scanf("%d", &arr[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d", &batas1, &batas2);
pairing1(batas1, batas2, i);
query.push_back(coba1);
}
sort(query.begin(), query.end());
kiri = 1;
kanan = 1;
add(1);
for (int i = 0; i < q; i++) {
temp1 = query[i];
while (kiri < temp1.kiri) {
remove(kiri);
kiri++;
}
while (kiri > temp1.kiri) {
kiri--;
add(kiri);
}
while (kanan < temp1.kanan) {
kanan++;
add(kanan);
}
while (kanan > temp1.kanan) {
remove(kanan);
kanan--;
}
ans[temp1.indeks] = jawaban;
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
| 10 | CPP |
N = int(input())
S = input()
cnt = 0
left = 0
for i in range(N):
if S[i] == '(':
cnt += 1
else:
if cnt == 0:
left += 1
else:
cnt -= 1
ans = ("("*left) + S + ")"*cnt
print(ans)
| 0 | PYTHON3 |
n, m = map(int, input().split(" "))
cds = list(map(int, input().split(" ")))
istaxi = list(map(int, input().split(" ")))
l = n + m
r = [0]*m
s = 0
while not istaxi[s]: s += 1
r[0] = s
e = l - 1
while not istaxi[e]: e -= 1
r[-1] += l - e - 1
#print(s, e, r, cds[s:e+1])
lt = s
l = []
p = 0
for i in range(s+1, e+1):
if istaxi[i] == 0:
l += [2*cds[i]-cds[lt]]
if istaxi[i] == 1:
#print(lt, i, l)
for j in l:
if j <= cds[i]: r[p] += 1
else: r[p+1] += 1
lt = i
p += 1
l = []
print(" ".join(map(str, r))) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double EPS = 1e-9;
const int inf = 1e9;
const double PI = 3.1415926535897932384626433832795;
const int N = 1 * 1e5 + 5;
int mas[3 * N];
long long sum[3 * N];
int main() {
ios_base::sync_with_stdio(0);
int n, s, f;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> mas[i];
}
cin >> s >> f;
int len = f - s;
for (int i = n; i < 3 * n; i++) {
mas[i] = mas[i % n];
}
sum[0] = mas[0];
for (int i = 1; i < 3 * n; i++) {
sum[i] = sum[i - 1] + mas[i];
}
long long mx = 0, ans = inf;
for (int i = 2 * n, k = s; i > 0; i--, k++) {
if (mx < sum[i + len - 1] - sum[i - 1]) {
mx = sum[i + len - 1] - sum[i - 1];
}
}
for (int i = 2 * n, k = s; i > 0; i--, k++) {
if (mx == sum[i + len - 1] - sum[i - 1]) {
if (k > n) k -= n;
ans = min(ans, (long long)k);
}
}
cout << ans;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string z;
cin >> n >> z;
int c1 = 0, c2 = 0;
for (int i = 0; i < n; i++) {
if (z[i] == 'W')
c1++;
else
c2++;
}
if (c1 % 2 != 0 && c2 % 2 != 0) {
cout << -1;
return 0;
}
if (c1 % 2 == 0) {
vector<int> ans;
for (int i = 0; i < n - 1; i++) {
if (z[i] == 'W') {
if (z[i + 1] == 'W') {
ans.push_back(i + 1);
i++;
} else {
ans.push_back(i + 1);
z[i + 1] = 'W';
}
}
}
int counter = ans.size();
cout << counter << "\n";
for (int i = 0; i < counter; i++) {
cout << ans[i] << " ";
}
return 0;
}
if (c2 % 2 == 0) {
vector<int> ans;
for (int i = 0; i < n - 1; i++) {
if (z[i] == 'B') {
if (z[i + 1] == 'B') {
ans.push_back(i + 1);
i++;
} else {
ans.push_back(i + 1);
z[i + 1] = 'B';
}
}
}
int counter = ans.size();
cout << counter << "\n";
for (int i = 0; i < counter; i++) {
cout << ans[i] << " ";
}
return 0;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long int solve(int n, int x) {
if (n <= 2)
return 1;
else {
n = n - 2;
if (n % x == 0)
return (n / x) + 1;
else
return (n / x) + 2;
}
}
int main() {
long int T;
long int x;
long int n;
cin >> T;
for (int i = 0; i < T; i++) {
cin >> n >> x;
cout << solve(n, x) << endl;
}
return 0;
}
| 7 | CPP |
n = int(input())
cost = [0] + [int(item) for item in input().split()]
route = [0] + [int(r) for r in input().split()]
visited = [0] + [False for i in range(n)]
ans = 0
for i in range(1,n+1):
if visited[i] == True:
continue
circle = []
node = i
while visited[node] == False:
visited[node] = True
circle.append(node)
node = route[node]
temp = circle.copy()
for vertex in temp:
if vertex == node:
break
tmp = circle.pop(0)
if circle != []:
ans += min([cost[item] for item in circle])
print(ans) | 10 | PYTHON3 |
Subsets and Splits