solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=5e5;
int a[N],w[N],n;
int judge(int x)
{
for(int i=1;i<=2*n-1;i++)a[i]=w[i]>x;
for(int i=n,j=n;i>1;i--,j++)
{
if(a[i]==a[i-1])return a[i]==0;
if(a[j]==a[j+1])return a[j]==0;
}
return a[n]^(n&1);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=2*n-1;i++)scanf("%d",w+i);
int l=1,r=2*n-1,mid;
while(l<r)
{
mid=(l+r)>>1;
if(judge(mid))r=mid;
else l=mid+1;
}
cout<<l<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int *arr = new int[n];
int *csum = new int[n];
int **pascal = new int *[1001];
for (int i = 0; i < 1001; i++) {
pascal[i] = new int[1001];
for (int j = 0; j < 1001; j++) {
if (j > i)
pascal[i][j] = 0;
else if (i == j)
pascal[i][j] = 1;
else if (j == 0)
pascal[i][j] = 1;
else {
pascal[i][j] = (pascal[i - 1][j - 1] + pascal[i - 1][j]) % 1000000007;
}
}
}
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (i == 0)
csum[i] = arr[i];
else
csum[i] = csum[i - 1] + arr[i];
}
long long int sum = 1;
for (int i = 1; i < n; i++) {
sum = (sum * pascal[csum[i - 1] + arr[i] - 1][csum[i - 1]]) % 1000000007;
}
cout << sum << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void test() {
int n, c0, c1, h;
cin >> n >> c0 >> c1 >> h;
string s;
cin >> s;
int zero = 0, one = 0;
for (int i = 0; i < n; i++)
if (s[i] == '0')
zero++;
else
one++;
long long ans = 0;
if (c0 + h < c1)
cout << ((c0 + h) * (one)) + (c0 * zero);
else {
if (c1 + h < c0)
cout << ((c1 + h) * (zero)) + (c1 * one);
else
cout << (c1 * one) + (c0 * (zero));
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) test();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 100001;
const int M = 20;
const int MOD = 1000000007;
const int inf = 1234567890;
const long long INF = 1122334455667788990;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int tc = 1, cs = 0;
while (tc--) {
double n, r, cosine, radius;
cin >> n >> r;
cosine = cos(2 * acos(-1) / n);
double x = sqrt((1 - cosine) / 2);
radius = (r * x) / (1 - x);
cout << fixed << setprecision(10);
cout << radius << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
double pd[6][6][2][10010];
int esq[6], dir[6];
double busca(int n, int q, int x, int k) {
double &res = pd[n][q][x][k];
if (res != -1) {
return res;
}
if (n == 0) {
if (x == 1) {
return 0;
} else {
if (q == 0) {
return res = 1;
} else {
return res = 0;
}
}
}
res = 0;
if (q > 0 && esq[n] <= k && k <= dir[n]) {
res += (1.0 / (dir[n] - esq[n] + 1)) * busca(n - 1, q - 1, x, k);
}
if (esq[n] < k) {
if (dir[n] < k) {
res += 1 * busca(n - 1, q, x, k);
} else {
res += (((double)(k - esq[n])) / (dir[n] - esq[n] + 1)) *
busca(n - 1, q, x, k);
}
}
if (x > 0) {
if (k < dir[n]) {
if (k < esq[n]) {
res += 1 * busca(n - 1, q, x - 1, k);
} else {
res += (((double)(dir[n] - k)) / (dir[n] - esq[n] + 1)) *
busca(n - 1, q, x - 1, k);
}
}
}
return res;
}
int main() {
int a, b, c, d;
for (a = 0; a < 6; a++) {
for (b = 0; b < 6; b++) {
for (c = 0; c < 2; c++) {
for (d = 0; d < 10010; d++) {
pd[a][b][c][d] = -1;
}
}
}
}
int n;
scanf("%d", &n);
int i, j;
for (i = 1; i <= n; i++) {
scanf("%d %d", &esq[i], &dir[i]);
}
double res = 0;
for (i = 1; i < 10010; i++) {
double soma = 0;
for (j = 1; j <= n; j++) {
if (j == 1) {
soma += busca(n, j, 1, i);
} else if (j == n) {
soma += busca(n, j, 0, i);
} else {
soma += busca(n, j, 0, i);
soma += busca(n, j, 1, i);
}
}
res += soma * i;
}
printf("%.16lf\n", res);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m, k = 0, e, mo = 0, d, i, l = -8909, j = -987, na = 0,
p = 0, o = 1, t = 0, z = 0;
cin >> n >> m;
long long int a[n], b[99999];
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (i = 1, j = 0, k = 0;; i++) {
if (n > j) {
if (i == a[j]) {
j++;
continue;
}
}
m = m - i;
if (m >= 0) {
b[k] = i;
k++;
} else
break;
}
cout << k << endl;
for (i = 0; i < k; i++) {
cout << b[i] << " ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[10005];
int main() {
int n, m;
int c = 0, max = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> m;
a[m]++;
}
for (int i = 0; i < 10000; i++) {
if (a[i] > max) {
max = a[i];
}
}
cout << max << endl;
return 0;
}
| 1 |
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
long long a[100]={0},i;
a[1]=2,a[2]=1;
for(i=3;i<=87;i++)
a[i]=a[i-1]+a[i-2];
cout<<a[n+1];
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void shikhar7s(long long int cas) {
long long int n, i, s = 0, ma = 0, in, maa = 0, inn;
cin >> n;
long long int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
if (a[i] > ma) {
ma = a[i];
in = i;
}
}
s -= ma;
vector<long long int> ans;
for (i = 0; i < n; i++) {
if (in != i) {
if (s - a[i] == ma) {
ans.push_back(i);
}
if (a[i] > maa) {
maa = a[i];
inn = i;
}
}
}
if (s == 2 * maa) ans.push_back(in);
cout << (long long int)ans.size() << "\n";
for (i = 0; i < (long long int)ans.size(); i++) cout << ans[i] + 1 << " ";
}
signed main() {
std::ios_base::sync_with_stdio(false);
long long int t = 1;
long long int cas = 1;
while (cas <= t) {
shikhar7s(cas);
cas++;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int N = 100054;
const double eps = 1e-13;
struct prob {
int a, b;
double p;
prob *read() { return scanf("%d%d%lf", &a, &b, &p), this; }
inline bool operator<(const prob &B) const { return b * p < B.b * B.p; }
} v[N];
struct vec2 {
double x, y;
vec2(double x0 = 0.0, double y0 = 0.0) : x(x0), y(y0) {}
inline vec2 operator-(const vec2 &B) const { return vec2(x - B.x, y - B.y); }
inline bool operator<(const vec2 &B) const {
return x < B.x || (x == B.x && y < B.y);
}
inline double operator^(const vec2 &B) const { return x * B.y - y * B.x; }
} p[N], ch[N], *rt = ch;
int n, CH;
double M;
inline void up(double &x, const double y) { x < y ? x = y : 0; }
double trans(long long t, long long n, double a, double b, double f_t) {
double ak = pow(1 - a, n), aq = (1 - ak) / a,
ay = ((n - 1) + ak - (1 - a) * n) / (a * a);
return ak * f_t + (aq * t + ay) * (a * M) + aq * b;
}
int main() {
int i, j, k;
long long t = 0, T;
double cur = 0, next, slope, slope_limit;
scanf("%d%lld", &n, &T);
for (i = 0; i < n; ++i) v[i].read(), p[i] = vec2(v[i].p, v[i].p * v[i].a);
{
prob *it = std::max_element(v, v + n);
M = it->b * it->p;
}
std::sort(p, p + n);
for (i = 0; i < n; *rt++ = p[i++])
for (; rt > ch + 1 && (((p[i]) - (rt[-2])) ^ ((rt[-1]) - (rt[-2]))) <= 0;
--rt)
;
CH = rt - ch;
for (i = 0; i < CH - 1 && ch[i].y < ch[i + 1].y; ++i)
;
for (; i < CH && t < T; ++i) {
slope_limit =
(i == CH - 1 ? INFINITY
: (ch[i].y - ch[i + 1].y) / (ch[i + 1].x - ch[i].x));
slope = t * M - cur;
if (slope > slope_limit) continue;
for (j = 0;; ++j) {
next = trans(t, 1ll << j, ch[i].x, ch[i].y, cur),
slope = (t + (1ll << j)) * M - next;
if (slope > slope_limit || t + (2ll << j) >= T) break;
}
for (k = j; k >= 0; --k) {
if (t + (1ll << k) >= T) continue;
next = trans(t, 1ll << k, ch[i].x, ch[i].y, cur),
slope = (t + (1ll << k)) * M - next;
if (slope <= slope_limit) t += 1ll << k, cur = next;
}
cur = (1 - ch[i].x) * cur + ch[i].x * M * t + ch[i].y, ++t;
}
printf("%.12lg\n", cur);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
int t, l, sum = 0;
for (int i = 0; i < n; i++) {
t = a[i];
l = 0;
for (int j = 0; j < m; j++) {
if (t <= b[j]) {
l = 1;
b[j] = 0;
break;
}
}
if (l == 0) {
sum++;
}
}
cout << sum;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, k, l, i, j, a, b, a1, a2, b1, b2, c1, c2, d[100005][3], res;
map<int, int> m;
priority_queue<pair<int, int> > q;
int main() {
cin >> n;
for (i = 0; i < n; i++) {
cin >> a;
m[a]++;
}
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
q.push({it->second, it->first});
while (q.size() >= 3) {
a1 = q.top().first - 1;
a2 = q.top().second;
q.pop();
b1 = q.top().first - 1;
b2 = q.top().second;
q.pop();
c1 = q.top().first - 1;
c2 = q.top().second;
q.pop();
if (a1 > 0) q.push({a1, a2});
if (b1 > 0) q.push({b1, b2});
if (c1 > 0) q.push({c1, c2});
d[res][0] = a2;
d[res][1] = b2;
d[res][2] = c2;
sort(d[res], d[res] + 3);
reverse(d[res], d[res] + 3);
res++;
}
cout << res << endl;
for (i = 0; i < res; i++)
cout << d[i][0] << " " << d[i][1] << " " << d[i][2] << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 7;
char s[N];
const int MOD = 1e9 + 9;
int A, invA;
int pw[N];
int add(int a, int b) {
a += b;
if (a < 0) a += MOD;
a %= MOD;
return a;
}
int mul(int a, int b) { return (long long)a * b % MOD; }
int bin_mul(int a, int b) {
a %= MOD;
int res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
struct Node {
bool first, last;
int len, hsh;
bool operator==(const Node& other) const {
return len == other.len && hsh == other.hsh;
}
};
Node t[N << 2];
Node combine(Node lhs, Node rhs) {
if (lhs.len == 0) return rhs;
if (rhs.len == 0) return lhs;
if (lhs.last && rhs.first) {
lhs.len--;
rhs.len--;
lhs.hsh = add(lhs.hsh, -1);
lhs.hsh = mul(lhs.hsh, invA);
rhs.hsh -= pw[rhs.len];
if (lhs.len == 0) lhs.first = false;
if (rhs.len == 0) rhs.last = false;
}
auto new_hsh = add(mul(lhs.hsh, pw[rhs.len]), rhs.hsh);
return {lhs.first, rhs.last, lhs.len + rhs.len, new_hsh};
}
void build(int v, int l, int r) {
if (l == r) {
if (s[l] == '0') {
t[v] = {false, false, 1, 0};
} else {
t[v] = {true, true, 1, 1};
}
return;
}
int mid = (l + r) >> 1;
build(v << 1, l, mid);
build(v << 1 | 1, mid + 1, r);
t[v] = combine(t[v << 1], t[v << 1 | 1]);
}
Node get(int v, int l, int r, int x, int y) {
if (l > y || r < x) return {false, false, 0, 0};
if (l >= x && r <= y) return t[v];
int mid = (l + r) >> 1;
return combine(get(v << 1, l, mid, x, y), get(v << 1 | 1, mid + 1, r, x, y));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
clock_t ttt = clock();
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
A = rng() % MOD;
invA = bin_mul(A, MOD - 2);
int n;
cin >> n;
pw[0] = 1;
for (int i = 1; i <= n; ++i) pw[i] = mul(pw[i - 1], A);
cin >> (s + 1);
int q;
cin >> q;
build(1, 1, n);
while (q--) {
int l1, l2, l;
cin >> l1 >> l2 >> l;
l--;
if (get(1, 1, n, l1, l1 + l) == get(1, 1, n, l2, l2 + l)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
cerr << "Time taken " << (double)(clock() - ttt) / CLOCKS_PER_SEC
<< " seconds\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18;
const int mod = (int)1e9 + 7;
const int MAXN = (int)1e5 + 5;
long long n;
bool var[MAXN];
int t[4 * MAXN];
int a[MAXN];
bool dur = 1;
void build(int v, int tl, int tr) {
if (tl == tr) {
if (dur)
t[v] = -1;
else
t[v] = n;
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
if (dur)
t[v] = min(t[2 * v], t[2 * v + 1]);
else
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
int que(int v, int tl, int tr, int l, int r) {
if (l > r)
return (dur ? n + 1 : -1);
else if (tl == l && tr == r)
return t[v];
else {
int tm = (tl + tr) / 2;
int q1 = que(2 * v, tl, tm, l, min(tm, r)),
q2 = que(2 * v + 1, tm + 1, tr, max(tm + 1, l), r);
if (dur) {
return min(q1, q2);
} else
return max(q1, q2);
}
}
void upd(int v, int tl, int tr, int pos, int val) {
if (tl == tr) {
t[v] = val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
upd(2 * v, tl, tm, pos, val);
else
upd(2 * v + 1, tm + 1, tr, pos, val);
if (dur)
t[v] = min(t[2 * v], t[2 * v + 1]);
else
t[v] = max(t[2 * v], t[2 * v + 1]);
}
}
int last[MAXN];
bool ot[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
ot[a[i]] = 1;
}
for (int i = 1; i <= n + 1; i++)
if (ot[i] == 0) {
var[i] = 1;
break;
}
memset(last, -1, sizeof(last));
dur = 1;
build(1, 1, n);
for (int i = 0; i < n; i++) {
int c = a[i];
if (c == 1) {
if (last[c] != i - 1) var[c] = 1;
} else if (que(1, 1, n, 1, c - 1) > last[c]) {
var[c] = 1;
}
upd(1, 1, n, c, i);
last[c] = i;
}
dur = 0;
build(1, 1, n);
for (int i = 1; i <= n; i++) last[i] = n;
for (int i = n - 1; i >= 0; i--) {
int c = a[i];
if (c == 1) {
if (last[c] != i + 1) var[c] = 1;
} else if (que(1, 1, n, 1, c - 1) < last[c])
var[c] = 1;
upd(1, 1, n, c, i);
last[c] = i;
}
for (int i = 1; i <= n + 2; i++) {
if (var[i] == 0) {
cout << i << '\n';
break;
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 100001;
struct segment{
int n, seg[3 * N];
void resize(int _n){
n = _n;
for(int i = 0 ; i < 2 * n ; i++){
seg[i] = 0;
}
}
void modify(int p, int val){
for(seg[p += n] = val ; p > 1 ; p >>= 1)
seg[p >> 1] = seg[p] + seg[p ^ 1];
}
int query(int l, int r){
if(r < l) return 0;
r++;
int ret = 0;
for(l += n, r += n ; l < r ; l >>= 1, r >>= 1){
if(l & 1) ret += seg[l++];
if(r & 1) ret += seg[--r];
}
return ret;
}
};
segment tree;
ll calc(string &a, string &b){
int n = a.size();
vector <int> v[4];
for(int i = n - 1 ; i >= 0 ; i--){
v[b[i] - '0'].push_back(i);
}
vector <int> p(n);
for(int i = 0 ; i < n ; i++){
p[i] = v[a[i] - '0'].back();
v[a[i] - '0'].pop_back();
}
tree.resize(n + 1);
ll ret = 0;
for(int i = 0 ; i < n ; i++){
ret += tree.query(p[i] + 1, n - 1);
tree.modify(p[i], 1);
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--){
string s;
cin >> s;
int n = s.size();
for(int i = 0 ; i < n ; i++){
if(s[i] == 'A') s[i] = '0';
else if(s[i] == 'N') s[i] = '1';
else if(s[i] == 'O') s[i] = '2';
else s[i] = '3';
}
vector <int> f = {0, 1, 2, 3};
string best_str;
ll best_value = -1;
do{
string t = s;
sort(t.begin(), t.end(), [&](char l, char r){
return f[l - '0'] < f[r - '0'];
});
ll c = calc(s, t);
if(c > best_value){
best_value = c;
best_str = t;
}
} while(next_permutation(f.begin(), f.end()));
for(int i = 0 ; i < n ; i++){
if(best_str[i] == '0') best_str[i] = 'A';
else if(best_str[i] == '1') best_str[i] = 'N';
else if(best_str[i] == '2') best_str[i] = 'O';
else best_str[i] = 'T';
}
cout << best_str << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll OO = 1e16;
int di[8] = {0, 0, 1, -1, -1, 1, -1, 1};
int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1};
string ys = "YES", no = "NO";
const long double dgr = acos(-1) / 180, dg = 180 / acos(-1);
const int mod = 1e9 + 7, N = 1e5 + 5;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
ll n, m, k;
cin >> n >> m >> k;
if (k > n + m - 2) return cout << -1, 0;
if (n > m) swap(n, m);
cout << (k < m ? max(m * (n / (k + 1)), n * (m / (k + 1))) : n / (k + 2 - m))
<< endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x;
cin >> x;
x--;
int ans[5] = {2, 3, 1, 2, 1};
cout << ans[x] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5, M = 3e3 + 5;
const long long inf = 1e18 + 100, mod = 1e9 + 7;
const double eps = 1e-9;
long long pos[N];
pair<long long, long long> a[N];
map<long long, long long> b;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m, k;
cin >> n >> m >> k;
for (long long i = 1; i <= n; ++i) cin >> a[i].first;
for (long long i = 1; i <= n; ++i) cin >> a[i].second;
for (long long i = 1; i <= n; ++i)
if (a[i].first > b[a[i].second]) b[a[i].second] = a[i].first;
long long cnt = 0;
while (k--) {
long long i;
cin >> i;
if (b[a[i].second] != a[i].first) cnt++;
}
cout << cnt;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k, n, a[100000], b[100000], c = 0, m = 0;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i] >> b[i];
for (i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1] && b[i] == b[i + 1]) {
c++;
m = max(m, c);
} else
c = 0;
}
cout << m + 1;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define FOR(x,n) for(int x=0;x<n;x++)
#define mp make_pair
#define PI 3.14159265358979323846264338327950288
typedef long long ll;
typedef pair<int,int> ii;
#define SEGTYPE int
#define SEGFN max
#define SEGMX INT_MIN // Change to INT_MIN or LLONG_MIN for range max query
const int MX = 1e5 + 1;
const int CHKSIZE = 250;
const int CHKS = MX/CHKSIZE + 1;
class fenwickTree {
public:
int* tree;
int size;
int get(int index) {
index++; // dummy node indexing
int ret = 0;
while(index > 0) {
ret += tree[index];
index -= (index & -index);
}
return ret;
}
void update(int index, int delta) {
if(index >= size) return;
index++; // dummy node indexing
while(index <= size) {
tree[index] += delta;
index += (index & -index);
}
}
int lbound(int x) {
int curr = 0, currsum = 0;
for(int i = 20; i >= 0; i--) {
int bit = (1 << i);
int up = curr + bit;
if(up > size || currsum + tree[up] >= x) continue;
curr = up;
currsum += tree[up];
}
return curr;
}
fenwickTree() {
size = MX;
tree = new int[size+1]; // +1 for dummy node at 0
memset(tree, 0, sizeof(int)*(size+1));
for(int i = 1; i < size; i++) {
update(i, 1);
}
}
};
int n;
int arr[MX], rollback[MX], nxt[2*MX];
fenwickTree trees[CHKS];
void upd1(int i, int val) {
int idx = i/CHKSIZE;
int start = i;
int end = min(idx*CHKSIZE + CHKSIZE, n) - 1;
for(int j = end; j >= i; j--) {
trees[idx].update(rollback[j], -1);
}
arr[i] = val;
for(int j = i; j <= end; j++) {
int x = trees[idx].lbound(arr[j]);
rollback[j] = x;
trees[idx].update(x, 1);
}
}
int query2(int i) {
int curr = arr[i];
i++;
while(i < n && nxt[i] == -1) {
if(arr[i] <= curr) curr++;
i++;
}
while(i < n) {
curr = trees[i/CHKSIZE].get(curr);
i = nxt[i];
}
return n - curr;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
memset(nxt, -1, sizeof nxt);
for(int i = 0; i < 2*MX; i += CHKSIZE) {
nxt[i] = i + CHKSIZE;
}
scanf("%d", &n);
FOR(i,n) {
scanf("%d", &arr[i]);
}
FOR(i,MX) rollback[i] = n;
int chks = (n + CHKSIZE - 1)/CHKSIZE;
FOR(i,chks) {
upd1(CHKSIZE * i, arr[CHKSIZE * i]);
}
int q;
scanf("%d", &q);
while(q--) {
int t;
scanf("%d", &t);
if(t == 1) {
int i, x;
scanf("%d %d", &i, &x);
i--;
upd1(i, x);
}
else {
int i;
scanf("%d", &i);
i--;
printf("%d\n", query2(i));
}
}
}
// UPDATE N WHEN UR DONE AAAAAAAAAAAAAAAAAAAAAAAAAAA (if it applies lol)
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
long long int a[n][m], b[n][m];
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m; j++) cin >> a[i][j];
}
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m; j++) cin >> b[i][j];
}
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m; j++) {
if (a[i][j] > b[i][j]) swap(a[i][j], b[i][j]);
}
}
long long int flag = 0;
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m - 1; j++) {
if (a[i][j] >= a[i][j + 1]) {
flag = 1;
break;
}
if (b[i][j] >= b[i][j + 1]) {
flag = 1;
break;
}
}
}
for (long long int i = 0; i < m; i++) {
for (long long int j = 0; j < n - 1; j++) {
if (a[j][i] >= a[j + 1][i]) {
flag = 1;
break;
}
if (b[j][i] >= b[j + 1][i]) {
flag = 1;
break;
}
}
}
if (flag == 1)
cout << "Impossible";
else
cout << "Possible";
return 0;
}
| 2 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
#define int long long
#define fst first
#define scd second
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
typedef pair<int, int> pii;
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
const int inf = 3e18;
int X[100010];
int Y[100010];
pii pos[100010];
int v[100010];
signed main()
{
omajinai;
int W, H; cin >> W >> H;
int N; cin >> N;
rep(i, N) {
cin >> X[i];
cin >> Y[i];
pos[i] = pii(X[i], Y[i]);
}
sort(X, X + N);
sort(Y, Y + N);
int ans = inf;
int ansx = -1, ansy = -1;
rep(i, 2) {
rep(j, 2) {
int x = X[N / 2 - i], y = Y[N / 2 - j];
int sum = 0;
int ma = 0;
rep(k, N) {
int a = abs(x - pos[k].fst) + abs(y - pos[k].scd);
sum += a * 2;
chmax(ma, a);
}
sum -= ma;
if (ans >= sum) {
ans = sum;
ansx = x;
ansy = y;
}
}
}
cout << ans << endl;
cout << ansx << ' ' << ansy << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char v[6][40];
int p[6];
bool used[6];
bool foi;
vector<string> res;
vector<string> g;
char first(int x) { return v[x][0]; }
char last(int x) { return v[x][strlen(v[x]) - 1]; }
bool check() {
if (strlen(v[p[0]]) + strlen(v[p[4]]) != strlen(v[p[3]]) + 1) return false;
if (strlen(v[p[1]]) + strlen(v[p[5]]) != strlen(v[p[2]]) + 1) return false;
if (first(p[0]) != first(p[1])) return false;
if (last(p[0]) != first(p[2])) return false;
if (last(p[1]) != first(p[3])) return false;
if (first(p[4]) != last(p[2])) return false;
if (last(p[4]) != last(p[5])) return false;
if (last(p[3]) != first(p[5])) return false;
if (v[p[3]][strlen(v[p[0]]) - 1] != v[p[2]][strlen(v[p[1]]) - 1])
return false;
return true;
}
void hor(int ind, int a, int b) {
int sz = strlen(v[ind]);
for (int i = b; i < b + sz; i++) {
g[a][i] = v[ind][i - b];
}
}
void ver(int ind, int a, int b) {
int sz = strlen(v[ind]);
for (int i = a; i < a + sz; i++) {
g[i][b] = v[ind][i - a];
}
}
void monta() {
int m = strlen(v[p[0]]) + strlen(v[p[4]]) - 1;
int n = strlen(v[p[1]]) + strlen(v[p[5]]) - 1;
string str = "";
for (int i = 0; i < m; i++) str = str + '.';
g.clear();
g.resize(n, str);
hor(p[0], 0, 0);
hor(p[3], strlen(v[p[1]]) - 1, 0);
hor(p[4], n - 1, strlen(v[p[0]]) - 1);
ver(p[1], 0, 0);
ver(p[2], 0, strlen(v[p[0]]) - 1);
ver(p[5], strlen(v[p[1]]) - 1, m - 1);
}
bool menor() {
if (!foi) return true;
for (int i = 0; i < min((int)res.size(), (int)g.size()); i++) {
if (res[i] < g[i]) return false;
if (res[i] > g[i]) return true;
}
if ((int)res.size() < (int)g.size()) return false;
return true;
}
bool rec(int ind) {
if (ind == 6) {
if (check()) {
monta();
if (menor()) {
res = g;
}
foi = true;
return true;
}
return false;
}
bool r = false;
for (int i = 0; i < 6; i++) {
if (used[i]) continue;
p[ind] = i;
used[i] = true;
if (rec(ind + 1)) r = true;
used[i] = false;
}
return r;
}
int main() {
for (int i = 0; i < 6; i++) {
scanf("%s", v[i]);
}
foi = false;
memset(used, false, sizeof(used));
if (rec(0)) {
for (int i = 0; i < (int)res.size(); i++) {
printf("%s\n", res[i].c_str());
}
} else {
printf("Impossible\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int n,k,bot=0, mid, top,aux,au,z=0;
cin >> n >> k;
vector<long long int>a(n);
vector<long long int>c(n);
for(int i=0;i<n;i++) cin >> a[i];
for(int i=0;i<n;i++) cin >> c[i];
sort(a.begin(),a.end());
sort(c.begin(),c.end());
top = a[n-1]*c[n-1];
while(top>bot){
mid = (bot + top)/2;
aux=0;
for(int i=0;i<n;i++){
au = min(mid/c[i], a[n-i-1]);
aux+= a[n-1-i]-au;
if(aux>k) break;
}
if(aux>k) bot=mid+1;
else top=mid;
}
cout << bot << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 200000;
const LL INF = 3e18;
const int MOD = 1e9 + 7;
int n; LL a, b;
LL si[N];
int pA[N], pB[N];
int main()
{
cin >> n >> a >> b;
for (int i = 1; i <= n; ++ i) cin >> si[i];
si[0] = -INF; si[n + 1] = INF;
pA[1] = pB[1] = 1;
for (int i = 2, lA = 1, rA = 1, lB = 1, rB = 1; i <= n + 1; ++ i)
{
while (si[i] - si[rA] >= a && rA < i - 1) rA ++;
if (si[i - 1] - si[i - 2] < b) lA = i - 1;
while (si[i] - si[rB] >= b && rB < i - 1) rB ++;
if (si[i - 1] - si[i - 2] < a) lB = i - 1;
pA[i] = (0ll + pA[i - 1] + (lA <= rA? pB[rA] - pB[lA - 1]: 0) + MOD) % MOD;
pB[i] = (0ll + pB[i - 1] + (lB <= rB? pA[rB] - pA[lB - 1]: 0) + MOD) % MOD;
}
cout << (0ll + pA[n + 1] - pA[n] + pB[n + 1] - pB[n] + 2ll * MOD) % MOD << endl;
}
| 0 |
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main(){
int d,i,j,k;
cin >>d;
double a[1000]={},b[1000]={};
for(i=0;i<d;i++) cin >>a[i];
for(i=0;i<d;i++) cin >>b[i];
double a1=0,a2=0,a3=0,ao=0;
for(i=0;i<d;i++){
a1+=fabs(a[i]-b[i]);
a2+=fabs((a[i]-b[i])*(a[i]-b[i]));
a3+=fabs((a[i]-b[i])*(a[i]-b[i])*(a[i]-b[i]));
ao=max(ao,fabs(a[i]-b[i]));
}
printf("%.5f\n",a1);
printf("%.5f\n",sqrt(a2));
printf("%.5f\n",cbrt(a3));
printf("%.5f\n",ao);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int M = 100 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
vector<int> edge[N];
int n, x, y, cnt;
bool dfs(int u, int p) {
int deg = 2;
for (int v : edge[u]) {
if (v == p) continue;
if (dfs(v, u) && deg > 0) {
cnt++;
deg--;
}
}
return deg > 0;
}
int main() {
scanf("%d%d%d", &n, &x, &y);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
long long ans = 0;
if (x < y) {
dfs(1, -1);
ans = 1ll * cnt * x + 1ll * (n - cnt - 1) * y;
} else {
bool flag = false;
for (int i = 1; i <= n; i++) {
if (edge[i].size() == n - 1) flag = true;
}
if (flag) {
ans = 1ll * x + 1ll * (n - 2) * y;
} else {
ans = 1ll * (n - 1) * y;
}
}
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-8;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()
int cnt[MAX_N]{};
bool inCycle[MAX_N];
vector<vector<int> > g;
void dfs(int i){
inCycle[i] = false;
cnt[i]--;
for(auto k : g[i]){
cnt[k]--;
if(cnt[k]==1){
dfs(k);
}
}
}
int main() {
int n;
cin >> n;
g.resize(n);
REP(i,n){
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
cnt[u]++;
cnt[v]++;
}
fill(inCycle,inCycle+n,true);
REP(i,n){
if(cnt[i]==1){
dfs(i);
}
}
int q;
cin >> q;
REP(i,q){
int u, v;
cin >> u >> v;
u--;
v--;
if(inCycle[u]&&inCycle[v]){
cout << 2 << endl;
}
else {
cout << 1 << endl;
}
}
return 0;
}
| 0 |
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int N=2e5+5;
int f1[N],f2[N];
int find1(int x){
if(f1[x]==x)
return x;
return f1[x]=find1(f1[x]);
}
int find2(int x){
if(f2[x]==x)
return x;
return f2[x]=find2(f2[x]);
}
map<pair<int,int>,int>p;
int main()
{
int n,k,l;
scanf("%d%d%d",&n,&k,&l);
for(int i=1;i<=n;i++)
f1[i]=f2[i]=i;
for(int i=1;i<=k;i++){
int u,v;
scanf("%d%d",&u,&v);
int a=find1(u),b=find1(v);
f1[a]=b;
}
for(int i=1;i<=l;i++){
int u,v;
scanf("%d%d",&u,&v);
int a=find2(u),b=find2(v);
f2[a]=b;
}
for(int i=1;i<=n;i++)
p[make_pair(find1(i),find2(i))]++;
for(int i=1;i<=n;i++)
printf("%d ",p[make_pair(f1[i],f2[i])]);
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007;
int main(){
string S;
cin >> S;
int n = S.length();
int s[n];
long long int dp[n+1][13];
for (int i=0;i<=n;i++){
for (int j=0;j<13;j++){
dp[i][j]=0;
}
}
dp[0][0]=1;
long long int ans;
for(int i=0;i<n;i++){
if(S[i]=='?')s[i]=-1;
else s[i]=S[i]-'0';
for(int j=0;j<10;j++){
if(s[i]!=-1 && s[i]!=j) continue;
for(int ki=0;ki<13;ki++){
dp[i+1][(ki*10+j)%13]+=dp[i][ki];
dp[i+1][(ki*10+j)%13]%=MOD;
}
}
}
cout << dp[n][5] << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long this_sum[100000], wait_sum[100000];
int main() {
int a;
cin >> a;
string s;
cin >> s;
int len = s.size();
for (int i = 0; i < len; i++) {
int sum = 0;
for (int j = i; j < len; j++) {
sum += s[j] - '0';
if (sum == 0) {
this_sum[sum]++;
} else if (a % sum == 0) {
if (sum <= a / sum) this_sum[sum]++;
if (sum >= a / sum) wait_sum[a / sum]++;
}
}
}
if (a == 0) {
cout << this_sum[0] * (len * (len + 1) - this_sum[0]) << endl;
} else {
long long sum = 0;
int limit = (int)sqrt(a + 0.5);
for (int i = 1; i <= limit; i++)
sum += this_sum[i] * wait_sum[i] * (1 + (i * i != a));
cout << sum << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long inf = 1e9 + 5;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<long>> dp(n + 1, vector<long>(2, inf));
dp[0][0] = 0;
dp[0][0] = 0;
long skips;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
skips = 0;
if (j == 0 && a[i] == 1) skips++;
dp[i + 1][j ^ 1] = min(dp[i + 1][j ^ 1], dp[i][j] + skips);
if (i < n - 1) {
if (j == 0 && a[i + 1] == 1) skips++;
dp[i + 2][j ^ 1] = min(dp[i + 2][j ^ 1], dp[i][j] + skips);
}
}
}
cout << min(dp[n][0], dp[n][1]) << "\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, l, q;
int a[10], t[10];
string ans[1000005];
bool f[10];
bool check(string st, int L, int R) {
if (st[L] == '0')
if (R == L + 1)
return true;
else
return false;
int x = 0;
for (int i = L; i < R; i++) {
x *= 10;
x += st[i] - 48;
}
return x >= 0 && x < 256;
}
void dfs(int x) {
if (x <= (l + 1) / 2) {
for (int i = 1; i <= n; i++) {
t[x] = a[i];
dfs(x + 1);
}
} else {
memset(f, 0, sizeof(f));
for (int i = 1; i <= l / 2; i++) {
t[l + 1 - i] = t[i];
}
for (int i = 1; i <= l; i++) {
f[t[i]] = true;
}
for (int i = 1; i <= n; i++) {
if (!f[a[i]]) return;
}
string st = "";
for (int i = 1; i <= l; i++) {
st += char(t[i] + '0');
}
for (int i = 1; i <= 3; i++) {
for (int j = i + 1; j <= i + 3; j++) {
for (int k = j + 1; k <= j + 3; k++) {
int tmp = st.size();
if (tmp - k >= 1 && tmp - k <= 3) {
if (check(st, 0, i) && check(st, i, j) && check(st, j, k) &&
check(st, k, tmp)) {
string s = st.substr(0, i) + "." + st.substr(i, j - i) + "." +
st.substr(j, k - j) + "." + st.substr(k, tmp - k);
q++;
ans[q] = s;
}
}
}
}
}
}
}
int main() {
cin >> n;
if (n >= 7) {
cout << 0;
return 0;
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (l = 4; l <= 12; l++) {
memset(t, 0, sizeof(t));
dfs(1);
}
cout << q << endl;
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
return 0;
}
| 3 |
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end
struct BIT{
int n; ll m=0; vector<ll> val;
BIT(int _n):n(_n),val(_n+10){}
void clear(){val.assign(n+10,0); m=0;}
void add(int i,ll x){
for(i++;i<=n;i+=(i&-i))val[i]+=x;
m+=x;
}
ll sum(int i){
ll res=0;
for(i++;i;i-=(i&-i))res+=val[i];
return res;
}
};
int main(){
int n; cin>>n;
vector<int> x(n),y(n);
rep(i,0,n)cin>>x[i]>>y[i];
vector<int> res(n),ord(n);
iota(ALL(ord),0);
sort(ALL(ord),[&](int i,int j){return x[i]<x[j];});
queue<int> ids; int ub=inf,cnt=0;
BIT b1(n+10),b2(n+10);
rep(i,0,n)b1.add(y[i],1);
for(int i:ord){
chmin(ub,y[i]); cnt++;
b1.add(y[i],-1); b2.add(y[i],1);
ids.push(i);
if(b1.sum(ub)+(b2.m-b2.sum(ub-1))==n){
while(!ids.empty()){
res[ids.front()]=cnt; ids.pop();
}
cnt=0;
}
}
while(!ids.empty()){
res[ids.front()]=cnt; ids.pop();
}
rep(i,0,n)cout<<res[i]<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long sum(long long l, long long r) {
return (l + r) * (r - l + 1) / 2 % mod;
}
int solve(int x1, int x2, int y1, int y2, int k, int p, int add) {
x1 = max(1, x1);
x2 = min(x2, (1 << p));
y1 = max(1, y1);
y2 = min(y2, (1 << p));
if (x1 > x2 || y1 > y2) return 0;
if (x2 - x1 < y2 - y1) return solve(y1, y2, x1, x2, k, p, add);
if (x1 == 1 && x2 == (1 << p)) {
long long cnt = (y2 - y1 + 1);
long long l = add + 1;
long long r = min((long long)k, (long long)add + (long long)(1 << p));
if (l > r) return 0;
return cnt * sum(l, r) % mod;
}
int ans = 0, h = (1 << (p - 1));
ans = (ans + solve(x1, x2, y1, y2, k, p - 1, add)) % mod;
ans = (ans + solve(x1 - h, x2 - h, y1, y2, k, p - 1, add + h)) % mod;
ans = (ans + solve(x1, x2, y1 - h, y2 - h, k, p - 1, add + h)) % mod;
ans = (ans + solve(x1 - h, x2 - h, y1 - h, y2 - h, k, p - 1, add)) % mod;
return ans;
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
int x1, x2, y1, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
printf("%d\n", solve(x1, x2, y1, y2, k, 30, 0));
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool used[2011];
int b[2011];
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> b[i];
int cand;
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = n; j; j--) {
if (used[j + k]) cnt++;
if (!used[j])
if (b[j] == cnt) cand = j;
}
cout << cand << " ";
used[cand] = true;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
int x = 7 % n;
for (int i = 0; i < n; i++){
if (!x){
cout << i+1 << endl;
return 0;
}
x = (x*10+7)%n;
}
cout << -1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const long long MAX = 2e9 + 7;
const long long mod = 1e9 + 7;
const long long maxn = 1e3 + 7;
int n;
string s[maxn];
set<string> ss;
int jude(string s) {
set<char> q;
int a = s.size();
for (int i = 0; i < s.size(); i++) q.insert(s[i]);
if (q.size() == a)
return 1;
else
return 0;
}
int main() {
cin >> n;
int k = 0;
for (int i = 0; i < n; i++) {
cin >> s[i];
sort(s[i].begin(), s[i].end());
s[i].erase(unique(s[i].begin(), s[i].end()), s[i].end());
int flag = 1;
if (jude(s[i])) ss.insert(s[i]);
}
cout << ss.size() << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300007;
int p[N];
bool visited[N];
bool roads[N];
vector<int> ans;
int n, k, d;
vector<pair<int, int> > v[N];
queue<int> Q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(visited, 0, sizeof(visited));
memset(roads, 0, sizeof(roads));
cin >> n >> k >> d;
int use, c1, c2;
for (int i = 1; i <= k; i++) {
cin >> use;
visited[use] = 1;
p[use] = use;
Q.push(use);
}
for (int i = 1; i <= n - 1; i++) {
cin >> c1 >> c2;
v[c1].push_back({c2, i});
v[c2].push_back({c1, i});
}
while (!Q.empty()) {
int c = Q.front();
Q.pop();
for (int i = 0; i < v[c].size(); i++) {
if (!visited[v[c][i].first]) {
visited[v[c][i].first] = 1;
p[v[c][i].first] = p[c];
Q.push(v[c][i].first);
} else if (p[v[c][i].first] != p[c] && !roads[v[c][i].second]) {
roads[v[c][i].second] = 1;
ans.push_back(v[c][i].second);
}
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll m = 999;
string S;
cin >> S;
for (ll j=0; j < S.size(); ++j) {
ll i = stoi(S.substr(j, 3));
m = min(m, abs(753-i));
}
cout << m << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool mas[100005];
int mapa[100005];
bool reg(int a) {
for (int i = 2; i * i <= a; i++) {
if (a % i == 0) {
if (mapa[i]) {
cout << "Conflict with " << mapa[i] << endl;
return false;
}
if (mapa[a / i]) {
cout << "Conflict with " << mapa[a / i] << endl;
return false;
}
}
}
if (mapa[a]) {
cout << "Conflict with " << mapa[a] << endl;
return false;
}
return true;
}
void fill(int a) {
for (int i = 2; i * i <= a; i++) {
if (a % i == 0) {
mapa[i] = a;
mapa[a / i] = a;
}
}
mapa[a] = a;
}
void clear(int a) {
for (int i = 2; i * i <= a; i++) {
if (a % i == 0) {
mapa[i] = 0;
mapa[a / i] = 0;
}
}
mapa[a] = 0;
}
int main() {
int n, m;
cin >> n >> m;
char sign;
int number;
for (int i = 0; i < m; i++) {
cin >> sign >> number;
if (sign == '+') {
if (mas[number]) {
cout << "Already on" << endl;
} else {
if (reg(number)) {
fill(number);
cout << "Success" << endl;
mas[number] = true;
}
}
}
if (sign == '-') {
if (mas[number]) {
clear(number);
cout << "Success" << endl;
mas[number] = false;
} else {
cout << "Already off" << endl;
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char c = getchar();
long long num = 0, f = 1;
for (; !isdigit(c); c = getchar()) f = c == '-' ? -1 : f;
for (; isdigit(c); c = getchar()) num = num * 10 + c - '0';
return num * f;
}
const int N = 2e5 + 5;
struct Edge {
long long u, v, w;
bool operator<(const Edge &A) const { return w < A.w; }
} edge[N * 2];
long long a[N], fa[N];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
int n = read(), m = read();
long long mn = 1234678951222311ll, pos;
for (int i = 1; i <= n; ++i) {
a[i] = read();
if (a[i] < mn) mn = a[i], pos = i;
}
for (int i = 1; i <= m; ++i)
edge[i].u = read(), edge[i].v = read(), edge[i].w = read();
int c = m;
for (int i = 1; i <= n; ++i) {
if (i == pos) continue;
edge[++c].u = i, edge[c].v = pos, edge[c].w = mn + a[i];
}
sort(edge + 1, edge + c + 1);
for (int i = 1; i <= n; ++i) fa[i] = i;
long long ans = 0, u, v, w, x, y;
for (int i = 1; i <= c; ++i) {
u = edge[i].u, v = edge[i].v, w = edge[i].w;
x = find(u), y = find(v);
if (x == y) continue;
fa[x] = y;
ans += w;
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int a[10000005];
long long GCD(long long a, long long b) {
if (b == 0) return a;
return GCD(b, a % b);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
long long S = 0;
for (int i = 0; i < n; i++) S += a[i];
long long D = 0, T = S;
for (int i = 0; i < n; i++) {
T -= a[i];
D += T - 1LL * (n - i - 1) * a[i];
}
T = S + 2 * D;
long long k = GCD(n, T);
cout << T / k << " " << n / k << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int tot = 0, fh = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') fh = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
tot = tot * 10 + c - '0';
c = getchar();
}
return tot * fh;
}
void getstr(char *a) {
char c = getchar();
int len = 0;
while (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9')))
c = getchar();
while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9')) {
a[++len] = c;
c = getchar();
}
}
char s[100000];
int q, n;
bool flag;
int main() {
q = read();
while (q--) {
flag = false;
n = read();
getstr(s);
if (n == 1)
cout << "NO"
<< "\n";
if (n == 2) {
if (s[1] >= s[2])
cout << "NO"
<< "\n";
else
flag = true;
}
if (n >= 3) flag = true;
if (flag) {
cout << "YES"
<< "\n";
cout << 2 << "\n";
cout << s[1] << " ";
for (int i = 2; i <= n; i++) cout << s[i];
cout << "\n";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int LIM = 3002;
int n, m, a, b;
ll g0, x, y, z, ar[LIM][LIM], mn[LIM][LIM], ans;
deque<int> dq;
void prep() {
for (int i = 1; i <= n; ++i) {
dq.clear();
for (int j = 1; j <= m; ++j) {
while (!dq.empty() && dq.front() <= j - b) dq.pop_front();
while (!dq.empty() && ar[i][dq.back()] >= ar[i][j]) dq.pop_back();
dq.push_back(j);
if (j >= b) mn[i][j] = ar[i][dq.front()];
}
}
}
void solve() {
for (int j = b; j <= m; ++j) {
dq.clear();
for (int i = 1; i <= n; ++i) {
while (!dq.empty() && dq.front() <= i - a) dq.pop_front();
while (!dq.empty() && mn[dq.back()][j] >= mn[i][j]) dq.pop_back();
dq.push_back(i);
if (i >= a) ans += mn[dq.front()][j];
}
}
}
int main() {
cin >> n >> m >> a >> b >> g0 >> x >> y >> z;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
ar[i][j] = g0;
((g0 *= x) += y) %= z;
}
prep();
solve();
cout << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int n, m;
int vis[MAXN], parent[MAXN], cnt[MAXN], rest[MAXN];
int level[MAXN];
bool is_inter[MAXN];
vector<vector<int>> adj;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bool is_interesting(int u) {
vis[u] = 1;
for (auto v : adj[u]) {
if (vis[v] == 2) {
return false;
} else if (vis[v] == 1)
continue;
else if (!is_interesting(v))
return false;
}
vis[u] = 2;
return true;
}
int find_root() {
uniform_int_distribution<> dis(0, n - 1);
for (int i = 0; i < 100; i++) {
int u = dis(rng);
fill(vis, vis + n, 0);
if (is_interesting(u)) return u;
}
return -1;
}
pair<int, int> dfs(int u) {
vis[u] = 1;
cnt[u] = 0;
parent[u] = u;
for (auto v : adj[u]) {
if (vis[v] == 1) {
cnt[u]++;
rest[v]++;
if (level[v] < level[parent[u]]) parent[u] = v;
} else {
level[v] = level[u] + 1;
int _cnt, p;
tie(_cnt, p) = dfs(v);
cnt[u] += _cnt;
if (level[p] < level[parent[u]]) {
parent[u] = p;
}
}
}
cnt[u] -= rest[u];
return {cnt[u], parent[u]};
}
int get_ans(int u) {
vis[u] = 1;
int ans = 0;
if (cnt[u] == 0) {
ans++;
is_inter[u] = true;
}
if (cnt[u] == 1) {
if (is_inter[parent[u]]) {
ans++;
is_inter[u] = true;
}
}
int p = -1;
for (auto v : adj[u]) {
if (vis[v])
p = v;
else
ans += get_ans(v);
}
return ans;
}
int main() {
;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
cin >> n >> m;
adj.clear();
adj.resize(n);
fill(is_inter, is_inter + n, false);
fill(cnt, cnt + n, 0);
fill(rest, rest + n, 0);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
}
int root = find_root();
if (root == -1) {
cout << "-1\n";
continue;
}
fill(vis, vis + n, 0);
level[root] = 0;
dfs(root);
fill(vis, vis + n, 0);
int cnt = get_ans(root);
if (5 * cnt < n)
cout << "-1\n";
else {
for (int u = 0; u < n; u++)
if (is_inter[u]) cout << u + 1 << ' ';
cout << '\n';
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, y, x, z, h, max = 0, m, t, i, j, k, cnt = 0, cnt1 = 0, sum = 0,
flag = 0, flag1 = 0;
cin >> n;
i = 1;
while (1) {
z = (2 * i) - 1;
if (z >= n) {
cout << i << endl;
break;
}
i++;
}
long long a = 1, b = 1;
for (i = 0; i < n; i++) {
cout << a << " " << b << endl;
if (i % 2 == 1) {
b++;
} else {
a++;
}
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
sort(a,a+n);
int l=1,r=n-1,tl=0,tr=0;
ll ans=0;
int flag=1;
while(l<=r)
{
long long k1=abs(a[tl]-a[l]),k2=abs(a[tl]-a[r]),k3=abs(a[tr]-a[l]),k4=abs(a[tr]-a[r]);
if(k1>=k2&&k1>=k3&&k1>=k4)
{
ans+=k1;
tl=l;l++;
}
else if(k2>=k1&&k2>=k3&&k2>=k4)
{
ans+=k2;
tl=r;r--;
}
else if(k3>=k1&&k3>=k2&&k3>=k4)
{
ans+=k3;
tr=l;l++;
}
else if(k4>=k1&&k4>=k2&&k4>=k3)
{
ans+=k4;
tr=r;r--;
}
}
printf("%lld\n",ans);
}
| 0 |
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef pair<int,int> P;
P p[3000];
int n;
int ans;
int main(){
while(cin>>n&&n){
ans=0;
for(int i=0;i<n;i++)
scanf("%d %d",&p[i].first,&p[i].second);
sort(p,p+n);
for(int i=0;i+1<n;i++){
for(int j=i+1;j<n;j++){
P a=p[i],b=p[j],c,d;
c.first = a.first + a.second - b.second;
c.second = a.second + b.first - a.first;
d.first = b.first + a.second - b.second;
d.second = b.second + b.first - a.first;
if(binary_search(p,p+n,c) == false)continue;
if(binary_search(p,p+n,d) == false)continue;
int d1 = (a.first-b.first);
int d2 = (a.second-b.second);
int d3 = d1*d1 + d2*d2;
ans=max(ans,d3);
}
}
cout<<ans<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct data {
int h, l, r, x;
data(int h, int l, int r, int x) : h(h), l(l), r(r), x(x) {}
};
vector<data> v;
bool mark[1000005];
int L[7005], R[7005], Log[200005];
int main() {
ios_base::sync_with_stdio(false);
for (int i = 1; i <= 200000; i *= 2) Log[i]++;
for (int i = 1; i <= 200000; i++) Log[i] += Log[i - 1];
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int c, h, l, r, x, id;
cin >> c;
if (c == 1) {
cin >> h >> l >> r >> x;
v.push_back(data(h, l, r, x));
} else {
int ans = 0;
cin >> h >> id;
L[h] = R[h] = id;
for (int i = h + 1; i <= n; i++) {
L[i] = L[i - 1] + Log[L[i - 1] - 1];
R[i] = R[i - 1] + Log[R[i - 1]];
}
for (data u : v) mark[u.x] |= u.h >= h && u.l <= R[u.h] && u.r >= L[u.h];
for (data u : v)
if (mark[u.x]) ans++, mark[u.x] = false;
cout << ans << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
while (q--) {
int n, m;
cin >> n >> m;
int arr[1010];
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int abb[1010];
for (int i = 0; i < m; i++) {
cin >> abb[i];
}
sort(abb, abb + m);
int acc[1010];
while (1) {
for (int i = 1; i <= n; i++) {
acc[i] = arr[i];
}
for (int i = m - 1; i >= 0; i--) {
int a = abb[i];
if (arr[a] > arr[a + 1]) {
swap(arr[a], arr[a + 1]);
}
}
bool ef = false;
for (int i = 1; i <= n; i++) {
if (acc[i] != arr[i]) {
ef = true;
break;
}
}
if (!ef) {
break;
}
}
bool gf = false;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[i + 1]) gf = true;
}
if (gf)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
| 2 |
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;
int main()
{
string s;
cin >> s;
int n = s.size();
int i = 0;
int len = 0;
char ret;
while(i < n){
int tmp = 1;
char prev = ' ';
while(i < n && s[i] != prev){
prev = s[i];
if(s[i] == 'e')
i += 3;
else
i += 7;
++ tmp;
}
if(tmp > len){
len = tmp;
ret = prev;
}
}
if(ret == 'e')
cout << "egg" << endl;
else
cout << "chicken" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
const int N = 120005;
struct node_t {
int tag, time, value, number;
ll answer;
} tree[N << 2];
int n, m, top_max, top_min, a[N], stack_max[N], stack_min[N];
vector<pair<int, int>> queries[N];
ll answer[N];
static void add_value(int x, int value) {
tree[x].value += value;
tree[x].tag += value;
}
static void add_time(int x, int value) {
tree[x].time += value;
tree[x].answer += (ll)value * tree[x].number;
}
static void push_up(int x) {
tree[x].value = min(tree[x << 1].value, tree[x << 1 | 1].value);
tree[x].answer = tree[x << 1].answer + tree[x << 1 | 1].answer;
tree[x].number = 0;
if (tree[x].value == tree[x << 1].value) {
tree[x].number += tree[x << 1].number;
}
if (tree[x].value == tree[x << 1 | 1].value) {
tree[x].number += tree[x << 1 | 1].number;
}
}
static void push_down(int x) {
if (tree[x].tag) {
add_value(x << 1, tree[x].tag);
add_value(x << 1 | 1, tree[x].tag);
tree[x].tag = 0;
}
if (tree[x].time) {
if (tree[x].value == tree[x << 1].value) {
add_time(x << 1, tree[x].time);
}
if (tree[x].value == tree[x << 1 | 1].value) {
add_time(x << 1 | 1, tree[x].time);
}
tree[x].time = 0;
}
}
static void build(int x, int l, int r) {
tree[x].value = l;
tree[x].number = 1;
if (l == r) {
return;
}
int mid = (l + r) / 2;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
}
static void modify(int x, int l, int r, int ql, int qr, int value) {
if (l == ql && r == qr) {
add_value(x, value);
return;
}
int mid = (l + r) / 2;
push_down(x);
if (qr <= mid) {
modify(x << 1, l, mid, ql, qr, value);
} else if (ql > mid) {
modify(x << 1 | 1, mid + 1, r, ql, qr, value);
} else {
modify(x << 1, l, mid, ql, mid, value);
modify(x << 1 | 1, mid + 1, r, mid + 1, qr, value);
}
push_up(x);
}
static ll query(int x, int l, int r, int ql, int qr) {
if (l == ql && r == qr) {
return tree[x].answer;
}
int mid = (l + r) / 2;
push_down(x);
if (qr <= mid) {
return query(x << 1, l, mid, ql, qr);
} else if (ql > mid) {
return query(x << 1 | 1, mid + 1, r, ql, qr);
} else {
return query(x << 1, l, mid, ql, mid) +
query(x << 1 | 1, mid + 1, r, mid + 1, qr);
}
}
void solve() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
cin >> m;
for (int i = 1; i <= m; ++i) {
int l, r;
cin >> l >> r;
queries[r].push_back(make_pair(l, i));
}
build(1, 1, n);
for (int i = 1; i <= n; ++i) {
add_value(1, -1);
while (top_max && a[stack_max[top_max]] < a[i]) {
modify(1, 1, n, stack_max[top_max - 1] + 1, stack_max[top_max],
a[i] - a[stack_max[top_max]]);
--top_max;
}
stack_max[++top_max] = i;
while (top_min && a[stack_min[top_min]] > a[i]) {
modify(1, 1, n, stack_min[top_min - 1] + 1, stack_min[top_min],
a[stack_min[top_min]] - a[i]);
--top_min;
}
stack_min[++top_min] = i;
add_time(1, 1);
for (auto q : queries[i]) {
answer[q.second] = query(1, 1, n, q.first, i);
}
}
for (int i = 1; i <= m; ++i) {
cout << answer[i] << '\n';
}
}
int main() {
solve();
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main(){
map<string,int> day;
day["SUN"] = 7;
day["MON"] = 6;
day["TUE"] = 5;
day["WED"] = 4;
day["THU"] = 3;
day["FRI"] = 2;
day["SAT"] = 1;
string s; cin >> s;
cout << day[s];
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 200001;
long long n, k;
string s;
int pv[N];
long long memo[N];
vector<long long> segTree(4 * N);
void update(int ptr, int l, int r, int i, long long val) {
if (r < i || l > i) return;
if (l == r) {
segTree[ptr] = val;
return;
}
update(ptr * 2, l, (l + r) / 2, i, val);
update(ptr * 2 + 1, (l + r) / 2 + 1, r, i, val);
segTree[ptr] = min(segTree[ptr * 2], segTree[ptr * 2 + 1]);
}
long long getVal(int ptr, int l, int r, int i, int j) {
if (r < i || l > j) return numeric_limits<long long>::max();
if (i <= l && r <= j) return segTree[ptr];
long long lVal = getVal(ptr * 2, l, (l + r) / 2, i, j);
long long rVal = getVal(ptr * 2 + 1, (l + r) / 2 + 1, r, i, j);
return min(lVal, rVal);
}
long long dp(int i) {
long long& ret = memo[i];
if (ret != -1) return ret;
if (i == 0) return ret = 0;
ret = numeric_limits<long long>::max();
if (pv[i] == -1) {
ret = i;
ret += dp(i - 1);
update(1, 0, n, i, ret);
return ret;
}
ret = getVal(1, 0, n, max(0ll, pv[i] - k - 1), i - 1);
ret += pv[i];
update(1, 0, n, i, ret);
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(memo, -1, sizeof memo);
cin >> n >> k >> s;
deque<int> v;
for (int i = 1; i <= n; i++) {
if (!v.empty() && v.front() < i - k) v.pop_front();
if (s[i - 1] == '1') v.push_back(i);
if (v.empty())
pv[i] = -1;
else
pv[i] = v.front();
}
for (int i = 1; i <= n; i++) dp(i);
cout << dp(n);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
long long count[200001] = {0};
long long m = 0;
long long hf = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
count[a[i]]++;
m = max(count[a[i]], m);
if (count[a[i]] == m) {
hf = a[i];
}
}
vector<long long> p;
for (long long i = 0; i < n; i++) {
if (a[i] == hf) {
p.push_back(i);
}
}
long long last = -1;
cout << n - m << endl;
for (long long i = 0; i < p.size(); i++) {
if (p[i] == 0) {
last = p[i];
continue;
}
if (p[i] == last + 1) {
last = p[i];
continue;
}
for (long long j = p[i] - 1; j > last; j--) {
if (a[j] < hf) {
cout << 1 << " " << j + 1 << " " << j + 2 << endl;
} else {
cout << 2 << " " << j + 1 << " " << j + 2 << endl;
}
}
last = p[i];
}
if (p[p.size() - 1] != n - 1) {
for (long long j = p[p.size() - 1] + 1; j < n; j++) {
if (a[j] < hf) {
cout << 1 << " " << j + 1 << " " << j << endl;
} else {
cout << 2 << " " << j + 1 << " " << j << endl;
}
}
}
}
| 4 |
#include <iostream>
using namespace std;
int W,H;
int x[4]={0,0,1,-1};
int y[4]={1,-1,0,0};
int i,j;
int count=0;
char room[100][100];
int dfs(int, int);
int main(){
while(1){
int sy,sx;
cin >> W >> H;
if(W==0&&H==0)break;
for(i = 0 ; i < H ; i++){
for(j = 0 ; j < W ; j++){
cin >> room[i][j];
if(room[i][j] == '@'){
sy = i;
sx = j;
}
}
}
cout << dfs(sx,sy) << endl;
}
return 0;
}
int dfs(int ax,int ay){
if(ax<0||ay<0)return 0;
if(ax>=W||ay>=H)return 0;
if(room[ay][ax]=='#')return 0;
room[ay][ax]='#';
int cnt=1;
int i;
for(i=0;i<4;i++){
int nx=ax+x[i];
int ny=ay+y[i];
cnt+=dfs(nx,ny);
}
return cnt;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;cin>>s;
int cnt = 0;
for(int i=0;i<s.size();i++) if(s[i] == '2') cnt += 1;
cout<<cnt<<endl;
return 0;
}
| 0 |
#include<cstdio>
using namespace std;
typedef long long LL;
const int N=100005;
struct node { int y,z,next; } a[N<<1];
LL an;
int s[3],head[N],z[N],i,j,k,n,m,S,E;
bool p[N],F;
char c;
inline char getc()
{
#define VV 10000000
static char s[VV],*l=s,*r=s;
if (l==r)
l=s,r=s+fread(s,1,VV,stdin);
return l==r?0:*l++;
}
int read(){ int z=0; do c=getc(); while (c<'0'||c>'9'); while (c>='0'&&c<='9') z*=10,z+=c-'0',c=getc(); return z; }
void jb(int y,int x)
{
a[++k]=(node){y,1,head[x]},head[x]=k,
a[++k]=(node){x,2,head[y]},head[y]=k;
}
void dfs(int w)
{
p[w]=1,++s[z[w]],++S;
for (register int j=head[w];j;j=a[j].next)
{
int c=a[j].y;
if (j&1) ++E;
if (!p[c])
z[c]=(z[w]+a[j].z)%3,dfs(c);
else
if (z[c]!=(z[w]+a[j].z)%3)
F=0;
}
}
int main()
{
n=read(),m=read();
while (m--) jb(read(),read());
for (i=1;i<=n;++i)
if (!p[i])
{
F=1,s[0]=s[1]=s[2]=S=E=0;
dfs(i);
if (F)
if (s[0]&&s[1]&&s[2])
an+=(LL)s[0]*s[1]+(LL)s[1]*s[2]+(LL)s[2]*s[0];
else
an+=E;
else
an+=(LL)S*S;
}
printf("%lld\n",an);
return 0;
}
| 0 |
#include<iostream>
#define MOD 1000000007
using namespace std;
int n,i,Dp[200005],Sum[200005],prv,x;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
Dp[0]=1;
prv=0;
for(i=1; i<=n; i++)
{
cin>>x;
if(prv!=x)
Sum[x]=(Sum[x]+Dp[i-1])%MOD;
Dp[i]=Sum[x];
prv=x;
}
cout<<Dp[n]<<"\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
static int ans[1000100];
scanf("%d%d", &n, &k);
if (n / k <= 2) {
printf("-1\n");
} else {
for (int i = 1; i <= k; ++i) {
ans[i] = i + 1 > k ? 1 : i + 1;
}
for (int i = k + 1; i <= n; ++i) {
ans[i] = i % k == 0 ? k : i % k;
}
for (int i = 1; i <= n; ++i) {
printf("%d%s", ans[i], i == n ? "\n" : " ");
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
pair<int, int> a[N], b[N], c[N];
multiset<int> L, R;
vector<pair<int, int> > S;
int sub[N], pre[N];
int main() {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
int A = 0, B = 0, C = 0;
for (int i = 1; i <= n; i++) {
int t, y, u;
scanf("%d %d %d", &t, &y, &u);
if (y == 1 && u == 1)
a[++A] = {t, i};
else if (y == 1)
b[++B] = {t, i};
else if (u == 1)
c[++C] = {t, i};
else
R.insert(t), S.push_back({t, i});
}
sort(a + 1, a + A + 1);
a[0] = {0, 0};
sort(b + 1, b + B + 1);
b[0] = {0, 0};
sort(c + 1, c + C + 1);
c[0] = {0, 0};
int x = min(B, C), sum = 0;
for (int i = B; i > x; i--) R.insert(b[i].first);
for (int i = C; i > x; i--) R.insert(c[i].first);
for (int i = 0; i <= x; i++) sub[i] = 2e9 + 1;
for (int i = 1; i <= x; i++) pre[i] = pre[i - 1] + b[i].first + c[i].first;
for (int i = x; i >= 0; i--) {
bool f = true;
if (2 * i > m) f = false;
int x = m - 2 * i;
if (k > i) {
if (k - i > A) f = false;
x -= k - i;
}
if (x < 0) f = false;
if (L.size() + R.size() < x) f = false;
if (f) {
while (L.size() < x) {
sum += *R.begin();
L.insert(*R.begin());
R.erase(R.begin());
}
while (L.size() > x) {
sum -= *L.rbegin();
R.insert(*L.rbegin());
L.erase(--L.end());
}
sub[i] = sum + pre[i];
}
if (i == 0) break;
if (R.size() && b[i].first >= *R.begin())
R.insert(b[i].first);
else
L.insert(b[i].first), sum += b[i].first;
if (R.size() && c[i].first >= *R.begin())
R.insert(c[i].first);
else
L.insert(c[i].first), sum += c[i].first;
}
int ans = 2e9 + 1, pos, now = 0;
for (int i = 0; i <= A; i++) {
now += a[i].first;
if (i > m) break;
if (i >= k) {
int x = m - i;
if (L.size() + R.size() < x) continue;
while (L.size() < x) {
sum += *R.begin();
L.insert(*R.begin());
R.erase(R.begin());
}
while (L.size() > x) {
sum -= *L.rbegin();
R.insert(*L.rbegin());
L.erase(--L.end());
}
int res = sum + now;
if (ans > res) ans = res, pos = i;
} else {
if (k - i > min(B, C)) continue;
long long res = now + 0ll + sub[k - i];
if (ans > res) ans = res, pos = i;
}
}
if (ans > 2e9) {
puts("-1");
return 0;
}
printf("%d\n", ans);
vector<int> V;
for (int i = 1; i <= pos; i++) V.push_back(a[i].second);
for (int i = 1; i <= k - pos; i++)
V.push_back(b[i].second), V.push_back(c[i].second);
for (int i = max(1, k - pos + 1); i <= B; i++) S.push_back(b[i]);
for (int i = max(1, k - pos + 1); i <= C; i++) S.push_back(c[i]);
sort(S.begin(), S.end());
for (int i = 0; i < m - pos - max(0, k - pos) * 2; i++)
V.push_back(S[i].second);
int siz = V.size();
for (int i = 0; i < siz; i++) printf("%d%c", V[i], i == siz - 1 ? '\n' : ' ');
return 0;
}
| 5 |
#include <bits/stdc++.h>
long long p, k, a[1000001], d;
int main() {
scanf("%lld%lld", &p, &k);
k = -k;
while (p) {
a[++d] = p % k, p /= k;
if (a[d] < 0) a[d] = -k + a[d], ++p;
}
printf("%lld\n", d);
for (register int i = 1; i <= d; ++i) printf("%lld ", a[i]);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int arr[100001];
int main() {
ios::sync_with_stdio(false);
;
cin.tie(0);
;
long long int i, j, n, x, y, ans = 0, cnt = 0, cnt1 = 0;
cin >> n;
for (i = 1; i <= n; i++) cin >> arr[i];
for (i = 1; i <= n; i++) {
if (arr[i] > 0)
ans += (arr[i] - 1);
else if (arr[i] < 0) {
ans += (-arr[i] - 1);
cnt++;
} else
cnt1++;
}
if (cnt % 2 == 0 || cnt1 > 0) {
ans += cnt1;
cout << ans;
return 0;
}
cout << ans + 2;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
long long int pow(long long int i, long long int z, long long int rem) {
long long int num = 1;
while (z) {
if (z & 1) num *= i;
i *= i;
num %= rem;
i %= rem;
z >>= 1;
}
return num;
}
int main() {
int n;
cin >> n;
if (n > 4) {
if (!isPrime(n)) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
cout << 1 << endl;
int z = n - 2;
for (int i = 1; i < n - 1; i++) cout << 1 + pow(i, z, n) % n << endl;
cout << n << endl;
}
} else {
cout << "YES" << endl;
if (n == 4)
cout << 1 << endl << 3 << endl << 2 << endl << 4 << endl;
else {
for (int i = 1; i <= n; i++) cout << i << endl;
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
char a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> a[i][j];
}
vector<int> v1, v2;
bool f = true, c = 0;
int row, col;
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < m - 1; j++) {
if (a[i][j] == '*' && a[i - 1][j] == '*' && a[i][j - 1] == '*' &&
a[i + 1][j] == '*' && a[i][j + 1] == '*') {
row = i;
col = j;
c++;
}
}
}
if (c == 1) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '*' && row != i && col != j) f = false;
}
}
if (f == true) {
for (int i = 0; i < n; i++) {
if (a[i][col] == '*') v1.push_back(i);
}
for (int j = 0; j < m; j++) {
if (a[row][j] == '*') v2.push_back(j);
}
for (int i = 1; i < v1.size(); i++) {
if (v1[i] - v1[i - 1] != 1) f = false;
}
for (int i = 1; i < v2.size(); i++) {
if (v2[i] - v2[i - 1] != 1) f = false;
}
}
} else
f = false;
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void sum(int *x, int *p, int n, int *r) {
int s1 = 0;
int s2 = 0;
int size1 = 0;
for (int i = 0; i < n; i++) {
if (p[i] == 0) {
s1 += x[i];
size1++;
} else
s2 += x[i];
}
r[0] = s1;
r[1] = s2;
r[2] = size1;
}
void print_ar(int *x, int *p, int n) {
for (int i = 0; i < n; i++) {
if (!p[i]) printf("%d ", x[i]);
}
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
int x[n];
int p[n];
int s[3];
int ww = 0;
for (int i = 1; i <= n; i++) {
x[i - 1] = i;
p[i - 1] = ww;
ww = (ww + 1) % 2;
}
int diff;
sum(x, p, n, s);
diff = s[0] - s[1];
while (abs(diff) > 1) {
if (diff > 0) {
for (int i = 0; i < n; i++) {
if (p[i] == 0) {
p[i] = 1;
break;
}
}
} else if (diff < 0) {
for (int i = 0; i < n; i++) {
if (p[i] == 1) {
p[i] = 0;
break;
}
}
}
sum(x, p, n, s);
diff = s[0] - s[1];
}
printf("%d\n", abs(diff));
printf("%d ", s[2]);
print_ar(x, p, n);
return 0;
}
| 3 |
#include <iostream>
using namespace std;
typedef long long ll;
const int INF = 1000100000;
#define rep(i,n) for(int i=0;i<n;++i)
ll max(ll x,ll y){
if(x>y)return x;
else return y;
}
ll min(ll x,ll y){
if(x>y)return y;
else return x;
}
int main(){
ll h,w;
cin >>h >> w;
int ans;
ll smax,smin;
if((h*w)%3==0)ans=0;
else ans=min(h,w);
for(ll i=1;i<h;i++){
smin=min(i*w,(w/2)*(h-i));
smax=max(i*w,((w+1)/2)*(h-i));
ans=min(ans,smax-smin);
}
for(ll i=1;i<w;i++){
smin=min(i*h,h/2*(w-i));
smax=max(i*h,(h+1)/2*(w-i));
ans=min(ans,smax-smin);
}
cout << ans << endl;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
#define PB push_back
typedef long long LL;
const LL P = 1e9+7;
const LL INF = 1e18;
const int N = 1e6+5;
const double pi = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, k;
void print(int i,int top,int diff){
cout<<i-diff<<" "<<top-i-diff<<" "<<top-diff<<endl;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
if(n<k*2-1){
cout<<"-1\n";
return 0;
}
if(n%2==1){
int k1 = n+1>>1;
int top = k1 + 3 * n - 1;
rep(i,n,n+k1-1){
print(i,top,k1-k);
top--;
}
rep(i,k1,n-1){
print(i,top,k1-k);
top--;
}
}else{
int k1 = n>>1;
int top = k1 + 3 * n - 1;
rep(i,n,n+k1-1){
print(i,top,k1-k);
top--;
}
rep(i,k1+1,n-1){
print(i,top,k1-k);
top--;
}
cout<<k<<" "<<top-n-(k1-k)<<" "<<top-(k1-k)<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long T = clock();
long long t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long c = 0, d = 0;
for (long long i = 0; i < n; i++) {
long long x;
cin >> x;
if (x % 2) {
d++;
} else {
c++;
}
}
if (c == n || d == n) {
cout << "YES" << ("\n");
;
} else {
cout << "NO" << ("\n");
;
}
}
cerr << "\nTIME: " << (long double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
T = clock();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long b, long long s) { return (s != 0) ? gcd(s, b % s) : b; }
long long Pow(long long a, long long b, long long c) {
if (b == 0)
return 1 % c;
else if (b == 1)
return a % c;
else {
long long A = Pow(a, b / 2, c);
A = (A * A) % c;
if (b & 1) A = (A * a) % c;
return A;
}
}
const long long INF = 1e9 + 7;
long long fac[200005], inv[200005];
long long f(long long h, long long w) {
long long ret = (fac[h + w] * inv[h]) % INF;
return (ret * inv[w]) % INF;
}
const int N = 2005;
pair<int, int> p[N];
long long d[N], x[N], y[N];
int main() {
fac[0] = 1;
inv[0] = 1;
for (int i = 1; i <= 200000; i++) {
fac[i] = (fac[i - 1] * i) % INF;
inv[i] = Pow(fac[i], INF - 2, INF);
}
int h, w, n;
scanf("%d %d %d", &h, &w, &n);
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
p[i] = pair<int, int>(x, y);
}
sort(p, p + n);
long long ret = f(h - 1, w - 1);
for (int i = 0; i < n; i++) {
d[i] = (d[i] + f(p[i].first - 1, p[i].second - 1)) % INF;
for (int j = i + 1; j < n; j++) {
if ((p[j].first >= p[i].first) && (p[j].second >= p[i].second)) {
d[j] = (d[j] + INF -
(d[i] * f(p[j].first - p[i].first, p[j].second - p[i].second)) %
INF) %
INF;
}
}
ret = (ret + INF - (f((h - p[i].first), (w - p[i].second)) * d[i]) % INF) %
INF;
}
printf("%I64d\n", ret);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5 + 5;
vector<int> g[mxn];
long long n, m;
inline void solve() {
cin >> n;
long long ans = n * (n + 1) / 2;
long long t = 1, c = 0;
for (; t <= n;) {
++c;
t *= 2;
}
--t;
cout << ans - t * 2 << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
for (; T--;) solve();
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 10, MOD = 1e9 + 7, inf = 0x3f3f3f3f,
INF = 0x3f3f3f3f3f3f3f3fll;
long long dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
bitset<N> r, v, mp[N];
char ch;
long long n, m, s;
void dfs(long long x, long long y) {
mp[x][y] = 0;
long long i, j;
for (long long k = 0; k <= 3; k++) {
i = x + dx[k], j = y + dy[k];
if (!i || !j || i > n || j > m || !mp[i][j]) continue;
dfs(i, j);
}
}
void esc() {
cout << -1 << '\n';
exit(0);
}
signed main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
cin >> ch;
if (ch == '#') {
if (r[i] && !mp[i][j - 1] || v[j] && !mp[i - 1][j]) esc();
mp[i][j] = r[i] = v[j] = 1;
}
}
}
if (r.count() == n ^ v.count() == m) esc();
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
if (mp[i][j]) dfs(i, j), s++;
}
}
cout << s << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector<int> cnt(255);
int ans = 0;
for (int i = 0; i < (int)s.size(); i++) {
char c = s[i];
if (c == 'F' && cnt['M'] > 0) {
ans = max(cnt['M'], ans + 1);
}
cnt[c]++;
}
printf("%d\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int cnt, n, ans = 100000;
string a[55], temp;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
cnt = 0;
for (int j = 0; j < n; j++) {
temp = a[j] + a[j];
if (temp.find(a[i]) == string::npos) {
cout << -1 << endl;
return 0;
} else
cnt += temp.find(a[i]);
}
ans = min(ans, cnt);
}
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long double eps = 1e-9;
const long long maxn = 31;
const long long inf = 5e18;
const long long minf = -inf;
long long ans = minf, n, u, r;
vector<long long> a(maxn), b(maxn), k(maxn), p(maxn);
void recur(long long op, long long flag, vector<long long> v) {
if ((u - op) % 2 == 0) {
long long res = 0;
for (long long i = 0; i < n; ++i) {
res += v[i] * k[i];
}
ans = max(ans, res);
}
if (op == u) return;
vector<long long> here(maxn);
for (long long i = 0; i < n; ++i) {
here[i] = v[p[i] - 1] + r;
}
recur(op + 1, 0, here);
if (flag) return;
for (long long i = 0; i < n; ++i) {
here[i] = v[i] ^ b[i];
}
recur(op + 1, 1, here);
return;
}
bool solve() {
cin >> n >> u >> r;
for (long long i = 0; i < n; ++i) cin >> a[i];
for (long long i = 0; i < n; ++i) cin >> b[i];
for (long long i = 0; i < n; ++i) cin >> k[i];
for (long long i = 0; i < n; ++i) cin >> p[i];
recur(0, 0, a);
cout << ans << "\n";
return true;
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
if (solve()) {
} else {
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, k, i, j, x, y, s, p, l, f, t, t1, t2, a[110][110];
bool ok;
int main() {
cin >> n >> k;
x = y = (k + 1) / 2;
while (n--) {
cin >> t;
f = -1;
for (j = 1; j <= k; j++)
for (l = 1; l + t - 1 <= k; l++) {
s = 0;
ok = true;
for (p = l; p < l + t; p++) {
if (a[j][p] == 1) {
ok = false;
break;
}
s += abs(j - x) + abs(p - y);
}
if (ok && ((f < 0) || (s < f))) {
t1 = j;
t2 = l;
f = s;
}
}
if (f < 0)
cout << -1 << endl;
else {
cout << t1 << " " << t2 << " " << t2 + t - 1 << endl;
for (j = t2; j <= t2 + t - 1; j++) a[t1][j] = 1;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int h,w,m; cin>>h>>w>>m;
vector <int> C(w+1), R(h+1);
set < pair<int,int> > s;
int maxR=0, maxC=0;
vector <int> mR,mC;
for(int i=0 ; i<m ; i++){
int x,y; cin>>x>>y;
s.insert({x,y});
R[x]++; C[y]++;
maxR = max(maxR,R[x]);
maxC = max(maxC,C[y]);
}
for(int i=1 ; i<=h; i++) if(R[i]==maxR) mR.push_back(i);
for(int i=1 ; i<=w; i++) if(C[i]==maxC) mC.push_back(i);
int flag=0, ans = maxR + maxC - 1;;
for(auto i:mR){
for(auto j:mC){
if(s.find({i,j})==s.end()){
ans++; flag=1; break;
}
}
if(flag==1) break;
}
cout<<ans;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T> struct UnionFind {
vector<int> par;
vector<int> rank;
vector<T> ws;
UnionFind(int n = 1, T e = 0)
: par(n), rank(n), ws(n) {
init(n, e);
}
void init(int n, T e) {
for (int i = 0; i < n; ++i) {
par[i] = i;
rank[i] = 0;
ws[i] = e;
}
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
ws[x] += ws[par[x]];
return par[x] = r;
}
}
bool same(int x, int y) {
return root(x) == root(y);
}
T weight(int x) {
root(x);
return ws[x];
}
T diff(int x, int y) {
return weight(y) - weight(x);
}
bool unite(int x, int y, T w = 0) {
w -= weight(y) - weight(x);
x = root(x);
y = root(y);
if (x == y) return false;
if (rank[x] < rank[y]) {
swap(x, y);
w = negate<T>()(w);
}
par[y] = x;
ws[y] = w;
if (rank[x] == rank[y]) ++rank[x];
return true;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q; cin >> n >> q;
UnionFind<ll> uf(n);
while (q--) {
int com; cin >> com;
if (com) {
int x, y; cin >> x >> y;
cout << ( (uf.same(x, y)) ? to_string(uf.diff(x, y)) : "?" ) << endl;
} else {
int x, y, z; cin >> x >> y >> z;
uf.unite(x, y, z);
}
}
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
class Lca{
public:
vector<int> depth;
vector<vector<int>> parent;
vector<vector<int>> G;
int root;
int n;
const int N_LOG_MAX = 25;
bool initialized = false;
Lca(vector<vector<int>> g, int _root){
n = g.size();
G = g;
root = _root;
parent = vector<vector<int>>(N_LOG_MAX, vector<int>(n));
depth = vector<int>(n, 0);
}
void init(){
initialized = true;
dfs(root, -1, 0);
for(int i = 0; i < N_LOG_MAX-1; i++){
for(int v = 0; v < n; v++){
if(parent[i][v] < 0) parent[i+1][v] = -1;
else parent[i+1][v] = parent[i][parent[i][v]];
}
}
}
int lca(int u, int v){
assert(initialized);
if(depth[u] > depth[v]) swap(u, v);
for(int i = 0; i < N_LOG_MAX; i++){
if((depth[v] - depth[u]) >> i & 1){
v = parent[i][v];
}
}
if(u == v) return u;
for(int i = N_LOG_MAX-1; i >= 0; i--){
if(parent[i][u] != parent[i][v]) {
u = parent[i][u];
v = parent[i][v];
}
}
return parent[0][u];
}
private:
void dfs(int v, int p, int d){
parent[0][v] = p;
depth[v] = d;
for(int i = 0; i < G[v].size(); i++){
if(G[v][i] != p) dfs(G[v][i], v, d+1);
}
}
};
vector<int> ga[300000];
vector<int> gb[300000];
int ord_b[300000];
int inv_ord_b[300000];
int ans;
void clear(int n){
ans = 0;
for(int i = 0; i < n; i++){
ga[i].clear();
gb[i].clear();
ord_b[i] = 0;
inv_ord_b[i] = 0;
}
}
void dfs_b(int v, int &i){
ord_b[v] = i;
inv_ord_b[i] = v;
i++;
for(int to : gb[v]){
dfs_b(to, i);
}
}
void dfs_a(int v, set<int> &st, Lca &lca){
int poped = -1, pushed = -1;
if(st.empty()){
st.insert(ord_b[v]);
pushed = v;
}else{
auto p = st.upper_bound(ord_b[v]);
if(p != st.begin()){
p--;
int u = inv_ord_b[*p];
// cout << v << ' ' << u << endl;
bool u_is_parent = (lca.lca(u, v) == u);
if(u_is_parent){
st.erase(ord_b[u]);
poped = u;
}
p++;
}
p = st.upper_bound(ord_b[v]);
if(p != st.end()){
int u = inv_ord_b[*p];
bool u_is_child = (lca.lca(u, v) == v);
if(!u_is_child){
st.insert(ord_b[v]);
pushed = v;
}
}else{
st.insert(ord_b[v]);
pushed = v;
}
}
// cout << v << ':';
// for(int x : st) cout << inv_ord_b[x] << ' ';
// cout << endl;
chmax(ans, (int)st.size());
for(int to : ga[v]){
dfs_a(to, st, lca);
}
if(poped != -1) st.insert(ord_b[poped]);
if(pushed != -1) st.erase(ord_b[pushed]);
}
void solve(){
int n; cin >> n;
vector<int> a(n), b(n);
for(int i = 1; i < n; i++){
cin >> a[i]; a[i]--;
ga[a[i]].push_back(i);
}
vector<vector<int>> g(n);
for(int i = 1; i < n; i++){
cin >> b[i]; b[i]--;
gb[b[i]].push_back(i);
g[b[i]].push_back(i);
g[i].push_back(b[i]);
}
Lca lca(g, 0);
lca.init();
int i = 0;
dfs_b(0, i);
set<int> st;
dfs_a(0, st, lca);
cout << ans << endl;
clear(n);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int t; cin >> t;
while(t--) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
using D = double;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
template <typename T>
T sqr(T a) {
return a * a;
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <typename T>
T sgn(T a) {
return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
template <typename T>
T abs(T a) {
return a > 0 ? a : -a;
}
const int inf = 2e9;
const ld st = 0.000001;
const ld pi = acos((ld)-1);
const int MAXN = 100000;
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
ll n;
cin >> n;
vector<ll> a;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
a.push_back(x);
}
sort(a.rbegin(), a.rend());
vector<ll> b;
b.push_back(a[0]);
for (int i = 1; i < n; i += 2) {
b.push_back(a[i]);
}
ll s = n - 1;
if ((n - 1) % 2 != 0) {
--s;
}
for (int i = s; i > 1; i -= 2) {
b.push_back(a[i]);
}
a = b;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (a[i] >= a[i + 1] + a[n - 1]) {
return cout << "NO", 0;
}
} else if (i == n - 1) {
if (a[i] >= a[i - 1] + a[0]) {
return cout << "NO", 0;
}
} else {
if (a[i] >= a[i - 1] + a[i + 1]) {
return cout << "NO", 0;
}
}
}
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 360361;
const int n = 360360;
const int INF = 1012345678;
long long a, b;
int k, dist[MAXN];
queue<int> q;
void update(int x, int value) {
if (dist[x] == INF) {
dist[x] = value;
q.push(x);
}
}
long long BFS(int s, int t) {
fill(dist, dist + MAXN, INF);
for (update(s, 0); !q.empty(); q.pop()) {
int x = q.front();
if (x) {
update(x - 1, dist[x] + 1);
}
for (int i = 2; i <= k; i++) {
update(x / i * i, dist[x] + 1);
}
}
return dist[t];
}
long long calc(long long a, long long b) {
long long A = a / n, B = b / n;
return A == B ? BFS(a % n, b % n)
: BFS(a % n, 0) + (A - B - 1) * BFS(n, 0) + BFS(n, b % n);
}
int main() {
scanf("%I64d%I64d%d", &a, &b, &k);
printf("%I64d\n", calc(a, b));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> edges[100005];
long long a[100005];
long long dfs(int x, int p) {
int cnt = 0;
long long sum = 0, maxx = 0;
for (auto y : edges[x]) {
if (y == p) continue;
cnt++;
long long ret = dfs(y, x);
sum += ret;
maxx = max(maxx, ret);
}
if (cnt == 0) {
return a[x];
}
if (a[x] > sum || a[x] < maxx) {
printf("NO\n");
exit(0);
}
long long send = a[x] * 2 - sum;
if (p == 0 && send != 0 || send < 0) {
printf("NO\n");
exit(0);
}
return send;
}
int main() {
int n, x, y;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
if (n == 2) {
if (a[1] != a[2]) {
printf("NO\n");
} else {
printf("YES\n");
}
return 0;
}
for (int i = 1; i < n; i++) {
scanf("%d %d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (edges[i].size() >= 2) {
dfs(i, 0);
printf("YES\n");
return 0;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mm = 1e5 + 10;
char str1[mm], str2[mm];
int main() {
int n;
while (~scanf("%d", &n)) {
scanf("%s%s", str1, str2);
int a = 0, b = 0, c = 0, d = 0;
for (int i = 0; i < n; ++i) {
if (str1[i] == '0' && str2[i] == '0')
a++;
else if (str1[i] == '0' && str2[i] == '1')
b++;
else if (str1[i] == '1' && str2[i] == '0')
c++;
else
d++;
}
int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
bool falg = false;
for (x2 = 0; x2 <= b; ++x2) {
for (x4 = 0; x4 <= d; ++x4) {
x1 = x4 + n / 2 - b - d;
if (x1 < 0 || x1 > a) continue;
x3 = n / 2 - x1 - x2 - x4;
if (x3 < 0 || x3 > c) continue;
falg = true;
break;
}
if (falg) break;
}
if (!falg)
printf("-1\n");
else if (x1 + x2 + x3 + x4 != n / 2 || x1 < 0 || x2 < 0 || x3 < 0 ||
x4 < 0 || (x3 + x2 + 2 * x4 != b + d)) {
printf("-1\n");
} else {
int cnt = 0;
for (int i = 0; i < n; ++i) {
bool tag = false;
if (str1[i] == '0' && str2[i] == '0' && x1 > 0) {
x1--;
tag = true;
} else if (str1[i] == '0' && str2[i] == '1' && x2 > 0) {
x2--;
tag = true;
} else if (str1[i] == '1' && str2[i] == '0' && x3 > 0) {
x3--;
tag = true;
} else if (str1[i] == '1' && str2[i] == '1' && x4 > 0) {
x4--;
tag = true;
}
if (tag) {
cnt++;
printf("%d", i + 1);
if (cnt == n / 2) {
printf("\n");
break;
} else {
printf(" ");
}
}
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char xau[20];
bool has[15], good[260];
string str[260];
map<string, bool> res;
int a[15], mask[260];
int n, statemax;
inline bool check(int x) {
int val = x;
do {
if (!has[x % 10]) return false;
mask[val] |= 1 << (x % 10);
x /= 10;
} while (x);
return true;
}
inline string so_xau(int x) {
sprintf(xau, "%d", x);
return xau;
}
bool numb(char s[], int l, int r, int &val) {
val = 0;
if (l == r) {
val = s[l] - '0';
return good[val];
}
if (s[l] == '0') return false;
int i;
for ((i) = (l); (i) <= (r); (i)++) val = val * 10 + s[i] - '0';
return (val < 256) && good[val];
}
void palin(char s[], int len) {
int i = 0, j = len - 1;
while (i < j) {
if (s[i] != s[j]) return;
i++, j--;
}
int k, cur, x, y, z, t;
string temp;
for ((i) = (0); (i) <= (((2) < (len - 4) ? (2) : (len - 4))); (i)++)
for ((j) = (i + 1); (j) <= (((i + 3) < (len - 3) ? (i + 3) : (len - 3)));
(j)++)
for ((k) = (j + 1); (k) <= (((j + 3) < (len - 2) ? (j + 3) : (len - 2)));
(k)++)
if (len - k - 1 > 0 && len - k - 1 <= 3) {
if (numb(s, 0, i, x) && numb(s, i + 1, j, y) &&
numb(s, j + 1, k, z) && numb(s, k + 1, len - 1, t)) {
temp = str[x] + '.' + str[y] + '.' + str[z] + '.' + str[t];
res[temp] = true;
}
}
}
void palin(char s1[], char s2[], int len) {
char s[20];
int i, j;
for ((i) = 0; (i) < (len); (i)++) s[i] = s1[i];
for ((j) = (len - 1); (j) >= 0; (j)--) s[i++] = s2[j];
len += len;
i = 0, j = len - 1;
while (i < j) {
if (s[i] != s[j]) return;
i++, j--;
}
int k, cur, x, y, z, t;
string temp;
for ((i) = (0); (i) <= (((2) < (len - 4) ? (2) : (len - 4))); (i)++)
for ((j) = (i + 1); (j) <= (((i + 3) < (len - 3) ? (i + 3) : (len - 3)));
(j)++)
for ((k) = (j + 1); (k) <= (((j + 3) < (len - 2) ? (j + 3) : (len - 2)));
(k)++)
if (len - k - 1 > 0 && len - k - 1 <= 3) {
if (numb(s, 0, i, x) && numb(s, i + 1, j, y) &&
numb(s, j + 1, k, z) && numb(s, k + 1, len - 1, t)) {
temp = str[x] + '.' + str[y] + '.' + str[z] + '.' + str[t];
res[temp] = true;
}
}
}
void palin(char s1[], char s2[], int len, int len2) {
char s[20];
int i, j;
for ((i) = 0; (i) < (len); (i)++) s[i] = s1[i];
for ((j) = (len2 - 1); (j) >= 0; (j)--) s[i++] = s2[j];
len += len2;
i = 0, j = len - 1;
while (i < j) {
if (s[i] != s[j]) return;
i++, j--;
}
int k, cur, x, y, z, t;
string temp;
for ((i) = (0); (i) <= (((2) < (len - 4) ? (2) : (len - 4))); (i)++)
for ((j) = (i + 1); (j) <= (((i + 3) < (len - 3) ? (i + 3) : (len - 3)));
(j)++)
for ((k) = (j + 1); (k) <= (((j + 3) < (len - 2) ? (j + 3) : (len - 2)));
(k)++)
if (len - k - 1 > 0 && len - k - 1 <= 3) {
if (numb(s, 0, i, x) && numb(s, i + 1, j, y) &&
numb(s, j + 1, k, z) && numb(s, k + 1, len - 1, t)) {
temp = str[x] + '.' + str[y] + '.' + str[z] + '.' + str[t];
res[temp] = true;
}
}
}
void ktra(int len) {
if (len >= 4) palin(xau, len);
palin(xau, xau, len);
palin(xau, xau, len, len - 1);
}
void dfs(int len, int state) {
if (len >= 2 && state == statemax) {
ktra(len);
}
if (len == 6) return;
int i;
for ((i) = 0; (i) < (n); (i)++) {
xau[len] = a[i] + '0';
dfs(len + 1, state | (1 << a[i]));
}
}
void input() {
int i;
scanf("%d", &n);
statemax = 0;
for ((i) = 0; (i) < (n); (i)++)
scanf("%d", &a[i]), has[a[i]] = true, statemax |= (1 << a[i]);
for ((i) = 0; (i) < (256); (i)++) good[i] = check(i), str[i] = so_xau(i);
dfs(0, 0);
printf("%d\n", res.size());
for (typeof(res.begin()) ite = res.begin(); ite != res.end(); ite++)
printf("%s\n", ite->first.c_str());
}
int main() {
input();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
if (n < 3)
cout << -1;
else {
cout << "5 3 2";
for (i = 4; i <= n; i++) cout << " 5";
}
cout << endl;
return 0;
}
| 1 |
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<cmath>
#include<complex>
#include<iomanip>
using namespace std;
#define REP(i,s,e) for(int i=int(s);i<=int(e);i++)
#define rep(i,n) for(int i=0;i<int(n);i++)
#define pi 3.14159265358979
int main(void){
int n,a,b,c,d,e;
for(;;){
cin >> n;
if(n==0) break;
vector<int>p;
rep(i,n){
cin >> a >> b >> c >> d >> e;
p.push_back(a+b+c+d+e);
}
sort(p.begin(),p.end());
cout << p[n-1] << ' ' << p[0] << endl;
p.clear();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100030;
const long long MODD = 1000000007;
long long fast_exp_mod(long long b, long long n, long long m) {
if (n == 0) return 1 % m;
if (n % 2 == 0) return fast_exp_mod((b * b) % m, n / 2, m);
return (fast_exp_mod(b, n - 1, m) * b) % m;
}
void do_case() {
long long p, k;
cin >> p >> k;
if (k == 0) {
cout << fast_exp_mod(p, p - 1, MODD) << endl;
return;
}
long long P = k;
long long orb = 1;
P = (P * k) % p;
while (P != k) {
P = (P * k) % p;
orb++;
}
orb = (p - 1) / orb;
long long zero = 0;
for (long long i = 0; i < p; i++)
if ((i * k) % p == i) zero++;
cout << (zero * fast_exp_mod(p, orb, MODD)) % MODD << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
do_case();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
stack<string> c;
set<string> ans;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
c.push(s);
}
while (!c.empty()) {
string s = c.top();
c.pop();
if (ans.find(s) == ans.end()) {
cout << s << endl;
ans.insert(s);
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
if (*(s.end() - 1) % 2 == 0)
cout << 0;
else
cout << 1;
}
| 4 |
#include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri(), k = ri();
int sum[n + 1];
sum[0] = 0;
std::map<int, int> ma{{0, 1}};
int64_t res = 0;
for (int i = 1; i <= n; i++) {
if (i >= k) ma[sum[i - k]]--;
int64_t a = ri() - 1 + k;
sum[i] = (sum[i - 1] + a) % k;
res += ma[sum[i]];
ma[sum[i]]++;
}
std::cout << res << std::endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long int y = 0, b = 0, a, B, c, d, e;
cin >> a >> B >> c >> d >> e;
y += c * 2, y += d, b += d, b += 3 * e;
y -= a;
b -= B;
if (y <= 0 && b > 0) {
cout << b << "\n";
return 0;
} else if (b <= 0 && y > 0) {
cout << y << "\n";
return 0;
}
if (y + b <= 0) {
cout << 0;
return 0;
}
cout << y + b;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxd = 3e5 + 10;
char s[maxd], tmp[maxd];
int pre[maxd];
int cnt;
vector<int> a;
int main() {
int n;
scanf("%d %s", &n, s + 1);
int now = 0, pos = 0, mi = 0;
for (int i = 1; s[i]; i++) {
now += ((s[i] == '(') ? 1 : -1);
if (now < mi) mi = now, pos = i;
}
if (now) {
printf("0\n1 1");
return 0;
}
for (int i = 1; i <= n; i++) tmp[i] = s[(pos + i - 1) % n + 1];
a.push_back(0);
for (int i = 1; i <= n; i++) {
s[i] = tmp[i];
pre[i] = pre[i - 1] + ((s[i] == '(') ? 1 : -1);
if (!pre[i]) cnt++, a.push_back(i);
}
int ans = cnt, ls = 1, rs = 1, len = a.size();
for (int i = 1; i < len; i++) {
int l = a[i - 1] + 1, r = a[i], t = 0;
for (int j = l; j <= r; j++) t += (pre[j] == 1);
if (t > ans) ans = t, ls = l, rs = r;
}
a.clear();
for (int i = 1; i <= n; i++)
if (pre[i] == 1) a.push_back(i);
len = a.size();
for (int i = 1; i < len; i++) {
int l = a[i - 1] + 1, r = a[i], t = cnt;
if (pre[l] == 0) continue;
for (int j = l; j <= r; j++) t += (pre[j] == 2);
if (t > ans) ans = t, ls = l, rs = r;
}
printf("%d\n%d %d", ans, (ls + pos - 1) % n + 1, (rs + pos - 1) % n + 1);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s, t;
cin >> s >> t;
for (int i = 0; i <= s.size() - 1; i++) {
int c = i;
for (int j = s.size() - 1; j > i; j--) {
if (s[j] < s[c]) {
c = j;
}
}
if (c != i) {
swap(s[i], s[c]);
break;
}
}
if (s >= t)
cout << "---\n";
else
cout << s << endl;
}
}
| 2 |
#include <bits/stdc++.h>
const int NN = 1010;
const long long Mod = 1000000007;
long long N, K;
long long dp[NN][NN][2][2];
long long sum[NN];
long long fac[NN];
long long a[NN];
long long C[NN][NN];
void DP() {
memset(dp, 0, sizeof(dp));
dp[1][0][0][0] = 1;
if (N > 1) dp[1][1][0][1] = 1;
for (int i = 1; i < N; i++) {
for (int j = 0; j <= i; j++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
if (dp[i][j][b][c] == 0) continue;
int tmp = dp[i][j][b][c];
dp[i + 1][j][c][0] = (dp[i + 1][j][c][0] + tmp) % Mod;
if (b == 0)
dp[i + 1][j + 1][c][0] = (dp[i + 1][j + 1][c][0] + tmp) % Mod;
if (i + 2 <= N)
dp[i + 1][j + 1][c][1] = (dp[i + 1][j + 1][c][1] + tmp) % Mod;
}
}
}
}
for (int i = 0; i <= N; i++) {
sum[i] = 0;
sum[i] = dp[N][i][0][0] + dp[N][i][0][1] + dp[N][i][1][0] + dp[N][i][1][1];
sum[i] %= Mod;
sum[i] = sum[i] * fac[N - i] % Mod;
}
}
int main() {
fac[1] = fac[0] = 1;
for (long long i = 2; i <= 1000; i++) fac[i] = fac[i - 1] * i % Mod;
C[0][0] = C[1][0] = C[1][1] = 1;
for (int i = 2; i <= 1000; i++) {
for (int j = 0; j <= i; j++) {
C[i][j] = C[i - 1][j - 1];
if (i - 1 >= j) C[i][j] = (C[i][j] + C[i - 1][j]) % Mod;
}
}
while (scanf("%d %d", &N, &K) == 2) {
DP();
a[N] = sum[N];
for (int i = N - 1; i >= K; i--) {
long long tmp = sum[i];
for (int j = i + 1; j <= N; j++) {
long long mid = C[j][i] * a[j];
mid %= Mod;
tmp -= mid;
if (tmp < 0) tmp += Mod;
}
a[i] = tmp;
}
printf("%d\n", (int)a[K]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char s[500 + 5];
bool graph[500 + 1][500 + 1];
int main() {
int n, m;
int finish_size = 0;
int first = 1;
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int tmp1, tmp2;
cin >> tmp1 >> tmp2;
graph[tmp1][tmp2] = true;
graph[tmp2][tmp1] = true;
}
for (int i = 1; i <= n; ++i) {
int cnt = 0;
for (int j = 1; j <= n; ++j)
if (graph[i][j]) cnt++;
if (cnt == n - 1) {
s[i] = 'b';
finish_size++;
}
}
for (int i = 1; i <= n; ++i) {
if (s[i] != 'b') continue;
for (int j = 1; j <= n; ++j) {
graph[i][j] = false;
graph[j][i] = false;
}
}
while (s[first] != '\0') first++;
if (finish_size != n) {
int first_a;
vector<int> a;
first_a = first;
a.push_back(first_a);
s[first_a] = 'a';
finish_size++;
if (finish_size != n) {
for (int i = 1; i <= n; ++i) {
if (graph[first_a][i]) {
graph[first_a][i] = false;
a.push_back(i);
s[i] = 'a';
}
}
for (int i = 1; i < a.size(); ++i) {
int vector_now = 0;
for (int j = 1; j <= n; ++j) {
if (graph[a[i]][j] && j == a[vector_now]) {
graph[a[i]][j] = false;
vector_now++;
} else if ((graph[a[i]][j] && j != a[vector_now]) ||
(!graph[a[i]][j] && j == a[vector_now])) {
if (a[i] == j) {
vector_now++;
continue;
}
cout << "NO" << endl;
return 0;
}
}
}
}
}
while (s[first] != '\0') first++;
if (finish_size != n) {
int first_c;
vector<int> c;
first_c = first;
c.push_back(first_c);
s[first_c] = 'c';
finish_size++;
if (finish_size != n) {
for (int i = 1; i <= n; ++i) {
if (graph[first_c][i]) {
if (s[i] == '\0') {
graph[first_c][i] = false;
c.push_back(i);
s[i] = 'c';
} else {
cout << "NO" << endl;
return 0;
}
}
}
for (int i = 1; i < c.size(); ++i) {
int vector_now = 0;
for (int j = 1; j <= n; ++j) {
if (graph[c[i]][j] && j == c[vector_now]) {
graph[c[i]][j] = false;
vector_now++;
} else if ((graph[c[i]][j] && j != c[vector_now]) ||
(!graph[c[i]][j] && j == c[vector_now])) {
if (c[i] == j) {
vector_now++;
continue;
}
cout << "NO" << endl;
return 0;
}
}
}
}
}
for (int i = 1; i <= n; ++i) {
if (s[i] == '\0') {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
for (int i = 1; i <= n; ++i) cout << s[i];
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int t, x, y;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &x);
printf("1 %d\n", x - 1);
}
return 0;
}
| 1 |
#include <iostream>
using namespace std;
int main() {
string S,T,U;
int A,B;
cin>>S>>T>>A>>B>>U;
if(U==S){
A--;
}
else{
B--;
}
cout<<A<<" "<<B;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int n,i,c,times;
string s;
int main()
{
cin>>n;
cin>>s;
for(i=0;i<n;i++)
if(s[i]=='W')
c++;
for(i=n-c;i<n;i++)
if(s[i]!='W')
times++;
cout<<times;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long MOD(long long, long long);
void doublesetprecision(double, int);
bool isEven(long long);
void solve() {
int n;
double ans = 0;
cin >> n;
int a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (long long i = n - 1; n - 1 < -1 ? i < -1 : i > -1;
n - 1 < -1 ? i += 2 : i -= 2) {
if (i - 1 >= 0) {
ans += (pow(a[i], 2) - pow(a[i - 1], 2));
} else if (i == 0)
ans += (pow(a[i], 2));
}
ans *= 3.1415926535897932384626;
doublesetprecision(ans, 10);
}
int main() {
long long t = 1;
while (t--) {
solve();
}
return 0;
}
bool isEven(long long n) {
if (n % 2 == 0) return 1;
return 0;
}
void doublesetprecision(double x, int p) {
cout << fixed << setprecision(p) << x;
}
long long MOD(long long x, long long M) { return (x % M + M) % M; }
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.