func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int n; int c[301]; vector<int> a[301]; vector<int> lt[301]; int id[301]; int dem = 0; int d[301]; void nhap() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &c[i]); id[c[i]] = i; } for (int i = 1; i <= n; i++) { string s; cin >> s; for (int j = 0; j < s.length(); j++) { if (s[j] == 1 ) { int u = c[i]; int v = c[j + 1]; a[u].push_back(v); a[v].push_back(u); } } } } void bfs(int s) { queue<int> q; q.push(s); dem++; d[s] = 1; while (!q.empty()) { int u = q.front(); q.pop(); lt[dem].push_back(u); for (vector<int>::iterator i = a[u].begin(); i != a[u].end(); i++) { if (d[*i] == 0) { d[*i] = 1; q.push(*i); } } } } void xuly(int k) { priority_queue<int, vector<int>, greater<int> > q; vector<int> t; for (auto i : lt[k]) { q.push(i); t.push_back(id[i]); } sort(t.begin(), t.end()); for (auto i : t) { c[i] = q.top(); q.pop(); } } int main() { nhap(); for (int i = 1; i <= n; i++) { if (d[i] == 0) { bfs(i); } } for (int i = 1; i <= dem; i++) { xuly(i); } for (int i = 1; i <= n; i++) cout << c[i] << ; return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } const long long N = 1e6 + 5; long long n, m; long long a[N], b[N], x[N], y[N]; map<long long, long long> ans; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i] >> x[i]; ans[a[i]] = x[i]; } cin >> m; for (long long i = 1; i <= m; i++) { cin >> b[i] >> y[i]; ans[b[i]] = max(ans[b[i]], y[i]); } long long answer = 0; for (auto it : ans) answer += it.second; cout << answer; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 888; long long dp[maxn][12]; void solve() { long long n; scanf( %lld , &n); memset(dp, -1, sizeof(dp)); dp[0][1] = 0; vector<long long> v[4]; for (long long i = 1; i <= n; i++) { for (long long j = 0; j < 4; j++) v[j].clear(); long long k; scanf( %lld , &k); while (k--) { long long c, d; scanf( %lld %lld , &c, &d); v[c].push_back(d); } for (long long j = 1; j <= 3; j++) { sort(v[j].begin(), v[j].end()); reverse(v[j].begin(), v[j].end()); } for (long long prec = 0; prec <= 9; prec++) { if (dp[i - 1][prec] < 0) continue; long long val = LLONG_MIN / 10; if (!v[1].empty()) val = max(val, v[1][0]); if (!v[2].empty()) val = max(val, v[2][0]); if (!v[3].empty()) val = max(val, v[3][0]); if (prec == 0) val *= 2; if (val > 0) dp[i][(prec + 1) % 10] = max(dp[i][(prec + 1) % 10], dp[i - 1][prec] + val); val = LLONG_MIN / 10; if (!v[1].empty() && !v[2].empty()) { long long temp = v[1][0] + v[2][0]; long long mx = max(v[1][0], v[2][0]); if (prec == 0 || prec == 9) temp += mx; val = max(val, temp); } if (v[1].size() >= 2) { long long temp = v[1][0] + v[1][1]; long long mx = max(v[1][0], v[1][1]); if (prec == 0 || prec == 9) temp += mx; val = max(val, temp); } if (val > 0) dp[i][(prec + 2) % 10] = max(dp[i][(prec + 2) % 10], dp[i - 1][prec] + val); val = LLONG_MIN / 10; if (v[1].size() >= 3) { long long temp = v[1][0] + v[1][1] + v[1][2]; long long mx = max(v[1][0], max(v[1][2], v[1][1])); if (prec == 0 || prec == 9 || prec == 8) temp += mx; val = max(val, temp); } if (val > 0) dp[i][(prec + 3) % 10] = max(dp[i][(prec + 3) % 10], dp[i - 1][prec] + val); dp[i][prec] = max(dp[i][prec], dp[i - 1][prec]); } } long long ans = 0; for (long long i = 0; i <= 9; i++) ans = max(ans, dp[n][i]); cout << ans << endl; } signed main() { solve(); }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; unsigned long long bt[maxn]; unsigned long long d[maxn]; map<unsigned long long, int> has; pair<int, int> edg[maxn]; int f[maxn]; int id[maxn]; int lab[maxn]; vector<int> e[maxn]; int vis[maxn]; int getf(int x) { if (x == f[x]) return x; else return f[x] = getf(f[x]); } void dfs(int x, int y, int de) { int i; lab[x] = de; vis[x] = 1; for (i = 0; i < (int)e[x].size(); i++) { if (e[x][i] == y) continue; dfs(e[x][i], x, de + 1); } } int main() { int i, j, n, m; cin >> n >> m; int x, y; bt[0] = 1; for (int i = 1; i <= n; i++) { bt[i] = 3 * bt[i - 1]; d[i] = bt[i]; } for (i = 0; i < m; i++) { scanf( %d%d , &x, &y); edg[i] = make_pair(x, y); d[x] += bt[y], d[y] += bt[x]; } j = 1; for (i = 1; i <= n; i++) { if (!has[d[i]]) has[d[i]] = j++; id[i] = has[d[i]]; f[i] = i; } for (i = 0; i < m; i++) { x = id[edg[i].first], y = id[edg[i].second]; if (x == y) continue; for (j = 0; j < (int)e[x].size(); j++) { if (e[x][j] == y) break; } if (j >= 2) { printf( NO n ); return 0; } if (j == (int)e[x].size()) e[x].push_back(y); else continue; for (j = 0; j < (int)e[y].size(); j++) { if (e[y][j] == x) break; } if (j >= 2) { printf( NO n ); return 0; } if (j == (int)e[y].size()) e[y].push_back(x); else continue; if (getf(x) == getf(y)) { printf( NO n ); return 0; } else f[getf(y)] = f[getf(x)]; } int cnt = 0; for (i = 1; i <= n; i++) { if (vis[id[i]] == 0 && (int)e[id[i]].size() < 2) { vis[id[i]] = 1; dfs(id[i], -1, 1); } } printf( YES n ); for (i = 1; i <= n; i++) printf(i == n ? %d n : %d , lab[id[i]]); }
|
#include <bits/stdc++.h> using namespace std; const long long MAX_N = 2e5 + 5, inf = 1e18, mod = 1000000007; const double PI = 3.1415926536; int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; template <typename T> ostream& operator<<(ostream& out, const vector<T>& mas) { for (const auto& x : mas) { out << x << ; } return out; } void ok() { cout << YES << n ; } void no() { cout << NO << n ; } inline long long nxt() { long long x; cin >> x; return x; } struct Node { vector<int> g; long long sum = 0; long long sum1 = 0; void push(int v) { g.push_back(v); } vector<int>::iterator begin() { return g.begin(); } vector<int>::iterator end() { return g.end(); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int n = nxt(); vector<Node> g(n); vector<long long> a(n); long long total = 0; for (int i = 0; i < n; i++) { a[i] = nxt(); total += a[i]; } for (int i = 0; i < n - 1; i++) { int v = nxt() - 1, u = nxt() - 1; g[v].push(u); g[u].push(v); } vector<int> lvl(n); function<void(int, int)> dfs = [&](int v, int p) { g[v].sum1 = a[v] * lvl[v]; g[v].sum = a[v]; for (const auto& to : g[v]) { if (to != p) { lvl[to] += lvl[v] + 1; dfs(to, v); g[v].sum += g[to].sum; g[v].sum1 += g[to].sum1; } } }; dfs(0, -1); long long ans = 0; long long res = g[0].sum1; function<void(int, int)> calc = [&](int v, int p) { ans = max(ans, res); for (const auto& to : g[v]) { if (to == p) continue; g[v].sum -= g[to].sum; res -= g[to].sum; g[to].sum += g[v].sum; res += g[v].sum; calc(to, v); res -= g[v].sum; g[to].sum -= g[v].sum; res += g[to].sum; g[v].sum += g[to].sum; } }; calc(0, -1); cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int Mod = 1000000000 + 7; const int MAXK = 1000000; int power(int x, int k) { int ret = 1; while (k) { if (k & 1) ret = 1LL * ret * x % Mod; x = 1LL * x * x % Mod; k >>= 1; } return ret; } int n, k, ans; int f[MAXK + 10]; void init() { cin >> n >> k; if (n <= k + 10) { for (int i = 1; i <= n; i++) ans = (power(i, k) + ans) % Mod; cout << ans; } else { for (int i = 1; i <= k + 2; i++) f[i] = (f[i - 1] + power(i, k)) % Mod; int s = 1, m = 1; for (int j = 2; j <= k + 2; j++) s = 1LL * s * (n - j) % Mod; for (int j = 2; j <= k + 2; j++) m = 1LL * m * ((1 - j) + Mod) % Mod; int l = -(k + 1), r = 1; for (int i = 1; i <= k + 2; i++) { ans = (1LL * f[i] * s % Mod * power(m, Mod - 2) % Mod + ans) % Mod; s = 1LL * s * (n - i) % Mod * power(n - (i + 1), Mod - 2) % Mod; m = 1LL * m * r % Mod * power(l + Mod, Mod - 2) % Mod; l++, r++; } cout << ans; } return; } int main() { init(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int const N = 202020; long long a[N]; long long l[N], r[N]; int main() { int n; long long k; scanf( %d%I64d , &n, &k); for (int i = 1; i <= (n); ++i) scanf( %I64d , &a[i]); for (int i = 1; i <= (n); ++i) l[i] = a[i - 1] == 1 ? 1 + l[i - 1] : 0; for (int i = n; i >= 1; --i) r[i] = a[i + 1] == 1 ? 1 + r[i + 1] : 0; long long mx = 0x3FFFFFFFFFFFFFFFLL; long long ans = 0; if (k == 1) { for (int i = 1; i <= (n); ++i) if (a[i] == 1) ++ans; } for (int i = 1; i <= (n); ++i) if (a[i] != 1) { long long ts = 0, tr = 1; for (int j = i; j <= n; ++j) { if (a[j] == 1) { ts += r[j - 1]; j += r[j - 1]; j -= 1; continue; } if (tr > mx / a[j]) break; ts += a[j]; tr *= a[j]; if (tr % k == 0) { long long need = tr / k - ts; if (need >= 0) { long long lc = min(need, l[i]), rc = min(need, r[j]); long long cnt = lc + rc - need + 1; cnt = max(cnt, 0LL); ans += cnt; } } } } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); int s = 0, ans = 0; for (int i = 1;; i++) { for (int j = 1; j <= i; j++) s += j; if (s > n) break; ans++; } printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; void zanj0() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void solve() { int n, m; cin >> n >> m; int S = int(0.45 * sqrt(n)) + 1; vector<int> v(n), rev_v(n), s_jump(n); for (int i = 0; i < n; i++) { cin >> v[i]; v[i]--; } for (int i = 0; i < n; i++) { rev_v[v[i]] = i; } auto compute_s_jump = [&](int index) -> int { int x = index; for (int i = 0; i < S; i++) { x = v[x]; } return s_jump[index] = x; }; for (int i = 0; i < n; i++) compute_s_jump(i); while (m--) { int type; cin >> type; if (type == 2) { int x, k; cin >> x >> k; x--; while (S <= k) { k -= S; x = s_jump[x]; } while (k--) { x = v[x]; } cout << x + 1 << n ; } else { int x, y; cin >> x >> y; x--; y--; swap(rev_v[v[x]], rev_v[v[y]]); swap(v[x], v[y]); int sx = compute_s_jump(x); int sy = compute_s_jump(y); for (int i = 0; i < S; i++) { s_jump[x] = sx; s_jump[y] = sy; x = rev_v[x]; y = rev_v[y]; sx = rev_v[sx]; sy = rev_v[sy]; } } } } int32_t main() { zanj0(); solve(); cerr << Time : << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << ms n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long int N = 1e6; ; int n, m, a, l, r, num; int arr[2]; int main() { scanf( %d%d , &n, &m); while (n--) { scanf( %d , &a); if (a == -1) ++arr[0]; else ++arr[1]; } while (m--) { scanf( %d%d , &l, &r); if (l == r) { printf( 0 n ); continue; } num = r - l + 1; if (num % 2 != 0) printf( 0 n ); else { if (num / 2 > arr[0] || num / 2 > arr[1]) printf( 0 n ); else printf( 1 n ); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long Maxn = 1e5 + 7; const long long Max = 1e3 + 7; const long long Mod = 1e9 + 7; const long long Inf = 1e9 + 7; string s[Maxn], V[Maxn]; long long ans[5]; long long check(char t) { if (t == u || t == o || t == a || t == i || t == e ) return true; return false; } void f(long long a, long long b, long long c, long long d) { if (V[a] == V[b] && V[c] == V[d]) ; else ans[1] = 0; if (V[a] == V[c] && V[b] == V[d]) ; else ans[2] = 0; if (V[a] == V[d] && V[c] == V[b]) ; else ans[3] = 0; if (V[a] == V[b] && V[c] == V[d] && V[c] == V[b]) ; else ans[4] = 0; } void print(long long i) { if (i == 1) cout << aabb << endl; if (i == 2) cout << abab << endl; if (i == 3) cout << abba << endl; if (i == 4) cout << aaaa << endl; } int main() { long long n, k; cin >> n >> k; for (long long i = 1; i <= 4 * n; i++) { cin >> s[i]; long long K = k; for (long long j = s[i].size() - 1; K && j >= 0; j--) { V[i] += s[i][j]; if (check(s[i][j])) K--; } if (K) { cout << NO << endl; return 0; } } ans[1] = ans[2] = ans[3] = ans[4] = 1; for (long long i = 1; i <= n; i++) { long long st = (i - 1) * 4; f(st + 1, st + 2, st + 3, st + 4); } for (long long i = 4; i >= 1; i--) { if (ans[i]) { print(i); return 0; } } cout << NO << endl; return 0; }
|
#include <bits/stdc++.h> #pragma gcc optimize( Ofast ) using namespace std; using ll = long long; const int N = 1e6 + 2; int n, cnt, f[N], vis[N]; int qt, q[N]; vector<int> v; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; if (n & 1) return cout << -1 n , 0; v.push_back(0); for (int i = 0; i < n; ++i) { int a; cin >> a; if (a > 0) { if (f[a] || vis[a]) return cout << -1 n , 0; ++f[a], ++cnt, vis[a] = 1, q[++qt] = a; } else { a = -a; if (!f[a]) return cout << -1 n , 0; --f[a], --cnt; if (!cnt) { v.push_back(i + 1); while (qt) vis[q[qt--]] = 0; } } } if (cnt) return cout << -1 n , 0; cout << v.size() - 1 << n ; for (int i = 1; i < v.size(); ++i) cout << v[i] - v[i - 1] << n [i + 1 == v.size()]; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n >> a >> b; for (int i = 0; (n - a * i) >= 0; i++) { if ((n - a * i) % b == 0) { cout << YES n ; cout << i << << (n - a * i) / b << endl; return 0; } } cout << NO n ; return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) const int N = (int)2e5 + 7; const int inf = (int)1e9 + 7; const int mod = (int)1e9 + 7; const long long linf = (long long)1e18 + 7; const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}; const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; using namespace std; struct line { long long k, b; int id; line() {} line(long long k, long long b, int id) : k(k), b(b), id(id) {} long long get(long long x) { return k * x + b; } bool operator<(const line &R) const { if (k != R.k) return k > R.k; return b > R.b; } }; int n; class CHT { private: vector<line> v; public: long double inter(line a, line b) { return (b.b - a.b) / (double)(a.k - b.k); } void add(line x) { while ((int)v.size() > 1 && inter(v[(int)v.size() - 2], x) >= inter(v[(int)v.size() - 2], v.back())) v.pop_back(); v.push_back(x); } void push(line x) { v.push_back(x); } void build() { vector<line> b; sort(v.begin(), v.end()); for (int i = 0; i < (int)v.size(); i++) { if ((int)b.size() && b.back().k == v[i].k) continue; b.push_back(v[i]); } v.clear(); for (auto it : b) { add(it); } } line get(long long x) { int l = 0, r = (int)v.size() - 2, res = 0; while (l <= r) { int mid = l + r >> 1; if (v[mid].get(x) <= v[mid + 1].get(x)) res = mid + 1, l = mid + 1; else r = mid - 1; } return v[res]; } }; struct SegTree { CHT t[N << 2]; void upd(int p, line x, int v = 1, int tl = 1, int tr = n) { t[v].push(x); if (tl == tr) return; int tm = tl + tr >> 1; if (p <= tm) upd(p, x, v << 1, tl, tm); else upd(p, x, v << 1 | 1, tm + 1, tr); } void build(int v = 1, int tl = 1, int tr = n) { if (tl == tr) return; int tm = tl + tr >> 1; build(v << 1, tl, tm); build(v << 1 | 1, tm + 1, tr); t[v].build(); } line maxi(line a, line b, int x) { if (a.get(x) >= b.get(x)) return a; return b; } line get(int l, int r, int x, int v = 1, int tl = 1, int tr = n) { if (l <= tl && tr <= r) return t[v].get(x); int tm = tl + tr >> 1; if (r <= tm) return get(l, r, x, v << 1, tl, tm); if (tm < l) return get(l, r, x, v << 1 | 1, tm + 1, tr); return maxi(get(l, r, x, v << 1, tl, tm), get(l, r, x, v << 1 | 1, tm + 1, tr), x); } } t; int a[N]; long long s1[N], s2[N]; int main() { ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; s1[i] = (long long)a[i] * i; s2[i] = a[i]; } for (int i = n; i >= 1; i--) { s1[i] += s1[i + 1]; s2[i] += s2[i + 1]; t.upd(i, line(s2[i + 1], -s1[i + 1], i)); } t.build(); long long ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans, t.get(i, n, i - 1).get(i - 1) + s1[i] - (i - 1) * s2[i]); } cout << ans; exit(0); }
|
#include <bits/stdc++.h> using namespace std; void answer() { int n; string s, t; cin >> n >> s >> t; int ps[26], pt[26]; int cs[26], ct[26]; memset(ps, -1, sizeof(ps)); memset(pt, -1, sizeof(pt)); memset(cs, 0, sizeof(cs)); memset(ct, 0, sizeof(ct)); for (int i = 0; i < s.size(); i++) { ps[s[i] - a ] = i; cs[s[i] - a ]++; } for (int i = 0; i < t.size(); i++) { pt[t[i] - a ] = i; ct[t[i] - a ]++; } for (int i = 0; i < 26; i++) { if (cs[i] != ct[i]) { cout << NO << n ; return; } } int c = 0; for (int i = 0; i < 26; i++) { if (cs[i] > 1 || ct[i] > 1) { cout << YES << n ; return; } for (int j = i + 1; j < 26; j++) { c += ((ps[i] < ps[j]) != (pt[i] < pt[j])); } } if (c % 2) cout << NO << n ; else cout << YES << n ; } int main() { ios::sync_with_stdio(0); cin.tie(0); int q; cin >> q; for (int i = 0; i < q; i++) answer(); return 0; }
|
#include <bits/stdc++.h> using namespace std; double tick() { static clock_t oldtick; clock_t newtick = clock(); double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC; oldtick = newtick; return diff; } long long gcd(long long a, long long b) { if ((a == 0) || (b == 0)) { return a + b; } return gcd(b, a % b); } long long powMod(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return (res % 1000000007); } long long pow2(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a); a = (a * a); b >>= 1; } return res; } bool isPrime(long long a) { for (long long i = 3; (i * i) <= a; i += 2) { if ((a % i) == 0) return false; } if ((a != 2) && ((a % 2) == 0)) { return false; } if (a == 1) { return false; } return true; } string conLlToStr(long long a) { stringstream mystr; mystr << a; return mystr.str(); } long long conStrToLl(string st) { long long numb = 0; long long len = st.size(), i, j = 0; for (long long i = len - 1; i >= 0; i--) { numb += (pow2(10, j) * (st[i] - 0 )); j++; } return numb; } long long basenoToDecimal(string st, long long basee) { long long i = 0, j, anss = 0; for (long long j = (int)st.length() - 1; j >= 0; j--) { anss += (st[j] - 0 ) * pow2(basee, i); i++; } return anss; } string decimalToString(long long num, long long basee) { long long i = 0, j, opop; string anss = ; vector<string> stri; stri.push_back( 0 ); stri.push_back( 1 ); stri.push_back( 2 ); stri.push_back( 3 ); stri.push_back( 4 ); stri.push_back( 5 ); stri.push_back( 6 ); stri.push_back( 7 ); stri.push_back( 8 ); stri.push_back( 9 ); stri.push_back( A ); stri.push_back( B ); stri.push_back( C ); stri.push_back( D ); stri.push_back( E ); stri.push_back( F ); if (num == 0) { return 0 ; } while (num) { opop = (num % basee); anss += stri[opop]; num /= basee; } reverse(anss.begin(), anss.end()); return anss; } long long my_sqrt(long long x) { long long y = (long long)(sqrtl((long double)x) + 0.5); while (y * y < x) { y++; } while (y * y > x) { y--; } if (y * y == x) { return y; } return -1; } long long my_crt(long long x) { long long y = (long long)(powl((long double)x, 1.0 / 3.0) + 0.5); while (y * y * y < x) { y++; } while (y * y * y > x) { y--; } if (y * y * y == x) { return y; } return -1; } const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; string S[100010]; vector<string> stringContained[100010][30]; vector<pair<string, string> > firstString, secondString; vector<pair<pair<string, string>, pair<string, string> > > ans; set<int> numVowel; vector<string> temp; bool isVowel(char ch) { if (ch == a || ch == e || ch == i || ch == o || ch == u ) { return 1; } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; for (long long i = 1; i <= N; i++) { cin >> S[i]; int vowel = 0; char lastVowel; for (long long j = 0; j <= (int)S[i].length() - 1; j++) { if (isVowel(S[i][j])) { vowel++; lastVowel = S[i][j]; } } if (vowel) { numVowel.insert(vowel); stringContained[vowel][lastVowel - a ].push_back(S[i]); } } for (auto it = numVowel.begin(); it != numVowel.end(); ++it) { int i = (*it); for (long long j = 0; j <= 25; j++) { for (int k = 0; k < (int)stringContained[i][j].size(); k += 2) { if ((k + 1) < (int)stringContained[i][j].size()) { secondString.push_back(make_pair(stringContained[i][j][k], stringContained[i][j][k + 1])); } } if (stringContained[i][j].size() % 2) { temp.push_back(stringContained[i][j][stringContained[i][j].size() - 1]); } } for (int k = 0; k < (int)temp.size(); k += 2) { if ((k + 1) < (int)temp.size()) { firstString.push_back(make_pair(temp[k], temp[k + 1])); } } temp.clear(); } int ind1 = 0, ind2 = 0; while (ind1 < firstString.size() && ind2 < secondString.size()) { ans.push_back(make_pair( make_pair(firstString[ind1].first, secondString[ind2].first), make_pair(firstString[ind1].second, secondString[ind2].second))); ind1 += 1; ind2 += 1; } for (int k = ind2; k < (int)secondString.size(); k += 2) { if ((k + 1) < (int)secondString.size()) { ans.push_back(make_pair( make_pair(secondString[k].first, secondString[k + 1].first), make_pair(secondString[k].second, secondString[k + 1].second))); } } cout << ans.size() << n ; for (long long i = 0; i <= (int)ans.size() - 1; i++) { cout << ans[i].first.first << << ans[i].first.second << n ; cout << ans[i].second.first << << ans[i].second.second << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T> inline T ABS(T a) { return a > 0 ? a : -a; } template <typename T> inline T MIN(T a, T b) { return a < b ? a : b; } template <typename T> inline T MAX(T a, T b) { return a > b ? a : b; } template <typename T> inline T CHKMIN(T &a, T b) { if (a > b) a = b; return a; } template <typename T> inline T CHKMAX(T &a, T b) { if (a < b) a = b; return a; } template <typename T> inline void SWAP(T &a, T &b) { static T c; c = a; a = b; b = c; } template <typename T, typename... T0> T MAX(T a, T b, T0... c) { return a > b ? MAX(a, c...) : MAX(b, c...); } template <typename T, typename... T0> T MIN(T a, T b, T0... c) { return a < b ? MIN(a, c...) : MIN(b, c...); } template <typename T, int n> void myin(T a[]) { for (int i = (0); i < (int)(n); ++i) cin >> a[i]; } template <typename T> void myin(T &a) { cin >> a; } template <typename T> void print(T a) { cout << a << ; } template <typename T, typename... T0> void print(T a, T0... b) { print(a); print(b...); } template <typename T> void println(T a) { cout << a << endl; } template <typename T, typename... T0> void println(T a, T0... b) { print(a); println(b...); } int n, in[120000]; long long dp[120000]; int main() { long long sum = 0; while (cin >> n) { for (int i = (0); i < (int)(n); ++i) { scanf( %d , in + i); sum += in[i]; } if (sum % 3 != 0) { puts( 0 ); continue; } long long tmp = sum / 3, now = 0, ans = 0; for (int i = (0); i < (int)(n); ++i) { now += in[i]; if (i == 0) dp[i] = 0; else dp[i] = dp[i - 1]; if (now == tmp) ++dp[i]; } tmp += tmp, now = in[0]; for (int i = (1); i < (int)(n - 1); ++i) { now += in[i]; if (now == tmp) ans += dp[i - 1]; } println(ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int kN = 1e5 + 10; const int MOD = 1e9 + 7; int N, M; int power(int d, int k) { int ret = 1, tmp = d; while (k) { if (k & 1) { ret = 1ll * ret * tmp % MOD; } tmp = 1ll * tmp * tmp % MOD; k >>= 1; } return ret; } void cant() { printf( 0 ); exit(0); } struct Edge { int u, v; bool diff; } es[kN]; struct UFS { int fa[kN]; int lowbit(int i) { return i & -i; } void init() { for (int i = 0; i < kN; i++) fa[i] = i; } int getFa(int i) { if (fa[i] != i) fa[i] = getFa(fa[i]); return fa[i]; } bool isSameFa(int u, int v) { int uf = getFa(u); int vf = getFa(v); return uf == vf; } void link(int u, int v) { int uf = getFa(u); int vf = getFa(v); fa[uf] = vf; } } ufs; int col[kN]; vector<int> G[kN]; void dfs(int u, int c) { if (col[u]) { if (col[u] != c) cant(); return; } col[u] = c; c = c == 1 ? 2 : 1; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; dfs(v, c); } } int main() { scanf( %d %d , &N, &M); for (int i = 1; i <= M; i++) { int op, u, v; scanf( %d %d %d , &u, &v, &op); if (u > v) swap(u, v); es[i].u = u, es[i].v = v, es[i].diff = !op; } ufs.init(); for (int i = 1; i <= M; i++) { if (es[i].diff == false) { ufs.link(es[i].u, es[i].v); } } for (int i = 1; i <= M; i++) { if (es[i].diff) { int uf = ufs.getFa(es[i].u); int vf = ufs.getFa(es[i].v); G[uf].push_back(vf); G[vf].push_back(uf); } } int cnt = 0; for (int i = 1; i <= N; i++) if (ufs.getFa(i) == i && !col[i]) { dfs(i, 1); cnt++; } int ans = power(2, cnt - 1); printf( %d , ans); return 0; }
|
#include <bits/stdc++.h> const int maxn = 100005; int n, cnt; int a[maxn], p[maxn]; int main() { scanf( %d , &n); p[1] = 1; for (int i = 2; i <= n; i++) if (p[i] == 0) { p[i] = ++cnt; for (int j = i; j <= n; j += i) a[j] = cnt, p[j] = 1; } for (int i = 2; i <= n; i++) printf( %d%c , a[i], i == n ? n : ); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int CMax = 5002; const int CMax2 = 100002; int m, B[CMax], A[CMax2], ats = 0, sk, pr; int main() { scanf( %d , &m); for (int i = 0; i < CMax; i++) { B[i] = 0; } for (int i = 0; i < m; i++) { scanf( %d , &sk); B[sk]++; } for (int i = 0; i < CMax; i++) { if (B[i] > 0) { B[i]--; A[ats] = i; ats++; pr = i; } } for (int i = pr - 1; i >= 0; i--) { if (B[i] > 0) { B[i]--; A[ats] = i; ats++; } } printf( %d n , ats); if (ats > 0) { for (int i = 0; i < ats - 1; i++) { printf( %d , A[i]); } printf( %d n , A[ats - 1]); } }
|
#include <bits/stdc++.h> using namespace std; int main() { int x, ans; string s; cin >> s; x = s.size(); for (int i = 1; i < x; i++) { if (s[i] == 1 ) { x++; break; } } cout << x / 2 << endl; return 0; }
|
#include<bits/stdc++.h> #define fast {ios_base::sync_with_stdio(false);cin.tie(NULL);} using namespace std; const int mxn=2e5+5,mod=1e9+7; int dp[mxn][20],a[mxn],nxt[mxn],n; vector<int>prime_factors[mxn]; void sieve(){ for(int i=2;i<mxn;i++){ if(prime_factors[i].empty()){ nxt[i]=mxn; for(int j=1;i*j<mxn;j++) prime_factors[i*j].push_back(i); } } } void lifting(){ dp[n+1][0]=n+1; for(int i=n;i>=1;i--){ dp[i][0]=dp[i+1][0]; for(auto x:prime_factors[a[i]]){ dp[i][0]=min(dp[i][0],nxt[x]); nxt[x]=i; } } for(int i=1;i<=19;i++){ for(int j=1;j<=n+1;j++) dp[j][i]=dp[dp[j][i-1]][i-1]; } } void solve(){ int q,i,j; cin>>n>>q; for(i=1;i<=n;i++) cin>>a[i]; sieve(); lifting(); while(q--){ int l,r; cin>>l>>r; int ans=0; for(i=19;i>=0;i--){ if(dp[l][i]<=r){ ans+=1<<i; l=dp[l][i]; } } cout<<ans+1<< n ; } } int main(void){ fast; solve(); }
|
#include <bits/stdc++.h> using namespace std; bool is_pal(string a) { string b = a; reverse(b.begin(), b.end()); return b == a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); ; string second; cin >> second; for (int i = 0; i < second.size(); i++) { string temp = second; for (char j = a ; j <= z ; j++) { if (j == second[i]) continue; temp[i] = j; if (is_pal(temp)) { cout << YES n ; return 0; } } } cout << NO n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, q; unordered_map<int, pair<int, int>> resi[50 * 3 + 2]; struct FLOW { int n, *pre, *dist; FLOW(int n) : n(n) { pre = (int *)malloc((n + 2) * sizeof(int)); dist = (int *)malloc((n + 2) * sizeof(int)); } ~FLOW() { free(pre); free(dist); } pair<int, int> SAP() { int flow = 0, cost = 0; for (; SPFA();) { for (int i = n + 1; i != n; i = pre[i]) { assert(resi[pre[i]].count(i)); resi[pre[i]][i].first -= pre[n]; if (resi[i].count(pre[i])) resi[i][pre[i]].first += pre[n]; else resi[i][pre[i]] = make_pair(pre[n], -resi[pre[i]][i].second); } flow += pre[n]; cost += pre[n] * dist[n + 1]; } return make_pair(flow, cost); } bool SPFA() { int nn = n + 2, q[nn + 1]; bool v[nn]; int h = 0, l = 1, x; for (int i = 0; i < nn; i++) dist[i] = (50 * 50 * 50); memset(pre, -1, nn * sizeof(int)); memset(v, false, nn * sizeof(bool)); dist[n] = 0, pre[n] = -1; q[0] = n, v[n] = true; while (l != 0) { x = q[h]; v[q[h]] = false; l--; h = (h + 1) % nn; for (unordered_map<int, pair<int, int>>::const_iterator ix = resi[x].begin(); ix != resi[x].end(); ix++) { if (ix->second.first == 0) continue; int i = ix->first, p = ix->second.second; if (dist[x] + p < dist[i]) { dist[i] = dist[x] + p; pre[i] = x; if (!v[i]) v[q[(h + l++) % nn] = i] = true; } } } if (pre[n + 1] != -1) { int flow = (50 * 50 * 50); for (int i = n + 1; i != n; i = pre[i]) if (resi[pre[i]][i].first < flow) flow = resi[pre[i]][i].first; pre[n] = flow; } return pre[n + 1] != -1; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; int arr[n][2], t, l, r, v; for (int i = 0; i < n; i++) arr[i][0] = 1, arr[i][1] = n; for (int iq = 0; iq < q; iq++) { cin >> t >> l >> r >> v; if (t == 1) for (int i = l - 1; i < r; i++) arr[i][0] = max(arr[i][0], v); else if (t == 2) for (int i = l - 1; i < r; i++) arr[i][1] = min(arr[i][1], v); } for (int i = 0; i < n; i++) { resi[3 * n][i] = make_pair(1, 0); resi[2 * n + i][3 * n + 1] = make_pair(n, 2 * i + 1); for (int j = 0; j < n; j++) resi[n + i][2 * n + j] = make_pair(1, 0); for (int j = arr[i][0] - 1; j < arr[i][1]; j++) resi[i][n + j] = make_pair(1, 0); } FLOW flow(3 * n); pair<int, int> ans = flow.SAP(); if (ans.first != n) cout << -1 << endl; else cout << ans.second << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long N = 500500; long long i, j, k, n, sum, ch, t, ans, num; long long a[N], b[N], c[N]; void R(long long &x) { x = 0; ch = getchar(); while (ch < 0 || 9 < ch) ch = getchar(); while ( 0 <= ch && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); } int main() { R(n); for (i = 1; i <= n; i++) { R(a[i]); R(b[i]); c[i] = a[i] ^ b[i]; sum ^= a[i]; } k = 60; for (i = 1; i <= n; i++) { for (j = i + 1; j <= n; j++) if (c[i] < c[j]) t = c[i], c[i] = c[j], c[j] = t; if (!c[i]) break; while (!(c[i] & 1ll << k)) k--; for (j = i + 1; j <= n; j++) if (c[j] & 1ll << k) c[j] ^= c[i]; } num = i - 1; ans = 1ll << num; j = 60; for (i = 1; i <= num; i++) { while (!(c[i] & 1ll << j)) j--; if (sum & 1ll << j) sum ^= c[i]; } if (sum) puts( 1/1 ); else printf( %I64d/%I64d n , ans - 1, ans); }
|
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; int answer = 0; int f[n]; for (int i = 0; i < n; i++) cin >> f[i]; sort(f, f + n); for (int i = n - 1; i >= 0; i -= k) answer += (2 * (f[i] - 1)); cout << answer << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int n; int a[102]; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; puts((n % 2 == 1) && (a[0] % 2 == 1) && (a[n - 1] % 2 == 1) ? Yes : No ); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MX = 100000; const int N = 1e5 + 5; const int K = 135; const int MOD = 1e9 + 7; int n, first[N], incr[N], x[K]; long long pw[K][N]; int main() { cin >> n; for (int i = 0; i < n; ++i) { int x; scanf( %d , &x); incr[x]++; } for (int i = 1; i < K; ++i) pw[i][0] = 1; for (int i = 0; i < N; ++i) pw[0][i] = 1; for (int j = 1; j < N; ++j) for (int i = 0; i < K; ++i) pw[i][j] = pw[i][j - 1] * i % MOD; for (int i = MX; i >= 1; --i) first[i] = first[i + 1] + incr[i]; long long ans = 0; for (int i = MX; i >= 1; --i) if (first[i]) { vector<int> d1, d2, d; int dd = 1; for (; dd * dd < i; ++dd) if (i % dd == 0) { d1.push_back(dd); d2.push_back(i / dd); } if (dd * dd == i) d1.push_back(dd); reverse(d2.begin(), d2.end()); merge(d1.begin(), d1.end(), d2.begin(), d2.end(), back_inserter(d)); int prev = 0; long long p1 = 1, p2 = 1; for (int j = (int)d.size() - 1; j >= 0; --j) { x[j + 1] = first[d[j]] - prev; prev += x[j + 1]; p1 = p1 * pw[j + 1][x[j + 1]] % MOD; if (j + 1 == d.size()) p2 = p2 * pw[j][x[j + 1]] % MOD; else p2 = p2 * pw[j + 1][x[j + 1]] % MOD; } ans += p1 - p2; ans %= MOD; if (ans < 0) ans += MOD; } cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main(void) { vector<int> z[26]; string a, b; cin >> a >> b; for (int i = 0; i < a.size(); ++i) z[a[i] - a ].push_back(i); bool ok = true; for (int i = 0; i < b.size(); ++i) { if (z[b[i] - a ].size() == 0) { ok = false; break; } } if (!ok) { cout << -1 << endl; } else { int cnt = 1, cur = -1; for (int i = 0; i < b.size(); ++i) { vector<int> &tmp = z[b[i] - a ]; vector<int>::iterator iter = upper_bound(tmp.begin(), tmp.end(), cur); if (iter == tmp.end()) { ++cnt; cur = tmp[0]; } else { cur = *iter; } } cout << cnt << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; double a, v, l, d, w, t1, t2, t3, t4, t5, dis1, dis2, dis3, v1; double chuli(double x) { double diss = (v + w) * ((v - w) / a) / 2; if (diss >= x) return (-2.0 * w + sqrt(4.0 * w * w + 8.0 * a * (x))) / (2.0 * a); else return (v - w) / a + (x - diss) / v; } int main() { cin >> a >> v >> l >> d >> w; if (v >= w) { t1 = w / a; dis1 = a * t1 * t1 / 2; v1 = a * sqrt(2 * d / a); if (dis1 >= l) printf( %.12lf n , sqrt(2.0 * l / a)); else if (dis1 >= d) printf( %.12lf n , t1 + chuli(l - dis1)); else { dis2 = 2 * (v - w) / a * w + (v - w) * (v - w) / a; if (dis1 + dis2 >= d) { t2 = 2.0 * (-2.0 * w + sqrt(4.0 * w * w + 4.0 * a * (d - dis1))) / (2.0 * a); t3 = chuli(l - d); printf( %.12lf n , t1 + t2 + t3); } else { t2 = v / a; t3 = (v - w) / a; t4 = (d - dis2 - dis1) / v; t5 = chuli(l - d); printf( %.12lf n , t2 + t3 + t4 + t5); } } } else { t1 = v / a; dis1 = a * t1 * t1 / 2; if (dis1 < l) t2 = (l - dis1) / v; else { t1 = sqrt(2 * l / a); t2 = 0.0; } printf( %.12lf n , t1 + t2); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int A, B, C, Cans = 214748364, n, r, ansA, ansB; bool f[1000010]; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } void Work() { int t, i, a = A, b = B; for (C = 0; A != 1 || B != 1;) { if (A > B) { f[++C] = 0; A -= B; } else { f[++C] = 1; B -= A; } if (C > n) break; } f[++C] = 0; if (C == n) { t = 0; for (int i = 2; i <= C; ++i) if (f[i] == f[i - 1]) ++t; if (t < Cans) Cans = t, ansA = a, ansB = b; } } void out() { if (Cans == 214748364) printf( IMPOSSIBLE n ); else { printf( %d n , Cans); A = ansA, B = ansB; Work(); for (int i = n; i >= 2; --i) printf( %c , f[i] ? B : T ); printf( %c n , f[1] ? B : T ); } } int main() { int i; scanf( %d%d , &n, &r); for (int i = 1; i <= r; ++i) if (gcd(i, r) == 1) { A = i, B = r; Work(); A = r, B = i; Work(); } out(); return 0; }
|
#include <bits/stdc++.h> using namespace std; struct point { double x, y; }; double dist(point P, point Q) { double dx = P.x - Q.x, dy = P.y - Q.y; return sqrt(dx * dx + dy * dy); } vector<point> crossing(point O1, double r1, point O2, double r2) { double d = dist(O1, O2); double t = (d * d + r1 * r1 - r2 * r2) / 2.0 / d / d; double fx = O1.x + (O2.x - O1.x) * t, fy = O1.y + (O2.y - O1.y) * t; double h = sqrt(r1 * r1 - t * d * t * d); double dx = (O2.y - O1.y) / d * h, dy = (O1.x - O2.x) / d * h; vector<point> ans; point ans1 = {fx + dx, fy + dy}; ans.push_back(ans1); point ans2 = {fx - dx, fy - dy}; ans.push_back(ans2); return ans; } vector<double> tri[10]; void read(int id) { point P, Q, R; cin >> P.x >> P.y >> Q.x >> Q.y >> R.x >> R.y; tri[id].push_back(dist(P, Q)); tri[id].push_back(dist(P, R)); tri[id].push_back(dist(Q, R)); sort(tri[id].begin(), tri[id].end()); } bool quad(double a, double b, double c, double d) { double x[] = {a, b, c, d}; sort(x, x + 4); return (x[0] + x[1] + x[2] + 1.0E-9 > x[3]); } bool triineq(double a, double b, double c) { double x[] = {a, b, c}; sort(x, x + 3); return (x[0] + x[1] + 1.0E-9 > x[2]); } bool equals(double x, double y) { return (x - y < 1.0E-9 && x - y > -1.0E-9); } int N; point P[20]; int a[(1 << 4)], dp[(1 << 4)]; void dfs(int mask) { int i, x, y, z; a[mask] = min(a[mask], N); for ((i) = 0; (i) < (int)(4); (i)++) if (!(mask & (1 << i))) { int mask2 = (mask | (1 << i)); bool found = false; for ((x) = 0; (x) < (int)(N); (x)++) for ((y) = 0; (y) < (int)(N); (y)++) for ((z) = 0; (z) < (int)(N); (z)++) { if (equals(dist(P[x], P[y]), tri[i][0]) && equals(dist(P[x], P[z]), tri[i][1]) && equals(dist(P[y], P[z]), tri[i][2])) { dfs(mask2); found = true; } } if (!found) for ((x) = 0; (x) < (int)(N); (x)++) for ((y) = 0; (y) < (int)(N); (y)++) { if (equals(dist(P[x], P[y]), tri[i][0])) { N++; P[N - 1] = crossing(P[x], tri[i][1], P[y], tri[i][2])[0]; dfs(mask2); P[N - 1] = crossing(P[x], tri[i][1], P[y], tri[i][2])[1]; dfs(mask2); N--; } if (equals(dist(P[x], P[y]), tri[i][1])) { N++; P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][2])[0]; dfs(mask2); P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][2])[1]; dfs(mask2); N--; } if (equals(dist(P[x], P[y]), tri[i][2])) { N++; P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][1])[0]; dfs(mask2); P[N - 1] = crossing(P[x], tri[i][0], P[y], tri[i][1])[1]; dfs(mask2); N--; } } } } vector<point> crossed(point O1, double r1, point O2, double r2) { vector<point> ans = crossing(O1, r1, O2, r2); vector<point> ans2 = crossing(O1, r2, O2, r1); ans.push_back(ans2[0]); ans.push_back(ans2[1]); return ans; } void init() { int a, b, c, i, j, k, x, y, z; for ((i) = 0; (i) < (int)(4); (i)++) { N = 3; P[0].x = P[0].y = P[1].y = 0.0; P[1].x = tri[i][0]; P[2] = crossing(P[0], tri[i][1], P[1], tri[i][2])[0]; dfs(1 << i); } for ((a) = 0; (a) < (int)(4); (a)++) for ((b) = 0; (b) < (int)(a); (b)++) for ((c) = 0; (c) < (int)(b); (c)++) for ((i) = 0; (i) < (int)(3); (i)++) for ((j) = 0; (j) < (int)(3); (j)++) for ((k) = 0; (k) < (int)(3); (k)++) { if (triineq(tri[a][i], tri[b][j], tri[c][k])) { N = 6; P[0].x = P[0].y = P[1].y = 0.0; P[1].x = tri[a][i]; P[2] = crossing(P[0], tri[b][j], P[1], tri[c][k])[0]; vector<point> three = crossed(P[0], tri[a][(i + 1) % 3], P[1], tri[a][(i + 2) % 3]); vector<point> four = crossed(P[0], tri[b][(j + 1) % 3], P[2], tri[b][(j + 2) % 3]); vector<point> five = crossed(P[1], tri[c][(k + 1) % 3], P[2], tri[c][(k + 2) % 3]); for ((x) = 0; (x) < (int)(4); (x)++) for ((y) = 0; (y) < (int)(4); (y)++) for ((z) = 0; (z) < (int)(4); (z)++) { P[3] = three[x]; P[4] = four[y]; P[5] = five[z]; dfs((1 << a) | (1 << b) | (1 << c)); } } } } int main() { int i, j, k, l; for ((i) = 0; (i) < (int)(4); (i)++) read(i); int ans = 9; for ((i) = 0; (i) < (int)(3); (i)++) for ((j) = 0; (j) < (int)(3); (j)++) for ((k) = 0; (k) < (int)(3); (k)++) for ((l) = 0; (l) < (int)(3); (l)++) if (quad(tri[0][i], tri[1][j], tri[2][k], tri[3][l])) ans = 8; for ((i) = 0; (i) < (int)((1 << 4)); (i)++) a[i] = (1 << 29); a[0] = 0; init(); for ((i) = 0; (i) < (int)((1 << 4)); (i)++) { dp[i] = a[i]; for (j = 1; j < i; j++) if ((i & j) == j) dp[i] = min(dp[i], dp[j] + a[i ^ j] - 1); } ans = min(ans, dp[15]); cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T> T mabs(const T &a) { return a < 0 ? -a : a; } const int mod = 1e9 + 9; const int SZ = 300300; struct mat { long long A[2][2]; mat(int a = 0, int b = 0, int c = 0, int d = 0) { A[0][0] = a; A[0][1] = b; A[1][0] = c; A[1][1] = d; } mat operator*(const mat &other) const { mat res; for (int i = (0), ei = (2); i < ei; i++) for (int j = (0), ej = (2); j < ej; j++) { for (int k = (0), ek = (2); k < ek; k++) res.A[i][j] += A[i][k] * other.A[k][j]; res.A[i][j] %= mod; } return res; } mat operator+(const mat &other) const { mat res; for (int i = (0), ei = (2); i < ei; i++) for (int j = (0), ej = (2); j < ej; j++) { res.A[i][j] = A[i][j] + other.A[i][j]; res.A[i][j] %= mod; } return res; } mat operator-(const mat &other) const { mat res; for (int i = (0), ei = (2); i < ei; i++) for (int j = (0), ej = (2); j < ej; j++) { res.A[i][j] = A[i][j] - other.A[i][j]; res.A[i][j] %= mod; } return res; } }; mat Fib(1, 1, 1, 0); mat EminusArev(-1, -1, -1, 0); mat Step[SZ]; long long Sum[4 * SZ]; mat M[4 * SZ]; int A[SZ]; void build(int v, int l, int r) { if (l == r) { Sum[v] = A[l]; return; } int c = (l + r) / 2; build(2 * v, l, c); build(2 * v + 1, c + 1, r); Sum[v] = (Sum[2 * v] + Sum[2 * v + 1]) % mod; } inline mat sumUp(int a, int b) { mat res = EminusArev * (Step[0] - Step[b - a + 1]); return Step[a] * res; } void add(int v, int l, int r, int ql, int qr, int st) { Sum[v] = (Sum[v] + sumUp(ql - st, qr - st).A[0][0]) % mod; if (l == ql && r == qr) { M[v] = M[v] + Step[ql - st]; return; } int c = (l + r) / 2; if (qr <= c) add(2 * v, l, c, ql, qr, st); else if (ql > c) add(2 * v + 1, c + 1, r, ql, qr, st); else { add(2 * v, l, c, ql, c, st); add(2 * v + 1, c + 1, r, c + 1, qr, st); } } long long getSum(int v, int l, int r, int ql, int qr) { if (l == ql && r == qr) { return Sum[v]; } int c = (l + r) / 2; long long res = 0; if (qr <= c) res = getSum(2 * v, l, c, ql, qr); else if (ql > c) res = getSum(2 * v + 1, c + 1, r, ql, qr); else res = getSum(2 * v, l, c, ql, c) + getSum(2 * v + 1, c + 1, r, c + 1, qr); return (res + (M[v] * sumUp(ql - l, qr - l)).A[0][0]) % mod; } void run() { Step[0] = mat(1, 0, 0, 1); for (int i = (1), ei = (SZ); i < ei; i++) Step[i] = Step[i - 1] * Fib; int n, m; cin >> n >> m; for (int i = (0), ei = (n); i < ei; i++) scanf( %d , A + i); build(1, 0, n - 1); for (int i = (0), ei = (m); i < ei; i++) { int t, a, b; scanf( %d%d%d , &t, &a, &b); --a; --b; if (t == 1) { add(1, 0, n - 1, a, b, a); } else { int res = getSum(1, 0, n - 1, a, b); res = (res + mod) % mod; printf( %d n , res); } } } int main() { run(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m; char mat[1501][1501]; int visited[1501][1501][3]; int getr(int x) { return (x % n + n) % n; } int getc(int x) { return (x % m + m) % m; } int dx[] = {0, -1, 0, 1}; int dy[] = {-1, 0, 1, 0}; bool dfs(int lx, int ly) { int x = getr(lx), y = getc(ly); if (visited[x][y][0] && (lx != visited[x][y][1] || ly != visited[x][y][2])) return 1; if (mat[x][y] == # || visited[x][y][0]) return 0; visited[x][y][0] = 1; visited[x][y][1] = lx; visited[x][y][2] = ly; for (int i = 0; i < 4; ++i) { if (dfs(lx + dx[i], ly + dy[i])) return 1; } return 0; } int main() { ios::sync_with_stdio(0); while (cin >> n >> m) { int sx, sy; memset(visited, 0, sizeof(visited)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> mat[i][j]; if (mat[i][j] == S ) sx = i, sy = j; } } if (dfs(sx, sy)) cout << Yes n ; else cout << No n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; vector<long long> prime; bool num[1000005 + 2]; void sieve() { num[0] = true; num[1] = true; for (long long i = 2; i <= 1000005; i++) { if (num[i] == false) { prime.push_back(i); for (long long j = 2 * i; j <= 1000005; j = j + i) { num[j] = true; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, i, j, k, l, a, b, c, p, q, r, t, ans, sum; cin >> t; vector<pair<long long, long long> > v; while (t--) { cin >> n; m = n; j = 0; while (n--) { cin >> k >> a; b = a + 1; c = b + 1; for (i = 1; i < k; i++) { cin >> a; if (a >= c) { b = b + (a - c) + 1; c = a + 2; } else c++; } v.push_back(make_pair(b, k)); } sort(v.begin(), v.end()); b = v[0].first + v[0].second; a = v[0].first; for (i = 1; i < m; i++) { if (v[i].first > b) { c = v[i].first - b; b = v[i].first + v[i].second; a = a + c; } else { b = b + v[i].second; } } cout << a << endl; v.clear(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-6; const int inf = 1e9; const int mod = 1e9 + 7; const int maxn = 5e5 + 10; int n; int a[1010], b[1010]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; } int ans = 0; for (int i = 1; i <= n; i++) { int r1 = 0, r2 = 0; for (int j = i; j <= n; j++) { r1 |= a[j]; r2 |= b[j]; ans = max(r1 + r2, ans); } } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, c; cin >> n >> a >> b >> c; long long res = 0; for (int i = 0; i <= a; ++i) { for (int j = 0; j <= b; ++j) { if (2 * n - i - 2 * j >= 0 && !((2 * n - i - 2 * j) % 4) && 2 * n - i - 2 * j <= 4 * c) { ++res; } } } cout << res << endl; return 0; }
|
#include <bits/stdc++.h> const int MAXN = (int)1e5 + 5; const int MODN = (int)1e9 + 7; int inf = 0x3f3f3f3f; using namespace std; char str[MAXN]; int num[MAXN]; int a[MAXN]; struct Com { int cost; int qua; } c[MAXN]; int cmp(Com a, Com b) { return a.cost < b.cost; } int main() { int n, m; while (scanf( %d , &n) != EOF) { for (int i = 0; i < n; i++) { scanf( %d%d , &c[i].cost, &c[i].qua); } sort(c, c + n, cmp); int flag = 0; for (int i = 1; i < n; i++) { if (c[i].qua < c[i - 1].qua && c[i].cost > c[i - 1].cost) { flag = 1; } } if (flag) puts( Happy Alex ); else puts( Poor Alex ); } }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int maxm = 15; const long long INF = 1e18; int n, m, k; int a[maxn]; long long dp[maxn][maxm]; void ReadInput() { cin >> n >> m >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; } void Solve() { fill_n(&dp[0][0], sizeof(dp) / sizeof(dp[0][0]), -INF); for (int i = 0; i <= n; ++i) dp[i][10] = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { if (j == 1) dp[i][j] = max((long long)a[i] - k, dp[i - 1][m] + a[i] - k); else dp[i][j] = dp[i - 1][j - 1] + a[i]; } long long ans = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) ans = max(ans, dp[i][j]); cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ReadInput(); Solve(); }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; char s[maxn], t[maxn]; int nxt[maxn]; void getNxt(int m) { int i = 1, j = 0; nxt[0] = 0; while (i < m) { if (t[i] == t[j]) nxt[i++] = ++j; else if (!j) i++; else j = nxt[j - 1]; } } int main() { scanf( %s%s , s, t); int len1 = strlen(s), len2 = strlen(t); int len1_0 = 0, len1_1 = 0, len2_0 = 0, len2_1 = 0; for (int i = 0; i < len1; i++) { int k = s[i] - 0 ; if (k == 0) len1_0++; else len1_1++; } for (int i = 0; i < len2; i++) { int k = t[i] - 0 ; if (k == 0) len2_0++; else len2_1++; } getNxt(len2); int k = nxt[len2 - 1]; if (len1 < len2) { for (int i = 0; i < len1; i++) cout << s[i]; } else if (len1 == len2) { if (len1_0 != len2_0 || len1_1 != len2_1) { for (int i = 0; i < len1; i++) cout << s[i]; } else { for (int i = 0; i < len1; i++) cout << t[i]; } } else { if (len2_0 == 0) { for (int i = 1; i <= len1_1; i++) { cout << 1 ; } for (int i = 1; i <= len1_0; i++) { cout << 0 ; } return 0; } else if (len2_1 == 0) { for (int i = 1; i <= len1_1; i++) { cout << 1 ; } for (int i = 1; i <= len1_0; i++) { cout << 0 ; } return 0; } if (len1_0 < len2_0 || len1_1 < len2_1) { for (int i = 0; i < len1; i++) cout << s[i]; } else { if (k == 0) { int p = min(len1_0 / len2_0, len1_1 / len2_1); for (int j = 1; j <= p; j++) { for (int i = 0; i < len2; i++) { cout << t[i]; } } len1_0 = len1_0 - len2_0 * p; len1_1 = len1_1 - len2_1 * p; for (int i = 1; i <= len1_0; i++) { cout << 0 ; } for (int i = 1; i <= len1_1; i++) { cout << 1 ; } } else { int f0 = 0, f1 = 0; for (int i = k; i < len2; i++) { int p = t[i] - 0 ; if (p == 0) f0++; else f1++; } for (int i = 0; i < len2; i++) { cout << t[i]; } len1_0 -= len2_0; len1_1 -= len2_1; int p = min(len1_0 / f0, len1_1 / f1); len1_0 = len1_0 - f0 * p; len1_1 = len1_1 - f1 * p; for (int j = 1; j <= p; j++) { for (int i = k; i < len2; i++) { cout << t[i]; } } for (int i = 1; i <= len1_0; i++) { cout << 0 ; } for (int i = 1; i <= len1_1; i++) { cout << 1 ; } } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize(2) #pragma GCC optimize(3) inline int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == - , ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - 0 , ch = getchar(); return f ? -x : x; } inline long long llread() { long long x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == - , ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - 0 , ch = getchar(); return f ? -x : x; } long long fact[110], c[110][110]; long long f[110][110][110]; long long n, m, k, P; signed main() { n = llread(), m = llread(), k = llread(), P = llread(); c[0][0] = fact[0] = 1; for (long long i = (1); i <= (n); i++) { c[i][0] = c[i][i] = 1; for (long long j = (1); j <= (i - 1); j++) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P; } for (long long i = (1); i <= (n); i++) fact[i] = (fact[i - 1] * i) % P; for (long long i = (0); i <= (n); i++) f[0][i][0] = 1; for (long long i = (1); i <= (n); i++) { f[i][1][1] = fact[i]; for (long long j = (i + 1); j <= (n); j++) f[i][j][0] = fact[i]; for (long long j = (2); j <= (min(i, m)); j++) { for (long long p = (0); p <= (min(i - j + 1, k)); p++) { f[i][j][p] = (f[i][j][p] + (f[i - 1][j - 1][p - (j == 1)] * 2)) % P; for (long long q = (2); q <= (i - 1); q++) { for (long long w = (0); w <= (p - (j == 1)); w++) { if (f[q - 1][j - 1][w] && f[i - q][j - 1][p - (j == 1) - w]) { f[i][j][p] = (f[i][j][p] + f[q - 1][j - 1][w] * f[i - q][j - 1][p - (j == 1) - w] % P * c[i - 1][q - 1]) % P; } } } } } } printf( %lld n , f[n][m][k]); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; char now = ; int count = 0, sum = 0, n; n = s.size(); for (int i = 0; i < n; i++) { if (now == s[i] and count < 5) count++; else sum++, now = s[i], count = 1; } cout << sum << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 100005; int t[N]; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; t[0] = 0; t[n + 1] = 0; for (int i = 1; i <= n; ++i) { cin >> t[i]; t[i] = min(t[i - 1] + 1, t[i]); } for (int i = n; i > 0; --i) { t[i] = min(t[i + 1] + 1, t[i]); } int res = 0; for (int i = 1; i <= n; ++i) res = max(res, t[i]); cout << res << n ; }
|
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 100000; const int MAXQ = 100000; int n, nq; int x[MAXN]; int qx[MAXQ], qt[MAXQ]; long long qans[MAXQ]; pair<int, int> o[MAXQ]; priority_queue<pair<int, pair<int, int>>> pq; struct TreapNode { int prio, sz, l, r, par; }; struct Treap { int n, root; vector<TreapNode> nodes; void init(int _n) { n = _n, root = -1; nodes = vector<TreapNode>(n); for (int i = (0); i < (n); ++i) nodes[i].prio = rand() % 1000 * 1000000 + rand() % 1000 * 1000 + rand() % 1000, nodes[i].sz = 1, nodes[i].l = nodes[i].r = nodes[i].par = -1; } int get(int offset) { int at = root; while (true) { assert(at != -1 && offset < nodes[at].sz); int l = nodes[at].l, r = nodes[at].r; if (l != -1) { if (offset < nodes[l].sz) { at = l; continue; } offset -= nodes[l].sz; } if (offset == 0) return at; else --offset; if (r != -1) { if (offset < nodes[r].sz) { at = r; continue; } offset -= nodes[r].sz; } assert(false); } } int find(int at) { int ret = 0; if (nodes[at].l != -1) ret += nodes[nodes[at].l].sz; while (nodes[at].par != -1) { int to = nodes[at].par; if (at == nodes[to].r) ret += 1 + getsz(nodes[to].l); at = to; } return ret; } int getsz(int a) { return a == -1 ? 0 : nodes[a].sz; } void setpar(int a, int b) { if (a != -1) nodes[a].par = b; } void split(int res, int &l, int &r, int sz) { if (res == -1) { assert(sz == 0); l = r = -1; } else if (sz <= getsz(nodes[res].l)) split(nodes[res].l, l, nodes[res].l, sz), setpar(nodes[res].l, res), r = res; else split(nodes[res].r, nodes[res].r, r, sz - 1 - getsz(nodes[res].l)), setpar(nodes[res].r, res), l = res; if (l != -1) nodes[l].par = -1; if (r != -1) nodes[r].par = -1; if (res != -1) nodes[res].sz = 1 + getsz(nodes[res].l) + getsz(nodes[res].r); } void merge(int &res, int l, int r) { if (l == -1) res = r; else if (r == -1) res = l; else if (nodes[l].prio > nodes[r].prio) merge(nodes[l].r, nodes[l].r, r), setpar(nodes[l].r, l), res = l; else merge(nodes[r].l, l, nodes[r].l), setpar(nodes[r].l, r), res = r; if (res != -1) nodes[res].sz = 1 + getsz(nodes[res].l) + getsz(nodes[res].r); } }; Treap treap; void initpockets(int sz) { treap.init(sz); treap.root = 0; } int getpocket(int offset) { return treap.get(offset); } int getoffset(int pocket) { return treap.find(pocket); } void insertpocket(int offset, int pocket) { int l, r; treap.split(treap.root, l, r, offset); treap.merge(l, l, pocket); treap.merge(treap.root, l, r); } void solve() { for (int i = (0); i < (nq); ++i) o[i] = make_pair(qx[i], i); sort(o, o + nq); int at = 0; while (at < nq && o[at].first < x[0]) { int id = o[at++].second; qans[id] = qx[id]; } int cround = 0, coffset = 0, cseen = 1; initpockets(n); pq = priority_queue<pair<int, pair<int, int>>>(); for (int i = (0); i < (n); ++i) { int cx = x[i], nx = i == n - 1 ? INT_MAX : x[i + 1]; while (at < nq && (nx == INT_MAX || o[at].first < nx)) { int id = o[at++].second; int qoffset = (coffset + qx[id] - cx) % cseen; int qround = cround + (qx[id] - cx) / cseen + (qoffset < coffset ? 1 : 0) + qt[id]; int qpocket = getpocket(qoffset); pq.push(make_pair(-qround, make_pair(id, qpocket))); } int dx = nx == INT_MAX ? INT_MAX : nx - cx; if (dx != INT_MAX && coffset + dx < cseen) { cx += dx, coffset += dx; } else { int rem = cseen - coffset; cx += rem, coffset = 0, ++cround; if (dx != INT_MAX) dx -= rem; int full = dx == INT_MAX ? INT_MAX : dx / cseen; while (!pq.empty() && (full == INT_MAX || -pq.top().first < cround + full)) { int qround = -pq.top().first, id = pq.top().second.first, qpocket = pq.top().second.second; pq.pop(); int qoffset = getoffset(qpocket); qans[id] = cx + (long long)(qround - cround) * cseen + qoffset; } if (full == INT_MAX) break; cround += full, cx += full * cseen, dx -= full * cseen, coffset += dx, cx += dx, dx = 0; } insertpocket(coffset, i + 1), ++cseen; } } void run() { scanf( %d%d , &n, &nq); for (int i = (0); i < (n); ++i) scanf( %d , &x[i]); for (int i = (0); i < (nq); ++i) scanf( %d%d , &qx[i], &qt[i]); solve(); for (int i = (0); i < (nq); ++i) printf( %lld n , qans[i]); } long long bfans[MAXQ]; void bf() { int sz = 0; for (int i = (0); i < (n); ++i) sz = max(sz, x[i] + 1); for (int i = (0); i < (nq); ++i) sz = max(sz, qx[i] + 1); vector<int> cur(sz); for (int i = (0); i < (sz); ++i) cur[i] = i; int nxt = sz; int t = 0; int rem = nq; while (rem > 0) { for (int i = (0); i < (nq); ++i) if (qt[i] == t) { bfans[i] = cur[qx[i]]; --rem; } int at = 0, idx = 0; for (int i = (0); i < (sz); ++i) { if (at < n && x[at] == i) { ++at; continue; } cur[idx++] = cur[i]; } while (idx < sz) cur[idx++] = nxt++; ++t; } } void stress() { for (int rep = (0); rep < (10000); ++rep) { n = 100; nq = 50; int lim = 5 * n; for (int i = (0); i < (n); ++i) x[i] = rand() % (lim - n); sort(x, x + n); for (int i = (0); i < (n); ++i) x[i] += i; for (int i = (0); i < (nq); ++i) { qx[i] = rand() % lim; qt[i] = rand() % 5; } solve(); bf(); bool ok = true; for (int i = (0); i < (nq); ++i) if (bfans[i] != qans[i]) ok = false; if (ok) { printf( . ); continue; } printf( err n ); printf( have: ); for (int i = (0); i < (nq); ++i) printf( %lld , qans[i]); puts( ); printf( want: ); for (int i = (0); i < (nq); ++i) printf( %lld , bfans[i]); puts( ); printf( %d %d n , n, nq); for (int i = (0); i < (n); ++i) { if (i != 0) printf( ); printf( %d , x[i]); } puts( ); for (int i = (0); i < (nq); ++i) printf( %d %d n , qx[i], qt[i]); break; } } int main() { run(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 5050; int c, d; char ch[N]; int Calc(int b, int e) { c = 0, d = 0; int ret = 0, i; for (i = b; i <= e; i++) { if (ch[i] == ( ) c++; if (ch[i] == ) ) { if (c) c--; else { if (d > 0) d--, c++; else return ret; } } if (ch[i] == ? ) { if (c) c--, d++; else c++; } if (c == 0) { ret++; } } return ret; } int main() { int n, i, sol = 0; scanf( %s , ch + 1); n = strlen(ch + 1); for (i = 1; i <= n; i++) sol += Calc(i, n); printf( %i n , sol); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt = 1; cin >> tt; while (tt--) { int n; cin >> n; int A[n]; for (int i = 0; i < n; i++) { cin >> A[i]; } int cnt = 0; sort(A, A + n); int left, right; if (n % 2 == 0) { left = n / 2 - 1; right = n / 2; } else { left = n / 2; right = n / 2 + 1; } while (cnt < n) { cout << A[left] << ; left--; cnt++; if (cnt == n) { break; } cout << A[right] << ; right++; cnt++; } cout << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int INT_MAX_VAL = (int)0x3F3F3F3F; int INT_MIN_VAL = (int)-0x3F3F3F3F; long long LONG_MAX_VAL = (long long)0x3F3F3F3F3F3F3F3F; long long LONG_MIN_VAL = (long long)-0x3F3F3F3F3F3F3F3F; long long vx, vy, wx, wy; long long R; long double t; bool solve(long double s, long double dx, long double dy) { if (s <= t) { dx -= vx * s; dy -= vy * s; } else { dx -= wx * (s - t) + vx * t; dy -= wy * (s - t) + vy * t; } return (dx * dx + dy * dy) <= (R * R * s * s); } int main() { cin.tie(0); ios::sync_with_stdio(false); long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; cin >> R >> t; cin >> vx >> vy >> wx >> wy; long double l = 0; long double r = 1.0e9; while ((abs(r - l) / max(1.l, l)) > 1e-9) { double c = (l + r) / 2; if (solve(c, x2 - x1, y2 - y1)) { r = c; } else { l = c; } } cout << setprecision(15) << fixed << l << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, c, p[55], t[55], x = 0, L = 0, R = 0; cin >> n >> c; for (int i = 0; i < n; i++) { cin >> p[i]; } for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < n; i++) { x += t[i]; L += max(0, p[i] - c * x); } x = 0; for (int i = n - 1; i > -1; i--) { x += t[i]; R += max(0, p[i] - c * x); } if (L > R) { cout << Limak << endl; } else if (L < R) { cout << Radewoosh << endl; } else cout << Tie << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long a[200005], b[200005]; bool cmp(int a, int b) { return a > b; } int main() { long long n; cin >> n; for (int i = 0; i < n; ++i) scanf( %I64d , &a[i]); for (int i = 0; i < n; ++i) scanf( %I64d , &b[i]); for (long long i = 1; i <= n; ++i) a[i - 1] *= i * (n - i + 1); sort(a, a + n); sort(b, b + n, cmp); long long ans = 0; for (int i = 0; i < n; ++i) { ans = (ans + (a[i] % 998244353) * b[i] % 998244353) % 998244353; } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, t; cin >> n >> t; int x = 0; while (n--) { int z; cin >> z; t -= (86400 - z); ++x; if (t <= 0) { cout << x << endl; return 0; } } }
|
#include <bits/stdc++.h> using namespace std; vector<int> vct; void DFS(int d, int num) { if (d == 9) { return; } else { int a = num * 10 + 4; vct.push_back(a); DFS(d + 1, a); int b = num * 10 + 7; vct.push_back(b); DFS(d + 1, b); } } long long calc(int a, int b, int l, int r) { int x = max(a, l); int y = min(b, r); if (x > y) { return 0; } else { return y - x + 1; } } int main() { vct.clear(); DFS(0, 0); vct.push_back(-1); vct.push_back(1000000009); sort(vct.begin(), vct.end()); int ss = vct.size(); int p1, p2, v1, v2; int k; while (scanf( %d %d %d %d %d , &p1, &p2, &v1, &v2, &k) != EOF) { long long ans = 0; for (int i = 1; i + k < ss; i++) { int x = vct[i - 1] + 1; int a = vct[i]; int b = vct[i + k - 1]; int y = vct[i + k] - 1; long long tmp = 0; tmp += calc(x, a, p1, p2) * calc(b, y, v1, v2); tmp += calc(x, a, v1, v2) * calc(b, y, p1, p2); if (k == 1 && v1 <= a && a <= p2 && tmp > 0) { tmp--; } ans += tmp; } double a = 1.0 * ans / (p2 - p1 + 1) / (v2 - v1 + 1); printf( %.20f n , a); } return 0; }
|
#include <bits/stdc++.h> using namespace std; string a = What are you doing at the end of the world? Are you busy? Will you save us? ; string pref = What are you doing while sending ; string suff = ? Are you busy? Will you send ; string last = ? ; long long f[100001]; long long q, n, k, n1, n2, n3, n4; const long long inf = 1e18 + 10; char solution(long long n, long long k) { if (k > f[n]) return . ; if (n == 0) return a[k - 1]; if (k <= n2) return pref[k - 1]; k = k - n2; if (k <= f[n - 1]) return solution(n - 1, k); k = k - f[n - 1]; if (k <= n3) return suff[k - 1]; k = k - n3; if (k <= f[n - 1]) return solution(n - 1, k); k = k - f[n - 1]; if (k <= n4) return last[k - 1]; } int main() { n1 = a.length(); n2 = pref.length(); n3 = suff.length(); n4 = last.length(); f[0] = n1; for (int i = 1; i <= 100000; i++) f[i] = min(inf, n2 + f[i - 1] + n3 + f[i - 1] + n4); cin >> q; while (q > 0) { cin >> n >> k; q = q - 1; cout << solution(n, k); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e9 + 8; const long double pi = 3.14159265359; template <class T> T fac(T n) { T res = 1, i; for (i = 2; i <= n; i++) res *= i; return res; } template <class T> T lcm(T a, T b) { return (a * b) / __gcd(a, b); } template <class T> T digitsum(T x) { T sum = 0; while (x) { sum += x % 10; x /= 10; } return sum; } string tostring(long long a) { string ans; while (a) { ans += char(a % 10 + 0 ); a /= 10; } return ans; } void nPermute(string str, long long n) { sort((str).begin(), (str).end()); long long i = 1; do { if (i == n) break; i++; } while (next_permutation(str.begin(), str.end())); cout << str << endl; } vector<long long> factors(long long n) { vector<long long> f; for (long long x = 2; x * x <= n; x++) { while (n % x == 0) { f.push_back(x); n /= x; } } if (n > 1) f.push_back(n); return f; } bool prime(long long n) { if (n < 2) return false; for (long long x = 2; x * x <= n; x++) if (n % x == 0) return false; return true; } long long ncr(long long n, long long k) { long long res = 1; if (k > n - k) k = n - k; for (long long i = 0; i < k; i++) { res *= (n - i); res /= (i + 1); } return res; } tuple<long long, long long, long long> Gcd(long long a, long long b) { if (b == 0) return {1, 0, a}; else { long long x, y, g; tie(x, y, g) = Gcd(b, a % b); return {y, x - (a / b) * y, g}; } } const long long mod = 1e5; long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res % m; } void solve() { long double n, r; cin >> n >> r; long double ang1 = 1 / (tan(pi / (2 * n))), ang2 = 1 / (tan(pi / n)); long double ans = (n * r * r) / (ang1 + ang2); cout << fixed << setprecision(50) << ans << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); cerr << Time elapsed : << 1.0 * clock() / CLOCKS_PER_SEC << sec n ; }
|
#include <bits/stdc++.h> using namespace std; int getV(char c) { if (c >= 0 && c <= 9 ) return c - 0 ; if (c >= A && c <= Z ) return 10 + c - A ; if (c >= a && c <= z ) return 36 + c - a ; if (c == - ) return 62; if (c == _ ) return 63; } int main() { string s; cin >> s; int N = s.length(); long long p3[6 * N + 1]; p3[0] = 1; for (int i = 1; i < 6 * N + 1; i++) p3[i] = (p3[i - 1] * 3) % 1000000007; int cnt = 0; for (int i = 0; i < N; i++) { int v = getV(s[i]); for (int j = 0; j < 6; j++) if ((v & (1 << j)) == 0) cnt++; } cout << p3[cnt]; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct edge { int u, v, w; bool operator < (const edge &oth) const { return w < oth.w; } } e[N << 1]; int n, m, x, mn = INT_MAX; long long ans, rem; struct disjoint_sets_union { int fa[N]; int Query(int p) { if(fa[p] == p) return p; return fa[p] = Query(fa[p]); } } dsu; queue<int> que; set<int> unvised, G[N]; bool vis[N], type[N]; void FindBlocks() { for(int i = 1; i <= n; i++) if(!vis[i]) { que.push(i); vis[i] = true; unvised.erase(i); dsu.fa[i] = i; while(!que.empty()) { int u = que.front(); que.pop(); for(set<int>::iterator it = unvised.begin(); it != unvised.end();) { int v = *it; it++; if(G[u].count(v)) continue; dsu.fa[v] = u; unvised.erase(v); vis[v] = true; que.push(v); rem--; } } } } int main() { ios::sync_with_stdio(false); cin >> n >> m; rem = 1LL * n * (n - 1) / 2 - m; for(int i = 1; i <= n; i++) unvised.insert(i); for(int i = 1; i <= m; i++) { cin >> e[i].u >> e[i].v >> e[i].w; G[e[i].u].insert(e[i].v); G[e[i].v].insert(e[i].u); x ^= e[i].w; } sort(e + 1, e + m + 1); FindBlocks(); for(int i = 1; i <= m; i++) { int fau = dsu.Query(e[i].u), fav = dsu.Query(e[i].v); if(fau != fav) { dsu.fa[fau] = fav; ans += e[i].w; type[i] = 1; // type 1 } } if(rem > 0) return printf( %lld n , ans) && 0; for(int i = 1; i <= n; i++) dsu.fa[i] = i; for(int i = 1; i <= m; i++) { int fau = dsu.Query(e[i].u), fav = dsu.Query(e[i].v); if(fau == fav) continue; // type 2 dsu.fa[fau] = fav; if(!type[i]) { mn = e[i].w; break; } // type 3 } printf( %lld n , ans + min(x, mn)); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n, k, left = 0, i; long long int temp; cin >> n >> k; vector<int> task(k, 0); vector<pair<int, int>> idl(n, make_pair(0, 0)); for (i = 0; i < n; i++) { cin >> temp; task[temp - 1]++; idl[i].first = temp; } for (i = 0; i < k; i++) left += (int)(task[i] == 0); for (i = 0; i < n; i++) { cin >> temp; if (task[idl[i].first - 1] == 1) temp = INT_MAX; idl[i].second = temp; } if (!left) { cout << 0 << endl; return 0; } sort(idl.begin(), idl.end(), [](const pair<int, int> &left, const pair<int, int> &right) { return left.second < right.second; }); temp = 0; for (i = 0; i < n && left; i++) { if (task[idl[i].first - 1] > 1) { temp += idl[i].second; task[idl[i].first - 1]--; left--; } } cout << temp << endl; 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 ? Possible : Impossible ); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { x += y; if (x >= mod) x -= mod; } void et() { puts( -1 ); exit(0); } int x[4], y[4]; pair<int, int> ans[4]; int ansVal = (1 << 30); void ckeck(int len, int bx, int by) { int d[4]; pair<int, int> tar[4]; for (int(i) = 0; (i) < (int)(4); (i)++) d[i] = i; tar[0] = {bx, by}; tar[1] = {bx + len, by}; tar[2] = {bx, by + len}; tar[3] = {bx + len, by + len}; do { bool ok = 1; int tmp = 0; for (int(i) = 0; (i) < (int)(4); (i)++) { int id = d[i]; if (x[id] != tar[i].first && y[id] != tar[i].second) { ok = 0; break; } else if (x[id] == tar[i].first) { tmp = max(tmp, abs(y[id] - tar[i].second)); } else tmp = max(tmp, abs(x[id] - tar[i].first)); } if (!ok) continue; if (tmp < ansVal) { ansVal = tmp; for (int(i) = 0; (i) < (int)(4); (i)++) ans[d[i]] = tar[i]; } } while (next_permutation(d, d + 4)); } void fmain() { ansVal = (1 << 30); for (int(i) = 0; (i) < (int)(4); (i)++) scanf( %d%d , x + i, y + i); set<int> ds; set<int> px, py; for (int(i) = 0; (i) < (int)(4); (i)++) for (int j = i + 1; j < 4; j++) { if (x[i] != x[j]) ds.insert(abs(x[i] - x[j])); if (y[i] != y[j]) ds.insert(abs(y[i] - y[j])); } for (int d : ds) { for (int(i) = 0; (i) < (int)(4); (i)++) { for (int j = -d; j <= d; j += d) { px.insert(x[i] + j); py.insert(y[i] + j); } } } for (int d : ds) { int id[4]; for (int(i) = 0; (i) < (int)(4); (i)++) id[i] = i; do { int mxX = -(1 << 30), miX = (1 << 30), mxY = -(1 << 30), miY = (1 << 30); int tx[4], ty[4]; tx[0] = x[id[0]]; ty[0] = y[id[0]]; tx[1] = x[id[1]] - d; ty[1] = y[id[1]]; tx[2] = x[id[2]]; ty[2] = y[id[2]] - d; tx[3] = x[id[3]] - d; ty[3] = y[id[3]] - d; for (int(i) = 0; (i) < (int)(4); (i)++) { mxX = max(mxX, tx[i]); miX = min(miX, tx[i]); mxY = max(mxY, ty[i]); miY = min(miY, ty[i]); } px.insert((mxX + miX) / 2); py.insert((mxY + miY) / 2); } while (next_permutation(id, id + 4)); } for (int d : ds) for (int xx : px) for (int yy : py) ckeck(d, xx, yy); if (ansVal == (1 << 30)) puts( -1 ); else { printf( %d n , ansVal); for (int(i) = 0; (i) < (int)(4); (i)++) printf( %d %d n , ans[i].first, ans[i].second); } } int main() { int t; cin >> t; while (t--) fmain(); return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; void subMain() { double a, b; cin >> a >> b; cout << fixed << (b * b - a * a) / (2 * a); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); subMain(); cerr << Time : << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << ms n ; return 0; }
|
#include <bits/stdc++.h> #pragma comment(linker, /stack:200000000 ) #pragma GCC optimize( Ofast,no-stack-protector ) #pragma GCC target( avx ) using namespace std; template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return __builtin_popcount(s); } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } const int MAXN = 100; const int MOD = 1e9 + 7; const long long MAXV = 1e6; const double eps = 1e-12; const long long INF = 1e16; inline string toStr(long long x) { string tmp = ; do tmp = char(x % 10 + 0 ) + tmp; while (x /= 10); return tmp; } inline long long toInt(string s) { long long res = 0; for (auto x : s) res = res * 10 + x - 0 ; return res; } inline string toBinStr(long long x) { string res = ; do res = (x % 2 ? 1 : 0 ) + res; while (x >>= 1LL); return res; } long long rnd(int k) { if (!k) return rand() % MAXV + 1; long long t = rand() % MAXV + 1; return (rand() % t) + (MAXV - t); } long long random_gen(int sign) { long long x = rnd(rand() % 2); long long s = rand() % 2; s = !s ? 1 : -1; return sign == 1 ? x : sign == -1 ? -x : s * x; } inline void debugArray(int a[], int n) { cerr << array = ; for (int i = 0; i < n; ++i) cerr << a[i] << ; cerr << n ; } int n, m; vector<int> row[MAXN]; bool color[MAXN]; string s[MAXN], actual[MAXN]; int main() { cin >> n >> m; for (int i = 0, _a = (n); i < _a; ++i) { cin >> s[i]; for (int j = 0, _a = (m); j < _a; ++j) if (s[i][j] == # ) row[i].push_back(j); } for (int i = 0, _a = (n); i < _a; ++i) { actual[i] = ; for (int j = 0, _a = (m); j < _a; ++j) actual[i] += . ; } for (int i = 0, _a = (n); i < _a; ++i) if (!color[i]) { vector<int> need; color[i] = true; need.push_back(i); for (int j = 0, _a = (n); j < _a; ++j) if (!color[j]) for (int k = 0, _a = (m); k < _a; ++k) if (s[i][k] == # && s[j][k] == # ) { need.push_back(j); color[j] = true; break; } for (auto x : need) for (auto y : row[i]) actual[x][y] = # ; } bool good = true; for (int i = 0, _a = (n); i < _a; ++i) for (int j = 0, _a = (m); j < _a; ++j) good &= actual[i][j] == s[i][j]; puts(good ? Yes : No ); }
|
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1; int n, k; int clo[N]; string s; bool block[N]; int dist(int a, int b) { if (a > b) swap(a, b); if (a == -1 || b == n) return n; return min(b - a, n - (b - a)); } char neg(char a) { if (a == W ) return B ; else return W ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> s; for (int i = 0; i < n; ++i) { if (s[i] == s[(i - 1 + n) % n] || s[i] == s[(i + 1) % n]) { block[i] = 1; } } int cur = -1, l = n, r = -1; for (int i = 0; i < n; ++i) { clo[i] = n; if (block[i]) { l = min(l, i); r = max(r, i); } } for (int i = 0; i < n; ++i) { if (block[i]) { cur = i; } clo[i] = min(clo[i], dist(i, cur)); } cur = -1; for (int i = n - 1; i >= 0; --i) { if (block[i]) { cur = i; } clo[i] = min(clo[i], dist(i, cur)); } for (int i = 0; i < n; ++i) { clo[i] = min(clo[i], dist(i, l)); clo[i] = min(clo[i], dist(i, r)); } for (int i = 0; i < n; ++i) { if (clo[i] <= k) { cout << ((clo[i] % 2 == 0) ? s[i] : neg(s[i])); } else { cout << ((k % 2 == 0) ? s[i] : neg(s[i])); } } }
|
#include <bits/stdc++.h> using namespace std; struct Node { int v, index; } a[100005]; int n, h, big, small, tmp, diff, num; int ans[100005]; int cmp(const void *i, const void *j) { Node x = *(Node *)i; Node y = *(Node *)j; if (x.v == y.v) { return x.index > y.index ? 1 : -1; } else { return x.v > y.v ? 1 : -1; } } int main() { while (scanf( %d %d , &n, &h) != EOF) { for (int i = 0; i < n; i++) { scanf( %d , &a[i].v); a[i].index = i; } qsort(a, n, sizeof(a[0]), cmp); num = 0, diff = (a[n - 1].v + a[n - 2].v) - (a[0].v + a[1].v); for (int i = 1; i < n; i++) { small = a[0].v + a[1].v + h; if (i >= 2 && small > a[1].v + a[2].v) small = a[1].v + a[2].v; if (i != n - 1 && small > a[0].v + a[i + 1].v) small = a[0].v + a[i + 1].v; if (i == n - 1) big = a[i].v + a[0].v + h; else big = a[n - 1].v + a[i].v + h; if (big < a[n - 1].v + a[n - 2].v) big = a[n - 1].v + a[n - 2].v; if (diff > big - small) { diff = big - small; num = i; } } fill(ans, ans + n, 1); for (int i = 1; i <= num; i++) ans[a[i].index] = 2; printf( %d n , diff); for (int i = 0; i < n; i++) printf( %d%c , ans[i], i == n - 1 ? n : ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double PI = acos(-1.0); const double e = 2.718281828459; const int N = 200000; inline int sgn(double a); inline long long gcd(long long a, long long b); inline long long mod_pow(long long x, long long n, long long mod); vector<int> vv; vector<int> vis[1005]; int main() { memset(vis, 0, sizeof(vis)); int n, m; string s; string t; cin >> n >> m; cin >> s; cin >> t; int maxidx = 0; int maxcount = 0; for (int i = 0; i <= m - n; i++) { int count = 0; for (int j = 0; j < n; j++) { if (s[j] != t[j + i]) { vis[i].push_back(j + 1); } else { ++count; } } if (maxcount < count) { maxcount = count; maxidx = i; } } cout << vis[maxidx].size() << endl; for (int i = 0; i < vis[maxidx].size(); i++) { cout << vis[maxidx][i] << ; } return 0; } inline int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } inline long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } long long mod_pow(long long x, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n = n >> 1; } return res; }
|
#include <bits/stdc++.h> using namespace std; const int Maxn = 1e5 + 1e3 + 2; const int mx = 20; int n, q, N, ans; int k[Maxn]; vector<int> V[Maxn]; vector<pair<int, int>> g; int h[Maxn], par[mx][Maxn]; bool mark[Maxn]; int st[Maxn], en[Maxn], T; set<pair<int, int>> ver, lca; void dfs(int u, int DD = 0) { st[u] = T++; par[0][u] = DD; for (int i = 1; i < mx; i++) par[i][u] = par[i - 1][par[i - 1][u]]; for (int i = 0; i < V[u].size(); i++) { int v = V[u][i]; if (v != DD) h[v] = h[u] + 1, dfs(v, u); } en[u] = T++; } int LCA(int u, int v) { if (h[v] < h[u]) swap(u, v); for (int i = mx; i--;) if (h[par[i][v]] >= h[u]) v = par[i][v]; if (v == u) return v; for (int i = mx; i--;) if (par[i][v] != par[i][u]) v = par[i][v], u = par[i][u]; return par[0][v]; } bool check() { bool res = true; for (int i = 0; i < N; i++) mark[k[i]] = true; for (int i = 0; i < N; i++) if (mark[par[0][k[i]]]) res = false; return res; } void solve() { if (!check()) { for (int i = 0; i < N; i++) mark[k[i]] = false; ans = -1; return; } ans = 0; ver.clear(); lca.clear(); for (int i = 0; i < N; i++) ver.insert(make_pair(st[k[i]], k[i])); set<pair<int, int>>::iterator it = ver.begin(); for (it++; it != ver.end(); it++) { int v = (*it).second; it--; int u = (*it).second; it++; int DD = LCA(u, v); lca.insert(make_pair(-h[DD], DD)); } for (; ver.size() > 1;) { ans++; pair<int, int> p = (*lca.begin()); lca.erase(p); int v = p.second; it = ver.upper_bound(make_pair(st[v], n)); for (; it != ver.end() && ver.size(); it++) { int u = (*it).second; if (st[u] > en[v]) break; if (mark[v]) ans++; g.push_back(*it); } ans -= mark[v]; if (!mark[v] && g.size() < 2) { g.clear(); ans--; continue; } for (int i = 0; i < g.size(); i++) ver.erase(g[i]), mark[g[i].second] = false; g.clear(); } for (int i = 0; i < N; i++) mark[k[i]] = false; } int main() { cin >> n; for (int u, v, i = 0; i < n - 1; i++) { cin >> u >> v; V[u].push_back(v); V[v].push_back(u); } h[1] = 1; dfs(1); cin >> q; for (int j = 0; j < q; j++) { cin >> N; for (int i = 0; i < N; i++) cin >> k[i]; solve(); cout << ans << endl; } }
|
#include <bits/stdc++.h> using namespace ::std; int n, m, k, ans; int a[105][105]; int d[3][105]; int a1[105]; int main() { scanf( %d %d %d , &n, &m, &k); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf( %d , &a[i][j]); } ans = 2140000000; if (n > 10) { for (int i = 1; i <= n; i++) { int an = 0; for (int j = 1; j <= n; j++) { int op = 0; for (int k = 1; k <= m; k++) if (a[i][k] != a[j][k]) op++; an += min(op, m - op); } ans = min(ans, an); } } else { for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < n; j++) { if ((i & (1 << j)) != 0) a1[j] = 1; else a1[j] = 0; } int an = 0; for (int j = 1; j <= m; j++) { int op = 0; for (int k = 1; k <= n; k++) if (a[k][j] != a1[k - 1]) op++; an += min(op, n - op); } ans = min(ans, an); } } if (ans > k) printf( -1 ); else printf( %d , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20; long long here[N + 100]; int n, m; long double ter[N + 100]; int go(string &s1, string &s2) { int val = 0; for (int i = int(0); i < int(m); ++i) if (s1[i] == s2[i]) val |= (1 << i); return val; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(14); cin >> n; vector<string> vec(n); for (int i = int(0); i < int(n); ++i) cin >> vec[i]; m = int((vec[0]).size()); for (int i = int(0); i < int(n); ++i) for (int j = int(i + 1); j < int(n); ++j) if (i != j) { int mask = go(vec[i], vec[j]); here[mask] |= (1LL << i) | (1LL << j); } int lim = (1 << m) - 1; for (int mask = int(lim); mask >= int(0); mask--) for (int i = int(0); i < int(m); ++i) here[mask] |= here[mask | (1 << i)]; ter[lim] = 0; for (int i = int(lim - 1); i >= int(0); i--) { ter[i] = __builtin_popcountll(here[i]); long double val = 0; for (int j = int(0); j < int(m); ++j) if (1 - ((i >> j) & 1)) val += ter[i | (1 << j)]; ter[i] += val / (m - __builtin_popcount(i)); } cout << ter[0] / n << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6; int n, k; int root; vector<int> G[MAXN + 10]; int res = 0; int dfs(int u, int p = -1) { if ((int)G[u].size() == 1) return 0; vector<int> d; for (int v : G[u]) { if (v == p) continue; d.push_back(dfs(v, u) + 1); } sort(d.begin(), d.end(), greater<int>()); while (d.size() > 1) { int b = d.back(); d.pop_back(); int a = d.back(); d.pop_back(); if (a + b > k) { res += d.size() + 1; return b; } else { d.push_back(a); } } return d[0]; } int main() { scanf( %d%d , &n, &k); for (int i = 0; i < n - 1; i++) { int u, v; scanf( %d%d , &u, &v); G[u].push_back(v); G[v].push_back(u); } root = 1; while ((int)G[root].size() == 1) ++root; dfs(root); res++; printf( %d n , res); }
|
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /STACK:64000000 ) int prec[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; const int mod = 1000000000 + 7; int n, k; long long doIt(int m, int bse) { int b[10]; for (int i = 0; i < 10; i++) b[i] = 0; int res = 0; while (!b[m]) { int can = true; for (int i = 0; i < m && can; i++) { int cur = i; int iter = m + 1; while (iter && cur) { cur = b[cur]; iter--; } can &= (cur == 0); } if (can) res++; int ind = 0; b[ind]++; while (b[ind] >= bse) { b[ind] = 0; ind++; b[ind]++; } } return res % mod; } int main() { cin >> n >> k; k = min(k, 8); long long res = 1; if (k > 1) res = doIt(k, k) % mod; for (int i = 0; i < n - k; i++) { res = res * 1LL * (n - k); res %= mod; } cout << res << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; int n, a, b; int scores[MAXN], answer[MAXN]; pair<int, int> tmp_scores[MAXN]; void solve() { if (a == b) { for (int i = 0; i < a; ++i) printf( 1 ); for (int i = 0; i < b; ++i) printf( 2 ); printf( n ); return; } if (b < a) for (int i = 0; i < n; ++i) tmp_scores[i] = make_pair(scores[i], i); else for (int i = 0; i < n; ++i) tmp_scores[i] = make_pair(6 - scores[i], i); sort(&tmp_scores[0], &tmp_scores[n]); for (int i = 0; i < a; ++i) answer[tmp_scores[i].second] = 1; for (int i = a; i < n; ++i) answer[tmp_scores[i].second] = 2; for (int i = 0; i < n; ++i) printf( %d , answer[i]); printf( n ); } int main() { scanf( %d%d%d , &n, &a, &b); for (int i = 0; i < n; ++i) scanf( %d , &scores[i]); solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; vector<vector<vector<ll>>> v; vector<vector<ll>> mat; vector<vector<ll>> matMal(vector<vector<ll>> mat1, vector<vector<ll>> mat2) { vector<vector<ll>> mat3((int)(mat1).size(), vector<ll>((int)(mat1).size())); for (int i = 0; i < (int)(mat1).size(); ++i) { for (int j = 0; j < (int)(mat1).size(); ++j) { ll sum = 0; for (int k = 0; k < (int)(mat1).size(); ++k) { sum = sum + (ll)mat1[i][k] * mat2[k][j] % 1000000007; } mat3[i][j] = sum % 1000000007; } } return mat3; } vector<vector<ll>> matMult(vector<vector<ll>> matr, ll x) { if (x == 1) return matr; if (x % 2 == 1) return matMal(matMult(matr, x - 1), mat); vector<vector<ll>> matTemp = matMult(matr, x / 2); return matMal(matTemp, matTemp); } int main() { int n, k, m; vector<ll> anzb = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; cin >> n >> k >> m; mat.resize((1 << m) * (k + 1), vector<ll>((1 << m) * (k + 1))); for (int i = 0; i < 1 << m; ++i) { for (int j = 0; j < k + 1; ++j) { if (i % 2 == 0) { mat[(k + 1) * i + j][(k + 1) * (i / 2) + j] = 1; mat[(k + 1) * i + j][(k + 1) * ((i + (1 << m)) / 2) + j] = 1; } else if (j != 0) { mat[(k + 1) * i + j][(k + 1) * (i / 2) + j - 1] = anzb[i / 2] + 1; mat[(k + 1) * i + j][(k + 1) * ((i + (1 << m)) / 2) + j - 1] = anzb[(i + (1 << m)) / 2] + 1; } } } mat = matMult(mat, n); ll sum = 0; for (int i = 0; i < 1 << m; ++i) { sum = (sum + mat[(k + 1) * i + k][0]) % 1000000007; } cout << sum << endl; }
|
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; pii operator+(pii& p1, pii& p2) { return {p1.first + p2.first, p1.second + p2.second}; } bool isvalidpos(pii& p, int n) { return p.first >= 0 && p.first < n && p.second >= 0 && p.second < n; } int main() { int n; cin >> n; vector<vector<int>> v1(n, vector<int>(n, -1)); vector<vector<int>> v2(n, vector<int>(n, -1)); v1[0][0] = 1; v1[n - 1][n - 1] = 0; v2[0][0] = 1; v2[n - 1][n - 1] = 0; queue<pii> q; q.push({0, 0}); pii dyx[] = {{2, 0}, {0, 2}, {1, 1}}; while (!q.empty()) { pii now = q.front(); q.pop(); for (int i = 0; i < 3; i++) { pii nxt = now + dyx[i]; if (!isvalidpos(nxt, n)) continue; if (v1[nxt.first][nxt.second] != -1) continue; cout << ? << now.first + 1 << << now.second + 1 << << nxt.first + 1 << << nxt.second + 1 << endl; cout.flush(); int res; cin >> res; if (res == 1) v2[nxt.first][nxt.second] = v1[nxt.first][nxt.second] = v1[now.first][now.second]; else v2[nxt.first][nxt.second] = v1[nxt.first][nxt.second] = 1 - v1[now.first][now.second]; q.push(nxt); } } v1[0][1] = 0; v2[0][1] = 1; q.push({0, 1}); while (!q.empty()) { pii now = q.front(); q.pop(); for (int i = 0; i < 3; i++) { pii nxt = now + dyx[i]; if (!isvalidpos(nxt, n)) continue; if (v1[nxt.first][nxt.second] != -1) continue; cout << ? << now.first + 1 << << now.second + 1 << << nxt.first + 1 << << nxt.second + 1 << endl; cout.flush(); int res; cin >> res; if (res == 1) { v2[nxt.first][nxt.second] = v2[now.first][now.second]; v1[nxt.first][nxt.second] = v1[now.first][now.second]; } else { v2[nxt.first][nxt.second] = 1 - v2[now.first][now.second]; v1[nxt.first][nxt.second] = 1 - v1[now.first][now.second]; } q.push(nxt); } } for (int i = 1; i < n; i += 2) { cout << ? << i + 1 << << 1 << << i + 2 << << 2 << endl; cout.flush(); int res; cin >> res; if (res == 1) { v2[i][0] = v2[i + 1][1]; v1[i][0] = v1[i + 1][1]; } else { v2[i][0] = 1 - v2[i + 1][1]; v1[i][0] = 1 - v1[i + 1][1]; } } bool answer = false; pii dyx2[][4] = {{{0, 0}, {0, 1}, {0, 2}, {1, 2}}, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}, {{0, 0}, {1, 0}, {1, 1}, {1, 2}}}; bool end = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bool palin1 = false; bool palin2 = false; pii now = {i, j}; for (int k1 = 0; k1 < 3; k1++) { bool check = true; for (int k2 = 0; k2 < 4; k2++) { pii nxt = now + dyx2[k1][k2]; if (!isvalidpos(nxt, n)) { check = false; break; } } if (!check) continue; if (v1[now.first + dyx2[k1][0].first] [now.second + dyx2[k1][0].second] == v1[now.first + dyx2[k1][3].first] [now.second + dyx2[k1][3].second] && v1[now.first + dyx2[k1][1].first] [now.second + dyx2[k1][1].second] == v1[now.first + dyx2[k1][2].first] [now.second + dyx2[k1][2].second]) palin1 = true; if (v2[now.first + dyx2[k1][0].first] [now.second + dyx2[k1][0].second] == v2[now.first + dyx2[k1][3].first] [now.second + dyx2[k1][3].second] && v2[now.first + dyx2[k1][1].first] [now.second + dyx2[k1][1].second] == v2[now.first + dyx2[k1][2].first] [now.second + dyx2[k1][2].second]) palin2 = true; } if (palin1 || palin2) { cout << ? << now.first + 1 << << now.second + 1 << << now.first + 2 << << now.second + 3 << endl; cout.flush(); int res; cin >> res; if (res == 1) { if (palin1) answer = true; else answer = false; } else { if (palin1) answer = false; else answer = true; } end = true; break; } } if (end) break; } cout << ! << endl; cout.flush(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (answer) cout << v1[i][j]; else cout << v2[i][j]; } cout << endl; cout.flush(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int a[105]; int main() { int i, n, j = 0, k, l; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a + 1, a + n); while (a[0] <= a[n - 1]) { j++; a[n - 1]--; a[0]++; sort(a + 1, a + n); } cout << j << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); cout.tie(NULL); ios_base ::sync_with_stdio(false); int n; cin >> n; vector<pair<long long, long long>> news(n + 1); for (int i = 1; i <= n; i++) cin >> news[i].first; for (int i = 1; i <= n; i++) cin >> news[i].second; long long ans = 0; priority_queue<long long> q; sort(news.begin() + 1, news.end()); long long val = news[1].first, sum = 0; news.push_back(make_pair(2e9, 0)); for (int i = 1; i <= n + 1; i++) { while (val < news[i].first && q.size() > 0) { sum -= q.top(); q.pop(); ans += sum; val++; } val = news[i].first; q.push(news[i].second); sum += news[i].second; } cout << ans; }
|
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize( Ofast ) #pragma GCC optimize( inline ) using namespace std; const long long p = 998244353, g = 3, gv = 332748118, N = 1e6 + 5; long long qpow(long long a, int b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % p; a = a * a % p; b >>= 1; } return ret; } long long inv(long long a) { return qpow(a, p - 2); } int rev[N]; void getrev(int len) { for (int i = 0; i < len; i++) { rev[i] = rev[i >> 1] >> 1; if (i & 1) rev[i] |= (len >> 1); } } void ntt(vector<long long> &a, int len, int tp) { a.resize(len); for (int i = 0; i < len; i++) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int i = 2; i <= len; i <<= 1) { long long wn = qpow(tp == 1 ? g : gv, (p - 1) / i); int stp = i >> 1; for (int j = 0; j < len; j += i) { long long w = 1; for (int k = j; k < j + stp; k++) { long long s1 = a[k], s2 = a[k + stp] * w % p; w = w * wn % p; a[k] = (s1 + s2) % p, a[k + stp] = (s1 + p - s2) % p; } } } if (tp == -1) { long long lv = inv(len); for (int i = 0; i < len; i++) a[i] = a[i] * lv % p; } } vector<long long> operator*(vector<long long> a, vector<long long> b) { int len = 1, n = a.size() + b.size() - 1; while (len < n) len <<= 1; getrev(len); ntt(a, len, 1), ntt(b, len, 1); for (int i = 0; i < len; i++) a[i] = a[i] * b[i] % p; ntt(a, len, -1); a.resize(n); return a; } vector<long long> operator+(vector<long long> a, vector<long long> b) { if (a.size() < b.size()) swap(a, b); for (int i = 0; i < b.size(); i++) a[i] = (a[i] + b[i]) % p; return a; } vector<long long> operator-(vector<long long> a) { for (int i = 0; i < a.size(); i++) a[i] = (p - a[i]) % p; return a; } vector<long long> operator-(vector<long long> a, vector<long long> b) { return a + (-b); } vector<long long> operator*(long long b, vector<long long> a) { for (int i = 0; i < a.size(); i++) a[i] = a[i] * b % p; return a; } vector<long long> operator/(vector<long long> a, long long b) { int v = inv(b); for (int i = 0; i < a.size(); i++) a[i] = a[i] * v % p; return a; } long long fac[N], finv[N], iv[N]; int main() { int n; long long m; scanf( %d%lld , &n, &m); n++; fac[0] = fac[1] = iv[1] = finv[0] = finv[1] = 1; for (int i = 2; i <= n; i++) fac[i] = fac[i - 1] * i % p, iv[i] = (p - p / i) * iv[p % i] % p, finv[i] = finv[i - 1] * iv[i] % p; vector<long long> a, b, c; a.resize(n), b.resize(n); for (int i = 0; i < n; i++) { int x; scanf( %d , &x); a[i] = x * 1ll * fac[i] % p, b[n - i - 1] = finv[i]; } c = a * b; for (int i = 0; i < n; i++) a[i] = c[n + i - 1] * 1ll * qpow(iv[i + 1], m % (p - 1)) % p, b[n - i - 1] = i & 1 ? p - finv[i] : finv[i]; c = a * b; for (int i = 0; i < n; i++) printf( %lld , c[n + i - 1] * 1ll * finv[i] % p); printf( n ); }
|
#include <bits/stdc++.h> using namespace std; int arr[1000001], dp[(1 << 22)], N; int main() { scanf( %d , &N); memset(dp, -1, sizeof dp); for (int i = 0; i < N; i++) { scanf( %d , &arr[i]); dp[arr[i]] = arr[i]; } for (int i = 0; i < (1 << 22); i++) { for (int j = 0; j < 22; j++) { if (i & (1 << j)) { dp[i] = max(dp[i], dp[i ^ (1 << j)]); } } } for (int i = 0; i < N; i++) { int need = dp[((1 << 22) - 1) ^ arr[i]]; printf( %d%c , need, i == N - 1 ? n : ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; string convert(string s) { int l = s.size(); if (l % 2 == 1) return s; string s1 = s.substr(0, l / 2), s2 = s.substr(l / 2); s1 = convert(s1); s2 = convert(s2); if (s1 < s2) return s1 + s2; else return s2 + s1; } bool equivalent(string s1, string s2) { s1 = convert(s1); s2 = convert(s2); return s1 == s2; } int main() { string a, b; cin >> a >> b; if (equivalent(a, b)) cout << YES << endl; else cout << NO << endl; }
|
#include <bits/stdc++.h> using namespace std; struct answer { int b, c; bool operator==(answer other) const { return b == other.b && c == other.c; } bool operator!=(answer other) const { return !(*this == other); } }; answer eval(string secret, string query) { string left_s, left_q; answer ans = {0, 0}; for (int i = 0; i < (int)(4); i++) { if (secret[i] == query[i]) ans.b++; else { left_s.push_back(secret[i]); left_q.push_back(query[i]); } } while (!left_s.empty()) { auto it = find(left_q.begin(), left_q.end(), left_s.back()); if (it != left_q.end()) { left_q.erase(it); ans.c++; } left_s.pop_back(); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vector<string> q(n); vector<answer> anss(n); for (int i = 0; i < (int)(n); i++) { cin >> q[i] >> anss[i].b >> anss[i].c; } vector<string> answers; for (int a = 0; a < 10; a++) { for (int b = 0; b < 10; b++) { if (a == b) continue; for (int c = 0; c < 10; c++) { if (a == c || b == c) continue; for (int d = 0; d < 10; d++) { if (a == d || b == d || c == d) continue; string s; s.push_back( 0 + a); s.push_back( 0 + b); s.push_back( 0 + c); s.push_back( 0 + d); bool ok = true; for (int i = 0; i < (int)(n); i++) { if (anss[i] != eval(s, q[i])) { ok = false; } } if (ok) { answers.push_back(s); } } } } } if (answers.empty()) { cout << Incorrect data << endl; } else if (answers.size() > 1) { cout << Need more data << endl; } else { cout << answers[0] << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000006; long long num[MAXN << 2]; long long sum[MAXN << 2]; long long N; void pushup(int rt) { num[rt] = num[rt << 1] + num[rt << 1 | 1]; sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } void update(int P, int C, int l, int r, int rt) { if (l == r) { num[rt] += C; sum[rt] += 1ll * P * C; return; } int m = (l + r) / 2; if (P <= m) update(P, C, l, m, rt << 1); else update(P, C, m + 1, r, rt << 1 | 1); pushup(rt); } long long query(int K, int l, int r, int rt) { if (l == r) { if (l == MAXN) { return 0; } if (K > 0) { return 1ll * K * l; } else return 0; } int m = (l + r) / 2; if (num[rt << 1] >= K) { return query(K, l, m, rt << 1); } else { return sum[rt << 1] + query(K - num[rt << 1], m + 1, r, rt << 1 | 1); } } vector<pair<int, int> > C[MAXN]; vector<pair<int, int> > O[MAXN]; int main() { int K, M; scanf( %d%d%d , &N, &K, &M); int l, r, c, p; for (int i = 0; i < M; i++) { scanf( %d%d%d%d , &l, &r, &c, &p); C[l].push_back(make_pair(p, c)); O[r].push_back(make_pair(p, c)); } long long ans = 0; for (int i = 1; i <= N; i++) { for (int j = 0; j < C[i].size(); j++) update(C[i][j].first, C[i][j].second, 1, MAXN, 1); ans += query(K, 1, MAXN, 1); for (int j = 0; j < O[i].size(); j++) update(O[i][j].first, -O[i][j].second, 1, MAXN, 1); } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long dp[1000000 + 9] = {0}; long long a1[1000000 + 9] = {0}; long long a2[1000000 + 9] = {0}; long long a3[1000000 + 9] = {0}; long long a4[1000000 + 9] = {0}; long long a5[1000000 + 9] = {0}; long long a6[1000000 + 9] = {0}; long long a7[1000000 + 9] = {0}; long long a8[1000000 + 9] = {0}; long long a9[1000000 + 9] = {0}; int main() { long long a, b, c = 0, d, e, i, j, k, l = 0; for (i = 1; i <= 1000000; i++) { a = i; xx: c = 1; while (a) { b = a % 10; a /= 10; if (b > 0) c *= b; } if (c > 9 && dp[c] == 0) { a = c; goto xx; } else { if (dp[c] == 0) dp[i] = c; else dp[i] = dp[c]; } } for (i = 1; i <= 1000000; i++) { a1[i] += a1[i - 1]; a2[i] += a2[i - 1]; a3[i] += a3[i - 1]; a4[i] += a4[i - 1]; a5[i] += a5[i - 1]; a6[i] += a6[i - 1]; a7[i] += a7[i - 1]; a8[i] += a8[i - 1]; a9[i] += a9[i - 1]; if (dp[i] == 1) a1[i]++; if (dp[i] == 2) a2[i]++; if (dp[i] == 3) a3[i]++; if (dp[i] == 4) a4[i]++; if (dp[i] == 5) a5[i]++; if (dp[i] == 6) a6[i]++; if (dp[i] == 7) a7[i]++; if (dp[i] == 8) a8[i]++; if (dp[i] == 9) a9[i]++; } cin >> a; while (a--) { cin >> c >> d >> e; l = 0; if (e == 1) { l = a1[d] - a1[c - 1]; } if (e == 2) { l = a2[d] - a2[c - 1]; } if (e == 3) { l = a3[d] - a3[c - 1]; } if (e == 4) { l = a4[d] - a4[c - 1]; } if (e == 5) { l = a5[d] - a5[c - 1]; } if (e == 6) { l = a6[d] - a6[c - 1]; } if (e == 7) { l = a7[d] - a7[c - 1]; } if (e == 8) { l = a8[d] - a8[c - 1]; } if (e == 9) { l = a9[d] - a9[c - 1]; } cout << l << endl; } }
|
#include <bits/stdc++.h> using namespace std; bool comp(int a, int b) { return (a > b); } int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } int main(void) { int n, p; cin >> n >> p; int l[n], r[n], i; long long int c, cp, l1, l2; double ans = 0; for (i = 0; i < n; ++i) cin >> l[i] >> r[i]; for (i = 0; i < n; ++i) { l1 = r[i] - l[i] + 1; l2 = r[(i - 1 + n) % n] - l[(i - 1 + n) % n] + 1; c = r[i] / p - (l[i] - 1) / p; cp = r[(i - 1 + n) % n] / p - (l[(i - 1 + n) % n] - 1) / p; ans += (2000.0 * (c * l2 + cp * l1 - (c * cp))) / (l1 * l2); } printf( %.10lf , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long int; using pii = pair<int, int>; using pll = pair<ll, ll>; using maxHeap = priority_queue<int>; using minHeap = priority_queue<int, vector<int>, greater<int>>; int main() { ios::sync_with_stdio(0), cin.tie(0); int t, x; double d, p; cout << fixed << setprecision(10); for (cin >> t; t; t--) { cin >> x; d = x; if (x * x - 4 * x < 0) { cout << N n ; } else { p = sqrt(d * d - 4 * d); cout << Y << (d - p) / 2 << << (d + p) / 2 << n ; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m; int const N = 3e5 + 3; int ans[N]; int temp[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; vector<pair<int, pair<int, int>>> edges; for (int i = (0); i < (m); ++i) { int u, v, w; cin >> u >> v >> w; --u; --v; edges.push_back({w, {u, v}}); } sort(begin(edges), end(edges)); fill(ans, ans + n, 0); fill(temp, temp + n, 0); for (int i = (0); i < (m); ++i) { vector<int> cand; int cur = edges[i].first; while (i < m && edges[i].first == cur) { int u = edges[i].second.first; int v = edges[i].second.second; cand.push_back(v); temp[v] = max(temp[v], ans[u] + 1); ++i; } --i; for (auto& e : cand) { ans[e] = max(temp[e], ans[e]); temp[e] = 0; } } cout << *max_element(ans, ans + n) << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; struct vec { ll x; ll y; }; void solve() { ll t, sx, sy, ex, ey; cin >> t >> sx >> sy >> ex >> ey; string s; cin >> s; for (int i = 0, time = 0; i < s.size(); ++i, ++time) { vec d = {ex - sx, ey - sy}; char quater; if (d.x > 0 && d.y > 0) quater = 1; else if (d.x < 0 && d.y > 0) quater = 2; else if (d.x < 0 && d.y < 0) quater = 3; else if (d.x > 0 && d.y < 0) quater = 4; else if (d.x > 0 && d.y == 0) quater = 5; else if (d.x == 0 && d.y > 0) quater = 6; else if (d.x < 0 && d.y == 0) quater = 7; else quater = 8; switch (s[i]) { case E : if (quater == 1 || quater == 4 || quater == 5) sx++; else continue; break; case N : if (quater == 1 || quater == 2 || quater == 6) sy++; else continue; break; case W : if (quater == 2 || quater == 3 || quater == 7) sx--; else continue; break; case S : if (quater == 3 || quater == 4 || quater == 8) sy--; else continue; } if (sx == ex && sy == ey) { cout << time + 1 << n ; return; } } cout << -1 << n ; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t = 1; while (t-- > 0) { solve(); } }
|
#include <bits/stdc++.h> using namespace std; string str; int main() { cin >> str; int hun, ten, one; if (str.size() == 1) { if (str[0] == 8 || str[0] == 0 ) { cout << YES n ; cout << str << endl; } else cout << NO n << endl; } else if (str.size() == 2) { ten = str[0] - 0 ; ten *= 10; one = str[1] - 0 ; if ((ten + one) % 8 == 0) { cout << YES n ; cout << str << endl; } else if ((ten / 10 == 8) || one == 8) { cout << YES n ; cout << 8 << endl; } else if (!ten || !one) { cout << YES n ; cout << 0 << endl; } else cout << NO n << endl; } else { for (int i = 0; i < str.size(); i++) if (str[i] == 8 || str[i] == 0 ) { cout << YES n ; cout << str[i] << endl; return 0; } for (int i = 0; i < str.size() - 1; i++) { ten = str[i] - 0 ; ten *= 10; for (int j = i + 1; j < str.size(); j++) { one = str[j] - 0 ; int temp = ten + one; if (temp % 8 == 0) { cout << YES n ; cout << temp << endl; return 0; } } } for (int i = 0; i < str.size() - 2; i++) { hun = str[i] - 0 ; hun *= 100; for (int j = i + 1; j < str.size() - 1; j++) { ten = str[j] - 0 ; ten *= 10; for (int k = j + 1; k < str.size(); k++) { one = str[k] - 0 ; int temp = hun + ten + one; if (temp % 8 == 0) { cout << YES n ; cout << temp << endl; return 0; } } } } cout << NO n << endl; } return 0; }
|
#include <bits/stdc++.h> long long n, i, res; using namespace std; int main() { cin >> n; long long a[n]; for (i = 0; i < n; i++) cin >> a[i]; int l = 0, r = n - 1; long long suma = 0, sumb = 0; while (l <= r) { if (suma < sumb) { suma += a[l]; l++; } else { if (suma == sumb) { res = max(res, suma); suma += a[l]; l++; } else { sumb += a[r]; r--; } } } if (suma == sumb) res = max(res, suma); cout << res; return 0; }
|
#include <bits/stdc++.h> int main() { using namespace std; ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k, t; cin >> n >> m >> k >> t; vector<pair<int, int>> w; for (int i = 0, r, c; i < k; i++, w.push_back(make_pair(r, c))) cin >> r >> c; sort(w.begin(), w.end()); const char* s[] = { Carrots , Kiwis , Grapes }; for (int r, c; t--;) { cin >> r >> c; auto v = make_pair(r, c); auto vi = upper_bound(w.begin(), w.end(), v) - w.begin(); int i = (r - 1) * m + c - 1; if (vi && w[vi - 1] == v) cout << Waste << n ; else cout << s[(i - vi) % 3] << n ; } }
|
#include <bits/stdc++.h> int mut(int x) { if (x < 0) return -x; else return x; } int d[1000001][2]; int d1[1000001][2]; int main(void) { int ax; int ay; int bx; int by; int cx; int cy; int c = 0; int min = 99999; scanf( %d%d%d%d%d%d , &ax, &ay, &bx, &by, &cx, &cy); int x = ax; int y = ay; d[c][0] = ax; d[c][1] = ay; c++; if (bx >= ax && by >= ay) { for (int i = y; i <= by; i++) { for (int j = x; j <= bx; j++) { if (min > mut(i - cy) + mut(j - cx)) { min = mut(i - cy) + mut(j - cx); d1[min][0] = j; d1[min][1] = i; } } } } else if (bx <= ax && by <= ay) { for (int i = y; i >= by; i--) { for (int j = x; j >= bx; j--) { if (min > mut(i - cy) + mut(j - cx)) { min = mut(i - cy) + mut(j - cx); d1[min][0] = j; d1[min][1] = i; } } } } else if (bx >= ax && by <= ay) { for (int i = y; i >= by; i--) { for (int j = x; j <= bx; j++) { if (min > mut(i - cy) + mut(j - cx)) { min = mut(i - cy) + mut(j - cx); d1[min][0] = j; d1[min][1] = i; } } } } else if (bx <= ax && by >= ay) { for (int i = y; i <= by; i++) { for (int j = x; j >= bx; j--) { if (min > mut(i - cy) + mut(j - cx)) { min = mut(i - cy) + mut(j - cx); d1[min][0] = j; d1[min][1] = i; } } } } int x1 = d1[min][0]; int y1 = d1[min][1]; if (ax < x1) { while (x < x1) { x++; d[c][0] = x; d[c][1] = y; c++; } } else if (ax > x1) { while (x > x1) { x--; d[c][0] = x; d[c][1] = y; c++; } } if (ay > y1) { while (y > y1) { y--; d[c][0] = x; d[c][1] = y; c++; } } else if (ay < y1) { while (y < y1) { y++; d[c][0] = x; d[c][1] = y; c++; } } x = x1; y = y1; if (x1 < bx) { while (x < bx) { x++; d[c][0] = x; d[c][1] = y; c++; } } else if (x1 > bx) { while (x > bx) { x--; d[c][0] = x; d[c][1] = y; c++; } } if (y1 > by) { while (y > by) { y--; d[c][0] = x; d[c][1] = y; c++; } } else if (y1 < by) { while (y < by) { y++; d[c][0] = x; d[c][1] = y; c++; } } x = x1; y = y1; if (x1 < cx) { while (x1 < cx) { x1++; d[c][0] = x1; d[c][1] = y1; c++; } } else if (x1 > cx) { while (x1 > cx) { x1--; d[c][0] = x1; d[c][1] = y1; c++; } } if (y1 < cy) { while (y1 < cy) { y1++; d[c][0] = x1; d[c][1] = y1; c++; } } else if (y1 > cy) { while (y1 > cy) { y1--; d[c][0] = x1; d[c][1] = y1; c++; } } printf( %d n , c); for (int i = 0; i < c; i++) { printf( %d %d n , d[i][0], d[i][1]); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int q; int h, w, k; int a[600][600]; int dist[17][17]; int dp[17][65539]; int from[17][65539]; int pos[17]; int getdown(int x, int y) { int sum = 0; for (int i = x - 4; i <= x; i++) { for (int j = y - 2; j <= y + 2; j++) { sum += a[i][j]; } } return sum; } int getup(int x, int y) { int sum = 0; for (int i = x; i <= x + 4; i++) { for (int j = y - 2; j <= y + 2; j++) { sum += a[i][j]; } } return sum; } int main() { cin >> q; for (int ii = 0; ii < q; ii++) { scanf( %d %d %d , &h, &w, &k); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { scanf( %d , &a[i][j]); } } for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { if (i != j) { dist[i][j] = 0; for (int kk = 5; kk < w - 5; kk++) { dist[i][j] += abs(getdown((i + 1) * (h / k) - 1, kk) - getup(j * (h / k), kk)); } } } } for (int mask = 0; mask < (1 << k); mask++) { for (int who = 0; who < k; who++) { dp[who][mask] = 1000000000; } } for (int mask = 0; mask < (1 << k); mask++) { for (int who = 0; who < k; who++) { if (!((1 << who) & mask)) { continue; } if (mask == (1 << who)) { dp[who][mask] = 0; } if (dp[who][mask] == 1000000000) { continue; } for (int next = 0; next < k; next++) { if (!((1 << next) & mask)) { int NewMask = mask | (1 << next); if (dp[next][NewMask] > dp[who][mask] + dist[who][next]) { dp[next][NewMask] = dp[who][mask] + dist[who][next]; from[next][NewMask] = who; } } } } } int ans = 1000000000; int i, j; for (int who = 0; who < k; who++) { ans = min(ans, dp[who][(1 << k) - 1]); if (ans == dp[who][(1 << k) - 1]) { i = who; j = (1 << k) - 1; } } vector<int> anss; while (j != 0) { int ni = from[i][j]; anss.push_back(i + 1); j ^= (1 << i); i = ni; } reverse(anss.begin(), anss.end()); for (int i = 0; i < (int)anss.size(); i++) { pos[anss[i]] = i + 1; } for (int i = 1; i <= k; i++) { printf( %d , pos[i]); } printf( n ); } return 0; }
|
#include <bits/stdc++.h> #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define int long long int #define double long double #define endl n const int mod = 1000000007; using namespace std; int modExpo(int x , int n) { int ans = 1; while (n > 0) { if ( n & 1 == 1 ) ans = ( ans * x ) % mod; x = ( x * x ) % mod; n = n / 2; } return ans; } void solve() { vector<int> v(4); for(int i=0;i<4;i++) cin>>v[i]; int x = max(v[0] , v[1]); int y = max(v[2] , v[3]); if(x < v[2] && x < v[3]) { cout<< NO n ; return; } if(y < v[0] && y < v[1]) { cout<< NO n ; return; } cout<< YES n ; // cout<<x<<y; } int32_t main() { #ifndef ONLINE_JUDGE freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif fast; int tt; cin >> tt; while (tt--) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 2 * 100 * 1000 + 10; string s, t; int d[N]; int LastSeen[N]; void input() { cin >> s >> t; return; } void PrePros() { int keep = (int)(t.size()) - 1, pnt = (int)(s.size()) - 1; while (keep >= 0) { if (s[pnt] == t[keep]) { LastSeen[keep] = pnt; keep--; } pnt--; } LastSeen[t.size()] = s.size(); t.push_back( * ); return; } void solve() { PrePros(); int lastfound = 0; for (int i = 0; i < s.size(); i++) { d[i] = LastSeen[lastfound] - i; if (s[i] == t[lastfound]) lastfound++; } return; } void output() { int ans = 0; for (int i = 0; i < s.size(); i++) ans = max(ans, d[i]); cout << ans << endl; return; } int main() { input(); solve(); output(); return 0; }
|
#include <bits/stdc++.h> using namespace std; void fastio() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } void input() {} int main() { fastio(); input(); long long int n; cin >> n; long long int sum = -1; map<long long int, long long int> make_pair; for (long long int i = 0; i < n; i++) { long long int x; cin >> x; make_pair[x - i] += x; sum = max(sum, make_pair[x - i]); } cout << sum << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; double a, b, c, d; int main() { char st = e ; cin >> a >> b >> c >> d; if (a / b == b / c && b / c == c / d) st = g ; if (a - b == b - c && b - c == c - d) st = a ; if (st == e ) cout << 42 << endl; else if (st == g ) { int d1 = d, b1 = b, a1 = a; if (d * b / a == d1 * b1 / a1) cout << d * b / a << endl; else cout << 42 << endl; } else cout << d + b - a << endl; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using pii = pair<int, int>; using vi = vector<int>; const int N = 1e5 + 100; int n, d[N], f[N][2], ans; vi G[N]; void dfs(int x, int fz) { f[x][0] = 0, f[x][1] = 1; for (int y : G[x]) if (y != fz) { dfs(y, x); ans = max({ans, f[x][0] + f[y][0], f[x][0] + f[y][1], f[x][1] + f[y][0]}); f[x][0] = max({f[x][0], f[y][0] + d[x] - 2, f[y][1] + d[x] - 2}); f[x][1] = max({f[x][1], f[y][0] + 1}); } ans = max(ans, f[x][1]); } signed main() { scanf( %d , &n); for (int i = 1; i < n; i++) { int x, y; scanf( %d %d , &x, &y); G[x].push_back(y), G[y].push_back(x); d[x]++, d[y]++; } dfs(1, 0); printf( %d n , ans); fprintf(stderr, time=%.4f n , (db)clock() / CLOCKS_PER_SEC); return 0; }
|
#include <bits/stdc++.h> using namespace std; int32_t main() { long long r, g, b; cin >> r >> g >> b; long long ans = 0; long long tr, tg, tb; for (long long i = 0; i <= min({r, g, b, 2LL}); i++) { long long t = i; tr = r; tg = g; tb = b; tr -= i; tg -= i; tb -= i; t += (tr / 3 + tg / 3 + tb / 3); ans = max(ans, t); } cout << ans; }
|
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } const long long int MOD = 1e9 + 7, INF = 1e18; long long int dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, dy[8] = {1, 0, 0, -1, -1, -1, 1, 1}; struct edge { long long int cost; long long int u, v; }; long long int N, M; long long int mp[200][200]; long long int delta[200][200]; int main() { cin >> N >> M; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { for (long long int j = (0), j_end_ = (M); j < j_end_; j++) { cin >> mp[i][j]; } } for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { for (long long int j = (0), j_end_ = (M); j < j_end_; j++) { delta[i][j] = mp[i][j]; } } bool flag = true; vector<pair<long long int, long long int> > pos; vector<long long int> _row_ans; vector<long long int> _column_ans; _row_ans.push_back(0); if (mp[0][0] == 0) _column_ans.push_back(0); else _column_ans.push_back(1); for (long long int i = (0), i_end_ = (M - 1); i < i_end_; i++) { _column_ans.push_back(mp[0][i] ^ mp[0][i + 1] ^ _column_ans.back()); } for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { _row_ans.push_back(mp[i][0] ^ mp[i + 1][0] ^ _row_ans.back()); for (long long int j = (0), j_end_ = (M - 1); j < j_end_; j++) { if ((mp[0][j] ^ mp[0][j + 1]) != (mp[i + 1][j] ^ mp[i + 1][j + 1])) { pos.push_back({i + 1, j + 1}); flag = false; } } } if (flag) { cout << YES << n ; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) cout << _row_ans[i]; cout << n ; for (long long int i = (0), i_end_ = (M); i < i_end_; i++) cout << _column_ans[i]; cout << n ; return 0; } if (((long long int)(pos).size()) != 1) { bool _flag = true; for (long long int i = (0), i_end_ = (((long long int)(pos).size())); i < i_end_; i++) { if (pos[i].first == 0) _flag = false; if (pos[0].second != pos[i].second) _flag = false; } for (long long int i = (0), i_end_ = (((long long int)(pos).size()) - 1); i < i_end_; i++) { if (pos[i].first + 1 != pos[i + 1].first) _flag = false; } if (((long long int)(pos).size()) != N - 1) _flag = false; if (_flag) { long long int r = pos[0].second; pos.clear(); pos.push_back({0, r}); } else { cout << NO << n ; return 0; } } for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { for (long long int j = (0), j_end_ = (M); j < j_end_; j++) { if (i * M + j >= pos[0].first * M + pos[0].second) mp[i][j] ^= 1; } } _row_ans.clear(); _column_ans.clear(); _row_ans.push_back(0); if (mp[0][0] == 0) _column_ans.push_back(0); else _column_ans.push_back(1); for (long long int i = (0), i_end_ = (M - 1); i < i_end_; i++) { _column_ans.push_back(mp[0][i] ^ mp[0][i + 1] ^ _column_ans.back()); } for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { _row_ans.push_back(mp[i][0] ^ mp[i + 1][0] ^ _row_ans.back()); } cout << YES << n ; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) cout << _row_ans[i]; cout << n ; for (long long int i = (0), i_end_ = (M); i < i_end_; i++) cout << _column_ans[i]; cout << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long MOD = (long long)(1e9 + 7); const int inf = (int)INFINITY; const long long INF = (long long)INFINITY; const int MAX = (int)(2e5 + 5); const int maxn = 2e5 + 5; int n, m, k, p[maxn], path[maxn]; vector<int> in[maxn], out[maxn]; int maxa, mina; set<pair<long long, long long> > st; void dijkstra() { while (st.size()) { pair<long long, long long> x = *st.begin(); st.erase(x); int i = x.second; for (auto A : in[i]) { if (path[i] + 1 < path[A]) { st.erase(pair<long long, long long>(path[A], A)); path[A] = path[i] + 1; st.insert(pair<long long, long long>(path[A], A)); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (long long i = 0; i < (m); ++i) { int u, v; cin >> u >> v; out[u].push_back(v); in[v].push_back(u); } cin >> k; for (int i = 1; i <= k; ++i) cin >> p[i]; memset(path, 63, sizeof path); path[p[k]] = 0; st.insert(pair<long long, long long>(0, p[k])); dijkstra(); for (int i = 2; i <= k; ++i) { if (path[p[i]] != path[p[i - 1]] - 1) { ++mina; ++maxa; } else { int e = 0; for (auto A : out[p[i - 1]]) if (A != p[i] && path[A] == path[p[i]]) e = 1; maxa += e; } } cout << mina << << maxa; return 0; }
|
#include <bits/stdc++.h> using namespace std; int x[1010], y[1010]; int lef(int x1, int y1, int x2, int y2) { if (x1) x1 = (x1 > 0 ? 1 : -1); if (y1) y1 = (y1 > 0 ? 1 : -1); if (x2) x2 = (x2 > 0 ? 1 : -1); if (y2) y2 = (y2 > 0 ? 1 : -1); if (x1 == 0 && y1 == 1) { if (x2 == 1) return 0; else return 1; } if (x1 == 0 && y1 == -1) { if (x2 == 1) return 1; else return 0; } return lef(-x2, -y2, -x1, -y1) > 0 ? 0 : 1; } int main() { int n, i, num1, num2; while (cin >> n) { for (i = 0; i <= n; i++) { scanf( %d %d , &x[i], &y[i]); } x[n + 1] = x[1]; y[n + 1] = y[1]; num1 = num2 = 0; for (i = 1; i <= n; i++) { if (lef((x[i] - x[i - 1]), (y[i] - y[i - 1]), (x[i + 1] - x[i]), (y[i + 1] - y[i]))) num1++; else num2++; } cout << min(num1, num2) << endl; } }
|
#include <bits/stdc++.h> using namespace std; char s1[100100], s2[100100]; long long ini; long long mod(long long a) { return ((a % 1000000007LL) + 1000000007LL) % 1000000007LL; } long long func(char* s, int t, bool som) { long long r = 0; long long ul = 0; long long soma = 0, ls = ini; long long qt = 0LL; bool tem = false; long long sum, lst, val, bas, qtd; sum = lst = ini; val = 0; bas = 3LL; qtd = 1LL; for (int i = t - 1, j = 0; i >= 0; i--, j++) { ul = (ul * 10 + s[j] - 0 ) % 1000000007LL; long long bas2 = (bas * bas) % 1000000007LL; if (s[i] == 7 ) { if (qt > 0) r = (r + ((qt - 1) * bas2) % 1000000007LL + (bas * (2 * soma - ls - ini)) % 1000000007LL + (lst * (ini + bas)) % 1000000007LL) % 1000000007LL; r = (r + val) % 1000000007LL; soma = (soma + sum + qt * bas) % 1000000007LL; qt = (qt + qtd) % 1000000007LL; if (tem) ls = (ls + bas) % 1000000007LL; tem = true; } else if (!tem) ls = (ls + bas) % 1000000007LL; val = (2 * val + (lst * (bas + ini)) % 1000000007LL + (bas2 * (qtd - 1)) % 1000000007LL + (bas * (2 * sum - lst - ini)) % 1000000007LL) % 1000000007LL; sum = (2 * sum + qtd * bas) % 1000000007LL; lst = (lst + bas) % 1000000007LL; bas = (bas * 10) % 1000000007LL; qtd = (qtd * 2) % 1000000007LL; } if (tem) return (r + ul * ls) % 1000000007LL; return r; } int main() { int tam; scanf( %s %s , s1, s2); tam = strlen(s1); ini = 0; for (int i = 0; i < tam; i++) ini = (ini * 10 + 4LL) % 1000000007LL; cout << mod(func(s2, tam, true) - func(s1, tam, false)) << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; long double eps = -1e12; inline bool eq(const long double &x, const long double &y) { return abs(x - y) < eps || abs(x - y) < eps * abs(x); } int main() { long double x, y, z; cin >> x >> y >> z; string expression[12] = { x^y^z , x^z^y , (x^y)^z , (x^z)^y , y^x^z , y^z^x , (y^x)^z , (y^z)^x , z^x^y , z^y^x , (z^x)^y , (z^y)^x }; vector<long double> ans(12, 0); long double res = -1000; long double logx = log(x); long double logy = log(y); long double logz = log(z); ans[0] = pow(y, z) * logx; ans[1] = pow(z, y) * logx; ans[2] = logx * y * z; ans[3] = ans[2]; ans[4] = pow(x, z) * logy; ans[5] = pow(z, x) * logy; ans[6] = logy * x * z; ans[7] = ans[6]; ans[8] = pow(x, y) * logz; ans[9] = pow(y, x) * logz; ans[10] = logz * y * x; ans[11] = ans[10]; int index = 0; for (int i = 0; i < 12; ++i) { if (ans[i] > res && !eq(ans[i], res)) { res = ans[i]; index = i; } } cout << expression[index] << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; struct Point { double X, Y, Z; void Get() { scanf( %lf%lf%lf , &X, &Y, &Z); } }; Point operator-(Point A, Point B) { return (Point){A.X - B.X, A.Y - B.Y, A.Z - B.Z}; } double operator*(Point A, Point B) { return A.X * B.X + A.Y * B.Y + A.Z * B.Z; } const double inf = 1e9; const double eps = 1e-6; double Solve(Point A, Point V, double R) { double a = V * V, b = A * V * 2, c = A * A - R * R; double Delta = b * b - 4 * a * c; if (Delta < -eps) return inf; Delta = sqrt(fabs(Delta)); double T0 = (-b + Delta) / (2 * a), T1 = (-b - Delta) / (2 * a); return (T0 < 0) ? inf : ((T1 < 0) ? T0 : T1); } int main() { Point O, V; O.Get(); V.Get(); double R, Ans = inf; scanf( %lf , &R); int N; scanf( %d , &N); while (N--) { Point _O; _O.Get(); double r; scanf( %lf , &r); Ans = min(Ans, Solve(O - _O, V, R + r)); int M; scanf( %d , &M); while (M--) { Point Delta; Delta.Get(); Ans = min(Ans, Solve(O - _O - Delta, V, R)); } } if (Ans == inf) printf( -1 n ); else printf( %0.10lf n , Ans); return 0; }
|
#include <bits/stdc++.h> struct Candidate { std::vector<int> d; Candidate(std::vector<int> d_) { d = d_; std::sort(d.begin(), d.end()); std::reverse(d.begin(), d.end()); } inline bool operator<(const Candidate &rhs) { if (d[0] < 0 || rhs.d[0] < 0) return d[0] < rhs.d[0]; for (int i = 0; i < static_cast<int>(d.size()); ++i) if (d[i] != rhs.d[i]) return d[i] < rhs.d[i]; return false; } }; int get_enemy_pos(std::vector<int> locations, bool mate) { std::cout << locations[0] + 1 << << locations[1] + 1 << << locations[2] + 1 << std::endl; if (!mate) { int pos; std::cin >> pos; return pos - 1; } else { return -1; } } std::vector<int> get_dist(std::vector<std::vector<int> > &adj, int src) { auto d = std::vector<int>(adj.size(), -1); auto q = std::queue<int>(); q.push(src); d[src] = 0; while (!q.empty()) { int at = q.front(); q.pop(); for (auto &next : adj[at]) { if (d[next] < 0) { d[next] = d[at] + 1; q.push(next); } } } return d; } int main() { int nodes, edges; std::cin >> nodes >> edges; auto adj = std::vector<std::vector<int> >(nodes, std::vector<int>()); for (int edge = 0; edge < edges; ++edge) { int src, dst; std::cin >> src >> dst; adj[src - 1].push_back(dst - 1); adj[dst - 1].push_back(src - 1); } srand(time(NULL)); auto dists = std::vector<std::vector<int> >(static_cast<int>(adj.size()), std::vector<int>()); int best_dist = nodes + 1; auto best_locs = std::vector<int>(3, -1); for (int iter = 0; iter < 5000; ++iter) { auto locs = std::vector<int>(3); for (int i = 0; i < static_cast<int>(locs.size()); ++i) { locs[i] = -1; bool ok = true; do { ok = true; locs[i] = rand() % nodes; for (int j = 0; j < i; ++j) { if (locs[i] == locs[j]) ok = false; } } while (!ok); if (dists[locs[i]].size() == 0) { dists[locs[i]] = get_dist(adj, locs[i]); } } int cand_dist = 1; for (int i = 0; i < nodes; ++i) { cand_dist = std::max( cand_dist, std::min(dists[locs[0]][i], std::min(dists[locs[1]][i], dists[locs[2]][i]))); } if (cand_dist < best_dist) { best_dist = cand_dist; best_locs = locs; } } auto locs = best_locs; auto enemy_pos = get_enemy_pos(locs, false); while (enemy_pos >= 0) { if (enemy_pos == locs[0] || enemy_pos == locs[1] || enemy_pos == locs[2]) { break; } auto targets = std::vector<int>(); targets.push_back(enemy_pos); for (int repeats = 0; repeats < 0; ++repeats) { auto ntargets = std::vector<int>(); for (auto &t : targets) for (auto &x : adj[t]) { ntargets.push_back(x); } for (auto &nt : ntargets) targets.push_back(nt); std::sort(targets.begin(), targets.end()); targets.resize(std::unique(targets.begin(), targets.end()) - targets.begin()); } for (auto &x : targets) { if (dists[x].size() == 0) { dists[x] = get_dist(adj, x); } } auto d = std::vector<int>(static_cast<int>(locs.size())); auto best = Candidate(std::vector<int>(static_cast<int>(locs.size()), nodes + 1)); static int best_locs[] = {-1, -1, -1}; static int by[3]; static int choice[3]; for (int i = 0; i < 3; ++i) { std::random_shuffle(adj[locs[i]].begin(), adj[locs[i]].end()); } for (choice[0] = 0; choice[0] <= adj[locs[0]].size(); ++choice[0]) for (choice[1] = 0; choice[1] <= adj[locs[1]].size(); ++choice[1]) for (choice[2] = 0; choice[2] <= adj[locs[2]].size(); ++choice[2]) { bool mate = false; for (int i = 0; i < 3; ++i) { by[i] = (choice[i] == 0 ? locs[i] : adj[locs[i]][choice[i] - 1]); if (by[i] == enemy_pos) mate = true; } if (by[0] == by[1] || by[1] == by[2] || by[0] == by[2]) { continue; } for (int j = 0; j < 3; ++j) d[j] = dists[targets[0]][by[j]]; auto cand = Candidate(d); if (mate) { cand.d[0] = -1; } if (cand < best) { best = cand; for (int i = 0; i < 3; ++i) { best_locs[i] = by[i]; } } } locs = std::vector<int>(best_locs, best_locs + 3); enemy_pos = get_enemy_pos(locs, best.d[0] < 0); } return 0; }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.