solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
const int inf = 1e9 + 1;
int n, m, k;
vector<int> v[N];
int p[N];
int found[N]{0};
void bfs(int u) {
found[u] = 1;
for (int i : v[u]) {
if (!found[i]) {
p[i] = u;
if (u == 1) k--;
bfs(i);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
{
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
if (v[1].size() < k) {
cout << ("NO") << '\n';
return 0;
}
bfs(1);
if (k < 0) {
cout << ("NO") << '\n';
return 0;
}
cout << ("YES") << '\n';
for (int i : v[1]) {
if (k && p[i] != 1) {
p[i] = 1;
k--;
}
}
for (int i = 2; i <= n; i++) {
cout << (p[i]) << " " << (i) << '\n';
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y, z, a, b, c, flag = 0;
cin >> x >> y >> z >> a >> b >> c;
a -= x;
if (a < 0) {
flag = 1;
}
if (a + b < y || a + b + c < y + z) {
flag = 1;
}
if (flag == 1) {
cout << "NO";
return 0;
}
cout << "YES";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int num = 0;
int buf;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> buf;
if (buf) {
num = 1;
break;
} else {
num = 0;
}
}
if (num) {
cout << "HARD";
} else {
cout << "EASY";
}
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 998244353;
long long perm[4010][4010];
void mult(long long& a, long long b) {
a *= b;
a %= MOD;
}
void initfact() {
perm[0][0] = 1;
for (int i = 1; i <= 4000; ++i) {
perm[i][i] = 1;
for (int j = i - 1; j >= 1; --j) {
perm[i][j] = perm[i][j + 1] * (j + 1);
perm[i][j] %= MOD;
}
perm[i][0] = perm[i][1];
}
}
int markr[4010], markc[4010];
long long Wr[2010][4010], Wc[2010][4010];
void getways(int R, long long W[][4010], int mark[4010]) {
for (int i = 0; i <= R; ++i) W[0][i] = 1;
for (int n = 1; n <= R / 2; ++n) {
for (int i = 1; i <= R; ++i) {
W[n][i] = W[n][i - 1];
if (!mark[i] && !mark[i - 1]) W[n][i] += W[n - 1][i - 2];
W[n][i] %= MOD;
}
}
}
void solve() {
int R, C, H;
cin >> R >> C >> H;
int r = 0, c = 0;
for (int i = 0; i < H; ++i) {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
markr[r1] = 1;
markr[r2] = 1;
markc[c1] = 1;
markc[c2] = 1;
r++;
c++;
if (r1 != r2) ++r;
if (c1 != c2) ++c;
}
initfact();
markr[0] = markc[0] = 1;
getways(R, Wr, markr);
getways(C, Wc, markc);
long long ans = 0;
for (int n = 0; n <= R / 2; ++n) {
for (int m = 0; m <= C / 2; ++m) {
int RR = R - 2 * n - r;
int CC = C - 2 * m - c;
if (RR < m) continue;
if (CC < n) continue;
long long cur = 1;
mult(cur, Wr[n][R]);
mult(cur, Wc[m][C]);
mult(cur, perm[RR][RR - m]);
mult(cur, perm[CC][CC - n]);
ans = (ans + cur) % MOD;
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int process(list<pair<int, int>> t0, list<pair<int, int>> t1, int x) {
auto result = 0;
while (!t0.empty() && !t1.empty()) {
auto max = 0;
auto max_it = t0.end();
for (auto it = t0.begin(); it != t0.end(); it++) {
if ((*it).first <= x && (*it).second > max) {
max_it = it;
max = (*it).second;
}
}
if (max_it != t0.end()) {
x += max;
t0.erase(max_it);
swap(t0, t1);
result++;
} else {
break;
}
}
if (!t0.empty()) {
for (auto& i : t0) {
if (i.first <= x) {
result++;
break;
}
}
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
int n, x;
cin >> n >> x;
list<pair<int, int>> t0, t1;
for (int t, h, m; cin >> t >> h >> m;) {
if (t == 1) {
t1.emplace_front(h, m);
} else {
t0.emplace_front(h, m);
}
}
cout << max(process(t0, t1, x), process(t1, t0, x));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using pii = pair<int, int>;
using PLL = pair<LL, LL>;
using UI = unsigned int;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int N = 3e5 + 10;
int T, n, a[N], pre[N], val[N], ans[N];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = (1); i < (n + 1); ++i) {
pre[i] = 0;
val[i] = -1;
ans[i] = inf;
}
for (int i = (1); i < (n + 1); ++i) {
scanf("%d", a + i);
val[a[i]] = max(val[a[i]], i - pre[a[i]]);
pre[a[i]] = i;
}
for (int i = (1); i < (n + 1); ++i) {
val[a[i]] = max(val[a[i]], n + 1 - pre[a[i]]);
}
for (int i = (1); i < (n + 1); ++i) {
if (val[i] != -1) {
ans[val[i]] = min(ans[val[i]], i);
}
}
ans[0] = inf;
for (int i = (1); i < (n + 1); ++i) {
ans[i] = min(ans[i], ans[i - 1]);
if (ans[i] == inf)
printf("%d%c", -1, i == n ? '\n' : ' ');
else
printf("%d%c", ans[i], i == n ? '\n' : ' ');
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
int n, c0, c1, h;
string s;
while (t != 0) {
cin >> n >> c0 >> c1 >> h >> s;
int maxC = max(c0, c1), minC = min(c0, c1);
int value = minC * n;
int cnt = 0;
for (int i = 0; i < n; i++)
if (s[i] == '1') cnt++;
if (maxC - minC > h) {
if (maxC == c1)
value += h * cnt;
else
value += h * (n - cnt);
} else {
if (maxC == c1)
value += (maxC - minC) * cnt;
else
value += (maxC - minC) * (n - cnt);
}
cout << value << endl;
t--;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int X, Y, n, m, cnt;
char s[1005];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= m; j++)
if (s[j] == 'B') cnt++, X += i, Y += j;
}
X /= cnt;
Y /= cnt;
printf("%d %d\n", X, Y);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const double pi = acos(-1);
const long long inf = 2e18;
const int mod = 1e9 + 9;
struct point {
int x, flag;
bool operator<(const point &a) const { return flag > a.flag; }
} s, q;
long long l[maxn], r[maxn], pos;
unsigned long long pre[maxn];
int main() {
int n, cnt = 0, m;
long long a[maxn], b[maxn];
map<long long, int> mp;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 1; i <= n; i++) {
if (!mp[a[i]]) {
a[++cnt] = a[i];
mp[a[i]] = 1;
}
}
sort(a + 1, a + 1 + cnt);
for (int i = 1; i < cnt; i++) b[i] = a[i + 1] - a[i];
b[cnt] = inf;
sort(b + 1, b + 1 + cnt);
for (int i = 1; i <= cnt; i++) pre[i] = pre[i - 1] + b[i];
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%lld %lld", &l[i], &r[i]);
for (int i = 1; i <= m; i++) {
pos = r[i] - l[i] + 1;
unsigned long long ans = pos * cnt;
int tot = lower_bound(b + 1, b + 1 + cnt, pos) - b;
tot--;
ans -= (tot * pos - pre[tot]);
if (i == m)
printf("%llu\n", ans);
else
printf("%llu ", ans);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int t, n, k;
long long dp[80][80];
vector<pair<pair<int, int>, int> > v;
long long solve(int i, int rem) {
if (i == n) {
if (rem) return -1e15;
return 0;
}
long long &ans = dp[i][rem];
if (ans != -1) return ans;
if (rem)
ans = solve(i + 1, rem - 1) + (k - rem) * v[i].first.first +
v[i].first.second;
ans = max(ans, solve(i + 1, rem) + v[i].first.first * (k - 1));
return ans;
}
vector<int> v1, v2;
void build(int i, int rem) {
if (i == n) return;
long long ans1 =
solve(i + 1, rem - 1) + (k - rem) * v[i].first.first + v[i].first.second;
long long ans2 = solve(i + 1, rem) + v[i].first.first * (k - 1);
if (ans1 > ans2) {
v1.push_back(v[i].second);
build(i + 1, rem - 1);
return;
}
v2.push_back(v[i].second);
build(i + 1, rem);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> k;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back({{y, x}, i + 1});
}
sort(v.begin(), v.end());
memset(dp, -1, sizeof dp);
solve(0, k);
build(0, k);
cout << v1.size() + v2.size() * 2 << "\n";
v2.push_back(v1.back());
v1.pop_back();
for (auto i : v1) cout << i << " ";
for (int i = 0; i < v2.size(); i++) {
if (i == v2.size() - 1)
cout << v2[i];
else
cout << v2[i] << " " << -v2[i] << " ";
}
cout << "\n";
v.clear();
v1.clear();
v2.clear();
}
}
| 8 |
#include <bits/stdc++.h>
template <typename T>
bool Chkmax(T &x, T y) {
return x < y ? x = y, true : false;
}
template <typename T>
bool Chkmin(T &x, T y) {
return y < x ? x = y, true : false;
}
template <typename T>
void Readin(T &x) {
x = 0;
int c = getchar(), k = 1;
for (; '0' > c || c > '9'; c = getchar())
if (c == '-') k = -1;
for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= k;
}
const int MAXN = 1e5 + 1e3, MAXM = MAXN, oo = 0x3f3f3f3f;
int n, m;
int a[MAXN], b[MAXM], g[MAXN], ans[MAXN];
int last[MAXN], pos[MAXN];
int best[MAXN];
int used[MAXM];
std::vector<int> s[MAXN];
void Print() {
int p = 0, lastans = -1;
for (int i = 1, _ = n; i <= _; i++)
if (best[i] > best[p]) p = i;
for (int i = n, _ = 1; i >= _; i--) {
if (a[i] != -1) {
ans[i] = a[i];
if (p == i) {
lastans = ans[i], p = last[i];
best[p] = best[i] - 1;
}
} else {
if (p != i)
ans[i] = -1;
else {
if (best[i] == 1) {
ans[i] = b[1];
used[1] = true;
p = 0;
continue;
}
int j, p0;
const std::vector<int> &tmp = s[best[i] - 1];
for (j = 0; j < tmp.size(); j++) {
if (tmp[j] >= i) continue;
p0 = std::upper_bound(b + 1, b + m + 1, a[tmp[j]]) - b;
if (p0 != m + 1 && (lastans == -1 || b[p0] < lastans)) {
ans[i] = b[p0];
used[p0] = true;
break;
}
}
if (j != tmp.size()) {
lastans = ans[i], p = tmp[j];
continue;
}
p0 = lastans == -1
? m
: (std::lower_bound(b + 1, b + m + 1, lastans) - b - 1);
ans[i] = b[p0];
used[p0] = true;
lastans = ans[i];
do --p;
while (a[p] != -1);
best[p] = best[i] - 1;
}
}
}
p = 1;
for (int i = 1, _ = n; i <= _; i++)
if (ans[i] == -1) {
while (used[p]) p++;
ans[i] = b[p];
used[p] = true;
}
for (int i = 1, _ = n; i <= _; i++) printf(i == n ? "%d\n" : "%d ", ans[i]);
}
void Solve() {
g[0] = 0;
for (int i = 1, _ = n; i <= _; i++) g[i] = oo;
for (int i = 1, _ = n; i <= _; i++) {
if (a[i] != -1) {
int p = std::lower_bound(g, g + n + 1, a[i]) - g;
last[i] = pos[p - 1];
g[p] = a[i];
pos[p] = i;
best[i] = p;
s[p].push_back(i);
} else {
int p = n;
while (g[p - 1] >= b[m]) p--;
best[i] = p;
for (int j = m, _ = 1; j >= _; j--) {
while (g[p - 1] >= b[j]) p--;
g[p] = b[j];
pos[p] = i;
}
}
}
}
void Init() {
Readin(n);
for (int i = 1, _ = n; i <= _; i++) Readin(a[i]);
Readin(m);
for (int i = 1, _ = m; i <= _; i++) Readin(b[i]);
std::sort(b + 1, b + m + 1);
}
int main() {
if (fopen("E.in", "r") != NULL) {
freopen("E.in", "r", stdin);
freopen("E.out", "w", stdout);
}
Init();
Solve();
Print();
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9')
s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * w;
}
struct edge {
int u, v, w;
} E[200010];
int n;
vector<pair<int, int> > e[200010];
vector<int> ans[3];
int dfs(int u, int ff, int w) {
if (u == w) return -1;
if (e[u].size() == 1) return u;
int val = n;
for (auto v : e[u])
if (v.first != ff) val = min(val, dfs(v.first, u, w));
return val;
}
void solve(int u, int v, int w) {
if (e[u].size() == 1 && e[v].size() == 1)
return ans[0].push_back(u), ans[1].push_back(v), ans[2].push_back(w),
void();
if (e[u].size() < e[v].size()) swap(u, v);
int id1 = -1, id2 = -1;
for (int i = 0; i < e[u].size(); i++)
if (e[u][i].first != v) {
int val = dfs(e[u][i].first, u, v);
if (val != -1) id2 = id1, id1 = val;
}
if (e[v].size() == 1) {
ans[0].push_back(id1), ans[1].push_back(v), ans[2].push_back(w / 2);
ans[0].push_back(id2), ans[1].push_back(v), ans[2].push_back(w / 2);
ans[0].push_back(id1), ans[1].push_back(id2), ans[2].push_back(-w / 2);
} else
solve(id1, v, w), solve(id1, u, -w);
}
int main() {
n = read();
for (int i = 1; i < n; i++) {
int u = read(), v = read(), w = read();
e[u].push_back(make_pair(v, w)), e[v].push_back(make_pair(u, w));
E[i].u = u, E[i].v = v, E[i].w = w;
}
for (int i = 1; i <= n; i++)
if (e[i].size() == 2 && e[i][0].second != e[i][1].second)
return puts("NO"), 0;
for (int i = 1; i < n; i++) solve(E[i].u, E[i].v, E[i].w);
puts("YES");
printf("%d\n", ans[0].size());
for (int i = 0; i < ans[0].size(); i++)
printf("%d %d %d\n", ans[0][i], ans[1][i], ans[2][i]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
long long qpow(long long a, long long b) {
long long ans = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
long long gcd(long long a, long long b) { return b > 0 ? gcd(b, a % b) : a; }
int n, m, T;
int id[maxn];
vector<int> g[maxn];
bool vis[10][10];
int gao() {
int x = 0;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) {
for (auto v : g[i]) {
if (vis[id[i]][id[v]]) continue;
vis[id[i]][id[v]] = vis[id[v]][id[i]] = 1;
x++;
}
}
return x;
}
int cal(int t) {
int res = 0, num = 0;
for (int i = 1; i <= n; i++) {
if (i != t) id[i] = ++num;
}
for (int i = 1; i <= 6; i++) {
id[t] = i;
res = max(res, gao());
}
return res;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
int ans = 0;
if (n <= 6) {
ans = m;
} else {
for (int i = 1; i <= n; i++) {
ans = max(ans, cal(i));
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using str = string;
using ll = long long;
using ld = long double;
template <typename T>
using vec = vector<T>;
template <typename A, typename B>
using pr = pair<A, B>;
struct Solver {
void run() {
ll n;
cin >> n;
ll count = 1;
for (int i = 3; i <= 2 * n; i++) count = (count * i) % 1000000007;
cout << count << '\n';
}
};
void boost() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
boost();
int t = 1;
cin >> t;
while (t--) {
Solver s;
s.run();
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool sum_bits(int x, int y, int k) {
int cnt = 0;
int q = x | y;
while (q) {
if ((x & 1) != (y & 1)) {
cnt += 1;
}
x = x >> 1;
y = y >> 1;
q = q >> 1;
}
if (cnt > k) {
return 0;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
long long a, b, c, m, n;
cin >> n;
long long arr[n];
long long sm = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
sm += arr[i];
}
sort(arr, arr + n);
cin >> m;
for (int i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
long long ans = 0;
auto it = lower_bound(arr, arr + n, x);
if (it == arr + n) {
sm -= arr[n - 1];
ans += (x - arr[n - 1]);
if (sm < y) {
ans += (y - sm);
}
cout << ans << endl;
sm += arr[n - 1];
} else {
sm -= *(it);
if (sm < y) {
ans += (y - sm);
}
sm += *(it);
long long ans2 = 0;
if (it != arr) {
sm -= *(it - 1);
ans2 += (x - *(it - 1));
if (sm < y) {
ans2 += (y - sm);
}
ans = min(ans, ans2);
sm += *(it - 1);
}
cout << ans << endl;
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int n;
cin >> n;
int l = n;
if (n % 2)
l = l / 2;
else
l = l / 2 - 1;
for (int i = l; i >= 1; i--) {
int p = n - i;
if (gcd(i, p) == 1) {
cout << i << " " << p;
break;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-13;
int T;
struct point {
double x, y;
} A, B, C, D, K, L, M, O;
inline point operator+(const point &a, const point &b) {
point ret;
ret.x = a.x + b.x;
ret.y = a.y + b.y;
return ret;
}
inline point operator-(const point &a, const point &b) {
point ret;
ret.x = a.x - b.x;
ret.y = a.y - b.y;
return ret;
}
inline double operator*(const point &a, const point &b) {
return a.x * b.x + a.y * b.y;
}
inline point operator*(const point &a, const double b) {
point ret;
ret.x = a.x * b;
ret.y = a.y * b;
return ret;
}
inline point operator/(const point &a, const double b) { return a * (1 / b); }
inline int cross(const point &a, const point &b) {
double ret = a.x * b.y - b.x * a.y;
if (fabs(ret) < eps) return 0;
if (ret > 0)
return 1;
else
return -1;
}
bool check(point L, point K, point M) {
if (cross(K - L, L - M) == 0) return false;
point sM = L + L - M, sK = K;
point E = (L + sK) / 2, F = (L + sM) / 2, P = L - sK, Q = L - sM, U;
double u = E * P, v = F * Q;
if (fabs(Q.x) > eps) {
double k = P.x / Q.x;
P = P - Q * k;
u -= k * v;
swap(P, Q);
swap(u, v);
swap(sK, sM);
}
U.y = v / Q.y;
if (fabs(P.x) > eps)
U.x = (u - P.y * U.y) / P.x;
else
Q = L - sM, v = F * Q, U.x = (v - Q.y * U.y) / Q.x;
B = U;
C = L + L - B;
A = K + K - B;
D = M + M - C;
if (cross(B - A, C - B) != cross(C - B, D - C) ||
cross(C - B, D - C) != cross(D - C, A - D) ||
cross(D - C, A - D) != cross(A - D, B - A))
return false;
return true;
}
int main() {
for (scanf("%d", &T); T--;) {
scanf("%lf%lf%lf%lf%lf%lf", &K.x, &K.y, &L.x, &L.y, &M.x, &M.y);
if (check(L, K, M) || check(K, L, M) || check(M, K, L))
printf("YES\n%.9lf %.9lf %.9lf %.9lf %.9lf %.9lf %.9lf %.9lf\n", A.x, A.y,
B.x, B.y, C.x, C.y, D.x, D.y);
else
printf("NO\n\n");
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long i, j = 0, k, l, x, y, z, p, q, r, c = 0;
char arr[27];
bool c26 = false, m26 = false, f = false, fc = false;
set<long long> s;
map<long long, long long> m1, m2;
vector<long long> v, v1;
string str;
cin >> str;
long long sl = str.length();
if (sl < 26) {
cout << -1;
return 0;
}
for (i = 1; i < 27; i++) arr[i] = i + 64;
for (i = 0; i < sl; i++) {
m1.clear();
v1.clear();
for (j = i, c = 0; j < i + 26 && i + 26 <= sl; j++) {
if (str[j] == '?') {
v1.push_back(j);
c++;
if (c == 26) {
c26 = true;
break;
}
continue;
}
if (m1[str[j]] >= 1) {
m1.clear();
break;
}
m1[str[j]]++;
}
if (c26)
break;
else if (m1.size() == 26) {
f = true;
break;
} else if (m1.size() + c == 26) {
fc = true;
break;
}
}
if (!c26 && !f && !fc) {
cout << -1;
return 0;
} else if (c26) {
for (i = 0, j = 1; i < v1.size() && j < 27; j++, i++) {
str[v1[i]] = arr[j];
}
} else if (fc) {
for (i = 0; i < v1.size(); i++) {
for (j = 1; j < 27; j++) {
if (!m1[arr[j]]) {
str[v1[i]] = arr[j];
m1[arr[j]]++;
break;
}
}
}
}
for (i = 0, j = 1; i < sl; i++) {
if (str[i] == '?') {
if (j == 27) j = 1;
str[i] = arr[j];
j++;
}
}
cout << str;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long dp1[100100][2], dp2[100100][2];
long long num[100100];
inline long long max(long long a, long long b) { return a > b ? a : b; }
int main() {
int n;
scanf("%d", &n);
for (int i = 2; i <= n; i++) scanf("%I64d", &num[i]);
dp1[1][0] = dp1[1][1] = 0;
for (int i = 2; i <= n; i++) {
if (num[i] > 1)
dp1[i][1] = dp1[i - 1][1] + num[i] / 2 * 2;
else
dp1[i][1] = 0;
if (num[i] % 2)
dp1[i][0] = dp1[i - 1][0] + num[i];
else
dp1[i][0] = max(dp1[i - 1][0] + num[i] - 1, dp1[i][1]);
}
dp2[n][0] = dp2[n][1] = 0;
for (int i = n - 1; i >= 1; i--) {
int j = i + 1;
if (num[j] > 1)
dp2[i][1] = dp2[j][1] + num[j] / 2 * 2;
else
dp2[i][1] = 0;
if (num[j] % 2)
dp2[i][0] = dp2[j][0] + num[j];
else
dp2[i][0] = max(dp2[i][1], dp2[j][0] + num[j] - 1);
}
long long ans = 0;
for (int i = 1; i <= n; i++)
ans = max(dp1[i][0] + dp2[i][1], max(ans, dp1[i][1] + dp2[i][0]));
printf("%I64d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N(707);
int dp[N][2], dp1[N][2];
int a[N], pw[N];
int mod(1e9 + 7);
void add(int& x, int y) {
if ((x += y) >= mod) x -= mod;
}
int main() {
string st;
cin >> st;
int n = st.size();
for (int i(0); i < n; i++) a[i + 1] = st[i] - '0';
pw[0] = 1;
for (int i(1); i <= n; i++) pw[i] = 10ll * pw[i - 1] % mod;
int ans = 0;
for (int u(0); u <= 8; u++) {
memset(dp, 0, sizeof(dp));
dp[0][1] = 1;
for (int i(1); i <= n; i++) {
memset(dp1, 0, sizeof(dp1));
for (int j(0); j < i; j++) {
for (int k(0); k <= 9; k++) {
add(dp1[j + (k <= u)][0], dp[j][0]);
if (k == a[i]) {
add(dp1[j + (k <= u)][1], dp[j][1]);
} else if (k < a[i]) {
add(dp1[j + (k <= u)][0], dp[j][1]);
}
}
}
memcpy(dp, dp1, sizeof(dp));
}
int sum((dp[0][0] + dp[0][1]) % mod);
for (int i(1); i <= n; i++) {
ans = (ans + (long long)sum * pw[n - i]) % mod;
sum = (sum + (long long)dp[i][0] + dp[i][1]) % mod;
}
}
cout << ans << endl;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int cur, num, msk;
bool adj[250][250];
void dfs(int nd, int dep, int sep) {
if (dep > 30) return;
adj[nd][cur] = true;
adj[cur][nd] = true;
adj[nd][cur + 1] = true;
adj[cur + 1][nd] = true;
adj[cur][cur + 2] = true;
adj[cur + 2][cur] = true;
adj[cur + 1][cur + 2] = true;
adj[cur + 2][cur + 1] = true;
if (msk & (1 << dep)) {
adj[nd][sep] = true;
adj[sep][nd] = true;
}
nd = cur + 2;
cur += 3;
dfs(nd, dep + 1, sep + 2);
}
int main() {
scanf("%d", &num);
cur = 3;
for (int i = 0; i < (int)100; i++) {
adj[cur][cur + 1] = true;
adj[cur + 1][cur] = true;
cur++;
}
adj[cur][2] = true;
adj[2][cur] = true;
cur++;
for (int i = 30; i >= 0; i--) {
if (num < (1 << i)) continue;
num -= (1 << i);
msk |= (1 << i);
}
dfs(1, 0, 3);
printf("249\n");
for (int i = 1; i < (int)250; i++) {
for (int j = 1; j < (int)250; j++) printf("%c", adj[i][j] ? 'Y' : 'N');
puts("");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N, M;
cin >> N >> M;
if (N > M) swap(N, M);
ll rem = 0;
if (N == 1) {
rem = min(M % 6, 6 - M % 6);
} else {
if (N % 2 == 1 && M % 2 == 1) {
rem = 1;
} else {
if (M == 2) {
rem = 4;
} else if (M == 3 || M == 7) {
rem = 2;
} else {
rem = 0;
}
}
}
cout << N * M - rem << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int dx8[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy8[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};
int X[6] = {0, 0, 0, -1, 0, 1};
int Y[6] = {0, 0, -1, 0, 1, 0};
int Z[6] = {1, -1, 0, 0, 0, 0};
int main() {
string str;
char s[100005];
scanf("%s", s);
str = s;
int n = 0;
for (int i = 0; i < str.size(); i++) {
n *= 10;
n += (str[i] - '0');
n %= 4;
}
if (n)
puts("0");
else
puts("4");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int freq[N];
int sum[N];
int getsum(int l, int r) {
int ret = sum[r];
if (l) ret -= sum[l - 1];
return ret;
}
void fill1(vector<int> &ans, int element) {
if (element < 0 || freq[element] == 0) return;
if (freq[element] == 1) {
ans.push_back(element);
return;
}
for (int i = 1; i <= freq[element] - 1; i++) ans.push_back(element);
fill1(ans, element - 1);
ans.push_back(element);
}
void fill2(vector<int> &ans, int element) {
if (element >= N || freq[element] == 0) return;
if (freq[element] == 1) {
ans.push_back(element);
return;
}
for (int i = 1; i <= freq[element] - 1; i++) ans.push_back(element);
fill2(ans, element + 1);
ans.push_back(element);
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int val;
cin >> val;
freq[val]++;
}
set<int> pos;
for (int i = 0; i < N; i++) {
sum[i] = freq[i];
if (i) sum[i] += sum[i - 1];
if (freq[i] <= 1) pos.insert(i);
}
int ans = 0, e1, e2, solo = -1;
for (int i = 0; i < N - 1; i++) {
int element1 = i, element2 = i + 1;
if (freq[element1] == 0 || freq[element2] == 0) {
if (freq[element1] > ans) {
solo = element1;
ans = freq[element1];
} else if (freq[element2] > ans) {
solo = element2;
ans = freq[element2];
}
continue;
}
int up = *(pos.upper_bound(element2 - 1));
set<int>::iterator it = pos.lower_bound(element1 + 1);
int down;
if (it == pos.end())
down = element1;
else {
it--;
down = (*it);
}
int sol = getsum(element2, up) + getsum(down, element1);
if (sol > ans) {
e1 = element1, e2 = element2, solo = -1, ans = sol;
}
}
cout << ans << '\n';
vector<int> out;
if (solo != -1) {
for (int i = 0; i < freq[solo]; i++) out.push_back(solo);
} else {
fill1(out, e1);
fill2(out, e2);
}
for (auto &x : out) cout << x << ' ';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, fa, f[300005], tot = 0;
vector<int> vec[300005];
int dfs(int cur) {
if ((int)vec[cur].size() == 0) {
tot++;
return 1;
}
if (f[cur]) {
int res = 0x3f3f3f3f;
for (int i = 0; i < vec[cur].size(); i++) {
res = min(res, dfs(vec[cur][i]));
}
return res;
} else {
int res = 0;
for (int i = 0; i < vec[cur].size(); i++) {
res += dfs(vec[cur][i]);
}
return res;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &f[i]);
for (int i = 2; i <= n; i++) {
scanf("%d", &fa);
vec[fa].push_back(i);
}
int k = dfs(1);
printf("%d\n", tot - k + 1);
}
| 5 |
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
const double sqrt3 = sqrt(3.0);
ll f(double r, ll x) {
ll res = 0L;
double h = 3.0 * x / 2 + 1;
if (r + 0.01 >= h) {
double d = 2 * sqrt(r * r - h * h);
res = d / sqrt3;
if ((x % 2 == 0 && res % 2 == 0) || (x % 2 == 1 && res % 2 == 1)) {
res++;
}
}
return res;
}
ll solution(int r) {
ll x = (r - sqrt3 / 2.0) / sqrt3;
ll result = x * (x + 1) / 2;
ll res = 0L;
do {
++x;
res = f(r, x);
result += res;
} while (res > 0);
return result;
}
int main() {
ios::sync_with_stdio(false);
cout.precision(10);
int r;
while (cin >> r) {
cout << 1 + 6 * solution(r) << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long unsigned int n, sol = 0;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) sol++;
}
cout << sol;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n;
string sl,sr;
bitset<N> l,r,d,d2,pro,ans;
inline bool operator<(bitset<N> &a,bitset<N> &b)
{
register int i;
for(i=n;i>=0;i--)
if(a[i]^b[i])
return a[i]<b[i];
return 0;
}
inline void add(bitset<N> &a)
{
register int i,j;
for(i=0;i<=n;i++)
if(!a[i])
{
for(j=0;j<i;j++)
a[j]=0;
a[i]=1;
return;
}
}
inline void sub(bitset<N> &a)
{
register int i,j;
for(i=0;i<=n;i++)
if(a[i])
{
for(j=0;j<i;j++)
a[j]=1;
a[i]=0;
return;
}
}
inline void run()
{
register int i;
d.reset();
for(d=l;!(r<d);add(d))
{
pro.reset();
for(d2=d;!(r<d2);add(d2))
{
pro^=d2;
if(ans<pro)
ans=pro;
}
}
for(i=n-1;i>=0;i--)
cout<<ans[i];
cout<<endl;
exit(0);
}
signed main()
{
int u,t=0;
register int i;
ios::sync_with_stdio(0);
cin>>n>>sl>>sr;
for(i=0;i<n;i++)
l[i]=sl[n-1-i]-'0',r[i]=sr[n-1-i]-'0';
if(r[n-1]!=l[n-1])
{
for(i=0;i<n;i++)
cout<<1;
cout<<endl;
return 0;
}
for(i=0;i<n;i++)
{
u=r[i]+(l[i]^1)+t;
d[i]=u&1;t=u>>1;
}
add(d);add(d);
for(i=3;i<n;i++)
if(d[i])
goto end1;
run();
end1:;
if(r[0]==0)
{
r[0]=1;
for(i=n-1;i>=0;i--)
cout<<r[i];
cout<<endl;
return 0;
}
else
{
for(i=n-1;i>=0;i--)
cout<<r[i];
cout<<endl;
return 0;
}
return 0;
} | 9 |
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<int> v(N);
vector<int> place(N + 1);
for (int i = 0; i < N; i++) {
cin >> v[i];
place[v[i]] = i;
}
vector<int> bob(N + 1, 0);
bob[N] = 1;
for (int i = N - 1; i >= 1; i--) {
int box = 0;
for (int j = place[i]; j < N; j += i) {
box |= bob[v[j]];
}
for (int j = place[i]; j >= 0; j -= i) {
box |= bob[v[j]];
}
bob[i] = box ^ 1;
}
for (int i = 0; i < N; i++) {
if (bob[v[i]])
cout << 'B';
else
cout << 'A';
}
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int s, n;
cin >> s >> n;
vector<pair<int, int>> v;
int arr1[n];
int arr2[n];
for (int i = 0; i < n; i++) {
cin >> arr1[i] >> arr2[i];
}
for (int i = 0; i < n; i++) {
v.push_back(make_pair(arr1[i], arr2[i]));
}
sort(v.begin(), v.end());
bool res = true;
for (int i = 0; i < n; i++) {
if (s > v[i].first) {
s += v[i].second;
} else {
res = false;
break;
}
}
if (res) {
cout << "YES\n";
} else {
cout << "NO\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -w;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = (s << 1) + (s << 3) + (ch ^ '0');
ch = getchar();
}
return s * w;
}
long long n, m, le = 1, ri = 0, ans = 0;
long long a[100005], dp[100005][25], v[100005];
void add(long long x) { ans += v[x], v[x]++; }
void del(long long x) { v[x]--, ans -= v[x]; }
void calc(long long l, long long r) {
while (le < l) del(a[le]), le++;
while (le > l) le--, add(a[le]);
while (ri < r) ri++, add(a[ri]);
while (ri > r) del(a[ri]), ri--;
}
void search_for_dream(long long l, long long r, long long L, long long R,
long long rt) {
if (l > r) return;
long long minv = 200000000000000007, pos = L, cnt = 0, mid = (l + r) >> 1;
for (long long i = L; i <= R; i++) {
calc(i, mid), cnt = ans + dp[i - 1][rt - 1];
if (cnt < minv) minv = cnt, pos = i;
}
dp[mid][rt] = minv;
search_for_dream(l, mid - 1, L, pos, rt);
search_for_dream(mid + 1, r, pos, R, rt);
}
signed main() {
n = read(), m = read();
for (long long i = 1; i <= n; i++) a[i] = read();
for (long long i = 1; i <= n; i++) {
dp[i][1] = dp[i - 1][1] + v[a[i]];
v[a[i]]++;
}
for (long long i = 1; i <= n; i++) v[i] = 0;
for (long long s = 2; s <= m; s++) search_for_dream(1, n, 1, n, s);
cout << dp[n][m] << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 50, L = 1050;
int A[N], lucky[N], n, lcnt, lnext[N];
vector<int> pos;
int islucky(int x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return false;
return true;
}
int main() {
scanf("%d", &n);
pos.push_back(0);
for (int i = 1; i <= n; ++i) {
scanf("%d", A + i);
if (islucky(A[i])) pos.push_back(i);
}
lcnt = pos.size();
for (int i = 1; i < lcnt; ++i) {
lnext[i] = lcnt;
for (int j = i + 1; j < lcnt; ++j)
if (A[pos[i]] == A[pos[j]]) {
lnext[i] = j;
break;
}
}
long long res =
(unsigned long long)(n + 2) * (n + 1) / 2 * n / 3 * (n - 1) / 4;
for (int i = 1; i + 1 < lcnt; ++i) {
set<int> s;
s.clear();
s.insert(pos[i]);
s.insert(n + 1);
long long ccnt = 0;
int cl = pos[i + 1] - pos[i];
for (int j = i; j; --j) {
if (lnext[j] > i) {
for (int k = lnext[j]; k < lcnt; k = lnext[k]) {
auto it = s.insert(pos[k]).first, l = prev(it), r = next(it);
if (l == s.begin())
ccnt += (long long)(*r - pos[k]) *
((long long)cl * (cl + 1) / 2 +
((long long)(pos[k] - pos[i + 1]) * cl));
else
ccnt += (long long)(*r - pos[k]) * (pos[k] - *l) * cl;
}
}
res -= ccnt * (pos[j] - pos[j - 1]);
}
}
printf("%I64d\n", res);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, x, count = 0, p;
string s;
char c;
cin >> n >> m;
cin >> s;
for (i = 1; i < n; i++) {
if (s.at(i) == 46 && s.at(i - 1) == 46) count++;
}
for (i = 0; i < m; i++) {
cin >> x >> c;
string str(1, c);
x--;
if (s.at(x) == '.' && c != '.' && n > 1) {
p = 0;
s.replace(x, 1, str);
if (x == 0 && s.at(x + 1) == 46)
p = 1;
else if (x == n - 1 && s.at(x - 1) == 46)
p = 1;
else if (x != 0 && x != n - 1) {
if (s.at(x - 1) == 46) p++;
if (s.at(x + 1) == 46) p++;
}
count -= p;
} else if (s.at(x) != '.' && c == '.' && n > 1) {
p = 0;
s.replace(x, 1, str);
if (x == 0 && s.at(x + 1) == 46)
p = 1;
else if (x == n - 1 && s.at(x - 1) == 46)
p = 1;
else if (x != 0 && x != n - 1) {
if (s.at(x - 1) == 46) p++;
if (s.at(x + 1) == 46) p++;
}
count += p;
} else
s.replace(x, 1, str);
cout << count << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n], s = 0;
bool f = 0;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n - 1; i++) {
if (a[i + 1] >= a[i]) f = 1;
}
if (f) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
const unsigned int Mod1 = 1000000009, Mod2 = 1000000007;
const unsigned int Bas1 = 12232, Bas2 = 14124;
struct mybit {
unsigned long long b[16];
mybit() { memset(b, 0, sizeof b); }
void clear() { memset(b, 0, sizeof b); }
void flip(int z) { b[z >> 6] ^= 1llu << (z & 63); }
unsigned int cnt() {
unsigned int ret = 0;
for (unsigned int j = 0u; j < 16u; ++j) ret += __builtin_popcount(b[j]);
return ret;
}
unsigned long long calc() {
unsigned int z1 = 0, z2 = 0;
for (unsigned int j = 0u; j < 16u; ++j)
z1 = (z1 + b[j] % Mod1 * Bas1) % Mod1,
z2 = (z2 + b[j] % Mod2 * Bas2) % Mod2;
return (unsigned long long)z1 << 32 | z2;
}
void operator^=(const mybit &p2) {
for (unsigned int j = 0u; j < 16u; ++j) b[j] ^= p2.b[j];
}
};
int N, S, Z, Q;
mybit bits[31], now;
std::unordered_map<unsigned long long, int> mp;
void DFS1(int st, int k) {
if (st > S) {
unsigned long long z = now.calc();
if (mp.find(z) != mp.end())
mp[z] = std::min(mp[z], k);
else
mp[z] = k;
return;
}
DFS1(st + 1, k);
now ^= bits[st];
DFS1(st + 1, k + 1);
now ^= bits[st];
}
int Ans;
void DFS2(int st, int k) {
if (st > Z) {
unsigned long long z = now.calc();
if (mp.find(z) != mp.end()) Ans = std::min(Ans, k + mp[z]);
return;
}
DFS2(st + 1, k);
now ^= bits[st];
DFS2(st + 1, k + 1);
now ^= bits[st];
}
int main() {
scanf("%d%d%d", &N, &S, &Q), Z = S / 3;
for (int i = 1; i <= S; ++i) {
unsigned int c, x;
scanf("%u", &c);
while (c--) {
scanf("%u", &x);
bits[i].flip(x - 1);
}
}
now.clear();
DFS1(Z + 1, 0);
while (Q--) {
now.clear();
unsigned int c, x;
scanf("%u", &c);
while (c--) {
scanf("%u", &x);
now.flip(x - 1);
}
Ans = S + 1;
DFS2(1, 0);
printf("%d\n", Ans == S + 1 ? -1 : Ans);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 100;
int n;
int m;
vector<int> adj1[N], adj2[N];
vector<int> poshteh;
int ans;
int inpp = 0;
int mop = 0;
int gh[N];
bool mark1[N];
bool mark2[N];
bool mark[N];
int p = 0;
int arr[N];
int point;
vector<int> scc[N];
bool out[N];
int tedad;
vector<int> q;
int f1[N], f2[N], f[N];
int bfs(int v) {
int alaki = v;
int S = 0, E = 0;
fill_n(f, n, false);
fill_n(mark, n, false);
q.clear();
q.push_back(v);
E++;
mark[v] = 1;
while (S < E) {
v = q[S];
S++;
for (int i = 0; i < adj1[v].size(); i++) {
int u = adj1[v][i];
if (!mark[u] && gh[u] == gh[v]) {
mark[u] = 1;
f[u] = f[v] + 1;
q.push_back(u);
E++;
} else {
if (u == alaki) {
int salaki = f[v] + 1;
return salaki;
}
}
}
}
}
void dfs1(int v) {
mark1[v] = true;
for (int i = 0; i < adj1[v].size(); i++) {
if (!mark1[adj1[v][i]]) {
dfs1(adj1[v][i]);
}
}
poshteh.push_back(v);
}
void dfs2(int v) {
mark2[v] = true;
gh[v] = mop;
scc[mop].push_back(v);
for (int i = 0; i < adj2[v].size(); i++) {
if (!mark2[adj2[v][i]]) {
dfs2(adj2[v][i]);
}
}
}
void moYab() {
for (int i = 0; i < n; i++) {
if (!mark1[i]) {
dfs1(i);
}
}
while (poshteh.size() != 0) {
int v = poshteh[poshteh.size() - 1];
poshteh.pop_back();
if (!mark2[v]) {
dfs2(v);
mop++;
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
adj1[x].push_back(y);
adj2[y].push_back(x);
}
moYab();
for (int i = 0; i < n; i++) {
for (int j = 0; j < adj1[i].size(); j++) {
int u = adj1[i][j];
if (gh[i] != gh[u]) {
out[gh[i]] = 1;
}
}
}
for (int i = 0; i < mop; i++) {
if (!out[i] && scc[i].size() > 1) {
int mini = N;
tedad++;
for (int j = 0; j < scc[i].size(); j++) {
int b = scc[i][j];
mini = min(mini, bfs(b));
}
ans = ans + mini;
}
}
cout << 998 * ans + n + tedad;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<bool> v;
vector<int> ans;
int n;
void dfs(int node, int i) {
v[node] = true;
ans[i++] = node;
if (i == n) return;
for (int j = 0; j < (int)g[node].size(); j++) {
if (!v[g[node][j]]) dfs(g[node][j], i);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
cin >> n;
g.clear();
g.resize(n);
ans.clear();
ans.resize(n);
for (int i = 0; i < n; i++) {
for (int j = 2; j <= 4; j++) {
if (i + j < n) {
g[i].push_back(i + j);
g[i + j].push_back(i);
}
}
}
for (int i = 0; i < n; i++) {
v.clear();
v.resize(n);
ans[n - 1] = -1;
dfs(i, 0);
if (ans[n - 1] != -1) {
for (int j = 0; j < n; j++) cout << ans[j] + 1 << " ";
cout << endl;
break;
}
}
if (ans[n - 1] == -1) cout << -1 << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
cout << "Mike" << endl;
int min_so_far = s[0] - 'a';
for (int i = 1; i < s.length(); i++) {
if (min_so_far < (s[i] - 'a')) {
cout << "Ann" << endl;
} else {
cout << "Mike" << endl;
}
min_so_far = min(min_so_far, s[i] - 'a');
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int maxn = 4007;
std::vector<char> lis;
struct Node {
char a[11], b;
} node[maxn];
int n, q;
int dfs(char s, int len) {
if (len == n) return 1;
int ret = 0;
for (int i = 1; i <= q; i++) {
if (node[i].b == s) ret += dfs(node[i].a[0], len + 1);
}
return ret;
}
int main() {
std::cin >> n >> q;
for (int i = 1; i <= q; i++) {
std::cin >> node[i].a >> node[i].b;
}
long long ans[11] = {0ll};
std::cout << dfs('a', 1) << "\n";
}
| 2 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <set>
#include <map>
#include <vector>
#include <cstring>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
#define re return
#define sz(n) (int)(n.size())
#define all(x) x.begin(),x.end()
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long int i;
const long mod = 1e9 + 7;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vi a(n);
int c0 = 0, c1 = 0, c2 = 0;
for (auto &i: a) {
cin >> i;
if (i % 3 == 0)
++c0;
else if (i % 3 == 1)
++c1;
else if (i % 3 == 2)
++c2;
}
int norm = n / 3;
int kol = 0;
if (c0 == c1 && c1 == c2){
cout << "0\n";
continue;
}
while (c0 != c1 || c1 != c2) {
if (c0 > norm) c0--, c1++, kol++;
if (c1 > norm) c1--, c2++, kol++;
if (c2 > norm) c2--, c0++, kol++;
}
cout << kol << '\n';
}
} | 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 300000;
const int MAX_E2 = 1 << 20;
const long double PI = acosl(-1.0);
struct Elm {
long double x, y, th;
Elm() : x(0.0), y(0.0), th(0.0) {}
Elm(long double _x, long double _y, long double _th)
: x(_x), y(_y), th(_th) {}
Elm &extend(int l) {
x += l * cosl(th), y += l * sinl(th);
return *this;
}
Elm &rot(long double dth) {
long double c = cosl(dth), s = sinl(dth);
long double x0 = c * x - s * y;
long double y0 = s * x + c * y;
x = x0, y = y0;
th += dth;
return *this;
}
Elm operator+(const Elm &e) const {
long double c = cosl(th), s = sinl(th);
long double x0 = c * e.x - s * e.y;
long double y0 = s * e.x + c * e.y;
return Elm(x + x0, y + y0, th + e.th);
}
};
template <typename T, const int MAX_E2>
struct SegTree {
int e2;
T nodes[MAX_E2], defv;
SegTree() {}
void init(int n, T _defv) {
defv = _defv;
for (e2 = 1; e2 < n; e2 <<= 1)
;
fill(nodes, nodes + MAX_E2, defv);
}
T &get(int i) { return nodes[e2 - 1 + i]; }
void seti(int i, T v) { get(i) = v; }
void setall() {
for (int j = e2 - 2; j >= 0; j--)
nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
}
void set(int i, T v) {
int j = e2 - 1 + i;
nodes[j] = v;
while (j > 0) {
j = (j - 1) / 2;
nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
}
}
};
SegTree<Elm, MAX_E2> st;
int main() {
int n, m;
scanf("%d%d", &n, &m);
st.init(n, Elm(0.0, 0.0, 0.0));
for (int i = 0; i < n; i++) st.seti(i, Elm(1.0, 0.0, 0.0));
st.setall();
for (int i = 0; i < m; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
y--;
Elm e = st.get(y);
if (x == 1)
e.extend(z);
else
e.rot(-PI * z / 180);
st.set(y, e);
Elm r = st.nodes[0];
printf("%.10Lf %.10Lf\n", r.x, r.y);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int a, b, c;
} d[200005];
bool cmp(const node& p, const node& q) { return p.c > q.c; }
int main() {
int n, k, i, ans;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) scanf("%d", &d[i].a);
for (i = 1; i <= n; i++) scanf("%d", &d[i].b), d[i].c = d[i].b - d[i].a;
sort(d + 1, d + 1 + n, cmp);
for (ans = 0, i = 1; i <= k; i++) ans += d[i].a;
for (; i <= n; i++) ans += min(d[i].a, d[i].b);
printf("%d\n", ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void itval(istream_iterator<string> it) {}
template <typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
itval(++it, args...);
}
const long long int MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {
cout << x << "\n";
}
template <typename T>
inline void printvec(T x) {
for (auto a : x) cout << a << ' ';
cout << '\n';
}
struct custom {
bool operator()(
const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2)
const {
return p1.first < p2.first;
}
};
long long int get_pow(long long int a, long long int b, long long int M = MOD) {
long long int res = 1;
while (b) {
if (b & 1) res = (res * a) % M;
a = (a * a) % M;
b >>= 1;
}
return res;
}
const long long int N = 1e5 + 2, inf = 2e18;
void solve() {
long long int n;
cin >> n;
vector<long long> l(n), r(n);
for (long long int i = (long long int)0; i < (long long int)(n); i++)
cin >> l[i] >> r[i];
sort(l.begin(), l.end());
sort(r.begin(), r.end());
long long int ans = 0;
for (long long int i = (long long int)0; i < (long long int)(n); i++)
ans += max(l[i], r[i]) + 1;
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int test = 1;
clock_t z = clock();
for (long long int tes = (long long int)0; tes < (long long int)(test);
tes++) {
solve();
}
fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC),
fflush(stderr);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a{}, b{};
cin >> a >> b;
int counter{};
while (1 > 0) {
if (a > b) break;
a *= 3;
b *= 2;
counter++;
}
cout << counter;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5 + 9;
int n;
int a[sz];
int cs[3][sz];
vector<pair<int, int>> ans;
int w = 1, l = 2;
int ns[3][sz], nt[3][sz];
int lost[3];
int nxt[3][sz];
int idx[sz];
int sec[3][sz];
bool ok(int s, int t) {
lost[1] = lost[2] = 0;
if (idx[t])
return ((ns[1][t] == s && ns[2][t] < s && sec[1][t] == n) ||
(ns[2][t] == s && ns[1][t] < s && sec[2][t] == n)) &&
idx[t] == n + 1;
ns[1][t] = ns[2][t] = 0;
idx[t] = 1;
while (idx[t] <= n) {
int nxtw = nxt[w][lost[w] + t];
int nxtl = nxt[l][lost[l] + t];
if (nxtw < nxtl) {
idx[t] = nxtw + 1;
ns[w][t]++;
sec[w][t] = nxtw;
lost[l] = cs[l][nxtw];
lost[w] = cs[w][nxtw];
} else if (nxtl < nxtw) {
idx[t] = nxtl + 1;
ns[l][t]++;
sec[l][t] = nxtl;
lost[l] = cs[l][nxtl];
lost[w] = cs[w][nxtl];
} else
break;
}
return ((ns[1][t] == s && ns[2][t] < s && sec[1][t] == n) ||
(ns[2][t] == s && ns[1][t] < s && sec[2][t] == n)) &&
idx[t] == n + 1;
;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
cs[1][i] = cs[1][i - 1] + (a[i] == 1);
cs[2][i] = cs[2][i - 1] + (a[i] == 2);
}
if (cs[1][n] == cs[2][n]) {
printf("0");
return 0;
}
for (int i = 1; i <= 3 * n; i++) nxt[1][i] = nxt[2][i] = 1e6;
nxt[w][cs[w][1]] = 1;
nxt[l][cs[l][1]] = 1;
for (int i = 2; i <= n; i++) {
if (cs[w][i] != cs[w][i - 1]) nxt[w][cs[w][i]] = i;
if (cs[l][i] != cs[l][i - 1]) nxt[l][cs[l][i]] = i;
}
for (long long s = 1; s <= n; s++) {
long long fst = ceil((n + s * 2 - 1) / (s * 4 - 2));
long long lst = n / s;
if (!fst) fst++;
for (long long t = fst; t <= lst; t++) {
if (ok(s, t)) ans.push_back(make_pair(s, t));
}
}
sort(ans.begin(), ans.end());
printf("%d\n", ans.size());
for (auto u : ans) printf("%d %d\n", u.first, u.second);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
long long ans[1000005] = {0};
int main() {
long long n, temp, sum = 0;
ans[0] = 1;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> temp;
vector<long long> v;
for (long long j = 1; j <= sqrt(temp); j++) {
if (temp % j == 0) {
v.push_back(j);
if (temp / j != j) v.push_back(temp / j);
}
}
sort(v.begin(), v.end(), greater<long long>());
for (long long j = 0; j < v.size(); j++)
ans[v[j]] = (ans[v[j]] + ans[v[j] - 1]) % MOD;
}
for (long long i = 1; i <= n; i++) sum = (sum + ans[i]) % MOD;
cout << sum;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long A[300005];
long long ans[300005];
long long egcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long g = egcd(b, a % b, y, x);
y -= x * (a / b);
return g;
}
long long inv_mod(long long x) {
long long a, b;
egcd(x, 998244353, a, b);
if (a < 0) a += 998244353;
return a;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
sort(A, A + n);
for (int i = 1; i < n; i++) A[i] = (A[i - 1] + A[i]) % 998244353;
ans[0] = 0;
for (int i = 1; i < n; i++) {
int m = n - i;
ans[i] = 0;
for (int j = (i % m == 0 ? m : i % m); j <= i; j += m) {
ans[i] += A[j - 1];
}
ans[i] %= 998244353;
}
long long m = inv_mod(n);
reverse(ans, ans + n);
for (int i = 0; i < n; i++) {
cout << (ans[i] * m) % 998244353 << " ";
}
cout << endl;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
char s1[maxn], s2[maxn];
long long dp[maxn][3][3];
int p1[maxn], p2[maxn], n, m;
int mp(char a) { return a == 'G' ? 0 : (a == 'R' ? 1 : 2); }
void ini(const char a[], int n, const char b[], int m, int p[]) {
p[0] = 0;
for (int i = 1; i < n + 1; i++) {
p[i] = p[i - 1] + 1;
while (p[i] <= m && a[i] != b[p[i]]) p[i]++;
p[i] = min(p[i], m);
}
}
int main() {
scanf("%s", s1 + 1);
scanf("%s", s2 + 1);
n = strlen(s1 + 1);
m = strlen(s2 + 1);
ini(s1, n, s2, m, p1);
ini(s2, m, s1, n, p2);
memset(dp, 0ll, sizeof(dp));
for (int i = 2; i < m + 1; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if (mp(s2[i - 1]) == j && mp(s2[i]) == k)
dp[i][j][k] = dp[i - 1][j][k] + 1;
else
dp[i][j][k] = dp[i - 1][j][k];
}
}
}
long long ret = 0ll;
int l = 1, r;
for (int i = 1; i < n + 1; i++) {
r = p1[i];
while (l <= m && p2[l] < i) l++;
ret += max(r - l + 1, 0);
if (i > 1 && l <= r) {
int j = mp(s1[i - 1]);
int k = mp(s1[i]);
if (j != k) {
ret -= dp[r][k][j];
ret += dp[l - 1][k][j];
}
}
}
cout << ret << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long int choose(long long int n, long long int m) {
long long int ans = 0;
long long int num = 1;
long long int den = 1;
for (long long int i = 0; i < m; i++) {
num *= (n - i);
num = num / (i + 1);
}
return num;
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) {
return b;
} else {
return gcd(b % a, a);
}
}
bool sortDes(long long int i, long long int j) { return i > j; }
long long int bits(long long int n) {
long long int count = 0;
while (n != 0) {
if (n % 2 != 0)
count++;
else
;
n /= 2;
}
return count;
}
int main() {
long long int i, j, k, n, m, q, l, r, d;
cin >> n >> k;
vector<pair<long long int, long long int> > a(n);
map<long long int, long long int> e;
vector<long long int> v(n);
d = 1;
for (i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
e[a[i].first]++;
}
for (i = 0; i < n; i++) {
if (e[a[i].first] > k) {
cout << "NO\n";
return 0;
}
}
if (k > n) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
sort(a.begin(), a.end());
long long int prev = -1;
for (i = 0; i < n; i++) {
if (prev == -1) {
prev = a[i].first;
v[a[i].second] = 1;
d = 1;
} else {
if (prev == a[i].first) {
d++;
if (d > k) {
v[a[i].second] = 1;
d = 1;
prev = a[i].first;
} else {
v[a[i].second] = d;
prev = a[i].first;
}
} else {
d++;
if (d > k) {
d = 1;
}
v[a[i].second] = d;
prev = a[i].first;
}
}
}
for (i = 0; i < n; i++) {
cout << v[i] << " ";
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, dp[4005][4005], dps[4005], comb[4005][4005];
int main() {
scanf("%d", &n);
dp[0][0] = 1;
dps[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++) {
dp[i][j] = (dp[i - 1][j - 1] + 1LL * j * dp[i - 1][j]) % 1000000007;
dps[i] += dp[i][j];
while (dps[i] >= 1000000007) dps[i] -= 1000000007;
}
comb[0][0] = 1;
for (int i = 1; i <= n; i++) {
comb[i][0] = 1;
for (int j = 1; j <= i; j++) {
comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
while (comb[i][j] >= 1000000007) comb[i][j] -= 1000000007;
}
}
int sol = 0;
for (int i = 0; i < n; i++)
sol = (sol + 1LL * comb[n][n - i] * dps[i]) % 1000000007;
printf("%d\n", sol);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m;
int a[N];
int pre[N], nxt[N];
int main() {
int i, j;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
for (i = 1; i <= n; i++) {
if (a[i] > a[i - 1])
pre[i] = pre[i - 1] + 1;
else
pre[i] = 1;
}
for (i = n; i > 0; i--) {
if (a[i] < a[i + 1])
nxt[i] = nxt[i + 1] + 1;
else
nxt[i] = 1;
}
int mx = -1;
for (i = 1; i <= n; i++) {
mx = max(mx, pre[i - 1] + 1);
mx = max(mx, nxt[i + 1] + 1);
if (i > 1 && i < n && a[i - 1] < a[i + 1] - 1)
mx = max(mx, pre[i - 1] + nxt[i + 1] + 1);
}
cout << mx;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, c, x, a[31];
inline long long read() {
long long sum = 0, x = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') x = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
sum = sum * 10 + ch - '0';
ch = getchar();
}
return sum * x;
}
inline void write(long long x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
return;
}
inline long long gcd(long long x, long long y) {
if (y == 0) return x;
return gcd(y, x % y);
}
signed main() {
n = read();
for (register long long i = 1; i <= n; ++i) a[i] = read();
for (register long long i = 1; i <= n; ++i)
if (a[i] & 1) {
++c;
x = i;
}
if (c > 1) {
write(0);
putchar('\n');
for (register long long i = 1; i <= n; ++i)
while (a[i]--) putchar('a' - 1 + i);
} else if (c == 1) {
c = a[1];
for (register long long i = 2; i <= n; ++i) c = gcd(c, a[i]);
write(c);
putchar('\n');
for (register long long i = 1; i <= c; ++i) {
for (register long long j = 1; j <= n; ++j)
if (j != x)
for (register long long k = 1; k <= a[j] / c / 2; ++k)
putchar('a' - 1 + j);
for (register long long j = 1; j <= a[x] / c; ++j) putchar('a' - 1 + x);
for (register long long j = n; j > 0; --j)
if (j != x)
for (register long long k = 1; k <= a[j] / c / 2; ++k)
putchar('a' - 1 + j);
}
} else {
c = a[1];
for (register long long i = 2; i <= n; ++i) c = gcd(c, a[i]);
write(c);
putchar('\n');
for (register long long i = 1; i <= c / 2; ++i) {
for (register long long j = 1; j <= n; ++j)
for (register long long k = 1; k <= a[j] / c; ++k) putchar('a' - 1 + j);
for (register long long j = n; j > 0; --j)
for (register long long k = 1; k <= a[j] / c; ++k) putchar('a' - 1 + j);
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100003;
const long long maxk = 1003;
set<long long> si[maxn];
long long n, m;
long long aib[maxn];
long long next(long long x) { return x & -x; }
void update(long long x, long long adder) {
for (long long i = x; i <= n; i += next(i)) {
aib[i] += adder;
}
}
long long query(long long x) {
long long ret = 0;
for (long long i = x; i >= 1; i -= next(i)) {
ret += aib[i];
}
return ret;
}
long long a;
int32_t main() {
ios_base::sync_with_stdio(false);
cin >> n;
long long maxime = 0;
for (long long i = 1; i <= n; i++) {
cin >> a;
maxime = max(maxime, a);
si[a].insert(i);
update(i, 1);
}
long long ans = 0;
long long oldPos = 0;
long long currPos = 1;
set<long long>::iterator it;
for (long long i = 1; i <= maxime; i++) {
if (si[i].size()) {
while (si[i].size()) {
if (si[i].upper_bound(oldPos) != si[i].end()) {
currPos = *(si[i].upper_bound(oldPos));
} else {
currPos = *(si[i].begin());
}
if (oldPos <= currPos) {
ans += query(currPos) - query(oldPos - 1);
} else {
ans += (query(n) - query(oldPos - 1)) + query(currPos);
}
si[i].erase(currPos);
oldPos = currPos;
update(oldPos, -1);
}
}
}
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXD = 11;
int n;
bool a[MAXD], b[MAXD];
inline void intoDec(int x, bool* t) {
for (int i = x; i > 0; i /= 10) t[i % 10] = true;
}
inline bool check(int x) {
memset(b, 0, sizeof(b));
intoDec(x, b);
for (int i = 0; i <= 9; ++i)
if (a[i] && b[i]) return true;
return false;
}
int main() {
scanf("%d\n", &n);
intoDec(n, a);
int res = 0;
for (int i = 1; i * i <= n; ++i)
if (n % i == 0) {
int j = n / i;
res += check(i) + (i != j && check(j));
}
printf("%d\n", res);
fclose(stdin);
fclose(stdout);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
long long binpow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, m, l, r;
cin >> n >> m >> l >> r;
if (r == l) {
cout << 1;
return 0;
}
long long ans = binpow(r - l + 1, n * m);
if ((n * m) % 2 == 1) {
cout << ans << '\n';
return 0;
}
if ((r - l + 1) % 2 == 0) {
cout << (ans * binpow(2, mod - 2)) % mod << "\n";
} else {
cout << ((ans + 1) * binpow(2, mod - 2)) % mod << "\n";
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct I {
long long lo, hi;
I(long long lo, long long hi) : lo(lo), hi(hi) {}
long long len() { return hi - lo + 1LL; }
};
I square_range(long long lo, long long hi) {
long long mostlo = (long long)ceil(sqrt(lo - 0.5));
long long mosthi = (long long)floor(sqrt(hi + 0.5));
if (lo <= 0) mostlo = 0;
return I(mostlo, mosthi);
}
const long long SUP = 5000000;
long long cnt[SUP * 2 + 2000];
void add_interval(I is) {
assert(is.lo <= is.hi);
cnt[is.lo + SUP]++;
cnt[is.hi + SUP + 1]--;
}
int main() {
long long N, M;
while (cin >> N >> M) {
memset((cnt), (0LL), sizeof(cnt));
long long res = 0;
for (long long b = (1); b <= (long long)(N); ++b) {
I is = square_range(b * b - M, b * b - 1);
res += (min(b * b, M) - is.len()) * 2LL;
if (is.len()) {
I is1 = is, is2 = is;
is1.hi += -b, is1.lo += -b;
is2.hi += +b, is2.lo += +b;
is2.hi = -is2.hi, is2.lo = -is2.lo;
swap(is2.hi, is2.lo);
add_interval(is1);
add_interval(is2);
}
}
long long su = 0;
for (long long j = (0); j <= (long long)(2 * SUP); ++j) {
su += cnt[j];
if (su > 0) res++;
}
cout << res << endl;
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b, c;
cin >> a >> b >> c;
a += b;
for (int i = 0; i < a.size(); ++i) {
int pos = c.find(a[i]);
if (pos + 1) {
c.erase(c.begin() + pos);
if (!c.size()) {
cout << "YES";
return 0;
}
continue;
} else {
cout << "NO";
return 0;
}
}
if (c.size())
cout << "NO";
else
cout << "YES";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
int n, m, k, dem;
int x[200005], y[200005];
int main() {
long long a;
cin >> a;
for (long long i = 2; i * i <= a; i++) {
while (a % (i * i) == 0) a /= i;
}
cout << a;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string str;
bool isHalfPal[5055][5055];
string builtStr;
int n;
class Trie {
public:
int child[2];
int cnt;
int total;
Trie(int childCnt) {
child[0] = child[1] = -1;
total = cnt = 0;
}
};
Trie* root;
vector<Trie*> suffixTrie;
void accumulate(Trie* node) {
node->total = node->cnt;
if (node->child[0] != -1) {
accumulate(suffixTrie[node->child[0]]);
node->total += suffixTrie[node->child[0]]->total;
}
if (node->child[1] != -1) {
accumulate(suffixTrie[node->child[1]]);
node->total += suffixTrie[node->child[1]]->total;
}
}
void DFS(Trie* node, int k) {
if (k <= node->cnt) {
return;
}
k -= node->cnt;
if (node->child[0] != -1) {
if (suffixTrie[node->child[0]]->total >= k) {
builtStr += 'a';
DFS(suffixTrie[node->child[0]], k);
} else {
builtStr += 'b';
DFS(suffixTrie[node->child[1]], k - suffixTrie[node->child[0]]->total);
}
} else {
builtStr += 'b';
DFS(suffixTrie[node->child[1]], k);
}
}
int main() {
cin >> str;
int k;
cin >> k;
n = str.size();
for (int i = 0; i < n; i++) {
isHalfPal[i][i] = true;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
isHalfPal[i][j] = true;
}
}
for (int len = 1; len <= n; len++) {
for (int i = 0; i + len - 1 < n; i++) {
int j = i + len - 1;
isHalfPal[i][j] = (str[i] == str[j] &&
(i + 2 >= n || j - 2 < 0 || isHalfPal[i + 2][j - 2]));
}
}
root = new Trie(2);
for (int i = 0; i < n; i++) {
int j = i;
Trie* temp = root;
while (j < n) {
if (str[j] == 'a') {
if (temp->child[0] == -1) {
temp->child[0] = suffixTrie.size();
temp = new Trie(2);
suffixTrie.push_back(temp);
} else {
temp = suffixTrie[temp->child[0]];
}
if (isHalfPal[i][j]) {
temp->cnt++;
}
} else {
if (temp->child[1] == -1) {
temp->child[1] = suffixTrie.size();
temp = new Trie(2);
suffixTrie.push_back(temp);
} else {
temp = suffixTrie[temp->child[1]];
}
if (isHalfPal[i][j]) {
temp->cnt++;
}
}
j++;
}
}
accumulate(root);
DFS(root, k);
cout << builtStr << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct help {
int a, b, i;
};
bool cmp(help &a, help &b) { return a.b < b.b; }
int main() {
int n;
cin >> n;
vector<int> vect(n);
for (int i = 0; i < n; ++i) cin >> vect[i];
int p;
cin >> p;
vector<help> tests(p);
for (int j = 0; j < p; ++j) {
cin >> tests[j].a >> tests[j].b;
tests[j].i = j;
}
sort(tests.begin(), tests.end(), cmp);
int k = 0;
vector<long long int> dp(n);
vector<long long int> ans(p);
int a = -1, b = -1;
while (k < p) {
if (tests[k].b > 500) {
long long int ans1 = 0;
a = tests[k].a;
b = tests[k].b;
--a;
for (int i = a; i < n; i += b) {
ans1 += (long long int)vect[i];
}
ans[tests[k].i] = ans1;
} else {
a = tests[k].a;
--a;
if (tests[k].b == b) {
ans[tests[k].i] = dp[a];
} else {
b = tests[k].b;
for (int i = n - 1; i >= 0; --i) {
dp[i] = (long long int)vect[i];
if (i + b < n) dp[i] += dp[i + b];
}
ans[tests[k].i] = dp[a];
}
}
++k;
}
for (auto x : ans) cout << x << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
char s[MAXN];
int len[MAXN];
stack<int> S;
int main(void) {
while (scanf("%s", s) == 1) {
while (!S.empty()) S.pop();
memset(len, 0, sizeof(len));
int mx = 0;
int cnt = 0;
for (int i = 0; s[i]; ++i) {
if (s[i] == '(')
S.push(i);
else {
if (!S.empty()) {
int p = S.top();
S.pop();
len[i] = (p > 0 ? len[p - 1] : 0) + (i - p + 1);
}
}
if (len[i] > mx) {
mx = len[i];
cnt = 1;
} else if (len[i] == mx) {
cnt++;
}
}
if (mx == 0)
puts("0 1");
else
printf("%d %d\n", mx, cnt);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool solve() {
int n;
cin >> n;
int arr[n];
for (auto &i : arr) cin >> i;
int o = 0, e = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2)
o++;
else
e++;
}
if (o % 2 == 0 && e % 2 == 0) return true;
sort(arr, arr + n);
for (int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] == 1) return true;
}
return false;
}
int main() {
int t;
cin >> t;
while (t--) {
if (solve())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
void test_case() {
string s;
cin >> s;
vector<bool> used(26);
string t;
t += s[0];
used[s[0] - 'a'] = 1;
long long pos = 0;
for (long long i = 1; i < (long long)((s).size()); i++) {
if (used[s[i] - 'a']) {
if (pos > 0 && t[pos - 1] == s[i])
pos--;
else if (pos < (long long)((t).size()) - 1 && t[pos + 1] == s[i])
pos++;
else {
cout << "NO\n";
return;
}
} else {
if (pos == 0) {
t = s[i] + t;
} else if (pos == (long long)((t).size()) - 1) {
t += s[i];
pos = (long long)((t).size()) - 1;
} else {
cout << "NO\n";
return;
}
}
used[s[i] - 'a'] = 1;
}
for (long long i = 0; i < 26; i++) {
if (!used[i]) t += (i + 'a');
}
cout << "YES\n" << t << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
cin >> t;
for (long long i = 0; i < t; i++) {
test_case();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int b_s(long long int a[], long long int l, long long int r,
long long int result) {
long long int m;
while (r >= l) {
m = l + (r - l) / 2;
if (a[m] == result)
return m;
else if (a[m] > result)
r = m - 1;
else
l = m + 1;
}
return -1;
}
void solve() {
long long int i, n, k, s = 0;
cin >> n >> k;
long long int a[n], b[n];
for (i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
}
for (i = 0; i < n; i++) cin >> b[i];
sort(a, a + n);
sort(b, b + n);
for (i = 0; i < k; i++) {
if (a[i] < b[n - 1 - i])
s = s - a[i] + b[n - 1 - i];
else
break;
}
cout << s << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool isVowel(char c) {
if (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u' or c == 'y')
return true;
return false;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
string s;
cin >> s;
string ans = "";
for (long long i = 0; i < n; i++) {
ans += s[i];
if (!isVowel(s[i])) continue;
long long j = i;
while (j < n && isVowel(s[j])) j++;
i = j - 1;
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int countfives(long n) {
int t = 0;
while (n % 5 == 0) {
n = n / 5;
t++;
}
return t;
}
int counttwos(long n) {
int t = 0;
while (n % 2 == 0) {
n = n / 2;
t++;
}
return t;
}
int main() {
long long n, k;
cin >> n >> k;
int t5 = countfives(n);
int t2 = counttwos(n);
while (t5 < k) {
n = n * 5;
t5++;
}
while (t2 < k) {
n = n * 2;
t2++;
}
cout << n;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int N = 2e5 + 5;
int di[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dj[] = {-1, 0, 1, 1, 1, 0, -1, -1};
string dir[] = {"LD", "L", "LU", "U", "RU", "R", "RD", "D"};
bool valid(int i, int j) { return (i >= 0 && j > 0 && i < 8 && j <= 8); }
string ans[10][10];
pair<int, int> parent[10][10];
bool vis[10][10];
int bfs(int i, int j, int distI, int distJ) {
queue<pair<int, int>> q;
q.push({i, j});
vis[i][j] = true;
int level = 0;
while (!q.empty()) {
int sz = q.size();
while (sz--) {
pair<int, int> curr = q.front();
q.pop();
if (curr.first == distI && curr.second == distJ) return level;
for (int k = 0; k < 8; ++k) {
int u = curr.first + di[k];
int v = curr.second + dj[k];
if (valid(u, v) && !vis[u][v]) {
vis[u][v] = true;
q.push({u, v});
parent[u][v] = {curr.first, curr.second};
ans[u][v] = dir[k];
}
}
}
level++;
}
return -1;
}
int main() {
fast();
string s, t;
cin >> s >> t;
pair<int, int> dist = {(t[0] - 'a'), (t[1] - '0')};
pair<int, int> src = {(s[0] - 'a'), (s[1] - '0')};
vector<string> d;
cout << bfs(src.first, src.second, dist.first, dist.second) << "\n";
parent[src.first][src.second] = {-1, -1};
while (parent[dist.first][dist.second] != make_pair(-1, -1)) {
d.push_back(ans[dist.first][dist.second]);
dist = parent[dist.first][dist.second];
}
reverse(d.begin(), d.end());
for (auto &c : d) cout << c << "\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long maxSubArraySum(vector<long long> a) {
long long siz = a.size();
long long max_so_far = a[0];
long long curr_max = a[0];
for (long long i = 1; i < siz; i++) {
curr_max = max(a[i], curr_max + a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main() {
long long n;
cin >> n;
long long ar[n];
for (long long i = 0; i < n; i++) {
cin >> ar[i];
}
bool t = true;
vector<long long> v1, v2;
for (long long i = 0; i < n - 1; i++) {
if (t) {
v1.push_back(abs(ar[i] - ar[i + 1]));
v2.push_back(-abs(ar[i] - ar[i + 1]));
} else {
v1.push_back(-abs(ar[i] - ar[i + 1]));
v2.push_back(abs(ar[i] - ar[i + 1]));
}
t = !t;
}
cout << max(maxSubArraySum(v1), maxSubArraySum(v2));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int f = 1, x = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
bitset<10000> b[60];
int main() {
int m = read(), n = read();
for (int i = 1; i <= m; ++i) {
int k = read();
bool flag = 1;
for (int j = 1; j <= k; ++j) {
int x = read();
b[i][x] = 1;
}
for (int j = 1; j < i; ++j)
if (!(b[i] & b[j]).count()) {
puts("impossible");
return 0;
}
}
puts("possible");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, par[100005], dis[100005], dp[20][100005], vis[100005];
vector<long long> a[100005];
double cnt[100005], sum[100005], rootsum[100005];
void dfs(long long node) {
vis[node] = 1;
cnt[node] = 1;
for (auto i : a[node]) {
if (!vis[i]) {
par[i] = node;
dis[i] = dis[node] + 1;
dfs(i);
cnt[node] += cnt[i];
sum[node] += sum[i] + cnt[i];
}
}
}
void dfs2(long long node) {
vis[node] = 1;
for (auto i : a[node]) {
if (!vis[i]) {
rootsum[i] = rootsum[node] - 2 * cnt[i] + n;
dfs2(i);
}
}
}
void pre() {
for (long long j = 0; j < 20; j++) {
for (long long i = 1; i < n + 1; i++) {
if (j == 0)
dp[j][i] = par[i];
else
dp[j][i] = dp[j - 1][dp[j - 1][i]];
}
}
}
long long lca(long long u, long long v) {
if (u == v) return u;
if (dis[u] < dis[v]) return lca(v, u);
for (long long j = 19; j >= 0; j--) {
if (dis[u] - (1 << j) >= dis[v]) {
u = dp[j][u];
}
}
if (u == v) return u;
for (long long j = 19; j >= 0; j--) {
if (dp[j][u] != dp[j][v]) {
u = dp[j][u];
v = dp[j][v];
}
}
return par[u];
}
long long f(long long u, long long v) {
for (long long j = 19; j >= 0; j--) {
if (dis[v] - (1 << j) > dis[u]) {
v = dp[j][v];
}
}
return v;
}
void solve() {
cin >> n >> m;
for (long long i = 0; i < n - 1; i++) {
long long first, second;
cin >> first >> second;
a[first].push_back(second);
a[second].push_back(first);
}
dfs(1);
pre();
rootsum[1] = sum[1];
memset(vis, 0, sizeof vis);
dfs2(1);
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
long long l = lca(u, v);
double ans = dis[u] + dis[v] - 2 * dis[l] + 1;
if (l == u) {
long long g = f(u, v);
ans += (rootsum[u] - (sum[g] + cnt[g])) / (n - cnt[g]) + sum[v] / cnt[v];
} else if (l == v) {
long long g = f(v, u);
ans += (rootsum[v] - (sum[g] + cnt[g])) / (n - cnt[g]) + sum[u] / cnt[u];
} else {
ans += sum[u] / cnt[u] + sum[v] / cnt[v];
}
cout << fixed << setprecision(10) << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int64_t mod = 1000000007, inf = 4e18;
struct comp {
int64_t operator()(const pair<int64_t, int64_t> a,
const pair<int64_t, int64_t> b) {
return a > b;
}
};
int32_t main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cout << fixed << setprecision(0);
int64_t n, tmp, tmp2;
cin >> n;
map<int64_t, vector<pair<int64_t, int64_t>>> mp;
map<int64_t, int64_t> fs;
vector<set<pair<int64_t, int64_t>, comp>> arr(n + 1);
for (int64_t i = 0; i < int64_t(n); ++i) {
cin >> tmp >> tmp2;
mp[tmp].push_back(pair<int64_t, int64_t>(tmp2, i + 1));
}
for (auto &v : mp) {
sort((v.second).begin(), (v.second).end(),
[](pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) {
return a > b;
});
arr[0].insert(pair<int64_t, int64_t>(0, v.first));
int64_t sum, i;
for (i = 0, sum = 0; i < v.second.size(); i++) {
sum += v.second[i].first;
arr[i + 1].insert(pair<int64_t, int64_t>(sum, v.first));
}
fs[v.first] = sum;
}
int64_t maxi = -inf;
pair<int64_t, int64_t> ind;
for (auto &v : mp) {
for (int64_t k = v.second.size() - 1; k <= v.second.size() + 1; k++) {
auto it = arr[k].begin();
if (it == arr[k].end()) continue;
if (it->second == v.first) it++;
if (it == arr[k].end()) continue;
int64_t val = it->first + fs[v.first];
if (maxi < val) {
maxi = val;
ind = {v.first, it->second};
}
}
}
cout << maxi << endl;
vector<pair<int64_t, int64_t>> v1, v2;
for (auto &v : mp) {
if (v.first == ind.first) v1 = v.second;
if (v.first == ind.second) v2 = v.second;
}
vector<int64_t> fin;
if (v1.size() > v2.size()) swap(v1, v2);
for (int64_t i = 0; i < v1.size(); i++) {
fin.push_back(v2[i].second), fin.push_back(v1[i].second);
}
if (v1.size() != v2.size()) fin.push_back(v2[v1.size()].second);
cout << fin.size() << endl;
for (auto &v : fin) cout << v << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << pow(2 * n - 1, 2) - 2 * n * (n - 1);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int T, n, m, k, a, h;
bool b;
int main() {
cin >> T;
while (T--) {
b = true;
cin >> n >> m >> k >> h;
while (--n) {
a = h;
cin >> h;
if (a - max(0, h - k) < -m) b = false;
m += a - max(0, h - k);
}
puts(b ? "YES" : "NO");
}
}
| 2 |
#include <bits/stdc++.h>
int a[4], cnt, ans2[1005];
char ans1[1005];
void jia(int x) {
a[x]++;
a[(x + 1) % 4]++;
++cnt;
ans1[cnt] = '+';
ans2[cnt] = x;
}
void chu(int x) {
a[x] >>= 1;
a[(x + 1) % 4] >>= 1;
++cnt;
ans1[cnt] = '/';
ans2[cnt] = x;
}
int main() {
for (int i = (int)0, _y = 3; i <= _y; i++) scanf("%d", &a[i]);
for (;;) {
int k = 0;
for (int i = (int)1, _y = 3; i <= _y; i++)
if (a[k] < a[i]) k = i;
if (a[k] == 1) break;
int l = (k - 1 + 4) % 4, r = (k + 1) % 4;
if (a[k] & 1) {
if (a[l] & 1)
jia(l), chu(l);
else if (a[r] & 1)
jia(k), chu(k);
else
jia(l), chu(k);
} else {
if ((a[l] & 1) ^ 1)
chu(l);
else if ((a[r] & 1) ^ 1)
chu(k);
else
jia(l), jia(k), chu(l);
}
}
for (int i = (int)1, _y = cnt; i <= _y; i++)
printf("%c%d\n", ans1[i], ans2[i] + 1);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
struct Point {
double x, y;
Point() {}
Point(double a, double b) {
x = a;
y = b;
}
double mod2() { return x * x + y * y; }
double mod() { return sqrt(x * x + y * y); }
double arg() { return atan2(y, x); }
Point ort() { return Point(-y, x); }
Point unit() {
double k = mod();
return Point(x / k, y / k);
}
};
Point operator+(const Point &a, const Point &b) {
return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b) {
return Point(a.x - b.x, a.y - b.y);
}
Point operator/(const Point &a, double k) { return Point(a.x / k, a.y / k); }
Point operator*(const Point &a, double k) { return Point(a.x * k, a.y * k); }
double dist(const Point &A, const Point &B) {
return hypot(A.x - B.x, A.y - B.y);
}
double cross(const Point &A, const Point &B) { return A.x * B.y - A.y * B.x; }
double dot(const Point &A, const Point &B) { return A.x * B.x + A.y * B.y; }
double area(const Point &A, const Point &B, const Point &C) {
return cross(B - A, C - A);
}
double dist2(Point p, Point q) { return dot(p - q, p - q); }
Point RotateCCW(Point p, double t) {
return Point(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t));
}
Point ProjectPointLine(Point a, Point b, Point c) {
return a + (b - a) * dot(c - a, b - a) / dot(b - a, b - a);
}
int main() {
Point p1;
double v1;
cin >> p1.x >> p1.y >> v1;
double r1 = p1.mod();
Point p2;
double v2, r2;
cin >> p2.x >> p2.y >> v2 >> r2;
double le = 0, ri = 1e9;
for (int rep = 0; rep < 50; rep++) {
double mid = (le + ri) / 2;
double teta = v1 * mid / r1;
Point current = RotateCCW(p1, teta);
double altura =
cross(Point(0, 0) - p2, current - p2) / (current - p2).mod();
if (altura < 0) altura = -altura;
bool inside = 1;
double aux = dot(current - p2, Point(0, 0) - p2);
if (aux < 0) inside = 0;
aux = dot(p2 - current, Point(0, 0) - current);
if (aux < 0) inside = 0;
if (altura >= r2) inside = 0;
bool ok = 1;
if (!inside) {
if (dist(p2, current) / v2 > mid) ok = 0;
} else {
double d2 = p2.mod();
double d1 = current.mod();
double d = sqrt(d1 * d1 - r2 * r2) + sqrt(d2 * d2 - r2 * r2);
teta = dot(p2, current) / p2.mod() / current.mod();
teta = acos(teta);
teta -= acos(r2 / d1) + acos(r2 / d2);
d += teta * r2;
if (d / v2 > mid) ok = 0;
}
if (ok)
ri = mid;
else
le = mid;
}
printf("%.10f\n", ri);
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int INF = 1e9 + 7;
const int MAXN = 4e5 + 10;
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b;
cin >> a >> b;
while (a && b) {
if (a >= b + b) {
a %= b + b;
} else if (b >= a + a) {
b %= a + a;
} else {
break;
}
}
cout << a << ' ' << b;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 3000;
vector<vector<long long>> mem(3000, vector<long long>(3000, -1));
string s, t;
vector<vector<long long>> sufs(26, vector<long long>(3000)), suft = sufs;
long long cnt = 0;
long long sol(long long i, long long j) {
if (j == 0) return 0;
long long &ans = mem[i][j];
if (ans != -1) return ans;
ans = 1e9;
cnt++;
if (i > 0) {
ans = 1 + sol(i - 1, j);
if (s[i - 1] == t[j - 1]) {
ans = min(ans, sol(i - 1, j - 1));
}
}
long long c = t[j - 1] - 'a';
if (sufs[c][i + 1] > suft[c][j + 1]) {
ans = min(ans, sol(i, j - 1));
}
return ans;
}
signed main() {
long long tt;
cin >> tt;
while (tt--) {
long long n;
cnt = 0;
cin >> n >> s >> t;
for (long long i = 0; i <= n; i++)
for (long long j = 0; j <= n; j++) mem[i][j] = -1;
for (long long i = 0; i <= 25; i++)
for (long long j = 0; j <= n + 1; j++) suft[i][j] = sufs[i][j] = 0;
for (long long i = n; i >= 1; --i) {
for (long long j = 0; j < 26; ++j) {
sufs[j][i] = sufs[j][i + 1];
suft[j][i] = suft[j][i + 1];
}
sufs[s[i - 1] - 'a'][i]++;
suft[t[i - 1] - 'a'][i]++;
}
long long res = sol(n, n);
cout << (res > 1e9 ? -1 : res) << '\n';
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int seg[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int main() {
int a, b;
int ans = 0;
cin >> a >> b;
for (int i = a; i <= b; i++) {
int temp = i;
while (temp > 0) {
ans += seg[temp % 10];
temp /= 10;
}
}
cout << ans << "\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long m[305][305];
long long g[305][305];
vector<long long> dijkstra(long long source) {
typedef struct pair {
long long vertex, distance;
bool operator<(const pair& ot) const {
return !(this->distance < ot.distance);
}
} PAIR;
priority_queue<PAIR> pqp;
vector<bool> visited(n + 1, false);
vector<long long> distance(n + 1, 2000000000);
pqp.push({source, 0});
distance.at(source) = 0;
while (!pqp.empty()) {
PAIR current_visiting = pqp.top();
pqp.pop();
if (visited.at(current_visiting.vertex)) continue;
visited.at(current_visiting.vertex) = true;
for (long long(i) = (1); (i) <= (n); ++(i)) {
if (visited.at(i) || i == current_visiting.vertex) continue;
if (current_visiting.distance + g[current_visiting.vertex][i] <
distance.at(i)) {
distance.at(i) =
current_visiting.distance + g[current_visiting.vertex][i];
pqp.push({i, distance.at(i)});
}
}
}
return distance;
}
void CodeForces_25C() {
typedef struct tri {
long long x, y, z;
bool operator<(const tri& ot) const { return !(this->z < ot.z); }
} TRI;
priority_queue<TRI> pqt;
cin >> n;
for (long long(i) = (1); (i) <= (n); ++(i)) {
for (long long(j) = (1); (j) <= (n); ++(j)) {
g[i][j] = 2000000000;
cin >> m[i][j];
}
}
long long k, a, b, c;
cin >> k;
for (long long(_) = (1); (_) <= (k); ++(_)) {
cin >> a >> b >> c;
for (long long(i) = (1); (i) <= (n); ++(i)) {
for (long long(j) = (1); (j) <= (n); ++(j)) {
if (m[i][a] + c + m[b][j] < m[i][j]) {
m[i][j] = m[i][a] + c + m[b][j];
}
if (m[i][b] + c + m[a][j] < m[i][j]) {
m[i][j] = m[i][b] + c + m[a][j];
}
}
}
long long s = 0;
for (long long(i) = (1); (i) <= (n - 1); ++(i))
for (long long(j) = (i + 1); (j) <= (n); ++(j)) s += m[i][j];
cout << s << " ";
}
cout << endl;
}
int main() {
CodeForces_25C();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int t, n;
pair<int, int> a[10000];
int main() {
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
for (int j = 0; j < n; j++) {
cin >> a[j].first;
a[j].second = j + 1;
}
sort(a, a + n);
if (a[0].first == a[n - 1].first) {
cout << "NO\n";
continue;
}
cout << "YES\n";
for (int j = 1; j < n; j++)
if (a[0].first == a[j].first)
continue;
else
cout << a[0].second << " " << a[j].second << "\n";
if (a[1].first == a[0].first)
for (int j = 1; a[j].first == a[0].first; j++)
cout << a[n - 1].second << " " << a[j].second << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int a[1000005], lst[1000005];
int main() {
ios_base::sync_with_stdio(false);
long long int n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
long double ans = 0;
for (long long i = 1; i <= n; i++) {
ans += (n - i + 1) * (i - lst[a[i]]) * 2 - 1;
lst[a[i]] = i;
}
long double n1 = n;
ans /= n1;
ans /= n1;
cout << fixed << setprecision(10) << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, s;
cin >> t >> s;
vector<char> lis(t);
for (int i = 0; i < t; i++) {
cin >> lis[i];
}
vector<char> exi;
for (int i = 0; i < t; i++) {
if (find(exi.begin(), exi.end(), lis[i]) == exi.end()) {
exi.push_back(lis[i]);
}
}
if (exi.size() < s) {
cout << 0 << endl;
} else {
int n = t;
for (int i = 0; i < exi.size(); i++) {
n = min((int)count(lis.begin(), lis.end(), exi[i]), n);
}
cout << n * s << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
char num[10000];
int i, sum = 0;
scanf("%s", num);
int max = n / 11;
for (i = 0; i < n; i++) {
if (num[i] == '8') sum++;
}
if (max > sum) max = sum;
printf("%d", max);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5010, mod = int(1e9) + 7;
int dp[N][N];
int n, a[N];
int mx[N * 20], sm[20], ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int y = 0; y <= n; y++) {
memset(sm, 0, sizeof(sm));
for (int i = 1; i <= n; i++) {
mx[a[i]] = 0;
mx[a[i] - 1] = 0;
mx[a[i] + 1] = 0;
}
for (int x = 0; x < y; x++) {
mx[a[x]] = max(mx[a[x]], dp[y][x]);
sm[a[x] % 7] = max(sm[a[x] % 7], dp[y][x]);
}
for (int x = y + 1; x <= n; x++) {
dp[x][y] = max(sm[a[x] % 7], max(mx[a[x] - 1], mx[a[x] + 1]));
dp[x][y] = max(dp[x][y], dp[y][0]) + 1;
mx[a[x]] = max(mx[a[x]], dp[x][y]);
sm[a[x] % 7] = max(sm[a[x] % 7], dp[x][y]);
ans = max(ans, dp[x][y]);
}
}
printf("%d", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int p[maxn];
void init(int n) {
for (int i = 1; i <= n; i++) p[i] = i;
}
int f(int x) { return p[x] == x ? x : p[x] = f(p[x]); }
void unite(int x, int y) {
x = f(x);
y = f(y);
p[x] = y;
}
vector<pair<int, int> > edge, edge1, ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, d, cnt = 0;
cin >> n >> m >> d;
int u, v;
init(n);
for (int i = 0; i < m; i++) {
cin >> u >> v;
if (u != 1 && v != 1) {
edge.push_back({u, v});
unite(u, v);
} else
edge1.push_back({u, v}), cnt++;
}
bool flat = cnt < d;
for (auto a : edge1) {
if (f(a.first) != f(a.second)) {
d--;
unite(a.first, a.second);
ans.push_back({a.first, a.second});
}
}
if (d < 0 || flat) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
init(n);
for (auto a : ans) {
unite(a.first, a.second);
cout << a.first << " " << a.second << "\n";
}
for (auto a : edge1) {
if (f(a.first) != f(a.second)) {
unite(a.first, a.second);
cout << a.first << " " << a.second << "\n";
d--;
}
if (d == 0) break;
}
for (auto a : edge) {
if (f(a.first) != f(a.second)) {
unite(a.first, a.second);
cout << a.first << " " << a.second << "\n";
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
pair<int, int> a[N];
int main() {
int n;
scanf("%d", &n);
int r;
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &a[i].first, &a[i].second, &r);
}
string left = "(((1-abs((t-", mid = ")))+abs((abs((t-", right = "))-1)))";
string ans1 = "";
string ans2 = "";
for (int i = 0; i < n - 1; ++i) {
ans1 += "(";
ans2 += "(";
}
for (int i = 0; i < n; ++i) {
string num = to_string(i);
ans1 +=
left + num + mid + num + right + "*" + to_string(a[i].first / 2) + ")";
ans2 +=
left + num + mid + num + right + "*" + to_string(a[i].second / 2) + ")";
if (i != 0) ans1 += ")", ans2 += ")";
if (i != n - 1) ans1 += "+", ans2 += "+";
}
cout << ans1 << endl;
cout << ans2 << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
int n, k, minimal, maximal;
int a[300009], sums[1000009];
inline int min(int v1, int v2) { return v1 < v2 ? v1 : v2; }
inline int max(int v1, int v2) { return v1 > v2 ? v1 : v2; }
void ReadData() {
scanf("%d %d", &n, &k);
minimal = 1000009;
maximal = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
minimal = min(minimal, a[i]);
maximal = max(maximal, a[i]);
}
}
int getSum(int l, int r) {
int l_v = sums[min(1000008, l - 1)];
int r_v = sums[min(1000008, r)];
return r_v - l_v;
}
int answer;
int compare(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; }
void Solve() {
if (minimal <= k + 1) {
answer = minimal;
return;
}
answer = 1;
memset(sums, 0, sizeof(sums));
qsort(a, n, sizeof(int), compare);
int index = 0;
for (int i = 1; i < 1000009; i++) {
sums[i] = sums[i - 1];
while (index < n && a[index] == i) {
sums[i]++;
index++;
}
}
for (int i = k + 1; i <= minimal; i++) {
int p = maximal / i;
int check = 0;
for (int j = 1; j <= p; j++) {
check += getSum(j * i, j * i + k);
}
if (check == n) {
answer = i;
}
}
}
void WriteData() { printf("%d\n", answer); }
int main() {
int QWE = 1;
for (int T = 0; T < QWE; T++) {
ReadData();
Solve();
WriteData();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
double const EPS = 1e-8, PI = acos(-1);
const int N = 2e3 + 9, M = 1e6 + 19, OO = 1e9 + 9;
int main() {
cout << fixed << setprecision(0), ios::sync_with_stdio(false),
cin.tie(nullptr), cout.tie(nullptr);
;
long long a, b, n;
int t;
cin >> t;
while (t--) {
cin >> a >> b >> n;
long long arr[3];
arr[0] = a, arr[1] = b, arr[2] = (a ^ b);
cout << arr[n % 3] << '\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long N, k = 1;
cin >> N;
while (N % k == 0) {
k *= 3;
}
cout << (N + k - 1) / k;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2e4 + 15;
pair<int, int> cards[10005];
bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
if (a.second > b.second) return true;
return ((a.first > b.first) && (a.second == b.second));
}
int main() {
int n, m, ans = 0, ma = -1, t, i, j;
int cnt = 1;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d%d", &cards[i].first, &cards[i].second);
sort(cards, cards + n, cmp);
for (i = 0; i < n; i++) {
if (cnt > 0) {
cnt += cards[i].second - 1;
ans += cards[i].first;
}
if (cnt == 0) break;
}
printf("%d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int N = 2e5 + 1;
int n;
vector<int> adj[N];
int dp[N][2];
int a[N];
int ans = 0;
void dfs(int id, int p) {
int mx[2];
mx[0] = 0, mx[1] = 0;
if (a[id] != 0) dp[id][a[id] - 1] = 1;
for (auto c : adj[id]) {
if (c == p) continue;
dfs(c, id);
dp[id][0] = max(dp[id][0], dp[c][0]);
dp[id][1] = max(dp[id][1], dp[c][1]);
if (a[id] != 0)
dp[id][a[id] - 1] = max(dp[id][a[id] - 1], dp[c][2 - a[id]] + 1);
ans = max(ans, mx[0] + dp[c][1]);
ans = max(ans, mx[1] + dp[c][0]);
if (a[id] != 0) ans = max(ans, mx[2 - a[id]] + dp[c][2 - a[id]] + 1);
mx[0] = max(mx[0], dp[c][0]);
mx[1] = max(mx[1], dp[c][1]);
}
}
void solve() {
cin >> n;
ans = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
adj[i].clear();
adj[i].shrink_to_fit();
dp[i][0] = dp[i][1] = 0;
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
cout << (ans + 2) / 2 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)(1e9 + 1337);
const long long LINF = (long long)(4e18);
const double EPS = (double)(1e-7);
int n;
int a[300010];
int d[300010];
long long do_solve() {
int lid = -1;
for (int i = 0; i < n; ++i) {
d[i] = -1;
for (int j = 1; i + 2 * j < n; ++j) {
if (a[i] == a[i + j] && a[i] == a[i + 2 * j]) {
d[i] = i + 2 * j;
lid = i;
break;
}
}
}
if (lid < 0) {
return 0;
}
long long cur = d[lid];
long long ans = n - cur;
for (int i = lid - 1; i >= 0; --i) {
if (d[i] != -1) {
cur = min(cur, (long long)d[i]);
}
ans += n - cur;
}
return ans;
}
void solve() {
string s;
cin >> s;
n = s.size();
for (int i = 0; i < n; ++i) {
a[i] = s[i] - '0';
}
cout << do_solve();
}
void stress() {
n = 300000;
mt19937 gen(1337);
uniform_int_distribution<int> distr(0, 1);
for (int i = 0; i < n; ++i) {
a[i] = distr(gen);
}
cout << do_solve();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
if (((n - 1) % 14 == 0 || (n - 2) % 14 == 0 || (n - 3) % 14 == 0 ||
(n - 4) % 14 == 0 || (n - 5) % 14 == 0 || (n - 6) % 14 == 0) &&
n > 14)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[25][25], mx[25][30], total[25][30], cm[25][30], dp[1 << 20];
char c[25][25];
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", c[i]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
mx[i][j] = 0;
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
mx[j][c[i][j] - 'a'] = max(mx[j][c[i][j] - 'a'], a[i][j]);
total[j][c[i][j] - 'a'] += a[i][j];
cm[j][c[i][j] - 'a'] |= (1 << i);
}
}
for (int mask = 0; mask < (1 << n); mask++) {
if (mask != ((1 << n) - 1))
dp[mask] = 1 << 30;
else
dp[mask] = 0;
}
for (int mask = (1 << n) - 1; mask >= 0; mask--) {
int ji = (~mask) & -(~mask), j = 0;
while (ji > 1) ji /= 2, j++;
if (j >= n) continue;
for (int i = m - 1; i >= 0; i--) {
dp[mask] = min(dp[mask], dp[mask | (1 << j)] + a[j][i]);
dp[mask] = min(dp[mask], total[i][c[j][i] - 'a'] - mx[i][c[j][i] - 'a'] +
dp[mask | cm[i][c[j][i] - 'a']]);
}
}
printf("%d\n", dp[0]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
bool home = 1;
signed realMain();
signed main() {
home = 0;
if (home) {
freopen("tony_stark", "r", stdin);
} else {
ios::sync_with_stdio(0);
cin.tie(0);
}
realMain();
}
long long mod;
long long add(long long a, long long b) {
a += b;
if (a >= mod) return a - mod;
if (a < 0) return a + mod;
return a;
}
long long mul(long long a, long long b) { return a * (long long)b % mod; }
long long pw(long long a, long long b) {
long long r = 1;
while (b) {
if (b & 1) r = mul(r, a);
a = mul(a, a);
b /= 2;
}
return r;
}
long long dv(long long a, long long b) { return mul(a, pw(b, mod - 2)); }
void baga(long long &a, long long x) { a = add(a, x); }
const long long N = 1234 + 7;
const long long D = 10 + 7;
long long fact[N], ifact[N], dp[N][D][N];
long long comb(long long n, long long k) {
assert(k < D);
long long ret = ifact[k];
for (long long i = n - k + 1; i <= n; i++) ret = mul(ret, i);
return ret;
}
signed realMain() {
long long n, wanda;
cin >> n >> wanda >> mod;
if (n <= 2) {
cout << "1\n";
return 0;
}
fact[0] = 1;
for (long long i = 1; i < N; i++) fact[i] = mul(fact[i - 1], i);
ifact[N - 1] = dv(1, fact[N - 1]);
for (long long i = N - 2; i >= 0; i--) ifact[i] = mul(ifact[i + 1], i + 1);
{ dp[1][0][0] = dp[1][wanda - 1][0] = 1; }
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j <= wanda; j++) {
for (long long k = 1; k <= n; k++) {
baga(dp[i][j][k], dp[i][j][k - 1]);
for (long long t = 1; t <= j && t * k <= i; t++) {
baga(dp[i][j][k], mul(dp[i - t * k][j - t][k - 1],
comb(dp[k][wanda - 1][k - 1] + t - 1, t)));
}
}
}
}
long long ret = dp[n][wanda][(n - 1) / 2];
if (n % 2 == 0) {
ret = add(ret, comb(dp[n / 2][wanda - 1][n / 2 - 1] + 1, 2));
}
cout << ret << "\n";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y;
cin >> n;
int left = 0, right = 0;
for (int i = 0; i < n; i++) {
cin >> x >> y;
if (x > 0)
right++;
else
left++;
}
if (left <= 1 || right <= 1)
cout << "Yes";
else
cout << "No";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
int n, m, k, q;
vector<int> hv[N];
long long ans = 0, ad;
multiset<int> st;
int id[N];
int main() {
scanf("%d%d%d%d", &n, &m, &k, &q);
for (int i = 1, x, y; i <= k; i++) scanf("%d%d", &x, &y), hv[x].push_back(y);
for (int i = 1; i <= n; i++) {
st.clear(), ad = 0;
for (int j = 0; j <= q; j++) st.insert(0), st.insert(m + 1);
for (int j = i; j <= n; j++, ans += ad)
for (auto v : hv[j]) {
auto itl = st.insert(v), itr = itl;
for (int h = q; ~h; h--) id[h] = *itl, --itl;
for (int h = 1; h <= q; h++) ++itr, id[h + q] = *itr;
for (int h = q; h < q + q; h++)
ad += (long long)(id[h + 1] - id[h]) * (id[h - q + 1] - id[h - q]);
}
}
printf("%I64d\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
const long long mod = 1000000007;
const double PI = 3.14159265358979323846;
using namespace std;
bool comp(long long a, long long b) { return a > b; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
t = 1;
while (t--) {
long long n, m;
cin >> n >> m;
long long* a = new long long[n];
for (long long i = 0; i < n; i++) cin >> a[i];
vector<pair<long long, long long>> v1(m), v2;
for (long long i = 0; i < m; i++) cin >> (v1[i].first) >> (v1[i].second);
v2.push_back(v1[m - 1]);
for (long long i = m - 2; i >= 0; i--) {
if (v1[i].second > v2[v2.size() - 1].second) {
v2.push_back(v1[i]);
}
}
reverse(v2.begin(), v2.end());
long long n1 = v2[0].second;
sort(a, a + n1);
vector<long long> b;
for (long long i = 0; i < n1; i++) b.push_back(a[i]);
long long id1 = 0, id2 = n1 - 1;
for (long long i = 1; i < v2.size(); i++) {
long long l = v2[i].second, r = v2[i - 1].second - 1;
for (long long j = r; j >= l; j--) {
a[j] = ((v2[i - 1].first == 1) ? b[id2--] : b[id1++]);
}
}
long long r = v2[v2.size() - 1].second;
for (long long i = 0; i < r; i++) a[i] = b[id1++];
if (v2[v2.size() - 1].first == 1) {
sort(a, a + r);
} else {
sort(a, a + r, comp);
}
for (long long i = 0; i < n; i++) cout << a[i] << " ";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long powmod(long long a, long long b, long long mod) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= 200; i++) {
if (n + (i - 1) / m >= i) {
;
} else {
cout << i - 1 << endl;
return 0;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int x, y;
cin >> x >> y;
int xc = n / 2, yc = n / 2;
if ((x == xc && y == yc) || (x == xc + 1 && y == yc) ||
(x == xc && y == yc + 1) || (x == xc + 1 && y == yc + 1))
cout << "NO";
else
cout << "YES";
return 0;
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.