solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; inline int read(void) { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } const int maxn = 5005, inf = 0x3f3f3f3f, maxv = maxn + maxn * 4, maxk = 1e5 + 5; const int maxe = maxn + maxk + maxn * 14 + 4 * maxn * 2 + maxn; int n, m, head[maxv], tot = 1, S, T; int cur[maxv], d[maxv], id[maxn << 2], ans[maxn]; bool is_second[maxn]; int spe[maxn][3], cnt[maxn]; struct Edge { int y, next, w; Edge() {} Edge(int _y, int _next, int _w) : y(_y), next(_next), w(_w) {} } e[maxe << 1]; inline void connect(int x, int y, int w) { e[++tot] = Edge(y, head[x], w); head[x] = tot; } inline int get(int x) { return x + 4 * m; } struct SegmentTree { int L[maxn << 2], R[maxn << 2]; void build(int o, int l, int r) { L[o] = l; R[o] = r; if (l == r) { connect(o, T, 1); connect(T, o, 0); id[o] = l; return; } int mid = (l + r) >> 1; build((o << 1), l, mid); build((o << 1 | 1), mid + 1, r); connect(o, (o << 1), inf); connect((o << 1), o, 0); connect(o, (o << 1 | 1), inf); connect((o << 1 | 1), o, 0); } void add(int o, int ql, int qr, int fr) { if (ql <= L[o] && R[o] <= qr) { connect(fr, o, 1); connect(o, fr, 0); return; } int mid = (L[o] + R[o]) >> 1; if (ql <= mid) add((o << 1), ql, qr, fr); if (qr > mid) add((o << 1 | 1), ql, qr, fr); } int find(int o, int q) { if (L[o] == R[o]) { return o; } int mid = (L[o] + R[o]) >> 1; if (q <= mid) return find((o << 1), q); else return find((o << 1 | 1), q); } } Tree; inline bool bfs(void) { queue<int> q; q.push(S); memset(d, -1, sizeof d); d[S] = 1; cur[S] = head[S]; while (q.size()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = e[i].next) { int y = e[i].y; if (d[y] == -1 && e[i].w) { d[y] = d[x] + 1; cur[y] = head[y]; if (y == T) return true; q.push(y); } } } return false; } int find(int x, int limit) { if (x == T) return limit; int flow = 0; for (int i = cur[x]; i && flow < limit; i = e[i].next) { int y = e[i].y; cur[x] = i; if (d[y] == d[x] + 1 && e[i].w) { int t = find(y, min(e[i].w, limit - flow)); if (!t) d[y] = -1; e[i].w -= t; e[i ^ 1].w += t; flow += t; } } return flow; } inline int dinic(void) { int r = 0, flow; while (bfs()) while ((flow = find(S, inf)) > 0) r += flow; return r; } void scheme(int now, int& s) { if (s) return; if (4 * m + 1 <= now && now <= 4 * m + n) { s = now - 4 * m; return; } for (int i = head[now]; i; i = e[i].next) { int y = e[i].y; if ((i & 1) && e[i].w) { scheme(y, s); --e[i].w; return; } } } int main() { n = read(); m = read(); S = 0; T = n + 4 * m + 1; Tree.build(1, 1, m); for (int i = 1; i <= n; ++i) { int opt = read(); if (opt == 0) { int K = read(); while (K--) { int x = read(); connect(get(i), Tree.find(1, x), 1); connect(Tree.find(1, x), get(i), 0); } connect(S, get(i), 1); connect(get(i), S, 0); } else if (opt == 1) { int l = read(), r = read(); Tree.add(1, l, r, get(i)); connect(S, get(i), 1); connect(get(i), S, 0); } else if (opt == 2) { int a = read(), b = read(), c = read(); connect(get(i), Tree.find(1, a), 1); connect(Tree.find(1, a), get(i), 0); connect(get(i), Tree.find(1, b), 1); connect(Tree.find(1, b), get(i), 0); connect(get(i), Tree.find(1, c), 1); connect(Tree.find(1, c), get(i), 0); connect(S, get(i), 2); connect(get(i), S, 0); is_second[i] = true; spe[i][0] = a; spe[i][1] = b; spe[i][2] = c; } } int tmp = dinic(); printf("%d\n", tmp); for (int i = head[T]; i; i = e[i].next) { int y = e[i].y; if (e[i].w) { scheme(y, ans[id[y]]); --e[i].w; } } for (int i = 1; i <= m; ++i) { ++cnt[ans[i]]; } for (int i = 1; i <= n; ++i) { if (cnt[i] == 1 && is_second[i]) { if (ans[spe[i][0]] != i) ans[spe[i][0]] = i; else ans[spe[i][1]] = i; } } for (int i = 1; i <= m; ++i) { if (ans[i]) printf("%d %d\n", ans[i], i); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long po(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = (res * a) % 1000000007; } a = (a * a) % 1000000007; b = b / 2; } return res % 1000000007; } long long n, s, f, ans, p, da, arr[100005], l, r; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); { cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i % n]; } cin >> s >> f; for (int i = 1; i < n; i++) { l = (s - i + n) % n; r = (f - i + n) % n; ans = ans + arr[l] - arr[r]; if (ans > da) da = ans, p = i; } cout << p + 1 << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; template<typename T> void out(T x) { cout << x << endl; exit(0); } #define watch(x) cout << (#x) << " is " << (x) << endl using ll = long long; const int maxn = 1e5 + 10; const ll inf = 1e10; int n, m; ll a[maxn][2]; // color, val bool viz[maxn]; vector<pair<ll,int>> g[maxn]; void dfs(int at) { viz[at] = true; for (auto ed: g[at]) { int to = ed.second; ll s = ed.first; if (viz[to]) continue; a[to][0] = -a[at][0]; a[to][1] = s - a[at][1]; dfs(to); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for (int i=0; i<m; i++) { int u,v,s; cin>>u>>v>>s; g[u].push_back({s,v}); g[v].push_back({s,u}); } a[1][0] = 1; dfs(1); ll lo = 1; ll hi = inf; ll ans = -1; //non-bipartite answer for (int i=1; i<=n; i++) { if (a[i][0] == -1) { hi = min(hi, a[i][1]-1); } else { lo = max(lo, 1-a[i][1]); } for (auto ed: g[i]) { ll s = ed.first; int to = ed.second; if (a[i][0]+a[to][0] == 0) { //-1,1 if (a[i][1]+a[to][1] != s) { out(0); } } else { // not bipartite ll cur = s - a[i][1] - a[to][1]; if (cur%2) out(0); cur /= (a[i][0]+a[to][0]); if (cur<=0) out(0); if (ans == -1) { ans = cur; } else if (ans != cur) { out(0); } } } } if (~ans) { if (lo <= ans && ans <= hi) out(1); out(0); } else { ll res = max(0ll,hi-lo+1); out(res); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i, max = 0; cin >> n; int arr[1001] = {0}; for (i = 1; i <= n; i++) { cin >> k; arr[k]++; if (k > max) max = k; } for (i = 1; i <= max - 2; i++) { if ((arr[i] != 0) && (arr[i + 1] != 0) && (arr[i + 2] != 0)) { cout << "YES"; return 0; } } cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; map<string, string> p, l; int main() { ios::sync_with_stdio(false); int q; cin >> q; string a, b; while (q--) { cin >> a >> b; if (l.find(a) == l.end()) { p[a] = b; l[b] = a; } else { p[l[a]] = b; l[b] = l[a]; p.erase(a); } } cout << p.size() << endl; for (map<string, string>::iterator itr = p.begin(); itr != p.end(); itr++) cout << itr->first << " " << itr->second << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; int a[1 << 21], temp[1 << 21]; int mp[1 << 21]; long long ans[22][2]; int mask[22]; void solve(int L, int R) { if (L == R) return; int M = (L + R) / 2; solve(L, M); solve(M + 1, R); int l = L, r = M + 1; int now = mp[R - L + 1]; int i = L; while (l <= M && r <= R) { if (a[l] > a[r]) { ans[now][0] += M - l + 1; temp[i++] = a[r++]; continue; } temp[i++] = a[l++]; } i = L; l = L; r = M + 1; while (l <= M && r <= R) { if (a[l] < a[r]) { ans[now][1] += R - r + 1; temp[i++] = a[l++]; continue; } temp[i++] = a[r++]; } while (l <= M) temp[i++] = a[l++]; while (r <= R) temp[i++] = a[r++]; for (int i = L; i <= R; i++) a[i] = temp[i]; } int main() { scanf("%d", &n); for (int i = 0; i <= n; i++) mp[1 << i] = i; for (int i = 1; i <= (1 << n); i++) scanf("%d", &a[i]); solve(1, 1 << n); long long res = 0; for (int i = 0; i <= n; i++) res += ans[i][0]; int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int p; scanf("%d", &p); for (int j = p; j >= 0; j--) { res += ans[j][mask[j] ^ 1] - ans[j][mask[j]]; mask[j] ^= 1; } printf("%I64d\n", res); } return 0; }
3
#include <bits/stdc++.h> #define int long long #define double long double #define INF 1e18 using namespace std; signed main() { int H, W, X, Y; cin >> H >> W >> X >> Y; if (H*W%2 == 1 && (X+Y)%2 == 1) { cout << "No" << endl; } else { cout << "Yes" << endl; } }
0
#include <bits/stdc++.h> const int N = 200000 + 10; inline std::pair<int, int> operator+(const std::pair<int, int> &lhs, const std::pair<int, int> &rhs) { return std::pair<int, int>(lhs.first + rhs.first, std::max(lhs.second, rhs.second)); } int n, k, val[N]; std::vector<int> adj[N]; int fa[N], size[N], f[N], g[N]; bool check(int th) { static int q[N]; fa[1] = 0; q[1] = 1; for (int i = 1, r = 1; i <= r; ++i) { int a = q[i]; for (int j = 0; j < adj[a].size(); ++j) { int b = adj[a][j]; if (b != fa[a]) fa[q[++r] = b] = a; } } for (int i = 1; i <= n; ++i) size[i] = 1; for (int i = n; i > 0; --i) size[fa[q[i]]] += size[q[i]]; for (int i = n; i > 0; --i) { int a = q[i]; if (val[a] < th) { f[a] = 0; continue; } f[a] = 1; int temp = 0; for (int j = 0; j < adj[a].size(); ++j) { int b = adj[a][j]; if (b == fa[a]) continue; if (f[b] == size[b]) f[a] += f[b]; else temp = std::max(temp, f[b]); } f[a] += temp; } for (int i = 1; i <= n; ++i) { int a = q[i]; static std::pair<int, int> info[N], pre[N], suf[N]; for (int j = 0; j < adj[a].size(); ++j) { int b = adj[a][j]; if (b == fa[a]) info[j] = (g[a] == n - size[a] ? std::pair<int, int>(g[a], 0) : std::pair<int, int>(0, g[a])); else info[j] = (f[b] == size[b] ? std::pair<int, int>(f[b], 0) : std::pair<int, int>(0, f[b])); } for (int j = 0; j < adj[a].size(); ++j) pre[j] = suf[j] = info[j]; for (int j = 1; j < adj[a].size(); ++j) pre[j] = pre[j - 1] + pre[j]; for (int j = adj[a].size() - 2; j >= 0; --j) suf[j] = suf[j + 1] + suf[j]; for (int j = 0; j < adj[a].size(); ++j) { std::pair<int, int> cur; if (j) cur = cur + pre[j - 1]; if (j + 1 < adj[a].size()) cur = cur + suf[j + 1]; int b = adj[a][j]; if (b == fa[a]) continue; if (val[a] >= th) g[b] = cur.first + cur.second + 1; else g[b] = 0; } } for (int a = 1; a <= n; ++a) { if (val[a] < th) continue; std::pair<int, int> cur; for (int i = 0; i < adj[a].size(); ++i) { int b = adj[a][i]; if (b == fa[a]) cur = cur + (g[a] == n - size[a] ? std::pair<int, int>(g[a], 0) : std::pair<int, int>(0, g[a])); else cur = cur + (f[b] == size[b] ? std::pair<int, int>(f[b], 0) : std::pair<int, int>(0, f[b])); } if (cur.first + cur.second + 1 >= k) return true; } return false; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &val[i]); for (int i = n - 1; i--;) { int a, b; scanf("%d%d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } int l = *std::min_element(val + 1, val + n + 1), r = *std::max_element(val + 1, val + n + 1); while (l < r) { int mid = (l + r + 1) / 2; if (check(mid)) l = mid; else r = mid - 1; } printf("%d\n", l); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2111111; long long go(long long n) { while (n % 3 == 0) n /= 3; return n / 3 + 1; } int main() { long long n; cin >> n; cout << go(n) << endl; return 0; }
1
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <cctype> #include <algorithm> #include <random> #include <bitset> #include <queue> #include <functional> #include <set> #include <map> #include <vector> #include <chrono> #include <iostream> #include <limits> #include <numeric> #define LOG(FMT...) fprintf(stderr, FMT) using namespace std; typedef long long ll; typedef unsigned long long ull; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<class T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; } template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { if (!v.empty()) { os << v.front(); for (int i = 1; i < v.size(); ++i) os << ' ' << v[i]; } return os; } const int P = 998244353; int norm(int x) { return x >= P ? (x - P) : x; } void add(int &x, int y) { if ((x += y) >= P) x -= P; } void sub(int &x, int y) { if ((x -= y) < 0) x += P; } void exGcd(int a, int b, int &x, int &y) { if (!b) { x = 1; y = 0; return; } exGcd(b, a % b, y, x); y -= a / b * x; } int inv(int a) { int x, y; exGcd(a, P, x, y); return norm(x + P); } const int N = 310; char s[N]; int n0, n1; int s0[N], s1[N]; int p0[N], p1[N]; int dp[N][N], f[N][N][N]; int len[N][N][N]; int main() { #ifdef ELEGIA freopen("test.in", "r", stdin); int nol_cl = clock(); #endif ios::sync_with_stdio(false); cin.tie(nullptr); cin >> s; int n = strlen(s); for (int i = 0; i < n; ++i) if (s[i] == '0') p0[n0++] = i; else p1[n1++] = i; for (int i = 0; i < n; ++i) { s0[i + 1] = s0[i] + (s[i] == '0'); s1[i + 1] = s1[i] + (s[i] == '1'); } dp[0][0] = 1; for (int i = 0; i <= n; ++i) { memcpy(f[i], dp, sizeof(dp)); for (int j = 0; j <= n0; ++j) for (int k = 0; k <= n1; ++k) { add(f[i][j][k + 1], f[i][j][k]); add(f[i][j + 1][k], f[i][j][k]); } if (i == n) break; for (int j = 0; j <= n0; ++j) for (int k = 0; k <= n1; ++k) { if (s[n - 1 - i] == '0') add(dp[j][k + 1], dp[j][k]); else add(dp[j + 1][k], dp[j][k]); } } memset(len, -1, sizeof(len)); len[0][0][0] = 0; for (int i = 0; i <= n0; ++i) { for (int j = 0; j <= n1; ++j) { for (int k = i + j; k >= 0; --k) { if (len[i][j][k] == -1) continue; if (k) { len[i][j][k - 1] = max(len[i][j][k - 1], len[i][j][k] + 1); } int c0 = 0, c1 = 0; for (int t = 0; t < 2 && len[i][j][k] + t < n; ++t) { if (s[len[i][j][k] + t] == '0') ++c0; else ++c1; } if (c0 == 2) { len[i + 2][j][k + 1] = max(len[i + 2][j][k + 1], len[i][j][k] + 2); } if (c1 == 2) { len[i][j + 2][k + 1] = max(len[i][j + 2][k + 1], len[i][j][k] + 2); } if (c0 && c1) len[i + 1][j + 1][k + 1] = max(len[i + 1][j + 1][k + 1], len[i][j][k] + 2); if (c0) len[i + 1][j][k] = max(len[i + 1][j][k], len[i][j][k] + 2); if (c1) len[i][j + 1][k] = max(len[i][j + 1][k], len[i][j][k] + 2); } } } int ans = 0; for (int i = 0; i <= n0; ++i) for (int j = 0; j <= n1; ++j) { if (i + j == n) continue; int al = len[i][j][0]; if (al == -1) continue; al = min(al, n); add(ans, f[n - al][s0[al] - i][s1[al] - j]); } cout << ans << '\n'; #ifdef ELEGIA LOG("Time: %dms\n", int ((clock() -nol_cl) / (double)CLOCKS_PER_SEC * 1000)); #endif return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { for (int i = 0; i < 10; i++) { if (i == 4) { cout << "normal"; return 0; } cout << i << endl; string s; getline(cin, s); if (s == "great!" || s == "don`t think so" || s == "don`t touch me!" || s == "not bad" || s == "cool") { cout << "normal"; return 0; } if (s == "don`t even" || s == "are you serious?" || s == "go die in a hole" || s == "no way" || s == "worse" || s == "terrible") { cout << "grumpy"; return 0; } if (s == "no" && i > 2) { cout << "normal"; return 0; } } cout << "normal"; return 0; }
2
#include <bits/stdc++.h> int a[105]; int b[105]; int visit[1000055]; int abs(int a) { if (a < 0) a = -a; return a; } int main() { int e, t, i, j, k, pp, sign, sum, n; scanf("%d", &t); for (e = 1; e <= t; e++) { scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]); memset(visit, 0, sizeof(visit)); for (i = 1; i <= n; i++) for (j = i + 1; j <= n; j++) visit[abs(a[j] - a[i])] = 1; b[1] = 1; sum = 1; sign = 0; for (i = 2; i <= 1000000; i++) { if (sum >= n) { sign = 1; break; } k = i; pp = 1; for (j = 1; j <= sum; j++) if (visit[k - b[j]] == 1) { pp = 0; break; } if (pp == 1) { sum++; b[sum] = k; } } if (sign == 1) { printf("YES\n"); for (i = 1; i < n; i++) printf("%d ", b[i]); printf("%d\n", b[n]); } else printf("NO\n"); } }
1
#include <bits/stdc++.h> using namespace std; using int64 = long long; void DBG() { cerr << "]" << '\n'; } template <class H, class... T> void DBG(H h, T... t) { cerr << h; if (sizeof...(t)) cerr << ", "; DBG(t...); } const int MOD = (int)1e9 + 7; int main() { ios_base::sync_with_stdio(0), cin.tie(0); auto solve = [](int a, int b, int k) { if (a < 0 || b < 0 || k < 0) return 0LL; int B = 30; int64 s[B + 2][2][2][2], c[B + 2][2][2][2]; memset(s, 0, sizeof(s)); memset(c, 0, sizeof(c)); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) c[B + 1][i][j][k] = 1; for (int i = B; i >= 0; i--) { for (int eqA = 0; eqA < 2; eqA++) { for (int eqB = 0; eqB < 2; eqB++) { for (int eqK = 0; eqK < 2; eqK++) { int bitA = a >> (B - i) & 1; int bitB = b >> (B - i) & 1; int bitK = k >> (B - i) & 1; int lA = eqA ? bitA : 1; int lB = eqB ? bitB : 1; int lK = eqK ? bitK : 1; for (int x = 0; x <= lA; x++) { for (int y = 0; y <= lB; y++) { if ((x ^ y) > lK) continue; int neqA = eqA && (x == bitA); int neqB = eqB && (y == bitB); int neqK = eqK && ((x ^ y) == bitK); c[i][eqA][eqB][eqK] += c[i + 1][neqA][neqB][neqK]; c[i][eqA][eqB][eqK] %= MOD; s[i][eqA][eqB][eqK] += s[i + 1][neqA][neqB][neqK]; s[i][eqA][eqB][eqK] %= MOD; int64 v = 1LL * (x ^ y) * (1LL << (B - i)) % MOD; s[i][eqA][eqB][eqK] += c[i + 1][neqA][neqB][neqK] * v % MOD; s[i][eqA][eqB][eqK] %= MOD; } } } } } } return s[0][1][1][1] + c[0][1][1][1]; }; int q; cin >> q; while (q--) { int f1, c1, f2, c2, k; cin >> f1 >> c1 >> f2 >> c2 >> k; f1--, c1--, f2--, c2--, k--; int64 ans = solve(f2, c2, k) - solve(f2, c1 - 1, k) - solve(f1 - 1, c2, k) + solve(f1 - 1, c1 - 1, k); ans = (ans + 10LL * MOD) % MOD; cout << ans << '\n'; } return 0; }
5
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector> using namespace std; int nodes,u,v,degrees[100005]={},root; long long arr[100005],totaledge[100005]={}; vector <int> node[100005]; bool ans=true; void dfs(int titik,int par) { for(int i=0;i<node[titik].size();i++) if(node[titik][i]!=par) dfs(node[titik][i],titik); long long edgetambahan=2*arr[titik]-totaledge[titik]; if(edgetambahan<0) ans=false; if(edgetambahan>arr[titik]) ans=false; if(titik!=root&&edgetambahan>arr[par]) ans=false; totaledge[par]+=edgetambahan; } int main() { scanf("%d",&nodes); for(int i=1;i<=nodes;i++) scanf("%lld",&arr[i]); for(int i=1;i<=nodes-1;i++) { scanf("%d%d",&u,&v); degrees[u]++;degrees[v]++; node[u].push_back(v); node[v].push_back(u); } if(nodes==2) { if(arr[1]==arr[2]) printf("YES\n"); else printf("NO\n"); return 0; } for(int i=1;i<=nodes;i++) { if(degrees[i]==1) totaledge[i]+=arr[i]; else root=i; } dfs(root,0); if(totaledge[0]!=0) ans=false; if(ans) printf("YES\n"); else printf("NO\n"); }
0
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; using pii = pair<long long, long long>; template <typename T> using prior = priority_queue<T, vector<T>, greater<T>>; template <typename T> using Prior = priority_queue<T>; const long long INF = 1E18; const long long mod = 1E9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, sx, sy, tx, ty; cin >> t; while (t--) { cin >> sx >> sy >> tx >> ty; cout << (tx - sx) * (ty - sy) + 1 << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout << fixed << setprecision(13); double n, r, ans; cin >> n >> r; ans = r * sin(acos(-1.0) / n) * (1 / (1 - sin(acos(-1.0) / n))); cout << setprecision(7); cout << ans << "\n"; }
3
#include <bits/stdc++.h> using namespace std; const int RLEN = 1 << 20 | 1; inline char gc() { static char ibuf[RLEN], *ib, *ob; (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin)); return (ib == ob) ? EOF : *ib++; } inline int read() { char ch = gc(); int res = 0; bool f = 1; while (!isdigit(ch)) f ^= ch == '-', ch = gc(); while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = gc(); return f ? res : -res; } inline long long readll() { char ch = gc(); long long res = 0; bool f = 1; while (!isdigit(ch)) f ^= ch == '-', ch = gc(); while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = gc(); return f ? res : -res; } inline int readstring(char *s) { int top = 0; char ch = gc(); while (isspace(ch)) ch = gc(); while (!isspace(ch) && ch != EOF) s[++top] = ch, ch = gc(); s[top + 1] = '\0'; return top; } template <typename tp> inline void chemx(tp &a, tp b) { a = max(a, b); } template <typename tp> inline void chemn(tp &a, tp b) { a = min(a, b); } const int mod = 998244353; inline int add(int a, int b) { return (a + b) >= mod ? (a + b - mod) : (a + b); } inline int dec(int a, int b) { return (a < b) ? (a - b + mod) : (a - b); } inline int mul(int a, int b) { static long long r; r = (long long)a * b; return (r >= mod) ? (r % mod) : r; } inline void Add(int &a, int b) { a = (a + b) >= mod ? (a + b - mod) : (a + b); } inline void Dec(int &a, int b) { a = (a < b) ? (a - b + mod) : (a - b); } inline void Mul(int &a, int b) { static long long r; r = (long long)a * b; a = (r >= mod) ? (r % mod) : r; } inline int ksm(int a, int b, int res = 1) { for (; b; b >>= 1, Mul(a, a)) (b & 1) && (Mul(res, a), 1); return res; } inline int Inv(int x) { return ksm(x, mod - 2); } inline int fix(long long x) { x %= mod; return (x < 0) ? x + mod : x; } const int N = 200005, M = 66; long long bas[M], a[M], b[M]; int n, m, k, sz, p[M], C[M][M]; inline void init_C() { for (int i = 0; i < M; i++) { C[i][0] = C[i][i] = 1; for (int j = 1; j < i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]); } } void dfs(int pos, long long res) { if (pos >= k) { p[__builtin_popcountll(res)]++; return; } dfs(pos + 1, res ^ a[pos]), dfs(pos + 1, res); } void dfs2(int pos, long long res) { if (pos >= m - k) { { p[__builtin_popcountll(res)]++; return; } } dfs2(pos + 1, res ^ b[pos]), dfs2(pos + 1, res); } inline void solve2() { for (int i = 0; i < m - k; i++) { long long res = 0; for (int j = 0; j < k; j++) if (a[j] & (1ll << i)) res ^= 1ll << j; b[i] = res | (1ll << (m - 1 - i)); } dfs2(0, 0); for (int i = 0; i <= m; i++) { int res = 0; for (int j = 0; j <= m; j++) { int c = 0; for (int k = 0; k <= i; k++) { int now = mul(C[m - j][i - k], C[j][k]); if (k & 1) Dec(c, now); else Add(c, now); } Add(res, mul(c, p[j])); } cout << mul(res, mul(ksm(2, n - k), Inv(ksm(2, m - k)))) << " "; } } inline void solve1() { dfs(0, 0); for (int i = 0; i <= m; i++) cout << mul(p[i], ksm(2, n - k)) << " "; exit(0); } inline void insert(long long x) { for (int i = m - 1; ~i; i--) if ((x >> i) & 1) { if (!bas[i]) { sz++, bas[i] = x; return; } x ^= bas[i]; } } int main() { init_C(); n = read(), m = read(); for (int i = 1; i <= n; i++) insert(readll()); for (int i = 0; i < m; i++) if (bas[i]) { for (int j = 0; j < i; j++) if (bas[j] && ((bas[i] >> j) & 1)) bas[i] ^= bas[j]; long long x = 0; for (int j = 0; j < m; j++) if (!bas[j]) { x <<= 1, x += (bas[i] >> j) & 1; } a[k] = x + (1ll << (m - 1 - k)), k++; } if (k <= 26) solve1(); else solve2(); }
5
#include <bits/stdc++.h> using namespace std; long long inf = 1LL << 62; int n; long long c, p[100010], s[100010]; long long dp[100010], tmp[100010]; int main() { scanf("%d%lld", &n, &c); for (int i = 1; i <= n; i++) scanf("%lld", &p[i]); for (int i = 1; i <= n; i++) scanf("%lld", &s[i]); for (int j = 0; j <= n; j++) dp[j] = inf; dp[0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= n; j++) tmp[j] = inf; tmp[0] = dp[0] + p[i]; for (int j = 1; j <= i; j++) tmp[j] = min(dp[j - 1] + s[i], dp[j] + p[i] + c * j); memcpy(dp, tmp, sizeof(dp)); } long long ans = inf; for (int i = 0; i <= n; i++) ans = min(ans, dp[i]); printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18 + 5; int mod = (int)1e9 + 7; const int logN = 18; const int inf = 1e9 + 9; const int N = 3003; int n, m, x, y, z, t; vector<int> v[2][N]; vector<pair<int, int> > go[2][N]; int dist[N][N]; pair<int, int> Q[N]; pair<int, pair<pair<int, int>, pair<int, int> > > ans; void get(int node, int wh) { int bas = 1, son = 0; Q[++son] = make_pair(node, 0); vector<pair<int, int> > temp; for (int i = 1; i <= n; i++) dist[node][i] = -6 * n; while (bas <= son) { int second = Q[bas].first, cs = Q[bas].second; bas++; temp.push_back(make_pair(second, cs)); dist[node][second] = cs; for (__typeof(v[wh][second].begin()) it = v[wh][second].begin(); it != v[wh][second].end(); it++) if (dist[node][*it] < 0) { dist[node][*it] = cs + 1; Q[++son] = make_pair(*it, cs + 1); } } int s = 3; while (s-- && temp.size()) { go[wh][node].push_back(temp.back()); temp.pop_back(); } } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d %d", &x, &y); v[0][x].push_back(y); v[1][y].push_back(x); } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dist[i][j] = -5 * n; for (int i = 1; i <= n; i++) { get(i, 1); get(i, 0); } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i != j) { for (__typeof(go[1][i].begin()) it = go[1][i].begin(); it != go[1][i].end(); it++) for (__typeof(go[0][j].begin()) it2 = go[0][j].begin(); it2 != go[0][j].end(); it2++) { if (it->first != j && it->first != it2->first && it->first != i) if (it2->first != j && it->first != it2->first && it2->first != i) { ans = max(ans, make_pair(it2->second + it->second + dist[i][j], make_pair(make_pair(it->first, i), make_pair(j, it2->first)))); } } } cout << ans.second.first.first << ' ' << ans.second.first.second << ' ' << ans.second.second.first << ' ' << ans.second.second.second << '\n'; }
2
#include <bits/stdc++.h> using namespace std; void solve(); int main() { solve(); return 0; } void solve() { long long int n, l, a[111]; cin >> n >> l; for (long long int i = 0; i < n; ++i) cin >> a[i]; long long int best = 0; for (long long int i = l; i <= 100; ++i) { long long int ans = 0; for (long long int j = 0; j < n; ++j) { ans += a[j] / i; } best = max(best, ans * i); } cout << best; }
3
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; const int mod = 1e9 + 7; int dcmp(long double x, long double y) { return fabs(x - y) <= 1e-12 ? 0 : x < y ? -1 : 1; } void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int n; long long a[5001]; long long rec(int i, int j) { long long ans = (j - i + 1); long long mn = 1e17; for (int idx = i; idx <= j; idx++) { mn = min(mn, a[idx]); } vector<int> v; bool open = 0; for (int idx = i; idx <= j; idx++) { a[idx] -= mn; if (a[idx] > 0 && !open) { open = 1; v.push_back(idx); } else if (a[idx] == 0 && open) { v.push_back(idx - 1); open = 0; } } if (open) v.push_back(j); for (int idx = 0; idx < v.size(); idx += 2) { mn += rec(v[idx], v[idx + 1]); } return min(ans, mn); } int main() { fast(); cin >> n; long long mn = 1e9 + 1; for (int i = 0; i < n; i++) { cin >> a[i]; } cout << rec(0, n - 1) << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int N = 1e3 + 10; int main() { int n, m; cin >> n >> m; if (n == 0 && m != 0) { cout << "Impossible" << endl; return 0; } cout << max(n, m) << ' ' << n + max(0, m - 1) << endl; return 0; }
1
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; const int N = 100010; int a[N], b[N]; int num[N]; int main() { int n, m; scanf("%d %d", &n, &m); int sum = 0; for (int i = 1; i <= m; i++) scanf("%d", &num[i]), sum += (num[i] & 1); if (m == 1) { if (num[1] == 1) puts("1\n1\n1"); else printf("%d \n2 \n%d %d\n", num[1], num[1] - 1, 1); return 0; } if (sum > 2) puts("Impossible"); else { for (int i = 2; i <= m; i++) if (num[i] & 1) { if (num[1] & 1) swap(num[m], num[i]); else swap(num[i], num[1]); } for (int i = 1; i <= m; i++) a[i] = num[i]; int total = 0; if (num[1] > 1) b[++total] = num[1] - 1; for (int i = 2; i < m; i++) b[++total] = num[i]; b[++total] = num[m] + 1; for (int i = 1; i <= m; i++) printf("%d ", a[i]); puts(""); printf("%d\n", total); for (int i = 1; i <= total; i++) printf("%d ", b[i]); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long a[202020]; long long f[202020]; int n, m; namespace force { void main() { while (m--) { int op; scanf("%d", &op); if (op == 1) { int x, v; scanf("%d %d", &x, &v); a[x] = v; } else if (op == 2) { int l, r; scanf("%d %d", &l, &r); long long ans = 0; for (int i = 0; i <= r - l; i++) (ans += ((f[i] * a[i + l] % 1000000000) + 1000000000) % 1000000000) %= 1000000000; printf("%I64d\n", (ans + 1000000000) % 1000000000); } else { int l, r, d; scanf("%d %d %d", &l, &r, &d); for (int i = l; i <= r; i++) a[i] += d; } } } } // namespace force namespace more { long long coef[202020][2]; long long sum[202020][2]; long long value[202020]; void precalc() { coef[0][0] = coef[1][1] = 1; coef[0][1] = coef[1][0] = 0; for (int i = 2; i <= n; i++) for (int j = 0; j < 2; j++) coef[i][j] = ((coef[i - 2][j] - coef[i - 1][j]) % 1000000000 + 1000000000) % 1000000000; } void update(int x, int v) { int dt = (v - value[x]); value[x] = v; for (int r = x; r <= n; r += r & -r) { sum[r][0] = ((sum[r][0] + (long long)dt * f[x - 1]) % 1000000000 + 1000000000) % 1000000000; sum[r][1] = ((sum[r][1] + (long long)dt * f[x]) % 1000000000 + 1000000000) % 1000000000; } } long long query(int x, int flag) { long long ret = 0; for (int r = x; r; r -= r & -r) (ret += sum[r][flag] % 1000000000 + 1000000000) %= 1000000000; return ret; } void main() { memset(value, 0, sizeof(value)); memset(sum, 0, sizeof(sum)); precalc(); for (int i = 1; i <= n; i++) update(i, a[i]); while (m--) { int op; scanf("%d", &op); if (op == 1) { int x, v; scanf("%d %d", &x, &v); update(x, v); } else if (op == 2) { int l, r; scanf("%d %d", &l, &r); long long q0 = ((query(r, 1) - query(l - 1, 1)) % 1000000000 + 1000000000) % 1000000000; long long q1 = ((query(r, 0) - query(l - 1, 0)) % 1000000000 + 1000000000) % 1000000000; printf("%I64d\n", ((q0 * coef[l][0] + q1 * coef[l][1]) % 1000000000 + 1000000000) % 1000000000); } } } } // namespace more int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); f[0] = f[1] = 1; for (int i = 2; i <= n; i++) f[i] = (f[i - 1] + f[i - 2]) % 1000000000; if (n <= 100 && m <= 10000) force::main(); else more::main(); return 0; }
5
#include <bits/stdc++.h> using namespace std; void computeLPSArray(long long* pat, long long M, long long* lps); int KMPSearch(long long* pat, long long* txt, long long n, long long m) { long long M = m - 1; long long N = n - 1; long long ans = 0; long long lps[M]; computeLPSArray(pat, M, lps); long long i = 0; long long j = 0; while (i < N) { if (pat[j] == txt[i]) { j++; i++; } if (j == M) { ans++; j = lps[j - 1]; } else if (i < N && pat[j] != txt[i]) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } return ans; } void computeLPSArray(long long* pat, long long M, long long* lps) { long long len = 0; lps[0] = 0; long long i = 1; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } int main() { long long n, m; cin >> n >> m; long long sura[n + 1]; long long pi[m + 1]; long long ans[m]; long long baal[n]; for (long long i = 0; i < n; i++) { cin >> sura[i]; } for (long long i = 0; i < m; i++) { cin >> pi[i]; } if (m == 1) { cout << n << endl; return 0; } if (n < m) { cout << 0 << endl; return 0; } for (long long i = 1; i < m; i++) { ans[i - 1] = (pi[i] - pi[i - 1]); } for (long long i = 1; i < n; i++) { baal[i - 1] = (sura[i] - sura[i - 1]); } cout << KMPSearch(ans, baal, n, m) << endl; }
4
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } long long int power(long long int x, long long int y) { long long int res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } void solve() { long long int n; cin >> n; string s1, s2; cin >> s1 >> s2; vector<long long int> v1[27], v2[27]; for (int i = 0; i < s1.length(); i++) { if (s1[i] != '?') v1[s1[i] - 'a'].push_back(i); else v1[26].push_back(i); } for (int i = 0; i < s2.length(); i++) { if (s2[i] != '?') v2[s2[i] - 'a'].push_back(i); else v2[26].push_back(i); } vector<pair<long long int, long long int> > vec; for (int i = 0; i < 26; i++) { while (v1[i].size() != 0 && v2[i].size() != 0) { vec.push_back(make_pair(v1[i].back() + 1, v2[i].back() + 1)); v1[i].pop_back(); v2[i].pop_back(); } } for (int i = 0; i < 26; i++) { while (v1[i].size() != 0 && v2[26].size() != 0) { vec.push_back(make_pair(v1[i].back() + 1, v2[26].back() + 1)); v1[i].pop_back(); v2[26].pop_back(); } } for (int i = 0; i < 26; i++) { while (v1[26].size() != 0 && v2[i].size() != 0) { vec.push_back(make_pair(v1[26].back() + 1, v2[i].back() + 1)); v1[26].pop_back(); v2[i].pop_back(); } } while (v1[26].size() != 0 && v2[26].size() != 0) { vec.push_back(make_pair(v1[26].back() + 1, v2[26].back() + 1)); v1[26].pop_back(); v2[26].pop_back(); } cout << vec.size() << endl; for (int i = 0; i < vec.size(); i++) cout << vec[i].first << " " << vec[i].second << endl; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long int t = 1; while (t--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int M = 2e6 + 7; int read() { int ans = 0, f = 1, c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ans = ans * 10 + (c - '0'); c = getchar(); } return ans * f; } long long tot; int s[3 * M]; long long ans[M][10]; int n, m, xp, qp, ep; int lowbit(int x) { return x & -x; } void add(int x, long long v) { while (x <= n) { s[x] += v; x += lowbit(x); } } int query(int x) { long long ans = 0; while (x) { ans += s[x]; x -= lowbit(x); } return ans; } struct Q { int r, h, id, pos; bool operator<(const Q& x) const { return h < x.h; } void calc() { ans[id][pos] = query(r); } } q[2 * M]; struct pos { int x, y, w; bool operator<(const pos& h) const { return y < h.y; } void calc() { add(x, w); } } e[M]; int main() { int x1, y1, x2, y2; n = read(); m = read(); for (int i = 1; i <= n; i++) { y1 = read(); e[ep++] = (pos){i, y1, 1}; } for (int i = 1; i <= m; i++) { x1 = read(); y1 = read(); x2 = read(); y2 = read(); swap(y1, y2); q[qp++] = (Q){x1 - 1, y2 - 1, i, 1}; q[qp++] = (Q){x1 - 1, y1, i, 2}; q[qp++] = (Q){x1 - 1, n, i, 3}; q[qp++] = (Q){x2, y2 - 1, i, 4}; q[qp++] = (Q){x2, y1, i, 5}; q[qp++] = (Q){x2, n, i, 6}; q[qp++] = (Q){n, y2 - 1, i, 7}; q[qp++] = (Q){n, y1, i, 8}; q[qp++] = (Q){n, n, i, 9}; } sort(e, e + ep); sort(q, q + qp); for (int i = 0, j = 0; i < qp; i++) { while (j < ep && e[j].y <= q[i].h) e[j++].calc(); q[i].calc(); } for (int i = 1; i <= m; i++) { tot = 0; long long h[10]; h[1] = ans[i][1]; h[2] = ans[i][2] - ans[i][1]; h[3] = ans[i][3] - ans[i][2]; h[4] = ans[i][4] - ans[i][1]; h[5] = ans[i][5] - ans[i][4] - ans[i][2] + ans[i][1]; h[6] = ans[i][6] - ans[i][5] - ans[i][3] + ans[i][2]; h[7] = ans[i][7] - ans[i][4]; h[8] = ans[i][8] - ans[i][7] - ans[i][5] + ans[i][4]; h[9] = ans[i][9] - ans[i][8] - ans[i][6] + ans[i][5]; tot = tot + h[5] * (h[1] + h[2] + h[3] + h[4] + h[6] + h[7] + h[8] + h[9]); tot = tot + h[5] * (h[5] - 1) / 2; tot = tot + h[4] * (h[8] + h[9] + h[6]); tot = tot + h[6] * (h[7] + h[8]); tot = tot + h[1] * (h[6] + h[8] + h[9]); tot = tot + h[3] * (h[4] + h[7] + h[8]); tot = tot + h[2] * (h[4] + h[6] + h[7] + h[8] + h[9]); printf("%I64d\n", tot); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 4000006; using ll = long long; ll f[N]; int main() { ll n, m; while (cin >> n >> m) { for (int i = 1; i <= n; i++) f[i] = 0; f[1] = 1; ll sum = 0; ll sum2 = 0; for (int i = 2; i <= n; i++) { (f[i] += sum) %= m; (f[i] += sum2) %= m; (f[i] += f[i - 1]) %= m; (sum += f[i - 1]) %= m; (f[i] += 1) %= m; sum2 = (f[i] - sum); while (sum2 < 0) sum2 += m; sum2 %= m; if (f[i] < 0) { printf("%d %lld %lld\n", i, f[i - 1], sum2); cout << sum2 << endl; break; } for (int j = 2; j * i <= n; j++) { (f[j * i] += ((f[i] - f[i - 1] + m) % m)) %= m; } } cout << f[n] << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; class AKorotkiePodstroki { public: void solve(std::istream& in, std::ostream& out) { int t; in >> t; while (t-- > 0) { string s; in >> s; string r; for (int i = 0; i < s.size(); i += 2) r += s[i]; r += *s.rbegin(); out << r << endl; } } }; int main() { AKorotkiePodstroki solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
1
#include <bits/stdc++.h> using namespace std; int solve(int* arr, int n) { int cnt = 0, head = arr[n - 1]; for (int i = n - 2; i >= 0; --i) { if (arr[i] > head) ++cnt; else { head = arr[i]; } } return cnt; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; int* arr = new int[n]; for (int i = 0; i < n; ++i) cin >> arr[i]; cout << solve(arr, n) << endl; delete arr; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long int MAX_N = 1e5 + 5; const long long int mod = 1e9 + 7; int a[MAX_N]; int b[MAX_N]; void love() { int n; cin >> n; int flag = -1; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; if (flag != b[i] && flag != 3) { if (flag != -1) { flag = 3; continue; } flag = b[i]; } } if (flag == 3) { cout << "Yes"; return; } for (int i = 1; i < n; i++) { if (a[i - 1] > a[i]) { cout << "No"; return; } } cout << "Yes"; } int main() { int T; cin >> T; while (T--) { love(); cout << endl; } return 0; }
2
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <utility> #include <vector> #define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++) #define DEBUGP(val) cerr << #val << "=" << val << "\n" using namespace std; typedef long long int ll; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PI; const ll mod = 1e9 + 7; int main(void) { ios::sync_with_stdio(false); cin.tie(0); int n; while (cin >> n && n) { vector<VI> a(n, VI(n)); REP(i, 0, n) REP(j, 0, n) cin >> a[i][j]; vector<VI> fl(n, VI(n)); REP(i, 0, n) { int mi = 1e8; REP(j, 0, n) mi = min(mi, a[i][j]); REP(j, 0, n) fl[i][j] |= mi == a[i][j] ? 1 : 0; } REP(j, 0, n) { int ma = 0; REP(i, 0, n) ma = max(ma, a[i][j]); REP(i, 0, n) fl[i][j] |= ma == a[i][j] ? 2 : 0; } int c = 0; REP(i, 0, n) REP(j, 0, n) { if (fl[i][j] == 3) c = a[i][j]; } cout << c << endl; } }
0
#include <bits/stdc++.h> using namespace std; int func(int b,char s){ if(b==1) return s-'0'; return -(s-'0'); } int main(){ string S; cin>>S; for(int i=0;i<(1<<3);i++){ bitset<3> b(i); //1を+ if(7-(S[0]-'0')==func(b[2],S[1])+func(b[1],S[2])+func(b[0],S[3])){ cout<<S[0]; for(int j=2;j>=0;j--) cout<<(b[j]==1?'+':'-')<<S[abs(3-j)]; cout<<"=7"<<endl; break; } } }
0
#include <bits/stdc++.h> using namespace std; int a[3001]; int main() { int n, v; cin >> n >> v; long int s = 0; for (int i = 0; i < n; ++i) { int f, b; cin >> f >> b; a[f] = a[f] + b; } for (int i = 1; i < 3005; ++i) { s = s + min(a[i], v); a[i] = a[i] - min(a[i], v); a[i + 1] = a[i + 1] + min(a[i], v); } cout << s << endl; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 1000 * 1000 * 1000 + 7; const long long LINF = INF * (long long)INF; const int MAX = 200005; long long T[MAX * 8]; long long lazy[MAX * 8]; vector<pair<int, int> > input(int n) { vector<pair<int, int> > v(n), nv; for (auto &first : v) cin >> first.first >> first.second; sort(v.begin(), v.end()); return v; } void push(int v) { T[v] += lazy[v]; lazy[v * 2] += lazy[v]; ; lazy[v * 2 + 1] += lazy[v]; lazy[v] = 0; } void add(int v, int tl, int tr, int l, int r, int val) { push(v); if (l > r) return; if (tl == l && tr == r) { lazy[v] += val; push(v); return; } int tm = (tl + tr) / 2; add(v * 2, tl, tm, l, min(r, tm), val); add(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r, val); T[v] = max(T[v * 2], T[v * 2 + 1]); } int main() { ios_base::sync_with_stdio(0); int n, m, p; cin >> n >> m >> p; long long ans = -LINF; vector<pair<int, int> > v = input(n); vector<pair<int, int> > w = input(m); m = (int)(w.size()); for (int i = 0; i < m; i++) { add(1, 0, m - 1, i, i, -w[i].second); } vector<pair<pair<int, int>, int> > monstr(p); for (auto &first : monstr) cin >> first.first.first >> first.first.second >> first.second; sort(monstr.begin(), monstr.end()); n = (int)(v.size()); int ptrM = 0; for (int i = 0; i < n; i++) { while (ptrM < p && monstr[ptrM].first.first < v[i].first) { int it = lower_bound(w.begin(), w.end(), make_pair(monstr[ptrM].first.second + 1, -INF)) - w.begin(); add(1, 0, m - 1, it, m - 1, monstr[ptrM].second); ptrM++; } ans = max(ans, T[1] - v[i].second); } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int _; cin >> _; while (_--) { long long int i, n; cin >> n; long long int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } if (a[0] < a[n - 1]) { cout << "YES\n"; } else { cout << "NO\n"; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int N; char grid[128][128]; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0), cout.precision(15); cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> grid[i][j]; } } vector<int> cols; for (int i = 0; i < N; i++) { int k = -1; for (int j = 0; j < N; j++) { if (grid[i][j] == '.') { k = j; break; } } if (k != -1) cols.push_back(k); else break; if (cols.size() == N) { for (int i = 0; i < N; i++) { cout << i + 1 << " " << cols[i] + 1 << "\n"; } return 0; } } vector<int> rows; for (int j = 0; j < N; j++) { int k = -1; for (int i = 0; i < N; i++) { if (grid[i][j] == '.') { k = i; break; } } if (k != -1) rows.push_back(k); else break; if (rows.size() == N) { for (int i = 0; i < N; i++) { cout << rows[i] + 1 << " " << i + 1 << "\n"; } return 0; } } cout << -1 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; vector<int> a; int solve(vector<int>& a, int bit) { if (bit < 0) { return 0; } vector<int> b0, b1; for (int& i : a) { if (((1 << bit) & i) == 0) { b0.push_back(i); } else { b1.push_back(i); } } if (!b0.size()) { return solve(b1, bit - 1); } if (!b1.size()) { return solve(b0, bit - 1); } return (1 << bit) + min(solve(b1, bit - 1), solve(b0, bit - 1)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; a.resize(n); for (int& i : a) { cin >> i; } cout << solve(a, 29) << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, j; cin >> n; vector<long long int> v[n]; long long int a, b, count = 0, temp1, temp2; for (i = 0; i < n; i++) { cin >> a >> b; if (a != b) count++; } if (count > 0) { cout << "Happy Alex"; } else { cout << "Poor Alex"; } }
1
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int Maxn = 50 + 10; int n, k, cnt; long long f[Maxn][Maxn][2], c[Maxn][Maxn], sum, ans; void assign() { freopen("Ksenia and Combinatorics.in", "r", stdin); freopen("Ksenia and Combinatorics.out", "w", stdout); } void close() { fclose(stdin); fclose(stdout); } void init() { scanf("%d%d\n", &n, &k); c[0][0] = 1; for (int i = 1; i <= n; ++i) { c[i][0] = 1; for (int j = 1; j <= i; ++j) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; } f[1][0][0] = 1; f[0][0][1] = 1; for (int i = 2; i <= n; ++i) for (int l = 0, r = i - 1; l <= r; ++l, --r) { sum = (l != r) ? c[i - 1][l] : c[i - 2][l - 1]; if (i != n) sum = (sum * i) % mod; for (int k1 = 0; 2 * k1 <= l; ++k1) for (int k2 = 0; 2 * k2 <= r; ++k2) for (int c1 = 0; c1 <= 1; ++c1) for (int c2 = 0; c2 <= 1; ++c2) if (f[l][k1][c1] && f[r][k2][c2]) { cnt = !(c1 && c2); ans = ((f[l][k1][c1] * f[r][k2][c2]) % mod * sum) % mod; (f[i][k1 + k2 + cnt][cnt] += ans) %= mod; } } cout << (f[n][k][0] + f[n][k][1]) % mod << endl; } int main() { init(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long bit[123456] = {0}, a[123456]; long long update(long long pos, long long val) { while (pos < 112345) { bit[pos] += val; pos += pos & (-pos); } return 0; } long long query(long long pos) { long long sumi = 0; while (pos > 0) { sumi += bit[pos]; pos -= pos & (-pos); } return sumi; } int main() { std::ios::sync_with_stdio(false); long long n, i; cin >> n; double nn = n, ans = 0, sumi = 0, sumi1 = 0, sumi2 = 0; nn *= 1.00; for (i = 1; i < n + 1; i++) { cin >> a[i]; } double total = nn * (nn + 1); total /= 2; for (i = 1; i < n + 1; i++) { ans = (nn - i) * (nn - i + 1); ans /= 4.00; ans /= total; ans *= (i * 1.00); sumi += ans; } for (i = 1; i < n + 1; i++) { ans = query(100005) - query(a[i]); update(a[i], 1); sumi1 += ans; } sumi += (sumi1 / (nn + 1)) * (nn - 1); sumi += (2 * sumi1 / (nn + 1)); for (i = 0; i < 123456; i++) { bit[i] = 0; } for (i = 1; i < n + 1; i++) { ans = query(100005) - query(a[i]); ans *= 1.0; ans /= total; update(a[i], i); sumi2 += ans * (nn - i + 1); } sumi -= sumi2; cout << setprecision(20) << sumi << endl; }
5
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; vector<char*> tokenizer(const char* args) { char* token = new char[111]; strcpy(token, args); token = strtok(token, ", "); vector<char*> v({token}); while (token = strtok(NULL, ", ")) v.push_back(token); return reverse(v.begin(), v.end()), v; } void debugg(vector<char*> args) { cerr << "\b\b "; } template <typename Head, typename... Tail> void debugg(vector<char*> args, Head H, Tail... T) { debug() << " [" << args.back() << ": " << H << "] "; args.pop_back(); debugg(args, T...); } const int mod = 1e9 + 7; const int MX = 0x3f3f3f3f; const int maxn = 5e3 + 1; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, i, j; cin >> n; string s; cin >> s; vector<vector<int> > lcp(n, vector<int>(n)); for (i = n - 1; i >= 0; i--) { for (j = n - 1; j >= 0; j--) { if (s[i] == s[j]) lcp[i][j] = 1 + (i + 1 < n and j + 1 < n ? lcp[i + 1][j + 1] : 0); } } vector<int> dp(n); dp[0] = n; for (i = 1; i <= n - 1; i++) { dp[i] = n - i; for (j = 0; j <= i - 1; j++) { if (s[j] < s[i]) dp[i] = max(dp[i], dp[j] + n - i); else if (s[i] == s[j]) { if (lcp[i][j] + i < n and s[i + lcp[i][j]] > s[j + lcp[i][j]]) dp[i] = max(dp[i], dp[j] + n - i - lcp[i][j]); } } } cout << *max_element((dp).begin(), (dp).end()) << "\n"; } return 0; }
5
#include <stdio.h> int main(void) { int ans = 0, i; char s[20], t[20] = "CODEFESTIVAL2016"; scanf("%s", s); for(i = 0; i < 16; ++i) if(s[i] != t[i]) ++ans; printf("%d\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int getN(long long int sum) { sum = sum * 8; sum = sqrt(sum + 1) - 1; sum /= 2; return sum; } long long int apSum(long long int n) { long long int a = 1, d = 1; long long int res = (2 * a + (n - 1) * d) * n; res /= 2; return res; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } bool isPrime(long long int n) { for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } long long int mod = 998244353; vector<vector<long long int>> dp; long long int get(long long int n, long long int k, long long int m) { if (n == 0) return 0; if (k == 0) { return m; } if (dp[n][k] != -1) return dp[n][k]; long long int res1 = get(n - 1, k - 1, m) * (m - 1); long long int res2 = get(n - 1, k, m); res1 %= mod; res2 %= mod; dp[n][k] = (res1 + res2) % mod; return dp[n][k]; } void solveKaro() { long long int n, m, k; cin >> n >> m >> k; dp = vector<vector<long long int>>(n + 1, vector<long long int>(n + 1, -1)); long long int ans = get(n, k, m); cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int tCase = 1; while (tCase--) { solveKaro(); } }
3
#include <bits/stdc++.h> using namespace std; int main() { int numbers; bool alien = false, elephant = false; map<int, int> length; for (int i = 0; i < 6; i++) { cin >> numbers; length[numbers]++; } for (std::map<int, int>::iterator it = length.begin(); it != length.end(); ++it) { if (it->second >= 4) alien = true; if (it->second == 2 || it->second == 6) elephant = true; } if (alien == false) cout << "Alien" << endl; else if (alien == true && elephant == true) cout << "Elephant" << endl; else cout << "Bear" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int n, k, d; cin >> n >> k >> d; int trueAns = 0; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; if (d == 1) { cout << 1 << endl; continue; } map<int, int> c; int ans = 0; for (int i = 0; i < d; ++i) { if (c[a[i] - 1] == 0) ++ans; c[a[i] - 1]++; } trueAns = ans; if (n == d) { cout << ans << endl; continue; } for (int i = 1; i < n; ++i) { if (i + d > n) break; c[a[i - 1] - 1]--; if (c[a[i - 1] - 1] == 0) --ans; if (c[a[i + d - 1] - 1] == 0) ++ans; c[a[i + d - 1] - 1]++; trueAns = min(ans, trueAns); } cout << trueAns << endl; } return 0; }
2
#include<bits/stdc++.h> using namespace std; bool solve(){ int A,B,C,N,t[1000][3],r[1000]; cin>>A>>B>>C; if(A==0&&B==0&&C==0)return false; cin>>N; for(int i=0;i<N;i++){ for(int j=0;j<3;j++){ cin>>t[i][j]; t[i][j]--; } cin>>r[i]; } int S[1000]={0}; for(int _=0;_<N;_++){ for(int i=0;i<N;i++){ if(r[i]){ for(int j=0;j<3;j++){ S[t[i][j]]=1; } } else{ int sum=0; for(int j=0;j<3;j++){ if(S[t[i][j]]==1)sum++; } if(sum!=2)continue; for(int j=0;j<3;j++){ if(S[t[i][j]]!=1)S[t[i][j]]=-1; } } } } for(int i=0;i<A+B+C;i++){ if(S[i]==-1)cout<<0<<endl; else if(S[i])cout<<1<<endl; else cout<<2<<endl; } return true; } int main(){ while(solve()); }
0
#include <bits/stdc++.h> using namespace std; void solve() { long long a, b; cin >> a >> b; if (a < b) { cout << 0 << endl; } else if (a == b) { cout << "infinity" << endl; } else { set<long long> sol; for (long long i = 1; i * i <= (a - b); i++) { if ((a - b) % i == 0) { if (i > b) { sol.insert(i); } if ((a - b) / i > b) { sol.insert((a - b) / i); } } } cout << sol.size() << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; long long int arr[100005], dp[100005]; int main() { long long int n, k; cin >> n >> k; for (long long int i = 1; i < n + 1; i++) cin >> arr[i]; dp[0] = 0; for (long long int i = 1; i < n + 1; i++) { if (arr[i] == 0) { long long int val = (max(1LL, i - k) == 0 && i == 1) ? 0 : 1; if (i - k <= 0) dp[i] = i - 1 + min(n, i + k) - i + 1; else dp[i] = min(n, i + k) - max(0LL, i - k) + 1; } else { long long int val = (arr[i] + k >= i) ? 0 : 1; long long int sol; if (arr[i] + k >= n) dp[i] = dp[arr[i]]; else { if (val == 0) sol = min(n, i + k) - min(n, arr[i] + k + 1) + 1; else sol = min(n, i + k) - min(n, max(arr[i] + k + 1, i - k)) + 1; dp[i] = dp[arr[i]] + sol; } } } for (int i = 1; i < n + 1; i++) cout << dp[i] << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a[10][10]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } int x1 = a[0][1] + a[0][2]; int x2 = a[1][0] + a[1][2]; int x3 = a[2][0] + a[2][1]; int y = (x1 + x2 + x3) / 2; a[0][0] = y - x1; a[1][1] = y - x2; a[2][2] = y - x3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << a[i][j] << " "; } cout << "\n"; } return 0; }
2
#include<iostream> using namespace std; int main() { int n,i,x,minn=0x7fffffff,cnt=0; cin>>n; for(i=1;i<=n;i++) { cin>>x; if(x<=minn) { minn=x; cnt++; } } cout<<cnt<<endl; return 0; }
0
#include <bits/stdc++.h> long long int M = 1000000007; using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(25); ; long long int T = 1; while (T--) { long long int n, a, b, ans = 1e18, i; cin >> n >> a >> b; long long int arr[6]; for (i = 0; i < 4; i++) arr[i] = a; for (i = 4; i < 6; i++) arr[i] = b; sort(arr, arr + 6); do { long long int curr = 0, sum = 0; for (i = 0; i < 6; i++) { sum += arr[i]; if (sum > n) { curr++; sum = arr[i]; } } ans = min(ans, curr + 1); } while (next_permutation(arr, arr + 6)); cout << ans << endl; } }
2
#include <bits/stdc++.h> const int N = 50; int n, dp[N + 2][N + 2][N + 2][N + 2]; char str[N + 3]; int dfs(int x, int y, int z, int w) { if (~dp[x][y][z][w]) return dp[x][y][z][w]; dp[x][y][z][w] = std::max(z - x + 1, w - y + 1); for (int i = x; i < z; ++i) dp[x][y][z][w] = std::min(dp[x][y][z][w], dfs(x, y, i, w) + dfs(i + 1, y, z, w)); for (int i = y; i < w; ++i) dp[x][y][z][w] = std::min(dp[x][y][z][w], dfs(x, y, z, i) + dfs(x, i + 1, z, w)); return dp[x][y][z][w]; } int main() { scanf("%d", &n); std::memset(dp, -1, sizeof(dp)); for (int i = 1; i <= n; ++i) { scanf("%s", str + 1); for (int j = 1; j <= n; ++j) dp[i][j][i][j] = (str[j] == '#'); } printf("%d\n", dfs(1, 1, n, n)); return 0; }
4
#include<bits/stdc++.h> using namespace std; #define lop(i,n) for(i=0;i<n;i++) #define lop1(i,n) for(ll i=1;i<=n;i++) #define lopr(i,n) for(i=n-1;i>=0;i--) #define ll long long int #define pb push_back #define all(v) v.begin(),v.end() #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define endl "\n" #define F first #define S second #define mem(arr,val) memset(arr,val,sizeof(arr)) #define pii pair<int,int> #define pll pair<ll,ll> #define LCM(a,b) (a*b)/__gcd(a,b) #define mii map<int,int> #define mll map<ll,ll> #define ub upper_bound #define lb lower_bound #define sz(x) (ll)x.size() #define ld long double #define pcnt(x) __builtin_popcountll(x) const long long I1 = 1e9 + 7; const long long I2 = 1e18; const long long MAXM = 1e5 + 5; template<typename T, typename T1>T maxn(T &a, T1 b) {if (b > a)a = b; return a;} template<typename T, typename T1>T minn(T &a, T1 b) {if (b < a)a = b; return a;} ll arr[MAXM]; bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int least_prime(int n) { int i; lop(i, 32) { if (isPrime(i) && n % i == 0) return i; } } bool isPrime(ll n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll eval(ll m) { return (m * (m - 1) * (m - 2) * (m - 3) / 24); } // Returns factorial of n ll fact(ll n) { ll res = 1; for (ll i = 2; i <= n; i++) res = (res * i) % I1; return res; } ll binomialCoeff(ll n, ll k) { // Base Cases if (k == 0 || k == n) return 1; // Recur return binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k); } ll nCr(ll n, ll r) { return (fact(n) / (fact(r) * fact(n - r))) % I1; } ll leastFrequent(ll arr[], ll n) { // Sort the array sort(arr, arr + n); // find the min frequency using linear traversal ll min_count = n + 1, res = -1, curr_count = 1; for (ll i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) curr_count++; else { if (curr_count < min_count) { min_count = curr_count; res = arr[i - 1]; } curr_count = 1; } } // If last element is least frequent if (curr_count < min_count) { min_count = curr_count; res = arr[n - 1]; } return res; } int findRem(ll y) { if (y == 1 || y == 2) return pow(14, y); int temp = findRem(y / 2); if (y % 2 == 0) { return (temp * temp) % 41; } else return (temp * temp * 14) % 41; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t; cin >> t; ll n, i, j, k; while (t--) { cin >> n; if (n == 1) cout << 9 << endl; else { cout << 9; n--; int j = 8; lop(i, n) { cout << j; j++; if (j > 9) j = 0; } cout << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int n, x, pos, sm, bg, s, e, mid, ncr[1010][1010], fact[1010]; int main() { for (int i = 0; i < 1010; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || i == j) ncr[i][j] = 1; else ncr[i][j] = (ncr[i - 1][j] + ncr[i - 1][j - 1]) % 1000000007; } } fact[0] = 1; for (int i = 1; i < 1010; i++) fact[i] = (i * fact[i - 1]) % 1000000007; scanf("%lld", &n); scanf("%lld", &x); scanf("%lld", &pos); s = 0; e = n; while (s < e) { mid = s + (e - s) / 2; if (mid <= pos) { sm += (mid < pos); s = mid + 1; } else { bg++; e = mid; } } printf("%lld\n", ((((ncr[x - 1][sm] * ncr[n - x][bg]) % 1000000007 * fact[sm]) % 1000000007 * fact[bg]) % 1000000007 * fact[n - sm - bg - 1]) % 1000000007); return 0; }
3
#include <bits/stdc++.h> using namespace std; char s[500]; int get(char c) { int q = int(c); return q; } int mirror(int code) { int res = 0; for (int i = 0; i < 8; i++) { if ((code & (1 << i)) != 0) { res |= (1 << (7 - i)); } } return res; } int main() { gets(s); int n = strlen(s); for (int i = 0; i < n; i++) { int pre = get(s[i]); pre = mirror(pre); int x = mirror((i != 0) ? get(s[i - 1]) : 0); x = (x - pre) % 256; if (x < 0) { x += 256; } printf("%d\n", x); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxlongint = 2147483647; const int biglongint = 2139062143; struct ljb { int dest, flow, cost; ljb *next, *other; } * head[7005], *tail[7005], *p, *pre[7005]; int m, n, uu, rc, fc, ans1, ans2, S, o, v; int a[105][105], biao[105][105], flag[10005], cost[10005], u[3000005], flow[10005]; void mpush(int x, int y, int flow, int cost) { ljb *p, *q; p = new ljb; p->next = 0; p->dest = y, p->flow = flow, p->cost = cost; tail[x]->next = p, tail[x] = p; q = new ljb; q->next = 0; q->dest = x, q->flow = 0, q->cost = -cost; tail[y]->next = q, tail[y] = q; p->other = q, q->other = p; } void spfa() { ans1 = 0; ans2 = 0; while (true) { memset(flag, 0, sizeof(flag)); for (int i = 0; i <= S; i++) cost[i] = maxlongint; cost[0] = 0; rc = 1, fc = 1, u[rc] = 0; memset(flow, 0, sizeof(flow)); flow[0] = maxlongint; while (rc <= fc) { flag[u[rc]] = 0; p = head[u[rc]]->next; while (p != 0) { if ((p->flow > 0) && (cost[u[rc]] + p->cost < cost[p->dest])) { flow[p->dest] = min(flow[u[rc]], p->flow); cost[p->dest] = cost[u[rc]] + p->cost; pre[p->dest] = p; if (flag[p->dest] == 0) { flag[p->dest] = 1; ++fc, u[fc] = p->dest; } } p = p->next; } ++rc; } if (cost[S] == maxlongint) break; ans1 += flow[S]; ans2 += cost[S] * flow[S]; uu = S; while (uu > 0) { pre[uu]->flow -= flow[S]; pre[uu]->other->flow += flow[S]; uu = pre[uu]->other->dest; } } } void pt(int x, int y, int xc, int yc) { if ((xc < 1) || (xc > m) || (yc < 1) || (yc > n)) return; if (a[x][y] == a[xc][yc]) mpush(biao[x][y], biao[xc][yc], 1, 0); else mpush(biao[x][y], biao[xc][yc], 1, 1); } int main() { scanf("%d %d", &m, &n); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]); o = 0; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if ((i + j) % 2 == 0) ++o, biao[i][j] = o; v = o; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if ((i + j) % 2 == 1) ++o, biao[i][j] = o; S = o + 1; for (int i = 0; i <= S; i++) { p = new ljb; p->dest = 0, p->cost = 0, p->flow = 0; p->next = 0, p->other = 0; head[i] = p, tail[i] = p; } for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if ((i + j) % 2 == 0) { pt(i, j, i - 1, j); pt(i, j, i, j - 1); pt(i, j, i, j + 1); pt(i, j, i + 1, j); } for (int i = 1; i <= v; i++) mpush(0, i, 1, 0); for (int i = v + 1; i <= o; i++) mpush(i, S, 1, 0); spfa(); cout << ans2 << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m; long long t[1502][1502]; long long dp[1502][1502]; long long pf[1502][1502]; int main() { cin >> n >> m; for (int i = 0; i < n + 1; i++) { for (int j = 0; j < m + 1; j++) { if (i < n && j < m) scanf("%lld", &t[i][j]); dp[i][j] = -(1LL << 62); pf[i][j] = -(1LL << 62); } } for (int i = 0; i < n; i++) { pf[i][0] = t[i][0]; for (int j = 1; j < m; j++) { pf[i][j] = pf[i][j - 1] + t[i][j]; } } bool sm = true; for (int j = 0; j < m; j++) dp[0][j] = pf[0][j]; for (int j = m - 1; j >= 0; j--) dp[0][j] = max(dp[0][j + 1], pf[0][j]); for (int i = 1; i < n; i++) { if (sm) { for (int j = 0; j < m - 1; j++) dp[i][j] = dp[i - 1][j + 1] + pf[i][j]; for (int j = 1; j < m - 1; j++) dp[i][j] = max(dp[i][j], dp[i][j - 1]); } else { for (int j = 1; j < m; j++) dp[i][j] = dp[i - 1][j - 1] + pf[i][j]; for (int j = m - 1; j >= 0; j--) dp[i][j] = max(dp[i][j + 1], dp[i][j]); } sm = !sm; } long long ans = -(1LL << 62); for (int j = 0; j < m; j++) ans = max(dp[n - 1][j], ans); cout << ans; return 0; }
5
#include <bits/stdc++.h> long long x; int a[100100], b[100100], c[100100], dd[100100], bb[100100], c1[100100], c2[100100], kk[100100], n, d, i, z, xx, j, t1, t2, k, bk, ck; int getNextX() { x = (x * 37 + 10007) % 1000000007; return (int)x; } void initAB() { for (i = 0; i < n; i++) a[i] = i + 1; for (i = 0; i < n; i++) { xx = getNextX() % (i + 1); z = a[i], a[i] = a[xx], a[xx] = z; } for (i = 0; i < n; i++) b[i] = i < d; for (i = 0; i < n; i++) { xx = getNextX() % (i + 1); z = b[i], b[i] = b[xx], b[xx] = z; } } void del(int j) { if (!c[j] && b[j - dd[i]]) { --ck; c[j] = i + 1; t1 = c1[j], t2 = c2[j]; if (t1 < n) c2[t1] = c2[j]; if (~t2) c1[t2] = c1[j]; } } void write(int c) { int a[6], k = 0; do { a[k++] = c % 10; c /= 10; } while (c); while (k--) putchar(a[k] + 48); putchar('\n'); } int main() { scanf("%d%d%I64d", &n, &d, &x); initAB(); for (i = 0; i < n; i++) dd[a[i] - 1] = i; for (i = 0; i < n; i++) if (b[i]) bb[bk++] = i; for (i = 0; i < n; i++) c1[i] = i + 1, c2[i] = i - 1; ck = n; for (i = n - 1; ~i; i--) { j = dd[i]; k = 0; while (j < n && c[j]) { kk[k++] = j; j = c1[j]; } for (t1 = 0; t1 < k; t1++) c1[kk[t1]] = j; if (bk < ck) { for (j = 0; j < bk && bb[j] + dd[i] < n; j++) if (!c[bb[j] + dd[i]]) del(bb[j] + dd[i]); } else { for (j = dd[i]; j < n; j = c1[j]) if (!c[j] && b[j - dd[i]]) del(j); } } for (i = 0; i < n; i++) write(c[i]); return 0; }
2
#include <bits/stdc++.h> int a[105], stor[105]; int main() { int n; scanf("%d", &n); int i; for (i = 0; i < n; i++) scanf("%d", &a[i]); int j, k = 0, max; max = a[2] - a[0]; for (i = 1; i < n - 1; i++) { if (i == 1) { max = a[2] - a[0]; for (j = 2; j < n - 1; j++) { if (a[j + 1] - a[j] > max) max = a[j + 1] - a[j]; } } else { max = a[1] - a[0]; for (j = 0; j < n; j++) { if (j == i) { if (a[j + 1] - a[j - 1] > max) max = a[j + 1] - a[j - 1]; } if (j + 1 == i) { if (a[j + 2] - a[j] > max) max = a[j + 2] - a[j]; } else if (a[j + 1] - a[j] > max) max = a[j + 1] - a[j]; } } stor[k++] = max; } int min = stor[0]; for (i = 0; i < k; i++) { if (stor[i] < min) min = stor[i]; } printf("%d", min); printf("\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; void Go() { long long int L, R; cin >> L >> R; if (2 * L <= R) cout << L << " " << 2 * L << '\n'; else cout << "-1 -1\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; cin >> t; while (t--) { Go(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a; int len, res; b = a; len = a.length(); a += a; a.erase(0, 1); res = a.find(b); cout << res + 1; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long segt[((long long)1 << 19) + 5]; long long constr(long long l, long long r, long long mas[], long long pos) { if (l == r) { segt[pos] = mas[l]; return 1; } else { long long mid = (l + r) / 2; long long bij = constr(l, mid, mas, pos * 2 + 1); constr(mid + 1, r, mas, pos * 2 + 2); if (bij % 2) segt[pos] = (segt[pos * 2 + 1] | segt[pos * 2 + 2]); else segt[pos] = (segt[pos * 2 + 1] ^ segt[pos * 2 + 2]); return bij + 1; } } long long upd(long long l, long long r, long long atr, long long val, long long pos) { if (r < atr || l > atr) return -1; if (l == r) { segt[pos] = val; return 1; } else { long long mid = (l + r) / 2; long long bij = upd(l, mid, atr, val, pos * 2 + 1); long long bij2 = upd(mid + 1, r, atr, val, pos * 2 + 2); if (bij == -1) swap(bij, bij2); if (bij % 2) segt[pos] = (segt[pos * 2 + 1] | segt[pos * 2 + 2]); else segt[pos] = (segt[pos * 2 + 1] ^ segt[pos * 2 + 2]); return bij + 1; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; n = ((long long)1 << n); long long mas[n]; for (long long i = 0; i < n; i++) cin >> mas[i]; constr(0, n - 1, mas, 0); while (m--) { long long atr, val; cin >> atr >> val; atr--; upd(0, n - 1, atr, val, 0); cout << segt[0] << "\n"; } return 0; }
4
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+10; struct node{ int x,y; node(int a,int b):x(a),y(b){} }; int vis[maxn],f[maxn],l[maxn]; int main(){ int n,k; scanf("%d%d",&n,&k); scanf("%d",&f[1]); for(int i=2;i<=n;++i){ int x; scanf("%d",&x); f[i]=x; vis[x]++; } int ans=0; if(f[1]!=1)ans++; queue<int> q; for(int i=1;i<=n;++i){ if(!vis[i])q.push(i),l[i]=0; } while(!q.empty()){ int v=q.front(),h=l[v];q.pop(); if(h==k-1&&f[v]!=1){ ans++; h=-1; } if(f[v]!=0&&f[v]!=1){ l[f[v]]=max(l[f[v]],h+1); vis[f[v]]--; if(vis[f[v]]==0)q.push(f[v]); } } cout<<ans<<endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int sx,sy,tx,ty; cin>>sx>>sy>>tx>>ty; cout<<string(tx-sx,'R')+string(ty-sy,'U')+string(tx-sx,'L')+string(ty-sy+1,'D')+string(tx-sx+1,'R')+string(ty-sy+1,'U')+'L'+'U'+string(tx-sx+1,'L')+string(ty-sy+1,'D')+'R'<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { vector<int> a, distinct; int x, n; int space; cin >> n >> space; for (int i = 0; i < n; ++i) { cin >> x; a.push_back(x); } distinct.resize(n); space *= 8; int reqd = space / n; if (reqd > 30 or n <= (1 << reqd)) { cout << 0 << '\n'; return 0; } int shouldBe = 1 << reqd; sort(a.begin(), a.end()); distinct[0] = 1; for (int i = 1; i < n; ++i) { if (a[i] == a[i - 1]) distinct[i] = distinct[i - 1]; else distinct[i] = distinct[i - 1] + 1; } int mini = -1; for (int i = 0; i < n; ++i) { if (i and a[i] == a[i - 1]) continue; int up = lower_bound(distinct.begin(), distinct.end(), (i ? distinct[i - 1] : 0) + shouldBe + 1) - distinct.begin(); auto current = i + n - up; if (current < mini or mini == -1) mini = current; } cout << mini << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; char aaa[33][33] = { 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1}; int main() { int xxx, yyy; cin >> xxx >> yyy; cout << (int)aaa[xxx][yyy] << endl; return 0; }
2
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<<endl #define smax(x,y) (x)=max((x),(y)) #define smin(x,y) (x)=min((x),(y)) #define MEM(x,y) memset((x),(y),sizeof (x)) #define sz(x) (int)(x).size() #define RT return using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; void solve() { ll ma[2][3], mi[2][3]; rep(i, 2)rep(j, 3) { ma[i][j] = LLONG_MIN / 3; mi[i][j] = LLONG_MAX / 3; } auto put = [&](int i, int j, ll z) { smax(ma[i][j], z); smin(mi[i][j], z); }; int N; cin >> N; rep(i, N) { ll x, y; char c; cin >> x >> y >> c; x *= 2; y *= 2; if (c == 'R') { put(0, 0, x); put(1, 2, y); } else if (c == 'L') { put(0, 1, x); put(1, 2, y); } else if (c == 'U') { put(0, 2, x); put(1, 0, y); } else { put(0, 2, x); put(1, 1, y); } } ll ans = LLONG_MAX; rep(t, 100000001) { static ll A[2]; rep(i, 2) { ll large = max({ ma[i][0] + t, ma[i][1] - t, ma[i][2] }); ll small = min({ mi[i][0] + t, mi[i][1] - t, mi[i][2] }); A[i] = large - small; } smin(ans, A[0] * A[1]); } cout << ans / 4.0 << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int oo = 1000000000; int n1, n2, xx, yy, m, r, b, Lid[810], Rid[810], idt, S, T, SS, TT, et, be[810], lows, erid[331000], ebid[331000], ansc, answ; char s[810], ans[810]; struct edg { int v, c, w, ne; } e[331000]; inline void adde(int u, int v, int c, int w) { e[++et].v = u; e[et].c = 0; e[et].w = -w; e[et].ne = be[v]; be[v] = et; e[++et].v = v; e[et].c = c; e[et].w = w; e[et].ne = be[u]; be[u] = et; } inline void add_edge(int u, int v, int l, int r, int w) { lows += l; if (l) { adde(SS, v, l, 0); adde(u, TT, l, 0); } adde(u, v, r - l, w); } int a[810], d[810], lase[810], lasv[810]; queue<int> q; bool inq[810]; bool spfa() { for (int i = 1; i <= idt; i++) d[i] = oo; d[SS] = 0; a[SS] = oo; q.push(SS); inq[SS] = 1; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; for (int i = be[u]; i; i = e[i].ne) if (e[i].c && d[u] + e[i].w < d[e[i].v]) { d[e[i].v] = d[u] + e[i].w; a[e[i].v] = a[u]; if (e[i].c < a[e[i].v]) a[e[i].v] = e[i].c; lase[e[i].v] = i; lasv[e[i].v] = u; if (!inq[e[i].v]) { q.push(e[i].v); inq[e[i].v] = 1; } } } if (d[TT] == oo) return 0; int u = TT; while (u != SS) { int &i = lase[u]; e[i].c -= a[TT]; e[i ^ 1].c += a[TT]; u = lasv[u]; } return 1; } void mcmf() { while (spfa()) { ansc += a[TT]; answ += a[TT] * d[TT]; } } int main() { scanf("%d%d%d%d%d", &n1, &n2, &m, &r, &b); for (int i = 1; i <= n1; i++) Lid[i] = ++idt; for (int i = 1; i <= n2; i++) Rid[i] = ++idt; S = ++idt; T = ++idt; SS = ++idt; TT = ++idt; et = 1; add_edge(T, S, 0, oo, 0); scanf("%s", s + 1); for (int i = 1; i <= n1; i++) { if (s[i] == 'R') { add_edge(S, Lid[i], 1, oo, 0); continue; } if (s[i] == 'B') { add_edge(Lid[i], T, 1, oo, 0); continue; } add_edge(S, Lid[i], 0, oo, 0); add_edge(Lid[i], T, 0, oo, 0); } scanf("%s", s + 1); for (int i = 1; i <= n2; i++) { if (s[i] == 'B') { add_edge(S, Rid[i], 1, oo, 0); continue; } if (s[i] == 'R') { add_edge(Rid[i], T, 1, oo, 0); continue; } add_edge(S, Rid[i], 0, oo, 0); add_edge(Rid[i], T, 0, oo, 0); } for (int i = 1; i <= m; i++) { scanf("%d%d", &xx, &yy); add_edge(Lid[xx], Rid[yy], 0, 1, r); erid[i] = et; add_edge(Rid[yy], Lid[xx], 0, 1, b); ebid[i] = et; } mcmf(); if (ansc != lows) { printf("-1\n"); return 0; } for (int i = 1; i <= m; i++) { if (!e[erid[i]].c && e[ebid[i]].c) { ans[i] = 'R'; continue; } if (e[erid[i]].c && !e[ebid[i]].c) { ans[i] = 'B'; continue; } ans[i] = 'U'; } printf("%d\n%s\n", answ, ans + 1); return 0; }
6
#include <bits/stdc++.h> const double pi = 3.1415926535897932384626433832795; bool test(long long x1, long long y1, long long x2, long long y2, long long x3, long long y3) { long long vx1 = x2 - x1; long long vy1 = y2 - y1; long long vx2 = x3 - x1; long long vy2 = y3 - y1; return ((vx1 * vx2 + vy1 * vy2) > 0); } long long vc(long long x1, long long y1, long long x2, long long y2) { return (x1 * y2 - x2 * y1); } using namespace std; int main() { int n, i; long long x0, y0, x, y, xp, yp, xf, yf; double rmx = 0, rmn = 1000000000; cin >> n >> x0 >> y0; cin >> xf >> yf; xp = xf; yp = yf; for (i = 0; i < n - 1; i++) { cin >> x >> y; rmx = max(rmx, hypot(x - x0, y - y0)); rmn = min(rmn, hypot(x - x0, y - y0)); if (test(xp, yp, x, y, x0, y0) && test(x, y, xp, yp, x0, y0)) rmn = min(rmn, abs(vc(x - x0, y - y0, xp - x0, yp - y0)) / (hypot(x - xp, y - yp))); xp = x; yp = y; } x = xf; y = yf; rmx = max(rmx, hypot(x - x0, y - y0)); rmn = min(rmn, hypot(x - x0, y - y0)); if (test(xp, yp, x, y, x0, y0) && test(x, y, xp, yp, x0, y0)) rmn = min(rmn, abs(vc(x - x0, y - y0, xp - x0, yp - y0)) / (hypot(x - xp, y - yp))); cout.precision(30); cout << pi * (rmx * rmx - rmn * rmn); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, p; bool ok(long long a, int b) { if (a < 0) return 0; if (b > a) return 0; while (a) { if (a % 2) b--; a /= 2; } return b >= 0; } int main() { scanf("%lld%lld", &n, &p); for (int i = 0; i < 100; i++, n -= p) if (ok(n, i)) { printf("%d\n", i); return 0; } puts("-1"); }
1
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e6 + 10; const long long inf = 1e15; long long mod = 1e9 + 7, fac[MAX], a[MAX], b[MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; long long ans = 0; long long x = 0, y = 0; cin >> n >> m; for (long long i = 0; i < n; i++) cin >> a[i], x += a[i]; for (long long i = 0; i < m; i++) cin >> b[i], y += b[i]; sort(a, a + n); sort(b, b + m); long long xx = x, yy = y; for (long long i = 0; i < m - 1; i++) xx += min(x, b[i]); for (long long i = 0; i < n - 1; i++) yy += min(y, a[i]); cout << min(xx, yy) << endl; }
2
#include <bits/stdc++.h> using namespace std; string str[10]; int main() { str[0] = "O-|-OOOO"; str[1] = "O-|O-OOO"; str[2] = "O-|OO-OO"; str[3] = "O-|OOO-O"; str[4] = "O-|OOOO-"; str[5] = "-O|-OOOO"; str[6] = "-O|O-OOO"; str[7] = "-O|OO-OO"; str[8] = "-O|OOO-O"; str[9] = "-O|OOOO-"; string input; cin >> input; for (long long i = input.length() - 1; i >= 0; i--) { cout << str[input[i] - '0'] << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { string a; string x[2]; x[0] = "aa"; x[1] = "bb"; for (int i = 0; 2 * i < n; i++) a += x[i % 2]; string b = a.substr(2); for (int i = 0; i < b.size(); ++i) b[i] += 2; string c = b; for (int i = 0; i < b.size(); ++i) c[i] += 2; cout << a << endl; cout << 'x' << b << 'z' << endl; cout << 'x' << c << 'z' << endl; cout << a << endl; } else { n -= 2; string a; string x[2]; x[0] = "aa"; x[1] = "bb"; for (int i = 0; 2 * i < n; i++) a += x[i % 2]; string b = a; for (int i = 0; i < b.size(); ++i) b[i] += 2; cout << a << 'z' << endl; cout << b << 'z' << endl; cout << 'x' << a << endl; cout << 'x' << b << endl; } return 0; }
1
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; int k,cnt=0; cin >> k; while(a>=b){ b*=2; cnt++; } while(b>=c){ c*=2; cnt++; } if(cnt>k) cout << "No\n"; else cout << "Yes\n"; }
0
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } int a[100010][10], b[100010][10]; int main() { int m, n; cin >> m >> n; for (int i = 1; i <= m; i++) b[i][0] = 0; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) { b[0][j] = 0; scanf("%d", &a[i][j]); } for (int j = 1; j <= n; j++) for (int i = 1; i <= m; i++) { b[i][j] = max(b[i - 1][j], b[i][j - 1]) + a[i][j]; if (j == n) printf("%d ", b[i][n]); } }
2
#include <bits/stdc++.h> using namespace std; int main() { int x, y; while (~scanf("%d%d", &x, &y)) { double z = sqrt(x * x * 1.0 + y * y * 1.0); int k = (int)z; if (x > 0 && y > 0 || x < 0 && y < 0) { if (z == k) printf("black\n"); else { if (k & 1) printf("white\n"); else printf("black\n"); } } else { if (z == k) printf("black\n"); else { if (k & 1) printf("black\n"); else printf("white\n"); } } } }
1
#include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using namespace std; int n; int in[5100]; unordered_set<int> vs; int Solve(int x, int y) { int d = y - x; if (vs.count(x-d) > 0) return 0; int r = 1; for (int x = y; vs.count(x) > 0; x += d) { r++; } return r; } int main() { while (cin >> n) { vs.clear(); for (int i = 0; i < n; ++i) { cin >> in[i]; vs.insert(in[i]); } sort(in, in+n); int ans = 0; for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { int r = Solve(in[i], in[j]); // cout << iter.first << " / " << r << endl; ans = max(ans, r); } } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; #define N 200050 int a[N]; int main(){ int n, k; scanf("%d%d", &n, &k); for(int i = 0; i < n; i ++) scanf("%d", &a[i]); for(int i = k; i < n; i ++){ if(a[i] > a[i - k]) puts("Yes"); else puts("No"); } }
0
#include <bits/stdc++.h> using namespace std; int ts, kk = 1, _DB = 1; inline int LEN(string a) { return a.length(); } inline int LEN(char a[]) { return strlen(a); } template <class T> inline T _abs(T n) { return (n < 0 ? -n : n); } template <class T> inline T _max(T a, T b) { return (a > b ? a : b); } template <class T> inline T _min(T a, T b) { return (a < b ? a : b); } template <class T> inline T _sq(T x) { return x * x; } template <class T> inline T _sqrt(T x) { return (T)sqrt((double)x); } template <class T> inline T _pow(T x, T y) { T z = 1; for (int i = 1; i <= y; i++) { z *= x; } return z; } template <class T> inline T _gcd(T a, T b) { a = _abs(a); b = _abs(b); if (!b) return a; return _gcd(b, a % b); } template <class T> inline T _lcm(T a, T b) { a = _abs(a); b = _abs(b); return (a / _gcd(a, b)) * b; } template <class T> inline T _extended(T a, T b, T &x, T &y) { a = _abs(a); b = _abs(b); T g, x1, y1; if (!b) { x = 1; y = 0; g = a; return g; } g = _extended(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return g; } template <class T> inline T getbit(T x, T i) { T t = 1; return (x & (t << i)); } template <class T> inline T setbit(T x, T i) { T t = 1; return (x | (t << i)); } template <class T> inline T resetbit(T x, T i) { T t = 1; return (x & (~(t << i))); } template <class T> inline T _bigmod(T n, T m) { T ans = 1, mult = n % 1000000007; while (m) { if (m & 1) ans = (ans * mult) % 1000000007; m >>= 1; mult = (mult * mult) % 1000000007; } ans %= 1000000007; return ans; } template <class T> inline T _modinv(T x) { return _bigmod(x, (T)(1000000007 - 2)) % 1000000007; } int n; int a[6][2] = {{3, 0}, {15, 1}, {81, 2}, {6723, 0}, {50625, 3}, {881920, 1}}; inline int grundy(double x) { x = sqrt(x); int high = (int)floor(x); x = sqrt(x); int low = (int)ceil(x); int srt, ed; srt = ed = 2000000000; for (int i = 0; i <= 5; i++) { if (low <= a[i][0]) srt = _min(srt, i); if (high <= a[i][0]) ed = _min(ed, i); } int ret = 0; set<int> st; for (int i = srt; i <= ed; i++) st.insert(a[i][1]); for (set<int>::iterator it = st.begin(); it != st.end(); it++, ret++) if (*it != ret) break; return ret; } int main() { cin >> n; int ans = 0; while (n--) { long long x; cin >> x; bool chk = false; for (int i = 0; i <= 5; i++) if (x <= a[i][0]) { ans ^= a[i][1]; chk = true; break; } if (!chk) ans ^= grundy((double)x); } if (ans) puts("Furlo"); else puts("Rublo"); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { vector<int> cnt(256, 0); string s; cin >> s; for (auto c : s) cnt[c]++; vector<int> odds; for (char i = 'a'; i <= 'z'; ++i) if (cnt[i] % 2 == 1) odds.push_back(i); int K = odds.size(); for (int i = 0; i < K / 2; ++i) cnt[odds[i]]++, cnt[odds[K - i - 1]]--; for (char i = 'a'; i <= 'z'; ++i) for (int j = 0; j < cnt[i] / 2; ++j) cout << i; for (char i = 'a'; i <= 'z'; ++i) if (cnt[i] % 2 == 1) cout << i; for (char i = 'z'; i >= 'a'; --i) for (int j = 0; j < cnt[i] / 2; ++j) cout << i; cout << endl; }
3
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 7; const int MOD = 1e9 + 7; const int dx[4] = {0, 0, -1, 1}; const int dy[4] = {-1, 1, 0, 0}; const int N = 4e5 + 1; int n, m, p; pair<int, int> a[N], b[N]; struct Data { int x, y, z; Data(int x, int y, int z) : x(x), y(y), z(z) {} }; vector<Data> monster; bool comp(Data A, Data B) { return A.x < B.x; } struct SegmentTree { int lazy[4 * N], val[4 * N]; void Down(int i) { if (lazy[i] == 0) return; lazy[i * 2] += lazy[i]; lazy[i * 2 + 1] += lazy[i]; val[i] += lazy[i]; lazy[i] = 0; } void Update(int u, int v, int cost, int i = 1, int l = 1, int r = m) { Down(i); if (u > r || v < l) return; if (u <= l && r <= v) { lazy[i] += cost; Down(i); return; } Update(u, v, cost, i * 2, l, (l + r) / 2); Update(u, v, cost, i * 2 + 1, (l + r) / 2 + 1, r); val[i] = max(val[i * 2], val[i * 2 + 1]); } } seg; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> p; for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; for (int i = 1; i <= m; i++) cin >> b[i].first >> b[i].second; sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + m); for (int i = 1; i <= p; i++) { int x, y, z; cin >> x >> y >> z; monster.push_back(Data(x, y, z)); } for (int i = 1; i <= m; i++) seg.Update(i, i, -b[i].second); sort(monster.begin(), monster.end(), comp); int ptr = 0, res = -INF; for (int i = 1; i <= n; i++) { while (ptr < p && monster[ptr].x < a[i].first) { int k = lower_bound(b + 1, b + 1 + m, pair<int, int>(monster[ptr].y, INF)) - b; if (k != m + 1) { seg.Update(k, m, monster[ptr].z); } ptr++; } res = max(res, -a[i].second + seg.val[1]); } cout << res; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { (ios_base::sync_with_stdio(false), cin.tie(nullptr)); int t; t = 1; while (t--) { int n, m; string s; cin >> n >> m >> ws; map<string, int> mp; for (int i = 0; i < n; i++) { getline(cin, s); mp[s]++; } int common = 0; for (int i = 0; i < m; i++) { getline(cin, s); if (mp[s] > 0) common++; } if (common % 2 != 0) { if (n > m - 1) cout << "YES" << "\n"; else cout << "NO" << "\n"; } else { if (n > m) cout << "YES" << "\n"; else cout << "NO" << "\n"; } } return EXIT_SUCCESS; }
2
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long n; cin >> n; long long ans = 0; for (long long p2 = 1; p2 <= n; p2 *= 2) { ans += n / p2; if (p2 > LLONG_MAX / 2) break; } cout << ans << "\n"; } }
3
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <complex> #include <queue> #include <set> #include <map> #include <assert.h> using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) typedef long long ll; string parse_target; string::const_iterator iter, last; bool consume(char expected) { if(*iter == expected) { iter++; return true; } else { assert(false); } } bool consume_if(char expected) { if(*iter == expected) { consume(expected); return true; } return false; } void debugger() { for(auto tmp = iter;tmp!=last;tmp++) cout << *tmp; cout << endl; } double number() { string str; for(; isdigit(*iter) || *iter=='-'; iter++) { str += *iter; } return stod(str); } typedef complex<double> Point; typedef vector<Point> Line; enum { undefined_style, line_style, point_style }; struct result_type { int style_; Line line_; Point point_; result_type(Point const& a, Point const& b): style_(line_style), point_() { line_.push_back(a), line_.push_back(b); } result_type(Point p): style_(point_style), line_(), point_(p) {} result_type(double x, double y): style_(point_style), line_(), point_(x, y) {} result_type(): style_(undefined_style), line_(), point_() {} }; double dot(Point const& a, Point const& b) { return real(conj(a)*b); } double cross(Point const& a, Point const& b) { return imag(conj(a)*b); } Point intersection(Line const& a, Line const& b) { Point va = a[1]-a[0], vb = b[1]-b[0]; return a[0] + va * cross(vb, b[0]-a[0]) / cross(vb, va); } Point projection_point(Line const& l, Point const& p) { double k = dot(l[1]-l[0], p-l[0]) / norm(l[1]-l[0]); return l[0]+k*(l[1]-l[0]); } Point reflection_point(Line const& l, Point const& p) { return p + 2.*(projection_point(l, p) - p); } result_type make_symmetric_point(result_type line, result_type p) { if(line.style_ == point_style) swap(line, p); return result_type(reflection_point(line.line_, p.point_)); } result_type rec(); result_type term() { result_type ret; if((*iter == '(') && (isdigit(*(iter+1)) || *(iter+1)=='-')) { consume('('); auto lhs = number(); consume(','); auto rhs = number(); consume(')'); return {lhs, rhs}; } else if(consume_if('(')) { auto expr = rec(); consume(')'); return expr; } else { if(ret.style_ == undefined_style) ret = rec(); consume(')'); return ret; } } result_type rec() { result_type ret = term(); for(;iter<last;) { if(consume_if('@')) { auto rhs = term(); if(ret.style_ == point_style && rhs.style_ == point_style) ret = {ret.point_, rhs.point_}; else if(ret.style_ == line_style && rhs.style_ == line_style) ret = intersection(ret.line_, rhs.line_); else ret = make_symmetric_point(ret, rhs); } else { break; } } return ret; } int main() { for(;getline(cin, parse_target);) { if(parse_target == "#") { break; } iter = parse_target.begin(), last = parse_target.end(); auto res = rec(); printf("%.8f %.8f\n", res.point_.real(), res.point_.imag()); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ int n, t, ci, ti, r=1001; cin>>n>>t; while(n--){ cin>>ci>>ti; if(ti<=t && ci<r){ r=ci; } } r<1001?cout<<r:cout<<"TLE"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxint = 2147483647; const int minint = -2147483647; const unsigned int maxunint = 4294967295; const int N = 200000; int h[5005]; int f(int l, int r, int p) { int i, minh = h[l], m = l; for (i = l; i <= r; i++) { if (h[i] < minh) { minh = h[i]; m = i; } } int ans = minh - p; if (m > l) ans += f(l, m - 1, minh); if (m < r) ans += f(m + 1, r, minh); if (ans > r - l + 1) ans = r - l + 1; return ans; } int main() { int n, i; cin >> n; for (i = 0; i < n; ++i) cin >> h[i]; cout << f(0, n - 1, 0); return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; long long ans = 0; long long a[500005]; int c[500005]; long long bit[500005]; vector<long long> b; void add(int k, long long x) { while (k <= n) { bit[k] = (bit[k] + x) % 1000000007; k += k & (-k); } } long long query(int k) { long long sum = 0; while (k > 0) { sum = (sum + bit[k]) % 1000000007; k -= k & (-k); } return sum; } int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); ans = (ans + a[i] * i % 1000000007 * (n - i + 1) % 1000000007) % 1000000007; b.push_back(a[i]); } sort(b.begin(), b.end()); for (int i = 1; i <= n; i++) { c[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin() + 1; } for (int i = 1; i <= n; i++) { long long now = query(c[i]) * (n - i + 1) % 1000000007 * a[i] % 1000000007; ans = (ans + now) % 1000000007; add(c[i], (long long)i); } memset(bit, 0, sizeof(bit)); for (int i = n; i >= 1; i--) { long long now = query(c[i]) * i % 1000000007 * a[i] % 1000000007; ans = (ans + now) % 1000000007; add(c[i], (long long)n - i + 1); } cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> size_t gcd(size_t a, size_t b) { while (a && b) { size_t rem = a % b; a = b; b = rem; } return a + b; } long solve(std::vector<size_t> const &len, std::vector<size_t> const &cst) { std::map<size_t, size_t> dt; for (size_t i = 0; i != len.size(); ++i) if (dt.find(len[i]) == dt.end()) dt[len[i]] = cst[i]; else dt[len[i]] = std::min(cst[i], dt[len[i]]); for (size_t i = 0; i != len.size(); ++i) { typename std::map<size_t, size_t>::iterator const end = dt.end(); for (typename std::map<size_t, size_t>::iterator it = dt.begin(); it != end; ++it) { size_t const div = gcd(len[i], it->first); size_t const cost = cst[i] + it->second; if (dt.find(div) == dt.end()) dt[div] = cost; else dt[div] = std::min(dt[div], cost); } } typename std::map<size_t, size_t>::iterator const it = dt.find(1); return it == dt.end() ? -1 : it->second; } int main() { size_t n; std::cin >> n; std::vector<size_t> len(n, 0); std::vector<size_t> cst(n, 0); for (size_t i = 0; i != n; ++i) std::cin >> len[i]; for (size_t i = 0; i != n; ++i) std::cin >> cst[i]; std::cout << solve(len, cst) << std::endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main(){ int N;cin>>N; int P[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; int L[15];int n; for(int i=0;i<15;i++){ n=N;L[i]=0; while(n>0){ n=n/P[i]; L[i]+=n; } }int id[]={74,24,14,4,2}; int j=0,temp=0; while(j<17&&temp<5){ if(L[j]>=id[temp]){ j++; }else{ id[temp]=j; temp++; } }int Sum=id[0]+id[1]*(id[4]-1)+id[2]*(id[3]-1)+id[3]*(id[3]-1)*(id[4]-2)/2; cout<<Sum<<endl; }
0
#include<iostream> using namespace std; int main(){ string s; cin >> s; int x=0; if(s[0]=='R') x++; if(s[1]=='R') x++; else{ x=0; } if(s[2]=='R') x++; if(s[0]=='R' && x==0) x++; cout << x << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; vector<vector<int>> b(4, vector<int>(4)); string s; cin >> s; int n = s.length(), i, j, k, l; for (i = 0; i < n; i++) { if (s[i] == '0') { for (k = 0; k < 4; k++) { for (j = 0; j < 3; j++) if (b[j][k] == 0 && b[j + 1][k] == 0) break; if (j < 3) { b[j][k] = 1, b[j + 1][k] = 1; break; } } } else { for (j = 0; j < 4; j++) { for (k = 0; k < 3; k++) if (b[j][k] == 0 && b[j][k + 1] == 0) break; if (k < 3) { b[j][k] = 1, b[j][k + 1] = 1; break; } } } cout << j + 1 << " " << k + 1 << endl; vector<int> rows, col; for (j = 0; j < 4; j++) { for (k = 0; k < 4; k++) if (b[j][k] == 0) break; if (k == 4) rows.push_back(j); } for (k = 0; k < 4; k++) { for (j = 0; j < 4; j++) if (b[j][k] == 0) break; if (j == 4) col.push_back(k); } for (auto x : rows) for (j = 0; j < 4; j++) b[x][j] = 0; for (auto x : col) for (j = 0; j < 4; j++) b[j][x] = 0; } return 0; }
1
#include <bits/stdc++.h> using namespace std; char s[100010]; char word[100010]; int num[100010]; int change() { int len = strlen(word); if (len < 3) return 0; if (strcmp(word + len - 4, "lios") == 0) return 1; if (strcmp(word + len - 5, "liala") == 0) return -1; if (strcmp(word + len - 3, "etr") == 0) return 2; if (strcmp(word + len - 4, "etra") == 0) return -2; if (strcmp(word + len - 6, "initis") == 0) return 3; if (strcmp(word + len - 6, "inites") == 0) return -3; return 0; } int main() { while (gets(s)) { int tp = 0, flag = 1, i, len = strlen(s), t, p = 0, cnt = 0; for (i = 0; i <= len; i++) { if (s[i] != ' ' && s[i] != '\0') { word[p++] = s[i]; } else { word[p] = '\0'; t = change(); cnt++; if (t != 0) num[tp++] = t; else { flag = 0; break; } } } if (cnt == 1 && flag == 1) { puts("YES"); continue; } if (flag == 0) { puts("NO"); continue; } cnt = 0; for (i = 0; i < tp; i++) if (abs(num[i]) == 2) cnt++; if (cnt != 1) { puts("NO"); continue; } flag = 1; if (num[0] < 0) { for (i = 1; i < tp; i++) if (num[i] > 0) { flag = 0; break; } } else { for (i = 1; i < tp; i++) if (num[i] < 0) { flag = 0; break; } } if (flag == 0) { puts("NO"); continue; } flag = 1; for (i = 1; i < tp; i++) { if (abs(num[i]) < abs(num[i - 1])) { flag = 0; break; } } if (flag) puts("YES"); else puts("NO"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int t, p = 0, c = 0, l; long long int i, n, k; cin >> n >> t; long long int a[200001]; for (i = 0; i < n; i++) { cin >> a[i]; p += a[i]; if (i == 0) k = a[i]; else k = min(k, a[i]); } c = t / p * n; t = t % p; while (t >= k) { long long int d = 0, e = 0; for (i = 0; i < n; i++) { if (t >= a[i]) { t -= a[i]; d++; e += a[i]; } } c = c + d + t / e * d; t %= e; } cout << c; }
4
#include <bits/stdc++.h> using namespace std; int n, x, a[3]; int main() { scanf("%d%d", &n, &x); n = n % 6; switch (n) { case 0: a[0] = 0; a[1] = 1; a[2] = 2; break; case 1: a[0] = 1; a[1] = 0; a[2] = 2; break; case 2: a[0] = 1; a[1] = 2; a[2] = 0; break; case 3: a[0] = 2; a[1] = 1; a[2] = 0; break; case 4: a[0] = 2; a[1] = 0; a[2] = 1; break; case 5: a[0] = 0; a[1] = 2; a[2] = 1; break; } printf("%d", a[x]); return 0; }
1
#include <bits/stdc++.h> typedef long long LL; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) using namespace std; class UnionFind { std::vector<int> p; public: UnionFind(int n) : p(n, -1) {} int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); } bool same(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (p[y] < p[x]) std::swap(x, y); if (p[x] == p[y]) --p[x]; p[y] = x; return true; } }; int main(void) { LL n; cin >> n; vector<pair<LL,LL>> x(n),y(n); UnionFind uf(n); REP(i,n) cin >> x[i].first >> y[i].first; REP(i,n) x[i].second=y[i].second=i; sort(x.begin(),x.end()); sort(y.begin(),y.end()); vector<tuple<LL,LL,LL>> cost; REP(i,n-1) { cost.emplace_back(x[i+1].first-x[i].first,x[i].second,x[i+1].second); cost.emplace_back(y[i+1].first-y[i].first,y[i].second,y[i+1].second); } sort(cost.begin(),cost.end()); LL answer=0ll; for(auto t:cost) { LL c,l,r; tie(c,l,r)=t; if(uf.unite(l,r)) answer+=c; } cout << answer << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long rand(long long l, long long r) { uniform_int_distribution<long long> uid(l, r); return uid(rng); } const int N = 5e5 + 5; bool vis[N]; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s, t; cin >> s >> t; string s1 = s; string t1 = t; sort(s1.begin(), s1.end()); sort(t1.begin(), t1.end()); if (s1 != t1) { cout << "NO" << endl; continue; } bool done = 0; for (int i = 0; i <= n - 2; i++) { if (s1[i] == s1[i + 1]) { done = 1; } if (t1[i] == t1[i + 1]) { done = 1; } } if (done) { cout << "YES" << endl; continue; } int x1 = 0, x2 = 0; for (int k = 0; k <= n; k++) { for (int i = 0; i <= n - 2; i++) { if (s[i] > s[i + 1]) { swap(s[i], s[i + 1]); x1++; } } } for (int k = 0; k <= n; k++) { for (int i = 0; i <= n - 2; i++) { if (t[i] > t[i + 1]) { swap(t[i], t[i + 1]); x2++; } } } if (abs(x1 - x2) & 1) { cout << "NO" << endl; } else { cout << "YES" << endl; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; #define EACH(i,a) for (auto& i : a) #define FOR(i,a,b) for (ll i=(a);i<(b);i++) #define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--) #define REP(i,n) for (ll i=0;i<(n);i++) #define RREP(i,n) for (ll i=(n)-1;i>=0;i--) #define debug(x) cout<<#x<<": "<<x<<endl #define pb push_back #define ALL(a) (a).begin(),(a).end() const ll linf = 1e18; const int inf = 1e9; const double eps = 1e-12; const double pi = acos(-1); template<typename T> istream& operator>>(istream& is, vector<T>& vec) { EACH(x,vec) is >> x; return is; } template<typename T> ostream& operator<<(ostream& os, vector<T>& vec) { REP(i,vec.size()) { if (i) os << " "; os << vec[i]; } return os; } template<typename T> ostream& operator<<(ostream& os, vector< vector<T> >& vec) { REP(i,vec.size()) { if (i) os << endl; os << vec[i]; } return os; } typedef complex<ld> Point; namespace std { bool operator<(const Point &lhs, const Point &rhs) { if (lhs.real() < rhs.real() - eps) return true; if (lhs.real() > rhs.real() + eps) return false; return lhs.imag() < rhs.imag(); } } Point input_point() { ld x, y; cin >> x >> y; return Point(x, y); } bool eq(ld a, ld b) { return (abs(a - b) < eps); } ld dot(Point a, Point b) { return real(conj(a) * b); } ld cross(Point a, Point b) { return imag(conj(a) * b); } class Line { public: Point a, b; Line () : a(Point(0, 0)), b(Point(0, 0)) {} Line (Point a, Point b) : a(a), b(b) {} }; class Circle { public: Point p; ld r; Circle () : p(Point(0, 0)), r(0) {} Circle (Point p, ld r) : p(p), r(r) {} }; int ccw (Point a, Point b, Point c) { b -= a; c -= a; if (cross(b, c) > eps) return 1; if (cross(b, c) < -eps) return -1; if (dot(b, c) < 0) return 2; if (norm(b) < norm(c)) return -2; return 0; } bool isis_ll (Line l, Line m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); } bool isis_ls (Line l, Line s) { return isis_ll(l, s) && (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps); } bool isis_ss(Line s, Line t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } bool isis_lp (Line l, Point p) { return (abs(cross(l.b - p, l.a - p)) < eps); } bool isis_sp (Line s, Point p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } Point proj (Line l, Point p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } Point is_ll (Line s, Line t) { Point sv = s.b - s.a, tv = t.b - t.a; assert(cross(sv, tv) != 0); return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } ld dist_lp (Line l, Point p) { return abs(p - proj(l, p)); } ld dist_ll (Line l, Line m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls (Line l, Line s) { return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp (Line s, Point p) { Point r = proj(s, p); return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p)); } ld dist_ss (Line s, Line t) { if (isis_ss(s, t)) return 0; return min({dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)}); } vector<Point> is_cc (Circle c1, Circle c2){ vector<Point> res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; ld rs = sqrt(dfr); Point diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * Point(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs)); return res; } vector<Point> is_lc (Circle c, Line l){ vector<Point> res; ld d = dist_lp(l, c.p); if (d < c.r + eps){ ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); Point nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } vector<Point> is_sc(Circle c, Line l){ vector<Point> v = is_lc(c, l), res; for (Point p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<Line> tangent_cp(Circle c, Point p) { vector<Line> ret; Point v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } Point v1 = v * Point(l / d, c.r / d); Point v2 = v * Point(l / d, -c.r / d); ret.push_back(Line(p, p + v1)); if (l < eps) return ret; ret.push_back(Line(p, p + v2)); return ret; } vector<Line> tangent_cc(Circle c1, Circle c2) { vector<Line> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<Line> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { Point v = c2.p - c1.p; v /= abs(v); Point q1 = c1.p + v * Point(0, 1) * c1.r; Point q2 = c1.p + v * Point(0, -1) * c1.r; ret.push_back(Line(q1, q1 + v)); ret.push_back(Line(q2, q2 + v)); } return ret; } typedef vector<Point> Polygon; ld area(const Polygon &p) { ld res = 0; int n = p.size(); REP(j,n) res += cross(p[j], p[(j+1)%n]); return res / 2; } bool is_counter_clockwise (const Polygon &poly) { ld angle = 0; int n = poly.size(); REP(i,n) { Point a = poly[i], b = poly[(i+1)%n], c = poly[(i+2)%n]; angle += arg((c - b) / (b - a)); } return angle > eps; } int is_in_polygon (const Polygon &poly, Point p) { ld angle = 0; int n = poly.size(); REP(i,n) { Point a = poly[i], b = poly[(i+1)%n]; if (isis_sp(Line(a, b), p)) return 1; angle += arg((b - p) / (a - p)); } return eq(angle, 0) ? 0 : 2; } Polygon convex_hull(vector<Point> ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); Polygon ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } Polygon convex_cut(const Polygon &ps, Line l) { int n = ps.size(); Polygon Q; REP(i,n) { Point A = ps[i], B = ps[(i+1)%n]; Line m = Line(A, B); if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) Q.push_back(is_ll(l, m)); } return Q; } void add_point(vector<Point> &ps, Point p) { for (Point q : ps) if (abs(q - p) < eps) return; ps.push_back(p); } typedef int Weight; struct Edge { int from, to; Weight weight; }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &g, int from, int to, Weight weight) { g[from].push_back((Edge){from, to, weight}); } Graph segment_arrangement(const vector<Line> &s, const vector<Point> &p) { int n = p.size(), m = s.size(); Graph g(n); REP(i,m) { vector<pair<ld,int>> vec; REP(j,n) if (isis_sp(s[i], p[j])) vec.emplace_back(abs(s[i].a - p[j]), j); sort(ALL(vec)); REP(j,vec.size()-1) { int from = vec[j].second, to = vec[j+1].second; add_edge(g, from, to, abs(p[from] - p[to])); } } return g; } Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) { int n = p.size(), m = c.size(); Graph g(n); REP(i,m) { vector<pair<ld,int>> vec; REP(j,n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps) vec.emplace_back(arg(c[i].p - p[j]), j); sort(ALL(vec)); REP(j,vec.size()-1) { int from = vec[j].second, to = vec[j+1].second; ld angle = vec[j+1].first - vec[j].first; add_edge(g, from, to, angle * c[i].r); } if (vec.size() >= 2) { int from = vec.back().second, to = vec.front().first; ld angle = vec.front().first - vec.back().first; add_edge(g, from, to, angle * c[i].r); } } return g; } vector<vector<int>> polygon; vector<int> seg2p[1024][1024]; Graph dual_graph(const vector<Line> &s, const vector<Point> &p) { int N = p.size(); polygon.clear(); REP(i,1024) REP(j,1024) seg2p[i][j].clear(); vector<vector<tuple<ld,int,bool>>> tup(N); REP(i,s.size()) { int a = -1, b = -1; REP(j,N) if (abs(s[i].a - p[j]) < eps) a = j; REP(j,N) if (abs(s[i].b - p[j]) < eps) b = j; assert(a >= 0 && b >= 0); tup[a].emplace_back(arg(s[i].b - s[i].a), b, false); tup[b].emplace_back(arg(s[i].a - s[i].b), a, false); } REP(i,N) sort(ALL(tup[i])); REP(i,N) { REP(j,tup[i].size()) { ld angle; int pos = j, from = i, to; bool flag; tie(angle, to, flag) = tup[i][j]; if (flag) continue; vector<int> ps; while (!flag) { ps.push_back(from); get<2>(tup[from][pos]) = true; seg2p[from][to].push_back(polygon.size()); seg2p[to][from].push_back(polygon.size()); angle += pi + eps; if (angle > pi) angle -= 2 * pi; auto it = lower_bound(ALL(tup[to]), make_tuple(angle, 0, false)); if (it == tup[to].end()) it = tup[to].begin(); from = to; tie(angle, to, flag) = *it; pos = it - tup[from].begin(); } polygon.push_back(ps); } } Graph g(polygon.size()); REP(i,N) REP(j,i) { if (seg2p[i][j].size() == 2) { int from = seg2p[i][j][0], to = seg2p[i][j][1]; g[from].push_back((Edge){from, to}); g[to].push_back((Edge){to, from}); } } return g; } const ld zoom = 25; const ld centerX = 6; const ld centerY = 5; void change_color(int r, int g, int b) { fprintf(stderr, "c.strokeStyle = 'rgb(%d, %d, %d)';\n", r, g, b); } int cordx(Point p) { return 400 + zoom * (p.real() - centerX); } int cordy(Point p) { return 400 - zoom * (p.imag() - centerY); } #define cord(p) cordx(p),cordy(p) void draw_point(Point p) { fprintf(stderr, "circle(%d, %d, %d)\n", cord(p), 2); } void draw_segment(Line l) { fprintf(stderr, "line(%d, %d, %d, %d)\n", cord(l.a), cord(l.b)); } void draw_line(Line l) { Point v = l.b - l.a; Line m(l.a - v * Point(1e4, 0), l.b + v * Point(1e4, 0)); fprintf(stderr, "line(%d, %d, %d, %d)\n", cord(m.a), cord(m.b)); } void draw_polygon(const Polygon &p) { int n = p.size(); REP(i,n) draw_segment(Line(p[i], p[(i+1)%n])); } void draw_circle(Circle c) { fprintf(stderr, "circle(%d, %d, %d)\n", cord(c.p), (int)(zoom * c.r)); } struct Jewel { vector<Circle> c; double ml, mr; }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; while (cin >> n, n) { vector<Jewel> v; REP(i, n) { double x, y, r, m; cin >> x >> y >> r >> m; v.pb({{{{x, y}, r}, {{x, y}, m+r}}, r, m+r}); } vector<Line> lines; REP(i, n) FOR(j, i+1, n) { REP(a, 2) REP(b, 2) { vector<Line> kh = tangent_cc(v[i].c[a], v[j].c[b]); EACH(l, kh) lines.pb(l); } } int ans = 0; EACH(l, lines) { int a = 0; REP(i, n) { double len = dist_lp(l, v[i].c[0].p); if (v[i].ml-eps <= len && len <= v[i].mr+eps) { ++a; } } ans = max(ans, a); } if (n == 1) ans = 1; cout << ans << endl; } }
0
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long int64; const int64 MOD = (int64)( 1e9 + 7 ); const int MAX_N = int(1e5); const int MAX_K = int(1e3); struct WarpHole { int sy, sx, gy, gx; WarpHole(){} WarpHole(int sy, int sx, int gy, int gx): sy(sy), sx(sx), gy(gy), gx(gx) {} }; bool operator< (const WarpHole& lhs, const WarpHole& rhs) { return make_pair(lhs.sy, lhs.sx) != make_pair(rhs.sy, rhs.sx) ? make_pair(lhs.sy, lhs.sx) < make_pair(rhs.sy, rhs.sx) : make_pair(lhs.gy, lhs.gx) != make_pair(rhs.gy, rhs.gx); } int N, M, K; int64 fact[2*MAX_N + 1], invFact[2*MAX_N + 1]; WarpHole warps[MAX_K + 1]; bool visited[MAX_K + 1][MAX_K + 1]; int64 memo[MAX_K + 1][MAX_K + 1]; int64 calcPow(int64 x, int64 n, int64 mod = MOD) { int64 ret = 1; for (; n; n >>= 1, x = x * x % mod) if (n&1) { ret = ret * x % mod; } return ret; } int64 getComb(int n, int k) { return fact[n] * invFact[n-k] % MOD * invFact[k] % MOD; } int64 getWay(int y, int x) { if (y < 0 || x < 0) { return 0; } return getComb(y + x, x); } void prepare() { fact[0] = invFact[0] = 1; for (int i = 0; i < 2*MAX_N; ++i) { fact[i+1] = (i + 1) * fact[i] % MOD; invFact[i+1] = calcPow(fact[i+1], MOD - 2, MOD); } } bool init() { scanf("%d%d%d", &N, &M, &K); for (int i = 0; i < K; ++i) { int sy, sx, gy, gx; scanf("%d%d%d%d", &sy, &sx, &gy, &gx); warps[i] = WarpHole(sy,sx,gy,gx); } memset(visited, false, sizeof(visited)); return N > 0; } //[0,k)までのワープホールが機能しているときの、 //(1,1)からt番目のワープホールの入り口までの経路の個数を返す int64 rec(const int k, const int t) { //memorize if (visited[k][t]) { return memo[k][t]; } visited[k][t] = true; int64& ret = memo[k][t]; //base if ( k == 0 ) { return ret = getWay(warps[t].sy - 1, warps[t].sx - 1); } //calc const int64 inOut = getWay(warps[t].sy - warps[k-1].sy, warps[t].sx - warps[k-1].sx), outOut = getWay(warps[t].sy - warps[k-1].gy, warps[t].sx - warps[k-1].gx); ret = (rec(k-1, t) - rec(k-1,k-1) * (inOut - outOut) % MOD + MOD) % MOD; return ret; } int solve() { for (int i = 0; i < K - 1; ++i) { swap(warps[i], *min_element(warps + i, warps + K)); } warps[K] = WarpHole(N, M, N, M); return int(rec(K, K)); } int main() { prepare(); for (;init();) { printf("%d\n", solve()); } return 0; }
0
#include<algorithm> #include<cstdio> int main(){ int sen[10000]; int bit[1024]; int num[1024],s; for(int i=0;i<1024;i++) for(num[i]=0,s=i;s>0;s >>= 1)num[i]+=s&1; int r,c,cc; while(true){ scanf("%d %d",&r,&c); if (!r)break; for(int i=0;i<c;i++) sen[i] = 0; for(int j=0;j<r;j++){ for(int i=0;i<c;i++){ scanf("%d",&cc); sen[i] <<= 1; sen[i] += cc; } } for(int i=0;i<1024;i++) bit[i] = 0; for(int i=0;i<c;i++) bit[sen[i]]++; int n,sum = 0,smax = 0,k=1,l; k <<= r-1; for(int i=0;i<k;i++){ sum = 0; for(int j=0;j<k<<1;j++){ s = j^i; sum += num[s] > (r>>1) ? num[s]*bit[j] : (r-num[s])*bit[j]; } if (sum>smax)smax = sum; } printf("%d\n",smax); } return 0; }
0