func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!(ch >= 0 && ch <= 9 )) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + (ch - 0 ); ch = getchar(); } return x * f; } void chkmin(int &a, int b) { if (b < a) a = b; } const int N = 81; const int INF = 88888888; int n; int dp[N][N][N][3]; char s[N]; vector<int> V, K, O; int calc(int pos, int a, int b, int c) { int ret = 0; for (int i = 1; i <= pos - 1; i++) { if (s[i] == V ) { if (a) a--; else ++ret; } if (s[i] == K ) { if (b) b--; else ++ret; } if (s[i] != V && s[i] != K ) { if (c) c--; else ++ret; } } return ret; } int main() { n = read(); scanf( %s , s + 1); for (int i = 1; i <= n; i++) { if (s[i] == V ) V.push_back(i); else if (s[i] == K ) K.push_back(i); else O.push_back(i); } for (int a = 0; a <= n; a++) for (int b = 0; b <= n; b++) for (int c = 0; c <= n; c++) for (int d = 0; d <= 1; d++) dp[a][b][c][d] = INF; dp[0][0][0][0] = 0; for (int a = 0; a <= V.size(); a++) { for (int b = 0; b <= K.size(); b++) { for (int c = 0; c <= O.size(); c++) { for (int flag = 0; flag <= 1; flag++) { if (a < V.size()) chkmin(dp[a + 1][b][c][1], dp[a][b][c][flag] + calc(V[a], a, b, c)); if (b < K.size() && !flag) chkmin(dp[a][b + 1][c][0], dp[a][b][c][flag] + calc(K[b], a, b, c)); if (c < O.size()) chkmin(dp[a][b][c + 1][0], dp[a][b][c][flag] + calc(O[c], a, b, c)); } } } } printf( %d n , min(dp[V.size()][K.size()][O.size()][0], dp[V.size()][K.size()][O.size()][1])); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { int n, a[100005]; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; int MIN = INT_MAX, MAX = -100; for (int i = 0; i < n; ++i) { if (a[i] != -1 && ((i > 0 && a[i - 1] == -1) || (i < n - 1 && a[i + 1] == -1))) { MIN = min(MIN, a[i]); MAX = max(MAX, a[i]); } } if (MIN == INT_MAX && MAX == -100) cout << 0 << << 0; else { int ans = (MAX + MIN) / 2, d = 0; for (int i = 0; i < n - 1; ++i) { if (a[i] == -1 && a[i + 1] != -1) d = max(d, abs(ans - a[i + 1])); else if (a[i] != -1 && a[i + 1] != -1) d = max(d, abs(a[i] - a[i + 1])); else if (a[i] != -1 && a[i + 1] == -1) d = max(d, abs(a[i] - ans)); } cout << d << << ans; } cout << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; double sqr(double x) { return x * x; } double distsqr(double x, double y, double a, double b) { return sqr((x - a)) + sqr((y - b)); } double cosine_angle(double a, double b, double c) { return (double)(sqr(a) + sqr(b) - sqr(c)) / ((double)2.0 * a * b); } template <class T> T Maximum(T a, T b) { return a > b ? a : b; } template <class T> T Minimum(T a, T b) { return a < b ? a : b; } template <class T> T GCD(T a, T b) { if (!b) return a; return GCD(b, a % b); } template <class T> void SWAP(T &a, T &b) { T temp = a; a = b; b = temp; return; } template <class T> T BigMod(T a, T n, T mod) { if (!n) return (T)1 % mod; long long int power = BigMod(a, n >> 1, mod); power %= mod; power *= power; power %= mod; if (n & 1) power *= a % mod, power %= mod; return power; } template <class T> T ExtendedEuclid(T a, T b, T c) { if (!a) return 0; return (b * ExtendedEuclid(b % a, a, -c) + c) / a; } template <class T> T ModInverse(T a, T m) { return ((ExtendedEuclid(a, m, (T)GCD(a, m))) % (m) + (m)) % (m); } long long int valk[100010], valk_1[100010], cval[100010]; void Processsing(int n, int k) { int i; long long int power, temp; if (k > 0) valk_1[k - 1] = 1; for (i = k, power = 1; i < n; i++) { if (i == k) valk[i] = 1; else { valk[i] = valk[i - 1] * i; valk[i] %= 1000000007; valk[i] *= ModInverse((long long int)(i - k), (long long int)1000000007); valk[i] %= 1000000007; } valk_1[i] = valk_1[i - 1] * i; valk_1[i] %= 1000000007; valk_1[i] *= ModInverse((long long int)(i - k + 1), (long long int)1000000007); valk_1[i] %= 1000000007; } for (i = 0; i < n - k; i++) { if (i) power *= 10; else power = 1; power %= 1000000007; temp = valk_1[n - 2 - i] * power; temp %= 1000000007; if (i) cval[i] = cval[i - 1] + temp; else cval[i] = temp; cval[i] %= 1000000007; } } int main() { int i, n, k; char c; long long int counts, temp; scanf( %d %d , &n, &k); Processsing(n, k); for (i = counts = 0; i < n; i++) { scanf( %c , &c); c -= 0 ; if (i < k) counts += cval[n - k - 1] * (int)c; else { temp = cval[n - 2 - i] + valk[i] * BigMod((long long int)10, (long long int)(n - 1 - i), (long long int)1000000007); temp %= 1000000007; temp *= (int)c; temp %= 1000000007; counts += temp; } counts %= 1000000007; } printf( %I64d n , counts); return 0; }
|
#include <bits/stdc++.h> template <typename T> extern inline void SWAP(T &a, T &b) { T t = a; a = b, b = t; } const int MAXN = 500005; const int MAXBIT = 20; const int MEMBIT = MAXBIT + 5; int bas[MEMBIT], pos[MEMBIT], n, rt, a[MAXN], q; struct quest { int lt, rt, id; bool operator<(const quest &tmp) const { return rt < tmp.rt; } } Q[MAXN]; void insert(int x, int id) { for (register int i = (MAXBIT); i >= (0); --i) { if (x & (1 << i)) { if (!bas[i]) { bas[i] = x, pos[i] = id; return; } if (pos[i] < id) SWAP(pos[i], id), SWAP(x, bas[i]); x ^= bas[i]; } } } int work(int lt) { int ans = 0; for (register int i = (MAXBIT); i >= (0); --i) if (pos[i] >= lt && ans < (ans ^ bas[i])) ans ^= bas[i]; return ans; } int ans[MAXN]; int main() { scanf( %d , &n); for (register int i = (1); i <= (n); ++i) scanf( %d , &a[i]); scanf( %d , &q); for (register int i = (1); i <= (q); ++i) scanf( %d%d , &Q[i].lt, &Q[i].rt), Q[i].id = i; std::sort(Q + 1, Q + 1 + q); for (register int i = (1); i <= (q); ++i) { while (rt < Q[i].rt) { ++rt; insert(a[rt], rt); } ans[Q[i].id] = work(Q[i].lt); } for (register int i = (1); i <= (q); ++i) printf( %d n , ans[i]); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<string> s(n); for (int i = 0; i < n; ++i) cin >> s[i]; int answer = 0; for (int col = 0; col < (int)s[0].size(); ++col) { bool is_sorted = true; for (int i = 0; i < n - 1; ++i) { if (s[i].substr(0, col + 1) > s[i + 1].substr(0, col + 1)) { is_sorted = false; } } if (!is_sorted) { ++answer; for (int i = 0; i < n; ++i) { string new_string; for (int j = 0; j < (int)s[i].size(); ++j) { if (j != col) { new_string += s[i][j]; } } s[i] = new_string; } --col; } } cout << answer << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n + 1, 0); vector<vector<int> > g(n + 1); int i, j, k; for (i = 1; i <= n; i++) { cin >> j >> k; v[i] = k; if (j != -1) { g[j].push_back(i); } } vector<int> ans; for (i = 1; i <= n; i++) { if (v[i] == 1) { int kk = 1; for (j = 0; j < g[i].size(); j++) { if (v[g[i][j]] == 0) kk = 0; } if (kk) ans.push_back(i); } } sort(ans.begin(), ans.end()); for (i = 0; i < ans.size(); i++) cout << ans[i] << ; if (ans.size() == 0) cout << -1; }
|
#include <bits/stdc++.h> using namespace std; int n, t; int main() { cin >> n >> t; if (n == 1 && t == 10) { cout << -1; return 0; } if (t == 2) { for (int i = 0; i < n - 1; i++) { cout << 9; } cout << 2; return 0; } if (t == 3) { for (int i = 0; i < n - 1; i++) { cout << 9; } cout << 3; return 0; } if (t == 4) { for (int i = 0; i < n - 1; i++) { cout << 4; } cout << 4; return 0; } if (t == 5) { for (int i = 0; i < n - 1; i++) { cout << 9; } cout << 5; return 0; } if (t == 6) { for (int i = 0; i < n - 1; i++) { cout << 9; } cout << 6; return 0; } if (t == 7) { for (int i = 0; i < n - 1; i++) { cout << 7; } cout << 7; return 0; } if (t == 8) { for (int i = 0; i < n - 1; i++) { cout << 8; } cout << 8; return 0; } if (t == 9) { for (int i = 0; i < n - 1; i++) { cout << 9; } cout << 9; return 0; } if (t == 10) { for (int i = 0; i < n - 1; i++) { cout << 9; } cout << 0; return 0; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265358979323846; long long MOD = 1e9 + 7; const char nl = n ; const long long inf = 1e15; long long power(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2) z = z * x; x = x * x; y /= 2; } return z; } long long gcd(long long a, long long b) { if (a < b) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } long long sq(long long a) { long long ans = (1ll * a * a); return ans; } bool isprime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long digitsum(long long x) { long long ans = 0; while (x > 0) { ans += (x % 10); x /= 10; } return ans; } void solve() { long long m; long long n, d; cin >> n >> d >> m; long long a[n]; long long i, j; vector<long long> large; vector<long long> small; for (long long i = 0; i < n; i++) { cin >> a[i]; if (a[i] <= m) { small.push_back(a[i]); } else { large.push_back(a[i]); } } long long ans = 0; sort(large.begin(), large.end(), greater<int>()); sort(small.begin(), small.end(), greater<int>()); long long temp = 0; for (i = 0; i < small.size(); i++) { temp += small[i]; } ans = max(ans, temp); for (i = 1; i <= large.size(); i++) { temp = 0; if (n - 1 - (i - 1) * (d + 1) >= 0) { long long x = small.size(); for (j = 0; j < min(n - 1 - (i - 1) * (d + 1), x); j++) { temp += small[j]; } temp += large[0]; for (j = 1; j <= i - 1; j++) { temp += large[j]; } } ans = max(ans, temp); } cout << ans << n ; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long TC = 1; while (TC--) { solve(); } }
|
#include <bits/stdc++.h> using namespace std; int n, s, K, r[55], f[55][2555]; char col[55]; pair<int, int> p[55]; inline void Umn(int &x, int y) { x = min(x, y); } int main() { scanf( %d%d%d , &n, &s, &K); for (int i = 1; i <= n; ++i) scanf( %d , &r[i]); scanf( %s , col + 1), col[0] = & ; for (int i = 1; i <= n; ++i) p[i] = (pair<int, int>){r[i], i}; sort(p + 1, p + n + 1); memset(f, 0x3f, sizeof f); f[0][0] = 0; p[0].second = s; int ans = 1e9; for (int i = 1; i <= n; ++i) { for (int j = p[i].first; j <= 2500; ++j) { for (int k = 0; k < i; ++k) if (p[k].first < p[i].first && (k == 0 || col[p[k].second] != col[p[i].second])) Umn(f[i][j], f[k][j - p[i].first] + abs(p[i].second - p[k].second)); if (j >= K) Umn(ans, f[i][j]); } } if (ans > 1e8) return puts( -1 ), 0; cout << ans << endl; }
|
#include <bits/stdc++.h> using namespace std; mt19937 rnd(51); long long ask(long long a, long long b, long long c) { cout << ? << a << << b << << c << endl; long long p; cin >> p; return p; } signed main() { long long t; cin >> t; while (t--) { long long n, bad = 0, good = 0; cin >> n; vector<long long> type(n + 1); for (long long i = 1; i <= n; i += 3) { type[i] = ask(i, i + 1, i + 2); } for (long long i = 1; i <= n; i += 3) { if (type[i] != type[i + 3]) { type[i + 1] = ask(i + 1, i + 2, i + 3), type[i + 2] = ask(i + 2, i + 3, i + 4); for (long long j = i; j < i + 3; j++) { if (type[j] != type[j + 1]) { if (type[j] == 0) { bad = j, good = j + 3; } else { bad = j + 3, good = j; } } } break; } } vector<long long> arr{bad}; for (long long i = 1; i <= n; i += 3) { if ((i - 1) / 3 == (bad - 1) / 3 || (i - 1) / 3 == (good - 1) / 3) { for (long long j = i; j < i + 3; j++) { if (bad == j || good == j) { continue; } if (ask(good, bad, j) == 0) { arr.emplace_back(j); } } } else if (type[i] == 0) { if (ask(i, i + 1, good) == 0) { arr.emplace_back(i); arr.emplace_back(i + 1); if (ask(i + 2, good, bad) == 0) arr.emplace_back(i + 2); } else { arr.emplace_back(i + 2); if (ask(i, good, bad) == 0) arr.emplace_back(i); else arr.emplace_back(i + 1); } } else { if (ask(i, i + 1, bad) == 1) { if (ask(i + 2, good, bad) == 0) arr.emplace_back(i + 2); } else { if (ask(i, good, bad) == 0) arr.emplace_back(i); else arr.emplace_back(i + 1); } } } cout << ! << arr.size() << ; for (auto to : arr) cout << to << ; cout << endl; } return 0; }
|
#include <bits/stdc++.h> long long int p = 1e9 + 7; long long int power(long long int x, long long int y) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { long long int n, k; scanf( %lld%lld , &n, &k); if (k == 0 || n == 0) { printf( %lld n , (2 % p * n % p) % p); return 0; } long long int x = power(2, k + 1); long long int g = power(2, k); long long int a1 = ((n) % p * g % p) % p; long long int a2 = ((n - 1) % p * g % p) % p; long long int num1 = (a1 % p * (a1 + 1) % p) % p; long long int num2 = (a2 % p * (a2 + 1) % p) % p; long long int num = (num1 - num2 + p) % p; long long int ans1 = power(g, p - 2); long long int ans = (num % p * ans1 % p) % p; printf( %lld n , ans); }
|
#include <bits/stdc++.h> using namespace std; struct event { int t, l, r, v; }; void solve() { int n, q; cin >> n >> q; vector<event> e(q); vector<int64_t> a(n, INT_MAX), upd(n, 0); for (int i = 0; i < q; ++i) { cin >> e[i].t; cin >> e[i].l; cin >> e[i].r; cin >> e[i].v; --e[i].l; --e[i].r; if (e[i].t == 1) { for (int j = e[i].l; j < e[i].r + 1; ++j) { upd[j] += e[i].v; } } } for (int i = q - 1; i >= 0; --i) { if (e[i].t == 2) { for (int j = e[i].l; j < e[i].r + 1; ++j) { a[j] = min(a[j], e[i].v - upd[j]); } } else { for (int j = e[i].l; j < e[i].r + 1; ++j) { upd[j] -= e[i].v; } } } vector<int64_t> tmp(a); for (int i = 0; i < q; ++i) { if (e[i].t == 1) { for (int j = e[i].l; j < e[i].r + 1; ++j) { tmp[j] += e[i].v; } } else { int64_t here = INT_MIN; for (int j = e[i].l; j < e[i].r + 1; ++j) { here = max(here, tmp[j]); } if (here != e[i].v) { cout << NO n ; return; } } } cout << YES n ; for (int64_t i : a) { if (i == INT_MAX) cout << rand() % 1000 << ; else cout << i << ; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } }
|
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; int main() { int n; cin >> n; string a, b; cin >> a >> b; int x[n], y[n]; for (int i = 0; i < n; i++) { x[i] = a[i] - 0 ; y[i] = b[i] - 0 ; } sort(x, x + n); sort(y, y + n); int px = 0, py = 0, sher = 0, mori = 0; vector<int> store; for (py = 0; py < n; py++) { if (x[px] > y[py]) { mori++; } else { px++; } } px = 0, py = 0; for (py = 0; py < n; py++) { if (x[px] < y[py]) { sher++; px++; } } cout << mori << endl << sher << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int t, n, num[10], tu[500], fir[10], flag[10]; char ss[1000005]; struct node { int v[5]; }; vector<node> p[100005]; map<long long, bool> ma[100005]; int main() { tu[ G ] = 1; tu[ H ] = 2; tu[ R ] = 3; tu[ S ] = 4; scanf( %d , &n); scanf( %s , &ss); for (int i = 0; i < n; i++) { ma[i].clear(); p[i].clear(); } if (ss[0] != ? ) { node no; memset(no.v, 0, sizeof(no.v)); no.v[tu[ss[0]]]++; p[0].push_back(no); } else { for (int i = 1; i <= 4; i++) { node no; memset(no.v, 0, sizeof(no.v)); no.v[i] = 1; p[0].push_back(no); } } for (int i = 1; i < n; i++) { for (vector<node>::iterator it = p[i - 1].begin(); it != p[i - 1].end(); it++) { node last = *it; if (ss[i] != ? ) { last.v[tu[ss[i]]]++; long long sum = 0; for (int k = 1; k <= 4; k++) sum = sum * 100000 + last.v[k]; if (!ma[i][sum]) { p[i].push_back(last); ma[i][sum] = 1; } continue; } int minv = 2000000000; for (int j = 1; j <= 4; j++) minv = min(minv, last.v[j]); for (int j = 1; j <= 4; j++) if (last.v[j] == minv) { node ne = last; ne.v[j]++; long long sum = 0; for (int k = 1; k <= 4; k++) sum = sum * 100000 + ne.v[k]; if (!ma[i][sum]) { p[i].push_back(ne); ma[i][sum] = 1; } } } } memset(flag, 0, sizeof(flag)); for (vector<node>::iterator it = p[n - 1].begin(); it != p[n - 1].end(); it++) { node no = *it; int minv = 2000000000; for (int i = 1; i <= 4; i++) minv = min(minv, no.v[i]); for (int i = 1; i <= 4; i++) if (minv == no.v[i]) flag[i] = 1; } for (int k = 1; k <= 4; k++) if (flag[k]) { if (k == 1) printf( Gryffindor n ); if (k == 2) printf( Hufflepuff n ); if (k == 3) printf( Ravenclaw n ); if (k == 4) printf( Slytherin n ); } printf( n ); }
|
#include <bits/stdc++.h> using namespace std; const int maxN = 1000100; const int Num = 999999; const int Mod = 1e9 + 7; int n; int Sm1[maxN], Sm2[maxN], Sm3[maxN]; void Plus(int &x, int y); void Minus(int &x, int y); int QPow(int x, int cnt); int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); ++Sm3[x]; } for (int i = 0; i <= Num; i++) Sm1[i] = 1ll * Sm3[i] * i % Mod, Sm2[i] = 1ll * Sm3[i] * i % Mod * i % Mod; for (int k = 8; k >= 0; k--) for (int i = 0, mul = 1; i <= 5; i++, mul = mul * 10) for (int j = 0; j <= Num; j++) if (j / mul % 10 == k) Plus(Sm1[j], Sm1[j + mul]), Plus(Sm2[j], Sm2[j + mul]), Plus(Sm3[j], Sm3[j + mul]); int inv2 = QPow(2, Mod - 2); for (int i = 0; i <= Num; i++) { if (!Sm3[i]) continue; int pw = QPow(2, Sm3[i] - 1); int k = (1ll * Sm1[i] * Sm1[i] % Mod + Sm2[i]) % Mod; Sm1[i] = 1ll * k * pw % Mod * inv2 % Mod; } for (int k = 0; k <= 8; k++) for (int i = 0, mul = 1; i <= 5; i++, mul = mul * 10) for (int j = 0; j <= Num; j++) if (j / mul % 10 == k) Minus(Sm1[j], Sm1[j + mul]); long long Ans = 0; for (int i = 0; i <= Num; i++) Ans = Ans ^ (1ll * Sm1[i] * i); printf( %I64d n , Ans); return 0; } void Plus(int &x, int y) { x += y; if (x >= Mod) x -= Mod; return; } void Minus(int &x, int y) { x -= y; if (x < 0) x += Mod; return; } int QPow(int x, int cnt) { int ret = 1; while (cnt) { if (cnt & 1) ret = 1ll * ret * x % Mod; x = 1ll * x * x % Mod; cnt >>= 1; } return ret; }
|
#include <bits/stdc++.h> using namespace std; int n, s, x1, x2, v1, v2, w, d, ans; int main() { cin >> s >> x1 >> x2 >> v1 >> v2 >> w >> d; ans = abs(x1 - x2) * v2; if (d == 1) { if (w < x2) { if (x1 < x2) { if (x1 >= w) ans = min(ans, (x2 - w) * v1); else ans = min(ans, (2 * s + x2 - w) * v1); } else ans = min(ans, (2 * s - x2 - w) * v1); } else { if (x1 > x2) ans = min(ans, (2 * s - x2 - w) * v1); else ans = min(ans, (2 * s + x2 - w) * v1); } } else { if (w < x2) { if (x1 > x2) ans = min(ans, (2 * s - x2 + w) * v1); else ans = min(ans, (x2 + w) * v1); } else { if (x1 < x2) ans = min(ans, (x2 + w) * v1); else { if (x1 <= w) ans = min(ans, (w - x2) * v1); else ans = min(ans, (2 * s + w - x2) * v1); } } } printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; pair<int, int> a[111]; long long int dp[105][105][3010]; int n, l; long long int cal(int pre, int ind, int rem) { if (rem == 0) return 1; if (rem < 0) return 0; long long int &ret = dp[pre][ind][rem]; if (ret != -1) return ret; ret++; for (int i = 0; i < n; i++) { if (i == ind) continue; if (a[i].second == pre) ret += cal(a[i].first, i, rem - a[i].second); if (a[i].first == pre && a[i].first != a[i].second) ret += cal(a[i].second, i, rem - a[i].first); ret %= 1000000007; } return ret % 1000000007; } int main() { cin >> n >> l; for (int i = 0; i < n; i++) scanf( %d%d , &a[i].first, &a[i].second); long long int ans = 0; memset(dp, -1, sizeof dp); for (int i = 0; i < n; i++) { ans += cal(a[i].first, i, l - a[i].second); if (a[i].first != a[i].second) ans += cal(a[i].second, i, l - a[i].first); ans %= 1000000007; } cout << ans % 1000000007; }
|
#include <bits/stdc++.h> using namespace std; int N, K; char s[1100000]; int cnt[1100000][3], tot[1100000]; int pb[1100000], pw[1100000]; bool can(int p, int f) { if (p < K) return false; if (f) { if (p - pb[p - 1] - 1 < K) return false; if (p > K && s[p - K - 1] == W ) return false; return true; } else { if (p - pw[p - 1] - 1 < K) return false; if (p > K && s[p - K - 1] == B ) return false; return true; } } int main() { int b, w, i; scanf( %d%d , &N, &K); scanf( %s , s); if (N < K * 2) { puts( 0 ); return 0; } b = w = -1; for (i = 0; i < N; i++) { if (s[i] == B ) b = i; if (s[i] == W ) w = i; pb[i] = b; pw[i] = w; } cnt[0][0] = 1; tot[0] = 1; for (i = 1; i <= N; i++) { if (s[i - 1] == X ) tot[i] = tot[i - 1] * 2 % 1000000007; else tot[i] = tot[i - 1]; if (s[i - 1] == X ) cnt[i][2] = cnt[i - 1][2] * 2 % 1000000007; else cnt[i][2] = cnt[i - 1][2]; if (can(i, 1)) { cnt[i][2] += cnt[max(i - K - 1, 0)][1]; cnt[i][2] %= 1000000007; if (can(i - K, 0)) { cnt[i][2] += cnt[max(i - K * 2 - 1, 0)][0]; cnt[i][2] %= 1000000007; } } if (s[i - 1] == X ) cnt[i][0] = cnt[i - 1][0] * 2 % 1000000007; else cnt[i][0] = cnt[i - 1][0]; if (can(i, 0)) { cnt[i][0] -= cnt[max(i - K - 1, 0)][0]; if (cnt[i][0] < 0) cnt[i][0] += 1000000007; } cnt[i][1] = tot[i]; cnt[i][1] -= cnt[i][0]; if (cnt[i][1] < 0) cnt[i][1] += 1000000007; cnt[i][1] -= cnt[i][2]; if (cnt[i][1] < 0) cnt[i][1] += 1000000007; } printf( %d n , cnt[N][2]); return 0; }
|
#include <bits/stdc++.h> using namespace std; char grid[10][10]; bool colW[10], colB[10]; int main() { int Case = 1; int l, p, q; char dump[2]; while (gets(grid[1])) { for (int i = 2; i <= 8; i++) { gets(grid[i]); } memset(colB, false, sizeof(colB)); memset(colW, false, sizeof(colW)); bool found = false; int white, black; for (int i = 1; i <= 8 && !found; i++) { for (int j = 0; j < 8 && !found; j++) { if (grid[i][j] == W ) { if (!colB[j]) { found = true; white = i - 1; } } else if (grid[i][j] == B ) { colB[j] = true; } } } found = false; for (int i = 8; i >= 1 && !found; i--) { for (int j = 0; j < 8 && !found; j++) { if (grid[i][j] == B ) { if (!colW[j]) { found = true; black = 8 - i; } } else if (grid[i][j] == W ) { colW[j] = true; } } } if (white <= black) puts( A ); else puts( B ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 5 + 1e5; int main() { int a, b, c, m, ans = 0; ios::sync_with_stdio(0); cin.tie(); cin >> a >> b; c = a - b; if (c < 0) cout << 0; else if (c == 0) cout << infinity ; else { for (int i = 1; i * i <= c; ++i) { if (c % i == 0) { m = i; if ((m * (a / m)) == c) ++ans; if (i != (c / i)) { m = c / i; if ((m * (a / m)) == c) ++ans; } } } cout << ans; } }
|
#include <bits/stdc++.h> using namespace std; int num(string s) { if (s == 00 ) return 0; if (s == 10 ) return 2; if (s == 11 ) return 3; return 1; } void solve(int x) { vector<int> v[4], vv[4]; int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { string s; int x; cin >> s >> x; v[num(s)].push_back(x); vv[num(s)].push_back(x); } for (int i = 0; i < 4; i++) { sort(vv[i].begin(), vv[i].end(), greater<int>()); sort(v[i].begin(), v[i].end(), greater<int>()); for (int j = 1; j < v[i].size(); j++) v[i][j] += v[i][j - 1]; } int t1, t2; int sz = v[3].size(); if (sz) ans += v[3][sz - 1]; t1 = sz; t2 = sz; int tot = sz; sz = min(v[1].size(), v[2].size()); if (sz > 0) ans += v[1][sz - 1] + v[2][sz - 1]; int i = sz, j = sz, k = 0; t1 += sz; t2 += sz; tot += sz + sz; int mx = 2 * t1 - tot; vector<int> fin; while (i < v[1].size()) fin.push_back(vv[1][i++]); while (j < v[2].size()) fin.push_back(vv[2][j++]); while (k < v[0].size()) fin.push_back(vv[0][k++]); sort(fin.begin(), fin.end(), greater<int>()); i = 0; while (mx && i < fin.size()) { mx--; ans += fin[i++]; } cout << ans; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; int T = t; while (t--) { solve(T - t); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int k; struct node { int ma[10]; int mi[10]; int cnt = 1; bool operator==(const node &a) const { bool f0 = false, f1 = false; for (int i = 0; i < k; ++i) { if (ma[i] > a.mi[i]) f0 = true; if (mi[i] < a.ma[i]) f1 = true; } if (f0 & f1) return true; return false; } bool operator<(const node &a) const { for (int i = 0; i < k; ++i) { if (ma[i] > a.mi[i]) return false; } return true; } }; int n; set<node> se; node cur; int main() { scanf( %d %d , &n, &k); for (int i = 0; i < n; ++i) { for (int j = 0; j < k; ++j) { scanf( %d , &cur.mi[j]); cur.ma[j] = cur.mi[j]; } cur.cnt = 1; set<node>::iterator it = se.find(cur); while (it != se.end()) { for (int i = 0; i < k; ++i) { cur.mi[i] = min(cur.mi[i], it->mi[i]); cur.ma[i] = max(cur.ma[i], it->ma[i]); } cur.cnt += it->cnt; se.erase(it); it = se.find(cur); } se.insert(cur); it = se.end(); --it; printf( %d n , it->cnt); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int Max = 510; int arr[Max][Max]; int ps[Max][Max]; bool check(int i, int j); int calcPs(int minX, int minY, int maxX, int maxY); int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { if (s[j] == 0 ) arr[i][j] = 0; else arr[i][j] = 1; } } for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + check(i, j); } } long long result = 0; for (int i = 1; i < n - 1; i++) { for (int j = i; j < n - 1; j++) { int l = 1; int r = 1; while (r < m - 1 && l < m - 1) { int calcedPs = calcPs(i, l, j, r); if (calcedPs >= k) { result += m - r - 1; l++; } else r++; } } } cout << result; } bool check(int i, int j) { return arr[i][j] == 1 && arr[i][j + 1] == 1 && arr[i + 1][j] == 1 && arr[i][j - 1] == 1 && arr[i - 1][j] == 1; } int calcPs(int minX, int minY, int maxX, int maxY) { minX--; minY--; return ps[maxX][maxY] - ps[minX][maxY] - ps[maxX][minY] + ps[minX][minY]; }
|
#include <bits/stdc++.h> using namespace std; char s[1000005]; int main() { int n, k; while (scanf( %d%d , &n, &k) != EOF) { if (k > n || (n > 1 && k == 1)) { printf( -1 n ); break; } else { if ((n - k) % 2) { for (int i = 1; i <= n - k + 1; i++) { s[i] = a ; s[i + 1] = b ; i++; } s[n - k + 2] = a ; int c = 99; for (int i = n - k + 3; i <= n; i++) { s[i] = c; c++; } } else { for (int i = 1; i <= n - k; i++) { s[i] = a ; s[i + 1] = b ; i++; } int c = 97; for (int i = n - k + 1; i <= n; i++) { s[i] = c; c++; } } } for (int i = 1; i <= n; i++) printf( %c , s[i]); cout << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <class T> string toStr(const T &x) { stringstream s; s << x; return s.str(); } template <class T> int toInt(const T &x) { stringstream s; s << x; int r; s >> r; return r; } const double pi = acos(-1); const double Pi2 = acos(0); struct point { double x, y; point() {} point(double X, double Y) : x(X), y(Y) {} point sub(point a) { return point(x - a.x, y - a.y); } point add(point a) { return point(x + a.x, y + a.y); } double cross(point a) { return x * a.y - y * a.x; } point multbyscalar(double ua) { return point(ua * x, ua * y); } }; int lucky(string cad) { for (int i = 0; i < cad.size(); ++i) if (cad[i] != 4 and cad[i] != 7 ) return 0; return 1; } int main() { int n, x, y; cin >> n >> x >> y; int pin = 1; n >>= 1; if ((x == n or x == n + 1) and (y == n or y == n + 1)) pin = 0; if (pin) cout << YES << endl; else cout << NO << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int nega = 1; while (!isdigit(ch)) { if (ch == - ) nega = -1; ch = getchar(); } int ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } int add(int x, int y) { return x + y >= 998244353 ? x + y - 998244353 : x + y; } int sub(int x, int y) { return x - y < 0 ? x - y + 998244353 : x - y; } int mul(int x, int y) { return 1LL * x * y % 998244353; } int B = 707; int n, m, a[500005], b[500005], fa[500005]; int c[500005], d[500005]; struct Query { int opt, l, r, x, ans; }; Query q[500005]; int find(int u) { return u == fa[u] ? u : fa[u] = find(fa[u]); } void merge(int x, int y) { if (c[y]) fa[c[x]] = c[y]; else { b[c[x]] = y; c[y] = c[x]; } d[y] += d[x]; c[x] = d[x] = 0; } signed main() { cin >> n >> m; for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= m; i++) { q[i].opt = read(), q[i].l = read(), q[i].r = read(), q[i].x = read(); } for (int L = 1, R = B; L <= n; L += B, R += B) { if (R > n) R = n; if (L > n) break; int maxn = 0, tag = 0; for (int k = 1; k <= 500000; k++) { if (c[k]) c[k] = 0; if (d[k]) d[k] = 0; } for (int k = L; k <= R; k++) { if (a[k] > maxn) maxn = a[k]; int x = a[k]; if (c[x]) fa[k] = c[x]; else { c[x] = fa[k] = k; b[k] = x; } d[x]++; } for (int j = 1; j <= m; j++) { int opt = q[j].opt, l = q[j].l, r = q[j].r, x = q[j].x; if (r < L || l > R) continue; if (opt == 1) { if (l <= L && R <= r) { if (x * 2 <= maxn - tag) { for (int k = tag + 1; k <= tag + x; k++) { if (c[k]) merge(k, k + x); } tag += x; } else { for (int k = maxn; k > tag + x; k--) { if (c[k]) merge(k, k - x); } maxn = min(maxn, tag + x); } } else { for (int k = L; k <= R; k++) { a[k] = b[find(k)]; c[a[k]] = d[a[k]] = 0; a[k] -= tag; } memset(b + L, 0, (R - L + 1) * 4); tag = 0; l = max(l, L), r = min(r, R); for (int k = l; k <= r; k++) if (a[k] > x) a[k] -= x; maxn = 0; for (int k = L; k <= R; k++) { if (a[k] > maxn) maxn = a[k]; int x = a[k]; if (c[x]) fa[k] = c[x]; else { c[x] = fa[k] = k; b[k] = x; } d[x]++; } } } if (opt == 2) { if (l <= L && R <= r) { if (x + tag <= 500000) q[j].ans += d[x + tag]; } else { l = max(l, L), r = min(r, R); for (int k = l; k <= r; k++) { if (b[find(k)] == tag + x) q[j].ans++; } } } } } for (int i = 1; i <= m; i++) { if (q[i].opt == 2) printf( %d n , q[i].ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, i, n, x, j; cin >> t; while (t--) { cin >> n; vector<int> a, b; for (i = 0; i < n; i++) { cin >> x; a.push_back(x); } for (i = 0; i < n; i++) { cin >> x; b.push_back(x); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); for (i = 0; i < n; i++) { cout << a[i] << ; } cout << n ; for (i = 0; i < n; i++) { cout << b[i] << ; } cout << n ; } return 0; }
|
#include <bits/stdc++.h> struct XD { double x, y, z; void input() { scanf( %lf%lf%lf , &x, &y, &z); } XD operator+(const XD& b) const { XD a; a.x = x + b.x; a.y = y + b.y; a.z = z + b.z; return a; } }; XD a, v; double r; inline double MIN(double a, double b) { return a < b ? a : b; } inline double solve(XD a, XD b, XD v, double r) { double c1, c2, c3; c1 = v.x * v.x + v.y * v.y + v.z * v.z; c2 = v.x * (a.x - b.x) * 2 + v.y * (a.y - b.y) * 2 + v.z * (a.z - b.z) * 2; c3 = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z) - r * r; if (c2 * c2 - 4 * c1 * c3 < 0) return 1e100; double s1 = (-c2 - sqrt(c2 * c2 - 4 * c1 * c3)) / (2 * c1); if (s1 < 0) return 1e100; else return s1; } int main() { a.input(); v.input(); scanf( %lf , &r); int n, i; scanf( %d , &n); double mint = 1e100; for (i = 0; i < n; i++) { XD cen; cen.input(); double ri; scanf( %lf , &ri); mint = MIN(mint, solve(a, cen, v, r + ri)); int q; scanf( %d , &q); while (q--) { XD dif; dif.input(); mint = MIN(mint, solve(a, cen + dif, v, r)); } } if (mint > 1e99) puts( -1 ); else printf( %.10lf n , mint); }
|
#include <bits/stdc++.h> bool compare(int a, int b) { return a < b; } void swapped(char s1, char s2) { char temp = s1; s1 = s2; s2 = temp; } long long int modd(long long int n, long long int k) { return (k + (n % k)) % k; } bool i(long long first) { return first && (!(first & (first - 1))); } bool p(long long n) { long long i, flag = 1; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) { flag = 0; break; } } if (flag == 1) { return true; } else { return false; } return 0; } using namespace std; void solve() { long long n; cin >> n; if (n == 1) { cout << FastestFinger << n ; return; } if (n == 2) { cout << Ashishgup << n ; return; } if (i(n)) { cout << FastestFinger << n ; return; } if (n & 1) { cout << Ashishgup << n ; return; } n /= 2; if (p(n)) { cout << FastestFinger << n ; return; } else { cout << Ashishgup << n ; return; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, mn, mx; cin >> n >> m >> mn >> mx; set<int> temp; for (int i = 0; i < m; ++i) { int x; cin >> x; temp.insert(x); } if (*temp.begin() < mn or *temp.rbegin() > mx) { cout << Incorrect << endl; return 0; } int off = 0; if (*temp.begin() != mn) off++; if (*temp.rbegin() != mx) off++; if (n - m >= off) { cout << Correct << endl; } else { cout << Incorrect << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int x, s, t; int main(void) { cin >> t >> s >> x; int a = (x - t) / s; if (t == x) { cout << YES << endl; } else if (a <= 0) { cout << NO << endl; } else if (t + a * s == x || t + a * s + 1 == x) { cout << YES << endl; } else { cout << NO << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; const int oo = 0x3f3f3f3f; inline long long read() { char ch = getchar(); int x = 0, f = 1; while (ch > 9 || ch < 0 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } long long N, M, a[MAXN], b[MAXN], leftt, rightt, mid, ans, cnt, st, nd; bool vis[MAXN]; namespace solution { void init() { N = read(); M = read(); for (int i = 1; i <= N; i++) a[i] = read(); for (int i = 1; i <= M; i++) b[i] = read(); sort(a + 1, a + N + 1); sort(b + 1, b + M + 1); } bool check(long long length) { int t = 1; for (int i = 1; i <= M; i++) { st = b[i] - length; nd = b[i] + length; while (t <= N && a[t] >= st && a[t] <= nd) t++; } return t > N; } void slove() { leftt = 0; rightt = 2000000000; while (leftt + 1 < rightt) { mid = (leftt + rightt) >> 1; if (check(mid)) rightt = mid; else leftt = mid; } if (check(leftt)) ans = leftt; else ans = rightt; } void output() { cout << ans << endl; } } // namespace solution int main() { using namespace solution; init(); slove(); output(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MOD(1000000007); const int INF((1 << 30) - 1); const int MAXN(30); string a[MAXN]; int type[200] = {0}; int main() { int n, m, x; scanf( %d%d%d , &n, &m, &x); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = A ; i <= Z ; i++) type[i] = INF; for (int i = a ; i <= z ; i++) type[i] = INF; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (a[i][j] != S ) type[a[i][j]] = 1; else { for (int k = 0; k < n; k++) for (int l = 0; l < m; l++) if (a[k][l] != S ) type[a[k][l] - a + A ] = min(type[a[k][l] - a + A ], (i - k) * (i - k) + (j - l) * (j - l)); } } int q; scanf( %d , &q); int ans = 0; while (q--) { char c; scanf( %c , &c); if (type[c] == INF) { printf( -1 ); return 0; } else if (c >= A && c <= Z && type[c] > x * x) ans++; } printf( %d , ans); }
|
#include <bits/stdc++.h> int dp[51 * 10001]; int main(void) { int n, d, a, sum, i, j; while (scanf( %d%d , &n, &d) != EOF) { sum = 0; memset(dp, 0, sizeof(dp)); dp[0] = 1; for (i = 0; i < n; i++) { scanf( %d , &a); sum += a; for (j = sum; j >= a; j--) { if (dp[j - a] == 1) dp[j] = 1; } } int k = 0; int ans = 0; while (true) { i = k + d; while (dp[i] == 0 && i > k) i--; if (k == i) break; k = i; ans++; } printf( %d %d n , k, ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 150000; struct node { long long l, r, sm, mx; } tr[4 * maxn]; inline node comb(node ll, node rr) { node ret; ret.l = max(ll.l, ll.sm + rr.l); ret.r = max(rr.r, rr.sm + ll.r); ret.sm = ll.sm + rr.sm; ret.mx = max(max(ll.mx, rr.mx), ll.r + rr.l); return ret; } inline void pushup(int o) { tr[o] = comb(tr[o * 2], tr[o * 2 + 1]); } long long a[maxn]; void build(int o, int l, int r) { if (l == r) { tr[o].l = tr[o].r = tr[o].sm = tr[o].mx = a[l]; return; } build(o * 2, l, (l + r) / 2); build(o * 2 + 1, (l + r) / 2 + 1, r); pushup(o); } node query(int o, int l, int r, int ql, int qr) { if (ql <= l && qr >= r) return tr[o]; if (qr <= (l + r) / 2) return query(o * 2, l, (l + r) / 2, ql, qr); if (ql > (l + r) / 2) return query(o * 2 + 1, (l + r) / 2 + 1, r, ql, qr); node ll = query(o * 2, l, (l + r) / 2, ql, qr), rr = query(o * 2 + 1, (l + r) / 2 + 1, r, ql, qr); return comb(ll, rr); } long long b[maxn]; int main() { long long n, q, c; scanf( %lld%lld%lld , &n, &q, &c); long long x; scanf( %lld , &x); n--; for (int i = 1; i <= n; i++) scanf( %lld , b + i); for (int i = 1; i <= n; i++) { scanf( %lld , &x); a[i] = (b[i] - b[i - 1]) * 50 - c * x; } build(1, 1, n); long long ans = 0; while (q--) { long long l, r; scanf( %lld%lld , &l, &r); r--; node tmp = query(1, 1, n, l, r); ans += max(tmp.mx, 0LL); } cout << fixed << setprecision(9) << 0.01 * ans << endl; }
|
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); long long chess[5][2004][2004]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int N, r1, r2, c1, c2; cin >> N; for (int i = 1; i < N + 1; i += 1) for (int j = 1; j < N + 1; j += 1) cin >> chess[0][i][j], chess[1][i][j] = chess[0][i][j] + chess[1][i - 1][j - 1], chess[2][i][j] = chess[0][i][j] + chess[2][i - 1][j + 1]; for (int i = N; i >= 1; i -= 1) for (int j = N; j >= 1; j -= 1) chess[3][i][j] = chess[0][i][j] + chess[3][i + 1][j + 1], chess[4][i][j] = chess[0][i][j] + chess[4][i + 1][j - 1]; long long ec = -1, oc = -1, t; for (int i = 1; i < N + 1; i += 1) for (int j = 1; j < N + 1; j += 1) { t = chess[0][i][j] + chess[1][i - 1][j - 1] + chess[2][i - 1][j + 1] + chess[3][i + 1][j + 1] + chess[4][i + 1][j - 1]; if (i + j & 1 && oc < t) r1 = i, c1 = j, oc = t; else if ((i + j & 1) == 0 && ec < t) r2 = i, c2 = j, ec = t; } cout << oc + ec << n << r1 << << c1 << << r2 << << c2 << n ; }
|
#include <bits/stdc++.h> //#define int long long #define pii pair<short, int> #define x1 x1228 #define y1 y1228 #define left left228 #define right right228 #define data data228 #define pb push_back #define eb emplace_back #define mp make_pair #define ff first #define ss second #define all(x) x.begin(), x.end() #define debug(x) cout << #x << : << x << endl; #define TIME (ld)clock()/CLOCKS_PER_SEC using namespace std; /** Fast input-output */ template <class T = int> inline T readInt(); inline double readDouble(); inline int readUInt(); inline int readChar(); // first non-blank character inline void readWord( char *s ); inline bool readLine( char *s ); // do not save n inline bool isEof(); inline int getChar(); inline int peekChar(); inline bool seekEof(); inline void skipBlanks(); template <class T> inline void writeInt( T x, char end = 0, int len = -1 ); inline void writeChar( int x ); inline void writeWord( const char *s ); inline void writeDouble( double x, int len = 0 ); inline void flush(); static struct buffer_flusher_t { ~buffer_flusher_t() { flush(); } } buffer_flusher; /** Read */ static const int buf_size = 4096; static unsigned char buf[buf_size]; static int buf_len = 0, buf_pos = 0; inline bool isEof() { if (buf_pos == buf_len) { buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin); if (buf_pos == buf_len) return 1; } return 0; } inline int getChar() { return isEof() ? -1 : buf[buf_pos++]; } inline int peekChar() { return isEof() ? -1 : buf[buf_pos]; } inline bool seekEof() { int c; while ((c = peekChar()) != -1 && c <= 32) buf_pos++; return c == -1; } inline void skipBlanks() { while (!isEof() && buf[buf_pos] <= 32U) buf_pos++; } inline int readChar() { int c = getChar(); while (c != -1 && c <= 32) c = getChar(); return c; } inline int readUInt() { int c = readChar(), x = 0; while ( 0 <= c && c <= 9 ) x = x * 10 + c - 0 , c = getChar(); return x; } template <class T> inline T readInt() { int s = 1, c = readChar(); T x = 0; if (c == - ) s = -1, c = getChar(); else if (c == + ) c = getChar(); while ( 0 <= c && c <= 9 ) x = x * 10 + c - 0 , c = getChar(); return s == 1 ? x : -x; } inline double readDouble() { int s = 1, c = readChar(); double x = 0; if (c == - ) s = -1, c = getChar(); while ( 0 <= c && c <= 9 ) x = x * 10 + c - 0 , c = getChar(); if (c == . ) { c = getChar(); double coef = 1; while ( 0 <= c && c <= 9 ) x += (c - 0 ) * (coef *= 1e-1), c = getChar(); } return s == 1 ? x : -x; } inline void readWord( char *s ) { int c = readChar(); while (c > 32) *s++ = c, c = getChar(); *s = 0; } inline bool readLine( char *s ) { int c = getChar(); while (c != n && c != -1) *s++ = c, c = getChar(); *s = 0; return c != -1; } /** Write */ static int write_buf_pos = 0; static char write_buf[buf_size]; inline void writeChar( int x ) { if (write_buf_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0; write_buf[write_buf_pos++] = x; } inline void flush() { if (write_buf_pos) fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0; } template <class T> inline void writeInt( T x, char end, int output_len ) { if (x < 0) writeChar( - ), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = 0 + x % 10, x /= 10; while (n < output_len) s[n++] = 0 ; while (n--) writeChar(s[n]); if (end) writeChar(end); } inline void writeWord( const char *s ) { while (*s) writeChar(*s++); } inline void writeDouble( double x, int output_len ) { if (x < 0) writeChar( - ), x = -x; int t = (int)x; writeInt(t), x -= t; writeChar( . ); for (int i = output_len - 1; i > 0; i--) { x *= 10; t = std::min(9, (int)x); writeChar( 0 + t), x -= t; } x *= 10; t = std::min(9, (int)(x + 0.5)); writeChar( 0 + t); } typedef long long ll; typedef long double ld; const int maxn = 1500 + 7, mod = 1e9 + 7, MAXN = 2e6 + 7; const double eps = 1e-9; const ll INF = 1e18, inf = 1e15; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); int n, q; int cl[maxn][maxn]; vector<pii> ladder1[2][maxn], ladder2[2][maxn]; vector<pii> merge(vector<pii> p1, vector<pii> p2) { vector<pii> res; res.reserve((int)p1.size() + (int)p2.size()); int pt1 = 0; int pt2 = 0; while (pt1 < (int)p1.size() && pt2 < (int)p2.size()) { if (p1[pt1].ff <= p2[pt2].ff) { res.pb(p1[pt1++]); } else { res.pb(p2[pt2++]); } } while (pt1 < (int)p1.size()) { res.pb(p1[pt1++]); } while (pt2 < (int)p2.size()) { res.pb(p2[pt2++]); } return res; } int used[maxn * maxn]; void compress(vector<pii> &res) { for (auto x : res) { used[x.ss] = 0; } vector<pii> nw; for (auto x : res) { if (!used[x.ss]) { nw.pb(x); used[x.ss] = 1; } } while ((int)nw.size() > q + 1) { nw.pop_back(); } res = nw; } void add(int i, int j, vector<pii> &cur, vector<int> &point) { short sz = min(i + 1, n - j); if ((int)cur.size() <= q) { point[0]++; point[sz]--; } else { point[0]++; point[min(sz, cur.back().ff)]--; } } void solve() { n = readInt(); q = readInt(); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cl[i][j] = readInt(); } } vector<int> point(n + 1); int pt = 0; for (int i = 0; i < n; ++i) { pt ^= 1; for (int j = n - 1; j >= 0; --j) { vector<pii> res = ladder1[pt][j + 1]; if (i) { res = merge(res, ladder1[pt ^ 1][j + 1]); } for (auto &x : res) { x.ff++; } res = merge(res, {{0, cl[i][j]}}); compress(res); ladder1[pt][j] = res; res.clear(); if (i) { res = merge(ladder2[pt ^ 1][j], ladder2[pt ^ 1][j + 1]); for (auto &x : res) { x.ff++; } } res = merge(res, {{0, cl[i][j]}}); compress(res); ladder2[pt][j] = res; res = merge(res, ladder1[pt][j]); compress(res); add(i, j, res, point); } for (int j = 0; j < n; ++j) { ladder1[pt ^ 1][j].clear(); ladder2[pt ^ 1][j].clear(); } } // cout << TIME; // return; int res = 0; for (int i = 0; i < n; ++i) { res += point[i]; cout << res << n ; } } signed main() { #ifdef LOCAL freopen( TASK.in , r , stdin); freopen( TASK.out , w , stdout); #endif // LOCAL ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); cout << fixed; int t = 1; for (int i = 0; i < t; ++i) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int a[300009]; vector<pair<pair<long long int, long long int>, pair<long long int, long long int>>> v; long long int ans = 1; vector<int> vec; bool check(int idx, long long int mid) { long long int dist, i, j, x, fuel = mid, cnt = v[idx].second.second, kitna_chlegi; for (i = v[idx].first.first; i < v[idx].first.second; i++) { x = a[i + 1] - a[i]; kitna_chlegi = fuel / v[idx].second.first; if (kitna_chlegi < x) { if (cnt == 0 || mid / v[idx].second.first < x) { return 0; } fuel = mid; cnt--; } fuel -= x * v[idx].second.first; } return 1; } void update(int idx) { if (check(idx, ans)) { return; } long long int l = ans, r = 1000000000000000000, temp = r; bool f; while (l <= r) { long long int mid = (l + r) >> 1; f = check(idx, mid); if (f) { temp = min(temp, mid); r = mid - 1; } else { l = mid + 1; } } ans = max(ans, temp); } int main() { long long int i, j, k, x, y, t, n, m, p; scanf( %lld%lld , &n, &m); for (i = 1; i <= n; i++) { scanf( %lld , &a[i]); } for (i = 0; i < m; i++) { scanf( %lld%lld , &x, &y); scanf( %lld%lld , &j, &k); v.push_back(make_pair(make_pair(x, y), make_pair(j, k))); vec.push_back(i); } random_shuffle(vec.begin(), vec.end()); for (i = 0; i < m; i++) { update(vec[i]); } cout << ans << endl; ; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int node[(ll)(1e5 + 7)], in[(ll)(1e5 + 7)], out[(ll)(1e5 + 7)], ti = 1, hie[(ll)(1e5 + 7)]; int dp[(ll)(1e5 + 7)][17]; vector<int> gr[(ll)(1e5 + 7)], hi[(ll)(1e5 + 7)]; void dfs(int node, int h) { hi[h].push_back(ti); hie[node] = h; in[node] = ti++; for (int x : gr[node]) { dfs(x, h + 1); } out[node] = ti++; } int main() { ios_base ::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> ve; for (int i = 1; i <= n; i++) { for (int j = 0; j < 17; j++) { dp[i][j] = -1; } } for (int i = 1; i <= n; i++) { int val; cin >> val; if (val) { dp[i][0] = val; gr[val].push_back(i); } else { ve.push_back(i); } } for (int x : ve) { dfs(x, 1); } for (int j = 1; j < 17; j++) { for (int i = 1; i <= n; i++) { if (dp[i][j - 1] != -1) { dp[i][j] = dp[dp[i][j - 1]][j - 1]; } } } int q; cin >> q; for (int i = 0; i < q; i++) { int x, p; cin >> x >> p; for (int j = 0; (1 << j) <= p; j++) { if ((1 << j) & p) { x = dp[x][j]; } } if (x == -1) { cout << 0 ; continue; } int hh = hie[x] + p; int xx = (upper_bound(hi[hh].begin(), hi[hh].end(), out[x]) - lower_bound(hi[hh].begin(), hi[hh].end(), in[x])); cout << max(0, xx - 1) << ; } cout << n ; }
|
#include <bits/stdc++.h> using namespace std; int x[110000]; char str[110000]; long long tail[110000], a[110000], b[110000], c[110000], sum[110000], cm1[110000], cm2[110000]; int n, m; const long long MOD = 1000000007L; long long mod_reset(long long x) { return (x % MOD + MOD) % MOD; } void extgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1L; y = 0L; return; } long long div = a / b; extgcd(b, a % b, y, x); y = mod_reset(y - div * x % MOD); } long long get_antimul(long long a) { long long x, y; extgcd(a, MOD, x, y); return x; } void conduct() { long long ans; scanf( %s , str); for (int i = 0; i < n; ++i) x[i + 1] = str[i] - 0 ; if (m == 0) { ans = 0L; for (int i = 1; i <= n; ++i) ans = (ans * 10 + x[i]) % MOD; cout << ans << endl; return; } cm1[n - m] = cm2[n - m] = 1; for (int i = n - m; i; --i) { long long ref = get_antimul(n - i - m + 1); cm1[i - 1] = (cm1[i] * (n - i)) % MOD * ref % MOD; cm2[i - 1] = (cm2[i] * (n - i - 1)) % MOD * ref % MOD; } tail[0] = 0L; for (int i = 1; i <= n; ++i) tail[i] = tail[i - 1] + x[n - i + 1]; a[0] = b[0] = sum[0] = ans = 0L; c[0] = 1L; for (int i = 1; i <= n - m; ++i) { a[i] = (a[i - 1] * 10 + x[i]) % MOD; c[i] = c[i - 1] * 10 % MOD; b[i] = (b[i - 1] + c[i - 1] * x[n - i + 1]) % MOD; sum[i] = (mod_reset(sum[i - 1] - b[i - 1]) * 10 + tail[n - i + 1]) % MOD; ans = (ans + mod_reset(sum[i] - a[i] - b[i]) * cm2[i] + (a[i] + b[i]) * cm1[i]) % MOD; } cout << ans << endl; } int main() { while (scanf( %d%d , &n, &m) != EOF) conduct(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const double PI = 3.141592653589793238463; const int MAXN = 100005; long long n, s; long long temp = 0; long long arr[MAXN]; bool ok(long long mid) { long long tArray[n]; for (long long i = 0; i < n; i++) { tArray[i] = (arr[i] + ((i + 1) * mid)); } sort(tArray, tArray + n); temp = 0; long long t = mid; for (int i = 0; i < t; i++) { if (temp + tArray[i] <= s) { temp += tArray[i]; } else { return 0; } } return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; for (int i = 0; i < n; i++) { cin >> arr[i]; } long long l = 0, r = n; long long ans = 0; long long mn = 0; while (l <= r) { long long mid = l + (r - l) / 2; if (ok(mid)) { ans = mid; mn = temp; l = mid + 1; } else { r = mid - 1; } } cout << ans << << mn << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 5; const int INF = 0x3f3f3f3f; const int MOD = 1e3; int ans[MAXN]; char s[MAXN]; void solve() { int n; cin >> n; int k, num; int s12, s13, s23; cout << ? << 1 << << 2 << endl; scanf( %d , &s12); cout << ? << 1 << << 3 << endl; scanf( %d , &s13); cout << ? << 2 << << 3 << endl; scanf( %d , &s23); ans[1] = s13 - s23; ans[2] = s12 - ans[1]; ans[3] = s13 - ans[1] - ans[2]; for (int i = 4; i <= n; i++) { cout << ? << i - 1 << << i << endl; scanf( %d , &ans[i]); ans[i] = ans[i] - ans[i - 1]; } cout << ! << ans[1] << flush; for (int i = 2; i <= n; i++) { cout << << ans[i] << flush; } cout << endl; return; } int main() { solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int r, g, b; cin >> r >> g >> b; int a[3] = {r, g, b}; int i = 0; while (a[0] > 0 || a[1] > 0 || a[2] > 0) { if (a[i % 3] > 0) a[i % 3] -= 2; i++; } cout << 30 + i - 1; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 1; int MOD = 1e9 + 7; const int INF = 1e9; const long long LINF = 1e18; void solve() { int n; cin >> n; string s; cin >> s; vector<int> a; int ans = 0; bool ok = true; bool all_s = true; vector<int> pref(n); for (int i = 0; i < n; ++i) { if (i == 0) { if (s[i] == G ) pref[i] = 1; else pref[i] = 0; } else { if (s[i] == G ) pref[i] = 1 + pref[i - 1]; else pref[i] = pref[i - 1]; } } for (int i = 0; i < n; ++i) { if (s[i] == S ) { ok = false; int l = 0, r = 0; bool k1 = false, k2 = false; int j = i - 1; while (j >= 0 && s[j] == G ) { l++; j--; } if (j > 0) { if (pref[j - 1]) k1 = true; } j = i + 1; while (j < n && s[j] == G ) { r++; j++; } if (j < n - 1 && pref[n - 1] - pref[j] > 0) k2 = true; if (k1 || k2) ans = max(ans, r + l + 1); else ans = max(ans, r + l); } else all_s = false; } if (ok) ans = n; if (all_s) ans = 0; cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } }
|
#include <bits/stdc++.h> long long ans, fac[101000], fac_inv[101000], sum, nn; int n, k, len; char s[101000]; inline long long C(int x, int y) { if (x < y || x < 0 || y < 0) return 0; if (y == 0) return 1; return fac[x] * fac_inv[y] % 1000000007 * fac_inv[x - y] % 1000000007; } inline long long power(long long x, int y) { long long ret = 1; for (; y; y >>= 1, x = x * x % 1000000007) if (y & 1) ret = ret * x % 1000000007; return ret; } int main() { scanf( %d%d , &n, &k); scanf( %s , s + 1); len = strlen(s + 1); fac[0] = 1; for (int i = 1; i <= 101000; i++) fac[i] = fac[i - 1] * i % 1000000007; fac_inv[101000] = power(fac[101000], 1000000007 - 2); for (int i = 101000 - 1; i + 1; i--) fac_inv[i] = (i + 1) * fac_inv[i + 1] % 1000000007; sum = ans = 0; nn = 1; for (int i = n, tmp; i; i--) { tmp = s[i] - 0 ; ans = (ans + sum * tmp % 1000000007 + nn * C(i - 1, k) % 1000000007 * tmp % 1000000007) % 1000000007; sum = (sum + C(i - 2, k - 1) * nn % 1000000007) % 1000000007; nn = nn * 10 % 1000000007; } printf( %I64d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; map<char, int> mp; map<char, int> mp1; map<char, int> mp2; long long frq[100002] = {}; bool lol = true; int main() { bool u = false; long long x, y, p; scanf( %lld%lld , &x, &y); long long num = y, sum = 0; if (x == 1 && y == 10) { printf( -1 n ); return 0; } if (y < 10) { for (int i = 1; i <= x; i++) { cout << num; } } else { for (int i = 1; i <= x; i++) { if (i == x) { cout << num % 10; } else cout << num / 10; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { string a, b; int n; cin >> n >> a >> b; int T[n], pom[n]; for (int i = 0; i < n; ++i) T[i] = -1; for (int i = 0; i < n; ++i) { bool ud = false; for (int j = 0; j < n; ++j) { if (T[j] == -1 && a[j] == b[i]) { ud = true; T[j] = i; break; } } if (!ud) { cout << -1 << endl; return 0; } } queue<int> ans; int t = (T[n - 1] == 0 ? n - 1 : n); while (t > 0) { int np = -1; for (int i = 0; i < n; ++i) if (T[i] == n - t) { np = i; break; } ans.push(n - np - 1); ans.push(1); ans.push(n); int id = 0; for (int i = np - 1; i >= 0; --i) { pom[id++] = T[i]; } for (int i = np + 1; i < t; ++i) { pom[i - 1] = T[i]; } t--; for (int i = 0; i < t; ++i) T[i] = pom[i]; for (int i = t; i < n; ++i) { T[i] = i - t; } } cout << ans.size() << endl; while (!ans.empty()) { cout << ans.front() << ; ans.pop(); } cout << endl; }
|
#include <bits/stdc++.h> template <class T> std::vector<T> ReadVector(size_t size) { std::vector<T> vector(size); for (auto& element : vector) { std::cin >> element; } return vector; } int64_t divisor(int64_t x) { for (int64_t d = 2; d * d <= x; ++d) { while (x % d == 0) { x /= d; return d; } } return x; } void YES(bool yes) { if (yes) { std::cout << YES n ; } else { std::cout << NO n ; } } class Solver { public: Solver() {} void solve() { int t; std::cin >> t; while (t--) { one(); } } int lo(int& a, int& d) { if (a + d == 0) return 0; int losers = 0; if (d > a) { int m = std::max(0, a); losers += m; d -= m; a -= m; } else if (d < a) { int m = std::max(0, d - 1); losers += m; a -= m; d -= m; } else { losers += d - 1; d = 1; a = 1; } return losers; } int wi(int& a, int& d) { int winners = 0; if (d > a) { int m = a; winners += m; d -= m; a = 0; } else if (d < a) { int m = d; winners += m; a -= m; d = 0; } else { winners += d; d = 0; a = 0; } return winners; } void one() { int n, x, y; std::cin >> n >> x >> y; if (x > y) std::swap(x, y); int a = x - 1; int b = n - x; int c = y - 1; int d = n - y; int losers = 0; losers += lo(a, d); losers += lo(c, b); losers += std::min(b, d); a = x - 1; b = n - x; c = y - 1; d = n - y; int winners = 1; winners += wi(a, d); winners += wi(c, b); winners += std::min(a, c); std::cout << n - losers << << winners << std::endl; } }; int main() { Solver solver; solver.solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = RDLU ; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? YES : NO ); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et() { puts( -1 ); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } char s[525][525], qy[525]; int a[525][525 + 300], q; void pp(int x, int y) { for (int(i) = 1; (i) <= (int)(x); (i)++) { for (int(j) = 1; (j) <= (int)(y); (j)++) printf( %d , a[i][j]); puts( ); } } void Gaw() { int SZ = n + q; int fr = 0; for (int(i) = 1; (i) <= (int)(m); (i)++) { int j = -1, r = i; while (r <= n) { for (int k = i; k <= m; k++) { if (abs(a[k][r]) != 0) { j = k; break; } } if (j == -1) r++; else { for (int k = i; k <= m; k++) swap(a[k][i], a[k][r]); break; } } if (n < r) break; if (j == -1) { continue; } fr++; if (i != j) for (int k = i; k <= SZ; k++) swap(a[i][k], a[j][k]); for (int k = i + 1; k <= m; k++) if (k != i && a[k][i] != 0) { for (int p = SZ; p >= i; p--) { a[k][p] = a[i][p] * (5 - a[k][i]) % 5 + a[k][p] * a[i][i] % 5; a[k][p] %= 5; } } } int ans = fastPow(5, n - fr); for (int(i) = 1; (i) <= (int)(q); (i)++) { int ta = ans; for (int(j) = 1; (j) <= (int)(m); (j)++) if (a[j][n + i] != 0) { bool ok = 0; for (int(x) = 1; (x) <= (int)(n); (x)++) if (a[j][x] != 0) { ok = 1; break; } if (ok) continue; ta = 0; break; } printf( %d n , ta); } } void fmain(int tid) { scanf( %d%d , &n, &m); for (int(i) = 1; (i) <= (int)(n); (i)++) scanf( %s , s[i] + 1); for (int(j) = 1; (j) <= (int)(m); (j)++) { for (int(i) = 1; (i) <= (int)(n); (i)++) { a[j][i] = s[i][j] - a ; } } scanf( %d , &q); for (int(i) = 1; (i) <= (int)(q); (i)++) { scanf( %s , qy + 1); for (int(j) = 1; (j) <= (int)(m); (j)++) { a[j][n + i] = qy[j] - a ; } } Gaw(); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
|
#include <bits/stdc++.h> using namespace std; char a[130000]; struct gg { int x, y, z; } b[130000]; int cmp(gg p, gg q) { return p.z < q.z; } void solve() { int n, m; cin >> n >> m; cin >> a; int l = 0; int p = 0, q = 0, o = 0; for (int i = 0; i < n; i++) if (a[i] == W ) { p = i; o = i; break; } for (int i = 0; i < n; i++) { if (a[i] == W ) { q = i; b[l].x = p; b[l].z = q - p - 1; b[l++].y = q; p = q; } } sort(b, b + l, cmp); for (int i = 0; i < l && m > 0; i++) { int gg = b[i].x, hh = b[i].y; for (int j = gg + 1; j < hh && m > 0; j++) if (a[j] == L ) { a[j] = W ; m--; } } for (int i = o; i >= 0 && m > 0; i--) if (a[i] == L ) { a[i] = W ; m--; } for (int i = q; i < n && m > 0; i++) if (a[i] == L ) { a[i] = W ; m--; } int s = 0; for (int i = 0; i < n; i++) if (a[i] == W ) { if (i == 0 || a[i - 1] != W ) s++; else s += 2; } printf( %d n , s); } int main() { int t; cin >> t; while (t--) { solve(); } }
|
#include <bits/stdc++.h> struct point { int x, l; } pp[1000000 * 2]; int qq[1000000 * 2], sk; int pcompare(const void *a, const void *b) { struct point *pa = (struct point *)a; struct point *pb = (struct point *)b; return pa->x != pb->x ? pa->x - pb->x : pb->l - pa->l; } void sort(struct point *pp, int n) { int i; srand(time(NULL)); for (i = n - 1; i >= 0; i--) { struct point tmp; int j = rand() % (i + 1); tmp = pp[i], pp[i] = pp[j], pp[j] = tmp; } qsort(pp, n, sizeof *pp, pcompare); } void sweep(struct point *pp, int n, int k) { int d, i; d = 0; for (i = 0; i <= n; i++) if (pp[i].l) { d++; if (d == k) qq[sk++] = pp[i].x; } else { d--; if (d == k - 1) qq[sk++] = pp[i].x; } printf( %d n , sk / 2); for (i = 0; i < sk; i++) printf( %d%c , qq[i], i % 2 == 0 ? : n ); } int main() { int n, k, i; scanf( %d%d , &n, &k); for (i = 0; i < n; i++) { int l, r; scanf( %d%d , &l, &r); pp[i * 2].x = l, pp[i * 2].l = 1; pp[i * 2 + 1].x = r, pp[i * 2 + 1].l = 0; } sort(pp, n * 2); sk = 0; sweep(pp, n * 2, k); return 0; }
|
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); long long f[110][110]; long long n, k; char s[110]; long long ch2i[225]; long long ich2i[110][225]; int main() { cin >> n >> k; scanf( %s , s); for (int i = a ; i < z + 1; i++) { ch2i[i] = -1; } for (int i = n - 1; i >= 0; i--) { for (int ch = a ; ch < z + 1; ch++) { ich2i[i][ch] = ch2i[ch]; } ch2i[s[i]] = i; } f[0][0] = 1; for (int i = 0; i < n; i++) { f[i][1] = 1; } for (int len = 2; len < n + 1; len++) { for (int i = 0; i < n - len + 1; i++) { for (int j = i + 1; j < n - len + 2; j++) { if (ich2i[i][s[j]] != j) continue; f[i][len] += f[j][len - 1]; } } } long long cnt = 0; long long ans = 0; for (int len = n + 1 - 1; len >= 0; len--) { for (int i = 0; i < n - len + 1; i++) { if (i != ch2i[s[i]]) continue; cnt += f[i][len]; ans += (n - len) * f[i][len]; if (cnt >= k) { ans -= (cnt - k) * (n - len); cout << ans << endl; return 0; } } } cout << -1 << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long a, b, sol; int main() { sol = 1; scanf( %lld%lld , &a, &b); sol *= ((1LL) << __builtin_popcountll(b)); if (((a - b) & 1) || (((a - b) >> 1) & b)) { puts( 0 ); return 0; } if (a == b) sol -= 2; if (!(a & 1) && !b) { puts( 1 ); return 0; } cout << sol; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 110, MAXK = 22, mod = 1000000007; struct edge { int adj, next; } e[MAXN << 1]; int n, k, f[MAXN][MAXK << 1]; int tot, head[MAXN], temp[MAXK << 1]; inline void AddEdge(int u, int v) { tot++; e[tot].adj = v, e[tot].next = head[u]; head[u] = tot; } inline void Init() { scanf( %d%d , &n, &k); for (int i = 1; i < n; i++) { int u, v; scanf( %d%d , &u, &v); AddEdge(u, v), AddEdge(v, u); } } void DFS(int u, int fa) { f[u][0] = f[u][k + 1] = 1; for (int i = head[u]; i; i = e[i].next) { int &v = e[i].adj; if (v == fa) continue; DFS(v, u); memset(temp, 0, sizeof(temp)); for (int i = 0; i <= 2 * k; i++) for (int j = 0; j <= 2 * k; j++) { if (i + j + 1 <= 2 * k + 1) temp[min(i, j + 1)] = (temp[min(i, j + 1)] + (long long)f[u][i] * f[v][j]) % mod; else temp[max(i, j + 1)] = (temp[max(i, j + 1)] + (long long)f[u][i] * f[v][j]) % mod; } memcpy(f[u], temp, sizeof(temp)); } } int main() { Init(); DFS(1, 0); int ans = 0; for (int i = 0; i <= k; i++) ans = (ans + f[1][i]) % mod; printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = int(500 * 1000 + 7); int n, k; vector<pair<int, int> > adj[N]; long long dp[N][2]; void dfs(int v, int p = -1) { vector<long long> x; long long curr = 0; for (auto it : adj[v]) { int to = it.first; int w = it.second; if (to == p) continue; dfs(to, v); curr += dp[to][0]; x.push_back(dp[to][1] + w - dp[to][0]); } sort(x.begin(), x.end(), greater<long long>()); for (int i = 0; i < min((int)x.size(), k); i++) { if (x[i] > 0) curr += x[i]; } dp[v][0] = dp[v][1] = curr; if (k <= (int)x.size() && x[k - 1] > 0) dp[v][1] -= x[k - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int q; cin >> q; while (q--) { cin >> n >> k; for (int i = 0; i < n; i++) adj[i].clear(); for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } dfs(0, -1); cout << dp[0][0] << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const long long ooo = 9223372036854775807ll; const int _cnt = 1000 * 1000 + 7; const int _p = 1000 * 1000 * 1000 + 7; const int N = 200005; const double PI = acos(-1.0); const double eps = 1e-9; int o(int x) { return x % _p; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void file_put() { freopen( filename.in , r , stdin); freopen( filename.out , w , stdout); } long long Pow(long long x, long long k) { long long ans = 1; for (; k; k >>= 1, x = x * x % _p) if (k & 1) (ans *= x) %= _p; return ans; } long long calc(int x, int k, int m) { return (Pow(10, k) * Pow(100 - 9 * x, m) % _p - Pow(91 - 9 * x, m)) % _p * x % _p * Pow(9, _p - 2) % _p; } char st[N]; int n, cnt[10], s = 0; long long ans = 0; int main() { scanf( %s , st + 1); n = strlen(st + 1); for (int i = (1); i <= (n); ++i) { for (int j = (0); j <= (st[i] - 0 - 1 + (i == n)); ++j) { cnt[j]++, s = 0; for (int k = (0); k <= (9); ++k) { s += cnt[k]; (ans += Pow(10, i - s) * calc(k, cnt[k], n - i) % _p) %= _p; } cnt[j]--; } cnt[st[i] - 0 ]++; } printf( %I64d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, c; long long t, a; scanf( %d%lld%d , &n, &t, &c); int ans = 0, tmp = 0; for (int i = 0; i < n; i++) { scanf( %lld , &a); if (a > t) { tmp = 0; } else { tmp++; if (tmp >= c) { ans++; } } } printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, p, q, mx = 0, sm = -1, smc = 0, cnt = 0, n; scanf( %d , &n); int arr[n + 5]; int a[100050]; memset(a, 0, sizeof(a)); for (i = 0; i < n; i++) { scanf( %d , &p); a[p]++; arr[i] = p; } sort(arr, arr + n); for (i = n - 1; i >= 0; i--) { if (a[arr[i]] % 2) { return !printf( Conan n ); } } printf( Agasa n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; long long mas[200100], k, s = 0, d; long long n, p = 1, del = 0, a, b, c; unsigned long long inf = (unsigned long long)1000000000 * 1000000000 * (long long)10; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) scanf( %I64d , &mas[i]); for (int i = 1; i <= n; i++, p++) { a = (p - 1); b = (n - del - p); c = mas[i]; if (a * b > (s - k) / c) { printf( %d n , i); p--; del++; } else { if (s < inf) s += (p - 1) * mas[i]; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int t, n, a[1000], k; int main() { cin >> t; while(t--) { cin >> n >> k; if(k <= (n - 1)/2) { int spt = 0, spt1 = k + 1; for(int i = 1; i <= 2*k + 1; i++) { if(i%2 == 1) a[i] = ++spt; else a[i] = ++spt1; } for(int i = 2*k + 2; i <= n; ++i) a[i] = ++spt1; for(int i = 1; i <= n; ++i) cout << a[i] << ; cout << n ; } else cout << -1 << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; struct node { char ch; int val; int idx; node(char ch = a , int val = -1, int idx = 0) : ch(ch), val(val), idx(idx) {} }; void swap(node& a, node& b) { node temp(a.ch, a.val, a.idx); a = b; b = temp; } void disp(vector<node>& arr) { for (auto e : arr) cout << e.ch << t ; cout << n ; for (auto e : arr) cout << e.val << t ; cout << n ; for (auto e : arr) cout << e.idx << t ; cout << n ; } int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); }; long long n; cin >> n; string str; cin >> str; vector<long long> res(n, -1); vector<node> arr(n); for (int i = 0; i < n; i++) { arr[i].ch = str[i]; arr[i].idx = i; } for (int i = 0; i < n; i++) { bool isZero = 0, isOne = 0; char smallestChar = arr[i].ch; int idx = i; for (int j = i; j < n; j++) { if (int(smallestChar) > int(arr[j].ch)) { smallestChar = arr[j].ch; idx = j; } } for (int j = i; j < idx; j++) { if (arr[j].val == 0) isZero = 1; else if (arr[j].val == 1) isOne = 1; } if (isZero and isOne) { cout << NO n ; return 0; } else if (isZero) { if (arr[idx].val == 0) { cout << NO n ; return 0; } else { arr[idx].val = 1; for (int j = i; j < idx; j++) arr[j].val = 0; for (int j = idx; j > i; j--) swap(arr[j], arr[j - 1]); } } else if (isOne) { if (arr[idx].val == 1) { cout << NO n ; return 0; } else { arr[idx].val = 0; for (int j = i; j < idx; j++) arr[j].val = 1; for (int j = idx; j > i; j--) swap(arr[j], arr[j - 1]); } } else { if (arr[idx].val == 1) { for (int j = i; j < idx; j++) arr[j].val = 0; for (int j = idx; j > i; j--) swap(arr[j], arr[j - 1]); } else if (arr[idx].val == 0) { for (int j = i; j < idx; j++) arr[j].val = 1; for (int j = idx; j > i; j--) swap(arr[j], arr[j - 1]); } else { arr[idx].val = 1; for (int j = i; j < idx; j++) arr[j].val = 0; for (int j = idx; j > i; j--) swap(arr[j], arr[j - 1]); } } } cout << YES n ; for (auto e : arr) res[e.idx] = e.val; for (auto e : res) cout << e; cout << n ; return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; int t[m], l[m], r[m]; int a[n + 1]; fill_n(a, n + 1, -1); a[0] = 1e5; for (int i = 0; i < m; i++) { cin >> t[i] >> l[i] >> r[i]; if (t[i] == 1) { for (int j = l[i] + 1; j <= r[i]; j++) { a[j] = 1; } } } for (int i = 0; i < m; i++) { if (t[i] == 0) { bool ok = false; for (int j = l[i] + 1; j <= r[i]; j++) { ok |= (a[j] == -1); } if (ok == false) { cout << NO n ; return 0; } } } cout << YES n ; for (int i = 1; i <= n; i++) { a[i] += a[i - 1]; cout << a[i] << ; } cout << n ; }
|
#include <bits/stdc++.h> const int MAX = 1e3 + 11; using namespace std; char A[MAX]; int W[33]; int main() { scanf( %s , (A)); int(K); scanf( %d , &K); for (int i = 0; i < (26); ++i) scanf( %d , &(W[i])); int mx = 0, N = int(strlen(A)); int v = *max_element(W, W + 26); for (int i = 0; i < (K + 1); ++i) { int s = 0, pos = i; for (int j = 0; j < (N); ++j) { s += W[A[j] - a ] * (j + 1 + i); } for (int k = 0; k < (pos); ++k) { s += (k + 1) * v; } for (int k = (pos + N); k < (K + N); ++k) { s += (k + 1) * v; } mx = max(mx, s); } printf( %d , mx); return 0; }
|
#include <bits/stdc++.h> using namespace std; int i, j, n; int a[1005]; string s; int main() { ios::sync_with_stdio(0); cin.tie(NULL); cin >> n; n--; cin >> s; for (i = 0; i <= n; i++) a[i] = 1; for (i = 0; i < n; i++) { if (s[i] == = ) a[i + 1] = a[i]; if (s[i] == R ) a[i + 1] = a[i] + 1; if (s[i] == L ) { for (j = i; j >= 0; j--) { if ((s[j] == L && a[j] > a[j + 1]) || (s[j] == R && a[j] < a[j + 1]) || (s[j] == = && a[j] == a[j + 1])) break; if ((s[j] == L )) a[j] = a[j + 1] + 1; if ((s[j] == = )) a[j] = a[j + 1]; } } } for (i = 0; i <= n; i++) cout << a[i] << ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); template <class T> void R(T &x) { cin >> x; } void R(int &x) { scanf( %d , &x); } void R(long long &x) { scanf( %lld , &x); } void R(double &x) { scanf( %lf , &x); } void R(char &x) { scanf( %c , &x); } void R(char *x) { scanf( %s , x); } long long _pow(long long a, long long b) { long long res = 1ll; while (b) { if (b & 1) res = res * a % 1000000007; a = a * a % 1000000007; b >>= 1; } return res; } int main() { int n, k; char t[10005], s[10005]; while (~scanf( %d , &(n))) { R(k); R(t); int len = strlen(t); int i, j, flag = 0; for (i = 1; i < len; i++) { for (j = i; j < len; j++) { if (t[j] != t[j - i]) break; } if (j == len) break; } j = len; for (int o = 1; o < k; o++) { for (int x = len - i; x < len; x++) t[j++] = t[x]; } t[j] = 0 ; puts(t); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int a[111][111][11], n, q, c, x, y, rx, ry, t, v; int main() { cin >> n >> q >> c; for (int i = 1; i <= n; i++) { cin >> x >> y >> t; a[x][y][t]++; } for (int l = 0; l <= c; l++) { for (int i = 1; i <= 100; i++) for (int j = 1; j <= 100; j++) a[i][j][l] += a[i][j - 1][l]; for (int i = 1; i <= 100; i++) for (int j = 1; j <= 100; j++) a[i][j][l] += a[i - 1][j][l]; } for (int i = 1; i <= q; i++) { v = 0; cin >> t >> x >> y >> rx >> ry; for (int j = 0; j <= c; j++) v += (j + t) % (c + 1) * (a[rx][ry][j] - a[rx][y - 1][j] - a[x - 1][ry][j] + a[x - 1][y - 1][j]); cout << v << endl; } return 0; }
|
#include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<climits> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define LL long long using namespace std; LL read(){ bool f=0;LL x=0;char c=getchar(); while(c< 0 || 9 <c){if(c== - )f=1;c=getchar();} while( 0 <=c&&c<= 9 ) x=(x<<3)+(x<<1)+(c^48),c=getchar(); return !f?x:-x; } typedef pair<long double,long double> pii; #define mp make_pair #define fi first #define se second #define lch (u<<1) #define rch (u<<1|1) const long double eps=1e-10; const int MAXN=200000; const int INF=0x3f3f3f3f; LL a[MAXN+5]; vector<pair<int,LL> > Tu[5*MAXN+5]; bool K(pii p,pii a,pii b){if(p.fi==a.fi) return 1;return (p.se-a.se)/(p.fi-a.fi)>=(p.se-b.se)/(p.fi-b.fi);} void Build(int u,int L,int R){ if(L==R){ Tu[u].push_back(mp(L,a[L])); return ; } int Mid=(L+R)>>1; Build(lch,L,Mid),Build(rch,Mid+1,R); Tu[u]=Tu[lch]; int lst=Tu[u].size()-1; for(int i=0;i<Tu[rch].size();i++){ while(lst&&K(Tu[u][lst-1],Tu[u][lst],Tu[rch][i])) Tu[u].pop_back(),lst--; Tu[u].push_back(Tu[rch][i]),lst++; } return ; } pair<long double,int> P[MAXN+5]; int Query(int u,int L,int R,int qL,int qR,int pos){ if(qL<=L&&R<=qR){ int siz=Tu[u].size(); int le=0,ri=siz; while(le+1<ri){ int mid=(le+ri)>>1; if(K(mp(pos,a[pos]),Tu[u][mid-1],Tu[u][mid])) le=mid; else ri=mid; } return Tu[u][le].first; } int Mid=(L+R)>>1,ret=-1; if(qL<=Mid) ret=Query(lch,L,Mid,qL,qR,pos); if(Mid+1<=qR){ int tmp=Query(rch,Mid+1,R,qL,qR,pos); if(ret==-1||K(mp(pos,a[pos]),mp(ret,a[ret]),mp(tmp,a[tmp]))) ret=tmp; } return ret; } int n,m; void Get(int L,int R,int ad){ if(L>=R) return ; int p=Query(1,0,n,L,R,L); P[p]=mp(1.0*(a[p]-a[L])/(p-L)-eps,R-L-1+ad); //printf( %f %d %d %d n ,(double)P[p].first,p,L,R-L-1+ad); Get(L,p-1,1),Get(p,R,ad); return ; } int ans[MAXN+5]; long double k[MAXN+5]; int main(){ n=read(),m=read(); for(int i=1;i<=n;i++) a[i]=a[i-1]+read(); Build(1,0,n); Get(0,n,0); sort(P+1,P+n+1); for(int i=n;i>=1;i--) ans[i]=max(P[i].second,ans[i+1]); for(int i=1;i<=m;i++){ k[i]=read(); int t=upper_bound(P+1,P+n+1,mp(k[i],0))-P; printf( %d ,ans[t]),putchar(i==m? n : ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, ans, Max, an, x; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &x); if (Max < x) { Max = x; ans = 0; an = 0; } if (x == Max) an++; else an = 0; ans = max(ans, an); } printf( %d , ans); }
|
#include <bits/stdc++.h> int main() { unsigned short n; unsigned short ac1; unsigned short i; unsigned short w; unsigned short ac2; unsigned short gw; if (scanf( %hu , &n) != 1) return -1; if (n < 1 || n > 100) return -1; ac1 = 0; for (i = 0; i < n; i++) { if (scanf( %hu , &w) != 1) return -1; if (w != 100 && w != 200) return -1; if (w == 100) ac1++; } ac2 = n - ac1; gw = 50 * (ac1 + 2 * ac2); if (!(gw % 200) || (!(gw % 100) && ac1)) printf( YES n ); else printf( NO n ); return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; const int maxN = 1005; long long x[maxN], y[maxN]; long long det(int i, int j, int k) { return (x[j] - x[i]) * (y[k] - y[i]) - (x[k] - x[i]) * (y[j] - y[i]); } long long askArea(int i, int j, int k) { cout << 1 << << i << << j << << k << endl; long long resp; cin >> resp; return resp; } int askVec(int i, int j, int k) { cout << 2 << << i << << j << << k << endl; int res; cin >> res; return res; } int n; int main() { cin >> n; int who = n; vector<int> up, down; for (int i = 2; i < n; i++) { if (askVec(1, i, who) < 0) { up.emplace_back(i); } else { down.emplace_back(i); } } vector<pair<long long, int> > get_vals; for (int v : up) { get_vals.emplace_back(askArea(1, v, who), v); } sort(get_vals.begin(), get_vals.end()); reverse(get_vals.begin(), get_vals.end()); deque<int> ans_up; if (!get_vals.empty()) ans_up.push_back(get_vals[0].second); for (int i = 1; i < get_vals.size(); i++) { int p = get_vals[i].second; if (askVec(1, p, ans_up.front()) < 0) ans_up.push_front(p); else ans_up.push_back(p); } vector<int> perm; for (int v : ans_up) perm.push_back(v); perm.push_back(who); get_vals.clear(); deque<int> ans_down; for (int v : down) { get_vals.emplace_back(askArea(1, v, who), v); } sort(get_vals.begin(), get_vals.end()); reverse(get_vals.begin(), get_vals.end()); if (!get_vals.empty()) ans_down.push_back(get_vals[0].second); for (int i = 1; i < get_vals.size(); i++) { int p = get_vals[i].second; if (askVec(1, p, ans_down.front()) < 0) ans_down.push_front(p); else ans_down.push_back(p); } for (int v : ans_down) perm.push_back(v); reverse(perm.begin(), perm.end()); cout << 0 << ; cout << 1 << ; for (int v : perm) cout << v << ; cout << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; 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 long long xx = 2e7; const long long MOD = 1e9 + 7; const long long inf = 1e18; long long isprime(long long n) { if (n == 2) return 1; if (n % 2 == 0) return 0; for (long long i = 3; i * i <= n; i += 2) { if (n % i == 0) return 0; } return 1; } signed main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; if (isprime(n)) { cout << 1 << n ; } else if (n % 2 == 0 || isprime(n - 2)) { cout << 2 << n ; } else cout << 3 << n ; }
|
#include <bits/stdc++.h> using namespace std; constexpr int maxn = 2e5 + 10, N = 260, M = 5e6 + 10; constexpr int MAX_INF = 0x3f3f3f3f; int mp[maxn]; int main() { int T; cin >> T; while (T--) { int n, m; scanf( %d%d , &n, &m); int root = 0; memset(mp, 0, sizeof(mp)); while (m--) { int a, b, c; scanf( %d%d%d , &a, &b, &c); mp[b] = 1; } for (int i = 1; i <= n; i++) { if (!mp[i]) { root = i; break; } } for (int i = 1; i <= n; i++) { if (i == root) continue; printf( %d %d n , i, root); } } }
|
#include <bits/stdc++.h> using namespace std; int n, hp = 0, row[100], col[100]; char cake[101][101]; int main() { for (int i = 0; i < 100; ++i) { row[i] = 0, col[i] = 0; } cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> cake[i][j]; if (cake[i][j] == C ) { ++row[j]; ++col[i]; } } } for (int i = 0; i < n; i++) { hp += (row[i] * (row[i] - 1)) / 2; hp += (col[i] * (col[i] - 1)) / 2; } cout << hp; return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, a[100001], b[100001]; void solve(int first) { vector<int> ans(1, first); for (int i = 1; i < n; i++) { int pre = ans.back(); for (int j = 0; j <= 3; j++) { if ((pre & j) == b[i] && (pre | j) == a[i]) { ans.push_back(j); break; } } } if (ans.size() != n) return; cout << YES n ; for (int x : ans) cout << x << ; exit(0); } int main() { cin >> n; for (int i = 1; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) cin >> b[i]; for (int i = 0; i <= 3; i++) solve(i); cout << NO ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m; int a[111]; int b[111]; int main() { cin >> n >> m; int s = 0; for (int i = 0; i < n; i++) { cin >> a[i]; s ^= a[i]; } int tmp = 0; for (int i = 0; i < m; i++) { cin >> b[i]; tmp ^= b[i]; } if (tmp != s) { cout << NO ; return 0; } cout << YES << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i == 0 && j == 0) { cout << (tmp ^ a[0] ^ b[0]) << ; } else if (i == 0) { cout << b[j] << ; } else if (j == 0) { cout << a[i] << ; } else { cout << 0 << ; } } cout << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, temp; cin >> n; string s; cin >> s; temp = n; for (int i = n - 2; i >= 0; i--) { if (s.substr(i, 2) == 10 || s.substr(i, 2) == 01 ) { temp -= 2; s.erase(i, 2); i++; } } cout << temp; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; map<int, bool> a; for (int i = 0, j; i < n; i++) { cin >> j; a[j] = 1; } int k = 0; vector<int> l; for (int i = 1; m - i >= 0; i++) if (!a[i]) { l.push_back(i); m -= i; } cout << l.size() << endl; for (int i = 0; i < l.size(); i++) cout << l[i] << ; }
|
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m, q, t = 1; int lson[N * 2], rson[N * 2], vm[2][N * 2], ovl[N * 2]; set<pair<int, int> > sm[2][N * 2]; map<int, int> op[N * 2]; void build(int pos, int x, int y) { vm[1][pos] = m + 1; if (x == y) return; int mid = (x + y) >> 1; lson[pos] = ++t; rson[pos] = ++t; build(lson[pos], x, mid); build(rson[pos], mid + 1, y); } inline int MAX(int knd, int pos) { if (sm[knd][pos].empty()) return 0; set<pair<int, int> >::iterator it = sm[knd][pos].end(); it--; return it->first; } inline int MIN(int knd, int pos) { if (sm[knd][pos].empty()) return m + 1; return sm[knd][pos].begin()->first; } void update(int pos) { vm[0][pos] = MAX(0, pos); if (lson[pos]) vm[0][pos] = max(vm[0][pos], vm[0][lson[pos]]); if (rson[pos]) vm[0][pos] = max(vm[0][pos], vm[0][rson[pos]]); vm[1][pos] = MIN(1, pos); if (lson[pos]) vm[1][pos] = min(vm[1][pos], vm[1][lson[pos]]); if (rson[pos]) vm[1][pos] = min(vm[1][pos], vm[1][rson[pos]]); ovl[pos] = (MIN(1, pos) <= vm[0][pos]) | (MAX(0, pos) >= vm[1][pos]); ovl[pos] |= ovl[lson[pos]] | ovl[rson[pos]]; } void modify(int knd, int pos, int l, int r, int x, int y, pair<int, int> v) { if (l >= x && r <= y) { if (knd == 0) { if (sm[knd][pos].find(v) != sm[knd][pos].end()) sm[knd][pos].erase(v); else sm[knd][pos].insert(v); } else { if (sm[knd][pos].find(v) != sm[knd][pos].end()) sm[knd][pos].erase(v); else sm[knd][pos].insert(v); } update(pos); return; } if (l > y || r < x) return; int mid = (l + r) >> 1; modify(knd, lson[pos], l, mid, x, y, v); modify(knd, rson[pos], mid + 1, r, x, y, v); update(pos); } int main() { scanf( %d%d%d , &n, &m, &q); build(1, 1, n); for (int i = 1; i <= q; i++) { int x, y; scanf( %d%d , &x, &y); if (op[x][y] == 0) { op[x][y] = i; if (x % 2 == 0) modify(0, 1, 1, n, 1, x / 2, make_pair(y / 2, i)); else modify(1, 1, 1, n, (x + 1) / 2, n, make_pair((y + 1) / 2, i)); } else { if (x % 2 == 0) modify(0, 1, 1, n, 1, x / 2, make_pair(y / 2, op[x][y])); else modify(1, 1, 1, n, (x + 1) / 2, n, make_pair((y + 1) / 2, op[x][y])); op[x][y] = 0; } if (ovl[1]) puts( NO ); else puts( YES ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; void solve() { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2 && y1 == y2) { cout << 10 ; return; } if (x1 == x2) { cout << 2 * (abs(y2 - y1) + 1) + 4; return; } if (y1 == y2) { cout << 2 * (abs(x2 - x1) + 1) + 4; return; } cout << 2 * (abs(x2 - x1) + abs(y2 - y1) + 2); } int main() { solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; template <class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } const int N = 1 << 7, M = 1 << 14; int a[M], b[M], c[M]; bool val[N], vis[N]; vector<int> g[N]; int n, m; bool dfs(int u, bool x) { val[u] = x; vis[u] = true; for (int e : g[u]) { int v = a[e] ^ b[e] ^ u; bool new_x = c[e] ^ 1 ^ x; if (!vis[v]) { if (!dfs(v, new_x)) return false; } else if (new_x != val[v]) return false; } return true; } bool solve(bool init) { fill(vis, vis + n, false); fill(val, val + n, false); for (auto u = (0); u < (n); ++u) if (!vis[u] && !dfs(u, init)) return false; return true; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (auto e = (0); e < (m); ++e) { cin >> a[e] >> b[e] >> c[e], --a[e], --b[e]; g[a[e]].push_back(e); g[b[e]].push_back(e); } if (solve(0) || solve(1)) { int cnt = 0; for (auto u = (0); u < (n); ++u) cnt += val[u]; cout << cnt << n ; for (auto e = (0); e < (m); ++e) assert(c[e] ^ val[a[e]] ^ val[b[e]]); for (auto u = (0); u < (n); ++u) if (val[u]) cout << u + 1 << ; } else cout << Impossible ; }
|
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int M = 400005; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 0 ; ch = getchar(); } return f * x; } int n, m, q, num; int hd[N], nxt[M], to[M], tot; inline void add(int x, int y) { to[++tot] = y; nxt[tot] = hd[x]; hd[x] = tot; } int dfn[N], low[N], dfn_cnt; int st[N], tp; bool vis[N]; vector<int> G[N]; void tarjan(int x) { vis[x] = 1; st[++tp] = x; dfn[x] = low[x] = ++dfn_cnt; for (int i = hd[x]; i; i = nxt[i]) { int y = to[i]; if (!dfn[y]) { tarjan(y); low[x] = min(low[x], low[y]); if (low[y] == dfn[x]) { ++num; while (st[tp + 1] != y) { G[num].push_back(st[tp]); G[st[tp]].push_back(num); vis[st[tp--]] = 0; } G[num].push_back(x); G[x].push_back(num); } } else if (vis[y]) low[x] = min(low[x], dfn[y]); } } multiset<int> s[N]; int siz[N], fa[N], dep[N], son[N]; void dfs_son(int x, int f) { fa[x] = f; siz[x] = 1; dep[x] = dep[f] + 1; for (int i = 0; i < G[x].size(); i++) { int y = G[x][i]; if (y == f) continue; dfs_son(y, x); siz[x] += siz[y]; if (siz[y] > siz[son[x]]) son[x] = y; } } int rev[N], top[N]; void dfs_chain(int x, int tp) { top[x] = tp; dfn[x] = ++dfn_cnt; rev[dfn_cnt] = x; if (son[x]) dfs_chain(son[x], tp); for (int i = 0; i < G[x].size(); i++) { int y = G[x][i]; if (y == fa[x] || y == son[x]) continue; dfs_chain(y, y); } } int val[N << 2]; int w[N]; void build(int l, int r, int p) { if (l == r) { val[p] = w[rev[l]]; return; } build(l, ((l + r) >> 1), (p << 1)); build(((l + r) >> 1) + 1, r, (p << 1 | 1)); val[p] = min(val[(p << 1)], val[(p << 1 | 1)]); } void modify(int l, int r, int t, int v, int p) { if (l == r) { val[p] = v; return; } if (t <= ((l + r) >> 1)) modify(l, ((l + r) >> 1), t, v, (p << 1)); else modify(((l + r) >> 1) + 1, r, t, v, (p << 1 | 1)); val[p] = min(val[(p << 1)], val[(p << 1 | 1)]); } int query(int l, int r, int L, int R, int p) { if (L <= l && r <= R) return val[p]; int ans = 0x3f3f3f3f; if (L <= ((l + r) >> 1)) ans = min(ans, query(l, ((l + r) >> 1), L, R, (p << 1))); if (R > ((l + r) >> 1)) ans = min(ans, query(((l + r) >> 1) + 1, r, L, R, (p << 1 | 1))); return ans; } int get_min(int x, int y) { int ans = 0x3f3f3f3f; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); ans = min(ans, query(1, num, dfn[top[x]], dfn[x], 1)); x = fa[top[x]]; } if (dfn[x] > dfn[y]) swap(x, y); ans = min(ans, query(1, num, dfn[x], dfn[y], 1)); if (x > n) ans = min(ans, w[fa[x]]); return ans; } int main() { n = read(); m = read(); q = read(); for (int i = 1; i <= n; i++) w[i] = read(); num = n; for (int i = 1, x, y; i <= m; i++) { x = read(); y = read(); add(x, y), add(y, x); } tarjan(1); dfn_cnt = 0; dfs_son(1, 0); dfs_chain(1, 1); for (int i = 1; i <= n; i++) if (fa[i]) s[fa[i]].insert(w[i]); for (int i = n + 1; i <= num; i++) w[i] = *s[i].begin(); memset(val, 0x3f, sizeof(val)); build(1, num, 1); char op; int x, y; while (q--) { cin >> op; x = read(); y = read(); if (op == C ) { modify(1, num, dfn[x], y, 1); if (fa[x]) { s[fa[x]].erase(s[fa[x]].lower_bound(w[x])); s[fa[x]].insert(y); if (w[fa[x]] != *s[fa[x]].begin()) { w[fa[x]] = *s[fa[x]].begin(); modify(1, num, dfn[fa[x]], w[fa[x]], 1); } } w[x] = y; } else printf( %d n , get_min(x, y)); } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T, typename TT> ostream& operator<<(ostream& s, pair<T, TT> t) { return s << ( << t.first << , << t.second << ) ; } template <typename T> ostream& operator<<(ostream& s, vector<T> t) { for (int(i) = 0; (i) < (sz(t)); ++(i)) s << t[i] << ; return s; } int r, n; long long MOD = 1000003; long long qp(long long a, long long b) { long long x = 1; while (b) { if (b & 1) x = x * a % MOD; a = a * a % MOD; b >>= 1; } return x; } int main() { cin >> r >> n; long long ans = 0, cur = 1; for (int a = n, b = 1; b <= r; a++, b++) { cur = cur * a % MOD * qp(b, MOD - 2) % MOD; ans = (ans + cur) % MOD; } cout << ans << n ; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string name, a; vector<pair<int, string> > s; vector<pair<int, string> > v(n); for (int i = 0; i < (int)n; i++) cin >> v[i].second >> v[i].first; sort(v.begin(), v.end()); int h = 1e9; for (int i = 0; i < (int)n; i++) { if (v[i].first > (int)s.size()) { cout << -1 << endl; return 0; } if (i && v[i - 1].first != v[i].first) h--; s.insert(s.begin() + v[i].first, make_pair(h, v[i].second)); } for (int i = 0; i < (int)s.size(); i++) cout << s[i].second << << s[i].first << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n, k; cin >> n >> k; long long int ans = 0; if (n < k) { cout << n << endl; continue; } while (n != 0) { if (n % k == 0) { n = n / k; ans++; } else { long long x = n % k; n -= x; ans += x; } } cout << ans << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 111; const double INF = 1e200; double dp[N][10 * N]; struct problem { int a, p; bool operator<(const problem &o) const { return a > o.a; } } p[N]; int n; double C, T; double solve(double x) { x /= 0.9; auto t = (sqrt(C * x) - 1) / C; t = max(t, 0.0); return x / (1 + C * t) + t; } int main() { int tc; cin >> tc; while (tc--) { cin >> n >> C >> T; for (int i = 0; i < n; i++) cin >> p[i].a >> p[i].p; sort(p, p + n); fill(dp[0], dp[N], INF); dp[0][0] = 0; int sum = 0; for (int i = n - 1; i >= 0; i--) { for (int k = n - 1 - i; k >= 0; k--) { for (int s = sum; s >= 0; s--) if (dp[k][s] < INF) { dp[k + 1][s + p[i].p] = min(dp[k + 1][s + p[i].p], dp[k][s] / 0.9 + p[i].a); } } sum += p[i].p; } int ans = 0; for (int s = sum; s > 0; s--) { for (int k = 0; k <= n; k++) { if (dp[k][s] < INF) { auto cost = k * 10 + solve(dp[k][s]); if (cost <= T) { ans = max(ans, s); } } } } cout << ans << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, a[101], ans = 0; cin >> n; bool flag = false; for (int i = 1; i <= n; i += 1) { cin >> a[i]; if (a[i] == 100) flag = true; ans += a[i]; } if (ans % 200 == 100) cout << NO << endl; else { if (ans % 400 == 0) cout << YES << endl; else if (flag) cout << YES << endl; else cout << NO << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 10 * 100 * 1000 + 10; vector<int> vec[N]; int bit[N]; int n, c; void addbit(int i, int p) { for (i++; i <= c; i += (i & -i)) bit[i] += p; } int get(int i) { int x = 0; for (i++; i > 0; i -= (i & -i)) x += bit[i]; return x; } void add(int l, int r) { addbit(l, 1); addbit(r, -1); } int main() { cin >> n >> c; for (int i = 0; i < n; i++) { int t; cin >> t; for (int j = 0; j < t; j++) { int a; cin >> a; vec[i].push_back(a - 1); } } for (int i = 0; i < n - 1; i++) { int j = 0, k = 0; while (j < (int)vec[i].size() && k < (int)vec[i + 1].size() && vec[i][j] == vec[i + 1][k]) j++, k++; if (j == (int)vec[i].size()) add(0, c); else if (k == (int)vec[i + 1].size()) { cout << -1 n ; return 0; } else { int a = vec[i][j], b = vec[i + 1][k]; int li = (c - a) % c, ri = (c - b) % c; if (li < ri) add(li, ri); else add(li, c), add(0, ri); } } for (int i = 0; i < c; i++) if (get(i) >= n - 1) { cout << i << endl; return 0; } cout << -1 << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, i; scanf( %i , &n); int a = n / 3, b = n - a; for (i = 1; i <= b; i++) printf( 1 %i n , i); for (i = 1; i <= a; i++) printf( 4 %i n , i * 2); return 0; }
|
#include <bits/stdc++.h> using namespace std; int n; string s, ret; bool dfs(int idx, int four, int seven, bool flag) { if (idx == n) return true; if (flag) { for (int i = 0; i < four; i++) ret[idx + i] = 4 ; for (int i = four; i < four + seven; i++) ret[idx + i] = 7 ; return true; } if (four > 0 && s[idx] <= 4 && dfs(idx + 1, four - 1, seven, s[idx] < 4 )) { ret[idx] = 4 ; return true; } if (seven > 0 && s[idx] <= 7 && dfs(idx + 1, four, seven - 1, s[idx] < 7 )) { ret[idx] = 7 ; return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> s; n = s.length(); ret.resize(n); if (n % 2 == 1 || !dfs(0, n / 2, n / 2, false)) { int half = (n + 2) / 2; for (int i = 0; i < half; i++) cout << 4 ; for (int i = 0; i < half; i++) cout << 7 ; cout << n ; } else cout << ret << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; inline void Routine() { srand(time(NULL)); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } template <class T, class U> istream &operator>>(istream &in, pair<T, U> &p) { in >> p.fi >> p.se; return in; } template <class T, class U> ostream &operator<<(ostream &out, const pair<T, U> &p) { out << p.first << << p.second; return out; } template <class T> istream &operator>>(istream &in, vector<T> &v) { for (auto &i : v) { in >> i; } return in; } template <class T> ostream &operator<<(ostream &out, const vector<T> &v) { for (auto &i : v) { out << i << n ; } return out; } const long long mod = 1e9 + 7; int main() { Routine(); long long q, l1, l2, r1, r2, i; cin >> q; for (i = 0; i < q; i++) { cin >> l1 >> r1 >> l2 >> r2; if (l1 != r2) { cout << l1 << << r2 << n ; } else { cout << r1 << << l2 << n ; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int mod = 1e9 + 7; using namespace std::chrono; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; string s; cin >> s; long long int a[26]; for (int i = 0; i < 26; i++) cin >> a[i]; long long int maxx = -1, len = 0, minn; vector<long long int> dp(n + 1, 0), dp2(n + 1, 1e12); for (int i = 0; i < n; i++) { minn = 1e12; for (int j = i; j >= 0; j--) { len = i - j + 1; minn = min(minn, a[s[j] - a ]); if (minn < len) break; long long int x = j - 1 >= 0 ? dp[j - 1] : 1; dp[i] = (dp[i] + x) % mod; maxx = max(maxx, len); x = j - 1 >= 0 ? dp2[j - 1] : 0; dp2[i] = min(dp2[i], x + 1); } } cout << dp[n - 1] % mod << endl << maxx << endl << dp2[n - 1]; return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<string> s; vector<pair<int, int> > v; int gf = 0; int x[9] = {-1, -1, -1, 0, 1, 1, 1, 0, 0}; int y[9] = {-1, 0, 1, 1, 1, 0, -1, -1, 0}; void dfs(int a, int b, int le) { if (gf) return; if (le == 8 || (a == 0 && b == 7)) { gf = 1; return; } int a1, b1, i, j, k, x1, y1, flag, x2, y2; for (i = 0; i < 9; i++) { flag = 0; a1 = a + x[i], b1 = b + y[i]; if (a1 >= 0 && a1 < 8 && b1 >= 0 && b1 < 8) { for (k = 0; k < v.size(); k++) { x1 = v[k].first + (le + 1); y1 = v[k].second; x2 = v[k].first + (le); y2 = v[k].second; if ((a1 == x1 && b1 == y1) || (a1 == x2 && b1 == y2)) flag = 1; } if (flag == 0) dfs(a1, b1, le + 1); } } } int main() { string st; int i; for (i = 0; i < 8; i++) { cin >> st; s.push_back(st); } int j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { if (s[i][j] == S ) v.push_back(make_pair(i, j)); } } dfs(7, 0, 0); if (gf) cout << WIN << n ; else cout << LOSE << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; struct Wall { int l, r, t; void init(int _l, int _r, int _t) { l = _l, r = _r, t = _t; } void input() { scanf( %d%d%d , &l, &r, &t); } static bool by_l(const Wall &a, const Wall &b) { return a.l < b.l; } bool operator<(const Wall &a) const { return t > a.t; } } w[500010], wc[500010]; struct Table { int a[500010 * 2], top; Table() { top = 0; } void insert(int x) { a[top++] = x; } void sort() { std::sort(a, a + top); top = unique(a, a + top) - a; } int search(int x) { return lower_bound(a, a + top, x) - a; } } table; struct Tri { int x, p; void init(int _x, int _p) { x = _x, p = _p; } bool operator<(const Tri &a) const { return x < a.x; } } tri[500010]; int q[500010]; int main() { int n, m; cin >> m >> n; for (int i = 0; i < n; ++i) { w[i].input(); } for (int i = 0; i < m; ++i) { scanf( %d , q + i); } sort(w, w + n, Wall::by_l); for (int i = 0; i < n; ++i) { table.insert(w[i].l), table.insert(w[i].r); } table.sort(); priority_queue<Wall> que; int nc = 0; for (int i = 0, j = 0; i < table.top - 1; ++i) { while (j < n && w[j].l <= table.a[i]) { que.push(w[j++]); } while (!que.empty()) { Wall wall = que.top(); if (wall.r > table.a[i]) { wc[nc++].init(table.a[i], table.a[i + 1], wall.t); break; } que.pop(); } } int ntri = 0; for (int i = 0; i < nc; ++i) { tri[ntri++].init(wc[i].t - wc[i].r, 1); tri[ntri++].init(wc[i].t - wc[i].l, -1); } sort(tri, tri + ntri); int sgm = 0, cnt = 0; for (int i = 0, j = 0; i < m; ++i) { while (j < ntri && tri[j].x <= q[i]) { cnt += tri[j].p; sgm += tri[j].x * tri[j].p; ++j; } printf( %d n , q[i] * cnt - sgm); } return 0; }
|
#include <bits/stdc++.h> int main() { int n; scanf( %d , &n); int a[200001]; int i; for (i = 0; i < n; i++) { scanf( %d , &a[i]); } long long buy = a[n - 1]; int pre = a[n - 1]; for (i = n - 2; i >= 0; i--) { if (a[i] >= pre - 1) { if (pre - 1 == 0) break; else buy += pre - 1; pre--; } else { buy += a[i]; pre = a[i]; } } printf( %lld n , buy); return 0; }
|
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); set<string> s; s.insert( don t even ); s.insert( worse ); s.insert( terrible ); s.insert( no way ); s.insert( go die in a hole ); s.insert( are you serious ); int c = 0; while (true) { cout << c++ << endl; string t; getline(cin, t); if (t != no ) { if (s.count(t) == 0) { cout << normal << endl; return 0; } else { cout << grumpy << endl; return 0; } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int max = a[0]; int pos = 0; for (int i = 0; i < n; i++) { if (max < a[i]) { max = a[i]; pos = i; } } bool k = true; for (int i = pos; i > 0; i--) { if (a[i - 1] >= a[i]) { k = false; } } for (int i = pos; i < n - 1; i++) { if (a[i] <= a[i + 1]) { k = false; } } if (k == true) printf( YES n ); else printf( NO n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<vector<int>> sum; vector<vector<bool>> used; int n, m; int g = 0; int k = 0; vector<pair<int, int>> p = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void dfs(int first, int second, const int &r1, const int &r2, const int &l1, const int &l2) { if (first <= r1 || first > r2 || second <= l1 || second > l2 || used[first][second]) return; used[first][second] = true; k++; for (int i = 0; i < 4; i++) dfs(first + p[i].first, second + p[i].second, r1, r2, l1, l2); } bool f(int r) { r = r * 2 + 1; k = 0; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) used[i][j] = false; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (i >= r && j >= r && sum[i][j] - sum[i - r][j] - sum[i][j - r] + sum[i - r][j - r] == r * r) { dfs(i, j, i - r, i, j - r, j); } if (k == g) return true; else return false; } int main() { cin >> n >> m; sum.resize(n + 1, vector<int>(m + 1, 0)); used.resize(n + 1, vector<bool>(m + 1, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char c; cin >> c; sum[i + 1][j + 1] = sum[i][j + 1] + sum[i + 1][j] - sum[i][j]; if (c == X ) sum[i + 1][j + 1]++; } g = sum.back().back(); int l = 0, r = min(n / 2 + 2, m / 2 + 2); while (l + 1 < r) { int c = (l + r) / 2; if (f(c)) l = c; else r = c; } cout << l << n ; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (i >= l + 1 && j >= l + 1 && i + l <= n && j + l <= m && sum[i + l][j + l] - sum[i - l - 1][j + l] - sum[i + l][j - l - 1] + sum[i - l - 1][j - l - 1] == (2 * l + 1) * (2 * l + 1)) cout << X ; else cout << . ; } cout << n ; } cin >> n; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int sum = 0, odd = 0, even = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (x % 2 == 0) { even++; } else { odd++; } sum += x; } if (sum % 2 == 1) { cout << YES n ; } else { if (odd < n && odd > 0) { cout << YES n ; } else { cout << NO n ; } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int INF = (int)1E9; const long long LINF = (long long)1E18; int INP, AM; char BUF[(1 << 10) + 1], *inp = BUF; template <typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); } template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } template <typename T> inline T sqr(T x) { return x * x; } const char DEBUG_PARAM[] = __LOCAL_TESTING ; const char IN[] = input.txt ; const char OUT[] = output.txt ; int ntest = 0, test; inline void init(); inline void run(); inline void stop() { ntest = test - 1; } int main(int argc, char* argv[]) { if (argc > 1 && strcmp(argv[1], DEBUG_PARAM) == 0) { freopen(IN, r , stdin); } init(); if (ntest == 0) { puts( ntest = ? ); return 0; } for (test = 1; test <= ntest; test++) { run(); } return 0; } const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; const long double EPS = 1E-9; const long long MODULO = 1000000007LL; inline void init() { ntest = 1; } double s; void run() { cin >> s; double a, b, c; cin >> a >> b >> c; if (a == 0 && b == 0 && c == 0) { cout << 0 0 0 ; return; } double sum = a + b + c; printf( %.20lf %.20lf %.20lf , a / sum * s, b / sum * s, c / sum * s); }
|
#include <bits/stdc++.h> #include <iomanip> // std::setprecision #define ll long long const long modulo=1000000007; using namespace std; int ctoi(char c) {return c- 0 ;} int compare(pair <int,pair<int,int>> one, pair <int,pair<int,int>> two){ if(one.first<two.first) return 1; return 0; } ll power(ll a, ll b){ ll res=1; while(b!=0){ if(b%2==1){ res*=a; //res%=modulo; } a*=a; //a%=modulo; b/=2; } return res; } int dp[501][501][21]={0}; int n,m,k; int dn[501][501]; int rightt[501][501]; int explore(int q,int i,int j){ if(q==0){ return 0; } if(dp[i][j][q]!=0){ //cout<< 1 ; return dp[i][j][q]; } int ans = dp[i][j][k]; ans=INT_MAX; if(j<m-1) ans=min(ans,explore(q-2,i,j+1) + 2*dn[i][j]); if(j>0)ans=min(ans,explore(q-2,i,j-1) +2*dn[i][j-1]); if(i<n-1)ans=min(ans,explore(q-2,i+1,j) +2*rightt[i][j]); if(i>0)ans=min(ans,explore(q-2,i-1,j) +2*rightt[i-1][j]); //ans=min(min(explore(d,r,n-2,i,j-1,dn,right) +2*dn[i][y0], explore(d,r,n-2,i,j+1,dn,right) +2*dn[i][y]),min(explore(d,r,n-2,i-1,j,dn,right)+2*right[x0][j], explore(d,r,n-2,i+1,j,dn,right)+2*right[x][j])); dp[i][j][q]=ans; return ans; } void solve() { cin>>n>>m>>k; if(k%2!=0){ for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cout<< -1 << ; } cout<<endl; } return; } for(int i=0;i<n;i++){ for(int j=0;j<m-1;j++){ cin>>dn[i][j]; } } for(int i=0;i<n-1;i++){ for(int j=0;j<m;j++){ cin>>rightt[i][j]; } } int ans[501][501]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ ans[i][j]=explore(k,i,j); cout<<ans[i][j]<< ; } cout<<endl; } } int main() { #ifndef ONLINE_JUDGE freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll t=1;//cin>>t; while(t--){ solve(); } return 0; } //rememberhere
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.