solution
stringlengths 53
181k
| difficulty
int64 0
27
|
|---|---|
#include <bits/stdc++.h>
using namespace std;
char s[5555];
int q;
int is[5555][5555] = {};
int main() {
scanf("%s", s);
int n = strlen(s);
for (int len = 1; len < n + 1; len++) {
for (int i = n - len; i >= 0; i--) {
int j = i + len - 1;
if (s[i] == s[j]) {
if (len <= 3)
is[i][j] = 1;
else {
is[i][j] = is[i + 1][j - 1];
}
}
}
}
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) is[i][j] += is[i][j - 1];
for (int i = 1; i < n; i++)
for (int j = i - 1; j < n; j++) is[i][j] += is[i - 1][j];
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int l, r;
scanf("%d%d", &l, &r);
l--;
r--;
printf("%d\n", is[r][r] - (l == 0 ? 0 : is[l - 1][r]));
}
return 0;
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
int **e, *used;
vector<int> number;
bool dfs(int v) {
used[v] = 1;
for (int i = 1; i <= e[v][0]; i++) {
if (used[e[v][i]] == 0 && dfs(e[v][i]) == false) {
return false;
} else if (used[e[v][i]] == 1) {
return false;
}
}
used[v] = 2;
number.push_back(v);
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, k, s;
cin >> n >> k;
int *main = new int[k];
for (int i = 0; i < k; i++) cin >> main[i];
e = new int *[n + 1];
used = new int[n + 1];
for (int i = 1; i <= n; i++) {
used[i] = 0;
cin >> s;
e[i] = new int[s + 1];
e[i][0] = s;
for (int j = 1; j <= s; j++) cin >> e[i][j];
}
for (int i = 0; i < k; i++)
if (used[main[i]] == 0 && dfs(main[i]) == false) {
cout << -1 << endl;
return 0;
}
cout << number.size() << endl;
for (int i = 0; i < number.size(); i++) cout << number[i] << " ";
cout << endl;
return 0;
}
| 7
|
#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n;
int num;
long long ans = 0;
priority_queue<int> q;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &num);
q.push(num);
if (num < q.top()) {
ans += (long long)q.top() - (long long)num;
q.pop();
q.push(num);
}
}
printf("%lld\n", ans);
return 0;
}
| 14
|
#include <bits/stdc++.h>
using namespace std;
long long pol[200][200];
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
pol[0][0] = 1;
pol[1][1] = 1;
for (int i = 2; i <= n + 1; i++) {
int ok = 1;
for (int j = 0; j <= i - 1; j++) pol[i][j + 1] += pol[i - 1][j];
for (int j = 0; j <= i - 2; j++) {
if (pol[i][j] + pol[i - 2][j] >= 2 || pol[i][j] + pol[i - 2][j] <= -2)
ok = 0;
}
if (ok)
for (int j = 0; j <= i - 2; j++) pol[i][j] += pol[i - 2][j];
else
for (int j = 0; j <= i - 2; j++) pol[i][j] -= pol[i - 2][j];
}
cout << n << endl;
for (int i = 0; i <= n; i++) cout << pol[n][i] << " \n"[i == n];
cout << n - 1 << endl;
for (int i = 0; i <= n - 1; i++) cout << pol[n - 1][i] << " \n"[i == n - 1];
}
| 14
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
int d[8][2] = {{1, 0}, {1, 1}, {1, -1}, {0, 1},
{0, -1}, {-1, -1}, {-1, 1}, {-1, 0}};
int answ = 0, n, sx, sy, p = 0;
cin >> n >> sx >> sy;
int a[n][2];
for (int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1];
for (int i = 0; i < 8; i++) {
int x = sx + d[i][0], y = sy + d[i][1];
if (x >= 0 && y >= 0 && x <= 1000000000 && y <= 1000000000) {
int cur = 0;
for (int j = 0; j < n; j++) {
int left = min(sx, a[j][0]), right = max(sx, a[j][0]),
top = max(sy, a[j][1]), bottom = min(sy, a[j][1]);
if (x >= left && x <= right && y >= bottom && y <= top) cur++;
}
if (cur > answ) answ = cur, p = i;
}
}
cout << answ << '\n';
cout << sx + d[p][0] << ' ' << sy + d[p][1];
return 0;
}
| 5
|
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::map<std::pair<std::string, int>,
std::vector<std::pair<std::string, int> > >
g;
std::pair<std::string, int> start;
for (int i = 0; i < n; ++i) {
std::pair<std::string, int> cur;
std::cin >> cur.first >> cur.second;
if (i == 0) {
start = cur;
}
int k;
std::cin >> k;
for (int j = 0; j < k; ++j) {
std::pair<std::string, int> to;
std::cin >> to.first >> to.second;
g[cur].push_back(to);
}
}
std::queue<std::pair<std::string, int> > q;
std::map<std::string, std::pair<int, int> > dist;
q.push(start);
dist[start.first] = std::make_pair(0, start.second);
while (!q.empty()) {
std::pair<std::string, int> v = q.front();
q.pop();
if (dist[v.first].second != v.second) {
continue;
}
for (auto to : g[v]) {
if (!dist.count(to.first) ||
(dist[to.first].first == dist[v.first].first + 1 &&
dist[to.first].second < to.second)) {
q.push(to);
dist[to.first] = std::make_pair(dist[v.first].first + 1, to.second);
}
}
}
std::cout << dist.size() - 1 << std::endl;
for (auto it : dist) {
if (it.first != start.first) {
std::cout << it.first << " " << it.second.second << std::endl;
}
}
return 0;
}
| 11
|
#include <bits/stdc++.h>
using namespace std;
const long long int maxn = (3e5) + 10, mod = 1e9 + 7;
long long int n, a[maxn], dp_D[maxn], dp_U[maxn], pd_D[maxn], pd_U[maxn];
vector<long long int> v[maxn];
void pre_DFS1(long long int x, long long int p) {
pd_D[x] = a[x];
for (auto u : v[x]) {
if (u == p) continue;
pre_DFS1(u, x);
pd_D[x] = max(pd_D[x], pd_D[u] + a[x]);
}
}
void pre_DFS2(long long int x, long long int p) {
long long int aa = -1, bb = -1;
for (auto u : v[x]) {
if (u == p) continue;
if (pd_D[u] >= aa) {
swap(aa, bb);
aa = pd_D[u];
} else if (pd_D[u] >= bb)
bb = pd_D[u];
}
if (bb == -1) bb++;
if (aa == -1) aa++;
for (auto u : v[x]) {
if (u == p) continue;
if (aa == pd_D[u])
pd_U[u] = max(pd_U[x] + a[u], bb + a[u] + a[x]);
else
pd_U[u] = max(pd_U[x] + a[u], aa + a[u] + a[x]);
pre_DFS2(u, x);
}
}
void DFS1(long long int x, long long int p) {
dp_D[x] = pd_D[x];
long long int aa = -1, bb = -1;
for (auto u : v[x]) {
if (u == p) continue;
DFS1(u, x);
dp_D[x] = max(dp_D[x], dp_D[u]);
if (pd_D[u] >= aa) {
swap(aa, bb);
aa = pd_D[u];
} else if (pd_D[u] > bb)
bb = pd_D[u];
}
if (bb == -1) bb++;
if (aa == -1) aa++;
dp_D[x] = max(dp_D[x], aa + bb + a[x]);
}
void DFS2(long long int x, long long int p) {
long long int aa = -1, bb = -1, cc = -1, dd = -1;
for (auto u : v[x]) {
if (u == p) continue;
if (pd_D[u] >= aa) {
swap(bb, cc);
swap(aa, bb);
aa = pd_D[u];
} else if (pd_D[u] >= bb) {
swap(bb, cc);
bb = pd_D[u];
} else if (pd_D[u] >= cc)
cc = pd_D[u];
}
if (aa == -1) aa++;
if (bb == -1) bb++;
if (cc == -1) cc++;
long long int qq = -1, ww = -1;
for (auto u : v[x]) {
if (u == p) continue;
if (pd_D[u] == aa)
dp_U[u] = max(bb + pd_U[x], bb + cc + a[x]);
else if (pd_D[u] == bb)
dp_U[u] = max(aa + pd_U[x], aa + cc + a[x]);
else
dp_U[u] = max(aa + pd_U[x], aa + bb + a[x]);
dp_U[u] = max(dp_U[u], dp_U[x]);
if (dp_D[u] >= qq) {
swap(qq, ww);
qq = dp_D[u];
} else if (dp_D[u] >= ww)
ww = dp_D[u];
}
for (auto u : v[x]) {
if (u == p) continue;
if (dp_D[u] == qq)
dp_U[u] = max(dp_U[u], ww);
else
dp_U[u] = max(dp_U[u], qq);
}
for (auto u : v[x]) {
if (u == p) continue;
DFS2(u, x);
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (long long int i = 1; i <= n; i++) cin >> a[i];
for (long long int i = 0; i < n - 1; i++) {
long long int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
pre_DFS1(1, -1);
pre_DFS2(1, -1);
DFS1(1, -1);
DFS2(1, -1);
long long int ans = 0;
for (long long int i = 1; i <= n; i++) {
ans = max(ans, dp_D[i] + dp_U[i]);
}
cout << ans << endl;
return 0;
}
| 18
|
#include <bits/stdc++.h>
using namespace std;
int main() {
char a;
int n, cnt[26] = {0};
bool OK = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
if (a >= 'A' && a <= 'Z') a += 32;
if (a >= 'a' && a <= 'z') cnt[a - 97]++;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] == 0) {
OK = false;
break;
}
}
if (!OK)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
if (m > n) swap(n, m);
if (n == 7 && m == 4)
cout << 21 << endl;
else if (n == 10 && m == 4)
cout << 30 << endl;
else if (m == 1)
cout << (n / 3 * 2 + (n % 3 > 1)) << endl;
else if (n % 4 == 0 && m % 4 == 0)
cout << (n * m - (n * m / 4)) << endl;
else
cout << (n * m - (n * m / 4) - 1) << endl;
return 0;
}
| 13
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 5;
const long long oo = 1LL << 60;
inline long long read() {
char ch = getchar();
long long x = 0, f = 1;
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long N, M, LINK[MAXN], len = 0, T, Q, head, tail, q[MAXN << 1], pos, ned,
mon, tim[MAXN], TIM, leftt, rightt, mid;
struct edge {
long long y, next, v;
} e[MAXN];
struct SHOP {
long long val, rem, id;
} node[MAXN];
namespace solution {
inline void insert(int x, int y, int v) {
e[++len].next = LINK[x];
LINK[x] = len;
e[len].y = y;
e[len].v = v;
}
inline void Insert(int x, int y, int v) {
insert(x, y, v);
insert(y, x, v);
}
inline bool cmp(SHOP x, SHOP y) { return x.val < y.val; }
void init() {
N = read();
M = read();
for (int i = 1; i <= M; i++) {
int x = read(), y = read();
Insert(x, y, 1);
}
T = read();
for (int i = 1; i <= T; i++) {
long long pos = read(), rem = read(), val = read();
node[i].val = val;
node[i].rem = rem;
node[i].id = pos;
}
Q = read();
sort(node + 1, node + T + 1, cmp);
}
void BFS() {
head = 1;
tail = 0;
q[++tail] = pos;
for (int i = 1; i <= N; i++) tim[i] = oo;
tim[pos] = 0;
for (; head <= tail; head++) {
int node = q[head];
TIM = max(TIM, tim[node]);
for (int i = LINK[node]; i; i = e[i].next)
if (tim[e[i].y] == oo) {
tim[e[i].y] = tim[node] + 1;
q[++tail] = e[i].y;
}
}
}
bool get(long long lim, long long need, long long mone) {
for (int i = 1; i <= T; i++)
if (tim[node[i].id] <= lim) {
long long t = min(node[i].rem, need);
need -= t;
mone -= t * node[i].val;
if (mone < 0) return 0;
}
return !need;
}
void slove() {
while (Q--) {
pos = read();
ned = read();
mon = read();
TIM = 0;
BFS();
leftt = 0;
rightt = TIM;
while (leftt + 1 < rightt) {
mid = (leftt + rightt) >> 1;
if (get(mid, ned, mon))
rightt = mid;
else
leftt = mid;
}
if (get(leftt, ned, mon))
printf("%d\n", leftt);
else if (get(rightt, ned, mon))
printf("%d\n", rightt);
else
puts("-1");
}
}
} // namespace solution
int main() {
using namespace solution;
init();
slove();
return 0;
}
| 13
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, x;
cin >> a >> b >> x;
if (x == 1) {
for (int i = 1; i <= a; i++) cout << "0";
for (int i = 1; i <= b; i++) cout << "1";
return 0;
}
int toggle = 0;
if (a >= b)
cout << "01";
else {
cout << "10";
toggle = 1;
}
a--;
b--;
x--;
x--;
while (x-- > 0 && a && b) {
cout << toggle;
if (toggle)
b--;
else
a--;
toggle = toggle ^ 1;
}
if (toggle) {
for (int i = 1; i <= a; i++) cout << "0";
for (int i = 1; i <= b; i++) cout << "1";
} else {
for (int i = 1; i <= b; i++) cout << "1";
for (int i = 1; i <= a; i++) cout << "0";
}
return 0;
}
| 5
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, x, y, n;
cin >> n;
while (n--) {
cin >> a >> b >> x >> y;
cout << max(max(max(b * x, b * (a - x - 1)), a * y), a * (b - y - 1))
<< endl;
}
return 0;
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
struct PR {
long long int vl, idx;
PR() {}
PR(long long int _vl, long long int _idx) { vl = _vl, idx = _idx; }
bool operator<(const PR &o) const {
if (vl == o.vl) {
return idx < o.idx;
}
return vl < o.vl;
}
};
long long int n, ara[100010];
multiset<PR> mul;
vector<PR> vec;
queue<PR> q;
long long int k;
vector<long long int> res;
bool cmp(PR lhs, PR rhs) { return lhs.idx < rhs.idx; }
void input() {
long long int i, j;
scanf("%I64d %I64d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%I64d", &ara[i]);
mul.insert(PR(ara[i], i));
}
}
void solve() {
long long int i, j, offst = 0, mn, sz;
std::multiset<PR>::iterator it;
while (!mul.empty() && k > 0) {
sz = mul.size();
mn = mul.begin()->vl;
mn = mn - offst;
if (mn * sz <= k) {
k -= mn * sz;
} else {
offst += (k / sz);
k = k % sz;
break;
}
while (!mul.empty() && mul.begin()->vl == mn + offst) {
it = mul.begin();
mul.erase(it);
}
offst += mn;
}
vec.assign(mul.begin(), mul.end());
sort(vec.begin(), vec.end(), cmp);
for (i = 0; i < vec.size(); i++) {
q.push(PR(vec[i].vl - offst, vec[i].idx));
}
while (!q.empty() && k--) {
PR tp = q.front();
q.pop();
tp.vl--;
if (tp.vl == 0) {
continue;
} else {
q.push(tp);
}
}
if (k > 0) {
printf("%lld", -1ll);
puts("");
return;
}
while (!q.empty()) {
PR tp = q.front();
q.pop();
res.push_back(tp.idx);
}
for (i = 0; i < res.size(); i++) {
if (i != 0) {
printf(" ");
}
printf("%I64d", res[i] + 1);
}
puts("");
}
int main() {
input();
solve();
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
int ar[2000000], a[200000];
void up(int r, int l1, int l2, int p) {
if (p > l2 || p < l1) return;
ar[r]++;
if (l1 == l2) return;
int m = (l1 + l2) / 2;
up(r << 1, l1, m, p);
up((r << 1) + 1, m + 1, l2, p);
}
int qu(int r, int l1, int l2, int q) {
if (l1 == l2) return a[l1];
int m = (l1 + l2) / 2;
if (ar[r << 1] >= q) return qu(r << 1, l1, m, q);
q -= ar[r << 1];
return qu((r << 1) + 1, m + 1, l2, q);
}
map<int, set<int> > mm;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, m;
cin >> n;
priority_queue<int> mq;
for (int i = 0; i < n; i++) {
cin >> a[i];
mm[a[i]].insert(i);
mq.push(a[i]);
}
cin >> m;
priority_queue<pair<pair<int, int>, int> > qs;
int res[m], k, p;
for (int i = 0; i < m; i++) {
cin >> k >> p;
qs.push(pair<pair<int, int>, int>(pair<int, int>(-k, p), i));
}
int lk = 0;
for (int i = 0; i < m; i++) {
pair<pair<int, int>, int> t = qs.top();
qs.pop();
for (; lk < -t.first.first; lk++) {
int t2 = mq.top();
mq.pop();
up(1, 0, n - 1, *mm[t2].begin());
mm[t2].erase(mm[t2].begin());
}
res[t.second] = qu(1, 0, n - 1, t.first.second);
}
for (int i = 0; i < m; i++) cout << res[i] << endl;
return 0;
}
| 8
|
#include <bits/stdc++.h>
using namespace std;
vector<int> tree[10000];
int main() {
int n;
cin >> n;
int a, b;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
--a;
--b;
tree[a].push_back(b);
tree[b].push_back(a);
}
uint64_t totalSum = 0;
uint64_t compensation = 0;
uint64_t localSum = 0;
for (int i = 0; i < n; i++) {
vector<int>& current = tree[i];
localSum = 0;
for (int j = 0; j < current.size(); j++) {
int other = current[j];
if (other != i) {
localSum += tree[other].size() - 1;
}
}
if (localSum & 1 == 1) {
compensation += 1;
}
totalSum += localSum / 2;
}
totalSum += compensation / 2;
cout << totalSum << endl;
}
| 5
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char ch = getchar();
if (!(~ch)) return 0;
int f = 1;
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
long long x = 0;
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
long long ans, f[21], cnt[10], tp[10];
inline void dfs(long long x, long long y) {
if (x == 10) {
long long s = 1, tmp = y;
for (int i = 0; i < 10; i++)
if (tp[i]) {
s = s * f[tmp] / f[tp[i]] / f[tmp - tp[i]];
tmp -= tp[i];
}
if (tp[0]) s -= s * tp[0] / y;
ans += s;
return;
}
int step = x + 1;
for (register int i = 1; i <= cnt[x]; ++i) {
tp[x] = i;
dfs(step, y + i);
}
if (!cnt[x]) dfs(step, y);
}
int main() {
f[0] = f[1] = 1;
for (long long i = 2; i < 21; ++i) f[i] = i * f[i - 1];
string s;
cin >> s;
int n = s.length();
for (register int i = 0; i < n; ++i) cnt[s[i] ^ 48]++;
dfs(0, 0);
printf("%lld\n", ans);
return 0;
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m, X, q[N], l[N], r[N];
int a[N], na[N], b[N], nb[N];
struct ZKW {
int nn, t[N * 4];
void init(int *a, int n) {
for (nn = 1; nn <= n; nn <<= 1)
;
for (int i = 1; i <= n; i++) t[i + nn] = a[i];
for (int i = nn - 1; i >= 1; i--) t[i] = max(t[i * 2], t[i * 2 + 1]);
}
int ask(int l, int r) {
int ans = 0;
l += nn - 1;
r += nn + 1;
for (; l ^ r ^ 1; l >>= 1, r >>= 1) {
if (!(l & 1)) ans = max(ans, t[l ^ 1]);
if (r & 1) ans = max(ans, t[r ^ 1]);
}
return ans;
}
} T1;
void init(int *a, int *na, int n) {
int t = q[1] = 1;
for (int i = 2; i <= n; i++) {
for (; t && a[i] <= a[q[t]]; --t) r[q[t]] = i;
l[i] = q[t];
q[++t] = i;
}
for (; t; --t) r[q[t]] = n + 1;
T1.init(a, n);
for (int i = 1; i <= n; i++) {
int v1, v2;
if (l[i] == 1 - 1)
v1 = N;
else
v1 = (l[i] == i - 1 ? a[i] : T1.ask(l[i] + 1, i - 1));
if (r[i] == n + 1)
v2 = N;
else
v2 = (r[i] == i + 1 ? a[i] : T1.ask(i + 1, r[i] - 1));
na[i] = min(v1, v2);
}
}
pair<int, int> qa[N], qb[N];
int t[N * 2];
void change(int x) {
for (; x < N * 2; x += x & (-x)) t[x]++;
}
int ask(int x) {
int s = 0;
for (; x; x -= x & (-x)) s += t[x];
return s;
}
int main() {
scanf("%d%d%d", &n, &m, &X);
++X;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
init(a, na, n);
init(b, nb, m);
for (int i = 1; i <= n; i++) qa[i] = pair<int, int>(na[i] - a[i], a[i]);
for (int i = 1; i <= m; i++) qb[i] = pair<int, int>(nb[i] - b[i], b[i]);
sort(qa + 1, qa + n + 1);
sort(qb + 1, qb + m + 1);
long long ans = 0;
int pa = n, pb = m;
for (; pa >= 1 || pb >= 1;)
if (pb < 1 || (pa >= 1 && qa[pa].first > qb[pb].first)) {
int r = X - qa[pa].second - 1;
int l = r + 1 - qa[pa].first;
if (r >= 1) ans += ask(r);
if (l >= 1) ans -= ask(l - 1);
pa--;
} else {
change(qb[pb].second);
pb--;
}
memset(t, 0, sizeof(t));
pa = n;
pb = m;
for (; pa >= 1 || pb >= 1;)
if (pa < 1 || (pb >= 1 && qb[pb].first >= qa[pa].first)) {
int r = X - qb[pb].second - 1;
int l = r + 1 - qb[pb].first;
if (r >= 1) ans += ask(r);
if (l >= 1) ans -= ask(l - 1);
pb--;
} else {
change(qa[pa].second);
pa--;
}
printf("%lld\n", ans);
}
| 26
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int N = 4004;
vector<int> g[N];
int a[N];
int beg[N], len[N], dp[N][N];
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, n, k, j;
cin >> n >> k;
for (i = 1; i < 1 + n; i++) cin >> beg[i] >> len[i];
memset(dp, 0, sizeof(dp));
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) dp[i][j] = 1;
dp[1][0] = beg[1] + len[1];
for (i = 2; i < 1 + n; i++) dp[i][0] = max(dp[i - 1][0], beg[i]) + len[i];
for (i = 1; i < 1 + n; i++)
for (j = 1; j < i; j++)
dp[i][j] = min(dp[i - 1][j - 1], len[i] + max(dp[i - 1][j], beg[i]));
beg[n + 1] = 86401;
int ans = 0;
for (i = 0; i < n + 1; i++) ans = max(ans, -dp[i][k] + beg[i + 1]);
cout << min(86400, ans) << endl;
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % mod;
base = ((long long)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m) {
int i, u, v;
while (m--) {
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par) continue;
dfs(v, u);
}
}
| 11
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
vector<vector<char>> shape(n, vector<char>(n));
for (int i = (0); i < (n); ++i) {
for (int j = (0); j < (n); ++j) {
cin >> shape[i][j];
}
}
vector<long long> rows(n, 0);
long long clearRows = 0;
vector<vector<long long>> hor(n, vector<long long>(n, 0));
for (int i = (0); i < (n); ++i) {
for (int j = n - 1; j >= 0; j--) {
if (shape[i][j] == 'W') {
if (j + 1 < n)
hor[i][j] = hor[i][j + 1] + 1;
else
hor[i][j] = 1;
} else {
hor[i][j] = 0;
}
}
if (hor[i][0] == n) {
clearRows++;
rows[i] = 1;
}
}
long long clearCols = 0;
vector<long long> cols(n, 0);
vector<vector<long long>> vert(n, vector<long long>(n, 0));
for (int j = 0; j < n; j++) {
for (int i = n - 1; i >= 0; i--) {
if (shape[i][j] == 'W') {
if (i + 1 < n)
vert[i][j] = vert[i + 1][j] + 1;
else
vert[i][j] = 1;
} else {
vert[i][j] = 0;
}
}
if (vert[0][j] == n) {
cols[j] = 1;
clearCols++;
}
}
vector<vector<long long>> clearsRow(n, vector<long long>(n, 0));
for (int i = (0); i < (n); ++i) {
for (int j = (0); j < (n); ++j) {
if (j > 0 && shape[i][j - 1] == 'B') break;
long long c = 0;
if (j + k < n) {
c = k + hor[i][j + k];
} else {
c = k;
}
if (c == n - j) {
clearsRow[i][j] = 1;
}
}
}
vector<vector<long long>> clearsCol(n, vector<long long>(n, 0));
for (int j = (0); j < (n); ++j) {
for (int i = (0); i < (n); ++i) {
if (i > 0 && shape[i - 1][j] == 'B') break;
long long c = 0;
if (i + k < n) {
c = k + vert[i + k][j];
} else {
c = k;
}
if (c == n - i) {
clearsCol[i][j] = 1;
}
}
}
vector<vector<long long>> ans(n, vector<long long>(n, 0));
for (int j = (0); j < (n); ++j) {
long long clear = clearRows;
for (int i = (0); i < (k); ++i) {
if (rows[i]) --clear;
if (clearsRow[i][j]) ++clear;
}
ans[0][j] = clear;
for (int i = (1); i < (n); ++i) {
if (rows[i - 1]) ++clear;
if (i + k - 1 < n && rows[i + k - 1]) --clear;
if (i + k - 1 < n && clearsRow[i + k - 1][j]) ++clear;
if (clearsRow[i - 1][j]) --clear;
ans[i][j] = clear;
}
}
for (int i = (0); i < (n); ++i) {
long long clear = clearCols;
for (int j = (0); j < (k); ++j) {
if (cols[j]) --clear;
if (clearsCol[i][j]) ++clear;
}
ans[i][0] += clear;
for (int j = (1); j < (n); ++j) {
if (cols[j - 1]) ++clear;
if (j + k - 1 < n && cols[j + k - 1]) --clear;
if (j + k - 1 < n && clearsCol[i][j + k - 1]) ++clear;
if (clearsCol[i][j - 1]) --clear;
ans[i][j] += clear;
}
}
long long res = 0;
for (int i = (0); i < (n); ++i) {
for (int j = (0); j < (n); ++j) {
res = ((res) > (ans[i][j]) ? (res) : (ans[i][j]));
}
}
cout << res << endl;
}
| 11
|
#include <bits/stdc++.h>
using namespace std;
struct p {
int a, b;
};
p point[1005];
int main() {
int t;
while ((scanf("%d", &t) != -1)) {
int flag = 0;
int ta, tb;
for (int i = 0; i < t; i++) {
scanf("%d %d", &point[i].a, &point[i].b);
if (point[i].a != point[i].b)
flag = 1;
else if (i != 0 && flag == 0 && point[i].a > ta)
flag = 2;
ta = point[i].a;
tb = point[i].b;
}
if (flag == 2)
cout << "unrated" << endl;
else if (flag == 1)
cout << "rated" << endl;
else
cout << "maybe" << endl;
}
}
| 1
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, *a, s = 0;
cin >> n >> m;
a = new int[n];
for (int i = 0; i < n; i++) a[i] = 0;
for (int i = 0; i < m; i++) {
int q, w, e;
cin >> q >> w >> e;
a[q - 1] -= e;
a[w - 1] += e;
}
for (int i = 0; i < n; i++)
if (a[i] < 0) s += a[i];
cout << -s;
return 0;
}
| 5
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int sum = 0, w[N], f[N * 2][N];
string s[N];
bool compare(string a, string b) { return (a < b); }
int min(int a, int b) { return (a < b ? a : b); }
int work(int l, int r) {
int d = ++sum;
if (l < r) {
int i, j, left, right, b = l;
for (i = l + 1; i < r; i++)
if (w[i] < w[b]) b = i;
left = work(l, b);
right = work(b + 1, r);
for (i = 0; i <= b + 1 - l; i++)
for (j = 0; j <= r - b; j++)
if (f[d][i + j] < f[left][i] + f[right][j] + w[b] * i * j)
f[d][i + j] = f[left][i] + f[right][j] + w[b] * i * j;
}
return (d);
}
int main() {
memset(f, 0, sizeof(f));
int n, k, i, j, temp;
cin >> n >> k;
for (i = 1; i <= n; i++) cin >> s[i];
sort(s + 1, s + n + 1, compare);
for (i = 1; i < n; i++) {
temp = min(s[i].size(), s[i + 1].size());
w[i] = temp;
for (j = 0; j < temp; j++)
if (s[i][j] != s[i + 1][j]) {
w[i] = j;
break;
}
}
work(1, n);
cout << f[1][k];
return (0);
}
| 17
|
#include <bits/stdc++.h>
int n;
std::vector<int> a;
std::vector<long long> ans;
std::map<std::vector<int>, long long> res;
std::vector<long long> chain;
std::vector<std::vector<long long>> chaint;
void dfs(int r, int x, std::vector<long long> g) {
if (r == 0) {
long long v = 0;
for (int i = 0; i < (1 << n); ++i)
v += __builtin_parity(((1 << n) - 1) ^ i) ? -g[i] : g[i];
res[a] = v;
return;
}
for (int i = x; i <= r; ++i) {
if (i != r && 2 * i > r) continue;
a.push_back(i);
auto g0 = g;
for (int j = 0; j < (1 << n); ++j) g0[j] *= chaint[i][j];
dfs(r - i, i, g0);
a.pop_back();
}
}
void f(std::vector<long long> &v) {
for (int i = 1; i < (1 << n); i *= 2)
for (int j = 0; j < (1 << n); j += 2 * i)
for (int k = 0; k < i; ++k) v[i + j + k] += v[j + k];
}
void h(std::vector<long long> &v) {
for (int i = 1; i < (1 << n); i *= 2)
for (int j = 0; j < (1 << n); j += 2 * i)
for (int k = 0; k < i; ++k) v[j + k] -= v[i + j + k];
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
std::vector<std::vector<bool>> g(n, std::vector<bool>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
char c;
std::cin >> c;
g[i][j] = c - '0';
}
}
chaint.assign(1 << n, std::vector<long long>(n));
chain.resize(1 << n);
for (int s = 1; s < (1 << n); ++s) {
if ((s & -s) == s) {
chaint[s][__builtin_ctz(s)] = 1;
} else {
for (int v = 0; v < n; ++v)
if (s >> v & 1)
for (int u = 0; u < n; ++u)
if (g[u][v] && (s >> u & 1)) chaint[s][v] += chaint[s ^ 1 << v][u];
}
for (int i = 0; i < n; ++i) chain[s] += chaint[s][i];
}
chaint.assign(n + 1, std::vector<long long>(1 << n));
for (int i = 1; i <= n; ++i) {
for (int s = 0; s < (1 << n); ++s)
if (__builtin_popcount(s) == i) chaint[i][s] = chain[s];
f(chaint[i]);
}
dfs(n, 1, std::vector<long long>(1 << n, 1));
ans.resize(1 << n);
for (int s = 0; s < (1 << (n - 1)); ++s) {
std::vector<int> a;
int l = -1;
for (int i = 0; i < n - 1; ++i) {
if (~s >> i & 1) {
a.push_back(i - l);
l = i;
}
}
a.push_back(n - 1 - l);
std::sort(a.begin(), a.end());
ans[s] = res[a];
}
--n;
h(ans);
for (int i = 0; i < (1 << n); ++i)
std::cout << ans[i] << " \n"[i == (1 << n) - 1];
return 0;
}
| 18
|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 1117;
vector<int> g[maxN];
queue<int> q;
bool mark[maxN];
int n, m, st, ex, dis[maxN], len[maxN], p, sum;
vector<int> bt;
void bfs(int v) {
mark[v] = true;
q.push(v);
while (q.empty() == false) {
int u = q.front();
q.pop();
for (int i = 0; i < g[u].size(); i++)
if (!mark[g[u][i]]) {
mark[g[u][i]] = true;
len[g[u][i]] = len[u] + 1;
q.push(g[u][i]);
}
}
}
void make_graph() {
p = 0;
bool b = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
p++;
if (dis[p] != -1) {
if (dis[p + 1] != -1 && j != m)
g[p].push_back(p + 1), g[p + 1].push_back(p);
if (dis[p - 1] != -1 && j != 1)
g[p].push_back(p - 1), g[p - 1].push_back(p);
int d1 = p - (m - j) - (j - 1) - 1, d2 = p + (m - j) + (j - 1) + 1;
if (d1 > 0 && dis[d1] != -1 && i != 1)
g[p].push_back(d1), g[d1].push_back(p);
if (d2 > 0 && dis[d2] != -1 && i != n)
g[p].push_back(d2), g[d2].push_back(p);
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
p++;
char c;
cin >> c;
if (c == 'S')
st = p, dis[p] = 0;
else if (c == 'E')
ex = p, dis[p] = 0;
else if (c != 'T') {
dis[p] = c - '0';
if (c - '0' > 0) bt.push_back(p);
} else
dis[p] = -1;
}
make_graph();
bfs(ex);
int me = len[st];
for (auto i : bt) {
if (len[i] <= me && len[i] > 0) sum += dis[i];
}
cout << sum << endl;
return 0;
}
| 7
|
#include <bits/stdc++.h>
using namespace std;
int f(string s) {
cout << s << endl;
int w;
cin >> w;
if (w == 0) exit(0);
return w;
}
int main() {
const int N = 300;
int st = f(string(N, 'a'));
int n = 2 * N - (st + f(string(N, 'b')));
string t(n, 'a');
int A = N - st, B = n - A;
st = B;
for (int i = 0; i < n - 1; i++) {
t[i] = 'b';
if (f(t) > st)
t[i] = 'a';
else
st--;
}
if (st) t.back() = 'b';
f(t);
}
| 15
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int N = 2e2 + 7;
const int M = 1e9 + 7;
const long long MAXN = 1e18 + 7;
const long long Mod = 998244353;
int _, i, j, k, n, m, t, T, p, s, x, y, z, l, r, h, len, o, u, v, w, st, en, sx,
sy, ma, mi, th, num, flag, ans, mid, cnt, now, tmp;
int a[N], c[N];
vector<int> g;
char ch;
string s1, s2, s3;
signed main() {
ios::sync_with_stdio(false);
int t = 1;
cin >> t;
while (t--) {
cin >> n;
n--;
k = 1;
g.clear();
while (n >= 3 * (1 << k)) {
g.push_back(1 << (k - 1));
n -= (1 << k);
k++;
}
if (k == 1) {
if (n == 1)
cout << 1 << endl << 0 << endl;
else if (n == 2)
cout << 1 << endl << 1 << endl;
else if (n == 3)
cout << 2 << endl << 0 << " " << 1 << endl;
else if (n == 4)
cout << 2 << endl << 1 << " " << 0 << endl;
else
cout << 2 << endl << 1 << " " << 1 << endl;
continue;
}
if (n == (1 << k))
g.push_back(1 << (k - 1));
else {
k--;
m = max(1 << k, int(ceil(1.0 * n / 3))) - (1 << k);
g.push_back(m), g.push_back(n - 2 * (m + (1 << k)));
}
cout << g.size() << endl;
for (i = 0; i < g.size(); i++) cout << g[i] << " ";
cout << endl;
}
return 0;
}
| 11
|
#include <bits/stdc++.h>
using namespace std;
ifstream first("test.in");
ofstream g("test.out");
char second[200005];
long long n, ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> second[i];
for (int i = 1; i <= n; i += 2) {
if (second[i] == second[i + 1]) {
if (second[i + 1] == 'a')
second[i + 1] = 'b';
else
second[i + 1] = 'a';
ans++;
}
}
cout << ans << '\n';
for (int i = 1; i <= n; i++) cout << second[i];
return 0;
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
long long n, l[100009], r[100009], c;
int main() {
cin >> n;
c = n;
for (int i = 0; i < n; i++) cin >> l[i] >> r[i];
sort(l, l + n);
sort(r, r + n);
for (int j = 0; j < n; j++) {
c += max(l[j], r[j]);
}
cout << c;
return 0;
}
| 11
|
#include <bits/stdc++.h>
using namespace std;
const int N = 21;
const int M = 1e6 + 50;
const int INF = 0x3f3f3f3f;
double eps = 1e-6;
char str[N][N];
int a[N][N], s[N][N], c[N][N];
int dp[1 << N];
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", str[i]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int maxv = -1;
for (int k = 0; k < n; k++)
if (str[i][j] == str[k][j]) {
maxv = max(maxv, a[k][j]);
s[i][j] |= 1 << k;
c[i][j] += a[k][j];
}
c[i][j] -= maxv;
}
memset(dp, INF, sizeof(dp));
dp[0] = 0;
for (int st = 0; st < (1 << n); st++) {
for (int j = 0; j < n; j++)
if (!(st & (1 << j))) {
for (int i = 0; i < m; i++) {
dp[st | (1 << j)] = min(dp[st | (1 << j)], dp[st] + a[j][i]);
dp[st | s[j][i]] = min(dp[st | s[j][i]], dp[st] + c[j][i]);
}
}
}
printf("%d\n", dp[(1 << n) - 1]);
return 0;
}
| 17
|
#include <bits/stdc++.h>
using namespace std;
int v[1024];
int main() {
int m, n, sum = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
int a, b;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
sum += min(v[a], v[b]);
}
printf("%d\n", sum);
}
| 6
|
#include <bits/stdc++.h>
using namespace std;
int g(int n) {
int maxi = 1;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
if (i == n / i)
maxi = max(maxi, i);
else
maxi = max(maxi, max(i, n / i));
}
}
return maxi;
}
int main() {
unordered_map<int, int> umap;
for (int k = 1; k <= 26; k++) {
int n = pow(2, k) - 1;
umap[n] = g(n);
}
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (umap.find(a) != umap.end()) {
cout << umap[a] << endl;
continue;
}
int l = (log((double)a) / log(2.0)) + 1;
cout << (long long)pow(2, l) - 1 << endl;
}
}
| 7
|
#include <bits/stdc++.h>
using namespace std;
const unsigned int M = 1000000007;
long long int ceil_div(long long int a, long long int b) {
return (a + b - 1) / b;
}
void fun() {
int a, b, n, i, j, x, nb, q, k;
cin >> n;
string str;
cin >> str;
if (n == 2) {
if (str[0] >= str[1]) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
cout << "2" << endl;
cout << str[0] << " " << str[1] << endl;
}
} else {
cout << "YES" << endl;
cout << "2" << endl;
cout << str[0] << " ";
for (i = 1; i < n; i++) {
cout << str[i];
}
cout << endl;
}
}
int main() {
long long int t;
cin >> t;
while (t--) {
fun();
}
}
| 1
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
string t;
string a, b, c, d, e;
cin >> t;
cin >> a >> b >> c >> d >> e;
if (t[0] == a[0] or t[0] == b[0] or t[0] == c[0] or t[0] == d[0] or
t[0] == e[0] or t[1] == a[1] or t[1] == b[1] or t[1] == c[1] or
t[1] == d[1] or t[1] == e[1])
cout << "YES";
else
cout << "NO";
return 0;
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
int pp[110][110];
int main() {
int a, b;
while (~scanf("%d %d", &a, &b)) {
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
cin >> pp[i][j];
}
}
int flag;
while (a > 0 && !(a & 1)) {
flag = 1;
a /= 2;
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
if (pp[i][j] != pp[a * 2 + 1 - i][j]) {
flag = 0;
break;
}
}
if (!flag) break;
}
if (!flag) break;
}
if (flag == 0)
cout << 2 * a << endl;
else
cout << a << endl;
}
return 0;
}
| 5
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 305;
const ll mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a, b, n;
for (int i = 1; i <= 300; i++) {
cout << 'a';
}
cout << endl;
cin >> a;
if (!a) return 0;
for (int i = 1; i <= 300; i++) {
cout << 'b';
}
cout << endl;
cin >> b;
if (!b) return 0;
a = 300 - a, b = 300 - b;
n = a + b;
vector<char> ans;
ans.assign(n, 'a');
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < ans.size(); j++) {
if (i != j)
cout << ans[j];
else
cout << 'b';
}
cout << endl;
int cnt;
cin >> cnt;
if (!cnt) return 0;
if (cnt <= b) {
ans[i] = 'b';
b--;
}
}
if (b) ans[n - 1] = 'b';
for (auto i : ans) {
cout << i;
}
cout << endl;
return 0;
}
| 15
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> g[N];
map<int, int> mp[2], p;
int root, n, ans[N], sz[N];
bool big[N];
void dfs(int u) {
sz[u] = 1, ans[u] = n - 1;
for (auto v : g[u]) {
dfs(v);
sz[u] += sz[v];
}
++mp[0][sz[u]];
}
void add(int u, int c) {
++mp[c][sz[u]];
if (mp[c ^ 1][sz[u]] == 1)
mp[c ^ 1].erase(sz[u]);
else
--mp[c ^ 1][sz[u]];
for (auto v : g[u])
if (!big[v]) add(v, c);
}
void gao(map<int, int> &G, int mx2, int mx1, int mn, int u, int off) {
int L = mx2, R = mx1, t = n - 1;
while (R - L > 1) {
int mid = L + R >> 1;
int cut = mx1 - mid + off;
auto it = G.lower_bound(cut);
if (it == G.end())
L = mid;
else if (it->first + mn <= mid + off)
R = mid;
else
L = mid;
}
ans[u] = min(ans[u], R);
}
void dfs2(int u, bool ok) {
++p[sz[u]];
if (mp[0][sz[u]] == 1)
mp[0].erase(sz[u]);
else
--mp[0][sz[u]];
int ws = -1, mx1 = -1, mx2 = -1, mn = n - 1;
for (auto v : g[u]) {
if (sz[v] > mx1)
ws = v, mx2 = mx1, mx1 = sz[v];
else if (sz[v] == mx1)
mx2 = mx1;
else
mx2 = max(mx2, sz[v]);
mn = min(mn, sz[v]);
}
if (u != root) mn = min(mn, n - sz[u]);
for (auto v : g[u])
if (v != ws) dfs2(v, 0);
if (~ws) big[ws] = 1, dfs2(ws, 1);
if (mx1 >= n - sz[u]) {
mx2 = max(mx2, n - sz[u]);
gao(mp[1], mx2 - 1, mx1, mn, u, 0);
}
++mp[0][sz[u]];
add(u, 1);
if (p[sz[u]] == 1)
p.erase(sz[u]);
else
--p[sz[u]];
if (n - sz[u] > mx1) {
mx2 = mx1;
gao(mp[0], mx2 - 1, n - sz[u], mn, u, 0);
gao(p, mx2 - 1, n - sz[u], mn, u, sz[u]);
}
if (~ws) big[ws] = 0;
if (!ok) add(u, 0);
}
int main() {
scanf("%d", &n);
for (int i = 1, x, y; i <= n; ++i) {
scanf("%d%d", &x, &y);
if (!x)
root = y;
else
g[x].push_back(y);
}
dfs(root);
dfs2(root, 1);
for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
return 0;
}
| 25
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
const int mod = 1e9;
vector<int> graph[1000];
bool used[1000];
vector<int> ans;
void dfs(int v) {
used[v] = 1;
ans.push_back(v);
for (auto to : graph[v]) {
if (used[to]) continue;
dfs(to);
return;
}
}
void solve() {
int n;
cin >> n;
ans.clear();
for (int i = 0; i < n; i++) {
graph[i].clear();
used[i] = false;
}
if (n < 4) {
cout << -1 << endl;
return;
} else {
for (int i = 0; i < n; i++) {
if (i + 2 < n) graph[i].push_back(i + 2);
if (i + 3 < n) graph[i].push_back(i + 3);
if (i + 4 < n) graph[i].push_back(i + 4);
}
for (int i = 0; i < n; i++) {
if (i - 2 >= 0) graph[i].push_back(i - 2);
if (i - 3 >= 0) graph[i].push_back(i - 3);
if (i - 4 >= 0) graph[i].push_back(i - 4);
}
for (int i = 0; i < n; i++) {
dfs(i);
if (ans.size() == n) {
for (int j = 0; j < n; j++) cout << ans[j] + 1 << " ";
cout << endl;
return;
}
ans.clear();
for (int j = 0; j < n; j++) used[j] = false;
}
}
cout << -1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
| 8
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(0) * 2;
const double EPS = 1e-8;
const long long MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int oo = 1e9;
const double foo = 1e30;
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcounll(s);
}
template <class T>
T sqr(T x) {
return x * x;
}
inline void addmod(int& a, int val, int p = MOD) {
if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
int main() {
int x1, y1, x2, y2, x, y, t;
cin >> x1 >> y1 >> x2 >> y2;
cin >> x >> y;
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
if (dx % x == 0 && dy % y == 0) {
if ((dx / x) % 2 == (dy / y) % 2) {
cout << "YES";
} else
cout << "NO";
} else
cout << "NO";
return 0;
}
| 4
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << n - 1 << "\n";
for (long long i = 1; i < n; i++) {
cout << i + 1 << " ";
}
cout << "\n";
}
return 0;
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
if (n == 1 || n == 2) {
cout << "No";
} else {
cout << "Yes", cout << endl;
int eve = n / 2;
int odd = n - eve;
cout << eve << " ";
for (int i = 2; i <= n; i++) {
if (i % 2 == 0) cout << i << " ";
}
cout << endl;
cout << odd << " ";
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) cout << i << " ";
}
}
return 0;
}
| 3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n;
cin >> t;
while (t--) {
cin >> n;
if (n <= 30) {
cout << "NO" << endl;
} else if (n == 36 || n == 40 || n == 44) {
cout << "YES" << endl;
cout << 6 << " " << 10 << " " << 15 << " " << n - 31 << endl;
} else {
cout << "YES" << endl;
cout << 6 << " " << 10 << " " << 14 << " " << n - 30 << endl;
}
}
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long num[maxn];
string s[100] = {
"4", "7", "44", "47", "77",
"444", "447", "477", "777", "4444",
"4447", "4477", "4777", "7777", "44444",
"44447", "44477", "44777", "47777", "77777",
"444444", "444447", "444477", "444777", "447777",
"477777", "777777", "4444444", "4444447", "4444477",
"4444777", "4447777", "4477777", "4777777", "7777777",
"44444444", "44444447", "44444477", "44444777", "44447777",
"44477777", "44777777", "47777777", "77777777", "444444444",
"444444447", "444444477", "444444777", "444447777", "444477777",
"444777777", "447777777", "477777777", "777777777"};
int len;
int main() {
int len = 0;
memset(num, 0, sizeof(num));
for (int i = 0; i < 54; i++) {
sort(s[i].begin(), s[i].end());
int x = 0;
for (int j = 0; j < s[i].size(); j++) x = x * 10 + s[i][j] - '0';
num[len++] = x;
while (next_permutation(s[i].begin(), s[i].end())) {
x = 0;
for (int j = 0; j < s[i].size(); j++) x = x * 10 + s[i][j] - '0';
num[len++] = x;
}
}
num[len++] = 4444444444;
sort(num, num + len);
long long l, r;
long long sum = 0;
long long MIN = 0;
scanf("%I64d %I64d", &l, &r);
for (int i = 0; i < len; i++) {
if (num[i] < l)
continue;
else if (num[i] >= l && num[i] < r) {
if (MIN == 0)
MIN = num[i];
else
MIN = min(MIN, num[i]);
sum += num[i] * (num[i] - max(num[i - 1], l));
} else {
if (num[i - 1] < l)
sum += num[i] * (r - l + 1);
else
sum += num[i] * (r - num[i - 1]);
break;
}
}
printf("%I64d\n", sum + MIN);
}
| 3
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
long long dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, mxp, reg;
cin >> n >> mxp >> reg;
long long last = mxp;
vector<long long> vis(n, 0), pw(n), dmg(n);
for (long long i = 0; i < n; i++) cin >> pw[i] >> dmg[i];
vector<pair<long long, long long> > ans;
long long t = 0;
while (1) {
long long prev = last;
for (long long i = 0; i < n; i++)
if (vis[i]) last -= dmg[i];
last += reg;
if (last > mxp) last = mxp;
if (last <= 0) break;
long long mx = 0;
long long k;
for (long long i = 0; i < n; i++) {
if (!vis[i]) {
if (pw[i] * mxp >= last * 100)
if (mx < dmg[i]) {
mx = dmg[i];
k = i;
}
}
}
if (mx) {
vis[k] = 1;
ans.push_back({t, k + 1});
}
t++;
if (prev > last || mx > 0)
continue;
else
break;
}
if (last > 0)
cout << "NO\n";
else {
cout << "YES\n";
cout << t << " " << ans.size() << "\n";
for (auto i : ans) cout << i.first << " " << i.second << "\n";
}
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<int> v);
void print_array(int* array, int start, int end);
int islands[30010], x, n, dd;
int memo[30010][400];
int solve(int pos, int d) {
if (d == 0) return 0;
if (pos > 30000) return 0;
if (memo[pos][d - dd + 200] != -1) return memo[pos][d - dd + 200];
return memo[pos][d - dd + 200] =
islands[pos] +
max(solve(pos + d, d),
max(solve(pos + d - 1, d - 1), solve(pos + d + 1, d + 1)));
}
int main() {
scanf("%d", &n), scanf("%d", &dd);
memset(islands, 0, sizeof islands);
for (int i = (0), _b = (n); i < _b; i++) {
scanf("%d", &x);
islands[x]++;
}
memset(memo, -1, sizeof memo);
printf("%d\n", solve(dd, dd));
return 0;
}
void print_array(int* array, int start, int end) {
printf("[");
for (int i = (start), _b = (end); i < _b; i++) {
printf("%d ", array[i]);
}
printf("]");
printf("\n");
}
void print_vector(vector<int> v) {
printf("[");
for (int i = (0), _b = (v.size()); i < _b; i++) {
printf("%d ", v[i]);
}
printf("]");
printf("\n");
}
| 11
|
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, string> m;
m[0] = "January", m[1] = "February", m[2] = "March", m[3] = "April",
m[4] = "May", m[5] = "June", m[6] = "July", m[7] = "August",
m[8] = "September", m[9] = "October", m[10] = "November", m[11] = "December";
int k, i, n;
string s;
cin >> s >> k;
for (i = 0; i < 12; i++) {
if (s == m[i]) {
break;
}
}
n = (i + k) % 12;
cout << m[n] << '\n';
return 0;
}
| 1
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
int a[maxn];
int main() {
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);
printf("%d", a[n - 1]);
for (int i = 1; i < n - 1; ++i) printf(" %d", a[i]);
printf(" %d\n", a[0]);
return 0;
}
| 5
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
struct Node {
int l, r, k, id;
bool operator<(const Node &a) const { return r < a.r; }
} a[MAXN], b[MAXN];
set<pair<int, int> > st;
int n, m, ans[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r, a[i].id = i;
cin >> m;
for (int i = 1; i <= m; i++) cin >> b[i].l >> b[i].r >> b[i].k, b[i].id = i;
sort(a + 1, a + n + 1);
sort(b + 1, b + m + 1);
for (int i = 1, pos = 1; i <= m; i++) {
while (pos <= n && a[pos].r <= b[i].r)
st.insert(make_pair(a[pos].l, a[pos].id)), pos++;
while (b[i].k) {
auto it = st.lower_bound(make_pair(b[i].l, 0));
if (it == st.end()) break;
ans[it->second] = b[i].id;
b[i].k--;
st.erase(it);
}
}
for (int i = 1; i <= n; i++)
if (!ans[i]) return cout << "NO" << endl, 0;
cout << "YES" << endl;
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
return 0;
}
| 13
|
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
int main() {
long long n, ans = 0, numb;
char f;
cin >> n >> f;
if (n % 2 == 0) {
if (n / 2 % 2 == 0)
numb = n / 2;
else
numb = n / 2 + 1;
} else {
if ((n - 1) / 2 % 2 == 0)
numb = n / 2 + 1;
else
numb = n / 2;
}
ans = (numb - 1) * 6;
ans += (numb - 1) / 2 * 4;
if ((numb) % 2 == 0) ++ans;
string s = "0fedabc";
ans += s.find(f);
cout << ans;
}
| 4
|
#include <bits/stdc++.h>
using namespace std;
int n, i, j;
int x, y, r;
int F1[50], F2[50];
int printN(int a) {
if (a < 0) {
cout << "(0-";
printN(-a);
cout << ")";
} else if (a <= 50)
cout << a;
else {
cout << "(50+";
printN(a - 50);
cout << ")";
}
return 0;
}
int printF(int *F, int i) {
int m = F[i], a = F[i - 1], b = F[i + 1];
cout << "((";
printN(m);
cout << "+(";
printN((b - a) / 2);
cout << "*((t-" << i << ")-abs((t-" << i << ")))))+abs(";
for (int j = i; j < n; j++) F[j] -= m;
if (i < n - 2)
printF(F, i + 1);
else {
cout << "(";
printN(b - m);
cout << "*(t-" << i << "))";
}
cout << "))";
return 0;
}
int main() {
cin >> n;
for (i = 0; i < n; i++) {
cin >> x >> y >> r;
F1[i] = 2 * (x / 2);
F2[i] = 2 * (y / 2);
}
if (n == 1)
cout << F1[0] << endl << F2[0];
else if (n == 2) {
cout << "(" << F1[0];
if (F1[1] >= F1[0])
cout << "+(" << F1[1] - F1[0];
else
cout << "-(" << F1[0] - F1[1];
cout << "*t))" << endl;
cout << "(" << F2[0];
if (F2[1] > F2[0])
cout << "+(" << F2[1] - F2[0];
else
cout << "-(" << F2[0] - F2[1];
cout << "*t))";
} else {
for (i = 0; i < n; i++) F1[i] += i * 50, F2[i] += i * 50;
cout << "(";
printF(F1, 1);
cout << "-(50*t))" << endl;
cout << "(";
printF(F2, 1);
cout << "-(50*t))";
}
return 0;
}
| 14
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const int N = 250010;
int n, uD, nxt[N], d[N];
long long ans = 0, sum[N], f[2][N];
vector<pair<int, int> > e[N];
vector<int> deg[N];
bool vis[N];
struct Heap {
priority_queue<long long> In, Out;
inline void push(long long x) { In.push(x); }
inline void erase(long long x) { Out.push(x); }
long long top() {
for (; !Out.empty() && In.top() == Out.top(); In.pop(), Out.pop())
;
return In.top();
}
void pop() { top(), In.pop(); }
long long size() { return In.size() - Out.size(); }
};
Heap h[N];
inline int Max(const int &x, const int &y) { return x > y ? x : y; }
inline bool cmp(const pair<int, int> &x, const pair<int, int> &y) {
return d[x.first] > d[y.first];
}
void solve(int x) {
for (auto v : e[x]) {
if (d[v.first] <= uD) break;
h[v.first].push(v.second), sum[v.first] += v.second;
}
}
void pop(int u, int num) {
for (; h[u].size() > num;) sum[u] -= h[u].top(), h[u].pop();
}
void Pop(int u, int num, vector<long long> &x) {
for (; h[u].size() > num;)
sum[u] -= h[u].top(), x.push_back(h[u].top()), h[u].pop();
}
void dfs(int u) {
vis[u] = 1;
int num = d[u] - uD;
pop(u, num);
long long ss = 0;
vector<long long> del, add;
for (auto v : e[u])
if (!vis[v.first]) {
if (d[v.first] <= uD) break;
dfs(v.first);
if (f[1][v.first] + v.second <= f[0][v.first])
--num, ss += f[1][v.first] + v.second;
else {
ss += f[0][v.first];
long long dt = f[1][v.first] + v.second - f[0][v.first];
del.push_back(dt), sum[u] += dt, h[u].push(dt);
}
}
Pop(u, Max(0, num), add), f[0][u] = ss + sum[u];
Pop(u, Max(0, num - 1), add), f[1][u] = ss + sum[u];
for (auto x : add) h[u].push(x), sum[u] += x;
for (auto x : del) h[u].erase(x), sum[u] -= x;
}
int main() {
scanf("%d", &n);
for (int i = 1, x, y, z; i < n; i++) {
scanf("%d%d%d", &x, &y, &z), ans += z, ++d[x], ++d[y];
e[x].emplace_back(y, z), e[y].emplace_back(x, z);
}
for (int i = 1; i <= n; i++)
deg[d[i]].push_back(i), sort(e[i].begin(), e[i].end(), cmp);
nxt[n] = n + 1;
for (int i = n - 1; i; i--) {
if (deg[i + 1].size())
nxt[i] = i + 1;
else
nxt[i] = nxt[i + 1];
}
printf("%I64d", ans);
for (int i = 1; i < n; i++) {
ans = 0, uD = i;
for (auto x : deg[i]) solve(x);
for (int j = nxt[i]; j <= n; j = nxt[j])
for (auto x : deg[j])
if (!vis[x]) dfs(x), ans += f[0][x];
for (int j = nxt[i]; j <= n; j = nxt[j])
for (auto x : deg[j]) vis[x] = 0;
printf(" %I64d", ans);
}
return 0;
}
| 26
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int c, d, tmp, plus, kurang, nilai, a, b, k, n = 0, min = 0, sum = 1, cek,
simpan[100], cetak[100];
char s;
simpan[0] = 1;
while (s != '=') {
scanf("%c", &s);
if (s == '?') n++;
if (s == '-') {
min++;
simpan[n] = 0;
}
if (s == '+') {
sum++;
simpan[n] = 1;
}
}
scanf("%d", &cek);
if (sum != 0) {
a = (cek + min) / sum;
b = (cek + min) % sum;
} else {
a = 0;
b = 0;
}
if (min != 0) {
c = (sum - cek) / min;
d = (sum - cek) % min;
} else {
c = 0;
d = 0;
}
if ((b > 0 && a + 1 > cek) || (b == 0 && a > cek) || (a == 0 && c > cek) ||
(c == 0 && a > cek) || (d > 0 && c + 1 > cek) || (d == 0 && c > cek) ||
(a == 0 && c == 0)) {
printf("Impossible\n");
return 0;
}
printf("Possible\n");
if (a > 0) {
for (tmp = 0; tmp < n; tmp++) {
if (simpan[tmp] == 1) {
if (b > 0) {
cetak[tmp] = a + 1;
b--;
} else {
cetak[tmp] = a;
}
} else {
cetak[tmp] = 1;
}
}
} else if (c > 0) {
for (tmp = 0; tmp < n; tmp++) {
if (simpan[tmp] == 0) {
if (d > 0) {
cetak[tmp] = c + 1;
d--;
} else {
cetak[tmp] = c;
}
} else {
cetak[tmp] = 1;
}
}
}
printf("%d ", cetak[0]);
for (tmp = 1; tmp < n; tmp++) {
if (simpan[tmp] == 1)
printf("+ ");
else
printf("- ");
printf("%d ", cetak[tmp]);
}
printf("= %d\n", cek);
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, cnt = 0;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> vec;
for (int i = 1; i < (n - 1); i++) {
if (a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1) vec.push_back(i + 1);
}
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++) {
if ((i + 1) < vec.size() && vec[i + 1] - vec[i] == 2) {
i++;
cnt++;
} else
cnt++;
}
cout << cnt << endl;
}
| 2
|
#include <bits/stdc++.h>
using namespace std;
const int N=1500;
array<int,N>a[N],b[N];
array<int,N>c[N],d,f;
vector<int>ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,m;cin>>n>>m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin>>a[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j <m; ++j) {
cin>>b[i][j];
}
}
for(int j=0;j<m;j++)
for(int i=0;i+1<n;i++)
if(b[i][j]>b[i+1][j])
c[i][j]=1,++d[j];
set<int>s;
for (int i = 0; i < n-1; ++i) s.insert(i);
for (;;) {
int p=-1;
for (int i = 0; i < m; ++i) {
if (!d[i])p=i;
}
if (p==-1)break;
d[p]=-1;
ans.push_back(p);
for(int i=0;i+1<n;i++){
if(!f[i]&&b[i][p]<b[i+1][p]){
f[i]=1;
for(int j=0;j<m;j++){
if(c[i][j])c[i][j]=0,--d[j];
}
}
}
}
vector<int>id(n);
iota(id.begin(),id.end(),0);
for (int i = (int)ans.size()-1;i>=0; --i) {
stable_sort(id.begin(),id.end(),[&](int u,int v){
return a[u][ans[i]]<a[v][ans[i]];
});
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[id[i]][j]!=b[i][j]){
puts("-1");return 0;
}
}
}
cout<<ans.size()<<'\n';
for (int i =(int)ans.size()-1;i>=0; --i) {
cout<<ans[i]+1<<' ';
}
}
| 18
|
#include <bits/stdc++.h>
using namespace std;
int a[100001] = {0}, b[100001] = {0}, n, x, y;
int main() {
cin >> n >> x;
int answer = -1;
while (n--) {
cin >> y;
if (a[y]) answer = 0;
if (answer && (a[x & y] || b[y])) answer = 1;
if (answer && answer != 1 && b[x & y]) answer = 2;
a[y] = b[x & y] = 1;
}
cout << answer << endl;
return 0;
}
| 4
|
#include <bits/stdc++.h>
using namespace std;
struct pnt {
int x, y, id;
pnt(int x = 0, int y = 0, int id = -1) : x(x), y(y), id(id) {}
void in(int i) {
scanf("%d %d", &x, &y);
id = i;
}
int dist(const pnt &p) { return abs(x + y - p.x - p.y); }
void out() { printf("%d %d\n", x, y); }
} p[4];
const int inf = 2e9;
int ans;
pnt q[4], r[4];
void update() {
int cur = 0;
for (int i = 0; i < 4; i++) {
if (r[i].x != p[i].x && r[i].y != p[i].y) return;
cur = max(cur, r[i].dist(p[i]));
}
if (ans < cur) return;
for (int i = 0; i < 4; i++) q[p[i].id] = r[i];
ans = cur;
}
void solve(int i, int j, int k, int l) {
r[l] = pnt(p[l].x, p[l].x + r[j].y - r[j].x);
r[i] = pnt(r[l].x, r[j].y);
r[k] = pnt(r[j].x, r[l].y);
update();
r[l] = pnt(p[l].y - r[j].y + r[j].x, p[l].y);
r[i] = pnt(r[l].x, r[j].y);
r[k] = pnt(r[j].x, r[l].y);
update();
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
for (int i = 0; i < 4; i++) p[i].in(i);
ans = inf;
sort(p, p + 4, [&](pnt a, pnt b) {
return pair<int, int>(a.x, a.y) < pair<int, int>(b.x, b.y);
});
if (p[0].x == p[1].x && p[2].x == p[3].x && p[1].x != p[2].x) {
int d = abs(p[1].x - p[2].x);
r[0].x = p[0].x;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; j++) {
int sum = p[i].y + p[j].y;
if ((i ^ j) & 1)
sum -= d;
else if (i & 1)
sum -= 2 * d;
for (int k = 0; k < 2; k++) {
r[0].y = sum >> 1;
if (k) r[0].y = sum - r[0].y;
r[1] = pnt(r[0].x, r[0].y + d);
r[2] = pnt(r[0].x + d, r[0].y);
r[3] = pnt(r[0].x + d, r[0].y + d);
update();
}
}
}
}
sort(p, p + 4, [&](pnt a, pnt b) {
return pair<int, int>(a.y, a.x) < pair<int, int>(b.y, b.x);
});
if (p[0].y == p[1].y && p[2].y == p[3].y && p[1].y != p[2].y) {
int d = abs(p[1].y - p[2].y);
r[0].y = p[0].y;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; j++) {
int sum = p[i].x + p[j].x;
if ((i ^ j) & 1)
sum -= d;
else if (i & 1)
sum -= 2 * d;
for (int k = 0; k < 2; k++) {
r[0].x = sum >> 1;
if (k) r[0].x = sum - r[0].x;
r[2] = pnt(r[0].x, r[0].y + d);
r[1] = pnt(r[0].x + d, r[0].y);
r[3] = pnt(r[0].x + d, r[0].y + d);
update();
}
}
}
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (i != j) {
int k = 0, l = 0;
while (k == i || k == j) k++;
l = k + 1;
while (l == i || l == j) l++;
pnt pt = pnt(p[j].x, p[i].y);
r[j] = pt;
solve(i, j, k, l);
solve(i, j, l, k);
r[i] = pt;
solve(k, i, j, l);
solve(l, i, j, k);
}
if (ans == inf)
puts("-1");
else {
printf("%d\n", ans);
for (int i = 0; i < 4; i++) q[i].out();
}
}
return 0;
}
| 22
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 0, i, a = 0, count1 = 0;
vector<int> inp;
string in;
cin >> n;
cin >> in;
char ch;
for (i = 0; i < n; i++) {
ch = a + '0';
if (in[i] != ch) count++;
a = 1 - a;
}
a = 1;
for (i = 0; i < n; i++) {
ch = a + '0';
if (in[i] != ch) count1++;
a = 1 - a;
}
cout << min(count, count1);
return 0;
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
const int m = 256;
int n, Q, sl, fh, ans, rt_num, fa[50010], dep[50010], vis[50010], lst[50010],
val[50010], f[50010][m], mx[50010][m];
int t, h[50010];
struct Tre {
int to, nxt;
} e[50010 << 1];
struct Trie {
int sn[2];
} tre[1 << 10];
int rd() {
sl = 0;
fh = 1;
char ch = getchar();
while (ch < '0' || '9' < ch) {
if (ch == '-') fh = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar();
return sl * fh;
}
void add(int u, int v) {
e[++t] = (Tre){v, h[u]};
h[u] = t;
e[++t] = (Tre){u, h[v]};
h[v] = t;
}
void ins(int x) {
int rt = 0;
for (int i = 7; ~i; --i) {
if (!tre[rt].sn[(x >> i) & 1]) {
tre[rt].sn[(x >> i) & 1] = ++rt_num;
tre[rt_num] = (Trie){{0, 0}};
}
rt = tre[rt].sn[(x >> i) & 1];
}
}
int query(int x, int u) {
int d, v = 0, rt = 0, ret = 0;
for (int i = 7; ~i; --i) {
d = ((x >> i) & 1) ^ 1;
if (tre[rt].sn[d])
rt = tre[rt].sn[d], ret |= 1 << i;
else
rt = tre[rt].sn[d ^= 1];
v |= d << i;
}
return ret << 8 | mx[u][v];
}
void upd(int &x, int y) {
if (y > x) x = y;
}
void dfs(int u) {
int v, i;
if (dep[u] >= m) {
rt_num = 0;
tre[0] = (Trie){{0, 0}};
for (i = u; dep[u] - dep[i] < m; i = fa[i]) {
upd(mx[u][val[i] >> 8], (dep[u] - dep[i] ^ val[i]) & 255);
if (vis[val[i] >> 8] != u) ins(val[i] >> 8), vis[val[i] >> 8] = u;
}
lst[u] = i;
for (i = 0; i < m; ++i) f[u][i] = query(i, u);
}
for (i = h[u]; i; i = e[i].nxt)
if ((v = e[i].to) != fa[u]) {
fa[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
int main() {
n = rd();
Q = rd();
int x, y, dis;
for (int i = 1; i <= n; ++i) val[i] = rd();
for (int i = 1; i < n; ++i) x = rd(), y = rd(), add(x, y);
dep[1] = 1;
dfs(1);
while (Q--) {
x = rd();
y = rd();
dis = ans = 0;
for (; dep[y] - dep[x] >= m; y = lst[y], dis++) ans = max(ans, f[y][dis]);
for (dis <<= 8; y != fa[x]; y = fa[y], dis++) ans = max(ans, val[y] ^ dis);
printf("%d\n", ans);
}
}
| 24
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int trie[N][26], tot, fail[N], fg[N], L[N];
char s[N], t[N];
int cnt[N], st[N], nex[N], ans[N];
queue<int> Q[N];
void insert(int ix) {
int len = strlen(t + 1), p = 0;
for (int i = 1; i <= len; i++) {
int c = t[i] - 'a';
if (!trie[p][c]) trie[p][c] = ++tot;
p = trie[p][c];
}
fg[p] = ix;
L[p] = len;
}
void getfail() {
queue<int> q;
for (int i = 0; i < 26; i++) {
if (trie[0][i]) {
fail[trie[0][i]] = 0;
q.push(trie[0][i]);
}
}
while (!q.empty()) {
int now = q.front();
q.pop();
if (fg[fail[now]])
nex[now] = fail[now];
else
nex[now] = nex[fail[now]];
for (int i = 0; i < 26; i++) {
if (trie[now][i]) {
fail[trie[now][i]] = trie[fail[now]][i];
q.push(trie[now][i]);
} else {
trie[now][i] = trie[fail[now]][i];
}
}
}
}
void getans() {
int len = strlen(s + 1), p = 0;
for (int i = 1; i <= len; i++) {
int c = s[i] - 'a';
p = trie[p][c];
for (int j = p; j; j = nex[j]) {
Q[fg[j]].push(i);
if (Q[fg[j]].size() == cnt[fg[j]])
ans[fg[j]] = min(ans[fg[j]], i - Q[fg[j]].front() + L[j]),
Q[fg[j]].pop();
}
}
}
int main() {
scanf("%s", s + 1);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%s", &cnt[i], t + 1);
insert(i);
ans[i] = N;
}
getfail();
getans();
for (int i = 1; i <= n; i++) printf("%d\n", ans[i] == N ? -1 : ans[i]);
return 0;
}
| 17
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline void read(long long &x) {
x = 0;
long long f = 1;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + (s ^ 48);
s = getchar();
}
x *= f;
}
inline long long Max(long long x, long long y) { return x > y ? x : y; }
inline long long Min(long long x, long long y) { return x < y ? x : y; }
inline long long Abs(long long x) {
if (x > 0) return x;
return -x;
}
long long a[400005][6], Good[400005];
int main() {
long long n;
read(n);
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= 5; ++j) read(a[i][j]);
if (n > 11) return !printf("0");
long long tot = 0;
for (register int i = 1; i <= n; ++i) {
for (register int j = 1; j <= n; ++j) {
if (i != j) {
for (register int k = 1; k <= n; ++k)
if (i != k && j != k) {
long long JD = 0;
for (register int l = 1; l <= 5; ++l)
JD += (a[j][l] - a[i][l]) * (a[k][l] - a[i][l]);
if (JD > 0) goto end;
}
}
}
Good[++tot] = i;
end:;
}
printf("%lld\n", tot);
for (register int i = 1; i <= tot; ++i) printf("%lld\n", Good[i]);
}
| 9
|
#include <bits/stdc++.h>
const int maxn = 510;
using namespace std;
const int dis[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}};
struct Node {
int x, y, d;
Node(int _x, int _y, int _d) : x(_x), y(_y), d(_d) {}
bool operator<(const Node &b) const {
if (d != b.d)
return d < b.d;
else if (x != b.x)
return x < b.x;
else if (y != b.y)
return y < b.y;
else
return false;
}
};
char s[maxn][maxn];
int d[maxn][maxn], n, m;
queue<pair<int, int> > q;
priority_queue<Node> que;
bool IsOk(int x, int y) {
if (x <= 0 || x > n)
return false;
else if (y <= 0 || y > m)
return false;
else if (s[x][y] != '.')
return false;
else if (d[x][y])
return false;
else
return true;
}
int main() {
int k;
scanf("%d%d%d", &n, &m, &k);
for (int i = (1); i <= (n); ++i) scanf("%s", s[i] + 1);
bool flag = true;
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (m); ++j)
if (flag && s[i][j] == '.') {
d[i][j] = 1;
q.push(pair<int, int>(i, j));
que.push(Node(i, j, d[i][j]));
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dis[0][i];
int ny = y + dis[1][i];
if (IsOk(nx, ny)) {
d[nx][ny] = d[x][y] + 1;
q.push(pair<int, int>(nx, ny));
que.push(Node(nx, ny, d[nx][ny]));
}
}
}
flag = false;
}
for (int i = (1); i <= (k); ++i) {
Node t = que.top();
que.pop();
s[t.x][t.y] = 'X';
}
for (int i = (1); i <= (n); ++i) printf("%s\n", s[i] + 1);
return 0;
}
| 8
|
#include <bits/stdc++.h>
using namespace std;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
struct circle {
double x, y, r, a1, a2, d;
} cir[100010];
bool cmp(circle c1, circle c2) {
if (c1.a1 == c2.a1) {
if (c1.a2 == c2.a2) return c1.d < c2.d;
return c1.a2 < c2.a2;
}
return c1.a1 < c2.a1;
}
int main() {
double x0, y0, v, T;
while (scanf("%lf %lf %lf %lf", &x0, &y0, &v, &T) == 4) {
double R = v * T;
int n, c = 0, f = 0;
scanf("%d", &n);
vector<pair<double, double> > thetas;
int k = 0;
for (int i = 0; i < n; i++) {
double x1, y1, r;
scanf("%lf %lf %lf", &x1, &y1, &r);
x1 -= x0;
y1 -= y0;
if (x1 * x1 + y1 * y1 <= r * r) f = 1;
double dis = sqrt(x1 * x1 + y1 * y1);
if (dis < (r + R)) {
double theta = atan2(y1, x1);
double DS = sqrt(x1 * x1 + y1 * y1 - r * r);
double phi;
if (DS <= R)
phi = asin(r / dis);
else {
phi = acos((dis * dis + R * R - r * r) / (2.0 * dis * R));
}
theta -= phi;
phi = theta + phi * 2.0;
if (theta < -acos(-1.0))
thetas.push_back({theta + 2.0 * acos(-1.0), acos(-1.0)}),
theta = -acos(-1.0);
if (phi > acos(-1.0))
thetas.push_back({-acos(-1.0), phi - 2.0 * acos(-1.0)}),
phi = acos(-1.0);
thetas.push_back({theta, phi});
}
}
double ans;
if (f)
ans = 1.0;
else {
sort(thetas.begin(), thetas.end());
double ang = 0.0;
double b1 = -500.0, b2 = 0.0;
for (int i = 0; i < (int)thetas.size(); i++) {
if (b1 < thetas[i].first) b1 = thetas[i].first;
if (thetas[i].second > b1) {
ang += thetas[i].second - b1;
b1 = thetas[i].second;
}
}
ans = ang / (2.0 * acos(-1.0));
ans = min(1.0, ans);
}
printf("%.15lf\n", ans);
}
return 0;
}
| 17
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<long long> avals, bvals;
int at = 0;
bool bad(ll a1, ll b1, ll a2, ll b2, ll a3, ll b3) {
return 1.0 * (b1 - b3) * (a2 - a1) < 1.0 * (b1 - b2) * (a3 - a1);
}
void add_line(ll a, ll b) {
while ((int)avals.size() >= 2 &&
bad(avals[avals.size() - 2], bvals[bvals.size() - 2],
avals[avals.size() - 1], bvals[bvals.size() - 1], a, b)) {
avals.pop_back();
bvals.pop_back();
}
avals.push_back(a);
bvals.push_back(b);
}
void get(ll x) {
if (at > (int)avals.size() - 1) at = avals.size() - 1;
while (at < (int)avals.size() - 1 &&
1.0 * avals[at + 1] * x + bvals[at + 1] <=
1.0 * avals[at] * x + bvals[at])
at++;
}
int main() {
int n;
scanf("%d", &n);
vector<ll> a(n), b(n);
for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
for (int i = 0; i < n; i++) scanf("%I64d", &b[i]);
vector<ll> dp(n + 1);
dp[0] = 0;
add_line(b[0], 0);
for (int i = 1; i < n; i++) {
get(a[i]);
dp[i] = avals[at] * a[i] + bvals[at];
add_line(b[i], dp[i]);
}
cout << dp[n - 1];
}
| 13
|
#include <bits/stdc++.h>
struct p {
int c, kol;
};
void swap(struct p *a, struct p *b) {
struct p c = *a;
*a = *b;
*b = c;
}
void divide(struct p *arr, int l, int r) {
if (r - l < 1) return;
int i = l - 1;
int j = r;
int razd = (rand() % (r - l)) + l;
swap(&arr[r], &arr[razd]);
while (j > i) {
do i++;
while (arr[i].c < arr[r].c);
do j--;
while (j != i && arr[j].c > arr[r].c);
if (j > i) swap(&arr[i], &arr[j]);
}
swap(&arr[i], &arr[r]);
divide(arr, i + 1, r);
divide(arr, l, i - 1);
}
void quickSort(struct p *arr, int size) {
srand(500000);
divide(arr, 0, size - 1);
}
int main() {
int n, max = -1;
static struct p arr[100055];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &arr[i].c, &arr[i].kol);
}
quickSort(arr, n);
arr[n].c = arr[n - 1].c + 1;
int z = arr[n].c + 20;
for (arr[n].c = arr[n - 1].c + 1; arr[n].c < z; arr[n].c++) {
int ok = 1;
int j = n - 1;
int st4 = 1;
while (j >= 0 && arr[n].c - 16 < arr[j].c) {
st4 <<= 2 * (arr[j + 1].c - arr[j].c);
if (arr[j].kol > st4) {
ok = 0;
break;
}
j--;
}
if (ok) {
printf("%d", arr[n].c);
return 0;
}
}
return 0;
}
| 8
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long lINF = 1e11;
const int P = 1e9 + 7, Q = 1e9 + 9;
const int base = 41;
const double PI = 3.141592653589793238;
const double eps = 1e-7;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int arr[100000];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
int d = gcd(arr[0], arr[1]);
for (int i = 2; i < n; ++i) {
d = gcd(d, arr[i]);
}
for (int i = 0; i < n; ++i) {
arr[i] /= d;
}
for (int i = 0; i < n; ++i) {
while (arr[i] % 2 == 0) arr[i] /= 2;
while (arr[i] % 3 == 0) arr[i] /= 3;
if (arr[i] == 1)
continue;
else {
cout << "No\n";
return 0;
}
}
cout << "Yes\n";
return 0;
}
| 5
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, x, y, t;
cin >> n;
x = n * n;
for (j = 1; j <= n * n / 2; j++) {
printf("%d ", j);
printf("%d ", x);
x--;
if (j % (n / 2) == 0) puts("");
}
return 0;
}
| 2
|
#include <bits/stdc++.h>
using namespace std;
string s[105];
int a[105][105];
int rr[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int cc[] = {1, 1, 1, 0, -1, -1, -1, 0};
class Node {
public:
int r, c;
};
int main(void) {
int t, n, m, cs = 0, i, j, k, x, cnt, flag = 0, tmp;
cin >> n >> m;
Node u, v;
int ur, uc, vr, vc;
for (i = 0; i < n; i++) {
cin >> s[i];
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
ur = i;
uc = j;
cnt = 0;
for (k = 0; k < 8; k++) {
vr = ur + rr[k];
vc = uc + cc[k];
if (vr >= 0 && vc >= 0 && vr < n && vc < m) {
if (s[vr][vc] == '*') cnt++;
}
}
tmp = s[i][j] - '0';
if (s[i][j] == '.' && cnt == 0) continue;
if (tmp == cnt) continue;
if (s[i][j] == '*') continue;
flag = 1;
break;
}
if (flag) break;
}
if (flag)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 3
|
#include <bits/stdc++.h>
using namespace std;
string s;
int k;
set<pair<string, int> > ms;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> s >> k;
for (int i = 0; i < s.size(); i++) ms.insert({s.substr(i, 1), i + 1});
for (int i = 0; i < k - 1; i++) {
if (ms.empty()) {
cout << "No such line.";
return 0;
}
auto u = *ms.begin();
ms.erase(ms.begin());
if (u.second == s.size()) continue;
ms.insert({u.first + s[u.second], u.second + 1});
}
cout << ms.begin()->first;
}
| 13
|
#include <bits/stdc++.h>
using namespace std;
void read(long long &x) { scanf("%lld", &x); }
void read(long long &x, long long &y) { scanf("%lld%lld", &x, &y); }
void read(long long &x, long long &y, long long &z) {
scanf("%lld%lld%lld", &x, &y, &z);
}
void print(long long x) { printf("%lld ", x); }
void println(long long x) { printf("%lld\n", x); }
const long long N = 200100;
long long n, x, c[N], mn, mx, calc[N], ans;
set<long long> s;
int main() {
cin >> n;
for (long long i = 1; i <= n; i++) {
read(x);
c[x]++;
if (c[x] == 1)
calc[1]++, s.insert(1);
else {
calc[c[x] - 1]--;
if (calc[c[x] - 1] == 0) s.erase(c[x] - 1);
calc[c[x]]++;
s.insert(c[x]);
}
mn = *s.begin();
mx = *s.rbegin();
if (s.size() == 1 && (mn == 1 || mn == i)) ans = i;
if (s.size() != 2) continue;
if (mn == 1 && calc[1] == 1) ans = i;
if (mx == mn + 1 && calc[mx] == 1) ans = i;
}
println(ans);
}
| 7
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e2 + 15;
bool vis[maxn][maxn][8][6][32], mp[maxn][maxn];
int f[8][2] = {{0, 1}, {1, 1}, {1, 0}, {1, -1},
{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}},
a[32], N;
void DFS(int x, int y, int fa, int cut, int nl) {
if (vis[x][y][fa][cut][nl] || nl > N) return;
vis[x][y][fa][cut][nl] = 1;
mp[x][y] = 1;
if (cut)
DFS(x + f[fa][0], y + f[fa][1], fa, cut - 1, nl);
else {
DFS(x, y, (fa + 1) % 8, a[nl + 1], nl + 1);
DFS(x, y, (fa - 1 + 8) % 8, a[nl + 1], nl + 1);
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%d", &a[i]);
DFS(150, 150, 0, a[1] - 1, 1);
int ans = 0;
for (int i = 0; i <= 300; i++)
for (int j = 0; j <= 300; j++)
if (mp[i][j]) ans++;
printf("%d\n", ans);
return 0;
}
| 11
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
if (a == b) {
if (a % 2)
cout << -a << endl;
else
cout << a << endl;
} else if (a % 2) {
long long dis = b - a + 1;
if (dis % 2)
cout << 1 * dis / 2 - b << endl;
else
cout << 1 * dis / 2 << endl;
} else {
long long dis = b - a + 1;
if (dis % 2)
cout << -1 * dis / 2 + b << endl;
else
cout << -1 * dis / 2 << endl;
}
}
return 0;
}
| 1
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
void Dzielniki(int x, vector<int>& d) {
for (int i = 1; i * i <= x; ++i) {
if (x % i == 0) d.push_back(i);
if (x % (x / i) == 0 && i * i != x) d.push_back(x / i);
}
}
int Szukaj1(vector<int>& a, int x) {
int l = 0, p = a.size() - 1, s, r = -1;
while (l <= p) {
s = (l + p) / 2;
if (a[s] >= x) {
r = s;
p = s - 1;
} else
l = s + 1;
}
return r;
}
int Szukaj2(vector<int>& a, int x) {
int l = 0, p = a.size() - 1, s, r = -1;
while (l <= p) {
s = (l + p) / 2;
if (a[s] <= x) {
r = s;
l = s + 1;
} else
p = s - 1;
}
return r;
}
long long Pot(long long a, long long wyk) {
if (wyk == 1) return a;
if (wyk % 2 == 1) return a * Pot(a, wyk - 1) % MOD;
long long x = Pot(a, wyk / 2);
return x * x % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a.begin(), a.end());
long long odp = 0;
for (int i = 1; i <= a[n - 1]; ++i) {
vector<int> d;
Dzielniki(i, d);
sort(d.begin(), d.end());
d.push_back(a[n - 1] + 1);
long long il = 1;
for (int j = 0; j < (int)d.size() - 1; ++j) {
int L = Szukaj1(a, d[j]);
int P = Szukaj2(a, d[j + 1] - 1);
if (L <= P && L != -1 && P != -1 && a[L] >= d[j] && a[P] < d[j + 1]) {
long long w = P - L + 1;
long long roz = Pot(j + 1, w);
if (j < (int)d.size() - 2)
il = (il * roz) % MOD;
else {
long long roz_bez = Pot(j, w);
long long r = roz - roz_bez;
if (r < 0) r += MOD;
il = (il * r) % MOD;
}
}
}
odp = (odp + il) % MOD;
}
cout << odp;
return 0;
}
| 12
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
int a[n], pre[n];
cin >> a[0];
pre[0] = a[0];
int res = pre[0];
for (long long i = 1; i <= n - 1; i++) {
cin >> a[i];
pre[i] = pre[i - 1] ^ a[i];
res = max(res, pre[i]);
}
for (long long i = 1; i <= n - 1; i++) {
for (long long j = 0; j < i; j++) {
res = max(res, pre[i] ^ pre[j]);
}
}
cout << res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long test = 1;
while (test--) solve();
return 0;
}
| 3
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int l, r, a, b, k;
scanf("%d%d%d%d%d", &l, &r, &a, &b, &k);
for (int i = a; i <= b; ++i)
if (l <= 1LL * i * k && 1LL * i * k <= r) {
printf("YES\n");
return 0;
}
printf("NO\n");
return 0;
}
| 4
|
#include <bits/stdc++.h>
using namespace std;
const int DY[] = {-1, 0, 1, 0};
const int DX[] = {0, 1, 0, -1};
vector<int> G[31];
long long X[31], Y[31];
void dfs(int u, int dep, long long x, long long y, int prever, int predir) {
X[u] = x;
Y[u] = y;
assert(abs(x) < 1ll << 60);
assert(abs(y) < 1ll << 60);
int dir = 0;
long long dis = 1ll << (55 - dep);
for (auto &(v) : (G[u]))
if (v != prever) {
if (dir == predir) dir++;
assert(dir < 4);
long long nx = x + DX[dir] * dis;
long long ny = y + DY[dir] * dis;
dfs(v, dep + 1, nx, ny, u, (dir + 2) % 4);
dir++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
while (cin >> n) {
vector<int> deg(n);
for (int(i) = (0); (i) < (int)(n); ++(i)) G[i].clear();
for (int(i) = (0); (i) < (int)(n - 1); ++(i)) {
int u, v;
cin >> u >> v;
--u;
--v;
deg[u]++;
deg[v]++;
G[u].push_back(v);
G[v].push_back(u);
}
int root = -1, ma = 0;
for (int(i) = (0); (i) < (int)(n); ++(i)) (ma) = max((ma), (deg[i]));
auto nma = count((deg).begin(), (deg).end(), ma);
if (ma > 4) {
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
for (int(i) = (0); (i) < (int)(n); ++(i))
if (deg[i] == ma) root = i;
dfs(root, 0, 0, 0, -1, -1);
for (int(i) = (0); (i) < (int)(n); ++(i)) {
cout << X[i] << ' ' << Y[i] << endl;
}
}
}
| 12
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[7], sum = 0;
for (int i = 1; i <= 6; i++) {
cin >> a[i];
sum += a[i];
}
if (sum % 2 == 1) {
cout << "NO";
return 0;
}
sort(a + 1, a + 7);
for (int i = 1; i <= 4; i++) {
for (int j = i + 1; j <= 5; j++) {
for (int k = j + 1; k <= 6; k++)
if (a[i] + a[j] + a[k] == sum / 2) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
return 0;
}
| 2
|
#include <bits/stdc++.h>
using namespace std;
int a[100010], b[100010], n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
if (a[1] != b[1] || a[n] != b[n]) {
printf("No");
return 0;
}
for (int i = 1; i < n; ++i) a[i] = a[i + 1] - a[i], b[i] = b[i + 1] - b[i];
sort(a + 1, a + n);
sort(b + 1, b + n);
for (int i = 1; i < n; ++i)
if (a[i] != b[i]) {
printf("No");
return 0;
}
printf("Yes");
return 0;
}
| 14
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
const long long inf = 0x3f3f3f3f3f3f3f3fll;
using namespace std;
void d1(long long x) { cout << x << endl; }
void d2(long long x, long long y) { cout << x << " " << y << endl; }
void d3(long long x, long long y, long long z) {
cout << x << " " << y << " " << z << endl;
}
void d4(long long w, long long x, long long y, long long z) {
cout << w << " " << x << " " << y << " " << z << endl;
}
void da(long long a[], long long n) {
for (long long i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
}
void dv(vector<long long>& v) {
for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long n, m, i, maxheight = 0, currheight, maxcurrpossible;
cin >> n >> m;
long long d[m], h[m], x, y;
bool flag = false;
for (i = 0; i < m; i++) cin >> d[i] >> h[i];
maxheight = ((h[0] + d[0] - 1) > (h[m - 1] + n - d[m - 1])
? (h[0] + d[0] - 1)
: (h[m - 1] + n - d[m - 1]));
for (i = 0; i < m - 1; i++) {
maxcurrpossible =
((h[i]) < (h[i + 1]) ? (h[i]) : (h[i + 1])) + abs(d[i + 1] - d[i]);
if (maxcurrpossible >= ((h[i]) > (h[i + 1]) ? (h[i]) : (h[i + 1]))) {
y = d[i + 1] - d[i];
x = abs(h[i + 1] - h[i]);
y = y - x;
currheight = ((h[i]) > (h[i + 1]) ? (h[i]) : (h[i + 1])) + y / 2;
maxheight = ((currheight) > (maxheight) ? (currheight) : (maxheight));
} else {
flag = true;
break;
}
}
if (flag)
cout << "IMPOSSIBLE";
else
cout << maxheight;
return 0;
}
| 8
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n, a[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
vector<pair<int, int>> ans;
int cur = 0;
while (cur < n && a[cur] == 0) cur += 1;
if (cur == n) {
cout << "NO\n";
return 0;
}
ans.push_back({0, cur});
for (int cur1 = cur + 1; cur1 < n; ++cur1) {
if (a[cur1] == 0)
ans.back().second += 1;
else
ans.push_back({cur1, cur1});
}
cout << "YES\n" << ans.size() << '\n';
for (auto p : ans) cout << (p.first + 1) << ' ' << (p.second + 1) << '\n';
}
| 4
|
#include <bits/stdc++.h>
using namespace std;
const unsigned int mod = 1e7 + 7;
const long long int N = 1e5;
long long int lazy[400009], segtree[400009], y = 0, n, col[200009], cnt,
a[2009];
bool visit[1000] = {false}, vis[10009] = {false};
vector<long long int> v[1000];
int main() {
long long int l, n, m, k = 0, cnt, q = 0, p = 0, t = 0, z = 0, w = INT_MAX,
pre = 1e5, mid, dp[1000] = {0}, h[26] = {0}, t1, t2,
now, r = 0, i = 0, j = 0, flag = 0, temp = 0, e = -1e4,
o = 0;
string s1, s2, s3, s;
char a[100][100];
for (i = 0; i < 3; i++) {
cin >> s1 >> s2 >> s3;
a[i][0] = s1[0], a[i][1] = s1[1], a[i][2] = s1[2];
a[i][3] = s2[0], a[i][4] = s2[1], a[i][5] = s2[2];
a[i][6] = s3[0], a[i][7] = s3[1], a[i][8] = s3[2];
}
for (i = 3; i < 6; i++) {
cin >> s1 >> s2 >> s3;
a[i][0] = s1[0], a[i][1] = s1[1], a[i][2] = s1[2];
a[i][3] = s2[0], a[i][4] = s2[1], a[i][5] = s2[2];
a[i][6] = s3[0], a[i][7] = s3[1], a[i][8] = s3[2];
}
for (i = 6; i < 9; i++) {
cin >> s1 >> s2 >> s3;
a[i][0] = s1[0], a[i][1] = s1[1], a[i][2] = s1[2];
a[i][3] = s2[0], a[i][4] = s2[1], a[i][5] = s2[2];
a[i][6] = s3[0], a[i][7] = s3[1], a[i][8] = s3[2];
}
cin >> n >> m;
t = m / 3;
n--, m--;
if (n == 0 || n == 3 || n == 6) {
if (m == 0 || m == 3 || m == 6) {
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
if (m == 1 || m == 4 || m == 7) {
for (i = 0; i < 3; i++) {
for (j = 3; j < 6; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
if (m == 2 || m == 5 || m == 8) {
for (i = 0; i < 3; i++) {
for (j = 6; j < 9; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
}
if (n == 1 || n == 4 || n == 7) {
if (m == 0 || m == 3 || m == 6) {
for (i = 3; i < 6; i++) {
for (j = 0; j < 3; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
if (m == 1 || m == 4 || m == 7) {
for (i = 3; i < 6; i++) {
for (j = 3; j < 6; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
if (m == 2 || m == 5 || m == 8) {
for (i = 3; i < 6; i++) {
for (j = 6; j < 9; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
}
if (n == 2 || n == 5 || n == 8) {
if (m == 0 || m == 3 || m == 6) {
for (i = 6; i < 9; i++) {
for (j = 0; j < 3; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
if (m == 1 || m == 4 || m == 7) {
for (i = 6; i < 9; i++) {
for (j = 3; j < 6; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
if (m == 2 || m == 5 || m == 8) {
for (i = 6; i < 9; i++) {
for (j = 6; j < 9; j++) {
if (a[i][j] == '.') a[i][j] = '!', flag++;
}
}
}
}
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (flag > 0)
cout << a[i][j];
else {
if (a[i][j] == '.')
cout << "!";
else
cout << a[i][j];
}
temp++;
if (temp == 3) cout << " ", temp = 0;
}
cout << endl;
o++;
if (o == 3) cout << endl, o = 0;
}
}
| 6
|
#include <bits/stdc++.h>
using namespace std;
int x;
bool dig[10];
inline bool is(int num) {
while (num > 0) {
if (dig[num % 10]) return true;
num /= 10;
}
return false;
}
int main() {
cin >> x;
int n = x;
memset(dig, 0, sizeof dig);
while (n > 0) {
dig[n % 10] = true;
n /= 10;
}
int ans = 0;
for (int i = 1; i <= sqrt(x); i++) {
if (x % i != 0) continue;
if (is(i)) ans++;
if (i != x / i && is(x / i)) ans++;
}
cout << ans << endl;
return 0;
}
| 5
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void chkmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
void chkmin(T &a, T b) {
if (a > b) a = b;
}
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, kk;
vector<int> v[105];
int dp[105][25][25];
void dfs(int y, int p = 0) {
int ep[25][25];
memset(ep, 0, sizeof ep);
dp[y][0][0] = 1;
dp[y][1][kk] = 1;
for (int i = 0; i < v[y].size(); i++) {
int x = v[y][i];
if (x == p) continue;
dfs(x, y);
memset(ep, 0, sizeof ep);
for (int m1 = 0; m1 <= kk; m1++) {
for (int m2 = 0; m2 <= kk; m2++) {
for (int n1 = 0; n1 <= kk; n1++) {
for (int n2 = 0; n2 <= kk; n2++) {
int nn = 0, mm = inf;
if (n1 + m2 > kk) chkmax(nn, n1);
if (n2 + m1 > kk) chkmax(nn, n2 + 1);
chkmin(mm, m1);
chkmin(mm, m2 + 1);
if (nn <= kk && mm <= kk)
ep[nn][mm] =
(ep[nn][mm] + 1ll * dp[y][n1][m1] * dp[x][n2][m2]) % mod;
}
}
}
}
for (int i = 0; i <= kk; i++)
for (int j = 0; j <= kk; j++) dp[y][i][j] = ep[i][j];
}
}
int main() {
scanf("%d %d", &n, &kk);
int x, y;
for (int i = 1; i < n; i++) {
scanf("%d %d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
int ans = 0;
for (int i = 0; i <= kk; i++) ans = (ans + dp[1][0][i]) % mod;
printf("%d\n", ans);
return 0;
}
| 17
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long minf = -(1e18);
pair<int, int> arr[200001];
bool sortx(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first)
return a.second < b.second;
else
return a.first < b.first;
}
bool sorty(pair<int, int> a, pair<int, int> b) {
if (a.second == b.second)
return a.first < b.first;
else
return a.second < b.second;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long n;
cin >> n;
long long x, y;
arr[0] = make_pair(-10000000000, -10000000000);
for (int i = 1; i <= n; i++) {
cin >> x >> y;
arr[i] = make_pair(x, y);
}
sort(arr + 1, arr + n + 1, sortx);
long long prev = arr[1].first;
long long ans = 1;
long long sum = 0;
for (int i = 2; i <= n; i++) {
if (arr[i].first == prev)
ans++;
else {
sum += (ans) * (ans - 1) / 2;
prev = arr[i].first;
ans = 1;
}
}
sum += (ans) * (ans - 1) / 2;
sort(arr + 1, arr + n + 1, sorty);
prev = arr[1].second;
ans = 1;
long long sol = 1;
long long sumo = 0;
long long xe = arr[1].first;
long long ye = arr[1].second;
for (int i = 2; i <= n; i++) {
if (arr[i].first == xe && arr[i].second == ye) {
sol++;
} else {
xe = arr[i].first;
ye = arr[i].second;
sumo += (sol) * (sol - 1) / 2;
sol = 1;
}
if (arr[i].second == prev)
ans++;
else {
sum += (ans) * (ans - 1) / 2;
prev = arr[i].second;
ans = 1;
}
}
sum += (ans) * (ans - 1) / 2;
sumo += (sol) * (sol - 1) / 2;
cout << sum - sumo;
return 0;
}
| 6
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[5001], b[5001];
int dp[5002][5002] = {0};
int ans = 0;
void solve() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1]) {
dp[i][j] = max(dp[i - 1][j - 1] + 2, 0);
}
dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]) - 1, dp[i][j]);
ans = max(ans, dp[i][j]);
}
}
cout << ans;
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
cin >> a >> b;
solve();
return 0;
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
long long Max(long long a, long long b) { return a > b ? a : b; }
long long Min(long long a, long long b) { return a < b ? a : b; }
int main() {
int d, l, v1, v2;
scanf("%d%d%d%d", &d, &l, &v1, &v2);
printf("%lf\n", double(l - d) / double(v1 + v2));
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, ans = 0;
cin >> a >> b >> c;
c -= c % 4;
b -= b % 2;
if (a * 2 <= b and a * 4 <= c) ans = a + a * 2 + a * 4;
if (b * 2 <= c and b / 2 <= a)
if (b + b * 2 + b / 2 > ans) ans = b + b * 2 + b / 2;
if (c / 4 <= a and c / 2 <= b)
if (c + c / 2 + c / 4 > ans) ans = c + c / 2 + c / 4;
cout << ans;
}
| 0
|
#include <bits/stdc++.h>
const long long N = 1e6;
using namespace std;
int query(string s) {
cout << s << endl;
fflush(stdout);
int n;
cin >> n;
return n;
}
string gena(int n) {
string s;
for (int i = 0; i < n; i++) s += 'a';
return s;
}
string genb(int n) {
string s;
for (int i = 0; i < n; i++) s += 'b';
return s;
}
int main() {
int n = query("a") + 1;
if (n == 1) return 0;
if (n == 301) {
query(genb(n - 1));
return 0;
}
int cntb = query(gena(n));
if (cntb == 0) return 0;
if (cntb == n) {
query(genb(n - 1));
return 0;
}
int cnta = n - cntb;
string cur;
for (int i = 0; i < cnta; i++) {
while (true) {
int ans = query(cur + "b" + gena(cnta - i));
if (ans <= 0) return 0;
if (ans != n - (cur.size() + 1 + cnta - i)) break;
cur += "b";
}
cur += "a";
}
int len = n - cur.size();
query(cur + genb(len));
}
| 15
|
#include <bits/stdc++.h>
using namespace std;
long long powe(long long a, long long b) {
long long M = 1000000007;
if (b == 0) return 1;
if (b % 2 == 0) {
return powe((a * a) % M, b / 2);
} else {
return (a * (powe(((a * a) % M), (b - 1) / 2)) % M);
}
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
const int mod = 1e9 + 7;
bool f(int x, int y) { return x > y; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
long long sum = 0;
for (int i = 0; i < n; ++i) {
long long a;
cin >> a;
sum += a;
}
n = n + 1;
int ans = 0;
for (int i = 1; i <= 5; ++i) {
if ((sum + i) % n != 1) ans++;
}
cout << ans;
return 0;
}
| 2
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
inline char gc() {
return getchar();
static const int maxs = 1 << 16;
static char buf[maxs], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, maxs, stdin), p1 == p2)
? EOF
: *p1++;
}
inline long long rd() {
long long x = 0;
char c = gc();
bool neg = 0;
while (c < '0' || c > '9') {
if (c == '-') neg = 1;
c = gc();
}
while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = gc();
return neg ? (~x + 1) : x;
}
struct Node {
long long x, y, v;
} p[maxn];
int N;
long long f[maxn];
int q[maxn], hd = 1, tl;
inline bool cmp(Node a, Node b) { return a.x < b.x; }
inline double getk(int a, int b) {
return 1.0 * (f[a] - f[b]) / (p[a].x - p[b].x);
}
int main() {
int i, j, k;
N = rd();
for (i = 1; i <= N; i++) {
p[i].x = rd(), p[i].y = rd(), p[i].v = rd();
}
sort(p + 1, p + N + 1, cmp);
q[hd = tl = 1] = 0;
for (i = 1; i <= N; i++) {
while (hd < tl && getk(q[hd], q[hd + 1]) > p[i].y) hd++;
f[i] = (p[i].x - p[q[hd]].x) * p[i].y - p[i].v + f[q[hd]];
while (hd < tl && getk(q[tl], q[tl - 1]) < getk(q[tl], i)) tl--;
q[++tl] = i;
}
long long ans = 0;
for (i = 1; i <= N; i++) ans = max(ans, f[i]);
printf("%I64d\n", ans);
return 0;
}
| 16
|
#include <bits/stdc++.h>
using namespace std;
long long factorials[200000 + 453];
long long inverses[200000 + 453];
long long mod = 1000000007;
long long add(long long a, long long b) {
return ((a % mod) + (b % mod)) % mod;
}
long long sub(long long a, long long b) {
return ((a % mod) - (b % mod) + mod) % mod;
}
long long mul(long long a, long long b) {
long long mod = 1000000007;
return ((a % mod) * (b % mod)) % mod;
}
long long poww(long long a, long long k) {
if (k == 0) {
return 1;
}
long long left = poww(a, k / 2);
left = mul(left, left);
if (k % 2 == 1) {
left = mul(left, a);
}
return left;
}
long long binv(long long a) { return poww(a, mod - 2); }
long long divv(long long a, long long b) {
long long bb = binv(b);
return mul(a, bb);
}
long long cee(long long n, long long r) {
long long num = factorials[n];
long long deno1, deno2, deno;
deno1 = inverses[r];
deno2 = inverses[n - r];
deno = mul(deno1, deno2);
return mul(num, deno);
}
int main() {
ios::sync_with_stdio(false);
int n;
long long a[200000 + 453];
factorials[0] = 1;
factorials[1] = 1;
inverses[0] = binv(1);
inverses[1] = binv(1);
for (long long i = 2; i <= 100000; i++) {
long long res = mul(factorials[i - 1], i);
factorials[i] = res;
inverses[i] = binv(res);
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = 0;
if (n % 4 == 1) {
long long count = 0;
for (int i = 0; i < n; i = i + 2) {
long long bc = (n) / 2;
long long x = cee(bc, count);
long long addd = mul(x, a[i]);
ans = add(ans, addd);
count++;
if (count > bc) {
break;
}
}
} else if (n % 4 == 2) {
long long count = 0;
for (int i = 0; i < n; i = i + 2) {
long long bc = (n - 2) / 2;
long long x = cee(bc, count);
long long addd = mul(x, add(a[i], a[i + 1]));
ans = add(ans, addd);
count++;
}
} else if (n % 4 == 3) {
vector<long long> ref[2];
long long cur_combination = (n - 2) / 4 + 1;
long long count = 0;
count = 0;
for (int i = 1; i < n; i = i + 2) {
long long bc = (n - 2) / 2;
;
long long x = cee(bc, count);
ref[1].push_back(x);
ref[1].push_back(x);
count++;
if (count > bc) break;
}
for (int i = 1; i <= 10; i++) {
ref[0].push_back(0);
ref[1].push_back(0);
}
count = 0;
for (long long i = 0; i < n; i++) {
long long multi;
if (i == 0) {
multi = 1;
ans = add(ans, a[i]);
continue;
}
if (i % 2 == 1) {
multi = add(ref[1][i], ref[1][i - 1]);
} else {
multi = sub(ref[1][i], ref[1][i - 1]);
}
long long addd = mul(multi, a[i]);
ans = add(addd, ans);
}
} else if (n % 4 == 0) {
long long count = 0;
long long bc = (n - 2) / 2;
long long x;
for (int i = 0; i < n; i = i + 2) {
x = cee(bc, count);
long long add1 = mul(x, a[i]);
long long add2 = mul(x, a[i + 1]);
ans = add(ans, add1);
ans = sub(ans, add2);
count++;
if (count > bc) break;
}
}
if (ans < 0) {
ans += mod;
}
cout << ans;
return 0;
}
| 14
|
#include <bits/stdc++.h>
int A, B, F;
char P[22][22];
bool chk(int a, int b, int c, int d, int p, int q, int r, int s) {
int i, j, f;
f = 0;
for (i = a; i <= b; i++) {
for (j = c; j <= d; j++) {
if (P[i][j] != P[i - a + p][j - c + r]) {
f = 1;
break;
}
}
if (f) break;
}
if (f == 0) return true;
f = 0;
for (i = a; i <= b; i++) {
for (j = c; j <= d; j++) {
if (P[i][j] != P[q + a - i][s + c - j]) {
f = 1;
break;
}
}
if (f) break;
}
if (f == 0) return true;
if (b - a == d - c) {
f = 0;
for (i = a; i <= b; i++) {
for (j = c; j <= d; j++) {
if (P[i][j] != P[j - c + p][s + a - i]) {
f = 1;
break;
}
}
if (f) break;
}
if (f == 0) return true;
f = 0;
for (i = a; i <= b; i++) {
for (j = c; j <= d; j++) {
if (P[i][j] != P[q + c - j][i - a + r]) {
f = 1;
break;
}
}
if (f) break;
}
if (f == 0) return true;
}
return false;
}
int main() {
int i, j, k, l, o, p, a, b, c, m;
c = 0;
m = 0x7fffffff;
scanf("%d %d", &A, &B);
for (i = 0; i < A; i++) scanf("%s", P[i]);
for (i = 1; i <= A; i++) {
if (A % i == 0) {
for (j = 1; j <= B; j++) {
if (B % j == 0) {
F = 0;
for (k = 0; k < A / i; k++) {
for (l = 0; l < B / j; l++) {
for (o = k; o < A / i; o++) {
for (p = 0; p < B / j; p++) {
if (k == o && l == p) continue;
if (chk(k * i, k * i + i - 1, l * j, l * j + j - 1, o * i,
o * i + i - 1, p * j, p * j + j - 1)) {
F = 1;
break;
}
}
if (F) break;
}
if (F) break;
}
if (F) break;
}
if (F == 0) {
if (m > i * j) {
m = i * j;
a = i;
b = j;
}
c++;
}
}
}
}
}
printf("%d\n%d %d", c, a, b);
return 0;
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned int a, b, x;
cin >> a >> b;
x = min(a, b);
unsigned long int fact = 1;
while (x > 1) {
fact = fact * x;
x--;
}
cout << fact;
return (0);
}
| 0
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void checkmin(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, const T &b) {
if (b > a) a = b;
}
const long long INF = 1e18 + 10;
const int MAX_N = 3e5 + 10;
long long H[MAX_N], Hash[MAX_N];
int n, a[MAX_N];
mt19937 fuck((int)time(NULL));
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
H[i] = uniform_int_distribution<long long>(1, INF)(fuck);
for (int i = 0; i < n; i++)
Hash[i] = H[a[i]], Hash[i] ^= i - 1 >= 0 ? Hash[i - 1] : 0;
for (int i = 2; i <= n; i++) H[i] ^= H[i - 1];
int ans = 0;
for (int i = 0; i < n; i++)
if (a[i] == 1) {
int mx = a[i];
ans++;
for (int j = i + 1; j < n; j++) {
mx = max(mx, a[j]);
if (a[j] == 1) break;
if (j - mx + 1 < 0) continue;
long long now = j - mx < 0 ? Hash[j] : Hash[j] ^ Hash[j - mx];
ans += now == H[mx];
}
mx = a[i];
for (int j = i - 1; j >= 0; j--) {
mx = max(mx, a[j]);
if (a[j] == 1) break;
if (j + mx - 1 >= n) continue;
long long now =
j - 1 < 0 ? Hash[j + mx - 1] : Hash[j + mx - 1] ^ Hash[j - 1];
ans += now == H[mx];
}
}
printf("%d\n", ans);
return 0;
}
| 17
|
#include <bits/stdc++.h>
using namespace std;
void build(unsigned int *A, vector<unsigned int> tree[], unsigned int index,
unsigned int s, unsigned int e) {
if (s == e)
tree[index].push_back(A[s]);
else {
unsigned int mid = (s + e) / 2;
build(A, tree, 2 * index, s, mid);
build(A, tree, 2 * index + 1, mid + 1, e);
unsigned int l = 0;
while (l != tree[2 * index].size())
tree[index].push_back(tree[2 * index][l++]);
l = 0;
while (l != tree[2 * index + 1].size())
tree[index].push_back(tree[2 * index + 1][l++]);
sort(tree[index].begin(), tree[index].end());
}
}
unsigned int query(vector<unsigned int> tree[], unsigned int index,
unsigned int s, unsigned int e, unsigned int l,
unsigned int r, unsigned int val) {
if (r < s || e < l)
return 0;
else if (l <= s && e <= r) {
unsigned int pos =
lower_bound(tree[index].begin(), tree[index].end(), val) -
tree[index].begin();
return pos;
} else {
unsigned int mid = (s + e) / 2;
unsigned int left = query(tree, 2 * index, s, mid, l, r, val);
unsigned int right = query(tree, 2 * index + 1, mid + 1, e, l, r, val);
return left + right;
}
}
void solve(unsigned int *C, unsigned int *D, unsigned int n) {
unsigned int l, r, val;
vector<unsigned int> tree[4 * n + 1];
build(D, tree, 1, 1, n);
long long int s = 0;
for (unsigned int i = 1; i < n; i++)
s += query(tree, 1, 1, n, i + 1, n, C[i]);
cout << s << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
unsigned int n;
cin >> n;
unsigned int A[n + 1];
unsigned int y;
map<unsigned int, unsigned int> m;
unsigned int C[n + 1], D[n + 1];
for (unsigned int i = 1; i <= n; i++) {
cin >> A[i];
auto it = m.find(A[i]);
if (it != m.end()) {
y = it->second;
y++;
m.erase(it);
m.insert(make_pair(A[i], y));
} else {
y = 1;
m.insert(make_pair(A[i], y));
}
C[i] = y;
}
for (unsigned int i = 1; i <= n; i++) D[i] = m.find(A[i])->second - C[i] + 1;
solve(C, D, n);
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
int a[5010];
long long sum[5010];
int main() {
int n;
scanf("%d", &n);
int i, j, k;
for (i = 0; i < n; i++) scanf("%d", &a[i]);
sum[0] = (long long)a[0];
for (i = 1; i < n; i++) sum[i] = sum[i - 1] + (long long)a[i];
sum[n] = sum[n - 1];
int d0, d1, d2, dd0, dd2;
long long ans = -1e18;
for (i = 0; i <= n; i++) {
long long mx1 = -1e18;
long long mx2 = -1e18;
for (j = 0; j <= i; j++) {
if (sum[j] - a[j] - (sum[i] - sum[j] + a[j] - a[i]) > mx1) {
mx1 = sum[j] - a[j] - (sum[i] - sum[j] + a[j] - a[i]);
dd0 = j;
}
}
for (k = i; k <= n; k++) {
if (sum[k] - sum[i] + a[i] - a[k] - (sum[n] - sum[k] + a[k] - a[n]) >
mx2) {
mx2 = sum[k] - sum[i] + a[i] - a[k] - (sum[n] - sum[k] + a[k] - a[n]);
dd2 = k;
}
}
if (mx1 + mx2 > ans) {
ans = mx1 + mx2;
d1 = i;
d0 = dd0;
d2 = dd2;
}
}
cout << d0 << " " << d1 << " " << d2 << endl;
return 0;
}
| 10
|
#include <bits/stdc++.h>
using namespace std;
vector<string> inivector;
string init;
int main() {
cin >> init;
int stringend = init.size() - 1;
for (int i = 0; i <= stringend; ++i) {
string tmp = "";
for (int j = i; j <= stringend; ++j) {
tmp += init[j];
inivector.push_back(tmp);
}
}
int ans = 0;
for (int i = 0; i < inivector.size() - 1; ++i) {
for (int j = i + 1; j < inivector.size(); ++j) {
if (inivector[i] == inivector[j]) {
int leng = inivector[i].length();
ans = max(ans, leng);
}
}
}
cout << ans << '\n';
}
| 4
|
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
long long int t;
cin >> t;
while (t--) {
long long int a, b;
cin >> a >> b;
long long int c = b % a;
long long int d = b / a;
cout << ((d + 1) * (d + 1) * c) + ((d) * (d) * (a - c)) << "\n";
}
return 0;
}
| 2
|
#include <bits/stdc++.h>
namespace io {
int F() {
int F = 1, n = 0;
char ch;
while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
;
ch == '-' ? F = 0 : n = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') n = n * 10 + ch - '0';
return F ? n : -n;
}
long long G() {
long long F = 1, n = 0;
char ch;
while ((ch = getchar()) != '-' && (ch < '0' || ch > '9'))
;
ch == '-' ? F = 0 : n = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') n = n * 10 + ch - '0';
return F ? n : -n;
}
} // namespace io
long long siz[1111111];
int fa[1111111];
int cnt[1111111];
int f[1111111];
long long gcd(long long x, long long y) {
long long r;
while (r = x % y) x = y, y = r;
return y;
}
int main() {
int n = io::F();
for (register int i = 1; i <= n; ++i) siz[i] = io::F();
for (register int i = 2; i <= n; ++i) fa[i] = io::F();
for (register int i = n; i > 1; --i) siz[fa[i]] += siz[i];
for (register int i = 1; i <= n; ++i) {
long long g = gcd(siz[i], siz[1]);
if (siz[1] / g <= n) cnt[siz[1] / g]++;
}
for (register int i = n; i; --i)
for (register int j = i + i; j <= n; j += i) cnt[j] += cnt[i];
f[1] = 1;
for (register int i = 1; i <= n; ++i)
for (register int j = i + i; j <= n; j += i)
if (cnt[j] == j) f[j] = (f[j] + f[i]) % 1000000007;
int ans = 0;
for (register int i = 1; i <= n; ++i) ans = (ans + f[i]) % 1000000007;
printf("%d\n", ans);
return 0;
}
| 19
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1& arg1) {
cerr << name << ": " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1& arg1, Args&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " /";
__f(comma + 1, args...);
}
template <class T>
bool mini(T& a, T b) {
return a > b ? (a = b, 1) : 0;
}
template <class T>
bool maxi(T& a, T b) {
return a < b ? (a = b, 1) : 0;
}
const int INF = 1e9 + 7;
const int N = 3e5 + 3;
int n, a[N], ans[N];
deque<int> mx;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = (1), _b = (n); i <= (_b); ++i) {
cin >> a[i];
a[n + i] = a[2 * n + i] = a[i];
}
int l = 1, r = 1;
while (l <= n && r <= 3 * n) {
while (!mx.empty() && a[mx.back()] <= a[r]) mx.pop_back();
mx.push_back(r);
while (!mx.empty() && a[mx.front()] > a[r + 1] * 2) {
ans[l] = r - l + 1;
if (ans[l] >= 2 * n) {
for (int i = (1), _b = (n); i <= (_b); ++i) cout << "-1 ";
return 0;
}
++l;
while (!mx.empty() && mx.front() < l) mx.pop_front();
}
++r;
}
for (int i = (1), _b = (n); i <= (_b); ++i) cout << ans[i] << ' ';
return 0;
}
| 12
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int MAXN = 3e5 + 10;
long long ex[MAXN], ey[MAXN], w[MAXN], str[MAXN], wtr[MAXN], n, m, k, d[MAXN];
vector<int> ng[MAXN];
bool updTr[MAXN];
int other(int id, int x) { return ex[id] + ey[id] - x; }
set<pair<long long, int> > s;
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> ex[i] >> ey[i] >> w[i];
ex[i]--;
ey[i]--;
ng[ex[i]].push_back(i);
ng[ey[i]].push_back(i);
}
for (int i = 0; i < k; i++) cin >> str[i] >> wtr[i], str[i]--;
for (int i = 0; i < n; i++) d[i] = INF;
d[0] = 0;
s.insert(pair<long long, int>(0, 0));
for (int i = 0; i < k; i++)
d[str[i]] = min((long long)wtr[i], d[str[i]]), updTr[str[i]] = true;
for (int i = 0; i < k; i++) {
s.insert(pair<long long, int>(d[str[i]], str[i]));
}
while (!s.empty()) {
int cur = s.begin()->second;
s.erase(s.begin());
for (int i = 0; i < ng[cur].size(); i++) {
int edgeId = ng[cur][i];
int u = other(edgeId, cur);
if (d[u] >= d[cur] + w[edgeId]) {
s.erase(pair<long long, int>(d[u], u));
d[u] = d[cur] + w[edgeId];
s.insert(pair<long long, int>(d[u], u));
updTr[u] = false;
}
}
}
int ans = 0;
for (int i = 1; i < n; i++) {
if (updTr[i]) {
ans++;
}
}
cout << k - ans << endl;
}
| 12
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, k, n, rev = 0, r, c1 = 0, c2 = 0, x, z, a[1000], ans = 0;
string s;
set<char> ch;
cin >> n >> s;
for (i = 0; i < n; i++) {
if (s[i] > 96) {
ch.insert(s[i]);
} else {
x = ch.size();
ans = max(ans, x);
ch.clear();
}
}
x = ch.size();
if (x != 0) {
ans = max(ans, x);
}
cout << ans;
return 0;
}
| 2
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.