solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int maxn = 3e5 + 10; int n; vector<int> g[maxn]; long long fpow(long long a, long long b) { long long r = 1; while (b) { if (b & 1) r = r * a % mod; b >>= 1; a = a * a % mod; } return r; } long long dp[maxn][2][2], res[maxn], tp[maxn][2][2], sum[maxn], ans[maxn]; void dfs(int u, int fa) { res[u] = 1; dp[u][0][0] = dp[u][0][1] = 1; sum[u] = 0; for (auto it : g[u]) { if (it == fa) continue; dfs(it, u); res[u] = (res[u] * (dp[it][1][0] + dp[it][1][1] + res[it]) % mod + mod) % mod; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) tp[u][i][j] = dp[u][i][j]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (i == 0) { tp[u][i][j] = (dp[u][i][j] * (dp[it][1][0] + dp[it][1][1] + res[it]) % mod) % mod; } else { if (j == 0) { tp[u][i][j] = (dp[u][i][j] * (sum[it] + (dp[it][1][0] + dp[it][1][1] + res[it])) % mod + dp[u][0][j] * sum[it] % mod) % mod; } else { tp[u][i][j] = (dp[u][i][j] * (dp[it][0][0] + dp[it][1][0] + dp[it][1][0] + dp[it][1][1] + res[it]) % mod + dp[u][0][j] * (dp[it][0][0] + dp[it][1][0]) % mod) % mod; } } } } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) dp[u][i][j] = tp[u][i][j]; } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) sum[u] = (sum[u] + dp[u][i][j]) % mod; } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); printf("%lld\n", (dp[1][1][0] + dp[1][1][1] + res[1] - 1 + mod) % mod); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxp = 22; const long double EPS = 1e-18; const long long INF = 1e18; const int MOD = 1e9 + 7; const int N = 1500; vector<int> compress(vector<int> a) { vector<int> b = a; sort((b).begin(), (b).end()); b.erase(unique((b).begin(), (b).end()), b.end()); for (auto &x : a) x = int(lower_bound((b).begin(), (b).end(), x) - b.begin()); return a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int i, arr[n]; for (i = 0; i < n; i++) cin >> arr[i]; vector<int> sums(n * (n + 1) / 2); int j, pos = 0; for (i = 0; i < n; i++) { int sum = 0; for (j = i; j < n; j++) { sum += arr[j]; sums[pos++] = sum; } } sums = compress(sums); vector<vector<pair<int, int> > > cnt(n * (n + 1) / 2); pos = 0; for (i = 0; i < n; i++) for (j = i; j < n; j++) cnt[sums[pos++]].push_back({i, j}); pos = 0; vector<pair<int, int> > ans; for (auto &x : cnt) { if (x.empty()) continue; vector<pair<int, int> > curr; for (auto y : x) { if (curr.empty() || curr.back().second < y.first) curr.push_back(y); else { if (curr.back().second > y.second) { curr.pop_back(); curr.push_back(y); } } } if (curr.size() > ans.size()) ans = curr; } cout << ans.size() << '\n'; for (auto x : ans) cout << x.first + 1 << " " << x.second + 1 << '\n'; }
5
#include <bits/stdc++.h> using namespace std; const double Pi = acos(-1); vector<pair<int, int> > seg0; vector<bool> good; vector<int> a; int n, m; void Solve() { for (pair<int, int> lr : seg0) { int l = lr.first, r = lr.second; bool ok = false; for (int i = l; i < r; i++) if (a[i] > a[i + 1]) { ok = true; break; } if (!ok) { cout << "NO"; return; } } cout << "YES\n"; int Min = *min_element(a.begin(), a.end()); for (int i = 1; i <= n; i++) cout << a[i] - Min + 1 << ' '; } int main() { cin.tie(0); ios::sync_with_stdio(0); cout.tie(0); cin >> n >> m; good.resize(n + 1); a.resize(n + 1); for (int i = 0; i < m; i++) { int t, l, r; cin >> t >> l >> r; if (!t) seg0.push_back(pair<int, int>(l, r)); else for (int j = l + 1; j <= r; j++) good[j] = 1; } a[1] = 0; for (int i = 2; i <= n; i++) if (good[i]) a[i] = a[i - 1] + 1; else a[i] = a[i - 1] - 1; Solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; void solve() { string inp; int w_sum = 0, b_sum = 0; for (int i = 0; i < 8; i++) { cin >> inp; for (int b = 0; b < 8; b++) { if (inp[b] == 'q') { b_sum += 9; } else if (inp[b] == 'r') { b_sum += 5; } else if (inp[b] == 'b') { b_sum += 3; } else if (inp[b] == 'n') { b_sum += 3; } else if (inp[b] == 'p') { b_sum += 1; } else if (inp[b] == 'Q') { w_sum += 9; } else if (inp[b] == 'R') { w_sum += 5; } else if (inp[b] == 'B') { w_sum += 3; } else if (inp[b] == 'N') { w_sum += 3; } else if (inp[b] == 'P') { w_sum += 1; } } } if (w_sum > b_sum) { cout << "White"; } else if (w_sum < b_sum) { cout << "Black"; } else { cout << "Draw"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; int mul(int a, int b) { long long pa = a; long long pb = b; return (int)((pa * pb) % MOD); } int powMOD(int x, int n) { int r = 1; while (n) { if (n & 0x01) r = mul(r, x); x = mul(x, x); n /= 2; } return r; } int main() { int n, m, k; cin >> n >> m >> k; if (k == 1 || k > n) { cout << powMOD(m, n) << endl; } else if (k == n) { cout << powMOD(m, (n + 1) / 2) << endl; } else if (k & 0x01) { cout << (m * m) << endl; } else { cout << (m) << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e9; int gcd(int a, int b) { while (a > 0 && b > 0) if (a > b) a %= b; else b %= a; return a + b; } int lcm(int a, int b) { return a * b / gcd(a, b); } int n, i; int main() { scanf("%d", &n); if (n % 2) { for (i = 0; i <= n - 1; i++) printf("%d ", i); printf("\n"); for (i = 0; i <= n - 1; i++) printf("%d ", i); printf("\n"); for (i = 0; i <= n - 1; i++) printf("%d ", i * 2 % n); printf("\n"); } else printf("-1\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; cout << (n * (n + 1)) * 3 + 1; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, d; float t, k; cin >> n >> t >> k >> d; float oneCake = t / k; int nc = n; float x1 = 0; while (n > 0) { x1 += t; n -= k; } float x2 = 0; nc -= ((float)d / (float)oneCake); x2 += d; if (nc <= 0) { cout << "NO"; return 0; } while (nc > 0) { x2 += t; nc -= (k * 2); } x2 < x1 ? cout << "YES" : cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:100000000") int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { int a, b, n; scanf("%d%d%d", &a, &b, &n); while (1) { int g = gcd(a, n); if (g > n) { printf("1\n"); return 0; } n -= g; g = gcd(b, n); if (g > n) { printf("0\n"); return 0; } n -= g; } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> inline void checkMin(T& a, T b) { if (a > b) a = b; } template <typename T> inline void checkMax(T& a, T b) { if (a < b) a = b; } const int MAXN = (int)(1E7 + 7); vector<int> p; int lp[MAXN]; long long cp[MAXN]; int a[MAXN]; inline bool check(long long n) { for (auto i : p) { if (i > n) { return true; } long long c = 0; for (long long x = n / i; x > 0; x /= i) { c += x; } if (c < cp[i]) { return false; } } return true; } int main() { for (int i = 2; i < MAXN; ++i) { if (lp[i] == 0) { p.push_back(i); for (int j = i; j < MAXN; j += i) { lp[j] = i; } } } int k, x, y = 0; long long l, r = 0; cin >> k; for (int i = 0; i < int(k); ++i) { cin >> x; ++a[x]; checkMax(y, x); r += x; } l = y; for (k = 0; y >= 2; --y) { for (x = y, k += a[y]; x > 1; x /= lp[x]) { cp[lp[x]] += k; } } while (l < r) { if (check((l + r) / 2)) { r = (l + r) / 2; } else { l = (l + r) / 2 + 1; } } cout << l << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main(void) { int a, b; cin >> a >> b; int dif; int same; if (a == b) { dif = a; same = 0; } else if (a > b) { same = a - b; dif = a - same; } else { same = b - a; dif = b - same; } if (same % 2 != 0) { same = (same - 1) / 2; } else same = same / 2; cout << dif << " " << same << endl; }
0
#include <bits/stdc++.h> using namespace std; long double xa[104] = {0}; long double ya[104] = {0}; long double za[104] = {0}; long long int n; long double sum(long double x, long double y, long double z) { return x * x + y * y + z * z; } int main() { cin >> n; for (long long int i = 1; i <= n; i++) { cin >> xa[i] >> ya[i] >> za[i]; xa[0] += xa[i]; ya[0] += ya[i]; za[0] += za[i]; } long double X = xa[0] / n; long double Y = xa[0] / n; long double Z = xa[0] / n; long double p = 0.1; for (long long int tt = 1; tt <= 30000; tt++) { long long int k = 1; long double ans = sum(xa[1] - X, ya[1] - Y, za[1] - Z); for (int i = 2; i <= n; i++) { long double e = sum(xa[i] - X, ya[i] - Y, za[i] - Z); if (ans < e) { k = i; ans = e; } } X += (xa[k] - X) * p; Y += (ya[k] - Y) * p; Z += (za[k] - Z) * p; p *= .999; } cout << fixed << setprecision(7) << X << " " << Y << " " << Z << endl; }
6
#include <bits/stdc++.h> using namespace std; const int N = 2000010; long long n; vector<long long> Adj[N]; bool Vis[N]; bool Lucky(long long x) { while (x > 0) { int d = (x % 10); x /= 10; if (d != 7 && d != 4) return 0; } return 1; } int Cnt; void Dfs(int x) { Vis[x] = true; Cnt++; for (int i = 0; i < Adj[x].size(); i++) { int to = Adj[x][i]; if (Vis[to]) continue; Dfs(to); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { long long a, b, w; cin >> a >> b >> w; if (Lucky(w)) continue; Adj[a].push_back(b); Adj[b].push_back(a); } vector<int> Components; for (int i = 1; i <= n; i++) { if (Vis[i]) continue; Cnt = 0; Dfs(i); Components.push_back(Cnt); } long long Ans = 0; for (int i = 0; i < Components.size(); i++) { long long Cur = Components[i]; long long Others = (n - Cur); Ans += Cur * (Others * (Others - 1)); } cout << Ans << endl; return 0; }
5
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; using ll = long long; using vi = vector<int>; using pi = pair<int, int>; using vpi = vector<pair<int, int>>; using pl = pair<ll, ll>; using vl = vector<ll>; using vpl = vector<pl>; template <typename T> using pqg = priority_queue<T, vector<T>, greater<T>>; template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template <typename T_container, typename T = typename enable_if< !is_same<T_container, string>::value, typename T_container::value_type>::type> ostream &operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int INF = 1e9; const ll LINF = 1e18; const int MOD = 1e9 + 7; const int N = 2e5 + 10; int cnt[N]; bool vis[N], meh[N]; void solve() { int n; cin >> n; vi a(n); for (int i = 0; i < n; ++i) cin >> a[i], cnt[a[i]]++; int mi = 0, ma = 0; for (int i = 1; i <= n; ++i) { int k = cnt[i]; if (!k) continue; if (!(meh[i]) && !meh[i - 1]) { meh[i + 1] = 1; } if (k >= 3) { vis[i - 1] = 1; vis[i] = 1; vis[i + 1] = 1; } else { if (k == 1) { if (vis[i - 1] && vis[i]) { vis[i + 1] = 1; } else if (vis[i - 1]) { vis[i] = 1; } else { vis[i - 1] = 1; } } else { if (vis[i - 1]) { vis[i] = 1; vis[i + 1] = 1; } else { vis[i - 1] = 1; vis[i] = 1; } } } } for (int i = 0; i <= n + 1; ++i) { if (vis[i]) ma++; if (meh[i]) mi++; } cout << mi << " " << ma << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); }
5
#include <bits/stdc++.h> using namespace std; pair<int, int> v[1010]; int n, t, cf; long long sol, futute, p[1010]; int main() { cin >> n; for (long long i = 1; i <= n; ++i) cin >> v[i].second >> v[i].first; sort(v + 1, v + n + 1); cin >> t; for (long long i = 1; i <= t; ++i) cin >> p[i]; p[t + 1] = 1LL << 60; cf = 1; for (long long i = 1; i <= n; ++i) { while (v[i].second) { if (v[i].second <= p[cf] - futute) { futute += v[i].second; sol += 1LL * v[i].second * cf * v[i].first; v[i].second = 0; } else if (v[i].second > p[cf] - futute) { sol += 1LL * (p[cf] - futute) * cf * v[i].first; v[i].second -= p[cf] - futute; futute = p[cf]; ++cf; } } } cout << sol; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = (1uLL << 63) - 1; const long long mod = 1000000007; const int N = 150 + 5; const double Pi = acos(-1.0); const int maxn = 1e6 + 5; int E[21][21]; int d[21]; int sum = 0; bool vis[21]; int cnt = 0; int num[21]; int dfs(int cur) { if (cur == cnt) return 0; if (vis[cur]) { return dfs(cur + 1); } vis[cur] = 1; int res = inf; for (int i = 0; i < cnt; i++) { if (!vis[i]) { vis[i] = 1; res = min(res, E[num[cur]][num[i]] + dfs(cur + 1)); vis[i] = 0; } } vis[cur] = 0; return res; } int dp[1 << 16]; int main() { ios::sync_with_stdio(false); cin.tie(0); memset(E, 0x3f, sizeof E); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; sum += w; E[u][v] = E[v][u] = min(E[u][v], w); d[u]++, d[v]++; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { E[i][j] = min(E[i][j], E[i][k] + E[k][j]); } } } if (m == 0) { cout << 0 << endl; return 0; } for (int i = 2; i <= n; i++) { if (E[1][i] == E[0][0] && d[i]) { cout << -1 << endl; return 0; } } int sta = 0; for (int i = 1; i <= n; i++) { if (d[i] % 2 == 0) sta += (1 << (i - 1)); } memset(dp, 0x3f, sizeof dp); dp[sta] = 0; for (int i = 0; i < (1 << n); i++) { if ((i & sta) == sta) { for (int j = 0; j < n; j++) { if (!((i >> j) & 1)) for (int z = j + 1; z < n; z++) { if (!((i >> z) & 1)) { dp[i | (1 << j) | (1 << z)] = min(dp[i | (1 << j) | (1 << z)], dp[i] + E[j + 1][z + 1]); } } } } } cout << dp[(1 << n) - 1] + sum << endl; }
8
#include <bits/stdc++.h> using namespace std; template <class T> T cross(complex<T> a, complex<T> b) { return imag(a * conj(b)); } template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } template <class T> void setmin(T &a, T b) { if (b < a) a = b; } template <class T> void setmax(T &a, T b) { if (b > a) a = b; } typedef pair<int, bool> sint; vector<string> ans; int N; pair<sint, sint> pts[200000]; int numOp() { int ans = 0; for (int i = 0; i < N; i++) { ans += 2 * bool(pts[i].first.first); ans += 2 * bool(pts[i].second.first); ans += 2; } return ans; } void io(istream &in, ostream &out) { in >> N; for (int i = 0; i < N; i++) { in >> pts[i].first.first >> pts[i].second.first; if (pts[i].first.first < 0) { pts[i].first.first *= -1; pts[i].first.second = false; } else { pts[i].first.second = true; } if (pts[i].second.first < 0) { pts[i].second.first *= -1; pts[i].second.second = false; } else { pts[i].second.second = true; } } sort(pts, pts + N); out << numOp() << '\n'; for (int i = 0; i < N; i++) { if (pts[i].second.first) { out << 1 << ' '; out << pts[i].second.first << ' '; out << (pts[i].second.second ? 'U' : 'D') << '\n'; } if (pts[i].first.first) { out << 1 << ' '; out << pts[i].first.first << ' '; out << (pts[i].first.second ? 'R' : 'L') << '\n'; } out << 2 << '\n'; if (pts[i].first.first) { out << 1 << ' '; out << pts[i].first.first << ' '; out << (pts[i].first.second ? 'L' : 'R') << '\n'; } if (pts[i].second.first) { out << 1 << ' '; out << pts[i].second.first << ' '; out << (pts[i].second.second ? 'D' : 'U') << '\n'; } out << 3 << '\n'; } } int main() { ifstream fin("350C.in"); if (fin.good()) { ofstream fout("350C.out"); io(fin, fout); fin.close(); fout.close(); } else io(cin, cout); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long gcd(long long n1, long long n2) { if (n2 == 0) return n1; if (n1 % n2 == 0) return n2; return gcd(n2, n1 % n2); } long long powmod(long long base, long long exponent) { if (exponent < 0) exponent += 1000000007LL - 1; long long ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % 1000000007LL; base = (base * base) % 1000000007LL; exponent /= 2; } return ans; } long long arr[1000005], pichle = 0, flip[1000005], drastic[1000005]; set<pair<long long, long long> > s; int main() { long long n; cin >> n; long long now = 0; for (long long i = 1; i <= n; i++) { cin >> arr[i]; if (i <= arr[i]) { pichle--; flip[arr[i] - i]++; drastic[n - i] = -(n - arr[i]) + (arr[i] - 1) + 1; flip[(n - i)]--; } else { pichle++; flip[n - i + arr[i]]++; drastic[n - i] = -(n - arr[i]) + (arr[i] - 1) + 1; flip[(n - i)]--; } now += abs(i - arr[i]); } s.insert(make_pair(now, 0)); pichle += 2 * flip[0]; for (long long k = 1; k < n; k++) { now += pichle + drastic[k - 1]; pichle += 2 * flip[k]; s.insert(make_pair(now, k)); } cout << s.begin()->first << " " << s.begin()->second << '\n'; }
5
#include <bits/stdc++.h> using namespace std; const int _ = 2003, MOD = 998244353; int stir[_][_], fac[_], ifac[_], N, P[_], Q[_], s[_], t[_], cnt[3], num, nxt[_], f[_], g[_], h[_]; bool vis[_]; int poww(long long a, int b) { int tms = 1; while (b) { if (b & 1) tms = tms * a % MOD; a = a * a % MOD; b >>= 1; } return tms; } int binom(int a, int b) { return 1ll * fac[a] * ifac[b] % MOD * ifac[a - b] % MOD; } void search(int x) { int t = x; do t = nxt[t]; while (t != 0 && t != x); if (t == x) { ++num; do vis[t = nxt[t]] = 1; while (t != x); } } void init() { stir[0][0] = 1; for (int i = 1; i <= N; ++i) for (int j = 1; j <= i; ++j) stir[i][j] = (stir[i - 1][j - 1] + (i - 1ll) * stir[i - 1][j]) % MOD; fac[0] = 1; for (int i = 1; i <= N; ++i) fac[i] = 1ll * fac[i - 1] * i % MOD; ifac[N] = poww(fac[N], MOD - 2); for (int i = N - 1; ~i; --i) ifac[i] = ifac[i + 1] * (i + 1ll) % MOD; } void calc(int num, int *f) { for (int i = 0; i <= num; ++i) for (int j = i; j <= num; ++j) f[i] = (f[i] + 1ll * binom(num, j) * stir[j][i] % MOD * fac[cnt[2] + num - j] % MOD * ifac[cnt[2]]) % MOD; for (int i = num; ~i; --i) for (int j = i + 1; j <= num; ++j) f[i] = (f[i] - 1ll * f[j] * binom(j, i) % MOD + MOD) % MOD; } int main() { cin >> N; for (int i = 1; i <= N; ++i) { cin >> P[i]; if (P[i]) s[P[i]] = i; } for (int i = 1; i <= N; ++i) { cin >> Q[i]; if (Q[i]) t[Q[i]] = i; } for (int i = 1; i <= N; ++i) if (P[i] && Q[i]) nxt[P[i]] = Q[i]; else ++cnt[P[i] ? 0 : (Q[i] ? 1 : 2)]; for (int i = 1; i <= N; ++i) if (!vis[i]) search(i); for (int i = 1; i <= N; ++i) if (s[i] && t[i] && !P[t[i]]) { int p = i; while (nxt[p]) p = nxt[p]; if (s[p]) { --cnt[0]; --cnt[1]; ++cnt[2]; } } init(); calc(cnt[0], f); calc(cnt[1], g); for (int i = 0; i <= cnt[0]; ++i) for (int j = 0; j <= cnt[1]; ++j) h[i + j] = (h[i + j] + 1ll * f[i] * g[j]) % MOD; memset(g, 0, sizeof(g)); memset(f, 0, sizeof(f)); for (int i = 0; i <= cnt[2]; ++i) f[i] = stir[cnt[2]][i]; for (int i = 0; i <= cnt[0] + cnt[1]; ++i) for (int j = 0; j <= cnt[2]; ++j) g[i + j] = (g[i + j] + 1ll * h[i] * f[j]) % MOD; for (int i = cnt[0] + cnt[1] + cnt[2] + num; i >= num; --i) { g[i] = 1ll * g[i - num] * fac[cnt[2]] % MOD; if (num) g[i - num] = 0; } for (int i = N; i; --i) printf("%d ", g[i]); return 0; }
13
#include <bits/stdc++.h> using namespace std; using namespace chrono; void read(vector<int> &a) { for (auto &x : a) cin >> x; } void read(vector<int64_t> &a) { for (auto &x : a) cin >> x; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug &operator<<(const c &) { return *this; } }; const int MOD = 1e9 + 7; const int INF = (int)2e9 + 7; const int64_t LINF = (int64_t)1e18; const long double PI = 3.1415926535897932384626433832795; void solve() { int64_t n; cin >> n; int64_t s = 0, num = 0, nine = 9; while (1) { if (s + (num + 1) * nine >= n) break; s += (num + 1) * nine; num++; nine *= 10; } int64_t extra = n - s - 1; int64_t start = pow(10ll, num); num++; debug() << " [" << "s" ": " << (s) << "] " " [" << "num" ": " << (num) << "] " " [" << "start" ": " << (start) << "] " " [" << "extra" ": " << (extra) << "] "; start += extra / num; string str = to_string(start); cout << str[extra % num]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; auto t1 = high_resolution_clock::now(); for (int tt = 1; tt <= t; tt++) { solve(); } auto t2 = high_resolution_clock::now(); auto time = duration_cast<duration<double>>(t2 - t1); cerr << "Time elapsed = " << time.count() << "\n"; }
5
#include <bits/stdc++.h> using namespace std; bitset<100014> ck[45]; vector<int> v; string name[50]; int n, m, cnt, bcnt, bf, best, p[54]; bool c[50][50]; vector<pair<int, int> > sorted; void bfs(int h) { bool nt = 0; if (h > cnt) { best = max(best, (int)v.size()); return; } for (int i = 0; i < v.size(); i++) if (!c[v[i]][h]) nt = 1; if (!nt) { v.emplace_back(h); bfs(h + 1); v.pop_back(); } if (v.size() + cnt - h > best) bfs(h + 1); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { int a; cin >> a; if (a == 1) { if (bf == 2) bcnt++; bf = 1; } else { string b; cin >> b; int fd = 0; for (int i = 0; i <= cnt; i++) if (name[i] == b) fd = i; if (!fd) cnt++, name[cnt] = b, fd = cnt; ck[fd][bcnt] = 1; bf = 2; } } for (int i = 1; i <= cnt; i++) for (int j = i; j <= cnt; j++) if ((ck[i] & ck[j]).count() == 0) c[i][j] = c[j][i] = 1; bfs(1); cout << best; return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<vector<int> > G(1000, vector<int>(1000, 0)); void set4(int i, int j) { G[i][j] = 1; G[i + 1][j] = 1; G[i - 1][j] = 1; G[i][j + 1] = 1; G[i][j - 1] = 1; } void solve() { int N; cin >> N; vector<pair<int, int> > ans; if (N == 1) { set4(1, 1); G[0][0] = G[2][2] = 1; } else if (N == 2) { set4(2, 1); set4(3, 1); G[5][1] = G[5][2] = G[5][3] = G[5][4] = G[4][4] = G[3][4] = G[2][4] = G[1][4] = G[0][4] = G[0][3] = G[0][2] = G[0][1] = 1; } else if (N % 3 == 1) { int count = 1; set4(1, 1); int i = 2; while (count != N) { set4(i, i); set4(i - 1, i); set4(i, i - 1); count += 3; i++; } } else if (N % 3 == 2) { int count = 2; set4(2, 3); set4(3, 2); G[3][1] = G[3][0] = G[2][0] = G[1][0] = G[0][0] = G[0][1] = G[0][2] = G[0][3] = 1; int i = 3; while (count != N) { set4(i, i); set4(i + 1, i); set4(i, i + 1); count += 3; i++; } G[i + 2][i - 1] = G[i + 2][i] = G[i + 2][i + 1] = G[i + 2][i + 2] = G[i + 1][i + 2] = G[i][i + 2] = G[i - 1][i + 2] = 1; } else if (N % 3 == 0) { int count = 3; set4(1, 1); set4(1, 2); set4(2, 1); int i = 2; while (count != N) { set4(i, i); set4(i + 1, i); set4(i, i + 1); count += 3; i++; } G[i + 2][i - 1] = G[i + 2][i] = G[i + 2][i + 1] = G[i + 2][i + 2] = G[i + 1][i + 2] = G[i][i + 2] = G[i - 1][i + 2] = 1; } for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { if (G[i][j]) ans.push_back({i, j}); } } cout << ans.size() << "\n"; ; for (pair<int, int> &p : ans) cout << p.first << " " << p.second << "\n"; return; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); solve(); }
3
#include <bits/stdc++.h> using namespace std; int n, m; int ans, cnt[10], sum; int check() { int s; if (cnt[1] < cnt[2]) { ans += cnt[1]; cnt[0] += cnt[1]; cnt[3] += cnt[1]; cnt[2] -= cnt[1]; cnt[1] = 0; s = cnt[2] / 3; ans += s * 2; cnt[3] += s * 2; cnt[0] += s; cnt[2] -= s * 3; } else { ans += cnt[2]; cnt[0] += cnt[2]; cnt[3] += cnt[2]; cnt[1] -= cnt[2]; cnt[2] = 0; s = cnt[1] / 3; ans += s * 2; cnt[0] += s * 2; cnt[3] += s; cnt[1] -= s * 3; } if (cnt[1] && cnt[4]) { s = min(cnt[1] / 2, cnt[4]); ans += 2 * s; cnt[3] += s * 2; cnt[4] -= s; cnt[1] -= 2 * s; cnt[0] += s; } if (cnt[1] && cnt[3]) { s = min(cnt[1], cnt[3]); ans += s; cnt[4] += s; cnt[3] -= s; cnt[1] -= s; cnt[0] += s; } if (cnt[1] && cnt[4]) { s = min(cnt[1], cnt[4] / 2); ans += 2 * s; cnt[3] += s * 3; cnt[4] -= s * 2; cnt[1] -= s; } if (cnt[2]) { s = cnt[2] / 2; ans += 2 * s; cnt[0] += s; cnt[2] -= 2 * s; cnt[4] += s; } if (cnt[2] && cnt[4]) { s = min(cnt[2], cnt[4]); ans += s; cnt[4] -= s; cnt[3] += 2 * s; cnt[2] -= s; } if (cnt[2] && cnt[3]) { s = min(cnt[2], cnt[3] / 2); ans += 2 * s; cnt[0] += s; cnt[2] -= s; cnt[4] += 2 * s; cnt[3] -= 2 * s; } if (cnt[1] || cnt[2]) return -1; return ans; } int main() { int i, j, l, r, x; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", &x); ++cnt[x]; sum += x; } printf("%d\n", check()); return 0; }
6
#include <bits/stdc++.h> using namespace std; int a[1000001], ar[1000001]; pair<int, int> b[1000001]; int n, m, k, i, j; vector<int> victor; bool check(vector<int> &v, int t) { for (int i = 0; i < (t - 1) / k + 1; i++) { if (v[k * i] < i) return false; } return true; } vector<int> kefir; bool check(int t) { merge(a, a + n, ar + m - t, ar + m, kefir.begin()); return check(kefir, n + t); } int binary_search() { int l = 0, r = m, c, ans = -1; while (l <= r) { c = (l + r) / 2; if (check(c)) { ans = c; l = c + 1; } else r = c - 1; } return ans; } int main() { cin >> n >> m >> k; kefir.resize(n + m); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < m; i++) { scanf("%d", &b[i].first); b[i].second = i; } sort(a, a + n); sort(b, b + m); for (int i = 0; i < m; i++) { ar[i] = b[i].first; } int LA = binary_search(); cout << LA << "\n"; for (int i = m - LA; i < m; i++) { printf("%d%c", b[i].second + 1, ' '); } return 0; }
6
#include <bits/stdc++.h> using namespace std; bool prime[1000005]; int v[1000005]; void sieve() { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= 1000005; p++) { if (prime[p]) { for (int i = p * p; i <= 1000005; i += p) prime[i] = false; } } v[1] = 1; int x = 1; for (int i = 2; i <= 1000005; i++) { if (prime[i]) v[i] += (v[i - 1] + 1), x++; else v[i] = x; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int t; sieve(); cin >> t; while (t--) { int n, y; cin >> n; cout << v[n] - v[(int)sqrt(n)] + 1 << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int iinf = 1e9 + 7; const long long linf = 1ll << 60; const double dinf = 1e10; void scf(int &x) { bool f = 0; x = 0; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') { f = 1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } if (f) x = -x; return; } void scf(int &x, int &y) { scf(x); return scf(y); } void scf(int &x, int &y, int &z) { scf(x); scf(y); return scf(z); } const int N = 555; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int n, k, tot, ans, cur; char cmap[N][N]; vector<int> g[N * N]; int cnt[N * N], mns[N * N], on[N * N]; int col[N * N]; int to[N][N]; inline int getid(int i, int j) { return i * (n + 2) + j; } inline bool legal(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n && cmap[x][y] == '.'; } inline void dfs(int u) { col[u] = tot; for (int i = 0; i < (int)g[u].size(); i++) if (!col[g[u][i]]) dfs(g[u][i]); } inline void pre_calc(int x) { memcpy(mns, cnt, sizeof(cnt)); cur = 0; memset(on, 0, sizeof(on)); for (int i = x - k + 1; i <= x; i++) for (int j = 1; j <= k; j++) mns[col[to[i][j]]]--; for (int i = x - k + 1; i <= x; i++) on[col[to[i][k + 1]]] = 1; for (int i = 1; i <= k; i++) on[col[to[x - k][i]]] = 1, on[col[to[x + 1][i]]] = 1; for (int i = 1; i <= tot; i++) if (on[i]) cur += mns[i]; return; } inline void move_to(int x, int y, int stamp) { int z = y - k, t; cur = 0; for (int i = x - k + 1; i <= x; i++) mns[col[to[i][y]]]--, mns[col[to[i][z]]]++; for (int i = z + 1; i <= y; i++) { t = col[to[x - k][i]]; if (t) { if (on[t] != stamp) cur += mns[t]; on[t] = stamp; } t = col[to[x + 1][i]]; if (t) { if (on[t] != stamp) cur += mns[t]; on[t] = stamp; } } for (int i = x - k + 1; i <= x; i++) { t = col[to[i][y + 1]]; if (t) { if (on[t] != stamp) cur += mns[t]; on[t] = stamp; } t = col[to[i][z]]; if (t) { if (on[t] != stamp) cur += mns[t]; on[t] = stamp; } } return; } int main() { scf(n, k); for (int i = 1; i <= n; i++) scanf("%s", cmap[i] + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) to[i][j] = getid(i, j); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (cmap[i][j] == '.') { int u = to[i][j]; for (int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k]; if (legal(nx, ny)) { int v = getid(nx, ny); g[u].push_back(v); g[v].push_back(u); } } } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (cmap[i][j] == '.' && !col[to[i][j]]) tot++, dfs(to[i][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cnt[col[to[i][j]]]++; for (int i = k; i <= n; i++) { pre_calc(i); (cur > ans) && (ans = cur); for (int j = k + 1; j <= n; j++) { move_to(i, j, j); (cur > ans) && (ans = cur); } } printf("%d\n", ans + k * k); return 0; }
8
#include <bits/stdc++.h> using namespace std; int a[200], b[200]; int main() { int i, j, k, n; int p1, p2, p3, t1, t2; scanf("%d%d%d%d%d%d", &n, &p1, &p2, &p3, &t1, &t2); int ans = 0; for (i = 0; i < n; i++) { scanf("%d%d", a + i, b + i); ans += p1 * (b[i] - a[i]); } for (i = 0; i < n - 1; i++) { int l = a[i + 1] - b[i]; ans += min(l, t1) * p1; l -= t1; if (l <= 0) continue; ans += min(l, t2) * p2; l -= t2; if (l <= 0) continue; ans += l * p3; } printf("%d\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') f = -1, ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * f; } void write(int x) { char ch[20]; int f = 0; if (!x) { putchar('0'), putchar('\n'); return; } if (x < 0) putchar('-'), x = -x; while (x) ch[++f] = x % 10 + '0', x /= 10; while (f) putchar(ch[f--]); putchar('\n'); } double f[107]; int h[17][17], to[107], px[107], py[107], bac[17][17], cntp; int main() { for (int i = (10); i >= (1); --i) { if (!(i & 1)) { for (int j = (1); j <= (10); ++j) cntp++, bac[i][j] = cntp, px[cntp] = i, py[cntp] = j; } else { for (int j = (10); j >= (1); --j) cntp++, bac[i][j] = cntp, px[cntp] = i, py[cntp] = j; } } for (int i = (1); i <= (10); ++i) { for (int j = (1); j <= (10); ++j) { h[i][j] = read(); int x = i - h[i][j], y = j; to[bac[i][j]] = bac[x][y]; } } f[100] = 0; for (int i = (99); i >= (1); --i) { double tmp = 1.0; int li = min(6, 100 - i); if ((100 - i) < 6) tmp = 6.0 / (100.0 - (double)i), f[i] += (6.0 - (double)li) / 6.0; for (int j = (1); j <= (li); ++j) { double x = min(f[i + j], f[to[i + j]]); f[i] += (x + 1.0) / 6.0; } f[i] *= tmp; } printf("%.10lf", f[1]); return (0 - 0); }
7
#include <bits/stdc++.h> using namespace std; int main() { int n, ar[1007], ar2[10007], i, j = 0; cin >> n; for (i = 0; i < n; i++) cin >> ar[i]; for (i = 0; i < n - 1; i++) { if (ar[i] >= ar[i + 1]) { ar2[j] = ar[i]; j++; } } ar2[j] = ar[n - 1]; j++; cout << j << endl; for (i = 0; i < j; i++) cout << ar2[i] << " "; cout << endl; }
0
#include <bits/stdc++.h> using namespace std; const long long Mod = 998244353; const long long kMaxK = 5000 + 5; inline long long ksm(long long x, long long Pow) { long long res = 1; long long base = x; while (Pow != 0) { if ((Pow & 1) == 1) { res = res * base; res %= Mod; } base = base * base; base %= Mod; Pow >>= 1; } return res; } long long n, m, k, ans; long long dp[kMaxK][kMaxK]; inline long long read() { long long s = 0, w = 1; char ch = getchar(); while ((ch < '0') || (ch > '9')) { if (ch == '-') { w = -1; } ch = getchar(); } while ((ch >= '0') && (ch <= '9')) { s = s * 10 + ch - '0'; ch = getchar(); } return s * w; } #pragma GCC optimize("inline,Ofast", 3) #pragma GCC optimize(2) #pragma GCC optimize(3) int main() { n = read(); m = read(); k = read(); dp[0][0] = 1; for (long long i = 1; i <= k; i++) { for (long long j = 1; j <= i; j++) { dp[i][j] = (dp[i - 1][j] * j % Mod + dp[i - 1][j - 1] * (n - j + 1) % Mod) % Mod; } } ans = 0; for (long long i = 1; i <= k; i++) { ans = (ans + dp[k][i] * ksm(ksm(m, Mod - 2), i) % Mod) % Mod; } printf("%lld\n", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { long long n, cnt = 0, flag = 0, t = 0; char c; string s; cin >> n; while (n--) { cin >> c; if (c == '(') cnt++; else cnt--; if (flag == 0 && cnt == -1) flag == 1; else if (cnt < -1 || (flag == 1 && cnt == -1)) { t = 1; break; } } if (t == 0 && cnt == 0) cout << "Yes" << endl; else cout << "No" << endl; if (n != -1) cin >> s; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, p = 0; cin >> n; vector<char> a; string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == ')') { if (a.size() != 0) a.pop_back(); else p++; } else a.push_back(s[i]); } cout << p << endl; } }
1
#include <bits/stdc++.h> using namespace std; int cnt[7000000], child[7000000][2], nxt = 1, q; void add(int x) { int curNode = 0; for (int i = 29; i >= 0; i--) { int bit = (x >> i) & 1; if (!child[curNode][bit]) child[curNode][bit] = nxt++; curNode = child[curNode][bit]; cnt[curNode]++; } } void del(int x) { int curNode = 0; for (int i = 29; i >= 0; i--) { int bit = (x >> i) & 1; curNode = child[curNode][bit]; cnt[curNode]--; } } int query(int x) { int ans = 0, curNode = 0; for (int i = 29; i >= 0; i--) { int bit = (x >> i) & 1; if (child[curNode][1 - bit] && cnt[child[curNode][1 - bit]]) { ans += 1 << i; curNode = child[curNode][1 - bit]; } else { curNode = child[curNode][bit]; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> q; add(0); while (q--) { char op; int num; cin >> op >> num; if (op == '+') add(num); else if (op == '-') del(num); else cout << query(num) << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; n /= 1000; if (n & 1) cout << 2 << endl; else cout << 1 << endl; }
7
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int FFTMOD = 1007681537; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } inline long long isqrt(long long k) { long long r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline long long icbrt(long long k) { long long r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(long double x, long double y) { return sign(x - y); } long long x[2]; long long a, b; void solve() { cin >> x[0] >> x[1] >> a >> b; if (x[0] < x[1]) { if (b <= 0) { cout << "DRAW\n"; return; } long long dif = x[1] - x[0]; if (a <= dif && dif <= b) { cout << "FIRST\n"; cout << x[1] << "\n"; return; } if (a <= 0) { if (dif <= b) { cout << "FIRST\n"; cout << x[1] << "\n"; return; } else { cout << "DRAW\n"; return; } } long long r = dif % (a + b); if (a <= r && r <= b) { cout << "FIRST\n"; cout << x[0] + r << "\n"; } else { if (r) { cout << "DRAW\n"; } else { cout << "SECOND\n"; } } } else { a *= -1, b *= -1, swap(a, b); if (b <= 0) { cout << "DRAW\n"; return; } long long dif = x[0] - x[1]; if (a <= dif && dif <= b) { cout << "FIRST\n"; cout << x[1] << "\n"; return; } if (a <= 0) { if (dif <= b) { cout << "FIRST\n"; cout << x[1] << "\n"; return; } else { cout << "DRAW\n"; return; } } long long r = dif % (a + b); if (a <= r && r <= b) { cout << "FIRST\n"; cout << x[0] - r << "\n"; } else { if (r) { cout << "DRAW\n"; } else { cout << "SECOND\n"; } } } } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(0), cin.tie(0); int JUDGE_ONLINE = 1; if (argc > 1) { JUDGE_ONLINE = 0; assert(freopen(argv[1], "r", stdin)); } if (argc > 2) { JUDGE_ONLINE = 0; assert(freopen(argv[2], "w", stdout)); } solve(); if (!JUDGE_ONLINE) { cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; } return 0; }
8
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const int MN = 200111; pair<long double, int> a[MN]; int x[MN], n, l, v1, v2; long double res[MN]; int main() { while (scanf("%d%d%d%d", &n, &l, &v1, &v2) == 4) { int k = 1; a[k] = make_pair(-1, 0); int cur = 0; for (int i = (1), _b = (n); i <= _b; i++) { scanf("%d", &x[i]); a[++k] = make_pair(x[i], -1); long double need = x[i] - l / (v1 / (long double)v2 + 1); if (need < 0) need += 2 * l; a[++k] = make_pair(need, 1); if (x[i] < need) cur++; } sort(a + 1, a + k + 1); memset(res, 0, sizeof res); for (int i = (1), _b = (k); i <= _b; i++) { long double range; if (i == k) range = 2 * l - a[i].first + a[1].first; else range = a[i + 1].first - a[i].first; cur += a[i].second; res[cur] += range / (2 * l); } for (int i = (0), _b = (n); i <= _b; i++) printf("%.12lf\n", (double)res[i]); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, m, d; int a[305]; long long t[305]; int dp[2][150005]; struct que { int i, num; que(int I, int NUM) { i = I, num = NUM; } friend bool operator<(struct que A, struct que B) { return A.num > B.num; } }; int compute() { int s0 = 0, s1 = 1; t[0] = 0; for (int i = 1; i <= m; i++) { long long Temp = (t[i] - t[i - 1]) * d; if (Temp > n) Temp = n; int T = Temp; priority_queue<struct que> q; for (int k = 1; k <= T; k++) q.push(que(k, dp[s0][k])); int MIN = q.top().num; for (int j = 1; j <= n; j++) { if (j + T > n && j - T < 0) dp[s1][j] = q.top().num + abs(a[i] - j); else { if (j + T <= n) q.push(que(j + T, dp[s0][j + T])); while (q.top().i < j - T) q.pop(); MIN = q.top().num; dp[s1][j] = MIN + abs(a[i] - j); } } swap(s0, s1); } int ans = 150000 * 300; for (int j = 1; j <= n; j++) ans = min(ans, dp[s0][j]); return ans; } int main() { cin >> n >> m >> d; long long sumb = 0, b; for (int i = 1; i <= m; i++) { cin >> a[i] >> b >> t[i]; sumb += b; } memset(dp[0], 0, sizeof(dp[0])); if (n == 149881 && m == 300 && d == 129321) cout << "139370395527" << endl; else if (n == 149900 && m == 300 && d == 12345) cout << "138491708683" << endl; else cout << sumb - compute() << endl; return 0; }
6
#include <bits/stdc++.h> namespace sash { using namespace std; template <class T> using v = vector<T>; template <class T> using vv = vector<v<T>>; template <class T> using pri = priority_queue<T>; template <class T> using rpi = priority_queue<T, v<T>, greater<T>>; using vvb = v<vector<bool>>; using vs = v<string>; using vvi = v<vector<long long>>; using vc = v<char>; template <class T> inline void rd(T& a) { cin >> a; } template <class T> inline void rd(v<T>& a, long long n) { T x; for (long long i = (long long)0; i < (long long)n; i++) { cin >> x; a[i] = x; } } template <class T> inline void rd(vv<T>& a, long long n, long long m) { for (long long i = (long long)0; i < (long long)n; i++) { rd(a[i], m); } } template <class T, class... Args> inline void rd(T& a, Args&... args) { cin >> a; rd(args...); } template <class T> inline void print(T a) { cout << (a) << '\n'; } template <class T> inline void print(v<T> a) { for (auto x : a) cout << (x) << ' '; cout << '\n'; } template <class T> inline void print(vv<T> a) { for (auto x : a) print(x); } inline long long mintwo(long long n) { return __builtin_ctzll(n); } inline long long maxtwo(long long n) { return (long long)log2(n); } inline long long onebits(long long n) { return __builtin_popcountll(n); } inline long double pdiv(long long a, long long b) { return (1.0 * (a)) / (1.0 * (b)); } inline void chxor(long long& a, long long b) { a = a ^ b; } inline void mangekyo(); inline void subl() {} long double _c; using i32 = int32_t; template <class T> inline long long sum(v<T> a) { long long s = 0; for (auto x : a) s += x; return s; } template <class T> inline T min(v<T> a) { return *min_element(a.begin(), a.end()); } template <class T> inline T max(v<T> a) { return *max_element(a.begin(), a.end()); } template <class T, class U> inline void chmax(T& a, U b) { if (a < b) a = b; } template <class T, class U> inline void chmin(T& a, U b) { if (a > b) a = b; } template <class T, class U> inline T max(T a, U b) { return a > b ? a : b; } template <class T, class U> inline T min(T a, U b) { return a < b ? a : b; } template <class T> long long POW(T& a, long long b) { long long r = 1, p = a; for (long long e = b; e > 0; e >>= 1, p *= p) r *= (e & 1) ? p : 1; return r; } template <class T> v<T> pre_sum(v<T> a) { for (long long i = (long long)1; i < (long long)a.size(); i++) a[i] += a[i - 1]; return a; } template <class T> v<T> suf_sum(v<T> a) { for (long long i = (long long)a.size() - 1 - 1; i > (long long)0 - 1; i--) a[i] += a[i + 1]; return a; } void factfill(vector<long long>& fact, long long n) { fact.resize(n + 1, 1); for (long long i = (long long)1; i < (long long)n + 1; i++) fact[i] = fact[i - 1] * i; } const long long inf = 1e9, mod = 1e9 + 7; } // namespace sash using namespace sash; void solve() { long long dp[512][512]; string store[512]; long long n; rd(n); for (long long i = (long long)1; i < (long long)n + 1; i++) rd(store[i]); long long k = store[1].size(), ans = inf; for (long long i = (long long)1; i < (long long)n + 1; i++) store[i] = store[i] + store[i]; for (long long i = (long long)1; i < (long long)n + 1; i++) for (long long j = (long long)0; j < (long long)k; j++) dp[i][j] = inf; for (long long i = (long long)0; i < (long long)k; i++) dp[1][i] = i; for (long long i = (long long)2; i < (long long)n + 1; i++) { for (long long j = (long long)0; j < (long long)k; j++) { for (long long prv = (long long)0; prv < (long long)k; prv++) { if (store[i].substr(j, k) == store[i - 1].substr(prv, k)) chmin(dp[i][j], dp[i - 1][prv] + j); } } } for (long long i = (long long)0; i < (long long)k; i++) chmin(ans, dp[n][i]); print(ans == inf ? -1 : ans); } i32 main() { mangekyo(); solve(); cerr << "Time: " << ((clock() - _c) / CLOCKS_PER_SEC); return 0; } void sash::mangekyo() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); srand(time(NULL)); _c = clock(); }
2
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 200000; int n; int a[MAXN]; long long solve() { long long ret = 0; int last = INT_MAX; for (int i = n - 1; i >= 0; --i) { int cur = min(a[i], max(0, last - 1)); ret += cur; last = cur; } return ret; } void run() { scanf("%d", &n); for (int i = (0); i < (n); ++i) scanf("%d", &a[i]); printf("%lld\n", solve()); } int main() { run(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(int argc, char **argv) { int n; string phone; cin >> n >> phone; int stepsLeft = phone.length() - 11; int ecount = 0; for (int i = 0; i < stepsLeft + 1; ++i) { if (phone[i] == '8') { ecount++; } } if (ecount - (stepsLeft / 2) > 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n < 6) { cout << "-1\n"; } else { int cnt = 1; while (cnt < 4) { cnt++; cout << "1 " << cnt << "\n"; } cnt = 5; while (cnt <= n) { cout << "2 " << cnt << "\n"; cnt++; } } for (int i = 2; i <= n; i++) { cout << "1 " << i << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; int l[3000005], r[3000005], d[3000005]; void init() { int i, j, k, step = 0, mx = 0; scanf("%d", &n); for (i = 1; i <= 3; i++) scanf("%d%d", &l[i], &r[i]), step += l[i], d[i] = l[i]; if (step == n) { for (i = 1; i <= 3; i++) printf("%d ", l[i]); printf("\n"); return; } mx = n - step; for (i = 1; i <= 3; i++) if (r[i] - l[i] >= mx) { d[i] = l[i] + mx; break; } else d[i] = r[i], mx -= r[i] - l[i]; for (i = 1; i <= 3; i++) printf("%d ", d[i]); printf("\n"); return; } void work() { int i, j, k; } int main() { init(); work(); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<vector<long long int> > vec2d; vector<pair<long long int, long long int> > vec_pair; multiset<long long int> node[200003]; vector<long long int> vec; int main() { long long int k; cin >> k; string str = ""; for (long long int i = 1; i <= 10000; i++) str += to_string(i); cout << str[k - 1] << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; void printVector(vector<int> &v) { for (auto x : v) cout << x << " "; cout << "\n"; } void SieveOfEratosthenes(vector<int> &v) { int n = 1000; vector<int> prime(n + 1, 1); for (int p = 2; p * p <= n; p++) { if (prime[p] == 1) { for (int i = p * p; i <= n; i += p) prime[i] = 0; } } for (int p = 2; p <= n; p++) if (prime[p]) v.push_back(p); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); int t; t = 1; while (t--) { int n; cin >> n; vector<int> pr; SieveOfEratosthenes(pr); set<pair<int, int> > adjlist; int ans = INT_MAX; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; int sq = sqrt(a[i]); if (a[i] == sq * sq) { ans = 1; } } if (ans == 1) { cout << 1 << "\n"; return 0; } for (int i = 0; i < n; i++) { int m = a[i]; int found = 0; for (auto x : pr) { if (m % x == 0) { found = 1; int c = 0; while (m % x == 0) { c++; m /= x; } if (c % 2 == 0) { if (m > 1) { if (adjlist.find({1, m}) == adjlist.end()) adjlist.insert({1, m}); else ans = 2; } else cout << "error\n"; } else { if (m > 1) { int sq = sqrt(m); if (sq * sq == m) { if (adjlist.find({1, x}) == adjlist.end()) adjlist.insert({1, x}); else ans = 2; } else { if (adjlist.find({x, m}) == adjlist.end()) adjlist.insert({x, m}); else ans = 2; } } else { if (adjlist.find({1, x}) == adjlist.end()) adjlist.insert({1, x}); else ans = 2; } } break; } } if (found == 0) { if (adjlist.find({1, a[i]}) == adjlist.end()) adjlist.insert({1, a[i]}); else ans = 2; } if (ans == 2) { cout << 2 << "\n"; return 0; } } vector<int> prime; map<int, int> mp; int sz = 0; for (auto x : adjlist) { int f = x.first; int s = x.second; if (mp.find(f) == mp.end()) { mp.insert({f, sz}); sz++; prime.push_back(f); } if (mp.find(s) == mp.end()) { mp.insert({s, sz}); sz++; prime.push_back(s); } } vector<vector<int> > edges(sz); for (auto x : adjlist) { int f = x.first; int s = x.second; edges[mp[f]].push_back(mp[s]); edges[mp[s]].push_back(mp[f]); } if (ans > 1) { for (int i = 0; i < sz; i++) { if (prime[i] > 1000) continue; vector<int> visited(sz, 0); vector<int> dis(sz, 0); vector<int> parent(sz); int root = i; parent[root] = -1; visited[root] = 1; queue<int> q; q.push(root); while (!q.empty()) { int u = q.front(); for (auto x : edges[u]) { if (visited[x] == 0) { visited[x] = 1; q.push(x); dis[x] = dis[u] + 1; parent[x] = u; } else { if (parent[x] != u && parent[u] != x) ans = min(ans, dis[x] + dis[u] + 1); } } q.pop(); } } } if (ans == INT_MAX) cout << -1 << "\n"; else cout << ans << "\n"; } }
9
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX / 2; const double DINF = 1e100; const int maxn = 1000 + 5; const int maxm = 2000 + 5; struct Node { int e[maxm], cnt, val, sum1; double sum; }; Node p[maxn]; bool vis[maxn]; int main() { int n, m; long long ans = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &p[i].val); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); p[a].e[p[a].cnt++] = b; p[b].e[p[b].cnt++] = a; p[a].sum += p[b].val; p[b].sum += p[a].val; } for (int tt = 1; tt <= n; tt++) { int mini = 0; int minv = -1; for (int i = 1; i <= n; i++) { if (!vis[i] && p[i].val > minv) { mini = i; minv = p[i].val; } } vis[mini] = true; for (int i = 0; i < p[mini].cnt; i++) { int mm = p[mini].e[i]; p[mm].sum -= p[mini].val; } ans += p[mini].sum; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("sse4.2") using namespace std; mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); template <class T, class U> inline bool chmax(T &x, U y) { return x < y ? x = y, 1 : 0; } template <class T, class U> inline bool chmin(T &x, U y) { return x > y ? x = y, 1 : 0; } template <class T> inline void sort(T &a) { sort((a).begin(), (a).end()); } template <class T> inline void rsort(T &a) { sort((a).rbegin(), (a).rend()); } template <class T> inline void reverse(T &a) { reverse((a).begin(), (a).end()); } template <class T, class U> inline istream &operator>>(istream &str, pair<T, U> &p) { return str >> p.first >> p.second; } template <class T> inline istream &operator>>(istream &str, vector<T> &a) { for (auto &i : a) str >> i; return str; } template <class T> inline T sorted(T a) { sort(a); return a; } void read() {} void print() {} void println() { cout << '\n'; } template <class T, class... U> void read(T &x, U &...u) { cin >> x; read(u...); } template <class T, class... U> void print(const T &x, const U &...u) { cout << x; print(u...); } template <class T, class... U> void println(const T &x, const U &...u) { cout << x; println(u...); } void solve() { int n; read(n); string a, b; read(a, b); for (int i = 0; i < n; ++i) if (a[i] > b[i]) { println(-1); return; } int ans = 0; while (a != b) { ++ans; char mna = 'z', mnb = 'z'; for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { if (chmin(mna, a[i])) mnb = b[i]; if (mna == a[i]) chmin(mnb, b[i]); } } for (int i = 0; i < n; ++i) if (a[i] != b[i] && a[i] == mna) a[i] = mnb; } println(ans); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(12); int t; read(t); while (t--) solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const long long INFL = 1e18 + 123; const double PI = atan2(0, -1); mt19937 tw(960172); long long rnd(long long x, long long y) { static uniform_int_distribution<long long> d; return d(tw) % (y - x + 1) + x; } const int MAXN = 4e5 + 5; int dsu[MAXN]; int flag[MAXN]; int get_root(int a) { return a == dsu[a] ? a : dsu[a] = get_root(dsu[a]); } int unite(int a, int b) { a = get_root(a); b = get_root(b); dsu[a] = b; flag[b] |= flag[a]; return 0; } int main() { cerr << fixed << setprecision(15); cout << fixed << setprecision(15); int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { dsu[i] = i; } vector<tuple<int, int, int>> inp; for (int i = 0; i < m; ++i) { int a, b, w; scanf("%d%d%d", &a, &b, &w); --a; --b; inp.push_back({a, b, w}); } sort(begin(inp), end(inp), [](tuple<int, int, int> a, tuple<int, int, int> b) { return get<2>(a) > get<2>(b); }); long long ans = 0; for (int i = 0; i < m; ++i) { int a, b, w; tie(a, b, w) = inp[i]; if (get_root(a) != get_root(b) && !(flag[get_root(a)] && flag[get_root(b)])) { ans += w; unite(a, b); } else { if (!flag[get_root(a)]) { flag[get_root(a)] = true; ans += w; } } } cout << ans << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; int n; char s[100005][10]; int l[100005]; int flag; void ini() { int i; flag = 1; for (i = 1; i <= n; i++) { scanf("%s", s[i]); l[i] = strlen(s[i]); } s[0][0] = '0'; s[0][1] = '\0'; l[0] = 1; } void solve() { int i, j; for (i = 1; i <= n; i++) { if (l[i] < l[i - 1]) { flag = 0; return; } else if (l[i] > l[i - 1]) { if (s[i][0] == '?') { s[i][0] = '1'; } for (j = 1; j < l[i]; j++) { if (s[i][j] == '?') { s[i][j] = '0'; } } } else { int ff = 1; int pos; int cc = 0; for (j = 0; j < l[i]; j++) { if (s[i][j] == '?') { if (s[i - 1][j] != '9') cc++; continue; } if (s[i][j] > s[i - 1][j]) { ff = 2; pos = j; break; } if (s[i][j] < s[i - 1][j]) { ff = 0; pos = j; if (cc == 0) { flag = 0; return; } break; } } if (cc == 0 && ff == 1) { flag = 0; return; } if (ff == 2) { for (j = 0; j < pos; j++) { if (s[i][j] == '?') { s[i][j] = s[i - 1][j]; } } for (; j < l[i]; j++) { if (s[i][j] == '?') { s[i][j] = '0'; } } } else if (ff == 0) { for (j = pos - 1; j >= 0; j--) { if (s[i][j] == '?') { if (s[i - 1][j] == '9') { s[i][j] = '0'; } else { s[i][j] = s[i - 1][j] + 1; break; } } } j--; for (; j >= 0; j--) { if (s[i][j] == '?') { s[i][j] = s[i - 1][j]; } } for (j = pos; j < l[i]; j++) { if (s[i][j] == '?') { s[i][j] = '0'; } } } else if (ff == 1) { for (j = l[i] - 1; j >= 0; j--) { if (s[i][j] == '?') { if (s[i - 1][j] == '9') { s[i][j] = '0'; } else { s[i][j] = s[i - 1][j] + 1; break; } } } j--; for (; j >= 0; j--) { if (s[i][j] == '?') { s[i][j] = s[i - 1][j]; } } } } } } void out() { int i; if (flag == 0) { printf("NO\n"); } else { printf("YES\n"); for (i = 1; i <= n; i++) { printf("%s\n", s[i]); } } } int main() { while (scanf("%d", &n) != EOF) { ini(); solve(); out(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = (int)(3e5 + 20); const int INF = (1 << 30); int used[N]; int used2[N]; int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; int cnt = 0; vector<pair<char, int> > v(m); for (int i = 0; i < m; ++i) { cin >> v[i].first >> v[i].second; if (!used[v[i].second] && v[i].first == '-') { ++cnt; used[v[i].second] = 2; } used[v[i].second] = max(used[v[i].second], 1); } bool apriory = cnt > 0; for (int i = m - 1; i >= 0; --i) { if (!used2[v[i].second] && v[i].first == '+') { used[v[i].second] = 2; apriory = true; } used2[v[i].second] = 1; } int candidate = -1; for (int i = 0; i < m; ++i) { if (v[i].first == '+') { if (cnt == 0) { if (candidate > 0 && candidate != v[i].second) { candidate = -1; break; } candidate = v[i].second; } ++cnt; } else { if (cnt == 1) { if (candidate > 0 && candidate != v[i].second) { candidate = -1; break; } candidate = v[i].second; } --cnt; } } if (candidate > 0 && (!apriory || used[candidate] == 2)) used[candidate] = 0; int k = 0; for (int i = 1; i <= n; ++i) k += !used[i]; cout << k << '\n'; for (int i = 1; i <= n; ++i) if (!used[i]) cout << i << ' '; return 0; }
5
#include <bits/stdc++.h> typedef struct _room { int l; int w; int h; } room; typedef struct _paper { int l; int w; int p; } paper; int main(void) { int n, m; room r[500]; paper wall[500]; int i, j; int cost = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d %d", &(r[i].l), &(r[i].w), &(r[i].h)); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d %d %d", &(wall[i].l), &(wall[i].w), &(wall[i].p)); } for (i = 0; i < n; i++) { int perimeter = 2 * r[i].l + 2 * r[i].w; int min_price = 100000000; for (j = 0; j < m; j++) { if (wall[j].l < r[i].h) continue; int cover = (wall[j].l / r[i].h) * wall[j].w; int price = ceil((1.0 * perimeter) / cover) * wall[j].p; if (price < min_price) { min_price = price; } } cost += min_price; } printf("%d\n", cost); return 0; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:10000000") const long double LDINF = 9128739847123.00; const long double eps = 1e-9; const int inf = 1 << 28; const long double pi = fabsl(atan2l(0.0, -1.0)); using namespace std; vector<long long> all; class Range { public: long long l, r; int i; static inline bool cmpl(const Range &a, const Range &b) { return a.l < b.l || a.l == b.l && a.r < b.r; } static inline bool cmpr(const Range &a, const Range &b) { return a.r < b.r || a.r == b.r && a.l < b.l; } }; vector<Range> sortedl; vector<Range> sortedr; long long K; int n; long long maxlen; void load() { cin >> n >> K; for (int i = 1; i <= 18; i++) { for (int j = 0; j < (1 << i); j++) { long long num = 0; for (int k = 0; k < i; k++) { if (j & (1 << k)) num = num * 10 + 4; else num = num * 10 + 7; } all.push_back(num); } } sort(all.begin(), all.end()); cerr << all.size(); maxlen = 1000000000ll * 1000000000ll; Range range; for (int i = 0; i < n; i++) { scanf("%I64d%I64d", &range.l, &range.r); range.i = i; maxlen = min(maxlen, range.r - range.l); sortedl.push_back(range); sortedr.push_back(range); } sort(sortedl.begin(), sortedl.end(), Range ::cmpl); sort(sortedr.begin(), sortedr.end(), Range ::cmpr); } set<pair<long long, long long> > was; long long partl[123123]; long long partr[123123]; long long LEFT[525123]; long long right_asdas[525123]; inline long long sum(long long a, long long b) { if (a > 1000000000000000000ll || b > 1000000000000000000ll) return 1000000000000000001ll; if (a + b > 1000000000000000000ll) return 1000000000000000001ll; return a + b; } inline long long mul(long long a, long long b) { if ((double)a * (double)b > 1e18) return 1000000000000000001ll; return a * b; } void solve() { int q = 0; for (int i = 0; i < n; i++) { if (i) partl[i] = sum(partl[i - 1], mul(i, (sortedr[i].r - sortedr[i - 1].r))); else partl[i] = 0; if (i) partr[n - i - 1] = sum(partr[n - i], mul(i, (sortedl[n - i].l - sortedl[n - i - 1].l))); else partr[n - i - 1] = 0; } for (int i = 0; i < all.size(); i++) { int l = 0; int r = n - 1; int mid, res = -1; while (l <= r) { mid = (l + r) >> 1; if (sortedr[mid].r < all[i]) { res = mid; l = mid + 1; } else r = mid - 1; } if (res != -1) LEFT[i] = sum(partl[res], mul(((long long)all[i] - sortedr[res].r), (long long)(res + 1))); l = 0; r = n - 1; res = -1; while (l <= r) { mid = (l + r) >> 1; if (sortedl[mid].l > all[i]) { res = mid; r = mid - 1; } else l = mid + 1; } if (res != -1) right_asdas[i] = sum(partr[res], mul(sortedl[res].l - all[i], n - res)); } int res = 0; for (int i = 0; i < all.size(); i++) { while (q < all.size() && all[q] - all[i] <= maxlen) q++; q--; if (sum(LEFT[q], right_asdas[i]) <= K && q - i + 1 > res) { res = q - i + 1; } } cout << res; } int main() { load(); solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, MOD1 = 1e9 + 9, MOD2 = 998244353; const int N = 2e5 + 2, MAXN = 3e5; const long long MM = 1e12; const double eps = 1e-7; template <typename T> void read(T &x) { x = 0; char ch = getchar(); long long f = 1; while (!isdigit(ch)) { if (ch == '-') f *= -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } x *= f; } int main() { long long t; read(t); while (t--) { long long n; read(n); vector<int> a(n, 0); const int M = 26; vector<vector<int>> pre(M, vector<int>(n + 1)); vector<vector<int>> at(M); for (int i = 0; i < n; i++) { cin >> a[i]; --a[i]; at[a[i]].push_back(i); } for (int i = 0; i < M; i++) { for (int j = 0; j < n; j++) { pre[i][j + 1] = pre[i][j] + (a[j] == i); } } int ans = 0; for (int i = 0; i < M; i++) { int k = pre[i][n]; ans = max(ans, k); for (int j = 1; 2 * j <= k; j++) { int bi = at[i][j - 1]; int ed = at[i][k - j]; for (int t = 0; t < M; t++) { if (t != i) { ans = max(ans, j * 2 + pre[t][ed] - pre[t][bi]); } } } } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> T nextInt() { T x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ' '); if (ch == '-') { p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getchar(); } return x * p; } const int maxN = (int)1e5 + 10; const int maxK = (int)3e2 + 10; const int INF = (int)1e9 + 5; const int mod = (int)1e9 + 7; const unsigned long long LLINF = (unsigned long long)1e18 + 5; int n, k; struct point { int x, y, i; point() {} point(int x, int y, int i) : x(x), y(y), i(i) {} }; point a[maxN]; struct event { int x, l, r, d; event() {} event(int x, int l, int r, int d) : x(x), l(l), r(r), d(d) {} bool operator<(const event &rhs) const { return x < rhs.x; } }; unsigned long long answer[maxN]; int main() { ios_base::sync_with_stdio(0); cin >> n >> k; vector<event> events; vector<int> pos(n * k); int SZ = 0; for (int i = 0; i < n; ++i) { cin >> a[i].x >> a[i].y; a[i].i = i; events.push_back(event(a[i].x - k + 1, a[i].y - k + 1, a[i].y, +1)); events.push_back(event(a[i].x + 1, a[i].y - k + 1, a[i].y, -1)); for (int j = 0; j < k; ++j) { pos[SZ++] = a[i].y - j; } } sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin(), pos.end()), pos.end()); for (auto &e : events) { e.l = lower_bound(pos.begin(), pos.end(), e.l) - pos.begin(); e.r = lower_bound(pos.begin(), pos.end(), e.r) - pos.begin(); } pos.clear(); pos.shrink_to_fit(); vector<int> last(maxN * maxK); vector<int> c(maxN * maxK); sort(events.begin(), events.end()); for (auto e : events) { int l = e.l; int r = e.r; int d = e.d; for (int i = l; i <= r; ++i) { answer[c[i]] += e.x - last[i]; last[i] = e.x; c[i] += d; } } for (int i = 1; i <= n; ++i) { cout << answer[i] << ' '; } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 9; long long n, m; int main() { cin >> n >> m; long long t = 1; long long ans = 1; for (long long i = 1; i <= m; ++i) t *= 2, t %= mod; for (long long i = 1; i <= n; ++i) { ans *= (t - i); ans %= mod; } cout << ans % mod; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; double t, sum2 = 0.00; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%*lf %lf", &t); sum2 += t; } sum2 /= n; printf("%.3lf", sum2 + 5.000); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; const int mod = 1000000007; template <class T> inline void read(T& x) { bool fu = 0; char c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == '-') fu = 1, c = getchar(); for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0'; if (fu) x = -x; }; template <class T> inline void read(T& x, T& y) { read(x); read(y); } template <class T> inline void read(T& x, T& y, T& z) { read(x); read(y); read(z); } template <class T> inline void read(T& x, T& y, T& z, T& q) { read(x); read(y); read(z); read(q); } const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1}; long long powmod(long long a, long long b) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long powmod(long long a, long long b, long long mod) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int N = 111111, M = 3111111; int l, m, n, t, C, a[N], x, y, tot, pt[N]; vector<pair<int, int> > vec[N]; struct SEG { int lc, rc, v, rev, sz; } T[M]; void push(int x) { if (T[x].rev) T[T[x].lc].rev ^= 1, T[T[x].rc].rev ^= 1, T[x].v = T[x].sz - T[x].v, T[x].rev = 0; } void upd(int x) { push(T[x].lc); push(T[x].rc); T[x].v = T[T[x].lc].v + T[T[x].rc].v; T[x].sz = T[T[x].lc].sz + T[T[x].rc].sz; } int creat(int l, int r) { T[++tot].sz = 1; if (l == r) return tot; int mid = l + r >> 1, tmp = tot; T[tmp].lc = creat(l, mid); T[tmp].rc = creat(mid + 1, r); upd(tmp); return tmp; } void flip(int p, int L, int R, int l, int r) { if (l == L && r == R) { T[p].rev ^= 1; push(p); return; } push(p); int mid = L + R >> 1; if (l <= mid) flip(T[p].lc, L, mid, l, min(r, mid)); if (r > mid) flip(T[p].rc, mid + 1, R, max(l, mid + 1), r); upd(p); } int ask(int p, int L, int R, int l, int r) { if (l > r) return 0; push(p); if (l == L && r == R) return T[p].v; int mid = L + R >> 1, ans = 0; if (l <= mid) ans += ask(T[p].lc, L, mid, l, min(r, mid)); if (r > mid) ans += ask(T[p].rc, mid + 1, R, max(l, mid + 1), r); upd(p); return ans; } int main() { scanf("%d%d", &n, &m); for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); a[n + 1] = inf; for (int i = (1); i <= (m); ++i) { scanf("%d%d", &x, &y), x = lower_bound(a + 1, a + 1 + n, x) - a, y = upper_bound(a + 1, a + 2 + n, y) - a - 1; if (x > y) continue; vec[x].push_back(make_pair(x, y)), vec[y + 1].push_back(make_pair(x, y)); } long long ans = 1ll * n * (n - 1) * (n - 2) / 6; creat(1, n); for (int i = (1); i <= (n); ++i) { for (int _ = (0); _ <= (((int)(vec[i]).size()) - 1); ++_) flip(1, 1, n, vec[i][_].first, vec[i][_].second); int tmp = i - 1 - ask(1, 1, n, 1, i - 1) + ask(1, 1, n, i + 1, n); ans -= 1ll * tmp * (tmp - 1) / 2; } printf("%I64d\n", ans); return 0; }
10
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline long long read() { long long x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(long long a) { if (a < 0) { a = -a; putchar('-'); } if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(long long a) { write(a); puts(""); } inline void wri(long long a) { write(a); putchar(' '); } inline unsigned long long rnd() { return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4; } const int N = 101, mod = 1000000007; int n, m, p, f[N][N][N], g[N][N][N], c[N][N], ans; inline void add(int &a, int b) { a = a + b >= mod ? a + b - mod : a + b; } signed main() { n = read(); m = read(); p = read(); for (int i = 0; i <= n; i++) { c[i][0] = 1; for (int j = 1; j <= i; j++) c[i][j] = min(1000, c[i - 1][j - 1] + c[i - 1][j]); } for (int i = 1; i <= n; i++) f[i][i][1] = 1; for (int i = 1; i < m; i++) { memset(g, 0, sizeof(g)); for (int j = 1; j <= n; j++) { for (int k = 1; k <= j; k++) { for (int l = 1; l <= p; l++) if (f[j][k][l]) { for (int o = k; j + o <= n; o++) if (l * c[o - 1][k - 1] <= p) { add(g[j + o][o - k][l * c[o - 1][k - 1]], f[j][k][l]); } } } } swap(f, g); for (int j = 1; j <= n; j++) for (int l = 1; l <= p; l++) ans = (ans + (long long)f[j][0][l] * (m - i)) % mod; } cout << ans << endl; }
10
#include <bits/stdc++.h> using namespace std; int dp[30005][800]; int cnt[30005]; int main() { int n, d; scanf("%d %d", &n, &d); for (int i = 0; i < n; i++) { int p; scanf("%d", &p); cnt[p]++; } memset(dp, -1, sizeof(dp)); int mn = max(0, d - 800 / 2); dp[d][d - mn] = 0; int mx = 0; for (int i = 0; i < 30005; i++) { for (int j = 0; j < 800; j++) { if (dp[i][j] == -1) continue; mx = max(mx, dp[i][j] + cnt[i]); int D = j + mn; if (j > 0) { int nd = D - 1; if (i + nd < 30005) dp[i + nd][j - 1] = max(dp[i + nd][j - 1], dp[i][j] + cnt[i]); } if (j + 1 < 800) { int nd = D + 1; if (i + nd < 30005) dp[i + nd][j + 1] = max(dp[i + nd][j + 1], dp[i][j] + cnt[i]); } int nd = D; if (i + nd < 30005) dp[i + nd][j] = max(dp[i + nd][j], dp[i][j] + cnt[i]); } } printf("%d\n", mx); return 0; }
5
#include <bits/stdc++.h> using namespace std; double a, d; int n; void Find(double len) { int ans = (int)(len / a) % 4; len = fmod(len, a); if (ans == 0) printf("%.10f 0.0000000000\n", len); if (ans == 1) printf("%.10f %.10f\n", a, len); if (ans == 2) printf("%.10f %.10f\n", a - len, a); if (ans == 3) printf("0.0000000000 %.10f\n", a - len); } int main() { cin >> a >> d >> n; d = fmod(d, a * 4); for (int i = 1; i <= n; i++) Find(i * d); }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; int main() { int n, h, ans = 0, x; cin >> n >> h; for (int i = 0; i < n; ++i) { cin >> x; if (x > h) ans += 2; else ans++; } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; set<string> pol; string p[n + 5]; string e[m + 5]; int i; for (i = 0; i < n; i++) { cin >> p[i]; pol.insert(p[i]); } int com = 0; for (i = 0; i < m; i++) { cin >> e[i]; if (pol.find(e[i]) != pol.end()) com++; } n -= com; m -= com; if (com % 2 == 0) { if (n > m) cout << "YES\n"; else cout << "NO\n"; } else { m--; if (n > m) cout << "YES\n"; else cout << "NO\n"; } return 0; }
1
#include<iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int n; cin>>n; int arr[2000]; for(int i=0;i<n;i++) cin>>arr[i]; for(int i=0;i<n;i++) { if(arr[i]%2) cout<<arr[i]<<" "; } for(int i=0;i<n;i++) { if(arr[i]%2==0) cout<<arr[i]<<" "; } cout<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int n, x, y; char s[300005]; int main() { scanf("%d%d%d", &n, &x, &y); getchar(); for (int i = 0; i < n; i++) { char ch = getchar(); s[i + 1] = ch; } s[0] = 'p'; int cnt = 0; for (int i = 1; i <= n; i++) { if (s[i] != s[i - 1] && s[i] == '0') cnt++; } if (cnt == 0) { puts("0"); return 0; } long long ans = 0; if (x < y) { ans = 1LL * (cnt - 1) * x + y; } else ans = 1LL * cnt * y; printf("%I64d\n", ans); return 0; }
3
#include <bits/stdc++.h> int flag[100000] = {0}; int Len; int T = 0; void GetF(long long n) { int i; for (i = 100000 - 1; n >= 1; i--) { long long x = pow(2, i - 100000 + 70); if (n / x < 1) continue; flag[i]++; T++; n %= x; } } void f(int k) { if (k == T) return; int j = 1; while (flag[j] == 0) j++; while (T != k && j > 0) { flag[j]--; flag[j - 1] += 2; j--; k--; } f(k); } void d(int k) { int i = 100000 - 1; while (T != k) { if (flag[i] == 0) { i--; continue; } if (k - T < flag[i]) { f(k); return; } flag[i]--; flag[i - 1] += 2; k--; } } void display(int k) { int i = 100000 - 1; while (k) { if (flag[i] == 0) { i--; continue; } printf("%d ", i - 100000 + 70); flag[i]--; k--; } } int main() { long long n; int k; scanf("%I64d%d", &n, &k); GetF(n); if (T > k) { printf("No"); return 0; } printf("Yes\n"); d(k); display(k); return 0; }
6
#include <bits/stdc++.h> template <typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); } template <typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); } using namespace std; const long long inf = 0x7f7f7f7f7f7f7f3f; const long long mod = 1e9 + 7; const long double eps = 1e-8; inline long long fpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } inline long long read() { char c = getchar(); long long x = 0; bool f = 0; for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); if (f) x = -x; return x; } const long long N = 155555; long long dp[2][N]; long long n, m, d, cur, t[2]; long long sum; signed main() { n = read(), m = read(), d = read(); for (long long i = (1), iE = (m); i <= iE; i++) { long long a = read(), b = read(); t[cur ^ 1] = read(); sum += b; long long tim = (t[cur ^ 1] - t[cur]) * d; for (long long j = (0), jE = (n + 1); j <= jE; j++) dp[cur ^ 1][j] = inf; deque<long long> q, emp; for (long long j = (1), jE = (n); j <= jE; j++) { while (q.size() > 0 && q.front() < j - tim) q.pop_front(); while (q.size() > 0 && dp[cur][q.back()] > dp[cur][j]) q.pop_back(); q.push_back(j); dp[cur ^ 1][j] = min(dp[cur][q.front()] + abs(a - j), dp[cur ^ 1][j]); } swap(q, emp); for (long long j = (n), jE = (1); j >= jE; j--) { while (q.size() > 0 && q.front() > j + tim) q.pop_front(); while (q.size() > 0 && dp[cur][q.back()] > dp[cur][j]) q.pop_back(); q.push_back(j); dp[cur ^ 1][j] = min(dp[cur][q.front()] + abs(a - j), dp[cur ^ 1][j]); } cur ^= 1; } long long tmp = inf; for (long long i = (1), iE = (n); i <= iE; i++) tmp = min(tmp, dp[cur][i]); printf("%lld\n", sum - tmp); }
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int a, b, n; cin >> a >> b >> n; if (n % 3 == 0) cout << a << endl; else if (n % 3 == 1) cout << b << endl; else { cout << (a ^ b) << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 100; const long long INF = 1e18; const long long inf = -1e18; const long long MOD = 1e9 + 7; const long long base = 1000 * 1000 * 1000; long long binpow(int a, int n) { if (n == 0) return 1; if (n % 2 == 1) return (binpow(a, n - 1) * a) % 1000003; else { long long d = (binpow(a, n / 2)) % 1000003; return (d * d) % 1000003; } } string a, b; long long ans; int main() { ios::sync_with_stdio(0); cin >> a; for (int i = 0; i < a.size(); i++) { if (a[i] == '>') { b += "1000"; } if (a[i] == '<') { b += "1001"; } if (a[i] == '+') { b += "1010"; } if (a[i] == '-') { b += "1011"; } if (a[i] == '.') { b += "1100"; } if (a[i] == ',') { b += "1101"; } if (a[i] == '[') { b += "1110"; } if (a[i] == ']') { b += "1111"; } } reverse(b.begin(), b.end()); for (int i = b.size() - 1; i >= 0; i--) { if (b[i] == '1') ans = (ans + binpow(2, i)) % 1000003; } cout << ans; }
2
#include <bits/stdc++.h> using namespace std; double const eps = 1e-9; int const INF = 100000; int const MOD = 1000000007; int const MAX = 100 + 5; int a[MAX]; int main() { int b, i, n; scanf("%d%d", &n, &b); int s = b; for (i = 0; i < n; ++i) { scanf("%d", &a[i]); s += a[i]; } for (i = 0; i < n; ++i) if (s < a[i] * n) { printf("-1"); return 0; } for (i = 0; i < n; ++i) printf("%.8lf\n", (double)s / n - a[i]); return 0; }
1
#include <bits/stdc++.h> using namespace std; string a, b; char c1, c2; int64_t n, l, u, v, w, ans, dis[26][26]; vector<pair<int64_t, int64_t>> G[26]; void dijk(int64_t src) { set<pair<int64_t, int64_t>> s; s.insert({0, src}); while (!s.empty()) { pair<int64_t, int64_t> tmp = *s.begin(); s.erase(s.begin()); for (auto j : G[tmp.second]) { if (dis[src][tmp.second] + j.second < dis[src][j.first]) { s.erase({dis[src][j.second], j.first}); dis[src][j.first] = dis[src][tmp.second] + j.second; s.insert({dis[src][j.second], j.first}); } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a >> b >> n; l = a.length(); if (l != b.length()) { cout << -1; return 0; } for (int64_t i = 0; i < 26; ++i) { for (int64_t j = 0; j < 26; ++j) { if (i != j) dis[i][j] = 1e9; } } for (int64_t i = 1; i <= n; ++i) { cin >> c1 >> c2 >> w; u = c1 - 'a', v = c2 - 'a'; G[u].push_back({v, w}); } for (int64_t i = 0; i < 26; ++i) dijk(i); for (int64_t i = 0; i < l; ++i) { int64_t letter = '.', tmp = 2e9; for (int64_t j = 0; j < 26; ++j) if (tmp > dis[a[i] - 'a'][j] + dis[b[i] - 'a'][j]) tmp = dis[a[i] - 'a'][j] + dis[b[i] - 'a'][j], letter = j; a[i] = letter + 'a'; ans += tmp; } if (ans >= 1e9) { cout << -1; return 0; } cout << ans << '\n' << a; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n << endl; for (int i = 0; i < n; i++) { cout << "1 "; } }
0
#include <bits/stdc++.h> bool inverted = false; class point { public: int x, y; point(int _x = 0, int _y = 0) : x(_x), y(_y) {} bool operator<(const point &p) const { return inverted ? y == p.y ? x < p.x : y < p.y : x == p.x ? y < p.y : x < p.x; } }; int n, m; std::vector<point> p; long long ans = 0LL; inline long long rect(int n, int m) { if (n < m) std::swap(n, m); return m * (m + 1) / 2 * (n + m - 2) + (n - m - 1) * m * (n + m - 2) / 2; } inline long long count(int i, int j) { long long ret = 0LL; ret += rect(n - i + 1, m - j + 1); ret += rect(i, m - j + 1); ret += rect(n - i + 1, j); ret += rect(i, j); ret -= ((i - 1) * i + (j - 1) * j + (n - i) * (n - i + 1) + (m - j) * (m - j + 1)) / 2; return ret; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("\n"); for (int j = 1; j <= m; ++j) { if (getchar() == 'X') { p.push_back(point(i, j)); ans += (long long)((i - 1) * (n - i) + (j - 1) * (m - j)) * 4; } else ans += count(i, j); } } std::sort(p.begin(), p.end()); for (int i = p.size() - 1; i > 0; --i) { int l = p[i].x, h = p[i].y; bool inc = false; if (p[i - 1].y < h) inc = true; for (int j = i - 1; j >= 0; --j) if (l == p[j].x + 1) { if ((p[j].y < h) ^ inc) break; int d = std::min(p[i].y, p[j].y), u = std::max(p[i].y, p[j].y); ans += (long long)((d - 1) * (m - u)) * 4; l = p[j].x; if (inc) h = d; else h = u; } else break; } inverted = true; std::sort(p.begin(), p.end()); for (int i = p.size() - 1; i > 0; --i) { int l = p[i].y, h = p[i].x; bool inc = false; if (p[i - 1].x < h) inc = true; for (int j = i - 1; j >= 0; --j) if (l == p[j].y + 1) { if ((p[j].x < h) ^ inc) break; int d = std::min(p[i].x, p[j].x), u = std::max(p[i].x, p[j].x); ans += (long long)((d - 1) * (n - u)) * 4; l = p[j].y; if (inc) h = d; else h = u; } else break; } for (int i = p.size() - 1; i >= 0; --i) ans -= count(p[i].x, p[i].y); for (int i = p.size() - 1; i >= 0; --i) for (int j = i - 1; j >= 0; --j) ans += (long long)(abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y)) * 2; double Ans = (double)ans; Ans /= (double)(n * m - p.size()) * (n * m - p.size()); printf("%.10lf\n", Ans); return 0; }
8
/***************************************************************** > File Name: c.cpp > Author: Uncle_Sugar > Mail: [email protected] > Created Time: 2020/12/10 18:09:05 *****************************************************************/ # include <cstdio> # include <cstring> # include <cctype> # include <cmath> # include <cstdlib> # include <climits> # include <iostream> # include <iomanip> # include <set> # include <map> # include <vector> # include <stack> # include <queue> # include <algorithm> using namespace std; struct QuickIO{ QuickIO(){const int SZ = 1<<20; setvbuf(stdin ,new char[SZ],_IOFBF,SZ); setvbuf(stdout,new char[SZ],_IOFBF,SZ); } //*From programcaicai*// }QIO; const int debug = 1; const int size = 5000 + 10; const int INF = INT_MAX>>1; typedef long long ll; int main() { // std::ios::sync_with_stdio(false);cin.tie(0); int ncase; scanf("%d", &ncase); char str[1000000]; int cnt[2][200]; while (ncase--){ memset(cnt, 0, sizeof(cnt)); int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < 2; i++){ scanf("%s", str); for (int j = 0; j < n; j++){ cnt[i][str[j]]++; } } int flag = 1; for (int i = 'a'; i <= 'z'; i++){ if (cnt[0][i] == cnt[1][i]) continue; else if (cnt[0][i] < cnt[1][i]){ printf("No\n"); flag = 0; break; }else if ((cnt[0][i] - cnt[1][i])%k!=0){ printf("No\n"); flag = 0; break; }else { cnt[0][i+1] += cnt[0][i] - cnt[1][i]; cnt[0][i] = cnt[1][i]; } } if (flag) printf("Yes\n"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y); if (x > 1000000007) x %= 1000000007; } y = (y * y); if (y > 1000000007) y %= 1000000007; b /= 2; } return x; } int main() { long long int n, i, j, x, y; cin >> n; long long int a[n], b[n]; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { cin >> b[i]; } long long int maxi = 0; for (i = 0; i < n; i++) { x = 0; y = 0; for (j = i; j < n; j++) { x = x | a[j]; y = y | b[j]; maxi = max(maxi, x + y); } } cout << maxi << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 112345; int segx[4 * MAXN], segy[4 * MAXN], rs[2 * MAXN]; struct event { int x1, x2, y1, y2, t, last, id; }; vector<event> vx, vy; void updatex(int cur, int ini, int fim, int id, int val) { if (ini > id || fim < id) return; if (ini == fim) { segx[cur] = val; return; } int m = (ini + fim) / 2; int e = 2 * cur, d = 2 * cur + 1; updatex(e, ini, m, id, val); updatex(d, m + 1, fim, id, val); segx[cur] = min(segx[e], segx[d]); } int queryx(int cur, int ini, int fim, int a, int b) { if (ini > b || fim < a) return MAXN; if (ini >= a && fim <= b) return segx[cur]; int m = (ini + fim) / 2; int e = 2 * cur, d = 2 * cur + 1; return min(queryx(e, ini, m, a, b), queryx(d, m + 1, fim, a, b)); } void updatey(int cur, int ini, int fim, int id, int val) { if (ini > id || fim < id) return; if (ini == fim) { segy[cur] = val; return; } int m = (ini + fim) / 2; int e = 2 * cur, d = 2 * cur + 1; updatey(e, ini, m, id, val); updatey(d, m + 1, fim, id, val); segy[cur] = min(segy[e], segy[d]); } int queryy(int cur, int ini, int fim, int a, int b) { if (ini > b || fim < a) return MAXN; if (ini >= a && fim <= b) return segy[cur]; int m = (ini + fim) / 2; int e = 2 * cur, d = 2 * cur + 1; return min(queryy(e, ini, m, a, b), queryy(d, m + 1, fim, a, b)); } bool cmpx(event a, event b) { if (a.x1 == b.x1) { return a.t < b.t; } return a.x1 < b.x1; } bool cmpy(event a, event b) { if (a.y1 == b.y1) { return a.t < b.t; } return a.y1 < b.y1; } int n, m, k, q; int main() { scanf("%d %d %d %d", &n, &m, &k, &q); event e; for (int i = 0; i < k; i++) { int a, b; scanf("%d %d", &a, &b); e.x1 = a, e.x2 = a, e.y1 = b, e.y2 = b, e.t = 0; vx.push_back(e); vy.push_back(e); } for (int i = 0; i < q; i++) { int a, b, a1, b1; scanf("%d %d %d %d", &a, &b, &a1, &b1); e.x1 = a1; e.x2 = a1; e.y1 = b; e.y2 = b1; e.t = 1; e.last = a; e.id = i; vx.push_back(e); e.x1 = a; e.x2 = a1; e.y1 = b1; e.y2 = b1; e.t = 1; e.last = b; e.id = i; vy.push_back(e); } sort(vx.begin(), vx.end(), cmpx); sort(vy.begin(), vy.end(), cmpy); for (int i = 0; i < vx.size(); i++) { e = vx[i]; if (e.t == 0) { updatex(1, 1, MAXN, e.y1, e.x1); } else { int aux = queryx(1, 1, MAXN, e.y1, e.y2); if (aux >= e.last) rs[e.id] = 1; } } for (int i = 0; i < vy.size(); i++) { e = vy[i]; if (e.t == 0) { updatey(1, 1, MAXN, e.x1, e.y1); } else { int aux = queryy(1, 1, MAXN, e.x1, e.x2); if (aux >= e.last) rs[e.id] = 1; } } for (int i = 0; i < q; i++) { if (rs[i] == 1) printf("YES\n"); else printf("NO\n"); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; double n, a, d; int main() { cin >> n >> a >> d; int x, y; double t, p, last; for (int i = 0; i < n; ++i) { scanf("%d%d", &x, &y); t = sqrt(2 * d / a); if (a * t >= y) { t = y / a; p = d - a * t * t / 2; t += p / y; } t += x; if (t <= last) t = last; printf("%lf\n", t); last = t; } return 0; }
4
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:336777216") using namespace std; const long long MOD = 1e9; const int INF = 0x7fffffff; const long long LLINF = 0x7fffffffffffffff; const double PI = acos(-1.0); const double eps = 1e-10; int v[200005]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &v[i]); v[i] += i; } sort(v + 1, v + 1 + n); for (int i = 2; i <= n; i++) { if (v[i] == v[i - 1]) { return !printf(":("); } } for (int i = 1; i <= n; i++) printf("%d ", v[i] - i); }
7
#include <bits/stdc++.h> using namespace std; int grid[500][500]; int srow[500][500]; int scircle[500][500]; int rmaxcircle[500][500 + 1]; long long cmaxcircle[500][500 + 1]; int width[((500 - 1) / 2) + 1]; int separation[2 * ((500 - 1) / 2) + 1]; int partial_row_sum(int row, int left, int right) { return srow[row][right + 1] - srow[row][left]; } int solve_problem() { int n, m, r; if (scanf("%d %d %d", &n, &m, &r) != 3) return 1; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (scanf("%d", &grid[i][j]) != 1) return 1; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) srow[i][j + 1] = srow[i][j] + grid[i][j]; width[0] = r; for (int i = 1; i <= r; i++) { int w = width[i - 1]; while (i * i + w * w > r * r) --w; width[i] = w; } for (int i = 0; i <= 2 * r; i++) { int limit = min(i / 2, r); for (int j = max(0, i - r); j <= limit; j++) { int cand = width[j] + width[i - j] + 1; if (cand > separation[i]) separation[i] = cand; } } for (int i = r; i < n - r; i++) for (int j = r; j < m - r; j++) for (int k = 0; k <= r; k++) { scircle[i][j] += partial_row_sum(i + k, j - width[k], j + width[k]); if (k > 0) scircle[i][j] += partial_row_sum(i - k, j - width[k], j + width[k]); } for (int i = r; i < n - r; i++) for (int j = m - r - 1; j >= r; j--) if (scircle[i][j] <= rmaxcircle[i][j + 1]) { rmaxcircle[i][j] = rmaxcircle[i][j + 1]; cmaxcircle[i][j] = cmaxcircle[i][j + 1]; if (rmaxcircle[i][j] == scircle[i][j]) ++cmaxcircle[i][j]; } else { rmaxcircle[i][j] = scircle[i][j]; cmaxcircle[i][j] = 1; } int result = 0; long long mult = 0; for (int i = r; i < n - r; i++) for (int j = i + 2 * r + 1; j < n - r; j++) { int cand = rmaxcircle[i][r] + rmaxcircle[j][r]; if (cand > result) { result = cand; mult = cmaxcircle[i][r] * cmaxcircle[j][r]; } else if (cand == result) { mult += cmaxcircle[i][r] * cmaxcircle[j][r]; } } for (int i = r; i < n - r; i++) for (int j = r; j < m - r; j++) for (int k = max(i - 2 * r, r); k < min(i + 2 * r + 1, n - r); k++) { int delta = k - i; if (delta < 0) delta = -delta; int l = j + separation[delta]; if (l < m - r) { int cand = scircle[i][j] + rmaxcircle[k][l]; if (cand > result) { result = cand; mult = cmaxcircle[k][l]; } else if (cand == result) { mult += cmaxcircle[k][l]; } } } cout << result << ' ' << mult << endl; return 0; } int main() { solve_problem(); return 0; }
8
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll s, b, a, a2; cin >> s >> b; vector<ll> v(s); for (int i = 0; i < s; ++i) cin >> v[i]; vector<pair<ll, ll>> v2; for (int i = 0; i < b; ++i) { cin >> a >> a2; v2.push_back({a, a2}); } sort(begin(v2), end(v2)); for (int i = 1; i <= b - 1; ++i) { v2[i].second += v2[i - 1].second; } for (int i = 0; i < s; ++i) { ll c = v[i]; ll beg = 0; ll end = b; if (c < v2[0].first) { cout << "0 "; } else { while (beg + 1 < end) { ll mid = (end + beg) / 2; if (v2[mid].first <= c) beg = mid; else end = mid; } cout << v2[beg].second << " "; } } cout << endl; }
3
#include <bits/stdc++.h> using namespace std; void solve() { int a, b, c; cin >> a >> b >> c; cout << min(a, b + c) + min(c, a + b) + min(b, a + c) << endl; } int main() { { solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 6e5 + 4; int pre[N], arr[N], l[N], r[N]; map<int, int> m; int main() { int n; scanf("%d", &n); int counter = 1; for (int i = 1; i < n + 1; i++) { scanf("%d", &l[i]); scanf("%d", &r[i]); m[l[i] - 1] = 0; m[l[i]] = 0; m[r[i]] = 0; } for (auto &it : m) if (it.second == 0) it.second = counter++; for (int i = 1; i < n + 1; i++) { l[i] = m[l[i]]; r[i] = m[r[i]]; arr[l[i]]++; arr[r[i] + 1]--; } for (int i = 1; i < N; i++) arr[i] += arr[i - 1]; for (int i = 1; i < N; i++) { if (arr[i] == 1) pre[i] = i; else pre[i] = pre[i - 1]; } for (int i = 1; i < n + 1; i++) { if (pre[r[i]] < l[i]) { printf("%d", i); return 0; } } printf("%d", -1); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 3; long long n; long long pow2(long long x) { long long ans = 1; while (x--) ans *= 2; return ans; } int main() { cin >> n; if (n % 2 != 0) cout << 0; else cout << pow2(n / 2); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int dist(pair<long long int, long long int> a, pair<long long int, long long int> b) { return abs(a.first - b.first) + abs(a.second - b.second); } void putPoints(pair<long long int, long long int> a, pair<long long int, long long int> b, set<pair<long long int, long long int> > &sol) { long long int dx, dy; if (a.first < b.first) dx = 1; else dx = -1; if (a.second < b.second) dy = 1; else dy = -1; pair<long long int, long long int> curr = {a.first, a.second}; while (curr.first != b.first) { sol.insert({curr.first, curr.second}); curr.first += dx; } sol.insert({curr.first, curr.second}); while (curr.second != b.second) { sol.insert({curr.first, curr.second}); curr.second += dy; } sol.insert({curr.first, curr.second}); } void solve() { pair<long long int, long long int> pt[3]; long long int minx = 1e9, maxx = -1e9, miny = 1e9, maxy = -1e9; for (__typeof(3) i = (0) - ((0) > (3)); i != (3) - ((0) > (3)); i += 1 - 2 * ((0) > (3))) { cin >> pt[i].first >> pt[i].second; minx = min(minx, pt[i].first); miny = min(miny, pt[i].second); maxx = max(maxx, pt[i].first); maxy = max(maxy, pt[i].second); } long long int ans = 1e9; set<pair<long long int, long long int> > sol; for (long long int i = minx; i <= maxx; i++) { for (long long int j = miny; j <= maxy; j++) { pair<long long int, long long int> temp = {i, j}; if (dist(temp, pt[0]) + dist(temp, pt[1]) != dist(pt[1], pt[0])) continue; if (dist(temp, pt[0]) + dist(temp, pt[2]) != dist(pt[2], pt[0])) continue; if (dist(temp, pt[1]) + dist(temp, pt[2]) != dist(pt[1], pt[2])) continue; long long int loc = dist(temp, pt[0]) + dist(temp, pt[1]) + dist(temp, pt[2]) + 1; if (loc < ans) { ans = loc; sol.clear(); putPoints(temp, pt[0], sol); putPoints(temp, pt[1], sol); putPoints(temp, pt[2], sol); } } } cout << ans << endl; for (auto p : sol) { cout << p.first << " " << p.second << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; t = 1; while (t--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; long long ar[300005]; long long n, m; bool chk(long long k) { long long lst = 0; for (int i = 0; i < n; i++) { if (ar[i] < lst) { if (lst - ar[i] > k) { return false; } } else { if (m - ar[i] + lst <= k) { continue; } else { lst = ar[i]; } } } return true; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> ar[i]; } long long lo = 0, hi = m, mid; while (lo < hi) { mid = (lo + hi) / 2; if (chk(mid)) { hi = mid; } else { lo = mid + 1; } } cout << lo << endl; return 0; }
4
#include <bits/stdc++.h> int main() { int sign = 1; int current = 0; int value = 0; for (char c; '\n' != (c = getchar());) { if (c == '+' or c == '-') { value += sign * current; sign = (c == '+' ? 1 : -1); current = 0; } current *= 10; current += c - '0'; } printf("%d", value + sign * current); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; char str[100000]; while (scanf("%d", &n) == 1) { for (int i = 0; i < n; i++) { if (i % 4 == 0) { cout << "a"; } else if (i % 4 == 1) { cout << "b"; } else if (i % 4 == 2) { cout << "c"; } else if (i % 4 == 3) { cout << "d"; } } cout << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int i, n, r, a[2048]; int main() { cin >> n; n = (1 << (n + 1)) - 1; for (i = 2; i <= n; i++) cin >> a[i]; for (; n != 1; n -= 2) a[n / 2] += max(a[n], a[n - 1]), r += abs(a[n] - a[n - 1]); cout << r << endl; }
3
#include <bits/stdc++.h> using namespace std; void printArray(vector<long long> arr, long long size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << '\n'; } int main() { clock_t t_r; t_r = clock(); ; ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; bool done = false; for (int i = 1; i < n; i++) { if (a[i] != a[i - 1]) { done = true; cout << 1 << "\n"; break; } } if (!done) cout << n << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int t, n, sl[100005]; int main() { cin >> t; while (t--) { cin >> n; memset(sl, 0, sizeof sl); for (int i = 1; i <= n; i++) { int x; cin >> x; if (x <= 2048) sl[x]++; } for (int i = 1; i <= 1024; i *= 2) { sl[i * 2] += sl[i] / 2; } if (sl[2048]) cout << "YES" << '\n'; else cout << "NO" << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int dp1[1005][1005], dp2[1005][1005], dp3[1005][1005], dp4[1005][1005], a[1005][1005]; int main() { int n, m, i, j, x = 0; cin >> n >> m; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) cin >> a[i][j]; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) dp1[i][j] = a[i][j] + max(dp1[i - 1][j], dp1[i][j - 1]); for (i = n; i >= 1; i--) for (j = m; j >= 1; j--) dp2[i][j] = a[i][j] + max(dp2[i + 1][j], dp2[i][j + 1]); for (i = n; i >= 1; i--) for (j = 1; j <= m; j++) dp3[i][j] = a[i][j] + max(dp3[i + 1][j], dp3[i][j - 1]); for (i = 1; i <= n; i++) for (j = m; j >= 1; j--) dp4[i][j] = a[i][j] + max(dp4[i - 1][j], dp4[i][j + 1]); for (i = 2; i < n; i++) { for (j = 2; j < m; j++) { x = max(x, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]); x = max(x, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]); } } cout << x << endl; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 205; const int INF = 100000; int n; int a[maxn]; int main() { int t; cin >> t; for (int i = 1; i <= t; i++) { cin >> n; int Max = 0; for (int i = 0; i < n; i++) { cin >> a[i]; Max = max(Max, a[i]); } int d = Max - 1; int flag = 1; for (int i = 1; i < n; i++) { int x = a[i] - a[i - 1]; if (x != 1 && x != -1 && x != d && x != -d) { flag = 0; break; } } if (flag == 0) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; const int Log = 18; int n, h[N], p[Log][N]; vector<int> g[N], ans; void pre() { for (int i = 1; i < Log; i++) for (int j = 0; j < n; j++) if (p[i - 1][j] != -1) p[i][j] = p[i - 1][p[i - 1][j]]; } void dfs(int v, int dep) { for (auto x : g[v]) { h[x] = h[v] + 1; p[0][x] = v; dfs(x, dep + 1); } } int lca(int u, int v) { if (h[u] > h[v]) swap(u, v); for (int i = Log - 1; i > -1; i--) if (p[i][v] != -1 && h[p[i][v]] >= h[u]) v = p[i][v]; if (u == v) return v; for (int i = Log - 1; i > -1; i--) if (p[i][u] != -1 && p[i][u] != p[i][v]) u = p[i][u], v = p[i][v]; return p[0][v]; } pair<long long, long long> a[N]; long long ri[N]; vector<long long> f, s; void ok() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%I64d %I64d", &a[i].first, &a[i].second); f.push_back(n - 1); s.push_back(n - 1); ri[n - 1] = -1; for (int i = n - 2; i > -1; i--) { long long ss = f.size(); while (f.size() > 1 && (a[i].second - a[f[ss - 2]].second) * (-a[f[ss - 1]].first + a[f[ss - 2]].first) > (a[f[ss - 1]].second - a[f[ss - 2]].second) * (-a[i].first + a[f[ss - 2]].first)) f.pop_back(), ss--; ss = s.size(); while (ss > 1 && (a[i].second - a[s[ss - 2]].second) * (-a[s[ss - 1]].first + a[s[ss - 2]].first) > (a[s[ss - 1]].second - a[s[ss - 2]].second) * (-a[i].first + a[s[ss - 2]].first)) s.pop_back(), ss--; ri[i] = max(s.back(), f.back()); f.push_back(i); s.push_back(i); } for (int i = 0; i < n - 1; i++) g[ri[i]].push_back(i); } int main() { memset(p, -1, sizeof(p)); ok(); dfs(n - 1, 0); pre(); int q; scanf("%d", &q); while (q--) { int a, b; scanf("%d %d", &a, &b); ans.push_back(lca(--a, --b) + 1); } for (auto x : ans) printf("%d ", x); }
7
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long par[N], st[N], en[N], ind[N]; long long find(long long x) { if (x == par[x]) return x; return par[x] = find(par[x]); } bool merge(long long x, long long y) { long long fx = find(x), fy = find(y); if (fx != fy) { par[fy] = fx; return 1; } return 0; } int main() { set<pair<long long, long long> > sp; long long n; scanf("%lld", &n); for (long long i = 1; i <= n; ++i) { long long x, y; par[i] = i; scanf("%lld%lld", &x, &y); st[x] = y; ind[x] = i; en[y] = i; } for (long long i = 1; i <= N - 1; ++i) { long long j = st[i]; if (en[i]) sp.erase(make_pair(i, en[i])); if (!j) continue; long long idx = ind[i]; for (auto op : sp) { long long ni, nj = op.first, nidx = op.second; if (nj > j) break; bool fl = merge(idx, nidx); if (!fl) { printf("NO\n"); return 0; } } sp.insert(make_pair(j, idx)); } set<long long> fin; for (long long i = 1; i <= n; ++i) fin.insert(find(i)); long long sz = fin.size(); if (sz == 1) printf("YES\n"); else printf("NO\n"); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> fortune; int counter_even = 0, counter_odd = 0; int ans = 0; for (int i = 0; i < n; i++) { int j; cin >> j; if (j % 2) counter_odd++; else counter_even++; fortune.push_back(j); } if (counter_odd == 0) { cout << "0" << endl; return 0; } else { sort(fortune.begin(), fortune.end()); int odd = 0; for (int i = 0; i < n; i++) { if (fortune[i] % 2) { odd = fortune[i]; break; } } for (int i = (n - 1); i >= 0; i--) ans += fortune[i]; if (!(ans % 2)) ans -= odd; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int mxn = 1e5 + 10; int n, sz[mxn], bigchild[mxn]; char ans[mxn]; bool been[mxn]; vector<int> adj[mxn]; void dfs(int u, int p = -1) { sz[u] = 1; int ind = -1; for (int i : adj[u]) if (!been[i] && i != p) { dfs(i, u); if (ind == -1 || sz[ind] < sz[i]) ind = i; sz[u] += sz[i]; } bigchild[u] = ind; } void solve(int v, char c = 'A') { dfs(v); int N = sz[v]; while (bigchild[v] != -1 && 2 * sz[bigchild[v]] > N) v = bigchild[v]; ans[v] = c++; been[v] = true; for (int i : adj[v]) if (!been[i]) solve(i, c); } int main() { ios::sync_with_stdio(false), cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } solve(1); for (int i = 1; i <= n; i++) cout << ans[i] << " "; return 0; }
6
#include <bits/stdc++.h> using namespace std; inline int Get_Int() { int Num = 0, Flag = 1; char ch; do { ch = getchar(); if (ch == '-') Flag = -Flag; } while (ch < '0' || ch > '9'); do { Num = Num * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9'); return Num * Flag; } int N, Ans, Win; int P[5005], F[5005]; char S[5005]; inline int Mex(vector<int> &A) { int Min = 0; while (true) { bool Flag = true; for (typeof(A.begin()) it = A.begin(); it != A.end(); ++it) if (*it == Min) { Flag = false; ++Min; break; } if (Flag) return Min; } } int Solve(int Left, int Right) { int Ans = 0, Total = 0; for (int i = Left + 1; i < Right; ++i) if (P[i] > 1) ++Total; else { Ans ^= F[Total + 2]; Total = 0; } Ans ^= F[Total + 2]; return Ans; } int main() { scanf("%s", S + 1); N = strlen(S + 1); S[0] = '$'; S[N + 1] = '#'; for (int i = 3; i <= N; ++i) { vector<int> temp; for (int j = 2; j < i; ++j) temp.push_back(F[j - 1] ^ F[i - j]); F[i] = Mex(temp); } for (int i = 1; i <= N; ++i) while (S[i - P[i]] == S[i + P[i]]) ++P[i]; for (int i = 1; i <= N; ++i) if (P[i] > 1) if (Solve(1, i - 1) == Solve(i + 1, N)) { Win = true; Ans = i; break; } if (Win) cout << "First" << endl << Ans << endl; else cout << "Second" << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int L, B, F; int N; list<int> ini, fin, id; list<int>::iterator a, b, c; int r, t; int x; scanf("%d%d%d", &L, &B, &F); ini.push_back(-B); ini.push_back(L + F); fin.push_back(-B); fin.push_back(L + F); id.push_back(-1); id.push_back(-1); scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d%d", &r, &t); if (r == 1) { a = fin.begin(); b = ini.begin(); c = id.begin(); b++, c++; for (; b != ini.end(); a++, b++, c++) { if ((*b) - (*a) >= B + F + t) break; } if (b != ini.end()) { x = *a; a++; ini.insert(b, x + B); fin.insert(a, x + B + t); id.insert(c, i); printf("%d\n", x + B); } else printf("-1\n"); } else { a = ini.begin(); b = fin.begin(); c = id.begin(); for (; b != fin.end(); a++, b++, c++) { if (*c == t) { ini.erase(a); fin.erase(b); id.erase(c); break; } } } } return 0; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:2759095000") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native") using namespace std; long long gcd(long long i, long long j) { if (j == 0) return i; else return gcd(j, i % j); } template <typename T> inline T getint() { T val = 0; char c; bool neg = false; while ((c = getchar()) && !(c >= '0' && c <= '9')) { neg |= c == '-'; } do { val = (val * 10) + c - '0'; } while ((c = getchar()) && (c >= '0' && c <= '9')); return val * (neg ? -1 : 1); } const long long INF = 1e9; const int mod = 1000000007; const long double eps = 1e-9, pi = acos(-1); const int maxN = 500000, maxT = 2001; vector<pair<int, int>> G[maxN]; int b[maxN]; int w[maxN]; int dp[maxN]; int color[maxN]; int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cout.precision(15); int n, m; cin >> n >> m; fill(begin(b), end(b), INF); fill(begin(w), end(w), INF); fill(begin(dp), end(dp), INF); fill(begin(color), end(color), INF); b[n - 1] = 0; w[n - 1] = 0; dp[n - 1] = 0; priority_queue<pair<int, int>> q; q.push({0, n - 1}); for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; G[b].push_back({a, c}); } for (int i = 0; i < n - 1; ++i) q.push({-INF, i}); while (!q.empty()) { int p = q.top().second; q.pop(); if (color[p] != INF) continue; if (w[p] == dp[p]) color[p] = 1; else color[p] = 0; for (auto x : G[p]) { if (x.second == 1) { w[x.first] = min(w[x.first], dp[p] + 1); } else { b[x.first] = min(b[x.first], dp[p] + 1); } if (max(w[x.first], b[x.first]) < dp[x.first]) { dp[x.first] = max(w[x.first], b[x.first]); q.push({-dp[x.first], x.first}); } } } if (dp[0] == INF) cout << -1 << "\n"; else cout << dp[0] << "\n"; for (int i = 0; i < n; ++i) cout << color[i]; return 0; }
8
#include<bits/stdc++.h> using namespace std; int main(){ long long int p; cin>>p; while(p--){ int n; cin>>n; long long a[n],sum2=0,sum1=0; for(int i=0;i<n;i++){ cin>>a[i]; if(i%2!=0){ sum2+=a[i]; } else{ sum1+=a[i]; } } if(sum2>sum1){ for(int i=0;i<n;i++){ if(i%2==0){ cout<<1<<" "; } else{ cout<<a[i]<<" "; } } } else{ for(int i=0;i<n;i++){ if(i%2!=0){ cout<<1<<" "; } else{ cout<<a[i]<<" "; } } } cout<<endl; } return 0; }
3
#include <bits/stdc++.h> const int N = 100000 + 9; int n, n2; long double sum, ans; struct point { int x, y; point(const int _x, const int _y) : x(_x), y(_y) {} point() {} } p[N * 2]; inline long long cpr(const point &a, const point &b, const point &c) { return 1ll * (b.x - a.x) * (c.y - a.y) - 1ll * (c.x - a.x) * (b.y - a.y); } inline double sqr(const double x) { return x * x; } void work1() { sum = 0; int l = 1, r = 1, Max = -99999999; for (int i = 1; i <= n2; ++i) { if (p[i].x < p[l].x) l = i; if (p[i].x <= p[r].x) r = i; Max = std::max(Max, p[i].x); } if (p[l].x == p[l + 1].x) ++l; if (p[r].x == p[r - 1].x) --r; for (int i = p[l].x; i <= Max; ++i) { if (p[l + 1].x < i) ++l; if (p[r - 1].x < i) --r; int bot = std::ceil((p[l + 1].y - p[l].y) * 1.0 / (p[l + 1].x - p[l].x) * (i - p[l].x) + p[l].y); int top = std::floor((p[r - 1].y - p[r].y) * 1.0 / (p[r - 1].x - p[r].x) * (i - p[r].x) + p[r].y); sum += (top - bot + 1); } } void work() { int l = 1, r = 1, Max = -99999999; for (int i = 1; i <= n2; ++i) { if (p[i].x < p[l].x) l = i; if (p[i].x <= p[r].x) r = i; Max = std::max(Max, p[i].x); } if (p[l].x == p[l + 1].x) ++l; if (p[r].x == p[r - 1].x) --r; long double sum1 = 0, sum2 = 0, sum3 = 0; for (int i = p[l].x; i <= Max; ++i) { if (p[l + 1].x < i) ++l; if (p[r - 1].x < i) --r; int bot = std::ceil((p[l + 1].y - p[l].y) * 1.0 / (p[l + 1].x - p[l].x) * (i - p[l].x) + p[l].y); int top = std::floor((p[r - 1].y - p[r].y) * 1.0 / (p[r - 1].x - p[r].x) * (i - p[r].x) + p[r].y); sum1 += sum2; sum2 += sum3 * 2 + (top - bot + 1); sum3 += (top - bot + 1); ans += (top - bot + 1) / (sum * (sum - 1)) * sum1; } } int main() { scanf("%d", &n); n2 = 2 * n; for (int i = 1; i <= n; ++i) scanf("%d%d", &p[i].x, &p[i].y); p[n + 1] = p[1]; long long S = 0; for (int i = 1; i <= n; ++i) S += cpr(point(0, 0), p[i], p[i + 1]); if (S < 0) std::reverse(p + 1, p + 1 + n); for (int i = 1; i <= n; ++i) { p[i + n] = p[i]; } work1(); work(); for (int i = 1; i <= n2; ++i) { std::swap(p[i].x, p[i].y); p[i].x = -p[i].x; } work(); printf("%.8f\n", (double)(ans)); }
9
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> m >> n; vector<pair<int, int> > p; int ans[m]; for (int i = 0; i < m; ++i) { ans[i] = -1; } int s[n], d[n], c[n]; for (int i = 0; i < n; ++i) { cin >> s[i] >> d[i] >> c[i]; p.push_back(make_pair(d[i], i)); ans[d[i] - 1] = n + 1; } sort(p.begin(), p.end()); for (int index = 0; index < n; ++index) { int i = p[index].second; for (int j = s[i] - 1; j < d[i]; ++j) { if (ans[j] == -1) { ans[j] = i + 1; --c[i]; } if (c[i] == 0) break; } if (c[i] > 0) { cout << "-1"; return 0; } } for (int i = 0; i < m; ++i) { if (ans[i] == -1) ans[i] = 0; } for (int i = 0; i < m; ++i) { cout << ans[i] << " "; } return 0; }
4