solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long int> v;
long long int ans = 0;
for (int i = 0; i < n; i++) {
if (i == 0 && a[i] == 1 && a[i + 1] == 2) {
ans++;
} else if (i == n - 1 && a[i] == 1000 && a[i - 1] == 999) {
ans++;
} else if (a[i + 1] - a[i] == 1 && a[i] - a[i - 1] == 1) {
ans++;
} else {
v.push_back(ans);
ans = 0;
}
}
v.push_back(ans);
cout << *max_element(v.begin(), v.end());
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
long long ans = 0;
for (int i = (0); i < (n); ++i) {
long long t, T, x, cost;
cin >> t >> T >> x >> cost;
if (t >= T) {
ans += cost + m * x;
continue;
}
long long aux1 = cost;
if (m > (T - t)) aux1 += m * x;
long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;
aux2 *= cost;
ans += min(aux1, aux2);
}
cout << ans << endl;
return 0;
}
| 11 |
#include<bits/stdc++.h>
using namespace std;
bool check(int x,int y,int z)
{
if((x>=y&&y>=z)||(x<=y&&y<=z))
return true;
else
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n+10];
for(int i=1;i<=n;i++)
cin>>a[i];
if(n==1||n==2)
{
cout<<2*n-1<<endl;
}
else {
int sum=2*n-1;
for(int i=1;i<=n-2;i++)
{
if(((a[i+2]-a[i+1])>=0&&(a[i+1]-a[i])>=0)||((a[i+2]-a[i+1])<=0&&(a[i+1]-a[i])<=0))
continue;
else
sum++;
}
int fg=0;
for(int i=1;i<=n-3;i++)
{
fg=0;
if(check(a[i],a[i+1],a[i+2]))
fg=1;
if(check(a[i],a[i+1],a[i+3]))
fg=1;
if(check(a[i+1],a[i+2],a[i+3]))
fg=1;
if(check(a[i],a[i+2],a[i+3]))
fg=1;
if(fg==0)
sum++;
}
cout<<sum<<endl;
}
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int h[maxn], preMax[maxn], suffMin[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &h[i]);
preMax[0] = 0;
suffMin[n + 1] = 1e9 + 10;
for (int i = 1; i <= n; i++) {
preMax[i] = max(preMax[i - 1], h[i]);
}
for (int i = n; i >= 1; i--) {
suffMin[i] = min(suffMin[i + 1], h[i]);
}
int ret = 0;
for (int i = 1; i <= n; i++) {
if (preMax[i] <= suffMin[i + 1]) {
ret++;
}
}
cout << ret << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int l, x, y;
cin >> l >> x >> y;
if ((x == l / 2 + 1 && y == l / 2 + 1) || (x == l / 2 && y == l / 2) ||
(x == l / 2 + 1 && y == l / 2) || (x == l / 2 && y == l / 2 + 1))
cout << "NO" << endl;
else
cout << "YES" << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void rd(T &ret) {
ret = 0;
bool ok = 0, u = 0;
for (;;) {
int c = getchar();
if (c >= '0' && c <= '9')
ret = (ret << 3) + (ret << 1) + c - '0', ok = 1;
else if (c == '-')
u = 1;
else if (ok) {
if (u) ret *= -1;
return;
}
}
}
long long pow_mod(long long p, long long n, long long mod) {
long long ret = 1;
for (; n; n >>= 1) {
if (n & 1) ret = ret * p % mod;
p = p * p % mod;
}
return ret;
}
template <class T>
bool chmin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool chmax(T &a, const T &b) {
return b > a ? a = b, 1 : 0;
}
const int Max_N = (int)6e5 + 9;
struct DS {
set<pair<long long, pair<int, int> > > has;
void init() { has.clear(); }
void erase(long long c, int l, int r) {
has.erase(pair<long long, pair<int, int> >(-c, pair<int, int>(l, r)));
}
void insert(long long c, int l, int r) {
has.insert(pair<long long, pair<int, int> >(-c, pair<int, int>(l, r)));
}
long long get_max() {
if (has.empty()) return 0;
return -(has.begin()->first);
}
} ds;
struct RMQ {
int mx[Max_N][30], lg2[Max_N];
void init(int n, int *a) {
for (int i = 0; i < n; ++i) {
mx[i][0] = a[i];
lg2[i] = (!i ? -1 : lg2[i >> 1] + 1);
}
lg2[n] = lg2[n >> 1] + 1;
for (int j = 1, l = 2; l <= n; ++j, l <<= 1) {
for (int i = 0; i + l - 1 < n; ++i)
mx[i][j] = std::min(mx[i][j - 1], mx[i + (l >> 1)][j - 1]);
}
}
int query(int a, int b) {
int k = lg2[b - a + 1];
return std::min(mx[a][k], mx[b - (1 << k) + 1][k]);
}
};
struct SA {
int n;
int sa[Max_N];
int rank[Max_N];
int h[Max_N];
int wa[Max_N], wb[Max_N], wv[Max_N], wt[Max_N];
RMQ rmq;
int cmp(int *r, int a, int b, int l) {
return r[a] == r[b] && r[a + l] == r[b + l];
}
void init(int *r, int n, int m) {
r[n] = 0;
this->n = n++;
int i, j, p, *first = wa, *second = wb, *t;
for (i = 0; i < m; ++i) wt[i] = 0;
for (i = 0; i < n; ++i) wt[first[i] = r[i]]++;
for (i = 1; i < m; ++i) wt[i] += wt[i - 1];
for (i = n - 1; i >= 0; --i) sa[--wt[first[i]]] = i;
for (j = 1, p = 1; p < n; j *= 2, m = p) {
for (p = 0, i = n - j; i < n; i++) second[p++] = i;
for (i = 0; i < n; ++i)
if (sa[i] >= j) second[p++] = sa[i] - j;
for (i = 0; i < n; ++i) wv[i] = first[second[i]];
for (i = 0; i < m; ++i) wt[i] = 0;
for (i = 0; i < n; ++i) wt[wv[i]]++;
for (i = 1; i < m; ++i) wt[i] += wt[i - 1];
for (i = n - 1; i >= 0; --i) sa[--wt[wv[i]]] = second[i];
for (t = first, first = second, second = t, p = 1, first[sa[0]] = 0,
i = 1;
i < n; i++)
first[sa[i]] = cmp(second, sa[i - 1], sa[i], j) ? p - 1 : p++;
}
calhei(r);
return;
}
void calhei(int *r) {
int i, j, k = 0;
for (i = 1; i <= n; ++i) rank[sa[i]] = i;
for (i = 0; i < n; h[rank[i++]] = k)
for (k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++)
;
for (int i = 0; i < n; ++i) {
sa[i] = sa[i + 1];
h[i] = h[i + 1];
rank[i]--;
}
rmq.init(n, h);
return;
}
int lcp(int a, int b) {
if (a == b) return n - a;
a = rank[a];
b = rank[b];
if (a > b) std::swap(a, b);
return rmq.query(a + 1, b);
}
} sa;
long long ans;
int L;
pair<int, int> bn[Max_N];
int lens[Max_N], an[Max_N], cost[Max_N], suffix_cost[Max_N], right___t[Max_N];
int len_per_suffix[Max_N];
long long suffix_cost_pre_sum[Max_N];
char s[Max_N];
int get_len(int p) { return len_per_suffix[sa.sa[p]]; }
long long get_cost_sum(int l, int r) {
if (l) return suffix_cost_pre_sum[r] - suffix_cost_pre_sum[l - 1];
return suffix_cost_pre_sum[r];
}
void erase(int l, int r, set<pair<int, int> > &pos) {
pos.erase(pair<int, int>(l, r));
ds.erase(get_cost_sum(l, r), l, r);
}
void insert(int l, int r, set<pair<int, int> > &pos) {
if (l == r) {
bool flag = 1;
int leng = get_len(l);
if (l && leng == sa.h[l]) flag = 0;
if (l < L - 1 && leng == sa.h[l + 1]) flag = 0;
if (flag) {
chmax(ans, 1ll * leng * suffix_cost[l]);
}
} else {
pos.insert(pair<int, int>(l, r));
ds.insert(get_cost_sum(l, r), l, r);
}
}
void partition(int l, int r, int m, set<pair<int, int> > &pos) {
erase(l, r, pos);
insert(l, m - 1, pos);
insert(m, r, pos);
}
int main() {
int n;
cin >> n;
L = 0;
bool test_flag = true;
for (int i = (0); i < (n); i++) {
scanf("%s", s);
if (!(s[0] == 'a' && s[1] == 'a' && s[2] == 'a' && s[3] == 'a' &&
s[4] == 'a'))
test_flag = false;
int l = strlen(s);
lens[i] = l;
for (int j = (0); j < (l); j++) {
an[L + j] = s[j] - 'a' + 1;
len_per_suffix[L + j] = l - j;
}
an[L + l] = 27 + i;
len_per_suffix[L + l] = 0;
L += l + 1;
}
ans = 0;
long long cost_sum = 0;
for (int i = (0); i < (n); i++) {
scanf("%d", cost + i);
cost_sum += cost[i];
}
sa.init(an, L, 27 + n);
for (int idx = 0, i = 0; i < n; ++i) {
for (int j = (0); j < (lens[i]); j++) {
suffix_cost[sa.rank[idx++]] = cost[i];
}
suffix_cost[sa.rank[idx++]] = 0;
}
suffix_cost_pre_sum[0] = suffix_cost[0];
for (int i = (1); i < (L); i++)
suffix_cost_pre_sum[i] = suffix_cost_pre_sum[i - 1] + suffix_cost[i];
for (int i = (0); i < (L); i++) {
int l = get_len(i);
bool flag = true;
if (i && sa.h[i] == l) flag = false;
if (i < L - 1 && sa.h[i + 1] == l) flag = false;
if (flag) chmax(ans, 1ll * l * suffix_cost[i]);
}
for (int i = (1); i < (L); i++) bn[i - 1] = pair<int, int>(sa.h[i], i);
sort(bn, bn + L - 1);
ds.init();
set<pair<int, int> > pos;
insert(0, L - 1, pos);
int last = -1;
for (int i = (0); i < (L - 1); i++) {
if (bn[i].first != last) {
chmax(ans, 1ll * bn[i].first * ds.get_max());
last = bn[i].first;
}
auto p = --(pos.upper_bound(pair<int, int>(bn[i].second, -1)));
int l = p->first, r = p->second;
partition(l, r, bn[i].second, pos);
}
cout << ans << endl;
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
int T;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
while (T--) {
double d;
cin >> d;
if (!d)
cout << "Y " << 0 << " " << 0 << '\n';
else if (d < 4)
cout << "N" << '\n';
else {
double denta = d * d - 4 * d;
double b = (d + sqrt(denta)) / 2;
cout << fixed << setprecision(9);
cout << "Y " << d - b << " " << b << '\n';
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> g1[111111], g2[111111], dfn, g3[111111];
int N, M, C[111111], gcnt, gnum, P[111111];
int ans, S[111111], chk;
void dfs(int node) {
int i;
C[node] = 1;
for (i = 0; i < g1[node].size(); i++)
if (!C[g1[node][i]]) dfs(g1[node][i]);
dfn.push_back(node);
}
void dfs2(int node) {
int i;
C[node] = 1;
P[node] = gcnt;
gnum++;
for (i = 0; i < g2[node].size(); i++)
if (!C[g2[node][i]]) dfs2(g2[node][i]);
}
void dfs3(int node) {
int i;
C[node] = 1;
gnum++;
if (S[node]) chk = 1;
for (i = 0; i < g3[node].size(); i++)
if (!C[g3[node][i]]) dfs3(g3[node][i]);
}
int main() {
int i, j, a, b;
scanf("%d%d", &N, &M);
for (i = 1; i <= M; i++) {
scanf("%d%d", &a, &b);
g1[a].push_back(b);
g2[b].push_back(a);
}
for (i = 1; i <= N; i++)
if (!C[i]) dfs(i);
memset(C, 0, sizeof C);
for (i = dfn.size() - 1; i >= 0; i--) {
if (!C[dfn[i]]) {
gcnt++;
gnum = 0;
dfs2(dfn[i]);
if (gnum > 1) {
ans += gnum - 1;
S[gcnt] = 1;
}
}
}
for (i = 1; i <= N; i++) {
for (j = 0; j < g1[i].size(); j++) {
if (P[i] == P[g1[i][j]]) continue;
g3[P[i]].push_back(P[g1[i][j]]);
g3[P[g1[i][j]]].push_back(P[i]);
}
}
memset(C, 0, sizeof C);
for (i = 1; i <= gcnt; i++) {
if (!C[i]) {
gnum = 0;
chk = 0;
dfs3(i);
if (chk)
ans += (gnum);
else
ans += gnum - 1;
}
}
printf("%d", ans);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using vi = vector<int>;
char s[5000];
int main() {
scanf("%s", s);
int n = strlen(s);
vector<char> a;
a.clear();
int l = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u') {
a.clear();
l = 0;
} else {
l++;
bool flag = false;
for (auto c : a) flag |= (s[i] == c);
if (!flag) a.push_back(s[i]);
if (l >= 3 && (int)a.size() >= 2) {
printf(" ");
a.clear();
a.push_back(s[i]);
l = 1;
}
}
printf("%c", s[i]);
}
puts("");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
set<int> s[105], t[105];
int main() {
int n;
int pr = 0;
int i;
int t = 0;
int ma = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
pr = pr - (x - t);
if (pr < 0) pr = 0;
if (y + pr > ma) ma = y + pr;
t = x;
pr = y + pr;
}
printf("%d %d\n", t + pr, ma);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int val[500005], lazy[500005], data[500005], size[500005];
int lc[500005], rc[500005], fa[500005];
void Add(int t, int ret) {
val[t] += ret;
lazy[t] += ret;
data[t] += ret;
}
void down(int t) {
if (lazy[t]) {
if (lc[t]) Add(lc[t], lazy[t]);
if (rc[t]) Add(rc[t], lazy[t]);
lazy[t] = 0;
}
}
void update(int t) {
data[t] = val[t];
size[t] = 1;
if (lc[t]) data[t] = max(data[t], data[lc[t]]), size[t] += size[lc[t]];
if (rc[t]) data[t] = max(data[t], data[rc[t]]), size[t] += size[rc[t]];
}
void zig(int x) {
int y = fa[x], z = fa[y];
fa[x] = z;
fa[y] = x;
fa[rc[x]] = y;
lc[y] = rc[x];
rc[x] = y;
if (lc[z] == y) lc[z] = x;
if (rc[z] == y) rc[z] = x;
update(y);
}
void zag(int x) {
int y = fa[x], z = fa[y];
fa[x] = z;
fa[y] = x;
fa[lc[x]] = y;
rc[y] = lc[x];
lc[x] = y;
if (lc[z] == y) lc[z] = x;
if (rc[z] == y) rc[z] = x;
update(y);
}
int stk[500005], root, tot, ans[500005];
void splay(int x) {
int y, z = 0;
for (y = x; y; y = fa[y]) stk[++z] = y;
for (; z; z--) down(stk[z]);
while (fa[x]) {
y = fa[x];
z = fa[y];
if (fa[y]) {
if (lc[z] == y) {
if (lc[y] == x)
zig(y), zig(x);
else
zag(x), zig(x);
} else {
if (rc[y] == x)
zag(y), zag(x);
else
zig(x), zag(x);
}
} else if (lc[y] == x)
zig(x);
else
zag(x);
}
root = x;
update(x);
}
void solve(int t) {
if (!t) return;
down(t);
solve(lc[t]);
++tot;
ans[tot] = val[t] - tot;
solve(rc[t]);
}
int tmp = 0;
void writeln(int t) {
if (!t) return;
down(t);
writeln(lc[t]);
++tmp;
printf("%d ", val[t] - tmp);
writeln(rc[t]);
}
int find(int x, int key) {
if (!x) return 0;
if (data[lc[x]] <= key) {
if (val[x] > key) return find(lc[x], key);
int t = find(rc[x], key);
if (t == 0) t = x;
return t;
} else
return find(lc[x], key);
}
int a[500005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 1; i--) {
val[i] = a[i] + i;
int t = find(root, val[i]);
if (t) {
splay(t);
if (!rc[t]) {
rc[t] = i;
fa[i] = t;
splay(i);
} else {
for (t = rc[t]; lc[t]; t = lc[t])
;
lc[t] = i;
fa[i] = t;
splay(i);
}
} else {
if (!root) {
root = i;
update(root);
continue;
}
for (t = root; lc[t]; t = lc[t])
;
lc[t] = i;
fa[i] = t;
splay(i);
}
}
tot = 0;
solve(root);
for (int i = 1; i <= n - 1; i++)
if (ans[i] > ans[i + 1]) {
printf(":(\n");
return 0;
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
printf("\n");
}
| 14 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000")
#pragma warning(disable : 6031)
#pragma warning(disable : 4244)
#pragma warning(disable : 26451)
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long mod = int(1e9) + 7;
const int INF = 1e9;
const long long LINF = 1e18;
int dist(int from, int to, vector<set<int>>& g) {
int n = (int)(g).size();
vector<int> dist(n, INF);
dist[from] = 0;
set<pair<int, int>> cur;
cur.insert({0, 0});
while (!cur.empty()) {
set<pair<int, int>> nx;
for (auto [u, d] : cur) {
for (auto v : g[u]) {
if (v == to) return d + 1;
if (d + 1 >= dist[v]) continue;
dist[v] = d + 1;
nx.insert({v, d + 1});
}
}
cur = nx;
}
return -1;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << setprecision(18) << fixed;
int n, m;
cin >> n >> m;
vector<set<int>> ga(n), gr(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
gr[u].insert(v);
gr[v].insert(u);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (i == j) continue;
if (!gr[i].count(j)) ga[i].insert(j);
}
int dr = dist(0, n - 1, gr);
int da = dist(0, n - 1, ga);
if (dr == -1 || da == -1) {
cout << -1;
return 0;
}
cout << max(1, max(dr, da));
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int finished = 0;
const int sz = 501;
char maze[sz][sz];
bool vtd[sz][sz];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct point {
int x, y;
};
void init() {
for (int i = 0; i < n; i++) {
scanf("%s", &maze[i]);
}
}
void dfs(point a) {
if (vtd[a.x][a.y]) return;
if (a.x < 0 || a.x >= n || a.y < 0 || a.y >= m) return;
vtd[a.x][a.y] = true;
int isend = 1;
for (int i = 0; i < 4; i++) {
point next;
next.x = a.x + dx[i];
next.y = a.y + dy[i];
if (maze[next.x][next.y] == '.') {
dfs(next);
}
}
if (k) maze[a.x][a.y] = 'X', k--;
}
void print() {
for (int i = 0; i < n; i++) puts(maze[i]);
}
int main() {
cin >> n >> m >> k;
init();
for (int i = 0; i < n; i++)
for (int j = 0; j < m && k; j++)
if (maze[i][j] == '.') {
dfs({i, j});
}
print();
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnl(7319);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, x;
cin >> n;
for (; n--;) {
cin >> x;
cout << x - (x + 1) % 2 << " ";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:100000000")
vector<string> v;
char s[1500];
int main() {
gets(s);
int l = strlen(s);
string cur = "";
int i;
for (i = 0; i < l; ++i) {
if (s[i] == ',') {
if (cur != "") v.push_back(cur);
v.push_back(",");
cur = "";
} else if ((s[i] == '.' && cur != "" && cur[cur.size() - 1] != '.') ||
(s[i] != '.' && s[i] != ' ' && cur != "" &&
cur[cur.size() - 1] == '.')) {
v.push_back(cur);
cur = s[i];
} else if (cur == "..." && s[i] != ' ') {
v.push_back(cur);
cur = s[i];
} else if (s[i] == ' ' && cur != "") {
v.push_back(cur);
cur = "";
} else if (s[i] != ' ') {
cur += s[i];
}
}
if (cur != "") {
v.push_back(cur);
}
string ans = v[0];
for (i = 0; i < v.size() - 1; ++i) {
if (v[i] == ",") {
ans += (" " + v[i + 1]);
} else if (v[i + 1] == "...") {
ans += (" " + v[i + 1]);
} else if (v[i][0] >= '0' && v[i][0] <= '9' && v[i + 1][0] >= '0' &&
v[i + 1][0] <= '9') {
ans += (" " + v[i + 1]);
} else {
ans += (v[i + 1]);
}
}
cout << ans;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, primes[] = {2, 3, 5, 7, 11, 13, 17, 19}, counters[20];
long long max_val;
set<long long> result;
void rec(int step, int limit, long long num) {
if (step == limit) {
if (num != 1) {
result.insert(num);
if (result.size() > n) {
result.erase(result.begin());
}
}
} else {
long long mult = 1;
while (true) {
long long new_num = num * mult;
if (new_num <= max_val) {
rec(step + 1, limit, new_num);
} else {
break;
}
mult *= primes[step];
}
}
}
int main() {
scanf("%d", &n);
max_val = 2 * n * n;
for (int m = 2;; m++) {
bool ok = true;
result.clear();
rec(0, m, 1);
for (int i = 0; i < m; i++) {
counters[i] = 0;
}
for (set<long long>::iterator it = result.begin(); it != result.end();
it++) {
long long num = *it;
for (int i = 0; i < m; i++) {
if (num % primes[i] == 0) {
counters[i]++;
}
}
}
for (int i = 0; i < m; i++) {
ok &= counters[i] >= (n + 1) / 2;
}
if (ok && result.size() == n) {
for (set<long long>::iterator it = result.begin(); it != result.end();
it++) {
long long num = *it;
if (it != result.begin()) {
printf(" ");
}
printf("%I64d", num);
}
puts("");
break;
}
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int b[N];
int ans;
int n, m, k;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
;
int a = 1;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
bool vk = 1;
bool vn = 1;
for (int i = 0; i < n; ++i) {
if (b[i] <= m && vn) {
a++;
} else
break;
}
for (int i = n - 1; i >= 0; --i) {
if (b[i] <= m && vk) {
a++;
} else
vk = 0;
}
cout << min(n, a - 1);
short int lalala;
;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[10], b[10];
int i, cnt, low;
int main() {
for (i = 1; i <= 3; i++) scanf("%d", &a[i]);
for (i = 1; i <= 3; i++) {
scanf("%d", &b[i]);
a[i] -= b[i];
if (a[i] > 0)
cnt += a[i] / 2;
else
low += a[i];
}
if (cnt + low >= 0)
printf("Yes\n");
else
printf("No\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, a[100100], u[100100];
bool w[100100];
int main() {
scanf("%d", &n);
if (n & 1) {
puts("-1");
return 0;
}
for (i = 0; i < n; i++) a[i] = (i * 2 + int(i * 2 < n)) % n;
for (k = 0; k < n; k++)
if (!u[k]) {
m++;
for (i = k, j = 0; !u[i]; j++) {
u[i] = m;
i = a[i];
}
}
w[1] = true;
printf("0 ");
for (i = a[0]; i != 0; i = a[i]) {
printf("%d ", i);
j = (i + n / 2) % n;
if (!w[u[j]]) {
swap(a[i], a[j]);
w[u[j]] = true;
}
}
puts("0");
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
set<int> row[100005], col[100005], rowRev[100005], colRev[100005];
int main() {
long long n, m, k, x, y;
cin >> n >> m >> k;
bool no = 0;
for (int i = 1; i <= n; i++) {
row[i].insert(m + 1);
rowRev[i].insert(0);
}
for (int i = 1; i <= m; i++) {
col[i].insert(n + 1);
colRev[i].insert(-1);
}
for (int i = 1; i <= k; i++) {
cin >> x >> y;
row[x].insert(y), col[y].insert(x);
rowRev[x].insert(-1 * y), colRev[y].insert(x * -1);
}
if (*col[1].upper_bound(1) - 1 + k == n * m) return cout << "Yes\n", 0;
long long j = 0, cnt = 1, r = 1, c = 1, upR = 1, dwR = n + 1, lfC = 0,
rtC = m + 1, cur;
while (cnt + k < n * m) {
if (j == 0) {
long long up = *row[r].upper_bound(c);
if ((up <= c + 1 || rtC - c <= 1) && cnt + k != n * m) {
no = 1;
break;
} else if (up <= c + 1 || rtC - c <= 1)
break;
cur = min(up, rtC);
cnt += (cur - c - 1);
c = cur - 1;
rtC = c;
} else if (j == 1) {
long long up = *col[c].upper_bound(r);
if ((up <= r + 1 || dwR - r <= 1) && cnt + k != n * m) {
no = 1;
break;
} else if (up <= r + 1 || dwR - r <= 1)
break;
cur = min(up, dwR);
cnt += (cur - r - 1);
r = cur - 1;
dwR = r;
} else if (j == 2) {
long long up = *rowRev[r].upper_bound(-1 * c);
up *= -1;
if ((c - up <= 1 || c - lfC <= 1) && cnt + k != n * m) {
no = 1;
break;
} else if (c - up <= 1 || c - lfC <= 1)
break;
cur = max(up, lfC);
cnt += (c - cur - 1);
c = cur + 1;
lfC = c;
} else {
long long up = *colRev[c].upper_bound(-1 * r);
up *= -1;
if ((r - up <= 1 || r - upR <= 1) && cnt != n * m) {
no = 1;
break;
} else if (r - up <= 1 || r - upR <= 1)
break;
cur = max(up, upR);
cnt += (r - cur - 1);
r = cur + 1;
upR = r;
}
j++;
j %= 4;
}
if (no)
cout << "No\n";
else
cout << "Yes\n";
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
string s;
long long int dp[1000001];
int main() {
ios::sync_with_stdio(false);
dp[1] = 1;
dp[2] = 2;
cin >> n;
for (int i = 3; i <= n; ++i) {
dp[i] = dp[i - 1] + 1;
dp[i] += dp[i - 2];
dp[i] = dp[i] % 1000000007;
}
cout << (dp[n] + dp[n - 1]) % 1000000007 << endl;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
int n, a[200009];
cin >> t;
while (t--) {
cin >> n;
map<int, int> dp, dp2;
dp[0] = n + 1;
dp2[0] = n;
int deficit = 0;
for (int i = 1; i <= 2 * n; i++) {
cin >> a[i];
if (a[i] == 1)
deficit++;
else
deficit--;
}
int l = 0;
for (int i = n; i >= 1; i--) {
if (a[i] == 1)
l++;
else
l--;
if (dp.find(l) == dp.end()) dp[l] = i;
}
int r = 0;
for (int i = n + 1; i <= 2 * n; i++) {
if (a[i] == 1)
r++;
else
r--;
if (dp2.find(r) == dp2.end()) dp2[r] = i;
}
int ans = 2 * n;
for (auto x : dp)
if (dp2.find(deficit - x.first) != dp2.end())
ans = min(ans, dp2[deficit - x.first] - x.second + 1);
cout << ans << "\n";
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int c[5010], n, x[5010], y[5010], top, d[5010], sta[5010], t;
struct DOT {
int x, y;
DOT(int _x = 0, int _y = 0) { x = _x, y = _y; }
} a[5010];
DOT operator-(DOT a, DOT b) { return DOT(a.x - b.x, a.y - b.y); }
int ctime(DOT a, DOT b) { return a.x * b.y - a.y * b.x; }
bool cmp(int x, int y) {
return a[x].x < a[y].x || (a[x].x == a[y].x && a[x].y < a[y].y);
}
void convexhull() {
for (int i = 1, _E_ = n; i <= _E_; ++i) d[i] = i;
sort(d + 1, d + n + 1, cmp);
for (int i = 1, _E_ = n; i <= _E_; ++i) {
while (top > 1 &&
ctime(a[sta[top]] - a[sta[top - 1]], a[d[i]] - a[sta[top]]) < 0)
--top;
sta[++top] = d[i];
}
int k = top;
for (int i = n - 1, _E_ = 1; i >= _E_; --i) {
while (top > k &&
ctime(a[sta[top]] - a[sta[top - 1]], a[d[i]] - a[sta[top]]) < 0)
--top;
sta[++top] = d[i];
}
--top;
}
void solve(int d, int e, int f) {
for (int i = 1, _E_ = n; i <= _E_; ++i)
if (c[i] != c[e])
if (ctime(a[i] - a[d], a[i] - a[e]) > 0)
if (ctime(a[i] - a[e], a[i] - a[f]) > 0)
if (ctime(a[i] - a[f], a[i] - a[d]) > 0) {
++t;
x[t] = i;
y[t] = f;
solve(d, e, i);
solve(f, i, e);
solve(i, f, d);
return;
}
for (int i = 1, _E_ = n; i <= _E_; ++i)
if (ctime(a[i] - a[d], a[i] - a[e]) > 0)
if (ctime(a[i] - a[e], a[i] - a[f]) > 0)
if (ctime(a[i] - a[f], a[i] - a[d]) > 0) {
++t;
x[t] = i;
y[t] = d;
}
}
int main() {
scanf("%d", &n);
for (int i = 1, _E_ = n; i <= _E_; ++i)
scanf("%d %d %d", &a[i].x, &a[i].y, c + i);
convexhull();
int cnt = 0;
for (int i = 1, _E_ = top; i <= _E_; ++i) d[i] = sta[i];
for (int i = 2, _E_ = top; i <= _E_; ++i)
if (c[d[i]] ^ c[d[i - 1]]) ++cnt;
if (cnt == 0) {
for (int i = 2, _E_ = top; i <= _E_; ++i) ++t, x[t] = d[i - 1], y[t] = d[i];
for (int i = 1, _E_ = n; i <= _E_; ++i)
if (c[i] ^ c[d[1]]) {
for (int j = 2, _E_ = top; j <= _E_; ++j) solve(d[j - 1], d[j], i);
solve(d[top], d[1], i);
printf("%d\n", t);
for (int j = 1, _E_ = t; j <= _E_; ++j)
printf("%d %d\n", x[j] - 1, y[j] - 1);
return 0;
}
for (int i = 2, _E_ = top - 1; i <= _E_; ++i) solve(d[1], d[i], d[i + 1]);
printf("%d\n", t);
for (int j = 1, _E_ = t; j <= _E_; ++j)
printf("%d %d\n", x[j] - 1, y[j] - 1);
} else if (cnt > 2)
printf("Impossible\n");
else {
int k = 1, j;
while (c[d[k]] == c[d[k + 1]]) {
d[k + top] = d[k];
++k;
}
d[k + top] = d[k];
for (int i = 1, _E_ = top; i <= _E_; ++i) d[i] = d[i + k];
for (int i = 2, _E_ = top; i <= _E_; ++i)
if (c[d[i]] ^ c[d[i - 1]]) {
j = i;
break;
}
for (int i = 2, _E_ = j - 1; i <= _E_; ++i)
++t, x[t] = d[i - 1], y[t] = d[i];
for (int i = j + 1, _E_ = top; i <= _E_; ++i)
++t, x[t] = d[i - 1], y[t] = d[i];
for (int i = 2, _E_ = j - 1; i <= _E_; ++i) solve(d[i - 1], d[i], d[j]);
for (int i = j, _E_ = top - 1; i <= _E_; ++i) solve(d[i], d[i + 1], d[1]);
printf("%d\n", t);
for (int i = 1, _E_ = t; i <= _E_; ++i)
printf("%d %d\n", x[i] - 1, y[i] - 1);
}
return 0;
}
| 24 |
#include <bits/stdc++.h>
using namespace std;
void run();
int main() {
ios::sync_with_stdio(0);
run();
}
long long const lots = 777777777;
int ways[110000][4][4];
int seg[1 << 18][2];
int psh[1 << 18][2];
int ans[1 << 18];
unsigned char val[1 << 17];
long long n, m;
void pull(long long i, long long x, long long more) {
ans[i] = 1;
if (more) {
ans[i] = ((long long)ans[i * 2 + 1] * (long long)ans[i * 2 + 2]) % lots;
return;
}
if (val[x] > 0) {
ans[i] = (x <= 0 or ways[0][val[x - 1]][val[x]]);
return;
}
if (seg[i][0] == x) {
long long pre = (x <= 0 ? 0 : val[x - 1]);
long long aft = (x >= n - 1 ? 0 : val[seg[i][1]]);
ans[i] = ways[seg[i][1] - seg[i][0]][pre][aft];
return;
}
}
void push(long long i, long long x, long long more) {
if (more) {
for (int a = 2; a--;) {
pull(i * 2 + 1 + a, x + a * (more + 1) / 2, more >> 1);
for (int b = 2; b--;) {
if (~psh[i][b]) {
psh[i * 2 + 1 + a][b] = psh[i][b];
ans[i * 2 + 1 + a] = 1;
}
}
}
if (psh[i * 2 + 1][0] == x) {
pull(i * 2 + 1, x, more >> 1);
}
}
for (int a = 2; a--;)
if (~psh[i][a]) seg[i][a] = psh[i][a], psh[i][a] = ~0;
}
int *find(long long x, long long i = 0, long long l = 0,
long long r = 1 << 17) {
push(i, l, r - l - 1);
long long m = (l + r) / 2;
int *res = l + 1 == r ? seg[i]
: x < m ? find(x, i * 2 + 1, l, m)
: find(x, i * 2 + 2, m, r);
pull(i, l, r - l - 1);
return res;
}
void reset(long long a, long long b, long long i = 0, long long l = 0,
long long r = 1 << 17) {
push(i, l, r - l - 1);
long long m = (l + r) / 2;
if (a < r and b > l) {
if (a <= l and b >= r) {
psh[i][0] = a;
psh[i][1] = b;
push(i, l, r - l - 1);
} else {
reset(a, b, i * 2 + 2, m, r);
}
if (l + 1 != r) {
reset(a, b, i * 2 + 1, l, m);
}
}
pull(i, l, r - l - 1);
if (r < 10) {
}
}
void debug(int i = 0, int l = 0, int r = 1 << 17) {
push(i, l, r - l - 1);
if (l + 1 == r) {
if (seg[i][0] == l) {
cout << " | " << seg[i][0] << " ... " << seg[i][1] << " val[" << l
<< "] = " << val[l] << " for " << ans[i] << endl;
}
} else {
long long m = (l + r) / 2;
debug(i * 2 + 1, l, m);
debug(i * 2 + 2, m, r);
}
pull(i, l, r - l - 1);
}
void update(long long x, long long y) {
long long was = val[x];
val[x] = y;
if (y == 0) {
long long a = (x == 0 or val[x - 1] != 0 ? x : find(x - 1)[0]);
long long b = (x == n - 1 or val[x + 1] != 0 ? 1 + x : find(x + 1)[1]);
reset(a, b);
} else if (was == 0) {
int *pos = find(x), a = pos[0], b = pos[1];
reset(a, x);
reset(x, x + 1);
reset(x + 1, b);
} else {
reset(x, x + 1);
if (x != 0 and val[x - 1] == 0) reset(find(x - 1)[0], x);
if (x != n - 1 and val[x + 1] == 0) reset(x + 1, find(x + 1)[1]);
}
find(x - 1);
find(x + 1);
find(x);
find(x - 1);
find(x + 1);
}
void run() {
cin >> n >> m;
for (int i = 1; i < 4; i++)
for (int j = 1; j < 4; j++) cin >> ways[0][i][j];
for (int i = 1 << 18; i--;) ans[i] = 1, psh[i][0] = psh[i][1] = -1;
for (int i = 1; i < 4; i++) ways[0][0][i] = ways[0][i][0] = 1;
for (int i = 0; i < 100000; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++)
for (int l = 1; l < 4; l++)
ways[i + 1][j][k] =
(ways[i + 1][j][k] +
(long long)ways[i][j][l] * (long long)ways[0][l][k]) %
lots;
reset(0, n);
while (m--) {
int x, y;
cin >> x >> y;
update(x - 1, y);
cout << ans[0] << endl;
}
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
char s[50050], ans[2650];
int num[30];
int f[2650][2650];
int n, len;
int search(int l, int r) {
if (r < l) return 0;
if (f[l][r]) return f[l][r];
if (l == r) return f[l][r] = 1;
if (s[l] == s[r]) return f[l][r] = search(l + 1, r - 1) + 2;
return f[l][r] = max(search(l + 1, r), search(l, r - 1));
}
void back(int l, int r) {
if (l > r) return;
if (l == r)
ans[len++] = s[l];
else if (s[l] == s[r]) {
ans[len++] = s[l];
back(l + 1, r - 1);
ans[len++] = s[r];
} else {
if (f[l + 1][r] > f[l][r - 1])
back(l + 1, r);
else
back(l, r - 1);
}
}
int main() {
scanf("%s", s);
n = strlen(s);
if (n >= 2600) {
for (int i = 0; i < n; i++) num[s[i] - 'a']++;
for (int i = 0; i < 26; i++)
if (num[i] >= 100) {
for (int j = 0; j < 100; j++) printf("%c", i + 'a');
printf("\n");
break;
}
} else {
search(0, n - 1);
len = 0;
back(0, n - 1);
ans[len] = '\0';
if (len <= 100)
printf("%s\n", ans);
else {
for (int i = 0; i < 50; i++) printf("%c", ans[i]);
for (int i = len - 50; i < len; i++) printf("%c", ans[i]);
printf("\n");
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x %= p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
{
long long n, o = 0, c = 0, ans = 0;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == '(')
o++;
else
c++;
}
if (o != c)
cout << -1 << "\n";
else {
long long a = 0, b = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
a++;
} else {
b++;
}
if (s[i] == ')' && b > a) {
a = 1, b = 0;
long long j = i;
while (j < n && a != b) {
j++;
if (s[j] == '(') {
b++;
} else {
a++;
}
}
ans += (j - i) + 1;
i = j;
a = 0, b = 0;
}
}
cout << ans << "\n";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, a[100], fa[100], id[100], S[100], cnt[40000], dp[100][40000],
c[200][200];
bool not_mn[100];
int find(int a) { return fa[a] == a ? a : fa[a] = find(fa[a]); }
void merge(int u, int v) {
int fu = find(u), fv = find(v);
if (fu == fv) return;
fa[fu] = fv;
}
int C(int n, int m) {
if (n < m) return 0;
if (n == m || m == 0) return 1;
if (c[n][m]) return c[n][m];
return C(n - 1, m) + C(n - 1, m - 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), fa[i] = i;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (i != j && a[i] % a[j] == 0) merge(i, j), not_mn[i] |= true;
}
int ans = 1, tot = 0;
for (int p = 1; p <= n; p++)
if (find(p) == p) {
memset(dp, 0, sizeof(dp));
int num = 0, c = 0;
for (int i = 1; i <= n; i++)
if (find(i) == p && !not_mn[i]) id[i] = ++c;
for (int i = 1; i <= n; i++)
if (find(i) == p && not_mn[i]) {
num++;
for (int j = 1; j <= n; j++)
if (!not_mn[j] && a[i] % a[j] == 0) S[i] |= (1 << (id[j] - 1));
}
if (!num) continue;
for (int T = 0; T < (1 << c); T++) {
cnt[T] = 0;
for (int i = 1; i <= n; i++)
if (find(i) == p && (S[i] | T) == T && not_mn[i]) cnt[T]++;
}
for (int i = 1; i <= n; i++)
if (find(i) == p && not_mn[i]) dp[1][S[i]]++;
for (int i = 1; i < num; i++)
for (int T = 0; T < (1 << c); T++)
if (dp[i][T]) {
dp[i + 1][T] = (dp[i + 1][T] + 1ll * dp[i][T] * (cnt[T] - i)) % mod;
for (int j = 1; j <= n; j++)
if (find(j) == p && not_mn[j] && (S[j] & T) && (S[j] | T) != T) {
dp[i + 1][S[j] | T] = (dp[i + 1][S[j] | T] + dp[i][T]) % mod;
}
}
ans =
1ll * ans * dp[num][(1 << c) - 1] % mod * C(tot + num - 1, tot) % mod;
tot += num - 1;
}
printf("%d\n", ans);
}
| 27 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4005, maxm = 70, oo = 1e9;
int f[maxn][maxm][maxm], vis[maxn][maxm][maxm];
int a[maxn], s[maxn];
int n, i;
void update(int &F, int x) { F = max(F, x); }
void solve(int l, int k, int d) {
if (vis[l][k][d]) return;
vis[l][k][d] = 1;
int &F = f[l][k][d];
int r = n - d - l + 1, x, y;
if (r - l + 1 < k) return;
F = -oo;
x = s[l + k - 1] - s[l - 1];
if (r - (l + k) + 1 < k) {
update(F, x);
} else {
solve(l + k, k, d);
y = x - (s[r] - s[r - k]) + f[l + k][k][d];
if (r - (l + k) + 1 > k) {
solve(l + k, k + 1, d + 1);
int z = x - (s[r] - s[r - k - 1]) + f[l + k][k + 1][d + 1];
update(F, min(y, z));
} else
update(F, y);
}
if (r - l + 1 < k + 1) return;
x = s[l + k] - s[l - 1];
if (r - (l + k + 1) + 1 < k + 1) {
update(F, x);
} else {
solve(l + k + 1, k + 1, d);
y = x - (s[r] - s[r - k - 1]) + f[l + k + 1][k + 1][d];
if (r - (l + k + 1) + 1 > k + 1) {
solve(l + k + 1, k + 2, d + 1);
int z = x - (s[r] - s[r - k - 2]) + f[l + k + 1][k + 2][d + 1];
update(F, min(y, z));
} else
update(F, y);
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
solve(1, 1, 0);
printf("%d", f[1][1][0]);
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int Mod = 1e9 + 7;
struct Edge {
int u, v;
long long x;
bool operator<(const Edge& B) const { return x < B.x; }
} E[N];
vector<int> G[N];
long long A[N];
int Pow2[N], ver[N << 1];
int n, m, k;
int col[N];
bool DFS(int o) {
for (int v : G[o]) {
if (!col[v]) {
col[v] = 3 - col[o];
if (!DFS(v)) return false;
}
if (col[v] + col[o] != 3) return false;
}
return true;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%I64d", &A[i]);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
E[i] = (Edge){u, v, A[u] ^ A[v]};
}
Pow2[0] = 1;
for (int i = 1; i <= max(n, k); i++) Pow2[i] = Pow2[i - 1] * 2 % Mod;
sort(E + 1, E + m + 1);
int ans = 0, ret = 0;
for (int i = 1; i <= m; i++) {
int r = i;
while (r <= m && E[r].x == E[i].x) ++r;
--r;
++ret;
int c = 0;
for (int j = i; j <= r; j++)
ver[++c] = E[j].u, ver[++c] = E[j].v, G[E[j].u].push_back(E[j].v),
G[E[j].v].push_back(E[j].u);
sort(ver + 1, ver + c + 1);
c = unique(ver + 1, ver + c + 1) - ver - 1;
int cnt = n - c;
for (int j = 1; j <= c; j++)
if (!col[ver[j]]) {
col[ver[j]] = 1;
if (!DFS(ver[j])) {
cnt = -1;
break;
}
++cnt;
}
if (cnt >= 0) ans = (ans + Pow2[cnt]) % Mod;
for (int j = 1; j <= c; j++) col[ver[j]] = 0, G[ver[j]].clear();
i = r;
}
ans = (ans + 1ll * (Pow2[k] + Mod - ret) * Pow2[n]) % Mod;
printf("%d\n", ans);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000005;
int pos[105];
int main() {
int n, m, k;
int ans = 0;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= k; i++) {
int x;
scanf("%d", &x);
pos[x] = i;
}
for (int i = 1; i <= n; i++)
for (int i = 1; i <= m; i++) {
int x;
scanf("%d", &x);
ans += pos[x];
for (int i = 1; i <= k; i++)
if (pos[i] < pos[x]) pos[i]++;
pos[x] = 1;
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 7;
const int mod = 1000000007;
int n, a[maxn];
long long dp[maxn][maxn], fac[maxn];
long long quick_pow(long long a, long long b) {
long long sum = 1;
while (b) {
if (b & 1) sum = (sum * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return sum;
}
long long inv(int n, int m) {
if (m > n) return 0;
return (((fac[n] * (quick_pow(fac[m], mod - 2)) % mod) *
quick_pow(fac[n - m], mod - 2) % mod) %
mod);
}
void init() {
fac[0] = 1;
for (int i = 1; i <= 100; i++) fac[i] = (fac[i - 1] * i) % mod;
}
int main() {
init();
scanf("%d", &n);
for (int i = 0; i < 10; i++) scanf("%d", &a[i]);
for (int i = a[9]; i <= n; i++) dp[9][i] = 1;
for (int i = 8; i >= 0; i--) {
for (int len = a[i]; len <= n; len++) {
for (int j = a[i]; j <= len; j++) {
dp[i][len] =
(dp[i][len] + dp[i + 1][len - j] * inv(i == 0 ? len - 1 : len, j)) %
mod;
}
}
}
long long ans = 0;
for (int i = 1; i <= n; i++) ans = (ans + dp[0][i]) % mod;
printf("%lld\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int arr[N];
int main() {
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> arr[i];
int na, ans = 0;
for (int i = 1; i <= n; i++) {
cin >> na;
if (na) ans += arr[i], arr[i] = 0;
arr[i] += arr[i - 1];
}
int nans = 0;
for (int i = 1; i + k - 1 <= n; i++) {
nans = max(nans, ans + arr[i + k - 1] - arr[i - 1]);
}
cout << nans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1005;
long long n, k, a[N], to[N], vis[N], tot[N], top, belong, du[N], pos;
inline long long read() {
long long ret = 0, ff = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') ff = -1;
ch = getchar();
}
while (isdigit(ch)) {
ret = ret * 10 + (ch ^ 48);
ch = getchar();
}
return ret * ff;
}
void write(long long x) {
if (x < 0) {
x = -x, putchar('-');
}
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
void writeln(long long x) { write(x), puts(""); }
void writesp(long long x) { write(x), putchar(' '); }
signed main() {
n = read(), k = read();
for (long long i = 1; i <= n; i++) {
long long x = read();
if (!x) continue;
to[x] = i;
du[i]++;
}
for (long long i = 1; i <= n; i++) {
if (!vis[i] && !du[i]) {
long long now = i, cnt = 0;
top++;
while (now) {
cnt++;
if (now == k) belong = top, pos = cnt;
vis[now] = 1;
tot[top]++;
now = to[now];
}
}
}
bitset<1005> bt;
bt[0] = 1;
for (long long i = 1; i <= top; i++) {
if (i == belong) continue;
bt |= bt << tot[i];
}
for (long long i = 0; i <= n; i++)
if (bt[i] == 1) writeln(i + pos);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
const int inf = 1e9;
bool isprime(long long x) {
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
void solve() {
long long n, k;
cin >> n >> k;
long long val = n - k;
long long val2 = n + k;
if (val == 1 && isprime(val2)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = (100000 + 5) * 2;
struct nood {
long long x;
int p;
} s[maxn], st[maxn];
bool cmp(nood a, nood b) { return a.x > b.x; }
bool cmp1(nood a, nood b) { return a.x < b.x; }
bool cmp2(nood a, nood b) { return a.p < b.p; }
int main() {
int m;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> s[i].x;
s[i].p = i;
}
for (int i = 0; i < m; i++) {
cin >> st[i].x;
st[i].p = i;
}
sort(s, s + m, cmp);
sort(st, st + m, cmp1);
for (int i = 0; i < m; i++) s[i].p = st[i].p;
sort(s, s + m, cmp2);
for (int i = 0; i < m; i++) {
cout << s[i].x;
if (i != m - 1) cout << " ";
}
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, s, t, u, v;
vector<int> adjlist[1005];
int shortestpath[1005][1005];
int ans = 0;
bool connected(int a, int b) {
bool connected = false;
if (a == b) return true;
for (int v : adjlist[a]) {
if (v == b) connected = true;
}
return connected;
}
void bfs(int s) {
queue<int> q;
q.push(s);
shortestpath[s][s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adjlist[u]) {
if (shortestpath[s][v] == -1) {
shortestpath[s][v] = shortestpath[s][u] + 1;
q.push(v);
}
}
}
}
int main() {
cin >> n >> m >> s >> t;
memset(shortestpath, -1, sizeof(shortestpath));
for (int i = 1; i <= m; i++) {
cin >> u >> v;
adjlist[u].push_back(v);
adjlist[v].push_back(u);
}
for (int i = 1; i <= n; i++) bfs(i);
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if ((shortestpath[s][i] + 1 + shortestpath[j][t]) >= shortestpath[s][t] &&
!connected(i, j) &&
(shortestpath[s][j] + 1 + shortestpath[i][t]) >= shortestpath[s][t]) {
ans++;
}
}
}
cout << ans << endl;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
int a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
for (int i = 0; i < n; i++) {
cin >> b[i];
b[i]--;
}
int ind1[n], ind2[n];
for (int i = 0; i < n; i++) {
ind1[a[i]] = i;
ind2[b[i]] = i;
}
int c[n];
for (int i = 0; i < n; i++) {
c[i] = ind2[a[i]];
if (i > 0) c[i] = max(c[i], c[i - 1]);
}
char ans[n];
char d = 'a';
int end = 0;
int st = 0;
for (int i = 0; i < n; i++) {
int g = max(i, c[ind1[b[i]]]);
end = max(end, g);
if (i != end) continue;
for (int j = st; j <= end; j++) ans[b[j]] = d;
if (d != 'z') d++;
st = i + 1;
end = i + 1;
}
for (int i = st; i < n; i++) {
ans[b[i]] = d;
}
if (d != 'z' && d - 'a' < k)
cout << "NO";
else {
cout << "YES\n";
for (int i = 0; i < n; i++) cout << ans[i];
}
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
map<char, int> x, y;
void init() {
x['1'] = 1;
y['1'] = 1;
x['2'] = 1;
y['2'] = 2;
x['3'] = 1;
y['3'] = 3;
x['4'] = 2;
y['4'] = 1;
x['5'] = 2;
y['5'] = 2;
x['6'] = 2;
y['6'] = 3;
x['7'] = 3;
y['7'] = 1;
x['8'] = 3;
y['8'] = 2;
x['9'] = 3;
y['9'] = 3;
x['0'] = 4;
y['0'] = 2;
}
bool check(int X, int Y) {
if (X < 1 || X > 4 || Y < 1 || Y > 3) return true;
if (X == 4 && Y != 2) return true;
return false;
}
int main() {
init();
vector<pair<int, int> > a;
int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i < n; i++) {
a.push_back(make_pair(x[s[i]] - x[s[i - 1]], y[s[i]] - y[s[i - 1]]));
}
int be = s[0];
for (int i = '0'; i <= '9'; i++) {
if (i == be) continue;
int tx = x[i];
int ty = y[i];
bool tag = false;
for (int j = 0; j < n - 1; j++) {
tx += a[j].first;
ty += a[j].second;
if (check(tx, ty)) {
tag = true;
break;
}
}
if (!tag) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
const int N = 2e5 + 5;
const long long int mod = 1e9 + 7;
const long long int Mod = 998244353;
const long double Pi = acos(-1);
const long long int Inf = 4e18;
const long double Eps = 1e-9;
int dx[9] = {0, 1, -1, 0, 0, 1, 1, -1, -1};
int dy[9] = {0, 0, 0, 1, -1, 1, -1, 1, -1};
using namespace std;
vector<int> g[26];
void TestCase() {
int n;
cin >> n;
vector<string> v(n);
vector<int> f(26), take(26, 0);
bool ok = true;
for (int i = 0; i < n; i++) {
cin >> v[i];
f.assign(26, 0);
for (int j = 0; j < (int)v[i].size(); j++) f[v[i][j] - 'a']++;
for (int j = 0; j < 26; j++) ok &= (f[j] <= 1);
}
if (ok) {
string t;
for (int i = 0; i < n; i++) {
for (int j = 0; j < (int)v[i].size(); j++) g[v[i][j] - 'a'].push_back(i);
}
map<int, int> p, q;
int cnt = 0;
while (1) {
if (p.empty()) {
char st = '-';
for (int i = 0; i < 26; i++) {
if ((!take[i]) && (!g[i].empty())) {
bool yes = true;
for (auto u : g[i]) {
if (v[u][0] != char(i + 'a')) yes = false;
}
if (yes) {
st = char(i + 'a');
break;
}
}
}
if (st == '-') {
for (int i = 0; i < 26; i++) {
if ((!take[i]) && (!g[i].empty())) {
ok = false;
break;
}
}
break;
} else {
t.push_back(st);
take[st - 'a'] = 1;
for (auto u : g[st - 'a']) p.insert({u, 0});
}
}
for (auto &u : p) {
if (u.second + 1 < (int)v[u.first].size())
q.insert({u.first, u.second + 1});
}
if (!q.empty()) {
int sid = q.begin()->first, cid = q.begin()->second;
char ch = v[sid][cid];
for (auto &o : q) {
if (ch != v[o.first][o.second]) ok = false;
}
if (!ok) break;
for (auto u : g[ch - 'a']) {
if (v[u][0] == ch)
q.insert({u, 0});
else {
if (q.find(u) == q.end()) ok = false;
}
}
if (!ok)
break;
else {
t.push_back(ch);
take[ch - 'a'] = 1;
}
}
p = q;
q.clear();
}
ok ? cout << t : cout << "NO";
} else
cout << "NO";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int T = 1;
while (T--) {
TestCase();
cout << "\n";
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
string str;
int cal() {
int res = 0;
for (int i = 0; i < str.size() - 1; i++)
if (str[i] == 'V' && str[i + 1] == 'K') res++;
return res;
}
int main() {
while (cin >> str) {
int ans = cal();
char pre;
for (int i = 0; i < str.size(); i++) {
if (i) str[i - 1] = (pre == 'V' ? 'V' : 'K');
pre = str[i];
str[i] = (str[i] == 'V' ? 'K' : 'V');
ans = max(ans, cal());
}
cout << ans << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
template <class T>
void out(const vector<T> &a) {
cout << "array: ";
for (int i = 0; i < (int)a.size(); i++) cout << a[i] << " ";
cout << endl;
cout.flush();
}
void binprint(int mask, int n) {
int i;
string s = "";
do {
s += (mask % 2 + '0');
mask /= 2;
} while (mask);
reverse(s.begin(), s.end());
s = string(max(n - (int)s.size(), 0), '0') + s;
for (i = (int)s.size() - n; i < (int)s.size(); i++) printf("%c", s[i]);
printf("\n");
}
void ASCII_Chart() {
int i, j, k;
printf("ASCII Chart:(30-129)\n");
for (i = 30; i <= 50; i++) {
for (j = 0; j < 5; j++) {
k = i + j * 20;
printf("%3d---> '%c' ", k, k);
}
printf("\n");
}
}
template <class T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <class T>
T Abs(T x) {
return x > 0 ? x : -x;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T Mod(T n, T m) {
return (n % m + m) % m;
}
int BigMod(long long B, long long P, long long M) {
long long R = 1;
while (P > 0) {
if (P % 2 == 1) {
R = (R * B) % M;
}
P /= 2;
B = (B * B) % M;
}
return (int)R;
}
long long mulmod(long long a, long long b, long long c) {
long long x = 0, y = a % c;
while (b > 0) {
if (b % 2 == 1) {
x = (x + y) % c;
}
y = (y * 2) % c;
b /= 2;
}
return x % c;
}
long long mpow(long long x, long long k) {
if (k == 0) return 1;
long long r = mpow(x, k / 2);
return k % 2 ? (r * r * x) : (r * r);
}
vector<string> SubstringGenerate(string str) {
int i, j, len;
vector<string> store;
len = (int)str.size();
for (i = 0; i < len; i++)
for (j = i; j <= len; j++) store.push_back(str.substr(i, j - i + 1));
return store;
}
template <class T>
inline bool isPrime(T n) {
if (n <= 1) return false;
for (T i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
bool IsVowel(char ch) {
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return true;
return false;
}
bool isUpperCase(char c) { return c >= 'A' && c <= 'Z'; }
bool isLowerCase(char c) { return c >= 'a' && c <= 'z'; }
bool isLetter(char c) { return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'; }
bool isDigit(char c) { return c >= '0' && c <= '9'; }
double deg2rad(double x) { return (PI * x) / 180.0; }
double rad2deg(double x) { return (180.0 * x) / PI; }
template <class T>
string toString(T n) {
ostringstream oss;
oss << n;
oss.flush();
return oss.str();
}
int toInt(string s) {
int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
long long toLl(string s) {
long long r = 0;
istringstream sin(s);
sin >> r;
return r;
}
double toDouble(string s) {
double r = 0;
istringstream sin(s);
sin >> r;
return r;
}
template <typename T>
static void splitstr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
struct debugger {
template <typename T>
debugger &operator,(const T &v) {
cerr << v << "\t";
return *this;
}
} dbg;
struct Point {
int x, y;
Point(int _x = 0, int _y = 0) {
x = _x;
y = _y;
}
};
bool com(int a, int b) { return a < b; }
int main() {
long long i, q, n;
char ch;
string str;
while (cin >> q) {
long long upper = ((1 << 31) - 1), down = (-(1 << 31));
vector<long long> v1, v2;
for (i = 0; i < q; i++) {
cin >> str >> n >> ch;
if (str == ">")
if (ch == 'N')
v1.push_back(n + 1);
else
v2.push_back(n);
else if (str == ">=")
if (ch == 'N')
v1.push_back(n);
else
v2.push_back(n - 1);
else if (str == "<")
if (ch == 'N')
v2.push_back(n - 1);
else
v1.push_back(n);
else if (str == "<=")
if (ch == 'N')
v2.push_back(n);
else
v1.push_back(n + 1);
}
for (i = 0; i < (int)v1.size(); i++) upper = min(upper, v1[i]);
for (i = 0; i < (int)v2.size(); i++) down = max(down, v2[i]);
if (upper == ((1 << 31) - 1) && down == (-(1 << 31)))
cout << "1" << endl;
else if (upper == ((1 << 31) - 1))
cout << down + 1 << endl;
else if (down == (-(1 << 31)))
cout << upper - 1 << endl;
else {
if ((down + 1) > (upper - 1))
cout << "Impossible" << endl;
else
cout << down + 1 << endl;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int M = 4e5 + 5;
pair<int, int> a[M];
int countt[M];
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int tot = 0;
for (int l, r, i = 1; i <= n; i++) {
scanf("%d%d", &l, &r);
a[tot++] = make_pair(l, -i);
a[tot++] = make_pair(r, i);
countt[i] = 0;
}
sort(a, a + tot);
int ans = 0;
multiset<int> b;
for (int i = 0; i < 2 * n; i++) {
if (a[i].second < 0)
b.insert(-a[i].second);
else
b.erase(b.find(a[i].second));
if (b.size() == 0) ans++;
if (b.size() == 1 && a[i].second > 0)
if (a[i + 1].second < 0)
if (a[i].first < a[i + 1].first) countt[*b.begin()]++;
if (b.size() == 1 && a[i].second < 0 && a[i + 1].second > 0)
countt[*b.begin()]--;
}
int sum = -1;
for (int i = 1; i <= n; i++) sum = max(sum, countt[i]);
printf("%d\n", ans + sum);
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 1007681537;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
long long r = sqrt(k) + 1;
while (r * r > k) r--;
return r;
}
inline long long icbrt(long long k) {
long long r = cbrt(k) + 1;
while (r * r * r > k) r--;
return r;
}
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; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
const int maxc = 62;
struct node_t {
node_t *p, *l, *r;
int key, size;
node_t* mx[maxc];
node_t(int key) : p(0), l(0), r(0), key(key), size(1) {
for (int i = (0); i < (maxc); i++) mx[i] = 0;
mx[key] = this;
}
};
int size(node_t* x) { return x ? x->size : 0; }
int isrt(node_t* x) { return !(x->p) || (x->p->l != x && x->p->r != x); }
int left(node_t* x) { return x->p->l == x; }
void setchild(node_t* x, node_t* p, int l) {
(l ? p->l : p->r) = x;
if (x) x->p = p;
}
void push(node_t* x) {}
void pull(node_t* x) {
x->size = size(x->l) + 1 + size(x->r);
for (int i = (0); i < (maxc); i++) {
x->mx[i] = 0;
}
x->mx[x->key] = x;
for (int i = (0); i < (maxc); i++) {
if (x->l) {
chkmax(x->mx[i], x->l->mx[i]);
}
if (x->r) {
chkmax(x->mx[i], x->r->mx[i]);
}
}
}
void rotate(node_t* x) {
node_t *p = x->p, *g = p->p;
int l = left(x);
setchild(l ? x->r : x->l, p, l);
if (!isrt(p))
setchild(x, g, left(p));
else
x->p = g;
setchild(p, x, !l);
pull(p);
}
node_t* splay(node_t* x) {
push(x);
while (!isrt(x)) {
node_t *p = x->p, *g = p->p;
if (g) push(g);
push(p), push(x);
if (!isrt(p)) rotate(left(x) != left(p) ? x : p);
rotate(x);
}
pull(x);
return x;
}
node_t* join(node_t* x, node_t* y) {
if (!x) return y;
while (x->r) push(x), x = x->r;
push(x);
setchild(y, x, 0);
return splay(x);
}
void split(node_t* t, node_t*& x, node_t*& y, int pos) {
if (pos < 0) {
x = 0, y = t;
return;
}
if (pos == size(t) - 1) {
x = t, y = 0;
return;
}
while (size(t->l) != pos) {
push(t);
if (size(t->l) > pos) {
t = t->l;
} else {
pos -= size(t->l) + 1;
t = t->r;
}
}
splay(t);
x = t;
y = x->r;
x->r = y->p = 0;
pull(x);
}
void split(node_t* t, node_t*& x, node_t*& y, node_t*& z, int l, int r) {
split(t, x, y, l - 1), split(y, y, z, r - l);
}
map<char, int> hs;
char rb[maxc];
void trace(node_t* x) {
if (!x) return;
push(x);
trace(x->l);
cout << char(rb[x->key]);
trace(x->r);
}
void solve() {
int ptr = 0;
for (int i = ('a'); i < ('z' + 1); i++) hs[i] = ptr++, rb[ptr - 1] = i;
for (int i = ('A'); i < ('Z' + 1); i++) hs[i] = ptr++, rb[ptr - 1] = i;
for (int i = ('0'); i < ('9' + 1); i++) hs[i] = ptr++, rb[ptr - 1] = i;
int n, m;
cin >> n >> m;
string s;
cin >> s;
node_t* rt = 0;
for (int i = (0); i < (int((s).size())); i++) {
int c = s[i];
node_t* x = new node_t(hs[c]);
rt = join(rt, x);
}
while (m--) {
int l, r;
char c;
cin >> l >> r >> c;
l--, r--;
int ic = hs[c];
node_t *x, *y, *z;
split(rt, x, y, z, l, r);
while (y && y->mx[ic]) {
node_t* w = y->mx[ic];
splay(w);
if (w->l) w->l->p = 0;
if (w->r) w->r->p = 0;
y = join(w->l, w->r);
}
rt = join(x, join(y, z));
}
trace(rt);
cout << "\n";
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
if (argc > 1) {
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
assert(freopen(argv[2], "wb", stdout));
}
solve();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int n, k, x, dy, y;
void p(int x, int y) { printf("%d %d\n", x, y); }
int main() {
scanf("%d%d", &n, &k);
if (k == 3) {
if (n == 3) {
printf("0 0\n0 1\n1 0\n");
return 0;
}
if (n == 4) {
printf("0 0\n0 100\n100 0\n1 1\n");
return 0;
}
printf("-1");
return 0;
}
p(0, 0);
n--;
k--;
dy = 300;
x = 0;
y = 0;
while (k > 0) {
x--;
dy--;
p(x, y + dy);
k--;
n--;
if (k == 0) break;
p(x, -y - dy);
k--;
n--;
y += dy;
}
dy = 300;
x = 100;
y = 0;
while (n > 0) {
x++;
dy--;
p(x, y + dy);
n--;
if (n == 0) break;
p(x, -y - dy);
n--;
y += dy;
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5005;
const int MAX1 = 200005;
const int LOGMAX = 25;
const double eps = 1e-11;
const int oo = 1e9 + 7;
const long long mod = 987654321;
int n, p;
vector<int> g[1005];
int cas;
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
cin >> cas;
while (cas--) {
cin >> n >> p;
int tot = 1;
g[1].push_back(2);
cout << 1 << ' ' << 2 << endl;
for (int i = 3; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (tot < (2 * i + p)) {
cout << j << " " << i << endl;
g[j].push_back(i);
tot++;
}
}
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int h[101];
queue<int> q;
int main() {
ios_base::sync_with_stdio(false);
int n, k, temp, ans = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> temp;
h[temp]++;
}
if (h[k] == n) {
cout << "0";
return 0;
}
if (n == 1) {
cout << k - temp;
return 0;
}
while (h[k] != n) {
ans++;
for (int i = 1; i + 1 <= k; i++) {
if (h[i]) {
h[i]--;
q.push(i);
}
}
while (!q.empty()) {
h[q.front() + 1]++;
q.pop();
}
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long x, y;
float k;
int main() {
cin >> x >> y;
k = sqrt(x * x + y * y);
if (ceil(k) > k) {
int z = (int)(ceil(k));
if (x * y > 0 == (z & 1))
cout << "black";
else
cout << "white";
} else
cout << "black";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int ans, sum, tmp;
int MAX, MIN;
int main() {
long long a, b, x1, y1, x2, y2;
cin >> a >> b >> x1 >> y1 >> x2 >> y2;
long long dx, dy;
dx = abs((x1 + y1) / (2 * a) - (x2 + y2) / (2 * a));
if ((x1 + y1) * (x2 + y2) < 0) dx++;
dy = abs((x1 - y1) / (2 * b) - (x2 - y2) / (2 * b));
if ((x1 - y1) * (x2 - y2) < 0) dy++;
cout << max(dx, dy);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[6][26];
a[0][0] = '+';
a[5][0] = '+';
for (int i = 1; i < 26; i++) {
a[0][i] = '-';
a[5][i] = '-';
}
a[0][25] = '+';
a[5][25] = '+';
for (int i = 1; i < 5; i++) {
a[i][0] = '|';
a[i][25] = '|';
}
a[1][24] = 'D';
a[2][24] = '.';
a[3][24] = '.';
a[4][24] = '.';
for (int i = 1; i <= 4; i++) {
for (int j = 2; j <= 22; j = j + 2) a[i][j] = '.';
}
a[1][23] = '|';
a[2][23] = '|';
a[3][23] = '.';
a[4][23] = '|';
for (int i = 2; i <= 23; i++) a[3][i] = '.';
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 21; j = j + 2) a[i][j] = '#';
}
for (int i = 2; i <= 24; i++) a[3][i] = '.';
int n;
cin >> n;
int x = 4;
if (n <= 4) x = n;
for (int i = 1; i <= x; i++) a[i][1] = 'O';
if (n > 4) {
n = n - 4;
int w = n / 3;
int l = 3 + (w - 1) * 2;
for (int i = 3; i <= l; i = i + 2) {
a[1][i] = 'O';
a[2][i] = 'O';
a[4][i] = 'O';
}
int rem = n % 3;
if (rem == 1) a[1][l + 2] = 'O';
if (rem == 2) {
a[1][l + 2] = 'O';
a[2][l + 2] = 'O';
}
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 26; j++) {
cout << a[i][j];
}
if (i == 1 || i == 4) cout << ")";
cout << endl;
}
return 0;
}
| 3 |
#include <iostream>
using namespace std;
int T;
int n;
int main() {
cin>>T;
while (T--) {
cin>>n;
int cnt = 0;
while (n) {
n >>= 1; cnt++;
}
n = 1; cnt--;
while (cnt--) {
n <<= 1;
}
cout<<n-1<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
char a[1005][1005];
int dist[5][1005][1005];
int n, m;
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
inline bool ok(int x, int y) {
if (x < 0) return false;
if (y < 0) return false;
if (x >= n) return false;
if (y >= m) return false;
if (a[x][y] == '#') return false;
return true;
}
void bfs(int num) {
int i, j;
static int quex[1000005];
static int quey[1000005];
int front = 0, rail = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a[i][j] == num + '0') {
quex[rail] = i;
quey[rail] = j;
dist[num][i][j] = 0;
rail++;
}
}
}
static int temp_quex[10000005];
static int temp_quey[10000005];
int t_front = 0, t_rail = 0;
for (; front < rail; front++) {
int nowx = quex[front];
int nowy = quey[front];
int i;
for (i = 0; i < 4; i++) {
int tx = nowx + d[i][0];
int ty = nowy + d[i][1];
if (ok(tx, ty)) {
if (dist[num][tx][ty] == -1) {
if (a[tx][ty] != '.') {
dist[num][tx][ty] = dist[num][nowx][nowy];
temp_quex[t_rail] = tx;
temp_quey[t_rail] = ty;
t_rail++;
} else {
dist[num][tx][ty] = dist[num][nowx][nowy] + 1;
quex[rail] = tx;
quey[rail] = ty;
rail++;
}
}
}
}
if (t_rail != t_front) {
quex[front] = temp_quex[t_front];
quey[front] = temp_quey[t_front];
t_front++;
front--;
}
}
}
int main() {
scanf("%d%d", &n, &m);
int i;
for (i = 0; i < n; i++) {
scanf("%s", a[i]);
}
memset(dist, -1, sizeof(dist));
bfs(1);
bfs(2);
bfs(3);
int min_ans = 999999999;
for (i = 0; i < n; i++) {
int j;
for (j = 0; j < m; j++) {
if (dist[1][i][j] == -1) continue;
if (dist[2][i][j] == -1) continue;
if (dist[3][i][j] == -1) continue;
int t = 0;
if (a[i][j] == '.') {
t = -2;
} else {
t = 0;
}
min_ans = min(min_ans, dist[1][i][j] + dist[2][i][j] + dist[3][i][j] + t);
}
}
if (min_ans == 999999999) {
puts("-1");
return 0;
}
printf("%d\n", min_ans);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string x;
while (cin >> n >> x) {
vector<int> all;
for (int i(1); i <= n; i++) {
if (n % i == 0) all.push_back(i);
}
for (int i(0); i < all.size(); i++) {
for (int j(0); j < all[i] / 2; j++) {
swap(x[j], x[all[i] - j - 1]);
}
}
cout << x << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
long long s, x;
int p1, p2, i, judge = 0;
scanf("%lld%lld", &s, &x);
if ((s - x) % 2 != 0)
printf("0\n");
else {
long long ans = 1;
if (s == x) judge = 1;
int flag = 1;
s = (s - x) / 2;
for (i = 0; i < 60; i++) {
p1 = (s >> i) & 1;
p2 = (x >> i) & 1;
if (p1 == 1 && p2 == 1) {
printf("0\n");
flag = 0;
break;
}
if (p2 == 1) ans *= 2;
}
if (flag) {
if (judge) ans -= 2;
printf("%lld\n", ans);
}
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long int MAX(long long int a, long long int b) { return a > b ? a : b; }
long long int MIN(long long int a, long long int b) { return a < b ? a : b; }
int main() {
long long int m, n, i, j;
scanf("%lld", &n), scanf("%lld", &m);
long long int min_ans, max_ans;
if (m > n) {
min_ans = 0;
max_ans = 0;
} else {
long long int m_temp = m;
long long int n_temp = n;
n_temp -= m_temp;
max_ans = ((n - m + 1) * (n - m)) / 2;
long long int quo = n / m;
long long int rem = n % m;
quo++;
min_ans =
rem * ((quo * (quo - 1)) / 2) + (m - rem) * ((quo - 1) * (quo - 2)) / 2;
}
cout << min_ans << " " << max_ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, d;
scanf("%d %d %d", &n, &a, &d);
double previous = 0;
while (n--) {
int t, v;
scanf("%d %d", &t, &v);
double next = 0;
double ti = v * 1.0 / a;
double di = a * ti * ti / 2.0;
if (di - d > 1e-9)
next = sqrt(d * 2.0 / a);
else
next = ti + (d - di) / v;
next += t;
if (previous - next > 1e-9) next = previous;
printf("%.12lf\n", next);
previous = next;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long x = 0, y = 0;
long long t = n * 2;
while (t--) {
long long xx, yy;
cin >> xx >> yy;
x += xx;
y += yy;
}
cout << x / n << " " << y / n << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct hull {
map<int, int> points;
long long area;
inline hull() {
points.clear();
area = 0;
}
inline map<int, int>::iterator prev(map<int, int>::iterator it) {
return it == points.begin() ? it : --it;
}
inline map<int, int>::iterator next(map<int, int>::iterator it) {
return it == points.end() ? it : ++it;
}
inline long long traparea(long long x1, long long y1, long long x2,
long long y2) {
return (x2 - x1) * (y1 + y2);
}
inline long long triarea(long long x1, long long y1, long long x2,
long long y2, long long x3, long long y3) {
return traparea(x1, y1, x2, y2) + traparea(x2, y2, x3, y3) +
traparea(x3, y3, x1, y1);
}
inline long long traparea(map<int, int>::iterator a,
map<int, int>::iterator b) {
return traparea(a->first, a->second, b->first, b->second);
}
inline long long triarea(map<int, int>::iterator it) {
long long sum = 0;
if (it != points.begin()) sum += traparea(prev(it), it);
if (next(it) != points.end()) sum += traparea(it, next(it));
if (it != points.begin() && next(it) != points.end())
sum += traparea(next(it), prev(it));
return sum;
}
inline bool bad(map<int, int>::iterator it) {
if (points.size() < 3 || it == points.begin() || next(it) == points.end())
return false;
return triarea(it) <= 0;
}
inline bool insert(int x, int y) {
if (points.find(x) != points.end()) {
if (y <= points[x]) return false;
area -= triarea(points.find(x));
points.erase(x);
}
map<int, int>::iterator it = points.insert(make_pair(x, y)).first;
if (bad(it)) {
points.erase(it);
return false;
}
area += triarea(it);
while (bad(prev(it))) {
area -= triarea(prev(it));
points.erase(prev(it));
}
while (bad(next(it))) {
area -= triarea(next(it));
points.erase(next(it));
}
return true;
}
inline bool contains(int x, int y) {
map<int, int>::iterator first = points.begin(), last = prev(points.end());
if (x < first->first || x > last->first) return false;
if (x == first->first) return y <= first->second;
if (x == last->first) return y <= last->second;
map<int, int>::iterator it = points.lower_bound(x), p = prev(it);
int x1 = p->first, y1 = p->second, x2 = it->first, y2 = it->second;
return triarea(x1, y1, x, y, x2, y2) <= 0;
}
};
int Q;
hull upper, lower;
int main() {
scanf("%d", &Q);
while (Q-- > 0) {
int type, x, y;
scanf("%d %d %d", &type, &x, &y);
if (type == 1) {
upper.insert(x, y);
lower.insert(x, -y);
} else if (type == 2)
puts((upper.contains(x, y) && lower.contains(x, -y)) ? "YES" : "NO");
}
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e6 + 10;
constexpr int LG = 20;
constexpr int MOD = 1e9 + 7;
constexpr int MOD2 = 1e9 + 9;
int n, k, p[N], sz, cnt[N], mx;
bitset<N> foo;
bool mark[N];
void dfs(int v) {
mark[v] = true;
sz++;
if (!mark[p[v]]) dfs(p[v]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= n; i++) {
if (!mark[i]) {
sz = 0;
dfs(i);
cnt[sz]++;
}
}
int lim = k;
for (int i = 1; i <= n; i++) {
if (lim == 0) break;
for (int j = 0; j < cnt[i]; j++) {
if (lim >= i / 2) {
lim -= i / 2;
mx += 2 * (i / 2);
} else {
mx += 2 * lim;
lim = 0;
}
}
}
for (int i = 1; i <= n; i += 2) {
if (lim <= cnt[i]) {
mx += lim;
lim = 0;
break;
} else {
lim -= cnt[i];
mx += cnt[i];
}
}
foo.set(0);
for (int i = 1; i <= n; i++) {
if (cnt[i]) {
int p = 1;
while (p <= cnt[i]) {
cnt[i] -= p;
foo |= foo << (p * i);
p <<= 1;
}
foo |= foo << (cnt[i] * i);
}
}
if (foo[k])
cout << k << ' ';
else
cout << k + 1 << ' ';
cout << mx << '\n';
return 0;
}
| 18 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int n, Q, x, y, u, v, kk, cnt, rt, f[200005][257];
int a[200005], father[200005], depth[200005], top[200005], head[200005],
ls[200005 * 16], rs[200005 * 16], d[200005 * 16];
struct Tree {
int nxt, to;
} e[200005];
inline void link(int x, int y) {
e[++kk].nxt = head[x];
e[kk].to = y;
head[x] = kk;
}
void dfs(int u, int fa) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa) continue;
father[v] = u;
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
void insert(int &k, int x, int i) {
if (!k) k = ++cnt, d[k] = 0, ls[k] = 0, rs[k] = 0;
d[k]++;
if (i < 0) return;
int opt = x & (1 << i);
if (opt)
insert(rs[k], x, i - 1);
else
insert(ls[k], x, i - 1);
}
inline void init() {
cnt = 0;
rt = 0;
}
int query(int k, int x, int i) {
if (i < 0) return 0;
int opt = (1 << i) & x;
if (opt) {
if (d[ls[k]])
return (1 << i) + query(ls[k], x, i - 1);
else
return query(rs[k], x, i - 1);
} else {
if (d[rs[k]])
return (1 << i) + query(rs[k], x, i - 1);
else
return query(ls[k], x, i - 1);
}
}
inline int read() {
int w = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) w = w * 10 + ch - '0';
return w;
}
int main() {
n = read();
Q = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i < n; i++) {
x = read();
y = read();
link(x, y);
link(y, x);
}
depth[1] = 1;
dfs(1, -1);
for (int i = 1; i <= n; i++) {
int u = i;
init();
for (int j = 0; j < 256 && u; j++) {
int x = a[u] ^ j;
insert(rt, x, 15);
u = father[u];
}
top[i] = u;
for (int j = 0; j < 256; j++) {
int x = j * 256;
f[i][j] = query(rt, x, 15);
}
}
while (Q--) {
u = read();
v = read();
int i = 0;
int ans = a[v];
for (i = 0; depth[v] - 255 >= depth[u] && v; v = top[v], i++)
ans = max(ans, f[v][i]);
for (i = i * 256; depth[v] >= depth[u]; v = father[v], i++)
ans = max(ans, a[v] ^ i);
printf("%d\n", ans);
}
return 0;
}
| 24 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e2;
const double EPS = 1e-9;
const int INF = 1e9;
const double PI = acos(-1);
int n;
int ar[N + 5][5];
int sqr(int a) { return a * a; }
int dot(int idxi, int idxa, int idxb) {
int ans = 0;
for (int i = 0; i < 5; ++i) {
ans += (ar[idxa][i] - ar[idxi][i]) * (ar[idxb][i] - ar[idxi][i]);
}
return ans;
}
double len(int idxi, int idxa) { return sqrt(dot(idxi, idxa, idxa)); }
double angle(int idxi, int idxa, int idxb) {
return acos(1.0 * dot(idxi, idxa, idxb) / len(idxi, idxa) / len(idxi, idxb));
}
int main() {
scanf("%d", &n);
if (n >= N) {
puts("0");
return 0;
}
if (n <= 2) {
printf("%d\n", n);
for (int i = 1; i <= n; ++i) {
printf("%d\n", i);
}
return 0;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 5; ++j) {
scanf("%d", &ar[i][j]);
}
}
vector<int> v;
for (int i = 0; i < n; ++i) {
bool bisa = true;
for (int j = 0; j < n && bisa; ++j) {
if (i == j) continue;
for (int k = j + 1; k < n && bisa; ++k) {
if (k == i) continue;
double angl = fabs(angle(i, j, k));
if (PI - 2 * angl > EPS) {
bisa = false;
}
}
}
if (bisa) v.push_back(i);
}
printf("%d\n", (int)v.size());
for (int i = 0; i < v.size(); ++i) {
printf("%d\n", v[i] + 1);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
vector<string> univ;
long long int n, m;
string second[100];
void awesome(string s) {
if (s.size() >= m) {
univ.push_back(s);
return;
}
long long int i;
for (i = 97; i <= 122; i++) {
char ch = i;
string str = s + ch;
awesome(str);
}
}
void generate() {
long long int i, j, k;
for (i = 97; i <= 122; i++) {
string s = "";
s += (char)(i);
awesome(s);
}
}
void generate_efficient(string s) {
long long int i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r, sum, ans,
tmp, it, pos, avg, cnt;
for (j = 0; j < m; j++) {
for (i = 97; i <= 122; i++) {
char ch = (char)(i);
string s2 = s;
s2[j] = ch;
awesome(s2);
}
}
}
bool dora(string a, string b) {
long long int i, j, cnt = 0;
for (i = 0; i < m; i++)
if (a[i] != b[i]) cnt++;
if (cnt <= 1) return true;
return false;
}
long long int jugaad() {
long long int i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r, sum, ans,
tmp, it, pos, avg, cnt;
for (auto it : univ) {
flag = 0;
for (i = 0; i < n; i++) {
if (dora(it, second[i])) {
} else
flag = 1;
}
if (flag == 0) {
cout << it << "\n";
return 1;
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r, sum, ans,
tmp, it, pos, avg, cnt;
long long int t;
cin >> t;
while (t--) {
f = 0;
flag = 0;
ans = 0;
cnt = 0;
sum = 0;
mn = LLONG_MAX;
mx = LLONG_MIN;
string s;
cin >> n >> m;
v.clear();
univ.clear();
for (i = 0; i < n; i++) cin >> second[i];
generate_efficient(second[0]);
ans = jugaad();
if (ans == -1) cout << -1 << "\n";
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T checkmin(T &a, T b) {
return (a < b) ? a : a = b;
}
template <class T>
inline T checkmax(T &a, T b) {
return (a > b) ? a : a = b;
}
template <class T>
T GCD(T a, T b) {
if (a < 0) return GCD(-a, b);
if (b < 0) return GCD(a, -b);
return (a == 0) ? b : GCD(b % a, a);
}
template <class T>
T LCM(T a, T b) {
if (a < 0) return LCM(-a, b);
if (b < 0) return LCM(a, -b);
return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b;
}
template <class T>
T sqr(T X) {
return X * X;
}
namespace Poor {
const int MaxiN = 2005;
int N, Circle, Square;
int A[MaxiN][MaxiN], B[MaxiN][MaxiN], S[MaxiN][MaxiN];
bool Vis[MaxiN][MaxiN];
inline int Ask(int X1, int Y1, int X2, int Y2) {
return S[X2][Y2] - S[X1 - 1][Y2] - S[X2][Y1 - 1] + S[X1 - 1][Y1 - 1];
}
void Run() {
scanf("%d", &N);
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j) scanf("%d", &A[i + 1][j + 1]);
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j)
S[i][j] = S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1] + A[i][j];
Circle = Square = 0;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) {
B[i][j] = A[i][j];
}
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j)
if (B[i][j] && !Vis[i][j]) {
deque<pair<int, int> > Q;
Q.push_back(make_pair(i, j));
vector<pair<int, int> > Visitor;
Vis[i][j] = 1;
while (!Q.empty()) {
int CX = Q.front().first, CY = Q.front().second;
Q.pop_front();
Visitor.push_back(make_pair(CX, CY));
for (int a = -1; a <= 1; ++a)
for (int b = -1; b <= 1; ++b) {
int TX = CX + a, TY = CY + b;
if (TX >= 1 && TX <= N && TY >= 1 && TY <= N && !Vis[TX][TY] &&
B[TX][TY]) {
Vis[TX][TY] = 1;
Q.push_back(make_pair(TX, TY));
}
}
}
int MinX = INT_MAX, MaxX = INT_MIN;
int MinY = INT_MAX, MaxY = INT_MIN;
for (typeof(Visitor.begin()) it = Visitor.begin(); it != Visitor.end();
++it) {
checkmin(MinX, it->first);
checkmax(MaxX, it->first);
checkmin(MinY, it->second);
checkmax(MaxY, it->second);
}
if (MaxY - MinY <= 9 || MaxX - MinX <= 9) continue;
double X = 0, Y = 0;
for (typeof(Visitor.begin()) it = Visitor.begin(); it != Visitor.end();
++it)
X += it->first, Y += it->second;
X /= Visitor.size(), Y /= Visitor.size();
double R = min(min(fabs(X - MinX), fabs(MaxX - X)),
min(fabs(Y - MinY), fabs(MaxY - Y)));
int Count = 0;
for (typeof(Visitor.begin()) it = Visitor.begin(); it != Visitor.end();
++it)
if (sqr(X - it->first) + sqr(Y - it->second) <= sqr(R)) ++Count;
double Rate =
(double)Count / (double)((MaxX - MinX + 1) * (MaxY - MinY + 1));
double Rate2 = (double)Visitor.size() /
(double)((MaxX - MinX + 1) * (MaxY - MinY + 1));
if ((Count + 20 >= (int)Visitor.size() ||
(double)Count / (double)Visitor.size() > 0.92) &&
fabs(Rate - 3.1415926 / 4.0) < 0.1 &&
fabs(Rate2 - 3.1415926 / 4.0) < 0.1)
++Circle;
else
++Square;
}
cout << Circle << " " << Square << endl;
}
} // namespace Poor
int main() {
Poor::Run();
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
bool v[maxn];
vector<int> a, b;
vector<pair<int, int> > ans;
int main() {
int n, m, k, x;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++) {
scanf("%d", &x);
v[x] = 1;
}
if (k == n) {
printf("-1\n");
return 0;
}
for (int i = 1; i <= n; i++) {
if (v[i] == 0) {
a.push_back(i);
} else
b.push_back(i);
}
for (int i = 1; i < a.size() && m > 0; i++) {
ans.push_back(make_pair(a[0], a[i]));
m--;
}
for (int i = 0; i < b.size() && m > 0; i++) {
ans.push_back(make_pair(a[0], b[i]));
m--;
}
for (int i = 1; i < a.size() && m > 0; i++) {
for (int j = i + 1; j < a.size() && m > 0; j++) {
if (m > 0) ans.push_back(make_pair(a[i], a[j]));
m--;
if (m <= 0) break;
}
if (m <= 0) break;
}
for (int i = 1; i < b.size() && m > 0; i++) {
for (int j = i + 1; j < b.size() && m > 0; j++) {
if (m > 0) ans.push_back(make_pair(b[i], b[j]));
m--;
if (m <= 0) break;
}
if (m <= 0) break;
}
for (int i = 1; i < a.size() && m > 0; i++) {
if (m > 0) ans.push_back(make_pair(b[0], a[i]));
m--;
if (m <= 0) break;
}
for (int i = 1; i < b.size() && m > 0; i++) {
for (int j = 1; j < a.size() && m > 0; j++) {
if (m > 0) ans.push_back(make_pair(b[i], a[j]));
m--;
if (m <= 0) break;
}
if (m <= 0) break;
}
if (m > 0)
printf("-1\n");
else {
for (int i = 0; i < ans.size(); i++)
printf("%d %d\n", ans[i].first, ans[i].second);
}
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int const N = 1000100;
struct Node {
int lf, rg;
int res;
Node *l, *r;
Node() { lf = rg = res = 0; }
Node(char ch) {
lf = (ch == '(');
rg = (ch == ')');
res = 0;
}
};
Node* comb(Node* b, Node* c, Node* a = new Node()) {
int add = min(b->lf, c->rg);
a->res = b->res + c->res + add;
a->lf = b->lf + c->lf - add;
a->rg = b->rg + c->rg - add;
return a;
}
int n, m;
char s[N];
Node* root = new Node();
void build(Node* v = root, int l = 0, int r = n) {
if (r - l == 1) {
*v = Node(s[l]);
} else {
int m = (l + r) / 2;
v->l = new Node();
build(v->l, l, m);
v->r = new Node();
build(v->r, m, r);
comb(v->l, v->r, v);
}
}
Node* get(int L, int R, Node* v = root, int l = 0, int r = n) {
if (L <= l && r <= R) {
return v;
}
if (R <= l || r <= L) {
return new Node();
}
int m = (l + r) / 2;
return comb(get(L, R, v->l, l, m), get(L, R, v->r, m, r));
}
int main() {
scanf("%s", s);
n = strlen(s);
build();
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", 2 * get(l - 1, r)->res);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, m, cnt, head[100010], maxcol, maxsum, size[100010];
long long ans, dp[100010][21][3], f[21][3];
template <class T>
bool checkmax(T &x, T y) {
return x < y ? x = y, 1 : 0;
}
template <class T>
bool checkmin(T &x, T y) {
return x > y ? x = y, 1 : 0;
}
template <class T>
void read(T &x) {
char s = getchar();
bool flag = false;
x = 0;
while (s < '0' || s > '9') {
if (s == '-') flag = true;
s = getchar();
}
while (s >= '0' && s <= '9') x = (x << 3) + (x << 1) + s - '0', s = getchar();
x = flag ? -x : x;
}
struct E {
int to, nxt;
} edge[100010 << 1];
inline void add(int u, int v) {
edge[++cnt] = (E){v, head[u]};
head[u] = cnt;
}
void dfs(int u, int fa) {
size[u] = 1;
dp[u][0][0] = maxcol - 1;
dp[u][1][1] = 1;
dp[u][0][2] = m - maxcol;
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
memset(f, 0, sizeof(f));
for (int j = 0; j <= size[u]; j++) {
for (int k = 0; k <= size[v]; k++) {
if (j + k > maxsum) continue;
f[j + k][0] =
(f[j + k][0] +
(dp[u][j][0] * ((dp[v][k][0] + dp[v][k][1] + dp[v][k][2]) % mod) %
mod)) %
mod;
f[j + k][1] = (f[j + k][1] + dp[u][j][1] * dp[v][k][0] % mod) % mod;
f[j + k][2] =
(f[j + k][2] + dp[u][j][2] * (dp[v][k][2] + dp[v][k][0]) % mod) %
mod;
}
}
size[u] = min(size[u] + size[v], maxsum);
for (int j = 0; j <= size[u]; j++)
for (int k = 0; k <= 2; k++) dp[u][j][k] = f[j][k];
}
}
int main() {
read(n);
read(m);
for (int i = 1; i < n; i++) {
int x, y;
read(x);
read(y);
add(x, y);
add(y, x);
}
read(maxcol);
read(maxsum);
dfs(1, 0);
for (int i = 0; i <= maxsum; i++)
for (int j = 0; j <= 2; j++) ans = (ans + dp[1][i][j]) % mod;
printf("%lld", ans);
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int k;
cin >> n >> k;
vector<int> d;
int x;
for (int i = 0; i < n; i++) {
cin >> x;
d.push_back(x);
}
int q;
int p;
cin >> q;
for (int i = 0; i < q; i++) {
cin >> p;
int t1 = 0;
int t2 = 0;
int f = 0;
int min = k;
int flag = 0;
for (t1 = 0; t1 <= k; t1++) {
for (t2 = 0; t2 <= k - t1; t2++) {
for (int poi = (int)d.size() - 1; poi > -1; poi--) {
int val = p - t1 * d[poi];
int l = 0;
int r = (int)d.size() - 1;
f = 0;
while (l <= r) {
int m = (l + r) / 2;
if (t2 * d[m] == val) {
f = 1;
flag = 1;
break;
}
if (t2 * d[m] < val) {
l = m + 1;
} else
r = m - 1;
}
if (f) {
if (t1 + t2 < min) {
min = t1 + t2;
}
}
}
if (f) {
if (t1 + t2 < min) {
min = t1 + t2;
}
}
}
if (f) {
if (t1 + t2 < min) {
min = t1 + t2;
}
}
}
if (flag)
cout << min;
else
cout << -1;
cout << endl;
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int s;
int a[2 * 100000 + 2], p[2 * 100000 + 2], d[2 * 100000 + 2];
int f(int n, int m) {
if (n == s) {
return s;
}
if (p[n] == a[n])
return f(d[n], m);
else {
if (a[n] - p[n] > m) {
p[n] += m;
m = 0;
return n;
} else {
m -= (a[n] - p[n]);
p[n] = a[n];
if (n != s - 1)
d[n] = f(d[n + 1], m);
else
d[n] = s;
return d[n];
}
}
}
int main() {
int n;
cin >> n;
s = n;
for (int i = 0; i < n; i++) {
cin >> a[i];
p[i] = 0;
d[i] = i;
}
int m;
cin >> m;
int q;
for (int i = 0; i < m; i++) {
cin >> q;
if (q == 2) {
int x;
cin >> x;
cout << p[x - 1] << endl;
} else {
int x, y;
cin >> x >> y;
x--;
int u = f(x, y);
}
}
}
| 10 |
#include <bits/stdc++.h>
long long a[400060] = {0}, b[450], n, m = 0, j, k, l, i, t, r, x, y, z, q;
int main() {
scanf("%lld", &n);
scanf("%lld", &x);
for (i = 2; i <= n; i++) {
scanf("%lld", &y);
if (x == y) {
x = x;
} else {
if (x < y) {
q = x;
x = y;
y = q;
}
q = 1;
while (q != 0) {
q = x % y;
x = y;
y = q;
}
}
}
if (x == 1) {
printf("1");
return 0;
}
z = sqrt(x);
for (i = 1; i <= z; i++) {
if (x % i == 0) m++;
}
if (z * z == x)
m = m * 2 - 1;
else
m = m * 2;
printf("%lld", m);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, sum = 0;
scanf(" %d", &n);
for (int i = 1; i <= n; i++) {
scanf(" %d", &x);
sum += i * x;
}
printf("%d\n", sum);
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
if (n % 2 == 0)
cout << n / 2 << "\n";
else
cout << (-(n + 1)) / 2 << "\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, t;
cin >> n >> t;
if (n == 1 && t == 10) {
cout << -1;
return 0;
}
if (t == 10) {
cout << 1;
for (int i = 1; i <= n - 1; i++) {
cout << 0;
}
return 0;
}
for (int i = 1; i <= n; i++) {
cout << t;
}
}
| 2 |
#include <bits/stdc++.h>
using ll = int64_t;
const int MAX = 1e5;
int32_t main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
int n, k;
std::cin >> n >> k;
std::vector<int> number_count(MAX + 1), favorite_count(MAX + 1), h(k + 1);
int lim = k * n;
for (int i = 0; i < lim; i++) {
int x;
std::cin >> x;
number_count[x]++;
}
for (int i = 0; i < n; i++) {
int x;
std::cin >> x;
favorite_count[x]++;
}
for (int i = 1; i <= k; i++) std::cin >> h[i];
std::vector<std::vector<int>> dp(501, std::vector<int>(5001 + 1000));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n * (k); j++) {
for (int cnt = 0; cnt <= k; cnt++) {
dp[i + 1][j + cnt] = std::max(dp[i + 1][j + cnt], dp[i][j] + h[cnt]);
}
}
}
int ans = 0;
for (int i = 1; i <= MAX; i++) {
if (favorite_count[i] > 0) ans += dp[favorite_count[i]][number_count[i]];
}
std::cout << ans;
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> ma;
long long int pre[100009], k;
int tree[100009], n;
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += (idx & -idx);
}
}
int sum(int idx) {
int val = 0;
while (idx > 0) {
val += tree[idx];
idx -= (idx & -idx);
}
return val;
}
int calidx(long long int val) {
int idx = lower_bound(ma.begin(), ma.end(), val) - ma.begin();
return idx + 1;
}
bool check(long long int val) {
long long int ans = 0, tmp = val;
for (int i = int(0); i <= int(n - 1); i++) update(calidx(pre[i]), 1);
for (int i = int(0); i <= int(n - 1); i++) {
ans += sum(n) - sum(calidx(tmp) - 1);
update(calidx(pre[i]), -1);
tmp = val + pre[i];
}
if (ans >= k) return true;
return false;
}
int main() {
scanf("%d", &n);
scanf("%lld", &k);
for (int i = int(0); i <= int(n - 1); i++) scanf("%lld", &pre[i]);
for (int i = int(1); i <= int(n - 1); i++) pre[i] += pre[i - 1];
for (int i = int(0); i <= int(n - 1); i++) ma.push_back(pre[i]);
sort(ma.begin(), ma.end());
long long int low = -1 * 2000000000 * 1ll * 100000,
high = 2000000000 * 1ll * 100000;
while (low < high) {
long long int mid = (low + high + 1) / 2;
if (check(mid))
low = mid;
else
high = mid - 1;
}
printf("%lld\n", low);
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
long long b[300], pow2[300];
int main() {
pow2[0] = 1;
for (int i = 1; i <= 35; i++) {
b[i] = b[i - 1] * 4 + 1;
pow2[i] = pow2[i - 1] * 2;
}
int T;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
int n;
long long k, sum = 0;
bool yesans = 0;
scanf("%d %lld", &n, &k);
if (n >= 32) {
printf("YES %d\n", n - 1);
continue;
}
for (int x = n; x >= 0; x--) {
sum += pow2[n - x] - 1;
if (k >= sum && k <= (b[n] - (pow2[n - x + 1] - 1) * b[x])) {
printf("YES %d\n", x);
yesans = 1;
break;
}
}
if (!yesans) printf("NO\n");
}
return 0;
}
| 12 |
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <queue>
#include <iomanip>
#include <fstream>
#include <climits>
#include <cstdlib>
#include <stack>
using namespace std;
// g++ -std=c++17 a.cpp
typedef long long ll;
typedef long double lld;
int INF = INT_MAX-1;
ll mod = 1e9+7;
ll gcd(ll a , ll b)
{
if(b==0) return a;
a%=b;
return gcd(b,a);
}
void solve(){
int n,m;
cin>>n>>m;
vector<ll> a(n), b(m);
for (int i =0; i<n; i++) cin>>a[i];
for (int i =0; i<m; i++) cin>>b[i];
if (n == 1){
for (int i = 0; i<m; i++) cout<<a[0]+b[i]<<" ";
cout<<"\n";
return;
}
sort(a.begin(), a.end());
ll g = a[1] - a[0];
for (int i =1; i<n-1; i++) g = gcd(g, a[i+1] - a[i]);
for (int i =0; i<m;i ++){
cout<<gcd(gcd(a[0] + b[i], a[1] + b[i]), g)<<" ";
}
cout<<"\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
// ifstream in("promote.in");
// cin.rdbuf(in.rdbuf());
// ofstream out("promote.out");
// cout.rdbuf(out.rdbuf());
int q;
q = 1;
while(q--) solve();
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
struct node {
long long x, y;
} a[MAXN];
bool cmp(const node& a, const node& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int main() {
int n;
long long x1, x2, k, b;
scanf("%d%I64d%I64d", &n, &x1, &x2);
for (int i = 0; i < n; ++i) {
scanf("%I64d%I64d", &k, &b);
a[i].x = k * x1 + b;
a[i].y = k * x2 + b;
}
sort(a, a + n, cmp);
bool flag = false;
for (int i = 1; i < n; ++i)
if (a[i].y < a[i - 1].y) flag = true;
if (flag)
puts("YES");
else
puts("NO");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(228);
const int maxn = 2e5;
pair<int, int> a[maxn];
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(a, a + n);
reverse(a, a + n);
vector<int> ve;
long long ans = 0;
for (int i = 0; i < m * k; ++i) {
ans += a[i].first;
ve.push_back(a[i].second);
}
cout << ans << '\n';
sort(ve.begin(), ve.end());
int cnt = 0;
for (int i = 0; i < (int)ve.size(); ++i) {
++cnt;
if (cnt == m && i != (int)ve.size() - 1) {
cout << ve[i] + 1 << ' ';
cnt = 0;
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
char vowel[] = {'a', 'e', 'i', 'o', 'u'};
int check(char s) {
for (int i = 0; i < 5; i++) {
if (vowel[i] == s) return 1;
}
return 0;
}
int main() {
string s;
cin >> s;
if (!check(s[s.size() - 1]) && s[s.size() - 1] != 'n') {
printf("NO\n");
return 0;
}
s.push_back('a');
int len = s.size();
for (int i = 0; i < len - 1; i++) {
if (!check(s[i])) {
if (s[i] != 'n') {
if (!check(s[i + 1])) {
printf("NO\n");
return 0;
}
}
}
}
printf("YES\n");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct name {
long long a, b;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long i, n, m, x, y, l, r, L, R, j, coun;
cin >> n >> m >> x >> y;
long long b[m];
name a[n];
for (i = 0; i < n; i++) {
cin >> a[i].a;
a[i].b = -1;
}
for (i = 0; i < m; i++) cin >> b[i];
l = a[0].a - x;
r = a[n - 1].a + y;
L = b[0];
R = b[m - 1];
if (l > R)
cout << 0;
else if (r < L)
cout << 0;
else {
j = 0;
coun = 0;
for (i = 0; i < n; i++) {
while (j != m) {
if (b[j] <= (a[i].a + y) && b[j] >= (a[i].a - x)) {
a[i].b = j;
j++;
coun++;
break;
} else if (b[j] > a[i].a + y)
break;
else
j++;
}
}
if (coun > 0) {
cout << coun << endl;
for (i = 0; i < n; i++) {
if (a[i].b != -1) cout << i + 1 << " " << a[i].b + 1 << endl;
}
} else
cout << coun;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
const int M = 998244353;
int n, a[N];
long long fac[N], inv[N], f[N], ans;
long long pow_mod(long long a, int b) {
long long ans = 1;
a = a % M;
while (b) {
if (b & 1) ans = ans * a % M;
a = a * a % M;
b >>= 1;
}
return ans % M;
}
void pre() {
int maxn = 1e3 + 5;
fac[0] = 1;
for (int i = 1; i <= maxn; i++) fac[i] = fac[i - 1] * i % M;
inv[maxn] = pow_mod(fac[maxn], M - 2);
for (int i = maxn - 1; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % M;
}
long long calc(int n, int m) {
if (n < m) return 0;
return fac[n] * inv[n - m] % M * inv[m] % M;
}
int main() {
pre();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
long long ans = 0;
for (int i = n; i >= 1; i--) {
if (a[i] > 0 && n - i >= a[i])
f[i] = calc(n - i, a[i]);
else
continue;
for (int j = i + a[i] + 1; j <= n; j++) {
if (a[j] > 0 && j - i - 1 >= a[i])
f[i] = (f[i] + f[j] * calc(j - i - 1, a[i]) % M) % M;
}
ans = (ans + f[i]) % M;
}
printf("%lld\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto &i : a) cin >> i;
map<int, int> sums;
vector<int> pos(2 * k + 2, 0);
int ans = INT_MAX;
for (int i = 0; i < n / 2; ++i) {
pos[min(a[i], a[n - i - 1]) + 1]++;
pos[max(a[i], a[n - i - 1]) + k + 1]--;
sums[a[i] + a[n - i - 1]]++;
}
for (int i = 1; i <= 2 * k; ++i) {
pos[i] += pos[i - 1];
ans = min(ans, pos[i] + 2 * (n / 2 - pos[i]) - sums[i]);
}
cout << ans << '\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &t) {
os << "[";
for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
if (it != t.begin()) os << ",";
os << *it;
}
os << "]";
return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &t) {
os << "{";
for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
if (it != t.begin()) os << ",";
os << *it;
}
os << "}";
return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &t) {
os << "{";
for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
if (it != t.begin()) os << ",";
os << *it;
}
os << "}";
return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int N = 7000;
int T, n, m, y, b, r;
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
while (cin >> n >> m) {
vector<int> moes(N + 1, 1);
for (int i = 2; i * i <= N; i++) {
for (int j = i * i; j <= N; j += i * i) {
moes[j] = 0;
}
}
vector<bitset<N + 1>> divs(N + 1);
vector<bitset<N + 1>> tbls(N + 1);
for (int i = 1; i <= (int)(N); i++) {
for (int j = 1; j <= (int)(i); j++) {
if (i % j == 0) {
divs[i].set(j);
}
}
for (int j = 1; j * i <= N; j++) {
if (moes[j]) {
tbls[i].set(i * j);
}
}
}
vector<bitset<N + 1>> bits(n);
for (int _ = 0; _ < (int)(m); _++) {
int t, x, y, z, v;
cin >> t;
if (t == 1) {
cin >> x >> v;
x--;
bits[x] = divs[v];
} else if (t == 2) {
cin >> x >> y >> z;
x--;
y--;
z--;
bits[x] = bits[y] ^ bits[z];
} else if (t == 3) {
cin >> x >> y >> z;
x--;
y--;
z--;
bits[x] = bits[y] & bits[z];
} else if (t == 4) {
cin >> x >> v;
x--;
putchar('0' + ((bits[x] & tbls[v]).count() % 2));
}
}
puts("");
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void mul(T &a, T &b, int k) {
T c(k * k, 0);
for (int i = 0; i < k; ++i)
for (int j = 0; j < k; ++j) {
for (int t = 0; t < k; ++t)
c[i * k + j] =
(c[i * k + j] + a[i * k + t] * b[t * k + j]) % (998244353 - 1);
}
copy(c.begin(), c.end(), a.begin());
}
template <typename T>
int64_t power(T &a, int k, int n) {
T r(k * k);
for (int i = 0; i < k; ++i) r[i * k + i] = 1;
for (int t = 1; t <= n; t *= 2) {
if (t & n) mul(r, a, k);
mul(a, a, k);
}
return r[0];
}
int64_t power(int64_t x, int n) {
int64_t r = 1;
int t = 1;
for (int t = 1; t <= n; t *= 2) {
if (t & n) r = r * x % 998244353;
x = x * x % 998244353;
}
return r;
}
template <typename T>
int find(T &u, int x) {
int l = 0, r = u.size() - 1;
while (l < r) {
int i = (l + r) / 2;
if (u[i].first < x) {
l = i + 1;
} else {
r = i;
}
}
if (l < u.size() && x == u[l].first)
return u[l].second;
else
return -1;
}
int dlog(int64_t a, int64_t y) {
vector<pair<int64_t, int>> u(31596), v(31596);
int64_t x = power(a, 31596);
int64_t r;
int i, j;
r = x;
for (i = 0; i < 31596; ++i, r = r * x % 998244353)
u[i] = pair<int64_t, int>(r, i);
sort(u.begin(), u.end());
r = y * a % 998244353;
for (i = 0; i < 31596; ++i, r = r * a % 998244353)
if ((j = find(u, r)) != -1) break;
i++;
j++;
return (998244353 - 1 + 31596 * j - i) % (998244353 - 1);
}
int64_t gcd(int64_t a, int64_t b) {
while (a % b) {
int64_t c = a % b;
a = b;
b = c;
}
return b;
}
int64_t inverse(int64_t a, int64_t n) {
int64_t tmp;
int64_t t = 0, t1 = 1;
int64_t r = n, r1 = a;
while (r1 != 0) {
int64_t q = r / r1;
tmp = t - q * t1;
t = t1;
t1 = tmp;
tmp = r - q * r1;
r = r1;
r1 = tmp;
}
if (t < 0) t = t % n + n;
return t;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, m, k;
cin >> k;
vector<int64_t> a(k * k, 0);
for (int i = 0; i < k; ++i) cin >> a[i];
for (int i = 0; i < k - 1; ++i) a[k * (i + 1) + i] = 1;
cin >> n >> m;
int64_t e = power(a, k, n - k);
int64_t y = dlog(3, m);
if (e != 0) {
int64_t d = gcd(e, 998244353 - 1);
if (y % d != 0) {
cout << -1 << endl;
} else {
int r = y / d * inverse(e / d, (998244353 - 1) / d) % (998244353 - 1);
cout << power(3, r) << endl;
}
} else {
if (y != 0) {
cout << -1 << endl;
} else {
cout << 1 << endl;
}
}
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 + 10;
char s[maxN], t[maxN];
int cs[26], ct[26];
int chk() {
memset(cs, 0, sizeof(cs));
for (int i = 0; s[i]; i++) cs[s[i] - 'a']++;
memset(ct, 0, sizeof(ct));
for (int i = 0; t[i]; i++) ct[t[i] - 'a']++;
for (int i = 0; i < 26; i++)
if (ct[i] < cs[i]) return 0;
return 1;
}
int chk_auto() {
int ls = strlen(s), lt = strlen(t);
int i, j;
for (i = 0, j = 0; i < ls && j < lt;) {
if (s[i] == t[j])
i++, j++;
else
j++;
}
return (i == ls);
}
int chk_arr() {
memset(cs, 0, sizeof(cs));
for (int i = 0; s[i]; i++) cs[s[i] - 'a']++;
memset(ct, 0, sizeof(ct));
for (int i = 0; t[i]; i++) ct[t[i] - 'a']++;
for (int i = 0; i < 26; i++)
if (ct[i] > cs[i]) return 1;
return 0;
}
int main() {
cin >> t >> s;
if (!chk()) {
cout << "need tree" << endl;
return 0;
}
if (chk_auto()) {
cout << "automaton" << endl;
return 0;
}
if (chk_arr()) {
cout << "both" << endl;
return 0;
}
cout << "array" << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define mod 998244353
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a,n) a, a + n
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_io;
ll n, q, k; cin>>n>>q>>k;
ll arr[n];
for(ll i=0;i<n;i++){
cin>>arr[i];
}
ll ans[n];
memset(ans, 0, sizeof(ll)*n);
ans[0]+=arr[0]-1;
ans[n-1]+=k-arr[n-1];
for(ll i=0;i<n;i++){
if(i!=0 && arr[i]!=arr[i-1]){
ans[i]+=arr[i]-arr[i-1]-1;
}
if(i!=n-1 && arr[i]!=arr[i+1]){
ans[i]+=arr[i+1]-arr[i]-1;
}
}
for(ll i=1;i<n;i++){
ans[i]+=ans[i-1];
// cout<<ans[i]<<" ";
}
for(ll j=0;j<q;j++){
ll x, y; cin>>x>>y;
ll finalAns= ans[y-1];
if(x>=2){
finalAns-=ans[x-2];
}
if(x!=1){
finalAns+=arr[x-2];
}
if(y!=n){
finalAns+=k-arr[y]+1;
}
cout<<finalAns<<endl;
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
const int SIZEN = 2010;
char mp[SIZEN][SIZEN] = {0};
int N, M;
int r, s;
int x, y;
bool vis[SIZEN][SIZEN] = {0};
struct Node {
int x, y, l, r;
Node(int _, int __, int ___, int ____) {
x = _;
y = __;
l = ___;
r = ____;
}
Node() { ; }
};
deque<Node> q;
int main() {
scanf("%d%d%d%d%d%d", &N, &M, &r, &s, &x, &y);
for (int i = 1; i <= N; i++) scanf("%s", mp[i] + 1);
q.push_front(Node(r, s, x, y));
int ans = 0;
while (!q.empty()) {
Node D = q.front();
q.pop_front();
x = D.x;
y = D.y;
int l = D.l;
r = D.r;
if (vis[x][y]) continue;
vis[x][y] = true;
ans++;
if (x + 1 <= N && mp[x + 1][y] == '.' && !vis[x + 1][y])
q.push_front(Node(x + 1, y, l, r));
if (x - 1 >= 1 && mp[x - 1][y] == '.' && !vis[x - 1][y])
q.push_front(Node(x - 1, y, l, r));
if (l && y - 1 >= 1 && mp[x][y - 1] == '.' && !vis[x][y - 1])
q.push_back(Node(x, y - 1, l - 1, r));
if (r && y + 1 <= M && mp[x][y + 1] == '.' && !vis[x][y + 1])
q.push_back(Node(x, y + 1, l, r - 1));
}
printf("%d\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
int n, a[30], b[30], c[30], d[30];
bool dfs(int dep, int limit) {
if (a[dep] == n) {
printf("%d\n", dep - 1);
for (int i = 1; i < dep; i++) {
printf("lea e%cx, [", 'a' + i);
if (c[i] != 0) {
printf("e%cx + ", 'a' + b[i] - 1);
if (d[i] == 0)
printf("e%cx", 'a' + c[i] - 1);
else
printf("%d*e%cx", 1 << d[i], 'a' + c[i] - 1);
} else
printf("%d*e%cx", 1 << d[i], 'a' + b[i] - 1);
printf("]\n");
}
return (true);
}
if (dep > limit) return (false);
for (int i = 1; i <= dep; i++) {
for (int j = 1; j < 4; j++) {
a[dep + 1] = a[i] << j;
b[dep] = i;
c[dep] = 0;
d[dep] = j;
if (dfs(dep + 1, limit)) return (true);
}
for (int j = 1; j <= dep; j++)
for (int k = 0; k < 4; k++) {
a[dep + 1] = a[i] + (a[j] << k);
b[dep] = i;
c[dep] = j;
d[dep] = k;
if (dfs(dep + 1, limit)) return (true);
}
}
return (false);
}
int main() {
scanf("%d", &n);
a[1] = 1;
for (int i = 0; i <= 26; i++)
if (dfs(1, i)) break;
return (0);
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int x = 0;
bool f = 0;
char c;
for (; (c = getchar()) < '0' || c > '9'; f = c == '-')
;
for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';
x = (x << 3) + (x << 1) + c - '0')
;
return f ? -x : x;
}
struct dom {
int l, r, y;
} d[200005];
struct que {
int l, r, id;
} q[200005];
int sum[(1 << 20)], a[200005 << 1], res[200005];
int n, cnt, m, cur, lp, rp;
bool tag[(1 << 20)];
inline bool cmp(que x, que y) { return (x.l == y.l) ? x.r < y.r : x.l < y.l; }
inline void up(int x) { sum[x] = sum[(x << 1)] + sum[(x << 1 | 1)]; }
inline void pushdown(int x, int l, int r) {
if (!tag[x]) return;
tag[(x << 1)] = tag[(x << 1 | 1)] = 1;
tag[x] = 0;
sum[(x << 1)] = a[((l + r) >> 1)] - a[l - 1];
sum[(x << 1 | 1)] = a[r] - a[((l + r) >> 1)];
}
inline void update(int x, int l, int r, int A, int B) {
if (A <= l && B >= r) {
sum[x] = a[r] - a[l - 1];
tag[x] = 1;
return;
}
pushdown(x, l, r);
if (A <= ((l + r) >> 1)) update((x << 1), l, ((l + r) >> 1), A, B);
if (B > ((l + r) >> 1)) update((x << 1 | 1), ((l + r) >> 1) + 1, r, A, B);
up(x);
}
inline int query(int x, int l, int r, int a, int b) {
if (a <= l && b >= r) return sum[x];
pushdown(x, l, r);
if (b <= ((l + r) >> 1)) return query((x << 1), l, ((l + r) >> 1), a, b);
if (a > ((l + r) >> 1))
return query((x << 1 | 1), ((l + r) >> 1) + 1, r, a, b);
return (query((x << 1), l, ((l + r) >> 1), a, ((l + r) >> 1)) +
query((x << 1 | 1), ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, b));
}
int main() {
n = in();
for (int i = 1; i <= n; ++i) {
d[i].l = in();
d[i].y = in();
d[i].r = d[i].l + d[i].y;
a[++cnt] = d[i].l;
a[++cnt] = d[i].r;
}
sort(a + 1, a + cnt + 1);
cnt = unique(a + 1, a + cnt + 1) - a - 1;
m = in();
for (int i = 1; i <= m; ++i) {
q[i].l = in();
q[i].r = in();
q[i].id = i;
}
sort(q + 1, q + m + 1, cmp);
cur = n;
for (int i = m; i; --i) {
while (cur >= q[i].l) {
lp = lower_bound(a + 1, a + cnt + 1, d[cur].l) - a + 1;
rp = lower_bound(a + 1, a + cnt + 1, d[cur].r) - a;
update(1, 1, cnt - 1, lp, rp);
--cur;
}
int x = q[i].l, y = q[i].r;
lp = lower_bound(a + 1, a + cnt + 1, d[x].l) - a + 1;
rp = lower_bound(a + 1, a + cnt + 1, d[y].l) - a;
res[q[i].id] = d[y].l - d[x].l - query(1, 1, cnt - 1, lp, rp);
}
for (int i = 1; i <= m; ++i) printf("%d\n", res[i]);
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1640005;
struct pi {
int x, y, r;
} pp[maxn / 20];
double dis(int x1, int y1) {
double xx = x1;
double yy = y1;
return sqrt(xx * xx + yy * yy);
}
struct ppi {
double y;
int id;
} p1[maxn];
int be(double x, double y) {
if (x > y) return 1;
if (fabs(x - y) < 1e-8) return 1;
return 0;
}
int se(double y, double x) {
if (x > y) return 1;
if (fabs(x - y) < 1e-8) return 1;
return 0;
}
int cmp(ppi a, ppi b) {
if (a.y != b.y) return a.y < b.y;
return a.id > b.id;
}
int cnt;
int main() {
int n, d;
cin >> n >> d;
for (int i = 1; i <= n; i++) scanf("%d%d%d", &pp[i].x, &pp[i].y, &pp[i].r);
for (int i = 1; i <= n; i++) {
double x = dis(pp[i].x, pp[i].y);
int w = (x - pp[i].r - 1e-8);
w = w / d;
double ord;
if (pp[i].x == 0) {
if (pp[i].y > 0)
ord = 3.141592653589793 / 2;
else
ord = 3.141592653589793 * 3 / 2;
} else {
ord = atan((1.0 * pp[i].y) / (pp[i].x));
if (ord > 0) {
if (pp[i].y < 0) {
ord += 3.141592653589793;
}
} else if (ord < 0) {
if (pp[i].y > 0) {
ord += 3.141592653589793;
} else
ord += 2 * 3.141592653589793;
} else {
if (pp[i].x < 0) ord = 3.141592653589793;
}
}
for (int j = w; d * j <= x + pp[i].r + 1e-8; j++) {
if (j * d >= x - pp[i].r) {
if (fabs(x - pp[i].r - j * d) < 1e-8 ||
fabs(x + pp[i].r - j * d) < 1e-8) {
p1[cnt].y = ord;
p1[cnt++].id = 1;
p1[cnt].y = ord;
p1[cnt++].id = -1;
continue;
}
double xi = acos((x * x + 1.0 * j * d * j * d - pp[i].r * pp[i].r) /
(2 * x * j * d));
if (ord + xi > 2 * 3.141592653589793) {
p1[cnt].y = 0;
p1[cnt++].id = 1;
p1[cnt].y = ord + xi - 2 * 3.141592653589793;
p1[cnt++].id = -1;
p1[cnt].y = ord;
p1[cnt++].id = 1;
p1[cnt].y = 2 * 3.141592653589793;
p1[cnt++].id = -1;
} else {
p1[cnt].y = ord;
p1[cnt++].id = 1;
p1[cnt].y = ord + xi;
p1[cnt++].id = -1;
}
if (ord - xi < 0) {
if (fabs(ord) < 1e-8) {
p1[cnt].y = 2 * 3.141592653589793 - xi;
p1[cnt++].id = 1;
p1[cnt].y = 2 * 3.141592653589793;
p1[cnt++].id = -1;
continue;
}
p1[cnt].y = 0;
p1[cnt++].id = 1;
p1[cnt].y = ord - 1e-8;
p1[cnt++].id = -1;
p1[cnt].y = 2 * 3.141592653589793 + ord - xi;
p1[cnt++].id = 1;
p1[cnt].y = 2 * 3.141592653589793;
p1[cnt++].id = -1;
} else {
p1[cnt].y = ord - xi;
p1[cnt++].id = 1;
p1[cnt].y = ord - 1e-8;
p1[cnt++].id = -1;
}
}
}
}
sort(p1, p1 + cnt, cmp);
int ma = 0;
int no = 0;
for (int i = 0; i < cnt; i++) {
no += p1[i].id;
ma = max(ma, no);
}
cout << ma << endl;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, m = 0;
cin >> n;
int a[n];
for (i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
m = (a[n - 1] - a[0]) / 2;
cout << a[0] + m << endl;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int a, x;
scanf("%d", &a);
for (int i = 0; i < a; i++) {
scanf("%d", &x);
printf("%d\n", (x % 7) ? (x / 7 + 1) : (x / 7));
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Matrix {
long long data[2][2];
long long &operator()(int x, int y) { return data[x][y]; }
};
Matrix Multiply(Matrix &a, Matrix &b) {
Matrix c;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
c(i, j) = 0;
for (int k = 0; k < 2; ++k) c(i, j) += a(i, k) * b(k, j);
c(i, j) %= 1000000007;
}
}
return c;
}
Matrix Power(Matrix &a, long long n) {
Matrix p = a;
Matrix product;
product(0, 0) = 1;
product(0, 1) = 0;
product(1, 0) = 0;
product(1, 1) = 1;
for (int i = 0; i < 62; ++i) {
if (n & (1LL << i)) product = Multiply(p, product);
p = Multiply(p, p);
}
return product;
}
int main(int argc, char *argv[]) {
Matrix a;
a(0, 0) = 3;
a(0, 1) = 1;
a(1, 0) = 1;
a(1, 1) = 3;
long long n;
cin >> n;
Matrix r = Power(a, n);
cout << r(0, 0) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int l, k = 1;
cin >> l;
string s, temp, temp2, ans;
cin >> s;
ans = s;
int ch = min_element(s.begin(), s.end()) - s.begin();
for (int i = 0; i < l; i++) {
if (s[i] != s[ch]) continue;
temp = s.substr(i);
temp2 = s.substr(0, i);
if ((i % 2 == 0 && l % 2 != 0) || (i % 2 != 0 && l % 2 == 0))
reverse(temp2.begin(), temp2.end());
temp = temp + temp2;
if (temp < ans) {
ans = temp;
k = i + 1;
}
}
cout << ans << endl << k << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], nxt[N][31] = {0};
int maxx[N][20];
int Log2[N];
int GetMaxx(int l, int r) {
int len = Log2[r - l + 1];
return max(maxx[l][len], maxx[r - (1 << len) + 1][len]);
}
int Solve(int start, int l, int r, int val) {
while (l <= r) {
int mid = (l + r) / 2;
if (GetMaxx(start, mid) >= val)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
int main() {
Log2[1] = 0;
for (int i = 2; i < N; i++) {
Log2[i] = Log2[i / 2] + 1;
}
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
maxx[i][0] = a[i];
}
for (int len = 1; (1 << len) <= n; len++)
for (int i = 1; i + (1 << len) - 1 <= n; i++)
maxx[i][len] = max(maxx[i][len - 1], maxx[i + (1 << (len - 1))][len - 1]);
long long res = 1ll * (n + 1) * n / 2;
for (int i = 0; i <= 30; i++) nxt[n][i] = n + 1;
for (int i = n - 1; i >= 1; i--) {
for (int j = 0; j <= 30; j++) {
nxt[i][j] = nxt[i + 1][j];
if ((1 << j) & a[i + 1]) nxt[i][j] = i + 1;
}
}
for (int l = 1; l <= n; l++) {
int r = l, now = a[l];
while (r <= n) {
int nxt_r = n + 1;
for (int j = 0; j <= 30; j++) {
if (now & (1 << j)) continue;
if (nxt[r][j] < nxt_r) nxt_r = nxt[r][j];
}
int t = Solve(l, r, nxt_r - 1, now);
res -= nxt_r - t;
now |= a[nxt_r];
r = nxt_r;
}
}
printf("%lld\n", res);
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int a[n];
int c = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int j = 0;
while (a[j] <= k && j < n) {
c++;
j++;
}
int m = n - 1;
while (a[m] <= k && m >= j) {
c++;
m--;
}
cout << c << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const int inf = 1e9;
namespace Flow {
const int maxn = 3e4;
const int maxm = 3e4;
int hd[maxn], dis[maxn], pre[maxn], preE[maxn], inq[maxn], tl = 0, MinCost = 0,
MaxFlow = 0, s, t;
struct edge {
int v, w, c, nxt, rev;
} E[maxm << 1];
int add_edge_private(int u, int v, int w, int c) {
E[++tl].v = v;
E[tl].w = w;
E[tl].c = c;
E[tl].nxt = hd[u];
hd[u] = tl;
return tl;
}
int full(int x) { return E[x].c == 0; }
int addEdge(int u, int v, int c, int w) {
int d1 = add_edge_private(u, v, w, c);
int d2 = add_edge_private(v, u, -w, 0);
E[d1].rev = d2;
E[d2].rev = d1;
return d1;
}
int bellman_ford() {
std::memset(dis, 127, sizeof(dis));
std::memset(inq, 0, sizeof(inq));
std::queue<int> q;
q.push(s);
dis[s] = 0;
inq[s] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = hd[u]; i; i = E[i].nxt) {
int v = E[i].v;
if (E[i].c == 0) continue;
if (dis[v] > dis[u] + E[i].w) {
dis[v] = dis[u] + E[i].w;
pre[v] = u;
preE[v] = i;
if (!inq[v]) {
q.push(v);
inq[v] = 1;
}
}
}
inq[u] = 0;
}
return dis[t] < dis[0];
}
int work() {
while (bellman_ford()) {
int f = inf;
int u = t;
while (u != s) {
f = std::min(f, E[preE[u]].c);
u = pre[u];
}
u = t;
MaxFlow += f;
while (u != s) {
MinCost += E[preE[u]].w * f;
E[preE[u]].c -= f;
E[E[preE[u]].rev].c += f;
u = pre[u];
}
}
return MinCost;
}
} // namespace Flow
int n1, n2, m, r, b, d[2005], u, v, R[405], B[405];
char s1[405], s2[405];
std::vector<int> c;
int main() {
scanf("%d%d%d%d%d", &n1, &n2, &m, &r, &b);
scanf("%s%s", s1 + 1, s2 + 1);
int s = n1 + n2 + 1;
int t = s + 1;
int SuperS = t + 1;
int SuperT = t + 2;
Flow::s = SuperS;
Flow::t = SuperT;
for (int i = 1; i <= n1; ++i) {
if (s1[i] == 'R') {
Flow::addEdge(s, i, inf, 0);
c.push_back(Flow::addEdge(SuperS, i, 1, 0));
d[s]++;
} else if (s1[i] == 'B') {
Flow::addEdge(i, t, inf, 0);
c.push_back(Flow::addEdge(i, SuperT, 1, 0));
d[t]++;
} else {
Flow::addEdge(s, i, inf, 0);
Flow::addEdge(i, t, inf, 0);
}
}
for (int i = 1; i <= n2; ++i) {
if (s2[i] == 'B') {
Flow::addEdge(s, i + n1, inf, 0);
c.push_back(Flow::addEdge(SuperS, i + n1, 1, 0));
d[s]++;
} else if (s2[i] == 'R') {
Flow::addEdge(i + n1, t, inf, 0);
c.push_back(Flow::addEdge(i + n1, SuperT, 1, 0));
d[t]++;
} else {
Flow::addEdge(s, i + n1, inf, 0);
Flow::addEdge(i + n1, t, inf, 0);
}
}
Flow::addEdge(t, s, inf, 0);
Flow::addEdge(s, SuperT, d[s], 0);
Flow::addEdge(SuperS, t, d[t], 0);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u, &v);
R[i] = Flow::addEdge(u, v + n1, 1, r);
B[i] = Flow::addEdge(v + n1, u, 1, b);
}
Flow::work();
for (auto x : c) {
if (!Flow::full(x)) {
puts("-1");
return 0;
}
}
printf("%d\n", Flow::MinCost);
for (int i = 1; i <= m; ++i) {
if (Flow::full(R[i]))
printf("R");
else if (Flow::full(B[i]))
printf("B");
else
printf("U");
}
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
complex<double> p[1000];
complex<double> u, v;
complex<double> v1, v2;
int n;
double a1, b1, c1;
double a2, b2, c2;
double det, XX1, YY1;
double l[1000];
double uu[1000];
void check() {
p[n + 1] = p[1];
p[0] = p[n];
for (int i = 1; i <= n; ++i) {
complex<double> v1 = p[i + 1] - p[i];
complex<double> v2 = p[i] - p[i - 1];
uu[i] = arg(v2 / v1) / 3.1415926535897932384626433832795 * 180;
l[i] = abs(v1);
printf("len = %.5f arg=%f\n", abs(p[i + 1] - p[i]),
arg(v2 / v1) / 3.1415926535897932384626433832795 * 180);
if (abs(uu[i] - 360.0 / n) < 1e-3) printf("uuu!\n");
};
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
if (abs(l[i] - l[j]) < 1e-3) printf("lll!\n");
}
int main() {
scanf("%i", &n);
if (n < 5)
printf("No solution\n");
else {
u = complex<double>(cos(2.0 * 3.1415926535897932384626433832795 / n),
sin(2.0 * 3.1415926535897932384626433832795 / n));
v = complex<double>(690, 0.0);
p[1] = complex<double>(0.0, 0.0);
for (int i = 2; i <= n - 1; ++i) {
p[i] = p[i - 1] + v;
v = v * u;
v = v * (abs(v) - 0.01) / abs(v);
}
v1 = v;
v2 = v * u;
a1 = v1.real();
b1 = v2.real();
c1 = p[1].real() - p[n - 1].real();
a2 = v1.imag();
b2 = v2.imag();
c2 = p[1].imag() - p[n - 1].imag();
det = a1 * b2 - a2 * b1;
XX1 = (c1 * b2 - c2 * b1) / det;
YY1 = (a1 * c2 - a2 * c1) / det;
p[n] = p[n - 1] + XX1 * v1;
p[n + 1] = p[n];
for (int i = 1; i <= n; ++i)
printf("%.9f %.9f\n", p[i].real(), p[i].imag());
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3 * 100000 + 5;
int a[maxn];
bool vis[maxn];
int ret[maxn];
int pnt[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!vis[i]) {
for (int j = i + 1; j <= n; j++)
if (a[j] % a[i] == 0)
vis[j] = 1;
else {
i = j - 1;
break;
}
}
memset(ret, 0, sizeof(ret));
int ans = 0, cnt = 0;
for (int i = n; i >= 1; i--)
if (vis[i])
cnt++;
else {
for (int j = i; j >= 1; j--)
if (a[j] % a[i] == 0) {
cnt++;
if (j == 1) {
pnt[i] = 1;
break;
}
} else {
pnt[i] = j + 1;
break;
}
ans = max(ans, cnt - 1);
ret[i] = cnt - 1;
cnt = 0;
}
int num = 0;
for (int i = 1; i <= n; i++)
if (ans == ret[i]) num++;
printf("%d %d\n", num, ans);
int p = 0;
for (int i = 1; i <= n; i++)
if (ans == ret[i]) printf("%d ", pnt[i]);
printf("\n");
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, p, flag = 0, q, r;
cin >> n;
map<int, int> m;
for (int i = 0; i < n; i++) {
cin >> p;
m[i + 1] = p;
}
for (int i = 1; i <= n; i++) {
p = m[i];
q = m[p];
r = m[q];
if (r == i) {
flag = 1;
break;
}
}
if (flag == 0)
cout << "NO";
else
cout << "YES";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, q, k, win, tot, sm, a[200001], sz[200001];
vector<int> G[200001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> sz[i];
G[a[i]].push_back(i);
}
cin >> q;
while (q--) {
vector<int> v;
cin >> k;
for (int i = 1; i <= k; ++i) cin >> m, v.push_back(m);
int s = 0, e = n, md, ans = 0, fin = 0;
while (s <= e) {
md = 2;
md = (s + e) / 2;
sm = 0;
for (auto j : v)
sm += G[j].end() - lower_bound(G[j].begin(), G[j].end(), md);
if (n - md + 1 - sm)
ans = md, s = md + 1;
else
e = md - 1;
}
if (!ans) {
cout << "0 0\n";
continue;
}
s = 1, e = ans;
while (s <= e) {
md = (s + e) / 2;
sm = upper_bound(G[a[ans]].begin(), G[a[ans]].end(), ans) -
lower_bound(G[a[ans]].begin(), G[a[ans]].end(), md);
for (auto j : v)
sm += upper_bound(G[j].begin(), G[j].end(), ans) -
lower_bound(G[j].begin(), G[j].end(), md);
if (ans - md + 1 == sm)
fin = md, e = md - 1;
else
s = md + 1;
}
fin = *lower_bound(G[a[ans]].begin(), G[a[ans]].end(), fin);
cout << a[ans] << ' ' << sz[fin] << '\n';
}
}
| 12 |
Subsets and Splits