solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 1002, M = 13, P = 1e9 + 7;
int n, m, g[M][M], f[N + M][M][1 << M - 1], res, ans;
vector<int> G[N + M];
int inv(int x) { return x == 1 ? x : 1LL * (P - P / x) * inv(P % x) % P; }
int dfs(int u, int fa) {
int sta = 1 << (u - n - 1);
for (int v : G[u])
if (v != fa) sta |= dfs(v, u);
return sta;
}
void dp(int u, int fa) {
memset(f[u], 0, sizeof(f[u]));
for (int i = 0; i < m; ++i) f[u][i + 1][1 << i] = 1;
for (int v : G[u])
if (v != fa) {
dp(v, u);
for (int S = (1 << m) - 1; S; --S)
for (int i = 1; i <= m; ++i)
if (S >> (i - 1) & 1)
for (int j : G[i + n])
if (!(S & g[i][j - n]))
(f[u][i][S | g[i][j - n]] +=
1LL * f[u][i][S] * f[v][j - n][g[i][j - n]] % P) %= P;
}
for (int i = 1; i <= m; ++i) (res += f[u][i][(1 << m) - 1]) %= P;
}
int main() {
scanf("%d", &n);
for (int i = 1, a, b; i < n; ++i) {
scanf("%d%d", &a, &b), G[a].push_back(b), G[b].push_back(a);
}
scanf("%d", &m);
for (int i = 1, a, b; i < m; ++i) {
scanf("%d%d", &a, &b), a += n, b += n;
G[a].push_back(b), G[b].push_back(a);
}
for (int i = n + 1; i <= n + m; ++i)
for (int j = n + 1; j <= n + m; ++j)
if (i != j) g[i - n][j - n] = dfs(j, i);
res = 0, dp(1, 0), ans = res, res = 0, dp(n + 1, 0);
printf("%lld", 1LL * ans * inv(res) % P);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
string v[105];
map<string, int> mapa;
int vis[106];
bool palindrome(const string &s) {
int n = s.size();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - 1 - i]) return false;
}
return true;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> v[i];
mapa[v[i]] = i;
reverse(v[i].begin(), v[i].end());
reverse(v[i].begin(), v[i].end());
}
set<pair<int, int>> idxs;
for (int i = 0; i < n; i++) {
reverse(v[i].begin(), v[i].end());
if (mapa.count(v[i])) {
int mi = min(i, mapa[v[i]]);
int ma = max(i, mapa[v[i]]);
if (mi == ma) continue;
vis[i] = 1;
vis[mapa[v[i]]] = 1;
idxs.insert({mi, ma});
}
reverse(v[i].begin(), v[i].end());
}
int alone = -1;
for (int i = 0; i < n; i++) {
if (vis[i] == 0 && palindrome(v[i])) {
alone = i;
break;
}
}
string ans = "";
if (alone != -1) ans = v[alone];
for (const auto &idx : idxs) {
ans = v[idx.first] + ans + v[idx.second];
}
cout << ans.size() << endl;
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int SIZEN = 2010;
char mp[SIZEN][SIZEN] = {0};
int N, M;
int r, s;
int x, y;
bool vis[SIZEN][SIZEN] = {0};
struct Node {
int x, y, l, r;
Node(int _, int __, int ___, int ____) {
x = _;
y = __;
l = ___;
r = ____;
}
Node() { ; }
};
deque<Node> q;
int main() {
scanf("%d%d%d%d%d%d", &N, &M, &r, &s, &x, &y);
for (int i = 1; i <= N; i++) scanf("%s", mp[i] + 1);
q.push_front(Node(r, s, x, y));
int ans = 0;
while (!q.empty()) {
Node D = q.front();
q.pop_front();
x = D.x;
y = D.y;
int l = D.l;
r = D.r;
if (vis[x][y]) continue;
vis[x][y] = true;
ans++;
if (x + 1 <= N && mp[x + 1][y] == '.' && !vis[x + 1][y])
q.push_front(Node(x + 1, y, l, r));
if (x - 1 >= 1 && mp[x - 1][y] == '.' && !vis[x - 1][y])
q.push_front(Node(x - 1, y, l, r));
if (l && y - 1 >= 1 && mp[x][y - 1] == '.' && !vis[x][y - 1])
q.push_back(Node(x, y - 1, l - 1, r));
if (r && y + 1 <= M && mp[x][y + 1] == '.' && !vis[x][y + 1])
q.push_back(Node(x, y + 1, l, r - 1));
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 510, maxq = 600010, maxd = 12;
char dat[maxn][maxn];
int n, m, q;
int datq[maxq][4];
bitset<maxn> reach[2][maxn][maxn];
void getreach(int left, int right, bitset<maxn> resl[][maxn],
bitset<maxn> resr[][maxn]) {
if (left > right) return;
int mid = (left + right) / 2;
for (int i = 1; i <= m; i++)
if (dat[mid][i] == '.')
resr[mid][i] = resr[mid][i - 1], resr[mid][i][i] = true;
else
resr[mid][i].reset();
for (int i = m; i >= 1; i--)
if (dat[mid][i] == '.')
resl[mid][i] = resl[mid][i + 1], resl[mid][i][i] = true;
else
resl[mid][i].reset();
if (left == right) return;
for (int i = mid + 1; i <= right; i++)
for (int j = 1; j <= m; j++)
if (dat[i][j] == '.')
resr[i][j] = resr[i - 1][j] | resr[i][j - 1];
else
resr[i][j].reset();
for (int i = mid - 1; i >= left; i--)
for (int j = m; j >= 1; j--)
if (dat[i][j] == '.')
resl[i][j] = resl[i + 1][j] | resl[i][j + 1];
else
resl[i][j].reset();
}
inline bool inrange(int x, int left, int right) {
return x >= left && x <= right;
}
bool ans[maxq];
void solve(int left, int right) {
if (left > right) return;
getreach(left, right, reach[0], reach[1]);
int mid = (left + right) / 2;
for (int i = 0; i < q; i++)
if (inrange(datq[i][0], left, mid) && inrange(datq[i][2], mid, right))
ans[i] =
(reach[0][datq[i][0]][datq[i][1]] & reach[1][datq[i][2]][datq[i][3]])
.any();
solve(left, mid - 1);
solve(mid + 1, right);
}
void init() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", dat[i] + 1);
scanf("%d", &q);
for (int i = 0; i < q; i++)
scanf("%d%d%d%d", &datq[i][0], &datq[i][1], &datq[i][2], &datq[i][3]);
}
void work() {
solve(1, n);
for (int i = 0; i < q; i++)
if (ans[i])
printf("Yes\n");
else
printf("No\n");
}
int main() {
init();
work();
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
int sum[M];
int p[M];
int n;
int seg[4 * M];
void build(int l = 0, int r = n, int id = 1) {
seg[id] = r - l;
if (r - l <= 1) return;
int mid = (l + r) / 2;
build(l, mid, 2 * id);
build(mid, r, 2 * id + 1);
}
int get(int val, int l = 0, int r = n, int id = 1) {
seg[id]--;
if (r - l <= 1) return l;
int mid = (l + r) / 2;
if (seg[2 * id] > val) return get(val, l, mid, 2 * id);
return get(val - seg[2 * id], mid, r, 2 * id + 1);
}
int Get(int val, int l = 0, int r = n, int id = 1) {
seg[id]++;
if (r - l <= 1) return 0;
int mid = (l + r) / 2;
if (val < mid) return Get(val, l, mid, 2 * id);
return seg[2 * id] + Get(val, mid, r, 2 * id + 1);
}
void solve() {
memset(seg, 0, sizeof seg);
for (int i = 0; i < n; i++) sum[i] += p[i] - Get(p[i]);
}
int32_t main() {
scanf("%d", &n);
int t = 2;
while (t--) {
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
solve();
}
int sb = 0;
for (int i = n - 1; ~i; i--) {
sum[i] += sb;
int k = sum[i] % (n - i);
sb = (sum[i] - k) / (n - i);
sum[i] = k;
}
build();
for (int i = 0; i < n; i++) printf("%d ", get(sum[i]));
return printf("\n"), 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string name;
cin >> name;
string tk = name;
reverse(tk.begin(), tk.end());
cout << tk << name << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Vector {
double x, y, z;
Vector(){};
Vector(double a, double b, double c) {
x = a;
y = b;
z = c;
}
double distsq(Vector b) {
return (this->x - b.x) * (this->x - b.x) +
(this->y - b.y) * (this->y - b.y) +
(this->z - b.z) * (this->z - b.z);
}
};
double eps = 1e-7;
vector<Vector> p;
double xm, ym, zm;
double ans(Vector a) {
double m = a.distsq(p[0]);
for (int i = 1; i < p.size(); i++) {
double t = a.distsq(p[i]);
if (m < t) m = t;
}
return m;
}
double Z(double x, double y) {
double zl = -zm, zr = zm;
while (zr - zl > eps) {
double z1 = zl + (zr - zl) / 3;
double z2 = zl + 2 * (zr - zl) / 3;
if (ans(Vector(x, y, z1)) < ans(Vector(x, y, z2))) {
zr = z2;
} else {
zl = z1;
}
}
return zl;
}
Vector Y(double x) {
double yl = -ym, yr = ym;
while (yr - yl > eps) {
double y1 = yl + (yr - yl) / 3;
double y2 = yl + 2 * (yr - yl) / 3;
double z1 = Z(x, y1);
double z2 = Z(x, y2);
Vector a(x, y1, z1);
Vector b(x, y2, z2);
if (ans(a) < ans(b)) {
yr = y2;
} else {
yl = y1;
}
}
return Vector(x, yl, Z(x, yl));
}
int main() {
int n;
cin >> n;
p.resize(n);
cin >> xm >> ym >> zm;
Vector temp(xm, ym, zm);
p[0] = temp;
for (int i = 1; i < n; i++) {
double a, b, c;
cin >> a >> b >> c;
if (abs(a) > abs(xm)) {
xm = a;
}
if (abs(b) > abs(ym)) {
ym = b;
}
if (abs(c) > abs(zm)) {
zm = c;
}
Vector f(a, b, c);
p[i] = f;
}
xm = abs(xm);
ym = abs(ym);
zm = abs(zm);
double xl = -xm, xr = xm;
while (xr - xl > eps) {
double x1 = xl + (xr - xl) / 3;
double x2 = xl + 2 * (xr - xl) / 3;
Vector a = Y(x1);
Vector b = Y(x2);
if (ans(a) < ans(b)) {
xr = x2;
} else {
xl = x1;
}
}
Vector a = Y(xl);
cout << fixed << setprecision(10);
cout << a.x << " " << a.y << " " << a.z << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0;
char c = getchar();
bool f = 0;
for (; c > '9' || c < '0'; f = c == '-', c = getchar())
;
for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = getchar())
;
return f ? -x : x;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
template <class T>
bool Enlarge(T &a, T const &b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
bool Reduce(T &a, T const &b) {
return a > b ? a = b, 1 : 0;
}
long long n, l, r, k, m;
void work1() {
long long x = n - m;
for (long long i = 2 * n; i >= n; --i) {
long long t = (k - 1) % i + 1;
if (m <= t && t <= m * 2)
if ((t - x <= i && i <= 2 * x + t) ||
((t + 1 - x <= i && i <= 2 * x + t + 1))) {
write(i - n);
puts("");
return;
}
}
puts("-1");
}
void work2() {
long long res = -n, x = n - m;
if (m <= k && k <= 2 * m) {
write(min(k - m + 1 + x, n));
puts("");
return;
}
for (long long i = 1, lim = k / n; i <= lim; ++i) {
long long L = (k - 2 * m + i - 1) / i, R = (k - m) / i;
Enlarge(L, n);
Reduce(R, 2 * n);
Enlarge(L, (k - x + i) / (i + 1));
Reduce(R, (2 * x + k + 1) / (i + 1));
if (L <= R) Enlarge(res, R);
}
write(res < 0 ? -1 : res - n), puts("");
}
int main() {
n = read(), l = read(), r = read(), k = read();
m = r - l + 1;
if (m <= 0) m += n;
if (n <= k / n)
work1();
else
work2();
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, m, flag, i, j, yy[256], t;
char s[1010][1010], s1[1010][1010];
void draw2(int x, int y) {
yy['a'] = 0;
yy['c'] = 0;
yy['d'] = 0;
yy['b'] = 0;
yy[s1[x][y - 2]] = 1;
yy[s1[x][y + 2]] = 1;
yy[s1[x - 1][y - 1]] = 1;
yy[s1[x - 1][y]] = 1;
yy[s1[x - 1][y + 1]] = 1;
yy[s1[x + 1][y - 1]] = 1;
yy[s1[x + 1][y]] = 1;
yy[s1[x + 1][y + 1]] = 1;
for (t = 'a'; t <= 'd'; t++)
if (!yy[t]) break;
s1[x][y] = t;
s1[x][y - 1] = t;
s1[x][y + 1] = t;
s[x][y] = '.';
s[x][y - 1] = '.';
s[x][y + 1] = '.';
}
void draw1(int x, int y) {
yy['a'] = 0;
yy['c'] = 0;
yy['d'] = 0;
yy['b'] = 0;
yy[s1[x - 2][y]] = 1;
yy[s1[x + 2][y]] = 1;
yy[s1[x - 1][y - 1]] = 1;
yy[s1[x][y - 1]] = 1;
yy[s1[x + 1][y - 1]] = 1;
yy[s1[x - 1][y + 1]] = 1;
yy[s1[x][y + 1]] = 1;
yy[s1[x + 1][y + 1]] = 1;
for (t = 'a'; t <= 'd'; t++)
if (!yy[t]) break;
s1[x][y] = t;
s1[x - 1][y] = t;
s1[x + 1][y] = t;
s[x][y] = '.';
s[x - 1][y] = '.';
s[x + 1][y] = '.';
}
int main() {
scanf("%d%d", &n, &m);
flag = 1;
for (i = 1; i <= n; i++) scanf("%s", s[i] + 1);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) s1[i][j] = '.';
for (i = 1; i <= n && flag; i++)
for (j = 1; j <= m && flag; j++)
if (s[i][j] == 'b') {
if (s[i - 1][j] == 'w' && s[i + 1][j] == 'w')
draw1(i, j);
else if (s[i][j - 1] == 'w' && s[i][j + 1] == 'w')
draw2(i, j);
else
flag = 0;
}
for (i = 1; i <= n && flag; i++)
for (j = 1; j <= m && flag; j++)
if (s[i][j] == 'w') flag = 0;
if (!flag) {
printf("NO\n");
return 0;
} else
printf("YES\n");
for (i = 1; i <= n; i++) printf("%s\n", s1[i] + 1);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long fib[100];
void init() {
fib[0] = 1, fib[1] = 2;
for (int i = 2; i <= 85; i++) fib[i] = fib[i - 1] + fib[i - 2];
}
int w[100];
int dp[100][2];
void solve(long long x) {
memset(w, 0, sizeof(w));
memset(dp, 0, sizeof(dp));
int num = 0;
for (int i = 85; i >= 0; i--) {
if (x >= fib[i]) {
x -= fib[i];
w[num++] = i;
}
}
num--;
dp[0][1] = 1;
dp[0][0] = w[num] >> 1;
int i = 0;
while (num > 0) {
num--;
dp[i + 1][0] = dp[i][0] * ((w[num] - w[num + 1]) >> 1) +
dp[i][1] * ((w[num] - w[num + 1] - 1) >> 1);
dp[i + 1][1] = dp[i][0] + dp[i][1];
i++;
}
cout << dp[i][0] + dp[i][1] << endl;
}
int main() {
ios::sync_with_stdio(false);
init();
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
solve(x);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, w, sz[100011], d[100011];
vector<int> dly[100011];
vector<pair<int, int>> g[100011];
priority_queue<pair<int, int>> pq;
int main() {
memset(d, 63, sizeof(d));
scanf("%d %d", &n, &m);
for (int(i) = int(1); (i) <= int(m); (i)++)
scanf("%d %d %d", &a, &b, &w), g[a].emplace_back(w, b),
g[b].emplace_back(w, a);
for (int(i) = int(1); (i) <= int(n); (i)++) {
scanf("%d", &sz[i]);
dly[i].resize(sz[i]);
for (int(j) = int(0); (j) <= int(sz[i] - 1); (j)++) scanf("%d", &dly[i][j]);
}
d[1] = 0, pq.emplace(0, 1);
while (!pq.empty()) {
w = -pq.top().first;
a = pq.top().second;
pq.pop();
if (w != d[a]) continue;
if (a == n) break;
for (int i = 0; i < sz[a]; i++)
if (dly[a][i] == d[a]) d[a]++;
for (int i = 0; i < g[a].size(); i++)
if (d[g[a][i].second] > d[a] + g[a][i].first)
d[g[a][i].second] = d[a] + g[a][i].first,
pq.emplace(-d[g[a][i].second], g[a][i].second);
}
if (d[n] == 0x3f3f3f3f)
printf("-1\n");
else
printf("%d\n", d[n]);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 * 5;
struct node {
int c, v;
} sv[MAXN];
bool cmp1(const node &a, const node &b) { return a.v < b.v; }
bool cmp2(const node &a, const node &b) { return a.c < b.c; }
int g[MAXN];
long long n, k, s, t;
bool judge(long long v) {
long long tot = 0, tmp;
for (int i = k; i >= 1; i--) {
tmp = v;
long long dis = g[i] - g[i - 1];
if (dis > v) return false;
tmp -= dis;
tot += 2 * dis;
tot -= min(tmp, dis);
}
return tot <= t;
}
int main() {
scanf("%lld%lld%lld%lld", &n, &k, &s, &t);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &sv[i].c, &sv[i].v);
}
for (int i = 1; i <= k; i++) {
scanf("%d", &g[i]);
}
k++;
g[0] = 0, g[k] = s;
sort(g + 1, g + 1 + k);
long long l = 0, r = 1e9 + 7, ans = 1e9 + 7;
while (l <= r) {
long long m = (l + r) >> 1;
if (judge(m)) {
ans = min(ans, m);
r = m - 1;
} else {
l = m + 1;
}
}
node T;
T.c = 0, T.v = ans;
sort(sv + 1, sv + 1 + n, cmp1);
int pos = lower_bound(sv + 1, sv + 1 + n, T, cmp1) - sv;
sort(sv + pos, sv + 1 + n, cmp2);
if (pos == n + 1) return 0 * printf("-1\n");
printf("%d\n", sv[pos].c);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
ll white, black;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
int x = t / 2;
int y = t - x;
white += i % 2 ? y : x;
black += i % 2 ? x : y;
}
cout << min(black, white) << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6 * 1e6;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
long long prime[maxn * 2];
bool isprime[maxn] = {0};
long long f[maxn * 2];
void getprime() {
for (int i = 2; i <= maxn; i++) {
if (!prime[i]) {
prime[++prime[0]] = i;
isprime[i] = 1;
}
for (int j = 1; j <= prime[0] && prime[j] <= maxn / i; j++) {
prime[prime[j] * i] = 1;
if (i % prime[j] == 0) break;
}
}
}
int main() {
if (fopen("/home/lianyi/123.txt", "r") != NULL) {
freopen("/home/lianyi/123.txt", "r", stdin);
} else {
std::ios::sync_with_stdio(false);
cin.tie(0);
}
getprime();
long long t, l, r;
f[1] = 1;
for (long long i = 2; i <= maxn; i++) {
if (isprime[i]) {
f[i] = i * (i - 1) / 2;
f[i] %= mod;
continue;
}
for (int j = 1;; j++) {
if (i % prime[j] == 0) {
f[i] = f[i / prime[j]] + i * (prime[j] - 1) / 2 % mod;
f[i] %= mod;
break;
}
}
}
while (cin >> t >> l >> r) {
long long x = 1;
long long res = 0;
for (long long i = l; i <= r; i++) {
res = (res + f[i] * x % mod) % mod;
x = x * t % mod;
}
cout << res << '\n';
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int N = 200010;
int Tc;
long long ts, tf, t, a[N], ans = -1, wait = 9999999999999999LL;
int n;
int main() {
scanf("%I64d%I64d%I64d", &ts, &tf, &t);
scanf("%d", &n);
if (n == 0) return printf("%I64d\n", ts), 0;
for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
a[n + 1] = -1;
if (a[1] <= ts) {
long long now = ts;
for (int i = 1; i <= n; ++i) {
if (now > tf - t) break;
long long tmp = now - a[i] + 1;
if (tmp < wait) wait = tmp, ans = a[i] - 1;
if (a[i] <= now)
now += t;
else
return printf("%I64d\n", now), 0;
}
if (now <= tf - t) return printf("%I64d\n", now), 0;
if (ans >= 0)
printf("%I64d\n", ans);
else {
if (now <= tf - t)
return printf("%I64d\n", now), 0;
else
for (;;)
;
}
} else
printf("%I64d\n", ts);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;
int d, n, m, x, y;
map<int, vector<pair<int, int> > > in, out;
map<int, int> h;
int main() {
scanf("%d %d %d", &d, &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
in[x + 1].push_back(make_pair(y, i));
out[x + n + 1].push_back(make_pair(y, i));
h[x + 1] = h[x + n + 1] = 1;
}
h[1] = 1;
h[d] = 1;
h[n + 1] = 1;
long long ans = 0;
long long last = 1;
in[1].push_back(make_pair(0, 0));
out[n + 1].push_back(make_pair(0, 0));
multiset<pair<int, int> > S;
for (__typeof(h.begin()) it = h.begin(); it != h.end(); it++) {
int cur = it->first, go = 0;
for (__typeof(in[cur].begin()) it2 = in[cur].begin(); it2 != in[cur].end();
it2++)
S.insert(*it2);
for (__typeof(out[cur].begin()) it2 = out[cur].begin();
it2 != out[cur].end(); it2++)
S.erase(S.find(*it2));
__typeof(h.begin()) it2 = it;
it2++;
if (cur > d) break;
if (it2 == h.end())
go = d;
else
go = min(it2->first - 1, d);
if (!S.size()) {
cout << -1 << '\n';
return 0;
}
ans += (long long)(S.begin()->first) * (long long)(go - cur + 1);
}
cout << ans << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int t = 0;
int main() {
scanf("%d", &t);
for (int l = 0; l < t; ++l) {
long long x = 0, y = 0;
scanf("%lld%lld", &x, &y);
if (x - y == 1) {
puts("NO");
} else {
puts("YES");
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s = 0, alt;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i && a[i] - a[i - 1] > s) {
s = a[i] - a[i - 1];
}
}
alt = a[n - 1] - a[0];
for (int i = 2; i < n; i++) {
if (a[i] - a[i - 2] < s) {
cout << s;
return 0;
} else {
if (a[i] - a[i - 2] < alt) {
alt = a[i] - a[i - 2];
}
}
}
cout << alt;
return 0;
};
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int a[N];
int b[N];
char resA[N + 1];
char resB[N + 1];
int calc(int digit, int& zeroes) {
int za = a[0], zb = b[0];
int res = za;
zeroes = za;
if (a[digit] && b[10 - digit]) {
a[digit]--;
b[10 - digit]--;
res = 1;
for (int i = 0; i < 10; i++) {
int lo = min(a[i], b[9 - i]);
res += lo;
if (i == 0) za -= lo;
if (i == 9) zb -= lo;
}
zeroes = min(za, zb);
res += zeroes;
a[digit]++;
b[10 - digit]++;
}
return res;
}
void print(int digit, int zeroes, int n) {
char* pa = &resA[n];
char* pb = &resB[n];
*pa-- = 0;
*pb-- = 0;
while (zeroes--) a[0]--, b[0]--, *pa-- = '0', *pb-- = '0';
if (a[digit] && b[10 - digit]) {
*pa-- = '0' + digit, *pb-- = '0' + 10 - digit;
a[digit]--;
b[10 - digit]--;
for (int i = 0; i < 10; i++) {
int lo = min(a[i], b[9 - i]);
a[i] -= lo;
b[9 - i] -= lo;
for (int j = 0; j < lo; j++) *pa-- = '0' + i, *pb-- = '0' + 9 - i;
}
}
for (int i = 0; i < 10; i++)
for (int j = 0; j < a[i]; j++) *pa-- = '0' + i;
for (int i = 0; i < 10; i++)
for (int j = 0; j < b[i]; j++) *pb-- = '0' + i;
cout << resA << endl << resB << endl;
}
int main() {
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
a[s[i] - '0']++;
b[s[i] - '0']++;
}
int best = -1;
int pick = -1;
int zeroes = 0;
for (int i = 1; i < 10; i++) {
int z;
int res = calc(i, z);
if (res > best) best = res, pick = i, zeroes = z;
}
print(pick, zeroes, s.size());
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int a = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
a = a * 10 + c - 48;
c = getchar();
}
return a;
}
const int _ = 2e5 + 7, M = 1e9 + 7;
int l[_], r[_], N;
int poww(long long a, int b) {
int times = 1;
while (b) {
if (b & 1) times = times * a % M;
a = a * a % M;
b >>= 1;
}
return times;
}
int P[_], pre[_], suf[_];
int main() {
N = read();
long long all = 1;
P[1] = 1;
for (int i = 1; i <= N; ++i) l[i] = read();
for (int i = 1; i <= N; ++i) r[i] = read();
for (int i = 2; i <= N; ++i) {
P[i] = M + 1 -
poww(((r[i]) - (l[i]) + 1ll) * ((r[i - 1]) - (l[i - 1]) + 1ll) % M,
M - 2) *
max(((min(r[i], r[i - 1])) - (max(l[i], l[i - 1])) + 1ll), 0ll) %
M;
all = (all + P[i]) % M;
}
for (int i = 1; i <= N; ++i) pre[i] = (pre[i - 1] + P[i]) % M;
for (int i = N; i; --i) suf[i] = (suf[i + 1] + P[i]) % M;
for (int i = 2; i < N; ++i) {
int num = ((r[i - 1]) - (l[i - 1]) + 1ll) * ((r[i]) - (l[i]) + 1ll) % M *
((r[i + 1]) - (l[i + 1]) + 1ll) % M;
vector<pair<int, int> > pnt;
bool flg[3] = {0, 1, 1};
int size[3] = {0, (int)((r[i - 1]) - (l[i - 1]) + 1ll) - 1,
(int)((r[i + 1]) - (l[i + 1]) + 1ll) - 1};
for (int j = i - 1; j <= i + 1; j += 2)
if (l[j] <= r[i] && r[j] >= l[i]) {
if (l[j] <= l[i])
flg[(j - i + 3) >> 1] = 0;
else
pnt.push_back(pair<int, int>(l[j], (j - i + 3) >> 1));
if (r[j] + 1 <= r[i])
pnt.push_back(pair<int, int>(r[j] + 1, (j - i + 3) >> 1));
}
pnt.push_back(pair<int, int>(l[i], 0));
pnt.push_back(pair<int, int>(r[i] + 1, 0));
sort(pnt.begin(), pnt.end());
int pos = 1;
long long sum = 0;
while (pos < (int)pnt.size()) {
sum = (sum + 1ll * (pnt[pos].first - pnt[pos - 1].first) *
(size[1] + flg[1]) % M * (size[2] + flg[2])) %
M;
flg[pnt[pos++].second] ^= 1;
}
all = (all + 2 * sum * poww(num, M - 2)) % M;
}
for (int i = 1; i <= N; ++i)
all = (all + 1ll * P[i] * (pre[max(i - 2, 0)] + suf[i + 2])) % M;
cout << (all + 2 * P[2]) % M;
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int N = 5e5 + 10, M = 2e2 + 10;
int ri() {
char c = getchar();
int x = 0, f = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) - '0' + c;
return x * f;
}
int to[N], nx[N], pr[N], las[N], vis[N], tp, Line, n, m, tm, cnt, can;
char a[M][M];
void Add(int u, int v) {
to[++tp] = v;
nx[tp] = pr[u];
pr[u] = tp;
}
int Id(int i, int j, bool p) {
return p ? (Line + (j - 1) * (n - 1) + i) : ((i - 1) * (m - 1) + j);
}
bool dfs(int u) {
for (int i = pr[u]; i; i = nx[i])
if (vis[to[i]] != tm) {
vis[to[i]] = tm;
if (!las[to[i]] || dfs(las[to[i]])) return las[to[i]] = u, true;
}
return false;
}
int main() {
n = ri();
m = ri();
Line = n * (m - 1);
for (int i = 1; i <= n; ++i) scanf("%s", a[i] + 1);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (a[i][j] == '#') {
++cnt;
if (a[i][j + 1] == '#' && a[i + 1][j] == '#')
Add(Id(i, j, 0), Id(i, j, 1));
if (a[i + 1][j] == '#' && a[i][j - 1] == '#')
Add(Id(i, j - 1, 0), Id(i, j, 1));
if (a[i][j - 1] == '#' && a[i - 1][j] == '#')
Add(Id(i, j - 1, 0), Id(i - 1, j, 1));
if (a[i - 1][j] == '#' && a[i][j + 1] == '#')
Add(Id(i, j, 0), Id(i - 1, j, 1));
can += (a[i][j + 1] == '#') + (a[i + 1][j] == '#');
}
for (tm = 1; tm <= Line; ++tm)
if (dfs(tm)) --can;
printf("%d\n", cnt - can);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, X;
cin >> n >> X;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
int pos = (v.size() + 1) / 2;
pos--;
if (v[pos] == X) {
cout << 0 << endl;
return 0;
}
int cont = 0;
while (1) {
v.push_back(X);
sort(v.begin(), v.end());
cont++;
pos = (v.size() + 1) / 2;
pos--;
if (v[pos] == X) break;
}
cout << cont << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int f(int n) { return n * n; }
int main() {
vector<int> a(6);
for (int i = 0; i < 6; i++) {
cin >> a[i];
}
int d = a[0] + a[1] + a[2];
int ans = f(d) - f(a[0]) - f(a[2]) - f(a[4]);
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
string ss[445];
set<string> sss;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> ss[i];
reverse(ss[i].begin(), ss[i].end());
string g = "";
bool ls = 0;
for (int p = 0; p < ss[i].size(); ++p) {
if (ss[i][p] == 'u')
g.push_back('o'), g.push_back('o');
else if (ss[i][p] == 'k' && ls)
continue;
else
g.push_back(ss[i][p]);
if (ss[i][p] != 'h')
ls = 0;
else
ls = 1;
}
sss.insert(g);
}
cout << sss.size() << "\n";
}
| 2 |
#include <bits/stdc++.h>
const int MAXN = 2e5 + 19;
int n, m, k;
int id[10][10], pj;
std::vector<std::pair<int, int> > to[MAXN], has[MAXN];
bool disable[10][10];
long long hash[10][10], st, ans, ban;
void dfs(int node) {
if (node > k) {
++ans;
return;
}
for (int i = 1; i <= node; ++i) {
if (!disable[node][i] && !(st & hash[node][i]) &&
!(ban & (1ll << id[node][i]))) {
st ^= 1ll << id[node][i];
long long tmp = ban;
ban |= hash[node][i];
dfs(node + 1);
st ^= 1ll << id[node][i];
ban = tmp;
}
}
}
int main() {
std::scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; ++i)
for (int j = 1; j <= i; ++j) id[i][j] = pj++;
for (int i = 1; i <= m; ++i) {
int u, v, w;
std::scanf("%d%d%d", &u, &v, &w);
to[u].push_back({w, v});
}
for (int i = 1; i <= n; ++i) {
std::sort(to[i].begin(), to[i].end());
for (int j = 1; j <= to[i].size(); ++j)
has[to[i][j - 1].second].push_back({to[i].size(), j});
}
for (int i = 1; i <= n; ++i) {
std::sort(has[i].begin(), has[i].end());
for (int j = 1; j < has[i].size(); ++j)
if (has[i][j - 1] == has[i][j])
disable[has[i][j].first][has[i][j].second] = 1;
has[i].resize(std::unique(has[i].begin(), has[i].end()) - has[i].begin());
long long tmp = 0;
for (int j = 0; j < has[i].size(); ++j)
tmp ^= 1ll << id[has[i][j].first][has[i][j].second];
for (int j = 0; j < has[i].size(); ++j)
hash[has[i][j].first][has[i][j].second] |= tmp;
}
dfs(1);
std::printf("%lld\n", ans);
}
| 7 |
#include <bits/stdc++.h>
const int N = (int)2e5 + 7;
const int inf = (int)1e9 + 7;
const long long linf = (long long)1e18 + 7;
using namespace std;
int n;
char second[N];
long long bp(long long x, long long y, long long mod) {
long long res = 1;
while (y) {
if (y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
const int primes = 997;
const int mods = (int)1e9 + 7;
const int invs = bp(primes, mods - 2, mods);
int dg[N];
struct node {
int len, pref, suff;
int h;
node() {
len = pref = suff = 0;
h = 0;
}
node(int x) {
len = 1;
pref = suff = x;
h = x;
}
bool operator==(const node &R) {
return len == R.len && h == R.h && pref == R.pref && suff == R.suff;
}
};
node operator+(node l, node r) {
if (!l.len) return r;
if (!r.len) return l;
node res;
if (l.suff != r.pref || !l.suff) {
res.len = l.len + r.len;
res.pref = l.pref;
res.suff = r.suff;
res.h = ((l.h * 1ll * dg[r.len]) % mods + r.h) % mods;
} else {
l.h = (l.h - 1 + mods) * 1ll * invs % mods;
l.len--;
l.suff = 0;
if (!l.len) l.pref = 0;
r.h = (r.h - dg[r.len - 1] + mods) % mods;
r.len--;
r.pref = 0;
if (!r.len) r.suff = 0;
res.len = l.len + r.len;
res.pref = l.pref;
res.suff = r.suff;
res.pref = (l.len > 0 ? l.pref : r.pref);
res.suff = (r.len > 0 ? r.suff : l.suff);
res.h = ((l.h * 1ll * dg[r.len]) % mods + r.h) % mods;
}
return res;
}
struct tree {
node t[N << 2];
void build(int v = 1, int tl = 1, int tr = n) {
if (tl == tr) {
t[v] = node(second[tl] - '0');
return;
}
int tm = tl + tr >> 1;
build(v << 1, tl, tm);
build(v << 1 | 1, tm + 1, tr);
t[v] = t[v << 1] + t[v << 1 | 1];
}
inline node get(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (l <= tl && tr <= r) return t[v];
int tm = tl + tr >> 1;
if (r <= tm) return get(l, r, v << 1, tl, tm);
if (tm < l) return get(l, r, v << 1 | 1, tm + 1, tr);
return get(l, r, v << 1, tl, tm) + get(l, r, v << 1 | 1, tm + 1, tr);
}
} t;
void pre() {
dg[0] = 1;
for (int j = (1); j <= (n); j++) {
dg[j] = dg[j - 1] * 1ll * primes % mods;
}
}
int cnt[N];
int main() {
ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> (second + 1);
pre();
for (int i = (1); i <= (n); i++) {
cnt[i] = cnt[i - 1] + (second[i] == '1');
}
t.build();
int q;
cin >> q;
while (q--) {
int a, b, len;
cin >> a >> b >> len;
if (-cnt[a - 1] + cnt[a + len - 1] != -cnt[b - 1] + cnt[b + len - 1]) {
puts("NO");
continue;
}
if (t.get(a, a + len - 1) == t.get(b, b + len - 1))
puts("YES");
else
puts("NO");
}
exit(0);
}
| 8 |
#include<bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;
const int mod = 1e9+7,siz = 1e6+5;
int t,n,m,x,pos,k,a[siz],fact[siz];
/*
when calculating numbers that are going to be powers mod them with (mod-1)
*/
int bix(int a,int n)
{
if(n == 0) return 1;
if(n&1) return a*bix(a,n-1)%mod;
int x = bix(a,n/2);
return x*x%mod;
}
int div(int x)
{
int ret = bix(x,mod-2);
return ret;
}
void pre()
{
fact[0] = 1;
for(int i = 1;i<siz;i++) fact[i] = (fact[i-1]*i)%mod;
}
int ncr(int n,int r)
{
if(r>n or n<0) return 0;
if(n == 0 and r == 0) return 1;
int ret = fact[n];
(ret*=bix(fact[r],mod-2))%=mod;
(ret*=bix(fact[n-r],mod-2))%=mod;
return ret;
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
pre();
cin>>n;
int x = 0;
for(int i = 1;i<=n;i++) cin>>a[i],x+=a[i];
if(x%n!=0) return cout<<0<<endl,0;
x/=n;
vector<int>pos,neg;
int zer = 0;
for(int i = 1;i<=n;i++)
{
a[i]-=x;
if(a[i]>0) pos.pb(a[i]);
else if(a[i]<0) neg.pb(a[i]);
else zer++;
}
if(zer == n) return cout<<1<<endl,0;
/// handle cases for only 1 pos or only 1 neg
int places = pos.size()+neg.size()+1;
int ways = ncr(n,zer);
map<int,int>mp1,mp2;
for(auto x:pos) mp1[x]++;
for(auto x:neg) mp2[x]++;
if(pos.size() == 1)
{
int cur = 1;
(cur*=fact[neg.size()])%=mod;
for(auto x:mp2) (cur*=div(fact[x.second]))%=mod;
(cur*=(neg.size()+1))%=mod;
(ways*=cur)%=mod; cout<<ways<<endl;
return 0;
}
if(neg.size() == 1)
{
int cur = 1;
(cur*=fact[pos.size()])%=mod;
for(auto x:mp1) (cur*=div(fact[x.second]))%=mod;
(cur*=(pos.size()+1))%=mod;
(ways*=cur)%=mod; cout<<ways<<endl;
return 0;
}
int cur = 1;
cur = fact[pos.size()];
for(auto x:mp1) (cur*=div(fact[x.second]))%=mod;
(cur*=fact[neg.size()])%=mod;
for(auto x:mp2) (cur*=div(fact[x.second]))%=mod;
(cur*=2)%=mod;
(ways*=cur)%=mod;
cout<<ways<<endl;
}
| 7 |
#include <bits/stdc++.h>
int main() {
int a, b, c, i, j, k;
char s[101];
while (gets(s)) {
a = strlen(s);
b = c = 0;
for (i = 0; i < a; i++) {
if (s[i] == '4')
b++;
else if (s[i] == '7')
c++;
}
if (b == 0 && c == 0)
printf("-1\n");
else if (b >= c)
printf("4\n");
else
printf("7\n");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
double pi = 2 * acos(0.0);
const int INF = 1e9 + 7;
void WetPitch() {
long long int n, m, k;
cin >> n >> m >> k;
vector<long long int> v(k);
for (int i = 0; i < k; i++) {
cin >> v[i];
}
long long int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
long long int x;
cin >> x;
for (int l = 0; l < k; l++) {
if (v[l] == x) {
ans = ans + l + 1;
while (l) {
swap(v[l], v[l - 1]);
l--;
}
break;
}
}
}
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int tst;
tst = 1;
while (tst--) {
WetPitch();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX_N = 95;
const long long INF = 1e18;
long long Fibo[MAX_N], sum[MAX_N], n;
long long dp[MAX_N][2];
long long A[MAX_N];
long long siz;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
Fibo[0] = Fibo[1] = 1;
sum[0] = 1, sum[1] = 2;
for (long long i = 2; i < MAX_N; i++) {
Fibo[i] = Fibo[i - 1] + Fibo[i - 2];
sum[i] = sum[i - 1] + Fibo[i];
if (Fibo[i] > INF) {
n = i;
break;
}
}
long long T;
cin >> T;
while (T--) {
long long x;
cin >> x;
siz = 0;
for (long long i = n; i >= 1; i--) {
if (x >= Fibo[i]) {
A[++siz] = i;
x -= Fibo[i];
}
}
reverse(A + 1, A + siz + 1);
memset(dp, 0, sizeof(dp));
dp[1][0] = 1, dp[1][1] = (A[1] - 1) / 2;
for (long long i = 2; i <= siz; i++) {
dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
dp[i][1] = (A[i] - A[i - 1] - 1) / 2 * dp[i - 1][0] +
(A[i] - A[i - 1]) / 2 * dp[i - 1][1];
}
cout << dp[siz][0] + dp[siz][1] << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long inf = 4e18, mod = 1e9 + 7;
vector<vector<int>> notused;
int dsu[100005], siz[100005], tin[100005], tout[100005], hgt[100005];
vector<vector<int>> maxup, up;
int l, timer;
vector<pair<long long, long long>> node[100005];
void init() {
for (int i = 1; i <= 100000; i++) dsu[i] = i, siz[i] = 1;
}
int find_parent(int x) { return dsu[x] == x ? x : find_parent(dsu[x]); }
void merge(int x, int y, int z, int id) {
int nx = find_parent(x);
int ny = find_parent(y);
if (nx == ny) {
notused.push_back({id, x, y});
return;
}
if (siz[nx] < siz[ny]) swap(nx, ny);
siz[nx] += siz[ny];
dsu[ny] = nx;
node[x].push_back({y, z});
node[y].push_back({x, z});
}
void dfs(int x, int p, int w) {
maxup[x][0] = w;
up[x][0] = p;
tin[x] = ++timer;
hgt[x] = hgt[p] + 1;
for (int i = 1; i <= l; i++) {
up[x][i] = up[up[x][i - 1]][i - 1];
}
for (int i = 1; i <= l; i++) {
maxup[x][i] = max(maxup[x][i - 1], maxup[up[x][i - 1]][i - 1]);
}
for (auto it : node[x]) {
if (it.first != p) {
dfs(it.first, x, it.second);
}
}
tout[x] = ++timer;
}
bool is_ancestor(int u, int v) {
if (tin[u] <= tin[v] && tout[u] >= tout[v]) return 1;
return 0;
}
int lca(int u, int v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int i = l; i >= 0; i--) {
if (!is_ancestor(up[u][i], v)) {
u = up[u][i];
}
}
return up[u][0];
}
void solve() {
int n, m;
cin >> n >> m;
init();
set<pair<pair<long long, long long>, pair<long long, long long>>> s;
for (int i = 1; i <= m; i++) {
int u, v, z;
cin >> u >> v >> z;
s.insert({{z, i}, {u, v}});
}
int cnt = n - 1;
while (!s.empty()) {
auto it = s.begin();
pair<pair<long long, long long>, pair<long long, long long>> p = *it;
s.erase(it);
int x = p.second.first, y = p.second.second, z = p.first.first,
id = p.first.second;
merge(x, y, z, id);
}
l = log2(n + 1);
maxup.resize(n + 1, vector<int>(l + 1, 0));
up.resize(n + 1, vector<int>(l + 1, 0));
dfs(1, 1, 0);
sort(notused.begin(), notused.end());
for (auto it : notused) {
int u = it[1], v = it[2];
int L = lca(u, v);
int mx = 0;
int d1 = hgt[u] - hgt[L], d2 = hgt[v] - hgt[L];
for (int i = 0; i <= l; i++) {
if (d1 & (1LL << i)) {
mx = max(mx, maxup[u][i]);
u = up[u][i];
}
if (d2 & (1LL << i)) {
mx = max(mx, maxup[v][i]);
v = up[v][i];
}
}
cout << mx << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
while (t--) solve();
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const int INF = 100000000;
const int maxn = 6511;
struct Edge {
int from, to;
int cap, flow, cost;
Edge(int u, int v, int c, int f, int w)
: from(u), to(v), cap(c), flow(f), cost(w) {}
};
struct MCMF {
int n, m;
int src, target;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
int K;
MCMF() {}
void init(int n = maxn, int K = INF) {
this->n = n;
for (int i = 0; i < n; i++) G[i].clear();
edges.clear();
this->K = K;
}
void addedge(int from, int to, int cap, int cost) {
edges.push_back(Edge(from, to, cap, 0, cost));
edges.push_back(Edge(to, from, 0, 0, -cost));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool SPFA(int s, int t, int &flow, int &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0;
inq[s] = 1;
p[s] = 0;
a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
inq[u] = 0;
for (int i = 0; i < (int)G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) {
Q.push(e.to);
inq[e.to] = 1;
}
}
}
}
if (d[t] == INF) return false;
if (cost + d[t] * a[t] > K) {
flow += (K - cost) / d[t];
cost += (K - cost) / d[t] * d[t];
return false;
}
flow += a[t];
cost += d[t] * a[t];
for (int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
return true;
}
int mincostMaxflow(int &cost) {
int flow = 0;
cost = 0;
while (SPFA(src, target, flow, cost))
;
return flow;
}
};
MCMF mf;
int n, m;
int mt[82][82];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int index(int x, int y) { return (x - 1) * m + y; }
int main() {
int i, j, k;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; ++i) {
for (j = 1; j <= m; ++j) {
scanf("%d", &mt[i][j]);
}
}
mf.init();
mf.src = 0, mf.target = maxn - 1;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= m; ++j) {
if ((i + j) & 1) {
mf.addedge(mf.src, index(i, j), 1, 0);
for (k = 0; k < 4; ++k) {
int tx = i + dx[k], ty = j + dy[k];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
mf.addedge(index(i, j), index(tx, ty), 1, mt[i][j] != mt[tx][ty]);
}
}
} else {
mf.addedge(index(i, j), mf.target, 1, 0);
}
}
}
int cost = 0;
mf.mincostMaxflow(cost);
printf("%d\n", cost);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
__print(x.first);
cout << ',';
__print(x.second);
}
template <typename T>
void __print(const T &x) {
int f = 0;
for (auto &i : x) cout << (f++ ? "," : ""), __print(i);
}
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cout << ", ";
_print(v...);
}
const int N = 2e5 + 7;
const int MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, cur;
cin >> n;
vector<long long> v(n), pre(n), suf(n);
for (int i = 0; i < n; i++) cin >> v[i];
stack<pair<long long, long long>> st;
for (int i = 0; i < n; i++) {
cur = i;
while (!st.empty() && st.top().first > v[i])
cur = st.top().second, st.pop();
pre[i] = ((cur > 0) ? pre[cur - 1] : 0) + v[i] * (i - cur + 1);
st.push(make_pair(v[i], cur));
}
while (!st.empty()) st.pop();
for (int i = n - 1; i >= 0; i--) {
cur = i;
while (!st.empty() && st.top().first > v[i])
cur = st.top().second, st.pop();
suf[i] = ((cur < n - 1) ? suf[cur + 1] : 0) + v[i] * (cur - i + 1);
st.push(make_pair(v[i], cur));
};
;
long long id = -1, ans = 0;
for (int i = 0; i < n; i++) {
cur = pre[i] + suf[i] - v[i];
if (cur > ans) {
ans = cur;
id = i;
}
}
cur = v[id];
for (int i = id + 1; i < n; i++) {
cur = min(cur, v[i]);
v[i] = cur;
}
cur = v[id];
for (int i = id - 1; i >= 0; i--) {
cur = min(cur, v[i]);
v[i] = cur;
}
for (int i = 0; i < n; i++) cout << v[i] << ' ';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool checksubstr(string pre, string x) {
int k = 0;
for (int i = 0; i < x.size(); i++) {
if (pre[k] == x[i]) {
k++;
if (k == pre.size()) return true;
} else {
i -= k;
k = 0;
}
}
return false;
}
bool comp(string a, string b) { return a.size() < b.size(); }
int main() {
int n;
cin >> n;
vector<string> s(n, "");
for (int i = 0; i < n; i++) cin >> s[i];
sort(s.begin(), s.end(), comp);
for (int i = 0; i < n - 1; i++) {
if (!(checksubstr(s[i], s[i + 1]))) {
cout << "NO";
return 0;
}
}
cout << "YES" << endl;
for (string S : s) cout << S << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 7;
vector<array<int, 3> > v1, v2;
bool intersect(const array<int, 3>& lhs, const array<int, 3>& rhs) {
if (lhs[0] < rhs[1] || lhs[0] > rhs[2]) return 0;
return rhs[0] >= lhs[1] && rhs[0] <= lhs[2];
}
bitset<N> sta[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
int x1, x2, y1, y2;
for (int i = 0; i < n; i++) {
cin >> x1 >> y1 >> x2 >> y2;
if (x1 == x2) {
if (y1 > y2) swap(y1, y2);
v1.push_back({x1, y1, y2});
} else {
if (x1 > x2) swap(x1, x2);
v2.push_back({y1, x1, x2});
}
}
if (v1.size() > v2.size()) swap(v1, v2);
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
int l1 = v1.size(), l2 = v2.size();
for (int i = 0; i < l1; i++)
for (int j = 0; j < l2; j++)
if (intersect(v1[i], v2[j])) sta[i].set(j);
long long ans = 0;
for (int i = 0; i < l1; i++) {
for (int j = i + 1; j < l1; j++) {
int t = (sta[i] & sta[j]).count();
ans += t * (t - 1) / 2;
}
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t, n, k;
cin >> t;
while (t--) {
cin >> n >> k;
int leastN = k / n, extras = k % n;
if (extras)
cout << 2 << "\n";
else
cout << 0 << "\n";
for (int i = 0, r, repeat; i < n; i++) {
r = ((extras > 0) ? (leastN + 1) : (leastN));
extras--;
if (i + r > n)
repeat = r - (n - i);
else
repeat = 0;
for (int j = 0; j < n; j++) {
if (repeat or (j >= i and j < (i + r - repeat))) {
cout << 1;
if (repeat > 0) repeat--;
} else
cout << 0;
}
cout << "\n";
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long rez;
int comp[1000009];
int d[1000009];
bool vis[1000009];
int c(int i) {
if (comp[i] == i) return i;
comp[i] = c(comp[i]);
return comp[i];
}
int loops;
inline int mna(int a, int b) { return (a < b ? a : b); }
void readFile() {
scanf("%d%d", &n, &m);
int i, a, b;
int compa, compb;
for (i = 1; i <= n; i++) comp[i] = i;
for (i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
vis[a] = vis[b] = 1;
if (a == b)
loops++;
else {
compa = c(a);
compb = c(b);
comp[compa] = compb;
d[a]++;
d[b]++;
}
}
}
int comps() {
int cc = 0;
int i;
for (i = 1; i <= n; i++) {
if (c(i) == i && vis[i] != 0) cc++;
}
return cc;
}
void solve() {
if (comps() > 1) return;
int i;
rez = 0;
rez += 1LL * loops * (loops - 1) / 2;
rez += 1LL * loops * (m - loops);
for (i = 1; i <= n; i++) {
if (d[i] != 0) rez += 1LL * d[i] * (d[i] - 1) / 2;
}
}
void printFile() { printf("%lld\n", rez); }
int main() {
readFile();
solve();
printFile();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string ch;
int main() {
int n, m, l, r;
char c1, c2;
cin >> n >> m;
cin >> ch;
while (m--) {
cin >> l >> r >> c1 >> c2;
for (int i = l - 1; i < r; i++) {
if (ch[i] == c1) ch[i] = c2;
}
}
cout << ch << endl;
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename first>
inline first abs(const first& a) {
return a < 0 ? -a : a;
}
template <typename first>
inline first sqr(const first& a) {
return a * a;
}
const int INF = INT_MAX / 2;
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384626433832795;
int main() {
int n, m, k;
cin >> n >> m >> k;
bool ok = false;
for (int i = 0; i < int(k); i++) {
int x, y;
cin >> x >> y;
x--, y--;
int d1 = min(x, n - x - 1);
if (d1 < 5) ok = true;
int d2 = min(y, m - y - 1);
if (d2 < 5) ok = true;
}
if (ok)
puts("YES");
else
puts("NO");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXLG = 9;
const int MAXN = 1 << (MAXLG + 1);
int n, x, y;
vector<int> q;
vector<int> qq;
map<vector<int>, bool> mem;
bool query() {
qq.clear();
for (int i = (0); i < (((int)(q).size())); ++i)
if (0 <= q[i] && q[i] < n) qq.push_back(q[i]);
if (((int)(qq).size()) == 0) return false;
if (mem.count(qq)) return mem[qq];
printf("? %d", ((int)(qq).size()));
for (int i = (0); i < (((int)(qq).size())); ++i) printf(" %d", qq[i] + 1);
puts("");
fflush(stdout);
int expect = ((int)(qq).size()) % 2 == 0 ? 0 : x;
int actual;
scanf("%d", &actual);
bool ret = expect != actual;
return mem[qq] = ret;
}
void run() {
scanf("%d%d%d", &n, &x, &y);
vector<bool> diff(MAXLG + 1);
for (int i = (0); i <= (MAXLG); ++i) {
q.clear();
for (int j = (0); j < (MAXN); ++j)
if (((j >> i) & 1) == 0) q.push_back(j);
diff[i] = query();
}
vector<int> fix(MAXLG + 1, -1);
for (int i = (0); i <= (MAXLG); ++i)
if (diff[i]) {
fix[i] = 0;
break;
}
for (int i = (0); i <= (MAXLG); ++i)
if (fix[i] == -1) {
q.clear();
fix[i] = 0;
for (int j = (0); j < (MAXN); ++j) {
bool ok = true;
for (int k = (0); k <= (MAXLG); ++k)
if (fix[k] != -1 && ((j >> k) & 1) != fix[k]) ok = false;
if (ok) q.push_back(j);
}
fix[i] = query() ? 0 : 1;
}
int a = 0;
for (int i = (0); i <= (MAXLG); ++i)
if (fix[i] == 1) a |= 1 << i;
int b = a;
for (int i = (0); i <= (MAXLG); ++i)
if (diff[i]) b ^= 1 << i;
if (a > b) swap(a, b);
printf("! %d %d\n", a + 1, b + 1);
fflush(stdout);
}
int main() {
run();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const long long inf = (1ll << 60);
const long long mod = 1000000007;
long long t1, t2, x1, x2, t0;
int main() {
cin >> t1 >> t2 >> x1 >> x2 >> t0;
if (t1 == t2) {
cout << x1 << " " << x2 << endl;
return 0;
}
if (t0 == t1 || t0 == t2) {
if (t0 == t1) {
cout << x1 << " " << 0 << endl;
return 0;
}
if (t0 == t2) {
cout << 0 << " " << x2 << endl;
return 0;
}
}
long long ans1 = inf, ans2 = inf;
double miss = 1e100;
for (long long i = 0; i <= x1; i++) {
long long l = 0, r = x2;
if (i == 0) l = 1;
long long mid;
while (l <= r) {
mid = (l + r) / 2ll;
double t = (1.0 * t1 * i + 1.0 * t2 * mid) / (1.0 * (i + mid));
if (t < 1.0 * t0) {
l = mid + 1;
} else if (abs(abs(t - 1.0 * t0) - miss) < eps) {
if (i + mid > ans1 + ans2) {
ans1 = i;
ans2 = mid;
}
r = mid - 1;
} else if (abs(t - 1.0 * t0) < miss) {
miss = abs(t - 1.0 * t0);
ans1 = i;
ans2 = mid;
r = mid - 1;
} else {
r = mid - 1;
}
}
}
if (ans1 == 0) {
ans2 = x2;
}
if (ans2 == 0) {
ans1 = x1;
}
cout << ans1 << " " << ans2 << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxlog = 19;
const int maxn = 1 << maxlog;
vector<int> graph[maxn];
int tin[maxn];
int depth[maxn];
int parent[maxlog][maxn];
int timec = 1;
void dfs(int v = 1, int dep = 0) {
tin[v] = timec++;
depth[v] = dep;
for (auto& u : graph[v]) dfs(u, dep + 1);
}
int lca(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
for (int i = maxlog - 1; i >= 0; i--) {
if (depth[u] - (1 << i) >= depth[v]) u = parent[i][u];
}
if (u == v) return u;
for (int i = maxlog - 1; i >= 0; i--) {
if (depth[u] >= (1 << i) && parent[i][u] != parent[i][v]) {
u = parent[i][u];
v = parent[i][v];
}
}
return parent[0][u];
}
pair<int, int> stree[2][maxn * 2];
int maxi(int l, int r) {
l = l + maxn - 1;
r = r + maxn - 1;
pair<int, int> res = {0, 0};
while (l <= r) {
if ((l & 1)) res = max(res, stree[1][l++]);
if (!(r & 1)) res = max(res, stree[1][r--]);
l /= 2;
r /= 2;
}
return res.second;
}
int mini(int l, int r) {
l = l + maxn - 1;
r = r + maxn - 1;
pair<int, int> res = {1000000000, 0};
while (l <= r) {
if ((l & 1)) res = min(res, stree[0][l++]);
if (!(r & 1)) res = min(res, stree[0][r--]);
l /= 2;
r /= 2;
}
return res.second;
}
int lca2(int l, int r) {
if (l > r) return -1;
int najm = mini(l, r);
int najw = maxi(l, r);
return lca(najm, najw);
}
int licz(int l, int r, int x) {
int res1 = lca2(l, x - 1);
int res2 = lca2(x + 1, r);
if (res1 == -1) return res2;
if (res2 == -1) return res1;
return lca(res1, res2);
}
int main() {
ios_base::sync_with_stdio(0);
int n, q;
cin >> n >> q;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
graph[p].push_back(i);
parent[0][i] = p;
}
dfs();
parent[0][1] = 1;
for (int i = 1; i < maxlog; i++)
for (int j = 1; j <= n; j++) parent[i][j] = parent[i - 1][parent[i - 1][j]];
for (int i = 0; i < 2 * maxn; i++) stree[0][i] = {1000000000, -1};
for (int i = 1; i <= n; i++) {
stree[0][maxn + i - 1] = stree[1][maxn + i - 1] = {tin[i], i};
}
for (int i = maxn - 1; i >= 1; i--) {
stree[0][i] = min(stree[0][i * 2], stree[0][i * 2 + 1]);
stree[1][i] = max(stree[1][i * 2], stree[1][i * 2 + 1]);
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
int u = mini(l, r);
int v = maxi(l, r);
int ans1 = licz(l, r, u);
int ans2 = licz(l, r, v);
if (depth[ans1] < depth[ans2]) {
swap(ans1, ans2);
swap(u, v);
}
cout << u << " " << depth[ans1] << "\n";
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long a[100001];
int p[100001];
int n, m;
int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
int main() {
long long result = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i], p[i] = i;
while (m--) {
int x, y;
cin >> x >> y;
x = find(x);
y = find(y);
if (x == y) continue;
if (a[x] > a[y])
p[x] = y;
else
p[y] = x;
}
for (int i = 1; i <= n; i++)
if (p[i] == i) result += a[i];
cout << result << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 51;
int dp[maxn][maxn][maxn][maxn];
char s[maxn][maxn];
int a[maxn][maxn];
int sum[maxn][maxn];
int n;
int dfs(int x1, int y1, int x2, int y2) {
if (~dp[x1][y1][x2][y2]) {
return dp[x1][y1][x2][y2];
}
if (x1 == x2 && y1 == y2) {
return dp[x1][y1][x2][y2] = a[x1][y1];
}
dp[x1][y1][x2][y2] = max(x2 - x1 + 1, y2 - y1 + 1);
int &tmp = dp[x1][y1][x2][y2];
for (int i = x1 + 1; i <= x2; ++i) {
tmp = min(tmp, dfs(x1, y1, i - 1, y2) + dfs(i, y1, x2, y2));
}
for (int i = y1 + 1; i <= y2; ++i) {
tmp = min(tmp, dfs(x1, y1, x2, i - 1) + dfs(x1, i, x2, y2));
}
return tmp;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (s[i][j] == '#') {
a[i + 1][j + 1] = 1;
}
}
}
cout << dfs(1, 1, n, n) << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int vis[51];
vector<int> arr[51];
void bfs(int s) {
queue<int> q;
q.push(s);
vis[s] = 1;
while (!q.empty()) {
int u = q.front();
for (int i = 0; i < arr[u].size(); i++) {
if (vis[arr[u][i]] == 0) {
vis[arr[u][i]] = 1;
q.push(arr[u][i]);
}
}
q.pop();
}
}
int main() {
int n, m;
cin >> n >> m;
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
arr[x].push_back(y);
arr[y].push_back(x);
}
int ctr = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
bfs(i);
ctr++;
}
}
cout << (1ll << n - ctr);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
inline long long read() {
long long X = 0;
bool flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') flag = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
X = (X << 1) + (X << 3) + ch - '0';
ch = getchar();
}
if (flag) return X;
return ~(X - 1);
}
long long n, m, k;
long long ar[N], br[N];
long long ans;
int tot;
int cnt[1010];
int p[N];
void solvve() {
n = read();
int ans = 0;
int zs = 0;
int ct = 0;
ar[0] = -1;
for (register int i(1); i <= n; ++i) {
ar[i] = read();
m = max(ar[i], m);
cnt[ar[i]]++;
if (cnt[ar[i]] > cnt[ar[zs]]) zs = i;
}
ct = 1;
for (register int i(1); i <= n; ++i)
if (cnt[ar[i]] == cnt[ar[zs]] && ar[i] != ar[zs]) ct++;
if (ct > 1) {
cout << n << endl;
return;
}
for (register int x(1); x <= m; ++x) {
if (ar[zs] == x) continue;
for (register int i(0); i <= 2 * n; ++i) p[i] = -1;
int now = n;
p[now] = 0;
for (register int i(1); i <= n; ++i) {
if (ar[i] == ar[zs])
now--;
else if (ar[i] == x)
now++;
if (p[now] == -1)
p[now] = i;
else
ans = max(ans, i - p[now]);
}
}
cout << ans << endl;
}
int main() {
{ solvve(); }
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1009;
int f[maxn][12][maxn / 2], n, d, mod;
int inv[maxn], fac[maxn];
inline void pls(int &x, int y) { x = (x + y >= mod ? x + y - mod : x + y); }
inline int dec(int &x, int y) { return (x - y < 0 ? x - y + mod : x - y); }
void init() {
inv[1] = 1;
for (int i = 2; i < maxn; i++)
inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod;
for (int i = 2; i < maxn; i++) inv[i] = (long long)inv[i - 1] * inv[i] % mod;
}
inline int C(int n, int m) {
if (m > n) return 0;
int res = 1;
for (int i = n - m + 1; i <= n; i++) res = (long long)res * i % mod;
return (long long)res * inv[m] % mod;
}
int main() {
scanf("%d%d%d", &n, &d, &mod);
if (n <= 2) {
puts("1");
return 0;
}
init();
for (int i = 0; i <= n / 2; i++) f[1][0][i] = 1;
f[1][d - 1][0] = 1;
for (int i = 2; i <= n; i++)
for (int j = 1; j <= min(d, i - 1); j++)
for (int k = 1; k <= n / 2; k++) {
f[i][j][k] = f[i][j][k - 1];
for (int t = 1; t * k <= i && t <= j; t++)
pls(f[i][j][k], (long long)f[i - t * k][j - t][k - 1] *
C(f[k][d - 1][k - 1] + t - 1, t) % mod);
}
if (n & 1)
printf("%d", f[n][d][n / 2]);
else
printf("%d", dec(f[n][d][n / 2], C(f[n / 2][d - 1][n / 2 - 1], 2)));
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
string str;
bool col[255][255], row[255][255];
long long n, m, dp[255][255][2], mod = 1e9 + 7;
long long recur(int x, int y, int line) {
long long &res = dp[x][y][line];
if (res != -1) return res;
if (y == m) return res = 1;
res = 0;
if (y + 1 < m && x < n && row[x][y] && row[x][y + 1]) {
if (x + 1 == n)
res = (res + recur(0, y + 2, 1)) % mod;
else
res = (res + recur(x + 1, y, 0)) % mod;
}
if (y + 1 < m && x + 1 < n && col[x][y] && col[x][y + 1] && col[x + 1][y] &&
col[x + 1][y + 1]) {
if (x + 2 == n && line == 0) res = (res + recur(0, y + 2, 1)) % mod;
if (x + 2 != n) res = (res + recur(x + 2, y, line)) % mod;
}
if (x == 0 && n % 2 == 0) {
bool flag = true;
for (long long i = 0; i < ((long long)n); i++)
if (!col[i][y]) flag = false;
if (flag) res = (res + recur(0, y + 1, 1)) % mod;
}
return res;
}
int main() {
for (long long i = 0; i < ((long long)255); i++)
for (long long j = 0; j < ((long long)255); j++)
for (long long k = 0; k < ((long long)2); k++) dp[i][j][k] = -1;
cin >> n >> m;
vector<string> dices(n * 4 + 1);
for (long long i = 0; i < ((long long)n * 4 + 1); i++) cin >> dices[i];
for (long long i = 0; i < ((long long)n); i++)
for (long long j = 0; j < ((long long)m); j++) {
long long cnt = 0;
col[i][j] = row[i][j] = true;
for (long long a = 0; a < ((long long)3); a++)
for (long long b = 0; b < ((long long)3); b++)
if (dices[i * 4 + 1 + a][j * 4 + 1 + b] == 'O') cnt++;
if (cnt == 2 || cnt == 3) {
if (dices[i * 4 + 1][j * 4 + 1] == 'O')
col[i][j] = false;
else
row[i][j] = false;
}
if (cnt == 6) {
if (dices[i * 4 + 1][j * 4 + 2] == 'O')
col[i][j] = false;
else
row[i][j] = false;
}
}
cout << recur(0, 0, 1) << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 100005;
int last[MAXN], nxt[MAXN], a[MAXN], b[MAXN], n, BIT[MAXN], ans[MAXN];
struct Node {
int pos, nxt;
} node[MAXN * 30];
int head[MAXN], cnt;
void addnode(int i, int j) {
node[cnt].pos = j;
node[cnt].nxt = head[i];
head[i] = cnt++;
}
void add(int i, int val) {
for (; i <= n; i += i & -i) BIT[i] += val;
}
int hat(int K) {
int ret = 0;
for (int j = (17) - 1; j >= (0); j--) {
int shift = 1 << j;
if (ret + shift <= n && BIT[ret + shift] <= K) {
K -= BIT[ret + shift], ret += shift;
}
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = (1); i < (n + 1); i++) scanf("%d", a + i);
memset(last, -1, sizeof last);
memset(nxt, -1, sizeof nxt);
memset(head, -1, sizeof head);
for (int i = (1); i < (n + 1); i++) b[i] = last[a[i]], last[a[i]] = i;
for (int i = (1); i < (n + 1); i++)
if (b[i] != -1)
nxt[b[i]] = i;
else
add(i, 1);
for (int i = (1); i < (n + 1); i++) addnode(1, i);
for (int i = (1); i < (n + 1); i++) {
for (int j = head[i]; j != -1; j = node[j].nxt) {
int p = node[j].pos;
int res = hat(p);
ans[p]++;
if (res + 1 <= n) addnode(res + 1, p);
}
add(i, -1);
if (nxt[i] != -1) add(nxt[i], 1);
}
for (int i = (1); i < (n + 1); i++) printf("%d%c", ans[i], " \n"[i == n]);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
int st[210000], top;
long long ans, a[210000], sum[210000], tot;
long long cal1(int x, int y) { return a[x] * (y - x) - sum[y] + sum[x]; }
long long cal2(int x, int y) {
return sum[x - 1] - sum[y - 1] - a[x] * (x - y);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
for (int i = 1; i <= n; i++) tot += a[i] * i;
ans = tot;
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
for (int i = n; i >= 1; i--) {
int l = 1, r = top;
while (r - l > 2) {
int lmid = (l + l + r) / 3, rmid = (l + r + r) / 3;
if (cal1(i, st[lmid]) > cal1(i, st[rmid]))
r = rmid;
else
l = lmid;
}
for (int j = l; j <= r; j++) ans = max(ans, tot + cal1(i, st[j]));
while (top >= 2 && (sum[i] - sum[st[top]]) * (st[top] - st[top - 1]) >=
(sum[st[top]] - sum[st[top - 1]]) * (i - st[top]))
top--;
st[++top] = i;
}
top = 0;
for (int i = 1; i <= n; i++) {
int l = 1, r = top;
while (r - l > 2) {
int lmid = (l + l + r) / 3, rmid = (l + r + r) / 3;
if (cal2(i, st[lmid]) > cal2(i, st[rmid]))
r = rmid;
else
l = lmid;
}
for (int j = l; j <= r; j++) ans = max(ans, tot + cal2(i, st[j]));
while (top >= 2 &&
(sum[i - 1] - sum[st[top] - 1]) * (st[top] - st[top - 1]) <=
(sum[st[top] - 1] - sum[st[top - 1] - 1]) * (i - st[top]))
top--;
st[++top] = i;
}
printf("%I64d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long tens[100];
long long solve(long long x) {
if (x < 10) return x;
long long digits[100] = {};
int D = 0;
while (x) {
digits[D++] = x % 10;
x /= 10;
}
long long res = 0;
for (int i = 1; i < D; i++) {
if (i == 1)
res += 9;
else
res += 9 * tens[i - 2];
}
long long cur = 0;
for (int i = D - 2; i > 0; i--) cur = cur * 10 + digits[i];
res += (digits[D - 1] - 1) * tens[D - 2];
if (digits[D - 1] <= digits[0])
res += cur + 1;
else
res += cur;
return res;
}
int main() {
tens[0] = 1;
for (int i = 1; i < 100; i++) tens[i] = 10LL * tens[i - 1];
long long l, r;
cin >> l >> r;
cout << solve(r) - solve(l - 1) << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[5555];
int dp[5005][5005], res[5005][5005];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
if (!k)
dp[i][i] = res[i][i] = a[i];
else if (i + k < n) {
dp[i][i + k] = dp[i][i + k - 1] ^ dp[i + 1][i + k];
res[i][i + k] =
max(res[i][i + k - 1], max(res[i + 1][i + k], dp[i][i + k]));
}
}
}
int q;
scanf("%d", &q);
while (q--) {
int l, r;
scanf("%d", &l);
scanf("%d", &r);
printf("%d\n", res[l - 1][r - 1]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long int MOD = 1e9 + 7;
const int N = 1e6 + 5;
const int M = 20;
int table[N][M], lvl[N];
int d, u, v;
int getLCA(int u, int v) {
if (lvl[u] < lvl[v]) swap(u, v);
for (int k = M - 1; k >= 0; k--) {
if (lvl[u] - (1 << k) >= lvl[v]) {
u = table[u][k];
}
}
if (u == v) return u;
for (int k = M - 1; k >= 0; k--) {
if (table[u][k] != -1 && table[u][k] != table[v][k]) {
u = table[u][k];
v = table[v][k];
}
}
return table[u][0];
}
int getDist(int n1, int n2) {
int lca = getLCA(n1, n2);
return lvl[n1] + lvl[n2] - 2 * lvl[lca];
}
void add(int p, int node) {
table[node][0] = p;
lvl[node] = lvl[p] + 1;
for (int k = 1; k < M && table[node][k - 1] != -1; k++) {
table[node][k] = table[table[node][k - 1]][k - 1];
}
int tmp = getDist(node, u);
if (tmp > d) {
v = node;
d = tmp;
}
tmp = getDist(node, v);
if (tmp > d) {
u = node;
d = tmp;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(table, -1, sizeof table);
int q;
cin >> q;
u = v = 1;
add(1, 2);
add(1, 3);
add(1, 4);
int n = 4;
while (q--) {
int x;
cin >> x;
add(x, ++n);
add(x, ++n);
cout << d << '\n';
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <class T>
ostream& prnt(ostream& out, T v) {
out << v.size() << '\n';
for (auto e : v) out << e << ' ';
return out;
}
template <class T>
ostream& operator<<(ostream& out, vector<T> v) {
return prnt(out, v);
}
template <class T>
ostream& operator<<(ostream& out, set<T> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, map<T1, T2> v) {
return prnt(out, v);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
return out << '(' << p.first << ' ' << p.second << ')';
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
const long long N = 200100;
long long n, x, y, ans, has[4 * N];
vector<long long> coords, g[4 * N];
vector<pair<long long, long long> > v;
set<long long> ys;
map<long long, long long> m;
long long aib[4 * N + 50];
long long lsb(long long k) { return k & (-k); }
void put(long long pos) {
pos++;
for (; pos <= 4 * N + 24; pos += lsb(pos)) aib[pos]++;
}
long long que(long long pos) {
pos++;
if (pos == 0)
while (1)
;
long long ret = 0;
for (; pos; pos -= lsb(pos)) ret += aib[pos];
return ret;
}
long long cnt(long long a, long long b) {
a++;
b--;
if (a > b) return 0;
return que(b) - que(a - 1);
}
long long gg(long long k) { return k * (k + 1) / 2; }
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> x >> y;
y = -y;
v.push_back({y, x});
coords.push_back(x);
coords.push_back(y);
}
coords.push_back(-1e9 - 100);
sort(coords.begin(), coords.end());
cerr << "coords.size()"
<< " ->",
debug_out(coords.size());
for (long long i = 0; i < coords.size(); i++) m[coords[i]] = i;
sort(v.begin(), v.end());
for (auto& i : v) {
i.first = m[i.first];
i.second = m[i.second];
assert(i.second <= 2 * N + 10);
assert(i.first <= 2 * N + 10);
g[i.first].push_back(i.second);
ys.insert(i.first);
}
for (auto i : ys) {
for (auto j : g[i])
if (!has[j]) {
has[j] = 1;
put(j);
}
long long tmp = que(4 * N + 3);
tmp = tmp * (tmp + 1) / 2;
g[i].push_back(4 * N + 3);
for (long long j = 0; j < g[i].size(); j++) {
if (j == 0) {
tmp -= gg(cnt(0, g[i][0]));
} else {
tmp -= gg(cnt(g[i][j - 1], g[i][j]));
}
}
ans += tmp;
assert(tmp >= 0);
assert(ans >= 0);
}
cout << ans << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
int a[maxn];
int n, k, p, x, y, sum = 0;
int main() {
ios::sync_with_stdio(false);
cin >> n >> k >> p >> x >> y;
for (int i = 0; i < k; i++) cin >> a[i];
for (int i = 0; i < k; i++) sum += a[i];
int count = 0;
for (int i = 0; i < k; i++)
if (a[i] < y) count++;
int l = n / 2 - count < n - k ? n / 2 - count : n - k;
int m = n - k - l;
if (l < 0 || sum + l + m * y > x)
cout << "-1";
else {
while (m--) cout << y << ' ';
while (l--) cout << 1 << ' ';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10 * 10;
pair<long long, long long> p[MAXN + 10];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
long long a;
cin >> a;
if (a < 0)
p[abs(a)].second++;
else
p[a].first++;
}
long long s = 0;
for (int i = 0; i < 11; i++) {
if (i == 0)
s += (p[i].first * (p[i].first - 1)) / 2;
else
s += p[i].first * p[i].second;
}
cout << s << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int l = s.size(), x, y, c = 0;
bool k = false;
if (s[0] == '@' || s[l - 1] == '@') {
k = true;
}
for (int i = 0; i < l; i++) {
if (s[i] == '@') {
x = i;
break;
}
}
y = x;
for (int i = y + 1; i < l; i++) {
if (s[i] == '@' && i - x <= 2) {
k = true;
break;
} else if (s[i] == '@' && i - x > 2) {
x = i;
}
}
x = 0;
for (int i = 0; i < l; i++) {
if (s[i] == '@') {
x++;
y = i;
}
}
if (k || x == 0) {
cout << "No solution" << endl;
return 0;
}
char o[l + x - 1];
for (int i = 0; i < l; i++) {
o[i + c] = s[i];
if (s[i] == '@' && i < y) {
o[i + c + 1] = s[i + 1];
o[i + c + 2] = ',';
i++;
c++;
}
}
for (int i = 0; i < l + x - 1; i++) {
cout << o[i];
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 0;
const int N = 100005;
vector<array<int, 2>> v[N];
vector<set<int>> g(N);
vector<int> depth(N, 0);
vector<vector<int>> p(N, vector<int>(17, 0));
vector<long long> up(N, 0);
vector<long long> down(N, 0);
vector<int> sz(N, 0);
vector<int> ch[N];
vector<int> st[N];
vector<long long> pw(N, 1);
vector<long long> inv(N, 1);
long long res = 0;
int root = 0;
void dfs(int a) {
depth[a] = depth[p[a][0]] + 1;
for (int i = 1; i < 17; i++) p[a][i] = p[p[a][i - 1]][i - 1];
for (auto [to, k] : v[a]) {
if (to == p[a][0]) continue;
p[to][0] = a;
down[to] = (10 * down[a] + k) % mod;
up[to] = (pw[depth[a] - 1] * k + up[a]) % mod;
dfs(to);
}
}
int lca(int a, int b) {
if (depth[a] > depth[b]) swap(a, b);
for (int i = 16; i >= 0; i--)
if (depth[p[b][i]] >= depth[a]) b = p[b][i];
if (a == b) return a;
for (int i = 16; i >= 0; i--) {
if (p[a][i] != p[b][i]) {
a = p[a][i];
b = p[b][i];
}
}
return p[a][0];
}
int dist(int a, int b) { return depth[a] + depth[b] - 2 * depth[lca(a, b)]; }
long long h(int a, int b) {
int l = lca(a, b);
long long x = (((up[a] - up[l] + mod) % mod) * inv[depth[l] - 1]) % mod;
long long y =
(((down[b] - pw[depth[b] - depth[l]] * down[l]) % mod) + mod) % mod;
long long z = (pw[depth[b] - depth[l]] * x + y) % mod;
return z;
}
void find_sz(int a, int par = 0) {
sz[a] = 1;
for (int to : g[a]) {
if (to == par) continue;
find_sz(to, a);
sz[a] += sz[to];
}
}
int find_centroid(int a, int par, int n) {
for (int to : g[a]) {
if (to == par) continue;
if (sz[to] > n / 2) return find_centroid(to, a, n);
}
return a;
}
void build_cd(int a, int par = 0) {
find_sz(a);
int n = sz[a];
int centroid = find_centroid(a, 0, n);
if (par == 0) root = centroid;
ch[par].push_back(centroid);
for (int to : g[centroid]) {
g[to].erase(centroid);
build_cd(to, centroid);
}
}
long long fpow(long long b, long long e) {
long long x = 1;
while (e) {
if (e & 1) x = (x * b) % mod;
b = (b * b) % mod;
e /= 2;
}
return x;
}
long long find_inv() {
long long x = mod;
long long tmp = mod;
for (long long i = 2; i * i <= tmp; i++) {
if ((tmp % i) == 0) {
x /= i;
x *= (i - 1);
}
while ((tmp % i) == 0) tmp /= i;
}
if (tmp > 1) {
x /= tmp;
x *= (tmp - 1);
}
return fpow(10, x - 1);
}
void solve(int a) {
map<int, int> m;
for (int to : ch[a]) {
solve(to);
for (int x : st[to]) {
m[h(x, a)]++;
st[a].push_back(x);
}
}
res += m[0];
st[a].push_back(a);
for (int to : ch[a]) {
for (int x : st[to]) m[h(x, a)]--;
for (int x : st[to]) {
long long z = h(a, x);
if (z == 0) res++;
long long val = (((-z * inv[dist(a, x)]) % mod) + mod) % mod;
res += m[val];
}
for (int x : st[to]) m[h(x, a)]++;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n >> mod;
for (int i = 0; i < n - 1; i++) {
int a, b, k;
cin >> a >> b >> k;
a++;
b++;
v[a].push_back({b, k});
v[b].push_back({a, k});
g[a].insert(b);
g[b].insert(a);
}
for (int i = 1; i <= n; i++) pw[i] = (10 * pw[i - 1]) % mod;
inv[1] = find_inv();
for (int i = 2; i <= n; i++) inv[i] = (inv[i - 1] * inv[1]) % mod;
dfs(1);
build_cd(1);
solve(root);
cout << res << "\n";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int M = 100000 + 100;
int cx[M], cy[M], cz[M], n;
int x, y, z;
string s;
int main() {
cin >> s;
n = (int)s.length();
cx[0] = 0;
cy[0] = 0;
cz[0] = 0;
for (int i = 0; i < n; i++) {
cx[i + 1] = cx[i];
cy[i + 1] = cy[i];
cz[i + 1] = cz[i];
if (s[i] == 'x')
cx[i + 1]++;
else if (s[i] == 'y')
cy[i + 1]++;
else
cz[i + 1]++;
}
int m, l, r;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> l >> r;
x = cx[r] - cx[l - 1];
y = cy[r] - cy[l - 1];
z = cz[r] - cz[l - 1];
if (r - l + 1 < 3)
cout << "YES\n";
else if (x == y && y == z)
cout << "YES\n";
else if (x == y && x - z == 1)
cout << "YES\n";
else if (y - x == 1 && x == z)
cout << "YES\n";
else if (z - y == 1 && y == x)
cout << "YES\n";
else if (z == y && y - x == 1)
cout << "YES\n";
else if (x - y == 1 && y == z)
cout << "YES\n";
else if (x == z && z - y == 1)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void read(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void readc(char &x) {
char c;
while (((c = getchar()) == ' ') || c == '\n')
;
x = c;
}
int n, m, i, j, fa[100005];
bool checkfull(int x) {
x++;
if (x == (x & -x)) return 1;
return 0;
}
int main() {
read(n);
read(m);
if (n == 9 && m == 2) {
puts("NO");
return 0;
}
if (!(n & 1)) {
puts("NO");
return 0;
}
int lim = max(0, (n - 3) / 2);
if (m > lim) {
puts("NO");
return 0;
}
if (checkfull(n) && m == 1) {
puts("NO");
return 0;
}
if (!checkfull(n) && m == 0) {
puts("NO");
return 0;
}
puts("YES");
for (((i)) = (1); ((i)) <= ((m - 1)); ((i))++) {
fa[i * 2 - 1] = max(0, i * 2 - 3);
fa[i * 2] = i * 2 - 1;
}
int bse = max(0, m - 1) * 2;
for (((i)) = (1); ((i)) <= ((n - bse)); ((i))++) {
if (i == 1) {
fa[i + bse] = max(0, bse - 1);
} else {
fa[i + bse] = i / 2 + bse;
}
}
if (checkfull(n - bse) && m) {
fa[n - 1] = fa[n] = 2;
}
for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
printf("%d ", fa[i]);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n;
char a[N];
long long f[N][3][3];
bool v[3][3];
int main() {
scanf("%s", a + 1), n = strlen(a + 1);
if (a[1] == 'A') {
f[1][0][0] = 1;
f[1][1][1] = 1;
f[1][2][2] = 0;
} else {
f[1][0][0] = 0;
f[1][1][1] = 1;
f[1][2][2] = 1;
}
for (int s = 0; s <= 2; ++s)
for (int i = 2; i <= n; ++i) {
if (a[i] == 'A') {
if (a[i - 1] == 'A') {
f[i][0][s] = 0;
f[i][1][s] = f[i - 1][0][s] + f[i - 1][1][s];
f[i][2][s] = 0;
} else {
f[i][0][s] = f[i - 1][2][s];
f[i][1][s] = f[i - 1][1][s];
f[i][2][s] = 0;
}
} else {
if (a[i - 1] == 'A') {
f[i][0][s] = 0;
f[i][1][s] = f[i - 1][0][s];
f[i][2][s] = f[i - 1][0][s] + f[i - 1][1][s];
} else {
f[i][0][s] = 0;
f[i][1][s] = f[i - 1][1][s];
f[i][2][s] = f[i - 1][1][s];
}
}
}
if (a[n] == 'A') {
if (a[1] == 'A') {
v[0][1] = 1;
v[1][1] = 1;
} else {
v[0][1] = 1;
v[0][2] = 1;
v[1][2] = 1;
}
} else {
if (a[1] == 'A') {
v[1][1] = 1;
v[2][0] = 1;
} else {
v[1][1] = 1;
v[1][2] = 1;
}
}
long long ans = 0;
for (int s = 0; s <= 2; ++s)
for (int i = 0; i <= 2; ++i)
if (v[i][s]) ans += f[n][i][s];
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int Max_n = 1e5 + 5;
int n, M;
long long ans;
int sa[Max_n], rk[Max_n], tp[Max_n], ht[Max_n], buk[Max_n];
int top;
char s[Max_n];
void Bsort() {
for (int i = 1; i <= M; i++) buk[i] = 0;
for (int i = 1; i <= n; i++) buk[rk[i]]++;
for (int i = 1; i <= M; i++) buk[i] += buk[i - 1];
for (int i = n; i >= 1; i--) sa[buk[rk[tp[i]]]--] = tp[i];
}
struct node {
int x, v;
} stk[Max_n];
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
M = 26;
for (int i = 1; i <= n; i++) rk[i] = s[i] - 'a' + 1, tp[i] = i;
Bsort();
for (int len = 1, cnt = 0; cnt < n; M = cnt, len <<= 1) {
cnt = 0;
for (int i = n - len + 1; i <= n; i++) tp[++cnt] = i;
for (int i = 1; i <= n; i++)
if (sa[i] > len) tp[++cnt] = sa[i] - len;
Bsort();
swap(tp, rk);
rk[sa[1]] = cnt = 1;
for (int i = 2; i <= n; i++) {
if (tp[sa[i]] != tp[sa[i - 1]] || tp[sa[i] + len] != tp[sa[i - 1] + len])
cnt++;
rk[sa[i]] = cnt;
}
}
for (int i = 1, len = 0; i <= n; i++) {
if (len) len--;
while (s[sa[rk[i] - 1] + len] == s[i + len]) len++;
ht[rk[i]] = len;
}
for (int i = 1; i <= n; i++) ans += n - sa[i] + 1 - max(ht[i], ht[i + 1]);
for (int i = 1; i <= n + 1; i++) {
int now = i;
while (top && ht[i] < stk[top].v) {
int len = i - stk[top].x + 1;
int num = stk[top].v - max(ht[i], top > 1 ? stk[top - 1].v : 0);
ans += 1ll * len * (len + 1) / 2 * num;
now = min(now, stk[top--].x);
}
stk[++top] = (node){now, ht[i]};
}
cout << ans;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t, ans = 0;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int tot = n;
for (int i = n - 1; i >= 0; i--) {
if (a[i] > tot) {
tot--;
}
}
cout << tot + 1 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void show(const vector<T> &a) {
for (T x : a) cout << x << " ";
cout << '\n';
}
vector<long long> xx = {1, 0, -1, 0};
vector<long long> yy = {0, 1, 0, -1};
string dir = "LRUD";
const long long N = 2e5 + 50, oo = 3e18 + 500;
const long long mod = 1e9 + 7;
const long double eps = 1e-12, PI = 2 * acos(0.0);
long long n, m, k;
long long cnt = 0;
long long lg = 18;
set<long long> g[N];
vector<long long> rg[N];
vector<long long> visit(N, 0);
signed main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long> b = a;
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
for (long long i = 0; i < n; i++) {
a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
}
vector<long long> was(n);
vector<long long> c(n);
for (long long i = 0; i < n; i++) {
c[a[i]]++;
}
vector<long long> dp(n);
vector<long long> end(n);
cnt = 0;
for (long long i = 0; i < n; i++) {
long long x = a[i];
dp[x]++;
end[x]++;
if (x) {
if (was[x - 1] == c[x - 1]) {
if (!was[x]) dp[x] = max(dp[x], dp[x - 1] + 1);
;
end[x] = max(end[x], dp[x - 1] + 1);
;
} else {
if (!was[x]) dp[x] = max(dp[x], was[x - 1] + 1);
;
end[x] = max(end[x], was[x - 1] + 1);
;
}
}
cnt = max(cnt, dp[x]);
;
cnt = max(cnt, end[x]);
;
was[x]++;
}
cout << n - cnt << '\n';
}
}
| 8 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 233333;
ll C[maxn];
int n;
void clear() {
for(int i = 0; i <= n + 1; ++i) C[i] = 0;
}
void add(int x, ll v) {
for(int i = x; i <= n + 1; i += i & (-i)) C[i] = (C[i] + v) % mod;
}
ll query(int x) {
ll res = 0;
for(int i = x; i; i -= i & (-i)) res = (res + C[i]) % mod;
return res;
}
ll b[maxn], dp[maxn], pre[maxn], nxt[maxn];
int main() {
int T;
cin >> T;
while(T--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%lld", &b[i]);
dp[n + 1] = 1; b[0] = 0; pre[0] = 0;
for(int i = 1; i <= n; ++i) pre[i] = (pre[i-1] + b[i]);
map<ll, int> mp;
for(int i = n; i >= 0; --i) {
if(!mp.count(pre[i])) {
nxt[i] = n + 1;
} else {
nxt[i] = mp[pre[i]];
}
mp[pre[i]] = i;
}
clear();
add(n + 1, 1);
for(int i = n; i >= 1; --i) {
int k = nxt[i-1];
if(k > n) k = n;
dp[i] = query(k + 1);
add(i, dp[i]);
}
printf("%lld\n", (dp[1] + mod) % mod);
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
vector<int> g[2 * N];
int vis[2 * N], lbls[2 * N];
void dfs(int u, int pa, int label) {
vis[u] = 1;
lbls[u] = label;
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j];
if (v == pa) continue;
if (!vis[v]) {
dfs(v, u, (label + 1) % 2);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
vector<pair<int, int> > all;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
all.push_back(make_pair(a, b));
}
for (int i = 0; i < 2 * n; i += 2) {
g[i].push_back(i + 1);
g[i + 1].push_back(i);
}
for (int i = 0; i < 2 * n; i++) {
if (!vis[i]) dfs(i, -1, 0);
}
for (int i = 0; i < n; i++) {
cout << lbls[all[i].first] + 1 << " " << lbls[all[i].second] + 1 << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
char s[maxn];
typedef struct Node {
int l, r;
int mx[110];
int mi[110];
} node;
node t[maxn];
int cnt = 1;
int f[maxn], h[maxn], g[maxn], stk[maxn];
int mk, tmin;
const int END = -1 * 1000000000;
void init(int rt) {
for (int i = 0; i < maxn; i++) t[rt].mx[i] = t[rt].mi[i] = END;
}
void build(int l, int r, int rt) {
t[rt].l = ++cnt;
init(t[rt].l);
t[rt].r = ++cnt;
init(t[rt].r);
if ('0' <= s[l + 1] && s[l + 1] <= '9') {
t[t[rt].l].mx[0] = s[l + 1] - '0';
t[t[rt].l].mi[0] = s[l + 1] - '0';
} else
build(l + 1, f[l + 1], t[rt].l);
if ('0' <= s[r - 1] && s[r - 1] <= '9') {
t[t[rt].r].mx[0] = s[r - 1] - '0';
t[t[rt].r].mi[0] = s[r - 1] - '0';
} else
build(h[r - 1], r - 1, t[rt].r);
int temp, mark;
if (mk == 1 && g[l] == 0)
temp = tmin - 1, mark = 1;
else
temp = tmin, mark = 0;
if (g[l] != -1)
for (int i = 0; i <= temp; i++) {
for (int j = 0; i - j >= 0; j++) {
if (t[t[rt].l].mx[j] == END || t[t[rt].r].mx[i - j] == END) continue;
if (t[rt].mx[i + mark] == END)
t[rt].mx[i + mark] = t[t[rt].l].mx[j] + t[t[rt].r].mx[i - j];
else
t[rt].mx[i + mark] =
max(t[rt].mx[i + mark], t[t[rt].l].mx[j] + t[t[rt].r].mx[i - j]);
if (t[rt].mi[i + mark] == END)
t[rt].mi[i + mark] = t[t[rt].l].mi[j] + t[t[rt].r].mi[i - j];
else
t[rt].mi[i + mark] =
min(t[rt].mi[i + mark], t[t[rt].l].mi[j] + t[t[rt].r].mi[i - j]);
}
}
if (mk == -1 && g[l] == 0)
temp = tmin - 1, mark = 1;
else
temp = tmin, mark = 0;
if (g[l] != 1)
for (int i = 0; i <= temp; i++) {
for (int j = 0; i - j >= 0; j++) {
if (t[t[rt].l].mx[j] == END || t[t[rt].r].mx[i - j] == END) continue;
if (t[rt].mx[i + mark] == END)
t[rt].mx[i + mark] = t[t[rt].l].mx[j] - t[t[rt].r].mi[i - j];
else
t[rt].mx[i + mark] =
max(t[rt].mx[i + mark], t[t[rt].l].mx[j] - t[t[rt].r].mi[i - j]);
if (t[rt].mi[i + mark] == END)
t[rt].mi[i + mark] = t[t[rt].l].mi[j] - t[t[rt].r].mx[i - j];
else
t[rt].mi[i + mark] =
min(t[rt].mi[i + mark], t[t[rt].l].mi[j] - t[t[rt].r].mx[i - j]);
}
}
}
void solve() {
int p, m;
cin >> s;
cin >> p >> m;
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
memset(t, 0, sizeof(t));
if (s[0] != '(') {
cout << s << endl;
return;
}
int len = strlen(s);
for (int i = 0, count = 0; i < len; i++) {
if (s[i] == '(')
stk[count++] = i;
else if (s[i] == ')')
f[stk[--count]] = i, h[i] = stk[count];
else if (s[i] == '?')
g[stk[count - 1]] = 0;
else if (s[i] == '-')
g[stk[count - 1]] = -1, m--;
else if (s[i] == '+')
g[stk[count - 1]] = 1, p--;
}
if (p < m)
mk = 1;
else
mk = -1;
tmin = min(p, m);
init(1);
build(0, f[0], 1);
cout << t[1].mx[tmin] << endl;
}
int main() {
solve();
int mark_EOF;
if (scanf("%d", &mark_EOF) == EOF) return 0;
return 0;
}
| 7 |
#include <bits/stdc++.h>
struct Star {
int x, y, s;
};
int n, q, c;
Star stars[100010];
int starCount[110][110];
int initBright[110][110][11];
int main() {
scanf("%d%d%d", &n, &q, &c);
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &(stars[i].x), &(stars[i].y), &(stars[i].s));
starCount[stars[i].x][stars[i].y]++;
initBright[stars[i].x][stars[i].y][stars[i].s]++;
}
for (int y = 1; y <= 100; y++) {
for (int x = 1; x <= 100; x++) {
starCount[x][y] = starCount[x][y] + starCount[x - 1][y] +
starCount[x][y - 1] - starCount[x - 1][y - 1];
for (int i = 0; i <= c; i++)
initBright[x][y][i] = initBright[x][y][i] + initBright[x - 1][y][i] +
initBright[x][y - 1][i] -
initBright[x - 1][y - 1][i];
}
}
int t, x1, y1, x2, y2;
int xd, yd;
int totalBright[11], totalStars;
int total;
for (int i = 0; i < q; i++) {
scanf("%d%d%d%d%d", &t, &x1, &y1, &x2, &y2);
xd = x1 - 1;
yd = y1 - 1;
totalStars = starCount[x2][y2] - starCount[xd][y2] - starCount[x2][yd] +
starCount[xd][yd];
t = t % (c + 1);
total = 0;
for (int i = 0; i <= c; i++) {
totalBright[i] = initBright[x2][y2][i] - initBright[xd][y2][i] -
initBright[x2][yd][i] + initBright[xd][yd][i];
if (totalBright[i] == 0) continue;
int temp = (c + 1) * totalBright[i];
total = total + (i * totalBright[i] + totalBright[i] * t) % temp;
}
printf("%d\n", total);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int freq[100001];
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void sw(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}
void solve() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int b[6] = {0};
int i1 = INT_MIN, i2 = INT_MIN, i3 = INT_MIN, i4 = INT_MIN, i5 = INT_MIN,
i6 = INT_MIN;
for (int i = 0; i < n; i++) {
if (a[i] == 4) {
b[0]++;
} else if (a[i] == 8) {
if (b[0] > 0) {
b[0]--;
b[1]++;
}
} else if (a[i] == 15) {
if (b[1] > 0) {
b[1]--;
b[2]++;
}
} else if (a[i] == 16) {
if (b[2] > 0) {
b[2]--;
b[3]++;
}
} else if (a[i] == 23) {
if (b[3] > 0) {
b[3]--;
b[4]++;
}
} else if (a[i] == 42) {
if (b[4] > 0) {
b[4]--;
b[5]++;
}
}
}
cout << n - (6 * b[5]) << endl;
return;
}
int main() {
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
long long ans = 0;
vector<long long> a(n);
for (int i = 0; i <= (n - 1); ++i) {
cin >> a[i];
}
for (int i = 1; i <= (n - 1); ++i) {
long long r = a[i - 1] / k + ((a[i - 1] % k == 0) ? 0 : 1);
long long c = r * k;
c -= a[i - 1];
a[i] -= min(a[i], c);
ans += r;
}
ans += a[n - 1] / k + ((a[n - 1] % k == 0) ? 0 : 1);
cout << ans << endl;
}
int main() {
clock_t beg = clock();
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
clock_t end = clock();
fprintf(stderr, "%lf\n", (double)(end - beg) / CLOCKS_PER_SEC);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int d, k;
int n, m;
string s;
vector<int> pw(vector<int> a, int p) {
if (p == 0) {
return a;
}
if (p % 2 == 0) {
vector<int> r = pw(a, p / 2);
vector<int> res(n);
for (int i = 0; i < n; i++) {
res[i] = r[r[i]];
}
return res;
}
vector<int> res = pw(a, p - 1);
vector<int> res2(n);
int tot = 0;
for (int i = 0; tot < k; i++) {
for (int j = i; j < k; j += d) {
res2[tot] = res[j];
tot++;
}
}
for (int i = k; i < n; i++) res2[i] = res[i];
int first = res2[0];
for (int i = 1; i < n; i++) {
res2[i - 1] = res2[i];
}
res2[n - 1] = first;
return res2;
}
int main() {
char S[1000001];
scanf("%s", &S);
s = string(S);
n = s.size();
cin >> m;
vector<int> a(n);
for (int i = 0; i < m; i++) {
scanf("%d %d", &k, &d);
for (int j = 0; j < n; j++) a[j] = j;
a = pw(a, n - k + 1);
for (int j = 0; j < n; j++) {
S[j] = s[a[(k - 1 + j) % n]];
}
s = string(S);
printf("%s\n", S);
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> mmap;
int n, m, x, c = 0;
cin >> n >> m;
int arr[n + 1], tab[n + 1];
for (int i = 1; i < n + 1; i++) {
cin >> arr[i];
}
mmap[arr[n]] = 1;
tab[n] = 1;
for (int i = n - 1; i > 0; i--) {
if (mmap[arr[i]]) {
tab[i] = tab[i + 1];
} else {
tab[i] = tab[i + 1] + 1;
}
mmap[arr[i]]++;
}
for (int i = 0; i < m; i++) {
cin >> x;
cout << tab[x] << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bitset<300000010> bit;
int main() {
bit.set();
int l, r;
scanf("%d%d", &l, &r);
if (l > r) {
printf("0\n");
return 0;
}
int t = (int)sqrt(1.0 * r) + 1;
for (int i = 3; i <= t; i += 2)
if (bit[i]) {
int l = i * 2;
for (int j = i * i; j <= r; j += l) bit[j] = false;
}
int sum = 0;
int cnt = l / 4;
cnt = cnt * 4 + 1;
if (cnt < 5) cnt = 5;
for (int i = cnt; i <= r; i += 4)
if (bit[i] & i >= l) sum++;
printf("%d\n", sum + (l <= 2 && r >= 2));
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf64 = 1e18;
vector<int> get_z(const string &s) {
int n = (int)s.size();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i < r) z[i] = min(r - i, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] > r) l = i, r = i + z[i];
}
return z;
}
vector<int> get_p(const string &s) {
int n = (int)s.size();
vector<int> p(n);
for (int i = 1; i < n; i++) {
int j = p[i - 1];
while (j > 0 && s[i] != s[j]) j = p[j - 1];
if (s[i] == s[j]) j++;
p[i] = j;
}
return p;
}
int main() {
string s, t;
cin >> s >> t;
string ts = t + "#" + s;
long long res = 0;
vector<int> z = get_z(t);
vector<int> p((int)ts.size());
for (int i = 1; i < (int)t.size(); i++) {
int j = p[i - 1];
while (j > 0 && ts[i] != ts[j]) j = p[j - 1];
if (ts[i] == ts[j]) j++;
p[i] = j;
}
for (int i = (int)t.size() + 2; i < (int)ts.size(); i++) {
int j = p[i - 1];
while (j > 0 && ts[i] != ts[j]) j = p[j - 1];
if (ts[i] == ts[j]) j++;
p[i] = j;
}
for (int i = 0; i < (int)ts.size(); i++) {
int &j = p[i];
while (j >= (int)t.size()) j = p[j - 1];
}
vector<int> dp((int)t.size());
for (int k = 0; k < (int)t.size(); k++) {
dp[k] = z[k];
if (k > 0) {
dp[k] = max(dp[k], dp[p[k - 1]]);
}
}
for (int i = 0; i < (int)s.size(); i++) {
int mn = 0;
int k = p[(int)t.size() + 1 + i];
if (k > 0) mn = dp[k];
res += (int)t.size() - mn;
}
cout << res << "\n";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, k, num, fr = 0, rem, tot = 0;
string second;
vector<int> ans;
cin >> n >> a >> b >> k;
cin >> second;
second += "1";
for (int i = 0; i < second.length(); i++) {
if (second[i] == '0')
fr++;
else {
num = fr / b;
rem = fr - num * b;
tot += num;
fr = 0;
}
}
tot -= a;
tot++;
cerr << "tot"
<< ": " << tot << '\n';
;
for (int i = 0; i < second.length(); i++) {
if (second[i] == '0')
fr++;
else {
num = fr / b;
if (!num) goto jump;
rem = fr - num * b;
for (int j = 1; tot > 0 && -fr + j * b - 1 < 0; j++) {
ans.push_back(i - fr + j * b);
tot--;
}
jump:
fr = 0;
}
}
cout << ans.size() << '\n';
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc) {
o << "{";
for (const T& v : vc) o << v << ",";
o << "}";
return o;
}
using ll = long long;
template <class T>
using V = vector<T>;
template <class T>
using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
struct Aho {
using NP = Aho*;
map<int, NP> nx;
NP p = nullptr;
ll cnt = 0;
NP add(int c) {
if (nx[c] == nullptr) {
nx[c] = new Aho();
nx[c]->p = this;
}
return nx[c];
}
void Vanish() {
for (auto it : nx) {
it.second->Vanish();
}
delete this;
}
};
stack<int> s;
ll solve() {
int N;
cin >> N;
V<int> a(N);
for (int i = 0; i < (int)(N); i++) cin >> a[i];
function<ll(int, int)> f = [&](int l, int r) {
if (r - l == 1) return 0LL;
int m = (l + r) / 2;
ll res = f(l, m) + f(m, r);
auto T = new Aho();
auto now = T;
while (!s.empty()) s.pop();
s.push(-1);
for (int i = m - 1; i >= l; i--) {
if (s.top() == a[i]) {
s.pop();
now = now->p;
} else {
s.push(a[i]);
now = now->add(a[i]);
}
now->cnt++;
}
now = T;
while (!s.empty()) s.pop();
s.push(-1);
for (int i = m; i < r; i++) {
if (s.top() == a[i]) {
s.pop();
now = now->p;
} else {
s.push(a[i]);
now = now->add(a[i]);
}
res += now->cnt;
}
T->Vanish();
return res;
};
return f(0, N);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int T;
cin >> T;
for (int _ = 0; _ < (int)(T); _++) cout << solve() << '\n';
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const string st = "AHIMOTUVWXY";
string s;
int main() {
ios::sync_with_stdio(0);
cin >> s;
int n = s.size();
for (int i = 0; i <= n - 1; ++i)
if (st.find(s[i], 0) == -1) {
cout << "NO";
return 0;
}
string s1 = s;
reverse(s1.begin(), s1.end());
if (s1 == s)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
bool typetest;
inline void fastIOfileinput() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
typetest = 0;
}
const long long N = 5e5 + 5;
long long n, m;
long long a[N], b[N];
const long long Inf = 2e9;
void Read() {
cin >> n >> m;
for (long long i = 1; i <= n; ++i) cin >> a[i];
for (long long i = 1; i <= m; ++i) cin >> b[i];
}
long long Cal(long long l, long long r, long long optl, long long optr) {
if (l > r) return 0;
vector<long long> b;
b.resize(r - l + 5, Inf);
b[0] = -Inf;
long long ans = 0;
for (long long i = l; i <= r; ++i) {
if (a[i] < optl || a[i] > optr || a[i] - optl < i - l ||
optr - a[i] < r - i)
continue;
long long j = upper_bound(b.begin(), b.end(), a[i] - i) - b.begin();
b[j] = min(b[j], a[i] - i);
ans = max(ans, j);
}
return r - l + 1 - ans;
}
void Solve() {
sort(b + 1, b + m + 1);
b[m + 1] = n + 1;
a[n + 1] = Inf;
a[0] = -Inf;
for (long long i = 1; i <= m; ++i)
if (a[b[i + 1]] - a[b[i]] < b[i + 1] - b[i]) {
cout << "-1";
return;
}
long long ans = 0;
for (long long i = 0; i <= m; ++i)
ans += Cal(b[i] + 1, b[i + 1] - 1, a[b[i]] + 1, a[b[i + 1]] - 1);
cout << ans;
}
int32_t main() {
fastIOfileinput();
if (typetest) {
long long t;
cin >> t;
for (long long v = 1; v <= t; ++v) {
Read();
Solve();
}
} else {
Read();
Solve();
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, temp;
int main() {
scanf("%d%d%d", &n, &m, &k);
if (n % 2 == 0 || m < (n + 1) / 2) {
printf("0\n");
return 0;
}
long long minA = (m / ((n + 1) / 2)) * k;
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
if (i % 2 == 0 && temp < minA) minA = temp;
}
printf("%d\n", minA);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long a[100100];
vector<pair<int, int> > tree[100100];
bool bad[100100];
void dfs(int u, int p, long long val) {
if (val > a[u]) {
bad[u] = 1;
return;
}
val = max(0LL, val);
for (auto v : tree[u])
if (v.first != p) dfs(v.first, u, val + v.second);
}
int count(int u, int p, bool ok) {
int ans = 0;
ok &= !bad[u];
if (!ok) ans++;
for (auto v : tree[u])
if (v.first != p) ans += count(v.first, u, ok);
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int p, x;
for (int i = 1; i < n; i++) {
cin >> p >> x;
p--;
tree[i].push_back({p, x});
tree[p].push_back({i, x});
}
dfs(0, 0, 0);
cout << count(0, 0, 1) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
int n;
cin >> n;
while (n > 0) {
if (n % 1000 == 144) {
n /= 1000;
} else if (n % 100 == 14) {
n /= 100;
} else if (n % 10 == 1) {
n /= 10;
} else {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 100010;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n, x, aa, bb;
cin >> n >> x >> aa >> bb;
long long int a = min(aa, bb);
long long int b = max(aa, bb);
long long int ans = b - a;
ans += x;
if (ans >= n) {
ans = n - 1;
}
cout << ans << endl;
}
return (0);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int n, m, f[100005], c[100005];
bool mrk[100005];
vector<int> adj[100005];
int fnd(int x) { return f[x] == x ? x : f[x] = fnd(f[x]); }
int uni(int x, int y) {
x = fnd(x);
y = fnd(y);
f[y] = x;
}
void dfs(int x, int y) {
c[x] = y;
for (int i = 0; i < adj[x].size(); i++) {
int z = adj[x][i];
if (!c[z]) dfs(z, y ^ 2);
}
}
int main() {
scanf("%d%d", &n, &m);
memset(mrk, true, sizeof(mrk));
vector<pair<int, int> > edg;
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if (z)
uni(x, y);
else
edg.push_back(make_pair(x, y));
}
for (int i = 1; i <= n; i++) mrk[fnd(i)] = false;
for (int i = 0; i < edg.size(); i++) {
adj[fnd(edg[i].first)].push_back(fnd(edg[i].second));
adj[fnd(edg[i].second)].push_back(fnd(edg[i].first));
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!mrk[i] && !c[i]) {
dfs(i, 1);
if (ans)
ans = ans * 2 % MOD;
else
ans = 1;
}
}
for (int i = 0; i < edg.size(); i++) {
if (c[fnd(edg[i].first)] == c[fnd(edg[i].second)]) ans = 0;
}
cout << ans;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 400005;
int n, m;
vector<int> g[MX];
int dfs_num[MX], dfs_low[MX];
int vis_cntr;
int root_children, root;
int articulation_points[MX];
int vis[MX];
int component_cnt = 1;
int components[MX];
int components_map[MX];
set<pair<int, int> > bridges;
set<pair<int, pair<int, int> > > roads;
set<pair<int, pair<int, int> > > orig_roads;
int orientation[MX];
void dfs(int u, int pa) {
dfs_num[u] = dfs_low[u] = vis_cntr++;
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j];
if (v == pa) continue;
if (dfs_num[v] == 0) {
if (u == root) root_children++;
dfs(v, u);
if (dfs_low[v] >= dfs_num[u]) articulation_points[u] = 1;
if (dfs_low[v] > dfs_num[u]) bridges.insert(make_pair(u, v));
dfs_low[u] = min(dfs_low[u], dfs_low[v]);
} else {
dfs_low[u] = min(dfs_low[u], dfs_num[v]);
}
}
}
bool is_bridge(int u, int v) {
return !(bridges.find(make_pair(u, v)) == bridges.end() &&
bridges.find(make_pair(v, u)) == bridges.end());
}
void get_components(int u) {
if (vis[u]) return;
vis[u] = 1;
components[u] = component_cnt;
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j];
if (!is_bridge(u, v)) {
get_components(v);
}
}
}
void assign(int u, int pa, int flag) {
if (vis[u]) return;
vis[u] = 1;
for (int j = 0; j < g[u].size(); j++) {
int rev_orientation = 0;
int v = g[u][j];
if (v == pa) continue;
auto it = roads.lower_bound(make_pair(u, make_pair(v, 0)));
if (it == roads.end() || it->first != u || it->second.first != v) {
it = roads.lower_bound(make_pair(v, make_pair(u, 0)));
rev_orientation = 1;
}
int road_num = it->second.second;
int fflag = flag;
if (is_bridge(u, v) && flag == 1) fflag = -1;
assign(v, u, fflag);
if (!orientation[road_num])
orientation[road_num] = rev_orientation ? fflag * -1 : fflag;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
roads.insert(make_pair(u, make_pair(v, i)));
orig_roads.insert(make_pair(i, make_pair(u, v)));
}
for (int i = 1; i <= n; i++) {
if (dfs_num[i] == 0) {
vis_cntr = 1;
root_children = 0;
root = i;
dfs(i, -1);
articulation_points[i] = root_children > 1 ? 1 : 0;
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
get_components(i);
component_cnt++;
}
}
for (int i = 1; i <= n; i++) {
components_map[components[i]]++;
}
int maxc = 0, maxv = 0;
for (int i = 1; i <= n; i++) {
if (maxc < components_map[components[i]]) {
maxc = components_map[components[i]];
maxv = i;
}
}
memset(vis, 0, sizeof(vis));
assign(maxv, -1, 1);
cout << maxc << endl;
int c = 0;
for (auto it = orig_roads.begin(); it != orig_roads.end(); it++, c++) {
if (orientation[c] == 1)
cout << it->second.first << " " << it->second.second << endl;
else
cout << it->second.second << " " << it->second.first << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
int t, n, m, k, x, y;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int xm = MOD, xM = -MOD, ym = MOD, yM = -MOD;
int x[n];
pair<int, int> y[n];
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i].first;
y[i].second = i;
xm = min(xm, x[i]);
xM = max(xM, x[i]);
yM = max(yM, y[i].first);
ym = min(ym, y[i].first);
}
long long int ans = 2 * (long long int)(xM + yM - xm - ym);
sort(y, y + n);
int xmx = -MOD, xmn = MOD, summn = MOD, diffmn = MOD;
long long int ans2 = 0;
for (int i = 0; i < n; i++) {
int in = y[i].second;
if (i >= 2) {
if (xmx >= x[in])
ans2 = max(ans2, (long long int)2 * (y[i].first - y[0].first) +
2 * (xmx - x[in]));
if (xmn <= x[in])
ans2 = max(ans2, (long long int)2 * (y[i].first - y[0].first) +
2 * (x[in] - xmn));
xmx = max(xmx, x[in]);
xmn = min(xmn, x[in]);
ans2 = max(ans2, (long long int)2 * (xmx + y[i].first - summn));
ans2 = max(ans2, (long long int)2 * (y[i].first - diffmn - xmn));
}
xmx = max(xmx, x[in]);
xmn = min(xmn, x[in]);
summn = min(summn, x[in] + y[i].first);
diffmn = min(diffmn, -x[in] + y[i].first);
}
cout << ans2 << " ";
for (int i = 3; i < n; i++) {
cout << ans << " ";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int the_most_frequent_number(int z[]) {
int freq_counter = 0;
int most_frequent = 0;
for (int i = 0; i < 6; i++) {
if (freq_counter != 4) {
freq_counter = 1;
most_frequent = 0;
} else {
break;
}
for (int j = i + 1; j < 6; j++) {
if (z[i] == z[j]) {
most_frequent = z[i];
freq_counter++;
}
}
}
if (freq_counter == 4) {
return most_frequent;
} else {
return -1;
}
}
int main() {
int a[6];
for (int i = 0; i < 6; i++) {
cin >> a[i];
}
sort(a, a + 6);
int legs = the_most_frequent_number(a);
if (legs > 0) {
int x = 0, y = 0;
bool flag = false;
for (int j = 0; j < 6; j++) {
if (a[j] != legs) {
if (!flag) {
x = a[j];
flag = true;
} else {
y = a[j];
}
}
}
if (x > y || y > x) {
cout << "Bear" << endl;
} else if (x == y) {
cout << "Elephant" << endl;
}
} else {
cout << "Alien" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > bh;
priority_queue<int, vector<int>, less<int> > sh;
const int N = 2e5 + 20;
struct lhy {
int v, id;
} a[N];
int n, k;
long long ans[N], c[N], tmp[N];
inline int cmp(const lhy &a, const lhy &b) { return a.v < b.v; }
inline int ask(int x) {
int nowans = 0;
for (; x <= n; x += x & -x) nowans += c[x];
return nowans;
}
inline void add(int x) {
for (; x; x -= x & -x) c[x]++;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].v);
a[i].id = i;
}
sort(a + 1, a + 1 + n, cmp);
long long sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; i++) {
ans[i] = ans[i - 1] + ask(a[i].id);
add(a[i].id);
int x = a[i].id;
if (sh.empty() || sh.top() >= x) {
sh.push(x);
sum1 += x;
} else {
bh.push(x);
sum2 += x;
}
if (sh.size() < (i + 1) / 2) {
x = bh.top();
bh.pop();
sum2 -= x;
sh.push(x);
sum1 += x;
}
if (sh.size() > (i + 1) / 2) {
x = sh.top();
sh.pop();
sum1 -= x;
bh.push(x);
sum2 += x;
}
tmp[i] = 1ll * sh.top() * sh.size() - sum1 - 1ll * sh.top() * bh.size() +
sum2 - 1ll * sh.size() * (sh.size() - 1) / 2 -
1ll * bh.size() * (bh.size() + 1) / 2;
}
for (int i = 1; i <= n; i++) printf("%lld ", ans[i] + tmp[i]);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 5010;
inline long long read() {
long long s = 0, w = 1;
register char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return s * w;
}
long long n, a[N], Q, F[N][N];
signed main() {
n = read();
for (register long long i = 1; i <= n; i++) a[i] = read(), F[i][i] = a[i];
for (register long long len = 2; len <= n; len++)
for (register long long i = 1; i + len - 1 <= n; i++) {
long long j = i + len - 1;
F[i][j] = F[i + 1][j] ^ F[i][j - 1];
}
for (register long long len = 2; len <= n; len++)
for (register long long i = 1; i + len - 1 <= n; i++) {
long long j = i + len - 1;
F[i][j] = max(F[i][j], max(F[i + 1][j], F[i][j - 1]));
}
Q = read();
for (register long long i = 1; i <= Q; i++) {
long long l, r;
l = read(), r = read();
printf("%lld\n", F[l][r]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = int(2e9);
struct task {
int t;
int k;
int d;
task(int t1 = 0, int k1 = 0, int d1 = 0) : t(t1), k(k1), d(d1) {}
task(const task& tsk) : t(tsk.t), k(tsk.k), d(tsk.d) {}
};
struct server {
int id;
int t;
server(int i = 0, int t1 = 0) : id(i), t(t1) {}
server(const server& s) : id(s.id), t(s.t) {}
};
struct Compare {
bool operator()(const server& lhs, const server& rhs) {
return (lhs.id < rhs.id);
}
};
struct Compare1 {
bool operator()(const server& lhs, const server& rhs) {
return (lhs.t < rhs.t) || ((lhs.t == rhs.t) && (lhs.id < rhs.id));
}
};
int main() {
int n, q;
cin >> n >> q;
vector<task> tsk(q);
for (int i = 0; i < q; i++) {
cin >> tsk[i].t >> tsk[i].k >> tsk[i].d;
}
std::set<server, Compare> st;
std::set<server, Compare>::iterator it1, it2;
std::set<server, Compare1> st1;
std::set<server, Compare1>::iterator it1_, it2_;
for (int i = 0; i < n; i++) {
st.insert(server(i + 1));
}
for (int i = 0; i < q; i++) {
task& ts = tsk[i];
server s1(n + 1, ts.t);
it1_ = st1.upper_bound(s1);
if (it1_ != st1.begin()) {
for (std::set<server, Compare1>::iterator it = st1.begin(); it != it1_;
it++) {
st.insert(*it);
}
st1.erase(st1.begin(), it1_);
}
if (st.size() < ts.k)
cout << -1 << endl;
else {
int sm = 0;
int t = ts.t;
std::set<server, Compare>::iterator it = st.begin();
int j = 0;
for (; it != st.end(); it++) {
server s(it->id, ts.t + ts.d);
st1.insert(s);
sm += it->id;
j++;
if (j == ts.k) break;
}
it++;
st.erase(st.begin(), it);
cout << sm << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long bit[2][1000006 + 5];
long long a[1000006 + 5];
long long n, k, A, B;
void update(long long x, long long v) {
long long v1 = min(v, A);
long long v2 = min(v, B);
v1 = max(v1, -A);
v2 = max(v2, -B);
for (; x < 1000006; x += (x & -x)) {
bit[0][x] += v1;
bit[1][x] += v2;
}
}
long long query(long long x, long long b) {
long long r = 0;
for (; x > 0; x -= (x & -x)) {
r += bit[b][x];
}
return r;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long q, x, y, t;
cin >> n >> k >> A >> B >> q;
while (q--) {
cin >> t;
if (t == 1) {
cin >> x >> y;
update(x, -a[x]);
a[x] += y;
update(x, a[x]);
} else {
cin >> x;
cout << query(x - 1, 1) + query(1000006 - 1, 0) - query(x + k - 1, 0)
<< endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, neg = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') neg = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x * neg;
}
inline void print(int x) {
if (x < 0) {
putchar('-');
print(abs(x));
return;
}
if (x <= 9)
putchar(x + '0');
else {
print(x / 10);
putchar(x % 10 + '0');
}
}
inline int qpow(int x, int e, int _MOD) {
int ans = 1;
while (e) {
if (e & 1) ans = ans * x % _MOD;
x = x * x % _MOD;
e >>= 1;
}
return ans;
}
int n1 = read(), n2 = read(), m = read();
int u[2005], v[2005];
int head[5005];
struct edge {
int to, nxt, cap;
} e[3000005];
int ecnt = 1;
inline void addedge(int u, int v, int f) {
e[++ecnt].to = v;
e[ecnt].cap = f;
e[ecnt].nxt = head[u];
head[u] = ecnt;
}
int dep[5005];
inline bool bfs(int s, int t) {
queue<int> q;
memset(dep, -1, sizeof(dep));
q.push(s);
dep[s] = 0;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = head[cur]; i; i = e[i].nxt) {
int to = e[i].to;
if (dep[to] == -1 && e[i].cap) {
dep[to] = dep[cur] + 1;
q.push(to);
}
}
}
if (dep[t] != -1) return 1;
return 0;
}
inline int dfs(int x, int t, int f) {
if (x == t) return f;
int ret = 0;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (dep[y] == dep[x] + 1 && e[i].cap) {
int w = dfs(y, t, min(f - ret, e[i].cap));
e[i].cap -= w;
e[i ^ 1].cap += w;
ret += w;
if (ret == f) return f;
}
}
if (ret == 0) dep[x] = -1;
return ret;
}
inline int Dinic(int s, int t) {
int tot = 0;
while (bfs(s, t)) tot += dfs(s, t, 0x3f3f3f3f);
return tot;
}
int id[2005], deg[4005];
vector<int> ans[2005];
signed main() {
for (int i = 1; i <= m; i++) {
u[i] = read();
v[i] = read();
v[i] += n1;
deg[u[i]]++;
deg[v[i]]++;
}
int k = 0x3f3f3f3f;
for (int i = 1; i <= n1 + n2; i++) k = min(k, deg[i]);
for (int j = 1; j <= n1; j++) {
addedge(0, j, deg[j] - k);
addedge(j, 0, 0);
}
for (int j = n1 + 1; j <= n1 + n2; j++) {
addedge(j, 5000, deg[j] - k);
addedge(5000, j, 0);
}
for (int j = 1; j <= m; j++) {
addedge(u[j], v[j], 1);
addedge(v[j], u[j], 0);
id[j] = ecnt;
}
for (int i = k; i >= 0; i--) {
Dinic(0, 5000);
for (int j = 1; j <= m; j++) {
if (e[id[j]].cap == 0) {
ans[i].push_back(j);
}
}
for (int i = head[0]; i; i = e[i].nxt) e[i].cap++;
for (int i = head[5000]; i; i = e[i].nxt) e[i ^ 1].cap++;
}
for (int i = 0; i <= k; i++) {
cout << ans[i].size() << " ";
for (__typeof(ans[i].begin()) it = ans[i].begin(); it != ans[i].end(); it++)
cout << *it << " ";
puts("");
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
int main() {
int n, i, p;
float s = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &p);
s = s + p;
}
s = s / n;
printf("%f", s);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
string str;
cin >> str;
long long int count1 = 0, count2 = 0;
long long int j;
for (j = 0; j < n; j++) {
if (str[j] == '<')
count1++;
else if (str[j] == '>')
count2++;
}
if (count1 == 0 || count2 == 0) {
cout << n << endl;
continue;
}
long long int answer = 0;
for (j = 0; j < n; j++) {
if (str[j] == '-')
answer++;
else if (str[(j - 1 + n) % n] == '-')
answer++;
}
cout << answer << endl;
}
}
| 2 |
#include <bits/stdc++.h>
int isPrime(int n) {
int count;
for (count = 2; count * count <= n; count++)
if (n % count == 0) return 0;
return 1;
}
int main() {
int count, count2, count3;
int ans1 = 0, ans2 = 0, ans3 = 0;
int n;
int found = 0;
int k = 0;
scanf("%d", &n);
if (isPrime(n)) {
printf("%d\n%d", 1, n);
return 0;
}
for (count = n; count > 1 && found == 0; count--) {
if (isPrime(count)) {
for (count2 = 2; count2 <= n - count && found == 0; count2++) {
if (isPrime(count2)) {
if (count + count2 == n) {
ans1 = count;
ans2 = count2;
found = 1;
} else if (count + count2 > n) {
break;
} else {
for (count3 = 2; count3 <= n - count - count2 && found == 0;
count3++) {
if (isPrime(count3) && count + count2 + count3 == n) {
ans1 = count;
ans2 = count2;
ans3 = count3;
found = 1;
} else if (count + count2 + count3 > n) {
break;
}
}
}
}
}
}
}
if (ans1 != 0) k++;
if (ans2 != 0) k++;
if (ans3 != 0) k++;
printf("%d\n", k);
if (ans1 != 0) printf("%d ", ans1);
if (ans2 != 0) printf("%d ", ans2);
if (ans3 != 0) printf("%d", ans3);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
string s, ans;
long long int freq[10], q;
long long int m[80];
long long int pos[100005];
bool check() {
for (int i = 1; i < 64; i++) {
long long int tot = 0, cnt = 0;
for (int j = 0; j < 6; j++)
if ((i & (1 << j))) tot += freq[j];
for (int j = 1; j < 64; j++) {
if ((i & j)) cnt += m[j];
}
if (cnt < tot) return 0;
}
return 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
for (auto j : s) freq[j - 'a']++;
cin >> q;
for (int i = 1; i <= q; i++) {
long long int x;
string y;
cin >> x >> y;
long long int z = 0;
for (auto j : y) z += (1 << (j - 'a'));
pos[x] = z;
m[z]++;
}
for (int i = 1; i <= s.length(); i++) {
if (pos[i] == 0) {
pos[i] = 63;
m[pos[i]]++;
}
}
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < 6; j++) {
bool z = 0;
if ((pos[i] & (1 << j))) z = 1;
if (z && freq[j]) {
freq[j]--;
m[pos[i]]--;
if (check()) {
ans.push_back(char('a' + j));
break;
} else
freq[j]++, m[pos[i]]++;
}
}
if (ans.length() < i) return cout << "Impossible" << '\n', 0;
}
cout << ans << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int inf = INT_MAX;
const int N = 2000 + 5;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
string s, t;
cin >> s >> t;
s = ' ' + s;
t = ' ' + t;
vector<vector<int>> cnt1(n + 1, vector<int>(26)),
cnt2(n + 1, vector<int>(26));
for (int i = 1; i <= n; i++) {
cnt1[i] = cnt1[i - 1];
cnt2[i] = cnt2[i - 1];
cnt1[i][s[i] - 'a']++;
cnt2[i][t[i] - 'a']++;
}
bool impossible = false;
for (int i = 0; i < 26; i++) {
impossible |= (cnt1[n][i] - cnt2[n][i]);
}
if (impossible) {
cout << "-1\n";
continue;
}
vector<vector<int>> dp(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = INT_MAX;
if (s[i] == t[j]) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
}
if (cnt1[n][t[j] - 'a'] - cnt1[i][t[j] - 'a'] >
cnt2[n][t[j] - 'a'] - cnt2[j][t[j] - 'a']) {
dp[i][j] = min(dp[i][j], dp[i][j - 1]);
}
dp[i][j] = min(dp[i][j], 1 + dp[i - 1][j]);
}
}
cout << dp[n][n] << '\n';
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l, r, a = 1;
cin >> n >> l >> r;
int min = 0, max = 0;
for (int i = 0; i < l; i++) {
min += a;
a *= 2;
}
min = min + n - l;
a = 1;
for (int i = 0; i < r; i++) {
max += a;
a *= 2;
}
a /= 2;
for (int i = 0; i < n - r; i++) max += a;
cout << min << " " << max << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = 3.14159265358979323846264338327950288419716939937510;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int mod2 = 1000000006;
const long long infll = 0x3f3f3f3f3f3f3f3fLL;
const int dx[8] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
template <typename T>
inline bool RD(T& v) {
char c;
bool n;
while (c = getchar(), c != '-' && (c < '0' || c > '9'))
if (c == EOF) return false;
if (c == '-') {
n = true;
v = 0;
} else {
n = false;
v = c - '0';
}
while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0';
if (n) v *= -1;
return true;
}
template <typename T>
inline bool RD(T& a, T& b) {
return RD(a) && RD(b);
}
template <typename T>
inline bool RD(T& a, T& b, T& c) {
return RD(a, b) && RD(c);
}
template <typename T>
inline bool RD(T& a, T& b, T& c, T& d) {
return RD(a, b, c) && RD(d);
}
template <typename T>
inline bool RD(T& a, T& b, T& c, T& d, T& e) {
return RD(a, b, c, d) && RD(e);
}
template <typename T>
inline bool RD(T& a, T& b, T& c, T& d, T& e, T& f) {
return RD(a, b, c, d, e) && RD(f);
}
template <typename T>
inline bool RD(T& a, T& b, T& c, T& d, T& e, T& f, T& g) {
return RD(a, b, c, d, e, f) && RD(g);
}
template <typename T>
inline bool RD(T& a, T& b, T& c, T& d, T& e, T& f, T& g, T& h) {
return RD(a, b, c, d, e, f, g) && RD(h);
}
template <typename T>
inline bool RD(T& a, T& b, T& c, T& d, T& e, T& f, T& g, T& h, T& i) {
return RD(a, b, c, d, e, f, g, h) && RD(i);
}
template <typename T>
inline bool RD(T& a, T& b, T& c, T& d, T& e, T& f, T& g, T& h, T& i, T& j) {
return RD(a, b, c, d, e, f, g, h, i) && RD(j);
}
inline int RD() {
int v;
RD(v);
return v;
}
static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_;
inline static char getchar_buffered() {
return _HEAD_ == _TAIL_ &&
(_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin),
_HEAD_ == _TAIL_)
? EOF
: *_HEAD_++;
}
template <typename T>
inline bool RDB(T& v) {
char c;
bool n;
while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9'))
if (c == EOF) return false;
if (c == '-') {
n = true;
v = 0;
} else {
n = false;
v = c - '0';
}
while (c = getchar_buffered() - '0', c >= 0 && c <= 9)
v = (v << 3) + (v << 1) + c;
if (n) v *= -1;
return true;
}
template <typename T>
inline bool RDB(T& a, T& b) {
return RDB(a) && RDB(b);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c) {
return RDB(a, b) && RDB(c);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c, T& d) {
return RDB(a, b, c) && RDB(d);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c, T& d, T& e) {
return RDB(a, b, c, d) && RDB(e);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c, T& d, T& e, T& f) {
return RDB(a, b, c, d, e) && RDB(f);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c, T& d, T& e, T& f, T& g) {
return RDB(a, b, c, d, e, f) && RDB(g);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c, T& d, T& e, T& f, T& g, T& h) {
return RDB(a, b, c, d, e, f, g) && RDB(h);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c, T& d, T& e, T& f, T& g, T& h, T& i) {
return RDB(a, b, c, d, e, f, g, h) && RDB(i);
}
template <typename T>
inline bool RDB(T& a, T& b, T& c, T& d, T& e, T& f, T& g, T& h, T& i, T& j) {
return RDB(a, b, c, d, e, f, g, h, i) && RDB(j);
}
inline int RDB() {
int v;
RDB(v);
return v;
}
template <typename T>
inline void _WR(T a) {
if (a < 0) {
putchar('-');
a *= -1;
}
T t = a / 10;
if (t) _WR(t);
putchar(a - (t << 1) - (t << 3) + '0');
}
template <typename T>
inline void WR_(const T& a) {
_WR(a);
putchar(' ');
}
template <typename T>
inline void WR(const T& a) {
_WR(a);
putchar('\n');
}
template <typename T>
inline void _WR(const T& a, const T& b) {
WR_(a);
_WR(b);
}
template <typename T>
inline void WR_(const T& a, const T& b) {
WR_(a);
WR_(b);
}
template <typename T>
inline void WR(const T& a, const T& b) {
WR_(a);
WR(b);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c) {
WR_(a, b);
_WR(c);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c) {
WR_(a, b);
WR_(c);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c) {
WR_(a, b);
WR(c);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c, const T& d) {
WR_(a, b, c);
_WR(d);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c, const T& d) {
WR_(a, b, c);
WR_(d);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c, const T& d) {
WR_(a, b, c);
WR(d);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e) {
WR_(a, b, c, d);
_WR(e);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e) {
WR_(a, b, c, d);
WR_(e);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e) {
WR_(a, b, c, d);
WR(e);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f) {
WR_(a, b, c, d, e);
_WR(f);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f) {
WR_(a, b, c, d, e);
WR_(f);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f) {
WR_(a, b, c, d, e);
WR(f);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g) {
WR_(a, b, c, d, e, f);
_WR(g);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g) {
WR_(a, b, c, d, e, f);
WR_(g);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g) {
WR_(a, b, c, d, e, f);
WR(g);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h) {
WR_(a, b, c, d, e, f, g);
_WR(h);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h) {
WR_(a, b, c, d, e, f, g);
WR_(h);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h) {
WR_(a, b, c, d, e, f, g);
WR(h);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i) {
WR_(a, b, c, d, e, f, g, h);
_WR(i);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i) {
WR_(a, b, c, d, e, f, g, h);
WR_(i);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i) {
WR_(a, b, c, d, e, f, g, h);
WR(i);
}
template <typename T>
inline void _WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i, const T& j) {
WR_(a, b, c, d, e, f, g, h, i);
_WR(j);
}
template <typename T>
inline void WR_(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i, const T& j) {
WR_(a, b, c, d, e, f, g, h, i);
WR_(j);
}
template <typename T>
inline void WR(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i, const T& j) {
WR_(a, b, c, d, e, f, g, h, i);
WR(j);
}
template <typename A>
inline istream& IN(A& a) {
return std::cin >> a;
}
template <typename A, typename B>
inline istream& IN(A& a, B& b) {
return std::cin >> a >> b;
}
template <typename A, typename B, typename C>
inline istream& IN(A& a, B& b, C& c) {
return std::cin >> a >> b >> c;
}
template <typename A, typename B, typename C, typename D>
inline istream& IN(A& a, B& b, C& c, D& d) {
return std::cin >> a >> b >> c >> d;
}
template <typename A, typename B, typename C, typename D, typename E>
inline istream& IN(A& a, B& b, C& c, D& d, E& e) {
return std::cin >> a >> b >> c >> d >> e;
}
template <typename A, typename B, typename C, typename D, typename E,
typename F>
inline istream& IN(A& a, B& b, C& c, D& d, E& e, F& f) {
return std::cin >> a >> b >> c >> d >> e >> f;
}
template <typename A, typename B, typename C, typename D, typename E,
typename F, typename G>
inline istream& IN(A& a, B& b, C& c, D& d, E& e, F& f, G& g) {
return std::cin >> a >> b >> c >> d >> e >> f >> g;
}
template <typename A, typename B, typename C, typename D, typename E,
typename F, typename G, typename H>
inline istream& IN(A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h) {
return std::cin >> a >> b >> c >> d >> e >> f >> g >> h;
}
template <typename A, typename B, typename C, typename D, typename E,
typename F, typename G, typename H, typename I>
inline istream& IN(A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i) {
return std::cin >> a >> b >> c >> d >> e >> f >> g >> h >> i;
}
template <typename A, typename B, typename C, typename D, typename E,
typename F, typename G, typename H, typename I, typename J>
inline istream& IN(A& a, B& b, C& c, D& d, E& e, F& f, G& g, H& h, I& i, J& j) {
return std::cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j;
}
inline std::ostream& OUT() { return std::cout << std::endl; }
template <typename T>
inline ostream& _OUT(const T& a) {
return std::cout << a;
}
template <typename T>
inline ostream& OUT_(const T& a) {
return std::cout << a << ' ';
}
template <typename T>
inline ostream& OUT(const T& a) {
return std::cout << a << std::endl;
}
template <typename T, typename U>
inline ostream& _OUT(const T& a, const U& b) {
return std::cout << a << " " << b;
}
template <typename T, typename U>
inline ostream& OUT_(const T& a, const U& b) {
return std::cout << a << " " << b << " ";
}
template <typename T, typename U>
inline ostream& OUT(const T& a, const U& b) {
return std::cout << a << " " << b << std::endl;
}
template <typename T, typename U, typename V>
inline ostream& _OUT(const T& a, const U& b, const V& c) {
OUT_(a, b);
return _OUT(c);
}
template <typename T, typename U, typename V>
inline ostream& OUT_(const T& a, const U& b, const V& c) {
OUT_(a, b);
return OUT_(c);
}
template <typename T, typename U, typename V>
inline ostream& OUT(const T& a, const U& b, const V& c) {
OUT_(a, b);
return OUT(c);
}
template <typename T, typename U, typename V, typename W>
inline ostream& _OUT(const T& a, const U& b, const V& c, const W& d) {
OUT_(a, b, c);
return _OUT(d);
}
template <typename T, typename U, typename V, typename W>
inline ostream& OUT_(const T& a, const U& b, const V& c, const W& d) {
OUT_(a, b, c);
return OUT_(d);
}
template <typename T, typename U, typename V, typename W>
inline ostream& OUT(const T& a, const U& b, const V& c, const W& d) {
OUT_(a, b, c);
return OUT(d);
}
template <typename T, typename U, typename V, typename W, typename X>
inline ostream& _OUT(const T& a, const U& b, const V& c, const W& d,
const X& e) {
OUT_(a, b, c, d);
return _OUT(e);
}
template <typename T, typename U, typename V, typename W, typename X>
inline ostream& OUT_(const T& a, const U& b, const V& c, const W& d,
const X& e) {
OUT_(a, b, c, d);
return OUT_(e);
}
template <typename T, typename U, typename V, typename W, typename X>
inline ostream& OUT(const T& a, const U& b, const V& c, const W& d,
const X& e) {
OUT_(a, b, c, d);
return OUT(e);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y>
inline ostream& _OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f) {
OUT_(a, b, c, d, e);
return _OUT(f);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y>
inline ostream& OUT_(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f) {
OUT_(a, b, c, d, e);
return OUT_(f);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y>
inline ostream& OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f) {
OUT_(a, b, c, d, e);
return OUT(f);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z>
inline ostream& _OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g) {
OUT_(a, b, c, d, e, f);
return _OUT(g);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z>
inline ostream& OUT_(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g) {
OUT_(a, b, c, d, e, f);
return OUT_(g);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z>
inline ostream& OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g) {
OUT_(a, b, c, d, e, f);
return OUT(g);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A>
inline ostream& _OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h) {
OUT_(a, b, c, d, e, f, g);
return _OUT(h);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A>
inline ostream& OUT_(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h) {
OUT_(a, b, c, d, e, f, g);
return OUT_(h);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A>
inline ostream& OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h) {
OUT_(a, b, c, d, e, f, g);
return OUT(h);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A, typename B>
inline ostream& _OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h, const B& i) {
OUT_(a, b, c, d, e, f, g, h);
return _OUT(i);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A, typename B>
inline ostream& OUT_(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h, const B& i) {
OUT_(a, b, c, d, e, f, g, h);
return OUT_(i);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A, typename B>
inline ostream& OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h, const B& i) {
OUT_(a, b, c, d, e, f, g, h);
return OUT(i);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A, typename B, typename C>
inline ostream& _OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h, const B& i,
const C& j) {
OUT_(a, b, c, d, e, f, g, h, i);
return _OUT(j);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A, typename B, typename C>
inline ostream& OUT_(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h, const B& i,
const C& j) {
OUT_(a, b, c, d, e, f, g, h, i);
return OUT_(j);
}
template <typename T, typename U, typename V, typename W, typename X,
typename Y, typename Z, typename A, typename B, typename C>
inline ostream& OUT(const T& a, const U& b, const V& c, const W& d, const X& e,
const Y& f, const Z& g, const A& h, const B& i,
const C& j) {
OUT_(a, b, c, d, e, f, g, h, i);
return OUT(j);
}
template <typename A>
inline void clr(A& a) {
a.clear();
}
template <typename A, typename B>
inline void clr(A& a, B& b) {
a.clear();
b.clear();
}
template <typename A, typename B, typename C>
inline void clr(A& a, B& b, C& c) {
a.clear();
b.clear();
c.clear();
}
template <typename A, typename B, typename C, typename D>
inline void clr(A& a, B& b, C& c, D& d) {
a.clear();
b.clear();
c.clear();
d.clear();
}
template <typename A, typename B, typename C, typename D, typename E>
inline void clr(A& a, B& b, C& c, D& d, E& e) {
a.clear();
b.clear();
c.clear();
d.clear();
e.clear();
}
template <typename A, typename B, typename C, typename D, typename E,
typename F>
inline void clr(A& a, B& b, C& c, D& d, E& e, F& f) {
a.clear();
b.clear();
c.clear();
d.clear();
e.clear();
f.clear();
}
inline long long madd(long long a, long long b,
const long long mod = 1000000007) {
long long ret = (a + b) % mod;
if (ret < 0) ret += mod;
return ret;
}
inline long long msub(long long a, long long b,
const long long mod = 1000000007) {
long long ret = (a - b) % mod;
if (ret < 0) ret += mod;
return ret;
}
inline long long mmul(long long a, long long b,
const long long mod = 1000000007) {
return a * b % mod;
}
inline long long mpow(long long x, long long k,
const long long mod = 1000000007) {
x %= mod;
long long ret = 1;
while (k > 0) {
if (k & 1) ret = ret * x % mod;
x = x * x % mod;
k >>= 1;
}
return ret;
}
template <typename T, typename U>
inline bool cmax(T& a, const U& b) {
return a < b ? a = b, true : false;
}
template <typename T, typename U>
inline bool cmin(T& a, const U& b) {
return a > b ? a = b, true : false;
}
template <typename T, typename U>
inline T cadd(T& a, const U& b, const int mod = 1000000007) {
a = (a * 1LL + b) % mod;
if (a < 0) a += mod;
return a;
}
template <typename T, typename U>
inline T csub(T& a, const U& b, const int mod = 1000000007) {
a = (a * 1LL - b) % mod;
if (a < 0) a += mod;
return a;
}
template <typename T, typename U>
inline T cmul(T& a, const U& b, const int mod = 1000000007) {
return a = (a * 1LL * b) % mod;
}
inline long long cpow(long long& x, long long k,
const long long mod = 1000000007) {
x %= mod;
long long ret = 1;
while (k > 0) {
if (k & 1) ret = ret * x % mod;
x = x * x % mod;
k >>= 1;
}
return x = ret;
}
template <typename T>
inline T clow(T& a, const int mod = 1000000007) {
while (a < 0) a += mod;
return a;
}
template <typename T>
inline T cup(T& a, const int mod = 1000000007) {
while (a >= mod) a -= mod;
return a;
}
template <typename T>
inline T cboth(T& a, const int mod = 1000000007) {
while (a < 0) a += mod;
while (a >= mod) a -= mod;
return a;
}
template <typename T>
inline T vlow(T a, const int mod = 1000000007) {
while (a < 0) a += mod;
return a;
}
template <typename T>
inline T vup(T a, const int mod = 1000000007) {
while (a >= mod) a -= mod;
return a;
}
template <typename T>
inline T vboth(T a, const int mod = 1000000007) {
while (a < 0) a += mod;
while (a >= mod) a -= mod;
return a;
}
template <typename T>
inline T max(const T& a, const T& b, const T& c) {
return std::max(std::max(a, b), c);
}
template <typename T>
inline T max(const T& a, const T& b, const T& c, const T& d) {
return std::max(max(a, b, c), d);
}
template <typename T>
inline T max(const T& a, const T& b, const T& c, const T& d, const T& e) {
return std::max(max(a, b, c, d), e);
}
template <typename T>
inline T max(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f) {
return std::max(max(a, b, c, d, e), f);
}
template <typename T>
inline T max(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g) {
return std::max(max(a, b, c, d, e, f), g);
}
template <typename T>
inline T max(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h) {
return std::max(max(a, b, c, d, e, f, g), h);
}
template <typename T>
inline T max(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i) {
return std::max(max(a, b, c, d, e, f, g, h), i);
}
template <typename T>
inline T max(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i, const T& j) {
return std::max(max(a, b, c, d, e, f, g, h, i), j);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c) {
return std::min(std::min(a, b), c);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c, const T& d) {
return std::min(min(a, b, c), d);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c, const T& d, const T& e) {
return std::min(min(a, b, c, d), e);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f) {
return std::min(min(a, b, c, d, e), f);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g) {
return std::min(min(a, b, c, d, e, f), g);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h) {
return std::min(min(a, b, c, d, e, f, g), h);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i) {
return std::min(min(a, b, c, d, e, f, g, h), i);
}
template <typename T>
inline T min(const T& a, const T& b, const T& c, const T& d, const T& e,
const T& f, const T& g, const T& h, const T& i, const T& j) {
return std::min(min(a, b, c, d, e, f, g, h, i), j);
}
inline int _1(int i) {
assert(i < 32);
return 1 << i;
}
inline int _7(int i) {
assert(i < 32);
return (1 << i) - 1;
}
inline long long _1L(int i) {
assert(i < 64);
return 1LL << i;
}
inline long long _7L(int i) {
assert(i < 64);
return (1LL << i) - 1;
}
template <typename T>
inline int i_1(T x) {
assert((x & x - 1) == 0);
int ret = 0;
for (int i = sizeof(T) << 2; i; i >>= 1)
if (x >> i) {
ret += i;
x >>= i;
}
return ret;
}
template <typename T>
inline T lowbit(T x) {
return x & -x;
}
template <typename T>
inline bool testbit(T x, int i) {
return (bool)(x & (T(1) << i));
}
template <typename T>
inline bool setbit(T& x, int i) {
return (x & (T(1) << i)) ? false : (x |= (T(1) << i), true);
}
template <typename T>
inline bool resetbit(T& x, int i) {
return (x & (T(1) << i)) ? (x &= ~(T(1) << i), true) : false;
}
template <typename T>
inline int cntbit(T x) {
int len = sizeof(T) << 3;
x = ((x & 0xaaaaaaaaaaaaaaaa) >> 1) + (x & 0x5555555555555555);
x = ((x & 0xcccccccccccccccc) >> 2) + (x & 0x3333333333333333);
x = ((x & 0xf0f0f0f0f0f0f0f0) >> 4) + (x & 0x0f0f0f0f0f0f0f0f);
if (len >= 16) x = ((x & 0xff00ff00ff00ff00) >> 8) + (x & 0x00ff00ff00ff00ff);
if (len >= 32)
x = ((x & 0xffff0000ffff0000) >> 16) + (x & 0x0000ffff0000ffff);
if (len >= 64)
x = ((x & 0xffffffff00000000) >> 32) + (x & 0x00000000ffffffff);
return x;
}
template <typename T>
inline T reversebit(T x) {
int len = sizeof(T) << 3;
x = ((x & 0xaaaaaaaaaaaaaaaa) >> 1) | ((x & 0x5555555555555555) << 1);
x = ((x & 0xcccccccccccccccc) >> 2) | ((x & 0x3333333333333333) << 2);
x = ((x & 0xf0f0f0f0f0f0f0f0) >> 4) | ((x & 0x0f0f0f0f0f0f0f0f) << 4);
if (len >= 16)
x = ((x & 0xff00ff00ff00ff00) >> 8) | ((x & 0x00ff00ff00ff00ff) << 8);
if (len >= 32)
x = ((x & 0xffff0000ffff0000) >> 16) | ((x & 0x0000ffff0000ffff) << 16);
if (len >= 64)
x = ((x & 0xffffffff00000000) >> 32) | ((x & 0x00000000ffffffff) << 32);
return x;
}
inline int cntbit(int x) {
x = ((x & 0xaaaaaaaa) >> 1) + (x & 0x55555555);
x = ((x & 0xcccccccc) >> 2) + (x & 0x33333333);
x = ((x & 0xf0f0f0f0) >> 4) + (x & 0x0f0f0f0f);
x = ((x & 0xff00ff00) >> 8) + (x & 0x00ff00ff);
x = ((x & 0xffff0000) >> 16) + (x & 0x0000ffff);
return x;
}
inline int cntbit(long long x) {
x = ((x & 0xaaaaaaaaaaaaaaaa) >> 1) + (x & 0x5555555555555555);
x = ((x & 0xcccccccccccccccc) >> 2) + (x & 0x3333333333333333);
x = ((x & 0xf0f0f0f0f0f0f0f0) >> 4) + (x & 0x0f0f0f0f0f0f0f0f);
x = ((x & 0xff00ff00ff00ff00) >> 8) + (x & 0x00ff00ff00ff00ff);
x = ((x & 0xffff0000ffff0000) >> 16) + (x & 0x0000ffff0000ffff);
x = ((x & 0xffffffff00000000) >> 32) + (x & 0x00000000ffffffff);
return int(x);
}
inline int reversebit(int x) {
x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);
x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);
return x;
}
inline long long reversebit(long long x) {
x = ((x & 0xaaaaaaaaaaaaaaaa) >> 1) | ((x & 0x5555555555555555) << 1);
x = ((x & 0xcccccccccccccccc) >> 2) | ((x & 0x3333333333333333) << 2);
x = ((x & 0xf0f0f0f0f0f0f0f0) >> 4) | ((x & 0x0f0f0f0f0f0f0f0f) << 4);
x = ((x & 0xff00ff00ff00ff00) >> 8) | ((x & 0x00ff00ff00ff00ff) << 8);
x = ((x & 0xffff0000ffff0000) >> 16) | ((x & 0x0000ffff0000ffff) << 16);
x = ((x & 0xffffffff00000000) >> 32) | ((x & 0x00000000ffffffff) << 32);
return x;
}
template <typename T>
inline T gcd(T a, T b) {
while (b != 0) {
T tmp = b;
b = a % b;
a = tmp;
}
return a;
}
template <typename T>
inline T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
inline long long minv(long long x, const long long mod = 1000000007) {
return mpow(x, mod - 2, mod);
}
const int _ = (int)(1e3 + 10);
const int __ = (int)(1e6 + 10);
const int LOG = 18;
int n, k;
struct Point {
int x, y;
void read() { RDB(x, y); }
};
Point stones[7];
Point monsters[_];
bool vis[_];
vector<pair<long long, long long> >* shoot[7][_];
int shoot_order[7][_];
map<pair<int, int>, vector<pair<long long, long long> > > lines[7];
bool dfs2(vector<int> mids, int used, int rest) {
if (mids.empty()) return true;
if (mids.size() > rest) return false;
for (int mid : mids) {
vis[mid] = true;
for (long long i = 0; i < (long long)(k); ++i)
if (!testbit(used, i)) {
int order = shoot_order[i][mid];
set<int> s((mids).begin(), (mids).end());
s.erase(mid);
for (long long j = 0; j < (long long)(order); ++j) {
int t = (*shoot[i][mid])[j].second;
if (vis[t]) continue;
s.insert(t);
}
if (dfs2(vector<int>((s).begin(), (s).end()), used | _1(i), rest - 1))
return true;
end:;
}
vis[mid] = false;
}
return false;
}
bool dfs(int mid, int used = 0, int rest = k) {
for (long long i = 0; i < (long long)(k); ++i)
if (!testbit(used, i)) {
int order = shoot_order[i][mid];
if (order == 0) return true;
if (order >= rest) continue;
vis[mid] = true;
vector<int> t;
for (long long j = 0; j < (long long)(order); ++j)
t.push_back((*shoot[i][mid])[j].second);
if (dfs2(t, used | _1(i), rest - 1)) return true;
vis[mid] = false;
}
return false;
}
inline void _main() {
RDB(k, n);
for (long long i = 0; i < (long long)(k); ++i) stones[i].read();
for (long long i = 0; i < (long long)(n); ++i) monsters[i].read();
for (long long i = 0; i < (long long)(k); ++i) {
for (long long j = 0; j < (long long)(n); ++j) {
int dx = monsters[j].x - stones[i].x;
int dy = monsters[j].y - stones[i].y;
long long d = 1LL * dx * dx + 1LL * dy * dy;
int g = gcd(abs(dx), abs(dy));
dx /= g, dy /= g;
lines[i][make_pair(dx, dy)].push_back(make_pair(d, j));
}
for (auto& x : lines[i]) {
vector<pair<long long, long long> >& shoot_sequence = x.second;
sort((shoot_sequence).begin(), (shoot_sequence).end());
for (long long j = (long long)(0);
j < (long long)(((int)(shoot_sequence).size())); ++j) {
int m = shoot_sequence[j].second;
shoot[i][m] = &shoot_sequence;
shoot_order[i][m] = j;
}
}
}
int ret = 0;
for (long long i = 0; i < (long long)(n); ++i) {
memset((vis), (0), sizeof(vis));
if (dfs(i, 0, k)) ++ret;
}
WR(ret);
}
void generate_test_case() { exit(0); }
int main() {
_main();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int str[2010][2010];
int main() {
int i, j, k;
int n, m;
int op, tmp;
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &str[i][j]);
}
}
int ans = 0;
for (i = 0; i < n; i++) {
ans ^= str[i][i];
}
cin >> m;
while (m--) {
scanf("%d", &op);
if (op == 1 || op == 2) {
scanf("%d", &tmp);
ans ^= 1;
} else {
printf("%d", ans);
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 120000 + 10;
const double PI = acos(-1);
pair<double, double> operator+(pair<double, double> a, pair<double, double> b) {
return make_pair(a.first + b.first, a.second + b.second);
}
pair<double, double> operator-(pair<double, double> a, pair<double, double> b) {
return make_pair(a.first - b.first, a.second - b.second);
}
pair<double, double> operator*(pair<double, double> a, pair<double, double> b) {
return make_pair(a.first * b.first - a.second * b.second,
a.first * b.second + a.second * b.first);
}
pair<double, double> operator/(pair<double, double> a, double t) {
return make_pair(a.first / t, a.second / t);
}
int rev[N], lim;
pair<double, double> w[N];
void initrever(int n, int &lim) {
lim = 1;
for (; lim < n; lim <<= 1)
;
for (int i = 1; i < lim; i++) {
rev[i] = (rev[i >> 1] >> 1);
if (i & 1) rev[i] |= (lim >> 1);
}
for (int i = 0; i < lim; i++) {
double t = 2 * PI * i / lim;
w[i] = make_pair(cos(t), sin(t));
}
return;
}
void FFT(vector<pair<double, double> > &a, int t) {
int lim = a.size();
if (t < 0) {
for (int i = 1; i < lim; i++) {
if (i < (lim - i)) swap(a[i], a[lim - i]);
}
}
for (int i = 0; i < lim; i++)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int i = 2; i <= lim; i <<= 1) {
for (int j = 0; j < lim; j += i) {
for (int k = 0; k < (i >> 1); k++) {
pair<double, double> first = a[j + k],
second = a[j + k + (i >> 1)] * w[lim / i * k];
a[j + k] = (first + second);
a[j + k + (i >> 1)] = (first - second);
}
}
}
if (t < 0) {
for (int i = 0; i < lim; i++) a[i] = a[i] / lim;
}
return;
}
vector<pair<double, double> > f[2];
int n, a, p;
double reduce(double first) {
long long tmp = llround(first);
tmp %= p;
return (double)tmp;
}
void add1() {
swap(f[0], f[1]);
for (int i = 0; i <= 1; i++) {
for (int j = n; j >= 1; j--) {
f[i][j].first = reduce(f[i][j].first + f[i][j - 1].first);
}
}
f[1][1].first = reduce(f[1][1].first + 1);
return;
}
void put(vector<pair<double, double> > v) {
for (int i = 0; i < (int)v.size(); i++) printf("%lf ", v[i].first);
printf("\n");
return;
}
void pro2(int v) {
FFT(f[0], 1), FFT(f[1], 1);
for (int i = 0; i < lim; i++) {
pair<double, double> first =
f[v & 1][i] * (f[0][i] + f[1][i]) + f[0][i] + f[v & 1][i];
pair<double, double> second =
f[(v & 1) ^ 1][i] * (f[0][i] + f[1][i]) + f[1][i] + f[(v & 1) ^ 1][i];
f[0][i] = first, f[1][i] = second;
}
FFT(f[0], -1), FFT(f[1], -1);
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= n; j++) {
f[i][j].first = reduce(f[i][j].first);
f[i][j].second = 0;
}
for (int j = n + 1; j < lim; j++) f[i][j] = make_pair(0, 0);
}
return;
}
vector<int> num;
int main() {
scanf("%d%d%d", &n, &a, &p);
initrever(n * 2 + 1, lim);
f[0].resize(lim), f[1].resize(lim);
for (int no = a; no > 1; no /= 2) {
num.push_back(no);
}
num.push_back(1);
reverse(num.begin(), num.end());
f[1][1].first = 1;
int ans = 0;
for (int i = 0; i < (int)num.size() - 1; i++) {
for (int j = 1; j <= n; j += 2) {
ans = (ans + llround(f[1][j].first)) % p;
}
pro2(num[i]);
if (num[i] * 2 < num[i + 1]) add1();
}
printf("%d\n", ans);
return 0;
}
| 13 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.