solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long INF = LLONG_MAX;
const long long base = 1e9;
const double PI = acos(-1);
const long long MOD = 1e9 + 7;
const long long N = 2e5 + 10;
vector<long long> g[N];
long long n, d[N];
bool vis[N];
void solve();
void bfs(long long s);
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
void solve() {
cin >> n;
for (long long i = 1, a; i <= n; ++i) {
cin >> a;
if (a > i) {
g[i].push_back(a);
}
}
for (long long i = 1; i <= n - 1; ++i) {
g[i].push_back(i + 1);
g[i + 1].push_back(i);
}
bfs(1);
for (long long i = 1; i <= n; ++i) {
cout << d[i] << " ";
}
cout << "\n";
return;
}
void bfs(long long s) {
queue<long long> q;
q.push(s);
vis[s] = true;
while (!q.empty()) {
long long v = q.front();
q.pop();
for (auto &x : g[v]) {
if (!vis[x]) {
vis[x] = true;
q.push(x);
d[x] = d[v] + 1;
}
}
}
return;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
void task_solver() {
long long n;
while (cin >> n) {
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
long long ans1 = 0, ans2 = 0;
long long right = 1e6, left = 1;
sort((a).begin(), (a).end());
for (long long i = 0; i < n; i++) {
if (a[i] <= 500000) {
ans1 = a[i];
left = a[i];
} else {
ans2 = max(1000000 - a[i], ans2);
right = a[i];
}
}
cout << max(ans1 - 1, ans2) << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
task_solver();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool o1;
long long n, m, P, a[505], b[3], f[505][505];
char s[505];
bool o2;
int main() {
cin >> n >> m >> P;
for (long long i = 1; i <= m; i++) {
scanf("%s", s + 1);
for (long long j = 1; j <= n; j++)
if (s[j] == '1') a[j]++;
}
for (long long i = 1; i <= n; i++) b[a[i]]++;
f[b[1]][b[0]] = 1;
for (long long j = n; j >= 0; j--) {
for (long long i = n; i >= 0; i--) {
if (j && i) (f[i][j - 1] += f[i][j] * j % P * i % P) %= P;
if (i - 2 >= 0) (f[i - 2][j] += i * (i - 1) / 2 % P * f[i][j] % P) %= P;
if (j - 2 >= 0)
(f[i + 2][j - 2] += j * (j - 1) / 2 % P * f[i][j] % P) %= P;
}
}
cout << f[0][0];
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > edges;
vector<int> orientation;
vector<int> g[100000];
int v[100000], counter = 0;
int source = 0;
bool dfs(int gdzie, int parent) {
if (v[gdzie] >= 0) {
return true;
}
int initial = counter;
v[gdzie] = counter++;
for (int i = 0; i < g[gdzie].size(); i++) {
if (orientation[g[gdzie][i]] == -1) {
if (edges[g[gdzie][i]].first == gdzie) {
orientation[g[gdzie][i]] = 2;
if (!dfs(edges[g[gdzie][i]].second, gdzie)) return false;
} else {
orientation[g[gdzie][i]] = 1;
if (!dfs(edges[g[gdzie][i]].first, gdzie)) return false;
}
}
int edge = edges[g[gdzie][i]].first;
if (edges[g[gdzie][i]].first == gdzie) {
edge = edges[g[gdzie][i]].second;
}
if (edge != parent && v[edge] < v[gdzie]) {
v[gdzie] = v[edge];
}
}
return (parent == -1 || v[gdzie] < initial);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int skad, dokad;
scanf("%d%d", &skad, &dokad);
edges.push_back(make_pair(skad - 1, dokad - 1));
orientation.push_back(-1);
g[skad - 1].push_back(orientation.size() - 1);
g[dokad - 1].push_back(orientation.size() - 1);
}
memset(v, -1, sizeof v);
if (!dfs(0, -1)) {
cout << "0" << endl;
} else {
for (int i = 0; i < m; i++) {
if (orientation[i] == 1) {
cout << edges[i].first + 1 << " " << edges[i].second + 1 << endl;
} else {
cout << edges[i].second + 1 << " " << edges[i].first + 1 << endl;
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
const int mod = 1e9 + 7, INF = 1 << 30;
const double EPS = 1e-12, PI = 3.1415926535897932384626;
const ll lmod = 1e9 + 7, LINF = 1LL << 60;
const int MAX_N = 200005;
template <typename T>
T inf;
template <>
constexpr int inf<int> = 1 << 30;
template <>
constexpr ll inf<ll> = 1LL << 60;
using Cost = ll;
using Node = int;
struct Edge {
Cost cost;
Node to;
Edge(Cost cost, Node to) : cost(cost), to(to) {}
};
using Graph = vector<vector<Edge>>;
vector<Cost> dijkstra(Graph &graph, vector<Cost> cost) {
using Pcn = pair<Cost, Node>;
priority_queue<Pcn, vector<Pcn>, greater<Pcn>> que;
vector<Cost> dist(graph.size(), inf<Cost>);
for (int i = 0; i < graph.size(); i++) {
dist[i] = cost[i];
que.push(Pcn(cost[i], i));
}
while (!que.empty()) {
Pcn p = que.top();
que.pop();
Node v = p.second;
if (dist[v] < p.first) continue;
for (Edge e : graph[v]) {
if (dist[v] + e.cost < dist[e.to]) {
dist[e.to] = dist[v] + e.cost;
que.push(Pcn(dist[e.to], e.to));
}
}
}
return dist;
}
int main() {
int N, M;
cin >> N >> M;
Graph g(N);
for (int i = 0; i < M; i++) {
int u, v;
ll c;
scanf("%d%d%lld", &u, &v, &c);
u--;
v--;
g[u].push_back(Edge(2LL * c, v));
g[v].push_back(Edge(2LL * c, u));
}
vector<Cost> co(N);
for (int i = 0; i < N; i++) scanf("%lld", &co[i]);
auto dist = move(dijkstra(g, co));
for (int i = 0; i < N; i++) cout << dist[i] << " ";
cout << "" << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
int R[2003][2], C[3002][2], A[3002][3002];
char S[3002][3002];
int main() {
int n, res = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%s", S[i]);
for (int i = 0; i < n; i++) {
for (int l = n - 1; l > i; l--) {
int x = (R[i][0] + C[l][0]) % 2;
if (x != S[i][l] - '0') R[i][0]++, C[l][0]++, res++;
}
}
for (int i = n - 1; i >= 0; i--) {
for (int l = 0; l < i; l++) {
int x = (R[i][1] + C[l][1]) % 2;
if (x != S[i][l] - '0') R[i][1]++, C[l][1]++, res++;
}
}
for (int i = 0; i < n; i++) {
int x = (R[i][0] + R[i][1] + C[i][0] + C[i][1]) % 2;
if (x != S[i][i] - '0') res++;
}
printf("%d\n", res);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long INF = 1LL << 60;
const long long mod = 1e9 + 7;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void solve() {
long long n, x, y;
cin >> n >> x >> y;
string a;
cin >> a;
long long cnt = 0;
for (long long i = 0; i < n; i++) {
if (i == 0 and a[i] == '0') ++cnt;
if (i != 0 and a[i - 1] == '1' and a[i] == '0') ++cnt;
}
if (cnt == 0) {
cout << 0 << endl;
return;
}
cout << min(y * cnt, y + x * (cnt - 1)) << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[10000];
int main() {
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int x = 0; x < n; ++x) {
cin >> a[x].first >> a[x].second;
}
sort(a, a + n);
int tang = m;
int time = 0;
int x = n - 1;
while (true) {
if (x < 0) break;
time += tang - a[x].first;
tang = a[x].first;
time = max(time, a[x].second);
tang = a[x].first;
while (x > 0 && a[x].first == a[x - 1].first) {
x--;
time = max(time, a[x].second);
}
x--;
}
cout << time + tang << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
int main() {
int i, j;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
if (n % 3 == 1) {
if (n / 3 < 2)
cout << -1 << endl;
else
cout << n / 3 - 2 << " " << 0 << " " << 1 << endl;
} else if (n % 3 == 2) {
if (n / 3 < 1)
cout << -1 << endl;
else
cout << n / 3 - 1 << " " << 1 << " " << 0 << endl;
} else
cout << n / 3 << " " << 0 << " " << 0 << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int sit, stand;
int m, i, t;
char s[205];
while (scanf("%d", &m) != EOF) {
scanf("%s", s);
sit = stand = 0;
for (i = 0; i < m; i++) {
if (s[i] == 'x')
sit++;
else
stand++;
}
if (sit - stand > 0) {
t = (sit - stand) / 2;
printf("%d\n", t);
for (i = 0; i < m; i++) {
if (s[i] == 'x' && t != 0) {
putchar(s[i] - 32);
t--;
} else
putchar(s[i]);
}
puts("");
} else if (stand - sit > 0) {
t = (stand - sit) / 2;
printf("%d\n", t);
for (i = 0; i < m; i++) {
if (s[i] == 'X' && t != 0) {
putchar(s[i] + 32);
t--;
} else
putchar(s[i]);
}
puts("");
} else {
printf("0\n");
printf("%s\n", s);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> read_int_array(int n) {
vector<int> arr;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
arr.push_back(temp);
}
return arr;
}
vector<long long int> read_llint_array(int n) {
vector<long long int> arr;
for (int i = 0; i < n; i++) {
long long int temp;
cin >> temp;
arr.push_back(temp);
}
return arr;
}
bool isprime(int n) {
if (n == 1 || n == 0) {
return false;
}
if (n == 2) {
return true;
}
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
vector<int> daily;
for (int i = 0; i < 7; i++) {
int p;
cin >> p;
daily.push_back(p);
}
int tot = accumulate(daily.begin(), daily.end(), 0);
if (tot == 1) {
for (int i = 0; i < 7; i++) {
if (daily[i] == 1) {
cout << i + 1 << endl;
return 0;
}
}
}
if (n % tot == 0) {
n = tot;
int i = -1;
while (n > 0) {
i++;
n -= daily[i];
}
cout << i + 1;
return 0;
} else {
n = n % tot;
int i = -1;
while (n > 0) {
i++;
n -= daily[i];
}
cout << i + 1;
return 0;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool intersecting(int c11, int c12, int c21, int c22);
int main() {
int n;
cin >> n;
vector<int> v(n, 0);
int flag = 0;
for (int i = 0; i < n; ++i) {
cin >> v[i];
if (!flag)
for (int j = 1; j < i; ++j)
if (intersecting(v[j], v[j - 1], v[i], v[i - 1])) flag = 1;
}
if (flag)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
bool intersecting(int c11, int c12, int c21, int c22) {
int c1min = c11, c1max = c12;
if (c1max < c1min) swap(c1min, c1max);
int c2min = c21, c2max = c22;
if (c2max < c2min) swap(c2min, c2max);
if ((c1min < c2min && c1max > c2min && c1max < c2max) ||
(c2min < c1min && c2max > c1min && c2max < c1max))
return true;
else
return false;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const long long MAX = (long long)1e5 + 10;
int main() {
string second;
cin >> second;
int n = second.size();
int h[26], d[26];
for (int i = 0; i < 26; i++) {
d[i] = 0;
h[i] = -1;
}
for (int i = 0; i < n; i++) {
if (d[second[i] - 'a'] < i - h[second[i] - 'a']) {
d[second[i] - 'a'] = i - h[second[i] - 'a'];
}
h[second[i] - 'a'] = i;
}
int minn = INF;
for (int i = 0; i < 26; i++) {
if (d[i] < n - h[i]) {
d[i] = n - h[i];
}
minn = min(minn, d[i]);
}
cout << minn << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 2e6 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const double PI = atan(1.0) * 4.0;
const long long mod = 1e9 + 7;
struct Node {
int id, val;
bool friend operator<(Node x, Node y) { return x.val < y.val; }
};
set<Node> s;
set<Node>::iterator it;
int n, m, k, ans[SIZE], col = 1;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1, x; i <= n; i++) {
scanf("%d", &x);
s.insert((Node){i, x});
}
while (s.size()) {
it = s.begin();
Node tmp = *it;
s.erase(it);
ans[tmp.id] = col;
int val = tmp.val;
while (1) {
tmp.val = val + k;
it = s.upper_bound(tmp);
if (it == s.end()) break;
tmp = *it;
s.erase(it);
ans[tmp.id] = col;
val = tmp.val;
}
col++;
}
printf("%d\n", col - 1);
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<T> &v) {
for (typename vector<T>::size_type i = 0; i < v.size(); ++i)
out << v[i] << " ";
out << "\n";
return out;
}
template <typename T, typename N>
std::ostream &operator<<(std::ostream &out, vector<pair<T, N> > &v) {
for (size_t i = 0; i < v.size(); ++i)
out << "(" << v[i].first << ", " << v[i].second << ") ";
out << "\n";
return out;
}
template <typename T>
std::ostream &operator<<(std::ostream &out, vector<vector<T> > &v) {
for (size_t i = 0; i < v.size(); ++i) {
for (size_t j = 0; j < v[i].size(); ++j) {
out << v[i][j] << " ";
}
out << "\n";
}
return out;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<vector<int> > assemblage(k, vector<int>());
int m;
for (int i = 0; i < k; ++i) {
cin >> m;
int a;
for (int j = 0; j < m; ++j) {
cin >> a;
assemblage[i].push_back(a);
}
}
int min_time = k;
for (int i = 0; i < k; ++i) {
if (assemblage[i][0] != 1) {
min_time += 2 * (assemblage[i].size() - 1);
} else {
for (int j = 0; j < (int)assemblage[i].size() - 1; ++j) {
if (assemblage[i][j + 1] != assemblage[i][j] + 1) {
min_time += 2 * (assemblage[i].size() - j - 1);
break;
}
}
}
}
cout << min_time - 1 << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ma = 360;
cin >> n;
int arr[n];
int s = 0;
int g = 0;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
s = 0;
if (i == n - 1) break;
for (int j = i; j < n + i; j++) {
s += arr[j % n];
g = 360 - s;
ma = min(ma, abs(g - s));
}
}
cout << ma << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void showpq(priority_queue<long long int> gq) {
priority_queue<long long int> g = gq;
while (!g.empty()) {
cout << '\t' << g.top();
g.pop();
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
long long int w[n];
vector<long long int> in;
map<long long int, long long int> mp;
for (long long int i = 0; i < n; i++) {
cin >> w[i];
in.push_back(w[i]);
mp[w[i]] = i + 1;
}
sort(in.begin(), in.end());
string s;
cin >> s;
long long int j = 0;
stack<long long int> s1;
for (long long int i = 0; i < s.length(); i++) {
if (s[i] == '0') {
cout << mp[in[j]] << " ";
long long int u = in[j];
s1.push(u);
j++;
} else {
long long int u = s1.top();
cout << mp[u] << " ";
s1.pop();
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 250005;
vector<int> g[maxn];
int pa[maxn];
int v[maxn];
int dep[maxn];
vector<int> leaf;
int n, m, k;
void build(int now) {
int x = 0, y = 0;
for (auto &i : g[now]) {
if (i == pa[now]) {
continue;
} else if (x == 0) {
x = i;
} else {
y = i;
}
}
vector<int> ans;
if ((dep[now] - dep[x] + 1) % 3) {
ans.push_back(now);
while (ans.back() != x) {
ans.push_back(pa[ans.back()]);
}
} else if ((dep[now] - dep[y] + 1) % 3) {
ans.push_back(now);
while (ans.back() != y) {
ans.push_back(pa[ans.back()]);
}
} else {
if (dep[x] < dep[y]) {
swap(x, y);
}
ans.push_back(now);
ans.push_back(x);
while (ans.back() != y) {
ans.push_back(pa[ans.back()]);
}
}
cout << ans.size() << '\n';
for (auto &i : ans) {
cout << i << ' ';
}
cout << '\n';
}
void dfs(int now) {
v[now] = 1;
int f = 0;
if (dep[now] * k >= n) {
cout << "PATH\n";
vector<int> ans{now};
while (ans.back() != 1) {
ans.push_back(pa[ans.back()]);
}
cout << ans.size() << '\n';
for (auto &i : ans) {
cout << i << ' ';
}
cout << '\n';
exit(0);
}
for (auto &i : g[now]) {
if (v[i]) {
continue;
}
dep[i] = dep[now] + 1;
pa[i] = now;
dfs(i);
f = 1;
}
if (!f) {
leaf.push_back(now);
}
}
void go() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dep[1] = 1;
dfs(1);
cout << "CYCLES\n";
for (int i = 0; i < k; i++) {
build(leaf[i]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int c = 0;
int t;
if (!c) {
t = 1;
} else {
cin >> t;
}
while (t--) {
go();
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int S = 555555;
vector<int> v[S];
int c[2][S], f[S], lz[S], sz[S], imp[S], n, ex[S], ey[S];
bool nr(int p) { return c[0][f[p]] == p || c[1][f[p]] == p; }
void up(int p) { sz[p] = sz[c[0][p]] + sz[c[1][p]] + imp[p]; }
void spin(int p) {
int F = f[p], G = f[F], d = c[1][F] == p, B = c[!d][p];
if (nr(F)) c[c[1][G] == F][G] = p;
c[!d][p] = F;
c[d][F] = B;
f[f[f[B] = F] = p] = G;
up(F);
}
void rev(int p) {
swap(c[0][p], c[1][p]);
lz[p] ^= 1;
}
void down(int p) {
if (lz[p]) rev(c[0][p]), rev(c[1][p]), lz[p] = 0;
}
void push(int p) {
if (nr(p)) push(f[p]);
down(p);
}
void splay(int p) {
push(p);
for (int F; F = f[p], nr(p); spin(p))
if (nr(F)) spin((c[1][f[F]] == F) == (c[1][F] == p) ? F : p);
up(p);
}
void access(int r) {
for (int p = r, y = 0; p; p = f[y = p]) splay(p), c[1][p] = y, up(p);
splay(r);
}
void make(int p) {
access(p);
rev(p);
}
void link(int a, int b) {
make(b);
f[b] = a;
}
void split(int a, int b) {
make(a);
access(b);
}
void cut(int a, int b) {
split(a, b);
c[0][b] = f[a] = 0;
up(b);
}
int find(int p) {
down(p);
return sz[c[0][p]] ? find(c[0][p]) : (p > n ? p : find(c[1][p]));
}
void dfs(int p, int fa) {
for (int i = 0, y; y = i < v[p].size() ? v[p][i] : 0; ++i)
if (y != fa) dfs(y, p);
if (p == 1) return;
split(p, fa);
splay(p);
int x = find(p);
printf("%d %d %d %d\n", fa, p, ex[x], ey[x]);
cut(ex[x], x);
cut(ey[x], x);
link(p, fa);
}
int main() {
scanf("%d", &n);
printf("%d\n", n - 1);
for (int i = 1, a, b; i < n; ++i)
scanf("%d%d", &a, &b), v[a].push_back(b), v[b].push_back(a);
for (int i = 1 + n, a, b; i < n + n; ++i)
scanf("%d%d", &ex[i], &ey[i]), imp[i] = 1, link(ex[i], i), link(i, ey[i]);
dfs(1, 0);
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
stringstream ssx, ssy;
for (int i = 0; i + 1 < n; i++) {
ssx << "(";
ssy << "(";
}
for (int i = 0; i < n; i++) {
int x, y, r;
cin >> x >> y >> r;
ssx << "(" << x / 2 << "*((1-abs((t-" << i << ")))+abs((abs((t-" << i
<< "))-1))))" << (i > 0 ? ")" : "") << (i + 1 < n ? "+" : "");
ssy << "(" << y / 2 << "*((1-abs((t-" << i << ")))+abs((abs((t-" << i
<< "))-1))))" << (i > 0 ? ")" : "") << (i + 1 < n ? "+" : "");
}
cout << ssx.str() << endl;
cout << ssy.str() << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 10;
long long a[Maxn], sum, ans, n;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
void init() {
scanf("%I64d", &n);
sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%I64d", &a[i]);
sum += a[i];
}
sort(a + 1, a + 1 + n);
}
void solve() {
ans = sum;
for (int i = 1; i <= n; i++) {
sum -= a[i];
ans += 2 * (sum - (n - i) * a[i]);
}
long long d = gcd(ans, n);
ans /= d, n /= d;
printf("%I64d %I64d\n", ans, n);
}
int main() {
init();
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
double power(float x, int y) {
float temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else {
if (y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
void solve() {
long long int n;
cin >> n;
vector<long long int> a(n);
for (long long int i = 0; i < n; i++) cin >> a[i];
;
long long int ans = a[0];
long long int t = a[0];
for (long long int i = 1; i < n; i++) {
t += a[i];
ans = min(ans, t);
}
cout << abs(ans) << endl;
}
int main() {
long long int t;
cin >> t;
while (t--) solve();
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T min3(T a, T b, T c) {
return min(a, min(b, c));
};
template <class T>
T max3(T a, T b, T c) {
return max(a, max(b, c));
};
void openFile() { freopen("/home/khaihanhdk/devplace/a.inp", "r", stdin); }
int sl[10000];
void pt(int x) {
if (x == 2)
sl[2]++;
else if (x == 3)
sl[3]++;
else if (x == 4) {
sl[3]++;
sl[2] += 2;
} else if (x == 5)
sl[5]++;
else if (x == 6) {
sl[5]++;
sl[3]++;
} else if (x == 7) {
sl[7]++;
} else if (x == 8) {
sl[7]++;
sl[2] += 3;
} else if (x == 9) {
sl[7]++;
sl[3] += 2;
sl[2]++;
}
}
char s[10000];
int main() {
int n;
scanf("%d", &n);
scanf("%s", &s);
for (int i = 0; i < n; i++) pt(s[i] - '0');
for (int i = 9; i >= 2; i--)
for (int j = 0; j < sl[i]; j++) printf("%d", i);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct sgt {
int l, r, mn, mm, tag;
long long sum;
} c[200005 << 2];
int n;
long long ans;
int a[200005];
long long h[200005];
vector<int> x[200005];
void pd(int o) {
if (c[o].tag) {
c[o << 1].mm = c[o << 1].mn = c[o].tag;
c[o << 1 | 1].mm = c[o << 1 | 1].mn = c[o].tag;
c[o << 1].tag = c[o << 1 | 1].tag = c[o].tag;
c[o << 1].sum = ((long long)c[o << 1].r - c[o << 1].l + 1) * c[o].tag;
c[o << 1 | 1].sum =
((long long)c[o << 1 | 1].r - c[o << 1 | 1].l + 1) * c[o].tag;
c[o].tag = 0;
}
}
void build(int l, int r, int o) {
c[o].l = l, c[o].r = r;
if (l == r) {
c[o].mn = c[o].sum = c[o].mm = l;
return;
}
int mid = (l + r) >> 1;
build(l, mid, o << 1);
build(mid + 1, r, o << 1 | 1);
c[o].mm = max(c[o << 1].mm, c[o << 1 | 1].mm);
c[o].mn = min(c[o << 1].mn, c[o << 1 | 1].mn);
c[o].sum = c[o << 1].sum + c[o << 1 | 1].sum;
}
void update(int l, int r, int k, int o) {
if (l > r) return;
if (l <= c[o].l && r >= c[o].r && k <= c[o].mn) return;
if (l <= c[o].l && r >= c[o].r && k >= c[o].mm) {
c[o].tag = k;
c[o].mm = c[o].mn = c[o].tag;
c[o].sum = ((long long)c[o].r - c[o].l + 1) * k;
return;
}
pd(o);
int mid = (c[o].l + c[o].r) >> 1;
if (l > mid)
update(l, r, k, o << 1 | 1);
else if (r <= mid)
update(l, r, k, o << 1);
else {
update(l, mid, k, o << 1);
update(mid + 1, r, k, o << 1 | 1);
}
c[o].mm = max(c[o << 1].mm, c[o << 1 | 1].mm);
c[o].mn = min(c[o << 1].mn, c[o << 1 | 1].mn);
c[o].sum = c[o << 1].sum + c[o << 1 | 1].sum;
}
int main() {
scanf("%d", &n);
h[0] = 3;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) {
int t = sqrt(a[i]);
for (int j = 1; j <= t; ++j) {
if (a[i] % j == 0) {
x[j].push_back(i);
if (j * j != a[i]) x[a[i] / j].push_back(i);
}
}
}
build(1, n, 1);
for (int i = 200000; i >= 1; --i) {
h[i] = (long long)n * n - c[1].sum + n;
if (x[i].size() <= 1)
continue;
else {
update(1, x[i][0], x[i][x[i].size() - 2], 1);
update(x[i][0] + 1, x[i][1], x[i][x[i].size() - 1], 1);
update(x[i][1] + 1, n, n + 1, 1);
}
}
for (int i = 200000; i >= 1; --i)
ans = ans + (long long)i * (h[i] - h[i - 1]);
printf("%lld", ans);
return 0;
}
| 10 |
#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 (b < a) {
a = b;
return 1;
}
return 0;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N, K;
cin >> N >> K;
vector<long long> A(N), sum(N + 1, 0);
long long ans = 0;
for (long long i = 0; i < N; i++) {
cin >> A[i];
ans += A[i];
sum[i + 1] += sum[i] + A[i];
}
vector<long long> tmp;
for (long long i = (1); i <= (N - 1); ++i) {
tmp.push_back(sum[N] - sum[i]);
}
sort((tmp).rbegin(), (tmp).rend());
for (long long i = 0; i < K - 1; i++) ans += tmp[i];
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int answer(vector<int> v) {
int n = v.size(), res = v[0] ^ v[1];
vector<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && st.back() < v[i]) st.pop_back();
st.push_back(v[i]);
if (st.size() >= 2) res = max(res, st[st.size() - 1] ^ st[st.size() - 2]);
}
return res;
}
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
int res = answer(v);
reverse(v.begin(), v.end());
cout << max(res, answer(v)) << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const double pi = 3.14159265358979323846;
int n, m;
vector<vector<int> > zapr;
bool read() {
if (!(cin >> n >> m)) return false;
zapr.resize(m);
for (int i = 0; i < int(m); ++i) {
for (int j = 0; j < int(4); ++j) {
int a;
cin >> a;
zapr[i].push_back(a);
}
zapr[i][1]--;
zapr[i][2]--;
}
return true;
}
void solve() {
vector<int> ans(n, 1e9);
for (int i = m - 1; i >= 0; --i) {
if (zapr[i][0] == 1) {
for (int j = zapr[i][1]; j <= zapr[i][2]; ++j)
ans[j] = min(1000000000, ans[j] - zapr[i][3]);
} else {
for (int j = zapr[i][1]; j <= zapr[i][2]; ++j)
ans[j] = min(ans[j], zapr[i][3]);
}
}
vector<int> check(ans);
bool ok = true;
for (int i = 0; i < int(m); ++i) {
if (zapr[i][0] == 1) {
for (int j = zapr[i][1]; j <= zapr[i][2]; ++j) ans[j] += zapr[i][3];
} else {
int res = -1e9;
for (int j = zapr[i][1]; j <= zapr[i][2]; ++j) res = max(res, ans[j]);
if (res != zapr[i][3] || res > 1e9) ok = false;
}
}
if (ok) {
cout << "YES" << endl;
for (int i = 0; i < int(n); ++i) cout << check[i] << ' ';
} else
cout << "NO" << endl;
}
int main() {
while (read()) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c = 0, m, s;
cin >> a >> b;
c += a;
while (a >= b) {
s = a / b;
m = a % b;
c += s;
a = s + m;
}
cout << c;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int f = 0;
int i = 1;
while (n >= i * (i + 1) / 2) {
n = n - (i * (i + 1)) / 2;
i++;
f++;
}
cout << f;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, k, a, b;
cin >> n;
a = sqrt(n);
b = n / a;
k = n % a;
if (k != 0)
cout << 2 * (a + b) + 2 << endl;
else
cout << 2 * (a + b) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
int n;
int main() {
long long t1, t2, x1, x2, t0;
cin >> t1 >> t2 >> x1 >> x2 >> t0;
while (true) {
long long chisl = t2 - t0;
long long znam = t0 - t1;
if (chisl == 0) {
if (znam == 0) {
cout << x1 << ' ' << x2;
return 0;
} else {
cout << "0 " << x2;
return 0;
}
}
if (znam == 0) {
cout << x1 << " 0";
return 0;
}
double tAns = 10000000.0;
long long ansY1 = -1;
long long ansY2 = -1;
for (long long y2 = 1; y2 <= x2; y2++) {
long long mnozh = y2 * chisl;
long long y1 = min(mnozh / znam, x1);
double tempT = (t1 * y1 + t2 * y2) / (double)(y1 + y2);
if ((double)t0 - tempT > 0.0000000001) continue;
if (abs(tempT - tAns) < 0.000001 && ansY1 + ansY2 < y1 + y2) {
ansY1 = y1;
ansY2 = y2;
}
if (tAns - tempT > 0.0000000001) {
tAns = tempT;
ansY1 = y1;
ansY2 = y2;
}
}
cout << ansY1 << ' ' << ansY2;
return 0;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
const int MOD = 1000000007;
const int INF = 1e8;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const long double EPS = 1e-7;
const int N = 4010;
struct Dinic {
struct Edge {
int from, to;
long long flow, cap;
};
vector<Edge> edge;
vector<int> g[N];
int ne = 0;
int lvl[N], vis[N], pass;
int qu[N], px[N], qt;
long long run(int s, int sink, long long minE) {
if (s == sink) return minE;
long long ans = 0;
for (; px[s] < (int)g[s].size(); px[s]++) {
int e = g[s][px[s]];
auto &v = edge[e], &rev = edge[e ^ 1];
if (lvl[v.to] != lvl[s] + 1 || v.flow >= v.cap) continue;
long long tmp = run(v.to, sink, min(minE, v.cap - v.flow));
v.flow += tmp, rev.flow -= tmp;
ans += tmp, minE -= tmp;
if (minE == 0) break;
}
return ans;
}
bool bfs(int source, int sink) {
qt = 0;
qu[qt++] = source;
lvl[source] = 1;
vis[source] = ++pass;
for (int i = 0; i < qt; i++) {
int u = qu[i];
px[u] = 0;
if (u == sink) return true;
for (auto& ed : g[u]) {
auto v = edge[ed];
if (v.flow >= v.cap || vis[v.to] == pass) continue;
vis[v.to] = pass;
lvl[v.to] = lvl[u] + 1;
qu[qt++] = v.to;
}
}
return false;
}
long long flow(int source, int sink) {
reset_flow();
long long ans = 0;
while (bfs(source, sink)) {
ans += run(source, sink, LLINF);
}
return ans;
}
void addEdge(int u, int v, long long c, long long rc) {
Edge e = {u, v, 0, c};
edge.push_back(e);
g[u].push_back(ne++);
e = {v, u, 0, rc};
edge.push_back(e);
g[v].push_back(ne++);
}
void reset_flow() {
for (int i = 0; i < ne; i++) edge[i].flow = 0;
memset(lvl, 0, sizeof(lvl));
memset(vis, 0, sizeof(vis));
memset(qu, 0, sizeof(qu));
memset(px, 0, sizeof(px));
qt = 0;
pass = 0;
}
};
int grau1[2010], grau2[2010];
int main() {
int n1, n2, m, u, v;
int sink = 4006, source = 4005;
cin >> n1 >> n2 >> m;
Dinic d;
vector<pair<int, int> > vet;
for (int i = 0; i < m; i++) {
cin >> u >> v;
u--;
v--;
vet.push_back({u, v});
grau1[u]++;
grau2[v]++;
}
int val = INF;
for (int i = 0; i < n1; i++) val = min(val, grau1[i]);
for (int i = 0; i < n2; i++) val = min(val, grau2[i]);
for (int i = 0; i < n1; i++) d.addEdge(sink, i, grau1[i] - val, 0);
for (int i = 0; i < n2; i++) d.addEdge(i + n1, source, grau2[i] - val, 0);
for (int i = 0; i < m; i++) d.addEdge(vet[i].first, vet[i].second + n1, 1, 0);
vector<vector<int> > ans(val, vector<int>());
d.flow(sink, source);
for (int i = 0; i < m; i++) {
int ind = n1 + n2 + i;
if (d.edge[2 * ind].flow == 0) ans[0].push_back(i + 1);
}
for (int i = 1; i < val; i++) {
for (int j = 0; j < n1 + n2; j++) d.edge[2 * j].cap++;
d.flow(sink, source);
for (int j = 0; j < m; j++) {
int ind = n1 + n2 + j;
if (d.edge[2 * ind].flow == 0) ans[i].push_back(j + 1);
}
}
cout << 0 << '\n';
for (int i = val - 1; i >= 0; i--) {
cout << ans[i].size();
for (auto a : ans[i]) cout << " " << a;
cout << '\n';
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, a;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
int x = sqrt(i * i + j * j);
if (x <= n && x >= j && x * x == (i * i + j * j)) a++;
}
}
cout << a;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
map<long long, long long> mp[200005];
long long n, u, v, val;
vector<long long> a[200005];
long long low[200005], num[200005], T, cnt, m, b[200005], c[200005];
stack<long long> st;
void dfs(long long u, long long p) {
low[u] = num[u] = ++T;
st.push(u);
for (long long j = (0); j <= (a[u].size() - 1); ++j) {
long long v = a[u][j];
if (v == p) continue;
if (num[v])
low[u] = min(low[u], num[v]);
else {
dfs(v, u);
low[u] = min(low[u], low[v]);
}
}
if (low[u] == num[u]) {
cnt = 0;
long long v;
do {
v = st.top();
b[++cnt] = v;
st.pop();
} while (v != u);
if (cnt > 1) {
m = cnt;
for (long long j = (1); j <= (cnt); ++j) c[j] = b[j];
}
}
}
long long dmxle[200005], dmxri[200005], mxle[200005], mxri[200005], ok[200005],
f[200005], sum[200005];
long long ans = -10000000000000000;
void get(long long u, long long p) {
vector<long long> vc;
for (long long j = (0); j <= (a[u].size() - 1); ++j) {
long long v = a[u][j];
if (v == p || ok[v]) continue;
get(v, u);
f[u] = max(f[u], f[v] + mp[u][v]);
vc.push_back(-f[v] - mp[u][v]);
}
ans = max(ans, f[u]);
if (vc.size() < 2) return;
sort(vc.begin(), vc.end());
ans = max(ans, -vc[0] - vc[1]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = (1); i <= (n); ++i) {
cin >> u >> v >> val;
mp[u][v] = val;
mp[v][u] = val;
a[u].push_back(v);
a[v].push_back(u);
}
dfs(1, 1);
for (long long i = (1); i <= (m); ++i) ok[c[i]] = 1;
for (long long i = (1); i <= (m); ++i) get(c[i], c[i]);
c[++m] = c[1];
for (long long i = (1); i <= (n); ++i) mp[0][i] = 0;
long long mxnow = -10000000000000000;
memset(sum, 0, sizeof(sum));
for (long long j = (1); j <= (m - 1); ++j) {
sum[j] = sum[j - 1] + mp[c[j - 1]][c[j]];
mxle[j] = mxle[j - 1];
mxle[j] = max(mxle[j], mxnow + sum[j] + f[c[j]]);
mxnow = max(mxnow, -sum[j] + f[c[j]]);
if (j == 1) continue;
dmxle[j] = dmxle[j - 1];
dmxle[j] = max(dmxle[j], f[c[j]] + sum[j]);
}
mxnow = -10000000000000000;
memset(sum, 0, sizeof(sum));
for (long long j = (m); j >= (2); --j) {
sum[j] = sum[j + 1] + mp[c[j + 1]][c[j]];
mxri[j] = mxri[j + 1];
mxri[j] = max(mxri[j], mxnow + sum[j] + f[c[j]]);
mxnow = max(mxnow, -sum[j] + f[c[j]]);
if (j == m) continue;
dmxri[j] = dmxri[j + 1];
dmxri[j] = max(dmxri[j], sum[j] + f[c[j]]);
}
long long kq = 10000000000000000;
for (long long i = (1); i <= (m - 1); ++i) {
long long tmp = mxle[i];
tmp = max(tmp, mxri[i + 1]);
tmp = max(tmp, dmxle[i] + dmxri[i + 1]);
tmp = max(tmp, f[c[1]] + dmxle[i]);
tmp = max(tmp, f[c[1]] + dmxri[i + 1]);
kq = min(kq, tmp);
}
cout << max(ans, kq);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, M, mins, maks, K, F, i;
cin >> N >> M;
mins = 1;
maks = 1000;
for (i = 0; i < M; i++) {
cin >> K >> F;
mins = max(mins, (K - 1) / F + 1);
if (F > 1) {
maks = min(maks, (K - 1) / (F - 1) + 1);
}
}
mins = (N - 1) / mins + 1;
if (maks == 1) {
maks = mins;
} else {
maks = (N - 1) / (maks - 1) + 1;
}
if (mins != maks) {
cout << -1 << '\n';
} else {
cout << mins << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int INF = 1e17;
int n, m, k, price[1001010], lst[1001010];
bool blocked[1001010];
long long int getMn(int power) {
int at = 0, cnt = 0, atTmp;
while (at < n) {
atTmp = at;
at += power;
if (at >= n) return cnt + 1;
at = lst[at];
if (at <= atTmp) return INF;
cnt++;
}
return cnt;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int x;
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
cin >> x;
blocked[x] = true;
}
for (int i = 1; i <= k; i++) cin >> price[i];
if (blocked[0]) {
cout << "-1\n";
return 0;
}
for (int i = 0; i <= n; i++) {
if (blocked[i])
lst[i] = lst[i - 1];
else
lst[i] = i;
}
long long int ans = INF;
for (int i = 1; i <= k; i++) {
long long int aux = getMn(i);
if (aux < INF) ans = min(ans, (long long int)price[i] * aux);
}
if (ans == INF) ans = -1;
cout << ans << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
const int MOD = 998244353;
int n, win[N], los[N], cp[N][N], strong[N], ans[N], a, b;
int ksm(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = (long long)ans * x % MOD;
x = (long long)x * x % MOD;
y >>= 1;
}
return ans;
}
int main() {
scanf("%d%d%d", &n, &a, &b);
win[0] = los[0] = 1;
win[1] = (long long)a * ksm(b, MOD - 2) % MOD;
los[1] = (1 + MOD - win[1]) % MOD;
for (int i = 2; i <= n; i++)
win[i] = (long long)win[i - 1] * win[1] % MOD,
los[i] = (long long)los[i - 1] * los[1] % MOD;
cp[1][0] = cp[1][1] = 1;
for (int s = 2; s <= n; s++) {
cp[s][0] = 1;
for (int i = 1; i <= s; i++) {
cp[s][i] = (long long)cp[s - 1][i] * los[i] % MOD +
(long long)cp[s - 1][i - 1] * win[s - i] % MOD;
cp[s][i] -= cp[s][i] >= MOD ? MOD : 0;
}
}
strong[1] = 1;
for (int s = 2; s <= n; s++) {
strong[s] = 1;
for (int i = 1; i < s; i++) {
strong[s] += MOD - (long long)strong[i] * cp[s][i] % MOD;
strong[s] -= strong[s] >= MOD ? MOD : 0;
}
}
ans[0] = ans[1] = 0;
for (int s = 2; s <= n; s++) {
for (int i = 1; i < s; i++) {
ans[s] += (long long)strong[i] * cp[s][i] % MOD *
((long long)i * (s - i) + (long long)i * (i - 1) / 2 +
(long long)ans[i] + (long long)ans[s - i]) %
MOD;
ans[s] -= ans[s] >= MOD ? MOD : 0;
}
ans[s] += (long long)strong[s] * cp[s][s] % MOD * (s * (s - 1) / 2) % MOD;
ans[s] = (long long)ans[s] *
ksm(1 + MOD - (long long)strong[s] * cp[s][s] % MOD, MOD - 2) %
MOD;
}
printf("%d", ans[n]);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d%d", &n, &m);
char name[18][15];
pair<string, string> p[125];
for (int i = 0; i < n; i++) scanf("%s", name[i]);
for (int i = 0; i < m; i++) {
char s1[15];
char s2[15];
scanf("%s%s", &s1, &s2);
p[i] = {(string)s1, (string)s2};
}
vector<string> res;
for (int i = 0; i < (1 << n); i++) {
int ii = i;
int cnt = 0;
vector<string> v;
while (ii) {
if (ii & 1) v.push_back((string)name[cnt]);
cnt++;
ii = ii >> 1;
}
bool f = 1;
for (int j = 0; j < v.size() && f; j++)
for (int k = 0; k < v.size() && f; k++)
for (int z = 0; z < m; z++)
if (p[z].first == v[j] && p[z].second == v[k]) {
f = 0;
break;
}
if (!f) continue;
if (v.size() > res.size()) res = v;
}
sort(res.begin(), res.end());
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++) printf("%s\n", res[i].c_str());
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
T gcd(T a, T b) {
return ((b == 0) ? a : gcd(b, a % b));
}
int n, m;
int cntN = 0, cntM = 0;
bool good(string a, string b) {
string s = a + b;
sort(s.begin(), s.end());
for (int i = (int)(1); i < (int)(s.size()); i++)
if (s[i] == s[i - 1]) return 0;
int n2 = 0, m2 = 0;
int powN = 1, powM = 1;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (int i = 0; i < (int)(a.size()); i++)
n2 += powN * (a[i] - '0'), powN *= 7;
for (int i = 0; i < (int)(b.size()); i++)
m2 += powM * (b[i] - '0'), powM *= 7;
if (n2 <= n && m2 <= m) return 1;
return 0;
}
int from7(string a) {
int n2 = 0, powN = 1;
for (int i = 0; i < (int)(a.size()); i++)
n2 += powN * (a[i] - '0'), powN *= 7;
return n2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
n--, m--;
string pos = "0123456";
int n1 = n, m1 = m;
while (n1) cntN++, n1 /= 7;
while (m1) cntM++, m1 /= 7;
cntN = max(cntN, 1);
cntM = max(cntM, 1);
int ans = 0;
map<string, int> vis;
do {
if ((cntN + cntM) <= pos.size()) {
string a = pos.substr(0, cntN);
string b = pos.substr(cntN, cntM);
if (vis[a + b]) goto skip;
vis[a + b] = 1;
if (good(a, b)) {
ans++;
}
}
skip:;
if ((cntN + cntM - 1) <= pos.size()) {
string a = "0" + pos.substr(0, cntN - 1);
string b = pos.substr(cntN - 1, cntM);
if (vis[a + b]) goto skip1;
vis[a + b] = 1;
if (good(a, b)) {
ans++;
}
a = pos.substr(0, cntN);
b = "0" + pos.substr(cntN, cntM - 1);
if (vis[a + b]) goto skip1;
vis[a + b] = 1;
if (good(a, b)) {
ans++;
}
}
skip1:;
} while (next_permutation(pos.begin(), pos.end()));
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int N = 100005;
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << n / 2 << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
bool vis[303];
int p[303];
vector<int> g[303];
vector<int> t[303];
void dfs(int x, int qq) {
if (vis[x]) return;
t[qq].push_back(x);
vis[x] = 1;
for (int i = 0; i < g[x].size(); i++) dfs(g[x][i], qq);
}
bool has[303];
int ans[303];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
char c;
cin >> c;
if (c == '1') {
g[i].push_back(j);
g[j].push_back(i);
}
}
for (int i = 0; i < n; i++) {
memset(vis, 0, sizeof(vis));
dfs(i, p[i]);
sort(t[p[i]].begin(), t[p[i]].end());
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < t[i].size(); j++) {
if (!has[t[i][j]]) {
ans[t[i][j]] = i;
has[t[i][j]] = 1;
break;
}
}
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
template <typename T1, typename T2>
inline void checkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void checkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
template <typename T1>
inline void sort(T1 &arr) {
sort(arr.begin(), arr.end());
}
template <typename T1>
inline void shuffle(T1 &arr) {
for (int i = -arr.size(); i < arr.size(); ++i) {
int first = ((rand() << 16) + rand()) % arr.size(),
second = ((rand() << 16) + rand()) % arr.size();
swap(arr[first], arr[second]);
}
}
template <typename T1>
inline void reverse(T1 &arr) {
reverse(arr.begin(), arr.end());
}
vector<long long> arr;
vector<bool> used;
long long dfs(long long v) {
used[v] = true;
if (!used[arr[v]])
return dfs(arr[v]) + 1;
else
return 1;
}
signed main() {
long long n;
cin >> n;
arr = vector<long long>(n);
for (auto &i : arr) cin >> i;
for (auto &i : arr) --i;
used = vector<bool>(n);
long long ans = 0;
for (int i = 0; i < n; ++i)
if (!used[i]) ans = (ans + (dfs(i) % 2 == 0 ? 1 : 0)) % 2;
if (ans == (3 * n) % 2)
cout << "Petr";
else
cout << "Um_nik";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long dp[1100][1100];
long long mo = 1000000007;
vector<long long> kai;
long long zyo(long long x, long long y) {
long long ret = 1, a = x;
while (y > 0) {
if (y % 2 == 1) ret = (ret * a) % mo;
a = (a * a) % mo;
y /= 2;
}
return ret;
}
long long extgcd(long long a, long long b, long long &x, long long &y) {
long long g = a;
x = 1;
y = 0;
if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
return g;
}
long long invMod(long long a, long long m) {
long long x, y;
if (extgcd(a, m, x, y) == 1) return (x + m) % m;
return 0;
}
int main() {
long long i, j, out = 0;
int n, m, k, x, y;
cin >> n >> m >> k;
if (m < 2) {
cout << zyo(k, n) << endl;
return 0;
}
kai.push_back(1);
for (i = 0; i < 1000010; i++) kai.push_back((kai[i] * (i + 1)) % mo);
for (i = 0; i < 1100; i++)
for (j = 0; j < 1100; j++) dp[i][j] = 0;
dp[0][0] = 1;
for (i = 0; i < 1050; i++)
for (j = 0; j < i + 1; j++) {
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * j) % mo;
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (j + 1)) % mo;
}
for (x = 1; x < n + 1; x++)
for (y = max(2 * x - k, 0); y < x + 1; y++) {
long long t = (kai[k] * invMod(kai[y], mo)) % mo;
t *= invMod(kai[x - y], mo);
t %= mo;
t *= invMod(kai[x - y], mo);
t %= mo;
t *= invMod(kai[k - 2 * x + y], mo);
t %= mo;
t *= zyo(y, n * (m - 2));
t %= mo;
t *= dp[n][x];
t %= mo;
out = (out + t * dp[n][x]) % mo;
}
cout << out << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
long long int k;
cin >> n >> k;
string s, t;
cin >> s >> t;
long long int ans = 0LL;
bool key = false;
long long int cnt = 0;
for (int i = 0; i < n; i++) {
if (!key) {
if (s[i] == t[i]) {
ans++;
} else {
k--;
ans += (n - i);
if (k == 0LL) {
break;
}
k--;
ans += (n - i);
if (k == 0LL) {
break;
}
cnt = 0LL;
key = true;
}
continue;
}
cnt = 2LL * cnt;
if (t[i] == 'b') {
cnt++;
}
if (s[i] == 'a') {
cnt++;
}
if (cnt >= k) {
ans += (n - i) * k;
break;
}
ans += cnt;
}
cout << ans << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long a[5][5], sum = 0, mx = 0;
long long b[5] = {0, 1, 2, 3, 4};
for (long long i = 0; i < 5; ++i) {
for (long long j = 0; j < 5; ++j) cin >> a[i][j];
}
do {
for (long long i = 0; i < 4; ++i) {
sum += (a[b[i]][b[i + 1]] + a[b[i + 1]][b[i]]);
}
sum += a[b[3]][b[4]] + a[b[4]][b[3]] + a[b[2]][b[3]] + a[b[3]][b[2]];
mx = max(mx, sum);
sum = 0;
} while (next_permutation(b, b + 5));
cout << mx;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
long long x;
cin >> x;
long long y;
cin >> y;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < n; i++) {
long long d = 0;
for (long long j = i - x; j < i; j++) {
if ((j) >= 0) {
if (a[i] >= a[j]) {
d = 1;
break;
}
}
}
for (long long j = i + 1; j <= y + i; j++) {
if ((j) < n) {
if (a[j] <= a[i]) {
d = 1;
break;
}
}
}
if (d == 0) {
cout << i + 1;
return 0;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e18;
const long long mod = 1e9 + 7;
const long long N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long calc(long long x, long long y, long long z) {
return (x - y) * (x - y) + (y - z) * (y - z) + (x - z) * (x - z);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
;
while (t--) {
long long na, nb, nc;
cin >> na >> nb >> nc;
;
vector<pair<long long, long long> > v;
for (long long i = (1); i <= (na); i++) {
long long x;
cin >> x;
;
v.push_back({x, 0});
}
for (long long i = (1); i <= (nb); i++) {
long long x;
cin >> x;
;
v.push_back({x, 1});
}
for (long long i = (1); i <= (nc); i++) {
long long x;
cin >> x;
;
v.push_back({x, 2});
}
sort(v.begin(), v.end());
long long n = (long long)(v.size());
vector<vector<long long> > f(n + 4, vector<long long>(3, 0));
vector<vector<long long> > bb(n + 4, vector<long long>(3, inf));
f[0][v[0].second] = v[0].first;
for (long long i = (1); i <= (n - 1); i++) {
pair<long long, long long> x = v[i];
for (long long j = (0); j <= (2); j++) f[i][j] = f[i - 1][j];
f[i][x.second] = x.first;
}
for (long long i = (n - 1); i >= (0); i--) {
pair<long long, long long> x = v[i];
for (long long j = (0); j <= (2); j++) bb[i][j] = bb[i + 1][j];
bb[i][x.second] = x.first;
}
long long ans = inf;
for (long long i = (1); i <= (n - 1); i++) {
pair<long long, long long> qq = v[i];
long long col = qq.second, x = qq.first, a, b;
if (col == 0) a = 1, b = 2;
if (col == 1) a = 0, b = 2;
if (col == 2) a = 0, b = 1;
if (f[i][a] > 0 && f[i][b] > 0) ans = min(ans, calc(x, f[i][a], f[i][b]));
if (f[i][a] > 0 && bb[i][b] < inf)
ans = min(ans, calc(x, f[i][a], bb[i][b]));
if (f[i][b] > 0 && bb[i][a] < inf)
ans = min(ans, calc(x, f[i][b], bb[i][a]));
if (bb[i][a] < inf && bb[i][b] < inf)
ans = min(ans, calc(x, bb[i][a], bb[i][b]));
}
cout << ans << '\n';
;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[101010];
int main() {
int c_res = 1, f_res = 1;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
if (a[i] >= a[i - 1])
c_res++;
else
c_res = 1;
f_res = max(f_res, c_res);
}
cout << f_res << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> tmp;
set<int> odd, even;
set<pair<int, int> > a[400005];
int x[400005], y[400005], dx[400005], dy[400005], d[400005], ans[400005];
int f[400005][2], _x, _y, n, i, last, k, cnt;
int Findx(int x) { return lower_bound(dx + 1, dx + _x + 1, x) - dx; }
int Findy(int y) { return lower_bound(dy + 1, dy + _y + 1, y) - dy; }
void del(int x) {
d[x]--;
if (d[x] & 1)
even.erase(x), odd.insert(x);
else if (d[x])
odd.erase(x), even.insert(x);
else
odd.erase(x);
}
void push() {
last ^= 1;
tmp = *a[k].begin();
a[k].erase(a[k].begin());
a[tmp.first].erase(a[tmp.first].find(make_pair(k, tmp.second)));
del(k);
del(tmp.first);
f[k][last]++;
f[tmp.first][last]++;
ans[tmp.second] = last;
k = tmp.first;
cnt++;
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &x[i], &y[i]);
dx[i] = x[i];
dy[i] = y[i];
}
sort(dx + 1, dx + n + 1);
_x = unique(dx + 1, dx + n + 1) - (dx + 1);
sort(dy + 1, dy + n + 1);
_y = unique(dy + 1, dy + n + 1) - (dy + 1);
for (i = 1; i <= n; i++) {
d[x[i] = Findx(x[i])]++;
d[y[i] = Findy(y[i]) + _x]++;
a[x[i]].insert(make_pair(y[i], i));
a[y[i]].insert(make_pair(x[i], i));
}
for (i = 1; i <= _x + _y; i++)
if (d[i] & 1)
odd.insert(i);
else
even.insert(i);
last = 0;
k = 0;
while (cnt < n) {
if (k && !a[k].empty()) {
push();
continue;
}
if (odd.empty())
k = *even.begin();
else
k = *odd.begin();
last = f[k][1] > f[k][0];
push();
}
for (i = 1; i <= n; i++) putchar(ans[i] ? 'b' : 'r');
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
using vv = vector<vector<T>>;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &t) {
os << "{";
for (int(i) = 0; (i) < (t.size()); ++(i)) {
os << t[i] << ",";
}
os << "}" << endl;
return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
return os << "(" << t.first << "," << t.second << ")";
}
void zAlgo(const string &str, vector<int> &re) {
re.resize(str.size());
re[0] = str.size();
int i = 1, j = 0;
while (i < str.size()) {
while (i + j < str.size() && str[j] == str[i + j]) ++j;
re[i] = j;
if (!j) {
++i;
continue;
}
int k = 1;
while (i + k < str.size() && k + re[k] < j) {
re[i + k] = re[k];
++k;
}
i += k;
j -= k;
}
}
int main() {
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(0);
int i, j, k;
int n, m, len;
scanf("%d %d", &n, &m);
char str_[n + 1];
scanf("%s", str_);
string str(str_);
len = str.size();
vector<int> p(m), z, sum(n + 1);
for (int(i) = 0; (i) < (m); ++(i)) {
scanf("%d", &p[i]);
--p[i];
}
zAlgo(str, z);
for (int(i) = 0; (i) < (m - 1); ++(i)) {
if (p[i + 1] - p[i] < len && z[p[i + 1] - p[i]] + p[i + 1] - p[i] < len) {
printf("0\n");
return 0;
}
}
for (int(i) = 0; (i) < (m); ++(i)) {
++sum[p[i]];
--sum[p[i] + len];
}
for (int(i) = 0; (i) < (n - 1); ++(i)) sum[i + 1] += sum[i];
long long re = 1, MOD = 1e9 + 7;
for (int(i) = 0; (i) < (n); ++(i))
if (sum[i] == 0) (re *= 26) %= MOD;
printf("%I64d\n", re);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T, class S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
return os << "(" << v.first << ", " << v.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "[";
for (int i = int(0); i <= int((static_cast<int>((v).size())) - 1); ++i) {
if (i) os << ", ";
os << v[i];
}
return os << "]";
}
template <class T>
bool setmax(T& _a, T _b) {
if (_a < _b) {
_a = _b;
return true;
}
return false;
}
template <class T>
bool setmin(T& _a, T _b) {
if (_b < _a) {
_a = _b;
return true;
}
return false;
}
template <class T>
T gcd(T _a, T _b) {
return _b == 0 ? _a : gcd(_b, _a % _b);
}
int main() {
int n;
cin >> n;
const vector<int> a = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 0};
int s = 0;
if (n == 0) s = 1;
while (n > 0) s += a[n & 15], n >>= 4;
cout << s << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5001;
int n;
double f[N][N], p[N], ans;
int gi() {
register int x = 0, k = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') k = -1, c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x * k;
}
int main() {
n = gi();
memset(f, -63, sizeof(f));
f[0][n] = 0;
for (register int i = 0; i <= n; ++i) scanf("%lf", &p[i]);
for (register int i = 1; i <= 5000; ++i)
for (register int j = 0; j <= (n << 1); ++j)
for (register int k = 0; k <= j && k <= n; ++k) {
register int q = n - k - k;
if (j + q >= 0 && j + q <= (n << 1))
f[i][j + q] = max(f[i][j + q], f[i - 1][j] + p[k]);
}
for (register int i = 1; i <= 5000; ++i) ans = max(ans, f[i][n] / i);
printf("%.10lf", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 2 == 0) {
cout << (n / 2) - 1;
} else {
cout << ((n + 1) / 2) - 1;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000009;
map<pair<int, int>, int> in, id;
set<int> S;
int X[100010], Y[100010];
bool check(pair<int, int> P) {
int x = P.first, y = P.second + 1;
for (int dx = x - 1; dx <= x + 1; dx++) {
if (in.count(make_pair(dx, y)) && in[make_pair(dx, y)] == 1) return 0;
}
return 1;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
X[i] = x, Y[i] = y;
in[make_pair(x, y)] = 0, id[make_pair(x, y)] = i;
}
for (map<pair<int, int>, int>::iterator it = in.begin(); it != in.end();
it++) {
int x = it->first.first, y = it->first.second - 1;
for (int dx = x - 1; dx <= x + 1; dx++) {
if (in.count(make_pair(dx, y))) {
in[it->first]++;
}
}
}
for (map<pair<int, int>, int>::iterator it = in.begin(); it != in.end();
it++) {
if (check(it->first)) S.insert(id[it->first]);
}
long long ans = 0;
for (int it = 0; it < n; it++) {
if (it & 1) {
int v = *S.begin();
S.erase(S.begin());
int y = Y[v] + 1;
id.erase(make_pair(X[v], Y[v]));
in[make_pair(X[v], Y[v])] = 0;
for (int x = X[v] - 1; x <= X[v] + 1; x++) {
if (!id.count(make_pair(x, y))) continue;
in[make_pair(x, y)]--;
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
ans = (ans * n + v) % mod;
} else {
int v = *(--S.end());
S.erase(--S.end());
int y = Y[v] + 1;
id.erase(make_pair(X[v], Y[v]));
in[make_pair(X[v], Y[v])] = 0;
for (int x = X[v] - 1; x <= X[v] + 1; x++) {
if (!id.count(make_pair(x, y))) continue;
in[make_pair(x, y)]--;
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
ans = (ans * n + v) % mod;
}
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string s[55], a = "A", b = "B";
char t[5];
vector<string> ans;
int main() {
for (int i = 1; i <= 26; i++) s[i] = a + char('a' + i - 1);
for (int i = 27; i <= 52; i++) s[i] = b + char('a' + i - 27);
int n, k, cnt = 1;
scanf("%d %d", &n, &k);
for (; cnt <= k - 1; cnt++) ans.push_back(s[cnt]);
scanf("%s", t);
if (t[0] == 'N')
ans.push_back(s[1]);
else
ans.push_back(s[cnt++]);
for (int i = 1; i <= n - k; i++) {
scanf("%s", t);
if (t[0] == 'N')
ans.push_back(ans[ans.size() - k + 1]);
else
ans.push_back(s[cnt++]);
}
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int freq[10];
set<vector<int> > vis;
long long n, ans, f[20];
long long calc() {
long long s = accumulate(v.begin(), v.end(), 0), res = 0;
res = f[s];
for (auto i : v) res /= f[i];
return res;
}
void solve() {
if (vis.count(v)) return;
vis.insert(v);
ans += calc();
if (v[0]) {
v[0]--;
ans -= calc();
v[0]++;
}
for (int i = 0; i < 10; i++) {
if (freq[i] > 0) {
v[i]++, freq[i]--;
solve();
v[i]--, freq[i]++;
}
}
}
int main() {
f[0] = f[1] = 1;
for (int i = 2; i < 20; i++) f[i] = i * f[i - 1];
cin >> n;
while (n) freq[n % 10]++, n /= 10;
for (int i = 0; i < 10; i++) {
v.push_back((freq[i] > 0));
freq[i] -= v[i];
}
solve();
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
char a[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
int c = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == '.') {
if ((i + 1 >= 0 && i + 1 <= n) && (i + 2 >= 0 && i + 2 <= n) &&
(j + 1 >= 0 && j + 1 <= n) && (j - 1 >= 0 && j - 1 <= n)) {
if (a[i + 1][j - 1] == '.' && a[i + 1][j] == '.' &&
a[i + 1][j + 1] == '.' && a[i + 2][j] == '.') {
a[i][j] = '#';
a[i + 1][j - 1] = '#';
a[i + 1][j] = '#';
a[i + 1][j + 1] = '#';
a[i + 2][j] = '#';
}
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == '.') {
c = 1;
break;
}
}
if (c == 1) break;
}
if (c == 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, INF = 0x3f3f3f3f;
int n, ans = INF;
int F[200][200], dis[200][200];
long long nums[maxn];
unordered_set<int> exist[100];
int idx[maxn], pos;
void init() {
for (int i = 0; i < 200; i++)
for (int j = 0; j < 200; j++) F[i][j] = INT_MAX / 3;
}
void Floyd() {
memcpy(dis, F, sizeof F);
for (int k = 1; k <= pos; k++) {
for (int i = 1; i < k; i++)
for (int j = i + 1; j < k; j++)
ans = min(ans, dis[i][j] + F[i][k] + F[k][j]);
for (int i = 1; i <= pos; i++)
for (int j = 1; j <= pos; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
init();
cin >> n;
for (int i = 0; i < n; i++) cin >> nums[i];
for (int k = 0; k < 60; k++) {
for (int i = 0; i < n; i++)
if (nums[i] >> k & 1) exist[k].insert(i);
if (exist[k].size() >= 3) {
ans = 3;
break;
}
if (exist[k].size() > 1) {
int a = *exist[k].begin();
int b = *(++exist[k].begin());
if (!idx[a]) idx[a] = ++pos;
if (!idx[b]) idx[b] = ++pos;
F[idx[a]][idx[b]] = F[idx[b]][idx[a]] = 1;
}
}
if (ans == INF) Floyd();
if (ans >= INT_MAX / 3)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
que;
stringstream ssin;
const long long LINF = 0x7fffffffffffffffll;
const int N = 5e3 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, n1, n2, n3, m, idx, cnt;
int e[M], ne[M], h[N], id[N], col[N], sz[N];
int dp[N][N], d[N][5];
bool flag;
vector<int> v[N][3];
inline long long read() {
char c = getchar();
long long x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool dfs(int u, int num) {
col[u] = num;
id[u] = cnt;
d[cnt][num]++;
v[cnt][num].push_back(u);
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!col[v]) {
if (!dfs(v, 3 - num)) return false;
} else if (col[v] == num)
return false;
}
return true;
}
int main() {
n = read();
m = read();
memset(h, -1, sizeof h);
;
n1 = read();
n2 = read();
n3 = read();
for (int i = 1; i <= m; ++i) {
int a, b;
a = read();
b = read();
add(a, b);
add(b, a);
}
flag = 1;
for (int i = 1; i <= n; ++i) {
if (!col[i]) {
id[i] = ++cnt;
if (!dfs(i, 1)) {
flag = 0;
break;
}
}
}
if (!flag) {
puts("NO");
return 0;
}
dp[0][0] = 1;
for (int i = 1; i <= cnt; ++i) {
for (int j = 0; j <= n2; ++j) {
for (int k = 1; k <= 2; ++k) {
if (j < d[i][k]) continue;
dp[i][j] |= dp[i - 1][j - d[i][k]];
}
}
}
if (!dp[cnt][n2]) {
puts("NO");
return 0;
}
puts("YES");
memset(col, 0, sizeof col);
;
int now = n2;
for (int i = cnt; i >= 1; --i) {
if (dp[i - 1][now - d[i][1]]) {
for (int j = 0; j < v[i][1].size(); ++j) {
col[v[i][1][j]] = 2;
}
now -= d[i][1];
} else if (dp[i - 1][now - d[i][2]]) {
for (int j = 0; j < v[i][2].size(); ++j) {
col[v[i][2][j]] = 2;
}
now -= d[i][2];
}
}
for (int i = 1; i <= n; ++i) {
if (!col[i]) {
if (n1) {
cout << 1;
n1--;
} else if (n3) {
cout << 3;
n3--;
}
} else
cout << 2;
}
puts("");
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1005;
vector<int> f[maxN];
int n;
vector<int> left_count[maxN];
vector<int> right_count[maxN];
void readFile() {
cin >> n;
for (int i = 0; i < n; i++) {
int k, tmp;
scanf("%d", &k);
for (int j = 0; j < k; j++) {
scanf("%d", &tmp);
f[i].push_back(tmp);
}
}
}
int main() {
readFile();
for (int i = 0; i < n; i++) sort(f[i].begin(), f[i].end());
int count = 0;
for (int i = 0; i < n; i++) {
int left = (i == 0) ? (n - 1) : (i - 1);
int right = (i == n - 1) ? 0 : (i + 1);
int left_index = 0;
int right_index = 0;
for (int j = 1; j < f[i].size(); j++) {
int left_count = 0;
int right_count = 0;
while (left_index < f[left].size() && f[left][left_index] < f[i][j]) {
if (f[left][left_index] > f[i][j - 1] && f[left][left_index] < f[i][j])
left_count++;
left_index++;
}
while (right_index < f[right].size() && f[right][right_index] < f[i][j]) {
if (f[right][right_index] > f[i][j - 1] &&
f[right][right_index] < f[i][j])
right_count++;
right_index++;
}
if (left_count != right_count) count++;
}
}
cout << count << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using db = double;
using dbl = long double;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using gr = vector<vector<int> >;
using grl = vector<vector<ll> >;
using st = stack<int>;
using stl = stack<ll>;
using qu = queue<int>;
using qul = queue<ll>;
using pq = priority_queue<int>;
using pql = priority_queue<ll>;
using piq = priority_queue<pair<int, int> >;
using piql = priority_queue<pair<ll, ll> >;
using ma = map<int, int>;
using ms = map<string, int>;
using mal = map<ll, ll>;
using mp = map<pair<int, int>, int>;
using mpl = map<pair<ll, ll>, int>;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
void input();
void compute();
int main() {
int T = 1;
scanf("%d", &T);
while (T--) {
input();
compute();
}
return 0;
}
ll n, k;
ll w;
ll tab[300001];
ll tab2[300001];
unordered_map<ll, ll> m;
void input() {
m.clear();
cin >> n >> k;
for (int(i) = 0; (i) < (n); (i)++) {
cin >> tab[i];
tab2[i] = tab[i];
}
}
void compute() {
w = 1;
sort(tab2, tab2 + n);
for (int(i) = 0; (i) < (n); (i)++) {
m[tab2[i]] = i;
}
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (m[tab[i]] + 1 != m[tab[i + 1]]) {
w++;
}
}
if (w <= k)
printf("YES\n");
else
printf("NO\n");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 5;
long long f[20][N];
int a[N][N], m;
long long s[N];
long long ans = 0;
void solve(int dep, int l, int r) {
if (l == r) {
long long sum = 0;
ans = max(ans, f[dep][m]);
for (int i = 1; i <= min(m, a[l][0]); ++i)
sum += a[l][i], ans = max(ans, sum + f[dep][m - i]);
return;
}
int mid = l + r >> 1;
for (int i = 0; i <= m; ++i) f[dep + 1][i] = f[dep][i];
for (int i = mid + 1; i <= r; ++i)
for (int j = m; j >= a[i][0]; --j)
f[dep + 1][j] = max(f[dep + 1][j], f[dep + 1][j - a[i][0]] + s[i]);
solve(dep + 1, l, mid);
for (int i = 0; i <= m; ++i) f[dep + 1][i] = f[dep][i];
for (int i = l; i <= mid; ++i)
for (int j = m; j >= a[i][0]; --j)
f[dep + 1][j] = max(f[dep + 1][j], f[dep + 1][j - a[i][0]] + s[i]);
solve(dep + 1, mid + 1, r);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i][0];
for (int j = 1; j <= a[i][0]; ++j) {
int x;
cin >> x;
if (j <= m) a[i][j] = x, s[i] += x;
}
}
solve(1, 1, n);
cout << ans;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
long long n, a, b;
cin >> n >> a >> b;
cout << n - min(a, b) + 1 << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int _maxn = 5011;
int n, type, queu[_maxn], head, tail;
long long h[_maxn], dpva[_maxn], pref[_maxn], dpol[_maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &h[i]);
pref[i] = pref[i - 1] + h[i];
}
queu[tail++] = 0;
for (int i = 1; i <= n; ++i) {
while (head + 1 < tail && dpva[queu[head]] <= pref[i] &&
dpva[queu[head + 1]] <= pref[i])
++head;
dpol[i] = dpol[queu[head]] + (i - queu[head] - 1);
dpva[i] = (pref[i] << 1) - pref[queu[head]];
while (head < tail && dpva[queu[tail - 1]] >= dpva[i]) --tail;
queu[tail++] = i;
}
printf("%lld", dpol[n]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 301000;
class DisjointSet {
public:
DisjointSet(int n = MAXN * 2)
: N(n),
parents(n, 0),
ranks(n, 0),
cntsX(n, 0),
cntsY(n, 0),
clock(0),
recSize(0) {
iota(parents.begin(), parents.end(), 0);
records.resize(N * 2);
}
void add(int x, int cx, int cy) {
cntsX[x] += cx;
cntsY[x] += cy;
if (x != parents[x]) {
cntsX[find(x)] += cx;
cntsY[find(x)] += cy;
}
}
int find(int x) {
if (x == parents[x]) {
return x;
}
backup(x);
return parents[x] = find(parents[x]);
}
long long calc(int x) {
int p = find(x);
return cntsX[p] * 1LL * cntsY[p];
}
int now() { return clock; }
long long merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return 0;
}
long long res = 0;
res -= calc(x);
res -= calc(y);
if (ranks[x] >= ranks[y]) {
swap(x, y);
}
++clock;
backup(x);
backup(y);
parents[x] = y;
if (ranks[x] == ranks[y]) {
++ranks[y];
}
cntsX[y] += cntsX[x];
cntsY[y] += cntsY[x];
res += calc(y);
return res;
}
void backup(int x) {
++clock;
if (recSize == records.size()) {
records.resize(recSize * 2);
}
records[recSize++] =
Datum(x, parents[x], cntsX[x], cntsY[x], clock, ranks[x]);
}
void revertTo(int time) {
while (recSize > 0) {
auto& rec = records[recSize - 1];
if (rec.timeStamp > time) {
int x = rec.x;
int p = rec.p;
parents[x] = p;
cntsX[x] = rec.cx;
cntsY[x] = rec.cy;
--recSize;
} else {
break;
}
}
}
private:
int N;
vector<int> parents;
vector<int> ranks;
vector<int> cntsX;
vector<int> cntsY;
int clock;
struct Datum {
Datum(int _x = 0, int _p = 0, int _cx = 0, int _cy = 0, int _t = 0,
int _r = 0)
: x(_x), p(_p), cx(_cx), cy(_cy), timeStamp(_t), rk(_r) {}
int x;
int p;
int cx;
int cy;
int rk;
int timeStamp;
};
int recSize;
vector<Datum> records;
};
class Solution {
public:
vector<long long> query(vector<pair<int, int>>& qv) {
int q = qv.size();
int maxX = 0;
int maxY = 0;
for (auto& p : qv) {
maxX = max(maxX, p.first);
maxY = max(maxY, p.second);
}
init(maxX, maxY, q);
map<pair<int, int>, int> mp;
int idx = 0;
for (auto& p : qv) {
if (mp.count(p) > 0) {
int l = mp[p];
int r = idx - 1;
insert(rt, lo, hi, l, r, p);
mp.erase(p);
} else {
mp[p] = idx;
}
++idx;
}
for (auto& pi : mp) {
auto p = pi.first;
int l = pi.second;
insert(rt, lo, hi, l, q - 1, p);
}
vector<long long> res(q, 0);
long long cnt = 0;
dfs(rt, lo, hi, cnt, res);
return res;
}
private:
DisjointSet ds;
int K;
int rt;
int lo;
int hi;
vector<vector<pair<int, int>>> nodes;
void init(int maxX, int maxY, int q) {
K = max(maxX + 1, maxY + 1) + 10;
nodes.clear();
nodes.resize(q * 4 + 100);
rt = 1;
lo = 0;
hi = q;
for (int i = 0; i <= maxX; ++i) {
ds.add(i, 1, 0);
}
for (int i = 0; i <= maxY; ++i) {
ds.add(i + K, 0, 1);
}
}
void insert(int x, int l, int r, int ll, int rr, pair<int, int>& v) {
if (l > rr || r < ll) {
return;
}
if (ll <= l && r <= rr) {
nodes[x].push_back(v);
return;
}
int m = (l + r) / 2;
insert(x << 1, l, m, ll, rr, v);
insert(x << 1 | 1, m + 1, r, ll, rr, v);
}
void dfs(int x, int l, int r, long long cnt, vector<long long>& res) {
int time = ds.now();
for (auto& p : nodes[x]) {
int x = p.first;
int y = p.second + K;
cnt += ds.merge(x, y);
}
if (l == r) {
res[l] = cnt;
} else {
int m = (l + r) / 2;
dfs(x << 1, l, m, cnt, res);
dfs(x << 1 | 1, m + 1, r, cnt, res);
}
ds.revertTo(time);
}
};
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
Solution sol;
int q;
cin >> q;
vector<pair<int, int>> qv;
qv.reserve(q);
for (int i = 0, x = 0, y = 0; i < q; ++i) {
cin >> x >> y;
qv.emplace_back(x - 1, y - 1);
}
auto res = sol.query(qv);
for (auto r : res) {
cout << r << " ";
}
cout << "\n";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graph;
vector<int> visited;
vector<int> out;
vector<int> res;
vector<int> rev_res;
void dfs_rec_util(int u) {
rev_res[u] = res.size();
res.push_back(u);
visited[u] = true;
for (int v : graph[u])
if (!visited[v]) dfs_rec_util(v);
out[u] = res.size();
}
void dfs_rec(int s) { dfs_rec_util(s); }
int main() {
int n, q;
cin >> n >> q;
visited = vector<int>(n, false);
out = vector<int>(n);
rev_res = vector<int>(n);
graph = vector<vector<int>>(n);
for (int i = 1; i < n; i++) {
int p;
cin >> p;
graph[--p].push_back(i);
}
dfs_rec(0);
vector<int> sol(q);
for (int i = 0; i < q; i++) {
int u, k;
cin >> u >> k;
--u;
--k;
int idx = rev_res[u];
if (idx + k < out[u]) {
sol[i] = res[idx + k] + 1;
} else {
sol[i] = -1;
}
}
for (int el : sol) cout << el << endl;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
void work() {
int n; cin>>n; vector<pii> a(n);
for(int i=0;i<n;i++)
cin>>a[i].first, a[i].second = i;
sort(all(a));
// for(int i=0;i<n;i++)
// cout<<a[i].first<<" "<<a[i].second<<"\n";
int c=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i].first * a[j].first > 2*n-1)
break;
if(a[i].first * a[j].first == a[i].second+a[j].second+2)
c++;
}
}
cout<<c<<"\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin>>t;
while(t--) {
work();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et() {
puts("-1");
exit(0);
}
int qy(int a, int b, int c) {
cout << a << " " << b << " " << c << endl;
string ret;
cin >> ret;
if (ret == "X") return 1;
if (ret == "Y") return 2;
return 3;
}
int ls[2105], rs[2105], fa[2105], cur, root, id, Sz, RT;
int qls[2105], qrs[2105], sz[2105], mx[2105];
bool vis[2105];
void calSize(int x) {
sz[x] = 0;
mx[x] = 0;
if (vis[x]) return;
if (ls[x]) {
calSize(ls[x]);
sz[x] += sz[ls[x]];
mx[x] = max(mx[x], sz[ls[x]]);
}
if (rs[x]) {
calSize(rs[x]);
sz[x] += sz[rs[x]];
mx[x] = max(mx[x], sz[rs[x]]);
}
sz[x]++;
mx[x] = max(mx[x], Sz - sz[x]);
if (mx[x] < mx[RT] && qls[x] != qrs[x]) RT = x;
}
void cal(int rt, int size) {
if (size <= 1) {
if (qls[rt] != qrs[rt]) {
int ans = qy(qls[rt], qrs[rt], id);
if (ans == 2)
rt = rs[rt];
else if (ans == 3) {
rt = ls[rt];
}
}
cur++;
ls[cur] = rt;
rs[cur] = id;
qls[cur] = qls[rt];
qrs[cur] = id;
fa[cur] = fa[rt];
if (fa[rt] != 0) {
int f = fa[rt];
if (ls[f] == rt)
ls[f] = cur;
else
rs[f] = cur;
} else
root = cur;
fa[rt] = fa[id] = cur;
return;
}
Sz = size;
RT = 0;
calSize(rt);
int ans = qy(qls[RT], qrs[RT], id);
vis[RT] = 1;
if (ans == 1)
cal(rt, size - sz[RT]);
else if (ans == 2)
cal(rs[RT], sz[rs[RT]]);
else
cal(ls[RT], sz[ls[RT]]);
}
void fmain(int tid) {
cin >> n;
root = cur = n + 1;
fa[1] = fa[2] = root;
for (int(i) = 1; (i) <= (int)(n); (i)++) qls[i] = qrs[i] = i;
ls[root] = qls[root] = 1;
rs[root] = qrs[root] = 2;
mx[0] = (1 << 30);
for (id = 3; id <= n; id++) {
memset(vis, 0, sizeof vis);
cal(root, id * 2 - 3);
}
cout << -1 << endl;
for (int(i) = 1; (i) <= (int)(n + n - 1); (i)++) {
if (fa[i] == 0) fa[i] = -1;
cout << fa[i] << " ";
}
cout << endl;
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| 12 |
/*
__ __ _ __ __
| || | (_) | | | |
_ _ | || |_ _ _ __ ___ __ _ | |_ ___ _ __ __ _ | |
| | | || || __|| || '_ ` _ \ / _` || __| / _ \ | '_ \ / _` || |
| |_| || || |_ | || | | | | || (_| || |_ | __/ ___ | |_) || (_| || |
\__,_||_| \__||_||_| |_| |_| \__,_| \__| \___||___|| |__/ \__,_| |_|
| |
|_|
++++++++++++++++++BE THE ULTIMATE VERSION OF YOURSELF++++++++++++++++++
*/
#include<bits/stdc++.h>
using namespace std;
#define ultimate ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ull unsigned long long
#define ld long double
#define vll vector <long long>
#define sll set <long long>
#define pb push_back
#define test long long tcse;cin>>tcse;while(tcse--)
#define f(start,from,to) for(start=from;start<to;start++)
#define el '\n'
#define pll pair<long long,long long>
#define vsort(vect) sort(vect.begin(),vect.end())
#define mine min_element
#define maxe max_element
#define ub upper_bound
#define lb lower_bound
#define F first
#define S second
#define mod 1000000007
bool chk(ll a[], ll n, vll od, vll ev)
{
ll i,j,k,v; j=k=0;
f(i,0,n)
{
if(i&1) v=od[j++];
else v=ev[k++];
if(a[i]!=v) return false;
}
return true;
}
void solution()
{
ll i,n; cin>>n;
ll a[n];
f(i,0,n) cin>>a[i];
vll od,ev;
f(i,0,n)
{
if(i&1) od.pb(a[i]);
else ev.pb(a[i]);
}
sort(a,a+n);
vsort(od); vsort(ev);
if(chk(a,n,od,ev)) cout<<"YES"<<el;
else cout<<"NO"<<el;
}
int32_t main()
{
ultimate
test
solution();
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> v(2 * n);
vector<long long> even;
vector<long long> odd;
for (long long i = 0; i < 2 * n; i++) {
cin >> v[i];
if (v[i] % 2 == 0)
even.push_back(i);
else
odd.push_back(i);
}
if (even.size() % 2 == 0 || odd.size() % 2 == 0) {
if (even.size() > 0) {
for (long long i = 0; i < even.size() - 2; i = i + 2) {
cout << even[i] + 1 << " " << even[i + 1] + 1 << endl;
}
if (odd.size() > 0) {
for (long long i = 0; i < odd.size() - 1; i = i + 2) {
cout << odd[i] + 1 << " " << odd[i + 1] + 1 << endl;
}
}
} else {
for (long long i = 0; i < odd.size() - 2; i = i + 2) {
cout << odd[i] + 1 << " " << odd[i + 1] + 1 << endl;
}
}
} else {
for (long long i = 1; i < even.size(); i = i + 2) {
cout << even[i] + 1 << " " << even[i + 1] + 1 << endl;
}
for (long long i = 1; i < odd.size(); i = i + 2) {
cout << odd[i] + 1 << " " << odd[i + 1] + 1 << endl;
}
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int w, k, n;
int sum = 0;
cin >> k;
cin >> n;
cin >> w;
for (int i = 1; i <= w; i++) {
sum = sum + (k * i);
}
if (sum > n) {
int x = sum - n;
cout << x << endl;
} else {
cout << 0 << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 25, maxm = 1e5 + 111, maxs = (1 << 20) + 7, mo = 998244353;
int N, M, tot;
char mp[maxn][maxm];
int a[maxs], b[maxs];
int work(int l) {
int res = 0;
for (int i = 1; i <= N; ++i) res = res << 1 | (mp[i][l] - '0');
return res;
}
int ksm(int x, int t) {
int res = 1;
while (t) {
if (t & 1) res = 1ll * res * x % mo;
x = 1ll * x * x % mo;
t >>= 1;
}
return res;
}
void XOR(int *f, int x) {
int aa, bb;
for (int k = 2; k <= tot; k <<= 1)
for (int i = 0; i < tot; i += k)
for (int j = i; j < i + k / 2; ++j) {
aa = f[j], bb = f[j + k / 2];
f[j] = (aa + bb) % mo;
f[j + k / 2] = (aa - bb + mo) % mo;
f[j] = 1ll * f[j] * x % mo;
f[j + k / 2] = 1ll * f[j + k / 2] * x % mo;
}
}
void MUL() {
for (int i = 0; i < tot; ++i) a[i] = 1ll * a[i] * b[i] % mo;
}
int main() {
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; ++i) scanf("%s", mp[i] + 1);
for (int i = 1; i <= M; ++i) a[work(i)]++;
tot = 1 << N;
for (int i = 0; i < tot; ++i)
b[i] = min(__builtin_popcount(i), N - __builtin_popcount(i));
XOR(a, 1);
XOR(b, 1);
MUL();
XOR(a, ksm(2, mo - 2));
int ans = N * M;
for (int i = 0; i < tot; ++i) ans = min(ans, a[i]);
cout << ans;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 2e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
map<pair<long long, long long>, long long> now;
int dfs(long long a, long long b) {
if (a > b) swap(a, b);
bool was = now.count(make_pair(a, b));
long long &res = now[make_pair(a, b)];
if (!a) return res = 0;
if (was) return res;
res = max(res, (long long)dfs(a, b % a) ^ 1);
long long x = b / a - 1;
if (a % 2 == 1) {
res = max(res, x % 2);
} else {
x %= a + 1;
res = max(res, x % 2);
if (x == a) res = 1;
}
return res;
}
int main(void) {
int t;
scanf("%d", &t);
for (int(i) = 0; (i) < (t); ++(i)) {
long long x, y;
cin >> x >> y;
puts(dfs(x, y) ? "First" : "Second");
}
return 0;
}
| 7 |
//Created by: Aditya Singh
//Bleed Blue
#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize("O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int,int>
#define unm map<int,int>
#define pb emplace_back
#define PI 3.1415926535897932384626433
#define inf 1e18
const int N = 300005;
vector<int> gr[2 * N];
string s;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t; cin >> t; while (t--)
{
int i, j, k, n, m, ans = 0, sum = 0;
cin >> n >> s;
for (i = 0; i <= 2 * n + 1; i++)
gr[i].clear();
vector<int> comp(2 * n + 2, -1), cnt;
for (i = 0; i < n; i++)
{
if (s[i] == 'L')
gr[2 * (i + 1)].pb(2 * i + 1), gr[2 * i + 1].pb(2 * (i + 1));
else
gr[i * 2].pb(2 * (i + 1) + 1), gr[2 * (i + 1) + 1].pb(i * 2);
}
for (i = 0; i < 2 * n + 2; i++)
{
if (comp[i] != -1)
continue;
int c = 1;
m = cnt.size();
queue<int> q;
q.push(i);
comp[i] = m;
while (!q.empty())
{
int cur = q.front();
q.pop();
for (int x : gr[cur])
{
if (comp[x] == -1)
{
c++;
comp[x] = m;
q.push(x);
}
}
}
cnt.pb(c);
}
for (i = 0; i <= n; i++)
cout << cnt[comp[i * 2]] << ' ';
cout << '\n';
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
static void redirect(int argc, const char **argv) {
ios::sync_with_stdio(false);
if (argc > 1) {
static filebuf f;
f.open(argv[1], ios::in);
cin.rdbuf(&f);
if (!cin) {
cerr << "Failed to open '" << argv[1] << "'" << endl;
exit(1);
}
}
if (argc > 2) {
static filebuf f;
f.open(argv[2], ios::out | ios::trunc);
cout.rdbuf(&f);
if (!cout) {
cerr << "Failed to open '" << argv[2] << "'" << endl;
}
}
}
int main(int argc, const char **argv) {
redirect(argc, argv);
int N;
cin >> N;
int last = 0;
vector<pair<int, int> > exams(N);
for (int i = 0; i < N; i++) cin >> exams[i].first >> exams[i].second;
sort(begin(exams), end(exams));
for (int i = 0; i < N; i++) {
if (exams[i].second >= last)
last = exams[i].second;
else
last = exams[i].first;
}
cout << last << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
int a[maxn][maxn], n, cnt;
void nhap() {
scanf("%d\n", &n);
cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
char c = getchar();
a[i][j] = c - '0';
if (a[i][j] == 3) {
printf("no\n");
exit(0);
}
cnt += (a[i][j] != 0);
}
scanf("\n");
}
}
bool ktr(int lx, int ly) {
int rx = 0, ry = 0;
for (int i = lx + 1; i <= n; i++)
if (a[i][ly] == 1) {
rx = i;
break;
}
for (int i = ly + 1; i <= n; i++)
if (a[lx][i] == 1) {
ry = i;
break;
}
if (rx == 0 || ry == 0) return 0;
for (int i = lx + 1; i < rx; i++) {
if (a[i][ly] != 2 || a[i][ry] != 2) return 0;
}
for (int i = ly + 1; i < ry; i++) {
if (a[lx][i] != 2 || a[rx][i] != 2) return 0;
}
int dem = 0;
for (int i = lx + 1; i < rx; i++) {
for (int j = ly + 1; j < ry; j++) {
if (a[i][j] != 4) return 0;
dem++;
}
}
if ((rx - lx + 1) * (ry - ly + 1) != cnt) return 0;
if (dem == 0) return 0;
return 1;
}
bool tinh() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (a[i][j] == 1) {
return ktr(i, j);
}
}
return 0;
}
int main() {
nhap();
if (tinh())
printf("yes\n");
else
printf("no\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
const int N = 3e3 + 5;
int R, C, n, k, sum[N], tmp[N], las[N], nex[N];
std::vector<int> g[N];
int main() {
scanf("%d%d%d%d", &R, &C, &n, &k);
long long ans = 0, res;
for (int i = 1, x, y; i <= n; ++i) scanf("%d%d", &x, &y), g[x].push_back(y);
for (int u = R, l, r, all; u >= 1; --u) {
for (auto o : g[u]) ++sum[o];
l = 1;
r = all = 0;
res = 0;
while (true) {
while (r + 1 <= C && all < k) all += sum[++r];
if (all < k) break;
while (l <= r && all >= k) res += C - r + 1, all -= sum[l++];
}
for (int i = 1; i <= C; ++i) {
tmp[i] = sum[i];
las[i] = i - 1;
nex[i] = i + 1;
}
for (int i = 1; i <= C; ++i)
if (!sum[i]) {
las[nex[i]] = las[i];
nex[las[i]] = nex[i];
}
for (int d = R; d >= u; --d) {
ans += res;
int suml, sumr;
for (auto o : g[d]) {
l = r = o;
suml = tmp[o];
sumr = 0;
while (nex[r] <= C && suml + sumr < k) sumr += tmp[r = nex[r]];
while (true) {
if (suml + sumr == k) res -= (l - las[l]) * (nex[r] - r);
suml += tmp[l = las[l]];
if (!l || suml > k) break;
while (suml + sumr > k) sumr -= tmp[r], r = las[r];
}
if (!--tmp[o]) las[nex[o]] = las[o], nex[las[o]] = nex[o];
}
}
}
printf("%lld\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, i, ans, x, y, z, ans1, ans2;
long long tmp2, a[100001];
int main() {
scanf("%d", &n);
a[0] = 0;
for (i = 1; i <= n; i++) {
tmp2 = 0;
scanf("%ld", &a[i]);
while (a[i] > 0) {
tmp2 += (a[i] % 10);
a[i] /= 10;
}
a[i] = tmp2 % 3;
}
sort(a, a + n + 1);
x = 0;
y = 0;
z = 0;
for (i = 1; i <= n; i++) {
if (a[i] == 1) {
x++;
} else if (a[i] == 2) {
y++;
} else if (a[i] == 0) {
z++;
}
}
ans1 = min(x, y);
ans2 = z / 2;
cout << ans1 + ans2 << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 30; ~i; i--)
if (n >> i & 1) cout << i + 1 << ' ';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
char s[100005];
queue<int> q1, q2;
int solve(int f) {
int ans = 0, p1 = 0, p2 = 0;
if (f == 1) {
for (int i = 0; i < n; i++) {
if (i % 2 == 1 && s[i] == 'b') p1++;
if (i % 2 == 0 && s[i] == 'r') p2++;
}
return abs(p2 - p1) + min(p2, p1);
} else {
for (int i = 0; i < n; i++) {
if (i % 2 == 1 && s[i] == 'r') p1++;
if (i % 2 == 0 && s[i] == 'b') p2++;
}
return abs(p2 - p1) + min(p2, p1);
}
}
int main() {
scanf("%d", &n);
scanf("%s", s);
while (!q1.empty()) q1.pop();
while (!q2.empty()) q2.pop();
int c1, c2;
c1 = solve(1);
c2 = solve(0);
printf("%d\n", min(c1, c2));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string n;
string n2 = "hello";
cin >> n;
int j = 0, pas = 0;
for (int i = 0; i < n.length(); i++) {
if (n[i] == n2[j]) {
j++;
pas++;
}
if (pas == 5) break;
}
if (pas == 5)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int rd() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
return x * f;
}
const int N = 300005;
bool vis[N];
int n, m;
vector<int> v;
void Main() {
n = rd(), m = rd();
for (int i = 1; i <= n * 3; ++i) vis[i] = 0;
v.clear();
for (int i = 1; i <= m; ++i) {
int x = rd(), y = rd();
if (!vis[x] && !vis[y]) vis[x] = vis[y] = 1, v.push_back(i);
}
if (v.size() >= n) {
puts("Matching");
for (int i = 0; i < n; ++i) printf("%d ", v[i]);
} else {
puts("IndSet");
for (int i = 1, j = 1; j <= n; ++i)
if (!vis[i]) printf("%d ", i), ++j;
}
puts("");
}
signed main() {
for (int T = rd(); T; --T) Main();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
static const int MAX = 27 * 200;
long long a[222];
int f[2][222][11111];
int V(int a) { return min(max(0, a + MAX), MAX * 2); }
int get(long long a, int b) {
int cnt = 0;
while (a % b == 0) {
++cnt;
a /= b;
}
return cnt;
}
void upd(int& a, int b) {
if (a == -1) {
a = b;
} else {
a = max(a, b);
}
}
int sign(int a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); }
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int x = 0, y = 1;
memset(f[x], 0xff, sizeof(f[x]));
f[x][0][V(0)] = 0;
for (int i = 0; i < n; ++i, swap(x, y)) {
int cnt2 = get(a[i], 2);
int cnt5 = get(a[i], 5);
memset(f[y], 0xff, sizeof(f[y]));
for (int used = 0; used <= k; ++used) {
for (int two_five = -MAX; two_five <= MAX; ++two_five) {
if (f[x][used][V(two_five)] == -1) {
continue;
}
upd(f[y][used][V(two_five)], f[x][used][V(two_five)]);
if (used + 1 <= k) {
int add = min(cnt2, cnt5);
if (sign(two_five) * sign(cnt2 - cnt5) == -1) {
add += min(abs(two_five), abs(cnt5 - cnt2));
}
int nxt_two_five = two_five + cnt2 - cnt5;
upd(f[y][used + 1][V(nxt_two_five)], f[x][used][V(two_five)] + add);
}
}
}
}
int ans = 0;
for (int two_five = -MAX; two_five <= MAX; ++two_five) {
if (f[x][k][V(two_five)] == -1) {
continue;
}
ans = max(ans, f[x][k][V(two_five)]);
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int i, j, n = s.size();
vector<int> v;
v.clear();
int cr = 0, ind, ccr;
for (i = 0; i < n; ++i) {
if (s[i] == '(')
++cr;
else {
--cr;
if (s[i] == '#') ccr = cr, ind = i, v.push_back(1);
}
if (cr < 0) {
cout << -1;
return 0;
}
}
if (cr > ccr) {
cout << -1;
return 0;
}
v[v.size() - 1] += cr;
ccr -= cr;
for (i = ind + 1; i < n; ++i) {
if (s[i] == '(')
++ccr;
else
--ccr;
if (ccr < 0) {
cout << -1;
return 0;
}
}
for (auto it : v) cout << it << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct car {
int start, end, here;
car(int s = 0, int e = 0, int h = 0) : start(s), end(e), here(h) {}
};
bool operator<(const car& op1, const car& op2) {
return make_pair(op1.start, make_pair(op1.end, op1.here)) <
make_pair(op2.start, make_pair(op2.end, op2.here));
}
bool operator==(const car& op1, const car& op2) {
return make_pair(op1.start, make_pair(op1.end, op1.here)) ==
make_pair(op2.start, make_pair(op2.end, op2.here));
}
int main() {
int L, b, f;
cin >> L >> b >> f;
vector<car> c, norm;
c.push_back(car(-b, -100 - b, 1));
c.push_back(car(L + f + 100, L + f, 1));
sort(c.begin(), c.end());
int n;
cin >> n;
for (int k = 0; k < n; ++k) {
int type;
cin >> type;
if (type == 1) {
int len;
cin >> len;
int fit = -1;
for (int i = 0; i < c.size(); ++i) {
if (!c[i].here) continue;
int near = -1;
for (int j = 0; j < c.size(); ++j) {
if (i == j || !c[j].here) continue;
if (c[j].end > c[i].start)
if (near == -1 || c[j].end < c[near].end) near = j;
}
if (near != -1)
if (c[near].end - c[i].start >= b + f + len) {
fit = c[i].start + b;
c.push_back(car(fit + len, fit, 1));
norm.push_back(car(fit + len, fit, 1));
break;
}
}
if (fit == -1) norm.push_back(car());
sort(c.begin(), c.end());
cout << fit << endl;
} else {
norm.push_back(car());
int ind;
cin >> ind;
ind--;
assert(norm[ind].here == 1);
for (int i = 0; i < c.size(); ++i)
if (norm[ind] == c[i]) {
c[i].here = 0;
norm[ind] = c[i];
break;
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> A(n);
vector<int> N(n), C(n, n);
for (int i = 0; i < n; i++) {
cin >> A[i];
A[i]--;
}
for (int i = n - 1; i >= 0; i--) {
N[i] = C[A[i]];
C[A[i]] = i;
}
set<int> s;
set<pair<int, int> > q;
int res = 0;
for (int i = 0; i < n; i++) {
if (s.size() < k) {
if (s.find(A[i]) == s.end()) {
res++;
s.insert(A[i]);
q.insert(make_pair(N[i], A[i]));
} else {
q.erase(make_pair(i, A[i]));
q.insert(make_pair(N[i], A[i]));
}
} else {
if (s.find(A[i]) == s.end()) {
pair<int, int> curr = *(--q.end());
int who = curr.second;
s.erase(who);
res++;
q.erase(curr);
q.insert(make_pair(N[i], A[i]));
s.insert(A[i]);
} else {
q.erase(make_pair(i, A[i]));
q.insert(make_pair(N[i], A[i]));
}
}
}
cout << res << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct darling {
int t, l;
} s[N];
struct car {
int ls, rs, ms;
int flag;
} a[N * 4];
void PushUp(int u, int m) {
a[u].ls = a[u << 1].ls;
a[u].rs = a[u << 1 | 1].rs;
if (a[u].ls == m - (m >> 1)) a[u].ls += a[u << 1 | 1].ls;
if (a[u].rs == (m >> 1)) a[u].rs += a[u << 1].rs;
a[u].ms =
max(a[u << 1 | 1].ls + a[u << 1].rs, max(a[u << 1].ms, a[u << 1 | 1].ms));
}
void PushDown(int u, int m) {
if (a[u].flag != -1) {
a[u << 1].flag = a[u << 1 | 1].flag = a[u].flag;
a[u << 1].ms = a[u << 1].ls = a[u << 1].rs = a[u].flag ? 0 : m - (m >> 1);
a[u << 1 | 1].ms = a[u << 1 | 1].ls = a[u << 1 | 1].rs =
a[u].flag ? 0 : (m >> 1);
a[u].flag = -1;
}
}
void build(int l, int r, int u) {
a[u].ms = a[u].ls = a[u].rs = r - l + 1;
a[u].flag = -1;
if (l == r) return;
int m = (l + r) >> 1;
build(l, m, u << 1);
build(m + 1, r, u << 1 | 1);
}
void update(int l1, int r1, int c, int l, int r, int u) {
if (l1 <= l && r <= r1) {
a[u].ms = a[u].ls = a[u].rs = c ? 0 : r - l + 1;
a[u].flag = c;
return;
}
PushDown(u, r - l + 1);
int m = (l + r) >> 1;
if (l1 <= m) update(l1, r1, c, l, m, u << 1);
if (m < r1) update(l1, r1, c, m + 1, r, u << 1 | 1);
PushUp(u, r - l + 1);
}
int query(int w, int l, int r, int u) {
if (l == r) return l;
PushDown(u, r - l + 1);
int m = (l + r) >> 1;
if (a[u << 1].ms >= w)
return query(w, l, m, u << 1);
else if (a[u << 1].rs + a[u << 1 | 1].ls >= w)
return m - a[u << 1].rs + 1;
return query(w, m + 1, r, u << 1 | 1);
}
int main() {
int L1, b, f;
while (~scanf("%d%d%d", &L1, &b, &f)) {
build(-b, L1 + f, 1);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int op, x;
scanf("%d%d", &op, &x);
if (op == 1) {
s[i].l = x;
if (a[1].ms < x + b + f + 1) {
puts("-1");
s[i].t = -1;
} else {
int p = query(x + b + f, -b, L1 + f, 1);
printf("%d\n", p + b);
s[i].t = p + b;
s[i].l = x;
update(p + b, p + b + x - 1, 1, -b, L1 + f, 1);
}
} else {
s[i].t = -1;
update(s[x].t, s[x].t + s[x].l - 1, 0, -b, L1 + f, 1);
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const long long INF = 8e18;
const int maxn = 1e4 + 5;
const int maxm = 1e3 + 5;
long long dis[maxn][maxm], a[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) cin >> a[i];
sort(a + 1, a + 1 + m);
int g, r;
cin >> g >> r;
for (int i = 0; i < maxn; ++i)
for (int j = 0; j < maxm; ++j) dis[i][j] = inf;
deque<pair<int, int> > dq;
dq.push_back({1, g});
dis[1][g] = 0;
while (!dq.empty()) {
auto now = dq.front();
dq.pop_front();
int i = now.first, j = now.second;
if (i < m) {
int ni = i + 1, nj = j - a[i + 1] + a[i];
if (nj > 0) {
if (dis[ni][nj] > dis[i][j]) {
dis[ni][nj] = dis[i][j];
dq.push_front({ni, nj});
}
} else if (nj == 0) {
nj = g;
if (dis[ni][nj] > dis[i][j] + 1) {
dis[ni][nj] = dis[i][j] + 1;
dq.push_back({ni, nj});
}
}
}
if (i > 1) {
int ni = i - 1, nj = j - a[i] + a[i - 1];
if (nj > 0) {
if (dis[ni][nj] > dis[i][j]) {
dis[ni][nj] = dis[i][j];
dq.push_front({ni, nj});
}
} else if (nj == 0) {
nj = g;
if (dis[ni][nj] > dis[i][j] + 1) {
dis[ni][nj] = dis[i][j] + 1;
dq.push_back({ni, g});
}
}
}
}
long long ans = INF;
for (int j = 0; j < g; ++j) {
if (dis[m][j] == inf) continue;
ans = min(ans, dis[m][j] * (g + r) + g - j);
}
if (dis[m][g] != inf) ans = min(ans, dis[m][g] * (g + r) - r);
if (ans == INF)
cout << -1 << '\n';
else
cout << ans << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
using max_pq = priority_queue<T>;
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
class BSuffixStructures {
int arr1[256] = {0}, arr2[256] = {0};
bool checkAuto(string s1, string s2) {
int i = 0, j = 0;
while (i < s1.length() && j < s2.length()) {
if (s1[i] == s2[j]) {
i++;
j++;
} else
i++;
}
if (j == s2.length()) return 1;
return 0;
}
bool checkArray(string s1, string s2) {
for (int i = 0; i <= s1.length() - 1; i++) arr1[s1[i]]++;
for (int i = 0; i <= s2.length() - 1; i++) arr2[s2[i]]++;
for (int i = 0; i <= 255; i++)
if (arr1[i] != arr2[i]) return 0;
return 1;
}
bool checkBoth(string s1, string s2) {
for (int i = 0; i <= 255; i++)
if (arr1[i] < arr2[i]) return 0;
return 1;
}
public:
void solve(std::istream& cin, std::ostream& cout) {
string s1, s2;
cin >> s1 >> s2;
if (checkAuto(s1, s2))
cout << "automaton";
else if (checkArray(s1, s2))
cout << "array";
else if (checkBoth(s1, s2))
cout << "both";
else
cout << "need tree";
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
BSuffixStructures solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int bag[1005];
int main() {
int n, m, c0, d0, a, b, c, d, ans;
scanf("%d%d%d%d", &n, &m, &c0, &d0);
memset(bag, -1, sizeof(bag));
bag[0] = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
for (int j = 0; j < a / b; j++) {
for (int k = n; k >= c; k--) {
if (bag[k - c] != -1) {
bag[k] = max(bag[k], bag[k - c] + d);
}
}
}
}
ans = 0;
for (int i = 0; i <= n; i++) {
if (bag[i] != -1) {
ans = max(ans, bag[i] + (n - i) / c0 * d0);
}
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include "bits/stdc++.h"
using namespace std;
template<class Xs>
int sz(const Xs &xs) { return static_cast<int>(xs.size()); }
using i64 = int64_t;
using f80 = long double;
using Str = string;
template<class T = int> using Vec = vector<T>;
template<class K = int, class H = hash<K>> using US = unordered_set<K, H>;
template<class K, class V, class H = hash<K>> using UM = unordered_map<K, V, H>;
template<class U = int, class V = U> using P = pair<U, V>;
using G = Vec<Vec<int>>;
struct Solver {
Solver(G g) : g(g) { n= g.size();}
Vec<P<P<int>>> getAns() {
edges[0].resize(n);
edges[1].resize(n);
dp[0].resize(n);
dp[1].resize(n);
dfs(0);
dfs2(0);
sort(left.begin(), left.end());
Vec<int> degree(n);
for (int i = 0; i < n; ++i) degree[i] = g[i].size();
vector<P<int>> deleted;
Vec<P<int>> added;
for (int i = 0; i < n; ++i) for (int j : g[i])
{
if (i < j)
{
if (!binary_search(left.begin(), left.end(), P<int>{i, j}))
{
deleted.emplace_back(i, j);
degree[i]--;
degree[j]--;
}
}
}
g.assign(n, Vec<int>());
visited.assign(n, false);
for (auto [x, y] : left)
{
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 0; i < n; ++i)
{
if (!visited[i]) {
comps.push_back(Vec<int>());
comp(i);
}
}
for (int i = 0; i + 1 < comps.size();++i)
{
int v = 0;
int u;
for (int x : comps[i]) if (degree[x] <= 1) {
v = x;
degree[v]++;
break;
}
for (int x : comps[i + 1]) if (degree[x] <= 1) {
u = x;
degree[u]++;
break;
}
added.emplace_back(u, v);
}
Vec<P<P<int>>> res;
for (int i = 0; i < deleted.size(); ++i)
res.emplace_back(deleted[i], added[i]);
return res;
}
void comp(int v)
{
visited[v] = true;
comps.back().push_back(v);
for (int u : g[v]) if (!visited[u]) comp(u);
}
void dfs(int vert, int par = -1)
{
Vec<P<int>> lose_if_edge;
i64 res = 0;
for (int nxt: g[vert])
{
if (nxt != par)
{
dfs(nxt, vert);
lose_if_edge.emplace_back(dp[0][nxt] - dp[1][nxt], nxt);
res += dp[0][nxt];
}
}
sort(lose_if_edge.begin(), lose_if_edge.end());
int num = 0;
while (num < min<int>(2, lose_if_edge.size()) && lose_if_edge[num].first == 0)
{
++num;
}
for (int i = 0; i< num;++i)
{
edges[0][vert].push_back(lose_if_edge[i].second);
}
dp[0][vert] = res + num;
if (num >= 1)
{
edges[1][vert].push_back(lose_if_edge[0].second);
}
dp[1][vert] = res + min(num, 1);
}
void dfs2(int vert, int par=-1, int i=0)
{
for (auto nxt : edges[i][vert])
{
if (nxt < vert)
{
left.emplace_back(nxt, vert);
}
else
left.emplace_back(vert, nxt);
}
for (int nxt : g[vert])
{
if (nxt == par) continue;
bool used = false;
for (int in : edges[i][vert])
{
if (nxt == in) used = true;
}
dfs2(nxt, vert, (used ? 1 : 0));
}
}
Vec<bool> visited;
Vec<Vec<int>> comps;
Vec<P<int>> left;
G g;
array<Vec<int>, 2> dp;
int n;
array<G, 2> edges;
};
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i)
{
int n; cin >> n;
G g(n);
for (int j = 0; j < n - 1; ++j)
{
int a, b;
cin >> a >> b;--a;--b;
g[a].push_back(b);
g[b].push_back(a);
}
auto ans = Solver(g).getAns();
cout << ans.size() << '\n';
for (auto [x, y]: ans)
{
cout << x.first + 1 << ' ' << x.second + 1 << ' ' << y.first + 1 << ' ' << y.second + 1 << '\n';
}
}
}
namespace {
auto fast_io = [] {
#ifndef DEBUG
# ifndef INTERACTIVE
ios::sync_with_stdio(false);
cin.tie(nullptr);
# endif // INTERACTIVE
# ifdef FILES
freopen(FILES".in", "r", stdin);
freopen(FILES".out", "w", stdout);
# endif // FILES
#endif // DEBUG
cout << setprecision(8) << fixed;
cerr << boolalpha << setprecision(4) << fixed;
return 0;
}();
} // namespace | 8 |
#include <bits/stdc++.h>
int n, s, x, mx, sum;
int main() {
scanf("%d%d", &n, &s);
for (register int i = 0; i < n; ++i) {
scanf("%d", &x);
sum += x;
if (x > mx) mx = x;
}
if (sum - mx > s)
printf("NO\n");
else
printf("YES\n");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> pair) {
return out << "(" << pair.first << ", " << pair.second << ")";
}
int N;
int sany[2][1029];
int x, k;
int yary[100009];
int al[100009];
int main() {
for (int i = (0); i <= (100009 - 3); ++i) yary[i] = i / 2 + (i % 2);
cin >> N >> k >> x;
for (int i = (0); i <= (1024); ++i) al[i] = i ^ x;
int ai;
for (int i = (1); i <= (N); ++i) {
cin >> ai;
sany[1][ai]++;
}
int pnt = 1;
int now;
int ____next;
int how;
for (int uw = (1); uw <= (k); ++uw) {
now = uw % 2;
____next = (uw + 1) % 2;
memset(sany[____next], 0, sizeof(sany[____next]));
pnt = 1;
for (int i = (0); i <= (1023); ++i) {
how = sany[now][i];
if (pnt == 1) {
sany[____next][al[i]] += yary[how];
sany[____next][i] += how - yary[how];
if ((how & 1) == 1) pnt = 0;
} else {
sany[____next][al[i]] += how - yary[how];
sany[____next][i] += yary[how];
if ((how & 1) == 1) pnt = 1;
}
}
}
int mn = 111111;
int mx = 0;
now = (k + 1) % 2;
for (int i = (0); i <= (1023); ++i)
if (sany[now][i] > 0) {
mx = i;
mn = min(mn, i);
}
cout << mx << " " << mn;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int dp[300];
void check(int &x, int y) {
if (x == -1 || x > y) x = y;
}
int a[10], b[10], n;
void dfs(int x) {
if (x == dp[n] + 1) return;
bool mark[300];
memset(mark, false, sizeof(mark));
for (int i = 0; i < x; i++) {
if (a[i] * 2 <= n) mark[a[i] * 2] = 1;
if (a[i] * 4 <= n) mark[a[i] * 4] = 1;
if (a[i] * 8 <= n) mark[a[i] * 8] = 1;
}
for (int i = 0; i < x; i++)
for (int j = 0; j < x; j++) {
if (a[i] + a[j] * 1 <= n) mark[a[i] + a[j] * 1] = 1;
if (a[i] + a[j] * 2 <= n) mark[a[i] + a[j] * 2] = 1;
if (a[i] + a[j] * 4 <= n) mark[a[i] + a[j] * 4] = 1;
if (a[i] + a[j] * 8 <= n) mark[a[i] + a[j] * 8] = 1;
}
if (mark[n]) {
dp[n] = x;
a[x] = n;
memcpy(b, a, sizeof(a));
return;
}
for (int k = a[x - 1] + 1; k < n; k++)
if (mark[k]) {
a[x] = k;
dfs(x + 1);
}
}
void writ(int x) {
for (int i = 0; i < x; i++)
for (int k = 0; k < 4; k++)
if (b[i] * (1 << k) == b[x]) {
printf("lea e%cx, [%d*e%cx]\n", 'a' + x, (1 << k), 'a' + i);
return;
}
for (int i = 0; i < x; i++)
for (int j = 0; j < x; j++)
for (int k = 0; k < 4; k++)
if (b[i] + b[j] * (1 << k) == b[x]) {
if (k)
printf("lea e%cx, [e%cx + %d*e%cx]\n", 'a' + x, 'a' + i, (1 << k),
'a' + j);
else
printf("lea e%cx, [e%cx + e%cx]\n", 'a' + x, 'a' + i, 'a' + j);
return;
}
}
int main() {
memset(dp, -1, sizeof(dp));
dp[1] = 0;
for (int i = 2; i <= 255; i++) {
for (int j = 1; j < i; j++) {
check(dp[i], dp[j] + dp[i - j] + 1);
if ((i - j) % 2 == 0) check(dp[i], dp[j] + dp[(i - j) / 2] + 1);
if ((i - j) % 4 == 0) check(dp[i], dp[j] + dp[(i - j) / 4] + 1);
if ((i - j) % 8 == 0) check(dp[i], dp[j] + dp[(i - j) / 8] + 1);
}
if ((i) % 2 == 0) check(dp[i], dp[i / 2] + 1);
if ((i) % 4 == 0) check(dp[i], dp[i / 4] + 1);
if ((i) % 8 == 0) check(dp[i], dp[i / 8] + 1);
}
scanf("%d", &n);
a[0] = 1;
dfs(1);
printf("%d\n", dp[n]);
for (int i = 1; i <= dp[n]; i++) writ(i);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int a[N], b[N], p[N], n, tid, ans[N];
pair<int, int> seg[N << 2];
bool did[N];
void build(int l, int r, int rt) {
if (l == r) {
seg[rt] = {b[l], l};
return;
}
int m = l + r >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]);
}
void del(int pos, int l, int r, int rt) {
if (l == r) {
seg[rt] = {0, 0};
return;
}
int m = l + r >> 1;
if (pos <= m)
del(pos, l, m, rt << 1);
else
del(pos, m + 1, r, rt << 1 | 1);
seg[rt] = max(seg[rt << 1], seg[rt << 1 | 1]);
}
pair<int, int> query(int L, int R, int l, int r, int rt) {
if (L <= l && R >= r) return seg[rt];
int m = l + r >> 1;
pair<int, int> ans = {0, 0};
if (L <= m) ans = max(ans, query(L, R, l, m, rt << 1));
if (R > m) ans = max(ans, query(L, R, m + 1, r, rt << 1 | 1));
return ans;
}
void dfs(int x) {
did[x] = true;
del(x, 1, n, 1);
if (b[x] != n + 1 && !did[b[x]]) dfs(b[x]);
if (a[x] > 1) {
while (1) {
pair<int, int> t = query(1, a[x] - 1, 1, n, 1);
if (t.first > x)
dfs(t.second);
else
break;
}
}
p[++tid] = x;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] == -1)
a[i] = n + 1;
else
b[a[i]] = i;
}
for (int i = 1; i <= n; i++)
if (!b[i]) b[i] = n + 1;
build(1, n, 1);
for (int i = 1; i <= n; i++)
if (!did[i]) dfs(i);
for (int i = 1; i <= n; i++) ans[p[i]] = i;
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
int tree[maxn << 2], id[maxn << 2];
int ans[maxn], pos[maxn], L[maxn], R[maxn];
int n;
vector<pair<int, int> > v[maxn];
void build(int o, int l, int r) {
if (l == r) {
tree[o] = L[pos[l]];
id[o] = l;
return;
}
int mid = l + r >> 1;
build(o << 1, l, mid);
build(o << 1 | 1, mid + 1, r);
if (tree[o << 1] < tree[o << 1 | 1])
id[o] = id[o << 1];
else
id[o] = id[o << 1 | 1];
tree[o] = min(tree[o << 1 | 1], tree[o << 1]);
}
pair<int, int> query(int o, int l, int r, int x, int y) {
if (x <= l && y >= r) return make_pair(tree[o], id[o]);
int mid = l + r >> 1;
pair<int, int> tmp1 = make_pair(-1, -1), tmp2 = make_pair(-1, -1);
if (mid >= x) tmp1 = query(o << 1, l, mid, x, y);
if (mid < y) tmp2 = query(o << 1 | 1, mid + 1, r, x, y);
if (tmp1.first == -1) return tmp2;
if (tmp2.first == -1) return tmp1;
if (tmp1.first > tmp2.first)
return tmp2;
else
return tmp1;
}
void print() {
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << endl;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &L[i], &R[i]);
v[L[i]].push_back(make_pair(R[i], i));
}
set<pair<int, int> > s;
for (int i = 1; i <= n; i++) {
s.insert(v[i].begin(), v[i].end());
ans[(*s.begin()).second] = i;
pos[i] = (*s.begin()).second;
s.erase(s.begin());
}
build(1, 1, n);
for (int i = 1; i <= n; i++) {
pair<int, int> tmp = query(1, 1, n, i + 1, R[pos[i]]);
if (i + 1 > R[pos[i]]) continue;
if (tmp.first <= i) {
printf("NO\n");
print();
swap(ans[pos[tmp.second]], ans[pos[i]]);
print();
return 0;
}
}
printf("YES\n");
print();
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int base = 1000000007;
vector<int> d[100100];
long long power(int x, int y) {
if (!y) return 1;
long long res = power(x, y / 2);
res *= res;
res %= base;
if (y % 2) res *= x, res %= base;
return res;
}
int main() {
int n, a[100100], mx = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i], mx = max(mx, a[i]);
sort(a, a + n);
for (int i = 1; i * 2 <= mx; i++)
for (int j = i * 2; j <= mx; j += i) d[j].push_back(i);
long long ans = 1;
for (int x = 2; x <= mx; x++) {
int D = d[x].size();
int id = lower_bound(a, a + n, x) - a;
long long res = power(D + 1, n - id) - power(D, n - id);
if (res < 0) res += base;
int last = id;
for (int i = D - 1; i >= 0; i--) {
id = lower_bound(a, a + n, d[x][i]) - a;
res = res * power(i + 1, last - id) % base;
last = id;
}
ans = (ans + res) % base;
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
const int lazy = int('a' - 'A');
const long long N = 1e7 + 1;
string alphabet = "abcdefghijklmnopqrstuvwxyz";
long long sum = 0, final = 0, result = 0;
template <class T = int>
T scan() {
T res;
std::cin >> res;
return res;
}
bool pal(int n) {
string lol = to_string(n);
string s = to_string(n);
reverse(s.begin(), s.end());
if (s == lol) {
return true;
} else {
return false;
}
}
using namespace std;
const long long n = 800000;
vector<long long> prime(n + 1);
vector<long long> it_is_me_dio_hahahahahaha() {
prime[1] = 1;
for (int p = 2; p <= n; p++) {
for (int i = p; i <= n; i += p) {
prime[i] += p;
}
prime[p]++;
}
return prime;
}
vector<string> all_numbers = {
"1", "11", "111", "1111", "2", "22", "222", "2222", "3",
"33", "333", "3333", "4", "44", "444", "4444", "5", "55",
"555", "5555", "6", "66", "666", "6666", "7", "77", "777",
"7777", "8", "88", "888", "8888", "9", "99", "999", "9999"};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int sum = 0;
for (int i = 0; i < all_numbers.size(); i++) {
sum += all_numbers[i].size();
if (s == all_numbers[i]) {
break;
}
}
cout << sum << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, k, count = 0, count2 = 0;
cin >> x >> k;
int a[x];
for (int i = 0; i < x; i++) {
a[i] = i;
}
for (int i = 0; i < k; i++) {
int t1, t2, t3;
cin >> t1;
if (t1 == 1) {
cin >> t2;
a[t2] = 0;
cin >> t3;
a[t3] = 0;
} else {
cin >> t2;
a[t2] = 0;
}
}
for (int i = 1; i < x; i++) {
if (a[i] != 0) count = count + 1;
if (a[i + 1] - a[i] == 1) {
count = count + 1;
count2 = count2 + 1;
a[i] = 0;
a[i + 1] = 0;
}
}
cout << count - count2 << " " << count << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100010;
const long long inf = LONG_LONG_MAX >> 2;
long long n, m;
long long a[maxn], b[maxn];
long long ans;
int main() {
scanf("%lld %lld", &n, &m);
for (long long i = 1; i <= n; i++) scanf("%lld", a + i);
for (long long i = 1; i <= m; i++) scanf("%lld", b + i);
sort(b + 1, b + 1 + m);
for (long long i = 1; i <= n; i++) {
long long k = upper_bound(b + 1, b + 1 + m, a[i]) - b;
long long x = (k > 1 ? a[i] - b[k - 1] : inf);
long long y = (k <= m ? b[k] - a[i] : inf);
ans = max(ans, min(x, y));
}
printf("%lld\n", ans);
return 0;
}
| 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.