solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
return make_pair(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) {
return make_pair(a.first - b.first, a.second - b.second);
}
pair<int, int> &operator+=(pair<int, int> &a, const pair<int, int> &b) {
a.first += b.first;
a.second += b.second;
return a;
}
pair<int, int> &operator-=(pair<int, int> &a, const pair<int, int> &b) {
a.first -= b.first;
a.second -= b.second;
return a;
}
template <class T, class U>
bool cmp_second(const pair<T, U> &a, const pair<T, U> &b) {
return a.second < b.second;
}
template <class T>
T gcd(T a, T b) {
a = abs(a);
b = abs(b);
while (b) {
T t = b;
b = a % b;
a = t;
}
return a;
}
template <class T>
pair<T, T> ext_gcd(T a, T b) {
T a0 = 1, a1 = 0, b0 = 0, b1 = 1;
if (a < 0) {
a = -a;
a0 = -1;
}
if (b < 0) {
b = -b;
b1 = -1;
}
while (b) {
T t, q = a / b;
t = b;
b = a - b * q;
a = t;
t = b0;
b0 = a0 - b0 * q;
a0 = t;
t = b1;
b1 = a1 - b1 * q;
a1 = t;
}
return make_pair(a0, a1);
}
inline int sg(int x) { return x ? (x > 0 ? 1 : -1) : 0; }
const int inf = 1000000;
class Segment {
public:
int l, r, id;
};
class Node {
public:
int vl, vr;
int cnt, mii;
pair<int, int> mil;
priority_queue<int, vector<int>, greater<int> > pq;
Node *_l, *_r;
Node(int vl, int vr) : vl(vl), vr(vr) {
cnt = 0;
mil = make_pair(inf, -1);
mii = inf;
if (vl == vr) pq.push(inf);
_l = _r = NULL;
}
~Node() {
delete _l;
delete _r;
}
Node *left() {
if (!_l) _l = new Node(vl, (vl + vr) / 2);
return _l;
}
Node *right() {
if (!_r) _r = new Node((vl + vr) / 2 + 1, vr);
return _r;
}
void insert(int x, int ind) {
cnt++;
mii = min(mii, ind);
if (vl == vr) {
if (cnt)
mil = make_pair(vl - cnt, vl);
else
mil = make_pair(inf, -1);
pq.push(ind);
} else {
int m = (vl + vr) / 2;
if (x <= m)
left()->insert(x, ind);
else
right()->insert(x, ind);
mil = min(left()->mil, right()->mil - make_pair(left()->cnt, 0));
}
}
int getmin(int r) {
if (r == vr)
return mii;
else {
int m = (vl + vr) / 2;
if (r <= m)
return left()->getmin(r);
else
return min(left()->getmin(m), right()->getmin(r));
}
}
int takemin(int r) { return takemin(r, getmin(r)); }
int takemin(int r, int minval) {
cnt--;
if (vl == vr) {
int ind = pq.top();
pq.pop();
mii = pq.top();
if (cnt)
mil = make_pair(vl - cnt, vl);
else
mil = make_pair(inf, -1);
return ind;
} else {
int ret, m = (vl + vr) / 2;
if (r <= m)
ret = left()->takemin(r, minval);
else if (left()->mii == minval)
ret = left()->takemin(m, minval);
else
ret = right()->takemin(r, minval);
mii = min(left()->mii, right()->mii);
mil = min(left()->mil, right()->mil - make_pair(left()->cnt, 0));
return ret;
}
}
};
int n;
Segment segbyr[2050], segbyl[2050];
int ans[2050], id2ri[2050];
bool used[2050];
bool cmpr(const Segment &a, const Segment &b) {
if (a.r != b.r)
return a.r < b.r;
else
return a.l < b.l;
}
bool cmpl(const Segment &a, const Segment &b) { return a.l < b.l; }
bool valid(int thr) {
int sbli = 0, sbri = 0;
Node tr(0, n);
for (int i = 0; i < n; i++) used[i] = 0;
for (int i = 0; i < n; i++) {
int lb = tr.mil.first;
int at = tr.mil.second;
int ind;
if (lb < i) return 0;
if (lb > i) {
while (sbri < n && used[sbri]) sbri++;
assert(sbri < n);
if (tr.getmin(n) == sbri)
ind = tr.takemin(n);
else
ind = sbri;
} else {
ind = tr.takemin(at);
}
ans[i] = segbyr[ind].id;
used[ind] = 1;
while (sbli < n && segbyl[sbli].l <= segbyr[ind].r) {
int u = id2ri[segbyl[sbli++].id];
if (used[u]) continue;
tr.insert(i + thr, u);
}
}
return 1;
}
void solve() {
sort(segbyr, segbyr + n, cmpr);
sort(segbyl, segbyl + n, cmpl);
for (int i = 0; i < n; i++) id2ri[segbyr[i].id] = i;
int lb = -1, rb = n;
while (lb < rb - 1) {
int m = (lb + rb) / 2;
if (valid(m))
rb = m;
else
lb = m;
}
valid(rb);
for (int i = 0; i < n; i++)
printf("%d%c", ans[i] + 1, i + 1 < n ? ' ' : '\n');
}
int main(void) {
vector<pair<int, int> > e;
scanf("%d", &(n));
for (int i = 0; i < n; i++) {
scanf("%d %d", &(segbyr[i].l), &(segbyr[i].r));
segbyr[i].id = i;
segbyl[i] = segbyr[i];
}
solve();
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c, d;
cin >> n >> a >> b >> c >> d;
double p = pow(abs(c - a), 2) + pow(abs(d - b), 2);
p = sqrt(p);
int q = ceil(p / (2 * n));
cout << q;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 10;
long long int dp[MAXN], last[1000];
int main() {
long long int n, k;
cin >> n >> k;
string s;
cin >> s;
long long int l = s.length();
dp[0] = 1;
long long int ma, ind = 0, mini;
for (long long int i = 1; i < l + 1 + n; ++i) {
dp[i] = (dp[i - 1] * 2LL);
dp[i] %= 1000000007;
if (i > l) {
char x = 'a';
ind = 0;
mini = last[x - 97];
for (long long int j = 0; j < k; ++j) {
if (last[j] < mini) {
mini = last[j];
ind = j;
}
}
s += (char)(ind + 97);
}
if (last[s[i - 1] - 97] != 0) dp[i] -= dp[last[s[i - 1] - 97] - 1];
if (dp[i] < 0) {
dp[i] += 1000000007;
}
last[s[i - 1] - 97] = i;
}
cout << dp[l + n] % 1000000007 << endl;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
int _f = N / 2, _g = M / 2;
for (int x = 1; x <= _f; x++) {
int tmp = N - x + 1;
for (int i = 0; i < M; i++) {
cout << x << ' ' << i + 1 << '\n';
cout << tmp << ' ' << M - i << '\n';
}
}
if (N & 1) {
for (int i = 0; i < _g; i++) {
cout << _f + 1 << ' ' << i + 1 << '\n';
cout << _f + 1 << ' ' << M - i << '\n';
}
if (M & 1) cout << _f + 1 << ' ' << _g + 1 << '\n';
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
int n, t, atm = 0;
std::vector<int> ans;
int ask(int l, int r) {
std::cout << "? " << l << " " << r << std::endl;
int x;
std::cin >> x;
return x;
}
bool same(const std::vector<int>& a) {
bool r = true;
for (int x : a) r &= x == a[0];
return r;
}
int distinct(const std::vector<int>& a) {
std::set<int> st;
for (int x : a) st.insert(x);
return st.size();
}
int pairwise(const std::vector<int>& a) {
int r = 0;
for (int i = 0; i < (int)a.size() - 1; i++) r += a[i] == a[i + 1];
return r;
}
void solve(int pos) {
if (n - t == pos + t - (atm << 1) || n - t == pos + t - 2 - (atm << 1)) {
std::vector<int> a;
for (int i = 0; i < 30; i++) a.push_back(ask(pos + 1, n));
bool sam = false;
if (t == n - t)
sam = same(a);
else
sam = distinct(a) == 2 && pairwise(a);
if (sam)
ans[pos] = n - t != pos + t - (atm << 1);
else
ans[pos] = n - t == pos + t - (atm << 1);
atm = a.back() -
(((n - pos) >> 1) + !sam * (n - t == pos + t - (atm << 1) ? -1 : 1));
t = a.back();
} else {
int q = ask(1, pos);
while (q == n - t) {
ask(1, n);
q = ask(1, pos);
}
ans[pos] = q != pos + t - (atm << 1);
atm = pos - atm - (q != pos + t - (atm << 1));
t = q;
}
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::cin >> n >> t;
ans.resize(n, 0);
ans.push_back(t);
for (int i = 1; i < n; i++) {
solve(i);
ans[n] -= ans[i];
}
std::cout << "! ";
for (int i = 1; i <= n; i++) std::cout << ans[i];
std::cout << std::endl;
}
| 18 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
long long v1, v2, t, d;
int dp[2002][2002];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> v1 >> v2;
cin >> t >> d;
dp[1][v1] = v1;
for (int i = 1; i < t; ++i)
for (int j = 0; j <= 2000; ++j) {
if (!dp[i][j]) continue;
for (int k = j - d; k <= j + d; ++k) {
if (k < 0) continue;
dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + k);
}
}
cout << dp[t][v2];
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int toint(T t) {
stringstream ss;
ss << t;
int r;
ss >> r;
return r;
}
int main() {
int a;
string b;
cin >> a >> b;
reverse(b.begin(), b.end());
cout << (toint(b) + a) << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n == 2) {
cout << 2;
} else {
cout << 1;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int dx[] = {0, 1, -1, 0, 1, -1, 1, -1, -2, 2, 0, 0},
dy[] = {1, 0, 0, -1, -1, 1, 1, -1, 0, 0, -2, 2};
using namespace std;
long long N[200005], D[200005];
const int M = 1e9 + 7;
long long dp[3000];
long long POWER(int b, int e) {
if (e == 0) return 1;
if (e & 1)
return (POWER(b, e - 1) % M * b) % M;
else {
long long t = POWER(b, e / 2) % M;
return t * t % M;
}
}
long long C(int n, int r) {
if (n == r) return 1;
return N[n] * ((D[r] * D[n - r]) % M) % M;
}
int main() {
N[1] = 1, D[1] = 1;
N[0] = D[0] = 1;
for (int i = 2; i < 200004; i++) {
N[i] = (N[i - 1] * i) % M;
D[i] = POWER(N[i], M - 2);
}
int h, w, n;
cin >> h >> w >> n;
long long ans = C(h + w - 2, h - 1);
vector<pair<int, int> > black;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
black.push_back(make_pair(a, b));
}
black.push_back(make_pair(h, w));
sort(black.begin(), black.end());
for (int i = 0; i < black.size(); i++) {
int x = black[i].first, y = black[i].second;
dp[i] = C(x + y - 2, x - 1);
for (int j = 0; j < i; j++) {
int a = black[j].first, b = black[j].second;
if (y >= b) {
dp[i] -= (dp[j] * C(x - a + y - b, x - a) % M);
dp[i] = (dp[i] + M) % M;
}
}
}
cout << dp[black.size() - 1] << endl;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rd(chrono ::system_clock ::now().time_since_epoch().count());
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m, k, q;
cin >> n >> m >> k >> q;
int i, r, c;
vector<int> gift[n];
int last = 0;
for (i = 0; i < k; i++) {
cin >> r >> c;
gift[r - 1].emplace_back(c - 1);
last = max(last, r - 1);
}
for (i = 0; i < n; i++) sort(gift[i].begin(), gift[i].end());
vector<int> b(q);
for (i = 0; i < q; i++) {
cin >> b[i];
b[i]--;
}
sort(b.begin(), b.end());
long long left, right;
vector<pair<int, long long> > ord;
if (gift[0].empty())
ord.emplace_back(b[0], b[0]);
else
ord.emplace_back(0, 0);
for (i = 0; i < n; i++) {
if (gift[i].empty()) continue;
left = right = 1e18;
int lg = gift[i][0];
int rg = gift[i].back();
while (!ord.empty()) {
int x = ord.back().first;
long long cur = ord.back().second;
ord.pop_back();
left = min(left, cur + abs(rg - x) + rg - lg);
right = min(right, cur + abs(lg - x) + rg - lg);
}
if (i < last) {
int ind = lower_bound(b.begin(), b.end(), lg) - b.begin();
if (ind < q) ord.emplace_back(b[ind], b[ind] - lg + left);
if (ind - 1 >= 0) ord.emplace_back(b[ind - 1], lg - b[ind - 1] + left);
ind = lower_bound(b.begin(), b.end(), rg) - b.begin();
if (ind < q) ord.emplace_back(b[ind], b[ind] - rg + right);
if (ind - 1 >= 0) ord.emplace_back(b[ind - 1], rg - b[ind - 1] + right);
} else {
ord.emplace_back(lg, left);
ord.emplace_back(rg, right);
break;
}
}
long long ans = 1e18;
for (auto &j : ord) ans = min(ans, j.second);
cout << ans + last;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, h = 0, sol = 0;
scanf("%I64d", &n);
while (++h) {
long long int r = 3 * h * (h + 1) / 2 - h;
if (r > n) break;
if ((n - r) % 3 == 0) sol++;
}
printf("%I64d\n", sol);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long int n, k, a[100014];
vector<long long int> res;
long long int get_diff(int i, long long int x) {
return -3 * x * x + 3 * x + a[i] - 1;
}
long long int eval(long long int minval, bool compres = false) {
long long int totk = 0;
for (int i = (0); i < (n); i++) {
long long int low, high, mid, resk;
low = 0;
high = a[i];
resk = 0;
while (low <= high) {
mid = (low + high) / 2;
if (get_diff(i, mid) >= minval) {
low = mid + 1;
resk = max(resk, mid);
} else
high = mid - 1;
}
if (compres) res.push_back(resk);
totk += resk;
}
return totk;
}
int main() {
scanf("%lld%lld", &n, &k);
for (int i = (0); i < (n); i++) scanf("%lld", &a[i]);
long long int low, high, mid, resval;
low = -4000000000000000007LL;
high = 4000000000000000007LL;
resval = -4000000000000000007LL;
while (low <= high) {
mid = (low + high) / 2;
if (eval(mid) >= k) {
low = mid + 1;
resval = max(resval, mid);
} else
high = mid - 1;
}
long long int used = eval(resval, true);
for (int i = (0); i < (res.size()); i++)
if (res[i] && get_diff(i, res[i]) == resval) {
if (used > k) {
--used;
--res[i];
}
}
for (int i = (0); i < (res.size()); i++)
printf("%lld%c", res[i], " \n"[i == res.size() - 1]);
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct RBNode {
enum Color { RED, BLACK };
RBNode *right = nullptr, *left = nullptr, *parent = nullptr;
T value;
int size = 0;
int color = RED;
inline bool isBlack() const { return color == BLACK; }
inline bool isRed() const { return color == RED; }
inline bool isLeftBlack() const {
return left == nullptr || left->color == BLACK;
}
inline bool isLeftRed() const { return left && left->color == RED; }
inline bool isRightBlack() const {
return right == nullptr || right->color == BLACK;
}
inline bool isRightRed() const { return right && right->color == RED; }
inline int leftSize() const { return left ? left->size : 0; }
inline int rightSize() const { return right ? right->size : 0; }
inline void paintBlack() { color = BLACK; }
inline void paintRed() { color = RED; }
};
template <typename T>
struct RBTree {
RBNode<T> *root = nullptr;
inline int size() const { return root ? root->size : 0; }
void insert(const T &value) {
RBNode<T> *parent;
bool left;
if (!find_insertion_point(value, parent, left)) return;
auto node = new RBNode<T>;
node->value = value;
node->parent = parent;
if (parent) {
if (left)
parent->left = node;
else
parent->right = node;
} else {
root = node;
}
on_insert(node);
start:
parent = node->parent;
if (!parent) {
node->paintBlack();
return;
}
if (parent->isBlack()) return;
auto g = parent->parent;
auto uncle = g->left == parent ? g->right : g->left;
if (uncle && uncle->isRed()) {
parent->paintBlack();
uncle->paintBlack();
g->paintRed();
node = g;
goto start;
}
if (node == parent->right && parent == g->left) {
rotate_left(parent);
swap(parent, node);
}
if (node == parent->left && parent == g->right) {
rotate_right(parent);
swap(parent, node);
}
parent->paintBlack();
g->paintRed();
if (node == parent->left)
rotate_right(g);
else
rotate_left(g);
}
RBNode<T> *successor(RBNode<T> *node) const {
auto current = node;
if (current->right) {
current = current->right;
while (current->left) current = current->left;
} else {
while (current) {
auto stop = !current->parent || current == current->parent->left;
current = current->parent;
if (stop) break;
}
}
return current;
}
int n_geq(const T &value) const {
RBNode<T> *parent, *node;
bool left;
if (!find_insertion_point(value, parent, left)) {
if (parent) {
node = left ? parent->left : parent->right;
} else {
node = root;
}
} else {
node = parent;
if (!left) {
while (node) {
auto stop = !node->parent || node == node->parent->left;
node = node->parent;
if (stop) break;
}
}
}
if (!node) return 0;
auto count = 1 + node->rightSize();
auto previous = node;
for (auto current = node->parent; current; current = current->parent) {
if (current->left == previous) count += 1 + current->rightSize();
previous = current;
}
return count;
}
inline int count_in_range(const T &from, const T &to) const {
return from < to ? n_geq(from) - n_geq(to) : 0;
}
bool find_insertion_point(const T &value, RBNode<T> *&parent,
bool &left) const {
auto current = root;
parent = nullptr;
while (current) {
if (current->value == value) return false;
parent = current;
left = !(current->value < value);
current = left ? current->left : current->right;
}
return true;
}
void rotate_left(RBNode<T> *node) {
on_rotate_left(node);
auto p = node->parent;
auto c = node->right;
node->right = c->left;
node->parent = c;
if (c->left) c->left->parent = node;
c->left = node;
c->parent = p;
if (p) {
if (p->left == node)
p->left = c;
else
p->right = c;
} else {
root = c;
}
}
void rotate_right(RBNode<T> *node) {
on_rotate_right(node);
auto p = node->parent;
auto c = node->left;
node->left = c->right;
node->parent = c;
if (c->right) c->right->parent = node;
c->right = node;
c->parent = p;
if (p) {
if (p->left == node)
p->left = c;
else
p->right = c;
} else {
root = c;
}
}
void on_rotate_left(RBNode<T> *node) {
auto t = node->leftSize() + node->right->leftSize() + 1;
node->right->size = node->size;
node->size = t;
}
void on_rotate_right(RBNode<T> *node) {
auto t = node->rightSize() + node->left->rightSize() + 1;
node->left->size = node->size;
node->size = t;
}
void on_insert(RBNode<T> *node) {
for (auto current = node; current; current = current->parent)
current->size++;
}
};
int scan() {
auto c = getchar();
while (c < '0' || c > '9') c = getchar();
int n;
for (n = 0; '0' <= c && c <= '9'; c = getchar()) n = n * 10 + c - '0';
return n;
}
struct Op {
int type;
int time;
int value;
};
bool operator<(const Op &a, const Op &b) {
if (a.value < b.value) return true;
if (a.value > b.value) return false;
return a.time < b.time;
}
bool operator==(const Op &a, const Op &b) {
return a.value == b.value && a.time == b.time;
}
int main() {
int N = scan();
vector<Op> ops;
ops.resize(N);
for (auto &op : ops) {
op.type = scan();
op.time = scan();
op.value = scan();
}
RBTree<Op> inserts, removals;
for (auto &op : ops) {
switch (op.type) {
case 1:
inserts.insert(op);
break;
case 2:
removals.insert(op);
break;
case 3:
Op from = {0, 1, op.value};
printf("%d\n", inserts.count_in_range(from, op) -
removals.count_in_range(from, op));
break;
}
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > D;
vector<bool> used;
int last;
int dfs(int v, int sum) {
used[v] = 1;
last = v;
if (D[v].size() == 0) {
return sum;
}
int to = D[v][0].first;
if (used[to] == 1) {
return -228;
}
int len = D[v][0].second;
return dfs(to, min(sum, len));
}
bool comp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
return a.second > b.second;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
int cnt1[n], cnt2[n];
for (int i = 0; i < n; i++) {
vector<pair<int, int> > c;
D.push_back(c);
used.push_back(0);
cnt1[i] = 0;
cnt2[i] = 0;
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
cnt1[a]++;
cnt2[b]++;
D[a].push_back({b, c});
}
vector<pair<pair<int, int>, int> > ans;
for (int i = 0; i < n; i++) {
if (cnt1[i] != 0 && cnt2[i] == 0) {
int x = dfs(i, 1e9);
if (x == -228) {
continue;
} else {
ans.push_back({{i, last}, x});
}
}
}
sort(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first.first + 1 << " " << ans[i].first.second + 1 << " "
<< ans[i].second << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
set<char> vowels;
vowels.insert('a');
vowels.insert('e');
vowels.insert('i');
vowels.insert('o');
vowels.insert('u');
bool aaaa = true;
bool aabb = false;
bool abba = false;
bool abab = false;
bool foundaaaa = false;
for (int i = 0; i < n; i++) {
string s;
vector<string> suf(4);
suf[0] = "0";
suf[1] = "1";
suf[2] = "2";
suf[3] = "3";
for (int j = 0; j < 4; j++) {
int vcount = 0;
cin >> s;
for (int pos = s.size() - 1; pos >= 0; pos--) {
if (vowels.count(s[pos])) {
vcount++;
}
if (vcount >= k) {
suf[j] = (s.c_str() + pos);
break;
}
}
}
if (suf[0] == suf[1] && suf[1] == suf[2] && suf[2] == suf[3]) {
foundaaaa = true;
} else if (suf[0] == suf[1] && suf[2] == suf[3]) {
if (aaaa || aabb) {
aabb = true;
aaaa = false;
} else {
cout << "NO" << endl;
return 0;
}
} else if (suf[0] == suf[2] && suf[1] == suf[3]) {
if (aaaa || abab) {
abab = true;
aaaa = false;
} else {
cout << "NO" << endl;
return 0;
}
} else if (suf[0] == suf[3] && suf[1] == suf[2]) {
if (aaaa || abba) {
abba = true;
aaaa = false;
} else {
cout << "NO" << endl;
return 0;
}
} else {
cout << "NO" << endl;
return 0;
}
}
assert((((int)abab + (int)abba + (int)aabb) == 1) || foundaaaa);
if (abab) {
cout << "abab" << endl;
return 0;
}
if (abba) {
cout << "abba" << endl;
return 0;
}
if (aabb) {
cout << "aabb" << endl;
return 0;
}
if (foundaaaa) {
cout << "aaaa" << endl;
return 0;
}
cout << "NO" << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, a[maxn], lst[maxn], maxL, ans;
inline int read() {
int ret = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - 48, ch = getchar();
return ret;
}
int main() {
memset(lst, -1, sizeof lst);
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) {
int x = -1;
if (a[i] <= 99998) x = max(x, lst[a[i] + 2]);
if (a[i] >= 2) x = max(x, lst[a[i] - 2]);
maxL = max(maxL, x);
ans = max(ans, i - maxL);
lst[a[i]] = i;
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10000017;
int n, i, j, b, a[N], l, r, m, rez[N];
bool navp[N];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (i = 0; i < n; ++i) {
cin >> b;
++a[b];
}
i = 2;
while (i < N) {
j = i * 2;
rez[i] = a[i];
while (j < N) {
rez[i] += a[j];
navp[j] = true;
j += i;
}
++i;
while ((navp[i] == true) && (i < N)) {
++i;
}
}
for (i = 3; i < N; ++i) {
rez[i] += rez[i - 1];
}
cin >> m;
for (i = 0; i < m; ++i) {
cin >> l >> r;
r = min(r, N - 10);
if (l > r) {
cout << "0\n";
} else {
cout << rez[r] - rez[l - 1] << "\n";
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 800005;
int n, a[N], pos[N], c[N], num[N];
long long sum[N];
void update(int x) {
for (; x <= n; x += (x & (-x))) c[x]++;
}
int Query(int x) {
int ans = 0;
for (; x; x -= (x & (-x))) ans += c[x];
return ans;
}
void modify(int cur, int l, int r, int pos) {
if (l == r) {
num[cur] = 1, sum[cur] = pos;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
modify(cur << 1, l, mid, pos);
else
modify(cur << 1 | 1, mid + 1, r, pos);
sum[cur] = sum[cur << 1] + sum[cur << 1 | 1];
num[cur] = num[cur << 1] + num[cur << 1 | 1];
}
int find(int cur, int l, int r, int pos) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (pos <= num[cur << 1]) return find(cur << 1, l, mid, pos);
return find(cur << 1 | 1, mid + 1, r, pos - num[cur << 1]);
}
long long query(int cur, int l, int r, int pos) {
if (1 <= l && r <= pos) return sum[cur];
int mid = (l + r) >> 1;
if (pos <= mid)
return query(cur << 1, l, mid, pos);
else
return sum[cur << 1] + query(cur << 1 | 1, mid + 1, r, pos);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pos[a[i]] = i;
long long tot = 0, val = 0;
for (int i = 1; i <= n; i++) {
tot += pos[i];
int l = (i + 1) / 2;
update(n - pos[i] + 1);
modify(1, 1, n, pos[i]);
int id = find(1, 1, n, l);
long long Sum = (id == 1 ? 0 : query(1, 1, n, id - 1));
val += Query(n - pos[i]);
printf("%lld ", val +
(1ll * (id * 2 - l) * (l - 1) -
1ll * (id * 2 + i - l) * (i - l + 1)) /
2 +
tot - 2 * Sum);
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s1, s2;
vector<pair<string, string>> lst;
for (int i = 0; i < n; i++) {
cin >> s1 >> s2;
bool got = false;
for (vector<pair<string, string>>::iterator it = lst.begin();
it != lst.end(); it++) {
if (it->second == s1) {
it->second = s2;
got = true;
break;
}
}
if (!got) lst.push_back(make_pair(s1, s2));
}
cout << lst.size() << endl;
for (vector<pair<string, string>>::iterator it = lst.begin(); it != lst.end();
it++) {
cout << it->first << " " << it->second << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void solve(string s, string t) {
int n = s.length();
unordered_map<char, int> mp;
for (int i = 0; i < n; i++) {
mp[s[i]]++;
}
for (int i = 0; i < n; i++) {
mp[t[i]]++;
}
for (auto i : mp) {
if (i.second % 2 == 1) {
cout << "NO"
<< "\n";
return;
}
}
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
bool done = false;
for (int j = i + 1; j < n; j++) {
if (s[j] == s[i]) {
v.push_back({j + 1, i + 1});
swap(t[i], s[j]);
done = true;
break;
}
}
if (!done) {
for (int j = i + 1; j < n; j++) {
if (t[j] == s[i]) {
v.push_back({j + 1, j + 1});
swap(s[j], t[j]);
v.push_back({j + 1, i + 1});
swap(t[i], s[j]);
break;
}
}
}
} else {
continue;
}
}
cout << "YES"
<< "\n";
cout << v.size() << "\n";
for (auto x : v) {
cout << x.first << " " << x.second << "\n";
}
}
int32_t main() {
int x;
cin >> x;
while (x--) {
int n;
cin >> n;
string s, t;
cin >> s >> t;
solve(s, t);
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
int vowels = 0, odd = 0;
cin >> str;
for (int i = 0; i < str.size(); i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u')
vowels++;
else if ((str[i] - '0' >= 0 && str[i] - '0' <= 9)) {
if ((int)(str[i] - '0') % 2 != 0) odd++;
}
}
cout << vowels + odd << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long const DIM = 104;
long long i, c;
char a;
int main() {
while (cin >> a) {
if (a == 'H' || a == 'Q' || a == '9') c = 1;
}
if (c)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
using namespace std;
int a, b, c, d, e, f, p = 1000000007, base = 157, mi = 1e9, id = 0, q, pos;
int ind[300000], ans[300000], si[300000], si2[300000];
pair<int, int> reb_price[300000];
string s, s2;
vector<int> vec[300000];
vector<pair<int, int>> rebra[300000];
vector<int> order;
void dfs() {
for (auto v : order) {
int ma = 0, ma2 = 0;
si[v] = 1;
for (auto i : rebra[v]) {
if (i.second == 0) {
continue;
}
b += (si[i.first] + i.second - 1) / id;
si[i.first] = (si[i.first] + i.second - 1) % id;
if (si[i.first] > ma) {
ma2 = ma;
ma = si[i.first];
} else if (si[i.first] > ma2) {
ma2 = si[i.first];
}
}
si[v] = ma + 1;
if (si[v] + ma2 >= id) {
si[v] = 0;
b++;
}
}
}
pair<int, int> dfs2(int v, int pred, int pred2) {
if (rebra[v].size() == 1 && v != 1) {
rebra[v][0] = {pred2, 0};
order.push_back(v);
return {v, 1};
}
if (rebra[v].size() == 2 && v != 1) {
if (rebra[v][0].first == pred) {
auto pa = dfs2(rebra[v][1].first, v, pred2);
return {pa.first, pa.second + 1};
} else {
auto pa = dfs2(rebra[v][0].first, v, pred2);
return {pa.first, pa.second + 1};
}
}
int j = 0;
for (auto i : rebra[v]) {
if (i.first == pred) {
rebra[v][j] = {pred2, 0};
j++;
continue;
}
rebra[v][j] = dfs2(i.first, v, v);
j++;
}
order.push_back(v);
return {v, 1};
}
int readint() {
int n = 0;
char c;
do {
c = fgetc(stdin);
} while (!(c == '-' || '0' <= c && c <= '9'));
int sign = 1;
if (c == '-') {
sign = -1;
c = fgetc(stdin);
}
do {
n = n * 10 + (c - '0');
c = fgetc(stdin);
} while ('0' <= c && c <= '9');
return n * sign;
}
signed main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
a = readint();
for (int i = 0; i < a - 1; i++) {
b = readint();
c = readint();
rebra[b].push_back({c, 1});
rebra[c].push_back({b, 1});
}
dfs2(1, -1, -1);
for (int i = 0; i < a; i = i) {
if (i == 0 || (b > 100 && i < 500)) {
b = 0;
id = i + 1;
dfs();
cout << b << '\n';
;
i++;
} else {
b = 0;
id = i + 1;
dfs();
c = b;
int l = i + 1, r = a / max(b, 1) + 1;
b = 0;
while (r - l > 1) {
int res = (r + l) / 2;
b = 0;
id = res;
dfs();
if (b == c) {
l = res;
} else {
r = res;
}
}
for (int j = i; j < l; j++) {
cout << c << '\n';
;
}
i = l;
}
}
cin >> a;
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
struct num {
vector<long long> z;
num() { z = vector<long long>(5); }
num operator+(const num &b) const {
num c;
for (int i = 0; i < 5; ++i) {
c.z[i] = z[i] + b.z[i];
}
return c;
}
num operator-(const num &b) const {
num c;
for (int i = 0; i < 5; ++i) {
c.z[i] = z[i] - b.z[i];
}
return c;
}
num operator*(const num &b) const {
num c;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
c.z[(i + j) % 5] += z[i] * b.z[j];
}
}
return c;
}
num shift(int w) {
num res;
for (int i = 0; i < 5; ++i) {
res.z[(i + w) % 5] = z[i];
}
return res;
}
};
long long power(long long x, long long y) {
long long res = 1;
for (; y; y >>= 1, x *= x) {
if (y & 1) {
res *= x;
}
}
return res;
}
num power(num x, int y) {
num res;
res.z[0] = 1;
for (; y; y >>= 1, x = x * x) {
if (y & 1) {
res = res * x;
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<num> a(N);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
++a[x].z[0];
}
vector<num> temp(10);
int base[5];
base[0] = 1;
for (int i = 1; i < 5; i++) base[i] = 10 * base[i - 1];
function<void(bool)> dft = [&](bool idft) {
for (int i = 0; i < 5; i++) {
for (int k = 0; k < N; k++) {
if ((k / base[i]) % 10 == 0) {
fill(temp.begin(), temp.end(), num());
for (int u = 0; u < 10; u++) {
for (int v = 0; v < 10; v++) {
int w = u * v % 10;
if (idft) w = 10 - w;
if (w & 1)
temp[u] = temp[u] - a[k + v * base[i]].shift(w + 5 >> 1);
else
temp[u] = temp[u] + a[k + v * base[i]].shift(w >> 1);
}
}
for (int u = 0; u < 10; u++) a[k + u * base[i]] = temp[u];
}
}
}
};
dft(false);
for (int i = 0; i < N; ++i) {
a[i] = power(a[i], n);
}
dft(true);
long long inv = power(3125, LLONG_MAX);
for (int i = 0; i < n; ++i) {
cout << ((a[i].z[0] - a[i].z[1] >> 5) * inv & (1ll << 58) - 1) << "\n";
}
return 0;
}
| 26 |
#include <bits/stdc++.h>
using namespace std;
int xd[] = {0, 1, 0, -1};
int yd[] = {1, 0, -1, 0};
int xx[] = {0, 1, 0, -1, 1, -1, 1, -1};
int yy[] = {1, 0, -1, 0, 1, 1, -1, -1};
int kx[] = {-2, -1, -2, -1, 1, 2, 2, 1};
int ky[] = {1, 2, -1, -2, -2, -1, 1, 2};
bool islucky(int x) {
int four = 0, seven = 0;
while (x) {
if (x % 10 != 7 && x % 10 != 4) return false;
if (x % 10 == 7)
seven++;
else
four++;
x = x / 10;
}
return four == seven;
}
bool is_prime(long long x) {
if (x == 1) return false;
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return false;
return true;
}
long long C(int k, int l) {
if (l == k) return 1;
if (l * 2 > k) l = k - l;
long long res = 1, temp = l;
for (int i = k - l + 1; i <= k; i++) {
res *= i / temp;
temp--;
}
return res;
}
long long sumOfDigit(int x) {
int res = 0;
while (x) {
res += x % 10;
x /= 10;
}
return res;
}
uint64_t bigPow(uint64_t base, uint64_t p) {
uint64_t temp = 1;
while (p--) {
temp *= base;
}
return temp;
}
vector<int> soso;
bool prime[10000001] = {0};
void SieveOfEratosthenes(int n) {
soso.push_back(2);
for (int p = 3; p * p <= n; p += 2) {
if (prime[p] == false) {
for (int i = p * 2; i <= n; i += p) prime[i] = true;
}
}
for (int i = 3; i <= n; i += 2)
if (!prime[i]) soso.push_back(i);
}
vector<vector<int> > AdjList;
bool visited[100005];
void dfs(int s) {
if (visited[s]) return;
visited[s] = 1;
for (int i = 0; i < AdjList[s].size(); i++) dfs(AdjList[s][i]);
return;
}
long long gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
unsigned long long fact(long long a) {
unsigned long long ans = 1;
for (int i = 1; i <= a; i++) ans *= i;
return ans;
}
bool lucky(int x) {
while (x) {
if (x % 10 == 7) return true;
x /= 10;
}
return false;
}
int main() {
int n;
cin >> n;
int ans = -1;
map<int, int> boxes;
for (int i = 0; i < n; i++) {
int b;
cin >> b;
boxes[b]++;
ans = max(ans, boxes[b]);
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void smax(T &a, T b) {
if (a < b) a = b;
}
const int maxn = 100 * 1000 + 100;
const int RANGE = 3 * 100 * 1000 + 100;
int n;
pair<pair<int, int>, pair<int, int> > LVR[maxn], VLR[maxn];
int add_val[4 * RANGE], max_val[4 * RANGE];
void add_range(int x, int nl, int nr, int ql, int qr, int val) {
if (nr <= ql || qr <= nl) return;
if (ql <= nl && nr <= qr) {
add_val[x] += val;
max_val[x] += val;
return;
}
add_val[2 * x + 1] += add_val[x], max_val[2 * x + 1] += add_val[x];
add_val[2 * x + 2] += add_val[x], max_val[2 * x + 2] += add_val[x];
add_val[x] = 0;
int nm = (nl + nr) / 2;
add_range(2 * x + 1, nl, nm, ql, qr, val);
add_range(2 * x + 2, nm, nr, ql, qr, val);
max_val[x] = max(max_val[2 * x + 1], max_val[2 * x + 2]);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 0, _n = (int)(n); i < _n; i++) {
cin >> LVR[i].first.first >> LVR[i].first.second >> LVR[i].second.first;
LVR[i].second.first++;
LVR[i].second.second = i;
VLR[i] = LVR[i];
swap(VLR[i].first.first, VLR[i].first.second);
}
sort(LVR, LVR + n);
sort(VLR, VLR + n);
int pos = 0;
int ans = 0, ind = 0;
for (int i = 0, _n = (int)(n); i < _n; i++) {
while (pos < n && LVR[pos].first.first <= VLR[i].first.first)
add_range(0, 0, RANGE, LVR[pos].first.second, LVR[pos].second.first, 1),
pos++;
if (max_val[0] >= ans) ans = max_val[0], ind = i;
add_range(0, 0, RANGE, VLR[i].first.first, VLR[i].second.first, -1);
}
cout << ans << endl;
vector<int> LS, RS;
for (int i = (int)(ind), _n = (int)(n); i < _n; i++)
if (VLR[i].first.second <= VLR[ind].first.first &&
VLR[i].first.first < VLR[ind].second.first)
LS.push_back(VLR[i].first.first), RS.push_back(VLR[i].second.first);
sort((LS).begin(), (LS).end()), sort((RS).begin(), (RS).end());
int max_cur = -1, max_i = -1, cur = 0;
pos = 0;
for (int i = 0, _n = (int)((int((LS).size()))); i < _n; i++) {
while (pos < (int((RS).size())) && RS[pos] <= LS[i]) cur--, pos++;
cur++;
if (max_cur < cur) max_cur = cur, max_i = i;
}
cerr << " @@ " << max_cur << ' ' << LS[max_i] << endl;
for (int i = (int)(ind), _n = (int)(n); i < _n; i++)
if (VLR[i].first.second <= VLR[ind].first.first &&
VLR[i].first.first < VLR[ind].second.first &&
VLR[i].first.first <= LS[max_i] && LS[max_i] < VLR[i].second.first)
cout << VLR[i].second.second + 1 << ' ';
cout << endl;
{
int _;
cin >> _;
return 0;
}
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
inline int getint() {
char c;
while ((c = getchar()) < '0' || c > '9')
;
return c - '0';
}
int a[200005], n, c[200005], flip;
pair<int, int> b[200005];
int fun(int pos, int len, int dir) {
int tl, t1, temp, temp2;
if (len == 0) {
return b[pos].second;
}
if (dir == 0) {
temp = upper_bound(a + 1, a + n + 1, a[pos] + len) - (a);
temp--;
if (temp == pos && flip == 1) {
return b[pos].second;
} else if (temp == pos) {
flip++;
return fun(pos, len, dir ^ 1);
}
tl = len - (a[temp] - a[pos]);
temp2 = lower_bound(a + 1, a + n + 1, a[temp] - tl) - (a);
if (temp2 == pos) {
tl = (a[temp] - a[pos]);
t1 = len / tl;
if (t1 & 1) {
return fun(temp, len % tl, 1);
} else {
return fun(pos, len % tl, 0);
}
} else {
return fun(temp, len - (a[temp] - a[pos]), 1);
}
} else {
temp = lower_bound(a + 1, a + n + 1, a[pos] - len) - (a);
if (temp == pos && flip == 1) {
return b[pos].second;
} else if (temp == pos) {
flip++;
return fun(pos, len, dir ^ 1);
}
tl = len - (a[pos] - a[temp]);
temp2 = upper_bound(a + 1, a + n + 1, a[temp] + tl) - (a);
temp2--;
if (temp2 == pos) {
tl = (a[pos] - a[temp]);
t1 = len / tl;
if (t1 & 1) {
return fun(temp, len % tl, 0);
} else {
return fun(pos, len % tl, 1);
}
} else {
return fun(temp, len - (a[pos] - a[temp]), 0);
}
}
}
int main() {
int m, i, pos, l, ans;
scanf("%d %d", &n, &m);
b[0].first = -1000000005;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
c[i] = a[i];
b[i].first = a[i];
b[i].second = i;
}
sort(b, b + n + 1);
for (i = 0; i <= n; i++) {
a[i] = b[i].first;
}
for (i = 0; i < m; i++) {
scanf("%d %d", &pos, &l);
flip = 0;
pos = lower_bound(a + 1, a + n + 1, c[pos]) - a;
ans = fun(pos, l, 0);
printf("%d\n", ans);
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
long long int n, m, sum, k, a, b, dp[1000005], pref[1000005];
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> n >> a >> b >> k;
for (int j = 0; j <= k; j++) {
for (int i = 1; i <= n; i++) {
if (j == 0) {
dp[a] = 1;
break;
}
long long int x = 0;
if (a > b) {
x = i - b - 1;
x /= 2;
dp[i] = pref[i - x] - dp[i];
dp[i] += 1000000007;
dp[i] %= 1000000007;
} else {
x = b - i - 1;
x /= 2;
dp[i] = pref[i + x] - dp[i];
dp[i] += 1000000007;
dp[i] %= 1000000007;
}
}
if (a < b)
for (int i = 1; i <= n; i++)
pref[i] = pref[i - 1] + dp[i], pref[i] %= 1000000007;
else
for (int i = n; i >= 1; i--)
pref[i] = pref[i + 1] + dp[i], pref[i] %= 1000000007;
}
if (a > b)
for (int i = b + 1; i <= n; i++) sum += dp[i], sum %= 1000000007;
else
for (int i = 1; i <= b - 1; i++) sum += dp[i], sum %= 1000000007;
cout << sum;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
using si = short int;
using usi = unsigned short int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
struct me {
pair<int, int> x;
ll y;
me *l, *r;
int cnt;
me() {
cnt = 0;
l = r = nullptr;
y = ((ll)rand() << 17) + rand();
}
};
using pme = me*;
int cnt(pme t) { return (t ? t->cnt : 0); }
void upd(pme t) {
if (t) t->cnt = 1 + cnt(t->l) + cnt(t->r);
}
void mer(pme& t, pme l, pme r) {
if (!l || !r) {
t = (l ? l : r);
return;
}
if (l->y > r->y) {
t = l;
mer(l->r, l->r, r);
} else {
t = r;
mer(r->l, l, r->l);
}
upd(t);
}
void split(pme t, pair<int, int> key, pme& l, pme& r) {
if (!t) {
l = r = nullptr;
return;
}
if (key < t->x) {
r = t;
split(t->l, key, l, r->l);
} else {
l = t;
split(t->r, key, t->r, r);
}
upd(l);
upd(r);
}
void er(pme& t, pair<int, int> key) {
if (!t) return;
if (t->x == key)
mer(t, t->l, t->r);
else if (t->x < key)
er(t->r, key);
else
er(t->l, key);
upd(t);
}
void ins(pme& t, pme key) {
if (!t) {
t = key;
} else if (key->y > t->y) {
split(t, key->x, key->l, key->r);
t = key;
} else
ins(key->x < t->x ? t->l : t->r, key);
upd(t);
}
struct my {
pme tree = nullptr;
void era(pair<int, int> a) {
er(tree, a);
return;
}
int cnt_upper(pair<int, int> a) {
pme l, r;
split(tree, a, l, r);
int ans = cnt(r);
mer(tree, l, r);
return ans;
}
void append(pair<int, int> a) {
pme n = new me;
n->x = a;
ins(tree, n);
return;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<pair<pii, int>> arr(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i].first.first;
cin >> arr[i].first.second;
cin >> arr[i].second;
}
sort((arr).begin(), (arr).end());
ll ans = 0;
map<int, my> in;
set<pair<int, int>> era;
for (int i = 0; i < n; ++i) {
while (era.size() > 0 && (*era.begin()).first < arr[i].first.first) {
0;
int ind = (*era.begin()).second;
era.erase(era.begin());
in[arr[ind].second].era({arr[ind].first.first, ind});
}
era.insert({arr[i].first.first + arr[i].first.second, i});
for (int jj = max(arr[i].second - k, 0); jj <= arr[i].second + k; ++jj) {
auto j = in.find(jj);
if (j != in.end()) {
ans += (*j).second.cnt_upper(
{arr[i].first.first - arr[i].first.second, -1});
}
}
in[arr[i].second].append({arr[i].first.first, i});
0;
}
cout << ans;
}
| 14 |
#include <bits/stdc++.h>
#define code \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define int long long
#define endl '\n'
#define MM(a, b) memset(a, b, sizeof(a))
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1)
{
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args)
{
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int N = 1e5 + 5;
int n, m;
int t[N], x[N], y[N];
int ans[N];
void dfs(int idx, int k)
{
if (idx == n)
return;
if (t[idx] == 1)
{
for (int i = 0; i < y[idx]; i++)
{
int inc = (int)(ceil(1.0 * x[idx] / 1e5));
int curk = k + (i + 1) * inc;
if (curk <= m && ans[curk] == -1)
{
ans[curk] = idx + 1;
dfs(idx + 1, curk);
}
else
return;
}
}
else
{
for (int i = 0; i < y[idx]; i++)
{
double inc = (int)(ceil(1.0 * x[idx] / 1e5));
int curk = ceil(k * inc);
if (curk <= m && ans[curk] == -1)
{
ans[curk] = idx + 1;
dfs(idx + 1, curk);
}
else
return;
}
}
}
int32_t main()
{
code;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> t[i] >> x[i] >> y[i];
}
MM(ans, -1);
queue<pair<int, int>> q;
q.push({0, 0});
while (!q.empty())
{
int sz = q.size();
while (sz--)
{
auto tp = q.front();
q.pop();
int idx = tp.first;
int k = tp.second;
if (idx == n)
continue;
if (t[idx] == 1)
{
int curk = k;
q.push({idx + 1, curk});
for (int i = 0; i < y[idx]; i++)
{
int inc = (int)(ceil(1.0 * x[idx] / 1e5));
curk = curk + inc;
if (curk <= m && ans[curk] == -1)
{
ans[curk] = idx + 1;
// trace(idx + 1, curk);
q.push({idx + 1, curk});
}
else
break;
}
}
else
{
int curk = k;
q.push({idx + 1, curk});
if (curk == 0)
continue;
for (int i = 0; i < y[idx]; i++)
{
double inc = (1.0 * x[idx]) / 1e5;
// trace(curk, x[idx]);
curk = ceil(1.0 * curk * inc);
// trace(curk, inc);
if (curk <= m && ans[curk] == -1)
{
ans[curk] = idx + 1;
// trace(idx + 1, curk);
q.push({idx + 1, curk});
}
else
break;
}
}
}
}
for (int i = 1; i <= m; i++)
{
cout << ans[i] << " ";
}
}
| 14 |
#include <bits/stdc++.h>
int n, m, s;
long long t;
std::map<std::pair<int, long long>, int> map;
int walk(int s) {
int &r = map[{s, 1}] = s;
for (int i = n - 1; i; --i)
if (r < m)
r = (r + i) % n;
else
r = (r + n - i) % n;
return r;
}
int run(int s, long long times) {
if (map.count({s, times})) return map[{s, times}];
if (!times) return s;
if (times == 1) return walk(s);
return map[{s, times}] = run(run(s, times >> 1), times + 1 >> 1);
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::cin >> n >> m >> s >> t, --s;
while (t % n) {
if (s < m)
s = (s + t--) % n;
else
s = (s + n - t-- % n) % n;
}
std::cout << run(s, t / n) + 1 << '\n';
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
namespace pcf {
long long dp[100][100010];
unsigned int ar[(10000010 >> 6) + 5] = {0};
int len = 0, primes[666666], counter[10000010];
void Sieve() {
(((ar[(0) >> 6]) |= (1 << (((0) >> 1) & 31)))),
(((ar[(1) >> 6]) |= (1 << (((1) >> 1) & 31))));
for (int i = 3; (i * i) < 10000010; i++, i++) {
if (!(((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))) {
int k = i << 1;
for (int j = (i * i); j < 10000010; j += k)
(((ar[(j) >> 6]) |= (1 << (((j) >> 1) & 31))));
}
}
for (int i = 1; i < 10000010; i++) {
counter[i] = counter[i - 1];
if ((((i) && ((i)&1) &&
(!(((ar[((i)) >> 6]) & (1 << ((((i)) >> 1) & 31)))))) ||
((i) == 2)))
primes[len++] = i, counter[i]++;
}
}
void init() {
Sieve();
for (int n = 0; n < 100; n++) {
for (int m = 0; m < 100010; m++) {
if (!n)
dp[n][m] = m;
else
dp[n][m] = dp[n - 1][m] - dp[n - 1][m / primes[n - 1]];
}
}
}
long long phi(long long m, int n) {
if (n == 0) return m;
if (primes[n - 1] >= m) return 1;
if (m < 100010 && n < 100) return dp[n][m];
return phi(m, n - 1) - phi(m / primes[n - 1], n - 1);
}
long long Lehmer(long long m) {
if (m < 10000010) return counter[m];
long long w, res = 0;
int i, a, s, c, x, y;
s = sqrt(0.9 + m), y = c = cbrt(0.9 + m);
a = counter[y], res = phi(m, a) + a - 1;
for (i = a; primes[i] <= s; i++)
res = res - Lehmer(m / primes[i]) + Lehmer(primes[i]) - 1;
return res;
}
} // namespace pcf
long long solve(long long n) {
int i, j, k, l;
long long x, y, res = 0;
for (i = 0; i < pcf::len; i++) {
x = pcf::primes[i], y = n / x;
if ((x * x) > n) break;
res += (pcf::Lehmer(y) - pcf::Lehmer(x));
}
for (i = 0; i < pcf::len; i++) {
x = pcf::primes[i];
if ((x * x * x) > n) break;
res++;
}
return res;
}
int main() {
pcf::init();
long long n, res;
cin >> n;
printf("%lld\n", solve(n));
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
typedef struct node {
int nxt[2];
int cnt;
} ND;
int n, m, tot;
const int p = 1000000000;
long long fact[20];
long long qpow(long long a, long long b, long long modp) {
long long ans;
for (ans = 1; b; b >>= 1, a = a * a % modp)
if (b & 1) ans = ans * a % modp;
return ans;
}
long long gcd(long long x, long long y) {
while (y) {
long long r = x % y;
x = y;
y = r;
}
return x;
}
void extgcd(long long a, long long b, long long &x, long long &y) {
if (b != 0) {
extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
}
long long mod_inv(long long a, long long modp) {
long long x, y;
extgcd(a, modp, x, y);
return (modp + x % modp) % modp;
}
long long mod_fact(long long n, long long p, long long &e) {
e = 0;
if (n == 0) return 1;
long long res = mod_fact(n / p, p, e);
e += n / p;
if (n / p % 2 != 0) return res * (p - fact[n % p]) % p;
return res * fact[n % p] % p;
}
long long lucas(long long n, long long k) {
if (n < 0 || k < 0 || n < k) return 0;
long long e1, e2, e3;
long long a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2),
a3 = mod_fact(n - k, p, e3);
if (e1 > e2 + e3) return 0;
return a1 * mod_inv(a2 * a3 % p, p) % p;
}
long long crt(long long a1, long long m1, long long a2, long long m2) {
long long g = gcd(m1, m2);
long long k = a1 % g;
if (k != a2 % g) return -1;
m1 /= g, m2 /= g;
a1 /= g, a2 /= g;
return ((a1 + m1 - a2 % m1) % m1 * mod_inv(m2 % m1, m1) % m1 * m2 + a2) %
(m1 * m2) * g +
k;
}
int main() {
int T = 1;
scanf("%d", &n);
int L, R, U, D;
L = 0, R = n;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", 1, 1, n, M);
fflush(stdout);
scanf("%d", &m);
if (m == 2)
R = M;
else
L = M;
}
int x1 = R, x2;
if (x1 > 1) {
printf("? %d %d %d %d\n", 1, 1, n, x1 - 1);
fflush(stdout);
scanf("%d", &m);
} else
m = 0;
if (m != 1)
x2 = R;
else {
L = 0, R = x1 - 1;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", 1, 1, n, M);
fflush(stdout);
scanf("%d", &m);
if (m == 1)
R = M;
else
L = M;
}
x2 = R;
}
L = 1, R = n + 1;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", 1, M, n, n);
fflush(stdout);
scanf("%d", &m);
if (m == 2)
L = M;
else
R = M;
}
int y1 = L, y2;
if (y1 < n) {
printf("? %d %d %d %d\n", 1, y1 + 1, n, n);
fflush(stdout);
scanf("%d", &m);
} else
m = 0;
if (m != 1)
y2 = L;
else {
L = y1 + 1, R = n + 1;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", 1, M, n, n);
fflush(stdout);
scanf("%d", &m);
if (m == 1)
L = M;
else
R = M;
}
y2 = L;
}
L = 0, R = n;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", 1, 1, M, n);
fflush(stdout);
scanf("%d", &m);
if (m == 2)
R = M;
else
L = M;
}
int u1 = R, u2;
if (u1 > 1) {
printf("? %d %d %d %d\n", 1, 1, u1 - 1, n);
fflush(stdout);
scanf("%d", &m);
} else
m = 0;
if (m != 1)
u2 = R;
else {
L = 0, R = u1 - 1;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", 1, 1, M, n);
fflush(stdout);
scanf("%d", &m);
if (m == 1)
R = M;
else
L = M;
}
u2 = R;
}
L = 1, R = n + 1;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", M, 1, n, n);
fflush(stdout);
scanf("%d", &m);
if (m == 2)
L = M;
else
R = M;
}
int d1 = L, d2;
if (d1 < n) {
printf("? %d %d %d %d\n", d1 + 1, 1, n, n);
fflush(stdout);
scanf("%d", &m);
} else
m = 0;
if (m != 1)
d2 = L;
else {
L = d1 + 1, R = n + 1;
while (L + 1 < R) {
int M = L + R >> 1;
printf("? %d %d %d %d\n", M, 1, n, n);
fflush(stdout);
scanf("%d", &m);
if (m == 1)
L = M;
else
R = M;
}
d2 = L;
}
if (y2 > x2) {
swap(y1, y2);
if (d2 > u2) {
swap(d1, d2);
printf("? %d %d %d %d\n", d1, y1, u1, x1);
fflush(stdout);
scanf("%d", &m);
if (m == 0) {
swap(u1, u2), swap(d1, d2);
}
} else {
printf("? %d %d %d %d\n", d2, y1, u2, x1);
fflush(stdout);
scanf("%d", &m);
if (m == 1) {
swap(d1, d2);
swap(u1, u2);
} else if (m == 0) {
printf("? %d %d %d %d\n", d2, y1, u1, x1);
fflush(stdout);
scanf("%d", &m);
if (m == 0) swap(u1, u2);
if (m == 1) swap(d1, d2);
}
}
} else {
swap(d1, d2);
printf("? %d %d %d %d\n", d1, y2, u1, x2);
fflush(stdout);
scanf("%d", &m);
if (m == 1) {
swap(x1, x2);
swap(y1, y2);
} else if (m == 0) {
printf("? %d %d %d %d\n", d1, y2, u1, x1);
fflush(stdout);
scanf("%d", &m);
if (m == 0) swap(x1, x2);
if (m == 1) swap(y1, y2);
}
}
printf("! %d %d %d %d %d %d %d %d\n", d1, y1, u1, x1, d2, y2, u2, x2);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 10, INF = INT_MAX, MOD = 1e9 + 7;
long long a[MAXN];
int main() {
ios::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
for (int i = 0, t; i < m; i++) cin >> t >> a[i];
sort(a, a + m, greater<int>());
long long ans = 0;
for (long long i = 0; i < m; i++) {
if (!(i % 2) && n - 1 < i * (i + 1) / 2) break;
if ((i % 2) && n < ((i + 1) * (i + 1)) / 2) break;
ans += a[i];
}
cout << ans << endl;
return 0;
}
| 12 |
#include <bits/stdc++.h>
const int N = 111111;
const long long mod = 1000000007;
struct Point {
int w, sccno;
} p[N];
struct Edge {
int u, v, next;
} e[3 * N], d[3 * N];
int tot, tod, eh[N], ed[N];
void add1(int u, int v) {
e[tot].u = u, e[tot].v = v, e[tot].next = eh[u], eh[u] = tot++;
}
void add2(int u, int v) {
d[tod].u = u, d[tod].v = v, d[tod].next = ed[u], ed[u] = tod++;
}
int vis[N], scc_cnt, S[N], idx;
void dfs1(int u) {
if (vis[u]) return;
vis[u] = 1;
for (int j = eh[u], v; ~j; j = e[j].next) {
v = e[j].v;
dfs1(v);
}
S[idx++] = u;
}
void dfs2(int u) {
if (p[u].sccno) return;
p[u].sccno = scc_cnt;
for (int j = ed[u], v; ~j; j = d[j].next) {
v = d[j].v;
dfs2(v);
}
}
void find_scc(int n) {
for (int i = 1; i <= n; ++i) dfs1(i);
for (int i = n - 1; i >= 0; --i) {
if (!p[S[i]].sccno) {
scc_cnt++;
dfs2(S[i]);
}
}
}
int cot[N], mx[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &p[i].w);
}
int m, u, v;
memset(eh, -1, sizeof eh);
memset(ed, -1, sizeof ed);
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
scanf("%d%d", &u, &v);
add1(u, v);
add2(v, u);
}
find_scc(n);
memset(mx, 0x3f, sizeof mx);
for (int i = 1; i <= n; ++i) {
if (p[i].w < mx[p[i].sccno]) {
mx[p[i].sccno] = p[i].w;
cot[p[i].sccno] = 1;
} else if (p[i].w == mx[p[i].sccno]) {
cot[p[i].sccno]++;
}
}
long long ans = 1, cost = 0;
for (int i = 1; i <= scc_cnt; ++i) {
cost += mx[i];
ans = (ans * cot[i]) % mod;
}
printf("%I64d %I64d\n", cost, ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int nNums, r;
cin >> nNums;
vector<int> nums(nNums), accMin(nNums);
for (int i = 0; i < nNums; i++) {
cin >> nums[i];
}
for (int i = nNums - 1; i >= 0; i--) {
if (i == nNums - 1)
accMin[i] = nums[i];
else
accMin[i] = min(nums[i], accMin[i + 1]);
}
for (int i = 0; i < nNums; i++) {
r = lower_bound(accMin.begin() + i + 1, accMin.end(), nums[i]) -
accMin.begin();
if (r - i <= 1)
cout << -1 << " ";
else
cout << r - i - 2 << " ";
}
cout << "\n";
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
string s;
int n, a[100005], b[100005];
bool f;
void cty(int o) {
int q = 0;
for (int i = 1; i <= n; i++) q = q * 10 + a[i];
int c[10];
for (int i = 1; i <= o; i++) c[i] = 9;
while (c[1]) {
int r = 0, r2 = 0;
for (int i = 1; i <= o; i++) r = r * 10 + c[i];
for (int i = o; i; i--) r2 = r2 * 10 + c[i];
if (r2 + r == q) {
for (int i = 1; i <= o; i++) printf("%d", c[i]);
printf("\n");
exit(0);
}
r = o;
while (c[r] == 0) c[r--] = 9;
c[r]--;
}
}
int main() {
cin >> s;
n = s.size();
for (int i = 0; i < n; i++) a[i + 1] = s[i] - '0';
int l = 1, r = n;
if (n == 1) {
printf("%d\n", a[1] & 1 ? 0 : a[1] >> 1);
return 0;
}
if (n == 2) {
int t = a[1] * 10 + a[2];
for (int i = 99; i >= 10; i--) {
if (i + i / 10 + (i % 10) * 10 == t) {
printf("%d\n", i);
return 0;
}
}
for (int i = 1; i <= 9; i++) {
if (t == i << 1) {
printf("%d\n", i);
return 0;
}
}
printf("0\n");
return 0;
}
if (n <= 5) {
for (int i = n; i >= 2; i--) cty(i);
printf("0\n");
return 0;
}
while (l <= r) {
if (a[l] < 0) f = 1;
if (a[r] < 0) a[r] += 10, a[r - 1]--;
if (l == 1 && a[r] == 0) {
if (a[1] != 1) f = 1;
a[r - 1]--;
l++;
b[l] = b[r] = 5;
r--;
if (a[2] > 1) f = 1;
a[3] += a[2] * 10;
l = 3;
continue;
}
if (l == 1 && a[r] > a[l]) {
a[r - 1]--;
a[2] += 10;
int t = a[r] + 10;
b[2] = 9;
b[r] = t - 9;
a[2] -= t;
if (a[2] < 0 || a[2] > 1)
f = 1;
else
a[3] += a[2] * 10;
l = 3;
r--;
continue;
}
if (l == r) {
if (a[l] & 1)
f = 1;
else
b[l] += a[l] >> 1;
break;
}
if (l + 1 == r) {
int x = a[l] * 10 + a[r];
bool bb = 0;
for (int i = 0; i <= 99; i++) {
if (i + (i / 10) + (i % 10) * 10 == x) {
bb = 1;
b[l] += i / 10;
b[r] += i % 10;
break;
}
}
if (!bb) f = 1;
break;
}
if (a[l] == a[r]) {
if (a[l] < 10)
b[l] += a[l];
else
b[l] = 9, b[r] = a[l] - 9;
} else {
if (a[l] < 10) {
if (a[l] == a[r] + 1)
b[l] += a[r], a[l + 1] += 10;
else if (a[l] == 1)
a[l + 1] += 10, a[r - 1]--, a[r] += 10, r++;
else
f = 1;
} else {
b[r] += a[l] - 9, b[l] += 9;
if (a[l] == a[r] + 10)
a[r - 1]--;
else if (a[l] == a[r] + 1)
a[l + 1] += 10, b[r] ? b[r]-- : b[l]--;
else if (a[l] == a[r] + 11)
a[r - 1]--, a[l + 1] += 10, b[r] ? b[r]-- : b[l]--;
else
f = 1;
}
}
l++;
r--;
}
for (int i = 1; i <= n; i++)
if (b[i] > 9) f = 1;
if (f)
printf("0\n");
else {
int y = 1;
while (b[y] == 0) y++;
for (int i = y; i <= n; i++) printf("%d", b[i]);
printf("\n");
}
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int k[1000000], wei[400], q[400], f[1000000];
int cmp(int a, int b) { return a > b; }
int main() {
int n;
int b, g = 0;
scanf("%d", &n);
scanf("%d", &q[0]);
b = q[0];
while (b > 0) {
if (b > 9) {
k[g++] = 9;
b -= 9;
} else {
k[g++] = b;
b = 0;
}
}
wei[0] = g;
for (int j = wei[0] - 1; j >= 0; j--) {
printf("%d", k[j]);
}
printf("\n");
for (int i = 1; i < n; i++) {
memcpy(f, k, sizeof(k));
scanf("%d", &q[i]);
if (q[i] > q[i - 1]) {
int sum = 0;
for (int j = 0; j < wei[i - 1]; j++) {
while (k[j] < 9) {
k[j] += 1;
sum += 1;
if (sum == (q[i] - q[i - 1])) {
j = wei[i - 1];
break;
}
}
}
wei[i] = wei[i - 1];
while (sum < (q[i] - q[i - 1])) {
wei[i] += 1;
while (sum < (q[i] - q[i - 1])) {
if (k[wei[i] - 1] >= 9) break;
k[wei[i] - 1] += 1;
sum += 1;
}
}
for (int j = wei[i] - 1; j >= 0; j--) {
printf("%d", k[j]);
}
printf("\n");
} else if (q[i] == q[i - 1]) {
wei[i] = wei[i - 1];
int gd;
for (int j = 0; j < wei[i - 1]; j++) {
if (k[j] != 0) {
gd = j;
k[j] -= 1;
break;
}
}
for (int j = gd + 1; j < wei[i - 1]; j++) {
if (k[j] < 9) {
k[j] += 1;
gd = -1;
break;
}
}
if (gd != -1) {
k[wei[i]] = 1;
wei[i] += 1;
}
if (wei[i] > wei[i - 1]) {
sort(k, k + wei[i] - 1, cmp);
} else {
int kkk;
for (int j = wei[i] - 1; j >= 0; j--) {
if (k[j] > f[j]) {
kkk = j;
break;
}
}
sort(k, k + kkk, cmp);
}
for (int j = wei[i] - 1; j >= 0; j--) {
printf("%d", k[j]);
}
printf("\n");
} else if (q[i] < q[i - 1]) {
wei[i] = wei[i - 1];
int sum = 0;
for (int j = 0; j < wei[i - 1]; j++) {
while (k[j] > 0) {
k[j] -= 1;
sum += 1;
if (sum == (q[i - 1] - q[i])) {
j = wei[i - 1];
break;
}
}
}
int gd;
for (int j = 0; j < wei[i - 1]; j++) {
if (k[j] != 0) {
gd = j;
k[j] -= 1;
break;
}
}
for (int j = gd + 1; j < wei[i - 1]; j++) {
if (k[j] < 9) {
k[j] += 1;
gd = -1;
break;
}
}
if (gd != -1) {
k[wei[i]] = 1;
wei[i] += 1;
}
if (wei[i] > wei[i - 1]) {
sort(k, k + wei[i] - 1, cmp);
} else {
int kkk;
for (int j = wei[i] - 1; j >= 0; j--) {
if (k[j] > f[j]) {
kkk = j;
break;
}
}
sort(k, k + kkk, cmp);
}
for (int j = wei[i] - 1; j >= 0; j--) {
printf("%d", k[j]);
}
printf("\n");
}
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int w, h, n;
int xx1, xx2, yy1, yy2;
int cnt;
bool e[110][110][4];
bool vis[110][110];
vector<int> ans;
void dfs(int x, int y) {
if (x < 0 || x >= w || y < 0 || y >= h || vis[x][y]) return;
vis[x][y] = true;
cnt++;
for (int i = 0; i < 4; i++)
if (!e[x][y][i]) dfs(x + dx[i], y + dy[i]);
return;
}
int main() {
scanf("%d%d%d", &w, &h, &n);
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d", &xx1, &yy1, &xx2, &yy2);
if (xx1 == xx2)
for (int j = yy1; j < yy2; j++) e[xx1 - 1][j][0] = e[xx1][j][1] = true;
if (yy1 == yy2)
for (int j = xx1; j < xx2; j++) e[j][yy1 - 1][2] = e[j][yy1][3] = true;
}
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
if (!vis[i][j]) {
cnt = 0;
dfs(i, j);
ans.push_back(cnt);
}
sort(ans.begin(), ans.end());
for (int i = 0; i < n + 1; i++) cout << ans[i] << ' ';
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int no, no1;
int n;
bool in = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s >> no >> no1;
if (no >= 2400 && no1 > no) in = 1;
}
if (in)
cout << "YES";
else
cout << "NO";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int temp = 0;
int i;
for (i = 0; i < n; i++) {
if (arr[i] == arr[0])
temp++;
else
break;
}
int ans = 0;
for (int j = i; j < n;) {
int cnt = 0;
int a = arr[j];
while (j < n && arr[j] == a) {
cnt++;
j++;
}
ans = max(ans, min(temp, cnt));
temp = cnt;
}
cout << ans * 2;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int n = 0, m = 0;
long long d = 0, lcm = 0;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void exgcd(int a, int b, long long &x, long long &y) {
if (b) {
exgcd(b, a % b, y, x);
y -= x * (a / b);
} else {
x = 1;
y = 0;
}
}
long long calc(int x, int y) {
if ((y - x) % d) {
return -1;
} else {
long long s = 0, t = 0;
exgcd(n, m, s, t);
s *= (y - x) / d;
s = (n * s + x) % lcm;
return (s + lcm) % lcm;
}
}
long long solve(int x, int y) {
long long ans = lcm;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
long long t = calc(i ? n - x : x, j ? m - y : y);
if (t != -1) {
if (((t / n) & 1) == i && ((t / m) & 1) == j) {
ans = std::min(ans, t);
}
}
}
}
return ans == lcm ? -1 : ans;
}
int main() {
int k = 0;
scanf("%d%d%d", &n, &m, &k);
d = gcd(n, m);
lcm = (long long)n * (long long)m / d;
for (int i = 0; i < k; i++) {
int x = 0, y = 0;
scanf("%d%d", &x, &y);
printf("%I64d\n", solve(x, y));
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[11][11];
for (int i = 1; i <= n; ++i) {
a[i][1] = 1;
a[1][i] = 1;
}
for (int i = 2; i <= n; ++i) {
for (int j = 2; j <= n; ++j) a[i][j] = a[i - 1][j] + a[i][j - 1];
}
cout << a[n][n];
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, K;
cin >> n >> K;
vector<string> v(n);
unordered_map<string, int> ind;
for (int i = 0; i < n; i++) {
cin >> v[i];
ind[v[i]] = i;
}
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
string t = "";
for (int k = 0; k < K; k++) {
if (v[i][k] == v[j][k])
t += v[i][k];
else
t += 'S' ^ 'E' ^ 'T' ^ v[i][k] ^ v[j][k];
}
if (ind.count(t) && ind[t] > j) ans++;
}
}
cout << ans << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
double x, y, minx = 999999999;
int diff = a - b;
int sum = a + b;
if (diff < 0) {
cout << -1 << endl;
return 0;
} else {
printf("%.12f\n", (a + b) / (2. * ((a + b) / (2 * b))));
}
return 0;
}
| 9 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
/*-----for personal-----*/
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define repn(i,n) rep(i,0,n)
/*-----for lib-----*/
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
const double EPS = 1e-5;
const int MOD = 1e9+7;
const int INF = INT_MAX / 2;
const ll LLINF = LLONG_MAX / 2;
//for(auto &h : H){ cout << h << " ";} cout << endl;
//for(auto &h : H){for(auto &k:h){cout << k << " ";}cout << endl;}
//for(auto [k,v] : mp){cout << k << " " << v << endl;}
//for(auto [k,v] : mp){cout << k << " "; for(auto &u:v){cout << u << " ";}cout << endl;}
bool judge(vector<ll> a){
int n = a.size();
vector<ll> rm(n);
rm[0] = a[0];
for(int i = 1; i < n; i++){
rm[i] = a[i] - rm[i-1];
if(i != n-1 && rm[i] < 0) return false;
}
if(rm[n-1]==0) return true;
else return false;
}
bool judge_r(vector<ll> a){
int n = a.size();
vector<ll> rm(n);
rm[n-1] = a[n-1];
for(int i = n-2; i >= 0; i--){
rm[i] = a[i] - rm[i+1];
if(i != 0 && rm[i] < 0) return false;
}
if(rm[0]==0) return true;
else return false;
}
void solve() {
int n; cin >> n;
vector<ll> a(n);
repn(i,n) cin >> a[i];
vector<ll> rm_l(n); vector<ll> rm_r(n);
/*
if (n == 1){
cout << "NO" << endl;
return;
}
*/
if(judge(a) || judge_r(a)){
cout << "YES" << endl;
return;
}
swap(a[0],a[1]);
if(judge(a) || judge_r(a)){
cout << "YES" << endl;
return;
}
swap(a[0],a[1]);
swap(a[n-2],a[n-1]);
if(judge(a) || judge_r(a)){
cout << "YES" << endl;
return;
}
swap(a[n-2],a[n-1]);
rm_l[0] = a[0];
for(int i = 1; i < n; i++){
if(rm_l[i-1]==-1){ rm_l[i] = -1; continue;}
rm_l[i] = a[i] - rm_l[i-1];
if(rm_l[i]<0) rm_l[i] = -1;
}
rm_r[n-1] = a[n-1];
for(int i = n-2; i >= 0; i--){
if(rm_r[i+1]==-1){ rm_r[i] = -1; continue;}
rm_r[i] = a[i] - rm_r[i+1];
if(rm_r[i]<0) rm_r[i] = -1;
}
for(int i = 1; i < n-2; i++){
if(rm_l[i-1] == -1 || rm_r[i+2] == -1) continue;
vector<ll> c = {rm_l[i-1], a[i], a[i+1], rm_r[i+2]};
swap(c[1], c[2]);
if(judge(c) || judge_r(c)){
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
int main() {
int tt;
cin >> tt;
while(tt--) {
solve();
}
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q, n;
cin >> q;
while (q--) {
cin >> n;
int count = 0;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
for (int i = 0; i < n - 1; i++)
if (v[i + 1] - v[i] == 1) {
count++;
break;
}
if (count > 0)
cout << 2 << endl;
else
cout << 1 << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
inline int rd() {
int x = 0, p = 1;
char a = getchar();
while ((a < 48 || a > 57) && a != '-') a = getchar();
if (a == '-') p = -p, a = getchar();
while (a > 47 && a < 58) x = (x << 1) + (x << 3) + (a & 15), a = getchar();
return x * p;
}
const int N = 200002;
const int mod = 1e9 + 7;
int n, k, a[N], s[N];
int fac[N], inv[N];
int ans;
inline int fpow(int b, int p = mod - 2) {
int ans = 1;
for (; p; b = 1ll * b * b % mod, p >>= 1)
if (p & 1) ans = 1ll * ans * b % mod;
return ans;
}
inline void init(int n) {
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
inv[n] = fpow(fac[n]);
for (int i = n - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
}
inline long long C(int n, int m) {
if (n < 0 || m < 0) return 0;
if (n < m) return 0;
return 1ll * fac[n] * inv[n - m] % mod * inv[m] % mod;
}
int main() {
n = rd(), k = rd();
init(n + n);
for (int i = 1; i <= n; i++)
scanf("%1d", &a[i]), s[i] = (s[i - 1] + a[i]) % mod;
for (int t = 0, pw = 1; t <= n; t++, pw = 10ll * pw % mod)
ans = (1ll * ans + pw % mod *
(1ll * C(n - t - 2, k - 1) * s[n - t - 1] % mod +
1ll * a[n - t] * C(n - t - 1, k) % mod)) %
mod;
printf("%d\n", ans);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long double eps = 1e-10;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
if (a < b) a = b;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<long long> B(n);
for (int i = (int)(0); i < (int)(n); i++) cin >> B[i];
vector<long long> G(m);
for (int i = (int)(0); i < (int)(m); i++) cin >> G[i];
long long bmax = *max_element(B.begin(), B.end());
long long gmin = *min_element(G.begin(), G.end());
if (bmax > gmin) {
cout << -1 << '\n';
return 0;
}
multiset<long long> st;
for (int i = (int)(0); i < (int)(m); i++) st.insert(G[i]);
vector<int> id(n);
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(), [&](int i, int j) { return B[i] > B[j]; });
long long ans = 0;
for (int i : id) {
ans += B[i];
if (st.count(B[i])) {
st.erase(st.find(B[i]));
}
int cnt = m - 1;
for (int j = (int)(0); j < (int)(m - 1); j++) {
if (st.empty()) break;
if (*st.rbegin() < B[i]) break;
ans += *st.rbegin();
st.erase(--st.end());
cnt--;
}
ans += B[i] * cnt;
}
cout << ans << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-8;
const long long INF = 0x3f3f3f3f;
const long long MAXN = 1e3 + 10;
const long long mod = 1e9 + 7;
long long a[205];
void solve() {
long long n;
cin >> n;
for (long long i = (1); i <= (n); ++i) cin >> a[i];
long long cnt = 0;
for (long long i = (1); i <= (n - 1); ++i) {
if (abs(a[i] - a[i + 1]) == 1) cnt++;
}
if (abs(a[1] - a[n]) == 1) cnt++;
if (cnt >= n - 1)
cout << "YES\n";
else
cout << "NO\n";
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long _ = 1;
cin >> _;
for (long long i = 1; i <= _; ++i) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, q, a[100101], b[100100], i, j, l, r, v;
long long sa, sb[100100], minb, maxb;
long long cauta() {
long long* p = lower_bound(sb + 1, sb + m - n + 2, sa);
long long poz = p - sb;
if (poz == 1)
return sb[1] - sa;
else if (poz == m - n + 2)
return sa - sb[poz - 1];
else
return min(abs(sb[poz] - sa), abs(sa - sb[poz - 1]));
}
int main() {
cin >> n >> m >> q;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= m; i++) cin >> b[i];
long long sgn = 1;
for (i = 1; i <= n; i++) {
sa += a[i] * sgn;
sgn = -sgn;
}
sgn = 1;
for (i = 1; i <= n; i++) {
sb[1] += sgn * b[i];
sgn = -sgn;
}
sgn = -sgn;
for (i = 2; i <= m - n + 1; i++) {
sb[i] = -sb[i - 1] + b[i - 1] + sgn * b[i + n - 1];
}
sort(sb + 1, sb + m - n + 2);
cout << cauta() << '\n';
for (i = 1; i <= q; i++) {
cin >> l >> r >> v;
if ((r - l + 1) % 2 == 1) {
if (l % 2 == 1)
sa += v;
else
sa -= v;
}
cout << cauta() << '\n';
}
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int a[n];
if (k > n) {
cout << "-1";
return 0;
}
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
cout << a[n - k] << " " << a[n - k] << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[20], n, s, x, y, i, j, d3[4] = {2, 4, 6}, d4[5] = {3, 6, 9, 12};
int t[5][5], ch = 100;
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n * n; i++) cin >> a[i];
sort(a, a + n);
bool f;
while (next_permutation(a, a + n * n) || ch) {
x = 0;
ch--;
f = true;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) t[i][j] = a[x++];
s = 0;
for (i = 0; i < n; i++) s += a[i];
for (i = 0; i < n && f; i++) {
x = y = 0;
for (j = 0; j < n && f; j++) x += t[i][j], y += t[j][i];
if (x != s || y != s) f = false;
}
if (!f) continue;
for (i = x = y = 0; i < n; i++) x += t[i][i], y += t[i][n - 1 - i];
if (x != s || y != s) continue;
cout << s << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) cout << t[i][j] << ' ';
cout << endl;
}
return 0;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
double x[3], y[3], r[3], s[3];
inline double sq(double x) { return x * x; }
inline double sc(double rx, double ry) {
int i;
for (i = 0; i < 3; i++) s[i] = sqrt(sq(rx - x[i]) + sq(ry - y[i])) / r[i];
double r = 0;
for (i = 0; i < 3; i++) r += sq(s[i] - s[(i + 1) % 3]);
return r;
}
int main() {
int i;
double rx = 0, ry = 0, d;
for (i = 0; i < 3; i++)
scanf("%lf%lf%lf", &x[i], &y[i], &r[i]), rx += x[i] / 3, ry += y[i] / 3;
d = 100;
while (d >= 1e-6) {
if (sc(rx, ry) > sc(rx + d, ry))
rx += d;
else if (sc(rx, ry) > sc(rx, ry + d))
ry += d;
else if (sc(rx, ry) > sc(rx - d, ry))
rx -= d;
else if (sc(rx, ry) > sc(rx, ry - d))
ry -= d;
else
d *= 0.70;
}
if (sc(rx, ry) < 1e-5) printf("%lf %lf\n", rx, ry);
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
int arr[100];
int rra[100];
int main() {
int a, b, c, d = 0;
cin >> a >> b;
for (int i = 0; i < b; i++) cin >> arr[i];
for (int i = 0; i < b; i++) {
c = arr[i];
d = 0;
while (c <= a) {
if (rra[d] == 0) rra[d] = arr[i];
c++;
d++;
}
}
for (int i = a - 1; i >= 0; i--) cout << rra[i] << " ";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 55;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long getRand(long long l, long long r) {
uniform_int_distribution<long long> uid(l, r);
return uid(rng);
}
long long n, k;
long long a[N];
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> k;
for (long long i = 1; i <= n; i++) cin >> a[i];
set<long long> have;
while (k > 0) {
shuffle(a + 1, a + n + 1, rng);
for (long long i = 1; i <= n && k > 0; i++) {
long long sum = 0;
for (long long j = i; j <= n && k > 0; j++) {
sum += a[j];
if (have.find(sum) == have.end()) {
k--;
cout << j - i + 1 << " ";
for (long long k = i; k <= j; k++) cout << a[k] << " ";
cout << "\n";
have.insert(sum);
}
}
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mod = 1000000007;
int n;
int ans[N], cnt[N];
vector<int> a[N], b[N];
set<int> s[N];
set<int> flag;
bool gao(int st) {
flag.clear();
int now = st;
for (int i = 0; i < n - 1; i++) {
s[i].clear();
for (int j = 0; j < a[i].size(); j++) {
s[i].insert(a[i][j]);
}
}
for (int i = 0; i < n; i++) {
ans[i] = now;
int nxt = -1;
int del = 0;
int expected = flag.size();
for (int j = 0; j < b[now].size(); j++) {
int idx = b[now][j];
s[idx].erase(now);
if (flag.find(idx) != flag.end()) {
del++;
if (s[idx].size() == 0) {
flag.erase(idx);
}
} else {
flag.insert(idx);
}
if (s[idx].size() == 1) {
if (nxt != -1) return false;
nxt = *s[idx].begin();
}
}
if (del != expected) return false;
now = nxt;
}
return true;
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
b[i].clear();
}
for (int i = 0; i < n - 1; i++) {
a[i].clear();
cin >> cnt[i];
for (int j = 0; j < cnt[i]; j++) {
int m;
cin >> m;
a[i].push_back(m);
b[m].push_back(i);
}
}
for (int st = 1; st <= n; st++) {
if (gao(st)) {
for (int i = 0; i < n - 1; i++) {
cout << ans[i] << " ";
}
cout << ans[n - 1] << endl;
break;
}
}
}
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
const int M = (1e5 + 5) * 3;
int main() {
string first, sec;
cin >> first >> sec;
if (first != sec)
cout << "1";
else
cout << first;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0, m = 0;
cin >> n >> m;
n++;
vector<int> buf(n, 0);
while (cin >> m) {
for (int i = m; i < n; i++) {
if (buf[i] == 0) {
buf[i] = m;
} else {
break;
}
}
}
for (int i = 1; i < n; i++) {
cout << buf[i] << ' ';
}
}
| 1 |
#include <bits/stdc++.h>
char c;
int neg;
int inp() {
c = getchar();
neg = 1;
while (c < '0' || c > '9') {
if (c == '-') neg = -1;
c = getchar();
}
int sum = 0;
while (c >= '0' && c <= '9') {
sum = sum * 10 + c - '0';
c = getchar();
}
return neg * sum;
}
int ch[7000010][2], cc[40];
int cnt = 1;
bool flg[7000010];
struct Node {
long long v;
int len;
bool neg;
} num[200010];
bool cmp(Node a, Node b) {
if (a.v == b.v) return a.neg < b.neg;
return a.v < b.v;
}
int ans = 0;
int res[400010][8];
void print(long long x) {
res[ans][1] = (x >> 24);
res[ans][2] = (x >> 16) % 256;
res[ans][3] = (x >> 8) % 256;
res[ans][4] = x % 256;
}
void dfs(int cur, int depth, long long num) {
if (cur == 0) return;
if (!flg[cur]) {
ans++;
print(num);
res[ans][5] = depth;
return;
}
dfs(ch[cur][0], depth + 1, num);
dfs(ch[cur][1], depth + 1, num + (1ll << (31 - depth)));
}
int main() {
int n = inp();
for (int i = 1; i <= n; i++) {
long long a = inp();
int nneg = neg;
long long b = inp();
long long c2 = inp();
long long d = inp();
if (nneg == -1) {
b = -b;
c2 = -c2;
d = -d;
}
num[i].v = (a << 24) + (b << 16) + (c2 << 8) + d;
if (c == '/') {
num[i].len = inp();
num[i].v >>= (32 - num[i].len);
} else
num[i].len = 32;
if (nneg == 1)
num[i].neg = true;
else
num[i].neg = false;
}
std::sort(num + 1, num + n + 1, cmp);
for (int i = 1; i <= n; i++) {
bool nneg = num[i].neg;
if (!nneg) num[i].v = -num[i].v;
memset(cc, 0, sizeof(cc));
int w = 0;
while (num[i].v) {
cc[++w] = (num[i].v & 1);
num[i].v >>= 1;
}
w = num[i].len;
int cur = 1;
for (int j = w; j >= 1; j--) {
if (nneg) flg[cur] = true;
if (!nneg && ch[cur][cc[j]] == 0) ch[cur][cc[j]] = ++cnt;
cur = ch[cur][cc[j]];
}
if (nneg) {
if (cur != 0) {
printf("-1\n");
return 0;
}
flg[cur] = true;
}
}
for (int i = 2; i <= cnt; i++)
if (!ch[i][0] && !ch[i][1] && flg[i]) {
printf("-1\n");
return 0;
}
dfs(1, 0, 0);
printf("%d\n", ans);
for (int i = 1; i <= ans; i++)
printf("%d.%d.%d.%d/%d\n", res[i][1], res[i][2], res[i][3], res[i][4],
res[i][5]);
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
struct data {
int x;
data(int _x) { x = _x; }
bool operator<(const data& cc) const { return x > cc.x; }
};
priority_queue<data> H1;
int a[100010], cnt, k;
long long b, now;
bool go(int x) {
now += x;
bool res = (now > b);
cnt++;
H1.push(data(x));
if (cnt >= k) {
cnt--;
now -= H1.top().x;
H1.pop();
}
return res;
}
int main() {
int i, j, n, an;
scanf("%d%d", &n, &k);
an = n;
cin >> b;
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = n - 1; i >= 1; i--) {
if (go(a[i])) an = min(an, i);
}
printf("%d\n", an);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
cout << x << endl;
exit(0);
}
using ll = long long;
const int maxn = 1e6 + 5;
int n;
int a[maxn];
vector<int> basis, vec;
void add(int x) {
int save = x;
for (int b : basis) {
x = min(x, x ^ b);
}
if (x > 0) {
basis.push_back(x);
vec.push_back(save);
sort(basis.rbegin(), basis.rend());
}
}
vector<int> gray_code(int x) {
vector<int> res = {0};
for (int i = 0; i < x; i++) {
for (int j = (1 << i) - 1; j >= 0; j--) {
res.push_back(res[j] ^ vec[i]);
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int x = 0;
int p = 0;
for (int i = 1; i < 20; i++) {
for (; p < n && a[p] < 1 << i; p++) {
add(a[p]);
}
if ((int)basis.size() == i) {
x = i;
}
}
basis.clear();
vec.clear();
for (int i = 0; i < n; i++) {
if (a[i] < (1 << x)) {
add(a[i]);
}
}
vector<int> res = gray_code(x);
cout << x << "\n";
for (int i : res) cout << i << " ";
cout << endl;
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string a, b;
cin >> a;
cin >> b;
vector<int> ans;
for (int i = 0; i < n; ++i)
if (a[i] != b[i]) {
ans.push_back(i + 1);
ans.push_back(1);
ans.push_back(i + 1);
}
cout << ans.size() << " ";
for (int i = 0; i < ans.size(); ++i) cout << ans[i] << " ";
cout << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << n / 2 << endl;
for (int i = 1; i < n / 2; i++) {
cout << "2 ";
}
if (n % 2 == 0) {
cout << "2";
} else {
cout << "3";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
stack<char> st;
for (int i = 0; i < n; i++) {
int m;
cin >> m;
string s;
cin >> s;
while (!st.empty()) {
st.pop();
}
int ans = m / 2, len = s.size();
for (int i = 0; i < len; i++) {
if (s[i] == '(')
st.push('(');
else {
if (st.size() != 0) {
st.pop();
ans--;
}
}
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int p[1000], sum[1000], good[1000], n, k, m, x, y;
int find(int x) {
if (p[x] == x)
return x;
else
return p[x] = find(p[x]);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 1; i <= k; i++) {
cin >> x >> y;
p[find(y)] = find(x);
}
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
if (find(x) == find(y)) good[find(x)] = -1;
}
m = 0;
for (int i = 1; i <= n; i++) sum[find(i)]++;
for (int i = 1; i <= n; i++) {
x = find(i);
if (good[find(i)] == 0) m = max(m, sum[x]);
}
cout << m;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int x1, y1, x2, y2;
map<pair<long long int, long long int>, int> m;
int test = 0;
int hor = 0;
int ver = 0;
for (int i = 0; i < 4; i++) {
cin >> x1 >> y1 >> x2 >> y2;
if ((y2 - y1) != 0) {
ver++;
}
if ((x2 - x1) != 0) {
hor++;
}
pair<long long int, long long int> p1 = make_pair(x1, y1);
pair<long long int, long long int> p2 = make_pair(x2, y2);
if (m[p1] == 1) {
test++;
}
if (m[p2] == 1) {
test++;
}
m[p1]++;
m[p2]++;
}
if (test != 4 || ver != 2 || hor != 2) {
cout << "NO";
} else {
cout << "YES";
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
int main(void) {
unsigned int n, m;
scanf("%d %d", &n, &m);
unsigned int points = m * n;
unsigned int moves = 0;
while (points != 0) {
points -= (m + n - 1);
m--;
n--;
moves++;
}
(moves % 2 != 0) ? printf("Akshat\n") : printf("Malvika\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string tar;
queue<int> q;
set<string> ans;
bool vis[60000][5] = {false}, check[60000] = {false};
int main() {
cin >> tar;
int len = tar.length();
vis[len][3] = true;
vis[len][2] = true;
q.push(len - 2);
q.push(len - 3);
while (!q.empty()) {
int pos = q.front();
q.pop();
if (pos >= 5 && (vis[pos + 2][2] == true || vis[pos + 2][3] == true)) {
if (pos + 2 == len) {
vis[pos][2] = true;
string s = tar.substr(pos, 2);
ans.insert(s);
} else {
if (vis[pos + 2][2] == true) {
string s = tar.substr(pos, 2), t = tar.substr(pos + 2, 2);
if (s != t) {
vis[pos][2] = true;
ans.insert(s);
}
}
if (vis[pos + 2][3] == true) {
string s = tar.substr(pos, 2);
vis[pos][2] = true;
ans.insert(s);
}
}
}
if (pos >= 5 && (vis[pos + 3][2] == true || vis[pos + 3][3] == true)) {
if (pos + 3 == len) {
vis[pos][3] = true;
string s = tar.substr(pos, 3);
ans.insert(s);
} else {
if (vis[pos + 3][3] == true) {
string s = tar.substr(pos, 3), t = tar.substr(pos + 3, 3);
if (s != t) {
vis[pos][3] = true;
ans.insert(s);
}
}
if (vis[pos + 3][2] == true) {
string s = tar.substr(pos, 3);
vis[pos][3] = true;
ans.insert(s);
}
}
}
if (pos - 2 >= 5 && check[pos - 2] == false) {
check[pos - 2] = true;
q.push(pos - 2);
}
if (pos - 3 >= 5 && check[pos - 3] == false) {
check[pos - 3] = true;
q.push(pos - 3);
}
}
printf("%d\n", ans.size());
for (set<string>::iterator iter = ans.begin(); iter != ans.end(); iter++) {
printf("%s\n", iter->c_str());
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
void out(bool f) {
if (f & 1)
cout << "Stannis" << endl;
else
cout << "Daenerys" << endl;
}
int main() {
int odd = 0, even = 0;
int n, k;
cin >> n >> k;
k = n - k;
for (int i = 0, x; i < n && cin >> x; i++)
if (x & 1)
++odd;
else
++even;
if (k == 0)
out(odd & 1);
else if (odd == 0)
out(0);
else if (even == 0)
out((n - k) & 1);
else {
int x = k / 2, y = k / 2;
if (k & 1) {
if (odd <= y)
out(0);
else if (y >= even && (n - k & 1) == 0)
out(0);
else
out(1);
} else {
if (even <= x && n - k & 1)
out(1);
else
out(0);
}
}
return 0;
}
| 14 |
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
typedef long long int ll;
#define MAX 1000000000
#define mod 1000000007
ll gcd(ll a, ll b){
if (a == 0)
return b;
else
return gcd(b % a, a);
}
ll lcm(ll a, ll b) {
return (a*b)/gcd(a, b);
}
ll fact(ll n){
if(n==0)
return 1;
else
return n*fact(n-1);
}
bool dig(char S){
return S>='0' && S<='9';
}
bool sma(char S){
return S>='a' && S<='z';
}
bool cap(char S){
return S>='A' && S<='Z';
}
ll nCr(ll n, ll r){
return (fact(n) / (fact(r)* fact(n - r)));
}
void solve(ll t){
ll N,X;
cin>>N>>X;
ll A[N],i,c1=0,c2=0,s=0;
for(i=0;i<N;i++){
cin>>A[i];
c1 += A[i]/X;
if(A[i]%X!=0)
c1++;
s += A[i];
}
c2 = s/X;
if(s%X!=0)
c2++;
cout<<min(c1,c2)<<" "<<max(c1,c2);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("D:\\codes\\input_10.txt","r",stdin);
//freopen("D:\\codes\\1output.txt","w",stdout);
ll T=1;
cin>>T;
for(ll t=1;t<=T;t++){
//cout<<"Case #"<<t<<": ";
solve(t);
cout<<"\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[10];
bool isprime(int z) {
if (z == 4 || z == 6 || z == 8 || z == 9) return false;
return true;
}
int main() {
int z;
cin >> z;
string s;
cin >> s;
int mx = 0;
for (int i = 0; i < z; i++) {
if (s[i] - '0' == 0 || s[i] - '0' == 1) continue;
for (int f = s[i] - '0'; f > 1; f--) {
if (isprime(f)) {
a[f]++;
mx = max(mx, f);
} else {
int n = f;
while (n % 2 == 0) {
a[2]++;
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
a[i]++;
n = n / i;
}
}
if (n > 2) a[n]++;
}
}
}
string ans = "";
for (int i = mx; i > 1; i--) {
if (isprime(i) == false) continue;
for (int j = 0; j < a[i]; j++) {
ans += (i + '0');
}
for (int j = i - 1; j > 1; j--) {
if (isprime(j) == false) {
int n = j;
while (n % 2 == 0) {
a[2] -= a[i];
n = n / 2;
}
for (int k = 3; k <= sqrt(n); k = k + 2) {
while (n % k == 0) {
a[k] -= a[i];
n = n / k;
}
}
if (n > 2) a[n] -= a[i];
} else
a[j] = a[j] - a[i];
}
}
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k, l, m, n, r1, r2, q;
cin >> n >> m;
string s[n + 10];
string t[m + 10];
for (i = 0; i < n; i++) cin >> s[i];
for (i = 0; i < m; i++) cin >> t[i];
cin >> q;
while (q--) {
cin >> l;
l--;
r1 = l % n;
r2 = l % m;
cout << s[r1] << t[r2] << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
set<int> s;
vector<int> ans;
int main() {
int n, m, a;
cin >> n >> m;
for (int i = 0; i < n; i++) {
scanf("%d", &a);
s.insert(a);
}
int i = 1, an = 0;
while (m >= i) {
if (s.find(i) == s.end()) {
an++;
ans.push_back(i);
m -= i;
}
i++;
}
cout << an << endl;
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " \n"[i == ans.size() - 1];
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SI = 1 << 21 | 1;
char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
int f, t;
inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }
inline void pc(char x) {
*OS++ = x;
if (OS == OT) flush();
}
template <class I>
inline void rd(I &x) {
for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++);
c < '0' || c > '9';
c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
if (c == '-') f = -1;
for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15),
c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
;
x *= f;
}
inline void rds(char *s, int &x) {
for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++);
c < 33 || c > 126;
c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
;
for (x = 0; c >= 33 && c <= 126;
s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
;
s[x + 1] = '\0';
}
template <class I>
inline void print(I x, char k = '\n') {
if (!x) pc('0');
if (x < 0) pc('-'), x = -x;
while (x) ch[++t] = x % 10 + '0', x /= 10;
while (t) pc(ch[t--]);
pc(k);
}
inline void prints(string s) {
int x = s.length();
while (t < x) pc(s[t++]);
pc('\n'), t = 0;
}
struct Flush {
~Flush() { flush(); }
} flusher;
} // namespace io
using io::print;
using io::prints;
using io::rd;
using io::rds;
const int N = 2e5 + 7;
int n, q, a[N], c[N], sz;
map<pair<int, int>, int> p;
long long s;
inline void del(int x, int y) {
--sz;
int z = p[make_pair(x, y)];
p.erase(make_pair(x, y));
if (c[z] > a[z]) --s;
c[z]--;
}
inline void add(int x, int y, int z) {
++sz;
p[make_pair(x, y)] = z;
++c[z];
if (c[z] > a[z]) ++s;
}
int main() {
rd(n);
for (int i = 1; i <= n; i++) rd(a[i]), s += a[i];
rd(q);
while (q--) {
int x, y, z;
rd(x), rd(y), rd(z);
if (p[make_pair(x, y)]) del(x, y);
if (z) add(x, y, z);
print(s - sz);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
namespace my_std {
bool ccc(long long a, long long b) { return a > b; }
long long my_pow(long long a, long long b, long long mod) {
long long res = 1;
if (!b) return 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
long long qpow(long long a, long long b) {
long long res = 1;
if (!b) return 1;
while (b) {
if (b & 1) res *= a;
a *= a;
b >>= 1;
}
return res;
}
inline long long read() {
long long sum = 0, f = 1;
char ch = 0;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
sum = sum * 10 + (ch ^ 48);
ch = getchar();
}
return sum * f;
}
inline void write(long long x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(long long x) {
write(x);
putchar('\n');
}
inline void writesp(long long x) {
write(x);
putchar(' ');
}
} // namespace my_std
using namespace my_std;
long long n, h, f[36][36];
int main() {
n = read();
h = read();
for (register long long i = (0); i <= (n); i++) f[0][i] = 1;
for (register long long i = (1); i <= (n); i++)
for (register long long j = (1); j <= (n); j++)
for (register long long k = (0); k <= (j - 1); k++)
f[j][i] += f[k][i - 1] * f[j - k - 1][i - 1];
write(f[n][n] - f[n][h - 1]);
}
| 11 |
#include<cstdio>
#include<algorithm>
using namespace std;
const int md=1000000007;
int f[101][10001];
int a[101],b[101],x[100001],ans[100001];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=2;i<=n;i++){
scanf("%d",&b[i]);
b[i]+=b[i-1];
}
for(int i=2;i<=n;i++)
b[i]+=b[i-1];
int q;
scanf("%d",&q);
for(int i=1;i<=q;i++)
scanf("%d",&x[i]);
int mn=2e9,mx=2e9;
for(int i=1,z=0;i<=n;i++){
mn=min(mn,-b[i]/i);
z+=a[i];
mx=min(mx,(z-b[i])/i+1);
}
mn--;
f[0][0]=1;
for(int j=1;j<=10000;j++)
f[0][j]=(f[0][j]+f[0][j-1])%md;
for(int o=mn;o<=mx;o++){
for(int i=1;i<=n;i++){
for(int j=0;j<=10000;j++){
if(j-b[i]>=i*o){
int k=min(a[i],j);
f[i][j]=(f[i-1][j]-(j-k-1==-1?0:f[i-1][j-k-1])+md)%md;
}
else f[i][j]=0;
}
for(int j=1;j<=10000;j++)
f[i][j]=(f[i][j]+f[i][j-1])%md;
}
ans[o-mn]=f[n][10000];
}
for(int i=1;i<=q;i++){
x[i]=max(x[i],mn);
x[i]=min(x[i],mx);
printf("%d\n",ans[x[i]-mn]);
}
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
int vis[110];
int main() {
int n, m, a, b;
cin >> n >> m;
memset(vis, 0, sizeof(vis));
while (n--) {
cin >> a >> b;
for (int i = a + 1; i <= b; i++) vis[i] = 1;
}
int sum = 0;
for (int i = 1; i <= m; i++)
if (vis[i]) sum++;
if (sum == m)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs.second < rhs.second;
}
int main() {
int n;
cin >> n;
pair<int, int> a[500001];
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
sort(a, a + n, cmp);
int ans = 0, cur = 0;
for (int i = 0; i < n; i++)
if (a[i].first > cur) ans++, cur = a[i].second;
cout << ans;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, p;
cin >> n >> p;
set<pair<int, int> > t;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
t.insert({x, i});
}
queue<int> qu;
qu.push((*t.begin()).second);
long long time = (*t.begin()).first;
long long res[n];
t.erase(t.begin());
set<int> cur;
set<int> elig;
cur.insert(qu.front());
for (int i = 0; i < n; i++) {
int x = qu.front();
qu.pop();
cur.erase(x);
time += p;
res[x] = time;
vector<pair<int, int> > tmp;
for (auto it : t) {
if (it.first > time) break;
tmp.push_back(it);
if (it.second < x && (cur.empty() || it.second < *cur.begin())) {
cur.insert(it.second);
qu.push(it.second);
} else {
elig.insert(it.second);
}
}
for (int k = 0; k < tmp.size(); k++) {
t.erase(tmp[k]);
}
if (!elig.empty() && (qu.empty() || (*elig.begin() < *cur.begin()))) {
qu.push(*elig.begin());
cur.insert(*elig.begin());
elig.erase(elig.begin());
}
if (qu.empty() && !t.empty()) {
qu.push((*t.begin()).second);
cur.insert((*t.begin()).second);
time = (*t.begin()).first;
t.erase(t.begin());
}
}
for (int i = 0; i < n; i++) cout << res[i] << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int q = 1;
for (int kase = 1; kase <= q; kase++) {
solve();
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5001;
long long d[2][maxn * 2];
long long get(long long a, long long l, long long r) {
if (a < l) {
return l - a;
} else if (a > r) {
return a - r;
}
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
long long x;
cin >> n >> x;
vector<long long> A(1, x);
vector<long long> l(n), r(n);
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
A.push_back(l[i]);
A.push_back(r[i]);
}
sort(A.begin(), A.end());
A.resize(unique(A.begin(), A.end()) - A.begin());
for (int i = 0; i < 2; i++) {
for (int j = 0; j < (int((A).size())); j++) {
d[i][j] = LLONG_MAX / 2;
}
}
d[0][lower_bound(A.begin(), A.end(), x) - A.begin()] = 0;
for (int i = 1; i <= n; i++) {
long long now = 0;
for (int j = 0; j < (int((A).size())); j++) {
if (j == 0) {
now = d[0][j];
} else {
now += A[j] - A[j - 1];
now = min(now, d[0][j]);
}
d[1][j] = now + get(A[j], l[i - 1], r[i - 1]);
}
for (int j = (int((A).size())) - 1; j >= 0; j--) {
if (j == (int((A).size())) - 1) {
now = d[0][j];
} else {
now += A[j + 1] - A[j];
now = min(now, d[0][j]);
}
d[1][j] = min(d[1][j], now + get(A[j], l[i - 1], r[i - 1]));
}
for (int j = 0; j < (int((A).size())); j++) {
d[0][j] = d[1][j];
d[1][j] = LLONG_MAX / 2;
}
}
long long res = LLONG_MAX;
for (int j = 0; j < (int((A).size())); j++) {
res = min(res, d[0][j]);
}
cout << res << '\n';
return 0;
}
| 13 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using std::abs;
using std::cerr;
using std::cin;
using std::cout;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::set;
using std::string;
using std::swap;
using std::vector;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
void _dbg(const char* _s, T _h) {
cerr << _s << " = " << _h << "\n";
}
template <typename T, typename... Ts>
void _dbg(const char* _s, T _h, Ts... _t) {
int _b = 0;
while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',') cerr << *_s++;
cerr << " = " << _h << ",";
_dbg(_s + 1, _t...);
}
struct init {
init() {
cin.tie(0);
std::iostream::sync_with_stdio(0);
cout << std::fixed << std::setprecision(10);
cerr << std::fixed << std::setprecision(5);
}
~init() {}
} init;
const int MAXN = 2e5 + 5;
const ll INF = 1e16;
template <class T, class plus = std::plus<T>, class minus = std::minus<T>>
struct fenwick_tree {
ll n;
T E;
vector<T> tree;
fenwick_tree(ll _n, T _E = T()) : n(_n), E(_E) { tree.assign(n + 1, E); }
fenwick_tree(const vector<ll>& a, T _E = T()) : n((ll)a.size()), E(_E) {
tree.assign(n + 1, E);
for (ll i = 0; i < n; ++i) add(i + 1, a[i]);
}
inline T get(ll i) {
T res = E;
for (; i; i -= i & -i) res = plus()(res, tree[i]);
return res;
}
inline void add(ll i, T x) {
for (; i <= n; i += i & -i) tree[i] = plus()(tree[i], x);
}
inline T get(ll l, ll r) { return minus()(get(r), get(l)); }
};
fenwick_tree<ll> fwt(MAXN);
ll timer = 0;
ll from[MAXN], to[MAXN], tin[MAXN], tout[MAXN];
ll w[MAXN], p[MAXN];
vector<ll> g[MAXN];
std::deque<pii> s[MAXN];
void no() {
cout << "-1\n";
exit(0);
}
void dfs(ll v, ll num = 0, ll d = 0) {
;
tin[v] = ++timer;
for (ll e : g[v]) {
dfs(to[e], e, d + 1);
if (s[to[e]].size() > s[v].size()) swap(s[to[e]], s[v]);
for (pii i : s[to[e]]) s[v].push_back(i);
}
tout[v] = timer;
while (fwt.get(tin[v], tout[v]) > p[num]) {
;
if (s[v].empty()) no();
pii pp = s[v].front();
s[v].pop_front();
ll need = fwt.get(tin[v], tout[v]) - p[num];
ll can = min(w[pp.second] - 1, p[pp.second] - fwt.get(tin[to[pp.second]],
tout[to[pp.second]]));
ll boom = min(can, need);
;
if (boom <= 0) continue;
w[pp.second] -= boom;
p[pp.second] -= boom;
fwt.add(tin[to[pp.second]], -boom);
if (w[pp.second] > 1) s[v].push_front(pp);
}
if (w[num] > 1) {
s[v].push_back({d, num});
}
fwt.add(tin[v], w[num]);
;
}
int32_t main() {
ll n;
cin >> n;
for (ll i = 1; i < n; ++i) {
cin >> from[i] >> to[i] >> w[i] >> p[i];
g[from[i]].emplace_back(i);
}
p[0] = INF;
dfs(1);
cout << n << '\n';
for (ll i = 1; i < n; ++i)
cout << from[i] << ' ' << to[i] << ' ' << w[i] << ' ' << p[i] << '\n';
return cos(1488) - cos(228);
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
int v[200010];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> v[i];
sort(v, v + n);
int mn = INT_MAX;
for (int i = 1; i < n; ++i) mn = min(abs(v[i] - v[i - 1]), mn);
int ans = 0;
for (int i = 1; i < n; ++i)
if (abs(v[i] - v[i - 1]) == mn) ans++;
cout << mn << " " << ans << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
bool Finish_read;
template <class T>
inline void read(T &x) {
Finish_read = 0;
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
if (ch == EOF) return;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x *= f;
Finish_read = 1;
}
template <class T>
inline void print(T x) {
if (x / 10 != 0) print(x / 10);
putchar(x % 10 + '0');
}
template <class T>
inline void writeln(T x) {
if (x < 0) putchar('-');
x = abs(x);
print(x);
putchar('\n');
}
template <class T>
inline void write(T x) {
if (x < 0) putchar('-');
x = abs(x);
print(x);
}
int d[100010], t[100010], f[100010], n, m;
vector<int> G[100010], O;
queue<int> q;
int main() {
read(n);
read(m);
for (int i = 1; i <= n; i++) read(t[i]);
for (int i = 1, x, y; i <= m; i++) {
read(x);
read(y);
x++;
y++;
G[y].push_back(x);
d[x]++;
}
for (int i = 1; i <= n; i++)
if (!d[i]) {
q.push(i);
f[i] = t[i];
}
while (!q.empty()) {
int u = q.front();
q.pop();
O.push_back(u);
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
d[v]--;
if (!d[v]) q.push(v);
}
}
for (int i = 0; i < O.size(); i++) {
int u = O[i];
for (int j = 0; j < G[u].size(); j++) {
int v = G[u][j];
if (t[v] && !t[u])
f[v] = max(f[v], f[u] + 1);
else
f[v] = max(f[v], f[u]);
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (t[i]) ans = max(ans, f[i]);
printf("%d\n", ans);
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k, ans = 0, x = 0;
cin >> n >> k;
string s, a;
cin >> s;
sort(s.begin(), s.end());
a.push_back(s[0]);
for (long long i = 1; i < n; i++) {
if (a[x] < s[i] - 1) a.push_back(s[i]), x++;
}
if (k > a.size()) {
cout << -1 << '\n';
return;
}
for (long long i = 0; i < k; i++) {
ans += a[i] - 'a' + 1;
}
cout << ans << '\n';
return;
}
int main() {
long long TESTS;
TESTS = 1;
while (TESTS--) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t, n, m, val, sum;
cin >> t;
while (t--) {
cin >> n >> m;
sum = 0;
for (int i = 0; i < n; i++) {
cin >> val;
sum += val;
}
cout << (sum == m ? "YES" : "NO") << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, j;
cin >> n;
long long int a[n], s = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
}
if (s % 2) {
cout << "NO\n";
return 0;
}
s /= 2;
long long int ss = 0;
map<long long int, long long int> mp;
for (j = 0; j <= n - 1; j++) {
ss += a[j];
mp[a[j]]++;
if (mp[ss - s]) {
cout << "YES\n";
return 0;
}
}
ss = 0;
mp.clear();
for (j = n - 1; j >= 0; j--) {
ss += a[j];
mp[a[j]]++;
if (mp[ss - s]) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int ps[MAX_N], cns[MAX_N], ss[MAX_N];
vector<int> nbrs[MAX_N];
double es[MAX_N];
int main() {
int n;
scanf("%d", &n);
ps[0] = -1;
for (int i = 1; i < n; i++) {
scanf("%d", ps + i), ps[i]--;
cns[ps[i]]++;
nbrs[ps[i]].push_back(i);
}
queue<int> q;
for (int u = 0; u < n; u++)
if (cns[u] == 0) q.push(u);
while (!q.empty()) {
int u = q.front();
q.pop();
int up = ps[u];
ss[u]++;
if (up >= 0) {
ss[up] += ss[u];
if (--cns[up] == 0) q.push(up);
}
}
es[0] = 1.0;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
vector<int> &nbru = nbrs[u];
for (vector<int>::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
int v = *vit;
es[v] = es[u] + (ss[u] - ss[v] + 1) * 0.5;
q.push(v);
}
}
for (int u = 0; u < n; u++) {
printf("%.1lf", es[u]);
putchar(u + 1 < n ? ' ' : '\n');
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string s, t;
cin >> s >> t;
int st = 0;
while (s[st] == t[st] && st < k) {
cout << s[st];
st++;
}
vector<int> q;
int p = 0;
for (int i = k - 1; i >= st; --i) {
int s1 = s[i] - 'a';
int t1 = t[i] - 'a';
int q1 = (s1 + t1 + p);
q.push_back(q1 % 26);
p = q1 / 26;
}
vector<int> ans;
for (int i = k - st - 1; i >= 0; --i) {
int q1 = q[i] + p * 26;
ans.push_back(q1 / 2);
p = q1 % 2;
}
for (auto i = ans.begin(); i != ans.end(); ++i) {
cout << char(*i + 'a');
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
char s[2000010];
int a[2000010];
vector<int> q;
inline void check(int x, int rnd) {
if (a[x]) return;
a[x] = rnd;
int now = s[x];
if (rnd & 1) now = 'W' + 'B' - now;
int last = (x - 1 + n) % n, nxt = (x + 1) % n;
if ((a[last] && a[last] != rnd) && (a[nxt] && a[nxt] != rnd))
s[x] = now;
else if (a[last] && (a[last] != rnd))
s[x] = s[last];
else
s[x] = s[nxt];
q.push_back(x);
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (s[i] == s[(i - 1 + n) % n] || s[i] == s[(i + 1) % n])
a[i] = -1, q.push_back(i);
}
for (int i = 1; i <= k; i++) {
if (q.empty()) break;
vector<int> tmp = q;
q.clear();
for (auto j : tmp) {
check((j - 1 + n) % n, i);
check((j + 1) % n, i);
}
}
for (int i = 0; i < n; i++) {
if (a[i] == 0 && (k % 2)) s[i] = 'W' + 'B' - s[i];
}
return puts(s), 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
const int maxe = 100000;
int a[maxn], b[maxn];
int c[maxn], p[maxn];
bool vis[maxn];
int cc[maxn][maxn];
char tt[maxn];
int par[maxn], high[maxn];
void init(int n) {
for (int i = 0; i <= n; i++) {
par[i] = i;
high[i] = 0;
}
}
int Find(int x) {
if (par[x] == x)
return x;
else
return par[x] = Find(par[x]);
}
void unite(int x, int y) {
x = Find(x);
y = Find(y);
if (x == y) return;
if (high[x] < high[y])
par[x] = y;
else {
par[y] = x;
if (high[x] == high[y]) high[x]++;
}
}
bool same(int x, int y) { return Find(x) == Find(y); }
int main() {
int n, ta;
scanf("%d", &n);
init(n);
memset(cc, 0, sizeof(cc));
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) {
scanf("%s", tt);
for (int j = 0; j < n; j++)
if (tt[j] == '1') cc[i][j] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (cc[i][j]) unite(i, j);
}
}
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
if (vis[i]) continue;
ta = 0;
p[ta] = i;
c[ta++] = a[i];
vis[i] = 1;
for (int j = 0; j < n; j++) {
if (i == j || vis[j]) continue;
if (same(i, j)) {
p[ta] = j;
c[ta++] = a[j];
vis[j] = 1;
}
}
sort(c, c + ta);
sort(p, p + ta);
for (int i = 0; i < ta; i++) b[p[i]] = c[i];
}
for (int i = 0; i < n; i++) printf("%d%c", b[i], i == n - 1 ? '\n' : ' ');
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
int n, mx;
vector<int> v;
vector<int> add[1000000];
pair<int, int> d[1000000];
int main() {
cin >> n;
vector<int> v(2 * n, -1);
for (int i = 0; i < n; i++) {
scanf("%d", &d[i].first);
d[i].second = i + 1;
}
sort(d, d + n);
reverse(d, d + n);
for (int i = 0; i < n; i++) {
v[i] = d[i].second * 2 - 1;
if (v[i + d[i].first] == -1 && i + d[i].first >= n) {
v[i + d[i].first] = d[i].second * 2;
mx = i + d[i].first;
} else
add[i + d[i].first - 1].push_back(d[i].second * 2);
}
for (int i = 0; i <= mx; i++) {
if (i) printf("%d %d\n", v[i - 1], v[i]);
for (int a : add[i]) printf("%d %d\n", v[i], a);
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, k, endX, endY;
int use[20][20];
int pos[2][20];
int dir[4] = {0, -1, 0, 1};
char A[20][20];
map<vector<int>, int> M;
vector<vector<int> > c;
inline int opposite(int x) {
if (x & 1) return 4 - x;
return 2 - x;
}
int solvable() {
use[endX][endY] = 1;
for (int t = 1; t <= n * m; t++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (A[i][j] != '#')
for (int k = 0; k < 4; k++)
use[i][j] = max(use[i][j], use[i + dir[k]][j + dir[3 - k]]);
return use[pos[0][1]][pos[1][1]];
}
void solve() {
if (!solvable()) {
printf("-1\n");
return;
}
vector<int> init;
init.push_back(pos[0][1]);
init.push_back(pos[1][1]);
for (int i = 2; i <= k; i++) {
for (int j = 0; j < 4; j++) {
int NewX = pos[0][i - 1] + dir[j];
int NewY = pos[1][i - 1] + dir[3 - j];
if (NewX == pos[0][i] && NewY == pos[1][i]) init.push_back(j);
}
}
c.push_back(init);
M[init] = 0;
int st = -1, dr = 0;
while (st != dr) {
st++;
int x = c[st][0];
int y = c[st][1];
use[x][y] = st + 5;
for (int i = 2; i < k; i++) {
x = x + dir[c[st][i]];
y = y + dir[3 - c[st][i]];
use[x][y] = st + 5;
}
x = c[st][0];
y = c[st][1];
for (int i = 0; i < 4; i++) {
int NewX = x + dir[i];
int NewY = y + dir[3 - i];
if (use[NewX][NewY] != st + 5 && A[NewX][NewY] != '#' && 0 < NewX &&
NewX <= n && 0 < NewY && NewY <= m) {
vector<int> NewConf;
NewConf.reserve(k + 1);
NewConf.push_back(NewX);
NewConf.push_back(NewY);
NewConf.push_back(opposite(i));
for (int j = 2; j < k; j++) NewConf.push_back(c[st][j]);
map<vector<int>, int>::iterator it = M.find(NewConf);
if (it == M.end()) {
if (NewX == endX && NewY == endY) {
printf("%d\n", M[c[st]] + 1);
return;
}
M[NewConf] = M[c[st]] + 1;
c.push_back(NewConf);
dr++;
}
}
}
}
printf("-1\n");
}
int main() {
scanf("%d %d\n", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", A[i] + 1);
A[i][0] = ' ';
for (int j = 1; j <= m; j++) {
if ('0' <= A[i][j] && A[i][j] <= '9') {
k = max(k, A[i][j] - '0');
pos[0][A[i][j] - '0'] = i;
pos[1][A[i][j] - '0'] = j;
}
if (A[i][j] == '@') {
endX = i;
endY = j;
}
}
}
solve();
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int D[100000], S[100000];
int main() {
int N;
cin >> N;
queue<int> Q;
for (int i = 0; i < N; i++) {
cin >> D[i] >> S[i];
if (D[i] == 1) {
Q.push(i);
}
}
vector<pair<int, int> > edges;
while (!Q.empty()) {
int i = Q.front();
Q.pop();
if (D[i] != 1) continue;
int v = S[i];
edges.push_back({i, v});
D[i]--;
D[v]--;
S[i] ^= v;
S[v] ^= i;
if (D[v] == 1) Q.push(v);
}
cout << edges.size() << endl;
for (auto it : edges) cout << it.first << " " << it.second << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e5 + 5;
const int maxm = 2e7 + 5;
const int mod = 1e9 + 7;
int n, rev[maxn], t[maxn];
char s[maxn], tmp[4][maxn];
int mul_inv(int a, int b) {
int b0 = b, t, q;
int x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b;
b = a % b;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
int chinese_remainder(vector<int> a, vector<int> b, int n) {
int p, prod = 1, sum = 0;
for (int i = 0; i < n; ++i) prod *= b[i];
for (int i = 0; i < n; ++i) {
p = prod / b[i];
sum += a[i] * mul_inv(p, b[i]) * p;
}
return sum % prod;
}
int main() {
scanf("%s", s);
int n = strlen(s);
for (int m = 26, j = 0; m >= 23; --m, j = 0) {
if (m == 24) continue;
for (int i = 0; i < n; ++i) {
tmp[26 - m][i] = (char)('a' + j);
++j;
j %= m;
}
}
for (int i = 0; i <= 3; ++i) {
if (i == 2) continue;
printf("? %s\n", tmp[i]);
fflush(stdout);
scanf("%s", tmp[i]);
}
for (int i = 0; i < n; ++i)
t[i] = chinese_remainder(
vector<int>{tmp[0][i] - 'a', tmp[1][i] - 'a', tmp[3][i] - 'a'},
vector<int>{26, 25, 23}, 3);
for (int i = 0; i < n; ++i) rev[t[i]] = i;
printf("! ");
for (int i = 0; i < n; ++i) printf("%c", s[rev[i]]);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
mt19937_64 mt(time(0));
long long gcd(long long a, long long b) {
if (a > b) swap(a, b);
if (a == 0) return b;
return gcd(b % a, a);
}
pair<long long, long long> get_res(long long a, long long b, long long n,
long long c) {
long long x = (n + 1) * a + n * b;
long long y = 2 * n + 1;
x = abs(x - y * c);
long long tmp = gcd(x, y);
return {x / tmp, y / tmp};
}
bool my_less(pair<long long, long long> x, pair<long long, long long> y) {
return x.first * y.second < y.first * x.second;
}
int solve(int a, int b, int c) {
if (c >= a) return 1;
if ((a + b) >= c * 2) return 2;
int tmp = (a - c) / (2 * c - a - b);
pair<long long, long long> res = get_res(a, b, tmp, c);
int ret = tmp;
if (tmp - 1 >= 0) {
pair<long long, long long> res1 = get_res(a, b, tmp - 1, c);
if (my_less(res1, res)) {
res = res1;
ret = tmp - 1;
}
}
pair<long long, long long> res2 = get_res(a, b, tmp + 1, c);
if (my_less(res2, res)) {
ret = tmp + 1;
}
return 2 * ret + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(10);
cout << fixed;
int t;
cin >> t;
while (t--) {
int a, b, c;
cin >> a >> b >> c;
cout << solve(a, b, c) << '\n';
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
char a, b;
int count, mx = 0;
int i, j;
cin >> n;
cin >> s;
for (i = 0; i < n - 1; i++) {
count = 0;
for (j = 0; j < n - 1; j++) {
if (s[j] == s[i] && s[j + 1] == s[i + 1]) count++;
}
if (count > mx) {
mx = count;
a = s[i];
b = s[i + 1];
}
}
cout << a << b;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
string s[MAXN], str[MAXN];
pair<string, string> e[MAXN];
map<string, int> mp;
map<string, pair<long long, long long>> mincost;
pair<int, int> num[MAXN];
int num2[MAXN], len[MAXN];
vector<int> par[MAXN];
bool v[MAXN];
bool cmp(pair<int, int> v1, pair<int, int> v2) {
if (v1.first < v2.first) return true;
if (v1.first > v2.first) return false;
if (len[v1.second] < len[v2.second]) return true;
return false;
}
void dfs(int cur) {
v[cur] = true;
for (auto x : par[cur]) {
if (v[x]) continue;
if (num2[x] > num2[cur]) {
num2[x] = num2[cur];
len[x] = len[cur];
dfs(x);
} else if (num2[x] == num2[cur] && len[x] > len[cur]) {
len[x] = len[cur];
dfs(x);
}
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int m, n;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> s[i];
for (int j = 0; j < s[i].length(); j++)
if (s[i][j] >= 'A' && s[i][j] <= 'Z') s[i][j] = 'a' + s[i][j] - 'A';
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> e[i].first >> e[i].second;
for (int j = 0; j < e[i].first.length(); j++)
if (e[i].first[j] >= 'A' && e[i].first[j] <= 'Z')
e[i].first[j] = 'a' + e[i].first[j] - 'A';
for (int j = 0; j < e[i].second.length(); j++)
if (e[i].second[j] >= 'A' && e[i].second[j] <= 'Z')
e[i].second[j] = 'a' + e[i].second[j] - 'A';
}
int col = 1;
for (int i = 0; i < n; i++) {
int id1, id2;
if (!mp[e[i].first]) {
int cur = 0;
for (int j = 0; j < e[i].first.length(); j++)
if (e[i].first[j] == 'r' || e[i].first[j] == 'R') cur++;
num[col] = {cur, col};
num2[col] = cur;
len[col] = e[i].first.length();
str[col] = e[i].first;
mp[e[i].first] = col++;
}
if (!mp[e[i].second]) {
int cur = 0;
for (int j = 0; j < e[i].second.length(); j++)
if (e[i].second[j] == 'r' || e[i].second[j] == 'R') cur++;
num[col] = {cur, col};
num2[col] = cur;
len[col] = e[i].second.length();
str[col] = e[i].second;
mp[e[i].second] = col++;
}
id1 = mp[e[i].first], id2 = mp[e[i].second];
par[id2].push_back(id1);
}
sort(num + 1, num + col, cmp);
for (int i = 1; i < col; i++) {
if (v[num[i].second]) continue;
v[num[i].second] = true;
for (auto x : par[num[i].second]) {
if (num2[x] > num[i].first ||
(num2[x] == num[i].first && len[x] > len[num[i].second])) {
if (num2[x] > num[i].first) num2[x] = num[i].first;
len[x] = len[num[i].second];
dfs(x);
}
}
}
for (int i = 1; i < col; i++) mincost[str[i]] = {num2[i], len[i]};
long long res = 0, reslen = 0;
for (int i = 0; i < m; i++) {
if (mp[s[i]]) {
res += mincost[s[i]].first;
reslen += mincost[s[i]].second;
} else {
reslen += s[i].length();
for (int j = 0; j < s[i].length(); j++)
if (s[i][j] == 'R' || s[i][j] == 'r') res++;
}
}
cout << res << " " << reslen << "\n";
}
| 16 |
#include <bits/stdc++.h>
int isgood[128];
char goods[32];
int plen;
char pat[100001];
int starpos;
char query[100001];
int match(int i, int j, int n) {
while (n-- > 0) {
if (query[i] != pat[j]) {
if ('?' != pat[j]) return 0;
if (0 == isgood[query[i]]) return 0;
}
i++;
j++;
}
return 1;
}
int ok(void) {
int ql = strlen(query);
if (starpos < 0) {
if (ql != plen) return 0;
return match(0, 0, ql);
} else {
int i = 0;
if (ql < plen - 1) return 0;
if (!match(0, 0, starpos)) return 0;
if (!match(starpos + ql - plen + 1, starpos + 1, plen - starpos - 1))
return 0;
if (ql >= plen)
for (i = starpos; i <= starpos + ql - plen; ++i)
if (isgood[query[i]]) return 0;
return 1;
}
}
int main(void) {
int i = 0;
int n = 0;
int len = 0;
scanf("%s", goods);
len = strlen(goods);
for (i = 0; i < len; ++i) isgood[goods[i]] = 1;
scanf("%s", pat);
plen = strlen(pat);
for (i = 0; i < plen; ++i)
if ('*' == pat[i]) {
starpos = i;
break;
}
if (i == plen) starpos = -1;
scanf("%d", &n);
while (n--) {
scanf("%s", query);
printf("%s\n", ok() ? "YES" : "NO");
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[2][55], b[55], i, j;
cin >> n;
for (i = 1; i <= n - 1; ++i) cin >> a[0][i];
a[0][0] = 0;
for (i = 0; i < n - 1; ++i) cin >> a[1][i];
a[1][n - 1] = 0;
for (i = 0; i < n; ++i) cin >> b[i];
for (i = 1, j = n - 2; i < n, j >= 0; ++i, --j) {
a[1][j] += a[1][j + 1];
a[0][i] += a[0][i - 1];
}
int min = 10000, x, min1 = 10000;
for (i = 0; i < n; ++i) {
if (a[0][i] + a[1][i] + b[i] < min) {
min = a[0][i] + a[1][i] + b[i];
x = i;
}
}
for (i = 0; i < n; ++i) {
if (i == x) continue;
if (a[0][i] + a[1][i] + b[i] < min1) min1 = a[0][i] + a[1][i] + b[i];
}
cout << min + min1;
return 0;
}
| 5 |
Subsets and Splits