solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long mod = 1e9 + 7;
const double eps = 1e-8;
const long long MAX = 1e7 + 20;
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
long long BM(long long B, long long P, long long M) {
long long R = 1;
while (P > 0) {
if (P & 1) {
R = (R * B) % M;
}
P /= 2;
B = (B * B) % M;
}
return (long long)R;
}
long long MI(long long x, long long m) { return BM(x, m - 2, m); }
int main() {
long long n, m;
cin >> n >> m;
long long tot = 0;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
tot += x;
}
if (n - 1 == m - tot)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n, k, a[MAX], ans, now;
void solve() {
int t;
scanf("%d", &t);
while (t--) {
ans = now = INF;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i + k <= n; ++i)
if (a[i + k] - a[i] < now)
now = a[i + k] - a[i], ans = (a[i] + a[i + k]) >> 1;
printf("%d\n", ans);
}
}
int main(void) {
solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = 1e18;
long long n, a[100005];
set<long long> ve[100005];
long long A[100005], sz[100005];
void init(long long n) {
for (long long i = 0; i < n + 1; i++) {
sz[i] = 1;
A[i] = i;
}
}
long long find(long long i) {
while (A[i] != i) {
A[i] = A[A[i]];
i = A[i];
}
return i;
}
long long _union(long long xr, long long yr) {
xr = find(xr), yr = find(yr);
if (xr == yr) return -1;
if (sz[xr] < sz[yr]) {
A[xr] = A[yr];
sz[yr] += sz[xr];
} else {
A[yr] = A[xr];
sz[xr] += sz[yr];
}
return 0;
}
long long vis[100005];
long long b[100005];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
cin >> n;
init(n + 2);
for (long long i = 0; i < n + 2; i++) {
vis[i] = 0;
}
vis[n + 1] = 1;
for (long long i = 1; i < n + 1; i++) {
cin >> a[i];
b[a[i]] = i;
}
long long j = 1, flag = 0;
long long k = -1;
for (long long i = 1; i < n + 1; i++) {
if (k == -1) {
k = b[i] + 1;
vis[b[i]] = 1;
if (vis[k] == 1) k = -1;
} else {
if (b[i] == k) {
vis[k] = 1;
k++;
} else {
flag = 1;
break;
}
if (vis[k] == 1) k = -1;
}
}
if (flag)
cout << "No\n";
else
cout << "Yes\n";
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 5;
const int mod = 998244353;
int dp[N][N];
char x[N], s[N];
int main() {
cin >> (x + 1) >> (s + 1);
int lx = strlen(x + 1), ls = strlen(s + 1);
for (int i = 1; i <= ls; i++)
if (x[1] == s[i]) dp[i][i] = 2;
for (int i = ls + 1; i <= lx; i++) dp[i][i] = 2;
for (int i = 2; i <= lx; i++)
for (int l = 1, r = i; r <= lx; l++, r++) {
if (l > ls || x[i] == s[l]) dp[l][r] = (dp[l][r] + dp[l + 1][r]) % mod;
if (r > ls || x[i] == s[r]) dp[l][r] = (dp[l][r] + dp[l][r - 1]) % mod;
}
int ans = 0;
for (int i = ls; i <= lx; i++) ans = (ans + dp[1][i]) % mod;
cout << ans << endl;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long a[N], b[N], cnt;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
b[i] = a[i];
}
sort(a, a + n);
for (int i = 0; i < n; ++i) {
if (a[i] != b[i]) cnt++;
if (cnt > 2) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int N = 407, M = 2407, inf = 40001;
int s, t, tot = 1, a[N], head[N], ver[M], next[M], edge[M], cost[M], flow[N],
dis[N], inq[N], id[N];
std::queue<int> q;
char str[N];
int read() {
int x;
scanf("%d", &x);
return x;
}
void add(int u, int v, int f, int c) {
ver[++tot] = v, next[tot] = head[u], head[u] = tot, edge[tot] = f,
cost[tot] = c;
ver[++tot] = u, next[tot] = head[v], head[v] = tot, edge[tot] = 0,
cost[tot] = -c;
}
int spfa() {
memset(dis + 1, 0x3f, t << 2), q.push(s), inq[s] = 1, dis[s] = 0,
flow[s] = inf, id[t] = -1;
for (int i, u, v; !q.empty();)
for (i = head[u = q.front()], q.pop(), inq[u] = 0; i; i = next[i])
if (edge[i] && dis[v = ver[i]] > dis[u] + cost[i])
if (dis[v] = dis[u] + cost[i], id[v] = i,
flow[v] = std::min(flow[u], edge[i]), !inq[v])
q.push(v), inq[v] = 1;
return dis[t] < 0;
}
int main() {
int n1 = read(), n2 = read(), m = read(), r = read(), b = read(), ans = 0;
s = n1 + n2 + 1, t = s + 1;
scanf("%s", str + 1);
for (int i = 1; i <= n1; ++i)
if (str[i] == 'R')
ans += inf, add(i, t, 1, -inf), add(i, t, inf, 0);
else if (str[i] == 'B')
ans += inf, add(s, i, 1, -inf), add(s, i, inf, 0);
else
add(s, i, inf, 0), add(i, t, inf, 0);
scanf("%s", str + 1);
for (int i = 1; i <= n2; ++i)
if (str[i] == 'B')
ans += inf, add(i + n1, t, 1, -inf), add(i + n1, t, inf, 0);
else if (str[i] == 'R')
ans += inf, add(s, i + n1, 1, -inf), add(s, i + n1, inf, 0);
else
add(s, i + n1, inf, 0), add(i + n1, t, inf, 0);
for (int i = 1, u, v; i <= m; ++i)
u = read(), v = read(), add(u, v + n1, 1, b), add(v + n1, u, 1, r);
for (int p; spfa();)
for (ans += dis[t] * flow[t], p = t; p ^ s; p = ver[id[p] ^ 1])
edge[id[p]] -= flow[t], edge[id[p] ^ 1] += flow[t];
if (ans >= inf) return puts("-1"), 0;
printf("%d\n", ans);
for (int i = 1, st = 4 * (n1 + n2) + 1; i <= m; ++i)
putchar(edge[st + i * 4 - 2] ? 'B' : edge[st + i * 4] ? 'R' : 'U');
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int ans1 = 0, ans2 = 0;
int main() {
cin >> n >> s;
int p = 0;
while (p < n) {
if ((p % 2 == 0 && s[p] == '1') || (p % 2 == 1 && s[p] == '0'))
if (p < n - 1 && s[p + 1] == s[p]) {
ans1++;
p++;
} else
ans1++;
p++;
}
p = 0;
while (p < n) {
if ((p % 2 == 0 && s[p] == '0') || (p % 2 == 1 && s[p] == '1'))
if (p < n - 1 && s[p + 1] == s[p]) {
ans2++;
p++;
} else
ans2++;
p++;
}
cout << min(ans1, ans2) << '\n';
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
long long a[n];
long long i, j;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (i = 0; i < n; i++) {
long long y = 1;
for (j = 0; j < 33; j++) {
long long p, q;
p = a[i] + y;
q = a[i] + 2 * y;
if (binary_search(a, a + n, p) && binary_search(a, a + n, q)) {
cout << 3 << endl;
cout << a[i] << " " << p << " " << q << endl;
return 0;
}
y = y * 2;
}
}
for (i = 0; i < n; i++) {
long long y = 1;
for (j = 0; j < 33; j++) {
long long p, q;
p = a[i] + y;
if (binary_search(a, a + n, p)) {
cout << 2 << endl;
cout << a[i] << " " << p;
return 0;
}
y = y * 2;
}
}
cout << "1" << endl << a[0];
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000 + 20;
int n, m, cnt, cntp, deg[N][N], vis[N][N];
int dir[4][2] = {1, 0, 0, -1, -1, 0, 0, 1};
char money[4] = {'v', '<', '^', '>'};
char a[N][N];
struct node {
int deg;
int x, y;
node(int a, int b, int t) { x = a, y = b, deg = t; }
bool operator<(struct node t) const { return deg > t.deg; }
};
queue<struct node> q;
int main() {
while (~scanf("%d%d", &m, &n)) {
getchar();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) scanf("%c", &a[i][j]);
getchar();
}
memset(vis, 0, sizeof(vis));
memset(deg, 0, sizeof(deg));
cnt = 0, cntp = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (a[i][j] == '.') {
cntp++;
struct node temp(i, j, 0);
for (int k = 0; k < 4; k++) {
int tx = i + dir[k][0], ty = j + dir[k][1];
if (tx >= 0 && tx < m && ty >= 0 && ty < n) {
if (a[tx][ty] == '.') temp.deg++;
}
}
if (temp.deg == 1) q.push(temp);
deg[i][j] = temp.deg;
}
while (!q.empty()) {
struct node temp = q.front();
q.pop();
if (!vis[temp.x][temp.y]) {
for (int k = 0; k < 4; k++) {
int tx = temp.x + dir[k][0], ty = temp.y + dir[k][1];
if (tx >= 0 && tx < m && ty >= 0 && ty < n && a[tx][ty] == '.' &&
!vis[tx][ty]) {
a[tx][ty] = money[k], a[temp.x][temp.y] = money[((k + 2) % 4)];
vis[tx][ty] = 1, vis[temp.x][temp.y] = 1;
for (int u = 0; u < 4; u++) {
int ttx = tx + dir[u][0], tty = ty + dir[u][1];
if (ttx >= 0 && ttx < m && tty >= 0 && tty < n &&
a[ttx][tty] == '.' && !vis[ttx][tty]) {
deg[ttx][tty]--;
if (deg[ttx][tty] == 1) {
struct node temp(ttx, tty, 1);
q.push(temp);
}
}
}
cnt += 2;
}
}
}
}
if (cnt != cntp)
printf("Not unique\n");
else
for (int i = 0; i < m; i++) printf("%s\n", a[i]);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int c = 0;
long long n, x;
cin >> n >> x;
for (int i = 1; i <= n; i++) {
if (x % i == 0 && x / i <= n) c++;
}
printf("%d", c);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int to, val;
node(int a, int b) : to(a), val(b) {}
};
vector<node> edge[300000 + 5];
int mark[300000 + 5], Next[300000 * 2 + 5][26], size[300000 + 5],
dep[300000 + 5], ans[300000 + 5];
int n, cnt;
int Merge(int x, int y) {
if (x == 0 || y == 0) return x | y;
int i, z = ++cnt;
size[z] = 1;
for (i = 0; i <= 25; i++) {
Next[z][i] = Merge(Next[x][i], Next[y][i]);
size[z] += size[Next[z][i]];
}
return z;
}
void dfs(int now) {
mark[now] = 1;
int i, l = edge[now].size();
size[now] = 1;
for (i = 0; i <= l - 1; i++) {
int v = edge[now][i].to, c = edge[now][i].val;
if (mark[v]) continue;
Next[now][c] = v;
dep[v] = dep[now] + 1;
dfs(v);
size[now] += size[v];
}
ans[dep[now]] += size[now];
int tmp = 0;
cnt = n;
for (i = 0; i <= 25; i++) tmp = Merge(tmp, Next[now][i]);
ans[dep[now]] -= max(size[tmp], 1);
return;
}
int main() {
int i, u, v;
char c[2];
scanf("%d", &n);
for (i = 1; i <= n - 1; i++)
scanf("%d%d%s", &u, &v, &c), edge[u].push_back(node(v, c[0] - 'a')),
edge[v].push_back(node(u, c[0] - 'a'));
dep[1] = 1;
dfs(1);
int ansi = 0;
for (i = 1; i <= n; i++)
if (ans[i] > ans[ansi]) ansi = i;
printf("%d\n%d\n", n - ans[ansi], ansi);
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int a, b;
cin >> a >> b;
if (a > b) {
long long int d = a - b;
if (d % 2 == 0)
cout << "1" << endl;
else
cout << "2" << endl;
} else if (a == b)
cout << "0" << endl;
else {
long long int d = b - a;
if (d % 2 == 0)
cout << "2" << endl;
else
cout << "1" << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
long long cnt = 0;
for (int i = n - 1; i > 0; i--) {
if (a[i] < a[i - 1]) cnt += a[i - 1] - a[i];
}
cout << cnt << endl;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < (t); i++) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int32_t main() {
int n;
cin >> n;
vector<int> p(n + 1);
set<int> s;
set<int> numbers;
for (int i = 1; i <= n; i++) {
s.insert(i);
numbers.insert(i);
}
int x, y;
while (s.size() >= 2) {
auto u = s.begin();
auto v = (next(s.begin()));
cout << "? " << *u << ' ' << *v << endl;
cin >> x;
cout << "? " << *v << ' ' << *u << endl;
cin >> y;
if (x > y) {
p[*u] = x;
s.erase(u);
numbers.erase(x);
} else {
p[*v] = y;
s.erase(v);
numbers.erase(y);
}
}
cout << "! ";
for (int i = 1; i <= n; i++) {
x = p[i];
if (!x) {
cout << *numbers.begin() << ' ';
} else {
cout << x << ' ';
}
}
cout << endl;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
vector<long long> t(n);
map<long long, long long> mapa;
long long sum = 0;
for (long long& e : t) {
cin >> e;
mapa[e]++;
sum += e;
}
long long wynik = 0;
for (long long e : t) {
if ((2 * sum) % n == 0) {
wynik += mapa[(2 * sum) / n - e];
if ((2 * sum) / n - e == e) wynik--;
}
}
wynik /= 2;
cout << wynik << "\n";
}
int main() {
int Q;
cin >> Q;
while (Q--) solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long t = 1;
while (t--) {
long long n, ans = 0;
cin >> n;
vector<long long> a(n), cnt(20);
for (auto &e : a) cin >> e;
for (long long i = 0; i < n; i++)
for (long long j = 0; j < 20; j++)
if (a[i] & (1 << j)) cnt[j]++;
for (long long i = 0; i < n; i++) {
long long count = 0;
for (long long j = 0; j < 20; j++)
if (cnt[j]) count += (1 << j), cnt[j]--;
ans += pow(count, 2);
}
cout << ans << "\n";
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long int n, m;
cin >> m >> n;
if (m > n || (m % 2 != n % 2))
cout << -1 << endl;
else if (n == m) {
if (n == 0)
cout << 0 << endl;
else
cout << 1 << endl << n << endl;
} else {
long long int x = (n - m) / 2;
if (m & x)
cout << 3 << endl << m << " " << x << " " << x << endl;
else
cout << 2 << endl << (m ^ x) << " " << x << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2> &p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a / gcd(a, b)) * b;
}
struct node {
bool visited = false;
vector<node *> adj;
};
void dfs(long long int &value, node *point) {
if (point->visited) return;
point->visited = true;
value *= 2;
if (point->adj.size() == 0) return;
for (long long int i = 0; i < point->adj.size(); i++)
dfs(value, point->adj[i]);
}
void solve() {
long long int n, m, v1, v2, value = 1;
cin >> n >> m;
vector<node> graph(n);
for (long long int i = 0; i < m; i++) {
cin >> v1 >> v2;
v1--;
v2--;
graph[v1].adj.push_back(&graph[v2]);
graph[v2].adj.push_back(&graph[v1]);
}
for (long long int i = 0; i < n; i++)
if (!graph[i].visited) {
graph[i].visited = true;
for (long long int j = 0; j < graph[i].adj.size(); j++)
dfs(value, graph[i].adj[j]);
}
cout << value;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, pri[N], tt, minp[N], mx[N], op[N], cnt[N], p[N];
bool vis[N];
vector<pair<int, int> > fac(int x) {
vector<pair<int, int> > ret;
while (x > 1) {
int y = minp[x];
ret.push_back(make_pair(y, 0));
while (x % y == 0) ++ret.back().second, x /= y;
}
return ret;
}
int main() {
for (int i = 2; i < N; i++)
if (!vis[i]) {
pri[++tt] = i;
minp[i] = i;
for (int j = i + i; j < N; j += i) {
vis[j] = 1;
if (!minp[j]) minp[j] = i;
}
}
cin >> n;
for (int i = 1; i <= n; i++) p[i] = gi();
sort(p + 1, p + n + 1, greater<int>());
for (int i = 1; i <= n; i++) {
if (!mx[p[i]])
mx[p[i]] = cnt[p[i]] = 1, op[i] = 1;
else {
auto vec = fac(p[i] - 1);
for (auto x : vec) {
if (mx[x.first] < x.second)
mx[x.first] = x.second, cnt[x.first] = 1;
else if (mx[x.first] == x.second)
++cnt[x.first];
}
}
}
int ans = 1;
bool fl = 0;
for (int i = 1; i < N; i++) {
for (int j = 1; j <= mx[i]; j++) ans = 1ll * ans * i % mod;
}
for (int i = 1; i <= n && !fl; i++)
if (op[i]) {
fl |= (mx[p[i]] > 1 || cnt[p[i]] > 1);
} else {
auto vec = fac(p[i] - 1);
bool all = 1;
for (auto x : vec) all &= (mx[x.first] > x.second || cnt[x.first] > 1);
fl |= all;
}
if (fl) ans = (ans + 1) % mod;
cout << ans;
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 2014;
long long d1[2 * NMAX], d2[2 * NMAX], sol[2];
pair<int, int> v[2];
int a[NMAX][NMAX];
inline void Update(const int c, const int i, const int j, const long long val) {
if (val > sol[c]) {
sol[c] = val;
v[c].first = i;
v[c].second = j;
}
}
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
sol[0] = sol[1] = -1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
int x;
cin >> a[i][j];
d1[i + j] += a[i][j];
d2[i - j + n] += a[i][j];
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
Update((i + j) & 1, i, j, d1[i + j] + d2[i - j + n] - a[i][j]);
cout << sol[0] + sol[1] << "\n";
if (v[0] > v[1]) swap(v[0], v[1]);
cout << v[0].first << " " << v[0].second << " ";
cout << v[1].first << " " << v[1].second << "\n";
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y = -1, i = 0;
long long k, cnt = 0;
cin >> n >> k;
while (i < n) {
cin >> x;
if (y < x) {
if (i != 0) cnt = 1;
y = x;
} else
cnt++;
if (cnt == k) break;
i++;
}
cout << y;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int L = (1 << 20) + 1;
char buf[L], *S, *T, c;
char getchar() {
if (__builtin_expect(S == T, 0)) {
T = (S = buf) + fread(buf, 1, L, stdin);
return (S == T ? EOF : *S++);
}
return *S++;
}
int inp() {
int x = 0, f = 1;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return x * f;
}
unsigned inpu() {
unsigned x = 0;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return x;
}
long long inp_ll() {
long long x = 0;
int f = 1;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return x * f;
}
char B[25], *outs = B + 20, *outr = B + 20;
template <class T>
inline void print(register T a, register char x = 0) {
if (x) *--outs = x, x = 0;
if (!a)
*--outs = '0';
else
while (a) *--outs = (a % 10) + 48, a /= 10;
if (x) *--outs = x;
fwrite(outs, outr - outs, 1, stdout);
outs = outr;
}
}; // namespace io
using io ::inp;
using io ::inp_ll;
using io ::inpu;
using io ::print;
using i32 = int;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
long long power(long long a, long long b, long long p) {
if (!b) return 1;
long long t = power(a, b / 2, p);
t = t * t % p;
if (b & 1) t = t * a % p;
return t;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <class T>
inline void freshmin(T &a, const T &b) {
if (a > b) a = b;
}
template <class T>
inline void freshmax(T &a, const T &b) {
if (a < b) a = b;
}
const int MAXN = 500010;
const int MAXK = 30;
const int MAXP = 10000000;
const int MOD = 1000000007;
const f80 MI = f80(1) / MOD;
const long long INF = 10000000000000000LL;
const long long inf = 5000000000000LL;
long long sum[MAXK];
multiset<int> H[MAXK];
int main() {
int n;
scanf("%d", &n);
int cnt = 0;
while (n--) {
char t;
int x;
scanf(" %c%d", &t, &x);
if (t == '+') {
++cnt;
for (int k = 0; k < MAXK; ++k) {
if (x < 1 << (k + 1)) {
sum[k] += x;
H[k].insert(x);
break;
}
}
} else if (t == '-') {
--cnt;
for (int k = 0; k < MAXK; ++k) {
if (x < 1 << (k + 1)) {
sum[k] -= x;
H[k].erase(H[k].lower_bound(x));
break;
}
}
}
long long cur = 0;
int ans = 0;
for (int k = 0; k < MAXK; ++k) {
if (H[k].empty()) continue;
if (*H[k].begin() > cur * 2) ++ans;
cur += sum[k];
}
printf("%d\n", cnt - ans);
}
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
vector<int> sm;
vector<vector<int> > g;
int dfs(int v, int pr = -1, int h = 0) {
int sz = 1;
sm[v] = h;
for (int i = 0; i < g[v].size(); i++) {
if (g[v][i] == pr) continue;
int k = dfs(g[v][i], v, h + 1);
sz += k;
sm[v] -= k;
}
return sz;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
g.resize(n);
sm.resize(n);
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(0);
sort(sm.rbegin(), sm.rend());
long long ans = 0;
for (int i = 0; i < k; i++) ans += sm[i];
cout << ans;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, n, x, y, z;
int main() {
string s1, s2;
cin >> s1 >> s2;
vector<string> v;
if (s1.size() != s2.size()) {
cout << "NO" << endl;
} else {
int def = 0;
for (int i = 0; i < s1.size(); i++) {
if (i < s2.size() && s1[i] != s2[i]) {
def++;
}
}
if (def <= 2) {
if (def == 0) {
cout << "YES" << endl;
} else if (def == 1) {
cout << "NO" << endl;
} else {
int a, b, c, d, x = 0;
for (int i = 0; i < s1.size(); i++) {
if (i < s2.size() && s1[i] != s2[i]) {
if (x == 0) {
a = s1[i];
b = s2[i];
x = 1;
} else {
c = s1[i];
d = s2[i];
}
}
}
if (a == d && c == b)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
} else
cout << "NO" << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int a[N], g[N][N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
g[u][v] = g[v][u] = w;
}
double ans = 0.0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (g[i][j] == 0) continue;
if ((double)(a[i] + a[j]) / (g[i][j]) > ans) {
ans = (double)(a[i] + a[j]) / (g[i][j]);
}
}
}
printf("%.10f\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1e5 + 5;
pair<int, int> tsh[MAXN];
int ans[MAXN];
bool cmp(pair<int, int> A, pair<int, int> B) {
if (A.first != B.first) return A.first > B.first;
return A.second < B.second;
}
struct node;
typedef node* Treap;
struct node {
int prio;
int val, buy;
int addBuy, addVal;
int id;
Treap l;
Treap r;
};
Treap root;
int getRand() { return (rand() << 16) + rand(); }
Treap resetNode(Treap n, Treap l, Treap r, int val, int buy, int id, int pri) {
if (!n) return n;
n->l = l;
n->r = r;
n->prio = pri;
n->val = val;
n->buy = buy;
n->addVal = n->addBuy = 0;
n->id = id;
return n;
}
void shift(Treap a) {
if (!a) return;
Treap l = a->l;
Treap r = a->r;
if (l) {
l->addBuy += a->addBuy;
l->addVal += a->addVal;
l->buy += a->addBuy;
l->val += a->addVal;
}
if (r) {
r->addBuy += a->addBuy;
r->addVal += a->addVal;
r->buy += a->addBuy;
r->val += a->addVal;
}
a->addBuy = a->addVal = 0;
}
void addTo(Treap a, int val, int buy) {
if (!a) return;
a->val += val;
a->addVal += val;
a->buy += buy;
a->addBuy += buy;
}
pair<Treap, Treap> split(Treap a, int val) {
if (!a) return make_pair(nullptr, nullptr);
shift(a);
if (a->val < val) {
pair<Treap, Treap> tmp = split(a->r, val);
Treap l = resetNode(a, a->l, tmp.first, a->val, a->buy, a->id, a->prio);
Treap r = tmp.second;
return make_pair(l, r);
} else {
pair<Treap, Treap> tmp = split(a->l, val);
Treap l = tmp.first;
Treap r = resetNode(a, tmp.second, a->r, a->val, a->buy, a->id, a->prio);
return make_pair(l, r);
}
}
Treap merge(Treap a, Treap b) {
if (b == nullptr) return a;
if (a == nullptr) return b;
shift(a);
shift(b);
Treap ans = nullptr;
if (a->prio < b->prio)
ans = resetNode(a, a->l, merge(a->r, b), a->val, a->buy, a->id, a->prio);
else
ans = resetNode(b, merge(a, b->l), b->r, b->val, b->buy, b->id, b->prio);
return ans;
}
int getSize(Treap a) {
if (!a) return 0;
cout << "ONE SIZE:" << a->val << endl;
return 1 + getSize(a->l) + getSize(a->r);
}
Treap insert(Treap a, Treap b) {
if (!a) return b;
pair<Treap, Treap> tmp = split(a, b->val);
return merge(merge(tmp.first, b), tmp.second);
}
void storeAnswers(Treap a) {
if (!a) return;
shift(a);
ans[a->id] = a->buy;
storeAnswers(a->l);
storeAnswers(a->r);
}
Treap addAllFirstToSecond(Treap a, Treap to) {
if (!a) return to;
shift(a);
if (!to) return a;
to = addAllFirstToSecond(a->l, to);
to = addAllFirstToSecond(a->r, to);
to = insert(to,
resetNode(a, nullptr, nullptr, a->val, a->buy, a->id, a->prio));
return to;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> tsh[i].second >> tsh[i].first;
sort(tsh, tsh + n, cmp);
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int b;
cin >> b;
Treap n = resetNode(new node(), nullptr, nullptr, b, 0, i, getRand());
root = insert(root, n);
}
for (int i = 0; i < n; i++) {
int c = tsh[i].second;
pair<Treap, Treap> tmp = split(root, c);
Treap l = tmp.first;
Treap r = tmp.second;
addTo(r, -c, 1);
pair<Treap, Treap> tmp2 = split(r, c);
Treap rightL = tmp2.first;
Treap rightR = tmp2.second;
l = addAllFirstToSecond(rightL, l);
root = merge(l, rightR);
}
storeAnswers(root);
for (int i = 0; i < k; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
void solve() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
if (a == 0 && d == 0) {
if (b % 2) {
cout << "Tidak Ya Tidak Tidak\n";
} else
cout << "Tidak Tidak Ya Tidak\n";
return;
}
if (b == 0 && c == 0) {
if (a % 2) {
cout << "Ya Tidak Tidak Tidak\n";
} else
cout << "Tidak Tidak Tidak Ya\n";
return;
}
if ((a + b) % 2)
cout << "Ya Ya Tidak Tidak\n";
else
cout << "Tidak Tidak Ya Ya\n";
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef struct node {
int vis, l, r;
} s;
long long a[200005];
s f[200005];
int main() {
long long n, k, a, c;
cin >> n >> k;
c = 3 * n - 2;
for (long long i = 0; i < k; i++) {
cin >> a;
if (!f[a].vis) {
f[a].vis = 1;
c--;
}
if (a > 1 && f[a - 1].vis && !f[a].l) {
f[a].l = 1;
c--;
}
if (a < n && f[a + 1].vis && !f[a].r) {
f[a].r = 1;
c--;
}
}
cout << c;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1 << 23, D = 1e6;
int tr[N * 2], mod[N * 2];
void push(int v) {
if (v < N) {
mod[v * 2] += mod[v];
mod[v * 2 + 1] += mod[v];
}
tr[v] += mod[v];
mod[v] = 0;
}
void rel(int v) { tr[v] = max(tr[v * 2], tr[v * 2 + 1]); }
void add(int cl, int cr, int d, int v = 1, int l = 0, int r = N) {
if (cr <= l || r <= cl) {
push(v);
return;
}
if (cl <= l && r <= cr) {
mod[v] += d;
push(v);
return;
}
push(v);
add(cl, cr, d, v * 2, l, (l + r) / 2);
add(cl, cr, d, v * 2 + 1, (l + r) / 2, r);
rel(v);
}
int getmx(int cl, int cr, int v = 1, int l = 0, int r = N) {
push(v);
if (cr <= l || r <= cl) return -N;
if (cl <= l && r <= cr) return tr[v];
return max(getmx(cl, cr, v * 2, l, (l + r) / 2),
getmx(cl, cr, v * 2 + 1, (l + r) / 2, r));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, r, cx, cy;
cin >> n >> r;
vector<pair<int, int>> a(n);
for (int i = 0; i < (n); i++) {
cin >> cx >> cy;
a[i] = {cx - cy, cx + cy + 2 * D};
assert(a[i].second >= 0);
}
r = r * 2 + 1;
sort((a).begin(), (a).end());
int p = 0, ans = 0;
for (int i = 0; i < (n); i++) {
while (a[i].first - a[p].first >= r) {
add(a[p].second, a[p].second + r, -1);
p++;
}
add(a[i].second, a[i].second + r, 1);
ans = max(ans, getmx(0, N));
}
cout << ans << '\n';
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n;
bool used[30][6];
int found[N], type[N];
bool subtracted[N];
int remain[10], org_remain[10];
vector<pair<char, char> > card;
map<char, int> mp;
string s;
vector<char> v;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
if (!used[s[0] - 'A'][s[1] - '0']) {
used[s[0] - 'A'][s[1] - '0'] = 1;
card.push_back({s[0], s[1]});
}
}
v = {'R', 'G', 'B', 'Y', 'W'};
for (int i = 1; i <= 5; i++) v.push_back('0' + i);
for (int i = 0; i < 10; i++) {
mp[v[i]] = i;
}
for (auto x : card) {
for (int i = 0; i < v.size(); i++) {
if (x.first == v[i]) remain[i]++;
if (x.second == v[i]) remain[i]++;
}
}
for (int i = 0; i < v.size(); i++) org_remain[i] = remain[i];
if (card.size() == 1) return cout << 0, 0;
int ans = 11;
for (int mask = 0; mask < (1 << 10); mask++) {
memset(found, 0, sizeof found);
memset(subtracted, 0, sizeof subtracted);
memset(type, 0, sizeof type);
for (int i = 0; i < 10; i++) remain[i] = org_remain[i];
vector<char> tmp;
vector<char> dm = {'R', 'Y', 'W', '1', '3', '5'};
for (int len = 0; len < 10; len++) {
if ((mask >> len) & 1) tmp.push_back(v[len]);
}
for (int i = 0; i < tmp.size(); i++) {
for (int j = 0; j < card.size(); j++) {
if (subtracted[j]) continue;
if (found[j] == 2 && !subtracted[j]) {
subtracted[j] = 1;
continue;
}
char c = card[j].first, num = card[j].second;
if (c == tmp[i]) {
found[j]++;
type[j] = 1;
} else if (num == tmp[i]) {
found[j]++;
type[j] = 2;
}
}
}
for (int j = 0; j < card.size(); j++) {
if (found[j] == 2) {
remain[mp[card[j].first]]--;
remain[mp[card[j].second]]--;
}
}
for (int cc = 0; cc < card.size(); cc++) {
for (int j = 0; j < card.size(); j++) {
if (found[j] == 2) continue;
if (found[j] == 1 && type[j] == 1 && remain[mp[card[j].first]] == 1) {
remain[mp[card[j].first]]--;
remain[mp[card[j].second]]--;
found[j] = 2;
}
if (found[j] == 1 && type[j] == 2 && remain[mp[card[j].second]] == 1) {
remain[mp[card[j].first]]--;
remain[mp[card[j].second]]--;
found[j] = 2;
}
}
int gay = 0;
for (int j = 0; j < card.size(); j++) {
if (found[j] != 2) gay++;
}
if (gay <= 1) {
ans = min(ans, (int)tmp.size());
}
}
}
cout << ans;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int dp[2][(int)(571)][(int)(571)], p[(int)(571)], n, k;
int main() {
cin >> n >> k;
p[0] = 1;
for (int i = 1; i <= 500; i++) p[i] = (2 * p[i - 1]) % 1000000007;
for (int i = 0; i <= n; i++) dp[(n + 1) & 1][k][i] = 1;
for (int x = n; x >= 1; x--)
for (int count = k; count >= 0; count--)
for (int last = x - 1; last >= 0; last--)
dp[x & 1][count][last] =
((long long)dp[(x + 1) & 1][count][last] * p[last] +
(long long)dp[(x + 1) & 1][count + 1][x] * p[last] % 1000000007 *
(p[x - last] - 1)) %
1000000007;
cout << dp[1][0][0] << '\n';
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, cont = 0;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int tmp = n - i;
if (tmp % i == 0) cont++;
}
cout << cont << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int x, y;
int max1, min1;
if (m == 0) {
cout << (n - 1);
return 0;
}
max1 = 0;
min1 = 10000000;
for (int i = 0; i < m; i++) {
cin >> x >> y;
if (x > y) swap(x, y);
max1 = max(max1, x);
min1 = min(min1, y);
}
cout << max(0, min1 - max1);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, n, i;
while (cin >> a >> b >> n) {
for (i = -1000; i <= 1000; i++) {
if (a * pow(i, n) == b) break;
}
if (a * pow(i, n) == b)
cout << i << endl;
else
cout << "No solution\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long xa, ya, xb, yb, xc, yc;
cin >> xa >> ya >> xb >> yb >> xc >> yc;
long long firstX = xb - xa;
long long firstY = yb - ya;
long long secondX = xc - xb;
long long secondY = yc - yb;
long long res = (firstX * secondY) - (secondX * firstY);
if (res < 0) cout << "RIGHT";
if (!res) cout << "TOWARDS";
if (res > 0) cout << "LEFT";
return 0;
;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
double pi = 2 * acos(0.0);
while (t--) {
long long n, r;
cin >> n >> r;
double x = sin((pi) / n);
cout << setprecision(8) << r * x / (1 - x);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int suit, val;
};
node nodes[55];
char s[55];
int dp[55][5][14][5][14][5][14], n;
int cal(int ind, int suit1, int val1, int suit2, int val2, int suit3, int val3);
int main() {
int i, j, k, l;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%s", s);
if (isalpha(s[0])) {
if (s[0] == 'T') {
nodes[i].val = 10;
}
if (s[0] == 'J') {
nodes[i].val = 11;
}
if (s[0] == 'Q') {
nodes[i].val = 12;
}
if (s[0] == 'K') {
nodes[i].val = 13;
}
if (s[0] == 'A') {
nodes[i].val = 1;
}
} else
nodes[i].val = s[0] - '0';
if (s[1] == 'S') {
nodes[i].suit = 1;
}
if (s[1] == 'D') {
nodes[i].suit = 2;
}
if (s[1] == 'H') {
nodes[i].suit = 3;
}
if (s[1] == 'C') {
nodes[i].suit = 4;
}
}
int flag = 0;
memset(dp, -1, sizeof(dp));
if (n < 3) {
if (n <= 1) flag = 1;
if (n == 2) {
if (nodes[2].suit == nodes[1].suit || nodes[2].val == nodes[1].val)
flag = 1;
else
flag = 0;
}
} else {
flag = cal(n, nodes[n - 2].suit, nodes[n - 2].val, nodes[n - 1].suit,
nodes[n - 1].val, nodes[n].suit, nodes[n].val);
}
if (flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
int cal(int ind, int suit1, int val1, int suit2, int val2, int suit3,
int val3) {
if (ind < 3) return 0;
if (ind == 3) {
if (suit3 == suit2 || val3 == val2) {
if (suit3 == suit1 || val3 == val1) return 1;
return 0;
}
return 0;
}
if (dp[ind][suit1][val1][suit2][val2][suit3][val3] != -1)
return dp[ind][suit1][val1][suit2][val2][suit3][val3];
int ret;
if (suit3 == suit2 || val3 == val2) {
ret = cal(ind - 1, nodes[ind - 3].suit, nodes[ind - 3].val, suit1, val1,
suit3, val3);
if (ret) return dp[ind][suit1][val1][suit2][val2][suit3][val3] = 1;
}
if (suit3 == nodes[ind - 3].suit || val3 == nodes[ind - 3].val) {
ret = cal(ind - 1, suit3, val3, suit1, val1, suit2, val2);
if (ret) return dp[ind][suit1][val1][suit2][val2][suit3][val3] = 1;
}
return dp[ind][suit1][val1][suit2][val2][suit3][val3] = 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int n, N;
cin >> n;
N = n / 2;
long long int a[n + 1], b[N + 1];
for (int i = 1; i <= N; i++) cin >> b[i];
int l, r;
long long int mn, mx;
l = N;
r = n - N + 1;
mn = b[N] / 2;
mx = b[N] - mn;
a[l] = mn;
a[r] = mx;
for (int i = N - 1; i > 0; i--) {
long long int num = a[l] + a[r];
l = i;
r = n - l + 1;
if (b[i] >= num) {
a[l] = mn;
a[r] = b[i] - a[l];
mn = a[l];
mx = a[r];
} else {
a[r] = mx;
a[l] = b[i] - a[r];
mn = a[l];
mx = a[r];
}
}
for (int i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long double EPS = 1e-9;
long long MOD2 = 998244353;
long long binpow(long long b, long long p, long long mod) {
long long ans = 1;
b %= mod;
for (; p; p >>= 1) {
if (p & 1) ans = ans * b % mod;
b = b * b % mod;
}
return ans;
}
long long gcd(long long a, long long b) {
if (b > a) {
return gcd(b, a);
}
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long expo(long long a, long long b, long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b = b >> 1;
}
return res;
}
void extendgcd(long long a, long long b, long long *v) {
if (b == 0) {
v[0] = 1;
v[1] = 0;
v[2] = a;
return;
}
extendgcd(b, a % b, v);
long long x = v[1];
v[1] = v[0] - v[1] * (a / b);
v[0] = x;
return;
}
long long mminv(long long a, long long b) {
long long arr[3];
extendgcd(a, b, arr);
return arr[0];
}
long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); }
bool revsort(long long a, long long b) { return a > b; }
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
long long combination(long long n, long long r, long long m, long long *fact,
long long *ifact) {
long long val1 = fact[n];
long long val2 = ifact[n - r];
long long val3 = ifact[r];
return (((val1 * val2) % m) * val3) % m;
}
vector<long long> sieve(int n) {
int *arr = new int[n + 1]();
vector<long long> vect;
for (int i = 2; i <= n; i++)
if (arr[i] == 0) {
vect.push_back(i);
for (int j = 2 * i; j <= n; j += i) arr[j] = 1;
}
return vect;
}
long long mod_add(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a + b) % m) + m) % m;
}
long long mod_mul(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a * b) % m) + m) % m;
}
long long mod_sub(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a - b) % m) + m) % m;
}
long long mod_div(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (mod_mul(a, mminvprime(b, m), m) + m) % m;
}
long long phin(long long n) {
long long number = n;
if (n % 2 == 0) {
number /= 2;
while (n % 2 == 0) n /= 2;
}
for (long long i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) {
while (n % i == 0) n /= i;
number = (number / i * (i - 1));
}
}
if (n > 1) number = (number / n * (n - 1));
return number;
}
void pre() {}
void solve() {
long long n;
cin >> n;
vector<long long> arr(n), occ(n + 1);
for (long long i = 0; i < (n); ++i) {
cin >> arr[i];
occ[arr[i]]++;
}
sort((arr).begin(), (arr).end());
vector<pair<long long, long long> > extra;
long long prev = 0, last = 0, res = 1;
for (long long i = 0; i <= n; i++) {
long long lesser = lower_bound((arr).begin(), (arr).end(), i) - arr.begin();
if (res == -1 or lesser < i) {
res = -1;
cout << "-1 ";
continue;
}
long long ans = occ[i] + prev;
if (occ[i] > 1) {
extra.push_back({i, occ[i] - 1});
}
if (occ[i] == 0) {
if (!extra.empty()) {
long long num = extra.back().first;
prev += i - num;
extra.back().second--;
if (extra.back().second == 0) {
extra.pop_back();
}
} else
res = -1;
}
cout << ans << ' ';
}
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
pre();
long long _t = 1;
cin >> _t;
for (long long i = 1; i <= _t; i++) {
solve();
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, i, j;
string s;
cin >> n;
cin >> s;
long long int pre = 0, ans = 0;
map<long long int, long long int> m;
for (i = 0; i < n; ++i) {
m[pre]++;
pre += (s[i] - '1');
ans += m[pre];
}
cout << ans << '\n';
}
void preCompute() {}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t = 1, tc;
cin >> t;
for (tc = 1; tc <= t; ++tc) {
preCompute();
solve();
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int inf = (1ll << 30) - 1;
const int maxn = (int)1e5 + 10;
using namespace std;
int n, k, x;
int a[5050];
bool dp[5050][5050];
bool u[5050][5050];
bool get(int p, int g) {
if (g == x % k) return true;
if (p == 0) return false;
if (u[p][g]) return dp[p][g];
u[p][g] = 1;
dp[p][g] = get(p - 1, g);
dp[p][g] |= get(p - 1, (g + a[p]) % k);
return dp[p][g];
}
void no() {
cout << "NO\n";
exit(0);
}
int used[5050];
int last = 0;
void rec(int p, int g) {
if (g == x % k) return;
if (get(p - 1, g)) {
rec(p - 1, g);
} else {
last = p;
used[p] = 1;
rec(p - 1, (g + a[p]) % k);
}
}
void solve() {
cin >> n >> k >> x;
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < x) no();
if (!get(n, 0)) no();
printf("YES\n");
rec(n, 0);
int nused = 0;
for (int i = 1; i <= n; i++) {
if (!used[i]) nused = i;
}
for (int i = 1; i <= n; i++) {
if (used[i]) {
if (last == i) continue;
if (a[i]) printf("%d %d %d\n", (int)((a[i] + k - 1) / k), i, last);
a[last] += a[i];
} else {
if (i == nused) continue;
if (a[i]) printf("%d %d %d\n", (int)((a[i] + k - 1) / k), i, nused);
a[nused] += a[i];
}
}
if (nused == 0) {
nused = 2;
}
if (last == 0) {
last = 1;
if (x > 0) printf("%d %d %d\n", x / k, nused, 1);
} else if (a[last] > x) {
printf("%d %d %d\n", (int)((a[last] - x + k - 1) / k), last, nused);
} else if (a[last] < x) {
printf("%d %d %d\n", (int)((x - a[last] + k - 1) / k), nused, last);
}
}
int main() { solve(); }
| 16 |
#include <bits/stdc++.h>
using namespace std;
int a[30], b[5];
long long s[(1 << 24) + 10], d[(1 << 24) + 10];
int n, k;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
scanf("%d", &k);
for (int i = 0; i < k; ++i) scanf("%d", &b[i]);
for (int i = 0; i < n; ++i) s[(1 << i)] = a[i];
d[0] = 1;
for (int i = 1; i < (1 << n); ++i) {
s[i] = s[i & (i - 1)] + s[i ^ (i & (i - 1))];
if (k > 0 && s[i] == b[0]) continue;
if (k > 1 && s[i] == b[1]) continue;
for (int j = 0; j < n; ++j)
if ((i >> j) & 1) {
d[i] += d[i ^ (1 << j)];
if (d[i] > 1000000007) d[i] -= 1000000007;
}
}
cout << d[(1 << n) - 1] << endl;
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int tst, a, b, c, r;
cin >> tst;
for (int w = 1; w <= tst; ++w) {
cin >> a >> b >> c >> r;
int aux;
if (b < a) aux = a, a = b, b = aux;
int xa = c - r, xb = c + r;
int intersec = 0;
if (a <= xa and xa <= b and a <= xb and xb <= b) {
intersec = xb - xa;
} else if (a <= xa and xa <= b) {
intersec = b - xa;
} else if (a <= xb and xb <= b) {
intersec = xb - a;
} else if (xa > b and xb > b)
intersec = 0;
else if (xa < a and xb < a)
intersec = 0;
else
intersec = b - a;
cout << b - a - intersec << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, icon = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') icon = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * icon;
}
int _abs(int x) { return x < 0 ? -x : x; }
long long _min(long long a, long long b) { return a < b ? a : b; }
const int N = 2000000 + 233;
int a[N], num_s = 0, num_b = 0, c[N];
int ss[N], bb[N], n;
long long ans = 0, tmp, last;
int main() {
n = read();
for (int i = 0; i < n; i++) {
a[i] = read();
c[i] = a[i] - i - 1;
if (c[i] > 0) {
num_b++;
bb[c[i]]++;
}
if (c[i] <= 0) {
num_s++;
ss[-c[i]]++;
}
ans += _abs(c[i]);
}
last = ans;
int u = 0;
for (int i = 1; i < n; i++) {
tmp = last;
tmp += num_s;
tmp -= num_b;
num_b -= bb[i];
num_s += bb[i];
c[n - i] -= i - 1;
if (c[n - i] <= 0) {
if (c[n - i] + n - 1 > 0) {
int dd = c[n - i] + n - 1 - (_abs(c[n - i]) + 1);
c[n - i] += n - 1;
tmp += dd;
num_b++;
num_s--;
bb[c[n - i] + i]++;
} else
tmp -= n;
} else
tmp += n - 2;
if (ans > tmp) ans = tmp, u = i;
last = tmp;
}
cout << ans << " " << u << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
struct Modint {
int val;
Modint(int a = 0) { val = a; }
Modint& operator+=(Modint r) {
val = (val + r.val) % 1000000007;
return *this;
}
Modint& operator-=(Modint r) {
val = (val + 1000000007 - r.val) % 1000000007;
return *this;
}
Modint& operator*=(Modint r) {
val = 1LL * val * r.val % 1000000007;
return *this;
}
Modint operator+(Modint r) { return Modint(*this) += r; }
Modint operator-(Modint r) { return Modint(*this) -= r; }
Modint operator*(Modint r) { return Modint(*this) *= r; }
};
Modint dp[1000001][2][3], dpsum[1000001][2][3];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
string s;
cin >> n >> k >> s;
vector<int> last[2] = {vector<int>(n + 1, 0), vector<int>(n + 1, 0)};
for (int i = 0; i < n; i++) {
last[0][i + 1] = last[0][i];
last[1][i + 1] = last[1][i];
if (s[i] == 'W') last[0][i + 1] = i + 1;
if (s[i] == 'B') last[1][i + 1] = i + 1;
}
dp[0][0][0] = 1;
dp[0][1][0] = 1;
for (int pos = 1; pos <= n; pos++) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++) {
dpsum[pos][i][j] += dpsum[pos - 1][i][j] + dp[pos - 1][i][j];
dp[pos][1 - i][j] += dpsum[pos][i][j] - dpsum[last[i][pos]][i][j];
}
if (last[1][pos] < pos - k + 1) {
dp[pos][0][1] -= dpsum[pos - k + 1][1][1] - dpsum[last[1][pos]][1][1];
dp[pos][0][2] += dpsum[pos - k + 1][1][1] - dpsum[last[1][pos]][1][1];
}
if (last[0][pos] < pos - k + 1) {
dp[pos][1][0] -= dpsum[pos - k + 1][0][0] - dpsum[last[0][pos]][0][0];
dp[pos][1][1] += dpsum[pos - k + 1][0][0] - dpsum[last[0][pos]][0][0];
}
}
cout << (dp[n][0][2] + dp[n][1][2]).val << '\n';
return 0;
}
| 16 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int N = 2e5 + 5;
const int MAX = 2e6 + 5;
const int MOD = 1000000007;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
srand(time(NULL));
cout << fixed << setprecision(10);
clock_t clk;
clk = clock();
;
long long n, x, y;
cin >> n >> x >> y;
vector<long long> dp(n + 1, 1e18);
dp[1] = x;
for (int i = 2; i <= n; i++) {
if (i & 1) {
dp[i] = min(dp[i - 1] + x, dp[(i + 1) / 2] + y + x);
} else {
dp[i] = min(dp[i / 2] + y, dp[i - 1] + x);
}
}
cout << dp[n] << "\n";
clk = clock() - clk;
cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
<< "\n";
return 0;
;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200001];
int ans[200001];
bool vis[200001];
void filll(int in, long long val) {
if (ans[in] != -1) return;
ans[in] = val;
for (auto el : adj[in]) {
filll(el, val);
}
}
int is_cycle(int in) {
if (ans[in] != -1) return ans[in];
int res = 1;
vis[in] = 1;
if (adj[in].size() == 2) {
res = (vis[adj[in][0]] ? 1 : is_cycle(adj[in][0])) &&
(vis[adj[in][1]] ? 1 : is_cycle(adj[in][1]));
} else {
res = 0;
}
filll(in, res);
return res;
}
long long eval(int a, int b, long long c) {
long long res = 1;
while (a--) res *= 2;
while (b--) res *= 3;
return res * c;
}
int main() {
int n;
cin >> n;
long long arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
pair<int, int> fac[n];
int taken[n];
memset(taken, 0, sizeof(taken));
int mn2, mx3;
mn2 = 100, mx3 = 0;
for (int i = 0; i < n; i++) {
int to, th;
to = th = 0;
while (arr[i] % 2 == 0) {
to++;
arr[i] /= 2;
}
while (arr[i] % 3 == 0) {
th++;
arr[i] /= 3;
}
mn2 = min(mn2, to);
mx3 = max(mx3, th);
fac[i].first = to;
fac[i].second = th;
}
pair<int, int> cur;
int curi;
for (int i = 0; i < n; i++) {
if (fac[i].first == mn2 && fac[i].second == mx3) {
cur = fac[i];
curi = i;
}
}
for (int j = 0; j < n; j++) {
cout << eval(cur.first, cur.second, arr[curi]) << ' ';
for (int i = 0; i < n; i++) {
if (fac[i].first == cur.first + 1 && fac[i].second == cur.second ||
fac[i].first == cur.first && fac[i].second == cur.second - 1) {
cur = fac[i];
curi = i;
break;
}
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n >> m;
cout << m / n + (m % n > 0) << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
int n, a, b, x, t[105];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> t[i];
for (int i = 1; i <= n; i++) {
cin >> x;
if (!x && t[i]) b++;
if (x && !t[i]) a++;
}
if (!b)
cout << -1;
else
cout << a / b + 1;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, maxn = 100001;
int n, d, a[maxn];
long long c(int n) { return (long long)n * (n - 1) / 2; }
int main() {
while (~scanf("%d%d", &n, &d)) {
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
long long ans = 0;
for (int i = 0; i < n; i++) {
int pos = upper_bound(a, a + n, a[i] + d) - a - 1;
ans += c(pos - i);
}
printf("%lld\n", ans);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, top, zhan[500001];
long long ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
while (top > 1 && zhan[top - 1] >= zhan[top] && zhan[top] <= x) {
ans += min(zhan[top - 1], x);
top--;
}
zhan[++top] = x;
}
sort(zhan + 1, zhan + top + 1);
for (int i = 1; i <= top - 2; i++) {
ans += zhan[i];
}
printf("%lld", ans);
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-9;
double fRand(double fMin, double fMax) {
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
template <class T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
struct pt {
double x, y;
pt operator+(pt p) { return {x + p.x, y + p.y}; }
pt operator-(pt p) { return {x - p.x, y - p.y}; }
pt operator*(double k) { return {x * k, y * k}; }
pt operator/(double k) { return {x / k, y / k}; }
double operator*(pt p) { return x * p.y - y * p.x; }
double operator|(pt p) { return x * p.x + y * p.y; }
friend istream &operator>>(istream &is, pt &p) {
is >> p.x >> p.y;
return is;
}
friend ostream &operator<<(ostream &os, pt &p) {
os << "(" << p.x << " " << p.y << ")";
return os;
}
};
pt perp(pt v) { return {-v.y, v.x}; }
double sq(pt v) { return v | v; }
double tangentsPolar(pt o1, double r1, pt o2, double r2, bool inner, int sgn) {
if (inner) r2 = -r2;
pt d = o2 - o1;
double dr = r1 - r2, d2 = sq(d), h2 = d2 - dr * dr;
pt v = (d * dr + perp(d) * sqrt(h2) * sgn) / d2;
pt p1 = o1 + v * r1, p2 = o2 + v * r2;
double x = (p1.x - p2.x) * sgn, y = (p1.y - p2.y) * sgn;
return atan2(y, x);
}
struct Event {
double polar;
bool open;
bool operator<(const Event &rhs) const { return polar < rhs.polar; }
};
bool cmp(Event e, double polar) { return e.polar <= polar; }
const int MAXN = 1005;
int n, k;
pt o[MAXN];
double r[MAXN];
int main() {
cin >> n >> k;
for (int i = (0); i <= (n - 1); ++i) cin >> o[i] >> r[i];
int maxCir = 1;
for (int i = (0); i <= (n - 1); ++i) {
vector<Event> events;
for (int j = (0); j <= (n - 1); ++j) {
if (i == j) continue;
for (int sgn : {-1, 1}) {
double lo = tangentsPolar(o[i], r[i], o[j], r[j], false, sgn);
double hi = tangentsPolar(o[i], r[i], o[j], r[j], true, sgn);
if (sgn == 1) swap(lo, hi);
if (lo < hi) {
events.push_back({lo, true});
events.push_back({hi, false});
} else {
events.push_back({lo, true});
events.push_back({PI, false});
events.push_back({-PI, true});
events.push_back({hi, false});
}
}
}
sort(events.begin(), events.end());
int cnt = 1, j = 0;
for (Event e : events) {
while (j < (int)events.size() && cmp(events[j], e.polar)) {
cnt += (events[j].open) ? 1 : -1;
++j;
}
maxCir = max(maxCir, cnt);
}
}
cout << 1 + 1LL * k * (k + 1) / 2 + 1LL * (k + 1) * (maxCir - 1) +
(n - maxCir);
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
int v[100000 + 1];
int main() {
int n, x, y, l, i, minim, i1;
scanf("%d%d%d", &n, &x, &y);
for (i = 0; i < n; i++) scanf("%d", &v[i]);
for (i = 0; i < n; i++) {
minim = v[i];
for (i1 = i; i1 >= 0 && i1 >= i - x; i1--) minim = min(v[i1], minim);
for (i1 = i; i1 < n && i1 <= i + y; i1++) minim = min(v[i1], minim);
if (minim == v[i]) {
printf("%d", i + 1);
i = n + 1;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
if (x == 0 && y != 0) {
cout << "Impossible" << endl;
return 0;
}
(y >= x) ? cout << y << " " : cout << x << " ";
(y != 0) ? cout << y - 1 + x << endl : cout << x << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int s = 0;
string a, b;
int v[200005];
bool ok(int k) {
string ac = a;
for (int i = 0; i < k; i++) {
ac[v[i]] = '*';
}
int r, poz = 0;
for (int i = 0; i < b.length(); i++) {
r = ac.find(b[i], poz);
if (r == -1) return false;
poz = r + 1;
}
return true;
}
int cautare() {
int pas = 1 << 30, i = -1;
while (pas != 0) {
if (i + pas <= a.length() && ok(i + pas)) i += pas;
pas /= 2;
}
return i;
}
int main() {
cin >> a >> b;
for (int i = 0; i < a.length(); i++) {
cin >> v[i];
v[i]--;
}
cout << cautare();
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <typename A>
ostream& operator<<(ostream& cout, vector<A> const& v);
template <typename A, typename B>
ostream& operator<<(ostream& cout, pair<A, B> const& p) {
return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream& operator<<(ostream& cout, vector<A> const& v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i) cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream& operator>>(istream& cin, pair<A, B>& p) {
cin >> p.first;
return cin >> p.second;
}
mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
void usaco(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
const long long mod = 1000000007;
struct segtree {
int n, depth;
vector<long long> tree;
void init(int second, long long* arr) {
n = second;
tree = vector<long long>(4 * second, -1);
init(0, 0, n - 1, arr);
}
long long init(int i, int l, int r, long long* arr) {
if (l == r) return tree[i] = arr[l];
int mid = (l + r) / 2;
long long a = init(2 * i + 1, l, mid, arr),
b = init(2 * i + 2, mid + 1, r, arr);
return tree[i] = min(a, b);
}
void update(int p, long long v) { update(0, 0, n - 1, p, v); }
long long update(int i, int tl, int tr, int q, long long v) {
if (tr < q || q < tl || tl > tr) return tree[i];
if (tl == tr) {
return tree[i] = v;
}
int mid = (tl + tr) / 2;
long long a = update(2 * i + 1, tl, mid, q, v),
b = update(2 * i + 2, mid + 1, tr, q, v);
return tree[i] = min(a, b);
}
long long query(int l, int r) { return query(0, 0, n - 1, l, r); }
long long query(int i, int tl, int tr, int ql, int qr) {
if (ql <= tl && tr <= qr) return tree[i];
if (tl > tr || tr < ql || qr < tl) return 1e9;
int mid = (tl + tr) / 2;
long long a = query(2 * i + 1, tl, mid, ql, qr),
b = query(2 * i + 2, mid + 1, tr, ql, qr);
return min(a, b);
}
long long bs(int i, int tl, int tr, int v) {
if (tree[i] >= v) return tr + 1;
if (tl == tr) return tl;
int mid = (tl + tr) / 2;
if (tree[2 * i + 1] < v)
return bs(2 * i + 1, tl, mid, v);
else
return bs(2 * i + 2, mid + 1, tr, v);
}
};
long long n, m, k, q, l, r, x, y, z;
const long long template_array_size = 1e6 + 3280;
long long a[template_array_size];
long long b[template_array_size];
long long c[template_array_size];
string second, t;
long long ans = 0;
vector<long long> vals[100005];
bool pos[100005];
long long last[100005];
segtree st;
void solve(int tc = 0) {
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
bool all_one = 1;
for (long long i = 0; i < n; i++) {
if (a[i] != 1) all_one = 0;
}
if (all_one) {
cout << 1 << '\n';
return;
}
for (long long i = 0; i < n; i++) {
vals[a[i]].push_back(i);
}
for (long long i = 0; i <= n + 1; i++) vals[i].push_back(n);
memset(pos, 0, sizeof(pos));
vector<pair<pair<long long, long long>, long long>> qv;
for (long long i = 2; i <= n + 2; i++) {
long long last = 0;
for (long long x : vals[i - 1]) {
if (last <= x - 1) {
qv.push_back(make_pair(make_pair(x - 1, last), i));
}
last = x + 1;
}
}
sort(qv.begin(), qv.end());
for (long long i = 0; i < n; i++) last[i] = -1;
st.init(n, last);
long long qp = 0;
for (long long i = 0; i < n; i++) {
st.update(a[i] - 1, i);
while (qp < qv.size() && qv[qp].first.first == i) {
long long x = st.bs(0, 0, n - 1, qv[qp].first.second);
pos[x + 1] = 1;
++qp;
}
}
long long ans = 2;
while (pos[ans]) ++ans;
cout << ans << '\n';
}
int main() {
{ ios_base::sync_with_stdio(false); }
{ cin.tie(NULL); }
int tc = 1;
for (int t = 0; t < tc; t++) solve(t);
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s = 1, t = 2;
cin >> n;
while (n) {
if (n % 2) s = (s * t) % 1000000007;
n >>= 1;
t = (t * t) % 1000000007;
}
cout << (s * (s + 1) / 2) % 1000000007;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[253][253];
int b[253][253];
int p[253 * 253];
int px[253], py[253];
int vx[253], vy[253];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
bool valid(int i, int j) {
if (i < 0 || i >= n || j < 0 || j >= m) return false;
if (b[i][j]) return false;
int k = 0;
if (i + 1 < n && j - 1 >= 0 && b[i + 1][j] > k && b[i][j - 1] > k)
return true;
if (i - 1 >= 0 && j + 1 < m && b[i - 1][j] > k && b[i][j + 1] > k)
return true;
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
px[i] = max(px[i], a[i][j]);
py[j] = max(py[j], a[i][j]);
}
for (int i = 0; i < n; i++) p[px[i]] ^= 1;
for (int j = 0; j < m; j++) p[py[j]] ^= 2;
sort(px, px + n);
reverse(px, px + n);
sort(py, py + m);
reverse(py, py + m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (py[j] >= px[i]) vx[i]++;
vx[i]--;
b[i][vx[i]] = px[i];
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++)
if (px[i] >= py[j]) vy[j]++;
vy[j]--;
b[vy[j]][j] = py[j];
}
set<pair<int, int> > A;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (valid(i, j)) A.insert(make_pair(i, j));
for (int k = n * m; k >= 1; k--)
if (!p[k]) {
bool F = false;
for (auto p : A) {
int i = p.first;
int j = p.second;
if (i + 1 < n && j - 1 >= 0 && b[i + 1][j] > k && b[i][j - 1] > k ||
i - 1 >= 0 && j + 1 < m && b[i - 1][j] > k && b[i][j + 1] > k) {
b[i][j] = k;
F = true;
A.erase(make_pair(i, j));
for (int d = 0; d < 4; d++) {
if (valid(i + dx[d], j + dy[d]))
A.insert(make_pair(i + dx[d], j + dy[d]));
}
break;
}
}
assert(F);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
printf("%d%c", b[i][j], j == m - 1 ? '\n' : ' ');
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int q;
cin >> q;
for (int i = 0; i < (int)(q); ++i) {
int n;
cin >> n;
if (n % 4 == 0) {
cout << n / 4 << "\n";
} else if (n % 4 == 1) {
if (n >= 9) {
cout << (n - 9) / 4 + 1 << "\n";
} else {
cout << "-1\n";
}
} else if (n % 4 == 2) {
if (n >= 6) {
cout << (n - 6) / 4 + 1 << "\n";
} else {
cout << "-1\n";
}
} else {
if (n >= 15) {
cout << (n - 15) / 4 + 2 << "\n";
} else {
cout << "-1\n";
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int number;
std::cin >> number;
std::vector<int64_t> diff2(number);
auto const diffAdd = [&](int index, int64_t value) {
if (index >= (int)diff2.size()) return;
diff2[index] += value;
};
auto const add = [&](int left, int right, int64_t start, int64_t change) {
assert(left < right);
assert(right <= number);
diffAdd(left, start);
diffAdd(left + 1, -start);
diffAdd(right, -start);
diffAdd(right + 1, start);
diffAdd(left + 1, change);
diffAdd(right, -change);
diffAdd(right, -(right - left - 1) * change);
diffAdd(right + 1, (right - left - 1) * change);
};
auto const addAbs = [&](int left, int count, int start) {
if (start < 0) {
auto delta = std::min(count, -start);
add(left, left + delta, -start, -1);
start += delta;
left += delta;
count -= delta;
}
if (count == 0) return;
assert(start >= 0);
add(left, left + count, start, 1);
};
for (int i = 0; i < number; ++i) {
int p;
std::cin >> p;
addAbs(0, number - i, i + 1 - p);
if (i != 0) addAbs(number - i, i, 1 - p);
}
std::partial_sum(begin(diff2), end(diff2), diff2.begin());
std::partial_sum(begin(diff2), end(diff2), diff2.begin());
auto const iter = std::min_element(begin(diff2), end(diff2));
std::cout << *iter << ' ' << iter - diff2.begin() << '\n';
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long int dfs(long long int x, long long int y, long long int i,
vector<pair<long long int, long long int>> heykel) {
if (x < 0 || x > 7 || y < 0 || y > 7) return 0;
for (auto &it : heykel)
if ((it.first++ == x || it.first == x) && it.second == y) return 0;
bool tmp = 1;
for (auto it : heykel)
if (it.first <= x) tmp = 0;
if (tmp || (x == 0 && y == 7)) return 1;
return (dfs(x - 1, y - 1, i + 1, heykel) || dfs(x - 1, y, i + 1, heykel) ||
dfs(x - 1, y + 1, i + 1, heykel) || dfs(x, y - 1, i + 1, heykel) ||
dfs(x, y, i + 1, heykel) || dfs(x, y + 1, i + 1, heykel) ||
dfs(x + 1, y - 1, i + 1, heykel) || dfs(x + 1, y, i + 1, heykel) ||
dfs(x + 1, y + 1, i + 1, heykel));
}
int main() {
vector<pair<long long int, long long int>> heykel;
char a;
for (long long int i = 0; i < 8; i++) {
for (long long int j = 0; j < 8; j++) {
cin >> a;
if (a == 'S') heykel.push_back(make_pair(i, j));
}
}
if (dfs(6, 0, 1, heykel) || dfs(6, 1, 1, heykel) || dfs(7, 1, 1, heykel) ||
dfs(7, 0, 1, heykel))
cout << "WIN";
else
cout << "LOSE";
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long c(0);
while (n--) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
c += (x2 - x1 + 1) * (y2 - y1 + 1);
}
cout << c;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") {
Tp end = std::next(_array, _N);
for (Tp current = _array; current != end; ++current) {
std::cout << *current << _delim;
}
std::cout << '\n';
}
using ll = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);
int arr[500000];
template <class Checker>
inline int bs(Checker can) {
int l = 1;
int r = INF;
while (l <= r) {
int mid = (l + r) >> 1;
if (can(mid)) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return l - 1;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, k;
cin >> n >> k;
ll sum = 0;
for (int i = 0; i < (n); ++i) {
cin >> arr[i];
sum += arr[i];
}
int l = 1;
int r = INF;
while (l <= r) {
int mid = (l + r) >> 1;
ll res = 0;
for (int i = 0; i < (n); ++i) {
if (mid > arr[i]) {
res += mid - arr[i];
}
}
if (res <= k) {
l = mid + 1;
} else {
r = mid - 1;
}
}
int minx = min((int)(sum / n), l - 1);
l = 1;
r = INF;
while (l <= r) {
int mid = (l + r) >> 1;
ll res = 0;
for (int i = 0; i < (n); ++i) {
if (arr[i] > mid) {
res += arr[i] - mid;
}
}
if (res > k) {
l = mid + 1;
} else {
r = mid - 1;
}
}
int maxx = max((int)(((sum)-1LL + (n)) / (n)), r + 1);
cout << maxx - minx << "\n";
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
long long n, rating;
long long level[30008], devote[30008];
long long p[30008];
long long cmp(long long x, long long y) {
if (devote[x] >= 0 and devote[y] < 0) return 1;
if (devote[x] < 0 and devote[y] >= 0) return 0;
if (devote[x] >= 0 and devote[y] >= 0) {
return level[x] < level[y];
}
if (devote[x] < 0 and devote[y] < 0) {
return level[x] + devote[x] > level[y] + devote[y];
}
assert(0);
}
signed main() {
scanf("%lld%lld", &n, &rating);
for (long long i = 1; i <= n; i++)
scanf("%lld%lld", &level[i], &devote[i]), p[i] = i;
sort(p + 1, p + n + 1, cmp);
long long ans = 0;
multiset<long long> corrupt;
for (long long i = 1; i <= n; i++) {
long long x = p[i];
if (devote[x] >= 0) {
if (level[x] <= rating) ans++, rating += devote[x];
} else {
if (level[x] <= rating and rating + devote[x] >= 0) {
ans++;
corrupt.insert(devote[x]);
rating += devote[x];
} else {
rating += devote[x];
corrupt.insert(devote[x]);
rating -= *corrupt.begin();
corrupt.erase(corrupt.begin());
}
}
}
printf("%lld\n", ans);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int base = 1000 * 1000 * 1000;
const int N = (int)2e5 + 2;
const int INF = (int)1e9 + 7;
const long double pi = acos(-1);
const long long linf = (long long)1e18;
long long n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
long long ans = 0, last = n;
if (n & 1) {
long long mn = linf;
for (long long div = 2; div <= sqrt(n); ++div) {
if (n % div == 0) {
mn = min(mn, div);
while (n % div == 0) {
n /= div;
}
}
}
if (n > 1) {
mn = min(mn, n);
}
last -= mn;
ans++;
}
ans += last / 2;
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char s[1000100];
struct sp {
int used;
int n;
int pos[10010];
} m[26];
int main() {
int i, j, r, p, l, curr;
char ch;
for (i = 0; i < 26; i++) {
m[i].used = 0;
m[i].n = 0;
}
for (l = 0;; l++) {
ch = getchar();
if (ch == '\n') break;
m[ch - 97].pos[m[ch - 97].n++] = l;
}
gets(s);
r = 1;
curr = 0;
for (i = 0; s[i] != 0; i++) {
ch = s[i] - 97;
if (m[ch].n == 0) {
cout << -1 << endl;
return 0;
} else {
if (m[ch].used == m[ch].n) {
r++;
curr = 0;
for (j = 0; j < 26; j++) m[j].used = 0;
}
p = m[ch].pos[m[ch].used++];
while (p < curr) {
if (m[ch].used == m[ch].n) {
r++;
curr = 0;
for (j = 0; j < 26; j++) m[j].used = 0;
}
p = m[ch].pos[m[ch].used++];
}
curr = p + 1;
}
}
cout << r << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string k, p;
char q, s;
long long a, b, c[1000000], d = 0, e = 0, f = 0, g = 0, h, i, j, l, m, n, o,
r, t, u, v, w, x, y, z;
cin >> a >> b;
for (i = 1; i <= b; i++) {
cin >> c[i];
}
for (i = 1; i < b; i++) {
d = a - c[i];
e = e + d;
}
cout << e;
}
| 2 |
#include <bits/stdc++.h>
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const long long int int_max = 1e18;
const long long int int_min = -1e18;
using namespace std;
long long int power(long long int x, long long int y) {
long long int temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else {
if (y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
void solve() {
long long int n, m1;
cin >> n >> m1;
string s[n + 2];
for (long long int i = 1; i <= n; i++) {
cin >> s[i];
}
map<pair<string, string>, long long int> m;
for (long long int i = 1; i <= m1; i++) {
string s1, s2;
cin >> s1 >> s2;
m[{s1, s2}] = 1;
m[{s2, s1}] = 1;
}
long long int temp = power(2ll, n);
temp--;
int flg = 0;
long long int mx = 0;
vector<string> finalans;
for (long long int i = 1; i <= temp; i++) {
vector<string> v;
for (long long int j = 0; j <= n - 1; j++) {
long long int temp1 = power(2ll, j);
if (temp1 & i) {
v.push_back(s[j + 1]);
}
}
for (long long int j = 0; j <= v.size() - 1; j++) {
for (long long int j1 = j + 1; j1 <= v.size() - 1; j1++) {
if (m[{v[j], v[j1]}] == 1) {
flg = 1;
break;
}
}
}
if (flg == 0) {
if (mx < v.size()) {
mx = v.size();
finalans = v;
}
}
flg = 0;
v.clear();
}
sort(finalans.begin(), finalans.end());
cout << finalans.size() << '\n';
for (string i : finalans) {
cout << i << '\n';
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
solve();
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
void debug(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
const int N = 100100;
bool special[N];
int cost[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
long long sumCost = 0;
for (int i = 0; i < (n); ++i) {
cin >> cost[i];
sumCost += cost[i];
}
long long ans = 0;
for (int i = 0; i < (k); ++i) {
int x;
cin >> x;
--x;
ans += (sumCost - cost[x]) * cost[x];
special[x] = true;
}
sumCost = 0;
long long sumSq = 0;
for (int i = 0; i < (n); ++i) {
if (special[i]) {
sumCost += cost[i];
sumSq += cost[i] * cost[i];
}
}
ans -= (sumCost * sumCost - sumSq) / 2;
for (int i = 0; i < (n); ++i) {
int j = (i + 1) % n;
if (!special[i]) {
if (!special[j]) {
ans += cost[i] * cost[j];
}
}
}
cout << ans << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const unsigned long long int MOD = 1e9 + 7;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
unsigned long long int POW(unsigned long long int p, int b) {
unsigned long long int ans = 1LL;
while (b > 0) {
if (b & 1) {
ans = ans * 1LL * p;
}
b /= 2;
p = p * 1LL * p;
}
return ans;
}
long long int power(long long int a, long long int b, long long int mod) {
long long int x = 1;
long long int y = a;
while (b > 0) {
if (b & 1) {
x = x * y;
x %= mod;
}
y = y * y;
y %= mod;
b /= 2;
}
return x;
}
long long int add(long long int a, long long int b, long long int m = MOD) {
long long int x = a + b;
while (x >= m) x -= m;
while (x < 0) x += m;
return x;
}
long long int sub(long long int a, long long int b, long long int m = MOD) {
long long int x = a - b;
while (x < 0) x += m;
while (x >= m) x -= m;
return x;
}
long long int mul(long long int a, long long int b, long long int m = MOD) {
long long int x = a * 1ll * b;
x %= m;
if (x < 0) x += m;
return x;
}
const int N = 1e6 + 2;
bool used[N];
bool isans[N];
vector<int> g[N];
vector<int> fk;
int data[N];
int root = 0;
long long int total = 0;
int kitna = 0;
int n;
int dp[N];
void dfs(int u, int p) {
dp[u] = data[u];
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j];
if (v == p) continue;
dfs(v, u);
if (dp[v] == kitna) {
fk.push_back(v);
} else {
dp[u] += dp[v];
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int p, ai;
cin >> p >> ai;
data[i] = ai;
total += 1LL * ai;
g[p].push_back(i);
g[i].push_back(p);
if (p == 0) root = i;
}
if (total % 3 != 0) cout << "-1", exit(0);
kitna = (int)total / 3;
dfs(root, 0);
if (fk.size() < 2) cout << "-1", exit(0);
cout << fk[0] << " " << fk[1] << endl;
}
| 12 |