solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, q;
cin >> n >> q;
unsigned long long int l = ceil((n * n) / 2);
unsigned long long int k = l + 1;
while (q--) {
unsigned long long int a, b;
cin >> a >> b;
if ((a + b) % 2 == 0) {
cout << (unsigned long long)((((unsigned long long)(a - 1) * n) + b + 1) /
2)
<< endl;
} else if (((a + b) & 1) && (n & 1)) {
if (b == 1) {
a = a - 1;
b = n;
cout << (unsigned long long)((((unsigned long long)(a - 1) * n) + b +
1) /
2 +
k)
<< endl;
} else {
b = b - 1;
cout << (unsigned long long)((((unsigned long long)(a - 1) * n) + b +
1) /
2 +
k)
<< endl;
}
} else if ((a + b) % 2 == 1 && n % 2 == 0) {
if (b % 2 == 0) {
b = b - 1;
cout << (unsigned long long)((((unsigned long long)(a - 1) * n) + b +
1) /
2 +
l)
<< endl;
} else {
b = b + 1;
cout << (unsigned long long)((((unsigned long long)(a - 1) * n) + b +
1) /
2 +
l)
<< endl;
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double g,r;
cin>>r>>g;
cout<<g*2-r<<endl;
} | 0 |
#include<vector>
#include<iostream>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
void solve(int q,vector<string> &a,vector<int> &b,vector<int> &c){
rep(i,a.size()){
int f = c[i]-b[i]+1;
if (f <= q && q <= c[i]){
cout <<a[i] <<" " << q-f+1 << endl;
return;
}
}
cout <<"Unknown" << endl;
}
int main(){
int n,q;
while(cin>>n>>q && n){
vector<string> a(n);
vector<int> b(n),c(n);
rep(i,n)cin>>a[i]>>b[i]>>c[i];
rep(i,q){
int p;cin>>p;solve(p,a,b,c);
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1000000;
pair<pair<int, int>, int> target[maxN], shot[maxN];
int res[maxN];
int sq(int x) { return x * x; }
int ins(pair<int, int> sh, pair<int, int> ta) {
return sq(sh.first - ta.first) + sq(sh.second) <= sq(ta.second);
}
int main() {
int n, m;
cin >> n;
for (int i = 0; i < (n); ++i) {
cin >> target[i].first.first >> target[i].first.second;
target[i].second = i;
}
sort(target, target + n);
cin >> m;
for (int i = 0; i < (m); ++i) {
cin >> shot[i].first.first >> shot[i].first.second;
shot[i].second = i;
}
sort(shot, shot + m);
for (int i = 0; i < (n); ++i) res[i] = INT_MAX;
int nt = 0;
for (int i = 0; i < (m); ++i) {
while (nt < n && shot[i].first.first >
target[nt].first.first + target[nt].first.second)
nt++;
if (nt == n) break;
if (ins(shot[i].first, target[nt].first)) {
res[target[nt].second] = (res[target[nt].second] > (shot[i].second))
? (shot[i].second)
: res[target[nt].second];
}
if (nt + 1 < n && ins(shot[i].first, target[nt + 1].first)) {
res[target[nt + 1].second] =
(res[target[nt + 1].second] > (shot[i].second))
? (shot[i].second)
: res[target[nt + 1].second];
}
}
cout << n - count(res, res + n, INT_MAX) << endl;
for (int i = 0; i < (n); ++i)
cout << (res[i] == INT_MAX ? -1 : res[i] + 1) << (i + 1 == n ? '\n' : ' ');
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x;
long long a[100007];
cin >> n >> x;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long time = 0;
long long i;
for (i = 0; i < n; i++) {
time += a[i] * x;
if (x > 1) x--;
}
cout << time;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const int MOD = 1e9 + 7;
int main() {
string s;
cin >> s;
int len = s.length();
int l, r, qq, ans, ck;
ans = 0;
for (int i = 0; i < len - 1; i++) {
l = r = qq = ck = 0;
for (int j = i; j < len; j++) {
if (s[j] == '(') {
l++;
ck++;
} else if (s[j] == ')') {
r++;
ck = max(0, ck - 1);
} else {
qq++;
ck = max(0, ck - 1);
}
if (r > l + qq) break;
if ((j - i + 1) % 2 == 0 && ck == 0 && (j - i + 1) / 2 >= l) ans++;
}
}
printf("%d\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 300000;
int a[max_n], b[max_n], p[max_n];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]--;
}
for (int i = 0; i < n; i++) {
int t;
scanf("%d", &t);
t--;
b[t] = i;
}
int res = n;
for (int i = 0; i < n; i++, res--) {
p[i] = b[a[i]];
if (i && p[i - 1] > p[i]) {
break;
}
}
printf("%d\n", res);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, p[maxn], in[maxn], out[maxn], T, st[maxn << 1][20],
lg[maxn << 1] = {-1}, o[30] = {1}, node[maxn];
int op, x, y, d, q, dep[maxn];
vector<int> G[maxn];
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
st[in[u] = ++T][0] = u;
for (auto v : G[u]) {
dfs(v, u);
st[++T][0] = u;
}
out[u] = T;
}
int ckmin(int u, int v) { return dep[u] < dep[v] ? u : v; }
void st_init() {
for (int i = 1; i <= 29; i++) o[i] = o[i - 1] * 2;
for (int i = 1; i <= T; i++) lg[i] = lg[i >> 1] + 1;
for (int j = 1; o[j] <= T; j++)
for (int i = 1; i + o[j] - 1 <= T; i++)
st[i][j] = ckmin(st[i][j - 1], st[i + o[j - 1]][j - 1]);
}
int lca(int u, int v) {
if (in[u] > in[v]) swap(u, v);
int k = lg[in[v] - in[u] + 1];
return ckmin(st[in[u]][k], st[in[v] - o[k] + 1][k]);
}
pair<int, int> road[maxn << 2];
bool v_on_road(int x, int u, int v) {
return (lca(u, v) == lca(u, x) && lca(v, x) == x) ||
(lca(u, v) == lca(v, x) && lca(u, x) == x);
}
bool road_cover(int x, int y, int u, int v) {
return v_on_road(x, u, v) && v_on_road(y, u, v);
}
pair<int, int> merge(pair<int, int> x, pair<int, int> y) {
if (!x.first || !y.first) return {0, 0};
int u[] = {x.first, x.second, y.first, y.second}, a[] = {0, 1, 2, 3};
do {
if (road_cover(u[a[0]], u[a[1]], u[a[2]], u[a[3]]))
return {u[a[2]], u[a[3]]};
} while (next_permutation(a, a + 4));
return {0, 0};
}
void build(int l, int r, int rt) {
if (l == r) {
road[rt] = {node[l], node[l]};
return void();
}
int m = (l + r) >> 1;
build(l, m, rt << 1), build(m + 1, r, rt << 1 | 1);
road[rt] = merge(road[rt << 1], road[rt << 1 | 1]);
}
void modify(int l, int r, int rt, int p) {
if (l == r) {
road[rt] = {node[l], node[l]};
return void();
}
int m = (l + r) >> 1;
if (p <= m)
modify(l, m, rt << 1, p);
else
modify(m + 1, r, rt << 1 | 1, p);
road[rt] = merge(road[rt << 1], road[rt << 1 | 1]);
}
int query(int l, int r, int rt, pair<int, int> cur) {
if (l == r) return merge(cur, road[rt]).first ? l : l - 1;
int m = (l + r) >> 1;
if (!road[rt << 1].first) return query(l, m, rt << 1, cur);
if (!cur.first) return query(m + 1, r, rt << 1 | 1, road[rt << 1]);
if (merge(cur, road[rt << 1]).first)
return query(m + 1, r, rt << 1 | 1, merge(cur, road[rt << 1]));
return query(l, m, rt << 1, cur);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", p + i);
for (int i = 1; i <= n; i++) node[p[i]] = i;
for (int i = 2; i <= n; i++) {
scanf("%d", &d);
G[d].push_back(i);
}
dfs(1, 0), st_init(), build(0, n - 1, 1);
scanf("%d", &q);
while (q--) {
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &x, &y);
swap(p[x], p[y]), swap(node[p[x]], node[p[y]]);
modify(0, n - 1, 1, p[x]), modify(0, n - 1, 1, p[y]);
} else
printf("%d\n", query(0, n - 1, 1, {0, 0}) + 1);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int min(long long int a, long long int b) { return a < b ? a : b; }
long long int min(long long int a, long long int b, long long int c) {
a = min(a, b);
a = min(a, c);
return a;
}
long long int max(long long int a, long long int b) { return a > b ? a : b; }
long long int max(long long int a, long long int b, long long int c) {
a = max(a, b);
a = max(a, c);
return a;
}
long long int dsum(long long int n) {
long long int ans = 0;
while (n) {
ans += (n % 10);
n /= 10;
}
return ans;
}
long long int power(long long int a, long long int b) {
long long int res = 1;
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
long long int Ceil(long long int a, long long int b) {
long long int res = a / b;
if (a % b != 0) res++;
return res;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &t : v) is >> t;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &t : v) {
os << t << "\n";
}
os << '\n';
return os;
}
long long int dxx[] = {1, 1, 0, -1, -1, -1, 0, 1};
long long int dyy[] = {0, 1, 1, 1, 0, -1, -1, -1};
long long int dx[] = {1, 0, -1, 0};
long long int dy[] = {0, 1, 0, -1};
struct pll {
long long int x, y;
};
bool comp(pll a, pll b) {
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
void solve(long long int the) {
char ch;
double aa, bb, cc, dd, xx;
string s1, s2, s3, str;
long long int i, j, k, a, b, c, d, n, m, l, r, x, y, z, low, high, mid,
sum = 0, ans = 0, temp, t;
cin >> n;
vector<long long int> v(n);
cin >> v;
vector<long long int> vv(n);
vector<double> v1;
for (i = 0; i < n; i++) v1.push_back(v[i]);
c = 0;
for (i = 0; i < n; i++) {
if (v[i] % 2 == 0) {
vv[i] = v[i] / 2;
}
}
a = 0;
for (i = 0; i < n; i++) {
if ((v[i] < 0) && (v[i] % 2 != 0)) {
if (a == 0) {
a = 1;
vv[i] = floor(v1[i] / 2);
} else {
a = 0;
vv[i] = ceil(v1[i] / 2);
}
}
}
a = 0;
for (i = 0; i < n; i++) {
if ((v[i] > 0) && (v[i] % 2 != 0)) {
if (a == 0) {
a = 1;
vv[i] = ceil(v1[i] / 2);
} else {
a = 0;
vv[i] = floor(v1[i] / 2);
}
}
}
cout << vv;
}
int main() {
long long int t = 1, c = 1;
while (t--) {
solve(c);
c++;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int inf = 0x7FFFFFFF;
template <class T>
inline bool checkmin(T &a, T b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool checkmax(T &a, T b) {
return b > a ? a = b, 1 : 0;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
int n, m, x[200], l[200], r[200];
int id[200];
int main(int argc, char *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &x[i]);
id[i] = i;
}
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
if (x[i] > x[j]) {
swap(x[i], x[j]);
swap(id[i], id[j]);
}
for (int i = 1; i <= m; i++) scanf("%d%d", &l[i], &r[i]);
for (int i = 1; i <= n; i++) x[id[i]] = i % 2;
for (int i = 1; i <= n; i++) {
if (i < n)
printf("%d ", x[i]);
else
printf("%d\n", x[i]);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
int a = N;
string ans;
if (a == 0) {
ans = "0";
}
while (a != 0) {
if (a % 2 == 0) {
ans = "0" + ans;
} else {
ans = "1" + ans;
a--;
}
a /= -2;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const long long N = 1000000;
long long n;
long long r1, r2, r3, d;
long long a[N + 1];
long long dp[N + 1];
long long x[N + 1], y[N + 1];
long long Sum[N + 1];
signed main() {
cin >> n >> r1 >> r2 >> r3 >> d;
for (long long i = 1; i <= n; i++) scanf("%lld", a + i);
for (long long i = 1; i <= n; i++) {
x[i] = a[i] * min(r1, r3) + r3;
y[i] = min(r2 + min(min(r1, r2), r3),
a[i] * min(r1, r3) + min(r1, r2) + min(min(r1, r2), r3));
Sum[i] = Sum[i - 1] + min(x[i], y[i]);
}
long long mn = inf;
dp[1] = x[1];
for (long long i = 2; i < n; i++) {
mn = min(mn, dp[i - 2] - Sum[i - 2] - 2 * (i - 1) * d);
dp[i] = min(dp[i - 1] + x[i], mn + Sum[i] + 2 * i * d);
}
dp[n] = dp[n - 1] + x[n];
for (long long i = 0; i <= n - 2; i++)
dp[n] = min(dp[n], dp[i] + Sum[n] - Sum[i] + 2 * n * d - 2 * (i + 1) * d),
dp[n] =
min(dp[n], dp[i] + Sum[n - 1] - Sum[i] + x[n] + n * d - (i + 1) * d);
cout << dp[n] + (n - 1) * d;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long dp[20][20];
int pre[50][50];
int a[50];
long long bin[20];
void print(int x, int y) {
if (x == 0 && y == 0) return;
if (pre[x][y] == 1) {
print(x - 1, y);
printf("M");
} else {
print(x, y - 1);
printf("H");
}
}
int main() {
int n;
bin[0] = 1;
for (int i = 1; i < 20; i++) bin[i] = 10 * bin[i - 1];
scanf("%d", &n);
for (int i = 1; i <= 2 * n; i++) scanf("%1d", &a[i]);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
if (j - 1 >= 0 && i - 1 < 0 ||
i - 1 >= 0 && j - 1 >= 0 &&
dp[i - 1][j] + a[i + j] * bin[n - i] <
dp[i][j - 1] + a[i + j] * bin[n - j]) {
dp[i][j] = dp[i][j - 1] + a[i + j] * bin[n - j];
pre[i][j] = 2;
} else if (j - 1 < 0 && i - 1 >= 0 ||
i - 1 >= 0 && j - 1 >= 0 &&
dp[i - 1][j] + a[i + j] * bin[n - i] >=
dp[i][j - 1] + a[i + j] * bin[n - j]) {
dp[i][j] = dp[i - 1][j] + a[i + j] * bin[n - i];
pre[i][j] = 1;
}
print(n, n);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
const int M = 1e9 + 7;
const double eps = 1e-6;
const double PI(acos(-1.0));
long long f[11][61][2049];
int q;
long long b, l, r;
int w[65], t;
long long dfs(int pos, int lim, int state, int start) {
if (pos < 1) return !state;
if (!lim && !start && ~f[b][pos][state]) return f[b][pos][state];
long long ans = 0;
for (int i = start; i < b; i++) {
if (lim && i > w[pos]) break;
ans += dfs(pos - 1, lim && (i == w[pos]), state ^ (1 << i), 0);
}
return (!lim && !start) ? f[b][pos][state] = ans : ans;
}
long long sum(long long x) {
for (t = 0; x; x /= b) w[++t] = x % b;
long long ans = 0;
for (int i = (1); i <= (t); i++) ans += dfs(i, i == t, 0, 1);
return ans;
}
int main() {
memset(f, -1, sizeof f);
for (cin >> q; q--;) {
cin >> b >> l >> r;
cout << sum(r) - sum(l - 1) << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:667772160")
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
long long a, b, st;
long long call(long long n) {
long long temp = 2 * st + (n - 1) * b;
temp = temp * n;
return temp / 2;
}
long long higt(long long n) { return a + (n - 1) * b; }
int main() {
long long n, l, m, t;
scanf("%lld %lld", &a, &b);
scanf("%lld", &n);
while (n--) {
scanf("%lld %lld", &l, &t);
scanf("%lld", &m);
long long temp = higt(l);
if (temp > t) {
printf("-1\n");
continue;
}
long long all = m * t;
long long lo = l, hg = l + max(m, t) + 7;
st = higt(l);
while (lo <= hg) {
long long mid = (lo + hg) / 2;
temp = call(mid - l + 1);
if (higt(mid) <= t and temp <= all)
lo = mid + 1;
else
hg = mid - 1;
}
printf("%lld\n", lo - 1);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, c[210], p[210], d[210];
bool b[210], a[210][210];
int find(int k) {
for (int i = 1; i <= n; i++)
if (c[i] == k)
if (d[i] == 0 && b[i]) return i;
return n + 1;
}
int calc(int k) {
memset(b, true, sizeof(b));
for (int i = 1; i <= n; i++) d[i] = p[i];
int ret = 0;
for (int z = 1; z <= n; z++) {
int x = find(k);
while (x > n) {
k++;
if (k == 4) k = 1;
ret++;
x = find(k);
}
b[x] = false;
ret++;
for (int i = 1; i <= n; i++)
if (a[i][x]) d[i]--;
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
memset(a, false, sizeof(a));
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
for (int j = 1; j <= p[i]; j++) {
int y;
scanf("%d", &y);
a[i][y] = true;
}
}
int ans = calc(1);
int temp;
temp = calc(2);
if (temp < ans) ans = temp;
temp = calc(3);
if (temp < ans) ans = temp;
printf("%d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;
const int mod = 1e9 + 7;
int a[maxn];
pair<int, int> dp[1 << 21];
void add(int x, int id) {
if (dp[x].first == -1) {
dp[x].first = id;
} else if (dp[x].second == -1) {
if (dp[x].first == id) return;
dp[x].second = id;
if (dp[x].first < dp[x].second) swap(dp[x].first, dp[x].second);
} else if (dp[x].first < id) {
dp[x].second = dp[x].first;
dp[x].first = id;
} else if (dp[x].second < id) {
if (dp[x].first == id) return;
dp[x].second = id;
}
}
void merge(int x1, int x2) {
add(x1, dp[x2].first);
add(x1, dp[x2].second);
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(a[i], i);
}
int s = 1 << 21;
for (int j = 0; j < 21; j++) {
for (int i = 0; i < s; i++) {
if (!(i >> j & 1)) merge(i, i ^ (1 << j));
}
}
int ans = 0;
for (int i = 1; i <= n - 2; i++) {
int res = 0, d = 0;
for (int j = 20; j >= 0; j--) {
if (a[i] >> j & 1)
res |= (1 << j);
else {
int mi = min(dp[d | (1 << j)].first, dp[d | (1 << j)].second);
if (mi > i) res |= (1 << j), d |= (1 << j);
}
}
ans = max(ans, res);
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long int num) {
bool flag = true;
for (long long int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = false;
break;
}
}
return flag;
}
bool isPowerOfTwo(int n) {
if (n == 0) return false;
return (ceil(log2(n)) == floor(log2(n)));
}
long long int OcD(long long int n) {
long long int x = 0;
long long int base = 1;
long long int t = n;
while (t != 0) {
long long int xp = t % 10;
t = t / 10;
x += xp * base;
base = base * 8;
}
return x;
}
long long int DcO(long long int n) {
int remainder;
long octal = 0, i = 1;
while (n != 0) {
remainder = n % 8;
n = n / 8;
octal = octal + (remainder * i);
i = i * 10;
}
return octal;
}
long long int DropLeadingDigit(long long int number) {
return (number %
(long long int)pow((long double)10,
(long double)floor(log((long double)number) /
log((long double)10))));
}
long long int remove_leftmost_digit(long long int n) {
static char temp[11];
sprintf(temp, "%d", n);
return atoi(temp + 1);
}
void Remove(char str[], long long int n) {
set<char> s(str, str + n - 1);
for (set<char>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a, b, c, d, e = 0, f = 0, n = 0, i, k = 0, j = 0, l;
long long int x[1000], y[1000];
string s, p, t;
cin >> a >> b >> c;
n = max(a, max(b, c));
i = (a + b + c) - n;
if (n < i)
cout << 0 << endl;
else {
j = n - i;
cout << j + 1 << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 234567;
vector <int> G[N];
int d[N], f[N], n, x, y;
void dfs(int u, int p, int d[]) {
for (auto v : G[u])
if (v != p) {
d[v] = d[u] + 1;
dfs(v, u, d);
}
}
int main() {
cin >> n >> x >> y;
for (int i = 1; i < n; i ++) {
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v); G[v].push_back(u);
}
dfs(y, 0, f);
dfs(x, 0, d);
int ans = 0;
for (int i = 1; i <= n; i ++) {
if (f[i] > d[i]) ans = max(ans, f[i]);
}
cout << ans-1 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[100005];
long long cnt[100];
int main() {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j < 60; j++) {
if (a[i] & ((long long)1 << j)) cnt[j]++;
}
}
long long ans = -1;
for (long long j = 59; j >= 0; j--) {
if (cnt[j] == 1) {
ans = j;
break;
}
}
if (ans == -1) {
for (long long i = 1; i <= n; i++) {
printf("%lld ", a[i]);
}
} else {
long long temp;
for (long long i = 1; i <= n; i++) {
if (a[i] & ((long long)1 << ans)) temp = i;
}
printf("%lld ", a[temp]);
for (long long i = 1; i <= n; i++) {
if (i == temp) continue;
printf("%lld ", a[i]);
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long ans, f[1000005], h[1000005], g[1000005], t;
long long n, m, k;
long long pow1(long long a, long long b) {
long long t = 1;
while (b) {
if (b & 1) t = t * a % 1000000007;
a = a * a % 1000000007;
b = b >> 1;
}
return t;
}
long long C(long long a, long long b) {
return h[a] * g[b] % 1000000007 * g[a - b] % 1000000007;
}
int main() {
scanf("%I64d%I64d%I64d", &n, &m, &k);
if (m == 1) {
printf("%I64d", pow1(k, n));
return 0;
}
for (long long i = 0; i <= 1000000; i++) h[i] = g[i] = 1;
for (long long i = 1; i <= 1000000; i++)
h[i] = h[i - 1] * i % 1000000007, g[i] = pow1(h[i], 1000000007 - 2);
for (long long i = 1; i <= min(n, k); i++) {
f[i] = pow1(i, n);
for (long long j = 1; j < i; j++)
f[i] = (f[i] - C(i, j) * f[j] % 1000000007 + 1000000007) % 1000000007;
}
if (m == 2) {
for (long long i = 1; i <= min(n, k); i++) {
t = f[i] * f[i] % 1000000007;
t = t * C(k, i) % 1000000007 * C(k, i) % 1000000007;
ans = (ans + t) % 1000000007;
}
} else {
for (long long i = 1; i <= min(n, k); i++)
for (long long j = 0; j <= min(n, k); j++) {
if (k < 2 * j + i || i + j > n) break;
t = f[i + j] * f[i + j] % 1000000007;
t = t * C(k, i) % 1000000007 * C(k - i, j) % 1000000007 *
C(k - i - j, j) % 1000000007;
t = t * pow1(i, n * (m - 2)) % 1000000007;
ans = (ans + t) % 1000000007;
}
}
printf("%I64d", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long dx[4] = {-1, 0, 0, 1};
const long long dy[4] = {0, -1, 1, 0};
void faster() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long n, m, k, b, x, y, ans, sum, a[456789], br[456789], sb, l, u, r, p, q,
i, j;
string s, t, str;
map<long long, long long> cnt;
char c;
bool ok1, ok2;
void dii() {
cin >> c >> n;
if (c == 'h' || c == 'a') ok1 = 1;
if (n == 1 || n == 8) ok2 = 1;
if (ok2 && ok1) {
cout << 3;
} else if (ok1 || ok2) {
cout << 5;
} else {
cout << 8;
}
}
int main() {
faster();
int q = 1;
while (q--) {
dii();
}
return 0;
}
| 1 |
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
inline double ABS(double a){return max(a,-a);}
struct Pt {
double x, y;
Pt() {}
Pt(double x, double y) : x(x), y(y) {}
Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
Pt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); }
Pt operator-() const { return Pt(-x, -y); }
Pt operator*(const double &k) const { return Pt(x * k, y * k); }
Pt operator/(const double &k) const { return Pt(x / k, y / k); }
bool operator < (const Pt &a)const{
if(sig(x-a.x))return 0>sig(x-a.x);
return 0>sig(y-a.y);
}
double ABS() const { return sqrt(x * x + y * y); }
double abs2() const { return x * x + y * y; }
double arg() const { return atan2(y, x); }
double dot(const Pt &a) const { return x * a.x + y * a.y; }
double det(const Pt &a) const { return x * a.y - y * a.x; }
};
bool eq(Pt a,Pt b){
return sig((a-b).abs2())==0;
}
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iSP(Pt a, Pt b, Pt c) {
int s = sig((b - a).det(c - a));
if (s) return s;
if (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b
if (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c
return 0;
}
Pt pLL(Pt a, Pt b, Pt c, Pt d) {
b = b - a; d = d - c; return a + b * (c - a).det(d) / b.det(d);
}
int iLL(Pt a, Pt b, Pt c, Pt d) {
if (sig((b - a).det(d - c))) return 1; // intersect
if (sig((b - a).det(c - a))) return 0; // parallel
return -1; // correspond
}
bool iSS(Pt a, Pt b, Pt c, Pt d) {
return (iSP(a, b, c) * iSP(a, b, d) <= 0 && iSP(c, d, a) * iSP(c, d, b) <= 0);
}
bool iSSstrict(Pt a, Pt b, Pt c, Pt d) {
return (sig(tri(a, b, c)) * sig(tri(a, b, d)) < 0 && sig(tri(c, d, a)) * sig(tri(c, d, b)) < 0);
}
Pt hLP(Pt a,Pt b,Pt c){
return pLL(a,b,c,c+(b-a)*Pt(0,1));
}
Pt tLP(Pt a,Pt b,Pt c){
Pt H=hLP(a,b,c);
return H+H-c;
}
int sGP(int n, vector<Pt>p, Pt a) {
int side = -1, i;
p.push_back(p[0]);
for (i = 0; i < n; ++i) {
Pt p0 = p[i] - a, p1 = p[i + 1] - a;
if (sig(p0.det(p1)) == 0 && sig(p0.dot(p1)) <= 0) return 0;
if (p0.y > p1.y) swap(p0, p1);
if (sig(p0.y) <= 0 && 0 < sig(p1.y) && sig(p0.det(p1)) > 0) side = -side;
}
return side;
}
vector<Pt>poly[110];
vector<Pt>hb;
double ijk[11000];
int v[11000];
bool iGSstrict(int t, Pt a, Pt b) {
for(int i=0;i<poly[t].size();i++){
if(iLL(a,b,poly[t][i],poly[t][(i+1)%poly[t].size()])==-1)continue;
if(iSSstrict(a,b,poly[t][i],poly[t][(i+1)%poly[t].size()]))return 1;
}
vector<Pt>tmp;
for(int i=0;i<poly[t].size();i++){
if(iLL(a,b,poly[t][i],poly[t][(i+1)%poly[t].size()])==-1)continue;
if(iSS(a,b,poly[t][i],poly[t][(i+1)%poly[t].size()])){
tmp.push_back(pLL(a,b,poly[t][i],poly[t][(i+1)%poly[t].size()]));
}
}
tmp.push_back(a);
tmp.push_back(b);
//if(sGP(poly[t].size(),poly[t],(a*0.99999+b*0.00001))==-1)return 1;
//if(sGP(poly[t].size(),poly[t],(b*0.99999+a*0.00001))==-1)return 1;
std::sort(tmp.begin(),tmp.end());
for(int i=1;i<tmp.size();i++){
Pt M=(tmp[i-1]+tmp[i])/2;
if(sGP(poly[t].size(),poly[t],M)==-1)return 1;
}
// printf("%d (%f %f) -> (%f %f)\n",t,a.x,a.y,b.x,b.y);
return 0;
}
int main(){
int a;
while(scanf("%d",&a),a){
for(int i=0;i<110;i++)poly[i].clear();
for(int i=0;i<a;i++){
int b;scanf("%d",&b);
for(int j=0;j<b;j++){
double X,Y;
scanf("%lf%lf",&X,&Y);
poly[i].push_back(Pt(X,Y));
}
}
double X1,Y1,X2,Y2;
scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
Pt S1=Pt(X1,Y1);
Pt G1=Pt(X2,Y2);
scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
Pt L1=Pt(X1,Y1);
Pt L2=Pt(X2,Y2);
Pt S2=tLP(L1,L2,S1);
Pt G2=tLP(L1,L2,G1);
int f1=-1;
int f2=-1;
int t1=-1;
int t2=-1;
for(int i=0;i<a;i++){
int n=poly[i].size();
if(~sGP(n,poly[i],S1))f1=i;
if(~sGP(n,poly[i],S2))f2=i;
if(~sGP(n,poly[i],G1))t1=i;
if(~sGP(n,poly[i],G2))t2=i;
}
if(f1==-1||f2==-1||t1==-1||t2==-1||f1!=t1||f2!=t2){
printf("impossible\n");continue;
}
hb.clear();
for(int i=0;i<poly[f1].size();i++)hb.push_back(poly[f1][i]);
for(int i=0;i<poly[f2].size();i++)hb.push_back(tLP(L1,L2,poly[f2][i]));
/* for(int i=0;i<poly[f1].size();i++){
for(int j=0;j<poly[f2].size();j++){
Pt v1=tLP(L1,L2,poly[f2][j]);
Pt v2=tLP(L1,L2,poly[f2][(j+1)%poly[f2].size()]);
if(iSSstrict(poly[f1][i],poly[f1][(i+1)%poly[f1].size()],v1,v2)){
hb.push_back(pLL(poly[f1][i],poly[f1][(i+1)%poly[f1].size()],v1,v2));
}
}
}*/
int S=hb.size();
hb.push_back(S1);
int T=hb.size();
hb.push_back(G1);
int sz=hb.size();
for(int i=0;i<11000;i++){
ijk[i]=999999999;v[i]=0;
}
ijk[S]=0;
// for(int i=0;i<sz;i++)printf("%f %f\n",hb[i].x,hb[i].y);
for(int i=0;i<sz;i++){
double dist=99999999;
int at=-1;
for(int j=0;j<sz;j++){
if(!v[j]&&ijk[j]<dist){
dist=ijk[j];
at=j;
}
}
if(!~at)break;
// printf("%f %f: %f\n",hb[at].x,hb[at].y,ijk[at]);
v[at]=1;
Pt from1=hb[at];
Pt from2=tLP(L1,L2,hb[at]);
for(int j=0;j<sz;j++){
if(v[j])continue;
if(ijk[j]<dist+(from1-hb[j]).ABS())continue;
Pt to1=hb[j];
Pt to2=tLP(L1,L2,to1);
// printf("%f %f %f %f\n",from1.x,from1.y,from2.x,from2.y);
if(iGSstrict(f1,from1,to1)){
// printf("(%f %f) -> (%f %f): hit on 1\n",from1.x,from1.y,to1.x,to1.y);
continue;
}
if(iGSstrict(f2,from2,to2)){
// printf("(%f %f) -> (%f %f): hit on 2\n",from1.x,from1.y,to1.x,to1.y);
continue;
}
ijk[j]=dist+(from1-hb[j]).ABS();
}
}
if(ijk[T]>99999999)printf("impossible\n");
else printf("%.12f\n",ijk[T]);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<long long, long long>;
using vi = vector<long long>;
using vvi = vector<vi>;
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto i = begin(v); i != end(v); i++)
os << *i << (i == end(v) - 1 ? "" : " ");
return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto i = begin(v); i != end(v); i++) is >> *i;
return is;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T, class U>
bool chmax(T &a, const U &b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
void psum(T &c) {
partial_sum(begin(c), end(c), begin(c));
}
template <class T>
using heap = priority_queue<T, deque<T>, greater<T>>;
struct before_main_function {
before_main_function() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << setprecision(15) << fixed;
}
} before_main_function;
signed main() {
vi a(6);
cin >> a;
long long n;
cin >> n;
vi b(n);
cin >> b;
vector<pii> c;
c.reserve(n * 6);
for (long long i = 0, ilen = (long long)(n); i < ilen; ++i) {
for (long long j = 0, jlen = (long long)(6); j < jlen; ++j) {
c.emplace_back(b[i] - a[j], i);
}
}
sort(begin(c), end(c));
map<long long, long long> cnt;
long long l = 0;
long long ans = INF;
for (long long r = 0, rlen = (long long)(((long long)c.size())); r < rlen;
++r) {
cnt[c[r].second] += 1;
while (((long long)cnt.size()) == n) {
chmin(ans, c[r].first - c[l].first);
if (cnt[c[l].second] == 1) {
cnt.erase(c[l].second);
} else {
cnt[c[l].second] -= 1;
}
l += 1;
}
}
cout << ans << endl;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
const double EPS=1e-6;
double evaluate(vector<double>&r){
double res=r[0]+r[r.size()-1];
for(int i=0;i+1<r.size();i++){
double h=(r[i]+r[i+1]);
double s=abs(r[i]-r[i+1]);
res+=sqrt(h*h-s*s);
}
return res;
}
double optimize(vector<double>r){
if(r.size()<=5){
sort(r.begin(),r.end());
double res=evaluate(r);
do{
res=min(res,evaluate(r));
}while(next_permutation(r.begin(),r.end()));
return res;
}
double T=10000;
double cool=0.99;
double mi=evaluate(r);
while(T>0.000001){
int a,b;
while(true){
a=rand()%r.size();
b=rand()%r.size();
if(a!=b)break;
}
double score=evaluate(r);
swap(r[a],r[b]);
double new_score=evaluate(r);
double p=exp(-abs(new_score-score)/T);
if(new_score>score || rand()<p*RAND_MAX);
else{
swap(r[a],r[b]);
}
mi=min(mi,new_score);
T*=cool;
}
return mi;
}
int main(){
srand((unsigned)time(NULL));
int l;
while(cin>>l){
string str;
getline(cin,str);
stringstream ss(str);
vector<double>r;
while(!ss.eof()){
double t;
ss>>t;
r.push_back(t);
}
if(optimize(r)<l+EPS)puts("OK");
else puts("NA");
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k, a, s, t;
int no_of_days() {
cin >> n >> k;
while (n--) cin >> a, s += a / k, t += (a % k > 0);
return (s + t + 1) / 2;
}
int main() { cout << no_of_days(); }
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, match[2010][2010], deg[2010][2010], xs[5] = {0, -1, 0, 1},
ys[5] = {1, 0, -1, 0};
string s[2100];
bool mark[2010][2010], mark2[2010][2010];
bool check(int i, int j) {
if (i >= n || j >= m || i < 0 || j < 0 || s[i][j] == '*') return 0;
return 1;
}
void dfs(int i, int j) {
mark[i][j] = 1;
for (int g = 0; g < 4; g++) {
int x2 = i + xs[g], y2 = j + ys[g];
if (check(x2, y2)) {
deg[i][j]++;
if (!mark[x2][y2]) dfs(x2, y2);
}
}
}
pair<int, int> find(int x, int y) {
for (int i = 0; i < 4; i++) {
int x2 = x + xs[i], y2 = y + ys[i];
if (check(x2, y2) && !mark2[x2][y2]) return make_pair(x2, y2);
}
}
void work(int x, int y, int i, int j) {
if (!check(x, y)) return;
mark2[i][j] = 1;
deg[x][y]--;
}
void BFS(int i, int j) {
if (deg[i][j] != 1 || match[i][j] != -1 || !check(i, j) || mark2[i][j])
return;
pair<int, int> p = find(i, j);
match[i][j] = p.first * m + p.second;
match[p.first][p.second] = i * m + j;
work(i + 1, j, i, j);
work(i - 1, j, i, j);
work(i, j + 1, i, j);
work(i, j - 1, i, j);
int x = p.first, y = p.second;
work(x + 1, y, x, y);
work(x, y + 1, x, y);
work(x - 1, y, x, y);
work(x, y - 1, x, y);
BFS(x + 1, y);
BFS(x - 1, y);
BFS(x, y + 1);
BFS(x, y - 1);
BFS(i + 1, j);
BFS(i - 1, j);
BFS(i, j + 1);
BFS(i, j - 1);
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == '.' && !mark[i][j]) dfs(i, j);
for (int i = 0; i < n; i++) fill(match[i], match[i] + m, -1);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (match[i][j] == -1) BFS(i, j);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (match[i][j] == -1 && s[i][j] == '.') {
cout << "Not unique" << endl;
return 0;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == '.') {
int x = match[i][j];
int y = i * m + j;
if (x - y == -m)
s[i][j] = 'v';
else if (x - y == m)
s[i][j] = '^';
else if (x - y == 1)
s[i][j] = '<';
else if (x - y == -1)
s[i][j] = '>';
}
for (int i = 0; i < n; i++) cout << s[i] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:160777216")
using namespace std;
int i, j, n, m, k;
int cnt = 0;
int main() {
int t;
cin >> t;
while (t--) {
cin >> n;
if (n == 1) {
cout << "! " << 1 << " " << 1 << endl;
continue;
}
int mn = 100;
int mx = -100;
for (i = 1; i <= n; i += 2) {
char c;
int l = i;
int r = i + 1;
if (n == i)
r = i;
else {
cout << "? " << l << " " << r << endl;
cout.flush();
cin >> c;
if (c == '>') swap(l, r);
}
if (mn == 100)
mn = l;
else {
cout << "? " << mn << " " << l << endl;
cout.flush();
cin >> c;
if (c == '>') mn = l;
}
if (mx == -100)
mx = r;
else {
cout << "? " << mx << " " << r << endl;
cout.flush();
cin >> c;
if (c == '<') mx = r;
}
}
cout << "! " << mn << " " << mx << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int a[128];
int main() {
int n, cnt = 0, m = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i + 'a'];
m = gcd(a[i + 'a'], m);
cnt += a[i + 'a'] & 1;
}
string s = "";
if (cnt > 1) {
puts("0");
for (int i = 0; i < n; ++i)
while (a[i + 'a']--) putchar(i + 'a');
puts("");
return 0;
}
cout << m << endl;
for (int i = 0; i < n; ++i) a[i + 'a'] /= m;
if (cnt == 1) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < a[i + 'a'] / 2; ++j) s.push_back(i + 'a');
for (int i = 0; i < n; ++i)
if (a[i + 'a'] & 1) s.push_back(i + 'a');
for (int i = n - 1; i >= 0; --i)
for (int j = 0; j < a[i + 'a'] / 2; ++j) s.push_back(i + 'a');
} else
for (int i = 0; i < n; ++i)
for (int j = 0; j < a[i + 'a']; ++j) s.push_back(i + 'a');
for (int i = 0; i < m; ++i) {
if (i & 1)
cout << s;
else
for (int j = s.size() - 1; j >= 0; --j) putchar(s[j]);
}
puts("");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 10, P = 29, mod = 1e9 + 7;
string s;
int n, sum, degree[2 * N];
long long p[N];
long long pref_hash[N], suf_hash[N];
bool is_palindrome(int right) {
return (pref_hash[right] * p[n - right]) % mod ==
(suf_hash[1] - suf_hash[right + 1] + mod) % mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> s;
n = s.size();
p[0] = 1;
for (int i = 1; i <= n + 5; ++i) {
p[i] = p[i - 1] * P % mod;
}
for (int i = 1; i <= n; ++i) {
pref_hash[i] = (pref_hash[i - 1] + ((int)s[i - 1] - 48) * p[i]) % mod;
}
for (int i = n; i >= 1; --i) {
suf_hash[i] = (suf_hash[i + 1] + ((int)s[i - 1] - 48) * p[n - i + 1]) % mod;
}
for (int i = 1; i <= n; ++i) {
if (is_palindrome(i)) {
degree[i]++;
degree[2 * i] = degree[i];
degree[2 * i + 1] = degree[i];
sum += degree[i];
}
}
cout << sum;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int q, n, u, pre[N], suf[N], cnt[N];
long long pw[N], hsh[N];
char s[N];
long long get_hash(int l, int r) { return hsh[r] - hsh[l - 1] * pw[r - l + 1]; }
bool cmp(int u, int v) {
int mx = min(n - u, n - v);
int le = 0, ri = mx;
while (le <= ri) {
int mi = (le + ri) / 2;
if (get_hash(u, u + mi) == get_hash(v, v + mi))
le = mi + 1;
else
ri = mi - 1;
}
if (le > mx)
return u > v;
else
return s[u + le] < s[v + le];
}
int find_prefix(int u, int v) {
if (u == 0) return 0;
int mx = min(n - u, n - v);
int le = 0, ri = mx;
while (le <= ri) {
int mi = (le + ri) / 2;
if (get_hash(u, u + mi) == get_hash(v, v + mi))
le = mi + 1;
else
ri = mi - 1;
}
return le;
}
void DFS(int u, int pr) {
if (--q == 0) {
for (int i = 0; i < pr; i++) putchar(s[suf[u] + i]);
exit(0);
}
cnt[u] = pr;
if (pre[u + 1] >= pr) DFS(u + 1, pr);
}
int main() {
scanf("%s%d", s + 1, &q);
n = strlen(s + 1);
pw[0] = 1;
for (int i = 1; i <= n; i++) {
pw[i] = pw[i - 1] * 31;
hsh[i] = hsh[i - 1] * 31 + s[i] - 'a' + 1;
suf[i] = i;
}
sort(suf + 1, suf + n + 1, cmp);
for (int i = 1; i <= n; i++) pre[i] = find_prefix(suf[i - 1], suf[i]);
for (int i = 1; i <= n; i++)
for (++cnt[i]; cnt[i] <= n - suf[i] + 1; ++cnt[i]) DFS(i, cnt[i]);
printf("No such line.");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
deque<long long> q;
long long n, a, b;
cin >> n;
for (int i = 1; i <= n; i++) {
if (n % i == 0)
q.push_back((n / i) * 2 + i * 2);
else {
a = (n - (n % i)) / i;
if (a < (n % i)) a += (n % i) * a, b = (n % i) % a;
b = n % i;
if (b == 1)
q.push_back(a * 2 + i * 2 + 2);
else
q.push_back(i * 2 + a * 2 + 2);
}
}
sort(q.begin(), q.end());
cout << q[0];
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int BOARD = 1e9;
int N, M;
vector<int> vertical;
vector<int> horizontal;
int main() {
scanf("%d %d", &N, &M);
vertical.resize(N);
for (int &x : vertical) scanf("%d", &x);
sort(vertical.begin(), vertical.end());
for (int i = 0; i < M; i++) {
int x1, x2, y;
scanf("%d %d %d", &x1, &x2, &y);
if (x1 == 1) horizontal.push_back(x2);
}
sort(horizontal.rbegin(), horizontal.rend());
vertical.push_back(BOARD);
int at_least = horizontal.size();
int answer = BOARD;
for (int v = 0; v < (int)vertical.size(); v++) {
int x = vertical[v];
while (at_least > 0 && horizontal[at_least - 1] < x) at_least--;
answer = min(answer, v + at_least);
}
printf("%d\n", answer);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2005, MOD = 1e9 + 7;
int n, h;
int a[N];
long long memo[N][N];
long long dp(int idx, int cnt) {
if (cnt < 0) return 0;
if (idx == n) return (cnt == 0);
if (memo[idx][cnt] != -1) return memo[idx][cnt];
long long &ret = memo[idx][cnt] = 0;
if (a[idx] - cnt - 1 == 0) {
ret += (cnt + 1) * dp(idx + 1, cnt), ret %= MOD;
ret += dp(idx + 1, cnt + 1), ret %= MOD;
}
if (a[idx] - cnt == 0) {
ret += dp(idx + 1, cnt), ret %= MOD;
ret += cnt * dp(idx + 1, cnt - 1) % MOD, ret %= MOD;
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> h;
for (int i = 0; i < n; i++) cin >> a[i], a[i] = h - a[i];
memset(memo, -1, sizeof memo);
long long ans = dp(0, 0);
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 200005;
double x, y, yw, xb, yb, r;
int main() {
scanf("%lf%lf%lf%lf%lf%lf", &y, &x, &yw, &xb, &yb, &r);
x -= 1e-12;
y += 1e-12;
y += r;
double xw = (yw - r - y) * xb / (2 * yw - 2 * r - yb - y);
double xx = xw * (x - y) / sqrt(xw * xw + (yw - r - y) * (yw - r - y));
if (xx <= r)
printf("-1\n");
else
printf("%.12f\n", xw);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 300010;
struct edge {
long long nxt, to;
} e[N << 1];
long long rt, n, m, r, tot, cnt, head[N], dep[N], fa[N], siz[N], son[N], dfn[N],
w[N], a[N], top[N];
struct tree {
long long sum, lz;
} tr[N << 2];
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
return x * f;
}
inline void add(long long u, long long v) {
e[++cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
inline void pushup(long long rt) {
tr[rt].sum = tr[rt << 1].sum + tr[rt << 1 | 1].sum;
}
inline void pushdown(long long rt, long long l, long long r) {
long long mid = (l + r) >> 1;
tr[rt << 1].sum += tr[rt].lz * (mid - l + 1);
tr[rt << 1].lz += tr[rt].lz;
tr[rt << 1 | 1].sum += tr[rt].lz * (r - mid);
tr[rt << 1 | 1].lz += tr[rt].lz;
tr[rt].lz = 0;
}
void build(long long rt, long long l, long long r) {
if (l == r) {
tr[rt].sum = w[l];
return;
}
long long mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(long long rt, long long l, long long r, long long L, long long R,
long long v) {
if (L <= l && r <= R) {
tr[rt].sum += v * (r - l + 1);
tr[rt].lz += v;
return;
}
pushdown(rt, l, r);
long long mid = (l + r) >> 1;
if (L <= mid) update(rt << 1, l, mid, L, R, v);
if (R > mid) update(rt << 1 | 1, mid + 1, r, L, R, v);
pushup(rt);
}
long long query(long long rt, long long l, long long r, long long L,
long long R) {
if (L <= l && r <= R) return tr[rt].sum;
pushdown(rt, l, r);
long long mid = (l + r) >> 1, ans = 0;
if (L <= mid) ans += query(rt << 1, l, mid, L, R);
if (R > mid) ans += query(rt << 1 | 1, mid + 1, r, L, R);
return ans;
}
void dfs1(long long u, long long f, long long deep) {
dep[u] = deep;
fa[u] = f;
siz[u] = 1;
for (long long i = head[u]; i; i = e[i].nxt) {
long long v = e[i].to;
if (v == f) continue;
dfs1(v, u, deep + 1);
siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(long long u, long long _top) {
dfn[u] = ++tot;
w[tot] = a[u];
top[u] = _top;
if (!son[u]) return;
dfs2(son[u], _top);
for (long long i = head[u]; i; i = e[i].nxt) {
long long v = e[i].to;
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
inline long long lca(long long x, long long y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) x ^= y ^= x ^= y;
x = fa[top[x]];
}
if (dep[x] > dep[y]) x ^= y ^= x ^= y;
return x;
}
inline long long find(long long x, long long y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) x ^= y ^= x ^= y;
if (fa[top[x]] == y) return top[x];
x = fa[top[x]];
}
if (dep[x] > dep[y]) x ^= y ^= x ^= y;
return son[x];
}
inline long long LCA(long long x, long long y) {
if (dep[x] > dep[y]) x ^= y ^= x ^= y;
if (lca(x, y) == x) {
if (dfn[rt] >= dfn[y] && dfn[rt] <= dfn[y] + siz[y] - 1) return y;
if (lca(x, rt) == x) return lca(y, rt);
return x;
}
if (dfn[rt] >= dfn[x] && dfn[rt] <= dfn[x] + siz[x] - 1) return x;
if (dfn[rt] >= dfn[y] && dfn[rt] <= dfn[y] + siz[y] - 1) return y;
if ((lca(x, rt) == rt && lca(x, y) == lca(y, rt)) ||
(lca(y, rt) == rt && lca(x, y) == lca(x, rt)))
return rt;
if (lca(x, rt) == lca(y, rt)) return lca(x, y);
if (lca(x, y) != lca(x, rt)) return lca(x, rt);
return lca(y, rt);
}
inline void modify(long long x, long long v) {
if (rt == x) {
update(1, 1, n, 1, n, v);
return;
}
if (lca(rt, x) != x)
update(1, 1, n, dfn[x], dfn[x] + siz[x] - 1, v);
else {
long long p = find(rt, x);
update(1, 1, n, 1, n, v);
update(1, 1, n, dfn[p], dfn[p] + siz[p] - 1, -v);
}
}
inline long long ask(long long x) {
if (x == rt) return query(1, 1, n, 1, n);
if (lca(rt, x) != x) return query(1, 1, n, dfn[x], dfn[x] + siz[x] - 1);
long long p = find(rt, x);
return query(1, 1, n, 1, n) - query(1, 1, n, dfn[p], dfn[p] + siz[p] - 1);
}
signed main() {
n = read();
m = read();
rt = 1;
for (long long i = 1; i <= n; i++) a[i] = read();
for (long long i = 1, u, v; i < n; i++) {
u = read(), v = read();
add(u, v);
add(v, u);
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, 1, n);
while (m--) {
long long opt = read();
if (opt == 1) rt = read();
if (opt == 2) {
long long u = read(), v = read(), w = read();
modify(LCA(u, v), w);
}
if (opt == 3) {
long long k = read();
printf("%lld\n", ask(k));
}
}
return 0;
}
| 5 |
#include "bits/stdc++.h"
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i], A[i]--;
long long NOW = 0, ANS, MIN = 0;
for (int i = 0; i < N - 1; i++) {
if (A[i] > A[i + 1]) NOW += A[i + 1] + 1, MIN++;
else NOW += A[i + 1] - A[i];
}
ANS = NOW;
vector<pair<int, int> > L(N - 1), R(N - 1);
for (int i = 0; i < N - 1; i++) {
L[i] = { A[i], i };
R[i] = { A[i + 1], i };
}
sort(L.begin(), L.end()), sort(R.begin(), R.end());
int LN = 0, RN = 0;
for (int i = 0; i < M; i++) {
NOW -= MIN;
while (LN < N - 1 && L[LN].first == i) {
MIN++;
LN++;
}
while (RN < N - 1 && R[RN].first == i) {
MIN--;
NOW += (A[R[RN].second + 1] - A[R[RN].second] + M) % M;
RN++;
}
ANS = min(ANS, NOW);
}
cout << ANS << endl;
} | 0 |
#include <iostream>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
cout << n - k + 1;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10, D = 10 + 2, L = 20;
int B;
long long inv[L];
long long dp[2][N][D];
long long com(long long n, long long m) {
n %= B;
if (m < 0 || n < m) return 0;
long long ret = n;
for (int i = 1; i < m; ++i) ret = ret * (n - i) % B * inv[i + 1] % B;
return ret;
}
int main() {
int n, d;
scanf("%d%d%d", &n, &d, &B);
if (n <= 2) {
puts("1");
return 0;
}
inv[0] = 0, inv[1] = 1;
for (int i = 2; i <= d; ++i) inv[i] = inv[B % i] * (B - B / i) % B;
int n0 = n - 1 >> 1;
for (int i = 0; i <= d; ++i) dp[1][i + 1][i] = 1;
for (int k = 2; k <= n0; ++k) {
long long(&f)[N][D] = dp[k - 1 & 1], (&g)[N][D] = dp[k & 1];
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= d; ++j) {
g[i][j] = f[i][j];
for (int t = 1; t <= j && k * t < i; ++t)
g[i][j] = (g[i][j] +
f[i - k * t][j - t] * com(f[k][d - 1] + t - 1, t) % B) %
B;
}
}
long long ans = dp[n0 & 1][n][d];
if (n + 1 & 1) ans = (ans + com(dp[n0 & 1][n >> 1][d - 1] + 1, 2)) % B;
printf("%d\n", (int)ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 322;
const long long LINF = 2e18 + 474;
const int MAXN = 1e5 + 228;
const int MOD = 1e9 + 7;
const double eps = 1e-14;
int n, k;
int a[MAXN], l[MAXN];
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
l[1] = 1;
for (int i = 2; i <= n; i++) {
int j = a[i];
if (j == 0) {
l[i] = min(i, k + 1);
} else
l[i] = l[j] + min(i - j - 1, 2 * k) + 1;
}
for (int i = 1; i <= n; i++) {
cout << l[i] + min(k, n - i) << " ";
}
}
int main() {
if (!1) {
freopen(
"474"
".in",
"r", stdin);
freopen(
"474"
".out",
"w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
while (T--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
string str;
int main() {
while (cin >> n >> k) {
cin >> str;
int s = 0, sum = 0;
int ans = 0;
for (int i = 0; i < n; ++i) {
if (str[i] == 'b') sum++;
while (sum > k)
if (str[s++] == 'b') sum--;
ans = max(ans, i - s + 1);
}
s = 0;
sum = 0;
for (int i = 0; i < n; ++i) {
if (str[i] == 'a') sum++;
while (sum > k)
if (str[s++] == 'a') sum--;
ans = max(ans, i - s + 1);
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> G[100];
pair<int, int> v[100];
bool visited[100];
void dfs(int u) {
visited[u] = true;
for (int v = 0; v < G[u].size(); v++) {
if (!visited[G[u][v]]) dfs(G[u][v]);
}
}
int main() {
int N, cnt = 0;
cin >> N;
for (int i = 0; i < N; i++) cin >> v[i].first >> v[i].second;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (v[i].first == v[j].first || v[i].second == v[j].second) {
G[i].push_back(j);
G[j].push_back(i);
}
}
}
for (int i = 0; i < N; i++) {
if (!visited[i]) {
cnt++;
dfs(i);
}
}
cout << cnt - 1 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n, k;
int i, j, temp;
scanf("%d %d", &n, &k);
if (2 * k >= n) {
printf("-1");
return 0;
}
printf("%d\n", n * k);
for (i = 1; i <= n; i++) {
for (j = i + 1; j <= k + i; j++) {
temp = (j > n) ? (j % n) : j;
printf("%d %d\n", i, temp);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000111000;
int main() {
int a, b, c, d, e;
char x[100][10], s;
scanf("%d%c", &a, &s);
for (int b = 0, _d = (int)a - 1; b <= _d; ++b) {
d = 0, e = 0;
scanf("%[^\n]", &x[b][0]);
scanf("%c", &s);
c = 0;
while (x[b][c] != '\0') c++;
if (x[b][0] == 'm' && x[b][1] == 'i' && x[b][2] == 'a' && x[b][3] == 'o' &&
x[b][4] == '.')
d = 1;
if (x[b][c - 5] == 'l' && x[b][c - 4] == 'a' && x[b][c - 3] == 'l' &&
x[b][c - 2] == 'a' && x[b][c - 1] == '.')
e = 1;
if (e == 1) {
if (d == 1)
printf("OMG>.< I don't know!\n");
else
printf("Freda's\n");
} else if (d == 1)
printf("Rainbow's\n");
else
printf("OMG>.< I don't know!\n");
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<ll> a(n), b(m), v(35);
for (int i = 0; i < n; i++) {
cin >> a[i];
for (ll j = 0; j < 35; j++)
if ((1LL << j) & a[i]) v[j]++;
}
for (int j = 0; j < m; j++) cin >> b[j];
sort(b.begin(), b.end());
int ans = 0;
for (int i = 0; i < m; i++) {
if (v[b[i]]) {
ans++;
v[b[i]]--;
} else {
int flag = 0, j;
for (j = b[i] + 1; j < 35; j++) {
if (v[j]) {
flag = 1;
break;
}
}
if (!flag) break;
ans++;
v[j]--;
j--;
while (j >= b[i]) {
v[j]++;
j--;
}
}
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
long long mod = 1e9 + 7;
long long inf = 1e18 + 3;
using namespace std;
long long powerexp(long long x, long long y, long long m = 1e9 + 7) {
if (y == 0) return 1;
long long p = powerexp(x, y / 2, mod) % mod;
p = (p * p) % mod;
return (y % 2 == 0) ? p : (x * p) % mod;
}
long long modinverse(long long a, long long m = 1e9 + 7) {
return powerexp(a, mod - 2, mod);
}
long long vector_upper_bound(vector<long long>& a, long long n, long long x) {
long long l = 0;
long long h = n;
while (l < h) {
long long mid = l + (h - l) / 2;
if (x >= a[mid]) {
l = mid + 1;
} else {
h = mid;
}
}
return l;
}
long long vector_lower_bound(vector<long long>& a, long long n, long long x) {
long long l = 0;
long long h = n;
while (l < h) {
long long mid = l + (h - l) / 2;
if (x <= a[mid]) {
h = mid;
} else {
l = mid + 1;
}
}
return l;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
t = 1;
while (t--) {
long long n;
cin >> n;
vector<long long> a(n), cnt(6, 0);
map<long long, long long> mapping;
mapping[4] = 0;
mapping[8] = 1;
mapping[15] = 2;
mapping[16] = 3;
mapping[23] = 4;
mapping[42] = 5;
for (long long i = (0); i < (n); i += (1)) {
cin >> a[i];
if (a[i] == 4) {
cnt[mapping[a[i]]]++;
} else {
if (cnt[mapping[a[i]] - 1]) {
cnt[mapping[a[i]] - 1]--;
cnt[mapping[a[i]]]++;
}
}
}
cout << (n - (6 * cnt[5])) << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100010;
const int inf = 2147483647;
int read() {
int x = 0, 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 ^ 48), ch = getchar();
return x * f;
}
int n, l, a[Maxn];
void print(int x) {
if (!x) return;
if (x == 2) {
printf("2 1 ");
return;
}
l = 0;
int t = x;
while (t) ++l, t >>= 1;
int tmp = ((1 << l - 1) - 1 << 1) ^ x;
print(tmp);
for (int i = x; i > tmp; i--) printf("%d ", i);
}
int main() {
n = read();
if (n & 1)
puts("NO");
else
puts("YES"), print(n), puts("");
if (n < 6) return puts("NO"), 0;
int flag = 0;
int t = n;
while (t) {
if (t & 1) flag++;
t >>= 1;
}
if (flag == 1) return puts("NO"), 0;
puts("YES");
memset(a, 0, sizeof(a));
if (n & 1) {
a[1] = 5, a[5] = 7, a[7] = 1, a[4] = 6, a[6] = 4;
for (int i = 1; i <= n; i++) {
if (a[i])
printf("%d ", a[i]);
else {
if (i & 1)
printf("%d ", i - 1);
else
printf("%d ", i + 1);
}
}
return 0;
}
if (n % 4 == 2) {
a[1] = 3, a[3] = 1, a[2] = n, a[n] = 2;
for (int i = 1; i <= n; i++) {
if (a[i])
printf("%d ", a[i]);
else {
if (i & 1)
printf("%d ", i - 1);
else
printf("%d ", i + 1);
}
}
return 0;
}
if (n % 4 == 0) {
a[n] = n - 1, a[n - 1] = n, a[n - 3] = n - 4, a[n - 4] = n - 3,
a[2] = n - 2, a[n - 2] = 2, a[1] = 3, a[3] = 1;
for (int i = 1; i <= n; i++) {
if (a[i])
printf("%d ", a[i]);
else {
if (i & 1)
printf("%d ", i - 1);
else
printf("%d ", i + 1);
}
}
return 0;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
set<string> out;
string in;
set<pair<int, string> > visited;
void DP(int pos, string prev) {
if (pos <= 5) return;
pair<int, string> tmp = make_pair(pos, prev);
if (visited.find(tmp) != visited.end()) return;
visited.insert(tmp);
string s = "";
if (pos >= 6) {
s = in.substr(pos - 1, 2);
if (s != prev) {
out.insert(s);
DP(pos - 2, s);
}
if (pos >= 7) {
s = in[pos - 2] + s;
if (s != prev) {
out.insert(s);
DP(pos - 3, s);
}
}
}
}
int main() {
cin >> in;
DP(in.size() - 1, "");
cout << out.size() << "\n";
for (auto it = out.begin(); it != out.end(); ++it) cout << (*it) << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma warning(disable : 4996)
using namespace std;
const int MAXN = 1000001;
const int inf = 1000000001;
const long double INF = 1.0 / 0.0;
const long double EPS = 1e-7;
const int mod = inf + 6;
const long double PI = acos(-1.0);
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.second == b.second) {
return a.first > b.first;
}
return a.second > b.second;
}
signed main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
;
int n;
cin >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
}
sort((a).begin(), (a).end(), cmp);
int ans = 0;
int cnt = 1;
int nw = 0;
while (cnt && nw < n) {
ans += a[nw].first;
cnt += a[nw].second;
cnt--;
nw++;
}
cout << ans;
}
| 2 |
#include <iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
if(n%2)cout<<(n+1)/2*n+1<<endl;
else cout<<n/2*(n+1)+1<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
const long long mod = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int ask(int x1, int y1, int x2, int y2) {
if (x1 < 1 || y1 < 1 || x2 < 1 || y2 < 1) return 0;
cout << "? " << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
int x;
cin >> x;
return x;
}
pair<pair<int, int>, pair<int, int> > get(int x1, int y1, int x2, int y2) {
int l = x1, r = x2;
while (l < r) {
int m = (l + r) / 2;
if (ask(m + 1, y1, x2, y2) > 0)
l = m + 1;
else
r = m;
}
pair<int, int> lu = {l, 0};
pair<int, int> rd = {0, 0};
l = y1, r = y2;
while (l < r) {
int m = (l + r) / 2;
if (ask(lu.first, m + 1, x2, y2) > 0)
l = m + 1;
else
r = m;
}
lu.second = l;
l = lu.first, r = x2;
while (l < r) {
int m = (l + r) / 2;
if (ask(lu.first, lu.second, m, y2) > 0)
r = m;
else
l = m + 1;
}
rd.first = l;
l = lu.second, r = y2;
while (l < r) {
int m = (l + r) / 2;
if (ask(lu.first, lu.second, rd.first, m) > 0)
r = m;
else
l = m + 1;
}
rd.second = l;
return make_pair(lu, rd);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
pair<pair<int, int>, pair<int, int> > x = get(1, 1, n, n);
pair<pair<int, int>, pair<int, int> > y;
if (ask(1, 1, x.first.first - 1, n)) {
y = get(1, 1, x.first.first - 1, n);
} else {
if (ask(1, 1, n, x.first.second - 1)) {
y = get(1, 1, n, x.first.second - 1);
} else {
y = get(1, x.second.second + 1, n, n);
}
}
cout << "! " << x.first.first << " " << x.first.second << " "
<< x.second.first << " " << x.second.second << " " << y.first.first
<< " " << y.first.second << " " << y.second.first << " "
<< y.second.second << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string s;
int n, open = 0, closed = 0;
vector<int> res;
int main() {
cin >> s;
n = s.length();
for (int i = 0; i < n; i++) {
if (s[i] == '(')
open++;
else if (s[i] == ')')
closed++, open--;
else if (s[i] == '#') {
res.push_back(1);
open--;
}
if (open < 0) {
puts("-1");
return 0;
}
}
res[res.size() - 1] += open;
int ind;
open = 0, closed = 0;
for (int i = n - 1; i >= 0; i--)
if (s[i] == '#') {
ind = i;
break;
}
for (int i = ind + 1; i < n; i++)
if (s[i] == '(') {
ind = i;
break;
}
for (int i = ind; i < n; i++) {
if (s[i] == '(') open++;
if (s[i] == ')') closed++;
}
if (open > closed) {
puts("-1");
return 0;
}
for (int i = 0; i < res.size(); i++) printf("%d\n", res[i]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
long long a, k, n[1000], m[1000], count = 0;
set<long long> s;
cin >> a >> k;
for (int i = 0; i <= 1000; i++) {
m[i] = 0;
}
for (int i = 0; i < a; i++) {
cin >> n[i];
m[n[i]]++;
s.insert(n[i]);
}
sort(m, m + 1000);
long long h = m[1000 - 1];
for (;; h++) {
if (h % k == 0) {
break;
}
}
for (int i = 1000 - s.size(); i < 1000; i++) {
count += abs(m[i] - h);
}
cout << count;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, M; cin >> A >> B >> M;
vector<int> a(A), b(B);
int min_a = 1e9, min_b = 1e9;
for (int &v:a) cin >> v, min_a = min(min_a, v);
for (int &v:b) cin >> v, min_b = min(min_b, v);
int mi = min_a + min_b;
while (M--) {
int x, y, c; cin >> x >> y >> c; --x; --y;
mi = min(mi, a[x] + b[y] - c);
}
cout << mi << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 10, mod = 199999;
int n, p[M], de[M], fa[M];
vector<int> son[M], ans;
void dfs(int x) {
cout << x << ' ';
if (son[x].empty()) return;
int mx = n;
for (auto v : son[x]) {
if (de[v] > de[mx]) mx = v;
}
int pre = 0;
for (auto v : son[x]) {
if (v == mx) continue;
dfs(v);
while (pre--) {
ans.push_back(v);
}
pre = de[v];
}
while (pre--) {
ans.push_back(mx);
}
dfs(mx);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int x;
cin >> x;
son[x].push_back(i);
de[i] = 1;
fa[i] = x;
}
for (int i = n - 1; ~i; --i) {
de[fa[i]] = max(de[fa[i]], de[i] + 1);
}
dfs(0);
cout << '\n';
cout << ans.size() << '\n';
for (auto x : ans) cout << x << " \n"[x == ans.back()];
}
| 6 |
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int n,t,x,h;
double ans=0.0;
cin>>n>>t;
while(n--){
cin>>x>>h;
ans=max(ans,(double)t*(double)h/(double)x);
}
printf("%.6lf\n",ans);
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int P, Q, R;
cin>>P>>Q>>R;
cout<<min(P + Q, min(Q + R, R + P));
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, e, f;
scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
if (a * c * e < b * d * f) {
cout << "Ron\n";
} else if (!c && d) {
cout << "Ron\n";
} else if (!a && b && d) {
cout << "Ron\n";
} else
cout << "Hermione\n";
return 0;
}
| 1 |
#include <iostream>
using namespace std;
int main(){
int n;
cin >>n;
for (int i=0;i<=25;i++){
for (int j=0;j<=25;j++){
if (4*i+7*j==n){
cout <<"Yes";
return 0;
}
}
}
cout <<"No";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int ara[2005];
int ara2[2005];
for (int i = 0; i < n; i++) {
cin >> ara[i];
ara2[i] = ara[i];
}
sort(ara2, ara2 + n, greater<int>());
int ara3[2005];
ara3[ara2[0]] = 1;
int k = 1;
for (int i = 1; i < n; i++) {
k++;
if (ara2[i] == ara2[i - 1]) continue;
ara3[ara2[i]] = k;
}
for (int i = 0; i < n; i++) {
cout << ara3[ara[i]] << " ";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, d, t, tem1, tem2, tem3, tem4, y = 1, sum = 0, ans = 0;
string s, c;
map<int, int> p;
int arr[30000 + 10];
int maxi = -1;
int dp[30000 + 10][500 + 10];
int rec(int index, int st) {
if (index > maxi) return 0;
int der = p[st];
if (dp[index][der] != -1) return dp[index][der];
int e = -1e9, r = -1e9, w = -1e9;
if (st != 1) e = arr[index] + rec(index + st - 1, st - 1);
r = arr[index] + rec(index + st, st);
w = arr[index] + rec(index + st + 1, st + 1);
return dp[index][der] = max(r, max(e, w));
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> tem1;
maxi = max(maxi, tem1);
arr[tem1]++;
}
int r2m = 1;
for (int i = max(1, d - 250); i <= d; i++) {
p[i] = r2m++;
}
for (int i = d + 1; i < d + 250; i++) {
p[i] = r2m++;
}
cout << rec(d, d);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char in[120];
pair<pair<string,int>,int> p[110];
int main(){
int a;scanf("%d",&a);
for(int i=0;i<a;i++){
int t;scanf("%s%d",in,&t);
string tmp=in;
p[i]=make_pair(make_pair(in,-t),i);
}
std::sort(p,p+a);
for(int i=0;i<a;i++)printf("%d\n",p[i].second+1);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e15 + 7;
const long long N = 1e6;
long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long fpow(long long n, long long k, long long p = MOD) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
long long inv(long long a, long long p = MOD) { return fpow(a, p - 2, p); }
long long mult(long long a, long long b, long long p = MOD) {
return (long long)((a % p) * (b % p)) % p;
}
vector<long long> prime(N + 1);
void sieve() {
prime[0] = prime[1] = 1;
for (long long i = 2; i <= N; i++) {
if (!prime[i]) {
prime[i] = i;
for (long long j = i * i; j <= N; j += i)
if (!prime[j]) prime[j] = i;
}
}
}
long long n, q, t;
long long tt(long long t) {
if (t & 1)
return (t >> 1) + 1;
else
tt(t + n - (t >> 1));
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
clock_t tStart = clock();
cin >> n >> q;
while (q--) {
cin >> t;
cout << tt(t) << "\n";
}
cerr << "Time Elapsed : " << fixed << setprecision(10)
<< (double)(clock() - tStart) / CLOCKS_PER_SEC << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string str, pal;
cin >> str;
vector<int> freq;
freq.push_back(1);
pal = str[0];
for (int i = 1; i < str.size(); i++) {
if (str[i] != str[i - 1]) {
freq.push_back(1);
pal += str[i];
} else
freq[freq.size() - 1]++;
}
str = pal;
reverse(pal.begin(), pal.end());
if (pal == str) {
bool possible = true;
for (int i = 0; i < freq.size() / 2; i++) {
if (freq[i] + freq[freq.size() - 1 - i] < 3) {
possible = false;
break;
}
}
if (possible) {
if (freq[freq.size() / 2] > 1)
cout << freq[freq.size() / 2] + 1 << '\n';
else
cout << 0 << '\n';
} else
cout << 0 << '\n';
} else
cout << 0 << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
for (int i = 0; i < s.size(); i++)
if (s[i] == 'r') cout << i + 1 << "\n";
for (int i = s.size() - 1; i > -1; i--)
if (s[i] == 'l') cout << i + 1 << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long get(long long x, long long len, int gg) {
if (gg) {
if ((x & 1) == gg)
return x / 2 + 1;
else {
long long pos = x / 2;
return get(pos, len / 2, gg ^ (len & 1)) + (len + 1) / 2;
}
} else {
if ((x & 1) == gg)
return x / 2;
else {
long long pos = x / 2 + 1;
return get(pos, (len + 1) / 2, gg ^ (len & 1)) + len / 2;
}
}
}
int main() {
int q;
scanf("%lld%d", &n, &q);
for (int o = 1; o <= q; ++o) {
long long x;
scanf("%lld", &x);
printf("%lld\n", get(x, n, 1));
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[111111];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0, x = -1;
for (int i = 0; i < n; i++)
if (a[i] == 1) {
if (x != -1) {
if (i - x <= 2)
ans = ans + i - x;
else
ans = ans + 2;
x = i;
} else {
ans++;
x = i;
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
double a[100005], ans;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, p;
cin >> n >> p;
for (long long int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
a[i] = 1.0 - (r / p - (l - 1) / p) * 1.0 / (r - l + 1) * 1.0;
}
for (long long int i = 0; i < n; i++) {
ans += (1.0 - 1.0 * a[i] * a[(i + 1) % n] * 1.0) * 2000.0;
}
cout << fixed << setprecision(12) << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long joy = 0;
long long a[100000 + 7], b[100000 + 7];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++)
if (b[i] > 2 * a[i] || b[i] == 1)
joy--;
else {
if (b[i] % 2 == 0)
joy = joy + b[i] * b[i] / 4;
else
joy = joy + (b[i] / 2) * (b[i] / 2 + 1);
}
cout << joy;
}
| 2 |
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
const double g = 9.8;
const double EPS = 1e-8;
namespace solver{
int n;
double v, gx, gy;
double x1[50], x2[50], y1[50], y2[50];
double calc_y(double vx, double vy, double x){
double t = x / vx;
return vy * t - g * t * t / 2;
}
int sign(double x){
if(x > EPS) return 1;
if(x < -EPS) return -1;
return 0;
}
int comp(double lb, double ub, double x){
if(x < lb + EPS) return -1;
if(x > ub - EPS) return 1;
return 0;
}
bool check(double bx, double by){
double a = g * g / 4.0;
double b = g * by - v * v;
double c = bx * bx + by * by;
double D = b * b - 4 * a * c;
//printf("D:%f\n", D);
if(D < 0) return false;
for(int s = -1; s <= 1; s += 2){
double tt = (-b + s * sqrt(D)) / (2 * a);
//printf("tt:%f\n", tt);
if(tt < 0) continue;
double t = sqrt(tt);
double vx = bx / t;
double vy = (by + g * t * t / 2) / t;
assert(sign(vx * vx + vy * vy - v * v) == 0);
double fh = calc_y(vx, vy, gx);
if(sign(fh - gy) < 0) continue;
bool ok = true;
for(int i = 0; i < n; i++){
if(x1[i] >= gx) continue;
if(comp(x1[i], x2[i], gx) == 0 && comp(y1[i], y2[i], gy) <= 0 && comp(y1[i], y2[i], fh) >= 0) {
ok = false;
}
int yl = comp(y1[i], y2[i], calc_y(vx, vy, x1[i]));
int yr = comp(y1[i], y2[i], calc_y(vx, vy, x2[i]));
int xh = comp(x1[i], x2[i], vx * vy / g);
int yh = comp(y1[i], y2[i], calc_y(vx, vy, vx * vy / g));
if(xh == 0 && yh >= 0 && yl <= 0) ok = false;
if(yl * yr <= 0) ok = false;
}
if(ok) return true;
}
return false;
}
bool solve(){
cin >> n;
cin >> v >> gx >> gy;
REP(i, n) cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
REP(i, n) x2[i] = min(x2[i], gx);
bool ans = false;
ans |= check(gx, gy);
REP(i, n){
ans |= check(x1[i], y2[i]);
ans |= check(x2[i], y2[i]);
}
return ans;
}
}
int main(){
cout << (solver::solve() ? "Yes" : "No") << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
void sync_stdio() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
struct Sync_stdio {
Sync_stdio() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
} _sync_stdio;
struct FAIL {
FAIL() {
cout << "CHANGE!!!"
<< "\n";
}
};
vector<vector<int>> cl;
int c = 0;
vector<string> v;
void dfs(int x, int y) {
cl[x][y] = c;
for (int i = (-1); i < (2); ++i) {
for (int j = (-1); j < (2); ++j) {
if (abs(i) + abs(j) != 1) {
continue;
}
if (v[x + i][y + j] == 'X') {
continue;
}
if (cl[x + i][y + j] != -1) {
continue;
}
dfs(x + i, y + j);
}
}
}
int main() {
int n, k;
cin >> n >> k;
v.assign(n + 2, string(n + 2, 'X'));
for (int i = (1); i < (n + 1); ++i) {
cin >> v[i];
v[i] = 'X' + v[i] + 'X';
}
cl.assign(n + 2, vector<int>(n + 2, -1));
for (int i = (0); i < (n + 2); ++i) {
for (int j = (0); j < (n + 2); ++j) {
if (v[i][j] == '.' && cl[i][j] == -1) {
dfs(i, j);
++c;
}
}
}
vector<int> cnt(n * n);
for (int i = (0); i < (n + 2); ++i) {
for (int j = (0); j < (n + 2); ++j) {
if (v[i][j] == 'X') {
continue;
}
++cnt[cl[i][j]];
}
}
int res = 0;
int t;
int allcnt = k * k;
vector<int> used(n * n);
for (int l = (1); l < (n - k + 2); ++l) {
used.assign(n * n, 0);
int tres = 0;
int tcnt = 0;
for (int i = (-1); i < (k + 1); ++i) {
for (int j = (0); j < (k + 2); ++j) {
if ((i == -1 || i == k) && (j == 0 || j == k + 1)) {
continue;
}
if (v[l + i][j] == 'X') {
continue;
}
if (i != -1 && i != k && j != 0 && j != k + 1) {
++tcnt;
}
++used[cl[l + i][j]];
if (used[cl[l + i][j]] == 1) {
tres += cnt[cl[l + i][j]];
}
}
}
res = max(res, tres + allcnt - tcnt);
for (int j = (k + 1); j < (n + 1); ++j) {
for (int i = (-1); i < (k + 1); ++i) {
t = j - k;
if (i != -1 && i != k) {
--t;
}
if (v[l + i][t] == 'X') {
continue;
}
--used[cl[l + i][t]];
if (used[cl[l + i][t]] == 0) {
tres -= cnt[cl[l + i][t]];
}
}
for (int i = (-1); i < (k + 1); ++i) {
t = j;
if (i != -1 && i != k) {
++t;
}
if (v[l + i][t] == 'X') {
continue;
}
++used[cl[l + i][t]];
if (used[cl[l + i][t]] == 1) {
tres += cnt[cl[l + i][t]];
}
}
for (int i = (0); i < (k); ++i) {
if (v[l + i][j] != 'X') {
++tcnt;
}
if (v[l + i][j - k] != 'X') {
--tcnt;
}
}
res = max(res, tres + allcnt - tcnt);
}
}
cout << res;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, s, t, front = 0, rev = 0, sub, sum = 0;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> d;
v.push_back(d);
sum += v[i];
}
cin >> s >> t;
if (s == t)
cout << 0 << endl;
else {
if (s < t)
for (int i = s - 1; i < t - 1; i++) {
front += v[i];
}
else if (t < s)
for (int i = t - 1; i < s - 1; i++) {
front += v[i];
}
rev = sum - front;
if (rev <= front)
cout << rev << endl;
else if (front < rev)
cout << front << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, a[1001];
char b[1001];
cin >> n;
if (n == 1) a[0] = 1;
if (n >= 2) {
a[0] = 1;
a[1] = 1;
}
for (j = 0; j <= n; j++) b[j] = 'o';
for (i = 2; i <= 16; i++) {
a[i] = a[i - 2] + a[i - 1];
}
for (j = 1; j <= 16; j++) {
b[a[j]] = 'O';
}
for (j = 1; j <= n; j++) cout << b[j];
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n, r, sum = 0;
cin >> n >> r;
for (int j = 0; j < n - 1; j++) {
int temp;
cin >> temp;
sum += temp;
}
int ret = (r - sum) % n;
while (ret <= 0) ret += n;
cout << ret << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline long long lin() {
long long x;
scanf("%lld", &x);
return x;
}
inline string get() {
char ch[2000010];
scanf("%s", ch);
return ch;
}
inline void read(long long *a, long long n) {
for (long long i = 0; i < n; i++) a[i] = in();
}
template <class blank>
inline void out(blank x) {
cout << x << "\n";
exit(0);
}
template <class blank>
inline void smin(blank &a, blank b) {
a = min(a, b);
}
template <class blank>
inline void smax(blank &a, blank b) {
a = max(a, b);
}
const long long maxn = 2e5 + 10;
const long long maxm = 1e6 + 10;
const long long maxlg = 21;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 10;
const long long SQ = 317;
long long n, q, a[maxn], b[maxn], id[maxn];
vector<pair<long long, long long> > events[4 * maxn];
inline long long val(long long id, long long t) { return a[id] + t * b[id]; }
inline long long where(long long i, long long j) {
if (b[i] >= b[j]) return inf;
long long ret = (a[i] - a[j]) / (b[j] - b[i]);
if (val(i, ret) > val(j, ret)) ret++;
return ret;
}
inline void build(long long p = 0, long long l = 0, long long r = n - 1) {
iota(id + l, id + r + 1, l);
sort(id + l, id + r + 1, [](const long long &f, const long long &s) {
return val(f, -maxn) < val(s, -maxn);
});
vector<pair<long long, long long> > &ptr = events[p];
ptr.push_back({-inf, id[l]});
for (long long i = l + 1; i <= r; i++) {
long long id = ::id[i];
while (ptr.size()) {
long long hit = where(id, ptr.back().second);
if (hit < ptr.back().first) {
ptr.pop_back();
} else
break;
}
long long hit = where(id, ptr.back().second);
if (hit != inf) {
ptr.push_back({hit, id});
}
}
if (l == r) return;
long long mid = (l + r) >> 1;
build(2 * p + 1, l, mid);
build(2 * p + 2, mid + 1, r);
}
inline long long relax(long long i, long long j, long long t) {
if (val(i, t) >= val(j, t)) return i;
return j;
}
inline long long get(long long ql, long long qr, long long t, long long p = 0,
long long l = 0, long long r = n - 1) {
if (l >= ql && r <= qr) {
auto pt =
lower_bound(events[p].begin(), events[p].end(), make_pair(t, inf));
if (pt == events[p].begin()) return val(pt->second, t);
return val((--pt)->second, t);
}
long long mid = (l + r) >> 1;
if (qr <= mid) return get(ql, qr, t, 2 * p + 1, l, mid);
if (ql > mid) return get(ql, qr, t, 2 * p + 2, mid + 1, r);
return min(get(ql, qr, t, 2 * p + 1, l, mid),
get(ql, qr, t, 2 * p + 2, mid + 1, r));
}
long long pref[maxn];
int32_t main() {
n = in();
read(a, n);
q = in();
for (long long i = 0; i < n; i++) {
pref[i] = a[i] + (i ? pref[i - 1] : 0);
b[i] = a[i];
a[i] = -pref[i] + a[i] * (i + 1);
}
build();
while (q--) {
long long i = in(), j = in() - 1;
cout << get(j - i + 1, j, i - j - 1) + pref[j] << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int count;
array<Node*, 26> children, transitions;
Node* pi;
void insert(string::iterator c) {
if (*c == 0)
count++;
else {
auto& child = children[*c - 'a'];
if (child == nullptr) child = new Node{};
child->insert(next(c));
}
}
};
struct Trie {
vector<string> words;
Node* root;
Trie() : root(new Node()) {}
bool is_empty() const { return words.empty(); }
void insert(string s) {
root->insert(begin(s));
words.push_back(move(s));
}
void aho_corasick() {
auto queue = std::queue<Node*>();
queue.push(root);
while (!queue.empty()) {
auto node = queue.front();
queue.pop();
for (auto c = 0; c < 26; c++) {
auto child = node->children[c];
auto trailing = node == root ? root : node->pi->transitions[c];
if (child != nullptr) {
node->transitions[c] = child;
child->pi = trailing;
child->count += trailing->count;
queue.push(child);
} else
node->transitions[c] = trailing;
}
}
}
int query(string::iterator c) const {
auto result = 0;
auto node = root;
for (; *c != 0; c++) {
node = node->transitions[*c - 'a'];
result += node->count;
}
return result;
}
};
struct DynamicTrie {
vector<Trie> tries;
void insert(string s) {
auto current = Trie();
current.insert(s);
for (auto& trie : tries) {
if (trie.is_empty()) {
current.aho_corasick();
trie = move(current);
return;
} else {
for (const auto& s : trie.words) current.insert(s);
trie = Trie();
}
}
current.aho_corasick();
tries.push_back(move(current));
}
int query(string::iterator c) const {
auto result = 0;
for (const auto& trie : tries)
if (!trie.is_empty()) result += trie.query(c);
return result;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
auto added_words = DynamicTrie();
auto removed_words = DynamicTrie();
int m;
cin >> m;
for (auto i = 0; i < m; i++) {
int t;
cin >> t;
string s;
cin >> s;
if (t == 1) added_words.insert(s);
if (t == 2) removed_words.insert(s);
if (t == 3)
cout << added_words.query(begin(s)) - removed_words.query(begin(s))
<< endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, c;
int main() {
int q;
cin >> q;
while (q--) {
cin >> a >> b >> c;
cout << (a + b + c) / 2 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s = "";
int cnt = 0;
for (int i = 0; i < n - 2; i++) {
string x;
cin >> x;
int k = s.length();
if (cnt < n - 4 && s[k - 1] == x[0]) {
s += x[1];
cnt++;
} else
s += x;
}
if (s.length() == 2)
cout << "b" << s << endl;
else
cout << s << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<string> adj[201];
int visited[201];
vector<string> v;
int dfs(int node, int dist) {
visited[node] = 1;
int m = dist;
for (int child = 0; child < adj[node].size(); child++) {
string temp = adj[node][child];
std::vector<string>::iterator it;
it = std::find(v.begin(), v.end(), temp);
int temp2 = dfs(it - v.begin(), dist + 1);
if (temp2 > m) {
m = temp2;
}
}
return m;
}
int main() {
int n;
cin >> n;
v.push_back("POLYCARP");
for (int i = 0; i < n; i++) {
string str1, str2, str3;
cin >> str1 >> str2 >> str3;
transform(str1.begin(), str1.end(), str1.begin(), ::toupper);
transform(str3.begin(), str3.end(), str3.begin(), ::toupper);
v.push_back(str1);
std::vector<string>::iterator it;
it = std::find(v.begin(), v.end(), str3);
adj[it - v.begin()].push_back(str1);
}
fill(visited, visited + n, 0);
cout << dfs(0, 1);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long pw(long long n, long long p) {
n %= 1000000007;
if (!p) return 1;
long long ans = (n * n) % 1000000007;
if (p % 2)
return (n * ((pw(ans, p / 2)) % 1000000007)) % 1000000007;
else
return pw(n * n, p / 2) % 1000000007;
}
int main() {
long long n;
cin >> n;
if (!n) {
cout << 1;
return 0;
}
long long ans = pw(2, n - 1) % 1000000007;
cout << (ans * (1 + 2 * ans)) % 1000000007;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char str[100005];
int n, k;
struct SAM {
int next[100005 * 2][26], fa[100005 * 2], len[100005 * 2];
int last, cnt;
int cntA[100005 * 2], A[100005 * 2];
long long num[100005 * 2], sz[100005 * 2];
void clear() {
last = cnt = 1;
fa[1] = len[1] = 0;
memset(next[1], 0, sizeof(next[1]));
}
void init(char *s) {
while (*s) {
Insert(*s - 'a');
s++;
}
}
void Insert(int c) {
int p = last;
int np = ++cnt;
memset(next[cnt], 0, sizeof next[cnt]);
len[np] = len[p] + 1;
last = np;
while (p && !next[p][c]) next[p][c] = np, p = fa[p];
if (!p)
fa[np] = 1;
else {
int q = next[p][c];
if (len[q] == len[p] + 1)
fa[np] = q;
else {
int nq = ++cnt;
len[nq] = len[p] + 1;
memcpy(next[nq], next[q], sizeof(next[q]));
fa[nq] = fa[q];
fa[np] = fa[q] = nq;
while (next[p][c] == q) next[p][c] = nq, p = fa[p];
}
}
}
void build() {
memset(cntA, 0, sizeof cntA);
memset(num, 0, sizeof num);
for (int i = 1; i <= cnt; i++) cntA[len[i]]++;
for (int i = 1; i <= n; i++) cntA[i] += cntA[i - 1];
for (int i = cnt; i >= 1; i--) A[cntA[len[i]]--] = i;
int tmp = 1;
for (int i = 0; i < n; i++) {
num[tmp = next[tmp][str[i] - 'a']] = 1;
}
for (int i = cnt; i >= 1; i--) {
int x = A[i];
num[fa[x]] += num[x];
}
num[1] = 0;
for (int i = cnt; i >= 1; i--) {
int x = A[i];
sz[x] = num[x];
for (int j = 0; j < 26; j++) {
if (!next[x][j]) continue;
sz[x] += sz[next[x][j]];
}
}
}
void solve(int k) {
build();
int now = 1, pos = 0;
while (k > 0) {
pos = 0;
while (pos < 26 && k > sz[next[now][pos]]) {
k -= sz[next[now][pos]];
pos++;
}
if (pos == 26) {
puts("No such line.");
return;
}
printf("%c", pos + 'a');
now = next[now][pos];
k -= num[now];
}
}
} sam;
int main() {
scanf("%s%d", str, &k);
n = strlen(str);
sam.clear();
sam.init(str);
sam.solve(k);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf64 = 1ll * inf * inf;
const int X = 1e6 + 5;
int prime[X];
void precalc() {
fill(prime, prime + X, 1);
prime[0] = prime[1] = 0;
for (int i = 2; i * i < X; i++) {
if (!prime[i]) continue;
for (int j = i * i; j < X; j += i) {
prime[j] = 0;
}
}
}
int n, m;
int main() {
precalc();
scanf("%d %d", &n, &m);
int W = n - 1;
while (!prime[W]) W++;
printf("%d %d\n", W, W);
for (int i = 1; i + 1 < n; i++) {
printf("%d %d %d\n", i, i + 1, 1);
}
printf("%d %d %d\n", n - 1, n, W - n + 2);
m -= (n - 1);
for (int i = 1; i <= n && m > 0; i++) {
for (int j = i + 2; j <= n && m > 0; j++) {
printf("%d %d %d\n", i, j, W + 1);
m--;
}
}
return 0;
}
| 3 |
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
double xa1,ya1,xa2,ya2,xb1,yb1,xb2,yb2;
while(cin >> xa1 >> ya1 >> xa2 >> ya2 >> xb1 >> yb1 >> xb2 >> yb2){
if (((xb1 <= xa1 && xa1 <= xb2) || (xa1 <= xb1 && xb1 <= xa2)) && ((yb1 <= ya1 && ya1 <= yb2) || (ya1 <= yb1 && yb1 <= ya2))) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long int tot;
long long int maxx;
long long int ans;
long long int a[2050];
void dfs(long long int i, long long int curr) {
long long int l, r;
l = 2 * i;
r = l + 1;
if (maxx < curr) {
maxx = curr;
}
if (l < tot) {
dfs(l, curr + a[l]);
dfs(r, curr + a[r]);
}
}
long long int dfs1(long long int i, long long int curr) {
long long int l, r;
l = 2 * i;
r = l + 1;
if (l >= tot) {
return maxx - curr;
} else {
long long int ll = dfs1(l, curr + a[l]);
long long int rr = dfs1(r, curr + a[r]);
long long int diff = abs(ll - rr);
long long int d = max((ll - diff), (rr - diff));
ans += (diff);
return d;
}
}
int main() {
long long int n, i, j, k;
cin >> n;
tot = (long long int)pow((double)2, (double)(1 + n));
for (i = 2; i < tot; i++) {
cin >> a[i];
}
maxx = 0;
ans = 0;
dfs(1, 0);
j = dfs1(1, 0);
ans += j;
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
int read() {
static int c, x;
while ((c = getchar()) < 48) {
}
x = c & 15;
while ((c = getchar()) >= 48) x = x * 10 + (c & 15);
return x;
}
struct edge {
int next, to;
} e[100010 << 1];
int ans = 0;
int head[100010], deg[100010], f0[100010], f1[100010];
inline void add(const int &x, const int &y) {
static int ei;
e[++ei].next = head[x];
e[ei].to = y;
head[x] = ei;
++deg[x];
}
void dfs(const int &x, const int &fa) {
f1[x] = 1;
f0[x] = deg[x] - 1;
for (int i = head[x]; i; i = e[i].next) {
const int y = e[i].to;
if (y != fa) {
dfs(y, x);
ans = std::max(ans, f0[x] + std::max(f0[y], f1[y]));
ans = std::max(ans, f1[x] + f0[y]);
f0[x] = std::max(f0[x], std::max(f0[y], f1[y]) + deg[x] - 2);
f1[x] = std::max(f1[x], f0[y] + 1);
}
}
}
int main() {
const int n = read();
for (int i = n; --i;) {
const int x = read(), y = read();
add(x, y);
add(y, x);
}
dfs(1, 0);
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int dp[2][60][60];
int q[60][60][60][60];
int ask(int a, int b, int c, int d) {
if (q[a][b][c][d] != -1) return q[a][b][c][d];
cout << "? " << a << ' ' << b << ' ' << c << ' ' << d << '\n';
cout.flush();
int x;
cin >> x;
if (x == -1) exit(0);
x = 1 ^ x;
q[a][b][c][d] = x;
return x;
}
void print(int v) {
cout << "!\n";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cout << dp[v][i][j];
cout << '\n';
}
}
int main() {
memset(q, -1, sizeof(q));
cin >> n;
for (int p = 0; p < 2; p++) {
memset(dp[p], -1, sizeof(dp[p]));
dp[p][1][1] = 1;
dp[p][n][n] = 0;
dp[p][1][2] = p;
dp[p][2][3] = dp[p][1][2] ^ ask(1, 2, 2, 3);
dp[p][3][2] = dp[p][1][2] ^ ask(1, 2, 3, 2);
dp[p][2][1] = dp[p][3][2] ^ ask(2, 1, 3, 2);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dp[p][i][j] == -1) {
if (i > 2) {
dp[p][i][j] = dp[p][i - 2][j] ^ ask(i - 2, j, i, j);
} else if (j > 2) {
dp[p][i][j] = dp[p][i][j - 2] ^ ask(i, j - 2, i, j);
} else {
dp[p][i][j] = dp[p][i - 1][j - 1] ^ ask(i - 1, j - 1, i, j);
}
}
}
}
}
for (int p = 0; p < 2; p++) {
for (int i = 3; i <= n; i++) {
for (int j = 2; j <= n; j++) {
if (dp[p][i - 2][j - 1] == dp[p][i][j]) {
if (dp[p][i - 1][j - 1] == dp[p][i][j - 1] ||
dp[p][i - 2][j] == dp[p][i - 1][j]) {
if (ask(i - 2, j - 1, i, j))
print(1 ^ p);
else
print(p);
return 0;
}
}
}
}
for (int i = 2; i <= n; i++) {
for (int j = 3; j <= n; j++) {
if (dp[p][i - 1][j - 2] == dp[p][i][j]) {
if (dp[p][i - 1][j - 1] == dp[p][i - 1][j] ||
dp[p][i][j - 2] == dp[p][i][j - 1]) {
if (ask(i - 1, j - 2, i, j))
print(1 ^ p);
else
print(p);
return 0;
}
}
}
}
}
}
| 5 |
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int MAX_SIZE = 50;
const int MAX_TASK_SIZE = 1000;
int R, C, M, a = 0;
int p[MAX_SIZE][MAX_SIZE] = {};
char f[MAX_SIZE][MAX_SIZE];
int costs[3][MAX_SIZE][MAX_SIZE];
int move(P src, P des, int t) {
if (src == des) {
return t;
}
f[src.first][src.second] = '@';
int ret = 0;
int dr[] = {-1, 1, 0, 0};
int dc[] = {0, 0, -1, 1};
for (int k = 0; k < 4; ++k) {
int i = src.first + dr[k];
int j = src.second + dc[k];
if (i + 1 && j + 1 && i < R && j < C && f[i][j] == '.') {
ret = move(P(i, j), des, t + 1);
if (ret) {
int cost = costs[1][i][j] + costs[2][i][j];
a += cost;
if (p[i][j]) {
int d = (t - p[i][j]) * costs[0][i][j] - cost;
if (d < 0) a += d;
}
p[i][j] = t;
break;
}
}
}
f[src.first][src.second] = '.';
return ret;
}
int calc(P tasks[]) {
int t = 1;
int i0 = tasks[0].first, j0 = tasks[0].second;
a = costs[1][i0][j0] + costs[2][i0][j0];
p[i0][j0] = t++;
for (int i = 0; i < M - 1; ++i) {
t = move(tasks[i], tasks[i + 1], t);
}
return a;
}
int main() {
int n;
P tasks[MAX_TASK_SIZE];
char s[MAX_SIZE + 1];
cin >> R >> C >> M;
for (int i = 0; i < R; ++i) {
cin >> s;
for (int j = 0; j < C; ++j) {
f[i][j] = s[j];
}
}
for (int k = 0; k < 3; ++k) {
for (int i = 0; i < R; ++i) {
for (int j = 0; j < C; ++j) {
cin >> n;
costs[k][i][j] = n;
}
}
}
for (int i = 0; i < M; ++i) {
int r, c;
cin >> r >> c;
tasks[i] = P(r, c);
}
cout << calc(tasks) << endl;
} | 0 |
#include <bits/stdc++.h>
int a[220001], b[220001], t[220001], c[220001];
void add(int index, int n, int val) {
for (; index <= n; index += ((index) & (-index))) c[index] += val;
}
int getSum(int index) {
int sum = 0;
for (; index > 0; index -= ((index) & (-index))) sum += c[index];
return sum;
}
template <class T>
inline void scanff(T &ret) {
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9')
;
while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar();
}
int main() {
int n, i, j, x;
scanff(n);
memset(c, 0, sizeof(c));
for (i = 1; i <= n; i++) {
scanff(x);
a[i] = x - getSum(++x) - 1;
add(++x, n, 1);
}
memset(c, 0, sizeof(c));
for (i = 1; i <= n; i++) {
scanff(x);
b[i] = x - getSum(++x) - 1;
add(++x, n, 1);
}
for (i = n; i >= 1; i--) {
t[i] = t[i] + a[i] + b[i];
if (t[i] >= n - i + 1) {
t[i] -= n - i + 1;
t[i - 1]++;
}
}
memset(c, 0, sizeof(c));
for (i = 1; i <= n; i++) add(i, n, 1);
int l, r, m;
for (i = 1; i <= n; i++) {
l = 1;
r = n;
while (l <= r) {
m = (l + r) >> 1;
if (getSum(m) >= t[i] + 1)
r = m - 1;
else
l = m + 1;
}
add(l, n, -1);
t[i] = l;
}
for (i = 1; i < n; i++) printf("%d ", t[i] - 1);
printf("%d\n", t[n] - 1);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 500005;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 100000007;
const double eps = 1e-10;
long long qpow(long long a, long long b) {
long long tmp = a % MOD, ans = 1;
while (b) {
if (b & 1) {
ans *= tmp, ans %= MOD;
}
tmp *= tmp, tmp %= MOD, b >>= 1;
}
return ans;
}
long long lowbit(long long x) { return x & -x; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
long long mmax(long long a, long long b, long long c) {
return max(a, max(b, c));
}
long long mmin(long long a, long long b, long long c) {
return min(a, min(b, c));
}
void mod(long long &a) {
a += MOD;
a %= MOD;
}
bool chk(long long now) {}
long long half(long long l, long long r) {
while (l <= r) {
long long m = (l + r) / 2;
if (chk(m))
r = m - 1;
else
l = m + 1;
}
return l;
}
long long ll(long long p) { return p << 1; }
long long rr(long long p) { return p << 1 | 1; }
long long mm(long long l, long long r) { return (l + r) / 2; }
long long lg(long long x) {
if (x == 0) return 1;
return (long long)log2(x) + 1;
}
bool smleql(double a, double b) {
if (a < b || fabs(a - b) <= eps) return true;
return false;
}
double len(double a, double b, double c, double d) {
return sqrt((a - c) * (a - c) + (b - d) * (b - d));
}
bool isp(long long x) {
if (x == 1) return false;
if (x == 2) return true;
for (long long i = 2; i * i <= x; ++i)
if (x % i == 0) return false;
return true;
}
inline long long read() {
char ch = getchar();
long long s = 0, w = 1;
while (ch < 48 || ch > 57) {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= 48 && ch <= 57) {
s = (s << 1) + (s << 3) + ch - 48;
ch = getchar();
}
return s * w;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
long long a, b, c, d, e, n;
string s;
map<string, long long> pos;
map<long long, string> out;
map<long long, long long> mp;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> a >> b >> c >> d >> e;
mp[1] = a;
mp[2] = b;
mp[3] = c;
mp[4] = d;
mp[5] = e;
pos["S"] = 1;
pos["M"] = 2;
pos["L"] = 3;
pos["XL"] = 4;
pos["XXL"] = 5;
out[1] = "S";
out[2] = "M";
out[3] = "L";
out[4] = "XL";
out[5] = "XXL";
cin >> n;
for (long long i = 1; i <= n; ++i) {
cin >> s;
if (mp[pos[s]] != 0) {
cout << out[pos[s]] << endl, mp[pos[s]]--;
continue;
}
for (long long i = 1; i <= 5; ++i) {
if (mp[pos[s] + i] != 0) {
cout << out[pos[s] + i] << endl, mp[pos[s] + i]--;
break;
}
if (mp[pos[s] - i] != 0) {
cout << out[pos[s] - i] << endl, mp[pos[s] - i]--;
break;
}
}
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define MN 400
#define mod 1000000007
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m,a[MN+5],b[MN+5],sum[MN+5][MN+5],f[MN+5][MN+5];
int main()
{
n=read();m=read();
for(int i=1;i<=n;++i) a[i]=read();
for(int i=1;i<=n;++i) b[i]=read();
for(int i=1;i<=400;++i)
for(int j=0,k=1;j<=400;++j)
sum[j][i]=(sum[j][i-1]+k)%mod,k=1LL*k*i%mod;
f[0][0]=1;
for(int i=1;i<=n;++i) for(int j=0;j<=m;++j) if(f[i-1][j])
for(int k=0;j+k<=m;++k)
f[i][j+k]=(f[i][j+k]+1LL*f[i-1][j]*(sum[k][b[i]]-sum[k][a[i]-1]+mod))%mod;
printf("%d\n",f[n][m]);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 140;
const int maxn = 20010;
int n, m, p;
int x[maxn], y[maxn], wa[maxn], wb[maxn], d[SIZE + 10][maxn],
f[SIZE + 10][maxn];
vector<char> res;
bitset<maxn> g[SIZE + 10];
int doit(int o, int n, int m) {
int L = o * SIZE, R = min((o + 1) * SIZE, n - 1);
for (int i = 0; i < m; i++) f[0][i] = d[o][i], g[0][i] = 0;
for (int i = 0; i < R - L; i++) {
f[i + 1][0] = f[i][0] + (x[L + i + 1] + y[0]) % p;
g[i + 1][0] = 0;
for (int j = 1; j < m; j++)
if (f[i][j] > f[i + 1][j - 1]) {
f[i + 1][j] = f[i][j] + (x[L + i + 1] + y[j]) % p;
g[i + 1][j] = 0;
} else {
f[i + 1][j] = f[i + 1][j - 1] + (x[L + i + 1] + y[j]) % p;
g[i + 1][j] = 1;
}
}
int u = R - L, v = m - 1;
for (;;)
if (g[u][v] == 0) {
if (u == 0) break;
res.push_back('C');
u--;
} else {
res.push_back('S');
v--;
}
return v + 1;
}
int main() {
cin >> n >> m >> p;
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < m; i++) cin >> y[i];
int *dp = wa, *dp1 = wb, *tmp;
dp[0] = (x[0] + y[0]) % p;
for (int i = 1; i < m; i++) dp[i] = dp[i - 1] + (x[0] + y[i]) % p;
for (int i = 1; i < n; i++) {
if ((i - 1) % SIZE == 0) memcpy(d[(i - 1) / SIZE], dp, sizeof(wa));
dp1[0] = dp[0] + (x[i] + y[0]) % p;
for (int j = 1; j < m; j++)
dp1[j] = max(dp1[j - 1], dp[j]) + (x[i] + y[j]) % p;
tmp = dp;
dp = dp1;
dp1 = tmp;
}
int cnt = m;
if (n > 1)
for (int i = (n - 2) / SIZE; i >= 0; i--) cnt = doit(i, n, cnt);
while (res.size() < n + m - 2) res.push_back('S');
cout << dp[m - 1] << endl;
for (int i = res.size() - 1; i >= 0; i--) cout << res[i];
cout << endl;
return 0;
}
| 5 |
#include <iostream>
using namespace std;
int main(){
long long N, A, B;
cin >> N >> A >> B;
cout << N/(A+B) * A + min(N%(A+B), A) << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int > Pi;
bool judge(int area){
//式変形だね!!
//area - y = (2y + 1)x
//(area - y) % (2y + 1)
for(int y = 1; y < min< int >(100000, area); y++){
if((area - y) % (2 * y + 1) == 0) return true;
}
return false;
}
int main(){
int N, room[100000];
scanf("%d", &N);
for(int i = 0; i < N; i++){
scanf("%d", &room[i]);
}
int ret = 0;
for(int i = 0; i < N; i++){
ret += !judge(room[i]);
}
printf("%d\n", ret);
} | 0 |
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int n,m;
while(cin >> n >> m && n){
vector<pair<string, int> > v;
int val=0;
string str;
int tmp;
for(int i=0;i<n;i++){
cin >> str >> tmp;
v.push_back(pair<string,int>(str,tmp));
}
for(int i=0;i<m;i++){
cin >> str;
bool matched;
for(int j=0;j<n;j++){
matched = true;
for(int k=0;k<8;k++){
if(v[j].first[k] == '*') continue;
if(v[j].first[k] != str[k]){
matched = false;
break;
}
}
if(matched){
val += v[j].second;
}
}
}
cout << val << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long fpow(long long n, long long k, long long p = 1000000007) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
long long inv(long long a, long long p = 1000000007 - 1) {
return fpow(a, p - 2, p);
}
long long Sqrt(long long x) {
if (x == 0 || x == 1) return x;
long long start = 1, end = x, ans;
while (start <= end) {
long long mid = (start + end) / 2;
if (mid * mid == x) return mid;
if (mid * mid < x) {
start = mid + 1;
ans = mid;
} else
end = mid - 1;
}
return ans;
}
long long power(long long x, long long y) {
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void in(long long &no) {
bool neg = false;
register long long c;
no = 0;
c = getchar();
if (c == '-') {
neg = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar()) no = no * 10 + c - 48;
if (neg) no *= -1;
}
long long maxx(long long a, long long b) {
if (a > b) return a;
return b;
}
long long minn(long long a, long long b) {
if (a < b) return a;
return b;
}
int main() {
long long n;
cin >> n;
long long a[n], b[n];
for (int i = (0); i < (n); i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
long long cnt = 0;
for (int i = (0); i < (n); i++) {
if (a[i] != b[i]) cnt++;
}
if (cnt > 2)
cout << "NO";
else
cout << "YES";
return 0;
}
| 3 |
#include<cstdio>
int main(){
int a,b;
scanf("%d %d",&a,&b);
if(a==1) a=14;
if(b==1) b=14;
if(a>b) printf("Alice\n");
else if(a<b) printf("Bob\n");
else printf("Draw\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int aaa = 0;
int n, pown = 1, a[240005], indx, indy, p[240005], lvl[240005], timer,
in[240005], out[240005], pw[4 * 240005], st, en, lc, oo, c[6], ans, q,
typ;
vector<int> v[240005], gg;
pair<int, pair<int, int> > tt[2 * 240005], l[2 * 240005][19], r[2 * 240005][19],
t[4 * 240005];
inline void dfs(int x, int par) {
gg.push_back(x);
p[x] = par;
timer++;
in[x] = timer;
for (int i = 0; i < v[x].size(); i++) {
if (v[x][i] == par) continue;
lvl[v[x][i]] = lvl[x] + 1;
dfs(v[x][i], x);
}
gg.push_back(x);
timer++;
out[x] = timer;
}
inline int lca(int x, int y) {
if (lvl[x] > lvl[y]) swap(x, y);
if (in[x] <= in[y] && out[x] >= out[y]) return x;
int t1 = in[x];
if (t1 > in[y]) t1 = in[y];
int t2 = out[x];
if (t2 < out[y]) t2 = out[y];
int sz = pw[t2 - t1];
pair<int, pair<int, int> > ret = l[t1][sz];
if (ret > r[t2][sz]) ret = r[t2][sz];
return p[gg[ret.second.first - 1]];
}
pair<int, pair<int, int> > doit() {
for (st = 1; st <= 4; st++) {
for (en = st + 1; en <= 4; en++) {
lc = lca(c[st], c[en]);
oo = 1;
for (int i = 1; i <= 4; i++) {
if (!((in[c[i]] <= in[c[st]] && out[c[st]] <= out[c[i]]) ||
(in[c[i]] <= in[c[en]] && out[c[en]] <= out[c[i]]))) {
oo = 0;
break;
}
if (!(in[lc] <= in[c[i]] && out[c[i]] <= out[lc])) {
oo = 0;
break;
}
}
if (oo) {
break;
}
}
if (oo) break;
}
if (oo)
return make_pair(1, make_pair(c[st], c[en]));
else
return make_pair(0, make_pair(0, 0));
}
void upd(int x) {
if (!x) return;
t[x].first = 0;
if (t[2 * x].first && t[2 * x + 1].first) {
c[1] = t[2 * x].second.first;
c[2] = t[2 * x].second.second;
c[3] = t[2 * x + 1].second.first;
c[4] = t[2 * x + 1].second.second;
t[x] = doit();
}
upd(x / 2);
}
void cnt(int x, int L, int R) {
if (t[x].first) {
if (!c[1]) {
c[1] = t[x].second.first;
c[2] = t[x].second.second;
ans += R - L + 1;
return;
}
c[3] = t[x].second.first;
c[4] = t[x].second.second;
pair<int, pair<int, int> > ret = doit();
if (ret.first) {
c[1] = ret.second.first;
c[2] = ret.second.second;
ans += R - L + 1;
return;
}
}
if (!t[2 * x].first) {
if (L == R) return;
cnt(2 * x, L, (L + R) / 2);
} else {
if (!c[1]) {
c[1] = t[2 * x].second.first;
c[2] = t[2 * x].second.second;
ans += (L + R) / 2 - L + 1;
if (L == R) return;
cnt(2 * x + 1, (L + R) / 2 + 1, R);
} else {
c[3] = t[2 * x].second.first;
c[4] = t[2 * x].second.second;
pair<int, pair<int, int> > ret = doit();
if (ret.first) {
c[1] = ret.second.first;
c[2] = ret.second.second;
ans += (L + R) / 2 - L + 1;
if (L == R) return;
cnt(2 * x + 1, (L + R) / 2 + 1, R);
} else {
if (L == R) return;
cnt(2 * x, L, (L + R) / 2);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
while (pown <= n) {
pown *= 2;
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i]++;
}
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
v[x].push_back(i);
}
cin >> q;
dfs(1, 1);
for (int i = 0; i < gg.size(); i++) {
tt[i + 1] = make_pair(lvl[gg[i]], make_pair(i + 1, i + 1));
}
for (int i = 2 * n; i >= 1; i--) {
l[i][0] = tt[i];
if (tt[i + 1].first < tt[i].first && i != 2 * n) l[i][0] = tt[i + 1];
if (i != 2 * n) l[i][0].second.second = i + 1;
r[l[i][0].second.second][0] = l[i][0];
for (int j = 1; j <= 18; j++) {
l[i][j] = l[i][j - 1];
if (l[i][j].first > l[l[i][j - 1].second.second][j - 1].first) {
l[i][j].first = l[l[i][j - 1].second.second][j - 1].first;
l[i][j].second.first = l[l[i][j - 1].second.second][j - 1].second.first;
}
l[i][j].second.second = l[l[i][j - 1].second.second][j - 1].second.second;
r[l[i][j].second.second][j] = l[i][j];
}
}
pw[1] = 0;
int pp = 2, txx = 1;
for (int i = 1; i < 19; i++) {
for (int j = pp; j < 2 * pp; j++) {
pw[j] = txx;
}
pp *= 2;
txx++;
}
for (int i = 1; i <= n; i++) {
t[pown + a[i] - 1].first = 1;
t[pown + a[i] - 1].second.first = i;
t[pown + a[i] - 1].second.second = i;
upd((pown + a[i] - 1) / 2);
}
while (q--) {
cin >> typ;
if (typ == 1) {
cin >> indx >> indy;
t[pown + a[indx] - 1].first = 1;
t[pown + a[indx] - 1].second.first = indy;
t[pown + a[indx] - 1].second.second = indy;
upd((pown + a[indx] - 1) / 2);
t[pown + a[indy] - 1].first = 1;
t[pown + a[indy] - 1].second.first = indx;
t[pown + a[indy] - 1].second.second = indx;
upd((pown + a[indy] - 1) / 2);
swap(a[indx], a[indy]);
} else {
ans = 0;
for (int i = 1; i <= 4; i++) c[i] = 0;
cnt(1, 1, pown);
cout << ans << endl;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void solve(int test_number);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.setf(ios::fixed);
cout.precision(9);
cerr.setf(ios::fixed);
cerr.precision(3);
int n = 1;
for (int i = 0; i < n; i++) {
solve(i);
}
}
pair<pair<int, int>, pair<int, int>> make_door(int n1, int m1, int n2, int m2) {
return make_pair(make_pair(n1, m1), make_pair(n2, m2));
}
void print(pair<pair<int, int>, pair<int, int>> d) {
cout << d.first.first << " " << d.first.second << " " << d.second.first << " "
<< d.second.second << endl;
}
void solve(int test_number) {
long long T;
cin >> T;
long long t = T;
int rep[40];
for (int i = 0; i < 39; i++) {
rep[i] = t % 3;
t /= 3;
}
int n = 42, m = 42;
vector<pair<pair<int, int>, pair<int, int>>> d1;
for (int col = 1; col + 5 <= n; col++) {
d1.push_back(make_door(col + 4, col, col + 5, col));
}
vector<pair<pair<int, int>, pair<int, int>>> d2;
for (int row = 1; row + 5 <= m; row++) {
d2.push_back(make_door(row, row + 4, row, row + 5));
}
vector<pair<pair<int, int>, pair<int, int>>> d3;
for (int p = 1; p <= 38; p++) {
d3.push_back(make_door(p, p + 3, p + 1, p + 3));
d3.push_back(make_door(p + 3, p, p + 3, p + 1));
}
vector<pair<pair<int, int>, pair<int, int>>> d4;
for (int p = 0; p <= 38; p++) {
if (rep[p] == 0) {
d4.push_back(make_door(p + 1, p + 3, p + 1, p + 4));
d4.push_back(make_door(p + 3, p + 1, p + 4, p + 1));
} else if (rep[p] == 1) {
d4.push_back(make_door(p + 1, p + 3, p + 1, p + 4));
}
}
vector<pair<pair<int, int>, pair<int, int>>> d5;
d5.push_back(make_door(n - 1, m - 1, n, m - 1));
d5.push_back(make_door(n - 1, m - 2, n, m - 2));
d5.push_back(make_door(n - 1, m - 1, n - 1, m));
d5.push_back(make_door(n - 2, m - 1, n - 2, m));
cout << n << " " << m << endl;
cout << d1.size() + d2.size() + d3.size() + d4.size() + d5.size() << endl;
for (auto d : d1) {
print(d);
}
for (auto d : d2) {
print(d);
}
for (auto d : d3) {
print(d);
}
for (auto d : d4) {
print(d);
}
for (auto d : d5) {
print(d);
}
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class C>
void mini(C &a4, C b4) {
a4 = min(a4, b4);
}
template <class C>
void maxi(C &a4, C b4) {
a4 = max(a4, b4);
}
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << '=' << h << ',';
_dbg(sdbg + 1, a...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
os << "[";
for (auto vv : V) os << vv << ",";
return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
long long n, k, l, r;
long long res = -1;
void spr(long long ob) {
if (ob <= 0) return;
long long kk = k - r - n * ob;
if (kk < 0) return;
long long res1 = kk / ob;
long long res2 = kk % ob;
if (res1 > n) {
res2 += ob * (res1 - n);
res1 = n;
}
(res2, res1, ob, kk);
if (res2 < r - 1 - n + res1) {
long long pom = r - 1 - n + res1 - res2;
(pom);
long long il = (pom + ob) / (ob + 1);
res1 -= il;
res2 += ob * il;
}
(res2, res1);
assert(res2 >= r - 1 - n + res1);
if (res2 > r || res1 < res2) {
return;
}
maxi(res, res1);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
if (0) cout << fixed << setprecision(6);
cin >> n >> l >> r >> k;
r = (r - l + 1 + n) % n;
if (r == 0) r = n;
if (k < r) {
cout << "-1\n";
return 0;
}
if (k <= 2 * r) {
long long jed = max(0ll, 2 * r - k - 1);
cout << n - jed << "\n";
return 0;
}
vector<long long> can;
if (k / n < 1e6) {
for (long long ob = 0; ob * n <= k; ob++) {
spr(ob);
}
} else {
for (long long i = (0); i <= ((long long)(n + 1) - 1); i++) {
long long ob = k / (n + i);
spr(ob);
spr(ob + 1);
spr(ob - 1);
}
}
cout << res << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int m, n;
int abss(int a) { return a < 0 ? -a : a; }
struct node {
int x, y;
};
vector<node> a[4][11];
bool cmp1(const node &a, const node &b) {
return abss(a.x - 1) + abss(a.y - 1) < abss(b.x - 1) + abss(b.y - 1);
}
bool cmp2(const node &a, const node &b) {
return abss(a.x - 1) + abss(a.y - m) < abss(b.x - 1) + abss(b.y - m);
}
bool cmp3(const node &a, const node &b) {
return abss(a.x - n) + abss(a.y - 1) < abss(b.x - n) + abss(b.y - 1);
}
bool cmp4(const node &a, const node &b) {
return abss(a.x - n) + abss(a.y - m) < abss(b.x - n) + abss(b.y - m);
}
int dis(node a, node b) { return abss(a.x - b.x) + abss(a.y - b.y); }
int cc[120000];
int main() {
int t;
node tmp;
int s, k;
scanf("%d %d %d %d", &n, &m, &s, &k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &t);
tmp.x = i;
tmp.y = j;
for (int x = 0; x < 4; x++) a[x][t].push_back(tmp);
}
for (int i = 1; i <= s; i++) {
sort(a[0][i].begin(), a[0][i].end(), cmp1);
sort(a[1][i].begin(), a[1][i].end(), cmp2);
sort(a[2][i].begin(), a[2][i].end(), cmp3);
sort(a[3][i].begin(), a[3][i].end(), cmp4);
}
for (int i = 0; i < k; i++) {
scanf("%d", &cc[i]);
}
int ans = 0;
for (int i = 1; i < k; i++) {
for (int j = 0; j < 4; j++) {
node ac = a[j][cc[i - 1]][0];
node wa = a[j][cc[i]][a[j][cc[i]].size() - 1];
ans = max(ans, dis(ac, wa));
}
}
cout << ans << endl;
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.