solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int N = 1e6 + 5;
int n;
double r;
double a[N];
double dx[N];
double dy[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> r;
for (int i = 1; i <= n; i++) {
cin >> a[i];
for (int j = 1; j < i; j++) {
if (abs(a[i] - a[j]) > 2 * r) continue;
double yy = (r * 2) * (r * 2) - (a[i] - a[j]) * (a[i] - a[j]);
yy = sqrt(yy);
dy[i] = max(dy[i], yy + dy[j]);
}
if (dy[i] == 0) dy[i] = r;
}
for (int i = 1; i <= n; i++) {
cout << fixed << setprecision(9) << dy[i] << " ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct pp {
int a, b, c;
} node[30];
map<pair<int, int>, pair<int, string> > mapp;
int start, endd, mx;
string ans;
void go1(int ind, int suma, int sumb, int sumc, string res) {
if (ind == endd) {
auto it = make_pair(sumb - suma, sumc - sumb);
if (mapp.count(it))
mapp[it] = max(mapp[it], make_pair(suma, res));
else
mapp[it] = make_pair(suma, res);
return;
}
go1(ind + 1, suma + node[ind].a, sumb + node[ind].b, sumc, res + "LM\n");
go1(ind + 1, suma, sumb + node[ind].b, sumc + node[ind].c, res + "MW\n");
go1(ind + 1, suma + node[ind].a, sumb, sumc + node[ind].c, res + "LW\n");
}
void go2(int ind, int suma, int sumb, int sumc, string res) {
if (ind == endd) {
auto it = make_pair(-sumb + suma, -sumc + sumb);
if (mapp.count(it)) {
int temp = suma + mapp[it].first;
if (temp > mx) {
mx = temp;
ans = mapp[it].second + res;
}
}
return;
}
go2(ind + 1, suma + node[ind].a, sumb + node[ind].b, sumc, res + "LM\n");
go2(ind + 1, suma, sumb + node[ind].b, sumc + node[ind].c, res + "MW\n");
go2(ind + 1, suma + node[ind].a, sumb, sumc + node[ind].c, res + "LW\n");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
;
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> node[i].a >> node[i].b >> node[i].c;
start = 0;
endd = n / 2;
go1(start, 0, 0, 0, "");
mx = INT_MIN;
ans = "Impossible\n";
start = endd;
endd = n;
go2(start, 0, 0, 0, "");
cout << ans;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int solve(vector<int>& A, int P, int K) {
int n = A.size();
sort(A.begin(), A.end());
int res = 0;
long long sum = 0;
for (int k = 0; k < K && k < n; ++k) {
int cnt = k;
long long cost = sum;
long long rem = P;
if (cost <= rem) {
rem -= cost;
for (int i = k + K - 1; i < n; i += K) {
if (rem >= A[i]) {
rem -= A[i];
cnt += K;
} else {
break;
}
}
res = max(res, cnt);
} else {
break;
}
sum += A[k];
}
return res;
}
};
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
Solution sol;
int t;
cin >> t;
while (t-- > 0) {
int n, p, k;
cin >> n >> p >> k;
vector<int> A(n);
for (int i = 0; i < n; ++i) {
cin >> A[i];
}
cout << sol.solve(A, p, k) << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << n << " ";
for (int i = 1; i < n; i++) cout << i << " ";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a, b, c;
cin >> a >> b >> c;
cout << max(a, max(b, c)) << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-12L;
const long double pi = acosl(-1);
struct point {
long double x;
long double y;
point(long double _x = 0, long double _y = 0) : x(_x), y(_y) {}
point &operator+=(point other) {
x += other.x;
y += other.y;
return *this;
}
point &operator-=(point other) {
x -= other.x;
y -= other.y;
return *this;
}
point &operator*=(long double other) {
x *= other;
y *= other;
return *this;
}
point &operator/=(long double other) {
x /= other;
y /= other;
return *this;
}
point operator-() { return point(*this) *= -1; }
point operator+(point rhs) { return point(*this) += rhs; }
point operator-(point rhs) { return point(*this) -= rhs; }
point operator*(long double rhs) { return point(*this) *= rhs; }
point operator/(long double rhs) { return point(*this) /= rhs; }
bool operator==(point rhs) {
return (abs(x - rhs.x) < eps && abs(y - rhs.y) < eps);
}
bool operator!=(point rhs) {
return (abs(x - rhs.x) > eps || abs(y - rhs.y) > eps);
}
};
string to_string(point a) {
return "(" + to_string(a.x) + ", " + to_string(a.y) + ")";
}
long double dot(point a, point b) { return a.x * b.x + a.y * b.y; }
long double cross(point a, point b) { return a.x * b.y - a.y * b.x; }
long double dist2(point a, point b) { return dot(a - b, a - b); }
long double dist(point a, point b) { return sqrtl(dist2(a, b)); }
long double norm(point a) { return sqrtl(dot(a, a)); }
long double arg(point a) { return atan2l(a.y, a.x); }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<point> p;
for (int i = 0; i < n; i++) {
point q;
cin >> q.x >> q.y;
if (abs(q.x) < eps && abs(q.y) < eps) {
k--;
} else {
p.emplace_back(q);
}
}
n = (int)p.size();
if (k <= 0) {
cout << 0 << '\n';
return 0;
}
long double lo = 0, hi = 2e5;
while (hi - lo > 1e-6) {
long double mid = (hi + lo) / 2;
vector<long double> add, remove, all;
int now = 0;
for (int i = 0; i < n; i++) {
long double d = norm(p[i]) / 2;
if (d > mid) {
continue;
}
long double a = sqrtl(mid * mid - d * d);
long double t = atanl(a / d);
long double f = atan2l(p[i].y, p[i].x);
long double beg = f - t;
long double end = f + t;
if (beg < -pi) {
beg += 2 * pi;
}
if (end > pi) {
end -= 2 * pi;
}
if (end < beg) {
now++;
}
add.emplace_back(beg);
remove.emplace_back(end);
all.emplace_back(beg);
all.emplace_back(end);
};
sort(add.begin(), add.end());
sort(remove.begin(), remove.end());
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
int ok = 0;
int it1 = 0, it2 = 0;
if (now >= k) {
ok = 1;
}
for (auto v : all) {
while (it1 < (int)add.size() && abs(add[it1] - v) < eps) {
now++;
it1++;
}
while (it2 < (int)remove.size() && abs(remove[it2] - v) < eps) {
now--;
it2++;
}
if (now >= k) {
ok = 1;
}
}
if (ok) {
hi = mid;
} else {
lo = mid;
}
}
cout << fixed << setprecision(12);
cout << hi << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > que;
map<pair<int, int>, int> mm;
int x[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int y[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int main() {
int x0, y0, x1, y1, n, i;
while (scanf("%d%d%d%d", &y0, &x0, &y1, &x1) != EOF) {
scanf("%d", &n);
mm.clear();
while (n--) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
for (i = a; i <= b; i++) mm[make_pair(i, r)] = -1;
}
mm[make_pair(x0, y0)] = 0;
mm[make_pair(x1, y1)] = -1;
que.push(make_pair(x0, y0));
while (!que.empty()) {
int tx = que.front().first;
int ty = que.front().second;
que.pop();
for (i = 0; i < 8; i++)
if (mm[make_pair(tx + x[i], ty + y[i])] < 0) {
mm[make_pair(tx + x[i], ty + y[i])] = mm[make_pair(tx, ty)] + 1;
que.push(make_pair(tx + x[i], ty + y[i]));
}
}
printf("%d\n", mm[make_pair(x1, y1)]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> s;
set<int> read;
const int MAXN = 100010;
int a[MAXN];
void print() {
if (read.empty()) {
puts("Nothing");
return;
}
set<int>::iterator i = read.end();
i--;
printf("%d\n", *i);
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= k; i++) {
s[a[i]]++;
}
for (map<int, int>::iterator it = s.begin(); it != s.end(); it++)
if ((it->second) == 1) read.insert(it->first);
print();
for (int i = k + 1; i <= n; i++) {
if (a[i] != a[i - k]) {
s[a[i - k]]--;
if (s[a[i - k]] == 1)
read.insert(a[i - k]);
else if (s[a[i - k]] == 0)
read.erase(a[i - k]);
s[a[i]]++;
if (s[a[i]] == 1)
read.insert(a[i]);
else if (s[a[i]] == 2)
read.erase(a[i]);
}
print();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int ans[100][100];
int main() {
scanf("%d%d", &n, &m);
if (n + m <= 4) {
if (n == 1 && m == 1) {
cout << 1 << endl;
return 0;
}
printf("-1\n");
return 0;
}
int cur = n * m;
if (cur % 2 == 0) --cur;
if (n >= m) {
for (int i = 0; i < (int)(n); ++i)
for (int j = 0; j < (int)(m); ++j) {
ans[i][j] = cur;
if (cur - 2 == -1) {
cur = n * m;
if (cur % 2 == 1) --cur;
} else {
cur -= 2;
}
}
} else {
for (int j = 0; j < (int)(m); ++j)
for (int i = 0; i < (int)(n); ++i) {
ans[i][j] = cur;
if (cur - 2 == -1) {
cur = n * m;
if (cur % 2 == 1) --cur;
} else {
cur -= 2;
}
}
}
for (int i = 0; i < (int)(n); ++i) {
for (int j = 0; j < (int)(m); ++j) printf("%d ", ans[i][j]);
printf("\n");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
int ans = -1;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
if (x < s) {
if (y != 0) {
ans = max(ans, 100 - y);
} else {
ans = max(ans, 0);
}
} else if (x == s && y == 0) {
ans = max(ans, 0);
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int _sieve_size;
bitset<10000010> bs;
vector<int> primes;
void sieve(int upperbound) {
_sieve_size = upperbound + 1;
bs.reset();
bs.flip();
bs.set(0, false);
bs.set(1, false);
for (int i = 2; i <= _sieve_size; i++)
if (bs.test((size_t)i)) {
for (int j = i * i; j <= _sieve_size; j += i) bs.set((size_t)j, false);
primes.push_back((int)i);
}
}
int main() {
sieve(100);
int i = 0, yes = 0;
bool c = false;
string str;
while (true) {
if (c && primes[i - 1] * primes[i - 1] <= 100) {
cout << primes[i - 1] * primes[i - 1] << endl;
fflush(stdout);
cin >> str;
if (str == "yes") ++yes;
c = false;
} else {
if (i == 19) {
cout << "prime";
fflush(stdout);
return 0;
}
if (yes >= 2) {
cout << "composite";
fflush(stdout);
return 0;
}
cout << primes[i] << endl;
fflush(stdout);
cin >> str;
if (str == "yes") {
++yes;
c = true;
}
++i;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[1000], cnt;
string s;
int main() {
getline(cin, s);
for (int i = 1; i <= s.length() - 2; i += 3) {
a[s[i]] = 1;
}
for (int i = 0; i <= 256; i++) cnt += a[i];
cout << cnt;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, a, b, t, kmin, kmax;
cin >> n >> m;
a = n / m;
b = n % m;
kmin = (a - 1) * a / 2 * (m - b) + a * (a + 1) / 2 * b;
t = n - m + 1;
kmax = (t - 1) * t / 2;
cout << kmin << " " << kmax;
}
| 2 |
#include <bits/stdc++.h>
template <class Num>
void read(Num &x) {
char c;
int flag = 1;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag *= -1;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c - '0');
x *= flag;
}
template <class Num>
void write(Num x) {
if (!x) {
putchar('0');
return;
}
if (x < 0) putchar('-'), x = -x;
static char s[20];
int sl = 0;
while (x) s[sl++] = x % 10 + '0', x /= 10;
while (sl) putchar(s[--sl]);
}
const int maxn = 2005, maxm = 1e5 + 20, nya = -1;
struct Edge {
int v, next;
Edge(int v = 0, int next = 0) : v(v), next(next) {}
} edge[maxm << 1];
int N, M, head[maxn], el;
int snum, set[maxn];
bool hash[maxn], leaf[maxn];
std::vector<int> G[maxn];
int tnum, root[maxn];
void newedge(int u, int v) { edge[el] = Edge(v, head[u]), head[u] = el++; }
void Tarjan(int a, int pre) {
static int dt = 0, low[maxn] = {0}, dfn[maxn] = {0}, stack[maxn] = {0},
top = 0;
static bool flag[maxn] = {false};
hash[a] = true;
low[a] = dfn[a] = ++dt;
stack[++top] = a, flag[a] = true;
for (int i = head[a]; i != nya; i = edge[i].next) {
if ((i ^ 1) == pre) continue;
int p = edge[i].v;
if (!hash[p])
Tarjan(p, i), low[a] = std::min(low[a], low[p]);
else if (flag[p])
low[a] = std::min(low[a], dfn[p]);
}
if (low[a] == dfn[a]) {
++snum;
while (true) {
int p = stack[top--];
flag[p] = false;
set[p] = snum;
if (p == a) break;
}
}
}
void init() {
int u, v;
read(N), read(M);
for (int i = 1; i <= N; i++) head[i] = nya;
for (int i = 1; i <= M; i++) {
read(u), read(v);
newedge(u, v), newedge(v, u);
}
}
void rebuild() {
for (int i = 1; i <= N; i++)
for (int j = head[i]; j != nya; j = edge[j].next) {
int v = set[edge[j].v];
if (set[i] != v) G[set[i]].push_back(v);
}
}
int dfs(int a) {
int ret = 0;
if (G[a].size() > 1) ret = a;
hash[a] = true;
for (int i = 0; i < G[a].size(); i++)
if (!hash[G[a][i]]) ret = std::max(dfs(G[a][i]), ret);
return ret;
}
std::pair<int, int> path(int a, int fa) {
std::pair<int, int> ret = std::make_pair(0, a);
for (int i = 0; i < G[a].size(); i++)
if (G[a][i] != fa && !leaf[G[a][i]]) ret = std::max(ret, path(G[a][i], a));
ret.first++;
return ret;
}
void solve() {
int ans = 0;
for (int i = 1; i <= snum; i++) hash[i] = false;
for (int i = 1; i <= snum; i++)
if (!hash[i]) root[++tnum] = dfs(i);
ans = N + tnum - 1;
for (int i = 1; i <= snum; i++)
if (G[i].size() <= 1) leaf[i] = true, ans--;
for (int i = 1; i <= tnum; i++)
if (root[i]) ans -= path(path(root[i], 0).second, 0).first;
write(ans);
}
int main() {
init();
for (int i = 1; i <= N; i++)
if (!hash[i]) Tarjan(i, M << 1 | 1);
rebuild();
solve();
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 123;
const int N = 2e5 + 123;
const int INF = 1e9 + 1;
const long long INFL = 3e18 + 1;
const double pi = acos(-1.0);
const double eps = 1e-9;
int n, a[101], sum, ans;
inline void boost() {
ios_base ::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
}
inline void solve() {
boost();
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
for (int i = 1; i <= n; i++) {
if ((sum - a[i]) % 2 == 0) ans++;
}
cout << ans;
}
int main() { solve(); }
| 0 |
#include <bits/stdc++.h>
int n, s, c[6], k[6], v[6], t, ans(0x7FFFFFFF);
int main() {
for (scanf("%d%d", &n, &s); n--;) scanf("%d", &t), c[t]++;
for (k[3] = s / (c[3] + c[4] + c[5]); k[3] >= 0; k[3]--)
for (k[4] = (s - c[3] * k[3]) / (c[4] + c[5]); k[4] >= k[3]; k[4]--) {
if ((s - c[3] * k[3] - c[4] * k[4]) % c[5]) continue;
k[5] = (s - c[3] * k[3] - c[4] * k[4]) / c[5];
if (c[5] * k[5] - c[4] * k[4] >= ans) break;
int cur = abs(c[3] * k[3] - c[4] * k[4]) + abs(c[4] * k[4] - c[5] * k[5]);
if (cur < ans) ans = cur, v[3] = k[3], v[4] = k[4], v[5] = k[5];
}
if (ans == 0x7FFFFFFF)
printf("-1");
else
printf("%d %d %d", v[3], v[4], v[5]);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int sum, div, i, j;
float ans, n;
cin >> n;
i = 5, div = 2, sum = 5;
while (true) {
if (n >= 1 && n <= 5) {
ans = n / 1;
break;
} else if (n >= 6 && n <= 15) {
ans = ceil((n - 5) / 2);
break;
} else if (n <= sum + i * 2) {
n = n - sum;
ans = ceil(n / div);
break;
} else {
i = i * 2;
sum = sum + i;
div = div * 2;
}
}
switch ((int)ans) {
case 1:
cout << "Sheldon" << endl;
break;
case 2:
cout << "Leonard" << endl;
break;
case 3:
cout << "Penny" << endl;
break;
case 4:
cout << "Rajesh" << endl;
break;
case 5:
cout << "Howard" << endl;
break;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
using namespace std;
using namespace chrono;
void solve() {
int64_t n;
cin >> n;
map<int64_t, int64_t> arr;
for (int64_t i = 0; i < n; ++i) {
int64_t a;
cin >> a;
arr[a]++;
}
map<int64_t, int64_t> temp = arr;
arr.clear();
for (int64_t i = 0; i < n - 1; ++i) {
int64_t a;
cin >> a;
temp[a]--;
arr[a]++;
}
for (auto i : temp) {
if (i.second > 0) {
cout << i.first << '\n';
break;
}
}
temp = arr;
for (int64_t i = 0; i < n - 2; ++i) {
int64_t a;
cin >> a;
temp[a]--;
}
for (auto x : temp) {
if (x.second > 0) {
cout << x.first << '\n';
break;
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
auto start1 = high_resolution_clock::now();
int64_t t = 1;
while (t--) {
solve();
}
auto stop1 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop1 - start1);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int d[4];
int main() {
int x, y, n, sx, sy, d1, d2;
cin >> n >> sx >> sy;
while (n--) {
cin >> x >> y;
d1 = x - sx;
d2 = y - sy;
if (d1 > 0)
d[0]++;
else if (d1 < 0)
d[2]++;
if (d2 > 0)
d[3]++;
else if (d2 < 0)
d[1]++;
}
if (d[0] > d[1] && d[0] > d[2] && d[0] > d[3]) {
cout << d[0] << endl;
cout << (sx + 1) << ' ' << sy << endl;
} else if (d[1] > d[2] && d[1] > d[3]) {
cout << d[1] << endl;
cout << sx << ' ' << (sy - 1) << endl;
} else if (d[2] > d[3]) {
cout << d[2] << endl;
cout << (sx - 1) << ' ' << sy << endl;
} else {
cout << d[3] << endl;
cout << sx << ' ' << (sy + 1) << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
stack<int> s;
int n, x, sp, y, ans = 0, over = 0;
cin >> n;
while (n--) {
cin >> x;
if (x == 1)
cin >> sp;
else if (x == 2)
ans += over, over = 0;
else if (x == 3)
cin >> y, s.push(y);
else if (x == 4)
over = 0;
else if (x == 5)
while (!s.empty()) s.pop();
else
over++;
while (!s.empty() && s.top() < sp) s.pop(), ans++;
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, a[100001];
int res = 0;
int f[100002];
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
f[i] = min(min(f[i - 1] + 1, (min(a[i - 1], a[i + 1]) + 1)), a[i]);
}
for (int i = n; i >= 1; --i) {
f[i] = min(f[i + 1] + 1, f[i]);
res = max(res, f[i]);
}
cout << res;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct Line {
long long m, k;
long long v(int x) { return 1ll * m * x + k; }
};
struct LiChao {
int l, r, m;
vector<Line> v;
Line line;
LiChao* ch[2] = {nullptr, nullptr};
LiChao(int l, int r) : l(l), r(r), m(l + r >> 1) {
line.m = 0;
line.k = 1ll << 60;
}
void insert(Line seg) {
if (r - l == 1) {
if (seg.v(l) < line.v(l)) {
line = seg;
}
} else {
if (m == 0) v.push_back(seg);
if (line.m > seg.m) swap(seg, line);
if (line.v(m) > seg.v(m)) {
swap(seg, line);
if (!ch[1]) ch[1] = new LiChao(m, r);
ch[1]->insert(seg);
} else {
if (!ch[0]) ch[0] = new LiChao(l, m);
ch[0]->insert(seg);
}
}
}
long long query(int x) {
if (r - l == 1) return line.v(x);
if (x < m and ch[0])
return min(ch[0]->query(x), line.v(x));
else if (x >= m and ch[1])
return min(ch[1]->query(x), line.v(x));
else
return line.v(x);
}
void erase() {
if (ch[0]) {
if (ch[0]->r - ch[0]->l != 1) ch[0]->erase();
delete ch[0];
}
if (ch[1]) {
if (ch[1]->r - ch[1]->l != 1) ch[1]->erase();
delete ch[1];
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
long long dp[n];
int a[n], b[n], aa, bb;
vector<int> nums[n];
for (int i = (0); i < (n); i++) cin >> a[i];
for (int i = (0); i < (n); i++) cin >> b[i];
for (int i = (0); i < (n - 1); i++) {
cin >> aa >> bb;
nums[aa - 1].push_back(bb - 1);
nums[bb - 1].push_back(aa - 1);
}
LiChao* root[n];
for (int i = (0); i < (n); i++) root[i] = new LiChao(-100004, 100004);
function<void(int, int)> merge = [&](int a, int b) {
if (root[a]->v.size() < root[b]->v.size()) swap(root[a], root[b]);
for (Line A : root[b]->v) {
root[a]->insert(A);
}
root[b]->erase();
};
function<void(int, int)> dfs = [&](int n, int pa) {
bool is = true;
for (int i : nums[n]) {
if (i == pa) continue;
dfs(i, n);
merge(n, i);
is = false;
}
dp[n] = is ? 0 : root[n]->query(a[n]);
root[n]->insert({b[n], dp[n]});
};
dfs(0, -1);
for (int i = (0); i < (n); i++) cout << dp[i] << " \n"[i == n - 1];
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 1000005, mod = 998244353;
int n, ft[MX], dp[MX], res;
string a, l, r, s, t;
vector<int> x, y;
int query(int i) {
int sum = 0;
while (i) {
sum += ft[i];
sum %= mod;
i -= i & -i;
}
return sum;
}
int query(int i, int j) {
int d = query(j) - query(i - 1);
return ((((d) % (mod)) + (mod)) % (mod));
}
void update(int i, int k) {
while (i < MX) {
ft[i] += k;
ft[i] %= mod;
i += i & -i;
}
}
void obtZF(string s, vector<int> &zf) {
int n = s.size();
zf.resize(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) zf[i] = min(r - i + 1, zf[i - l]);
while (i + zf[i] < n && s[zf[i]] == s[i + zf[i]]) zf[i]++;
if (i + zf[i] - 1 > r) l = i, r = i + zf[i] - 1;
}
}
int izq(int i) {
int f = x[l.size() + i];
if (f >= l.size()) return i + l.size();
if (l.size() + i + f == x.size()) return n + 1;
if (s[l.size() + i + f] > s[f]) return i + l.size();
return i + l.size() + 1;
}
int der(int i) {
int f = y[r.size() + i];
if (f >= r.size()) return min(i + (int)r.size(), n);
if (r.size() + i + f == y.size()) return n;
if (t[r.size() + i + f] < t[f]) return min(i + (int)r.size(), n);
return min(i + (int)r.size() - 1, n);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> a >> l >> r;
n = a.size();
s = l + a;
t = r + a;
obtZF(s, x);
obtZF(t, y);
dp[n] = 1;
update(n, 1);
for (int i = n - 1; i >= 0; i--) {
if (a[i] == '0') {
dp[i] = dp[i + 1] * (l == "0");
if (i) update(i, dp[i]);
continue;
}
int l = izq(i), r = der(i);
if (l > r) {
dp[i] = 0;
continue;
}
dp[i] = query(l, r);
if (i) update(i, dp[i]);
}
cout << ((((dp[0]) % (mod)) + (mod)) % (mod)) << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
template <class X, class Y>
void minimize(X &a, const Y &b) {
if (a > b) a = b;
}
template <class X, class Y>
void maximize(X &a, const Y &b) {
if (a < b) a = b;
}
const long maxc = 1000000000 + 10;
const int MN = 1010;
using namespace std;
struct re {
int x, y, r;
re(){};
re(int X, int Y) {
x = X;
y = Y;
};
const bool operator<(const re &a) const { return r > a.r; }
};
int n, g[MN];
re a[MN];
vector<int> e[MN];
long double res;
bool inside(re a, re b) {
return ((long long)a.x - b.x) * (a.x - b.x) +
((long long)a.y - b.y) * (a.y - b.y) <=
((long long)b.r) * b.r;
}
long double area(re a) {
return ((long double)a.r * a.r * 3.141592653589793238462643383279502884);
}
void dfs(int x, int l) {
if (x != 0) res = res + area(a[x]) * g[l];
for (auto v : e[x]) {
dfs(v, l + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = (1); (i <= (n)); i++) cin >> a[i].x >> a[i].y >> a[i].r;
sort(a + 1, a + 1 + n);
for (int i = (1); (i <= (n)); i++) {
bool ok = false;
for (int j = (i - 1); (j >= (1)); j--) {
if (inside(a[i], a[j])) {
e[j].push_back(i);
ok = true;
break;
}
}
if (!ok) e[0].push_back(i);
}
int o1 = 1;
int o2 = 1;
for (int i = (1); (i <= (n)); i++) {
if (o1)
g[i] = 1, o1 = 0;
else if (o2)
g[i] = 1, o2 = 0;
else
g[i] = -1, o1 = 1;
}
dfs(0, 0);
cout << setprecision(10) << res;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int a[1005], br[1005];
vector<pair<int, int> > ans;
bool je[1005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
bool p = 1;
for (int i = 0; i < n - 1; ++i) {
int first, second;
cin >> first >> second;
if (second != n || first == n) {
p = 0;
}
a[i] = first;
}
if (!p) {
cout << "NO" << endl;
return 0;
}
sort(a, a + n - 1);
int last = a[0], tu = 1;
je[a[0]] = 1;
for (int i = 1; i < n - 1; ++i) {
if (a[i] <= i) {
cout << "NO" << endl;
return 0;
}
if (a[i] > a[i - 1]) {
ans.push_back(make_pair(last, a[i]));
last = a[i];
je[a[i]] = 1;
} else {
while (je[tu]) {
tu++;
}
ans.push_back(make_pair(last, tu));
last = tu;
je[tu] = 1;
}
}
ans.push_back(make_pair(last, n));
cout << "YES" << endl;
for (int i = 0; i < ans.size(); ++i) {
cout << ans[i].first << ' ' << ans[i].second << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const size_t MAX_N = 2e5 + 5;
const int mult[2] = {-1, 1};
const int inve[2] = {1, -1};
const int bound[2] = {1, 0};
const int inveb[2] = {0, 1};
const int INF = 2e9;
pair<int, int> operator*(pair<int, int> a, pair<int, int> b) {
return {min(a.first, b.first), min(a.second, b.second)};
}
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
return {a.first + b.first, a.second + b.second};
}
pair<int, int>& operator+=(pair<int, int>& a, pair<int, int> b) {
a.first += b.first;
a.second += b.second;
return a;
}
int n;
pair<int, int> tree[MAX_N << 2], lazy[MAX_N << 2];
inline pair<int, int> get_val(int v) { return tree[v] + lazy[v]; }
void push(int p, pair<int, int> val, int v = 1, int vl = 0, int vr = n) {
if (vl + 1 == vr) {
tree[v] = val;
return;
}
int mid = (vl + vr) >> 1;
if (p < mid)
push(p, val, v << 1, vl, mid);
else
push(p, val, v << 1 | 1, mid, vr);
tree[v] = tree[v << 1] * tree[v << 1 | 1];
}
void add(int l, int r, pair<int, int> val, int v = 1, int vl = 0, int vr = n) {
if (vl >= r || vr <= l) return;
if (l <= vl && vr <= r) {
lazy[v] += val;
return;
}
int mid = (vl + vr) >> 1;
add(l, r, val, v << 1, vl, mid);
add(l, r, val, v << 1 | 1, mid, vr);
tree[v] = get_val(v << 1) * get_val(v << 1 | 1);
}
pair<int, int> query(int l, int r, int v = 1, int vl = 0, int vr = n) {
if (vl >= r || vr <= l) return {INF, INF};
if (l <= vl && vr <= r) return get_val(v);
int mid = (vl + vr) >> 1;
return query(l, r, v << 1, vl, mid) * query(l, r, v << 1 | 1, mid, vr) +
lazy[v];
}
int query(int p) {
return p & 1 ? query(p, p + 1).second : query(p, p + 1).first;
}
int main() {
scanf("%d", &n);
for (int i = 0, a, lst = 0; i < n; ++i) {
scanf("%d", &a);
push(i,
i & 1 ? pair<int, int>(INF, a - lst) : pair<int, int>(a - lst, INF));
lst = a - lst;
}
int q;
scanf("%d", &q);
for (int typ, a, b, k; q--;) {
scanf("%d%d%d", &typ, &a, &b);
++b;
if (typ == 1) {
scanf("%d", &k);
add(a, b, a & 1 ? pair<int, int>(0, k) : pair<int, int>(k, 0));
if ((b ^ a) & 1) {
add(b, n, pair<int, int>(k * mult[b & 1], 0));
add(b, n, pair<int, int>(0, k * inve[b & 1]));
}
} else {
int pre = (a ? query(a - 1) : 0);
int en = (b ? query(b - 1) : 0) + mult[(a ^ b) & 1] * pre;
pair<int, int> tmp = query(a, b);
printf("%d\n", en == ((a ^ b) & 1) &&
tmp.first + inve[a & 1] * pre >= bound[a & 1] &&
tmp.second + mult[a & 1] * pre >= inveb[a & 1]);
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, x, n;
while (scanf("%d", &n) != EOF) {
int sum = 0;
for (i = 1; i <= n; i++) sum = (sum ^ i);
for (i = 1; i < n; i++) {
scanf("%d", &x);
sum = (sum ^ x);
}
printf("%d\n", sum);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void find(long long i, vector<set<long long>> &v, vector<long long> &ans) {
long long cnt = 0, z = 0;
ans.push_back(i);
for (auto &j : v) {
j.erase(i);
if ((long long)((j).size()) == 1) cnt++;
}
if (cnt != 1) return;
for (auto j : v) {
if ((long long)((j).size()) == 1) {
long long val = *(j.begin());
find(val, v, ans);
break;
}
}
}
signed 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;
vector<set<long long>> V(n - 1);
for (long long i = 0; i < n - 1; ++i) {
long long k;
cin >> k;
for (long long j = 0; j < k; ++j) {
long long temp;
cin >> temp;
V[i].insert(temp);
}
}
for (long long i = 1; i <= n; ++i) {
vector<set<long long>> v = V;
vector<long long> ans;
find(i, v, ans);
if ((long long)((ans).size()) == n) {
v = V;
vector<long long> vis(n - 1, 0);
for (long long j = 1; j < n; ++j) {
set<long long> ch;
for (long long k = j; k >= 0; --k) {
ch.insert(ans[k]);
for (long long x = 0; x < n - 1; ++x) {
if (vis[x]) continue;
if (v[x] == ch) {
vis[x] = 1;
}
}
}
}
bool f = 1;
for (long long j = 0; j < n - 1; ++j) {
if (!vis[j]) f = 0;
}
if (f) {
for (auto j : ans) cout << j << " ";
cout << "\n";
break;
}
}
}
}
return 0;
}
| 8 |
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for (int z = 0; z < t; z++) {
int n;
cin>>n;
int m = 1;
int l = 0;
int lc = 0;
for (int i = 1; i <= n; i++) {
int x;
cin>>x;
if (x == l) {
lc++;
m = max(lc, m);
} else {
lc = 1;
l = x;
}
}
cout << m << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e2 + 7;
const int mod = 1e9 + 7;
int n, m;
bool a[N][N];
bool cx[N], cy[N];
int cnt, p;
void ck(int b) {
for (int i = 1; i <= m; i++) {
cy[i] = a[b][i];
}
cnt = 0;
cx[b] = false;
for (int i = 1; i <= n; i++) {
if (i == b) continue;
cx[i] = (a[i][1] != cy[1]);
for (int j = 1; j <= m; j++) {
if (a[i][j]) {
if (cy[j] != cx[i])
continue;
else {
cnt++;
p = i;
break;
}
} else {
if (cy[j] == cx[i])
continue;
else {
cnt++;
p = i;
break;
}
}
}
if (cnt > 1) break;
}
}
bool jg() {
bool flag = false, ok = true;
for (int j = 1; j <= m; j++) {
if (flag) {
if (a[p][j] && cy[j] != cx[p])
ok = false;
else if (!a[p][j] && cy[j] == cx[p])
ok = false;
} else {
if (a[p][j] && cy[j] == cx[p])
flag = true;
else if (!a[p][j] && cy[j] != cx[p])
flag = true;
}
}
return ok;
}
bool sv(int b) {
ck(b);
if (cnt == 0) {
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d", cx[i]);
printf("\n");
for (int i = 1; i <= m; i++) printf("%d", cy[i]);
printf("\n");
return true;
} else if (cnt == 1) {
bool ok = true;
cx[p] = false;
ok = jg();
if (!ok) {
cx[p] = true;
ok = jg();
}
if (ok) {
printf("YES\n");
for (int i = 1; i <= n; i++) {
if (i <= p)
printf("%d", cx[i]);
else
printf("%d", !cx[i]);
}
printf("\n");
for (int i = 1; i <= m; i++) printf("%d", cy[i]);
printf("\n");
}
return ok;
} else
return false;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
if (sv(1))
;
else if (sv(2))
;
else
printf("NO\n");
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct TT {
int a, b, dir;
};
TT tt[1];
long long ff[11], n, m, i, j, k, l, t, s, r, mmax, mmin, num, ans;
long long MOD = 1e9 + 7;
int main() {
scanf("%I64d", &n);
r = n / 7 * 2;
t = n % 7;
if (t > 5)
t -= 5;
else
t = 0;
r += t;
s = n / 7 * 2;
t = n % 7;
if (t > 2) t = 2;
s += t;
printf("%I64d %I64d\n", r, s);
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using namespace std;
int n, m;
string s[2100];
int dp[2100][2100];
pair<int, int> f[2100][2100];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) f[i][j] = {-1, -1};
int mx = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i) dp[i][j] = max(dp[i][j], dp[i - 1][j]);
if (j) dp[i][j] = max(dp[i][j], dp[i][j - 1]);
if (s[i][j] == 'a') dp[i][j]++;
if (i + j + 1 - dp[i][j] <= k) mx = max(mx, (int)i + j + 1);
}
}
int m = n;
if (mx == n + m - 1) {
for (int i = 0; i < mx; i++) cout << "a";
return 0;
}
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j + 1 - dp[i][j] <= k) {
if (i + j + 1 == mx) v.push_back({i, j});
}
}
}
string pas = "";
if (v.size() == 0) {
pas += s[0][0];
v.push_back({0, 0});
}
while (mx--) pas += "a";
while (v.size()) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int mn = 10000;
for (auto k : v) {
int x = k.first;
int y = k.second;
if (x < n - 1) mn = min(mn, (int)s[x + 1][y]);
if (y < n - 1) mn = min(mn, (int)s[x][y + 1]);
if (x == n - 1 && y == n - 1) {
cout << pas;
return 0;
}
}
vector<pair<int, int> > t;
for (auto k : v) {
int x = k.first;
int y = k.second;
if (x < n - 1) {
if (mn == s[x + 1][y]) t.push_back({x + 1, y});
}
if (y < n - 1) {
if (mn == s[x][y + 1]) t.push_back({x, y + 1});
}
}
pas += (char)mn;
v = t;
}
}
| 5 |
#include <bits/stdc++.h>
const int N = 3e5 + 5, inf = 2e9;
using namespace std;
struct node {
int x, y;
} q1[N << 1], q2[N << 2];
int k1[N], k2[N], w[N], df[N], lo[N], ansd[N];
int su1, su2, tot, ext, n;
multiset<int> v[N];
stack<int> st;
namespace shupou {
struct node {
int l, r, mi;
} q[N << 2];
int df[N], dy[N], si[N], er[N], d[N], fa[N], t[N];
int s = 0;
inline void pushup(int x) { q[x].mi = min(q[x << 1].mi, q[(x << 1) | 1].mi); }
void build(int x, int l, int r) {
q[x].l = l;
q[x].r = r;
if (l == r) return (void)(q[x].mi = w[dy[l]]);
int m = (l + r) >> 1;
build(x << 1, l, m);
build((x << 1) | 1, m + 1, r);
pushup(x);
}
void modify(int x, int l, int r, int y) {
if (q[x].r < l || r < q[x].l) return;
if (q[x].l >= l && q[x].r <= r) return (void)(q[x].mi = y);
modify(x << 1, l, r, y);
modify((x << 1) | 1, l, r, y);
pushup(x);
}
int query(int x, int l, int r) {
if (q[x].r < l || r < q[x].l) return inf;
if (q[x].l >= l && q[x].r <= r) return q[x].mi;
return min(query(x << 1, l, r), query((x << 1) | 1, l, r));
}
void dfs1(int x, int f) {
si[x] = 1;
for (int i = k2[x]; i; i = q2[i].x) {
int y = q2[i].y;
if (y == f) continue;
d[y] = d[x] + 1;
fa[y] = x;
dfs1(y, x);
si[x] += si[y];
if (si[er[x]] < si[y]) er[x] = y;
}
}
void dfs2(int x, int to) {
df[x] = ++s;
dy[s] = x;
t[x] = to;
if (!er[x]) return;
dfs2(er[x], to);
for (int i = k2[x]; i; i = q2[i].x)
if (!t[q2[i].y]) dfs2(q2[i].y, q2[i].y);
}
inline pair<int, int> ask(int x, int y) {
int mi = inf;
while (t[x] != t[y]) {
if (d[t[x]] < d[t[y]]) swap(x, y);
mi = min(mi, query(1, df[t[x]], df[x]));
x = fa[t[x]];
}
if (d[x] > d[y]) swap(x, y);
mi = min(mi, query(1, df[x], df[y]));
return make_pair(x, mi);
}
} // namespace shupou
inline int r() {
char h = getchar();
int y = 0;
while (h < '0' || h > '9') h = getchar();
while (h >= '0' && h <= '9') y = y * 10 + h - '0', h = getchar();
return y;
}
inline int rrc() {
char h = getchar();
while (h != 'A' && h != 'C') h = getchar();
return h == 'C';
}
inline void jr1(int a, int b) {
q1[++su1] = (node){k1[a], b};
k1[a] = su1;
}
inline void jr2(int a, int b) {
q2[++su2] = (node){k2[a], b};
k2[a] = su2;
swap(a, b);
q2[++su2] = (node){k2[a], b};
k2[a] = su2;
}
inline void updata(int x) {
if (v[x - n].empty()) return (void)(w[x] = inf);
multiset<int>::iterator it = v[x - n].begin();
w[x] = *it;
}
void tarjan(int x, int fa) {
st.push(x);
df[x] = lo[x] = ++tot;
for (int i = k1[x]; i; i = q1[i].x) {
int y = q1[i].y;
if (y == fa) continue;
if (!df[y]) {
tarjan(y, x);
lo[x] = min(lo[x], lo[y]);
if (lo[y] >= df[x]) {
++ext;
while (st.top() != y) jr2(st.top(), ext), st.pop();
jr2(st.top(), ext), st.pop();
jr2(ext, x);
}
} else
lo[x] = min(lo[x], df[y]);
}
}
signed main() {
n = ext = r();
int m = r(), q = r();
for (int i = 1; i <= n; i++) w[i] = r();
for (int i = 1; i <= m; i++) {
int a = r(), b = r();
jr1(a, b);
jr1(b, a);
}
tarjan(1, 0);
shupou::dfs1(1, 0);
shupou::dfs2(1, 1);
for (int i = 1; i <= n; i++)
if (shupou::fa[i]) v[shupou::fa[i] - n].insert(w[i]);
for (int i = n + 1; i <= ext; i++) updata(i);
shupou::build(1, 1, ext);
w[0] = inf;
int cnt = 0;
while (q--) {
int h = rrc(), a = r(), b = r();
if (!h) {
pair<int, int> ans = shupou::ask(a, b);
printf("%d\n",
min(ans.second, ans.first > n ? w[shupou::fa[ans.first]] : inf));
} else if (w[a] != b) {
multiset<int>::iterator it;
int y = shupou::fa[a];
if (y) {
it = v[y - n].find(w[a]);
v[y - n].erase(it);
v[y - n].insert(b);
int z = w[y];
updata(y);
if (z != w[y]) shupou::modify(1, shupou::df[y], shupou::df[y], w[y]);
}
shupou::modify(1, shupou::df[a], shupou::df[a], b);
w[a] = b;
}
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
std::map<long long, long long> v;
int n, i, a;
long long sum, ma;
int main() {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a;
sum += a;
v[sum]++;
}
for (auto &it : v) {
if (it.second > ma) {
ma = it.second;
}
}
cout << n - ma << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct vertex {
long long inside, edge;
int size = 1;
vector<int> child;
};
int ans = 0;
multiset<long long> st;
vertex g[100009];
void dfs(int v, bool flag, long long deep) {
if (flag) {
for (int i = 0; i < g[v].child.size(); i++) {
dfs(g[v].child[i], 1, 0);
g[v].size += g[g[v].child[i]].size;
}
return;
}
if (deep - g[v].inside > *st.begin()) flag = 1;
st.insert(deep);
for (int i = 0; i < g[v].child.size(); i++) {
dfs(g[v].child[i], flag, deep + g[g[v].child[i]].edge);
g[v].size += g[g[v].child[i]].size;
}
if (flag) ans += g[v].size;
flag = 0;
st.erase(deep);
}
int main() {
ios_base::sync_with_stdio(0);
int n;
st.insert(100000000000000000);
cin >> n;
for (int i = 1; i <= n; i++) cin >> g[i].inside;
for (int i = 2, c; i <= n; i++) {
cin >> c >> g[i].edge;
g[c].child.push_back(i);
}
dfs(1, 0, 0);
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, m, k, x = 1;
cin >> n;
int st = n / 2, end = n / 2;
for (i = 0; i <= n / 2; i++) {
for (j = 0; j < st; j++) cout << "*";
for (k = 0; k < x; k++) cout << "D";
for (m = end; m > 0; m--) cout << "*";
st--;
end--;
x += 2;
cout << endl;
}
x -= 2;
st++;
end++;
for (i = 0; i < n / 2; i++) {
st++;
x -= 2;
end++;
for (j = 0; j < st; j++) cout << "*";
for (k = 0; k < x; k++) cout << "D";
for (m = end; m > 0; m--) cout << "*";
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
const int LG = 21;
const int N = 500005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;
using namespace std;
void dfs(int v, int paint, vector<int> &col,
vector<vector<pair<int, int> > > &graph, vector<int> &need, int &cnt0,
int &cnt1, int &fl) {
col[v] = paint;
if (paint == -1)
cnt0++;
else
cnt1++;
if (need[v] != 0) {
if (paint == need[v])
fl = 1;
else
fl = -1;
}
for (auto p : graph[v]) {
if (col[p.first] == 0) {
dfs(p.first, p.second * paint, col, graph, need, cnt0, cnt1, fl);
}
}
}
int root[N];
vector<int> suns[N];
int cntsame[N];
int sametoroot[N];
int fl[N];
int ans;
int Root(int x) { return root[x] == x ? x : Root(root[x]); }
void complete_data(int v, int FL) {
int x = Root(v);
if (fl[x] != 0) return;
FL *= sametoroot[v];
int took_before = min(cntsame[x], (int)suns[x].size() - cntsame[x]);
ans -= took_before;
fl[x] = FL;
if (FL == 1)
ans += cntsame[x];
else
ans += suns[x].size() - cntsame[x];
}
void Merge(int v, int u, int w) {
int x = Root(v), y = Root(u);
if (x == y) return;
if (suns[x].size() > suns[y].size()) {
swap(v, u);
swap(x, y);
}
w *= sametoroot[v];
w *= sametoroot[u];
if (fl[x] == 0 && fl[y] == 0) {
ans -= min(cntsame[x], (int)suns[x].size() - cntsame[x]);
ans -= min(cntsame[y], (int)suns[y].size() - cntsame[y]);
}
if (fl[x] == 0 && fl[y] != 0) {
complete_data(x, fl[y] * w);
}
if (fl[x] != 0 && fl[y] == 0) {
complete_data(y, fl[x] * w);
}
for (auto u : suns[x]) {
suns[y].push_back(u);
sametoroot[u] *= w;
if (sametoroot[u] == 1) cntsame[y]++;
}
if (fl[x] == 0 && fl[y] == 0) {
ans += min(cntsame[y], (int)suns[y].size() - cntsame[y]);
}
root[x] = y;
}
signed main() {
srand(time(NULL));
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<vector<int> > contains(n);
for (int i = 0; i < k; i++) {
int c;
cin >> c;
for (int j = 0; j < c; j++) {
int x = 0;
cin >> x;
x--;
contains[x].push_back(i);
}
}
vector<vector<pair<int, int> > > graph(k);
vector<int> need(k, 0);
for (int i = 0; i < k; i++) root[i] = i;
for (int i = 0; i < k; i++) suns[i].push_back(i);
for (int i = 0; i < k; i++) cntsame[i] = 1;
for (int i = 0; i < k; i++) sametoroot[i] = 1;
for (int i = 0; i < n; i++) {
if (contains[i].size() == 1) {
if (s[i] == '1') {
need[contains[i][0]] = -1;
complete_data(contains[i][0], -1);
} else {
need[contains[i][0]] = 1;
complete_data(contains[i][0], 1);
}
}
if (contains[i].size() == 2) {
if (s[i] == '0') {
graph[contains[i][0]].push_back({contains[i][1], -1});
graph[contains[i][1]].push_back({contains[i][0], -1});
Merge(contains[i][0], contains[i][1], -1);
} else {
graph[contains[i][0]].push_back({contains[i][1], 1});
graph[contains[i][1]].push_back({contains[i][0], 1});
Merge(contains[i][0], contains[i][1], 1);
}
}
cout << ans << endl;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T S(T a) {
return a * a;
}
template <class T>
inline string tostring(T a) {
ostringstream os("");
os << a;
return os.str();
}
template <typename T>
inline long long tolong(T a) {
long long res;
istringstream os(a);
os >> res;
return res;
}
template <typename T>
inline T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <typename T>
inline T bigmod(T a, T b, T m) {
if (b == 0)
return 1;
else if (b % 2 == 0)
return S(bigmod(a, b / 2, m)) % m;
else
return (a % m * bigmod(a, b - 1, m)) % m;
}
const int inf = (int)1e9 + 5;
const long long linf = (long long)1e16 + 5;
const long long modd = (long long)1e9 + 7;
const int mod = 10000007;
void pr(int f) {
if (f == 1)
cout << "Yes\n";
else
cout << "No\n";
}
map<string, string> ma;
set<int> s;
multiset<string>::iterator it;
deque<char> q;
int vis[55100];
int visited[230];
int main() {
ios_base::sync_with_stdio(0);
long long n;
cin >> n;
long long ar[n];
for (long long i = 0; i < n; i++) cin >> ar[i];
sort(ar, ar + n);
long long ans = 0;
for (long long i = 0; i < n; i++) {
ans += abs((ar[i] - (i + 1)));
}
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N, M, Q;
const int MAXN = 2e5 + 5;
set<int> odd[MAXN], even[MAXN];
struct node {
int l, r;
int mi, mx;
bool flag;
} tr[MAXN << 2];
void update(int id) {
if (tr[id].l == tr[id].r) {
tr[id].flag = tr[id].mi > tr[id].mx;
} else {
if (tr[id << 1].flag && tr[id << 1 | 1].flag &&
(tr[id << 1].mi > tr[id << 1 | 1].mx))
tr[id].flag = true;
else
tr[id].flag = false;
tr[id].mi = min(tr[id << 1].mi, tr[id << 1 | 1].mi);
tr[id].mx = max(tr[id << 1].mx, tr[id << 1 | 1].mx);
}
}
void update_odd(int id, int pos, int val) {
if (tr[id].l == tr[id].r) {
tr[id].mi = val;
update(id);
return;
}
int mid = (tr[id].l + tr[id].r) >> 1;
if (pos <= mid)
update_odd(id << 1, pos, val);
else
update_odd(id << 1 | 1, pos, val);
update(id);
}
void update_even(int id, int pos, int val) {
if (tr[id].l == tr[id].r) {
tr[id].mx = val;
update(id);
return;
}
int mid = (tr[id].l + tr[id].r) >> 1;
if (pos <= mid)
update_even(id << 1, pos, val);
else
update_even(id << 1 | 1, pos, val);
update(id);
}
void build(int id, int l, int r) {
tr[id].l = l;
tr[id].r = r;
tr[id].mi = M + 1;
tr[id].mx = 0;
tr[id].flag = true;
if (l == r) return;
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
}
int main() {
scanf("%d%d%d", &N, &M, &Q);
for (int i = 1; i <= N; i++) {
odd[i].insert(M + 1);
even[i].insert(0);
}
build(1, 1, N);
int x, y;
while (Q--) {
scanf("%d%d", &x, &y);
if (x & 1) {
x = (x + 1) / 2;
y = (y + 1) / 2;
odd[x].insert(y);
update_odd(1, x, *odd[x].begin());
} else {
x /= 2;
y /= 2;
even[x].insert(y);
update_even(1, x, *--even[x].end());
}
if (tr[1].flag)
puts("YES");
else
puts("NO");
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
inline long long int modadd(long long int n, long long int m,
long long int p = 1000000007) {
return ((n + m) % p + p) % p;
}
inline long long int modsub(long long int n, long long int m,
long long int p = 1000000007) {
return ((n - m + p) % p + p) % p;
}
inline long long int modpro(long long int n, long long int m,
long long int p = 1000000007) {
return (((n % p) * (m % p)) % p + p) % p;
}
unsigned long long int powe(long long int first, long long int second) {
unsigned long long int res = 1;
while (second > 0) {
if (second & 1) res = res * first;
second = second >> 1;
first = first * first;
}
return res;
}
long long int modpow(long long int first, long long int second,
long long int p = 1000000007) {
long long int res = 1;
while (second > 0) {
if (second & 1) res = modpro(res, first, p);
second = second >> 1;
first = modpro(first, first, p);
}
return res;
}
inline long long int modInverse(long long int n, long long int p = 1000000007) {
if (n == 1) return 1;
return modpow(n, p - 2, p);
}
inline long long int moddiv(long long int n, long long int m,
long long int p = 1000000007) {
return modpro(n, modInverse(m, p), p);
}
inline long long int modadd3(long long int first, long long int second,
long long int z, long long int p = 1000000007) {
return modadd(modadd(first, second, p), z, p);
}
inline long long int modadd4(long long int first, long long int second,
long long int z, long long int w,
long long int p = 1000000007) {
return modadd(modadd(first, second, p), modadd(z, w, p), p);
}
inline long long int modnCr(long long int fac[], int n, int r,
long long int p = 1000000007) {
if (r == 0) return 1;
return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T>
inline T max3(T first, T second, T z) {
return max(max(first, second), z);
}
template <typename T>
inline T max4(T first, T second, T z, T w) {
return max(max3(first, second, w), z);
}
template <typename T>
inline T min3(T first, T second, T z) {
return min(min(first, second), z);
}
template <typename T>
inline T min4(T first, T second, T z, T w) {
return min(min3(first, second, w), z);
}
template <typename T>
void printArr(T *arr, int s, int n) {
for (int i = s; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
long long int a[200005];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int erer = 1;
for (int erer2 = (1); erer2 < (erer + 1); erer2++) {
int n;
cin >> n;
for (int i = (0); i < (n); i++) cin >> a[i];
long long int first, f;
cin >> first >> f;
long long int ans = 0;
for (int i = (0); i < (n); i++) {
if (a[i] <= first) continue;
long long int tt = (a[i] + f - 1) / (first + f);
ans += (tt * f);
}
cout << ans;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, p, q, r, c, s, x, y;
cin >> n;
k = sqrt(n);
p = k * k;
q = n - p;
r = q / k;
c = (k + r);
s = n - k * c;
if (s > 0) {
cout << 2 * (k + c) + 2 << endl;
;
} else
cout << 2 * (k + c) << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s, t;
int l[200005], r[200005], last[26];
int main() {
ios_base::sync_with_stdio(0);
cin >> s >> t;
n = s.length();
m = t.length();
int it = m - 1;
for (int i = 0; i < 26; i++) last[i] = -1;
for (int is = n - 1; is >= 0; is--) {
l[is] = -1;
if (last[s[is] - 'a'] != -1) l[is] = last[s[is] - 'a'];
if (it >= 0 and t[it] == s[is]) l[is] = it;
if (it >= 0 and s[is] == t[it]) last[s[is] - 'a'] = it--;
}
it = 0;
for (int i = 0; i < 26; i++) last[i] = -1;
for (int is = 0; is <= n - 1; is++) {
r[is] = -1;
if (last[s[is] - 'a'] != -1) r[is] = last[s[is] - 'a'];
if (it < m and t[it] == s[is]) r[is] = it;
if (it < m and s[is] == t[it]) last[s[is] - 'a'] = it++;
}
for (int i = 0; i < n; i++)
if (l[i] == -1 or r[i] == -1 or l[i] > r[i]) {
cout << "No\n";
return 0;
}
cout << "Yes\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m, k, i, j, p, q, x, y, ans = 0, cnt = 0;
string s;
bool flag = false;
cin >> s;
n = s.size();
x = 0;
vector<char> v;
for (i = 0; i < n; i++) {
if (s[i] != '1')
v.push_back(s[i]);
else
x++;
}
for (i = 0; i < v.size(); i++) {
if (flag == false) {
if (v[i] == '2') {
flag = true;
for (j = 0; j < x; j++) cout << 1;
x = 0;
cout << 2;
} else
cout << v[i];
} else
cout << v[i];
}
if (x != 0) {
for (i = 0; i < x; i++) cout << 1;
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string st;
int LAST, FIRST;
int i, p[3020000], t[1020000], l, lf, rt, f[1020000], s, Mn = 1e9, mid;
bool check(int x) {
f[0] = FIRST - 1;
for (i = 1; i <= s; ++i) {
f[i] = t[i];
if (f[i - 1] > t[i] || (p[t[i]] - p[f[i - 1]] < 1 && i > 1) ||
(i == 1 && p[t[1]] < 1))
f[i] = t[i] + x;
if (t[i] > x && p[t[i] - x - 1] - p[f[i - 1]] > 0) return 0;
if (i > 1 && p[f[i - 2]] >= p[t[i] - x - 1]) f[i] = max(f[i], t[i - 1] + x);
}
if (f[s] >= LAST) return 1;
return 0;
}
int main() {
cin >> l;
LAST = l;
FIRST = 1E9;
cin >> st;
st = " " + st;
for (i = 1; i <= l; ++i) {
if (st[i] == 'P') t[++s] = i;
p[i] = p[i - 1];
if (st[i] == '*') p[i]++, LAST = i, FIRST = min(FIRST, i);
}
for (i = l + 1; i <= l * 3 + 1; ++i) p[i] = p[i - 1];
if (s == 1) {
if (p[l] == 0) {
cout << "0 0";
} else {
if ((p[t[1]] > p[LAST] - p[t[1]]) ||
(p[t[1]] == p[LAST] - p[t[1]] && t[1] - FIRST < LAST - t[1]))
cout << p[t[1]] << " " << t[1] - FIRST;
else
cout << p[LAST] - p[t[1]] << " " << LAST - t[1];
}
} else {
cout << p[l];
lf = 0;
rt = l;
while (lf <= rt) {
mid = (lf + rt) >> 1;
if (check(mid))
Mn = min(mid, Mn), rt = mid - 1;
else
lf = mid + 1;
}
cout << " " << Mn;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 1e9;
int n, k;
int maxh, tme;
int st[maxn], ed[maxn];
int h[maxn];
vector<pair<int, int>> H[maxn];
vector<int> G[maxn];
void dfs(int v, int p = -1) {
st[v] = tme;
for (int u : G[v]) {
if (u != p) {
tme++;
h[u] = h[v] + 1;
dfs(u, v);
}
}
ed[v] = tme;
}
int check(int u, int v, int h) {
int mini = min(st[u], st[v]);
int maxi = max(st[u], st[v]);
int ind = upper_bound(H[h].begin(), H[h].end(), make_pair(mini, inf)) -
H[h].begin() - 1;
if (ind == -1) {
return -1;
}
if (ed[H[h][ind].second] >= maxi) {
return H[h][ind].second;
}
return -1;
}
int dis(int v, int u) {
int jad = 0;
int lo = 0, hi = maxh;
while (hi - 1 > lo) {
int mid = (lo + hi) >> 1;
int res = check(v, u, mid);
if (res == -1) {
hi = mid;
} else {
jad = res;
lo = mid;
}
}
return (h[u] - h[jad]) + (h[v] - h[jad]);
}
struct P {
int e = 0;
set<pair<int, int>> S;
void insert(int v) {
if (S.empty()) {
S.insert(make_pair(st[v], v));
return;
}
if (S.size() == 1) {
e += 2 * dis((*S.begin()).second, v);
S.insert(make_pair(st[v], v));
return;
}
auto nxt = S.upper_bound(make_pair(st[v], inf));
auto prv = nxt;
if (nxt == S.end()) {
nxt = S.begin();
prv--;
} else if (nxt == S.begin()) {
prv = S.end();
prv--;
} else {
prv--;
}
e += dis(v, (*nxt).second) + dis((*prv).second, v);
e -= dis((*nxt).second, (*prv).second);
S.insert(make_pair(st[v], v));
}
void erase(int v) {
S.erase(make_pair(st[v], v));
if (S.empty()) {
return;
}
if (S.size() == 1) {
e = 0;
return;
}
auto nxt = S.upper_bound(make_pair(st[v], inf));
auto prv = nxt;
if (nxt == S.end()) {
nxt = S.begin();
prv--;
} else if (nxt == S.begin()) {
prv = S.end();
prv--;
} else {
prv--;
}
e -= dis(v, (*nxt).second) + dis((*prv).second, v);
e += dis((*nxt).second, (*prv).second);
}
};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(0);
for (int v = 0; v < n; v++) {
H[h[v]].push_back({st[v], v});
maxh = max(maxh, h[v]);
}
for (int h = 0; h <= maxh; h++) {
sort(H[h].begin(), H[h].end());
}
P p;
int ans = 0;
int ptr = 0;
for (int l = 0; l < n; l++) {
while (ptr < n && p.e / 2 + 1 <= k) {
p.insert(ptr++);
}
if (p.e / 2 + 1 > k) {
ans = max(ans, ptr - l - 1);
p.erase(l);
} else {
ans = max(ans, ptr - l);
}
}
cout << ans << '\n';
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long k, b, n, t;
cin >> k >> b >> n >> t;
long long x = 1ll;
long long answer = 0;
for (answer = 0; answer < n; ++answer) {
x = k * x + b;
if (t < x) {
break;
}
}
cout << (n - answer) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > G[100100];
long long grundy[100100];
long long pref[100100];
int cnt[100100];
long long get(int i, int j) {
long long ans = pref[j];
if (i) ans ^= pref[i - 1];
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
for (long long i = 3; i < 100100; i++) {
int sq = (sqrt(i) + 10) * 2;
for (long long j = 2; j <= sq; j++) {
long long top = j * (j - 1) + 2 * i;
long long bottom = 1 + j + j - 1;
if (top % bottom != 0) continue;
long long h = top / bottom;
long long l = h - (j - 1);
if (l <= 0) continue;
G[i].push_back(make_pair(l, h));
}
}
grundy[1] = grundy[2] = 0;
pref[1] = pref[2] = 0;
for (long long i = 3; i < 100100; i++) {
int sq = sqrt(i) * 2;
memset(cnt, 0, 4 * sq);
for (int j = 0; j < G[i].size(); j++) {
int l = G[i][j].first, h = G[i][j].second;
int g = get(l, h);
cnt[g] = 1;
}
for (int j = 0; j < sq; j++) {
if (!cnt[j]) {
grundy[i] = j;
break;
}
}
pref[i] = pref[i - 1] ^ grundy[i];
}
long long n;
cin >> n;
if (grundy[n] == 0) return cout << -1 << endl, 0;
int ans = 1e9;
int sq = sqrt(n) * 2;
for (int i = 0; i < G[n].size(); i++) {
int l = G[n][i].first, h = G[n][i].second;
if (get(l, h) == 0) ans = min(ans, h - l + 1);
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int n, c, honey[1005], profit, max, i;
while (scanf("%d%d", &n, &c) != EOF) {
max = 0;
for (i = 0; i < n; i++) scanf("%d", &honey[i]);
for (i = 0; i < n - 1; i++) {
profit = honey[i] - honey[i + 1] - c;
if (max < profit) max = profit;
}
printf("%d\n", max);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2020;
int n, k, p;
int dp[maxn][maxn];
int a[maxn], key[maxn];
int main() {
scanf("%d %d %d", &n, &k, &p);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= k; ++i) scanf("%d", &key[i]);
sort(a + 1, a + 1 + n);
sort(key + 1, key + 1 + k);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= k; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == j)
dp[i][j] = max(dp[i - 1][j - 1], abs(a[j] - key[i]) + abs(p - key[i]));
else
dp[i][j] = min(dp[i - 1][j], max(dp[i - 1][j - 1],
abs(a[j] - key[i]) + abs(p - key[i])));
}
}
printf("%d", dp[k][n]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, i, curr = 1;
vector<int> rez;
string k;
cin >> a >> k;
bool flag = 0;
if (k[0] == 'B') flag = 1;
i = 0;
while (i != a - 1) {
if (k[i] != k[i + 1]) {
if (flag) rez.push_back(curr);
curr = 0;
flag = !flag;
}
curr++;
i++;
}
if (flag) rez.push_back(curr);
cout << rez.size() << endl;
for (int i = 0; i < int(rez.size()); ++i) cout << rez[i] << ' ';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int taka[1000004];
int tt[1000003];
int main() {
taka[0] = 1;
{} {}
int n;
cin >> n;
cout << n * 2 - 1 << " 2" << endl;
cout << "1 2" << endl;
}
| 3 |
#include <bits/stdc++.h>
inline int rd() {
int x = 0, p = 1;
char a = getchar();
while ((a < 48 || a > 57) && a != '-') a = getchar();
if (a == '-') p = -p, a = getchar();
while (a > 47 && a < 58) x = (x << 1) + (x << 3) + (a & 15), a = getchar();
return x * p;
}
template <typename T>
inline T min(T x, T y) {
return x < y ? x : y;
}
const int N = 200002;
struct Edge {
int to, next;
long long flow;
} edge[N << 1];
int head[N], cnt = -1;
int n, s, t;
long long ans;
inline void add(int f, int t, long long w) {
edge[++cnt].next = head[f];
edge[cnt].to = t;
edge[cnt].flow = w;
head[f] = cnt;
}
inline void ins(int u, int v, long long f) { add(u, v, f), add(v, u, 0); }
int h[N], gap[N];
long long e[N];
int vis[N];
struct cmp {
bool operator()(const int &x, const int &y) const { return h[x] < h[y]; }
};
std::priority_queue<int, std::vector<int>, cmp> q;
inline void bfs() {
std::queue<int> q;
for (int i = 1; i <= t; i++) h[i] = (2100000000);
h[t] = 0, q.push(t);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (edge[i ^ 1].flow && h[v] > h[u] + 1) h[v] = h[u] + 1, q.push(v);
}
}
}
inline void push(int u) {
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (edge[i].flow && h[u] == h[v] + 1) {
long long f = min(e[u], edge[i].flow);
edge[i].flow -= f, edge[i ^ 1].flow += f;
e[u] -= f, e[v] += f;
if (v != s && v != t && !vis[v]) q.push(v), vis[v] = 1;
}
if (!e[u]) break;
}
}
inline void relable(int u) {
h[u] = (2100000000);
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (edge[i].flow && h[u] > h[v] + 1) h[u] = h[v] + 1;
}
}
inline long long HLPP() {
bfs();
if (h[s] == (2100000000)) return 0;
h[s] = t;
for (int i = 1; i <= t; i++)
if (h[i] != (2100000000)) gap[h[i]]++;
for (int i = head[s]; i != -1; i = edge[i].next) {
int v = edge[i].to;
long long f = 0;
if ((f = edge[i].flow)) {
edge[i].flow -= f, edge[i ^ 1].flow += f;
e[s] -= f, e[v] += f;
if (v != s && v != t && !vis[v]) q.push(v), vis[v] = 1;
}
}
while (!q.empty()) {
int u = q.top();
q.pop();
vis[u] = 0, push(u);
if (e[u]) {
gap[h[u]]--;
if (!gap[h[u]])
for (int v = 1; v <= t; v++)
if (v != s && v != t && h[v] < t + 1 && h[u] < h[v]) h[v] = t + 1;
relable(u), gap[h[u]]++;
q.push(u), vis[u] = 1;
}
}
return e[t];
}
int main() {
memset(head, -1, sizeof head);
n = rd();
s = n + n + 1, t = s + 1;
for (int i = 1; i <= n; i++) {
int x = rd(), v;
while (x--) {
v = rd();
ins(i, v + n, (210000000000ll));
}
ins(i + n, t, (500000000));
}
for (int i = 1; i <= n; i++) {
int x = rd();
ins(s, i, -x + (500000000));
ans += (-x + (500000000));
}
printf("%lld\n", min(-(ans - HLPP()), 0ll));
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int a[100000];
bool p[10000000];
int main() {
int i, j, k, x, now, an = 0, n;
scanf("%d%d", &n, &x);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
if (x == 2) {
puts("0");
return 0;
}
sort(a, a + n);
if (a[0] == 1) {
puts("1");
return 0;
}
for (i = 2; i < 10000000 && i < x; i++) {
if (!p[i]) {
int *r = lower_bound(a, a + n, i);
if (r == NULL)
break;
else if (*r != i)
break;
else
an++;
for (j = i + i; j < 10000000; j += i) p[j] = 1;
}
}
if (i == x)
printf("%d\n", an);
else
puts("-1");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
map<int, pair<int, int> > lf, rg;
vector<pair<int, int> > ki, ka;
long long dist(pair<int, int> a, pair<int, int> b) {
long long x = abs(a.first - b.first);
long long y = abs(a.second - b.second);
return x + y;
}
long long dp[N + 5][2];
long long rec(int idx, int cur) {
if (idx == ki.size() - 1) return dist(ki[idx], ka[idx]);
long long &res = dp[idx][cur];
if (res != -1) return res;
res = 0;
if (cur == 0) {
res = min(rec(idx + 1, 0) + dist(ka[idx], ki[idx + 1]),
rec(idx + 1, 1) + dist(ka[idx], ka[idx + 1])) +
dist(ki[idx], ka[idx]);
} else {
res = min(rec(idx + 1, 0) + dist(ki[idx], ki[idx + 1]),
rec(idx + 1, 1) + dist(ki[idx], ka[idx + 1])) +
dist(ki[idx], ka[idx]);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp, -1, sizeof dp);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
int mx = max(a, b);
if (lf.count(mx)) {
pair<int, int> tmL = lf[mx], tmR = rg[mx];
if (tmL.first > a)
tmL = pair<int, int>(a, b);
else if (tmL.first == a && tmL.second < b)
tmL = pair<int, int>(a, b);
if (tmR.second > b)
tmR = pair<int, int>(a, b);
else if (tmR.second == b && tmR.first < a)
tmR = pair<int, int>(a, b);
lf[mx] = tmL, rg[mx] = tmR;
} else {
lf[mx] = rg[mx] = pair<int, int>(a, b);
}
}
for (auto x : lf) ki.push_back(x.second);
for (auto x : rg) ka.push_back(x.second);
long long ans = min(dist(pair<int, int>(0, 0), ki[0]) + rec(0, 0),
dist(pair<int, int>(0, 0), ka[0]) + rec(0, 1));
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
string s;
cin >> s;
int cnt = 0;
long long ans = 1;
for (int i = 1; i < s.length() + 1; i++) {
if (s[i - 1] + s[i] == '0' + '9')
cnt++;
else {
if (cnt && cnt % 2 == 0) {
ans *= (cnt / 2 + 1);
}
cnt = 0;
}
}
if (cnt && cnt % 2 == 0) {
ans *= (cnt / 2 + 1);
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long double dp[15][15];
int a[15][15];
pair<int, int> get(int x, int y, int i) {
int dir = 1;
if (x % 2 == 0) dir = -1;
y += dir * i;
if (y > 9) {
x--;
y = 19 - y;
} else if (y < 0) {
x--;
y = abs(y) - 1;
}
return make_pair(x, y);
}
long double go(int x, int y) {
if (x == 0 && y == 0) return 0;
if (dp[x][y] > -1) return dp[x][y];
long double cnt = 0, tot = 0;
long double sum = 0;
for (int i = 1; i <= 6; i++) {
tot++;
pair<int, int> p = get(x, y, i);
int rx = p.first, ry = p.second;
if (rx < 0 || ry < 0) {
cnt++;
continue;
}
sum += min(go(rx, ry), go(rx - a[rx][ry], ry));
}
dp[x][y] = (tot + sum) / (tot - cnt);
return dp[x][y];
}
int main() {
ios_base::sync_with_stdio(false);
for (int i = 0; i <= 9; i++)
for (int j = 0; j <= 9; j++) cin >> a[i][j], dp[i][j] = -1;
cout << fixed << setprecision(10) << go(9, 0);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 228;
const int MAXN = 1e5 + 7;
const int NEUTRAL = -INF;
const long long MOD = 1e9 + 7;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int> > a(n, vector<int>(m)), b(m, vector<int>(n)), ans;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> b[i][j];
}
}
int cur;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (b[i][0] == a[j][0]) {
ans.push_back(a[j]);
cur = i;
}
}
}
for (int i = 1; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (b[cur][i] == a[j][0]) {
ans.push_back(a[j]);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << ans[i][j] << ' ';
}
cout << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
set<pair<long long, long long> > all;
long long what[300005];
int main() {
long long T, l, r, N, M, ans = 0, i, a, b, x, y;
scanf("%lld", &T);
while (T--) {
all.clear();
scanf("%lld %lld", &N, &M);
l = 0;
ans = 0;
for (i = 0; i < N; i++) {
scanf("%lld", &what[i]);
if (i && what[i] > what[i - 1]) {
ans += what[l] - what[i - 1];
all.insert(make_pair(l, i - 1));
l = i;
}
}
ans += what[l] - what[i - 1];
all.insert(make_pair(l, i - 1));
l = i;
printf("%lld\n", ans + what[N - 1]);
while (M--) {
scanf("%lld %lld", &a, &b);
a--;
b--;
auto t = *prev(all.upper_bound(make_pair(a, 1e9)));
if (a != t.first) {
ans -= what[t.first] - what[t.second];
ans += what[t.first] - what[a - 1];
ans += what[a] - what[t.second];
all.erase(t);
all.insert(make_pair(t.first, a - 1));
all.insert(make_pair(a, t.second));
}
t = *prev(all.upper_bound(make_pair(a, 1e9)));
if (a != t.second) {
ans -= what[t.first] - what[t.second];
ans += what[a + 1] - what[t.second];
ans += what[t.first] - what[a];
all.erase(t);
all.insert(make_pair(t.first, a));
all.insert(make_pair(a + 1, t.second));
}
t = *prev(all.upper_bound(make_pair(b, 1e9)));
if (b != t.first) {
ans -= what[t.first] - what[t.second];
ans += what[t.first] - what[b - 1];
ans += what[b] - what[t.second];
all.erase(t);
all.insert(make_pair(t.first, b - 1));
all.insert(make_pair(b, t.second));
}
t = *prev(all.upper_bound(make_pair(b, 1e9)));
if (b != t.second) {
ans -= what[t.first] - what[t.second];
ans += what[b + 1] - what[t.second];
ans += what[t.first] - what[b];
all.erase(t);
all.insert(make_pair(t.first, b));
all.insert(make_pair(b + 1, t.second));
}
swap(what[a], what[b]);
t = *prev(all.upper_bound(make_pair(a, 1e9)));
if (t.first != 0 && what[t.first - 1] >= what[t.first]) {
auto t2 = *prev(all.find(t));
ans -= what[t.first] - what[t.second];
ans -= what[t2.first] - what[t2.second];
ans += what[t2.first] - what[t.second];
all.erase(t);
all.erase(t2);
all.insert(make_pair(t2.first, t.second));
}
t = *prev(all.upper_bound(make_pair(a, 1e9)));
if (t.second != N - 1 && what[t.second] >= what[t.second + 1]) {
auto t2 = *next(all.find(t));
ans -= what[t.first] - what[t.second];
ans -= what[t2.first] - what[t2.second];
ans += what[t.first] - what[t2.second];
all.erase(t);
all.erase(t2);
all.insert(make_pair(t.first, t2.second));
}
t = *prev(all.upper_bound(make_pair(b, 1e9)));
if (t.first != 0 && what[t.first - 1] >= what[t.first]) {
auto t2 = *prev(all.find(t));
ans -= what[t.first] - what[t.second];
ans -= what[t2.first] - what[t2.second];
ans += what[t2.first] - what[t.second];
all.erase(t);
all.erase(t2);
all.insert(make_pair(t2.first, t.second));
}
t = *prev(all.upper_bound(make_pair(b, 1e9)));
if (t.second != N - 1 && what[t.second] >= what[t.second + 1]) {
auto t2 = *next(all.find(t));
ans -= what[t.first] - what[t.second];
ans -= what[t2.first] - what[t2.second];
ans += what[t.first] - what[t2.second];
all.erase(t);
all.erase(t2);
all.insert(make_pair(t.first, t2.second));
}
printf("%lld\n", ans + what[N - 1]);
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x;
cin >> x;
vector<int> v(x);
vector<int> in(x);
for (int i = 0; i < x; i++) {
cin >> v[i];
in[i] = i + 1;
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < x - i - 1; j++) {
if (v[j] > v[j + 1]) {
int t = v[j];
v[j] = v[j + 1];
v[j + 1] = t;
t = in[j];
in[j] = in[j + 1];
in[j + 1] = t;
}
}
}
int la = v.size(), fi = 1;
for (int i = 0; i < x / 2; i++) {
cout << in[fi - 1] << " " << in[la - 1] << endl;
fi++;
la--;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long T;
cin >> T;
while (T--) {
string s;
cin >> s;
long long ans = (long long)s.size(), curr = 0;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == '+') {
curr++;
} else {
curr--;
}
if (curr < 0) {
ans += (i + 1);
curr++;
}
}
cout << ans << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long double PI = acosl(-1);
bool compare_int(int a, int b) { return (a > b); }
bool compare_string(string a, string b) { return a.size() < b.size(); }
bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) {
if (a.second == b.second)
return a.first < b.first;
else
return (a.second > b.second);
}
bool cmp(pair<string, int> x, pair<string, int> y) {
return (x.second < y.second);
}
void NA() {
printf("NO\n");
exit(0);
}
void YA() {
printf("YES\n");
exit(0);
}
bool valid(string s1, string s2, string s3) {
string t;
map<char, int> vis;
for (int i = 0; i < s2.size(); i++) {
for (int j = 0; j < s1.size(); j++) {
if (vis[s1[j]] == 0) t += s1[j];
}
vis[s2[i]]++;
}
return (t == s3);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
string s, ans1, ans2;
cin >> s;
int len = s.size();
map<char, int> cnt;
map<char, bool> vis;
for (int i = len - 1; i >= 0; i--) {
if (vis[s[i]] == false) {
vis[s[i]] = true;
ans2 += s[i];
}
}
bool ok = false;
for (int i = 0; i < len; i++) {
cnt[s[i]]++;
long long int tot = 0, sum = 0;
for (int j = 0; j < ans2.size(); j++) {
if (cnt[ans2[j]] > 0) {
sum += cnt[ans2[j]];
tot += sum;
} else {
tot = 0;
break;
}
}
if (tot == len) {
ans1 = s.substr(0, i + 1);
reverse(ans2.begin(), ans2.end());
ok = true;
break;
}
}
if (ok and valid(ans1, ans2, s))
cout << ans1 << ' ' << ans2 << "\n";
else
cout << -1 << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b, ans = 0;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &a, &b);
if (a > b) ans++;
if (b > a) ans--;
}
if (ans > 0) printf("Mishka");
if (ans == 0) printf("Friendship is magic!^^");
if (ans < 0) printf("Chris");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[505];
scanf("%s", s);
int n = strlen(s), m = -1, f = 1;
for (int i = 0; i < n; i++) {
int x = s[i] - 'a';
if (x > m + 1) {
f = 0;
break;
} else if (x == m + 1)
m++;
}
if (f)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int lower(long long int low, long long int high, long long int a[],
long long int x) {
if (low <= high) {
long long int mid = (low + high) / 2;
if (a[mid] < x) {
return lower(mid + 1, high, a, x);
} else
return lower(low, mid - 1, a, x);
}
return high;
}
int main() {
int n, m, k, t;
cin >> n >> m >> k >> t;
long long int x, y;
long long int result[k];
for (int i = 0; i < k; i++) {
cin >> x >> y;
x--, y--;
result[i] = x * m + y;
}
sort(result, result + k);
long long int a, b;
for (int i = 0; i < t; i++) {
cin >> a >> b;
a--, b--;
if (binary_search(result, result + k, a * m + b)) {
cout << "Waste" << endl;
} else {
long long int r = lower(0, k, result, a * m + b) + 1;
int j = (a * m + b - r) % 3;
if (j == 0) {
cout << "Carrots" << endl;
} else if (j == 1) {
cout << "Kiwis" << endl;
} else {
cout << "Grapes" << endl;
}
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int a[5005];
int dp[5005][5005];
int f[5005][5005];
void dfs(int l, int k) {
if (dp[l][k] >= 0) return;
if (k == 1) {
f[l][k] = a[l];
dp[l][k] = a[l];
return;
}
dfs(l + 1, k - 1);
dfs(l, k - 1);
f[l][k] = f[l + 1][k - 1] ^ f[l][k - 1];
dp[l][k] = max(max(dp[l + 1][k - 1], dp[l][k - 1]), f[l][k]);
}
int main() {
memset(dp, 128, sizeof(dp));
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
dfs(1, n);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", dp[l][r - l + 1]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a, b, q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> q;
while (q--) {
cin >> a >> b;
int c = min(a, b);
int d = max(a, b);
if (2 * c >= d)
cout << 4 * c * c << '\n';
else
cout << d * d << '\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string s[310][310], t[310][310];
deque<char> dq[310][310];
int n, m;
struct P {
int x1, y1, x2, y2;
};
vector<P> ans;
void oper(int x1, int y1, int x2, int y2) {
ans.push_back({x1, y1, x2, y2});
assert(x1 == x2 || y1 == y2);
char c = dq[x1][y1].back();
dq[x1][y1].pop_back();
dq[x2][y2].push_front(c);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> s[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> t[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (char ss : s[i][j]) dq[i][j].push_back(ss);
while (!dq[1][1].empty()) oper(1, 1, 1, 2);
while (!dq[n][m].empty()) oper(n, m, n - 1, m);
while (!dq[1][m].empty()) {
char c = dq[1][m].back();
if (c == '0')
oper(1, m, 1, 1);
else
oper(1, m, n, m);
}
while (!dq[n][1].empty()) {
char c = dq[n][1].back();
if (c == '0')
oper(n, 1, 1, 1);
else
oper(n, 1, n, m);
}
for (int i = 2; i < n; i++) {
while (!dq[i][1].empty()) {
char c = dq[i][1].back();
if (c == '0')
oper(i, 1, 1, 1);
else
oper(i, 1, n, 1);
}
while (!dq[i][m].empty()) {
char c = dq[i][m].back();
if (c == '1')
oper(i, m, n, m);
else
oper(i, m, 1, m);
}
}
for (int i = 2; i < m; i++) {
while (!dq[1][i].empty()) {
char c = dq[1][i].back();
if (c == '0')
oper(1, i, 1, 1);
else
oper(1, i, 1, m);
}
while (!dq[n][i].empty()) {
char c = dq[n][i].back();
if (c == '1')
oper(n, i, n, m);
else
oper(n, i, n, 1);
}
}
while (!dq[1][m].empty()) {
char c = dq[1][m].back();
if (c == '0')
oper(1, m, 1, 1);
else
oper(1, m, n, m);
}
while (!dq[n][1].empty()) {
char c = dq[n][1].back();
if (c == '0')
oper(n, 1, 1, 1);
else
oper(n, 1, n, m);
}
for (int i = 2; i < n; i++)
for (int j = 2; j < m; j++) {
while (!dq[i][j].empty()) {
char c = dq[i][j].back();
if (c == '0')
oper(i, j, i, 1);
else
oper(i, j, i, m);
}
}
for (int i = 2; i < n; i++) {
while (!dq[i][1].empty()) {
oper(i, 1, 1, 1);
}
while (!dq[i][m].empty()) {
oper(i, m, n, m);
}
}
for (int i = 2; i < n; i++)
for (int j = 2; j < m; j++) {
for (int k = t[i][j].size(); k--;) {
if (t[i][j][k] == '0')
oper(1, 1, i, 1), oper(i, 1, i, j);
else
oper(n, m, i, m), oper(i, m, i, j);
}
}
for (int i = 2; i < n; i++) {
for (int k = t[i][1].size(); k--;) {
if (t[i][1][k] == '0')
oper(1, 1, i, 1);
else
oper(n, m, n, 1), oper(n, 1, i, 1);
}
for (int k = t[i][m].size(); k--;) {
if (t[i][m][k] == '1')
oper(n, m, i, m);
else
oper(1, 1, 1, m), oper(1, m, i, m);
}
}
for (int i = 2; i < m; i++) {
for (int k = t[1][i].size(); k--;) {
if (t[1][i][k] == '0')
oper(1, 1, 1, i);
else
oper(n, m, 1, m), oper(1, m, 1, i);
}
for (int k = t[n][i].size(); k--;) {
if (t[n][i][k] == '1')
oper(n, m, n, i);
else
oper(1, 1, n, 1), oper(n, 1, n, i);
}
}
for (int k = t[1][1].size(); k--;) {
if (t[1][1][k] == '0')
oper(1, 1, n, 1), oper(n, 1, 1, 1);
else
oper(n, m, n, 1), oper(n, 1, 1, 1);
}
for (int k = t[n][m].size(); k--;) {
if (t[n][m][k] == '1')
oper(n, m, n, 1), oper(n, 1, n, m);
else
oper(1, 1, n, 1), oper(n, 1, n, m);
}
for (int k = t[n][1].size(); k--;) {
if (t[n][1][k] == '0')
oper(1, 1, n, 1);
else
oper(n, m, n, 1);
}
for (int k = t[1][m].size(); k--;) {
if (t[1][m][k] == '0')
oper(1, 1, 1, m);
else
oper(n, m, 1, m);
}
printf("%d\n", ans.size());
for (P ss : ans) printf("%d %d %d %d\n", ss.x1, ss.y1, ss.x2, ss.y2);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
double hi = n;
string w1;
string w2;
cin >> w1;
cin >> w2;
long long int gk = w1.length() * w2.length();
string w3 = w1;
string w4 = w2;
long long int jkkl = w1.length() * w2.length();
double bn = w1.length();
double bn2 = w2.length();
double ls = jkkl / bn;
double ls2 = jkkl / bn2;
for (long long int i = 1; i < ls; i++) {
w1 = w1 + w3;
}
for (long long int i = 1; i < ls2; i++) {
w2 = w2 + w4;
}
string w10 = w1.substr(0, jkkl);
string w11 = w2.substr(0, jkkl);
long long int pinch1 = 0;
long long int pinch2 = 0;
for (long long int i = 0; i < jkkl; i++) {
if (w10[i] == w11[i]) {
continue;
}
if (w10[i] == 'R' && w11[i] == 'S') {
pinch2++;
} else if (w10[i] == 'S' && w11[i] == 'R') {
pinch1++;
} else if (w10[i] == 'S' && w11[i] == 'P') {
pinch2++;
} else if (w10[i] == 'P' && w11[i] == 'S') {
pinch1++;
} else if (w10[i] == 'R' && w11[i] == 'P') {
pinch1++;
} else if (w10[i] == 'P' && w11[i] == 'R') {
pinch2++;
}
}
long long int hame = n / jkkl;
pinch1 = pinch1 * hame;
pinch2 = pinch2 * hame;
if (hame * jkkl == n) {
cout << pinch1 << " " << pinch2 << endl;
} else {
long long int mm = hame * jkkl;
long long int dif = abs(mm - n);
for (long long int i = 0; i < dif; i++) {
if (w10[i] == w11[i]) {
continue;
}
if (w10[i] == 'R' && w11[i] == 'S') {
pinch2++;
} else if (w10[i] == 'S' && w11[i] == 'R') {
pinch1++;
} else if (w10[i] == 'S' && w11[i] == 'P') {
pinch2++;
} else if (w10[i] == 'P' && w11[i] == 'S') {
pinch1++;
} else if (w10[i] == 'R' && w11[i] == 'P') {
pinch1++;
} else if (w10[i] == 'P' && w11[i] == 'R') {
pinch2++;
}
}
cout << pinch1 << " " << pinch2 << endl;
}
}
| 2 |
#include <bits/stdc++.h>
int N, K, dp[1000001], a[1000001], b[1000001], cnt[1000001], rank[1000001], ans;
long long L;
int binSearch(int k) {
int l = 0, r = N - 1, mid;
while (l < r) {
mid = l + r >> 1;
if (b[mid] < k)
l = mid + 1;
else
r = mid;
}
return l;
}
int main() {
int i, j, r;
long long q;
scanf("%d%lld%d", &N, &L, &K);
for (i = 0; i < N; ++i) {
scanf("%d", &a[i]);
b[i] = a[i];
}
std::sort(b, b + N);
for (i = 1; i < N; ++i) rank[i] = rank[i - 1] + (b[i] != b[i - 1]);
for (i = 0; i < N; ++i) {
a[i] = rank[binSearch(a[i])];
++cnt[a[i]];
}
q = L / N;
r = L % N;
K = std::min((long long)K, q + (r > 0));
for (i = 0; i <= rank[N - 1]; ++i) dp[i] = 1;
for (i = 1; i <= K; ++i) {
for (j = 0; j < r; ++j) {
ans += dp[a[j]];
if (ans >= 1000000007) ans -= 1000000007;
}
for (j = 0; j <= rank[N - 1]; ++j)
dp[j] = (long long)dp[j] * cnt[j] % 1000000007;
for (j = 1; j <= rank[N - 1]; ++j) {
dp[j] += dp[j - 1];
if (dp[j] >= 1000000007) dp[j] -= 1000000007;
}
ans = (ans + (q - i + 1) % 1000000007 * dp[rank[N - 1]]) % 1000000007;
}
printf("%d", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long A = 100000000000000LL, N = 100228;
multiset<long long>::iterator p;
multiset<long long> g;
long long i, j, n, o, m, a[N], b[N], c[N];
int main() {
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i], c[a[i]] = i;
for (i = 1; i <= n; i++) cin >> b[i], g.insert(i - c[b[i]]);
for (i = 1; i <= n; i++) {
p = g.lower_bound(i - 1);
o = (*p) - (i - 1);
if (p != g.begin()) o = min(o, (i - 1) - (*(--p)));
cout << o << "\n";
g.erase(g.find(i - c[b[i]]));
g.insert((n + i) - c[b[i]]);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, h;
cin >> n >> d >> h;
int used[100000 + 7];
memset(used, 0, sizeof used);
if (d > 2 * h) {
cout << -1 << endl;
return 0;
}
if (d == 1 && h == 1) {
if (n > 2) {
cout << -1 << endl;
return 0;
}
}
vector<pair<int, int> > v;
int start = 1, finished1, rem1;
for (int i = int(0); i <= int(h - 1); i++) {
v.push_back(pair<int, int>(start, start + 1));
used[start] = 1;
used[start + 1] = 1;
if (i == h - 1) {
rem1 = start;
}
start++;
}
int remdia = d - h;
finished1 = start + 1;
for (int i = int(0); i <= int(remdia - 1); i++) {
if (i == 0) {
v.push_back(pair<int, int>(1, finished1));
used[finished1] = 1;
} else {
v.push_back(pair<int, int>(finished1, finished1 + 1));
used[finished1] = 1;
used[finished1 + 1] = 1;
finished1++;
}
}
for (int i = int(2); i <= int(n); i++) {
if (used[i] == 0 && i != rem1) {
v.push_back(pair<int, int>(rem1, i));
}
}
if ((int)v.size() == n - 1) {
for (int i = int(0); i <= int(v.size() - 1); i++) {
cout << v[i].first << " " << v[i].second << endl;
}
} else {
cout << -1 << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int sum, L, R;
Node *l, *r;
Node() {
sum = 0;
l = NULL;
r = NULL;
}
};
int n;
int a[100010];
int f[100010];
Node *root[100010];
void Build(Node *&o, int L, int R) {
o = new Node();
o->L = L;
o->R = R;
if (L == R) return;
int M = L + (R - L) / 2;
Build(o->l, L, M);
Build(o->r, M + 1, R);
}
int uX, uN;
void Update(Node *&o, Node *p) {
o = new Node();
*o = *p;
o->sum += uX;
if (o->L == o->R) return;
int M = o->L + (o->R - o->L) / 2;
if (uN <= M)
Update(o->l, p->l);
else
Update(o->r, p->r);
}
int Query(Node *o, int kth) {
if (o->L == o->R) return o->L;
if (kth > o->l->sum)
return Query(o->r, kth - o->l->sum);
else
return Query(o->l, kth);
}
int pre[100010];
int main() {
ios::sync_with_stdio(false);
int i, j;
cin >> n;
Build(root[n + 1], 1, n);
for (i = 1; i <= n; ++i) cin >> a[i];
for (i = n; i > 0; --i) {
if (pre[a[i]]) {
uN = pre[a[i]];
uX = -1;
Update(root[i], root[i + 1]);
uN = i;
uX = 1;
Update(root[i], root[i]);
} else {
uN = i;
uX = 1;
Update(root[i], root[i + 1]);
}
pre[a[i]] = i;
}
int cnt;
for (i = 1; i <= n; ++i) {
j = 1;
cnt = 0;
while (j <= n) {
if (i + 1 <= root[j]->sum)
j = Query(root[j], i + 1);
else
j = n + 1;
++cnt;
}
cout << cnt << ' ';
}
cout << endl;
return 0;
}
| 8 |
#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("unroll-loops")
using namespace std;
const int N = 7e4 + 123;
int s, n, To[N], a[N];
int id, w[N], c[N], ls[N];
bitset<70003> dp, used;
pair<int, int> p[N];
vector<int> gr[N];
inline void calc(int val, int cnt) {
int t = 1;
while (1) {
if (cnt - t <= 0) {
long long a = val * cnt;
if (a <= s) w[++id] = a, c[id] = cnt;
break;
}
cnt -= t;
long long a = 1ll * val * t;
if (a <= s) w[++id] = a, c[id] = t;
t *= 2;
}
}
int main() {
scanf("%d %d\n", &n, &s);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i].first);
p[i].second = i;
a[i] = p[i].first;
}
sort(p + 1, p + 1 + n);
reverse(p + 1, p + 1 + n);
if (p[1].first > s) return printf("-1"), 0;
s = s - p[1].first;
for (int i = 2; i <= n; i++) gr[p[i].first].push_back(p[i].second);
for (int i = 1; i <= 70000; i++)
if ((int)gr[i].size() != 0) calc(i, (int)gr[i].size());
dp[0] = 1;
for (int i = 1; i <= id; i++)
for (int j = s; j >= 0; j--)
if (j + w[i] <= s && dp[j])
if (!dp[j + w[i]]) dp[j + w[i]] = 1, ls[j + w[i]] = i;
if (!dp[s]) return printf("-1"), 0;
int tmp = s;
int Sum = 0;
for (int i = ls[tmp]; i != 0;) {
int u = w[i] / c[i], cnt = c[i];
for (int j = 0; j < (int)gr[u].size(); j++) {
int to = gr[u][j];
if (used[to]) continue;
used[to] = 1;
cnt--;
if (cnt == 0) break;
}
tmp = tmp - w[i];
if (tmp == 0) break;
i = ls[tmp];
}
used[p[1].second] = 1;
for (int i = n; i >= 1; i--) {
if (!used[p[i].second]) {
To[p[i - 1].second] = p[i].second;
}
}
for (int i = 1; i <= n; i++) {
if (To[i] == 0)
printf("%d 0\n", a[i]);
else {
printf("%d 1 %d\n", a[i] - a[To[i]], To[i]);
}
}
}
| 9 |
#include <bits/stdc++.h>
int d[] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6, 2, 1, 8, 7, 5,
9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12};
bool OK(int k) {
for (int i = 2; i * i <= k; ++i) {
int tk = k;
while (tk % i == 0) tk /= i;
if (tk == 1) return false;
}
return true;
}
int main() {
int n, res = 1;
int sum = 0;
scanf("%d", &n);
long long k = 1;
while (k * k < n) k++;
for (int i = 2; i <= k; ++i) {
if (!OK(i)) continue;
long long j = i;
int ct = 0;
while (j <= n) j > k ? ++sum : 0, ct++, j *= i;
res ^= d[ct];
}
if ((n - k - sum) % 2) res ^= 1;
puts(res ? "Vasya" : "Petya");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long double a[200100], b[200100];
long double t[500100 * 4], sz, t1[500100 * 4];
map<int, int> mp1;
void update(int v, long double x) {
v = v + sz - 1;
t[v] += x;
t1[v]++;
v >>= 1;
while (v) {
t[v] = t[v + v] + t[v + v + 1];
t1[v] = t1[v + v] + t1[v + v + 1];
v >>= 1;
}
}
long double get(int v, int l, int r, int tl, int tr) {
if (l > r || tl > r || l > tr || tl > tr) return 0;
if (tl <= l && r <= tr) {
return t[v];
}
int mid = (l + r) >> 1;
return get(v + v, l, mid, tl, tr) + get(v + v + 1, mid + 1, r, tl, tr);
}
long double get1(int v, int l, int r, int tl, int tr) {
if (l > r || tl > r || l > tr || tl > tr) return 0;
if (tl <= l && r <= tr) {
return t1[v];
}
int mid = (l + r) >> 1;
return get1(v + v, l, mid, tl, tr) + get1(v + v + 1, mid + 1, r, tl, tr);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp1[a[i]] = 1;
mp1[a[i] - 2] = 1;
mp1[a[i] + 2] = 1;
}
int cur = 0;
for (auto it = mp1.begin(); it != mp1.end(); it++) {
cur++;
it->second = cur;
}
sz = 1;
while (sz < cur) sz += sz;
for (int i = 1; i <= n; i++) {
b[i] = mp1[a[i]];
}
long double ans = 0;
for (int i = 1; i <= n; i++) {
long double sum =
get(1, 1, sz, 1, mp1[a[i] - 2]) + get(1, 1, sz, mp1[a[i] + 2], cur);
long double kol =
get1(1, 1, sz, 1, mp1[a[i] - 2]) + get1(1, 1, sz, mp1[a[i] + 2], cur);
ans += (a[i] * kol - sum);
update(b[i], a[i]);
}
cout.precision(0);
cout << fixed << ans;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int size(const T& a) {
return (int)a.size();
}
template <class T>
T sqr(const T& a) {
return a * a;
}
const int max_n = 100100;
struct Point {
int x, y;
void scan() { scanf("%d %d", &x, &y); }
} p[max_n], r[8];
const int a[8] = {0, -1, -1, -1, 0, 1, 1, 1};
const int b[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int c[8];
int main() {
int n;
scanf("%d", &n);
memset(c, 128, sizeof(c));
for (int i = 0; i < n; i++) {
p[i].scan();
for (int j = 0; j < 8; j++) {
c[j] = max(c[j], p[i].x * a[j] + p[i].y * b[j]);
}
}
for (int j = 0; j < 8; j++) {
c[j]++;
}
for (int i = 0; i < 8; i++) {
int j = (i + 1) & 7;
int d = (a[i] * b[j] - a[j] * b[i]);
int dx = c[i] * b[j] - c[j] * b[i];
int dy = a[i] * c[j] - a[j] * c[i];
r[i].x = dx / d;
r[i].y = dy / d;
}
int res = 0;
for (int i = 0; i < 8; i++) {
int j = (i + 1) & 7;
res += max(abs(r[i].x - r[j].x), abs(r[i].y - r[j].y));
}
cout << res;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1010;
int N, M, Type, X1, Y1, X2, Y2;
long long Val, Aib[4][NMAX][NMAX];
int LSB(int X) { return (X & (X - 1)) ^ X; }
void Update(int X, int Y, long long Val) {
int Index = (X % 2 ? 1 : 0) + (Y % 2 ? 2 : 0);
for (int i = X; i <= N; i += LSB(i))
for (int j = Y; j <= N; j += LSB(j)) Aib[Index][i][j] ^= Val;
}
long long Query(int X, int Y) {
int Index = (X % 2 ? 1 : 0) + (Y % 2 ? 2 : 0);
long long Ans = 0;
for (int i = X; i; i -= LSB(i))
for (int j = Y; j; j -= LSB(j)) Ans ^= Aib[Index][i][j];
return Ans;
}
int main() {
scanf("%i %i", &N, &M);
for (; M; M--) {
scanf("%i", &Type);
if (Type == 1) {
scanf("%i %i %i %i", &X1, &Y1, &X2, &Y2);
long long Ans = Query(X2, Y2) ^ Query(X1 - 1, Y2) ^ Query(X2, Y1 - 1) ^
Query(X1 - 1, Y1 - 1);
printf("%I64d\n", Ans);
} else {
scanf("%i %i %i %i %I64d", &X1, &Y1, &X2, &Y2, &Val);
Update(X1, Y1, Val);
Update(X2 + 1, Y1, Val);
Update(X1, Y2 + 1, Val);
Update(X2 + 1, Y2 + 1, Val);
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int MAXN = 1000006, Mo = 1e9 + 7;
namespace PAM {
static const int H = 26;
int last, Slen, size;
int nt[MAXN][H], fl[MAXN], len[MAXN], S[MAXN];
int seriesLink[MAXN], f[MAXN], g[MAXN];
int newnode(int l) {
for (int i = (0); i <= (H - 1); ++i) nt[size][i] = 0;
len[size] = l;
return size++;
}
inline int diff(int x) { return len[x] - len[fl[x]]; }
void init() {
size = last = Slen = 0;
newnode(0), newnode(-1);
S[Slen] = -1, fl[0] = 1, fl[1] = 1;
f[0] = 1, g[0] = g[1] = 0;
}
int getFail(int x) {
while (S[Slen - len[x] - 1] != S[Slen]) x = fl[x];
return x;
}
inline void update(int x[], int y, int z) {
if (x[0] > y) x[0] = y, x[1] = z;
}
int getSum(int x) {
int y = Slen - (len[seriesLink[x]] + diff(x));
g[x] = f[y];
if (diff(x) == diff(fl[x])) {
g[x] += g[fl[x]];
if (g[x] >= Mo) g[x] -= Mo;
}
return g[x];
}
void add(int c) {
S[++Slen] = (c -= 'a');
int cur = getFail(last);
if (!nt[cur][c]) {
int now = newnode(len[cur] + 2);
fl[now] = nt[getFail(fl[cur])][c], nt[cur][c] = now;
if (diff(now) == diff(fl[now]))
seriesLink[now] = seriesLink[fl[now]];
else
seriesLink[now] = fl[now];
}
last = nt[cur][c];
f[Slen] = 0;
int tmp = 0;
for (int now = last; len[now] > 0; now = seriesLink[now]) {
int gS = getSum(now);
if (!(Slen & 1)) {
tmp += gS;
if (tmp >= Mo) tmp -= Mo;
}
}
f[Slen] = tmp;
}
void print() {
for (int i = (1); i <= (Slen); ++i) putchar('a' + S[i]);
puts("");
for (int i = (1); i <= (Slen); ++i) printf("%d%c", f[i], " \n"[i == Slen]);
for (int i = (0); i <= (size - 1); ++i) {
printf("%d:fail[%d]=%d,len[%d]=%d\n", i, i, fl[i], i, len[i]);
for (int j = (0); j <= (H - 1); ++j)
if (nt[i][j]) printf("(%c%d)", 'a' + j, nt[i][j]);
puts("");
}
}
}; // namespace PAM
int n, m;
char s[MAXN];
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
PAM::init();
for (int i = (1); i <= (n / 2); ++i) PAM::add(s[i]), PAM::add(s[n + 1 - i]);
printf("%d\n", PAM::f[n]);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double PI = 3.141592653589793238463;
const int N = 2e5 + 1;
vector<int> v[51];
bool visited[51];
int sleep[51];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int x, r, l;
cin >> x;
for (int i = 1; i <= x * (x - 1) / 2 - 1; i++) {
cin >> r >> l;
v[r].push_back(l);
v[l].push_back(r);
sleep[r]++;
}
int o = 0, t;
for (int i = 1; i <= x; i++) {
if (v[i].size() != x - 1) {
if (!o)
o = i;
else {
t = i;
break;
}
}
}
if (sleep[o] >= sleep[t])
cout << o << " " << t << '\n';
else
cout << t << " " << o << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
char a[5000050];
stack<char> st;
int cnt;
int main() {
int n;
while (cin >> n) {
if (n == 1 || n == 2)
return 0 * puts("No");
else {
puts("Yes");
if (n % 2 == 0) {
cout << 2 << " " << 1 << " " << n << endl;
cout << n - 2 << " ";
for (int i = 2; i <= n - 1; i++) {
cout << i;
printf("%c", i == n - 1 ? '\n' : ' ');
}
} else {
cout << 2 << " " << 1 << " " << n << endl;
cout << n - 2 << " ";
for (int i = 2; i <= n - 1; i++) {
cout << i;
printf("%c", i == n - 1 ? '\n' : ' ');
}
}
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long n, m;
cin >> n >> m;
vector<pair<long long, long long> > v[n + 1];
for (long long i = 1; i <= n; i++) {
v[i].push_back({0, 0});
}
for (long long i = 0; i < m; i++) {
long long a, b, c;
cin >> a >> b >> c;
v[a][0].first += c;
v[b][0].second += c;
}
long long sum = 0;
for (long long i = 1; i <= n; i++) {
if (v[i][0].second >= v[i][0].first) sum += v[i][0].second - v[i][0].first;
}
cout << sum;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, t[maxn];
int dp[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &t[i]);
dp[1] = 20;
set<pair<int, int> > s;
s.insert(pair<int, int>(t[1], 1));
for (int i = 2; i <= n; ++i) {
dp[i] = dp[i - 1] + 20;
set<pair<int, int> >::iterator it =
s.lower_bound(pair<int, int>(t[i] - 90 + 1, -1));
if (it != s.end()) dp[i] = min(dp[i], dp[it->second - 1] + 50);
it = s.lower_bound(pair<int, int>(t[i] - 1440 + 1, -1));
if (it != s.end()) dp[i] = min(dp[i], dp[it->second - 1] + 120);
s.insert(pair<int, int>(t[i], i));
}
for (int i = 1; i <= n; ++i) printf("%d\n", dp[i] - dp[i - 1]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
#pragma hdrstop
using namespace std;
inline void sIO() {}
inline void iIO() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
inline void fIO(string fn) {
freopen((fn + ".in").c_str(), "r", stdin);
freopen((fn + ".out").c_str(), "w", stdout);
}
inline void TM() {}
inline void swap(short& a, short& b) { b ^= a ^= b ^= a; }
inline void swap(int& a, int& b) { b ^= a ^= b ^= a; }
inline void swap(char& a, char& b) { b ^= a ^= b ^= a; }
inline void swap(long long& a, long long& b) { b ^= a ^= b ^= a; }
template <class T>
inline T abs(T x) {
return x < 0 ? -x : x;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T min(T& a, T& b) {
return a < b ? a : b;
}
template <class T>
inline T max(T& a, T& b) {
return b < a ? a : b;
}
template <class T>
inline T gcd(T a, T b) {
if (a < b) swap(a, b);
while (b) {
a %= b;
swap(a, b);
}
return a;
}
template <class T>
inline T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <class T>
inline bool isPrime(T n) {
if (n < 2) return false;
T kk = (T)sqrt(n + 0.);
for (T i = 2; i <= kk; ++i)
if (!(n % i)) return false;
return true;
}
template <class T>
inline string toa(T x) {
stringstream ss;
ss << x;
string ret;
ss >> ret;
return ret;
}
template <class T>
inline T ppow(T a, long long b) {
T ret = 1;
while (b) {
if (b & 1) ret *= a;
a *= a;
b >>= 1;
}
return ret;
}
template <class T>
inline T ppow(T a, long long b, long long md) {
T ret = 1;
a %= md;
while (b) {
if (b & 1) ret = ret * a % md;
a = a * a % md;
b >>= 1;
}
return ret % md;
}
inline int toi(string s) {
stringstream ss;
ss << s;
int ret;
ss >> ret;
return ret;
}
inline long long tol(string s) {
stringstream ss;
ss << s;
long long ret;
ss >> ret;
return ret;
}
inline int Random() { return ((rand() << 16) | rand()); }
inline int Random(int l, int r) {
if (l == r) return l;
return Random() % (r - l) + l;
}
inline char upperCase(char ch) {
return (ch >= 'a' && ch <= 'z') ? ch ^ 32 : ch;
}
inline char lowerCase(char ch) {
return (ch >= 'A' && ch <= 'Z') ? ch ^ 32 : ch;
}
inline string upperCase(string s) {
int ls = s.length();
for (int i = 0; i < ls; ++i)
if (s[i] >= 'a' && s[i] <= 'z') s[i] ^= 32;
return s;
}
inline string lowerCase(string s) {
int ls = s.length();
for (int i = 0; i < ls; ++i)
if (s[i] >= 'A' && s[i] <= 'Z') s[i] ^= 32;
return s;
}
inline int dig(char ch) { return ch - 48; }
inline bool isAlpha(char ch) {
return (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z');
}
inline bool isDigit(char ch) { return (ch >= '0' && ch <= '9'); }
inline bool isLowerCase(char ch) { return (ch >= 'a' && ch <= 'z'); }
inline bool isUpperCase(char ch) { return (ch >= 'A' && ch <= 'Z'); }
int __;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const int MD = 1000000007;
int n, x, y;
vector<int> g[111111];
double ans;
void dfs(int v, int p, int cur) {
ans += 1. / cur;
int tt = (int)g[v].size(), to;
for (int i = 0; i < tt; ++i)
if ((to = g[v][i]) != p) dfs(to, v, cur + 1);
}
int main() {
sIO();
scanf("%d", &n);
for (int i = 1; i < n; ++i)
scanf("%d%d", &x, &y), g[x].push_back(y), g[y].push_back(x);
ans = 0, dfs(1, 0, 1);
printf("%.16lf", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
string t;
cin >> t;
int k;
cin >> k;
int n = t.size();
int canes = 0;
int flakes = 0;
for (int i = 0; i < t.size(); i++) {
if (t[i] == '?')
canes++;
else if (t[i] == '*')
flakes++;
}
if (flakes == 0) {
bool use[n];
int sz = n;
for (int i = 0; i < n; i++) use[i] = true;
for (int i = 0; i < n; i++) {
if (t[i] == '?') {
sz--;
use[i] = false;
}
}
for (int i = 0; i < n; i++) {
if (t[i] == '?') {
if (sz > k) {
sz--;
use[i - 1] = false;
}
}
}
if (sz == k) {
for (int i = 0; i < n; i++)
if (use[i]) cout << t[i];
} else {
cout << "Impossible\n";
return 0;
}
} else {
if (n - canes * 2 - flakes * 2 <= k) {
bool use[n];
int sz = n;
int idx = -1;
for (int i = 0; i < n; i++) {
use[i] = true;
}
for (int i = 0; i < n; i++) {
if (t[i] == '?') {
use[i - 1] = false;
use[i] = false;
sz -= 2;
} else if (t[i] == '*') {
use[i] = false;
use[i - 1] = false;
if (idx == -1) idx = i - 1;
sz -= 2;
}
}
for (int i = 0; i < n; i++) {
if (i == idx) {
while (sz != k) {
cout << t[i];
sz++;
}
} else {
if (use[i]) cout << t[i];
}
}
} else {
cout << "Impossible\n";
return 0;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
enum state { rest, write, sport };
int memo[101];
int check_num(int *x, int N, int z, state s, int counter) {
int counter1, counter2;
for (int i = z; i < N; i++) {
if (x[i] == 0) {
counter++;
s = rest;
} else if (x[i] == 1) {
if (s == write) {
counter++;
s = rest;
} else {
s = write;
}
} else if (x[i] == 2) {
if (s == sport) {
counter++;
s = rest;
} else {
s = sport;
}
} else {
if (s == sport)
s = write;
else if (s == write)
s = sport;
else {
if (memo[i] != -1) return (counter + memo[i]);
counter1 = check_num(x, N, i + 1, write, 0);
counter2 = check_num(x, N, i + 1, sport, 0);
memo[i] = (counter1 < counter2) ? counter1 : counter2;
return (counter + memo[i]);
}
}
}
return counter;
}
int main() {
int N, counter = 0;
int x[101];
state s = rest;
cin >> N;
for (int i = 0; i < N; i++) {
memo[i] = -1;
cin >> x[i];
}
cout << check_num(x, N, 0, s, counter);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int he[300010], ver[2 * 300010], nxt[2 * 300010], tot, in[300010];
void add(int x, int y) {
in[y]++;
ver[++tot] = y;
nxt[tot] = he[x];
he[x] = tot;
}
string c[300010];
int n, m;
int cg(int x, int y) { return (x - 1) * m + y; }
int dfn[300010], cnt, low[300010];
void dfs(int x) {
dfn[x] = ++cnt;
for (int i = he[x]; i; i = nxt[i]) {
if (dfn[ver[i]]) continue;
dfs(ver[i]);
}
low[x] = cnt;
return;
}
struct line {
int x, y, pos, v;
} p[4 * 300010];
bool cmp(line a, line b) { return a.pos < b.pos; }
int sum[300010 * 4], sz[300010 * 4];
void push_up(int k, int len) {
if (sum[k]) {
sz[k] = len;
} else
sz[k] = sz[2 * k] + sz[2 * k + 1];
return;
}
void change(int l, int r, int ll, int rr, int v, int k) {
if (ll <= l && r <= rr) {
sum[k] += v;
push_up(k, r - l + 1);
return;
}
int mid = (l + r) >> 1;
if (ll <= mid) change(l, mid, ll, rr, v, 2 * k);
if (rr >= mid + 1) change(mid + 1, r, ll, rr, v, 2 * k + 1);
push_up(k, r - l + 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
cin >> c[i];
c[i] = " " + c[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (c[i][j] == 'U') {
if (i != 1) add(cg(i - 1, j), cg(i + 1, j));
if (i != n - 1) add(cg(i + 2, j), cg(i, j));
}
if (c[i][j] == 'L') {
if (j != 1) add(cg(i, j - 1), cg(i, j + 1));
if (j != m - 1) add(cg(i, j + 2), cg(i, j));
}
}
}
for (int i = 1; i <= n * m; i++) {
if (!in[i]) {
dfs(i);
}
}
cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (c[i][j] == 'U') {
int a = cg(i, j), b = cg(i + 1, j);
p[++cnt] = {dfn[a], low[a], dfn[b], 1};
p[++cnt] = {dfn[a], low[a], low[b] + 1, -1};
p[++cnt] = {dfn[b], low[b], dfn[a], 1};
p[++cnt] = {dfn[b], low[b], low[a] + 1, -1};
}
if (c[i][j] == 'L') {
int a = cg(i, j), b = cg(i, j + 1);
p[++cnt] = {dfn[a], low[a], dfn[b], 1};
p[++cnt] = {dfn[a], low[a], low[b] + 1, -1};
p[++cnt] = {dfn[b], low[b], dfn[a], 1};
p[++cnt] = {dfn[b], low[b], low[a] + 1, -1};
}
}
}
sort(p + 1, p + cnt + 1, cmp);
int last = 0;
long long ans = 0;
for (int i = 1; i <= cnt; i++) {
if (i != 1) {
ans += 1LL * (p[i].pos - last) * (sz[1]);
}
change(1, n * m, p[i].x, p[i].y, p[i].v, 1);
last = p[i].pos;
}
printf("%lld", ans / 2);
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 1e5 + 10, maxm = 1e6 + 10, lg = 17, mod = 998244353,
inf = 1e18;
long long n, len, a, b, par[maxn][lg + 1], h[maxn], zuz[maxn], tguz;
pair<long long, long long> mx[maxn][4];
vector<long long> g[maxn];
bool kor[maxn];
void dfd(long long v = 1, long long p = 0) {
h[v] = h[p] + 1;
par[v][0] = p;
for (int i = 1; i <= lg; i++) par[v][i] = par[par[v][i - 1]][i - 1];
fill(mx[v], mx[v] + 4, make_pair(0, v));
for (auto u : g[v])
if (u != p) {
dfd(u, v);
mx[v][0] = mx[u][3];
mx[v][0].first++;
sort(mx[v], mx[v] + 4);
}
}
void dfu(long long v = 1, long long p = 0) {
if (mx[v][1].first >= len) zuz[v]++, tguz++;
for (auto u : g[v])
if (u != p) {
mx[u][0] = (mx[v][3].second == mx[u][3].second ? mx[v][2] : mx[v][3]);
mx[u][0].first++;
sort(mx[u], mx[u] + 4);
dfu(u, v);
zuz[v] += zuz[u];
}
}
long long gup(long long v, long long x) {
for (int i = 0; i <= lg; i++)
if (x >> i & 1) v = par[v][i];
return v;
}
long long lca(long long v, long long u) {
if (h[v] < h[u]) swap(v, u);
v = gup(v, h[v] - h[u]);
if (v == u) return v;
for (int i = lg; i >= 0; i--)
if (par[v][i] != par[u][i]) v = par[v][i], u = par[u][i];
return par[v][0];
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long T;
cin >> T;
while (T--) {
cin >> n >> a >> b;
for (int i = 1; i < n; i++) {
long long v, u;
cin >> v >> u;
g[v].push_back(u), g[u].push_back(v);
}
dfd();
len = h[a] + h[b] - 2 * h[lca(a, b)];
dfu();
while (1) {
long long ms;
for (int i : {3, 2, 1, 0}) {
long long na = mx[a][i].second;
if (h[b] + h[na] - 2 * h[lca(b, na)] == len + mx[a][i].first) {
ms = mx[a][i].first;
a = na;
break;
}
}
long long o = lca(a, b);
if (h[b] - h[o] >= ms)
b = gup(b, ms);
else {
ms -= h[b] - h[o];
b = gup(a, h[a] - (h[o] + ms));
}
if (kor[a]) {
cout << "NO\n";
break;
}
o = lca(a, b);
if (o == b) {
if (zuz[gup(a, h[a] - h[b] - 1)] < tguz) {
cout << "YES\n";
break;
}
} else if (zuz[b]) {
cout << "YES\n";
break;
}
kor[a] = 1;
swap(a, b);
}
for (int i = 1; i <= n; i++) {
g[i].clear(), kor[i] = 0, h[i] = 0, zuz[i] = 0;
memset(par[i], 0, sizeof(par[i]));
}
tguz = 0;
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void dbg(T a) {}
template <typename T, typename... Arg>
void dbg(T a, Arg... arg) {}
const int maxn = (1e6) + 7;
const int inf = (1e9) + 7;
const long long LLinf = (1e18) + 7;
const long double eps = 1e-9;
const long long mod = 1e9 + 7;
int ile(vector<int> V) {
int res = 0;
while (((int)(V).size()) && V.back() == 0) {
V.pop_back();
res++;
}
if (((int)(V).size()) == 0) return 10000;
return res;
}
int rob(vector<int> V, int a, int b) {
int cnt = 0;
for (int i = 0; i < ((int)(V).size()); i++)
if (V[i] == a) {
for (int j = i - 1; j >= 0; j--) {
swap(V[j], V[j + 1]);
cnt++;
}
break;
}
for (int i = 1; i < ((int)(V).size()); i++)
if (V[i] == b) {
for (int j = i - 1; j > 0; j--) {
swap(V[j], V[j + 1]);
cnt++;
}
break;
}
if (V[0] == a && V[1] == b)
return cnt + ile(V);
else
return 1000;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
vector<int> V;
while (n > 0) {
V.push_back(n % 10);
n /= 10LL;
}
int res = 1000;
res = min(res, rob(V, 0, 0));
res = min(res, rob(V, 5, 2));
res = min(res, rob(V, 0, 5));
res = min(res, rob(V, 5, 7));
if (res < 100)
cout << res;
else
cout << "-1";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct quer {
int typ, l, r, d;
} q[5009];
int a[5009], b[5009], diff[5009], final[5009];
int main() {
int i, j, t1, t2, t3, t4, n, m;
scanf("%d %d", &n, &m);
for (i = 0; i < m; i++) {
scanf("%d %d %d %d", &q[i].typ, &q[i].l, &q[i].r, &q[i].d);
}
for (i = 1; i <= n; i++) {
diff[i] = 0;
b[i] = 1e9;
}
for (i = 0; i < m; i++) {
if (q[i].typ == 1) {
for (j = q[i].l; j <= q[i].r; j++) {
diff[j] += q[i].d;
}
} else {
for (j = q[i].l; j <= q[i].r; j++) {
b[j] = min(b[j], q[i].d - diff[j]);
}
}
}
for (i = 1; i <= n; i++) {
a[i] = b[i];
final[i] = a[i];
}
for (i = 0; i < m; i++) {
if (q[i].typ == 1) {
for (j = q[i].l; j <= q[i].r; j++) {
a[j] += q[i].d;
}
} else {
t1 = -2e9;
for (j = q[i].l; j <= q[i].r; j++) {
t1 = max(t1, a[j]);
}
if (t1 != q[i].d) {
printf("NO\n");
return 0;
}
}
}
printf("YES\n");
for (i = 1; i <= n; i++) {
printf("%d ", final[i]);
}
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 16;
int main() {
int n, m;
int graph[N + 1][N + 1];
string volunteers[N + 1];
map<string, int> rel;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
graph[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
string name;
cin >> name;
volunteers[i] = name;
rel[name] = i;
}
for (int i = 0; i < m; i++) {
string f, s;
cin >> f >> s;
graph[rel[f]][rel[s]] = graph[rel[s]][rel[f]] = 1;
}
int combination = 0, match = 0;
for (int mask = 0; mask < (1 << n); mask++) {
bool notPair = false;
int it = 0;
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
for (int j = 0; j < n; j++) {
if (mask & (1 << j)) {
if (graph[i][j]) {
notPair = true;
}
}
it++;
}
}
}
if (!notPair && it > match) {
combination = mask;
match = it;
}
}
vector<string> group;
for (int i = 0; i < n; i++) {
if (combination & (1 << i)) {
group.push_back(volunteers[i]);
}
}
sort(group.begin(), group.end());
cout << group.size() << endl;
for (int i = 0; i < group.size(); i++) {
cout << group.at(i) << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 10010;
bool vis[Maxn];
vector<int> s[Maxn];
vector<int> v[Maxn];
int cnt = 0;
void dfs(int k) {
vis[k] = true;
bool flag = false;
for (int i = 0; i < v[k].size(); i++) {
for (int j = 0; j < s[v[k][i]].size(); j++) {
if (!vis[s[v[k][i]][j]]) {
dfs(s[v[k][i]][j]);
}
}
}
}
int main() {
int N, M;
cin >> N >> M;
int a, tmp;
int sum = 0;
for (int i = 1; i <= N; i++) {
cin >> tmp;
sum += tmp;
for (int j = 1; j <= tmp; j++) {
cin >> a;
v[i].push_back(a);
s[a].push_back(i);
}
}
for (int i = 1; i <= N; i++) {
if (!vis[i]) {
dfs(i);
cnt++;
}
}
if (sum) {
cnt -= 1;
}
cout << cnt << endl;
}
| 3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60000000")
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = 3.1415926535897932384626433832795;
template <class T>
T sqr(T x) {
return x * x;
}
char s[3000000], s1[3000000];
int n;
double calc(char *s) {
double res = 0;
int rx = 0;
for (int i = 0; i < (int)(n); i++)
if (s[i] != 'X') rx++;
if (n % 2 == 0)
res = (double)rx / (n + 1) / 2.;
else
res = (double)rx / n / 2.;
int dp = 0;
int ry = 0;
int len = 0;
int i = 0;
int l1 = 0;
while (i < n) {
if ((s[i] == 'L' && ((dp + i) & 1) == 1) ||
(s[i] == 'R' && ((dp + i) & 1) == 0))
len++;
else if ((s[i] == 'L' && ((dp + i) & 1) == 0) ||
(s[i] == 'R' && ((dp + i) & 1) == 1)) {
if (len > 1) {
dp++;
ry += len;
len = 0;
continue;
} else if (len == 1) {
l1++;
len = 0;
}
ry++;
}
i++;
}
if (len > 1) {
ry += len;
dp++;
len = 0;
}
if (((n + dp) & 1) == 1) {
dp++;
if (len == 1) ry++;
} else if (len == 1)
l1++;
while (l1 > 0 && dp + n >= ry * 2) {
ry++;
l1--;
dp += 2;
}
res = max(res, (double)ry / (n + dp));
return res;
}
int main() {
scanf("%s", s1);
n = strlen(s1);
int p = 0;
memset(s, 0, sizeof(s));
s[p++] = s1[0];
for (int i = 1; i < n; i++) {
if (s1[i] == s1[i - 1] && s1[i] != 'X') s[p++] = 'X';
s[p++] = s1[i];
}
double res = 0;
n = strlen(s);
if (s[0] == s[n - 1] && s[0] != 'X') {
s[n++] = 'X';
double res1 = calc(s);
for (int i = n - 1; i > 0; i--) s[i] = s[i - 1];
s[0] = 'X';
double res2 = calc(s);
res = max(res1, res2);
} else
res = calc(s);
res = res * 100;
res = floor(res * 1e6) / 1e6;
printf("%.6lf\n", res);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int p;
int c;
int main() {
cin >> p;
for (int x = 1; x < p; x++) {
int a = x;
int v = 0;
for (int i = 1; i <= p - 2; i++) {
if (a == 1) v = 1;
a = (a * x) % p;
}
if (a != 1) v = 1;
if (v == 0) c++;
}
cout << c << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c, d = 0, e, f, g, h, i;
string s, k;
cin >> a >> b >> s;
for (i = 0; i < b; i++) {
s = s + '0';
}
for (i = 0; i < a; i = i * 1) {
c = b;
while (s[i + c] == '0') {
c = c - 1;
if (c == 0) {
cout << -1;
return 0;
}
}
i = i + c;
d = d + 1;
if (i == a - 1) break;
}
cout << d;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long toint(string s) {
unsigned long long a = 0, c = 1;
for (int i = 0; i < s.size(); i++) {
a += (s[(s.size() - i - 1)] - '0') * c;
c *= 10;
}
return a;
}
long long arr[10000], brr[10000];
int main() {
fast();
long long t, b, p, f, h, c, mx = INT_MIN, mx1 = INT_MIN, x, y, bb;
string s, ss;
cin >> t;
while (t--) {
cin >> b >> p >> f;
cin >> h >> c;
bb = b;
x = min(p, b / 2);
b -= (x * 2);
y = min(f, b / 2);
mx = x * h + y * c;
y = min(f, bb / 2);
bb -= (y * 2);
x = min(p, bb / 2);
mx1 = x * h + y * c;
cout << max(mx1, mx) << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
if (k == n) {
cout << -1;
} else {
for (int i = 1; i < n - k; ++i) {
cout << i + 1 << " ";
}
cout << 1 << " ";
for (int i = n - k + 1; i <= n; ++i) {
cout << i << " ";
}
}
return 0;
}
| 2 |
// {{{ Template
/*CYJian yyds!*/
/*ldx gg txdy!*/
/*sto karry,tiger,lsqs orz*/
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vector <int> poly;
typedef pair <int,int> pii;
#define fi first
#define se second
#define rez resize
#define pp pop_back
#define pb push_back
#define mkp make_pair
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof((x)))
const int inf=1e9+7;
const int mod=1e9+7;
template <class T> inline T read() {
char ch; bool flag=0; T x=0;
while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
return flag?-x:x;
}
struct {
inline operator ll() {return read<ll>();}
inline operator int() {return read<int>();}
inline operator bool() {return read<bool>();}
inline operator double() {double _tmp; return scanf("%lf",&_tmp),_tmp;}
template <class T> inline void operator () (T &x) {x=*this;}
template <class T,class ...A> inline void operator () (T &x,A &...a)
{x=*this,this->operator()(a...);}
} IN;
template <class T> inline void chkmax(T &x,T y) {if(x<y) x=y;}
template <class T> inline void chkmin(T &x,T y) {if(x>y) x=y;}
inline int mul(int x,int y) {return 1ll*x*y%mod;}
inline int dec(int x,int y) {x-=y; if(x<0) x+=mod; return x;}
inline int add(int x,int y) {x+=y; if(x>=mod) x-=mod; return x;}
inline void sub(int &x,int y) {x-=y; if(x<0) x+=mod;}
inline void pls(int &x,int y) {x+=y; if(x>=mod) x-=mod;}
inline int modpow(int x,int y,int res=1) {
if(y==-1) y=mod-2;
for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
return res;
}
// }}}
const int N=2e5+5;
ll a[N],sum[N];
int n,m,q[N],L[N],R[N];
struct Node {
int l,r; ll val;
bool operator < (const Node &ele) const {return val>ele.val||(val==ele.val&&r>ele.r);}
}; std::set <Node> S;
inline ll calc(int l,int r) {return (sum[r]-sum[l-1]+r-l)/(r-l+1); }
inline Node merge(Node a,Node b) {
R[a.l]=b.r,L[b.r]=a.l;
return (Node){a.l,b.r,calc(a.l,b.r)};
}
int ans[N],qwq[N],tim[N];
int main() {
IN(n,m);
lep(i,1,n) IN(a[i]); a[n]=-2e18;
lep(i,1,n) L[i]=R[i]=i,S.insert((Node){i,i,a[i]});
lep(i,1,n) sum[i]=sum[i-1]+a[i];
int c=0; tim[0]=inf;
while(S.size()>1) {
Node now=*S.begin(),nxt=(Node){now.r+1,R[now.r+1],calc(now.r+1,R[now.r+1])};
S.erase(now),S.erase(nxt),S.insert(merge(now,nxt));
if(now.val<tim[c]) ++c,qwq[c]=max(qwq[c-1],nxt.r-now.l),tim[c]=now.val;
else chkmax(qwq[c],nxt.r-now.l);
}
lep(i,1,c) --tim[i];
std::reverse(tim+1,tim+1+c),std::reverse(qwq+1,qwq+1+c);
lep(i,1,m) IN(q[i]),printf("%d ",qwq[lower_bound(tim+1,tim+1+c,q[i])-tim]);
return puts(""),0;
} | 10 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double pi = acos(-1.0);
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, d;
while (cin >> n >> d) {
pair<int, int> p[n];
int dist[n][n], a[n];
memset(a, 0, sizeof(a));
memset(dist, 0, sizeof(dist));
for (int i = 0; i < n - 2; i++) cin >> a[i + 1];
for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j)
dist[i][j] =
(abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second)) *
d -
a[i];
}
}
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dist[i][j] = ((dist[i][j]) < (dist[i][k] + dist[k][j])
? (dist[i][j])
: (dist[i][k] + dist[k][j]));
cout << dist[0][n - 1] << '\n';
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
struct Cake {
long long V;
int id;
bool operator<(const Cake a) const {
if (V != a.V) return V < a.V;
return id > a.id;
}
} cake[100005];
long long dp[400005];
int p;
long long v;
void update(int l, int r, int o) {
if (l == r) {
dp[o] = v;
return;
}
int m = (l + r) / 2;
if (p <= m)
update(l, m, o * 2);
else
update(m + 1, r, o * 2 + 1);
dp[o] = max(dp[o * 2], dp[o * 2 + 1]);
}
int ql, qr;
long long query(int l, int r, int o) {
if (l >= ql && r <= qr) return dp[o];
long long ret = 0;
int mid = (l + r) / 2;
if (ql <= mid) ret = max(ret, query(l, mid, o * 2));
if (qr > mid) ret = max(ret, query(mid + 1, r, o * 2 + 1));
return ret;
}
int main() {
memset(dp, 0, sizeof(dp));
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
long long r, h;
scanf("%lld%lld", &r, &h);
cake[i].V = r * r * h;
cake[i].id = i;
}
sort(cake + 1, cake + n + 1);
for (int i = 1; i <= n; ++i) {
ql = 1;
qr = cake[i].id;
v = query(1, n, 1) + cake[i].V;
p = cake[i].id;
update(1, n, 1);
}
ql = 1;
qr = n;
long long ans = query(1, n, 1);
printf("%.6lf\n", ans * PI);
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define maxn 100086
using namespace std;
const int p = 998244353;
int n;
int c[27];
int f[405][3][3][215][215], g[405][2][2][215];
inline void add(int &x, int y){
x += y;
if(x >= p) x -= p;
}
int main(){
scanf("%d", &n);
for(int i = 1;i <= 26;i++) scanf("%d", &c[i]);
f[0][0][0][0][0] = 24 * 24;
f[0][0][1][1][0] = 24;
f[0][0][2][0][1] = 24;
f[0][1][0][1][0] = 24;
f[0][1][1][2][0] = 1;
f[0][1][2][1][1] = 1;
f[0][2][0][0][1] = 24;
f[0][2][1][1][1] = 1;
f[0][2][2][0][2] = 1;
for(int i = 0;i < n - 2;i++){
for(int j = 0;j <= 2;j++){
for(int k = 0;k <= 2;k++){
for(int l = 0;l <= 210;l++){
for(int m = 0;m <= 210;m++){
add(f[i + 1][k][0][l][m], 1ll * (24 - (j == 0)) * f[i][j][k][l][m] % p);
if(j != 1) add(f[i + 1][k][1][l + 1][m], f[i][j][k][l][m]);
if(j != 2) add(f[i + 1][k][2][l][m + 1], f[i][j][k][l][m]);
}
}
}
}
}
g[0][0][0][0] = 25 * 25;
g[0][0][1][1] = 25;
g[0][1][0][1] = 25;
g[0][1][1][2] = 1;
for(int i = 0;i < n - 2;i++){
for(int j = 0;j <= 1;j++){
for(int k = 0;k <= 1;k++){
for(int l = 0;l <= 210;l++){
add(g[i + 1][k][0][l], 1ll * (25 - (j == 0)) * g[i][j][k][l] % p);
if(j != 1) add(g[i + 1][k][1][l + 1], g[i][j][k][l]);
}
}
}
}
int ans = 1;
for(int i = 1;i <= n;i++) ans = 1ll * ans * (26 - (i > 2)) % p;
for(int i = 1;i <= 26;i++){
for(int k = c[i] + 1;k <= 211;k++){
for(int jj = 0;jj <= 1;jj++){
for(int kk = 0;kk <= 1;kk++){
add(ans, p - g[n - 2][jj][kk][k]);
}
}
}
for(int j = 1;j < i;j++){
for(int k = c[i] + 1;k <= 211;k++){
for(int l = c[j] + 1;l <= 211;l++){
for(int jj = 0;jj <= 2;jj++){
for(int kk = 0;kk <= 2;kk++){
add(ans, f[n - 2][jj][kk][k][l]);
}
}
}
}
}
}
printf("%d", ans);
} | 9 |