solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int a[6]; int main() { int m, x, y, i; scanf("%d", &m); for (i = 0; i < m; ++i) { scanf("%d%d", &x, &y); ++a[x], ++a[y]; } if (m != 5) printf("WIN"); else { for (i = 1; i <= 5; ++i) { if (a[i] != 2) { printf("WIN"); return 0; } } printf("FAIL"); } return 0; }
2
#include<bits/stdc++.h> using namespace std; int main() { int n,k,s; cin>>n>>k>>s; for(int i=1;i<=k;i++) printf("%d ",s); if(s==1) s++; else s--; for(int i=k+1;i<=n;i++) printf("%d ",s); }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long long INF = 1e18; const int MAXN = 1e5 + 5; const int T = 1e3 + 10; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; long long binpow(long long val, long long deg) { if (!deg) return 1; if (deg & 1) return binpow(val, deg - 1) * val % MOD; long long res = binpow(val, deg / 2); return (res * res) % MOD; } long long inv(long long a) { return binpow(a, MOD - 2); } pair<int, int> operator+(const pair<int, int>& a, const pair<int, int>& b) { return make_pair(a.first + b.first, a.second + b.second); } pair<int, int> operator-(const pair<int, int>& a, const pair<int, int>& b) { return make_pair(a.first - b.first, a.second - b.second); } void rotate90(int& first, int& second, int x0, int y0) { first = first - x0; second = second - y0; swap(first, second); first = -first; first += x0; second += y0; } long long i, j; long long v[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed; cout.precision(10); long long n, first; cin >> n >> first; long long s = 0; for (i = 1; i <= n; i++) cin >> v[i], s += v[i]; long long minv = 1e15; map<long long, long long> cnt; for (i = 1; i <= n; i++) { cnt[s - v[i]]++; minv = min(minv, s - v[i]); } while (cnt[minv] % first == 0) { cnt[minv + 1] += cnt[minv] / first; minv++; } cout << binpow(first, min(minv, s)); return 0; }
3
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; vector<int> sz; UnionFind(int n = 0) { if (n > 0) initialize(n); } void initialize(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[y] = x; sz[x] += sz[y]; } bool same(int x, int y) { return find(x) == find(y); } }; long long N, K, M; const int MAX = 500000; vector<int> edges[MAX]; int depth[MAX], parent[MAX]; void dfs(int i, int prev) { for (auto j : edges[i]) { if (j == prev) continue; parent[j] = i; depth[j] = depth[i] + 1; dfs(j, i); } } int main() { long long i, j, k; cin >> N >> K >> M; UnionFind uf(N); set<pair<int, int>> myedges; for (i = 0; i < K; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; if (a > b) swap(a, b); myedges.insert({a, b}); edges[a].push_back(b); edges[b].push_back(a); uf.unite(a, b); } vector<vector<int>> youredges; for (i = 0; i < M; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); youredges.push_back({c, a - 1, b - 1}); } sort(youredges.begin(), youredges.end()); for (auto& e : youredges) { int a = e[1], b = e[2]; if (!uf.same(a, b)) { uf.unite(a, b); edges[a].push_back(b); edges[b].push_back(a); } } dfs(0, -1); uf.initialize(N); long long ans = 0; for (auto& e : youredges) { int a = uf.find(e[1]), b = uf.find(e[2]); long long c = e[0], num = 0; vector<int> children = {}; while (a != b) { if (depth[a] < depth[b]) { children.push_back(b); int p = parent[b]; if (myedges.count({min(b, p), max(b, p)})) num++; b = uf.find(p); } else { children.push_back(a); int p = parent[a]; if (myedges.count({min(a, p), max(a, p)})) num++; a = uf.find(p); } } ans += c * num; for (auto x : children) uf.unite(a, x); } if (uf.sz[0] != N) ans = -1; cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; const int INF = (int)1E9; const long long INF64 = (long long)1E18; const long double EPS = 1E-8; const long double PI = 3.1415926535897932384626433832795; char buf[100]; long long phi(long long x) { sprintf(buf, "%I64d", x); for (int i = 0; i < (int)(strlen(buf)); i++) buf[i] = (9 - (buf[i] - '0')) + '0'; sscanf(buf, "%I64d", &x); return x; } long long f(long long x) { return x * phi(x); } long long solve(long long l, long long r) { while (r - l > 10) { long long mid1 = (2 * l + r) / 3; long long mid2 = (l + 2 * r) / 3; if (f(mid1) < f(mid2)) l = mid1; else r = mid2; } long long ans = 0; for (long long x = l; x <= r; x++) ans = max(ans, f(x)); return ans; } int main() { long long l, r; cin >> l >> r; long long pw = 1, ans = 0; for (int i = 0; i < (int)(11); i++) { ans = max(ans, solve(max(pw, l), min(r, pw * 10 - 1))); pw *= 10; } cout << ans << endl; return 0; }
1
#include<iostream> #include<queue> #include<string> #include<algorithm> #include<vector> #include<cstring> using namespace std; int main(){ int m, n, s, i; char no[100]; while(cin >> m >> n, m != 0 || n != 0) { queue<int> q; for(int j = 0; j < m; j++) q.push(j+1); i = 1; while(n--) { if(q.size() != 1) { cin >> no; int p = q.front(); q.pop(); if(i%3 == 0 && i% 5 == 0) { if(strcmp(no, "FizzBuzz") == 0) q.push(p); } else if(i % 3 == 0 && strcmp(no,"Fizz") == 0) q.push(p); else if(i % 5 == 0 && strcmp(no ,"Buzz") == 0) q.push(p); else if(i % 3 != 0 && i % 5 != 0 && i == atoi(no)) q.push(p); i++; } else cin >> no; } vector<int> v(1050); while(q.size()) { v.push_back(q.front()); q.pop(); } sort(v.begin(), v.end()); while(v.size()) { if(v[0]) { cout << v[0]; if(v.size() - 1) cout << ' '; } v.erase(v.begin()); } cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m, k; set<int> edge[3001]; map<pair<int, int>, set<int>> forb; int d[3001][2], pre[3001][2], cnt[3001]; void bfs() { queue<pair<int, int>> q; q.push({1, 0}); while (q.size()) { int cur = q.front().first, from = q.front().second; q.pop(); set<int> leftover; for (int i : edge[cur]) { if (i == 1) continue; int nodefrom = pre[cur][from]; if (edge[i].size() && cnt[i] < 2 && cnt[0] != cur) { if (nodefrom > 0) { set<int> ss = forb[{nodefrom, cur}]; if (ss.find(i) != ss.end()) { leftover.insert(i); continue; } } pre[i][cnt[i]] = cur; d[i][cnt[i]] = d[cur][from] + 1; q.push({i, cnt[i]}); cnt[i]++; } } edge[cur] = leftover; } if (d[n][0]) cout << d[n][0] << endl; else { printf("-1"); return; } int dist = d[n][0]; vector<int> sol; int v = n; while (v != 1) { sol.push_back(v); if (d[v][0] == dist) { v = pre[v][0]; } else v = pre[v][1]; dist--; } sol.push_back(v); for (int i = sol.size() - 1; i >= 0; i--) printf("%d ", sol[i]); } int main() { cin >> n >> m >> k; int x, y, z; for (int i = 0; i < m; i++) { cin >> x >> y; edge[x].insert(y); edge[y].insert(x); } for (int i = 0; i < k; i++) { cin >> x >> y >> z; forb[{x, y}].insert(z); } bfs(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long n, k; int m; long long a[400005], b[400005]; long long L0, R0; void init() { L0 = R0 = -1; long long l = 1, r = n; while (l <= r) { long long mid = l + r >> 1, s = 0; for (int i = 0; i <= m; ++i) s += (((b[i]) + (mid)-1) / (mid)) - 1; if (s <= k) r = (R0 = mid) - 1; else l = mid + 1; } l = 1, r = n; while (l <= r) { long long mid = l + r >> 1, s = 0; bool bz = 1; for (int i = 0; i <= m && bz; ++i) { if (b[i] / mid == 0) bz = 0; s += b[i] / mid - 1; } if (bz && s >= k) l = (L0 = mid) + 1; else r = mid - 1; } } pair<long long, long long> p[400005]; multiset<long long> s; int main() { int T; scanf("%d", &T); while (T--) { scanf("%lld%d%lld", &n, &m, &k); for (int i = 1; i <= m; ++i) scanf("%lld", &a[i]); a[m + 1] = n; for (int i = 0; i <= m; ++i) b[i] = a[i + 1] - a[i]; init(); long long ans; assert(L0 <= R0); bool bz = 1; for (int i = 0; i <= m; ++i) bz &= ((b[i] + R0 - 1) / R0 <= b[i] / L0); if (bz) ans = R0 - L0; else { s.clear(); s.insert(L0), s.insert(R0); int k = 0; for (int i = 0; i <= m; ++i) if ((((b[i]) + (R0)-1) / (R0)) > b[i] / L0) { p[++k] = {b[i] / (((b[i]) + (R0)-1) / (R0)), (((b[i]) + (b[i] / L0) - 1) / (b[i] / L0))}; s.insert(p[k].first); } sort(p + 1, p + k + 1); ans = *s.rbegin() - *s.begin(); for (int i = 1; i <= k; ++i) { s.erase(s.find(p[i].first)); s.insert(p[i].second); ans = min(ans, *s.rbegin() - *s.begin()); } } printf("%lld\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> G[1000 * 100 + 10]; int dis[1000 * 100 + 10][200], mark[1000 * 100 + 100], col[1000 * 100 + 100], n, m, k, s; void BFS(int c) { vector<int> Q; for (int i = 0; i < n; i++) { if (col[i] == c) { Q.push_back(i); mark[i] = 1; } else mark[i] = 0; } int id = 0; while (id < Q.size()) { int u = Q[id]; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (!mark[v]) { dis[v][c] = dis[u][c] + 1; mark[v] = 1; Q.push_back(v); } dis[v][c] = min(dis[v][c], dis[u][c] + 1); } id++; } } int main() { cin.tie(0); cout.tie(0); cin >> n >> m >> k >> s; for (int i = 0; i < n; i++) cin >> col[i]; while (m--) { int u, v; cin >> u >> v; u--, v--; G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= k; i++) BFS(i); for (int i = 0; i < n; i++) { sort(dis[i], dis[i] + k + 1); int ans = 0; for (int j = 1; j < s + 1; j++) ans += dis[i][j]; cout << ans << ' '; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, q; cin >> n >> q; vector<pair<long long, long long>> v; for (long long i = 0; i < n; i++) { v.push_back({i + 1, 0}); } while (q--) { long long t, k, d; cin >> t >> k >> d; long long av = 0; for (long long i = 0; i < n; i++) { if (v[i].second < t) { av++; } } if (av < k) { cout << -1; } else { long long sum = 0; for (long long i = 0; i < n; i++) { if (v[i].second < t) { v[i].second = t + d - 1; sum += i + 1; k--; } if (k == 0) { cout << sum; break; } } } cout << endl; } }
3
#include <bits/stdc++.h> using namespace std; int main(void){ int n, m; cin >> n >> m; if(n % 2 == 1){ for(int i = 0; i < n/2 + 1; i++){ if(i == 0) cout << 0; else cout << " " << 0; } for(int i = 0; i < n/2; i++){ cout << " " << m; } cout << endl; }else{ for(int i = 0; i < n/2 + 1; i++){ if(i == 0) cout << 0; else cout << " " << 0; } for(int i = 0; i < n/2 - 1; i++){ cout << " " << m; } cout << endl; } return 0; }
0
#include <bits/stdc++.h> #define rep(i,l,n) for(int i=l;i<n;i++) #define all(a) a.begin(),a.end() #define o(a) cout<<a<<endl using namespace std; int main(){ int n; while(1){ cin>>n; if(n==0) break; vector<int> a(n); rep(i,0,n) cin>>a[i]; int MAX=a[0]; rep(i,0,n){ int sum=0; rep(j,i,n){ sum+=a[j]; MAX=max(MAX,sum); } } o(MAX); } }
0
#include <bits/stdc++.h> const int N = 505; const int mod = 1e9 + 7; using namespace std; int n; int k; long long d[N][N]; int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i < k; i++) { d[0][i] = -1e18; } for (int i = 1; i <= n; i++) { long long a, b; cin >> a >> b; for (int j = 0; j < k; j++) { d[i][j] = d[i - 1][j]; } for (int x = 0; x < k; x++) { long long y = (a + b - x) % k; if (x > a || y > b) { continue; } long long cost, shit; shit = a + b - x; for (int h = 0, nh; h < k; h++) { nh = h + x; cost = shit; if (nh >= k) { nh -= k; cost += k; } d[i][nh] = max(d[i][nh], d[i - 1][h] + cost); } } } long long res = 0; for (int i = 0; i < k; i++) { res = max(res, d[n][i] / k); } cout << res << "\n"; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, x, y; cin >> n >> m; for (i = 1; i <= m; i++) cin >> x >> y; for (i = 1; i <= n; i++) cout << i % 2; return 0; }
2
#include <bits/stdc++.h> using namespace std; int N = 1, n, m, x, y, k, t, a[400005], b[400005], cay_vai_lon[400005], sieu_cay_khong_lo[800005]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; while (N <= n) N <<= 1; for (int i = 1; i <= m; i++) { cin >> t; if (!(t - 1)) { cin >> x >> y >> k; cay_vai_lon[i] = x - y; for (int l = N + y, r = N + y + k - 1; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1) sieu_cay_khong_lo[l] = sieu_cay_khong_lo[r] = i; } else { cin >> x; int t(0); for (int j = N + x; j; j >>= 1) if (t < sieu_cay_khong_lo[j]) t = sieu_cay_khong_lo[j]; if (!t) cout << b[x] << endl; else cout << a[x + cay_vai_lon[t]] << endl; } } }
5
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; clock_t clk = clock(); long long int i, j; void solve(void); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int t = 1; while (t--) solve(); return 0; } void solve() { string s; int k; cin >> s >> k; set<pair<string, int> > ans; for (i = 0; i <= (int)s.size() - 1; ++i) { pair<string, int> p; p.first = s[i]; p.second = i; ans.insert(p); } long long int n = (int)s.size(); n *= (n + 1); n /= 2; if (n < k) { cout << "No such line.\n"; return; } int cnt = 0; while (true) { ++cnt; auto it = ans.begin(); int idx = (*it).second; string t = (*it).first; ans.erase(it); if (cnt == k) { cout << t << '\n'; return; } if (idx < (int)s.size() - 1) { ++idx; t += s[idx]; ans.insert({t, idx}); } } }
2
#include <bits/stdc++.h> using namespace std; set<pair<int, int> > s; set<pair<int, int> >::iterator st, it, pre, suc; int h[120000], l[120000], r[120000], i, j, k, o, n, p, q, P[120000], f[120000]; bool cmp(const int& a, const int& b) { return h[a] > h[b]; } int main() { scanf("%d%*d", &n); for (i = 1; i <= n; ++i) scanf("%d%d%d", &h[i], &l[i], &r[i]), P[i] = i; h[0] = 1e9; l[0] = -2e9; r[0] = 2e9; f[0] = 2e9; h[n + 1] = -1; l[n + 1] = -1e9; r[n + 1] = 1e9; P[n + 1] = n + 1; sort(P + 1, P + n + 2, cmp); s.insert(make_pair(-1e9, 0)); s.insert(make_pair(1e9, 0)); for (o = 1; o <= n + 1; ++o) { i = P[o]; st = s.lower_bound(make_pair(l[i], i)); for (it = st, it--; it->first < r[i]; ++it) { pre = it; if (pre != s.begin()) pre--; suc = it; suc++; j = it->second; p = pre->second; q = suc->second; if ((h[j] <= h[p] || l[j] >= r[p] || r[p] <= l[i]) && (h[j] <= h[q] || r[j] <= l[q] || l[q] >= r[i])) f[i] = max(f[i], min(f[j], min(r[i], r[j]) - max(l[i], l[j]))); } it--; j = it->second; s.erase(st, ++it); if (r[i] != it->first) s.insert(make_pair(r[i], j)); s.insert(make_pair(l[i], i)); } printf("%d\n", f[n + 1]); }
4
#include <bits/stdc++.h> using namespace std; const int N = 500500; int n, k; long long dp[N][2]; vector<pair<int, int> > adj[N]; long long get(int v, int p, int par) { long long& ans = dp[v][p]; if (ans != -1) { return ans; } ans = 0; long long sum = 0; vector<long long> ot; for (pair<int, int> e : adj[v]) { int u = e.second; int w = e.first; if (u == par) { continue; } sum += get(u, 0, v); long long peg = get(u, 1, v) + w - get(u, 0, v); if (peg > 0) { ot.push_back(peg); } } sort(ot.begin(), ot.end()); reverse(ot.begin(), ot.end()); ans = sum; for (int i = 0; i < min(k - p, (int)ot.size()); i++) { ans += ot[i]; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { cin >> n >> k; for (int i = 1; i <= n; i++) { dp[i][0] = dp[i][1] = -1; adj[i].clear(); } for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; adj[v].push_back(pair<int, int>(w, u)); adj[u].push_back(pair<int, int>(w, v)); } cout << get(1, 0, -1) << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 100005, M = 6; int a[N][M]; struct RMQ { int n, lg; int d[M][N][20]; void init(int N, int M) { n = N; for (lg = 0; (1 << lg) <= n; lg++) ; lg--; for (int j = 1; j <= M; j++) { for (int i = 1; i <= n; i++) d[j][i][0] = i; for (int L = 1; L <= lg; L++) for (int i = 1; i + (1 << L) - 1 <= n; i++) { int x = d[j][i][L - 1], y = d[j][i + (1 << (L - 1))][L - 1]; d[j][i][L] = (a[x][j] > a[y][j] ? x : y); } } } int max(int j, int L, int R) { if (L > R) return 0; if (L == R) return d[j][L][0]; for (lg = 0; L + (1 << lg) - 1 < R; lg++) ; lg--; int x = d[j][L][lg], y = d[j][R - (1 << lg) + 1][lg]; return (a[x][j] > a[y][j] ? x : y); } } rmq; int main() { int n, m, k; while (cin >> n >> m >> k) { for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; rmq.init(n, m); int ans[M] = {0}; int _max = 0, pre = 1; for (int i = 1; i <= n; i++) { int sum = 0, ind; for (int j = 1; j <= m; j++) { ind = rmq.max(j, pre, i); sum += a[ind][j]; } while (sum > k) { ind = i + 1; for (int j = 1; j <= m; j++) ind = min(ind, rmq.max(j, pre, i)); pre = ind + 1; sum = 0; for (int j = 1; j <= m; j++) sum += a[rmq.max(j, pre, i)][j]; } if (i - pre + 1 > _max) { _max = i - pre + 1; for (int j = 1; j <= m; j++) ans[j] = a[rmq.max(j, pre, i)][j]; } } for (int j = 1; j <= m; j++) cout << ans[j] << " "; cout << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; #define MAX 100000 int main() { vector<int> p; bool prime[MAX]; fill(prime, prime + MAX, 1); prime[0] = prime[1] = 0; for (int i = 2; i < MAX; i++) { if (prime[i]) { p.push_back(i); for (int j = 2*i; j < MAX; j += i) { prime[j] = 0; } } } int N; while (cin >> N) { cout << *(lower_bound(p.begin(), p.end(), N) - 1) << " " << *upper_bound(p.begin(), p.end(), N) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1500; const int N = 1e3 + 7, M = N * 2; const int inf = 0x3f3f3f3f; const long long INF = 0xFFFFFFFFFF; const long long mod = 1e9 + 7; inline long long read(); int p[N]; int b[N]; int find1(int x) { if (p[x] != x) p[x] = find1(p[x]); return p[x]; } int find2(int x) { if (b[x] != x) b[x] = find2(b[x]); return b[x]; } void solve() { int n, m1, m2; cin >> n >> m1 >> m2; vector<pair<int, int> > ans; for (int i = 1; i <= n; i++) { p[i] = b[i] = i; } for (int i = 1; i <= m1; i++) { int x, y; cin >> x >> y; p[find1(x)] = find1(y); } for (int i = 1; i <= m2; i++) { int x, y; cin >> x >> y; b[find2(x)] = find2(y); } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { int x, y, c, d; x = find1(i), y = find1(j); c = find2(i), d = find2(j); if (x != y && c != d) { p[x] = y; b[c] = d; ans.push_back({i, j}); } } } cout << ans.size() << '\n'; for (auto x : ans) { cout << x.first << ' ' << x.second << '\n'; } } int main() { ios::sync_with_stdio(false); int T = 1; for (int cas = 1; cas <= T; cas++) { solve(); } return 0; } inline long long read() { char ch = getchar(); long long p = 1, data = 0; while (ch < '0' || ch > '9') { if (ch == '-') p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { data = data * 10 + (ch ^ 48); ch = getchar(); } return p * data; }
4
#include <bits/stdc++.h> #define int long long #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define all(x) (x).begin(),(x).end() #define pb push_back #define F first #define S second using namespace std; typedef pair<int,int> P; const int MOD=1000000007; int INF=100100100100100; int a[2010]; signed main(){ int n,k,q;cin>>n>>k>>q; int ans=INF; rep(i,n)cin>>a[i]; rep(ima,n){ vector<int> res; vector<int> v; rep(i,n){ if(a[i]>=a[ima])v.pb(a[i]); if(a[i]<a[ima] || i==n-1){ if(v.size()>=k){ int m=v.size(); sort(all(v)); rep(po,m-k+1){ res.pb(v[po]); } } v.clear(); } } if(res.size()>=q){ sort(all(res)); ans=min(ans,res[q-1]-res[0]); } } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, a, b; int main() { scanf("%d%d%d", &n, &a, &b); a = (a + b) % n; printf("%d\n", (a == 0 ? n : (n + a) % n)); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const double PI = 3.141592653589793; int licz, maxx, i, j, akt, n, m, L[5005], R[5005], U[5005], D[5005], sum, w; char dir[5005]; char t[5005]; vector<int> wek; void hide(int a) { L[R[a]] = L[a]; R[L[a]] = R[a]; U[D[a]] = U[a]; D[U[a]] = D[a]; } void show(int a) { L[R[a]] = a; R[L[a]] = a; U[D[a]] = a; D[U[a]] = a; } void wypisz() { for (int i = 1; i <= n * m; i++) printf("%d %d %d %d %d\n", i, L[i], U[i], R[i], D[i]); } int main() { scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { scanf("%s", &t); for (j = 0; j < m; j++) { w = i * m + j + 1; dir[w] = t[j]; if (j == 0) L[w] = 0; else L[w] = w - 1; if (j == m - 1) R[w] = 0; else R[w] = w + 1; if (i == 0) U[w] = 0; else U[w] = w - m; if (i == n - 1) D[w] = 0; else D[w] = w + m; } } for (i = 1; i <= m * n; i++) if (dir[i] == '.') hide(i); for (i = 1; i <= m * n; i++) if (dir[i] != '.') { akt = i; licz = 0; while (akt != 0) { licz++; wek.push_back(akt); hide(akt); if (dir[akt] == 'L') akt = L[akt]; else if (dir[akt] == 'R') akt = R[akt]; else if (dir[akt] == 'U') akt = U[akt]; else if (dir[akt] == 'D') akt = D[akt]; } while (wek.size() > 0) { show(wek.back()); wek.pop_back(); } if (licz == maxx) sum++; else if (licz > maxx) { sum = 1; maxx = licz; } } printf("%d %d\n", maxx, sum); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int x, y; cin >> x >> y; if (x < y) cout << x << " " << (y - x) / 2; else cout << y << " " << (x - y) / 2; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f; const int maxn = 2e5 + 5; int a[maxn]; bool ans[maxn]; priority_queue<int, vector<int>, greater<int> > dw; priority_queue<int> up; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; if (n == 1) { cout << "YES" << endl << 1 << endl; return 0; } up.push(-inf); dw.push(inf); int i = 1; if (a[i + 1] > a[i]) { up.push(a[i]); ans[i] = 0; } else if (a[i + 1] < a[i]) { dw.push(a[i]); ans[i] = 1; } else { up.push(a[i]); dw.push(a[++i]); ans[i - 1] = 0; ans[i] = 1; } i++; while (i <= n) { if (a[i] > up.top() && a[i] < dw.top()) { if (a[i + 1] > a[i]) { up.push(a[i]); ans[i] = 0; } else if (a[i + 1] < a[i]) { dw.push(a[i]); ans[i] = 1; } else { up.push(a[i]); dw.push(a[++i]); ans[i - 1] = 0; ans[i] = 1; } } else if (a[i] > up.top()) { up.push(a[i]); ans[i] = 0; } else if (a[i] < dw.top()) { dw.push(a[i]); ans[i] = 1; } else { cout << "NO" << endl; return 0; } i++; } cout << "YES" << endl; for (int j = 1; j <= n; j++) cout << ans[j] << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; int dp[204][104]; bool vis[204][104]; int n, k, l; vector<int> delta; vector<int> d; int dfs(int t, int pos) { int &ans = dp[t][pos]; vis[t][pos] = 1; if (pos == n + 1) return ans = 1; if (ans != -1) return ans; if (pos > 0) { if (d[pos - 1] + delta[t] > l) { return ans = 0; } } int further = 0; further = dfs((t + 1) % (2 * k), pos + 1); int stay = 0; if (!vis[(t + 1) % (2 * k)][pos]) stay = dfs((t + 1) % (2 * k), pos); ans = max(further, stay); return ans; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int t; cin >> t; while (t--) { cin >> n >> k >> l; for (int i = 0; i <= 2 * k; ++i) for (int j = 0; j <= n + 1; j++) dp[i][j] = -1, vis[i][j] = 0; delta.clear(); d.clear(); for (int i = 0; i <= k; i++) delta.push_back(i); for (int i = k - 1; i >= 1; i--) delta.push_back(i); for (int i = 0; i < n; i++) { int di; cin >> di; d.push_back(di); } cout << (dfs(0, 0) ? "yes" : "no") << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int vis[N + 100], cnt[N + 100]; int main() { int n, ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { int a; scanf("%d", &a); vis[a] = 1; } for (int i = N; i > 0; i--) { if (!vis[i]) { continue; } cnt[i] = 1; for (int j = 2 * i; j <= N; j += i) { cnt[i] = max(cnt[i], 1 + cnt[j]); } ans = max(ans, cnt[i]); } printf("%d\n", ans); }
6
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin>>n; vector<string> g(n); for(auto &s:g) cin>>s; int res=-1; for(int u=0;u<n;++u){ queue<int> q; q.push(u); vector<int> cost(n,-1); vector<int> clr(n,-1); clr[u]=0; cost[u]=0; while(!q.empty()){ int x=q.front(); q.pop(); for(int v=0;v<n;++v) if(g[x][v]=='1' && clr[v]==-1){ clr[v]=!clr[x]; q.push(v); cost[v]=cost[x]+1; } } int cur=*max_element(cost.begin(),cost.end()); if(u==0) for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(g[i][j]=='1' && clr[i]==clr[j]){ cout<<-1<<endl; return 0; } res=max(res,cur+1); } cout<<res<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int arr1[100010], arr2[100010], ans[200020]; int ansit; int main() { int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, cnt, st, ed, fl; char tmp; scanf("%d", &t); for (i = 0; i < t; i++) { scanf("%d", &n); scanf("%c", &tmp); for (j = 0; j < n; j++) { scanf("%c", &tmp); if (tmp == '0') arr1[j] = 0; else arr1[j] = 1; } scanf("%c", &tmp); for (j = 0; j < n; j++) { scanf("%c", &tmp); if (tmp == '0') arr2[j] = 0; else arr2[j] = 1; } ansit = 0; st = 0; ed = n - 1; fl = 0; for (j = n - 1; j >= 0; j--) { if ((arr1[ed] + fl) % 2 == arr2[j]) { if (st < ed) ed--; else ed++; } else { if ((arr1[st] + fl) % 2 != arr2[j]) { ans[ansit] = j + 1; ansit++; z = st; st = ed; ed = z; if (st < ed) ed--; else ed++; fl = 1 - fl; } else { ans[ansit] = 1; ansit++; ans[ansit] = j + 1; ansit++; z = st; st = ed; ed = z; if (st < ed) ed--; else ed++; fl = 1 - fl; } } } printf("%d ", ansit); for (j = 0; j < ansit; j++) printf("%d ", ans[j]); printf("\n"); } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k; string a; cin >> n >> a; k = n; for (int i = 0; i < n - 1; i++) { if (a[i] != a[i + 1]) { k--; i++; } } cout << k; }
1
#include <bits/stdc++.h> using namespace std; struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; template <class T> auto add = [](T a, T b) -> T { return a + b; }; template <class T> auto f_max = [](T a, T b) -> T { return max(a, b); }; template <class T> auto f_min = [](T a, T b) -> T { return min(a, b); }; template <class T> using V = vector<T>; using Vl = V<long long int>; using VVl = V<Vl>; template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; } template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (T& in : v) is >> in; return is; } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } long long int ceil(long long int a, long long int b) { return (a + b - 1) / b; } long long int digit(long long int a) { return (long long int)log10(a); } long long int e_dist(pair<long long int, long long int> a, pair<long long int, long long int> b) { return abs(a.first - b.first) * abs(a.first - b.first) + abs(a.second - b.second) * abs(a.second - b.second); } long long int m_dist(pair<long long int, long long int> a, pair<long long int, long long int> b) { return abs(a.first - b.first) + abs(a.second - b.second); } void Worshall_Floyd(VVl& g) { for (long long int k = (0), k_end_ = (((long long int)(g).size())); k < k_end_; ++k) for (long long int i = (0), i_end_ = (((long long int)(g).size())); i < i_end_; ++i) for (long long int j = (0), j_end_ = (((long long int)(g).size())); j < j_end_; ++j) chmin(g[i][j], g[i][k] + g[k][j]); } const long long int MOD1000000007 = 1000000007, MOD998244353 = 998244353, INF = 1e18; long long int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1}, dy[8] = {0, 1, 0, -1, -1, -1, 1, 1}; bool YN(bool flag) { cout << (flag ? "YES" : "NO") << '\n'; return flag; } bool yn(bool flag) { cout << (flag ? "Yes" : "No") << '\n'; return flag; } struct Edge { long long int from, to; long long int cost; Edge(long long int u, long long int v, long long int c) { cost = c; from = u; to = v; } bool operator<(const Edge& e) const { return cost < e.cost; } }; struct WeightedEdge { long long int to; long long int cost; WeightedEdge(long long int v, long long int c = 1) { to = v; cost = c; } bool operator<(const WeightedEdge& e) const { return cost < e.cost; } }; using WeightedGraph = V<V<WeightedEdge>>; long long int N; int main() { string hard = "hard"; cin >> N; string s; cin >> s; Vl arr(N); cin >> arr; VVl dp(N + 1, Vl(5, INF)); dp[0][0] = 0; for (long long int i = (0), i_end_ = (N); i < i_end_; ++i) { for (long long int k = (0), k_end_ = (4); k < k_end_; ++k) { if (s[i] == hard[k]) { chmin(dp[i + 1][k + 1], dp[i][k]); chmin(dp[i + 1][k], dp[i][k] + arr[i]); } else { chmin(dp[i + 1][k], dp[i][k]); } } } long long int minv = INF; for (long long int k = (0), k_end_ = (4); k < k_end_; ++k) chmin(minv, dp[N][k]); cout << minv << '\n'; }
4
#include<bits/stdc++.h> using namespace std; const int N=1000007; int a[N],b[N]; int main() { int n,m,t;cin>>n>>m;t=(m+1)/2; for(int i=1;i<=t;++i) a[i]=i,b[i]=t+t+1-i; for(int i=t+1;i<=m;++i) a[i]=t+i,b[i]=a[i]+2*(m-i+1); for(int i=1;i<=m;++i) printf("%d %d\n",a[i],b[i]); }
0
#include <bits/stdc++.h> using namespace std; int IN() { int c, f, x; while (!isdigit(c = getchar()) && c != '-') ; c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0'); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0'; return !f ? x : -x; } const int N = 200000 + 19; struct QType { int i, l, r; }; vector<QType> I[N], O[N]; struct Rect { int x1, y1, x2, y2; } R[N]; int A[N], B[N], vis[N]; int n, ans; int mn[N * 8], mx[N * 8]; set<int> s[N * 4]; int Ql, Qr, id, opt; void upd(int x) { mn[x] = min(mn[(x << 1)], mn[(x << 1 | 1)]); mx[x] = max(mx[(x << 1)], mx[(x << 1 | 1)]); if (!s[x].empty()) { int tmp = *--s[x].end(); mn[x] = max(mn[x], tmp); if (tmp > mx[x]) mx[x] = 0; if (tmp >= mn[x] && !vis[tmp]) { mx[x] = max(mx[x], tmp); } } } void Modify(int x, int L, int R) { if (Ql <= L && R <= Qr) { if (opt == 1) { s[x].insert(id); } else if (opt == 2) { s[x].erase(id); } upd(x); return; } if (Ql <= ((L + R) >> 1)) Modify((x << 1), L, ((L + R) >> 1)); if (Qr > ((L + R) >> 1)) Modify((x << 1 | 1), ((L + R) >> 1) + 1, R); upd(x); } int main() { n = IN(); for (int i = 1; i < n + 1; i++) { R[i] = (Rect){IN(), IN(), IN(), IN()}; A[++*A] = R[i].x1; A[++*A] = R[i].x2; B[++*B] = R[i].y1; B[++*B] = R[i].y2; } sort(A + 1, A + *A + 1); *A = unique(A + 1, A + *A + 1) - A - 1; sort(B + 1, B + *B + 1); *B = unique(B + 1, B + *B + 1) - B - 1; for (int i = 1; i < n + 1; i++) { R[i].x1 = lower_bound(A + 1, A + *A + 1, R[i].x1) - A; R[i].x2 = lower_bound(A + 1, A + *A + 1, R[i].x2) - A - 1; R[i].y1 = lower_bound(B + 1, B + *B + 1, R[i].y1) - B; R[i].y2 = lower_bound(B + 1, B + *B + 1, R[i].y2) - B - 1; I[R[i].x1].push_back((QType){i, R[i].y1, R[i].y2}); O[R[i].x2].push_back((QType){i, R[i].y1, R[i].y2}); } for (int w = 1; w < *A + 1; w++) { for (QType T : I[w]) { id = T.i; Ql = T.l; Qr = T.r; opt = 1; Modify(1, 1, *B); } while (mx[1]) { vis[mx[1]] = 1; Ql = R[mx[1]].y1; Qr = R[mx[1]].y2; opt = 3; Modify(1, 1, *B); ans++; } for (QType T : O[w]) { id = T.i; Ql = T.l; Qr = T.r; opt = 2; Modify(1, 1, *B); } } printf("%d\n", ans + 1); }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int N, A[MAXN], B[MAXN]; int st[4 * MAXN]; int query_st(int L, int R) { int ans = 0; for (L += N, R += N; L < R; L >>= 1, R >>= 1) { if (L & 1) ans += st[L++]; if (R & 1) ans += st[--R]; } return ans; } void update_st(int p, int v) { for (st[p += N] = v; p > 1; p >>= 1) st[p >> 1] = st[p] + st[p ^ 1]; } int build_tour(int n, int L, int R) { if (L == R) return st[n] = 1; int mid = (L + R) / 2; return st[n] = build_tour(n << 1, L, mid) + build_tour(n << 1 | 1, mid + 1, R); } int update_tour(int n, int L, int R, int p, int v) { if (L == R) return st[n] = v; int mid = (L + R) / 2; if (p <= mid) return st[n] = update_tour(n << 1, L, mid, p, v) + st[n << 1 | 1]; return st[n] = update_tour(n << 1 | 1, mid + 1, R, p, v) + st[n << 1]; } int kth_tour(int n, int L, int R, int k) { if (L == R) return L; int mid = (L + R) / 2; if (st[n << 1] <= k) return kth_tour(n << 1 | 1, mid + 1, R, k - st[n << 1]); return kth_tour(n << 1, L, mid, k); } int main() { scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", &A[i]); for (int i = 0; i < N; i++) scanf("%d", &B[i]); for (int i = N - 1; i >= 0; i--) { update_st(A[i], 1); A[i] = query_st(0, A[i]); } for (int i = 1; i < 2 * N; i++) st[i] = 0; for (int i = N - 1; i >= 0; i--) { update_st(B[i], 1); A[i] += query_st(0, B[i]); if (i > 0) A[i - 1] += A[i] / (N - i); A[i] %= N - i; } build_tour(1, 0, N - 1); for (int i = 0; i < N; i++) { B[i] = kth_tour(1, 0, N - 1, A[i]); update_tour(1, 0, N - 1, B[i], 0); } printf("%d", B[0]); for (int i = 1; i < N; i++) printf(" %d", B[i]); printf("\n"); }
4
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <class T> T Gcd(T x, T y) { return y ? Gcd(y, x % y) : x; } template <class T> T Lcm(T x, T y) { return x / Gcd(x, y) * y; } template <class T> void Adj(T &x, T y) { if (x >= y) x %= y; while (x < 0) x += y; } const long long MOD = 998244353LL; const int M = 5000005; const int N = 100005; int n, m, a[N], b[N], c[N], f[N], t, s; long long ans; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[a[i - 1]] = a[i]; } b[a[n]] = 0; for (int i = 2; i <= m; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &c[j]); if (b[c[j - 1]] != c[j]) b[c[j - 1]] = 0; } if (b[c[n]]) b[c[n]] = 0; } for (int i = 1; i <= n; i++) if (!f[a[i]]) { t = a[i]; s = 0; while (t) { s++; f[t] = 1; t = b[t]; } ans += 1LL * s * (s + 1) / 2; } printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const long long INF = 1e18; const double eps = 1e-6; const int maxn = 4e5 + 5; map<long long, long long> mp; long long n, I, K, idx, a, b[maxn], sum[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> I; for (int i = 1; i <= n; ++i) { cin >> a; if (!mp[a]) b[++idx] = a; ++mp[a]; } K = pow(2, min(19 * 1LL, 8 * I / n)); if (K >= idx) { cout << 0 << endl; return 0; } sort(b + 1, b + 1 + idx); for (int i = 1; i <= idx; ++i) sum[i] = sum[i - 1] + mp[b[i]]; long long res = INF; for (int i = 1; i + K - 1 <= idx; ++i) res = min(res, (sum[i - 1] - sum[0]) + (sum[idx] - sum[i + K - 1])); cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cout << 1; cout << endl; for (int i = 1; i < n; i++) cout << 8; cout << 9; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; int Szukaj(vector<int>& a, int x) { if (a.size() == 0) return -1; int l = 0, p = a.size() - 1, s, r = -1; while (l <= p) { s = (l + p) / 2; if (a[s] >= x) { r = s; p = s - 1; } else l = s + 1; } return r; } int main() { ios_base::sync_with_stdio(0); int n, m, s, e; cin >> n >> m >> s >> e; vector<int> a(n); vector<vector<int> > b(100000); for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; } for (int i = 0; i < m; ++i) { int x; cin >> x; b[x - 1].push_back(i); } vector<vector<int> > dp(301, vector<int>(n, INF)); for (int i = 0; i < n; ++i) if (b[a[i]].size() != 0) dp[1][i] = b[a[i]][0]; for (int i = 2; i <= 300; ++i) { int pier = INF; for (int j = 0; j < n; ++j) { if (j != 0) pier = min(pier, dp[i - 1][j - 1]); if (pier != INF) { int nast = Szukaj(b[a[j]], pier + 1); if (nast != -1) dp[i][j] = b[a[j]][nast]; } } } int odp = 0; for (int i = 1; i <= 300; ++i) for (int j = 0; j < n; ++j) if (dp[i][j] != INF && e * i + (j + 1) + (dp[i][j] + 1) <= s) odp = max(odp, i); cout << odp; return 0; }
3
#include <bits/stdc++.h> long long INF = (1ll << 61); using namespace std; long double EPS = 1e-15; struct pt { long double x, y; pt() {} pt(long double x, long double y) : x(x), y(y) {} pt(const pt &p) : x(p.x), y(p.y) {} pt operator+(const pt &p) const { return pt(x + p.x, y + p.y); } pt operator-(const pt &p) const { return pt(x - p.x, y - p.y); } pt operator*(long double c) const { return pt(x * c, y * c); } pt operator/(long double c) const { return pt(x / c, y / c); } bool operator==(const pt &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; double dot(pt p, pt q) { return p.x * q.x + p.y * q.y; } double dist2(pt p, pt q) { return dot(p - q, p - q); } double cross(pt p, pt q) { return p.x * q.y - p.y * q.x; } ostream &operator<<(ostream &os, const pt &p) { return os << "(" << p.x << "," << p.y << ")"; } struct seg { pt p, q; int id; seg(pt &x, pt &y, int id) : p(x), q(y), id(id) {} long double get_y(long double x) const { if (abs(p.x - q.x) < EPS) return p.y; return p.y + (q.y - p.y) * (x - p.x) / (q.x - p.x); } bool operator<(const seg &b) const { long double x = max(min(p.x, q.x), min(b.p.x, b.q.x)); return get_y(x) < b.get_y(x) - EPS; } }; inline bool intersect1d(long double l1, long double r1, long double l2, long double r2) { if (l1 > r1) swap(l1, r1); if (l2 > r2) swap(l2, r2); return max(l1, l2) <= min(r1, r2) + EPS; } inline int vec(const pt &a, const pt &b, const pt &c) { long double s = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); return abs(s) < EPS ? 0 : s > 0 ? +1 : -1; } bool intersect(const seg &a, const seg &b) { return intersect1d(a.p.x, a.q.x, b.p.x, b.q.x) && intersect1d(a.p.y, a.q.y, b.p.y, b.q.y) && vec(a.p, a.q, b.p) * vec(a.p, a.q, b.q) <= 0 && vec(b.p, b.q, a.p) * vec(b.p, b.q, a.q) <= 0; } bool LinesParallel(pt a, pt b, pt c, pt d) { return fabs(cross(b - a, c - d)) < EPS; } bool LinesCollinear(pt a, pt b, pt c, pt d) { return LinesParallel(a, b, c, d) && fabs(cross(a - b, a - c)) < EPS && fabs(cross(c - d, c - a)) < EPS; } bool SegmentsIntersect(pt a, pt b, pt c, pt d) { if (LinesCollinear(a, b, c, d)) { if (dist2(a, c) < EPS || dist2(a, d) < EPS || dist2(b, c) < EPS || dist2(b, d) < EPS) return true; if (dot(c - a, c - b) > 0 && dot(d - a, d - b) > 0 && dot(c - b, d - b) > 0) return false; return true; } if (cross(d - a, b - a) * cross(c - a, b - a) > 0) return false; if (cross(a - c, d - c) * cross(b - c, d - c) > 0) return false; return true; } pt ComputeLineIntersection(pt a, pt b, pt c, pt d) { b = b - a; d = c - d; c = c - a; return a + b * cross(c, d) / cross(b, d); } pt find_intp(const seg &a, const seg &b) { return ComputeLineIntersection(a.p, a.q, b.p, b.q); } struct line_sweep { struct event { long double x; int tp, id; event() {} event(long double x, int tp, int id) : x(x), tp(tp), id(id) {} bool operator<(const event &e) const { if (abs(x - e.x) > EPS) return x < e.x; return tp > e.tp; } }; set<seg> s; vector<set<seg>::iterator> where; inline set<seg>::iterator prev(set<seg>::iterator it) { return it == s.begin() ? s.end() : --it; } inline set<seg>::iterator next(set<seg>::iterator it) { return ++it; } vector<pt> solve(const vector<seg> &a, bool find_points = 0) { vector<pt> res; int n = (int)a.size(); vector<event> e; for (int i = 0; i < n; ++i) { e.push_back(event(min(a[i].p.x, a[i].q.x), +1, i)); e.push_back(event(max(a[i].p.x, a[i].q.x), -1, i)); } sort(e.begin(), e.end()); s.clear(); where.resize(a.size()); for (size_t i = 0; i < e.size(); ++i) { int id = e[i].id; if (e[i].tp == +1) { auto nxt = s.lower_bound(a[id]), prv = prev(nxt); if (nxt != s.end() && intersect(*nxt, a[id])) { pt p = find_intp(*nxt, a[id]); res.push_back(p); if (!find_points) return res; } if (prv != s.end() && intersect(*prv, a[id])) { pt p = find_intp(*prv, a[id]); res.push_back(p); if (!find_points) return res; } where[id] = s.insert(nxt, a[id]); } else { if (where[id] == s.end()) continue; auto nxt = next(where[id]), prv = prev(where[id]); if (nxt != s.end() && prv != s.end() && intersect(*nxt, *prv)) { pt p = find_intp(*nxt, *prv); res.push_back(p); if (!find_points) return res; } s.erase(where[id]); } } return res; } }; struct car { long double dx, dy; pt p0; car(long double x, long double y, long double _dx, long double _dy, long double s) { p0 = pt(x, y); long double n = sqrt(dist2(pt(_dx, _dy), pt(0, 0))); dx = _dx * s / n; dy = _dy * s / n; } pt wait(long double t) { return p0 + pt(dx * t, dy * t); } }; int n; vector<car> cars; bool check(long double t) { vector<seg> segs; for (int i = 0; i < n; i++) { pt a = cars[i].p0; pt b = cars[i].wait(t); segs.push_back(seg(a, b, i)); } line_sweep ls; vector<pt> pts = ls.solve(segs); return int((pts).size()) > 0; } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { long double x, y, dx, dy, s; cin >> x >> y >> dx >> dy >> s; cars.push_back(car(x, y, dx, dy, s)); } long double i = 0, j = 1e12; while (fabs(j - i) / fmax(1, fabs(j)) > 1e-12) { long double m = (i + j) / 2; if (check(m)) { j = m; } else { i = m; } } if (j > 1e11) { cout << "No show :(" << endl; } else { cout << setprecision(20) << i << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, z = 0, v = 0, c = 0, b; cin >> n; for (int i = 0; i < n; i++) { cin >> x >> y; if (x % 2 != 0) z++; if (y % 2 != 0) v++; if ((y % 2 == 0 && x % 2 != 0) || (y % 2 != 0 && x % 2 == 0)) c++; } if (v % 2 == 0 && z % 2 == 0) cout << 0 << endl; else if (c % 2 == 0 && c > 0) cout << 1 << endl; else cout << -1 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAX = 1e3 + 7; const int INF = 1e9; int n, m, k, p, q, a, b, c, d, cnt = 0, ans = 0; map<int, int> mp; int ar[MAX]; vector<int> used; set<pair<int, int> > did; int solve(int a, int b) { if (did.find({a, b}) != did.end()) return 0; int ret = 2; used.push_back(a); used.push_back(b); mp[a]--; mp[b]--; while (true) { int tmp = a + b; if (tmp > 1e9) break; if (mp.find(tmp) == mp.end() || mp[tmp] <= 0) break; mp[tmp]--; used.push_back(tmp); a = b; b = tmp; ret++; } for (int i = 0; i < used.size(); ++i) mp[used[i]]++; used.clear(); return ret; } int MAIN() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> ar[i]; mp[ar[i]]++; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (i != j) { ans = max(ans, solve(ar[i], ar[j])); did.insert({ar[i], ar[j]}); } cout << ans << endl; return 0; } int main() { ios_base::sync_with_stdio(false); int start = clock(); cout << fixed << setprecision(16); int ret = MAIN(); return ret; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int a, b, c, ans = 0; cin >> a >> b >> c; for (int i = 0; i <= a; i++) { if (b >= 2 * i && c >= 4 * i) { ans = 7 * i; } } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); } template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class T> ostream &operator<<(ostream &os, set<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) { os << "{ "; for (auto m : M) os << "(" << m.first << ":" << m.second << ") "; return os << "}"; } int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); long long n; cin >> n; long long ans = n; vector<long long> l(n), r(n); for (int i = 0; i < (n); ++i) { cin >> l[i] >> r[i]; } sort(begin(l), end(l)); sort(begin(r), end(r)); for (int i = 0; i < (n); ++i) { ans += max(l[i], r[i]); } cout << ans << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; int stone[100005]; int main() { int l, w; cin >> l >> w; long long sum = 0; for (int i = 1; i <= l - 1; i++) { scanf("%d", &stone[i]); if (i <= w) sum += stone[i]; } int cnt = 0; for (int i = w + 1; i <= l - 1; i++) { if (stone[i] <= stone[i - w] - cnt) { sum = sum - stone[i - w] + stone[i] + cnt; cnt = 0; } else { cnt = stone[i] - (stone[i - w] - cnt); } } printf("%d\n", sum); return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } template <class T1, class T2, class T3, class T4> void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6 << endl; } int main() { int n, k; while (scanf("%d%d", &n, &k) == 2) { if (k == 2) { printf("%d\n", n - 1); } else { int now = n / k; now *= 2; if (n % k == 2) now++; else if (n % k > 2) now += 2; deb(now); } queue<int> q; int k1 = 0, cnt = 2; for (int i = 1; i <= k; i++) { printf("%d %d\n", 1, cnt); q.push(cnt++); n--; } n--; while (n) { int now = q.front(); q.pop(); printf("%d %d\n", now, cnt); q.push(cnt++); n--; } } }
4
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *r) -> decltype(cerr << *r); template <class c> char dud(...); struct muu { template <class c> muu &operator<<(const c &) { return *this; } }; using pii = pair<int, int>; using ld = long double; using ll = long long; int dp[3007][3007]; const int mod = 998244353; int main() { string s1, s2; cin >> s1 >> s2; ll odp = 0; int q = s1.size(); for (int i = (int)q - 1; i >= 0; i--) { if (i + 1 >= s2.size()) dp[i + 1][0] += 1; for (int w = 1; w <= q - i; w++) if (w - 1 >= s2.size() || s1[i] == s2[w - 1]) dp[i][w] += dp[i + 1][w - 1]; for (int w = 0; q - i - w > 0; w++) { int bylo_jedynek = q - i - w; if (q - bylo_jedynek >= s2.size() || s1[i] == s2[q - bylo_jedynek]) dp[i][w] += dp[i + 1][w]; } for (int w = 0; w <= q - i; w++) if (dp[i][w] >= mod) dp[i][w] -= mod; } for (int w = 0; w <= s1.size(); w++) { odp += dp[0][w]; odp %= mod; } printf("%lld", odp); return 0; }
3
#include<bits/stdc++.h> using namespace std; int main(){ long long n,p; cin >> n >> p; long long a[n]; for(int i=0;i<n;i++){ cin >> a[i]; if(a[i]%2==1){ cout << (long long)pow(2,n-1); return 0; } } if(p==0)cout << (long long)pow(2,n); else cout << 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000005; int n; int tp[MAXN]; int son[MAXN][2]; int f[MAXN], g[MAXN], h[MAXN]; void dfs(int o) { if (tp[o] == 0) return; if (tp[o] == 4) { dfs(son[o][0]); f[o] = !f[son[o][0]]; } else { dfs(son[o][0]), dfs(son[o][1]); int a = f[son[o][0]], b = f[son[o][1]]; f[o] = tp[o] == 1 ? (a & b) : (tp[o] == 2 ? (a | b) : (a ^ b)); } } void dfs_1(int o) { if (tp[o] == 0) return; if (tp[o] == 4) g[son[o][0]] = g[o], dfs_1(son[o][0]); else { int a = son[o][0], b = son[o][1]; int x = f[a], y = f[b], c, d; int res; c = !x, d = y; res = tp[o] == 1 ? (c & d) : (tp[o] == 2 ? (c | d) : (c ^ d)); g[a] = res != f[o] ? g[o] : h[o]; c = x, d = !y; res = tp[o] == 1 ? (c & d) : (tp[o] == 2 ? (c | d) : (c ^ d)); g[b] = res != f[o] ? g[o] : h[o]; dfs_1(a), dfs_1(b); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) f[i] = -1; char s[5]; for (int i = 1; i <= n; i++) { scanf("%s", s); if (s[0] == 'A' || s[0] == 'X' || s[0] == 'O') { scanf("%d %d", &son[i][0], &son[i][1]); tp[i] = s[0] == 'A' ? 1 : (s[0] == 'O' ? 2 : 3); } else if (s[0] == 'N') scanf("%d", &son[i][0]), tp[i] = 4; else scanf("%d", &f[i]); } dfs(1); g[1] = !f[1]; for (int i = 1; i <= n; i++) h[i] = f[1]; dfs_1(1); for (int i = 1; i <= n; i++) if (tp[i] == 0) printf("%d", g[i]); return 0; }
6
#include <bits/stdc++.h> #define debug(x) cout << #x" = " << x; #define st first #define nd second using namespace std; using namespace placeholders; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; const int MAXN = 5E5 + 10, SIGMA = 26, D = 18; char s[MAXN]; int nex[MAXN][SIGMA + 1]; int p[D][MAXN]; int main(){ scanf("%s", s); int n = strlen(s); fill_n(nex[n], SIGMA, n + 1); p[0][n] = n + 1; fill_n(nex[n + 1], SIGMA, n + 1); p[0][n + 1] = n + 1; for (int i = n - 1; i >= 0; --i){ int m = s[i] - 'a'; nex[i][m] = i + 1; for (int j = m + 1; j <= SIGMA; ++j) nex[i][j] = nex[nex[i][j - 1]][j - 1]; for (int j = 0; j < m; ++j) nex[i][j] = nex[nex[i][SIGMA]][j]; p[0][i] = nex[i][SIGMA]; } for (int i = 1; i < D; ++i) for (int j = 0; j <= n + 1; ++j) p[i][j] = p[i - 1][p[i - 1][j]]; int q; scanf("%d", &q); for (int l, r, qi = 0; qi < q; ++qi){ scanf("%d%d", &l, &r); --l; for (int i = D - 1; i >= 0; --i) if (p[i][l] <= r) l = p[i][l]; puts(l == r ? "Yes" : "No"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; ; int MOD = 1e9 + 7; struct mod_int { int val; mod_int(long long v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int mod_inv(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } mod_int &operator+=(const mod_int &other) { val += other.val; if (val >= MOD) val -= MOD; return *this; } mod_int &operator-=(const mod_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } mod_int &operator*=(const mod_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); } friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; } friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; } friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; } friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; } mod_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } mod_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } mod_int operator++(int) { mod_int before = *this; ++*this; return before; } mod_int operator--(int) { mod_int before = *this; --*this; return before; } mod_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const mod_int &other) const { return val == other.val; } bool operator!=(const mod_int &other) const { return val != other.val; } mod_int inv() const { return mod_inv(val); } mod_int pow(long long p) const { assert(p >= 0); mod_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } friend ostream &operator<<(ostream &stream, const mod_int &m) { return stream << m.val; } friend istream &operator>>(istream &stream, mod_int &m) { return stream >> m.val; } }; const int N = 2e5 + 10; mod_int p[N], fac[N], inv[N]; map<pair<int, int>, mod_int> dp; int pre; mod_int ncr(int n, int r) { return fac[n] * inv[r] * inv[n - r]; } mod_int tot_ncr(int n, int k) { if ((int)((dp).size()) == 0) { for (int i = 0; i <= k; i++) { dp[{n, k}] += ncr(n, i); } pre = k; } else { dp[{n, k}] = mod_int(2) * mod_int(dp[{n - 1, pre}]); dp[{n, k}] -= ncr(n - 1, pre); while (pre > k) { dp[{n, k}] -= ncr(n, pre); pre--; } } return dp[{n, k}]; } void solve() { int n; long long T; mod_int ans = 2; cin >> n >> T; long long t[n + 1]; t[0] = 0; for (int i = 1; i <= n; ++i) { cin >> t[i]; t[i] = t[i - 1] + t[i]; } for (int i = 1; i <= n; ++i) { if (t[i] + i <= T) p[i] = 1; else if (t[i] > T) p[i] = 0; else p[i] = tot_ncr(i, T - t[i]) / ans.pow(i); } cout << accumulate(p + 1, p + n + 1, mod_int(0)) << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; clock_t start, end; start = clock(); cout << fixed << setprecision(15); fac[0] = inv[0] = 1; for (int i = 1; i < N; i++) { fac[i] = fac[i - 1] * i; inv[i] = inv[i - 1] / i; } while (t--) { solve(); } end = clock(); double time_taken = double(end - start) / double(CLOCKS_PER_SEC); }
6
#include <bits/stdc++.h> int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); if (n > m || n > k) { printf("NO"); } else { printf("YES"); } }
1
#include<cstdio> #include<cstring> #include<vector> #include<algorithm> #define MAXN 100000 using namespace std; typedef long long LL; vector<int> G[MAXN+5]; int N,ans; int cnt[MAXN+5][21]; void DFS(int u,int fa) { for(int i=0;i<(int)G[u].size();i++) { int v=G[u][i]; if(v==fa) continue; DFS(v,u); for(int j=0;j<20;j++) cnt[u][j]+=cnt[v][j]; } int val=0; for(int i=20;i>=0;i--) if(cnt[u][i]>=2) { val=i+1; break; } while(cnt[u][val]==1) val++; ans=max(ans,val); cnt[u][val]++; for(int i=0;i<val;i++) cnt[u][i]=0; } int main() { // freopen("uninity.in","r",stdin); // freopen("uninity.out","w",stdout); scanf("%d",&N); int u,v; for(int i=1;i<N;i++) { scanf("%d %d",&u,&v); G[u].push_back(v); G[v].push_back(u); } DFS(1,-1); printf("%d\n",ans); return 0; }
0
#include <cassert> #include <algorithm> #include <vector> #include <iostream> using namespace std; vector<bool> f(string &a, string &b) { int la = a.size(), lb = b.size(); vector<bool> ret(la+1, true); for (int i = 0; i < la; i++) { for (int j = i, k = 0; j < la && k < lb; j++, k++) { if (a[j] != '?' && b[k] != '?' && a[j] != b[k]) { ret[i] = false; break; } } } return ret; } int calc(string &a, string &b, string &c) { vector<bool> ab = f(a, b), bc = f(b, c), ac = f(a, c); int la = a.size(), lb = b.size(), lc = c.size(), ans = la+lb+lc; for (int i = 0; i <= la; i++) { for (int j = 0; j <= max(la, lb); j++) { if (ab[i] && (j >= lb || bc[j]) && (i + j >= la || ac[i+j])) { ans = min(ans, max({la, i + lb, i + j + lc})); } } } return ans; } int main() { string a, b, c; cin >> a >> b >> c; cout << min({calc(a, b, c), calc(a, c, b), calc(b, a, c), calc(b, c, a), calc(c, a, b), calc(c, b, a)}) << endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 200000; int n, K, D, a[maxn + 5], b[maxn + 5], tem[maxn + 5], r[maxn + 5], ans, AL, AR; bool vis[maxn + 5]; int top[2], stk[2][maxn + 5], R[2][maxn + 5]; long long MIN[(maxn << 2) + 5], tag[(maxn << 2) + 5]; inline int Find(int x, int L = 1, int R = tem[0]) { for (int mid = L + (R - L >> 1); L <= R; mid = L + (R - L >> 1)) if (x == tem[mid]) return mid; else x < tem[mid] ? R = mid - 1 : L = mid + 1; } inline void OrzZH() { int ans = 1, L = 1, R = 1, lst = 1; for (int i = 2; i <= n; i++) if (a[i - 1] != a[i]) { if (i - lst > ans) ans = i - lst, L = lst, R = i - 1; lst = i; } if (n - lst + 1 > ans) L = lst, R = n; printf("%d %d\n", L, R); } inline void Addtag(int p, long long k) { MIN[p] += k; tag[p] += k; } inline void Pushdown(int p) { if (tag[p]) Addtag(p << 1, tag[p]), Addtag(p << 1 | 1, tag[p]), tag[p] = 0; } void Insert(int L, int R, long long k, int l = 1, int r = n, int p = 1) { if (L == l && r == R) return Addtag(p, k); int mid = l + (r - l >> 1); Pushdown(p); if (R <= mid) Insert(L, R, k, l, mid, p << 1); else if (L > mid) Insert(L, R, k, mid + 1, r, p << 1 | 1); else Insert(L, mid, k, l, mid, p << 1), Insert(mid + 1, R, k, mid + 1, r, p << 1 | 1); (MIN[p] = min(MIN[(p) << 1], MIN[(p) << 1 | 1])); } inline void Push(int x, int lst = 0) { lst = x; while (top[0] && stk[0][top[0]] > a[x]) Insert(lst + 1, R[0][top[0]], stk[0][top[0]] - a[x]), lst = R[0][top[0]--]; stk[0][++top[0]] = a[x]; R[0][top[0]] = lst; lst = x; while (top[1] && stk[1][top[1]] < a[x]) Insert(lst + 1, R[1][top[1]], a[x] - stk[1][top[1]]), lst = R[1][top[1]--]; stk[1][++top[1]] = a[x]; R[1][top[1]] = lst; } int Ask(int L, int R, long long k, int l = 1, int r = n, int p = 1) { if (R < l || r < L) return -1; if (l == r) return l; int mid = l + (r - l >> 1); Pushdown(p); int pos = -1; if (MIN[p << 1 | 1] <= k) pos = Ask(L, R, k, mid + 1, r, p << 1 | 1); if (pos < 0 && MIN[p << 1] <= k) pos = Ask(L, R, k, l, mid, p << 1); return pos; } int main() { scanf("%d%d%d", &n, &K, &D); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); if (!D) return OrzZH(), 0; for (int i = 1; i <= n; i++) b[i] = (a[i] % D + D) % D; tem[0] = 0; for (int i = 1; i <= n; i++) tem[++tem[0]] = a[i]; sort(tem + 1, tem + 1 + tem[0]); tem[0] = unique(tem + 1, tem + 1 + tem[0]) - tem - 1; for (int i = 1; i <= n; i++) a[i] = Find(a[i]); for (int i = 1, j = 0; i <= n; vis[a[i]] = false, r[i++] = j) while (j < n && b[i] == b[j + 1] && !vis[a[j + 1]]) vis[a[++j]] = true; for (int i = 1; i <= n; i++) a[i] = tem[a[i]]; ans = 1; AL = n; AR = n; Insert(n, n, (long long)-D * n); Push(n); for (int i = n - 1; i; i--) { Insert(i, i, (long long)-D * i); Push(i); if (MIN[1] > (long long)D * (K - i)) continue; int pos = Ask(i, r[i], (long long)D * (K - i)); if (pos - i + 1 >= ans) ans = pos - i + 1, AL = i, AR = pos; } return printf("%d %d\n", AL, AR), 0; }
5
#include <bits/stdc++.h> using namespace std; int n, a, k; const int N = 1e6 + 5; int t[2 * N]; void modify(int p) { for (t[p += n]++; p > 1; p >>= 1) t[p >> 1] = t[p] + t[p ^ 1]; } int query(int l, int r) { int res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) res += t[l++]; if (r & 1) res += t[--r]; } return res; } int main() { cin >> n >> k; if (2 * k > n) k = n - k; for (int i = 0; i < N; i++) t[i] = 0; int cur = 0; long long cnt = 1; vector<long long> retvals; for (int i = 0; i < n; i++) { cnt++; if (cur + k < n) { cnt += query(cur + 1, cur + k); modify(cur); cur += k; modify(cur); } else { cnt += query(cur + 1, n); cnt += query(0, (cur + k) % n); modify(cur); cur = (cur + k) % n; modify(cur); } retvals.push_back(cnt); } for (int i = 0; i < retvals.size() - 1; i++) { cout << retvals[i] << " "; } cout << retvals.back(); return 0; }
4
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; const int MAX_N = 15; int dist[MAX_N][MAX_N]; // dist[i][j] i から j への距離 // dp[i][j]: 状態iでjにいるときの最短経路 // 状態iはビットで管理: 00011->点0と点1は通貨済 int dp[1 << MAX_N][MAX_N]; int n, m; const int inf = 1001001; // 巡回セールスマン問題 int dfs(int s, int v) { if (dp[s][v] >= 0) return dp[s][v]; if (s == (1 << n) - 1 && v == 0) return dp[s][v] = 0; int ans = inf; rep(u, n) { // uに行ったことがない場合 if (!(s >> u & 1)) ans = min(ans, dfs(s | 1 << u, u) + dist[v][u]); } return dp[s][v] = ans; } int main() { cin >> n >> m; memset(dp, -1, sizeof(dp)); // パスがない場合は距離がinfになる fill(dist[0], dist[0] + MAX_N * MAX_N, inf); rep(i, m) { int f, t, c; cin >> f >> t >> c; dist[f][t] = c; } int ans = dfs(0, 0); ans = ans == inf ? -1 : ans; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef pair <int, int> pii; int n, m; int a[777][777]; int col[777]; bool bad; int W, B; vector <pii> v; int mem[777][777]; void dfs(int p) { if(col[p] == 0) ++W; else ++B; for(int i = 1; i <= n; i++) { if(i == p) continue; if(a[p][i] == 0) { if(col[i] == -1) { col[i] = col[p] ^ 1; dfs(i); } else if (col[i] == col[p]) { bad = true; return ; } } } } int nC2(int n) { return (n * (n - 1)) / 2; } int dp(int now, int white) { if(now == v.size()) { int black = n - white; return nC2(white) + nC2(black); } if(mem[now][white] != -1) return mem[now][white]; int ans = min(dp(now + 1, white + v[now].first), dp(now + 1, white + v[now].second)); return mem[now][white] = ans; } int main() { scanf("%d %d", &n, &m); for(int i = 0; i < m; i++) { int p, q; scanf("%d %d", &p, &q); a[p][q] = a[q][p] = 1; } memset(col, -1, sizeof col); for(int i = 1; i <= n; i++) { if(col[i] == -1) { col[i] = 0; W = B = 0; dfs(i); v.emplace_back(W, B); } } if(bad) { printf("-1\n"); exit(0); } memset(mem, -1, sizeof mem); printf("%d\n", dp(0, 0)); return 0; }
0
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <iostream> #include <cstdio> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef pair <int,int> P; typedef pair <int,P> PP; static const double EPS = 1e-8; int tx[] = {0,1,0,-1}; int ty[] = {-1,0,1,0}; const static char weekdays[][4] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; const static char times[][6] = {"Day","Night"}; const char* compute_weekday(int minutes){ int tmp = (minutes / (60 * 24)) % 7; return weekdays[tmp]; } const char* compute_time_band(int minutes){ int idx = 0; int tmp = minutes % (60 * 24); if(tmp >= 6 * 60 && tmp < 18 * 60){ idx = 0; } else{ idx = 1; } return times[idx]; } int main(){ int wait_hatch; int num_of_egg; int stage_life; char mutation_weekday[4]; char mutation_time_band[6]; int inv_mutation_prob; int total_stages; double probs[101]; while(~scanf("%d %d %d %s %s %d %d", &wait_hatch, &num_of_egg, &stage_life, mutation_weekday, mutation_time_band, &inv_mutation_prob, &total_stages)){ if(total_stages == 0) break; probs[0] = 1.0; for(int i=0;i<num_of_egg;i++){ probs[i+1] = probs[i] * (1.0 - 1.0/inv_mutation_prob); } double res = 0.0; for(int start=0; start < 60 * 24 * 7;start++){ int init_start = start; double prob = 1.0; for(int round = 0; round < total_stages; round++){ int time = init_start + wait_hatch; if(strcmp(mutation_weekday,"All") != 0 && strcmp(compute_weekday(time),mutation_weekday) != 0){ init_start += stage_life; continue; } if(strcmp(mutation_time_band,"All") != 0 && strcmp(compute_time_band(time),mutation_time_band) != 0){ init_start += stage_life; continue; } if(strcmp(mutation_weekday,"All") != 0 && strcmp(compute_weekday(init_start),compute_weekday(time)) != 0){ init_start += stage_life; continue; } if(strcmp(mutation_time_band,"All") != 0 && strcmp(compute_time_band(init_start),compute_time_band(time)) != 0){ init_start += stage_life; continue; } prob *= probs[num_of_egg]; init_start += stage_life; } res = max(res,1.0-prob); } printf("%.10f\n",res); } }
0
#include <bits/stdc++.h> using namespace std; int n, cur, a[100005]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); cur = 1; for (int i = 0; i < n; i++) { if (a[i] >= cur) { cur++; } } printf("%d\n", cur); return 0; }
2
#include<bits/stdc++.h> using namespace std; int a[200000]; int main() { int n,ans=0; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } for(int i=n;i>=1;i--) { for(int j=i+i;j<=n;j+=i) { a[i]^=a[j]; }if(a[i]) { ans++; } } cout<<ans<<endl; for(int i=1;i<=n;i++) { if(a[i]) { cout<<i<<endl; } } }
0
#include <iostream> #include <queue> using namespace std; void loadData(int *data,int *cData){ for(int i=0;i<=9;i++){ cData[i] = data[i]; } } int main(){ //dataが固定用。aData,bDataを変化させて行う。aDataは頭を固定しbDataにてへんか int data[9+1]; int aData[9+1]; int bData[9+1]; int cData[9+1]; int ansData[9+1]; int ansState=0; int ansCnt = 0; int size = 0; char tmp; queue<int> ans; while(cin>>tmp){ if(tmp=='0')break; int flag = 0; ansState = 0; ansCnt = 0; for(int i=0;i<=9;i++){ data[i]=0; ansData[i]=0; } //データ入力 data[tmp-'0']++; for(int i=1;i<13;i++){ cin>>tmp; data[tmp-'0']++; } int head,sum; //なにを追加するのかのループ for(int i=1;i<=9;i++){ head=0; sum = 0; loadData(data,aData); if(aData[i]==4){ continue; } aData[i]++; //頭をまず考える。 for(int j=1;j<=9;j++){ head = 0; sum = 0; loadData(aData,bData); if(bData[j]-2>=0){ //cout<<"head is "<<j<<endl; head++; bData[j]-=2; } for(int l=0;l<2;l++){ sum = 0; if(l==0){ loadData(bData,cData); //さらに階段状 for(int k=1;k<=7;k++){ if(cData[k] && cData[k+1] && cData[k+2]){ cData[k]--; cData[k+1]--; cData[k+2]--; sum++; k=0; } } //その後3つあるものを考える。 for(int k=1;k<=9;k++){ if(cData[k]-3>=0){ cData[k]-=3; sum++; //cout<<"triple,k = "<<k<<" sum = "<<sum<<endl; } } // cout<<"i ="<<i<<"head = "<<head<<"sum = "<<sum<<endl; if(head==1 && sum == 4){ ansData[i]++; ansCnt++; } } else{ loadData(bData,cData); //その後3つあるものを考える。 for(int k=1;k<=9;k++){ if(cData[k]-3>=0){ cData[k]-=3; sum++; // cout<<"triple,k = "<<k<<" sum = "<<sum<<endl; } } //さらに階段状 for(int k=1;k<=7;k++){ if(cData[k] && cData[k+1] && cData[k+2]){ cData[k]--; cData[k+1]--; cData[k+2]--; sum++; k=0; } } // cout<<"i ="<<i<<"head = "<<head<<"sum = "<<sum<<endl; if(head==1 && sum == 4){ ansData[i]++; ansCnt++; } } } } } /* for(int i=1;i<=9;i++){ if(ansData[i]){ ansState++; if(ansCnt>ansState+1){ cout<<i<<" "; } else{ cout<<i; } } } if(ansCnt==0){ cout<<"0"; } cout<<endl; */ if(ansCnt==0){ ans.push(0); size++; } else{ for(int x=1;x<=9;x++){ if(ansData[x]){ ans.push(x); size++; } } } ans.push(-1); } size = ans.size(); for(int i=0;i<size;i++){ if(ans.front()!=-1){ cout<<ans.front(); ans.pop(); if(ans.front()!=-1){ cout<<" "; } } else{ cout<<endl; ans.pop(); } } return 0; } /* Sample Input 3649596966777 6358665788577 9118992346175 9643871425498 7755542764533 1133557799246 Output for the Sample Input 2 3 5 8 3 4 1 2 3 4 5 6 7 8 9 7 8 9 1 2 3 4 6 7 8 0 */
0
#include <bits/stdc++.h> using namespace std; vector<long long> a, used; long long mem[100000 + 10]; long long solve(int ind) { if (ind < 0) return 0; if (mem[ind] != -1) return mem[ind]; if (ind - 1 >= 0 && a[ind - 1] == a[ind] - 1) return mem[ind] = max(a[ind] * used[a[ind]] + solve(ind - 2), solve(ind - 1)); return mem[ind] = a[ind] * used[a[ind]] + solve(ind - 1); } int main() { int n; while (cin >> n) { memset(mem, -1, sizeof mem); a.clear(); used.clear(); used.resize(100005, 0); for (int i = 0; i < n; i++) { int tmp; cin >> tmp; if (!used[tmp]) a.push_back(tmp); used[tmp]++; } sort(a.begin(), a.end()); cout << solve(a.size() - 1) << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, q; cin >> n >> k; vector<long long> a(n + 1); vector<long long> b(n + 1); map<long long, int> mp; for (long long i = 1; i <= n; i++) cin >> a[i]; long long count = 0; for (auto i = n; i >= 1; i--) { if (mp[a[i]] != 1) { count += 1; mp[a[i]] = 1; b[i] = count; } else { b[i] = count; } } while (k--) { cin >> q; cout << b[q] << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[1000001]; int main() { int n, ans = 1; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); if (++a[x] > a[ans]) ans = x; } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int T, n, m, i, j, x, s, c = 0, flag, ans = 0; cin >> n; m = s = 0; for (i = 1; i <= n; i++) { m = m + i; s = s + m; if (s > n) { s = s - m; ans = i - 1; break; } } if (s == n) ans = i - 1; cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int n; int main() { vector<pair<int, int> > v; v.clear(); cin >> n; int sn = sqrt(n); for (int i = 2; i < sn + 1; i++) { int c = 0; bool flag = true; while (flag) { if (n % i == 0) { c++; n /= i; } else { flag = false; } } if (c > 0) v.push_back(make_pair(i, c)); } for (int i = 0; i < v.size(); i++) { n *= v[i].first; } cout << n; }
2
#include <bits/stdc++.h> using namespace std; template <class T> string toStr(T x) { stringstream ss; ss << x; return ss.str(); } template <class T1, class T2> string toStr(pair<T1, T2> x) { return toStr(x.first) + ": " + toStr(x.second); } template <class T> string containerToStr(T v) { string r = "["; for (__typeof(v.begin()) it = v.begin(); it != v.end(); ++it) { if (r != "[") r += ", "; r += toStr(*it); } return r + "]"; } template <class T> string toStr(vector<T> v) { return containerToStr(v); } template <class T> string toStr(set<T> v) { return containerToStr(v); } template <class T1, class T2> string toStr(map<T1, T2> v) { return containerToStr(v); } struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} }; Point operator-(const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); } Point P[100 * 1000 + 10]; int N; long long vect(Point a, Point b) { return a.x * b.y - a.y * b.x; } bool operator<(const Point &a, const Point &b) { long long t = vect(a - P[0], b - P[0]); return t > 0; } bool inside(int pa, int push_back, int pc, Point q) { Point a = P[pa]; Point b = P[push_back]; Point c = P[pc]; long long s1 = vect(b - a, q - a); long long s2 = vect(c - b, q - b); long long s3 = vect(a - c, q - c); if (s1 >= 0 && s2 > 0 && s3 >= 0) return true; if (s1 <= 0 && s2 < 0 && s3 <= 0) return true; return false; } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) { int x, y; scanf("%d %d", &x, &y); P[i].x = x; P[i].y = y; } int ix = 0; for (int i = 0; i < N; ++i) { if (P[i].y < P[ix].y || (P[i].y == P[ix].y && P[i].x < P[ix].x)) { ix = i; } } swap(P[ix], P[0]); sort(P + 1, P + N); int n; scanf("%d", &n); while (n--) { int x, y; scanf("%d %d", &x, &y); Point q; q.x = x; q.y = y; if (vect(P[1] - P[0], q - P[0]) <= 0 || vect(P[N - 1] - P[0], q - P[0]) >= 0) { puts("NO"); return 0; } int p = (int)(lower_bound(P + 1, P + N, q) - P); bool f = false; if (p - 1 > 0 && inside(0, p - 1, p, q)) { f = true; } if (!f) { puts("NO"); return 0; } } puts("YES"); return 0; }
2
#include <iostream> #include <fstream> #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <sstream> #include <string> #include <vector> using namespace std; #define EPS 1e-9 #define INF MOD #define MOD 1000000007LL #define fir first #define iss istringstream #define sst stringstream #define ite iterator #define ll long long #define mp make_pair #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<n;i++) #define pi pair<int,int> #define pb push_back #define sec second #define sh(i) (1LL<<i) #define sz size() #define vi vector<int> #define vc vector #define vl vector<ll> #define vs vector<string> int Y1,m1,d1,y2,m2,d2,m[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int main(){ while(cin>>Y1>>m1>>d1>>y2>>m2>>d2){ if(Y1<0||m1<0||d1<0||y2<0||m2<0||d2<0)return 0; int a=0; while(Y1!=y2||m1!=m2||d1!=d2){ d1++; if(d1==m[m1]+1+(m1==2&&(Y1%4==0&&Y1%100!=0||Y1%400==0)))m1++,d1=1; if(m1==13)Y1++,m1=1; a++; } cout<<a<<endl; } }
0
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int n, l, t, a[100001], cnt; int main(int argc, char const *argv[]) { scanf("%d%d%d", &n, &l, &t); for (int i = 1; i <= n; i++) { int op; scanf("%d%d", &a[i], &op); a[i] += op == 1 ? t : -t; cnt += a[i] / l; if (a[i] % l < 0) { cnt--; } a[i] = (a[i] % l + l) % l; } sort(a + 1, a + n + 1); cnt = (cnt % n + n) % n; for (int i = cnt + 1; i <= n; i++) { printf("%d\n", a[i]); } for (int i = 1; i <= cnt; i++) { printf("%d\n", a[i]); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100000; int flag[N], s[5][5], n; vector<int> a[6][N]; void init() { for (int i = 2; i < N; i++) { if (flag[i]) continue; for (int j = i + i; j < N; j += i) flag[j] = 1; int t = i; for (int j = 1; j < 5; j++) { t /= 10; a[j][t].push_back(i); } } } int dfs(int i) { int num = 0; for (int j = 0; j < i; j++) { num = num * 10 + s[i][j]; } if (i == n - 1) return a[1][num].size(); int ret = 0; for (int j = 0; j < a[n - i][num].size(); j++) { int p = a[n - i][num][j]; for (int k = n - 1; k >= i; k--) { s[i][k] = s[k][i] = p % 10; p = p / 10; } ret += dfs(i + 1); } return ret; } int main() { init(); int t; scanf("%d", &t); while (t--) { char str[10]; scanf("%s", str); n = strlen(str); for (int i = 0; i < n; i++) { s[i][0] = s[0][i] = str[i] - '0'; } printf("%d\n", dfs(1)); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int Max(int &a,int b){return a = max(a,b);} int dp[2][1001][1001]; int check(const string &a,const string &b){ int n = a.size(); int m = b.size(); assert(n==m); int res = 0; for(int i=0;i<=n;i++){ for(int j=0;j<=n;j++){ Max(res,dp[0][i][j]); Max(res,dp[1][i][j]); if(j<n) Max(dp[1][i][j+1],dp[1][i][j]); if(i<n)Max(dp[0][i+1][j],dp[1][i][j]+1); if(i<n&&j<n&&a[i]==b[j])Max(dp[1][i+1][j+1],dp[0][i][j]+1); } } return res>=n; } int main(){ string a,b; cin>>a>>b; bool ans = check(a,b); cout<<(ans? "Yes":"No")<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a, t, b, c, sum = 0; cin >> t; for (int i = 0; i < t; i++) { cin >> a >> b; if (a > b) { sum = a + b; if (sum % 2 == 0) { cout << "1" << endl; } else cout << "2" << endl; } if (a < b) { sum = a + b; if (sum % 2 == 0) { cout << "2" << endl; } else cout << "1" << endl; } if (a == b) { cout << "0" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, tab[100001], occ[100001]; set<int> ss; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> tab[i]; ss.insert(tab[n]); occ[n] = 1; for (int i = n - 1; i >= 1; i--) { if (ss.count(tab[i]) != 0) occ[i] = occ[i + 1]; else { ss.insert(tab[i]); occ[i] = occ[i + 1] + 1; } } for (int i = 1; i <= m; i++) { int x; cin >> x; cout << occ[x] << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { register int c = getchar(); x = 0; int neg = 0; for (; ((c < 48 || c > 57) && c != '-'); c = getchar()) ; if (c == '-') { neg = 1; c = getchar(); } for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } if (neg) x = -x; } inline void writeln(int x) { char buffor[21]; register int i = 0; int neg = 0; if (x < 0) { neg = 1; x = -x; } do { buffor[i++] = (x % 10) + '0'; x /= 10; } while (x); i--; if (neg) putchar('-'); while (i >= 0) putchar(buffor[i--]); putchar('\n'); } const int N = (int)2e5 + 10; int n, par[N], t[N], q, List[N]; vector<int> a[N]; int in[N], out[N], Time = 0; struct segment_tree { int low[4 * N], high[4 * N], it[4 * N], lazy[4 * N]; void build(int x, int l, int r) { low[x] = l; high[x] = r; lazy[x] = 0; if (l == r) { it[x] = t[List[l]]; return; }; int m = (l + r) / 2; build(2 * x, l, m); build(2 * x + 1, m + 1, r); it[x] = it[2 * x] + it[2 * x + 1]; } void down_lazy(int x) { if (lazy[x] == 0) return; it[2 * x] = high[2 * x] - low[2 * x] + 1 - it[2 * x]; it[2 * x + 1] = high[2 * x + 1] - low[2 * x + 1] + 1 - it[2 * x + 1]; lazy[2 * x] ^= lazy[x]; lazy[2 * x + 1] ^= lazy[x]; lazy[x] = 0; } void update(int x, int l, int r) { if (l > high[x] or r < low[x]) return; if (low[x] >= l and high[x] <= r) { it[x] = high[x] - low[x] + 1 - it[x]; lazy[x] ^= 1; return; } down_lazy(x); update(2 * x, l, r); update(2 * x + 1, l, r); it[x] = it[2 * x] + it[2 * x + 1]; } int query(int x, int l, int r) { if (l > high[x] or r < low[x]) return 0; if (low[x] >= l and high[x] <= r) return it[x]; down_lazy(x); return query(2 * x, l, r) + query(2 * x + 1, l, r); } } light; void input() { cin >> n; for (int i = 2; i <= n; i++) { cin >> par[i]; a[par[i]].push_back(i); a[i].push_back(par[i]); } for (int i = 1; i <= n; i++) cin >> t[i]; } void dfs(int u) { in[u] = ++Time; List[Time] = u; for (auto v : a[u]) if (v != par[u]) dfs(v); out[u] = Time; } void solve() { dfs(1); light.build(1, 1, n); cin >> q; for (int i = 1; i <= q; i++) { string typ; int v; cin >> typ; cin >> v; if (typ == "pow") light.update(1, in[v], out[v]); else cout << light.query(1, in[v], out[v]) << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); input(); solve(); return 0; }
5
#define _USE_MATH_DEFINES #include <iostream> #include <complex> #include <algorithm> #include <vector> #include <stack> #include <string> #include <queue> #include <cmath> #include <math.h> #include <numeric> #include <list> #include <sstream> #include <fstream> #include <iomanip> #include <climits> #include <set> #include <memory.h> #include <memory> #include <cstdio> #include <cstdlib> #include <cctype> #include <cassert> #include <map> #include <cassert> #include <time.h> #include <ctime> using namespace std; typedef complex<double> xy_t; typedef pair<xy_t, xy_t> line; typedef long long ll; typedef pair<int, int> P; typedef pair<int , P> PP; typedef pair<int, string> Ps; typedef vector<int> vec; typedef vector<vec> mat; const int INF = 1 << 30; const double EPS = 1e-9; const double PI = 3.1415926535897932384626433832795; const int CLK = CLOCKS_PER_SEC; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, m, n) for(int i = m; i < n; i++) bool field[5005][5]; int block(int d, int p, int q){ int x, y; if(d == 1) { x = p; y = 1;} else {y = p; x = 1;} for(int i = 5000; i >= 1; i--){ bool f = false; rep(j, x){ if(field[i-1][q+j]) f = true; } if(f){ rep(j, y){ rep(k, x){ field[i+j][q+k] = true; } } return i; } } rep(i, y)rep(j, x) field[i][q+j] = true; return 0; } void mov(int pos){ rep(i, 5) field[pos][i] = false; rep2(i, pos, 5000)rep(j, 5) swap(field[i+1][j], field[i][j]); } void update(int pos){ int cnt = 0; for(int i = 4; i >= 0; i--){ bool f = true; rep(j, 5){ if(!field[pos+i][j]) f = false; } if(f) mov(pos + i); } } void drop(){ int d, p, q; cin >> d >> p >> q; int pos = block(d, p, --q); update(pos); } int cnt(){ int res = 0; rep(i, 5000)rep(j, 5) if(field[i][j]) res++; return res; } int main(){ int n; while(cin >> n && n){ memset(field, false, sizeof(field)); rep(i, n){ drop(); // cout << i << " " << cnt() << endl; } cout << cnt() << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; inline void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long int solve(long long int a, long long int b, long long int mn, long long int e, long long int h) { if (mn > 0) { long long int s = 0; if (mn >= e * a) { s = e; mn -= e * a; if (mn >= h * b) { s += b; mn -= h * b; } else { s += (mn / b); mn -= (mn / b) * b; } } else { s += (mn / a); mn -= (mn / a) * a; } return s; } return 0; } int main() { int t; cin >> t; while (t--) { long long int n, tm, a, b; cin >> n >> tm >> a >> b; vector<long long int> occ(n), ty(n); vector<pair<long long int, long long int>> oc; long long int mn = tm, mx = 0; long long int e = 0, h = 0; for (int i = 0; i < n; i++) { cin >> ty[i]; if (ty[i] == 0) e++; else h++; } for (int i = 0; i < n; i++) { cin >> occ[i]; mn = min(mn, occ[i]); mx = max(mx, occ[i]); oc.push_back(make_pair(occ[i], ty[i])); } mn -= 1; long long int sum = a * e + b * h; if (sum <= tm) { cout << n << "\n"; continue; } sort(oc.begin(), oc.end()); oc.push_back(make_pair(tm + 1, -1)); long long int mark = 0; if (mn > 0) { long long int s = 0; if (mn >= e * a) { s = e; mn -= e * a; if (mn >= h * b) { s += b; mn -= h * b; } else { s += (mn / b); mn -= (mn / b) * b; } } else { s += (mn / a); mn -= (mn / a) * a; } mark = max(mark, s); } int i = 0; long long int th = 0, tp = 0, th1 = 0; long long int d = 0; while (i < n) { pair<int, int> p = oc[i]; th = p.first; th1 = oc[i + 1].first; tp += ((p.second == 0) ? a : b); if (p.second == 0) e -= 1; else h -= 1; d += 1; if (tp < th1) { th1 -= tp; th1 -= 1; mark = max(mark, d + solve(a, b, th1, e, h)); } i++; } cout << mark << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<vector<int>> x(4, vector<int>(n)); if (k > (n - 2) * 2) cout << "NO"; else { cout << "YES" << endl; for (int i = 0; i < 4; i++) { for (int j = 0; j < n; j++) { if (i == 0 || j == 0 || i == 3 || j == n - 1) x[i][j] = 0; else { if (j != n / 2 && k > 1 && x[i][j] == 0) { x[i][j] = 1; x[i][n - j - 1] = 1; k -= 2; } } } } int i = 1; while (k > 0) { x[i][n / 2] = 1; --k; i++; } for (i = 0; i < 4; i++) { for (int j = 0; j < n; j++) { if (x[i][j] == 0) cout << '.'; else cout << '#'; } cout << endl; } cin >> n; } return (0); }
2
#include <bits/stdc++.h> const int N = 2e3 + 5; int TEST_NUM = 1; using namespace std; void calc(string *res, long double *d, long double x, long double y, long double z) { res[1] = "x^y^z"; d[1] = log(x) * pow(y, z); res[2] = "x^z^y"; d[2] = log(x) * pow(z, y); res[3] = "(x^y)^z"; d[3] = log(x) * y * z; res[4] = "(x^z)^y"; d[4] = log(x) * y * z; res[5] = "y^x^z"; d[5] = log(y) * pow(x, z); res[6] = "y^z^x"; d[6] = log(y) * pow(z, x); res[7] = "(y^x)^z"; d[7] = log(y) * x * z; res[8] = "(y^z)^x"; d[8] = log(y) * x * z; res[9] = "z^x^y"; d[9] = log(z) * pow(x, y); res[10] = "z^y^x"; d[10] = log(z) * pow(y, x); res[11] = "(z^x)^y"; d[11] = log(z) * x * y; res[12] = "(z^y)^x"; d[12] = log(z) * x * y; } int main() { long double x, y, z; string res[15]; long double d[15]; cin >> x >> y >> z; calc(res, d, x, y, z); int mxi = -1; for (int i = 1; i <= 12; i++) { if (mxi == -1 || d[mxi] < d[i]) { mxi = i; } } cout << res[mxi] << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 500000; int n; bool seq[MAXN], goal[MAXN]; vector<int> node[MAXN], ans; void DFS(int curr, int parent, bool cpar, bool opar) { if (seq[curr] ^ goal[curr] != cpar) { cpar = !cpar; ans.push_back(curr); } for (int a = 0; a < node[curr].size(); a++) if (node[curr][a] != parent) DFS(node[curr][a], curr, opar, cpar); } int main() { ios_base::sync_with_stdio(false); scanf("%i", &n); for (int a = 0, b, c; a < n - 1; a++) { scanf("%i%i", &b, &c); node[b - 1].push_back(c - 1); node[c - 1].push_back(b - 1); } for (int a = 0, b; a < n; a++) { scanf("%i", &b); seq[a] = b; } for (int a = 0, b; a < n; a++) { scanf("%i", &b); goal[a] = b; } DFS(0, -1, false, false); printf("%i\n", ans.size()); for (int a = 0; a < ans.size(); a++) printf("%i\n", ans[a] + 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; long long a[200005]; long long k,l,r,mid,ans; long long work(long long a,long long b) { return a / b + (((a ^ b) >= 0) and a % b); } long long _work(long long a, long long b) { return a / b - (((a ^ b) < 0) and a % b); } long long check(long long w){ long long cnt=0,div; for (register int i=1;i<=n;++i){ if (a[i]==0){ if (w>=0) cnt+=n-1; } if (a[i]>0){ div=_work(w,a[i]); int pos=upper_bound(a+1,a+1+n,div)-a; --pos; if (pos>=i) cnt+=pos-1; else cnt+=pos; } if (a[i]<0){ div=work(w,a[i]); int pos=lower_bound(a+1,a+1+n,div)-a; if (pos<=i) cnt+=n-pos; else cnt+=n-pos+1; } } return cnt/2; } int main(){ scanf ("%d%lld",&n,&k); for (register int i=1;i<=n;++i) scanf ("%lld",&a[i]); sort (a+1,a+1+n); l=-1e18;r=1e18; while (l<=r){ mid=l+r>>1; if (check(mid)>=k) ans=mid,r=mid-1; else l=mid+1; } printf ("%lld\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAX = 100000 + 5; const int MAXn = 3005; const int MAXi = 3; vector<char> a; bool res = true; int main() { string b, c; cin >> b >> c; for (int i = 0; i < b.size(); i++) { a.push_back(b[i]); } sort(a.begin(), a.end()); for (int i = 0; i < a.size(); i++) { if (a[i] != '0') { swap(a[i], a[0]); break; } } if (a.size() == c.size()) { for (int i = 0; i < a.size(); i++) { if (a[i] != c[i]) { res = false; break; } } } else { res = false; } if (res) { cout << "OK" << endl; } else { cout << "WRONG_ANSWER " << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, indexsize; vector<int> fac; cin >> n; for (int i = 1; i <= n; ++i) { int x = n / i; if (i > x) { break; } if (n % i == 0) { fac.push_back(i); fac.push_back(x); } } indexsize = fac.size() - 1; cout << fac[indexsize - 1] << endl; cout << fac[indexsize]; return 0; }
1
#include <bits/stdc++.h> using namespace std; int a1234; inline void xxx() { for (;;) ; } inline int rd(int l, int r) { return rand() % (r - l + 1) + l; } const int mxn = 2e5 + 3, mod = 1e9 + 7, mxm = 1e6 + 3; inline long long por(long long x, int y) { long long res = 1; for (; y; y >>= 1) { if (y & 1) res = res * x % mod; x = x * x % mod; } return res; } int n, Q, k; long long jc[mxn], inv[mxn]; int ph[mxm], pr[mxm], pt, bv[mxm]; inline long long C(int x, int y) { if (x < y) return 0; return jc[x] * inv[y] % mod * inv[x - y] % mod; } int cnt[mxm]; long long ans; inline void add(int x) { ans += (C(cnt[x] + 1, k) - C(cnt[x], k)) * ph[x] % mod; ++cnt[x]; } int main() { jc[0] = inv[0] = 1; for (int i = 1; i < mxn; ++i) jc[i] = jc[i - 1] * i % mod; inv[mxn - 1] = por(jc[mxn - 1], mod - 2); for (int i = mxn - 2; i; --i) inv[i] = inv[i + 1] * (i + 1) % mod; ph[1] = 1; for (int i = 2; i < mxm; ++i) { if (!bv[i]) bv[i] = i, pr[++pt] = i, ph[i] = i - 1; for (int j = 1; j <= pt && i * pr[j] < mxm && pr[j] <= bv[i]; ++j) { bv[i * pr[j]] = pr[j]; ph[i * pr[j]] = ph[i] * (pr[j] != bv[i] ? pr[j] - 1 : pr[j]); } } a1234 = scanf("%d%d%d", &n, &k, &Q); for (int T = 1; T <= n + Q; ++T) { int x; a1234 = scanf("%d", &x); for (int i = 1; i * i <= x; ++i) if (x % i == 0) { add(i); if (i * i != x) add(x / i); } if (T > n) ans = (ans % mod + mod) % mod, printf("%lld\n", ans); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MAXFFTBIT = 21; const int MAXFFTN = 1 << MAXFFTBIT; struct Complex { double real, image; Complex(double real = 0, double image = 0) : real(real), image(image) {} Complex operator+(const Complex &rhs) const { return Complex(real + rhs.real, image + rhs.image); } Complex operator-(const Complex &rhs) const { return Complex(real - rhs.real, image - rhs.image); } Complex operator*(const Complex &rhs) const { return Complex(real * rhs.real - image * rhs.image, real * rhs.image + image * rhs.real); } Complex operator*(const double scale) const { return Complex(real * scale, image * scale); } Complex operator/(const double scale) const { return Complex(real / scale, image / scale); } Complex operator*(const int scale) const { return Complex(real * scale, image * scale); } Complex operator/(const int scale) const { return Complex(real / scale, image / scale); } }; void FFT(Complex *a, bool inverse) { for (int mask = 0; mask < MAXFFTN; mask++) { int newMask = 0; for (int i = 0; i < MAXFFTBIT; i++) if (mask & (1 << i)) newMask |= 1 << (MAXFFTBIT - i - 1); if (mask < newMask) swap(a[mask], a[newMask]); } double theta = acos(-1.0); Complex w, mul, u, v; for (int mask = 1; mask < MAXFFTN; mask <<= 1) { w = Complex(cos(theta), sin(theta) * (inverse ? -1 : 1)); for (int i = 0; i < MAXFFTN; i += mask << 1) { mul = Complex(1, 0); for (int j = 0; j < mask; j++) { u = a[i + j]; v = a[i + j + mask] * mul; a[i + j] = u + v; a[i + j + mask] = u - v; mul = mul * w; } } theta /= 2; } if (inverse) for (int i = 0; i < MAXFFTN; i++) a[i] = a[i] / MAXFFTN; } void FFTmultiply(Complex *a, Complex *b, Complex *c) { FFT(a, false); FFT(b, false); for (int i = 0; i < MAXFFTN; i++) c[i] = a[i] * b[i]; FFT(c, true); } void FFTsquare(Complex *a, Complex *b) { FFT(a, false); for (int i = 0; i < MAXFFTN; i++) b[i] = a[i] * a[i]; FFT(b, true); } int n, m; Complex a[MAXFFTN], b[MAXFFTN], c[MAXFFTN]; int main() { scanf("%d%d", &n, &m); for (int i = (0); i < (n); i++) { int x; scanf("%d", &x); a[x] = c[x] = Complex(1, 0); } FFTsquare(a, b); bool fail = false; for (int i = (0); i < (m + 1); i++) { if (c[i].real < 0.5 && b[i].real > 0.5) { fail = true; break; } } if (fail) { puts("NO"); } else { puts("YES"); int res = 0; for (int i = (0); i < (MAXFFTN); i++) { res += c[i].real > 0.5 && b[i].real < 0.5; } printf("%d\n", res); for (int i = (0); i < (MAXFFTN); i++) { if (c[i].real > 0.5 && b[i].real < 0.5) { printf("%d ", i); } } putchar('\n'); } return 0; }
5
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { ll n,k; cin >> n >> k; vector<ll> a(n); for (ll i=0;i<n;i++) cin >> a[i]; sort(a.begin(),a.end()); ll right=ll(1e18)+1; ll left=-right; while (right-left>1) { ll mid=(left+right)/2; ll cnt=0; for (ll i=0;i<n;i++) { if (a[i]<0) { ll ng=-1,ok=n; while (ok-ng>1) { ll m=(ng+ok)/2; if (a[i]*a[m]<mid) ok=m; else ng=m; } cnt+=n-ok; } else { ll ng=-1,ok=n; while (ok-ng>1) { ll m=(ng+ok)/2; if (a[i]*a[m]<mid) ng=m; else ok=m; } cnt+=ok; } if (a[i]*a[i]<mid) cnt--; } if (cnt/2<k) left=mid; else right=mid; } cout << left << endl; }
0
#include <bits/stdc++.h> using namespace std; long long int n; string s; struct st { long long int size; long long int Op; long long int Cl; st() { size = 0; Op = 0; Cl = 0; } }; struct st t[2000001]; struct st combine(struct st x, struct st y) { struct st z; z.size = x.size + y.size; long long int p = min(x.Op, y.Cl); z.size += p; x.Op -= p; y.Cl -= p; z.Op = x.Op + y.Op; z.Cl = x.Cl + y.Cl; return z; } void build() { for (long long int i = 0; i < n; i++) { if (s[i] == '(') t[n + i].Op = 1; else t[n + i].Cl = 1; } for (long long int i = n - 1; i > 0; i--) { t[i] = combine(t[2 * i], t[2 * i + 1]); } } void query(long long int l, long long int r) { l += n; r += n; struct st left, right; while (l < r) { if (l % 2 != 0) { left = combine(left, t[l]); l++; } if (r % 2 != 0) { --r; right = combine(t[r], right); } l = l / 2; r = r / 2; } left = combine(left, right); cout << 2 * left.size << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> s; n = s.size(); build(); long long int m; cin >> m; while (m--) { long long int l, r; cin >> l >> r; query(l - 1, r); } }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7; int a[maxn], dp[maxn], res[maxn]; vector<int> e[maxn], ok[maxn]; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } void dfs(int u, int f) { for (int i = 0; i < (int)e[u].size(); ++i) { int v = e[u][i]; if (v == f) continue; dp[v] = gcd(dp[u], a[v]); ok[v].push_back(dp[u]); for (int j = 0; j < (int)ok[u].size(); ++j) { ok[v].push_back(gcd(a[v], ok[u][j])); } sort(ok[v].begin(), ok[v].end()); ok[v].erase(unique(ok[v].begin(), ok[v].end()), ok[v].end()); dfs(v, u); } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } dp[1] = a[1]; ok[1].push_back(0); dfs(1, 0); for (int i = 1; i <= n; ++i) { res[i] = dp[i]; if (!ok[i].empty()) res[i] = max(res[i], ok[i].back()); } for (int i = 1; i <= n; ++i) printf("%d ", res[i]); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 100005, LN = 20; int L, p[LN][N], d[N], n; long long S, w[N], pre[N]; vector<int> v[N]; int kth(int x, int k) { for (int i = LN - 1; i >= 0; i--) { if (k & (1 << i)) x = p[i][x]; } return x; } void dfs(int u) { for (int v1 : v[u]) { pre[v1] = pre[u] + w[v1]; d[v1] = d[u] + 1; dfs(v1); } } int ans; pair<long long, int> dfs1(int u) { vector<pair<long long, int> > tmp; for (int v1 : v[u]) tmp.push_back(dfs1(v1)); int mx = -1; pair<long long, int> ret; for (auto itr : tmp) { if (itr.second == L) continue; int low = 0, high = d[u]; while (low < high) { int mid = (low + high + 1) >> 1; int x = kth(u, mid); if (pre[u] - pre[p[0][x]] + itr.first > S) high = mid - 1; else low = mid; } int x = kth(u, high); if (pre[u] - pre[p[0][x]] + itr.first <= S) { int curr = min(d[u] - d[x] + 1, L - itr.second); if (curr > mx) { mx = curr; ret = make_pair(itr.first + w[u], itr.second + 1); } } } if ((int)(tmp).size() == 0) { ret = make_pair(w[u], 1); if (u == 1) ans++; return ret; } ans += (int)(tmp).size() - 1; if (u == 1) ans++; if (mx == -1) ans++, ret = make_pair(w[u], 1); return ret; } int main() { cin >> n >> L >> S; for (int i = 1; i <= n; i++) { cin >> w[i]; if (w[i] > S) { cout << -1 << endl; return 0; } } pre[1] = w[1]; for (int i = 2; i <= n; i++) { cin >> p[0][i]; v[p[0][i]].push_back(i); } dfs(1); for (int i = 1; i < LN; i++) { for (int j = 1; j <= n; j++) { p[i][j] = p[i - 1][p[i - 1][j]]; } } dfs1(1); cout << ans << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; unsigned long long int Pow(unsigned long long number, int Power) { if (Power == 0) return 1; if (Power == 1) return number % 1000000007; unsigned long long temp; temp = Pow(number, Power / 2); temp *= temp; if (Power % 2 == 1) temp *= number; return temp % 1000000007; } int main() { ios_base::sync_with_stdio(false); long long n, x, y; cin >> n >> x >> y; if (x == 1 && y > 1 && n <= y) { for (int i = 0; i < n; i++) { cout << 1 << endl; } return 0; } if (n > y) { cout << -1 << endl; return 0; } x -= n - 1; y -= n - 1; if (y * y >= x && y > 0 && x > 0) { for (int i = 0; i < n - 1; i++) { cout << 1 << endl; } cout << y << endl; return 0; } cout << -1 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3 * 1e5 + 10; int a[N]; const int MOD = 1e9 + 7; long long fastMul(long long n, int k) { long long ret = 1; while (k) { if (k & 1) { ret = ret * n % MOD; } n = n * n % MOD; k >>= 1; } return ret; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a, a + n); long long Ans = 0; for (int i = 0; i < n; ++i) { Ans += (fastMul(2, i) - fastMul(2, n - 1 - i) + MOD) % MOD * a[i] % MOD; Ans = Ans % MOD; } printf("%I64d\n", Ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long int t; cin >> t; while (t--) { long int n, k; cin >> n >> k; string s; cin >> s; long int i = 0, ans = 1; i = 0; while (i != k) { long int j = i; char current = s[j]; if (s[j] == '?') { set<char> se; while (j < n) { se.insert(s[j]); j = j + k; } if (se.find('1') != se.end() && se.find('0') != se.end()) { ans = 0; } else if (se.find('1') != se.end()) { j = i; while (j < n) { s[j] = '1'; j = j + k; } } else if (se.find('0') != se.end()) { j = i; while (j < n) { s[j] = '0'; j = j + k; } } } else { while (j < n) { if (s[j] == '?') { s[j] = current; } else if (s[j] == current) { } else { ans = 0; break; } j = j + k; } } if (ans == 0) { break; } i++; } if (ans == 0) { cout << "NO" << endl; } else { i = 0; long int count0 = 0, count1 = 0, count2 = 0; while (i != k) { if (s[i] == '?') { count2++; } else if (s[i] == '1') { count1++; } else { count0++; } i++; } if (count0 <= (k / 2) && count1 <= (k / 2)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, d, h; cin >> n >> d >> h; if (d == h && d == 1 && n > 2) { printf("-1\n"); return 0; } if (d + 1 <= n && 2 * h >= d) { for (int i = 1; i <= h; i++) { printf("%d %d\n", i, i + 1); } if (d != h) { printf("1 %d\n", h + 2); for (int i = 1; i < (d - h); i++) { printf("%d %d\n", h + i + 1, h + i + 2); } int k = n - d - 1; while (k) { printf("%d %d\n", 1, d + 1 + k); k--; } } else { int k = n - h - 1; while (k) { printf("2 %d\n", h + 1 + k); k--; } } } else { printf("-1\n"); } }
2
#include <bits/stdc++.h> using namespace std; int n; double x[100], y[100], z[100]; int main() { scanf("%d", &n); for (int i = 0; i < (n); i++) scanf("%lf%lf%lf", x + i, y + i, z + i); double x0 = 0, y0 = 0, z0 = 0, lambda = 1; for (int _ = 0; _ < (100000); _++) { int i_opt; double d_opt = -1; for (int i = 0; i < (n); i++) { double d = ((x[i] - x0) * (x[i] - x0) + (y[i] - y0) * (y[i] - y0) + (z[i] - z0) * (z[i] - z0)); if (d_opt < d) d_opt = d, i_opt = i; } x0 += lambda * (x[i_opt] - x0); y0 += lambda * (y[i_opt] - y0); z0 += lambda * (z[i_opt] - z0); lambda *= 0.999; } printf("%.9f %.9f %.9f\n", x0, y0, z0); return 0; }
5
#include <bits/stdc++.h> const long long MAX_N = 1e6 + 10; const long long MOD = 1e9 + 7; using namespace std; int a[MAX_N]; int dp[MAX_N]; int main() { int n; string s; cin >> n >> s; if (n & 1) return cout << 0, 0; for (int i = 1; i <= n; ++i) { a[i] = a[i - 1]; a[i] += (s[i - 1] == '('); a[i] -= (s[i - 1] == ')'); } dp[n] = a[n]; for (int i = n - 1; i > 0; --i) dp[i] = min(dp[i + 1], a[i]); long long ans = 0; if (s[n - 1] == '(') { s[n - 1] = ')'; int now = 0; for (int i = 0; i < n; ++i) { now += (s[i] == '('); now -= (s[i] == ')'); if (now < 0) return cout << 0, 0; } return cout << (!now), 0; } if (s[0] == ')') { s[0] = '('; int now = 0; for (int i = 0; i < n; ++i) { now += (s[i] == '('); now -= (s[i] == ')'); if (now < 0) return cout << 0, 0; } return cout << (!now), 0; } for (int i = 2; i < n; ++i) { if (s[i - 1] == '(') { if (dp[i] >= 2 and a[n] == 2) ans++; } else { if (dp[i] >= -2 and a[n] == -2) ans++; } if (a[i] < 0) break; } cout << ans; return 0; }
5
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <string> #include <vector> using namespace std; #define INF (1<<29) #define EPS (1e-10) #define mp make_pair #define pb push_back typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef pair<int,int> pii; int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0}; int main(){ int n, m; cin >> n >> m; vector<pii> treasure; for( int i = 0; i < n; i++ ){ int x, y; //cin >> x >> y; scanf("%d %d", &x, &y); treasure.pb( mp(x,y) ); } sort(treasure.begin(), treasure.end()); for( int i = 0; i < m; i++ ){ int x1, y1, x2, y2; //cin >> x1 >> y1 >> x2 >> y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); // search int ans = 0; vector<pii>::iterator it = lower_bound(treasure.begin(),treasure.end(),mp(x1,y1)); while( 1 ){ if( it == treasure.end() || *it > mp(x2,y2) ) break; if( (*it).first > x2 ) it = upper_bound(treasure.begin(), treasure.end(),mp(-INF,(*it).second+1)); if( x1 <= (*it).first && (*it).first <= x2 && y1 <= (*it).second && (*it).second <= y2 ) ans++; it++; } //cout << ans << endl; printf("%d\n", ans); } }
0
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (q < m) { m = q; return true; } else return false; } void solve() { int N; cin >> N; int X; cin >> X; vector<int> a(N); cin >> a; unordered_map<int, int> m; for (auto v : a) m[v] = 1; int i = 1; while (i <= 100 && X >= 0) { if (m.count(i) == 0) { --X; } ++i; } --i; cout << i + X << endl; } int main() { int T; cin >> T; while (T--) { solve(); } return 0; }
1
#include <bits/stdc++.h> const int N = 5005, djq = 998244353; int n, f[N][N], fac[N]; int main() { std::cin >> n; f[1][0] = fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * (n - i + 1) % djq; for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) { f[i + 1][j] = (1ll * (j + 1) * f[i][j] + f[i + 1][j]) % djq; f[i + 1][j + 1] = (1ll * (i - j) * f[i][j] + f[i + 1][j + 1]) % djq; } for (int k = 1; k <= n; k++) { int ans = 0; for (int i = k; i <= n; i++) ans = (1ll * f[i][k - 1] * fac[n - i] + ans) % djq; printf("%d ", ans); } return puts(""), 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 20, OFF = 4e5 + 10, OFF2 = 9e5; const long long inf = 1e18; int n, s; vector<pair<int, int> > g[N]; long long dist[N]; void build(int v, int i, int j) { if (i == j) { g[v].push_back({OFF2 + i, 0}); g[OFF2 + i].push_back({OFF + v, 0}); return; } int mid = i + j >> 1; build(v * 2, i, mid); build(v * 2 + 1, mid + 1, j); g[v].push_back({v * 2, 0}); g[v].push_back({v * 2 + 1, 0}); g[OFF + v * 2].push_back({OFF + v, 0}); g[OFF + v * 2 + 1].push_back({OFF + v, 0}); } void upd1(int v, int i, int j, int& l, int& r, int& node, int& w) { if (i > r or j < l) return; if (i >= l and j <= r) { g[OFF2 + node].push_back({v, w}); return; } int mid = i + j >> 1; upd1(v * 2, i, mid, l, r, node, w); upd1(v * 2 + 1, mid + 1, j, l, r, node, w); } void upd2(int v, int i, int j, int& l, int& r, int& node, int& w) { if (i > r or j < l) return; if (i >= l and j <= r) { g[OFF + v].push_back({OFF2 + node, w}); return; } int mid = i + j >> 1; upd2(v * 2, i, mid, l, r, node, w); upd2(v * 2 + 1, mid + 1, j, l, r, node, w); } void solve() { set<pair<long long, int> > heap; for (int i = 1; i < N; i++) dist[i] = inf; dist[OFF2 + s] = 0; heap.insert({dist[OFF2 + s], OFF2 + s}); int node; long long cost; while (!heap.empty()) { cost = (*heap.begin()).first; node = heap.begin()->second; heap.erase(heap.begin()); for (auto grp : g[node]) { long long cst = grp.second + cost; if (cst < dist[grp.first]) { pair<long long, int> to_find = {dist[grp.first], grp.first}; if (heap.find(to_find) != heap.end()) heap.erase(to_find); dist[grp.first] = cst; heap.insert({cst, grp.first}); } } } } int main() { int q, qt, tail, head, u, v, w; scanf("%d %d %d", &n, &q, &s); build(1, 1, n); while (q--) { scanf("%d", &qt); if (qt == 1) { scanf("%d %d %d", &tail, &head, &w); g[OFF2 + tail].push_back({OFF2 + head, w}); } else { scanf("%d %d %d %d", &tail, &u, &v, &w); if (qt == 2) upd1(1, 1, n, u, v, tail, w); else upd2(1, 1, n, u, v, tail, w); } } solve(); long long cst; for (int i = 1; i <= n; i++) { cst = dist[i + OFF2]; if (cst >= inf) printf("-1 "); else printf("%lld ", cst); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 2005; int a[maxn]; map<int, int> mp1, mp2; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; int ans = INT_MAX; for (int i = 0; i <= n; i++) { if (i != 0) { mp1[a[i]]++; if (mp1[a[i]] >= 2) break; } mp2 = mp1; for (int j = n; j >= i; j--) { mp2[a[j]]++; if (mp2[a[j]] >= 2) { ans = min(ans, j - i); break; } } } if (ans == INT_MAX) cout << 0 << endl; else cout << ans << endl; return 0; }
2