solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 3;
int main() {
int n, k;
cin >> n >> k;
long long ans = 0;
int cnt = 2 * n - 3;
for (int i = 0; i < min(k, n / 2); i++) {
ans += cnt;
cnt -= 4;
}
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct cliente {
long long orden;
long long M;
};
bool compara(cliente A, cliente B) { return (A.M < B.M); }
int main(void) {
int n;
long long d;
int a, b, x, y;
scanf("%d %lli", &n, &d);
cliente cli[n];
int pija[n];
scanf("%d %d", &a, &b);
for (int i = 0; i < n; i++) {
scanf("%d %d", &x, &y);
cli[i].M = a * x + b * y;
cli[i].orden = i + 1;
}
sort(cli, cli + n, compara);
int i = 0;
while (i < n && d >= cli[i].M) {
d = d - cli[i].M;
i = i + 1;
}
printf("%d\n", i);
if (i > 0) {
for (int j = 0; j < i - 1; j++) {
printf("%lld ", cli[j].orden);
}
printf("%lld\n", cli[i - 1].orden);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int32_t Mod = 1e9 + 7;
const int mod = pow(10, 9) + 7;
bool compare(pair<int, int> p1, pair<int, int> p2) {
return p1.second < p2.first;
}
void open_file() {}
void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int pr(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
void solve() {
int n;
cin >> n;
if (pr(n))
cout << 1 << "\n";
else
cout << (n & 1 and !pr(n - 2) ? "3" : "2") << "\n";
}
int main() {
open_file();
fast_io();
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long d, n, m;
pair<long long, long long> v[200005];
int main() {
scanf("%I64d %I64d %I64d", &d, &n, &m);
for (int i = 0; i < m; i++) scanf("%I64d %I64d", &v[i].first, &v[i].second);
sort(v, v + m);
priority_queue<pair<long long, long long> > inside_range, outside_range;
for (int i = 0; i < m; i++)
outside_range.push(make_pair(-v[i].first, v[i].second));
long long position = 0, fuel = n, total = 0;
while (true) {
if (position + fuel >= d) {
printf("%I64d\n", total);
return 0;
}
while (!outside_range.empty()) {
pair<long long, long long> t = outside_range.top();
if (-t.first <= position + fuel) {
inside_range.push(make_pair(-t.second, -t.first));
outside_range.pop();
} else
break;
}
if (inside_range.empty()) break;
while (!inside_range.empty()) {
pair<long long, long long> t = inside_range.top();
if (t.second < position) {
inside_range.pop();
continue;
}
if (t.second > position) {
fuel -= t.second - position;
position = t.second;
continue;
}
if (t.second == position) {
if (fuel == n) {
inside_range.pop();
continue;
} else {
if (outside_range.empty()) {
total += min(n - fuel, d - (position + fuel)) * (-t.first);
fuel += min(n - fuel, d - (position + fuel));
break;
} else {
pair<long long, long long> t2 = outside_range.top();
long long c = min(n - fuel, (-t2.first) - (position + fuel));
total += c * (-t.first);
fuel += c;
break;
}
}
}
}
}
printf("-1\n");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, c = 0;
scanf("%d%d", &n, &m);
while (n--) {
int a;
scanf("%d", &a);
c += a;
printf("%d ", c / m);
c %= m;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string str;
cin >> str;
bool flag = false;
for (int i = 0; i < str.size(); i++) {
if (str[i] == 'H' || str[i] == 'Q' || str[i] == '9') {
flag = true;
break;
}
}
if (flag)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[510000], color, ans;
void solve() {
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> blogs;
map<int, int> them;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 0; i < n; i++) {
int theme;
color.push_back(false);
cin >> theme;
them[i] = theme;
blogs.push_back({theme, i});
}
sort(blogs.begin(), blogs.end());
for (auto blog : blogs) {
vector<int> neigbours;
int v = blog.second;
int theme = blog.first;
for (auto sosed : g[v]) {
if (!color[sosed]) {
continue;
}
neigbours.push_back(them[sosed]);
}
sort(neigbours.begin(), neigbours.end());
int minn_empty = -1;
for (int i = 0; i < neigbours.size() - 1 && neigbours.size() > 0; i++) {
if (i == 0 && neigbours[i] > 1) {
minn_empty = 1;
}
if (neigbours[i + 1] - neigbours[i] > 1 && minn_empty == -1) {
minn_empty = neigbours[i] + 1;
}
}
if (neigbours.size() == 0) {
minn_empty = 1;
}
if (minn_empty == -1) {
minn_empty = neigbours[neigbours.size() - 1] + 1;
}
if (minn_empty != theme) {
cout << "-1\n";
return;
}
ans.push_back(v);
color[v] = true;
}
for (auto v : ans) {
cout << v + 1 << " ";
}
}
int main() {
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 25, S = (1 << 22) + 10;
int g[N], conn[S];
int main() {
int n, m;
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
g[u] |= (1 << (v - 1));
g[v] |= (1 << (u - 1));
}
int subsets = (1 << n);
int need = 0;
for (int i = 1; i <= n; ++i) {
g[i] |= (1 << (i - 1));
if (g[i] != subsets - 1) {
need |= (1 << (i - 1));
}
}
if (need == 0) {
cout << 0;
return 0;
} else {
conn[0] = true;
}
int best = n, best_mask = subsets - 1;
for (int i = 1; i < subsets; ++i) {
int ctr = __builtin_popcount(i);
if (ctr >= best) {
continue;
}
int covered = 0;
for (int j = 1; j <= n; ++j) {
if (i & (1 << (j - 1))) {
covered |= g[j];
}
}
if (ctr == 1) {
conn[i] = true;
} else {
for (int j = 1; j <= n; ++j) {
if (i & (1 << (j - 1))) {
int temp = i - (1 << (j - 1));
if (conn[temp] and (g[j] & temp) > 0) {
conn[i] = true;
break;
}
}
}
}
if (conn[i] and (covered | need) == covered) {
best = ctr;
best_mask = i;
}
}
cout << best << endl;
for (int i = 1; i <= n; ++i) {
if (best_mask & (1 << (i - 1))) {
cout << i << ' ';
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int A, B, C;
int a[205], b[205], c[205];
int dp[201][201][201];
inline void chmax(int &aa, int xx) {
if (xx > aa) aa = xx;
}
int main() {
ios::sync_with_stdio(false);
cin >> A >> B >> C;
for (int i = 1; i <= A; ++i) cin >> a[i];
for (int i = 1; i <= B; ++i) cin >> b[i];
for (int i = 1; i <= C; ++i) cin >> c[i];
sort(a + 1, a + A + 1);
reverse(a + 1, a + A + 1);
sort(b + 1, b + B + 1);
reverse(b + 1, b + B + 1);
sort(c + 1, c + C + 1);
reverse(c + 1, c + C + 1);
int ans = 0;
for (int i = 0; i < A + 1; ++i)
for (int j = 0; j < B + 1; ++j)
for (int k = 0; k < C + 1; ++k) {
if (i < A && j < B)
chmax(dp[i + 1][j + 1][k], dp[i][j][k] + a[i + 1] * b[j + 1]);
if (j < B && k < C)
chmax(dp[i][j + 1][k + 1], dp[i][j][k] + b[j + 1] * c[k + 1]);
if (k < C && i < A)
chmax(dp[i + 1][j][k + 1], dp[i][j][k] + c[k + 1] * a[i + 1]);
ans = max(ans, dp[i][j][k]);
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
int n, m, t;
int main() {
scanf("%d%d", &n, &m);
if (m == 3) {
if (n == 3) {
printf("0 0\n0 1\n1 0\n");
return 0;
}
if (n == 4) {
printf("0 0\n0 10\n10 0\n1 1\n");
return 0;
}
printf("-1\n");
return 0;
} else {
int c(0);
for (int i = 1; i <= m; i++) printf("%d %d\n", c, c * c), c++;
c = 1;
int lowx = 9999999, lowy = -9999999;
for (int j = 1; j <= n - m; j++)
printf("%d %d\n", lowx - c, lowy + c * c), c++;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool solve(int n, int s) {
long long sum = n * 1ll * (n + 1) / 2;
if (sum < s) return false;
return sum % 2 == s % 2;
}
int main() {
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
int d = abs(a - b);
if (d == 0) {
cout << "0\n";
continue;
}
int cnt = 1;
while (!solve(cnt, d)) cnt++;
cout << cnt << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int mod = 998244353;
const int maxn = 1e5 + 5;
const int inf = 1e9;
const long long onf = 1e18;
using namespace std;
void work() {
int n, t;
scanf("%d%d", &n, &t);
int ans = 0, tim = inf;
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
int temp = x;
while (temp < t) temp += y;
if (temp - t < tim) {
ans = i, tim = temp - t;
}
}
printf("%d\n", ans);
}
int main() {
int t = 1, Case = 1;
while (t--) {
work();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int x1, x2, x3, y, y2, y3;
bool z = 0;
vector<pair<int, pair<int, char> > > x;
void check() {
if (x2 == x3 && y2 + y3 == x1 && x2 + y == x1) {
z = 1;
cout << x1 << endl;
for (int i = 1; i <= x1; i++) {
for (int j = 1; j <= x1; j++) {
if (i > y) {
if (j <= y2) {
cout << x[1].second.second;
} else {
cout << x[2].second.second;
}
} else
cout << x[0].second.second;
}
cout << endl;
}
}
}
int main() {
cin >> x1 >> y >> x2 >> y2 >> x3 >> y3;
if (x1 < y) {
swap(x1, y);
}
if (x2 < y2) {
swap(x2, y2);
}
if (x3 < y3) {
swap(x3, y3);
}
x.push_back(make_pair(x1, make_pair(y, 'A')));
x.push_back(make_pair(x2, make_pair(y2, 'B')));
x.push_back(make_pair(x3, make_pair(y3, 'C')));
sort(x.begin(), x.end());
swap(x[0], x[2]);
x1 = x[0].first;
y = x[0].second.first;
x2 = x[1].first;
y2 = x[1].second.first;
x3 = x[2].first;
y3 = x[2].second.first;
if (x1 == x2 && x2 == x3) {
if (x1 == y + y2 + y3) {
cout << x1 << " ";
for (int i = 1; i <= x1; i++) {
for (int j = 1; j <= x1; j++) {
if (i <= y) {
cout << x[0].second.second;
} else if (i <= y2 + y) {
cout << x[1].second.second;
} else
cout << x[2].second.second;
}
cout << endl;
}
return 0;
}
}
check();
if (!z) {
swap(x2, y2);
check();
}
if (!z) {
swap(x3, y3);
check();
}
if (!z) {
swap(x2, y2);
check();
}
if (!z) {
cout << -1;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = LLONG_MAX;
const long double PI = 3.141592653589793238463;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long T;
cin >> T;
while (T--) {
long long n;
cin >> n;
cout << 2 << "\n";
long long a = n;
for (long long i = n - 1; i >= (long long)1; i--) {
long long b = i;
cout << a << " " << b << "\n";
if ((a + b) % 2 == 1)
a = (a + b) / 2 + 1;
else
a = (a + b) / 2;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, t, i, j, k, a[7], res[7];
long long b[6][6] = {2, 1, 1, 1, 0, 0, 1, 2, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0,
0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1};
long long c[6][6], d[6][6];
void m(long long A[][6], long long B[][6]) {
for (long long i = 0; i < 6; i++)
for (long long j = 0; j < 6; j++) {
c[i][j] = 0;
for (long long r = 0; r < 6; r++)
c[i][j] = (c[i][j] + A[i][r] * B[r][j]) % n;
}
}
void p(long long t) {
if (!t) return;
p(t / 2);
for (long long i = 0; i < 6; i++)
for (long long j = 0; j < 6; j++) d[i][j] = c[i][j];
m(d, d);
if (t % 2) {
for (long long i = 0; i < 6; i++)
for (long long j = 0; j < 6; j++) d[i][j] = c[i][j];
m(d, b);
}
}
int main() {
for (i = 0; i < 6; i++) c[i][i] = 1;
a[5] = 1;
cin >> n >> a[0] >> a[1] >> a[2] >> a[3] >> t;
p(t);
for (long long i = 0; i < 6; i++)
for (long long k = 0; k < 6; k++) res[k] += (a[i] + n) * c[i][k];
cout << (res[0] + n - 1) % n + 1 << " " << (res[1] + n - 1) % n + 1 << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int M = 2e2 + 5;
const int inf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3f;
long long n, k;
char a[M];
long long dp[M][M];
long long pre[M];
long long tmp, ans;
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
cin >> a + 1;
dp[0][0] = 1;
memset(pre, -1, sizeof pre);
for (int i = 1; i <= n; i++) {
dp[i][0] = 1;
for (int j = 1; j <= i; j++) {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
if (pre[a[i] - 'a'] != -1 && j >= i - pre[a[i] - 'a']) {
dp[i][j] -= dp[pre[a[i] - 'a'] - 1][j - (i - pre[a[i] - 'a'])];
}
}
pre[a[i] - 'a'] = i;
}
tmp = ans = 0;
for (int i = 0; i <= n; i++) {
if (tmp + dp[n][i] >= k) {
ans += min(dp[n][i], k - tmp) * i;
tmp = k;
break;
} else {
ans += dp[n][i] * i;
tmp += dp[n][i];
}
}
if (tmp < k) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long m, n;
cin >> m >> n;
vector<vector<long long>> arr(m, vector<long long>(n));
vector<long long> adj(n, -1);
for (long long i = 0; i < m; i++) {
for (long long j = 0; j < n; j++) {
cin >> arr[i][j];
adj[j] = max(adj[j], arr[i][j]);
}
}
long long a = INT_MAX;
for (long long i = 0; i < n; i++) {
a = min(a, adj[i]);
}
if (m <= n - 1) {
cout << a;
return;
}
long long x = -1;
for (long long i = 0; i < m; i++) {
sort(arr[i].begin(), arr[i].end());
x = max(x, arr[i][n - 2]);
}
cout << min(x, a);
return;
}
int32_t main() {
long long t;
cin >> t;
while (t--) {
solve();
cout << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class t, class u>
void chmax(t& first, u second) {
if (first < second) first = second;
}
template <class t, class u>
void chmin(t& first, u second) {
if (second < first) first = second;
}
template <class t>
using vc = vector<t>;
template <class t>
using vvc = vc<vc<t>>;
using pi = pair<ll, ll>;
using vi = vc<ll>;
template <class t, class u>
ostream& operator<<(ostream& os, const pair<t, u>& p) {
return os << "{" << p.first << "," << p.second << "}";
}
template <class t>
ostream& operator<<(ostream& os, const vc<t>& v) {
os << "{";
for (auto e : v) os << e << ",";
return os << "}";
}
using uint = unsigned;
using ull = unsigned long long;
template <class t, size_t n>
ostream& operator<<(ostream& os, const array<t, n>& first) {
return os << vc<t>(first.begin(), first.end());
}
template <ll i, class T>
void print_tuple(ostream&, const T&) {}
template <ll i, class T, class H, class... Args>
void print_tuple(ostream& os, const T& t) {
if (i) os << ",";
os << get<i>(t);
print_tuple<i + 1, T, Args...>(os, t);
}
template <class... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
os << "{";
print_tuple<0, tuple<Args...>, Args...>(os, t);
return os << "}";
}
template <class t>
void print(t x, ll suc = 1) {
cout << x;
if (suc == 1) cout << "\n";
if (suc == 2) cout << " ";
}
ll read() {
ll i;
cin >> i;
return i;
}
vi readvi(ll n, ll off = 0) {
vi v(n);
for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off;
return v;
}
pi readpi(ll off = 0) {
ll first, second;
cin >> first >> second;
return pi(first + off, second + off);
}
template <class T>
void print(const vector<T>& v, ll suc = 1) {
for (ll i = ll(0); i < ll(v.size()); i++)
print(v[i], i == ll(v.size()) - 1 ? suc : 2);
}
string readString() {
string s;
cin >> s;
return s;
}
template <class T>
T sq(const T& t) {
return t * t;
}
void yes(bool ex = true) {
cout << "Yes"
<< "\n";
if (ex) exit(0);
}
void no(bool ex = true) {
cout << "No"
<< "\n";
if (ex) exit(0);
}
void possible(bool ex = true) {
cout << "Possible"
<< "\n";
if (ex) exit(0);
}
void impossible(bool ex = true) {
cout << "Impossible"
<< "\n";
if (ex) exit(0);
}
constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; }
const ll infLL = LLONG_MAX / 3;
const ll inf = infLL;
ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); }
ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); }
ll popcount(signed t) { return __builtin_popcount(t); }
ll popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(ll i) { return i && (i & -i) == i; }
ll mask(ll i) { return (ll(1) << i) - 1; }
bool inc(ll first, ll second, ll c) { return first <= second && second <= c; }
template <class t>
void mkuni(vc<t>& v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
ll rand_int(ll l, ll r) {
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<ll>(l, r)(gen);
}
template <class t>
void myshuffle(vc<t>& first) {
for (ll i = ll(0); i < ll(ll(first.size())); i++)
swap(first[i], first[rand_int(0, i)]);
}
template <class t>
ll lwb(const vc<t>& v, const t& first) {
return lower_bound(v.begin(), v.end(), first) - v.begin();
}
vvc<ll> readGraph(ll n, ll m) {
vvc<ll> g(n);
for (ll i = ll(0); i < ll(m); i++) {
ll first, second;
cin >> first >> second;
first--;
second--;
g[first].push_back(second);
g[second].push_back(first);
}
return g;
}
vvc<ll> readTree(ll n) { return readGraph(n, n - 1); }
struct modinfo {
uint mod, root;
};
template <modinfo const& ref>
struct modular {
static constexpr uint const& mod = ref.mod;
static modular root() { return modular(ref.root); }
uint v;
modular(ll vv = 0) { s(vv % mod + mod); }
modular& s(uint vv) {
v = vv < mod ? vv : vv - mod;
return *this;
}
modular operator-() const { return modular() - *this; }
modular& operator+=(const modular& rhs) { return s(v + rhs.v); }
modular& operator-=(const modular& rhs) { return s(v + mod - rhs.v); }
modular& operator*=(const modular& rhs) {
v = ull(v) * rhs.v % mod;
return *this;
}
modular& operator/=(const modular& rhs) { return *this *= rhs.inv(); }
modular operator+(const modular& rhs) const { return modular(*this) += rhs; }
modular operator-(const modular& rhs) const { return modular(*this) -= rhs; }
modular operator*(const modular& rhs) const { return modular(*this) *= rhs; }
modular operator/(const modular& rhs) const { return modular(*this) /= rhs; }
modular pow(ll n) const {
modular res(1), x(*this);
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
modular inv() const { return pow(mod - 2); }
friend modular operator+(ll x, const modular& y) { return modular(x) + y; }
friend modular operator-(ll x, const modular& y) { return modular(x) - y; }
friend modular operator*(ll x, const modular& y) { return modular(x) * y; }
friend modular operator/(ll x, const modular& y) { return modular(x) / y; }
friend ostream& operator<<(ostream& os, const modular& m) {
return os << m.v;
}
friend istream& operator>>(istream& is, modular& m) {
ll x;
is >> x;
m = modular(x);
return is;
}
bool operator<(const modular& r) const { return v < r.v; }
bool operator==(const modular& r) const { return v == r.v; }
bool operator!=(const modular& r) const { return v != r.v; }
explicit operator bool() const { return v; }
};
modinfo base{1, 0};
using mint = modular<base>;
const ll vmax = 10010;
mint fact[vmax], finv[vmax], invs[vmax];
void initfact() {
fact[0] = 1;
for (ll i = ll(1); i < ll(vmax); i++) {
fact[i] = fact[i - 1] * i;
}
finv[vmax - 1] = fact[vmax - 1].inv();
for (ll i = vmax - 2; i >= 0; i--) {
finv[i] = finv[i + 1] * (i + 1);
}
for (ll i = vmax - 1; i >= 1; i--) {
invs[i] = finv[i] * fact[i - 1];
}
}
mint choose(ll n, ll k) { return fact[n] * finv[n - k] * finv[k]; }
mint binom(ll first, ll second) {
return fact[first + second] * finv[first] * finv[second];
}
mint catalan(ll n) {
return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0);
}
void slv() {
ll n, m;
cin >> n >> m >> base.mod;
initfact();
mint ans;
for (ll i = ll(1); i < ll(m); i++) {
mint f = mint(i + 1).pow(i) * i / (n - i) * finv[i];
for (ll j = ll(0); j < ll(m - i); j++) {
ll k = i + j;
mint g = mint(n - i).pow(j) * finv[j];
mint h = (n - k) * mint(n + 1).pow(m - 1 - k) * fact[k];
ans += f * g * h;
}
}
ans *= mint(2).pow(m - 1) * (n + 1 - m);
print(ans);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
slv();
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
string str[5] = {"Danil", "Olya", "Slava", "Ann", "Nikita"};
string S;
int main() {
while (cin >> S) {
int num = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < S.size(); j++) {
if (S.substr(j, str[i].size()) == str[i]) num++;
}
}
if (num == 1) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 2000 * 1000 * 1000;
const int MAXN = 100010;
template <typename T>
inline T sqr(T n) {
return n * n;
}
int n, m;
pair<int, int> edge[MAXN];
vector<int> out[MAXN];
int in[MAXN];
bool used[MAXN];
bool dfs(int v) {
used[v] = true;
int nxt = -1;
for (int to : out[v]) {
in[to]--;
if (in[to] == 0) {
if (nxt != -1) {
return false;
}
nxt = to;
}
}
if (nxt == -1) {
return true;
}
return dfs(nxt);
}
bool check(int len) {
for (int i = 1; i <= n; i++) {
out[i].clear();
in[i] = 0;
}
memset(used, false, sizeof(used[0]) * (n + 1));
for (int i = 0; i < len; i++) {
out[edge[i].first].push_back(edge[i].second);
in[edge[i].second]++;
}
for (int i = 1; i <= n; i++) {
if (in[i] == 0) {
if (!dfs(i)) {
return false;
}
break;
}
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
return false;
}
}
return true;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &edge[i].first, &edge[i].second);
}
int l = 0, r = m - 1;
int ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid + 1)) {
ans = mid + 1;
r = mid - 1;
} else {
l = mid + 1;
}
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> z_function(string s) {
int n = (int)s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
int main() {
int n, m, q;
string s, t;
while (cin >> n >> m >> q >> s >> t) {
const auto z = z_function(t + '$' + s);
vector<int> is_occurance(1 + n, 0), cumsum_ocuurances(1 + n);
for (int i = 0; i <= n - m; ++i) {
is_occurance[i + 1] = m == z[m + 1 + i];
}
partial_sum(is_occurance.begin(), is_occurance.end(),
cumsum_ocuurances.begin());
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
cout << (r - l + 1 >= m
? cumsum_ocuurances[r - m + 1] - cumsum_ocuurances[l - 1]
: 0)
<< endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void input(vector<T> &arr, long long int n) {
T temp;
for (long long int i = 0; i < n; i++) cin >> temp, arr.push_back(temp);
}
template <typename T>
void output(vector<T> arr) {
T temp;
for (auto x : arr) cout << x << " ";
cout << endl;
}
template <typename T>
void input_set(set<T> &arr, long long int n) {
T temp;
for (long long int i = 0; i < n; i++) cin >> temp, arr.insert(temp);
}
long long int power(long long int num, long long int base) {
if (base == 0) return 1;
if (base % 2)
return (num * power(num, base - 1));
else {
long long int x = power(num, base / 2);
x = x * x;
return x;
}
}
long long int f(long long int n) {
if (n == 1 || n == 2) {
return 1;
}
if (n == 4) return 3;
if (n % 2 == 0 && (n / 2) % 2 == 0) return n - f(n - 1);
if (n % 2 == 0 && (n / 2) % 2 == 1) return n - f(n / 2);
if (((n - 1) / 2) % 2 == 0 || (n - 1) / 2 == 4) {
return n - f(n - 1);
}
return 1 + f((n - 1) / 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int testcases = 1;
cin >> testcases;
while (testcases--) {
long long int n, a, i, ans = 0;
cin >> n;
cout << f(n) << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, a1, b1, a2, b2;
cin >> t;
while (t--) {
cin >> a1 >> b1;
cin >> a2 >> b2;
if (a1 > b1) swap(a1, b1);
if (a2 > b2) swap(a2, b2);
if (b1 == b2 && a1 + a2 == b1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
const int INF = INT_MAX / 3;
const int MAXLOG = 20;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, d, m, l;
cin >> n >> d >> m >> l;
long long pos = 0;
for (int i = 1; i <= n; i++) {
long long f = (i - 1ll) * m, t = f + l;
if (pos + d < f) {
cout << pos + d;
return 0;
}
pos = t - t % d;
}
cout << pos + d;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void print(int n, int m, int a, int b) {
int ans[n][m];
memset(ans, 0, sizeof(ans));
int jump = 1;
while (jump < m) {
if ((jump * n) % m == 0) break;
jump++;
}
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < a; j++) {
ans[i][(j + k) % m] = 1;
}
k = k + jump;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << ans[i][j];
}
cout << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m, a, b;
cin >> n >> m >> a >> b;
if (n * a == m * b) {
cout << "YES" << endl;
print(n, m, a, b);
} else
cout << "NO" << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = int(1e9);
int n, w, k;
int a[200005];
int t[200005];
struct Cmp {
inline bool operator()(const int &lhs, const int &rhs) const {
if (t[lhs] != t[rhs]) return t[lhs] > t[rhs];
return lhs < rhs;
}
};
struct Cmp2 {
inline bool operator()(const int &lhs, const int &rhs) const {
if (t[lhs] != t[rhs]) return t[lhs] < t[rhs];
return lhs < rhs;
}
};
set<int, Cmp> lo;
set<int, Cmp2> hi;
int main() {
scanf("%d%d%d", &n, &w, &k);
for (int i = 0; i < (int)(n); ++i) {
scanf("%d", a + i);
}
for (int i = 0; i < (int)(n); ++i) {
scanf("%d", t + i);
}
int cur = 0;
int sumT = 0;
int ans = 0;
int j = n - 1;
for (int i = n - 1; i >= 0; --i) {
int mx = (lo.empty() ? -INF : t[*lo.begin()]);
int mn = (hi.empty() ? INF : t[*hi.begin()]);
if (t[i] >= mx && t[i] <= mn) {
if (int(hi.size()) < w) {
hi.insert(i);
sumT += (t[i] + 1) / 2;
} else {
lo.insert(i);
sumT += t[i];
}
} else if (t[i] < mx) {
lo.insert(i);
sumT += t[i];
} else {
hi.insert(i);
sumT += (t[i] + 1) / 2;
}
if (int(hi.size()) > w) {
int val = *hi.begin();
hi.erase(hi.begin());
lo.insert(val);
sumT -= (t[val] + 1) / 2;
sumT += t[val];
}
cur += a[i];
while (sumT > k) {
auto it = lo.find(j);
if (it == lo.end()) {
hi.erase(j);
sumT -= (t[j] + 1) / 2;
if (!lo.empty()) {
int val = *lo.begin();
lo.erase(lo.begin());
hi.insert(val);
sumT -= t[val];
sumT += (t[val] + 1) / 2;
}
} else {
lo.erase(it);
sumT -= t[j];
}
cur -= a[j];
--j;
}
ans = max(ans, cur);
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
k = 240 - k;
int sum = 0, ans = 0;
for (int i = 1; i <= n; i++) {
sum += i * 5;
if (sum > k) break;
ans++;
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char s[1005];
long long ans = 0, x = 0, flag = 1;
int main() {
cin >> s;
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] < '0' || s[i] > '9') {
ans += x * flag;
x = 0;
flag = s[i] == '+' ? 1 : -1;
x = x * 10 + s[i] - '0';
} else
x *= 10, x += s[i] - '0';
}
ans += x * flag;
printf("%d", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
long long n, m, k;
void Main() {
scanf("%lld%lld%lld", &n, &m, &k);
if (n > m) swap(n, m);
if (k < m)
return puts("-1"), void();
else {
int flag1 = (k - n) % 2, flag2 = (m - n) % 2;
if (flag1 == 0 && flag2 == 0) printf("%lld\n", k);
if (flag1 == 0 && flag2 == 1) printf("%lld\n", k - 1);
if (flag1 == 1 && flag2 == 0) printf("%lld\n", k - 2);
if (flag1 == 1 && flag2 == 1) printf("%lld\n", k - 1);
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) Main();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long const N = 312312;
long long const INF = 1e9 + 7;
long long n, a[N], d;
long long ne[N], pre[N], pos[N], dp[N], par[N];
pair<long long, long long> t[4 * N];
pair<long long, long long> get(long long v, long long tl, long long tr,
long long l, long long r) {
if (l > r) return {-1LL, -1LL};
if (tl == l && tr == r)
return t[v];
else {
long long tm = (tl + tr) >> 1;
return max(get(v + v, tl, tm, l, min(r, tm)),
get(v + v + 1, tm + 1, tr, max(l, tm + 1), r));
t[v] = max(t[v + v], t[v + v + 1]);
}
}
void update(long long v, long long tl, long long tr, long long pos,
long long val1, long long val2) {
if (tl == tr) {
t[v] = {val1, val2};
} else {
long long tm = (tl + tr) >> 1;
if (pos <= tm)
update(v + v, tl, tm, pos, val1, val2);
else
update(v + v + 1, tm + 1, tr, pos, val1, val2);
t[v] = max(t[v + v], t[v + v + 1]);
}
}
int main() {
vector<pair<long long, long long> > v;
scanf("%I64d", &n);
scanf("%I64d", &d);
for (long long c = 1; c <= n; c++) {
scanf("%I64d", &a[c]);
v.push_back({a[c], c});
dp[c] = 1;
par[c] = -1LL;
}
sort(v.begin(), v.end());
for (long long c = 0; c < v.size(); c++) {
pos[v[c].second] = c + 1;
long long x = v[c].first + d;
long long l = 0, r = v.size() - 1, ans = -1LL;
while (l <= r) {
long long mid = (l + r) >> 1;
if (v[mid].first >= x)
ans = mid, r = mid - 1;
else
l = mid + 1;
}
if (ans == -1LL)
ans = n + 1;
else
ans++;
ne[v[c].second] = ans;
l = 0, r = v.size() - 1, ans = -1LL;
x = v[c].first - d;
while (l <= r) {
long long mid = (l + r) >> 1;
if (v[mid].first <= x)
ans = mid, l = mid + 1;
else
r = mid - 1;
}
if (ans == -1LL)
ans = 0;
else
ans++;
pre[v[c].second] = ans;
}
for (long long c = 1; c <= n; c++) {
pair<long long, long long> prevlen = {-1LL, -1LL};
pair<long long, long long> nextlen = {-1LL, -1LL};
prevlen = get(1, 1, n, 1, pre[c]);
nextlen = get(1, 1, n, ne[c], n);
pair<long long, long long> bestanswer = max(prevlen, nextlen);
if (bestanswer.first + 1 > dp[c]) {
dp[c] = bestanswer.first + 1;
par[c] = bestanswer.second;
}
update(1, 1, n, pos[c], dp[c], c);
}
long long ans = -1LL, ind = -1LL;
for (long long c = 1; c <= n; c++) {
if (dp[c] > ans) {
ans = dp[c];
ind = c;
}
}
printf("%I64d\n", ans);
vector<long long> path;
for (long long c = ind; c != -1LL; c = par[c]) {
path.push_back(c);
}
reverse(path.begin(), path.end());
for (long long c = 0; c < path.size(); c++) {
printf("%I64d ", path[c]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void islam() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
}
int main() {
islam();
int g[10][10];
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j) cin >> g[i][j];
int ans = -1, tmp;
int p[6] = {0, 1, 2, 3, 4};
int i = 0;
tmp = g[p[0]][p[1]] + g[p[1]][p[0]];
tmp += g[p[2]][p[3]] + g[p[3]][p[2]];
tmp += g[p[1]][p[2]] + g[p[2]][p[1]];
tmp += g[p[3]][p[4]] + g[p[4]][p[3]];
tmp += g[p[2]][p[3]] + g[p[3]][p[2]];
tmp += g[p[3]][p[4]] + g[p[4]][p[3]];
ans = tmp;
while (next_permutation(p, p + 5)) {
tmp = g[p[0]][p[1]] + g[p[1]][p[0]];
tmp += g[p[2]][p[3]] + g[p[3]][p[2]];
tmp += g[p[1]][p[2]] + g[p[2]][p[1]];
tmp += g[p[3]][p[4]] + g[p[4]][p[3]];
tmp += g[p[2]][p[3]] + g[p[3]][p[2]];
tmp += g[p[3]][p[4]] + g[p[4]][p[3]];
if (tmp > ans) {
ans = tmp;
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d %d", &n, &k);
int n2 = n;
n /= 2;
if (k == 0 && n2 == 1) {
printf("%d", 1);
return 0;
}
if (n > k || !n) {
cout << -1;
return 0;
}
int aux = 1;
if (k > n) {
n--;
n2 -= 2;
aux = k - n;
printf("%d %d ", aux, 2 * aux);
}
for (int i = 2 * aux + 1; i <= 2 * aux + n2; i++) printf("%d ", i);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
const int inf = 0xfffffff;
const int mod = 1e9 + 7;
const double pi = acos(-1);
int a[maxn], vis[maxn];
int rMax;
int n, k, d;
inline void open(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
bool bfs() {
queue<int> q;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int now = q.front();
q.pop();
int pos = upper_bound(a + now, a + 1 + n, a[now] + d) - a;
if (pos - now >= k) {
if (pos == n + 1) return 1;
now += k;
if (now < rMax) now = rMax;
for (int i = now; i <= pos; i++) {
if (!vis[i]) {
vis[i] = 1;
q.push(i);
}
}
rMax = pos;
}
}
return 0;
}
int main() {
scanf("%d%d%d", &n, &k, &d);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + 1 + n);
if (bfs())
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, k, p, i, j;
cin >> n >> k >> p;
long long a[n], b[k];
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (j = 0; j < k; j++) {
cin >> b[j];
}
long long start = 0, end = (long long)3000000000, ans;
while (start <= end) {
long long mid = (start + end) / 2;
set<long long> s;
for (i = 0; i < k; i++) {
s.insert(b[i]);
}
for (i = 0; i < n; i++) {
long long x = a[i], o = p;
if (x <= o) {
if (x + o > mid || 1) {
long long y = (x + o - mid) / 2 + (x + o - mid) % 2;
auto val = s.lower_bound(y);
if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
s.erase(s.lower_bound(y));
else
break;
} else {
auto val = s.lower_bound(x);
if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
s.erase(s.lower_bound(x));
else
break;
}
} else {
if (x + o > mid || 1) {
long long y = (x + o - mid) / 2 + (x + o - mid) % 2;
auto val = s.lower_bound(y);
if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
s.erase(s.lower_bound(y));
else
break;
} else {
auto val = s.lower_bound(o);
if (val != s.end() && abs(x - *val) + abs(o - *val) <= mid)
s.erase(s.lower_bound(o));
else
break;
}
}
}
if (i == n) {
ans = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int prime = 999983;
const int INF = 0x7FFFFFFF;
const long long INFF = 0x7FFFFFFFFFFFFFFF;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const long long mod = 1e9 + 7;
long long qpow(long long a, long long b) {
long long s = 1;
while (b > 0) {
if (b & 1) s = s * a % mod;
a = a * a % mod;
b >>= 1;
}
return s;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1};
const int maxn = 3e5 + 10, maxm = 250 + 10;
char ar[maxn], s[maxm][4];
int len[3];
int nxt[maxn][30];
int dp[maxm][maxm][maxm];
char op[3], ch[maxn];
int main(void) {
int n, m;
cin >> n >> m;
cin >> (ar + 1);
for (int i = 0; i < 26; ++i) nxt[n][i] = nxt[n + 1][i] = n + 1;
for (int i = n - 1; i >= 0; --i) {
for (int j = 0; j < 26; ++j) nxt[i][j] = nxt[i + 1][j];
nxt[i][(int)(ar[i + 1] - 'a')] = i + 1;
}
for (int i = 1; i <= m; ++i) {
int t;
scanf("%s %d", op, &t);
t--;
if (op[0] == '-') {
len[t]--;
} else {
scanf("%s", ch);
++len[t];
s[len[t]][t] = ch[0];
for (int i = t == 0 ? len[0] : 0; i <= len[0]; ++i) {
for (int j = t == 1 ? len[1] : 0; j <= len[1]; ++j) {
for (int k = t == 2 ? len[2] : 0; k <= len[2]; ++k) {
dp[i][j][k] = n + 1;
}
}
}
for (int i = t == 0 ? len[0] : 0; i <= len[0]; ++i) {
for (int j = t == 1 ? len[1] : 0; j <= len[1]; ++j) {
for (int k = t == 2 ? len[2] : 0; k <= len[2]; ++k) {
if (i > 0)
dp[i][j][k] =
min(dp[i][j][k], nxt[dp[i - 1][j][k]][s[i][0] - 'a']);
if (j > 0)
dp[i][j][k] =
min(dp[i][j][k], nxt[dp[i][j - 1][k]][s[j][1] - 'a']);
if (k > 0)
dp[i][j][k] =
min(dp[i][j][k], nxt[dp[i][j][k - 1]][s[k][2] - 'a']);
}
}
}
}
puts(dp[len[0]][len[1]][len[2]] <= n ? "YES" : "NO");
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<char> v;
char c = 'a';
for (int i = 0; i < 26; i++) {
v.push_back(c);
c++;
}
string out = "";
for (int i = 0; i < m; i++) {
out += v[i];
}
for (int i = 0; i < n; i++) cout << out[i % m];
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int niz[100005];
int res;
int n;
void prb(int x) {
for (int j = 1; j <= x; j++) {
int tren = 0;
for (int i = j; i <= n; i += x) {
tren += niz[i];
}
res = max(res, tren);
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.precision(10);
cout << fixed;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> niz[i];
res += niz[i];
}
for (int x = 1; x * x <= n; x++) {
if (n % x == 0) {
if (n / x >= 3) prb(x);
if (x >= 3) prb(n / x);
}
}
cout << res;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int value(vector<int> v) {
int val = 0, i, j, n = v.size();
for (i = 0; i < n; i++) {
for (j = i; j < n; j++)
val += *min_element(v.begin() + i, v.begin() + j + 1);
}
return val;
}
int main() {
ios_base::sync_with_stdio(0);
;
cin.tie(0);
cout.tie(0);
int n, i, x;
long long int m;
cin >> n >> m;
m--;
int a[n + 1];
int l = 0, r = n - 1;
for (i = 1; i < n; i++) {
if (m >= (1LL << (n - i - 1))) {
m -= (1LL << (n - i - 1));
a[r--] = i;
} else
a[l++] = i;
}
a[l] = n;
for (i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char grid[1001][1001];
int visited[1001][1001];
int arr[1000001];
int N, M, num = 0;
void recur(int r, int c, int n) {
if (r < 0 || c < 0 || r >= N || c >= M || grid[r][c] != '.') {
arr[n] = num;
return;
}
grid[r][c] = '!';
visited[r][c] = n;
if (grid[r - 1][c] == '*') num++;
if (grid[r + 1][c] == '*') num++;
if (grid[r][c - 1] == '*') num++;
if (grid[r][c + 1] == '*') num++;
recur(r - 1, c, n);
recur(r + 1, c, n);
recur(r, c - 1, n);
recur(r, c + 1, n);
}
int main() {
int K;
cin >> N >> M >> K;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> grid[i][j];
}
}
int x, y, n = 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (grid[i][j] == '.') {
num = 0;
recur(i, j, n);
n++;
}
}
}
for (int i = 0; i < K; i++) {
cin >> x >> y;
cout << arr[visited[x - 1][y - 1]] << endl;
}
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getrnd(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }
template <typename T1, typename T2>
inline bool relax(T1& a, const T2& b) {
return a > b ? a = b, true : false;
}
template <typename T1, typename T2>
inline bool strain(T1& a, const T2& b) {
return a < b ? a = b, true : false;
}
const int N = 1e5 + 3, K = 103;
int dp[K][N], l[K], r[K];
struct min_queue {
deque<int> q;
min_queue() {}
void push(int x) {
while (!q.empty() && q.back() > x) q.pop_back();
q.push_back(x);
}
int getmin() {
if (q.empty()) return 1e9;
return q.front();
}
void pop(int x) {
if (!q.empty() && q.front() == x) q.pop_front();
}
};
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < k; ++i) cin >> l[i] >> r[i];
l[k] = r[k] = 2 * n;
for (int i = 0; i < K; ++i)
for (int j = 0; j < N; ++j) dp[i][j] = 1e9;
if (l[0] <= n) dp[0][l[0]] = 0;
for (int i = 1; i <= k; ++i) {
min_queue q1, q2;
for (int x = 0; x < (r[i - 1] - l[i - 1]); ++x)
if (l[i] - x >= 0) q1.push(dp[i - 1][l[i] - x]);
for (int x = 1; x <= (r[i - 1] - l[i - 1] - 1); ++x)
if (-(l[i] - r[i - 1]) - x >= 0)
q2.push(dp[i - 1][-(l[i] - r[i - 1]) - x]);
for (int t = 0; t <= n; ++t) {
if (t - (l[i] - l[i - 1]) >= 0)
relax(dp[i][t], dp[i - 1][t - (l[i] - l[i - 1])]);
if (r[i - 1] - l[i - 1] - 1 >= 0) {
if (t - (l[i] - r[i - 1]) >= 0)
q2.push(dp[i - 1][t - (l[i] - r[i - 1])]);
relax(dp[i][t], 2 + q2.getmin());
if (t - (l[i] - r[i - 1]) - (r[i - 1] - l[i - 1] - 1) >= 0)
q2.pop(dp[i - 1][t - (l[i] - r[i - 1]) - (r[i - 1] - l[i - 1] - 1)]);
}
if (l[i] - t - (r[i - 1] - l[i - 1]) >= 0)
q1.push(dp[i - 1][l[i] - t - (r[i - 1] - l[i - 1])]);
relax(dp[i][t], 1 + q1.getmin());
if (l[i] - t >= 0) q1.pop(dp[i - 1][l[i] - t]);
}
}
int res = dp[k][n];
if (res > 1e7)
cout << "Hungry\n";
else
cout << "Full\n" << res << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(0));
int t = 1;
while (t--) solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAXn = 100 * 1000 + 17;
int n;
long long arr[MAXn];
vector<int> adj[MAXn];
long long dp[MAXn];
bool mark[MAXn];
long long sum;
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
while (b != 0) {
a = a % b;
swap(a, b);
}
return a;
}
long long LCM(long long a, long long b) {
if (min(a, b) == 0)
return 0;
else
return (a / GCD(a, b)) * b;
}
void DFS(int u) {
mark[u] = 1;
long long lcm = 1;
int k = 0;
long long minimum = 1000000000LL * 1000000000LL;
for (int i = 0; i < adj[u].size(); i++) {
if (mark[adj[u][i]] == 0) {
DFS(adj[u][i]);
if (arr[adj[u][i]] == 0) {
arr[u] = 0;
return;
}
lcm = LCM(lcm, dp[adj[u][i]]);
minimum = min(minimum, arr[adj[u][i]]);
k++;
}
}
if (k == 0) {
dp[u] = 1;
return;
}
dp[u] = lcm * k;
arr[u] = (minimum / lcm) * k * lcm;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
DFS(0);
cout << sum - arr[0] << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
int bit[500000 + 1];
void add(int a, int w) {
for (int x = a; x <= n; x += x & (-x)) bit[x] += w;
}
int sum(int a) {
int ret = 0;
for (int x = a; x > 0; x -= x & (-x)) ret += bit[x];
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
n = static_cast<int>(s.size());
for (int i = 0; i < n; i++) {
if (s[i] == 'I' || s[i] == 'E' || s[i] == 'A' || s[i] == 'O' ||
s[i] == 'U' || s[i] == 'Y') {
add(i + 1, 1);
}
}
double res = 0;
double all = 0;
for (int i = 1; i <= n; i++) {
all += 1.0 / i;
}
if (n % 2 == 0) {
for (int j = 0; j < n / 2; j++) {
res += (double)(sum(n - j) - sum(j)) * all;
all = all - 1.0 / (j + 1) - 1.0 / (n - j);
}
} else {
for (int j = 0; j < n / 2 + 1; j++) {
res += (double)(sum(n - j) - sum(j)) * all;
all = all - 1.0 / (j + 1) - 1.0 / (n - j);
}
}
cout << setprecision(10) << res << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
double x[1005], y[1005];
struct P {
double k, b;
} val[1000005];
bool cmp(P u, P v) {
if (u.k == v.k) return u.b < v.b;
return u.k < v.k;
}
int main() {
scanf(" %d", &n);
for (int i = 1; i <= n; i++) scanf(" %lf %lf", &x[i], &y[i]);
int cnt = 0;
for (int i = 1; i <= n - 1; i++) {
for (int j = i + 1; j <= n; j++) {
if (x[i] == x[j]) {
val[++cnt].b = x[i];
val[cnt].k = 1e9;
} else {
val[++cnt].k = (y[j] - y[i]) / (x[j] - x[i]);
val[cnt].b = y[i] - x[i] * val[cnt].k;
}
}
}
sort(val + 1, val + cnt + 1, cmp);
double posk = val[1].k - 1, posb = val[1].b - 1;
long long num = 0, sol = 1, sum = 0;
for (int i = 1; i <= cnt; i++) {
if (val[i].k == posk) {
if (val[i].b == posb) {
} else {
posb = val[i].b;
sol++;
num++;
}
if (i == cnt) sum -= sol * (sol - 1) / 2;
} else {
sum -= sol * (sol - 1) / 2;
sol = 1;
posk = val[i].k;
posb = val[i].b;
num++;
}
}
sum += num * (num - 1) / 2;
printf("%lld\n", sum);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, cnt = 0;
cin >> n >> m;
string s[n];
for (i = 0; i < n; i++) {
cin >> s[i];
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < m - 1; j++) {
if (((s[i][j] * s[i + 1][j]) * (s[i][j + 1]) * (s[i + 1][j + 1]) ==
(('f') * ('a') * ('c') * ('e')))) {
cnt++;
}
}
}
cout << cnt << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 5;
struct edge {
int from, to, id, cost;
};
int N, M, s, t;
vector<edge> G[MAXN];
bool vis[MAXN], found;
vector<edge> path;
void dfs1(int u, int id = -1) {
if (u == t) return void(found = true);
vis[u] = true;
for (int i = 0; i < G[u].size(); i++) {
if (G[u][i].id == id) continue;
int v = G[u][i].to;
if (vis[v]) continue;
dfs1(v, id);
if (found) {
if (id == -1) path.push_back(G[u][i]);
return;
}
}
}
int td[MAXN], low[MAXN];
vector<edge> bridges;
bool dfs(int u, int idp, int id) {
bool ret = (u == t);
low[u] = td[u];
for (int i = 0; i < G[u].size(); i++) {
if (G[u][i].id == id || G[u][i].id == idp) continue;
int v = G[u][i].to;
if (td[v] > td[u]) continue;
if (td[v] == -1) {
td[v] = td[u] + 1;
bool vret = dfs(v, G[u][i].id, id);
ret |= vret;
low[u] = min(low[u], low[v]);
if (low[v] == td[v] && vret) bridges.push_back(G[u][i]);
} else {
low[u] = min(low[u], td[v]);
}
}
return ret;
}
int main() {
scanf("%d", &N), scanf("%d", &M);
scanf("%d", &s), scanf("%d", &t);
s--;
t--;
for (int i = 0; i < M; i++) {
int u, v, cost;
scanf("%d", &u), scanf("%d", &v);
u--;
v--;
scanf("%d", &cost);
G[u].push_back({u, v, i, cost});
G[v].push_back({v, u, i, cost});
}
dfs1(s);
if (!found) {
printf("0\n0\n");
return 0;
}
int ans = -1;
vector<int> vans;
for (int i = 0; i < path.size(); i++) {
int id = path[i].id;
found = false;
memset(vis, false, N * sizeof(bool));
dfs1(s, id);
if (!found) {
if (ans == -1 || path[i].cost < ans) {
ans = path[i].cost;
vans.clear();
vans.push_back(id);
}
} else {
bridges.clear();
memset(td, -1, sizeof(td));
td[s] = 0;
dfs(s, -1, id);
if (bridges.size() > 0) {
for (int j = 0; j < bridges.size(); j++) {
int nans = path[i].cost + bridges[j].cost;
if (ans == -1 || nans < ans) {
ans = nans;
vans.clear();
vans.push_back(id);
vans.push_back(bridges[j].id);
}
}
}
}
}
printf("%d\n", ans);
if (ans != -1) {
printf("%d\n", (int)vans.size());
for (int i = 0; i < vans.size(); i++) {
printf("%d%c", vans[i] + 1, " \n"[i == vans.size() - 1]);
}
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
typedef long SMatrix[50][50];
typedef long BMatrix[100][100];
void getMatrix(long n, SMatrix m) {
for (int i = 0; i < n; i++) {
string str;
cin >> str;
for (long j = 0; j < n; j++) {
string substr = str.substr(j, 1);
if (substr == "o") m[i][j] = 2;
if (substr == "x") m[i][j] = 1;
if (substr == ".") m[i][j] = 0;
}
}
}
void printBMatrix(long n, BMatrix m) {
for (int i = 0; i < 2 * n - 1; i++) {
for (int j = 0; j < 2 * n - 1; j++) {
int a = m[i][j];
switch (m[i][j]) {
case 0:
cout << ".";
break;
case 1:
cout << "x";
break;
case 2:
cout << "o";
break;
}
}
cout << endl;
}
}
void initiateBMatrix(long n, BMatrix m) {
for (int i = 0; i < 2 * n - 1; i++)
for (int j = 0; j < 2 * n - 1; j++)
(m)[i][j] = ((i == (n - 1)) && (j == (n - 1))) ? 2 : 1;
}
void deleteX(long n, SMatrix m, BMatrix result, int x, int y) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (m[i][j] == 0) {
result[i - x + n - 1][j - y + n - 1] = 0;
}
}
void algo(long n, SMatrix m, BMatrix result) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (m[i][j] == 2) deleteX(n, m, result, i, j);
}
int isX(long n, SMatrix m, BMatrix result, int x, int y) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if ((m[i][j] == 2) && (result[x - i + n - 1][y - j + n - 1] == 1))
return 1;
return 0;
}
int checkResult(long n, SMatrix m, BMatrix result) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if ((m[i][j] == 1) && (isX(n, m, result, i, j) == 0)) return 0;
return 1;
}
int main() {
long n;
cin >> n;
SMatrix m;
BMatrix result;
getMatrix(n, m);
initiateBMatrix(n, result);
algo(n, m, result);
long status = checkResult(n, m, result);
if (status) {
cout << "YES" << endl;
printBMatrix(n, result);
} else
cout << "NO" << endl;
cin >> n;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long a[1000003];
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
long long n, c;
cin >> n;
c = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] == 1) {
++c;
}
}
if (c == n) {
for (int i = 0; i < n - 1; ++i) {
cout << "1"
<< " ";
}
cout << "2"
<< "\n";
return 0;
}
sort(a, a + n);
for (long long i = 0; i < n; ++i) {
if (i == 0) {
cout << "1"
<< " ";
} else {
cout << a[i - 1] << " ";
}
}
cout << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
long long x = 0, ans = 0;
for (long long i = 0; i < n; i++) {
if (a[i] >= 0) {
x += a[i];
} else {
if (x >= -1 * a[i])
x += a[i];
else {
ans += (-1 * a[i] - x);
x = 0;
}
}
}
cout << ans << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, x, y;
vector<int> vec[2 * 100005];
pair<int, int> hz[100005];
int viz[2 * 100005];
void dfs(int nod, int col) {
viz[nod] = col;
for (auto it : vec[nod]) {
if (viz[it] == col) {
cout << "-1";
exit(0);
}
if (!viz[it]) dfs(it, 3 - col);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cerr.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x >> y;
vec[x].push_back(y);
vec[y].push_back(x);
vec[i << 1].push_back(i << 1 | 1);
vec[i << 1 | 1].push_back(i << 1);
hz[i] = make_pair(x, y);
}
for (int i = 1; i <= n * 2; i++)
if (!viz[i]) dfs(i, 1);
for (int i = 1; i <= n; i++)
cout << viz[hz[i].first] << ' ' << viz[hz[i].second] << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f;
const int _OO = -1 * OO;
const double EPS = (1e-9);
const double pi = 3.14159265;
using namespace std;
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy8[8] = {1, -1, 0, 1, -1, 1, -1, 0};
long long gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(int a, int b) { return (a / gcd(a, b)) * b; }
const int N = 1e5 + 7;
string s[N][3];
bool GO[N][2][2];
int arr[N], n;
long long dp[N][2];
long long solve(int idx, bool lst) {
if (idx == n) {
return (lst == 1 ? arr[idx - 1] : 0);
}
long long& ret = dp[idx][lst];
if (~ret) return ret;
ret = 1e18;
if (GO[idx][0][lst]) {
ret = min(ret, solve(idx + 1, 0) + (lst == 1 ? arr[idx - 1] : 0));
}
if (GO[idx][1][lst]) {
ret = min(ret, solve(idx + 1, 1) + (lst == 1 ? arr[idx - 1] : 0));
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
cin >> s[i][0];
s[i][1] = s[i][0];
reverse(s[i][1].begin(), s[i][1].end());
}
for (int i = 1; i < n; i++) {
if (s[i][0] >= s[i - 1][0]) GO[i][0][0] = 1;
if (s[i][0] >= s[i - 1][1]) GO[i][0][1] = 1;
if (s[i][1] >= s[i - 1][0]) GO[i][1][0] = 1;
if (s[i][1] >= s[i - 1][1]) GO[i][1][1] = 1;
}
long long cnt = min(solve(1, 0), solve(1, 1));
cout << (cnt >= 1e18 ? -1 : cnt);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int i = 0;
int sum = 0;
while (n > sum) {
sum += i;
if (n < sum) break;
i++;
n -= sum;
}
cout << i - 1;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
char c;
while (getline(cin, s)) {
for (int i = s.size() - 1; i >= 0; i--) {
if (isalpha(s[i])) {
c = s[i];
break;
}
}
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'Y')
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b, c;
cin >> a >> b >> c;
int i = 0, j = 0;
while (i == 0) {
if (c.find(a[i]) != string::npos) {
c.erase(c.find(a[i]), 1);
a.erase(i, 1);
} else
i++;
}
while (j == 0) {
if (c.find(b[j]) != string::npos) {
c.erase(c.find(b[j]), 1);
b.erase(j, 1);
} else
j++;
}
if (a.empty() && b.empty() && c.empty())
cout << "YES";
else
cout << "NO";
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
using namespace std;
clock_t time_p = clock();
void aryanc403() {}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
return uniform_int_distribution<long long int>(l, r)(rng);
}
class CMP {
public:
bool operator()(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
return !(a.first < b.first || (a.first == b.first && a.second <= b.second));
}
};
void add(map<long long int, long long int> &m, long long int x,
long long int cnt = 1) {
auto jt = m.find(x);
if (jt == m.end())
m.insert({x, cnt});
else
jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
long long int cnt = 1) {
auto jt = m.find(x);
if (jt->second <= cnt)
m.erase(jt);
else
jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int mod = 1000000007L;
long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;
long long int m;
string s;
long long int a, b, c;
long double dist(long double x1, long double y1, long double x2,
long double y2) {
long double ans = 0;
ans += (x1 - x2) * (x1 - x2);
ans += (y1 - y2) * (y1 - y2);
ans = sqrt(ans);
return ans;
}
long double chk(long long int x1, long long int y1, long long int x2,
long long int y2) {
long double x, y;
y = (-c - a * x1) / (long double)b;
x = (-c - b * y2) / (long double)a;
;
;
;
return abs(y1 - y) + abs(x2 - x) + dist(x1, y, x, y2);
}
long double chk2(long long int x1, long long int y1, long long int x2,
long long int y2) {
long double xa, xb;
xa = (-c - b * y1) / (long double)a;
xb = (-c - b * y2) / (long double)a;
return abs(x1 - xa) + abs(x2 - xb) + dist(xa, y1, xb, y2);
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << std::fixed << std::setprecision(35);
{
long long int x1, x2, y1, y2;
cin >> a >> b >> c;
cin >> x1 >> y1 >> x2 >> y2;
long double ans = abs(x1 - x2) + abs(y1 - y2);
if (x1 == x2 || y1 == y2 || a == 0 || b == 0) {
cout << ans << "\n";
return 0;
}
ans = min(ans, chk(x1, y1, x2, y2));
ans = min(ans, chk(x2, y2, x1, y1));
ans = min(ans, chk2(x2, y2, x1, y1));
swap(a, b);
ans = min(ans, chk2(y2, x2, y1, x1));
cout << ans << "\n";
}
aryanc403();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
namespace IO {
const int sz = 1 << 15;
char inbuf[sz], outbuf[sz];
char *pinbuf = inbuf + sz;
char *poutbuf = outbuf;
inline char _getchar() {
if (pinbuf == inbuf + sz) fread(inbuf, 1, sz, stdin), pinbuf = inbuf;
return *(pinbuf++);
}
inline void _putchar(char x) {
if (poutbuf == outbuf + sz) fwrite(outbuf, 1, sz, stdout), poutbuf = outbuf;
*(poutbuf++) = x;
}
inline void flush() {
if (poutbuf != outbuf)
fwrite(outbuf, 1, poutbuf - outbuf, stdout), poutbuf = outbuf;
}
} // namespace IO
inline int read() {
int x = 0, p = 1;
char c = IO::_getchar();
while (c < '0' || c > '9') {
if (c == '-') p = -1;
c = IO::_getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = IO::_getchar();
return x * p;
}
const int Maxn = 22;
const int Maxm = 105;
int eu[Maxm], ev[Maxm], ew[Maxm], n, m, t;
int dat[500005][Maxn], W[500005];
long long Ha[500005];
bool usey[500005][Maxn];
vector<int> CanUse;
struct S {
int I;
bool operator<(const S &s) const {
if (W[I] != W[s.I]) return W[I] < W[s.I];
return I < s.I;
}
};
multiset<S> id;
unordered_set<long long> Vis;
int main() {
scanf("%d %d %d", &n, &m, &t);
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &eu[i], &ev[i], &ew[i]);
}
for (int i = 1; i < 500005; i++) CanUse.push_back(i);
S zero;
zero.I = 0;
id.insert(zero);
int lim = 2e9;
while (!id.empty()) {
int Id = (*id.begin()).I;
id.erase(id.begin());
t--;
if (t == 0) {
printf("%d\n", W[Id]);
return 0;
}
for (int i = 0; i < m; i++) {
if (!dat[Id][eu[i]] && !usey[Id][ev[i]]) {
int TW = W[Id] + ew[i];
if (TW >= lim) continue;
int nid = CanUse.back();
CanUse.pop_back();
for (int j = 1; j <= n; j++) dat[nid][j] = dat[Id][j];
for (int j = 1; j <= n; j++) usey[nid][j] = usey[Id][j];
usey[nid][ev[i]] = 1;
dat[nid][eu[i]] = ev[i];
Ha[nid] = 0;
W[nid] = TW;
for (int j = 1; j <= n; j++) {
Ha[nid] = Ha[nid] * 394291323 + dat[nid][j];
}
if (Vis.find(Ha[nid]) != Vis.end()) {
CanUse.push_back(nid);
continue;
}
Vis.insert(Ha[nid]);
S tt;
tt.I = nid;
id.insert(tt);
}
}
if (id.size() > t) {
while (id.size() != t) {
int tmp = (*id.rbegin()).I;
CanUse.push_back(tmp);
Vis.erase(Ha[tmp]);
id.erase(*id.rbegin());
}
int tmp = (*id.rbegin()).I;
lim = W[tmp];
}
}
return ~~(0 ^ 0 ^ 0);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
int n;
char a[5001][5001];
int met[301];
bool was[301];
int dif = 0;
int main() {
srand(time(NULL));
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
cin >> a[i][j];
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (!(i == j || j == n - i + 1)) {
if (a[i][j] == a[1][1] || a[i][j] == a[1][n]) {
cout << "NO";
return 0;
}
if (!was[a[i][j]]) ++dif;
was[a[i][j]] = 1;
if (dif == 2) {
cout << "NO";
return 0;
}
continue;
}
if (i == j) {
++met[a[i][j]];
continue;
}
if (j == n - i + 1) ++met[a[i][j]];
}
if (met[a[1][1]] == n + n - 1)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int k, i, b, j, ans = 0;
cin >> k;
map<string, vector<string> > sl;
map<string, int> kolv;
vector<string> ch(k);
string a;
for (i = 0; i < k; i++) {
cin >> ch[i];
kolv[ch[i]] += 1;
string ne = ch[i].substr(0, 3);
sl[ne].push_back(ch[i].substr(3, 1));
}
for (i = 0; i < k; i++) {
a = ch[i];
if (kolv[a] > 1) {
string osn = a.substr(0, 3);
for (j = 0; j < 10; j++)
if (find(sl[osn].begin(), sl[osn].end(), to_string(j)) ==
sl[osn].end()) {
a[3] = j + '0';
ans++;
break;
}
kolv[ch[i]] -= 1;
sl[osn].push_back(to_string(j));
ch[i] = a;
}
}
cout << ans << endl;
for (auto c : ch) {
cout << c << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[2005];
pair<int, int> b[2005];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = pair<int, int>(-a[i], i);
}
sort(b + 1, b + 1 + n);
vector<int> v;
int ans = 0;
for (int i = 1; i <= k; i++) {
v.push_back(b[i].second);
ans += -b[i].first;
}
int pv = 0;
sort(v.begin(), v.end());
printf("%d\n", ans);
for (int i = 0; i < v.size(); i++) {
if (i == v.size() - 1)
printf("%d ", n - pv);
else
printf("%d ", v[i] - pv), pv = v[i];
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 + 10;
const int maxm = maxn * 4 + 10;
const int inf = 1e8;
const long long mod = 1e9 + 7;
int getint() {
char c;
while ((c = getchar()) && !(c >= '0' && c <= '9') && c != '-')
;
int ret = c - '0', sgn = 0;
if (c == '-') sgn = 1, ret = 0;
while ((c = getchar()) && c >= '0' && c <= '9') ret = ret * 10 + c - '0';
if (sgn) ret = -ret;
return ret;
}
const int limit = 100;
int dp[limit * 100 + 10], a[maxn], cnt[maxn];
int main() {
int n, t;
while (scanf("%d%d", &n, &t) == 2) {
int i;
for (i = 1; i <= n; i++) a[i] = getint();
int ans = 0;
if (t <= limit) {
for (int k = 1; k <= t; k++) {
for (int i = 1; i <= n; i++) {
int temp = (k - 1) * n + i;
dp[temp] = 1;
for (int j = 1; j < temp; j++) {
if (a[(j - 1) % n + 1] <= a[(temp - 1) % n + 1]) {
dp[temp] = max(dp[temp], dp[j] + 1);
}
}
ans = max(ans, dp[temp]);
}
}
cout << ans << endl;
} else {
for (int k = 1; k <= 100; k++) {
for (int i = 1; i <= n; i++) {
int temp = (k - 1) * n + i;
dp[temp] = 1;
for (int j = 1; j < temp; j++) {
if (a[(j - 1) % n + 1] <= a[(temp - 1) % n + 1]) {
dp[temp] = max(dp[temp], dp[j] + 1);
}
}
ans = max(ans, dp[temp]);
}
}
memset(cnt, 0, sizeof(cnt));
int tot = 1;
for (int i = 1; i <= n; i++) {
cnt[a[i]]++;
}
for (int i = 1; i <= n; i++) {
if (cnt[a[i]] > tot) tot = cnt[a[i]];
}
ans = ans + (t - 100) * tot;
cout << ans << endl;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> answer;
string str;
int pre[5000];
int f(int i, int j) {
if (i >= j) return 0;
if (str[i] == '(') {
answer.push_back(f(i + 1, pre[i] - 1));
return 0;
}
if (str[i] == '[') {
int cnt = 0;
while (i < j) {
cnt += f(i + 1, pre[i] - 1);
i = pre[i] + 1;
}
return cnt;
}
if (str[i] == '{') {
int cnt = 0;
while (i < j) {
cnt += f(i + 1, pre[i] - 1) + 1;
i = pre[i] + 1;
}
return cnt;
}
return 0;
}
void replace(string word, char c) {
vector<int> v;
int cnt = 0;
for (int i = 0; i <= str.size() - word.size(); i++)
if (str.substr(i, word.size()) == word) v.push_back(i);
for (int i = 0; i < v.size(); i++) {
str = str.substr(0, v[i] - cnt * word.size() + cnt) + c +
str.substr(v[i] + word.size() - cnt * word.size() + cnt);
cnt++;
}
}
int main() {
string t;
while (cin >> t) str += t;
replace("<table>", '(');
replace("</table>", ')');
replace("<tr>", '[');
replace("</tr>", ']');
replace("<td>", '{');
replace("</td>", '}');
stack<int> S;
for (int i = 0; i < str.size(); i++)
if (str[i] == '(' || str[i] == '{' || str[i] == '[')
S.push(i);
else {
pre[S.top()] = i;
S.pop();
}
f(0, str.size());
sort(answer.begin(), answer.end());
for (int i = 0; i < answer.size(); i++) {
cout << answer[i];
if (i + 1 != answer.size()) cout << " ";
}
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 9e18;
const int inf = 2147483647;
long long phi(long long n) {
long long ans = n;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
}
if (n > 1) ans = ans / n * (n - 1);
return ans;
}
long long qpow(long long a, long long b, long long mod) {
long long r = 1;
for (; b; b >>= 1) {
if (b & 1) r = (r * a) % mod;
a = (a * a) % mod;
}
return r;
}
long long C(int n, int m) {
if (m < n - m) m = n - m;
long long ans = 1;
for (int i = m + 1; i <= n; i++) ans *= i;
for (int i = 1; i <= n - m; i++) ans /= i;
return ans;
}
const int N = 1e6 + 5;
int a[N];
int dp[N][3][3][3];
void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
a[x]++;
}
int ans = 0;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
if (j + k + l > a[i]) continue;
for (int p = 0; p < 3; p++) {
dp[i][j][k][l] = max(dp[i][j][k][l], dp[i - 1][p][j][k] + l +
(a[i] - j - k - l) / 3);
}
}
}
}
}
for (int i = 0; i < 3; i++) {
ans = max(ans, dp[m][i][0][0]);
}
printf("%d\n", ans);
}
int main() {
work();
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long int mod = 1000000007;
double PI = 3.14159265358979323846264338327950288;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
long long int arr[1000010] = {0};
int main() {
long long int m, n, q, test;
string s1, s2, s3, s4;
char a1[5], a2[5];
cin >> s1 >> s2 >> s3 >> s4;
if (s1[0] == 'X') {
a1[0] = s1[1];
a1[1] = s2[1];
a1[2] = s2[0];
} else if (s1[1] == 'X') {
a1[0] = s1[0];
a1[1] = s2[1];
a1[2] = s2[0];
} else if (s2[0] == 'X') {
a1[0] = s1[0];
a1[1] = s1[1];
a1[2] = s2[1];
} else if (s2[1] == 'X') {
a1[0] = s1[0];
a1[1] = s1[1];
a1[2] = s2[0];
}
if (s3[0] == 'X') {
a2[0] = s3[1];
a2[1] = s4[1];
a2[2] = s4[0];
} else if (s3[1] == 'X') {
a2[0] = s3[0];
a2[1] = s4[1];
a2[2] = s4[0];
} else if (s4[0] == 'X') {
a2[0] = s3[0];
a2[1] = s3[1];
a2[2] = s4[1];
} else if (s4[1] == 'X') {
a2[0] = s3[0];
a2[1] = s3[1];
a2[2] = s4[0];
}
if ((a1[0] == 'A' && a1[1] == 'B' && a1[2] == 'C') ||
(a1[0] == 'C' && a1[1] == 'A' && a1[2] == 'B') ||
(a1[0] == 'B' && a1[1] == 'C' && a1[2] == 'A')) {
if ((a2[0] == 'A' && a2[1] == 'B' && a2[2] == 'C') ||
(a2[0] == 'C' && a2[1] == 'A' && a2[2] == 'B') ||
(a2[0] == 'B' && a2[1] == 'C' && a2[2] == 'A'))
printf("%s\n", "YES");
else
printf("%s\n", "NO");
} else {
if ((a2[0] == 'A' && a2[1] == 'B' && a2[2] == 'C') ||
(a2[0] == 'C' && a2[1] == 'A' && a2[2] == 'B') ||
(a2[0] == 'B' && a2[1] == 'C' && a2[2] == 'A'))
printf("%s\n", "NO");
else
printf("%s\n", "YES");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int const N = 1010;
int a[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= (n * 2); ++i) scanf("%d", &a[i]);
int ans = 0;
for (int i = 1; i < n * 2; i += 2) {
if (a[i] == a[i + 1]) continue;
for (int j = i + 1; j <= n * 2; ++j) {
if (a[j] == a[i]) {
for (int k = j - 1; k > i; --k) {
swap(a[k], a[k + 1]);
++ans;
}
break;
}
}
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> a, b;
long long ans;
long long bs(long long st, long long en) {
long long mid;
if (st < en) {
mid = st + ((en - st) / 2);
long long tmp;
for (int i = 0; i < n; ++i) {
long long e = (mid * a[i]) - b[i];
e = e < 0 ? 0 : e;
tmp += e;
}
if (tmp > k) {
bs(st, mid);
} else {
ans = mid;
bs(mid + 1, en);
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
int x;
for (int i = 0; i < n; ++i) {
cin >> x;
a.push_back(x);
}
for (int i = 0; i < n; ++i) {
cin >> x;
b.push_back(x);
}
long long t = bs(0, 10e14);
cout << t << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
stack<int> s[300005];
priority_queue<int> w;
int main() {
int n, q, x, y, maxt = 0, cnt = 0, read = 0;
scanf("%d %d", &n, &q);
for (int i = 0; i < q; i++) {
scanf("%d %d", &x, &y);
if (x == 1) {
cnt++;
s[y].push(cnt);
} else if (x == 2) {
while (!s[y].empty()) {
if (s[y].top() > maxt) {
w.push(-s[y].top());
read++;
}
s[y].pop();
}
} else if (x == 3) {
while (!w.empty() && y >= -w.top()) {
w.pop();
read--;
}
maxt = max(maxt, y);
}
printf("%d\n", cnt - maxt - read);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
int ans = max(abs(y1 - y2), abs(x1 - x2));
printf("%d\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << ' ' << H;
debug_out(T...);
}
const int INF = 1e9;
const long long MOD = 1e9 + 7;
const int N = 1e5 + 5, MO = 320, SQ = 600;
int n, q, A[N], QL[N], QR[N], P[N], T[N], C[N], K[N], Q[N];
vector<int> B, X, Y;
bool CMP(int i, int j) {
if (QL[i] / MO != QL[j] / MO) return QL[i] < QL[j];
return (QL[i] / MO) % 2 ? QR[i] < QR[j] : QR[i] > QR[j];
}
void Update(int x, int y) { T[C[x]]--, C[x] += y, T[C[x]]++; }
void Insert(int x, int y) {
if (x >= SQ && !K[x]) Y.push_back(x);
K[x] += y;
}
int Huf() {
X.clear(), Y.clear();
for (int i : B)
if (C[i] >= SQ) X.push_back(C[i]), K[C[i]]++;
sort(X.begin(), X.end());
int huf = 0, c = 0, x = 0, y = 0;
for (int i = 1; i < SQ; i++)
if (K[i] + T[i]) {
K[i] += T[i];
if (c) huf += i + c, K[i]--, Insert(i + c, 1), c = 0;
if (K[i] & 1) c = i, K[i]--;
if (K[i]) huf += i * K[i], Insert(i + i, K[i] >> 1), K[i] = 0;
}
while (x < (int)(X.size()) || y < (int)(Y.size())) {
while (x + 1 < (int)(X.size()) && X[x + 1] == X[x]) x++;
while (y + 1 < (int)(Y.size()) && Y[y + 1] == Y[y]) y++;
int i = 0;
if (x == (int)(X.size()))
i = Y[y++];
else if (y == (int)(Y.size()))
i = X[x++];
else if (X[x] < Y[y])
i = X[x++];
else
i = Y[y++];
if (c) huf += i + c, K[i]--, Insert(i + c, 1), c = 0;
if (K[i] & 1) c = i, K[i]--;
if (K[i]) huf += i * K[i], Insert(i + i, K[i] >> 1), K[i] = 0;
}
return huf;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
mt19937 Rnd(time(0));
cin >> n;
for (int i = 0; i < n; i++) cin >> A[i], C[A[i]]++;
for (int i = 0; i < N; i++)
if (C[i] >= SQ) B.push_back(i);
for (int i = 0; i < n; i++) C[A[i]]--;
cin >> q;
for (int i = 0; i < q; i++) cin >> QL[i] >> QR[i], QL[i]--, P[i] = i;
sort(P, P + q, CMP);
int L = 0, R = 0;
for (int i = 0; i < q; i++) {
int l = QL[P[i]], r = QR[P[i]];
while (l < L) Update(A[--L], +1);
while (R < r) Update(A[R++], +1);
while (L < l) Update(A[L++], -1);
while (r < R) Update(A[--R], -1);
Q[P[i]] = Huf();
}
for (int i = 0; i < q; i++) cout << Q[i] << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
struct Function {
string name;
vector<string> types;
} f[1011], cur;
bool operator==(const Function &a, const Function &b) {
if (a.name != b.name) return false;
if (a.types.size() != b.types.size()) return false;
for (int i = 0, _a = (a.types.size()); i < _a; i++)
if (a.types[i] != "T" && b.types[i] != "T")
if (a.types[i] != b.types[i]) return false;
return true;
}
map<string, string> types;
int nFunc, nVar, nCall;
char s[1011];
int main() {
scanf("%d\n", &nFunc);
for (int i = 1; i <= nFunc; i++) {
gets(s);
{
for (int _ = 0, _a = (strlen(s)); _ < _a; _++)
if (s[_] == '(' || s[_] == ')' || s[_] == ',') s[_] = ' ';
};
istringstream sin(s);
string tmp;
sin >> tmp;
sin >> f[i].name;
f[i].types.clear();
while (sin >> tmp) {
f[i].types.push_back(tmp);
}
}
scanf("%d\n", &nVar);
for (int i = 1; i <= nVar; i++) {
gets(s);
{
for (int _ = 0, _a = (strlen(s)); _ < _a; _++)
if (s[_] == '(' || s[_] == ')' || s[_] == ',') s[_] = ' ';
};
string t, v;
istringstream sin(s);
sin >> t >> v;
types[v] = t;
}
scanf("%d\n", &nCall);
for (int i = 1; i <= nCall; i++) {
gets(s);
{
for (int _ = 0, _a = (strlen(s)); _ < _a; _++)
if (s[_] == '(' || s[_] == ')' || s[_] == ',') s[_] = ' ';
};
istringstream sin(s);
sin >> cur.name;
cur.types.clear();
string tmp;
while (sin >> tmp) {
cur.types.push_back(types[tmp]);
}
int res = 0;
for (int i = 1; i <= nFunc; i++) {
if (cur == f[i]) res++;
}
printf("%d\n", res);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0, y = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') y = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * y;
}
long long t, n, m;
long long a[2000010], pl[2000010], b[2000010];
signed main() {
t = read();
while (t--) {
n = read(), m = read();
for (long long i = 1; i <= n; i++) a[i] = pl[i] = b[i] = 0;
set<long long> q;
for (long long i = 1; i <= n; i++) a[i] = read(), pl[a[i]] = i;
for (long long j = 1; j <= m; j++) {
b[j] = read();
q.insert(b[j]);
}
long long ans = 1ll;
for (long long i = 1; i <= m; i++) {
long long ji = 0;
q.erase(b[i]);
ji += ((pl[b[i]] > 1) && !q.count(a[pl[b[i]] - 1]));
ji += ((pl[b[i]] < n) && !q.count(a[pl[b[i]] + 1]));
ans = ji * ans % 998244353;
}
cout << ans << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t[3], h, m, s, t1, t2;
cin >> h >> m >> s >> t1 >> t2;
t[0] = 60 * 60 * h + 60 * m + s;
t[1] = 12 * 60 * m + 12 * s;
t[2] = 12 * 60 * s;
t1 *= 60 * 60;
t2 *= 60 * 60;
sort(t, t + 3);
if (t[0] < t1 && t1 < t[1]) {
if (t[0] < t2 && t2 < t[1])
cout << "Yes";
else
cout << "No";
} else if (t[1] < t1 && t1 < t[2]) {
if (t[1] < t2 && t2 < t[2])
cout << "Yes";
else
cout << "No";
} else if (t[0] > t1 || t1 > t[2]) {
if (t[0] > t2 || t2 > t[2])
cout << "Yes";
else
cout << "No";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void sciar(int a[], int n) {
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
}
vector<int> graph[100];
vector<int> node;
int wtf = 0;
void mk_nd(string a, string b) {
int sz = min(a.size(), b.size());
for (int i = 0; i < sz; i++) {
if (a[i] != b[i]) {
int u = a[i] - 'a';
int v = b[i] - 'a';
node.push_back(u);
graph[u].push_back(v);
break;
}
if (a.size() > b.size() && i == sz - 1) wtf = 1;
}
}
int vst[1000];
stack<int> lst;
bool DFS(int sr) {
vst[sr] = 1;
for (int i = 0; i < graph[sr].size(); i++) {
int nd = graph[sr][i];
if (vst[nd] == 0)
DFS(nd);
else if (vst[nd] == 1) {
wtf = 1;
}
}
lst.push(sr);
vst[sr] = 2;
}
int main() {
int n;
scanf("%d", &n);
string st1, st2;
cin >> st1;
n--;
while (n--) {
cin >> st2;
mk_nd(st1, st2);
st1 = st2;
}
for (int i = 0; i < node.size(); i++) {
if (vst[node[i]] == 0) DFS(node[i]);
}
if (wtf == 1) {
cout << "Impossible";
return 0;
}
map<char, int> mp;
while (!lst.empty()) {
char x = lst.top() + 'a';
cout << x;
mp[x] = 1;
lst.pop();
}
for (char i = 'a'; i <= 'z'; i++) {
if (mp[i] != 1) cout << i;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, S;
struct Parcel {
int in, out, w, s, v;
bool operator<(const Parcel &other) const {
if (out != other.out) return out < other.out;
return in > other.in;
}
} parcel[505];
int dp[1005];
int val[505][1005];
int main() {
scanf("%d%d", &n, &S);
for (int i = 0; i < n; ++i)
scanf("%d%d%d%d%d", &parcel[i].in, &parcel[i].out, &parcel[i].w,
&parcel[i].s, &parcel[i].v);
parcel[n].in = 0;
parcel[n].out = 2 * n;
parcel[n].w = 0;
parcel[n].s = S;
parcel[n].v = 0;
sort(parcel, parcel + n + 1);
for (int i = 0; i <= n; ++i) {
for (int w = 0; w <= 1000 - parcel[i].w && w <= parcel[i].s; ++w) {
memset(dp, 0, 4008);
for (int j = 0; j < i; ++j) {
if (j && parcel[j].out > parcel[j - 1].out) {
for (int k = parcel[j - 1].out; k < parcel[j].out; ++k)
if (dp[k] > dp[k + 1]) dp[k + 1] = dp[k];
}
if (parcel[j].in < parcel[i].in) continue;
int newval = val[j][w] + dp[parcel[j].in];
if (newval > dp[parcel[j].out]) dp[parcel[j].out] = newval;
}
if (i)
val[i][w + parcel[i].w] = dp[parcel[i - 1].out];
else
val[i][w + parcel[i].w] = 0;
val[i][w + parcel[i].w] += parcel[i].v;
}
for (int w = parcel[i].s + parcel[i].w; w <= 1000; ++w)
val[i][w + 1] = val[i][w];
}
printf("%d\n", val[n][S]);
}
| 9 |
#include <bits/stdc++.h>
char a[3000005], b[3000005];
char s[3000005];
int fix[30], map[30], used[30];
int doEqual(int k, int n) {
for (int i = 1; i <= k; i++) {
map[i] = fix[i] = used[i] = 0;
}
for (int i = 1; i <= n; i++) {
int u = s[i] - 'a' + 1;
int v = a[i] - 'a' + 1;
if (fix[u]) {
if (map[u] != v) return 0;
} else {
if (used[v]) return 0;
used[v] = 1;
fix[u] = 1;
map[u] = v;
}
}
return 1;
}
void fullFill(int k) {
printf("YES\n");
for (int i = 1; i <= k; i++) {
if (fix[i] == 0) {
int find = -1;
for (int j = 1; j <= k; j++) {
if (!used[j]) {
find = j;
break;
}
}
used[find] = 1;
fix[i] = 1;
map[i] = find;
}
printf("%c", map[i] - 1 + 'a');
}
printf("\n");
}
int getSmaller(int p, int k, int n) {
for (int i = p; i <= n; i++) {
int u = s[i] - 'a' + 1, v = b[i] - 'a' + 1;
if (fix[u]) {
if (map[u] > v) return 0;
if (map[u] < v) return 1;
} else {
int find = -1;
for (int j = 1; j <= v - 1; j++) {
if (!used[j]) {
find = j;
break;
}
}
if (find != -1) {
used[find] = 1;
fix[u] = 1;
map[u] = find;
return 1;
} else {
if (used[v])
return 0;
else {
used[v] = 1;
fix[u] = 1;
map[u] = v;
}
}
}
}
return 1;
}
int getBigger(int p, int k, int n) {
for (int i = p; i <= n; i++) {
int u = s[i] - 'a' + 1, v = a[i] - 'a' + 1;
if (fix[u]) {
if (map[u] < v) return 0;
if (map[u] > v) return 1;
} else {
int find = -1;
for (int j = v + 1; j <= k; j++) {
if (!used[j]) {
find = j;
break;
}
}
if (find != -1) {
used[find] = 1;
fix[u] = 1;
map[u] = find;
return 1;
} else {
if (used[v])
return 0;
else {
used[v] = 1;
fix[u] = 1;
map[u] = v;
}
}
}
}
return 1;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int k;
scanf("%d%s%s%s", &k, s + 1, a + 1, b + 1);
int n = strlen(s + 1);
int p = 0;
while (p + 1 <= n && a[p + 1] == b[p + 1]) p++;
for (int i = 1; i <= k; i++) used[i] = fix[i] = map[i] = 0;
if (!doEqual(k, p))
printf("NO\n");
else {
if (p == n) {
fullFill(k);
} else {
p++;
int u = s[p] - 'a' + 1;
int va = a[p] - 'a' + 1, vb = b[p] - 'a' + 1;
if (fix[u]) {
if (map[u] < va || map[u] > vb)
printf("NO\n");
else if (map[u] > va && map[u] < vb) {
fullFill(k);
} else {
if (map[u] == va) {
if (getBigger(p + 1, k, n)) {
fullFill(k);
} else
printf("NO\n");
} else {
if (getSmaller(p + 1, k, n)) {
fullFill(k);
} else
printf("NO\n");
}
}
} else {
int find = -1;
for (int i = va + 1; i <= vb - 1; i++) {
if (!used[i]) {
find = i;
break;
}
}
if (find != -1) {
used[find] = 1;
fix[u] = 1;
map[u] = find;
fullFill(k);
} else {
int find = -1;
if (!used[va]) {
used[va] = 1;
fix[u] = 1;
map[u] = va;
if (getBigger(p + 1, k, n)) find = 1;
}
if (find == -1) {
doEqual(k, p - 1);
if (!used[vb]) {
used[vb] = 1;
fix[u] = 1;
map[u] = vb;
if (getSmaller(p + 1, k, n)) find = 1;
}
}
if (find != -1) {
fullFill(k);
} else
printf("NO\n");
}
}
}
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
double hi;
double b_h(double hi2, double b1) {
double b2, ret;
b2 = (hi2 / hi) * (b1 / 2);
ret = b1 - 2 * b2;
return ret;
}
int main() {
double base, area, seg, ang, ans;
int i, j, k, n;
cin >> n >> hi;
vector<double> ara;
base = 1.0;
area = hi * 0.5;
seg = area / (double)n;
double lo, up, mid;
lo = 0.0;
up = hi;
ans = 0.0;
for (i = 0; i < n - 1; i++) {
double lo, up, mid = 0.0;
lo = 0.0;
up = hi;
if (i > 0) up = ara[i - 1];
while (lo <= up) {
mid = (lo + up) / 2.0;
ans = (0.5 * (base + b_h(mid, base)) * mid);
if (fabs(ans - seg) <= 0.000000000001) {
base = b_h(mid, base);
break;
} else if ((seg - ans) > 0.000000000001) {
lo = mid + 0.000000000001;
} else {
up = mid - 0.000000000001;
}
}
if (i == 0)
ara.push_back(hi - mid);
else
ara.push_back(ara[i - 1] - mid);
hi = ara[i];
}
reverse(ara.begin(), ara.end());
for (i = 0; i < ara.size(); i++)
cout << fixed << setprecision(12) << ara[i] << " ";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
int k;
cin >> k;
int a[26], mx = 0;
for (int i = 0; i < 26; ++i) {
cin >> a[i];
mx = max(mx, a[i]);
}
long long int w = 0;
for (int i = 0; i < s.length(); ++i) w += (i + 1) * a[s[i] - 'a'];
for (int i = s.length(); i < s.length() + k; ++i) w += (i + 1) * mx;
cout << w;
}
int main() {
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
map<int, bool> mp;
int main() {
int n;
cin >> n;
if (n % 2 == 0) {
int ans = n / 2;
ans *= n;
cout << ans << endl;
for (int i = 1; i <= n; i++) {
bool test;
if (i % 2 != 0)
test = true;
else
test = false;
for (int j = 1; j <= n; j++) {
if (test) {
if (j % 2 == 0)
cout << '.';
else
cout << 'C';
} else {
if (j % 2 == 0)
cout << 'C';
else
cout << '.';
}
}
cout << endl;
}
} else {
int ans = (n / 2 + 1);
ans *= (n / 2 + 1);
ans += (n / 2 * (n / 2));
cout << ans << endl;
for (int i = 1; i <= n; i++) {
bool test;
if (i % 2 != 0)
test = true;
else
test = false;
for (int j = 1; j <= n; j++) {
if (test) {
if (j % 2 == 0)
cout << '.';
else
cout << 'C';
} else {
if (j % 2 == 0)
cout << 'C';
else
cout << '.';
}
}
cout << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
const int N = 505;
char str[N];
int cnt;
int f[N][1 << 16];
void updata(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void gett(int ch, int f[]) {
int pos, x;
if (isupper(ch)) {
pos = ch - 'A', x = 1;
} else {
pos = ch - 'a', x = 0;
}
int opt = 0;
for (int mask = 0; mask < 1 << 4; ++mask)
if ((mask >> pos & 1) == x) opt |= 1 << mask;
updata(f[opt], 1);
}
int g1[1 << 16], g2[1 << 16], g3[1 << 16];
void fwtor(int f1[], int f2[], int f3[]) {
for (int i = 0; i < 1 << 16; ++i) {
g1[i] = f1[i];
g2[i] = f2[i];
}
for (int i = 0; i < 16; ++i)
for (int j = 0; j < 1 << 16; ++j)
if (!(j >> i & 1)) {
updata(g1[j | (1 << i)], g1[j]);
updata(g2[j | (1 << i)], g2[j]);
}
for (int i = 0; i < 1 << 16; ++i) g3[i] = (long long)g1[i] * g2[i] % P;
for (int i = 0; i < 16; ++i)
for (int j = 0; j < 1 << 16; ++j)
if ((j >> i & 1)) {
updata(g3[j], P - g3[j ^ (1 << i)]);
}
for (int i = 0; i < 1 << 16; ++i) updata(f3[i], g3[i]);
}
void fwtand(int f1[], int f2[], int f3[]) {
for (int i = 0; i < 1 << 16; ++i) {
g1[i] = f1[i];
g2[i] = f2[i];
}
for (int i = 0; i < 16; ++i)
for (int j = 0; j < 1 << 16; ++j)
if (!(j >> i & 1)) {
updata(g1[j], g1[j | (1 << i)]);
updata(g2[j], g2[j | (1 << i)]);
}
for (int i = 0; i < 1 << 16; ++i) g3[i] = (long long)g1[i] * g2[i] % P;
for (int i = 0; i < 16; ++i)
for (int j = 0; j < 1 << 16; ++j)
if ((j >> i & 1)) {
updata(g3[j ^ (1 << i)], P - g3[j]);
}
for (int i = 0; i < 1 << 16; ++i) updata(f3[i], g3[i]);
}
int solve(int l, int r) {
int Q;
int flag = 0;
if (l == r) {
Q = ++cnt;
if (str[l] != '?') {
gett(str[l], f[Q]);
} else {
for (int i = 0; i < 4; ++i) {
gett('a' + i, f[Q]);
gett('A' + i, f[Q]);
}
}
flag = 1;
} else {
int sum = 0;
for (int i = l; i <= r; ++i) {
if (str[i] == '(')
++sum;
else if (str[i] == ')')
--sum;
else if (sum == 0) {
Q = ++cnt;
flag = 1;
int z1 = solve(l, i - 1);
int z2 = solve(i + 1, r);
if (str[i] != '&') fwtor(f[z1], f[z2], f[Q]);
if (str[i] != '|') fwtand(f[z1], f[z2], f[Q]);
break;
}
}
}
if (flag == 0 && str[l] == '(' && str[r] == ')') Q = solve(l + 1, r - 1);
return Q;
}
int main() {
scanf("%s", str);
int m = solve(0, strlen(str) - 1);
int n;
scanf("%d", &n);
int opt1 = 0, opt2 = 0;
for (int i = 1; i <= n; ++i) {
int mask = 0;
for (int j = 0; j < 4; ++j) {
int a;
scanf("%d", &a);
mask |= a << j;
}
int q;
scanf("%d", &q);
opt1 |= 1 << mask;
opt2 |= q << mask;
}
int ans = 0;
for (int mask = 0; mask < 1 << 16; ++mask)
if ((mask & opt1) == opt2) updata(ans, f[m][mask]);
printf("%d\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m, a, b, ans = 0;
cin >> n >> m;
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m; j++) {
cin >> a >> b;
if (a + b) ans++;
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define umap unordered_map
#define Ff(i, x, y) for(ll i=x;i<y;i++)
#define Fr(i, x, y) for(ll i=x;i>=y;i--)
#define Ffit(it, x) for(auto it=x.begin();it != x.end();it++)
#define Frit(it, x) for(auto it=x.rbegin();it != x.rend();it++)
#define mem(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define mp make_pair
#define vii vector<int>
#define umii unordered_map<int,int>
#define usi unordered_set<int>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define I first
#define II second
#define MOD 1000000007
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define test ll t;cin>>t;while(t--)
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
int32_t main()
{
c_p_c();
test
{
int x,y,x1=0,x2=0,y1=0,y2=0,flag=0; cin>>x>>y; string s; cin>>s;
Ff(i,0,s.size()){
if(s[i]=='U')y1++;
else if(s[i]=='D')y2++;
else if(s[i]=='R')x1++;
else x2++;
}
if(x>=0)
{
if(x1>=x)
{
if((y>=0 && y1>=y) || (y<=0 && y2>=abs(y)))flag=1;
}
}
else
{
if(x2>=abs(x))
{
if((y>=0 && y1>=y) || (y<=0 && y2>=abs(y)))flag=1;
}
}
if(flag)cout << "YES\n";
else cout << "NO\n";
}
return 0;
} | 0 |
#include <bits/stdc++.h>
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
const int ms = 250500;
std::queue<int> q;
bool got[ms];
long long p[ms], r[ms], dist[ms], mass[ms];
template <class T = long long>
struct FenwickTree {
public:
FenwickTree(std::vector<T> pts) {
std::sort(pts.begin(), pts.end());
pts.resize(std::unique(pts.begin(), pts.end()) - pts.begin());
ord = pts;
fw.resize(ord.size() + 1);
}
void upd(T x, T y, int v) {
for (int xx = std::upper_bound(ord.begin(), ord.end(), x) - ord.begin();
xx < fw.size(); xx += xx & -xx) {
fw[xx].insert({y, v});
}
}
void qry(T x, T y) {
for (int xx = std::upper_bound(ord.begin(), ord.end(), x) - ord.begin();
xx > 0; xx -= xx & -xx) {
auto end = fw[xx].lower_bound({y + 1, -1});
for (auto it = fw[xx].begin(); it != end; it++) {
if (!got[it->second]) {
got[it->second] = true;
q.push(it->second);
}
}
fw[xx].erase(fw[xx].begin(), end);
}
}
private:
std::vector<T> ord;
std::vector<std::set<std::pair<T, int>>> fw;
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
long long bx, by, n;
std::cin >> bx >> by >> p[0] >> r[0] >> n;
r[0] *= r[0];
int ans = -1;
q.push(0);
std::vector<long long> pts;
for (int i = 1; i <= n; i++) {
long long x, y;
std::cin >> x >> y >> mass[i] >> p[i] >> r[i];
r[i] *= r[i];
x -= bx;
y -= by;
dist[i] = x * x + y * y;
pts.push_back(mass[i]);
}
FenwickTree<long long> tree(pts);
for (int i = 1; i <= n; i++) {
tree.upd(mass[i], dist[i], i);
}
while (!q.empty()) {
long long curR = r[q.front()], curP = p[q.front()];
q.pop();
ans++;
tree.qry(curP, curR);
}
std::cout << ans << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a, b, n, ans;
long long read() {
long long x = 0, F = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') F = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * F;
}
int main() {
a = read(), b = read();
n = a + b;
ans = 0;
int r;
for (int l = 1; l <= n; l = r + 1) {
r = n / (n / l);
int B = n / l;
int l1 = (a + B) / (B + 1), r1 = a / B, l2 = (b + B) / (B + 1), r2 = b / B;
if (l1 <= r1 && l2 <= r2)
ans += max(0, min(r1 + r2, r) - max(l1 + l2, l) + 1);
}
printf("%d", ans);
}
| 9 |
#include <bits/stdc++.h>
int main() {
int tests;
std::cin >> tests;
for (int t = 0; t < tests; ++t) {
int n, k;
std::cin >> n >> k;
std::vector<std::vector<int>> sets(k);
for (int i = 0; i < k; ++i) {
int c;
std::cin >> c;
sets[i].resize(c);
for (int& j : sets[i]) std::cin >> j;
}
std::cout << "? " << n;
for (int i = 1; i <= n; ++i) std::cout << ' ' << i;
std::cout << std::endl;
int max;
std::cin >> max;
int i = 0;
if (k > 1) {
int s = (k + 1) / 2;
do {
int c = 0;
for (int j = 0; j < s; ++j) c += sets[i + j].size();
std::cout << "? " << c;
for (int j = 0; j < s; ++j)
for (int h : sets[i + j]) std::cout << ' ' << h;
std::cout << std::endl;
int x;
std::cin >> x;
if (x != max) i += s;
if (s == 1) break;
s = (s + 1) / 2;
s = std::min(s, k - i);
} while (true);
}
int max2;
if (i < k) {
std::cout << "? " << n - sets[i].size();
std::vector<bool> used(n + 1, true);
for (int j : sets[i]) used[j] = false;
for (int j = 1; j <= n; ++j)
if (used[j]) std::cout << ' ' << j;
std::cout << std::endl;
std::cin >> max2;
} else {
max2 = max;
}
std::cout << "! ";
for (int j = 0; j < k; ++j)
if (i == j)
std::cout << max2 << ' ';
else
std::cout << max << ' ';
std::cout << std::endl;
std::string answer;
std::cin >> answer;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<long> a;
vector<long> b;
int nA, nB;
cin >> nA >> nB;
int k, m;
cin >> k >> m;
for (int i = 1; i <= nA; i++) {
int x;
cin >> x;
a.push_back(x);
}
for (int i = 1; i <= nB; i++) {
int x;
cin >> x;
b.push_back(x);
}
int dem = 0;
if (a[k - 1] >= b[nB - m]) {
cout << "NO";
} else
cout << "YES";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
struct SegTree {
int n, M;
vector<int> lz, tr;
SegTree() {}
SegTree(int n, int M, int *a) : n(n), M(M), lz(4 * n), tr(4 * n) {
build(1, 1, n, a);
}
int combine(int left, int right) {
int ans = left + right;
if (ans >= M) ans -= M;
return ans;
}
void propagate(int u, int st, int en) {
if (lz[u] == 1) return;
tr[u] = (1LL * tr[u] * lz[u]) % M;
if (st != en) {
lz[2 * u] = (1LL * lz[u] * lz[2 * u]) % M;
lz[2 * u + 1] = (1LL * lz[u] * lz[2 * u + 1]) % M;
}
lz[u] = 1;
}
void build(int u, int st, int en, int *a) {
if (st == en) {
tr[u] = a[st] % M;
lz[u] = 1;
} else {
int mid = (st + en) / 2;
build(2 * u, st, mid, a);
build(2 * u + 1, mid + 1, en, a);
tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
lz[u] = 1;
}
}
void update(int u, int st, int en, int l, int r, int x) {
propagate(u, st, en);
if (r < st || en < l)
return;
else if (l <= st && en <= r) {
lz[u] = (1LL * lz[u] * x) % M;
propagate(u, st, en);
} else {
int mid = (st + en) / 2;
update(2 * u, st, mid, l, r, x);
update(2 * u + 1, mid + 1, en, l, r, x);
tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
}
}
void Set(int u, int st, int en, int idx, int val) {
propagate(u, st, en);
if (idx < st || en < idx)
return;
else if (st == en)
tr[u] = val;
else {
int mid = (st + en) / 2;
Set(2 * u, st, mid, idx, val);
Set(2 * u + 1, mid + 1, en, idx, val);
tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
}
}
int query(int u, int st, int en, int l, int r) {
propagate(u, st, en);
if (r < st || en < l) return 0;
if (l <= st && en <= r)
return tr[u];
else {
int mid = (st + en) / 2;
return combine(query(2 * u, st, mid, l, r),
query(2 * u + 1, mid + 1, en, l, r));
}
}
};
struct Fenwick {
int n;
vector<int> ft;
Fenwick() {}
Fenwick(int n) : n(n), ft(n + 1) {}
void update(int idx, int val) {
for (; idx > 0; idx -= idx & -idx) ft[idx] += val;
}
int get(int idx) {
int sum = 0;
for (; idx <= n; idx += idx & -idx) sum += ft[idx];
return sum;
}
void update(int l, int r, int val) {
update(r, val);
update(l - 1, -val);
update(r, 0);
update(l - 1, 0);
}
};
Fenwick ft[10];
vector<int> factor(int x) {
vector<int> pf;
for (int i = 2; i * i <= x; i++) {
if (x % i) continue;
while (x % i == 0) x /= i;
pf.push_back(i);
}
if (x > 1) pf.push_back(x);
return pf;
}
int a[N], b[N];
const int MX = 2e6 + 7;
int power[10][MX];
int n, mod;
vector<int> pf;
long long egcd(long long a, long long b, long long &x, long long &y) {
long long xx = y = 0;
long long yy = x = 1;
while (b) {
long long q = a / b;
long long t = b;
b = a % b;
a = t;
t = xx;
xx = x - q * xx;
x = t;
t = yy;
yy = y - q * yy;
y = t;
}
return a;
}
long long inverse(long long a, long long m) {
long long x, y;
long long g = egcd(a, m, x, y);
if (g > 1) return -1;
return (x % m + m) % m;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> mod;
pf = factor(mod);
int k = pf.size();
for (int i = 0; i < k; i++) {
int p = pf[i];
ft[i] = Fenwick(n);
power[i][0] = 1;
int x = 100000, cnt = 0;
while (x) x /= p, cnt++;
for (int j = 1; j <= cnt * n; j++)
power[i][j] = (1LL * power[i][j - 1] * p) % mod;
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
int x = a[i];
for (int j = 0; j < k; j++) {
int p = pf[j];
int cnt = 0;
while (x % p == 0) x /= p, cnt++;
if (cnt == 0) continue;
ft[j].update(i, i, cnt);
}
b[i] = x;
}
SegTree st(n, mod, a), stp(n, mod, b);
int q;
cin >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int l, r, x;
cin >> l >> r >> x;
st.update(1, 1, n, l, r, x);
for (int i = 0; i < k; i++) {
int p = pf[i];
int cnt = 0;
while (x % p == 0) x /= p, cnt++;
if (cnt == 0) continue;
ft[i].update(l, r, cnt);
}
stp.update(1, 1, n, l, r, x);
} else if (t == 2) {
int k, x;
cin >> k >> x;
int cur = 1;
for (int i = 0; i < pf.size(); i++) {
int p = pf[i];
int cnt = 0;
while (x % p == 0) x /= p, cnt++;
if (cnt) ft[i].update(k, k, -cnt);
int trucnt = ft[i].get(k);
cur = (1LL * cur * power[i][trucnt]) % mod;
}
stp.update(1, 1, n, k, k, inverse(x, mod));
cur = (1LL * cur * stp.query(1, 1, n, k, k)) % mod;
st.Set(1, 1, n, k, cur);
} else if (t == 3) {
int l, r;
cin >> l >> r;
cout << st.query(1, 1, n, l, r) << "\n";
}
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
const int mo = 1e9 + 9;
const int S = 383008016;
const int S_inv = 276601605;
const int A = 691504013;
const int B = 308495997;
int n, m, v[N], pA[N], pB[N];
namespace cipolla {
const int mo = 1e9 + 9;
int a, t;
struct Node {
int x, y;
Node operator*(const Node &b) const {
return (Node){
(int)((1ll * x * b.x % mo + 1ll * y * b.y % mo * t % mo) % mo),
(int)((1ll * x * b.y % mo + 1ll * y * b.x % mo) % mo)};
}
};
int qpow(int a, int b) {
int ans = 1;
for (; b >= 1; b >>= 1, a = 1ll * a * a % mo)
if (b & 1) ans = 1ll * ans * a % mo;
return ans;
}
Node nqpow(Node a, int b) {
Node ans = (Node){1, 0};
for (; b >= 1; b >>= 1, a = a * a)
if (b & 1) ans = ans * a;
return ans;
}
int sqrt(int n) {
a = 1;
srand(time(0));
while (qpow(((t = (1ll * a * a % mo - n) % mo + mo) % mo), (mo - 1) / 2) !=
mo - 1)
a = 1ll * rand() * rand() % mo;
return nqpow((Node){a, 1}, (mo + 1) / 2).x;
}
} // namespace cipolla
namespace seg1 {
int s[N << 2], fir[N << 2], nl, nr;
void add(int u, int p, int l, int r) {
(s[u] += p == 0 ? 0 : p * (pA[r - l + 1] - 1ll) % mo * A % mo) %= mo;
(fir[u] += p) %= mo;
}
void pd(int u, int l, int r) {
if (fir[u]) {
int mid = (l + r) >> 1;
add(u << 1, fir[u], l, mid);
add(u << 1 | 1, 1ll * fir[u] * pA[mid - l + 1] % mo, mid + 1, r);
fir[u] = 0;
}
}
void modify(int u, int l, int r, int ql, int qr) {
if (l == ql && r == qr) {
add(u, 1ll * S_inv * pA[l - nl + 1] % mo, l, r);
return;
}
int mid = (l + r) >> 1;
pd(u, l, r);
if (qr <= mid)
modify(u << 1, l, mid, ql, qr);
else if (ql > mid)
modify(u << 1 | 1, mid + 1, r, ql, qr);
else {
modify(u << 1, l, mid, ql, mid);
modify(u << 1 | 1, mid + 1, r, mid + 1, qr);
}
s[u] = (s[u << 1] + s[u << 1 | 1]) % mo;
}
int qry(int u, int l, int r, int ql, int qr) {
if (l == ql && r == qr) return s[u];
int mid = (l + r) >> 1;
pd(u, l, r);
if (qr <= mid) return qry(u << 1, l, mid, ql, qr);
if (ql > mid) return qry(u << 1 | 1, mid + 1, r, ql, qr);
return (qry(u << 1, l, mid, ql, mid) +
qry(u << 1 | 1, mid + 1, r, mid + 1, qr)) %
mo;
}
} // namespace seg1
namespace seg2 {
int s[N << 2], fir[N << 2], nl, nr;
void add(int u, int p, int l, int r) {
(s[u] += p == 0 ? 0 : p * (pB[r - l + 1] - 1ll) % mo * B % mo) %= mo;
(fir[u] += p) %= mo;
}
void pd(int u, int l, int r) {
if (fir[u]) {
int mid = (l + r) >> 1;
add(u << 1, fir[u], l, mid);
add(u << 1 | 1, 1ll * fir[u] * pB[mid - l + 1] % mo, mid + 1, r);
fir[u] = 0;
}
}
void modify(int u, int l, int r, int ql, int qr) {
if (l == ql && r == qr) {
add(u, 1ll * S_inv * pB[l - nl + 1] % mo, l, r);
return;
}
int mid = (l + r) >> 1;
pd(u, l, r);
if (qr <= mid)
modify(u << 1, l, mid, ql, qr);
else if (ql > mid)
modify(u << 1 | 1, mid + 1, r, ql, qr);
else {
modify(u << 1, l, mid, ql, mid);
modify(u << 1 | 1, mid + 1, r, mid + 1, qr);
}
s[u] = (s[u << 1] + s[u << 1 | 1]) % mo;
}
int qry(int u, int l, int r, int ql, int qr) {
if (l == ql && r == qr) return s[u];
int mid = (l + r) >> 1;
pd(u, l, r);
if (qr <= mid) return qry(u << 1, l, mid, ql, qr);
if (ql > mid) return qry(u << 1 | 1, mid + 1, r, ql, qr);
return (qry(u << 1, l, mid, ql, mid) +
qry(u << 1 | 1, mid + 1, r, mid + 1, qr)) %
mo;
}
} // namespace seg2
void solve(int l, int r) {
seg1 ::nl = l;
seg1 ::nr = r;
seg1 ::modify(1, 1, n, l, r);
seg2 ::nl = l;
seg2 ::nr = r;
seg2 ::modify(1, 1, n, l, r);
}
int query(int l, int r) {
return (seg1 ::qry(1, 1, n, l, r) - seg2 ::qry(1, 1, n, l, r) + mo) % mo;
}
int main() {
scanf("%d%d", &n, &m);
pA[0] = pB[0] = 1;
for (int i = 1; i <= n; i++) {
pA[i] = 1ll * pA[i - 1] * A % mo;
pB[i] = 1ll * pB[i - 1] * B % mo;
}
for (int i = 1; i <= n; i++) {
scanf("%d", v + i);
(v[i] += v[i - 1]) %= mo;
}
for (int l, r, op, i = 1; i <= m; i++) {
scanf("%d%d%d", &op, &l, &r);
if (op == 1) solve(l, r);
if (op == 2) {
int ans = (v[r] - v[l - 1] + mo) % mo;
printf("%d\n", (ans + query(l, r)) % mo);
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1200031;
int n, ar[N], w[N];
int gcd(int a, int b) {
while (a && b) a > b ? a %= b : b %= a;
return a + b;
}
vector<pair<int, int> > edges;
vector<int> entries[N];
int sz[N];
long long ANS[N];
vector<int> interesting;
long long cur_pairs;
void run_cleanup() {
for (int i = 0; i < interesting.size(); i++) {
int id = interesting[i];
sz[id] = 1;
w[id] = id;
}
}
int get(int x) {
interesting.push_back(x);
if (x == w[x]) return x;
return w[x] = get(w[x]);
}
long long F(int x) { return 1ll * x * (x - 1) / 2; }
void merge(int a, int b) {
a = get(a);
b = get(b);
if (a == b) return;
cur_pairs -= F(sz[a]);
cur_pairs -= F(sz[b]);
w[a] = b;
sz[b] += sz[a];
cur_pairs += F(sz[b]);
}
vector<int> divs[N];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
edges.push_back(make_pair(a, b));
}
for (int i = 0; i < edges.size(); i++) {
int here = gcd(ar[edges[i].first], ar[edges[i].second]);
entries[here].push_back(i);
}
for (int i = 1; i <= n; i++) {
w[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= 200000; i++) {
interesting.clear();
cur_pairs = 0;
for (int j = i; j <= 200000; j += i) {
for (int q = 0; q < entries[j].size(); q++) {
int id = entries[j][q];
merge(edges[id].first, edges[id].second);
}
}
ANS[i] = cur_pairs;
run_cleanup();
}
for (int i = 1; i <= 200000; i++) {
for (int j = i * 2; j <= 200000; j += i) divs[j].push_back(i);
}
for (int i = 200000; i; --i) {
for (int j = 0; j < divs[i].size(); j++) {
int p = divs[i][j];
ANS[p] -= ANS[i];
}
}
for (int i = 1; i <= n; i++) {
ANS[ar[i]]++;
}
for (int i = 1; i <= 200000; i++) {
if (ANS[i]) cout << i << " " << ANS[i] << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
long long ST[4 * N];
long long st[4 * N], label[4 * N];
long long ct[15][N];
inline int LC(int i) { return 2 * i + 1; }
inline int RC(int i) { return 2 * i + 2; }
long long mod = 1e9 + 9;
long long pow(long long a, int p) {
long long ans = 1ll, tmp = a;
while (p) {
if (p & 1) ans = (ans * tmp) % mod;
tmp = (tmp * tmp) % mod;
p >>= 1;
}
return ans;
}
void update(int L, int R, int i, int a, int b, long long k);
void renew(int L, int R, int i);
pair<long long, int> query(int L, int R, int i, int a, int b);
void build(int L, int R, int i);
char x[N];
int main() {
int n, m, k, a, b, c, t;
scanf("%d%d%d", &n, &m, &k);
scanf("%s", x);
for (int i = 0; i < 10; i++) ct[i][1] = (long long)i;
for (int i = 0; i < 10; i++)
for (int j = 2; j <= n; j++) ct[i][j] = (ct[i][j - 1] * 10ll + i) % mod;
build(0, n - 1, 0);
m += k;
while (m--) {
scanf("%d%d%d%d", &t, &a, &b, &c);
a--;
b--;
if (t == 1)
update(0, n - 1, 0, a, b, c);
else {
if (c >= (b - a + 1))
puts("YES");
else {
int len = (b - a + 1);
bool ok = true;
len %= c;
if (len == 0)
ok = (query(0, n - 1, 0, a, b - c).first ==
query(0, n - 1, 0, a + c, b).first);
else
ok = (query(0, n - 1, 0, a, b - len - c).first ==
query(0, n - 1, 0, a + c, b - len).first) &&
(query(0, n - 1, 0, a, a + c).first ==
query(0, n - 1, 0, b - c, b).first);
if (ok)
puts("YES");
else
puts("NO");
}
}
}
}
void build(int L, int R, int i) {
st[i] = 0ll;
label[i] = 0;
if (L == R) {
ST[i] = (x[L] - '0');
return;
}
int M = (L + R) / 2;
build(L, M, LC(i));
build(M + 1, R, RC(i));
ST[i] = ST[LC(i)] * pow(10ll, R - M) + ST[RC(i)];
ST[i] %= mod;
return;
}
void update(int L, int R, int i, int a, int b, long long k) {
if (a <= L && b >= R) {
if (L == R)
ST[i] = k;
else {
label[i] = 1;
st[i] = k;
ST[i] = ct[k][R - L + 1];
}
return;
}
if (a > R || b < L) return;
int M = (L + R) / 2;
if (label[i] == 1) renew(L, R, i);
update(L, M, LC(i), a, b, k);
update(M + 1, R, RC(i), a, b, k);
ST[i] = ST[LC(i)] * pow(10ll, R - M) + ST[RC(i)];
ST[i] %= mod;
}
void renew(int L, int R, int i) {
label[i] = 0;
long long k = st[i];
if (L == R) return;
int M = (L + R) / 2;
update(L, M, LC(i), L, M, k);
update(M + 1, R, RC(i), M + 1, R, k);
}
pair<long long, int> query(int L, int R, int i, int a, int b) {
if (a <= L && b >= R) return make_pair(ST[i], R - L + 1);
if (a > R || b < L) return make_pair(0ll, 0);
int M = (L + R) / 2;
if (label[i] == 1) renew(L, R, i);
pair<long long, int> tmp1 = query(L, M, LC(i), a, b);
pair<long long, int> tmp2 = query(M + 1, R, RC(i), a, b);
int size = tmp1.second + tmp2.second;
long long ans = tmp1.first * pow(10ll, tmp2.second) + tmp2.first;
ans %= mod;
return make_pair(ans, size);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int read() {
char ch = getchar();
int x = 0, y = 1;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') {
y = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * y;
}
const int N = 3e5 + 5;
struct node {
long long delta;
int id;
} a[N];
int cmp(node a, node b) { return a.delta < b.delta; }
int n, m;
long long sum1[N], sum2[N], ans[N], pls[N];
int x[N], y[N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
a[i].id = i;
x[i] = read(), y[i] = read();
}
for (int i = 1; i <= m; ++i) {
int u = read(), v = read();
long long aa = x[u] + y[v];
long long bb = x[v] + y[u];
if (aa < bb) {
pls[u] += aa;
pls[v] += aa;
} else {
pls[u] += bb;
pls[v] += bb;
}
}
for (int i = 1; i <= n; ++i) a[i].delta = x[i] - y[i];
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; ++i) sum1[i] = sum1[i - 1] + x[a[i].id];
for (int i = n; i >= 1; --i) sum2[i] = sum2[i + 1] + y[a[i].id];
for (int i = 1; i <= n; ++i) {
int id1 = a[i].id;
long long aa = x[id1];
long long bb = y[id1];
if (i > 1) ans[id1] += (i - 1) * bb + sum1[i - 1];
if (i < n) ans[id1] += (n - i) * aa + sum2[i + 1];
}
for (int i = 1; i <= n; ++i) cout << ans[i] - pls[i] << ' ';
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, a[100000];
cin >> n;
map<long long int, long long int> s;
map<long long int, long long int> used;
map<long long int, long long int> start;
map<long long int, long long int> ans;
map<long long int, long long int> last;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
last[a[i]] = i;
start.insert(make_pair(a[i], i));
if (s[a[i]] > 0) {
used[a[i]] = min((i + 1) - s[a[i]], used[a[i]]);
s[a[i]] = i + 1;
} else {
s[a[i]] = i + 1;
used.insert(make_pair(a[i], 1000001));
}
}
for (auto it = used.begin(); it != used.end(); it++) {
if (it->second == 1000001) {
ans.insert(make_pair(it->first, 0));
} else {
bool ok = false;
long long int now = start[it->first];
while (now < n) {
if (a[now] != it->first) {
break;
}
if (now == last[it->first]) {
ok = true;
break;
}
now += it->second;
}
if (ok) {
ans.insert(make_pair(it->first, it->second));
}
}
}
cout << ans.size() << endl;
for (auto it = ans.begin(); it != ans.end(); it++) {
cout << it->first << " " << it->second << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int mod = 1e9 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long powmod(long long a, long long b) {
long long res = 1;
if (a >= mod) a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a;
if (res >= mod) res %= mod;
a = a * a;
if (a >= mod) a %= mod;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
char s[n + 1];
for (long long i = (long long)(1); i < (long long)(n + 1); i++) cin >> s[i];
char curr = '.';
char last = '.';
int start = 0;
int fin = 0;
int flag = 0;
for (long long i = (long long)(1); i < (long long)(n + 1); i++) {
if (s[i] != '.') {
last = curr;
curr = s[i];
}
if (curr == 'L' && last == 'R') {
flag = 1;
fin = i - 1;
break;
}
}
int flag2 = 0;
if (flag == 1) {
for (long long i = (long long)(1); i < (long long)(n + 1); i++) {
if (s[i] != '.') {
start = i;
break;
}
}
} else {
int check = 0, check0 = 0;
;
for (long long i = (long long)(1); i < (long long)(n + 1); i++) {
if (s[i] == 'L' && s[i - 1] == '.' && check == 0) {
fin = i - 1;
check = 1;
}
if (s[i] == '.' && s[i - 1] == 'L' && check0 == 0) {
start = i - 1;
check0 = 1;
}
}
int check1 = 0, check2 = 0;
for (long long i = (long long)(1); i < (long long)(n + 1); i++) {
if (s[i] == 'R' && s[i + 1] == '.' && check1 == 0) {
fin = i + 1;
check = 1;
}
if (s[i] == 'R' && s[i - 1] == '.' && check2 == 0) {
start = i;
check2 = 1;
}
}
}
cout << start << " " << fin << '\n';
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std ;
const int maxn = 6e5 + 10 ;
const int inf = 0x3f3f3f3f ;
struct Dinic
{
int tot ;
int Next[maxn],last[maxn],to[maxn],flow[maxn],q[maxn],dis[maxn],S,T;
void init()
{
tot = 1 ;
memset(last , 0 , sizeof(last)) ;
}
void add(int x,int y,int v) {
Next[++tot]=last[x]; last[x]=tot; to[tot]=y; flow[tot]=v;
}
void auto_add(int x,int y,int w) {
add(x,y,w);
add(y,x,0);
}
bool bfs() {
int l=0,r=1; q[1]=S;
for (int i=1;i<=T;i++) dis[i]=0;
dis[S]=1;
while (l<r) {
int k=q[++l];
for (int i=last[k];i;i=Next[i]) {
if (dis[to[i]]||!flow[i]) continue;
dis[q[++r]=to[i]]=dis[k]+1;
}
}
return dis[T]>0;
}
int dfs(int x,int y) {
if (!y) return 0;
if (x==T) return y;
int sum=0;
for (int i=last[x];i;i=Next[i]) {
if (dis[to[i]]!=dis[x]+1||!flow[i]) continue;
int tmp=dfs(to[i],min(y-sum,flow[i]));
if (tmp) {
sum+=tmp;
flow[i]-=tmp;
flow[i^1]+=tmp;
}
else dis[to[i]]=0;
if (sum==y) return sum;
}
return sum;
}
int dinic() {
int ans=0;
while (bfs()) ans+=dfs(S,inf);
return ans;
}
} dinic ;
int main()
{
std::ios::sync_with_stdio(false) , cin.tie(0) ;
int n ;
cin >> n ;
vector<int> a(n + 1) , b(n + 1) ;
for(int i = 1 ; i <= n ; i ++) cin >> a[i] ;
for(int i = 1 ; i <= n ; i ++) cin >> b[i] ;
dinic.init() ;
dinic.S = n + 1 ;
dinic.T = n + 2 ;
int ans = 0 ;
for(int i = 1 ; i <= n ; i ++)
{
if(b[i] > 0)
{
ans += b[i] ;
dinic.auto_add(dinic.S , i , b[i]) ;
}
else if(b[i] < 0)
{
dinic.auto_add(i , dinic.T , -b[i]) ;
}
}
vector<int> lst(101 , -1) ;
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= 100 ; j ++)
if(a[i] % j == 0)
{
if(lst[j] != -1)
dinic.auto_add(i , lst[j] , inf) ;
}
lst[a[i]] = i ;
}
cout << ans - dinic.dinic() << '\n' ;
return 0 ;
} | 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3, P = 998244353;
int fc[N], ifc[N];
int qp(int a, int b) {
int r = 1;
for (; b; b >>= 1, a = a * 1ll * a % P)
if (b & 1) r = r * 1ll * a % P;
return r;
}
int main() {
int n, m = 0, i, j, k, l, o, s = 0;
scanf("%d%d%d", &n, &k, &o), l = o, fc[0] = 1;
if (n == 1) return puts("0"), 0;
for (i = 1; i < n; ++i) scanf("%d", &j), m += j != l, l = j;
for (i = 1, m += j != o; i <= m; ++i) fc[i] = fc[i - 1] * 1ll * i % P;
for (i = m, ifc[m] = qp(fc[m], P - 2); i; --i)
ifc[i - 1] = ifc[i] * 1ll * i % P;
for (i = m / 2, o = (k - 2) * 1ll * (k - 2) % P, l = qp(k - 2, m - i * 2); ~i;
--i)
s = (s + (fc[m] * 1ll * ifc[i] % P * ifc[(m) - (i)] % P) *
(fc[m - i] * 1ll * ifc[i] % P * ifc[(m - i) - (i)] % P) % P *
l) %
P,
l = l * 1ll * o % P;
printf("%lld",
((qp(k, m) - s) * 1ll * qp(2, P - 2) % P * qp(k, n - m) % P + P) % P);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n;
int l[200005], r[200005];
long long fact[200005];
vector<int> c[200005];
long long dfs(int x, int pre) {
long long ans = 1, tmp = 0;
for (int i = 0; i < c[x].size(); i++) {
if (c[x][i] != pre) {
ans *= dfs(c[x][i], x);
ans %= 998244353;
tmp++;
}
}
if (x > 1)
ans *= fact[tmp + 1];
else {
ans *= fact[tmp];
ans %= 998244353;
ans *= n;
}
ans %= 998244353;
return ans;
}
int main() {
cin >> n;
fact[0] = 1;
for (int i = 1; i <= n + 2; i++) {
fact[i] = fact[i - 1] * i;
fact[i] %= 998244353;
}
for (int i = 1; i < n; i++) {
cin >> l[i] >> r[i];
c[l[i]].push_back(r[i]);
c[r[i]].push_back(l[i]);
}
printf("%lld\n", dfs(1, -1));
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 105;
const long long INF = 1e15 + 5;
const long long MOD = 1e9 + 7;
const long long N = 1e5 + 5;
const double PI = 3.14159265358979323846264338327950288419716939937510;
long long n, k, q[M], s[M], temp[M], p[M], f[M], g[M];
bool eq() {
long long flag = 1;
for (long long i = 1; i <= n; i++) {
if (s[i] != p[i]) flag = 0;
}
if (flag) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (long long i = 1; i <= n; i++) cin >> q[i];
for (long long i = 1; i <= n; i++) cin >> s[i];
long long flag = 0;
for (long long i = 1; i <= n; i++) p[i] = i;
for (long long i = 1; i <= n; i++) f[q[i]] = p[i];
for (long long i = 1; i <= n; i++) g[i] = p[q[i]];
bool r = true;
for (long long i = 1; i <= n; i++) {
if (f[i] != g[i]) r = false;
}
if (eq()) {
cout << "NO\n";
return 0;
}
bool h1 = false;
bool h2 = false;
for (long long i = 1; i <= k; i++) {
for (long long i = 1; i <= n; i++) temp[q[i]] = p[i];
for (long long i = 1; i <= n; i++) p[i] = temp[i];
if (eq()) {
if (r == true && i == k) h1 = true;
if (r == false && ((k - i) % 2) == 0) h1 = true;
break;
}
}
for (long long i = 1; i <= n; i++) p[i] = i;
for (long long i = 1; i <= k; i++) {
for (long long i = 1; i <= n; i++) temp[i] = p[q[i]];
for (long long i = 1; i <= n; i++) p[i] = temp[i];
if (eq()) {
if (r == true && i == k) h2 = true;
if (r == false && ((k - i) % 2) == 0) h2 = true;
break;
}
}
if (h1 || h2)
cout << "YES\n";
else
cout << "NO\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int n, m, s, t, in[N], out[N], nrl[N], d[N], vi[N], a[N];
int tot, first[N], nxt[N], point[N], data[N];
inline int read() {
int f = 1, x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
return x * f;
}
inline void add_edge(int x, int y, int z) {
nxt[++tot] = first[x];
first[x] = tot;
point[tot] = y;
data[tot] = z;
nxt[++tot] = first[y];
first[y] = tot;
point[tot] = x;
data[tot] = 0;
}
bool bfs(int n) {
queue<int> q;
for (int i = 0; i <= n; i++) nrl[i] = first[i], d[i] = 0x3f3f3f3f, vi[i] = 0;
d[s] = 0;
q.push(s);
while (!q.empty()) {
int x = q.front();
q.pop();
vi[x] = 1;
for (int i = first[x]; i != -1; i = nxt[i]) {
int u = point[i];
if (d[u] > d[x] + 1 && data[i]) {
d[u] = d[x] + 1;
if (u == t) return 1;
q.push(u);
}
}
}
return 0;
}
int dfs(int x, int flow) {
if (x == t) return flow;
int sum = 0;
for (int i = nrl[x]; i != -1; i = nxt[i]) {
int u = point[i];
if (d[u] == d[x] + 1 && data[i]) {
int tmp = dfs(u, min(flow, data[i]));
data[i] -= tmp, flow -= tmp;
data[i ^ 1] += tmp, sum += tmp;
if (flow <= 0) break;
}
nrl[x] = nxt[i];
}
return sum;
}
inline int id(int i, int j) { return (i - 1) * (n + 1) + j + 1; }
signed main() {
tot = -1;
memset(first, -1, sizeof(first));
n = read();
m = read();
s = 0;
t = n * (n + 1) + 1;
for (int i = 1; i <= m; i++) {
int u = read(), v = read(), w = read();
in[v] += w, out[u] += w;
for (int j = 0; j < n; j++) add_edge(id(v, j), id(u, j + 1), 0x3f3f3f3f);
}
int MIN = 0x3f3f3f3f, add = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j < n; j++) MIN = min(MIN, j * (out[i] - in[i]));
if (MIN < 0) add = 1 - MIN;
for (int i = 1; i <= n; i++) {
add_edge(s, id(i, 0), 0x3f3f3f3f);
add_edge(id(i, n), t, 0x3f3f3f3f);
for (int j = 0; j < n; j++)
add_edge(id(i, j), id(i, j + 1), j * (out[i] - in[i]) + add);
}
int ans = 0;
while (bfs(t)) ans += dfs(s, 0x3f3f3f3f);
for (int i = 0; i <= tot; i += 2) {
if (vi[point[i]] == vi[point[i ^ 1]] || data[i]) continue;
a[(point[i ^ 1] - 1) / (n + 1) + 1] = (point[i ^ 1] - 1) % (n + 1);
}
for (int i = 1; i <= n; i++) printf("%d ", a[i]);
printf("\n");
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int a[200], b[200];
int main() {
int n, j, i, L, c = 0, m, bl = 0;
cin >> n >> L;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n; i++) cin >> b[i];
for (i = 1; i <= L; i++) {
c = 0;
for (j = 0; j < n; j++) {
if (a[j] == b[j]) c++;
a[j] = a[j] + 1;
if (a[j] >= L) {
a[j] = a[j] % L;
sort(a, a + n);
}
}
if (c == n) {
bl = 1;
break;
}
}
(bl == 1) ? cout << "YES" << endl : cout << "NO" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
int n1 = 0, n0 = 0, n2 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
n0++;
} else if (s[i] == '1') {
n1++;
} else {
n2++;
}
}
for (int i = 0; i < n; i++) {
if (n0 >= n / 3) break;
if (s[i] == '1') {
if (n1 > n / 3) {
s[i] = '0';
n0++;
n1--;
}
}
if (s[i] == '2') {
if (n2 > n / 3) {
s[i] = '0';
n0++;
n2--;
}
}
}
reverse(s.begin(), s.end());
for (int i = 0; i < n; i++) {
if (n2 >= n / 3) {
break;
}
if (s[i] == '1') {
if (n1 > n / 3) {
s[i] = '2';
n2++;
n1--;
}
}
if (s[i] == '0') {
if (n0 > n / 3) {
s[i] = '2';
n2++;
n0--;
}
}
}
for (int i = 0; i < n; i++) {
if (n0 == n / 3 || n1 == n / 3) {
break;
}
if (s[i] == '0') {
s[i] = '1';
n1++;
n0--;
}
}
reverse(s.begin(), s.end());
for (int i = 0; i < n; i++) {
if (n2 == n / 3 || n1 == n / 3) {
break;
}
if (s[i] == '2') {
s[i] = '1';
n1++;
n2--;
}
}
cout << s << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string n, k;
cin >> n >> k;
bool check = false;
int j = 0;
for (int i = 0; i < k.length(); i++) {
if (k[i] == n[j]) {
j++;
if (j == n.length()) {
k = k.substr(i + 1);
check = true;
break;
}
}
}
if (!check) {
cout << 0 << endl;
return 0;
}
j--;
check = false;
for (int i = k.length() - 1; i >= 0; i--) {
if (k[i] == n[j]) {
j--;
if (j < 0) {
k = k.substr(0, i);
check = true;
break;
}
}
}
if (!check) {
cout << 0 << endl;
return 0;
} else {
cout << k.length() + 1 << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
long long f(long long mid) {
long long i;
long long ans = 0;
for (i = 1; i <= n; i++) {
ans += min((mid) / i, m);
}
if (ans >= k) return 1;
return 0;
}
long long f1(long long mid) {
long long i;
long long ans = 0;
for (i = 1; i <= n; i++) {
ans += min((mid) / i, m);
}
if (ans < k) return 1;
return 0;
}
long long f2(long long mid) {
long long i;
long long ans = 0;
for (i = 1; i <= n; i++) {
ans += min((mid) / i, m);
}
if (ans == k) {
for (i = 1; i <= n; i++)
if (mid % i == 0 && i > 1 || mid <= m) return 1;
}
return 0;
}
int main() {
cin >> n >> m >> k;
long long low, high;
high = n * m;
low = 1;
while (high - low > 1) {
long long mid;
mid = (low + high) / 2;
if (f(mid)) {
high = mid;
} else
low = mid;
}
if (f(low))
cout << low << endl;
else
cout << high;
return 0LL;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> let[26];
int n;
const int maxn = 1e5;
int get_id[26][maxn];
string s;
void build() {
for (int i = 0; i < 26; ++i) {
int st = n, id = 0;
if (let[i].size()) st = let[i][0];
for (int j = 0; j < n; ++j) {
if (s[j] - 'a' == i) ++id;
if (id < let[i].size())
st = let[i][id];
else
st = n;
get_id[i][j] = st;
}
}
}
int get_next(int num, char ch) {
if (num == -1) {
if (let[ch - 'a'].size()) return let[ch - 'a'][0];
return n;
}
if (num == n) return n;
return get_id[ch - 'a'][num];
}
void solve() {
int q;
cin >> n >> q;
cin >> s;
for (int i = 0; i < n; ++i) let[s[i] - 'a'].push_back(i);
build();
int dp[3][251][251];
for (int i = 0; i <= 250; ++i)
for (int j = 0; j <= 250; ++j) dp[0][i][j] = n;
for (int i = 0; i <= 250; ++i)
for (int j = 0; j <= 250; ++j) dp[1][i][j] = n;
for (int i = 0; i <= 250; ++i)
for (int j = 0; j <= 250; ++j) dp[2][i][j] = n;
vector<string> st(3);
dp[0][0][0] = -1;
dp[1][0][0] = -1;
dp[2][0][0] = -1;
while (q) {
--q;
char type;
cin >> type;
if (type == '-') {
int num;
cin >> num;
--num;
st[num].erase(st[num].begin() + st[num].size() - 1);
for (int i = 0; i < 251; ++i)
for (int j = 0; j < 251; ++j) dp[num][i][j] = n;
dp[num][0][0] = -1;
for (int i = 0; i <= st[(num + 1) % 3].size(); ++i) {
for (int j = 0; j <= st[(num + 2) % 3].size(); ++j) {
if (i)
dp[num][i][j] =
min(dp[num][i][j],
get_next(dp[num][i - 1][j], st[(num + 1) % 3][i - 1]));
if (j)
dp[num][i][j] =
min(dp[num][i][j],
get_next(dp[num][i][j - 1], st[(num + 2) % 3][j - 1]));
}
}
for (int i = 0; i < st[num].size(); ++i) {
for (int j = 0; j <= st[(num + 1) % 3].size(); ++j) {
for (int k = 0; k <= st[(num + 2) % 3].size(); ++k) {
int val = get_next(dp[num][j][k], st[num][i]);
if (j)
val = min(val,
get_next(dp[num][j - 1][k], st[(num + 1) % 3][j - 1]));
if (k)
val = min(val,
get_next(dp[num][j][k - 1], st[(num + 2) % 3][k - 1]));
dp[num][j][k] = val;
}
}
}
for (int i = 0; i <= st[(num + 2) % 3].size(); ++i)
dp[(num + 1) % 3][i][st[num].size() + 1] = n;
for (int i = 0; i <= st[(num + 1) % 3].size(); ++i)
dp[(num + 2) % 3][st[num].size() + 1][i] = n;
} else {
int num;
char ch;
cin >> num >> ch;
--num;
st[num] += ch;
for (int i = 0; i <= st[(num + 1) % 3].size(); ++i) {
for (int j = 0; j <= st[(num + 2) % 3].size(); ++j) {
int val = get_next(dp[num][i][j], ch);
if (i)
val =
min(val, get_next(dp[num][i - 1][j], st[(num + 1) % 3][i - 1]));
if (j)
val =
min(val, get_next(dp[num][i][j - 1], st[(num + 2) % 3][j - 1]));
dp[num][i][j] = val;
}
}
for (int i = 0; i <= st[(num + 2) % 3].size(); ++i)
dp[(num + 1) % 3][i][st[num].size()] =
dp[num][st[(num + 1) % 3].size()][i];
for (int i = 0; i <= st[(num + 1) % 3].size(); ++i)
dp[(num + 2) % 3][st[num].size()][i] =
dp[num][i][st[(num + 2) % 3].size()];
}
if (dp[0][st[1].size()][st[2].size()] != n)
cout << "YES\n";
else
cout << "NO\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 7 |