solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; int a[N * 2], b[N]; vector<int> v[N]; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { int t, n, i, l, x, k; long long ret = 0; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d", &a[i]), a[i + n] = a[i]; for (i = 1; i < n; ++i) v[gcd(i, n)].push_back(i); for (t = 1; t < n; ++t) { if (v[t].size() == 0) continue; l = t, x = 0, k = 0; for (i = 0; i < l; ++i) b[i] = a[i]; for (i = l; i < n; ++i) b[i % l] = max(b[i % l], a[i]); for (i = 0; i < n * 2; ++i) { if (a[i] == b[i % l]) { ++k; if (x < v[t].size() && v[t][x] <= k) ++x; } else k = 0, x = 0; if (i >= n) ret += x; } } cout << ret << endl; }
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int n, limit; int nxt[N], lab[N], flag[N]; pair<int, int> a[N]; vector<int> ans1, ans2; vector<vector<int> > ans3; vector<pair<int, int> > v; int root(int u) { if (lab[u] < 0) return u; return lab[u] = root(lab[u]); } void join(int u, int v) { int l1 = root(u), l2 = root(v); if (l1 == l2) return; if (lab[l1] > lab[l2]) swap(l1, l2); lab[l1] += lab[l2]; lab[l2] = l1; } void show(vector<int> &s) { cout << s.size() << '\n'; for (auto &x : s) cout << x << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(0); cin >> n >> limit; memset(lab, -1, sizeof lab); for (int i = 1; i <= n; ++i) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { int j1 = i, j2 = i; while (j2 < n && a[j2 + 1].first == a[j2].first) j2++; for (; i <= j2; ++i) { while (j1 <= a[i].second && a[i].second <= j2 && a[i].second != i) swap(a[i], a[a[i].second]); } i = j2; } for (int i = 1; i <= n; ++i) if (a[i].second != i) { nxt[a[i].second] = i; join(a[i].second, i); v.push_back(a[i]); } for (int i = 0; i < v.size(); ++i) { while (i + 1 < v.size() && v[i + 1].first == v[i].first) { i++; int idx1 = v[i].second, idx2 = v[i - 1].second; if (root(idx1) == root(idx2)) continue; join(idx1, idx2); swap(nxt[idx1], nxt[idx2]); } } int sum = 0, tmp = 0; for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; tmp++; sum += abs(lab[root(a[i].second)]); } if (limit < sum) { cout << -1; return 0; } int addmx = limit - sum; int cnt = 0; memset(flag, 0, sizeof flag); for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; cnt++; if (cnt <= addmx) { ans2.push_back(a[i].second); int cur = a[i].second; do { ans1.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); } else { vector<int> cycle; int cur = a[i].second; do { cycle.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); ans3.push_back(cycle); } } if (ans1.size()) ans3.push_back(ans1); reverse(ans2.begin(), ans2.end()); if (ans2.size() > 1) ans3.push_back(ans2); cout << ans3.size() << '\n'; for (auto &s : ans3) show(s); return 0; }
11
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { x = 0; char c = getchar(); bool flag = false; while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } if (flag) x = -x; } int n, m, all = (1 << 6) - 1; int cnt[100010], ct[100010], siz[100010], val[100010], sta[100010]; char str[100010], t[100010]; bool check() { for (int s = 0; s <= all; ++s) if (val[s] < siz[s]) return false; return true; } int main() { scanf("%s", str + 1), n = strlen(str + 1), read(m); for (int i = 1; i <= n; ++i) cnt[str[i] - 'a']++, ct[1 << (str[i] - 'a')]++; for (int s = 0; s <= all; ++s) siz[s] = siz[s - (s & (-s))] + ct[(s & (-s))]; for (int i = 1; i <= m; ++i) { int pos, len; read(pos), scanf("%s", t + 1), len = strlen(t + 1); for (int j = 1; j <= len; ++j) sta[pos] |= (1 << (t[j] - 'a')); } for (int i = 1; i <= n; ++i) if (!sta[i]) sta[i] = all; for (int s = 0; s <= all; ++s) for (int i = 1; i <= n; ++i) if (s & sta[i]) val[s]++; if (!check()) { puts("Impossible"); return 0; } for (int i = 1; i <= n; ++i) { for (int s = 0; s <= all; ++s) if (s & sta[i]) val[s]--; for (int c = 0; c < 6; ++c) { if (!cnt[c] || !(sta[i] & (1 << c))) continue; for (int s = 0; s <= all; ++s) if (s & (1 << c)) siz[s]--; cnt[c]--; if (check()) { putchar('a' + c); break; } for (int s = 0; s <= all; ++s) if (s & (1 << c)) siz[s]++; cnt[c]++; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k, sum, res, c; cin >> n >> k; res = 240 - k, sum = 0, c = 0; for (int i = 1; i <= n; i++) { sum += 5 * i; if (sum > res) break; c = c + 1; } cout << c << "\n"; }
0
#include <bits/stdc++.h> using namespace std; template <class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; } template <class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; } const int MAXN = (1 << 20); int x1, x2, a, b; void read() { cin >> x1 >> x2 >> a >> b; } void first(int x) { cout << "FIRST" << '\n' << x << '\n'; exit(0); } void second() { cout << "SECOND" << '\n'; exit(0); } void draw() { cout << "DRAW" << '\n'; exit(0); } void solve() { int delta = x2 - x1; if (x1 + a <= x2 && x1 + b >= x2) first(x2); if (a <= 0 && b >= 0) draw(); if ((x1 < x2 && b < 0) || (x1 > x2 && b > 0)) draw(); if (delta < 0) { delta = -delta; int temp = -a; a = -b; b = temp; } int rem = delta % (a + b); if (rem == 0) second(); else if (rem < a || rem > b) draw(); else first(x1 < x2 ? x1 + rem : x1 - rem); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
8
#include <bits/stdc++.h> int a, v, l, d, w; double t; int main() { scanf("%d%d%d%d%d", &a, &v, &l, &d, &w); if (w > v) w = v; if (2 * a * d > w * w) { double t1 = sqrt((1.0 * w * w / a / a + 2.0 * d / a) / 2); if (a * t1 <= v) t = 2 * sqrt((1.0 * w * w / a / a + 2.0 * d / a) / 2) - 1.0 * w / a; else t = 1.0 * v / a + 1.0 * (v - w) / a + (d - v * v / 2.0 / a - (v * v - w * w) / 2.0 / a) / v; if (1.0 * (v * v - w * w) / 2 / a > l - d) t += (sqrt(w * w + 2 * a * (l - d)) - w) / a; else t += 1.0 * (v - w) / a + (l - d - (v * v - w * w) / (2.0 * a)) / v; } else if (2 * a * l > v * v) t = 1.0 * v / a + (l - v * v / 2.0 / a) / v; else t = sqrt(2.0 * l / a); printf("%.12lf", t); return 0; }
6
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector<ll> vi; typedef vector<vector<ll>> vvi; typedef vector<bool> vb; typedef pair<ll,ll> pii; #define fo(i,s,e_ex) for(i=s;i<e_ex;i++) #define Fo(i,k,n) for(i=k;k<n?i<=n:i>=n;k<n?i+=1:i-=1) #define endl '\n' #define MOD 1000000007//998244353 #define setbits(x) __builtin_popcountll(x) #define pbb push_back #define mpp make_pair #define ff first #define ss second #define all(x) x.begin(),x.end() #define mset(arr,val) memset(arr,val,sizeof(arr)) //cout<<fixed<<setprecision(10)<<pi; #define custpq(E,cm,nm) priority_queue<E,vector<E>,decltype(&cm)> nm(cm); void solve(ll caseno){ ll i,j,n,mn=INT_MAX,idx; cin>>n; vi arr(n); fo(i,0,n) { cin>>arr[i]; if(arr[i]<=mn){ mn=arr[i]; idx=i; } } cout<<n-1<<endl; for(i=0;i<n;i++){ if(i==idx) continue; cout<<i+1<<' '<<(idx)+1<<' '<<mn+abs(idx-i)<<' '<<mn<<endl; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); ll t=1; cin>>t; for(ll i=1;i<=t;i++){ //cout<<"Case #"<<i<<": "; solve(i); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; long long dp[55][55]; void init() { dp[0][0] = 1; for (int i = 1; i <= 52; i++) for (int j = 0; j <= i; j++) { dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; } } long long two(long long i) { return (long long)1 << i; } int p; void solve(long long n) { n++; int dig[55], tot = 0; int pos = -1; for (int i = 52; i >= 0; i--) if (two(i) & n) { pos = i; break; } for (int i = pos; i >= 0; i--) if (two(i) & n) dig[tot++] = 1; else dig[tot++] = 0; long long ans = 0; long long cnt = 0; for (int i = 0; i < tot; i++) if (dig[i]) { if (p >= cnt) ans += dp[tot - i - 1][p - cnt]; cnt++; } if (cnt == p) ans++; if (p == 1) ans--; cout << ans << endl; } int main() { ios::sync_with_stdio(false); init(); long long n, t; cin >> n >> t; int flag = 0; for (int i = 0; i < 52; i++) { if (two(i) == t) { flag = 1; p = i; } } p++; if (!flag) { puts("0"); return 0; } solve(n); }
6
#include <bits/stdc++.h> using namespace std; int p[100005]; int add[100005]; int f[100005]; int first[100005], second[100005], x[100005]; int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } int read() { int 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; } int main() { int n = read(), m = read(), i, j, k; for (i = 0; i < m; i++) first[i] = read(), second[i] = read(), x[i] = read(); for (i = 0; i <= 30; i++) { for (j = 1; j <= n + 1; j++) f[j] = j, p[j] = 0; for (j = 0; j < m; j++) if ((x[j] & (1 << i)) >> i) for (k = find(first[j]); k <= second[j]; k = find(f[k])) p[k] = 1, f[k] = f[k + 1]; for (j = 1; j <= n; j++) add[j] += p[j] * (1 << i), p[j] += p[j - 1]; for (j = 0; j < m; j++) if (!((x[j] & (1 << i)) >> i)) if (p[second[j]] - p[first[j] - 1] == second[j] - first[j] + 1) { cout << "NO"; return 0; } } puts("YES"); for (i = 1; i <= n; i++) printf("%d ", add[i]); }
5
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long int INF = 1e17; long long int power(long long int x, long long int y) { long long int t = 1; while (y > 0) { if (y % 2) y -= 1, t = t * x % mod; else y /= 2, x = x * x % mod; } return t; } long long int inverse(long long int q) { long long int t; t = power(q, mod - 2); return t; } bool cmp(pair<long long int, long long int> p1, pair<long long int, long long int> p2) { if (p1.second > p2.second) { return true; } else if (p1.second == p2.second) { if (p1.first < p2.first) { return true; } else { return false; } } else { return false; } } long long int col[1000001]; vector<long long int> v[1000001]; long long int vis[1000001] = {0}; set<long long int> s1[1000001]; void dfs(long long int x) { long long int cou = 0, i; for (i = 0; i < v[x].size(); i++) { if (col[x] != col[v[x][i]]) { s1[col[x]].insert(col[v[x][i]]); s1[col[v[x][i]]].insert(col[x]); } } } int main() { long long int n, m, i, j, k, l, x, y; cin >> n >> m; long long int ans = INT_MAX, mex = 0; for (i = 1; i < n + 1; i++) { cin >> col[i]; ans = min(ans, col[i]); } for (i = 0; i < m; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (i = 1; i <= n; i++) { dfs(i); } long long int res = 0; for (i = 1; i <= 1000000; i++) { if (s1[i].size() > res) { res = s1[i].size(); ans = i; } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; int n, m, cnt; long long ans; struct Edge { int x, y, c, id, u; inline friend bool operator<(const Edge &a, const Edge &b) { return a.c < b.c; } } E[200010]; inline bool cmp(Edge a, Edge b) { return a.id < b.id; } int g[200010], dep[200010]; int f[200010]; int fa[200010][20], fas[200010][20]; struct edge { int t, c, next; } e[400010]; inline void add_edge(int x, int y, int z) { e[++cnt].t = y; e[cnt].c = z; e[cnt].next = g[x]; g[x] = cnt; } inline int Find(int x) { return f[x] = x == f[x] ? x : Find(f[x]); } inline void dfs(int x) { for (int i = 1; i < 20; i++) { fa[x][i] = fa[fa[x][i - 1]][i - 1]; fas[x][i] = max(fas[fa[x][i - 1]][i - 1], fas[x][i - 1]); } for (int i = g[x]; i; i = e[i].next) if (!dep[e[i].t]) { dep[e[i].t] = dep[x] + 1; fa[e[i].t][0] = x; fas[e[i].t][0] = e[i].c; dfs(e[i].t); } } inline void swim(int &x, int h) { for (int i = 0; h; i++) { if (h & 1) x = fa[x][i]; h >>= 1; } } inline int LCA(int x, int y) { if (dep[x] < dep[y]) swap(x, y); swim(x, dep[x] - dep[y]); for (int i = 19; x != y; i--) if (fa[x][i] != fa[y][i] || i == 0) { x = fa[x][i]; y = fa[y][i]; if (i == 0) i++; } return x; } inline int lnma(int x, int y) { int h = dep[x] - dep[y], temp = 0; for (int i = 0; h; i++) { if (h & 1) { temp = max(temp, fas[x][i]); x = fa[x][i]; } h >>= 1; } return temp; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &E[i].x, &E[i].y, &E[i].c); E[i].id = i; } sort(E + 1, E + m + 1); for (int i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= m; i++) { if (Find(E[i].x) != Find(E[i].y)) { E[i].u = 1; f[Find(E[i].x)] = Find(E[i].y); ans += (long long)E[i].c; add_edge(E[i].x, E[i].y, E[i].c); add_edge(E[i].y, E[i].x, E[i].c); } } fas[1][0] = INF; fa[1][0] = 1; dep[1] = 1; dfs(1); sort(E + 1, E + m + 1, cmp); for (int i = 1; i <= m; i++) { int x = E[i].x, y = E[i].y; int z = LCA(x, y); if (E[i].u) printf("%I64d\n", ans); else { int temp = max(lnma(x, z), lnma(y, z)); printf("%I64d\n", ans - temp + E[i].c); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, m, col[100005]; long long sum[2], num[100005]; bool ex[2]; vector<int> edge[100005]; void dfs(int v, int c) { if (col[v] != -1) return; col[v] = c; sum[c] += num[v]; for (int i = 0; i < edge[v].size(); i++) { if (col[edge[v][i]] == -1) dfs(edge[v][i], 1 - c); else if (col[edge[v][i]] == c) { ex[c] = 1; } } } pair<int, int> zip[100050]; map<pair<int, int>, long long> val; int par[100005], ran[100005]; void init() { for (int i = 0; i < 100005; i++) par[i] = i; } int find(int x) { if (x == par[x]) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (ran[x] < ran[y]) par[x] = y; else { par[y] = x; if (ran[x] == ran[y]) ran[x]++; } } bool same(int x, int y) { return find(x) == find(y); } vector<int> G[100005]; long long DFS(int v, int u) { long long V = num[v]; for (int i = 0; i < G[v].size(); i++) { if (G[v][i] == u) continue; V -= DFS(G[v][i], v); } if (u == -1) assert(V == 0LL); else { val[make_pair(v, u)] = val[make_pair(u, v)] = V; } return V; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> num[i]; init(); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; zip[i] = make_pair(a, b); edge[a].push_back(b); edge[b].push_back(a); } memset(col, -1, sizeof(col)); dfs(1, 0); if (sum[0] != sum[1] && !ex[0] && !ex[1]) { puts("NO"); return 0; } puts("YES"); for (int i = 1; i <= n; i++) { for (int j = 0; j < edge[i].size(); j++) { if (i > edge[i][j]) continue; if (col[i] != col[edge[i][j]]) { continue; } long long hoge = sum[col[i]] - sum[1 - col[i]]; hoge /= 2; val[make_pair(i, edge[i][j])] = val[make_pair(edge[i][j], i)] = hoge; num[i] -= hoge; num[edge[i][j]] -= hoge; goto nxt; } } nxt:; for (int i = 1; i <= n; i++) { for (int j = 0; j < edge[i].size(); j++) { if (i > edge[i][j]) continue; if (col[i] != col[edge[i][j]]) { int a = i, b = edge[i][j]; if (!same(a, b)) { G[a].push_back(b); G[b].push_back(a); unite(a, b); } continue; } } } DFS(1, -1); for (int i = 0; i < m; i++) printf("%lld\n", val[zip[i]]); }
9
#include <bits/stdc++.h> using namespace std; template <class T> void smin(T& a, T val) { if (a > val) a = val; } template <class T> void smax(T& a, T val) { if (a < val) a = val; } template <typename T> inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { bool first = true; os << "["; for (auto i : v) { if (!first) os << ", "; os << i; first = false; } return os << "]"; } const int MAX = 100010; int n, m, suff[MAX], l, a[MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } set<int> s; for (int i = n; i >= 1; --i) { s.insert(a[i]); suff[i] = int(s.size()); } for (int i = 0; i < m; ++i) { cin >> l; cout << suff[l] << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 35, M = 1e9 + 7; int a[N * 2], k, tot; long long n; struct zz { int a[N][N]; void write() { for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) printf("%d ", a[i][j]); puts(""); } } } A[N * 2][N * 2], pre[N * 2][N * 2], suf[N * 2][N * 2]; zz cf(zz x, zz y) { zz z; memset(z.a, 0, sizeof z.a); for (int i = 0; i <= k; i++) for (int j = 0; j <= k; j++) for (int l = 0; l <= k; l++) z.a[i][j] = (z.a[i][j] + (long long)x.a[i][l] * y.a[l][j]) % M; return z; } int main() { scanf("%lld%d", &n, &k); while (n) { a[++tot] = n % k; n /= k; } for (int i = 0; i < k; i++) { for (int j = 0; j <= k; j++) A[0][i].a[j][j] = 1; for (int j = 0; j <= k; j++) A[0][i].a[i][j] = 1; } pre[0][0] = A[0][0]; suf[0][k - 1] = A[0][k - 1]; for (int i = 1; i < k; i++) pre[0][i] = cf(pre[0][i - 1], A[0][i]); for (int i = k - 2; i >= 0; i--) suf[0][i] = cf(A[0][i], suf[0][i + 1]); for (int i = 1; i < tot; i++) { A[i][0] = suf[i - 1][0]; for (int j = 1; j < k; j++) A[i][j] = cf(suf[i - 1][j], pre[i - 1][j - 1]); pre[i][0] = A[i][0]; suf[i][k - 1] = A[i][k - 1]; for (int j = 1; j < k; j++) pre[i][j] = cf(pre[i][j - 1], A[i][j]); for (int j = k - 2; j >= 0; j--) suf[i][j] = cf(A[i][j], suf[i][j + 1]); } zz Ans; int cnt = 0; memset(Ans.a, 0, sizeof Ans.a); for (int i = 0; i <= k; i++) Ans.a[i][i] = 1; for (int i = tot; i; i--) { for (int j = 0; j < a[i]; j++) Ans = cf(Ans, A[i - 1][(j + cnt) % k]); (cnt += a[i]) %= k; } int ans = 0; for (int i = 0; i <= k; i++) (ans += Ans.a[i][k]) %= M; printf("%d\n", ans); return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ind = 2, dup = n, ans = 0; if (n == 4) { cout << 1; return 0; } if (n == 7) { cout << 2; return 0; } while (dup > 10) { dup = dup / 10; ans += ind; ind = ind << 1; } ind = 1; while (n) { if (n % 10 == 7) ans += (ind); ind = ind << 1; n = n / 10; } cout << ans + 1 << endl; }
1
#include <bits/stdc++.h> int main() { int n, s, q; char c; scanf("%d%d", &n, &s); if (n == 1) { scanf("%d", &q); if (s > 0) printf("0"); else { printf("%d", q); } } else { fgetc(stdin); c = fgetc(stdin); c -= '0'; if (c > 1 && s > 0) { printf("1"); s--; } else printf("%d", c); n--; c = fgetc(stdin); c -= '0'; while (n > 0) { if (s > 0 && c > 0) { printf("0"); s--; } else printf("%d", c); n--; c = fgetc(stdin); c -= '0'; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long n, m, a, b; vector<int> grafo[505], cyc; int vis[505], cycle; bool ok; void find(int nodo) { vis[nodo]++; for (int i = 0; i < grafo[nodo].size() && ok; i++) { if ((nodo != a || grafo[nodo][i] != b) && vis[grafo[nodo][i]] < 2) { if (vis[grafo[nodo][i]] == 1) { ok = false; break; } else find(grafo[nodo][i]); } } vis[nodo]++; } void dfs(int nodo) { if (vis[nodo] == 1) { cycle = 1; vis[nodo]++; cyc.push_back(nodo); return; } vis[nodo]++; for (int i = 0; i < grafo[nodo].size(); i++) { if (vis[grafo[nodo][i]] != 2) dfs(grafo[nodo][i]); if (cycle > 0) break; } if (vis[nodo] == 2) { cycle = 2; cyc.push_back(nodo); return; } if (vis[nodo] == 1 && cycle == 1) cyc.push_back(nodo); vis[nodo]++; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; grafo[a].push_back(b); } for (int i = 1; i <= n; i++) { if (cycle == 0) dfs(i); else break; } ok = true; for (int i = 0; i < int(cyc.size()) - 1; i++) { ok = true; a = cyc[i + 1]; b = cyc[i]; for (int j = 0; j <= n; j++) vis[j] = 0; for (int j = 1; j <= n; j++) if (vis[j] == 0) find(j); if (ok == true) { break; } } if (ok) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int H, M, m; scanf("%d:%d\n", &H, &M); scanf("%d", &m); int h = m / 60; m = m % 60; H = ((H + h) + (M + m) / 60) % 24; M = (M + m) % 60; if (H < 10) printf("0"); printf("%d:", H); if (M < 10) printf("0"); printf("%d\n", M); return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(void) { cin.tie(0)->sync_with_stdio(false); int n; cin >> n; ll arr[n]; vector<ll> o; vector<ll> e; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] % 2) o.push_back(arr[i]); else e.push_back(arr[i]); } sort(o.begin(), o.end()); sort(e.begin(), e.end()); int num = min(o.size(), e.size()); o.resize(o.size() - num); e.resize(e.size() - num); ll sum = 0; if (o.size() == 0 && e.size() == 0) { cout << 0 << '\n'; return 0; } if (o.size() == 0) { for (int i = 0; i < e.size() - 1; i++) { sum += e[i]; } } else if (e.size() == 0) { for (int i = 0; i < o.size() - 1; i++) { sum += o[i]; } } cout << sum << '\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k; pair<int, int> a[105]; int main() { int i; cin >> n >> k; for (i = 1; i <= n; i++) { scanf("%d", &a[i].first); a[i].second = i; } sort(a + 1, a + n + 1); for (i = 1; i <= n; i++) { k -= a[i].first; if (k < 0) { n = i - 1; break; } } cout << n << endl; for (i = 1; i <= n; i++) { cout << a[i].second << ' '; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; int main() { long long a, b; cin >> a >> b; long long ans = 0; for (long long m = 1; m <= b - 1; m++) { long long now = a * (a + 1) / 2; now %= MOD; now *= m; now %= MOD; now *= b; now %= MOD; ans += now + a * m; ans %= MOD; } cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; pair<double, int> p[1005]; bool func(pair<double, int> a, pair<double, int> b) { if (a.first < b.first) return true; else if (a.first == b.first) return a.second > b.second; else return false; } int main() { int n, s; cin >> n >> s; int pop = 0; for (int i = 0; i < n; i++) { double x, y; scanf("%lf %lf %d", &x, &y, &p[i].second); p[i].first = sqrt(x * x + y * y); pop += p[i].second; } if ((1000000 - s) > pop) printf("-1\n"); else { sort(p, p + n, func); int now = 0; int id = 0; while (now < (1000000 - s)) { now += p[id].second; id++; } printf("%.8lf\n", p[id - 1].first); } }
2
#include <bits/stdc++.h> using namespace std; int convert(int A[], int K) { int n = 0; for (int i = K - 1, f = 1; i >= 0; i--, f *= 10) { n += A[i] * f; } return n; } int main() { int N, K; cin >> N >> K; cin.get(); int A[N][K]; for (int i = 0; i < N; i++) { for (int j = 0; j < K; j++) { A[i][j] = cin.get() - '0'; } cin.get(); } int P[K]; for (int i = 0; i < K; i++) P[i] = i; int B[N], pn[K], d, min_d = numeric_limits<int>::max(); do { for (int i = 0; i < N; i++) { for (int j = 0; j < K; j++) { pn[j] = A[i][P[j]]; } B[i] = convert(pn, K); } d = *max_element(B, B + N) - *min_element(B, B + N); min_d = min(d, min_d); } while (next_permutation(P, P + K)); cout << min_d << endl; }
3
#include <bits/stdc++.h> using namespace std; int n, i, j, sz; string ans, s1; char str[100007]; unordered_set<char> st[100007]; int main() { cin >> n; for (i = 1; i <= n; i++) { scanf("%s", str); sz = strlen(str); for (j = 0; j <= sz - 1; j++) if (str[j] != '?') st[j].insert(str[j]); } for (i = 0; i <= sz - 1; i++) if (st[i].size() == 1) printf("%c", *st[i].begin()); else if (st[i].size() == 0) printf("a"); else printf("?"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int MAXN = 1e5 + 100; pair<int, int> a[MAXN]; long long check(long long tl, long long tr) { if (tl == tr) return 0; if (tr == tl + 1) return 1; if (tr & 1) { if (tl & 1) return 0; return 1; } if (tl <= tr / 4) return check(tl, tr / 4); if (tl > (tr / 4) * 2) return ((tr - tl) & 1); else return 1; } long long find(long long tl, long long tr) { if (tl * 2 > tr) return 1; long long w = 0; w = tr / 2 + 3; while (2 * w > tr) w--; return check(tl, w); } int main() { int tt = 1; while (tt--) { long long n; cin >> n; for (int i = 0; i < n; i++) { long long x, y; cin >> x >> y; a[i] = {check(x, y), find(x, y)}; } long long first = 1, second = 0; for (int i = 0; i < n; i++) { if (first == 1 and second == 1) break; if (first == 0 and second == 0) break; if (second == 1) { a[i].first ^= 1; a[i].second ^= 1; } first = a[i].second; second = a[i].first; } cout << second << " " << first << endl; } return 0; }
9
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") int n, m, need[2000010], need2[2000010], tmp[2000010]; vector<int> orders[2000010]; vector<pair<int, int> > last; int can(int k) { for (int i = 1; i <= n; i++) { tmp[i] = 0; need2[i] = need[i]; } last.clear(); for (int i = 1; i <= k; i++) for (int j = 0; j < orders[i].size(); j++) tmp[orders[i][j]] = i; for (int i = 1; i <= n; i++) last.push_back(make_pair(tmp[i], i)); sort(last.begin(), last.end()); int used = 0; for (int i = 0; i < last.size(); i++) { need2[last[i].second] -= min(max((last[i].first - used), 0), need[last[i].second]); used += need[last[i].second] - need2[last[i].second]; } int remain = 0; for (int i = 1; i <= n; i++) remain += need2[i]; remain *= 2; return k >= remain + used; } void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> need[i]; int d, t; for (int i = 1; i <= m; i++) { cin >> d >> t; orders[d].push_back(t); } int l = 1, r = 4e5, m; while (l <= r) { m = (l + r) / 2; if (can(m)) r = m - 1; else l = m + 1; } if (can(r)) cout << r; else cout << l; } int main() { solve(); }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int x, y, z, a, b, c; int f = 0; cin >> x >> y >> z >> a >> b >> c; int q; if ((a + b + c) >= (x + y + z)) { if (a >= x) { a = a - x; if ((a + b) >= y) { q = a + b - y; if ((q + c) >= z) { cout << "YES" << endl; } else f = 1; } else { f = 1; } } else { f = 1; } } else f = 1; if (f == 1) cout << "NO" << endl; }
0
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long A, B, C, l, r, m; bool ok(long long n, long long i) { if (n == 0) return 0; long long diff = m - i * n; if (abs(diff) > r - l) return 0; if (diff <= 0) { C = l - diff, B = l; } else { B = r, C = r - diff; } return 1; } void solve() { cin >> l >> r >> m; for (long long i = l; i <= r; ++i) { long long n = m / i; A = i; if (ok(n, i)) { cout << A << " " << B << " " << C; break; } else if (ok(n + 1, i)) { cout << A << " " << B << " " << C; break; } } cout << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, x[200010]; scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < n; i++) { scanf("%d", &x[i]); } int c = 0; int ans; ans = 0; for (int i = 0; i < n; i++) { if (x[i] == 1) { if (a > 0) a--; else if (b > 0) { b--; c++; } else if (c > 0) c--; else ans++; } if (x[i] == 2) { if (b > 0) b--; else ans += 2; } } printf("%d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } const int mod = 1e9 + 7; const int inf = 1 << 30; const int maxn = 100005; vector<int> adj[maxn]; set<int> adjList[maxn]; int p[maxn], a[maxn], indeg[maxn]; int cont = 1; bool hasCycle(int V) { vector<int> color(V + 1); bool dir = true; function<bool(int, int)> dfs = [&](int u, int parent) { color[u] = 1; bool ans = false; int ret = 0; for (int v : adjList[u]) { if (color[v] == 0) ans |= dfs(v, u); else if (color[v] == 1 && (dir || v != parent || ret++)) ans = true; } color[u] = 2; return ans; }; for (int u = 1; u <= V; ++u) if (color[u] == 0 && dfs(u, -1)) return true; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; p[v] = u; adj[u].push_back(v); } set<int> valid; for (int i = 1; i <= n; i++) { cin >> a[i]; valid.insert(a[i]); } queue<pair<int, int> > q1; for (int i = 1; i <= n; i++) { if (p[i] == 0) { q1.push(pair<int, int>(i, i)); } } while (!q1.empty()) { auto e = q1.front(); q1.pop(); int u = e.first, f = e.second; if (u != f) { if (adjList[f].find(u) == adjList[f].end()) { adjList[f].insert(u); indeg[u]++; } } for (int v : adj[u]) { if (f != a[v]) { if (adjList[a[v]].find(f) == adjList[a[v]].end()) { adjList[a[v]].insert(f); indeg[f]++; } } q1.push(pair<int, int>(v, a[v])); } } if (hasCycle(n)) { cout << -1 << '\n'; return 0; } queue<int> q; for (int i = 1; i <= n; i++) { if (valid.find(i) == valid.end()) continue; if (indeg[i] == 0) q.push(i); } vector<int> ans; while (!q.empty()) { int u = q.front(); q.pop(); ans.push_back(u); for (int v : adjList[u]) { indeg[v]--; if (indeg[v] == 0) q.push(v); } } cout << ans.size() << '\n'; for (int x : ans) cout << x << " "; cout << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long int t, i, j, k; cin >> t; while (t--) { string a; cin >> a; long long int n; n = a.size(); if (n == 1) { cout << "0\n"; continue; } long long int count[n]; for (i = 0; i < n; i++) count[0] = 0; int flag = 0; for (i = 0; i < n; i++) { long long int left0 = 0, left1 = 0, right0 = 0, right1 = 0; for (j = i - 1; j >= 0; j--) { if (a[j] == '0') left0++; else left1++; } for (j = i; j < n; j++) { if (a[j] == '0') right0++; else right1++; } count[i] = min(right1 + left0, right0 + left1); if (count[i] == 0) { flag = 1; break; } } long long int min = count[0]; if (flag == 1) { cout << "0\n"; continue; } for (i = 0; i < n; i++) { if (count[i] < min) min = count[i]; } cout << min << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; const int N = 3 * 100111; vector<vector<int> > adj; int bl[N]; int hbl[N]; int shbl[N]; int dbl[N]; int main() { int n, p; cin >> n >> p; adj.resize(n); int a, b; for (int i = 0; i < (n); ++i) { cin >> a >> b; --a; --b; adj[a].push_back(b); adj[b].push_back(a); bl[a]++; bl[b]++; } for (int i = 0; i < (n); ++i) hbl[bl[i]]++; shbl[n - 1] = hbl[n - 1]; for (int i = n - 2; i >= 0; --i) shbl[i] = hbl[i] + shbl[i + 1]; long long ans = 0; int cans; for (int i = 0; i < (n); ++i) { int pm = p - bl[i]; if (pm <= 0) ans += n - 1; else { cans = shbl[pm]; if (bl[i] >= pm) cans--; for (int j = 0; j < (adj[i].size()); ++j) dbl[adj[i][j]]--; for (int j = 0; j < (adj[i].size()); ++j) { int v = adj[i][j]; if (bl[v] >= pm && bl[v] + dbl[v] < pm) --cans; dbl[v] = 0; } ans += cans; } } cout << ans / 2 << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; inline long long mexp(long long x, long long n, long long m = MOD) { long long res = 1; x %= m; while (n) { if (n & 1) res = (res * x) % m; n >>= 1; x = (x * x) % m; } return res; } int32_t main(void) { long long n, m, k, inv = 1; cin >> n >> m >> k; if (2 * k >= n || 2 * k >= m) { cout << 0 << '\n'; return 0; } long long res = 1; for (int i = 1; i <= 2 * k; ++i) { res = (res * (n - i) * (m - i)) % MOD; inv = (inv * i) % MOD; } inv = mexp(inv, MOD - 2, MOD); inv = (inv * inv) % MOD; cout << (res * inv) % MOD << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { string s, ans; int a, b, n; cin >> n >> a >> b; for (int i = 0; i <= a - b; i++) { s += 'a'; } for (int i = 0; i < b - 1; i++) { s += 'a' + i + 1; } for (int i = 0; i < n; i++) { ans += s[i % a]; } cout << ans << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N; scanf("%d", &N); if (N & 1) printf("%d\n", (N - 1) / 2); else { int i = 2; while (i * 2 <= N) i *= 2; printf("%d\n", (N - i) / 2); } }
7
#include <bits/stdc++.h> using namespace std; void game(char n, char p, int &rn, int &rp) { if ((n == 'R' && p == 'S') || (n == 'P' && p == 'R') || (n == 'S' && p == 'P')) rn++; else if ((p == 'R' && n == 'S') || (p == 'P' && n == 'R') || (p == 'S' && n == 'P')) rp++; } int main() { int n = 0, p = 0; long long g; cin >> g; string ns, ps; cin >> ns; cin >> ps; if (ns == ps) cout << 0 << " " << 0 << endl; else { for (long long i = 0; i < ns.length() * ps.length(); i++) { game(ns[i % ns.length()], ps[i % ps.length()], n, p); } p *= g / (ns.length() * ps.length()); n *= g / (ns.length() * ps.length()); for (long long i = 0; i < g % (ns.length() * ps.length()); i++) { game(ns[i % ns.length()], ps[i % ps.length()], n, p); } cout << p << " " << n << endl; } return 0; }
2
#include<bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define fo(i,n) for(int i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define ll long long #define si(x) scanf("%d",&x) #define sl(x) scanf("%lld",&x) #define ss(s) scanf("%s",s) #define pi(x) printf("%d\n",x) #define pl(x) printf("%lld\n",x) #define ps(s) printf("%s\n",s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #define PI 3.1415926535897932384626 typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0,lim-1); return uid(rang); } int mpow(int base, int exp); void ipgraph(int n, int m); void dfs(int u, int par); const int mod = 998244353 ; const int N = 1e6; // const int N = (2e5+10), M = N; //======================= const int INF = 1e9; vi g[N]; int a[N]; ll fac[N], inv[N], b[N]; void pre(){ int i; fac[0] = inv[0] = 1; fac[1] = inv[1] = 1; Fo(i, 2, N){ fac[i] = (i*fac[i-1])%mod; inv[i] = (mpow(i, mod-2)*inv[i-1])%mod; } } ll C(int n, int r){ if (r>n)return 0; ll ans = fac[n]; ans *= inv[r]; ans %= mod; ans *= inv[n-r]; ans %= mod; return ans; } void solve() { int n,m; cin>>n>>m; ll d[n][m]; fo(i,n){ fo(j,m)cin>>d[i][j]; } ll x = 0; fo(i,m){ vl t; fo(j,n){ t.pb( d[j][i]); } sortall(t); ll temp = 1; fo(j,n){ // if(t[j]-1-j <= 0) temp = 0; temp = temp*(t[j] - 1 - j); temp%=mod; } x+=((fac[n] - temp)%mod); // deb2(x, i); // deb(temp); x%=mod; } x = (x+mod)%mod; // cout<<x<<endl; // deb(fac[n]); ll y = inv[n]; y = (y+mod)%mod; // deb(y); cout<<((x*y) + mod)%mod; // fo(i,10){ // deb2(i, inv[i]); // } } int main() { pre(); #ifndef ONLINE_JUDGE freopen("DATA.in", "r", stdin); freopen("DATA.out", "w", stdout); #endif ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int t = 1; // cin >> t; while(t--) { solve(); } return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; } void ipgraph(int n, int m){ int i, u, v; while(m--){ cin>>u>>v; u--, v--; g[u].pb(v); g[v].pb(u); } } void dfs(int u, int par){ for(int v:g[u]){ if (v == par) continue; dfs(v, u); } }
6
#include <bits/stdc++.h> using namespace std; int main() { string hate = "I hate"; string love = "I love"; int n; cin >> n; if (n == 1) { cout << hate << " it" << endl; exit(0); } if (n == 2) { cout << hate << " that " << love << " it" << endl; exit(0); } for (int i = 1; i < n; i++) { if (i % 2) cout << hate << " that "; else cout << love << " that "; } if (n % 2) cout << hate << " it" << endl; else cout << love << " it"; }
0
#include <bits/stdc++.h> using namespace std; const int p = 10007; int n, m, f[510][210][210], a[310][310]; char s[210]; bool odd; int power(int x, int k) { int ret = 1; for (; k; k >>= 1, x = x * x % p) if (k & 1) ret = ret * x % p; return ret; } void gauss_elimination(int n) { for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) if (a[j][i]) { for (int k = i; k <= n; ++k) swap(a[i][k], a[j][k]); swap(a[i][0], a[j][0]); break; } int t = power(a[i][i], p - 2); for (int j = i; j <= n; ++j) (a[i][j] *= t) %= p; (a[i][0] *= t) %= p; for (int j = 1; j <= n; ++j) if (i != j && a[j][i]) { a[j][0] = (a[j][0] + p - a[j][i] * a[i][0] % p) % p; for (int k = n; k >= i; --k) a[j][k] = (a[j][k] + p - a[j][i] * a[i][k] % p) % p; } } } int main() { scanf("%s%d", s + 1, &m), m += n = strlen(s + 1); f[0][1][n] = 1; int x = n - 1, y = (n + 1) / 2; for (int i = 0; i <= x + y + n; ++i) { for (int j = 1; j <= n; ++j) for (int k = j; k <= n; ++k) if (f[i][j][k]) if (s[j] == s[k]) { (f[i + 1][j][k] += f[i][j][k] * 25) %= p; (f[i + 1][j + 1][max(j, k - 1)] += f[i][j][k]) %= p; } else { (f[i + 1][j][k] += f[i][j][k] * 24) %= p; (f[i + 1][j + 1][k] += f[i][j][k]) %= p; (f[i + 1][j][k - 1] += f[i][j][k]) %= p; } for (int j = 0; j <= n; ++j) (f[i + 1][j + 1][j] += f[i][j + 1][j] * 26) %= p; } for (int i = 1; i <= x + y + 1; ++i) { for (int j = 0; j <= n; ++j) if (m & 1) (a[i][0] += f[i - 1][j + 1][j] * 26 + f[i - 1][j][j]) %= p; else (a[i][0] += f[i][j + 1][j]) %= p; int t = power(24, i); for (int j = 1; j <= x; ++j) a[i][j] = t, (t *= i) %= p; t = power(25, i); for (int j = 1; j <= y; ++j) a[i][j + x] = t, (t *= i) %= p; a[i][x + y + 1] = power(26, i); } gauss_elimination(x + y + 1), m = (m + 1) / 2; int ans = power(26, m) * a[x + y + 1][0] % p, t = power(24, m); for (int j = 1; j <= x; ++j) (ans += a[j][0] * t) %= p, (t *= m % p) %= p; t = power(25, m); for (int j = 1; j <= y; ++j) (ans += a[j + x][0] * t) %= p, (t *= m % p) %= p; printf("%d\n", ans); return 0; }
11
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:66777216") using namespace std; const int N = 1001002; int a, b, c, d, i, j, n, m, k, tot, now, last, T; int nx[N][26], sib[N], pr[N], fir[N]; int cnt[N], en[N], pcnt[2][N]; char str[502]; int num[N]; int dp[2][2002][2002]; vector<int> lev[503]; int cfir[2002], ce[2002], ccnt[2002], csib[2002]; int dfs(int v, int h = 0) { lev[h].push_back(v); for (int _n((26) - 1), i(0); i <= _n; i++) { if (nx[v][i] != -1) cnt[v] += dfs(nx[v][i], h + 1); } return cnt[v]; } inline int g(int v, int c) { if (c < 0) return -1000000000; if (v == -1 && !c) return 0; if (v == -1) return -1000000000; if (c > pcnt[last][v]) return -1000000000; if (!c) return 0; return dp[last][v][c]; } inline int f(int v, int c) { if (c < 0) return -1000000000; if (v == -1 && !c) return 0; if (v == -1) return -1000000000; if (c > pcnt[now][v]) return -1000000000; if (!c) return 0; return dp[now][v][c]; } int rec(int v, int c) { if (c < 0) return -1000000000; if (v == -1 && !c) return 0; if (v == -1) return -1000000000; if (c > pcnt[now][v]) return -1000000000; if (!c) return 0; int ans = 0, fr = ccnt[v] - ce[v]; for (int i = (min(ccnt[v], c)), _b = (0); i >= _b; i--) { int lf = c - i; if (lf && csib[v] == -1) break; int v1 = g(cfir[v], min(fr, i)); if (v1 < 0) continue; int v2 = f(csib[v], lf); if (v2 < 0) continue; ans = max(ans, i * (i - 1) / 2 + v1 + v2); } return dp[now][v][c] = ans; } int main() { memset(nx, -1, sizeof(nx)); pr[0] = -1; k = 1; scanf("%d%d", &n, &tot); for (int _n((n)-1), i(0); i <= _n; i++) { scanf("%s", str); m = (int)strlen(str); a = 0; for (int _n((m)-1), i(0); i <= _n; i++) { if (nx[a][str[i] - 'a'] == -1) { nx[a][str[i] - 'a'] = k++; pr[k - 1] = a; } a = nx[a][str[i] - 'a']; } ++cnt[a]; ++en[a]; } for (int _n((k)-1), i(0); i <= _n; i++) { fir[i] = -1; for (int j = (25), _b = (0); j >= _b; j--) { if (nx[i][j] == -1) continue; sib[nx[i][j]] = fir[i]; fir[i] = nx[i][j]; } } dfs(0); memset((dp), 0, sizeof(dp)); now = 0, last = 1; for (int h = (500), _b = (1); h >= _b; h--) { if (lev[h].empty()) continue; now ^= 1; last ^= 1; for (int _n(((int)((lev[h]).size())) - 1), i(0); i <= _n; i++) num[lev[h][i]] = i; T = (int)((lev[h]).size()); for (int i = ((int)((lev[h]).size()) - 1), _b = (0); i >= _b; i--) { int v = lev[h][i]; if (sib[v] == -1) pcnt[now][i] = cnt[v]; else pcnt[now][i] = cnt[v] + pcnt[now][i + 1]; if (fir[v] == -1) cfir[i] = -1; else cfir[i] = num[fir[v]]; if (sib[v] == -1) csib[i] = -1; else csib[i] = i + 1; ce[i] = en[v]; ccnt[i] = cnt[v]; } for (int i = ((int)((lev[h]).size()) - 1), _b = (0); i >= _b; i--) { int v = lev[h][i]; for (int _n((tot + 1) - 1), j(0); j <= _n; j++) { if (j > pcnt[now][i]) break; rec(i, j); } } } int ans = dp[now][0][tot]; printf("%d\n", ans); }
7
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int X; void solve() { int n; cin >> n; ; vector<int> v; for (long long i = 0; (0 > n - 1) ? i >= n - 1 : i <= n - 1; (0 > n - 1) ? i -= 1 : i += 1) { cin >> X; v.push_back(X); }; unordered_set<int> s; for (long long i = 0; (0 > n - 1) ? i >= n - 1 : i <= n - 1; (0 > n - 1) ? i -= 1 : i += 1) { s.insert(v[i]); } if (s.size() == n) cout << "NO" << "\n"; else cout << "YES" << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; if (true) cin >> T; while (T--) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, minm1 = 3000000000, x, y, maxm1 = -1, A, B; cin >> n; while (n--) { cin >> x >> y; if (y < minm1) { minm1 = y; } if (x > maxm1) { maxm1 = x; } } long long int minm2 = 3000000000, maxm2 = -1; cin >> m; while (m--) { cin >> x >> y; if (x > maxm2) { maxm2 = x; } if (y < minm2) { minm2 = y; } } A = maxm2 - minm1; B = maxm1 - minm2; if (A < 0 && B < 0) cout << "0"; else { A = max(A, B); cout << A; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int max(int a, int b) { return (a > b) ? a : b; } int mc(int dp[], int a, int b, int c, int n) { if (n == 0) return 0; if (n < 0) return -2; if (dp[n] != -1) return dp[n]; dp[n] = max(max(mc(dp, a, b, c, n - a), mc(dp, a, b, c, n - b)), mc(dp, a, b, c, n - c)); if (dp[n] != -2) dp[n]++; return dp[n]; } int main() { int n, m; cin >> n >> m; int dp[n], a[n]; bool p[100001]; memset(dp, -1, sizeof dp); memset(p, false, sizeof p); for (int i = 0; i < n; i++) { cin >> a[i]; } p[a[n - 1]] = true; dp[n - 1] = 1; for (int i = n - 2; i >= 0; i--) { if (!p[a[i]]) { dp[i] = dp[i + 1] + 1; p[a[i]] = true; } else { dp[i] = dp[i + 1]; } } int l; for (int i = 0; i < m; i++) { cin >> l; l--; cout << dp[l] << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int mx = 1000; int v[mx][mx], n, m, ans[mx]; map<int, int> r; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int x = -1; for (int j = 0; j < n; j++) { cin >> v[i][j]; x = max(x, v[i][j]); } int mn = 1e9 + 100; for (int j = 0; j < n; j++) { if (v[i][j] == x) { mn = min(mn, j + 1); } } ans[i] = mn; ++r[mn]; } int x = -1; for (int i = 0; i < m; i++) { x = max(x, r[ans[i]]); } int mn = 1e9 + 100; for (int j = 0; j < n; j++) { if (r[ans[j]] == x) { mn = min(mn, ans[j]); } } cout << mn << endl; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int mod = 1e9 + 7; long long l, r, t; int main() { cin >> l >> r; cout << "YES" << endl; for (long long i = l; i <= r - 1; i += 2) { cout << i << " " << i + 1 << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> v(n); set<long long> s; for (long long x = 0; x < n; x++) { cin >> v[x]; } long long index = -1; if (v[n - 1] > v[n - 2]) { for (long long x = n - 1; x >= 1; x--) { if (v[x] < v[x - 1]) { index = x; break; } } cout << (index == -1 ? 0 : index) << "\n"; } else { long long count = 0; long long index = -1; for (long long x = n - 1; x > 0; x--) { if (v[x] < v[x - 1] && count == 1) { index = x; break; } if (v[x] > v[x - 1]) { count = 1; } } cout << (index == -1 ? 0 : index) << "\n"; } } }
2
#include <bits/stdc++.h> using namespace std; inline void R(int &v) { static char ch; v = 0; bool p = 0; do { ch = getchar(); if (ch == '-') p = 1; } while (!isdigit(ch)); while (isdigit(ch)) { v = (v + (v << 2) << 1) + (ch ^ '0'); ch = getchar(); } if (p) v = -v; } inline void R(long long &v) { static char ch; v = 0; bool p = 0; do { ch = getchar(); if (ch == '-') p = 1; } while (!isdigit(ch)); while (isdigit(ch)) { v = (v + (v << 2) << 1) + (ch ^ '0'); ch = getchar(); } if (p) v = -v; } inline void R(double &v) { static char ch; v = 0; bool p = 0; do { ch = getchar(); if (ch == '-') p = 1; } while (!isdigit(ch)); while (isdigit(ch)) { v = v * 10 + (ch ^ '0'); ch = getchar(); } if (p) v = -v; } int n, m; int dp[100005]; int k; int main() { R(k); dp[1] = 0; int tot; for (long long i = 2; i <= 100000; ++i) { dp[i] = dp[i / 2] + dp[(i + 1) / 2] + (i / 2) * ((i + 1) / 2); if (dp[i] > 100000) { tot = i; break; } } int temp; if (!k) { putchar('a'); return 0; } for (int i = 0; i < 26; ++i) { if (!k) break; temp = upper_bound(dp + 1, dp + tot + 1, k) - dp; for (int j = 1; j <= temp - 1; ++j) { putchar('a' + i); } k -= dp[temp - 1]; } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int n; pair<long long int, long long int> ar[3005]; long long int dp[3001][3001]; long long int find(long long int pos, long long int pre) { if (pos > n) return 0; if (dp[pos][pre] != 1e18) return dp[pos][pre]; long long int y = ar[pos].second + find(pos + 1, pos); long long int z = abs(ar[pre].first - ar[pos].first) + find(pos + 1, pre); return dp[pos][pre] = min(z, y); } bool comp(pair<long long int, long long int> a, pair<long long int, long long int> b) { return a.first < b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); for (int i = 0; i <= 3000; i++) { for (int j = 0; j <= 3000; j++) dp[i][j] = 1e18; } cin >> n; for (int i = 1; i <= n; i++) { cin >> ar[i].first >> ar[i].second; } sort(ar + 1, ar + n + 1, comp); long long int ans = find(2, 1) + ar[1].second; cout << ans << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; if (n <= 2) { cout << "1" << endl; cout << "1" << endl; return 0; } if (n == 3) { cout << "2" << endl; cout << "1 3" << endl; return 0; } cout << n << endl; deque<int> q; for (int i = 2; i <= n; i += 2) { q.push_front(i); } for (int i = 1; i <= n; i += 2) { q.push_front(i); } for (auto i : q) { cout << i << " "; } cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); cout << "\n"; } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; return 0; } void solve() { int n; cin >> n; int a[n]; int b[n]; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } int top = 0; int low{0}; int left = 0; int right = 0; int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j != i) { if (a[j] == a[i] && b[j] > b[i]) top = 1; else if (a[j] == a[i] && b[j] < b[i]) low = 1; else if (b[j] == b[i] && a[j] > a[i]) right = 1; else if (b[j] == b[i] && a[j] < a[i]) left = 1; } } if (top + left + right + low == 4) count++; top = left = right = low = 0; } cout << count << '\n'; }
1
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); const long long MAX_N = 2e5 + 7; vector<long long> g[MAX_N]; signed main() { ios::sync_with_stdio(0); cin.tie(0); long long q; cin >> q; long long sum_all = 0, sum_max = 0; set<long long> ma, ost, lightnings, fireballs; while (q--) { long long type, d; cin >> type >> d; sum_all += d; if (type == 0) { if (d > 0) { fireballs.insert(d); ost.insert(d); if (!lightnings.empty()) { long long mi = *ma.begin(); long long max_ost = *(--ost.end()); if (mi < max_ost) { ma.erase(mi); ost.erase(max_ost); ma.insert(max_ost); ost.insert(mi); sum_max += max_ost - mi; } } } else { d *= -1; fireballs.erase(d); if (ost.count(d)) ost.erase(d); if (ma.count(d)) { ma.erase(d); sum_max -= d; } if (ma.size() < lightnings.size()) { long long ma_ost = *(--ost.end()); ost.erase(ma_ost); ma.insert(ma_ost); sum_max += ma_ost; } } } else { if (d > 0) { lightnings.insert(d); ost.insert(d); long long ma_ost = *(--ost.end()); ost.erase(ma_ost); ma.insert(ma_ost); sum_max += ma_ost; } else { d *= -1; lightnings.erase(d); if (ost.count(d)) ost.erase(d); if (ma.count(d)) { ma.erase(d); sum_max -= d; } if (ma.size() > lightnings.size()) { long long mi = *ma.begin(); ma.erase(mi); sum_max -= mi; ost.insert(mi); } } } if (lightnings.empty()) { cout << sum_all << endl; } else if (fireballs.empty()) { long long mi_l = *lightnings.begin(); cout << sum_all + sum_max - mi_l << endl; } else { long long mi_l = *lightnings.begin(); long long ma_f = *(--fireballs.end()); if (mi_l > ma_f) { cout << sum_all + sum_max - mi_l + ma_f << endl; } else { cout << sum_all + sum_max << endl; } } } }
7
#include <bits/stdc++.h> const int Maxn = 1000 * 1000 + 10; using namespace std; int main() { vector<int> A[10000]; int B[10000]; int n; cin >> n; n *= 2; int e[10000]; for (int i = 0; i < n; i++) { int a; cin >> a; A[a].push_back(i); B[i] = a; } bool k = true; int size1 = 0, size2 = 0; int s1 = 0, s2 = 0; for (int i = 10; i <= 99; i++) { if (A[i].size() > 1) { for (int j = 0; j < 2; j++) { if (j % 2 == 0) e[A[i][A[i].size() - 1]] = 1; else e[A[i][A[i].size() - 1]] = 2; A[i].pop_back(); } s1++; s2++; size1++; size2++; } else if (A[i].size() == 0) continue; else { if (k) { e[A[i][0]] = 1; size1++; s1++; } else { s2++; size2++; e[A[i][0]] = 2; } k = !k; A[i].pop_back(); } } cout << s1 * s2 << endl; for (int i = 0; i < n; i++) { while (!A[B[i]].empty()) { if (size1 < n / 2) { e[A[B[i]][A[B[i]].size() - 1]] = 1; size1++; A[B[i]].pop_back(); } else { e[A[B[i]][A[B[i]].size() - 1]] = 2; size2++; A[B[i]].pop_back(); } } } for (int i = 0; i < n; i++) { cout << e[i] << " "; } cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int dp[8000][2]; int dgr[8000][2]; int main() { int n; cin >> n; int k1; cin >> k1; int kk1[7500], kk2[7500]; for (int i = 0; i < k1; i++) scanf("%d", &kk1[i]); int k2; cin >> k2; for (int i = 0; i < k2; i++) scanf("%d", &kk2[i]); memset(dp, 0, sizeof(dp)); dp[0][0] = 2, dp[0][1] = 2; queue<pair<int, int> > q; q.push(pair<int, int>(0, 0)); q.push(pair<int, int>(0, 1)); for (int i = 0; i < n; i++) dgr[i][0] = k1, dgr[i][1] = k2; while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); int x = p.first; int y = p.second; if (y == 1) { if (dp[x][y] == 2) { for (int i = 0; i < k1; i++) if (!dp[(x - kk1[i] + n) % n][0]) { dp[(x - kk1[i] + n) % n][0] = 1; q.push(pair<int, int>((x - kk1[i] + n) % n, 0)); } } else if (dp[x][y] == 1) { for (int i = 0; i < k1; i++) { if (!dp[(x - kk1[i] + n) % n][0]) dgr[(x - kk1[i] + n) % n][0]--; if (dgr[(x - kk1[i] + n) % n][0] == 0) { dp[(x - kk1[i] + n) % n][0] = 2; q.push(pair<int, int>((x - kk1[i] + n) % n, 0)); } } } } else { if (dp[x][y] == 2) { for (int i = 0; i < k2; i++) if (!dp[(x - kk2[i] + n) % n][1]) { dp[(x - kk2[i] + n) % n][1] = 1; q.push(pair<int, int>((x - kk2[i] + n) % n, 1)); } } else if (dp[x][y] == 1) { for (int i = 0; i < k2; i++) if (!dp[(x - kk2[i] + n) % n][1]) { dgr[(x - kk2[i] + n) % n][1]--; if (dgr[(x - kk2[i] + n) % n][1] == 0) { dp[(x - kk2[i] + n) % n][1] = 2; q.push(pair<int, int>((x - kk2[i] + n) % n, 1)); } } } } } for (int i = 1; i < n; i++) { if (dp[i][0] == 1) printf("Win "); if (dp[i][0] == 2) printf("Lose "); if (dp[i][0] == 0) printf("Loop "); } printf("\n"); for (int i = 1; i < n; i++) { if (dp[i][1] == 1) printf("Win "); if (dp[i][1] == 2) printf("Lose "); if (dp[i][1] == 0) printf("Loop "); } }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); string s, res = ""; int flag = 0; char ch = 'a'; cin >> s; for (int i = 0; i < s.size(); i++) { if (!flag && s[i] <= ch) { res += ch; ch++; } else { res += s[i]; } if (ch == 'z' + 1) { flag = 1; } } if (flag) { cout << res; } else cout << "-1"; }
2
#include <bits/stdc++.h> using namespace std; const int max_n = 500555, inf = 1000111222; struct tree { pair<long long, int> a[8 * max_n]; long long f[8 * max_n]; void build(int v, int l, int r) { if (l == r) { a[v] = {0, l}; f[v] = 0; return; } int mid = (l + r) / 2; build(2 * v, l, mid); build(2 * v + 1, mid + 1, r); a[v] = max(a[2 * v], a[2 * v + 1]); } void push(int v, int l, int r) { if (f[v] != 0) { f[2 * v] += f[v]; f[2 * v + 1] += f[v]; a[2 * v].first += f[v]; a[2 * v + 1].first += f[v]; f[v] = 0; } } void update(int v, int tl, int tr, int l, int r, int value) { if (tl == l && tr == r) { f[v] += value; a[v].first += value; return; } push(v, tl, tr); int mid = (tl + tr) / 2; if (r <= mid) { update(2 * v, tl, mid, l, r, value); } else if (l > mid) { update(2 * v + 1, mid + 1, tr, l, r, value); } else { update(2 * v, tl, mid, l, mid, value); update(2 * v + 1, mid + 1, tr, mid + 1, r, value); } a[v] = max(a[2 * v], a[2 * v + 1]); } pair<long long, int> get_max(int v, int tl, int tr, int l, int r) { if (tl == l && tr == r) { return a[v]; } push(v, tl, tr); int mid = (tl + tr) / 2; if (r <= mid) { return get_max(2 * v, tl, mid, l, r); } if (l > mid) { return get_max(2 * v + 1, mid + 1, tr, l, r); } return max(get_max(2 * v, tl, mid, l, mid), get_max(2 * v + 1, mid + 1, tr, mid + 1, r)); } }; struct point { int x, y, c, l, r; void read() { scanf("%d%d%d", &x, &y, &c); l = min(x, y); r = max(x, y); } bool operator<(const point &p) const { return l > p.l; } }; int n; point p[max_n]; vector<int> v; tree t; int compress(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { p[i].read(); v.push_back(p[i].l); v.push_back(p[i].r); } sort(p, p + n); sort(v.begin(), v.end()); v.push_back(inf); v.erase(unique(v.begin(), v.end()), v.end()); const int sz = v.size() - 1; t.build(1, 0, sz); for (int i = 0; i < v.size(); ++i) { t.update(1, 0, sz, i, i, -v[i]); } long long ans = 0; int l = inf, r = inf; for (int i = 0; i < n;) { int pos = i; while (i < n && p[i].l == p[pos].l) { t.update(1, 0, sz, compress(p[i].r), sz, p[i].c); ++i; } auto mx = t.get_max(1, 0, sz, compress(p[pos].l), sz); long long res = mx.first + p[pos].l; if (ans < res) { ans = res; l = p[pos].l; r = v[mx.second]; } } printf("%I64d\n%d %d %d %d\n", ans, l, l, r, r); return 0; }
8
#include <bits/stdc++.h> using namespace std; template <typename T> T BigMod(T b, T p, T m) { if (p == 0) return 1; if (p % 2 == 0) { T s = BigMod(b, p / 2, m); return ((s % m) * (s % m)) % m; } return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; } template <typename T> T ModInv(T b, T m) { return BigMod(b, m - 2, m); } template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { n = n * 10 + (ch - '0'); ch = getchar(); if (ch < '0' || ch > '9') break; } return (ng ? -n : n); } template <typename T> T POW(T B, T printf) { if (printf == 0) return 1; if (printf & 1) return B * POW(B, printf - 1); else return (POW(B, printf / 2) * POW(B, printf / 2)); } template <typename T> T Bigmod(T b, T p, T m) { if (p == 0) return 1; else if (!(p & 1)) return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m; else return ((b % m) * Bigmod(b, p - 1, m)) % m; } template <typename T> T Dis(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); } template <typename T> T Angle(T x1, T y1, T x2, T y2) { return atan(double(y1 - y2) / double(x1 - x2)); } template <typename T> T DIFF(T a, T b) { T d = a - b; if (d < 0) return -d; else return d; } template <typename T> T ABS(T a) { if (a < 0) return -a; else return a; } template <typename T> T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <typename T> T lcm(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } template <typename T> T euclide(T a, T b, T &x, T &y) { if (a < 0) { T d = euclide(-a, b, x, y); x = -x; return d; } if (b < 0) { T d = euclide(a, -b, x, y); y = -y; return d; } if (b == 0) { x = 1; y = 0; return a; } else { T d = euclide(b, a % b, x, y); T t = x; x = y; y = t - (a / b) * y; return d; } } template <typename T> void ia(T a[], int n) { for (int i = 0; i < n; i++) cin >> a[i]; } template <typename T> void pa(T a[], int n) { for (int i = 0; i < n - 1; i++) cout << a[i] << " "; cout << a[n - 1] << endl; } template <typename T> long long int isLeft(T a, T b, T c) { return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); } int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } template <class T, class first> inline T togglebit(T a, first i) { T t = 1; return (a ^ (t << i)); } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } int toLlint(string s) { long long int sm; stringstream ss(s); ss >> sm; return sm; } int cdigittoint(char ch) { return ch - '0'; } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); } double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); } long long int BIT[1000007]; int mxVal; int a[1000007]; void Update(int idx, long long int v) { while (idx <= mxVal) BIT[idx] = (BIT[idx] + v) % 1000000007, idx += idx & -idx; } long long int Query(int idx) { long long int ans = 0; while (idx) ans = (ans + BIT[idx]) % 1000000007, idx -= idx & -idx; return ans; } int main() { int(n); scanf("%d", &n); mxVal = 1000007 - 2; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { long long int prv = Query(a[i]); long long int now = ((prv * a[i]) % 1000000007 + a[i]) % 1000000007; long long int ext = (now - (Query(a[i]) - Query(a[i] - 1)) + 1000000007) % 1000000007; Update(a[i], ext); } printf("%lld\n", Query(mxVal)); return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long N = 200200; long long n, k; map<long long, long long> mp; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { cin >> n >> k; mp.clear(); for (long long i = 1; i <= n; i++) { long long x; cin >> x; mp[x]++; } for (long long i = 1; i <= n; i++) { long long x; cin >> x; } vector<pair<long long, long long>> v; for (auto i : mp) { v.push_back({i.first, i.second}); } n = v.size(); vector<long long> cnt(n); long long r = 0; long long tot = 0; multiset<long long> st; for (long long i = 0; i < n; i++) { while (r < n && v[r].first - v[i].first <= k) { tot += v[r].second; r++; } cnt[i] = tot; st.insert(cnt[i]); tot -= v[i].second; } long long ans = 0; r = 0; for (long long i = 0; i < n; i++) { while (r < n && v[r].first - v[i].first <= k) { st.erase(st.find(cnt[r])); r++; } if ((long long)st.size()) { ans = max(ans, cnt[i] + *st.rbegin()); } else { ans = max(ans, cnt[i]); } } cout << ans << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; void solve() {} int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int arr[n]; unordered_map<int, int> mp; for (int i = 0; i < n; ++i) { cin >> arr[i]; mp[arr[i]]++; } int count = mp[4]; count += mp[3]; mp[1] = max(0, mp[1] - mp[3]); count += (2 * mp[2] + mp[1] + 3) / 4; cout << count; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long X, Y; cin >> X >> Y; if (gcd(X, Y) > 1) { cout << "Impossible" << endl; return 0; } while (X != 0 && Y != 0) { if (X > Y) { long long d = X / Y; X %= Y; if (X == 0) cout << d - 1 << 'A'; else cout << d << 'A'; } else { long long d = Y / X; Y %= X; if (Y == 0) cout << d - 1 << 'B'; else cout << d << 'B'; } } cout << endl; }
8
#include <bits/stdc++.h> using namespace std; int T, n, k; long long ans; int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &k); ans = 0; int now = ceil((double)n / 2); for (int i = 1, x; i <= n * k; i++) { scanf("%d", &x); if (i <= (now - 1) * k) continue; if ((i - 1 - (now - 1) * k) % (n - now + 1) == 0) ans += x; } printf("%lld\n", ans); } return 0; }
0
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") template <class T> inline void RD(T &x) { char c; for (c = getchar(); c < '0'; c = getchar()) ; x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0'; } template <class T> inline void OT(const T &x) { cout << x << endl; } template <class T> inline T &_RD(T &x) { RD(x); return x; } inline int RD() { int x; RD(x); return x; } inline long long RD_LL() { int x; RD(x); return x; } inline double RD_DB() { double x; RD(x); return x; } template <class T0, class T1> inline void RD(T0 &x0, T1 &x1) { RD(x0), RD(x1); } template <class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2) { RD(x0), RD(x1), RD(x2); } template <class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { RD(x0), RD(x1), RD(x2), RD(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); } template <class T0, class T1> inline void OT(T0 &x0, T1 &x1) { OT(x0), OT(x1); } template <class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2) { OT(x0), OT(x1), OT(x2); } template <class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { OT(x0), OT(x1), OT(x2), OT(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); } template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); } template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) { RST(A0), RST(A1); } template <class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2) { RST(A0), RST(A1), RST(A2); } template <class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { RST(A0), RST(A1), RST(A2), RST(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); } template <class T> inline void CLR(T &A) { A.clear(); } template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) { CLR(A0), CLR(A1); } template <class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2) { CLR(A0), CLR(A1), CLR(A2); } template <class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { CLR(A0), CLR(A1), CLR(A2), CLR(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); } template <class T> inline void CLR(T &A, int n) { for (int i = 0; i < int(n); ++i) CLR(A[i]); } template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); } template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) { FLC(A0, x), FLC(A1, x); } template <class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2) { FLC(A0), FLC(A1), FLC(A2); } template <class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { FLC(A0), FLC(A1), FLC(A2), FLC(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6); } template <class T> inline void checkMin(T &a, T b) { if (b < a) a = b; } template <class T> inline void checkMax(T &a, T b) { if (b > a) a = b; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } const int MOD = 1000000007; const int INF = 0x7fffffff; const double EPS = 1e-6; const double OO = 1e15; const int N = 20000 + 5; bool A[3], B[3], _B[3], B_[3]; int x[3], m[3], l[3], u[3]; int res; void dfs() { int _i, i_; for (int i = 0; i < int(3); ++i) if (!_B[i]) { if (i == 0) _i = 1, i_ = 2; else if (i == 1) _i = 0, i_ = 2; else _i = 0, i_ = 1; if (B[i] && !B_[i]) { if (u[i] == i_) swap(_i, i_); B_[i] = true, _B[_i] = false; if (B[_i] && !B_[_i]) { checkMax(res, x[i] + l[i] + l[_i] + (A[i_] ? 0 : m[i_])); } else { int x_i = x[_i]; for (int j = int(-l[i]); j <= int(l[i]); ++j) if (j != 0) { int xx = x[i] + j; if (x[i_] != xx && (abs(x[i_] - xx) == 1 || j == 1 || j == l[i])) { checkMax(res, xx); x[_i] = xx, dfs(); x[_i] = x_i; } } } _B[_i] = true, B_[i] = false; } else { if (!B[i]) { if (!_B[_i] && x[_i] == x[i] + -1) { x[_i] = x[i], B[i] = _B[_i] = true, u[i] = _i, dfs(), x[_i] = x[i] + -1; B[i] = _B[_i] = false; }; if (!_B[_i] && x[_i] == x[i] + 1) { x[_i] = x[i], B[i] = _B[_i] = true, u[i] = _i, dfs(), x[_i] = x[i] + 1; B[i] = _B[_i] = false; }; if (!_B[i_] && x[i_] == x[i] + -1) { x[i_] = x[i], B[i] = _B[i_] = true, u[i] = i_, dfs(), x[i_] = x[i] + -1; B[i] = _B[i_] = false; }; if (!_B[i_] && x[i_] == x[i] + 1) { x[i_] = x[i], B[i] = _B[i_] = true, u[i] = i_, dfs(), x[i_] = x[i] + 1; B[i] = _B[i_] = false; }; } if (!A[i]) { A[i] = true; int xi = x[i]; for (int j = int(-m[i]); j <= int(m[i]); ++j) if (j != 0) { int xx = xi + j; if (x[_i] != xx && x[i_] != xx && (abs(x[_i] - xx) == 1 || abs(x[i_] - xx) == 1 || j == 1 || j == m[i])) { checkMax(res, xx); x[i] = xx, dfs(), x[i] = xi; } } A[i] = false; } } } } int main() { for (int i = 0; i < int(3); ++i) RD(x[i], m[i], l[i]); res = max(x[0], x[1], x[2]); dfs(); OT(res); }
8
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 7, MOD = 998244353; long long int n, fact[MAX], ans; long long int powMod(long long int a, long long int b) { long long int res = 1; while (b) { if (b & 1) res = (res * a) % MOD; b >>= 1; a = (a * a) % MOD; } return res; } int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); fact[0] = fact[1] = 1; for (long long int i = 2; i < MAX; i++) { fact[i] = (fact[i - 1] * i) % MOD; } cin >> n; ans = (n * fact[n]) % MOD; for (long long int i = 1; i < n; i++) { ans -= (fact[n] * powMod(fact[i], MOD - 2)) % MOD; ans = (ans + MOD) % MOD; } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a[105]; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); cout << a[n - k] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct ww { long double x, y; } a[100010]; int i, j, k, n, m, xx, yy; long double LU[100010], RU[100010], LD[100010], RD[100010]; inline bool judge(long double len) { int i, j; j = 0; for (i = 1; i <= n; i++) { for (; a[j + 1].x + len < a[i].x; j++) ; if (max(LU[j], RU[i + 1]) - min(LD[j], RD[i + 1]) <= len) return 1; } return 0; } inline bool cc1(const ww &a, const ww &b) { return a.x < b.x; } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d%d", &xx, &yy); a[i].x = yy - xx, a[i].y = xx + yy; } sort(a + 1, a + n + 1, cc1); LD[0] = RD[n + 1] = 1e10; LU[0] = RU[n + 1] = -1e10; for (i = 1; i <= n; i++) LD[i] = min(LD[i - 1], a[i].y), LU[i] = max(LU[i - 1], a[i].y); for (i = n; i; i--) RD[i] = min(RD[i + 1], a[i].y), RU[i] = max(RU[i + 1], a[i].y); long double l = 0, r = 1e10, mid; for (; l + 1e-7 < r;) { mid = (l + r) / 2; if (judge(mid)) r = mid; else l = mid; } double an = r; printf("%.10lf\n", an / 2); return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long minf = -(1e18); int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long n, m; cin >> n >> m; vector<long long> arr(n + 1); vector<long long> brr(m + 1); for (long long i = 1; i < n + 1; i++) cin >> arr[i]; for (long long i = 1; i < m + 1; i++) cin >> brr[i]; vector<long long> crr(n + 1); crr[1] = 1; for (long long i = 2; i < n + 1; i++) { crr[i] = crr[i - 1] + arr[i - 1]; } for (int i = 1; i <= m; i++) { long long x = brr[i]; std::vector<long long>::iterator low; low = std::lower_bound(crr.begin(), crr.end(), x); if (crr[low - crr.begin()] == x) { cout << low - crr.begin() << " 1" << endl; } else { cout << low - crr.begin() - 1 << " " << x - crr[low - crr.begin() - 1] + 1 << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; using lli = long long int; using pii = pair<int, int>; using vi = vector<int>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vlli = vector<long long int>; using vpii = vector<pair<int, int>>; const int N = 2000000; vlli prime; vb isprime(N + 5, true); void sieve() { isprime[1] = 1; for (int i = 2; i <= N; i++) if (isprime[i]) { for (int j = 2 * i; j <= N; j += i) isprime[j] = false; prime.push_back(i); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); sieve(); lli n, x, y, curr, ans = 1000111000111000111LL, z, g, l, r, m; cin >> n >> x >> y; vlli cnt(N + 5, 0), sum(N + 5, 0); for (int i = 0; i < n; i++) { cin >> z; cnt[z]++; sum[z] += z; } for (int i = 1; i <= N; i++) cnt[i] += cnt[i - 1], sum[i] += sum[i - 1]; for (int i = 0; i < prime.size(); i++) { g = prime[i]; curr = 0; for (int j = g; j <= N; j += g) { l = j - g; r = j; m = max(l + 1, j - (x / y)) - 1; curr += (cnt[m] - cnt[l]) * x; curr += ((cnt[r] - cnt[m]) * r - (sum[r] - sum[m])) * y; } ans = min(ans, curr); } cout << ans; }
6
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, k = 1111; set<int> s1, s2; cin >> a >> b >> c >> d; b -= a, d -= c; while (k--) { b += a; d += c; s1.insert(b); s2.insert(d); if (s1.count(d)) return cout << d, 0; if (s2.count(b)) return cout << b, 0; } return cout << -1, 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> v[121000], rv[121000]; struct dat { int pos, x, y, z; dat(int pos = 0, int x = 0, int y = 0, int z = 0) : pos(pos), x(x), y(y), z(z) {} } p[121000]; bool vis[121000]; int deg[121000]; bool operator<(dat a, dat b) { if (a.pos != b.pos) return a.pos < b.pos; } set<int> s; set<int>::iterator it; bool find(int i) { if (s.find(i) != s.end()) return 1; return 0; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= 4 * n; i++) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z), p[i].pos = i, v[p[i].x].push_back(i), rv[p[i].y].push_back(i), rv[p[i].z].push_back(i); for (int i = 1; i <= 4 * n; i++) vis[i] = false; for (int i = 1; i <= n; i++) deg[i] = 4; puts("YES"); for (int i = 1; i <= 4 * n; i++) s.insert(i); int cnt = 0; while (cnt < 4 * n) { if (s.empty()) break; it = s.begin(); int pos = *it; int x = p[pos].x; int y = p[pos].y; int z = p[pos].z; s.erase(it); if (vis[pos]) continue; vis[pos] = true; cnt++; printf("%d ", pos); deg[x]--; deg[y]++; deg[z]++; for (int i = 0; i < rv[x].size(); i++) { int j = rv[x][i]; if (vis[j]) continue; int y1 = p[j].y; int z1 = p[j].z; int x1 = p[j].x; deg[x1]--; deg[y1]++; deg[z1]++; if (deg[y1] <= 9 && deg[z1] <= 9) { if (!find(j)) s.insert(j); } else { if (find(j)) s.erase(j); } deg[x1]++; deg[y1]--; deg[z1]--; } for (int i = 0; i < rv[y].size(); i++) { int j = rv[y][i]; if (vis[j]) continue; int y1 = p[j].y; int z1 = p[j].z; int x1 = p[j].x; deg[x1]--; deg[y1]++; deg[z1]++; if (deg[y1] <= 9 && deg[z1] <= 9) { if (!find(j)) s.insert(j); } else { if (find(j)) s.erase(j); } deg[x1]++; deg[y1]--; deg[z1]--; } for (int i = 0; i < rv[z].size(); i++) { int j = rv[z][i]; if (vis[j]) continue; int y1 = p[j].y; int z1 = p[j].z; int x1 = p[j].x; deg[x1]--; deg[y1]++; deg[z1]++; if (deg[y1] <= 9 && deg[z1] <= 9) { if (!find(j)) s.insert(j); } else { if (find(j)) s.erase(j); } deg[x1]++; deg[y1]--; deg[z1]--; } } return 0; }
9
#include <bits/stdc++.h> using namespace std; int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12), 0); using Int = long long; const int DX[8] = {-1, 0, +1, -1, +1, -1, 0, +1}; const int DY[8] = {-1, -1, -1, 0, 0, +1, +1, +1}; int main() { int N, M; cin >> N >> M; vector<string> A(N); for (auto &s : A) cin >> s; vector<vector<int>> B(N, vector<int>(M, 1e9)); queue<tuple<int, int, int>> Q; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (A[i][j] == '.') { B[i][j] = 0; Q.emplace(0, i, j); } } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (A[i][j] == 'X' && (i == 0 || i == N - 1 || j == 0 || j == M - 1)) { B[i][j] = 1; Q.emplace(1, i, j); } } } while (!Q.empty()) { auto [k, i, j] = Q.front(); Q.pop(); for (int t = 0; t < 8; t++) { int ni = i + DY[t]; int nj = j + DX[t]; if (0 <= ni && ni < N && 0 <= nj && nj < M && B[ni][nj] > k + 1) { B[ni][nj] = k + 1; Q.emplace(k + 1, ni, nj); } } } auto solve = [&](int k) { vector<vector<int>> D(N + 1, vector<int>(M + 1)); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (B[i][j] > k) { if (i - k < 0 || j - k < 0 || i + k + 1 > N || j + k + 1 > M) return false; D[i - k][j - k] += 1; D[i - k][j + k + 1] -= 1; D[i + k + 1][j - k] -= 1; D[i + k + 1][j + k + 1] += 1; } } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { D[i][j + 1] += D[i][j]; } } for (int j = 0; j < M; j++) { for (int i = 0; i < N; i++) { D[i + 1][j] += D[i][j]; } } bool ok = true; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int a = (A[i][j] == 'X'); int b = (D[i][j] > 0); if (a != b) ok = false; } } return ok; }; int ok = 0, ng = min(N, M); while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; (solve(mid) ? ok : ng) = mid; } cout << ok << '\n'; if (ok == 0) { for (auto a : A) cout << a << '\n'; return 0; } else { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cout << (B[i][j] > ok ? 'X' : '.'); } cout << '\n'; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; int x, K, n, q; int state[205], pos[305], tot; long long dp[2][205], cost[35]; struct matrix { long long f[105][105]; matrix() { memset(f, 0, sizeof(f)); } friend matrix operator*(const matrix &a, const matrix &b) { matrix c; for (int i = 1; i <= tot; ++i) { for (int j = 1; j <= tot; ++j) { c.f[i][j] = 1000000000000000000LL; } } for (int i = 1; i <= tot; ++i) { for (int j = 1; j <= tot; ++j) { for (int k = 1; k <= tot; ++k) { c.f[i][j] = min(a.f[i][k] + b.f[k][j], c.f[i][j]); } } } return c; } } A, B, C; struct node { int p, val; } qry[35], nq[35]; int cnt; bool cmp(node a, node b) { return a.p < b.p; } void movestep(int num) { B = A; for (int i = 1; i <= tot; ++i) { for (int j = 1; j <= tot; ++j) { C.f[i][j] = 1000000000000000000LL; if (i == j) C.f[i][j] = 0; } } while (num) { if (num & 1) { C = B * C; } B = B * B; num >>= 1; } } void update(long long &x, const long long &y) { x = min(x, y); } int main() { scanf("%d%d%d%d", &x, &K, &n, &q); for (int i = 1; i <= K; ++i) scanf("%lld", &cost[i]); for (int i = 0; i < 1 << K; ++i) { int cnt = 0; for (int j = 0; j < K; ++j) { if (i & (1 << j)) ++cnt; } if (cnt == x) { state[++tot] = i; pos[i] = tot; } } for (int i = 1; i <= tot; i++) { for (int j = 1; j <= tot; j++) { A.f[i][j] = 1000000000000000000LL; } if (state[i] & 1) { for (int h = 1; h <= K; ++h) { if (!(state[i] & (1 << h))) { int trans = state[i] | (1 << h); trans >>= 1; A.f[i][pos[trans]] = cost[h]; } } } else { A.f[i][pos[state[i] >> 1]] = 0; } } for (int i = 1; i <= q; ++i) { scanf("%d%d", &qry[i].p, &qry[i].val); } sort(qry + 1, qry + q + 1, cmp); int now = 1, cur = 1; for (int i = 1; i <= tot; ++i) { dp[now][i] = 1000000000000000000LL; if (state[i] == (1 << x) - 1) { dp[now][i] = 0; } } for (int i = 1; i <= q; ++i) { int t = i; while (t < q && qry[t + 1].p - K <= qry[t].p) ++t; if (cur < qry[i].p - K) { if (qry[i].p - K - cur > 0) { movestep(qry[i].p - K - cur); for (int j = 1; j <= tot; ++j) dp[now ^ 1][j] = 1000000000000000000LL; for (int j = 1; j <= tot; ++j) { for (int h = 1; h <= tot; ++h) { update(dp[now ^ 1][j], dp[now][h] + C.f[h][j]); } } now ^= 1; cur = qry[i].p - K; } } while (cur < qry[t].p && cur < n - x + 1) { for (int h = 1; h <= tot; ++h) dp[now ^ 1][h] = 1000000000000000000LL; for (int h = 1; h <= tot; ++h) { if (!(state[h] & 1)) { update(dp[now ^ 1][pos[state[h] >> 1]], dp[now][h]); continue; } for (int l = 1; l <= K; ++l) { int S = state[h]; if (!((S >> l) & 1)) S |= 1 << l; else continue; S >>= 1; long long delta = cost[l]; for (int c = 1; c <= q; ++c) { if (cur + l == qry[c].p) delta += qry[c].val; } update(dp[now ^ 1][pos[S]], dp[now][h] + delta); } } now = now ^ 1; ++cur; } cur = qry[t].p; i = t; } if (cur < n - x + 1) { movestep(n - x + 1 - cur); for (int j = 1; j <= tot; ++j) dp[now ^ 1][j] = 1000000000000000000LL; for (int j = 1; j <= tot; ++j) { for (int h = 1; h <= tot; ++h) { update(dp[now ^ 1][j], dp[now][h] + C.f[h][j]); } } now ^= 1; } printf("%lld\n", dp[now][pos[(1 << x) - 1]]); return 0; }
10
#include <bits/stdc++.h> using namespace std; const long long N = 2005, mod = 1e9 + 7, base = 233, inv2 = (mod + 1) / 2; long long pre[2][N], suf[2][N], Hash[N], pw[N]; long long f[2][N][N], g[2][N][N], ans, cnt; long long n, m; char s[2][N], t[N]; long long hpre(long long o, long long l, long long r) { return (pre[o][r] - pre[o][l - 1] * pw[r - l + 1] % mod + mod) % mod; } long long hsuf(long long o, long long l, long long r) { return (suf[o][l] - suf[o][r + 1] * pw[r - l + 1] % mod + mod) % mod; } long long merge(long long x, long long y, long long len) { return (x * pw[len] % mod + y) % mod; } void add(long long &x, long long y) { x = (x + y) % mod; } void solve() { memset(f, 0, sizeof(f)); memset(g, 0, sizeof(g)); for (long long i = 1; i <= m; ++i) Hash[i] = (base * Hash[i - 1] % mod + (t[i] - 'a')) % mod; for (long long i : {0, 1}) for (long long j = 1; j <= n; ++j) for (long long k = 1; k <= j; ++k) { long long len = (j - k + 1) * 2; if (len > m) continue; long long x = hsuf(!i, k, j), y = hpre(i, k, j), z = Hash[len]; if (merge(x, y, j - k + 1) == z) len == m ? ++cnt : g[i][j][len] = 1; } for (long long i : {0, 1}) for (long long j = 1; j <= n; ++j) if (s[i][j] == t[1]) g[i][j][1] = 1; for (long long k = 1; k < m; ++k) for (long long i : {0, 1}) for (long long j = 1; j <= n; ++j) { if (j < n && s[i][j + 1] == t[k + 1]) add(f[i][j + 1][k + 1], g[i][j][k]), add(f[i][j + 1][k + 1], f[i][j][k]); if (s[!i][j] == t[k + 1]) add(g[!i][j][k + 1], f[i][j][k]); } for (long long i = m; i >= 1; --i) Hash[i] = (base * Hash[i + 1] % mod + (t[i] - 'a')) % mod; for (long long i : {0, 1}) { for (long long j = 1; j <= n; ++j) if (s[i][j] == t[m]) { if (m == 1) ++ans; add(ans, f[i][j - 1][m - 1] + g[i][j - 1][m - 1]); } for (long long j = 1; j <= n; ++j) for (long long k = j; k <= n; ++k) { long long len = (k - j + 1) * 2; if (len > m) continue; long long x = hpre(!i, j, k), y = hsuf(i, j, k), z = Hash[m - len + 1]; if (merge(x, y, k - j + 1) == z) (len == m && m != 2) ? (void)++cnt : add(ans, f[i][j - 1][m - len] + g[i][j - 1][m - len]); } } } signed main() { cin >> (s[0] + 1) >> (s[1] + 1); n = strlen(s[0] + 1); cin >> (t + 1); m = strlen(t + 1); pw[0] = 1; for (long long i = 1; i <= max(n, m); ++i) pw[i] = pw[i - 1] * base % mod; for (long long i : {0, 1}) { for (long long j = 1; j <= n; ++j) pre[i][j] = (pre[i][j - 1] * base % mod + (s[i][j] - 'a')) % mod; for (long long j = n; j >= 1; --j) suf[i][j] = (suf[i][j + 1] * base % mod + (s[i][j] - 'a')) % mod; } solve(); reverse(t + 1, t + m + 1); solve(); ans = (ans + cnt / 2) % mod; if (m == 1) ans = ans * inv2 % mod; cout << ans; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int n, p; cin >> n; if (n % 3 == 1) n--; if (n % 3 == 2) n++; n /= 3; cout << n / 12 << " " << n % 12 << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, k, temp; cin >> n >> k; int a[n]; double sum[n]; int max1 = -1; for (i = 0; i < n; i++) { cin >> a[i]; if (i) sum[i] = sum[i - 1] + a[i]; else sum[i] = a[i]; if (a[i] > max1) max1 = a[i]; } if (k == 1) { cout << fixed << setprecision(9) << max1 << endl; return 0; } double dp[n][n]; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (i) dp[i][j] = sum[j] - sum[i - 1]; else dp[i][j] = sum[j]; } } double m = -1; for (i = 0; i < n; i++) { for (j = i + k - 1; j < n; j++) { if ((dp[i][j] / (j - i + 1)) > m) m = dp[i][j] / (j - i + 1); } } cout << fixed << setprecision(9) << m << endl; }
2
#include <bits/stdc++.h> using namespace std; const int infi = 1e9; const long long int infl = 1e18; const int MOD = 1e9 + 7; long long int log_2(long long int n) { return (n > 1ll) ? 1ll + log_2(n / 2) : 0ll; } inline void add(long long int &a, long long int b) { a += b; if (a >= MOD) a -= MOD; } inline void sub(long long int &a, long long int b) { a -= b; if (a < 0) a += MOD; } inline string IntToString(long long int a) { char x[100]; sprintf(x, "%lld", a); string s = x; return s; } inline long long int StringToInt(string a) { char x[100]; long long int res; strcpy(x, a.c_str()); sscanf(x, "%lld", &res); return res; } inline string GetString(void) { char x[1000005]; scanf("%s", x); string s = x; return s; } inline string uppercase(string s) { int n = (int)s.size(); for (int(i) = (0); (i) < (n); ++(i)) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 'a' + 'A'; return s; } inline string lowercase(string s) { int n = (int)s.size(); for (int(i) = (0); (i) < (n); ++(i)) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a'; return s; } inline void OPEN(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } void showarr(long long int arr[], long long int n) { int i; for (i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } void showvec(vector<long long int> v) { int i; for (i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } int isPalindrome(string str) { int l = 0; int h = str.length() - 1; while (h > l) { if (str[l++] != str[h--]) { return 0; } } return 1; } void showvecpair(vector<pair<long long int, long long int> > v) { for (int i = 0; i < (int)v.size(); i++) { cout << v[i].first << " " << v[i].second << endl; } } bool sortbysec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } void bin(unsigned n) { unsigned i; for (i = 1 << 31; i > 0; i = i / 2) (n & i) ? printf("1") : printf("0"); } long long int cntset(long long int n) { bitset<64> b1(n); return b1.count(); } long long int p[100005]; void seive() { long long int i, j; for (i = 0; i < 100005; i++) p[i] = i; for (i = 2; i <= sqrt(100005); i++) { if (p[i] != i) continue; for (j = i * i; j < 100005; j += i) { if (p[j] == j) p[j] = i; } } return; } long long int power(long long int n, long long int x) { long long int j = n, prod = 1; while (x) { if (x % 2) { prod = ((prod % MOD) * (j % MOD)) % MOD; } j = (j * j) % MOD; x = x / 2; } return prod; } long long int fact(long long int n) { long long int i, prod = 1; for (i = 1; i <= n; i++) { prod = (prod * i) % MOD; } return prod; } void solve() { long long int a, b, c, d, x, y, n, m, k, l, i, j, Max = INT_MIN, Min = INT_MAX, ctr = 0, cnt = 0, ans = 1, prod = 1; char ch; cin >> n >> k; vector<pair<long long int, long long int> > dp, v; for (i = 0; i < n; i++) { cin >> a; v.push_back({a, i + 1}); } sort((v).begin(), (v).end()); ctr = 0; while (ctr < k) { if (v[n - 1].first == v[0].first) break; else { ctr++; v[n - 1].first--; v[0].first++; dp.push_back({v[n - 1].second, v[0].second}); sort((v).begin(), (v).end()); } } cout << v[n - 1].first - v[0].first << " " << ctr << endl; for (i = 0; i < dp.size(); i++) { cout << dp[i].first << " " << dp[i].second << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, T; T = 1; for (i = 1; i <= T; i++) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; inline bool muldiv(const char &c) { return c == '*' || c == '/'; } inline bool addsub(const char &c) { return c == '+' || c == '-'; } inline bool op(const char &c) { return addsub(c) || muldiv(c); } const int mod = 1000003; int d[2222][2222], a[2222], pos[2222], x[2222], y[2222]; int main() { string s; cin >> s; int n = s.size(), m = 0, t = 0; for (int i = 1; i < n; i++) if (op(s[i - 1]) && muldiv(s[i])) { cout << 0 << endl; return 0; } if (muldiv(s[0]) || op(s[n - 1])) { cout << 0 << endl; return 0; } for (int i = 0; i < n; i++) if (!isdigit(s[i])) t++; else if (!i || t) { a[m++] = t - 1; t = 0; } a[n = 0]++; for (int i = 0; i < m; i++) { pos[i] = n; for (int j = a[i]; j >= 0; j--) { x[n] = i; y[n++] = j; } } for (int i = n - 1; i >= 0; i--) { d[i][x[i]] = 1; for (int j = x[i] + 1; j < m; j++) { if (y[i]) d[i][j] = d[i + 1][j]; long long add = 0; for (int k = x[i]; k < j; k++) add += (long long)d[i][k] * d[pos[k + 1]][j]; d[i][j] = (d[i][j] + add) % mod; } } cout << d[0][m - 1] << endl; return 0; }
9
#include <bits/stdc++.h> char s[100010]; int cnt, last, n, num, tot; int main() { scanf("%s", s + 1); n = strlen(s + 1); for (int i = 1; i <= n; i++) { if (s[i] == '(') cnt++; else if (s[i] == ')') cnt--; else { last = i; cnt--; num++; } if (cnt < 0) { printf("-1"); return 0; } } tot = cnt; cnt = 0; for (int i = 1; i <= n; i++) { if (s[i] == '(') cnt++; else cnt--; if (i == last) cnt -= tot; if (cnt < 0) { printf("-1"); return 0; } } for (int i = 1; i <= num; i++) printf("%d\n", i == num ? tot + 1 : 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007LL; long long int a, b, n; int main() { cin >> a >> b >> n; n--; n = n % 6; long long int ans = 0; switch (n) { case (0): { ans = (a + mod) % mod; break; } case (1): { ans = (b + mod) % mod; break; } case (2): { ans = (b - a + 2 * mod) % mod; break; } case (3): { ans = (mod - a) % mod; break; } case (4): { ans = (mod - b) % mod; break; } case (5): { ans = (a - b + 2 * mod) % mod; break; } } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int y[2][370]; int main() { int n; cin >> n; char g; int a, b; for (int i = 0; i < n; i++) { cin >> g >> a >> b; for (int j = a; j <= b; j++) { y[g == 'F'][j]++; } } int maxres = 0; for (int i = 0; i < 370; i++) { maxres = max(maxres, min(y[0][i], y[1][i])); } cout << 2 * maxres; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 21 * 1e+8; int t, k, cnt, ans; bool week[7]; int solve(int x) { int c = k, days = 0; if (c > 1e+3) { days = (c - 1e+3 + 1) / cnt; c -= days * cnt; days *= 7; } while (c > 0) { c -= week[x % 7]; x++, days++; } return days; } int main() { cin >> t; while (t--) { ans = INF; cnt = 0; cin >> k; for (int i = 0; i < 7; i++) { cin >> week[i]; cnt += week[i]; } for (int i = 0; i < 7; i++) { ans = min(ans, solve(i)); } cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000 * 1000 * 1000 + 7; int n, m; char grid[256][256]; long long dp[256][1 << 15][2][2]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", grid[i]); if (n > m) { swap(n, m); for (int i = 0; i < max(n, m); i++) for (int j = i + 1; j < max(n, m); j++) swap(grid[i][j], grid[j][i]); } dp[0][0][0][0] = 1; for (int pos = 0; pos < n * m; pos++) for (int mask = 0; mask < (1 << n); mask++) for (int f1 = 0; f1 < 2; f1++) for (int f2 = 0; f2 < 2; f2++) { int lig = pos % n, col = pos / n; if (grid[lig][col] == 'x') { int nouv_mask = mask; if (nouv_mask & (1 << lig)) nouv_mask -= (1 << lig); dp[pos + 1][nouv_mask][0][f2] = (dp[pos + 1][nouv_mask][0][f2] + dp[pos][mask][f1][f2]) % MOD; } else { if (lig != n - 1) { bool is_protected = f1 || (mask & (1 << lig)); if (is_protected || !f2) dp[pos + 1][mask][f1][f2 + !is_protected] = (dp[pos + 1][mask][f1][f2 + !is_protected] + dp[pos][mask][f1][f2]) % MOD; dp[pos + 1][mask | (1 << lig)][1][f2] = (dp[pos + 1][mask | (1 << lig)][1][f2] + dp[pos][mask][f1][f2]) % MOD; } else { bool is_protected = f1 || (mask & (1 << lig)); if (is_protected || !f2) dp[pos + 1][mask][0][f2 + !is_protected] = (dp[pos + 1][mask][0][f2 + !is_protected] + dp[pos][mask][f1][f2]) % MOD; dp[pos + 1][mask | (1 << lig)][0][f2] = (dp[pos + 1][mask | (1 << lig)][0][f2] + dp[pos][mask][f1][f2]) % MOD; } } } long long ans = 0; for (int mask = 0; mask < (1 << n); mask++) for (int f1 = 0; f1 < 2; f1++) for (int f2 = 0; f2 < 2; f2++) ans = (ans + dp[n * m][mask][f1][f2]) % MOD; printf("%lld\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1000010; const int mod = 1000000007; char s[N]; long long a[N]; void ck(int pos, char c) { if (s[pos] != '?') return; if (s[pos - 1] != c && s[pos + 1] != c) s[pos] = c; } int main() { ios::sync_with_stdio(false), cin.tie(0); int n, m, t; cin >> t; s[0] = 'a'; while (t--) { cin >> s + 1; int len = strlen(s + 1); for (int i = 1; i <= len; i++) { if (s[i] == s[i + 1] && s[i] != '?') { puts("-1"); goto loop; } if (s[i] == '?') { ck(i, 'a'); ck(i, 'b'); ck(i, 'c'); } } printf("%s\n", s + 1); loop:; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; pair<pair<char, char>, int> p[110]; cin >> n; for (int i = 1; i <= n; i++) { string s; cin >> s; sort(s.begin(), s.end()); bool ok = true; for (int j = 1; j < s.size() - 1; j++) { if (s[j] != s[0] && s[j] != s[s.size() - 1]) { ok = false; break; } } if (!ok) continue; p[i] = make_pair(make_pair(s[0], s[s.length() - 1]), s.length()); } int ans = 0; for (int i = 0; i < 26; i++) { for (int j = i; j < 26; j++) { int cnt = 0; for (int k = 1; k <= n; k++) { if ((p[k].first.first == i + 'a' || p[k].first.first == j + 'a') && (p[k].first.second == i + 'a' || p[k].first.second == j + 'a')) { cnt += p[k].second; } } ans = max(ans, cnt); } } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { ll t; cin >> t; while (t--) { ll n; cin >> n; ll ar[n]; map<ll, ll> ma; set<ll> s; for (ll i = 0; i < n; i++) { cin >> ar[i]; ma[ar[i]]++; s.insert(ar[i]); } ll sum = 0; for (auto x : ma) sum += x.second; for (auto it = s.rbegin(); it != s.rend(); it++) { if (*it <= sum) break; else sum -= ma[*it]; } cout << sum + 1 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long int N = 2e5 + 5; void solve() { long long int n; cin >> n; vector<long long int> a(n), b(n); for (auto &it : a) cin >> it; for (auto &it : b) cin >> it; vector<long long int> v(n, 0); for (long long int i = 0; i < n; i++) { if (a[i] == b[i]) v[i] = a[i]; } long long int cnt = 0; for (long long int i = 0; i < n; i++) { if (v[i] == 0) cnt++; } set<long long int> s; for (long long int i = 0; i < n; i++) { s.insert(v[i]); } if (cnt == 1) { long long int x = 0; for (long long int i = 1; i <= n; i++) { if (s.find(i) == s.end()) { x = i; break; } } for (long long int i = 0; i < n; i++) { if (v[i] == 0) { v[i] = x; break; } } for (long long int i = 0; i < n; i++) cout << v[i] << " "; cout << "\n"; return; } if (cnt == 2) { for (long long int i = 0; i < n; i++) { if (v[i] == 0) { if (s.find(a[i]) != s.end() || s.find(b[i]) != s.end()) { if (s.find(a[i]) == s.end()) { v[i] = a[i]; s.insert(v[i]); } else { v[i] = b[i]; s.insert(v[i]); } } } } for (long long int i = 0; i < n; i++) { if (v[i] == 0) { if (s.find(a[i]) == s.end()) { v[i] = a[i]; s.insert(v[i]); } else if (s.find(b[i]) == s.end()) { v[i] = b[i]; s.insert(v[i]); } } } for (long long int i = 0; i < n; i++) cout << v[i] << " "; cout << "\n"; return; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int counts = 0, counts2 = 0; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; if (a < 0) counts++; else counts2++; } if (counts <= 1 || counts2 <= 1) cout << "Yes"; else cout << "No"; }
0
#include <bits/stdc++.h> using namespace std; void Erase(string& str) { if (str.find('X') != string::npos) str.erase(str.find('X'), 1); } int main() { string a, b, c, d; cin >> a >> b >> c >> d; Erase(a), Erase(b), Erase(c), Erase(d); reverse(b.begin(), b.end()); reverse(d.begin(), d.end()); a += b, a += a; c += d; a.find(c) == string::npos ? puts("NO") : puts("YES"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 50; int pre[30]; int minn[30]; char str[N]; int main() { int i, j; scanf("%s", str); int len = strlen(str); for (i = 0; i < 30; i++) minn[i] = 0; memset(pre, -1, sizeof(pre)); for (i = 0; i < len; i++) { int id = str[i] - 'a'; minn[id] = max(minn[id], i - pre[id]); pre[id] = i; } for (i = 0; i < 26; i++) { minn[i] = max(minn[i], len - pre[i]); } int res = N; for (i = 0; i < 26; i++) { if (minn[i] == 0) continue; res = min(res, minn[i]); } printf("%d\n", res); return 0; }
3
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = 0; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? x : -x; } long long n, f[105][5], ans; char ch[105]; void dp() { for (long long i = 1; i < n; i++) { f[i + 1][0] = (ch[i] == 'A') ? f[i][0] + f[i][1] : f[i][1]; if (ch[i + 1] == 'A') { if (ch[i] == 'B') f[i + 1][1] = f[i][0]; } else f[i + 1][1] = f[i][1]; } } signed main() { scanf("%s", ch + 1); n = strlen(ch + 1); ch[++n] = ch[1]; f[1][0] = 1; dp(); ans = f[n][0]; memset(f, 0, sizeof(f)); f[1][1] = 1; dp(); ans += f[n][1]; printf("%lld", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; priority_queue<long long int, vector<long long int>, greater<long long int> > pq; int main() { ios_base::sync_with_stdio(0); long long int n, i, s = 0, j, k, p = 0; cin >> n; for (i = 0; i < n; i++) { cin >> k; pq.push(k); } while (!pq.empty()) { if (p <= pq.top()) { p = p + pq.top(); s++; } pq.pop(); } cout << s << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e9 + 9; const long long N = 500500; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long T; cin >> T; while (T--) { long long n, k; cin >> n >> k; string s, t; cin >> s; vector<pair<long long, long long> > ans; for (long long i = 1; i <= k - 1; i++) { t += "()"; } for (long long i = 1; i <= (n - (2 * k) + 2) / 2; i++) { t += "("; } for (long long i = 1; i <= (n - (2 * k) + 2) / 2; i++) { t += ")"; } for (long long i = 0; i < n; i++) { if (s[i] == t[i]) continue; for (long long j = i + 1; j < n; j++) { if (s[j] == t[i]) { ans.push_back({i + 1, j + 1}); reverse(s.begin() + i, s.begin() + j + 1); break; } } } cout << ans.size() << '\n'; for (auto z : ans) { cout << z.first << " " << z.second << '\n'; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const int N = 300005; int n, m; vector<int> v[N]; vector<int> a[N]; int c[N]; void dfs(int x, int p) { set<int> s; for (int i = 0; i < v[x].size(); ++i) { int y = v[x][i]; if (c[y]) s.insert(c[y]); } int z = 1; for (int i = 0; i < v[x].size(); ++i) { int y = v[x][i]; if (c[y]) continue; while (s.find(z) != s.end()) ++z; c[y] = z; s.insert(z); } for (int i = 0; i < a[x].size(); ++i) { int h = a[x][i]; if (h == p) continue; dfs(h, x); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { int q; scanf("%d", &q); while (q--) { int x; scanf("%d", &x); v[i].push_back(x); } } for (int i = 0; i < n - 1; ++i) { int x, y; scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } dfs(1, 1); int ans = 0; for (int i = 1; i <= m; ++i) { if (!c[i]) c[i] = 1; ans = max(ans, c[i]); } printf("%d\n", ans); for (int i = 1; i <= m; ++i) printf("%d ", c[i]); printf("\n"); return 0; }
7
#include <bits/stdc++.h> using namespace std; int N, R, C, a[1000010][30], sum[1000010]; string str[1000010]; char s[5100000]; int id, mx, len[1000010]; int find_root(int k, int r) { int u = k; for (int i = 0; i < 30; i++) { if (r & (1 << i)) u = a[u][i]; } return u - k; } int main() { scanf("%d%d%d", &N, &R, &C); int cu = 0; for (int i = 1; i <= N; i++) { scanf("%s", s + cu); len[i] = strlen(s + cu); if (i > 1) sum[i] = len[i] + sum[i - 1]; else sum[i] = len[i]; cu += len[i]; } for (int i = 1; i <= N; i++) { int ed = max(i, a[i - 1][0]); while (ed <= N && ed - i + sum[ed] - sum[i - 1] <= C) ed++; a[i][0] = ed; } for (int j = 0; j < 30; j++) a[N + 1][j] = N + 1; for (int j = 1; j < 30; j++) for (int i = 1; i <= N; i++) a[i][j] = a[a[i][j - 1]][j - 1]; id = 1; mx = 0; for (int i = 1; i <= N; i++) { int cnt = find_root(i, R); if (mx < cnt) mx = cnt, id = i; } int cur = 0, pos = -1; for (int i = id; i <= N && cur < R; i++) { if (pos + len[i] >= C) { cur++; puts(""); if (cur == R) return 0; pos = -1; } if (pos + len[i] >= C) return 0; if (pos >= 0) putchar(' '); for (int j = sum[i - 1]; j < sum[i]; j++) putchar(s[j]); pos += len[i] + 1; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n; long long a[105]; long long k = 0; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; k = a[0] - a[1]; if (n > 2) { for (int i = 1; i < n - 1; i++) { if (k != a[i] - a[i + 1]) { cout << a[n - 1] << '\n'; return 0; } } } cout << a[n - 1] - k << '\n'; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> T POW(T B, T printf) { if (printf == 0) return 1; if (printf & 1) return B * POW(B, printf - 1); else return (POW(B, printf / 2) * POW(B, printf / 2)); } template <typename T> T BIGMOD(T b, T p, T m) { if (p == 0) return 1; else if (!(p & 1)) return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m; else return ((b % m) * Bigmod(b, p - 1, m)) % m; } template <typename T> T DIS(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); } template <typename T> T ANGLE(T x1, T y1, T x2, T y2) { return atan(double(y1 - y2) / double(x1 - x2)); } template <typename T> T DIFF(T a, T b) { T d = a - b; if (d < 0) return -d; else return d; } template <typename T> T ABS(T a) { if (a < 0) return -a; else return a; } template <typename T> T GCD(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <typename T> T LCM(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } template <typename T> T EUCLIDE(T a, T b, T &x, T &y) { if (a < 0) { T d = euclide(-a, b, x, y); x = -x; return d; } if (b < 0) { T d = euclide(a, -b, x, y); y = -y; return d; } if (b == 0) { x = 1; y = 0; return a; } else { T d = euclide(b, a % b, x, y); T t = x; x = y; y = t - (a / b) * y; return d; } } template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { n = n * 10 + (ch - '0'); ch = getchar(); if (ch < '0' || ch > '9') break; } return (ng ? -n : n); } template <typename T> long long int isLeft(T a, T b, T c) { return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y); } int Check(int N, int pos) { return (N & (1 << pos)); } int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } double DEG(double x) { return (180.0 * x) / ((2.0 * acos(0.0))); } double RAD(double x) { return (x * (double)(2.0 * acos(0.0))) / (180.0); } void P_ARR(int *ar, int a, int b) { if (a > b) swap(a, b); if (a <= b) cout << ar[a]; for (int i = a + 1; i <= b; i++) cout << " " << ar[i]; cout << endl; } void P_BIN(int n) { if (n == 0) return; P_BIN(n / 2); printf("%d", n % 2); } long long int ar[100007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int tc, cas = 1; long long int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z; cin >> n; for (i = 1; i <= n; i++) cin >> ar[i]; long long int low = ar[1]; long long int mid, high = 1000000000000; long long int ans; while (low <= high) { mid = (low + high) >> 1; bool flag = true; long long int res = mid; for (i = 1; i <= n; i++) { res += ar[i - 1] - ar[i]; if (res < 0) { flag = false; break; } } if (flag) { ans = mid; high = mid - 1; } else { low = mid + 1; } } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAX = 51; int n, m; char a[MAX][MAX]; void solve() { int i, j, minr = 52, maxr = -1, minl = 52, maxl = -1; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (a[i][j] == '*') { minr = min(minr, i); maxr = max(maxr, i); minl = min(minl, j); maxl = max(maxl, j); } } } for (i = minr; i <= maxr; i++) { for (j = minl; j <= maxl; j++) { printf("%c", a[i][j]); } puts(""); } } int main() { int i, j; while (scanf("%d%d", &n, &m) == 2) { getchar(); for (i = 0; i < n; i++) { gets(a[i]); } solve(); } fclose(stdin); fclose(stdout); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n; cin >> n; int cnt = 0; while (n > 1) { if (n % 5 == 0) { n = (4 * n) / 5; cnt++; continue; } else if (n % 3 == 0) { n = (2 * n) / 3; cnt++; continue; } else if (n % 2 == 0) { n = (n) / 2; cnt++; continue; } else { cnt = -1; break; } } cout << cnt << endl; } }
0
#include <bits/stdc++.h> using namespace std; struct item { bool l; int c; }; int main(void) { ios_base::sync_with_stdio(false); string s1, s2; cin >> s1 >> s2; if (s1.length() < s2.length()) { cout << "need tree"; return 0; } bool aut = false, ar = false; int i = 0; for (; i < s1.length(); i++) { if (s1[i] == s2[0]) break; } if (i == s1.length()) { cout << "need tree"; return 0; } for (int j = 1; j < s2.length(); j++) { for (; i < s1.length(); i++) { if (s1[i] == s2[j]) break; } if (i == s1.length()) { ar = true; break; } i++; } int s_1[26] = {0}; int s_2[26] = {0}; for (int i = 0; i < s1.length(); i++) s_1[s1[i] - 'a']++; for (int i = 0; i < s2.length(); i++) s_2[s2[i] - 'a']++; for (int i = 0; i < 26; i++) { if (s_1[i] < s_2[i]) { cout << "need tree"; return 0; } else if (s_1[i] > s_2[i]) { aut = true; } } if (aut && ar) cout << "both"; else if (aut) cout << "automaton"; else if (ar) cout << "array"; return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ll md = 1000000007; vector<ll> p, fac(2, 1), ifac(2, 1); ll add(ll a, ll b, ll mod = md) { return (a % mod + b % mod + mod) % mod; } ll mul(ll a, ll b, ll mod = md) { return ((a % mod) * (b % mod)) % mod; } ll power(ll x, ll n, ll mod = md) { ll res = 1; x %= mod; while (n) { if (n & 1) res = ((res % mod) * (x % mod)) % mod; n = (n >> 1); x = ((x % mod) * (x % mod)) % mod; } return res; } void cachefact(ll lim, ll mod = md) { for (int i = 2; i <= lim; i++) { ifac.push_back((ifac[mod % i] * (mod - mod / i) % mod) % mod); } for (ll i = 2; i <= lim; i++) { fac.push_back((fac[i - 1] * i) % mod); ifac[i] = (ifac[i - 1] * ifac[i]) % mod; } } ll C(ll n, ll r, ll mod = md) { if (n < r) return 0; return mul(fac[n], mul(ifac[r], ifac[n - r], mod), mod); } void sieve(ll n) { vector<bool> prime(n + 1, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) { prime[j] = false; } } } p.push_back(2); for (int i = 3; i <= n; i += 2) { if (prime[i]) { p.push_back(i); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, x; cin >> n; vector<ll> f(1e5 + 1, 0), cnt(1e5 + 1, 0); for (int i = 0; i < n; i++) { cin >> x; f[x]++; } for (int i = 1; i <= 1e5; i++) { for (int j = i; j <= 1e5; j += i) { cnt[i] += f[j]; } } for (int i = 1e5; i >= 1; i--) { cnt[i] = (power(2, cnt[i]) - 1 + md) % md; for (int j = 2 * i; j <= 1e5; j += i) { cnt[i] = (cnt[i] - cnt[j] + md) % md; } } cout << cnt[1] << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int n; cin >> n; int l[n], r[n], nl, nr; for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; multiset<int> sl, sr; int ans = 0; for (int i = 0; i < n; i++) sl.insert(l[i]), sr.insert(r[i]); for (int i = 0; i < n; i++) { int nl, nr; int ta = l[i], tb = r[i]; sl.erase(sl.find(ta)), sr.erase(sr.find(tb)); ans = max(ans, (*(sr.begin())) - (*(--sl.end()))); sl.insert(ta), sr.insert(tb); } cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<pair<int, pair<int, int> > > data(n + 1); for (int i = 0; i < n; ++i) cin >> data[i].first >> data[i].second.first, data[i].second.second = i + 1; data[n] = pair<int, pair<int, int> >(0, pair<int, int>(0, -1)); sort(data.begin(), data.end()); vector<int> ans; for (int i = 1; i < n + 1; ++i) { bool flag = true; int prev = 0; for (int j = 1; j < n + 1; ++j) { if (i == j) continue; flag &= !(data[j].first < data[prev].second.first); prev = j; } if (flag) ans.push_back(data[i].second.second); } sort(ans.begin(), ans.end()); cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); ++i) cout << ans[i] << ' '; cout << '\n'; return 0; }
4