solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
template <class T>
inline T cAbs(T v) {
return v > 0 ? v : (-v);
}
namespace std {
const int maxN = 1000000 + 10;
int N;
char str[maxN];
int idx[maxN], nd;
int stk[maxN], ns;
void __main__() {
scanf("%s", str);
N = strlen(str);
int cut = 0;
for (int i = 0; i < N; i++)
if (str[i] != str[0]) {
cut = i;
break;
}
char ch = str[0];
strcpy(str, str + cut);
int end = N - cut;
while (end < N) str[end++] = ch;
str[end] = 0;
nd = 0;
int res = 0;
for (int i = 0; i < N;) {
idx[nd++] = i;
int j = i;
while (j < N && str[j] == str[i]) j++;
if (j - i + 1 >= 3) res++;
i = j;
}
if (nd == 1) {
printf("%d\n", res);
return;
}
ns = 0;
for (int i = 0; i < nd; i++) {
if ((idx[i] + 1) % N != idx[(i + 1) % nd]) continue;
if ((idx[i] - 1 + N) % N != idx[(i - 1 + nd) % nd]) continue;
stk[ns++] = idx[i];
}
int upd = 0;
for (int i = 0; i < ns; i++) {
if ((stk[i] - stk[upd]) != (i - upd)) {
upd = i;
break;
}
}
for (int i = upd; i < upd + ns;) {
int j = i + 1, k = 1;
while (j < upd + ns && (stk[(j - 1) % ns] + 1) % N == stk[j % ns]) j++, k++;
res += (k + 1) >> 1;
i = j;
}
printf("%d\n", res);
}
} // namespace std
int main() {
std::__main__();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
class SuffixArray {
public:
int sz, index1, index2;
vector<int> rnk, tmp, sa, lcp;
SuffixArray(const string &arg) : sz((int)arg.size()), rnk(sz + 1), tmp(sz + 1), sa(sz + 1), lcp(sz + 1) { make_sa(arg), make_lcp(arg); }
void make_sa(const string &s) {
index1 = sz;
for (int i = 0; i <= index1; i++) {
sa.at(i) = i;
rnk.at(i) = (i < index1) ? s.at(i) : -1;
}
auto comp = [&](const int i, const int j) {
if (rnk.at(i) != rnk.at(j)) {
return rnk.at(i) < rnk.at(j);
} else {
const int ri = (i + index2 <= index1) ? rnk.at(i + index2) : -1;
const int rj = (j + index2 <= index1) ? rnk.at(j + index2) : -1;
return ri < rj;
}
};
for (index2 = 1; index2 <= index1; index2 *= 2) {
sort(sa.begin(), sa.end(), comp);
tmp.at(sa.at(0)) = 0;
for (int i = 1; i <= index1; i++) {
tmp.at(sa.at(i)) = tmp.at(sa.at(i - 1)) + (comp(sa.at(i - 1), sa.at(i)) ? 1 : 0);
}
for (int i = 0; i <= index1; i++) {
rnk.at(i) = tmp.at(i);
}
}
}
void make_lcp(const string &s) {
for (int i = 0; i <= sz; i++) {
rnk.at(sa.at(i)) = i;
}
int h = 0;
lcp.at(0) = 0;
for (int i = 0; i < sz; i++) {
int j = sa.at(rnk.at(i) - 1);
if (h > 0) {
--h;
}
for (; j + h < sz && i + h < sz; h++) {
if (s.at(j + h) != s.at(i + h)) {
break;
}
}
lcp.at(rnk.at(i) - 1) = h;
}
}
};
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
string S;
cin >> S;
int N = S.size();
SuffixArray SA(S);
long ans = 0;
for (int i = 0; i < N; i++) {
ans += N - SA.sa.at(i + 1) - SA.lcp.at(i);
}
cout << ans << "\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int maxn = 100000 + 5;
const long double eps = 1e-6;
long double PI = acos((long double)(-1));
int n;
long double p, q;
long double x[maxn], y[maxn];
long double f(long double a) {
long double b = 1e100;
for (int i = 1; i <= n; i++) b = min(b, (1.0 - a * x[i]) / y[i]);
return a * p + b * q;
}
int main() {
std::ios_base::sync_with_stdio(false);
int i, j;
cin >> n >> p >> q;
for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
long double l = 0, r = 1e100, mid1, mid2;
for (int i = 1; i <= n; i++) r = min(r, 1.0 / x[i]);
for (int i = 1; i <= 50; i++) {
mid1 = (l + r) * 0.5;
mid2 = (mid1 + r) * 0.5;
if (f(mid1) < f(mid2))
l = mid1;
else
r = mid2;
}
cout << fixed << setprecision(10);
cout << f(l) << endl;
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int n;
string s[100001];
int main() {
cin>>n>>s[0];
for(int i=1; i<=n; i++)
cin>>s[i];
sort(s+1,s+1+n);
for(int i=1; i<=n; i++)
cout<<s[i];
return 0;
}
| 0 |
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;
//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> H >> W;
vector<string>s(H);
for (auto &i : s)cin >> i;
vector<vector<int>>adis(H, vector<int>(W, MOD));
vector<vector<int>>bdis(H, vector<int>(W, MOD));
vector<vector<int>>cdis(H, vector<int>(W, MOD));
queue<pair<int, int>>Q;
int dir[] = { 1,0,-1,0,1 };
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (s[i][j] == '@') {
Q.emplace(i, j);
adis[i][j] = 0;
}
}
}
while (!Q.empty()) {
int cy, cx;
tie(cy, cx) = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int ny = cy + dir[i];
int nx = cx + dir[i + 1];
if (ny < 0 || nx < 0 || ny >= H || nx >= W)continue;
if (s[ny][nx] == '#')continue;
if (adis[ny][nx] > adis[cy][cx] + 1) {
adis[ny][nx] = adis[cy][cx] + 1;
Q.emplace(ny, nx);
}
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (s[i][j] == '$') {
Q.emplace(i, j);
bdis[i][j] = 0;
}
}
}
while (!Q.empty()) {
int cy, cx;
tie(cy, cx) = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int ny = cy + dir[i];
int nx = cx + dir[i + 1];
if (ny < 0 || nx < 0 || ny >= H || nx >= W)continue;
if (s[ny][nx] == '#')continue;
if (bdis[ny][nx] > bdis[cy][cx] + 1) {
bdis[ny][nx] = bdis[cy][cx] + 1;
Q.emplace(ny, nx);
}
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (s[i][j] == '@') {
Q.emplace(i, j);
cdis[i][j] = 0;
}
}
}
while (!Q.empty()) {
int cy, cx;
tie(cy, cx) = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int ny = cy + dir[i];
int nx = cx + dir[i + 1];
if (ny < 0 || nx < 0 || ny >= H || nx >= W)continue;
if (s[ny][nx] == '#')continue;
if (adis[ny][nx] >= bdis[ny][nx])continue;
if (cdis[ny][nx] > cdis[cy][cx] + 1) {
cdis[ny][nx] = cdis[cy][cx] + 1;
Q.emplace(ny, nx);
}
}
}
int ans = MOD;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (s[i][j] == '%') {
ans = cdis[i][j];
}
}
}
if (ans == MOD)cout << "No\n";
else cout << "Yes\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a.begin(), a.end());
for (int i = 0; i < n / 2; ++i) {
cout << a[i + 1] << ' ' << a[0] << '\n';
}
}
bool multitest = true;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
size_t number_of_tests = 1;
if (multitest) {
cin >> number_of_tests;
}
for (size_t _ = 0; _ < number_of_tests; ++_) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int in[100009], n, m, x, y, last, current;
bool visited[100009];
vector<int> listt[100009];
map<pair<int, int>, int> idx;
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
in[y]++, idx[{x, y}] = i + 1;
listt[x].push_back(y);
}
queue<pair<int, int>> q;
for (int i = 1; i <= n; i++)
if (in[i] == 0) q.push({i, 0});
while (q.size()) {
if (q.size() > 1) {
cout << "-1";
return 0;
}
current = q.front().first, last = q.front().second;
q.pop();
for (int i = 0; i < listt[current].size(); i++) {
in[listt[current][i]]--;
if (in[listt[current][i]] == 0) {
visited[listt[current][i]] = true;
last = max(last, idx[{current, listt[current][i]}]);
q.push({listt[current][i], last});
}
}
}
cout << last;
return 0;
}
| 4 |
#include<cstdio>
#include<iostream>
using namespace std;
#define MAX 2000000
int H,A[MAX+1];
void maxHeapify(int i)
{
int l,r,largest;
l = 2*i;
r = 2*i + 1;
if(l <= H && A[l] > A[i]) largest = l;
else largest = i;
if(r <= H && A[r] > A[largest]) largest = r;
if(largest != i)
{
swap(A[i],A[largest]);
maxHeapify(largest);
}
}
int main()
{
int i;
cin >> H;
for(i=1;i<=H;i++)
{
cin >> A[i];
}
for(i = H/2 ;i>=1;i--)
{
maxHeapify(i);
}
for(i=1;i<=H;i++)
{
cout << " " << A[i];
}
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = (int)2e5 + 7;
int n, m, t, l, a[MaxN];
char str[MaxN];
int main() {
int i;
scanf("%d%d", &n, &t);
scanf("%s", str + 1);
l = strlen(str + 1);
for (i = 1; i <= l; i++) {
if (str[i] == '.') break;
a[i] = str[i] - '0';
}
m = i;
for (i = m + 1; i <= l; i++) {
a[i - 1] = str[i] - '0';
if (str[i] >= '5') break;
}
if (i <= l) {
i--;
while (t-- && i >= m) {
i--;
l = i;
a[i]++;
while (a[i] > 9) a[i - 1]++, a[i] = 0, i--;
while (i >= m && a[i] < 5) i--;
}
}
while (l >= m && a[l] == 0) l--;
if (a[0] != 0) printf("%d", a[0]);
for (i = 1; i < m; i++) printf("%d", a[i]);
if (m <= l) {
printf(".");
for (i = m; i <= l; i++) printf("%d", a[i]);
}
printf("\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, e, f[5009], dp[5002][5002], jm[5009];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> a;
for (b = 1; b <= a; b++) {
cin >> f[b];
jm[b] = f[b] + jm[b - 1];
}
for (b = a; b >= 1; b--) {
d = a;
dp[b][1] = jm[a] - jm[b - 1];
for (c = 2; c <= a - b + 1; c++) {
dp[b][c] = -1;
while (b < d) {
if (d + c - 2 > a) {
d--;
continue;
}
if (jm[d - 1] - jm[b - 1] <= dp[d][c - 1]) {
dp[b][c] = jm[d - 1] - jm[b - 1];
break;
}
d--;
}
}
}
for (b = a; b >= 1; b--) {
if (dp[1][b] != -1) {
cout << a - b;
return 0;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
priority_queue<long long int> pq;
int x;
for (int i = 0; i < n; i++) {
cin >> x;
pq.push(-1 * x);
}
if ((pq.size() & 1) == 0) pq.push(0);
long long ans = 0;
while (pq.size() > 1) {
long long int sum = pq.top();
pq.pop();
sum += pq.top();
pq.pop();
sum += pq.top();
pq.pop();
ans -= sum;
pq.push(sum);
}
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long numMatch;
long long C;
vector<long long> vecRes;
set<long long> setRes;
long long getCompareRes(long long n) {
long long tmp = n * (n - 1) / 2 + C * n;
if (tmp == numMatch) {
return 0;
} else if (tmp < numMatch) {
return -1;
} else {
return 1;
}
}
long long getAns() {
long long lb = 1, ub;
if (C == 0) {
ub = 1LL << 32 - 1;
} else {
ub = min(1LL << 32 - 1, numMatch / C);
}
long long mid;
while (lb <= ub) {
long long mid = (lb + ub) / 2;
int compareRes = getCompareRes(mid);
if (compareRes == -1) {
lb = mid + 1;
} else if (compareRes == 1) {
ub = mid - 1;
} else {
return mid;
}
}
return -1;
}
long long getBase(int exp) {
long long base = 1;
for (int i = 0; i < exp; i++) {
base *= 2;
}
return base;
}
int main() {
while (scanf("%I64d", &numMatch) != -1) {
setRes.clear();
for (int i = 0; i <= 64; i++) {
C = getBase(i) - 1;
long long res = getAns();
if ((res != -1) && res % 2 != 0) {
setRes.insert(res * (C + 1));
}
}
if (setRes.empty()) {
printf("-1\n");
continue;
}
vecRes.clear();
for (set<long long>::iterator it = setRes.begin(); it != setRes.end();
it++) {
vecRes.push_back(*it);
}
sort(vecRes.begin(), vecRes.end());
for (int i = 0; i < vecRes.size(); i++) {
cout << vecRes[i] << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int imax = 1e9 + 7;
const long long lmax = 1e18;
vector<int> v;
int getLength(int n) {
int cnt = 0;
while (n > 0) {
cnt++;
n /= 10;
}
return cnt;
}
int calc() {
vector<long long> cnt(15, 0);
long long i, n, val, ans = 0, m, tm, d;
cin >> n;
v.resize(n);
for (i = 0; i < ((int)v.size()); ++i) {
cin >> v[i];
cnt[getLength(v[i])]++;
}
const long long Mod = 998244353;
int j, pos;
for (i = 0; i < n; ++i) {
val = v[i];
pos = 1;
tm = 1;
for (; val > 0; tm = (tm * 10) % Mod, pos++) {
d = val % 10;
val /= 10;
m = tm;
for (j = 1; j < pos; j++) {
m = (m * 10) % Mod;
ans += (d * cnt[j] * m) % Mod;
ans %= Mod;
}
for (; j < 15; j++) {
ans += (d * cnt[j] * m) % Mod;
ans %= Mod;
ans += (d * cnt[j] * m * 10) % Mod;
ans %= Mod;
}
}
}
cout << ans;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
calc();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e6 + 10, inf = 1e18, mod = 1e9 + 7;
struct pair_hash {
template <class T1, class T2>
std::size_t operator()(const std::pair<T1, T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ h2;
}
};
long long pw(long long te, long long tee) {
if (tee == 0) return 1;
long long res = pw(te, tee / 2);
return (tee % 2 ? te * res * res : res * res);
}
long long n, k;
string ans;
void Read_input() { cin >> n >> k; }
void Solve() {
if (k == 1) {
ans += '0';
for (long long i = 1; i < n; i++) ans += '1';
cout << ans;
} else {
long long tm = (n - k) / 2 + 1;
for (long long i = 1; i <= n; i++) cout << (i % tm != 0);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
Read_input(), Solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n >> m;
vector<long long> arr(m);
for (long long& i : arr) cin >> i;
if (n == 1) {
cout << 0 << endl;
return 0;
}
map<long long, long long> left;
map<long long, long long> right;
for (long long i = 0; i < n; ++i) {
left[i + 1] = 1;
right[i + 1] = 1;
}
for (long long i = 0; i < m; ++i) {
left[arr[i] + i + 2] += left[arr[i] + i + 1];
left[arr[i] + i + 1] = 0;
right[arr[i] - i - 2] += right[arr[i] - i - 1];
right[arr[i] - i - 1] = 0;
}
long long ans = 0;
for (auto [i, j] : right) {
ans += j * min(n, i + m + 1ll);
}
for (auto [i, j] : left) {
ans -= j * (max(1ll, i - m - 1ll) - 1ll);
}
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
double x, y, a[50050], b[50050], ans;
struct data {
double angle;
int id;
friend bool operator<(data a, data b) { return a.angle < b.angle; }
} d[100010];
int cnt, last[50050];
int sum[100010];
inline void add(int p, int w) {
while (p <= cnt) sum[p] += w, p += p & -p;
}
inline int qry(int p) {
int s = 0;
while (p) s += sum[p], p -= p & -p;
return s;
}
int check(double r) {
double A, B, C, nx, ny, delt;
cnt = 0;
for (int i = 1; i <= n; ++i) {
A = 1 + a[i] * a[i];
B = -2 * x + 2 * a[i] * (b[i] - y);
C = x * x + (b[i] - y) * (b[i] - y) - r * r;
if (B * B <= 4 * A * C) continue;
delt = sqrt(B * B - 4 * A * C);
nx = (-B + delt) / A / 2;
ny = a[i] * nx + b[i];
d[++cnt] = data{atan2(ny - y, nx - x), i};
nx = (-B - delt) / A / 2;
ny = a[i] * nx + b[i];
d[++cnt] = data{atan2(ny - y, nx - x), i};
}
sort(d + 1, d + 1 + cnt);
int res = 0;
for (int i = 1; i <= cnt; ++i) {
if (last[d[i].id])
res += qry(i) - qry(last[d[i].id]), add(last[d[i].id], -1),
last[d[i].id] = 0;
else
add(i, 1), last[d[i].id] = i;
}
return res;
}
int pre[100010], nxt[100010], lst;
inline double calc(int i, int j) {
double nx = (b[j] - b[i]) / (a[i] - a[j]), ny = nx * a[i] + b[i];
return sqrt((nx - x) * (nx - x) + (ny - y) * (ny - y));
}
int main() {
scanf("%d %lf %lf %d", &n, &x, &y, &m);
x /= 1000, y /= 1000;
for (int i = 1; i <= n; ++i)
scanf("%lf %lf", &a[i], &b[i]), a[i] /= 1000, b[i] /= 1000;
double l = 0, r = 100000000000000, mid;
for (int t = 1; t <= 100; ++t) {
mid = (l + r) / 2;
if (check(mid) >= m)
r = mid;
else
l = mid;
}
check(l);
int res = 0;
lst = 0;
for (int i = 1; i <= cnt; ++i) {
if (last[d[i].id]) {
int p = lst;
while (p > last[d[i].id])
++res, ans += calc(d[i].id, d[p].id), p = pre[p];
nxt[pre[p]] = nxt[p];
pre[nxt[p]] = pre[p];
if (p == lst) lst = pre[lst];
} else {
nxt[lst] = i;
pre[i] = lst;
lst = i;
last[d[i].id] = i;
}
}
ans += (m - res) * l;
printf("%.12lf", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
int a[maxn];
struct node {
int az, id;
bool operator<(const node& a) const { return az < a.az; }
};
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, i, j;
cin >> n;
priority_queue<node> p;
for (i = 1; i <= n; i++) {
int x;
cin >> x;
struct node aa;
aa.az = x;
aa.id = i;
if (x != 0) p.push(aa);
}
vector<pair<int, int> > ans;
while (!p.empty()) {
struct node aa, bb;
aa = p.top();
p.pop();
if (!p.empty()) {
bb = p.top();
p.pop();
} else
break;
int xx = aa.az;
int yy = bb.az;
aa.az--;
bb.az--;
ans.push_back(make_pair(aa.id, bb.id));
if (aa.az > 0) p.push(aa);
if (bb.az > 0) p.push(bb);
}
int k = ans.size();
cout << k << endl;
for (auto it : ans) {
printf("%d %d\n", it.first, it.second);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long q;
long long n, k;
string s;
long long fun(string &x) {
long long cnt = 0;
for (long long i = 0; i <= k - 1; i++) {
if (s[i] == x[i % 3]) ++cnt;
}
long long ans = cnt;
for (long long i = k; i <= n - 1; i++) {
if (s[i - k] == x[(i - k) % 3]) --cnt;
if (s[i] == x[i % 3]) ++cnt;
ans = max(ans, cnt);
}
return (k - ans);
}
void solve() {
cin >> q;
while (q--) {
cin >> n >> k;
cin >> s;
string x[3] = {"RGB", "GBR", "BRG"};
cout << min({fun(x[0]), fun(x[1]), fun(x[2])}) << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
solve();
}
| 4 |
#include <bits/stdc++.h>
const long long mod = 1000000000 + 7;
const long long INF = 9000000000000000000;
using namespace std;
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b / (gcd(a, b))); }
void solve() {
long long n;
cin >> n;
vector<pair<long long, long long> > arr;
for (long long i = 0; i < n; i++) {
long long x, y;
cin >> x >> y;
arr.push_back({x, y});
}
sort(arr.begin(), arr.end());
long long cnt = 0;
for (long long i = 0; i < n; i++) {
long long x1 = arr[i].first, y1 = arr[i].second;
for (long long j = i + 2; j < n; j++) {
long long x2 = arr[j].first, y2 = arr[j].second;
long long temp1 = abs(x2 + x1), temp2 = abs(y2 + y1);
if (temp1 % 2 == 0 && temp2 % 2 == 0) {
temp1 = x2 + x1;
temp2 = y2 + y1;
temp1 /= 2;
temp2 /= 2;
pair<long long, long long> temp = {temp1, temp2};
auto it = lower_bound(arr.begin(), arr.end(), temp);
if (it != arr.end() && (*it) == temp) cnt++;
}
}
}
cout << cnt << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long tests = 1;
while (tests--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool is_square(long long a, long long b) {
long long res = a * b;
if (res < 0) {
return false;
}
long long s = sqrt(res);
for (int i = -1; i <= 1; i++)
if ((s + i) * (s + i) == res) return true;
return false;
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cout << setprecision(20);
int n;
cin >> n;
vector<int> vv(n);
for (int i = 0; i < n; i++) cin >> vv[i];
vector<bool> been(n, false);
vector<int> cmp(n, -1);
int n_cmp = 0;
for (int i = 0; i < n; i++) {
if (!vv[i]) {
cmp[i] = -1;
continue;
}
if (been[i]) continue;
queue<int> s;
s.push(i);
been[i] = true;
cmp[i] = n_cmp;
while (!s.empty()) {
int v = s.front();
s.pop();
for (int j = i + 1; j < n; j++) {
if (vv[j] && is_square(vv[i], vv[j]) && !been[j]) {
been[j] = true;
cmp[j] = n_cmp;
s.push(j);
}
}
}
n_cmp++;
}
vector<int> ans(n + 1, 0);
for (int i = 0; i < n; i++) {
vector<bool> b(n, false);
int x = 0;
for (int j = i; j < n; j++) {
if (cmp[j] != -1 && !b[cmp[j]]) {
b[cmp[j]] = true;
x++;
}
ans[max(x, 1)]++;
}
}
for (int i = 1; i < n + 1; i++) cout << ans[i] << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int n, i, j, count = 0;
long int d;
scanf("%d %ld", &n, &d);
long int a[n];
for (i = 0; i < n; i++) {
scanf("%ld", &a[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (abs(a[i] - a[j]) <= d) count++;
}
}
printf("%ld", count * 2);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, x, y;
bool chk(long long mid) {
long long num = mid / x;
long long f = mid - num;
num = mid / y;
long long s = mid - num;
return a <= f && b <= s && mid - (mid / (x * y)) >= a + b;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> a >> b >> x >> y;
long long st = a + b, ed = 1e12;
while (st < ed) {
long long mid = (st + ed) / 2;
if (chk(mid))
ed = mid;
else
st = mid + 1;
}
cout << ed;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N, M, Q;
pair<int, int> e[200201];
int res[200201];
int isBi = 1;
int p[200201], col[200201], rnk[200201], bi[200201];
vector<array<int, 5> > undo;
pair<int, int> Find(int n) {
if (n == p[n]) return make_pair(n, 0);
auto ret = Find(p[n]);
ret.second ^= col[n];
return ret;
}
void Union(int a, int b) {
auto ita = Find(a), itb = Find(b);
a = ita.first;
b = itb.first;
if (a == b) {
undo.push_back({a, a, 0, bi[a], isBi});
if (ita.second == itb.second) bi[a] = 0, isBi = 0;
return;
}
if (rnk[a] > rnk[b]) swap(a, b);
p[a] = b;
col[a] = ita.second ^ itb.second ^ 1;
if (rnk[a] == rnk[b])
rnk[b]++, undo.push_back({a, b, 1, bi[b], isBi});
else
undo.push_back({a, b, 0, bi[b], isBi});
bi[b] &= bi[a];
isBi &= bi[b];
}
void Undo() {
auto [a, b, r, bb, bbb] = undo.back();
undo.pop_back();
p[a] = a;
if (r) rnk[b]--;
bi[b] = bb;
isBi = bbb;
}
void dnc(int L, int R, int le, int ri) {
int mid = L + R >> 1;
res[mid] = min(ri, mid + 1);
int cnt = 0;
for (int i = R; i > mid; i--) Union(e[i].first, e[i].second), cnt++;
if (isBi == 0)
res[mid] = le;
else {
for (int i = le + 1; i <= min(ri - 1, mid); i++) {
Union(e[i].first, e[i].second);
cnt++;
if (isBi == 0) {
res[mid] = i;
break;
}
}
}
while (cnt > 0) Undo(), cnt--;
if (L <= mid - 1) {
for (int i = R; i >= mid; i--) Union(e[i].first, e[i].second), cnt++;
dnc(L, mid - 1, le, res[mid]);
while (cnt > 0) Undo(), cnt--;
}
if (mid + 1 <= R) {
for (int i = le + 1; i <= res[mid]; i++)
Union(e[i].first, e[i].second), cnt++;
dnc(mid + 1, R, res[mid], ri);
while (cnt > 0) Undo(), cnt--;
}
}
int main() {
for (int i = 0; i < 200201; i++) p[i] = i, bi[i] = 1;
scanf("%d %d %d", &N, &M, &Q);
for (int i = 1; i <= M; i++) scanf("%d %d", &e[i].first, &e[i].second);
dnc(1, M, 0, M + 1);
while (Q--) {
int l, r;
scanf("%d %d", &l, &r);
if (l <= res[r])
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, z, arr[100100];
string k, t;
int main() {
cin >> n;
if (n % 10 < 5)
cout << n / 10 * 10;
else
cout << (n + 9) / 10 * 10;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define int long long
int N,M;
vector<int>A,B,C;
signed main(){
cin>>N;A.resize(N);
for(int i=0;i<N;i++)cin>>A[i];
cin>>M;
B.resize(M);C.resize(M);
for(int i=0;i<M;i++)cin>>B[i];
for(int i=0;i<M;i++)cin>>C[i];
sort(A.begin(),A.end());
vector<int>sum(N);
for(int i=0;i<N;i++){
sum[i]=A[i]+(i?sum[i-1]:0);
}
for(int i=0;i<M;i++){
int u=upper_bound(A.begin(),A.end(),B[i])-A.begin();
int val;
if(u==0)val=0;
else val=sum[u-1];
if(C[i]<=val)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> m >> n;
while (n--) {
if (m % 10 == 0)
m /= 10;
else
m--;
}
cout << m;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
vector<int> g[N];
int cnt[1 << 24], cnt2[1 << 24], n;
int a[N], x, is, val, have[N], ed;
void dfs(int x = 1, int d = 0, int fa = 0) {
if (g[x].size() == 0) {
is = (d & 1);
}
for (auto &it : g[x])
if (it != fa) dfs(it, d + 1, x);
}
void dfs2(int x = 1, int d = 0, int fa = 0) {
if ((d & 1) == is) have[++ed] = a[x];
for (auto &it : g[x])
if (it != fa) dfs2(it, d + 1, x);
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); ++i) scanf("%d", a + i), cnt[a[i]]++;
for (int i = (2); i <= (n); ++i) {
scanf("%d", &x);
g[x].push_back(i);
}
dfs(), dfs2();
for (int i = (1); i <= (ed); ++i) val ^= have[i], cnt2[have[i]]++;
long long ans = 0;
for (int i = (1); i <= (ed); ++i) {
int tmp = val ^ have[i];
ans += cnt[tmp] - cnt2[tmp];
}
if (!val) {
ans += 1ll * ed * (ed - 1) / 2;
ans += 1ll * (n - ed) * (n - ed - 1) / 2;
}
printf("%lld\n", ans);
return 0;
}
| 5 |
#include <iostream>
#include <cmath>
#include <vector>
#include <numeric>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define ll long long int
#define i_run_i_speed_i_sanic ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void me_gusta_senorita() {
int n, k;
cin >> n >> k;
vector<int> num(n);
int mex = 0, mx = -1;
map<int, bool> found;
for (int i = 0; i < n; i++) {
cin >> num[i];
found[num[i]] = true;
mx = max(mx, num[i]);
}
sort(all(num));
for (int i = 0; i < n; i++)
if (num[i] == mex)
mex++;
int element = (mx + mex + 1) / 2;
if (mex > mx)
cout << n + k << endl;
else
cout << n + (!found[element] && (k)) << endl;
return;
}
int main() {
i_run_i_speed_i_sanic
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
ll cases = 1;
cin >> cases;
while (cases--)
me_gusta_senorita();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long kadane(const vector<long long> &v) {
long long ans = 0;
long long curr = 0;
for (long long n : v) {
curr += n;
ans = max(ans, curr);
if (curr < 0) curr = 0;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
scanf("%d", &n);
vector<long long> v(n);
for (int i = 0; i < n; i++) scanf("%lld", &v[i]);
vector<long long> v2(n - 1), v3(n - 1);
int x = -1;
for (int i = 0; i + 1 < n; i++) {
v2[i] = abs(v[i] - v[i + 1]) * x;
v3[i] = v2[i] * -1;
x = (x == -1) ? 1 : -1;
}
printf("%lld\n", max(kadane(v2), kadane(v3)));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, sum;
cin >> n >> m;
if (n > m) {
n = n - m;
cout << m << " ";
sum = n / 2;
} else {
m = m - n;
cout << n << " ";
sum = m / 2;
}
cout << sum;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pci = pair<char, int>;
using vi = vector<int>;
using vll = vector<ll>;
const ll infll = 1e18 + 3;
const int maxn = 1e6 + 77;
const ll basell = 1e18 + 3;
const int mod = 1e9 + 7;
const ld eps = 1e-7;
const int inf = 1009000999;
const int baseint = 1000200013;
const ld PI = acos(-1.0);
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
int inline solve() {
ios::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
;
int n, k;
cin >> n >> k;
string str;
cin >> str;
char a = 'z', b = 'a';
set<char> h;
for (int i = 0; i < n; ++i) {
h.insert(str[i]);
a = min(str[i], a);
b = max(str[i], b);
}
if (n < k) {
for (int j = 0; j < n; ++j) {
cout << str[j];
}
for (int i = n; i < k; ++i) {
cout << a;
}
return 0;
}
int o = 0;
for (int i = n - 1; i >= 0; --i) {
if (str[i] < b && i < k) {
o = 1;
for (int j = 0; j < i; ++j) {
cout << str[j];
}
for (auto it = h.begin(); it != h.end(); ++it) {
if (*it > str[i]) {
cout << *it;
break;
}
}
for (int j = i + 1; j < k; ++j) {
cout << a;
}
break;
}
}
if (o == 0) {
for (int j = 0; j < n; ++j) {
cout << str[j];
}
}
}
int32_t main() { solve(); }
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct Dim {
long long int x, y;
};
vector<vector<long long int> > g;
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
long long int first = 0, second = 0;
vector<long long int> middle;
for (long long int n = (0); n < (long long int)(N); ++n) {
int S;
cin >> S;
if (S % 2 == 0) {
for (long long int s = (0); s < (long long int)(S); ++s) {
int x;
cin >> x;
if (s < S / 2) {
first += x;
} else {
second += x;
}
}
} else {
for (long long int s = (0); s < (long long int)(S); ++s) {
int x;
cin >> x;
if (s < S / 2) {
first += x;
} else if (s == S / 2) {
middle.push_back(x);
} else {
second += x;
}
}
}
}
sort(middle.begin(), middle.end());
reverse(middle.begin(), middle.end());
for (long long int i = (0); i < (long long int)(middle.size()); ++i) {
if (i % 2 == 0) {
first += middle[i];
} else {
second += middle[i];
}
}
cout << first << " " << second << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1100000;
const long long MOD = 1000000007;
vector<long long> v;
long long frac[N + 10], ifrac[N + 10];
long long g[N + 10], f[N + 10], sum[N + 10], temp[N + 10], k, tp[N + 10];
int n, q;
struct Mobius {
int mu[N + 10], sum[N + 10], primes[N + 10], tot;
bool f[N + 10];
void init() {
memset(mu, 0, sizeof(mu));
memset(f, 0, sizeof(f));
memset(sum, 0, sizeof(sum));
sum[1] = mu[1] = 1;
for (int i = 2; i <= N; i++) {
if (!f[i]) {
primes[tot++] = i;
mu[i] = -1;
}
for (long long j = 0, k; j < tot; j++) {
k = 1ll * i * primes[j];
if (k > N) break;
f[k] = 1;
if (i % primes[j] == 0) {
mu[k] = 0;
break;
}
mu[k] = -mu[i];
}
sum[i] = sum[i - 1] + mu[i];
}
}
} Mu;
long long P(long long x, long long y) {
long long ans = 1;
for (; y; y >>= 1, x = x * x % MOD)
if (y & 1) ans = ans * x % MOD;
return ans;
}
long long getans(int x) {
if (sum[x] >= k)
return frac[sum[x]] * ifrac[k] % MOD * ifrac[sum[x] - k] % MOD;
return 0;
}
int main() {
Mu.init();
scanf("%d%lld%d", &n, &k, &q);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
for (int j = 1; j * j <= x; j++) {
if (x % j != 0) continue;
sum[j]++;
if (j * j != x) sum[x / j]++;
}
}
frac[0] = 1;
for (int i = 1; i <= N; i++) frac[i] = frac[i - 1] * i % MOD;
for (int i = 0; i <= N; i++) ifrac[i] = P(frac[i], MOD - 2);
for (int i = 1; i <= N; i++) f[i] = getans(i);
for (int i = 1; i <= N; i++)
for (int j = i; j <= N; j += i) g[i] = (g[i] + f[j] * Mu.mu[j / i]) % MOD;
long long ans = 0;
for (int i = 1; i <= N; i++) ans = (ans + i * g[i]) % MOD;
for (long long i = 1; i <= N; i++)
for (long long j = i; j <= N; j += i)
tp[j] = (tp[j] + Mu.mu[j / i] * i + MOD) % MOD;
for (int i = 1; i <= q; i++) {
int x;
scanf("%d", &x);
v.clear();
for (int j = 1; j * j <= x; j++) {
if (x % j != 0) continue;
v.push_back(j);
if (j * j != x) v.push_back(x / j);
}
for (int j = 0; j < v.size(); j++) {
sum[v[j]]++;
temp[v[j]] = (getans(v[j]) - f[v[j]] + MOD) % MOD;
f[v[j]] = (f[v[j]] + temp[v[j]] % MOD);
ans = (ans + temp[v[j]] * tp[v[j]] % MOD + MOD) % MOD;
}
printf("%lld\n", ans);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, j, i;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int len = 0;
long long int arr[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
len = max(arr[i], len);
}
string s;
char c = 'a';
for (i = 0; i <= len; i++) {
s += c;
c++;
if (c == 'z') {
c = 'a';
}
}
if (len == 0) {
s = 'a';
}
string s1 = "";
cout << s << endl;
for (i = 0; i < n; i++) {
s1 = "";
for (j = 0; j < len + 1; j++) {
long long int x = 0;
if (arr[i] > 0) {
arr[i]--;
s1 += s[j];
} else {
char c = s[j];
if (c == 'z') {
s1 += c - 1;
} else {
s1 += c + 1;
}
}
}
s = s1;
cout << s1 << endl;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, inf = 1e9;
long long arr[maxn], n, k;
int cnt = 0, cnt2 = 0;
int num(string s) {
int ans = 0, t = 0;
if (s[0] == '-') t++;
for (int i = t; i < s.size(); i++) ans = ans * 10 + (int)(s[i] - '0');
if (t) ans *= -1;
return ans;
}
void input() {
cin >> n >> k;
for (int i = 0; i < n + 1; i++) {
string s;
cin >> s;
if (s == "?")
cnt++, arr[i] = inf;
else
cnt2++, arr[i] = num(s);
}
}
bool check() {
long long ans = 0, p = k;
for (int i = n; i > 0; i--) {
arr[i - 1] += arr[i] * k;
cerr << arr[i] << endl;
if (abs(arr[i - 1]) > (1e9)) return false;
}
cerr << arr[0] << endl;
if (arr[0] == 0) return true;
return false;
}
int main() {
input();
if (k == 0) {
if (arr[0] == inf) {
if (cnt2 % 2 == 0)
cout << "No" << endl;
else
cout << "Yes" << endl;
} else if (arr[0] != 0)
cout << "No" << endl;
else
cout << "Yes" << endl;
} else {
if (cnt != 0) {
if (cnt2 % 2 == cnt % 2)
cout << "Yes" << endl;
else
cout << "No" << endl;
} else if (check())
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long diff = -2e10, mx = -2e10;
for (int i = 0; i < n; ++i) {
mx = max(mx, a[i]);
diff = max(mx - a[i], diff);
}
int ans = 0;
while (diff) {
diff /= 2;
++ans;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;
const int N = 10000002;
int num[N];
int main() {
for (int i = 1; i * i < N; ++i) {
for (int j = 1; i * i * j < N; ++j) {
if (i == 1)
num[j] = j;
else
num[i * i * j] = min(num[i * i * j], j);
}
}
int a, n;
cin >> a >> n;
long long ans = 0;
for (int i = a; i < a + n; ++i) ans += num[i];
cout << ans << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char s[2000006], S[4000006];
long long R[4000006], n;
long long N, a[2000006];
const long long Mod = 51123987;
void Manacher(char *s, int n) {
long long p = 0, mx = 0;
for (int i = 1; i <= n; i++) {
if (mx > i)
R[i] = min(mx - i, R[2 * p - i]);
else
R[i] = 1;
while (S[R[i] + i] == S[i - R[i]]) R[i]++;
if (i + R[i] - 1 > mx) mx = i + R[i] - 1, p = i;
}
}
long long Sum, Ans, Start[2000006], Finish[2000006];
int main() {
scanf("%d", &n);
scanf("%s", s + 1);
S[0] = '$';
S[1] = '#';
for (int i = 1; s[i]; i++) S[i << 1] = s[i], S[(i << 1) + 1] = '#';
N = n * 2 + 1;
S[++N] = '@';
Manacher(S, N);
for (int i = 1; i <= N; i++) {
if (S[i] == '#')
a[(i - (R[i] - 2)) >> 1]++, a[(i + 1) >> 1]--;
else
a[(i - (R[i] - 2)) >> 1]++, a[(i + 2) >> 1]--;
Sum = Sum + (R[i] >> 1);
}
for (int i = 1; i <= n; i++) Start[i] = Start[i - 1] + a[i];
memset(a, 0, sizeof(a));
for (int i = 1; i <= N - 1; i++) {
if (S[i] == '#')
a[(i + 1) >> 1]++, a[(i + R[i]) >> 1]--;
else
a[i >> 1]++, a[(i + R[i]) >> 1]--;
}
for (int i = 1; i <= n; i++) Finish[i] = Finish[i - 1] + a[i];
long long S1 = Sum - 1;
if (S1 & 1)
Sum >>= 1;
else
S1 >>= 1;
Ans = (S1 % Mod) * (Sum % Mod) % Mod;
S1 = 0;
for (int i = 1; i <= n; i++) {
Ans = (Ans + Mod - S1 % Mod * Start[i] % Mod) % Mod;
(S1 += Finish[i]) %= Mod;
}
printf("%I64d\n", Ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10, mod = 998244353;
long long dp[N][N], n, ans[N];
int main() {
cin >> n;
dp[1][1] = ans[1] = 1;
for (int i = 2; i <= n; ++i)
for (int j = 1; j <= i; ++j) {
dp[i][j] = (dp[i - 1][j] * j + dp[i - 1][j - 1] * (i - j + 1)) % mod;
ans[j] = (ans[j] * i + dp[i][j]) % mod;
}
for (int i = 1; i <= n; ++i) cout << ans[i] << " ";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
char s[maxn][maxn];
int n, m;
bool vis[maxn][maxn];
bool f(int x, int y);
bool ok(int x, int y) {
if (x < 0 || x >= n - 1 || y < 0 || y >= m - 1 || vis[x][y]) return 0;
return f(x, y);
}
struct node {
int x, y;
node() {}
node(int a, int b) : x(a), y(b) {}
} q[maxn * maxn];
bool f(int x, int y) {
int c = 0;
if (s[x][y] == '*') {
c++;
}
if (s[x][y + 1] == '*') {
c++;
}
if (s[x + 1][y] == '*') {
c++;
}
if (s[x + 1][y + 1] == '*') {
c++;
}
return c == 1;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", s + i);
}
int front = 1, tail = 0;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < m - 1; j++)
if (f(i, j)) {
q[++tail] = node(i, j);
}
while (tail >= front) {
node t = q[front++], p;
int c = 0;
if (s[t.x][t.y] == '*') {
c++;
p = node(t.x, t.y);
}
if (s[t.x][t.y + 1] == '*') {
c++;
p = node(t.x, t.y + 1);
}
if (s[t.x + 1][t.y] == '*') {
c++;
p = node(t.x + 1, t.y);
}
if (s[t.x + 1][t.y + 1] == '*') {
c++;
p = node(t.x + 1, t.y + 1);
}
vis[t.x][t.y] = 1;
if (c == 1) {
s[p.x][p.y] = '.';
if (ok(p.x, p.y)) q[++tail] = node(p.x, p.y);
if (ok(p.x - 1, p.y - 1)) q[++tail] = node(p.x - 1, p.y - 1);
if (ok(p.x - 1, p.y)) q[++tail] = node(p.x - 1, p.y);
if (ok(p.x, p.y - 1)) q[++tail] = node(p.x, p.y - 1);
}
}
for (int i = 0; i < n; i++) printf("%s\n", s + i);
}
| 4 |
#include<cstdio>
#include<cstring>
#include<algorithm>
const int N=1000010;
int a[N],val[N][2],ret[N];
int n;
inline void Modify(int u,int v) {
if(a[val[u][0]]<a[v]) {
val[u][1]=val[u][0];
val[u][0]=v;
} else if(a[val[u][1]]<a[v] && v!=val[u][0]) {
val[u][1]=v;
}
}
signed main() {
scanf("%d",&n); int u=1<<n;
for(int i=1;i<=u;i++) {
scanf("%d",&a[i]);
}
val[0][0]=1;
for(int sta=1;sta<u;sta++) {
val[sta][0]=sta+1;
for(int i=0;i<n;i++)
if((1<<i)&sta) {
Modify(sta,val[sta^(1<<i)][0]);
Modify(sta,val[sta^(1<<i)][1]);
}
}
for(int i=1;i<u;i++) ret[i]=std::max(a[val[i][0]]+a[val[i][1]],ret[i-1]);
for(int i=1;i<u;i++) printf("%d\n",ret[i]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int i = -2 * 1e9, j = 2 * 1e9;
for (int k = 0; k < n; k++) {
string sign, ans;
int x;
cin >> sign >> x >> ans;
if ((sign == ">=" && ans == "N") || (sign == "<" && ans == "Y")) {
if (x - 1 < j) j = x - 1;
} else if ((sign == "<=" && ans == "N") || (sign == ">" && ans == "Y")) {
if (x + 1 > i) i = x + 1;
} else if ((sign == ">" && ans == "N") || (sign == "<=" && ans == "Y")) {
if (x < j) j = x;
} else {
if (x > i) i = x;
}
}
if (j < i) {
cout << "Impossible" << endl;
} else {
cout << i << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)998244353;
const long long maxn = (long long)1e5 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max() - 1;
const long long INF = numeric_limits<long long>::max() - 1;
void solve() {
long long n, k, d;
map<long long, long long> m;
cin >> n >> k >> d;
long long a[n + 5];
for (int i = 1; i <= (n); i++) cin >> a[i];
long long l = 1;
long long sz = 0, mx = 0;
for (int i = 1; i <= d; i++) m[a[i]]++, sz += (m[a[i]] == 1);
mx = sz;
for (int i = d + 1; i <= n; i++) {
m[a[l]]--;
if (!m[a[l]]) sz--;
m[a[i]]++;
if (m[a[i]] == 1) sz++;
l++;
mx = min(mx, sz);
}
cout << mx << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long T;
T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500010, P = 1000000007;
int n, a[maxn][2], st[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &a[i][0], &a[i][1]);
st[i * 2 - 1] = a[i][0], st[i * 2] = a[i][1];
}
sort(st + 1, st + n + n + 1);
long long ans = 0, sum = 0;
for (int i = 1; i <= n; i++) {
ans += a[i][0], sum += a[i][1];
}
ans = min(ans, sum), sum = 0;
for (int i = 1; i <= n; i++) {
sum += st[i];
}
for (int i = 1; i <= n; i++) {
if (max(a[i][0], a[i][1]) < st[n]) printf("%lld\n", sum), exit(0);
if (min(a[i][0], a[i][1]) > st[n]) printf("%lld\n", sum), exit(0);
long long t = sum + max(a[i][0], a[i][1]) - st[n];
ans = min(ans, t + max(0, min(a[i][0], a[i][1]) - st[n - 1]));
}
printf("%lld\n", ans);
return 0;
}
| 0 |
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#define debug(x) cerr << #x << " = " << (x) << endl;
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 500010
int main(){
int n,k,sum;
string s,t;
int left[SIZE] = {}, right[SIZE] = {};
cin >> n >> k >> s >> t;
for(int i=0;i<n;i++){
if(i == 0 || t[i-1] != t[i]) left[i]++;
if(i == n-1 || t[i] != t[i+1]) right[i]++;
}
for(int i=0;i<n;i++) right[i+1] += right[i];
for(int i=n;i>0;i--) left[i-1] += left[i];
sum = left[0];
priority_queue<pair<int,int> > pq0, pq1;
int sameDP = 0;
pq0.push({-INF, INF});
pq1.push({-INF, INF});
pq0.push({0, -1});
for(int i=0;i<n;i++){
while(pq0.size() && pq0.top().second < i - k){
pq0.pop();
}
while(pq1.size() && pq1.top().second < i - k){
pq1.pop();
}
if(t[i] != s[i]) sameDP = INF;
int p0 = -1 * pq0.top().first;
int p1 = -1 * pq1.top().first;
int l0 = pq0.top().second;
int l1 = pq1.top().second;
int calc0, calc1;
if(l0 == -1 || l0 == INF)
calc0 = p0 + (sum - left[i+1])/2 + 1;
else
calc0 = p0 + right[l0]/2 + (sum - left[i+1] - right[l0])/2 + 1;
if(l1 == -1 || l1 == INF)
calc1 = p1 + (sum - left[i+1])/2 + 1;
else
calc1 = p1 + right[l1]/2 + (sum - left[i+1] - right[l1])/2 + 1;
sameDP = min({calc0, calc1, sameDP});
if(right[i]%2 == 0)
pq0.push({-(sameDP - right[i]/2), i});
else
pq1.push({-(sameDP - right[i]/2), i});
}
printf("%d\n",sameDP);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
int n, a[maxn], tp, b[maxn], c[maxn], l[maxn], r[maxn], ch[maxn], ans;
struct mmp {
int len, id;
bool operator<(const mmp &op) const {
if (len == op.len)
return id > op.id;
else
return len < op.len;
}
} p;
priority_queue<mmp> pq;
int main() {
int i, j, x, y, id;
cin >> n;
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= n; i++) {
j = i;
while (j <= n && a[j] == a[i]) j++;
b[++tp] = a[i];
c[tp] = j - i;
l[tp] = tp - 1;
r[tp] = tp + 1;
p.id = tp;
p.len = j - i;
pq.push(p);
i = j - 1;
}
while (!pq.empty()) {
p = pq.top();
pq.pop();
id = p.id;
if (ch[id]) continue;
ans++;
ch[id] = 1;
x = l[id];
y = r[id];
r[x] = y;
l[y] = x;
if (x && y && b[x] == b[y]) {
c[x] += c[y];
p.len = c[x];
p.id = x;
pq.push(p);
ch[y] = 1;
r[x] = r[y];
l[r[y]] = x;
}
}
cout << ans << "\n";
return 0;
}
| 5 |
#include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
for(int i=0;i<n;i++)cin >> a[i];
int sum=0;
for(int i=0;i<n;i++){
sum+=a[i]-2*a[i]*(i%2);
}
cout << sum << " ";
for(int i=1;i<n;i++){
sum=-sum+2*a[i-1];
cout << sum << " ";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int check(vector<long long int> v, long long int ma, long long int x,
long long int k, long long int y) {
long long int n = v.size(), cnt = 0;
if (!n) return 0;
sort(v.begin(), v.end());
if (v[n - 1] > ma && n < k) return -1;
bool val = (v[n - 1] < ma ? true : false);
cnt += (n % k) * y;
n -= n % k;
if (!n) return cnt;
if (y * k >= x)
cnt += (n / k * x);
else if (val)
cnt += n * y;
else
cnt += (n - k) * y + x;
return cnt;
}
void solve() {
long long int n, m, x, k, y;
cin >> n >> m >> x >> k >> y;
long long int a[n], b[m];
for (long long int i = 0; i < n; i++) cin >> a[i];
for (long long int i = 0; i < m; i++) cin >> b[i];
long long int prev = -1;
vector<long long int> v;
long long int cnt = 0, ki = 0;
for (long long int i = 0; i < n; i++) {
if (a[i] != b[ki])
v.push_back(a[i]);
else {
long long int ma = max(prev, a[i]), xi = check(v, ma, x, k, y);
if (xi == -1) {
cout << -1 << endl;
return;
} else
cnt += xi;
v.clear();
prev = a[i], ++ki;
}
}
if (ki < m)
cout << -1 << endl;
else {
long long int xi = check(v, prev, x, k, y);
if (xi == -1) {
cout << -1 << endl;
return;
} else
cnt += xi;
cout << cnt << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 4 |
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
while(1){
int n;
cin>>n;
if(n==0) break;
int x[20],y[20];
for(int i=0;i<n;i++) cin>>x[i]>>y[i];
int vx=10,vy=10;
int ans[20];
for(int i=0;i<n;i++) ans[i]=0;
int m;
cin>>m;
char s[30];
int l[30];
for(int i=0;i<m;i++){
cin>>s[i]>>l[i];
for(int j=0;j<l[i];j++){
if(s[i]=='N') vy++;
else if(s[i]=='E') vx++;
else if(s[i]=='S') vy--;
else vx--;
for(int k=0;k<n;k++){
if(vx==x[k]&&vy==y[k]) ans[k]=1;
}
}
}
int tmp=1;
for(int i=0;i<n;i++){
if(ans[i]==0){
tmp=0;
break;
}
}
cout<<((tmp)?"Yes":"No")<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int a, ans, q0, q1;
int b[39][39];
int main() {
scanf("%d", &a);
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++) scanf("%d", &b[i][j]);
for (int i = 0; i < a; i++)
for (int j = 0; j < a; j++) {
q0 = 0;
q1 = 0;
for (int l = 0; l < a; l++) {
q0 += b[i][l];
q1 += b[l][j];
}
if (q0 < q1) ans++;
}
printf("%d", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > G[100010];
long long x[100010];
long long t_bisc[100010];
long long T[100010];
long long imp[100010];
long long dfs(long long a, long long p, long long t) {
T[a] = t;
for (pair<long long, long long> z : G[a]) {
if (z.first == p) continue;
dfs(z.first, a, t - 2 * z.second);
}
return 0;
}
map<long long, long long> use;
map<long long, long long> not_use;
long long use_time = 0;
long long num_bisc = 0;
long long dfs2(long long a, long long p) {
pair<long long, long long> map_p = make_pair(-t_bisc[a], x[a]);
map<long long, long long>::iterator it = not_use.find(-map_p.first), it2;
if (it == not_use.end()) {
not_use.emplace(-map_p.first, map_p.second);
} else {
it->second += map_p.second;
}
it2 = not_use.begin();
it = use.begin();
if (it != use.end() && it2 != not_use.end() && -it->first > it2->first) {
use_time += it2->first * it2->second;
num_bisc += it2->second;
it = use.find(-it2->first);
if (it == use.end())
use.emplace(-it2->first, it2->second);
else
it->second += it2->second;
not_use.erase(it2);
}
it = use.begin();
if (it != use.end() && use_time > T[a]) {
long long extra = use_time - T[a];
it = use.begin();
while (it != use.end() && extra >= (-it->first) * it->second) {
use_time -= (-it->first) * it->second;
num_bisc -= it->second;
extra -= (-it->first) * it->second;
it2 = not_use.find(-it->first);
if (it2 == not_use.end())
not_use.emplace(-it->first, it->second);
else
it2->second += it->second;
use.erase(it);
it = use.begin();
}
it = use.begin();
if (it != use.end() && extra > 0) {
long long pric = -it->first;
long long qnt = extra / pric + ((extra % pric > 0));
use_time -= pric * qnt;
num_bisc -= qnt;
it2 = not_use.find(pric);
if (it2 == not_use.end())
not_use.emplace(pric, qnt);
else
it2->second += qnt;
it->second -= qnt;
}
}
it2 = not_use.begin();
it = use.begin();
if (it2 != not_use.end() && use_time + it2->first <= T[a]) {
long long extra = T[a] - use_time;
while (it2 != not_use.end() && extra >= it2->first * it2->second) {
use_time += it2->first * it2->second;
num_bisc += it2->second;
extra -= it2->first * it2->second;
it = use.find(-it2->first);
if (it == use.end())
use.emplace(-it2->first, it2->second);
else
it->second += it2->second;
not_use.erase(it2);
it2 = not_use.begin();
}
it2 = not_use.begin();
if (it2 != not_use.end() && extra > 0) {
long long pric = it2->first;
long long qnt = extra / pric;
if (qnt) {
use_time += pric * qnt;
num_bisc += qnt;
it = use.find(-pric);
if (it == use.end())
use.emplace(-pric, qnt);
else
it->second += qnt;
it2->second -= qnt;
}
}
it = use.begin();
}
imp[a] = num_bisc;
for (pair<long long, long long> z : G[a]) {
if (z.first == p) continue;
dfs2(z.first, a);
}
it = use.find(map_p.first);
if (it == use.end()) {
it = not_use.find(-map_p.first);
if (it != not_use.end()) {
it->second -= map_p.second;
if (it->second <= 0) not_use.erase(it);
}
} else {
if (it->second <= map_p.second) {
long long minu = it->second;
use_time -= (-it->first) * it->second;
num_bisc -= it->second;
use.erase(it);
it = not_use.find(-map_p.first);
if (it != not_use.end()) {
it->second -= (map_p.second - minu);
if (it->second <= 0) not_use.erase(it);
}
} else {
use_time -= (-it->first) * map_p.second;
num_bisc -= map_p.second;
it->second -= map_p.second;
}
}
return 0;
}
long long dfs3(long long a, long long p) {
long long ma, ma2 = -1;
ma = -1;
for (pair<long long, long long> z : G[a]) {
if (z.first == p) continue;
long long poss = dfs3(z.first, a);
if (poss > ma) {
ma2 = ma;
ma = poss;
} else if (poss > ma2) {
ma2 = poss;
}
}
if (G[a].size() == 1 && p != -1) return imp[a];
return p == -1 ? max(ma, imp[a]) : max(ma2, imp[a]);
}
int main() {
long long n, t;
scanf("%lld %lld", &n, &t);
for (int i = 0; i < n; i++) scanf("%lld", &x[i]);
for (int i = 0; i < n; i++) scanf("%lld", &t_bisc[i]);
for (long long i = 1; i < n; i++) {
long long p, l;
scanf("%lld %lld", &p, &l);
p--;
G[p].push_back(make_pair(i, l));
G[i].push_back(make_pair(p, l));
}
dfs(0, -1, t);
use.clear();
not_use.clear();
dfs2(0, -1);
printf("%lld\n", dfs3(0, -1));
return 0;
}
| 6 |
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <functional>
#define pb push_back
#define fi first
#define sec second
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
int main(){
int n, m, a;
while(scanf("%d%d%d", &n, &m, &a),n||m||a){
vector<PP> v;
for(int i = 0; i < m; i++){
int h,p,q;
scanf("%d%d%d", &h, &p, &q);
v.pb(PP(h,P(p,q)));
}
sort(v.begin(),v.end(),greater<PP>());
for(int i = 0; i < m; i++){
if(a == v[i].sec.fi) a = v[i].sec.sec;
else if(a == v[i].sec.sec) a = v[i].sec.fi;
}
printf("%d\n", a);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int INF = 0x3f3f3f3f;
const double PI = 3.1415926535;
int main() {
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
if (n == 1)
cout << "1337" << endl;
else if (n == 2)
cout << "13377" << endl;
else {
vector<long long> v;
for (long long i = 44723; i >= 1;) {
if (i * (i - 1) / 2 <= n) {
v.push_back(i);
n -= i * (i - 1) / 2;
if (n == 0) break;
} else
i--;
}
v.push_back(0);
cout << '1';
for (int(i) = (int)(v.size() - 2); (i) >= (int)(0); --(i)) {
for (int(j) = 0; (j) < (int)(v[i] - v[i + 1]); ++(j)) {
cout << '3';
}
cout << 7;
}
cout << endl;
}
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
#define int long long
#define fr(i,a,b) for(int i(a),end##i(b);i<=end##i;i++)
#define fd(i,a,b) for(int i(a),end##i(b);i>=end##i;i--)
using namespace std;
const int mod=998244353;
const int maxn=4e7+5;
int ksm(int x,int k){
int ans=1;
while(k){
if(k&1)ans=ans*x%mod;
x=x*x%mod;
k>>=1;
}
return ans;
}
int jc[maxn],inv[maxn];
void init(int n){
jc[0]=1;
fr(i,1,n)jc[i]=jc[i-1]*i%mod;
inv[n]=ksm(jc[n],mod-2);
fd(i,n-1,0)inv[i]=inv[i+1]*(i+1)%mod;
}
int C(int n,int m){
if(n<0||m<0||n<m)return 0;
return jc[n]*inv[m]%mod*inv[n-m]%mod;
}
int D(int n,int m){
if(n<0||n+m<0||((n+m)&1))return 0;
return C(n,n+m>>1);
}
int x,y,t,ans;
signed main(){
#ifdef pig
freopen("f.in","r",stdin);
freopen("f.out","w",stdout);
#endif
cin>>t>>y>>x;
if(!x&&!y&&t){
cout<<1;
return 0;
}
init(3e7);y=x-y;
if(2*x-t-y==0)ans=D(t-1,2*x-t-1)-D(t-1,2*x-t+1);
fr(k,1,2*x-y){
int h=2*x-k-y;
(ans+=C(h+t-k-1,h)*(D(k-1,2*x-k-1)-D(k-1,2*x-k+1)))%=mod;
}
cout<<(ans+mod)%mod;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int k = 1, prev, next;
cin >> prev;
unsigned long long int ans = 0;
for (int i = 1; i < n; i++) {
cin >> next;
ans += k;
if (next == prev) {
k++;
} else
k = 1;
prev = next;
}
ans += k;
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, d;
int p[100];
int val[501000], s[501000];
int ans;
int que[501000], head, tail;
int main() {
int i, j, sum = 0;
scanf("%d%d", &n, &d);
for (i = 1; i <= n; ++i) scanf("%d", p + i);
sort(p + 1, p + n + 1);
for (i = 1; i <= n; ++i) {
if (sum + d < p[i])
break;
else
sum += p[i];
}
printf("%d ", sum);
val[0] = 1;
for (--i; i; --i)
for (j = sum - p[i]; ~j; --j)
if (val[j]) val[j + p[i]] = 1;
for (i = 1; i <= sum; ++i)
if (val[i]) {
while (que[head] < i - d) ++head;
s[i] = s[que[head]] + 1;
que[++tail] = i;
}
printf("%d\n", s[sum]);
return 0;
}
| 2 |
#include<iostream>
#include<string>
using namespace std;
int main(){
long long n,i=0,s=1;
cin>>n;
while(true){
if(s>=n)
break;
i++;
s*=3;
}
cout<<i<<endl;
return 0;
}
| 0 |
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> s;
int k;
while (cin >> k){
if (0 == k){
k = s.top();
s.pop();
cout << k << "\n";
}
else s.push(k);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long dp[210][210][210];
char s[210], t[5];
int main() {
cin >> n >> m;
cin >> s + 1 >> t + 1;
if (t[1] == t[2]) {
int cnt = 0;
for (int i = 1; i <= n; i++)
if (s[i] == t[1]) cnt++;
cnt += m;
if (cnt > n) cnt = n;
if (cnt < 2)
cout << 0 << endl;
else
cout << (cnt * (cnt - 1)) / 2 << endl;
return 0;
}
memset(dp, -0x3f, sizeof(dp));
for (int i = 0; i <= m; i++) dp[0][i][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= m; j++)
for (int k = 0; k <= i; k++) {
if (j == 0) {
if (s[i] == t[1])
dp[i][j][k] = dp[i - 1][j][k - 1];
else if (s[i] == t[2])
dp[i][j][k] = dp[i - 1][j][k] + k;
else
dp[i][j][k] = dp[i - 1][j][k];
} else {
if (s[i] == t[1])
dp[i][j][k] = max(dp[i - 1][j - 1][k] + k, dp[i - 1][j][k - 1]);
else if (s[i] == t[2])
dp[i][j][k] = max(dp[i - 1][j - 1][k - 1], dp[i - 1][j][k] + k);
else
dp[i][j][k] = max(dp[i - 1][j][k], max(dp[i - 1][j - 1][k] + k,
dp[i - 1][j - 1][k - 1]));
}
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[n][m][i]);
}
cout << ans << endl;
return 0;
}
| 6 |
#include <algorithm>
#include <cstring>
#include <cstdio>
typedef long long ll;
ll arr[300005], sum[300005], dp_l[300005], dp_r[300005], dp[300005], tmp[300005], ans[300005];
int que[300005];
inline void rev(int n)
{
std::reverse(arr + 1, arr + n + 1);
for (int i = 1; i <= n; i++)
sum[i] = sum[i - 1] - arr[i];
}
inline ll calc(ll *val, int x, int y)
{
ll a = val[x] + (ll)x * (x - 1) / 2 - sum[x], b = val[y] + (ll)y * (y - 1) / 2 - sum[y];
return std::max((ll)y + 1, (b - a - 1) / (y - x) + 1);
}
inline void init(ll *res, int n)
{
int he = 0, ta = 0;
for (int i = 1; i <= n; i++)
{
while (he < ta && calc(res, que[ta - 1], que[ta]) <= i)
ta--;
int pos = que[ta];
// printf("%d %d\n", i, pos);
res[i] = std::max(res[i - 1], res[pos] + (ll)(i - pos) * (i - pos + 1) / 2 + sum[i] - sum[pos]);
while (he < ta && calc(res, que[ta - 1], que[ta]) <= calc(res, que[ta], i))
ta--;
que[++ta] = i;
}
}
void work(int l, int r, int coef)
{
if (l == r)
{
dp[l] = std::max(dp[l], -arr[l] + 1 + dp_l[l - 1] + dp_r[r + 1]);
return;
}
int m = l + r + coef >> 1, he = 0, ta = -1;
for (int i = l - 1; i < m; i++)
{
while (he < ta && calc(dp_l, que[ta - 1], que[ta]) <= calc(dp_l, que[ta], i))
ta--;
que[++ta] = i;
}
for (int i = m + 1; i <= r; i++)
{
while (he < ta && calc(dp_l, que[ta - 1], que[ta]) <= i)
ta--;
int pos = que[ta];
tmp[i] = dp_l[pos] + (ll)(i - pos) * (i - pos + 1) / 2 + sum[i] - sum[pos] + dp_r[i + 1];
}
for (int i = r - 1; i > m; i--)
tmp[i] = std::max(tmp[i], tmp[i + 1]);
for (int i = r; i > m; i--)
dp[i] = std::max(dp[i], tmp[i]);
work(l, m, coef);
work(m + 1, r, coef);
}
int main()
{
// freopen("ARC066-F.in", "r", stdin);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld", arr + i);
sum[i] = sum[i - 1] - arr[i];
}
init(dp_l, n);
rev(n);
init(dp_r, n);
std::reverse(dp_r + 1, dp_r + n + 1);
rev(n);
memset(dp, -0x3f, sizeof(dp));
work(1, n, 0);
memcpy(ans, dp, sizeof(dp));
rev(n);
memset(dp, -0x3f, sizeof(dp));
for (int i = 1; i <= n; i++)
std::swap(dp_l[i], dp_r[n - i + 1]);
work(1, n, -1);
for (int i = 1; i <= n; i++)
ans[i] = std::max(ans[i], dp[n - i + 1]);
rev(n);
for (int i = 1; i <= n; i++)
std::swap(dp_l[i], dp_r[n - i + 1]);
int q;
scanf("%d", &q);
while (q--)
{
int x, y;
scanf("%d%d", &x, &y);
// printf("%lld %lld\n", dp_l[x - 1], dp_r[x + 1]);
printf("%lld\n", std::max(ans[x] + arr[x] - y, dp_l[x - 1] + dp_r[x + 1]));
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int> > VVI;
typedef priority_queue<int, vector<int>, greater<int> > PQ;
const int MAX = 1e5 + 10, MOD = 1e9 + 7;
int n;
void solve() {
cin >> n;
int res = 1e9;
for (int i = 1; i <= 63244; i++) {
int steps = (i - 1);
int rem = n - i;
steps += (rem / i);
if (rem % i) steps++;
res = min(res, steps);
}
cout << res << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
;
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
m = max(m, 1);
int res = min(n - m, m);
cout << res << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long tests;
cin >> tests;
while (tests--) {
long long n, k, l;
cin >> n >> k >> l;
vector<long long> dep(n + 1), safe = {0};
for (long long i = 1; i <= n; i++) {
cin >> dep[i];
if (dep[i] + k <= l) safe.push_back(i);
}
safe.push_back(n + 1);
bool ok = true;
for (long long i = 0; i < safe.size() - 1 and ok; i++) {
long long tide = k;
bool down = true;
for (long long j = safe[i] + 1; j < safe[i + 1]; j++) {
tide += down ? -1 : 1;
if (down) tide -= max(0LL, dep[j] + tide - l);
if (tide < 0 or dep[j] + tide > l) {
ok = false;
break;
}
if (!tide) down = false;
}
}
if (ok)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void c_a_j() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
c_a_j();
long long n;
cin >> n;
vector<long long> v(n);
for (long long i = 0; i < n; i++) cin >> v[i];
long long check = 1, count = 0, zero = 0;
for (long long i = 0; i < n; i++) {
if (v[i] > 0) {
count += v[i] - 1;
v[i] = 1;
check *= v[i];
} else if (v[i] < 0) {
count += abs(-1 - v[i]);
v[i] = -1;
check *= v[i];
} else
zero++;
}
if (check == 1 || zero)
cout << count + zero << endl;
else
cout << count + 2 << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> arr;
void mymain() {
cin >> n;
arr.resize(n);
for (long long i = 0; i < n; i++) cin >> arr[i];
sort(arr.begin(), arr.end());
long long taken = 0;
long long cnt = 0;
while (taken != n) {
long long sz = 0;
long long lst = -1;
for (long long i = 0; i < n; i++) {
if (arr[i] == -1) continue;
if (arr[i] >= sz) {
sz++;
taken++;
if (lst == -1) cnt++;
lst = arr[i];
arr[i] = -1;
}
}
}
cout << cnt << '\n';
}
signed main() {
long long t = 1;
for (long long tt = 0; tt < t; tt++) {
mymain();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int book[15][15];
struct sair {
string str[105];
int co;
int x, y;
};
int dir[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, 1, -1, -1, 1, -1, -1, 1};
string D[8] = {"R", "D", "L", "U", "RD", "LU", "LD", "RU"};
sair bfs(sair s) {
queue<sair> Q;
Q.push(s);
sair e;
while (!Q.empty()) {
s = Q.front();
Q.pop();
for (int i = 0; i < 8; i++) {
e = s;
e.x = s.x + dir[i][0];
e.y = s.y + dir[i][1];
if (e.x >= 0 && e.x < 8 && e.y >= 0 && e.y < 8 && book[e.x][e.y] != 1) {
e.str[e.co] = D[i];
e.co++;
Q.push(e);
if (book[e.x][e.y] == 2) {
return e;
}
book[e.x][e.y] = 1;
}
}
}
}
int main() {
string str, str1;
cin >> str;
sair s;
book[7 - (str[1] - '1')][str[0] - 'a'] = 1;
s.y = str[0] - 'a', s.x = 7 - (str[1] - '1'), s.co = 0;
cin >> str1;
book[7 - (str1[1] - '1')][str1[0] - 'a'] = 2;
if (str == str1) {
cout << 0 << endl;
return 0;
}
s = bfs(s);
cout << s.co << endl;
for (int i = 0; i < s.co; i++) {
cout << s.str[i] << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[1000000], n, dp[1000000], ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[a[i]] = i;
}
for (int i = n; i >= 1; i--)
if (dp[a[i]] == i) ans = i;
cout << a[ans] << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int arr[5005][5005] = {};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
;
int n, m, k;
cin >> n >> m >> k;
map<int, int> row, col, rn, cn;
for (int i = 1; i <= k; i++) {
int o, w, e;
cin >> o >> w >> e;
if (o == 1) {
row[w] = e;
rn[w] = i;
} else {
col[w] = e;
cn[w] = i;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (rn[i] > cn[j])
cout << row[i] << " ";
else
cout << col[j] << " ";
}
cout << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int s[500005];
int n, top;
long long ans;
int main() {
n = read();
for (int i = 1; i <= n; i++) {
int x = read();
while (top > 1 && s[top - 1] >= s[top] && x >= s[top]) {
ans += min(s[top - 1], x);
top--;
}
s[++top] = x;
}
sort(s + 1, s + top + 1);
for (int i = 1; i <= top - 2; i++) ans += s[i];
printf("%I64d", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double PI = 2.0 * acos(0.0);
const double EPS = 1e-5;
bitset<520000> dp;
int sign[520000];
int main() {
int n, d;
scanf("%d %d", &n, &d);
dp[0] = true;
for (int i = 0; i < n; i++) {
int c;
scanf("%d", &c);
for (int j = 520000 - c - 1; j >= 0; j--)
if (dp[j]) dp[j + c] = true;
}
for (int i = 0; i < 520000; i++)
if (dp[i])
sign[i] = i;
else
sign[i] = sign[i - 1];
int price = 0;
int day = 0;
while (price != sign[price + d]) {
price = sign[price + d];
day++;
}
printf("%d %d\n", price, day);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int flag = 0;
int cur = 0;
int ans = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] >= '0' && s[i] <= '9') {
cur = cur * 10 + (s[i] - '0');
} else {
if (!flag) {
ans += cur;
} else {
ans -= cur;
}
cur = s[i] - '0';
if (s[i] == '+') {
flag = 0;
} else {
flag = 1;
}
}
}
if (!flag)
ans += cur;
else
ans -= cur;
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int n, m, mod;
int a[20005];
int dp[2][55][105];
int main() {
scanf("%d%d%d", &n, &m, &mod);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] %= mod;
int cur = 0;
for (int k = 1; k <= m + 1; k++)
for (int val = 0; val < mod; val++) dp[0][k][val] = -inf;
dp[0][m + 1][0] = 0;
for (int x = n; x >= 1; x--) {
cur = !cur;
for (int k = 1; k <= m + 1; k++)
for (int val = 0; val < mod; val++) dp[cur][k][val] = -inf;
for (int k = 1; k <= m; k++) {
for (int val = 0; val < mod; val++) {
dp[cur][k][val] = max(dp[!cur][k + 1][a[x + 1]] + val,
dp[!cur][k][(val + a[x + 1]) % mod]);
}
}
}
printf("%d", dp[cur][1][a[1]]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 2e18 + 5;
int main() {
long long n, m, w, h, mx = 0;
cin >> n;
vector<int> a(n + 1);
for (int i = (1); i < (n + 1); i++) cin >> a[i];
cin >> m;
for (int i = (0); i < (m); i++) {
cin >> w >> h;
mx = mx > a[w] ? mx : a[w];
cout << mx << endl;
mx += h;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
const int N = 2e5 + 10;
int n, m, k, l, r, opt, ans, flag;
char c;
int f[1005];
char s[N], t[N];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
inline void Write(int re) {
if (re < 0) {
putchar('-');
re = -re;
}
if (re > 9) Write(re / 10);
putchar(re % 10 + '0');
}
int main() {
n = read();
m = read();
k = read();
scanf("%s", s);
for (int i = 1; i <= m; i++) {
opt = read();
if (opt == 1) {
l = read();
r = read();
scanf("%c", &c);
for (int j = l - 1; j <= r - 1; j++) s[j] = c;
if (l == 1 && r == n)
flag = 1;
else
flag = 0;
} else {
scanf("%s", t);
if (flag || k == 1) {
printf("%d\n", n);
continue;
}
for (int j = 0; j < k; j++) f[t[j]] = j;
ans = 1;
for (int j = 1; j < n; j++)
if (f[s[j]] <= f[s[j - 1]]) ans++;
Write(ans);
puts("");
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int e = 2e5 + 100;
const int p = 1000000007;
long long dp[3][e];
int h, r, g;
int main() {
cin >> r >> g;
if (r == 0 or g == 0) {
cout << 1;
return 0;
}
for (h = 1;; h++) {
if (h * (h + 1) > 2 * r + g * 2) {
break;
}
}
h--;
dp[1][1] = 1;
dp[1][0] = 1;
for (int i = 2; i <= h; i++) {
int w = i % 2;
int l = w ^ 1;
for (int j = 0; j <= g; j++) {
dp[w][j] = 0;
}
for (int j = 0; j <= g; j++) {
if (i * (i + 1) / 2 - j <= r) dp[w][j] += dp[l][j];
if (dp[w][j] > p) dp[w][j] -= p;
if (j - i >= 0) dp[w][j] += dp[l][j - i];
if (dp[w][j] > p) dp[w][j] -= p;
}
}
long long ans = 0;
for (int i = 0; i <= g; i++) {
ans += dp[h % 2][i];
ans %= p;
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3 * 100 * 1000;
const long long INF = 1LL * 1000 * 1000 * 1000 * 1000;
int finishLine, numberOfRamps;
set<pair<int, int> > rampPositions;
vector<pair<int, pair<long long, bool> > > adj[MAXN];
set<pair<long long, int> > chosenTree;
set<pair<int, int> > ramps[MAXN];
long long dis[MAXN];
pair<int, bool> parent[MAXN];
set<pair<int, int> > bridges[MAXN];
struct comp_pair_int {
bool operator()(const pair<int, int> &a, const int &b) {
return (a.first < b);
}
bool operator()(const int &a, const pair<int, int> &b) {
return (a < b.first);
}
};
void dijkstra() {
for (int i = 0; i < 2 * numberOfRamps + 2; ++i) {
dis[i] = INF;
chosenTree.insert(make_pair(INF, i));
}
dis[2 * numberOfRamps] = 0;
chosenTree.insert(make_pair(0, 2 * numberOfRamps));
while (chosenTree.size()) {
int source = (*chosenTree.begin()).second;
chosenTree.erase(chosenTree.begin());
for (int i = 0; i < adj[source].size(); ++i) {
int neighbor = adj[source][i].first;
int weight = adj[source][i].second.first;
bool isRamp = adj[source][i].second.second;
if (dis[neighbor] > dis[source] + weight) {
chosenTree.erase(make_pair(dis[neighbor], neighbor));
parent[neighbor].first = source;
parent[neighbor].second = isRamp;
dis[neighbor] = dis[source] + weight;
chosenTree.insert(make_pair(dis[neighbor], neighbor));
}
}
}
return;
}
int main() {
cin >> numberOfRamps >> finishLine;
for (int i = 0; i < numberOfRamps; ++i) {
int start, finish, travelTime, intersection;
cin >> start >> finish >> travelTime >> intersection;
finish += start;
start -= intersection;
if (start < 0) {
continue;
}
adj[2 * i].push_back(
make_pair(2 * i + 1, make_pair(travelTime + intersection, true)));
rampPositions.insert(make_pair(start, 2 * i));
rampPositions.insert(make_pair(finish, 2 * i + 1));
ramps[2 * i].insert(make_pair(2 * i + 1, i + 1));
}
rampPositions.insert(make_pair(0, 2 * numberOfRamps));
rampPositions.insert(make_pair(finishLine, 2 * numberOfRamps + 1));
{
set<pair<int, int> >::iterator test = rampPositions.begin();
while (test != rampPositions.end()) {
test++;
}
}
set<pair<int, int> >::iterator current = rampPositions.begin(),
prev = rampPositions.begin();
current++;
while (current != rampPositions.end()) {
int vertexOne = (*current).second, vertexTwo = (*prev).second,
distance = (*current).first - (*prev).first;
adj[vertexOne].push_back(make_pair(vertexTwo, make_pair(distance, false)));
adj[vertexTwo].push_back(make_pair(vertexOne, make_pair(distance, false)));
current++;
prev++;
}
dijkstra();
cout << dis[2 * numberOfRamps + 1] << endl;
vector<int> ans;
int root = 2 * numberOfRamps + 1, par = -1;
bool isRamp = false;
while (root != 2 * numberOfRamps) {
par = parent[root].first;
isRamp = parent[root].second;
if (isRamp) {
set<pair<int, int> >::iterator pos;
pos = lower_bound(ramps[par].begin(), ramps[par].end(), root,
comp_pair_int());
ans.push_back((*pos).second);
}
root = par;
}
cout << ans.size() << endl;
while (ans.size()) {
cout << ans[ans.size() - 1] << " ";
ans.pop_back();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, a, b, c, d;
cin >> n >> m >> a >> b >> c >> d;
if (abs(a - c) >= 5 || abs(b - d) >= 5 || abs(a - c) + abs(b - d) >= 7)
cout << "Second" << endl;
else
cout << "First" << endl;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,l;
cin>>n>>m>>l;
int a[100][100];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)cin>>a[i][j];
int b[100][100];
for(int i=0;i<m;i++)
for(int j=0;j<l;j++)cin>>b[i][j];
long long c[100][100]={};
for(int i=0;i<n;i++)
for(int j=0;j<l;j++)
for(int k=0;k<m;k++)c[i][j]+=a[i][k]*b[k][j];
for(int i=0;i<n;i++){
for(int j=0;j<l;j++){
if(j) cout<<" ";
cout<<c[i][j];
}
cout<<endl;
}
}
| 0 |
#include <stdio.h>
int b, r, g, c, s, t;
int main() {
while(~scanf("%d%d%d%d%d%d", &b, &r, &g, &c, &s, &t)&&t) {
printf("%d\n", b*95 + r*63 + g*7 + c*2 + s*3 - t*3 + 100);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void printv(vector<int> arr, int n) {
for (int i = 0; i < n; ++i) printf("%d ", arr[i]);
cout << endl;
}
void print(int arr[], int n) {
for (int i = 0; i < n; ++i) printf("%d ", arr[i]);
cout << endl;
}
bool isPrime(int n) {
bool flag = 1;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) {
flag = 0;
break;
}
}
return (flag ? 1 : 0);
}
const int naX = 1e5 + 5;
vector<int> g[naX];
int dist, orig_dist, node;
int n, a, b, da, db;
void dfs(int c, int par, int d) {
if (d > dist) {
dist = d;
node = c;
}
for (auto &it : g[c]) {
if (it == par) continue;
dfs(it, c, d + 1);
}
}
void dfs2(int c, int par, int d) {
if (c == b) orig_dist = d;
for (auto &it : g[c]) {
if (it == par) continue;
dfs2(it, c, d + 1);
}
}
void test_case() {
scanf("%d%d%d%d%d", &n, &a, &b, &da, &db);
for (int i = 1; i <= n; ++i) g[i].clear();
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dist = -1;
dfs(1, 1, 0);
dist = -1;
dfs(node, node, 0);
db = min(dist, db);
dfs2(a, a, 0);
if (db > 2 * da && orig_dist > da) {
printf("Bob\n");
} else {
printf("Alice\n");
}
}
int main() {
int t = 1;
scanf("%d", &t);
while (t--) {
test_case();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int i;
for (i = n - 1; i > 0; i--) {
if (arr[i] != arr[i - 1]) break;
}
if (k > i)
cout << i << endl;
else
cout << -1 << endl;
return 0;
}
| 1 |
//Love and Freedom.
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 20021225
#define mdn 998244353
#define N 410
using namespace std;
int read()
{
int s=0,t=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') t=-1; ch=getchar();}
while(ch>='0' && ch<='9') s=s*10+ch-'0',ch=getchar();
return s*t;
}
void upd(int &x,int y){x+=x+y>=mdn?y-mdn:y;}
int ksm(int bs,int mi)
{
int ans=1;
while(mi)
{
if(mi&1) ans=1ll*ans*bs%mdn;
bs=1ll*bs*bs%mdn; mi>>=1;
}
return ans;
}
int f[2][N][N],a[N],b[N],n,s,sa,sb;
int main()
{
n=read();
for(int i=1;i<=n;i++) a[i]=read(),b[i]=read(),sa+=a[i],sb+=b[i];
int cur=0,lst=1; f[cur][0][0]=mdn-1;
for(int i=1;i<=n;i++)
{
cur^=1,lst^=1; memcpy(f[cur],f[lst],sizeof(f[cur]));
for(int p=1,l=0;l<b[i];l++,p=1ll*p*a[i]%mdn*ksm(l,mdn-2)%mdn)
for(int j=a[i];j<=sa;j++) for(int k=l;k<=sb;k++)
upd(f[cur][j][k],mdn-1ll*p*f[lst][j-a[i]][k-l]%mdn);
}
int ans=0;
for(int i=1;i<=sa;i++)
{
int p=1,inv=ksm(i,mdn-2),q=1ll*sa*inv%mdn;
for(int k=0;k<=sb;k++,p=1ll*p*inv%mdn*k%mdn)
upd(ans,1ll*f[cur][i][k]*p%mdn*q%mdn);
}
printf("%d\n",ans);
return 0;
}
| 0 |
#include<cstdio>
#define max 10000
#define NIL -1
struct Node { int pt, L, R; };
Node t[max];
int n,d[max],h[max];
void setd(int u,int d2){
if(u == NIL) return;
d[u] = d2;
setd(t[u].L, d2+1);
setd(t[u].R, d2+1);
}
int seth(int u){
int h1 = 0, h2 = 0;
if(t[u].L != NIL)
h1 = seth(t[u].L) + 1;
if(t[u].R != NIL)
h2 = seth(t[u].R) + 1;
return h[u] = (h1 > h2 ? h1 : h2);
}
int gets(int u){
if(t[u].pt == NIL) return NIL;
if(t[t[u].pt].L != u && t[t[u].pt].L != NIL){
return t[t[u].pt].L;}
if(t[t[u].pt].R != u && t[t[u].pt].R != NIL){
return t[t[u].pt].R;}
return NIL;
}
void print(int u) {
printf("node %d: ", u);
printf("parent = %d, ",t[u].pt);
printf("sibling = %d, ",gets(u));
int dg = 0;
if(t[u].L != NIL) dg++;
if(t[u].R != NIL) dg++;
printf("degree = %d, ", dg);
printf("depth = %d, ", d[u]);
printf("height = %d, ", h[u]);
if(t[u].pt == NIL){
printf("root\n");
}else if(t[u].L == NIL && t[u].R == NIL){
printf("leaf\n");
}else {
printf("internal node\n");
}
}
int main(){
int v,l,r,rt=0;
scanf("%d",&n);
for(int i=0;i < n;i++) t[i].pt = NIL;
for(int i=0;i < n;i++){
scanf("%d %d %d",&v,&l,&r);
t[v].L = l;
t[v].R = r;
if(l != NIL) t[l].pt=v;
if(r != NIL) t[r].pt=v;
}
for(int i=0;i<n;i++) if(t[i].pt == NIL) rt = i;
setd(rt,0);
seth(rt);
for(int i = 0;i < n;i++) print(i);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1000000007
#define ALL(a) (a).begin(),(a).end()
int main() {
ll p,c,P[100010],C[100010];
cin>>p>>c;
vector<int>a[100010];
for(int i=0;i<c;i++){
int x,y;
cin>>x>>y;
x--;
P[i]=x;C[i]=y;
a[x].push_back(y);
}
for(int i=0;i<p;i++){
sort(a[i].begin(),a[i].end());
}
for(int i=0;i<c;i++){
printf("%06d",P[i]+1);
auto itr=lower_bound(ALL(a[P[i]]),C[i]);
printf("%06d\n",itr-a[P[i]].begin()+1);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const int M = 18;
const int K = 10;
int par[N][M];
vector<int> path[N][M];
int n, m, q;
vector<int> adj[N], ids[N];
int c[N];
int d[N];
vector<int> uni(vector<int> a, vector<int> b) {
vector<int> res;
int aa = 0, bb = 0;
while (a.size() > aa && b.size() > bb && res.size() < K) {
if (a[aa] < b[bb]) {
res.push_back(a[aa++]);
} else {
res.push_back(b[bb++]);
}
}
while (res.size() < K && aa < a.size()) {
res.push_back(a[aa++]);
}
while (res.size() < K && bb < b.size()) {
res.push_back(b[bb++]);
}
return res;
}
void dfs(int v, int p) {
if (p != -1) d[v] = d[p] + 1;
par[v][0] = p;
for (int i = 1; i < M; i++) {
if (par[v][i - 1] != -1) {
par[v][i] = par[par[v][i - 1]][i - 1];
}
}
if (p != -1) path[v][0] = ids[p];
for (int i = 1; i < M; i++) {
if (par[v][i] != -1) {
path[v][i] = uni(path[v][i - 1], path[par[v][i - 1]][i - 1]);
}
}
for (int u : adj[v])
if (u != p) {
dfs(u, v);
}
}
vector<int> lca(int v, int u) {
if (v == u) return ids[v];
if (d[v] < d[u]) swap(v, u);
vector<int> res = ids[v];
for (int i = M - 1; i >= 0; i--) {
if (par[v][i] != -1 && d[par[v][i]] >= d[u]) {
res = uni(res, path[v][i]);
v = par[v][i];
}
}
if (v == u) return res;
res = uni(res, ids[u]);
for (int i = M - 1; i >= 0; i--) {
if (par[v][i] != par[u][i]) {
res = uni(res, path[v][i]);
v = par[v][i];
res = uni(res, path[u][i]);
u = par[u][i];
}
}
return uni(res, ids[par[v][0]]);
;
}
vector<int> get(int v, int u) {
if (v == u) return ids[v];
vector<int> res;
for (int i = M - 1; i >= 0; i--) {
if (par[v][i] != -1 && d[par[v][i]] >= d[u]) {
res = uni(res, path[v][i]);
v = par[v][i];
}
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
scanf("%d %d %d", &n, &m, &q);
for (int i = 0; i < n - 1; i++) {
int a, b;
scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= m; i++) {
scanf("%d", c + i);
ids[c[i]].push_back(i);
}
memset(par, -1, sizeof par);
dfs(1, -1);
while (q--) {
int v, u, a;
scanf("%d %d %d", &v, &u, &a);
vector<int> res = lca(v, u);
printf("%d", min(a, int(res.size())));
for (int i = 0; i < min(a, int(res.size())); i++) {
printf(" %d", res[i]);
}
printf("\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
bool exist[10004][45];
int main() {
ios_base::sync_with_stdio(false);
int n, m;
scanf("%d%d", &m, &n);
for (int i = 0; i < 10004; i++)
for (int j = 0; j < 45; j++) exist[i][j] = true;
vector<set<int>> ve;
int cur = 0;
while (m--) {
cur++;
int t;
scanf("%d", &t);
vector<int> temp;
while (t--) {
int x;
scanf("%d", &x);
temp.push_back(x);
exist[x][cur] = false;
}
for (int i = 1; i < cur; i++) {
bool b = false;
for (auto x : temp) {
if (!exist[x][i]) {
b = true;
break;
}
}
if (!b) return cout << "impossible\n", 0;
}
}
cout << "possible\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int N = 1e5 + 51;
struct edge {
int to, next, w;
} e[N << 1];
int head[N], cnt;
void addedge(int x, int y, int z) {
e[++cnt] = (edge){y, x[head], z}, x[head] = cnt;
e[++cnt] = (edge){x, y[head], z}, y[head] = cnt;
}
int col[N], par[N], vis[N], pt, pv, ans, a2, mxdep;
std::vector<int> v;
void upd(int x) {
if (x > ans) ans = x, a2 = 0;
a2 += x == ans;
}
void dfs1(int x, int p, int d, int *arr) {
x[par] = p, x[arr] = d;
if (x[col] && d > pv) pv = d, pt = x;
for (int i = x[head], nx; i; i = e[i].next)
if ((nx = e[i].to) != p) dfs1(nx, x, d + e[i].w, arr);
}
int dfs2(int x, int p, int d) {
if (x[col]) mxdep = std::max(mxdep, d);
int c = x[col];
for (int i = x[head], nx; i; i = e[i].next)
if ((nx = e[i].to) != p && !nx[vis]) c += dfs2(nx, x, d + e[i].w);
if (!x[col]) upd(c);
return c;
}
int n, x, y, z, s, t, disL[N], disR[N], L[N], R[N], sz[N], dep[N], sL, sR;
std::vector<int> lnk;
int main() {
for (scanf("%d%d", &n, &x); x--;) scanf("%d", &y), y[col] = 1;
for (int i = 1; i < n; i++) scanf("%d%d%d", &x, &y, &z), addedge(x, y, z);
dfs1(1, 0, 0, disL), s = pt, pv = -1;
dfs1(s, 0, 0, disL), t = pt, pv = -1;
dfs1(t, 0, 0, disR);
for (int i = s; i; i = i[par]) lnk.push_back(i), i[vis] = 1;
for (int i : lnk) mxdep = -1, i[sz] = dfs2(i, 0, 0), x[dep] = mxdep;
for (int i : lnk) {
if (i[disL] < i[disR]) i[R] += i[sz], sR += i[sz];
}
std::reverse(lnk.begin(), lnk.end());
for (int i : lnk) {
if (i[disL] > i[disR]) i[L] += i[sz];
}
for (int i : lnk) {
sR -= i[R];
if (!i[col]) upd(sL + sR + i[sz]);
sL += i[L];
}
printf("%d %d", ans, a2);
}
| 5 |
#include<iostream>
using namespace std;
int main()
{
int t,n;
int s,f;
int stu;
cin>>t;
while(t!=0){
cin>>n;
stu=0;
for(int i=0;i<n;i++){
cin>>s>>f;
stu+=(f-s);
}
if(t<=stu)
cout<<"OK\n";
else
cout<<t-stu<<'\n';
cin>>t;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int nb, ns, nc, pb, ps, pc;
int mb, ms, mc;
long long r;
bool gen(long long& k) {
long long c = max(0ll, k * mb - nb) * pb + max(0ll, k * ms - ns) * ps +
max(0ll, k * mc - nc) * pc;
return c <= r;
}
int main(int argc, char* argv[]) {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
cin >> nb >> ns >> nc;
cin >> pb >> ps >> pc;
cin >> r;
mb = 0;
ms = 0;
mc = 0;
for (auto& v : s) {
if (v == 'B')
++mb;
else if (v == 'S')
++ms;
else if (v == 'C')
++mc;
}
long long iL = 0, iR = 1e13;
while (iL < iR) {
long long iM = (iL + iR + 1) / 2;
if (gen(iM))
iL = iM;
else
iR = iM - 1;
}
cout << iL << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int s, v1, v2, t1, t2;
cin >> s >> v1 >> v2 >> t1 >> t2;
int time1 = t1 + s * v1 + t1;
int time2 = t2 + s * v2 + t2;
if (time1 > time2) {
cout << "Second";
} else if (time1 < time2) {
cout << "First";
} else {
cout << "Friendship";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:836777216")
using namespace std;
string s1, s2;
int a[33][33];
const int inf = 1011111111;
int main() {
getline(cin, s1);
getline(cin, s2);
if (s1.size() != s2.size()) {
puts("-1");
return 0;
}
int m;
cin >> m;
for (int i(0); i < (26); i++)
for (int j(0); j < (26); j++) a[i][j] = (i == j ? 0 : inf);
for (int i(0); i < (m); i++) {
char c1, c2;
int x;
scanf(" %c %c %d", &c1, &c2, &x);
a[c1 - 'a'][c2 - 'a'] = min(x, a[c1 - 'a'][c2 - 'a']);
}
for (int k(0); k < (26); k++)
for (int i(0); i < (26); i++)
for (int j(0); j < (26); j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
int res = 0;
for (int i(0); i < (s1.size()); i++) {
int x = inf;
for (int j(0); j < (26); j++)
x = min(x, a[s1[i] - 'a'][j] + a[s2[i] - 'a'][j]);
if (x == inf) {
puts("-1");
return 0;
} else
res += x;
for (int j(0); j < (26); j++)
if (x == a[s1[i] - 'a'][j] + a[s2[i] - 'a'][j]) {
s1[i] = char('a' + j);
break;
}
}
cout << res << endl;
printf("%s\n", s1.c_str());
return 0;
}
| 2 |
#include <bits/stdc++.h>
int Fl, Pn, SI = 100;
char mch = ' ', ch, Bf[21];
template <typename t>
void in(t& a) {
a = 0;
ch = getchar();
Fl = 1;
while ((ch < '0') || (ch > '9')) Fl = (ch == '-') ? -Fl : Fl, ch = getchar();
while ((ch >= '0') && (ch <= '9')) a = a * 10 + ch - '0', ch = getchar();
a *= Fl;
}
template <typename t>
void out(t a) {
if (a < 0) putchar('-'), a = -a;
if (a == 0) putchar('0');
while (a) Bf[++Pn] = a % 10 + '0', a /= 10;
while (Pn) putchar(Bf[Pn]), --Pn;
putchar(mch);
}
template <typename t, typename... ARGS>
void in(t& a, ARGS&... args) {
in(a);
in(args...);
}
template <typename t, typename... ARGS>
void out(t a, ARGS... args) {
out(a);
out(args...);
}
using namespace std;
struct node {
int num, ind;
bool operator<(const node& b) const {
if (num != b.num) return num < b.num;
return ind < b.ind;
}
};
priority_queue<node> q;
int n, f[100001], a[100001];
int main() {
in(n);
for (register int i = 1; i <= n; i++) in(a[i]);
for (register int i = 1; i <= n; i++) q.push({a[i], i}), f[i] = -2;
for (register int i = n; i >= 1; i--) {
if (f[i] != -2) continue;
f[i] = -1;
while (!q.empty() && q.top().num > a[i]) {
if (f[q.top().ind] == -2) f[q.top().ind] = i - q.top().ind - 1;
q.pop();
}
}
for (register int i = 1; i <= n; i++) out(f[i]);
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000000000000000
#define io ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define endl '\n'
#define pii pair<ll, ll>
#define piii pair<ll, pair<ll, ll>>
#define mod 1000000007
ll n;
ll m;
void solve() {
cin>>n;
if(n == 1) {
cout<<1<<endl;
return;
}
if(n == 2) {
cout<<-1<<endl;
return;
}
vector<vector<int>> ans(n, vector<int> (n, -1));
int num = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if((i+j)%2 == 0) {
ans[i][j] = num++;
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if((i+j)%2 != 0) {
ans[i][j] = num++;
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout<<ans[i][j]<<' ';
}
cout<<endl;
}
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
io
ll t = 1;
cin>>t;
ll c = 1;
while (t--)
{
solve();
c++;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
set<string> vis;
set<string> sol;
int n, q;
const int N = 36;
string a[N];
char b[N];
void dfs(const string &s) {
if ((int)s.length() == n) {
sol.insert(s);
return;
}
if (vis.count(s)) {
return;
}
vis.insert(s);
for (int j = (int)(0); j < (int)(q); j++) {
if (b[j] != s[0]) {
continue;
}
string tmp = a[j] + s.substr(1);
dfs(tmp);
}
}
int main(void) {
cin >> n >> q;
for (int i = (int)(0); i < (int)(q); i++) {
cin >> a[i] >> b[i];
}
dfs("a");
cout << sol.size() << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1005;
int c[MAX_N];
int rv[MAX_N];
int main() {
int n, m, d, suml;
cin >> n >> m >> d;
for (int i = 1; i <= m; i++) cin >> c[i];
for (int i = m, pos = n; i >= 1; i--) {
for (int j = 0; j < c[i]; j++) {
rv[pos - j] = i;
}
pos -= c[i];
}
int pos = 0, flag = false;
for (int i = 1; i <= m; i++) {
pos += d;
if (pos > n || rv[pos]) {
flag = true;
break;
}
int plat = pos + 1;
while (!rv[plat]) plat++;
int len = c[rv[plat]];
for (int j = 0; j < len; j++) {
swap(rv[pos + j], rv[plat + j]);
}
pos += len - 1;
}
if (pos + d > n || flag) {
cout << "YES\n";
cout << rv[1];
for (int i = 2; i <= n; i++) cout << ' ' << rv[i];
} else
cout << "NO";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define MAX_N (1<<19)+1
class BIT {
public:
int limit;
int last;
map<int, int> m, mrev;
vector<int> t;
BIT(int l): limit(l), last(0), t(MAX_N, 0) {}
void incubate(int rid) {
if (t[MAX_N-1] == limit) {
add(find(1), -1);
}
last ++;
m[last] = rid;
mrev[rid] = last;
_add(last, 1);
}
void add(int rid, int x) {
_add(mrev[rid], x);
}
void _add(int id, int x) {
for (int i=id; i<MAX_N; i+=(i&-i)) {
t[i] += x;
}
}
int sum(int num) {
int res = 0;
for (int i=num; i>0; i-=(i&-i)) {
res += t[i];
}
return res;
}
int find(int num) {
if (sum(1) == num) {
return m[1];
}
int l = 1, r = MAX_N;
while (abs(r-l) > 1) {
int mi = (l+r) / 2;
if (sum(mi) < num) {
l = mi;
} else {
r = mi;
}
}
return m[l+1];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q, lim;
while (cin >> q >> lim, (q || lim)) {
int query, x;
BIT bit(lim);
for (int i=0; i<q; ++i) {
cin >> query >> x;
if (query == 0) {
bit.incubate(x);
} else if (query == 1) {
bit.add(bit.find(x), -1);
} else if (query == 2) {
cout << bit.find(x) << endl;
} else if (query == 3) {
bit.add(x, -1);
}
}
cout << "end" << endl;
}
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline long long read() {
long long x = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c <= '9' && c >= '0') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return w == 1 ? x : -x;
}
const int N = 2e3 + 10;
int a[N], n;
int main() {
int _ = read();
while (_--) {
int n = read();
if (n == 1) {
puts("FastestFinger");
continue;
}
if (n == 2) {
puts("Ashishgup");
continue;
}
if (n % 2) {
puts("Ashishgup");
continue;
}
int num1 = 0, num2 = 0;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
int res = 0;
while (n % i == 0) n = n / i, res++;
if (i == 2) num1 = res;
if (i % 2) num2 += res;
}
}
if (n == 2) num1++;
if (n != 1 && n % 2) num2++;
if (num1 == 1) {
if (num2 > 1)
puts("Ashishgup");
else
puts("FastestFinger");
} else {
if (num2 > 0)
puts("Ashishgup");
else
puts("FastestFinger");
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 5;
const long long max_val = 2e5 + 10;
long long mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s;
cin >> s;
long long co = 0;
for (long long i = n - 1; i >= 0; i--) {
co += (s[i] - '0');
}
for (long long i = 0; i < n; i++) {
if (s[i] != '0') co++;
}
if (s[n - 1] != '0') co--;
cout << co << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& cout, vector<T> V) {
cout << "[ ";
for (auto v : V) cout << v << ' ';
return cout << ']';
}
template <class L, class R>
ostream& operator<<(ostream& cout, pair<L, R> P) {
return cout << '(' << P.first << ',' << P.second << ')';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string a;
cin >> a;
reverse(a.begin(), a.end());
long long int n = a.size();
vector<long long int> b;
for (long long int i = 0; i < (n); i++)
if (a[i] == 'M') b.push_back(i);
if (!b.size()) {
cout << 0 << '\n';
return 0;
}
long long int pt = b[0], t = b[0];
long long int g = b[0];
for (long long int i = 1; i < b.size(); i++) {
g += b[i] - b[i - 1] - 1;
if (pt > b[i] - b[i - 1] - 1) {
t = max(g, pt + 1);
} else {
t = g;
}
pt = t;
}
cout << t << '\n';
cerr << "Time : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC
<< "ms\n";
;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, m, nn, q;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
bool cat[30][30];
struct Matrix {
int a[22][22]{};
} ans, base;
int main() {
auto add = [&](int &x, int y) {
x += y;
if (x >= mod) x -= mod;
};
auto mul = [&](Matrix a, Matrix b) {
Matrix c;
for (int i = 0; i < nn; i++) {
for (int j = 0; j < nn; j++) {
for (int k = 0; k < nn; k++) {
add(c.a[i][j], a.a[i][k] * 1ll * b.a[k][j] % mod);
}
}
}
return c;
};
auto qpow = [&](int b) {
for (; b; b >>= 1, base = mul(base, base))
if (b & 1) ans = mul(base, ans);
};
auto build = [&] {
memset(base.a, 0, sizeof base.a);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int id = i * m + j;
base.a[id][id] = 1;
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
int id1 = nx * m + ny;
base.a[id][id1] = cat[i][j] == 0 && cat[nx][ny] == 0;
}
}
}
}
};
scanf("%d %d %d", &n, &m, &q);
ans.a[0][0] = 1;
nn = n * m;
int nowt = 1;
while (q--) {
build();
int op, x, y, t;
scanf("%d %d %d %d", &op, &x, &y, &t);
x--;
y--;
qpow(t - nowt);
nowt = t;
if (op == 1)
printf("%d\n", ans.a[x * m + y][0]);
else if (op == 2)
cat[x][y] = true, ans.a[x * m + y][0] = 0;
else
cat[x][y] = false, ans.a[x * m + y][0] = 0;
}
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.