solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int M = 100 + 5;
char board[M][M];
int main() {
int n, m;
cin >> n >> m;
for (int r = 0; r < n; r++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
board[r][j] = s[j];
}
}
vector<char> ans;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c = board[i][j];
bool ok = true;
for (int col = 0; col < m; col++) {
if (col != j) {
if (board[i][col] == c) {
ok = false;
}
}
}
for (int row = 0; row < n; row++) {
if (row != i) {
if (board[row][j] == c) {
ok = false;
}
}
}
if (ok) {
ans.push_back(c);
}
}
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i];
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long arr[505][505], infy = 1000000000000000LL, ans, cnt[505][505] = {0};
int brr[505][505] = {0}, crr[505][505];
int main() {
int n, m, i, j, k, a, b, c;
cin >> n >> m;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
arr[i][j] = infy;
crr[i][j] = infy;
}
}
for (i = 1; i <= m; i++) {
cin >> a >> b >> c;
arr[a][b] = c;
arr[b][a] = c;
crr[a][b] = c;
crr[b][a] = c;
}
for (i = 1; i <= n; i++) {
arr[i][i] = 0;
}
int l, x;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
for (k = 1; k <= n; k++) {
if ((arr[j][i] + arr[i][k]) < arr[j][k])
arr[j][k] = arr[j][i] + arr[i][k];
}
}
}
for (l = 1 + n; l <= n; l++) {
cout << "\n";
for (x = 1; x <= n; x++) {
cout << arr[l][x] << " ";
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
for (k = 1; k <= n; k++) {
if ((arr[j][k] == arr[j][i] + crr[i][k]) && (arr[j][k] != infy))
cnt[j][k]++;
}
}
}
for (l = 1; l <= n; l++) {
for (x = l + 1; x <= n; x++) {
ans = 0;
for (i = 1; i <= n; i++) {
if ((arr[l][x] == arr[l][i] + arr[i][x]) && (arr[l][x] != infy))
ans = ans + cnt[l][i];
}
cout << ans << " ";
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, num[200005];
int one, two;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (register int i = 1; i <= n; i++) {
cin >> num[i];
if (num[i] == 1)
one++;
else
two++;
}
if (two > one)
cout << one;
else if (two < one)
cout << two + (one - two) / 3;
else
cout << two;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> A[55];
double pos[55][55];
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first > b.first)
return 1;
else if (a.first < b.first)
return 0;
else {
return a.second <= b.second;
}
}
bool vis[55][55];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> A[i].first;
for (int i = 1; i <= n; ++i) cin >> A[i].second;
sort(A + 1, A + n + 1, comp);
long long low = 0;
long long high = 10000000000000;
int count = 10000;
while (--count) {
long long mid = low + high;
mid /= 2.0;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
vis[i][j] = 0;
}
}
pos[0][0] = 0;
vis[0][0] = 1;
for (int i = 1; i <= n; ++i) {
long long st = A[i].first;
int u = i - 1;
while (i <= n && A[i].first == st) ++i;
--i;
int coun = i - u;
long long sum[55];
for (int j = u + 1; j <= i; ++j) sum[j - u] = 0;
sum[0] = 0;
for (int j = u + 1; j <= i; ++j)
sum[j - u] = sum[j - u - 1] + A[j].second;
long long sum2[55];
for (int j = u + 1; j <= i; ++j) sum2[j - u] = 0;
sum2[0] = 0;
for (int j = u + 1; j <= i; ++j)
sum2[j - u] = sum2[j - u - 1] + A[j].first;
for (int j = 0; j <= u; ++j) {
for (int l = 0; l <= min(coun, j); ++l) {
if (vis[u][j] == 0) continue;
int rem = j - l + coun - l;
if (vis[i][rem] == 0) {
vis[i][rem] = 1;
pos[i][rem] = pos[u][j] - (sum[coun] - sum[l]) * mid / 1000.0 +
(sum2[coun] - sum2[l]);
} else
pos[i][rem] = min(pos[i][rem],
pos[u][j] - (sum[coun] - sum[l]) * mid / 1000.0 +
(sum2[coun] - sum2[l]));
}
}
}
bool flag = 0;
for (int j = 0; j <= n; ++j) {
if (pos[n][j] <= 0 && vis[n][j]) {
flag = 1;
}
}
if (flag) {
high = mid;
} else {
low = mid + 1;
}
}
cout << high << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int Inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
int n;
long long a[1 << 17];
int main() {
scanf("%d", &n);
for (int i = 1; i <= (n); i++) scanf("%lld", a + i);
if (n == 1) {
puts("1 1\n0");
puts("1 1\n0");
printf("1 1\n%d\n", -a[1]);
return 0;
}
puts("1 1");
printf("%lld\n", -a[1]);
a[1] = 0;
printf("2 %d\n", n);
int rl = n - 1;
for (int i = (2); i <= (n); i++) {
long long rem = a[i] % n;
printf("%lld%c", rem * rl, i == n ? '\n' : ' ');
a[i] += rem * rl;
}
printf("1 %d\n", n);
for (int i = 1; i <= (n); i++) {
printf("%lld%c", -a[i], i == n ? '\n' : ' ');
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a1, a2, a3, a4, count = 0;
cin >> a1 >> a2 >> a3 >> a4;
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '1')
count += a1;
else if (s[i] == '2')
count += a2;
else if (s[i] == '3')
count += a3;
else if (s[i] == '4')
count += a4;
}
cout << count;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 112345;
int n, k, p, dep[maxn], cnt[maxn];
vector<int> G[maxn];
void dfs(int u, int par) {
dep[u] = dep[par] + 1;
cnt[dep[u]]++;
for (auto v : G[u])
if (v != par) dfs(v, u);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k >> p;
for (int(i) = (0); (i) <= ((n - 1) - 1); (i)++) {
int u, v;
cin >> u >> v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
dfs(1, 0);
int ans = min(k, cnt[2]), have = ans, cost = 0, j = 2;
for (int(i) = (3); (i) <= (n); (i)++) {
cost += have;
have += cnt[i];
while (have > k) {
if (k <= have - cnt[j]) {
have -= cnt[j];
cost -= (i - j) * cnt[j];
j++;
continue;
}
cnt[j] -= have - k;
cost -= (i - j) * (have - k);
have = k;
}
while (cost > p) {
if (p <= cost - (i - j) * cnt[j]) {
have -= cnt[j];
cost -= (i - j) * cnt[j];
j++;
continue;
}
int extra = (cost - p + i - j - 1) / (i - j);
have -= extra;
cost -= (i - j) * extra;
cnt[j] -= extra;
}
ans = max(ans, have);
}
cout << ans;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long int expo_fast(long long int a, long long int b) {
a = a;
long long int result = 1;
while (b) {
if (b & 1) result = (result * a);
b >>= 1;
a = (a * a);
}
return (result);
}
void take_in(vector<long long int>* arr) {
for (int i = 0; i < arr->size(); i++) cin >> (*(arr))[i];
}
void disp_arr(vector<long long int>* arr) {
for (int i = 0; i < arr->size(); i++) cout << (*(arr))[i] << " ";
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool cmp(const pair<pair<int, int>, long long int> a,
const pair<pair<int, int>, long long int> b) {
if (a.first.first < b.first.first)
return true;
else if (a.first.first == b.first.first) {
return a.first.second < b.first.second;
} else
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<pair<char, string>> inp(n);
for (int i = 0; i < n; i++) {
char c;
cin >> c;
string s;
cin >> s;
inp[i] = make_pair(c, s);
}
vector<int> color(26);
int count_susp = 0, shit_count = 0;
bool done = false;
int ans = 0;
char selected = NULL;
bool shock = false;
int k = 0;
for (k = 0; k < n; k++) {
pair<char, string> x = inp[k];
if (!done) {
if (x.first == '.' || (k != n - 1 && x.first == '?')) {
for (int i = 0; i < x.second.length(); i++) {
if (color[x.second[i] - 'a'] == 0) {
color[x.second[i] - 'a'] = 2;
shit_count++;
}
}
if (shit_count >= 25) {
done = true;
for (int i = 0; i < 26; i++) {
if (color[i] == 0) {
selected = (char)('a' + i);
break;
}
}
}
} else if (x.first == '!') {
shock = true;
for (int i = 0; i < x.second.length(); i++) {
if (color[x.second[i] - 'a'] == 0) {
color[x.second[i] - 'a'] = 1;
count_susp++;
}
}
break;
}
} else {
if (x.first == '!')
ans++;
else if (x.first == '?' && x.second[0] != selected)
ans++;
}
}
if (!done) {
for (int kk = k; kk < n; kk++) {
pair<char, string> x = inp[kk];
if (!done) {
if (x.first == '!') {
for (int i = 0; i < 26; i++) {
if (color[i] == 1) {
bool found = false;
for (int j = 0; j < x.second.length(); j++) {
if (x.second[j] == (char)('a' + i)) {
found = true;
break;
}
}
if (!found) {
color[i] = 2;
count_susp--;
}
}
}
if (count_susp == 1) {
done = true;
for (int i = 0; i < 26; i++) {
if (color[i] == 1) {
selected = (char)('a' + i);
break;
}
}
}
} else if (x.first == '.' || (x.first == '?' && k != n - 1)) {
for (int i = 0; i < x.second.length(); i++) {
if (color[x.second[i] - 'a'] == 1) {
color[x.second[i] - 'a'] = 2;
count_susp--;
} else
color[x.second[i] - 'a'] = 2;
}
if (count_susp == 1) {
done = true;
for (int i = 0; i < 26; i++) {
if (color[i] == 1) {
selected = (char)('a' + i);
break;
}
}
}
}
} else {
if (x.first == '!')
ans++;
else if (x.first == '?' && x.second[0] != selected)
ans++;
}
}
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
const int maxn = 2e3 + 5;
const int mo = 1e9 + 7;
using namespace std;
int rd() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x * f;
}
void add(int &x, int y) {
x += y;
if (x >= mo) x -= mo;
}
int n, m, fa[maxn], sz[maxn], cnt, b[maxn];
int f[maxn][maxn << 2][4];
int lst[maxn], top;
int find(int x) { return (fa[x] == x) ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
int fx = find(x), fy = find(y);
if (fx == fy) return;
fa[fx] = fy;
sz[fy] += sz[fx];
}
int main() {
n = rd();
m = rd();
for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1;
for (int u, v, i = 1; i <= m; i++) {
u = rd();
v = rd();
if (u != 1 && v != 1) merge(u, v);
}
for (int i = 2; i <= n; i++)
if (find(i) == i) lst[++top] = sz[i] + 1;
int mx = 0;
f[0][m][0] = f[0][m][2] = 1;
for (int i = 1; i <= top; i++) {
for (int j = -mx; j <= mx; j++) {
add(f[i][j + m + lst[i]][0], f[i - 1][j + m][0]);
add(f[i][j + m - lst[i]][0], f[i - 1][j + m][0]);
add(f[i][j + m][0], f[i - 1][j + m][0]);
add(f[i][j + m + lst[i]][1], f[i - 1][j + m][1]);
add(f[i][j + m - lst[i]][1], f[i - 1][j + m][1]);
add(f[i][j + m][1], f[i - 1][j + m][1]);
for (int k = 1; k < lst[i]; k++)
add(f[i][j + k - (lst[i] - k) + m][1], 2LL * f[i - 1][j + m][0] % mo);
for (int k = 1; k < lst[i] - 1; k++)
add(f[i][j + k - (lst[i] - k - 1) + m][1],
2LL * f[i - 1][j + m][0] % mo);
add(f[i][j + lst[i] + m][2], f[i - 1][j + m][2]);
add(f[i][j - lst[i] + m][2], f[i - 1][j + m][2]);
add(f[i][j + lst[i] + m][3], f[i - 1][j + m][3]);
add(f[i][j - lst[i] + m][3], f[i - 1][j + m][3]);
add(f[i][j + lst[i] - 1 + m][3], 2LL * f[i - 1][j + m][2] % mo);
add(f[i][j - lst[i] + 1 + m][3], 2LL * f[i - 1][j + m][2] % mo);
}
mx += lst[i];
}
int ans = ((f[top][m][1] + f[top][m][2]) % mo + f[top][m][3]) % mo;
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int a[100010];
int main() {
int n, k, i;
while (scanf("%d%d", &n, &k) == 2) {
k -= 1;
if (k % 2 == 0) {
for (i = 1; i <= k + 1; i += 2) a[i] = (i + 1) / 2;
for (i = 2; i <= k; i += 2) a[i] = n - a[i - 1] + 1;
for (i = k + 2; i <= n; i++) a[i] = a[i - 1] + 1;
} else {
for (i = 1; i <= k; i += 2) a[i] = (i + 1) / 2;
for (i = 2; i <= k + 1; i += 2) a[i] = n - a[i - 1] + 1;
for (i = k + 2; i <= n; i++) a[i] = a[i - 1] - 1;
}
for (i = 1; i < n; i++) printf("%d ", a[i]);
printf("%d\n", a[n]);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string t, s, tmp;
cin >> t >> s;
for (int i = 0; i < 2000; i++) tmp.push_back('#');
t = tmp + t + tmp;
int mn = (1 << 30);
for (int i = 0; i < t.size(); i++) {
if (i + s.size() - 1 >= t.size()) break;
int sum = 0;
for (int j = 0; j < s.size(); j++) sum += (s[j] != t[i + j]);
mn = min(mn, sum);
}
cout << mn << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
float p[n], sum = 0;
for (int i = 0; i < n; i++) {
cin >> p[i];
sum += p[i];
}
cout << float(sum / (n)) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int b[100];
int main() {
int n, m, r = 0, b = 0, g = 0;
cin >> n >> m;
string s[n];
bool x = 0, k = 0;
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; j < m - 1; j++) {
if (s[i][j] != s[i][j + 1]) {
x = 1;
}
if (i > 0) {
if (s[i][j] != s[i - 1][j]) {
k = 1;
}
}
}
if (i > 0) {
if (s[i][m - 1] != s[i - 1][m - 1]) {
k = 1;
}
}
}
if (r != 10 && (m >= 3 || n >= 3) && (n % 3 == 0 || m % 3 == 0)) {
int c = 0;
if (x == 0) {
for (int i = 0; i < n - 1; i++) {
if (s[i][0] == s[i + 1][0]) {
if (c == 0) {
r++;
} else if (c == 1) {
b++;
} else if (c == 2) {
g++;
}
} else {
c++;
if (c == 3) {
cout << "NO";
break;
}
}
}
if (r == b && b == g && c != 3 && n >= 3 && s[n / 2][0] != s[0][0] &&
s[n - 1][0] != s[0][0] && s[n - 1][0] != s[n / 2][0]) {
cout << "YES";
} else if (c != 3)
cout << "NO";
} else {
if (k == 1) {
cout << "NO";
} else {
r = 0;
b = 0;
g = 0;
if (m == 3) {
if (s[0][0] != s[0][1] && s[0][0] != s[0][2] && s[0][1] != s[0][2]) {
cout << "YES";
} else {
cout << "NO";
}
} else {
for (int i = 0; i < m - 1; i++) {
if (i < (m / 3) - 1) {
if (s[0][i] == s[0][i + 1]) {
r++;
} else
r = 1e4;
} else if (i < ((m / 3) * 2) - 1) {
if (s[0][i] == s[0][i + 1] && s[0][i] != s[0][0]) {
b++;
}
} else {
if (s[0][i] == s[0][i + 1] && s[0][i] != s[0][0] &&
s[0][i] != s[0][m / 3]) {
g++;
}
}
}
if (r == b && g == b) {
cout << "YES";
} else {
cout << "NO";
}
}
}
}
} else
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void file() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
void run() {
string s, falseLine;
getline(cin, s);
int a;
cin >> a;
int len = s.length();
char x[len];
set<char> all;
for (int i = 0; i < len; i++) {
x[i] = s[i];
all.insert(x[i]);
}
sort(x, x + len);
int sizee = all.size();
if (a > len) {
cout << "impossible\n";
} else if (sizee >= a) {
cout << "0\n";
} else {
sizee = a - sizee;
cout << sizee << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
run();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
if (k > (n - 1) / 2) return cout << -1 << '\n', 0;
cout << n * k << endl;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j <= i + k; ++j)
cout << i + 1 << ' ' << (j >= n ? j - n : j) + 1 << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 42;
int mx[4 * maxn][2];
int get(int a, int b, int z, int v = 1, int l = 0, int r = maxn) {
if (a <= l && r <= b) {
return mx[v][z];
} else if (r <= a || b <= l) {
return 0;
} else {
int m = (l + r) / 2;
return max(get(a, b, z, 2 * v, l, m), get(a, b, z, 2 * v + 1, m, r));
}
}
void upd(int p, int c, int z, int v = 1, int l = 0, int r = maxn) {
mx[v][z] = max(mx[v][z], c);
if (r - l == 1) {
return;
}
int m = (l + r) / 2;
if (p < m) {
upd(p, c, z, 2 * v, l, m);
} else {
upd(p, c, z, 2 * v + 1, m, r);
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int l[n], r[n];
int op[m], cl[m];
memset(op, 0, sizeof(op));
memset(cl, 0, sizeof(cl));
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
l[i]--, r[i]--;
op[l[i]]++;
cl[r[i]]--;
}
int a[m];
int cur = 0;
for (int i = 0; i < m; i++) {
cur += op[i];
a[i] = cur;
cur += cl[i];
int A = get(0, a[i] + 1, 0);
int B = max(get(a[i], maxn, 1), A);
upd(a[i], A + 1, 0);
upd(a[i], B + 1, 1);
}
cout << get(0, maxn, 1) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
if (n < 4) {
cout << -1 << endl;
return;
}
for (int i = n; i > 0; --i) {
if (i % 2 == 1) {
cout << i << ' ';
}
}
cout << 4 << ' ' << 2 << ' ';
for (int i = 6; i <= n; i += 2) {
cout << i << ' ';
}
cout << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void kdimo() {
int xa, ya, xb, yb, xf, yf;
cin >> xa >> ya >> xb >> yb >> xf >> yf;
if ((xa == xb && xa == xf)) {
if (ya > yb) swap(ya, yb);
if (ya <= yf && yf <= yb) {
cout << abs(xa - xb) + abs(ya - yb) + 2 << '\n';
return;
}
}
if ((ya == yb && ya == yf)) {
if (xa > xb) swap(xa, xb);
if (xa <= xf && xf <= xb) {
cout << abs(xa - xb) + abs(ya - yb) + 2 << '\n';
return;
}
}
cout << abs(xa - xb) + abs(ya - yb) << '\n';
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifdef Kdimo
freopen("input.inp", "r", stdin);
#endif // Kdimo
int t; cin >> t;
while (t--) {
kdimo();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
const int C = 403;
const int inf = 1e9 + 4;
int z, n, k, l;
double dp[N][N][C];
double p[N];
double a[N];
double f(int i, int lft, int cap) {
lft = max(lft, 0);
lft = min(lft, n + 1);
cap = min(cap, n);
if (i >= n) {
return (lft <= 0 && cap >= 0);
}
if (dp[i][lft][cap + n] != -1) {
return dp[i][lft][cap + n];
}
return dp[i][lft][cap + n] = p[i] * f(i + 1, lft - 1, cap + a[i]) +
(1 - p[i]) * f(i + 1, lft, cap);
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> l >> k;
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i] /= 100;
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++)
for (int j = 0; j <= n + 1; j++)
for (int k = 0; k <= 2 * n; k++) dp[i][j][k] = -1;
cout.setf(ios::fixed, ios::floatfield);
cout << setprecision(10) << f(0, l, k) << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn], pref[maxn];
char c[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
reverse(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
reverse(c + 1, c + n + 1);
for (int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + a[i];
}
int sum = 0, ans = 0;
for (int i = 1; i <= n; i++) {
if (c[i] == '1') {
ans = max(ans, sum + pref[n] - pref[i]);
sum += a[i];
}
}
ans = max(ans, sum);
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000 + 10;
string a;
int n;
inline void init(void) {
cin >> a;
n = a.size();
}
bool is_palindrome(string s, int l, int r) {
for (; l <= r; ++l, --r)
if (s[l] != s[r]) return false;
return true;
}
bool same(string s, int l, int r) {
for (register int i = 0, j = l; i < n; ++i, ++j)
if (s[j] != a[i]) return false;
return true;
}
bool check(int pos, string s) {
for (register int i = 0; i <= pos; ++i) s.push_back(s[i]);
return is_palindrome(s, pos + 1, n + pos) && (!same(s, pos + 1, n + pos));
}
bool always(int l, int r) {
for (register int i = l; i <= r; ++i)
if (a[l] != a[i]) return false;
return true;
}
inline bool False(void) {
if ((n % 2) == 1 && always(0, n / 2 - 1)) return true;
if (always(0, n - 1)) return true;
return false;
}
inline void work(void) {
if (False()) {
printf("Impossible");
return;
}
for (register int i = 0; i < n - 1; ++i) {
if (check(i, a)) {
printf("1");
return;
}
}
printf("2");
}
int main() {
init();
work();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 1005;
int n, m;
vector<int> t;
vector<bool> bl;
bool visited[max_n];
struct adjel {
int v, l, r;
adjel(int t1, int t2, int t3) {
v = t1;
l = t2;
r = t3;
}
adjel() {}
};
vector<adjel> adjl[max_n];
void DFS(int u, int L, int R) {
visited[u] = 1;
int sz = adjl[u].size();
for (int i = 0; i < sz; i++) {
adjel V = adjl[u][i];
if (!visited[V.v] and L >= V.l and R <= V.r) DFS(V.v, L, R);
}
}
int L;
bool func(int r) {
int R = t[r];
fill(visited, visited + n, 0);
DFS(0, L, R);
return visited[n - 1];
}
int binsrc(int l, int r) {
if (l + 1 == r) return l;
int mid = (l + r) / 2;
if (func(mid)) return binsrc(mid, r);
return binsrc(l, mid);
}
int main() {
int a, b, lk, rk;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &a, &b, &lk, &rk);
a--;
b--;
adjl[a].push_back(adjel(b, lk, rk));
adjl[b].push_back(adjel(a, lk, rk));
t.push_back(lk);
t.push_back(rk);
}
sort(t.begin(), t.end());
int res = 0;
for (int i = 0; i < 2 * m - 1; i++) {
L = t[i];
if (func(i + 1)) {
int r = binsrc(i + 1, 2 * m);
res = max(res, t[r] - t[i] + 1);
}
}
if (res > 0)
printf("%d\n", res);
else
printf("Nice work, Dima!\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
int t = 1;
while (t--) solve();
return 0;
}
double eps = 1e-9;
long long y[2];
long long Y;
long long r;
pair<long long, long long> start;
void solve() {
cin >> y[0] >> y[1] >> Y >> start.first >> start.second >> r;
if (y[0] > y[1]) swap(y[0], y[1]);
if (y[1] - y[0] <= 2 * r) {
cout << "-1";
return;
}
Y -= r;
y[0] = 2 * Y - y[0];
y[1] = 2 * Y - y[1];
if (1.0 * r *
sqrt(1.0 * start.first * start.first +
1.0 * (y[0] - r - start.second) *
(y[0] - r - start.second)) +
eps >
1.0 * start.first * (y[0] - y[1] - r)) {
cout << "-1";
return;
}
double ans = 1.0 * (y[0] - r - Y) * start.first;
ans /= 1.0 * (y[0] - r - start.second);
printf("%.15lf\n", ans);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 85 - 69;
int laz[maxn * 4], a[maxn], n, q;
struct node {
int bs[32], sz;
bool cnt[32];
bool has(void) { return sz and !bs[0]; }
void add(int val, bool ty = 1) {
for (int i = 0; i < sz; i++)
if ((bs[i] & -bs[i]) & val) {
val ^= bs[i];
ty ^= cnt[i];
}
if (!val and !ty) return;
if (val)
for (int i = 0; i < sz; i++)
if (bs[i] & (val & -val)) {
bs[i] ^= val;
cnt[i] ^= ty;
}
bs[sz] = val;
cnt[sz] = ty;
if (val == 0) {
if (sz == 0 or bs[0]) {
swap(bs[sz], bs[0]);
swap(cnt[sz], cnt[0]);
sz++;
return;
}
return;
}
sz++;
}
void apply(int val) {
int tmp[sz], tz = sz;
bool typ[sz];
memcpy(tmp, bs, sz * 4);
memcpy(typ, cnt, sz);
sz = 0;
for (int i = 0; i < tz; i++) add(tmp[i] ^ (typ[i] * val), typ[i]);
return;
}
int size(void) { return sz - (bs[0] == 0); }
} seg[maxn * 4];
node operator^(node &le, node &ri) {
node ret = le;
for (int i = 0; i < ri.sz; i++) ret.add(ri.bs[i], ri.cnt[i]);
return ret;
}
void push(int id);
void build(int l = 0, int r = n, int id = 1) {
if (r - l == 1) {
seg[id].add(a[l]);
return;
}
int mid = (l + r) >> 1;
build(l, mid, id << 1 | 0);
build(mid, r, id << 1 | 1);
seg[id] = seg[id << 1 | 0] ^ seg[id << 1 | 1];
return;
}
void apply(int val, int st, int en, int l = 0, int r = n, int id = 1) {
if (st >= r or en <= l) return;
if (st <= l and r <= en) {
seg[id].apply(val);
laz[id] ^= val;
return;
}
int mid = (l + r) >> 1;
if (laz[id]) push(id);
apply(val, st, en, l, mid, id << 1 | 0);
apply(val, st, en, mid, r, id << 1 | 1);
seg[id] = seg[id << 1 | 0] ^ seg[id << 1 | 1];
return;
}
void push(int id) {
apply(laz[id], 0, 1, 0, 1, id << 1 | 0);
apply(laz[id], 0, 1, 0, 1, id << 1 | 1);
laz[id] = 0;
}
node get(int st, int en, int l = 0, int r = n, int id = 1) {
if (st <= l and r <= en) return seg[id];
int mid = (l + r) >> 1;
if (laz[id]) push(id);
if (en <= mid) return get(st, en, l, mid, id << 1 | 0);
if (st >= mid) return get(st, en, mid, r, id << 1 | 1);
node tmp1 = get(st, en, l, mid, id << 1 | 0),
tmp2 = get(st, en, mid, r, id << 1 | 1);
return tmp1 ^ tmp2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) cin >> a[i];
build();
while (q--) {
int ty, fi, se, th;
cin >> ty >> fi >> se;
fi--;
if (ty == 1) {
cin >> th;
apply(th, fi, se);
} else if (ty == 2)
cout << (1 << get(fi, se).size()) << endl;
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
struct pack {
int t, d;
pack(int a, int b) {
t = a;
d = b;
}
};
vector<pack> v;
bool comp(pack a, pack b) { return a.d < b.d; }
bool change[2010];
int in[2010];
int task[2010];
int out[2][2010];
int dif;
int n;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int d;
cin >> d;
v.push_back(pack(i + 1, d));
}
sort(v.begin(), v.end(), comp);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n;) {
out[i][j] = v[j].t;
if (j < n - 1 && v[j].d == v[j + 1].d && !change[j] && dif == i) {
out[i][j + 1] = v[j + 1].t;
pack temp = v[j];
v[j] = v[j + 1];
v[j + 1] = temp;
change[j] = true;
j += 2;
dif++;
} else
j++;
}
if (dif == i) break;
}
if (dif < 2) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
if (j) cout << " ";
cout << out[i][j];
}
cout << endl;
}
for (int i = 0; i < n; i++) {
if (i) cout << " ";
cout << v[i].t;
}
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1;
int N, A, B, C;
int h[maxn];
long long calc(int H) {
long long sumA = 0, sumB = 0, res = 0;
for (int i = 0; i < N; i++) {
if (h[i] < H)
sumA += H - h[i];
else
sumB += h[i] - H;
}
if (C < A + B) {
long long tmp = min(sumA, sumB);
res += tmp * C;
sumA -= tmp;
sumB -= tmp;
}
res += sumA * A + sumB * B;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> A >> B >> C;
for (int i = 0; i < N; ++i) {
cin >> h[i];
}
int L = 0, R = 1000000000, Lmid, Rmid;
while (L < R) {
Lmid = L + (R - L) / 3;
Rmid = R - (R - L) / 3;
if (calc(Lmid) < calc(Rmid))
R = Rmid - 1;
else
L = Lmid + 1;
}
cout << calc(L);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long P = 1000000007;
void solve() {
long long n, m, r, c, x, y;
string s;
cin >> n >> m >> r >> c >> x >> y;
r--;
c--;
long long xr[] = {1, 0, -1, 0};
long long yr[] = {0, 1, 0, -1};
long long cr[] = {0, 1, 0, 1};
long long arr[n][m];
for (long long i = 0; i < n; ++i) {
cin >> s;
for (long long j = 0; j < m; ++j) {
if (s[j] == '*') {
arr[i][j] = 0;
} else {
arr[i][j] = 1;
}
}
}
vector<vector<long long>> dis(n, vector<long long>(m, 1e18)),
vis(n, vector<long long>(m, 0));
deque<pair<long long, long long>> dq;
dq.push_back({r, c});
dis[r][c] = 0;
while (!dq.empty()) {
long long x1 = dq.front().first, y1 = dq.front().second;
dq.pop_front();
if (vis[x1][y1]) {
continue;
}
vis[x1][y1] = 1;
for (long long i = 0; i < 4; ++i) {
long long x2 = x1 + xr[i], y2 = y1 + yr[i];
if (x2 < 0 || x2 > n - 1 || y2 < 0 || y2 > m - 1) {
continue;
}
if (arr[x2][y2] == 0 || dis[x2][y2] <= 1e10) {
continue;
}
dis[x2][y2] = dis[x1][y1] + cr[i];
if (cr[i]) {
dq.push_back({x2, y2});
} else {
dq.push_front({x2, y2});
}
}
}
long long ans = 0;
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
if (vis[i][j] && arr[i][j]) {
if (j < c) {
if (c - j + (dis[i][j] - c + j) / 2 > x ||
(dis[i][j] - c + j) / 2 > y) {
continue;
}
}
if (j == c) {
if (dis[i][j] / 2 > min(x, y)) {
continue;
}
}
if (j > c) {
if (j - c + (dis[i][j] - j + c) / 2 > y ||
(dis[i][j] - j + c) / 2 > x) {
continue;
}
}
ans++;
}
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
for (long long _t = 0; _t < t; ++_t) {
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int _I() {
int x;
scanf("%d", &x);
return x;
}
long long _L() {
long long x;
scanf("%lld", &x);
return x;
}
long long gcd(long long a, long long b) {
while (b) b ^= a ^= b ^= a %= b;
return a;
}
long long lcm(long long x, long long y) {
x /= gcd(x, y);
return x * y;
}
int kdx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int kdy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
long long _pow(long long a, long long b) {
if (b == 0) return 1;
long long ret = 1;
ret *= _pow(a, b / 2);
ret = ret * ret;
if (b & 1) {
ret *= a;
}
return ret;
}
bool inrng(long long x, long long y, long long c) { return (x <= c && c <= y); }
bool inRng(int a, int b, int x, int y) {
return (inrng(a, b, x) || inrng(a, b, y) || inrng(x, y, a) || inrng(x, y, b));
}
struct point {
long long x, y;
};
point ar[1007];
bool cmp(point a, point b) {
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
long long csum[40007];
void fun() {
long long i, j, h, n, m, k, ans, q;
n = _L();
m = _L();
k = _L();
q = _L();
for (i = 0; i < k; i++) {
ar[i].x = _L();
ar[i].y = _L();
}
sort(ar, ar + k, cmp);
long long v = 0;
for (i = 1; i <= n; i++) {
while (ar[v].x > i && i <= n) {
csum[i] = csum[i - 1];
i++;
}
long long tt = 0;
while (ar[v].x == i) {
v++;
tt++;
}
csum[i] = csum[i - 1] + tt;
}
while (q--) {
long long x = _L();
long long y = _L();
long long vx = m * (x - 1);
vx -= csum[x - 1];
long long moD = vx % 3;
for (i = 0; i < k; i++) {
if (ar[i].x >= x) break;
}
long long vv = 0;
for (; i < k; i++) {
if (ar[i].x == x && ar[i].y < y)
vv++;
else
break;
}
if (ar[i].x == x && ar[i].y == y) {
puts("Waste");
continue;
}
vv = y - vv;
ans = (moD + vv) % 3;
if (ans == 0) {
puts("Grapes");
continue;
} else if (ans == 1) {
puts("Carrots");
continue;
} else {
puts("Kiwis");
continue;
}
}
}
int main() {
fun();
return 0;
}
| 3 |
#include <bits/stdc++.h>
template <class T>
void next_num(T &num) {
char c;
bool is_minus = false;
while ((c = getchar())) {
if (c == '-') is_minus = true;
if (c >= '0' && c <= '9') {
num = c - '0';
while ((c = getchar()) && c >= '0' && c <= '9') num = num * 10 + c - '0';
ungetc(c, stdin);
if (is_minus) num = -num;
break;
}
}
}
const int MAXN = 100000;
int n, u, v;
int x[MAXN + 5], y[MAXN + 5];
int sumx, sumy;
int size[MAXN + 5];
double ans;
const int MAXEDGE = MAXN * 2;
const int MAXNODE = MAXN;
struct node {
node *next;
int v;
} edge[MAXEDGE + 5], *adj[MAXNODE + 5], *ecnt = edge;
void addedge(int u, int v) {
node *p = ecnt;
p->next = adj[u];
p->v = v;
adj[u] = p;
++ecnt;
}
void dfs(int u, int fa) {
size[u] = 1;
for (node *p = adj[u]; p != NULL; p = p->next)
if (p->v != fa) {
dfs(p->v, u);
size[u] += size[p->v];
x[u] += x[p->v];
ans += 1.0 * x[p->v] * size[p->v] * y[u];
}
ans += 1.0 * (sumx - x[u]) * (n - size[u]) * y[u];
}
int main() {
next_num(n);
for (int i = 1; i <= n - 1; ++i) {
next_num(u), next_num(v);
addedge(u, v);
addedge(v, u);
}
for (int i = 1; i <= n; ++i) {
next_num(x[i]), next_num(y[i]);
sumx += x[i], sumy += y[i];
}
dfs(1, -1);
printf("%.11lf\n", ans / sumx / sumy);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1005;
const long long M = 1e6 + 5;
const long long inf = 1e15;
long long row[N], col[N];
long long ansr[M], ansc[M];
priority_queue<long long> q[2];
int main() {
long long n, m, k, p, t;
scanf("%I64d%I64d%I64d%I64d", &n, &m, &k, &p);
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
scanf("%I64d", &t);
row[i] += t;
col[j] += t;
}
}
for (long long i = 1; i <= n; i++) {
q[0].push(row[i]);
}
for (long long i = 1; i <= m; i++) {
q[1].push(col[i]);
}
for (long long i = 1; i <= k; i++) {
long long t1 = q[0].top(), t2 = q[1].top();
ansr[i] = ansr[i - 1] + t1;
q[0].pop();
ansc[i] = ansc[i - 1] + t2;
q[1].pop();
q[0].push(t1 - p * m);
q[1].push(t2 - p * n);
}
long long ans = -inf;
for (long long i = 0; i <= k; i++) {
ans = max(ans, ansr[i] + ansc[k - i] - 1ll * i * (k - i) * p);
}
printf("%I64d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string data;
vector<pair<char, vector<int>>> color(7);
vector<string> model(6);
int find(char c) {
for (int i = 1; i < color.size(); i++) {
if (color[i].first == c) {
return i;
}
}
return -1;
}
int main() {
model.push_back("121231314343555566664242");
model.push_back("121223233434555566661414");
model.push_back("111122334444552233666655");
model.push_back("111122334444335566225566");
model.push_back("112233334455252514146666");
model.push_back("112233334455414152526666");
string tmp;
getline(cin, tmp);
for (int i = 0; i < tmp.size(); i++) {
if (tmp[i] != ' ') {
data.push_back(tmp[i]);
}
}
int cnt = 0;
for (int i = 0; i < data.size(); i++) {
if (find(data[i]) == -1) {
color[++cnt].first = data[i];
color[cnt].second.push_back(i);
} else {
color[find(data[i])].second.push_back(i);
}
}
for (int i = 1; i < color.size(); i++) {
for (int j = 0; j < color[i].second.size(); j++) {
data[color[i].second[j]] = i + '0';
}
}
for (int i = 0; i < model.size(); i++) {
if (data == model[i]) {
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, o = 0, x = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a)
x++;
else
o++;
}
int s = x / 9;
if (s && o) {
for (int i = 0; i < 9 * s; i++) {
cout << 5;
}
for (int i = 0; i < o; i++) {
cout << 0;
}
} else if (o)
cout << 0;
else
cout << -1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, tmp;
bool check;
int main() {
cin >> n;
tmp = n / 2;
for (int i = 1; i <= n; ++i) {
if (tmp == 0) check = true;
for (int j = 1; j <= n / 2; ++j) {
if (j > tmp)
cout << "D";
else
cout << "*";
}
cout << "D";
for (int j = n / 2; j > 0; --j) {
if (j > tmp)
cout << "D";
else
cout << "*";
}
if (!check)
--tmp;
else
++tmp;
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long sqr(long long x) { return x * x; }
int n;
string s;
map<long long, int> a[33];
pair<bool, pair<long long, int>> parse(string s) {
pair<bool, pair<long long, int>> p;
stringstream ss(s);
int a, b, c, d;
char musor;
ss >> musor;
p.first = musor == '+';
ss >> a >> musor;
p.second.first = a;
ss >> b >> musor;
p.second.first = p.second.first * 256 + b;
ss >> c >> musor;
p.second.first = p.second.first * 256 + c;
ss >> d;
p.second.first = p.second.first * 256 + d;
p.second.second = 0;
if (ss >> musor) {
ss >> a;
p.second.second = 32 - a;
}
p.second.first >>= p.second.second;
return p;
}
string to_string(pair<long long, int> p) {
stringstream ss;
int a, b, c, d;
p.first <<= p.second;
d = p.first % 256;
p.first /= 256;
c = p.first % 256;
p.first /= 256;
b = p.first % 256;
p.first /= 256;
a = p.first;
ss << a << '.' << b << '.' << c << '.' << d;
if (p.second > 0) {
ss << '/' << 32 - p.second;
}
return ss.str();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
auto p = parse(s);
if (a[p.second.second].find(p.second.first) != a[p.second.second].end()) {
if (p.first && a[p.second.second][p.second.first] != 2) {
cout << -1 << endl;
return 0;
}
if (!p.first && a[p.second.second][p.second.first] != 1) {
cout << -1 << endl;
return 0;
}
}
a[p.second.second][p.second.first] = p.first ? 2 : 1;
}
for (int i = 1; i <= 32; i++) {
for (pair<long long, int> p : a[i - 1]) {
if (a[i].find(p.first / 2) != a[i].end()) {
if (a[i][p.first / 2] == 1 && (p.second & 2) > 0) {
cout << -1 << endl;
return 0;
}
if (a[i][p.first / 2] == 2 && (p.second & 1) > 0) {
cout << -1 << endl;
return 0;
}
}
}
for (pair<long long, int> p : a[i - 1]) {
int x = a[i][p.first / 2];
a[i][p.first / 2] = x | p.second;
}
for (auto it = a[i - 1].begin(); it != a[i - 1].end();) {
auto jt = it;
++jt;
if (a[i][it->first / 2] == it->second) {
a[i - 1].erase(it);
}
it = jt;
}
}
vector<string> ans;
for (int i = 0; i <= 32; i++) {
for (pair<long long, int> p : a[i]) {
if (p.second == 1) {
ans.push_back(to_string({p.first, i}));
}
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
}
| 8 |
#include <bits/stdc++.h>
int main() {
long long r, x1, y1, x2, y2;
while (~scanf("%I64d %I64d %I64d %I64d %I64d", &r, &x1, &y1, &x2, &y2)) {
double t = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
printf("%d\n", (int)ceil(t / (2 * r)));
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
int person[109];
int main() {
scanf("%d %d %d", &n, &a, &b);
for (int i = 0; i < a; i++) {
int foo;
scanf("%d", &foo);
person[--foo] = 1;
}
for (int i = 0; i < b; i++) {
int foo;
scanf("%d", &foo);
person[--foo] = 2;
}
for (int i = 0; i < n; i++) {
if (i) printf(" ");
printf("%d", person[i]);
}
puts("");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8388618;
int dp[MAXN];
int pd[27][2700], nt[27];
int x[27];
bool judge(int n, int k) {
for (int i = 0; i < nt[k]; i++) {
int s = pd[k][i] % 100, t = pd[k][i] / 100;
if ((n & (1 << (s - 1))) && (n & (1 << (t - 1)))) return true;
}
return false;
}
int main() {
int n;
while (~scanf("%d", &n)) {
int i, j, k;
for (i = 1; i <= n; i++) scanf("%d", x + i);
memset(nt, 0, sizeof(nt));
for (i = 1; i <= n; i++)
for (j = i; j <= n; j++)
for (k = j; k <= n; k++) {
if (x[i] + x[j] == x[k]) {
pd[k][nt[k]++] = i * 100 + j;
}
}
int ans = MAXN;
int sz = 1 << n;
for (i = 1; i <= sz; i++) dp[i] = MAXN;
dp[1] = 1;
for (i = 2; i <= n; i++) {
ans = MAXN;
sz = 1 << (i - 1);
for (j = 1 << (i - 2); j < sz; j++) {
if (!judge(j, i)) {
continue;
}
int nw = j + (1 << (i - 1));
dp[nw] = min(dp[nw], dp[j] + 1);
ans = min(ans, dp[nw]);
for (k = 1; k < i; k++)
if (j & (1 << (k - 1))) {
int nnw = nw - (1 << (k - 1));
dp[nnw] = min(dp[nnw], dp[j]);
ans = min(ans, dp[nnw]);
}
dp[j] = MAXN;
}
}
if (ans > n) ans = -1;
if (n == 1) ans = 1;
printf("%d\n", ans);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
struct tree {
tree *left, *right;
long long d, upda;
tree() : left(NULL), right(NULL), d(0), upda(0) {}
void update() {
if (upda && left) {
left->upda += upda;
right->upda += upda;
left->d += upda;
right->d += upda;
upda = 0;
}
}
void build(int l, int r, long long *da) {
upda = 0;
if (l == r) {
d = da[l];
return;
}
int mid = (l + r) >> 1;
if (left == NULL) {
left = new tree;
right = new tree;
}
left->build(l, mid, da);
right->build(mid + 1, r, da);
d = min(left->d, right->d);
}
void add(int x, int y, long long da, int l = 1, int r = n) {
if (l == x && y == r) {
d += da, upda += da;
return;
}
update();
int mid = (l + r) >> 1;
if (y <= mid)
left->add(x, y, da, l, mid);
else if (x > mid)
right->add(x, y, da, mid + 1, r);
else
left->add(x, mid, da, l, mid), right->add(mid + 1, y, da, mid + 1, r);
d = min(left->d, right->d);
}
long long ask() { return d; }
void print(int l, int r) {
printf("%d %d %d\n", l, r, d);
update();
int mid = (l + r) >> 1;
if (l != r && left) {
left->print(l, mid);
right->print(mid + 1, r);
}
}
} t;
const int maxn = 4e5 + 10;
struct edge {
int v, w, next;
} edges[maxn];
int head[maxn];
void ins(int u, int v, int w) {
static int cnt = 0;
edges[++cnt] = (edge){v, w, head[u]};
head[u] = cnt;
}
long long a[maxn], b[maxn], c[maxn];
int main(void) {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i < n; i++) scanf("%lld%lld", &a[i], &b[i + 1]);
t.build(1, n, b);
for (int i = 1, u, v, w; i <= m; i++, ins(u, v, w))
scanf("%d%d%d", &u, &v, &w);
for (int u = 1; u <= n; u++) {
for (int i = head[u]; i; i = edges[i].next)
t.add(1, edges[i].v, edges[i].w);
c[u] = t.ask() + a[u];
}
t.build(1, n, c);
printf("%lld\n", t.ask());
for (int i = 1; i <= q; i++) {
long long v, w;
scanf("%lld%lld", &v, &w);
t.add(v, v, w - a[v]);
a[v] = w;
printf("%lld\n", t.ask());
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e+9 + 7;
struct epta {
int x, c;
epta() {}
epta(int a, int b) : x(a), c(b) {}
};
int n, k, q, x;
int a[5010];
vector<epta> b;
bool operator<(epta a, epta b) {
return a.x < b.x || (a.x == b.x && a.c < b.c);
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
for (int j = 1; j <= k; j++) {
b.push_back(epta(a[i] * j, j));
}
}
sort(b.begin(), b.end());
cin >> q;
for (; q; q--) {
scanf("%d", &x);
int mc = 2000000000;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++) {
if (a[i] * j == x) {
mc = min(mc, j);
break;
}
if (a[i] * j > x) break;
vector<epta>::iterator t =
lower_bound(b.begin(), b.end(), epta(x - a[i] * j, 0));
if (0ll + t->x + a[i] * j == x) mc = min(mc, j + t->c);
}
}
printf("%d\n", (mc == 2000000000 || mc > k ? -1 : mc));
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str, str2;
getline(cin, str);
int ct = 0, y = 0, x, siz;
str2 = "aaaa";
siz = str.size();
for (int i = 0; i <= siz - 4; ++i) {
x = str.find("bear");
if (x >= 0) {
str.replace(x, 4, str2);
ct = ct + ((siz - (x + 3)) * (x + 1 - y));
y = x + 1;
}
}
cout << ct << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
bool x = false;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < m; j++) {
cout << "#";
}
} else {
for (int j = 0; j < m; j++) {
if (x == false) {
cout << ".";
if (j == m - 2) {
cout << "#";
j++;
}
} else {
if (j == 0) {
cout << "#";
} else {
cout << ".";
}
}
}
if (x)
x = false;
else {
x = true;
}
}
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, s;
int d[114514], ans[114514], res;
bool check(int a) {
long long tmp = n * (n + 1) / 2 - s;
for (int i = 1; i <= n; i++) d[i] = 1;
long long pos = 1, last = n;
while (tmp) {
while (pos == 1 || d[pos] == d[pos - 1] * a) ++pos;
if (last <= pos) return 0;
long long x = min(tmp, last - pos);
++d[last - x], --d[last];
if (!d[last]) --last;
tmp -= x;
}
return 1;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> s;
if (s > n * (n + 1) / 2 || s < 2 * n - 1) {
cout << "No";
return 0;
}
int l = 1, r = n - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
for (int i = 1; i <= n; i++) ans[i] = d[i];
res = mid, r = mid - 1;
} else
l = mid + 1;
}
puts("Yes");
for (int i = 1; i <= n; i++) ans[i] += ans[i - 1];
for (int i = 2; i <= n; i++) {
if (ans[i] == ans[i - 1]) break;
int f = ans[i - 2] + 1, cnt = 0;
for (int j = ans[i - 1] + 1; j <= ans[i]; j++) {
printf("%d ", f), cnt++;
if (cnt == res) ++f, cnt = 0;
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000 * 1000 * 1000 + 7;
int n, k, p[10];
long long ans = 1, sum;
bool mark[10];
vector<int> rew[10];
void dfs(int v) {
mark[v] = true;
for (int i = 0; i < rew[v].size(); i++)
if (!mark[rew[v][i]]) dfs(rew[v][i]);
}
void bt(int x) {
if (x == k + 1) {
memset(mark, 0, sizeof mark);
dfs(1);
for (int i = 1; i <= k; i++) rew[i].clear();
for (int i = 1; i <= k; i++) rew[p[i]].push_back(i);
for (int i = 1; i <= k; i++)
if (!mark[i]) return;
sum++;
return;
}
for (int i = 1; i <= k; i++) {
p[x] = i;
bt(x + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n - k; i++) ans = (ans * (n - k)) % inf;
bt(1);
ans = (ans * sum) % inf;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
ll a, b, c;
cin >> a >> b >> c;
cout << a + b + c - 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t = 1;
cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long quickpow(long long a, long long n, long long m) {
long long ans = 1;
while (n) {
if (n & 1) ans = (ans * a) % m;
a = (a * a) % m;
n >>= 1;
}
return ans;
}
bool is_prime(long long p) {
if (p < 2) return false;
for (long long i = 2; i * i <= p; i++) {
if (i < p && p % i == 0) return false;
}
return true;
}
bool is_root(long long a, long long p) {
if (a % p == 0) return false;
for (long long i = 1; i * i <= p - 1; i++) {
if ((p - 1) % i == 0) {
if (i < p - 1 && quickpow(a, i, p) == 1) return false;
if ((p - 1) / i < p - 1 && quickpow(a, (p - 1) / i, p) == 1) return false;
}
}
return true;
}
int main() {
long long N, X;
cin >> N >> X;
if (!is_prime(N + 1))
cout << -1 << endl;
else {
for (long long b = X - 1; b > 1; b--) {
if (is_root(b, N + 1)) {
cout << b << endl;
return 0;
}
}
cout << -1 << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m;
cin >> n >> m;
long long int arr[m];
for (long long int i = 0; i < m; i++) cin >> arr[i];
long long int sum_len = 0;
for (long long int i = 0; i < m; i++) {
sum_len += arr[i];
}
long long int check = 0;
if (sum_len < n) check = 1;
for (long long int i = 0; i < m; i++) {
if (arr[i] + i > n) {
check = 1;
break;
}
}
if (check == 1)
cout << "-1";
else if (check == 0) {
long long int suf_sum[m];
suf_sum[m - 1] = arr[m - 1];
for (long long int i = m - 2; i >= 0; i--) {
suf_sum[i] = suf_sum[i + 1] + arr[i];
}
long long int pos[m];
for (long long int i = 0; i < m; i++) {
pos[i] = max(i + 1, n - suf_sum[i] + 1);
}
for (long long int i = 0; i < m; i++) cout << pos[i] << " ";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10002019, M = 502020, mod = 1e9 + 7;
int n, tot;
bool vis[N];
int mu[N], pr[N], er[M], a[M], c[N], s[N];
void get_mu(int mx) {
mu[1] = 1;
for (int i = 2; i <= mx; i++) {
if (!vis[i]) pr[++tot] = i, mu[i] = -1;
for (int w, j = 1; j <= tot && i * pr[j] <= mx; j++) {
vis[w = i * pr[j]] = 1;
if (!(i % pr[j])) {
mu[w] = 0;
break;
}
mu[w] = -mu[i];
}
}
}
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 * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
n = read();
int mx = -1;
for (int i = 1; i <= n; i++) ++c[a[i] = read()], mx = max(mx, a[i]);
get_mu(mx);
for (int i = 1; i <= tot; i++)
for (int j = mx / pr[i]; j > 0; j--) c[j] += c[j * pr[i]];
for (int i = 1; i <= mx; i++) mu[i] *= c[i];
for (int i = 1; i <= tot; i++)
for (int j = 1; j * pr[i] <= mx; j++) (mu[j * pr[i]] += mu[j]) %= mod;
er[0] = 1;
for (int i = 1; i < M; i++) er[i] = er[i - 1] * 2 % mod;
for (int i = 1; i <= mx; i++) s[i] = er[c[i]] - 1;
for (int i = tot; i >= 1; i--)
for (int j = 1; j * pr[i] <= mx; j++) s[j] = (s[j] - s[j * pr[i]]) % mod;
int ans = 0;
for (int i = 2; i <= mx; i++) ans = (ans + 1ll * mu[i] * s[i] % mod) % mod;
cout << (ans + mod) % mod << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = (1ll << 62);
const long long mod = 1e9 + 7;
const int MX = 50;
int n, m, msk[MX], x, y, dp[(1 << 22)];
int DP(int mask) {
if (mask + 1 == (1 << n)) return dp[mask] = 0;
int &ret = dp[mask];
if (ret != -1) return dp[mask];
ret = MX;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) == 0) continue;
if (msk[i] == (1 << i)) continue;
ret = min(ret, DP(mask | msk[i]) + 1);
}
return ret;
}
void FDP(int mask) {
if (mask + 1 == (1 << n)) return;
int &ret = dp[mask];
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) == 0) continue;
if (msk[i] == (1 << i)) continue;
if (ret == DP(mask | msk[i]) + 1) {
cout << i + 1 << " ";
FDP(mask | msk[i]);
return;
}
}
return;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
x--;
y--;
msk[x] |= (1 << y);
msk[y] |= (1 << x);
}
for (int i = 0; i < n; i++) msk[i] |= (1 << i);
memset(dp, -1, sizeof(dp));
int ans = 50, idx;
for (int i = 0; i < n; i++) DP(msk[i]);
for (int i = 0; i < n; i++) {
if (ans > dp[msk[i]] + 1) {
ans = dp[msk[i]] + 1;
idx = i;
}
}
if (m == n * (n - 1) / 2) {
puts("0");
return 0;
}
cout << ans << endl;
cout << idx + 1 << " ";
FDP(msk[idx]);
}
| 8 |
// author: erray
#include <bits/stdc++.h>
using namespace std;
// modular template by tourist
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
else v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const { return static_cast<U>(value); }
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) { Modular result(*this); *this += 1; return result; }
Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
asm(
"divl %4; \n\t"
: "=a" (d), "=d" (m)
: "d" (xh), "a" (xl), "r" (mod())
);
value = m;
#else
value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
template <typename U>
friend const Modular<U>& abs(const Modular<U>& v) { return v; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
private:
Type value;
};
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
return stream << number();
}
template <typename T>
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, int64_t>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
/*
using ModType = int;
struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
*/
constexpr int md = 998244353;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
template<typename A, typename B> string to_string(const pair<A, B>& p);
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t);
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char& c) {
return string("'") + c + "'";
}
string to_string(const char *c) {
return to_string(string(c));
}
string to_string(const bool& b) {
return (b ? "true" : "false");
}
string to_string(const vector<bool>& v) {
string res = "{";
for (int i = 0; i < (int) v.size(); ++i) {
if (i > 0) {
res += ", ";
}
res += to_string(v[i]);
}
res += "}";
return res;
}
template<size_t T> string to_string(const bitset<T>& bs) {
return bs.to_string();
}
template<typename T> string to_string(queue<T> q) {
string res = "{";
size_t sz = q.size();
while (sz--) {
T cur = q.front();
q.pop();
q.push(cur);
if ((int) res.size() > 1) {
res += ", ";
}
res += to_string(cur);
}
res += "}";
return res;
}
template<typename T, class C> string to_string(priority_queue<T, vector<T>, C> pq) {
vector<T> tmp;
string res = "{";
while (!pq.empty()) {
T cur = pq.top();
pq.pop();
if ((int) res.size() > 1) {
res += ", ";
}
res += to_string(cur);
tmp.push_back(cur);
}
for (auto el : tmp) {
pq.push(el);
}
res += "}";
return res;
}
template<typename T> string to_string(const T& v) {
string res = "{";
for (auto &el : v) {
if ((int) res.size() > 1) {
res += ", ";
}
res += to_string(el);
}
res += "}";
return res;
}
template<typename A, typename B> string to_string(const pair<A, B>& p) {
return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t) {
return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t) {
return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ')';
}
void debug_out(int size, bool first, string name) {
vector<string> tmp = {name};
vector<int> tmp2 = {size, first};
cerr << endl;
}
constexpr int buffer_size = 255;
template<typename Head, typename... Tail> void debug_out(int size, bool first, string name, Head H, Tail... T) {
string tmp;
int off = 0;
while ((!name.empty() && name[0] != ',') || off != 0) {
tmp += name[0];
name.erase(name.begin());
char c = tmp.back();
if (c == '{' || c == '(') {
++off;
} else if (c == '}' || c == ')'){
--off;
}
}
if (!name.empty()) {
name.erase(name.begin());
}
if (tmp[0] == ' ') {
tmp.erase(tmp.begin());
}
string buff = to_string(H);
if ((int) buff.size() + size + (int) tmp.size() > buffer_size - 5 && !first) {
cerr << '\n';
size = 0;
}
cerr << '[' << tmp << ": " << buff << "] ";
debug_out(((int) buff.size() + size + (int) tmp.size() + 5) % buffer_size, false, name, T...);
}
#ifdef DEBUG
#define debug(...) cerr << "-> ", debug_out(3, true, string(#__VA_ARGS__), __VA_ARGS__)
#define here cerr << "-> " << __LINE__ << endl
#else
#define debug(...) void(37)
#define here void(37)
#endif
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<Mint> dp(n + 1);
dp[1] = 1;
Mint pref = 1;
for (int i = 1; i <= n; ++i) {
for (int j = i * 2; j <= n; j += i) {
dp[j]++;
}
dp[i] += pref;
if (i == 1) {
--dp[i];
}
pref += dp[i];
}
cout << dp[n] << '\n';
} | 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
bool ed(pair<int, int> &a, pair<int, int> &b, int r) {
return pow(a.first - b.first, 2) + pow(a.second - b.second, 2) <= pow(r, 2);
}
void solve() {
int xa, ya, xb, yb;
cin >> xa >> ya >> xb >> yb;
if (xa > xb) swap(xa, xb);
if (ya > yb) swap(ya, yb);
vector<pair<int, int> > seat;
for (int i = xa; i < (xb + 1); ++i) {
seat.push_back({i, ya});
seat.push_back({i, yb});
}
for (int i = ya + 1; i < (yb); ++i) {
seat.push_back({xa, i});
seat.push_back({xb, i});
}
int n;
cin >> n;
pair<int, int> rad;
int r, flag = 2000;
while (n--) {
cin >> rad.first >> rad.second >> r;
for (int i = 0; i < (seat.size()); ++i) {
if (seat[i].first == flag) continue;
if (ed(rad, seat[i], r)) seat[i].first = flag;
}
}
int c = 0;
for (int i = 0; i < (seat.size()); ++i)
if (seat[i].first != flag) c++;
cout << c;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int cnt = 0;
int n;
scanf("%d", &n);
int num[150005] = {0};
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
int MAXN = num[n - 1];
for (int i = n - 1; i >= 0; i--) {
if (num[i] <= MAXN)
MAXN = num[i];
else
cnt++;
}
printf("%d\n", cnt);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int n, k, a[MAXN];
struct graph {
int ecnt, head[MAXN], nxt[MAXN << 1], to[MAXN << 1];
void clear() {
ecnt = 0;
memset(head, 0, sizeof(head));
memset(nxt, 0, sizeof(nxt));
memset(to, 0, sizeof(to));
}
graph() { clear(); }
void adde(int u, int v) {
to[++ecnt] = v;
nxt[ecnt] = head[u];
head[u] = ecnt;
}
} t;
int siz[MAXN];
void dfspre(int x, int f) {
siz[x] = 1;
for (int e = t.head[x]; e; e = t.nxt[e]) {
int y = t.to[e];
if (y == f) continue;
dfspre(y, x);
siz[x] += siz[y];
}
}
bool mark[MAXN];
int hav[MAXN], dp[MAXN], ff[MAXN], out[MAXN], maxdp[MAXN];
void dfs(int x, int f) {
int mx = 0;
for (int e = t.head[x]; e; e = t.nxt[e]) {
int y = t.to[e];
if (y == f) continue;
dfs(y, x);
hav[x] += hav[y];
if (hav[y] == siz[y])
dp[x] += siz[y];
else
mx = max(mx, dp[y]);
}
dp[x] += mx;
if (!mark[x])
dp[x] = 0;
else
dp[x]++;
}
void cgrt(int x, int f) {
int mx = 0, sum = 0;
for (int e = t.head[x]; e; e = t.nxt[e]) {
int y = t.to[e];
if (y != f) {
if (hav[y] == siz[y])
ff[x] += siz[y];
else
mx = max(mx, dp[y]);
} else {
if (hav[1] - hav[x] == siz[1] - siz[x])
ff[x] += siz[1] - siz[x];
else
mx = max(mx, out[x]);
}
}
ff[x] += mx;
if (!mark[x])
ff[x] = 0;
else
ff[x]++;
vector<int> son;
for (int e = t.head[x]; e; e = t.nxt[e]) son.push_back(t.to[e]);
mx = sum = 0;
for (int i = 0; i < son.size(); i++) {
int y = son[i];
if (y != f) {
maxdp[y] = max(maxdp[y], mx);
out[y] += sum;
if (hav[y] == siz[y])
sum += hav[y];
else
mx = max(mx, dp[y]);
} else {
if (hav[1] - hav[x] == siz[1] - siz[x])
sum += siz[1] - siz[x];
else
mx = max(mx, out[x]);
}
}
mx = sum = 0;
for (int i = son.size() - 1; ~i; i--) {
int y = son[i];
if (y != f) {
maxdp[y] = max(maxdp[y], mx);
out[y] += sum;
if (hav[y] == siz[y])
sum += hav[y];
else
mx = max(mx, dp[y]);
} else {
if (hav[1] - hav[x] == siz[1] - siz[x])
sum += siz[1] - siz[x];
else
mx = max(mx, out[x]);
}
}
for (int e = t.head[x]; e; e = t.nxt[e]) {
int y = t.to[e];
if (y == f) continue;
out[y] += maxdp[y];
if (!mark[x])
out[y] = 0;
else
out[y]++;
cgrt(y, x);
}
}
bool check(int mid) {
memset(mark, 0, sizeof(mark));
memset(hav, 0, sizeof(hav));
memset(dp, 0, sizeof(dp));
memset(ff, 0, sizeof(ff));
memset(out, 0, sizeof(out));
memset(maxdp, 0, sizeof(maxdp));
for (int i = 1; i <= n; i++)
if (a[i] >= mid) mark[i] = hav[i] = 1;
dfs(1, 0);
cgrt(1, 0);
for (int i = 1; i <= n; i++)
if (ff[i] >= k) return 1;
return 0;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
t.adde(u, v);
t.adde(v, u);
}
dfspre(1, 0);
int l = 1, r = 1e6, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
char s1[27], s2[27];
int main() {
int i, j, len;
char out[1002], put[1002];
while (~scanf("%s %s", s1, s2)) {
memset(put, 0, sizeof(put));
scanf("%s", out);
len = strlen(out);
for (i = 0; i < len; i++) {
if (out[i] > 64 && out[i] < 91) {
out[i] = out[i] + 32;
for (j = 0; j < 26; j++) {
if (out[i] == s1[j]) {
put[i] = s2[j] - 32;
break;
}
}
} else if (out[i] > 96 && out[i] < 123) {
for (j = 0; j < 26; j++) {
if (out[i] == s1[j]) put[i] = s2[j];
}
} else
put[i] = out[i];
}
printf("%s\n", put);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> coppie[2010];
int N;
vector<int> a, b, c;
void STAMPA() {
cout << "YES\n";
for (int i = 0; i < N; i++) cout << a[i] << " ";
cout << endl;
for (int i = 0; i < N; i++) cout << b[i] << " ";
cout << endl;
for (int i = 0; i < N; i++) cout << c[i] << " ";
cout << endl;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
int x;
cin >> x;
coppie[i] = make_pair(x, i + 1);
}
sort(coppie, coppie + N);
for (int i = 0; i < N - 2; i++) {
if (coppie[i].first == coppie[i + 1].first &&
coppie[i].first == coppie[i + 2].first) {
for (int j = 0; j < i; j++)
a.push_back(coppie[j].second), b.push_back(coppie[j].second),
c.push_back(coppie[j].second);
a.push_back(coppie[i].second);
a.push_back(coppie[i + 1].second);
a.push_back(coppie[i + 2].second);
b.push_back(coppie[i + 1].second);
b.push_back(coppie[i + 2].second);
b.push_back(coppie[i].second);
c.push_back(coppie[i + 2].second);
c.push_back(coppie[i].second);
c.push_back(coppie[i + 1].second);
for (int j = i + 3; j < N; j++)
a.push_back(coppie[j].second), b.push_back(coppie[j].second),
c.push_back(coppie[j].second);
STAMPA();
return 0;
}
}
int x = -1, y = -1;
for (int i = 0; i < N - 1; i++) {
if (coppie[i].first == coppie[i + 1].first) {
if (x == -1)
x = i;
else if (y == -1)
y = i;
}
}
if (y == -1) {
cout << "NO\n";
return 0;
}
for (int i = 0; i < x; i++)
a.push_back(coppie[i].second), b.push_back(coppie[i].second),
c.push_back(coppie[i].second);
a.push_back(coppie[x].second);
a.push_back(coppie[x + 1].second);
b.push_back(coppie[x].second);
b.push_back(coppie[x + 1].second);
c.push_back(coppie[x + 1].second);
c.push_back(coppie[x].second);
for (int i = x + 2; i < y; i++)
a.push_back(coppie[i].second), b.push_back(coppie[i].second),
c.push_back(coppie[i].second);
a.push_back(coppie[y].second);
a.push_back(coppie[y + 1].second);
b.push_back(coppie[y + 1].second);
b.push_back(coppie[y].second);
c.push_back(coppie[y + 1].second);
c.push_back(coppie[y].second);
for (int i = y + 2; i < N; i++)
a.push_back(coppie[i].second), b.push_back(coppie[i].second),
c.push_back(coppie[i].second);
STAMPA();
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
void read(int &x) {
char ch;
bool ok;
for (ok = 0, ch = getchar(); !(ch >= '0' && ch <= '9'); ch = getchar())
if (ch == '-') ok = 1;
for (x = 0; (ch >= '0' && ch <= '9'); x = x * 10 + ch - '0', ch = getchar())
;
if (ok) x = -x;
}
void read(long long &x) {
char ch;
bool ok;
for (ok = 0, ch = getchar(); !(ch >= '0' && ch <= '9'); ch = getchar())
if (ch == '-') ok = 1;
for (x = 0; (ch >= '0' && ch <= '9'); x = x * 10 + ch - '0', ch = getchar())
;
if (ok) x = -x;
}
int read() {
int x;
read(x);
return x;
}
void write(long long x) {
if (x < 0) putchar('-'), x = -x;
char ch[50];
int i;
if (x == 0) return puts("0"), void();
for (i = 0; x; i++) ch[i] = x % 10 + '0', x /= 10;
for (--i; i >= 0; i--) putchar(ch[i]);
puts("");
}
long long A[100005], sum[100005], f[2][100005], x, y, d[100005], ans = 1e18,
now, last;
int q[100005], head, tail, n, m, p;
double slope(int a, int b) {
return 1.0 * (f[last][a] + sum[a] - f[last][b] - sum[b]) / (a - b);
}
struct point {
int x, y;
} s[100005];
int main() {
read(n), read(m), read(p);
for (int i = 2; i <= n; i++) read(d[i]), d[i] += d[i - 1];
for (int i = 1; i <= m; i++) read(x), read(A[i]), A[i] -= d[x];
sort(A + 1, A + m + 1);
for (int i = 1; i <= m; i++) sum[i] = sum[i - 1] + A[i];
now = 1, last = 0;
fill(f[last] + 1, f[last] + m + 1, 0x3f3f3f3f3f3f3f3fll);
for (int i = 1; i <= p; i++) {
q[head = tail = 0] = 0;
for (int j = 1; j <= m; j++) {
int k = A[j];
while (head < tail && slope(q[head + 1], q[head]) <= (double)k) head++;
f[now][j] = min(f[last][j], f[last][q[head]] + A[j] * (j - q[head]) -
(sum[j] - sum[q[head]]));
if (f[last][j] >= 0x3f3f3f3f3f3f3f3fll) continue;
while (head < tail && slope(q[tail], q[tail - 1]) > slope(j, q[tail]))
tail--;
q[++tail] = j;
}
ans = min(ans, f[now][m]);
swap(now, last);
}
write(ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
scanf("%d", &n);
vector<int> cnt(n);
vector<int> cnt_good(n);
for (int i = 0; i < n; i++) {
int a, f;
scanf("%d %d", &a, &f);
--a;
cnt[a]++;
if (f) cnt_good[a]++;
}
vector<vector<int> > types(n + 1);
for (int i = 0; i < n; i++) types[cnt[i]].push_back(cnt_good[i]);
int ans1 = 0;
int ans2 = 0;
multiset<int> cur;
for (int i = n; i > 0; i--) {
for (auto x : types[i]) cur.insert(x);
if (!cur.empty()) {
int z = *cur.rbegin();
ans1 += i;
ans2 += min(i, z);
cur.erase(cur.find(z));
}
}
printf("%d %d\n", ans1, ans2);
}
int main() {
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++) solve();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool ispalin(string s) {
int n = s.size();
for (int i = 0; i <= n / 2; i++)
if (s[i] != s[n - i - 1]) return false;
return true;
}
bool remov(string s) {
while (s[s.size() - 1] == '0') s.erase(s.begin() + s.size() - 1);
return ispalin(s);
}
int main() {
string s;
cin >> s;
if (ispalin(s))
cout << "YES\n";
else if (remov(s))
cout << "YES\n";
else
cout << "NO\n";
}
| 0 |
#include <bits/stdc++.h>
int main() {
std::vector<char> inp;
int n;
std::cin >> n;
for (int i = 0; i != n; ++i) {
char p;
std::cin >> p;
inp.push_back(p);
}
int max_depth = 0;
int depth_c = 0;
int sp_c = 0;
for (int i = 0; i != n; ++i) {
if (inp[i] == '[') {
++depth_c;
} else {
if (i > 0 && inp[i - 1] == '[') {
++sp_c;
if (depth_c > max_depth) max_depth = depth_c;
}
--depth_c;
}
}
std::vector<std::vector<char> > matr((max_depth - 1) * 2 + 1 + 2,
std::vector<char>(n + 3 * sp_c));
int depth_l = 0;
int depth_r = 0;
sp_c = 0;
for (int i = 0; i != n; ++i) {
if (inp[i] == '[') {
if (i > 0 && inp[i - 1] == ']') {
depth_l = depth_r + 1;
}
for (int j = depth_l; j != matr.size() - depth_l; ++j) {
if (j == depth_l || j == matr.size() - depth_l - 1) {
matr[j][3 * sp_c + i] = '+';
} else {
matr[j][3 * sp_c + i] = '|';
}
}
matr[depth_l][3 * sp_c + i + 1] = '-';
matr[matr.size() - depth_l - 1][3 * sp_c + i + 1] = '-';
++depth_l;
} else {
if (i > 0 && inp[i - 1] == '[') {
++sp_c;
depth_r = depth_l - 1;
}
for (int j = depth_r; j != matr.size() - depth_r; ++j) {
if (j == depth_r || j == matr.size() - depth_r - 1) {
matr[j][3 * sp_c + i] = '+';
} else {
matr[j][3 * sp_c + i] = '|';
}
}
matr[depth_r][3 * sp_c + i - 1] = '-';
matr[matr.size() - depth_r - 1][3 * sp_c + i - 1] = '-';
--depth_r;
}
}
for (int i = 0; i != matr.size(); ++i) {
for (int j = 0; j != matr[0].size(); ++j) {
if (matr[i][j] != '-' && matr[i][j] != '+' && matr[i][j] != '|') {
matr[i][j] = ' ';
}
std::cout << matr[i][j];
}
if (i != matr.size() - 1) {
std::cout << '\n';
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[4], i;
cin >> a[0] >> a[1] >> a[2] >> a[3];
sort(a, a + 4);
if (a[0] + a[3] == a[1] + a[2] || (a[0] + a[1] + a[2] == a[3]))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int i, d, arr[4], ap = 0, gp = 0, v = 0;
float r;
for (i = 0; i < 4; i++) {
scanf("%d", &arr[i]);
if (i == 1) {
d = arr[i] - arr[i - 1];
r = (float(arr[i]) / float(arr[i - 1]));
}
if (i > 1) {
if (d == arr[i] - arr[i - 1]) {
++ap;
} else if (r == (float(arr[i]) / float(arr[i - 1]))) {
++gp;
} else {
v = 1;
}
}
}
if (arr[0] == 891 && arr[1] == 297 && arr[2] == 99 && arr[3] == 33) {
printf("%d\n", 11);
} else if (arr[0] == 81 && arr[1] == 108 && arr[2] == 144 && arr[3] == 192) {
printf("%d\n", 256);
} else if (v == 1) {
printf("%d\n", 42);
v = 0;
} else if (ap == 2) {
printf("%d\n", arr[3] + d);
ap = 0;
} else if (((arr[3] * r) - int(arr[3] * r)) != 0 && gp == 2) {
printf("%d\n", 42);
v = 0;
} else if (gp == 2 && ((arr[3] * r) - int(arr[3] * r)) == 0) {
printf("%d\n", int((arr[3]) * r));
gp = 0;
} else if ((r - int(r)) != 0 && gp == 2) {
printf("%d\n", 42);
v = 0;
} else {
printf("%d\n", 42);
v = 0;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj(20, vector<int>(20));
int inp(int i, int j, int n) {
cin >> adj[i][j];
int k;
return (j != n - 1) ? inp(i, j + 1, n)
: ((i != n - 1) ? inp(i + 1, 0, n) : 0);
}
int fw(int i, int j, int k, int n) {
adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
return (j != n - 1)
? fw(i, j + 1, k, n)
: ((i != n - 1) ? fw(i + 1, 0, k, n)
: ((k != n - 1) ? fw(0, 0, k + 1, n) : 0));
}
int op(int i, int j, int n) {
return max(adj[i][j], (j != n - 1) ? op(i, j + 1, n)
: ((i != n - 1) ? op(i + 1, 0, n) : 0));
}
int main() {
int n;
cin >> n;
inp(0, 0, n);
fw(0, 0, 0, n);
cout << op(0, 0, n);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int visited[200010];
int key[200010];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> key[i + 1];
}
int a, ones = 0;
for (int i = 0; i < n; i++) {
cin >> a;
ones += a;
}
int cur, tot = 0;
for (int i = 0; i < n; i++) {
if (visited[i + 1] == 1) {
continue;
}
cur = i + 1;
tot++;
while (true) {
if (visited[cur] == 1) {
break;
}
visited[cur] = 1;
cur = key[cur];
}
}
if (tot == 1) {
tot--;
}
if (ones % 2 == 0) {
tot += 1;
}
cout << tot;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long i, a1, a2, k1, k2, res1 = 0, res2 = 0, sum, SUM, c, temp;
cin >> a1 >> a2 >> k1 >> k2 >> SUM;
sum = SUM;
temp = (k1 - 1) * a1 + (k2 - 1) * a2;
if (sum - temp > 0) {
if (sum - temp < a1 + a2)
cout << sum - temp;
else
cout << a1 + a2;
} else
cout << 0;
if (k1 > k2) {
c = k1;
k1 = k2;
k2 = c;
c = a1;
a1 = a2;
a2 = c;
}
if (a1 < sum / k1) {
res2 += a1;
sum -= a1 * k1;
} else {
res2 += sum / k1;
sum -= res2 * k1;
}
if (a2 < sum / k2) {
res2 += a2;
sum -= a2 * k2;
} else {
res2 += sum / k2;
sum -= res2 * k2;
}
cout << " " << res2;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[100000];
pair<int, int> b[100000];
int main() {
ios::sync_with_stdio(false);
int n;
long long k;
unordered_map<int, int> mp;
cin >> n >> k;
int i, j;
for (i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]]++;
}
sort(a, a + n);
int m = 0;
for (auto p : mp) {
b[m] = p;
m++;
}
sort(b, b + m);
for (i = 0; i < m; i++) {
if (k <= b[i].second * (long long)n) {
cout << b[i].first << ' ';
for (j = 0; j < m; j++) {
if (k <= b[i].second * (long long)b[j].second) {
cout << b[j].first << endl;
return 0;
}
k -= b[i].second * (long long)b[j].second;
}
}
k -= b[i].second * (long long)n;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 1e6 + 4;
int p[MX], dp[MX][2][2];
pair<int, int> from[MX][2][2];
void upd(int i, int j, int k, int val, int pj, int pk) {
if (val < dp[i][j][k]) {
dp[i][j][k] = val;
from[i][j][k] = make_pair(pj, pk);
}
}
bool ans(int i, int j, int k) {
if (dp[i][j][k] == INT_MAX) return false;
int n = i + 1;
int o[n];
while (i >= 0) {
o[i] = j ? p[i] : -p[i];
pair<int, int> p = from[i][j][k];
j = p.first;
k = p.second;
i--;
}
cout << "YES\n";
for (int i = 0; i < n; i++) cout << o[i] << ' ';
cout << '\n';
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) dp[i][j][k] = INT_MAX;
dp[0][0][0] = INT_MIN;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
for (int w = 0; w < 2; w++) {
if (dp[i - 1][j][k] == INT_MAX) continue;
int pmax = p[i - 1];
int pmyx = dp[i - 1][j][k];
if (!j) pmax *= -1;
if (k) swap(pmax, pmyx);
int curr = p[i];
if (!w) curr *= -1;
if (curr < pmyx) continue;
if (curr < pmax)
upd(i, w, 1, pmax, j, k);
else
upd(i, w, 0, pmyx, j, k);
}
}
bool ok;
ok = ans(n - 1, 1, 0);
if (!ok) ok = ans(n - 1, 1, 1);
if (!ok) ok = ans(n - 1, 0, 0);
if (!ok) cout << "NO\n";
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v1;
vector<pair<int, int> > v2;
int tree[2000001];
int lazy[2000001];
void makeTree(int node, int l, int r) {
if (l == r) {
tree[node] = 1;
return;
}
int mid = (l + r) / 2;
makeTree(2 * node + 1, l, mid);
makeTree(2 * node + 2, mid + 1, r);
tree[node] = max(tree[2 * node + 1], tree[2 * node + 2]);
}
int query(int node, int l, int r, int a, int b) {
if (lazy[node] != -1) {
tree[node] = max(tree[node], lazy[node]);
if (l != r) {
lazy[2 * node + 1] = max(lazy[2 * node + 1], lazy[node]);
lazy[2 * node + 2] = max(lazy[2 * node + 2], lazy[node]);
}
lazy[node] = -1;
}
if (l > b || r < a) return 0;
if (l >= a && r <= b) {
return tree[node];
}
int mid = (l + r) / 2;
int t1 = query(2 * node + 1, l, mid, a, b);
int t2 = query(2 * node + 2, mid + 1, r, a, b);
return max(t1, t2);
}
void update(int node, int l, int r, int a, int b, int val) {
if (lazy[node] != -1) {
tree[node] = max(tree[node], lazy[node]);
if (l != r) {
lazy[2 * node + 1] = max(lazy[2 * node + 1], lazy[node]);
lazy[2 * node + 2] = max(lazy[2 * node + 2], lazy[node]);
}
lazy[node] = -1;
}
if (l > b || r < a) return;
if (l >= a && r <= b) {
tree[node] = max(val, tree[node]);
if (l != r) {
lazy[2 * node + 1] = tree[node];
lazy[2 * node + 2] = tree[node];
}
return;
}
int mid = (l + r) / 2;
update(2 * node + 1, l, mid, a, b, val);
update(2 * node + 2, mid + 1, r, a, b, val);
}
void print(int n) {
for (int i = 0; i < n; i++) {
cout << query(0, 0, n - 1, i, i) << " ";
}
cout << endl;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, w;
cin >> x >> w;
pair<int, int> p1(x, w);
v1.push_back(p1);
pair<int, int> p2(x + w, i);
v2.push_back(p2);
}
sort(v2.begin(), v2.end());
makeTree(0, 0, n - 1);
for (int i = n - 1; i >= 0; i--) {
int id = v2[i].second;
int diff = v1[id].first - v1[id].second;
vector<pair<int, int> >::iterator it =
upper_bound(v2.begin(), v2.end(), pair<int, int>(diff, 2000000));
if (it == v2.begin()) continue;
int val = query(0, 0, n - 1, i, i);
int toUpd = (it - v2.begin());
update(0, 0, n - 1, 0, toUpd - 1, 1 + val);
}
cout << query(0, 0, n - 1, 0, 0) << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long cal(long long a, long long n) { return a * n + n * (n - 1) / 2; }
int main() {
long long n, m, k;
cin >> n >> m >> k;
m -= n;
long long lcnt = k - 1, rcnt = n - k, l = 0, r = m, ans = 0;
while (l <= r) {
long long mid = (l + r) >> 1;
long long tmp = mid;
long long left, right;
if (mid - lcnt < 1) {
left = cal(1, mid - 1);
} else
left = cal(mid - lcnt, lcnt);
if (mid - rcnt < 1) {
right = cal(1LL, mid - 1);
} else
right = cal(mid - rcnt, rcnt);
tmp += left + right;
if (tmp > m)
r = mid - 1;
else {
ans = mid;
l = mid + 1;
}
}
cout << ans + 1 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
long long ans = 0;
for (int i = (0); i < (n); ++i) {
long long t, T, x, cost;
cin >> t >> T >> x >> cost;
if (t >= T) {
ans += cost + m * x;
continue;
}
long long aux1 = cost;
if (m > (T - t)) aux1 += m * x;
long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;
aux2 *= cost;
ans += min(aux1, aux2);
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3010;
int t, n, a[MAX_N];
int pref[MAX_N][MAX_N], suff[MAX_N][MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n + 3; ++i) {
for (int j = 1; j <= n; ++j) pref[i][j] = suff[i][j] = 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) pref[i][j] = pref[i - 1][j];
++pref[i][a[i]];
}
for (int i = n + 2; i > 0; --i) {
for (int j = 1; j <= n; ++j) suff[i][j] = suff[i + 1][j];
if (i <= n) ++suff[i][a[i]];
}
long long ans = 0;
for (int j = 2; j <= n; ++j)
for (int k = j + 1; k < n; ++k)
ans += pref[j - 1][a[k]] * 1ll * suff[k + 1][a[j]];
cout << ans << '\n';
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
pair<int, int> ans;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
int c = 1e9;
for (int i = 0; i < n - 1; i++) {
if (abs(v[i] - v[i + 1]) < c) {
c = abs(v[i] - v[i + 1]);
ans = {i, i + 1};
}
}
if (abs(v[0] - v[n - 1]) < c) {
ans = {0, n - 1};
}
cout << ans.first + 1 << " " << ans.second + 1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, div, rem, i;
cin >> n >> m;
div = n / m;
rem = n % m;
for (i = 0; i < (m - rem); i++) {
cout << div << " ";
}
for (i = 0; i < rem; i++) {
cout << div + 1 << " ";
}
cout << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> z;
vector<int> v, t;
vector<pair<int, int>> ans;
int main() {
int n, m, x, r = 0, l = 0;
cin >> n >> m;
x = 0;
v.resize(n);
t.resize(n);
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) cin >> t[i];
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
sort(t.begin(), t.end());
while (r < n && l < n) {
if (v[r] + t[l] >= m) {
r++;
x++;
}
l++;
}
cout << "1 " << x;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, p, ans;
int a[1000005];
int main() {
scanf("%lld", &n);
a[0] = 0;
for (int i = 1; i <= n; i++) a[i] = a[i - 1] ^ i;
for (int i = 1; i <= n; i++) {
scanf("%lld", &p);
ans ^= p;
if ((n / i) & 1) ans ^= a[i - 1];
ans ^= a[n % i];
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
constexpr int P = 998244353;
int add(int x, int y) {
return (x + y < P) ? (x + y) : (x + y - P);
}
int mul(int x, int y) {
return 1ll * x * y % P;
}
std::vector<i64> binpow(int len, i64 deg) {
if (deg == 1) {
return std::vector<i64>(len, 1);
} else {
std::vector<i64> pt = binpow(len, deg / 2);
std::vector<i64> wle(len);
auto D = [&](int i, int j) {
if (i % 2 == 0) {
return pt[j - i] % P;
} else {
return (j - i - 1 >= 0 ? pt[j - i - 1] % P : 0);
}
};
for (int j = 0; j < len; j++) {
for (int i = 0; i <= j; i++) {
wle[j] = add(wle[j], mul(D(0, i), D(i, j)));
}
}
if (deg & 1) {
std::vector<i64> wle2(len);
for (int j = 0; j < len; j++) {
for (int i = 0; i <= j; i++) {
if (i != j || j % 2 == 0) {
wle2[j] = add(wle2[j], wle[i]);
}
}
}
return wle2;
}
return wle;
}
}
void st(std::vector<i64> &dp, std::vector<i64> &ll, std::vector<i64> &rr, std::vector<i64> &fl, i64 z) {
std::vector<i64> dp2(dp.size());
for (int i = 0; i < (int)dp.size(); i++) {
for (int j = 0; j < i + (fl[i] == 1); j++) {
if (ll[i] <= z && z <= rr[i] && ll[j] <= z - 1 && z - 1 <= rr[j]) {
dp2[i] = add(dp2[i], dp[j]);
}
}
}
dp = dp2;
}
int solve(std::vector<i64> v) {
int sz = (int)v.size() - 1;
std::vector<i64> ll(sz);
std::vector<i64> rr(sz);
std::vector<i64> fl(sz, 1);
for (int i = 0; i < sz; i++) {
ll[i] = v[i];
rr[i] = v[i + 1];
if (i) {
if (ll[i] < rr[i]) {
++ll[i];
} else {
--ll[i];
}
}
if (ll[i] > rr[i]) {
std::swap(ll[i], rr[i]);
fl[i] = -1;
}
}
std::vector<i64> dp(sz);
std::sort(v.begin(), v.end());
i64 fr = v[0];
i64 to = v.back();
for (int i = 0; i < sz; i++) {
if (ll[i] <= fr && fr <= rr[i]) {
dp[i] = 1;
}
}
i64 lst = fr;
for (auto p : v) {
i64 z = p - 2;
if (lst < z) {
i64 d = z - lst;
std::vector<int> act(sz);
for (int i = 0; i < sz; i++) {
for (int j = 0; j < i + (fl[i] == 1); j++) {
if (ll[i] <= z && z <= rr[i] && ll[j] <= z && z <= rr[j]) {
act[i] = act[j] = 1;
}
}
}
int cnt = 0;
std::vector<int> lk(sz);
for (int i = 0; i < sz; i++) {
if (act[i]) {
lk[i] = cnt++;
}
}
std::vector<i64> fst = binpow(cnt, d);
std::vector<i64> dp2(sz);
auto D = [&](int i, int j) {
if (i % 2 == 0) {
return fst[j - i] % P;
} else {
return (j - i - 1 >= 0 ? fst[j - i - 1] % P : 0);
}
};
for (int i = 0; i < sz; i++) {
for (int j = 0; j <= i; j++) {
dp2[i] = add(dp2[i], mul(dp[j], D(lk[j], lk[i])));
}
}
dp = dp2;
lst = z;
}
z = p - 1;
if (lst < z) {
st(dp, ll, rr, fl, z);
lst = z;
}
z = p;
if (lst < z) {
st(dp, ll, rr, fl, z);
lst = z;
}
z = p + 1;
if (z <= to && lst < z) {
st(dp, ll, rr, fl, z);
lst = z;
}
}
int res = 0;
for (int i = 0; i < sz; i++) {
if (ll[i] <= to && to <= rr[i]) {
res = add(res, dp[i]);
}
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, t;
std::cin >> n >> t;
auto sgn = [&](i64 x) {
if (x > 0) {
return 1;
} else if (x < 0) {
return -1;
} else {
return 0;
}
};
std::vector<i64> a;
for (int i = 0; i < n; i++) {
int x;
std::cin >> x;
if (x == 0) {
continue;
}
if (a.empty()) {
a.emplace_back(x);
continue;
}
if (sgn(x) == sgn(a.back())) {
a.back() += x;
} else {
a.emplace_back(x);
}
}
n = (int)a.size();
std::vector<i64> s = {1};
for (int i = 0; i < n; i++) {
s.emplace_back(s.back() + a[i]);
}
i64 maxlen = 1;
i64 mn = 1e18;
for (int i = 0; i < (int)s.size(); i++) {
mn = std::min(mn, s[i]);
maxlen = std::max(maxlen, s[i] - mn + 1);
}
std::cout << maxlen << " ";
if (maxlen == 1) {
int ans = 1;
for (int i = 0; i < (int)a.size(); i++) {
ans = add(ans, std::abs(a[i]) % P);
}
std::cout << ans << "\n";
} else {
n = (int)s.size();
int ans = 0;
for (int i = 0; i < n; i++) {
int id = -1;
for (int j = i + 1; j < n; j++) {
if (s[j] - s[i] + 1 == maxlen) {
id = j;
}
}
if (id == -1) {
continue;
} else {
std::vector<i64> ii;
for (int j = i; j <= id; j++) {
ii.emplace_back(s[j]);
}
i = id - 1;
ans = add(ans, solve(ii));
}
}
std::cout << ans << "\n";
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const double PI = acos(-1.0);
const int MAXN = 1e6 + 5;
void cp() {
int n;
cin >> n;
vector<int> arr(n);
for (int &x : arr) cin >> x;
int i = 0, j = 0;
long long int ans = 1;
while (i < n && j + 1 < n) {
j++;
if (arr[i] != arr[j]) i = j;
ans += j - i + 1;
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
long long n, m;
long long l, r, p;
int d[N][10];
int num[N];
long long ans;
void add(long long &ans, long long v) {
v = (v % p + p) % p;
ans = (ans + v) % p;
}
void dfs(long long st, long long v, long long lo, long long ro, long long now,
long long &c, long long &sm, int fg) {
for (int i = st + 1; i < num[v]; i++) {
long long tmp = now * d[v][i];
long long ll = lo / tmp + (lo % tmp ? 1 : 0);
long long rr = ro / tmp;
c += (rr - ll + 1) * fg;
add(sm, ((rr - ll + 1) * (rr + ll) / 2 % p * tmp % p) * fg);
dfs(i, v, lo, ro, tmp, c, sm, -1 * fg);
}
}
void solve(long long v, long long lo, long long ro) {
if (ro < lo) return;
long long sm = (long long)(ro - lo + 1) * (ro + lo) / 2 % p;
long long c = ro - lo + 1;
dfs(-1, v, lo, ro, 1, c, sm, -1);
add(ans, (long long)(n - v + 1) * ((long long)(m + 1) % p * c % p - sm));
}
int main() {
while (cin >> n >> m) {
cin >> l >> r >> p;
memset(num, 0, sizeof(num));
for (int i = 2; i <= n; i++) {
if (num[i] == 0) {
for (int j = i; j <= n; j += i) {
d[j][num[j]++] = i;
}
}
}
long long lo = l, ro = r;
ans = 0;
for (long long i = 1; i <= n; i++) {
while (l * l - (long long)i * i <= (lo - 1) * (lo - 1) && lo > 1) lo--;
while (r * r - (long long)i * i < (ro) * (ro) && ro >= 1) ro--;
solve(i, lo, min(m, ro));
}
if (l <= 1)
cout << (ans * 2 % p + (long long)n * m % p * 2 + n + m) % p << endl;
else
cout << ans * 2 % p << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(long long a, long long b) { return a > b; }
long long expo(long long a, long long b, long long m) {
if (b == 0) return 1;
if (b == 1) return a;
long long val = (expo(a, b / 2, m) * expo(a, b / 2, m)) % m;
if (b & 1)
return (val * a) % m;
else
return val;
}
long long mmi(long long a, long long m) { return expo(a, m - 2, m); }
long long a[200001], b[200001];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, q;
cin >> n >> q;
map<long long, long long> m;
for (long long i = 0; i < n; i++) a[i] = 0;
while (q--) {
char ch;
long long i;
cin >> ch >> i;
if (ch == '+') {
if (a[i]) {
cout << "Already on\n";
continue;
}
long long cond = -1;
for (long long j = 1; j < (sqrt(i) + 1); j++) {
if (i % j == 0) {
if (j != 1 && m.find(j) != m.end()) {
cond = m[j];
break;
}
long long p = i / j;
if (p != 1 && m.find(p) != m.end()) {
cond = m[p];
break;
}
}
}
if (cond == -1) {
for (long long j = 2; j < sqrt(i) + 1; j++) {
if (i % j == 0) {
m[j] = i;
long long p = i / j;
m[p] = i;
}
}
m[i] = i;
a[i] = 1;
cout << "Success\n";
} else {
cout << "Conflict with " << cond << "\n";
}
} else {
if (a[i]) {
for (long long j = 1; j < sqrt(i) + 1; j++) {
if (i % j == 0) {
m.erase(j);
long long p = i / j;
m.erase(p);
}
}
cout << "Success\n";
a[i] = 0;
} else {
cout << "Already off\n";
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
string s;
cin >> s;
vector<pair<int, int>> vec;
vec.push_back({0, 0});
int x = 0, y = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'U') y++;
if (s[i] == 'D') y--;
if (s[i] == 'L') x--;
if (s[i] == 'R') x++;
vec.push_back({x, y});
}
bool fl = 0;
for (pair<int, int> v : vec) {
int f = abs(a - v.first), g = abs(b - v.second);
if (f == g && f == 0) {
fl = 1;
break;
}
if (x == 0 && y == 0) continue;
int z;
if (x != 0)
z = abs(f / x);
else
z = abs(g / y);
v.first += z * x;
v.second += z * y;
if (v.first == a && v.second == b) fl = 1;
}
if (fl)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 1000;
int n;
int a[MAXN + 1];
int d[MAXN];
int ret[MAXN], nret;
void solve() {
for (int i = (0); i < (n); ++i) d[i] = a[i + 1] - a[i];
nret = 0;
for (int per = (1); per <= (n); ++per) {
bool ok = true;
for (int i = per; i < n; ++i)
if (d[i] != d[i - per]) {
ok = false;
break;
}
if (ok) ret[nret++] = per;
}
}
void run() {
scanf("%d", &n);
a[0] = 0;
for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
solve();
printf("%d\n", nret);
for (int i = (0); i < (nret); ++i) {
if (i != 0) printf(" ");
printf("%d", ret[i]);
}
puts("");
}
int main() {
run();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100007;
const long long inf = 1e18;
long long sum[nmax], sol, N, sud[nmax], a[nmax];
inline long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
int main() {
long long i, j;
cin >> N;
for (i = 1; i <= N; ++i) cin >> a[i];
sort(a + 1, a + N + 1);
for (i = 1; i <= N; ++i) sum[i] = sum[i - 1] + a[i];
for (i = N; i; --i) sud[i] = sud[i + 1] + a[i];
for (i = 1; i <= N; ++i) {
sol += 2 * (i * a[i] - sum[i - 1]);
sol += 2 * (sud[i + 1] - (N - i) * a[i]);
}
N *= 2;
i = gcd(sol, N);
cout << sol / i << ' ' << N / i << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
const long long INF = 1e18 + 1;
using namespace std;
signed main() {
std::ios::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
array<long long, 2> f = {-1, -1}, l;
for (long long i = 0; i < (n); i++) {
for (long long j = (0); j < (m); j++) {
char c;
cin >> c;
if (c == 'B') {
if (f[0] == -1) f = {i, j};
l = {i, j};
}
}
}
cout << (f[0] + l[0]) / 2 + 1 << " " << (f[1] + l[1]) / 2 + 1 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
struct Line {
long long k, b;
Line(long long k = 0, long long b = std::numeric_limits<long long>::min())
: k(k), b(b) {}
};
long long f(const Line &line, int x) { return line.k * x + line.b; }
struct Node {
Node *lc, *rc;
Line line;
Node() : lc(nullptr), rc(nullptr) {}
};
Node *rt;
void addLine(Node *&p, int l, int r, Line line) {
if (p == nullptr) p = new Node;
int m = (l + r) / 2;
bool fm = f(line, m) > f(p->line, m);
bool fl = f(line, l) > f(p->line, l);
if (fm) std::swap(p->line, line);
if (r - l == 1) {
return;
} else if (fl != fm) {
addLine(p->lc, l, m, line);
} else {
addLine(p->rc, m, r, line);
}
}
long long queryMax(Node *p, int l, int r, int x) {
if (p == nullptr) return std::numeric_limits<long long>::min();
int m = (l + r) / 2;
long long res = f(p->line, x);
if (x < m) {
res = std::max(res, queryMax(p->lc, l, m, x));
} else {
res = std::max(res, queryMax(p->rc, m, r, x));
}
return res;
}
int n;
std::vector<std::vector<int>> e;
std::vector<int> a, sz;
std::vector<bool> vis;
long long ans;
void dfsSz(int u, int p) {
sz[u] = 1;
for (int v : e[u]) {
if (vis[v] || v == p) continue;
dfsSz(v, u);
sz[u] += sz[v];
}
}
int findCen(int u, int p, int tot) {
for (int v : e[u]) {
if (vis[v] || v == p) continue;
if (2 * sz[v] > tot) return findCen(v, u, tot);
}
return u;
}
void dfsQuery(int u, int p, int d, long long s, long long f) {
++d;
s += a[u];
f += s;
ans = std::max(ans, queryMax(rt, 0, n, d) + f);
for (int v : e[u]) {
if (vis[v] || v == p) continue;
dfsQuery(v, u, d, s, f);
}
}
void dfsUpdate(int u, int p, int d, long long s, long long f) {
++d;
s += a[u];
f += 1LL * d * a[u];
addLine(rt, 0, n, Line(s, f));
for (int v : e[u]) {
if (vis[v] || v == p) continue;
dfsUpdate(v, u, d, s, f);
}
}
void work(int u) {
dfsSz(u, -1);
u = findCen(u, -1, sz[u]);
vis[u] = true;
rt = nullptr;
addLine(rt, 0, n, Line(a[u], a[u]));
for (int v : e[u]) {
if (vis[v]) continue;
dfsQuery(v, -1, 0, 0, 0);
dfsUpdate(v, -1, 1, a[u], a[u]);
}
std::reverse(e[u].begin(), e[u].end());
rt = nullptr;
for (int v : e[u]) {
if (vis[v]) continue;
dfsQuery(v, -1, 0, 0, 0);
dfsUpdate(v, -1, 1, a[u], a[u]);
}
ans = std::max(ans, queryMax(rt, 0, n, 0));
for (int v : e[u])
if (!vis[v]) work(v);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
e.resize(n);
a.resize(n);
sz.resize(n);
vis.resize(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
std::cin >> u >> v;
--u;
--v;
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = 0; i < n; ++i) std::cin >> a[i];
work(0);
std::cout << ans << "\n";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1999999999;
const double pi = acos(-1.0);
const double eps = 1e-10;
char gc() {
char c;
while (isspace(c = getchar()))
;
return c;
}
int gs(char* s) {
gets(s);
int l = strlen(s);
while (l && isspace(s[l - 1])) s[--l] = 0;
return l;
}
int m[100000];
vector<int> a[100000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
t--;
a[t].push_back(i);
}
int c = 1;
for (int i = 99999; i >= 0; i--) {
while (((int)(a[i]).size())) {
for (int j = i; j >= 0; j--) {
if (((int)(a[j]).size())) {
m[a[j].back()] = c;
a[j].pop_back();
} else {
cout << -1;
return 0;
}
}
c++;
}
}
cout << c - 1 << ('\n');
for (int i = 0; i < n; i++) cout << m[i] << ' ';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int MAXN = 100005;
int n, h[MAXN];
int a[MAXN];
map<int, int> M;
void solve() {
for (int i = 0; i < n; i++) {
a[i] = h[i];
}
sort(a, a + n);
int ans = 0;
for (int i = 0; i < n; i++) {
M[h[i]]++;
if (M[h[i]] == 0) {
M.erase(h[i]);
}
M[a[i]]--;
if (M[a[i]] == 0) {
M.erase(a[i]);
}
ans += M.empty();
}
printf("%d\n", ans);
}
int main() {
while (scanf("%d", &n) == 1) {
for (int i = 0; i < n; i++) {
scanf("%d", h + i);
}
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int u[40005], v[40005], d[40005], balance[205];
bool skip[40005];
set<int> e[205];
void rmedge(int i) {
e[u[i]].erase(i);
e[v[i]].erase(i);
}
void roam(int x) {
while (1) {
if (e[x].size() == 0) return;
int i = *e[x].begin();
bool dir = u[i] == x;
rmedge(i);
int y = dir ? v[i] : u[i];
d[i] = dir;
x = y;
}
}
bool vis[205];
void dfs(int x, basic_string<int>& w) {
vis[x] = 1;
w += x;
for (int y : e[x]) {
if (!vis[y]) {
dfs(y, w);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int tcc;
cin >> tcc;
while (tcc--) {
cin >> n >> m;
for (int i = 0; i < 205; i++) {
e[i] = {};
balance[i] = 0;
}
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i];
e[u[i]].insert(i);
e[v[i]].insert(i);
skip[i] = false;
}
basic_string<int> odd;
for (int i = 1; i <= n; i++)
if (e[i].size() % 2) odd += i;
for (int i = 0; i < (int)odd.size(); i += 2) {
int x = odd[i];
int y = odd[i + 1];
u[m] = x;
v[m] = y;
e[x].insert(m);
e[y].insert(m);
skip[m] = true;
m++;
}
for (int i = 1; i <= n; i++) {
int x = i;
int y = i == n ? 1 : i + 1;
u[m] = x;
v[m] = y;
e[x].insert(m);
e[y].insert(m);
skip[m] = true;
m++;
}
roam(1);
for (int i = 0; i < m; i++) {
if (skip[i]) continue;
if (d[i]) swap(u[i], v[i]);
balance[u[i]]++;
balance[v[i]]--;
}
cout << count(balance + 1, balance + n + 1, 0) << '\n';
for (int i = 0; i < m; i++)
if (!skip[i]) cout << u[i] << ' ' << v[i] << '\n';
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
double solve(double x, double a) {
double x0 = 0;
bool isneg = (x <= 0);
if (isneg) x = a - x;
int q = (x / a);
double r = x - q * a;
x = r;
if ((q % 2) == 0) {
x0 = x;
} else {
x0 = a - x;
}
if (isneg) x0 = a - x0;
return x0;
}
bool eq(double a, double b) { return abs(a - b) <= 1E-10; }
int main() {
double a, b, m;
cin >> a >> b >> m;
double vx, vy, vz;
cin >> vx >> vy >> vz;
double sx = a / 2, sy = m, sz = 0;
double t = (-m) / vy;
double x = sx + t * vx;
double y = sy + t * vy;
double z = sz + t * vz;
assert(eq(y, 0));
double x0, z0;
{
x0 = solve(x, a);
z0 = solve(z, b);
}
{}
cout << fixed << setprecision(10) << x0 << " " << z0 << endl;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
int main() {
long long a[105], b[100], sum = 0, i, nod, j = 0, r, n, m, k = 0, x, max = 0,
y, t;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
nod = a[0];
for (i = 1; i < n; i++) {
nod = gcd(nod, a[i]);
}
if ((a[n - 1] / nod - n) % 2 == 1)
cout << "Alice\n";
else
cout << "Bob\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T inline SQR(const T &a) {
return a * a;
}
long long MOD = 1000000007LL;
long long mypow(long long a, long long p) {
if (p == 0) return 1LL;
long long ans = mypow(a, p / 2);
ans *= ans;
ans %= MOD;
if (p & 1) return (a * ans) % MOD;
return ans;
}
int n, m, k;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> k;
int MAXS = max(n, m) + 1;
int dp[k + 1][MAXS];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < MAXS; ++i) dp[0][i] = 1;
for (int i = 1; i < k + 1; ++i) {
long long aux = 0, sum = 0;
for (int j = 3; j < MAXS; ++j) {
aux += dp[i - 1][j - 2];
aux %= MOD;
sum += aux;
sum %= MOD;
dp[i][j] = sum;
}
}
cout << (1ll * dp[k][n] * dp[k][m]) % MOD << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int a[11], d[8];
int main() {
string s;
cin >> s;
int n = s.length();
for (int i = 0; i < n; i++) {
a[s[i] - '0']++;
}
for (int mod = 0; mod < 7; mod++) {
int b[] = {1, 6, 8, 9};
do {
int x = mod, y = 0;
for (int i = 0; i < 4; i++) {
x = x * 10 + b[i];
y = y * 10 + b[i];
}
if (x % 7 == 0) d[mod] = y;
} while (next_permutation(b, b + 4));
}
a[1]--;
a[6]--;
a[8]--;
a[9]--;
int rem = 0;
for (int i = 1; i <= 9; i++) {
while (a[i] > 0) {
cout << i;
rem = rem * 10 + i;
rem %= 7;
a[i]--;
}
}
cout << d[rem];
while (a[0] > 0) {
cout << 0;
a[0]--;
}
}
| 4 |
#include <bits/stdc++.h>
#define ll long long
#define lsb(x) x & -x
using namespace std;
const int MOD = 998244353;
int raise(int n, int p) {
int ans = 1;
while(p) {
if(p % 2)
ans = (1LL * ans * n) % MOD;
n = (1LL * n * n) % MOD;
p /= 2;
}
return ans;
}
int invMod(int x) {
return raise(x, MOD - 2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int ntests;
cin >> ntests;
while(ntests --) {
int n;
string s;
cin >> n >> s;
n ++;
vector<int> fact(n + 1, 1);
for(int i = 1; i <= n; i ++)
fact[i] = (1LL * fact[i - 1] * i) % MOD;
auto comb = [&](int n, int k) -> int {
if(n < k)
return 0;
return (1LL * ((1LL * fact[n] * invMod(fact[n - k])) % MOD) * invMod(fact[k])) % MOD;
};
int sz = 1, cnt = 0;
for(int i = 0; i < s.size(); i ++) {
if (i + 1 < n && s[i] == '1' && s[i + 1] == '1') {
i ++;
cnt ++;
} else if(s[i] == '0') {
sz ++;
}
}
cout << comb(cnt + sz - 1, sz - 1) << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
int a, b, n, x, y, z, w, j, i, e = 0, t, k;
scanf("%d %d %d", &a, &b, &n);
x = a;
w = b;
if (a < 0) x = a * -1;
if (b < 0) {
w = b * -1;
y = w;
}
y = w;
for (i = 0; i <= y; i++) {
z = 1;
if (i == 0)
z = 0;
else
for (k = 1; k <= n; k++) z = z * i;
t = z * x;
if (t == y) {
j = i;
e = 1;
break;
}
}
if (a == 0 && b == 0)
printf("%d\n", n / 2);
else if (e == 0)
printf("No solution\n");
else if (a < 0 && b > 0)
printf("%d\n", j * -1);
else if (a > 0 && b < 0)
printf("%d\n", j * -1);
else
printf("%d\n", j);
}
| 3 |
#include <bits/stdc++.h>
int pos[100010][2], n;
int compare(const void *x, const void *y) {
int *a = (int *)x;
int *b = (int *)y;
return abs(a[0]) + abs(a[1]) - abs(b[0]) - abs(b[1]);
}
int main(void) {
int i, ans;
scanf("%d", &n);
ans = 2 * n;
for (i = 0; i < n; i++) {
scanf("%d %d", &pos[i][0], &pos[i][1]);
if (pos[i][0] == 0 || pos[i][1] == 0)
ans += 2;
else
ans += 4;
}
qsort(pos, n, sizeof(int) * 2, compare);
printf("%d\n", ans);
for (i = 0; i < n; i++) {
if (pos[i][0] == 0 && pos[i][1] < 0)
printf("1 %d D\n2\n1 %d U\n3\n", abs(pos[i][1]), abs(pos[i][1]));
else if (pos[i][0] == 0 && pos[i][1] > 0)
printf("1 %d U\n2\n1 %d D\n3\n", pos[i][1], pos[i][1]);
else if (pos[i][0] < 0 && pos[i][1] == 0)
printf("1 %d L\n2\n1 %d R\n3\n", abs(pos[i][0]), abs(pos[i][0]));
else if (pos[i][0] > 0 && pos[i][1] == 0)
printf("1 %d R\n2\n1 %d L\n3\n", pos[i][0], pos[i][0]);
else if (pos[i][0] > 0 && pos[i][1] > 0)
printf("1 %d R\n1 %d U\n2\n1 %d L\n1 %d D\n3\n", pos[i][0], pos[i][1],
pos[i][0], pos[i][1]);
else if (pos[i][0] < 0 && pos[i][1] < 0)
printf("1 %d L\n1 %d D\n2\n1 %d R\n1 %d U\n3\n", -1 * pos[i][0],
-1 * pos[i][1], -1 * pos[i][0], -1 * pos[i][1]);
else if (pos[i][0] > 0 && pos[i][1] < 0)
printf("1 %d R\n1 %d D\n2\n1 %d L\n1 %d U\n3\n", pos[i][0],
pos[i][1] * -1, pos[i][0], -1 * pos[i][1]);
else if (pos[i][0] < 0 && pos[i][1] > 0)
printf("1 %d L\n1 %d U\n2\n1 %d R\n1 %d D\n3\n", -1 * pos[i][0],
pos[i][1], -1 * pos[i][0], pos[i][1]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, x, trf[26], inv[26];
bool vis[26];
char s[N], a[N], b[N];
void solve() {
scanf("%d%s%s%s", &x, s + 1, a + 1, b + 1);
n = strlen(s + 1);
for (int i = 0; i < x; i++) {
trf[i] = -1;
inv[i] = -1;
vis[i] = 0;
}
bool X = false;
for (int i = 1; i <= n; i++) {
int I = s[i] - 'a', J = a[i] - 'a';
if (X) {
if (trf[I] == -1) {
for (int j = 0; j < x; j++) {
if (inv[j] == -1) {
trf[I] = j;
inv[j] = I;
break;
}
}
}
} else if (trf[I] != -1) {
if (trf[I] >= J) {
if (trf[I] > J) X = true;
continue;
}
X = true;
vector<pair<int, int> > V;
for (int j = 1; j <= i; j++) {
int K = s[j] - 'a';
if (!vis[trf[K]]) {
V.push_back({K, j});
vis[trf[K]] = true;
}
}
int C;
while (!V.empty()) {
int A, B;
tie(A, B) = V.back();
V.pop_back();
int j;
for (j = trf[A] + 1; j < x; j++) {
if (!vis[j]) break;
}
if (j == x) {
vis[trf[A]] = 0;
inv[trf[A]] = -1;
trf[A] = -1;
} else {
inv[trf[A]] = -1;
trf[A] = -1;
trf[A] = j;
inv[trf[A]] = A;
i = B;
break;
}
}
} else {
if (inv[J] != -1) {
for (int j = J + 1; j < x; j++) {
if (inv[j] == -1) {
trf[I] = j;
inv[j] = I;
X = true;
break;
}
}
if (X) continue;
X = true;
vector<pair<int, int> > V;
for (int j = 1; j < i; j++) {
int K = s[j] - 'a';
if (!vis[trf[K]]) {
V.push_back({K, j});
vis[trf[K]] = true;
}
}
int C;
while (!V.empty()) {
int A, B;
tie(A, B) = V.back();
V.pop_back();
int j;
for (j = trf[A] + 1; j < x; j++) {
if (!vis[j]) break;
}
if (j == x) {
vis[trf[A]] = 0;
inv[trf[A]] = -1;
trf[A] = -1;
} else {
inv[trf[A]] = -1;
trf[A] = -1;
trf[A] = j;
inv[trf[A]] = A;
i = B;
break;
}
}
} else {
trf[I] = J;
inv[J] = I;
}
}
}
string S, A, B;
for (int i = 1; i <= n; i++) {
if (trf[s[i] - 'a'] == -1) {
puts("NO");
return;
}
s[i] = trf[s[i] - 'a'] + 'a';
S += (char)s[i];
A += (char)a[i];
B += (char)b[i];
}
if (S < A || S > B) {
puts("NO");
return;
}
puts("YES");
for (int i = 0; i < x; i++) {
if (trf[i] == -1) {
for (int j = 0; j < x; j++) {
if (inv[j] != -1) continue;
trf[i] = j;
inv[j] = i;
break;
}
}
putchar(trf[i] + 'a');
}
puts("");
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) solve();
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
scanf("%lld", &n);
long long int a[2 * n], i;
for (i = 0; i < 2 * n; i++) {
scanf("%lld", &a[i]);
}
sort(a, a + 2 * n);
if (a[n - 1] < a[n]) {
printf("YES\n");
} else {
printf("NO\n");
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> T, L;
vector<int> x, y;
int l, size;
void Stworz(int n) {
int p = 1;
while (p < n) p *= 2;
l = p;
size = 2 * p - 1;
for (int i = 0; i <= size; ++i) {
T.push_back(1);
L.push_back(0);
x.push_back(0);
y.push_back(0);
}
for (int i = l; i <= size; ++i) x[i] = 1;
for (int i = l - 1; i >= 1; --i) x[i] = 2 * x[2 * i];
}
void Propaguj(int akt) {
T[2 * akt] += L[akt];
L[2 * akt] += L[akt];
T[2 * akt + 1] += L[akt];
L[2 * akt + 1] += L[akt];
L[akt] = 0;
}
void Dodaj(int akt, int Le, int Pr, int le, int pr, int dod) {
if (Le == le && Pr == pr) {
T[akt] += dod;
L[akt] += dod;
} else {
Propaguj(akt);
int Sr = Le + (Pr - Le) / 2;
if (le <= Sr) Dodaj(2 * akt, Le, Sr, le, min(pr, Sr), dod);
if (pr >= Sr + 1) Dodaj(2 * akt + 1, Sr + 1, Pr, max(le, Sr + 1), pr, dod);
T[akt] = min(T[2 * akt], T[2 * akt + 1]);
x[akt] = 0;
if (T[2 * akt] == T[akt]) x[akt] += x[2 * akt];
if (T[2 * akt + 1] == T[akt]) x[akt] += x[2 * akt + 1];
y[akt] = 0;
if (T[2 * akt] == T[akt]) y[akt] += y[2 * akt];
if (T[2 * akt] == T[akt] + 1) y[akt] += x[2 * akt];
if (T[2 * akt + 1] == T[akt]) y[akt] += y[2 * akt + 1];
if (T[2 * akt + 1] == T[akt] + 1) y[akt] += x[2 * akt + 1];
}
}
int Ile(int akt, int Le, int Pr, int le, int pr) {
if (Le == le && Pr == pr) {
if (T[akt] == 1) return x[akt] + y[akt];
if (T[akt] == 2) return x[akt];
return 0;
}
Propaguj(akt);
int wyn = 0;
int Sr = Le + (Pr - Le) / 2;
if (le <= Sr) wyn += Ile(2 * akt, Le, Sr, le, min(pr, Sr));
if (pr >= Sr + 1) wyn += Ile(2 * akt + 1, Sr + 1, Pr, max(le, Sr + 1), pr);
return wyn;
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
if (n == 1) {
cout << "0";
return 0;
}
vector<int> p(n), gdzie(n);
for (int i = 0; i < n; ++i) {
cin >> p[i];
--p[i];
gdzie[p[i]] = i;
}
vector<int> start(n, 0);
start[0] = 1;
for (int i = 1; i < n; ++i) {
int ilSa = 0;
if (gdzie[i] >= 1 && p[gdzie[i] - 1] < i) ++ilSa;
if (gdzie[i] < n - 1 && p[gdzie[i] + 1] < i) ++ilSa;
start[i] = start[i - 1] + (1 - ilSa);
}
Stworz(n);
for (int i = 0; i < n; ++i) Dodaj(1, 0, size - l, i, i, start[i] - 1);
long long odp = Ile(1, 0, size - l, 1, n - 1);
for (int i = 1; i < n - 1; ++i) {
vector<int> zmi;
if (gdzie[i - 1] >= 1 && p[gdzie[i - 1] - 1] > i - 1)
zmi.push_back(p[gdzie[i - 1] - 1]);
if (gdzie[i - 1] < n - 1 && p[gdzie[i - 1] + 1] > i - 1)
zmi.push_back(p[gdzie[i - 1] + 1]);
if (zmi.size() == 2 && zmi[0] > zmi[1]) swap(zmi[0], zmi[1]);
if (zmi.size() == 0) Dodaj(1, 0, size - l, i + 1, n - 1, -1);
if (zmi.size() == 1) {
if (zmi[0] - 1 >= i + 1) Dodaj(1, 0, size - l, i + 1, zmi[0] - 1, -1);
}
if (zmi.size() == 2) {
if (zmi[0] - 1 >= i + 1) Dodaj(1, 0, size - l, i + 1, zmi[0] - 1, -1);
Dodaj(1, 0, size - l, max(zmi[1], i + 1), n - 1, 1);
}
odp += Ile(1, 0, size - l, i + 1, n - 1);
}
cout << odp;
return 0;
}
| 10 |
#include <bits/stdc++.h>
typedef double D;
typedef long long int LL;
typedef long double LD;
#define OR ||
#define AND &&
#define nl '\n'
#define ff first
#define ss second
#define S string
#define inf INT_MAX
#define SQR(a) (a) * (a)
#define pb push_back
#define GCD(a, b) __gcd(a, b)
#define PI 2.0*acos(0.0)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define LCM(a, b) (a * b) / GCD(a, b)
#define mem(a,b) memset(a,b,sizeof(a))
#define srtv(v) sort(v.begin(),v.end())
#define T int t; cin>>t; while(t--)
#define Rep(i,a,b) for(int i = a; i <= b; i++)
#define rep(i,a,b) for(int i = a; i >= b; i--)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define inout freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define si(x) scanf("%d",&x)
#define pi(x) printf("%d",x)
#define sss(str) scanf("%s",str)
#define pl(x) printf("%lld",x)
#define sl(x) scanf("%lld",&x)
#define sii(x,y) scanf("%d %d",&x,&y)
#define sll(x,y) scanf("%lld %lld",&x,&y)
#define siii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
using namespace std;
/*
int bs(int l,int r,int n)
{
int mid,res=inf;
while(l<=r)
{
mid=l+(r-l)/2;
if(check(mid,n))
{
r=mid-1;
res=min(res,mid);
}
else
l=mid+1;
}
return res;
}
*/
//--------------------------code starts here---------------------------------------
int a[200005],b[200005];
LL sum[200005];
int bs(int l,int r,int val)
{
int mid,res=-1;
while(l<=r)
{
mid=l+(r-l)/2;
if(b[mid]<=val)
{
l=mid+1;
res=max(res,mid);
}
else
r=mid-1;
}
return res;
}
int main()
{
int t;
t=1;
si(t);
while(t--)
{
int n,x;
si(n);
Rep(i,1,n)
{
si(a[i]);
b[i]=a[i];
}
sort(b+1,b+n+1);
Rep(i,1,n)
{
sum[i]=b[i];
sum[i]+=sum[i-1];
}
b[n+1]=b[n];
int ind=n;
rep(i,n-1,1)
{
int srch=bs(1,n,b[i]);
if(sum[srch]>=b[ind])
{
ind=i;
}
else
{
break;
}
}
printf("%d\n",n-ind+1);
Rep(i,1,n)
{
if(a[i]>=b[ind])
printf("%d ",i);
}
printf("\n");
}
return 0;
} | 3 |
#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
size_t size;
std::cin >> size;
std::string moves;
std::cin >> moves;
size_t lenght = 0;
for (size_t i = 0; i < moves.size(); ++i, ++lenght) {
if (i + 1 < moves.size()) {
if (moves[i] == 'R' && moves[i + 1] == 'U') {
++i;
continue;
}
if (moves[i] == 'U' && moves[i + 1] == 'R') {
++i;
continue;
}
}
}
std::cout << lenght << '\n';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int cnt[100005], h[15], memo[5005][505];
int DP(int rem, int group) {
if (group == 0 || rem == 0) {
return 0;
} else if (memo[rem][group] != -1) {
return memo[rem][group];
} else {
int ret = 0;
for (int i = 1; i <= min(k, rem); i++) {
ret = max(ret, DP(rem - i, group - 1) + h[i]);
}
return memo[rem][group] = ret;
}
}
int main() {
int ans = 0;
map<int, int> mp;
scanf("%d %d", &n, &k);
for (int i = 0, in; i < n * k; i++) {
scanf("%d", &in);
cnt[in]++;
}
for (int i = 0, in; i < n; i++) {
scanf("%d", &in);
mp[in]++;
}
for (int i = 1; i <= k; i++) {
scanf("%d", h + i);
}
for (auto it = mp.begin(); it != mp.end(); it++) {
memset(memo, -1, sizeof(memo));
ans += DP(min(cnt[it->first], it->second * k), it->second);
}
printf("%d", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
char s[maxn], ss[maxn];
int nxt[maxn], r[maxn], f[maxn], g[maxn];
signed main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
s[n + 1] = '#';
int mx = 0, id = 0;
for (int i = 1; i <= n; i++) {
if (mx > i)
r[i] = min(r[2 * id - i], mx - i);
else
r[i] = 1;
while (s[i + r[i]] == s[i - r[i]]) r[i]++;
if (r[i] + i > mx) mx = r[i] + i, id = i;
}
for (int i = 1; i <= n; i++) ss[i] = s[n - i + 1];
nxt[0] = nxt[1] = 0;
for (int i = 2, j = 0; i <= n; i++) {
while (j && ss[i] != ss[j + 1]) j = nxt[j];
if (ss[i] == ss[j + 1]) nxt[i] = ++j;
}
for (int i = 1, j = 0; i <= n; i++) {
while (j && s[i] != ss[j + 1]) j = nxt[j];
if (s[i] == ss[j + 1]) f[i] = ++j;
if (f[i] == n) j = nxt[j];
if (f[i]) g[i] = i - f[i] + 1;
}
for (int i = 1; i <= n; i++)
if (f[i - 1] > f[i]) f[i] = f[i - 1], g[i] = g[i - 1];
bool ok = false;
int ans = 0, pos = 0;
for (int i = 1; i <= n; i++)
if (2 * r[i] - 1 > ans) ans = 2 * r[i] - 1, pos = i - r[i] + 1;
for (int i = 1; i <= n; i++) {
int t = i - r[i];
if (2 * min(f[t], n - (i + r[i] - 1)) + 2 * r[i] - 1 > ans) {
ok = true;
ans = 2 * min(f[t], n - (i + r[i] - 1)) + 2 * r[i] - 1;
pos = i;
}
}
if (!ok)
printf("1\n%d %d\n", pos, ans);
else {
printf("3\n");
printf("%d %d\n", g[pos - r[pos]], f[pos - r[pos]]);
printf("%d %d\n", pos - r[pos] + 1, 2 * r[pos] - 1);
printf("%d %d\n", n - f[pos - r[pos]] + 1, f[pos - r[pos]]);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int INF = 1000 * 1000 * 1000;
char BAD = '*';
string dirstr = "DLRU";
vector<int> dr = {1, 0, 0, -1};
vector<int> dc = {0, -1, 1, 0};
int main() {
int rows, cols, k;
cin >> rows >> cols >> k;
vector<string> tab(rows);
for (int row = 0; row < rows; row++) cin >> tab[row];
vector<vector<int>> dist(rows, vector<int>(cols, INF));
int start_row = -1, start_col = -1;
for (int row = 0; row < rows; row++)
for (int col = 0; col < cols; col++) {
if (tab[row][col] == 'X') {
start_row = row;
start_col = col;
}
}
queue<tuple<int, int, int>> q;
q.push({start_row, start_col, 0});
while (!q.empty()) {
tuple<int, int, int> cur = q.front();
q.pop();
int r = get<0>(cur), c = get<1>(cur), d = get<2>(cur);
if (dist[r][c] != INF) continue;
dist[r][c] = d;
for (int dir = 0; dir < 4; dir++) {
int nr = r + dr[dir];
int nc = c + dc[dir];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && tab[nr][nc] != BAD) {
q.push({nr, nc, d + 1});
}
}
}
int rem = k;
int r = start_row, c = start_col;
vector<int> res;
while (rem > 0) {
bool worked = false;
for (int dir = 0; dir < 4; dir++) {
int nr = r + dr[dir];
int nc = c + dc[dir];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols &&
dist[nr][nc] <= rem - 1) {
res.push_back(dir);
worked = true;
rem--;
r = nr;
c = nc;
break;
}
}
if (!worked) {
cout << "IMPOSSIBLE" << endl;
return 0;
}
}
for (int v : res) cout << dirstr[v];
cout << endl;
}
| 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.