solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
long long int summ(long long int n) {
long long int res = n * (n + 1);
res /= 2;
res %= 1000000007;
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string a;
cin >> a;
long long int i, j, n = a.size();
a = '$' + a;
long long int pre[n + 1];
pre[1] = a[1] - '0';
for (i = 2; i <= n; i++) {
pre[i] = pre[i - 1] + (a[i] - '0');
}
long long int m = 1;
long long int p = 1;
long long int ans = 0;
for (i = n; i >= 2; i--) {
long long int f = pre[i - 1] % 1000000007;
long long int tmp = (f * m) % 1000000007;
long long int tmp1 = a[i] - '0';
tmp1 %= 1000000007;
tmp1 *= (summ(i - 1));
tmp1 %= 1000000007;
ans = ans + ((tmp + tmp1) % 1000000007 * p) % 1000000007;
ans %= 1000000007;
p *= 10;
p %= 1000000007;
m++;
m %= 1000000007;
}
cout << ans << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int len = 0;
char word[1000], word2[1000], temp[1000];
scanf("%s", word);
for (int i = 0; word[i] != '\0'; i++) {
len++;
}
for (int i = 0; i < len / 2; i++) {
swap(word[i], word[len - 1 - i]);
}
scanf("%s", word2);
for (int i = 0; i < len; i++) {
if (word[i] != word2[i]) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = s.length();
int cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'N') cnt++;
}
if (cnt == 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
map<long long int, long long int> m;
vector<long long int> v, v1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, x, y, t, k;
string s;
cin >> s;
long long int sum = 0;
for (long long int i = 0; i < s.length() - 1; i++) {
if (s[i] == s[i + 1] and s[i] == 'v') {
sum++;
}
if (s[i] == 'o') {
v.push_back(sum);
}
}
long long int ans = 0;
for (long long int i = 0; i < v.size(); i++) {
ans += ((sum - v[i]) * (v[i]));
}
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
long long mx = 0, n = a.size(), m = b.size();
for (int i = m - 1; i >= 0; i--) {
int cnt = 0;
for (int j = 0; j < n && i + j < m; j++)
if (a[j] == b[i + j]) cnt++;
if (cnt > mx) mx = cnt;
}
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < m && j + i < n; j++)
if (a[i + j] == b[j]) cnt++;
if (cnt > mx) mx = cnt;
}
cout << m - mx << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[300100], s[1000100], n, k;
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
for (int i = 0; i < 1000100; i++) {
s[i] = 0;
}
for (int i = 0; i < n; i++) {
cin >> a[i];
s[a[i]]++;
}
sort(a, a + n);
for (int i = 1; i < 1000100; i++) {
s[i] += s[i - 1];
}
for (int i = a[0]; i > 0; i--) {
int t = 0;
for (int j = i; j <= a[n - 1]; j += i) {
t += s[min(j + min(k, i - 1), 1000100 - 1)] - s[j - 1];
}
if (t == n) {
cout << i;
return 0;
}
}
}
| 6 |
#include <bits/stdc++.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1000000007;
#define vi vector<int>
#define vp vector<pair<int, int>>
#define graph vector<vector<int>>
#define vs vector<string>
const int MAXN = 100005;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
{
ll x, y;
cin >> x >> y;
ll c = 0;
for (ll i = 1; i * i < x; i++)
c += max(0ll, min(y, x / i - 1) - i);
cout << c << "\n";
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
vector<char> v;
cin >> s;
int l = s.size(), i;
for (i = 0; i < l; i++) {
if (s[i] == '1' || s[i] == '2' || s[i] == '3') v.push_back(s[i]);
}
sort(v.begin(), v.end());
cout << v[0];
for (i = 1; i < v.size(); i++) cout << "+" << v[i];
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = 2 * 3;
long long ans2 = 3 * 3 * (n - 3);
for (int i = 0; i < n - 2; i++) ans *= 4;
for (int i = 0; i < n - 3; i++) ans2 *= 4;
cout << ans + ans2 << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int sum[1505][1505];
long long int dp[1505][1505];
int main() {
int n, m, i, j;
long long int t, s, ans;
while (~scanf("%d %d", &n, &m)) {
for (i = 0; i < n; i++) {
s = 0;
for (j = 0; j < m; j++) {
scanf("%I64d", &t);
s += t;
sum[i][j] = s;
}
}
for (i = 0; i < m; i++) dp[0][i] = sum[0][i];
for (i = 1; i < n; i++) {
if (i % 2 == 1) {
dp[i][m - 1] = -1e18;
t = -1e18;
for (j = m - 2; j >= 0; j--) {
t = max(dp[i - 1][j + 1], t);
dp[i][j] = t + sum[i][j];
}
} else {
dp[i][0] = -1e18;
t = -1e18;
for (j = 1; j < m; j++) {
t = max(dp[i - 1][j - 1], t);
dp[i][j] = t + sum[i][j];
}
}
}
ans = -1e18;
for (i = 0; i < m; i++) ans = max(dp[n - 1][i], ans);
printf("%I64d\n", ans);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> poli, head;
long long n, m, pishte, zaman, t, l = -1, r = 2e6 + 5, t1, t2;
string s;
int main() {
cin >> n;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == '*') poli.push_back(i);
if (s[i] == 'P') head.push_back(i);
}
while (r - l > 1) {
zaman = (r + l) / 2;
auto ithead = head.begin();
auto itpead = poli.begin();
while (ithead < head.end()) {
t = zaman;
if (itpead == poli.end()) break;
if (t < abs(*ithead - *itpead)) {
ithead++;
continue;
}
if (*ithead > *itpead) {
t1 = t;
t -= (*ithead - *itpead);
t /= 2;
if (t >= (*ithead - *itpead)) {
t1 -= 2 * (*ithead - *itpead);
itpead = upper_bound(poli.begin(), poli.end(), *ithead + t1);
} else {
itpead = upper_bound(poli.begin(), poli.end(), *ithead + t);
}
} else {
itpead = upper_bound(poli.begin(), poli.end(), *ithead + t);
}
ithead++;
}
if (itpead == poli.end())
r = zaman;
else
l = zaman;
}
cout << r;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int main() {
int n, i, j, m, s = 0, t = 0, k, d;
scanf("%d", &n);
a.resize(n + 1, 0);
for (i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
s += a[i];
}
for (i = 3; i <= n; ++i) {
if (n % i == 0) {
d = n / i;
for (j = 1; j <= d; ++j) {
t = 0;
for (k = j; k <= n; k += d) t += a[k];
s = max(t, s);
}
}
}
printf("%d\n", s);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 21;
char s[N][1000];
int n, k, f[N][N], len[N], ans = 0, a[N];
void dfs(int now, int last, int sum) {
if (now > k) {
ans = max(ans, sum);
return;
}
for (int i = last + 1; i <= n - (k - now); ++i) {
int tmp = sum;
a[now] = i;
for (int j = 1; j < now; ++j) tmp += f[a[j]][i];
dfs(now + 1, i, tmp);
}
}
int main() {
scanf("%d%d\n", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%s", s[i]), len[i] = strlen(s[i]);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j)
for (int k = 0; k < len[i] && s[i][k] == s[j][k]; ++k) ++f[i][j];
dfs(1, 0, 0);
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int pow(long long int x, long long int y, long long int m) {
long long int res = 1;
x = x % m;
while (y > 0) {
if (y & 1) res = ((res % m) * (x % m)) % m;
y = y >> 1;
x = ((x % m) * (x % m)) % m;
}
return res % m;
}
template <typename Arg1>
void ZZ(const char* name, Arg1&& arg1) {
std::cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " = " << arg1;
ZZ(comma, args...);
}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int clz(unsigned long long N) {
return N ? 64LL - __builtin_clzll(N) : -INF;
}
clock_t time_p = clock();
void abhigyan10() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
return uniform_int_distribution<long long int>(l, r)(rng);
}
const long long int mod = 1000000007;
long long int modinv(long long int x) { return pow(x, mod - 2, mod); }
long long int a[70];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
a[0] = 0;
a[1] = 1;
long long int i;
for (i = 2; i <= 66; i++) {
a[i] = i + 2LL * (a[i - 1]) - (i - 1);
}
while (t--) {
long long int ans = 0, n;
cin >> n;
while (n > 0) {
long long int j = 1, x = 0;
while (j <= n && j > 0) {
x++;
j = (1LL << x);
}
ans += a[x];
n -= (1LL << (x - 1));
}
cout << ans << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[10] = {'8', 'a', 'a', '3', '6', '9', '4', '7', '0', '5'};
int s[10] = {0, 0, 0, 1, 0, 0, 0, 1, 0, 0};
char ch[11];
int main() {
scanf("%s", ch);
int len = strlen(ch), i, j;
for (i = 0, j = len - 1; i < j; i++, j--)
if (a[ch[i] - '0'] != ch[j]) return puts("No"), 0;
if (i == j && !s[ch[i] - '0']) return puts("No"), 0;
puts("Yes");
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int tot = 0, fh = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') fh = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
tot = tot * 10 + c - '0';
c = getchar();
}
return tot * fh;
}
void getstr(char *a) {
char c = getchar();
int len = 0;
while (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9')))
c = getchar();
while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9')) {
a[++len] = c;
c = getchar();
}
}
int gcd(int x, int y) {
while (y) {
int z = x % y;
x = y;
y = z;
}
return x;
}
const int maxn = 2020;
int t, n, a[maxn], rem, tmp, b[maxn], c[maxn];
bool d[maxn];
int main() {
t = read();
while (t--) {
n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
d[i] = true;
}
tmp = 0;
rem = 1;
for (int i = 1; i <= n; i++)
if (a[i] > tmp) {
tmp = a[i];
rem = i;
}
b[1] = a[rem];
c[1] = a[rem];
d[rem] = false;
for (int i = 2; i <= n; i++) {
tmp = 0;
for (int j = 1; j <= n; j++)
if (d[j] && gcd(c[i - 1], a[j]) > tmp) {
rem = j;
tmp = gcd(c[i - 1], a[j]);
}
b[i] = a[rem];
c[i] = gcd(b[i], c[i - 1]);
d[rem] = false;
}
for (int i = 1; i <= n; i++) cout << b[i] << " ";
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int check1(string s) {
int len = s.size();
if (s[len - 1] == '.' && s[len - 2] == 'a' && s[len - 3] == 'l' &&
s[len - 4] == 'a' && s[len - 5] == 'l')
return 1;
return 0;
}
int check2(string s) {
int len = s.size();
if (s[0] == 'm' && s[1] == 'i' && s[2] == 'a' && s[3] == 'o' && s[4] == '.')
return 1;
return 0;
}
int main() {
scanf("%d", &n);
getchar();
for (int i = 1; i <= n; ++i) {
getline(cin, s);
if ((check1(s) && check2(s)) || (!check1(s) && !check2(s)))
cout << "OMG>.< I don't know!\n";
else if (check1(s))
cout << "Freda's\n";
else if (check2(s))
cout << "Rainbow's\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int MaxN = 1010;
long long f[MaxN];
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
f[0] = a, f[1] = b;
for (int i = 2; i <= c; i++) f[i] = f[i - 1] + f[i - 2];
printf("%I64d\n", f[c]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
namespace fastIO {
static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;
char buf[IN_BUF], *ps = buf, *pt = buf;
inline char my_getchar() {
return ps == pt && (pt = (ps = buf) + fread(buf, 1, IN_BUF, stdin), ps == pt)
? EOF
: *ps++;
}
template <typename T>
inline bool read(T &x) {
char op = 0, ch = my_getchar();
for (x = 0; !isdigit(ch) && ch != EOF; ch = my_getchar())
if (ch == '-') op ^= 1;
if (ch == EOF) return false;
for (; isdigit(ch); ch = my_getchar()) x = x * 10 + (ch ^ '0');
if (ch != '.')
return op ? x = -x : 0, true;
else
ch = my_getchar();
T t = 1;
for (; isdigit(ch); ch = my_getchar()) t /= 10, x += (ch ^ '0') * t;
return op ? x = -x : 0, true;
}
inline int reads(char *s) {
int n = 0;
char ch = my_getchar();
for (; isspace(ch) && ch != EOF; ch = my_getchar())
;
for (; !isspace(ch) && ch != EOF; ch = my_getchar()) s[n++] = ch;
return s[n] = '\0', n;
}
char pbuf[OUT_BUF], *pp = pbuf;
struct _flusher {
~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); }
} OutputFlusher;
inline void my_putchar(char x) {
pp == pbuf + OUT_BUF ? fwrite(pbuf, 1, OUT_BUF, stdout), pp = pbuf : 0,
*pp++ = x;
}
template <typename T>
inline void print_(T x) {
if (x == 0) return my_putchar('0'), void(0);
std::vector<int> num;
for (x < 0 ? my_putchar('-'), x = -x : 0; x; x /= 10) num.push_back(x % 10);
while (!num.empty()) my_putchar(num.back() ^ '0'), num.pop_back();
}
template <typename T>
inline void print(T x, char ch = '\n') {
print_(x), my_putchar(ch);
}
inline void prints_(const char *s, int n = -1) {
if (n == -1) n = strlen(s);
for (register int i = 0; i < n; ++i) my_putchar(s[i]);
}
inline void prints(const char *s, int n = -1, char ch = '\n') {
prints_(s, n), my_putchar(ch);
}
template <typename T>
inline void printd_(T x, int n) {
if (x < 0) x = -x, my_putchar('-');
long long a, b;
for (register int i = 0; i < n; ++i) x *= 10;
x = (long long)(x + 0.5);
for (register int i = 0; i < n; ++i) x /= 10;
a = x, x -= a;
for (register int i = 0; i < n; ++i) x *= 10;
b = x;
print_(a);
if (n)
my_putchar('.');
else
return;
std::vector<int> num(n);
for (register int i = 0; i < n; ++i) num[i] = b % 10, b /= 10;
while (!num.empty()) my_putchar(num.back() ^ '0'), num.pop_back();
}
template <typename T>
inline void printd(T x, int n, char ch = '\n') {
printd_(x, n), my_putchar(ch);
}
} // namespace fastIO
using namespace fastIO;
const int P = 998244353, Inv2 = (P + 1) >> 1;
int n, m, l, r;
int qpow(int a, long long b) {
int s = 1;
for (; b; b >>= 1, a = 1ll * a * a % P)
if (b & 1) s = 1ll * s * a % P;
return s;
}
void solve() {
read(n), read(m), read(l), read(r);
if (1ll * n * m % 2 == 1) return print(qpow(r - l + 1, 1ll * n * m)), void(0);
if ((r - l + 1) & 1)
return print(1ll * (qpow(r - l + 1, 1ll * n * m) + 1) * Inv2 % P), void(0);
else
return print(1ll * qpow(r - l + 1, 1ll * n * m) * Inv2 % P), void(0);
}
int main() {
int T = 1;
while (T--) solve();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:256000000")
const int N = 200000;
int n;
int k = 0;
int m[3] = {0, 1, 2};
vector<vector<int> > gr;
int cou[N];
int ans[N];
int main() {
scanf("%d", &n);
gr.resize(n);
for (int i = 0; i < n; ++i) {
int t;
scanf("%d", &t);
gr[t].push_back(i);
}
int k = 0;
for (int i = 0; i < n; ++i) {
int t = cou[k];
while (k >= 0 && t >= gr[k].size()) {
k -= 3;
t = cou[k];
}
if (k < 0) {
cout << "Impossible";
return 0;
}
ans[i] = gr[k][t];
cou[k]++;
k++;
}
cout << "Possible\n";
for (int i = 0; i < n; ++i) cout << ans[i] + 1 << ' ';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int nn = 258;
const int MOD = 1e9 + 7;
const int BASE = 257;
char s[nn][nn];
int v[nn];
long long t[nn];
long long power[nn];
long long res;
int n, m;
void manacher(const vector<int> &k) {
int p = k.size() * 2 - 1;
vector<int> f(p, 0);
vector<int> a(p, -8);
for (int i = 0; i < p; i += 2) a[i] = k[i / 2];
int l = 0, r = -1;
for (int i = 0, j = 0; i < p; i++) {
j = (i > r ? 0 : min(f[l + r - i], r - i)) + 1;
while (i - j >= 0 && i + j < p && a[i - j] == a[i + j]) j++;
f[i] = --j;
if (i + j > r) {
r = i + j;
l = i - j;
}
if (i & 1 ^ 1)
res += f[i] / 2 + 1;
else
res += (f[i] + 1) / 2;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
power[0] = 1;
int tmp = 1;
for (int i = 1; i < 26; i++) {
power[i] = (power[i - 1] * BASE) % MOD;
}
for (int i = 1; i <= n; i++) {
cin >> s[i] + 1;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
v[j] = 0;
t[j] = 0;
}
for (int j = i; j <= m; j++) {
vector<vector<int> > b;
bool pre = false;
for (int k = 1; k <= n; k++) {
v[k] ^= 1 << (s[k][j] - 97);
t[k] = (t[k] + power[s[k][j] - 97]) % (MOD + 2);
bool curr = __builtin_popcount(v[k]) <= 1;
if (curr) {
if (curr != pre) b.push_back(vector<int>());
b.back().push_back(t[k]);
}
pre = curr;
}
for (vector<int> &k : b) {
manacher(k);
}
}
}
cout << res;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3001, mod = 1e9 + 7;
const double eps = 1e-9;
template <typename A, typename B>
inline bool chmin(A &a, B b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B>
inline bool chmax(A &a, B b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename A, typename B>
inline long long add(A x, B y) {
if (x + y < 0) return x + y + mod;
return x + y >= mod ? x + y - mod : x + y;
}
template <typename A, typename B>
inline void add2(A &x, B y) {
if (x + y < 0)
x = x + y + mod;
else
x = (x + y >= mod ? x + y - mod : x + y);
}
template <typename A, typename B>
inline long long mul(A x, B y) {
return 1ll * x * y % mod;
}
template <typename A, typename B>
inline void mul2(A &x, B y) {
x = (1ll * x * y % mod + mod) % mod;
}
template <typename A>
inline void debug(A a) {
cout << a << '\n';
}
template <typename A>
inline long long sqr(A x) {
return 1ll * x * x;
}
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, x, y;
double f[MAXN][MAXN], A[MAXN], B[MAXN], C[MAXN];
signed main() {
N = read();
M = read();
x = read(), y = read();
if (M == 1) {
cout << 2 * (N - x);
return 0;
}
for (int i = N - 1; i >= 1; i--) {
A[1] = 1;
B[1] = -0.5, C[1] = 1.5 + 0.5 * f[i + 1][1];
for (int j = 2; j <= M - 1; j++) {
double p = (-0.25 / A[j - 1]);
A[j] = 0.75 - p * B[j - 1];
B[j] = -0.25;
C[j] = 1.0 + 0.25 * f[i + 1][j] - p * C[j - 1];
}
double tmp = -0.5 / A[M - 1];
f[i][M] = (C[M - 1] * tmp - 1.5 - 0.5 * f[i + 1][M]) / (B[M - 1] * tmp - 1);
f[i][M - 1] = (C[M - 1] - B[M - 1] * f[i][M]) / A[M - 1];
for (int j = M - 2; j >= 1; j--)
f[i][j] = (C[j] - B[j] * f[i][j + 1]) / A[j];
}
printf("%.5lf", f[x][y]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * acos(0.0), EPS = 1e-9;
const int INF = 1e9, NINF = -1e9, MOD = 1e9 + 7;
void GO() { cout << fixed << setprecision(10); }
int main() {
GO();
int n, m, q, r, l, c, a[1001] = {};
string second, t;
cin >> n >> m >> q >> second >> t;
for (int i = 0; i <= n - m; i++)
if (second.substr(i, m) == t) a[i] = 1;
while (q--) {
c = 0;
cin >> r >> l;
for (int i = r - 1; i <= l - m; i++)
if (a[i]) c++;
cout << c << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int INF = 1e9 + 7;
const int MAXN = 3e5 + 10;
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
for (int i = a; i <= 2 * a; i++) {
for (int j = b; j <= 2 * b; j++) {
for (int k = c; k <= 2 * c; k++) {
if (i > j && j > k && d <= i && d <= j && d <= k) {
if (k <= d + d && j > d + d && i > d + d) {
cout << i << '\n' << j << '\n' << k;
return 0;
}
}
}
}
}
cout << -1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, x, a[105], c, ans, t;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
c = 350;
for (int i = 1; i <= n; i++)
if (a[i] <= c) {
c -= a[i];
ans++;
} else {
x = i;
break;
}
if (!x) x = n + 1;
a[x] -= c;
c = 0;
for (int i = x; i <= n; i++)
if (c + a[i] <= 360) {
c += a[i];
t += c;
ans++;
}
cout << ans << " " << t << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, i;
cin >> n >> k;
int f[n], t[n];
int maxx = 0, count[n];
for (i = 0; i < n; i++) {
cin >> f[i] >> t[i];
if (t[i] > k) {
count[i] = f[i] - (t[i] - k);
} else
count[i] = f[i];
}
cout << *max_element(count, count + n);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, a, x, b, y;
cin >> n >> a >> x >> b >> y;
bool check = false;
while (1) {
if (a == x || b == y) break;
a = a + 1;
b = b - 1;
if (a > n) a = 1;
if (b < 1) b = n;
if (a == b) {
check = true;
break;
}
}
if (a == b) check = true;
if (check)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y, i, j, yes;
char a[55][55];
int xf, yf, xe, ye;
while (cin >> x >> y) {
xf = 0;
yf = 0;
xe = x - 1;
ye = y - 1;
memset(a, '0', sizeof(a));
for (i = 0; i < x; i++) {
cin >> a[i];
}
for (i = 0; i < x; i++) {
yes = 0;
for (j = 0; j < y; j++) {
if (a[i][j] == '.') yes++;
}
if (yes == y)
xf++;
else
break;
}
for (i = x - 1; i >= 0; i--) {
yes = 0;
for (j = 0; j < y; j++) {
if (a[i][j] == '.') yes++;
}
if (yes == y)
xe--;
else
break;
}
for (j = 0; j < y; j++) {
yes = 0;
for (i = 0; i < x; i++) {
if (a[i][j] == '.') yes++;
}
if (yes == x)
yf++;
else
break;
}
for (j = y - 1; j >= 0; j--) {
yes = 0;
for (i = 0; i < x; i++) {
if (a[i][j] == '.') yes++;
}
if (yes == x)
ye--;
else
break;
}
for (i = xf; i <= xe; i++) {
for (j = yf; j <= ye; j++) {
cout << a[i][j];
}
cout << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int M[200050][25] = {0};
int A[200050];
int NO[200050][25] = {INT_MAX};
int B[200050];
int N;
int minidx, maxidx;
void process1() {
int i, j;
for (i = 0; i < N; i++) M[i][0] = i;
for (j = 1; 1 << j <= N; j++)
for (i = 0; i + (1 << j) - 1 < N; i++)
if (A[M[i][j - 1]] > A[M[i + (1 << (j - 1))][j - 1]])
M[i][j] = M[i][j - 1];
else
M[i][j] = M[i + (1 << (j - 1))][j - 1];
}
int RMQ1(int i, int j) {
int k = (log2(j - i + 1));
return max(A[M[i][k]], A[M[j - (1 << k) + 1][k]]);
}
int RMQ2(int i, int j) {
int k = (log2(j - i + 1));
return min(B[NO[i][k]], B[NO[j - (1 << k) + 1][k]]);
}
void process2() {
int i, j;
for (i = 0; i < N; i++) NO[i][0] = i;
for (j = 1; 1 << j <= N; j++)
for (i = 0; i + (1 << j) - 1 < N; i++)
if (B[NO[i][j - 1]] < B[NO[i + (1 << (j - 1))][j - 1]])
NO[i][j] = NO[i][j - 1];
else
NO[i][j] = NO[i + (1 << (j - 1))][j - 1];
}
void rec_bin1(int lo, int hi, int i) {
int mid;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
if (RMQ1(i, mid) - RMQ2(i, mid) == 0) {
maxidx = max(mid, maxidx);
minidx = min(minidx, mid);
}
if (RMQ1(i, mid) - RMQ2(i, mid) <= 0)
lo = mid + 1;
else
hi = mid - 1;
}
}
void rec_bin2(int lo, int hi, int i) {
int mid;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
if (RMQ1(i, mid) - RMQ2(i, mid) == 0) {
maxidx = max(mid, maxidx);
minidx = min(minidx, mid);
}
if (RMQ1(i, mid) - RMQ2(i, mid) < 0)
lo = mid + 1;
else
hi = mid - 1;
}
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) cin >> A[i];
for (int i = 0; i < N; i++) cin >> B[i];
process1();
process2();
long long ans = 0;
for (int i = 0; i < N; i++) {
minidx = INT_MAX;
maxidx = -1;
rec_bin1(i, N - 1, i);
rec_bin2(i, N - 1, i);
if (maxidx != -1) ans = ans + maxidx - minidx + 1;
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
using namespace std;
const int MAXN = 600010;
int n, m, k, u, v, x, y, t, a, b;
int par[MAXN], col[MAXN];
long long s0[MAXN], s1[MAXN], ans;
vector<int> vec[MAXN];
string S;
pair<int, int> Getpar(int x) {
if (par[x] == x) return {x, 0};
pair<int, int> p = Getpar(par[x]);
return {par[x] = p.first, col[x] ^= p.second};
}
void Join(int u, int v, int w) {
pair<int, int> x = Getpar(u);
pair<int, int> y = Getpar(v);
if (x.first == y.first) return;
ans -= min(s1[x.first], s0[x.first]);
ans -= min(s1[y.first], s0[y.first]);
w ^= x.second ^ y.second;
if (w) {
swap(s1[y.first], s0[y.first]);
col[y.first] = 1;
}
par[y.first] = x.first;
s1[x.first] += s1[y.first];
s0[x.first] += s0[y.first];
s1[y.first] = s0[y.first] = 0;
ans += min(s1[x.first], s0[x.first]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> S;
for (int i = 1; i <= k; i++) par[i] = i, s0[i] = 1;
for (int i = 1; i <= k; i++) {
cin >> x;
while (x--) {
cin >> y;
vec[y].push_back(i);
}
}
S = "." + S;
int N = k;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
N++;
par[N] = N;
s0[N] = k + 1;
Join(vec[i][0], N, S[i] == '0');
}
if (vec[i].size() == 2) Join(vec[i][0], vec[i][1], S[i] == '0');
cout << ans << '\n';
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
//#define int long long
int get(int b, int i, int j) {
cout << b << ' ' << i + 1 << ' ' << j + 1 << endl;
int x;
cin >> x;
return x;
}
void solve() {
int n;
cin >> n;
int b;
cin >> b;
vector<vector<int>> arr(n, vector<int>(n, -1));
int cnt = n * n;
while (cnt > 0) {
bool done = false;
if (b != 1) {
for (int i = 0; i < n && !done; ++i) {
for (int j = 0; j < n && !done; ++j) {
if (arr[i][j] != -1 || (i + j) % 2 == 1)
continue;
done = true;
b = get(1, i, j);
arr[i][j] = 1;
cnt--;
}
}
if (done)
continue;
}
int a = 2;
if (b == 2)
a = 3;
for (int i = 0; i < n && !done; ++i) {
for (int j = 0; j < n && !done; ++j) {
if (arr[i][j] != -1 || (i + j) % 2 == 0)
continue;
done = true;
b = get(a, i, j);
arr[i][j] = a;
cnt--;
}
}
if (done)
continue;
a = 5 - a;
for (int i = 0; i < n && !done; ++i) {
for (int j = 0; j < n && !done; ++j) {
if (arr[i][j] != -1)
continue;
done = true;
b = get(a, i, j);
arr[i][j] = a;
cnt--;
}
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
} | 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
vector<int> ads[100005];
int p[100005];
int cl[100005];
void dfs(int n) {
int x, y;
if (cl[n] == 1) {
for (x = 0; x < ads[n].size(); x++) {
if (cl[ads[n][x]] == 0) break;
}
if (x == ads[n].size()) ans.push_back(n);
}
for (int i = 0; i < ads[n].size(); i++) {
dfs(ads[n][i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
for (int qq = 1; qq <= T; qq++) {
int n, root;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
p[i + 1] = x;
if (x == -1)
root = i + 1;
else
ads[x].push_back(i + 1);
cl[i + 1] = y;
}
dfs(root);
if (ans.size() == 0) cout << "-1" << endl;
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
cout << ans[i];
if (i != ans.size() - 1)
cout << ' ';
else
cout << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int total, mult, val[100010];
bool vis[100010];
stack<long long int> ss;
vector<long long int> x;
vector<long long int> adj[100010 * 3];
vector<long long int> adj2[100010 * 3];
void dfs(long long int s) {
vis[s] = 1;
for (int i : adj[s]) {
if (!vis[i]) dfs(i);
}
ss.push(s);
}
void dfs2(long long int s) {
vis[s] = 1;
x.push_back(s);
for (int i : adj2[s]) {
if (!vis[i]) dfs2(i);
}
}
int main() {
long long int i, j, k;
long long int n, m;
long long int u, v;
long long int count;
scanf("%I64d", &n);
for (i = 1; i <= n; i++) scanf("%I64d", &val[i]);
scanf("%I64d", &m);
for (i = 0; i < m; i++) {
scanf("%I64d%I6d", &u, &v);
adj[u].push_back(v);
adj2[v].push_back(u);
}
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++) {
if (!vis[i]) dfs(i);
}
total = 0, mult = 1;
memset(vis, 0, sizeof(vis));
while (!ss.empty()) {
if (!vis[ss.top()]) {
x.clear();
dfs2(ss.top());
m = 1000000007;
count = 0;
for (i = 0; i < x.size(); i++) m = min(m, val[x[i]]);
total += m;
for (i = 0; i < x.size(); i++) {
if (val[x[i]] == m) count++;
}
mult *= (count % 1000000007);
}
ss.pop();
}
printf("%I64d %I64d", total, mult % 1000000007);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 0, f1, f2, f3, f4, i, j;
cin >> n;
int x[n], y[n];
for (i = 0; i < n; i++) cin >> x[i] >> y[i];
for (i = 0; i < n; i++) {
f1 = 0, f2 = 0, f3 = 0, f4 = 0;
for (j = 0; j < n; j++) {
if (x[j] < x[i] && y[i] == y[j]) f1 = 1;
if (x[j] > x[i] && y[i] == y[j]) f2 = 1;
if (x[j] == x[i] && y[i] > y[j]) f3 = 1;
if (x[j] == x[i] && y[i] < y[j]) f4 = 1;
}
if (f1 && f2 && f3 && f4) count++;
}
cout << count;
}
| 1 |
#include <bits/stdc++.h>
const int MaxN = 6e5 + 50;
template <class t>
inline void read(t &s) {
s = 0;
register int f = 1;
register char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
s *= f;
return;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
read(x);
read(a...);
}
template <class t>
inline void write(t x) {
if (x < 0) putchar('-'), x = -x;
int buf[21], top = 0;
while (x) buf[++top] = x % 10, x /= 10;
if (!top) buf[++top] = 0;
while (top) putchar(buf[top--] ^ '0');
return;
}
inline void setIn(std::string s) {
freopen(s.c_str(), "r", stdin);
return;
}
inline void setOut(std::string s) {
freopen(s.c_str(), "w", stdout);
return;
}
inline void setIO(std::string s = "") {
setIn(s + ".in");
setOut(s + ".out");
return;
}
template <class t>
inline bool checkmin(t &x, t y) {
if (x > y) {
x = y;
return 1;
}
return 0;
}
template <class t>
inline bool checkmax(t &x, t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
inline int lowbit(int x) { return x & (-x); }
std::vector<std::pair<int, int> > E[MaxN], G[MaxN];
int n, m;
int dfn[MaxN], low[MaxN], vis[MaxN], sta[MaxN], top, dfncnt;
int col[MaxN], s[MaxN], scc;
int f[MaxN];
inline void tarjan(int u, int fa) {
dfn[u] = low[u] = ++dfncnt, sta[top++] = u, vis[u] = 1;
for (auto v : E[u])
if (v.first != fa) {
if (!dfn[v.first])
tarjan(v.first, u), checkmin(low[u], low[v.first]);
else if (vis[v.first])
checkmin(low[u], dfn[v.first]);
}
if (dfn[u] == low[u]) {
++scc;
do --top, vis[sta[top]] = 0, col[sta[top]] = scc;
while (sta[top] != u);
}
}
namespace SP {
int up[MaxN][21], LG[MaxN], dep[MaxN], dfn[MaxN], dfncnt;
int father[MaxN];
inline void dfs(int u, int fa) {
father[u] = fa, f[u] += s[u];
dep[u] = dep[fa] + 1, up[dfn[u] = ++dfncnt][0] = u;
for (auto v : G[u])
if (v.first != fa)
f[v.first] = f[u] + v.second, dfs(v.first, u), up[++dfncnt][0] = u;
}
inline int min(int u, int v) { return dep[u] < dep[v] ? u : v; }
inline void init() {
for (int i = 2; i < MaxN; ++i) LG[i] = LG[i >> 1] + 1;
dfs(1, 0);
for (int k = 1; k < 21; ++k)
for (int i = 1; i + (1 << k) - 1 <= dfncnt; ++i)
up[i][k] = min(up[i][k - 1], up[i + (1 << (k - 1))][k - 1]);
}
inline int lca(int u, int v) {
u = dfn[u], v = dfn[v];
if (u > v) std::swap(u, v);
register int k = LG[v - u + 1];
return min(up[u][k], up[v - (1 << k) + 1][k]);
}
inline int fuck(int u, int v) {
register int g = lca(u, v);
return f[u] + f[v] - (f[g] << 1) - s[g];
}
} // namespace SP
signed main(void) {
read(n, m);
register int u, v, w;
for (int i = 1; i <= m; ++i)
read(u, v, w), E[u].push_back(std::make_pair(v, w)),
E[v].push_back(std::make_pair(u, w));
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0);
for (int u = 1; u <= n; ++u)
for (auto v : E[u])
if (col[u] != col[v.first])
G[col[u]].push_back(std::make_pair(col[v.first], v.second));
for (int u = 1; u <= n; ++u)
for (auto v : E[u])
if (col[u] == col[v.first]) s[col[u]] += v.second;
SP::init();
read(u, v);
std::puts(SP::fuck(col[u], col[v]) ? "YES" : "NO");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool comp(string a, string b) { return a.length() < b.length(); }
int main() {
int n, sec;
cin >> n >> sec;
vector<string> s;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
s.push_back(str);
}
string pwd;
cin >> pwd;
sort(s.begin(), s.end(), comp);
int len = pwd.length();
int maxtime = 0, mintime = 0, times = 0;
int ansmin = 0, ansmax = 0;
int flg = 0;
for (int i = 0; i < n; i++) {
string str = s[i];
if (str.length() < len) {
mintime++;
maxtime++;
} else if (flg == 0 && str.length() == len) {
mintime++;
maxtime++;
ansmin = mintime;
flg = 1;
} else if (flg == 1 && str.length() == len) {
maxtime++;
}
if ((str.length() > len) || (i == n - 1) || (s[i + 1].length() > len)) {
ansmax = maxtime;
break;
}
times++;
if (times == sec) {
mintime += 5;
maxtime += 5;
times = 0;
}
}
cout << ansmin << " " << maxtime << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long l, p[11] = {0}, n[11] = {0}, k;
cin >> l;
for (int i = 0; i < l; i++) {
cin >> k;
if (k > 0)
p[k]++;
else if (k < 0)
n[-k]++;
else
p[0]++;
}
long long int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += p[i] * n[i];
}
sum += (p[0] * (p[0] - 1)) / 2;
cout << sum;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[500];
int used[500];
long long gcd(long long x, long long y) {
if (y == 0)
return x;
else
return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
int main() {
int i, n, j;
long long ans, mini, tmp;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]--;
}
ans = 1;
mini = 0;
for (i = 0; i < n; i++) {
memset(used, 0, sizeof(used));
tmp = i;
j = 1;
while (used[tmp] == 0) {
used[tmp] = j;
j++;
tmp = a[tmp];
}
ans = lcm(j - used[tmp], ans);
if (used[tmp] > mini) mini = used[tmp];
}
mini--;
tmp = ans;
while (ans < mini) ans += tmp;
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-9;
int main() {
ios_base::sync_with_stdio(false);
long long n;
cin >> n;
long long result = 1;
for (long long i = 2; i * i <= n; i++) {
if (n % i) continue;
result *= i;
while (n % i == 0) n /= i;
}
if (n > 1) result *= n;
cout << result << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
int n, k;
cin >> n >> k;
auto a = vector<int>(n);
for (int i = 0; i < (n); ++i) cin >> a[i];
auto next_pos = vector<deque<int>>(n);
for (int i = 0; i < (n); ++i) {
auto book = a[i] - 1;
next_pos[book].push_back(i);
}
for (int i = 0; i < (n); ++i) next_pos[i].push_back(n);
set<pair<int, int>> books;
auto buys = 0;
for (int i = 0; i < (n); ++i) {
auto book = a[i] - 1;
next_pos[book].pop_front();
if (books.count({i, book}) > 0) {
books.erase({i, book});
books.insert({next_pos[book].front(), book});
continue;
}
if (books.size() == k) {
books.erase(prev(books.end()));
}
buys++;
books.insert({next_pos[book].front(), book});
}
cout << buys << '\n';
cout.flush();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline bool checkBit(long long n, long long i) {
return n & ((long long)1 << i);
}
inline long long setBit(long long n, long long i) {
return n | ((long long)1 << i);
;
}
inline long long resetBit(long long n, long long i) {
return n & (~((long long)1 << i));
}
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long &a) { a = (a + 1000000007) % 1000000007; }
inline long long modMul(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long modPow(long long b, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1;
}
return r;
}
inline long long modInv(long long a) { return modPow(a, 1000000007 - 2); }
inline long long modDiv(long long a, long long b) {
return modMul(a, modInv(b));
}
inline long long fact(long long r) {
long long ans = 1;
while (r) ans = modMul(ans, r), r--;
return ans;
}
inline long long invfact(long long r) {
long long ans = 1;
while (r) ans = modMul(ans, modInv(r)), r--;
return ans;
}
inline double dgcd(double a, double b) {
if (a < b) return dgcd(b, a);
if (b < .0001) return a;
return dgcd(b, fmod(a, b));
}
void solve() {
long long n;
cin >> n;
long long a, cnta[6] = {0}, cntb[6] = {0}, tot[6] = {0}, ans = 0;
for (long long i = 0; i < n; i++) {
cin >> a;
cnta[a]++;
tot[a]++;
}
for (long long i = 0; i < n; i++) {
cin >> a;
cntb[a]++;
tot[a]++;
}
for (long long i = 0; i < 6; i++) {
if (tot[i] % 2) {
cout << -1 << endl;
return;
}
ans += abs(cnta[i] - tot[i] / 2);
}
cout << ans / 2 << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t = 1;
while (t--) {
solve();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> a(n + 1, 0);
unordered_map<long long, long long> m;
vector<char> ans(n + 1, 'A');
for (long long i = 1; i <= n; i++) {
cin >> a[i];
m[a[i]] = i;
}
for (long long i = n; i > 0; i--) {
long long idx = m[i] - i, flag = 0;
while (idx > 0 && !flag) {
if (a[idx] > i && ans[idx] == 'B') flag = 1;
idx -= i;
}
idx = m[i] + i;
while (idx <= n && !flag) {
if (a[idx] > i && ans[idx] == 'B') flag = 1;
idx += i;
}
ans[m[i]] = (flag) ? 'A' : 'B';
}
for (int i = 1; i <= n; i++) cout << ans[i];
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int dx[] = {1, 0, -1, 0};
long long int dy[] = {0, 1, 0, -1};
const long long int N = 1000 * 1000 + 5;
const long long int size = 1000 * 100 * 2 + 5;
const long long int mod = 1000 * 1000 * 1000 + 7;
long long int gcd(long long int a, long long int b) {
if (!b) return a;
return gcd(b, a % b);
}
long long int power_mod(long long int first, long long int second,
long long int p) {
long long int res = 1;
first %= p;
while (second > 0) {
if (second & 1) res = (res * first) % p;
second = second >> 1;
first = (first * first) % p;
}
return res;
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int sumofdigits(long long int n) {
long long int out = 0;
while (n) {
out += (n % 10);
n /= 10;
}
return out;
}
bool isPrime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long int power(long long int a, long long int b) {
if (b == 0) return 1;
if (b & 1) return a * power(a, b - 1);
int res = power(a, b / 2);
return res * res;
}
int main() {
long long int tt;
cin >> tt;
for (int _tt = 0; _tt < tt; _tt++) {
long long int n, r;
cin >> n >> r;
set<long long int> a;
long long int temp;
for (long long int i = 0; i < n; i++) {
cin >> temp;
a.insert(temp);
}
long long int sz = a.size();
long long int store, count = 0;
vector<long long int> v;
set<long long int>::iterator it;
for (it = a.begin(); it != a.end(); it++) {
store = sz - (count + 1);
v.push_back(*it - (store * r));
count++;
}
long long int sum = 0;
for (long long int i = 0; i < v.size(); i++) {
if (v[i] > 0) {
sum++;
}
}
cout << sum << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const long long inf = 1000000000000000LL;
struct Friend {
int tax, gir, pic;
string name;
Friend() { tax = gir = pic = 0; }
};
void calc(Friend& f, string n) {
vector<long long> a;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 2; ++j) {
int k = i * 3 + j;
a.push_back(n[k] - '0');
}
}
vector<long long> b = a;
sort(a.begin(), a.end(), greater<int>());
if (b == a && a[0] != a[1] && a[1] != a[2] && a[2] != a[3] && a[3] != a[4] &&
a[4] != a[5]) {
f.pic++;
} else if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3] && a[3] == a[4] &&
a[4] == a[5]) {
f.tax++;
} else {
f.gir++;
}
}
bool check(char c, string& a) {
int l = a.length();
if (l >= 2) {
if (c == a[l - 1] && c == a[l - 2]) {
return false;
}
}
if (l >= 3) {
if (c == a[l - 1] && a[l - 2] == a[l - 3]) return false;
}
return true;
}
int main() {
clock_t start = clock();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
srand(13);
long long n, x, y;
cin >> n >> x >> y;
x -= n - 1;
y -= n - 1;
if (y < 1) {
cout << "-1";
return 0;
}
if (y * y >= x) {
cout << y;
for (int i = 0; i < n - 1; ++i) {
cout << " 1";
}
} else {
cout << "-1";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, m[200000], n;
cin >> n >> b >> a;
int a1 = a, i;
for (int i = 0; i < n; i++) cin >> m[i];
for (i = 0; i < n; i++) {
if (m[i] == 0) {
if (a != 0)
a--;
else if (a == 0 && b != 0)
b--;
else {
cout << i;
return 0;
}
} else if (m[i] == 1) {
if (b != 0 && a < a1) {
b--;
a++;
} else if (a != 0)
a--;
else if (a == 0 && b != 0)
b--;
else {
cout << i;
return 0;
}
}
}
cout << i;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long Set(long long N, long long pos) { return N = N | (1 << pos); }
long long reset(long long N, long long pos) { return N = N & ~(1 << pos); }
bool check(long long N, long long pos) { return (bool)(N & (1 << pos)); }
void CI(long long &_x) { scanf("%I64d", &_x); }
void CO(long long &_x) { cout << _x; }
template <typename T>
void getarray(T a[], long long n) {
for (long long i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void prLLIarray(T a[], long long n) {
for (long long i = 0; i < n - 1; i++) cout << a[i] << " ";
cout << a[n - 1] << endl;
}
const double EPS = 1e-9;
const long long INF = 0x7f7f7f7f;
long long dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1};
long long dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1};
long long dr4[4] = {0, 0, 1, -1};
long long dc4[4] = {-1, 1, 0, 0};
long long kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1};
long long kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2};
long long pile[100005];
long long n;
long long mem[100005];
int main() {
memset(mem, -1, sizeof mem);
CI(n);
for (long long i = 0; i < n; i++) {
CI(pile[i]);
}
sort(pile, pile + n);
reverse(pile, pile + n);
for (long long i = 1; i < n; i++) {
pile[i] += pile[i - 1];
}
long long query;
CI(query);
while (query--) {
long long k, ans = 0;
CI(k);
if (mem[k] != -1) {
cout << mem[k] << "\n";
continue;
}
long long sz = 1;
for (long long i = 1, t = 1; i < n; i += sz, t++) {
sz *= k;
ans += (pile[min(n - 1, i + sz - 1)] - pile[i - 1]) * t;
}
mem[k] = ans;
cout << ans << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string n, m;
cin >> n;
cin >> m;
int sheets[26], garland[26];
memset(sheets, 0, sizeof(sheets));
memset(garland, 0, sizeof(garland));
for (int i = 0; i < n.size(); i++) sheets[n[i] - 'a']++;
for (int i = 0; i < m.size(); i++) garland[m[i] - 'a']++;
int ans = 0;
for (int i = 0; i < 26; i++) {
int p = min(sheets[i], garland[i]);
if (p == 0 && garland[i] != 0) {
ans = 0;
break;
}
ans += p;
}
if (ans)
cout << ans;
else
cout << "-1";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > a;
bool vis[100100];
int main() {
int n, i, j;
scanf("%d", &n);
for (i = 3; i * 2 <= n; i += 2)
if (!vis[i]) {
vector<int> b;
b.clear();
for (j = i; j <= n; j += 2 * i)
if (!vis[j]) {
vis[j] = true;
b.push_back(j);
}
for (j = b.size() - 1; j > 0; j -= 2) {
a.push_back(pair<int, int>(b[j], b[j - 1]));
vis[b[j]] = vis[b[j - 1]] = true;
}
if (b.size() & 1) {
a.push_back(pair<int, int>(i, i * 2));
vis[i] = vis[i * 2] = true;
}
}
if (n & 1) n--;
int flag = 0, last;
for (i = n; i > 0; i -= 2) {
if (vis[i]) continue;
if (!flag)
last = i, flag = 1;
else
flag = 0, a.push_back(pair<int, int>(i, last));
}
printf("%d\n", a.size());
for (i = 0; i < a.size(); i++) printf("%d %d\n", a[i].first, a[i].second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int step[3005][3005], N, M, a, b, res = -1, p[5], ans[5], rstep[3005][3005];
pair<int, int> MAX[3005][5], RMAX[3005][5];
vector<int> e[3005];
inline void bfs(int x) {
queue<int> q;
step[x][x] = 0;
q.push(x);
while (!q.empty()) {
int ff = q.front();
q.pop();
for (vector<int>::iterator it = e[ff].begin(); it != e[ff].end(); it++)
if (step[x][*it] == -1) {
step[x][*it] = step[x][ff] + 1;
q.push(*it);
}
}
}
inline void upDate() {
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++)
if (i != j && p[i] == p[j]) return;
int l = 0;
for (int i = 1; i < 4; i++) l += step[p[i]][p[i + 1]];
if (l > res) {
res = l;
for (int i = 1; i <= 4; i++) ans[i] = p[i];
}
}
void init() {
for (int i = 1; i <= N; i++) {
MAX[i][0] = MAX[i][1] = MAX[i][2] = MAX[i][3] = make_pair(-1, -1);
RMAX[i][0] = RMAX[i][1] = RMAX[i][2] = RMAX[i][3] = make_pair(-1, -1);
for (int j = 1; j <= N; j++) {
if (i == j) continue;
MAX[i][3] = make_pair(step[i][j], j);
for (int k = 3; k > 0; k--)
if (MAX[i][k] > MAX[i][k - 1])
swap(MAX[i][k], MAX[i][k - 1]);
else
break;
}
for (int j = 1; j <= N; j++) {
if (i == j) continue;
RMAX[i][3] = make_pair(rstep[i][j], j);
for (int k = 3; k > 0; k--)
if (RMAX[i][k] > RMAX[i][k - 1])
swap(RMAX[i][k], RMAX[i][k - 1]);
else
break;
}
}
}
int main() {
memset(step, -1, sizeof(step));
cin >> N >> M;
while (M--) {
cin >> a >> b;
if (a != b) e[a].push_back(b);
}
for (int i = 1; i <= N; i++) bfs(i);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) rstep[i][j] = step[j][i];
init();
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
if (i != j && step[i][j] != -1) {
p[2] = i, p[3] = j;
for (int u = 0; u < 4; u++)
if (RMAX[i][u].second > 0)
for (int k = 0; k < 4; k++)
if (MAX[i][k].second > 0) {
p[1] = RMAX[i][u].second;
p[4] = MAX[j][k].second;
upDate();
}
}
for (int i = 1; i <= 4; i++) cout << ans[i] << " ";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
enum class NodeState { UNVISITED, VISITED };
struct neighbour {
int id;
int cost;
int edgeId;
neighbour(int _id, int _cost, int _edgeId) {
id = _id;
cost = _cost;
edgeId = _edgeId;
}
};
int N, M, A, B;
vector<neighbour> nb[1005];
NodeState state[1005];
int from[1005];
int tick = 0;
int low[1005];
bool reachesB[1005];
int disabledEdge;
int minEdge, minCost;
int dfsFromEdge[1005];
void dfs(int v) {
state[v] = NodeState::VISITED;
low[v] = tick++;
if (v == B) reachesB[v] = true;
for (neighbour n : nb[v]) {
if (n.edgeId == disabledEdge || n.edgeId == dfsFromEdge[v]) continue;
if (state[n.id] == NodeState::UNVISITED) {
dfsFromEdge[n.id] = n.edgeId;
dfs(n.id);
}
if (reachesB[n.id]) reachesB[v] = true;
if (low[v] < low[n.id] && reachesB[n.id]) {
if (n.cost < minCost) {
minCost = n.cost;
minEdge = n.edgeId;
}
}
}
for (neighbour n : nb[v]) {
if (n.edgeId != disabledEdge && n.edgeId != dfsFromEdge[v] &&
low[n.id] < low[v])
low[v] = low[n.id];
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> N >> M >> A >> B;
--A;
--B;
for (int i = 0; i < M; ++i) {
int a, b, w;
cin >> a >> b >> w;
--a;
--b;
nb[a].emplace_back(b, w, i);
nb[b].emplace_back(a, w, i);
}
fill(state, state + N, NodeState::UNVISITED);
fill(from, from + N, -1);
queue<int> bfs;
bfs.push(A);
state[A] = NodeState::VISITED;
while (bfs.size() > 0) {
int c = bfs.front();
bfs.pop();
for (neighbour n : nb[c]) {
if (state[n.id] == NodeState::UNVISITED) {
state[n.id] = NodeState::VISITED;
from[n.id] = c;
bfs.push(n.id);
}
}
}
if (state[B] == NodeState::UNVISITED) {
cout << "0\n0\n";
return 0;
}
int cur = B;
int overallBestCost = INT_MAX;
vector<int> roadsToClose;
while (from[cur] != -1) {
fill(state, state + N, NodeState::UNVISITED);
fill(low, low + N, INT_MAX);
fill(reachesB, reachesB + N, false);
fill(dfsFromEdge, dfsFromEdge + N, -1);
tick = 0;
minCost = INT_MAX;
minEdge = -1;
disabledEdge = -1;
int cost;
for (neighbour n : nb[cur])
if (n.id == from[cur]) {
disabledEdge = n.edgeId;
cost = n.cost;
break;
}
assert(disabledEdge >= 0);
dfs(A);
if (state[B] == NodeState::UNVISITED) {
if (cost < overallBestCost) {
overallBestCost = cost;
roadsToClose.clear();
roadsToClose.push_back(disabledEdge);
}
} else if (minEdge >= 0) {
cost += minCost;
if (cost < overallBestCost) {
overallBestCost = cost;
roadsToClose.clear();
roadsToClose.push_back(disabledEdge);
roadsToClose.push_back(minEdge);
}
}
cur = from[cur];
}
fill(state, state + N, NodeState::UNVISITED);
bfs.push(A);
state[A] = NodeState::VISITED;
if (overallBestCost == INT_MAX) {
cout << "-1\n";
return 0;
}
cout << overallBestCost << '\n' << roadsToClose.size() << '\n';
for (int i : roadsToClose) cout << i + 1 << ' ';
cout << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int grundy(int x) {
if (x < 2)
return x;
else if (x == 3)
return (k % 2);
else if (x % 2)
return 0;
else {
int res = 0;
set<int> st;
st.insert(grundy(x - 1));
st.insert((k % 2) * grundy(x / 2));
while (st.count(res)) res++;
return res;
}
}
int main() {
int i, a, x = 0;
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &a);
x ^= grundy(a);
}
if (x)
puts("Kevin");
else
puts("Nicky");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long t, a, b, c, d;
int main() {
cin >> t;
while (t--) {
cin >> a >> b >> c >> d;
long long cd = c / d, abd = a / (b * d);
if (c <= d && a <= b * c) {
cout << a << endl;
} else if (c <= d) {
cout << "-1\n";
} else if (a > cd * b * d + b * (c % d)) {
cout << "-1\n";
} else {
cout << a * abd + a - b * d * abd * (abd + 1) / 2 << "\n";
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int A, B;
char picture[50][50];
bool check(int first, int second) {
for (int a1 = 0; a1 < A; a1 += first)
for (int a2 = 0; a2 < A; a2 += first)
for (int b1 = 0; b1 < B; b1 += second)
for (int b2 = 0; b2 < B; b2 += second) {
if (a1 == a2 && b1 == b2) continue;
bool flag = false;
for (int i = 0; i < first; i++)
for (int j = 0; j < second; j++)
flag |= (picture[a1 + i][b1 + j] != picture[a2 + i][b2 + j]);
if (!flag) return false;
flag = false;
for (int i = 0; i < first; i++)
for (int j = 0; j < second; j++)
flag |= (picture[a1 + first - i - 1][b1 + second - j - 1] !=
picture[a2 + i][b2 + j]);
if (!flag) return false;
if (first != second) continue;
flag = false;
for (int i = 0; i < first; i++)
for (int j = 0; j < second; j++)
flag |= (picture[a1 + j][b1 + first - i - 1] !=
picture[a2 + i][b2 + j]);
if (!flag) return false;
flag = false;
for (int i = 0; i < first; i++)
for (int j = 0; j < second; j++)
flag |= (picture[a1 + second - j - 1][b1 + i] !=
picture[a2 + i][b2 + j]);
if (!flag) return false;
}
return true;
}
int main(int argc, char** argv) {
scanf("%d%d", &A, &B);
for (int i = 0; i < A; i++) scanf("%s", picture[i]);
pair<int, int> best(A, B);
int count = 0;
for (int first = 1; first <= A; first++)
for (int second = 1; second <= B; second++) {
if ((A % first) || (B % second)) continue;
if (check(first, second)) {
count++;
if ((first * second < best.first * best.second) ||
((first * second == best.first * best.second) &&
first < best.first))
best = pair<int, int>(first, second);
}
}
printf("%d\n", count);
printf("%d %d\n", best.first, best.second);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int dp[2][510][510] = {1}, bug[510], n, m, b, mod, ans = 0;
int main() {
cin >> n >> m >> b >> mod;
for (int i = 1; i <= n; i++) {
cin >> bug[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= b; k++) {
if (j && k >= bug[i])
dp[i % 2][j][k] =
(dp[i % 2][j - 1][k - bug[i]] + dp[!(i % 2)][j][k]) % mod;
else
dp[i % 2][j][k] = dp[!(i % 2)][j][k];
if (i == n && j == m) ans = (ans + dp[i % 2][j][k]) % mod;
}
}
}
cout << ans << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
long long b[maxn];
int n;
map<long long, long long> mapper;
int main() {
long long sum = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
mapper[b[i] - i] += b[i];
sum = max(sum, mapper[b[i] - i]);
}
cout << sum << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
bool isGood(int x) {
int o = 0, z = 0;
int lastOne = -1;
for (int i = 0; i <= 31; ++i)
if ((x & (1 << i)) > 0) lastOne = i;
for (int i = 0; i <= lastOne; ++i) {
if ((x & (1 << i)) == 0 && o == 0) {
++z;
} else if ((x & (1 << i)) > 1) {
++o;
} else {
return false;
}
}
return (z + 1 == o);
}
int main() {
int n;
cin >> n;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
if (i * i == n)
v.push_back(i);
else {
v.push_back(i);
v.push_back(n / i);
}
}
}
int ans = 1;
for (auto it : v) {
if (ans < it && isGood(it)) ans = it;
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1005;
int a[MAX_N];
int n, m, c;
bool check(int pos) {
if (pos < n) {
return false;
}
for (int i = 1; i < n; i++) {
if (a[i] > a[i + 1] || a[i] == 0) {
return false;
}
}
return true;
}
int main() {
scanf("%d %d %d", &n, &m, &c);
for (int j = 1; j <= m; j++) {
int x;
scanf("%d", &x);
int res = -1;
if (2 * x > c) {
for (int i = n; i >= 1; i--) {
if (a[i] == 0 || a[i] < x) {
a[i] = x;
printf("%d\n", i);
fflush(stdout);
break;
}
}
} else {
for (int i = 1; i <= n; i++) {
if (a[i] == 0 || a[i] > x) {
a[i] = x;
printf("%d\n", i);
fflush(stdout);
break;
}
}
}
if (check(j) == true) {
return 0;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long const mo = 1e9 + 7;
long double const pi = 2.00 * asin(1.00);
long long mod(long long k) { return (k + mo) % mo; }
long long addmod(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mulmod(long long a, long long b) { return mod(mod(a) * mod(b)); }
long long submod(long long a, long long b) {
return mod(a % mo - b % mo + mo) % mo;
}
void solve() {
string s;
cin >> s;
int res = 0;
vector<int> v(10, 0);
for (int i = 0; i < s.size(); ++i) {
v[s[i] - '0']++;
}
int res1 = 1;
for (int i = 0; i <= 9; ++i) res1 = max(res1, v[i]);
res1 = s.size() - res1;
for (char i = '0'; i <= '9'; ++i) {
for (char j = i + 1; j <= '9'; ++j) {
int noi = 0, noj = 0;
bool x = true, y = true;
for (int k = 0; k < s.size(); ++k) {
if (x && s[k] == i) {
++noi;
x = false;
y = true;
}
if (y && s[k] == j) {
++noj;
y = false;
x = true;
}
}
res = max(res, min(noi, noj));
}
}
cout << min(res1, (int)s.size() - 2 * res) << "\n";
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout << fixed;
cout.precision(15);
;
int t = 1;
cin >> t;
for (int i = 1; i <= t; ++i) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct comparator {
bool operator()(int i, int j) { return i > j; }
};
int main() {
;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int n;
cin >> n;
string second;
int a;
priority_queue<int, std::vector<int>, comparator> h;
vector<pair<string, int> > v;
for (int i = 1; i <= n; i++) {
cin >> second;
if (second == "insert") {
cin >> a;
h.push(a);
v.push_back({"insert", a});
} else if (second == "getMin") {
cin >> a;
if (h.size() != 0) {
while (h.size() && h.top() < a) {
v.push_back({"removeMin", 0});
h.pop();
}
if (h.top() != a) {
h.push(a);
v.push_back({"insert", a});
}
v.push_back({"getMin", a});
} else {
v.push_back({"insert", a});
h.push(a);
v.push_back({"getMin", a});
}
} else {
if (h.size() == 0) {
v.push_back({"insert", a});
v.push_back({"removeMin", 0});
} else {
v.push_back({"removeMin", 0});
h.pop();
}
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
if (v[i].first == "removeMin") {
cout << "removeMin" << endl;
} else {
cout << v[i].first << " " << v[i].second << endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d%d", &n, &m);
int x[300], y[300];
for (int i = 0; i < m; ++i) {
scanf("%d%d", &x[i], &y[i]);
}
map<int, int> best;
for (int i = 0; i < m; ++i)
for (int j = i + 1; j < m; ++j) {
long long A = y[j] - y[i];
long long B = x[i] - x[j];
long long C = -x[i] * A - y[i] * B;
bool ok = true;
if (A != 0 && C % A == 0) {
long long d = -C / A;
if (d >= 1 && d <= n) {
int t = 2;
for (int k = 0; k < m; ++k)
if (i != k && j != k && A * x[k] + B * y[k] + C == 0) {
++t;
}
if (ok) {
if (best.count(d) == 0 || best[d] < t) best[d] = t;
}
}
}
}
int sum = 0;
for (map<int, int>::iterator mi = best.begin(); mi != best.end(); ++mi) {
sum += mi->second;
}
sum += n - best.size();
cout << sum << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
int arr[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
}
int o = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + o);
for (i = 0; i < n; i++) {
cout << arr[i] << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string s;
long long a[1005];
long long count = 0;
cin >> s;
for (long long i = 0; i < s.size(); i += 2) {
a[count++] = s[i] - '0';
}
sort(a, a + count);
for (long long i = 0; i < count; i++) {
cout << a[i];
if (i != count - 1) {
cout << "+";
}
}
cout << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[10], n;
char s[120000];
int main() {
scanf("%d", &n);
scanf("%s", s);
for (int i = 0; i < n; ++i) {
if (s[i] == 'A')
a[0]++;
else if (s[i] == 'C')
a[1]++;
else if (s[i] == 'G')
a[2]++;
else
a[3]++;
}
int ret = 0, k = 0;
for (int i = 0; i < 4; ++i) ret = max(ret, a[i]);
for (int i = 0; i < 4; ++i)
if (a[i] == ret) k++;
long long ans = 1;
for (int i = 0; i < n; ++i) {
ans = ans * k % 1000000007;
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long int n;
int t;
cin >> t;
while (t--) {
cin >> n;
vector<long long int> v(n);
for (auto &it : v) cin >> it;
;
long long int sum = accumulate(v.begin(), v.end(), 0);
if (sum == 0)
cout << "NO" << endl;
else {
cout << "YES" << endl;
if (sum > 0)
sort(v.rbegin(), v.rend());
else
sort(v.begin(), v.end());
for (auto it : v) cout << it << ' ';
cout << endl;
;
}
v.clear();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void in() { return; }
template <typename T, typename... Types>
void in(T &a, Types &...b) {
cin >> (a);
in(b...);
}
void o() { return; }
template <typename T, typename... Types>
void o(T a, Types... b) {
cout << (a);
cout << ' ';
o(b...);
}
bool sortin(const pair<long long int, long long int> &e,
const pair<long long int, long long int> &f) {
return (e.first < f.first);
}
bool POT(long long int x) { return x && (!(x & (x - 1))); }
long long int i, j, k, l, m, n, p, q, r, a, b, c, x, y, z, ts, mn = 1e18,
mod = 1e9 + 7;
long long int ar[250005], br[250005], xr[250005], tem[250005];
void insrt(long long int x, long long int val) {
for (i = 1; i * i <= x; i++) {
if (x % i == 0) ar[i] = ar[x / i] = val;
}
ar[1] = 0;
}
long long int conflict(long long int x) {
for (long long int i = 2; i * i <= x; i++) {
if (x % i == 0) {
if (ar[i]) return ar[i];
if (ar[x / i]) return ar[x / i];
}
}
if (ar[x]) return ar[x];
return 0;
}
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
in(n, m);
while (m--) {
char ch;
in(ch, a);
if (ch == '+') {
if (br[a])
o("Already on");
else if (c = conflict(a))
o("Conflict with", c);
else
o("Success"), br[a] = 1, insrt(a, a);
} else {
if (br[a])
o("Success"), br[a] = 0, insrt(a, 0);
else
o("Already off");
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
const long long inf = 1e18;
unsigned int dp[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie();
int n, cnt = 0;
cin >> n;
string s;
cin >> s;
if (n & 1) return cout << 0 << endl, 0;
dp[0] = 1;
for (int i = 0; i < n; i++) {
if (s[i] != '?') {
cnt++;
for (int j = i + 1; j >= 0; j -= 2) dp[j] = (j == 0 ? 0 : dp[j - 1]);
} else {
for (int j = i + 1; j >= 0; j -= 2)
dp[j] = dp[j + 1] + (j == 0 ? 0 : dp[j - 1]);
}
}
int num = (n - 2 * cnt) / 2;
unsigned int ans = dp[0];
for (int i = 0; i < num; i++) ans *= 25;
return cout << ans << endl, 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n;
map<int, int> mp;
for (int i = 0; i < n; i++) {
cin >> x;
mp[x]++;
}
if (mp[2] > 0) {
cout << 2 << " ";
mp[2]--;
}
if (mp[1] > 0) {
cout << 1 << " ";
mp[1]--;
}
for (int i = 0; i < mp[2]; i++) cout << 2 << " ";
for (int i = 0; i < mp[1]; i++) cout << 1 << " ";
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 5;
int ar[MAXN];
int N;
int v[MAXN];
bool f(long double k) {
long double left = 1e27;
bool ok = false;
for (int(i) = (N); (i) >= (1); (i)--) {
if (v[i] > 0) {
if (left <= ar[i] + v[i] * k && ok) return true;
} else {
ok = true;
left = min(left, ar[i] + v[i] * k);
}
}
return false;
}
int main() {
cin >> N;
for (int(i) = (1); (i) <= (N); (i)++)
scanf(" %d", ar + i), scanf(" %d", v + i);
long double l = 0, r = 1e18;
for (int(i) = (1); (i) <= (100); (i)++) {
long double m = (l + r) / 2;
if (f(m))
r = m;
else
l = m;
}
if (r == 1e18)
cout << -1 << endl, 0;
else
cout << setprecision(10) << fixed << r << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
long long read() {
char ch = getchar();
long long x = 0, f = 1;
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 k, n;
string s, t;
map<char, int> mp_s, mp_t, mp;
vector<int> pos[1005], pos_s[1005];
int main() {
k = read();
while (k--) {
n = read();
mp_s.clear();
mp_t.clear();
mp.clear();
for (int i = 0; i <= n; i++) pos[i].clear();
cin >> s >> t;
if (s.size() != t.size()) {
cout << "no" << endl;
continue;
}
int cnt = 0;
for (auto p : s) mp_s[p]++, mp[p]++, pos_s[p].push_back(cnt++);
cnt = 0;
for (auto p : t) mp_t[p]++, pos[p].push_back(cnt++), mp[p]++;
bool flag = 0;
for (auto p : mp)
if (p.second % 2) flag = 1;
if (flag) {
printf("NO\n");
continue;
}
vector<pair<int, int> > ans;
for (int i = 0; i < s.size(); i++) {
if (s[i] != t[i]) {
bool flag = 0;
for (int j = i + 1; j < t.size(); j++) {
if (t[j] == s[i]) {
swap(s[i + 1], t[j]);
swap(s[i + 1], t[i]);
ans.push_back(make_pair(i + 2, j + 1));
ans.push_back(make_pair(i + 2, i + 1));
flag = 1;
break;
}
}
if (!flag) {
for (int j = i + 1; j < s.size(); j++) {
if (s[j] == s[i]) {
swap(s[j], t[i]);
ans.push_back(make_pair(j + 1, i + 1));
break;
}
}
}
}
}
printf("YES\n");
printf("%d\n", ans.size());
for (auto k : ans) printf("%d %d\n", k.first, k.second);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int menor = 0x3f3f3f3f;
long long int resp = 0;
long long int posi;
long long int n;
long long int v[100005];
int main() {
ios_base::sync_with_stdio(false);
;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (v[i] < menor) menor = v[i];
}
resp = menor;
for (int i = 0; i < n; i++) v[i] -= menor;
posi = menor % n;
long long int time = 0;
while (v[posi] - time > 0) {
posi++;
posi %= n;
time++;
}
cout << posi + 1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, a[102][102][102], res, was;
int change[3][3];
int main() {
change[0][0] = 1;
change[1][1] = 1;
change[2][2] = 1;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int q = 1; q <= k; q++) scanf("%1d", &a[i][j][q]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int z = 1; z <= k; z++) {
was = 0;
for (int q = 0; q < 3; q++)
for (int q1 = 0; q1 < 3; q1++) {
if (was) continue;
if (a[i][j][z])
if (a[i + change[0][q]][j + change[1][q]][z + change[2][q]] &&
a[i - change[0][q1]][j - change[1][q1]][z - change[2][q1]])
if (q == q1) {
res++;
was = 1;
} else if (!a[i + change[0][q] - change[0][q1]]
[j + change[1][q] - change[1][q1]]
[z + change[2][q] - change[2][q1]]) {
res++;
was = 1;
}
}
}
printf("%d\n", res);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, p, t, ans, f[100010], g[100010];
inline void div(int a, int b, int &c, int &r) {
c = a / b;
r = a % b;
}
int main() {
scanf("%*d%d%d%d", &n, &p, &t);
g[0] = -t;
for (int i = 1, k1 = 0, k2 = 0, mx = 0; i <= n; ++i) {
int x, y, h, v;
scanf("%d%d", &x, &y);
div(y - x, p, v, h);
g[i] = y - h;
while (g[k1] <= x - t) {
mx = max(mx, f[k1]);
++k1;
}
f[i] = mx + v;
k2 = k1;
int hh, vv;
div(y - g[k2] - t, p, vv, hh);
if (f[k2] + vv > f[i] || (f[k2] + vv == f[i] && y - hh < g[i])) {
f[i] = f[k2] + vv;
g[i] = y - hh;
}
ans = max(ans, f[i]);
}
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int n, h[N], cnt, m, vis[N][2], st, len[N], f = 0, top;
int chu[N], dfn[N], low[N], tim, inq[N], scc, Stack[N], t[N], mx;
struct Edge {
int to, next;
} e[N << 1];
void add(int u, int v) {
e[++cnt] = (Edge){v, h[u]};
h[u] = cnt;
}
void Tarjan(int u) {
dfn[u] = low[u] = ++tim;
Stack[++top] = u;
inq[u] = 1;
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
if (!dfn[v]) {
Tarjan(v);
low[u] = min(low[u], low[v]);
} else {
if (inq[v]) low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
++scc;
while (Stack[top] ^ u) {
inq[Stack[top]] = 0;
t[scc]++;
--top;
}
inq[u] = 0;
--top;
t[scc]++;
mx = max(mx, t[scc]);
}
}
void dfs(int u, int mk) {
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
if (vis[v][mk ^ 1]) continue;
vis[v][mk ^ 1] = 1;
len[++top] = v;
if (!mk && !chu[v]) {
printf("Win\n");
for (int i = 0; i <= top; i++) printf("%d ", len[i]);
exit(0);
}
dfs(v, mk ^ 1);
--top;
}
}
int gi() {
int x = 0, t = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') t = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
return x * t;
}
int main() {
n = gi();
m = gi();
for (int i = 1; i <= n; i++) {
chu[i] = gi();
for (int j = 1; j <= chu[i]; j++) add(i, gi());
}
len[0] = gi();
dfs(len[0], 0);
top = 0;
Tarjan(len[0]);
if (mx > 1) {
printf("Draw\n");
return 0;
}
printf("Lose\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
int a[1000010], st[1000010], top = 0;
long long sum[1000010];
int main() {
int n, i, j;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
for (i = 1; i <= n; i++) {
while (top && (sum[i] - sum[st[top]]) * (i - st[top - 1]) <=
(sum[i] - sum[st[top - 1]]) * (i - st[top]))
top--;
st[++top] = i;
}
for (i = 1; i <= top; i++) {
double ret = (sum[st[i]] - sum[st[i - 1]]) * 1.0 / (st[i] - st[i - 1]);
for (j = st[i - 1] + 1; j <= st[i]; j++) printf("%.9lf\n", ret);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int emp_c[n];
long long max_s[n];
memset(max_s, 0, sizeof(max_s));
long long globalMax = LONG_MIN;
for (int i = 0; i < n; i++) {
cin >> emp_c[i];
for (int j = 0; j < emp_c[i]; j++) {
long long x;
cin >> x;
max_s[i] = max(x, max_s[i]);
}
globalMax = max(globalMax, max_s[i]);
}
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += (globalMax - max_s[i]) * (emp_c[i]);
}
cout << sum;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int z;
int main() {
int b;
string a;
cin >> b >> a;
for (int i = 0; i < b; i++) {
if (a[i] == '1') {
z++;
} else {
cout << z;
z = 0;
}
}
cout << z << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 50;
int t, n, a[N], b[N], p[N], f[N];
vector<int> v[N];
void change(int x, int k) {
for (; x <= n; x += x & -x) f[x] = max(f[x], k);
}
int ask(int x) {
int ret = 0;
for (; x; x ^= x & -x) ret = max(ret, f[x]);
return ret;
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
bool flag = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
v[a[i]].clear();
}
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
v[b[i]].push_back(i);
}
for (int i = n; i; i--) {
if (!v[a[i]].size())
flag = 1;
else {
p[i] = v[a[i]][v[a[i]].size() - 1];
v[a[i]].pop_back();
}
}
for (int i = 1; i <= n; i++) f[i] = 0;
for (int i = 1; i <= n; i++) {
if (ask(a[i] - 1) > p[i]) flag = 1;
change(a[i], p[i]);
}
puts(flag ? "NO" : "YES");
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
typedef long long ll;
const int MAX_SIZE = 5e3 + 1;
ll persons[MAX_SIZE], chairs[MAX_SIZE];
ll dp[MAX_SIZE][MAX_SIZE];
int main()
{
ll n {0}, total_persons {0}, total_chairs {0};
ll occupied {1}, empty {0}, num;
std::cin >> n;
for (ll i = 0; i < n; i++) {
std::cin >> num;
if (num == occupied) {
persons[++total_persons] = i;
}
else {
chairs[++total_chairs] = i;
}
}
for (int p = 1; p <= total_persons; p++) {
for (int c = p; c <= total_chairs; c++) {
dp[p][c] = dp[p - 1][c - 1] + std::abs(persons[p] - chairs[c]);
if (c - 1 >= p) {
dp[p][c] = std::min(dp[p][c],
dp[p][c - 1]);
}
}
}
std::cout << dp[total_persons][total_chairs] << std::endl;
return 0;
} | 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int x, y, k;
cin >> x >> y >> k;
long long int coal = k;
long long int s = coal * y;
s += k;
s--;
long long int total = s / (x - 1);
long long int other = s % (x - 1);
if (other) {
total++;
}
cout << total + k << endl;
}
return 0;
}
| 1 |
#include <iostream>
#include <algorithm>
constexpr int N = 105;
constexpr int p = 1000000007;
int add(int x, int y) { return (x += y) >= p ? x - p : x; }
int sub(int x, int y) { return (x -= y) < 0 ? x + p : x; }
int n, q, k;
int c[N], b[N];
int f[N][N * N];
int s[2][N * N];
int l[N];
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
std::cin >> n;
int sumc = 0;
for (int i = 1; i <= n; ++i) std::cin >> c[i], sumc += c[i];
for (int i = 1; i < n; ++i) std::cin >> b[i];
std::cin >> q;
while (q--) {
std::cin >> k;
int sumb = k;
for (int i = 1; i <= n; ++i) {
l[i] = l[i - 1] + sumb;
sumb += b[i];
}
f[0][0] = 1;
for (int i = 0; i <= sumc; ++i) s[0][i] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < l[i] && j <= sumc; ++j) s[1][j] = 0;
for (int j = std::max(l[i], 0); j <= sumc; ++j) {
f[i][j] = add(f[i][j], sub(s[0][j], j - c[i] - 1 >= 0 ? s[0][j - c[i] - 1] : 0));
s[1][j] = add(f[i][j], j > 0 ? s[1][j - 1] : 0);
}
std::swap(s[0], s[1]);
}
int min = k * n + sumb - k;
int ans = 0;
for (int i = std::max(min, 0); i <= sumc; ++i) {
ans = add(ans, f[n][i]);
}
std::cout << ans << '\n';
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
mt19937 gen_rand;
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &a) {
os << "(";
os << a.first << ", ";
os << a.second;
os << ")";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &a) {
os << "{";
bool was = false;
for (auto &x : a) {
if (was) {
os << ", ";
}
was = true;
os << x;
}
os << "}";
return os;
}
template <typename T>
inline void debug(const char *sdbg, T x) {
cerr << "The value of " << sdbg << " is " << x << "\n";
};
template <typename T, typename... U>
inline void debug(const char *sdbg, T h, U... t) {
cerr << "The value of ";
while (*sdbg != ',') {
cerr << *sdbg++;
}
cerr << " is " << h << "\n";
debug(sdbg + 1, t...);
cerr << "\n";
}
template <typename T>
T abs(T x) {
if (x < 0) {
return -x;
} else {
return x;
}
}
template <typename T>
T sqr(T x) {
return x * x;
};
const long long INF_FOR_SHORT_TIME = (long long)(1e9);
template <typename T>
T mul(T a, T b, T m) {
if (a <= INF_FOR_SHORT_TIME && b <= INF_FOR_SHORT_TIME) {
return (a * b) % m;
}
T q = (long long)((long double)a * (long double)b / (long double)m);
T r = a * b - q * m;
return (r + 5 * m) % m;
}
template <typename T, typename U>
T binpow(T a, U n, T MOD) {
T res = 1;
while (n) {
if (n & 1) {
res = mul(res, a, MOD);
}
a = mul(a, a, MOD);
n >>= 1;
}
return res;
};
template <typename T>
int sign(T x) {
if (x < 0) {
return -1;
} else if (x > 0) {
return 1;
} else {
return 0;
}
};
template <typename T>
T gcd(T a, T b) {
if (a > b) {
swap(a, b);
}
while (a) {
b %= a;
swap(a, b);
}
return b;
};
template <typename T>
bool uin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
};
template <typename T>
bool uax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
};
int n, m, x;
void read() {
cin >> m >> x;
n = (1 << m);
}
void solve() {
vector<int> a, used(n, 0);
if (x < n) used[x] = 1;
for (int i = 1; i < n; ++i) {
if (used[i]) {
continue;
}
a.push_back(i);
if ((i ^ x) < n) used[i ^ x] = 1;
}
cout << int((a).size()) << "\n";
for (int i = 0; i < int((a).size()); ++i) {
if (i > 0) {
cout << (a[i] ^ a[i - 1]) << " ";
} else {
cout << a[i] << " ";
}
}
}
int main() {
gen_rand.seed(time(0));
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long a[40005], b[40005];
signed main() {
long long n, m, k;
cin >> n >> m >> k;
long long cnt = 0;
for (long long i = 1; i <= n; i++) {
long long x;
scanf("%lld", &x);
if (x == 1) {
cnt++;
a[1]++;
a[cnt + 1]--;
} else {
cnt = 0;
}
}
cnt = 0;
for (long long j = 1; j <= m; j++) {
long long x;
scanf("%lld", &x);
if (x == 1) {
cnt++;
b[1]++;
b[cnt + 1]--;
} else {
cnt = 0;
}
}
for (long long i = 1; i <= n; i++) {
a[i] += a[i - 1];
}
for (long long i = 1; i <= m; i++) {
b[i] += b[i - 1];
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
if (k % i == 0 && k / i <= m) {
ans += a[i] * b[k / i];
}
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[105];
int main() {
ios_base::sync_with_stdio(0);
;
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
ans += abs(temp);
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long maxn = 32;
typedef long long myarray[maxn];
long long answer;
long long now[maxn];
long long a[maxn];
long long k[maxn];
long long b[maxn];
long long r;
long total, n;
long p[maxn];
void solve(long ost, long inl) {
if (ost % 2 == 0) {
long long nnew = 0;
for (long j = 1; j <= n; j++) nnew += now[j] * k[j];
answer = max(answer, nnew);
}
if (ost == 0) return;
myarray temp, temp2;
if (inl != 1) {
memcpy(temp, now, sizeof(now));
for (long j = 1; j <= n; j++) now[j] = now[j] ^ b[j];
solve(ost - 1, inl + 1);
memcpy(now, temp, sizeof(now));
}
memcpy(temp, now, sizeof(now));
for (long j = 1; j <= n; j++) temp2[j] = now[p[j]] + r;
memcpy(now, temp2, sizeof(temp2));
solve(ost - 1, 0);
memcpy(now, temp, sizeof(temp));
}
int main() {
cin >> n >> total >> r;
long q;
for (q = 1; q <= n; q++) {
cin >> a[q];
now[q] = a[q];
}
for (q = 1; q <= n; q++) cin >> b[q];
for (q = 1; q <= n; q++) cin >> k[q];
for (q = 1; q <= n; q++) cin >> p[q];
answer = -inf;
solve(total, 0);
cout << answer;
}
| 6 |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001; //check the limits, dummy
void solve() {
int N; cin >> N;
string S, T; cin >> S >> T;
F0R(i, N) {
if (T[i] != '0') {
if (S[i] == '0') {
F0R(j, i) cout << '0';
FOR(j, i, N) cout << '1';
cout << nl;
} else {
if (S != T) {
F0Rd(j, N) {
if (S[j] == '0') {
S[j]++; break;
}
S[j]--;
}
if (S != T) T[N-1] = '1';
}
cout << T << nl;
}
return;
}
}
cout << T << nl;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int T = 1;
// cin >> T;
while(T--) {
solve();
}
return 0;
}
// read the question correctly (ll vs int)
// template by bqi343
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
struct Point {
long long x, y;
} P[maxn];
int main() {
int n;
cin >> n;
long long xmax = -0x3f3f3f3f, xmin = 0x3f3f3f3f, ymax = -0x3f3f3f3f,
ymin = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) {
cin >> P[i].x >> P[i].y;
xmax = max(xmax, P[i].x);
xmin = min(xmin, P[i].x);
ymax = max(ymax, P[i].y);
ymin = min(ymin, P[i].y);
}
long long ans = -1;
for (int i = 1; i <= n; i++) {
ans = max(ans, 2 * (max(P[i].x - xmin, xmax - P[i].x) +
max(P[i].y - ymin, ymax - P[i].y)));
}
cout << ans << " ";
ans = 2 * (xmax - xmin + ymax - ymin);
for (int i = 4; i <= n; i++) cout << ans << " ";
cout << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, m, c = 0;
string s = "qwertyuiopasdfghjkl;zxcvbnm,./ ", x, y;
cin >> x >> y;
string f = "";
for (i = 0; i != y.size(); i++) {
for (j = 0; j < s.size(); j++) {
if (x == "R" && s[j] == y[i]) f += s[j - 1];
if (x == "L" && s[j] == y[i]) f += s[j + 1];
}
}
cout << f;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
const long long INF = (1LL << 60) - 1;
long long a[MAXN], b[MAXN];
struct func {
long long l, m, r;
func() {}
func(long long _l, long long _m, long long _r) : l(_l), m(_m), r(_r) {}
func operator+(const func &t) const {
return func(max(l, t.l), max(m, t.m), max(r, t.r));
}
long long get(long long x) { return max({-2 * x, m, 2 * x + r}); }
} s[MAXN << 2];
inline void push_up(int n) { s[n] = s[n << 1] + s[n << 1 | 1]; }
void update(int p, int l, int r, int n) {
if (l == r) {
long long t = fabs(b[l]) + fabs(b[l - 1]);
s[n] = func(-b[l] + b[l - 1] - t, abs(b[l] + b[l - 1]) - t,
b[l] - b[l - 1] - t);
return;
}
int m = (l + r) / 2;
if (p <= m)
update(p, l, m, n << 1);
else
update(p, m + 1, r, n << 1 | 1);
push_up(n);
}
func query(int ql, int qr, int l, int r, int n) {
if (ql == l && qr == r) return s[n];
int m = (l + r) / 2;
if (qr <= m) return query(ql, qr, l, m, n << 1);
if (ql > m) return query(ql, qr, m + 1, r, n << 1 | 1);
return query(ql, m, l, m, n << 1) + query(m + 1, qr, m + 1, r, n << 1 | 1);
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
long long sum = 0;
for (int i = 1; i < n; i++) b[i] = a[i] - a[i + 1], sum += abs(b[i]);
for (int i = 2; i < n; i++) update(i, 2, n - 1, 1);
int q;
scanf("%d", &q);
while (q--) {
int ty, l, r;
long long x;
scanf("%d%d%d%lld", &ty, &l, &r, &x);
if (ty == 1) {
long long res = -INF;
if (l == 1) res = max(res, sum - abs(b[1]) + abs(b[1] + x)), l++;
if (r == n) res = max(res, sum - abs(b[n - 1]) + abs(b[n - 1] - x)), r--;
if (l <= r) res = max(res, sum + query(l, r, 2, n - 1, 1).get(x));
printf("%lld\n", res);
} else {
if (l > 1) {
sum -= abs(b[l - 1]), b[l - 1] -= x, sum += abs(b[l - 1]);
if (l - 1 > 1) update(l - 1, 2, n - 1, 1);
update(l, 2, n - 1, 1);
}
if (r < n) {
sum -= abs(b[r]), b[r] += x, sum += abs(b[r]);
if (r + 1 < n) update(r + 1, 2, n - 1, 1);
update(r, 2, n - 1, 1);
}
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3005;
const long long INF = 1e16;
const int MOD = 998244353;
long long b[MAXN], w[MAXN], size[MAXN];
pair<long long, long long> dp[MAXN][MAXN], tmp[MAXN];
int n, m, T;
vector<int> e[MAXN];
void dfs(int u, int fa) {
dp[u][0] = make_pair(0, b[u]);
size[u] = 1;
for (int i = 0; i < e[u].size(); ++i) {
int v = e[u][i];
if (v == fa) continue;
dfs(v, u);
for (int j = 0; j <= min((long long)m, size[u] + size[v]); ++j)
tmp[j] = make_pair(0, -INF);
for (int j = 0; j <= min((long long)m, size[u]); ++j)
for (int k = 0; k <= min((long long)(m - j), size[v]); ++k) {
tmp[k + j] =
max(tmp[k + j], make_pair(dp[v][k].first + dp[u][j].first,
dp[v][k].second + dp[u][j].second));
tmp[k + j + 1] = max(
tmp[k + j + 1],
make_pair(dp[v][k].first + dp[u][j].first + (dp[v][k].second > 0),
dp[u][j].second));
}
size[u] += size[v];
for (int j = 0; j <= min((long long)m, size[u]); ++j) dp[u][j] = tmp[j];
}
}
int main() {
scanf("%d", &T);
while (T) {
--T;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
size[i] = 0, tmp[i] = make_pair(0, 0), e[i].clear();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) dp[i][j] = make_pair(0, 0);
for (int i = 1; i <= n; ++i) cin >> w[i];
for (int i = 1; i <= n; ++i) {
cin >> b[i];
b[i] -= w[i];
}
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
e[u].push_back(v), e[v].push_back(u);
}
dfs(1, 0);
cout << dp[1][m - 1].first + (dp[1][m - 1].second > 0) << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
int F[(1 << 20) + 5];
int sta, now;
char s[1000005];
std::map<char, int> mp;
int count(int x) {
int num = 0;
while (x) {
if (x % 2) ++num;
x /= 2;
}
return num;
}
int main() {
int num = 0;
scanf("%s", s + 1);
int len = strlen(s + 1);
for (int i = 1; i <= len; i++) {
int l = i;
int now = 0;
while (l <= i + 19 && l <= len) {
if (!mp[s[l]]) mp[s[l]] = ++num;
int x = mp[s[l]];
x = 1 << (x - 1);
if (now & x)
break;
else {
now ^= x;
F[now] = count(now);
l++;
}
}
}
int sta = 1 << num;
sta--;
for (int i = 1; i <= sta; i++) {
for (int j = 1; j <= num; j++)
if (i & (1 << (j - 1))) F[i] = std::max(F[i], F[i ^ (1 << (j - 1))]);
}
int ans = 0;
for (int i = 1; i <= sta; i++) {
ans = std::max(ans, F[sta - i] + F[i]);
}
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T load() {
T r;
cin >> r;
return r;
}
template <typename T>
vector<T> loadMany(int n) {
vector<T> rs(n);
generate(rs.begin(), rs.end(), &load<T>);
return rs;
}
struct Dummyf {
template <typename... Ts>
void operator()(Ts&&...) {}
};
struct Graph {
Graph(int n) : edges(n) {}
void addEdge1(int a, int b) { edges[a].push_back(b); }
void addEdge2(int a, int b) {
addEdge1(a, b);
addEdge1(b, a);
}
int size() const { return (int)edges.size(); }
template <typename Pre = Dummyf, typename Post = Dummyf,
typename PreE = Dummyf, typename PostE = Dummyf,
typename FailE = Dummyf>
void DFS(int source, Pre pre, Post post, PreE pree, PostE poste,
FailE faile) const {
auto visit = vector<bool>(size(), false);
implDFS(source, visit, pre, post, pree, poste, faile);
}
template <typename Pre = Dummyf, typename Post = Dummyf,
typename PreE = Dummyf, typename PostE = Dummyf,
typename FailE = Dummyf>
void implDFS(int v, vector<bool>& visit, Pre pre, Post post, PreE pree,
PostE poste, FailE faile) const {
visit[v] = true;
pre(v);
for (auto u : edges[v])
if (not visit[u])
pree(v, u), implDFS(u, visit, pre, post, pree, poste, faile),
poste(v, u);
else
faile(v, u);
post(v);
}
vector<vector<int>> edges;
};
Graph loadEdges(int n, int m) {
auto g = Graph(n);
for (auto i = 0; i < m; ++i) g.addEdge2(load<int>() - 1, load<int>() - 1);
return g;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
auto n = load<int>();
auto s = load<int>();
auto tree = loadEdges(n, n - 1);
auto leaves = 0;
for (auto v = 0; v < n; ++v)
if (tree.edges[v].size() == 1) ++leaves;
cout << fixed << setprecision(18) << ((long double)(2 * s) / leaves) << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, d, modulo;
int inv[99];
const int N(1111);
int dp[N][N][15];
int f(int i, int j, int k) {
if (dp[i][j][k] != -1) {
return dp[i][j][k];
} else if (i == 0 && j == 0 && k == 0) {
return 1;
} else if (j == 0) {
return 0;
} else if (dp[i][j][k] != -1) {
return dp[i][j][k];
} else {
int& res(dp[i][j][k]);
res = 0;
int x(j == 1 ? 1 : f(j - 1, j - 1, d));
int lst(1);
for (int o(0); o * j <= i && o <= k; o++) {
res = (res + (long long)f(i - o * j, j - 1, k - o) * lst) % modulo;
lst = (long long)lst * (x + o + modulo) % modulo * inv[o + 1] % modulo;
}
return res;
}
}
int pw(int x, int n) {
int res(1);
while (n) {
if (n & 1) {
res = (long long)res * x % modulo;
}
x = (long long)x * x % modulo;
n /= 2;
}
return res;
}
int main() {
scanf("%d%d%d", &n, &d, &modulo);
d--;
if (d == 1 || n <= 2) {
printf("1\n");
return 0;
}
for (int i(1); i <= d + 1; i++) {
inv[i] = pw(i, modulo - 2);
}
memset(dp, -1, sizeof(dp));
dp[0][0][0] = 1;
f(n - 1, n - 1, d + 1);
for (int i(0); i <= n - 1; i++) {
f(i, i, d + 1);
f(i, i, d);
}
int ans(dp[n - 1][n - 1][d + 1]);
dp[0][0][d] = 0;
for (int i(1); i <= n - i; i++) {
ans =
(ans -
(long long)dp[i - 1][i - 1][d] * dp[n - i - 1][n - i - 1][d] % modulo +
modulo) %
modulo;
}
if (n % 2 == 0) {
ans = (ans + (long long)(dp[n / 2 - 1][n / 2 - 1][d]) *
(dp[n / 2 - 1][n / 2 - 1][d] + 1) / 2) %
modulo;
}
printf("%d\n", ans);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int maxn = 50;
vector<int> v1, v2;
int n, mod, Size;
int a[maxn];
int main() {
scanf("%d %d", &n, &mod);
int t = n / 2, sum, ans = 0, p;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 0; i < (1 << t); ++i) {
sum = 0;
for (int j = 0; j < t; ++j) {
if (i & (1 << j)) {
sum += a[j];
sum %= mod;
}
}
v1.push_back(sum);
}
for (int i = 0; i < (1 << (n - t)); ++i) {
sum = 0;
for (int j = 0; j < n - t; ++j) {
if (i & (1 << j)) {
sum += a[t + j];
sum %= mod;
}
}
v2.push_back(sum);
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
v1.erase(unique(v1.begin(), v1.end()), v1.end());
v2.erase(unique(v2.begin(), v2.end()), v2.end());
Size = v1.size();
for (int i = 0; i < Size; ++i) {
p = upper_bound(v2.begin(), v2.end(), mod - 1 - v1[i]) - v2.begin();
ans = max(ans, v1[i] + v2[p - 1]);
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct InputReader {
char buf[1000001];
int p;
inline InputReader() { p = 1000001; }
inline void Flush() {
p = 0;
fread(buf, 1, 1000001, stdin);
}
inline char C() {
if (p >= 1000001) Flush();
return buf[p++];
}
inline char Readnum() {
char ch = C();
while (!isdigit(ch) && ch != '-') ch = C();
return ch;
}
inline void Readalpha(char &c) {
c = C();
while (!isalpha(c)) c = C();
}
int operator()() {
int ans = 0, fu = 1;
char ch = Readnum();
if (ch == '-') fu = -1, ch = C();
while (ch >= '0' && ch <= '9') {
ans = ans * 10 + ch - '0';
ch = C();
}
return ans * fu;
}
long long Readll() {
long long ans = 0LL, fu = 1LL;
char ch = Readnum();
if (ch == '-') fu = -1LL, ch = C();
while (ch >= '0' && ch <= '9') {
ans = ans * 10LL + ch - '0';
ch = C();
}
return ans * fu;
}
inline void Readstring(string &x) {
x.clear();
char ch = C();
while (!isdigit(ch) && !isalpha(ch) && ch != '#' && ch != '.') ch = C();
while (isdigit(ch) || isalpha(ch) || ch == '#' || ch == '.') {
x += ch;
ch = C();
}
}
inline void Readchstring(char s[]) {
int len = 0;
char ch = C();
while (!isdigit(ch) && !isalpha(ch)) ch = C();
while (isdigit(ch) || isalpha(ch)) {
s[len++] = ch;
ch = C();
}
s[len] = '\0';
}
inline void Specialread(char &c) {
c = C();
while (!isdigit(c) && !isalpha(c) && c != '#' && c != '.' && c != '=' &&
c != 'B')
c = C();
}
} In;
inline void Read(int &x) { x = In(); }
inline void Read(int &x, int &y) {
x = In();
y = In();
}
inline void Read(int &x1, int &x2, int &x3) {
x1 = In();
x2 = In();
x3 = In();
}
inline void Read(int &x1, int &x2, int &x3, int &x4) {
x1 = In();
x2 = In();
x3 = In();
x4 = In();
}
inline void Read(long long &x) { x = In.Readll(); }
inline void Read(long long &x, long long &y) {
x = In.Readll();
y = In.Readll();
}
inline void Read(long long &x1, long long &x2, long long &x3) {
x1 = In.Readll();
x2 = In.Readll();
x3 = In.Readll();
}
inline void Read(long long &x1, long long &x2, long long &x3, long long &x4) {
x1 = In.Readll();
x2 = In.Readll();
x3 = In.Readll();
x4 = In.Readll();
}
template <typename T>
void Read(T a[], int st, int ed) {
for (int(i) = (st); (i) <= (ed); ++(i)) Read(a[i]);
}
inline void FILEIO() {}
inline void FILEIO(string pname) {
freopen((pname + ".in").c_str(), "r", stdin);
freopen((pname + ".out").c_str(), "w", stdout);
}
inline void FILEIO_OICONTEST(string pname) {
freopen((pname + ".in").c_str(), "r", stdin);
freopen((pname + ".out").c_str(), "w", stdout);
}
void Printtime() {}
void END() {
Printtime();
exit(0);
}
template <typename T>
void END(T mes) {
cout << mes << endl;
END();
}
template <typename T>
void Print(T a[], int s, int t, char sp = ' ', char ed = '\n') {
if (s > t) return;
for (int i = s; i < t; i++) cout << a[i] << sp;
cout << a[t] << ed;
cout.flush();
}
template <typename T>
void Print(T a, int s = 0, int t = -1, char sp = ' ', char ed = '\n') {
if (t == -1) t = a.size() - 1;
for (int i = s; i <= t; i++) cout << a[i] << sp;
cout << ed;
cout.flush();
}
long long Pow(long long a, long long b, long long p = (998244353LL)) {
long long ret = 1;
for (; b; a = a * a % p, b >>= 1LL)
if (b & 1LL) ret = ret * a % p;
return ret;
}
long long Inv(long long a, long long p = (998244353LL)) {
return Pow(a, p - 2LL, p);
}
inline long long A2(long long x) { return x * (x - 1); }
int n;
int a[5010];
int diffa[5010], difftop = 0;
int cnt[5010];
long long fact[5010], invfact[5010];
void Init() {
fact[0] = invfact[0] = 1;
for (int(i) = 1; (i) <= (n); ++(i)) {
fact[i] = fact[i - 1] * i % (998244353LL);
invfact[i] = Inv(fact[i]);
}
}
long long sel[5010][5010];
void Pre() {
sel[0][0] = 1;
for (int(i) = 1; (i) <= (difftop); ++(i)) {
int unum = diffa[i];
for (int(j) = (0); (j) <= (i); ++(j)) {
sel[i][j] = sel[i - 1][j];
if (j) {
sel[i][j] += sel[i - 1][j - 1] * cnt[unum] % (998244353LL);
sel[i][j] = (sel[i][j]) >= (998244353LL) ? ((sel[i][j]) - (998244353LL))
: (sel[i][j]);
}
}
}
}
long long Solve(int x) {
int mxp = 0, le = 0, geq = 0;
for (int(i) = 1; (i) <= (difftop); ++(i)) {
if (diffa[i] < x) {
le += cnt[diffa[i]];
mxp = i;
} else
geq += cnt[diffa[i]];
}
geq -= 2;
long long ans = 0;
for (int(fr) = (0); (fr) <= (le); ++(fr)) {
long long tans = sel[mxp][fr] * fact[le - fr + geq] % (998244353LL);
ans += tans;
ans = (ans) >= (998244353LL) ? ((ans) - (998244353LL)) : (ans);
}
return ans;
}
int main() {
FILEIO();
Read(n);
for (int(i) = 1; (i) <= (n); ++(i)) Read(a[i]);
sort(a + 1, a + 1 + n);
for (int(i) = 1; (i) <= (n); ++(i)) {
if (!cnt[a[i]]) diffa[++difftop] = a[i];
++cnt[a[i]];
}
Init();
Pre();
long long ans = 0;
for (int(i) = 1; (i) <= (difftop); ++(i)) {
int unum = diffa[i];
if (cnt[unum] < 2) continue;
long long tans = A2(cnt[unum]) * Solve(unum) % (998244353LL);
ans += tans;
ans = (ans) >= (998244353LL) ? ((ans) - (998244353LL)) : (ans);
}
cout << ans * invfact[n] % (998244353LL) << endl;
END();
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 50;
struct node {
long long a, b, cnt;
} p[N];
bool cmp(node x, node y) { return x.a < y.a; }
long long sum[N];
int vis[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &p[i].a);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &p[i].b);
}
sort(p + 1, p + n + 1, cmp);
int cnt = 0;
int t;
for (int i = 1; i <= n; i = t) {
t = i + 1;
p[++cnt] = p[i];
p[cnt].cnt = 1;
while (t <= n && p[t].a == p[i].a) {
p[cnt].b += p[t].b;
p[cnt].cnt++;
t++;
}
}
long long ans = 0;
for (int i = 1; i <= cnt; i++) {
if (p[i].cnt >= 2) {
ans += p[i].b;
vis[i] = 1;
for (int j = 1; j < i; j++) {
if (vis[j]) continue;
if ((p[j].a | p[i].a) == p[i].a) {
ans += p[j].b;
vis[j] = 1;
}
}
}
}
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, cnt;
long long a, sum, ans[100010];
queue<long long> q[100010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a);
for (int j = 60; j >= 0; j--) {
if (a & (1LL << j)) {
q[j].push(a);
break;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 60; j++) {
if (!(sum & (1LL << j)) & !q[j].empty()) {
sum ^= q[j].front();
ans[++cnt] = q[j].front();
q[j].pop();
break;
}
}
}
if (cnt != n)
printf("No\n");
else {
printf("Yes\n");
for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int len, ans;
bool a[100010], b[100010];
char x[100010] = {};
void input() {
scanf("%s", &x);
len = strlen(x);
for (int i = 1; i <= len; ++i) a[i] = x[i - 1] - '0';
scanf("%s", &x);
for (int i = 1; i <= len; ++i) b[i] = x[i - 1] - '0';
}
void work() {
for (int i = 1; i <= len; ++i)
if (a[i] ^ b[i]) ++ans;
if (ans & 1) {
printf("impossible\n");
return;
}
ans >>= 1;
for (int i = 1; i <= len; ++i)
if (ans && (a[i] ^ b[i])) {
printf("%d", !a[i]);
--ans;
} else
printf("%d", a[i]);
printf("\n");
}
int main() {
input();
work();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const int maxn = 200050;
int a[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i), a[i] ^= a[i - 1];
if (!a[n]) {
puts("-1");
return 0;
}
for (int x = 1, y = 0; x <= n && y <= 31;) {
for (int i = x; i <= n; ++i) {
if ((a[i] >> y) & 1) {
swap(a[x], a[i]);
break;
}
}
if ((a[x] >> y) % 2 == 0) {
++y;
continue;
}
for (int i = x + 1; i <= n; ++i) {
if ((a[i] >> y) & 1) a[i] ^= a[x];
}
++x, ++y;
}
int ans = 0;
for (int i = 1; i <= n; ++i) ans += (a[i] != 0);
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count1 = 0, count2 = 0;
string str;
cin >> n;
cin >> str;
for (int i = 0; i < n; i++) {
if (str[i] == 'S' && str[i + 1] == 'F')
count1++;
else if (str[i] == 'F' && str[i + 1] == 'S')
count2++;
}
if (count1 > count2)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
int t[200010];
int main() {
int n, k;
cin >> n >> k;
int tol = 0, ans = 0;
int pre = -1, last = -1;
for (int i = 1; i <= n; i++) {
scanf("%d", &t[i]);
if (t[i] < 0) {
last = i;
ans += 2;
tol++;
if (pre != -1) vec.push_back(i - pre - 1);
pre = i;
}
}
k -= tol;
if (k < 0) {
puts("-1");
} else {
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++) {
if (k - vec[i] >= 0) {
k -= vec[i];
ans -= 2;
} else
break;
}
if (n - last <= k) ans--;
printf("%d\n", ans);
}
return 0;
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.