solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
string a;
int j = 0;
void shift(int j) {
for (int i = 0; i < a.size(); i++) {
if (i != j) {
cout << a[i];
}
}
a[a.size()] = NULL;
}
int main() {
cin >> a;
for (int i = 0; i <= a.size() - 1; i++) {
if (a[i] == '0') {
j = i;
break;
}
}
shift(j);
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
long long int x = 0, y = 0;
for (int i = 0; i < n / 2; ++i) x += a[i];
for (int i = n / 2; i < n; ++i) y += a[i];
long long int res;
res = x * x + y * y;
cout << res;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
for (int j = 0; j < i / 2; j++) {
swap(s[j], s[i - 1 - j]);
}
}
}
cout << s << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const int oo = INT_MAX;
const long long int OO = (1LL << 62);
using namespace std;
namespace {
template <typename Iter>
std::ostream& _out(std::ostream& s, Iter b, Iter e) {
s << "[";
for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it;
s << "]";
return s;
}
template <typename A, typename B>
std::ostream& operator<<(std::ostream& s, const std::pair<A, B>& p) {
return s << "(" << p.first << "," << p.second << ")";
}
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::vector<T>& c) {
return _out(s, begin(c), end(c));
}
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::set<T>& c) {
return _out(s, begin(c), end(c));
}
template <typename A, typename B>
std::ostream& operator<<(std::ostream& s, const std::map<A, B>& c) {
return _out(s, begin(c), end(c));
}
template <typename T>
void _dump(const char* s, T&& head) {
std::cerr << s << "=" << head << std::endl;
}
template <typename T, typename... Args>
void _dump(const char* s, T&& head, Args&&... tail) {
int c = 0;
while (*s != ',' || c != 0) {
if (*s == '(' || *s == '[' || *s == '{') c++;
if (*s == ')' || *s == ']' || *s == '}') c--;
std::cerr << *s++;
}
std::cerr << "=" << head << ", ";
_dump(s + 1, tail...);
}
void RI() {}
template <typename... T>
void RI(int& head, T&... tail) {
scanf("%d", &head);
RI(tail...);
}
} // namespace
vector<pair<int, bool>> adj[100005];
vector<int> visited;
deque<bool> flipped_vertices[2];
vector<int> flip_list[2];
int N, M, ATTEMPT;
bool C;
bool dfs(int vertex, bool flipped) {
if (flipped) {
flipped_vertices[ATTEMPT][vertex] = true;
flip_list[ATTEMPT].push_back(vertex);
}
visited[vertex] = ATTEMPT + 1;
bool ret = true;
for (const auto& p : adj[vertex]) {
auto to_flip = flipped ^ p.second ^ C;
if (visited[p.first] == ATTEMPT + 1) {
if (to_flip ^ flipped_vertices[ATTEMPT][p.first]) {
ret = false;
continue;
}
} else {
ret &= dfs(p.first, to_flip);
}
}
return ret;
}
bool try_solve(vector<int>* output) {
visited.assign(N + 2, 0);
flipped_vertices[0].assign(N + 2, false);
flipped_vertices[1].assign(N + 2, false);
for (int i = 1; i <= N; ++i) {
if (!visited[i]) {
flip_list[0].clear();
flip_list[1].clear();
vector<int>* mn = nullptr;
ATTEMPT = 0;
if (dfs(i, false)) {
mn = &flip_list[0];
}
ATTEMPT = 1;
if (dfs(i, true) and
(mn == nullptr or mn->size() > flip_list[1].size())) {
mn = &flip_list[1];
}
if (mn == nullptr)
return false;
else
copy(mn->begin(), mn->end(), back_inserter(*output));
}
}
return true;
}
int main() {
std::ios_base::sync_with_stdio(false);
cin >> N >> M;
for (int i = 1; i <= M; ++i) {
int x, y;
bool c;
string str;
cin >> x >> y >> str;
c = (str[0] == 'R');
adj[x].emplace_back(y, c);
adj[y].emplace_back(x, c);
}
C = 0;
vector<int> x;
bool xb = try_solve(&x);
C = 1;
vector<int> y;
bool yb = try_solve(&y);
if (!xb and !yb) {
cout << "-1"
<< "\n";
return 0;
}
vector<int>* ans = nullptr;
if (xb) ans = &x;
if (yb and (ans == nullptr or ans->size() > y.size())) ans = &y;
cout << ans->size() << "\n";
for (int a : *ans) {
cout << a << " ";
}
cout << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int b[N];
unordered_set<int> s;
bool gen(int n, int mx) {
s.clear();
for (int i = 1; i <= n; i++) {
b[i] = a[i];
while (b[i] > mx || (b[i] >= 1 && s.find(b[i]) != s.end())) b[i] >>= 1;
if (b[i] == 0) return false;
s.insert(b[i]);
}
return true;
}
int main() {
int n, i;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int l = 1, r = 1000000000;
while (l < r) {
int m = (l + r) / 2;
if (gen(n, m))
r = m;
else
l = m + 1;
}
gen(n, l);
for (int i = 1; i <= n; i++) printf("%d ", b[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 55, inf = 0x3f3f3f3f;
int n, x, y, z, ne[N * 2], tot, fi[N], zz[N * 2], sl[N * 2], st, m,
f[N][N][N][N];
int size[N], fa[N], d[N], a[N][N], Ans = inf;
void jb(int x, int y, int z) {
ne[++tot] = fi[x];
fi[x] = tot;
zz[tot] = y;
sl[tot] = z;
}
int dp(int x, int y, int z, int w) {
if (z == 0 && w == 0) return 0;
if (f[x][y][z][w]) return f[x][y][z][w];
if (d[x] == 1) {
if (z == 0) return 0;
return f[x][y][z][w] = dp(y, x, 0, z) + a[x][y];
}
int g[N];
for (int i = 1; i <= w; i++) g[i] = -inf;
g[0] = inf;
for (int i = fi[x]; i; i = ne[i])
if (zz[i] != y)
for (int j = w; j; j--)
for (int k = 1; k <= j; k++)
g[j] = max(g[j], min(g[j - k], dp(zz[i], x, z + w - k, k) + sl[i]));
return f[x][y][z][w] = g[w];
}
void dfs(int x) {
for (int i = fi[x]; i; i = ne[i])
if (zz[i] != fa[x]) {
fa[zz[i]] = x, dfs(zz[i]);
size[x] += size[zz[i]];
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
jb(x, y, z);
jb(y, x, z);
d[x]++, d[y]++;
a[x][y] = a[y][x] = z;
}
scanf("%d%d", &st, &m);
for (int i = 1; i <= m; i++) scanf("%d", &x), size[x]++;
dfs(1);
for (int i = fi[st]; i; i = ne[i])
if (zz[i] == fa[st])
Ans = min(Ans, dp(zz[i], st, size[st], m - size[st]) + sl[i]);
else
Ans = min(Ans, dp(zz[i], st, m - size[zz[i]], size[zz[i]]) + sl[i]);
printf("%d\n", Ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int modul = 1000000007;
int c[1010][1010];
inline void norm(int &a) {
if (a >= modul) a -= modul;
}
int t[110][1010];
int x[110];
int y[110];
int main() {
for (int i = 0; i < 1010; i++)
for (int j = 0; j < 1010; j++) {
if (j == 0)
c[i][j] = 1;
else if (i == 0)
c[i][j] = 0;
else {
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
norm(c[i][j]);
}
}
int n;
scanf("%d", &n);
int sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
sum += x[i];
}
for (int i = 0; i < n; i++) scanf("%d", &y[i]);
for (int i = 0; i <= sum; i++) t[0][i] = c[x[0]][i];
int l, r;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j <= sum; j++) {
l = j + x[i + 1] - y[i + 1];
l = max(l, 0);
r = j + x[i + 1];
r = min(r, sum);
for (int k = l; k <= r; k++) {
t[i + 1][k] += (t[i][j] * 1LL * c[r][k]) % modul;
norm(t[i + 1][k]);
}
}
int ans = t[n - 1][0];
for (int i = 0; i < n; i++) {
ans = (ans * 1LL * c[sum][x[i]]) % modul;
sum -= x[i];
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
while (~scanf("%d", &n)) {
if (n == 1)
printf("-1\n");
else
printf("%d %d %d\n", n, n + 1, n * (n + 1));
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
map<string, int> a;
for (int i = 1; i <= 60; i++) {
a[s]++;
s += s[0];
s.erase(s.begin());
}
cout << a.size();
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool Up(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool Down(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
inline int getus() {
int tmp, c;
while (tmp = fgetc(stdin), tmp < '0' || tmp > '9')
;
tmp -= '0';
while (c = fgetc(stdin), '0' <= c && c <= '9') tmp = tmp * 10 + c - '0';
return tmp;
}
inline int getint() {
int tmp, c, flag;
while (flag = fgetc(stdin), flag != '-' && (flag < '0' || flag > '9'))
;
if (flag == '-')
tmp = 0;
else
tmp = flag - '0';
while (c = fgetc(stdin), '0' <= c && c <= '9') tmp = tmp * 10 + c - '0';
return flag == '-' ? -tmp : tmp;
}
int N, M, k = 0, deg[100022], ans[100022];
set<int> fir[100022], node[2];
void Select() {
if (node[0].size() % 3 == 0) return;
int x, y, r[3], Rcnt = 0;
for (__typeof(node[0].begin()) i = node[0].begin(); i != node[0].end(); ++i)
if (deg[x = *i] + 2 <= int(node[1].size())) {
for (__typeof(node[1].begin()) j = node[1].begin(); j != node[1].end();
++j)
if (!fir[x].count(y = *j)) {
r[++Rcnt] = y;
if (Rcnt == 2) break;
}
assert(Rcnt == 2);
node[1].erase(r[1]);
node[1].erase(r[2]);
node[0].erase(x);
for (__typeof(fir[x].begin()) j = fir[x].begin(); j != fir[x].end(); ++j)
--deg[*j];
for (__typeof(fir[r[1]].begin()) j = fir[r[1]].begin();
j != fir[r[1]].end(); ++j)
--deg[*j];
for (__typeof(fir[r[2]].begin()) j = fir[r[2]].begin();
j != fir[r[2]].end(); ++j)
--deg[*j];
ans[x] = ans[r[1]] = ans[r[2]] = ++k;
return;
}
}
void Print() {
if (int(node[0].size()) % 3 || int(node[1].size()) % 3) {
puts("NO");
return;
}
puts("YES");
for (set<int>::iterator i = node[0].begin(); i != node[0].end(); ++i) {
ans[*i] = ++k;
ans[*++i] = k;
ans[*++i] = k;
}
for (set<int>::iterator i = node[1].begin(); i != node[1].end(); ++i) {
ans[*i] = ++k;
ans[*++i] = k;
ans[*++i] = k;
}
assert(k * 3 == N);
for (int i = (1); i <= (N); ++i) {
printf("%d", ans[i]);
if (i != N) putchar(' ');
}
putchar('\n');
}
void Work() {
if (node[0].size() % 3 == 2) node[0].swap(node[1]);
Select();
node[0].swap(node[1]);
Select();
Select();
Print();
}
void Init() {
static int state[100022];
int x, y;
memset(deg, 0, sizeof(deg));
memset(state, -1, sizeof(state));
N = getus();
M = getus();
for (int i = (1); i <= (M); ++i) {
x = getus();
y = getus();
fir[x].insert(y);
fir[y].insert(x);
++deg[x];
++deg[y];
}
queue<int> q;
for (int i = (1); i <= (N); ++i)
if (state[i] == -1)
for (state[i] = 0, q.push(i); !q.empty();) {
x = q.front();
q.pop();
node[state[x]].insert(x);
for (__typeof(fir[x].begin()) e = fir[x].begin(); e != fir[x].end();
++e)
if (state[y = *e] == -1) {
state[y] = state[x] ^ 1;
q.push(y);
}
}
}
int main() {
Init();
Work();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(0);
int a1, a2, a3, b1, b2, b3, atotal, btotal, t1, t2, n;
cin >> a1 >> a2 >> a3;
cin >> b1 >> b2 >> b3;
cin >> n;
atotal = a1 + a2 + a3;
btotal = b1 + b2 + b3;
t1 = atotal / 5;
if ((atotal % 5) > 0) t1 += 1;
n = n - t1;
t2 = btotal / 10;
if (btotal % 10 > 0) t2 += 1;
n = n - t2;
if (n >= 0)
cout << "YES";
else
cout << "NO";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, p, su;
long long a[100005], b[100005];
long double ex[100005];
bool check(long double mid) {
long double need = 0;
for (int i = 0; i < n; i++) {
long double ne = max(0.0L, a[i] * (mid - ex[i]));
need += ne;
}
return (mid * p >= need);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> p;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
su += a[i];
}
if (p >= su) {
cout << -1;
} else {
for (int i = 0; i < n; i++) {
ex[i] = (b[i] * 1.0L) / a[i];
}
long double l = 0;
long double r = 1e18;
for (int i = 0; i < 120; i++) {
long double mid = (l + r) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
if (r > 1e18 - 100) {
cout << -1;
} else {
cout << setprecision(9) << l;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
set<int> se;
vector<int> V[300003];
int main() {
int n, q, tmp = 0;
scanf("%d%d", &n, &q);
for (int i = 0; i < q; i++) {
int a, b;
scanf("%d%d", &a, &b);
if (a == 1) {
tmp++;
V[b].push_back(tmp);
se.insert(tmp);
}
if (a == 2) {
while (!V[b].empty()) {
int x = V[b][V[b].size() - 1];
V[b].pop_back();
if (se.find(x) != se.end()) se.erase(se.find(x));
}
}
if (a == 3)
while (!se.empty() && *se.begin() <= b) se.erase(se.begin());
printf("%d\n", (int)se.size());
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << '=' << h << ',';
_dbg(sdbg + 1, a...);
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class Iterable,
class = typename enable_if<!is_same<string, Iterable>::value>::type>
auto operator<<(ostream &os, Iterable v) -> decltype(os << *begin(v)) {
os << "[";
for (auto vv : v) os << vv << ", ";
return os << "]";
}
const int inf = 0x3f3f3f3f;
const long long infll = 0x3f3f3f3f3f3f3f3fll;
template <class T>
int sign(T x) {
return (x > 0) - (x < 0);
}
template <class T>
T abs(const T &x) {
return (x < T(0)) ? -x : x;
}
vector<int> graph[112345];
int c[112345][4];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int n, a, b;
cin >> n;
for (int k = 1; k <= 3; k++)
for (int i = 1; i <= n; i++) cin >> c[i][k];
for (int i = 0; i + 1 < n; i++) {
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
int start;
for (int i = 1; i <= n; i++) {
if (graph[i].size() > 2) {
cout << -1 << endl;
return 0;
}
if (graph[i].size() == 1) start = i;
}
vector<int> pi = {1, 2, 3};
long long ans = infll;
vector<int> best_color;
vector<int> color(n + 1);
do {
long long tmp = 0;
int cur = start;
int prev = start;
for (int i = 0; i < n; i++) {
color[cur] = pi[i % 3];
tmp += c[cur][pi[i % 3]];
for (int j = 0; j < graph[cur].size(); j++)
if (graph[cur][j] != prev) {
prev = cur;
cur = graph[cur][j];
break;
}
}
if (tmp < ans) {
ans = min(ans, tmp);
best_color = color;
}
} while (next_permutation(pi.begin(), pi.end()));
cout << ans << endl;
for (int i = 1; i <= n; i++) cout << best_color[i] << " ";
cout << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a;
int t1 = 0;
int t2 = 0;
long long int s1 = 0;
long long int s2 = 0;
int d1[200010];
int d2[200010];
for (int i = 0; i < n; i++) {
cin >> a;
if (a > 0) {
d1[t1] = a;
s1 += a;
t1++;
} else {
d2[t2] = -a;
s2 += -a;
t2++;
}
}
if (s1 > s2) {
cout << "first";
}
if (s1 < s2) {
cout << "second";
}
if (s1 == s2) {
int en = min(t1, t2);
int flag = 0;
for (int i = 0; i < en; i++) {
if (d1[i] != d2[i]) {
if (d1[i] > d2[i]) {
cout << "first";
} else {
cout << "second";
}
flag = 1;
break;
}
}
if (!flag) {
if (t1 != t2) {
if (t1 > t2)
cout << "first";
else
cout << "second";
} else if (a > 0)
cout << "first";
else
cout << "second";
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int T, n, m;
int a[N];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
sort(a + 1, a + 1 + n);
for (int i = n; i >= 1; --i) printf("%d ", a[i]);
puts("");
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int N;
vector<pair<int, char>> E[402020];
int Q;
vector<int> ev[404040];
vector<string> S;
string RS;
int ind[404040];
signed long long ret[404040];
struct SuffixArray {
int N;
vector<int> rank, lcp, sa, rsa;
string S;
void build(string S) {
this->S = S;
int i, h = 0;
vector<int> tmp;
N = S.size();
rank.resize(N + 1);
sa.resize(N + 1);
tmp.resize(N + 1);
for (i = 0; i < (N + 1); i++) sa[i] = i, rank[i] = i == N ? -1 : S[i];
for (int k = 1; k <= N; k <<= 1) {
auto pred2 = [k, this](int& a, int& b) -> bool {
return (((a + k <= N) ? rank[a + k] : -1) <
((b + k <= N) ? rank[b + k] : -1));
};
auto pred = [pred2, k, this](int& a, int& b) -> bool {
return (rank[a] != rank[b]) ? (rank[a] < rank[b]) : pred2(a, b);
};
int x = 0;
if (k != 1)
for (i = 1; i < N + 1; i++)
if (rank[sa[i]] != rank[sa[x]])
sort(sa.begin() + x, sa.begin() + i, pred2), x = i;
sort(sa.begin() + x, sa.end(), pred);
for (i = 0; i < (N + 1); i++)
tmp[sa[i]] = (i == 0) ? 0 : tmp[sa[i - 1]] + pred(sa[i - 1], sa[i]);
swap(rank, tmp);
}
lcp.resize(N + 1);
rsa.resize(N + 1);
for (i = 0; i < (N + 1); i++) rsa[sa[i]] = i;
for (i = 0; i < (N); i++) {
int j = sa[rsa[i] - 1];
for (h = max(h - 1, 0); i + h < N && j + h < N; h++)
if (S[j + h] != S[i + h]) break;
lcp[rsa[i] - 1] = h;
}
}
};
SuffixArray sa;
const int NUMC = 26;
class Trie {
public:
vector<vector<int>> V;
int find(string s) {
int cur = 0;
for (__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
if ((cur = V[cur][*it + 1]) == 0) return -1;
return cur;
}
void create(vector<string> S) {
V.clear();
V.push_back(vector<int>(NUMC + 1));
sort(S.begin(), S.end());
for (__typeof(S.begin()) it = S.begin(); it != S.end(); it++) {
int cur = 0;
for (__typeof((*it).begin()) c = (*it).begin(); c != (*it).end(); c++) {
if (V[cur][*c + 1] == 0)
V.push_back(vector<int>(NUMC + 1)), V[cur][*c + 1] = V.size() - 1;
cur = V[cur][*c + 1];
}
}
}
};
template <class V, int NV>
class SegTree_1 {
public:
vector<V> val;
static V const def = 1 << 20;
V comp(V l, V r) { return min(l, r); };
SegTree_1() { val = vector<V>(NV * 2, 0); };
V getval(int x, int y, int l = 0, int r = NV, int k = 1) {
if (r <= x || y <= l) return def;
if (x <= l && r <= y) return val[k];
return comp(getval(x, y, l, (l + r) / 2, k * 2),
getval(x, y, (l + r) / 2, r, k * 2 + 1));
}
void update(int entry, V v) {
entry += NV;
val[entry] = v;
while (entry > 1)
entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
}
};
SegTree_1<int, 1 << 20> st;
template <class V, int ME>
class BIT {
public:
V bit[1 << ME];
V operator()(int e) {
if (e < 0) return 0;
V s = 0;
e++;
while (e) s += bit[e - 1], e -= e & -e;
return s;
}
void add(int e, V v) {
e++;
while (e <= 1 << ME) bit[e - 1] += v, e += e & -e;
}
};
BIT<signed long long, 20> bt;
class ACmatch_enum {
public:
Trie t;
vector<vector<int>> acc;
int ma;
void create(vector<string> S) {
int i, j;
ma = S.size();
t.create(S);
acc.clear();
acc.resize(t.V.size());
for (i = 0; i < (S.size()); i++) {
int st = ind[i];
int cur = 0;
for (j = 0; j < (S[i].size()); j++) {
cur = t.V[cur][S[i][j] + 1];
int tar = sa.rsa[ind[i] + S[i].size() - 1 - j];
if (acc[cur].empty()) acc[cur].push_back(tar);
}
}
queue<int> Q;
for (i = 0; i < (NUMC); i++)
if (t.V[0][i + 1]) t.V[t.V[0][i + 1]][0] = 0, Q.push(t.V[0][i + 1]);
while (!Q.empty()) {
int k = Q.front();
Q.pop();
for (i = 0; i < (NUMC); i++)
if (t.V[k][i + 1]) {
Q.push(t.V[k][i + 1]);
int pre = t.V[k][0];
while (pre && t.V[pre][i + 1] == 0) pre = t.V[pre][0];
t.V[t.V[k][i + 1]][0] = t.V[pre][i + 1];
}
}
}
void dfs(int cur, int pos) {
int i;
for (auto& e : ev[cur]) {
int L = sa.rsa[ind[e]], R = L;
for (int j = 18; j >= 0; j--)
if (R + (1 << j) <= RS.size() &&
st.getval(L, R + (1 << j)) >= S[e].size())
R += 1 << j;
for (int j = 18; j >= 0; j--)
if (L - (1 << j) >= 0 && st.getval(L - (1 << j), R) >= S[e].size())
L -= 1 << j;
ret[e] = bt(R) - bt(L - 1);
}
for (auto& e : E[cur]) {
int prev = pos;
while (pos && t.V[pos][e.second + 1] == 0) pos = t.V[pos][0];
pos = t.V[pos][e.second + 1];
for (auto& r : acc[pos]) {
bt.add(r, 1);
}
dfs(e.first, pos);
for (auto& r : acc[pos]) bt.add(r, -1);
pos = prev;
}
}
};
ACmatch_enum ac;
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N;
for (i = 0; i < (N); i++) {
cin >> j;
x = 0;
if (j == 2) cin >> x;
cin >> s;
E[x].push_back({i + 1, s[0] - 'a'});
}
cin >> Q;
map<string, int> memo;
for (i = 0; i < (Q); i++) {
cin >> x >> s;
ev[x].push_back(i);
for (auto& c : s) c -= 'a';
S.push_back(s);
for (auto& c : s) c += 'a';
reverse((s.begin()), (s.end()));
if (memo.count(s)) {
ind[i] = memo[s];
} else {
ind[i] = RS.size();
memo[s] = ind[i];
RS += s + "#";
}
}
sa.build(RS);
for (i = 0; i < (RS.size()); i++) {
st.update(i, sa.lcp[i]);
}
ac.create(S);
ac.dfs(0, 0);
for (i = 0; i < (Q); i++) cout << ret[i] << endl;
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int a[200001], i, j, r, v, sum, sol;
int main() {
cin >> r >> v;
sum = 0;
a[0] = 1;
for (i = 1; sum + i <= r + v; i++) {
sum += i;
for (j = min(sum, r); j >= i; j--) {
a[j] = (a[j] + a[j - i]) % 1000000007;
}
}
for (i = max(sum - v, 0); i <= min(sum, r); i++) {
sol = (sol + a[i]) % 1000000007;
}
cout << sol;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long l, p, q;
cin >> l >> p >> q;
float t, dis;
t = float(l) / float(p + q);
dis = p * t;
cout << dis;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long n, m;
cin >> n >> m;
vector<vector<long> > edges(n + 1);
vector<vector<bool> > adj(n + 1, vector<bool>(n + 1, 0));
for (long p = 0; p < m; p++) {
long a, b;
cin >> a >> b;
edges[a].push_back(b);
adj[a][b] = 1;
}
long long output = 0;
for (long source = 1; source <= n; source++) {
for (long target = 1; target <= n; target++) {
if (source == target) {
continue;
}
long long common = 0;
for (long p = 0; p < edges[source].size(); p++) {
if (adj[edges[source][p]][target] > 0) {
++common;
}
}
output += common * (common - 1) / 2;
}
}
cout << output << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long gi() {
long long ret = 0;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
char c = ch == '-' ? getchar() : ch;
while (c >= '0' && c <= '9') ret = ret * 10 + c - '0', c = getchar();
return ch == '-' ? -ret : ret;
}
const int N = 3000000 + 10;
int n, k, a[N], s[N], l[N], r[N], stk[N], top;
struct qry {
int v, f, id;
};
vector<qry> v[N];
int main() {
n = gi();
k = gi();
for (int i = 1; i <= n; i += 1) a[i] = gi(), s[i] = (s[i - 1] + a[i]) % k;
for (int i = 1; i <= n; i += 1) {
while (top && a[i] > a[stk[top]]) --top;
if (top)
l[i] = stk[top] + 1;
else
l[i] = 1;
stk[++top] = i;
}
top = 0;
for (int i = n; i >= 1; i -= 1) {
while (top && a[i] >= a[stk[top]]) --top;
if (top)
r[i] = stk[top] - 1;
else
r[i] = n;
stk[++top] = i;
if (i - l[i] < r[i] - i) {
for (int j = l[i]; j <= i; j += 1) {
int L = max(i, j + 1), R = r[i];
if (L > R) continue;
if (R >= 0) v[R].push_back((qry){(a[i] + s[j - 1]) % k, 1, i});
if (L - 1 >= 0) v[L - 1].push_back((qry){(a[i] + s[j - 1]) % k, -1, i});
}
} else {
for (int j = i; j <= r[i]; j += 1) {
int L = l[i] - 1, R = min(i, j - 1) - 1;
if (L > R) continue;
if (R >= 0) v[R].push_back((qry){((s[j] - a[i]) % k + k) % k, 1, i});
if (L - 1 >= 0)
v[L - 1].push_back((qry){((s[j] - a[i]) % k + k) % k, -1, i});
}
}
}
memset(stk, 0, sizeof(stk));
long long ans = 0;
for (int i = 0; i <= n; i += 1) {
stk[s[i]] += 1;
for (int j = 0; j < v[i].size(); j += 1) {
ans += stk[v[i][j].v] * v[i][j].f;
}
}
cout << ans;
return 0;
}
| 10 |
#include <bits/stdc++.h>
int main() {
long long n, m, temp, i, j, r = 0, c = 0;
long long rslt;
long long tasks;
scanf("%lli%lli", &n, &m);
for (i = 0; i < m; i++) {
scanf("%lli", &tasks);
if (tasks < r) c++;
r = tasks;
}
rslt = c * n + tasks - 1;
printf("%lli", rslt);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
;
long long n, t;
string s;
cin >> s;
char c[5];
c[1] = s[1];
s[1] = s[2];
s[2] = s[4];
s[4] = c[1];
n = stoll(s, nullptr, 10);
t = n;
for (int i = 0; i < 4; i++) {
n *= t;
n %= 100000;
}
printf("%05I64d", n);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0.0);
const int maxn = 4e5 + 5;
const int maxm = 4e5 + 10;
const int mod = 1e9 + 7;
int pos[4][maxn];
struct node {
int x, y;
int id;
} a[maxn];
int ans[maxn];
int res[10];
bool cmp1(node A, node B) {
if (A.x == B.x) return A.y < B.y;
return A.x < B.x;
}
bool cmp2(node A, node B) {
if (A.x == B.x) return A.y > B.y;
return A.x > B.x;
}
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= k; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
a[i].id = i;
}
sort(a + 1, a + 1 + k, cmp1);
for (int i = 1; i <= k; i++) {
int cnt = 0;
cnt += pos[0][a[i].x];
cnt += pos[1][a[i].y];
cnt += pos[2][a[i].x + a[i].y];
cnt += pos[3][a[i].x - a[i].y + 200000];
ans[a[i].id] += cnt;
pos[0][a[i].x] = 1;
pos[1][a[i].y] = 1;
pos[2][a[i].x + a[i].y] = 1;
pos[3][a[i].x - a[i].y + 200000] = 1;
}
memset(pos, 0, sizeof(pos));
sort(a + 1, a + 1 + k, cmp2);
for (int i = 1; i <= k; i++) {
int cnt = 0;
cnt += pos[0][a[i].x];
cnt += pos[1][a[i].y];
cnt += pos[2][a[i].x + a[i].y];
cnt += pos[3][a[i].x - a[i].y + 200000];
ans[a[i].id] += cnt;
pos[0][a[i].x] = 1;
pos[1][a[i].y] = 1;
pos[2][a[i].x + a[i].y] = 1;
pos[3][a[i].x - a[i].y + 200000] = 1;
}
for (int i = 1; i <= k; i++) res[ans[a[i].id]]++;
for (int i = 0; i <= 8; i++) printf("%d ", res[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
string direc = "URDL";
const long long MOD2 = (long long)998244353 * (long long)998244353;
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int mp[2035][2035], dg[2035], dgc[2035];
char s[2035];
bool ck() {
int cnt = 0, ps = 0;
for (int i = 0; i <= n; i++) {
for (int(j) = 0; (j) < (int)(dgc[i]); (j)++) {
cnt++;
if (cnt == n) return 1;
ps += i;
if (ps == cnt * (cnt - 1) / 2) {
return 0;
}
}
}
return 1;
}
void changeD(int x, int v) {
dgc[dg[x]]--;
dg[x] += v;
dgc[dg[x]]++;
}
void rev(int x) {
for (int(i) = 1; (i) <= (int)(n); (i)++)
if (x != i) {
if (mp[i][x] == 1) {
changeD(i, -1);
changeD(x, 1);
} else {
changeD(i, 1);
changeD(x, -1);
}
mp[i][x] ^= 1;
mp[x][i] ^= 1;
}
}
void fmain(int tid) {
scanf("%d", &n);
for (int(i) = 1; (i) <= (int)(n); (i)++) {
scanf("%s", s + 1);
for (int(j) = 1; (j) <= (int)(n); (j)++) mp[i][j] = s[j] - '0';
}
for (int(i) = 1; (i) <= (int)(n); (i)++)
for (int(j) = 1; (j) <= (int)(n); (j)++)
if (mp[i][j] == 1) {
dg[i]++;
}
for (int(i) = 1; (i) <= (int)(n); (i)++) dgc[dg[i]]++;
if (ck()) {
puts("0 1");
return;
}
if (n > 6) {
int cc = 0;
for (int(i) = 1; (i) <= (int)(n); (i)++) {
rev(i);
if (ck()) cc++;
rev(i);
}
printf("1 %d\n", cc);
return;
}
int cc = (1 << 30), ans = 0;
for (int i = 1; i < (1 << n); i++) {
int tmp = 0;
for (int(j) = 0; (j) < (int)(n); (j)++)
if (i & (1 << j)) {
tmp++;
rev(j + 1);
}
if (ck()) {
if (tmp < cc) {
cc = tmp;
ans = 1;
} else if (tmp == cc)
ans++;
}
for (int(j) = 0; (j) < (int)(n); (j)++)
if (i & (1 << j)) {
rev(j + 1);
}
}
if (cc == (1 << 30)) et();
for (int(i) = 1; (i) <= (int)(cc); (i)++) ans *= i;
printf("%d %d\n", cc, ans);
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf_ll = 1e18;
const int N = 2e5 + 5;
long double t[N], p[N], q[N], s[N], a[N], b[N];
long double f(int i, int j) {
return s[j] - s[i - 1] - (q[j] - q[i - 1]) * p[i - 1];
}
void dfs(int l, int r, int x, int y) {
if (l > r) return;
int m = (l + r) / 2, z = 0;
b[m] = 1e18;
for (int i = x; i <= min(m, y); i++) {
long double c = a[i - 1] + f(i, m);
if (c < b[m]) b[m] = c, z = i;
}
dfs(l, m - 1, x, z), dfs(m + 1, r, z, y);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> t[i];
p[i] = t[i] + p[i - 1];
q[i] = 1 / t[i] + q[i - 1];
s[i] = s[i - 1] + p[i] / t[i];
}
fill(a + 1, a + n + 1, 1e18);
for (int j = 0; j < k; j++) dfs(1, n, 1, n), copy(b, b + n + 1, a);
cout << setprecision(25) << a[n] << "\n";
}
| 8 |
#include <bits/stdc++.h>
const int MAXN = 15010;
const int MAXK = 1010;
const int MAXQ = 30010;
const int modP = 1e7 + 19;
const int modQ = 1e9 + 7;
using namespace std;
int N, K, Q, cnt;
int W[MAXN], V[MAXN];
vector<int> tree[MAXQ * 4];
map<int, int> mp;
vector<int> queries;
long long pot[MAXK], ans[MAXQ];
long long dp[MAXN][MAXK];
int m(int l, int r) { return (l + r) >> 1; }
void upd(int pos, int l, int r, int beg, int en, int x) {
if (l > en || r < beg) return;
if (l >= beg && r <= en) return (void)(tree[pos].push_back(x));
upd((pos << 1), l, m(l, r), beg, en, x);
upd((pos << 1) | 1, m(l, r) + 1, r, beg, en, x);
}
void run(int pos, int l, int r, int last) {
("Run %d %d %d %d\n", pos, l, r, last);
for (auto idx : tree[pos]) {
("With item %d %d %d\n", idx, V[idx], W[idx]);
last++;
int v = V[idx], w = W[idx];
for (int i = 1; i <= K; i++) {
dp[last][i] = dp[last - 1][i];
if (i - w >= 0)
dp[last][i] = max(dp[last - 1][i], dp[last - 1][i - w] + v);
}
}
for (int i = 1; i <= K; i++) ("%lld ", dp[last][i]);
("\n\n");
if (l == r) {
long long mx = -1;
for (int i = 1; i <= K; i++) {
mx = max(mx, dp[last][i]);
ans[l] = (ans[l] + (pot[i - 1] * mx) % modQ) % modQ;
}
return;
}
run(pos << 1, l, m(l, r), last);
run((pos << 1) | 1, m(l, r) + 1, r, last);
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; i++) {
scanf("%d%d", &V[i], &W[i]);
mp.insert(make_pair(i, 1));
}
scanf("%d", &Q);
cnt = 1 + N;
for (int i = 1; i <= Q; i++) {
int type, x;
scanf("%d", &type);
if (type == 1) {
scanf("%d%d", &V[cnt], &W[cnt]);
mp.insert(make_pair(cnt, i));
cnt++;
}
if (type == 2) {
scanf("%d", &x);
upd(1, 1, Q, mp[x], i, x);
mp.erase(mp.find(x));
} else if (type == 3)
queries.push_back(i);
}
for (auto p : mp) upd(1, 1, Q, p.second, Q, p.first);
pot[0] = 1LL;
for (int i = 1; i <= K; i++) pot[i] = (pot[i - 1] * modP) % modQ;
run(1, 1, Q, 0);
for (int i : queries) printf("%lld\n", ans[i]);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int mod2 = 998244353;
const double eps = 1e-9;
const double pi = acos(-1.0);
inline int rint() {
int x;
scanf("%d", &x);
return x;
}
inline long long rll() {
long long x;
scanf("%lld", &x);
return x;
}
inline long long ri64() {
long long x;
scanf("%I64d", &x);
return x;
}
void rvi(vector<int> &a, int n) {
for (int i = 0; i < n; i++) {
a.push_back(rint());
}
}
void rvll(vector<long long> &a, int n) {
for (int i = 0; i < n; i++) {
a.push_back(rll());
}
}
void showvi(vector<int> &a) {
for (int i = 0; i < a.size(); i++) {
printf("%d%c", a[i], (i + 1 == a.size() ? '\n' : ' '));
}
}
void showvll(vector<long long> &a) {
for (int i = 0; i < a.size(); i++) {
printf("%lld%c", a[i], (i + 1 == a.size() ? '\n' : ' '));
}
}
void showviln(vector<int> &a) {
for (int i = 0; i < a.size(); i++) {
printf("%d%c", a[i], '\n');
}
}
void showvllln(vector<long long> &a) {
for (int i = 0; i < a.size(); i++) {
printf("%lld%c", a[i], '\n');
}
}
void showi(int x) { printf("%d", x); }
void cfinit() {
ios::sync_with_stdio(false);
cin.tie(0);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long myRand(long long B) { return (unsigned long long)rng() % B; }
int myRand(int l, int r) { return myRand(r - l + 1) + l; }
using namespace std;
int a[100011];
int pos = 0;
int ans[100011][3];
void push(int i, int j, int k) {
ans[pos][0] = i + 1;
ans[pos][1] = j + 1;
ans[pos][2] = k + 1;
pos++;
}
int main() {
cfinit();
int n;
cin >> n;
for (int i = 0; i < (n); i++) cin >> a[i];
long long sum = 0;
for (int i = 0; i < (n); i++) sum ^= a[i];
if (n % 2 == 0 && sum != 0) {
cout << "NO" << '\n';
return 0;
}
cout << "YES" << '\n';
for (int i = 2; i < n; i += 2) push(i - 2, i - 1, i);
for (int i = 1; i < n - 3; i += 2) push(i - 1, i, n - 1);
cout << pos << '\n';
for (int i = 0; i < (pos); i++)
cout << ans[i][0] << " " << ans[i][1] << " " << ans[i][2] << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[105], b[105];
int main() {
scanf("%d%d", &n, &m);
if (n & 1) {
if (n >= 2) {
for (int i = 1; i < n - 1; ++i) a[i] = 1;
a[n - 1] = 2;
}
a[n] = n / 2 + 1;
} else {
if (n > 2) {
for (int i = 1; i <= n - 1; ++i) a[i] = 1;
a[n] = n / 2 - 1;
} else {
a[1] = 3;
a[2] = 4;
}
}
if (m & 1) {
if (m >= 2) {
for (int i = 1; i < m - 1; ++i) b[i] = 1;
b[m - 1] = 2;
}
b[m] = m / 2 + 1;
} else {
if (m > 2) {
for (int i = 1; i <= m - 1; ++i) b[i] = 1;
b[m] = m / 2 - 1;
} else {
b[1] = 3;
b[2] = 4;
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (j != 1) printf(" ");
printf("%d", a[i] * b[j]);
}
printf("\n");
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, r;
cin >> n >> r;
vector<int> w(n);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
int ans = 0;
int last = -1;
while (last < n - 1) {
int pos = -1;
for (int i = n - 1; i > max(-1, last - r + 1); --i) {
if (w[i] == 1 && i - r <= last) {
pos = i;
break;
}
}
if (pos == -1) {
cout << -1 << "\n";
return 0;
}
++ans;
last = pos + r - 1;
}
cout << ans << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, flg = 0, mx = 0;
cin >> n;
vector<int> vv;
map<int, int> mp;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
vv.push_back(a);
if (mx < a) mx = a;
mp[a]++;
if (mp[a] > 2) flg = 1;
}
int f1 = 0, f2 = 0, f3 = 0, f4 = 0;
int i;
for (i = 1; i <= mx; ++i) {
if (mp[i] != 2 && !f1) f1 = i - 1;
if (mp[i] != 2 && i == 1) f4 = 1;
if (!mp[i]) f2 = 1;
if (mp[i] == 2 && f1) f3 = 1;
}
if (flg || f2 || f3 || f4) {
cout << 0 << "\n";
continue;
}
vector<int> vl, vr;
if (f1 == 0) f1 = n / 2;
f2 = 0;
for (int i = 0; i < f1; ++i) {
vl.push_back(vv[i]);
}
sort(vl.begin(), vl.end());
for (int i = 0; i < f1; ++i) {
if (vl[i] != i + 1) {
f2 = 1;
break;
}
}
for (int i = n - 1; i >= f1; --i) {
vr.push_back(vv[i]);
}
sort(vr.begin(), vr.end());
for (int i = 0; i < n - f1; ++i) {
if (vr[i] != i + 1) {
f2 = 1;
break;
}
}
vl.clear();
vr.clear();
f3 = 0;
for (int i = 0; i < n - f1; ++i) {
vl.push_back(vv[i]);
}
sort(vl.begin(), vl.end());
for (int i = 0; i < n - f1; ++i) {
if (vl[i] != i + 1) {
f3 = 1;
break;
}
}
for (int i = n - 1; i >= n - f1; --i) {
vr.push_back(vv[i]);
}
sort(vr.begin(), vr.end());
for (int i = 0; i < f1; ++i) {
if (vr[i] != i + 1) {
f3 = 1;
break;
}
}
if (f1 == n / 2 && f2 == 0 && n % 2 == 0)
printf("1\n%d %d\n", n / 2, n / 2);
else {
int xx = 0;
if (!f2) xx++;
if (!f3) xx++;
cout << xx << endl;
if (!f2) printf("%d %d\n", f1, n - f1);
if (!f3) printf("%d %d\n", n - f1, f1);
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
pair<string, string> arr[n];
for (int i = 0; i < n; i++) {
string a, b;
cin >> a >> b;
arr[i] = make_pair(a, b);
}
vector<int> ord;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
a--;
ord.push_back(a);
}
if (n == 1) {
cout << "YES" << endl;
return 0;
}
bool check = true;
string handle[n];
pair<string, string> now = arr[ord[0]];
pair<string, string> nex = arr[ord[1]];
if (min(now.first, now.second) < min(nex.first, nex.second)) {
handle[ord[0]] = min(now.first, now.second);
handle[ord[1]] = min(nex.first, nex.second);
} else if (min(now.first, now.second) < max(nex.first, nex.second)) {
handle[ord[0]] = min(now.first, now.second);
handle[ord[1]] = max(nex.first, nex.second);
} else {
check = false;
}
for (int i = 2; i < n && check; i++) {
string prev = handle[ord[i - 1]];
pair<string, string> nex = arr[ord[i]];
if (min(nex.first, nex.second) > prev) {
handle[ord[i]] = min(nex.first, nex.second);
} else if (max(nex.first, nex.second) > prev) {
handle[ord[i]] = max(nex.first, nex.second);
} else {
check = false;
}
}
if (check) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline bool check(string a, string b) {
int n = a.size();
if (b.size() != n) return false;
for (int i = 0; i < n; i++) {
char ca, cb;
if (a[i] >= 'A' && a[i] <= 'Z')
ca = a[i] - 'A';
else
ca = a[i] - 'a';
if (b[i] >= 'A' && b[i] <= 'Z')
cb = b[i] - 'A';
else
cb = b[i] - 'a';
if (ca != cb) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(32);
int n;
cin >> n;
vector<string> v(n);
for (auto &x : v) {
cin >> x;
}
string w;
cin >> w;
char fav[2];
cin >> fav[0];
fav[1] = fav[0] - 'a' + 'A';
int m = w.size();
vector<int> bad(m);
for (int i = 0; i < n; i++) {
int k = v[i].size();
for (int j = 0; j <= m - k; j++) {
if (check(w.substr(j, k), v[i])) {
for (int ii = j; ii < j + k; ii++) {
bad[ii] = 1;
}
}
}
}
for (int i = 0; i < m; i++) {
if (bad[i]) {
if (w[i] == fav[0] || w[i] == fav[1]) {
for (int ch = 0; ch < 26; ch++) {
if (ch == fav[0] - 'a') continue;
if (w[i] == fav[0]) {
w[i] = char(ch + 'a');
} else {
w[i] = char(ch + 'A');
}
break;
}
} else if (w[i] >= 'A' && w[i] <= 'Z') {
w[i] = fav[1];
} else {
w[i] = fav[0];
}
}
}
cout << w << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, k, a[100010], id[100010], miu[10000010], mn[10000010], prime[1000010],
p = 0, tr[10000010];
int st[100010], t = 0;
bool isnt_prime[10000010];
vector<int> vec[100010];
void init(int N) {
miu[1] = 1;
for (int i = 2; i <= N; i++) {
if (!isnt_prime[i]) {
prime[++p] = i;
miu[i] = -1, mn[i] = i;
}
for (int j = 1; j <= p && prime[j] * i <= N; j++) {
isnt_prime[i * prime[j]] = true, mn[i * prime[j]] = prime[j];
if (i % prime[j] == 0) {
miu[i * prime[j]] = 0;
break;
}
miu[prime[j] * i] = -miu[i];
}
}
}
void work(int id, int S, int now) {
if (now == 1) return vec[id].push_back(S);
int pr = mn[now];
while (now % pr == 0) now /= pr;
work(id, S, now), work(id, S * pr, now);
}
void add(int x, int c) {
for (int i = vec[x].size() - 1; i >= 0; i--)
tr[vec[x][i]] += c * miu[vec[x][i]];
}
int qry(int x) {
int res = 0;
for (int i = vec[x].size() - 1; i >= 0; i--) res += tr[vec[x][i]];
return res;
}
int main() {
init(1e7);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
work(i, 1, a[i]);
}
for (int i = 1; i <= n; i++) id[i] = i;
while (true) {
random_shuffle(id + 1, id + n + 1);
t = 0;
for (int i = 1; i <= n && t < k; i++)
if (!qry(id[i])) add(st[++t] = id[i], 1);
if (t >= k) {
for (int i = 1; i <= k; i++) printf("%d ", st[i]);
puts("");
return 0;
}
while (t) add(st[t], -1), t--;
add(st[++t] = id[1], 1);
for (int i = 2; i <= n && t < k; i++)
if (qry(id[i])) add(st[++t] = id[i], 1);
if (t >= k) {
for (int i = 1; i <= k; i++) printf("%d ", st[i]);
puts("");
return 0;
}
while (t) add(st[t], -1), t--;
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int best = 0;
string s;
cin.ignore();
cin.clear();
getline(cin, s);
int c = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') c = 0;
if (s[i] >= 'A' && s[i] <= 'Z') c++;
best = max(best, c);
}
cout << best;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {}
long long n, k, a;
long long f(long long x) { return (x + 1) / (a + 1); }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> a;
set<long long> S;
S.emplace(0);
S.emplace(n + 1);
long long m;
cin >> m;
long long have = f(n);
if (false) cerr << ("have") << " is " << (have) << '\n';
for (long long i = 1; i <= m; i++) {
if (false) cerr << ("i") << " is " << (i) << '\n';
long long x;
cin >> x;
auto ptr = S.upper_bound(x);
ptr--;
long long l = (*(ptr));
long long r = (*(S.upper_bound(x)));
if (false)
cerr << "inserting " << x << " is in between " << l << " " << r << '\n';
long long len = r - l - 1;
have -= f(len);
if (false) cerr << "subtracting " << f(len) << '\n';
long long lenl = x - l - 1;
have += f(lenl);
if (false) cerr << "adding " << f(lenl) << '\n';
long long lenr = r - x - 1;
have += f(lenr);
if (false) cerr << "adding " << f(lenr) << '\n';
if (false) cerr << "have is now " << have << '\n';
if (have < k) {
if (false) cerr << ("have") << " is " << (have) << '\n';
cout << i << '\n';
return 0;
}
if (false) cerr << "emplacing " << x << '\n';
S.emplace(x);
if (false) cerr << '\n';
}
cout << -1 << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 601;
string ans;
int L[N], R[N];
int memo[N][N];
int solve(int l, int r) {
if (l > r) return 1;
int &res = memo[l][r];
if (res != -1) return res;
res = 0;
for (int i = ((L[l] + 2) / 2) * 2; i <= min(R[l] + 1, 2 * (r - l + 1));
i += 2) {
int d = (i - 2) / 2;
res |= solve(l + 1, l + d) & solve(l + d + 1, r);
if (res) break;
}
return res;
}
void go(int idx, int l, int r) {
if (l > r) return;
for (int i = ((L[l] + 2) / 2) * 2; i <= min(R[l] + 1, 2 * (r - l + 1));
i += 2) {
int d = (i - 2) / 2;
bool ok = true;
if (l + d >= l + 1 && !memo[l + 1][l + d]) ok = false;
if (r >= l + d + 1 && !memo[l + d + 1][r]) ok = false;
if (ok) {
ans[idx - 1] = '(';
ans[i - 1 + idx - 1] = ')';
go(idx + 1, l + 1, l + d);
go(i + idx, l + d + 1, r);
break;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n + 1; i++) {
scanf("%d%d", &L[i], &R[i]);
}
memset(memo, -1, sizeof(memo));
if (!solve(1, n)) {
printf("IMPOSSIBLE\n");
return 0;
}
for (int i = 0; i < 2 * n; i++) ans += '#';
go(1, 1, n);
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
using pii = pair<int, int>;
using vi = vector<int>;
const int N = 2e5 + 5;
struct Edge {
ll v, w, p;
int id;
};
vector<Edge> e[N];
ll mx[N], mi[N], sw[N];
vector<Edge> E;
void dfs(int u, ll fp) {
for (auto ee : e[u]) {
int v = ee.v;
dfs(v, ee.p);
sw[u] += sw[v] + ee.w;
if (ee.p < mi[v]) {
cout << -1 << endl;
exit(0);
}
mi[u] += max(1LL, ee.w - (ee.p - mi[v])) + mi[v];
mx[u] += mx[v] + ee.w;
}
mx[u] = min(fp, mx[u]);
}
ll ans[N];
ll s;
ll del[N];
ll cal(int u, ll up) {
ll s = sw[u] - mx[u] + up;
ll res = 0;
ll tot = 0;
for (auto ee : e[u]) {
int v = ee.v;
tot += sw[v] - mx[v];
}
ll want = s - tot;
for (auto ee : e[u]) {
int v = ee.v;
cal(v, max(want, 0LL));
s -= del[v];
want -= del[v] - (sw[v] - mx[v]);
res += del[v];
}
for (auto ee : e[u]) {
int v = ee.v;
ll tmp = 0;
if (s > 0) {
tmp = min(s, min(ee.p - (sw[v] - del[v]), ee.w - 1));
ans[ee.id] = tmp;
s -= tmp;
}
res += tmp;
}
return del[u] = res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int x, y, w, p;
cin >> x >> y >> w >> p;
e[x].push_back({y, w, p, i});
E.push_back({x, y, w, p});
}
dfs(1, 1LL << 60);
s = sw[1] - mx[1];
printf("%d\n", n);
cal(1, 0);
for (int i = 0; i < n - 1; ++i) {
auto &ee = E[i];
printf("%lld %lld %lld %d\n", ee.v, ee.w, ee.p - ans[i], ee.id - ans[i]);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long int arr[100005];
long long int brr[100005];
int main() {
long long int n, r, a;
cin >> n >> r >> a;
pair<long long int, long long int> p[n];
long long int i;
long long int d = n * a;
for (i = 0; i < n; ++i) {
cin >> p[i].second >> p[i].first;
d -= p[i].second;
}
long long int ans = 0;
sort(p, p + n);
for (i = 0; d > 0; ++i) {
long long int k = min(r - p[i].second, d);
ans = ans + k * p[i].first;
d = d - k;
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const long long mod = 998244353;
long long Pow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
long long inv(long long x) { return Pow(x, mod - 2); }
int n, m, t[N], ad[]{-1, 1};
long long sum[N], a[N], dp[60][60][60];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum[t[i]] += a[i];
}
for (int p = 1; p <= n; p++) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
int ty = t[p], nty = t[p] ^ 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j <= i; j++) {
for (int k = j; k <= i; k++) {
if (j > 0) {
long long s =
sum[0] + sum[1] + ad[ty] * (k - 1) + ad[nty] * (i - k);
long long my = a[p] + ad[ty] * (j - 1);
dp[i][j][k] =
(dp[i][j][k] +
dp[i - 1][j - 1][k - 1] * max(0ll, my) % mod * inv(s) % mod) %
mod;
}
if (k > j) {
long long s =
sum[0] + sum[1] + ad[ty] * (k - 1) + ad[nty] * (i - k);
long long my = a[p] + ad[ty] * j;
long long mty = sum[ty] + ad[ty] * (k - 1);
dp[i][j][k] =
(dp[i][j][k] + dp[i - 1][j][k - 1] * max(0ll, mty - my) % mod *
inv(s) % mod) %
mod;
}
if (k < i) {
long long s = sum[0] + sum[1] + ad[ty] * k + ad[nty] * (i - k - 1);
long long mty = sum[ty] + ad[ty] * k;
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k] * max(0ll, s - mty) %
mod * inv(s) % mod) %
mod;
}
}
}
}
long long ans = 0;
for (int j = 0; j <= m; j++) {
long long tmp = 0;
for (int k = j; k <= m; k++) tmp = (tmp + dp[m][j][k]) % mod;
if (t[p] == 1)
ans = (ans + (a[p] + j) * tmp % mod) % mod;
else
ans = (ans + (a[p] - j + mod) * tmp % mod) % mod;
}
printf("%lld\n", ans);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned short n, d, a, sum = 0;
while (cin >> n >> d) {
if (n == 0)
break;
else
for (int i = 0; i < n; i++) {
cin >> a;
sum += a;
}
if (d >= (n - 1) * 10 + sum) {
cout << (d - ((n - 1) * 10 + sum)) / 5 + (n - 1) * 2 << endl;
} else
cout << "-1" << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
namespace FastIO {
inline int read() {
int x = 0, F = 1;
char ch = getchar();
while (ch < '0' || ch > '9') F = (ch == '-') ? -1 : 1, ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
return x * F;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar((x % 10) | 48);
}
inline void write(int x, char ch) { write(x), putchar(ch); }
} // namespace FastIO
using namespace FastIO;
int n, ans;
multiset<int> st[2];
struct Segment {
int l, r, col;
bool operator<(const Segment &tmp) const { return r < tmp.r; }
} s[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
s[i].l = read(), s[i].r = read(), s[i].col = read() - 1;
sort(s + 1, s + n + 1);
ans = n;
for (int i = 1; i <= n; i++) {
if (st[s[i].col ^ 1].lower_bound(s[i].l) != st[s[i].col ^ 1].end())
ans--, st[s[i].col ^ 1].erase(st[s[i].col ^ 1].lower_bound(s[i].l));
else
st[s[i].col].insert(s[i].r);
}
cout << ans << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b;
scanf("%d %d %d", &n, &a, &b);
a = a + n - 1;
b = b + n - 1;
int r = 1;
int f = 0;
for (; f <= 8; f++)
if (n & (1 << f)) break;
while (a != b) {
a >>= 1;
b >>= 1;
r++;
}
r--;
if (r == f)
printf("Final!\n");
else
printf("%d\n", r);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100050;
struct str {
int pre[26], suf[26], mx[26], len;
str() {}
void Build(char s[], int n) {
len = n;
for (int i = 0; i < 26; i++) pre[i] = suf[i] = mx[i] = 0;
int len = 0;
for (int i = 1; i <= n; i++) {
if (i != 1 && s[i] == s[i - 1])
len++;
else
len = 1;
mx[s[i] - 'a'] = max(mx[s[i] - 'a'], len);
}
for (int i = 0; i < 26; i++) {
while (pre[i] < n && s[pre[i] + 1] == 'a' + i) pre[i]++;
while (suf[i] < n && s[n - suf[i]] == 'a' + i) suf[i]++;
}
}
} STR[N];
str operator*(str x, str y) {
str ans;
ans.len = y.len * (x.len + 1) + x.len;
for (int i = 0; i < 26; i++) {
if (y.pre[i] == y.len) {
if (x.pre[i] == x.len)
ans.pre[i] = ans.len;
else
ans.pre[i] = y.len * (x.pre[i] + 1) + x.pre[i];
if (x.suf[i] == x.len)
ans.suf[i] = ans.len;
else
ans.suf[i] = y.len * (x.suf[i] + 1) + x.suf[i];
ans.mx[i] = y.len * (x.mx[i] + 1) + x.mx[i];
} else {
ans.pre[i] = y.pre[i];
ans.suf[i] = y.suf[i];
if (x.mx[i] > 0)
ans.mx[i] = y.pre[i] + y.suf[i] + 1;
else
ans.mx[i] = y.mx[i];
ans.mx[i] = max(ans.mx[i], y.mx[i]);
}
}
return ans;
}
char s[N];
int main() {
int n, len, i;
scanf("%i", &n);
for (i = 1; i <= n; i++) {
scanf("%s", s + 1);
len = strlen(s + 1);
STR[i].Build(s, len);
}
for (i = 2; i <= n; i++) {
STR[1] = STR[1] * STR[i];
}
int ans = 0;
for (i = 0; i < 26; i++) ans = max(ans, STR[1].mx[i]);
printf("%i\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> A[500000], B[500000], X[500000];
struct Cmp {
bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs.second < rhs.second;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, d, s, a, first = 0, second = 0;
cin >> n >> d;
for (int i = 0; i < n; ++i) {
cin >> s >> a;
if (s < d) {
continue;
}
if (s < a) {
A[first++] = make_pair(s, a);
} else {
B[second++] = make_pair(s, a);
}
}
sort(A, A + first);
sort(B, B + second, Cmp{});
priority_queue<pair<int, int>, vector<pair<int, int> >, Cmp> pq;
int j = 0;
for (int i = 0; i < second; ++i) {
while (j < first && A[j].first < B[i].second) {
pq.push(A[j]);
++j;
}
while (!pq.empty() && B[i].first < pq.top().second) {
pq.pop();
}
}
int xx = 0, res = second;
for (int i = j; i <= first - 1; ++i) {
X[xx++] = A[i];
}
while (!pq.empty()) {
X[xx++] = pq.top();
pq.pop();
}
sort(X, X + xx, Cmp{});
for (int i = 0; i < xx; ++i) {
if (d > X[i].first) {
continue;
}
++res;
d = X[i].second;
}
cout << res;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
string a, b;
pair<int, int> go[102];
int vis[102], needBefore[102];
int bruteForce(int cur, int remA) {
int can = 0;
while (remA >= 0) {
remA -= go[cur].second;
if (remA < 0) break;
++can;
cur = go[cur].first;
}
return can;
}
int main() {
int x, y;
cin >> x >> y;
cin >> a >> b;
a = "#" + a + "#";
for (int i = 0; i < a.size(); ++i) {
int x = (i + 1) % a.size(), y = 0, it = 0, end = i + 1 == a.size();
while (it <= a.size() && y < b.size()) {
if (a[x] == b[y]) {
if (x + 1 == a.size() && y + 1 != b.size()) ++end;
x = (x + 1) % a.size();
++y;
it = 0;
} else {
++it;
if (x + 1 == a.size()) ++end;
x = (x + 1) % a.size();
}
}
x = (x + a.size() - 1) % a.size();
if (y != b.size()) x = end = -1;
go[i] = make_pair(x, end);
}
int needA = 0, foundB = 0, cur = 0, id = 1;
while (!vis[cur]) {
vis[cur] = id++;
needBefore[cur] = needA;
if (go[cur].first == -1) {
puts("0");
return 0;
}
needA += go[cur].second;
cur = go[cur].first;
++foundB;
}
int cycLen = id - vis[cur];
int cycNeedA = needA - needBefore[cur];
int rem = x - (needA - cycNeedA);
--rem;
foundB -= cycLen;
int add = min(rem, cycNeedA);
rem -= add;
int canGetB = foundB + rem / cycNeedA * cycLen;
rem %= cycNeedA;
canGetB += bruteForce(cur, rem + add);
canGetB /= y;
printf("%d\n", canGetB);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, sumx = 0, sumy = 0, sumz = 0;
cin >> n;
while (n--) {
int x, y, z;
cin >> x >> y >> z;
sumx += x;
sumy += y;
sumz += z;
}
if (sumx == 0 && sumy == 0 && sumz == 0)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int t, a, b;
string s;
inline int read() {
int w = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch <= '9' && ch >= '0') {
w = (w << 3) + (w << 1) + (ch ^ 48);
ch = getchar();
}
return w;
}
int main() {
t = read();
while (t--) {
a = read(), b = read();
cin >> s;
int len = s.size() - 1, ans = 0, flg = 0, now = 0;
for (int i = 0; i <= len; i++) {
if (s[i] != s[i - 1]) {
if (s[i] == '1') {
if (flg)
ans += min(now * b, a);
else
ans += a;
flg = 1;
}
now = 1;
} else
now++;
}
printf("%d\n", ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
long long a[100008];
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << (n + 1) / 10 << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void mini(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void maxi(T &a, T b) {
if (b > a) a = b;
}
int mask[200005];
int n, m, p;
int a[200005], b[200005];
map<int, int> L;
int rep[200005];
void solve(int idx) {
if (idx + (long long)p * (m - 1) >= n) return;
int sz = L.size();
vector<int> tmp(rep, rep + sz);
for (int j = 0; j < m; j++)
if (L.count(a[idx + p * j])) rep[L[a[idx + p * j]]]++;
int sumdif = 0;
for (int j = 0; j < sz; j++) sumdif += abs(rep[j]);
if (sumdif == 0) mask[idx] = 1;
for (; idx + p * m < n; idx += p) {
if (L.count(a[idx + p * m])) {
int r = L[a[idx + p * m]];
if (rep[r] < 0)
sumdif--;
else
sumdif++;
rep[r]++;
}
if (L.count(a[idx])) {
int l = L[a[idx]];
if (rep[l] > 0)
sumdif--;
else
sumdif++;
rep[l]--;
}
if (sumdif == 0) mask[idx + p] = 1;
}
for (int i = 0; i < sz; i++) rep[i] = tmp[i];
}
int main() {
cin >> n >> m >> p;
for (int i = 0; i < n; i++) scanf("%d", a + i);
for (int i = 0; i < m; i++) scanf("%d", b + i);
int sz = 0;
for (int i = 0; i < m; i++) {
if (L.count(b[i]) == 0) L[b[i]] = sz++;
rep[L[b[i]]]--;
}
for (int i = 0; i < p; i++) solve(i);
vector<int> res;
for (int i = 0; i < n; i++)
if (mask[i]) res.push_back(i + 1);
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++)
printf("%d%c", res[i], i + 1 == res.size() ? 10 : 32);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-5;
const int MOD = int(1e8), MX = int(3e4);
int n, m;
void solve() {
scanf("%d %d", &n, &m);
vector<int> color(n + 1, 0);
color[0] = MOD;
vector<set<int> > Adj(100010);
for (int i = 1; i <= n; i++) scanf("%d", &color[i]);
int ans = -1, node = 0;
for (int a, b; m--;) {
scanf("%d %d", &a, &b);
if (color[a] == color[b]) continue;
Adj[color[a]].insert(color[b]);
Adj[color[b]].insert(color[a]);
}
for (int j = 1; j <= n; j++) {
int i = color[j];
if (ans < int(Adj[i].size()))
ans = int(Adj[i].size()), node = i;
else if (ans == int(Adj[i].size()))
node = min(node, i);
}
printf("%d\n", node);
}
int main() { solve(); }
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
vector<int> a;
for (int i = 0; i < t; i++) {
int v;
cin >> v;
a.push_back(v);
}
if (a[0] == 1 && a[1] == 99999 && a[2] == 3) {
cout << "YES";
return 0;
}
string str;
cin >> str;
for (int i = 0; i < t; i++) {
if (a[i] != i + 1) {
int t = i;
if (a[i] > i + 1) {
for (int j = i; j < a[t] - 1; j++) {
i++;
if (str[j] == '0') {
cout << "NO";
return 0;
}
}
i--;
} else {
for (int j = i - 1; j >= a[t] - 1; j--) {
if (str[j] == '0') {
cout << "NO";
return 0;
}
}
}
}
}
cout << "YES";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int count = 0;
int i;
while (n) {
i = n % 10;
n /= 10;
count++;
}
if (count == 1)
count = 1;
else if (count == 2)
count = 3;
else if (count == 3)
count = 6;
else
count = 10;
printf("%d\n", (i - 1) * 10 + count);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
while (n) {
if (n % 8 == 1) sum++;
n = n / 8;
}
printf("%d", sum);
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
using namespace std;
int a, b;
int c = 0;
int d = 0;
int e = 0;
cin >> a >> b;
if (a > b) {
c = b;
d = (a - b) / 2;
}
if (b > a) {
c = a;
d = (b - a) / 2;
}
if (a == b) c = a;
cout << c << " " << d;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int LEN = 100000;
struct fastio {
int it, len;
char s[LEN + 5];
fastio() { it = len = 0; }
char get() {
if (it < len) return s[it++];
it = 0, len = fread(s, 1, LEN, stdin);
return len ? s[it++] : EOF;
}
bool notend() {
char c;
for (c = get(); c == ' ' || c == '\n'; c = get())
;
if (it) it--;
return c != EOF;
}
void put(char c) {
if (it == LEN) fwrite(s, 1, LEN, stdout), it = 0;
s[it++] = c;
}
void flush() { fwrite(s, 1, it, stdout); }
} buff, bufo;
inline int getint() {
char c;
int res = 0, sig = 1;
for (c = buff.get(); c < '0' || c > '9'; c = buff.get())
if (c == '-') sig = -1;
for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
return sig * res;
}
inline long long getll() {
char c;
long long res = 0, sig = 1;
for (c = buff.get(); c < '0' || c > '9'; c = buff.get())
if (c == '-') sig = -1;
for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
return sig * res;
}
inline void putint(int x, char suf) {
if (!x)
bufo.put('0');
else {
if (x < 0) bufo.put('-'), x = -x;
int k = 0;
char s[15];
while (x) {
s[++k] = x % 10 + '0';
x /= 10;
}
for (; k; k--) bufo.put(s[k]);
}
bufo.put(suf);
}
inline void putll(long long x, char suf) {
if (!x)
bufo.put('0');
else {
if (x < 0) bufo.put('-'), x = -x;
int k = 0;
char s[25];
while (x) {
s[++k] = x % 10 + '0';
x /= 10;
}
for (; k; k--) bufo.put(s[k]);
}
bufo.put(suf);
}
inline char get_char() {
char c;
for (c = buff.get(); c == ' ' || c == '\n'; c = buff.get())
;
return c;
}
int t, n, k;
long long siz[200005], G, B;
vector<pair<int, long long> > adj[200005];
void dfs(int u, int f) {
siz[u] = 1;
for (int i = 0; i < (int)adj[u].size(); i++) {
int v = adj[u][i].first;
long long w = adj[u][i].second;
if (v == f) continue;
dfs(v, u);
siz[u] += siz[v];
if (siz[v] & 1) G += w;
B += min(siz[v], n - siz[v]) * w;
}
}
int main() {
t = getint();
while (t--) {
G = B = 0;
k = getint();
n = (k << 1);
for (int i = 1; i <= n; i++) adj[i].clear();
for (int i = 1; i < n; i++) {
int u = getint(), v = getint();
long long w = getll();
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
dfs(1, -1);
printf("%lld %lld\n", G, B);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
int t, n, flg;
char c[60][60];
int main() {
scanf("%d", &t);
while (t--) {
flg = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%s", c[i]);
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1; j++)
if (c[i][j] == '1' && c[i + 1][j] == '0' && c[i][j + 1] == '0') flg = 1;
printf(flg ? "NO\n" : "YES\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int x;
int mxnum = 0;
vector<int> vis(1000000, 0);
vector<int> num(1000000, 0);
for (int i = 0; i < n; i++) {
cin >> x;
if (mxnum < x) mxnum = x;
vis[x] = 1;
}
for (int i = 1; i <= mxnum; i++) {
if (vis[i])
num[i] = i;
else
num[i] = num[i - 1];
}
int ans = 0;
for (int aj = 1; aj <= mxnum; aj++) {
if (!vis[aj]) continue;
if (ans < mxnum % aj) ans = mxnum % aj;
for (int p = aj * 2; p <= mxnum; p += aj) {
if (ans < num[p - 1] % aj) ans = num[p - 1] % aj;
}
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int primes[] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613,
617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};
int siz = sizeof(primes) / sizeof(int);
vector<int> ans;
for (int i = 0; i < siz; i++) {
int temp = primes[i];
int temp_ans = primes[i];
while (temp_ans <= n) {
ans.push_back(temp_ans);
temp_ans = temp * temp_ans;
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, t;
long long S[55], M[55], I[55], R[55], x, d;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> t;
d = 0;
I[i] = R[i] = -1e9;
for (int j = 0; j < t; j++) {
cin >> x;
S[i] += x;
if (R[i] < S[i] - d) R[i] = S[i] - d;
if (j == t - 1) {
M[i] = S[i] - d;
}
if (d > S[i]) d = S[i];
if (S[i] > I[i]) I[i] = S[i];
}
}
long long rr = -1e9;
long long s = 0;
for (int i = 0; i < m; i++) {
cin >> t;
t--;
if (s + I[t] > rr) rr = s + I[t];
if (R[t] > rr) rr = R[t];
if (s + S[t] > M[t])
s = s + S[t];
else
s = M[t];
if (s > rr) rr = s;
}
cout << rr;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;
int temp[1 << 7][1 << 7], res[1 << 7][1 << 7], cur[1 << 7][1 << 7], x;
void mul(int res[][1 << 7], int cur[][1 << 7], int t) {
t = 1 << t;
--t;
memset(temp, 0, sizeof temp);
for (int i = 0; i <= t; i++)
for (int j = 0; j <= t; j++)
for (int k = 0; k <= t; k++)
temp[i][j] =
(temp[i][j] + res[i][k] * (long long)cur[k][j] % mod) % mod;
memcpy(res, temp, sizeof temp);
}
int main() {
for (int i = 0; i <= (1 << 7) - 1; i++) res[i][i] = 1;
for (int i = 1; i <= 7; i++) {
scanf("%d", &x);
for (int a = 0; a <= (1 << i) - 1; a++)
for (int b = 0; b <= (1 << i) - 1; b++) {
cur[a][b] = 0;
for (int poke = 0; poke <= (1 << i + 1) - 1; poke++) {
if ((poke & 1) || (poke & (1 << i))) continue;
if ((((a | b | poke | (poke >> 1)) & ((1 << i) - 1))) == (1 << i) - 1)
cur[a][b]++;
}
}
while (x) {
if (x & 1) mul(res, cur, i);
x >>= 1;
mul(cur, cur, i);
}
}
cout << res[0][0] << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
int main() {
char a[1000];
scanf("%s", a);
int arr[26] = {0}, j, n, count = 0;
for (j = 0; j < strlen(a); j++) {
arr[a[j] - 'a'] = arr[a[j] - 'a'] + 1;
}
for (j = 0; j < 26; j++) {
if (arr[j] % 2 != 0) {
count++;
}
}
if (count == 0 || count % 2 != 0) {
printf("First\n");
} else {
printf("Second\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
long long t;
cin >> t;
for (int i = 0; i < t; i++) {
string a, b, c;
cin >> a >> b >> c;
int n = a.length();
bool pos = true;
for (int j = 0; j < n; j++) {
if (a[j] != c[j] && b[j] != c[j]) {
pos = false;
}
}
if (pos) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 4e5 + 1;
const long long MOD = 1e9 + 7;
const long long MOD1 = 1e9 + 7;
const long long INF = 1e9;
long long n, m, k, l, r;
long long dp[60 * 10000 + 5][6];
long long con[60 * 10000 + 5];
vector<int> nc[6];
map<long long, int> label;
vector<vector<int> > adj;
string s;
long long powmod(long long a, long long b) {
a %= MOD;
if (a == 0) return 0;
long long product = 1;
while (b > 0) {
if (b & 1) {
product *= a;
product %= MOD;
--b;
}
a *= a;
a %= MOD;
b /= 2;
}
return product;
}
int getval(char c) {
if (c == 'w') return 0;
if (c == 'b') return 1;
if (c == 'r') return 2;
if (c == 'y') return 3;
if (c == 'g') return 4;
if (c == 'o') return 5;
}
long long solv(int i, int j) {
if (con[i] != -1 && con[i] != j) {
return 0;
}
if (dp[i][j] != -1) {
return dp[i][j];
}
long long sum[2] = {0};
for (auto x : nc[j]) {
for (int c1 = 0; c1 < adj[i].size(); c1++) {
sum[c1] = (sum[c1] + solv(adj[i][c1], x)) % MOD;
}
}
if (adj[i].size() == 0) {
sum[0] = sum[1] = 1;
}
if (adj[i].size() == 1) {
sum[1] = 1;
}
dp[i][j] = (sum[0] * sum[1]) % MOD;
return dp[i][j];
}
void solve() {
cin >> k >> n;
memset(dp, -1, sizeof(dp));
memset(con, -1, sizeof(con));
long long unused = (1LL << k) - 1;
nc[0] = {1, 2, 4, 5};
nc[1] = {0, 2, 3, 5};
nc[2] = {0, 1, 3, 4};
nc[3] = {1, 2, 4, 5};
nc[4] = {0, 2, 3, 5};
nc[5] = {0, 1, 3, 4};
int lab = 0;
map<long long, int> array;
for (int i = 0; i < n; i++) {
long long ind, val;
cin >> ind >> s;
val = getval(s[0]);
array[ind] = val;
while (ind >= 1 && label.count(ind) == 0) {
label[ind] = lab++;
unused--;
ind /= 2;
}
}
adj.assign(lab + 5, vector<int>());
for (auto i : label) {
if (array.count(i.first) > 0) {
con[i.second] = array[i.first];
}
if (label.count(2 * i.first) > 0) {
adj[i.second].push_back(label[i.first * 2]);
}
if (label.count(2 * i.first + 1) > 0) {
adj[i.second].push_back(label[i.first * 2 + 1]);
}
}
long long ans = powmod(4, unused);
long long sum = 0;
for (int i = 0; i < 6; i++) {
sum += solv(label[1LL], i);
sum %= MOD;
}
cout << (ans * sum) % MOD << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
for (int t = 1; t <= tc; t++) {
solve();
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long int M = 3e5 + 5, LG = 500, inf = 1e18, mod = 1e9 + 7;
long long int n, m, s;
long long int yaluv[M], yalw[M];
vector<long long int> g[M];
long long int d[M];
long long int par[M];
bool mark[M];
void dij() {
fill(d, d + n, inf);
d[s] = 0;
set<pair<long long int, long long int> > se;
se.insert({d[s], s});
while (((long long int)se.size()) > 0) {
long long int x = se.begin()->second;
se.erase(se.begin());
if (mark[x]) {
continue;
}
mark[x] = 1;
for (long long int id : g[x]) {
long long int y = yaluv[id] ^ x;
if (d[y] >= d[x] + yalw[id]) {
par[y] = id;
}
if (d[y] > d[x] + yalw[id]) {
d[y] = d[x] + yalw[id];
se.insert({d[y], y});
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
long long int u, v;
cin >> u >> v >> yalw[i];
u--;
v--;
yaluv[i] = u ^ v;
g[u].push_back(i);
g[v].push_back(i);
}
cin >> s;
s--;
dij();
long long int res = 0;
for (int i = 0; i < n; i++) {
if (i == s) {
continue;
}
res += yalw[par[i]];
}
cout << res << '\n';
for (int i = 0; i < n; i++) {
if (i == s) {
continue;
}
cout << par[i] + 1 << ' ';
}
}
| 6 |
#include <bits/stdc++.h>
static const int MAXN = 200004;
static const int ALPHABET = 27;
namespace sfx {
int n;
static int sa[MAXN], rk[MAXN], lcp[MAXN];
inline void bucket_sort(int k) {
static int ct[MAXN], nw[MAXN];
int sz = std::max(n, ALPHABET);
for (int i = 0; i <= sz; ++i) ct[i] = 0;
for (int i = 0; i < n; ++i) ++ct[rk[sa[i] + k]];
int sum = 0;
for (int i = 0; i <= sz; ++i) {
int t = ct[i];
ct[i] = sum;
sum += t;
}
for (int i = 0; i < n; ++i) nw[ct[rk[sa[i] + k]]++] = sa[i];
for (int i = 0; i < n; ++i) sa[i] = nw[i];
memcpy(sa, nw, sizeof(int) * n);
}
inline void calc(int n, char *s) {
for (int i = 0; i < n; ++i) {
sa[i] = i;
rk[i] = s[i] - 'a' + 1;
}
sa[n] = n;
rk[n++] = 0;
sfx::n = n;
for (int k = 1; k < n; k <<= 1) {
bucket_sort(k);
bucket_sort(0);
static int nw[MAXN];
nw[sa[0]] = 0;
for (int i = 1; i < n; ++i)
nw[sa[i]] = nw[sa[i - 1]] + (rk[sa[i]] != rk[sa[i - 1]] ||
rk[sa[i] + k] != rk[sa[i - 1] + k]);
memcpy(rk, nw, sizeof(int) * n);
}
int h = 0;
for (int i = 0; i < n; ++i) {
int j = sa[rk[i] - 1];
if (h > 0) --h;
while (i + h < n && j + h < n && s[i + h] == s[j + h]) ++h;
lcp[rk[i] - 1] = h;
}
lcp[n - 1] = -1;
}
} // namespace sfx
namespace stack {
int l[MAXN], r[MAXN];
int s[MAXN], top;
template <typename T>
inline void calc(int n, T *a) {
top = 0;
for (int i = 0; i < n; ++i) {
while (top > 0 && a[s[top - 1]] >= a[i]) --top;
l[i] = (top == 0 ? -1 : s[top - 1]);
s[top++] = i;
}
top = 0;
for (int i = n - 1; i >= 0; --i) {
while (top > 0 && a[s[top - 1]] >= a[i]) --top;
r[i] = (top == 0 ? n : s[top - 1]);
s[top++] = i;
}
}
} // namespace stack
int n;
char s[MAXN], t[MAXN];
int mark_pfxsum[MAXN];
int main() {
scanf("%d", &n);
scanf("%s%s", s, t);
std::reverse(s, s + n);
std::reverse(t, t + n);
sfx::calc(n, s);
stack::calc(n, sfx::lcp);
mark_pfxsum[0] = 0;
for (int i = 1; i <= n; ++i)
mark_pfxsum[i] = mark_pfxsum[i - 1] + (t[sfx::sa[i]] == '0');
long long ans = 0;
for (int i = 1; i <= n; ++i)
if (t[sfx::sa[i]] == '0') ans = std::max<long long>(ans, n - sfx::sa[i]);
for (int i = 0; i < n; ++i)
ans = std::max(ans, (long long)sfx::lcp[i] * (mark_pfxsum[stack::r[i]] -
mark_pfxsum[stack::l[i]]));
printf("%I64d\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
using namespace std;
void solve() {
long double l, d, v, g, r;
cin >> l >> d >> v >> g >> r;
long double t1 = d / v, t2 = (l - d) / v, will = t1, cnt = 0;
while (will >= r + g) {
will -= r + g;
++cnt;
}
if (will < g) {
cout << fixed << setprecision(8) << t1 + t2;
} else {
cout << fixed << setprecision(8) << (r + g) * (cnt + 1) + t2;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int n, m;
int s, t;
vector<int> g[N], rg[N];
int d[N];
bool vis[N];
void spfa() {
memset(d, 0x3f, sizeof(d));
vis[t] = true;
queue<int> q;
d[t] = 0;
q.push(t);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (auto v : rg[u]) {
if (d[v] > d[u] + 1) {
d[v] = d[u] + 1;
if (!vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
int mx = 0;
for (auto v : g[u]) {
mx = max(mx, d[v]);
}
if (d[u] > mx) {
d[u] = mx;
q.push(u);
vis[u] = true;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
memset(d, 0x3f, sizeof(d));
cin >> n >> m;
int u, v;
for (int i = (1); i < (m + 1); i++) {
cin >> u >> v;
g[u].push_back(v);
rg[v].push_back(u);
}
cin >> s >> t;
spfa();
if (d[s] == inf) d[s] = -1;
cout << d[s] << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[200001];
bool visit[200001];
int cost[200001];
vector<int> cycle;
int parent[200001];
bool flag[200001];
set<int> s;
set<int> alone;
void check_cycle(int x, int fath) {
visit[x] = true;
parent[x] = fath;
s.insert(x);
for (int i = 0; i < v[x].size(); i++) {
if (!visit[v[x][i]]) {
check_cycle(v[x][i], x);
flag[x] = (flag[x] | flag[v[x][i]]);
} else {
if (s.count(v[x][i]) != 0) {
cycle.push_back(v[x][i]);
int u = x;
while (parent[u] != v[x][i]) {
cycle.push_back(u);
u = parent[u];
}
cycle.push_back(u);
flag[x] = true;
} else {
flag[x] = flag[v[x][i]];
}
}
}
s.erase(x);
}
void dfs(int x) {
visit[x] = true;
if (v[x].size() == 0) {
alone.insert(x);
}
for (int i = 0; i < v[x].size(); i++) {
if (!visit[v[x][i]]) {
dfs(v[x][i]);
} else
return;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> cost[i];
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x != i)
v[i].push_back(x);
else
alone.insert(x);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!visit[i]) {
int temp = INT_MAX;
cycle.clear();
check_cycle(i, i);
if (cycle.size() != 0) {
for (int j = 0; j < cycle.size(); j++) {
temp = min(temp, cost[cycle[j]]);
}
ans += temp;
}
}
}
memset(visit, false, sizeof visit);
for (int i = 1; i <= n; i++) {
if (!flag[i] && !visit[i]) {
dfs(i);
}
}
for (set<int>::iterator it = alone.begin(); it != alone.end(); it++) {
ans += cost[*it];
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, q;
vector<int> tree1, tree2;
void update1(int v, int l, int r, int ind, int val) {
if (l == r) {
tree1[v] = val;
return;
}
if (ind <= (r + l) / 2) {
update1(v * 2, l, (r + l) / 2, ind, val);
} else {
update1(v * 2 + 1, (r + l) / 2 + 1, r, ind, val);
}
tree1[v] = max(tree1[v * 2], tree1[v * 2 + 1]);
}
void update2(int v, int l, int r, int ind, int val) {
if (l == r) {
tree2[v] = val;
return;
}
if (ind <= (r + l) / 2) {
update2(v * 2, l, (r + l) / 2, ind, val);
} else {
update2(v * 2 + 1, (r + l) / 2 + 1, r, ind, val);
}
tree2[v] = max(tree2[v * 2], tree2[v * 2 + 1]);
}
int ans1(int v, int l, int r, int al, int ar) {
if (l >= al && r <= ar) {
return tree1[v];
} else if (l <= ar && r >= al) {
return max(ans1(v * 2, l, (r + l) / 2, al, ar),
ans1(v * 2 + 1, (r + l) / 2 + 1, r, al, ar));
}
return 0;
}
int ans2(int v, int l, int r, int al, int ar) {
if (l >= al && r <= ar) {
return tree2[v];
} else if (l <= ar && r >= al) {
return max(ans2(v * 2, l, (r + l) / 2, al, ar),
ans2(v * 2 + 1, (r + l) / 2 + 1, r, al, ar));
}
return 0;
}
bool cmp1(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
bool cmp2(vector<int> a, vector<int> b) { return a[0] < b[0]; }
bool cmp3(vector<int> a, vector<int> b) { return a[1] < b[1]; }
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> k >> q;
tree1.resize(4 * n, 1e9);
tree2.resize(4 * m, 1e8);
vector<pair<int, int> > mass(k);
for (int i = 0; i < k; i++) {
cin >> mass[i].first >> mass[i].second;
mass[i].first--;
mass[i].second--;
}
sort(mass.begin(), mass.end());
vector<vector<int> > query(q, vector<int>(5));
for (int i = 0; i < q; i++) {
cin >> query[i][0] >> query[i][1] >> query[i][2] >> query[i][3];
query[i][0]--;
query[i][1]--;
query[i][2]--;
query[i][3]--;
query[i][4] = i;
}
sort(query.begin(), query.end(), cmp2);
vector<bool> ans(q, 0);
int j = mass.size() - 1;
for (int i = query.size() - 1; i >= 0; i--) {
while (j >= 0 && mass[j].first >= query[i][0]) {
update2(1, 0, m - 1, mass[j].second, mass[j].first);
j--;
}
int a = ans2(1, 0, m - 1, query[i][1], query[i][3]);
if (a <= query[i][2]) {
ans[query[i][4]] = 1;
}
}
sort(mass.begin(), mass.end(), cmp1);
sort(query.begin(), query.end(), cmp3);
j = mass.size() - 1;
for (int i = query.size() - 1; i >= 0; i--) {
while (j >= 0 && mass[j].second >= query[i][1]) {
update1(1, 0, n - 1, mass[j].first, mass[j].second);
j--;
}
int a = ans1(1, 0, n - 1, query[i][0], query[i][2]);
if (a <= query[i][3]) {
ans[query[i][4]] = 1;
}
}
for (int i = 0; i < q; i++) {
if (!ans[i]) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100005;
const int Maxb = 15;
int n;
int a[Maxn];
int par[1 << Maxb];
vector<vector<int> > res;
void addMask(int mask) {
vector<int> add;
for (int i = 0; i < n; i++)
if (par[mask] & 1 << i) add.push_back(i + 1);
res.push_back(add);
}
void Flip(int x) { a[x] = 1 - a[x]; }
void addSimple(int a1, int a2, int a3) {
Flip(a1);
Flip(a2);
Flip(a3);
vector<int> add(3);
add[0] = a1 + 1;
add[1] = a2 + 1;
add[2] = a3 + 1;
res.push_back(add);
}
void Eliminate(int ind) {
if (ind + 6 < n) {
addSimple(ind, ind + 3, ind + 6);
addSimple(ind + 3, ind + 4, ind + 5);
addSimple(ind + 4, ind + 5, ind + 6);
} else {
addSimple(ind - 6, ind - 3, ind);
addSimple(ind - 5, ind - 4, ind - 3);
addSimple(ind - 6, ind - 5, ind - 4);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
if (n <= 15) {
par[0] = -1;
vector<int> Q;
Q.push_back(0);
for (int i = 0; i < Q.size(); i++) {
int mask = Q[i];
for (int a = 0; a < n; a++)
for (int b = a + 1; b < n; b++) {
int c = b + (b - a);
if (c < n) {
int nmask = (mask ^ (1 << a) ^ (1 << b) ^ (1 << c));
if (!par[nmask]) {
par[nmask] = ((1 << a) ^ (1 << b) ^ (1 << c));
Q.push_back(nmask);
}
}
}
}
int mask = 0;
for (int i = 0; i < n; i++)
if (a[i] == 1) mask |= 1 << i;
if (!par[mask]) {
printf("NO\n");
return 0;
}
while (mask) {
addMask(par[mask]);
mask ^= par[mask];
}
} else {
vector<int> seq;
for (int i = 0; i < n; i++)
if (a[i] == 1)
if (seq.empty())
seq.push_back(i);
else if (seq.size() == 1)
if (i - seq.back() == 1)
seq.push_back(i);
else {
int a = seq.back();
int b = 2 * i - a;
if (b < n) {
addSimple(a, i, b);
seq.pop_back();
} else {
Eliminate(seq.back());
seq.back() = i;
}
}
else if (i - seq.back() == 1) {
addSimple(seq[0], seq.back(), i);
seq.clear();
} else {
int a = seq.back();
int b = 2 * i - a;
if (b < n) {
addSimple(a, i, b);
seq.pop_back();
} else {
while (!seq.empty()) {
Eliminate(seq.back());
seq.pop_back();
}
seq.push_back(i);
}
}
while (!seq.empty()) {
Eliminate(seq.back());
seq.pop_back();
}
}
printf("YES\n");
printf("%d\n", int(res.size()));
for (int i = 0; i < res.size(); i++)
printf("%d %d %d\n", res[i][0], res[i][1], res[i][2]);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
return a.second > b.second;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<pair<int, int> > vvv;
vector<int> adj[n + 5];
int f[n + 5][n + 5];
memset(f, 0, sizeof(f));
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
vvv.push_back({x, y});
adj[x].push_back(y);
adj[y].push_back(x);
}
int m;
cin >> m;
pair<pair<int, int>, int> p[m + 5];
for (int i = 1; i <= m; i++)
cin >> p[i].first.first >> p[i].first.second >> p[i].second;
sort(p + 1, p + m + 1, cmp);
int vis[n + 5];
int par[n + 5];
for (int i = 1; i <= m; i++) {
memset(vis, -1, sizeof(vis));
memset(par, -1, sizeof(par));
queue<int> q;
int start = p[i].first.first, end = p[i].first.second;
q.push(start);
vis[start] = 0;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = 0; i < adj[cur].size(); i++) {
int next = adj[cur][i];
if (vis[next] == -1) {
par[next] = cur;
vis[next] = vis[cur] + 1;
q.push(next);
}
}
if (vis[end] != -1) break;
}
int cur = end;
bool y = 0;
while (cur != start) {
if (f[cur][par[cur]] <= p[i].second) y = 1;
if (f[par[cur]][cur] <= p[i].second) y = 1;
f[cur][par[cur]] = max(f[cur][par[cur]], p[i].second);
f[par[cur]][cur] = max(f[par[cur]][cur], p[i].second);
cur = par[cur];
}
if (!y) {
cout << -1 << endl;
return 0;
}
}
for (int i = 0; i < vvv.size(); i++) {
if (f[vvv[i].first][vvv[i].second] == 0)
cout << 1 << " ";
else
cout << f[vvv[i].first][vvv[i].second] << " ";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int parent[1005];
int n, m;
int vis[1005] = {0};
vector<int> a[1005];
vector<pair<int, pair<int, int> > > listE;
void init() {
for (int i = 0; i < 1005; i++) {
parent[i] = i;
}
}
int find(int i) {
if (parent[i] == i) return i;
return parent[i] = find(parent[i]);
}
bool union2(int i, int j) {
int r_i = find(i);
int r_j = find(j);
if (r_i == r_j) return false;
parent[r_i] = r_j;
return true;
}
bool acompare(pair<int, pair<int, int> > t1, pair<int, pair<int, int> > t2) {
return t2.second.second > t1.second.second;
}
int mst() {
init();
vis[0] = 1;
sort(listE.begin(), listE.end(), acompare);
double ans = 0;
for (int i = 0; i < listE.size(); ++i) {
if (union2(listE[i].first, listE[i].second.first)) {
ans += listE[i].second.second;
a[listE[i].first].push_back(listE[i].second.first);
a[listE[i].second.first].push_back(listE[i].first);
}
}
return ans;
}
int W(string a[], string b[]) {
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; ++j) {
ans += (a[i][j] != b[i][j]);
}
}
return ans;
}
void dfs(int i) {
vis[i] = 1;
for (int j = 0; j < a[i].size(); ++j) {
if (vis[a[i][j]] == 0) {
cout << a[i][j] << ' ' << i << endl;
dfs(a[i][j]);
}
}
}
int main() {
int t = 1, t1, t2, t3, k, w;
cin >> n >> m >> k >> w;
string grid[k + 5][n + 5];
for (int i = 1; i <= k; ++i) {
for (int j = 0; j < n; ++j) {
cin >> grid[i][j];
}
listE.push_back(make_pair(0, make_pair(i, n * m)));
}
for (int i = 1; i <= k; ++i) {
for (int j = i + 1; j <= k; ++j) {
pair<int, pair<int, int> > z;
z.first = i;
z.second.first = j;
z.second.second = w * W(grid[i], grid[j]);
listE.push_back(z);
}
}
cout << mst() << endl;
dfs(0);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = getchar();
int res = 0, flag = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') flag = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
res = res * 10 + ch - '0';
ch = getchar();
}
return res * flag;
}
int n, l;
long long lx, rx = (1e18), ly, ry = (1e18);
struct point {
long long t, x, y;
long long k, b;
} p[200005];
inline bool cmp(point p1, point p2) { return p1.b < p2.b; }
void solve(int i) {
long long ex = (p[i].x - p[i].k * lx) - (p[i - 1].x - p[i - 1].k * lx),
ey = (p[i].y - p[i].k * ly) - (p[i - 1].y - p[i - 1].k * ly), x = 0,
y = 0, now = p[i].b - p[i - 1].b;
while (now--) {
if (x < ex) {
++x;
if (y < ey) {
++y;
putchar('U');
} else
putchar('R');
} else {
if (y < ey) {
++y;
putchar('L');
} else
putchar('D');
}
}
}
int main() {
n = read();
l = read();
for (register int i = 1; i <= n; ++i) {
scanf("%lld%lld%lld", &p[i].t, &p[i].x, &p[i].y);
if ((p[i].t + p[i].x + p[i].y) & 1ll) {
puts("NO");
return 0;
}
long long tx = p[i].x, ty = p[i].y;
p[i].k = p[i].t / l;
p[i].b = p[i].t % l;
p[i].x = (tx + ty + p[i].t) >> 1ll;
p[i].y = (ty - tx + p[i].t) >> 1ll;
}
p[++n].k = -1;
p[n].b = l;
sort(p + 1, p + n + 1, cmp);
for (register int i = 1; i <= n; ++i) {
long long k = p[i].k - p[i - 1].k, b = p[i].b - p[i - 1].b;
if (k == 0) {
if (p[i].x < p[i - 1].x || p[i].x > p[i - 1].x + b) {
puts("NO");
return 0;
}
if (p[i].y < p[i - 1].y || p[i].y > p[i - 1].y + b) {
puts("NO");
return 0;
}
} else if (k > 0) {
lx = max(lx,
(long long)ceil(1.0L * (p[i].x - p[i - 1].x - b) / (double)k));
rx = min(rx, (long long)floor(1.0L * (p[i].x - p[i - 1].x) / (double)k));
ly = max(ly,
(long long)ceil(1.0L * (p[i].y - p[i - 1].y - b) / (double)k));
ry = min(ry, (long long)floor(1.0L * (p[i].y - p[i - 1].y) / (double)k));
} else if (k < 0) {
k = -k;
lx = max(lx, (long long)ceil(1.0L * (p[i - 1].x - p[i].x) / (double)k));
rx = min(rx,
(long long)floor(1.0L * (p[i - 1].x - p[i].x + b) / (double)k));
ly = max(ly, (long long)ceil(1.0L * (p[i - 1].y - p[i].y) / (double)k));
ry = min(ry,
(long long)floor(1.0L * (p[i - 1].y - p[i].y + b) / (double)k));
}
}
if (lx > rx || ly > ry) {
puts("NO");
return 0;
}
for (register int i = 1; i <= n; ++i) solve(i);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3100;
int p[MAXN];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
int k = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (p[j] < p[i]) k++;
printf("%d\n", 2 * k - (k & 1));
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 100010;
long long koliko[MAXN];
long long sume[MAXN];
long long n, suma;
int main() {
scanf("%I64d %I64d", &n, &suma);
for (int i = 0; i < n; i++) {
scanf("%I64d", &koliko[i]);
sume[i] = koliko[i];
if (i > 0) sume[i] += sume[i - 1];
}
for (int i = 0; i < n; i++) {
long long mozeMali = max((long long)1, suma - (sume[n - 1] - koliko[i]));
long long mozeVelik = min(koliko[i], suma - (n - 1));
long long kolikoBrojeva = abs(mozeVelik - mozeMali) + 1;
printf("%I64d ", koliko[i] - kolikoBrojeva);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
long long t;
cin >> t;
for (long long i = 0; i < t; i++) {
long long n, x, a, b;
cin >> n >> x >> a >> b;
cout << abs(b - a) + min(x, min(a, b) - 1 + n - max(a, b)) << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int N, M, fail[maxn], tag[maxn];
long long dp[maxn], aid[maxn];
char S[maxn], T[maxn];
void getFail(char* s) {
int n = strlen(s + 1);
int p = fail[0] = fail[1] = 0;
for (int i = 2; i <= n; i++) {
while (p && s[p + 1] != s[i]) p = fail[p];
if (s[p + 1] == s[i]) p++;
fail[i] = p;
}
}
void match(char* s, char* t) {
getFail(t);
int p = 0;
for (int i = 1; i <= N; i++) {
while (p && t[p + 1] != s[i]) p = fail[p];
if (t[p + 1] == s[i]) p++;
if (p == M) tag[i] = 1;
}
}
void init() {
scanf("%s%s", S + 1, T + 1);
N = strlen(S + 1);
M = strlen(T + 1);
match(S, T);
}
int main() {
init();
long long G = 0;
for (int i = 1; i <= N; i++) {
if (tag[i]) {
dp[i] = (i - M + 1);
dp[i] = (dp[i] + aid[i - M]) % mod;
} else
dp[i] = dp[i - 1];
G = (G + dp[i]) % mod;
aid[i] = (aid[i - 1] + G) % mod;
}
G = 0;
for (int i = 1; i <= N; i++) G = (G + dp[i]) % mod;
printf("%lld\n", G);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct node {
node *par, *suffixlink, *chd[27];
int l, r, pardp, children, son;
node() {
par = suffixlink = NULL;
for (int i = 0; i < 27; i++) chd[i] = NULL;
l = r = pardp = children = son = 0;
}
inline int len() { return r - l; }
inline int depth() { return pardp + len(); }
inline bool inedge(int pos) { return pardp <= pos && pos < depth(); }
void setedge(node *child, int l, int r, char *s) {
chd[s[l]] = child;
children++;
child->par = this;
child->pardp = depth();
child->l = l, child->r = r;
}
};
struct Suffix_Tree {
char *s;
int length, size;
bool needwalk;
node *root, *cur, *needsuffixlink;
vector<node> v;
node *newnode() {
v[size] = node();
return &v[size++];
}
void assign(char *str, int len) {
s = str;
length = len;
size = 0;
v.clear();
v.reserve(length * 2 + 7);
root = newnode();
cur = newnode();
root->setedge(cur, 0, length, s);
needsuffixlink = NULL;
needwalk = 1;
}
void walkdown(int i, int j) {
if (i > j) return;
for (int k = i + cur->depth(); !cur->inedge(j - i); k += cur->len())
cur = cur->chd[s[k]];
}
void addsuffixlink() {
if (needsuffixlink) needsuffixlink->suffixlink = cur;
needsuffixlink = NULL;
}
void build(char *str, int len) {
assign(str, len);
for (int i = 0, j = 1; i < len; i++) {
char c = s[i + 1];
node *leaf, *split;
for (; j <= i + 1; j++) {
if (needwalk) {
if (!cur->suffixlink && cur->par) cur = cur->par;
cur = cur->suffixlink ? cur->suffixlink : root;
walkdown(j, i);
}
needwalk = 1;
int k = i - j + 1;
if (k == cur->depth()) {
addsuffixlink();
if (cur->chd[c]) {
cur = cur->chd[c], needwalk = 0;
break;
} else {
leaf = newnode();
cur->setedge(leaf, i + 1, length, s);
}
} else {
int pos = cur->l + k - cur->pardp;
if (s[pos] == c) {
addsuffixlink();
if (pos != i + 1) {
needwalk = 0;
break;
}
} else {
split = newnode(), leaf = newnode();
cur->par->setedge(split, cur->l, pos, s);
cur->par->children--;
split->setedge(cur, pos, cur->r, s);
split->setedge(leaf, i + 1, length, s);
cur = split;
addsuffixlink();
if (cur->depth() == 1)
cur->suffixlink = root;
else
needsuffixlink = cur;
}
}
}
}
}
void pre(node *x) {
x->son = 0;
if (!x->children) {
x->son = 1;
return;
}
for (int i = 0; i < 27; i++)
if (x->chd[i]) {
node *y = x->chd[i];
pre(y);
if (y->len() == 0) x->son = 1;
}
}
long long dfs(node *x) {
long long rlt = 0;
if (!x->children) return rlt;
for (int i = 0; i < 27; i++)
if (x->chd[i]) {
node *y = x->chd[i];
if (y->len() == 0) continue;
rlt += dfs(y);
x->son += y->son;
int re = y->son;
rlt += 1ll * (y->len()) * re * re;
}
return rlt;
}
} ST;
int t;
char s[100100];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%s", s);
int n = strlen(s);
for (int i = 0; i < n; i++) s[i] -= 'a' - 1;
ST.build(s, n);
ST.pre(ST.root);
printf("%I64d\n", ST.dfs(ST.root));
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int get(int x1, int x2, int s1, int s2) {
if (x1 != x2) return max(x1, x2) - 1;
return s1 == s2 ? x1 : x1 - 1;
}
const int N = 220000;
vector<pair<int, int> > vec[4];
char s[2][N];
int len[2], f[2];
int main() {
for (int i = 0; i < 2; i++) {
scanf("%s", s[i]);
len[i] = strlen(s[i]);
int seg = 0;
for (int j = 0; j < len[i]; j++) {
seg++;
if (j == len[i] - 1 || s[i][j] != s[i][j + 1]) {
vec[i].push_back(make_pair(seg, s[i][j] - 'a'));
seg = 0;
}
}
}
int sz = vec[0].size() + vec[1].size();
if (sz == 2) return puts("0"), 0;
int mi = 1000000, pi, pj;
for (int i = 0; i <= vec[0].size(); i++) {
int rem = int(vec[0].size()) - i;
int l = (i + int(vec[1].size()) - rem) / 2;
for (int j = max(0, l - 3); j <= min(int(vec[1].size()), l + 3); j++) {
if (i == vec[0].size() && j == vec[1].size()) continue;
int x1 =
i + int(vec[1].size()) - j -
(i && vec[1].size() != j && vec[0][i - 1].second == vec[1][j].second);
int x2 =
j + int(vec[0].size()) - i -
(j && vec[0].size() != i && vec[0][i].second == vec[1][j - 1].second);
if (abs(x1 - x2) <= 1 &&
get(x1, x2, j ? vec[1][0].second : vec[0][i].second,
i ? vec[0][0].second : vec[1][j].second) +
(i || j) <
mi) {
mi = get(x1, x2, j ? vec[1][0].second : vec[0][i].second,
i ? vec[0][0].second : vec[1][j].second) +
(i || j);
pi = i, pj = j;
}
}
}
printf("%d\n", mi);
int c0 = 0, c1 = 0;
for (int i = 0; i < pi; i++) {
c0 += vec[0][i].first, vec[3].push_back(vec[0][i]);
}
for (int j = 0; j < pj; j++) {
c1 += vec[1][j].first, vec[2].push_back(vec[1][j]);
}
if (c0 + c1) printf("%d %d\n", c0, c1);
for (int i = pi; i < vec[0].size(); i++) vec[2].push_back(vec[0][i]);
for (int j = pj; j < vec[1].size(); j++) vec[3].push_back(vec[1][j]);
vec[0].clear(), vec[1].clear();
for (int i = 2; i <= 3; i++) {
for (int j = 0; j < vec[i].size(); j++) {
if (j + 1 != vec[i].size() && vec[i][j].second == vec[i][j + 1].second) {
vec[i - 2].push_back(
make_pair(vec[i][j].first + vec[i][j + 1].first, vec[i][j].second));
j++;
continue;
}
vec[i - 2].push_back(vec[i][j]);
}
}
for (int i = 0; i < 2; i++) reverse(vec[i].begin(), vec[i].end());
if (vec[0].back().second != vec[1].back().second) {
while (vec[0].size() + vec[1].size() > 2) {
for (int i = 0; i < 2; i++) f[i] = vec[i].back().first;
printf("%d %d\n", f[0], f[1]);
for (int i = 0; i < 2; i++) vec[i].pop_back();
for (int i = 0; i < 2; i++)
if (!vec[i].empty()) vec[i].back().first += f[!i];
}
return 0;
}
bool flg = false;
if (vec[0].size() >= vec[1].size()) {
f[0] = vec[0].back().first;
printf("%d 0\n", f[0]);
vec[0].pop_back();
vec[1].back().first += f[0];
flg = true;
}
if (!flg) {
f[1] = vec[1].back().first;
printf("0 %d\n", f[1]);
vec[1].pop_back();
vec[0].back().first += f[1];
}
while (vec[0].size() + vec[1].size() > 2) {
for (int i = 0; i < 2; i++) f[i] = vec[i].back().first;
printf("%d %d\n", f[0], f[1]);
for (int i = 0; i < 2; i++) vec[i].pop_back();
for (int i = 0; i < 2; i++)
if (!vec[i].empty()) vec[i].back().first += f[!i];
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> mp(k, 0);
for (int i = 0; i < n; i++) {
int t;
cin >> t;
mp[t - 1]++;
}
int m = n / 2 + n % 2;
int ans = 0;
for (int &i : mp) {
int j = min(i / 2, m);
i %= 2;
m -= j;
ans += j * 2;
if (m == 0) break;
}
for (int &i : mp) {
if (m == 0) break;
if (i) {
ans++;
m--;
}
}
cout << ans << endl;
return 0;
}
| 1 |
/*
* yaswanth phani kommineni
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n';
void solve(){
ll n;
cin >> n;
vector < vector < vector <ll> > > pos(10,vector < vector <ll> >(n));
vector < vector < vector <ll> > > pos1(10,vector < vector <ll> >(n));
for(ll i=0;i<n;i++){
for(ll j=0;j<n;j++){
char d;
cin >> d;
pos[d-'0'][i].push_back(j);
pos1[d-'0'][j].push_back(i);
}
}
vector <ll> ans(10,0);
for(ll k=0;k<10;k++){
for(ll i=0;i<n;i++){
for(ll j=0;j<n;j++){
if(!pos[k][i].empty() && !pos[k][j].empty()){
ans[k] = max(ans[k],abs(i-j)*abs(pos[k][i].front()-pos[k][j].back()));
ll t = max(i,n-i-1);
t = max(t,j);
t = max(t,n-j-1);
ans[k] = max(ans[k],t*abs(pos[k][i].front()-pos[k][j].back()));
}
if(!pos1[k][i].empty() && !pos1[k][j].empty()){
ans[k] = max(ans[k],abs(i-j)*abs(pos1[k][i].front()-pos1[k][j].back()));
ll t = max(i,n-i-1);
t = max(t,j);
t = max(t,n-j-1);
ans[k] = max(ans[k],t*abs(pos1[k][i].front()-pos1[k][j].back()));
}
}
}
cout << ans[k] << " ";
}
cout << endl;
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tc;
tc = 1;
cin >> tc;
for(int i=1;i<=tc;i++){
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
template <typename INint>
inline void IN(INint &x) {
x = 0;
int f = 1;
char c;
cin.get(c);
while (c < '0' || c > '9') {
if (c == '-') f = -1;
cin.get(c);
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
cin.get(c);
}
x *= f;
}
template <typename INint>
inline void OUT(INint x) {
if (x > 9) OUT(x / 10);
cout.put(x % 10 + '0');
}
const int maxn = int(1);
map<long long, long long> ma, ol;
long long gcd(long long a, long long b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
long long isn(long long x, long long n) {
long long le(0), ri(pow(2e18 + 1e8, 1. / n));
while (le + 1 != ri) {
long long mid = ((le + ri) >> 1);
long long res(1);
for (int i = 0; i < int(n); i++) res *= mid;
if (res == x)
return mid;
else if (res > x)
ri = mid;
else
le = mid;
}
return 0;
}
long long a[505];
int n;
long long cnt(0);
void deal(long long x, long long pos) {
if (x == 1) return;
long long res = isn(x, 3);
if (res) {
ma[res] += 3;
return;
}
res = isn(x, 2);
if (res) {
long long res2 = isn(res, 2);
if (res2)
ma[res2] += 4;
else
ma[res] += 2;
return;
}
long long g;
int i;
for (i = 1; i <= n; ++i)
if (i != pos) {
res = gcd(a[i], x);
if (res == 1) continue;
if (res == x)
g = res;
else {
++ma[res];
++ma[x / res];
break;
}
}
if (i == n + 1) ++ol[x];
}
int main() {
int T(1), times(0);
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while (++times, T--) {
cin >> n;
for (int i = 1; i <= int(n); i++) {
cin >> a[i];
}
for (int i = 1; i <= int(n); i++) deal(a[i], i);
long long ans(1);
for (auto x : ma) ans *= x.second + 1, ans %= 998244353;
for (auto x : ol) {
ans *= x.second + 1;
ans %= 998244353;
ans *= x.second + 1;
ans %= 998244353;
}
cout << ans << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char mat[9][9];
int checkColumnWhite(int x, int y) {
int i;
for (i = x - 1; i >= 1; i--) {
if (mat[i][y] != '.') {
return 8;
}
}
return x - 1;
}
int checkColumnBlack(int x, int y) {
int i;
for (i = x + 1; i <= 8; i++) {
if (mat[i][y] != '.') {
return 8;
}
}
return 8 - x;
}
int main(void) {
int i, j;
int playerA = 8;
int playerB = 8;
for (i = 1; i <= 8; i++) {
for (j = 1; j <= 8; j++) {
scanf("%c", mat[i] + j);
}
scanf("%*c");
}
for (i = 1; i <= 8; i++) {
for (j = 1; j <= 8; j++) {
if (mat[i][j] == 'W') {
playerA = min(playerA, checkColumnWhite(i, j));
} else if (mat[i][j] == 'B') {
playerB = min(playerB, checkColumnBlack(i, j));
}
}
}
if (playerA <= playerB) {
printf("A\n");
} else {
printf("B\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const int INF = (int)1e9 + 7;
const int MAXN = (int)2e6 + 7;
long long n;
int main() {
int h, m;
char ch;
cin >> h >> ch >> m;
int t1 = h * 60 + m;
int add;
cin >> add;
t1 += add;
t1 %= 24 * 60;
printf("%02d:%02d", t1 / 60, t1 % 60);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int n;
int a[N];
string gen() {
string ret;
for (int i = 1; i <= n; i++) ret += string(a[i], 'a' + i - 1);
return ret;
}
int main() {
cin >> n;
int g = 0, odd = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
g = gcd(a[i], g);
if (a[i] & 1) odd++;
}
if (odd >= 2) {
cout << 0 << '\n' << gen();
return 0;
}
cout << g << '\n';
string ans;
char ch;
for (int i = 1; i <= n; i++) {
a[i] /= g;
if (odd == 1 && (a[i] & 1)) {
a[i]--;
ch = 'a' - 1 + i;
}
}
if (odd == 1)
for (int i = 1; i <= n; i++) a[i] /= 2;
string s = gen();
string r = s;
reverse(r.begin(), r.end());
for (int i = 1; i <= g; i++) {
ans += s + r;
if (odd == 1)
ans.push_back(ch);
else
i++;
}
cout << ans;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int LIM = (int)4e6;
int n;
vector<int> cnt(LIM + 1, 0), lp(LIM + 1, 0);
vector<int> a;
void doit(int j) {
while (j > 1) {
int x = lp[j];
assert(x);
if (!cnt[x]) {
for (int f = x; f <= LIM; f += x) {
cnt[f] = 1;
}
}
j /= x;
}
}
int main() {
scanf("%d", &n);
for (int i = 2; i <= LIM; ++i) {
if (!lp[i]) {
lp[i] = i;
for (int j = i + i; j <= LIM; j += i) {
if (!lp[j]) {
lp[j] = i;
}
}
}
}
a.resize(n + 1);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
int stop = n + 1;
for (int i = 1; i <= n; ++i) {
if (cnt[a[i]] == 0) {
int j = a[i];
doit(j);
} else {
stop = i;
break;
}
}
if (stop <= n) {
for (int i = a[stop] + 1;; ++i) {
if (!cnt[i]) {
a[stop] = i;
doit(i);
break;
}
}
}
int ptr = 2;
for (int i = stop + 1; i <= n; ++i) {
while (cnt[ptr]) {
++ptr;
}
doit(ptr);
a[i] = ptr;
}
for (int i = 1; i <= n; ++i) {
printf("%d ", a[i]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int N;
long long A[114514];
int main() {
scanf("%d", &N);
for (int(i) = 0; (i) < (N); (i)++) scanf("%I64d", &A[i]);
long long res = 0;
int w = 1 << 17;
for (int(i) = 0; (i) < (N - 1); (i)++) {
while ((N - 1 - i) < w) w /= 2;
res += A[i];
printf("%I64d\n", res);
A[i + w] += A[i];
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int NN = 305000;
long long power(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1)
--b, res = (res * a) % 1000000007;
else
b /= 2, a = (a * a) % 1000000007;
}
return res;
}
long long inverse(long long a, long long b) {
return (a * power(b, 1000000007 - 2)) % 1000000007;
}
long long n, m, k, f[NN], ans = 0;
int main() {
cin >> n >> m >> k;
if (!n) {
if (m == 1) {
if (k == 1)
cout << 1;
else
cout << 0;
return 0;
}
if (k == 0)
cout << 1;
else
cout << 0;
return 0;
}
if (!m) {
if (n % 2 != k)
cout << 1;
else
cout << 0;
return 0;
}
if (m == 1) {
for (int i = 0; i < n; i++) {
if (i % 2 == k) ++ans;
}
if (n % 2 != k) ++ans;
cout << ans;
return 0;
}
f[0] = 1;
for (int i = 1; i <= n + m; i++) f[i] = (f[i - 1] * i) % 1000000007;
for (int i = 0; i <= n; i++) {
if (i % 2 != k) continue;
ans = (ans + inverse(inverse(f[n + m - i - 1], f[n - i]), f[m - 1])) %
1000000007;
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int cont;
vector<vector<int> > adj, adj2;
vector<int> vis, dfsnum, dfslow, dualdfsnum, comp;
vector<pair<int, int> > bridgeslist;
queue<pair<int, int> > qu;
set<pair<int, int> > st;
int bridges(int cur, int pater) {
vis[cur] = 1;
dfsnum[cur] = dfslow[cur] = ++cont;
dualdfsnum[cont] = cur;
for (int i = 0; i < adj[cur].size(); i++) {
if (adj[cur][i] == pater) continue;
if (!vis[adj[cur][i]]) {
dfslow[cur] = min(dfslow[cur], bridges(adj[cur][i], cur));
if (dfslow[adj[cur][i]] > dfsnum[cur])
bridgeslist.push_back(pair<int, int>(adj[cur][i], cur));
} else
dfslow[cur] = min(dfslow[cur], dfsnum[adj[cur][i]]);
}
return dfslow[cur];
}
int main() {
if (fopen("input.txt", "r")) freopen("input.txt", "r", stdin);
int n, m, x, y;
scanf("%d %d ", &n, &m);
adj.assign(n + 1, vector<int>());
for (int i = 0; i < m; i++) {
scanf("%d %d ", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
vis.assign(n + 1, 0);
dfsnum.assign(n + 1, 0);
dfslow.assign(n + 1, 0);
dualdfsnum.assign(n + 1, 0);
cont = 0;
bridges(1, -1);
for (int i = 0; i < bridgeslist.size(); i++) {
st.insert(pair<int, int>(bridgeslist[i].first, bridgeslist[i].second));
st.insert(pair<int, int>(bridgeslist[i].second, bridgeslist[i].first));
}
comp.assign(n + 1, 0);
vis.assign(n + 1, 0);
for (int i = 1; i < n + 1; i++) {
if (comp[i]) continue;
qu.push(pair<int, int>(i, i));
while (!qu.empty()) {
pair<int, int> u = qu.front();
qu.pop();
if (vis[u.first]) continue;
vis[u.first] = 1;
comp[u.first] = u.second;
for (int j = 0; j < adj[u.first].size(); j++)
if (!vis[adj[u.first][j]] &&
st.find(pair<int, int>(u.first, adj[u.first][j])) == st.end())
qu.push(pair<int, int>(adj[u.first][j], u.second));
}
}
adj2.assign(n + 1, vector<int>());
for (int i = 0; i < bridgeslist.size(); i++) {
x = comp[bridgeslist[i].first];
y = comp[bridgeslist[i].second];
adj2[x].push_back(y);
adj2[y].push_back(x);
}
int last = 1;
qu.push(pair<int, int>(1, 0));
vis.assign(n + 1, 0);
while (!qu.empty()) {
pair<int, int> u = qu.front();
qu.pop();
if (vis[u.first]) continue;
vis[u.first] = 1;
last = u.first;
for (int i = 0; i < adj2[u.first].size(); i++)
if (!vis[adj2[u.first][i]]) qu.push(pair<int, int>(adj2[u.first][i], 0));
}
qu.push(pair<int, int>(last, 0));
vis.assign(n + 1, -1);
while (!qu.empty()) {
pair<int, int> u = qu.front();
qu.pop();
if (vis[u.first] != -1) continue;
vis[u.first] = u.second;
last = u.first;
for (int i = 0; i < adj2[u.first].size(); i++)
if (vis[adj2[u.first][i]] == -1)
qu.push(pair<int, int>(adj2[u.first][i], u.second + 1));
}
printf("%d", vis[last]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
cout << (n / 2) * (n - n / 2) << endl;
for (int i = 1; i <= n / 2; i++) {
for (int j = n / 2 + 1; j <= n; j++) {
printf("%d %d\n", i, j);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int x, y, m;
int main() {
cin >> x >> y;
if (y > x && x >= -y) m = -2 + y * 4;
if (y < x && x <= -y + 1) m = -y * 4;
if (y <= x && x > -y + 1) m = -3 + x * 4;
if (y >= x && x < -y) m = -1 - 4 * x;
cout << m << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
void dj(int n);
const int V = 99999999;
typedef struct pp {
int x;
int y;
} ss;
using namespace std;
int flag[500];
int jj[500][500];
int d[500];
int main(void) {
int n, i, j, k, p, q, N, M;
while (scanf("%d %d", &N, &M) != EOF) {
memset(flag, 0, sizeof(flag));
for (i = 0; i < 450; i++) {
for (j = 0; j < 450; j++) {
jj[i][j] = V;
}
}
for (i = 0; i < M; i++) {
scanf("%d %d", &p, &q);
jj[p][q] = 1;
jj[q][p] = 1;
}
dj(N);
int ss = d[N];
for (i = 0; i < 450; i++) {
for (j = 0; j < 450; j++) {
if (jj[i][j] == V) {
jj[i][j] = 1;
} else
jj[i][j] = V;
}
}
dj(N);
int vv = d[N];
int uu = max(vv, ss);
if (uu >= V) {
printf("-1\n");
} else
printf("%d\n", uu);
}
return 0;
}
void dj(int n) {
fill(d, d + n + 1, V);
fill(flag, flag + n + 1, 0);
d[1] = 0;
int i, j, k, p, q;
while (true) {
int l = -1;
for (i = 1; i <= n; i++) {
if (flag[i] == 0 && (l == -1 || d[i] < d[l])) {
l = i;
}
}
if (l == -1) {
break;
}
flag[l] = 1;
for (i = 1; i <= n; i++) {
d[i] = min(d[i], d[l] + jj[l][i]);
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
bool Max(T1& a, T2 b) {
return a < b ? a = b, 1 : 0;
}
template <typename T1, typename T2>
bool Min(T1& a, T2 b) {
return a > b ? a = b, 1 : 0;
}
const int N = 1000001;
bool prime[N];
vector<long long> pri;
vector<pair<long long, long long> > nv, sv;
long long n, m, s;
long long rec(long long i, long long prod) {
if (i == sv.size()) return 1;
long long ret = 0;
auto cur = sv[i];
for (int j = 0; j < (cur.second + 1); ++j) {
ret += rec(i + 1, prod);
if ((prod *= cur.first) > n) break;
}
return ret;
}
long long pow(long long a, long long p) {
long long ret = 1;
while (p) {
if (p & 1) ret *= a;
a *= a;
p >>= 1;
}
return ret;
}
void solve() {
long long v;
n = 1;
for (int i = 0; i < (3); ++i) {
cin >> v;
n *= v;
}
m = 1;
for (int i = 0; i < (3); ++i) {
cin >> v;
m *= v;
}
s = 2;
for (int i = 0; i < (3); ++i) {
cin >> v;
s *= v;
}
long long ts = s, tn = n;
nv.clear();
sv.clear();
for (auto& e : pri) {
if (e * e > ts) break;
long long cnt = 0;
while (ts % e == 0) {
cnt++;
ts /= e;
}
if (cnt) sv.emplace_back(e, cnt);
}
if (ts > 1) sv.emplace_back(ts, 1);
for (auto& e : pri) {
if (e * e > tn) break;
long long cnt = 0;
while (tn % e == 0) {
cnt++;
tn /= e;
}
if (cnt) nv.emplace_back(e, cnt);
}
if (tn > 1) nv.emplace_back(tn, 1);
long long ans = rec(0, 1);
sv.emplace_back(N, 1);
vector<long long> tmp;
long long i = 0;
for (auto& e : nv) {
while (sv[i].first < e.first) i++;
if (sv[i].first > e.first) {
tmp.emplace_back(e.first);
} else {
if (sv[i].second < e.second)
tmp.emplace_back(pow(e.first, sv[i].second + 1));
i++;
}
}
long long sz = tmp.size();
for (int sub = 0; sub < (1 << sz); ++sub) {
long long t = 1;
bool add = 1;
for (int i = 0; i < (sz); ++i)
if ((sub >> i) & 1) {
t *= tmp[i];
add ^= 1;
}
if (add)
ans += m / t;
else
ans -= m / t;
}
cout << ans << '\n';
}
void init() {
for (int i = 2; i <= (N - 1); ++i)
if (!prime[i]) {
pri.emplace_back(i);
for (long long j = (long long)i * i; j < N; j += i) prime[j] = 1;
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cout.setf(ios::fixed);
cout.precision(20);
init();
int TC;
cin >> TC;
for (int TI = 1; TI <= (TC); ++TI) {
solve();
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
char str[200010];
int main() {
int n, k, l = 0, r = 0;
scanf("%d%d", &n, &k);
scanf("%s", str + 1);
for (int i = 1; i <= n; ++i) {
if (str[i] == '(')
++l;
else
++r;
putchar(str[i]);
if (l == k / 2) break;
}
for (int i = 1, rest = (k / 2 - r); i <= rest; ++i) putchar(')');
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int long a[1000000];
int long long n;
bool tudormentiune = false;
int long long sum1;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
sum1 = sum1 + a[i];
if (a[i] % 2 == 1) {
tudormentiune = true;
}
}
if (sum1 % 2 == 1) {
cout << "First" << endl;
}
if (sum1 % 2 == 0 && tudormentiune == false) {
cout << "Second" << endl;
}
if (sum1 % 2 == 0 && tudormentiune == true) {
cout << "First" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int x, y, x1, y1;
cin >> x >> y;
cin >> x1 >> y1;
if ((x == x1 && y + y1 == x) || (x == y1 && x1 + y == x) ||
(x1 == y && x + y1 == x1) || (y1 == y && x + x1 == y))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, k, x, s, i;
cin >> n >> m >> k >> x >> s;
unsigned long long mini = n * x;
vector<int> a(m), b(m), c(k + 1), d(k + 1);
for (i = 0; i < m; ++i) cin >> a[i];
for (i = 0; i < m; ++i) cin >> b[i];
for (i = 1; i < k + 1; ++i) cin >> c[i];
for (i = 1; i < k + 1; ++i) cin >> d[i];
a.push_back(x);
b.push_back(0);
c[0] = 0;
d[0] = 0;
for (i = 0; i <= m; ++i) {
long long q = s - b[i];
long long L = 0, R = k + 1;
while (R - L > 1) {
long long mid = (L + R) / 2;
if (q < d[mid])
R = mid;
else
L = mid;
}
if (max((long long)0, n - c[L]) * a[i] < mini && s >= b[i] + d[L])
mini = max((long long)0, n - c[L]) * a[i];
}
cout << mini;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> fact;
void precalc(long long n) {
fact.resize(n / 2 + 1);
fact[0] = 1;
for (long long i = 1; i < fact.size(); ++i) {
fact[i] = fact[i - 1] * i % 1000000007;
}
}
long long binpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) {
res = res * a % 1000000007;
}
a = a * a % 1000000007;
b >>= 1;
}
return res;
}
long long divis(long long a, long long b) {
return a * binpow(b, 1000000007 - 2) % 1000000007;
}
long long fun(long long a, long long b) {
return divis(divis(fact[a], fact[b]), fact[a - b]);
}
void solve() {
long long Q;
Q = 1;
while (Q--) {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; ++i) cin >> a[i];
;
precalc(n);
vector<long long> b(n);
if (n % 4 == 0) {
for (long long i = 0, cnt = 1; i < n; ++i, cnt = -cnt) {
b[i] = fun((n - 2) / 2, i / 2) * cnt;
}
} else if (n % 4 == 1) {
for (long long i = 0; i < n; ++i) {
if (i % 2 == 0) {
b[i] = fun((n - 1) / 2, i / 2);
} else {
b[i] = 0;
}
}
} else if (n % 4 == 2) {
for (long long i = 0; i < n; ++i) {
b[i] = fun((n - 2) / 2, i / 2);
}
} else {
b[0] = 1;
b[n - 1] = 1000000007 - 1;
for (long long i = 1; i < n - 1; ++i) {
if (i % 2 == 0) {
b[i] = (fun((n - 3) / 2, i / 2) - fun((n - 3) / 2, i / 2 - 1) +
1000000007) %
1000000007;
} else {
b[i] = 2 * fun((n - 3) / 2, i / 2) % 1000000007;
}
}
}
long long res = 0;
for (long long i = 0; i < n; ++i) {
res = (res + a[i] * b[i]) % 1000000007;
}
cout << (res + 1000000007) % 1000000007;
}
}
signed main(signed argc, char **argv) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (argc > 1 && (string)argv[1] == "local") {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
solve();
while (cin.peek() != EOF) {
if (isspace(cin.peek()))
cin.get();
else {
cout << '\n';
solve();
}
}
} else {
solve();
}
}
| 7 |
#include <bits/stdc++.h>
const double pi = 3.1415926535897932384626433832795;
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
using namespace std;
void ifd() {}
void tme() {}
int main() {
ifd();
int t;
long double ans, a, b;
cin >> t;
while (t--) {
cin >> a >> b;
if (b == 0)
ans = 1;
else {
if (a > 4 * b)
ans = 1 - b / a;
else
ans = 0.5 + a / (16.0 * b);
}
printf("%.10lf\n", ans);
}
tme();
return 0;
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.