solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
queue<pair<long long, long long>> que;
map<long long, bool> st;
long long ans, a[N], cnt;
long long t = 0, n, m;
void solve() {
while (que.size()) {
auto t = que.front();
que.pop();
if (st[t.second]) continue;
st[t.second] = true, a[cnt++] = t.second, ans += t.first;
if (!st[t.second - 1]) que.push({t.first + 1, t.second - 1});
if (!st[t.second + 1]) que.push({t.first + 1, t.second + 1});
if (cnt == m) break;
}
}
signed main() {
for (cin >> n >> m; n--; cin >> a[t++], st[a[t - 1]] = true)
;
for (long long(i) = 0; (i) < (t); (i)++)
que.push({1, a[i] - 1}), que.push({1, a[i] + 1});
solve();
cout << ans << '\n';
for (long long(i) = 0; (i) < (cnt); (i)++) cout << a[i] << ' ';
return 0;
}
| 5 |
#include <bits/stdc++.h>
template <typename tp>
inline tp abs(const tp& x) {
return (x > 0) ? x : -x;
}
template <typename tp>
inline tp max(const tp& x, const tp& y) {
return (x > y) ? x : y;
}
template <typename tp>
inline tp min(const tp& x, const tp& y) {
return (x < y) ? x : y;
}
template <typename tp>
inline bool chkmax(tp& x, const tp& y) {
return (x >= y) ? (false) : (x = y, true);
}
template <typename tp>
inline bool chkmin(tp& x, const tp& y) {
return (x <= y) ? (false) : (x = y, true);
}
template <typename tp>
inline tp scanf(tp& in) {
in = 0;
char ch = getchar();
tp f = 1;
for (; ch > '9' || ch < '0'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) in = in * 10 + ch - '0';
return in *= f;
}
static const int maxn = 50 + 11;
static const int maxm = 6e3 + 11;
static const double inf = 1e15;
static const double eps = 1e-9;
int n;
int R;
int fi[maxn];
int si[maxn];
int pi[maxn];
double f[maxn][maxm];
inline int cmp(const double& x, const double& y) {
return (abs(x - y) < eps) ? 0 : (x > y) ? 1 : -1;
}
inline double min(const double& x, const double& y) {
return (cmp(x, y) == -1) ? x : y;
}
inline bool chkmin(double& x, const double& y) {
return (cmp(x, y) != 1) ? (false) : (x = y, true);
}
inline bool chk(const double& ept) {
for (int i = (n), iend = (1); i >= (iend); --i) {
for (register int j = (R + 1), jend = (R + si[i]); j <= (jend); ++j)
f[i + 1][j] = ept;
for (register int j = (0), jend = (R); j <= (jend); ++j) {
double t1 = (f[i + 1][j + fi[i]] + fi[i]) * pi[i];
double t2 = (f[i + 1][j + si[i]] + si[i]) * (100 - pi[i]);
f[i][j] = min((t1 + t2) / 100.0, ept);
}
}
return cmp(f[1][0], ept) == -1;
}
int main() {
scanf(n);
scanf(R);
for (register int i = (1), iend = (n); i <= (iend); ++i)
scanf(fi[i]), scanf(si[i]), scanf(pi[i]);
double l = 0.0, r = inf, ans;
while ((r - l) > eps) {
double mid = (l + r) / 2.0;
if (chk(mid))
ans = mid, r = mid;
else
l = mid;
}
printf("%.10lf\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, s, p, a[105];
int main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
sort(a + 1, a + n + 1);
for (int i = 1; i < n; i++) p += a[i];
if (p <= s)
puts("YES");
else
puts("NO");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int N;
pair<int, int> P[52];
int Xn;
vector<int> X;
map<int, int> dx;
vector<pair<int, int> > V[52];
long long cc[52][52][52 * 100];
long long dp(int n, int r, int sum) {
if (n == -1) return sum == 0 ? 0 : 1e18;
long long &ret = cc[n][r][sum];
if (ret != -1) return ret;
ret = 1e18;
if (r >= V[n].size()) ret = min(ret, dp(n - 1, r - (int)V[n].size(), sum));
int tmp = 0;
int cnt = 0;
long long cum = 0;
for (int i = (int)V[n].size() - 1; i >= 0; i--) {
tmp += V[n][i].first;
cnt++;
cum += P[V[n][i].second].first;
if (tmp <= sum && r >= (int)V[n].size() - cnt)
ret =
min(ret, dp(n - 1, r + 2 * cnt - (int)V[n].size(), sum - tmp) + cum);
}
return ret;
}
int main() {
cout << fixed;
cout.precision(0);
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &P[i].first);
X.push_back(P[i].first);
}
for (int i = 0; i < N; i++) {
scanf("%d", &P[i].second);
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end()) - X.begin());
Xn = X.size();
for (int i = 0; i < Xn; i++) dx[X[i]] = i;
for (int i = 0; i < N; i++) V[dx[P[i].first]].push_back({P[i].second, i});
for (int i = 0; i < 52; i++) {
sort(V[i].begin(), V[i].end());
}
double ans = 1e18;
memset(cc, -1, sizeof(cc));
for (int i = 1; i <= 100 * N; i++) {
ans = min(ans, (double)dp(Xn - 1, 0, i) / i);
}
ans *= 1000;
cout << ceil(ans);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int bPow(int v, int u, int mod) {
if (!u) return 1;
int res = bPow(v, u / 2, mod);
res = (res * 1ll * res) % mod;
if (u & 1) res = (res * 1ll * v) % mod;
return res;
}
bool fb(long long x) { return 0; }
int bSearch(int l, int r) {
while (r - l > 1) {
int m = (l + r) / 2;
if (fb(m))
l = m;
else
r = m;
}
if (fb(l + 1)) l++;
if (!fb(l)) l = 0;
return l;
}
void faster() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
if (0) {
freopen(
"a"
".in",
"r", stdin);
freopen(
"a"
".out",
"w", stdout);
}
}
vector<int> prime;
bool notprime[1000010];
void resh() {
int n = 1000010 - 10;
for (int i = 2; i < n; i++)
if (!notprime[i]) {
prime.push_back(i);
for (int j = i; j < n; j += i) notprime[j] = 1;
}
}
int is_comp(long long p) {
for (int i = 0; prime[i] * 1ll * prime[i] <= p; i++) {
if (p % prime[i] == 0) return prime[i];
}
return 0;
}
bool is_prime(long long p) {
for (int i = 0; prime[i] * 1ll * prime[i] <= p; i++) {
if (p % prime[i] == 0) return 0;
}
return 1;
}
int gcd(int a, int b) {
if (a > b) swap(a, b);
if (!a) return b;
return gcd(b % a, a);
}
int n, m, p;
string s, c;
vector<string> v[4];
void ask(int l, int r) {
cout << "? " << l << " " << r << endl;
cout.flush();
m = (r - l + 1) * (r - l + 2) / 2;
for (int i = 0; i < m; i++) {
cin >> c;
sort(c.begin(), c.end());
v[p].push_back(c);
}
}
void print() {
cout << "! " << s << endl;
cout.flush();
}
int w[33], W[33], cnt[33], q[33];
void ch() {
for (int j = 0; j < 26; j++) w[j] = 0;
}
void fs(string& a) {
for (int i = 0; i < a.size(); i++) w[a[i] - 'a']++;
}
void half() {
map<string, int> ma;
for (int i = 0; i < v[0].size(); i++) ma[v[0][i]]++;
for (int i = 0; i < v[1].size(); i++) ma[v[1][i]]--;
vector<string> temp[111];
for (auto& x : ma)
for (int i = 0; i < x.second; i++)
temp[x.first.size() - 1].push_back(x.first);
m = n - n / 2;
for (int i = 0; i < m; i++) {
string t = temp[i][0];
for (int j = 0; j <= i; j++) {
w[t[j] - 'a']--;
if (w[t[j] - 'a'] < 0) {
s += t[j];
break;
}
}
ch();
for (int j = 0; j < s.size(); j++) w[s[j] - 'a']++;
}
}
void solve() {
half();
vector<string> temp[111];
for (int i = 0; i < v[2].size(); i++)
temp[v[2][i].size() - 1].push_back(v[2][i]);
c = temp[n - 1][0];
for (int i = 0; i < c.size(); i++) W[c[i] - 'a']++;
string t = "", cur = "";
m = n - n / 2 - 1;
int l = 0;
for (int i = n - 2; i >= m; i--) {
ch();
cnt[s[l] - 'a']++;
for (int j = 0; j < temp[i].size(); j++) fs(temp[i][j]);
for (int j = 0; j < 26; j++) q[j] = (l + 2) * W[j] - cnt[j];
for (int j = 0; j < 26; j++)
if (w[j] < q[j]) cur = j + 'a';
cnt[cur[0] - 'a']++;
t += cur;
l++;
for (int j = 0; j < l; j++) {
cnt[s[j] - 'a']++;
cnt[t[j] - 'a']++;
}
}
reverse(t.begin(), t.end());
s += t;
}
int main() {
faster();
cin >> n;
if (n < 4) {
for (int i = 1; i <= n; i++) {
ask(i, i);
s += c;
}
print();
return 0;
}
ask(1, n - n / 2);
p++;
ask(2, n - n / 2);
p++;
ask(1, n);
solve();
print();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
int sum = 0;
int mx = 0;
int mn = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
mx = max(mx, sum);
mn = min(mn, sum);
}
cout << max(0, w - mx + mn + 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y) {
long long res = 1;
x = x % 1000000007;
while (y > 0) {
if (y & 1) {
res = (res * x) % 1000000007;
}
y = y >> 1;
x = (x * x) % 1000000007;
}
return res;
}
long long inv(long long x) { return power(x, 1000000007 - 2); }
long long n, m, arr[1001], mx = (long long)4e18, mn = -(long long)4e18, ans,
dp[1001][20001];
long long fun(long long pos, long long sm) {
if (pos >= n) {
return 0;
}
if (dp[pos][10000 + sm] != -1) {
return dp[pos][10000 + sm];
}
long long ways = 0;
if (sm + arr[pos] == 0) {
ways = (ways + 1) % 1000000007;
}
if (sm - arr[pos] == 0) {
ways = (ways + 1) % 1000000007;
}
ways = (ways + fun(pos + 1, sm + arr[pos])) % 1000000007;
ways = (ways + fun(pos + 1, sm - arr[pos])) % 1000000007;
return dp[pos][10000 + sm] = ways;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
ans = (ans + fun(i, 0)) % 1000000007;
}
cout << ans;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1), EPS = 1e-7;
const int dx[] = {-1, 0, 0, 1, -1, 1, 1, -1};
const int dy[] = {0, -1, 1, 0, 1, 1, -1, -1};
long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); }
long long lcm(long long x, long long y) { return ((x / gcd(x, y)) * y); }
long long power(long long x, long long y) {
if (y == 0) return 1;
if (y == 1) return x;
long long r = power(x, y / 2);
return r * r * power(x, y % 2);
}
void file() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
struct Data {
long long from, to, w;
Data(long long f, long long t, long long ww) {
from = f;
to = t;
w = ww;
}
bool operator<(const Data &rhs) const { return w > rhs.w; }
};
int main() {
file();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
map<int, int> mp;
int mx = 0;
for (int i = 0; i < n; ++i) {
if (v[i] % k != 0) {
++mp[k - v[i] % k];
mx = max(mx, mp[k - (v[i] % k)]);
}
}
long long ans = 0;
for (const auto &ch : mp) {
if (ch.second == mx) ans = (long long)k * (ch.second - 1) + ch.first + 1;
}
cout << ans << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n = 250;
int T, A[250];
int main() {
cin.tie(NULL);
ios::sync_with_stdio(0);
cin >> T;
for (int t = 0; t < T; t++) {
int maxi = 0, mini = INT_MAX, sum = 0, sum2 = 0, sum4 = 0;
for (int i = 0; i < n; i++) {
cin >> A[i];
mini = min(mini, A[i]);
maxi = max(maxi, A[i]);
sum4 += A[i] * A[i] * A[i] * A[i];
sum2 += A[i] * A[i];
}
double k = float(sum4) / sum2 / sum2 * n;
k -= 3;
cout << (k < -0.8 ? "uniform" : "poisson") << endl;
}
}
| 10 |
#include <bits/stdc++.h>
int N;
int p[100000];
int count;
int max;
int main() {
std::cin >> N;
if (N == 0) std::cout << 0 << std::endl;
std::cin >> p[0];
for (int i = 1; i < N; i++) {
std::cin >> p[i];
if (p[i] >= p[i - 1]) {
count++;
if (count > max) max = count;
} else
count = 0;
}
std::cout << max + 1 << std::endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int u[1111];
int M[1111];
int MM[1111];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
M[i] = 1000000000;
}
int d = 512;
for (; d > 0; d /= 2) {
vector<int> f;
memset(u, 0, sizeof(u));
for (int i = 1; i <= n; i += d * 2) {
for (int j = 0; j < d; j++) {
if (i + j <= n) {
u[i + j] = 1;
f.push_back(i + j);
}
}
}
printf("%d\n", (int)f.size());
for (int i = 0; i < f.size(); i++) {
printf("%d", f[i]);
if (i + 1 < f.size()) {
printf(" ");
} else {
printf("\n");
}
}
fflush(stdout);
for (int i = 1; i <= n; i++) {
scanf("%d", &MM[i]);
}
for (int i = 1; i <= n; i++) {
if (u[i] == 0) {
M[i] = min(M[i], MM[i]);
}
}
f.clear();
memset(u, 0, sizeof(u));
for (int i = d + 1; i <= n; i += d * 2) {
for (int j = 0; j < d; j++) {
if (i + j <= n) {
u[i + j] = 1;
f.push_back(i + j);
}
}
}
if (f.size() == 0) {
continue;
}
printf("%d\n", (int)f.size());
for (int i = 0; i < f.size(); i++) {
printf("%d", f[i]);
if (i + 1 < f.size()) {
printf(" ");
} else {
printf("\n");
}
}
fflush(stdout);
for (int i = 1; i <= n; i++) {
scanf("%d", &MM[i]);
}
for (int i = 1; i <= n; i++) {
if (u[i] == 0) {
M[i] = min(M[i], MM[i]);
}
}
}
printf("-1\n");
fflush(stdout);
for (int i = 1; i <= n; i++) {
printf("%d", M[i]);
if (i < n) {
printf(" ");
} else {
printf("\n");
}
}
fflush(stdout);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
unsigned long long int n, m, a, b;
long long int del, ext;
cin >> n >> m >> a >> b;
del = (n % m);
ext = (m - del);
cout << min((del * b), (ext * a));
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
inline int sgn(double xx) {
if (fabs(xx) < 1e-9) return 0;
return xx > 0 ? 1 : -1;
}
struct Point {
double x, y;
Point() {}
Point(double X, double Y) : x(X), y(Y) {}
} p[(200 + 50)];
inline Point operator-(const Point &P, const Point &Q) {
return Point(P.x - Q.x, P.y - Q.y);
}
inline double operator*(const Point &P, const Point &Q) {
return P.x * Q.y - P.y * Q.x;
}
struct Seg {
Point p1, p2;
Seg() {}
Seg(Point P1, Point P2) : p1(P1), p2(P2) {}
};
inline bool quickquit(Seg s1, Seg s2) {
double Maxx1 = max(s1.p1.x, s1.p2.x);
double Maxy1 = max(s1.p1.y, s1.p2.y);
double Minx1 = min(s1.p1.x, s1.p2.x);
double Miny1 = min(s1.p1.y, s1.p2.y);
double Maxx2 = max(s2.p1.x, s2.p2.x);
double Maxy2 = max(s2.p1.y, s2.p2.y);
double Minx2 = min(s2.p1.x, s2.p2.x);
double Miny2 = min(s2.p1.y, s2.p2.y);
if (sgn(Maxx1 - Minx2) < 0 || sgn(Maxx2 - Minx1) < 0) return 1;
if (sgn(Maxy1 - Miny2) < 0 || sgn(Maxy2 - Miny1) < 0) return 1;
return 0;
}
inline bool Cross(Seg s1, Seg s2) {
if (quickquit(s1, s2)) return 0;
if (sgn((s1.p1 - s2.p1) * (s2.p2 - s2.p1)) *
sgn((s1.p2 - s2.p1) * (s2.p2 - s2.p1)) >
0)
return 0;
if (sgn((s2.p1 - s1.p1) * (s1.p2 - s1.p1)) *
sgn((s2.p2 - s1.p1) * (s1.p2 - s1.p1)) >
0)
return 0;
return 1;
}
bool yes[(200 + 50)][(200 + 50)];
void Init() {
int i, j, k;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
for (i = 0; i < n; i++) {
yes[i][(i + 1) % n] = yes[(i + 1) % n][i] = 1;
for (j = i + 2; j < n; j++) {
yes[i][j] = 1;
for (k = 0; k < n; k++) {
if (k == i || (k + 1) % n == i) continue;
if (k == j || (k + 1) % n == j) continue;
if (Cross(Seg(p[i], p[j]), Seg(p[k], p[(k + 1) % n]))) {
yes[i][j] = 0;
break;
}
}
double A1 = 0.0, A2 = 0.0;
for (k = i; k != j; k = (k + 1) % n)
A1 += (p[k] - p[i]) * (p[(k + 1) % n] - p[i]);
for (k = j; k != i; k = (k + 1) % n)
A2 += (p[k] - p[i]) * (p[(k + 1) % n] - p[i]);
if (sgn(A1 * A2) <= 0) yes[i][j] = 0;
yes[j][i] = yes[i][j];
}
}
}
long long f[(200 + 50)][(200 + 50)];
void Solve() {
int i, j, k;
for (i = 0; i < n; i++) f[i][1] = 1;
for (j = 2; j < n; j++)
for (i = 0; i < n; i++) {
f[i][j] = 0;
for (k = (i + 1) % n; k != (i + j) % n; k = (k + 1) % n)
if (yes[i][k] && yes[(i + j) % n][k])
f[i][j] = (f[i][j] + f[i][(k - i + n) % n] *
f[k][(i + j - k + n) % n] % 1000000007) %
1000000007;
}
printf("%I64d\n", f[0][n - 1]);
}
int main() {
Init();
Solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 20;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
map<char, char> a;
char s[maxn];
int main() {
cin >> s;
int kt = -1;
int k1 = -1;
int n = strlen(s) - 1;
for (int i = (0); i <= (n - 1); i++) {
if (s[i] == '.') {
kt = i;
break;
}
}
if (kt != -1)
while (s[n] == '0') n--;
int t = 0;
while (s[t] == '0') t++;
if (s[t] == '.') kt = t, s[t] = '0';
while (s[t] == '0') t++;
for (int i = (t); i <= (n); i++) {
if (s[i] == '.' && kt == -1) kt = i;
if (s[i] - '0' >= 0 && s[i] - '0' <= 9 && k1 == -1) k1 = i;
}
if (kt == -1) n++, kt = n, s[n] = '.';
if (s[n] == '.') {
s[n] = '0';
while (s[n] == '0') n--;
}
printf("%c", s[k1]);
if (k1 < n) {
printf(".");
for (int i = (k1 + 1); i <= (n); i++)
if (s[i] != '.') printf("%c", s[i]);
}
if (kt != k1 + 1) {
printf("E");
if (kt < k1)
printf("%d", kt - k1);
else
printf("%d", kt - k1 - 1);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, q, block;
int A[1000005], B[1000005], C[1000005];
int cnt[1000005];
int ans[1000005];
long long answer = 0;
int posOfLast[1000005];
int realNum[1000005];
int pref[1000005];
struct node {
int L, R, i;
} Q[1000005];
vector<node> queries[1000005];
int tree[4 * 1000005];
void update(int lo, int hi, int idx, int val, int pos) {
if (lo == hi) {
tree[pos] = val;
return;
}
int mid = (lo + hi) / 2;
if (idx <= mid)
update(lo, mid, idx, val, pos * 2);
else
update(mid + 1, hi, idx, val, pos * 2 + 1);
tree[pos] = (tree[pos * 2] ^ tree[pos * 2 + 1]);
}
int query(int lo, int hi, int qlo, int qhi, int pos) {
if (lo >= qlo && hi <= qhi) return tree[pos];
int mid = (lo + hi) / 2;
if (qhi <= mid)
return query(lo, mid, qlo, qhi, pos * 2);
else if (qlo > mid)
return query(mid + 1, hi, qlo, qhi, pos * 2 + 1);
else
return (query(lo, mid, qlo, qhi, pos * 2) ^
query(mid + 1, hi, qlo, qhi, pos * 2 + 1));
}
bool myCmp(int a, int b) { return A[a] < A[b]; }
int main() {
memset(tree, 0, sizeof tree);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", A + i);
B[i] = i;
C[i] = A[i];
pref[i] = A[i];
if (i > 0) pref[i] ^= pref[i - 1];
}
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%d %d", &Q[i].L, &Q[i].R);
Q[i].L--;
Q[i].R--;
Q[i].i = i;
queries[Q[i].R].push_back(Q[i]);
}
int rnk = 0;
sort(B, B + n, myCmp);
realNum[B[0]] = A[B[0]];
A[B[0]] = rnk;
for (int i = 1; i < n; i++) {
if (C[B[i]] != C[B[i - 1]]) {
rnk++;
}
realNum[B[i]] = A[B[i]];
A[B[i]] = rnk;
}
memset(posOfLast, -1, sizeof posOfLast);
for (int i = 0; i < n; i++) {
if (posOfLast[A[i]] != -1) update(0, n, posOfLast[A[i]], 0, 1);
posOfLast[A[i]] = i;
update(0, n, posOfLast[A[i]], realNum[i], 1);
for (auto curQ : queries[i]) {
long long sum = 0;
sum = query(0, n, curQ.L, curQ.R, 1);
ans[curQ.i] = sum ^ (pref[curQ.R] ^ (curQ.L > 0 ? pref[curQ.L - 1] : 0));
}
}
for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
set<int> st;
vector<priority_queue<pair<long long, int>, vector<pair<long long, int> >,
greater<pair<long long, int> > > >
taxis;
void add_taxi(int i, int j, long long t) {
if (taxis[i].empty()) st.insert(i);
taxis[i].push(pair<long long, int>(t, j));
}
int main() {
int N, M, Q;
cin >> N >> M >> Q;
priority_queue<long long, vector<long long>, greater<long long> > events;
map<long long, vector<pair<int, int> > > mp;
taxis.resize(N);
for (int j = 0; j < M; j++) {
int i;
scanf("%d", &i), i--;
add_taxi(i, j, 0);
}
vector<long long> t(Q);
vector<int> a(Q), b(Q);
for (int q = 0; q < Q; q++) {
scanf("%lld%d%d", &t[q], &a[q], &b[q]), a[q]--, b[q]--;
events.push(t[q]);
}
int q = 0;
while (!events.empty()) {
long long t0 = events.top();
events.pop();
for (pair<int, int> z : mp[t0]) add_taxi(z.first, z.second, t0);
mp[t0].clear();
for (; q < Q && t[q] <= t0 && !st.empty(); q++) {
int i0 = a[q];
vector<vector<long long> > v;
{
auto it = st.upper_bound(i0);
if (it != st.begin()) {
it--;
int i = *it;
pair<long long, int> z = taxis[i].top();
v.push_back({i0 - i, z.first, z.second, i});
}
}
{
auto it = st.lower_bound(i0);
if (it != st.end()) {
int i = *it;
pair<long long, int> z = taxis[i].top();
v.push_back({i - i0, z.first, z.second, i});
}
}
sort(v.begin(), v.end());
int i = v[0][3], j = v[0][2];
taxis[i].pop();
if (taxis[i].empty()) st.erase(i);
long long t1 = t0 + abs(i - a[q]) + abs(a[q] - b[q]);
events.push(t1);
mp[t1].push_back(pair<int, int>(b[q], j));
printf("%d %lld\n", j + 1, t0 + abs(i - a[q]) - t[q]);
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
bool check(long long int x, long long int arr[], long long int n) {
if (n / x <= 2 && n % x != 0) return false;
for (long long int i = 0; i < x; i++) {
bool f = true;
for (long long int j = i; j < n; j += x) {
if (arr[j] != 1) {
f = false;
break;
}
}
if (f) return true;
}
return false;
}
int main() {
long long int n;
cin >> n;
long long int arr[n];
for (long long int i = 0; i < n; i++) cin >> arr[i];
vector<long long int> gap;
gap.push_back(1);
for (long long int i = 2; i * i <= n; i++) {
if (n % i == 0 && n / i >= 3) {
gap.push_back(i);
if (i >= 3) gap.push_back(n / i);
}
}
for (long long int i = 0; i < gap.size(); i++) {
if (check(gap[i], arr, n)) {
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int N, l[82], r[82], V[200];
double Ans[82], F[82][82];
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i)
scanf("%d%d", l + i, r + i), V[++*V] = l[i], V[++*V] = r[i];
sort(V + 1, V + *V + 1);
*V = unique(V + 1, V + *V + 1) - V;
for (int i = 1; i <= N; ++i) {
memset(Ans, 0, sizeof Ans);
for (int j = 1, p, m, L, R; j < *V - 1; ++j)
if (l[i] <= V[j] && r[i] > V[j]) {
L = V[j];
R = V[j + 1];
memset(F, p = m = 0, sizeof F);
**F = 1;
for (int k = 1; k <= N; ++k)
if (k ^ i)
if (r[k] <= L)
++p;
else if (l[k] < R) {
double inp = 1. * (L - l[k]) / (r[k] - l[k]),
inm = 1. * (R - L) / (r[k] - l[k]);
for (int x = m; ~x; --x)
for (int y = m - x; ~y; --y) {
F[x + 1][y] += F[x][y] * inp;
F[x][y + 1] += F[x][y] * inm;
F[x][y] *= 1 - inp - inm;
}
++m;
}
double tmp = 1. * (R - L) / (r[i] - l[i]);
for (int x = m; ~x; --x)
for (int y = m - x; ~y; --y)
if (F[x][y] > 1e-12)
for (int k = x + 1 + p; k <= x + y + 1 + p; ++k)
Ans[k] += F[x][y] / (y + 1) * tmp;
}
for (int i = 1; i <= N; ++i) printf("%.8lf ", Ans[i]);
puts("");
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
const long long N = 1e5 + 4;
long long n, m, Q, T, mx, sum, ac, ans1, ans2, top;
long long a[N], aa[N], b[N], L[N], R[N], B[N], st[N];
inline bool check_3(long long res) {
vector<pair<long long, long long> > vec;
for (long long i = 1, las = 1; i <= m; i++)
if (a[i] != -1) {
if (las < i) vec.push_back(make_pair(las, i - 1));
las = i + 1;
}
long long l, r, x, y;
for (auto v : vec) {
l = v.first;
r = v.second;
if ((r - l + 1) * (B[r + 1] - B[r]) < res) {
res -= (r - l + 1) * (B[r + 1] - B[r]);
for (long long i = l; i <= r; i++) B[i] = B[r + 1];
} else {
x = res / (r - l + 1);
y = res - (r - l + 1) * x;
for (long long i = l; i <= r; i++) B[i] += x;
for (long long i = 0; i < y; i++) B[r - i]++;
res = 0;
}
}
return !res;
}
inline long long calc(long long i, long long j, long long *L) {
return L[j] + B[i] + (n - j) * (m - i);
}
inline long long check_5(long long *L) {
long long ret = min(L[n], B[m]);
top = 0;
for (long long i = 0; i < n; i++) {
while (top > 1 && (L[st[top]] - L[st[top - 1]]) * (i - st[top]) >=
(L[i] - L[st[top]]) * (st[top] - st[top - 1]))
top--;
st[++top] = i;
}
for (long long i = m - 1, j = 1; i >= 0; i--) {
while (j < top && calc(i, st[j], L) >= calc(i, st[j + 1], L)) j++;
ret = min(ret, calc(i, st[j], L));
}
return ret;
}
inline bool check_4() {
for (long long i = 1; i <= m; i++) B[i] += B[i - 1];
return check_5(L) >= L[n] && check_5(R) >= T;
}
inline bool check_2(long long mid, long long len) {
ac = 0;
for (long long i = 1; i <= m; i++) B[i] = b[i];
long long res = T - sum;
for (long long i = m - len + 1; i <= m; i++) {
res -= mid - B[i];
B[i] = a[i] = mid;
}
if (res < 0) return 0;
if (!check_3(res)) return 1;
if (check_4()) {
ac = 1;
ans1 = len;
ans2 = mid;
return 1;
}
return 0;
}
inline bool check_1(long long len) {
ans1 = ans2 = -1;
long long id = -1;
for (long long i = m - len + 1; i <= m; i++)
if (a[i] != -1) {
if (id != -1 && id != a[i]) return 0;
id = a[i];
}
long long l, r, mid, ret = 0;
if (id != -1)
l = r = id;
else
l = mx, r = min(n, (T - sum) / len + mx);
while (l < r) {
mid = (l + r + 1) >> 1;
if (check_2(mid, len))
l = mid;
else
r = mid - 1;
ret |= ac;
}
check_2(r, len);
ret |= ac;
for (long long i = m - len + 1; i <= m; i++) a[i] = aa[i];
return ret;
}
signed main() {
n = read();
m = read();
for (long long i = 1; i <= n; i++) {
L[i] = read();
R[i] = read();
}
Q = read();
memset(a, -1, sizeof(a));
for (long long i = 1, x; i <= Q; i++) {
x = read();
a[x] = read();
}
T = read();
sort(L + 1, L + n + 1);
sort(R + 1, R + n + 1);
for (long long i = 1; i <= n; i++) {
L[i] += L[i - 1];
R[i] += R[i - 1];
}
reverse(a + 1, a + m + 1);
memcpy(aa, a, sizeof(a));
for (long long i = 1; i <= m; i++) {
if (a[i] != -1) mx = a[i];
b[i] = mx;
sum += mx;
}
if (sum > T) {
puts("-1 -1");
return (0 - 0);
}
long long l = 1, r = m, mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (check_1(mid))
l = mid;
else
r = mid - 1;
}
check_1(r);
cout << ans1 << " " << ans2 << "\n";
return (0 - 0);
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int a[1000052], b[1000005];
int dp[4005][105][2];
const long long mod = 1e9 + 7;
int main() {
int n, m, y, x, i, j, k, t;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) {
scanf("%d%d", &a[i], &b[i]);
}
for (i = 1; i <= n; i++) {
dp[a[i]][i][0]++;
if (b[i] != a[i]) {
dp[b[i]][i][1]++;
}
}
for (i = 1; i < m; i++) {
for (j = 1; j <= n; j++) {
for (k = 1; k <= n; k++) {
if (k != j) {
if (a[k] == b[j]) {
dp[i + a[k]][k][0] += dp[i][j][0];
dp[i + a[k]][k][0] %= mod;
}
if (a[k] != b[k] && b[k] == b[j]) {
dp[i + b[k]][k][1] += dp[i][j][0];
dp[i + b[k]][k][1] %= mod;
}
if (a[k] == a[j]) {
dp[i + a[k]][k][0] += dp[i][j][1];
dp[i + a[k]][k][0] %= mod;
}
if (a[k] != b[k] && b[k] == a[j]) {
dp[i + b[k]][k][1] += dp[i][j][1];
dp[i + b[k]][k][1] %= mod;
}
}
}
}
}
long long ans = 0;
for (i = 1; i <= n; i++) {
ans += dp[m][i][0] + dp[m][i][1];
}
memset(dp, 0, sizeof(dp));
printf("%I64d\n", ans % mod);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int main() {
long long dp[120][120];
long long aux[120][26];
long long n, k;
cin >> n >> k;
k--;
string a;
cin >> a;
for (long long i = 0; i < n; i++) {
dp[i][1] = 1;
for (long long j = 0; j < 26; j++) {
if ((a[i] - 'a') == j && (!i))
aux[i][j] = i;
else
aux[i][j] = -1;
}
}
for (long long i = 1; i < n; i++) {
for (long long j = 0; j < 26; j++) {
aux[i][j] = aux[i - 1][j];
}
aux[i][a[i] - 'a'] = i;
}
long long ans = 0;
for (long long len = 2; len < n; len++) {
for (long long i = 1; i < n; i++) {
for (long long j = 0; j < 26; j++) {
if (aux[i - 1][j] != -1)
dp[i][len] =
min((long long)1e12, dp[i][len] + dp[aux[i - 1][j]][len - 1]);
}
}
}
for (long long len = n - 1; len >= 0; len--) {
long long cnt = 0;
for (long long j = 0; j < 26; j++) {
if (aux[n - 1][j] != -1) {
cnt += dp[aux[n - 1][j]][len];
}
}
if (cnt >= k) {
ans += k * (n - len);
k = 0;
break;
} else {
ans += cnt * (n - len);
k -= cnt;
}
}
if (k == 1) {
ans += n;
--k;
}
if (k > 0)
cout << -1 << endl;
else
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int mapa[1000001] = {0};
int dp[1000001] = {0};
for (int i = 0; i < n; i++) {
int a;
cin >> a;
mapa[a] += 1;
dp[a] = mapa[a];
}
for (int i = 1; i < 1000001; i++) {
if (mapa[i]) {
for (int j = 2 * i; j < 1000001; j += i) {
if (mapa[j]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
}
int ans = 0;
for (int i = 1; i < 1000001; i++) ans = max(ans, dp[i]);
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
string a[n + 10];
for (int i = 0; i < n; i++) cin >> a[i];
int i, j, x = 0, y = 0;
int steps = 0;
for (i = 0; i < n; i++)
for (j = (i % 2 ? m - 1 : 0); j >= 0 && j < m; j += (i % 2 ? -1 : 1))
if (a[i][j] == 'W') {
steps += abs(x - i) + abs(y - j);
x = i;
y = j;
}
cout << steps << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int head[maxn], Next[maxn * 2], ver[maxn * 2], tot;
int a[maxn];
int ans[maxn * 100], cnt;
void add(int x, int y) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
void dfs(int x, int fa = 0) {
if (x != 1) a[x] = -a[x];
ans[++cnt] = x;
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (y != fa) {
dfs(y, x);
ans[++cnt] = x;
a[x] = -a[x];
}
}
if (a[x] == -1 && x != 1) {
ans[++cnt] = fa;
a[fa] = -a[fa];
ans[++cnt] = x;
a[x] = -a[x];
return;
} else if (a[x] == -1 && x == 1) {
int y = ver[head[1]];
ans[++cnt] = y;
ans[++cnt] = x;
ans[++cnt] = y;
}
}
int main() {
int n, x, y;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1);
for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 300333, mod = 998244353;
int mul(int x, int y) { return (1LL * x * y) % mod; }
int power(int a, int b) {
if (b == 0) {
return 1 % mod;
}
if (b % 2 == 0) {
return power(mul(a, a), b / 2);
}
return mul(a, power(a, b - 1));
}
int inv(int x) {
x %= mod;
return power(x, mod - 2);
}
int n, s, a[max_n], dp[max_n];
int A[max_n], B[max_n], C[max_n];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
s += a[i];
}
const int rs = inv(s);
for (int x = 0; x < s; ++x) {
const int coef = inv(1 + mod - mul(s - x, mul(rs, mul(n - 2, inv(n - 1)))));
A[x] = mul(coef, mul(x, rs));
B[x] = mul(coef, mul(s - x, mul(rs, inv(n - 1))));
C[x] = coef;
}
for (int x = 1; x < s; ++x) {
int nb, nc;
const int coef = inv(mod + 1 - mul(A[x], B[x - 1]));
nb = mul(coef, B[x]);
nc = mul(coef, mul(A[x], C[x - 1]) + C[x]);
A[x] = 0;
B[x] = nb;
C[x] = nc;
}
for (int x = s - 1; x >= 0; --x) {
dp[x] = (mul(B[x], dp[x + 1]) + C[x]) % mod;
}
int tot = 0;
for (int i = 0; i < n; ++i) {
tot += dp[a[i]];
tot %= mod;
}
int ans = mul(n, tot + mod - mul(n - 1, dp[0]));
ans = mul(ans, inv(mul(n, n)));
printf("%d\n", ans);
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
char a[maxn];
char sh[maxn], xi[maxn];
int main() {
scanf("%s", a);
int num, l, r, sum, tol, ans = 0;
int len = strlen(a);
for (int i = 0; i < len; i++) {
if (a[i] >= 'A' && a[i] <= 'Z') {
num = 0;
while (i < len && a[i] >= 'A' && a[i] <= 'Z') {
sh[num++] = a[i++];
}
i--;
sh[num] = '\0';
} else {
if (a[i] == ':') {
int sum = 1, flag = 0;
for (int j = i + 1; j < len; j++) {
if (sum == 0)
break;
else if (a[j] == ':') {
sum++;
} else if (a[j] == '.' && a[j - 1] == '.') {
sum--;
} else if (a[j] >= 'A' && a[j] <= 'Z') {
int num = 0;
while (j < len && a[j] >= 'A' && a[j] <= 'Z') {
xi[num++] = a[j++];
}
xi[num] = '\0';
j--;
if (strcmp(sh, xi) == 0) ans++;
}
}
}
}
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
long long n, m;
long long ksm(long long x, long long y) {
long long ans = 1;
for (; y; y >>= 1, x = x * x % M)
if (y & 1) (ans *= x) %= M;
return ans;
}
signed main() {
scanf("%lld%lld", &n, &m);
printf("%lld\n",
(n + 1 - m) * ksm(n + 1, M - 2) % M * ksm(2 * (n + 1), m) % M);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[100005];
vector<vector<int>> graph(200);
int dist(int x, int y) {
vector<int> d(200, 2000000000);
queue<int> Q;
d[x] = 0;
Q.push(x);
while (!Q.empty()) {
int f = Q.front();
Q.pop();
for (int i : graph[f]) {
if (f == x && i == y) continue;
if (d[i] > d[f] + 1) {
d[i] = d[f] + 1;
Q.push(i);
}
}
}
return d[y];
}
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) {
i--;
n--;
}
}
for (int i = 0; i < 60; i++) {
int cnt = 0;
for (int k = 0; k < n; k++)
if ((a[k] >> i) & 1) cnt++;
if (cnt >= 3) {
cout << 3 << endl;
return 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] & a[j]) {
graph[i].push_back(j);
graph[j].push_back(i);
}
}
}
int ans = 2000000000;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] & a[j]) {
ans = min(ans, dist(i, j) + 1);
}
}
}
if (ans == 2000000000) ans = -1;
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
char s[600005];
int w[600005];
int kmp[600005];
int nonparent[600005];
int rmq[600005][20];
int log_2[600005];
int rmq_query(int l, int r) {
int L = log_2[r - l + 1];
return min(rmq[l][L], rmq[r - (1 << L) + 1][L]);
}
map<int, int> freq;
int main() {
log_2[1] = 0;
for (int i = 2; i <= 600000; i++) {
if ((1 << (log_2[i - 1] + 1)) == i) {
log_2[i] = log_2[i - 1] + 1;
} else {
log_2[i] = log_2[i - 1];
}
}
nonparent[0] = -1;
scanf("%d", &n);
long long low = 0, high = 0;
long long delta = 0;
for (int i = 1; i <= n; i++) {
char Tmp[5];
scanf("%s%d", Tmp, w + i);
char c = (Tmp[0] - 'a' +
(((low % 26LL) + high * (1000000000000000000LL % 26LL)) % 26)) %
26 +
'a';
w[i] ^= ((low % (long long)(1 << 30)) +
(high * (long long)(1000000000000000000LL % (1 << 30)))) %
(long long)(1 << 30);
s[i] = c;
if (i == 1) {
kmp[i] = 0;
} else {
int cur = kmp[i - 1];
while (1) {
if (s[cur + 1] == c) {
kmp[i] = cur + 1;
break;
}
if (cur == 0) break;
cur = kmp[cur];
}
}
nonparent[i] = -1;
if (kmp[i]) {
if (s[kmp[kmp[i]] + 1] == s[kmp[i] + 1]) {
nonparent[i] = nonparent[kmp[i]];
} else {
nonparent[i] = kmp[kmp[i]];
}
}
int cur = i - 1;
while (cur > 0) {
if (s[cur + 1] != c) {
int x = rmq_query(i - cur, i - 1);
freq[x]--;
delta -= (long long)x;
}
if (s[kmp[cur] + 1] != c) {
cur = kmp[cur];
} else {
cur = nonparent[cur];
}
}
if (c == s[1]) {
freq[w[i]]++;
delta += (long long)w[i];
}
if (w[i] < (1 << 30) - 1) {
auto iter = freq.upper_bound(w[i]);
auto tmp = iter;
long long tot = 0;
while (tmp != freq.end()) {
tot += (long long)tmp->second;
delta -= (long long)tmp->first * (long long)tmp->second;
tmp++;
}
freq.erase(iter, freq.end());
freq[w[i]] += tot;
delta += tot * (long long)w[i];
}
low += delta;
while (low >= 1000000000000000000LL) {
low -= 1000000000000000000LL;
high++;
}
if (high) {
printf("%lld%018lld\n", high, low);
} else {
printf("%lld\n", low);
}
rmq[i][0] = w[i];
for (int j = 1; (1 << j) <= i; j++) {
int from = i - (1 << j) + 1;
rmq[from][j] = min(rmq[from][j - 1], rmq[from + (1 << (j - 1))][j - 1]);
}
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int c, v0, v1, a, l;
cin >> c >> v0 >> v1 >> a >> l;
int v = v0, k = 0;
while (c > 0) {
c -= v;
if (c <= 0) {
cout << k + 1 << endl;
return 0;
}
if (v + a <= v1)
v += a;
else
v = v1;
c += l;
k++;
}
cout << k << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, a, last(0), continuous(1);
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a);
if (a == last) {
continuous += 1;
} else {
continuous = 1;
}
last = a;
}
printf("%d\n", continuous + k > n ? n - continuous : -1);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int m, x, y;
map<pair<int, int>, int> itb;
map<int, pair<int, int> > bti;
set<int> blocks;
set<int> q;
vector<int> result;
bool ex_g(int xx, int yy) {
if (itb.find(make_pair(xx, yy)) != itb.end())
return (blocks.find(itb[make_pair(xx, yy)]) != blocks.end());
else
return false;
}
bool can_pull(int idx) {
int xx = bti[idx].first;
int yy = bti[idx].second;
if (ex_g(xx, yy + 1) and !ex_g(xx - 1, yy) and !ex_g(xx + 1, yy)) {
return false;
}
if (ex_g(xx - 1, yy + 1) and !ex_g(xx - 1, yy) and !ex_g(xx - 2, yy)) {
return false;
}
if (ex_g(xx + 1, yy + 1) and !ex_g(xx + 1, yy) and !ex_g(xx + 2, yy)) {
return false;
}
return true;
}
bool can_pull(int xx, int yy) {
return ex_g(xx, yy) && can_pull(itb[make_pair(xx, yy)]);
}
void remove_el(bool turn) {
int r = (turn ? *q.rbegin() : *q.begin());
if (!can_pull(r)) {
q.erase(r);
remove_el(turn);
return;
}
result.push_back(r);
q.erase(r);
blocks.erase(r);
int xx = bti[r].first;
int yy = bti[r].second;
if (can_pull(xx, yy - 1)) {
q.insert(itb[make_pair(xx, yy - 1)]);
}
if (can_pull(xx - 1, yy - 1)) {
q.insert(itb[make_pair(xx - 1, yy - 1)]);
}
if (can_pull(xx + 1, yy - 1)) {
q.insert(itb[make_pair(xx + 1, yy - 1)]);
}
}
int main() {
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
itb[make_pair(x, y)] = i;
bti[i] = make_pair(x, y);
blocks.insert(i);
}
for (int i = 0; i < m; i++)
if (can_pull(i)) {
q.insert(i);
}
bool t = true;
for (int i = 0; i < m; i++) {
if (t)
remove_el(true);
else
remove_el(false);
t = !t;
}
long long int sum = 0;
long long int c = (1000 * 1000 * 1000) + 9;
for (int i = 0; i < result.size(); i++) {
sum *= m;
sum %= c;
sum += result[i];
sum %= c;
}
cout << sum << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int St[4 * (int)(1e6 + 10)];
int arr[(int)(1e6 + 10)], bas, son, s, size;
int n, m, k, i, j, x, y, z, ans[(int)(1e6 + 10)];
int upd(int root, int bas, int son, int x, int t) {
if (bas == son) {
return St[root] = 0;
}
if (St[(root << 1)] + t >= x)
return St[root] = upd((root << 1), bas, ((bas + son) >> 1), x, t) +
St[((root << 1) | 1)];
else
return St[root] = upd(((root << 1) | 1), ((bas + son) >> 1) + 1, son, x,
t + St[(root << 1)]) +
St[(root << 1)];
}
int upd2(int root, int bas, int son, int x) {
if (bas > x or son < x) return St[root];
if (bas == son) return St[root] = 1;
return St[root] = upd2((root << 1), bas, ((bas + son) >> 1), x) +
upd2(((root << 1) | 1), ((bas + son) >> 1) + 1, son, x);
}
void yaz(int root, int bas, int son) {
if (bas == son) {
if (St[root]) cout << ans[bas];
return;
}
yaz((root << 1), bas, ((bas + son) >> 1));
yaz(((root << 1) | 1), ((bas + son) >> 1) + 1, son);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (i = 1; i <= m; i++) cin >> arr[i];
for (i = 1; i <= n; i++) {
cin >> x;
if (x == -1) {
bas = upper_bound(arr + 1, arr + 1 + m, size) - arr - 1;
for (j = 1; j <= bas; j++) {
size--;
upd(1, 1, (int)(1e6 + 10), arr[j] - j + 1, 0);
}
} else {
size++;
upd2(1, 1, (int)(1e6 + 10), ++s);
ans[s] = x;
}
}
yaz(1, 1, (int)(1e6 + 10));
if (!size) cout << "Poor stack!" << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
scanf("%d%d", &N, &M);
char st[N][M + 1];
int col[M], row[N];
int val[M][2], valr[N][2];
bool con = true;
for (int i = 0; i < M; i++) {
col[i] = -1;
val[i][0] = -1;
val[i][1] = -1;
}
for (int i = 0; i < N; i++) {
row[i] = -1;
valr[i][0] = -1;
valr[i][1] = -1;
}
int pl[N];
for (int i = 0; i < N; i++) {
pl[i] = 0;
scanf("%s", st[i]);
for (int j = 0; j < M; j++) {
if (st[i][j] != '.') {
pl[i]++;
col[j] = 1;
row[i] = 1;
if (st[i][j] == '1' || st[i][j] == '4') {
if (val[j][i % 2] == 1)
con = false;
else {
val[j][i % 2] = 0;
val[j][(i + 1) % 2] = 1;
}
} else {
if (val[j][i % 2] == 0)
con = false;
else {
val[j][i % 2] = 1;
val[j][(i + 1) % 2] = 0;
}
}
if (st[i][j] == '1' || st[i][j] == '2') {
if (valr[i][j % 2] == 1)
con = false;
else {
valr[i][j % 2] = 0;
valr[i][(j + 1) % 2] = 1;
}
} else {
if (valr[i][j % 2] == 0)
con = false;
else {
valr[i][j % 2] = 1;
valr[i][(j + 1) % 2] = 0;
}
}
}
}
}
for (int i = 0; i < N && con; i++) {
for (int j = 0; j < M && con; j++) {
if (st[i][j] != '.') {
switch (st[i][j]) {
case '1':
if (!((i == 0 || (st[i - 1][j] == '.' || st[i - 1][j] == '2' ||
st[i - 1][j] == '3')) &&
(j == 0 || (st[i][j - 1] == '.' || st[i][j - 1] == '3' ||
st[i][j - 1] == '4'))))
con = false;
break;
case '2':
if (!((i == N - 1 || (st[i + 1][j] == '.' || st[i + 1][j] == '1' ||
st[i + 1][j] == '4')) &&
(j == 0 || (st[i][j - 1] == '.' || st[i][j - 1] == '3' ||
st[i][j - 1] == '4'))))
con = false;
break;
case '3':
if (!((i == N - 1 || (st[i + 1][j] == '.' || st[i + 1][j] == '1' ||
st[i + 1][j] == '4')) &&
(j == M - 1 || (st[i][j + 1] == '.' || st[i][j + 1] == '1' ||
st[i][j + 1] == '2'))))
con = false;
break;
case '4':
if (!((i == 0 || (st[i - 1][j] == '.' || st[i - 1][j] == '2' ||
st[i - 1][j] == '3')) &&
(j == M - 1 || (st[i][j + 1] == '.' || st[i][j + 1] == '1' ||
st[i][j + 1] == '2'))))
con = false;
break;
}
}
}
}
if (!con) {
cout << "0" << endl;
return 0;
}
long long ans = 1;
int counter = 0;
int m = 1000003;
for (int i = 0; i < M; i++)
if (col[i] == -1) counter++;
for (int i = 0; i < N; i++)
if (row[i] == -1) counter++;
for (int i = 0; i < counter; i++) ans = (ans * 2) % m;
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
const int md = 1e9 + 7;
const long long inf = 4e18;
const int OO = 1;
const int OOO = 1;
using namespace std;
const int M = 50005, N = 26;
int n;
int T = 0;
vector<int> a;
bool dp[N][M * N] = {};
int to[N][M * N] = {};
void upd(int v) {
for (int i = N - 2; i >= 0; i--) {
for (int j = 0; j < M * (i + 1); j++)
if (dp[i][j] && !dp[i + 1][j + v]) {
dp[i + 1][j + v] = true;
to[i + 1][j + v] = v;
}
}
}
int ans = md;
int y;
int opti;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
n *= 2;
a.resize(n);
for (auto &i : a) cin >> i, T += i;
sort(a.begin(), a.end());
dp[0][0] = 1;
for (int i = n - 2; i >= 1; i--) {
upd(a[i + 1]);
for (int j = 0; j < M * N; j++)
if (dp[n / 2 - 1][j]) {
int S2 = a[i] + j;
int S1 = T - S2;
int m1 = a[0];
int m2 = a[i];
int res = max(S1 + m2, S2 + m1);
if (res < ans) {
ans = res;
y = j;
opti = i;
}
}
}
vector<int> arr1, arr2;
multiset<int> SS;
for (const auto &i : a) SS.insert(i);
arr1.push_back(a[opti]);
SS.erase(SS.find(a[opti]));
int pos = y, x = n / 2 - 1;
while (x > 0) {
int v = to[x][pos];
pos -= v;
x--;
arr1.push_back(v);
SS.erase(SS.find(v));
}
sort(arr1.rbegin(), arr1.rend());
for (const auto &i : SS) cout << i << " ";
cout << '\n';
for (const auto &i : arr1) cout << i << " ";
cout << '\n';
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 11;
int a[N], d, n, m;
void dfs(int l, int r, int k) {
if (l == r - 1) {
a[l] = k;
return;
}
if (m == 0) {
if (k == 0) return;
a[r - 1] = k;
return;
}
m -= 2;
int mid = (l + r) / 2;
if (k == 0) {
k = d;
d--;
}
dfs(l, mid, k);
dfs(mid, r, 0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
m--;
if (m % 2 == 1) {
cout << -1 << endl;
return 0;
}
d = n;
dfs(0, n, 0);
if (m > 0) {
cout << -1 << endl;
return 0;
}
int ks = 1;
for (int i = 0; i < n; i++)
if (a[i] != 0)
cout << a[i] << " ";
else {
cout << ks << " ";
ks++;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353, MAXN = 100005, LOGN = 18, MAXM = 1005;
inline void S(int& n) {
n = 0;
int ch = getchar();
int sign = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
}
n = n * sign;
}
const int SHIFT = 30, ETF = MOD - 1, LIM = (int)1e9;
const long long int INF = (long long int)1e18 + 1;
const double PI = (double)acos(-1), EPSILON = 1e-10;
int countDeletion(int k, int l, int r) {
if (l / k == r / k) return 0;
return (r - (l / k) * k + 1) - k;
}
int main() {
int n, k, m, s;
cin >> m >> k >> n >> s;
vector<int> flowers(m), required(s);
for (int i = 0; i < m; i++) {
S(flowers[i]);
}
unordered_map<int, int> schematic;
for (int i = 0; i < s; i++) {
S(required[i]);
schematic[required[i]]++;
}
int notMet = schematic.size();
if (0) {
cout << "notMet"
<< "=" << notMet << endl;
}
int l = 0, r = -1;
unordered_map<int, int> made;
for (int i = 0; i < m; i++) {
int f = flowers[i];
made[f]++;
if (made[f] == schematic[f]) {
notMet--;
}
if (notMet == 0) {
while (l <= i) {
int ff = flowers[l];
if (made[ff] > schematic[ff]) {
l++;
made[ff]--;
} else {
break;
}
}
int afford = m - k * n;
int waste = countDeletion(k, l, i);
if (waste <= afford) {
r = i;
break;
}
}
}
if (0) {
cout << "l"
<< "=" << l << endl;
cout << "r"
<< "=" << r << endl;
}
if (r == -1) {
printf("%d\n", r);
} else {
int sz = countDeletion(k, l, r);
if (sz == 0) {
printf("%d\n", sz);
return 0;
}
if (0) {
cout << "Map "
<< "schematic"
<< ":" << endl;
for (__typeof((schematic).begin()) iittrr = (schematic).begin();
iittrr != (schematic).end(); iittrr++)
cout << "( " << (iittrr->first) << " , " << (iittrr->second) << " )"
<< endl;
cout << endl;
cout << "sz"
<< "=" << sz << endl;
}
unordered_set<int> myset;
for (int i = (l / k) * k; i < r + 1; i++) {
myset.insert(i);
}
for (int i = (l / k) * k; i < r + 1; i++) {
int f = flowers[i];
if (schematic[f] > 0) {
schematic[f]--;
myset.erase(i);
}
}
int rem = r + 1 - (l / k) * k - s - sz;
for (int i = 0; i < rem; i++) {
myset.erase(*(myset.begin()));
}
if (0) {
cout << "myset.size()"
<< "=" << myset.size() << endl;
}
printf("%d\n", sz);
for (__typeof((myset).begin()) itr = (myset).begin(); itr != (myset).end();
itr++) {
printf("%d ", (*itr) + 1);
}
printf("\n");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long k;
struct p {
int x, y;
};
int c[100][100], f[200];
long long g[300][300];
p a[200];
int min(int o, int p) {
if (o < p) return o;
return p;
}
bool cmp(p a, p b) {
if (a.x < b.x) {
return true;
}
return false;
}
long long work(int o) {
f[o] = 0;
memset(g, 0, sizeof(g));
g[0][1] = 1;
bool u = true;
for (int i = 1; i <= n + m - 2; i++)
if (f[i] < 0) {
for (int j = 0; j <= n + m - 2; j++) {
if (j > 0) g[i][j] += g[i - 1][j - 1];
if (g[i][j] >= k) g[i][j] = k;
if (j < n + m - 2) g[i][j] += g[i - 1][j + 1];
if (g[i][j] >= k) g[i][j] = k;
}
} else {
if (f[i] == 0) {
for (int j = 1; j <= n + m - 2; j++) g[i][j] = g[i - 1][j - 1];
} else {
for (int j = 0; j < n + m - 2; j++) g[i][j] = g[i - 1][j + 1];
}
}
f[o] = -1;
return g[n + m - 2][0];
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i <= n + m - 2; i++) {
a[i].x = n * m + 100;
a[i].y = i;
f[i] = -1;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
scanf("%d", &c[i][j]);
a[i + j].x = min(a[i + j].x, c[i][j]);
}
sort(a, a + n + m - 1, cmp);
f[0] = 0;
f[n + m - 2] = 1;
for (int i = 0; i <= n + m - 2; i++)
if (f[a[i].y] < 0) {
long long tmp = work(a[i].y);
if (tmp < k) {
k -= tmp;
f[a[i].y] = 1;
} else {
f[a[i].y] = 0;
}
}
char ch[200][200];
memset(ch, 0, sizeof(ch));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (f[i + j] == 0) {
ch[i][j] = '(';
} else {
ch[i][j] = ')';
}
for (int i = 0; i < n; i++) printf("%s\n", ch[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int a[110];
int main() {
int n, k;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
cin >> k;
sort(a, a + n);
int ans = 0, now = 1, j;
for (int i = 0; i < n; i++) {
now = 1;
j = i;
while (a[j + 1] - a[i] <= k && j + 1 < n) {
j++;
now++;
}
if (ans < now) ans = now;
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, k, t;
bool mf[5], ff[5];
int h[100], w[100], r[100];
vector<int> val;
void rec(int ind, int sum) {
if (ind == k - 1) {
val.push_back(sum);
if (mf[h[ind] - 1] & ff[w[ind] - 1]) val.push_back(sum + r[ind]);
return;
}
rec(ind + 1, sum);
if (mf[h[ind] - 1] & ff[w[ind] - 1]) {
mf[h[ind] - 1] = false;
ff[w[ind] - 1] = false;
rec(ind + 1, sum + r[ind]);
mf[h[ind] - 1] = true;
ff[w[ind] - 1] = true;
}
}
int main() {
cin >> n >> k >> t;
for (int i = 0; i < n; i++) mf[i] = ff[i] = true;
for (int i = 0; i < k; i++) {
cin >> h[i] >> w[i] >> r[i];
}
rec(0, 0);
sort(val.begin(), val.end());
cout << val[t - 1];
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
int n;
int a, b, c;
multiset<int> S;
int res;
int Remove(int x) {
multiset<int>::iterator it = S.upper_bound(x);
if (it == S.begin()) return 0;
it--;
int val = *it;
S.erase(it);
return val;
}
void Phase0() {
multiset<int>::iterator it;
while (!S.empty()) {
it = S.end();
it--;
if (*it > b + c) {
res++;
S.erase(it);
} else
return;
}
}
void Phase1() {
multiset<int>::iterator it;
while (!S.empty()) {
it = S.end();
it--;
if (*it > a + c) {
res++;
S.erase(it);
Remove(a);
} else
return;
}
}
void Phase2() {
multiset<int>::iterator it;
while (!S.empty()) {
it = S.end();
it--;
if (*it > max(a + b, c)) {
res++;
S.erase(it);
Remove(b);
} else
return;
}
}
void Phase3() {
multiset<int>::iterator it;
while (!S.empty()) {
it = S.end();
it--;
if (*it > c) {
res++;
S.erase(it);
Remove(c);
} else
break;
}
while (!S.empty()) {
it = S.end();
it--;
res++;
S.erase(it);
int v1 = Remove(a), v2 = Remove(b);
if (!v1 || !v2) {
if (v1) S.insert(v1);
if (v2) S.insert(v2);
Remove(a + b);
break;
}
}
while (!S.empty()) {
it = S.end();
it--;
res++;
S.erase(it);
Remove(a + b);
}
}
int main() {
scanf("%d", &n);
scanf("%d %d %d", &a, &b, &c);
if (a > b) swap(a, b);
if (b > c) swap(b, c);
if (a > b) swap(a, b);
while (n--) {
int t;
scanf("%d", &t);
S.insert(t);
}
if (*S.rbegin() > a + b + c) {
printf("-1\n");
return 0;
}
Phase0();
Phase1();
Phase2();
Phase3();
printf("%d\n", res);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (arr[0] >= arr[n - 1]) {
cout << "NO" << endl;
} else
cout << "YES" << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e18 + 5;
int main() {
int n, k;
cin >> n >> k;
map<int, int> f;
vector<pair<long long, long long> > v;
for (int x, i = 0; i < n; i++) {
cin >> x;
f[x]++;
}
for (auto x : f) v.emplace_back(x.first, x.second);
n = v.size();
vector<long long> leftSum(n);
vector<long long> rightSum(n);
vector<long long> amountL(n);
vector<long long> amountR(n);
amountR[n - 1] = v[n - 1].second;
amountL[0] = v[0].second;
rightSum[n - 1] = v[n - 1].first * v[n - 1].second;
leftSum[0] = v[0].first * v[0].second;
for (int i = 1; i < n; i++) {
leftSum[i] = leftSum[i - 1] + v[i].first * v[i].second;
amountL[i] = amountL[i - 1] + v[i].second;
}
for (int i = n - 2; i >= 0; i--) {
rightSum[i] = rightSum[i + 1] + v[i].first * v[i].second;
amountR[i] = amountR[i + 1] + v[i].second;
}
for (int i = 0; i < n; i++) {
amountR[i] -= v[i].second;
amountL[i] -= v[i].second;
leftSum[i] -= v[i].first * v[i].second;
rightSum[i] -= v[i].first * v[i].second;
}
long long ans = INF;
for (int i = 0; i < n; i++) {
long long need = k - v[i].second;
if (need <= 0LL) {
cout << "0\n";
return 0;
}
long long costL =
abs((v[i].first - 1) * amountL[i] - leftSum[i]) + min(amountL[i], need);
long long costR = abs((v[i].first + 1) * amountR[i] - rightSum[i]) +
min(amountR[i], need);
if (amountL[i] >= need) {
ans = min(ans, costL);
}
if (amountR[i] >= need) {
ans = min(ans, costR);
}
ans = min(ans, costL + abs((v[i].first + 1LL) * amountR[i] - rightSum[i]) +
max(need - amountL[i], 0LL));
ans = min(ans, costR + abs((v[i].first - 1LL) * amountL[i] - leftSum[i]) +
max(need - amountR[i], 0LL));
}
cout << ans << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
unsigned int a[100000 + 5], b[100000 + 5], p;
int f(int x) { return ~((x - 1) >> 31); }
int main() {
int n, t, l, r, k, i;
long long ans = 0;
for (scanf("%d", &n); n--;) {
scanf("%d%d%d", &t, &l, &r);
if (t == 1) {
scanf("%d", &k);
p = k > 0 ? k - 1 : -k - 1;
if (k > 0)
for (i = l; i < r; ++i) a[i] = (a[i] - 1 < p ? a[i] - 1 : p) + 1;
else
for (i = l; i < r; ++i) b[i] = (b[i] - 1 < p ? b[i] - 1 : p) + 1;
} else {
for (ans = 0, i = l; i < r; ++i) ans += (a[i] + b[i]) & f(a[i]) & f(b[i]);
printf("%I64d\n", ans);
}
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
float f[1000 + 10][1000 + 10];
int n, m, ans;
int main() {
scanf("%d%d", &n, &m);
f[1][1] = m;
for (int i = 2; i <= n; i++)
for (int j = 1; j <= i; j++) {
if (f[i - 1][j - 1] - 1 > 0) f[i][j] += (f[i - 1][j - 1] - 1) / 2.0;
if (f[i - 1][j] - 1 > 0) f[i][j] += (f[i - 1][j] - 1) / 2.0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
if (f[i][j] >= 1) ans++;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const long long mod = 1e9 + 7;
template <class nmsl>
inline void read(nmsl &x) {
x = 0;
char ch = getchar(), w = 0;
while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x = w ? -x : x;
}
long long power(long long x, long long y, long long z) {
x = (x % z);
long long res = 1LL;
while (y > 0) {
if (y & 1) {
res = (res * x) % z;
}
x = (x * x) % z;
y = y >> 1;
}
return res;
}
long long modInv(long long n, long long mod) { return power(n, mod - 2, mod); }
long long nCrModP(long long n, long long r, long long mod) {
if (r == 0) return 1;
long long fac[n + 1];
fac[0] = 1;
for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;
return (fac[n] * modInv(fac[r], mod) % mod * modInv(fac[n - r], mod) % mod) %
mod;
}
long long countSetBits(long long n) {
long long count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
bool vis[n][m];
memset(vis, false, sizeof(vis));
char arr[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> arr[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int c = 0;
for (int k = i + 1; k < n; k++) {
if (arr[k][j] == arr[i][j]) {
vis[k][j] = true;
c++;
}
}
for (int k = j + 1; k < m; k++) {
if (arr[i][k] == arr[i][j]) {
vis[i][k] = true;
c++;
}
}
if (c > 0) vis[i][j] = true;
}
}
string s = "";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!vis[i][j]) {
s += arr[i][j];
}
}
}
cout << s << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int power(long long int x, long long int y) {
long long int temp;
if (y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
bool prime_check(long long int n) {
long long int i, j;
if (n == 1) {
return false;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
long long int fact(long long int n) {
long long int prod = 1;
for (long long int i = 1; i <= n; i++) {
prod = (prod * i) % 1000000007;
}
return prod;
}
bool vowl(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
const int MAX = 100000;
const int MIN = -100000;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
string grid[1002];
bool vis[1002][1002];
int n, m;
bool check(int x, int y) {
if (x >= 1 && x <= n && y >= 1 && y <= m && grid[x][y] == '#' && !vis[x][y])
return true;
return false;
}
void dfs(int x, int y) {
vis[x][y] = true;
for (int i = 0; i < 4; i++) {
int new_x = x + dx[i];
int new_y = y + dy[i];
if (check(new_x, new_y)) dfs(new_x, new_y);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i, j;
cin >> n >> m;
for (i = 1; i <= n; i++) {
cin >> grid[i];
grid[i] = '*' + grid[i];
}
bool WRow = false, WCol = false;
for (i = 1; i <= n; i++) {
int mini = MAX;
int maxi = MIN;
int cnt = 0;
for (j = 1; j <= m; j++) {
if (grid[i][j] == '#') {
mini = min(mini, j);
maxi = max(maxi, j);
cnt++;
}
}
if (mini == MAX)
WRow = true;
else if (maxi - mini + 1 != cnt) {
cout << -1 << "\n";
return 0;
}
}
for (j = 1; j <= m; j++) {
int mini = MAX;
int maxi = MIN;
int cnt = 0;
for (i = 1; i <= n; i++) {
if (grid[i][j] == '#') {
mini = min(mini, i);
maxi = max(maxi, i);
cnt++;
}
}
if (mini == MAX)
WCol = true;
else if (maxi - mini + 1 != cnt) {
cout << -1 << "\n";
return 0;
}
}
if ((!WCol && WRow) || (WCol && !WRow)) {
cout << -1 << "\n";
return 0;
}
memset(vis, false, sizeof(vis));
int count = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (grid[i][j] == '#' && !vis[i][j]) {
dfs(i, j);
count++;
}
}
}
cout << count << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
i >> p.first >> p.second;
return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
for (auto& v : t) {
i >> v;
}
return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
o << p.first << ' ' << p.second;
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
if (t.empty()) o << '\n';
for (size_t i = 0; i < t.size(); ++i) {
o << t[i] << " \n"[i == t.size() - 1];
}
return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
unsigned int logceil(long long first) {
return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0;
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
hash<T> t;
hash<U> u;
size_t operator()(const pair<T, U>& p) const {
return t(p.first) ^ (u(p.second) << 7);
}
};
} // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
l = m + 1;
r = m;
} else {
h = m - 1;
}
}
return r;
}
template <typename F>
double bshd(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
l = m;
} else {
h = m;
}
}
return (l + h) / 2;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
h = m - 1;
r = m;
} else {
l = m + 1;
}
}
return r;
}
template <typename F>
double bsld(double l, double h, const F& f) {
unsigned int r = 200;
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
h = m;
} else {
l = m;
}
}
return (l + h) / 2;
}
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
public:
vector2() {}
vector2(size_t a, size_t b, T t = T())
: vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector2<T>> {
public:
vector3() {}
vector3(size_t a, size_t b, size_t c, T t = T())
: vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T>
class vector4 : public vector<vector3<T>> {
public:
vector4() {}
vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
: vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T>
class vector5 : public vector<vector4<T>> {
public:
vector5() {}
vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
: vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};
class GOmkarAndPies {
public:
vector<std::pair<int, int>> D;
int N, M, K;
void fix(int p) {
for (int i = 0; i < K; ++i) {
int s = p ^ 1 << i;
if (D[s].first > D[p].first + 1) {
D[s] = {D[p].first + 1, D[p].second};
fix(s);
}
}
};
void solve(istream& cin, ostream& cout) {
cin >> N >> M >> K;
string S, T;
cin >> S >> T;
vector<std::pair<int, int>> P(N);
cin >> P;
for (std::pair<int, int>& p : P) {
p.first--;
p.second--;
}
vector<int> X(K);
iota(X.begin(), X.end(), 0);
vector<int> TVal, SVal;
auto apply = [&](const string& S, const vector<int>& V) {
int p = 0;
for (int i = 0; i < K; ++i) p |= (S[i] - '0') << V[i];
return p;
};
SVal.push_back(apply(S, X));
TVal.push_back(apply(T, X));
for (int i = 0; i < N; ++i) {
swap(X[P[i].first], X[P[i].second]);
SVal.push_back(apply(S, X));
TVal.push_back(apply(T, X));
}
D.assign(1 << K, {K + 1, -1});
pair<int, std::pair<int, int>> ans{K + 1, {-1, -1}};
for (int i = N - M; i >= 0; --i) {
int TX = TVal[i + M];
D[TX] = {0, i + M};
fix(TX);
int SX = SVal[i];
ans = min(ans, {D[SX].first, {i + 1, D[SX].second}});
}
cout << K - ans.first << '\n' << ans.second << '\n';
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
GOmkarAndPies solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| 10 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:250000000")
using namespace std;
int food[100009], fun[100009];
int main() {
ios_base::sync_with_stdio(false);
int T;
cin >> T;
for (int t = 0; t < T; ++t) {
int m, k;
cin >> m >> k;
vector<int> a(k), not_all(k), res(k);
for (int i = 0; i < k; ++i) {
cin >> a[i];
}
bool all_fun = true;
int summ = 0;
for (int i = 0; i < m - 1; ++i) {
cin >> food[i] >> fun[i];
food[i]--;
if (all_fun && fun[i] == 1) {
all_fun = false;
}
if (!all_fun && food[i] >= 0) {
not_all[food[i]]++;
}
}
all_fun = true;
for (int i = 0; i < m - 1; ++i) {
if (all_fun && fun[i] == 1) {
all_fun = false;
int minn = 2147483000;
for (int j = 0; j < k; ++j) {
if (not_all[j] == 0) {
if (a[j] <= summ) {
res[j] = 1;
}
minn = min(minn, a[j]);
}
}
summ -= minn;
}
if (food[i] >= 0) {
a[food[i]]--;
} else {
summ++;
}
}
for (int j = 0; j < k; ++j) {
if (res[j] == 1 || a[j] <= summ) {
cout << 'Y';
} else {
cout << 'N';
}
}
cout << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
char name[2][111];
int n, t, id, x, det;
char side[2], cate[2];
int tot[2][111];
int main() {
scanf("%s%s", name[0], name[1]);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %s %d %s", &t, side, &id, cate);
x = (side[0] == 'a') ? 1 : 0;
if (tot[x][id] >= 2) continue;
det = (cate[0] == 'y') ? 1 : 2;
tot[x][id] += det;
if (tot[x][id] >= 2) printf("%s %d %d\n", name[x], id, t);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
int tree[MAXN][10], fail[MAXN], word[MAXN], cnt;
void Init() {
for (int i = 0; i <= cnt; i++) {
word[i] = fail[i] = 0;
memset(tree[i], 0, sizeof(tree[i]));
}
cnt = 0;
}
void Insert(string s) {
int root = 0;
for (int i = 0; i < (int)s.size(); i++) {
int id = s[i] - '0';
if (!tree[root][id]) tree[root][id] = ++cnt;
root = tree[root][id];
}
word[root] = 1;
}
void get_fail() {
queue<int> q;
for (int i = 0; i < 10; i++)
if (tree[0][i]) {
fail[tree[0][i]] = 0;
q.push(tree[0][i]);
}
while (!q.empty()) {
int rt = q.front();
q.pop();
for (int i = 0; i < 10; i++) {
if (tree[rt][i]) {
fail[tree[rt][i]] = tree[fail[rt]][i];
word[tree[rt][i]] += word[fail[tree[rt][i]]];
q.push(tree[rt][i]);
} else
tree[rt][i] = tree[fail[rt]][i];
}
}
}
string s;
int x, tot;
void dfs(string str, int sum) {
if (sum == x) {
for (int i = 0; i < (int)str.size(); i++) {
for (int j = i, cal = 0; j < (int)str.size(); j++) {
cal += str[j] - '0';
if (cal < x && x % cal == 0) return;
}
}
Insert(str);
} else
for (int i = 1; i <= 9 && i + sum <= x; i++)
dfs(str + char('0' + i), sum + i);
}
int main(void) {
Init();
cin >> s >> x;
dfs("", 0);
get_fail();
vector<int> dp(cnt, s.size());
dp[0] = 0;
for (int i = 0; i < (int)s.size(); i++) {
vector<int> tp(cnt, s.size());
for (int j = 0; j < cnt; j++) {
tp[j] = min(tp[j], dp[j] + 1);
if (!word[tree[j][s[i] - '0']])
tp[tree[j][s[i] - '0']] = min(tp[tree[j][s[i] - '0']], dp[j]);
}
swap(dp, tp);
}
int ans = s.size();
for (int i = 0; i < cnt; i++) ans = min(ans, dp[i]);
cout << ans << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
int x = 0, y = 0, s = 0, aux, i;
cin >> n;
aux = n;
if (aux % 7 == 0) {
for (i = 1; i <= aux / 7; i++) cout << "7";
return 0;
}
while (n > 0) {
s++;
n -= 4;
if (n % 7 == 0 && n > 0) {
x = s;
y = n / 7;
break;
}
}
if (x && y) {
for (i = 1; i <= x; i++) cout << "4";
for (i = 1; i <= y; i++) cout << "7";
} else if (!x && !y) {
if (aux % 4 == 0)
for (i = 1; i <= aux / 4; i++) cout << "4";
else
cout << "-1\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int m[5][5] = {
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};
int a;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cin >> a;
m[i - 1][j] = m[i - 1][j] ^ (a % 2);
m[i][j - 1] = m[i][j - 1] ^ (a % 2);
m[i][j] = m[i][j] ^ (a % 2);
m[i][j + 1] = m[i][j + 1] ^ (a % 2);
m[i + 1][j] = m[i + 1][j] ^ (a % 2);
}
}
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << m[i][j];
}
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q, i, l, r, d;
cin >> q;
for (i = 0; i < q; i++) {
cin >> l >> r >> d;
int x = r / d;
if ((d * 1) < l)
cout << d * 1 << endl;
else if ((x * d) <= r)
cout << ((x + 1) * d) << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return w ? -x : x;
}
long long f[1010][1010];
const int mod = 1e9 + 7;
long long k, pa, pb;
long long inv_pa, inv_pb, inv_pa_pb;
long long fastpow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) (ans *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ans;
}
long long dp(long long a, long long b) {
if (a + b >= k) return (a + b + 1ll * pa * inv_pb % mod) % mod;
if (f[a][b] != -1) return f[a][b];
return f[a][b] = ((1ll * pa * dp(a + 1, b)) % mod +
(1ll * pb * dp(a, a + b)) % mod) %
mod * inv_pa_pb % mod;
}
int main() {
k = read(), pa = read(), pb = read();
inv_pa = fastpow(pa, mod - 2);
inv_pb = fastpow(pb, mod - 2);
inv_pa_pb = fastpow(pa + pb, mod - 2);
memset(f, -1, sizeof(f));
printf("%lld", dp(1, 0));
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long double mx, tmp;
string res;
void Update(long double value, string s) {
if (value - mx > 1e-10) {
mx = value;
res = s;
}
}
int main() {
long double x, y, z;
cin >> x >> y >> z;
mx = log(x) * pow(y, z);
res = "x^y^z";
Update(log(x) * pow(z, y), "x^z^y");
Update(log(x) * y * z, "(x^y)^z");
Update(log(x) * y * z, "(x^z)^y");
Update(log(y) * pow(x, z), "y^x^z");
Update(log(y) * pow(z, x), "y^z^x");
Update(log(y) * x * z, "(y^x)^z");
Update(log(y) * x * z, "(y^z)^x");
Update(log(z) * pow(x, y), "z^x^y");
Update(log(z) * pow(y, x), "z^y^x");
Update(log(z) * x * y, "(z^x)^y");
Update(log(z) * x * y, "(z^y)^x");
cout << res << endl;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
int n, a[N];
int t[M];
int f[N], p[M], ans;
int lowbit(int x) { return x & (-x); }
void Upd(int pos, int x) {
for (int i = pos; i < M; i += lowbit(i)) t[i] = (t[i] + x) % mod;
}
int Qry(int pos) {
int ret = 0;
for (int i = pos; i > 0; i -= lowbit(i)) ret = (ret + t[i]) % mod;
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
memset(t, 0, sizeof(t));
memset(p, 0, sizeof(p));
Upd(1, 1);
for (int i = 1; i <= n; ++i) {
p[a[i]] = i;
int tmp = Qry(a[i] + 1);
int tmp1 = Qry(a[i]);
f[i] = 1ll * a[i] * tmp % mod;
Upd(a[i] + 1, ((f[i] - (tmp - tmp1)) % mod + mod) % mod);
}
ans = 0;
for (int i = 1; i < M; ++i)
if (p[i] != 0) ans = (ans + f[p[i]]) % mod;
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int l = 0; l < t; l++) {
long long n, s, k;
cin >> n >> s >> k;
unordered_set<long long> tilos;
for (int i = 0; i < k; ++i) {
long long x;
cin >> x;
tilos.insert(x);
}
for (long long i = 0; i <= k; i++) {
if ((s + i <= n) && (tilos.find(s + i) == tilos.end())) {
cout << i << endl;
break;
}
if ((s - i >= 1) && (tilos.find(s - i) == tilos.end())) {
cout << i << endl;
break;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "no-stack-protector")
using namespace std;
using ll = long long;
using ld = double;
using pii = pair<int, int>;
using uint = unsigned int;
using ull = unsigned long long;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
default_random_engine generator;
void solve();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << setprecision(16) << fixed;
cerr << setprecision(16) << fixed;
int tests = 1;
for (int test = 1; test <= tests; ++test) {
solve();
}
}
const int MAXN = 100100;
vector<int> g[MAXN];
vector<pii> path;
void dfs(int v, int k, int lim, int p = -1) {
for (int i = 0; i < (int)(g[v]).size(); ++i) {
int u = g[v][i];
if (u == p) {
swap(g[v][i], g[v].back());
g[v].pop_back();
break;
}
}
int cur = k;
int i = 0;
for (; i < (int)(g[v]).size() && cur < lim; ++i) {
int u = g[v][i];
path.push_back({u, ++cur});
dfs(u, cur, lim, v);
path.push_back({v, cur});
}
if (i == (int)(g[v]).size()) {
if (k != 0) {
path.push_back({v, k - 1});
}
return;
}
int rem = (int)(g[v]).size() - i;
assert(k - rem - 1 < cur);
cur = k - rem - 1;
assert(cur >= 0);
path.push_back({v, cur});
for (; i < (int)(g[v]).size(); ++i) {
int u = g[v][i];
path.push_back({u, ++cur});
dfs(u, cur, lim, v);
path.push_back({v, cur});
}
assert(cur == k - 1);
}
void solve() {
int n;
cin >> n;
path.clear();
for (int i = 0; i < n; ++i) {
g[i].clear();
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
path.push_back({0, 0});
int maxx = (int)(g[0]).size();
for (int i = 1; i < n; ++i) {
maxx = max(maxx, (int)(g[i]).size());
}
dfs(0, 0, maxx);
assert((int)(path).size() <= 1e6);
for (int i = 0; i < (int)(path).size(); ++i) {
assert(path[i].second <= maxx);
}
cout << (int)(path).size() << "\n";
for (auto [x, y] : path) {
cout << x + 1 << " " << y << "\n";
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const long long infll = (long long)1.01e18;
const long double eps = 1e-9;
const long double pi = acos((long double)-1);
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x; }
void precalc() {}
const int maxn = (int)3e5 + 5;
int n;
int a[maxn];
bool read() {
if (scanf("%d", &n) < 1) {
return false;
}
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
return true;
}
int m;
int p[maxn], ts[maxn];
int get(int x) { return (p[x] == x ? x : p[x] = get(p[x])); }
void unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) {
return;
}
if (ts[x] < ts[y]) {
swap(x, y);
}
p[y] = x;
ts[x] += ts[y];
}
set<pair<int, int>, greater<pair<int, int>>> st;
deque<int> vs[maxn];
void merge(int x, int y, int k) {
if (k < ((int)(vs[y]).size()) - 2) {
int tmp = vs[x].front();
vs[x].pop_front();
vs[x].push_back(tmp);
}
int curk = ((int)(vs[y]).size());
int lst = vs[x].back();
unite(vs[x].back(), vs[y].back());
vs[x].pop_back();
vs[y].pop_back();
int pr = lst;
while (curk > k) {
pr = vs[x].back();
unite(vs[x].back(), vs[y].back());
vs[x].pop_back();
vs[y].pop_back();
curk -= 2;
}
vs[x].push_back(pr);
while (!vs[y].empty()) {
vs[x].push_back(vs[y].back());
vs[y].pop_back();
}
vs[x].push_back(lst);
}
int ids[maxn];
void printAns() {
int curid = 0;
for (int i = 0; i < m; i++) {
if (p[i] == i) {
ids[i] = curid++;
}
}
printf("%d\n", curid);
int cur = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < a[i]; j++) {
printf("%d ", ids[get(cur++)] + 1);
}
printf("\n");
}
}
void solve() {
m = 0;
st.clear();
for (int i = 0; i < n; i++) {
st.insert(make_pair(a[i], i));
vs[i].clear();
for (int j = 0; j < a[i]; j++) {
vs[i].push_back(m++);
}
}
for (int i = 0; i < m; i++) {
p[i] = i;
ts[i] = 1;
}
long long s = 0;
for (int i = 0; i < n; i++) {
s += a[i] - 2;
}
if (2 * (st.begin()->first - 2) - s >= 3) {
auto it = st.begin();
it++;
while (it != st.end()) {
merge(st.begin()->second, it->second, -it->first + 2);
it++;
}
printAns();
return;
}
while (((int)(st).size()) > 1) {
int x = st.begin()->second;
st.erase(st.begin());
int y = st.begin()->second;
st.erase(st.begin());
bool found = false;
for (int k = -((int)(vs[y]).size()) + 2; k <= ((int)(vs[y]).size()) - 2;
k += 2) {
int na = ((int)(vs[x]).size()) - 2 + k;
if (!na) {
continue;
}
long long ns = s;
ns -= ((int)(vs[y]).size()) - 2;
ns += k;
if (2 * na - ns >= 3) {
continue;
}
if (!st.empty()) {
int aa = st.begin()->first - 2;
if (2 * aa - ns >= 3) {
continue;
}
}
found = true;
merge(x, y, k);
s = ns;
break;
}
assert(found);
st.insert(make_pair(((int)(vs[x]).size()), x));
}
printAns();
}
int main() {
precalc();
while (read()) {
solve();
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int c, r, n, k;
int mp[15][15];
int dp[15][15];
int main() {
cin >> r >> c >> n >> k;
memset(mp, 0, sizeof(mp));
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
mp[i][j] = 0;
}
}
int x, y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
mp[x][y] = 1;
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mp[i][j];
}
}
int ans = 0;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
for (int a = i; a <= r; a++) {
for (int b = j; b <= c; b++) {
if (dp[a][b] - dp[i - 1][b] - dp[a][j - 1] + dp[i - 1][j - 1] >= k) {
ans++;
}
}
}
}
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000005;
const long long oo = 1e18;
const double eps = 1e-11;
int x, y, z;
int xx1, yy1, z1;
int a, b, c;
bool win(int X, int Y, int Z) {
if (X > 0 && xx1 <= 0) return 1;
if (X <= 0) return 0;
if (Y <= z1) return 0;
if (yy1 <= Z) return 1;
int cc, bb;
cc = X / (yy1 - Z);
bb = xx1 / (Y - z1);
if (X % (yy1 - Z) != 0) cc++;
if (xx1 % (Y - z1) != 0) bb++;
if (cc > bb) return 1;
return 0;
}
int sol = 1e9;
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
cin >> x >> y >> z;
cin >> xx1 >> yy1 >> z1;
cin >> a >> b >> c;
for (int i = 0; i <= 1000; i++)
for (int j = 0; j <= 200; j++)
for (int k = 0; k <= 200; k++) {
if (win(x + i, y + j, z + k)) {
sol = min(sol, i * a + b * j + c * k);
}
}
cout << sol << endl;
}
| 5 |
#include <bits/stdc++.h>
const long long MXN = 5e5 + 4;
const long long MNN = 1e3 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long OO = 1e9 + 500;
using namespace std;
long long l, r;
int main() {
ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
;
cin >> l >> r;
cout << "YES" << '\n';
for (long long i = 0; i < r - l; i += 2) {
cout << i + l << ' ' << l + i + 1 << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
char cs[100011];
int a[100011];
int N, K;
int main() {
scanf("%s", cs);
scanf("%d", &K);
N = strlen(cs);
if (1ll * N * (N + 1) / 2 < K) {
printf("No such line.\n");
return 0;
}
for (int i = (0); i < (int)(N); i++) a[i] = i;
int top = N;
K--;
long long cnt[26];
int ct[26];
while (top > 0 && K >= 0) {
memset((cnt), 0, sizeof(cnt));
memset((ct), 0, sizeof(ct));
for (int i = (0); i < (int)(top); i++) {
int idx = cs[a[i]] - 'a';
cnt[idx] += N - a[i];
ct[idx]++;
}
int u;
for (int i = (0); i < (int)(26); i++)
if (K >= cnt[i])
K -= cnt[i];
else {
u = i;
break;
}
printf("%c", (char)('a' + u));
K -= ct[u];
int tmp = 0;
for (int i = (0); i < (int)(top); i++) {
int idx = cs[a[i]] - 'a';
if (idx == u && a[i] < N - 1) a[tmp++] = a[i] + 1;
}
top = tmp;
}
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
uint64_t sum(uint64_t x) {
uint64_t sum = 0;
uint64_t no;
while (x > 0) {
no = x % 10;
x = x / 10;
sum = sum + no;
}
return (sum);
}
int main() {
uint64_t num, num2;
cin >> num >> num2;
uint64_t ans, x;
x = sum(num);
uint64_t last, mid, first;
first = 1;
last = num;
mid = (first + last) / 2;
while (first <= last) {
x = sum(mid);
if (mid - x < num2) {
first = mid + 1;
} else {
last = mid - 1;
}
mid = (first + last) / 2;
}
ans = num - mid;
if (ans > 0) {
cout << "" << ans;
} else {
cout << "0";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long BIT[400005][2];
void upd(long long idx, long long val, long long I) {
while (idx <= n) {
BIT[idx][I] += val;
idx += (idx & (-idx));
}
}
long long que(long long idx, long long I) {
long long anse = 0;
while (idx) {
anse += BIT[idx][I];
idx -= (idx & (-idx));
}
return anse;
}
long long q;
long long k;
long long a;
long long b;
int main() {
cin >> n >> k >> a >> b >> q;
for (long long i = 1; i <= n; ++i) {
upd(i, b, 0);
upd(i, a, 1);
}
long long typ, dd, aa, ab;
while (q--) {
cin >> typ;
if (typ == 1) {
cin >> dd >> aa;
ab = aa;
long long idx = dd;
aa = min(aa, que(idx, 0) - que(idx - 1, 0));
ab = min(ab, que(idx, 1) - que(idx - 1, 1));
upd(dd, -aa, 0);
upd(dd, -ab, 1);
} else {
cin >> dd;
long long anse = 0;
anse += que(dd - 1, 0);
anse += que(n, 1);
anse -= que(dd + k - 1, 1);
cout << ((dd - 1) * b + (n - (dd + k - 1)) * a - anse) << "\n";
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
priority_queue<int> pq;
int main() {
int n;
scanf("%d", &n);
long long res = 0;
int x;
scanf("%d", &x);
pq.push(x);
for (int i = 1; i < n; ++i) {
scanf("%d", &x);
pq.push(x);
if (pq.top() > x) {
res += pq.top() - x;
pq.pop();
pq.push(x);
}
}
printf("%lld", res);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int i, n, rez;
string s;
int main(void) {
cin >> s;
s = "00" + s;
n = s.length() - 1;
i = n;
while (s[i] == '0') --i;
while (i > 0) {
++rez;
char ch = s[i - 1];
--i;
while (s[i] == ch && i >= 0) --i;
}
cout << rez;
return (0);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void maloosh_lazma() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int main() {
maloosh_lazma();
int n;
cin >> n;
int *x = new int[n];
for (int i = 0; i < n; i++) {
cin >> x[i];
}
sort(x, x + n);
long long now = 1;
for (int i = 0; i < n; i++) {
if (x[i] >= now) {
now++;
}
}
cout << now;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int q = 1; q <= t; q++) {
long long int n;
long long int x;
cin >> n >> x;
vector<long long int> skls;
for (int i = 0; i < n; i++) {
long long int inp;
cin >> inp;
skls.push_back(inp);
}
sort(skls.begin(), skls.end());
long long int cnt = 0;
long long int sk = 0;
long long int ptr = 1;
for (int i = n - 1; i >= 0; i--) {
sk = ptr * skls[i];
if (sk >= x) {
cnt++;
ptr = 1;
sk = 0;
} else {
ptr++;
}
}
cout << cnt << endl;
}
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
ll n,k,m;
cin>>n>>k>>m;
map<ll,ll>mp;
for(int i=0;i<m;i++){
ll x;
cin>>x;
mp[x]=1;
}
if((n-m)%(k-1)){
cout<<"NO"<<endl;
return;
}
ll cnt=0;
ll have=k/2;
ll xd=0,flag=0;
for(int i=1;i<=n;i++){
if(mp[i]!=1)cnt++;
else{
xd++;
if(cnt>=have && (n-i-(m-xd))>=have){
flag=1;
break;
}
}
}
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
ll t=1;
cin>>t;
while(t--){
solve();
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, c, d, x[25][62] = {}, high = 0;
cin >> a;
for (int b = 0; b < a; b++) {
cin >> c >> d;
x[c][d]++;
high = max(high, x[c][d]);
}
cout << high << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[100000];
int con[1000000];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i <= n; i++) {
con[i] = 0;
}
int coun = 0;
for (int i = 0; i < m; i++) {
int k;
scanf("%d", &k);
int con1 = 0;
for (int j = 0; j < k; j++) {
cin >> a[j];
if (a[j] > 0) {
if (con[a[j]] == -1) {
con1 = 1;
} else {
con[a[j]] = 1;
}
} else {
if (con[-a[j]] == 1) {
con1 = 1;
} else {
con[-a[j]] = -1;
}
}
}
if (con1) {
coun++;
}
for (int j = 0; j < k; j++) {
if (a[j] > 0) {
con[a[j]] = 0;
} else {
con[-a[j]] = 0;
}
}
}
if (coun == m) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
const int MOD2 = 998244353;
const int INF = 1 << 30;
const ll INF2 = (ll)1 << 60;
void scan(int& a) { scanf("%d", &a); }
void scan(long long& a) { scanf("%lld", &a); }
template <class T, class L>
void scan(pair<T, L>& p) {
scan(p.first);
scan(p.second);
}
template <class T>
void scan(T& a) {
cin >> a;
}
template <class T>
void scan(vector<T>& vec) {
for (auto&& it : vec) scan(it);
}
void in() {}
template <class Head, class... Tail>
void in(Head& head, Tail&... tail) {
scan(head);
in(tail...);
}
void print(const int& a) { printf("%d", a); }
void print(const long long& a) { printf("%lld", a); }
void print(const double& a) { printf("%.15lf", a); }
template <class T, class L>
void print(const pair<T, L>& p) {
print(p.first);
putchar(' ');
print(p.second);
}
template <class T>
void print(const T& a) {
cout << a;
}
template <class T>
void print(const vector<T>& vec) {
if (vec.empty()) return;
print(vec[0]);
for (auto it = vec.begin(); ++it != vec.end();) {
putchar(' ');
print(*it);
}
}
void out() { putchar('\n'); }
template <class T>
void out(const T& t) {
print(t);
putchar('\n');
}
template <class Head, class... Tail>
void out(const Head& head, const Tail&... tail) {
print(head);
putchar(' ');
out(tail...);
}
template <class T>
void dprint(const T& a) {
cerr << a;
}
template <class T>
void dprint(const vector<T>& vec) {
if (vec.empty()) return;
cerr << vec[0];
for (auto it = vec.begin(); ++it != vec.end();) {
cerr << " " << *it;
}
}
void debug() { cerr << endl; }
template <class T>
void debug(const T& t) {
dprint(t);
cerr << endl;
}
template <class Head, class... Tail>
void debug(const Head& head, const Tail&... tail) {
dprint(head);
cerr << " ";
debug(tail...);
}
ll intpow(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) ans *= a;
a *= a;
b /= 2;
}
return ans;
}
ll modpow(ll a, ll b, ll p) {
ll ans = 1;
while (b) {
if (b & 1) (ans *= a) %= p;
(a *= a) %= p;
b /= 2;
}
return ans;
}
ll updivide(ll a, ll b) {
if (a % b == 0)
return a / b;
else
return (a / b) + 1;
}
template <class T>
void chmax(T& a, const T b) {
if (b > a) a = b;
}
template <class T>
void chmin(T& a, const T b) {
if (b < a) a = b;
}
int main() {
ll n;
in(n);
if (n == 0) {
out(0, 0, 0);
return 0;
}
vector<ll> vec(50, 0);
vec[0] = 0;
vec[1] = 1;
for (int i = 2; i < 50; i++) {
vec[i] = vec[i - 1] + vec[i - 2];
if (vec[i] == n) {
out(vec[i - 1], vec[i - 2], 0);
return 0;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
set<pair<pair<int, int>, int> > pcs;
int l[1010101];
int u[1010101];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
int i2 = 2;
pcs.insert({{1, n}, 1});
pcs.insert({{n + 1, n + 2}, 1});
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
char s;
cin >> s;
auto it = pcs.lower_bound({{x, n + 3}, 0});
it--;
auto t = *it;
if (t.first.first > x || t.first.second < x) {
cout << "0\n";
} else {
if (s == 'L') {
cout << x - l[t.second] << '\n';
} else {
cout << y - u[t.second] << '\n';
}
pcs.erase(it);
if (x > t.first.first) {
l[i2] = l[t.second];
u[i2] = u[t.second];
if (s == 'L') {
u[i2] = y;
}
pcs.insert({{t.first.first, x - 1}, i2++});
}
if (x < t.first.second) {
l[i2] = l[t.second];
u[i2] = u[t.second];
if (s == 'U') {
l[i2] = x;
}
pcs.insert({{x + 1, t.first.second}, i2++});
}
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
double P[110], PP, PP2, MAX;
int n;
int main() {
int i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%lf", &P[i]);
sort(P, P + n);
if (P[n - 1] == 1.0) {
printf("%.13lf\n", 1.0);
return 0;
}
for (i = 0; i < n; i++) {
PP = 1;
PP2 = 0;
for (j = i; j < n; j++) {
PP = PP * (1 - P[j]);
PP2 += P[j] / (1 - P[j]);
}
if (MAX < PP * PP2) MAX = PP * PP2;
}
printf("%.13lf\n", MAX);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool in_range(long long l, long long v, long long r) {
return (l <= v && v <= r);
}
int main() {
long long x;
cin >> x;
x = ((-x) % 360 + 360) % 360;
int ans = 0;
if (x >= 225 && x < 315)
ans = 1;
else if (x >= 135 && x < 225)
ans = 2;
else if (x > 45 && x < 135)
ans = 3;
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcountll(s);
}
template <class T>
T gcd(T a, T b) {
T r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
long long n, L, d;
int RR[2025];
long long f[2025][2025];
map<pair<int, long long>, long long> M;
long long cal(int id, long long x) {
if (M[make_pair(id, x)]) return M[make_pair(id, x)] - 1;
fflush(stdout);
cout << "? " << id << " " << x << endl;
long long res;
cin >> res;
M[make_pair(id, x)] = res + 1;
return res;
}
long long find(int id, int vt) {
long long &res = f[id][vt];
if (res != -1) return res;
long long x = vt * d;
long long l = 0, r = (long long)(1e18);
while (l < r) {
long long mid = (l + r) / 2;
if (cal(id, mid) < x)
l = mid + 1;
else
r = mid;
}
res = l;
return res;
}
pair<vector<int>, vector<int> > divide(vector<int> V, int u, int v, int k) {
int len = (v - u + 1);
int id = V[rand() % len];
long long vt = find(id, k);
vector<int> behon, lonhon, bang;
for (int i = 0; i < (((int)(V).size())); ++i) {
int x = V[i];
long long y = cal(x, vt);
if (y > k * d)
behon.push_back(x);
else if (y == k * d)
bang.push_back(x);
else
lonhon.push_back(x);
}
pair<vector<int>, vector<int> > res;
if (((int)(behon).size()) > k - u) {
pair<vector<int>, vector<int> > temp =
divide(behon, u, u + ((int)(behon).size()) - 1, k);
res = temp;
for (int i = 0; i < (((int)(bang).size())); ++i)
res.second.push_back(bang[i]);
for (int i = 0; i < (((int)(lonhon).size())); ++i)
res.second.push_back(lonhon[i]);
temp.first.clear();
temp.second.clear();
} else if (((int)(lonhon).size()) > v - k) {
pair<vector<int>, vector<int> > temp =
divide(lonhon, v - ((int)(lonhon).size()) + 1, v, k);
res = temp;
for (int i = 0; i < (((int)(behon).size())); ++i)
res.first.push_back(behon[i]);
for (int i = 0; i < (((int)(bang).size())); ++i)
res.first.push_back(bang[i]);
temp.first.clear();
temp.second.clear();
} else {
for (int i = 0; i < (((int)(behon).size())); ++i)
res.first.push_back(behon[i]);
for (int i = 0; i < (((int)(bang).size())); ++i) {
if (((int)(res.first).size()) < k - u + 1)
res.first.push_back(bang[i]);
else
res.second.push_back(bang[i]);
}
for (int i = 0; i < (((int)(lonhon).size())); ++i)
res.second.push_back(lonhon[i]);
}
V.clear();
behon.clear();
bang.clear();
lonhon.clear();
return res;
}
void go(vector<int> V, int u, int v) {
int mid = (u + v) / 2;
int len = (v - u + 1);
if (u == v) {
RR[V[0]] = u;
return;
}
pair<vector<int>, vector<int> > VV = divide(V, u, v, mid);
go(VV.first, u, mid);
go(VV.second, mid + 1, v);
}
void sol() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
memset(f, -1, sizeof(f));
cin >> n >> L;
d = L / n;
vector<int> V;
for (int i = (1); i <= (n); ++i) V.push_back(i);
go(V, 1, n);
vector<long long> vv;
vv.push_back(0);
for (int i = (1); i <= (n); ++i) vv.push_back(find(i, RR[i]));
sort((vv).begin(), (vv).end());
cout << "!" << endl;
for (int i = (1); i <= (n); ++i)
cout << vv[RR[i] - 1] << " " << vv[RR[i]] << endl;
}
int main() {
sol();
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> g[N];
int du[N], fa[N];
bool vis[N];
queue<int> q;
void dfs(int r, int f) {
fa[r] = f;
for (int i = 0; i < g[r].size(); i++) {
int v = g[r][i];
if (v != f) dfs(v, r);
}
q.push(r);
}
vector<int> ans;
void del(int x) {
vis[x] = true;
ans.push_back(x);
for (int i = 0; i < g[x].size(); i++) {
int v = g[x][i];
if (!vis[v] && fa[x] != v) del(v);
}
}
int main() {
int n;
scanf("%d", &n);
int x;
memset(du, 0, sizeof(du));
memset(vis, false, sizeof(vis));
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
if (x == 0) continue;
du[x] += 1;
du[i] += 1;
g[i].push_back(x);
g[x].push_back(i);
}
dfs(1, -1);
while (!q.empty()) {
int x = q.front();
q.pop();
if (du[x] % 2 == 0) {
del(x);
du[fa[x]] -= 1;
}
}
if (ans.size() != n)
printf("NO\n");
else {
printf("YES\n");
for (int i = 0; i < n; i++) printf("%d\n", ans[i]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, q, w, x, y, r, rr, i;
char s[30], sx[30], sy[30];
map<string, int> mapa;
struct padure {
int tata, rel;
} v[100005], aux, aux2, xx, yy;
padure tata(int x) {
if (v[x].tata == x) {
aux.tata = x;
aux.rel = v[x].rel;
return aux;
} else {
aux2 = tata(v[x].tata);
v[x].rel ^= v[v[x].tata].rel;
aux.rel ^= aux2.rel;
aux.tata = aux2.tata;
v[x].tata = aux.tata;
return aux;
}
}
int main() {
scanf("%d%d%d\n", &n, &q, &w);
for (i = 1; i <= n; i++) {
scanf("%s ", &s);
mapa[s] = i;
v[i].tata = i;
v[i].rel = 0;
}
for (i = 1; i <= q; i++) {
scanf("%d %s %s\n", &r, &sx, &sy);
x = mapa.find(sx)->second;
y = mapa.find(sy)->second;
r--;
xx = tata(x);
yy = tata(y);
if (xx.tata != yy.tata) {
v[xx.tata].tata = yy.tata;
v[xx.tata].rel = r ^ v[x].rel ^ v[y].rel;
printf("YES\n");
} else {
xx = tata(x);
yy = tata(y);
rr = v[x].rel ^ v[y].rel;
if (rr != r)
printf("NO\n");
else
printf("YES\n");
}
}
for (i = 1; i <= w; i++) {
scanf("%s %s\n", &sx, &sy);
x = mapa.find(sx)->second;
y = mapa.find(sy)->second;
xx = tata(x);
yy = tata(y);
rr = v[x].rel ^ v[y].rel;
if (xx.tata == yy.tata)
printf("%d\n", rr + 1);
else
printf("3\n");
}
return 0;
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool com(ll a,ll b)
{
return a<b;
}
#define f(i,a,n) for(ll i=a;i<n;i++)
#define rf(i,n,a) for(ll i=n;i>=a;i--)
#define fe(x,a) for(auto x: a)
#define test ll T;cin>>T;while(T--)
#define endl "\n"
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define ok cout<<endl
#define vl vector<ll>
#define vf vector<float>
#define vc vector<char>
#define vs vector<string>
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sl set<ll>
#define pll pair<ll,ll>
#define psl pair<string,ll>
#define msl map<string,ll>
#define mll map<ll,ll>
#define fastio {ios_base::sync_with_stdio(false);cin.tie(NULL);}
#define ft first
#define s second
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/__gcd(a,b)
int main()
{
fastio
test
{
ll n,m,d,x;
cin>>n>>m>>d;
x = (m-1) + m*(n-1);
if(x==d)
yes;
else
no;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long double x, xx, yy, v, T, vx, vy, wx, wy, y;
bool check(long double t) {
long double disx = min(t, T) * (vx);
if (t >= T) disx += (t - T) * wx;
long double disy = min(t, T) * (vy);
if (t >= T) disy += (t - T) * wy;
disx = disx + x;
disy = disy + y;
long double ans = (disx - xx) * (disx - xx) + (disy - yy) * (disy - yy);
if (ans <= v * t * v * t) return true;
return false;
}
int main() {
cin >> x >> y >> xx >> yy;
cin >> v >> T;
cin >> vx >> vy >> wx >> wy;
long double r = 1e12, l = 0;
for (int i = 1; i <= 300; i++) {
long double mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
cout << setprecision(15) << r;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
T &chmin(T &a, const T &b) {
return a = min(a, b);
}
template <typename T>
T &chmax(T &a, const T &b) {
return a = max(a, b);
}
template <typename T>
T inf;
template <>
constexpr int inf<int> = 1e9;
template <>
constexpr ll inf<ll> = 1e18;
template <>
constexpr ld inf<ld> = 1e30;
struct yes_no : numpunct<char> {
string_type do_truename() const { return "Yes"; }
string_type do_falsename() const { return "No"; }
};
ll step[30][256][256];
void update(vector<ll> &dp, int index) {
vector<ll> ndp(256, ll(1e18));
for (int i = 0; i < (int)(256); i++)
for (int j = 0; j < (int)(256); j++)
chmin(ndp[j], dp[i] + step[index][i][j]);
dp = move(ndp);
}
int main() {
locale loc(locale(), new yes_no);
cout << boolalpha << setprecision(12) << fixed;
cout.imbue(loc);
int x, k, n, q;
cin >> x >> k >> n >> q;
vector<int> c(k);
for (int i = 0; i < (int)(k); i++) cin >> c[i];
vector<pair<int, int>> vec;
ll res = 0;
for (int i = 0; i < (int)(q); i++) {
int p, w;
cin >> p >> w;
if (p + x > n)
res += w;
else
vec.emplace_back(p - 1, w);
}
vec.emplace_back(0, 0);
vec.emplace_back(n - x, 0);
sort((vec).begin(), (vec).end());
for (int i = 0; i < (int)(256); i++)
for (int j = 0; j < (int)(256); j++) step[0][i][j] = 1e18;
for (int i = 0; i < (int)(256); i++) {
if (i & 1) {
int ni = i / 2;
for (int j = 0; j < (int)(k); j++) {
if (((ni >> j) & 1) == 0) step[0][i][ni | (1 << j)] = c[j];
}
} else {
step[0][i][i / 2] = 0;
}
}
for (int i = 0; i < (int)(29); i++) {
for (int j = 0; j < (int)(256); j++)
for (int k = 0; k < (int)(256); k++) step[i + 1][j][k] = 1e18;
for (int j = 0; j < (int)(256); j++)
for (int k = 0; k < (int)(256); k++)
for (int l = 0; l < (int)(256); l++) {
chmin(step[i + 1][j][l], step[i][j][k] + step[i][k][l]);
}
}
vector<ll> dp(256, ll(1e18));
dp[(1 << x) - 1] = 0;
for (int i = 0; i < (int)(int(vec.size()) - 1); i++) {
int count = vec[i + 1].first - vec[i].first;
for (int j = 0; j < (int)(30); j++) {
if ((count >> j) & 1) update(dp, j);
}
for (int j = 0; j < (int)(256); j++) {
if (j & 1) dp[j] += vec[i + 1].second;
}
}
cout << dp[(1 << x) - 1] + res << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007, M = 60, LOG = 63;
const int N = M * 6;
const long long mod = MOD;
const int inv2 = (mod + 1) / 2;
const double pi = acos(-1);
int m;
int inv[N];
int add(int a, int b) { return (a += b) >= mod ? a - mod : a; }
int dec(int a, int b) { return (a -= b) < 0 ? a + mod : a; }
int mul(int a, int b) { return (long long)a * b % mod; }
int qpow(int a, int b) {
int ret = 1;
for (; b; b >>= 1) {
if (b & 1) ret = mul(ret, a);
a = mul(a, a);
}
return ret;
}
void initinv() {
inv[1] = 1;
for (int i = 2; i < N; i++) inv[i] = mul(mod - mod / i, inv[mod % i]);
}
void rev(complex<double> *a, int n) {
for (int i = 1, j = n / 2; i < n - 1; ++i) {
if (i < j) swap(a[i], a[j]);
int k = n / 2;
while (j >= k) j -= k, k /= 2;
if (j < k) j += k;
}
}
void fft(complex<double> *a, int n, int f) {
rev(a, n);
for (int m = 1; m <= n; m <<= 1) {
complex<double> wm =
complex<double>(cos(f * 2 * pi / m), sin(f * 2 * pi / m));
for (int k = 0; k < n; k += m) {
complex<double> w = complex<double>(1, 0);
for (int j = 0; j < m / 2; ++j) {
complex<double> u = a[k + j];
complex<double> t = w * a[k + j + m / 2];
a[k + j] = u + t;
a[k + j + m / 2] = u - t;
w = w * wm;
}
}
}
}
void conv(int n, long long *x, long long *y, long long *z) {
static complex<double> p[N], q[N], a[N], b[N], c[N], d[N], w[N];
static complex<double> r(0.5, 0), h(0, -0.5), o(0, 1);
for (int i = 0, ed = n; i < ed; ++i) {
w[i] = complex<double>(cos(2 * pi * i / n), sin(2 * pi * i / n));
x[i] = (x[i] + mod) % mod, y[i] = (y[i] + mod) % mod;
p[i] = complex<double>(x[i] >> 15, x[i] & 32767),
q[i] = complex<double>(y[i] >> 15, y[i] & 32767);
}
fft(p, n, 1);
fft(q, n, 1);
for (int i = 0, ed = n; i < ed; ++i) {
int j = i ? (n - i) : 0;
static complex<double> ka, ba, kb, bb;
ka = (p[i] + conj(p[j])) * r;
ba = (p[i] - conj(p[j])) * h;
kb = (q[i] + conj(q[j])) * r;
bb = (q[i] - conj(q[j])) * h;
a[j] = ka * kb;
b[j] = ka * bb;
c[j] = kb * ba;
d[j] = ba * bb;
}
for (int i = 0, ed = n; i < ed; ++i) {
p[i] = a[i] + b[i] * o;
q[i] = c[i] + d[i] * o;
}
fft(p, n, 1);
fft(q, n, 1);
for (int i = 0, ed = n; i < ed; ++i) {
long long a, b, c, d;
a = (long long)(p[i].real() / n + 0.5) % mod;
b = (long long)(p[i].imag() / n + 0.5) % mod;
c = (long long)(q[i].real() / n + 0.5) % mod;
d = (long long)(q[i].imag() / n + 0.5) % mod;
z[i] = ((a << 30) + ((b + c) << 15) + d) % mod;
}
}
void cmult(int *a, int n, int *b, int m, int *c) {
static long long A[N], B[N], C[N];
int l;
for (l = 1; l <= (n + m - 1); l <<= 1)
;
for (int i = 0; i < n; i++) A[i] = a[i];
for (int i = 0; i < m; i++) B[i] = b[i];
fill(A + n, A + l, 0);
fill(B + m, B + l, 0);
conv(l, A, B, C);
for (int i = 0; i < l; i++) c[i] = C[i] % mod;
}
void cinv(int *a, int *b, int n) {
static int C[N];
if (n == 0) {
b[0] = qpow(a[0], mod - 2);
return;
}
cinv(a, b, n >> 1);
cmult(b, n, b, n, C);
cmult(C, n, a, n, C);
for (int i = 0; i < n; i++) b[i] = dec(mul(2, b[i]), C[i]);
fill(b + n, b + 2 * n, 0);
}
void cintegrate(int *f, int *g, int n) {
for (int i = n; i >= 1; i--) g[i] = mul(f[i - 1], inv[i]);
g[0] = 0;
}
void cderivation(int *f, int *g, int n) {
for (int i = 1; i < n; i++) g[i - 1] = mul(f[i], i);
g[n - 1] = 0;
}
void cln(int *f, int *g, int n) {
static int D[N], A[N];
cderivation(f, D, n);
cinv(f, A, n);
cmult(A, n, D, n, A);
cintegrate(A, g, n);
}
void cexp(int *a, int *b, int n) {
static int D[N];
if (n == 1) {
b[0] = 1;
return;
}
cexp(a, b, n >> 1);
cln(b, D, n);
for (int i = 0; i < n; i++) D[i] = dec(a[i], D[i]);
D[0] = add(1, D[0]);
cmult(D, n, b, n, b);
}
void csqrt(int *a, int *b, int n) {
static int C[N];
if (n == 1) {
b[0] = sqrt(a[0] + 0.5);
return;
}
csqrt(a, b, n >> 1);
cinv(b, C, n);
cmult(a, n, C, n, C);
for (int i = 0; i < n; i++) b[i] = mul(inv2, add(b[i], C[i]));
fill(b + n, b + 2 * n, 0);
}
void cpow(int *f, int *g, int n, int k) {
static int C[N];
cln(f, C, n);
for (int i = 0; i < n; i++) C[i] = mul(C[i], k);
cexp(C, g, n);
}
void cdiv(int *a, int n, int *b, int m, int *ret) {
static int c[N], d[N], e[N], l;
while (b[m - 1] == 0) m--;
if (n < m) return;
for (l = 1; l <= n - m + 1; l <<= 1)
;
reverse_copy(a, a + n, c);
reverse_copy(b, b + m, d);
for (int i = m; i < n + m + 1; i++) d[i] = 0;
memset(e, 0, sizeof(e[0]) * (l * 2));
cinv(d, e, l);
cmult(e, n - m + 1, c, n - m + 1, d);
reverse_copy(d, d + n - m + 1, ret);
}
void cmod(int *a, int n, int *b, int m, int *ret) {
static int c[N];
while (b[m - 1] == 0) m--;
cdiv(a, n, b, m, c);
cmult(c, n - m + 1, b, m, c);
for (int i = 0; i < m - 1; i++) ret[i] = add(a[i] - c[i], mod);
}
void cgcd(int *a, int n, int *b, int m, int *c) {
static int r[N];
fill(c, c + max(m, n), 0);
int i;
while (1) {
cmod(a, n, b, m, r);
for (i = m - 2; i >= 0; i--)
if (r[i]) break;
i++;
if (i == 0) break;
memcpy(a, b, sizeof(b[0]) * m);
memcpy(b, r, sizeof(r[0]) * i);
n = m;
m = i;
}
memcpy(c, b, sizeof(b[0]) * m);
int invc = qpow(c[0], mod - 2);
for (int i = 0; i < m; i++) c[i] = mul(c[i], invc);
}
int lagrange(int *f, int n, int x) {
static int C[N], D[N];
cinv(f, C, n);
cpow(C, D, n, x);
return mul(D[x - 1], qpow(x, mod - 2));
}
struct Vector {
int a[M];
Vector() { memset(a, 0, sizeof(a)); }
int &operator[](const int &i) { return a[i]; }
const int operator[](const int &i) const { return a[i]; }
int operator*(const Vector &b) {
int ret = 0;
for (int i = 0; i < m; ++i) {
if ((ret += (long long)a[i] * b[i] % MOD) >= MOD) {
ret -= MOD;
}
}
return ret;
}
Vector operator+(const Vector &b) {
Vector ret;
for (int i = 0; i < m; ++i) {
if ((ret[i] = a[i] + b[i]) >= MOD) {
ret[i] -= MOD;
}
}
return ret;
}
};
Vector operator*(int k, const Vector &b) {
Vector ret;
for (int i = 0; i < m; ++i) {
ret[i] = (long long)k * b[i] % MOD;
}
return ret;
}
struct Matrix {
int a[M][M];
int *operator[](const int &i) { return a[i]; }
const int *operator[](const int &i) const { return a[i]; }
Vector operator*(const Vector &b) {
Vector ret;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
if ((ret[i] += (long long)a[i][j] * b[j] % MOD) >= MOD) {
ret[i] -= MOD;
}
}
}
return ret;
}
};
int inverse(int a) {
return a == 1 ? 1 : (long long)(MOD - MOD / a) * inverse(MOD % a) % MOD;
}
struct Poly {
vector<int> a;
Poly() { a.clear(); }
Poly(vector<int> &a) : a(a) {}
int length() const { return a.size(); }
Poly move(int d) {
vector<int> na(d, 0);
na.insert(na.end(), a.begin(), a.end());
return Poly(na);
}
int calc(vector<int> &d, int pos) {
int ret = 0;
for (int i = 0; i < (int)a.size(); ++i) {
if ((ret += (long long)d[pos - i] * a[i] % MOD) >= MOD) {
ret -= MOD;
}
}
return ret;
}
Poly operator-(const Poly &b) {
vector<int> na(max(this->length(), b.length()));
for (int i = 0; i < (int)na.size(); ++i) {
int aa = i < this->length() ? this->a[i] : 0,
bb = i < b.length() ? b.a[i] : 0;
na[i] = (aa + MOD - bb) % MOD;
}
return Poly(na);
}
};
Poly operator*(const int &c, const Poly &p) {
vector<int> na(p.length());
for (int i = 0; i < (int)na.size(); ++i) {
na[i] = (long long)c * p.a[i] % MOD;
}
return na;
}
vector<int> Berlekamp(vector<int> a) {
int n = a.size();
Poly s, b;
s.a.push_back(1), b.a.push_back(1);
for (int i = 1, j = 0, ld = a[0]; i < n; ++i) {
int d = s.calc(a, i);
if (d) {
if ((s.length() - 1) * 2 <= i) {
Poly ob = b;
b = s;
s = s - (long long)d * inverse(ld) % MOD * ob.move(i - j);
j = i;
ld = d;
} else {
s = s - (long long)d * inverse(ld) % MOD * b.move(i - j);
}
}
}
return s.a;
}
Vector LinearRec(vector<Vector> &first, vector<int> &trans, long long k) {
int n = first.size();
int m;
static int a[N];
static int b[N];
static int c[N];
static int d[N];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
for (int i = 0; i < n; i++) a[i] = trans[i];
a[n] = 1;
reverse(a, a + n);
for (int i = 0; i < n; i++) a[i] = MOD - a[i];
for (m = 1; m <= n + 1; m <<= 1)
;
b[1] = 1;
c[0] = 1;
for (k--; k; k >>= 1) {
if (k & 1) {
memset(d, 0, sizeof(d));
cmult(c, m, b, m, c);
cmod(c, 2 * m, a, m, d);
memcpy(c, d, sizeof(d));
}
memset(d, 0, sizeof(d));
cmult(b, m, b, m, b);
cmod(b, 2 * m, a, m, d);
memcpy(b, d, sizeof(d));
}
Vector ans;
for (int i = 0; i < n; i++) ans = ans + c[i] * first[i];
return ans;
};
Vector solve(Matrix &A, long long k, Vector &b) {
vector<Vector> vs;
vs.push_back(A * b);
for (int i = 1; i < m * 2; ++i) {
vs.push_back(A * vs.back());
}
if (k == 0) {
return b;
} else if (k <= m * 2) {
return vs[k - 1];
}
Vector x;
for (int i = 0; i < m; ++i) {
x[i] = rand() % MOD;
}
vector<int> a(m * 2);
for (int i = 0; i < m * 2; ++i) {
a[i] = vs[i] * x;
}
vector<int> s = Berlekamp(a);
s.erase(s.begin());
for (int i = 0; i < s.size(); ++i) {
s[i] = (MOD - s[i]) % MOD;
}
vector<Vector> vf(vs.begin(), vs.begin() + s.size());
return LinearRec(vf, s, k);
}
int n;
long long k;
int main() {
initinv();
scanf("%lld %d %d", &k, &m, &n);
Matrix A;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
A[i][j] = 1;
}
}
for (int i = 0; i < n; ++i) {
char s[3];
scanf("%s", s);
int t1 =
'a' <= s[0] && s[0] <= 'z' ? t1 = s[0] - 'a' : t1 = s[0] - 'A' + 26;
int t2 =
'a' <= s[1] && s[1] <= 'z' ? t2 = s[1] - 'a' : t2 = s[1] - 'A' + 26;
A[t1][t2] = 0;
}
Vector b;
for (int i = 0; i < m; ++i) {
b[i] = 1;
}
int ans = solve(A, k - 1, b) * b;
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
template <class T>
inline bool cmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
inline bool cmin(T& a, const T& b) {
return a > b ? a = b, 1 : 0;
}
using namespace std;
char ss[1 << 17], *A = ss, *B = ss;
inline char gc() {
return A == B && (B = (A = ss) + fread(ss, 1, 1 << 17, stdin), A == B) ? -1
: *A++;
}
template <class T>
inline void sd(T& x) {
char c;
T y = 1;
while (c = gc(), (c < 48 || 57 < c) && c != -1)
if (c == 45) y = -1;
x = c - 48;
while (c = gc(), 47 < c && c < 58) x = x * 10 + c - 48;
x *= y;
}
const int N = 3e5 + 5, M = N << 1;
typedef int arr[N];
struct Edge {
int nx, to;
} e[M];
int n, S, ans, cntE;
arr fi, deg;
inline void add(int u, int v) { e[++cntE] = (Edge){fi[u], v}, fi[u] = cntE; }
inline void clear(int p) {
cntE = 0;
memset(fi, 0, p);
memset(deg, -1, p);
}
void dfs(int u, int p, int dis) {
if (dis > ans) ans = dis, S = u;
for (int i = fi[u], v = e[i].to; i; v = e[i = e[i].nx].to)
if (v ^ p) dfs(v, u, dis + deg[v]);
}
inline int sol() {
sd(n);
clear((n + 2) << 2);
int u, v;
for (register int i = 2, I = n + 1; i < I; ++i) {
sd(u), sd(v);
add(u, v), add(v, u);
++deg[u], ++deg[v];
}
dfs(1, ans = -1, deg[1]);
dfs(S, ans = -1, deg[S]);
return ans + 2;
}
int main() {
int q;
sd(q);
while (q--) printf("%d\n", sol());
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[100100], s = 0, d = 0, ans = 0;
bool b[100100];
void read(void) {
cin >> n;
for (long long i = 0; i < n; ++i) cin >> a[i], s += a[i];
}
void din(void) {
if (s % 3 != 0) {
cout << 0;
return;
}
s = s / 3;
for (long long i = n - 1, p = 0; i > 0; --i) {
p += a[i];
if (p == s) b[i - 1] = 1, d++;
}
for (long long i = 0, p = 0; i < n; ++i) {
p += a[i];
if (b[i]) --d;
if (p == s) ans += d;
}
cout << ans;
}
int main() {
read();
din();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long powm(long long base, long long exp, long long mod = 1000000007) {
long long ans = 1;
while (exp) {
if (exp & 1) ans = (ans * base) % mod;
exp >>= 1, base = (base * base) % mod;
}
return ans;
}
void fastscan(int &x) {
register int c = getchar();
x = 0;
for (; (c < 48 || c > 57); c = getchar())
;
for (; c > 47 && c < 58; c = getchar()) {
x = (x << 1) + (x << 3) + c - 48;
}
}
const int N = 1e3 + 5;
string str, ans;
map<long long, long long> freq;
vector<string> v;
stack<pair<string, long long> > stk;
void rekt() {
cout << "Error occurred" << endl;
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
cin.ignore();
getline(cin, str);
if (str == "int") {
cout << "int" << endl;
exit(0);
}
long long curr = 0;
for (int i = 0; i < (str).size(); ++i) {
if (str[i] == ' ') {
v.push_back(str.substr(curr, i - curr));
curr = i + 1;
}
}
v.push_back(str.substr(curr, (str).size() - curr));
for (int i = 0; i < (v).size(); i++) {
if (v[i] == "pair") {
stk.push(make_pair(v[i], i));
ans += "pair<";
}
if (i != (v).size() - 1 and stk.empty()) rekt();
long long id = stk.top().second;
if (v[i] == "int") {
freq[id]++;
ans += "int";
}
while (freq[id] == 2) {
stk.pop();
ans += ">";
if (i != (v).size() - 1 and stk.empty()) rekt();
if (i == (v).size() - 1 and stk.empty()) break;
id = stk.top().second;
freq[id]++;
}
if (freq[id] == 1) ans += ",";
}
if (!stk.empty()) rekt();
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int MOD = 1e9 + 7;
const double PI = 2 * acos(0.0);
long long a[N], b[N], c[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
if (n > 1) {
for (int i = 0; i < n; i++) {
if (i == 0) {
c[i] = max(0LL, a[i] - b[n - 1]);
} else {
c[i] = max(0LL, a[i] - b[i - 1]);
}
}
} else {
cout << a[0] << '\n';
continue;
}
long long ans = 1e18, sum = 0;
for (int i = 0; i < n; i++) {
sum += c[i];
}
for (int i = 0; i < n; i++) {
long long tmp = sum;
if (c[i]) {
tmp -= c[i];
}
ans = min(ans, a[i] + tmp);
}
cout << ans << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void solve() {
long long int n;
cin >> n;
cout << (n - 1) / 2 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int getid(int p, int c) { return (p << 1) + c; }
int n, m1, m2;
class SAT {
public:
vector<int> v[2010];
int bel[2010], ans[2010], dfn[2010], low[2010], stk[2010], top, bcnt, tim;
bool fail, vis[2010], s[2010];
int flag[2010];
bitset<2010> bt[2010];
void addedge(int s, int e) {
v[s].push_back(e);
bt[s].set(e);
}
void addregulation(int a, int b) {
int c1 = 0, c2 = 0;
if (a < 0) a = -a, c1 = 1;
if (b < 0) b = -b, c2 = 1;
addedge(getid(a, c1), getid(b, c2 ^ 1));
addedge(getid(b, c2), getid(a, c1 ^ 1));
}
void dfs(int np) {
stk[++top] = np;
dfn[np] = low[np] = ++tim;
vis[np] = 1;
for (int &x : v[np]) {
if (!dfn[x]) {
dfs(x);
low[np] = min(low[np], low[x]);
} else if (!vis[x]) {
low[np] = min(low[np], dfn[x]);
}
}
if (low[np] == dfn[np]) {
int tmp;
bcnt++;
while (tmp = stk[top--]) {
bel[tmp] = bcnt;
vis[tmp] = 0;
if (tmp == np) break;
}
}
}
void dfs2(int np) {
if (flag[np] != -1) return;
flag[np] = 1;
flag[np ^ 1] = 0;
for (int i = 2; i <= n * 2 + 1; i++) {
if (bt[np].test(i)) dfs2(i);
}
}
void trans() {
for (int i = 2; i <= n * 2 + 1; i++) bt[i].set(i);
for (int j = 2; j <= n * 2 + 1; j++) {
for (int i = 2; i <= n * 2 + 1; i++) {
if (bt[i].test(j)) bt[i] |= bt[j];
}
}
for (int i = 2; i <= n * 2 + 1; i++) {
for (int j = 2; j <= n * 2 + 1; j++) {
if (bt[i].test(j)) s[i] = s[j] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (bt[getid(i, 0)].test(getid(i, 1)) &&
bt[getid(i, 1)].test(getid(i, 0))) {
fail = 1;
break;
}
}
if (fail) return;
memset(flag, 255, sizeof(flag));
for (int i = 2; i <= n * 2 + 1; i++) {
if (bt[i].test(i ^ 1)) dfs2(i ^ 1);
}
}
void solve(vector<int> c) {
for (int &x : c) dfs2(x);
for (int i = 2; i <= n * 2 + 1; i++)
if (flag[i] == -1) dfs2(i);
for (int i = 1; i <= n; i++) printf("%c ", flag[getid(i, 1)] + '0');
}
} S1, S2;
int main() {
scanf("%d%d%d", &n, &m1, &m2);
for (int i = 1, ti, tj; i <= m1; i++) {
scanf("%d%d", &ti, &tj);
S1.addregulation(ti, tj);
}
for (int i = 1, ti, tj; i <= m2; i++) {
scanf("%d%d", &ti, &tj);
S2.addregulation(ti, tj);
}
S1.trans();
S2.trans();
if (S1.fail && S2.fail)
puts("SIMILAR");
else if (S1.fail)
S2.solve(vector<int>());
else if (S2.fail)
S1.solve(vector<int>());
else {
for (int i = 2; i <= n * 2 + 1; i++) {
if (S1.flag[i] == -1 && S2.flag[i] == 1) {
S1.solve(vector<int>({i ^ 1}));
return 0;
}
if (S2.flag[i] == -1 && S1.flag[i] == 1) {
S2.solve(vector<int>({i ^ 1}));
return 0;
}
}
for (int i = 2; i <= n * 2 + 1; i++) {
if (S1.flag[i] == -1) {
for (int j = 2; j <= n * 2 + 1; j++) {
if (S1.flag[j] == -1) {
if (S1.bt[i].test(j) && !S2.bt[i].test(j)) {
S2.solve(vector<int>({i, j ^ 1}));
return 0;
}
}
}
}
}
puts("SIMILAR");
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
struct P {
int x, y;
};
constexpr int sz = 100000 + 2;
int hl[sz];
int hr[sz];
int vu[sz];
int vd[sz];
int main(int argc, char **argv) {
int d, n, m;
cin >> d >> n >> m;
P dd[d][2];
for (auto &c : dd) {
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
c[0] = {min(x1, x2), min(y1, y2)};
c[1] = {max(x1, x2), max(y1, y2)};
}
int cl, cr, ct, cb;
cin >> cl >> cr >> ct >> cb;
for (auto &c : dd) {
if (c[0].x == c[1].x) {
hl[c[0].x + 1]++;
hr[c[0].x - 1]++;
vu[c[1].y]++;
vd[c[0].y]++;
} else {
hl[c[1].x]++;
hr[c[0].x]++;
vu[c[0].y + 1]++;
vd[c[0].y - 1]++;
}
}
for (int i = 1; i < sz; i++) {
hl[i] += hl[i - 1];
vu[i] += vu[i - 1];
}
for (int i = sz - 2; i >= 0; i--) {
hr[i] += hr[i + 1];
vd[i] += vd[i + 1];
}
int ans = 0;
for (int i = 0; i < d; i++) {
auto &c = dd[i];
if (c[0].x == c[1].x) {
if (hl[c[0].x] == cl && hr[c[0].x] == cr && vu[c[1].y] == ct + 1 &&
vd[c[0].y] == cb + 1) {
ans = i + 2;
break;
}
} else {
if (vu[c[0].y] == ct && vd[c[0].y] == cb && hl[c[1].x] == cl + 1 &&
hr[c[0].x] == cr + 1) {
ans = i + 2;
break;
}
}
}
cout << (ans - 1) << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, k, n;
cin >> n;
k = 0;
for (i = 1; i <= n; i++) {
if (i % 2) {
k++;
printf("%lld ", k);
} else {
printf("%lld ", n - k + 1);
}
}
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
char s[1000005], t[1000005], w[1000005];
namespace PalindromicAutoMaton {
int trie[1000005][26], len[1000005], fail[1000005], cnt[1000005],
chain[1000005];
int d[1000005], top[1000005];
int tot, last;
int now;
void Build() {
tot = 1;
len[0] = 0;
fail[0] = 1;
len[1] = -1;
fail[1] = 0;
last = 0;
now = 0;
}
int New(int l) {
tot++;
memset(trie[tot], 0, sizeof(trie[tot]));
len[tot] = l, fail[tot] = 0;
return tot;
}
int GetFail(int x) {
while (w[now - len[x] - 1] != w[now]) x = fail[x];
return x;
}
void Add(int ch) {
now++;
int cur = GetFail(last);
if (!trie[cur][ch]) {
int nd = New(len[cur] + 2);
fail[nd] = trie[GetFail(fail[cur])][ch];
chain[nd] = chain[fail[nd]] + 1;
d[nd] = len[nd] - len[fail[nd]];
top[nd] = (d[nd] == d[fail[nd]] ? top[fail[nd]] : fail[nd]);
trie[cur][ch] = nd;
}
cnt[trie[cur][ch]]++;
last = trie[cur][ch];
}
} // namespace PalindromicAutoMaton
using namespace PalindromicAutoMaton;
int Min[1000005], dp[1000005], hd[1000005], to[1000005];
bool Check(int l, int r) {
for (int i = l; i <= r; ++i) {
if (s[i] != t[i]) return false;
}
return true;
}
int ansL[1000005], ansR[1000005];
int main() {
scanf("%s", s + 1);
scanf("%s", t + 1);
int n = strlen(s + 1);
int cnt = 0;
for (int i = 1; i <= n; ++i) {
w[++cnt] = s[i], w[++cnt] = t[i];
}
n *= 2;
Build();
for (int i = 1; i <= n; ++i) {
dp[i] = 0x3f3f3f3f;
}
for (int i = 1; i <= n; ++i) {
Add(w[i] - 'a');
for (int p = last; p > 1; p = top[p]) {
hd[p] = i - len[top[p]] - d[p];
if (d[p] == d[fail[p]] && dp[hd[fail[p]]] < dp[hd[p]]) {
hd[p] = hd[fail[p]];
}
if (i % 2 == 0 && dp[hd[p]] + 1 < dp[i]) {
dp[i] = dp[hd[p]] + 1;
to[i] = hd[p];
}
}
if (i % 2 == 0 && w[i] == w[i - 1]) {
dp[i] = dp[i - 2];
to[i] = i - 2;
}
}
if (dp[n] > n) {
puts("-1");
return 0;
}
int ans = 0;
for (int i = n; i; i = to[i]) {
int L = to[i] / 2 + 1, R = i / 2;
if (L == R) continue;
ansL[++ans] = L, ansR[ans] = R;
}
printf("%d\n", ans);
for (int i = 1; i <= ans; ++i) {
printf("%d %d\n", ansL[i], ansR[i]);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int v[30005];
int n, t, now;
now = 0;
cin >> n >> t;
for (int i = 0; i < n - 1; i++) {
cin >> v[i];
}
for (int i = 0; i < n - 1; i++) {
now += v[now];
if (now == t - 1) {
cout << "YES";
return 0;
}
if (now >= t) {
cout << "NO";
return 0;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
for (; b; a %= b, swap(a, b))
;
return a;
}
int n, m;
vector<string> color;
vector<string> dir;
vector<int> loopSize;
vector<vector<pair<int, int> > > loops;
vector<vector<int> > loopNo;
vector<vector<int> > loopNum;
vector<vector<int> > cache;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
char op[5] = "ULDR";
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
loopSize.clear();
loops.clear();
cin >> n >> m;
color.resize(n);
dir.resize(n);
for (int i = 0; i < n; i++) cin >> color[i];
for (int i = 0; i < n; i++) cin >> dir[i];
cache.resize(n);
for (int i = 0; i < n; i++) cache[i] = vector<int>(m, 0);
loopNum.resize(n);
for (int i = 0; i < n; i++) loopNum[i] = vector<int>(m, -1);
loopNo.resize(n);
for (int i = 0; i < n; i++) loopNo[i] = vector<int>(m, -1);
int all = 0;
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (cache[i][j]) continue;
vector<pair<int, int> > stk;
int x = i, y = j;
while (true) {
int cdir = -1;
if (dir[x][y] == 'D')
cdir = 0;
else if (dir[x][y] == 'R')
cdir = 1;
else if (dir[x][y] == 'U')
cdir = 2;
else if (dir[x][y] == 'L')
cdir = 3;
cache[x][y] = 1;
stk.push_back({x, y});
int nx = x + dx[cdir];
int ny = y + dy[cdir];
if (cache[nx][ny]) {
if (loopNum[nx][ny] == -1) {
int cloopNo = loops.size();
vector<pair<int, int> > loop;
loop.push_back({nx, ny});
int loop_cnt = 0;
loopNum[nx][ny] = loop_cnt++;
loopNo[nx][ny] = cloopNo;
while (!stk.empty()) {
int cx = stk.back().first;
int cy = stk.back().second;
stk.pop_back();
if (cx == nx && cy == ny) break;
loopNum[cx][cy] = loop_cnt++;
loopNo[cx][cy] = cloopNo;
loop.push_back({cx, cy});
}
loopSize.push_back(loop_cnt);
int curLoopNum = 0;
while (!stk.empty()) {
int cx = stk.back().first;
int cy = stk.back().second;
stk.pop_back();
curLoopNum = (curLoopNum + 1) % loop_cnt;
loopNum[cx][cy] = curLoopNum;
loopNo[cx][cy] = cloopNo;
loop.push_back({cx, cy});
}
loops.push_back(loop);
} else {
int cloopNo = loopNo[nx][ny];
int loop_cnt = loopSize[cloopNo];
int curLoopNum = loopNum[nx][ny];
while (!stk.empty()) {
int cx = stk.back().first;
int cy = stk.back().second;
stk.pop_back();
curLoopNum = (curLoopNum + 1) % loop_cnt;
loopNum[cx][cy] = curLoopNum;
loopNo[cx][cy] = cloopNo;
loops[cloopNo].push_back({cx, cy});
}
}
break;
} else {
x = nx;
y = ny;
}
}
}
for (int i = 0; i < loops.size(); i++) {
int loop_cnt = loopSize[i];
vector<int> cnt(loop_cnt);
for (pair<int, int> it : loops[i]) {
int x = it.first;
int y = it.second;
int cloopNum = loopNum[x][y];
if (color[x][y] == '0') cnt[cloopNum]++;
}
sort(cnt.rbegin(), cnt.rend());
all += loopSize[i];
for (auto it : cnt)
if (it > 0) ans++;
}
cout << all << ' ' << ans << '\n';
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N];
long long p[N], sum[N];
double go(int mid, int k) {
double ret = sum[n] - sum[n - k];
ret += sum[mid] - sum[mid - k - 1];
ret /= (2 * k + 1);
ret -= a[mid];
return ret;
}
int main() {
int i, j, k;
cin >> n;
for (i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + 1 + n);
sum[0] = 0;
int l = 1, r = 0;
double ret = -10;
for (i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
for (i = 2; i < n; ++i) {
k = 0;
for (j = 20; j >= 0; --j) {
k += 1 << j;
if (i > n - k || i - k - 1 < 0 || go(i, k) < go(i, k - 1)) k -= 1 << j;
}
if (ret < go(i, k)) {
ret = go(i, k);
l = i;
r = k;
}
}
printf("%d\n", 2 * r + 1);
for (i = l - r; i <= l; ++i) printf("%d ", a[i]);
for (i = n - r + 1; i <= n; ++i) printf("%d ", a[i]);
cout << endl;
}
| 8 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int n, m;
int p[64], a[64][2], x[64];
long long dp[64][64][64][2];
void kmp(string &cad) {
p[0] = 0;
for (int i = 1; i < m; ++i) {
p[i] = p[i - 1];
while (p[i] > 0 and cad[p[i]] != cad[i]) p[i] = p[p[i] - 1];
if (cad[p[i]] == cad[i]) p[i]++;
}
for (int i = m - 1; i >= 0; --i) p[i + 1] = p[i];
}
long long solve(int k, int i, int j, int has) {
long long &ans = dp[k][i][j][has];
if (ans != -1) return ans;
if (i == n) return ans = ((k == j) and has);
ans = 0;
for (int t = int(0); t < int(2); ++t)
ans += solve(k, i + 1, a[j][t], has || (a[j][t] == m));
return ans;
}
int main() {
string cad;
cin >> n >> cad;
m = int(cad.size());
for (int i = 0; i < m; ++i) x[i] = cad[i] - '0';
kmp(cad);
a[0][x[0]] = 1;
a[0][x[0] ^ 1] = 0;
for (int i = 1; i < m; ++i) {
a[i][x[i]] = i + 1;
a[i][x[i] ^ 1] = a[p[i]][x[i] ^ 1];
}
a[m][0] = a[p[m]][0];
a[m][1] = a[p[m]][1];
memset(dp, -1, sizeof(dp));
long long ans = 0;
for (int i = int(0); i < int(m + 1); ++i) ans += solve(i, 0, i, 0);
cout << ans << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int c[1000007], n, m, ans[300005], maxi;
struct node {
int l, r, ind;
} a[1000007];
int lowbit(int x) { return x & -x; }
void add(int x) {
while (x <= maxi) {
c[x]++;
x += lowbit(x);
}
}
int sum(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= lowbit(x);
}
return s;
}
int cmp(node a, node b) {
if (a.l == b.l) {
if (a.r == b.r) return a.ind < b.ind;
return a.r < b.r;
}
return a.l > b.l;
}
int main() {
int i, j, k, x, pre, cnt;
maxi = 1000007 - 5;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r), a[i].ind = 0;
int tot = n;
for (i = 1; i <= m; i++) {
scanf("%d%d", &cnt, &pre);
if (x > 1) a[++tot].l = 1, a[tot].r = pre - 1, a[tot].ind = i;
for (j = 1; j < cnt; j++) {
scanf("%d", &x);
if (x - 1 >= pre + 1)
a[++tot].l = pre + 1, a[tot].r = x - 1, a[tot].ind = i;
pre = x;
}
a[++tot].l = pre + 1, a[tot].r = maxi, a[tot].ind = i;
}
sort(a + 1, a + tot + 1, cmp);
for (i = 1; i <= tot; i++)
if (a[i].ind > 0)
ans[a[i].ind] += sum(a[i].r);
else
add(a[i].r);
for (i = 1; i <= m; i++) printf("%d\n", n - ans[i]);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, ans, cnt[205], c[205], tmp[205], fa[205];
vector<int> con[205];
queue<int> q;
bool vis[205];
void init() {
int x, y;
memset(cnt, 0, sizeof(cnt));
memset(fa, -1, sizeof(fa));
for (int i = 1; i <= n; i++) {
con[i].clear();
scanf("%d", &c[i]);
c[i]--;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
while (x--) {
scanf("%d", &y);
con[y].push_back(i);
cnt[i]++;
}
}
}
int test(int cur) {
int remain = n, re = 0, x;
memset(vis, false, sizeof(vis));
memcpy(tmp, cnt, sizeof(cnt));
while (remain > 0) {
for (int i = 1; i <= n; i++)
if (!vis[i] && !tmp[i] && c[i] == cur) {
vis[i] = true;
q.push(i);
while (!q.empty()) {
x = q.front();
q.pop();
remain--;
vis[x] = true;
for (int j = 0; j <= (int)con[x].size() - 1; j++)
if (!vis[con[x][j]]) {
tmp[con[x][j]]--;
if (!tmp[con[x][j]] && c[con[x][j]] == cur) {
vis[con[x][j]] = true;
q.push(con[x][j]);
}
}
}
}
if (remain == 0) break;
cur = (cur + 1) % 3;
re++;
}
return re;
}
int main() {
while (~scanf("%d", &n)) {
init();
ans = 205 * 205;
for (int i = 0; i <= 2; i++) ans = min(ans, n + test(i));
printf("%d\n", ans);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, k = 0, n, m, x1, y1, x2, y2, x, y;
long long ans;
scanf("%d", &t);
while (k < t) {
scanf("%d%d%d%d%d%d", &m, &n, &x1, &y1, &x2, &y2);
x = x1 > x2 ? x1 - x2 : x2 - x1;
y = y1 > y2 ? y1 - y2 : y2 - y1;
if (m >= 2 * x) {
if (n >= 2 * y)
ans = (long long)2 * x * y;
else
ans = (long long)(2 * y - n) * m + (long long)2 * x * (n - y);
} else {
ans = (long long)(2 * x - m) * n + (long long)2 * y * (m - x);
}
cout << ans << endl;
k++;
}
return 0;
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.