func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int n, m; struct Node { long long x, y; } Edge[300005]; long long pre[300005]; bool cmp(Node a, Node b) { return a.y > b.y; } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) { scanf( %lld%lld , &Edge[i].x, &Edge[i].y); } sort(Edge + 1, Edge + n + 1, cmp); priority_queue<int, vector<int>, greater<int>> q; long long ans = 0; for (int i = 1; i <= n; i++) { if (q.size() < m) { q.push(Edge[i].x); pre[i] = pre[i - 1] + Edge[i].x; ans = max(ans, Edge[i].y * pre[i]); } else { if (Edge[i].x <= q.top()) { pre[i] = pre[i - 1]; } else { pre[i] = pre[i - 1] - q.top() + Edge[i].x; q.pop(); q.push(Edge[i].x); ans = max(ans, Edge[i].y * pre[i]); } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char x, y, t; struct node { char c; int p; } ans[4]; inline bool cmp(const node &a, const node &b) { return a.p < b.p; } int main() { for (register int i = 1; i <= 3; i++) { ans[i].c = A + i - 1; cin >> x >> t >> y; if (t == > ) swap(x, y); ans[x - A + 1].p++; ans[y - A + 1].p += 2; } sort(ans + 1, ans + 4, cmp); if (ans[1].p == ans[2].p || ans[2].p == ans[3].p) { cout << Impossible ; return 0; } cout << ans[1].c << ans[2].c << ans[3].c; return 0; }
#include <bits/stdc++.h> using namespace std; const int MX = 1e6 + 5; long long ans[MX]; long long upd[MX]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, w; cin >> n >> w; vector<int> a[n]; int mx[n]; for (int i = 0; i < n; i++) { int l; cin >> l; a[i] = vector<int>(l); mx[i] = -1000000000; for (int j = 0; j < l; j++) { cin >> a[i][j]; mx[i] = max(mx[i], a[i][j]); } } memset(ans, 0, sizeof(ans)); for (int i = 0; i < n; i++) { if (w >= 2 * a[i].size() + 1) { int currmx = 0; for (int j = 0; j < a[i].size(); j++) { currmx = max(currmx, a[i][j]); ans[j + 1] += currmx; } currmx = 0; for (int j = a[i].size() - 1; j >= 0; j--) { currmx = max(currmx, a[i][j]); ans[w - ((a[i].size() - 1) - j)] += currmx; } upd[a[i].size()] -= max(mx[i], 0); upd[w - a[i].size()] += max(mx[i], 0); } else { deque<int> q; int idx = 0; bool zero = false; for (int j = 1; j <= w; j++) { if (idx < a[i].size()) { while (!q.empty() && a[i][q.back()] <= a[i][idx]) { q.pop_back(); } q.push_back(idx++); } zero = (a[i].size() <= (j - 1)) || (a[i].size() <= (w - j)); if (zero) ans[j] += max(a[i][q.front()], 0); else ans[j] += a[i][q.front()]; if (((a[i].size() - q.front()) + j) >= w + 1) { q.pop_front(); } } } } for (int i = w; i >= 1; i--) { upd[i] += upd[i + 1]; } for (int i = 1; i <= w; i++) cout << ans[i] + upd[i] << n ; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string a; string b; cin >> a >> b; int order = 0; while (a[order] == 0 ) { order++; } a.erase(0, order); order = 0; while (b[order] == 0 ) { order++; } b.erase(0, order); if (a.size() > b.size()) { cout << > ; } else if (a.size() < b.size()) { cout << < ; } else { for (int i = 0; i < a.size(); i++) { if (a[i] > b[i]) { cout << > ; return 0; } else if (a[i] < b[i]) { cout << < ; return 0; } } cout << = ; } }
#include <bits/stdc++.h> unsigned long long tmp[131073], invn; int a_[131072]; inline int ksm(unsigned long long a, int b) { int ans = 1; while (b) (b & 1) && (ans = a * ans % 998244353), a = a * a % 998244353, b >>= 1; return ans; } void init(int n) { for (int i = 1; i < n; i++) a_[i] = i & 1 ? a_[i ^ 1] | n >> 1 : a_[i >> 1] >> 1; for (int i = tmp[0] = 1, j = ksm(3, (998244353 - 1) / n); i <= n; i++) tmp[i] = tmp[i - 1] * j % 998244353; invn = ksm(n, 998244353 - 2); } void ntt(int a[], int n, bool typ) { int p; for (int i = 1; i < n; i++) if (a_[i] < i) p = a[i], a[i] = a[a_[i]], a[a_[i]] = p; if (typ) for (int i = 1, d = n >> 1; d; i <<= 1, d >>= 1) for (int j = 0; j < n; j += i << 1) for (int k = 0; k < i; k++) p = tmp[n - d * k] * a[i + j + k] % 998244353, (a[i + j + k] = a[j + k] + 998244353 - p) >= 998244353 && (a[i + j + k] -= 998244353), (a[j + k] += p) >= 998244353 && (a[j + k] -= 998244353); else for (int i = 1, d = n >> 1; d; i <<= 1, d >>= 1) for (int j = 0; j < n; j += i << 1) for (int k = 0; k < i; k++) p = tmp[d * k] * a[i + j + k] % 998244353, (a[i + j + k] = a[j + k] + 998244353 - p) >= 998244353 && (a[i + j + k] -= 998244353), (a[j + k] += p) >= 998244353 && (a[j + k] -= 998244353); if (typ) for (int i = 0; i < n; i++) a[i] = invn * a[i] % 998244353; } void getinv(int n, int a[], int b[]) { static int tmp[131072]; memset(b, 0, sizeof(int) * (n << 1)); b[0] = ksm(a[0], 998244353 - 2); for (int i = 1; i < n; i <<= 1) { init(i << 2); memset(tmp, 0, sizeof(int) * (i << 2)); memcpy(tmp, a, sizeof(int) * (i << 1)); ntt(tmp, i << 2, 0); ntt(b, i << 2, 0); for (int j = 0; j < i << 2; j++) b[j] = (1ull * (998244353 - tmp[j]) * b[j] % 998244353 + 2) * b[j] % 998244353; ntt(b, i << 2, 1); memset(b + (i << 1), 0, sizeof(int) * (i << 1)); } } int n, m, a[131072], b[131072], d0[131072], d1[131072], len, ans; void work(int v) { if (v == 1) { for (int i = 0; i < len; i++) a[i] = 1; ans = 1; return; } work(v >> 1); memset(d0, 0, sizeof(int) * (len << 1)); memset(d1, 0, sizeof(int) * (len << 1)); for (int i = 0; i < len; i++) (i & 1 ? d1 : d0)[i] = a[i]; if (v & 1) ++d1[1] == 998244353 && (d1[1] = 0); memset(a, 0, sizeof(int) * (len << 1)); for (int i = 0; i < len; i++) a[i] = d1[i] ? 998244353 - d1[i] : 0; ++a[0] == 998244353 && (a[0] = 0); getinv(len, a, b); if (v == m || !(n & 1)) { int Ans = 0; for (int i = 1; i <= n; i += 2) Ans = (1ull * d1[i] * b[n - i] % 998244353 * i + Ans) % 998244353; if (!(n & 1)) ans = (2ull * ans + Ans) % 998244353; else ans = Ans; if (v == m) return; } init(len << 1); ntt(d0, len << 1, 0); for (int i = 0; i < len << 1; i++) d0[i] = 1ull * d0[i] * d0[i] % 998244353; ntt(d0, len << 1, 1); memset(d0 + len, 0, sizeof(int) * len); ntt(d0, len << 1, 0); ntt(b, len << 1, 0); for (int i = 0; i < len << 1; i++) a[i] = 1ull * d0[i] * b[i] % 998244353; ntt(a, len << 1, 1); for (int i = 1; i < len; i += 2) (a[i] += d1[i]) >= 998244353 && (a[i] -= 998244353); if (v & 1) a[1] ? a[1]-- : a[1] = 998244353 - 1; } int main() { scanf( %d%d , &n, &m); len = n << 1; while (len ^ len & -len) len ^= len & -len; work(m); printf( %d n , ans); }
#include <bits/stdc++.h> using i8 = std::int8_t; using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using u8 = std::uint8_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t; auto SolveTest() -> void { int n; std::cin >> n; std::vector<int> a(n); for (auto &elem : a) { std::cin >> elem; } std::set<int> numbers(std::begin(a), std::end(a)); for (auto it = std::begin(numbers); std::next(it) != std::end(numbers); ++it) { if (std::abs(*it - *std::next(it)) > 1) { std::cout << NO n ; return; } } std::cout << YES n ; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); i32 test_count; std::cin >> test_count; for (i32 t = 0; t < test_count; ++t) { SolveTest(); } }
#include <bits/stdc++.h> using namespace std; int n; unordered_map<int, int> mask, f; int SG(int s) { if (f.count(s)) return f[s]; bool t[30] = {0}; for (int i = 0; s >> i; i++) { t[SG(s >> (i + 1) | (s & ((1 << i) - 1)))] = 1; } for (int i = 0; i < 30; i++) if (!t[i]) return f[s] = i; return 30; } int main() { scanf( %d , &n); for (int i = 1, x; i <= n; i++) { scanf( %d , &x); for (int d = 2; d * d <= x; d++) if (x % d == 0) { int c = 0; while (x % d == 0) x /= d, c++; mask[d] |= 1 << (c - 1); } if (x > 1) mask[x] |= 1; } int ans = 0; for (auto s : mask) ans ^= SG(s.second); puts(ans ? Mojtaba : Arpa ); return 0; }
//Author: AnandRaj uux #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vpii; typedef pair<ll,ll> pll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pll> vpll; #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define test() int t;cin>>t;while(t--) #define all(v) v.begin(),v.end() #define prinp(p) cout<<p.first<< <<p.second<<endl #define prinv(V) for(auto v:V) cout<<v<< ;cout<<endl #define take(V,f,n) for(int in=f;in<f+n;in++) cin>>V[in] #define what(x) cerr<<#x<< = <<x<<endl #define KStest() int t,t1;cin>>t;t1=t;while(t--) #define KScout cout<< Case # <<t1-t<< : mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1e9+7,MAX = 1e6+5; const ll INF = 1e18+5; vvi A; bool can = true; vi TS; vi vis,inStack; void dfs(int x, int parent) { vis[x] = true; inStack[x] = true; for(auto v:A[x]) { if (vis[v] && inStack[v]) { can = false; return; } if(!vis[v]) { dfs(v, x); } } inStack[x] = false; TS.push_back(x); } int main() { int n,m,k; cin>>n>>m>>k; A.resize(n+1); vector<string> P(n+1); map<string,int> Sp; take(P,1,n); for(int i=1;i<=n;i++) Sp[P[i]] = i; vis.resize(n+1); inStack.resize(n+1); vector<int> MT(m+1); vector<string> S(m+1); for(int i=1;i<=m;i++) { cin>>S[i]>>MT[i]; set<string> X; X.insert( ); for(int j=0;j<k;j++) { set<string> XX; for(auto x:X) { XX.insert(x+ _ ); XX.insert(x+S[i][j]); } X = XX; } if(X.count(P[MT[i]])==0) {can = false;break;} for(auto x:X) { if(Sp.find(x)==Sp.end() || Sp[x]==MT[i]) continue; else { A[MT[i]].push_back(Sp[x]); } } } // for(int i=1;i<=n;i++) // { // prinv(A[i]); // } for(int i=1;i<=n;i++) { if(!vis[i]) dfs(i,-1); } if(can) { cout<< YES <<endl; reverse(all(TS)); prinv(TS); } else cout<< NO <<endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5; inline long long rd() { long long x = 0; char c = getchar(); int neg = 1; while (c < 0 || c > 9 ) { if (c == - ) neg = -1; c = getchar(); } while (c >= 0 && c <= 9 ) x = x * 10 + c - 0 , c = getchar(); return x * neg; } pair<int, int> p[maxn]; int main() { int i, j, k; for (i = 1; i <= 3; i++) p[i].first = rd(), p[i].second = rd(); sort(p + 1, p + 4); if (p[1].second <= p[2].second && p[1].second <= p[3].second) { if (p[2].second <= p[3].second) { int ans = p[2].second - p[1].second + p[3].first - p[1].first + p[3].second - p[2].second + 1; printf( %d n , ans); for (i = p[1].second; i <= p[2].second; i++) printf( %d %d n , p[1].first, i); for (i = p[1].first + 1; i <= p[3].first; i++) printf( %d %d n , i, p[2].second); for (i = p[2].second + 1; i <= p[3].second; i++) printf( %d %d n , p[3].first, i); } else { int ans = p[3].second - p[1].second + p[3].first - p[1].first + p[2].second - p[3].second + 1; printf( %d n , ans); for (i = p[1].second; i <= p[3].second; i++) printf( %d %d n , p[1].first, i); for (i = p[1].first + 1; i <= p[3].first; i++) printf( %d %d n , i, p[3].second); for (i = p[3].second + 1; i <= p[2].second; i++) printf( %d %d n , p[2].first, i); } } else if (p[1].second <= p[2].second && p[1].second > p[3].second) { int ans = p[1].second - p[3].second + p[3].first - p[1].first + p[2].second - p[1].second + 1; printf( %d n , ans); for (i = p[3].second; i <= p[1].second; i++) printf( %d %d n , p[3].first, i); for (i = p[1].first; i <= p[3].first - 1; i++) printf( %d %d n , i, p[1].second); for (i = p[1].second + 1; i <= p[2].second; i++) printf( %d %d n , p[2].first, i); } else if (p[1].second > p[2].second && p[1].second <= p[3].second) { int ans = p[3].second - p[1].second + p[3].first - p[1].first + p[1].second - p[2].second + 1; printf( %d n , ans); for (i = p[1].second; i <= p[3].second; i++) printf( %d %d n , p[3].first, i); for (i = p[1].first; i <= p[3].first - 1; i++) printf( %d %d n , i, p[1].second); for (i = p[2].second; i <= p[1].second - 1; i++) printf( %d %d n , p[2].first, i); } else if (p[1].second > p[2].second && p[1].second > p[3].second) { if (p[2].second <= p[3].second) { int ans = p[1].second - p[2].second + p[2].first - p[1].first + p[3].first - p[2].first + 1; printf( %d n , ans); for (i = p[1].first; i <= p[3].first; i++) printf( %d %d n , i, p[3].second); for (i = p[2].second; i <= p[3].second - 1; i++) printf( %d %d n , p[2].first, i); for (i = p[3].second + 1; i <= p[1].second; i++) printf( %d %d n , p[1].first, i); } else { int ans = p[3].first - p[1].first + p[1].second - p[3].second + 1; printf( %d n , ans); for (i = p[2].second; i <= p[1].second; i++) printf( %d %d n , p[1].first, i); for (i = p[1].first + 1; i <= p[3].first; i++) printf( %d %d n , i, p[2].second); for (i = p[3].second; i <= p[2].second - 1; i++) printf( %d %d n , p[3].first, i); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 405; int n, kk; vector<pair<int, int>> v[N][N], e[N]; int dp[N][N], sz[N], f[N]; void dfs1(int pos, int fa) { sz[pos] = 1; for (auto &i : e[pos]) if (i.first != fa) { dfs1(i.first, pos); sz[pos] += sz[i.first]; f[i.first] = i.second; } } void dfs2(int pos, int fa) { if (pos == 1) dp[pos][sz[pos]] = 0; else dp[pos][sz[pos]] = 1; dp[pos][0] = 0; for (auto &i : e[pos]) { if (i.first == fa) continue; dfs2(i.first, pos); for (int j = sz[pos] - 1; j > 0; --j) { int tk = -1; for (int k = 0; k <= sz[i.first] && k <= j; k++) if (dp[pos][j] > dp[i.first][k] + dp[pos][j - k]) { dp[pos][j] = dp[i.first][k] + dp[pos][j - k], tk = k; v[pos][j] = v[pos][j - tk], v[pos][j].push_back({i.first, tk}); } if (tk != -1) v[pos][j] = v[pos][j - tk], v[pos][j].push_back({i.first, tk}); } } } void print_ans(int pos, int cnt) { if (sz[pos] == cnt) { cout << f[pos] << ; return; } for (auto &i : v[pos][cnt]) print_ans(i.first, i.second); } int main() { ios::sync_with_stdio(false); cin >> n >> kk; cin.tie(0); for (int i = 1; i < n; i++) { int t1, t2; cin >> t1 >> t2; e[t1].push_back({t2, i}); e[t2].push_back({t1, i}); } memset(dp, 63, sizeof(dp)); dfs1(1, 0); dfs2(1, 0); int ans = dp[1][n - kk], pos = 1; for (int i = 2; i <= n; i++) if (sz[i] >= kk && ans > dp[i][sz[i] - kk] + 1) ans = dp[i][sz[i] - kk] + 1, pos = i; cout << ans << endl; if (pos != 1) cout << f[pos] << endl; print_ans(pos, sz[pos] - kk); return 0; }
#include <bits/stdc++.h> using namespace std; int c3[100010], c2[1010]; int graph[110][110]; int main() { ios ::sync_with_stdio(0); cin.tie(0); memset(graph, 0, sizeof graph); for (int i = (3), _b = (100); i <= _b; ++i) { c3[i] = (i * (i - 1) * (i - 2)) / 6; } for (int i = (2), _b = (100); i <= _b; ++i) { c2[i] = (i * (i - 1)) / 2; } int k, esco; cin >> k; for (int i = (100), _b = (3); i >= _b; --i) { if (k >= c3[i]) { k -= c3[i]; esco = i; break; } } for (int i = 0; i < (int)(esco); ++i) { for (int j = 0; j < (int)(esco); ++j) { if (i != j) graph[i][j] = graph[j][i] = 1; } } int fixo = esco; while (k > 0) { int cont = 0; for (int i = (fixo), _b = (2); i >= _b; --i) { cont = i; if (k >= c2[i]) break; } for (int i = 0; i < (int)(cont); ++i) graph[i][esco] = graph[esco][i] = 1; k -= c2[cont]; esco++; } cout << esco << endl; for (int i = 0; i < (int)(esco); ++i) { for (int j = 0; j < (int)(esco); ++j) { cout << graph[i][j] << ; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout << setprecision(10); int row, column; char temp; cin >> row >> column; for (int i = 0; i < int(row - 1); ++i) { for (int j = 0; j < int(column); ++j) { cin >> temp; } } string s; cin >> s; int s1 = 0; for (int i = 0; i < int(column); ++i) { if (s[i] == B ) { s1++; while (s[i] == B ) i++; } } cout << s1; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; int test[maxn]; int main() { int n, num(0), x; cin >> n; char ch; int l = 2e5; int r = l + 1; int mi, ma; for (int i = 0; i < n; i++) { cin >> ch >> x; if (ch == L ) { test[x] = l; num++; l--; mi = test[x]; } else if (ch == R ) { test[x] = r; num++; r++; ma = test[x]; } else if (ch == ? ) { int temp = test[x]; int ans = min(temp - mi, r - 1 - temp); cout << ans << endl; } } }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 2; int len, ans = 0, maxdis[N]; vector<int> adj[N], cur; void dfs(int x, int p) { if (adj[x].size() == 1) { maxdis[x] = 0; return; } int i, idxmin1; for (i = 0; i < adj[x].size(); i++) { if (adj[x][i] != p) { dfs(adj[x][i], x); } } cur.clear(); for (i = 0; i < adj[x].size(); i++) { if (adj[x][i] != p) { cur.push_back(maxdis[adj[x][i]] + 1); } } sort(cur.begin(), cur.end()); for (i = 0; i < cur.size() - 1; i++) { if (cur[i] + cur[i + 1] > len) { break; } } ans += cur.size() - 1 - i; maxdis[x] = cur[i]; } int main() { ios::sync_with_stdio(0); int n, i, j, k, l = 0; cin >> n >> len; for (i = 1; i < n; i++) { cin >> j >> k; adj[j].push_back(k); adj[k].push_back(j); } for (i = 1; i <= n; i++) { if (adj[i].size() > 1) { l = i; break; } } dfs(l, l); cout << ans + 1; }
#include <bits/stdc++.h> using namespace std; struct node { int u , v , w; node(){} node(int u,int v,int w): u(u) , v(v) , w(w){}; bool operator < (const node &o) const { return w < o.w; } }; struct dsu { int par[200010]; void init() { for (int i = 1 ; i <= 200000 ; ++i) par[i] = i; } int fin(int u) { if (u != par[u]) par[u] = fin(par[u]); return par[u]; } bool join(int u , int v) { u = fin(u); v = fin(v); if (u == v) return false; if (par[u] > par[v]) swap(u , v); par[u] = v; return true; } } a , b; set < int > vertex; vector < int > g[200010]; vector < node > edge; long long rest; void DFS(int u) { vertex.erase(u); sort(g[u].begin(),g[u].end()); int v = 0; while (1) { auto it = vertex.lower_bound(v); if (it == vertex.end()) return; v = *it; if (!binary_search(g[u].begin(),g[u].end(),v)) { a.join(u , v); DFS(v); rest--; } v++; } } int main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); long long n , m; int x = 0; cin >> n >> m; rest = n * (n - 1) / 2 - m; for (int i = 1 ; i <= m ; ++i) { int u , v , w; cin >> u >> v >> w; x ^= w; g[u].push_back(v); g[v].push_back(u); edge.emplace_back(u , v , w); } a.init();b.init(); for (int i = 1 ; i <= n ; ++i) vertex.insert(i); for (int i = 1 ; i <= n ; ++i) if (vertex.count(i)) DFS(i); if (rest > 0) x = 0; int res = 0; sort(edge.begin(),edge.end()); for (auto p : edge) { int u = p.u , v = p.v , w = p.w; if (a.join(u , v)) { res += w; b.join(u , v); } else if (b.join(u , v)) x = min(x , w); } cout << res + x; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N]; vector<int> v; int main() { int n, s; scanf( %d%d , &n, &s); int ans = 0; for (int i = 1; i <= n; i++) scanf( %d , &a[i]); if (a[s] != 0) ans++; for (int i = 1; i <= n; i++) if (i == s) continue; else v.push_back(a[i]); v.push_back(0); sort(v.begin(), v.end()); int cnt = 0; int idx = 0; while (v[idx] == 0 && idx < n) cnt++, idx++; ans += cnt; cnt--; ans--; int last = n - 1; for (int i = idx; i <= last; i++) { if (i == 0) continue; if (v[i] == v[i - 1]) continue; if (v[i - 1] == v[i] - 1) continue; int diff = v[i] - v[i - 1] - 1; if (diff <= cnt) { cnt -= diff; continue; } diff -= cnt; cnt = 0; while (diff--) { if (last >= i) { last--; ans++; continue; } if (last <= i) break; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int base = 998244353; int sum(int a, int b) { int c = a + b; if (c >= base) return c - base; return c; } int diff(int a, int b) { int c = a - b; if (c < 0) return c + base; return c; } int mul(int a, int b) { return (int)(a * (long long)b % base); } int pow(int a, int b) { if (!b) return 1; if (b == 1) return a; int d = pow(mul(a, a), b / 2); if (b % 2) return mul(d, a); return d; } int divis(int a, int b) { return mul(a, pow(b, base - 2)); } struct segment_tree { public: segment_tree() : ts(0) {} explicit segment_tree(vector<int> &p) : ts(1) { size_t n = p.size(); while (ts < n) { ts <<= 1u; } st.resize(2 * ts); for (int i = ts; i < ts + n; ++i) { st[i] = p[i - ts]; } for (int i = (int)ts - 1; i; --i) { st[i] = mul(st[i * 2], st[2 * i + 1]); } } int get_mul(int l, int r) { l += ts; r += (int)ts - 1; int res = 1; while (l <= r) { if (l % 2) { res = mul(res, st[l++]); } if (r % 2 == 0) { res = mul(res, st[r--]); } l /= 2; r /= 2; } return res; } private: size_t ts; vector<int> st; }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; vector<int> p(n); for (auto &x : p) { cin >> x; } segment_tree st(p); vector<int> deg100(n + 1), pref(n + 1); deg100[0] = pref[0] = 1; for (int i = 1; i <= n; ++i) { deg100[i] = mul(100, deg100[i - 1]); } for (int i = 1; i <= n; ++i) { pref[i] = mul(pref[i - 1], p[i - 1]); } vector<int> dot_product(n); dot_product[0] = 100; for (int i = 1; i < n; ++i) { dot_product[i] = mul(100, sum(dot_product[i - 1], pref[i])); } int ans = divis(dot_product.back(), pref[n]); set<pair<int, int>> ind = {{0, ans}, {n, 0}}; function<int(int, int)> func = [&](int l, int r) { if (r - l == 1) { return 100; } int res = dot_product[r - 1]; int a = 0; if (l) { a = mul(deg100[r - l], dot_product[l - 1]); } res = diff(res, a); return divis(res, pref[l]); }; while (q--) { int i; cin >> i; --i; auto v = ind.lower_bound({i, 0}); if (v->first != i) { int r = v->first; --v; int l = v->first; ans = diff(ans, v->second); int fst = divis(func(l, i), st.get_mul(l, i)); int snd = divis(func(i, r), st.get_mul(i, r)); ind.insert({v->first, fst}); ind.erase(v); ind.insert({i, snd}); ans = sum(ans, fst); ans = sum(ans, snd); } else { auto u = prev(v); ans = diff(ans, v->second); ans = diff(ans, u->second); ind.erase(v); int l = u->first, r = next(u)->first; int val = divis(func(l, r), st.get_mul(l, r)); ans = sum(ans, val); ind.insert({u->first, val}); ind.erase(u); } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long m = 1e9 + 7; long long c[4010][4010]; long long dp[4010]; int main() { c[0][1] = 0; for (int i = 0; i <= 4000; i++) c[i][0] = 1; for (int i = 1; i <= 4000; i++) { for (int j = 1; j < i + 2; j++) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % m; } dp[0] = 1; for (int i = 1; i <= 4000; i++) { dp[i] = 0; for (int j = 0; j < i; j++) { dp[i] = (dp[i] + dp[i - j - 1] * c[i - 1][j]) % m; } } int n; cin >> n; long long ans = 0; for (int i = 0; i < n; i++) ans = (ans + dp[i] * c[n][i]) % m; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, p; long long int cost; bool na; string line; vector<int> a, b; pair<int, int> t; priority_queue<pair<int, int> > q; getline(cin, line); n = line.length(); a.resize(n); b.resize(n); m = 0; for (int i = 0; i < n; i++) { if (line[i] == ? ) { cin >> a[i] >> b[i]; ++m; } } p = 0; cost = 0LL; if (n & 1) { na = true; } else { na = false; for (int i = 0; i < n; i++) { if (line[i] == ( ) { ++p; } else if (line[i] == ) ) { --p; } else { --p; q.push(make_pair(b[i] - a[i], i)); line[i] = ) ; cost += b[i]; } if (p < 0) { if (q.empty()) { na = true; break; } t = q.top(); q.pop(); cost -= t.first; line[t.second] = ( ; p += 2; } } if (p) { na = true; } } if (na) { cout << -1 << endl; } else { cout << cost << endl; cout << line << endl; } return 0; }
#include<cstdio> #include<iostream> #include<queue> #include<algorithm> #include<cstring> #include<set> #include<vector> #include<cmath> #define PII pair<int,int> #define pb push_back #define ep emplace_back #define mp make_pair #define fi first #define se second using namespace std; inline int read(){ int x=0,f=1;char c=getchar(); while(c< 0 ||c> 9 ){ if(c== - ) f=-1; c=getchar();} while(c>= 0 &&c<= 9 ){x=(x<<3)+(x<<1)+(c^48);c=getchar();} return f==1?x:~x+1; } inline void print(int x){ if(x<0) putchar( - ),x=~x+1; if(x>=10) print(x/10); putchar((x%10)|48); } int n; int a[100010]; char str[10]; bool add,fminus,times; char ans[100010]; const int mxn=131072; void solve(int l,int r){ if(l>r) return ; int sum=1; int cnt=0; vector<int>pos; pos.pb(l-1); for(int i=l;i<=r;++i){ if(sum<=mxn) sum*=a[i]; if(a[i]>1){ ++cnt; pos.pb(i); } } ++cnt;pos.pb(r+1); if(sum>mxn){ for(int i=l;i<r;++i) ans[i]= + ; for(int i=pos[1];i<pos[cnt-1];++i) ans[i]= * ; return ; } vector<int>dp(cnt+1),pre(cnt+1);dp[0]=0; for(int i=0;i<cnt;++i){ int s=dp[i]+pos[i+1]-pos[i]-1,t=1; for(int j=i+1;j<=cnt;++j){ t=t*a[pos[j]]; int r=s+t; if(r>dp[j]){ pre[j]=i;dp[j]=r; } } } int ps=cnt; while(ps){ int t=ps; ps=pre[ps]; for(int i=max(l,pos[ps]);i<pos[ps+1];++i) ans[i]= + ; for(int i=pos[ps+1];i<pos[t];++i) ans[i]= * ; } } int main(){ n=read(); for(int i=1;i<=n;++i) a[i]=read(); scanf( %s ,str+1); for(int i=1;i<=strlen(str+1);++i){ switch(str[i]){ case + :{add=1; break;} case - :fminus=1;break ; case * :times=1;break ; } } if(!times){ if(add) for(int i=1;i<n;++i) printf( %d+ ,a[i]); else for(int i=1;i<n;++i) printf( %d- ,a[i]);printf( %d n ,a[n]); return 0; } if(!add){ if(fminus){ int p=n+1; for(int i=1;i<=n;++i) if(a[i]==0){p=i;break ;} if(p==n+1){ for(int i=1;i<n;++i) printf( %d* ,a[i]);printf( %d n ,a[n]); return 0; } for(int i=1;i<p-1;++i) printf( %d* ,a[i]); if(p!=1) printf( %d- ,a[p-1]); for(int i=p;i<n;++i) printf( %d* ,a[i]);printf( %d n ,a[n]); } else{ // if(n==67878) printf( 43423423 n ); for(int i=1;i<n;++i) printf( %d* ,a[i]);printf( %d n ,a[n]); } return 0; } int l=1,r=l; while(l<=n){ while(a[r]!=0) ++r; ans[r-1]= + ,ans[r]= + ; solve(l,r-1);l=r+1;r=l; } for(int i=1;i<n;++i) printf( %d%c ,a[i],ans[i]);printf( %d n ,a[n]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int singles, doubles, n; cin >> n >> singles >> doubles; int refused = 0, partlyOccDoubles = 0; for (int i = 0; i < n; i++) { int cur; cin >> cur; if (cur == 1) { if (singles > 0) { singles--; } else if (doubles > 0) { doubles--; partlyOccDoubles++; } else if (partlyOccDoubles > 0) { partlyOccDoubles--; } else { refused += cur; } } else { if (doubles == 0) refused += cur; else doubles--; } } cout << refused; return 0; }
#include <bits/stdc++.h> using namespace std; void run(); int main() { ios::sync_with_stdio(0); cout.precision(10); run(); } struct vec2 { int x, y, sum; vec2 *c, *rgb; inline bool operator<(vec2 const &a) const { return x < a.x or x == a.x and y < a.y; } }; int yc[1000002], n; struct thingy { vec2 *things; int n, pivot; thingy *l, *r; thingy(vec2 *u, vec2 *v) { pivot = u[rand() % (n = (v - u))].y; auto a = u, b = v - 1, c = b; while (a <= b) if (b->y == pivot) swap(a++[0], b[0]); else --b; for (sort(u, b = a); b < c;) { while (b < c and b->y < pivot) ++b; while (b < c and c->y > pivot) --c; if (b < c) swap(*b, *c); } while (b < v and b->y < pivot) ++b; l = (a < b ? new thingy(a, b) : nullptr); r = (b < v ? new thingy(b, v) : nullptr); int sum = 0; vec2 *last[2] = {}; things = new vec2[n]; for (int i = 0, j = 0, k = 0, t = 0; t < n; ++t) { vec2 &from = (i < (a - u) and (j == b - a or u[i] < l->things[j]) and (k == v - b or u[i] < r->things[k]) ? u[i++] : j < (b - a) and (k == v - b or l->things[j] < r->things[k]) ? l->things[j++] : r->things[k++]); things[t] = {from.x, from.y, sum += (from.y <= pivot), &from, max((from.y <= pivot ? last[1] : nullptr), (from.y >= pivot ? last[0] : nullptr))}; if (things[t].y != pivot) last[pivot < things[t].y] = things + t; } } int query(int x, int y) const { vec2 *a = things - 1, *b = things + n; while (a + 1 < b) { vec2 *m = a + (b - a) / 2; (m->x <= x ? a : b) = m; } if (a < things) return 0; thingy const *on = this; int res = 0; while (a and on) { if (on->pivot <= y) res += a->sum; if (y == on->pivot) return res; while (a and (a->y == on->pivot or (a->y < on->pivot) != (y < on->pivot))) a = a->rgb; if (a) on = (y < on->pivot ? on->l : on->r), a = a->c; } return res; } ~thingy() { delete things; delete l; delete r; } }; vec2 v[100002]; void run() { int worth[9]; cin >> n; for (int i = n; i--;) cin >> v[i].x >> v[i].y, yc[i] = v[i].y; sort(yc, yc + n); yc[n] = 1 << 30; for (int i = 9; i--;) cin >> worth[i]; sort(worth, worth + 9); thingy data(v, v + n); sort(v, v + n); v[n].x = 1 << 30; do { int sx[3] = {}, sy[3] = {}, si[3] = {}, sj[3] = {}; for (int i = 9; i--;) si[i / 3] += worth[i], sj[i % 3] += worth[i]; for (int i = 0; i < 2; i++) si[i + 1] += si[i], sj[i + 1] += sj[i]; for (int i = 3; i--;) si[i]--, sj[i]--; if ((sx[0] = v[si[0]].x) == v[si[0] + 1].x) continue; if ((sx[1] = v[si[1]].x) == v[si[1] + 1].x) continue; if ((sy[0] = yc[sj[0]]) == yc[sj[0] + 1]) continue; if ((sy[1] = yc[sj[1]]) == yc[sj[1] + 1]) continue; sx[2] = sy[2] = 1 << 30; int u = data.query(sx[0], sy[0]); if (u - 0 != worth[0]) continue; int v = data.query(sx[1], sy[0]); if (v - u != worth[3]) continue; int w = data.query(sx[2], sy[0]); if (w - v != worth[6]) continue; int a = data.query(sx[0], sy[1]); if (a - 0 != worth[0] + worth[1]) continue; int b = data.query(sx[1], sy[1]); if (b - a != worth[3] + worth[4]) continue; int c = data.query(sx[2], sy[1]); if (c - b != worth[6] + worth[7]) continue; long double ra = sx[0] + 0.5, rb = sx[1] + 0.5, rc = sy[0] + 0.5, rd = sy[1] + 0.5; cout << ra << << rb << n << rc << << rd << endl; return; } while (next_permutation(worth, worth + 9)); cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; int f[1001000]; int dp[22][1001000]; int get(int x, int k) { for (int i = 21; i >= 0; i--) if (k & (1 << i)) x = dp[i][x]; return x; } int dist[1001000]; int foi[1001000]; vector<int> L[1001000]; char str[1001000]; char nova[1001000]; void bfs(int x, int dc) { queue<pair<int, int> > Q; Q.push(pair<int, int>(x, 0)); while (Q.size()) { pair<int, int> u = Q.front(); Q.pop(); for (vector<int>::iterator it = L[u.first].begin(); it < L[u.first].end(); it++) if (!foi[*it]) { foi[*it] = 1; dist[*it] = 1 + u.second; Q.push(pair<int, int>(*it, dist[*it])); } } } char debugs[1001000]; int main() { int n, m; scanf( %s , str); n = strlen(str); scanf( %d , &m); while (m--) { int d, k; scanf( %d%d , &k, &d); for (int i = 0; i <= n; i++) { L[i].clear(); dist[i] = 9999999; foi[i] = 0; } int cont = 0; for (int r = 0; r < d; r++) for (int i = r; i < k; i += d) { L[i + 1].push_back(cont); f[cont] = i + 1; ; cont++; } f[k] = k; bfs(k, 0); for (int i = 0; i < k; i++) dp[0][i] = f[i]; dp[0][k] = k; for (int j = 1; j < 22; j++) { for (int i = 0; i < k; i++) { dp[j][i] = dp[j - 1][dp[j - 1][i]]; ; } dp[j][k] = k; } for (int i = 0; i < k; i++) ; for (int i = 0; i < n; i++) { int y0 = 0; int p0 = i; int u = get(0, i + 1) - 1; if (i + k > n) y0 = i - n + k, p0 = n - k; u = get(y0, p0 + 1) - 1; ; if (dist[y0] > p0) { nova[i] = str[u]; continue; } int p = dist[y0]; nova[i] = str[p0 - p + k]; debugs[i] = str[p0 - p + k]; if (debugs[i] < a || debugs[i] > z ) debugs[i] = ? ; } for (int i = 0; i < n; i++) str[i] = nova[i]; printf( %s n , str); for (int i = 0; i < n; i++) ; ; } }
#include <bits/stdc++.h> using namespace std; int dp[10][10][10][10], pres_path; void dfs(int x, int y, int u, int v, int p); void dfs_next(int x, int y, int u, int v, int p) { int &t = dp[x][y][u][v]; if (t == 0 or t > pres_path + 1) { t = ++pres_path; if (p < 10) dfs(x, y, u, v, p + 1); --pres_path; } } void dfs(int x, int y, int u, int v, int p) { dfs_next(x, y, u, (v + x) % 10, p), dfs_next(x, y, u, (v + y) % 10, p); } int main() { string s; ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), cin >> s; for (int x = 0; x < int(10); ++x) for (int y = 0; y < int(10); ++y) for (int u = 0; u < int(10); ++u) dfs(x, y, u, u, 1); for (int x = 0; x < int(10); ++x) { for (int y = 0; y < int(10); ++y) { bool valid = true; int count = 0; for (int l, i = 0, j = 1, k = s.size(); j < k and valid; i = j++) if ((l = dp[x][y][s[i] - 0 ][s[j] - 0 ])) count += l - 1; else count = -1, valid = false; cout << count << ; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000007; char s[maxn]; int n, cnt, a[maxn]; bool check(int p, bool need_touch) { int x = 0, maxv = 0; bool touched = 0; for (int i = 1, _c = cnt; i <= _c; i++) { maxv = max(maxv, x); x += a[i]; if (x <= p) { x = p + 1; touched = 1; } } return (x > maxv) && (!need_touch || touched); } int main() { cnt = 0; gets(s + 1); n = strlen(s + 1); int x = 1; while (x <= n) { int y = x; while (y < n && s[y + 1] == s[x]) ++y; a[++cnt] = y - x + 1; if (s[x] == L ) a[cnt] *= -1; x = y + 1; } if (a[cnt] < 0) for (int i = 1, _c = cnt; i <= _c; i++) a[i] *= -1; if (check(-n - 1, 0)) { puts( 1 ); return 0; } int res = 0; int l = -n, r = -1, mid, pos = 0; while (l <= r) { mid = (l + r) / 2; if (check(mid, 1)) { pos = mid; r = mid - 1; } else l = mid + 1; } res -= pos; cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int read() { int x = 0; int sign = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { sign |= c == - ; } for (; isdigit(c); c = getchar()) { x = x * 10 + c - 0 ; } return sign ? -x : x; } const int ms = 1 << 22; const int lim = (1 << 22) - 1; int arr[ms], pai[ms], comps; bool hav[ms]; bool vis[ms]; int find(int x) { if (x == pai[x]) return x; return pai[x] = find(pai[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x != y) { --comps; pai[x] = y; } } void dfs(int u, int mask) { if (hav[mask]) unite(u, mask); if (vis[mask]) return; vis[mask] = true; if (mask == 0) return; int last_bit = 31 - __builtin_clz(mask); for (int i = last_bit; i >= 0; --i) { int nmask = mask & (~(1 << i)); dfs(u, nmask); } } int main() { ios::sync_with_stdio(0); cin.tie(0); iota(pai, pai + ms, 0); int n = read(), m = read(); comps = m; for (int i = 0; i < m; ++i) { arr[i] = read(); hav[arr[i]] = true; } for (int i = 0; i < m; ++i) { dfs(arr[i], ~arr[i] & lim); } cout << comps << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; if (k > (n % 2 == 0 ? n * n / 2 : (n * n + 1) / 2)) { cout << NO << endl; return 0; } cout << YES << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (k && (i + j) % 2 == 0) { cout << L ; k--; } else { cout << S ; } } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10; int tot; vector<int> primes; int notP[N]; vector<long long> divisors[N]; map<pair<long long, int>, int> dp; int isP(long long x) { for (int i = 0; (long long)primes[i] * primes[i] <= x; ++i) { if (x % primes[i] == 0) return false; } return true; } int dfs(long long x, int cur) { if (dp.count(make_pair(x, cur))) { return dp[make_pair(x, cur)]; } int &ret = dp[make_pair(x, cur)]; if (cur == tot) { if (x == 1) { ret++; } else if (x > N) { ret += isP(x - 1); } return ret; } ret += dfs(x, cur + 1); for (int i = 0; i < divisors[cur].size(); ++i) { if (x % divisors[cur][i] == 0) { ret += dfs(x / divisors[cur][i], cur + 1); } } return ret; } void solve() { long long n; cin >> n; tot = 0; for (int i = 0; i < primes.size(); ++i) { int prime = primes[i]; int flag = false; for (long long tmp = prime; tmp < n; tmp *= prime) { if (n % (tmp + 1) == 0) { if (!flag) { flag = true; ++tot; } divisors[tot - 1].push_back(tmp + 1); } } } cout << dfs(n, 0) << endl; } int main() { for (int i = 2; i < N; ++i) { if (notP[i]) continue; primes.push_back(i); for (long long j = (long long)i * i; j < N; j += i) { notP[j] = true; } } solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); int i, j, k; int ag[101], a[101][101]; int g = 0, dr[101], dl[101]; for (i = 1; i <= n; i++) { scanf( %d , &ag[i]); g += ag[i]; dl[i] = 1; dr[i] = ag[i]; for (j = 1; j <= ag[i]; j++) scanf( %d , &a[i][j]); } int m[10001], mg = 0; int s1 = 0, s2 = 0; for (i = 1; i <= n; i++) { if (ag[i] % 2 == 1) { m[mg] = a[i][(ag[i] + 1) / 2]; mg++; } for (j = 1; j <= ag[i]; j++) { if (j <= ag[i] / 2) s1 += a[i][j]; if (j > (ag[i] + 1) / 2) s2 += a[i][j]; } } sort(m, m + mg); for (i = mg - 1; i >= 0; i--) { if ((mg - i) % 2 == 1) s1 += m[i]; else s2 += m[i]; } printf( %d %d , s1, s2); return 0; }
#include <bits/stdc++.h> const int maxn = 400010, maxq = 400010; int n, cnt[maxn], tmp[maxn], lg[maxn]; int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } struct SA { char S[maxn]; int nxt[maxn], mn[maxn], sa[maxn], r[2][maxn], *rk, ht[20][maxn]; void make() { for (int i = 0; i < 128; i++) cnt[i] = 0, tmp[i] = 1 << 30; mn[n] = 1 << 30; for (int i = n; i--;) nxt[i] = tmp[S[i]], tmp[S[i]] = i, mn[i] = min(nxt[i], mn[i + 1]); for (int i = 0; i < n; i++) cnt[S[i]]++; for (int i = 1; i < 128; i++) cnt[i] += cnt[i - 1]; for (int i = n; i--;) sa[--cnt[S[i]]] = i; int tot = 0; rk = *r; for (int i = 0; i < n; i++) rk[sa[i]] = i && S[sa[i - 1]] == S[sa[i]] ? tot - 1 : tot++; for (int k = 1, *t = r[1]; tot < n; k *= 2) { for (int i = 0; i < k; i++) tmp[i] = i + n - k; for (int i = 0, j = k; i < n; i++) if (sa[i] >= k) tmp[j++] = sa[i] - k; for (int i = 0; i < tot; i++) cnt[i] = 0; for (int i = 0; i < n; i++) cnt[rk[i]]++; for (int i = 1; i < tot; i++) cnt[i] += cnt[i - 1]; for (int i = n; i--;) sa[--cnt[rk[tmp[i]]]] = tmp[i]; rk[n] = -1; for (int i = tot = 0; i < n; i++) t[sa[i]] = i && rk[sa[i - 1]] == rk[sa[i]] && rk[sa[i - 1] + k] == rk[sa[i] + k] ? tot - 1 : tot++; std::swap(rk, t); } for (int i = 0, h = 0; i < n; ht[0][rk[i++]] = h) for (h ? --h : 0; rk[i] + 1 < n && S[i + h] == S[sa[rk[i] + 1] + h]; h++) ; for (int k = 1; k <= lg[n]; k++) for (int i = 0; i + (1 << k) <= n; i++) ht[k][i] = min(ht[k - 1][i], ht[k - 1][i + (1 << k - 1)]); } int lcp(int i, int j) { if (i == n || j == n) return 0; int k; if ((i = rk[i]) > (j = rk[j])) return k = lg[i - j], min(ht[k][j], ht[k][i - (1 << k)]); return k = lg[j - i], min(ht[k][i], ht[k][j - (1 << k)]); } } S0, S1; int R[maxq], ans[maxq]; std::vector<int> pf[maxn], qs[maxn]; int ul[maxn], ur[maxn], dl[maxn], dr[maxn], ml[maxn], pl[maxn], pr[maxn], list[maxn]; int find(int* u, int i) { return u[i] == i ? i : u[i] = find(u, u[i]); } void upd(int* u, int* d, int l, int r, int x) { while ((l = find(u, l)) < r) d ? d[l] = x : 0, u[l] = find(u, l + 1); } int calc(int l, int r, int br) { int len = r - l; for (int i = pf[len].size(); i--;) { int p = len / pf[len][i]; if (S0.lcp(l, l + p) >= len - p) return 1; } if (dl[l] && l + dl[l] * 2 <= r) return 2; if (dr[r] && r - dr[r] * 2 >= l) return 2; if (r > br && ul[r] > r) return 2; for (int i = 1; l + i < r && l + i <= br; i++) if (S0.lcp(l, r - i) >= i) return 2; if (S0.nxt[l] < r || S1.nxt[n - r] < n - l || ml[l] <= r) return 3; if (S0.mn[l] < r) return 4; return -1; } int main() { scanf( %d%s , &n, S0.S); for (int i = n, j = 0; i--; j++) S1.S[j] = S0.S[i]; for (int i = 2; i <= n; i++) { lg[i] = lg[i / 2] + 1; if (!pf[i].size()) for (int j = i; j <= n; j += i) pf[j].push_back(i); } S0.make(); S1.make(); for (int i = 0; i <= n + 1; i++) ul[i] = ur[i] = i; for (int l = 1; l <= n / 2; l++) { int la = -1; for (int i = 0; i + l <= n; i += l) if (i > la) { int pl = i - S1.lcp(n - i - l, n - i), pr = i + S0.lcp(i, i + l); if (pr - pl >= l) { upd(ul, dl, pl, pr - l + 1, l); upd(ur, dr, pl + l * 2, pr + l + 1, l); } la = pr; } } ml[n] = 1 << 30; for (int i = n; i--;) { ml[i] = ml[i + 1]; if (dl[i]) ml[i] = min(ml[i], i + dl[i] * 2); } int q; scanf( %d , &q); for (int i = 0, l; i < q; i++) { scanf( %d%d , &l, R + i); qs[l - 1].push_back(i); } int b = 0; while (b * b < n * 4) b++; for (int bl = 0, br = b; bl < n; bl = br, br += b) { if (br > n) br = n; for (int i = 0; i <= br; i++) cnt[i] = 0; for (int i = br + 1; i <= n; i++) { ul[i] = i; pl[i] = br - S1.lcp(n - i, n - br); cnt[pl[i]]++; } for (int i = 0; i < br; i++) cnt[i + 1] += cnt[i]; int tot = cnt[br]; for (int i = n; i > br; i--) tmp[--cnt[pl[i]]] = i; for (int l = bl, i = 0; l < n && l < br; l++) { for (; i < tot && pl[tmp[i]] <= l; i++) upd(ul, 0, tmp[i], tmp[i] + S0.lcp(br, tmp[i]) + 1, 0); for (int id = qs[l].size(); id--;) { int x = qs[l][id]; ans[x] = calc(l, R[x], br); } } } for (int i = 0; i < q; i++) printf( %d n , ans[i]); }
#include <bits/stdc++.h> using namespace std; string ls[] = { , one , two , three , four , five , six , seven , eight , nine }; string fr[] = { , , twenty , thirty , forty , fifty , sixty , seventy , eighty , ninety }; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int first = n / 10, second = n % 10; switch (n) { case 0: cout << zero << endl; break; case 10: cout << ten << endl; break; case 11: cout << eleven << endl; break; case 12: cout << twelve << endl; break; case 13: cout << thirteen << endl; break; case 14: cout << fourteen << endl; break; case 15: cout << fifteen << endl; break; case 16: cout << sixteen << endl; break; case 17: cout << seventeen << endl; break; case 18: cout << eighteen << endl; break; case 19: cout << nineteen << endl; break; default: { if (!first) cout << ls[second] << endl; else if (!second) cout << fr[first] << endl; else cout << fr[first] << - << ls[second] << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { double n, m, k; cin >> n >> m >> k; if (m == 0) cout << 0 << endl; else if (n / k >= m) { cout << (int)m << endl; } else { double zz = (m - (n / k)); int tt; tt = ceil(zz / (k - 1)); cout << << (int)(n / k) - tt << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int r, h, sum, n; float k, l; cin >> r >> h; n = h / r; h -= n * r; l = r / 2.0; k = sqrt(3) / 2 * r; if (h < l) sum = n * 2 + 1; if (h >= l && h < k) sum = n * 2 + 2; if (h >= k) sum = n * 2 + 3; cout << sum << endl; return 0; }
#include <bits/stdc++.h> namespace IO { const unsigned int Buffsize = 1 << 15, Output = 1 << 23; static char Ch_[Buffsize], *S_ = Ch_, *T_ = Ch_; static char Out[Output], *nowps = Out; inline void flush() { fwrite(Out, 1, nowps - Out, stdout); nowps = Out; } template <typename T> inline T Read() { T _ = 0; static char __; T ___ = 1; for (__ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), S_ == T_) ? 0 : *S_++); !isdigit(__); __ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), S_ == T_) ? 0 : *S_++)) if (__ == - ) ___ = -1; for (; isdigit(__); __ = ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), S_ == T_) ? 0 : *S_++)) _ = (_ << 3) + (_ << 1) + (__ ^ 48); return _ *= ___; } template <typename T> inline void write(T _, char __ = n ) { if (!_) *nowps++ = 0 ; if (_ < 0) *nowps++ = - , _ = -_; static unsigned int sta[111], tp; for (tp = 0; _; _ /= 10) sta[++tp] = _ % 10; for (; tp; *nowps++ = sta[tp--] ^ 48) ; (*nowps++) = __; } template <class T> inline bool chkmax(T &_, T __) { return _ < __ ? _ = __, 1 : 0; } template <class T> inline bool chkmin(T &_, T __) { return _ > __ ? _ = __, 1 : 0; } } // namespace IO using namespace std; using namespace IO; const int N = 2e5 + 10, Mod = 1e9 + 7; int n; int fa[N], sz[N], flag[N], ans; int find(int first) { return first == fa[first] ? first : fa[first] = find(fa[first]); } void merge(int first, int second) { int fx = find(first), fy = find(second); if (fx != fy) { fa[fx] = fy; sz[fy] += sz[fx]; flag[fy] |= flag[fx]; } else flag[fy] = 1; } int main() { n = Read<int>(); for (int i = 1; i <= 2 * n; ++i) fa[i] = i, sz[i] = 1, flag[i] = 0; for (register int i = (1), i_end_ = (n); i <= i_end_; ++i) { int u = Read<int>(), v = Read<int>(); if (u != v) merge(u, v); else flag[u] = 2; } ans = 1; for (register int i = (1), i_end_ = (2 * n); i <= i_end_; ++i) if (find(i) == i) { if (flag[i] == 0) ans = (1ll * ans * sz[i]) % Mod; else if (flag[i] == 1) ans = (ans * 2ll) % Mod; } printf( %d n , ans); return flush(), 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); vector<int> v(n); vector<pair<int, int>> vv(n); for (int i = 0; i < n; i++) { scanf( %d , &v[i]); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); for (int i = 0; i < n; i++) { scanf( %d , &vv[i].first); vv[i].second = i; } sort(vv.begin(), vv.end()); vector<int> ans(n); for (int i = 0; i < n; i++) { ans[vv[i].second] = v[i]; } for (int i = 0; i < ans.size(); i++) cout << ans[i] << ; cout << n ; }
#include <bits/stdc++.h> using namespace std; ostream& operator<<(ostream& os, vector<int>& x) { for (int i = 0; i < x.size(); i++) os << x[i] << ; return os; } ostream& operator<<(ostream& os, pair<int, int> x) { cout << x.first << << x.second << ; return os; } ostream& operator<<(ostream& os, vector<pair<int, int> >& x) { for (int i = 0; i < x.size(); i++) os << x[i] << n ; return os; } vector<pair<int, int> > changed; bool mark[500005]; struct dsu { int par[500005]; dsu() { for (int i = 1; i <= 500005 - 5; i++) par[i] = i; } int find(int x, bool cache = false) { if (cache) { if (!mark[x]) { mark[x] = true; changed.push_back(make_pair(x, par[x])); } } if (par[x] == x) return x; return par[x] = find(par[x]); } bool uni(int x, int y, bool cache = false) { x = find(x, cache); y = find(y, cache); if (x != y) { if (cache) { if (!mark[x]) { mark[x] = true; changed.push_back(make_pair(x, par[x])); } } par[x] = y; return true; } return false; } } d; struct data { int u, v, c; data(int _u, int _v, int _c) : u(_u), v(_v), c(_c){}; }; bool byPrice(data x, data y) { return x.c < y.c; } int n, m, q; vector<data> edge; vector<data> oredge; vector<vector<vector<int> > > query(500005); vector<vector<pair<int, int> > > withW(500005); vector<vector<int> > tmp(500005); bool ans[500005]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v, c; cin >> u >> v >> c; edge.push_back(data(u, v, c)); withW[c].push_back(make_pair(u, v)); } cin >> q; for (int i = 1; i <= q; i++) { int num; cin >> num; vector<int> zz; while (num--) { int t; cin >> t; t--; zz.push_back(t); tmp[edge[t].c].push_back(t); } for (int j = 0; j < zz.size(); j++) { int c = edge[zz[j]].c; if (tmp[c].empty()) continue; tmp[c].push_back(i); query[c].push_back(tmp[c]); tmp[c].clear(); } } memset(ans, 1, sizeof(ans)); for (int w = 1; w <= 500000; w++) { for (int i = 0; i < query[w].size(); i++) { for (int j = 0; j < query[w][i].size() - 1; j++) { int num = query[w][i][j]; assert(edge[num].c == w); int u = edge[num].u; int v = edge[num].v; d.find(u); d.find(v); } } for (int i = 0; i < query[w].size(); i++) { int which = query[w][i].back(); bool ok = true; for (int j = 0; j < query[w][i].size() - 1; j++) { int num = query[w][i][j]; int u = edge[num].u; int v = edge[num].v; if (!d.uni(u, v, 1)) { ok = false; break; } } if (!ok) { ans[which] = false; } for (int j = 0; j < changed.size(); j++) { d.par[changed[j].first] = changed[j].second; mark[changed[j].first] = false; } changed.clear(); } for (int i = 0; i < withW[w].size(); i++) { int u = withW[w][i].first; int v = withW[w][i].second; d.uni(u, v); } } for (int i = 1; i <= q; i++) { if (ans[i] == 1) cout << YES << n ; else cout << NO << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int a1[n], a2[n]; for (int i = 0; i < n; i++) a1[i] = s[i] - 0 ; for (int i = n; i < 2 * n; i++) a2[i - n] = s[i] - 0 ; for (int i = 0; i < n; i++) { int s1(i), s2(i); for (int j = i + 1; j < n; j++) { if (a1[j] < a1[s1]) s1 = j; if (a2[j] < a2[s2]) s2 = j; } swap(a1[i], a1[s1]); swap(a2[i], a2[s2]); } int l(0), m(0); for (int i = 0; i < n; i++) if (a1[i] < a2[i]) l += 1; else if (a1[i] > a2[i]) m += 1; else if (a1[i] == a2[i]) { cout << NO << endl; return 0; } if ((l == n) || (m == n)) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = int(1e9); long long n, m; pair<long long, long long> p = make_pair(0, 0); long long b[222222]; long long c[222222]; long long ans; map<long long, long long> mp; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; mp[x]++; } cin >> m; for (int i = 1; i <= m; i++) cin >> b[i]; for (int i = 1; i <= m; i++) cin >> c[i]; for (int i = 1; i <= m; i++) if (p <= make_pair(mp[b[i]], mp[c[i]])) ans = i, p = make_pair(mp[b[i]], mp[c[i]]); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<long long> heros(n); for (auto& x : heros) cin >> x; sort(heros.begin(), heros.end()); long long sum = accumulate(heros.begin(), heros.end(), 0ll); int m; cin >> m; while (m--) { long long atk, dfns; cin >> atk >> dfns; int id = lower_bound(heros.begin(), heros.end(), atk) - heros.begin(); long long ans = 2e18 + 5; if (id > 0) { ans = min(ans, atk - heros[id - 1] + max(0ll, dfns - (sum - heros[id - 1]))); } if (id < n) { ans = min(ans, max(0ll, dfns - (sum - heros[id]))); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 3; int last[22][22]; int a[maxn], dp[20][maxn]; int main() { ios_base::sync_with_stdio(false); int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; int l = 19; memset(last, 63, sizeof last); memset(dp, 63, sizeof dp); for (int i = n; i >= 1; i--) { for (int fb = 0; fb < l; fb++) for (int sb = 0; sb < l; sb++) if (a[i] & (1 << fb) and a[i] & (1 << sb)) last[fb][sb] = i; for (int fb = 0; fb < l; fb++) for (int sb = 0; sb < l; sb++) if (a[i] & (1 << sb)) dp[fb][i] = min(dp[fb][i], last[fb][sb]); for (int fb = 0; fb < l; fb++) for (int sb = 0; sb < l; sb++) if (dp[sb][i] <= n) dp[fb][i] = min(dp[fb][i], dp[fb][dp[sb][i]]); } while (q--) { int x, y; cin >> x >> y; bool ret = 0; for (int i = 0; i < l; i++) if (a[y] & (1 << i) and dp[i][x] <= y) ret |= 1; if (ret == 1) cout << Shi n ; else cout << Fou n ; } }
#include <bits/stdc++.h> using namespace std; int n, k; int a[100005]; int b[100005]; bool ok(long long x) { long long s = k; for (int i = 0; i < n; i++) { if (x * a[i] >= b[i]) { s -= (x * a[i] - b[i]); } if (s < 0) { return false; } } return true; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int j = 0; j < n; j++) { cin >> b[j]; } if (n == 1) { cout << (b[0] + k) / a[0] << endl; } else { int sum = 0; long long l = 0, r = 0xffffffff; long long mid; while (l <= r) { mid = (l + r) >> 1; if (ok(mid)) { l = mid + 1; sum = mid; } else { r = mid - 1; } } cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> a(n); map<string, int> M; for (int i = 0; i < n; ++i) { cin >> a[i]; M[a[i]] = i; } vector<vector<int> > Inv(1 << n, vector<int>(n, 0)); for (int mask = 1; mask < (1 << n); ++mask) for (int j = 0; j < n; ++j) if ((mask & (1 << j)) == 0) for (int l = 0; l < n; ++l) if ((mask & (1 << l)) > 0 && M[a[j]] < M[a[l]]) ++Inv[mask][j]; int m; cin >> m; int p = -1, nr; for (int i = 0; i < m; ++i) { int k; cin >> k; vector<string> b(k); for (int j = 0; j < k; ++j) cin >> b[j]; vector<vector<int> > nast(k, vector<int>(n, INF)); if (M.find(b[k - 1]) != M.end()) nast[k - 1][M[b[k - 1]]] = k - 1; for (int j = k - 2; j >= 0; --j) { for (int l = 0; l < n; ++l) nast[j][l] = nast[j + 1][l]; if (M.find(b[j]) != M.end()) nast[j][M[b[j]]] = j; } vector<vector<int> > dp((1 << n), vector<int>(n * (n - 1) / 2 + 1, INF)); for (int j = 0; j < n; ++j) if (nast[0][j] != INF) dp[1 << j][0] = nast[0][j]; for (int mask = 1; mask < (1 << n); ++mask) { for (int inv = 0; inv <= n * (n - 1) / 2; ++inv) { if (dp[mask][inv] == INF) continue; for (int j = 0; j < n; ++j) { if ((mask & (1 << j)) == 0) { int noweInv = inv + Inv[mask][j]; if (dp[mask][inv] + 1 < k && nast[dp[mask][inv] + 1][j] != INF) dp[mask | (1 << j)][noweInv] = min(dp[mask | (1 << j)][noweInv], nast[dp[mask][inv] + 1][j]); } } } } for (int j = 0; j <= n * (n - 1) / 2; ++j) { if (dp[(1 << n) - 1][j] != INF && n * (n - 1) / 2 - j + 1 > p) { p = n * (n - 1) / 2 - j + 1; nr = i; } } } if (p == -1) cout << Brand new problem! ; else { cout << nr + 1 << endl; cout << [: ; for (int i = 0; i < p; ++i) cout << | ; cout << :] ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 14; const int Q = 101; int n, m, q; long long dp[N][1 << N]; bool edge[N][N]; int a[Q], b[Q], c[Q]; int lowbit(int x) { return x & (-x); } int in(int i, int S) { return ((S >> i) & 1); } bool check(int u, int k) { for (int i = 0; i < n; i++) if (i != u) { for (int j = 0; j < n; j++) if (j != u) if (edge[i][j] && (in(i, k) ^ in(j, k))) return false; } return true; } long long DP(int u, int S) { if (dp[u][S] != -1) return dp[u][S]; dp[u][S] = 0; int status = S - (1 << u); int t; for (t = 0; t < n; t++) if (in(t, status)) break; for (int k = status; k; k = (k - 1) & status) if (in(t, k)) { if (!check(u, k)) continue; int v, cnt = 0; for (int i = 0; i < n; i++) { if (edge[u][i] && in(i, k)) { cnt++; v = i; } } if (cnt >= 2) continue; bool flag = true; for (int i = 1; i <= q; i++) { if (c[i] == u && in(a[i], k) && in(b[i], k)) { flag = false; break; } if (in(c[i], k) && (!in(a[i], k) || !in(b[i], k))) { flag = false; break; } } if (!flag) continue; if (cnt == 1) { dp[u][S] += DP(v, k) * DP(u, S - k); } else { for (v = 0; v < n; v++) if (in(v, k)) dp[u][S] += DP(v, k) * DP(u, S - k); } } return dp[u][S]; } int main() { scanf( %d%d%d , &n, &m, &q); for (int i = 1; i <= m; i++) { int x, y; scanf( %d%d , &x, &y); x--, y--; edge[x][y] = edge[y][x] = true; } for (int i = 1; i <= q; i++) { scanf( %d%d%d , &a[i], &b[i], &c[i]); a[i]--; b[i]--; c[i]--; } memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; i++) dp[i][1 << i] = 1; printf( %lld , DP(0, (1 << n) - 1)); return 0; }
#include <bits/stdc++.h> using namespace std; int t = 1; void solve() { long long n, i, k, s = 0, b; cin >> n >> k; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); b = a[0]; for (i = 1; i < n; i++) { long long c = (k - a[i]) / b; if (c > 0) s += c; } cout << s << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 20; const int inf = 0x3f3f3f3f; const int modd = 1e9 + 7; inline int read() { int x = 0, f = 1; char c = getchar(); while (c != - && (c < 0 || c > 9 )) c = getchar(); if (c == - ) f = -1, c = getchar(); while (c >= 0 && c <= 9 ) x = x * 10 + c - 0 , c = getchar(); return f * x; } template <class T> inline void sc(T &x) { char c; x = 0; while ((c = getchar()) < 0 ) ; while (c >= 0 && c <= 9 ) x = x * 10 + (c - 48), c = getchar(); } inline long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } inline long long exgcd(long long a, long long b, long long &x, long long &y) { long long d; (b == 0 ? (x = 1, y = 0, d = a) : (d = exgcd(b, a % b, y, x), y -= a / b * x)); return d; } inline long long qpow(long long a, long long n) { long long sum = 1; while (n) { if (n & 1) sum = sum * a % modd; a = a * a % modd; n >>= 1; } return sum; } inline long long qmul(long long a, long long n) { long long sum = 0; while (n) { if (n & 1) sum = (sum + a) % modd; a = (a + a) % modd; n >>= 1; } return sum; } inline long long inv(long long a) { return qpow(a, modd - 2); } int a[maxn]; int main() { int n, m; scanf( %d%d , &n, &m); if (m && n < 3) return printf( -1 n ), 0; int i = 1; for (; i <= n;) { if (m >= (i - 1) / 2) { a[i] = i, m -= (i - 1) / 2; i++; continue; } if (m > 0) a[i] = 2 * (i - m) - 1, m = 0, i++; if (!m) break; } for (int j = n; j >= i; j--) a[j] = (int)9e8 - (n - j) * (a[i - 1] + 1); if (!m) { for (int i = 1; i <= n; i++) printf( %d%c , a[i], n [i == n]); return 0; } printf( -1 n ); }
#include <bits/stdc++.h> using namespace std; int n, leaf; vector<int> vt[100005]; int ara[100005], chk[100005], level[100005], child[100005]; map<int, int> mp; void bfs() { int i, j, k; queue<int> qu; qu.push(0); chk[0] = 1; level[0] = 0; while (!qu.empty()) { k = qu.front(); qu.pop(); for (i = 0; i < vt[k].size(); ++i) { j = vt[k][i]; if (!chk[j]) { qu.push(j); level[j] = level[k] + 1; chk[j] = 1; } } } } int main() { long long i, j, k; long long xr, ans; scanf( %d , &n); for (i = 0; i < n; ++i) scanf( %d , &ara[i]); for (i = 1; i < n; ++i) { scanf( %d , &k); k--; child[k] = 1; vt[i].push_back(k); vt[k].push_back(i); } bfs(); for (i = 0; i < n; ++i) if (!child[i]) leaf = i; for (i = 0, xr = 0, k = n; i < n; ++i) { if (level[leaf] % 2 == level[i] % 2) xr ^= ara[i], k--, mp[ara[i]]++; ; } ans = 0; if (xr == 0) { ans += (k * (k - 1)) / 2; k = n - k; ans += (k * (k - 1)) / 2; for (i = 0; i < n; ++i) { if (level[leaf] % 2 != level[i] % 2) ans += mp[ara[i]]; } } else { for (i = 0; i < n; ++i) { if (level[leaf] % 2 == level[i] % 2) continue; k = xr ^ ara[i]; ans += mp[k]; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int testnum; const int maxn = 100005; int par[maxn]; int size[maxn]; int find(int u) { if (par[u] == u) return u; return par[u] = find(par[u]); } bool unify(int u, int v) { int cu = find(u), cv = find(v); if (cu == cv) return true; if (size[cu] > size[cv]) { par[cv] = cu; size[cu] += size[cv]; } else { par[cu] = cv; size[cv] += size[cu]; } return false; } int n, m; void preprocess() { for (int i = 0; i < maxn; i++) { par[i] = i; size[i] = 1; } } long long mod = 1000000009; void solve() { int a, b; long long ans = 1; for (int i = 0; i < m; i++) { scanf( %d , &a); scanf( %d , &b); bool z = unify(a, b); if (z) { ans = (ans + ans) % mod; } cout << (ans - 1) << endl; } } bool input() { scanf( %d , &n); scanf( %d , &m); return true; } int main() { preprocess(); int T = 1; for (testnum = 1; testnum <= T; testnum++) { if (!input()) break; solve(); } }
#include <bits/stdc++.h> using namespace std; int n; int main() { scanf( %d , &n); if (n <= 2) { puts( No ); return 0; } puts( Yes ); if (n & 1) { printf( 2 1 %d n , n); printf( %d , n - 2); for (int i = 2; i < n; i++) printf( %d , i); puts( ); } else { printf( %d n , n / 2); for (int i = 1; i <= n; i++) if (i & 1) printf( %d , i); puts( ); printf( %d n , n / 2); for (int i = 1; i <= n; i++) if (!(i & 1)) printf( %d , i); puts( ); } }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long N = 100000; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << { ; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << , ; os << *it; } return os << } ; } template <typename first, typename second> ostream &operator<<(ostream &os, const pair<first, second> &p) { return os << ( << p.first << , << p.second << ) ; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << [ ; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << , ; os << *it; } return os << ] ; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { os << [ ; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << , ; os << *it; } return os << ] ; } template <typename first, typename second> ostream &operator<<(ostream &os, const map<first, second> &v) { os << [ ; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << , ; os << it->first << = << it->second; } return os << ] ; } pair<long long, long long> get(multiset<long long> &m) { auto it = m.end(); it--; return {*m.begin(), *it}; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long p; cin >> p; long long q; cin >> q; long long r; cin >> r; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } vector<long long> temp; temp.push_back(p); temp.push_back(q); temp.push_back(r); multiset<long long> m(a.begin(), a.end()), pref; ; long long res = LLONG_MIN; for (long long i = 0; i < n; i++) { pref.insert(a[i]); pair<long long, long long> left = get(pref); pair<long long, long long> ryt = get(m); long long mid = a[i]; long long cur = mid * temp[1]; if (temp[0] > 0) { cur += (temp[0] * left.second); } else { cur += (temp[0] * left.first); } if (temp[2] > 0) { cur += (temp[2] * ryt.second); } else { cur += (temp[2] * ryt.first); } res = max(res, cur); m.erase(m.find(a[i])); } cout << res << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int ab, bc, ca, abc; int temp[4]; for (int i = 0; i < 4; i++) { cin >> temp[i]; } sort(temp, temp + 4); ab = temp[0]; bc = temp[1]; ca = temp[2]; abc = temp[3]; int a, b, c; a = abc - bc; b = ab - a; c = ca - a; cout << a << << b << << c << endl; }
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e) { cout << e << 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; } string st; long long cum1[500010], cum2[500010], cum3[500010], cumhelp3[500010]; bool okay(char ch) { return ch == A || ch == E || ch == I || ch == O || ch == U || ch == Y ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int mini, i, j, n, baki1, baki2; cin >> st; for (i = 0; i < st.size(); i++) { if (!okay(st[i])) continue; baki1 = i; baki2 = st.size() - i - 1; mini = min(baki1, baki2); mini++; cum1[1]++; cum1[mini + 1]--; if (baki1 > baki2) swap(baki1, baki2); if (baki2 > baki1) { cum2[baki1 + 2] += mini; cum2[baki2 + 2] -= mini; } mini--; cumhelp3[st.size() - mini + 2]--; cum3[st.size() - mini + 1] += mini; } double ans = 0; for (i = 1; i <= st.size(); i++) { cum1[i] += cum1[i - 1]; cum2[i] += cum2[i - 1]; cumhelp3[i] += cumhelp3[i - 1]; cum3[i] += cum3[i - 1] + cumhelp3[i]; } for (i = 1; i <= st.size(); i++) { cum1[i] *= (long long)i; ans += (double)(cum1[i] + cum2[i] + cum3[i]) / (double)i; } cout << fixed << setprecision(12) << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, T, ans = 0; cin >> n >> a >> b >> T; vector<int> d(n, 1); for (int& p : d) { char c; cin >> c; if (c == w ) p += b; } vector<int> rtime(n), ltime(n); rtime[0] = d[0], ltime[0] = d[0]; for (int i = 1; i < n; i++) { rtime[i] = d[i] + a + rtime[i - 1]; ltime[i] = d[n - i] + a + ltime[i - 1]; } if (d[0] <= T) ans = 1; for (int i = 1; i < n; i++) { int time = T - rtime[i]; if (time >= 0) ans = max(ans, 1 + i); time -= i * a; if (time > 0) { time += d[0]; int num_left = lower_bound(ltime.begin(), ltime.end(), time) - ltime.begin(); if (ltime[num_left] > time && num_left > 0) num_left--; ans = max(ans, 1 + i + num_left); } } for (int i = 1; i < n; i++) { int time = T - ltime[i]; if (time >= 0) ans = max(ans, 1 + i); time -= i * a; if (time > 0) { time += d[0]; int num_right = lower_bound(rtime.begin(), rtime.end(), time) - rtime.begin(); if (rtime[num_right] > time && num_right > 0) num_right--; ans = max(ans, 1 + i + num_right); } } cout << min(n, ans) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, l; cin >> n >> l; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); double maxy = INT_MIN * 1.0; for (int i = 0; i < n - 1; i++) { if (arr[i + 1] - arr[i] > maxy) maxy = arr[i + 1] - arr[i]; } maxy *= 1.0; if (arr[0] == 0 or arr[n - 1] == l) { if (arr[0] == 0 and arr[n - 1] == l) { cout << fixed << setprecision(10) << maxy / 2; } else if (arr[0] == 0) { cout << fixed << setprecision(10) << max(maxy / 2, (l - arr[n - 1]) * 1.0) << n ; } else { cout << fixed << setprecision(10) << max(maxy / 2, (arr[0]) * 1.0) << n ; } } else { cout << fixed << setprecision(10) << max(maxy / 2, max(l - arr[n - 1], arr[0]) * 1.0) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { if (a > b) return a; else return b; } long long min(long long a, long long b) { if (a < b) return a; else return b; } long long power(long long B, long long P) { if (P == 0) return 1; long long X = power(B, P / 2); if (P % 2 == 0) return X * X; else return B * X * X; } long long fx4[] = {1, -1, 0, 0}; long long fy4[] = {0, 0, 1, -1}; int main() { long long T, N, M, X, Y, W, K, Q, R, P; cin >> N >> M; long long e1 = 0, o1 = 0, e2 = 0, o2 = 0; for (int i = 1; i <= N; i++) { cin >> X; if (X % 2) o1++; else e1++; } for (int i = 1; i <= M; i++) { cin >> X; if (X % 2) o2++; else e2++; } cout << (min(e1, o2) + min(o1, e2)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x6fffffff; const int MINF = 0x80000000; const long long mod = 1000000007; const int cons = 100001; const double pi = 3.141592653589793; vector<int> pos; bool isOk(int x, int t) { double res = 0; for (int i = 1; i < pos.size(); i++) { int diff = pos[i] - pos[i - 1]; int l = -1; int r = x + 1; while (l + 1 < r) { int mid = l + r >> 1; int y = x - mid; double avail = (double)y / 2 + mid; if (avail >= (double)diff) r = mid; else l = mid; } if (r == x + 1) return false; diff -= r; res += (double)r * 2; res += min((double)diff, ((double)x - r) / 2); } return res <= (double)t; } int main() { int n, k, s, t; cin >> n >> k >> s >> t; map<int, int> M; for (int i = 0; i < n; i++) { int c, v; scanf( %d%d , &c, &v); M[c] = max(M[c], v); } int cur = 0; vector<pair<int, int> > dat; for (auto it : M) { if (cur < it.second) { cur = it.second; dat.push_back(it); } } pos.resize(k); for (int i = 0; i < k; i++) { scanf( %d , &pos[i]); } pos.push_back(0); pos.push_back(s); sort(pos.begin(), pos.end()); int L = -1; int R = dat.size(); while (L + 1 < R) { int mid = L + R >> 1; if (isOk(dat[mid].second, t)) R = mid; else L = mid; } if (R == dat.size()) puts( -1 ); else printf( %d n , dat[R].first); return 0; }
#include <bits/stdc++.h> using namespace std; const int oo = 1000000001; int dp[1200][1200], out[1200][5], pre[1200][1200], a[1200]; int i, j, last, n, mi; int main() { while (~scanf( %d , &n)) { for (i = 0; i < n; i++) scanf( %d , &a[i]); if (n == 1) { printf( %d n%d n , a[0], 1); continue; } if (n == 2) { printf( %d n%d %d n , max(a[0], a[1]), 1, 2); continue; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) dp[i][j] = oo; dp[1][0] = max(a[1], a[2]); dp[1][1] = max(a[0], a[2]); dp[1][2] = max(a[0], a[1]); for (i = 1; 2 * i + 2 < n; i++) { for (j = 0; j <= 2 * i; j++) { if (dp[i + 1][j] - max(a[2 * i + 2], a[2 * i + 1]) > dp[i][j]) { dp[i + 1][j] = dp[i][j] + max(a[2 * i + 2], a[2 * i + 1]); pre[i + 1][j] = j; } if (dp[i + 1][2 * i + 2] - max(a[j], a[2 * i + 1]) > dp[i][j]) { dp[i + 1][2 * i + 2] = dp[i][j] + max(a[j], a[2 * i + 1]); pre[i + 1][2 * i + 2] = j; } if (dp[i + 1][2 * i + 1] - max(a[j], a[2 * i + 2]) > dp[i][j]) { dp[i + 1][2 * i + 1] = dp[i][j] + max(a[j], a[2 * i + 2]); pre[i + 1][2 * i + 1] = j; } } } if (n % 2 == 1) { mi = oo; for (i = 0; i < n; i++) { if (mi > dp[n / 2][i] + a[i]) { mi = dp[n / 2][i] + a[i]; last = i; } } printf( %d n , mi); out[n / 2][0] = last; for (i = n / 2; i > 0; i--) { int te = pre[i][last]; if (last == 2 * i - 1) { out[i - 1][0] = te; out[i - 1][1] = 2 * i; last = te; } else if (last == 2 * i) { out[i - 1][0] = te; out[i - 1][1] = 2 * i - 1; last = te; } else { out[i - 1][0] = 2 * i - 1; out[i - 1][1] = 2 * i; last = te; } } for (i = 0; i < n / 2; i++) printf( %d %d n , out[i][0] + 1, out[i][1] + 1); printf( %d n , out[n / 2][0] + 1); } else { mi = oo; for (i = 0; i < n; i++) { if (mi > dp[n / 2 - 1][i] + max(a[i], a[n - 1])) { mi = dp[n / 2 - 1][i] + max(a[i], a[n - 1]); last = i; } } printf( %d n , mi); out[n / 2 - 1][0] = last; out[n / 2 - 1][1] = n - 1; for (i = n / 2 - 1; i > 0; i--) { int te = pre[i][last]; if (last == 2 * i - 1) { out[i - 1][0] = te; out[i - 1][1] = 2 * i; last = te; } else if (last == 2 * i) { out[i - 1][0] = te; out[i - 1][1] = 2 * i - 1; last = te; } else { out[i - 1][0] = 2 * i - 1; out[i - 1][1] = 2 * i; last = te; } } for (i = 0; i < n / 2; i++) printf( %d %d n , out[i][0] + 1, out[i][1] + 1); } } }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /STACK:1024000000,1024000000 ) template <typename T> T Max(T a, T b) { return a > b ? a : b; } template <typename T> T Min(T a, T b) { return a < b ? a : b; } template <typename T> T Abs(T A) { return A > 0 ? A : -1 * A; } template <typename T> void Swap(T &a, T &b) { T tmp = a; a = b; b = tmp; } const long long LINF = 0x3FFFFFFFFFFFFFFLL; const int INF = 0X3FFFFFFF; const int MAXN = 1e5 + 5; long long sum[MAXN << 2][5]; int cnt[MAXN << 2]; char opt[MAXN][5]; int a[MAXN], c[MAXN]; inline void PushUp(int rt) { for (int i = 0; i < 5; i++) sum[rt][i] = sum[rt << 1][i] + sum[rt << 1 | 1][((i - cnt[rt << 1]) % 5 + 5) % 5]; } void Build(int l, int r, int rt) { cnt[rt] = 0; fill(sum[rt], sum[rt] + 5, 0); if (l == r) return; int m = (l + r) >> 1; Build(l, m, rt << 1); Build(m + 1, r, rt << 1 | 1); } void update(int pos, int val, int col, int l, int r, int rt) { if (col) cnt[rt]++; else cnt[rt]--; if (l == r) { sum[rt][0] = (long long)col * val; return; } int m = (l + r) >> 1; if (pos <= m) update(pos, val, col, l, m, rt << 1); else update(pos, val, col, m + 1, r, rt << 1 | 1); PushUp(rt); } int main() { int n; while (~scanf( %d , &n)) { int id = 0; for (int i = 1; i <= n; i++) { scanf( %s , opt[i]); if (opt[i][0] != s ) { scanf( %d , c + i); a[id++] = c[i]; } } sort(a, a + id); int num = unique(a, a + id) - a; if (num) Build(1, num, 1); else sum[1][2] = 0; for (int i = 1; i <= n; i++) { int pos = lower_bound(a, a + num, c[i]) - a + 1; if (opt[i][0] == a ) update(pos, c[i], 1, 1, num, 1); else if (opt[i][0] == d ) { if (cnt[1]) update(pos, c[i], 0, 1, num, 1); } else { if (cnt[1] == 0) sum[1][2] = 0; printf( %I64d n , sum[1][2]); } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN = 2e5 + 5; const long long base = 9999991; int n, m; char s1[maxN], s2[maxN]; vector<int> res; long long p[maxN], h1[30], h2[30]; int mark[30]; int check() { for (int i = 0; i <= 25; ++i) if (h1[i]) { mark[i] = -1; for (int j = 0; j <= 25; ++j) if (h1[i] == h2[j]) mark[i] = j; if (mark[i] == -1) return 0; } else mark[i] = -2; for (int i = 0; i <= 25; ++i) if (h1[i] && mark[mark[i]] != i && mark[mark[i]] != -2) return 0; return 1; } int main() { scanf( %d %d n%s n%s , &n, &m, s1 + 1, s2 + 1); p[0] = 1; for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * 2 % base; for (int i = 1; i <= m; ++i) for (int j = 0; j <= 25; ++j) { if (s1[i] - a == j) h1[j] = (h1[j] * 2 + 1) % base; else h1[j] = h1[j] * 2 % base; if (s2[i] - a == j) h2[j] = (h2[j] * 2 + 1) % base; else h2[j] = h2[j] * 2 % base; } if (check()) res.push_back(1); for (int i = m + 1; i <= n; ++i) { for (int j = 0; j <= 25; ++j) { if (s1[i - m] - a == j) h1[j] = (h1[j] - p[m - 1] + base) % base; ; if (s1[i] - a == j) h1[j] = (h1[j] * 2 + 1) % base; else h1[j] = h1[j] * 2 % base; } if (check()) res.push_back(i - m + 1); } printf( %d n , (int)res.size()); for (int i = 0; i < (int)res.size(); ++i) printf( %d , res[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c; cin >> a >> b >> c; long long sum = 2 * c; sum += 2 * min(a, b); if (a > b || b > a) sum++; cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int ans = INT_MAX; char W[100001]; scanf( %100000s , W); string s = W, t = Bulbasaur ; map<char, int> I, M; for (char ch : t) ++I[ch]; for (char ch : s) ++M[ch]; for (auto p : I) { ans = min(ans, M[p.first] / p.second); } printf( %d , ans); }
#include <bits/stdc++.h> using namespace std; const int N = 2e5; const int lg = 22; int spt[2 * N + 100][lg], par[N + 100][lg], H[N + 100], E[2 * N + 100], L[2 * N + 100]; int dept[2 * N + 100], vis[N + 100], mx[N + 100][lg], P[N + 100], rg[2 * N + 100], idx = 1; vector<pair<int, int> > g[N + 100]; vector<pair<long, pair<int, int> > > edge1, edge2; int find(int i) { if (P[i] == i) return i; return P[i] = find(P[i]); } bool sameset(int i, int j) { return (find(i) == find(j)); } void dfs(int v, int dep, int p) { dept[v] = dep; H[v] = idx; L[idx] = dep; E[idx++] = v; for (auto it : g[v]) { if (it.first != p) { mx[it.first][0] = it.second; par[it.first][0] = v; dfs(it.first, dep + 1, v); E[idx] = v; L[idx++] = dep; } } } int getmin(int i, int j) { int l = min(i, j), r = max(i, j); int lr = rg[r - l + 1]; if (L[spt[l][lr]] <= L[spt[r - (1 << lr) + 1][lr]]) return spt[l][lr]; else return spt[r - (1 << lr) + 1][lr]; } int main() { int n, m; scanf( %d%d , &n, &m); for (int i = 1; i <= m; i++) { int u, v, w; scanf( %d%d%d , &u, &v, &w); edge1.push_back(make_pair(w, make_pair(u, v))); edge2.push_back(make_pair(w, make_pair(u, v))); } sort(edge1.begin(), edge1.end()); for (int i = 1; i <= n; i++) P[i] = i; for (int i = 0; (1 << i) <= 2 * N; i++) { rg[1 << i] = i; } for (int i = 1; i <= 2 * N; i++) { if (!rg[i]) rg[i] = rg[i - 1]; } long long ans = 0; for (auto it : edge1) { int t1 = it.first; int u = it.second.first, v = it.second.second; if (!sameset(u, v)) { ans += t1; g[u].push_back(make_pair(v, t1)); g[v].push_back(make_pair(u, t1)); u = find(u); v = find(v); P[v] = u; } } memset(dept, -1, sizeof dept); dfs(1, 0, 0); for (int j = 1; j < lg; j++) { for (int i = 1; i <= n; i++) { par[i][j] = par[par[i][j - 1]][j - 1]; mx[i][j] = max(mx[i][j - 1], mx[par[i][j - 1]][j - 1]); } } for (int i = 1; i < 2 * n; i++) { spt[i][0] = i; } for (int j = 1; j < lg; j++) { for (int i = 1; i + (1 << j) - 1 < 2 * n; i++) { if (L[spt[i][j - 1]] <= L[spt[i + (1 << (j - 1))][j - 1]]) spt[i][j] = spt[i][j - 1]; else spt[i][j] = spt[i + (1 << (j - 1))][j - 1]; } } for (auto it : edge2) { int u = it.second.first, v = it.second.second; int lcs = E[getmin(H[u], H[v])]; int minus = 0; int cur = u; for (int i = lg - 1; i >= 0; i--) { if (dept[par[cur][i]] >= dept[lcs]) { minus = max(minus, mx[cur][i]); cur = par[cur][i]; } } cur = v; for (int i = lg - 1; i >= 0; i--) { if (dept[par[cur][i]] >= dept[lcs]) { minus = max(minus, mx[cur][i]); cur = par[cur][i]; } } printf( %lld n , ans + it.first - minus); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; int n; int data[maxn]; int f[25][maxn]; bool mark[(1 << 25)], tt; int res = 0, p; int tmp; int tm, tp; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> data[i]; memset(mark, 0, sizeof(mark)); for (int k = 0; k <= 20; k++) { p = -1; for (int i = n; i >= 1; i--) { if ((data[i] & (1 << k)) == 0) { if (p == -1) { p = i; } f[k][i] = p; } else p = -1; } } for (int i = 1; i <= n; i++) { tmp = data[i]; if (!mark[tmp]) { mark[tmp] = 1; res++; } p = i; while (p <= n) { tm = n + 1; for (int k = 0; k <= 20; k++) if ((tmp & (1 << k)) == 0 && f[k][i] < tm && f[k][i] > 0) { tp = k; tm = f[k][i]; } if (tm >= n) break; for (int k = 0; k <= 20; k++) if (f[k][i] == tm) tmp += (1 << k); if (!mark[tmp]) { mark[tmp] = 1; res++; } p = tm + 1; } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; map<string, int> person; vector<pair<int, string> > points; int cnt; bool compare(pair<int, string> a, pair<int, string> b) { if (a.first == b.first) return a.second < b.second; return a.first > b.first; } void update(string name, int p = 0) { if (!person[name]) { person[name] = ++cnt; points.push_back(make_pair(p, name)); } else points[person[name] - 1].first += p; } int main() { string me; int n; cin >> me >> n; for (int i = 0; i < n; i++) { string X, Y, type, temp; cin >> X >> type; int point; if (type == posted ) { cin >> temp >> Y >> temp; point = 15; } else if (type == commented ) { cin >> temp >> Y >> temp; point = 10; } else { cin >> Y >> temp; point = 5; } Y = Y.substr(0, Y.length() - 2); if (X == me) update(Y, point); else if (Y == me) update(X, point); else update(X), update(Y); } sort(points.begin(), points.end(), compare); for (int i = 0; i < cnt; i++) cout << points[i].second << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxn = 2e6 + 5; char s[maxn], t[maxn]; int m; struct Suffixam { struct state { int len, link; map<char, int> next; }; int id[maxn << 1], size[maxn << 1], cnt[maxn << 1]; state st[maxn << 1]; int last, sz; int vis[maxn << 1]; void sam_init() { memset(vis, -1, sizeof(vis)); sz = 0; st[0].len = 0; st[0].link = -1; st[0].next.clear(); sz++; last = 0; } void sam_extend(char c) { int cur = sz++; st[cur].len = st[last].len + 1; st[cur].next.clear(); size[cur] = 1; int p = last; while (~p && !st[p].next.count(c)) { st[p].next[c] = cur; p = st[p].link; } if (p == -1) { st[cur].link = 0; } else { int q = st[p].next[c]; if (st[p].len + 1 == st[q].len) { st[cur].link = q; } else { int clone = sz++; st[clone].len = st[p].len + 1; st[clone].next = st[q].next; st[clone].link = st[q].link; while (~p && st[p].next[c] == q) { st[p].next[c] = clone; p = st[p].link; } st[q].link = st[cur].link = clone; } } last = cur; } void get() { for (int i = 1; i < sz; ++i) { ++cnt[st[i].len]; } for (int i = 1; i < sz; ++i) { cnt[i] += cnt[i - 1]; } for (int i = 1; i < sz; ++i) { id[cnt[st[i].len]--] = i; } for (int i = sz; i >= 1; --i) { size[st[id[i]].link] += size[id[i]]; } } void solve(const char *ss, int x) { ll ans = 0; int len = 0, now = 0; for (int i = 0; ss[i]; ++i) { if (st[now].next.count(ss[i]) != 0) { now = st[now].next[ss[i]]; len++; while (~now && st[st[now].link].len >= m) { now = st[now].link; len = st[now].len; } if (vis[now] != x && len >= m) { ans += size[now]; vis[now] = x; } } else { while (~now && !st[now].next.count(ss[i])) { now = st[now].link; } if (now != -1) { len = st[now].len + 1; now = st[now].next[ss[i]]; } else { len = 0; now = 0; } while (~now && st[st[now].link].len >= m) { now = st[now].link; len = st[now].len; } if (vis[now] != x && len >= m) { ans += size[now]; vis[now] = x; } } } printf( %lld n , ans); } } sam; int main() { scanf( %s , s + 1); int n = strlen(s + 1); sam.sam_init(); for (int i = 1; i <= n; ++i) { sam.sam_extend(s[i]); } sam.get(); int q; scanf( %d , &q); while (q--) { scanf( %s , t + 1); m = strlen(t + 1); for (int i = 1; i <= m; ++i) { t[i + m] = t[i]; } t[2 * m + 1] = 0; sam.solve(t + 1, q); } return 0; }
#include <bits/stdc++.h> using namespace std; const int mN = 30; int a[mN]; int n; void output(int x) { string s; string mid; for (int i = 1; i <= n; i++) { if (a[i] / x % 2) mid += char(i + a - 1); for (int j = 1; j <= a[i] / x / 2; j++) s += char(i + a - 1); } s += mid; for (int i = n; i >= 1; i--) { for (int j = 1; j <= a[i] / x / 2; j++) s += char(i + a - 1); } for (int i = 1; i <= x; i++) { cout << s; reverse(s.begin(), s.end()); } } int gcd(int x, int y) { if (x % y == 0) return y; return gcd(y, x % y); } int main() { cin >> n; int odd = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] % 2) odd++; } int maxl = a[1]; for (int i = 2; i <= n; i++) maxl = gcd(maxl, a[i]); if (odd > 1) { cout << 0 << endl; output(1); } else if (maxl > 1) { if (odd <= 1) { cout << maxl << endl; output(maxl); } } else { cout << maxl << endl; output(maxl); } }
#include <bits/stdc++.h> using namespace std; const int maxN = 130 * 1000 + 5; const int mod = 1000 * 1000 * 1000 + 7; short dp[21][32][maxN]; int mas[maxN]; int ss[6][maxN]; int a[20], b[20]; vector<int> v[6]; int c[20], d[20]; int n, m; char ans[20][20]; int p; void solve(int lev, int mask, int state) { if (lev == 0) return; int tmp = state; for (int z = 0; z < n; z++) { c[z] = tmp % p; tmp /= p; } int smask = dp[lev][mask][state]; for (int j = 0; j < n; j++) { d[j] = c[j]; if ((mask >> j & 1) != (smask >> j & 1)) { if (mask >> j & 1) d[j]--; } } int sstate = 0; for (int j = n - 1; j >= 0; j--) sstate *= p, sstate += d[j]; solve(lev - 1, smask, sstate); for (int i = 0; i < n; i++) ans[i][lev - 1] = ((mask >> i & 1) ? * : . ); } int tof[32][32], tofm[32][32]; int main() { ios::sync_with_stdio(0); cin.tie(); memset(dp, -1, sizeof dp); cin >> n >> m; for (int mask = 0; mask < (1 << n); mask++) { int bef = 0, st = 0; for (int i = 0; i < n; i++) if (mask >> i & 1) { if (bef == 0) st++; bef = 1; } else bef = 0; v[st].push_back(mask); } for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; p = *max_element(a, a + n); p++; int stat = 0; for (int i = n - 1; i >= 0; i--) stat *= p, stat += a[i]; for (int state = 0; state < stat + 1; state++) { int tmp = state; for (int z = 0; z < n; z++) { ss[z][state] = tmp % p; if (ss[z][state] == 0) mas[state] |= (1 << z); tmp /= p; } } for (int mask = 0; mask < (1 << n); mask++) for (int smask = 0; smask < (1 << n); smask++) { int pp = 0; for (int j = 0; j < n; j++) { d[j] = 0; if ((mask >> j & 1) != (smask >> j & 1)) { if (mask >> j & 1) d[j]--, tofm[mask][smask] |= (1 << j); } } int sstate = 0; for (int j = n - 1; j >= 0; j--) sstate *= p, sstate += d[j]; tof[mask][smask] = sstate; } for (int i = 1; i < m + 1; i++) { int ind = b[i - 1]; for (int state = 0; state < stat + 1; state++) { for (auto mask : v[ind]) { for (auto smask : v[(i == 1 ? 0 : b[i - 2])]) { if (tofm[mask][smask] & mas[state]) continue; int sstate = state + tof[mask][smask]; if (dp[i - 1][smask][sstate] != -1 || (i == 1 && sstate == 0 && smask == 0)) { dp[i][mask][state] = smask; break; } } } } } int answer = -1; for (int i = 0; i < (1 << n); i++) { if (dp[m][i][stat] > -1) { answer = i; break; } } solve(m, answer, stat); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << ans[i][j]; cout << endl; } return 0; }
#include <bits/stdc++.h> long long min(long long x, long long y) { if (x > y) return y; else return x; } int main() { long long n, m; long long k; scanf( %I64d %I64d %I64d , &n, &m, &k); long long l = 1, r = n * m, mid, sum; while (l <= r) { mid = (l + r) >> 1; sum = 0; for (int i = 1; i <= n; i++) sum += min(m, (mid - 1) / i); if (sum < k) l = mid + 1; else r = mid - 1; } printf( %I64d , l - 1); scanf( ); }
#include <bits/stdc++.h> using namespace std; bool vis[8][8]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); char t; int y, x; cin >> t >> y; --y; x = t - a ; for (int i = 0; i < 8; i++) { vis[i][y] = true; vis[x][i] = true; } auto mark = [&](int x, int y) { vis[x][y] = true; for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { if (i * i + j * j != 5) continue; int dx = x + i, dy = y + j; if (dx >= 0 && dx < 8 && dy >= 0 && dy < 8) vis[dx][dy] = true; } } }; mark(x, y); cin >> t >> y; t; --y; mark(t - a , y); int cnt = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) cnt += vis[i][j]; } cout << 64 - cnt; return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e16; const int N = 900300; const int K = 40; int n; long long a[N]; long long sum[N]; long long dp[N][2]; int par[N][2][3]; int intPoint[N]; int m; vector<int> ans; long long getCost(int L, int R, int f1, int f2) { int len = R - L; if (len == 1) { if (a[L] == 0) return 0; if (f1 == 0 || f2 == 0) return -1; return a[L]; } if (len == 2) { if (a[L] == a[L + 1]) return 0; if (a[L] < a[L + 1]) { if (f2 == 0) return -1; return abs(a[L + 1] - a[L]); } else { if (f1 == 0) return -1; return abs(a[L + 1] - a[L]); } } if (f1 == 0 && a[L] > a[L + 1]) return -1; if (f2 == 0 && a[R - 1] > a[R - 2]) return -1; long long cost = abs(sum[R] - sum[L]); if (len == 3) { if (a[L] + a[L + 2] <= a[L + 1]) return cost; if (f1 == 1 && f2 == 1) return cost; if (f1 == 0 && f2 == 0) return -1; return cost; } return cost; } void restoreAns(int L, int R, int f1, int f2) { int len = R - L; if (len == 1) { return; } if (len == 2) { if (a[L] == 0 || a[L + 1] == 0) return; ans.push_back(L); return; } if (f1 == 0 && a[L] != 0 && a[L + 1] != 0) { long long x = min(a[L], a[L + 1]); ans.push_back(L); a[L] -= x; a[L + 1] -= x; } if (f2 == 0 && a[R - 1] != 0 && a[R - 2] != 0) { long long x = min(a[R - 1], a[R - 2]); ans.push_back(R - 2); a[R - 2] -= x; a[R - 1] -= x; } for (int i = L; i < R - 1; i++) { if (a[i] != 0 && a[i + 1] != 0) { long long x = min(a[i], a[i + 1]); ans.push_back(i); a[i] -= x; a[i + 1] -= x; } } } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %lld , &a[i]); sum[i + 1] = sum[i]; if (i % 2 == 0) sum[i + 1] += a[i]; else sum[i + 1] -= a[i]; } for (int i = 0; i <= n; i++) { if (i < 5 || i > n - 5) { intPoint[m++] = i; continue; } if (a[i - 1] <= a[i - 2] || a[i] <= a[i + 1]) intPoint[m++] = i; } for (int i = 0; i <= m; i++) dp[i][0] = dp[i][1] = -INF; dp[0][0] = 0; dp[0][1] = 0; for (int i = 0; i < m; i++) for (int f1 = 0; f1 < 2; f1++) { if (dp[i][f1] < 0) continue; for (int j = i + 1; j < min(m, i + K); j++) { for (int f2 = 0; f2 < 2 - f1; f2++) for (int f3 = 0; f3 < 2; f3++) { long long cost = getCost(intPoint[i], intPoint[j], f2, f3); if (cost == -1) continue; if (dp[i][f1] + cost > dp[j][f3]) { dp[j][f3] = dp[i][f1] + cost; par[j][f3][0] = i; par[j][f3][1] = f1; par[j][f3][2] = f2; } } } } int x = m - 1; int f = 0; if (dp[x][1] > dp[x][0]) f = 1; while (x > 0) { restoreAns(intPoint[par[x][f][0]], intPoint[x], par[x][f][2], f); int nx = par[x][f][0]; f = par[x][f][1]; x = nx; } printf( %d n , (int)ans.size()); for (int y : ans) printf( %d n , 1 + y); return 0; }
#include <bits/stdc++.h> using namespace std; int p[111], s[111], v[111]; int main() { int n, m, k, i, Nr = 0, c; cin >> n >> m >> k; for (i = 1; i <= n; ++i) cin >> p[i]; for (i = 1; i <= n; ++i) { cin >> s[i]; if (p[i] > v[s[i]]) v[s[i]] = p[i]; } for (i = 1; i <= k; ++i) { cin >> c; if (p[c] < v[s[c]]) Nr++; } cout << Nr << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, u, cnt = 1, ch[4000005][4], dp[4000005][4], dpp[60][4][4]; long long rev[2][4], nw = 0, ans = 0; char s[100005]; const long long inf = 2e18; void dfs(int p) { for (int i = 0; i < 4; i++) dp[p][i] = inf; for (int i = 0; i < 4; i++) { if (!ch[p][i]) dp[p][i] = 0; else { dfs(ch[p][i]); for (int j = 0; j < 4; j++) dp[p][j] = min(dp[p][j], dp[ch[p][i]][j] + 1); } } } int main() { scanf( %lld%s , &n, s + 1); m = strlen(s + 1); for (int i = 1; i <= m; i++) { int lim = min(m, 1ll * i + 39); u = 1; for (int j = i; j <= lim; j++) { int le = s[j] - A ; if (!ch[u][le]) ch[u][le] = ++cnt; u = ch[u][le]; } } dfs(1); for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) dpp[0][i][j] = dp[ch[1][i]][j] + 1; for (int i = 1; i < 60; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { dpp[i][j][k] = inf; for (int u = 0; u < 4; u++) dpp[i][j][k] = min(dpp[i][j][k], dpp[i - 1][j][u] + dpp[i - 1][u][k]); } } } ans = 0; for (int i = 59; i >= 0; i--) { for (int j = 0; j < 4; j++) rev[!nw][j] = inf; for (int j = 0; j < 4; j++) for (int k = 0; k < 4; k++) rev[!nw][k] = min(rev[!nw][k], rev[nw][j] + dpp[i][j][k]); long long sum = inf; for (int j = 0; j < 4; j++) sum = min(sum, rev[!nw][j]); if (sum < n) { ans += (1ll << i); for (int j = 0; j < 4; j++) if (rev[!nw][j] >= n) rev[!nw][j] = inf; } else for (int j = 0; j < 4; j++) rev[!nw][j] = rev[nw][j]; nw = !nw; } printf( %lld n , ans + 1); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> ans, a; int n, temp; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &temp); a.push_back(temp); } for (int i = 0; i < a.size(); i++) { if (a[i] == 1) { ans.push_back(1); } else { ans[ans.size() - 1]++; } } printf( %d n%d , (int)ans.size(), ans[0]); for (int i = 1; i < ans.size(); i++) { printf( %d , ans[i]); } printf( n ); }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; string gen = ACTG ; int sum = 10000; vector<bool> used_big(n, false); vector<bool> used_gen(4, false); for (int i = 0; i <= n - 4; i++) { int tmp_sum = 0; for (int j = 0; j < 4; j++) { int dif = abs(s[i + j] - gen[j]); if (26 - dif < dif) dif = 26 - dif; tmp_sum += dif; } if (tmp_sum < sum) sum = tmp_sum; } cout << sum; }
#include <bits/stdc++.h> using namespace std; void precalc(){}; const int N = 3010; int n, m; vector<vector<int>> g; vector<vector<int>> dist; pair<int, int> pre[N][3], post[N][3]; void bfs(int x) { queue<int> q; q.push(x); while (q.size()) { int head = q.front(); q.pop(); for (auto c : g[head]) { if (dist[x][c] > n) { dist[x][c] = dist[x][head] + 1; q.push(c); } } } } inline bool distinct(int a, int b, int c, int d) { vector<int> z = {a, b, c, d}; sort(z.begin(), z.end()); return z[0] != z[1] && z[1] != z[2] && z[2] != z[3]; } void solve(int ncase) { cin >> n >> m; g.assign(n + 1, vector<int>()); dist.assign(n + 1, vector<int>(n + 1, n + 1)); for (int i = 1; i <= n; i++) { dist[i][i] = 0; } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; g[x].push_back(y); } for (int i = 1; i <= n; i++) { bfs(i); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (dist[j][i] <= n) { pair<int, int> tmppre = pair<int, int>(dist[j][i], j); if (tmppre > pre[i][2]) { pre[i][2] = tmppre; if (pre[i][2] > pre[i][1]) { swap(pre[i][2], pre[i][1]); if (pre[i][1] > pre[i][0]) { swap(pre[i][1], pre[i][0]); } } } } if (dist[i][j] <= n) { pair<int, int> tmppost = pair<int, int>(dist[i][j], j); if (tmppost > post[i][2]) { post[i][2] = tmppost; if (post[i][2] > post[i][1]) { swap(post[i][2], post[i][1]); if (post[i][1] > post[i][0]) { swap(post[i][1], post[i][0]); } } } } } } int ans = -1; vector<int> ret(4); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j || dist[i][j] > n) continue; for (int a = 0; a < 3; a++) { if (pre[i][a].second == 0) continue; for (int b = 0; b < 3; b++) { if (post[j][b].second == 0) continue; int tmp = pre[i][a].first + dist[i][j] + post[j][b].first; if (tmp > ans && distinct(pre[i][a].second, i, j, post[j][b].second)) { ans = tmp; ret = {pre[i][a].second, i, j, post[j][b].second}; } } } } } cout << ret[0] << << ret[1] << << ret[2] << << ret[3] << endl; } int main() { int T = 1; int ncase = 0; while (T--) { solve(++ncase); } }
#include <bits/stdc++.h> using namespace std; int main() { int n, t, c = 1; cin >> n >> t; int mn = INT_MAX; for (int i = 1; i <= n; i++) { int s, d; cin >> s >> d; while (s < t) { s += d; } if (s < mn) { mn = s; c = i; } } cout << c << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); string a; cin >> a; vector<int> res[2]; for (char e : a) { vector<int> ne[2]; if (e == a ) { ne[0] = res[1]; ne[0].push_back(1); ne[1] = res[1]; ne[1].push_back(0); } else { ne[0] = res[0]; ne[0].push_back(0); ne[1] = res[0]; ne[1].push_back(1); } res[0] = ne[0]; res[1] = ne[1]; } for (int e : res[0]) { cout << e << ; } }
#include <bits/stdc++.h> using namespace std; int v, m, d, f; int main() { int n, a, b, c; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b >> c; if (a == 1) { v += b; m += c; } else { d += b; f += c; } } if (v >= m) cout << LIVE << n ; else cout << DEAD << n ; if (d >= f) cout << LIVE << n ; else cout << DEAD << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); } template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); } template <class T> inline T ison(T n, T pos) { return (bool)(n & ((T)1 << pos)); } template <class T> inline T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; } template <typename T> string NumberToString(T Number) { ostringstream second; second << Number; return second.str(); } inline int nxt() { int aaa; scanf( %d , &aaa); return aaa; } inline long long int lxt() { long long int aaa; scanf( %lld , &aaa); return aaa; } inline double dxt() { double aaa; scanf( %lf , &aaa); return aaa; } template <class T> inline T bigmod(T p, T e, T m) { T ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; } return (T)ret; } int ar[200010]; vector<int> v; map<int, int> mp; int main() { int c = 0; int i = 1; while (1) { if (c > 100000) break; c += i; v.push_back(c); mp[c] = i; i++; } int n = nxt(); if (n == 0) { cout << a << endl; return 0; } vector<int> ans; while (n) { for (int i = v.size() - 1; i >= 0; i--) { if (v[i] <= n) { ans.push_back(v[i]); n -= v[i]; } } } int t = 0; for (int i = 0; i < ans.size(); i++) { int x = ans[i]; int lagbe = mp[x]; for (int j = 0; j <= lagbe; j++) { printf( %c , t + a ); } t++; } printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f; const long long MOD = 1e9 + 7; const long double EPS = 1e-9; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string grid[3][3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { cin >> grid[i][k][j]; } } } int a, b; cin >> a >> b; int gi = (a - 1) % 3; int gj = (b - 1) % 3; bool shit = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (grid[gi][gj][i][j] == . ) shit = false; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (shit || i == gi && k == gj) { for (int l = 0; l < 3; l++) { if (grid[i][k][j][l] == . ) cout << ! ; else cout << grid[i][k][j][l]; } cout << ; } else { cout << grid[i][k][j] << ; } } cout << n ; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a, b, arr[100009]; int main() { cin >> n >> a >> b; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); cout << max(arr[n - a] - arr[n - a - 1], 0); }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); long long n, m; cin >> n >> m; map<long long, set<long long>> adj; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; adj[a].insert(b); adj[b].insert(a); } long long mx = INT_MIN, v = 0; map<long long, long long> deg; for (auto i : adj) { deg[i.first] = i.second.size(); if (mx < i.second.size()) { mx = i.second.size(); v = i.first; } } deg[v] = 0; for (auto i : adj[v]) { deg[i]--; } long long f = 0; for (long long i = 1; i <= n; i++) { if (deg[i] > 0) { f = 1; break; } } if (!f) { cout << YES n ; return 0; } mx = INT_MIN, v = 0; for (long long i = 1; i <= n; i++) { if (mx < deg[i]) { mx = deg[i]; v = i; } } deg[v] = 0; for (auto i : adj[v]) { deg[i]--; } f = 0; for (long long i = 1; i <= n; i++) { if (deg[i] > 0) { f = 1; break; } } if (!f) { cout << YES n ; return 0; } cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); inline double cross(double x1, double y1010101010101, double x2, double y2) { return x1 * x2 + y1010101010101 * y2; } inline double len(double x, double y) { return sqrt(x * x + y * y); } inline double angle(double x1, double y1010101010101, double x2, double y2) { if (x1 == 0 && y1010101010101 == 0) return pi; if (x2 == 0 && y2 == 0) return pi; return acos(cross(x1, y1010101010101, x2, y2) / len(x1, y1010101010101) / len(x2, y2)); } int n, x[100005], y[100005], cur = 0; struct data { double tx, ty; int fx, fy; int cx, cy; data operator~() { data ans = *this; ans.tx = -ans.tx; ans.ty = -ans.ty; ans.cx = -ans.cx; ans.cy = -ans.cy; return ans; } } d[100005 << 5]; inline data add(int x, int y, int cx, int cy) { data ans; ans.tx = d[x].tx * cx + d[y].tx * cy; ans.ty = d[x].ty * cx + d[y].ty * cy; ans.cx = cx; ans.cy = cy; ans.fx = x; ans.fy = y; return ans; } int neg[100005 << 5]; inline void dump(int x) { if (x <= n) return; neg[d[x].fx] = neg[x] * d[x].cx; neg[d[x].fy] = neg[x] * d[x].cy; dump(d[x].fx); dump(d[x].fy); } inline void dfs(int x, int y, int z, int w) { if (z == n + 1) { d[++cur] = ~d[x]; d[++cur] = ~d[y]; vector<int> t; t.push_back(x); t.push_back(y); t.push_back(cur - 1); t.push_back(cur); for (int i = 0; i < 4; i += 2) for (int j = 1; j < 4; j += 2) { int u = t[i], v = t[j]; if (len(d[u].tx + d[v].tx, d[u].ty + d[v].ty) <= 1500000.0) { if (i == 2) neg[x] = -1; else neg[x] = 1; if (j == 3) neg[y] = -1; else neg[y] = 1; dump(x); dump(y); return; } } } d[++cur] = ~d[x]; d[++cur] = ~d[y]; d[++cur] = ~d[z]; vector<int> t; t.push_back(x); t.push_back(y); t.push_back(z); t.push_back(cur - 2); t.push_back(cur - 1); t.push_back(cur); for (int i = 0; i < 6; i++) for (int j = 0; j < i; j++) { if ((i % 3) == (j % 3)) continue; int a = t[i], b = t[j]; if (angle(d[a].tx, d[a].ty, d[b].tx, d[b].ty) >= 2.0 * pi / 3) { d[++cur] = add(a, b, 1, 1); int nxt = 3 - (i % 3) - (j % 3); if (nxt == 0) dfs(cur, x, w, w + 1); if (nxt == 1) dfs(cur, y, w, w + 1); if (nxt == 2) dfs(cur, z, w, w + 1); return; } } } int main() { scanf( %d , &n); if (n == 1) return puts( 1 ), 0; for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); for (int i = 1; i <= n; i++) { d[i].tx = x[i]; d[i].ty = y[i]; d[i].fx = i; d[i].fy = 0; d[i].cx = 1; d[i].cy = 0; } cur = n; dfs(1, 2, 3, 4); for (int i = 1; i <= n; i++) printf( %d , neg[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int mod = 1e9 + 7; const long long inf = 1e17; char a[maxn], b[maxn], s[maxn]; int v[200]; int main() { int q; scanf( %s%s , a, b); int n = strlen(a); for (int i = 0; i < n; ++i) v[a[i]] = 1; n = strlen(b); scanf( %d , &q); while (q--) { scanf( %s , s); int len = strlen(s); int o = len - n + 1; if (o < 0) { puts( NO ); continue; } int i = 0, j = 0, k; while (i < n) { if (b[i] == s[j]) { i++; j++; } else if (b[i] == ? && v[s[j]]) { i++; j++; } else if (b[i] == * ) { for (k = 0; k < o && j < len; ++k, ++j) if (v[s[j]]) break; if (k < o) break; i++; } else { break; } } if (i < n || j < len) puts( NO ); else puts( YES ); } return 0; }
#include <bits/stdc++.h> using namespace std; struct hehe { unsigned long long l, r; mutable bool z; inline bool operator<(hehe y) const { return l < y.l; } }; set<hehe> s; inline set<hehe>::iterator split(unsigned long long pos) { hehe qaq; qaq.l = pos; qaq.r = 0; qaq.z = 0; set<hehe>::iterator itp = s.lower_bound(qaq); if (itp->l == pos && itp != s.end()) return itp; itp--; unsigned long long l = itp->l, r = itp->r; bool val = itp->z; s.erase(itp); qaq.l = l; qaq.r = pos - 1; qaq.z = val; s.insert(qaq); qaq.l = pos; qaq.r = r; qaq.z = val; set<hehe>::iterator ret = s.insert(qaq).first; return ret; } inline unsigned long long read() { unsigned long long s = 0; char ch = getchar(); while (ch < 0 || ch > 9 ) ch = getchar(); while (ch >= 0 && ch <= 9 ) s = s * 10 + ch - 0 , ch = getchar(); return s; } inline void rev(unsigned long long l, unsigned long long r) { set<hehe>::iterator itr, itl; itr = split(r + 1); itl = split(l); for (set<hehe>::iterator it = itl; it != itr; ++it) it->z ^= 1; } inline void fz(unsigned long long l, unsigned long long r, unsigned long long val) { set<hehe>::iterator itl, itr; itr = split(r + 1); itl = split(l); s.erase(itl, itr); hehe qwq; qwq.l = l; qwq.r = r; qwq.z = val; s.insert(qwq); } inline unsigned long long que() { for (set<hehe>::iterator it = s.begin(); it != s.end(); ++it) { if (it->z == 0) return it->l; } return 1000000000000000001llu; } signed main() { unsigned long long n; n = read(); hehe qwq; qwq.l = 1; qwq.r = 1000000000000000000llu; qwq.z = 0; s.insert(qwq); while (n--) { unsigned long long opt, l, r; opt = read(); l = read(); r = read(); if (opt == 1) fz(l, r, 1); else if (opt == 2) fz(l, r, 0); else if (opt == 3) rev(l, r); printf( %lld n , que()); } return 0; }
#include <bits/stdc++.h> using namespace std; multiset<int> st; multiset<int>::iterator it; int arr[200000]; int main() { int n, i, a, b, c, ans = 0, temp[3]; scanf( %d , &n); scanf( %d , &temp[0]); scanf( %d , &temp[1]); scanf( %d , &temp[2]); sort(temp, temp + 3); a = temp[0]; b = temp[1]; c = temp[2]; for (i = 0; i < n; i++) { scanf( %d , &arr[i]); st.insert(-arr[i]); } for (i = 0; i < n; i++) if (arr[i] > a + b + c) { printf( -1 n ); return 0; } while (!st.empty()) { it = st.lower_bound(-1 * (a + b + c)); if (it == st.end() or -1 * (*it) <= (b + c)) break; st.erase(it); ans++; } while (!st.empty()) { it = st.lower_bound(-1 * (b + c)); if (it == st.end() or -1 * (*it) <= (a + c)) break; st.erase(it); it = st.lower_bound(-1 * (a)); if (it != st.end()) st.erase(it); ans++; } while (!st.empty()) { it = st.lower_bound(-1 * (a + c)); if (it == st.end() or -1 * (*it) <= (max(a + b, c))) break; st.erase(it); it = st.lower_bound(-1 * (b)); if (it != st.end()) st.erase(it); ans++; } if ((a + b) > c) { while (!st.empty()) { it = st.lower_bound(-1 * (a + b)); if (it == st.end() or -1 * (*it) <= c) break; st.erase(it); it = st.lower_bound(-1 * (c)); if (it != st.end()) st.erase(it); ans++; } } while (!st.empty()) { it = st.lower_bound(-1 * (c)); if (it == st.end()) break; st.erase(it); it = st.lower_bound(-1 * (b)); if (it != st.end()) { st.erase(it); it = st.lower_bound(-1 * (a)); if (it != st.end()) st.erase(it); } else { it = st.lower_bound(-1 * (a + b)); if (it != st.end()) st.erase(it); } ans++; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; int best = 1e9; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; best = min(best, x - 1); best = min(best, n - x); best = min(best, y - 1); best = min(best, m - y); } if (best > 4) cout << NO n ; else cout << YES n ; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> ans1; vector<char> ans2; int n1, n2; cin >> n1; vector<int> a1(n1); for (int i = 0; i < n1; i++) { cin >> a1[i]; } cin >> n2; vector<int> a2(n2); for (int i = 0; i < n2; i++) { cin >> a2[i]; } int now1 = 0, now2 = 0; for (int i = 0; i < n2; i++) { int s = 0; while (now2 < n1 && s < a2[i]) { s += a1[now2]; now2++; } if (now2 > n1 || s != a2[i] || now1 == now2) { cout << NO ; return 0; } int temp = now1; for (int j = now1; j < now2; j++) { if (a1[temp] <= a1[j] && (j - 1 >= now1 && a1[j - 1] < a1[j] || j + 1 < now2 && a1[j + 1] < a1[j])) { temp = j; } } if (temp == now1 && temp + 1 < now2 && a1[temp + 1] == a1[temp]) { cout << NO ; return 0; } if (temp - 1 >= now1 && a1[temp] > a1[temp - 1]) { int him = a1[temp]; int j = temp - 1; while (j >= now1 && him > a1[j]) { him += a1[j]; ans1.push_back(i + j - now1 + 2); ans2.push_back( L ); j--; } j = temp + 1; while (j < now2 && him > a1[j]) { him += a1[j]; ans1.push_back(i + 1); ans2.push_back( R ); j++; } } else { if (temp + 1 < now2 && a1[temp] > a1[temp + 1]) { int him = a1[temp]; int j = temp + 1; while (j < now2 && him > a1[j]) { him += a1[j]; ans1.push_back(i + temp - now1 + 1); ans2.push_back( R ); j++; } j = temp - 1; while (j >= now1 && him > a1[j]) { him += a1[j]; ans1.push_back(i + j - now1 + 2); ans2.push_back( L ); j--; } } } now1 = now2; now2 = now1; } if (now2 != a1.size()) { cout << NO ; return 0; } cout << YES << endl; for (int i = 0; i < ans1.size(); i++) { cout << ans1[i] << << ans2[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int t; int n; long long dp[500010][2]; long long x; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n + 1; i++) { dp[i][0] = 0; dp[i][1] = 0; } dp[0][0] = 1; long long ans = 0; for (int i = 1; i <= n; i++) { cin >> x; x++; dp[x][0] = (dp[x][0] + dp[x][0]) % mod; dp[x][0] = (dp[x][0] + dp[x - 1][0]) % mod; dp[x][1] = (dp[x][1] + dp[x][1]) % mod; dp[x + 2][1] = (dp[x + 2][1] + dp[x + 2][1]) % mod; if (x > 1) { dp[x][1] = (dp[x][1] + dp[x - 2][0]) % mod; } } for (int i = 1; i <= n + 1; i++) { ans = (ans + dp[i][0]) % mod; ans = (ans + dp[i][1]) % mod; } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long t, n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; long long k = 0, l = 0; for (int i = 0; i < (n); i++) { long long a; cin >> a; if (a % 2 != i % 2) if (a % 2) k++; else l++; } if (k != l) cout << -1 n ; else cout << k << n ; } }
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int tot; struct node { node *ch[26], *fail; int len, id; node* init(int tlen) { memset(ch, 0, sizeof(ch)); fail = NULL; len = tlen; return this; } } * root, spT[N]; vector<int> g[N]; void addin(char* c) { node* tmp = root; while (*c != 0 ) { int t = *c - a ; if (tmp->ch[t] == NULL) tmp->ch[t] = spT[++tot].init(tmp->len + 1); tmp = tmp->ch[t]; c++; } } queue<node*> q; int en, st; void getfail() { q.push(root); while (!q.empty()) { node* tmp = q.front(); q.pop(); for (int t = 0; t < 26; t++) if (tmp->ch[t] != NULL) { if (tmp == root) tmp->ch[t]->fail = root; else { node* p = tmp->fail; while (p != root && p->ch[t] == NULL) p = p->fail; if (p->ch[t] != NULL) tmp->ch[t]->fail = p->ch[t]; else tmp->ch[t]->fail = root; } q.push(tmp->ch[t]); } } } char S[N]; int f[N][2]; int dfs(int x) { f[x][0] = 0; f[x][1] = 1; for (auto p : g[x]) { dfs(p); f[x][1] += f[p][0]; f[x][0] += max(f[p][0], f[p][1]); } return max(f[x][0], f[x][1]); } bool v[N]; int main() { int T, n; for (int i = (1); i <= (N - 1); i++) spT[i].id = i; cin >> T; while (T--) { root = spT[tot = 1].init(0); cin >> n; for (int i = (1); i <= (n); i++) { scanf( %s , S); addin(S); } getfail(); for (int i = (1); i <= (tot); i++) g[i].clear(), v[i] = 0; for (int i = (1); i <= (tot); i++) { node* tmp = &spT[i]; if (tmp->fail && tmp->fail != root && tmp->fail->len + 1 == tmp->len) { g[tmp->fail->id].push_back(tmp->id); v[tmp->id] = 1; } } int ans = 0; for (int i = (2); i <= (tot); i++) if (!v[i]) ans += dfs(i); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); string cad; cin >> cad; map<char, int> M; for (int i = 0; i < cad.size(); i++) M[cad[i]]++; int xx = M[ x ]; int yy = M[ y ]; int ans = max(xx, yy) - min(xx, yy); if (max(xx, yy) == M[ x ]) cout << string(ans, x ) << endl; else cout << string(ans, y ) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200 * 1000 + 85; int n, m; int a[N]; vector<int> vec[N]; pair<pair<int, int>, int> b[N]; int mapp[N]; int ans[N]; int seg[N << 2]; inline void add(int, int, int = 1, int = 0, int = N); inline int get(int, int = 1, int = 0, int = N); int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; mapp[a[i]] = i; } for (int i = 1; i <= n; i++) { int j = 1; while (j * i <= n) { if (mapp[j * i] >= mapp[i]) vec[mapp[j * i]].push_back(mapp[i]); if (mapp[i] > mapp[j * i]) vec[mapp[i]].push_back(mapp[j * i]); j++; } } for (int i = 0; i < m; i++) { cin >> b[i].first.second >> b[i].first.first; b[i].first.second--; b[i].first.first--; b[i].second = i; } sort(b, b + m); int k = 0; for (int t = 0; t < m; t++) { while (k <= b[t].first.first) { for (int i = 0; i < vec[k].size(); i++) add(0, vec[k][i] + 1); k++; } ans[b[t].second] = get(b[t].first.second); } for (int i = 0; i < m; i++) cout << ans[i] << n ; } inline void add(int st, int en, int id, int l, int r) { if (en <= l or r <= st) return; if (st <= l and r <= en) { seg[id]++; return; } int mid = (l + r) >> 1, lc = id << 1, rc = lc | 1; add(st, en, lc, l, mid); add(st, en, rc, mid, r); } inline int get(int idx, int id, int l, int r) { if (idx < l or r <= idx) return 0; if (l + 1 == r) return seg[id]; int mid = (l + r) >> 1, lc = id << 1, rc = lc | 1; return get(idx, lc, l, mid) + get(idx, rc, mid, r) + seg[id]; }
#include <bits/stdc++.h> using namespace std; int n, m, cnt; vector<int> e[105]; bool f[105]; void dfs(int x) { f[x] = 1, ++cnt; for (int i = 0; i < e[x].size(); ++i) if (!f[e[x][i]]) dfs(e[x][i]); } void work() { int i, x, y; scanf( %d%d , &n, &m); if (n != m) { puts( NO ); return; } for (i = 1; i <= m; ++i) { scanf( %d%d , &x, &y); e[x].push_back(y); e[y].push_back(x); } dfs(1); if (cnt == n) puts( FHTAGN! ); else puts( NO ); } int main() { work(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 2e2 + 10; int main() { int n, m; cin >> n >> m; int l = 1, r = INF; int ans = 0; while (l <= r) { int mid = (r + l) / 2; int z = mid / 6; int x = mid / 2 - z; int y = mid / 3 - z; if (z >= max(0, n - x) + max(0, m - y)) ans = mid, r = mid - 1; else l = mid + 1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int cnta[10], cntb[10]; int a[15][2], b[15][2]; int one_common(int x, int y) { if (a[x][0] == b[y][0] && a[x][1] != b[y][1]) return a[x][0]; if (a[x][1] == b[y][0] && a[x][0] != b[y][1]) return a[x][1]; if (a[x][0] == b[y][1] && a[x][1] != b[y][0]) return a[x][0]; if (a[x][1] == b[y][1] && a[x][0] != b[y][0]) return a[x][1]; return 0; } int main() { scanf( %d%d , &n, &m); int x, y; for (int i = 0; i < n; i++) { scanf( %d%d , &x, &y); cnta[x]++; cnta[y]++; a[i][0] = x; a[i][1] = y; } for (int i = 0; i < m; i++) { scanf( %d%d , &x, &y); cntb[x]++; cntb[y]++; b[i][0] = x; b[i][1] = y; } set<int> num; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (one_common(i, j)) num.insert(one_common(i, j)); } } if (num.size() == 1) { printf( %d n , *num.begin()); return 0; } for (int i = 0; i < n; i++) { set<int> num; for (int j = 0; j < m; j++) if (one_common(i, j)) num.insert(one_common(i, j)); if (num.size() > 1) { printf( -1 n ); return 0; } } for (int i = 0; i < m; i++) { set<int> num; for (int j = 0; j < n; j++) if (one_common(j, i)) num.insert(one_common(j, i)); if (num.size() > 1) { printf( -1 n ); return 0; } } printf( 0 n ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; const int MAX = 1e6; vector<bool> t(MAX + 1); for (int i = 0; i < n; ++i) { int x; cin >> x; t[x] = true; } int d = 0; vector<int> sol; for (int i = 1; i <= MAX / 2; ++i) { if (t[i] ^ t[MAX - i + 1]) { sol.push_back(t[i] ? MAX - i + 1 : i); t[i] = t[MAX - i + 1] = true; } else if (t[i]) { ++d; } } for (int i = 1; i <= MAX / 2 && d; ++i) { if (!t[i]) { sol.push_back(MAX - i + 1); sol.push_back(i); --d; } } cout << sol.size() << n ; for (int i : sol) { cout << i << ; } cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000000007; const long long INF64 = 2000000000000000007; const int eps = 10e-6; long long solve(vector<int> one, vector<int> two, vector<int> three) { long long min_w = INF64; for (auto x : one) { long long b = x; auto a = lower_bound(two.begin(), two.end(), b); auto c = upper_bound(three.begin(), three.end(), b); if (a == two.end() || c == three.begin()) continue; long long aa = two[a - two.begin()]; long long cc = three[c - three.begin() - 1]; min_w = min(min_w, (aa - b) * (aa - b) + (b - cc) * (b - cc) + (cc - aa) * (cc - aa)); } return min_w; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int k, l, m; cin >> k >> l >> m; vector<int> red(k); vector<int> green(l); vector<int> blue(m); for (int i = 0; i < k; i++) cin >> red[i]; for (int i = 0; i < l; i++) cin >> green[i]; for (int i = 0; i < m; i++) cin >> blue[i]; sort(red.begin(), red.end()); sort(blue.begin(), blue.end()); sort(green.begin(), green.end()); long long ans = INF64; ans = min(ans, solve(red, green, blue)); ans = min(ans, solve(red, blue, green)); ans = min(ans, solve(green, red, blue)); ans = min(ans, solve(green, blue, red)); ans = min(ans, solve(blue, green, red)); ans = min(ans, solve(blue, red, green)); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a[100001]; long long s[100001], ans[100001]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); reverse(a + 1, a + n + 1); for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i]; cin >> m; for (int i = 0; i < m; i++) { int k; cin >> k; if (!ans[k]) { long long res = 0; long long koef = 1, cnt = k, j0 = 2, j1; while (j0 <= n) { j1 = min((long long)n, j0 + cnt - 1); res += (s[j1] - s[j0 - 1]) * koef; j0 = j0 + cnt; koef++; cnt *= k; } ans[k] = res; } cout << ans[k] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char s[101]; cin >> s; int k = 1; for (int i = 1; s[i] != 0 ; ++i) { if (s[i] == s[i - 1]) k++; else k = 1; if (k == 7) break; } if (k >= 7) cout << YES ; else cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int> > AdjList; vector<int> dist; vector<int> unused; void update() { queue<int> now; for (int i = 0; i < unused.size(); i++) { dist[unused[i]] = 0; now.push(unused[i]); } while (!now.empty()) { int u = now.front(); now.pop(); for (int i = 0; i < AdjList[u].size(); i++) { int v = AdjList[u][i]; if (dist[u] + 1 < dist[v]) { dist[v] = dist[u] + 1; now.push(v); } } } unused.clear(); } int main() { int V, q; cin >> V >> q; AdjList.assign(V + 1, vector<int>()); dist.assign(V + 1, -1); for (int x, y, i = 0; i < V - 1; i++) { cin >> x >> y; AdjList[x].push_back(y); AdjList[y].push_back(x); } dist[1] = 0; queue<int> now; now.push(1); while (!now.empty()) { int u = now.front(); now.pop(); for (int i = 0; i < AdjList[u].size(); i++) { int v = AdjList[u][i]; if (dist[v] == -1) { dist[v] = dist[u] + 1; now.push(v); } } } for (int t, from, i = 0; i < q; i++) { cin >> t >> from; if (t == 1) { unused.push_back(from); } else { update(); cout << dist[from] << endl; } } return 0; }