solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long int oo = 1e18; const long long int mod = 1e9; const int base = 31; const int tx[4] = {0, 0, 1, -1}; const int ty[4] = {1, -1, 0, 0}; const long long int maxN = 100 + 5; long long int n; string s1; void solve() { cin >> n >> s1; s1 = ' ' + s1; for (long long int j = 1; j <= (n); j++) if (s1[j] == '0') if (j > (n + 1) / 2) { cout << 1 << " " << j << " " << 1 << " " << j - 1 << " " << endl; return; } else { cout << j << " " << n << " " << j + 1 << " " << n << endl; return; } cout << 1 << " " << n - 1 << " " << 2 << " " << n << endl; return; } int main() { { ios::sync_with_stdio(false); cin.tie(0); }; long long int t = 1; cin >> t; while (t--) solve(); return 0; }
7
#include <bits/stdc++.h> long long n, a[10005], b[10005], ac, hd, tl, mid, c, cnt, lw, hi; int main() { scanf("%I64d%I64d", &n, &c); for (long long i = 1; i <= n; i++) scanf("%I64d%I64d", &a[i], &b[i]); hd = 1; tl = (1 << 30); lw = -1; while (hd <= tl) { mid = (hd + tl) / 2; cnt = 0; for (long long i = 1; i <= n; i++) { cnt = cnt + ((a[i] * mid) / b[i]) + 1; } if (cnt == c && (lw == -1 || mid < lw)) lw = mid; if (cnt < c) hd = mid + 1; else tl = mid - 1; } hd = 1; tl = (1 << 30); hi = -1; while (hd <= tl) { mid = (hd + tl) / 2; cnt = 0; for (long long i = 1; i <= n; i++) { cnt = cnt + ((a[i] * mid) / b[i]) + 1; } if (cnt == c && (hi == -1 || mid > hi)) hi = mid; if (cnt <= c) hd = mid + 1; else tl = mid - 1; } if (hi != -1) ac = hi - lw + 1; else ac = 0; printf("%I64d\n", ac, lw, hi); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int n; int ans[maxn]; int tree[maxn]; struct node { int val; int id; bool friend operator<(const node u, const node v) { return u.val < v.val; } } p[maxn]; int lowbit(int k) { return k & (-k); } void add(int x) { while (x <= n) { tree[x] += 1; x += lowbit(x); } } int get_sum(int x) { int ans = 0; while (x) { ans += tree[x]; x -= lowbit(x); } return ans; } void solve() { int pos = 1; for (int i = 1; i <= n; i = pos) { while (pos <= n && p[pos].val == p[i].val) { pos++; } for (int j = i; j < pos; j++) { int id = p[j].id; for (int k = 1; k <= n - 1 && k * (id - 1) + 2 <= n; k++) { ans[k] += get_sum(min(n, k * id + 1)) - get_sum(k * (id - 1) + 1); } } for (int j = i; j < pos; j++) { add(p[j].id); } } for (int i = 1; i <= n - 1; i++) { cout << ans[i] << " "; } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i].val; p[i].id = i; } sort(p + 1, p + 1 + n); solve(); }
14
#include <bits/stdc++.h> using namespace std; string slash = "/"; string ad = "://"; string dot = "."; string dom = "ru"; int main() { string str; string strue; cin >> str; int sizee = str.size(); int m = 0; if (str[0] == 'f') { strue = "ftp" + ad; m += 3; } if (str[0] == 'h') { strue = "http" + ad; m += 4; } int z = 0; int pointerr = m + 2; for (int i = m; i < sizee; i++) { if (str[i] == 'r') { if (str[i + 1] == 'u') { if (z != 0) { strue += dot; strue += dom; break; } else { strue += str[i]; pointerr++; } } else { strue += str[i]; pointerr++; } } else { strue += str[i]; pointerr++; } z++; } if (sizee > pointerr) { strue += slash; for (int i = pointerr; i < sizee; i++) { strue += str[i]; } } cout << strue; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { string s; cin >> s; s.push_back('R'); int cur = 0; int mx = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'R') { mx = max(mx, i + 1 - cur); cur = i + 1; } } if (mx == 0) { cout << s.size(); cout << "\n"; } else cout << mx << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; const double eps = 1e-9; const int mod = 998244353; int n, m, x; void solve(int tc) { cin >> n >> m; vector<int> a(n), b(m); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < m; ++i) { cin >> b[i]; } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int ans = 1; int ai = 0, bi = 0; while (ai < n and bi < m) { while (ai < n and a[ai] > b[bi]) { ++ai; } if (a[ai] != b[bi]) { cout << "0\n"; return; } int ind = ai; while (ai < n and a[ai] >= b[bi]) { ++ai; } ++bi; if (bi == m) break; ans = (1LL * ans * (ai - ind)) % mod; } if (bi < m or ai < n) { cout << "0\n"; return; } cout << ans << "\n"; } int main() { int tt = 1; for (int i = 1; i <= tt; ++i) { solve(i); } return 0; }
13
#include <bits/stdc++.h> using namespace std; int main() { int N, i; cin >> N; long long int num1[N], num2[N], sum = 0; for (i = 0; i < N; i++) { cin >> num1[i]; sum += num1[i]; } for (i = 0; i < N; i++) cin >> num2[i]; sort(num2, num2 + N); if (num2[N - 1] + num2[N - 2] >= sum) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, x, y; int nowx, cur = 1, ncur; int st, ed, id; pair<int, int> p[100005]; vector<pair<int, int> > v, nv; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); p[i] = make_pair(x, y); } sort(p + 1, p + m + 1); v.push_back(make_pair(1, 1)); while (cur <= m) { if (v.size() && p[cur].first != nowx + 1) { st = v[0].first; v.clear(); v.push_back(make_pair(st, n)); } for (ncur = cur; p[ncur].first == p[cur].first; ncur++) ; st = 1; id = 0; nv.clear(); for (int i = cur; i <= ncur; i++) { ed = (i < ncur) ? (p[i].second - 1) : n; for (; id < v.size() && v[id].second < st; id++) ; if (id < v.size() && max(v[id].first, st) <= ed) nv.push_back(make_pair(max(v[id].first, st), ed)); st = ed + 2; } v = nv; nowx = p[cur].first; cur = ncur; } printf("%d\n", (v.size() && (nowx < n || v[v.size() - 1].second == n)) ? n * 2 - 2 : -1); }
17
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int b[100] = {0}, g[100] = {0}; int B, G; cin >> B; int x; for (int i = 1; i <= B; i++) { cin >> x; b[x] = 1; } cin >> G; for (int i = 1; i <= G; i++) { cin >> x; g[x] = 1; } for (int i = 1; i <= 10005; i++) { int cb = i % n; int cg = i % m; if (b[cb] == 1 && g[cg] != 1) g[cg] = 1; else if (b[cb] != 1 && g[cg] == 1) b[cb] = 1; } int flag = 0; for (int i = 0; i < n; i++) { if (b[i] == 0) { cout << "No"; flag = 1; break; } } if (flag == 0) { for (int i = 0; i < m; i++) { if (g[i] == 0) { cout << "No"; flag = 1; break; } } } if (flag == 0) cout << "Yes"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int t,s; struct p{ int x; int y; }; struct a{ int s; p pos[4]; }; char ch; a zip[405]; int main(){ cin>>t; for(int times=0;times<t;times++){ cin>>zip[times].s; int k=0,flag=1; for(int i=1;i<=zip[times].s;i++){ for(int j=1;j<=zip[times].s;j++){ cin>>ch; if(ch=='*'){ zip[times].pos[k].x=i; zip[times].pos[k].y=j; k++; } } } if(zip[times].pos[0].x==zip[times].pos[1].x){ if(zip[times].pos[0].x<zip[times].s){ zip[times].pos[2].x=zip[times].pos[0].x+1; zip[times].pos[3].x=zip[times].pos[0].x+1; zip[times].pos[2].y=zip[times].pos[0].y; zip[times].pos[3].y=zip[times].pos[1].y; } else if(zip[times].pos[0].x>1){ zip[times].pos[2].x=zip[times].pos[0].x-1; zip[times].pos[3].x=zip[times].pos[0].x-1; zip[times].pos[2].y=zip[times].pos[0].y; zip[times].pos[3].y=zip[times].pos[1].y; } } else if(zip[times].pos[0].y==zip[times].pos[1].y){ if(zip[times].pos[0].y<zip[times].s){ zip[times].pos[2].y=zip[times].pos[0].y+1; zip[times].pos[3].y=zip[times].pos[0].y+1; zip[times].pos[2].x=zip[times].pos[0].x; zip[times].pos[3].x=zip[times].pos[1].x; } else if(zip[times].pos[0].y>1){ zip[times].pos[2].y=zip[times].pos[0].y-1; zip[times].pos[3].y=zip[times].pos[0].y-1; zip[times].pos[2].x=zip[times].pos[0].x; zip[times].pos[3].x=zip[times].pos[1].x; } } else { zip[times].pos[2].y=zip[times].pos[0].y; zip[times].pos[3].y=zip[times].pos[1].y; zip[times].pos[2].x=zip[times].pos[1].x; zip[times].pos[3].x=zip[times].pos[0].x; } } for(int times=0;times<t;times++){ int flag[405][405]={0}; for(int k=0;k<4;k++){ flag[zip[times].pos[k].x][zip[times].pos[k].y]=1; } for(int i=1;i<=zip[times].s;i++){ for(int j=1;j<=zip[times].s;j++){ if(flag[i][j])printf("*"); else printf("."); } printf("\n"); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; for (int i = 0; i < (int)v.size(); i++) { if (i) os << ", "; os << v[i]; } os << "}"; return os; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } vector<string> ops; struct basis { vector<long long> V; int size; basis() { size = 0; } bool insert(long long x, bool add = true) { if (x == 0) return false; long long curr = add ? x : 0; vector<string> to_add; for (long long a : V) if (a & -a & x) { x ^= a; if (curr != 0) { to_add.push_back(to_string(curr) + " ^ " + to_string(a)); } curr ^= a; } bool ret = x == 0; if (!add) { if (ret) { copy(((to_add).begin()), ((to_add).end()), back_inserter(ops)); } return ret; } if (ret) return 0; copy(((to_add).begin()), ((to_add).end()), back_inserter(ops)); for (long long& a : V) { if (x & -x & a) { ops.push_back(to_string(a) + " ^ " + to_string(x)); a ^= x; } } V.push_back(x); size++; return 1; } }; void go(long long x) { basis B; int X = x; x *= 2; for (; x < 1e18; x *= 2) { ops.push_back(to_string(x / 2) + " + " + to_string(x / 2)); ops.push_back(to_string(x) + " + " + to_string(X)); if (x != 2 * X) { ops.push_back(to_string(x / 2) + " + " + to_string(x / 2 - X)); } B.insert(x - X); B.insert(x + X); if (B.insert(1, false)) { return; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int x; cin >> x; go(x); cout << ops.size() << "\n"; for (auto it : ops) cout << it << "\n"; cout << "\n"; }
17
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 205, P = 10007; char s[N]; int n, m, f[N][N][N], len, g[N], ans; struct matrix { int f[N * 2][N * 2]; matrix() { memset(f, 0, sizeof(f)); } inline matrix operator*(const matrix &b) const { matrix c; for (int i = 1; i <= m; i++) for (int k = 1; k <= m; k++) { if (!f[i][k]) continue; for (int j = 1; j <= m; j++) c.f[i][j] = (c.f[i][j] + f[i][k] * b.f[k][j]) % P; } return c; } }; inline matrix ksm(matrix a, int b) { matrix res; for (int i = 1; i <= m; i++) res.f[i][i] = 1; while (b) { if (b & 1) res = res * a; a = a * a, b >>= 1; } return res; } int main() { scanf("%s%d", s + 1, &n); len = strlen(s + 1); f[1][len][0] = 1; for (int l = len; l; l--) for (int i = 1; i <= len - l + 1; i++) { int j = i + l - 1; for (int k = 0; k <= len; k++) { if (!f[i][j][k]) continue; if (len == 1) { g[k] = (g[k] + f[i][j][k]) % P; continue; } if (s[i] == s[j]) { f[i + 1][j - 1][k] = (f[i + 1][j - 1][k] + f[i][j][k]) % P; if (i + 1 > j - 1) g[k] = (g[k] + f[i][j][k]) % P; } else f[i + 1][j][k + 1] = (f[i + 1][j][k + 1] + f[i][j][k]) % P, f[i][j - 1][k + 1] = (f[i][j - 1][k + 1] + f[i][j][k]) % P; } } matrix c; for (int i = 1; i < len; i++) c.f[i][i + 1] = 1; for (int i = 1; i <= len; i++) c.f[i][i] = 24; c.f[len][len + 1] = 1; int num = (len + 1) / 2; for (int i = 1; i < num; i++) c.f[i + len][i + len + 1] = 1; for (int i = 1; i <= num; i++) c.f[i + len][i + len] = 25; m = num * 2 + len; for (int i = 1; i <= num; i++) c.f[i + len][i + len + num] = 1; for (int i = 1; i <= num; i++) c.f[i + len + num][i + len + num] = 26; c = ksm(c, (len + n) / 2); for (int i = 0; i <= len; i++) { int to = (len + 1 - i) / 2; ans = (ans + c.f[len - i + 1][len + to + num] * g[i]) % P; } if ((len + n) & 1) { ans = ans * 26 % P; for (int i = 0; i <= len; i++) if ((len - i) & 1) ans = (ans + c.f[len + 1 - i][len + (len + 1 - i) / 2] * g[i]) % P; } printf("%d\n", ans); }
22
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; long long a[maxn], b[maxn]; void solve() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%lld", &a[i]); sort(a, a + n); long long sum(0), cnt(0); for (int i = 0; i < n; ++i) { if (i) cout << " "; b[i % m] += a[i]; sum += b[i % m]; cout << sum; } } int main() { int t; solve(); }
7
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b, long long m) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } int main() { long long t = 1, b = 1; while (t--) { map<long long, long long> m[26]; long long x[26]; for (char i = 'a'; i <= 'z'; ++i) { cin >> x[i - 'a']; } string s; cin >> s; long long pre[s.size()]; pre[0] = x[s[0] - 'a']; m[s[0] - 'a'][pre[0]]++; long long c = 0; for (long long int i = 1; i < s.size(); i++) { long long y = s[i] - 'a'; c += m[y][pre[i - 1]]; pre[i] = pre[i - 1] + x[y]; m[y][pre[i]]++; } cout << c << "\n"; } return 0; }
10
#include <bits/stdc++.h> using namespace std; bool s[1001]; int main() { int n; cin >> n; int m; cin >> m; vector<pair<int, int> > v; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; s[a] = true; s[b] = true; } int sm; for (int i = 1; i <= n; i++) { if (!s[i]) { sm = i; break; } } cout << n - 1 << endl; for (int i = 1; i <= n; i++) { if (i != sm) cout << sm << " " << i << endl; } }
5
#include <bits/stdc++.h> using namespace std; int main() { string letter; cin >> letter; pair<int, int> dp[letter.size()]; dp[0] = make_pair(0, islower(letter[0]) ? 1 : 0); for (int i = 1; i < letter.size(); ++i) { if (islower(letter[i])) { dp[i].second = dp[i - 1].second + 1; dp[i].first = dp[i - 1].first; } else { dp[i].second = dp[i - 1].second; if (dp[i - 1].first == dp[i - 1].second) dp[i].first = dp[i - 1].first; else { dp[i].first = min(dp[i - 1].first + 1, dp[i - 1].second); } } } cout << dp[letter.size() - 1].first << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e18; int main() { long long b, c, d, i, j, k, l, n, x, y, t, ans; cin >> n; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } cout << 1 << " " << 1 << endl << -a[0] << endl; a[0] = 0; if (n == 1) { cout << 1 << " " << 1 << endl << 0 << endl << 1 << " " << 1 << endl << 0 << endl; } else { cout << 2 << " " << n << endl; for (i = 1; i <= n - 1; i++) { cout << (n - 1) * a[i] << " "; a[i] = a[i] + (n - 1) * a[i]; } cout << endl << 1 << " " << n << endl; for (i = 0; i < n; i++) { cout << -a[i] << " "; } cout << endl; } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long power(long long a, long long n) { long long res = 1; while (n != 0) { if (n % 2) { res = res * a; n--; } else { a = a * a; n = n / 2; } } return res; } vector<int> tree[200001]; vector<int> v(200001); vector<int> in(200001); vector<int> ans(200001); void dfs1(int u, int par) { for (auto it = tree[u].begin(); it != tree[u].end(); it++) { if (*it != par) { dfs1(*it, u); } } int temp = 0; for (auto it = tree[u].begin(); it != tree[u].end(); it++) { if (*it != par && in[*it] > 0) temp += in[*it]; } in[u] = v[u] + temp; } void dfs2(int u, int par) { int sum = 0; if (in[u] > 0 && par != -1) { int temp = ans[par] - in[u]; if (temp > 0) sum += temp; } else if (par != -1 && ans[par] > 0) sum += ans[par]; ans[u] = in[u] + sum; for (auto it = tree[u].begin(); it != tree[u].end(); it++) { if (*it != par) dfs2(*it, u); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; if (v[i] == 0) v[i] = -1; } for (int i = 1; i <= n - 1; i++) { int a, b; cin >> a >> b; tree[a - 1].push_back(b - 1); tree[b - 1].push_back(a - 1); } dfs1(0, -1); ans[0] = in[0]; dfs2(0, -1); for (int i = 0; i < n; i++) cout << ans[i] << " "; }
10
#include <bits/stdc++.h> using namespace std; int n; int a[100 + 10]; int cnt[100 + 10]; int f[10000 + 10][100 + 10]; bool vis[10000 + 10][100 + 10]; int main() { scanf("%d", &n); f[0][0] = 1; int tot = 0; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); if (cnt[a[i]] == 0) ++tot; ++cnt[a[i]]; } if (tot < 3) { printf("%d\n", n); return 0; } int sum = 0; for (int i = 1; i <= 100; ++i) { for (int j = sum; j >= 0; --j) for (int k = 10000 - i; k >= 0; --k) if (f[k][j] > 0) for (int l = 1; l <= cnt[i] && k + l * i <= 10000; ++l) f[k + l * i][j + l] += f[k][j]; sum += cnt[i]; } int ans = 1; for (int i = 1; i <= 100; ++i) for (int j = cnt[i]; j > ans; --j) if (f[j * i][j] == 1) { ans = max(ans, j); break; } printf("%d\n", ans); return 0; }
13
#include <bits/stdc++.h> using namespace std; void read(vector<long long> &v) { for (int i = 0; i < v.size(); i++) cin >> v[i]; } void print(vector<long long> v) { for (int i = 0; i < v.size(); i++) cout << v[i] << " "; } long long n, m, a, b, temp; string str; set<long long> s; vector<long long> v; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; long long dp1 = 0, dp0 = 0; dp1 = 1; for (int i = 2; i <= n; i++) { if (i % 2 == 1) { dp1 = (dp1 + dp0 + 1) % 1000000007; } else { dp0 = (dp0 + dp1 + 1) % 1000000007; } } cout << (dp1 + dp0) % 1000000007; }
8
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const int n_ = 1e5 + 1000; long long power(long long a, long long n, long long mod = MOD) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int n, m, g[n_], G, vis[n_], dis[n_], sum, C; vector<int> e[n_], re[n_], tp, cmp[n_]; queue<int> q; void dfs(int u) { vis[u] = 1; for (auto v : e[u]) if (!vis[v]) dfs(v); tp.push_back(u); } void rdfs(int u, int c) { g[u] = c; cmp[c].push_back(u); vis[u] = 1; for (auto v : re[u]) if (!vis[v]) rdfs(v, c); } void SCC() { for (int i = 0; i < n; i++) { if (!vis[i]) dfs(i); } memset(vis, 0, sizeof vis); for (int i = n - 1; i >= 0; i--) { if (!vis[tp[i]]) rdfs(tp[i], G++); } } int bfs(int s) { int ret = INF; vis[s] = 1; q.push(s); dis[s] = 0; while (q.size()) { int u = q.front(); q.pop(); for (auto v : e[u]) { if (!vis[v]) { vis[v] = 1; dis[v] = dis[u] + 1; q.push(v); } else if (v == s) { ret = min(ret, dis[u] + 1); } } } return ret; } int get(int c) { int ret = INF; for (auto u : cmp[c]) { while (q.size()) q.pop(); memset(vis, 0, sizeof vis); ret = min(ret, bfs(u)); } return (ret == INF ? 0 : ret); } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; e[u].push_back(v); re[v].push_back(u); } SCC(); for (int i = 0; i < G; i++) { int fl = 1; for (auto u : cmp[i]) { for (auto v : e[u]) { fl &= (g[u] == g[v]); } } if (fl) { int tmp = get(i); sum += tmp; C += (tmp != 0); } } cout << n + C + 998 * sum << endl; return 0; }
21
#include <bits/stdc++.h> const int md = 1e9 + 7, inf = 1e9 + 7; using namespace std; void debug(string names) { cout << '\n'; } template <typename A1, typename... A2> void debug(string names, A1 par, A2... left) { int pos = 0; for (; pos < names.size() && names[pos] != ' ' && names[pos] != ','; pos++) cout << names[pos]; cout << ": " << par << " "; while (pos < names.size() && (names[pos] == ' ' || names[pos] == ',')) { pos++; } names.erase(names.begin(), names.begin() + pos); debug(names, left...); } struct fenwick { int n; vector<long long> tree; fenwick() {} fenwick(int sz) { n = sz; tree.resize(n + 1, 0); } void upd(int pos, long long add) { for (; pos <= n; pos += (pos & -pos)) tree[pos] += add; } long long query(int pos) { long long rtn = 0; for (; pos; pos -= (pos & -pos)) rtn += tree[pos]; return rtn; } }; int q; vector<pair<int, int>> a; map<int, int> to, ba; fenwick F; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> q; char ch; a.resize(q); for (int i = 0; i < q; i++) { cin >> ch >> a[i].second; if (ch == '-') a[i].first = -1; else a[i].first = 1; } int nxt = 1; for (auto &i : a) to[i.second] = 0; for (auto &i : to) i.second = nxt++; for (auto &i : to) ba[i.second] = i.first; F = fenwick(nxt + 10); int nxtind = 0; set<pair<long long, int>> val; for (auto &i : a) { if (i.first == 1) { val.insert({i.second, nxt}); nxt++; F.upd(to[i.second], i.second); } else { val.erase(val.lower_bound({i.second, -1})); F.upd(to[i.second], -i.second); } if (val.size()) { int cnt = 0; auto it = val.begin(), it2 = it; long long cursum = it->first; while (it != val.end()) { it2 = val.upper_bound({2 * cursum, -1}); if (it2 != val.end()) { long long ss = F.query(to[it2->first] - 1); if (2 * ss < it2->first) cnt++; it = it2; cursum = ss + it2->first; } else { it = val.end(); } } cout << (int)val.size() - 1 - cnt << '\n'; } else { cout << 0 << '\n'; } } }
20
#include <bits/stdc++.h> using namespace std; int a[100001], b[100001] = {0}, c, d, i, j, n, m, l, r; int main() { cin >> n >> m; l = 0; bool tr = false; for (i = 0; i < n; i++) { cin >> a[i]; if (b[a[i]] == 0) c++; if (c == m && !tr) tr = true, r = i; if (!tr) b[a[i]]++; } if (!tr) cout << "-1 -1" << endl; else { while (1) { if (r - l == 0) break; b[a[l]]--; if (b[a[l]] == 0) break; l++; } cout << l + 1 << " " << r + 1 << endl; } return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<int> ans; int m, n = 1e6; int main() { cin >> m; while (n--) { int s = 0, times = 10, pw = 5; while (times--) { s += n / pw; pw *= 5; } if (m == s) ans.push_back(n); } printf("%d\n", ans.size()); sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } int main() { long long t = 1; while (t--) { long double a, b, c; cin >> a >> b >> c; long double val1 = (b * b - 4 * a * c); val1 = sqrt(val1); long double val2 = -1 * (b)-val1; long double val3 = -1 * (b) + val1; val2 = val2 / (2 * a); val3 = val3 / (2 * a); cout << fixed << setprecision(15) << max(val2, val3) << "\n"; cout << fixed << setprecision(15) << min(val2, val3) << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int T[n]; for (int i = 0; i < n; ++i) cin >> T[i]; string s; cin >> s; int maxp, minl; minl = -1000000000; maxp = 1000000000; int lc = 4; char lch = '0'; for (int i = 4; i < n; ++i) { if (s[i] != lch) { lc = 1; lch = s[i]; if (lch == '0') { int m5 = T[i - 4]; for (int j = 0; j < 4; ++j) m5 = min(m5, T[i - j]); maxp = min(maxp, m5 - 1); } else { int m5 = T[i - 4]; for (int j = 0; j < 4; ++j) m5 = max(m5, T[i - j]); minl = max(minl, m5 + 1); } } } cout << minl << " " << maxp << endl; }
8
#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = pair<int, int>; lint ccw(pi a, pi b, pi c) { int dx1 = b.first - a.first; int dy1 = b.second - a.second; int dx2 = c.first - a.first; int dy2 = c.second - a.second; return 1ll * dx1 * dy2 - 1ll * dy1 * dx2; } lint dist(pi a, pi b) { int dx1 = b.first - a.first; int dy1 = b.second - a.second; return 1ll * dx1 * dx1 + 1ll * dy1 * dy1; } int fail[1000005]; bool rot_same(vector<lint> a, vector<lint> b) { for (int i = 0; i < a.size(); i++) b.push_back(b[i]); int p = 0; for (int i = 1; i < a.size(); i++) { while (p && a[i] != a[p]) p = fail[p]; if (a[i] == a[p]) p++; fail[i + 1] = p; } p = 0; for (auto &i : b) { while (p && i != a[p]) p = fail[p]; if (i == a[p]) p++; if (p == a.size()) return true; } return false; } vector<pi> getPoly(int n) { vector<pi> h; for (int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); h.emplace_back(x, y); } swap(h[0], *min_element(h.begin(), h.end())); sort(h.begin() + 1, h.end(), [&](const pi &a, const pi &b) { auto x = ccw(h[0], a, b); if (x != 0) return x > 0; return dist(h[0], a) < dist(h[0], b); }); vector<pi> poly; for (auto &i : h) { while (poly.size() >= 2 && ccw(poly[poly.size() - 2], poly.back(), i) <= 0) { poly.pop_back(); } poly.push_back(i); } return poly; } int main() { int n, m; cin >> n >> m; auto p1 = getPoly(n); auto p2 = getPoly(m); if (p1.size() != p2.size()) { puts("NO"); return 0; } if (p1.size() == 2 && p2.size() == 2) { lint c1 = dist(p1[0], p1[1]); lint c2 = dist(p2[0], p2[1]); if (c1 != c2) puts("NO"); else puts("YES"); return 0; } n = p1.size(); vector<lint> seq1, seq2; for (int i = 0; i < n; i++) { seq1.push_back(-dist(p1[i], p1[(i + 1) % n])); seq1.push_back(ccw(p1[i], p1[(i + 1) % n], p1[(i + 2) % n])); seq2.push_back(-dist(p2[i], p2[(i + 1) % n])); seq2.push_back(ccw(p2[i], p2[(i + 1) % n], p2[(i + 2) % n])); } puts(rot_same(seq1, seq2) ? "YES" : "NO"); }
16
#include <bits/stdc++.h> using namespace std; struct cir { int x, y, r; } p[20010]; struct node { long double x, y; }; struct line { long double thi; int id, cnt, dfn; } q[1000010]; int n, D, tot, c[20010], Cnt[50], ord[50]; long double pi = acos(-1), T1[50], T2[50]; void read(int &x) { char ch = getchar(); int mark = 1; for (; ch != '-' && (ch < '0' || ch > '9'); ch = getchar()) ; if (ch == '-') mark = -1, ch = getchar(); for (x = 0; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48; x *= mark; } inline long double sqr(long double x) { return x * x; } inline long double Dis(node A, node B) { return sqrt(sqr(A.x - B.x) + sqr(A.y - B.y)); } node make(long double x, long double y) { node ans; ans.x = x; ans.y = y; return ans; } void Add(long double thi, int id, int cnt, int dfn) { if (thi <= -pi) thi += pi * 2; if (thi > pi) thi -= pi * 2; q[++tot].thi = thi; q[tot].id = id; q[tot].cnt = cnt; q[tot].dfn = dfn; } long double Abs(long double x) { if (x < 0) return -x; return x; } inline bool cmp(line A, line B) { if (Abs(A.thi - B.thi) <= 1e-9 && A.id == B.id) return A.dfn < B.dfn; return A.thi < B.thi; } inline bool cmp1(int a, int b) { return Cnt[a] > Cnt[b]; } int main() { read(n); read(D); for (int i = 1; i <= n; i++) { read(p[i].x), read(p[i].y), read(p[i].r); long double O = Dis(make(0, 0), make(p[i].x, p[i].y)), L = O - p[i].r, R = O + p[i].r; int Max = R / D, top = 0; long double tha = atan2(p[i].y, p[i].x); for (int k = Max; k * D >= L; k--) { long double thi = acos((sqr(O) + sqr(k * D) - sqr(p[i].r)) / (O * k * D) / 2); int s = 0; for (int j = Max; j * D >= L; j--) s += (sqrt(sqr(O) + sqr(j * D) - O * j * D * cos(thi) * 2) <= 1e-9 + p[i].r); T1[++top] = tha - thi; T2[top] = tha + thi; Cnt[top] = s; ord[top] = top; } if (top) { sort(ord + 1, ord + top + 1, cmp1); int t = 0; for (int j = top; j; j--) Add(T1[ord[j]], i, Cnt[ord[j]], ++t); for (int j = 1; j < top; j++) Add(T2[ord[j]], i, Cnt[ord[j + 1]], ++t); Add(T2[ord[top]], i, 0, ++t); } } if (!tot) printf("0\n"); else { sort(q + 1, q + tot + 1, cmp); int sum = 0, Max = 0; long double dx = cos(q[1].thi) * D, dy = sin(q[1].thi) * D; for (int i = 1; i <= n; i++) { node tmp = make(p[i].x, p[i].y); long double O = Dis(make(0, 0), tmp), L = O - p[i].r, R = O + p[i].r; int Max = R / D; for (int j = Max; j * D >= L; j--) c[i] += (Dis(tmp, make(dx * j, dy * j)) <= 1e-9 + p[i].r); sum += c[i]; } for (int i = 1; i <= tot; i++) { sum -= c[q[i].id]; c[q[i].id] = q[i].cnt; sum += q[i].cnt; Max = max(Max, sum); } printf("%d\n", Max); } return 0; }
18
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, sum, t; cin >> a >> b >> c >> d; if (c > a) { t = c; c = a; a = t; } if (b < d) { t = b; b = d; d = t; } sum = (a + 2) * (b + d + 2); sum = sum - (a * (b + d)); cout << sum; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t) { int n; cin >> n; string s; cin >> s; int p = 0; for (int i = 0; i < n - 1; i++) if (s[i] != s[i + 1]) p = 1; if (p == 0) { if (s[0] == '1') cout << n * 2 << "\n"; else cout << n << "\n"; } else { int x, y; for (int i = 0; i < n; i++) if (s[i] == '1') { x = i; break; } for (int i = n - 1; i >= 0; i--) if (s[i] == '1') { y = i; break; } int h = 0; h = max(x, n - 1 - y); h = max(h, n - 1 - x); h = max(h, y); cout << 2 * (h + 1) << "\n"; } t--; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[1000], b, c, i, x, n, m, y, k; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { if (a[i] >= a[i + 1]) k++; } cout << k << endl; for (i = 0; i < n; i++) { if (a[i] >= a[i + 1]) cout << a[i] << " "; } }
0
#include <bits/stdc++.h> using namespace std; int n, m, Q, num = 0, top = 0, tot = 0, d[110010], h[110010], fa[110010][20], id[110010], cut[110010], dfn[110010], low[110010], vis[110010]; pair<int, int> q[110010 << 1]; struct edge { int x, y, next; } mp[110010 << 1]; inline char gc() { static char *S, *T, buf[1 << 16]; if (T == S) { T = (S = buf) + fread(buf, 1, 1 << 16, stdin); if (T == S) return EOF; } return *S++; } inline int read() { int x = 0, f = 1; char ch = gc(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = gc(); } while ('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = gc(); return x * f; } inline void ins(int x, int y) { mp[++num].x = x; mp[num].y = y; mp[num].next = h[x]; h[x] = num; mp[++num].x = y; mp[num].y = x; mp[num].next = h[y]; h[y] = num; } void dfs1(int x, int f) { for (int i = 1; i <= 17; i++) { if (!fa[x][i - 1]) break; fa[x][i] = fa[fa[x][i - 1]][i - 1]; } id[x] = f; for (int i = h[x]; i; i = mp[i].next) { int y = mp[i].y; if (d[y]) continue; d[y] = d[x] + 1; fa[y][0] = x; dfs1(y, f); } } void tarjan(int x, int f) { dfn[x] = low[x] = ++tot; for (int i = h[x]; i; i = mp[i].next) { int y = mp[i].y; if (y == f || dfn[y] >= dfn[x]) continue; q[++top] = make_pair(x, y); if (!dfn[y]) { tarjan(y, x); low[x] = min(low[x], low[y]); if (low[y] >= dfn[x]) { int tmp = top, u, v, flag = 0; do { u = q[top].first, v = q[top--].second; if ((d[u] & 1) == (d[v] & 1)) { flag = 1; break; } } while (!(x == u && y == v)); if (!flag) continue; top = tmp; do { u = q[top].first, v = q[top--].second; cut[u] = cut[v] = 1; } while (!(x == u && y == v)); cut[x] = 0; } } else low[x] = min(low[x], dfn[y]); } } inline void dfs(int x) { cut[x] += cut[fa[x][0]]; vis[x] = 1; for (int i = h[x]; i; i = mp[i].next) { int y = mp[i].y; if (vis[y]) continue; dfs(y); } } inline int lca(int x, int y) { if (d[x] < d[y]) swap(x, y); int len = d[x] - d[y]; for (int i = 0; i <= 17; i++) { if (len & (1 << i)) x = fa[x][i]; } if (x == y) return x; for (int i = 17; i >= 0; i--) { if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; } return fa[x][0]; } int main() { n = read(); m = read(); for (int i = 1; i <= m; i++) ins(read(), read()); for (int i = 1; i <= n; i++) { if (!d[i]) d[i] = 1, dfs1(i, i), tarjan(i, 0), dfs(i); } Q = read(); for (int i = 1; i <= Q; i++) { int x = read(), y = read(); if (id[x] != id[y]) { puts("No"); continue; } int z = lca(x, y); if ((d[x] + d[y] - 2 * d[z]) & 1) { puts("Yes"); continue; } if (cut[x] + cut[y] - 2 * cut[z] > 0) puts("Yes"); else puts("No"); } return 0; }
14
#include <bits/stdc++.h> using namespace std; int t, n, a[300005], k, b[300005]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> t; for (int z = 1; z <= t; z++) { bool ok = true; string s; cin >> n >> k; cin >> s; for (int i = 1; i <= n; i++) if (s[i - 1] != '?') a[i] = s[i - 1] - '0'; else a[i] = 2; for (int i = 1; i <= k; i++) { int j = i, tg = 2; while (j <= n) { if (tg != 2 && a[j] != 2 && tg != a[j]) { ok = false; break; } if (a[j] != 2) tg = a[j]; j += k; } b[i] = tg; } int dem = 0, dem1 = 0, dem2 = 0; for (int i = 1; i <= k; i++) if (b[i] == 0) dem++; else if (b[i] == 1) dem1++; else dem2++; int kc = abs(dem1 - dem); if (kc > dem2) ok = false; else if ((dem2 - kc) % 2) ok = false; if (ok == true) cout << "YES" << '\n'; else cout << "NO" << '\n'; } }
7
#include <bits/stdc++.h> using namespace std; long long solve() { long long p, q, ma = 1, mi = 1000000000000000000; cin >> p >> q; map<long long, long long> z; if (q > p || p % q != 0) return p; else { while (q % 2 == 0) { q = q / 2; z[2]++; } for (long long i = 3; i <= sqrt(q); i += 2) { while (q % i == 0) { z[i]++; q = q / i; } } if (q > 2) z[q]++; for (auto it = z.begin(); it != z.end(); it++) { long long f = 0, r = it->first, s = p; while (s % r == 0) { f++; s /= r; } mi = min(mi, (long long)pow(it->first, f - it->second + 1)); } return (p / mi); } } int main() { int t = 1; cin >> t; while (t--) cout << solve() << endl; return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("O3") 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 pow1(long long int a, long long int b, long long int m = 1000000007) { if (b == 0) return 1ll; else if (b == 1) return a; else { long long int x = pow1(a, b / 2, m); x *= x; x %= m; if (b % 2) { x *= a; x %= m; } return x; } } template <class avgType> avgType avg(avgType a, avgType b) { return (a + b) / 2; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count()); int randInt() { return rng() % INT_MAX; } long long int randLL() { return rng64() % LLONG_MAX; } vector<int> hashmods = {1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093}; int n, a[210000]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n; long long int ans = 0, s1 = 0, s2 = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (i % 2) s1 += a[i]; else s2 += a[i]; } ans = s1; for (int i = 1; i <= n; i++) { if (i % 2) { s1 -= a[i]; s2 += a[i]; ans = max({s2, ans}); } else { s2 -= a[i]; s1 += a[i]; ans = max({s1, ans}); } } cout << ans << "\n"; return 0; }
13
#include <bits/stdc++.h> using namespace std; char s[2010]; char u[2010]; char t[6010]; int main() { scanf("%s", s); scanf("%s", u); int ans = 100000; int ls, lu; ls = strlen(s); lu = strlen(u); for (int i = 0; i < lu; i++) t[i] = 1; for (int i = lu; i < (lu + ls); i++) t[i] = s[i - lu]; for (int i = 0; i < lu; i++) t[i + lu + ls] = 1; int lt = lu + lu + ls; for (int i = 0; (i + lu) < lt; i++) { int count = 0; for (int j = 0; j < lu; j++) if (u[j] != t[i + j]) count++; ans = min(ans, count); } printf("%d", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; deque<long long> pos, a, b; set<pair<long long, long long> > l, r, left; cin >> n; pos.resize(n); a.resize(n); b.resize(n); for (long long i = 0; i < n; i++) { cin >> a[i]; a[i]--; pos[a[i]] = i; } for (long long i = 0; i < n; i++) { cin >> b[i]; b[i]--; long long d = pos[b[i]] - i; if (d >= 0) { l.insert({d, b[i]}); left.insert({n - 1 - pos[b[i]] + i + 1, b[i]}); } else { r.insert({abs(d), b[i]}); left.insert({abs(d), b[i]}); } } long long ptr = -1; for (long long i = 0; i < n; i++) { if (ptr >= 0) { long long x = b[ptr]; l.erase({pos[x] - ptr, x}); r.insert({n - 1 - pos[x] + ptr + 1, x}); } ptr++; while (left.size() && (*left.begin()).first == i) { pair<long long, long long> fir = (*left.begin()); left.erase(left.begin()); r.erase(fir); fir.first *= -1; l.insert(fir); } long long mn = 1e18; if (l.size()) mn = min(mn, (*l.begin()).first + i); if (r.size()) mn = min(mn, (*r.begin()).first - i); cout << mn << "\n"; } return 0; }
13
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 5; vector<int> g[N]; int low[N], dis[N], timer = 0, vis[N] = {0}; int disjoint[N] = {0}, labels[N]; vector<int> bridgeTree[N]; vector<pair<int, int> > bridgeNode[N]; map<pair<int, int>, int> bridg; map<pair<int, int>, int> id; vector<pair<int, int> > edge; void dfs(int s, int p) { low[s] = dis[s] = timer++; vis[s] = 1; for (auto i : g[s]) { if (!vis[i]) { dfs(i, s); low[s] = min(low[s], low[i]); if (dis[s] < low[i]) { bridg[make_pair(min(i, s), max(i, s))] = 1; } } else if (i != p) { low[s] = min(low[s], dis[i]); } } } void connectedComponents(int s, int lbl) { labels[s] = lbl; disjoint[lbl]++; vis[s] = 1; for (auto i : g[s]) if (!vis[i]) { if (bridg[make_pair(min(i, s), max(i, s))]) continue; connectedComponents(i, lbl); } } void joinComponent(int s, int p, int lbl) { vis[s] = 1; for (auto i : g[s]) { if (vis[i] == 2 || i == p || bridg[make_pair(min(i, s), max(i, s))]) continue; int idx = id[make_pair(min(i, s), max(i, s))]; edge[idx] = make_pair(s, i); if (vis[i] == 0) joinComponent(i, s, lbl); } vis[s] = 2; } void buildTree(int s) { vis[s] = 1; for (auto i : g[s]) { if (vis[i]) continue; if (bridg[make_pair(min(i, s), max(i, s))]) { bridgeTree[labels[s]].push_back(labels[i]), bridgeNode[labels[s]].push_back(make_pair(s, i)); bridgeTree[labels[i]].push_back(labels[s]), bridgeNode[labels[i]].push_back(make_pair(i, s)); } buildTree(i); } } void bridgeDfs(int s, int p) { for (int i = 0; i < (int)bridgeTree[s].size(); i++) { int lbl = bridgeTree[s][i], u = bridgeNode[s][i].first, v = bridgeNode[s][i].second; if (lbl == p) continue; int idx = id[make_pair(min(u, v), max(u, v))]; edge[idx] = make_pair(v, u); bridgeDfs(lbl, s); } } int solve(int n) { memset(vis, 0, sizeof(vis)); for (int j = 1; j <= n; j++) if (!vis[j]) dfs(j, 0); int lbl = 1; memset(vis, 0, sizeof(vis)); for (int j = 1; j <= n; j++) if (!vis[j]) connectedComponents(j, lbl++); memset(vis, 0, sizeof(vis)); for (int j = 1; j <= n; j++) if (!vis[j]) joinComponent(j, 0, labels[j]); int mx = 0, src = -1; for (int j = 1; j < lbl; j++) if (disjoint[j] > mx) mx = disjoint[j], src = j; memset(vis, 0, sizeof(vis)); buildTree(1); bridgeDfs(src, 0); return mx; } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int j = 0; j < m; j++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); id[make_pair(min(u, v), max(u, v))] = j; edge.push_back(make_pair(u, v)); } cout << solve(n) << endl; for (auto i : edge) cout << i.first << " " << i.second << endl; return 0; }
15
#include <bits/stdc++.h> using namespace std; int main() { long long int n; char c; cin >> n >> c; long long int aux = 0; if (c == 'a') aux = 4; if (c == 'b') aux = 5; if (c == 'c') aux = 6; if (c == 'd') aux = 3; if (c == 'e') aux = 2; if (c == 'f') aux = 1; if (n % 4 == 1 || n % 4 == 2) { long long int auxx = 0; if (n % 4 == 1) auxx++; else auxx += 2; n--; cout << n + aux + (6 * ((n + auxx) / 2)); } else { cout << n - 3 + aux + (6 * ((n / 2) - 1)); } }
4
#include <bits/stdc++.h> using namespace std; const int Maxn = 300005; int ans, cnt1, cnt2, n, x[Maxn], y[Maxn]; bool del1[Maxn], del2[Maxn]; struct line { int x, y, id; bool operator<(const line &tmp) const { return y > tmp.y; } } L1[Maxn], L2[Maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]); for (int i = 1; i <= n; i++) if (x[i] < y[i]) L1[++cnt1].x = x[i], L1[cnt1].y = y[i], L1[cnt1].id = i; sort(L1 + 1, L1 + 1 + cnt1); ans = cnt1; for (int i = 1; i < n; i++) if (L1[i].y < L1[i + 1].x) ans--, del1[i] = true; int tmp = 0; for (int i = 1; i <= n; i++) if (x[i] > y[i]) L2[++cnt2].x = y[i], L2[cnt2].y = x[i], L2[cnt2].id = i; sort(L2 + 1, L2 + 1 + cnt2); tmp = cnt2; for (int i = 1; i < n; i++) if (L2[i].y < L2[i + 1].x) tmp--, del2[i] = true; printf("%d\n", max(tmp, ans)); if (tmp < ans) { for (int i = 1; i <= cnt1; i++) if (!del1[i]) printf("%d ", L1[i].id); } else { for (int i = cnt2; i >= 1; i--) if (!del2[i]) printf("%d ", L2[i].id); } return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; ++i) { int a, b, c; cin >> a >> b >> c; if (a + c <= b) cout << 0 << '\n'; else { int m = 0; if (b + c < a) m = (a - (b + c) + 1) / 2 - 1; cout << (a + c - b + 1) / 2 - m << '\n'; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1; const int inf = 1e9; const int M = 1e9; int k; int main() { scanf("%d", &k); if (k & 1) { puts("YES"); int n = 4 * (k - 1) + 2; int m = 2 * (k - 1) * (k - 1) + 2 * (k - 1) + 1 + (k - 1); cout << n << " " << m << endl; int cnt = 2 * k - 1; for (int i = 2; i <= k; i++) cout << 1 << " " << i << endl; for (int i = 2; i <= k; i++) for (int j = 1; j < k; j++) cout << i << " " << j + k << endl; for (int i = 1; i < k; i += 2) cout << k + i << " " << k + i + 1 << endl; for (int i = 2; i <= k; i++) cout << cnt + 1 << " " << cnt + i << endl; for (int i = 2; i <= k; i++) for (int j = 1; j < k; j++) cout << cnt + i << " " << cnt + j + k << endl; for (int i = 1; i < k; i += 2) cout << cnt + k + i << " " << cnt + k + i + 1 << endl; cout << 1 << " " << cnt + 1 << endl; } else puts("NO"); return 0; }
11
#include <bits/stdc++.h> using namespace std; int n, k; int a, b; int ans = 1000000007; map<int, int> A, B, C; int main() { scanf("%d", &n); k = n / 2 + n % 2; for (int i = 1; i <= n; i++) { scanf("%d%d", &a, &b); A[a]++; B[b]++; if (a == b) C[a]++; } for (auto i : A) { if (i.second + B[i.first] - C[i.first] >= k) { int j = k - min(i.second, k); if (B[i.first] - C[i.first] >= j) ans = min(ans, j); } } for (auto i : B) if (i.second >= k) ans = min(ans, k); if (ans == 1000000007) ans = -1; printf("%d", ans); }
7
#include <bits/stdc++.h> using namespace std; long long int Ceil(long long int a, long long int b) { return a / b + (a % b != 0); } long long int fastModExp(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b = b >> 1; } return res; } map<long long int, long long int> mpx; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n; cin >> n; vector<long long int> v; long long int x; for (int i = 0; i < n; i++) { cin >> x; v.push_back(x); }; int a = max_element(v.begin(), v.end()) - v.begin(); long long int top = v[a]; bool p = true; long long int r; int j = a + 1, i = a - 1; while (true) { if (i >= 0 && j < n) { r = max(v[i], v[j]); if (r >= top) { p = false; break; } else { top = r; if (r == v[j]) j++; else i--; } } else if (i < 0 && j >= n) { break; } else if (i < 0) { r = v[j]; if (r >= top) { p = false; break; } else { top = r; j++; } } else if (j >= n) { r = v[i]; if (r >= top) { p = false; break; } else { top = r; i--; } } } if (p) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int divs[1000001] = {0}; int mark[1000001] = {0}; void prime() { for (int i = 2; i < 1000000; i++) { if (!mark[i]) { divs[i] = i; int k = 1; if (i < 1000) for (int j = i * i; j <= 1000000; j += i) { if (!mark[j]) { mark[j] = 1; divs[j] = i; } } } } } long long GCD(long long a, long long b) { if (!b) return a; return GCD(b, a % b); } long long LCM(long long a, long long b) { return (a * b) / GCD(a, b); } int gcd[101][101], lcm[101][101], mul[101]; int n, m; int ans[101]; int main() { prime(); for (int i = 0; i < 101; i++) ans[i] = 1; cin >> n >> m; memset(gcd, 0, sizeof gcd); memset(lcm, 0, sizeof lcm); int a, b, g, l; for (int i = 0; i < m; i++) { scanf("%d%d%d%d", &a, &b, &g, &l); gcd[a - 1][b - 1] = gcd[b - 1][a - 1] = g; lcm[a - 1][b - 1] = lcm[b - 1][a - 1] = l; } int t = 1; for (int i = 0; i < n; i++) { long long k = 1; for (int j = 0; j < n; j++) { if (gcd[i][j]) { k = LCM(k, gcd[i][j]); } } ans[i] = k; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (gcd[i][j] && gcd[i][j] != GCD(ans[i], ans[j])) t = 0; } } if (t) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (lcm[i][j]) { long long x = LCM(ans[i], ans[j]); if (x > lcm[i][j] || ((long long)lcm[i][j] % x)) t = 0; lcm[i][j] /= x; } } } int y; if (t) { for (int i = 0; i < n; i++) { mul[i] = 0; for (int j = 0; j < n; j++) { if (lcm[i][j]) { mul[i] = GCD(mul[i], lcm[i][j]); } } } for (int i = 0; i < n; i++) { mul[i] = 0; for (int j = 0; j < n; j++) { if (lcm[i][j]) { mul[i] = GCD(mul[i], lcm[i][j]); } } for (int j = 0; j < n; j++) { if (gcd[i][j]) { int k = lcm[i][j] / mul[i]; int p = k; while (k > 1) { int y = divs[k]; while (k % y == 0) { k /= y; lcm[i][j] /= y; } while (lcm[i][j] % y == 0) { lcm[i][j] /= y; p *= y; } } lcm[j][i] = lcm[i][j]; ans[j] *= p; if (mul[j] % p) { t = 0; break; } mul[j] /= p; for (int k = 0; k < n; k++) { if (k != i) { lcm[j][k] /= p; lcm[k][j] /= p; } } } } } for (int i = 0; i < n; i++) { int k = 0; for (int j = 0; j < n; j++) { if (lcm[i][j]) { k = GCD(k, lcm[i][j]); } } if (!k) k = 1; for (int j = 0; j < n; j++) { int x = lcm[i][j] / k; while (x > 1) { y = divs[x]; while (k % y == 0) k /= y; x /= y; } } ans[i] *= k; for (int j = 0; j < n; j++) { lcm[i][j] /= k; lcm[j][i] /= k; } } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (lcm[i][j] > 1) t = 0; if (gcd[i][j] && gcd[i][j] != GCD(ans[i], ans[j])) t = 0; } } } if (!t) cout << "NO" << endl; else { cout << "YES" << endl; for (int i = 0; i < n; i++) { if (i) cout << " "; cout << ans[i]; } cout << endl; } return 0; }
13
#include <bits/stdc++.h> using namespace std; struct myComparator { bool operator()(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) { if (a.first == b.first) { return a.second.first > b.second.first; } return a.first < b.first; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tc; cin >> tc; while (tc--) { int n; cin >> n; vector<int> ans(n + 1); priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, myComparator> max_heap; max_heap.push({n, {1, n}}); int pos = 1; while (!max_heap.empty()) { pair<int, pair<int, int> > val = max_heap.top(); max_heap.pop(); int l = val.second.first; int r = val.second.second; int mid = (l + r) / 2; ans[mid] = pos; if (l <= mid - 1) max_heap.push({mid - l, {l, mid - 1}}); if (mid + 1 <= r) max_heap.push({r - mid, {mid + 1, r}}); ++pos; } for (int i = 1; i <= n; ++i) { cout << ans[i] << " "; } cout << endl; } cout << endl; }
8
#include <bits/stdc++.h> using namespace std; map<int, long long> A[2], sum[2]; int ansMax = -1; long long ans, total[2]; void update(int m, long long ways) { if (m > ansMax) { ansMax = m; ans = ways; } else if (m == ansMax) { ans += ways; } } int main() { int N, M; scanf("%d%d", &N, &M); for (int i = 0; i < M; ++i) { int a, b; scanf("%d%d", &a, &b); ++A[a][b]; ++total[a]; } for (int i = 0; i < 2; ++i) { long long s = 0; for (auto j = A[i].begin(); j != A[i].end(); ++j) { sum[i][j->first] = s; s += j->second; } } for (int i = 0; i < 2; ++i) { if (A[i].empty()) continue; auto st = A[i].begin(); auto en = A[i].rbegin(); int cnt = 2 * (en->first - st->first); long long ways = !cnt ? st->second * (st->second - 1) * (st->second - 2) / 6 : st->second * en->second * (total[i] - st->second - en->second) + st->second * (st->second - 1) / 2 * en->second + en->second * (en->second - 1) / 2 * st->second; update(cnt, ways); if (A[1 - i].empty()) continue; for (auto j = A[i].begin(); j != A[i].end(); ++j) { auto p = sum[1 - i].lower_bound(j->first); auto same = A[1 - i].lower_bound(j->first); if (p != sum[1 - i].end() && p != sum[1 - i].begin()) { ways = p->second * (total[1 - i] - p->second); if (p->first == j->first) { ways += same->second * (same->second - 1) / 2 + same->second * (total[1 - i] - p->second - same->second); } cnt = 2 * N; } else { if (p == sum[1 - i].end()) { --p; --same; } cnt = 2 * (N - abs(p->first - j->first)); ways = same->second * (same->second - 1) / 2 + (total[1 - i] - same->second) * same->second; } update(cnt, ways * j->second); } } printf("%lld\n", ans); }
20
#include <bits/stdc++.h> using namespace std; int n; int a[100005]; int dp[100005][6]; int solve(int i, int previous) { if (i == n - 1) { return dp[i][previous] = 1; } if (dp[i][previous] != -1) return dp[i][previous]; dp[i][previous] = 0; for (int j = 1; j <= 5; j++) { if (a[i] < a[i + 1] && j > previous) { dp[i][previous] = max(solve(i + 1, j) + 1, dp[i][previous]); } else if (a[i] > a[i + 1] && j < previous) { dp[i][previous] = max(solve(i + 1, j) + 1, dp[i][previous]); } else if (a[i] == a[i + 1] && j != previous) { dp[i][previous] = max(solve(i + 1, j) + 1, dp[i][previous]); } } return dp[i][previous]; } bool k = false; void build(int i, int previous) { if (i == n - 1) { cout << previous << " "; k = true; return; } for (int j = 1; j <= 5; j++) { if (a[i] < a[i + 1] && j > previous && dp[i][previous] == solve(i + 1, j) + 1) { cout << previous << " "; build(i + 1, j); return; } else if (a[i] > a[i + 1] && j < previous && dp[i][previous] == solve(i + 1, j) + 1) { cout << previous << " "; build(i + 1, j); return; } else if (a[i] == a[i + 1] && j != previous && dp[i][previous] == solve(i + 1, j) + 1) { cout << previous << " "; build(i + 1, j); return; } } return; } int main() { memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int m = 0; for (int i = 1; i <= 5; i++) { m = max(m, solve(0, i)); } if (m != n) cout << -1; else { for (int j = 1; j <= 5; j++) { if (dp[0][j] == m) build(0, j); if (k) return 0; } } }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; set<pair<int, int>> curr; set<pair<int, int>> deleted; for (int i = 0; i < n; ++i) { int left, right; cin >> left >> right; auto it = curr.lower_bound({left, -1}); if (it != curr.begin()) { auto it1 = it; --it1; if (it1->second >= left) { left = it1->first; --it; } } while (it != curr.end() && right >= it->second) { deleted.insert(*it); ++it; } if (it != curr.end() && it->first <= right) { right = it->second; deleted.insert(*it); } for (const auto& d : deleted) { curr.erase(d); } deleted.clear(); curr.insert({left, right}); cout << curr.size() << "\n"; } }
13
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; const int N = 1e5 + 100; const int ITERS = 60; const double eps = 1e-9; double sqr(double x) { return x * x; } struct point { double x, y; point() {} point(double _x, double _y) : x(_x), y(_y) {} friend point operator/(const point& p, double d) { return point(p.x / d, p.y / d); } friend point operator+(const point& a, const point& b) { return point(a.x + b.x, a.y + b.y); } friend point operator-(const point& a, const point& b) { return point(a.x - b.x, a.y - b.y); } friend point operator*(const point& p, double d) { return point(p.x * d, p.y * d); } double norm() const { return sqrt(sqr(x) + sqr(y)); } }; int n, k; vector<pair<point, pair<int, int>>> p; bool can[N]; double x[N], y[N]; void init() {} bool eq(double a, double b) { return fabs(a - b) < eps; } int half(const point& p) { if (p.y > -eps) { return 0; } return 1; } double vec(const point& a, const point& b) { return a.x * b.y - b.x * a.y; } bool vec_cmp(const pair<point, pair<int, int>>& a, const pair<point, pair<int, int>>& b) { int ha = half(a.first), hb = half(b.first); if (ha != hb) { return ha < hb; } double v = vec(a.first, b.first); if (v > eps) { return true; } if (v < -eps) { return false; } return a.second < b.second; } void build_inter(point p, double r, point* res) { point mid = p / 2; double d = sqrt(max(sqr(r) - sqr(mid.x) - sqr(mid.y), 0.0)); if (eq(d, 0.0)) { res[0] = res[1] = mid; return; } point v(mid.y, -mid.x); v = v / v.norm(); res[0] = mid + v * d; res[1] = mid - v * d; } bool check(double r) { p.clear(); for (int i = 0; i < n; ++i) { can[i] = false; if (eq(x[i], 0) && eq(y[i], 0)) { continue; } if (sqr(x[i]) + sqr(y[i]) - 4 * sqr(r) > eps) { continue; } point inter[2]; build_inter(point(x[i], y[i]), r, inter); assert(eq(inter[0].norm(), r)); assert(eq(inter[1].norm(), r)); assert(eq((inter[0] - point(x[i], y[i])).norm(), r)); assert(eq((inter[1] - point(x[i], y[i])).norm(), r)); p.push_back(make_pair(inter[0], make_pair(i, 0))); p.push_back(make_pair(inter[1], make_pair(i, 1))); } sort((p).begin(), (p).end(), vec_cmp); int nk = k; for (int it = 0; it < 2; ++it) { int i = 0; while (i < ((int)(p).size())) { int j = i; while (j + 1 < ((int)(p).size()) && eq(p[i].first.x, p[j + 1].first.x) && eq(p[i].first.y, p[j + 1].first.y)) { ++j; } for (int k = i; k <= j; ++k) { if (p[k].second.second == 0) { if (!can[p[k].second.first]) { can[p[k].second.first] = true; --nk; if (nk <= 0) { return true; } } } } for (int k = i; k <= j; ++k) { if (p[k].second.second == 1) { if (can[p[k].second.first]) { can[p[k].second.first] = false; ++nk; } } } i = j + 1; } } return false; } void solve() { scanf("%d %d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%lf %lf", x + i, y + i); if (eq(x[i], 0) && eq(y[i], 0)) { --k; } } if (k <= 0) { printf("0\n"); return; } double l = 0, r = 2e5; for (int it = 0; it < ITERS; ++it) { double mid = (l + r) / 2; if (check(mid)) { r = mid; } else { l = mid; } } printf("%.09lf\n", (l + r) / 2); } int main() { init(); int t = 1; for (int i = 0; i < t; ++i) { solve(); } return 0; }
15
#include <bits/stdc++.h> using namespace std; int mymin(vector<long long int> v) { return *min_element(v.begin(), v.end()); } long long int mymax(vector<long long int> v) { return *max_element(v.begin(), v.end()); } long long int power(long long int a, long long int b, long long int p) { long long int res = 1; a %= p; while (b != 0) { if (b % 2) res = (res % p * a % p) % p; b /= 2; a = (a % p * a % p) % p; } return res; } vector<string> split(string str, char delimeter) { vector<string> res; istringstream iss(str); string token; while (getline(iss, token, delimeter)) { if (token.length() > 0) res.push_back(token); } return res; } void randomize(vector<int> &arr) { srand(time(NULL)); for (int i = arr.size() - 1; i > 0; i--) { int j = rand() % (i + 1); swap(arr[i], arr[j]); } } long long int mgcd(long long int a, long long int b) { if (a == 0) return b; return mgcd(b % a, a); } int main() { clock_t tstart = clock(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; string grid[n]; for (int i = 0; i < n; i++) cin >> grid[i]; bool is_visited[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) is_visited[i][j] = 0; vector<vector<int> > vec; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == '*') { int length = 0; bool is_valid = true; while (is_valid) { length++; if (i + length >= n || i - length < 0) { is_valid = false; goto lp; } if (j + length >= m || j - length < 0) { is_valid = false; goto lp; } for (int k = i; k <= i + length; k++) if (grid[k][j] == '.') { is_valid = false; break; } for (int k = i; k >= i - length; k--) if (grid[k][j] == '.') { is_valid = false; break; } for (int k = j; k <= j + length; k++) if (grid[i][k] == '.') { is_valid = false; break; } for (int k = j; k >= j - length; k--) if (grid[i][k] == '.') { is_valid = false; break; } if (is_valid) { for (int k = i; k <= i + length; k++) is_visited[k][j] = true; for (int k = i; k >= i - length; k--) is_visited[k][j] = true; for (int k = j; k <= j + length; k++) is_visited[i][k] = true; for (int k = j; k >= j - length; k--) is_visited[i][k] = true; } } lp: length--; if (length > 0) vec.push_back({i + 1, j + 1, length}); } } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (grid[i][j] == '*' && is_visited[i][j] == false) { cout << "-1"; return 0; } } cout << vec.size() << "\n"; for (int i = 0; i < vec.size(); i++) cout << vec[i][0] << " " << vec[i][1] << " " << vec[i][2] << "\n"; return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long MAXN = 30; map<string, long long> mp; set<string> name; set<string> st[MAXN]; bool suffix(string &s, string &t) { if (t.size() > s.size()) return false; long long tln = t.size(), sln = s.size(); for (long long i = sln - tln, j = 0; i < sln; j++, i++) if (s[i] != t[j]) return false; return true; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; for (long long i = 0; i < n; i++) { string s, num; long long t, ind; cin >> s >> t; if (name.find(s) == name.end()) { name.insert(s); mp[s] = name.size(); } ind = mp[s]; while (t--) { cin >> num; st[ind].insert(num); } } for (auto x : mp) { set<string> mrk; for (auto s : st[x.second]) { if (mrk.find(s) != mrk.end()) continue; for (auto t : st[x.second]) { if (s == t || mrk.find(t) != mrk.end()) continue; if (suffix(s, t)) mrk.insert(t); } } for (auto s : mrk) st[x.second].erase(s); } cout << mp.size() << endl; for (auto x : mp) { cout << x.first << " " << st[x.second].size() << " "; for (auto s : st[x.second]) cout << s << " "; cout << endl; } }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, s; cin >> n >> s; long long sum = 1000000 - s; pair<long double, long long> p[n]; for (int i = 0; i < n; i++) { cin >> x >> y >> p[i].second; x = abs(x); y = abs(y); p[i].first = sqrt(x * x + y * y); } sort(p, p + n); for (int i = 0; i < n; i++) { sum -= p[i].second; if (sum <= 0) { cout << fixed << setprecision(7) << p[i].first; return 0; } } cout << -1; }
4
#include <bits/stdc++.h> using namespace std; const int NMAX = 2010; const int DX[4] = {-1, 0, 0, 1}; const int DY[4] = {0, -1, 1, 0}; int N, M, Deg[NMAX][NMAX]; char A[NMAX][NMAX]; bool Used[NMAX][NMAX]; queue<pair<int, int> > Q; int main() { scanf("%i %i\n", &N, &M); for (int i = 1; i <= N; ++i) gets(A[i] + 1); for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) { if (A[i][j] == '*') continue; for (int k = 0; k < 4; ++k) { int ni = i + DX[k], nj = j + DY[k]; if (1 <= ni && ni <= N && 1 <= nj && nj <= M && A[ni][nj] == '.') Deg[i][j]++; } if (Deg[i][j] == 1) Q.push(make_pair(i, j)); } while (!Q.empty()) { int X = Q.front().first, Y = Q.front().second, NX, NY, NNX, NNY; Q.pop(); if (Deg[X][Y] != 1 || Used[X][Y]) continue; for (int i = 0; i < 4; ++i) { NX = X + DX[i], NY = Y + DY[i]; if (1 <= NX && NX <= N && 1 <= NY && NY <= M && !Used[NX][NY] && A[NX][NY] == '.') break; } Used[X][Y] = Used[NX][NY] = 1; Deg[X][Y] = Deg[NX][NY] = 0; if (X == NX) { A[X][min(Y, NY)] = '<'; A[X][max(Y, NY)] = '>'; } else { A[min(X, NX)][Y] = '^'; A[max(X, NX)][Y] = 'v'; } for (int i = 0; i < 4; ++i) { NNX = NX + DX[i], NNY = NY + DY[i]; if (1 <= NNX && NNX <= N && 1 <= NNY && NNY <= M && !Used[NNX][NNY] && A[NNX][NNY] == '.') { Deg[NNX][NNY]--; if (Deg[NNX][NNY] == 1) Q.push(make_pair(NNX, NNY)); } } } for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) if (A[i][j] == '.') { printf("Not unique\n"); return 0; } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) printf("%c", A[i][j]); printf("\n"); } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 5e5; const int M = N + 7; const int MOD = 1e9 + 7; const long long INF = 1e16 + 17; int st[M], ft[M], tim, ver[M]; int sz[M], big[M]; int h[M], xr[M]; vector<pair<int, int> > g[M]; int mask[M * 10]; int ans[M]; void preCalc(int v) { st[v] = ++tim; ver[tim] = v; sz[v] = 1; for (auto u : g[v]) { h[u.first] = h[v] + 1; xr[u.first] = xr[v] ^ (1 << u.second); preCalc(u.first); sz[v] += sz[u.first]; if (sz[u.first] > sz[big[v]]) { big[v] = u.first; } } ft[v] = tim; } void dfs(int v, int keep = 0) { for (auto u : g[v]) { if (u.first != big[v]) { dfs(u.first); ans[v] = max(ans[v], ans[u.first]); } } if (big[v]) dfs(big[v], 1); ans[v] = max(ans[v], ans[big[v]]); for (int i = -1; i < 22; ++i) { int msk = (i >= 0 ? 1 << i : 0); msk ^= xr[v]; ans[v] = max(ans[v], mask[msk] - h[v]); } mask[xr[v]] = max(mask[xr[v]], h[v]); for (auto u : g[v]) { if (u.first == big[v]) continue; for (int i = st[u.first]; i <= ft[u.first]; ++i) { int to = ver[i]; for (int j = -1; j < 22; ++j) { int msk = j >= 0 ? 1 << j : 0; msk = msk ^ xr[to]; if (mask[msk]) ans[v] = max(ans[v], mask[msk] - h[v] + h[to] - h[v]); } } for (int i = st[u.first]; i <= ft[u.first]; ++i) { int to = ver[i]; mask[xr[to]] = max(mask[xr[to]], h[to]); } } if (!keep) { for (int i = st[v]; i <= ft[v]; ++i) { int u = ver[i]; mask[xr[u]] = 0; } } } int main() { int n; scanf("%d", &n); for (int i = 2; i <= n; ++i) { int par; char x; scanf("%d %c", &par, &x); g[par].push_back(make_pair(i, x - 'a')); } h[1] = 1; preCalc(1); dfs(1); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); printf("\n"); return 0; }
21
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const long long INF = 1000000000000; const long long F = LLONG_MIN; const int A = 1234567; long long sum(long long num) { long long re = 0; while (num != 0) { re += num % 10; num /= 10; } return re; } int main() { long long a, b, out = 0; cin >> a >> b; long long start = 1, end = a; while (start <= end) { long long mid = (start + end) / 2; if (mid - sum(mid) >= b) { out = mid; end = mid - 1; } else start = mid + 1; } if (out != 0) cout << a - out + 1; else cout << 0; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; while (n--) { string s; cin >> s; int m = s.length(); int f = 1; for (char ch = 'a'; ch <= 'z'; ch++) { if (count(s.begin(), s.end(), ch) == 1) { if (ch + m - 1 > 'z') { f = 0; break; } for (char c = ch; c <= ch + m - 1; c++) { if (count(s.begin(), s.end(), c) != 1) { f = 0; break; } } break; } if (count(s.begin(), s.end(), ch) > 1) { f = 0; break; } } if (!f) cout << "NO" << endl; else cout << "YES" << endl; } }
0
#include <bits/stdc++.h> using namespace std; vector<int> a[100010]; int n, m, i, x, y, ans; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> x >> y; a[x].push_back(y); a[y].push_back(x); } for (int i = 1; i <= n; ++i) ans += a[i].size() % 2; ans /= 2; for (int i = 1; i <= n; ++i) if (a[i].size() == 2) if (x = a[i][0], y = a[i][1], x == y) a[x].clear(), ans++; else a[x][a[x][1] == i] = y, a[y][a[y][1] == i] = x; cout << ans << ' ' << m << endl; }
19
#include <bits/stdc++.h> using namespace std; char ch[2010]; int i, j, n, g[2010][2010], f[2010][2010], ans; int main() { scanf("%s", ch + 1); n = strlen(ch + 1); for (i = 0; i <= n + 1; i++) { if (ch[i] == '(') break; f[i][0] = 1; } for (i = n + 1; i >= 1; i--) { if (ch[i] == ')') break; g[i][0] = 1; } for (i = 1; i <= n; i++) for (j = 1; j <= i; j++) { if (ch[i] == '(') f[i][j] = f[i - 1][j - 1]; else if (ch[i] == ')') f[i][j] = f[i - 1][j]; else f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) % 998244353; } for (i = n; i >= 1; i--) for (j = 1; j <= n - i + 1; j++) { if (ch[i] == ')') g[i][j] = g[i + 1][j - 1]; else if (ch[i] == '(') g[i][j] = g[i + 1][j]; else g[i][j] = (g[i + 1][j] + g[i + 1][j - 1]) % 998244353; } for (i = 1; i < n; i++) { for (j = 1; j <= min(i, n - i); j++) { ans += 1ll * f[i][j] * g[i + 1][j] % 998244353 * j % 998244353; ans %= 998244353; } } printf("%d\n", ans); }
18
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { return a > b ? a : b; } void solve() { int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); long long int ans1, ans2, ans3; ans1 = a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4] * a[n - 5]; ans2 = a[0] * a[1] * a[n - 1] * a[n - 2] * a[n - 3]; ans3 = a[0] * a[1] * a[2] * a[3] * a[n - 1]; cout << max(max(ans1, ans2), ans3); cout << endl; } int main() { int t = 1; cin >> t; while (t--) solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; int dif = s - n + 1; for (int k = 1; k < s; k++) { int y = s - k; if (k <= n - 1 || y <= n - 1) continue; if (k >= dif || y >= dif) continue; cout << "YES" << '\n'; for (int i = 1; i < n; i++) cout << 1 << ' '; cout << dif << '\n'; cout << k << '\n'; return 0; } cout << "NO" << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } long long x = 0; inline long long first(long long t) { return abs(x - 2 * t); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tc = 1; cin >> tc; while (tc--) { long long n, m; cin >> n >> m; vector<pair<long long, long long> > v(n), ranges(n), ranget(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; long long lb = 0, ub = 0; x = n * m; for (int i = 0; i < n; i++) { lb += max(0LL, m - v[i].second); ub += min(m, v[i].first); ranget[i] = {max(0LL, m - v[i].second), min(m, v[i].first)}; ranges[i] = {lb, ub}; x += v[i].first - v[i].second; ; ; }; ; long long l = lb; long long r = ub; while (r - l > 4LL) { long long m1 = (r + l) / 2; long long m2 = (r + l) / 2 + 1; if (first(m1) > first(m2)) l = m1; else r = m2; } long long ans = LLONG_MAX; long long theta; for (long long i = l; i <= r; i++) { if (ans > first(i)) { ans = first(i); theta = i; } } cout << ans << "\n"; ; vector<pair<long long, long long> > first(n); ; for (int i = n - 1; i >= 1; i--) { if (theta - ranget[i].second < ranges[i - 1].first) { long long eata = theta - ranges[i - 1].first; long long eatb = m - eata; theta = ranges[i - 1].first; first[i] = {eata, eatb}; } else { long long eata = ranget[i].second; long long eatb = m - eata; theta -= ranget[i].second; first[i] = {eata, eatb}; } } first[0] = {theta, m - theta}; for (int i = 0; i < n; i++) cout << first[i].first << " " << first[i].second << "\n"; } }
14
#include <bits/stdc++.h> using namespace std; long long k, d[8000000]; int a[105], q; long long ans; int n; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; for (int j = 1; j * j <= a[i]; j++) { d[q++] = (a[i] + j - 1) / j; d[q++] = j; } k += a[i]; } sort(d, d + q); int u = unique(d, d + q) - d; for (long long i = u - 1; i >= 0; i--) { long long v = 0; for (int j = 1; j <= n; j++) { v += ((a[j] + d[i] - 1) / d[i]); } if (v * d[i] <= k) { printf("%I64d\n", k / v); break; } } return 0; }
15
#include <bits/stdc++.h> const long long SZ = 1e5 + 1984; const long long inf = 1e18; const long long MOD = 1e9 + 7; const long long MD = 998244353; long long opnmbr = 1; using namespace std; long long powermod(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % MOD; b = b / 2; a = (a * a) % MOD; } return ans; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } long long cnt[71]; long long dp[1 << 19]; long long dp2[1 << 19]; long long primes[19] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67}; long long getMask(long long a) { long long mask = 0; for (long long i = 0; i < (19); i++) { while (a % primes[i] == 0) { mask ^= (1 << i); a /= primes[i]; } } return mask; } void krdiyasuru() { long long n; cin >> n; for (long long i = 0; i < (n); i++) { long long a; cin >> a; cnt[a]++; } long long pw[SZ]; pw[0] = 1; for (long long i = 1; i < (SZ); i++) { pw[i] = (2 * pw[i - 1]) % MOD; } dp[0] = 1; for (long long i = 1; i < (71); i++) { long long mask = getMask(i); for (long long j = 0; j < (1 << 19); j++) { dp2[j] = dp[j]; dp[j] = 0; } for (long long j = 0; j < (1 << 19); j++) { dp[j] += dp2[j] * ((cnt[i] == 0) ? 1 : pw[cnt[i] - 1]); dp[j] %= MOD; dp[j ^ mask] += dp2[j] * ((cnt[i] == 0) ? 0 : pw[cnt[i] - 1]); dp[j ^ mask] %= MOD; } } cout << dp[0] - 1 << endl; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); { krdiyasuru(); } }
12
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long int arr[n]; for (int i = 0; i < n; i++) { arr[i] = i + 2; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; int n, ta; set<int> s; set<int>::iterator it; int main() { scanf("%d", &n); while (n--) { scanf("%d", &ta); s.insert(ta); } for (it = s.begin(); it != s.end(); it++) for (int i = 0; i <= 30; i++) if (s.find(*it + (1 << i)) != s.end() && s.find(*it - (1 << i)) != s.end()) { printf("3\n%d %d %d\n", *it - (1 << i), *it, *it + (1 << i)); return 0; } for (it = s.begin(); it != s.end(); it++) for (int i = 0; i <= 30; i++) if (s.find(*it + (1 << i)) != s.end()) { printf("2\n%d %d\n", *it, *it + (1 << i)); return 0; } printf("1\n%d\n", *s.begin()); return 0; }
10
#include <bits/stdc++.h> template <class First, class Second> std::ostream& operator<<(std::ostream& out, const std::pair<First, Second>& pair) { out << pair.first << ": " << pair.second; return out; } template <class T> std::ostream& operator<<(std::ostream& out, const std::vector<T>& vec) { out << "("; if (!vec.empty()) { out << vec.front(); for (auto i = std::next(vec.begin()); i != vec.end(); ++i) { out << ", " << *i; } } out << ")"; return out; } template <class T> std::ostream& operator<<(std::ostream& out, const std::list<T>& list) { out << "("; if (!list.empty()) { out << list.front(); for (auto i = std::next(list.begin()); i != list.end(); ++i) { out << ", " << *i; } } out << ")"; return out; } template <class T> std::ostream& operator<<(std::ostream& out, const std::set<T>& set) { out << "{"; if (!set.empty()) { out << *set.begin(); for (auto i = std::next(set.begin()); i != set.end(); ++i) { out << ", " << *i; } } out << "}"; return out; } template <class K, class V> std::ostream& operator<<(std::ostream& out, const std::map<K, V>& map) { out << "{"; if (!map.empty()) { out << *map.begin(); for (auto i = std::next(map.begin()); i != map.end(); ++i) { out << ", " << *i; } } out << "}"; return out; } template <class Object> std::string str(const Object& obj) { std::ostringstream out; out << obj; return out.str(); } void print() { std::cout << "\n"; } template <typename T, typename... Args> void print(T v, Args... args) { std::cout << v << " "; print(args...); } void scan() {} template <typename T, typename... Args> void scan(T& v, Args&... args) { std::cin >> v; scan(args...); } template <class T> void scan(std::vector<T>& vec) { for (T& value : vec) { std::cin >> value; } } template <class T> void sort(std::vector<T>& vec) { sort(vec.begin(), vec.end()); } using namespace std; int main(int argc, char* argv[]) { (void)argc; (void)argv; std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; scan(n); vector<int> a(n); scan(a); vector<int64_t> pref(1, 0); for (auto x : a) { pref.push_back(pref.back() + x); } int beg = 0; int end = 1; set<int64_t> seen; seen.insert(0); int64_t res = 0; while (beg < n) { while (end <= n && !seen.count(pref[end])) { seen.insert(pref[end]); ++end; } res += end - beg - 1; seen.erase(pref[beg++]); } print(res); return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long U = 1e18; int n; vector<long long> arr, A, B; void gen(long long u, int i, vector<int> &c, vector<long long> &C) { if (i == c.size()) { C.emplace_back(u); return; } gen(u, i + 1, c, C); for (; u <= U / c[i];) { gen(u *= c[i], i + 1, c, C); } } long long get(long long k) { long long j = 0; long long ans = 0; for (int i = 0; i < A.size(); ++i) { while (j < B.size() && A[i] <= k / B[j]) j++; ans += j; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; int val, k; for (int i = 0; i < n; ++i) { cin >> val; arr.emplace_back(val); } cin >> k; vector<int> a, b; for (int i = 0; i < n; i += 2) { a.emplace_back(arr[i]); } for (int i = 1; i < n; i += 2) { b.emplace_back(arr[i]); } gen(1, 0, a, A); gen(1, 0, b, B); sort(A.begin(), A.end(), greater<long long>()); sort(B.begin(), B.end()); long long lo = 0, hi = U; while (lo != hi) { long long m = (lo + hi) / 2; if (get(m) < k) { lo = m + 1; } else hi = m; } cout << hi << '\n'; }
16
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int par[1]; int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void join(int x, int y) { par[find(x)] = y; } long long mod_expo(long long a, long long b, long long mod) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans; } long long gcd(long long a, long long b, long long& x, long long& y) { if (!a) { x = 0; y = 1; return b; } long long x1, y1; long long d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } const int N = 100005; int a[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, i, j, k, p, l; int ans, sum; cin >> n; for (i = 0; i < 2 * n; ++i) { cin >> a[i]; } sort(a, a + 2 * n); ans = INT_MAX; for (i = 0; i < 2 * n; ++i) { for (j = i + 1; j < 2 * n; ++j) { sum = 0; for (k = l = 0; k < 2 * n; ++k) { if (k == i || k == j) continue; if (l % 2) sum += a[k] - a[p]; p = k; ++l; } ans = min(ans, sum); } } cout << ans << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2000 * 100 + 10; int x1, y11111, x2, y2, dist[10][10]; pair<int, int> turnlad[4] = {make_pair(0, 1), make_pair(1, 0), make_pair(-1, 0), make_pair(0, -1)}; pair<int, int> turnslon[4] = {make_pair(1, 1), make_pair(-1, 1), make_pair(1, -1), make_pair(-1, -1)}; pair<int, int> turncrow[8] = { make_pair(0, 1), make_pair(1, 0), make_pair(-1, 0), make_pair(0, -1), make_pair(1, 1), make_pair(-1, 1), make_pair(1, -1), make_pair(-1, -1)}; void init() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { dist[i][j] = INT_MAX; } } dist[x1][y11111] = 0; } bool good(int x, int y) { return (x >= 0 && x < 8 && y >= 0 && y < 8); } int lad() { set<pair<int, pair<int, int> > > s; init(); s.insert(make_pair(0, make_pair(x1, y11111))); while (!s.empty()) { pair<int, pair<int, int> > cur = *s.begin(); s.erase(s.begin()); for (int i = -7; i < 8; i++) { if (i != 0) { for (int j = 0; j < 4; j++) { int nx = cur.second.first + turnlad[j].first * i; int ny = cur.second.second + turnlad[j].second * i; if (good(nx, ny) && dist[nx][ny] > dist[cur.second.first][cur.second.second] + 1) { s.erase(make_pair(dist[nx][ny], make_pair(nx, ny))); dist[nx][ny] = dist[cur.second.first][cur.second.second] + 1; s.insert(make_pair(dist[nx][ny], make_pair(nx, ny))); } } } } } return (dist[x2][y2] == INT_MAX ? 0 : dist[x2][y2]); } int slon() { set<pair<int, pair<int, int> > > s; init(); s.insert(make_pair(0, make_pair(x1, y11111))); while (!s.empty()) { pair<int, pair<int, int> > cur = *s.begin(); s.erase(s.begin()); for (int i = -7; i < 8; i++) { if (i != 0) { for (int j = 0; j < 4; j++) { int nx = cur.second.first + turnslon[j].first * i; int ny = cur.second.second + turnslon[j].second * i; if (good(nx, ny) && dist[nx][ny] > dist[cur.second.first][cur.second.second] + 1) { s.erase(make_pair(dist[nx][ny], make_pair(nx, ny))); dist[nx][ny] = dist[cur.second.first][cur.second.second] + 1; s.insert(make_pair(dist[nx][ny], make_pair(nx, ny))); } } } } } return (dist[x2][y2] == INT_MAX ? 0 : dist[x2][y2]); } int crow() { set<pair<int, pair<int, int> > > s; init(); s.insert(make_pair(0, make_pair(x1, y11111))); while (!s.empty()) { pair<int, pair<int, int> > cur = *s.begin(); s.erase(s.begin()); for (int j = 0; j < 8; j++) { int nx = cur.second.first + turncrow[j].first; int ny = cur.second.second + turncrow[j].second; if (good(nx, ny) && dist[nx][ny] > dist[cur.second.first][cur.second.second] + 1) { s.erase(make_pair(dist[nx][ny], make_pair(nx, ny))); dist[nx][ny] = dist[cur.second.first][cur.second.second] + 1; s.insert(make_pair(dist[nx][ny], make_pair(nx, ny))); } } } return (dist[x2][y2] == INT_MAX ? 0 : dist[x2][y2]); } int main() { scanf("%d%d%d%d", &x1, &y11111, &x2, &y2); x1--, y11111--, y2--, x2--; int ans1 = lad(); int ans2 = slon(); int ans3 = crow(); printf("%d %d %d", ans1, ans2, ans3); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { return (a / gcd(a, b)) * b; } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T bpow(T p, T e) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } template <class T> inline T gethash(T h[], T power[], int l, int r) { if (r < l) return (T)0; return h[l] - h[r + 1] * power[r - l + 1]; } struct data { data() {} bool friend operator<(data a, data b) { return 0; } }; int n, m, k; long long N, M, K; int a[10004]; int main() { int t, tc = 1; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } sort(a + 1, a + n + 1); int mx = 0; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { if (a[j] - a[i] <= k) { mx = max(mx, j - i + 1); } } } printf("%d", n - mx); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, k, a[N]; vector<int> g[N]; int dp[N], tp[N]; int Mid; int use[N]; bool vis[N]; void dfs(int u, int p) { vis[u] = true; tp[u] = 0; dp[u] = 0; if (a[u] < Mid) { tp[u] = 1; return; } int Max = 0; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == p) continue; dfs(v, u); if (tp[v] == 1) { tp[u] = 1; Max = max(Max, dp[v]); } else dp[u] += dp[v]; } dp[u] += Max + 1; } int tot; void dfs2(int u, int p, int tpp, int dpp) { if (a[u] < Mid) return; int v1 = 0, v2 = 0; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == p) continue; if (tp[v]) { if (dp[v] >= dp[v1]) { v2 = v1; v1 = v; } else if (dp[v] >= dp[v2]) v2 = v; } } if (tp[v1]) { if (tpp) tot = max(tot, max(dpp, dp[v1]) + dp[u] - dp[v1]); else tot = max(tot, dpp + dp[u]); } else tot = max(tot, dpp + dp[u]); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == p) continue; if (tp[v1] == 0) dfs2(v, u, tpp, dpp + dp[u] - dp[v]); else { if (v == v1) { if (tp[v2] == 0) dfs2(v, u, tpp, dpp + dp[u] - dp[v1]); else { if (tpp) dfs2(v, u, tpp, max(dpp, dp[v2]) + dp[u] - dp[v1]); else dfs2(v, u, 1, dpp + dp[u] - dp[v1] + dp[v2]); } } else { if (tp[v]) { if (tpp) dfs2(v, u, tpp, max(dpp, dp[v1]) + dp[u] - dp[v1]); else dfs2(v, u, 1, dpp + dp[u]); } else { if (tpp) dfs2(v, u, tpp, max(dpp, dp[v1]) + dp[u] - dp[v1] - dp[v]); else dfs2(v, u, 1, dpp + dp[u] - dp[v]); } } } } } int cal(int mid) { Mid = mid; int ans = 0; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { if (a[i] < Mid) continue; if (vis[i]) continue; dfs(i, i); tot = 0; dfs2(i, i, 0, 0); ans = max(tot, ans); } return ans; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int u, v; for (int i = 1; i <= n - 1; i++) { scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } int l = 1, r = 1000001; while (l < r) { int mid = (l + r) >> 1; if (cal(mid) >= k) l = mid + 1; else r = mid; } printf("%d\n", l - 1); return 0; }
18
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5; long long N, M; long long A[MAXN]; long long r[MAXN]; bool is_good(long long t) { memcpy(r, A, sizeof(A)); for (long long i = N, s = M, b = 0; i > 0;) { if (r[i]) { if (!b) { if (!s) { return false; } b = t - i; s--; } long long v = min(b, r[i]); b -= v; r[i] -= v; } else { i--; } } return true; } int main() { ios_base::sync_with_stdio(0); cin >> N >> M; for (long long i = 1; i <= N; i++) { cin >> A[i]; } long long mi = 0, ma = 1e18; while (ma - mi > 1) { long long md = (mi + ma) / 2; if (is_good(md)) ma = md; else mi = md; } cout << ma << '\n'; return 0; }
14
#include <bits/stdc++.h> using namespace std; const long long N = 300000 + 77; long long n, Prof, a[N], b[N]; long long A, Profit[N], Mx[N << 2], lz[N << 2]; vector<pair<long long, long long> > V; set<long long> S; vector<pair<long long, long long> > Ql[N], Qr[N]; inline void Shift(long long l, long long r, long long id) { long long x = lz[id]; lz[id] = 0; Mx[id] += x; if (r - l > 1) lz[id << 1] += x, lz[id << 1 ^ 1] += x; } void Add(long long ql, long long qr, long long x, long long l = 1, long long r = n + 1, long long id = 1) { Shift(l, r, id); if (qr <= l || r <= ql) return; if (ql <= l && r <= qr) { lz[id] += x; Shift(l, r, id); return; } long long mid = ((l + r) >> 1); Add(ql, qr, x, l, mid, id << 1); Add(ql, qr, x, mid, r, id << 1 ^ 1); Mx[id] = max(Mx[id << 1], Mx[id << 1 ^ 1]); } long long Get(long long ql, long long qr, long long l = 1, long long r = n + 1, long long id = 1) { Shift(l, r, id); if (qr <= l || r <= ql) return -1e18; if (ql <= l && r <= qr) return Mx[id]; long long mid = ((l + r) >> 1); return max(Get(ql, qr, l, mid, id << 1), Get(ql, qr, mid, r, id << 1 ^ 1)); } int32_t main() { scanf("%d %d", &n, &Prof); for (long long i = 1; i <= n; ++i) { scanf("%d %d", b + i, a + i); a[i] = -a[i] + Prof; A = max(A, a[i] * 1ll); if (i > 1) V.push_back(make_pair(abs(b[i] - b[i - 1]), i)); } S.insert(n + 1); S.insert(0); sort(V.begin(), V.end()); reverse(V.begin(), V.end()); for (auto t : V) { long long d = t.first, id = t.second; auto it = S.lower_bound(id); long long limitR = (*it) - 1; it--; long long limitL = (*it); S.insert(id); Profit[id] -= d * 1ll * d; Ql[id - 1].push_back(make_pair(id, limitL)); Qr[id].push_back(make_pair(id, limitR)); } for (long long i = 1; i <= n; ++i) { Add(1, i + 1, a[i]); for (auto x : Ql[i]) Profit[x.first] += Get(x.second, i + 1); } memset(Mx, 0, sizeof(Mx)); memset(lz, 0, sizeof(lz)); for (long long i = n; i > 0; --i) { Add(i, n + 1, a[i]); for (auto x : Qr[i]) Profit[x.first] += Get(i, x.second + 1); } for (long long i = 2; i <= n; ++i) A = max(A, Profit[i]); printf("%lld\n", A); return 0; }
16
#include <bits/stdc++.h> using namespace std; int N; vector<pair<int, int> > edge[110]; void dfs(int x, int first, double len) { double step = 2. / (edge[x].size()); double cur = len; for (int i = 0; i < edge[x].size(); ++i) { if (edge[x][i].first == first) continue; printf("1 %d ", edge[x][i].second + 1); cur += step; if (cur > 1.) cur -= 2.; if (cur >= 0.) { printf("%d %d %.10lf\n", x, edge[x][i].first, cur); } else { printf("%d %d %.10lf\n", edge[x][i].first, x, 1 + cur); } dfs(edge[x][i].first, x, cur >= 0. ? cur - 1. : cur + 1.); } } int main() { scanf("%d", &N); for (int i = 0; i < N - 1; ++i) { int u, v; scanf("%d %d", &u, &v); edge[u].push_back(make_pair(v, i)); edge[v].push_back(make_pair(u, i)); } printf("%d\n", N - 1); dfs(1, -1, 0.0); }
17
#include <bits/stdc++.h> using namespace std; vector<int> v[100], a[100]; int mark[50] = {0}; void dfs(int i, int cnt) { a[cnt].push_back(i); mark[i] = 1; int s = v[i].size(); for (int j = 0; j < s; j++) { if (!mark[v[i][j]]) dfs(v[i][j], cnt); } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); v[x].push_back(y); v[y].push_back(x); } int cnt = 0; for (int i = 1; i < n + 1; i++) { if (!mark[i] && v[i].size()) { dfs(i, cnt); cnt++; } } if (cnt > n / 3) { cout << -1; return 0; } for (int i = 0; i < cnt + 1; i++) { if (a[i].size() > 3) { cout << -1; return 0; } } vector<int> b; for (int i = 1; i < n + 1; i++) { if (mark[i] == 0) { b.push_back(i); } } cnt -= 1; int k = 0; for (int i = 0; i < n / 3; i++) { if (cnt >= 0) { int g = a[cnt].size(); if (g == 3) { cout << a[cnt][0] << " " << a[cnt][1] << " " << a[cnt][2] << endl; } else if (g == 2) { cout << a[cnt][0] << " " << a[cnt][1] << " " << b[k++] << endl; } else if (g == 1) { cout << a[cnt][0] << " "; cout << b[k++] << " "; cout << b[k++] << endl; } cnt--; } else { cout << b[k++] << " "; cout << b[k++] << " "; cout << b[k++] << endl; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int inf = 1e9, mod = 1e9 + 7; const int N = 5e5 + 5; set<pair<int, int> > S; vector<pair<int, int> > v; int h[N], a[N], x, n; long long ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); S.insert(make_pair(i, a[i])); v.push_back(make_pair(a[i], i)); } set<pair<int, int> >::iterator it, it2, it3; sort(v.begin(), v.end()); for (__typeof(v.begin()) c = v.begin(); c != v.end(); c++) { int index = c->second; if (index == 1 || index == n) continue; it2 = S.lower_bound(make_pair(index, 0)); it = it2; it3 = it2; it--; it3++; int cost = min(it->second, it3->second); if (it2->second > cost) continue; ans += cost; S.erase(it2); } vector<int> v2; for (__typeof(S.begin()) it = S.begin(); it != S.end(); it++) v2.push_back(it->second); sort(v2.begin(), v2.end()); for (int i = 0; i <= (int)v2.size() - 3; i++) ans += v2[i]; cout << ans << '\n'; return 0; }
17
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 29; const double dinf = 1e30; const long long linf = 1LL << 55; string s; int cnt[20]; int main() { while (cin >> s) { memset(cnt, 0, sizeof(cnt)); bool found = false; int wenhao = 0; for (int i = 0; i < s.length(); i++) { wenhao += (s[i] == '?'); if (isalpha(s[i])) cnt[s[i] - 'A']++; } if (isalpha(s[0])) found = true; long long ans = 1; if (s[0] == '?') ans = 9, wenhao--; int count = 0; for (int i = 0; i < 20; i++) { count += (cnt[i] > 0); } int temp = 10; if (found) { ans *= 9; temp--; for (int i = 2; i <= count; i++) { ans *= temp, temp--; } } else { for (int i = 1; i <= count; i++) ans *= temp, temp--; } cout << ans; string tmp(wenhao, '0'); cout << tmp << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; inline void read_string_to_vector(vector<string> &vec) { string str; getline(cin, str); vec.push_back(str); } template <typename T> inline void read_digit_to_vector(vector<T> &vec) { T t; cin >> t; vec.push_back(t); } long long n, l, r, Ql, Qr; long long w[300001]; long long dp[300001][2]; long long ls[300001]; long long rs[300001]; long long res = 1e15; int main() { ios::sync_with_stdio(false); cin >> n >> l >> r >> Ql >> Qr; for (register long long i = 0; i < n; ++i) cin >> w[i]; for (register long long i = 1; i < n + 1; ++i) ls[i] = ls[i - 1] + w[i - 1]; for (register long long i = 1; i < n + 1; ++i) rs[i] = rs[i - 1] + w[n - i]; for (register long long i = 0; i < n + 1; ++i) { long long ck = l * ls[i] + rs[n - i] * r; if (i < n - i) ck += Qr * (n - (i << 1) - 1); if (i > n - i) ck += Ql * ((i << 1) - n - 1); res = min(ck, res); } cout << res << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; int tbl[256]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; tbl[s[0]]++; } int count = 0; for (int c = 'a'; c <= 'z'; c++) { if (tbl[c]) { int a = tbl[c] / 2 - 1; int b = tbl[c] - tbl[c] / 2 - 1; count += a * (a + 1) / 2; count += b * (b + 1) / 2; } } cout << count << endl; }
1
#include <bits/stdc++.h> using namespace std; int n, k; int oldmem[10010]; int newmem[10010]; bool ok[10010]; int procinc(int pos) { int respos = 1; while (oldmem[pos] + oldmem[respos++] > n - pos) ; newmem[pos] += oldmem[--respos]; if (newmem[pos] == n - pos) ok[pos] = 1; return respos; } bool isok() { bool res = true; for (int i = 1; i <= n; i++) res = res && ok[i]; return res; } int main() { cin >> n >> k; ok[n] = 1; for (int i = 1; i < n; i++) { oldmem[i] = 1; newmem[i] = 1; } int iter = 0; while (!isok()) { for (int i = 1; i < n; i++) printf("%d ", procinc(i)); for (int i = 1; i < n; i++) oldmem[i] = newmem[i]; printf("%d\n", n); iter++; } for (int i = iter; i < k; i++) { for (int i = 0; i < n; i++) printf("%d ", n); printf("\n"); } return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("-Ofast") using namespace std; long long int findgcd(long long int a, long long int b) { if (b == 0) return a; return findgcd(b, a % b); } bool check(long long int n, long long int s) { long long int tem = n; long long int sum = 0; while (tem > 0) { sum += tem % 10; tem = tem / 10; } if (abs(n - sum) >= s) return true; else return false; } bool cmp(vector<int> a, vector<int> b) { if (a[0] == b[0]) return a[1] < b[1]; else return a[0] < b[0]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; t = 1; for (int i1 = 0; i1 < t; i1++) { int n; cin >> n; vector<vector<int> > arr(n, vector<int>(2)), arrn; for (int i = 0; i < n; i++) cin >> arr[i][0] >> arr[i][1]; sort(arr.begin(), arr.end(), cmp); int mini = arr[0][1]; int ans = 1; for (int i = 1; i < n; i++) { if (arr[i][0] > mini) { ans++; mini = arr[i][1]; } else mini = min(arr[i][1], mini); } cout << ans; } }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int sum = 0, l = 0, r = n - 1; while (1) { if (a[l] <= k) { sum++; l++; } else if (a[r] <= k) { sum++; r--; } else break; if (r < l) break; } cout << sum << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long pot[100005]; int nw[100005]; long long mypow(long long x) { if (pot[x] != -1) return pot[x]; if (!x) return 1; return pot[x] = (mypow(x - 1) * 2) % MOD; } int main() { memset(pot, -1, sizeof(pot)); long long n, a, b, l = 0; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> a >> b; if (!nw[a]) l++; else if (nw[a] == 1) l--; if (!nw[b]) l++; else if (nw[b] == 1) l--; nw[a]++; nw[b]++; } a = 0; for (int i = 1; i <= n; i++) { if (nw[i] == 1) a += mypow(n - l + 1); else a += mypow(n - l); a %= MOD; } cout << a << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } inline int readInt() { int x; scanf("%d", &x); return x; } double bin_pow(double a, long long b) { double res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } long long dp[60]; int main() { long long x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; long long h = abs(y1 - y2); long long w = abs(x1 - x2); if (w % 2 == 1) { cout << (2 + (h - 2) / 2) * (w / 2) + (1 + (h - 2) / 2) * (w / 2) << endl; } else { cout << (2 + (h - 2) / 2) * (w / 2 + 1) + (1 + (h - 2) / 2) * (w / 2) << endl; } return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { float r, d; cin >> r >> d; float y = r, x = r - d; int n; cin >> n; int t = 0; while (n--) { int a, b, rr; cin >> a >> b >> rr; float dist = sqrt(a * a + b * b); if (dist - rr >= x && dist + rr <= y) { t++; } } cout << t; }
3
#include <bits/stdc++.h> using namespace std; int n, k, q, ti[30005], vi, wi, xi, v[15005], w[15005], st[15005], en[15005]; vector<int> tree[4 * 30005]; int dp[30][1005]; void add(int i, int L, int R, int p, int q, int y) { if ((q < L) || (R < p)) return; if ((p <= L) && (R <= q)) { tree[i].push_back(y); return; } int mid = (L + R) / 2; add(2 * i, L, mid, p, q, y); add(2 * i + 1, mid + 1, R, p, q, y); } void solve(int i, int L, int R, int d) { for (int j = 0; j < tree[i].size(); j++) for (int z = k; z >= 0; z--) if (z >= w[tree[i][j]]) dp[d][z] = max(dp[d][z], dp[d][z - w[tree[i][j]]] + v[tree[i][j]]); if (L == R) { if (ti[L] == 3) { long long r = 0, p = 1; for (int j = 1; j <= k; j++) { r += dp[d][j] * p; r %= 1000000007; p *= 10000019; p %= 1000000007; } printf("%I64d\n", r); } return; } int mid = (L + R) / 2; for (int z = 0; z <= k; z++) dp[d + 1][z] = dp[d][z]; solve(2 * i, L, mid, d + 1); for (int z = 0; z <= k; z++) dp[d + 1][z] = dp[d][z]; solve(2 * i + 1, mid + 1, R, d + 1); } int main() { scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) scanf("%d %d", &v[i], &w[i]); scanf("%d", &q); for (int i = 0; i < n; i++) { st[i] = 0; en[i] = q - 1; } for (int i = 0; i < q; i++) { scanf("%d", &ti[i]); if (ti[i] == 1) { scanf("%d %d", &vi, &wi); v[n] = vi, w[n] = wi; st[n] = i; en[n] = q - 1; n++; } else if (ti[i] == 2) { scanf("%d", &xi); en[xi - 1] = i; } else continue; } for (int i = 0; i < n; i++) add(1, 0, q - 1, st[i], en[i], i); solve(1, 0, q - 1, 0); return 0; }
20
#include <bits/stdc++.h> using namespace std; int main() { int sum = 0; int n; double in[100000] = {}; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf", &in[i]); sum += (int)floor(in[i]); } for (int i = 0; i < n; i++) { if (sum < 0) { printf("%d\n", (int)ceil(in[i])); if (in[i] != (int)in[i]) sum++; } else printf("%d\n", (int)floor(in[i])); } }
7
#include <bits/stdc++.h> using namespace std; int main() { double n, y, x; cin >> n >> x >> y; double k = n * y / 100; k = ceil(k); if (k > x) cout << k - x; else cout << 0; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; long long a[1000100], sumf, f[1000100], n, sum[1000100], sums; signed main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); for (long long i = 1; i <= n; i++) sum[i] = (a[i] + sum[i - 1]) % MOD; for (long long i = 1; i <= n; i++) { f[i] = (sum[i] + sumf + sums) % MOD; sumf = (sumf + f[i]) % MOD; sums = (sums * 2 + sum[i]) % MOD; } printf("%lld", f[n]); return 0; }
12
#include <bits/stdc++.h> using namespace std; struct eve { int t, num, s; bool d; bool operator<(const eve& B) const { return t < B.t; } } S[100]; char home[10000], away[10000]; int sav[2][100]; int main() { scanf("%s%s", home, away); int n; scanf("%d", &n); register int i, j; char typ[10]; for (i = 1; i <= n; ++i) { scanf("%d", &S[i].t); scanf("%s", typ); S[i].d = (typ[0] == 'h') ? 0 : 1; scanf("%d", &S[i].num); scanf("%s", typ); S[i].s = (typ[0] == 'y') ? 1 : 2; } sort(S + 1, S + n + 1); for (i = 1; i <= n; ++i) { if (sav[S[i].d][S[i].num] >= 2) continue; sav[S[i].d][S[i].num] += S[i].s; if (sav[S[i].d][S[i].num] >= 2) printf("%s %d %d\n", S[i].d == 0 ? home : away, S[i].num, S[i].t); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, k, A; pair<int, int> X[8]; double maxRes = 0.0; int d[8]; void getCandy(int curr_idx, int k) { if (curr_idx == n) { double res = 0.0; for (int mask = 0; mask < (1 << n); mask++) { int win_votes = 0; int level_sum = 0; double p = 1.0; for (int i = 0; i < n; i++) { int level = X[i].first; int percent = X[i].second + 10 * d[i]; double q = (((double)percent) / 100.0); if (mask & (1 << i)) { win_votes++; p *= q; } else { p *= (1.0 - q); level_sum += level; } } if (win_votes > n / 2) res += p; else res += p * (((double)A) / ((double)(A + level_sum))); } if (res > maxRes) maxRes = res; } else { for (int i = 0; i <= k && X[curr_idx].second + i * 10 <= 100; i++) { d[curr_idx] = i; getCandy(curr_idx + 1, k - i); } } } int main() { cin >> n >> k >> A; for (int i = 0; i < n; i++) { cin >> X[i].first >> X[i].second; } getCandy(0, k); printf("%1.10lf\n", maxRes); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 1000 + 19; int f[N][100][2], Pp[N], Pk[N]; int n, k, P; int F(int x, int t, int C) { int &Ans = f[x][t][C]; if (~Ans) return Ans; if (!t && C) return Ans = (x == n ? 1 : 9LL * Pp[n - x - 1] % P); if (x == n) return Ans = 0; Ans = 0; for (int i = 0; i < 10; i++) (Ans += F(x + 1, (t + 1LL * i * Pk[x] % k) % k, C || i)) %= P; return Ans; } int main() { memset(f, -1, sizeof(f)); scanf("%d%d%d", &n, &k, &P); Pp[0] = 1; for (int i = 1; i < n + 1; i++) Pp[i] = 10LL * Pp[i - 1] % P; Pk[0] = 1; for (int i = 1; i < n + 1; i++) Pk[i] = 10LL * Pk[i - 1] % k; printf("%d\n", F(0, 0, 0)); }
14
#include <bits/stdc++.h> using namespace std; int main() { float H, L; scanf("%f %f", &H, &L); printf("%.6f", (L * L - H * H) / (2 * H)); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 4005; const int M = 20005; const int T = 40005; struct Item { int c, v, t; Item() {} Item(int t) : t(t) {} bool operator<(const Item& x) const { return t < x.t; } } a[N]; struct Que { int a, b, ans, id; bool operator<(const Que& x) const { return a < x.a; } } b[M]; bool cmp(const Que& x, const Que& y) { return x.id < y.id; } int n, m, p, f[N][N], g[N][N]; int main() { scanf("%d%d", &n, &p); for (int i = 0; i < n; i++) scanf("%d%d%d", &a[i].c, &a[i].v, &a[i].t); sort(a, a + n); scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d%d", &b[i].a, &b[i].b); b[i].id = i; } sort(b, b + m); int pp = 0, l, r, li = 0, ri = 0; for (int k = 0; (k + 1) * p < T; k++) { int q = upper_bound(a, a + n, Item(k * p)) - a; for (int i = 1; (q - i >= 0) && (a[q - i].t >= (k - 1) * p); i++) for (int j = 0; j < N; j++) { f[i][j] = f[i - 1][j]; if (j >= a[q - i].c) f[i][j] = max(f[i][j], f[i - 1][j - a[q - i].c] + a[q - i].v); } for (int i = 0; (q + i < n) && (a[q + i].t < (k + 1) * p); i++) for (int j = 0; j < N; j++) { g[i + 1][j] = g[i][j]; if (j >= a[q + i].c) g[i + 1][j] = max(g[i + 1][j], g[i][j - a[q + i].c] + a[q + i].v); } while ((pp < m) && (b[pp].a < (k + 1) * p)) { l = max(b[pp].a - p + 1, 0), r = b[pp].a; while ((li < n) && (a[li].t < l)) li++; while ((ri < n) && (a[ri].t <= r)) ri++; int& ans = b[pp].ans = 0; for (int t = 0; t <= b[pp].b; t++) ans = max(ans, f[q - li][t] + g[ri - q][b[pp].b - t]); pp++; } } sort(b, b + m, cmp); for (int i = 0; i < m; i++) printf("%d\n", b[i].ans); return 0; }
19
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; int T,n,m,a[1005][1005],b[1005][1005],x,y,p,st,cnt; char s[100005]; int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) scanf("%d",&a[i][j]); scanf("%s",s+1); x=y=p=cnt=0; for(int i=1;i<=m;++i){ if(s[i]=='U')--x; if(s[i]=='D')++x; if(s[i]=='L')--y; if(s[i]=='R')++y; if(s[i]=='I'){ swap(y,p); if(!cnt)cnt=1,st=0; else (cnt&1)^st?--cnt:++cnt; } if(s[i]=='C'){ swap(x,p); if(!cnt)cnt=1,st=1; else (cnt&1)^st?++cnt:--cnt; } } cnt%=6; for(int k=1,kk=st;k<=cnt;++k,kk^=1){ if(kk==0) for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) b[i][a[i][j]]=j; else for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) b[a[i][j]][j]=i; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) a[i][j]=b[i][j]; } for(int i=1,ii;i<=n;++i){ for(int j=1,jj,tmp;j<=n;++j){ ii=((i-x)%n+n)%n;if(!ii)ii=n; jj=((j-y)%n+n)%n;if(!jj)jj=n; tmp=((a[ii][jj]+p)%n+n)%n; if(!tmp)tmp=n; printf("%d%c",tmp," \n"[j==n]); } } } return 0; }
19
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const long long INF = 1e9 + 47; const long long LINF = INF * INF; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace IntModulo { const int mod = 998244353; inline int add(int x, int y, int m = mod) { if (x + y < m) return x + y; return x + y - m; } inline int sub(int x, int y, int m = mod) { if (x >= y) return x - y; return x - y + m; } inline int mult(int x, int y, int m = mod) { return x * (long long)y % m; } inline int power(int x, int y, int m = mod) { int r = 1; while (y) { if (y & 1) r = mult(r, x, m); x = mult(x, x, m); y >>= 1; } return r; } inline int inverse(int x, int m = mod) { return power(x, m - 2, m); } inline void ADD(int& x, int y, int m = mod) { x += y; if (x >= m) x -= m; } inline void SUB(int& x, int y, int m = mod) { x -= y; if (x < 0) x += m; } inline void MULT(int& x, int y, int m = mod) { x = x * (long long)y % m; } }; // namespace IntModulo namespace Combinatorics { using namespace IntModulo; const int N = 10000 + 57; int fact[N]; int inv[N]; int invFact[N]; bool inited = false; inline int C(int n, int k) { return n < k ? 0 : mult(fact[n], mult(invFact[k], invFact[n - k])); } inline int H(int n, int k) { return C(n + k - 1, k); } void init() { cerr << "Combinatorics::N" << " = " << Combinatorics::N << endl; ; inited = true; inv[1] = 1; for (int i = (2); i < (N); ++i) inv[i] = mult(mod - mod / i, inv[mod % i]); invFact[0] = fact[0] = 1; for (int i = (1); i < (N); ++i) { fact[i] = mult(i, fact[i - 1]); invFact[i] = mult(invFact[i - 1], inv[i]); } } }; // namespace Combinatorics namespace BerlekampMassey { using namespace IntModulo; inline vector<int> BM(vector<int> x) { vector<int> ls, cur; int lf, ld; for (int i = (0); i < ((int)((x).size())); ++i) { int t = 0; for (int j = (0); j < ((int)((cur).size())); ++j) t = add(t, mult(x[i - j - 1], cur[j])); if (t == x[i]) continue; if (!(int)((cur).size())) { cur.resize(i + 1); lf = i; ld = sub(t, x[i]); continue; } int k = mult(sub(t, x[i]), inverse(ld)); vector<int> c(i - lf - 1); c.push_back(k); for (int j = (0); j < ((int)((ls).size())); ++j) c.push_back(sub(0, mult(k, ls[j]))); if ((int)((c).size()) < (int)((cur).size())) c.resize((int)((cur).size())); for (int j = (0); j < ((int)((cur).size())); ++j) c[j] = add(c[j], cur[j]); if (i - lf + (int)((ls).size()) >= (int)((cur).size())) { ls = cur; lf = i; ld = sub(t, x[i]); } cur = c; } return cur; } inline void multiply(int m, vector<int>& p, vector<int>& q, vector<int>& v) { vector<int> t(2 * m, 0); for (int i = (0); i < (m); ++i) for (int j = (0); j < (m); ++j) t[i + j] = add(t[i + j], mult(p[i], q[j])); for (int i = (2 * m) - 1; i >= (m); --i) for (int j = (m)-1; j >= (0); --j) t[i - j - 1] = add(t[i - j - 1], mult(t[i], v[j])); for (int i = (0); i < (m); ++i) p[i] = t[i]; } inline int solve(vector<int> x, long long n) { if ((int)n < (int)((x).size())) return x[n]; vector<int> v = BM(x); int m = v.size(); if (!m) return 0; vector<int> s(m + 1, 0); vector<int> t(m + 1, 0); s[0] = 1; t[1] = 1; if (m == 1) t[0] = v[0]; while (n) { if (n & 1) multiply(m, s, t, v); multiply(m, t, t, v); n >>= 1; } int ans = 0; for (int i = (0); i < (m); ++i) ans = add(ans, mult(s[i], x[i])); return ans; } }; // namespace BerlekampMassey namespace LagrangeInterpolation { using namespace Combinatorics; int solve(vector<int>& y, long long x) { int n = (int)((y).size()); if ((int)x < n) return y[x]; if (!Combinatorics::inited) Combinatorics::init(); vector<int> pref(n); pref[0] = 1; for (int i = (1); i < (n); ++i) pref[i] = mult(pref[i - 1], (x - i) % mod); vector<int> suf(n + 1); suf[n] = 1; for (int i = (n)-1; i >= (1); --i) suf[i] = mult(suf[i + 1], (x - i) % mod); int ans = 0; for (int i = (1); i < (n); ++i) { int tut = mult( y[i], mult(pref[i - 1], mult(suf[i + 1], mult(invFact[i - 1], invFact[n - i - 1])))); if ((n - i) & 1) ans = add(ans, tut); else ans = sub(ans, tut); } return ans; } int powerSum(int k, long long n) { vector<int> y(k + 3, 0); for (int i = (1); i < ((int)((y).size())); ++i) y[i] = add(y[i - 1], power(i, k)); return solve(y, n); } }; // namespace LagrangeInterpolation using namespace IntModulo; const int N = 10000 + 57; int p[N]; int q[N]; int x[N]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m, k; cin >> n >> m >> k; Combinatorics::init(); p[0] = q[0] = 1; p[1] = inverse(m); q[1] = sub(1, p[1]); for (int i = (2); i < (N); ++i) p[i] = mult(p[i - 1], p[1]); for (int i = (2); i < (N); ++i) q[i] = mult(q[i - 1], q[1]); for (int i = (1); i < (N); ++i) x[i] = power(i, k); vector<int> ans(N, 0); for (int nn = (0); nn < (N); ++nn) { int tut = 0; for (int i = (1); i < (nn + 1); ++i) ADD(tut, mult(p[i], mult(q[nn - i], mult(x[i], Combinatorics::C(nn, i))))); ans[nn] = tut; } cout << LagrangeInterpolation::solve(ans, n) << endl; cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; return 0; }
18
#include <bits/stdc++.h> using namespace std; int main() { int n, p, q; string s; cin >> n >> p >> q >> s; int a, b, fl = 0; for (int i = (0); i <= (n / p); i++) { if ((n - i * p) % q == 0) { a = i; b = (n - i * p) / q; fl = 1; break; } } if (fl == 0) cout << -1; else { cout << a + b << endl; for (int i = (0); i <= (a - 1); i++) { for (int j = (0); j <= (p - 1); j++) cout << s[i * p + j]; cout << endl; } for (int i = (0); i <= (b - 1); i++) { for (int j = (0); j <= (q - 1); j++) cout << s[a * p + i * q + j]; cout << endl; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 200005; int x[N], a[N], cnta, n; int main() { scanf("%d", &n); int x1, ans = 1; for (int i = 1; i <= n; i++) { scanf("%d", &x1); if (x[x1] <= 1) x[x1]++; else ans = 0; } if (!ans) { printf("NO"); return 0; } for (int i = 0; i <= N; i++) if (x[i]) { a[++cnta] = i; x[i]--; } printf("YES\n%d\n", cnta); for (int i = 1; i <= cnta; i++) printf("%d ", a[i]); printf("\n%d\n", n - cnta); for (int i = N; i >= 0; i--) if (x[i]) printf("%d ", i); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int prime = 999983; const int INF = 0x7FFFFFFF; const long long INFF = 0x7FFFFFFFFFFFFFFF; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-6; const long long mod = 1e9 + 7; long long qpow(long long a, long long b) { long long s = 1; while (b > 0) { if (b & 1) s = s * a % mod; a = a * a % mod; b >>= 1; } return s; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1}; const int maxn = 55; int a[maxn][maxn]; int ask(int x1, int y1, int x2, int y2) { cout << "? " << x1 << " " << y1 << " " << x2 << " " << y2 << endl; int t; cin >> t; return t; } int main(void) { int n; cin >> n; a[1][1] = 1; a[n][n] = 0; a[1][2] = 1; a[3][2] = 1 ^ a[1][2] ^ ask(1, 2, 3, 2); a[2][1] = 1 ^ a[3][2] ^ ask(2, 1, 3, 2); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i == 1 && j == 1) continue; if (i == n && j == n) continue; if (i == 3 && j == 2) continue; if (i == 1 && j == 2) continue; if (i == 2 && j == 1) continue; if (i == 1) a[i][j] = 1 ^ a[i][j - 2] ^ ask(i, j - 2, i, j); else if (j == 1) a[i][j] = 1 ^ a[i - 2][j] ^ ask(i - 2, j, i, j); else a[i][j] = 1 ^ a[i - 1][j - 1] ^ ask(i - 1, j - 1, i, j); } } for (int i = 1; i <= n; i += 2) { if (!a[i + 2][i + 2]) { int x1, y1, x2, y2; if (a[i][i] ^ a[i + 1][i] ^ a[i + 2][i] ^ a[i + 2][i + 1]) { x1 = i + 1, y1 = i; x2 = i + 2, y2 = i + 2; } else { x1 = i, y1 = i; x2 = i + 2, y2 = i + 1; } int t = 1 ^ a[x1][y1] ^ ask(x1, y1, x2, y2); if (t != a[x2][y2]) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if ((i + j) & 1) a[i][j] = !a[i][j]; } } } cout << "!" << endl; for (int i = 1; i <= n; ++i, cout << endl) { for (int j = 1; j <= n; ++j) cout << a[i][j]; } break; } } return 0; }
16