solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int n, c[200005], p[200005], q[200005], cnt[200005], ans[200005]; void add(int i, int x) { for (; i <= n + 1; i += i & -i) c[i] += x; } int sum(int i) { int s = 0; for (; i > 0; i -= i & -i) s += c[i]; return s; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &p[i]), p[i]++; for (int i = 0; i < n; i++) scanf("%d", &q[i]), q[i]++; for (int i = 0; i < n; i++) add(i + 1, 1); for (int i = 0; i < n; i++) { int v = sum(p[i]) - 1; cnt[n - i - 1] += v; add(p[i], -1); } memset(c, 0, sizeof(c)); for (int i = 0; i < n; i++) add(i + 1, 1); for (int i = 0; i < n; i++) { int v = sum(q[i]) - 1; cnt[n - i - 1] += v; add(q[i], -1); } for (int i = 0; i < n; i++) if (cnt[i] > i) cnt[i + 1]++, cnt[i] -= (i + 1); memset(c, 0, sizeof(c)); for (int i = 0; i < n; i++) add(i + 1, 1), cnt[i]++; for (int i = n - 1; i >= 0; i--) { int l = 1, r = n; while (l < r) { int m = (l + r) / 2; if (sum(m) >= cnt[i]) r = m; else l = m + 1; } ans[i] = l - 1; add(l, -1); } for (int i = n - 1; i >= 0; i--) printf("%d ", ans[i]); puts(""); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long INF = 1e9 + 7; vector<int> v[500005]; int vis[500005]; vector<int> c; void dfs(int x) { vis[x] = 1; c.push_back(x); for (auto it : v[x]) { if (vis[it] == 0) dfs(it); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1; while (t--) { int n, m; cin >> n >> m; int ans[n + 1]; for (int i = 0; i < m; i++) { int k; cin >> k; int nv[k]; for (int z = 0; z < k; z++) cin >> nv[z]; for (int z = 1; z < k; z++) { v[nv[z - 1]].push_back(nv[z]); v[nv[z]].push_back(nv[z - 1]); } } for (int i = 1; i <= n; i++) { if (vis[i] == 0) { dfs(i); for (auto it : c) { ans[it] = (int)c.size(); } c.clear(); } } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } } }
3
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; string s; cin >> s; vector<long long> a(n); for (long long i = 0; i < n; i++) { long long x = s[i] - '0'; a[i] = x; } vector<long long> pre(n + 1); pre[0] = 0; map<long long, long long> freqs; for (long long i = 0; i < n; i++) { pre[i + 1] = a[i] + pre[i]; } for (long long i = 0; i <= n; i++) { pre[i] = pre[i] - i; freqs[pre[i]]++; } cout << endl; long long cnt = 0; for (auto i : freqs) { long long val = i.second; cnt += (val * (val - 1)) / 2; } cout << cnt << endl; } }
3
#include <bits/stdc++.h> using namespace std; void solve() { int n, x; cin >> n >> x; vector<long long> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; vector<vector<long long> > dp(n + 1, vector<long long>(5, -1e9)); for (int i = 0; i < 5; ++i) dp[0][i] = 0; for (int i = 1; i <= n; ++i) { dp[i][1] = a[i - 1]; dp[i][2] = a[i - 1] * x; dp[i][3] = a[i - 1]; } long long ans = 0; for (int j = 1; j < 5; ++j) for (int i = 1; i <= n; ++i) dp[i][j] += max(0ll, max(dp[i - 1][j], dp[i - 1][j - 1])); for (int i = 1; i <= 3; ++i) for (int j = 1; j <= n; ++j) ans = max(ans, dp[j][i]); cout << ans; } int main() { ios::sync_with_stdio(0); int TTT = 1; while (TTT--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); template <class T> T sqr(T x) { return x * x; } template <class T> T alex(T x) { return (x > 0) ? x : -x; } template <class T> void read(T& x) { x = 0; T f = 1; char ch = getchar(); while ((ch < 48 || ch > 57) && ch != 45) ch = getchar(); if (ch == 45) f = -1, ch = getchar(); while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar(); x *= f; } struct Hash { map<int, set<int> > hash_num[4]; } hash_string; char s[50005]; int n, q; int get_hash(char* s, int l, int r) { int res = 0; for (int i = (l); i <= (r); i++) res = res * 29 + s[i] - 'a' + 1; return res; } char s1[50005], s2[50005]; map<pair<pair<int, int>, pair<int, int> >, int> ans; int solve(const set<int>& set1, const set<int>& set2, int len1, int len2) { int ans = n + 1; if (set1.size() > set2.size()) { for (set<int>::iterator it = set2.begin(); it != set2.end(); it++) { set<int>::iterator el = set1.lower_bound(*it); if (el != set1.end()) { ans = min(ans, max((*el + len1 - 1), (*it + len2 - 1)) - (*it) + 1); } if (el != set1.begin()) { el--; ans = min(ans, max((*el + len1 - 1), (*it + len2 - 1)) - (*el) + 1); } } } else { for (set<int>::iterator it = set1.begin(); it != set1.end(); it++) { set<int>::iterator el = set2.lower_bound(*it); if (el != set2.end()) { ans = min(ans, max((*it + len1 - 1), (*el + len2 - 1)) - (*it) + 1); } if (el != set2.begin()) { el--; ans = min(ans, max((*it + len1 - 1), (*el + len2 - 1)) - (*el) + 1); } } } if (ans == n + 1) return -1; else return ans; } int main() { scanf("%s", s + 1); n = strlen(s + 1); read(q); for (int i = (1); i <= (n - 1 + 1); i++) hash_string.hash_num[0][get_hash(s, i, i + 1 - 1)].insert(i); for (int i = (1); i <= (n - 2 + 1); i++) hash_string.hash_num[1][get_hash(s, i, i + 2 - 1)].insert(i); for (int i = (1); i <= (n - 3 + 1); i++) hash_string.hash_num[2][get_hash(s, i, i + 3 - 1)].insert(i); for (int i = (1); i <= (n - 4 + 1); i++) hash_string.hash_num[3][get_hash(s, i, i + 4 - 1)].insert(i); for (int i = (1); i <= (q); i++) { scanf("%s", s1 + 1); scanf("%s", s2 + 1); int len1 = strlen(s1 + 1), len2 = strlen(s2 + 1); int hash1 = get_hash(s1, 1, len1), hash2 = get_hash(s2, 1, len2); if (ans.count(make_pair(make_pair(len1, hash1), make_pair(len2, hash2)))) printf("%d\n", ans[make_pair(make_pair(len1, hash1), make_pair(len2, hash2))]); else if (ans.count( make_pair(make_pair(len2, hash2), make_pair(len1, hash1)))) printf("%d\n", ans[make_pair(make_pair(len2, hash2), make_pair(len1, hash1))]); else printf("%d\n", ans[make_pair(make_pair(len1, hash1), make_pair(len2, hash2))] = solve(hash_string.hash_num[len1 - 1][hash1], hash_string.hash_num[len2 - 1][hash2], len1, len2)); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int n, m, a[N], qL[N], qR[N], bit[N], ans[N]; vector<int> vec[N]; int lowbit(int x) { return x & (-x); } void insert(int k, int x) { for (; k <= n; k += lowbit(k)) bit[k] += x; return; } int query(int k) { int res = 0; for (; k > 0; k -= lowbit(k)) res += bit[k]; return res; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); qL[i] = x + 1, qR[i] = n - y; vec[qR[i]].push_back(i); } for (int i = 1; i <= n; ++i) { if (i >= a[i]) { int c = i - a[i], l = 1, r = i, nR = 0; while (l <= r) { int mid = l + r >> 1; if (query(mid) >= c) nR = mid, l = mid + 1; else r = mid - 1; } if (nR > 0) insert(1, 1), insert(nR + 1, -1); } for (int j = 0, sze = vec[i].size(); j < sze; ++j) { int k = vec[i][j]; ans[k] = query(qL[k]); } } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); }
5
#include <bits/stdc++.h> using namespace std; int t, n; int MAXN; vector<long long> S, D; vector<pair<long long, pair<long long, long long> > > C[2]; void input(); void solve(); long long dist(long long, long long, int); pair<long long, long long> toFL(long long, int); int main() { input(); solve(); return 0; } void input() { cin >> t; S.push_back(1); S.push_back(2); D.push_back(0); D.push_back(1); while (S[((int)(S.size())) - 1] < (long long)(1e16)) { S.push_back(S[((int)(S.size())) - 1] + S[((int)(S.size())) - 2]); D.push_back(D[((int)(D.size())) - 2] + 1); } S.push_back(S[((int)(S.size())) - 1] + S[((int)(S.size())) - 2]); D.push_back(D[((int)(D.size())) - 2] + 1); MAXN = ((int)(S.size())) - 1; } void solve() { cin >> n; while (t--) { long long a, b; cin >> a >> b; for (int i = 0; i < 2; i++) { C[i].clear(); C[i].resize(MAXN + 1); } cout << dist(a, b, n) << endl; } } long long dist(long long a, long long b, int n) { if (a > b) swap(a, b); n = min(MAXN, n); if (a == b || n == 0) return 0; if (b - a == 1) return 1; if (n == 1 || n == 2) return 1; long long l = S[n - 1]; if (a <= l) { if (b <= l) return (min(dist(a, b, n - 1), min(toFL(a, n - 1).first, toFL(a, n - 1).second) + min(toFL(b, n - 1).first, toFL(b, n - 1).second) + 2)); return (min(toFL(a, n - 1).first, toFL(a, n - 1).second) + toFL(b - l, n - 2).first + 1); } return (dist(a - l, b - l, n - 2)); } pair<long long, long long> toFL(long long v, int n) { for (int i = 0; i < 2; i++) if (C[i][n].first == v) return C[i][n].second; if (n == 0) return make_pair(0, 0); if (n == 1) return make_pair(v == 1 ? 0 : 1, v == 1 ? 1 : 0); int k = 0; if (C[k][n].first) k++; assert(C[k][n].first == 0); if (v > S[n - 1]) { pair<long long, long long> to = toFL(v - S[n - 1], n - 2); C[k][n] = make_pair(v, make_pair(to.first + 1, to.second)); return C[k][n].second; } pair<long long, long long> to = toFL(v, n - 1); C[k][n] = make_pair(v, make_pair(min(to.first, to.second + 2), min(to.first, to.second) + 1 + D[n - 2])); return C[k][n].second; }
5
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) #define repb(i,a,b) for(int i=a;i>=b;i--) #define all(a) a.begin(),a.end() #define o(a) cout<<a<<endl #define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; signed main() { int n; cin >> n; if(n == 0){ cout << 1 << endl; }else if(n == 1){ cout << 2 << endl; }else if(n == 2){ cout << 1 << endl; }else{ cout << 0 << endl; } }
0
#include <bits/stdc++.h> using namespace std; map<int, int> c; int p[100010]; set<int> pr; int t[100010]; void solve() { int n; cin >> n; for (int i = 0; i < n; ++i) { int a; cin >> a; p[i] = a; if (pr.empty()) { pr.insert(a); continue; } auto up = pr.upper_bound(a), it = pr.end(); --it; if (up == it) { ++c[*up]; c[a] = 1; } else if (up != pr.end()) { t[a] = 2; c[a] = 1; } pr.insert(a); } int an = 100010; for (int i = 1; i < n + 1; ++i) { if (c[an] < c[i] || (c[an] == c[i] && i < an)) an = i; } cout << an; } int main() { solve(); }
3
#include <bits/stdc++.h> using namespace std; vector<string> getRoot(int a3, int a4, int a1, int a2) { vector<string> ret; string s = ""; int x = 0, y = 0; int c1 = a1, c2 = a2; bool flag = true; for (int i = 0; i < 2; i++) { while (true) { if (flag) { if (s.size() > 0 && s[s.size() - 1] == '7') { if (y + 1 > a4) break; y++; } s += "4"; c1--; } else { if (s.size() > 0 && s[s.size() - 1] == '4') { if (x + 1 > a3) break; x++; } s += "7"; c2--; } flag = !flag; } if (x == a3 && y == a4 && c1 >= 0 && c2 >= 0) ret.push_back(s); flag = false; s = ""; x = y = 0; c1 = a1, c2 = a2; } return ret; } void solve(string root, int a1, int a2, int a3, int a4) { int aa, bb; aa = a1, bb = a2; for (int j = 0; j < root.size(); j++) if (root[j] == '4') aa--; else if (root[j] == '7') bb--; if (aa == -1 || bb == -1) { cout << -1 << endl; return; } int pos = root.size(); for (int i = root.size() - 1; i >= 0; i--) { if (root[i] == '7') { pos = i; break; } } string r = ""; for (int i = 0; i < root.size(); i++) { if (root[i] == '4' && aa != 0) { cout << string(aa, '4'); aa = 0; } else if (pos == i) { cout << string(bb, '7'); bb = 0; } cout << root[i]; } if (aa != 0) cout << string(aa, '4'); if (bb != 0) cout << string(bb, '7'); cout << endl; } int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) { cout << "-1" << endl; } else { vector<string> roots = getRoot(a3, a4, a1, a2); string res = ""; for (int i = 0; i < roots.size(); i++) { if (res == "") res = roots[i]; else res = min(res, roots[i]); } if (res == "") cout << -1 << endl; else solve(res, a1, a2, a3, a4); } }
2
#include <bits/stdc++.h> const int MAXINT = 2147483640; const long long MAXLL = 9223372036854775800LL; const long long MAXN = 4000000; using namespace std; struct { long long ans, open, close; } t[MAXN], res; long long l, r; void modify(long long v, long long tl, long long tr, long long pos, long long v1, long long v2) { if (tl == tr) { t[v].open = v1; t[v].close = v2; return; } long long tm = (tl + tr) / 2; if (pos <= tm) modify(v * 2, tl, tm, pos, v1, v2); else modify(v * 2 + 1, tm + 1, tr, pos, v1, v2); long long k = min(t[v * 2].open, t[v * 2 + 1].close); t[v].ans = t[v * 2].ans + t[v * 2 + 1].ans + k * 2; t[v].open = t[v * 2].open + t[v * 2 + 1].open - k; t[v].close = t[v * 2].close + t[v * 2 + 1].close - k; } void get(long long v, long long tl, long long tr) { if (tl >= l && tr <= r) { long long k = min(res.open, t[v].close); res.ans = res.ans + t[v].ans + k * 2; res.open = res.open - k + t[v].open; res.close = res.close - l + t[v].close; return; } long long tm = (tl + tr) / 2; if (l <= tm && r >= tl) get(v * 2, tl, tm); if (l <= tr && r > tm) get(v * 2 + 1, tm + 1, tr); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; long long i, j, n, m; cin >> s; n = s.size(); for (i = 0; i < n; ++i) { if (s[i] == '(') modify(1, 1, n, i + 1, 1, 0); else modify(1, 1, n, i + 1, 0, 1); } cin >> m; for (i = 1; i <= m; ++i) { cin >> l >> r; res.ans = 0; res.open = 0; res.close = 0; get(1, 1, n); cout << res.ans << endl; } return 0; }
5
#include<iostream> #include<vector> #define MAX 10000000 using namespace std; int main(){ int n; vector<char> a; vector<int> box; for(int i=0;i<=MAX;i++) a.push_back('1'); a[0]='0'; a[1]='0'; for(int i=2;i<=MAX;i++) if(a[i]=='1'){ if(i>=13&&(a[i]=='1'&&a[i-2]=='1'&&a[i-6]=='1'&&a[i-8]=='1')){ box.push_back(i); } for(int j=i*2;j<=MAX;j+=i) a[j]='0'; } while(cin>>n,n){ int i; for(i=0;i<(int)box.size()&&box[i]<=n;i++); cout<<box[i-1]<<endl; } return 0; }
0
#include <bits/stdc++.h> #include <math.h> #include <cmath> using namespace std; #define int long long int void fun() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int gcd(int a, int b) { if(a == 0) return b; return gcd(b % a, a); } const int N = 2e5 + 7; const int INF = 3e18+7; const int mod = 1e9 + 7; signed main() { fun(); int tests = 1; cin>>tests; while(tests--) { int n; cin >> n; string s; cin >> s; map<pair<int, int>, int> mapp; int d = 0, k = 0; for(char ch: s) { if(ch == 'D') d++; else k++; int x = d, y = k; int g = gcd(x, y); x /= g; y /= g; mapp[make_pair(x, y)]++; cout << mapp[make_pair(x, y)] << ' '; } cout << '\n'; } //cerr << "time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " second \n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> ms(10, -1e18); ms[0] = 0; for (int k, i = 0; i < n; i++) { vector<long long> nms(10, -1e18); cin >> k; multiset<int> d1; int d2 = 0, d3 = 0; int c, d; for (int j = 0; j < k; j++) { cin >> c >> d; if (c == 1) { d1.insert(d); if (d1.size() > 3) d1.erase(d1.begin()); } else if (c == 2) d2 = max(d2, d); else d3 = max(d3, d); } vector<pair<int, int> > bestd; for (int x : d1) bestd.push_back(make_pair(1, x)); if (d2 > 0) bestd.push_back(make_pair(2, d2)); if (d3 > 0) bestd.push_back(make_pair(3, d3)); for (int j = 0; j < 10; j++) { nms[j] = max(nms[j], ms[j]); for (int i1 = 0; i1 < bestd.size(); i1++) nms[(j + 1) % 10] = max(nms[(j + 1) % 10], ms[j] + bestd[i1].second * ((j >= 9) ? 2 : 1)); for (int i1 = 0; i1 < bestd.size(); i1++) { for (int i2 = i1 + 1; i2 < bestd.size(); i2++) { if (bestd[i1].first + bestd[i2].first <= 3) nms[(j + 2) % 10] = max( nms[(j + 2) % 10], ms[j] + bestd[i1].second + bestd[i2].second + max(bestd[i1].second, bestd[i2].second) * ((j >= 8) ? 1 : 0)); } } for (int i1 = 0; i1 < bestd.size(); i1++) { for (int i2 = i1 + 1; i2 < bestd.size(); i2++) { for (int i3 = i2 + 1; i3 < bestd.size(); i3++) { if (bestd[i1].first + bestd[i2].first + bestd[i3].first <= 3) nms[(j + 3) % 10] = max(nms[(j + 3) % 10], ms[j] + bestd[i1].second + bestd[i2].second + bestd[i3].second + max(bestd[i1].second, max(bestd[i2].second, bestd[i3].second)) * ((j >= 7) ? 1 : 0)); } } } } for (int j = 0; j < 10; j++) ms[j] = nms[j]; } long long ans = 0; for (int j = 0; j < 10; j++) ans = max(ans, ms[j]); cout << ans << endl; return 0; }
6
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;++i) using namespace std; using ll=long long; constexpr ll mod=1000000007; constexpr int inf=1000000000; constexpr ll linf=1000000000000000000; int main(){ string n; cin>>n; int sz=n.size(); int dp[sz+1][2]; dp[0][0]=0; dp[0][1]=1; rep(i,sz){ int d=n[i]-'0'; dp[i+1][0]=min(dp[i][0]+d,dp[i][1]+10-d); dp[i+1][1]=min(dp[i][0]+d+1,dp[i][1]+9-d); } cout<<dp[sz][0]<<endl; }
0
#include <bits/stdc++.h> using namespace std; vector<int> adj[200001]; bool vis[200001]; int c = 0; int ms = 0, l, sz = 0, nd; bool flag = true; void check(int s) { vis[s] = true; if (adj[s].size() == 2) { for (auto it : adj[s]) { if (!vis[it]) { sz += adj[it].size() - 1; nd++; l = it; check(it); } } } else { flag = false; for (auto it : adj[s]) { if (!vis[it]) { sz += adj[it].size() - 1; nd++; l = it; check(it); } } return; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; int a, b; if (m == 0) { cout << m << endl; return 0; } for (int i = 0; i < m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } int ans = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { l = 0; sz = adj[i].size() - 1; nd = 1; flag = true; check(i); if (flag && sz >= 3 && sz == nd && flag && adj[l].size() == 2 && find(adj[l].begin(), adj[l].end(), i) != adj[l].end()) ans++; } } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<bool> vb; typedef pair<ll,ll> pll; const ll mod=1000000007; const ll inf=1000000000000; #define rep(i,m,n) for(ll i=m;i<n;i++) int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n; cin >> n; vector<pll> p(2*n); rep(i,0,n) { cin >> p[i].first; p[i].second=-1; } rep(i,n,2*n) { cin >> p[i].first; p[i].second=1; } sort(p.begin(),p.end()); ll res=1; stack<ll> a,b; rep(i,0,2*n){ if(p[i].second==-1){ if(!b.empty()) { res*=b.size(); res%=mod; b.pop(); } else a.push(p[i].first); } else if(p[i].second==1){ if(!a.empty()) { res*=a.size(); res%=mod; a.pop(); } else b.push(p[i].first); } } cout << res << endl; }
0
#include <bits/stdc++.h> using namespace std; namespace IO { void setIn(string s) { freopen(s.c_str(), "r", stdin); } void setOut(string s) { freopen(s.c_str(), "w", stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin.exceptions(cin.failbit); if (s.size()) { setIn(s + ".inp"); setOut(s + ".out"); } else { } } } // namespace IO using namespace IO; namespace Function { template <typename T1, typename T2> void amax(T1 &a, T2 b) { if (typeid(a).name() == typeid(int).name() && typeid(b).name() == typeid(long long).name()) { cout << "Error on amax"; exit(0); } if (a < b) a = b; } template <typename T1, typename T2> void amin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T> void compress(vector<T> &a) { sort(a.begin(), a.end()); a.resize(unique(a.begin(), a.end()) - a.begin()); } template <typename T> long long sqr(T x) { return 1LL * x * x; } template <typename T1, typename T2> long long gcd(T1 a, T2 b) { return (b == 0 ? a : gcd(b, a % b)); } template <typename T1, typename T2> long long lcm(T1 a, T2 b) { return 1LL * a / gcd(a, b) * b; } } // namespace Function using namespace Function; namespace Output { void print(int x) { cout << x << "\n"; } void print(unsigned int x) { cout << x << "\n"; } void print(long unsigned int x) { cout << x << "\n"; } void print(long long x) { cout << x << "\n"; } void print(unsigned long long x) { cout << x << "\n"; } void print(float x) { cout << x << "\n"; } void print(double x) { cout << x << "\n"; } void print(long double x) { cout << x << "\n"; } void print(char x) { cout << x << "\n"; } void print(const char *x) { cout << x << "\n"; } void print(string x) { cout << x << "\n"; } void print(bool x) { cout << x << "\n"; } template <class T, class... Ts> void print(T t, Ts... ts) { cout << t << " "; print(ts...); } template <typename T1, typename T2> void print(pair<T1, T2> a) { print(a.first, a.second); } template <typename T> void print(T a) { for (auto it : a) { print(it); } } template <class T, class... Ts> void prine(T t, Ts... ts) { print(t, ts...); exit(0); } } // namespace Output using namespace Output; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; const int INF = 1e9; const long long INFL = 1e18; const int MOD = 1e9 + 7; const int N = 1e5 + 10; int x[N], y[N], u[N], v[N]; bool check[N]; int d[N][4]; int main() { setIO(); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < k; i++) { cin >> x[i] >> y[i]; } for (int i = 0; i < k; i++) { cin >> u[i] >> v[i]; } vector<int> res; for (int i = 0; i < n; i++) { if (i > 0) { res.push_back('D'); } if (i & 1) { for (int j = 1; j < m; j++) { res.push_back('L'); } } else { for (int j = 1; j < m; j++) { res.push_back('R'); } } } for (int i = 0; i < n; i++) { if (i > 0) { res.push_back('U'); } if (i & 1) { for (int j = 1; j < m; j++) { res.push_back('R'); } } else { for (int j = 1; j < m; j++) { res.push_back('L'); } } } print(res.size()); for (char x : res) { cout << x; } return 0; }
3
#include <bits/stdc++.h> using namespace std; struct Node { long long x, y; long long pos; Node(long long x = 0, long long y = 0, long long pos = 0) : x(x), y(y), pos(pos){}; }; Node a[100005], b[100005]; bool vis[100005]; long long n, ans, k; bool compare1(Node a, Node b) { return a.x < b.x; } void dfs(int xleft, int xright, int yleft, int yright, int cnt) { while (vis[a[xleft].pos]) xleft++; while (vis[a[xright].pos]) xright--; while (vis[b[yleft].pos]) yleft++; while (vis[b[yright].pos]) yright--; if (cnt == k) { long long ansx = a[xright].x - a[xleft].x, ansy = b[yright].y - b[yleft].y; if (ansx * ansy == 0) return; ans = min(ans, (ansx * ansy) / 4); return; } vis[a[xleft].pos] = true; dfs(xleft + 1, xright, yleft, yright, cnt + 1); vis[a[xleft].pos] = false; vis[a[xright].pos] = true; dfs(xleft, xright - 1, yleft, yright, cnt + 1); vis[a[xright].pos] = false; vis[b[yleft].pos] = true; dfs(xleft, xright, yleft + 1, yright, cnt + 1); vis[b[yleft].pos] = false; vis[b[yright].pos] = true; dfs(xleft, xright, yleft, yright - 1, cnt + 1); vis[b[yright].pos] = false; } bool compare2(Node a, Node b) { return a.y < b.y; } int main() { ans = 0x3f3f3f3f3f3f3f3f; scanf("%I64d%I64d", &n, &k); memset(vis, 0, sizeof(vis)); long long x1, y1, x2, y2; for (long long i = 1; i <= n; i++) { scanf("%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2); a[i] = Node(x1 + x2, y1 + y2, i); } for (long long i = 1; i <= n; i++) { b[i] = a[i]; } sort(a + 1, a + n + 1, compare1); sort(b + 1, b + n + 1, compare2); long long cnt = 0; long long xleft = 1, xright = n, yleft = 1, yright = n; dfs(1, n, 1, n, 0); if (ans == 0x3f3f3f3f3f3f3f3f) ans = 1; if (ans == 6) ans = 4; printf("%I64d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pair<int, int>>; using vvi = vector<vi>; int Bit(int mask, int b) { return (mask >> b) & 1; } template <class T> bool ckmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <class T> bool ckmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; } const int INFi = 2e9; const ll INF = 2e18; const int maxN = 605; const int LG = 20; void solve() { int w, h; cin >> w >> h; ll ans = 0; auto read = [&]() { int k; cin >> k; int x1; cin >> x1; int x2; for (int _ = 0; _ < (k - 1); ++_) cin >> x2; return x2 - x1; }; ans = max(ans, 1ll * read() * h); ans = max(ans, 1ll * read() * h); ans = max(ans, 1ll * read() * w); ans = max(ans, 1ll * read() * w); cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; cin >> t; for (int i = 0; i < (t); ++i) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int X[100005], Y[100005], W[100005]; pair<int, int> R[100005]; set<pair<int, int> > S[100000 + 100005], s; bool is_pleasing(int idx) { if (!R[idx].first && !R[idx].second) { s.insert(R[idx]); return true; } if (!R[idx].first) { if (s.find(pair<int, int>(0, R[idx].second - 1)) == s.end()) return false; s.insert(R[idx]); return true; } if (!R[idx].second) { if (s.find(pair<int, int>(R[idx].first - 1, 0)) == s.end()) return false; s.insert(R[idx]); return true; } if (s.find(pair<int, int>(R[idx].first - 1, R[idx].second)) == s.end() || s.find(pair<int, int>(R[idx].first, R[idx].second - 1)) == s.end()) return false; s.insert(R[idx]); return true; } bool solve(int n) { int i; for (i = 0; i < n; ++i) S[100000 + Y[i] - X[i]].insert(pair<int, int>(X[i], Y[i])); for (i = 0; i < n; ++i) if (S[100000 + W[i]].empty()) return false; else { R[i] = *S[100000 + W[i]].begin(); if (!is_pleasing(i)) return false; S[100000 + W[i]].erase(S[100000 + W[i]].begin()); } return true; } int main() { int n, i; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d %d", X + i, Y + i); for (i = 0; i < n; ++i) scanf("%d", W + i); if (solve(n)) { puts("YES"); for (i = 0; i < n; ++i) printf("%d %d\n", R[i].first, R[i].second); } else puts("NO"); return 0; }
3
#include <bits/stdc++.h> using namespace std; #define int long long #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define RFOR(i, s, n) for (int i = (n) - 1; i >= (int)(s); i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define ALL(a) a.begin(), a.end() constexpr long long INF = 1e18; template<class T>inline bool CHMAX(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T>inline bool CHMIN(T&a,T b){if(a>b){a=b;return true;}return false;} int d[303][303]; void warshall_floyd(int n) { // nは頂点数 for (int i = 0; i < n; i++) // 経由する頂点 for (int j = 0; j < n; j++) // 開始頂点 for (int k = 0; k < n; k++) // 終端 d[j][k] = min(d[j][k], d[j][i] + d[i][k]); } signed main(){ int N,M; cin >> N >> M; REP(i,303)REP(j,303)d[i][j] = INF; REP(i,303)d[i][i] = 0; REP(i,M){ int a,b; cin >> a >> b; a--;b--; d[a][b] = 1; } warshall_floyd(N); vector<int>ans[N]; REP(i,N){ REP(j,N){ if(d[i][j]!=INF&&d[j][i]!=INF)ans[i].push_back(j+1); } REP(j,ans[i].size())cout << ans[i][j] << " \n"[j+1 == ans[i].size()]; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << "0 0 " << n; return 0; }
1
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int maxn = 50 + 2; const int MOD = 1000000007; inline int inc(int a, int b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); } inline int dec(int a, int b) { return (a >= b) ? (a - b) : (a + MOD - b); } inline int mul(int a, int b) { return 1LL * a * b % MOD; } inline void updata(int &x, int y) { x = inc(x,y); } inline int power(int x, int k) { int tmp = 1; while(k) { if(k & 1) tmp = mul(tmp,x); x = mul(x,x); k >>= 1; } return tmp; } int n, C[maxn][maxn], dp[maxn][maxn]; int main() { scanf("%d", &n); C[0][0] = 1; for(int i = 1;i <= n;i ++) { C[i][0] = 1; for(int j = 1;j <= i;j ++) C[i][j] = inc(C[i - 1][j],C[i - 1][j - 1]); } dp[1][0] = dp[1][1] = n; for(int i = 1;i < n;i ++) { for(int j = 0;j <= i;j ++) { for(int k = max(1,j);k <= n - i;k ++) { if((k - j) & 1) continue; int now = (k - j) / 2; for(int x = 0;x <= k;x ++) { int xs = mul(C[n - i][k],C[k][x]); updata(dp[i + k][abs(now - x)],mul(xs,dp[i][j])); } } } } int Ans = dp[n][0]; printf("%d\n", Ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; vector<int> pos(26, -1); int a, b, c; for (int i = 0; i < s.size(); i++) { int x = s[i] - 'A'; if (pos[x] != -1) { a = pos[x]; c = i - pos[x] - 1; b = i; break; } pos[x] = i; } if (c == 0) { cout << "Impossible"; return 0; } int k = c / 2; vector<vector<char> > data(2, vector<char>(13, '#')); data[0][12 - k] = s[b]; int now = 12 - k - 1, x = 0; int a1 = a; a--; while (a >= 0) { if (now == -1) { x = 1; now = 0; } data[x][now] = s[a]; if (x) now++; else now--; a--; } a = a1 + 1; x = 0; now = 12 - k + 1; while (a != s.size()) { if (a == b) { a++; continue; } if (now == 13) { x = 1; now = 12; } if (now == -1) { x = 0; now = 0; } data[x][now] = s[a]; if (x) now--; else now++; a++; } for (int i = 0; i < 2; i++) { for (int j = 0; j < 13; j++) { cout << data[i][j]; } cout << endl; } }
3
#include<bits/stdc++.h> using namespace std; const int N = 2e5+7; typedef long long LL; int n,sum; string s; int quick(int a,int b,int mod){ LL ans=1; while(b){ if(b&1) ans = ans*a%mod; a = (LL)a*a%mod; b>>=1; } return ans; } int fun(int n){ int ans=0; while(n){ ans++; n =n &(n-1); } return ans; } int main(){ cin>>n>>s; for(auto c :s) if(c=='1') sum++; int cnt1 = 0 ,cnt2 = 0, p1 = sum + 1 , p2 = sum - 1; for(int i=0;i<n;i++) if(s[i]=='1'){ cnt1 = (cnt1 + quick(2,n-i-1,p1))%p1; if(p2>0) cnt2 = (cnt2 + quick(2,n-i-1,p2))%p2; } int t,ans=0; for(int i=0;i<n;i++){ ans=1; if(s[i]=='0') t = (cnt1+quick(2,n-i-1,p1)+p1)%p1; else if(p2<=0) {cout<<0<<"\n"; continue;} else t = (cnt2-quick(2,n-i-1,p2)+p2)%p2; if(!t) cout<<ans<<"\n"; else { while(t) ans++,t = t%fun(t); cout<<ans<<endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, k; bool used[2010], freed[2010]; long long f[2010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { int a; scanf("%d ", &a); if (!~a) continue; used[a] = freed[i] = true; } for (int i = 1; i <= n; ++i) if (!used[i]) if (freed[i]) ++m; else ++k; f[0] = 1; for (int i = 1; i <= m; ++i) (f[0] *= i) %= 1000000007; f[1] = f[0] * m % 1000000007; for (int i = 2; i <= k; ++i) f[i] = (m * f[i - 1] + (i - 1) * (f[i - 1] + f[i - 2])) % 1000000007; printf("%d\n", f[k]); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { double l, p, q; cin >> l >> p >> q; double ans = (p * l) / (p + q); std::cout << std::setprecision(5) << ans << '\n'; }
1
#include <bits/stdc++.h> using namespace std; vector<vector<long long>> memo(1001, vector<long long>(1001, -1)); long long cnk(long long n, long long k) { if (memo[n][k] == -1) { if (n < k) memo[n][k] = 0; else if (n == k) memo[n][k] = 1; else if (k == 0) memo[n][k] = 1; else { memo[n][k] = cnk(n - 1, k) + cnk(n - 1, k - 1); memo[n][k] %= 1000000007; } } return memo[n][k]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k, sum = 0, a; cin >> k; long long ans = 1; for (int i = 0; i < k; ++i) { cin >> a; ans *= cnk(sum + a - 1, a - 1); ans %= 1000000007; sum += a; } cout << ans << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; void solve() { long long int n; cin >> n; vector<long long int> arr(n, 0); for (long long int i = 0; i < n; i++) { cin >> arr[i]; } map<long long int, vector<long long int>> ans; for (long long int i = 0; i < n; i++) { long long int x = arr[i], c = 0; while ((x % 2) == 0) { c++; x >>= 1; } ans[c].push_back(i); } long long int maxx = 0; long long int ind = -1; for (auto i : ans) { if (i.second.size() > maxx) { maxx = i.second.size(); ind = i.first; } } cout << n - maxx << endl; for (auto i : ans) { if (ind == i.first) continue; for (auto j : i.second) cout << arr[j] << " "; } cout << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int d[200000], n; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= 1e5; i++) d[i] = -1; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; int ans = 0; for (int j = 1; j * j <= x; j++) if (x % j == 0) { if (d[j] < i - y) ans++; if (d[x / j] < i - y && j * j != x) ans++; d[j] = i; d[x / j] = i; } cout << ans << endl; } }
2
#include <bits/stdc++.h> using namespace std; long long N, E, V; long long e, ans; long long son[310]; long long A[310], B[310]; long long f[310]; long long ed[610], nxt[610]; long long second[200010], T[200010], W[200010]; long long p[310], C[310]; void adde(long long i, long long j) { e++; ed[e] = j; nxt[e] = son[i]; son[i] = e; } long long findr(long long i) { if (f[i] == i) return i; return f[i] = findr(f[i]); } void update(long long s, long long t, long long w) { if (w <= 0) return; second[++ans] = s; T[ans] = t; W[ans] = w; A[s] -= w; } void dfs2(long long i, long long lst) { p[i] = A[i] - B[i]; for (long long j = son[i]; j; j = nxt[j]) if (ed[j] != lst) { dfs2(ed[j], i); p[i] += p[ed[j]]; } } void dfs(long long i, long long lst, long long P) { long long tmp, Tmp; for (long long j = son[i]; j && P > 0; j = nxt[j]) if (ed[j] != lst && p[ed[j]] > 0) { if (p[ed[j]] < P) tmp = p[ed[j]]; else tmp = P; A[i] += tmp; P -= tmp; if (A[ed[j]] > B[ed[j]]) { if (A[ed[j]] >= tmp) { update(ed[j], i, tmp); if (A[ed[j]] < B[ed[j]]) dfs(ed[j], i, B[ed[j]] - A[ed[j]]); } else { tmp -= A[ed[j]]; update(ed[j], i, A[ed[j]]); dfs(ed[j], i, B[ed[j]] + tmp); update(ed[j], i, tmp); } } else { if (A[ed[j]] >= tmp) { update(ed[j], i, tmp); dfs(ed[j], i, tmp); } else { Tmp = tmp - A[ed[j]]; update(ed[j], i, A[ed[j]]); dfs(ed[j], i, tmp); update(ed[j], i, Tmp); } } } } signed main() { cin >> N >> V >> E; for (long long i = 1; i <= N; i++) cin >> A[i]; for (long long i = 1; i <= N; i++) cin >> B[i]; for (long long i = 1; i <= N; i++) f[i] = i; for (long long i = 1, x, y, f1, f2; i <= E; i++) { cin >> x >> y; f1 = findr(x); f2 = findr(y); if (f1 != f2) { adde(x, y); adde(y, x); f[f1] = f2; } } for (long long i = 1; i <= N; i++) f[i] = findr(i); for (long long i = 1; i <= N; i++) { C[f[i]] += A[i]; C[f[i]] -= B[i]; } for (long long i = 1; i <= N; i++) if (C[i] != 0) { puts("NO"); return 0; } for (long long i = 1; i <= N; i++) if (A[i] < B[i]) { dfs2(i, 0); dfs(i, 0, B[i] - A[i]); } cout << ans << endl; for (long long i = 1; i <= ans; i++) cout << second[i] << ' ' << T[i] << ' ' << W[i] << endl; return 0; }
3
#include<bits/stdc++.h> using namespace std; int a,b,c,d,e; int main(){ scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); printf("%d\n",max((c-a)*60+d-b-e,0)); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 100; long long n, k; char s1[N], s2[N]; signed main() { cin >> n >> k; cin >> s1 + 1; cin >> s2 + 1; long long tmp = 1, ans = 0; for (int i = 1; i <= n; i++) { tmp *= 2; if (s1[i] == 'b') tmp--; if (s2[i] == 'a') tmp--; if (tmp > k) tmp = k + 1; ans += min(tmp, k); } cout << ans; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e+5 + 10; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; int64_t cur = 0; for (int i = 0; i < n; ++i) { int a; cin >> a; if (i & 1) a *= -1; cur += a; } vector<int> b(m); set<int64_t> fans; int64_t fcur = 0; for (int i = 0; i < m; ++i) { cin >> b[i]; if (i & 1) b[i] *= -1; fcur += b[i]; if (i >= n) fcur -= b[i - n]; if (i >= n - 1) { if ((i - n + 1) & 1) fans.insert(-fcur); else fans.insert(fcur); } } vector<int> l(q + 1), r(q + 1), x(q + 1); for (int i = 1; i <= q; ++i) { cin >> l[i] >> r[i] >> x[i]; } for (int i = 0; i <= q; ++i) { --l[i]; if ((r[i] - l[i]) & 1) { if (l[i] & 1) x[i] *= -1; cur += x[i]; } auto it = fans.lower_bound(cur); int64_t ans = 1e+18; if (it != fans.end()) { ans = abs(cur - *it); } if (it != fans.begin()) { --it; ans = min(ans, abs(cur - *it)); } cout << ans << '\n'; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[100000 + 5], b[100000 + 5]; int n, s, t; int main() { cin >> n >> s >> t; for (int i = 1; i <= n; i++) { cin >> a[i]; } int cnt = 0; while (s != t && !b[s]) { cnt++; b[s] = 1; s = a[s]; } if (s != t) { cout << -1 << endl; } else { cout << cnt << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; pair<long long, long long> p[n]; for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second; long long left = p[0].first; vector<pair<long long, long long> > vec; multiset<long long> s; for (int i = 1; i < n; i++) { if (p[i].first > left) { s.insert(p[i].second - p[i].first); } else vec.push_back(make_pair(p[i].first, p[i].second)); } sort(vec.begin(), vec.end()); int pos = s.size() + 1; int j = vec.size() - 1; while (left > 0 && pos > 1) { left -= (*(s.begin()) + 1); s.erase(s.begin()); if (left < 0) break; while (j >= 0 && vec[j].first > left) { s.insert(vec[j].second - vec[j].first); j--; } pos = min(pos, (int)(s.size() + 1)); } cout << pos; }
4
#include <bits/stdc++.h> using namespace std; const int N = 6e5 + 100, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { a += b; if (a < 0) a += MOD; else if (a >= MOD) a -= MOD; return a; } void _sum(int &a, int b) { a = sum(a, b); } int mul(int a, int b) { return 1LL * a * b % MOD; } void _mul(int &a, int b) { a = mul(a, b); } int power(int a, int b) { int res = 1; while (b) { if (b & 1) _mul(res, a); _mul(a, a); b >>= 1; } return res; } int n, q, p[N], dex[N], val[N], power_2[N], pre_pro[N]; vector<int> vec; struct node { int ans, sigma, sigma_l, sigma_r, cnt; void print(int s, int e) { return; cerr << s << ' ' << e << " ans = " << ans << " sigma = " << sigma << " sigma_l = " << sigma_l << " sigma_r = " << sigma_r << " cnt = " << cnt << '\n'; } } seg[N << 2]; void merge(int v, int lc, int rc) { seg[v].sigma = sum(seg[lc].sigma, seg[rc].sigma); seg[v].sigma_l = sum(seg[lc].sigma_l, mul(seg[rc].sigma_l, power_2[seg[lc].cnt])); seg[v].sigma_r = sum(mul(seg[lc].sigma_r, power_2[seg[rc].cnt]), seg[rc].sigma_r); seg[v].cnt = seg[lc].cnt + seg[rc].cnt; seg[v].ans = sum(sum(mul(seg[lc].ans, power_2[seg[rc].cnt]), mul(power_2[seg[lc].cnt], seg[rc].ans)), mul(seg[lc].sigma_l, seg[rc].sigma_r)); } void add(int dex, int val, int v = 1, int s = 0, int e = vec.size()) { if (dex < s || e <= dex) return; if (e - s == 1) { seg[v].cnt += val; seg[v].sigma = mul(seg[v].cnt, vec[s]); seg[v].sigma_l = seg[v].sigma_r = mul(vec[s], sum(power_2[seg[v].cnt], -1)); seg[v].ans = mul(mul(vec[s], vec[s]), pre_pro[seg[v].cnt]); seg[v].print(s, e); return; } int mid = s + e >> 1, lc = v << 1, rc = lc | 1; add(dex, val, lc, s, mid); add(dex, val, rc, mid, e); merge(v, lc, rc); seg[v].print(s, e); } int f(int x) { return lower_bound(vec.begin(), vec.end(), x) - vec.begin(); } void input() { cin >> n; for (int i = 0; i < n; i++) { cin >> p[i]; vec.push_back(p[i]); } cin >> q; for (int i = 0; i < q; i++) { cin >> dex[i] >> val[i]; dex[i]--; vec.push_back(val[i]); } } void ready() { power_2[0] = 1; for (int i = 1; i < N; i++) power_2[i] = mul(power_2[i - 1], 2); for (int i = 2; i < N; i++) pre_pro[i] = sum(pre_pro[i - 1], mul(i - 1, power_2[i - 2])); sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); for (int i = 0; i < n; i++) p[i] = f(p[i]); for (int i = 0; i < q; i++) val[i] = f(val[i]); } void solve() { int Z = power(power_2[n], MOD - 2); for (int i = 0; i < n; i++) add(p[i], 1); cout << mul(seg[1].ans, Z) << '\n'; for (int i = 0; i < q; i++) { add(p[dex[i]], -1); add(p[dex[i]] = val[i], 1); cout << mul(seg[1].ans, Z) << '\n'; } } int main() { ios::sync_with_stdio(false), cin.tie(0); input(); ready(); solve(); }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int n, a[MAXN]; long long dp1[MAXN][2], dp2[MAXN][2]; void init() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); } void solve() { dp1[0][0] = dp1[0][1] = 0; for (int i = 1; i <= n; ++i) { bool isOdd = a[i] % 2; if (a[i] > 1) dp1[i][0] = dp1[i - 1][0] + a[i] - isOdd; dp1[i][1] = max(dp1[i - 1][0], dp1[i - 1][1]) + a[i] - (!isOdd); } dp2[n][0] = dp2[n][1] = 0; for (int i = n - 1; i >= 0; --i) { bool isOdd = a[i + 1] % 2; if (a[i + 1] > 1) dp2[i][0] = dp2[i + 1][0] + a[i + 1] - isOdd; dp2[i][1] = max(dp2[i + 1][0], dp2[i + 1][1]) + a[i + 1] - (!isOdd); } long long ans = 0; for (int i = 0; i <= n; ++i) { ans = max(ans, dp1[i][1]); ans = max(ans, dp1[i][0] + max(dp2[i][0], dp2[i][1])); ans = max(ans, dp2[i][1]); ans = max(ans, dp2[i][0] + max(dp1[i][0], dp1[i][1])); } cout << ans << endl; } int main() { init(); solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 100100; const int inf = 1000000000; bool vis[200001]; vector<int> v; long long n, m, k; string x, y; int dfs(int ind) { if (vis[ind]) return 1; vis[ind] = 1; return dfs(v[ind]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; v.push_back(-1); for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } int ans = 0; for (int i = 1; i < n + 1; i++) { if (!vis[i]) ans += dfs(i); } if (ans == 1) ans = 0; vector<int> b(n); int o = 0, z = 0; for (auto &x : b) { cin >> x; if (x) o++; else z++; } if (!(o % 2)) { ans++; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int SIZE = 500010; const int INF = 2147483640; const double eps = 1e-8; inline void RD(int &x) { x = 0; char c; c = getchar(); bool flag = 0; if (c == '-') flag = 1; while (c < '0' || c > '9') { if (c == '-') { flag = 1; } c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar(); } long long num[SIZE]; long long s1, s2, d, a1; long long yz[SIZE]; long long inv(long long a, long long b, long long mod) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } int n; long long m, tmp; int main(int argc, char const *argv[]) { scanf("%I64d%d", &m, &n); s1 = s2 = 0; for (int i = 1; i <= n; i++) { scanf("%I64d", &num[i]); s1 += num[i]; s2 += num[i] * num[i]; s1 %= m; s2 %= m; } if (n == 1) { printf("%lld 1\n", num[1]); return 0; } sort(num + 1, num + 1 + n); for (int i = 2; i <= n; i++) { d = (num[i] - num[1] + m) % m; a1 = (s1 % m - ((n % m * (n - 1) % m) % m * inv(2, m - 2, m) % m * d % m) % m + m) % m * inv(n, m - 2, m) % m; tmp = (n * a1) % m * a1 % m; tmp = (tmp + ((n % m * (n - 1) % m * (2 * n - 1) % m) % m * d % m * d % m * inv(6, m - 2, m) % m)) % m; tmp = (tmp + n % m * (n - 1) % m * d % m * a1 % m) % m; if (tmp == s2) { yz[1] = a1; for (int k = 2; k <= n; k++) yz[k] = (yz[k - 1] + d) % m; sort(yz + 1, yz + 1 + n); bool flag = 1; for (int k = 1; k <= n; k++) { if (yz[k] != num[k]) { flag = 0; break; } } if (flag) { printf("%I64d %I64d\n", a1, d); return 0; } } } printf("-1\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; struct Friend { int c, k, s; } F[105]; const long long inf = 0x7fffffffffffffff; long long a[2][1 << 20]; int cmp(Friend a, Friend b) { return a.k < b.k; } int main() { int n, m, k, i, j; while (scanf("%d%d%d", &n, &m, &k) != EOF) { for (i = 0; i < n; i++) { int tn; scanf("%d%d%d", &F[i].c, &F[i].k, &tn); for (j = F[i].s = 0; j < tn; j++) { int tmp; scanf("%d", &tmp); F[i].s |= 1 << (tmp - 1); } } sort(F, F + n, cmp); for (i = 0; i < 1 << m; i++) a[0][i] = a[1][i] = inf; a[1][0] = 0; long long ans = inf; for (i = 0; i < n; i++) { memcpy(a[0], a[1], sizeof(a[1])); for (j = 0; j < 1 << m; j++) if (a[0][j] != inf) a[1][j | F[i].s] = min(a[1][j | F[i].s], a[0][j] + F[i].c); if (a[1][(1 << m) - 1] != inf) { ans = min(ans, a[1][(1 << m) - 1] + (long long)F[i].k * k); a[1][(1 << m) - 1] = inf; } } printf("%I64d\n", ans == inf ? -1 : ans); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { char c = getchar(); bool w = false; while (c != EOF && c != '\n') { if (c == ' ') w = true; if (c == '.' || c == ',' || c == '!' || c == '?') { putchar(c); w = true; } if ((c != ' ' && c != '.' && c != ',' && c != '!' && c != '?')) { if (w) { w = false; putchar(' '); } putchar(c); } c = getchar(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int a, b; long long ans = -1e15; int idx; long long sqr(long long x) { return x * x; } long long solve(int i) { long long res = 1LL * i * i + (a - i); long long cnt1 = a - i + 2; long long cnt = b % cnt1; long long tmp = cnt * sqr((b / cnt1) + 1); tmp += (cnt1 - cnt) * sqr(b / cnt1); res -= tmp; return res; } void printt(int val, char s) { while (val--) { printf("%c", s); } } void print(int i) { vector<int> v; int cnt1 = a - i + 2; int cnt = b % cnt1; for (int i = 0; i < cnt; ++i) { v.emplace_back((b / cnt1) + 1); } for (int i = 0; i < b - cnt; ++i) { v.emplace_back(b / cnt1); } vector<int> v2; v2.emplace_back(i); for (int j = i + 1; j <= a; ++j) { v2.emplace_back(1); } int x = 0; int y = 0; for (int j = 1; j <= a - i + 1 + a - i + 2; ++j) { if (j & 1) { printt(v[x++], 'x'); } else { printt(v2[y++], 'o'); } } } int main() { cin >> a >> b; if (!a) { cout << -sqr(b) << endl; for (int i = 0; i < b; ++i) { cout << "x"; } return 0; } if (!b) { cout << sqr(a) << endl; for (int i = 0; i < a; ++i) { cout << "o"; } return 0; } for (int i = 1; i <= a; ++i) { long long tmp = solve(i); if (tmp > ans) { ans = tmp; idx = i; } } cout << ans << "\n"; print(idx); }
3
#include <bits/stdc++.h> using namespace std; const int mo = 1e9 + 7; int n, m, k; int a[15][15]; int f[15][15]; int Log2[1 << 12]; int use[15]; int lowbit(int x) { return x & -x; } int dfs(int x, int y) { if (y > m) ++x, y = 1; if (x > n) return 1; int res = 0, first = -1; int bit = f[x - 1][y] | f[x][y - 1]; for (int i = (~bit) & ((1 << k) - 1); i; i -= lowbit(i)) { int tmp = Log2[lowbit(i)]; if (a[x][y] == 0 || a[x][y] == tmp) { ++use[tmp]; f[x][y] = bit | (1 << (tmp - 1)); if (use[tmp] == 1) { if (first == -1) first = dfs(x, y + 1); res += first; } else res += dfs(x, y + 1); res = res % mo; --use[tmp]; } } return res; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { scanf("%d", &a[i][j]); ++use[a[i][j]]; } if (n + m - 1 > k) { printf("0\n"); return 0; } int tmp = 1; for (int i = 1; i <= k; ++i) { Log2[tmp] = i; tmp *= 2; } printf("%d", dfs(1, 1)); }
2
#include <bits/stdc++.h> using namespace std; const int M = 200005; struct SuffixArray { string a; int N, m; vector<int> SA, LCP, x, y, w, c; SuffixArray(string _a, int m = 256) : a(" " + _a), N(a.size()), m(m), SA(N), LCP(N), x(N), y(N), w(max(m, N)), c(N) { a[0] = 0; DA(); kasaiLCP(); { rotate(SA.begin(), SA.begin() + 1, SA.end()); SA.pop_back(); }; { rotate(LCP.begin(), LCP.begin() + 1, LCP.end()); LCP.pop_back(); }; a.erase(a.begin()); for (int i = 0; i < (int)SA.size(); ++i) --SA[i]; buildRMQ(); } inline bool cmp(const int a, const int b, const int l) { return (y[a] == y[b] && y[a + l] == y[b + l]); } void Sort() { for (int i = 0; i < m; ++i) w[i] = 0; for (int i = 0; i < N; ++i) ++w[x[y[i]]]; for (int i = 0; i < m - 1; ++i) w[i + 1] += w[i]; for (int i = N - 1; i >= 0; --i) SA[--w[x[y[i]]]] = y[i]; } void DA() { for (int i = 0; i < N; ++i) x[i] = a[i], y[i] = i; Sort(); for (int i, j = 1, p = 1; p < N; j <<= 1, m = p) { for (p = 0, i = N - j; i < N; i++) y[p++] = i; for (int k = 0; k < N; ++k) if (SA[k] >= j) y[p++] = SA[k] - j; Sort(); for (swap(x, y), p = 1, x[SA[0]] = 0, i = 1; i < N; ++i) x[SA[i]] = cmp(SA[i - 1], SA[i], j) ? p - 1 : p++; } } void kasaiLCP() { for (int i = 0; i < N; i++) c[SA[i]] = i; for (int i = 0, j, k = 0; i < N; LCP[c[i++]] = k) if (c[i] > 0) for (k ? k-- : 0, j = SA[c[i] - 1]; a[i + k] == a[j + k]; k++) ; else k = 0; }; vector<vector<pair<int, int> > > rmq; void buildRMQ() { rmq.resize(N, vector<pair<int, int> >(20, {1e9, 1e9})); for (int i = N - 1; i > 0; --i) { rmq[i][0] = make_pair(LCP[i], i); for (int j = 1; j < 20; ++j) { if (i + (1 << j) - 1 >= N) continue; rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]); } } } pair<int, int> get(int l, int r) { int lg_val = log2(r - l); return min(rmq[l + 1][lg_val], rmq[r - (1 << lg_val) + 1][lg_val]); } }; int id[M], n, cnt; string s, inv; long long answer; set<int> pos[M]; int solve(int l, int r, int x, int y, SuffixArray &sa, SuffixArray &sa2) { int idx = ++cnt; if (l == r) { int p = sa.SA[r]; if (p == 0) { return idx; } int leftside = p; int rightside = s.size() - p; y = min(y, rightside); if (y >= x) answer += 1LL * (y - x + 1) * leftside; pos[idx].insert(id[p]); return idx; } auto minpos = sa.get(l, r); int idlarge, idsmall; if (minpos.second - l > r - minpos.second) { idlarge = solve(l, minpos.second - 1, x, y, sa, sa2); idsmall = solve(minpos.second, r, minpos.first + 1, y, sa, sa2); } else { idlarge = solve(minpos.second, r, x, y, sa, sa2); idsmall = solve(l, minpos.second - 1, minpos.first + 1, y, sa, sa2); } for (auto t : pos[idsmall]) { auto it = pos[idlarge].upper_bound(t); int maxlen = n - sa2.SA[t], bound = -1; if (it != pos[idlarge].end()) { bound = max(bound, sa2.get(t, *it).first); } if (it != pos[idlarge].begin()) { --it; bound = max(bound, sa2.get(*it, t).first); } if (bound < maxlen && minpos.first >= x) answer += 1LL * (maxlen - bound) * (minpos.first - x + 1); pos[idlarge].insert(t); } return idlarge; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); if (fopen("inp", "r")) freopen("inp", "r", stdin); cin >> s; inv = s; reverse(inv.begin(), inv.end()); n = s.size(); SuffixArray sa(s + " "), sa2(inv + " "); answer = 1LL * n * (n + 1) / 2; for (auto &x : sa.LCP) answer -= x; for (int i = 0; i < sa2.SA.size(); ++i) { int p = sa2.SA[i]; id[n - p + 1] = i; } solve(0, n, 0, n + 5, sa, sa2); cout << answer + 1 << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; vector<vector<int>> tmp, v; vector<int> dis; int n, m; bool hlp(int i, int j) { if (i > n || j > m || !v[i][j]) return 0; if (i == n && j == m) return 1; if (tmp[i][j] != -1) return tmp[i][j]; return tmp[i][j] = hlp(i + 1, j) + hlp(i, j + 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; tmp = vector<vector<int>>(n + 1, vector<int>(m + 1, -1)); v = vector<vector<int>>(n + 1, vector<int>(m + 1)); dis = vector<int>(n + m); char ch; int ans = 2; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { cin >> ch; if (ch == '.') v[i][j] = 1; } hlp(1, 1); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (tmp[i][j] > 0) ++dis[i + j - 1]; for (int i = 2; i < dis.size() - 1; ++i) ans = min(ans, dis[i]); cout << ans; }
4
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int dy[] = {1, 1, 1, 0, 0, 0, -1, -1, -1}; vector<vector<string> > a(10, vector<string>(8, string(8, '.'))); int dp[10][10][10]; int go(int n, int x, int y) { int& res = dp[n][x][y]; if (res != -1) return res; if (n == 8) return (res = 1); for (int i = 0; i < 9; i++) { int x2 = x + dx[i]; int y2 = y + dy[i]; if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) { if (x2 == 7 && y2 == 7) return 1; if (a[n][y2][x2] == '.' && a[n + 1][y2][x2] == '.' && go(n + 1, x2, y2)) return (res = 1); } } return (res = 0); } int main() { for (int n = 0; n < 10; n++) for (int x = 0; x < 10; x++) for (int y = 0; y < 10; y++) dp[n][x][y] = -1; for (int i = 7; i >= 0; i--) cin >> a[0][i]; a[0][0][0] = '.'; a[0][7][7] = '.'; for (int j = 1; j < 8; j++) for (int i = 0; i + j < 8; i++) a[j][i] = a[0][j + i]; if (go(0, 0, 0)) cout << "WIN" << endl; else cout << "LOSE" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long n = 0, x[1010], y[1010], f[1010], m, d[1010][1010], c[1010][1010]; long long bin(long long x, long long a) { long long y = 1; while (a) { if (a & 1) y *= x, y %= 1000000007ll; x *= x, x %= 1000000007ll; a >>= 1; } return y; } void pre(void) { f[0] = 1; for (int i = 1; i < 1010; ++i) f[i] = (i * f[i - 1]) % 1000000007ll; for (int i = 0; i < 1010; ++i) c[i][0] = 1; for (int i = 1; i < 1010; ++i) for (int j = 1; j <= i; ++j) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; if (c[i][j] >= 1000000007ll) c[i][j] -= 1000000007ll; } } void read(void) { cin >> m; for (int i = 1; i <= m; ++i) { cin >> x[i]; n += x[i]; } for (int i = 1; i <= m; ++i) { cin >> y[i]; } } void din(void) { d[0][0] = 1; for (int i = 0; i <= m; ++i) for (int s = 0; s <= n; ++s) for (int j = 0; j <= s + x[i] && j <= y[i]; ++j) d[i + 1][s + x[i] - j] = (d[i + 1][s + x[i] - j] + d[i][s] * c[s + x[i]][j]) % 1000000007ll; long long ans = d[m + 1][0]; ans = (ans * f[n]) % 1000000007ll; for (int i = 1; i <= n; ++i) ans = (ans * bin(f[x[i]], 1000000007ll - 2)) % 1000000007ll; cout << ans << "\n"; } int main() { pre(); read(); din(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e2 + 5; int a[maxn]; int bo[maxn]; int fin[105]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; bo[a[i]]++; } sort(a + 1, a + n + 1); int maxx = 1; int i = 0; while (n) { int k = 1; while (bo[a[i]]) { if (a[i] < fin[k]) { k++; } fin[k]++; bo[a[i]]--; n--; } i++; if (k > maxx) { maxx = k; } } cout << maxx << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long ml = 5e3 + 101; const long long mo = 998244353; long long n, m, k, ans, inv, mul; long long dp[ml][ml]; inline long long read() { long long r = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) r = (r << 1) + (r << 3) + (c ^ 48), c = getchar(); return r * f; } inline void wr(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) wr(x / 10); putchar(x % 10 + '0'); } inline void port(long long x, char s) { wr(x); putchar(s); } long long qpow(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = res * x % mo; x = x * x % mo; y >>= 1; } return res; } int main() { n = read(), m = read(), k = read(); dp[0][0] = 1; for (long long i = 1; i <= k; i++) { for (long long j = 1; j <= i; j++) dp[i][j] = (dp[i - 1][j] * j % mo + dp[i - 1][j - 1] * (n - j + 1) % mo) % mo; } mul = inv = qpow(m, mo - 2); for (long long i = 1; i <= k; i++) ans = (ans + dp[k][i] * mul % mo) % mo, mul = (mul * inv) % mo; port(ans, '\n'); return 0; }
6
#include <bits/stdc++.h> using namespace std; void solve() { int q; cin >> q; int n, k, d; unordered_map<int, int> used; int k1 = 1, a[200001]; for (int p = 0; p < q; p++) { k1 = 1; cin >> n >> k >> d; cin >> a[0]; used[a[0]] = 1; int mn = 1; for (int i = 1; i < n; i++) { cin >> a[i]; if (i > d - 1) { used[a[i - d]]--; if (used[a[i - d]] == 0) k1--; used[a[i]]++; if (used[a[i]] == 1) k1++; mn = min(k1, mn); } else if (i == d - 1) { if (!used[a[i]]) k1++; used[a[i]]++; mn = k1; } else { if (!used[a[i]]) k1++; used[a[i]]++; } } used.clear(); cout << mn << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < 1; i++) { solve(); cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000, MAXM = 30000, oo = 2000000009; int n, m, s, t, a[MAXM * 2 + 9], b[MAXM * 2 + 9], c[MAXM * 2 + 9], del[MAXM * 2 + 9], par[MAXN + 9], ans; int cntNum, num[MAXN + 9], low[MAXN + 9], bridge[MAXM * 2 + 9]; vector<int> adj[MAXN + 9], vAns; int Rev(int x) { if (x > m) { return x - m; } return x + m; } void FindPath(int start) { fill(par, par + n + 1, -1); par[start] = 0; deque<int> dq; dq.push_back(start); while (!dq.empty()) { int x = dq.front(); dq.pop_front(); for (int i = 0; i < adj[x].size(); i++) { int id = adj[x][i]; if (!del[id] && par[b[id]] == -1) { par[b[id]] = id; dq.push_back(b[id]); } } } } void Dfs(int x) { cntNum++; num[x] = cntNum; low[x] = n + 1; for (int i = 0; i < adj[x].size(); i++) { int id = adj[x][i]; if (!del[id]) { del[Rev(id)] = 1; if (!num[b[id]]) { par[b[id]] = id; Dfs(b[id]); low[x] = min(low[x], low[b[id]]); } else { low[x] = min(low[x], num[b[id]]); } } } } int main() { ios::sync_with_stdio(false); ; cin >> n >> m >> s >> t; for (int i = 1; i <= m; i++) { cin >> a[i] >> b[i] >> c[i]; a[i + m] = b[i]; b[i + m] = a[i]; c[i + m] = c[i]; adj[a[i]].push_back(i); adj[b[i]].push_back(i + m); } ans = oo; FindPath(s); if (par[t] == -1) { cout << "0\n0\n"; return 0; } for (int dEdge = par[t]; dEdge != 0; dEdge = par[a[dEdge]]) { fill(del, del + m * 2 + 1, 0); del[dEdge] = 1; del[Rev(dEdge)] = 1; FindPath(s); if (par[t] == -1) { if (c[dEdge] < ans) { ans = c[dEdge]; vAns.clear(); vAns.push_back((dEdge - 1) % m + 1); } continue; } cntNum = 0; fill(num, num + n + 1, 0); Dfs(s); if (num[t] != 0) { for (int dEdge2 = par[t]; dEdge2 != 0; dEdge2 = par[a[dEdge2]]) { if (low[b[dEdge2]] >= num[b[dEdge2]]) { if (c[dEdge] + c[dEdge2] < ans) { ans = c[dEdge] + c[dEdge2]; vAns.clear(); vAns.push_back((dEdge - 1) % m + 1); vAns.push_back((dEdge2 - 1) % m + 1); } } } } } if (ans == oo) { cout << "-1\n"; return 0; } cout << ans << "\n"; cout << vAns.size() << "\n"; for (int i = 0; i < vAns.size(); i++) { cout << vAns[i] << " "; } cout << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int N; string A, B; vector<int> freqA[30]; vector<int> freqB[30]; double ans; void solve(vector<int> a, vector<int> b) { int j = -1; long long int sum = 0; for (auto i : a) { while (j + 1 < b.size() && b[j + 1] <= i) { ++j; sum += b[j]; } ans += (N - i + 1LL) * sum; } swap(a, b); j = -1; sum = 0; for (auto i : a) { while (j + 1 < b.size() && b[j + 1] < i) { ++j; sum += b[j]; } ans += (N - i + 1LL) * sum; } } int main() { ios_base ::sync_with_stdio(false); cin >> N; cin >> A; A = " " + A; cin >> B; B = " " + B; for (int i = 1; i <= N; ++i) { freqA[A[i] - 'A' + 1].push_back(i); freqB[B[i] - 'A' + 1].push_back(i); } for (int i = 1; i <= 26; ++i) solve(freqA[i], freqB[i]); long long int cnt = 0; for (int i = 1; i <= N; ++i) cnt += (N - i + 1LL) * (N - i + 1LL); double res = ans / cnt; cout << fixed << setprecision(9) << res << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p) { out << '(' << p.first << ", " << p.second << ')'; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T> const& v) { long long l = v.size(); for (long long i = 0; i < l - 1; i++) out << v[i] << ' '; if (l > 0) out << v[l - 1]; return out; } template <typename T> ostream& operator<<(ostream& out, set<T> const& v) { for (auto i = v.begin(); i != v.end(); i++) out << (*i) << ' '; return out; } template <typename T, typename V> ostream& operator<<(ostream& out, map<T, V> const& v) { for (auto i = v.begin(); i != v.end(); i++) out << "\n" << (i->first) << ":" << (i->second); return out; } template <typename T> void trace(const char* name, T&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename T, typename... Args> void trace(const char* names, T&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; trace(comma + 1, args...); } const int N = 1e5 + 199; long double t[4 * N]; long double combine(long double a, long double b) { return a * b; } void build(int v, int tl, int tr) { if (tl == tr) { t[v] = 1.0; } else { int tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); t[v] = combine(t[v * 2], t[v * 2 + 1]); } } void update(int v, int tl, int tr, int pos, long double new_val) { if (tl == tr) { t[v] = new_val; } else { int tm = (tl + tr) / 2; if (pos <= tm) update(v * 2, tl, tm, pos, new_val); else update(v * 2 + 1, tm + 1, tr, pos, new_val); t[v] = combine(t[v * 2], t[v * 2 + 1]); } } struct event { int a, h, l, r, s, ind; }; int n, m; map<int, vector<event>> sweepin, sweepout, sweepoutleft; map<int, vector<pair<int, int>>> shrooms; long double ans[N]; int solve() { cin >> n >> m; set<int> all; for (int i = 1; i <= n; i++) { int a, h, l, r; cin >> a >> h >> l >> r; int s = 100 - l - r; sweepin[a].push_back({a, h, l, r, s, i}); sweepout[a + h].push_back({a, h, l, r, s, i}); sweepoutleft[a - h].push_back({a, h, l, r, s, i}); all.insert(a); all.insert(a + h); all.insert(a - h); } build(1, 1, n); for (int i = 1; i <= m; i++) { int b, z; cin >> b >> z; shrooms[b].push_back({z, i}); ans[i] = z; all.insert(b); } for (auto i : all) { for (auto s : shrooms[i]) { ans[s.second] *= t[1]; } for (auto j : sweepin[i]) { long double fallinleft = (j.l + j.s + 0.00) / 100.00; update(1, 1, n, j.ind, fallinleft); } for (auto j : sweepout[i]) { update(1, 1, n, j.ind, 1.0); } } build(1, 1, n); for (auto it = all.rbegin(); it != all.rend(); it++) { int i = *it; for (auto s : shrooms[i]) { ans[s.second] *= t[1]; } for (auto j : sweepin[i]) { long double fallinright = (j.r + j.s + 0.00) / 100.00; update(1, 1, n, j.ind, fallinright); } for (auto j : sweepoutleft[i]) { update(1, 1, n, j.ind, 1.0); } } long double final_ans = 0.0; for (int i = 1; i <= m; i++) { final_ans += ans[i]; } cout << fixed << setprecision(12) << final_ans << "\n"; return 0; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) solve(); }
5
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main(){ while(1){ int w, q; int a[101]; scanf(" %d %d", &w, &q); if(w==0) break; memset(a, -1, sizeof(a)); for(int i=0; i<q; ++i){ char order; scanf(" %c", &order); if(order=='s'){ int x, y; scanf(" %d %d", &x, &y); bool flag=true; for(int i=0; i+y<=w; ++i){ if(a[i]==-1){ bool sl=true; for(int j=0; j<y; ++j){ if(a[i+j]!=-1){ sl=false; break; } } if(sl){ for(int j=0; j<y; ++j) a[i+j]=x; printf("%d\n", i); flag=false; } } if(!flag) break; } if(flag) printf("impossible\n"); } else if(order=='w'){ int z; scanf(" %d", &z); for(int i=0; i<w; ++i){ if(z==a[i]) a[i]=-1; } } } printf("END\n"); } }
0
#include <bits/stdc++.h> using namespace std; bool debug = false; const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1, 0}; template <typename X> inline X square(const X& a) { return a * a; } int scan_d() { bool minus = false; int result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } if (ch == '-') minus = true; else result = ch - '0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = (result << 3) + (result << 1) + (ch - '0'); } if (minus) return -result; else return result; } long scan_ld() { bool minus = false; long result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } if (ch == '-') minus = true; else result = ch - '0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = (result << 3) + (result << 1) + (ch - '0'); } if (minus) return -result; else return result; } long long scan_lld() { bool minus = false; long long result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } if (ch == '-') minus = true; else result = ch - '0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = (result << 3) + (result << 1) + (ch - '0'); } if (minus) return -result; else return result; } long long unsigned scan_llu() { long long unsigned result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } result = ch - '0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = (result << 3) + (result << 1) + (ch - '0'); } return result; } void print_d(int n) { if (n < 0) { n = -n; putchar('-'); } int i = 10; char output_buffer[10]; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 10); } void print_ld(long n) { if (n < 0) { n = -n; putchar('-'); } int i = 11; char output_buffer[11]; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 11); } void print_lld(long long n) { if (n < 0) { n = -n; putchar('-'); } int i = 21; char output_buffer[21]; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 21); } void print_llu(long long unsigned n) { int i = 21; char output_buffer[21]; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 21); } void println_d(int n) { if (n < 0) { n = -n; putchar('-'); } int i = 10; char output_buffer[11]; output_buffer[10] = '\n'; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 11); } void println_ld(long n) { if (n < 0) { n = -n; putchar('-'); } int i = 11; char output_buffer[12]; output_buffer[11] = '\n'; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 12); } void println_lld(long long n) { if (n < 0) { n = -n; putchar('-'); } int i = 21; char output_buffer[22]; output_buffer[21] = '\n'; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 22); } void println_llu(long long unsigned n) { int i = 21; char output_buffer[22]; output_buffer[21] = '\n'; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 22); } char sp; void printsp_d(int n) { if (n < 0) { n = -n; putchar('-'); } int i = 10; char output_buffer[11]; output_buffer[10] = sp; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 11); } void printsp_ld(long n) { if (n < 0) { n = -n; putchar('-'); } int i = 11; char output_buffer[12]; output_buffer[11] = sp; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 12); } void printsp_lld(long long n) { if (n < 0) { n = -n; putchar('-'); } int i = 21; char output_buffer[22]; output_buffer[21] = sp; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 22); } void printsp_llu(long long unsigned n) { int i = 21; char output_buffer[22]; output_buffer[21] = sp; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar(output_buffer[i]); } while (++i < 22); } int main() { int n, i, odd, even, beg, end; long long temp = 0; vector<int> a; beg = end = -1; odd = even = 0; n = scan_d(); for (i = 0; i < n; i++) a.push_back(scan_d()); for (i = 0; i < n; i++) { if (a[i] & 1) odd++; else { end = i; even++; } } if (odd == n) { cout << "First"; return 0; } if (even == n) { cout << "Second"; return 0; } for (i = beg; i <= end; i++) temp += a[i]; if (temp & 1) { cout << "First"; return 0; } if (beg != 0 or end != (n - 1)) cout << "First"; else cout << "Second"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int p[100002]; int main() { int n, s, t; scanf("%d%d%d", &n, &s, &t); p[0] = 0; for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); } vector<bool> used(100002, 0); int cnt = 0; bool found = (s == t); int news = s; while (news != t && !found) { cnt++; used[news] = 1; news = p[news]; if (used[news] == 1) { break; } else if (news == t) { found = true; break; } } if (found) { printf("%d", cnt); } else { printf("-1"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; vector <int> ans; int v[101]; void solve () { int n; cin >> n; for (int i = 1;i<=n;++i) cin >> v[i]; ans.push_back(v[1]); for (int i = 2;i<=n;++i) { int place = -1; for (int j = 0;j<ans.size();++j) if (ans[j] < v[i]) place = j; if (place!=-1) ans[place] = v[i]; else ans.push_back(v[i]); } cout << ans.size() << '\n'; ans.clear(); return ; } int main () { int t; cin >> t; while (t--) solve(); }
1
#include <bits/stdc++.h> using namespace std; long long inline mo(long long a) { return a % (long long)(1e9 + 7); } long long po(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res % p; } template <typename T> void inline enter(vector<T> &a) { for (T &e : a) cin >> e; } 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...); } void JUDGE() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; long long cost_fire, range, cost_ber; cin >> cost_fire >> range >> cost_ber; vector<long long> a(n), b(m); enter(a); enter(b); b.push_back(-(long long)(1e18 + 5)); a.push_back(-(long long)(1e18 + 5)); n++; m++; long long pos = 1, j = 0; vector<vector<long long>> spaces; for (long long i = 0; i < m; i++) { vector<long long> space; while (j < n && a[j] != b[i]) { space.push_back(a[j]); j++; } if (j >= n) { pos = 0; break; } j++; spaces.push_back(space); } if (!pos) { cout << -1 << "\n"; return 0; } long long si = spaces.size(), ans = 0; for (long long i = 0; i < si; i++) { long long cur_si = spaces[i].size(); if (!cur_si) continue; vector<long long> space = spaces[i]; long long left = -(long long)(1e18 + 5), right = -(long long)(1e18 + 5); if (i) left = b[i - 1]; right = b[i]; if (cur_si < range) { if (*max_element(begin(space), end(space)) > max(left, right)) { pos = 0; break; } else { ans += cost_ber * cur_si; continue; } } if (range * cost_ber <= cost_fire) { long long great = 0; for (long long j = 0; j < cur_si; j++) great += (space[j] > max(left, right)); if (!great) { ans += cost_ber * cur_si; continue; } ans += cost_fire + (cur_si - range) * cost_ber; } else { long long rem = cur_si % range; ans += rem * cost_ber + (cur_si / range) * cost_fire; } } if (!pos) { cout << -1 << "\n"; return 0; } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int maxn = 200000 + 2; constexpr int maxm = 300000 + 2; constexpr int maxq = 500000 + 2; int a[maxm], b[maxm], d[maxm]; int type[maxq], id[maxq]; int pi[maxn], p[maxn]; vector<int> add[maxq]; pair<int*, int> cp[maxq][2]; pair<int, int> spp[maxq]; int gp(int u) { return p[u] < 0 ? u : gp(p[u]); } set<pair<int, int>, greater<pair<int, int>>> mer[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, q; cin >> n >> m >> q; for (int i = 1; i <= n; i += 1) cin >> pi[i]; for (int i = 1; i <= m; i += 1) { cin >> a[i] >> b[i]; d[i] = q + 1; } for (int i = 1; i <= q; i += 1) { cin >> type[i] >> id[i]; if (type[i] == 2) d[id[i]] = i; } for (int i = 1; i <= n; i += 1) { p[i] = -1; mer[i].insert({pi[i], i}); } for (int i = 1; i <= m; i += 1) add[d[i]].push_back(i); for (int i = q + 1; i >= 1; i -= 1) { for (int j : add[i]) { int pa = gp(a[j]), pb = gp(b[j]); if (pa == pb) { type[i] = 0; continue; } if (p[pa] < p[pb]) swap(pa, pb); if (i <= q) cp[i][1] = {&p[pb], p[pb]}; p[pb] += p[pa]; if (i <= q) cp[i][0] = {&p[pa], p[pa]}; p[pa] = pb; if (i <= q) spp[i] = {pa, pb}; for (auto p : mer[pa]) mer[pb].insert(p); } } for (int i = 1; i <= q; i += 1) { if (type[i] == 1) { int r = gp(id[i]), x = 0; while (not mer[r].empty()) { auto p = *mer[r].begin(); mer[r].erase(p); if (pi[p.second]) { x = p.second; break; } } cout << pi[x] << "\n"; pi[x] = 0; } else if (type[i] == 2) { *cp[i][0].first = cp[i][0].second; *cp[i][1].first = cp[i][1].second; for (auto x : mer[spp[i].first]) if (mer[spp[i].second].count(x)) mer[spp[i].second].erase(x); } continue; for (int i = 1; i <= n; i += 1) if (mer[i].size() and p[i] < 0) { for (auto p : mer[i]) cout << p.second << " "; cout << "; "; } cout << endl; } return 0; }
4
#include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <cstring> #include <queue> using namespace std; double s; int n,m,set[11000]; struct point { double x,y; }p[11000]; struct edge { int x,y; double w; }e[510000]; bool cmp(const struct edge &a,const struct edge &b){return b.w<a.w;} double dis(point&a,point&b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int findset(int x) { if(x!=set[x]) set[x]=findset(set[x]); return set[x]; } void unionset(int x,int y) { int fx=findset(x),fy=findset(y); if(fx!=fy) set[fx]=fy; } void Krusal() { for(int i=0;i<=n;i++) set[i]=i; sort(e,e+m,cmp); for(int i=0;i<m;i++) { int a=findset(e[i].x),b=findset(e[i].y); if(a==b) continue; s-=e[i].w; unionset(a,b); } } int main() { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); s=0.0; for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); u--;v--; e[i].x=u;e[i].y=v; e[i].w=dis(p[u],p[v]); s+=e[i].w; } Krusal(); printf("%.3lf",s); return 0; }
0
#include <iostream> #include <vector> int main(){ int n; std::cin >> n; std::vector<int> co(200000,0),dp(n,0),da(n); int c; std::cin >> c; da[0]=c; dp[0]=1; co[c-1]=1; for(int i=1;i<n;i++){ std::cin >> c; da[i]=c; dp[i]=dp[i-1]; if(da[i]!=da[i-1]){ dp[i]=(co[c-1]+dp[i])%1000000007; co[c-1]=(dp[i-1]+co[c-1])%1000000007; } } std::cout << dp[n-1] << std::endl; }
0
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int pri[1010],wai[1010],dp[2][1010]; int main(){ int W,N,cas=0; while(true){ cas++; cin>>W; if(W==0) break; cin>>N; for(int i=1;i<=N;i++){ scanf("%d,%d",&pri[i],&wai[i]); } fill(dp[0],dp[0]+W+1,0); fill(dp[1],dp[1]+W+1,0); int in=0,ou=1; for(int i=1;i<=N;i++){ for(int j=0;j<=W;j++) dp[ou][j]=dp[in][j]; for(int j=0;j<=W;j++){ if(j+wai[i]<=W){ dp[ou][j+wai[i]]=max(dp[ou][j+wai[i]],dp[in][j]+pri[i]); } } swap(in,ou); } int res=0,cnt=0; for(int i=0;i<=W;i++){ if(res<dp[in][i]){ res=dp[in][i]; cnt=i; } } cout<<"Case "<<cas<<":"<<endl<<res<<endl<<cnt<<endl; } return 0; }
0
#include<bits/stdc++.h> #define ll long long #define vll vector<ll> using namespace std; int main() { ll n,m,k,i,j; cin>>n>>m>>k; for(i=0;i<=n;i++)for(j=0;j<=m;j++)if(i*m+j*n-2*i*j==k) { cout<<"Yes";return 0; } cout<<"No"; }
0
#include <bits/stdc++.h> using namespace std; long long a[100000], n, k, S; int b[100000], t; long long f(long long x) { long long res = 0; for (int i = 0; i < (int)(n); i++) if (a[i] <= x) res += a[i]; else res += x; return res; } int main() { scanf("%I64d%I64d", &n, &k); for (int i = 0; i < (int)(n); i++) { scanf("%I64d", a + i); S += a[i]; } if (k > S) { puts("-1"); return 0; } long long l = 0, r = 1000000001; while (r - l > 1) { long long m = (l + r) / 2; if (f(m) <= k) l = m; else r = m; } k -= f(l); if (k < 0) { puts("-1"); return 0; } for (int i = 0; i < (int)(n); i++) if (a[i] > l + 1) b[t++] = i + 1; else if (t >= k && a[i] > l) b[t++] = i + 1; else if (t < k && a[i] == l + 1) --k; rotate(b, b + k, b + t); for (int i = 0; i < (int)(t); i++) printf("%d ", b[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; int N; string S; int get(int f, int s) { if(s < 0) return 1; int ret = 0; int d[10] = {0}; for(int i = f; i < N-s; i++) { int c = S[i] - '0'; if(d[c]) continue; d[c]++; ret += get(i+1, s-1); } return ret; } int main() { cin >> N >> S; cout << get(0, 2); }
0
#include <bits/stdc++.h> int main() { int n; std::cin >> n; std::vector<std::pair<int, int>> r(n); std::vector<std::pair<int, int>> b(n); for (auto &i : r) { std::cin >> i.second >> i.first; } for (auto &i : b) { std::cin >> i.first >> i.second; } std::sort(r.begin(), r.end()); std::sort(b.begin(), b.end()); int count = 0; for (auto &i : b) { std::vector<decltype(r)::iterator> itr_v; for (auto itr = r.begin(); itr != r.end(); itr++) { if (itr->first < i.second && itr->second < i.first) { itr_v.push_back(itr); } } if (!itr_v.empty()) { count++; r.erase(itr_v.back()); } } std::cout << count << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; void dv(vector<long long int> g1) { for (auto i = g1.begin(); i != g1.end(); ++i) { cout << *i << " "; } } int invert(int a) { if (a == 0) { return 1; } else if (a == 1) { return 0; } else { return -1; } } long long int fact(long long int n) { long long int res = 1; for (long long int i = 2; i <= n; i++) res = res * i; return res; } long long int nCr(long long int n, long long int r) { return fact(n) / (fact(r) * fact(n - r)); } void swap(int &x, int &y) { int temp; temp = x; x = y; y = temp; return; } int main() { long long int n; cin >> n; if (n == 4 || n == 2) { if (n == 4) { cout << 3 << endl; } else { cout << 1 << endl; } } else { cout << nCr(n, n / 2) * (fact(n / 2 - 1) * (fact(n / 2 - 1)) / 2) << endl; } }
5
#include <bits/stdc++.h> using namespace std; int const N = 100001; int n, q, h[N], en[N], ex[N], cm, fm[N], pa[N][18], lg[N], mxl; vector<vector<pair<int, int> > > x; vector<int> tr[N], f; int t = 0; void pl(int v, int p = 0, int hi = 0) { en[v] = ++t; h[v] = hi; fm[v] = cm; pa[v][0] = p; for (int i = 1; i < mxl; ++i) if (pa[v][i - 1]) pa[v][i] = pa[pa[v][i - 1]][i - 1]; else break; x[cm].push_back(make_pair(h[v], en[v])); for (int i = 0; i < tr[v].size(); ++i) if (tr[v][i] != p) pl(tr[v][i], v, hi + 1); ex[v] = t + 1; } int main() { scanf("%d", &n); int z = 0, r = 2; for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); if (t) tr[t].push_back(i + 1); else f.push_back(i + 1); if (i + 1 == r) r <<= 1, ++z; lg[i + 1] = z; } mxl = lg[n] + 1; for (int i = 0; i < f.size(); ++i) { x.resize(i + 1); pl(f[i]); sort(x[cm].begin(), x[cm].end()); ++cm; } scanf("%d", &q); while (q--) { int v, l; scanf("%d%d", &v, &l); if (h[v] < l) printf("0 "); else { int t = h[v]; while (l) { v = pa[v][lg[l]]; l -= 1 << lg[l]; } int an = lower_bound(x[fm[v]].begin(), x[fm[v]].end(), make_pair(t, ex[v])) - lower_bound(x[fm[v]].begin(), x[fm[v]].end(), make_pair(t, en[v])); printf("%d ", an - 1); } } }
5
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; struct edge { int to, cap, cost, rev; edge(int to_, int cap_, int cost_, int rev_):to(to_), cap(cap_), cost(cost_), rev(rev_) {} }; constexpr int INF = (1 << 29); int V; vector<vector<edge>> G; vector<int> h; vector<int> dist; vector<int> prevv, preve; void add_edge(int from, int to, int cap, int cost) { if(from == to) return; G[from].emplace_back(to, cap, cost, G[to].size()); G[to].emplace_back(from, 0, -cost, G[from].size() - 1); } int solve(int s, int t, int k) { fill(begin(h), end(h), 0); int cost = 0; int ans = 0; while(true) { priority_queue<P, vector<P>, greater<P>> que; fill(begin(dist), end(dist), INF); dist[s] = 0; que.emplace(0, s); while(!que.empty()) { const P p = que.top(); que.pop(); const int v = p.second; if(dist[v] < p.first) continue; for(int i = 0; i < G[v].size(); ++i) { edge &e = G[v][i]; if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.emplace(dist[e.to], e.to); } } } if(dist[t] == INF) break; for(int v = 0; v < V; ++v) { h[v] += dist[v]; } const int d = 1; cost += d * h[t]; if(cost > k) break; for(int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } ++ans; } return ans; } int main() { int a, b, k; cin >> a >> b >> k; vector<vector<int>> mat(a, vector<int>(b, 0)); for(int i = 0; i < a; ++i) { string s; cin >> s; for(int j = 0; j < b; ++j) { if(s[j] == '1') { ++mat[i][j]; } } } for(int j = 0; j < b; ++j) { string s; cin >> s; for(int i = 0; i < a; ++i) { if(s[i] == '1') { ++mat[i][j]; } } } const int S = a + b; const int T = S + 1; V = a + b + 2; G.resize(V); h.resize(V); dist.resize(V); prevv.resize(V); preve.resize(V); for(int i = 0; i < a; ++i) { const int v = i; add_edge(S, v, 1, 0); for(int j = 0; j < b; ++j) { const int u = j + a; add_edge(v, u, 1, mat[i][j]); } } for(int j = 0; j < b; ++j) { const int v = j + a; add_edge(v, T, 1, 0); } cout << solve(S, T, k) << endl; return 0; }
0
#include <bits/stdc++.h> const int N = 1e5 + 5; int n, m; struct Chairman { static const int M = 20 * N; int idx, rt[N], ls[M], rs[M], val[M]; void modify(int &p, int l, int r, int u, int x, int v) { p = ++idx, ls[p] = ls[u], rs[p] = rs[u], val[p] = std::max(val[u], v); if (l == r) { return; } int mid = (l + r) >> 1; if (x <= mid) { modify(ls[p], l, mid, ls[u], x, v); } else { modify(rs[p], mid + 1, r, rs[u], x, v); } } int query(int p, int l, int r, int x, int y) { if (x <= l && r <= y) { return val[p]; } int mid = (l + r) >> 1; if (y <= mid) { return query(ls[p], l, mid, x, y); } else if (x > mid) { return query(rs[p], mid + 1, r, x, y); } else { return std::max(query(ls[p], l, mid, x, mid), query(rs[p], mid + 1, r, mid + 1, y)); } } } cha; int main() { scanf("%d%d", &n, &m); int ans = 0; for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w), w += 2; int now = cha.query(cha.rt[u], 1, 100001, 1, w - 1) + 1; cha.modify(cha.rt[v], 1, 100001, cha.rt[v], w, now); ans = std::max(ans, now); } printf("%d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<bool> level; for (int i = 0; i < n; ++i) { char temp; cin >> temp; if (temp == '*') level.push_back(true); else level.push_back(false); } for (int i = 0; i < n; ++i) { for (int j = 1; j < (n - i + 3) / 4; ++j) { if (level[i] && level[i + j] && level[i + 2 * j] && level[i + 3 * j] && level[i + 4 * j]) { cout << "yes\n"; return 0; } } } cout << "no\n"; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFF = 0x3f3f3f3f3f3f3f3fll; const long long M = 1e9 + 7; const long long maxn = 1e6 + 107; const double pi = acos(-1.0); const double eps = 0.0000000001; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } template <typename T> inline void pr2(T x, int k = 64) { long long i; for (i = 0; i < k; i++) fprintf(stderr, "%d", (x >> i) & 1); putchar(' '); } template <typename T> inline void add_(T &A, int B, long long MOD = M) { A += B; (A >= MOD) && (A -= MOD); } template <typename T> inline void mul_(T &A, long long B, long long MOD = M) { A = (A * B) % MOD; } template <typename T> inline void mod_(T &A, long long MOD = M) { A %= MOD; A += MOD; A %= MOD; } template <typename T> inline void max_(T &A, T B) { (A < B) && (A = B); } template <typename T> inline void min_(T &A, T B) { (A > B) && (A = B); } template <typename T> inline T abs(T a) { return a > 0 ? a : -a; } inline long long powMM(long long a, long long b, long long mod = M) { long long ret = 1; for (; b; b >>= 1ll, a = a * a % mod) if (b & 1) ret = ret * a % mod; return ret; } int startTime; void startTimer() { startTime = clock(); } void printTimer() { fprintf(stderr, "/--- Time: %ld milliseconds ---/\n", clock() - startTime); } char str[maxn], nxt[maxn]; int dpnow[maxn], dpnxt[maxn]; int l[maxn], l01[maxn], l10[maxn]; int main() { int _, i, j; int n; scanf("%d", &n); int lpre, lnow; dpnow[1] = 1; lpre = lnow = 0; for (_ = 1; _ <= n; _++) { scanf("%s", nxt + 1); lnow = strlen(nxt + 1); nxt[lnow + 1] = 1; for (i = lnow + 1; i >= 1; i--) { if (i > lpre || nxt[i] != str[i + 1]) l01[i] = i; else l01[i] = l01[i + 1]; if (i > lpre || nxt[i] != str[i - 1]) l10[i] = i; else l10[i] = l10[i + 1]; if (i > lpre || nxt[i] != str[i]) l[i] = i; else l[i] = l[i + 1]; } for (i = 1; i <= lnow + 1; i++) { dpnxt[i] = 0; for (j = 1; j <= lpre + 1; j++) { bool ok = 0; if (i == lnow + 1 && j == lpre + 1) { ok = str[l[1]] <= nxt[l[1]]; } if (i == lnow + 1) { if (l[1] < j) ok = str[l[1]] <= nxt[l[1]]; else ok = str[l01[j] + 1] <= nxt[l01[j]]; } else if (j == lnow + 1) { if (l[1] < i) ok = str[l[1]] <= nxt[l[1]]; else ok = str[l10[i + 1] - 1] <= nxt[l10[i + 1]]; } else if (i == j) { if (l[1] < i) ok = str[l[1]] <= nxt[l[1]]; else ok = str[l[i + 1]] <= nxt[l[i + 1]]; } else if (i < j) { if (l[1] < i) { if (str[l[1]] <= nxt[l[1]]) ok = 1; } else if (i <= l[1] && l10[i + 1] - 1 < j) { if (str[l10[i + 1] - 1] <= nxt[l10[i + 1]]) ok = 1; } else if (i <= l[1] && j <= l10[i + 1] - 1) { if (str[l[j + 1]] <= nxt[l[j + 1]]) ok = 1; } } else { if (l[1] < j) { if (str[l[1]] <= nxt[l[1]]) ok = 1; } else if (j <= l[1] && l01[j] < i) { if (str[l01[j] + 1] <= nxt[l01[j]]) ok = 1; } else if (j <= l[1] && i <= l01[j]) { if (str[l[i + 1]] <= nxt[l[i + 1]]) ok = 1; } } if (ok) add_(dpnxt[i], dpnow[j]); } } for (i = 1; i <= lpre + 1; i++) dpnow[i] = str[i] = 0; for (i = 1; i <= lnow + 1; i++) dpnow[i] = dpnxt[i], str[i] = nxt[i]; str[lnow + 1] = 0; lpre = lnow; } int ans = 0; for (i = 1; i <= lnow + 1; i++) add_(ans, dpnow[i]); printf("%d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; int main() { int t, n; scanf("%d", &t); while (t--) { scanf("%d", &n); printf("%d\n", n); for (int i = 1; i <= n; i++) printf("%d ", i); puts(""); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 50; int a[maxn], b[maxn - 1]; const int maxl = 51; long long d[maxn][maxl]; long long whole[maxl]; void solve(int *a, int n, int pmin, int pmax) { memset(d, 0, sizeof(d)); d[0][0] = 1; for (int it = 0; it < n; it++) { for (int taken = it; taken >= 0; taken--) { for (int sum = 0; sum <= pmax; sum++) { long long cur = d[taken][sum]; if (!cur) continue; if (sum + a[it] <= pmax) d[taken + 1][sum + a[it]] += cur; } } } for (int taken = 0; taken <= n; taken++) for (int x = pmin; x <= pmax; x++) whole[taken] += d[taken][x]; } int main() { int n; while (scanf("%d", &n) >= 1) { int sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } int p; scanf("%d", &p); if (p >= sum) { printf("%d\n", n); continue; } memset(whole, 0, sizeof(whole)); for (int i = 0; i < n; i++) { int r = 0; for (int j = 0; j < n; j++) if (j != i) b[r++] = a[j]; solve(b, n - 1, p - a[i] + 1, p); } double ans = 0; for (int i = 0; i < n; i++) { double cur = whole[i]; for (int j = 1; j <= i; j++) cur *= j; for (int j = 1; j <= n - 1 - i; j++) cur *= j; ans += cur * i; } for (int i = 1; i <= n; i++) ans /= i; printf("%.18lf\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, k, i; cin >> n >> m >> k; long long int a[m]; for (i = 0; i < m; i++) { cin >> a[i]; } long long int cnt = 0; long long int ans = 0; for (i = 0; i < m;) { long long int c = (a[i] - cnt - 1) / k; long long int j = i; while ((((a[i] - cnt - 1) / k) <= c) && i < m) { i++; } cnt += i - j; ans++; } cout << ans << endl; }
1
#include<bits/stdc++.h> using namespace std; int n,a,b,sum; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a>>b; sum+=b+1-a; } cout<<sum; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int n; long long int h; int main() { cin >> n; h = (n - 2) * (n - 2); cout << h << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long n, q, bs; long long input[2000000]; long long ans[2000000]; unordered_map<long long, long long> cnt; struct query { long long L, R, indx; } arr[2000000]; bool cmp(const query &n1, const query &n2) { long long block1 = n1.L / bs; long long block2 = n2.L / bs; if (block1 == block2) return n1.R < n2.R; else return block1 < block2; } long long sum = 0; void add(long long i) { cnt[input[i]]++; if (cnt[input[i]] == input[i]) sum++; if (cnt[input[i]] == input[i] + 1) sum--; } void del(long long i) { cnt[input[i]]--; if (cnt[input[i]] == input[i]) sum++; if (cnt[input[i]] == input[i] - 1) sum--; } int main() { scanf("%lld %lld", &n, &q); bs = sqrt(n); for (long long i = 0; i < n; i++) { scanf("%lld", &input[i]); } for (long long i = 0; i < q; i++) { scanf("%lld %lld", &arr[i].L, &arr[i].R); --arr[i].L; --arr[i].R; arr[i].indx = i; } sort(arr, arr + q, cmp); long long left = 0, right = -1; sum = 0; for (long long i = 0; i < q; i++) { long long l = arr[i].L; long long r = arr[i].R; long long in = arr[i].indx; while (l > left) { del(left++); } while (l < left) { add(--left); } while (r > right) { add(++right); } while (r < right) { del(right--); } ans[in] = sum; } for (long long i = 0; i < q; i++) printf("%lld\n", ans[i]); return 0; }
2
#include <bits/stdc++.h> long long a[30], num[(1 << 20) + 1]; int f[(1 << 20) + 1], g[(1 << 20) + 1], lg[(1 << 20) + 1], ppc[(1 << 20) + 1]; int main() { register int n, ans = 0; scanf("%d", &n); for (register int i = 1; i <= n; i++) { scanf("%lld", &a[i]); if (a[i] == 0) n--, i--; } lg[0] = -1; for (register int i = 1; i < (1 << n); i++) { lg[i] = lg[i / 2] + 1; num[i] = num[i ^ (1 << lg[i])] + a[lg[i] + 1]; ppc[i] = ppc[i / 2] + (i & 1); for (register int S = i ^ (1 << lg[i]), j = S; j; j = (j - 1) & S) { if ((num[j] - num[i - j]) > -ppc[i] && (num[j] - num[i - j]) < ppc[i] && ((num[j] - num[i - j]) & 1) != (ppc[i] & 1)) { g[i] = 1; break; } } } for (register int i = 0; i < (1 << n) - 1; i++) { if (f[i] > ans) ans = f[i]; register int S = ((1 << n) - 1) ^ i; if (f[i + (1 << lg[S])] < f[i]) f[i + (1 << lg[S])] = f[i]; for (register int j = S; j >= (1 << lg[S]); j = (j - 1) & S) { if (g[j] && f[i] >= f[i + j]) f[i + j] = f[i] + 1; } } if (f[(1 << n) - 1] > ans) ans = f[(1 << n) - 1]; printf("%d\n", n - ans); }
6
#include <bits/stdc++.h> using namespace std; vector<int> v; int main() { int n; cin >> n; int i, a[n]; for (i = 0; i < n; ++i) cin >> a[i]; int z = a[0], st = a[0], maxm = 0, l = 1; for (i = 1; i < n; ++i) { if (a[i] == abs(1 - z)) { l++; z = a[i]; } else { if (st != z && st == 0) { if (z == 0 && l & 1) v.push_back(0); for (int j = 0; j < l / 2; ++j) v.push_back(0); for (int j = 0; j < l / 2; ++j) v.push_back(1); if (z == 1 && l & 1) v.push_back(1); } else if (st != z && st == 1) { for (int j = 0; j < l / 2; ++j) v.push_back(1); if (z == 1 && l & 1) v.push_back(1); if (z == 0 && l & 1) v.push_back(0); for (int j = 0; j < l / 2; ++j) v.push_back(0); } else if (st == 0) { for (int j = 0; j < l; ++j) v.push_back(0); } else { for (int j = 0; j < l; ++j) v.push_back(1); } z = a[i]; st = a[i]; maxm = max(maxm, l); l = 1; } } if (st != z && st == 0) { if (z == 0 && l & 1) v.push_back(0); for (int j = 0; j < l / 2; ++j) v.push_back(0); for (int j = 0; j < l / 2; ++j) v.push_back(1); if (z == 1 && l & 1) v.push_back(1); } else if (st != z && st == 1) { for (int j = 0; j < l / 2; ++j) v.push_back(1); if (z == 1 && l & 1) v.push_back(1); if (z == 0 && l & 1) v.push_back(0); for (int j = 0; j < l / 2; ++j) v.push_back(0); } else if (st == 0) { for (int j = 0; j < l; ++j) v.push_back(0); } else { for (int j = 0; j < l; ++j) v.push_back(1); } maxm = max(maxm, l); if (maxm & 1) cout << (maxm / 2) << endl; else cout << maxm / 2 - 1 << endl; for (i = 0; i < v.size(); ++i) cout << v[i] << " "; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int K = 26; struct vertex { vector<int> next; int cnt; vertex() : next(K, -1), cnt(1) {} }; vector<vertex> trie; vector<vector<pair<int, char>>> graph; vector<int> sz; vector<int> height; void init_dfs(int v, int h = 0) { sz[v] = 1; height[v] = h; for (auto& e : graph[v]) { int to = e.first; init_dfs(to, h + 1); sz[v] += sz[to]; if (sz[to] > sz[graph[v][0].first]) swap(e, graph[v][0]); } } vector<int> ans; void transfuse(int v, vector<vertex>& t, int& last, int state = 0) { for (auto& e : graph[v]) { int to = e.first; int c = e.second - 'a'; if (t[state].next[c] == -1) { t[state].next[c] = last; ++last; } transfuse(to, t, last, t[state].next[c]); } } void cnt_trie(int state, vector<vertex>& t) { for (auto to : t[state].next) if (to != -1) { cnt_trie(to, t); t[state].cnt += t[to].cnt; } } int count(int stSub, vector<vertex>& subtrie, int stLeader, vector<vertex>& trie) { int res = 0; for (int c = 0; c < K; ++c) { if (subtrie[stSub].next[c] == -1) continue; if (trie[stLeader].next[c] == -1) res += subtrie[subtrie[stSub].next[c]].cnt; else res += count(subtrie[stSub].next[c], subtrie, trie[stLeader].next[c], trie); } return res; } void dfs(int v) { if (graph[v].size() == 0) return; int leader = graph[v][0].first; vector<vertex> subtrie(sz[v] - sz[leader]); int last = 1; for (int j = 1; j < graph[v].size(); ++j) { int u = graph[v][j].first; transfuse(u, subtrie, last); } cnt_trie(0, subtrie); int res = trie[leader].cnt + count(0, subtrie, leader, trie); ans[height[v]] += sz[v] - res; for (auto& e : graph[v]) dfs(e.first); } int main() { int n; cin >> n; sz.resize(n); graph.resize(n); ans.resize(n); height.resize(n); trie.resize(n); for (int i = 0; i < n - 1; ++i) { int u, v; char c; cin >> u >> v >> c; --u, --v; graph[u].push_back(make_pair(v, c)); trie[u].next[c - 'a'] = v; } init_dfs(0); cnt_trie(0, trie); dfs(0); int delta = -1; int ans_p = -1; for (int p = 0; p < n; ++p) if (delta < ans[p]) { delta = ans[p]; ans_p = p; } cout << n - delta << endl << ans_p + 1; return 0; }
6
#include <bits/stdc++.h> using namespace std; string F[100010]; char str[1000]; int T[100010], e; set<string> A, B, AB, BA, FA, FB; vector<pair<string, string> > ans; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s%d", str, T + i); F[i] = string(str); if (T[i] == 1) e++; } for (int i = 1; i <= n; i++) { if (i <= e) A.insert(to_string(i)); else B.insert(to_string(i)); } for (int i = 1; i <= n; i++) { if (A.count(F[i])) { A.erase(F[i]); if (T[i] == 0) AB.insert(F[i]); } else if (B.count(F[i])) { B.erase(F[i]); if (T[i] == 1) BA.insert(F[i]); } else { if (T[i] == 1) FA.insert(F[i]); else FB.insert(F[i]); } } while (A.size() || B.size() || AB.size() || BA.size()) { if (A.size() && BA.size()) { ans.push_back({*BA.begin(), *A.begin()}); A.erase(A.begin()); B.insert(*BA.begin()); BA.erase(BA.begin()); continue; } if (B.size() && AB.size()) { ans.push_back({*AB.begin(), *B.begin()}); B.erase(B.begin()); A.insert(*AB.begin()); AB.erase(AB.begin()); continue; } if (A.size() && FA.size()) { ans.push_back({*FA.begin(), *A.begin()}); A.erase(A.begin()); FA.erase(FA.begin()); continue; } if (B.size() && FB.size()) { ans.push_back({*FB.begin(), *B.begin()}); B.erase(B.begin()); FB.erase(FB.begin()); continue; } ans.push_back({*AB.begin(), "tmp"}); A.insert(*AB.begin()); AB.erase(AB.begin()); FB.insert("tmp"); } printf("%d\n", (int)ans.size()); for (pair<string, string>& x : ans) printf("move %s %s\n", x.first.c_str(), x.second.c_str()); return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> day_seq; vector<int> main_seq; int sec, mn, hr, dw, dm, month; int mlen[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { cin >> sec >> mn >> hr >> dw >> dm >> month; if (dw > 0) --dw; if (dm > 0) --dm; if (month > 0) --month; int ctr0, ctr1; ctr0 = 0; for (int h = 0; h < 24; ++h) { for (int m = 0; m < 60; ++m) { for (int s = 0; s < 60; ++s) { if (h == hr || hr == -1) { if (m == mn || mn == -1) { if (s == sec || sec == -1) { day_seq.push_back(ctr0); } } } ++ctr0; } } } int st = 3; ctr1 = 0; for (int _y = 0; _y < 400; ++_y) { int is_leap = 0; int second = _y + 1970; if (second % 4 == 0 && (second % 400 == 0 || second % 100 > 0)) is_leap = 1; if (is_leap) mlen[1]++; for (int m = 0; m < 12; ++m) { for (int d = 0; d < mlen[m]; ++d) { if (m == month || month == -1) { if (dm != -1 && dw != -1) { if (d == dm || st == dw) { main_seq.push_back(ctr1); } } if (dm == -1 && dw == -1) { main_seq.push_back(ctr1); } if (dm == -1 && st == dw) { main_seq.push_back(ctr1); } if (dw == -1 && d == dm) { main_seq.push_back(ctr1); } } ++st; if (st == 7) st = 0; ++ctr1; } } if (is_leap) mlen[1]--; } int ctr; cin >> ctr; while (ctr--) { long long now; cin >> now; long long main_div = now / (1LL * ctr0 * ctr1); long long main_mod = now % (1LL * ctr0 * ctr1); long long day_div = main_mod / ctr0; long long day_mod = main_mod % ctr0; if (day_div > main_seq.back() || (day_div == main_seq.back() && day_mod >= day_seq.back())) { day_div = main_seq[0]; day_mod = day_seq[0]; ++main_div; } else if (day_mod >= day_seq.back() || (*lower_bound((main_seq).begin(), (main_seq).end(), day_div)) != day_div) { day_div = (*upper_bound((main_seq).begin(), (main_seq).end(), day_div)); day_mod = day_seq[0]; } else { day_mod = (*upper_bound((day_seq).begin(), (day_seq).end(), day_mod)); } cout << main_div * ctr0 * ctr1 + day_div * ctr0 + day_mod << endl; } }
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); queue<int> current; int res = 0; for (int i = 0; i < n; ++i) { if (current.size() != 0 and current.front() < a[i]) { ++res; current.pop(); } current.push(a[i]); } cout << res << endl; }
3
#include <bits/stdc++.h> using namespace std; const long double pi = acos(-1.0); const long double eps = 1e-12; int cmp(long double a, long double b) { if (fabs(a - b) <= eps) return 0; return (a > b) ? 1 : -1; } struct P { long double x, y; P() {} P(long double X, long double Y) { x = X, y = Y; } P operator+(P a) { return P(x + a.x, y + a.y); } P operator-(P a) { return P(x - a.x, y - a.y); } long double operator*(P a) { return x * a.x + y * a.y; } long double operator%(P a) { return x * a.y - y * a.x; } P rot(long double ang) { long double c = cos(ang), s = sin(ang); return P(x * c - y * s, x * s + y * c); } bool operator==(P a) { return cmp(x, a.x) == 0 && cmp(y, a.y) == 0; } } p[1010]; int ig[1010]; int pols[110][5]; int n; int main() { scanf("%d", &n); P v = P(10, 0); P pt = P(0, 0); long double ang = pi / 2.5; for (int i = 0; i < 5; ++i) { p[i] = pt; pt = pt + v.rot(i * ang); } long double dist = sqrt((p[2] - p[4]) * (p[2] - p[4])); int pnt = 5; for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < 4; ++j) { p[pnt++] = P(p[j].x + (i + 1) * dist, p[j].y); } } printf("%d\n", pnt); for (int i = 0; i < pnt; ++i) { cout << setprecision(12) << fixed << p[i].x << " " << p[i].y << endl; } for (int i = 0; i < 5; ++i) { if (i) printf(" "); printf("%d", i + 1); pols[0][i] = i; } printf("\n"); for (int i = 1; i < n; ++i) { int in = 5 + (i - 1) * 4; for (int j = 0; j < 4; ++j) { if (j) printf(" "); printf("%d", 1 + j + in); pols[i][j] = j + in; } pols[i][4] = pols[i - 1][2]; printf(" %d\n", 1 + pols[i][4]); } printf("%d %d %d %d", pols[0][0] + 1, pols[0][3] + 1, pols[0][1] + 1, pols[0][4] + 1); for (int i = 0; i < n; ++i) { printf(" %d", pols[i][2] + 1); } for (int i = n - 1; i >= 1; --i) { printf(" %d %d %d %d", pols[i][0] + 1, pols[i][3] + 1, pols[i][1] + 1, pols[i][4] + 1); } printf(" %d\n", pols[0][0] + 1); return 0; }
4
#include<iostream> using namespace std; int main(){ int n; int cnt; while(1){ cin>>n; if(n==0)break; cnt=0; while(1){ if(n==1)break; if(n%2==0){ n=n/2; cnt++; } if(n%2!=0&&n!=1){ n=n*3+1; cnt++; } } cout<<cnt<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i)) #define REP(i, n) FOR(i, n, 0) #define OF64 std::setprecision(10) const ll MOD = 1000000007; const ll INF = (ll)1e15; class LazySegmentTree { public: LazySegmentTree(ll n) { ll c = 1; while (c < n) c <<= 1; REP(i, 2 * c - 1) { node.push_back(0); lazy.push_back(0); } mN = c - 1; } //! 0-index 区間加算 void add(ll value, int left, int right, int k = 0, int l = 0, int r = -1) { if (r < 0) r = mN; LazyProgation(k); if (r < left || right < l) return; if (left <= l && r <= right) { //! 区間を完全被覆しているのでlazyに区間和を入れる lazy[k] += value; LazyProgation(k); } else { add(value, left, right, 2 * k + 1, l, (l + r) / 2); add(value, left, right, 2 * k + 2, (l + r) / 2 + 1, r); node[k] = std::min(node[2 * k + 1], node[2 * k + 2]); } } //! 0-index 最小値 ll min(ll left, ll right, int k = 0, int l = 0, int r = -1) { if (r < 0) r = mN; if (r < left || right < l) return INF; LazyProgation(k); if (left <= l && r <= right) return node[k]; return std::min(min(left, right, 2 * k + 1, l, (l + r) / 2), min(left, right, 2 * k + 2, (l + r) / 2 + 1, r)); } void dev() { FOR(i, node.size(), 0) { cout << node[i] << ":" << lazy[i] << " "; } cout << endl; } private: //! index番目のnodeの遅延評価 void LazyProgation(int index) { if (lazy[index] == 0) return; node[index] += lazy[index]; if (2 * index + 1 < node.size()) { lazy[2 * index + 1] += lazy[index]; lazy[2 * index + 2] += lazy[index]; } lazy[index] = 0; } ll mN; vector<ll> node; //! 遅延評価配列 vector<ll> lazy; }; int main() { ll N, Q; cin >> N >> Q; LazySegmentTree seg(N); REP(i, Q) { ll c, s, t; cin >> c >> s >> t; if (c == 0) { ll x; cin >> x; seg.add(x, s, t); } else if (c == 1) { cout << seg.min(s, t) << endl; } } return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll MAX = 1e17 + 7; const ll N = 1e5+7; struct aa{ ll u,d; bool operator < (const aa & a) const{ return d>a.d; } }; vector<aa>arr[N]; bool vis[N]; ll n,m; ll d[N]; void Dij(){ fill(d+1,d+n+1,MAX); d[1]=0; priority_queue<aa>Q; Q.push({1,0}); while(!Q.empty()){ aa x=Q.top(); Q.pop(); ll u=x.u; if(vis[u])continue; vis[u]=1; for(ll i=0;i<arr[u].size();i++){ aa e=arr[u][i]; if(d[e.u]>d[u]+e.d){ d[e.u]=d[u]+e.d; Q.push({e.u,d[e.u]}); } } } } int main(){ ll a,b,w; scanf("%lld%lld",&n,&m); for(ll i=2;i<=n;i++){ arr[i].push_back({i-1,0}); } for(ll i=0;i<m;i++){ scanf("%lld%lld%lld",&a,&b,&w); arr[b].push_back({a,w}); arr[a].push_back({b,w}); } Dij(); if(d[n]==MAX) d[n]=-1; cout<<d[n]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a[5][5], i, j; for (i = 0; i < 5; i++) for (j = 0; j < 5; j++) { cin >> a[i][j]; if (a[i][j] == 1) cout << abs(2 - i) + abs(2 - j); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long double PI = acosl(-1); bool compare_int(int a, int b) { return (a > b); } bool compare_string(string a, string b) { return a.size() < b.size(); } bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { if (a.second == b.second) return a.first > b.first; else return (a.second > b.second); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, n, x, y; cin >> t; while (t--) { vector<int> xx, yy; cin >> n; int kace = INT_MAX; for (int i = 0; i < n; i++) { cin >> x >> y; xx.push_back(x); yy.push_back(y); } sort(xx.begin(), xx.end()); sort(yy.begin(), yy.end()); cout << max(*max_element(xx.begin(), xx.end()) - *min_element(yy.begin(), yy.end()), 0) << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long N = 8e4 + 5; long long n, res, countTwice; long long sz[N]; bool vis[N]; vector<vector<long long> > v(N); inline long long F(long long x) { return (x * (x - 1)) >> 1; } void dfs(long long node) { vis[node] = true; sz[node] = 1; long long pathsHere = 0; long long aux = 0, twiceHere = 0; for (long long to : v[node]) { if (vis[to]) continue; dfs(to); pathsHere += sz[to] * sz[node]; twiceHere += F(sz[to]) * aux; aux += F(sz[to]); sz[node] += sz[to]; } countTwice += twiceHere; res += pathsHere * F(n - sz[node]); } signed main() { scanf("%lld", &n); for (long long i = 1; i <= n - 1; i++) { long long x, y; scanf("%lld %lld", &x, &y); v[x].emplace_back(y); v[y].emplace_back(x); } dfs(1); printf("%lld\n", (res << 1) - (countTwice << 1)); }
4
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; int x = 0; string op; char arr[1000][1000]; bool put = true; int counter = 0; vector<string> vs; if ((a >= 1) && (a <= 1000)) { int i, j; for (int i = 0; i < a; i++) { string s; for (int j = 0; j < a; j++) { if (put) { counter++; s.append("C"); } else { s.append("."); } put = !put; } if (a % 2 == 0) { put = !put; } vs.push_back(s); } cout << counter << endl; for (int i = 0; i < a; i++) { cout << vs.at(i) << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; ; int ans = 0; int x[1100], y[1100]; bool right, left, up, down; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (x[j] > x[i] && y[j] == y[i]) right = true; if (x[j] < x[i] && y[j] == y[i]) left = true; if (x[j] == x[i] && y[j] > y[i]) down = true; if (x[j] == x[i] && y[j] < y[i]) up = true; } if (up && down && left && right) ans++; up = false; down = false; left = false; right = false; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f, N = 1e3 + 10, P = 1e9 + 7; template <class T> inline bool chkmin(T &A, T B) { return B < A ? A = B, 1 : 0; } template <class T> inline bool chkmax(T &A, T B) { return A < B ? A = B, 1 : 0; } template <class T, class _> inline void Add(T &first, _ second) { first += second; if (first >= P) first -= P; if (first < 0) first += P; } int n, m, K; long long Fac[N], Inv[N]; long long C(int n, int m) { if (m > n) return 0; return Fac[n] * Inv[m] % P * Inv[n - m] % P; } int main() { Fac[0] = 1; for (int i = (1), i_end = (N - 10); i <= i_end; ++i) Fac[i] = Fac[i - 1] * i % P; Inv[1] = 1; for (int i = (2), i_end = (N - 10); i <= i_end; ++i) Inv[i] = (P - P / i) * Inv[P % i] % P; Inv[0] = 1; for (int i = (1), i_end = (N - 10); i <= i_end; ++i) Inv[i] = Inv[i - 1] * Inv[i] % P; scanf("%d%d%d", &n, &m, &K); printf("%lld\n", C(n - 1, 2 * K) * C(m - 1, 2 * K) % P); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 10; vector<int> v; int father[maxn][2]; int rk[maxn][2]; int sz[maxn][2]; int Find(int v, int t) { return father[v][t] = father[v][t] == v ? v : Find(father[v][t], t); } void merge(int x, int y, int t) { int a = Find(x, t), b = Find(y, t); if (sz[a][t] == 0) sz[a][t]++; if (sz[b][t] == 0) sz[b][t]++; if (rk[a][t] < rk[b][t]) { father[a][t] = b; sz[b][t] += sz[a][t]; } else { father[b][t] = a; sz[a][t] += sz[b][t]; if (rk[b][t] == rk[a][t]) { ++rk[a][t]; } } } int main() { int n; int u, v, t; while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) { father[i][0] = father[i][1] = i; } memset(rk, 0, sizeof(rk)); memset(sz, 0, sizeof(sz)); for (int i = 1; i < n; i++) { scanf("%d%d%d", &u, &v, &t); merge(u, v, t); } long long ans = 0; for (int i = 1; i <= n; i++) { int a = Find(i, 0), b = Find(i, 1); if (sz[a][0] != 0 && sz[b][1] != 0) { ans += (long long)(sz[a][0] - 1) * (sz[b][1] - 1); } if (sz[a][0] != 0) { ans += sz[a][0] - 1; } if (sz[b][1] != 0) { ans += sz[b][1] - 1; } } printf("%I64d\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; string a, b; int cnt1 = 0, cnt2 = 0; void ReadInput() { cin >> a >> b; } void Solve() { for (int i = 0; i < a.size(); i += 2) { if (a[i] == '[' && a[i + 1] == ']') { if (b[i] == '(' && b[i + 1] == ')') cnt1++; if (b[i] == '8' && b[i + 1] == '<') cnt2++; } if (a[i] == '(' && a[i + 1] == ')') { if (b[i] == '[' && b[i + 1] == ']') cnt2++; if (b[i] == '8' && b[i + 1] == '<') cnt1++; } if (a[i] == '8' && a[i + 1] == '<') { if (b[i] == '(' && b[i + 1] == ')') cnt2++; if (b[i] == '[' && b[i + 1] == ']') cnt1++; } } if (cnt1 == cnt2) cout << "TIE"; else if (cnt1 > cnt2) cout << "TEAM 1 WINS"; else cout << "TEAM 2 WINS"; } int main() { ReadInput(); Solve(); }
1
#include <bits/stdc++.h> using namespace std; typedef struct man { int num; int sum; } man; int n, t1, t2, sale; int s[1000][2]; man m[1000]; int cmp(man a, man b) { if (a.sum == b.sum) { return a.num < b.num; } return a.sum > b.sum; } int main() { int i, j; cin >> n >> t1 >> t2 >> sale; sale = 100 - sale; for (i = 0; i < n; i++) { cin >> s[i][0] >> s[i][1]; s[i][0] *= 100; s[i][1] *= 100; } for (i = 0; i < n; i++) { m[i].num = i + 1; m[i].sum = max(int(s[i][0] * t1 * float(sale) / 100) + s[i][1] * t2, int(s[i][1] * t1 * float(sale) / 100) + s[i][0] * t2); } sort(m, m + n, cmp); for (i = 0; i < n; i++) { cout << m[i].num << " "; printf("%.2f\n", float(m[i].sum) / 100.0); } return 0; }
2