solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int INF = (-1u) / 2;
const long long int INF2 = (-1ull) / 2;
int a, b, i, d[1011000], j, k, n, m, timer = 0, l, r, x, y, s;
int c[1011000], cnt = 0, fl = 0, a2, a3 = -1000000, ans = 0;
pair<int, int> t[400010];
int tree[400010];
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v] = make_pair(c[tl], c[tl]);
tree[v] = INF;
} else {
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = make_pair(max(t[v * 2].first, t[v * 2 + 1].first),
min(t[v * 2].second, t[v * 2 + 1].second));
tree[v] = -1;
}
}
int take_max(int v, int tl, int tr, int l, int r) {
if (l > r)
return -1000000001;
else if (tl == l && tr == r)
return t[v].first;
else {
int tm = (tl + tr) / 2;
return max(take_max(v * 2, tl, tm, l, min(r, tm)),
take_max(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r));
}
}
int take_min(int v, int tl, int tr, int l, int r) {
if (l > r)
return INF;
else if (tl == l && tr == r)
return t[v].second;
else {
int tm = (tl + tr) / 2;
return min(take_min(v * 2, tl, tm, l, min(r, tm)),
take_min(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r));
}
}
void update(int v, int tl, int tr, int l, int r, int key) {
if (l > r || key == -1) return;
if (tl == l && tr == r) {
if (tree[v] == -1 || key < tree[v]) tree[v] = key;
} else {
int tm = (tl + tr) / 2;
update(v * 2, tl, tm, tl, tm, tree[v]);
update(v * 2 + 1, tm + 1, tr, tm + 1, tr, tree[v]);
tree[v] = -1;
update(v * 2, tl, tm, l, min(r, tm), key);
update(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r, key);
}
}
int take(int v, int tl, int tr, int pos) {
if (tl == tr)
return tree[v];
else {
int tm = (tl + tr) / 2;
update(v * 2, tl, tm, tl, tm, tree[v]);
update(v * 2 + 1, tm + 1, tr, tm + 1, tr, tree[v]);
tree[v] = -1;
if (pos <= tm) return take(v * 2, tl, tm, pos);
return take(v * 2 + 1, tm + 1, tr, pos);
}
}
int main() {
scanf("%d%d%d", &n, &s, &k);
for (i = 1; i <= n; i++) {
scanf("%d", &c[i]);
}
build(1, 1, n);
for (i = 1; i <= n; i++) {
a = 0;
if (i != 1) a = take(1, 1, n, i - 1);
if (i == 1 || a != INF) {
l = i + k - 1;
r = n;
while (l < r) {
int m = (l + r) / 2;
if (take_max(1, 1, n, i, m) - take_min(1, 1, n, i, m) > s)
r = m;
else
l = m + 1;
}
m = r;
if (take_max(1, 1, n, i, m) - take_min(1, 1, n, i, m) > s) r--;
if (i + k - 1 <= r)
x = r;
else
continue;
if (take_max(1, 1, n, i, i + k - 1) - take_min(1, 1, n, i, i + k - 1) > s)
continue;
update(1, 1, n, i + k - 1, x, a + 1);
}
}
x = take(1, 1, n, n);
if (x == INF) x = -1;
cout << x << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const long long infll = (long long)1.01e18;
const long double eps = 1e-9;
const long double pi = acos((long double)-1);
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x; }
const int n = (int)1e6, l = 6;
const int maxq = (int)1e5 + 5;
int k;
int f[l];
int q;
int p10[l + 1];
int qs[maxq];
void precalc() {
p10[0] = 1;
for (int i = 1; i <= l; ++i) {
p10[i] = p10[i - 1] * 10;
}
}
bool read() {
if (scanf("%d", &k) < 1) {
return false;
}
for (int i = 0; i < l; ++i) {
scanf("%d", &f[i]);
}
scanf("%d", &q);
for (int qq = 0; qq < q; ++qq) {
scanf("%d", &qs[qq]);
}
return true;
}
long long dp[l + 1][n];
long long getVal(int x, int f) {
if (x > k * 9) {
return -infll;
}
int nines = x / 9;
int rem = x % 9;
long long res = (long long)nines * f * 3ll;
if (!rem) {
return res;
}
int have = k - nines;
if (have == 1) {
if (rem == 6) {
res += f * 2ll;
} else if (rem == 3) {
res += f;
}
return res;
}
if (rem >= 6) {
res += f * 2ll;
} else if (rem >= 3) {
res += f;
}
return res;
}
long long solve(int x) {
dp[l][0] = 0;
for (int d = l - 1; d >= 0; --d) {
int dig = (x / p10[d]) % 10;
int mx = x / p10[d];
int nmx = x / p10[d + 1];
for (int carry = 0; carry <= mx; ++carry) {
if (!d && carry) {
break;
}
auto &cur = dp[d][carry];
cur = -infll;
for (int ncarry = carry / 10; ncarry <= nmx; ++ncarry) {
int need = (ncarry * 10 + dig) - carry;
if (need < 0) {
continue;
}
long long val = getVal(need, f[d]);
if (val >= 0) {
cur = max(cur, dp[d + 1][ncarry] + val);
}
}
}
}
return dp[0][0];
}
void solve() {
for (int qq = 0; qq < q; ++qq) {
printf("%lld\n", solve(qs[qq]));
}
}
int main() {
precalc();
while (read()) {
solve();
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int conversion(string p) {
int o;
o = atoi(p.c_str());
return o;
}
string toString(int h) {
stringstream ss;
ss << h;
return ss.str();
}
string toStringLong(long long h) {
stringstream ss;
ss << h;
return ss.str();
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
int lcm(int a, int b) { return (a * (b / gcd(a, b))); }
int main() {
int n, k;
cin >> n >> k;
string x;
cin >> x;
int i = 0;
while (i < n) {
int var = i;
int suma = i + k;
if (suma >= n) {
cout << "YES" << endl;
return 0;
} else {
i = suma;
while (x[i] == '#') {
i--;
if (i == var) {
cout << "NO" << endl;
return 0;
}
}
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long int m = 0, n;
int arr[300000];
int main() {
cin >> n;
for (unsigned long long int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
for (unsigned long long int i = 0; i < n; i++) m += (i + 2) * arr[i];
m -= arr[n - 1];
cout << m;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
map<string, int> dict;
const int maxn = 100010;
int fa[maxn];
int ran[maxn];
void Init(int n) {
for (int i = 0; i <= n; i++) {
fa[i] = i;
ran[i] = 0;
}
}
int Find(int x) {
if (x == fa[x]) return fa[x];
int y = Find(fa[x]);
ran[x] = (ran[x] + ran[fa[x]]) % 2;
return fa[x] = y;
}
int Union(int typ, int x, int y) {
int x1 = Find(x);
int y1 = Find(y);
fa[x1] = y1;
ran[x1] = (ran[x] + typ + ran[y]) % 2;
return 0;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> m >> q;
Init(n);
for (int i = 0; i < n; i++) {
string str;
cin >> str;
dict[str] = i + 1;
}
for (int i = 0; i < m; i++) {
int r;
string a, b;
cin >> r >> a >> b;
r--;
int numa = dict[a];
int numb = dict[b];
if (Find(numa) == Find(numb)) {
int preR = (ran[numa] + ran[numb]) % 2;
if (r == preR) {
puts("YES");
} else {
puts("NO");
}
} else {
Union(r, numa, numb);
puts("YES");
}
}
for (int i = 0; i < q; i++) {
string a, b;
cin >> a >> b;
int numa = dict[a];
int numb = dict[b];
if (Find(numa) == Find(numb)) {
int preR = (ran[numa] + ran[numb]) % 2;
printf("%d\n", preR + 1);
} else {
printf("3\n");
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
n = (s[n - 2] - 48) * 10 + (s[n - 1] - 48);
if (n % 4)
cout << "0";
else
cout << "4";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
pair<int, int> a[N];
int b[N];
int pr[N];
int mx[N];
int mn[N];
set<int> roots;
int n, m, k;
int find(int x) { return pr[x] == x ? x : pr[x] = find(pr[x]); }
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
pr[y] = x;
mx[x] = max(mx[x], mx[y]);
mn[x] = min(mn[x], mn[y]);
roots.erase(y);
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
cin >> n >> m >> k;
for (int i = (0); i <= ((n)-1); i += (+1)) {
cin >> b[i];
mx[i] = b[i];
mn[i] = b[i];
pr[i] = i;
roots.insert(i);
if (i > 0) {
a[i - 1] = {b[i] - b[i - 1], i};
}
}
sort(a, a + n - 1);
for (int i = (0); i <= ((n)-1); i += (+1)) {
if ((int)(roots).size() > k) {
auto &[length, j] = a[i];
unite(j - 1, j);
} else {
break;
}
}
int length = 0;
for (int x : roots) {
length += mx[x] - mn[x] + 1;
}
cout << length << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t, x, y;
cin >> n;
vector<int> v2;
vector<pair<int, int> > l;
for (int i = 0; i < n; i++) {
cin >> t >> x >> y;
if (t == 1) {
l.push_back({x, y});
} else {
x--;
y--;
queue<int> que;
vector<bool> visited(100, 0);
visited[x] = 1;
que.push(x);
while (que.size()) {
x = que.front();
que.pop();
for (int i = 0; i < l.size(); i++) {
if (l[x].first > l[i].first && l[x].first < l[i].second &&
visited[i] == 0) {
visited[i] = 1;
que.push(i);
} else if (l[x].second > l[i].first && l[x].second < l[i].second &&
visited[i] == 0) {
visited[i] = 1;
que.push(i);
}
}
}
if (visited[y]) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
long long int tab[4][32][1000 + 1];
long long int mrec(int q, int n, int k) {
int i;
long long int *t_nk = &tab[q][n][k];
if (*t_nk == -1) {
if (q == 0) {
*t_nk = mrec(3, n - 1, k - 1);
} else {
int s = 0;
for (i = 0; i <= k; i++) {
s = (s + mrec(0, n, i) * mrec(q - 1, n, k - i)) % 7340033;
}
*t_nk = s;
}
}
return *t_nk;
}
int split_count(int n) {
int c = 0;
while (n & 1 && n != 1) {
c++;
n >>= 1;
}
return c;
}
int main() {
int p, i;
memset(tab, 255, sizeof tab);
for (p = 0; p < 4; p++) {
for (i = 1; i <= 1000; ++i) {
tab[p][0][i] = 0;
}
for (i = 0; i < 32; ++i) {
tab[p][i][0] = 1;
}
}
int tests = 0;
scanf("%d", &tests);
for (; tests; tests--) {
int n = 0, k = 0;
scanf("%d%d", &n, &k);
n = split_count(n);
printf("%d\n", (int)mrec(0, n, k));
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
const int mod = (int)(1e9 + 7);
int main() {
string s;
cin >> s;
int n = s.length();
for (int i = 0; i < n; ++i) {
int x = s[i] - '0';
cout << min(x, 9 - x > 0 || i > 0 ? 9 - x : x);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
std::mt19937 rnd(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 500000;
int n;
int a[MAXN];
struct Options {
int sgn;
long long delta;
set<pair<long long, long long>> ranges;
void flip(int a) {
sgn = -sgn;
delta = a - delta;
}
void trim(int l, int r) {
if (l > r) return;
long long xl = sgn * (l - delta), xr = sgn * (r - delta);
if (sgn == -1) swap(xl, xr);
while (!ranges.empty()) {
pair<long long, long long> cur = *ranges.begin();
if (cur.first >= xl) break;
ranges.erase(ranges.begin());
if (cur.second < xl) continue;
ranges.insert(make_pair(xl, cur.second));
}
while (!ranges.empty()) {
pair<long long, long long> cur = *ranges.rbegin();
if (cur.second <= xr) break;
ranges.erase(prev(ranges.end()));
if (cur.first > xr) continue;
ranges.insert(make_pair(cur.first, xr));
}
}
bool empty() { return ranges.empty(); }
void add(int l, int r) {
long long xl = sgn * (l - delta), xr = sgn * (r - delta);
if (sgn == -1) swap(xl, xr);
ranges.insert(make_pair(xl, xr));
}
void add(int y) {
long long x = sgn * (y - delta);
ranges.insert(make_pair(x, x));
}
void clear() {
ranges.clear();
sgn = +1;
delta = 0;
}
bool contains(int y) {
long long x = sgn * (y - delta);
auto it = ranges.upper_bound(make_pair(x, LLONG_MAX));
if (it == ranges.begin()) return false;
--it;
return x <= it->second;
}
void print() {
for (auto it = ranges.begin(); it != ranges.end(); ++it)
printf(" [%lld..%lld]", sgn * it->first + delta,
sgn * it->second + delta);
}
};
int solve() {
int save = 0;
Options options;
options.clear();
for (int i = (0); i < (n); ++i) {
options.trim(1, a[i] - 1);
if (options.empty()) {
options.add(1, a[i] - 1);
} else {
++save;
}
options.flip(a[i]);
if (a[i] % 2 == 0) {
int half = a[i] / 2;
if (options.contains(half)) {
++save;
options.clear();
}
options.add(half);
}
}
return 2 * n - save;
}
void run() {
scanf("%d", &n);
for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
printf("%d\n", solve());
}
int main() {
int ncase;
scanf("%d", &ncase);
for (int i = (1); i <= (ncase); ++i) run();
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
set<int> mySet;
typedef struct {
int left;
int right;
int ind;
int val;
} command;
int tree[8000000];
int n;
command c;
vector<command> commands;
bool sortByLeft(command b, command a) { return b.left > a.left; }
bool sortByInd(command b, command a) { return b.ind < a.ind; }
void update(int ind, int s, int e, int num) {
if (s == e && s == num) {
tree[ind]++;
return;
}
if (s > num || e < num) {
return;
}
int mid = (s + e) / 2;
update(2 * ind, s, mid, num);
update(2 * ind + 1, mid + 1, e, num);
tree[ind] = tree[2 * ind] + tree[2 * ind + 1];
}
int query(int ind, int s, int e, int num) {
if (s > num) {
return 0;
}
if (e <= num) {
return tree[ind];
}
int mid = (s + e) / 2;
int l = query(2 * ind, s, mid, num);
int r = query(2 * ind + 1, mid + 1, e, num);
return l + r;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d %d", &c.left, &c.right);
c.ind = i;
commands.push_back(c);
mySet.insert(c.left);
mySet.insert(c.right);
}
int i = 0;
for (set<int>::iterator it = mySet.begin(); it != mySet.end(); it++) {
mp[*it] = i;
i++;
}
int lastIndex = mySet.size() - 1;
sort(commands.begin(), commands.end(), sortByLeft);
for (int i = 0; i < n; i++) {
update(1, 0, lastIndex, mp[commands[i].right]);
int tmp = query(1, 0, lastIndex, mp[commands[i].right]) - 1;
if (tmp == -1) tmp = 0;
commands[i].val = tmp;
}
sort(commands.begin(), commands.end(), sortByInd);
for (int i = 0; i < n; i++) {
cout << commands[i].val << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class V, int ME>
class BIT {
public:
V bit[1 << ME];
V operator[](int e) {
if (e < 0) return 0;
V s = 0;
e++;
while (e) s += bit[e - 1], e -= e & -e;
return s;
}
void add(int e, V v) {
e++;
while (e <= 1 << ME) bit[e - 1] += v, e += e & -e;
}
};
BIT<signed long long, 20> bt;
template <class V, int NV>
class SegTree_1 {
public:
vector<V> val;
static V const def = 1LL << 60;
V comp(V l, V r) { return min(l, r); };
SegTree_1() { val = vector<V>(NV * 2, 1LL << 60); };
V getval(int x, int y, int l = 0, int r = NV, int k = 1) {
if (r <= x || y <= l) return def;
if (x <= l && r <= y) return val[k];
return comp(getval(x, y, l, (l + r) / 2, k * 2),
getval(x, y, (l + r) / 2, r, k * 2 + 1));
}
void update(int entry, V v) {
entry += NV;
val[entry] = v;
while (entry > 1)
entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
}
};
int N, Q;
signed long long AS;
set<int> S;
SegTree_1<signed long long, 1 << 20> penalty;
void update(int cur) {
if (cur <= 1 || cur >= N) return;
S.erase(cur);
penalty.update(cur, 1LL << 60);
if (bt[cur] < bt[cur - 1] && bt[cur] < bt[cur + 1]) {
S.insert(cur);
} else {
penalty.update(cur,
max({0LL, bt[cur - 1] - bt[cur], bt[cur + 1] - bt[cur]}));
}
}
signed long long hoge(int cur, int add) {
signed long long ret = 0;
ret -= abs(bt[cur] - bt[cur - 1]);
ret += abs(bt[cur] + add - bt[cur - 1]);
ret -= abs(bt[cur] - bt[cur + 1]);
ret += abs(bt[cur] + add - bt[cur + 1]);
return ret;
}
void solve() {
int i, j, k, l, r, x, y;
string s;
scanf("%d", &N);
int pre = 0;
for (i = 1; i <= N; i++) {
scanf("%d", &x);
bt.add(i, x - pre);
if (i > 1) AS += abs(x - pre);
pre = x;
}
for (i = 2; i <= N - 1; i++) update(i);
S.insert(N + 1);
S.insert(N + 2);
scanf("%d", &Q);
while (Q--) {
int T, L, R, X;
scanf("%d%d%d%d", &T, &L, &R, &X);
if (T == 1) {
signed long long ret = AS + max(hoge(L, X), hoge(R, X));
auto it = S.lower_bound(L);
if (*it <= R) ret = max(ret, AS + hoge(*it, X));
signed long long p = penalty.getval(L, R + 1);
if (p < X) ret = max(ret, AS + X + X - 2 * p);
cout << ret << endl;
} else {
AS -= abs(bt[L] - bt[L - 1]);
AS -= abs(bt[R] - bt[R + 1]);
bt.add(L, X);
bt.add(R + 1, -X);
AS += abs(bt[L] - bt[L - 1]);
AS += abs(bt[R] - bt[R + 1]);
update(L);
update(L - 1);
update(R);
update(R + 1);
}
}
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int n, m, d[100000];
memset(d, 0, sizeof(d));
cin >> n >> m;
for (int i = 0; i < m; i++) {
bool rgb[3];
memset(rgb, 0, sizeof(rgb));
int t[3];
for (int i = 0; i < 3; i++) {
cin >> t[i];
t[i]--;
if (d[t[i]]) rgb[d[t[i]] - 1] = true;
}
for (int i = 0; i < 3; i++) {
if (!d[t[i]]) {
for (int j = 0; j < 3; j++)
if (!rgb[j]) {
d[t[i]] = j + 1;
rgb[j] = 1;
break;
}
}
}
}
for (int i = 0; i < n; i++) cout << d[i] << ' ';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int LOGN = 20;
const int INF = int(1e9);
int a[100005];
int main() {
int n, b, d;
cin >> n >> b >> d;
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n)))
cin >> a[i];
int cnt = 0;
long long int sum = 0;
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n))) {
if (a[i] <= b) {
sum += a[i];
}
if (sum > d) {
cnt++;
sum = 0;
}
}
if (sum > d) {
cnt++;
sum = 0;
}
cout << cnt << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
long long N;
cin >> N;
long long pre[2 * 123456];
memset(pre, 0, sizeof pre);
while (N--) {
long long X;
cin >> X;
X--;
pre[X]++;
}
long long prev = 0;
for (long long i = 1; i < str.size(); i++) pre[i] += pre[i - 1];
for (long long i = 0; i < str.size() / 2; i++) {
if (pre[i] % 2 == 1) swap(str[i], str[str.size() - 1 - i]);
}
cout << str;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[100010];
long long v[100010], l[100010], p;
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", a + i);
sort(a, a + n);
l[0] = 1;
p = 0;
for (int i = 1; i < n; i++) {
long long x = l[i - 1] * (a[i] - a[i - 1]) + v[i - 1];
for (; x > k;) {
x -= a[i] - a[p];
p++;
}
l[i] = i - p + 1;
v[i] = x;
}
p = 0;
for (int i = 1; i < n; i++)
if (l[p] < l[i]) p = i;
cout << l[p] << ' ' << a[p] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<int> v;
cin >> n;
int n2 = n;
while (n--) {
int aux;
cin >> aux;
v.push_back(aux);
}
sort(v.begin(), v.end());
int res = 0;
int dia = 1;
for (int i = 0; i < n2; i++) {
if (v[i] >= dia) {
dia++;
res++;
}
}
cout << res << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[100000];
long long d[100001];
vector<long long> v[100001];
int n, m;
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) scanf("%d", a + i);
for (int i = 0; i < m; i++) {
if (i && a[i] != a[i - 1]) {
d[a[i]] += abs(a[i] - a[i - 1]);
v[a[i]].push_back(a[i - 1]);
}
if (i < m - 1 && a[i] != a[i + 1]) {
v[a[i]].push_back(a[i + 1]);
d[a[i]] += abs(a[i] - a[i + 1]);
}
}
long long c = 0;
for (int i = 1; i <= n; i++)
if (v[i].size()) {
sort(v[i].begin(), v[i].end());
long long temp = v[i][v[i].size() / 2];
long long ts = 0;
for (int u = 0; u < v[i].size(); u++) ts += abs(temp - v[i][u]);
if (d[i] - ts > c) c = d[i] - ts;
}
long long ans = 0;
for (int i = 1; i <= n; i++) ans += d[i];
ans = ans / 2;
ans = ans - c;
printf("%I64d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5;
void solve() {
long long int n;
cin >> n;
vector<long long int> v(n);
for (auto &it : v) cin >> it;
;
for (long long int i = 0; i < n; i++) {
while (v[i] % 2 == 0) {
v[i] /= 2;
}
while (v[i] % 3 == 0) {
v[i] /= 3;
}
}
sort(v.begin(), v.end());
if (v[0] == v.back()) {
cout << "Yes"
<< "\n";
return;
}
cout << "No"
<< "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int t = 1;
while (t--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long k, n, s, p;
cin >> k >> n >> s >> p;
long long N = (n + s - 1) / s;
N = N * k;
long long A = (N + p - 1) / p;
cout << A << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
static char s[1000005];
static int n;
int main(void) {
gets(s);
n = strlen(s);
int ret = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0') continue;
ret++;
if (i == 0) continue;
if (s[i - 1] == '0') continue;
while (--i >= 0 && s[i] == '1')
;
if (i >= 0)
s[i++] = 1;
else
ret++;
}
printf("%d", ret);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int x, i, j, n;
cin >> x;
vector<long long int> vec;
long long int count = 0;
while (true) {
long long int y;
for (i = 30; i >= 0; i--) {
y = (1 << i);
if (y <= x && (y & x) == 0) {
count++;
break;
}
}
if (i == -1) break;
y = y * 2;
long long int z = log2(y);
vec.push_back(z);
y--;
x = x ^ y;
for (i = 30; i >= 0; i--) {
y = (1 << i);
if (y <= x && (y & x) == 0) {
count++;
break;
}
}
if (i == -1) break;
x++;
}
cout << count << endl;
for (i = 0; i < vec.size(); i++) cout << vec[i] << " ";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, mod = 1e9 + 7;
const long long inf = 1e18;
int N, n;
int ans[4 * maxn], ansl[4 * maxn], ansr[4 * maxn], pl[4 * maxn], pr[4 * maxn],
sm[4 * maxn], tot[4 * maxn];
int arr[maxn];
void Merge(int id) {
pl[id] = max(pl[2 * id], -sm[2 * id] + pl[2 * id + 1]);
pr[id] = max(pr[2 * id + 1], sm[2 * id + 1] + pr[2 * id]);
tot[id] = max(sm[2 * id] + tot[2 * id + 1], tot[2 * id] - sm[2 * id + 1]);
ansl[id] = max(ansl[2 * id], max(tot[2 * id] + pl[2 * id + 1],
sm[2 * id] + ansl[2 * id + 1]));
ansr[id] = max(ansr[2 * id + 1], max(tot[2 * id + 1] + pr[2 * id],
-sm[2 * id + 1] + ansr[2 * id]));
sm[id] = sm[2 * id] + sm[2 * id + 1];
ans[id] = max(ans[2 * id], ans[2 * id + 1]);
ans[id] = max(ans[id], pr[2 * id] + ansl[2 * id + 1]);
ans[id] = max(ans[id], ansr[2 * id] + pl[2 * id + 1]);
}
void change(int pos, int x, int l = 0, int r = N, int id = 1) {
if (r - l == 1) {
ans[id] = ansl[id] = ansr[id] = tot[id] = abs(x);
pl[id] = -x, pr[id] = x;
sm[id] = x;
return;
}
int mid = (l + r) >> 1;
if (pos < mid)
change(pos, x, l, mid, 2 * id);
else
change(pos, x, mid, r, 2 * id + 1);
Merge(id);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
N = 2 * (n - 1);
string s;
cin >> s;
for (int i = 0; i < N; i++) {
arr[i] = (s[i] == '(' ? -1 : 1);
change(i, arr[i]);
}
cout << ans[1] << "\n";
while (q--) {
int a, b;
cin >> a >> b;
--a, --b;
swap(arr[a], arr[b]);
change(a, arr[a]);
change(b, arr[b]);
cout << ans[1] << "\n";
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
int b[maxn];
int cnt;
int main() {
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
string a;
cin >> a;
cnt = 0;
bool flag = true;
for (int i = 0; i < a.size(); i++) {
int t = a[i] - 'a';
b[cnt++] = t;
}
sort(b, b + cnt);
for (int i = 1; i < cnt; i++) {
if (b[i] - b[i - 1] != 1) {
flag = false;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
struct point {
long long x, y;
point() {}
point(int x, int y) {
this->x = x;
this->y = y;
}
};
point p[MAXN];
long long calcSurface(point A, point B, point C) {
return (A.x * B.y + A.y * C.x + B.x * C.y) -
(A.y * B.x + A.x * C.y + B.y * C.x);
}
int sign(long long x) {
if (x < 0) return -1;
return 1;
}
void testing() {
int n;
cin >> n;
vector<point> v;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back(point(x, y));
}
for (int i = 2; i < v.size(); i++) {
cout << calcSurface(v[0], v[1], v[i]) << '\n';
}
}
long long askQuery(int t, int i, int j, int k) {
cout << t << " " << i << " " << j << " " << k << '\n';
cout.flush();
long long x;
cin >> x;
return x;
}
void answerQuery(vector<int> p) {
cout << "0";
for (int item : p) cout << " " << item;
cout << '\n';
cout.flush();
}
long long val[MAXN];
int main() {
int n;
cin >> n;
int second = 2;
for (int i = 3; i <= n; i++) {
if (askQuery(2, 1, second, i) == -1) {
second = i;
}
}
for (int i = 2; i <= n; i++) {
if (i == second) continue;
val[i] = askQuery(1, 1, second, i);
}
int maxInd = 0;
for (int i = 2; i <= n; i++) {
if (i == second) continue;
if (val[i] > val[maxInd]) {
maxInd = i;
}
}
for (int i = 2; i <= n; i++) {
if (i == second || i == maxInd) continue;
if (val[i] == val[maxInd] && askQuery(2, 1, maxInd, i) == -1) {
maxInd = i;
}
}
vector<pair<long long, int>> v1, v2;
for (int i = 2; i <= n; i++) {
if (i == maxInd) {
v1.push_back({val[i], i});
continue;
}
if (askQuery(2, 1, i, maxInd) == 1) {
v1.push_back({val[i], i});
} else {
v2.push_back({val[i], i});
}
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end(), greater<pair<long long, int>>());
vector<int> p = {1};
for (pair<long long, int> x : v1) p.push_back(x.second);
for (pair<long long, int> x : v2) p.push_back(x.second);
answerQuery(p);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
string s;
long long int a;
long long int n, sum, p[4007], f[40007];
long long int res = 0;
long long int gs(long long int l, long long int r) {
if (!l) return p[r];
return p[r] - p[l - 1];
}
int main() {
std::cin >> a >> s;
n = s.size();
memset(p, 0, sizeof(p));
memset(f, 0, sizeof(f));
p[0] = (s[0] - '0');
for (int i = 1; i < n; i++) p[i] = p[i - 1] + (s[i] - '0');
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) f[gs(i, j)]++;
for (int i = 1; i < 40007; i++)
if (!(a % i) && (a / i < 40006)) res += f[i] * f[a / i];
if (!a)
std::cout << f[0] * ((long long int)n * n + n - f[0]) << '\n';
else
cout << res;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int arr[maxn];
void solve() {
int n, k;
scanf("%d", &n);
scanf("%d", &k);
string str;
cin >> str;
int start = 0, end = n - 1;
for (int i = 1; i <= n - 1; i++) {
int suff = i;
int pre = 0;
while (str[pre] == str[suff]) {
pre++;
suff++;
}
if (suff == n) {
start = n - i;
break;
}
}
k--;
cout << str;
while (k--) {
for (int i = start; i <= end; i++) {
cout << str[i];
}
}
cout << endl;
}
int main() {
int t = 1;
while (t--) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n) {
if (n == 1) return false;
if (n == 2) return true;
for (int x = 2; x * x <= n; ++x) {
if ((n % x) == 0) return false;
}
return true;
}
struct edge {
int b;
int cap, f;
int rev_id;
};
vector<edge> E[208];
int vis[208], depth[208];
bool bfs_aug(int s, int t, const int& iter) {
queue<int> Q;
Q.push(s);
vis[s] = iter;
depth[s] = 0;
int i, j, numE;
while (!Q.empty()) {
i = Q.front();
Q.pop();
if (i == t) return true;
numE = E[i].size();
for (int x = 0; x < (numE); ++x)
if (j = E[i][x].b, vis[j] < iter && E[i][x].cap > E[i][x].f)
vis[j] = iter, depth[j] = depth[i] + 1, Q.push(j);
}
return false;
}
int dfs_aug(int i, int t, int res) {
if (i == t) return res;
int numE = E[i].size();
int sum = 0, v = 0;
for (int x = 0; x < (numE); ++x) {
int j = E[i][x].b, r = E[i][x].rev_id;
int c = min(E[i][x].cap - E[i][x].f, res);
if (c > 0 && depth[j] == depth[i] + 1 && (v = dfs_aug(j, t, c)) > 0)
E[i][x].f += v, E[j][r].f -= v, res -= v, sum += v;
}
assert(res >= 0);
return sum;
}
int dinics(int s, int t) {
int ans = 0;
memset(vis, 0, sizeof(vis));
for (int phase = 1; bfs_aug(s, t, phase) > 0; ++phase)
ans += dfs_aug(s, t, 1000000000);
return ans;
}
void clear_flow() {
for (int i = 0; i < (208); ++i)
for (int x = 0; x < ((int)E[i].size()); ++x) E[i][x].f = 0;
}
void add_edge(int a, int b, int c) {
edge x = {b, c, 0, (int)E[b].size()};
edge y = {a, 0, 0, (int)E[a].size()};
E[a].push_back(x);
E[b].push_back(y);
}
int A[208];
vector<int> D[208];
bool z[208];
void solve(int i, vector<int>& ret) {
while (!z[i]) {
z[i] = true;
ret.push_back(i);
assert(D[i].size() == 2);
if (z[D[i][0]] && z[D[i][1]]) return;
if (z[D[i][0]])
i = D[i][1];
else
i = D[i][0];
}
}
int main() {
int N;
scanf("%d", &N);
for (int i = 0; i < (N); ++i) scanf("%d", &A[i]);
for (int i = 0; i < (N); ++i) {
if (A[i] % 2)
add_edge((208 - 1), i, 2);
else
add_edge(i, (208 - 2), 2);
}
for (int i = 0; i < (N); ++i) {
if ((A[i] % 2) == 0) continue;
for (int j = 0; j < (N); ++j) {
if (is_prime(A[i] + A[j])) {
add_edge(i, j, 1);
}
}
}
int ans = dinics((208 - 1), (208 - 2));
if (ans < N)
printf("Impossible\n");
else {
assert(ans == N);
for (int i = 0; i < (N); ++i) {
if ((A[i] % 2) == 0) continue;
int numE = E[i].size();
for (int x = 0; x < (numE); ++x) {
if (E[i][x].f > 0 && E[i][x].b != (208 - 1) && E[i][x].b != (208 - 2)) {
int j = E[i][x].b;
D[i].push_back(j);
D[j].push_back(i);
}
}
}
vector<vector<int> > ans;
vector<int> tmp;
for (int i = 0; i < (N); ++i) {
if (z[i]) continue;
tmp.clear();
solve(i, tmp);
ans.push_back(tmp);
}
int Z = ans.size();
printf("%d\n", Z);
for (int i = 0; i < (Z); ++i) {
int X = ans[i].size();
printf("%d", X);
for (int j = 0; j < (X); ++j) printf(" %d", ans[i][j] + 1);
printf("\n");
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll ar[1000009], n, inp[(ll)(5e3 + 4)], cc = 0, cnt[(ll)(5e3 + 4)], nn;
vector<pair<ll, ll>> pr[(ll)(5e3 + 4)];
vector<ll> pri;
vector<vector<pair<ll, ll>>> vvp;
bool ch[(ll)(5e3 + 4)];
void sieve() {
for (ll i = 2; i < (ll)(5e3 + 4); i++) {
if (!ch[i]) {
pri.push_back(i);
inp[i] = cc++;
for (ll j = i; j < (ll)(5e3 + 4); j += i) {
ll xx = j, cc = 0;
while (xx % i == 0) {
cc++;
xx /= i;
}
pr[j].push_back(make_pair(i, cc));
ch[j] = 1;
}
}
}
}
ll ans(ll i, ll j) {
if (i > j) {
return 0;
}
if (vvp[ar[j] - 2].size() == 0) {
return 0;
}
vector<pair<ll, ll>> vp;
vector<ll> count;
ll ss = vvp[ar[j] - 2].size(), pe = j, co = cnt[ar[j]], ma = 0,
mco = cnt[ar[j]];
for (ll x = j - 1; x >= i - 1; x--) {
if (x == i - 1 || vvp[ar[x] - 2].size() != ss) {
if (co > mco) {
ma = vp.size();
mco = co;
}
vp.push_back(make_pair(x + 1, pe));
pe = x;
if (x >= i) {
co = cnt[ar[x]];
ss = vvp[ar[x] - 2].size();
}
} else {
co += cnt[ar[x]];
}
}
if (mco * 2 <= n) {
return 0;
}
ll d = vvp[ar[vp[ma].first] - 2].back().second;
for (ll x = vp[ma].first; x <= vp[ma].second; x++) {
if ((vvp[ar[x] - 2].back().second -= d) == 0) {
vvp[ar[x] - 2].pop_back();
}
}
return (d * (2 * mco - n) + ans(vp[ma].first, vp[ma].second));
}
int main() {
cin >> n;
ll ma = 0;
for (ll i = 0; i < n; i++) {
cin >> ar[i];
cnt[ar[i]]++;
ma = max(ar[i], ma);
}
sort(ar, ar + n);
nn = unique(ar, ar + n) - ar;
sieve();
vector<pair<ll, ll>> vp;
ll ii = 0;
for (ll i = 2; i <= ma; i++) {
if (ii + 1 < pri.size() && pri[ii + 1] <= i) {
ii++;
}
for (auto x : pr[i]) {
if (inp[x.first] == vp.size()) {
vp.push_back(x);
} else {
vp[inp[x.first]].second += x.second;
}
}
vvp.push_back(vp);
}
ll an = 0, st = 0;
for (ll i = 0; i < nn; i++) {
if (ar[i] <= 1) {
st++;
continue;
}
for (auto x : vvp[ar[i] - 2]) {
an += cnt[ar[i]] * x.second;
}
}
cout << an - ans(st, nn - 1) << "\n";
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using pii = pair<int, int>;
using vi = vector<int>;
mt19937 mrand(time(0));
ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; }
ll get(ll l, ll r) { return get(r - l + 1) + l; }
const int MX = 4000000 * 4 + 100;
int n, r, tot, mx[MX], tag[MX], ans;
struct pack {
int x, l, r, v;
} q[600100];
void pushdown(int k) {
if (tag[k]) {
mx[(k << 1)] += tag[k], tag[(k << 1)] += tag[k];
mx[(k << 1 | 1)] += tag[k], tag[(k << 1 | 1)] += tag[k];
tag[k] = 0;
}
}
void modify(int k, int l, int r, int L, int R, int v) {
if (L <= l && r <= R) return mx[k] += v, tag[k] += v, void();
int mid = l + r >> 1;
pushdown(k);
if (L <= mid) modify((k << 1), l, mid, L, R, v);
if (R > mid) modify((k << 1 | 1), mid + 1, r, L, R, v);
mx[k] = max(mx[(k << 1)], mx[(k << 1 | 1)]);
}
signed main() {
scanf("%d %d", &n, &r);
for (int i = 1; i <= n; i++) {
pii p;
scanf("%d %d", &p.first, &p.second);
p = make_pair(p.first - p.second, p.first + p.second);
q[++tot] = (pack){p.first - r, p.second - r, p.second + r, 1};
q[++tot] = (pack){p.first + r + 1, p.second - r, p.second + r, -1};
}
sort(q + 1, q + 1 + tot,
[&](const pack& a, const pack& b) { return a.x < b.x; });
for (int i = 1, j = 1; i <= tot; i = j) {
while (q[j].x == q[i].x) modify(1, -2e6, 2e6, q[j].l, q[j].r, q[j].v), j++;
ans = max(ans, mx[1]);
}
printf("%d\n", ans);
fprintf(stderr, "time=%.4f\n", (db)clock() / CLOCKS_PER_SEC);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long n, k, m = 0, answ = 0;
cin >> n >> k;
vector<long long> a(n, 0);
cin >> a[0];
m = a[0];
for (long i = 1; i < n; i++) {
cin >> a[i];
if (a[i] < m) m = a[i];
}
for (long i = 0; i < n; i++) {
if ((a[i] - m) % k == 0) {
answ += (a[i] - m) / k;
} else {
answ = -1;
cout << answ;
return 0;
}
}
cout << answ;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long p = 1000000007;
long long get(long long x) {
if (x == 0) return 0;
long long sum = 0;
long long l = 1, r = 1, p1 = 1, p2 = 1;
for (long long i = 1, cnt = 1; l <= (long long)1000000000000000000;
i <<= 1, cnt ^= 1) {
r = l + i;
if (cnt % 2) {
if (x >= l && x < r) {
sum = (sum + (x - l + 1) % p *
(p1 % p * 2 % p + (x - l + 1) % p - 2 + p) % p) %
p;
break;
} else {
sum = (sum + i % p * (p1 % p * 2 % p + i % p - 2 + p) % p) % p;
}
p1 += i;
} else {
if (x >= l && x < r) {
sum = (sum + (x - l + 1) % p *
(p2 % p * 2 % p + (x - l + 1) % p - 1 + p) % p) %
p;
break;
} else {
sum = (sum + i % p * (p2 % p * 2 % p + i % p - 1 + p) % p) % p;
}
p2 += i;
}
l = r;
}
return sum;
}
vector<int> v;
void init() {
long long l = 1, r = 1, p1 = 1, p2 = 1;
for (long long i = 1, cnt = 1; i <= 1e6; i <<= 1, cnt++) {
if (cnt % 2) {
for (int j = p1; j < p1 + i; j++) {
v.push_back(2 * j - 1);
}
p1 += i;
} else {
for (int j = p2; j < p2 + i; j++) {
v.push_back(2 * j);
}
p2 += i;
}
}
}
long long check(long long x) {
long long sum = 0;
for (int i = 0; i < x; i++) {
sum += v[i];
sum %= p;
}
return sum;
}
int main() {
long long l, r;
cin >> l >> r;
cout << (get(r) - get(l - 1) + p) % p << endl;
}
| 5 |
#include <bits/stdc++.h>
const int MAX_N = 1e5 + 7;
int n, sub[MAX_N], freq[MAX_N];
std::vector<int> gr[MAX_N];
int dfs(const int& cur) {
sub[cur] = gr[cur].empty();
for (const auto& nei : gr[cur]) sub[cur] += dfs(nei);
return sub[cur];
}
int main(void) {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::cin >> n;
for (int i = 1, p; i < n; ++i) {
std::cin >> p;
gr[p - 1].push_back(i);
}
dfs(0);
std::sort(sub, sub + n);
for (int i = 0; i < n; ++i) std::cout << sub[i] << " ";
std::cout << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int sum[200001] = {0};
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) sum[arr[i] + arr[j]]++;
int maax = INT_MIN;
for (int i = 0; i < 200001; i++) maax = max(maax, sum[i]);
cout << maax;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void merge(vector<long long int>& v, long long int l, long long int m,
long long int h, long long int& cnt) {
vector<long long int> a, b;
for (int i = l; i <= m; i++) {
a.push_back(v[i]);
}
for (int i = m + 1; i <= h; i++) {
b.push_back(v[i]);
}
long long int i = l, j = 0, k = 0;
while (i <= h) {
if (k == b.size() || (j < a.size() && a[j] <= b[k])) {
v[i++] = a[j++];
} else {
v[i++] = b[k++];
cnt += (long long int)a.size() - j;
}
}
}
void mergeSort(vector<long long int>& v, long long int l, long long int h,
long long int& cnt) {
if (l >= h) return;
int m = (l + h) / 2;
mergeSort(v, l, m, cnt);
mergeSort(v, m + 1, h, cnt);
merge(v, l, m, h, cnt);
}
int main() {
long long int n;
string s;
cin >> n >> s;
map<char, priority_queue<long long int, vector<long long int>,
greater<long long int> > >
m;
for (int i = 0; i < n; i++) {
m[s[i]].push(i);
}
string x = s;
reverse(x.begin(), x.end());
vector<long long int> v(n);
for (int i = 0; i < n; i++) {
v[i] = m[x[i]].top();
m[x[i]].pop();
}
long long int cnt = 0;
mergeSort(v, 0, n - 1, cnt);
cout << cnt;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x, y, z;
cin >> n >> m;
int arr[n + 1];
for (int i = 0; i < n + 1; i++) arr[i] = 0;
while (m--) {
cin >> x >> y >> z;
arr[x] += z;
arr[y] -= z;
}
long long ans = 0;
for (int i = 0; i < n + 1; i++)
if (arr[i] > 0) ans += arr[i];
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int a[1000], q, mas, h, p, s, i, j, k, n, fail,
rez = 0, x1[102], y1[102], y2[102], x2[102], m[102];
long double x, y;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
k = abs(x1[i] - x2[i]);
m[i] = k * k * k;
}
for (i = 1; i <= n; i++) {
fail = 0;
for (j = 2; j <= i; j++) {
x = 0;
y = 0;
p = 0;
s = 0;
for (h = j; h <= i; h++) {
x = x + (x1[h] + x2[h]) * m[h] / 2;
y = y + (y1[h] + y2[h]) * m[h] / 2;
}
mas = 0;
for (q = j; q <= i; q++) mas = mas + m[q];
x = x / (mas);
y = y / (mas);
if ((x1[j - 1] - x) * (x2[j - 1] - x) > 0) {
cout << rez;
return 0;
}
if ((y1[j - 1] - y) * (y2[j - 1] - y) > 0) {
cout << rez;
return 0;
}
}
rez = i;
}
cout << rez << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
double a, b, c;
double ans;
double dist(double x1, double y1, double x2, double y2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
pair<double, double> computeX(double x) { return {x, (a * x + c) / -b}; }
pair<double, double> computeY(double y) { return {(b * y + c) / -a, y}; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> a >> b >> c;
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ans = abs(x1 - x2) + abs(y1 - y2);
vector<pair<double, double>> v1, v2;
v1.push_back(computeX(x1));
v1.push_back(computeY(y1));
v2.push_back(computeX(x2));
v2.push_back(computeY(y2));
for (auto i : v1) {
for (auto j : v2) {
ans = min(ans, dist(x1, y1, i.first, i.second) +
dist(i.first, i.second, j.first, j.second) +
dist(j.first, j.second, x2, y2));
}
}
cout << fixed << setprecision(20) << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, points = 0, change = 0;
cin >> N;
int computer[N], col[N];
for (int i = 0; i < N; i++) {
cin >> computer[i];
if (computer[i] <= points) {
points++;
col[i] = 1;
} else {
col[i] = 0;
}
}
while (points < N) {
if (change % 2 == 0) {
for (int i = N - 1; i >= 0; i--) {
if (col[i] == 0 && computer[i] <= points) {
points++;
col[i] = 1;
}
}
} else {
for (int i = 0; i < N; i++) {
if (col[i] == 0 && computer[i] <= points) {
points++;
col[i] = 1;
}
}
}
change++;
}
cout << change << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
return (a.second < b.second);
}
int parent(int i, vector<long long> &a) {
if (a[i] == i) return i;
return parent(a[i], a);
}
void make_par(vector<long long> &a, int x, int y) {
int para = parent(x, a);
int parb = parent(y, a);
a[parb] = para;
}
long long jjjj(long long j) {
long long c = 0;
while (j != 0) {
j /= 2;
++c;
}
return c;
}
long long maxi(long long a, long long b) { return (a > b) ? a : b; }
int rminutil(vector<long long> &a, int si, int sj, int i, int j, int k) {
if (si >= i && sj <= j) {
return a[k];
}
if (sj < i || si > j) {
return INT_MAX;
}
int mid = si + (sj - si) / 2;
return min(rminutil(a, si, mid, i, j, 2 * k),
rminutil(a, mid + 1, sj, i, j, 2 * k + 1));
}
int rmin(vector<long long> &a, int n, int i, int j) {
if (i < 0 || j > n - 1 || i > j) {
cout << "invalid";
return -1;
}
return rminutil(a, 0, n - 1, i, j, 1);
}
void RMQ(vector<long long> &a, int b[], int i, int j, int n, int k) {
if (i == j) {
a[k] = b[i];
}
int mid = i + (j - i) / 2;
if (a[k] == -1) {
if (a[2 * k] == -1) {
RMQ(a, b, i, mid, n, 2 * k);
}
if (a[2 * k + 1] == -1) {
RMQ(a, b, mid + 1, j, n, 2 * k + 1);
}
a[k] = min(a[2 * k], a[2 * k + 1]);
}
}
void minupdate(vector<long long> &a, int si, int sj, int i, int old, int ne,
int k) {
if (i < si || i > sj) {
return;
}
if (si == sj) {
a[k] = ne;
}
if (si != sj) {
int mid = si + (sj - si) / 2;
if (i <= mid) {
minupdate(a, si, mid, i, old, ne, 2 * k);
} else {
minupdate(a, mid + 1, sj, i, old, ne, 2 * k + 1);
}
a[k] = min(a[2 * k], a[2 * k + 1]);
}
}
void sumupdate(vector<long long> &a, int si, int sj, int i, int old, int ne,
int k) {
if (i < si || i > sj) {
return;
}
a[k] = a[k] + ne - old;
if (si != sj) {
int mid = si + (sj - si) / 2;
sumupdate(a, si, mid, i, old, ne, 2 * k);
sumupdate(a, mid + 1, sj, i, old, ne, 2 * k + 1);
}
}
long long rsumutil(vector<long long> &a, int si, int sj, int i, int j, int k) {
if (si >= i && sj <= j) {
return a[k];
}
if (sj < i || si > j) {
return 0;
}
int mid = si + (sj - si) / 2;
return rsumutil(a, si, mid, i, min(j, mid), 2 * k) +
rsumutil(a, mid + 1, sj, max(i, mid + 1), j, 2 * k + 1);
}
long long rsum(vector<long long> &a, int n, int i, int j) {
if (i < 0 || j >= n || i > j) {
cout << "invalid";
return -1;
}
return rsumutil(a, 0, n - 1, i, j, 1);
}
void RSQ(vector<long long> &a, int b[], int i, int j, int n, int k) {
if (i == j) {
a[k] = b[i];
}
int mid = i + (j - i) / 2;
if (a[k] == -1) {
if (a[2 * k] == -1) {
RSQ(a, b, i, mid, n, 2 * k);
}
if (a[2 * k + 1] == -1) {
RSQ(a, b, mid + 1, j, n, 2 * k + 1);
}
a[k] = a[2 * k] + a[2 * k + 1];
}
}
long long binaryexp(long long a, long long b, long long i) {
if (b == 0) return 1LL;
if (b == 1) return a;
long long k = binaryexp(a, b / 2, i);
if (b & 1) {
return (((k * k) % i) * a) % i;
} else
return (k * k) % i;
}
int func(int a) {
int pp = a;
int c = 0;
while (a > 0) {
if (pp == binaryexp(4, c, 1000000007)) {
return c;
}
++c;
a /= 4;
}
return c;
}
int main() {
int t;
t = 1;
while (t--) {
int n;
cin >> n;
pair<int, int> *a = new pair<int, int>[n];
for (long long i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a, a + n);
int s = 0;
for (long long i = 0; i < n; i++) {
int kk = func(a[i].second);
if (kk == 0) {
kk = 1;
}
s = max(s, kk + a[i].first);
}
cout << s;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 50 + 5;
const int C = 2500 + 5;
const int MOD = 1000000007;
inline int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
inline int mul(int64_t a, int64_t b) { return static_cast<int>(a * b % MOD); }
int d[N], p[N], tot[3];
int dp0[N][C];
int dp12[N][N][C];
int way[N][N][N][4];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, t;
cin >> n >> t;
for (int i = 0; i < n; ++i) cin >> d[i] >> p[i];
dp0[0][0] = 1;
dp12[0][0][0] = 1;
for (int i = 0; i < n; ++i) {
tot[--p[i]]++;
switch (p[i]) {
case 0:
for (int j = tot[0]; j >= 1; --j)
for (int s = d[i]; s < C; ++s)
dp0[j][s] = add(dp0[j][s], dp0[j - 1][s - d[i]]);
break;
case 1:
for (int j = tot[1]; j >= 1; --j)
for (int k = tot[2]; k >= 0; --k)
for (int s = d[i]; s < C; ++s)
dp12[j][k][s] = add(dp12[j][k][s], dp12[j - 1][k][s - d[i]]);
break;
case 2:
for (int j = tot[1]; j >= 0; --j)
for (int k = tot[2]; k >= 1; --k)
for (int s = d[i]; s < C; ++s)
dp12[j][k][s] = add(dp12[j][k][s], dp12[j][k - 1][s - d[i]]);
break;
}
}
way[0][0][0][3] = 1;
for (int i = 0; i <= tot[0]; ++i) {
for (int j = 0; j <= tot[1]; ++j) {
for (int k = 0; k <= tot[2]; ++k) {
if (i) {
way[i][j][k][0] = add(way[i][j][k][0], mul(i, way[i - 1][j][k][1]));
way[i][j][k][0] = add(way[i][j][k][0], mul(i, way[i - 1][j][k][2]));
way[i][j][k][0] = add(way[i][j][k][0], mul(i, way[i - 1][j][k][3]));
}
if (j) {
way[i][j][k][1] = add(way[i][j][k][1], mul(j, way[i][j - 1][k][0]));
way[i][j][k][1] = add(way[i][j][k][1], mul(j, way[i][j - 1][k][2]));
way[i][j][k][1] = add(way[i][j][k][1], mul(j, way[i][j - 1][k][3]));
}
if (k) {
way[i][j][k][2] = add(way[i][j][k][2], mul(k, way[i][j][k - 1][0]));
way[i][j][k][2] = add(way[i][j][k][2], mul(k, way[i][j][k - 1][1]));
way[i][j][k][2] = add(way[i][j][k][2], mul(k, way[i][j][k - 1][3]));
}
}
}
}
for (int i = 0; i <= tot[0]; ++i)
for (int j = 0; j <= tot[1]; ++j)
for (int k = 0; k <= tot[2]; ++k)
for (int s = 0; s < 3; ++s)
way[i][j][k][3] = add(way[i][j][k][3], way[i][j][k][s]);
int ans = 0;
for (int i = 0; i <= tot[0]; ++i)
for (int j = 0; j <= tot[1]; ++j)
for (int k = 0; k <= tot[2]; ++k)
for (int s = 0; s <= t; ++s)
ans =
add(ans, mul(way[i][j][k][3], mul(dp0[i][s], dp12[j][k][t - s])));
cout << ans << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
double pi = 3.1415926536;
const int oo = (int)1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int di[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dj[] = {1, -1, 0, 0, 1, -1, 1, -1};
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> myvec(n);
int mn = oo;
for (int i = 0; i < n; i++) {
cin >> myvec[i];
}
int counter = 0, ans = 0;
for (int i = 0; i < n - 1; i++) {
if (myvec[i] > myvec[i + 1]) {
ans += counter;
ans++;
counter = 0;
} else
counter++;
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
string s, s1;
cin >> n >> s;
long long ans = 0;
for (int i = 0; i < n - 1; i++) {
string st = s.substr(i, 2);
long long cnt = 0;
for (long long j = 0; j < n - 1; j++) {
string st2 = s.substr(j, 2);
if (st == st2) {
cnt++;
}
}
if (cnt > ans) {
ans = cnt;
s1 = st;
}
}
cout << s1;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int Q = 1 << 18;
long long st[2 * Q];
int n, q;
void add(int v, long long x) {
for (v += Q; v > 0; v /= 2) st[v] += x;
}
long long sum(int r) {
long long res = 0;
int l = 0;
for (l += Q, r += Q; l < r; l /= 2, r /= 2) {
if (l & 1) res += st[l++];
if (r & 1) res += st[--r];
}
return res;
}
int getL(const long long& str) {
int l = 0, r = n + 1, m;
while (r - l > 1) {
m = (l + r) / 2;
long long x = sum(m);
if (x <= str)
l = m;
else
r = m;
}
return l;
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 0; i < n; i++) {
long long x;
scanf("%I64d", &x);
add(i, x);
}
long long lastSum = 0;
for (int i = 0; i < q; i++) {
long long str;
scanf("%I64d", &str);
lastSum += str;
int l = getL(lastSum);
if (l == n) {
lastSum = 0;
printf("%d\n", n);
} else
printf("%d\n", n - l);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
int cost = 0;
cin >> n;
cost += n / 5;
n %= 5;
cost += n / 4;
n %= 4;
cost += n / 3;
n %= 3;
cost += n / 2;
n %= 2;
cost += n / 1;
n %= 1;
cout << cost << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int mn = 1e9, x;
int sm = 0;
int y = 0;
for (int i = 0; i < n; i++) {
for (int o = 0; o < m; o++) {
cin >> x;
mn = min(mn, abs(x));
sm += abs(x);
y += (x < 0);
}
}
if (y % 2 == 0) {
cout << sm << endl;
} else {
cout << sm - 2 * mn << endl;
}
}
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
const int maxn = 123456;
int n, ans, du[1005];
int main() {
scanf("%d", &n);
for (int i = 1, x, y; i < n; ++i) {
scanf("%d%d", &x, &y);
du[x]++;
du[y]++;
}
for (int i = 1; i <= n; ++i) ans += (du[i] == 1);
printf("%d\n", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long const MOD = 1e9 + 7;
void solve() {
int n, i, j;
cin >> n;
int a[2 * n];
for (i = 0; i < 2 * n; i++) cin >> a[i];
i = 0;
int curr;
vector<int> v;
int vis[55] = {0};
while (i < 2 * n) {
curr = a[i];
j = i + 1;
if (vis[curr] == 0) {
v.push_back(curr);
vis[curr] = 1;
}
while (j < 2 * n && a[j] != curr) {
if (vis[a[j]] == 0) {
v.push_back(a[j]);
vis[a[j]] = 1;
}
j++;
if (v.size() == n) break;
}
i = j + 1;
if (v.size() == n) break;
}
for (auto i : v) cout << i << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) solve();
}
| 0 |
#include <bits/stdc++.h>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <cmath>
#define s_mod 1000000007
#define pi 3.14159265
// sin((deg*pi)/180.0);
using namespace std;
long long int ceil2(long long int a, long long int b)
{
return (a + b - 1) / b;
}
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
// 人生で最も重要なことは、コードを実行する必要があるということです。
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
long long int n;
int input;
vector<long long int> ice;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>input;
ice.push_back(input);
}
long long int andx = ice[0];
for(int i=1;i<n;i++)
{
andx = andx&(ice[i]);
}
// count same as final value
long long int count = 0;
for(int i=0;i<n;i++)
{
if(ice[i]==andx)
{
count++; // will be used in the permutation
}
}
long long int permute = 0;
if(count>1)
{
permute = (count*(count-1))%s_mod;
for(int i=(n-2);i>=1;i--)
{
permute = (permute*i)%s_mod;
}
}
cout<<permute<<endl;
ice.clear();
}
return 0;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int X[100005], Y[100005];
void swap(int &x, int &y) {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
int main() {
int n, m, x, y, z, p;
scanf("%d%d%d%d%d%d", &n, &m, &x, &y, &z, &p);
x %= 4;
y %= 2;
z %= 4;
int i, j;
for (i = 0; i < p; i++) {
scanf("%d%d", &X[i], &Y[i]);
}
for (i = 0; i < x; i++) {
for (j = 0; j < p; j++) {
int tx, ty;
tx = X[j];
ty = Y[j];
X[j] = ty;
Y[j] = n + 1 - tx;
}
swap(n, m);
}
if (y % 2 == 1) {
for (j = 0; j < p; j++) {
Y[j] = m - Y[j] + 1;
}
}
for (i = 0; i < z; i++) {
for (j = 0; j < p; j++) {
int tx, ty;
tx = X[j];
ty = Y[j];
X[j] = m + 1 - ty;
Y[j] = tx;
}
swap(n, m);
}
for (j = 0; j < p; j++) printf("%d %d\n", X[j], Y[j]);
return 0;
}
| 3 |
#ifdef F
#include "dandan.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define dbg(x...)
#endif
#define FIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define w(x) int x; cin >> x; while(x--)
#define forn(i, n) for(int i = 0; i < (n); ++i)
#define int long long
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define ff first
#define ss second
#define pb push_back
#define endl '\n'
const int N = 2e5 + 5, K = 18;
//[len][i] stores count of ith letter in prefix of length len
vector<vector<int>> pref(N, vector<int> (K));
//[idx][i] stores smallest index >= idx
//where block of x characters of ith letter can be placed
vector<vector<int>> nxt(N, vector<int> (K));
int n, k;
bool check(int x)
{
fill(all(nxt[n]), n + 1);
for (int i = n - 1; i >= 0; --i)
forn(c, k)
{
nxt[i][c] = nxt[i + 1][c];
if (i + x <= n and pref[i + x][c] - pref[i][c] == x)
nxt[i][c] = i;
}
//stores (index where mask ended + 1)
vector<int> dp(1ll << k);
for (int mask = 1; mask < (1ll << k); ++mask)
{
dp[mask] = n + 1;
forn(last, k)
{
if ((mask & (1ll << last)) == 0)
continue;
int nmask = mask ^ (1ll << last);
if (dp[nmask] + x > n)
continue;
dp[mask] = min(dp[mask], nxt[dp[nmask]][last] + x);
}
}
return dp[(1ll << k) - 1] <= n;
}
int32_t main()
{
FIO
{
cin >> n >> k;
string s;
cin >> s;
forn(c, k)
for (int len = 1; len <= n; ++len)
{
pref[len][c] = pref[len - 1][c];
if (s[len - 1] - 'a' == c or s[len - 1] == '?')
pref[len][c]++;
}
int lo = 0, hi = n + 1;
while (hi - lo > 1)
{
int mid = hi + lo >> 1;
if (check(mid))
lo = mid;
else
hi = mid;
}
cout << lo;
}
return 0;
} | 8 |
#include <bits/stdc++.h>
using namespace std;
int val(string s) {
int l = s.length();
int x = 0;
for (int j = 0; j < l; j++) x = x * 2 + (((int)(s[j] - '0')) % 2);
return x;
}
int main() {
int t;
string s;
char c;
cin >> t;
vector<int> cnt(262144, 0);
multiset<string> ms;
multiset<string>::iterator it;
for (int i = 0; i < t; i++) {
cin >> c >> s;
int x = val(s);
if (c == '+') {
cnt[x]++;
ms.insert(s);
} else if (c == '-') {
cnt[x]--;
it = ms.find(s);
ms.erase(it);
} else {
cout << cnt[x] << "\n";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
int count = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 1) count++;
}
cout << count << endl;
for (int i = 1; i < n; i++) {
if (a[i] == 1) cout << a[i - 1] << ' ';
}
cout << a[n - 1];
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimization("O3")
using namespace std;
const long long inf = INT_MAX;
const long long inf2 = 1e18;
const long long MAX = 1e5 + 5;
const long long MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double EPS = 1e-9;
void init(int precision) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(precision);
}
inline void iofile() { freopen("input.txt", "r", stdin); }
long long n;
int main() {
init(10);
cin >> n;
cout << (n & 1 ? 0 : ((n / 2 - 1) / 2)) << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
template <typename T>
std::string to_string(T value) {
std::ostringstream os;
os << value;
return os.str();
}
using namespace std;
long double cnt[800005];
long double sm[800005];
void add(int node, int b, int e, int val, long long r_val) {
if (b > val || e < val) {
return;
}
if (b <= val && val <= e) {
cnt[node]++;
sm[node] += r_val;
if (b == e) {
return;
}
}
int md = (b + e) / 2;
int left = 2 * node;
int right = left + 1;
add(left, b, md, val, r_val);
add(right, md + 1, e, val, r_val);
return;
}
long double ans;
void query(int node, int b, int e, int x, int y, int r_val) {
if (b > y || e < x) {
return;
}
if (x <= b && e <= y) {
long long t = cnt[node] * r_val;
t -= sm[node];
ans += t;
return;
}
int md = (b + e) / 2;
int left = 2 * node;
int right = left + 1;
query(left, b, md, x, y, r_val);
query(right, md + 1, e, x, y, r_val);
return;
}
int main() {
int n;
scanf("%d", &n);
int ns[n];
map<int, int> mp1;
for (int i = 0; i < n; i++) {
scanf("%d", &ns[i]);
mp1.insert({ns[i], 0});
}
int val = 0;
for (auto it = mp1.begin(); it != mp1.end(); it++) {
val++;
it->second = val;
}
int mx = val;
map<int, int> mp2;
for (auto it = mp1.begin(); it != mp1.end(); it++) {
mp2.insert({it->second, it->first});
}
add(1, 1, mx, mp1[ns[0]], ns[0]);
ans = 0;
map<int, int> mp;
mp.insert({ns[0], mp1[ns[0]]});
for (int i = 1; i < n; i++) {
int val = mp1[ns[i]];
long long r_val = ns[i];
auto it = mp.lower_bound(r_val + 2);
if (it != mp.end()) {
query(1, 1, mx, it->second, mx, r_val);
}
it = mp.lower_bound(r_val - 1);
if (it != mp.begin()) {
it--;
query(1, 1, mx, 1, it->second, r_val);
}
add(1, 1, mx, val, r_val);
mp.insert({r_val, val});
}
string s = to_string(ans);
for (int i = 0; i < s.length(); i++) {
if (s[i] == '.') {
break;
}
cout << s[i];
}
cout << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, x, y, z;
int main() {
while (~scanf("%d%d%d%d%d%d", &a, &b, &c, &x, &y, &z)) {
int res1 = 0, res2 = 0;
if (a > x)
res1 += (a - x) / 2;
else
res2 += x - a;
if (b > y)
res1 += (b - y) / 2;
else
res2 += y - b;
if (c > z)
res1 += (c - z) / 2;
else
res2 += z - c;
if (res1 >= res2)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<long long, long long>;
using vi = vector<long long>;
using vvi = vector<vi>;
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto i = begin(v); i != end(v); i++)
os << *i << (i == end(v) - 1 ? "" : " ");
return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto i = begin(v); i != end(v); i++) is >> *i;
return is;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T, class U>
bool chmax(T &a, const U &b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
void psum(T &c) {
partial_sum(begin(c), end(c), begin(c));
}
template <class T>
using heap = priority_queue<T, vector<T>, greater<T>>;
struct before_main_function {
before_main_function() {
cin.tie(nullptr);
ios::sync_with_stdio(0);
cout << setprecision(15) << fixed;
}
} before_main_function;
signed main() {
long long T;
cin >> T;
while (T--) {
string s;
cin >> s;
vector<pair<char, long long>> L;
L.emplace_back(s.front(), 1);
for (long long i = 1, ilen = (long long)(((long long)s.size())); i < ilen;
++i) {
if (s[i] == s[i - 1]) {
L.back().second++;
} else {
L.emplace_back(s[i], 1);
}
}
set<char> ans;
for (auto x : L) {
if (x.second % 2) {
ans.insert(x.first);
}
}
for (char c : ans) cout << c;
cout << endl;
}
return (0 ^ 0 ^ 0);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> a[100005];
int ss, dd = 0, ans = 1;
void dfs(int x, int p, int d) {
if (a[x].size() == 1) {
if (d > dd) {
ss = x;
dd = d;
}
ans = max(ans, d);
}
for (int i = 0; i < a[x].size(); i++)
if (a[x][i] != p) dfs(a[x][i], x, d + 1);
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1, -1, 0);
dfs(ss, -1, 0);
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3020;
const int mod = int(1e9) + 7;
int n, m;
char c[maxn][maxn];
long long d[3][maxn][maxn], AA, BB, AB, BA;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
d[1][1][2] = (c[1][2] == '.');
d[2][2][1] = (c[2][1] == '.');
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (c[i][j + 1] == '.') {
for (int k = 1; k <= 2; k++) {
d[k][i][j + 1] += d[k][i][j];
if (d[k][i][j + 1] >= mod) d[k][i][j + 1] -= mod;
}
}
if (c[i + 1][j] == '.') {
for (int k = 1; k <= 2; k++) {
d[k][i + 1][j] += d[k][i][j];
if (d[k][i + 1][j] >= mod) d[k][i + 1][j] -= mod;
}
}
}
AA = d[1][n - 1][m], BB = d[2][n][m - 1];
AB = d[1][n][m - 1], BA = d[2][n - 1][m];
cout << (((AA * BB) % mod) + mod - (AB * BA) % mod) % mod;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <class T1>
void deb(T1 e) {
cout << e << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
string arr[110], arr2[110];
vector<int> adj[30];
int ind[30];
string top_sort() {
string ans;
int i, u, v;
queue<int> q;
for (i = 0; i < 26; i++) {
if (!ind[i]) {
ans += (char)(i + 'a');
q.push(i);
}
}
while (!q.empty()) {
u = q.front();
q.pop();
for (i = 0; i < (int)adj[u].size(); i++) {
v = adj[u][i];
ind[v]--;
if (!ind[v]) {
ans += (char)(v + 'a');
q.push(v);
}
}
}
if ((int)ans.size() == 26) return ans;
return "Impossible";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int i, j, n, c, k;
c = 0;
int maxi = 0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> arr[i];
arr2[i] = "";
maxi = max(maxi, (int)arr[i].size());
}
int a, b;
int mini;
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++) {
mini = min((int)arr[i].size(), (int)arr[j].size());
if (arr[i].substr(0, mini) == arr[j].substr(0, mini) &&
(int)arr[i].size() > (int)arr[j].size()) {
deb("Impossible");
return 0;
}
}
for (j = 0; j < maxi; j++) {
for (i = 0; i < n; i++) {
for (k = i + 1; k < n; k++) {
if (arr2[i] == arr2[k] && j < (int)arr[i].size() &&
j < (int)arr[k].size()) {
a = arr[i][j] - 'a';
b = arr[k][j] - 'a';
if (a != b) {
adj[a].push_back(b);
ind[b]++;
}
}
}
}
for (i = 0; i < n; i++)
if (j < (int)arr[i].size()) arr2[i] += arr[i][j];
}
deb(top_sort());
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
string s;
cin >> n >> s;
long long right = 0;
long long left = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') right++;
if (s[i] == ')' && !right) left++;
if (s[i] == ')' && right) right--;
}
if ((right + left) % 2 == 0 && right <= 1 && left <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = 0;
}
}
vector<vector<int>> qs(q);
for (int i = 0; i < q; i++) {
int t;
cin >> t;
if (t == 1 || t == 2) {
int x;
cin >> x;
qs[i].push_back(t);
qs[i].push_back(x);
} else {
int r, c, x;
cin >> r >> c >> x;
qs[i].push_back(t);
qs[i].push_back(r);
qs[i].push_back(c);
qs[i].push_back(x);
}
}
for (int i = q - 1; i >= 0; i--) {
int t = qs[i][0];
if (t == 1) {
int r = qs[i][1];
r--;
int l = a[r][m - 1];
for (int j = m - 1; j > 0; j--) {
a[r][j] = a[r][j - 1];
}
a[r][0] = l;
} else if (t == 2) {
int c = qs[i][1];
c--;
int l = a[n - 1][c];
for (int j = n - 1; j > 0; j--) {
a[j][c] = a[j - 1][c];
}
a[0][c] = l;
} else {
int r = qs[i][1], c = qs[i][2], x = qs[i][3];
r--;
c--;
a[r][c] = x;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << " ";
}
cout << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int N, C, w;
string name[101] = {
"dummy", "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na",
"Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V",
"Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br",
"Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag",
"Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr",
"Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu",
"Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi",
"Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am",
"Cm", "Bk", "Cf", "Es", "Fm"};
string X[20];
int A[20], W[20], sum, csum;
int all, dp[1 << 20], sb[1 << 20];
vector<int> sol;
void solve() {
all = (1 << N) - 1;
dp[0] = 0;
sb[0] = 0;
for (int msk = 0; msk <= all; msk++) {
for (int I = 0; I < N; I++)
if (!(msk & (1 << I))) {
int tdp = dp[msk];
int tsb = sb[msk] + A[I + 1];
if (tsb == W[tdp + 1]) {
tsb = 0;
tdp++;
}
if (tdp >= dp[msk ^ (1 << I)]) {
dp[msk ^ (1 << I)] = tdp;
sb[msk ^ (1 << I)] = tsb;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> C;
for (int I = 1; I <= N; I++) {
cin >> X[I];
A[I] = find(name, name + 101, X[I]) - name;
sum += A[I];
}
for (int I = 1; I <= C; I++) {
cin >> X[I];
W[I] = find(name, name + 101, X[I]) - name;
csum += W[I];
}
assert(sum == csum);
w = C;
solve();
if (dp[all] == C && sb[all] == 0) {
cout << "YES" << endl;
int curr = all;
do {
sol.clear();
assert(!sb[curr]);
for (int I = 0; I < N; I++)
if (curr & (1 << I))
if (dp[curr ^ (1 << I)] == dp[curr] - 1 &&
sb[curr ^ (1 << I)] + A[I + 1] == W[dp[curr]]) {
sol.push_back(I + 1);
curr ^= (1 << I);
break;
}
while (sb[curr]) {
for (int I = 0; I < N; I++)
if (curr & (1 << I)) {
if (dp[curr ^ (1 << I)] == dp[curr]) {
sol.push_back(I + 1);
curr ^= (1 << I);
}
}
}
for (int I = 0; I < int((sol).size()); I++) {
cout << name[A[sol[I]]];
if (I + 1 < int((sol).size())) cout << "+";
}
cout << "->" << name[W[w--]] << endl;
} while (curr);
} else {
cout << "NO" << endl;
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int dx2[] = {+1, -1, 0, 0, +1, +1, -1, -1};
int dy2[] = {0, 0, +1, -1, +1, -1, -1, +1};
long long squ(long long x) { return (x * x); }
long long power(long long n, long long k) {
long long N = n;
for (long long i = 2; i <= k; i++) N *= n;
return N;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int mx = 0, rm, ar = 0, i, j, sr, ck, n;
string s;
for (i = 0; i < 3; i++) s += ('A' + i);
vector<int> x(3), y(3);
for (i = 0; i < 3; i++) {
cin >> x[i] >> y[i];
ar += (x[i] * y[i]);
}
for (i = 1; i <= 200; i++) {
if (squ(i) == ar) {
n = i;
break;
}
if (squ(i) > ar) {
cout << -1;
return 0;
}
}
vector<int> v;
for (i = 0; i < 3; i++) {
if (x[i] == n) {
swap(x[i], y[i]);
}
if (y[i] == n) v.push_back(i);
}
if (v.size() == 3) {
cout << n << endl;
for (i = 0; i < 3; i++) {
for (j = 0; j < x[i]; j++) {
for (rm = 0; rm < y[i]; rm++) cout << s[i];
cout << endl;
}
}
return 0;
}
if (v.size() == 1) {
int cur = x[v[0]];
rm = n - cur;
ck = 0;
for (i = 0; i < 3; i++) {
if (i != v[0]) {
if (y[i] == rm) swap(x[i], y[i]);
if (x[i] == rm) ck++;
}
}
if (ck == 2) {
int p1 = (v[0] + 1) % 3;
int p2 = (p1 + 1) % 3;
cout << n << endl;
for (i = 0; i < cur; i++) {
for (j = 0; j < y[v[0]]; j++) cout << s[v[0]];
cout << endl;
}
for (i = 0; i < rm; i++) {
for (j = 0; j < y[p1]; j++) cout << s[p1];
for (j = 0; j < y[p2]; j++) cout << s[p2];
cout << endl;
}
}
return 0;
}
cout << -1;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n) {
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
inline long long getPow(long long a, long long b) {
long long res = 1, tp = a;
while (b) {
if (b & 1ll) {
res *= tp;
res %= 1000000007;
}
tp *= tp;
tp %= 1000000007;
b >>= 1ll;
}
return res;
}
inline long long to_ll(string s) {
long long cur = 0;
for (int i = 0; i < s.size(); i++) {
cur = cur * 10 + s[i] - '0';
}
return cur;
}
inline string to_str(long long x) {
string s = "";
while (x) {
s += char(x % 10 + '0');
x /= 10;
}
reverse(s.begin(), s.end());
return s;
}
inline long long nxt() {
long long x;
scanf("%lld", &x);
return x;
}
void ok() {
puts("YES");
exit(0);
}
void panic() {
puts("NO");
exit(0);
}
const long long N = 1e6 + 5;
int main() {
long long n = nxt();
vector<char> buf(n);
set<char> fix;
for (int i = 0; i < n; i++) {
cin >> buf[i];
fix.insert(buf[i]);
}
long long k = nxt();
vector<string> mas(k);
for (int i = 0; i < k; i++) {
cin >> mas[i];
}
vector<long long> ans(26, 1);
for (int i = 0; i < k; i++) {
bool flag = 1;
for (int j = 0; j < n; j++) {
if (binary_search(fix.begin(), fix.end(), mas[i][j]) && buf[j] == '*') {
flag = 0;
}
if (mas[i][j] != buf[j] && buf[j] != '*') {
flag = 0;
}
}
for (int j = 0; j < 26 && flag; j++) {
if (mas[i].find(j + 'a') == string::npos ||
binary_search(fix.begin(), fix.end(), j + 'a')) {
ans[j] = 0;
}
}
}
cout << accumulate(ans.begin(), ans.end(), 0ll);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool vis[100001];
int make[100001], cnt = 0;
void linear_prime_build(int n) {
int i, j;
for (i = 2; i <= n; i++) {
if (!vis[i]) make[++cnt] = i;
for (j = 1; j <= cnt && i * make[j] <= n; j++) {
vis[i * make[j]] = true;
if (i % make[j] == 0) break;
}
}
}
int main() {
int n, i;
scanf("%d", &n);
n++;
linear_prime_build(n);
printf(n <= 3 ? "1\n" : "2\n");
for (i = 2; i <= n; i++) printf(vis[i] ? "2 " : "1 ");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b;
cin >> a >> b;
long long ans = 0;
long long d = (b * (b - 1) / 2) % 1000000007;
long long c = (a * (a + 1) / 2) % 1000000007;
ans = (c * b + a) % 1000000007;
cout << (ans * d) % 1000000007;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool exist[4004005];
int n;
int x[3005], y[3005];
int main() {
while (scanf("%d", &n) != EOF) {
memset(exist, 0, sizeof(exist));
for (int i = 0; i < n; i++) {
scanf("%d%d", &x[i], &y[i]);
x[i] += 1000;
y[i] += 1000;
exist[x[i] * 2001 + y[i]] = true;
}
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j) {
int nx = x[j] + x[i], ny = y[j] + y[i];
if (nx % 2 == 1 || ny % 2 == 1) continue;
nx /= 2;
ny /= 2;
if (exist[nx * 2001 + ny]) ans++;
}
printf("%d\n", ans / 2);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const uint32_t mod7 = uint32_t(1000000007);
const uint32_t mod9 = uint32_t(1000000009);
const int32_t inf31 = numeric_limits<int32_t>::max();
const uint32_t inf32 = numeric_limits<uint32_t>::max();
const int64_t inf63 = numeric_limits<int64_t>::max();
const uint64_t inf64 = numeric_limits<uint64_t>::max();
inline bool isvowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
string get_string(string& s, uint32_t k) {
for (uint32_t i = uint32_t(s.size()) - 1, j = 0; i < s.size(); --i) {
if (isvowel(s[i]) && ++j == k) {
return s.substr(i);
}
}
throw 0;
}
int32_t main(int32_t argc, char const* argv[]) {
ios::sync_with_stdio(0);
cin.tie(0);
uint32_t n, k;
cin >> n >> k;
try {
vector<vector<string>> quatrains(n);
for (uint32_t i = 0; i < n; ++i) {
for (uint32_t j = 0; j < 4; ++j) {
string s;
cin >> s;
quatrains[i].push_back(get_string(s, k));
}
}
bool flag = true;
for (uint32_t i = 0; i < n; ++i) {
if (quatrains[i][0].compare(quatrains[i][1]) != 0 ||
quatrains[i][1].compare(quatrains[i][2]) != 0 ||
quatrains[i][2].compare(quatrains[i][3]) != 0) {
flag = false;
break;
}
}
if (flag) {
cout << "aaaa" << '\n';
return 0;
}
flag = true;
for (uint32_t i = 0; i < n; ++i) {
if (quatrains[i][0].compare(quatrains[i][2]) != 0 ||
quatrains[i][1].compare(quatrains[i][3]) != 0) {
flag = false;
break;
}
}
if (flag) {
cout << "abab" << '\n';
return 0;
}
flag = true;
for (uint32_t i = 0; i < n; ++i) {
if (quatrains[i][0].compare(quatrains[i][3]) != 0 ||
quatrains[i][1].compare(quatrains[i][2]) != 0) {
flag = false;
break;
}
}
if (flag) {
cout << "abba" << '\n';
return 0;
}
flag = true;
for (uint32_t i = 0; i < n; ++i) {
if (quatrains[i][0].compare(quatrains[i][1]) != 0 ||
quatrains[i][2].compare(quatrains[i][3]) != 0) {
flag = false;
break;
}
}
if (flag) {
cout << "aabb" << '\n';
return 0;
}
cout << "NO" << '\n';
} catch (int32_t _) {
cout << "NO" << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000010;
int MOD = 998244353;
int inv2 = (MOD + 1) >> 1, N, n, x, y, z;
long long a1[MAXN], b1[MAXN], a2[MAXN], b2[MAXN], a3[MAXN], b3[MAXN];
long long quick_power(long long a, long long t, long long mod) {
long long ans = 1;
while (t) {
if (t & 1) ans = ans * a % mod;
a = a * a % mod;
t >>= 1;
}
return ans;
}
void or_FWT(long long *P, int opt) {
for (int i = 2; i <= N; i <<= 1)
for (int p = i >> 1, j = 0; j < N; j += i)
for (int k = j; k < j + p; ++k)
P[k + p] += P[k] * opt, P[k + p] = (P[k + p] + MOD) % MOD;
}
void and_FWT(long long *P, int opt) {
for (int i = 2; i <= N; i <<= 1)
for (int p = i >> 1, j = 0; j < N; j += i)
for (int k = j; k < j + p; ++k)
P[k] += P[k + p] * opt, P[k] = (P[k] + MOD) % MOD;
}
void xor_FWT(long long *P, int opt) {
for (int i = 2; i <= N; i <<= 1)
for (int p = i >> 1, j = 0; j < N; j += i)
for (int k = j; k < j + p; ++k) {
long long x = P[k], y = P[k + p];
P[k] = (x + y) % MOD;
P[k + p] = (x - y + MOD) % MOD;
if (opt == -1)
P[k] = 1ll * P[k] * inv2 % MOD,
P[k + p] = 1ll * P[k + p] * inv2 % MOD;
}
}
int main() {
scanf("%d %d", &n, &N);
N = 1 << N;
int sum = 0;
scanf("%d %d %d", &x, &y, &z);
for (int i = 0, a, b, c; i < n; i++) {
scanf("%d %d %d", &a, &b, &c);
sum ^= a;
b ^= a;
c ^= a;
a1[b]++;
a2[c]++;
a3[b ^ c]++;
}
xor_FWT(a1, 1);
xor_FWT(a2, 1);
xor_FWT(a3, 1);
for (int i = 0; i < N; i++) {
int c1 = (1ll * n + a1[i] + a2[i] + a3[i]) % MOD / 4;
int c2 = (1ll * n + a1[i] - c1 - c1 + MOD) % MOD / 2;
int c3 = (1ll * n + a2[i] - c1 - c1 + MOD) % MOD / 2;
int c4 = (1ll * n + a3[i] - c1 - c1 + MOD) % MOD / 2;
long long ans = 1;
ans = ans * quick_power((1ll * x + y + z), c1, MOD) % MOD;
ans = ans * quick_power((1ll * x + y - z + MOD) % MOD, c2, MOD) % MOD;
ans = ans * quick_power((1ll * x - y + z + MOD) % MOD, c3, MOD) % MOD;
ans = ans * quick_power((1ll * x - y - z + MOD * 2) % MOD, c4, MOD) % MOD;
b1[i] = ans;
}
xor_FWT(b1, -1);
for (int i = 0; i < N; i++) {
printf("%lld ", b1[i ^ sum]);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
char s[10010];
int main() {
scanf("%s", s);
int n = strlen(s);
printf("%s", s);
for (int i = n - 1; i >= 0; i--) putchar(s[i]);
puts("");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int mins, n, k, a[10], last[10];
char str[10][10];
struct s {
char str1[10];
} na[10];
bool cmp(s c, s d) {
if (strcmp(c.str1, d.str1) > 0) return true;
return false;
}
void solve() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) na[i].str1[j] = str[i][a[j] - 1];
na[i].str1[k] = '\0';
}
sort(na, na + n, cmp);
int c = atoi(na[0].str1), d = atoi(na[n - 1].str1);
int t = c - d;
if (t < mins) mins = t;
}
int main() {
while (scanf("%d %d", &n, &k) == 2) {
mins = 100000000;
for (int i = 0; i < n; i++) {
scanf("%s", str[i]);
strcpy(na[i].str1, str[i]);
}
if (k == 1) {
sort(na, na + n, cmp);
printf("%d\n", na[0].str1[0] - na[n - 1].str1[0]);
continue;
}
for (int i = 0; i < k; i++) a[i] = i + 1;
do {
if (!equal(a, a + k, last)) {
copy(a, a + k, last);
solve();
}
} while (next_permutation(a, a + k));
printf("%d\n", mins);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int ar[100010], n, dp[100010][2], a, b;
int rec(int p, int v) {
if (p == n) return 0;
if (dp[p][v] != -1) return dp[p][v];
if (v == 0)
return dp[p][v] = max(rec(p + 1, 1) + ar[p], rec(p + 1, 0) - ar[p]);
else
return dp[p][v] = min(rec(p + 1, 0) - ar[p], rec(p + 1, 1) + ar[p]);
}
void prin(int p, int v) {
if (p == n) return;
if (v == 0) {
if (rec(p + 1, 1) + ar[p] == rec(p, v)) {
a += ar[p];
prin(p + 1, 1);
} else {
b += ar[p];
prin(p + 1, 0);
}
} else {
if (rec(p + 1, 0) - ar[p] == rec(p, v)) {
b += ar[p];
prin(p + 1, 0);
} else {
a += ar[p];
prin(p + 1, 1);
}
}
return;
}
int main() {
ios::sync_with_stdio(false);
memset(dp, -1, sizeof(dp));
int i, j, m, c = 0;
cin >> n;
for (int i = 0; i <= n - 1; i++) cin >> ar[i], c += ar[i];
j = rec(0, 0);
prin(0, 0);
cout << b << ' ' << a << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
if ((a + b + c + d + e + f) % 2 != 0) {
cout << "NO" << endl;
return 0;
}
vector<long long> v(6);
v[0] = a;
v[1] = b;
v[2] = c;
v[3] = d;
v[4] = e;
v[5] = f;
int l1, l2, l3, flag = 1;
for (int i(0); i < 6; i++) {
for (int j(1); j < 6; j++) {
for (int k(2); k < 6; k++) {
if (i != j && i != k && j != k) {
for (int l(0); j < 6; l++) {
if (i != l && j != l && k != l && flag == 1) {
l1 = l;
flag++;
} else if (i != l && j != l && k != l && flag == 2) {
l2 = l;
flag++;
} else if (i != l && j != l && k != l && flag == 3) {
l3 = l;
flag = 1;
break;
}
}
if (v[i] + v[j] + v[k] == v[l1] + v[l2] + v[l3]) {
cout << "YES";
return 0;
}
}
}
}
}
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int n, m;
long long int arr[300000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long int TESTS, q, a, s, b, r, k, c, p, h, w, d, x, y, z, xs, ys, t;
TESTS = 1;
while (TESTS--) {
cin >> n;
for (long long int i = 0; i <= n - 1; i++) cin >> arr[i];
sort(arr, arr + n);
long long int sum = arr[0];
long long int prev = arr[0];
long long int num = 0;
long long int cou = 0;
if (arr[0] == 0) cou++;
for (long long int i = 1; i <= n - 1; i++) {
if (arr[i] - i < prev) {
num++;
if (i > 1) {
if (arr[i - 2] >= arr[i] - 1) {
cout << "cslnb";
return 0;
}
}
}
sum += arr[i] - i;
if (arr[i] == 0) cou++;
prev = arr[i] - i;
if (prev < 0) {
cout << "cslnb";
return 0;
}
}
if (cou == n) {
cout << "cslnb";
return 0;
}
if (num > 1)
cout << "cslnb";
else {
if (sum % 2 == 0)
cout << "cslnb";
else
cout << "sjfnb";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1510, MOD = 1e9 + 7, K = 100010;
long long power(long long a, int b) {
long long res = 1;
while (b) {
if (b & 1) (res *= a) %= MOD;
(a *= a) %= MOD;
b >>= 1;
}
return res;
}
int n, m, a, b, k;
long long p, dp[N][N], jc[K], inv[K], pro[N], sum[N], spro[N], ans;
long long comb(int a, int b) {
return (a < 0 || b < 0 || a < b) ? 0
: jc[a] * inv[b] % MOD * inv[a - b] % MOD;
}
void prework() {
jc[0] = 1;
for (int i = 1; i <= k; ++i) jc[i] = jc[i - 1] * i % MOD;
inv[k] = power(jc[k], MOD - 2);
for (int i = k - 1; i >= 0; --i) inv[i] = inv[i + 1] * (i + 1) % MOD;
for (int i = 0; i <= m && i <= k; ++i)
pro[i] = comb(k, i) * power(p, i) % MOD * power(MOD + 1 - p, k - i) % MOD;
for (int i = 0; i <= m; ++i) spro[i] = pro[i];
for (int i = 1; i <= m; ++i) (spro[i] += spro[i - 1]) %= MOD;
}
int main() {
scanf("%d%d%d%d%d", &n, &m, &a, &b, &k);
p = 1ll * a * power(b, MOD - 2) % MOD;
prework();
dp[0][m] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) sum[j] = (sum[j - 1] + dp[i - 1][j]) % MOD;
for (int j = 1; j <= m; ++j) {
dp[i][j] = (dp[i][j - 1] + pro[j - 1] * sum[m] % MOD) % MOD;
(dp[i][j] -= pro[j - 1] * sum[j - 1] % MOD) %= MOD;
}
for (int j = 1; j <= m; ++j) {
(dp[i][j] -= sum[m - j] * spro[j - 1] % MOD) %= MOD;
(dp[i][j] *= pro[m - j]) %= MOD;
}
}
for (int i = 1; i <= m; ++i) (ans += dp[n][i]) %= MOD;
ans = (ans % MOD + MOD) % MOD;
cout << ans << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
char s1[10], s2[10];
int main() {
int d1, d2, dd;
scanf("%s%s", s1, s2);
if (s1[0] == 's' && s1[1] == 'u')
d1 = 0;
else if (s1[0] == 'm')
d1 = 1;
else if (s1[0] == 't' && s1[1] == 'u')
d1 = 2;
else if (s1[0] == 'w')
d1 = 3;
else if (s1[0] == 't' && s1[1] == 'h')
d1 = 4;
else if (s1[0] == 'f')
d1 = 5;
else if (s1[0] == 's' && s1[1] == 'a')
d1 = 6;
if (s2[0] == 's' && s2[1] == 'u')
d2 = 0;
else if (s2[0] == 'm')
d2 = 1;
else if (s2[0] == 't' && s2[1] == 'u')
d2 = 2;
else if (s2[0] == 'w')
d2 = 3;
else if (s2[0] == 't' && s2[1] == 'h')
d2 = 4;
else if (s2[0] == 'f')
d2 = 5;
else if (s2[0] == 's' && s2[1] == 'a')
d2 = 6;
dd = (d2 - d1 + 7) % 7;
if (dd == 0 || dd == 3 || dd == 2)
printf("YES");
else
printf("NO");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[200005], s1[200005], s2[200005], s3[200005], s4[200005], s5[200005],
s6[200005];
void solve() {
int n, ans = 1e9, flag = 0, temp = 0;
scanf("%d %s", &n, s);
for (int i = 0; i < n; i++) {
if (i % 3 == 0)
s1[i] = 'R';
else if (i % 3 == 1)
s1[i] = 'B';
else
s1[i] = 'G';
if (s1[i] != s[i]) temp++;
}
if (temp < ans) {
flag = 1;
ans = temp;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0)
s2[i] = 'R';
else if (i % 3 == 1)
s2[i] = 'G';
else
s2[i] = 'B';
if (s2[i] != s[i]) temp++;
}
if (temp < ans) {
flag = 2;
ans = temp;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0)
s3[i] = 'B';
else if (i % 3 == 1)
s3[i] = 'R';
else
s3[i] = 'G';
if (s3[i] != s[i]) temp++;
}
if (temp < ans) {
flag = 3;
ans = temp;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0)
s4[i] = 'B';
else if (i % 3 == 1)
s4[i] = 'G';
else
s4[i] = 'R';
if (s4[i] != s[i]) temp++;
}
if (temp < ans) {
flag = 4;
ans = temp;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0)
s5[i] = 'G';
else if (i % 3 == 1)
s5[i] = 'R';
else
s5[i] = 'B';
if (s5[i] != s[i]) temp++;
}
if (temp < ans) {
flag = 5;
ans = temp;
}
temp = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0)
s6[i] = 'G';
else if (i % 3 == 1)
s6[i] = 'B';
else
s6[i] = 'R';
if (s6[i] != s[i]) temp++;
}
if (temp < ans) {
flag = 6;
ans = temp;
}
printf("%d\n", ans);
if (flag == 1)
printf("%s\n", s1);
else if (flag == 2)
printf("%s\n", s2);
else if (flag == 3)
printf("%s\n", s3);
else if (flag == 4)
printf("%s\n", s4);
else if (flag == 5)
printf("%s\n", s5);
else if (flag == 6)
printf("%s\n", s6);
}
int main() { solve(); }
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int a[100];
int n;
int main() {
cin >> n;
for (int i = 0; i < (int)(n); i++) cin >> a[i];
std::sort(a, a + n);
int solved = 0;
int pena = 0;
int sum = 10;
;
for (int i = 0; i < n; i++) {
sum += a[i];
if (sum > 720) break;
solved++;
if (sum <= 360) {
pena += 0;
} else {
pena += sum - 360;
}
}
cout << solved << " " << pena << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
const int mod = 1000000007;
struct Mod {
public:
int num;
Mod() : Mod(0) { ; }
Mod(long long int n) : num((n % mod + mod) % mod) {
static_assert(mod < INT_MAX / 2,
"mod is too big, please make num 'long long int' from 'int'");
}
Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
operator int() { return num; }
};
Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) {
return Mod((mod + a.num - b.num) % mod);
}
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) {
return Mod(((long long)a.num * b.num) % mod);
}
Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b) * a; }
Mod operator*(const Mod a, const int b) { return Mod(b) * a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod &a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
if (n == 0) return Mod(1);
Mod res = (a * a) ^ (n / 2);
if (n % 2) res = res * a;
return res;
}
Mod mod_pow(const Mod a, const long long int n) {
if (n == 0) return Mod(1);
Mod res = mod_pow((a * a), (n / 2));
if (n % 2) res = res * a;
return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
assert(b.num != 0);
return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) { return Mod(a) / b; }
Mod operator/=(Mod &a, const Mod b) { return a = a / b; }
Mod fact[1024000], factinv[1024000];
void init(const int amax = 1024000) {
fact[0] = Mod(1);
factinv[0] = 1;
for (int i = 0; i < amax - 1; ++i) {
fact[i + 1] = fact[i] * Mod(i + 1);
factinv[i + 1] = factinv[i] / Mod(i + 1);
}
}
Mod comb(const int a, const int b) {
return fact[a] * factinv[b] * factinv[a - b];
}
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
};
int main() {
int N;
cin >> N;
vector<pair<int, int>> ps;
map<int, vector<int>> x_mp, y_mp;
for (int i = 0; i < N; ++i) {
int a, b;
scanf("%d %d", &a, &b);
ps.push_back(make_pair(a, b));
x_mp[a].push_back(i);
y_mp[b].push_back(i);
}
UnionFind uf(N);
for (auto am : x_mp) {
for (int i = 0; i < am.second.size() - 1; ++i) {
uf.unionSet(am.second[i], am.second[i + 1]);
}
}
for (auto am : y_mp) {
for (int i = 0; i < am.second.size() - 1; ++i) {
uf.unionSet(am.second[i], am.second[i + 1]);
}
}
map<int, vector<int>> ids;
for (int i = 0; i < N; ++i) {
ids[uf.root(i)].push_back(i);
}
Mod answer = 1;
for (auto idd : ids) {
auto v(idd.second);
vector<int> xs, ys;
for (auto n : v) {
xs.emplace_back(ps[n].first);
ys.emplace_back(ps[n].second);
}
sort(xs.begin(), xs.end());
sort(ys.begin(), ys.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
int sum = xs.size() + ys.size();
if (sum > v.size()) {
assert(sum == v.size() + 1);
answer *= mod_pow(2, sum) - Mod(1);
} else {
answer *= mod_pow(2, sum);
}
}
cout << answer.num << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long k, b, n, t;
int main() {
cin >> k >> b >> n >> t;
long long nr = 1;
for (int i = 1; i <= n; ++i) {
nr = nr * k + b;
if (nr >= t) {
if (nr == t) {
cout << n - i;
} else {
cout << n - i + 1;
}
return 0;
}
}
cout << 0;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<string> s;
for (int i = 0; i < 4 * n; i++) {
string p;
cin >> p;
string temp;
int count = 0;
for (int j = 0; j < p.size(); j++) {
if (p[j] == 'a' || p[j] == 'e' || p[j] == 'i' || p[j] == 'o' ||
p[j] == 'u')
count++;
}
if (count < k) {
cout << "NO";
exit(0);
}
count = 0;
for (int j = p.size() - 1; j >= 0; j--) {
temp.push_back(p[j]);
if (p[j] == 'a' || p[j] == 'e' || p[j] == 'i' || p[j] == 'o' ||
p[j] == 'u')
count++;
if (count == k) break;
}
s.push_back(temp);
}
string a[] = {"aaaa", "aabb", "abab", "abba"};
vector<vector<int> > l;
for (int i = 0; i < s.size(); i = i + 4) {
vector<int> temp;
if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == s[i + 3])
temp.push_back(0);
if (s[i] == s[i + 1] && s[i + 2] == s[i + 3]) temp.push_back(1);
if (s[i] == s[i + 2] && s[i + 1] == s[i + 3]) temp.push_back(2);
if (s[i] == s[i + 3] && s[i + 1] == s[i + 2]) temp.push_back(3);
if (temp.size() == 0) {
cout << "NO";
exit(0);
}
l.push_back(temp);
}
for (int i = 0; i < l.size(); i++) {
for (int j = 0; j < l[i].size(); j++) {
int flag = 0;
for (int k = 0; k < l.size(); k++) {
if (k != i) {
for (int m = 0; m < l[k].size(); m++)
if (l[k][m] == l[i][j]) flag++;
}
}
if (flag == l.size() - 1) {
cout << a[l[i][j]];
exit(0);
}
}
}
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, b, p, kalanOyuncu, bottle, towel, x;
int ikiUssu(int a) {
int cevap = 1, i = 0;
for (; cevap <= a; cevap *= 2) {
i++;
}
return i - 1;
}
void fonksiyon(int kisiSayisi) {
if (kisiSayisi == 1) {
cout << bottle << " " << towel;
return;
}
x = ikiUssu(kisiSayisi);
bottle += x * (2 * b + 1);
fonksiyon(kisiSayisi - x);
}
int main(int argc, const char* argv[]) {
cin >> n >> b >> p;
towel = n * p;
fonksiyon(n);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, n, k;
cin >> m >> n >> k;
int arr[1005];
memset(arr, 255, sizeof(arr));
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
if (arr[a] == -1)
arr[a] = b;
else
arr[a] = min(arr[a], b);
}
int res = 0;
for (int i = 0; i < n; i++)
if (arr[i] >= 0) res += arr[i];
cout << min(res, k) << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = ~(1 << 31);
inline int in() {
int a;
scanf("%d", &a);
return a;
}
template <class T1, class T2>
inline pair<T1, T2> reverse_make_pair(T1 x, T2 y) {
return pair<T1, T2>(y, x);
}
template <class T>
ostream& operator<<(ostream& out, const vector<T> a) {
for (int i = 0; i < (int)a.size(); i++) out << a[i] << ' ';
return out;
}
template <class T>
ostream& operator<<(ostream& out, const set<T> a) {
int i = 0;
while (a.begin() + i != a.end()) {
cout << *(a.begin() + i) << ' ';
++i;
}
return out;
}
double din() {
double a;
scanf("%lf", &a);
return a;
}
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const double eps = 1e-6;
int logbin(int a) {
a--;
int res = 1;
while (a) a >>= 1, res <<= 1;
return res;
}
int binpow(int a, int n) {
int res = 1;
while (n) {
if (n & 1) res *= a;
a *= a;
n >>= 1;
}
return res;
}
struct treap {
treap *l, *r;
int c, y;
int size;
char color;
treap(char _c) {
l = r = NULL;
size = 1;
color = c;
y = rand();
recalc();
}
void recalc() {
size = 1;
if (l != NULL) {
size += l->size;
l->color = color;
}
if (r != NULL) {
size += r->size;
r->color = color;
}
}
};
void recalc(treap*& a) {
if (a == NULL) return;
a->recalc();
}
void merge(treap*& t, treap* l, treap* r) {
if (l == NULL) return (void)(t = r);
if (r == NULL) return (void)(t = l);
recalc(r);
recalc(l);
if (r->y > l->y) {
recalc(l);
recalc(r->l);
merge(r->l, l, r->l);
t = r;
} else {
recalc(l->r);
recalc(r);
merge(l->r, l->r, r);
t = l;
}
recalc(t);
}
void split(treap* v, int x, treap*& l, treap*& r) {
if (v == NULL) {
r = l = NULL;
return;
}
recalc(v);
int cur = 1;
if (v->l != NULL) cur += v->l->size;
if (cur <= x) {
recalc(v->r);
split(v->r, x - cur, v->r, r);
l = v;
} else {
recalc(v->l);
split(v->l, x, l, v->l);
r = v;
}
recalc(v);
}
void dfs(treap* v) {
if (v->l != NULL) dfs(v->l);
cout << v->c << ' ';
if (v->r != NULL) dfs(v->r);
}
int main() {
string s;
cin >> s;
vector<vector<int> > ch(s.size(), vector<int>(26, 0));
vector<int> ac(26, 0);
for (int i = 0; i < s.size(); ++i) {
int e = s[i] - 'a';
ac[e]++;
}
ch[0] = ac;
for (int i = 0; i < s.size(); ++i) {
int e = s[i] - 'a';
if (i) ch[i] = ch[i - 1];
ch[i][e]--;
}
vector<char> res;
for (int i = 0; i < s.size(); ++i) {
bool bb = 1;
int e = s[i] - 'a';
for (int j = e + 1; j < 26; ++j) {
if (ch[i][j] > 0) {
bb = 0;
break;
}
}
if (bb) res.push_back(s[i]);
}
for (int i = 0; i < res.size(); ++i) {
cout << res[i];
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, k;
cin >> n >> k;
long long int a[n + 1], i, j;
vector<double> v[5001];
for (i = 1; i <= n; i++) {
cin >> a[i];
v[1].push_back(a[i]);
}
double s, avg;
for (i = 1; i <= n - 1; i++) {
s = a[i];
for (j = i + 1; j <= n; j++) {
s += a[j];
avg = (double)s / (j - i + 1);
v[j - i + 1].push_back(avg);
}
}
double ans = 0;
for (i = k; i <= 5000; i++) {
if (v[i].size() != 0)
ans = max(ans, *max_element(v[i].begin(), v[i].end()));
}
printf("%.15f", ans);
}
| 2 |
#include <bits/stdc++.h>
int month[25] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int is_leap(int year) {
if (year % 400 == 0)
return 1;
else if (year % 100 == 0)
return 0;
else if (year % 4 == 0)
return 1;
else
return 0;
}
int foo(char arr[]) {
if (strcmp(arr, "monday") == 0) return 1;
if (strcmp(arr, "tuesday") == 0) return 2;
if (strcmp(arr, "wednesday") == 0) return 3;
if (strcmp(arr, "thursday") == 0) return 4;
if (strcmp(arr, "friday") == 0) return 5;
if (strcmp(arr, "saturday") == 0) return 6;
if (strcmp(arr, "sunday") == 0) return 7;
}
char in[250];
char in1[250];
int main() {
gets(in);
gets(in1);
int x = foo(in);
int x1 = foo(in1);
int start = 2015;
int day = 4;
int find = 401;
while (find--) {
if (!is_leap(start))
month[1] = 28;
else
month[1] = 29;
int flag = 0;
for (int i = 0; i < 12; i++) {
int dos = month[i];
if (!is_leap(start)) {
if (flag && day == x1) {
printf("YES");
return 0;
}
if (flag && day != x1) flag = 0;
if (day == x) flag = 1;
}
while (dos) {
day++;
if (day == 8) day = 1;
dos--;
}
}
start++;
}
printf("NO");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int S[33][33];
long long F[33][33];
int main() {
ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
int n;
cin >> n;
vector<int> SS(1111, 1);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
S[i + 1][j] = S[i][j] + 1;
S[i][j + 1] = S[i][j] + 1;
if (SS[S[i][j]])
cout << (F[i][j] = (1ll << S[i][j])) << " ";
else
cout << (F[i][j] = 0) << " ";
SS[S[i][j]] ^= 1;
}
cout << '\n';
}
cout.flush();
int q;
cin >> q;
long long a;
while (q--) {
cin >> a;
int x = 1, y = 1;
while (x + y != 2 * n) {
cout << x << " " << y << "\n";
if (x == n) {
y++;
} else if (y == n) {
x++;
} else {
if ((a >> (S[x][y] + 1) & 1ll)) {
if (F[x + 1][y])
x++;
else
y++;
} else {
if (F[x + 1][y] == 0)
x++;
else
y++;
}
}
}
cout << n << " " << n << "\n";
cout.flush();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
long long POW(long long a, long long b, long long MMM = MOD) {
long long ret = 1;
for (; b; b >>= 1, a = (a * a) % MMM)
if (b & 1) ret = (ret * a) % MMM;
return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
if (a == 0 || b == 0) return a + b;
return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int ddx[] = {-1, -2, 1, -2, 2, -1, 2, 1}, ddy[] = {-2, -1, -2, 1, -1, 2, 1, 2};
int a[300001], b[300001], t[300001];
vector<int> vv[300001];
int tree[300005];
int n, m;
void upd(int i, int k) {
while (i <= n) {
tree[i] += k;
i += (i & -i);
}
}
int sum(int i) {
int c = 0;
while (i > 0) {
c += tree[i];
i -= (i & -i);
}
return c;
}
vector<int> Tree[1048576];
void init(int node, int S, int E) {
if (S == E) {
Tree[node].push_back(t[S]);
return;
}
init(2 * node, S, (S + E) / 2);
init(2 * node + 1, (S + E) / 2 + 1, E);
int i = 0, j = 0, n = Tree[2 * node].size(), m = Tree[2 * node + 1].size();
while (1) {
if (i == n && j == m) break;
if (i == n)
Tree[node].push_back(Tree[2 * node + 1][j++]);
else if (j == m)
Tree[node].push_back(Tree[2 * node][i++]);
else if (Tree[2 * node][i] > Tree[2 * node + 1][j])
Tree[node].push_back(Tree[2 * node + 1][j++]);
else
Tree[node].push_back(Tree[2 * node][i++]);
}
}
int find(int node, int S, int E, int i, int j, int k) {
if (i > E || j < S) return 0;
if (S == E) {
if (t[S] > k)
return 1;
else
return 0;
}
if (j >= E && i <= S)
return Tree[node].end() -
upper_bound(Tree[node].begin(), Tree[node].end(), k);
return find(2 * node, S, (S + E) / 2, i, j, k) +
find(2 * node + 1, (S + E) / 2 + 1, E, i, j, k);
}
int main() {
scanf("%d%d", &n, &m);
for (int(i) = (1); (i) <= (n); (i) += (1)) scanf("%d", a + i);
for (int(i) = (n); (i) >= (1); (i) -= (1)) {
b[i] = i - a[i];
if (b[i] >= 0) vv[b[i]].push_back(i);
}
for (int x : vv[0]) {
t[x] = x;
upd(x, 1);
}
for (int(i) = (1); (i) <= (n); (i) += (1)) {
if (b[i] <= 0) continue;
int l = 1, r = i - 1;
while (l <= r) {
int m = l + r >> 1;
if (sum(i) - sum(m - 1) >= b[i])
l = m + 1;
else
r = m - 1;
}
t[i] = r;
if (r > 0) upd(r, 1);
}
init(1, 1, n);
for (int(i) = (0); (i) <= (m - 1); (i) += (1)) {
int l, r;
scanf("%d%d", &l, &r);
l = l + 1, r = n - r;
printf("%d\n", find(1, 1, n, l, r, l - 1));
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
double dist(double x1, double y1, double x2, double y2) {
return sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
double a, b, c, x1, y1, x2, y2;
cin >> a >> b >> c;
cin >> x1 >> y1 >> x2 >> y2;
double max1 = abs(x2 - x1) + abs(y2 - y1);
double inter1x = (-c - y1 * b) / a;
double dist1x = abs(inter1x - x1);
double inter1y = (-c - x1 * a) / b;
double dist1y = abs(inter1y - y1);
double inter2x = (-c - y2 * b) / a;
double dist2x = abs(inter2x - x2);
double inter2y = (-c - x2 * a) / b;
double dist2y = abs(inter2y - y2);
double combo1x2x = dist(inter1x, y1, inter2x, y2) + dist1x + dist2x;
double combo1x2y = dist(inter1x, y1, x2, inter2y) + dist1x + dist2y;
double combo1y2x = dist(x1, inter1y, inter2x, y2) + dist1y + dist2x;
double combo1y2y = dist(x1, inter1y, x2, inter2y) + dist1y + dist2y;
cout << setprecision(8)
<< min(max1, min(min(combo1x2x, combo1x2y), min(combo1y2x, combo1y2y)))
<< "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, ans1, ans2;
char S[25][25];
int A[45][45];
int sg[45][45][45][45];
int Sg(int u, int d, int l, int r) {
if (u > d || l > r) return 0;
if (sg[u][d][l][r] != -1) return sg[u][d][l][r];
int vis[525] = {0};
for (int i = u; i <= d; i++)
for (int j = l; j <= r; j++) {
if (A[i][j] == 1) vis[Sg(u, i - 1, l, r) ^ Sg(i + 1, d, l, r)] = 1;
if (A[i][j] == 2) vis[Sg(u, d, l, j - 1) ^ Sg(u, d, j + 1, r)] = 1;
if (A[i][j] == 3)
vis[Sg(u, i - 1, l, j - 1) ^ Sg(u, i - 1, j + 1, r) ^
Sg(i + 1, d, l, j - 1) ^ Sg(i + 1, d, j + 1, r)] = 1;
}
for (int i = 0; i <= 520; i++) {
if (!vis[i]) {
sg[u][d][l][r] = i;
break;
}
}
return sg[u][d][l][r];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", S[i] + 1);
memset(A, 0, sizeof(A));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if ((i & 1) == (j & 1)) {
int a = (i + j) / 2, b = (m + 1) / 2 + (i - j) / 2;
if (S[i][j] == 'L') A[a][b] = 1;
if (S[i][j] == 'R') A[a][b] = 2;
if (S[i][j] == 'X') A[a][b] = 3;
}
}
}
memset(sg, -1, sizeof(sg));
ans1 = Sg(1, (n + m) / 2, 1, (n + m) / 2);
memset(A, 0, sizeof(A));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if ((i & 1) != (j & 1)) {
int a = (i + j) / 2, b = (m + 1) / 2 + (i - j + 1) / 2;
if (S[i][j] == 'L') A[a][b] = 1;
if (S[i][j] == 'R') A[a][b] = 2;
if (S[i][j] == 'X') A[a][b] = 3;
}
}
}
memset(sg, -1, sizeof(sg));
ans2 = Sg(1, (n + m) / 2 + 2, 1, (n + m) / 2 + 2);
if (ans1 == ans2)
puts("LOSE");
else
puts("WIN");
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a[100001], b[100001], i, j, n;
int main() {
cin >> n;
for (i = 0; i < 6 * n; i++) {
cin >> a[i];
b[a[i]]++;
}
for (i = 0; i < 6 * n; i++) {
for (j = 0; j < 6 * n; j++)
if (i / 6 != j / 6) {
++b[a[i] + 10 * a[j]];
}
}
i = 1;
while (b[i]) i++;
cout << i - 1;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, ans = 0;
scanf("%d%d", &n, &m);
while (m != n) {
if (n > m) {
ans += n - m;
m = n;
} else {
if (m % 2) ans++;
m = (m + 1) / 2;
ans++;
}
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 51;
struct Query {
int x, y, id;
inline bool operator<(const Query &rhs) const { return y < rhs.y; }
};
Query qry[MAXN];
int n, m, x, y, tp, ptr, l, r, mid, rr;
int c[MAXN], pr[MAXN], st[MAXN], res[MAXN];
inline int read() {
register int num = 0, neg = 1;
register char ch = getchar();
while (!isdigit(ch) && ch != '-') {
ch = getchar();
}
if (ch == '-') {
neg = -1;
ch = getchar();
}
while (isdigit(ch)) {
num = (num << 3) + (num << 1) + (ch - '0');
ch = getchar();
}
return num * neg;
}
inline long double slope(int u, int v) {
return 1.0L * (pr[u] - pr[v] - 1.0L * c[u] * u + 1.0L * c[v] * v) /
(c[u] - c[v]);
}
inline int calc(int x) {
int l = 1, r = tp, mid;
while (l < r) {
mid = (l + r) >> 1;
st[mid] < x ? l = mid + 1 : r = mid;
}
return l;
}
int main() {
n = read();
for (register int i = 1; i <= n; i++) {
pr[i] = pr[i - 1] + (c[i] = read());
}
m = read();
for (register int i = 1; i <= m; i++) {
x = read(), y = read(), qry[i] = (Query){x, y, i};
}
sort(qry + 1, qry + m + 1), ptr = 1;
for (register int i = 1; i <= n; i++) {
while (tp && c[st[tp]] >= c[i]) {
tp--;
}
while (tp > 1 && slope(st[tp], i) >= slope(st[tp - 1], i)) {
tp--;
}
st[++tp] = i;
while (qry[ptr].y == i && ptr <= m) {
x = qry[ptr].x, y = qry[ptr].y, l = calc(y - x), r = tp;
while (l < r) {
mid = (l + r) >> 1;
slope(st[mid], st[mid + 1]) < x - y ? r = mid : l = mid + 1;
}
l = st[l], r = qry[ptr].y,
res[qry[ptr++].id] = pr[r] - pr[l] + (x - r + l) * c[l];
}
}
for (register int i = 1; i <= m; i++) {
printf("%d\n", res[i]);
}
}
| 10 |
#include <bits/stdc++.h>
const int N = 1e6 + 10;
using namespace std;
inline int min(int a, int b) { return a < b ? a : b; }
int main() {
int a, b, c;
while (~scanf("%d%d%d", &a, &b, &c)) {
int k = min(min(a / 3, b / 2), c / 2);
a -= 3 * k;
b -= 2 * k;
c -= 2 * k;
if (a == 0 && b == 0 && c == 0) {
printf("%d\n", k * 7);
} else {
if (a == 0) {
if (b == 0 || c == 0) {
printf("%d\n", k * 7 + 1);
} else {
printf("%d\n", k * 7 + 2);
}
} else if (b == 0) {
if (c == 0) {
if (a > 2) {
printf("%d\n", k * 7 + 2);
} else {
printf("%d\n", k * 7 + 1);
}
} else {
if (c >= 2) {
printf("%d\n", k * 7 + 3);
} else {
printf("%d\n", k * 7 + 2);
}
}
} else if (c == 0) {
if (a >= 2 && b >= 2) {
printf("%d\n", k * 7 + 4);
} else if (b < 2 && a >= 2) {
printf("%d\n", k * 7 + 3);
} else if (a < 2) {
printf("%d\n", k * 7 + 2);
}
} else {
int kk = min(min(a, b), c), sum = 0;
if (kk == a) {
if (a == 1) {
if (c >= 2) {
if (b >= 2) {
b = 2;
}
printf("%d\n", k * 7 + a + b + 2);
} else {
printf("%d\n", k * 7 + 3);
}
} else {
printf("%d\n", k * 7 + 6);
}
} else {
if (b == 1) {
if (c >= 2) {
if (a >= 3) {
a = 3;
}
printf("%d\n", k * 7 + a + 3);
} else if (c == 1) {
if (a >= 3) {
a = 3;
}
printf("%d\n", k * 7 + a + 2);
}
} else if (c == 1) {
if (b >= 2) {
if (a >= 3) {
a = 3;
}
printf("%d\n", k * 7 + a + 3);
} else if (b == 1) {
if (a >= 3) {
a = 3;
}
printf("%d\n", k * 7 + a + 2);
}
}
}
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[6][6];
for (int i = 0; i < 4; i++) {
scanf("%s", a[i]);
}
bool flag = false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int s = 0;
for (int ii = 0; ii < 2; ii++) {
for (int jj = 0; jj < 2; jj++) {
if (a[i][j] == a[i + ii][j + jj]) {
s++;
}
}
}
if (s <= 1 || s >= 3) {
flag = true;
break;
}
}
if (flag) {
break;
}
}
printf("%s\n", flag ? "YES" : "NO");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int b[100100][11][10][5];
char s[100100];
char nab[] = {
'A',
'G',
'T',
'C',
};
void update(int u, int id, int r, int op, int x) {
for (; u < 100100; u += u & -u) b[u][id][r][op] += x;
}
int calc(int u, int id, int r, int op, int ans = 0) {
for (; u; u -= u & -u) ans += b[u][id][r][op];
return ans;
}
char e[20];
int main() {
cin >> s;
int n = strlen(s);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 10; j++) {
int r = i % j;
if (r == 0) r = j;
for (int k = 0; k < 4; k++) {
if (nab[k] == s[i - 1]) {
update((i + j - 1) / j, j, r, k, 1);
}
}
}
}
int q;
cin >> q;
while (q--) {
int t;
scanf("%d", &t);
if (t == 1) {
int x;
char c;
scanf("%d %c", &x, &c);
for (int i = 1; i <= 10; i++) {
int r = x % i;
if (r == 0) r = i;
for (int k = 0; k < 4; k++) {
if (nab[k] == s[x - 1]) {
update((x + i - 1) / i, i, r, k, -1);
}
}
}
s[x - 1] = c;
for (int i = 1; i <= 10; i++) {
int r = x % i;
if (r == 0) r = i;
for (int k = 0; k < 4; k++) {
if (nab[k] == s[x - 1]) {
update((x + i - 1) / i, i, r, k, 1);
}
}
}
} else {
int l, r;
scanf("%d %d %s", &l, &r, e);
int len = strlen(e), ans = 0;
for (int i = 1; i <= len; i++) {
int rr = (i + l - 1) % len;
if (rr == 0) rr = len;
int L = (i + l - 1 + len - 1) / len;
int R = (r + len - 1) / len;
int p = r % len;
if (p == 0) p = len;
if (p < rr) R--;
if (L > R) continue;
for (int k = 0; k < 4; k++) {
if (nab[k] == e[i - 1]) {
ans += calc(R, len, rr, k) - calc(L - 1, len, rr, k);
}
}
}
printf("%d\n", ans);
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
if (n < 3) {
cout << "-1\n";
return 0;
}
for (int i = 0; i < n - 3; i++) {
for (int j = 1; j <= n; j++) {
if ((n % 2 && !(i % 2)) || (!(n % 2) && i % 2))
cout << j + i * n << " ";
else
cout << (i + 1) * n - j + 1 << " ";
}
cout << "\n";
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < n - 3; j++) {
cout << n * n - 9 - i * (n - 3) - j << " ";
}
if (i == 0)
cout << 1 + n * n - 9 << " " << 7 + n * n - 9 << " " << 9 + n * n - 9
<< "\n";
if (i == 1)
cout << 3 + n * n - 9 << " " << 2 + n * n - 9 << " " << 5 + n * n - 9
<< "\n";
if (i == 2)
cout << 4 + n * n - 9 << " " << 8 + n * n - 9 << " " << 6 + n * n - 9
<< "\n";
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
size_t pos = 0;
while (pos < s.length() && s[pos] != 'h') ++pos;
++pos;
while (pos < s.length() && s[pos] != 'e') ++pos;
++pos;
while (pos < s.length() && s[pos] != 'l') ++pos;
++pos;
while (pos < s.length() && s[pos] != 'l') ++pos;
++pos;
while (pos < s.length() && s[pos] != 'o') ++pos;
if (pos < s.length()) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.