func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int main() { int i = 0, j = 0, cs = 0, in; int n; cin >> n; vector<int> v; int sum = 0, s = 0; for (int i = 1; i < n; i++) { cout << ? << i << << n << n ; int x; cin >> x; if (i > 1) { v.push_back(sum - x - s); s += v.back(); } else sum = x; } cout << ? << 1 << << n - 1 << n ; int x; cin >> x; cout << ! ; for (int i = 0; i < v.size(); i++) cout << v[i] << ; cout << x - s << << sum - x << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5; int N, M, A[MAXN + 10], ans[MAXN + 10]; int main() { int i, j; scanf( %d%d , &N, &M); long long sum = 0; for (i = 1; i <= M; i++) scanf( %d , &A[i]), sum += A[i]; if (sum < N) return !printf( -1 ); int last = 987654321; for (i = M; i >= 1; i--) { int t = min(N - A[i] + 1, last - 1); if (t <= 0) return !printf( -1 ); ans[i] = t; last = t; } int start = 1; for (i = 1; i <= M; i++) { if (start >= ans[i]) break; ans[i] = start; start = A[i] + ans[i]; } for (i = 1; i <= M; i++) printf( %d , ans[i]); }
#include <bits/stdc++.h> using namespace std; int n; double p[100008]; struct Tprogram { void open() { freopen( 1.in , r , stdin); freopen( 1.out , w , stdout); } void close() {} void init() { scanf( %d , &n); for (int i = 0; i <= n; i++) scanf( %lf , &p[i]); } void work() { double ans = 0; for (int i = 0; i <= n >> 1; i++) for (int j = (n >> 1) + 1; j <= n; j++) ans = ((p[i] * (2 * j - n) + p[j] * (n - 2 * i)) / (2 * j - 2 * i) > ans ? (p[i] * (2 * j - n) + p[j] * (n - 2 * i)) / (2 * j - 2 * i) : ans); printf( %.10lf n , ans); } } Program; int main() { Program.init(); Program.work(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; int m; cin >> n >> m; vector<int> zahlen(n); for (int i = 0; i < n; i++) cin >> zahlen[i]; for (int i = 0; i < m; i++) { int l, r, x; cin >> l >> r >> x; l--; x--; r--; int tr = zahlen[x]; int count = 0; for (int i = l; i <= r; i++) { if (zahlen[i] < tr) count++; } if ((l + count) == x) cout << Yes << endl; else cout << No << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; using vii = vector<ii>; const ll MOD = 998244353; const ll INF = 1e15 + 9; const int MAXN = 1000006; int n, m, a[MAXN], p[MAXN], b[MAXN]; int rev[MAXN]; vi pos[MAXN]; vll sum[MAXN]; ll dp[MAXN], negsum[MAXN]; inline ll isum(int val, int R) { int index = lower_bound(pos[val].begin(), pos[val].end(), R) - pos[val].begin(); return sum[val][index]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { cin >> p[i]; } cin >> m; for (int i = 1; i <= m; ++i) { cin >> b[i]; } a[n + 1] = n + 1; b[m + 1] = n + 1; ++n, ++m; for (int i = 0; i <= m; ++i) { sum[2 * i + 1] = sum[2 * i] = vll(1); } for (int i = 0; i <= n; ++i) { int bi = lower_bound(b, b + m, a[i]) - b; a[i] = 2 * bi - (b[bi] != a[i]); pos[a[i]].push_back(i); ll prevsum = sum[a[i]].back(); sum[a[i]].push_back(prevsum + max(p[i], 0)); negsum[i + 1] = negsum[i] + min(p[i], 0); } for (int i = 1; i <= n; ++i) { if (a[i] & 1) { dp[i] = INF; continue; } auto it = lower_bound(pos[a[i] - 2].begin(), pos[a[i] - 2].end(), i); if (it == pos[a[i] - 2].begin()) { dp[i] = INF; continue; } --it; dp[i] = dp[*it] + isum(a[i] - 1, i) + isum(a[i], i) + negsum[i] - negsum[*it + 1]; it = lower_bound(pos[a[i]].begin(), pos[a[i]].end(), i); if (it != pos[a[i]].begin()) { --it; dp[i] = min(dp[i], dp[*it] + negsum[i + 1] - negsum[*it + 1]); } } if (dp[n] >= INF / 2) { cout << NO n ; } else { cout << YES n << dp[n] << n ; } }
#include <bits/stdc++.h> using namespace std; long long n, a, b, ans; vector<long long> v[100005]; vector<long long> res; long long cost(long long num) { res.clear(); long long sum = 0; long long need = num - v[0].size(); for (long long i = 1; i <= 100000; i++) { for (long long j = 0; j < v[i].size(); j++) { if (v[i].size() - j >= num) { need--; sum += v[i][j]; } else res.push_back(v[i][j]); } } sort(res.begin(), res.end()); for (long long i = 1; i <= need; i++) { sum += res[i - 1]; } return sum; } int main() { scanf( %lld , &n); for (long long i = 1; i <= n; i++) { scanf( %lld%lld , &a, &b); v[a].push_back(b); } for (long long i = 1; i <= 100000; i++) sort(v[i].begin(), v[i].end()); long long l = v[0].size(), r = n, m1, m2; ans = min(cost(l), cost(r)); while (l + 2 < r) { m1 = l + (r - l) / 3; m2 = r - (r - l) / 3; long long a1 = cost(m1); long long a2 = cost(m2); if (a1 > a2) l = m1, ans = min(ans, a2); else r = m2, ans = min(ans, a1); } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Container> void print(Container& container) { auto Start = container.begin(), End = container.end(); while (Start != End) cout << *(Start++) << ; cout << n ; } const int N = 5e2 + 1; int dp[2][N][N], a[N]; inline void solve() { int n, m, b, mod; cin >> n >> m >> b >> mod; for (int i = 1; i <= n; ++i) cin >> a[i]; dp[0][0][0] = 1; for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { for (int k = 0; k <= b; ++k) { dp[i & 1][j][k] = dp[i & 1 ^ 1][j][k]; if (j && k >= a[i]) dp[i & 1][j][k] += dp[i & 1][j - 1][k - a[i]]; dp[i & 1][j][k] %= mod; } } } int ans = 0; for (int k = 0; k <= b; ++k) ans = (ans + dp[n & 1][m][k]) % mod; cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; while (T--) solve(); }
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; getline(cin, s1); getline(cin, s2); sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); int j(0); for (int i = 0; i < s1.size(); i++) { while (s2[j] == ) j++; while (s1[i] == ) i++; if (s1[i] == s2[j]) j++; if (j == s2.size()) { cout << YES ; return 0; } } cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n; cin >> n; vector<int> list; int maxx, ma; for (int i = 0; i < n; i++) { int u; cin >> u; list.push_back(u); } int u1, u2, u3; int flag; for (int i = 1; i < n - 1; i++) { flag = 0; for (int j = 0; j < i; j++) { if (list[i] > list[j]) { flag = 1; u1 = j; break; } } for (int j = i + 1; j < n; j++) { if (list[i] > list[j]) { if (flag == 1) { flag = 2; u2 = j; break; } else { break; } } } if (flag == 2) { u3 = i; break; } } if (flag == 2) { cout << YES << endl; cout << u1 + 1 << << u3 + 1 << << u2 + 1 << endl; } else { cout << NO << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long a1[1000007], b1[1000007]; int main() { long long n, i, j, k = 0; cin >> n; for (i = 0; i < n; i++) { string st; cin >> st; long long si = st.size(), c1 = 0, c2 = 0; for (j = 0; j < si; j++) { if (st[j] == ( ) { c1++; } if (st[j] == ) ) { if (c1 > 0) { c1--; } else { c2++; } } } if (c1 == 0 && c2 == 0) k++; else if (c2 == 0) a1[c1]++; else if (c1 == 0) b1[c2]++; } long long ans = 0; ans = ans + (k / 2); for (i = 0; i <= 1000007; i++) { ans = ans + min(a1[i], b1[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int DEBUG = 0; bool isss(vector<int> s1, vector<int> s2) { int si1, si2; si1 = s1.size(); si2 = s2.size(); for (int i = 0; i < si1; i++) { bool ttt = false; for (int j = 0; j < si2; j++) { if (s1[i] == s2[j]) { ttt = true; } } if (!ttt) return false; } return true; } int main(int argc, char **argv) { DEBUG = (argc >= 2) ? atoi(argv[1]) : 0; int n; cin >> n; int m[n]; vector<int> a[n]; for (int i = 0; i < n; i++) { cin >> m[i]; for (int j = 0; j < m[i]; j++) { int tmp; cin >> tmp; a[i].push_back(tmp); } } bool isp[n]; for (int i = 0; i < n; i++) isp[i] = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { if (isss(a[i], a[j])) { isp[j] = false; } } } } for (int i = 0; i < n; i++) if (isp[i]) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int arr[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) cin >> arr[i][j]; } bool chk[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) chk[i][j] = 1; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (arr[i][j] % 2 == 0) continue; else { if (chk[i][j] == 1) chk[i][j] = 0; else chk[i][j] = 1; if (j + 1 <= 2) { if (chk[i][j + 1] == 1) chk[i][j + 1] = 0; else chk[i][j + 1] = 1; } if (j - 1 >= 0) { if (chk[i][j - 1] == 1) chk[i][j - 1] = 0; else chk[i][j - 1] = 1; } if (i + 1 <= 2) { if (chk[i + 1][j] == 1) chk[i + 1][j] = 0; else chk[i + 1][j] = 1; } if (i - 1 >= 0) { if (chk[i - 1][j] == 1) chk[i - 1][j] = 0; else chk[i - 1][j] = 1; } } } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << chk[i][j]; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = (long long)1e5 + 10; vector<long long> G[MAXN]; pair<long long, long long> p; vector<long long> v; long long n, x, y, k, ans, sum, flag; string s; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x >> y; v.push_back(x); v.push_back(y); } cin >> k; long long i = 0; while (!flag) { if (v[i] < k) { i++; } else { flag = 1; i--; } } cout << n - (i + 1) / 2; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, i, j, k; cin >> n; vector<string> v; vector<pair<int, string> > v1; for (i = 0; i < n; i++) { string s; cin >> s; v.push_back(s); v1.push_back(make_pair(s.length(), s)); } sort(v1.begin(), v1.end()); int flag = 1; for (i = n - 1; i >= 0; i--) { pair<int, string> temp = v1[i]; string s = temp.second; for (j = 0; j < i; j++) { pair<int, string> temp1 = v1[j]; string s1 = temp1.second; if (s.length() != s1.length()) { if (s.find(s1) != std::string::npos) continue; else { flag = 0; break; } } else { if (s != s1) { flag = 0; break; } } } if (flag == 0) break; } if (flag == 0) cout << NO n ; else { cout << YES n ; for (i = 0; i < n; i++) cout << v1[i].second << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 50 + 10; int a[maxn][maxn]; int dx[10] = {0, 2, 1, 0, -1, -2}; int dy[10] = {2, 0, 1, -2, -1, 0}; int sx, sy; struct Node { int x, y; }; int main() { int n; scanf( %d , &n); memset(a, -1, sizeof(a)); a[1][1] = 1; a[n][n] = 0; queue<Node> q; q.push((Node){1, 1}); while (!q.empty()) { Node tmp = q.front(); q.pop(); int x = tmp.x, y = tmp.y; for (int i = 0; i < 3; i++) { int tx = x + dx[i], ty = y + dy[i]; if (tx < 1 || ty < 1 || tx > n || ty > n) continue; if (a[tx][ty] != -1) continue; cout << ? << << x << << y << << tx << << ty << n ; cout.flush(); int t; cin >> t; a[tx][ty] = t ? a[x][y] : a[x][y] ^ 1; q.push((Node){tx, ty}); } } for (int i = 1; i <= n; i++) { if (i + 2 > n) break; for (int j = 1; j <= n; j++) { if (j + 2 > n) break; if (a[i][j] == 1 && a[i + 2][j + 2] == 0) { sx = i, sy = j; break; } } } int tmp; cout << ? << << sx << << sy + 1 << << sx + 1 << << sy + 2 << n ; cout.flush(); cin >> tmp; if (tmp) { cout << ? << << sx << << sy << << sx + 1 << << sy + 2 << n ; cout.flush(); cin >> tmp; if (tmp) { a[sx][sy + 1] = a[sx + 1][sy + 2] = 1; } else { if (a[sx][sy + 2] == 1) { a[sx][sy + 1] = a[sx + 1][sy + 2] = 0; } else { cout << ? << << sx << << sy + 1 << << sx + 2 << << sy + 2 << n ; cout.flush(); cin >> tmp; if (tmp) { a[sx][sy + 1] = a[sx + 1][sy + 2] = 0; } else { a[sx][sy + 1] = a[sx + 1][sy + 2] = 1; } } } } else { cout << ? << << sx << << sy << << sx + 1 << << sy + 2 << n ; cout.flush(); cin >> tmp; if (tmp) { a[sx][sy + 1] = 0; a[sx + 1][sy + 2] = 1; } else { cout << ? << << sx << << sy + 1 << << sx + 2 << << sy + 2 << n ; cout.flush(); cin >> tmp; if (tmp) { a[sx][sy + 1] = 0; a[sx + 1][sy + 2] = 1; } else { a[sx][sy + 1] = 1; a[sx + 1][sy + 2] = 0; } } } q.push((Node){sx, sy + 1}); while (!q.empty()) { Node tmp = q.front(); q.pop(); int x = tmp.x, y = tmp.y; for (int i = 0; i < 6; i++) { int tx = x + dx[i], ty = y + dy[i]; if (tx < 1 || ty < 1 || tx > n || ty > n) continue; if (a[tx][ty] != -1) continue; if (i < 3) cout << ? << << x << << y << << tx << << ty << n ; else cout << ? << << tx << << ty << << x << << y << n ; cout.flush(); int t; cin >> t; a[tx][ty] = t ? a[x][y] : a[x][y] ^ 1; q.push((Node){tx, ty}); } } cout << ! << n ; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << a[i][j]; } cout << n ; } cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, sum = 0, x, count = 0; cin >> n; long long int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum = sum + a[i]; } sort(a, a + n); if (sum % 2 == 0) { cout << sum; return 0; } else { for (i = 0; i < n; i++) { x = sum - a[i]; if (x % 2 == 0) { cout << x; return 0; } } } }
#include <bits/stdc++.h> using namespace std; bool m(int i, int j) { return (i % 10) > (j % 10); } int main() { int x, y; cin >> y >> x; vector<int> vi; int i = 0, j = 0; int k = 0; for (i = 0; i < y; i++) { cin >> j; k += (j % 10); vi.push_back(j); } sort(vi.begin(), vi.begin() + y, m); for (i = 0; i < y && x > 0; i++) { k = (10 - (vi[i] % 10)); if (x >= k && (vi[i] + k) <= 100) { x -= k; vi[i] += k; } } for (i = 0; i < y && x > 0; i++) { while ((vi[i] < 100) && (x >= 10)) { vi[i] += 10; x -= 10; } } k = 0; for (i = 0; i < y; i++) k += vi[i] / 10; cout << k << ; return EXIT_SUCCESS; }
#include <bits/stdc++.h> using namespace std; const int T = 1 << 19; struct nod { int best = 0, best2 = 0; int pref0 = 0, pref1 = 0, suf0 = 0, suf1 = 0; int pref01 = 0, pref10 = 0, suf01 = 0, suf10 = 0; int cnt0 = 0, cnt1 = 0; }; void umax(int &a, int b) { a = max(a, b); } nod merge(nod a, nod b) { nod c; c.cnt0 = a.cnt0 + b.cnt0; c.cnt1 = a.cnt1 + b.cnt1; c.best = max({a.best, b.best, a.suf01 + b.pref1, a.suf0 + b.pref01}); c.best2 = max({a.best2, b.best2, a.suf10 + b.pref0, a.suf1 + b.pref10}); c.suf0 = (b.cnt1 == 0 ? b.suf0 + a.suf0 : b.suf0); c.suf1 = (b.cnt0 == 0 ? b.suf1 + a.suf1 : b.suf1); c.pref0 = (a.cnt1 == 0 ? a.pref0 + b.pref0 : a.pref0); c.pref1 = (a.cnt0 == 0 ? a.pref1 + b.pref1 : a.pref1); c.pref01 = (a.cnt1 == 0 ? a.cnt0 + b.pref01 : a.pref01); if (a.pref01 == a.cnt0 + a.cnt1) umax(c.pref01, a.pref01 + b.pref1); umax(c.pref01, max(c.pref0, c.pref1)); c.pref10 = (a.cnt0 == 0 ? a.cnt1 + b.pref10 : a.pref10); if (a.pref10 == a.cnt0 + a.cnt1) umax(c.pref10, a.pref10 + b.pref0); umax(c.pref10, max(c.pref0, c.pref1)); c.suf01 = (b.cnt0 == 0 ? b.cnt1 + a.suf01 : b.suf01); if (b.suf01 == b.cnt0 + b.cnt1) umax(c.suf01, b.suf01 + a.suf0); umax(c.suf01, max(c.suf0, c.suf1)); c.suf10 = (b.cnt1 == 0 ? b.cnt0 + a.suf10 : b.suf10); if (b.suf10 == b.cnt0 + b.cnt1) umax(c.suf10, b.suf10 + a.suf1); umax(c.suf10, max(c.suf0, c.suf1)); return c; } nod t[T + T]; bool tag[T + T]; int n, m; char s[T]; void odwroc(int v) { swap(t[v].best, t[v].best2); swap(t[v].cnt0, t[v].cnt1); swap(t[v].pref01, t[v].pref10); swap(t[v].suf01, t[v].suf10); swap(t[v].pref0, t[v].pref1); swap(t[v].suf0, t[v].suf1); } void push(int v) { if (!tag[v]) return; for (auto u : {2 * v, 2 * v + 1}) { tag[u] ^= 1; odwroc(u); } tag[v] = 0; } void invert(int x, int y, int v = 1, int l = 0, int r = T - 1) { if (x <= l && r <= y) { tag[v] ^= 1; odwroc(v); return; } push(v); int m = (l + r) / 2; if (x <= m) invert(x, y, 2 * v, l, m); if (m < y) invert(x, y, 2 * v + 1, m + 1, r); t[v] = merge(t[2 * v], t[2 * v + 1]); } nod query(int x, int y, int v = 1, int l = 0, int r = T - 1) { if (x <= l && r <= y) { return t[v]; } push(v); nod res; int m = (l + r) / 2; if (x <= m) res = merge(res, query(x, y, 2 * v, l, m)); if (m < y) res = merge(res, query(x, y, 2 * v + 1, m + 1, r)); return res; } int main() { scanf( %d%d , &n, &m); scanf( %s , s + 1); for (int i = 1; i <= n; ++i) { int v = i + T; t[v].best = 1; if (s[i] == > ) t[v].pref0 = t[v].suf0 = t[v].pref01 = t[v].pref10 = t[v].suf01 = t[v].suf10 = t[v].cnt0 = t[v].best2 = 1; else t[v].pref1 = t[v].suf1 = t[v].pref01 = t[v].pref10 = t[v].suf01 = t[v].suf10 = t[v].cnt1 = t[v].best2 = 1; } for (int i = T - 1; 1 <= i; --i) t[i] = merge(t[2 * i], t[2 * i + 1]); while (m--) { int a, b; scanf( %d%d , &a, &b); invert(a, b); printf( %d n , query(a, b).best); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long y = 0; unsigned int x = 0; int i; string s; cin >> s; if (s.length() > 19) printf( BigInteger n ); else if (s.length() > 10) { y = 0; for (i = 0; i < s.length(); i++) { if (y > 0) y *= 10; y += s[i] - 0 ; } if (y > 9223372036854775807) printf( BigInteger n ); else printf( long n ); } else if (s.length() > 5) { y = 0; for (i = 0; i < s.length(); i++) { if (y > 0) y *= 10; y += s[i] - 0 ; } if (y > 2147483647) printf( long n ); else printf( int n ); } else if (s.length() > 3) { y = 0; for (i = 0; i < s.length(); i++) { if (y > 0) y *= 10; y += s[i] - 0 ; } if (y > 32767) printf( int n ); else printf( short n ); } else if (s.length() > 0) { y = 0; for (i = 0; i < s.length(); i++) { if (y > 0) y *= 10; y += s[i] - 0 ; } if (y > 127) printf( short n ); else printf( byte n ); } else printf( byte n ); }
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, sum = 0, p = 0, res = 0; map<long long, long long> m; cin >> n; m[0] = 1; for (long long i = 1; i <= n; i++) { cin >> a; sum += a; p = max(p, m[sum]); res += i - p; m[sum] = i + 1; } cout << res; }
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int lo[N]; int n, r[N], lb[N], rb[N], i, j, k; struct ST { pair<int, int> f[20][N]; function<pair<int, int>(pair<int, int>, pair<int, int>)> uni; void ini(int* a) { for (int i = 1; i <= n * 3; ++i) f[0][i] = {a[i], i}; for (int i = 1; (1 << i) <= n * 3; ++i) for (int j = 1; j + (1 << i) - 1 <= n * 3; ++j) f[i][j] = uni(f[i - 1][j], f[i - 1][j + (1 << i - 1)]); } inline pair<int, int> ask(int x, int y) { int l = lo[y - x + 1]; return uni(f[l][x], f[l][y - (1 << l) + 1]); } } ss[2]; int anc[2][20][N]; inline int calc(int x, int y) { int x1, y1, step = 0, x2, y2; x1 = ss[0].ask(x, y).second; y1 = ss[1].ask(x, y).second; for (int i = 19; i >= 0; --i) { x2 = anc[0][i][x1]; y2 = anc[1][i][y1]; if (rb[y2] - lb[x2] + 1 >= n || (lb[x2] < x && ss[1].ask(lb[x2], x - 1).first > y) || (y < rb[y2] && ss[0].ask(y + 1, rb[y2]).first < x)) continue; step += 1 << i; x1 = x2; y1 = y2; } x1 = lb[x1]; y1 = rb[y1]; ++step; return y1 - x1 + 1 >= n ? step : calc(x1, y1) + step; } int main() { for (i = 2; i < N; ++i) lo[i] = lo[i >> 1] + 1; ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (i = 1; i <= n; ++i) cin >> r[i], r[i + n] = r[i + n * 2] = r[i]; for (i = 1; i <= n * 3; ++i) lb[i] = max(1, i - r[i]), rb[i] = min(n * 3, i + r[i]); ss[0].uni = [&](pair<int, int> a, pair<int, int> b) { return min(a, b); }; ss[1].uni = [&](pair<int, int> a, pair<int, int> b) { return max(a, b); }; ss[0].ini(lb); ss[1].ini(rb); for (i = 1; i <= n * 3; ++i) anc[0][0][i] = ss[0].ask(lb[i], rb[i]).second, anc[1][0][i] = ss[1].ask(lb[i], rb[i]).second; for (k = 0; k < 2; ++k) for (i = 1; i < 20; ++i) for (j = 1; j <= n * 3; ++j) anc[k][i][j] = anc[k][i - 1][anc[k][i - 1][j]]; for (i = n + 1; i <= n * 2; ++i) cout << (n == 1 ? 0 : calc(i, i)) << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void Fast() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int gcd(int a, int b) { if (b == 0) return a; gcd(b, a % b); } int lcm(int a, int b) { return a * b / gcd(a, b); } int main() { Fast(); string s; cin >> s; int cnt = 0; while (s.size() >= 2) { int n = 0; for (int i = 0; i < s.length(); i++) n += s[i] - 0 ; s = to_string(n); cnt++; } cout << cnt; return 0; }
#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 inf = 0x7fffffff; const int maxn = 505; const long long mod = 1e9 + 7; long long dp[maxn][maxn]; int n; int main() { ios::sync_with_stdio(false); cin >> n; dp[1][1] = dp[1][0] = 1; for (int i = 2; i <= n; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k + j <= n; k++) { long long x = (dp[i - 1][j] * dp[i - 1][k]) % mod; dp[i][k + j] += x * (2 * (k + j) + 1); dp[i][k + j] %= mod; dp[i][k + j + 1] = (dp[i][k + j + 1] + x) % mod; dp[i][k + j - 1] += x * (k + j) * (k + j - 1); dp[i][k + j - 1] %= mod; } } } cout << dp[n][1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> primes; int prime_marker[1000000]; void PrecomputePrimes() { const int MAX_PRIME = 1000000; for (int i = 2; i * i < MAX_PRIME; ++i) { for (int j = i; i * j < MAX_PRIME; ++j) { prime_marker[i * j] = 1; } } for (int i = 2; i < MAX_PRIME; ++i) { if (prime_marker[i]) continue; primes.push_back(i); } } vector<int64_t> prime_factors; vector<int> factor_max_power; void ComputePrimeFactors(int64_t val) { for (int i = 0; i < primes.size(); ++i) { if (val % primes[i] == 0) { prime_factors.push_back(primes[i]); factor_max_power.push_back(0); while (val % primes[i] == 0) { val /= primes[i]; factor_max_power.back()++; } } } if (val > 1000000) { prime_factors.push_back(val); factor_max_power.push_back(1); } } vector<int64_t> arr; vector<int> cofactor_power; void ComputeCofactorPower(int64_t val) { cofactor_power.resize(prime_factors.size()); for (int i = 0; i < prime_factors.size(); ++i) { cofactor_power[i] = 0; if (val % prime_factors[i] == 0) { while (val % prime_factors[i] == 0) { val /= prime_factors[i]; cofactor_power[i]++; } } } } vector<int> basis; void InitializeBasis() { basis.resize(prime_factors.size()); basis[0] = 1; for (int i = 1; i < prime_factors.size(); ++i) { basis[i] = basis[i - 1] * (factor_max_power[i - 1] + 1); } } void MapToIndices(int hash, vector<int>* indices) { for (int i = basis.size() - 1; i >= 0; --i) { (*indices)[i] = hash / basis[i]; hash -= (*indices)[i] * basis[i]; } } int IndicesToHash(const vector<int>& indices) { int hash = 0; for (int i = 0; i < indices.size(); ++i) { hash += indices[i] * basis[i]; } return hash; } vector<vector<int>> hash_indices; void InitHashIndices(int max_hash) { hash_indices.resize(max_hash, vector<int>(basis.size())); for (int i = 0; i < max_hash; ++i) { MapToIndices(i, &hash_indices[i]); } } struct DpState { int length = 100000; int64_t cost = 0; int parent; bool is_chosen = false; DpState(int length, int64_t cost, int parent, bool is_chosen) : length(length), cost(cost), parent(parent), is_chosen(is_chosen) {} DpState() {} bool operator<(const DpState& other) const { if (length == other.length) return cost < other.cost; return length < other.length; } }; DpState dp[1003][7000]; void UpdateDP(int cur, int hash, const vector<int>& indices) { if (cur == 0) { if (hash == 0) { dp[cur][hash] = DpState{0, 0, -1, false}; return; } bool is_divisible = true; for (int i = 0; i < indices.size(); ++i) { if (indices[i] > cofactor_power[i]) { is_divisible = false; break; } } if (is_divisible) { dp[cur][hash] = DpState{1, arr[cur], -1, true}; } return; } vector<int> prev_indices(indices.size()); for (int i = 0; i < indices.size(); ++i) { prev_indices[i] = max(0, indices[i] - cofactor_power[i]); } int prev_hash = IndicesToHash(prev_indices); DpState state1 = {dp[cur - 1][prev_hash].length + 1, dp[cur - 1][prev_hash].cost + arr[cur], prev_hash, true}; DpState state2 = {dp[cur - 1][hash].length, dp[cur - 1][hash].cost, hash, false}; if (state1 < state2) { dp[cur][hash] = state1; } else { dp[cur][hash] = state2; } } int main() { int n; int64_t k; PrecomputePrimes(); scanf( %d , &n); cin >> k; int64_t val; for (int i = 0; i < n; ++i) { cin >> val; arr.push_back(val); } if (k == 1) { int lowest_index = 0; for (int i = 1; i < n; ++i) { if (arr[i] < arr[lowest_index]) lowest_index = i; } printf( 1 n%d n , lowest_index + 1); return 0; } ComputePrimeFactors(k); InitializeBasis(); int max_hash = basis.back() * (factor_max_power.back() + 1); InitHashIndices(max_hash); for (int i = 0; i < n; ++i) { ComputeCofactorPower(arr[i]); for (int hash = 0; hash < max_hash; ++hash) { UpdateDP(i, hash, hash_indices[hash]); } } int cur_hash = max_hash - 1; if (dp[n - 1][cur_hash].length >= 100000) { printf( -1 n ); return 0; } printf( %d n , dp[n - 1][cur_hash].length); for (int i = n - 1; i >= 0; --i) { if (dp[i][cur_hash].is_chosen) { printf( %d , i + 1); } cur_hash = dp[i][cur_hash].parent; } printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long int n, i, d; cin >> n; long int a[n], b[n], c[1000000]; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { cin >> b[i]; c[b[i]] = i; } d = 0; for (i = 1; i < n; i++) { if (c[a[i]] < c[a[i - 1]]) { d++; c[a[i]] = c[a[i - 1]]; } } cout << d; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> per, obs; long long n, k, p; bool check(long long t) { long long i = 0; for (long long j = 0; j <= obs.size() - 1; j++) { if (abs(per[i] - obs[j]) + abs(p - obs[j]) <= t) { i++; if (i == n) return 1; } } return 0; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; t = 1; for (long long test = 1; test <= t; test++) { cin >> n >> k >> p; per.resize(n); obs.resize(k); for (long long i = 0; i <= n - 1; i++) cin >> per[i]; for (long long i = 0; i <= k - 1; i++) cin >> obs[i]; sort(per.begin(), per.end()); sort(obs.begin(), obs.end()); long long ans = 1e10, st = 0, end = 1e10; while (st <= end) { long long mid = (st + end) >> 1; if (check(mid)) { ans = min(ans, mid); end = mid - 1; } else st = mid + 1; } cout << ans; } return 0; }
#include <cstdio> #include <cstring> #include <iostream> #include <vector> #include <cmath> #include <cstdlib> #include <ctime> #include <algorithm> #include <string> #include <map> #define ll long long #define ls id << 1 #define rs id << 1 | 1 #define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type)) #define memarray(array, value) memset(array, value, sizeof(array)) #define fillarray(array, value, begin, end) fill((array) + (begin), (array) + (end) + 1, value) #define fillvector(v, value) fill((v).begin(), (v).end(), value) #define pb(x) push_back(x) #define st(x) (1LL << (x)) #define pii pair<int, int> #define mp(a, b) make_pair((a), (b)) #define Flush fflush(stdout) #define vecfirst (*vec.begin()) #define veclast (*vec.rbegin()) #define vecall(v) (v).begin(), (v).end() #define vecupsort(v) (sort((v).begin(), (v).end())) #define vecdownsort(v, type) (sort(vecall(v), greater<type>())) #define veccmpsort(v, cmp) (sort(vecall(v), cmp)) using namespace std; const int N = 500050; const int inf = 0x3f3f3f3f; const ll llinf = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const int MOD = 1e9 + 7; const double PI = acos(-1.0); clock_t TIME__START, TIME__END; void program_end() { #ifdef ONLINE printf( n nTime used: %.6lf(s) n , ((double)TIME__END - TIME__START) / 1000); system( pause ); #endif } int n; int ask1(int x, int y) { printf( 1 %d %d n , x, y), Flush; int ret; scanf( %d , &ret); return ret; } int ask2(int x, const vector<int> &vec) { printf( 2 %d %d , x, (int)vec.size()); for (auto i : vec) printf( %d , i); putchar( n ), Flush; int ret; scanf( %d , &ret); return ret; } int ans[105][105]; vector<int> e; vector<int> mergesort(int l, int r) { vector<int> ret; if (l == r) { ret.push_back(l); return ret; } int mid = (l + r) >> 1; auto vl = mergesort(l, mid); auto vr = mergesort(mid + 1, r); int i = 0, j = 0; while (i < (int)vl.size() && j < (int)vr.size()) { int r = ask1(vl[i], vr[j]); if (r == 1) { ret.push_back(vl[i]); i++; } else { ret.push_back(vr[j]); j++; } } while (i < (int)vl.size()) ret.push_back(vl[i]), i++; while (j < (int)vr.size()) ret.push_back(vr[j]), j++; return ret; } void work1() { e.clear(); e = mergesort(0, n - 1); for (int i = 0; i < (int)e.size(); ++i) for (int j = i; j < (int)e.size(); ++j) ans[e[i]][e[j]] = 1; } inline void solve() { memarray(ans, 0); scanf( %d , &n); work1(); int u = n - 1, p = n - 2; #ifdef VINGYING printf( Hamiton: ); for (auto i : e) cout << i << ; putchar( n ); #endif while (u >= 0 && p >= 0) { vector<int> v; for (int i = 0; i <= p; ++i) v.push_back(e[i]); int r = ask2(e[u], v); if (r == 1) p--; if (p < 0) { ans[e[u]][e[0]]=1; break; } if (r == 0) { ans[e[u]][e[p+1]]=1; u--; if (p==u)p--; if (p < 0) { ans[e[u]][e[0]]=1; break; } } } for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) ans[i][j]=(ans[i][j]||(ans[i][k]&&ans[k][j])); printf( 3 n ); for (int i = 0; i < n; ++i, putchar( n )) for (int j = 0; j < n; ++j) printf( %d , ans[i][j]); Flush; int resp; scanf( %d ,&resp); if(resp==-1)exit(0); } int main() { TIME__START = clock(); int Test = 1; scanf( %d , &Test); while (Test--) { solve(); // if (Test) // putchar( n ); } TIME__END = clock(); program_end(); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:16777216 ) using namespace std; template <class T> inline void checkmin(T &a, T b) { if (a > b) a = b; } template <class T> inline void checkmax(T &a, T b) { if (a < b) a = b; } const int oo = 1 << 30; const double eps = 1e-7; const int N = 512; const int M = 1; const long long P = 10000000097ll; int sgn(double x) { return (x > eps) - (x < -eps); } int fcmp(double a, double b) { return sgn(a - b); } double sqr(double x) { return x * x; } struct Point { double x, y; Point() {} Point(double a, double b) : x(a), y(b) {} Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } Point operator*(double d) const { return Point(x * d, y * d); } double operator*(const Point &p) const { return x * p.y - y * p.x; } Point operator/(double d) const { return Point(x / d, y / d); } double norm() const { return sqrt(x * x + y * y); } Point rot(double alpha) const { return Point(x * cos(alpha) - y * sin(alpha), x * sin(alpha) + y * cos(alpha)); } void cin() { scanf( %lf %lf , &x, &y); } void cout(char *s = p = ) const { printf( %s %f %f , s, x, y); } }; struct Line { Point a, b; Line() {} Line(const Point &p, const Point &q) : a(p), b(q) {} bool para(const Line &lin) const { Point u, v; u = b - a; v = lin.b - lin.a; return sgn(u * v) == 0; } bool l_to_s(const Line &seg) const { return sgn((b - a) * (seg.a - a)) * sgn((b - a) * (seg.b - a)) < 0; } bool s_to_s(const Line &seg) const { return l_to_s(seg) && seg.l_to_s(*this); } Point l_on_l(const Line &lin) const { double s1, s2; s1 = (lin.a - a) * (lin.b - a); s2 = (lin.b - b) * (lin.a - b); return (a * s2 + b * s1) / (s1 + s2); } int l_to_p(const Point &p) const { return sgn((b - a) * (p - a)); } }; Point ar[N][5]; Point br[N][5]; Point tmple[2][N]; Point *x, *y; double h, f; int n; double ans; double area(Point *p, int pn) { int i, j, k; double ans = 0; for (i = 1; i < pn; i++) ans += (p[i] - p[0]) * (p[i + 1] - p[0]); if (pn < -11) { for (i = 0; i < (pn); i++) printf( %.3f %.3f--> , p[i].x, p[i].y); puts( ); } return ans; } void read() { int i, j, k; double a, b; double fix, low; ans = 0; scanf( %d , &n); scanf( %lf %lf , &h, &f); fix = f / (f - h); low = (f + h) / (f - h); for (i = 0; i < (n); i++) { scanf( %lf %lf , &a, &b); ar[i][0] = Point(a * fix, 0); ar[i][1] = Point(b * fix, 0); ar[i][2] = Point(b, h); ar[i][3] = Point(a, h); ar[i][4] = ar[i][0]; br[i][0] = Point(a * fix, 0); br[i][1] = Point(b * fix, 0); br[i][2] = Point(b * low, h); br[i][3] = Point(a * low, h); br[i][4] = br[i][0]; ans += area(ar[i], 4) + area(br[i], 4); } } void cut(Point *p) { int xn, yn; int i, j, k; int ii, jj; Line lin, seg; for (i = 0; i < (n); i++) { for (j = 0; j < (5); j++) x[j] = p[j]; xn = 4; for (j = 0; j < (4); j++) { lin = Line(ar[i][j], ar[i][j + 1]); yn = 0; for (ii = 1; ii <= xn; ii++) { seg = Line(x[ii - 1], x[ii]); if (lin.l_to_s(seg)) y[yn++] = lin.l_on_l(seg); if (lin.l_to_p(x[ii]) >= 0) y[yn++] = x[ii]; } y[yn] = y[0]; swap(x, y); swap(xn, yn); } ans -= area(x, xn); } } void sub() { int i, j, k; x = tmple[0]; y = tmple[1]; for (i = 0; i < (n); i++) cut(br[i]); } int main() { int T, _ = 0; read(); sub(); printf( %.6f n , ans); return 0; }
#include <bits/stdc++.h> int n, c; void p(int a) { if (a <= n) { c++; p(a * 10); p(a * 10 + 1); } } int main() { std::cin >> n; p(1); std::cout << c; }
#include <bits/stdc++.h> using namespace std; const int N = 200100; const int M = 2 * N; const long long MOD1 = 98764321261LL; const long long MOD = 998244353LL; inline int lowbit(int x) { return x & (-x); } struct fenwick { long long a[N]; int n; void init(int n) { for (int i = 0; i <= n; ++i) a[i] = 0; this->n = n; } void add(int x, long long v) { for (int i = x; i <= n; i += lowbit(i)) a[i] += v; } long long query(int x) { long long ret = 0; for (int i = x; i; i ^= lowbit(i)) ret += a[i]; return ret; } void rangeadd(int l, int r, long long v) { add(l, v); add(r + 1, -v); } long long rangequery(int l, int r) { l = max(l, 1); r = min(r, n); if (r < l) return 0; long long ret = query(r); ret -= query(l - 1); return ret; } }; inline long long powmod(long long x, long long y, long long MOD) { long long ret = 1; while (y) { if (y & 1LL) ret = ret * x % MOD; x = x * x % MOD; y = y >> 1; } return ret; } struct graph { int head[N], next[M], to[M]; int tot; inline void init(int n) { for (int i = 0; i <= n; ++i) head[i] = -1; tot = 0; } inline void addedge(int x, int y) { to[tot] = y; next[tot] = head[x]; head[x] = tot; ++tot; } }; struct point { long long x, y; point() {} point(long long x, long long y) : x(x), y(y) {} void read() { scanf( %lld%lld , &x, &y); } point operator+(const point &rhs) const { return point(x + rhs.x, y + rhs.y); } point operator-(const point &rhs) const { return point(x - rhs.x, y - rhs.y); } long long operator^(const point &rhs) const { return x * rhs.y - y * rhs.x; } bool operator<(const point &rhs) const { if (y != rhs.y) return y < rhs.y; return x < rhs.x; } }; long long phi(long long x) { long long res = x; for (long long i = 2; i * i <= x; ++i) { if (x % i == 0) { res = res / i * (i - 1); while (x % i == 0) x /= i; } } if (x > 1) res = res / x * (x - 1); return res; } int n, m; int ans; int a[N], b[N], deg[N]; graph G; void dfs(int x, int p) { a[x] = 1; b[x] = 0; int chcnt = 0; int maxa = -1; int maxb = -1; for (int i = G.head[x]; i != -1; i = G.next[i]) { int y = G.to[i]; if (y == p) continue; dfs(y, x); int temp; if (maxa != -1) { temp = a[maxa] + a[y] + deg[x] - 2; ans = max(ans, temp); temp = a[maxa] + b[y] + deg[x] - 2; ans = max(ans, temp); } if (maxb != -1) { temp = b[maxb] + b[y] + deg[x] - 2; ans = max(ans, temp); temp = b[maxb] + b[y] + 1; ans = max(ans, temp); temp = b[maxb] + a[y] + deg[x] - 2; ans = max(ans, temp); } a[x] = max(a[x], b[y] + 1); b[x] = max(b[x], a[y] + deg[x] - 1 - (p != 0)); b[x] = max(b[x], b[y] + deg[x] - 1 - (p != 0)); if (maxa == -1 || a[y] > a[maxa]) maxa = y; if (maxb == -1 || b[y] > b[maxb]) maxb = y; } ans = max(ans, a[x]); ans = max(ans, b[x] + (p != 0)); } int main() { int cases = 1; for (int iii = 1; iii <= cases; ++iii) { scanf( %d , &n); G.init(n); for (int i = 1; i < n; ++i) { int x, y; scanf( %d%d , &x, &y); G.addedge(x, y); G.addedge(y, x); ++deg[x]; ++deg[y]; } ans = 0; dfs(1, 0); printf( %d n , ans); } }
#include <bits/stdc++.h> using namespace std; void prt(long x) { cerr << x; } void prt(long long x) { cerr << x; } void prt(unsigned x) { cerr << x; } void prt(unsigned long x) { cerr << x; } void prt(unsigned long long x) { cerr << x; } void prt(float x) { cerr << x; } void prt(double x) { cerr << x; } void prt(long double x) { cerr << x; } void prt(char x) { cerr << << x << ; } void prt(const char *x) { cerr << << x << ; } void prt(const string &x) { cerr << << x << ; } void prt(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void prt(const pair<T, V> &x) { cerr << { ; prt(x.first); cerr << , ; prt(x.second); cerr << } ; } template <typename T> void prt(const T &x) { long long f = 0; cerr << { ; for (auto &i : x) cerr << (f++ ? , : ), prt(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { prt(t); if (sizeof...(v)) cerr << , ; _print(v...); } void err(istream_iterator<string> it) {} template <typename T, typename... brgs> void err(istream_iterator<string> it, T a, brgs... args) { cerr << *it << = << a << endl; err(++it, args...); } long long kadane(long long *a, long long n) { long long max_so_far = INT_MIN, max_ending_here = 0; for (__typeof(n) i = (0); i != (n); i += 1 - 2 * ((0) > (n))) { max_ending_here += a[i]; max_so_far = max(max_so_far, max_ending_here); if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } void solve() { long long n; cin >> n; double ans = 1.0 / tan(3.1415926535897932384626 / (2 * n)); cout << fixed; cout << setprecision(10) << ans << endl; return; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > ed[200005]; int n, m, k; unsigned long long Hash[200005]; unsigned long long H = 1e9 + 7; unsigned long long a[10][10]; unsigned long long sum; long long ans = 0; void dfs(int kk1, unsigned long long s) { if (kk1 == k) { if (s == sum) { ans += 1; } return; } for (int i = 0; i < kk1 + 1; i++) { dfs(kk1 + 1, s + a[kk1 + 1][i]); } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; ed[u].push_back(make_pair(w, v)); } for (int i = 1; i <= n; i++) { sort(ed[i].begin(), ed[i].end()); } Hash[1] = 1; sum = 1; for (int i = 2; i <= n; i++) { Hash[i] = Hash[i - 1] * H; sum += Hash[i]; } for (int i = 1; i <= n; i++) { int tp = ed[i].size(); for (int j = 0; j < tp; j++) { int tpp = ed[i][j].second; a[tp][j] += Hash[tpp]; } } dfs(1, a[1][0]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int compare(const void *a, const void *b) { int A = *((int *)a); int B = *((int *)b); return B - A; } int main() { long long a[200000], b, c = 0, d, f = 0, i, j, k, n, m, p = 0, q = 0, r, t = 0, l, mid = 0, sum = 0, x, y, s = 0; string ch[3000][2]; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; i = 0; j = n - 1; while (i <= j) { c++; if (c % 2 != 0) { if (a[i] > a[j]) { s = s + a[i]; i++; } else { s = s + a[j]; j--; } } else { if (a[i] > a[j]) { f = f + a[i]; i++; } else { f = f + a[j]; j--; } } } cout << s << << f; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n + 2]; vector<int> s; s.push_back(n); for (int i = 2; i < n + 1; i++) cin >> arr[i]; arr[1] = 0; int k = n; while (k > 0) { s.push_back(arr[k]); k = arr[k]; } for (int i = s.size() - 2; i > -1; i--) { cout << s[i] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long c[105], x[105][105], dp[105][105][105]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m, k, i, j, l, p, ans = 10000000000000000; cin >> n >> m >> k; for (i = 1; i <= n; i++) cin >> c[i]; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) cin >> x[i][j]; for (i = 0; i <= 100; i++) for (j = 0; j <= 100; j++) for (l = 0; l <= 100; l++) dp[i][j][l] = 10000000000000000; if (c[1] == 0) { for (i = 1; i <= m; i++) dp[1][1][i] = x[1][i]; } else dp[1][1][c[1]] = 0; for (i = 2; i <= n; i++) { if (c[i] == 0) { for (p = 1; p <= k; p++) { for (j = 1; j <= m; j++) { for (l = 1; l <= m; l++) { if (j == l) { dp[i][p][l] = min(dp[i][p][l], dp[i - 1][p][j] + x[i][l]); } else { dp[i][p + 1][l] = min(dp[i][p + 1][l], dp[i - 1][p][j] + x[i][l]); } } } } } else { for (p = 1; p <= k; p++) { for (j = 1; j <= m; j++) { if (j == c[i]) { dp[i][p][c[i]] = min(dp[i][p][c[i]], dp[i - 1][p][j]); } else { dp[i][p + 1][c[i]] = min(dp[i][p + 1][c[i]], dp[i - 1][p][j]); } } } } } if (c[n] == 0) { for (i = 1; i <= m; i++) ans = min(ans, dp[n][k][i]); } else ans = dp[n][k][c[n]]; if (ans != 10000000000000000) cout << ans; else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e6; vector<int> g[MAXN + 1]; pair<int, int> arr[MAXN + 1]; int k; void dfs(int nod, int par, int lvl) { arr[nod] = {2 * MAXN, 0}; for (auto it : g[nod]) { if (it != par) { dfs(it, nod, lvl + 1); if (lvl >= arr[it].first) { arr[nod].first = min(arr[nod].first, arr[it].first); arr[nod].second += arr[it].second; } } } if (g[nod].size() == 0) { arr[nod] = {lvl - k, 1}; } } int dp[MAXN + 1]; void dfs1(int nod, int par, int lvl) { dp[nod] = (g[nod].size() == 0); for (auto it : g[nod]) { if (it != par) { dfs1(it, nod, lvl + 1); int cur = arr[nod].second; if (lvl >= arr[it].first) { cur -= arr[it].second; } dp[nod] = max(dp[nod], cur + dp[it]); } } } int main() { int i, n, x, y; ios::sync_with_stdio(false); cin >> n >> k; for (i = 2; i <= n; i++) { cin >> x; g[x].push_back(i); } dfs(1, 0, 1); dfs1(1, 0, 1); cout << dp[1]; return 0; }
#include <bits/stdc++.h> char s[101]; char t[10] = ><+-.,[] ; int c[10] = {8, 9, 10, 11, 12, 13, 14, 15}; void func() { int n = strlen(s), i, j, ans = 0; const int mod = 1000003; for (i = 0; i < n; i++) { for (j = 0; j < 8; j++) { if (t[j] == s[i]) { ans = (ans * 16 + c[j]) % mod; break; } } } printf( %d n , ans); } int main(int argc, char **argv) { while (scanf( %s , s) == 1) { func(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 1; i <= t; i++) { int x1, x2, y1, y2; string arr[8]; for (int i = 0; i < 8; i++) cin >> arr[i]; bool check = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (arr[i][j] == K && check == 0) { check = 1; x1 = j; y1 = i; } else if (arr[i][j] == K ) { x2 = j; y2 = i; } } } if ((x1 == x2 && abs(y1 - y2) == 4) || (y1 == y2 && abs(x1 - x2) == 4)) cout << YES ; else if (abs(x1 - x2) == 4 && abs(y1 - y2) == 4) cout << YES ; else cout << NO ; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; long long MAX = 9223372036854775807; int visited[1001][1001]; int main() { ios_base::sync_with_stdio(0); ; int n, m; cin >> n >> m; int a[n][m], l[n][m], r[n][m], u[n][m], d[n][m]; for (int i = 0; i < n; ++i) { string s; cin >> s; for (int j = 0; j < s.size(); ++j) { if (s[j] == . ) a[i][j] = 0; else a[i][j] = 1; } } for (int i = 0; i < n; ++i) { int temp = 0; for (int j = 0; j < m; ++j) { l[i][j] = temp; temp += a[i][j]; if (a[i][j] == 0) temp = 0; } temp = 0; for (int j = m - 1; j >= 0; j--) { r[i][j] = temp; temp += a[i][j]; if (a[i][j] == 0) temp = 0; } } for (int j = 0; j < m; ++j) { int temp = 0; for (int i = 0; i < n; ++i) { u[i][j] = temp; temp += a[i][j]; if (a[i][j] == 0) temp = 0; } temp = 0; for (int i = n - 1; i >= 0; i--) { d[i][j] = temp; temp += a[i][j]; if (a[i][j] == 0) temp = 0; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j]) { l[i][j] = min(min(l[i][j], r[i][j]), min(u[i][j], d[i][j])); } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { u[i][j] = 0; r[i][j] = 0; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] and l[i][j] > 0) { int k = l[i][j]; u[i - k][j]++; if (i + k + 1 < n) u[i + k + 1][j]--; r[i][j - k]++; if (j + k + 1 < m) r[i][j + k + 1]--; } } } for (int i = 0; i < n; ++i) { int temp = 0; for (int j = 0; j < m; ++j) { temp += r[i][j]; visited[i][j] = temp; } } for (int j = 0; j < m; ++j) { int temp = 0; for (int i = 0; i < n; ++i) { temp += u[i][j]; visited[i][j] += temp; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] and visited[i][j] == 0) { cout << -1 << n ; return 0; ; } if (a[i][j] == 0 and visited[i][j] > 0) { cout << -1 << n ; return 0; ; } } } vector<pair<pair<int, int>, int> > ans; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] and l[i][j] > 0) { ans.push_back({{i + 1, j + 1}, l[i][j]}); } } } cout << ans.size() << n ; ; for (auto i : ans) { cout << i.first.first << ; ; cout << i.first.second << ; ; cout << i.second << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { int n, m, k; cin >> n >> m >> k; vector<int> p(n); for (int i = 0; i < n; ++i) cin >> p[i]; vector<int> s(n); for (int i = 0; i < n; ++i) cin >> s[i]; vector<int> c(k); for (int i = 0; i < k; ++i) cin >> c[i]; vector<vector<int>> schools(m + 1, vector<int>()); for (int i = 0; i < n; ++i) { schools[s[i]].push_back(p[i]); } int ans = 0; for (int i = 0; i < k; ++i) { int cur = c[i]; int school = s[cur - 1]; int power = p[cur - 1]; vector<int> ss = schools[school]; sort(ss.rbegin(), ss.rend()); if (ss[0] != power) ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; const double oo = 1E7, eps = 1E-7; struct point { double x, y; } P[maxn]; int n; double ansl, ansr; point operator+(point a, point b) { return (point){a.x + b.x, a.y + b.y}; } point operator-(point a, point b) { return (point){a.x - b.x, a.y - b.y}; } double operator*(point a, point b) { return a.x * b.x + a.y * b.y; } double operator/(point a, point b) { return a.x * b.y - b.x * a.y; } double get(point A, point B) { double a, b, c, res; a = A.y - B.y; b = B.x - A.x; c = A / B; if (a) res = (-c - b * P[1].y) / a; else res = oo; return res; } int main() { int i, j; double x, sita; scanf( %d , &n); scanf( %lf%lf , &P[n].x, &P[n].y); for (i = 1; i < n; ++i) scanf( %lf%lf , &P[i].x, &P[i].y); ansl = min(P[1].x, P[n].x), ansr = max(P[1].x, P[n].x); for (i = 2; i < n; ++i) { j = i + 1; x = get(P[i], P[j]); sita = (P[1].y - P[i].y) * (P[j].x - P[i].x); if (sita > (P[j].y - P[i].y) * (ansl - P[i].x)) ansl = max(ansl, x); if (sita > (P[j].y - P[i].y) * (ansr - P[i].x)) ansr = min(ansr, x); } if (ansr >= ansl) printf( %.0lf , floor(ansr) - floor(ansl - eps)); else printf( 0 ); return 0; }
#include <bits/stdc++.h> using namespace std; long long int a[100001] = {0}; long long int d[100001] = {0}; long long int vis[100001] = {0}; long long int cons[100001] = {0}; vector<long long int> vec[100001]; void dfs(long long int x, long long int p) { vis[x] = 1; if (a[x] == 1) { if (a[p] == 1) { cons[x] = cons[p] + 1; } else { cons[x] = 1; } d[x] = max(d[p], cons[x]); } else { cons[x] = 0; d[x] = d[p]; } for (long long int i = 0; i < vec[x].size(); i++) { if (vis[vec[x][i]] == 1) continue; else { dfs(vec[x][i], x); } } } signed main() { long long int n, m; cin >> n >> m; for (long long int i = 0; i < n; i++) cin >> a[i + 1]; for (long long int i = 0; i < n - 1; i++) { long long int x, y; cin >> x >> y; vec[x].push_back(y); vec[y].push_back(x); } dfs(1, 0); long long int count = 0; for (long long int i = 2; i <= n; i++) { if (vec[i].size() == 1 && d[i] <= m) count++; } cout << count; }
#include <bits/stdc++.h> using namespace std; inline int read() { int res = 0; int neg; while (true) { char ch = getchar(); if (ch >= 0 && ch <= 9 || ch == - ) { if (ch == - ) neg = -1; else neg = 1, res = ch - 0 ; break; } } while (true) { char ch = getchar(); if (ch >= 0 && ch <= 9 ) res *= 10, res += ch - 0 ; else break; } return res * neg; } const int maxn = 100020; const int MOd = 1e7; int a, L, R, st[maxn], at; bool flag, used[maxn]; vector<pair<int, pair<int, int> > > w[maxn]; vector<pair<int, int> > w2[maxn]; int dfs(int n, int back, int sz) { int ret = 1; st[++at] = n; for (int i = 0; i < w[n].size(); i++) if (w[n][i].first != back && !used[w[n][i].first]) { int t = dfs(w[n][i].first, n, sz); w[n][i].second.second = t; ret += t; } for (int i = 0; i < w[n].size(); i++) if (w[n][i].first == back) w[n][i].second.second = sz - ret; return ret; } vector<int> Cw[maxn]; int ans1, ans2, sz[maxn]; void center(int n, int back, int szz) { at = 0; dfs(n, 0, szz); int C = n; for (int i = 1; i <= at; i++) { int t = st[i]; int c = 0; for (int j = 0; j < w[t].size(); j++) if (w[t][j].second.second > szz / 2) { c = 1; break; } if (!c) C = t; } Cw[back].push_back(C); sz[C] = szz; used[C] = 1; for (int i = 0; i < w[C].size(); i++) if (!used[w[C][i].first]) center(w[C][i].first, C, w[C][i].second.second); } int N, T; pair<int, int> segment[maxn * 3]; pair<int, pair<int, int> > st2[maxn]; void dfss(int &n, int &back, int der, int s) { st2[++at].first = n; st2[at].second.first = der; st2[at].second.second = s + s - der; pair<int, int> t; t.first = -1e9; t.second = 0; int l = L - der, r = R - der; if (l < N && 0 <= l) { if (r > N - 1) r = N - 1; for (l += N, r += N; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1) { if (l & 1) t = max(t, (segment[l])); if (~r & 1) t = max(t, (segment[r])); } } if (t.first + s + s - der >= 0) { ans1 = n; ans2 = t.second; flag = 1; return; } for (int i = 0; i < w2[n].size() && !flag; i++) if (w2[n][i].first != back && !used[w2[n][i].first]) dfss(w2[n][i].first, n, der + 1, s + w2[n][i].second); } void calc(int &C) { N = 1; while (N < sz[C]) N <<= 1; for (int i = 1; i < N + N; i++) segment[i] = pair<int, int>(-1e9, 0); for (int k = N; k; k >>= 1) segment[k] = pair<int, int>(0, C); for (int i = 0; i < w2[C].size(); i++) if (!used[w2[C][i].first]) { at = 0; dfss(w2[C][i].first, C, 1, w2[C][i].second); if (flag) return; for (int i = 1; i <= at; i++) { pair<int, pair<int, int> > &t = st2[i]; int k = t.second.first + N; if (segment[k].first < t.second.second) { segment[k] = pair<int, int>(t.second.second, t.first); for (k >>= 1; k; k >>= 1) segment[k] = max(segment[k + k], segment[k + k + 1]); } } } used[C] = 1; for (int i = 0; i < Cw[C].size(); i++) { calc(Cw[C][i]); if (flag) return; } } bool f(int n) { for (int i = 1; i <= a; i++) for (int j = 0; j < w[i].size(); j++) w2[i][j].second = (w[i][j].second.first >= n); flag = 0; memset(used, 0, sizeof(used)); calc(Cw[0][0]); return flag; } int main() { scanf( %d %d %d , &a, &L, &R); for (int i = 1, j, k, t; i < a; i++) { scanf( %d %d %d , &j, &k, &t); w[j].push_back(pair<int, pair<int, int> >(k, pair<int, int>(t, 0))); w[k].push_back(pair<int, pair<int, int> >(j, pair<int, int>(t, 0))); w2[j].push_back(pair<int, int>(k, 0)); w2[k].push_back(pair<int, int>(j, 0)); } center(1, 0, a); int t = -1; for (int k = 29; k >= 0; k--) if (f(t + (1 << k))) t += 1 << k; printf( %d %d n , ans1, ans2); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /stack:200000000 ) #pragma GCC optimize( Ofast ) #pragma target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) using namespace std; long long read() { long long x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == - ) f = -1; for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * f; } void write(long long x) { if (x < 0) x = -x, putchar( - ); if (x >= 10) write(x / 10); putchar(x % 10 + 0 ); } void writeln(long long x) { write(x); puts( ); } const int N = 500000; int E[N], X[N], R[N], D[N]; int u[N], v[N]; bool b[N]; int i, m, n, t, w, x, y, S_E; inline void A_E(int x, int y) { S_E++, E[S_E] = y, X[S_E] = R[x], R[x] = S_E, D[x]++; return; } inline int O_E(int x) { return ((x - 1) ^ 1) + 1; } inline void dfs(int x) { for (int i = R[x]; R[x] = X[i], i != 0; i = R[x]) if (!b[i]) { b[i] = true, b[O_E(i)] = true; dfs(E[i]); w++, u[w] = x, v[w] = E[i]; } return; } int main() { n = read(), m = read(); for (i = 1; i <= m; i++) { x = read(), y = read(); A_E(x, y), A_E(y, x); } for (i = 1; i <= n; i++) if (D[i] & 1) if (t) m++, A_E(i, t), A_E(t, i), t = 0; else t = i; if (m & 1) m++, A_E(1, 1), A_E(1, 1); writeln(m); dfs(1); for (i = w; i > 0; i--) if (!(i & 1)) printf( %d %d n , u[i], v[i]); else printf( %d %d n , v[i], u[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100005]; cin >> n; int fa = 1; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] != i) { fa = 0; } } if (fa == 1) { cout << 0 ; } else { int ji = n - 1; while (a[ji] < a[ji + 1]) { ji--; } cout << ji; } }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9, MOD = 1e9 + 7, N = 1e5 + 1, M = 20; void setIO(string name = ) { ios::sync_with_stdio(false); cin.tie(0); if (name.size()) { freopen((name + .in ).c_str(), r , stdin); freopen((name + .out ).c_str(), w , stdout); } } int n, m, a[N], s, pxor[1000001], ans[N]; void pre() { pxor[0] = 0; for (int i = 1; i < 1000001; i++) pxor[i] = pxor[i - 1] ^ i; } string bit(int j) { string ans; for (int i = 0; i < M; i++) { ans += (j & 1) + 0 ; j >>= 1; } reverse(ans.begin(), ans.end()); return ans; } struct node1 { int mx = -INF; node1 *nxt[2]{}; }; struct node2 { int mn = INF; node2 *nxt[2]{}; }; node1 *rt1 = new node1(); node2 *rt2 = new node2(); void insert1(node1 *o, int i) { string s = bit(pxor[i]); for (char c : s) { if (!o->nxt[c - 0 ]) o->nxt[c - 0 ] = new node1(); o = o->nxt[c - 0 ]; o->mx = max(o->mx, i); } } void insert2(node2 *o, int i) { string s = bit(pxor[i]); for (char c : s) { if (!o->nxt[c - 0 ]) o->nxt[c - 0 ] = new node2(); o = o->nxt[c - 0 ]; o->mn = min(o->mn, i); } } void insert(int i) { insert1(rt1, i); insert2(rt2, i - 1); } int get1(node1 *o, int i) { string s = bit(pxor[i]); int ans = 0, shift = M - 1; for (char c : s) { int tmp = 1 ^ (c - 0 ); if (o->nxt[tmp] && o->nxt[tmp]->mx > i) { o = o->nxt[tmp]; ans += (1 << shift); } else if (o->nxt[1 ^ tmp] && o->nxt[1 ^ tmp]->mx > i) { o = o->nxt[tmp ^ 1]; } else return 0; shift--; } return ans; } int get2(node2 *o, int i) { string s = bit(pxor[i]); int ans = 0, shift = M - 1; for (char c : s) { int tmp = 1 ^ (c - 0 ); if (o->nxt[tmp] && o->nxt[tmp]->mn < i) { o = o->nxt[tmp]; ans += (1 << shift); } else if (o->nxt[1 ^ tmp] && o->nxt[1 ^ tmp]->mn < i) { o = o->nxt[tmp ^ 1]; } else return 0; shift--; } return ans; } int get(int i) { return max(get1(rt1, i - 1), get2(rt2, i)); } void clearall(node1 *o) { o->mx = -INF; if (o->nxt[0] && o->nxt[0]->mx != -INF) clearall(o->nxt[0]); if (o->nxt[1] && o->nxt[1]->mx != -INF) clearall(o->nxt[1]); } void clearall(node2 *o) { o->mn = INF; if (o->nxt[0] && o->nxt[0]->mn != INF) clearall(o->nxt[0]); if (o->nxt[1] && o->nxt[1]->mn != INF) clearall(o->nxt[1]); } void init() { clearall(rt1); clearall(rt2); } struct query { int l, r, id, bl; query(int ll, int rr, int i) : l(ll), r(rr), id(i), bl(l / s) {} bool operator<(const query &other) const { return make_pair(bl, r) < make_pair(other.bl, other.r); } }; query make_query(int ll, int rr, int i) { return query(ll, rr, i); } vector<query> qry; signed main() { setIO(); pre(); cin >> n >> m; s = ceil(sqrt(n)); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) { int l, r; cin >> l >> r; qry.push_back(make_query(l - 1, r - 1, i)); } sort(qry.begin(), qry.end()); for (int i = 0, j = 0; i < m; i = j) { while (j < m && qry[j].bl == qry[i].bl) j++; int lmx = 0, mid = j, right, nowmx = 0; for (int k = i; k < j; k++) right = lmx = max(lmx, qry[k].l); for (int k = i; k < j; k++) if (qry[k].r > lmx) { mid = k; break; } init(); for (int k = mid; k < j; k++) { ans[qry[k].id] = nowmx; while (right <= qry[k].r) { insert(a[right]); nowmx = ans[qry[k].id] = max(ans[qry[k].id], get(a[right])); right++; } for (int l = lmx; l >= qry[k].l; l--) { ans[qry[k].id] = max(ans[qry[k].id], get(a[l])); } } for (int k = i; k < j; k++) { init(); for (int l = min(qry[k].r, lmx); l >= qry[k].l; l--) { insert(a[l]); ans[qry[k].id] = max(ans[qry[k].id], get(a[l])); } } } for (int i = 0; i < m; i++) cout << ans[i] << n ; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:65777216 ) using namespace std; char s[1111][1111]; int n, m; int best; int now; int v[1111][1111]; vector<pair<int, int> > c; bool u[1111][1111]; void dfs(int x, int y) { if (u[x][y]) return; u[x][y] = true; for (int dx(-1); dx < (2); dx++) for (int dy(-1); dy < (2); dy++) { int nx = dx + x; int ny = dy + y; if (nx >= 0 && nx < n && ny >= 0 && ny < m) if (s[nx][ny] == 0 ) dfs(nx, ny); else c.push_back(pair<int, int>(nx, ny)); else c.push_back(pair<int, int>(-1, -1)); } } int startx, starty; int dfs2(int x, int y, int prevx = -1, int prevy = -1) { if (x == startx && y == starty && prevx != -1) return 0; int res = 1; for (int dx(-1); dx < (2); dx++) for (int dy(-1); dy < (2); dy++) if (abs(dx) + abs(dy) == 1) { int nx = x + dx; int ny = y + dy; if (nx >= 0 && nx < n && ny >= 0 && ny < m && v[nx][ny] == v[x][y]) if (nx != prevx || ny != prevy) { res += dfs2(nx, ny, x, y); if (prevx == -1) return res; } } return res; } int main() { cin >> n >> m; for (int i(0); i < (n); i++) scanf( %s , s[i]); for (int i(1); i < (n); i++) for (int j(1); j < (m); j++) if (s[i][j] == 1 && s[i][j - 1] == 1 && s[i - 1][j - 1] == 1 && s[i - 1][j] == 1 ) best = 4; for (int i(0); i < (n); i++) for (int j(0); j < (m); j++) if (s[i][j] == 0 && !u[i][j]) { c.clear(); dfs(i, j); now++; bool good = 1; int num = 0; for (int k(0); k < (c.size()); k++) if (c[k].first == -1) { good = 0; break; } else if (v[c[k].first][c[k].second] != now) v[c[k].first][c[k].second] = now, num++; for (int k(0); k < (c.size()); k++) { int num = 0; for (int dx(-1); dx < (2); dx++) for (int dy(-1); dy < (2); dy++) if (abs(dx) + abs(dy) == 1) { int nx = c[k].first + dx; int ny = c[k].second + dy; if (nx >= 0 && nx < n && ny >= 0 && ny < m && s[nx][ny] == 1 ) if (v[nx][ny] == now) num++; } if (num != 2) { good = 0; break; } } if (good && num > 4) { startx = c[0].first; starty = c[0].second; if (dfs2(c[0].first, c[0].second) == num) best = max(best, num); } } cout << best << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string l, r; cin >> l >> r; vector<stack<int>> l_array; vector<stack<int>> r_array; for (int i = 0; i < 27; i++) { stack<int> buf; l_array.push_back(buf); r_array.push_back(buf); } for (int i = 0; i < n; i++) { if (l[i] == ? ) { l_array[26].push(i); } else { l_array[l[i] - a ].push(i); } if (r[i] == ? ) { r_array[26].push(i); } else { r_array[r[i] - a ].push(i); } } vector<pair<int, int>> answer; for (int i = 0; i < 26; i++) { while (!l_array[i].empty() && !r_array[i].empty()) { answer.push_back(make_pair(l_array[i].top(), r_array[i].top())); l_array[i].pop(); r_array[i].pop(); } while (!l_array[i].empty() && !r_array[26].empty()) { answer.push_back(make_pair(l_array[i].top(), r_array[26].top())); l_array[i].pop(); r_array[26].pop(); } while (!r_array[i].empty() && !l_array[26].empty()) { answer.push_back(make_pair(l_array[26].top(), r_array[i].top())); l_array[26].pop(); r_array[i].pop(); } } while (!l_array[26].empty() && !r_array[26].empty()) { answer.push_back(make_pair(l_array[26].top(), r_array[26].top())); l_array[26].pop(); r_array[26].pop(); } int count = answer.size(); cout << count << endl; for (int i = 0; i < count; i++) { cout << answer[i].first + 1 << << answer[i].second + 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, z, x, c, sum, a[101][101], us[101][101]; char cc, stl; set<long long> s; void dfs(long long i, long long j) { if (a[i][j] == 0 || us[i][j] == 1 || i > n || j > m || i < 1 || j < 1) return; us[i][j] = 1; if (s.count(a[i][j]) == 0) sum++, s.insert(a[i][j]); if (a[i][j] == stl - A + 1) { dfs(i + 1, j); dfs(i - 1, j); dfs(i, j + 1); dfs(i, j - 1); } } int main() { cin >> n >> m >> stl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> cc; if (cc != . ) a[i][j] = cc - A + 1; if (cc == stl) z = i, x = j; } } s.insert((int)stl - A + 1); dfs(z, x); cout << sum << n ; }
#include <bits/stdc++.h> using namespace std; const int okX[] = {0, 0, 0, 1, 1, 2, 2, 2}; const int okY[] = {0, 1, 2, 0, 2, 0, 1, 2}; const int n = 8; void normalize(int a[n]) { vector<int> v(a, a + n); sort(v.begin(), v.end()); map<int, int> m; for (int i = 0; i < v.size(); ++i) if (!m.count(v[i])) m.insert(make_pair(v[i], m.size())); for (int i = 0; i < 8; ++i) a[i] = m[a[i]]; } int main() { int x[n], y[n]; for (int i = 0; i < n; ++i) cin >> x[i] >> y[i]; normalize(x); normalize(y); bool used[n]; memset(used, false, sizeof(used)); for (int i = 0; i < 8; ++i) { bool ok = false; for (int j = 0; j < 8; ++j) if (x[i] == okX[j] && y[i] == okY[j] && !used[j]) ok = true, used[j] = true; if (!ok) { printf( ugly n ); return 0; } } printf( respectable n ); return 0; }
#include <bits/stdc++.h> using namespace std; bool is_prime[100000]; void gen_is_primes(int n); void gen_prime_groups(int n, vector<bool>& to_match, list<int>& candidate_primes, list<pair<int, int> >& groups); void gen_prime_groups(int n, int p, vector<bool>& to_match, list<pair<int, int> >& groups); void gen_unmatched_multiples(int n, int i, vector<bool>& to_match, list<int>& unmatched_multiples); void gen_even_groups(vector<bool>& to_match, list<pair<int, int> >& groups); void gen_is_primes(int n) { fill_n(is_prime, n, true); is_prime[0] = false; is_prime[1] = false; for (int i = 2; i <= n / 2; i++) { if (is_prime[i]) { for (int j = 2 * i; j <= n; j += i) { is_prime[j] = false; } } } } void gen_prime_groups(int n, vector<bool>& to_match, list<int>& candidate_primes, list<pair<int, int> >& groups) { for (list<int>::iterator i = candidate_primes.begin(); i != candidate_primes.end(); i++) { gen_prime_groups(n, *i, to_match, groups); } } void gen_prime_groups(int n, int p, vector<bool>& to_match, list<pair<int, int> >& groups) { list<int>* unmatched_multiples = new list<int>(); gen_unmatched_multiples(n, p, to_match, *unmatched_multiples); for (list<int>::iterator it = unmatched_multiples->begin(); it != unmatched_multiples->end(); it++) { int curr = *it; int curr_partner = *(++it); to_match[curr] = false; to_match[curr_partner] = false; groups.push_back(make_pair(curr, curr_partner)); } delete unmatched_multiples; } void gen_unmatched_multiples(int n, int i, vector<bool>& to_match, list<int>& unmatched_multiples) { for (int curr_multiple = i; curr_multiple <= n; curr_multiple += i) { if (to_match[curr_multiple]) { unmatched_multiples.push_back(curr_multiple); } } if (unmatched_multiples.size() % 2 != 0) { unmatched_multiples.remove(2 * i); } } void gen_even_groups(vector<bool>& to_match, list<pair<int, int> >& groups) { list<int> unmatched; for (vector<bool>::iterator i = to_match.begin(); i != to_match.end(); i++) { if (*i) { int i_index = i - to_match.begin(); unmatched.push_back(i_index); } } if (unmatched.size() % 2 != 0) { unmatched.erase(unmatched.begin()); } for (list<int>::iterator i = unmatched.begin(); i != unmatched.end(); i++) { int curr = *i; int curr_partner = *(++i); groups.push_back(make_pair(curr, curr_partner)); } } int main() { int n; cin >> n; gen_is_primes(n); vector<bool> to_match(n, false); for (int i = 2; i <= n; i++) { to_match[i] = !is_prime[i] || (i <= n / 2); } list<int>* candidate_primes = new list<int>; for (int i = n / 2; i >= 3; i--) { if (is_prime[i]) { candidate_primes->push_back(i); } } list<pair<int, int> >* groups = new list<pair<int, int> >; gen_prime_groups(n, to_match, *candidate_primes, *groups); gen_even_groups(to_match, *groups); cout << groups->size() << endl; for (list<pair<int, int> >::iterator it = groups->begin(); it != groups->end(); it++) { cout << it->first << << it->second << endl; } delete groups; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma ) #pragma GCC optimize( unroll-loops ) using namespace std; long long MOD9 = 998244353; long long MOD = 1000000007; double eps = 1e-12; void solve() { int n; cin >> n; vector<pair<int, int> > v(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; sort((v).begin(), (v).end()); int p = 0, c = 0; for (int i = 1; i < n; i++) { if (v[i].second < v[p].second) c++; else p = i; } cout << c << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, d1, d2; bool d[400001]; int a[200001]; int nextprime(int x) { for (int i = x + 1; i <= 2 * n; ++i) if (d[i]) return i; return 2 * n + 1000000; } int main() { ios_base::sync_with_stdio(0); cin >> n; int x; for (int i = 1; i <= n; ++i) { cin >> x; d1 += x == 1; d2 += x == 2; } memset(d, sizeof(d), 0); for (int i = 2; i <= 2 * n; ++i) { bool f = 1; for (int j = 2; j * j <= i; ++j) if (i % j == 0) { f = 0; break; } d[i] = f; } int prime = 0; int t = n; while (n > 0) { int tmp = nextprime(prime); while (tmp - prime >= 2 && d2 > 0 && n > 0) prime += 2, d2--, a[n] = 2, n--; while (tmp - prime >= 1 && d1 > 0 && n > 0) prime += 1, d1--, a[n] = 1, n--; while (tmp - prime >= 1 && d1 == 0 && d2 > 0) prime += 2, d2--, a[n] = 2, n--; } for (int i = t; i > 0; --i) cout << a[i] << ; }
#include <bits/stdc++.h> using namespace std; long long pwr(long long base, long long p, long long mod = (1000000007LL)) { long long ans = 1; while (p) { if (p & 1) ans = (ans * base) % mod; base = (base * base) % mod; p /= 2; } return ans; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } const double eps = 1e-7; struct PT { double x, y; PT() {} PT(double x, double y) : x(x), y(y) {} bool operator<(const PT &rhs) const { return make_pair(y, x) < make_pair(rhs.y, rhs.x); } bool operator==(const PT &rhs) const { return make_pair(y, x) == make_pair(rhs.y, rhs.x); } }; double cross(PT p, PT q) { return p.x * q.y - p.y * q.x; } double area2(PT a, PT b, PT c) { return cross(a, b) + cross(b, c) + cross(c, a); } void ConvexHull(vector<PT> &pts) { sort(pts.begin(), pts.end()); pts.erase(unique(pts.begin(), pts.end()), pts.end()); vector<PT> up, dn; for (int i = 0; i < pts.size(); i++) { while (up.size() > 1 && area2(up[up.size() - 2], up.back(), pts[i]) > eps) up.pop_back(); while (dn.size() > 1 && area2(dn[dn.size() - 2], dn.back(), pts[i]) < -eps) dn.pop_back(); up.push_back(pts[i]); dn.push_back(pts[i]); } pts = dn; for (int i = (int)up.size() - 2; i >= 1; i--) pts.push_back(up[i]); } vector<PT> arr; set<PT> banned; int main() { int n1, n2; scanf( %d%d , &n1, &n2); int X, Y; scanf( %d%d , &X, &Y); while (n1--) { int x, y; scanf( %d%d , &x, &y); banned.insert(PT(x, y)); arr.push_back(PT(x, y)); } int max_x = -1, max_y = -1; while (n2--) { int x, y; scanf( %d%d , &x, &y); arr.push_back(PT(x, y)); max_x = max(max_x, x); max_y = max(max_y, y); } arr.push_back(PT(0, max_y)); arr.push_back(PT(max_x, 0)); arr.push_back(PT(0, 0)); ConvexHull(arr); for (auto it : arr) if (banned.find(it) != banned.end()) { printf( Max ); return 0; } printf( Min ); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; const int MAXM = 100 + 10; const int MOD = 1000000007; const int INF = 1000000001; int a[20][120]; int l[20], r[20]; int f[20], g[20]; bool w[20]; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; int L = m + 1, R = 0; for (int i = n; i >= 1; i--) { l[i] = m + 1; for (int j = 0; j <= m + 1; j++) { char c; cin >> c; a[i][j] = c - 0 ; w[i] |= a[i][j]; if (a[i][j]) { r[i] = j; if (l[i] == m + 1) l[i] = j; } } } while (n > 1 && !w[n]) n--; for (int i = 0; i <= m + 1; i++) if (a[n][i]) { if (i < L) L = i; if (i > R) R = i; } f[1] = r[1] * 2; g[1] = m + 1; f[0] = -1; g[0] = 1000000000; for (int i = 2; i <= n; i++) { f[i] = g[i - 1] + m + 2; g[i] = f[i - 1] + m + 2; f[i] = min(f[i], f[i - 1] + 1 + r[i] * 2); g[i] = min(g[i], g[i - 1] + 1 + (m + 1 - l[i]) * 2); } cout << min(f[n - 1] + 1 + R, g[n - 1] + 1 + m + 1 - L) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { short n; int n1, n2; bool state = false; char handle[11]; cin >> n; for (short i = 0; i < n; i++) { cin >> handle; cin >> n1; cin >> n2; if (n1 >= 2400 && n2 > n1) state = true; } if (state) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000 * 1000 * 1000 + 7; const double EPS = 1e-9; int bit_count(int first) { return first == 0 ? 0 : 1 + bit_count(first & (first - 1)); } inline int low_bit(int first) { return first & -first; } inline int sign(double first) { return first < -EPS ? -1 : first > EPS ? 1 : 0; } inline int sign(int first) { return (first > 0) - (first < 0); } int nextComb(int first) { if (!first) { fprintf(stderr, nextComb(0) called n ); return ((1 << 30) - 1 + (1 << 30)); } int smallest = first & -first; int ripple = first + smallest; int ones = first ^ ripple; ones = (ones >> 2) / smallest; return ripple | ones; } template <class T> inline T gcd(T a, T b) { a = abs(a); b = abs(b); while (b) { int r = a % b; a = b; b = r; } return a; } template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } inline int getInt() { int a; return scanf( %d , &a) ? a : (fprintf(stderr, trying to read n ), -1); } inline double getDouble() { double a; return scanf( %lf , &a) ? a : (fprintf(stderr, trying to read n ), -1.0); } inline double myRand() { return ((double)rand() / RAND_MAX) + ((double)rand() / RAND_MAX / RAND_MAX); } const int N = 109; int s, c; int Left[N], Right[N]; vector<pair<int, pair<int, int> > > getOptions(int n) { vector<pair<int, pair<int, int> > > ret; for (int i = (int)0; i < (int)s; ++i) { if (Left[i] == -1) { int L = c - n / 2, R = c + (n - 1) / 2, cost = 0; for (int j = L; j <= R; ++j) cost += abs(c - i) + abs(c - j); ret.push_back(make_pair(cost, pair<int, int>(i, L))); } else { int L = Left[i] - n, R = Left[i] - 1, cost = 0; if (L >= 0) { for (int j = L; j <= R; ++j) cost += abs(c - i) + abs(c - j); ret.push_back(make_pair(cost, pair<int, int>(i, L))); } L = Right[i] + 1; R = Right[i] + n; cost = 0; if (R < s) { for (int j = L; j <= R; ++j) cost += abs(c - i) + abs(c - j); ret.push_back(make_pair(cost, pair<int, int>(i, L))); } } } return ret; } void update(int first, int a, int b) { if (Left[first] == -1) { Left[first] = a; Right[first] = b; } else { if (a < Left[first]) Left[first] = a; if (b > Right[first]) Right[first] = b; } } void myCode() { int n = getInt(); s = getInt(); c = s / 2; for (int i = (int)0; i < (int)s; ++i) Left[i] = Right[i] = -1; for (int i = (int)0; i < (int)n; ++i) { int first = getInt(); if (first > s) { printf( -1 n ); continue; } vector<pair<int, pair<int, int> > > a = getOptions(first); if (a.empty()) { printf( -1 n ); continue; } sort((a).begin(), (a).end()); printf( %d %d %d n , a[0].second.first + 1, a[0].second.second + 1, a[0].second.second + first); update(a[0].second.first, a[0].second.second, a[0].second.second + first - 1); } } int main() { srand(time(NULL)); myCode(); return 0; }
#include <bits/stdc++.h> using namespace std; int graph[27]; int grapht[27]; vector<bool> used(27, false); vector<bool> opened(27, false); string result; void dfs(int node) { used[node] = true; opened[node] = true; if (graph[node] == -1) { result += (node + a ); opened[node] = false; return; } if (opened[graph[node]]) { cout << NO ; exit(0); } dfs(graph[node]); opened[node] = false; result += (node + a ); } void dfsn(int node) { opened[node] = true; if (graph[node] != -1) { if (opened[graph[node]]) { cout << NO ; exit(0); } dfsn(graph[node]); } opened[node] = false; } set<int> letter; int main() { int n; cin >> n; fill(graph, graph + 27, -1); fill(grapht, grapht + 27, -1); while (n--) { string s; cin >> s; for (int i = 0; i < s.size() - 1; i++) { int b = s[i] - a ; int e = s[i + 1] - a ; if ((graph[b] != -1 && graph[b] != e) || (grapht[e] != -1 && grapht[e] != b)) { cout << NO ; exit(0); } graph[b] = e; grapht[e] = b; } for (auto i = s.begin(); i != s.end(); i++) { letter.insert(*i - a ); } } for (int i = 0; i < 27; i++) { dfsn(i); } for (int i = 26; i >= 0; i--) { if (letter.find(i) != letter.end() && grapht[i] == -1) { dfs(i); } } reverse(result.begin(), result.end()); cout << result; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<ll, ll>; const ll MOD = 1e9 + 7, N = 1e5 + 10; void test() { ll n; cin >> n; vector<ll> a(n); for (ll i = 0; i < n; i++) { cin >> a[i]; } vector<ll> ans; sort(a.begin(), a.end()); ll l = 0, h = n - 1; while (l < h) { ans.push_back(a[l]); ans.push_back(a[h]); l++, h--; } if (l == h) ans.push_back(a[l]); reverse(ans.begin(), ans.end()); for (ll i = 0; i < n; i++) { cout << ans[i] << n [i == n - 1]; } return; } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); ll tt = 1; cin >> tt; for (ll i = 0; i < tt; i++) test(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int N = 1e4 + 10; int main() { string str; cin >> str; cout << str[0]; int flag = 0; for (int i = 1; i < str.length(); i++) { if (!(i + 2 >= str.length() || flag == 1)) { if (str.substr(i, 2) == at ) { cout << @ ; i++; flag = 1; continue; } } if (i + 3 < str.length()) { if (str.substr(i, 3) == dot ) { cout << . ; i += 2; continue; } } cout << str[i]; } return 0; }
#include<bits/stdc++.h> #define mod 998244353 using namespace std; int n, A[2], f[200010], pw2; int powmod(int x, int y){ int ans = 1; for(;y;y >>= 1, x = 1ll * x * x % mod) if(y & 1)ans = 1ll * x * ans % mod; return ans; } int main(){ scanf( %d , &n); A[0] = 1; A[1] = 0; pw2 = powmod(2, mod - 2); for(int i = 1; i <= n; i++){ A[0] = 1ll * A[0] * pw2 % mod; A[1] = 1ll * A[1] * pw2 % mod; f[i] = A[!(i & 1)]; A[i & 1] += f[i]; } printf( %d n , f[n]); }
#include <bits/stdc++.h> using namespace std; const int N = 100; int gi() { int w = 0; bool q = 1; char c = getchar(); while ((c < 0 || c > 9 ) && c != - ) c = getchar(); if (c == - ) q = 0, c = getchar(); while (c >= 0 && c <= 9 ) w = w * 10 + c - 0 , c = getchar(); return q ? w : -w; } const int mod = 1e9 + 7; int f[N][N], g[N][N]; int inv[N]; inline void inc(int &x, int y) { (x += y) >= mod ? x -= mod : 0; } inline int C(int n, int m) { n += m - 1; int ans = 1; for (int i = 1; i <= m; i++) ans = 1LL * ans * (n - i + 1) % mod * inv[i] % mod; return ans; } int main() { int n = gi(), m = gi(), i, j, k, t, s, lim = n + 1; for (inv[0] = inv[1] = 1, i = 2; i < N; i++) inv[i] = mod - 1LL * (mod / i) * inv[mod % i] % mod; f[0][1] = 1; for (i = 0; i <= n; i++) for (j = 0; j <= lim; j++) { if (f[i][j]) for (k = 0; k <= i && k + i <= n; k++) for (t = 0; t <= lim; t++) { if (k == i) if (t < j) inc(g[i + k + 1][t], 2LL * f[i][j] * f[k][t] % mod); else if (t == j) inc(g[i + k + 1][j], 1LL * f[i][j] * f[k][t] % mod); else break; else inc(g[i + k + 1][min(j, t)], 2LL * f[i][j] * f[k][t] % mod); } if (g[i][j]) for (k = n - i; k >= 0; k--) for (t = lim - j; t >= 0; t--) if (f[k][t]) for (s = min((n - k) / i, (lim - t) / j); s; s--) inc(f[k + i * s][t + j * s], 1LL * f[k][t] * C(g[i][j], s) % mod); } printf( %d n , f[n][m]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (~scanf( %d , &n)) { printf( %d n , n); for (int i = 1; i <= n; i++) printf( 1 ); printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; bool compare(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second > b.second; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); long long i, j; string s; cin >> s; long long n = s.length(); long long f = 1; if (n < 4) { cout << 0; } else { long long sm = 0; long long fsm = 0; for (i = 0; i < n - 3; i++) { if (s[i] == b && s[i + 1] == e && s[i + 2] == a && s[i + 3] == r ) { sm = i + 1; } fsm += sm; } cout << fsm; } }
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; long long ar[n]; for (long long i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); long long dist = 0; for (long long i = 0; i < n; i++) dist += (long long)abs(ar[i] - (i + 1)); cout << dist << n ; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long T = 1; while (T--) solve(); return 0; }
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 100000; long long n; struct ab { long long a; long long b; bool operator < (const struct ab &c) const { return b < c.b; } } aa[N + 5]; long long sus[N + 5]; int main() { scanf( %lld , &n); long long su = 0; for (int i = 1; i <= n; ++i) { scanf( %lld%lld , &aa[i].a, &aa[i].b); su += aa[i].a; } sort(aa + 1, aa + n + 1); sus[n + 1] = 0; for (int i = n; i >= 1; --i) { sus[i] = sus[i + 1] + aa[i].a; } long long ans = 0; long long p = 0, ext = 0; for (int i = 1; i <= n; ++i) { long long cc = aa[i].b - (p + ext); if (sus[i + 1] <= ext) { if (aa[i].b <= p + ext) { ans += aa[i].a - (ext - sus[i + 1]); } else if (aa[i].b <= p + ext + aa[i].a - (ext - sus[i + 1])) { ans += p + ext + aa[i].a - (ext - sus[i + 1]) - aa[i].b; ans += 2 * cc; } else { ans += 2 * (aa[i].a - (ext - sus[i + 1])); } break; } else if (aa[i].b <= p + ext) { ans += aa[i].a; p += aa[i].a; } else if (sus[i + 1] - ext >= cc) { ans += 2 * cc; ext += cc; ans += aa[i].a; p += aa[i].a; } else { ans += 2 * (sus[i + 1] - ext); cc -= sus[i + 1] - ext; if (cc >= aa[i].a) { ans += 2 * aa[i].a; } else { ans += 2 * cc + (aa[i].a - cc); } break; } } // for (int i = 1; i <= n; ++i) // { // if (aa[i].b >= su) // { // ans += 2 * (su - p - ext); // break; // } // else if (aa[i].b <= p + ext) // { // ans += aa[i].a; // p += aa[i].a; // } // else if (sus[i + 1] >= aa[i].b - p) // { // ans += 2 * (aa[i].b - p - ext); // ext += aa[i].b - p - ext; // ans += aa[i].a; // p += aa[i].a; // } // else if (sus[i + 1] < aa[i].b - p - ext) // { // ans += // } // } printf( %lld , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int iinf = 1e9 + 7; const long long linf = 1ll << 60; const double dinf = 1e10; template <typename T> inline void scf(T &x) { bool f = 0; x = 0; char c = getchar(); while ((c < 0 || c > 9 ) && c != - ) c = getchar(); if (c == - ) { f = 1; c = getchar(); } while (c >= 0 && c <= 9 ) { x = x * 10 + c - 0 ; c = getchar(); } if (f) x = -x; return; } template <typename T1, typename T2> void scf(T1 &x, T2 &y) { scf(x); return scf(y); } template <typename T1, typename T2, typename T3> void scf(T1 &x, T2 &y, T3 &z) { scf(x); scf(y); return scf(z); } const int N = 233; int n, l, k; int p[N], a[N]; double dp[N][N][N * 2]; int main() { scf(n, l, k); for (int i = (1); i <= (n); i++) scf(p[i]); for (int i = (1); i <= (n); i++) scf(a[i]); dp[0][0][k + 200] = 1.0; for (int i = 0; i < (n); i++) for (int j = (0); j <= (i); j++) for (int k = (0); k <= (400); k++) if (dp[i][j][k] > 1e-20) { dp[i + 1][j][k] += dp[i][j][k] * (1.0 * (100 - p[i + 1]) / 100.0); int nxtk = k; if (a[i + 1] < 0) nxtk--; else nxtk += a[i + 1]; if (nxtk > 400) nxtk = 400; if (k >= 0) dp[i + 1][j + 1][nxtk] += dp[i][j][k] * (1.0 * p[i + 1] / 100.0); } double ans = 0.0; for (int j = (l); j <= (n); j++) for (int k = (200); k <= (400); k++) ans += dp[n][j][k]; printf( %.12f n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<int> v1(n), v2(m); vector<int> v; for (auto &x : v1) cin >> x; for (auto &x : v2) cin >> x; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (v1[i] == v2[j]) v.push_back(v1[i]); } if (v.size()) { cout << Yes << n ; cout << 1 << << v[0] << n ; } else cout << No << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; static int LOCAL = 0; const long long int mod = 1000000007ll; const long long int MOD = 1000000009ll; const long long int INF = LLONG_MAX / 10; const int inf = INT_MAX / 10; long long int powermod(long long int _a, long long int _b, long long int _m = mod) { long long int _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a) % _m; _b /= 2; _a = (_a * _a) % _m; } return _r; } long long int power(long long int _a, long long int _b) { long long int _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } long long int add(long long int a, long long int b, long long int m = mod) { long long int x = a + b; while (x >= m) x -= m; return x; } long long int sub(long long int a, long long int b, long long int m = mod) { long long int x = a - b; while (x < 0) x += m; return x; } long long int mul(long long int a, long long int b, long long int m = mod) { return ((a % m) * (b % m)) % m; } long long int gcd(long long int a, long long int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } long long int lcm(long long int a, long long int b) { return (max(a, b) / gcd(a, b)) * min(a, b); } struct pair_hash { std::size_t operator()(const std::pair<int, int> &p) const { auto h1 = std::hash<long long int>{}(p.first); auto h2 = std::hash<long long int>{}(p.second); return (int)add(h1, mul(h2, h1, MOD)); } }; struct cmp { bool operator()(pair<int, pair<long long int, long long int> > const &l, pair<int, pair<long long int, long long int> > const &r) { return l.second.first < r.second.first; } } myobject; multiset<int> second; int main() { int n, x; cin >> n; double sum = 0; for (int i = 1; i <= n; i++) scanf( %d , &x), second.insert(x), sum += x; double avg = sum / n; int cnt = 0; while (!(avg >= 4.50)) { x = *second.begin(); second.erase(second.begin()); sum -= x; sum += 5; avg = sum / n; cnt++; } cout << cnt; return 0; }
/* Author: EndlessK * Time: 2021-05-06 20:40:25 **/ #include<bits/stdc++.h> #define maxn 100010 #define pb push_back #define ll long long #define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std; int main() { fast; int t; cin>>t; map<int,int> mp; while(t--) { int n; int ans=0; cin>>n; for(int i=1;i<=min(9,n);i++) { ans++; } for(int i=11;i<=min(99,n);i+=11) { ans++; } for(int i=111;i<=min(999,n);i+=111) { ans++; } for(int i=1111;i<=min(9999,n);i+=1111) { ans++; } for(int i=11111;i<=min(99999,n);i+=11111) { ans++; } for(int i=111111;i<=min(999999,n);i+=111111) { ans++; } for(int i=1111111;i<=min(9999999,n);i+=1111111) { ans++; } for(int i=11111111;i<=min(99999999,n);i+=11111111) { ans++; } for(int i=111111111;i<=min(999999999,n);i+=111111111) { ans++; } cout<<ans<< n ; } return 0; }
#include <bits/stdc++.h> using namespace std; bool way[5003][5003]; int dis[5003], counter; vector<pair<int, int> > edge[5003], child[5003]; priority_queue<pair<int, int> > pq[5003]; void cari(int pos) { int j, sz; sz = edge[pos].size(); for (j = 0; j < sz; ++j) { if (way[pos][edge[pos][j].first]) { way[pos][edge[pos][j].first] = false; way[edge[pos][j].first][pos] = false; child[pos].push_back(edge[pos][j]); cari(edge[pos][j].first); } } } void cek(int posisi, int time) { int i, j, x, size, sz; sz = child[posisi].size(); for (i = 0; i < sz; ++i) { x = child[posisi][i].first; size = child[posisi][i].second; for (j = 0; j < size; ++j) { if (pq[x].empty()) break; pq[posisi].push(pq[x].top()); if (posisi == 1) { dis[pq[x].top().second] = time; ++counter; } pq[x].pop(); } cek(x, time); } } int main() { int i, a, b, c, n, sz, arrive, dummy, division[5003]; scanf( %d , &n); for (i = 1; i <= n; ++i) scanf( %d , &division[i]); memset(way, true, sizeof(way)); for (i = 1; i < n; ++i) { scanf( %d %d %d , &a, &b, &c); edge[a].push_back(make_pair(b, c)); edge[b].push_back(make_pair(a, c)); } cari(1); for (i = 2; i <= n; ++i) pq[i].push(make_pair(-division[i], i)); sz = child[1].size(); dis[1] = 0; int temp = 1; counter = 1; while (counter < n) { cek(1, temp); ++temp; } for (i = 1; i <= n; ++i) { if (i != n) printf( %d , dis[i]); else printf( %d n , dis[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; long long GCD(long long a, long long b) { if (b == 0) return a; return (a % b == 0 ? b : GCD(b, a % b)); } long long POW(long long base, long long exp) { long long val; val = 1; while (exp > 0) { if (exp % 2 == 1) { val = (val * base) % 1000000007; } base = (base * base) % 1000000007; exp = exp / 2; } return val; } int a[100005]; int pos[100005]; int main() { int n, i, c, mx; scanf( %d , &n); for (i = 1; i <= n; i++) { scanf( %d , &a[i]); } for (i = 1; i <= n; i++) { pos[a[i]] = i; } mx = 0; for (i = 1; i <= n; i++) { c = 1; while (i < n && pos[i + 1] > pos[i]) { i++; c++; } mx = max(mx, c); } printf( %d n , n - mx); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int maxn = 2e5 + 10, mod = 1e9 + 7, inf = 0x3f3f3f3f; int n, a[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); int _t; cin >> _t; while (_t--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; bool isok = true; sort(a + 1, a + 1 + n); for (int i = 2; i <= n; i++) if (a[i] - a[i - 1] > 1) isok = false; (isok ? cout << YES n : cout << NO n ); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000ll * 1000 * 1000 + 7; int main() { vector<string> name; string str; int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> str; name.push_back(str); } long long ans = 1ll; for (int i = 0; i < m; i++) { set<char> s; for (int j = 0; j < n; j++) s.insert(name[j][i]); ans = (ans * s.size()) % mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a[26]; int A[26]; string s, t; int x1, x2; char c; int main() { cin >> s >> t; for (int i = 0; i < t.size(); i++) { c = t[i]; if (c > 96) a[c - 97]++; else A[c - 65]++; } for (int i = 0; i < s.size(); i++) { c = s[i]; if (c > 96) { if (a[c - 97]) { a[c - 97]--; x1++; s[i] = 0 ; } } else if (A[c - 65]) { A[c - 65]--; x1++; s[i] = 0 ; } } for (int i = 0; i < s.size(); i++) { c = s[i]; if (c == 0 ) continue; if (c >= 97) { c = c - 32; if (A[c - 65]) { x2++; A[c - 65]--; } } else { c = c + 32; if (a[c - 97]) { x2++; a[c - 97]--; } } } cout << x1 << << x2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 10; int T, n, m, sum, du[maxn], K; vector<pair<int, int> > p[maxn]; struct node { int x, y; long long val; bool operator<(const node i) const { return val < i.val; } } a[maxn]; queue<int> q2; int b[maxn], cnt; long long dis[1000][1000]; long long ans[maxn], sum1, INF; int main() { cin >> n >> m >> K; for (int i = 1; i <= m; i++) { scanf( %d %d %lld , &a[i].x, &a[i].y, &a[i].val); } sort(a + 1, a + m + 1); int Mx = min(m, K); memset(dis, 0x3f, sizeof(dis)); INF = dis[0][0]; for (int i = 1; i <= Mx; i++) { if (!b[a[i].x]) { cnt++; b[a[i].x] = cnt; } if (!b[a[i].y]) { cnt++; b[a[i].y] = cnt; } dis[b[a[i].x]][b[a[i].y]] = dis[b[a[i].y]][b[a[i].x]] = a[i].val; } for (int i = 1; i <= cnt; i++) dis[i][i] = 0; for (int k = 1; k <= cnt; k++) { for (int i = 1; i <= cnt; i++) { for (int j = 1; j <= cnt; j++) { dis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]); } } } for (int i = 1; i <= cnt; i++) { for (int j = i + 1; j <= cnt; j++) { if (i != j && dis[i][j] != INF) { ans[++sum1] = dis[i][j]; } } } sort(ans + 1, ans + sum1 + 1); cout << ans[K] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct online_hull { map<int, int> points; long long area; inline online_hull() { points.clear(); area = 0; } inline map<int, int>::iterator prev(map<int, int>::iterator it) { return it == points.begin() ? it : --it; } inline map<int, int>::iterator next(map<int, int>::iterator it) { return it == points.end() ? it : ++it; } inline long long traparea(long long x1, long long y1, long long x2, long long y2) { return (x2 - x1) * (y1 + y2); } inline long long triarea(long long x1, long long y1, long long x2, long long y2, long long x3, long long y3) { return traparea(x1, y1, x2, y2) + traparea(x2, y2, x3, y3) + traparea(x3, y3, x1, y1); } inline long long traparea(map<int, int>::iterator a, map<int, int>::iterator b) { return traparea(a->first, a->second, b->first, b->second); } inline long long triarea(map<int, int>::iterator it) { long long sum = 0; if (it != points.begin()) sum += traparea(prev(it), it); if (next(it) != points.end()) sum += traparea(it, next(it)); if (it != points.begin() && next(it) != points.end()) sum += traparea(next(it), prev(it)); return sum; } inline bool bad(map<int, int>::iterator it) { if (points.size() < 3 || it == points.begin() || next(it) == points.end()) return false; return triarea(it) <= 0; } inline bool insert(int x, int y) { if (points.find(x) != points.end()) { if (y <= points[x]) return false; area -= triarea(points.find(x)); points.erase(x); } map<int, int>::iterator it = points.insert(make_pair(x, y)).first; if (bad(it)) { points.erase(it); return false; } area += triarea(it); while (bad(prev(it))) { area -= triarea(prev(it)); points.erase(prev(it)); } while (bad(next(it))) { area -= triarea(next(it)); points.erase(next(it)); } return true; } inline bool contains(int x, int y) { if (points.empty()) return false; map<int, int>::iterator first = points.begin(), last = prev(points.end()); if (x < first->first || x > last->first) return false; if (x == first->first) return y <= first->second; if (x == last->first) return y <= last->second; map<int, int>::iterator it = points.lower_bound(x), p = prev(it); return triarea(p->first, p->second, x, y, it->first, it->second) <= 0; } }; int Q; online_hull upper, lower; int main() { scanf( %d , &Q); while (Q-- > 0) { int type, x, y; scanf( %d %d %d , &type, &x, &y); if (type == 1) { upper.insert(x, y); lower.insert(x, -y); } else if (type == 2) puts((upper.contains(x, y) && lower.contains(x, -y)) ? YES : NO ); } return 0; }
#include <bits/stdc++.h> using namespace std; map<string, int> id; inline int getID(string s) { if (id.find(s) == id.end()) { int sz = id.size(); id[s] = sz; } return id[s]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, a, b, k, f; cin >> n >> a >> b >> k >> f; map<pair<int, int>, int> rcost; int last = -1, sum = 0; for (int i = 0; i < n; ++i) { string u, v; cin >> u >> v; int su = getID(u); int sv = getID(v); if (last == su) { rcost[pair<int, int>(min(su, sv), max(su, sv))] += b; sum += b; } else { rcost[pair<int, int>(min(su, sv), max(su, sv))] += a; sum += a; } last = sv; } vector<int> diff; for (auto it = rcost.begin(); it != rcost.end(); it++) { int cost = it->second; if (cost > f) { diff.push_back(cost - f); } } sort(diff.rbegin(), diff.rend()); for (int i = 0; i < min((int)diff.size(), k); ++i) { sum -= diff[i]; } cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; inline int two(int n) { return 1 << n; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; const double pi = 2 * acos(0.0); const int mod = 1000000; int main() { std::ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; long long m, n; cin >> m >> n; double res = m; for (int i = 1; i < m; i++) { res -= pow(i / double(m), n); } cout << fixed << setprecision(10) << res; }
#include <bits/stdc++.h> using namespace std; const int N = 2005; const long long inf = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; vector<int> ga[N], gb[N]; int n, a, b; int ca[N], cb[N]; int dp[N][N][2]; int ha[N], hb[N]; int lfta[N], rgta[N], lftb[N], rgtb[N]; int cnta, cntb; int faa[N][11], fab[N][11]; void dfsa(int u, int last) { lfta[u] = ++cnta; ha[u] = ha[last] + 1; for (int i = 1; i < 11; i++) faa[u][i] = faa[faa[u][i - 1]][i - 1]; for (auto v : ga[u]) { if (v == last) continue; faa[v][0] = u; dfsa(v, u); } rgta[u] = cnta; } void dfsb(int u, int last) { lftb[u] = ++cntb; hb[u] = hb[last] + 1; for (int i = 1; i < 11; i++) fab[u][i] = fab[fab[u][i - 1]][i - 1]; for (auto v : gb[u]) { if (v == last) continue; fab[v][0] = u; dfsb(v, u); } rgtb[u] = cntb; } int costa(int u, int v) { int x = u; if (!v) return ha[u] - 1; for (int i = 10; i >= 0; i--) { if (!faa[x][i]) continue; if (lfta[faa[x][i]] <= lfta[v] && rgta[faa[x][i]] >= rgta[v]) continue; x = faa[x][i]; } return ha[u] - ha[x] + 1; } int costb(int u, int v) { int x = u; if (!v) return hb[u] - 1; for (int i = 10; i >= 0; i--) { if (!fab[x][i]) continue; if (lftb[fab[x][i]] <= lftb[v] && rgtb[fab[x][i]] >= rgtb[v]) continue; x = fab[x][i]; } return hb[u] - hb[x] + 1; } int main() { int i, j; cin >> n; cin >> a; for (i = 2; i <= a; i++) { int fa; cin >> fa; ga[i].push_back(fa); ga[fa].push_back(i); } for (i = 1; i <= n; i++) cin >> ca[i]; cin >> b; for (i = 2; i <= b; i++) { int fa; cin >> fa; gb[i].push_back(fa); gb[fa].push_back(i); } for (i = 1; i <= n; i++) cin >> cb[i]; dfsa(1, 0); dfsb(1, 0); memset(dp, 0x3f, sizeof(dp)); dp[0][0][0] = dp[0][0][1] = 0; for (i = 1; i <= n; i++) { for (j = 0; j < i; j++) { dp[i][j][0] = min(dp[i][j][0], dp[i - 1][j][0] + costa(ca[i], ca[i - 1])); dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][1] + costb(cb[i], cb[i - 1])); dp[i][i - 1][0] = min(dp[i][i - 1][0], dp[i - 1][j][1] + costa(ca[i], ca[j])); dp[i][i - 1][1] = min(dp[i][i - 1][1], dp[i - 1][j][0] + costb(cb[i], cb[j])); } } int ans = 0x3f3f3f3f; for (i = 0; i < n; i++) { ans = min(ans, dp[n][i][0]); ans = min(ans, dp[n][i][1]); } cout << (a - 1) + (b - 1) - ans; return 0; }
#include <bits/stdc++.h> using namespace std; struct th { int id, num; th() { id = 0, num = 0; } th(int a, int b) { id = a, num = b; } bool operator<(const th &a) const { if (num != a.num) return num < a.num; return id < a.id; } }; set<th> x; vector<int> ns[1000005]; int n, m; int hp[1000005]; long long dv = 0; int a[1000005]; long long sm = 0; void work(int bg, int n) { if (!n) return; else { if (bg == m) a[bg] = n; while (1) { set<th>::iterator t = x.end(); t--; if ((*t).num < n) break; th n1 = (*t); x.erase(n1); int nd = n1.num / n; for (int j = bg; j <= m; j++) for (int tm = 0; tm < nd; tm++) ns[j].push_back(n1.id); sm -= nd * n; n1.num %= n; x.insert(n1); } if (bg == m) return; set<th>::iterator q = x.begin(); int nt = sm / n; a[bg] = (*q).num; ns[bg].push_back((*q).id); x.erase(q); sm -= nt * a[bg]; if (a[bg]) { set<th>::iterator t = x.begin(); for (int j = 1; j < nt; j++) { th q1 = *t; t++; x.erase(q1); ns[bg].push_back(q1.id); q1.num -= a[bg]; x.insert(q1); } } work(bg + 1, n - a[bg]); } } int main() { scanf( %d%d , &n, &m); long long sum = 0; for (int i = 1; i <= m; i++) { scanf( %d , &hp[i]); sum += hp[i]; } dv = sum / n; if (sum % n) dv++; sum = dv * n; sm = sum; for (int i = 1; i < m; i++) sum -= hp[i]; hp[m] = sum; for (int i = 1; i <= m; i++) x.insert(th(i, hp[i])); work(1, n); cout << dv << endl; for (int i = 1; i <= m; i++) printf( %d , a[i]); printf( n ); for (int j = 1; j <= m; j++) while (ns[j].size() < dv) ns[j].push_back(1); for (int i = 1; i <= dv; i++) { for (int j = 1; j <= m; j++) printf( %d , ns[j][i - 1]); printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, k, m; int arr[50]; int sub[50]; int result = 0; int task_tot_time = 0; void take(int score, int t) { if (0 >= t) return; int tt = t; int ss = score; for (decltype(0) i = 0; i < k; i++) { int su = sub[i]; while (su > 0) { if (arr[i] > tt) break; ss++; tt -= arr[i]; su--; } } result = max(result, ss); if (t >= task_tot_time) { int task_left = sub[0]; for (decltype(1) i = 1; i < k; i++) task_left = min(task_left, sub[i]); if (task_left == 0) return; for (decltype(0) i = 0; i < k; i++) sub[i]--; result = max(result, score + k + 1); take(score + k + 1, t - task_tot_time); } } int main() { scanf( %d , &n) == 0, scanf( %d , &k) == 0, scanf( %d , &m) == 0; for (decltype(0) i = 0; i < k; i++) { scanf( %d , &arr[i]) == 0; task_tot_time += arr[i]; } for (decltype(0) i = 0; i < k; i++) sub[i] = n; sort(arr, arr + k); take(0, m); printf( %d , result); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 110; const int MOD = 1e9 + 7; long long n, k, dp[MAXN][MAXN][MAXN], local[MAXN][MAXN]; vector<int> adj[MAXN]; void add(long long& v, long long u) { v = (v + u) % MOD; } void dfs(int v, int par = -1) { dp[v][1][0] = dp[v][0][1] = 1; for (auto i : adj[v]) { if (i == par) continue; dfs(i, v); for (int w = 0; w < 23; w++) for (int j = 0; j < 50; j++) { local[w][j] = dp[v][w][j]; dp[v][w][j] = 0; } for (int a = 0; a <= (k + 1); a++) for (int b = 0; b <= (2 * k + 1); b++) for (int c = 0; c <= (k + 1); c++) for (int d = 0; d <= (2 * k + 1); d++) { int locala = 0; if (!d) locala = b; if (!b && d) locala = d + 1; if (b && d) locala = min(b, d + 1); int localb = a; if (localb && locala) { if ((localb - 1 + locala - 1) <= k) localb = 0; } int localc = c; if (localc) localc++; if (localc && locala) { if ((locala - 1 + localc - 1) <= k) localc = 0; } int locald = 0; if (!localc) locald = localb; if (!localb && localc) locald = localc; if (localb && localc) locald = max(localc, localb); if (locald <= (k + 1) && locala <= (2 * k + 3)) add(dp[v][locald][locala], local[a][b] * dp[i][c][d] % MOD); } } } int main() { ios::sync_with_stdio(0); cin >> n >> k; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1); long long answer = 0; for (int i = 0; i <= (2 * k + 3); i++) answer = (answer + dp[1][0][i]) % MOD; cout << answer << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long arr[200200]; int main() { string a, b; cin >> a >> b; long long x = 0, y = 0; for (int i = 0; i <= b.size() - a.size(); i++) x += (b[i] - 0 ); for (int i = 0; i < a.size(); i++) { y += (a[i] == 0 ) ? x : (b.size() - a.size() + 1) - x; x += b[b.size() - a.size() + 1 + i] - b[i]; } cout << y; return 0; }
#include <bits/stdc++.h> using namespace std; void Heap(int A[], int n, int l, int r) { int x, i, j; x = A[l]; i = l; while (1) { j = 2 * i; if (j > r) break; if (j < r && A[j + 1] >= A[j]) j++; if (x >= A[j]) break; A[i] = A[j]; i = j; } A[i] = x; } void HeapSort(int A[], int n) { int l, r, temp; l = n / 2; while (l >= 0) { Heap(A, n, l, n - 1); l--; } r = n - 1; while (r >= 1) { temp = A[0]; A[0] = A[r]; A[r] = temp; r--; Heap(A, n, 0, r); } } int mini(int a, int b, int c = 1000000010) { if (a <= b && a <= c) return a; else if (b <= c && b <= a) return b; else return c; } int maxi(int a, int b) { if (a > b) return a; else return b; } unsigned long long int fac(unsigned long long int a) { unsigned long long int r = 1; for (int i = 2; i <= a; i++) r *= i; return r; } bool issimple(unsigned long long n) { for (unsigned long long i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } unsigned long long nod(unsigned long long a, unsigned long long b) { unsigned long long temp; while (b) { a %= b; temp = a; a = b; b = temp; } return a; } unsigned long long nok(unsigned long long a, unsigned long long b, unsigned long long nod) { return a / nod * b; } int main() { int x, y, count, t, i, n, k, m; bool flag; cin >> t; while (t--) { cin >> n; count = n / 2 + n % 2; cout << count << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long dot(complex<long long> c1, complex<long long> c2) { return imag(c1 * conj(c2)); } double dot(complex<double> c1, complex<double> c2) { return imag(c1 * conj(c2)); } long long point(complex<long long> c1, complex<long long> c2) { return real(c1 * conj(c2)); } double point(complex<double> c1, complex<double> c2) { return real(c1 * conj(c2)); } void solve() { long double n, pi2, alpha, res; pi2 = acos(0.0); cin >> n; alpha = pi2 * (n - 1.0) / n; res = tan(alpha); cout << res << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, cases; cin >> cases; cout << fixed << setprecision(10); for (t = 0; t < cases; t++) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 47 == 0 || n % 744 == 0 || n % 4 == 0 || n % 7 == 0 || n % 44 == 0 || n % 444 == 0 || n % 777 == 0 || n % 74 == 0 || n % 447 == 0 || n % 474 == 0 || n % 477 == 0) { cout << YES ; } else { cout << NO ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 45; int nd, nm, G[N][N]; int cntClique, pts[N], res[N], cnt[N]; map<string, int> H; int n; vector<int> v; bool dfs(int pos, int num) { for (int i = pos + 1; i <= n; ++i) { if (cnt[i] + num <= cntClique) return false; if (G[pos][i]) { int ok = 1; for (int id = 1; id <= num; ++id) { if (!G[i][pts[id]]) { ok = 0; break; } } if (ok) { pts[num + 1] = i; if (dfs(i, num + 1)) return true; } } } if (num > cntClique) { for (int i = 1; i <= num; ++i) { res[i] = pts[i]; } cntClique = num; return true; } return false; } void maxClique() { cntClique = -1; for (int i = n; i > 0; --i) { pts[1] = i; dfs(i, 1); cnt[i] = cntClique; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(G, 1, sizeof G); cin >> nd >> nm; for (int i = 1; i <= nd; ++i) { int op; string s; cin >> op; if (op == 1) { v.clear(); } else { cin >> s; if (!H.count(s)) { H[s] = ++n; } int y = H[s]; for (auto& x : v) { G[x][y] = 0; G[y][x] = 0; } v.push_back(y); } } maxClique(); if (cntClique < 0) { cout << 0 ; } else { cout << cntClique; } return 0; }
#include <bits/stdc++.h> using namespace std; void _print(long long int t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } bool cmp(pair<long long int, long long int> a, pair<long long int, long long int> b) { if (a.first != b.first) return a.first > b.first; else return a.second > b.second; } long long int binarySearch(vector<long long int> v, long long int l, long long int r, long long int x) { while (l <= r) { long long int mid = l + (r - l) / 2; if (v[mid] == x) return mid; else if (v[mid] < x) l = mid + 1; else r = mid - 1; } return -1; } long long int power(long long int x, long long int n) { if (n == 0) return 1; else if (n % 2 == 0) return power(x * x, n / 2); else return x * power(x * x, (n - 1) / 2); } long long int ModularExpo(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void solve() { long long int n; cin >> n; for (long long int i = 0; i < n; i++) cout << i + 2 << ; cout << n ; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 100001; int n, m, x, y, b, r, p, l, ri; long long ans; vector<int> B, R, P; int bef(int x, int c) { if (!c) return (lower_bound(B.begin(), B.end(), x) - B.begin() - 1); return (lower_bound(R.begin(), R.end(), x) - R.begin() - 1); } int aft(int x, int c) { if (!c) return (lower_bound(B.begin(), B.end(), x) - B.begin()); return (lower_bound(R.begin(), R.end(), x) - R.begin()); } long long get(int x) { long long mn = ri - B[x]; for (int i = x; i < b && B[i] < ri; i++) { long long left = B[i] - l, right = 0; if (i + 1 != b && B[i + 1] < ri) right = ri - B[i + 1]; mn = min(mn, left + right); } return mn; } long long get1(int x) { long long mn = ri - R[x]; for (int i = x; i < r && R[i] < ri; i++) { long long left = R[i] - l, right = 0; if (i + 1 != r && R[i + 1] < ri) right = ri - R[i + 1]; mn = min(mn, left + right); } return mn; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { char c; cin >> x >> c; if (c == B ) B.push_back(x); else if (c == R ) R.push_back(x); else P.push_back(x); } b = B.size(); r = R.size(); p = P.size(); if (!P.size()) { if (b) ans = B[b - 1] - B[0]; if (r) ans += R[r - 1] - R[0]; cout << ans; return 0; } x = bef(P[0], 0); y = bef(P[0], 1); if (x >= 0) ans += P[0] - B[0]; if (y >= 0) ans += P[0] - R[0]; x = aft(P[p - 1], 0); y = aft(P[p - 1], 1); if (x != B.size()) ans += B[b - 1] - P[p - 1]; if (y != R.size()) ans += R[r - 1] - P[p - 1]; for (int i = 0; i < p - 1; i++) { l = P[i]; ri = P[i + 1]; long long dist = ri - l; int x = aft(l, 0), y = aft(l, 1); if ((x == b || B[x] > ri) && (y == r || R[y] > ri)) { ans += dist; continue; } long long mn = 2 * dist; long long k = dist; if (x < b && B[x] < ri) k += get(x); if (y < r && R[y] < ri) k += get1(y); ans += min(mn, k); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2 * int(1e9); const long long INFll = 1ll * int(1e9) * int(1e9); const int MOD = 1000000007; template <typename T> ostream& operator<<(ostream& out, pair<T, T>& a) { out << a.first << << a.second; return out; } template <typename T> istream& operator>>(istream& in, pair<T, T>& a) { in >> a.first >> a.second; return in; } template <typename T> ostream& operator<<(ostream& out, vector<pair<T, T> > a) { for (int i = 0; i < a.size(); ++i) out << a[i] << endl; return out; } template <typename T> istream& operator>>(istream& in, vector<T>& a) { for (int i = 0; i < a.size(); ++i) in >> a[i]; return in; } template <typename T> ostream& operator<<(ostream& out, vector<T> a) { for (int i = 0; i < a.size(); ++i) if (i == a.size() - 1) out << a[i]; else out << a[i] << ; return out; } struct IN { bool any; operator bool() const { return any; } } CIN; inline IN& operator>>(IN& in, int& x) { x = 0, in.any = 0; char c; while ((c = getchar()) > 0) { if (c < 48 || c > 57) if (in.any) return in; else continue; in.any = 1, x = x * 10 + c - 0 ; } return in; } int main() { ios_base::sync_with_stdio(0); int q; CIN >> q; while (q--) { int n, m; CIN >> n >> m; vector<vector<int> > a(n, vector<int>(m)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) CIN >> a[i][j]; int sum = 0; for (int j = 0; j < m; ++j) sum += abs(a[n / 2][j] - a[n / 2 - 1][j]); if (sum / long double(m) > 17) cout << YES n ; else cout << NO n ; if (0) cerr << sum / long double(m) << endl; } if (0) cerr << fixed << setprecision(0) << TIME = << clock() / (long double)CLOCKS_PER_SEC * 1000 << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; void dir(char& smb, int& x, int& y) { if (smb == L ) x--; else if (smb == R ) x++; else if (smb == U ) y++; else y--; } void solve() { int n; cin >> n; string s; cin >> s; map<pair<int, int>, int> mpp; int x = 0, y = 0; mpp[make_pair(0, 0)] = 0; int mn = n + 5, l = -1, r = n + 1; for (int i = 1; i < n + 1; i++) { char smb = s[i - 1]; dir(smb, x, y); auto it = mpp.find(make_pair(x, y)); if (it != mpp.end()) { pair<pair<int, int>, int> p = *it; int ind = p.second; if (i - ind - 1 < mn) { mn = i - ind - 1; l = ind; r = i - 1; } } mpp[make_pair(x, y)] = i; } if (l == -1) cout << -1 n ; else cout << l + 1 << << r + 1 << n ; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tc; cin >> tc; while (tc--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, x1, y1, x2, y2; cin >> n >> m >> x1 >> y1 >> x2 >> y2; int x = abs(x1 - x2), y = abs(y1 - y2); if (x <= 4 && y <= 4 && x + y <= 6) cout << First << endl; else cout << Second << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; template <typename T> void print(vector<T> v) { for (T &i : v) cout << i << ; cout << n ; } void solve() { unsigned long long int n, x; cin >> n >> x; vector<unsigned long long int> v(n); for (unsigned long long int &i : v) cin >> i; sort(v.begin(), v.end()); unsigned long long int sum = 0; for (unsigned long long int i : v) { sum += i * x; if (x > 1) x--; } cout << sum << n ; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int tcs = 1; while (tcs--) solve(); return 0; }
#include <bits/stdc++.h> int main() { long long n, m; int dig[10]; char cn[12], cm[12]; while (scanf( %s n%s , &cn, &cm) != EOF) { if (strlen(cn) != strlen(cm)) { printf( WRONG_ANSWER n ); continue; } n = atol(cn); m = atol(cm); for (int i = 0; i < 10; i++) dig[i] = 0; if (n < 10) { if (n == m) printf( OK n ); else printf( WRONG_ANSWER n ); } else { while (n) { dig[n % 10]++; n /= 10; } long long ret = 0; for (int i = 1; i <= 9; i++) { if (dig[i]) { ret *= 10; ret += i; dig[i]--; break; } } for (int i = 0; i <= 9; i++) { while (dig[i]) { ret *= 10; ret += i; dig[i]--; } } if (ret == m) printf( OK n ); else printf( WRONG_ANSWER n ); } } return 0; }
#include <bits/stdc++.h> using namespace std; void input(long &n) { scanf( %ld , &n); } void output(long n) { printf( %ld , n); } void input(long &n, long a[]) { input(n); for (long i = 1; i <= n; i++) input(a[i]); } bool test(long n, long a[], long id1, long id2, long tid, vector<long> &st1, vector<long> &st2) { st1.clear(); st2.clear(); st1.push_back(a[id1]); st1.push_back(a[id2]); long isize2; if (tid == -1) { isize2 = 0; } else { isize2 = 1; st2.push_back(a[tid]); } long seed = a[id2] - a[id1]; long seed2 = 0; long nextnum2 = 0; long nextnum = a[id2] + seed; for (long i = id2 + 1; i <= n; i++) { if (isize2 >= 2 && a[i] == nextnum && a[i] == nextnum2) { if (i == n) { nextnum += seed; st1.push_back(a[i]); } else if (nextnum + seed == a[i + 1]) { nextnum += seed; st1.push_back(a[i]); } else if (nextnum2 + seed2 == a[i + 1]) { st2.push_back(a[i]); nextnum2 += seed2; isize2++; } else return false; } else if (a[i] == nextnum) { nextnum += seed; st1.push_back(a[i]); } else { if (isize2 >= 2) { if (nextnum2 == a[i]) { st2.push_back(a[i]); isize2++; nextnum2 += seed2; } else if (isize2 == 2) { st2.push_back(st2[1]); isize2++; st2[1] = st1.back(); st1.pop_back(); if (st2[2] - st2[1] != st2[1] - st2[0]) return false; seed2 = st2[2] - st2[1]; nextnum2 = st2[2] + seed2; i--; } else return false; } else { st2.push_back(a[i]); isize2++; if (isize2 == 2) { seed2 = st2[1] - st2[0]; nextnum2 = st2[1] + seed2; } } } } if (seed == 0 && st1.size() > 2) return false; if (seed2 == 0 && st2.size() > 2) return false; if (isize2 == 0) return false; return true; } bool solve(long n, long a[], vector<long> &st1, vector<long> &st2) { if (n == 2) { st1.push_back(a[1]); st2.push_back(a[2]); return true; } else if (n == 3 && a[1] == a[2] && a[2] == a[3]) { st1.push_back(a[1]); st1.push_back(a[1]); st2.push_back(a[1]); return true; } else if (n == 4 && a[1] == a[2] && a[2] == a[3] && a[3] == a[4]) { st1.push_back(a[1]); st1.push_back(a[1]); st2.push_back(a[1]); st2.push_back(a[1]); return true; } if (test(n, a, 1, 3, 2, st1, st2)) return true; if (test(n, a, 2, 3, 1, st1, st2)) return true; if (test(n, a, 1, 2, -1, st1, st2)) return true; return false; } void output(bool res, vector<long> &st1, vector<long> &st2) { if (res) { for (vector<long>::iterator it = st1.begin(); it < st1.end(); it++) printf( %ld , *it); printf( n ); for (vector<long>::iterator it = st2.begin(); it < st2.end(); it++) printf( %ld , *it); } else cout << No solution n ; } long n, a[30010 + 1]; vector<long> st1, st2; int main() { input(n, a); bool res = solve(n, a, st1, st2); output(res, st1, st2); }
#include <bits/stdc++.h> using namespace std; int a1, a2; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> a1 >> a2; int res = 0; if (a1 == 1 && a2 == 1) { cout << 0 << n ; return 0; } while (a1 > 0 && a2 > 0) { if (a1 < a2) ++a1, a2 -= 2; else ++a2, a1 -= 2; ++res; } cout << res << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b = 1, c = 2, o = 1, j, k; cin >> a; int p = a / 2; for (k = 0; k < a; k++) { for (j = 0; j < p; j++) { cout << c << ; c += 2; } for (j = 0; j < a - p * 2; j++) { cout << b << ; b += 2; } for (int j = 0; j < p; j++) { cout << c << ; c += 2; } cout << endl; if (p == 0) o = 0; if (o == 0) p++; else p--; } return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std; int ar[30005]; int dp1[30005][205][2], dp2[30005][205][2][5]; int main() { int n, k; scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) scanf( %d , &ar[i]), ar[i] += ar[i - 1]; fill(dp1[0][0], dp1[n][k] + 2, -1000000000); fill(dp2[0][0][0], dp2[n][k][1] + 5, -1000000000); dp1[0][0][0] = 0; dp1[0][0][1] = 0; for (int i = 0; i < 2; i++) for (int j = -2; j <= 2; j++) dp2[1][1][i][j + 2] = j * ar[1]; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) for (int l = 0; l < 2; l++) { int &tmp = dp1[i][j][l]; if (!j) tmp = 0; else { tmp = max(tmp, dp1[i - 1][j][l]); int l1 = (!l ? 0 : 2), l2 = l1 - 2; if (j == 1) l1--, l2++; else if (j == k) l1 = 1, l2 = -1; tmp = max(tmp, dp2[i][j][0][l1 + 2]); tmp = max(tmp, dp2[i][j][1][l2 + 2]); } for (int r = -1 - (l != 0); r <= 2 - (l != 0); r++) dp2[i + 1][j + 1][(l != 0)][r + 2] = max(dp2[i][j + 1][(l != 0)][r + 2], tmp) + r * (ar[i + 1] - ar[i]); } printf( %d n , max(dp1[n][k][0], dp1[n][k][1])); }
#include <bits/stdc++.h> using namespace std; void aayush() { ios_base::sync_with_stdio(false); cin.tie(NULL); } int main() { aayush(); long long int t; cin >> t; while (t--) { long long int n; cin >> n; if (n % 4 == 0) { cout << YES << endl; } else { cout << NO << endl; } } return 0; }