solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; using namespace std; int n, ans = 99999999; int z(int x, int y) { if (y == 1) return x - 1; if (x == 0) return 100000000; if (y == 0) return 100000000; if (x == y) return 100000000; return z(y, x % y) + x / y; } int main() { cin >> n; if (n < 2) { cout << 0; return 0; } if (n == 2) { cout << 1; return 0; } for (int i = 2; i < n; i++) { ans = min(z(n, i), ans); } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; void __print(long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { long long f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const long long inf = 3e18; const long long N = 5e5 + 5; long long n, m; char boss; char mat[102][102]; long long vis[102][102]; long long rr[] = {0, 1, 0, -1}; long long cc[] = {1, 0, -1, 0}; map<pair<long long, long long>, bool> table; set<char> ans; bool inside(long long x, long long y) { return (x >= 1 && x <= n && y >= 1 && y <= m); } void dfs(long long x, long long y) { if (!inside(x, y)) return; if (mat[x][y] != boss) return; if (vis[x][y]) return; vis[x][y] = true; table[{x, y}] = true; for (long long i = 0; i < 4; i++) { long long xd = rr[i] + x; long long yd = cc[i] + y; dfs(xd, yd); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> boss; for (long long i = 1; i <= (n); i++) for (long long j = 1; j <= (m); j++) cin >> mat[i][j]; for (long long i = 1; i <= (n); i++) { for (long long j = 1; j <= (m); j++) { if (mat[i][j] == boss) { if (!vis[i][j]) dfs(i, j); } } } for (auto it : table) { long long x = it.first.first, y = it.first.second; for (long long i = 0; i < 4; i++) { long long xd = x + rr[i], yd = y + cc[i]; if (inside(xd, yd) and (mat[xd][yd] != boss) and (mat[xd][yd] != '.')) { ans.insert(mat[xd][yd]); } } } if (ans.count(boss)) ans.erase(ans.find(boss)); cout << ((long long)(ans.size())) << '\n'; }
2
#include <bits/stdc++.h> using namespace std; typedef struct node { double x; double y; double r; } NODE; NODE sta[3], ans[3]; double center_x, center_y; bool comp(const NODE &x, const NODE &y) { return (x.r < y.r); } bool cmp(const NODE &x, const NODE &y) { return (x.y < y.y); } void output() { int alpha, beta; alpha = sqrt((sta[0].x - ans[0].x) * (sta[0].x - ans[0].x) + (sta[0].y - ans[0].y) * (sta[0].y - ans[0].y)); beta = sqrt((sta[0].x - ans[1].x) * (sta[0].x - ans[1].x) + (sta[0].y - ans[1].y) * (sta[0].y - ans[1].y)); if (alpha < beta) printf("%.5lf %.5lf\n", ans[0].x, ans[0].y); else printf("%.5lf %.5lf\n", ans[1].x, ans[1].y); } void deal_all_same() { double point[3][2]; double temp[3][2]; double center_x, center_y; for (int i = 0; i < 3; i++) { point[i][0] = sta[i].x; point[i][1] = sta[i].y; } temp[0][0] = (point[1][0] * point[1][0] - point[2][0] * point[2][0] + point[1][1] * point[1][1] - point[2][1] * point[2][1]) / 2; temp[0][1] = point[1][1] - point[2][1]; temp[1][0] = (point[2][0] * point[2][0] - point[0][0] * point[0][0] + point[2][1] * point[2][1] - point[0][1] * point[0][1]) / 2; temp[1][1] = point[2][1] - point[0][1]; temp[2][0] = point[1][0] - point[2][0]; temp[2][1] = point[2][0] - point[0][0]; center_x = temp[1][1] * temp[0][0] - temp[0][1] * temp[1][0]; center_y = temp[2][0] * temp[1][0] - temp[0][0] * temp[2][1]; center_x /= temp[2][0] * temp[1][1] - temp[0][1] * temp[2][1]; center_y /= temp[2][0] * temp[1][1] - temp[0][1] * temp[2][1]; printf("%.5lf %.5lf\n", center_x, center_y); } void deal_same(int i, int j, int k) { double x0, y0, k0, x1, y1; k0 = sta[i].r / sta[k].r; x0 = (sta[i].x + sta[k].x * k0) / (1 + k0); y0 = (sta[i].y + sta[k].y * k0) / (1 + k0); x1 = (sta[i].x - sta[k].x * k0) / (1 - k0); y1 = (sta[i].y - sta[k].y * k0) / (1 - k0); if (sta[i].y != sta[j].y) k0 = -(sta[i].x - sta[j].x) / (sta[i].y - sta[j].y); NODE round; round.x = (x0 + x1) / 2; round.y = (y0 + y1) / 2; round.r = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) / 2; x0 = (sta[i].x + sta[j].x) / 2; y0 = (sta[i].y + sta[j].y) / 2; double d; if (sta[i].y != sta[j].y) d = abs((k0 * (round.x - x0) + y0 - round.y) / sqrt(1 + k0 * k0)); else d = abs(round.x - x0); double temp, lenth, alpha; if (d < round.r) { lenth = sqrt(round.r * round.r - d * d); temp = k0 * k0 + 1; if (sta[i].y != sta[j].y) { x1 = (1 * round.x + k0 * round.y - k0 * (y0 - k0 * x0)) / temp; y1 = (k0 * k0 * round.y + k0 * round.x + 1 * (y0 - k0 * x0)) / temp; if (k0 >= 0) alpha = atan(k0); else alpha = atan(k0) + 3.14159265358; } else { x1 = x0; y1 = round.y; alpha = 3.14159265358 / 2; } ans[0].x = x1 + lenth * cos(alpha); ans[0].y = y1 + lenth * sin(alpha); ans[1].x = x1 - lenth * cos(alpha); ans[1].y = y1 - lenth * sin(alpha); output(); } } void deal_different(int i, int j, int k) { NODE round1, round2; double x0, x1, y0, y1, k0, d; double alpha, beta, gama; k0 = sta[i].r / sta[k].r; x0 = (sta[i].x + sta[k].x * k0) / (1 + k0); y0 = (sta[i].y + sta[k].y * k0) / (1 + k0); x1 = (sta[i].x - sta[k].x * k0) / (1 - k0); y1 = (sta[i].y - sta[k].y * k0) / (1 - k0); round1.x = (x0 + x1) / 2; round1.y = (y0 + y1) / 2; round1.r = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) / 2; k0 = sta[j].r / sta[k].r; x0 = (sta[j].x + sta[k].x * k0) / (1 + k0); y0 = (sta[j].y + sta[k].y * k0) / (1 + k0); x1 = (sta[j].x - sta[k].x * k0) / (1 - k0); y1 = (sta[j].y - sta[k].y * k0) / (1 - k0); round2.x = (x0 + x1) / 2; round2.y = (y0 + y1) / 2; round2.r = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) / 2; d = sqrt((round1.x - round2.x) * (round1.x - round2.x) + (round1.y - round2.y) * (round1.y - round2.y)); if (d <= round1.r + round2.r) { if (round2.x == round1.x) alpha = 3.14159265358 / 2; else { k0 = (round2.y - round1.y) / (round2.x - round1.x); if (k0 >= 0) alpha = atan(k0); else alpha = atan(k0) + 3.14159265358; } k0 = (round1.r * round1.r + d * d - round2.r * round2.r) / (2 * round1.r * d); beta = acos(k0); if (round1.y > round2.y) { beta = 3.14159265358 - beta; } ans[0].x = round1.x + round1.r * cos(alpha + beta); ans[0].y = round1.y + round1.r * sin(alpha + beta); ans[1].x = round1.x + round1.r * cos(alpha - beta); ans[1].y = round1.y + round1.r * sin(alpha - beta); output(); } } int main(int argc, char const *argv[]) { for (int i = 0; i < 3; i++) { cin >> sta[i].x >> sta[i].y >> sta[i].r; } sort(sta, sta + 3, comp); if (sta[0].r == sta[2].r) { deal_all_same(); } else if (sta[0].r == sta[1].r) { deal_same(0, 1, 2); } else if (sta[1].r == sta[2].r) { deal_same(1, 2, 0); } else { sort(sta, sta + 3, cmp); if (sta[0].y == sta[1].y) deal_different(0, 1, 2); else deal_different(1, 2, 0); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long t, n, m; string s[2000010]; const int way[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; int edgecnt[2000010]; queue<pair<int, int>> q; void bfs() { pair<int, int> tmp = q.front(); q.pop(); int i = tmp.first, j = tmp.second; for (int k = 0; k < 4; k++) { int ii = i + way[k][0], jj = j + way[k][1]; if (ii >= 0 && ii < n && jj >= 0 && jj < m) { int tt = ii * m + jj; if (s[ii][jj] == '.' && edgecnt[tt] >= 1) { edgecnt[tt]--; if (edgecnt[tt] <= 1) { edgecnt[tt] = 0; q.push(make_pair(ii, jj)); } } } } if (s[i][j] != 'L') s[i][j] = '+'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); cin >> t; while (t--) { while (!q.empty()) q.pop(); cin >> n >> m; for (int i = 0; i < n * m; i++) edgecnt[i] = 0; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] == '.' || s[i][j] == 'L') for (int k = 0; k < 4; k++) { int ii = i + way[k][0], jj = j + way[k][1]; if (ii >= 0 && ii < n && jj >= 0 && jj < m && (s[ii][jj] == '.' || s[ii][jj] == 'L')) edgecnt[i * m + j]++; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (s[i][j] == 'L') { q.push(make_pair(i, j)); edgecnt[i * m + j] = 0; } while (!q.empty()) bfs(); for (int i = 0; i < n; i++) cout << s[i] << '\n'; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, b = 0, j; cin >> n; vector<int> v1; for (int i = 1; i <= n; i++) { if (n % i == 0) { int j = n / i; v1.push_back(i); v1.push_back(j); b = b + 2; if (i >= j) { break; } } } cout << v1.at(b - 1) << " " << v1.at(b - 2); return 0; }
1
#include <bits/stdc++.h> using namespace std; //typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; //container util //------------------------------------------ #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) //repetition //------------------------------------------ #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second //constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); const int NN = 1 << 11; void ins(VI& xs, int x){ xs.insert(upper_bound(ALL(xs), x), x); } bool del(VI& xs, int x){ auto it = lower_bound(ALL(xs), x); if(it == end(xs) || *it != x) return false; xs.erase(it); return true; } int num(VI& xs, int l, int u){ return lower_bound(ALL(xs), u) - lower_bound(ALL(xs), l); } class SegT{ private: VI dat[2 * NN - 1]; public: SegT(){ } // dat[idx] = a, 0-indexed void update(int x, int y){ int idx = x + NN - 1; ins(dat[idx], y); while(idx > 0){ idx = (idx - 1) / 2; ins(dat[idx], y); } } void erase(int x, int y){ int idx = x + NN - 1; if(!del(dat[idx], y)) return; while(idx > 0){ idx = (idx - 1) / 2; if(!del(dat[idx], y)) return; } } int query(int lx, int ux, int ly, int uy, int k, int l, int r){ if(r <= lx || ux <= l) return 0; if(lx <= l && r <= ux) return num(dat[k], ly, uy); else{ int vl = query(lx, ux, ly, uy, k*2+1, l, (l+r)/2); int vr = query(lx, ux, ly, uy, k*2+2, (l+r)/2, r); return vl + vr; } } int query(int lx, int ly, int ux, int uy){ return query(lx, ux, ly, uy, 0, 0, NN); } }; int main(){ int H, W, T, Q; scanf("%d%d%d%d", &H, &W, &T, &Q); using TP = tuple<double,int,PII,PII>; vector<TP> qs; REP(q,Q){ double t; int c, h, w, h1 = -1, w1 = -1; scanf("%lf%d%d%d", &t, &c, &h, &w); --h; --w; if(c == 0){ qs.push_back(make_tuple(t, 0, MP(h,w), MP(h1, w1))); t += T - 0.1; c = 3; } else if(c == 2){ scanf("%d%d", &h1, &w1); } qs.push_back(make_tuple(t, c, MP(h,w), MP(h1, w1))); } SORT(qs); SegT s1, s2; for(auto& tp: qs){ double t; int c; PII p1, p2; tie(t, c, p1, p2) = tp; if(c == 0){ s1.update(p1.SS, p1.FF); } else if(c == 1){ s2.erase(p1.SS, p1.FF); } else if(c == 2){ printf("%d %d\n", s2.query(p1.SS, p1.FF, p2.SS, p2.FF), s1.query(p1.SS, p1.FF, p2.SS, p2.FF)); } else{ s1.erase(p1.SS, p1.FF); s2.update(p1.SS, p1.FF); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; pair<pair<int, int>, int> data[100005]; int n, N, id[100005], vot[100005], f[100005], m; vector<pair<int, int> > A[100005]; vector<int> B[100005]; int calc(int x) { if (vot[0] + x == 0) return 2100000000; int sum = 0, cost = 0; for (int i = (int)1; i <= (int)m; i++) f[i] = 0; for (int i = (int)1; i <= (int)n; i++) { int k = vot[0] + x; if (k > vot[i]) continue; sum += vot[i] - k + 1; if (sum > x) return 2100000000; cost += B[i][vot[i] - k]; for (int j = (int)0; j <= (int)vot[i] - k; j++) f[A[i][j].second] = 1; } int u = x - sum, i = 0; while (u) { i++; if (i > m) return 2100000000; if (data[i].first.second == 0) continue; if (f[data[i].second]) continue; cost += data[i].first.first; u--; } return cost; } int main() { scanf("%d", &n); m = n; for (int i = (int)1; i <= (int)n; i++) { int x, y; scanf("%d%d", &x, &y); A[x].push_back(make_pair(y, i)); N = max(N, x); data[i] = make_pair(make_pair(y, x), i); } n = N; sort(data + 1, data + m + 1); for (int i = (int)0; i <= (int)n; i++) sort(A[i].begin(), A[i].end()); for (int i = (int)0; i <= (int)n; i++) { if (A[i].size() == 0) continue; B[i].resize(A[i].size()); B[i][0] = A[i][0].first; for (int j = (int)1; j <= (int)A[i].size() - 1; j++) { B[i][j] = B[i][j - 1] + A[i][j].first; } } for (int i = (int)0; i <= (int)n; i++) vot[i] = A[i].size(); int l = 0, r = m; int ans = 2100000000; while (r - l > 5) { int md1 = (2 * l + r) / 3, md2 = (l + 2 * r) / 3; int co1 = calc(md1), co2 = calc(md2); ans = min(ans, min(co1, co2)); if (co1 <= co2) r = md2; else l = md1; } for (int i = (int)l; i <= (int)r; i++) { ans = min(ans, calc(i)); } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t, n, temp1, temp2, temp3; unsigned long long int cons = 998244353; cin >> t; for (int j = 0; j < t; j++) { cin >> n; if (n <= 998244353 * 2) { if (n % 2) { cout << 7; n -= 3; } for (int i = 0; i < n / 2; i++) { cout << 1; } cout << endl; } else { temp3 = n - cons * 2; if (temp3 <= 3) { for (int k = 0; k < temp3; k++) { cout << 7; n -= 3; } temp2 = (cons - temp3) * 3 - (n / 2); temp1 = (n - (temp2 * 2)) / 6; for (int i = 0; i < temp1 / 3; i++) { cout << 9; } for (int i = 0; i < temp2 / 2; i++) { cout << 1; } cout << endl; } else { temp2 = (3 * cons - (n / 2)) / 2; temp1 = (n - (temp2 * 2)) / 6; for (int i = 0; i < temp1; i++) { cout << 9; } temp3 = n - temp2 * 2 - temp1 * 6; for (int i = 0; i < temp3; i++) { cout << 7; temp2 -= 1; } for (int i = 0; i < temp2; i++) { cout << 1; } cout << endl; } } } }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n == 0) { cout << "1"; return 0; } if (n == 1) { cout << "4"; return 0; } long long t = 4; long long i = 1, j = n - 1, r = 0; while (i < n && j > 0) { if ((i + 1) * (i + 1) + (j * j) <= n * n) i++; else if ((i + 1) * (i + 1) + (j - 1) * (j - 1) <= n * n) i++, j--; else j--; r++; } cout << t + r * 4; return 0; }
3
#include <bits/stdc++.h> using namespace std; void gn(int &x) { x = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); } int T; long long n, m, s, ans; int p[1000010], tot, a[1000010], top, lim[1000010]; int id[1000010], cnt[1000010]; bool np[1000010]; void sieve(int n) { for (int i = 2; i <= n; ++i) { if (!np[i]) { p[++tot] = i; id[i] = tot; } for (int j = 1; j <= tot && 1ll * i * p[j] <= n; ++j) { np[i * p[j]] = 1; if (i % p[j] == 0) break; } } } void calc(int x) { for (int j = 1; j <= tot && 1ll * p[j] * p[j] <= x; ++j) { while (x % p[j] == 0) x /= p[j], ++cnt[j]; } if (x != 1) ++cnt[id[x]]; } void dfs(int k, long long x) { if (x > n) return; if (k > top) { ++ans; return; } for (int j = 0; j <= cnt[a[k]]; ++j) { dfs(k + 1, x); if (x > n / p[a[k]]) break; x *= p[a[k]]; } } int main() { gn(T); sieve(1000000); while (T--) { for (int i = 1; i <= tot; ++i) cnt[i] = 0; int tmp, n1, n2, n3; n = m = s = 1; gn(n1); gn(n2); gn(n3); n = 1ll * n1 * n2 * n3; for (int i = 1; i <= 3; ++i) { gn(tmp); m *= tmp; } for (int i = 1; i <= 3; ++i) { gn(tmp); calc(tmp); s *= tmp; } calc(2ll); top = 0; for (int i = 1; i <= tot; ++i) if (cnt[i]) a[++top] = i; ans = 0; dfs(1, 1); for (int i = 1; i <= tot; ++i) cnt[i] = 0; calc(n1); calc(n2); calc(n3); top = 0; for (int i = 1; i <= tot; ++i) if (cnt[i]) a[++top] = i; for (int i = 1; i <= top; ++i) { int tmp = 0; unsigned long long now = 2ull * s; while (now % (unsigned long long)p[a[i]] == 0) { now /= (unsigned long long)p[a[i]]; ++tmp; } lim[i] = (tmp >= cnt[a[i]]) ? -1 : tmp; } for (int i = 0; i < (1 << top); ++i) { long long tmp = 1, v = 1; bool flag = 1; for (int j = 1; j <= top; ++j) if ((i & (1 << (j - 1)))) { v = -v; if (lim[j] == -1) flag = 0; else { for (int k = 1; k <= lim[j] + 1; ++k) { if (tmp > m / p[a[j]]) flag = 0; else tmp *= p[a[j]]; } } } if (flag) ans += v * (m / tmp); } printf("%I64d\n", ans); } return 0; }
3
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<iomanip> #include<set> using namespace std; int main() { vector<char> ls; int n; while(true) { cin >> n; if(n==0)break; vector<int> key(n); for(int i=0;i<n;i++) cin >> key[i]; string s; cin >> s; string ans; for(int i=0;i<s.size();i++) { int tmp; if('a'<=s[i] and s[i]<='z') tmp=s[i]-'a'; else tmp=s[i]-'A'+26; int aft=((tmp-key[i%n]+52)%52); if(aft<26) ans+=('a'+aft); else ans+=('A'+(aft-26)); } cout << ans << endl; } }
0
#include <iostream> int main() { int m, d; std::cin >> m >> d; int ans = 0; for (int i = 1; i <= m; ++i) for (int j = 22; j <= d; ++j) if ((j % 10) >= 2 && (j / 10) * (j % 10) == i) ++ans; std::cout << ans << std::endl; }
0
#include <bits/stdc++.h> using namespace std; vector<int> edges[100005]; bool visited[100005]; bool arr[100005]; int dp[100005]; int parent[26]; int find(int x) { if (parent[x] == -1) { return x; } return find(parent[x]); } int main() { int n; cin >> n; int indegree[n], xo[n]; queue<int> q; for (int i = 0; i < n; i++) { cin >> indegree[i] >> xo[i]; if (indegree[i] == 1) { q.push(i); } } vector<pair<int, int> > ans; while (!q.empty()) { int x = q.front(); q.pop(); if (indegree[x] == 0) { continue; } if (indegree[x] == 1) { int toconnect = xo[x]; ans.push_back({x, toconnect}); indegree[x] == 0; indegree[toconnect]--; xo[toconnect] ^= x; if (indegree[toconnect] == 1) { q.push(toconnect); } } } cout << ans.size() << endl; for (auto it : ans) { cout << it.first << " " << it.second << endl; } }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int sum = 0; for (int i = 0; i < n; i++) { sum += (s[i] == 'B'); } vector<int> A; if (sum % 2 == 0) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'B') { A.push_back(i + 1); s[i] = 'W'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else { s[i + 1] = 'B'; } } } cout << A.size() << "\n"; for (auto u : A) { cout << u << " "; } cout << "\n"; } else { if (n % 2 == 1) { for (int i = 0; i < n - 1; i++) { if (s[i] == 'W') { A.push_back(i + 1); s[i] = 'B'; if (s[i + 1] == 'B') s[i + 1] = 'W'; else { s[i + 1] = 'B'; } } } cout << A.size() << "\n"; for (auto u : A) { cout << u << " "; } cout << "\n"; } else { cout << "-1\n"; } } return 0; }
2
#include <bits/stdc++.h> template <class _Tp = int> _Tp read() { _Tp w = 0; bool f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) w = (w << 3) + (w << 1) + (ch ^ 48), ch = getchar(); return f ? -w : w; } const int kMaxN = 5e2 + 5; int n, m; std::bitset<kMaxN> dp[65][2][kMaxN], can_reach, hold; int main() { n = read(), m = read(); for (int i = 1, u, v, w; i <= m; i++) { u = read(), v = read(), w = read(); dp[0][w][u][v] = true; } for (int i = 1; i <= 63; i++) { for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) { if (dp[i - 1][0][j][k]) dp[i][0][j] |= dp[i - 1][1][k]; if (dp[i - 1][1][j][k]) dp[i][1][j] |= dp[i - 1][0][k]; } } long long ans = 0, stats = 0; can_reach[1] = true; for (int i = 63; i >= 0; i--) { hold.reset(); for (int j = 1; j <= n; j++) if (can_reach[j]) hold |= dp[i][stats][j]; if (hold.count()) { stats ^= 1; can_reach = hold; ans += (1ll << i); } } if (ans > 1e18) ans = -1; printf("%lld\n", ans); }
6
#include <bits/stdc++.h> using namespace std; int n, k; pair<int, int> p[100005]; long long cnt[100005]; int pre[30000005]; int size[30000005]; int lst[100005]; int y[100005]; int main() { scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d %d", &p[i].first, &p[i].second); y[i] = p[i].second; } sort(p, p + n); sort(y, y + n); int fin = -1000000600; int sz = 0; for (int i = 0; i < n; i++) { if (y[i] - k > fin) sz += k; else sz += y[i] - fin; lst[i] = sz; fin = y[i]; } for (int i = 0; i < n; i++) { int yi = lower_bound(y, y + n, p[i].second) - y; for (int j = 1; j <= k; j++) { size[lst[yi] - j + 1]++; } } for (int i = 1; i < sz; i++) { size[i] += size[i - 1]; } size[sz] = size[sz - 1]; for (int i = 0; i < n; i++) { int yi = lower_bound(y, y + n, p[i].second) - y; for (int j = 1; j <= k; j++) { pre[size[lst[yi] - j]++] = (p[i].first); } } memset(cnt, 0, sizeof(cnt)); int state = 0; int li; for (int x = 0; x < sz; x++) { state = 0; li = -1000000600; int z = 0; if (x) z = size[x - 1]; int j = z; while (z < size[x]) { if (j == size[x] || pre[z] + k < pre[j]) { cnt[state] += pre[z] + k - li; state--; li = pre[z] + k; z++; } else { cnt[state] += pre[j] - li; state++; li = pre[j]; j++; } } } for (int i = 1; i <= n; i++) printf("%lld\n", cnt[i]); }
4
#include <bits/stdc++.h> const int inf = (1ll << 30) - 1; const int maxn = (int)1e5 + 10; using namespace std; int n; int k; int m; map<long long, vector<int> > M; long long a[500500], b[500500], c[500500], P[500500]; long long mod = 1000 * 1000 * 1000 + 7; int p[500500]; long long ans = 0; int SZ; void clr(int x) { p[x] = x; } int get(int x) { if (p[x] == x) return x; return p[x] = get(p[x]); } void merge(int x, int y) { x = get(x); y = get(y); if (x == y) return; SZ--; p[x] = y; } void solve() { cin >> n >> m >> k; for (int i = 0; i < n; i++) { scanf("%lld", &c[i]); p[i] = i; } for (int i = 0; i < m; i++) { scanf("%lld%lld", &a[i], &b[i]); --a[i]; --b[i]; long long d = c[a[i]] ^ c[b[i]]; M[d].push_back(i); } long long g = (1ll << k) - M.size(); P[0] = 1; for (int i = 1; i <= n; i++) { P[i] = P[i - 1] * 2 % mod; } ans = (g % mod * P[n]) % mod; for (__typeof(M.begin()) it = M.begin(); it != M.end(); it++) { SZ = n; for (__typeof(it->second.begin()) it2 = it->second.begin(); it2 != it->second.end(); it2++) { merge(a[*it2], b[*it2]); } ans = (ans + P[SZ]) % mod; for (__typeof(it->second.begin()) it2 = it->second.begin(); it2 != it->second.end(); it2++) { clr(a[*it2]); clr(b[*it2]); } } cout << ans << "\n"; } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = int(1e6) + 100; struct LINK { int id, next; }; int n, now; int h[maxn]; int f[maxn], ans[maxn]; bool vis[maxn]; LINK t[maxn * 2]; vector<int> deep[maxn]; void join(int u, int v) { t[now].id = v; t[now].next = h[u]; h[u] = now++; t[now].id = u; t[now].next = h[v]; h[v] = now++; } void read() { scanf("%d", &n); for (int i = 1; i <= n; ++i) h[i] = -1; for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); join(x, y); } } void merge(vector<int> &x, vector<int> &y, int &wx, int &wy) { if (x.size() < y.size()) { swap(x, y); swap(wx, wy); } int ys = y.size(); int xs = x.size(); for (int i = ys - 1; i >= 0; --i) { x[xs - (ys - i)] += y[i]; if (x[xs - (ys - i)] > x[wx] || (x[xs - (ys - i)] == x[wx] && (xs - (ys - i)) > wx)) wx = xs - (ys - i); } y.clear(); } void dfs(int cur) { vis[cur] = true; deep[cur].clear(); deep[cur].push_back(1); f[cur] = 0; for (int i = h[cur]; i > -1; i = t[i].next) if (!vis[t[i].id]) { dfs(t[i].id); deep[t[i].id].push_back(0); merge(deep[cur], deep[t[i].id], f[cur], f[t[i].id]); } ans[cur] = deep[cur].size() - f[cur] - 1; } void solve() { dfs(1); for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); } int main() { read(); solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; int p[10000001]; int fa[10000001], fb[10000001]; int a[100005], b[100005]; void gao(int n, int x[], int g[]) { for (int i = 0; i < n; ++i) { scanf("%d", &x[i]); for (int y = x[i]; y > 1; y /= p[y]) ++g[p[y]]; } } void output(int x[], int n, int f[]) { for (int i = 0; i < n; ++i) { for (int y = x[i]; y > 1; y /= p[y]) while (f[p[y]] > 0 && x[i] % p[y] == 0) { --f[p[y]]; x[i] /= p[y]; } printf("%d", x[i]); putchar(i == n - 1 ? '\n' : ' '); } } int main() { for (int i = 2; i < 10000001; ++i) if (!p[i]) { for (int j = i; j < 10000001; j += i) p[j] = i; } int an, bn; scanf("%d %d", &an, &bn); gao(an, a, fa); gao(bn, b, fb); for (int i = 0; i < 10000001; ++i) fa[i] = fb[i] = min(fa[i], fb[i]); printf("%d %d\n", an, bn); output(a, an, fa); output(b, bn, fb); return 0; }
3
#include <bits/stdc++.h> using namespace std; namespace minmax { template <class T> T max(T&& A) { return A; } template <class T> T min(T&& A) { return A; } template <class T> T max(T&& A, T&& B) { return A > B ? A : B; } template <class T> T min(T&& A, T&& B) { return A < B ? A : B; } template <class T, class... Ts> T max(T&& A, Ts&&... ts) { T B = max(ts...); return A > B ? A : B; } template <class T, class... Ts> T min(T&& A, Ts&&... ts) { T B = min(ts...); return A < B ? A : B; } template <class T, class... Ts> T chmax(T&& A, Ts&&... ts) { A = max(A, ts...); return A; } template <class T, class... Ts> T chmin(T&& A, Ts&&... ts) { A = min(A, ts...); return A; } } // namespace minmax namespace input { template <class T> void re(T&& Xx) { cin >> Xx; } template <class T1, class T2> void re(pair<T1, T2>& Pp) { re(Pp.first); re(Pp.second); } template <class T> void re(vector<T>& Aa) { for (int i = 0; i < (int)(Aa).size(); i++) re(Aa[i]); } template <class T1, class T2> void rea(T1&& Aa, T2 t) { for (int i = 0; i < t; i++) re(Aa[i]); } template <class T1, class T2, class... Ts> void rea(T1&& Aa, T2 t, Ts&&... ts) { rea(Aa, t); rea(ts...); } template <class T1, class T2> void rea1(T1&& Aa, T2 t) { for (int i = 1; i <= t; i++) re(Aa[i]); } template <class T1, class T2, class... Ts> void rea1(T1&& Aa, T2 t, Ts&... ts) { rea1(Aa, t); rea1(ts...); } template <class T, class... Ts> void re(T&& t, Ts&... ts) { re(t); re(ts...); } } // namespace input namespace output { void pr(int Xx) { cout << Xx; } void pr(bool Xx) { cout << Xx; } void pr(long long Xx) { cout << Xx; } void pr(long long unsigned Xx) { cout << Xx; } void pr(double Xx) { cout << Xx; } void pr(char Xx) { cout << Xx; } void pr(const string& Xx) { cout << Xx; } void pr(const char* Xx) { cout << Xx; } void pr(const char* Xx, size_t len) { cout << string(Xx, len); } void ps() { cout << "\n"; } void pn() {} void pw() { pr(" "); } void pc() { pr("]"); ps(); } template <class T1, class T2> void pr(const pair<T1, T2>& Xx) { pr(Xx.first); pw(); pr(Xx.second); } template <class T> void pr(const T&); bool parse(const char* t) { if (t == "\n") return true; return false; } template <class T> bool parse(T&& t) { return false; } template <class T1, class T2> bool parsepair(const pair<T1, T2>& Xx) { return true; } template <class T> bool parsepair(T&& t) { return false; } template <class T1, class T2> void psa(T1&& Aa, T2 t) { for (int i = 0; i < t; i++) pr(Aa[i]), pw(); ps(); } template <class T1, class T2> void pna(T1&& Aa, T2 t) { for (int i = 0; i < t; i++) pr(Aa[i]), ps(); } template <class T1, class T2> void psa2(T1&& Aa, T2 t1, T2 t2) { for (int i = 0; i < t1; i++) { for (int j = 0; j < t2; j++) pr(Aa[i][j]), pw(); ps(); } } template <class T> void pr(const T& Xx) { bool fst = 1; bool op = 0; if (parsepair(*Xx.begin())) op = 1; for (const auto& Aa : Xx) { if (!fst) pw(); if (op) pr("{"); pr(Aa), fst = 0; if (op) pr("}"); } } template <class T, class... Ts> void pr(const T& t, const Ts&... ts) { pr(t); pr(ts...); } template <class T, class... Ts> void ps(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) { if (!parse(t)) pw(); } ps(ts...); } template <class T> void pn(const T& t) { for (const auto& Aa : t) ps(Aa); } template <class T, class... Ts> void pw(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pw(); pw(ts...); } template <class T, class... Ts> void pc(const T& t, const Ts&... ts) { bool op = 0; if (parsepair(t)) op = 1; if (op) pr("{"); pr(t); if (op) pr("}"); if (sizeof...(ts)) pr(", "); pc(ts...); } namespace trace { template <class T1, class T2> void __f(const char* name, const T1& Xx, const T2& y) { pr("[", y, "] = "); ps(Xx); } template <class T1, class T2, class... Ts> void __f(const char* name, const T1& Xx, const T2& y, const Ts&... rest) { const char* open = strchr(name, '['); pr("[", y, "]"); __f(open + 1, Xx, rest...); } template <class T1, class T2, class... Ts> void __f0(const char* name, const T1& Xx, const T2& y, const Ts&... rest) { const char* open = strchr(name, '['); pr(name, size_t(open - name)); __f(name, Xx, y, rest...); } template <class T> void __fn(int n, const char* name, const T& Xx) { for (int i = 0; i < n; i++) pr(name), __f(name, Xx[i], i); } } // namespace trace } // namespace output using namespace minmax; using namespace input; using namespace output; using namespace output::trace; using ll = long long; using pii = pair<int, int>; const int N = 2e5 + 5; vector<pair<char, int>> cc(string y) { vector<pair<char, int>> v; for (auto& x : y) { if ((int)(v).size() and v.back().first == x) v.back().second++; else v.emplace_back(x, 1); } return v; } int solve() { int i = 0, j = -1, n; string s, t; re(s, t); vector<pair<char, int>> vs = cc(s), vt = cc(t); if ((int)(vs).size() != (int)(vt).size()) return ps("NO"), 0; for (i = 0; i < (int)(vs).size(); i++) if (vs[i].first != vt[i].first or vt[i].second < vs[i].second) return ps("NO"), 0; ps("YES"); return 0; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int Q; for (cin >> Q; Q; Q--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long *fac, *ifac; long long powmod(long long b, long long e, long long mod) { long long res = 1; while (e > 0) { if (e % 2 == 1) res = (res * b) % mod; e = e / 2; b = (b * b) % mod; } return (res % mod); } long long inv(long long n, long long mod) { return powmod(n, mod - 2, mod); } long long C(long long n, long long r, long long mod) { if (n < r) return 0; long long ans = fac[n]; ans = (ans * ifac[r]) % mod; ans = (ans * ifac[n - r]) % mod; return ans % mod; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, i, j, k, x, y, z; string s; cin >> s; n = s.length(); fac = (long long *)malloc((n + 1) * sizeof(long long)); ifac = (long long *)malloc((n + 1) * sizeof(long long)); fac[0] = 1; fac[1] = 1; for (i = (2); i < (n + 1); ++i) fac[i] = (i * fac[i - 1]) % mod; ifac[0] = ifac[1] = 1; for (i = (2); i < (n + 1); ++i) ifac[i] = (inv(i, mod) * ifac[i - 1]) % mod; long long left[n], right[n + 1]; if (s[0] == '(') left[0] = 1; else left[0] = 0; for (i = (1); i < (n); ++i) { left[i] = left[i - 1]; if (s[i] == '(') left[i]++; } right[n] = 0; if (s[n - 1] == ')') right[n - 1] = 1; else right[n - 1] = 0; for (i = n - 2; i >= 0; i--) { right[i] = right[i + 1]; if (s[i] == ')') right[i]++; } long long ans = 0; for (i = (0); i < (n); ++i) { if (s[i] == '(') { x = left[i]; y = right[i + 1]; z = C(x + y - 1, x, mod); ans = (ans + z) % mod; } } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long a, b, ans; int main() { cin >> a >> b; if (a == b) { cout << "infinity"; return 0; } if (a < b) { cout << "0"; return 0; } for (int i = 1; i * i <= a - b; i++) if ((a - b) % i == 0) { if (i > b) ans++; if ((a - b) / i > b) ans++; } if (ceil(sqrt(a - b)) == int(sqrt(a - b)) && int(sqrt(a - b)) > b) ans--; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int N; struct SegmentTree1 { int siz; vector<long long> tree; long long def = -2001001001; SegmentTree1(int temp) { siz = temp; for (int i = 0; i < siz * 2; i++) tree.push_back(def); } SegmentTree1() {} void update(int pos, long long val) { pos += siz; tree[pos] = oper(tree[pos], val); for (pos /= 2; pos >= 1; pos /= 2) tree[pos] = oper(tree[pos * 2], tree[pos * 2 + 1]); } long long query(int L, int R) { L += siz; R += siz; long long x = def; while (L <= R) { if (L % 2 == 1) x = oper(x, tree[L++]); if (R % 2 == 0) x = oper(x, tree[R--]); L /= 2; R /= 2; } return x; } long long oper(long long a, long long b) { return max(a, b); } }; struct SegmentTree2 { int siz; vector<long long> tree; long long def = 2001001001; SegmentTree2(int temp) { siz = temp; for (int i = 0; i < siz * 2; i++) tree.push_back(def); } SegmentTree2() {} void update(int pos, long long val) { pos += siz; tree[pos] = oper(tree[pos], val); for (pos /= 2; pos >= 1; pos /= 2) tree[pos] = oper(tree[pos * 2], tree[pos * 2 + 1]); } long long query(int L, int R) { L += siz; R += siz; long long x = def; while (L <= R) { if (L % 2 == 1) x = oper(x, tree[L++]); if (R % 2 == 0) x = oper(x, tree[R--]); L /= 2; R /= 2; } return x; } long long oper(long long a, long long b) { return min(a, b); } }; int const X = 1000005; int a[X], b[X]; long long lmax[X], rmax[X], lmin[X], rmin[X]; void reorder() { vector<pair<int, int>> v; for (int i = 0; i < N; i++) v.push_back({a[i], i}); sort(v.begin(), v.end()); for (int i = 0; i < N; i++) a[v[i].second] = i + 1; } int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &b[i]); a[i] = b[i]; } reorder(); SegmentTree1 s = SegmentTree1(X); for (int i = 0; i < N; i++) { lmax[i] = s.query(a[i], 1000000); if (lmax[i] == -2001001001) lmax[i] = -1; lmax[i] = i - lmax[i]; lmin[i] = s.query(1, a[i]); if (lmin[i] == -2001001001) lmin[i] = -1; lmin[i] = i - lmin[i]; s.update(a[i], i); } SegmentTree2 t = SegmentTree2(X); for (int i = N - 1; i >= 0; i--) { rmax[i] = t.query(a[i], 1000000); if (rmax[i] == 2001001001) rmax[i] = N; rmax[i] = rmax[i] - i; rmin[i] = t.query(1, a[i]); if (rmin[i] == 2001001001) rmin[i] = N; rmin[i] = rmin[i] - i; t.update(a[i], i); } long long ans = 0; for (int i = 0; i < N; i++) { ans += b[i] * lmax[i] * rmax[i]; ans -= b[i] * lmin[i] * rmin[i]; } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long n, m; cin >> n >> m; unsigned long long a[101] = {0}; for (unsigned long long i = 0; i < m; i++) { unsigned long long k, f; cin >> k >> f; a[k] = f; } unsigned long long ans = 0; for (unsigned long long i = 1; i <= 100; i++) { bool ok = true; unsigned long long c = 0, f = 1, ans_f = 1; for (unsigned long long j = 1; j <= 100; j++) { if (j == n) ans_f = f; if (a[j] == 0) { c += 1; } else { if (a[j] == f) { c += 1; } else { ok = false; break; } } if (c >= i) { f += 1; c = 0; } } if (ok) { if (ans != 0 && ans != ans_f) { cout << -1; return 0; } ans = ans_f; } } if (ans == 0) { cout << -1; return 0; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int al, bl, ar, br; cin >> al >> ar >> bl >> br; if (br >= (al - 1) && ((al + 1) * 2) >= br) { cout << "YES"; return 0; } if (bl >= (ar - 1) && ((ar + 1) * 2) >= bl) { cout << "YES"; return 0; } cout << "NO"; return 0; }
1
#include <bits/stdc++.h> int main() { int r; int summ = 0; int n; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> r; summ += r; } std::cout << summ / n << std::endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { char a[100000]; gets(a); int len = strlen(a); string s; for (int i = 0; i < len; i++) { if (a[i] != ' ') s += tolower(a[i]); } int si = s.size(); if (s[si - 2] == 'a' || s[si - 2] == 'e' || s[si - 2] == 'i' || s[si - 2] == 'o' || s[si - 2] == 'u' || s[si - 2] == 'y') cout << "YES"; else cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; pair<long long, long long> tree[4 * N]; long long a[N], dp[N]; void build(int node, int s, int e) { if (s == e) { tree[node] = {a[s], s}; return; } int md = (s + e) >> 1; build(node << 1, s, md); build((node << 1) + 1, md + 1, e); tree[node] = max(tree[node << 1], tree[(node << 1) + 1]); } pair<long long, long long> qry(int node, int s, int e, int l, int r) { if (r < s || e < l) return {-1, -1}; if (l <= s && e <= r) return tree[node]; int md = (s + e) >> 1; return max(qry(node << 1, s, md, l, r), qry((node << 1) + 1, md + 1, e, l, r)); } int main() { int n; scanf("%d", &n); n--; for (int i = 0; i < n; i++) { scanf("%d", a + i); a[i]--; } a[n] = n + 1; long long ret = 0; build(1, 0, n); for (int i = n - 1; i >= 0; i--) { auto to = qry(1, 0, n, i + 1, a[i]); dp[i] = dp[to.second] + to.second - a[i] + (n - i); ret += dp[i]; } cout << ret << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; if (n % 2 != 0) { cout << (n + 1) / 2 << endl; } else if (n % 2 == 0 && n != 0) { cout << n + 1 << endl; } else if (n == 0) cout << "0" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x = 0; int y = 0; int z = 0; for (int i = 0; i < n; i++) { int t_x; int t_y; int t_z; cin >> t_x; cin >> t_y; cin >> t_z; x += t_x; y += t_y; z += t_z; } if (x == 0) { if (y == 0) { if (z == 0) { cout << "YES"; } } } else { cout << "NO"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; #define rep(i, n) for(int i = 0;i < (int)(n);i++) int main() { const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}, inf = 10e9; int H, W, nh, nw, m = 0;cin >> H >> W; P p; vector<string> v(H); vector<vector<int>> d(H, vector<int>(W)); queue<P> q; rep(i, H) cin >> v[i]; rep(i, H) rep(j, W) if(v[i][j] == '.') { rep(k, H) rep(l, W) d[k][l] = inf; q.push(P(i, j)); d[i][j] = 0; while(q.size() != 0) { p = q.front(); q.pop(); rep(k, 4) { nh = p.first + dx[k], nw = p.second + dy[k]; if(0 <= nh && nh < H && 0 <= nw && nw < W && v[nh][nw] == '.' && d[nh][nw] == inf) { q.push(P(nh, nw)); d[nh][nw] = d[p.first][p.second] + 1; if(m < d[nh][nw]) m = d[nh][nw]; } } } } cout << m << endl; }
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define inf 1000000007 #define int long long signed main() { int n; cin >> n; vector<pair<int, int>> v; rep(i, n) { int a, b; cin >> a >> b; v.push_back(make_pair(a, 1)); v.push_back(make_pair(b + 1, -1)); } sort(v.begin(), v.end()); int now = 0; int ma = 0; for(auto i: v) { now += i.second; ma = max(ma, now); } cout << ma << endl; }
0
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long c, d; if (b == 1) { return a % 1000000007; } else if (b % 2 == 0) { c = power(a, b / 2); return (c * c) % 1000000007; } else { c = power(a, b / 2); d = (c * c) % 1000000007; d = (d * a) % 1000000007; return d; } } int main() { int n; scanf("%d", &n); vector<int> a(n); vector<int> finl(n); vector<int> finlidx(n); int i, j, ii, jj, tp, per, x; for (i = 0; i < n; i++) { scanf("%d", &a[i]); finl[i] = a[i]; finlidx[i] = 0; } int q; scanf("%d", &q); vector<int> prefix(q); for (i = 0; i < q; i++) { prefix[i] = 0; } for (i = 0; i < q; i++) { scanf("%d", &tp); if (tp == 2) { scanf("%d", &x); prefix[i] = x; } else { scanf("%d %d", &per, &x); finl[per - 1] = x; finlidx[per - 1] = i; } } for (i = q - 2; i >= 0; i--) { prefix[i] = max(prefix[i + 1], prefix[i]); } for (i = 0; i < n; i++) { printf("%d ", max(finl[i], prefix[finlidx[i]])); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, rep[2000009]; long long cur, ans; bool is[2000009]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); rep[x]++; is[x] = 1; } for (int i = 200000; i >= 0; i--) rep[i] += rep[i + 1]; for (int i = 1; i <= 200000; i++) { if (!is[i]) continue; cur = 0; for (int j = i; j <= 200000; j += i) { cur += (rep[j] - rep[i + j]) * j; } ans = max(ans, cur); } printf("%lld\n", ans); return 0; }
6
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; long long INF = 1e9; const int N = 1031; int n, m, k; int board[N][N]; int si, sj; int dist[N][N], used[N][N]; bool outside(int a, int b) { return (a < 1 || a > n || b < 1 || b > m); } bool valid_move(int a, int b, int D) { if (outside(a, b)) return false; if (board[a][b]) return false; return (dist[a][b] <= D); } bool is_blocked(int a, int b) { for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (abs(dx) + abs(dy) != 1) continue; if (valid_move(a + dx, b + dy, 10000000)) return false; } } return true; } queue<pair<int, int> > qu; string ans; int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for (int i = 1; i <= n; i++) { string st; cin >> st; for (int j = 1; j <= m; j++) { if (st[j - 1] == '*') { board[i][j] = 1; continue; } if (st[j - 1] == 'X') { si = i; sj = j; } } } qu.push(make_pair(si, sj)); used[si][sj] = 1; while (qu.size()) { pair<int, int> p = qu.front(); qu.pop(); int a = p.first; int b = p.second; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (abs(dx) + abs(dy) != 1) continue; if (outside(a + dx, b + dy)) continue; if (board[a + dx][b + dy]) continue; if (used[a + dx][b + dy]) continue; used[a + dx][b + dy] = 1; dist[a + dx][b + dy] = dist[a][b] + 1; qu.push(make_pair(a + dx, b + dy)); } } } if (k % 2 == 1 || is_blocked(si, sj)) { cout << "IMPOSSIBLE" << endl; return 0; } for (int i = 1; i <= k; i++) { if (valid_move(si + 1, sj, k - i)) { ans += "D"; si++; } else if (valid_move(si, sj - 1, k - i)) { ans += "L"; sj--; } else if (valid_move(si, sj + 1, k - i)) { ans += "R"; sj++; } else { ans += "U"; si--; } } cout << ans << endl; cin.get(); cin.get(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; if (b > a) cout << "-1" << endl; else if (a % b == 0 && (a / b) % 2 == 1) cout << b << endl; else { int temp = (a - b) / (b * 2) * 2 + 1; double ans = (double)(a + b) / (temp + 1); printf("%.10lf\n", ans); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int b[1000005]; int main() { ios::sync_with_stdio(false); int n, m, k; cin >> n >> m >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) b[a[i]] = 1; int sum = 0, result = 0, len = 0; for (int i = 0; i < 1000005; i++) { sum += b[i]; len++; if (len == m + 1 && i - m >= 0) { sum -= b[i - m]; len--; } if (sum >= k) { sum--; result++; b[i] = 0; } } cout << result << endl; }
4
#include <bits/stdc++.h> int main(void) { char str[30], c = ' ', field[2][13]; fgets(str, 30, stdin); int i = 1, i2, j2, l; while (str[i]) { if (c == ' ') { for (int j = 0; j < i; j++) { if (str[i] == str[j]) { c = str[j]; l = i - j - 1; i2 = j; } } } if (str[i] == str[i - 1]) { printf("Impossible"); return 0; } i++; } i = 0; for (int j = 12 - l / 2; j < 13; j++) { field[0][j] = str[i2 + i]; i++; } i2 += i; i = 0; for (int j = 12; j > 12 - ceil(l / 2.0); j--) { field[1][j] = str[i2 + i]; i++; j2 = j; } i2 += i + 1; j2--; i = 0; for (; j2 >= 0; j2--) { field[1][j2] = str[(i2 + i) % 27]; i++; } for (int j = 0; j < 12 - l / 2; j++) { field[0][j] = str[(i2 + i) % 27]; i++; } for (int j = 0; j < 26; j++) { printf("%c", field[j / 13][j % 13]); if (j == 12) { printf("\n"); } } }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 300000 + 100; const int maxm = 2000000 + 100; const int maxt = 3000000 + 100; const int maxk = 10 + 3; const long long unit = 1LL; const int INF = 0x3f3f3f3f; const long long Inf = 0x3f3f3f3f3f3f3f3fLL; const double eps = 1e-3; const double inf = 1e15; const double pi = acos(-1.0); const long long mod = 1000000007LL; inline long long read() { long long x = 0; bool t = false; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') t = true, ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); return t ? -x : x; } int a, b; char str[maxn]; int main() { int T; T = read(); while (T--) { a = read(), b = read(); scanf("%s", str + 1); int n = strlen(str + 1); bool ok = true; int cnt = 0, cnt1 = 0, x = 0; for (int i = 1; i <= n; ++i) { if (str[i] == '.') { int j = i; while (j <= n && str[j] == '.') ++j; --j; int len = j - i + 1; if (len < b) continue; if ((len < a) || (a + 4 * b - 1 <= len)) { ok = false; break; } if (a <= len && len < b + b) ++cnt1; if (len >= b + b && len >= a) ++cnt, x = len; i = j; } } if (!ok || (cnt >= 2)) { printf("NO\n"); continue; } if (cnt == 0) { if (cnt1 & 1) printf("YES\n"); else printf("NO\n"); } else { ok = false; for (int l = 0; l <= x && l < 2 * b; ++l) { if (b <= l && l < a) continue; int r = x - l - a; if (b <= r && r < a) continue; if (r < 0) break; if (r >= 2 * b) continue; int tmp = cnt1; if (a <= l) ++tmp; if (a <= r) ++tmp; if (!(tmp & 1)) { ok = true; break; } } if (ok) printf("YES\n"); else printf("NO\n"); } } return 0; }
5
#include <bits/stdc++.h> const int MOD = 998244353; int binPow(int a, int n) { int res = 1; while (n) { if (n & 1) res = (1LL * res * a) % MOD; a = (1LL * a * a) % MOD; n >>= 1; } return res; } using namespace std; long long int fact(long long num) { long long res = 1; for (int i = 1; i <= num; i++) { res = res * i; } return res; } vector<long long int> primefactors(long long int num) { vector<long long int> res; for (int i = 2; i * i <= num; i++) { if (num % i == 0) { res.push_back(i); } while (num % i == 0) { num /= i; } } if (num > 1) { res.push_back(num); } return res; } long long int factorial(long long int num) { long long int res = 1; for (int i = 1; i <= num; i++) { res = res % MOD * i % MOD; res = res % MOD; } return res % MOD; } int numberOfSetBits(int n) { int res = 0; while (n) { res += n & 1; n >>= 1; } return res; } static bool comp(pair<int, int> &p1, pair<int, int> &p2) { if (p1.first == p2.first) { return p1.second < p2.second; } return p1.first < p2.first; } long long int G(long long int x) { while (x % 10 == 0) { x = x / 10; } return x; } int highestPowerof2(int n) { int p = (int)log2(n); return (int)pow(2, p); } bool check(int arr[], int n) { for (int i = 0; i < n - 1; i++) { if (arr[i] > arr[i + 1]) { return false; } } return true; } long long int fun(long long int n) { if (n < 5) return max(1LL, n - 1); if (n % 2 == 0 && n % 4 != 0) return n - fun(n / 2); else return n - fun(n - 1); } int sum(long long int n) { int res = 0; while (n > 0) { res += n % 10; n /= 10; } return res; } bool isSame(vector<int> &arr) { for (int i = 0; i < arr.size() - 1; i++) { if (arr[i] != arr[i + 1]) return false; } return true; } long long int calc(vector<int> &nums) { sort(nums.begin(), nums.end()); int mid = nums.size() / 2; long long int res = 0; for (int i = 0; i < nums.size(); i++) { res += abs(nums[i] - nums[mid]); } return res; } void solve() { string s; cin >> s; int n = s.size(); cout << 3 << '\n'; cout << "R" << " " << n - 1 << '\n'; cout << "L" << " " << n << '\n'; cout << "L" << " " << 2 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<int> vec; int n, x; void Fun(int l, int r, int& c) { if (l >= n) return; int t = ceil(1.0 * x / vec[l]); if (r - t + 1 >= l) { ++c; Fun(l + 1, r - t + 1, c); } return; } void Solve() { cin >> n >> x; vec.resize(n); for (auto& el : vec) cin >> el; sort(vec.begin(), vec.end(), greater<int>()); int ans = 0, c = 1; for (auto& el : vec) { if (el * c >= x) { ++ans; c = 0; } ++c; } cout << ans << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) Solve(); }
3
#include <iostream> using namespace std; int main() { int n, q; while (cin >> n >> q) { if (n == 0 && q == 0) { break; } int hyou[101] = {0}; for (int i = 0; i < n; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int a; cin >> a; hyou[a]++; } } int max = 0; int data = 0; for (int i = 0; i < 101; i++) { if (hyou[i] >= q) { if (hyou[i] > max) { max = hyou[i]; data = i; } } } cout << data << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<int> tu[120]; long long dp[120][70], zs[120], biaoji[120]; long long n, k; long long mod = 1e9 + 7; void dfs(int gen) { if (biaoji[gen]) return; biaoji[gen] = 1; dp[gen][0] = dp[gen][k + 1] = 1; for (int a = 0; a < tu[gen].size(); a++) { if (biaoji[tu[gen][a]]) continue; dfs(tu[gen][a]); memset(zs, 0, sizeof(zs)); for (int b = 0; b <= 2 * k; b++) { for (int c = 0; c <= 2 * k; c++) { if (b + c <= 2 * k) { zs[min(b, c + 1)] += dp[gen][b] * dp[tu[gen][a]][c] % mod; zs[min(b, c + 1)] %= mod; } else { zs[max(b, c + 1)] += dp[gen][b] * dp[tu[gen][a]][c] % mod; zs[max(b, c + 1)] %= mod; } } } for (int b = 0; b <= 2 * k; b++) dp[gen][b] = zs[b]; } } int main() { cin >> n >> k; long long q, w; for (long long a = 1; a < n; a++) { cin >> q >> w; tu[q].push_back(w); tu[w].push_back(q); } dfs(1); long long gs = 0; for (long long a = 0; a <= k; a++) gs = (gs + dp[1][a]) % mod; cout << (gs + mod) % mod << endl; }
3
#include <bits/stdc++.h> template <class T> T min(T x, T y) { return x < y ? x : y; } int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int s[4010][4010], f[4010][4010], n, k; void Dfs(int x, int L, int R, int l, int r) { if (L > R) return; int mid = (L + R) >> 1, MID; f[x][mid] = 0x7fffffff >> 1; for (int i = l; i <= min(r, mid); i++) if (f[x][mid] > f[x - 1][i - 1] + s[mid][mid] - s[i - 1][mid] - s[mid][i - 1] + s[i - 1][i - 1]) f[x][mid] = f[x - 1][i - 1] + s[mid][mid] - s[i - 1][mid] - s[mid][i - 1] + s[i - 1][i - 1], MID = i; Dfs(x, L, mid - 1, l, MID), Dfs(x, mid + 1, R, MID, r); } signed main() { n = read(), k = read(); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + read(); for (int i = 1; i <= n; i++) f[0][i] = 0x7fffffff >> 1; for (int i = 1; i <= k; i++) Dfs(i, 1, n, 1, n); printf("%d\n", f[k][n] >> 1); return 0; }
5
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; typedef double db; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pi> vpi; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define FR(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define RF(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) #define sz(x) int((x).size()) const int MAXN = (int)(2e5)+5;; int H[MAXN]; int main() { cin.tie(0)->sync_with_stdio(0); int T; cin >> T; iter: while (T-->0) { int N, K; cin >> N >> K; FR(i, N) { cin >> H[i]; } int l = H[0]-K+1; int r = H[0]+K-1; FOR(i, 1, N-1) { int l1 = H[i]; int r1 = H[i]+K-1; l = max(l1, l); r = min(r1, r); if (r < l) { cout << "NO" << '\n'; goto iter; } else { r += K-1; l -= K-1; } } if (l <= H[N-1] && H[N-1] <= r) { cout << "YES" << '\n'; } else { cout << "NO" << '\n'; } } }
3
#include <bits/stdc++.h> using namespace std; int n, m; int a, b; int data[1001] = {0}; int find(int n) { int parent = n; vector<int> compress; while (data[parent] != parent) { compress.push_back(parent); parent = data[parent]; } for (auto i : compress) data[i] = parent; return parent; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) data[i] = i; for (int i = 0; i < m; i++) { cin >> a >> b; int a_find = find(a); int b_find = find(b); if (a_find == b_find) { cout << "no"; return 0; } data[a_find] = b_find; } int check = find(b); for (int i = 0; i < n; i++) { if (find(i) != check) { cout << "no"; return 0; } } cout << "yes"; return 0; }
3
#include<iostream> #include <string> using namespace std; int weak(string& s, int& i); int strong(string& s, int& i); int fc(string& s, int& i); int inte(string& s, int& i); int weak(string& s, int& i) { int buf = strong(s, i); while (s[i] == '+' || s[i] == '-') { if (s[i] == '+') { i++; buf += strong(s, i); } else { i++; buf -= strong(s, i); } } return buf; } int strong(string& s, int& i) { int buf = fc(s, i); while (s[i] == '*' || s[i] == '/') { if (s[i] == '*') { i++; buf *= fc(s, i); } else { i++; buf /= fc(s, i); } } return buf; } int fc(string& s, int& i) { if ('0' <= s[i] && '9' >= s[i])return inte(s, i); else { i++; int re = weak(s, i); i++; return re; } } int inte(string& s, int& i) { int buf = s[i] - '0'; i++; while ('0' <= s[i] && '9' >= s[i]) { buf = buf * 10 + s[i] - '0'; i++; } return buf; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; int j = 0; cin >> s; cout << weak(s, j) << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { long int n; cin >> n; n = n % 4; if (n == 0 || n == 3) cout << "0" << endl; else if (n == 1) cout << "1" << endl; else if (n == 2) cout << "1" << endl; }
1
#include <bits/stdc++.h> using namespace std; struct point { long long x, y; point(long long _x, long long _y) { x = _x; y = _y; } point() {} }; struct cmp { bool operator()(point a, point b) { return (a.x < b.x); } }; struct vec { long long x, y; vec(long long _x, long long _y) { x = _x; y = _y; } vec() {} }; set<point, cmp> daftar; int q, jenis, last = 0; long long totalmana, a, b; bool ans; long double gradien(point p1, point p2) { return (long double)(p1.y - p2.y) / (p1.x - p2.x); } long double manacost(long double indeks) { set<point, cmp>::iterator it1, it2; it1 = daftar.lower_bound(point((long long)indeks, 0)); it2 = daftar.upper_bound(point((long long)indeks, 0)); if (it1 == it2) it1--; if (it2 == daftar.end()) return 1000000000001LL; return (long double)it1->y + gradien(*it1, *it2) * ((long double)indeks - it1->x); } vec toVec(point awal, point akhir) { return vec(akhir.x - awal.x, akhir.y - awal.y); } long double cross(vec v1, vec v2) { return (long double)v1.x * (long double)v2.y - (long double)v1.y * (long double)v2.x; } long double cross(point p1, point p2, point p3) { return cross(toVec(p1, p2), toVec(p1, p3)); } int keatas(int pembilang, int penyebut) { int ret = pembilang / penyebut; if ((pembilang % penyebut) != 0) ret++; return ret; } void seimbangkan(point baru) { auto it = daftar.find(baru); while (prev(it) != daftar.begin()) { auto it2 = prev(it); auto it3 = prev(it2); if (cross(*it3, *it2, *it) > 1e-9) break; daftar.erase(it2); } while (next(it) != daftar.end()) { auto it2 = next(it); auto it3 = next(it2); if (cross(*it, *it2, *it3) > 1e-9) break; daftar.erase(it2); } } int main() { daftar.insert(point(0, 0)); scanf("%d%lld", &q, &totalmana); for (int query = 1; query <= q; query++) { scanf("%d%lld%lld", &jenis, &a, &b); a = (a + last) % 1000000 + 1; b = (b + last) % 1000000 + 1; if (jenis == 1) { if (manacost(a) > (long double)b + 1e-9) { daftar.insert(point(a, b)); seimbangkan(point(a, b)); } } else { swap(a, b); auto it = daftar.lower_bound(point((a + b - 1) / b, 0)); ans = false; if (it != daftar.end()) { if (it->y * b <= totalmana) ans = true; else { auto it2 = prev(it); if ((cross(toVec(point(it2->x * b, it2->y * b), point(a, totalmana)), toVec(point(it2->x * b, it2->y * b), point(it->x * b, it->y * b)))) < 1e-9) ans = true; } } if (ans) printf("YES\n"), last = query; else printf("NO\n"); } } }
6
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& cout, pair<T1, T2>& p) { cout << p.first << "\t" << p.second << "\n"; return cout; } template <class T> istream& operator>>(istream& cin, vector<T>& arr) { for (long long i = 0; i < arr.size(); i++) cin >> arr[i]; return cin; } template <class T> ostream& operator<<(ostream& cout, vector<T>& arr) { for (long long i = 0; i < arr.size(); i++) cout << arr[i] << " "; cout << "\n"; return cout; } template <class T> ostream& operator<<(ostream& cout, vector<vector<T>>& arr) { for (long long i = 0; i < arr.size(); i++) { for (long long j = 0; j < arr[i].size(); j++) { cout << arr[i][j] << " "; } cout << "\n"; } return cout; } template <class T1, class T2> ostream& operator<<(ostream& cout, vector<pair<T1, T2>>& arr) { for (long long i = 0; i < arr.size(); i++) cout << arr[i].first << "\t" << arr[i].second << "\n"; return cout; } long long dirx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; long long diry[] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long dx[] = {-1, 1, 0, 0}; long long dy[] = {0, 0, -1, 1}; long long min(long long a, long long b) { if (a < b) return a; return b; } long long max(long long a, long long b) { if (a > b) return a; return b; } void solve() { long long n; cin >> n; vector<long long> arr(n); cin >> arr; if (n == 1) { cout << "1 1\n" << arr[0] * (-1) << "\n"; cout << "1 1\n0\n"; cout << "1 1\n0\n"; } else { cout << "1 " << n - 1 << "\n"; for (long long i = 0; i < n - 1; i++) { cout << arr[i] * (n - 1) << " "; arr[i] += arr[i] * (n - 1); } cout << "\n"; cout << n << " " << n << "\n"; cout << arr[n - 1] * (-1) << "\n"; arr[n - 1] = 0; cout << "1 " << n << "\n"; for (auto e : arr) cout << (-1) * e << " "; cout << "\n"; } } int32_t main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); }; long long testCases = 1; for (long long i = 1; i <= testCases; i++) { solve(); } return 0; }
3
#include <bits/stdc++.h> int num[1024]; int main() { int k; int n; int temp; scanf("%d", &k); while (k--) { memset(num, 0, sizeof(int) * 1024); scanf("%d", &n); int i; int max = 1; for (i = 0; i < n; ++i) { scanf("%d", &temp); int j; for (j = 1; j <= temp; ++j) ++num[j]; } for (i = 1; i <= n; ++i) { if (num[i] >= i) max = i; } printf("%d\n", max); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int T, N, K, L, t, d, a; int main() { cin >> T; while (T--) { a = t = 0; cin >> N >> K >> L; while (N--) { cin >> d; d -= L; t *= d > -K; t = std::max(t, K + d); a |= d + t++ > K; } if (a) cout << "No" << endl; else cout << "Yes" << endl; } }
2
#include <bits/stdc++.h> using namespace std; int mov[256][2]; int c[111][2]; int a, b, n, i, x; char s[111]; int f(int a, int b) { if (a != 0) return b / a; else return 0; } int main() { mov['U'][0] = 0; mov['U'][1] = 1; mov['D'][0] = 0; mov['D'][1] = -1; mov['L'][0] = -1; mov['L'][1] = 0; mov['R'][0] = 1; mov['R'][1] = 0; scanf("%d%d\n", &a, &b); gets(s); n = strlen(s); for (i = 0; i < n; i++) { c[i + 1][0] = c[i][0] + mov[s[i]][0]; c[i + 1][1] = c[i][1] + mov[s[i]][1]; } for (i = 0; i < n; i++) { x = max(max(f(c[n][0], a - c[i][0]), f(c[n][1], b - c[i][1])), 0); if (x * c[n][0] == a - c[i][0] && c[n][1] * x == b - c[i][1]) { printf("Yes"); return 0; } } printf("No"); return 0; }
1
#include <iostream> #include <algorithm> using namespace std; int main() { int d; scanf("%d", &d); int c[26]; int s[365][26]; int t[365]; for (int i = 0; i < 26; i++) { scanf("%d", &c[i]); } for (int i = 0; i < d; i++) { for (int j = 0; j < 26; j++) { scanf("%d", &s[i][j]); } } for (int i = 0; i < d; i++) { scanf("%d", &t[i]); t[i]--; } int last[26] = {}; long long ans = 0; for (int i = 0; i < d; i++) { last[t[i]] = i + 1; ans += s[i][t[i]]; for (int j = 0; j < 26; j++) { ans -= c[j] * (i + 1 - last[j]); } printf("%lld\n", ans); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 500005; const long long MOD = 1e9 + 7; int n, m, k, z[2 * N], match[2][N + 1]; char tmp[N]; string s, t; int main() { scanf("%d%d%d", &n, &m, &k); scanf("%s", tmp); s = string(tmp); scanf("%s", tmp); t = string(tmp); memset(match, -1, sizeof match); for (int c = 0; c < 2; c++) { string str = t + "#" + s; memset(z, 0, sizeof z); vector<vector<int> > mx(m + 1); set<int> a; for (int i = 1, L = 0, R = 1; i < str.size(); i++) { if (i < R) z[i] = min(z[i - L], R - i); while (i + z[i] < str.size() && str[i + z[i]] == str[z[i]]) z[i]++; if (i + z[i] > R) L = i, R = i + z[i]; } for (int i = 0; i < n; i++) { mx[z[i + (m + 1)]].push_back(i); if (z[i + (m + 1)] >= m) { int x = min(i, n - 2 * k), y = x + k; if (y >= n) continue; puts("Yes"); printf("%d %d\n", x + 1, y + 1); return 0; } } for (int i = m; i > 0; i--) { for (int j = 0; j < mx[i].size(); j++) a.insert(mx[i][j]); if (auto it = a.lower_bound(k - i); it != a.end()) match[c][i] = *it; } reverse(s.begin(), s.end()); reverse(t.begin(), t.end()); } for (int i = 1; i < m; i++) { if (i <= k && m - i <= k && match[0][i] != -1 && match[1][m - i] != -1) { int x = match[0][i] + i - 1, y = match[1][m - i] + (m - i) - 1; if (x + y >= n - 1) continue; puts("Yes"); printf("%d %d\n", x - (k - 1) + 1, n - 1 - y + 1); return 0; } } puts("No"); return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> inline 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 <class T> inline 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 <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; } string form(string a) { string ret = ""; for (__typeof((int)a.size()) i = 0; i < ((int)a.size()); i++) { if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 32; if (a[i] != ' ' && a[i] != '?') ret.push_back(a[i]); } return ret; } int main() { string a; while (getline(cin, a)) { a = form(a); if (a[a.size() - 1] == 'a' || a[a.size() - 1] == 'e' || a[a.size() - 1] == 'i' || a[a.size() - 1] == 'o' || a[a.size() - 1] == 'u' || a[a.size() - 1] == 'y') puts("YES"); else puts("NO"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, k; cin >> n >> m >> k; long long w, x, y; vector<pair<long long, long long>> v[n + 1]; map<pair<long long, long long>, long long> mapp; for (long long i = 0; i < m; i++) { cin >> x >> y >> w; v[x].push_back(make_pair(y, w)); v[y].push_back(make_pair(x, w)); mapp[make_pair(x, y)] = i + 1; mapp[make_pair(y, x)] = i + 1; } long long d[n + 1]; set<pair<long long, long long>> s; bool vis[n + 1]; long long parent[n + 1]; for (long long i = 0; i <= n; i++) { vis[i] = false; d[i] = 1e18; } d[1] = 0; parent[1] = 1; s.insert(make_pair(0, 1)); long long cnt = 0; while (!s.empty()) { pair<long long, long long> ele = *s.begin(); long long wt = ele.first; long long pos = ele.second; s.erase(ele); if (vis[pos] == true) continue; vis[pos] = true; cnt++; if (cnt == k + 1) break; for (auto j : v[pos]) { if (d[j.first] > d[pos] + j.second) { parent[j.first] = pos; d[j.first] = d[pos] + j.second; s.insert(make_pair(d[j.first], j.first)); } } } vector<long long> ans; for (long long i = 2; i <= n; i++) { if (vis[i] == true) { ans.push_back(mapp[make_pair(i, parent[i])]); } } cout << ans.size() << endl; ; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " "; }
4
#include <bits/stdc++.h> using namespace std; const int L_N = 1e6 + 10; int N, M; const int INF = 1e8; struct node { int l_mi, l_mx; int r_mi, r_mx; int l, r; node() { l_mi = r_mi = INF; } } t[L_N]; void cant() { printf("IMPOSSIBLE\n"); exit(0); } int st[L_N][22]; int rmq(int l, int r) { if (l > r) return 0; int ret = 0; for (int k = 21; k >= 0; k--) { int x = l + (1 << k); if (x <= r + 1) ret = max(ret, st[l][k]), l = x; } return ret; } void solve(int u, int l, int r) { if (l > r) return; int l_need = l - 1; if (t[u].l_mx) l_need = rmq(l, t[u].l_mx); if (l_need >= t[u].r_mi) cant(); if (l_need > r) printf("bug!!\n"); if (l_need >= l) t[u].l = u + 1, solve(u + 1, l + 1, l_need); if (l_need < r) t[u].r = l_need + 1, solve(l_need + 1, l_need + 2, r); } void dfs(int u) { if (t[u].l) dfs(t[u].l); printf("%d ", u); if (t[u].r) dfs(t[u].r); } int main() { scanf("%d %d", &N, &M); for (int i = 1; i <= M; i++) { int a, b; scanf("%d %d", &a, &b); char dir[10]; scanf("%s", dir); if (a >= b) cant(); if (dir[0] == 'L') { t[a].l_mi = min(t[a].l_mi, b); t[a].l_mx = max(t[a].l_mx, b); } else { t[a].r_mi = min(t[a].r_mi, b); t[a].r_mx = max(t[a].r_mx, b); } } for (int i = N; i >= 1; i--) { int mx = max(i, max(t[i].l_mx, t[i].r_mx)); mx = max(mx, rmq(i + 1, mx)); st[i][0] = mx; for (int k = 1; k < 22; k++) { st[i][k] = st[i][k - 1]; if (i + (1 << (k - 1)) <= N) st[i][k] = max(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]); } } solve(1, 2, N); dfs(1); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main(){ long long int l, a, b, m; cin >> l >> a >> b >> m; vector<long long int> pow10(19, 1);// pow10[i] = 10^i for(int i=0; i+1<19; i++) pow10[i+1] = pow10[i] * 10; vector<vector<long long int>> base_matrix(3, vector<long long int>(3)); base_matrix[0][0] = 1; base_matrix[0][1] = 1; base_matrix[1][1] = 1; base_matrix[1][2] = b % m; base_matrix[2][2] = 1; bool flag = false; vector<long long int> ans = {0, (a % m), 1}; for(int d=0; d<18; d++){ if(pow10[d+1] > a+(l-1)*b) flag = true; base_matrix[0][0] = pow10[d+1] % m; vector<vector<long long int>> matrix = base_matrix; long long int c = 0; if((pow10[d+1]-1) - a >= 0LL) c = (min(a+(l-1)*b, (pow10[d+1]-1)) - a) / b + 1; if((pow10[d] -1) - a >= 0LL) c -= ((pow10[d]-1) - a) / b + 1; //cout << c << " "; for(int b=0; b<60; b++){ if(c >> b & 1){ vector<long long int> memo_ans(3); for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ memo_ans[i] += (matrix[i][j] * ans[j]) % m; memo_ans[i] %= m; } } ans = memo_ans; } // doubling vector<vector<long long int>> memo_matrix(3, vector<long long int>(3)); for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ for(int k=0; k<3; k++){ memo_matrix[i][j] += (matrix[i][k] * matrix[k][j]) % m; memo_matrix[i][j] %= m; } } } matrix = memo_matrix; } if(flag == true) break; } cout << ans[0] << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll N = 2e6 + 87; const ll MOD = 1e9 + 7; ll f[N], fi[N]; ll c(ll n, ll k) { return f[n] * fi[k] % MOD * fi[n - k] % MOD; } int main() { ios::sync_with_stdio(0); cin.tie(0); f[0] = f[1] = fi[1] = fi[0] = 1; for (int i = 2; i < N; ++i) f[i] = i * f[i - 1] % MOD; for (int i = 2; i < N; ++i) fi[i] = (MOD - MOD / i) * fi[MOD % i] % MOD; for (int i = 2; i < N; ++i) (fi[i] *= fi[i - 1]) %= MOD; ll r1, c1, r2, c2, ans = 0; cin >> r1 >> c1 >> r2 >> c2; for (ll i = r1; i <= r2; ++i) ans += c(c2 + i + 1, i + 1) - c(c1 + i, i + 1); cout << (ans % MOD + MOD) % MOD; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const int MINF = 0x80000000; const long long mod = 1000000007; const int cons = 60; const int b16 = 1 << 16; int s[cons]; int prime[16] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; int ans[101]; int xt[101][b16]; int yt[101][b16]; int main() { for (int j = 1; j < cons; j++) { for (int i = 0; i < 16; i++) { if (j % prime[i] == 0) s[j] = s[j] | (1 << i); } } int n; cin >> n; memset(xt, 0x7f, sizeof(xt)); for (int i = 0; i < b16; i++) xt[0][i] = 0; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); for (int j = 0; j < b16; j++) { for (int k = 1; k < cons; k++) { if (!(s[k] & j)) { int t = s[k] | j; if (xt[i][t] > xt[i - 1][j] + abs(x - k)) { xt[i][t] = xt[i - 1][j] + abs(x - k); yt[i][t] = k; } } } } } int t = 0; for (int i = 0; i < b16; i++) { if (xt[n][i] < xt[n][t]) { t = i; } } for (int i = n; i; --i) { int x = yt[i][t]; ans[i] = x; t ^= s[x]; } for (int i = 1; i <= n; i++) { printf("%d ", ans[i]); } return 0; }
2
#include <bits/stdc++.h> const int N = 2e5 + 10; long long dp[N]; int a[N]; long long sum[N]; int main() { int n; std::cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum[i] += sum[i - 1] + a[i]; } long long maxn = sum[n]; for (int i = n - 1; i >= 1; i--) { dp[i] = maxn; maxn = std::max(maxn, sum[i] - dp[i]); } printf("%lld\n", dp[1]); }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 105; const long long INF = 1e18; const long long magic = 100000; vector<int> cycle; vector<set<int>> g; vector<int> used; void euler_cycle(int v) { used[v] = 1; stack<int> s; s.push(v); while (!s.empty()) { int v = s.top(); if (g[v].empty()) { s.pop(); cycle.push_back(v); } else { int to = *g[v].begin(); g[v].erase(to); g[to].erase(v); s.push(to); } } } void solve() { int n, m; cin >> n >> m; g.resize(n + 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; g[u].insert(v); g[v].insert(u); } for (int i = 0; i < n; i++) { if (g[i].size() % 2) { g[i].insert(0); g[0].insert(i); } } used.assign(n + 1, -1); vector<pair<int, int>> all_edges; for (int i = 0; i < n; i++) { if (used[i] == -1) { cycle.clear(); euler_cycle(i); if (cycle.size() == 1) continue; cycle.push_back(cycle[0]); vector<pair<int, int>> edges; for (int i = 0; i < cycle.size() - 1; i++) { edges.push_back({cycle[i], cycle[i + 1]}); if (edges[i].first > edges[i].second) swap(edges[i].first, edges[i].second); } for (int i = 0; i < edges.size(); i++) { if (edges[i].first == edges[i].second) continue; if (edges[i].first != 0 && ((i % 2) == 0)) { all_edges.push_back(edges[i]); continue; } if ((i % 2) == 1 && edges[i].first != 0) { int prev = (i + edges.size() - 1) % edges.size(); int next = (i + 1) % edges.size(); if (edges[prev].first == 0) { all_edges.push_back(edges[i]); } else if (edges[next].first == 0) { all_edges.push_back(edges[i]); } } } } } cout << all_edges.size() << endl; for (int i = 0; i < all_edges.size(); i++) { cout << all_edges[i].first << " " << all_edges[i].second << endl; } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main(){ int r,c; while(1){ cin>>r>>c; if(r==0) return 0; int senbei[r][c]; for(int i=0;i<r;i++){ for(int j=0;j<c;j++) cin>>senbei[i][j]; } int ans=0; bool temp[r][c]; for(int t=0;t<(1<<r);t++){ for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ temp[i][j]=senbei[i][j]; } } for(int i=0;i<r;i++){ if((t>>i)%2==1){ for(int j=0;j<c;j++) temp[i][j]^=1; } } int cnt=0; for(int i=0;i<c;i++){ int omote=0,ura=0; for(int j=0;j<r;j++){ if(temp[j][i]) omote++; else ura++; } cnt+=max(omote,ura); } ans=max(ans,cnt); } cout<<ans<<endl; } }
0
#include <iostream> #include <algorithm> #include <map> using namespace std; int main(){ int n; long long int a[100000]; int count = 0; map<long int, int> m; cin >> n; for(int i = 0;i < n;i++){ cin >> a[i]; m[a[i]] = i; } sort(a, a + n); for(int i = 0;i < n;i++){ if((m[a[i]] - i) % 2){ count++; } } cout << (count / 2) << endl; }
0
#include <bits/stdc++.h> using namespace std; int chel[100005]; pair<int, int> min1[530], min2[530]; pair<int, pair<int, int> > summa[530]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { int kol; cin >> kol; for (int j = 1; j <= kol; j++) { int first; cin >> first; chel[i] += (1 << (first - 1)); } } for (int i = 1; i <= m; i++) { int kol, mask = 0, cost; cin >> cost >> kol; for (int j = 1; j <= kol; j++) { int first; cin >> first; mask += (1 << (first - 1)); } if (min1[mask].first == 0) min1[mask] = make_pair(cost, i); else { if (min2[mask].first == 0) { min2[mask] = make_pair(cost, i); if (min1[mask].first > min2[mask].first) swap(min1[mask], min2[mask]); } else { if (cost <= min1[mask].first) { min2[mask] = min1[mask]; min1[mask] = make_pair(cost, i); } else if (cost <= min2[mask].first) min2[mask] = make_pair(cost, i); } } } for (int i = 0; i < 512; i++) for (int j = 0; j < 512; j++) { if (min1[i].first == 0 || min1[j].first == 0) continue; if (i == j && min2[i].first == 0) continue; int ms = (i | j), sum = 0, uk1 = 0, uk2 = 0; if (i != j) sum = min1[i].first + min1[j].first, uk1 = min1[i].second, uk2 = min1[j].second; else sum = min1[i].first + min2[i].first, uk1 = min1[i].second, uk2 = min2[i].second; if (summa[ms].first == 0 || summa[ms].first > sum) summa[ms] = make_pair(sum, make_pair(uk1, uk2)); } int ans = -1, ts = 0, pos1 = 0, pos2 = 0; for (int mask = 0; mask < 512; mask++) { if (summa[mask].first == 0) continue; int tek = 0; for (int i = 1; i <= n; i++) if ((mask & chel[i]) == chel[i]) tek++; if (tek > ans || (tek == ans && summa[mask].first < ts)) ans = tek, ts = summa[mask].first, pos1 = summa[mask].second.first, pos2 = summa[mask].second.second; } cout << pos1 << ' ' << pos2; return 0; }
6
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<n;i++) using namespace std; void func(int d, vector<int>&v) { for (int i = 1; i*i <= d; i++) { if (d%i == 0)v.push_back(i); } } int main() { int a, b; while (scanf("%d%d", &a, &b), a) { vector<int>u, v; func(a, u); func(b, v); int Min = INT_MAX; for (int i : u)for (int j : v) { int k = a / i, l = b / j; int p[]{ i,j,k,l }; sort(p, p + 4); int sum = 0; rep(i, 3)sum += pow(p[i + 1] - p[i], 2); Min = min(Min, sum); } printf("%d\n", Min); } }
0
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int,int> P; const double EPS = 1e-12; const int INF = numeric_limits<int>::max()/2; const int MOD = 1e9+7; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n,m; while(cin>>n>>m,n){ vector<ll> s(n),d(m+1); for(int i=0;i<n;i++) cin>>s[i]; for(int i=1;i<=m;i++) cin>>d[i]; sort(d.begin(),d.end()); vector<vector<ll>> dp(m+1,vector<ll>(1<<n,0)); vector<ll> sum(1<<n,0); for(int i=0;i<(1<<n);i++){ ll tmp=i; for(int j=0;j<n;j++){ if(tmp%2) sum[i]+=s[j]; tmp/=2; } //cout<<i<<" "<<sum[i]<<endl; } for(int i=1;i<=m;i++){ for(int j=0;j<(1<<n);j++){ dp[i][j]=dp[i-1][j]+abs(sum[j]-d[i]); } for(int j=0;j<(1<<n);j++){ for(int k=0;k<n;k++){ dp[i][j]=min(dp[i][j],dp[i][j & ~(1<<k)]); } } } ll res=INF; for(int i=0;i<(1<<n);i++) res=min(res,dp[m][i]); cout<<res<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int n, k; double l, v1, v2; double binser(double target) { return target / (v1 + v2); } double msafa; double solve(double start, double End, int students) { if (start >= End) { msafa = End; return 0; } if (students == 0) { msafa = min(start + 0.0001, End); return min(0.0001, End - start) / v2; } double t = binser(min(0.0002, 2.0 * (End - start))); double r = solve(start + v1 * t, End, max(0, students - k)); if (start == 0) return l / (msafa / (r + t)); return r + t; } int main() { cin >> n >> l >> v1 >> v2 >> k; if (k == n) { cout << fixed << setprecision(7) << l / v2 << endl; return 0; } cout << fixed << setprecision(10) << solve(0, l, n - k) << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, s, a, b; cin >> n >> s; int ans = s; for (int i = 0; i < n; i++) { cin >> a >> b; ans = max(ans, (a + b)); } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; double pi = 3.14159265; long long n; vector<vector<long long> > g; vector<int> c; long long rt; vector<long long> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; g.resize(n + 1); c.resize(n + 1); for (long long i = 1; i <= n; i++) { long long temp1; int id; cin >> temp1 >> id; if (temp1 == -1) { rt = i; c[i] = id; } else { g[i].push_back(temp1); g[temp1].push_back(i); c[i] = id; } } queue<long long> q; q.push(rt); vector<bool> vis(n + 1, false); vis[rt] = true; while (!q.empty()) { long long cur = q.front(); q.pop(); int flag = 1; for (auto x : g[cur]) { if (!vis[x]) { vis[x] = true; q.push(x); if (c[x] == 0) flag = 0; } } if (c[cur] == 1 && flag == 1) ans.push_back(cur); } sort((ans).begin(), (ans).end()); if (ans.empty()) cout << -1; else for (auto x : ans) cout << x << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { char s[10], t[10]; for (int i = 0; i < 4; ++i) { scanf(" %c", &s[i]); } for (int j = 0; j < 4; ++j) { scanf(" %c", &t[j]); } swap(s[2], s[3]); swap(t[2], t[3]); for (int i = 0, j = 0; i < 4; ++i) { if (s[i] != 'X') { s[j++] = s[i]; } } s[3] = '\0'; for (int i = 0, j = 0; i < 4; ++i) { if (t[i] != 'X') { t[j++] = t[i]; } } t[3] = '\0'; ; for (int i = 0; i < 3; ++i) { int ok = 1; for (int j = 0; j < 3; ++j) { ok &= (s[j] == t[(i + j) % 3]); } if (ok) { printf("YES\n"); return 0; } } printf("NO\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; void solution() { string s; cin >> s; s[0] = toupper(s[0]); cout << s << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; for (int t = 0; t < T; t++) { solution(); } return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000") using namespace std; const int N = 100010; int h[N]; int main() { int n; scanf("%d", &n); int i, j; for (int k = n - 1; k--; scanf("%d%d", &i, &j), h[i - 1]++, h[j - 1]++) ; int f = 1; for (int i = 0; i < n; f &= h[i] != 2, i++) ; if (f) printf("YES\n"); else printf("NO\n"); return 0; }
1
#include<bits/stdc++.h> using namespace std; int main(){ double r; cin>>r; printf("%.7f %.7f\n",r*r*M_PI,2*r*M_PI); }
0
#include <bits/stdc++.h> using namespace std; int main() { char cad1[100]; char cad2[100]; int cd = 0; scanf("%s %s", &cad1, &cad2); for (int i = 0; i < strlen(cad1); ++i) { if (cad1[strlen(cad1) - 1] != '*') { for (int j = 0; j < strlen(cad2); ++j) { if (cad1[i] == cad2[j]) { cad1[i] = '*'; cad2[j] = '+'; cd++; break; } cad2[j] = '+'; } } } printf("%d\n", cd + 1); return 0; }
1
#include<cstdio> using namespace std; int n; int a[20010],b[20010],p[20010]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&p[i]); for (int i=1;i<=n;i++) { a[i]=i*(n+1); b[i]=(n-i+1)*(n+1); } for (int i=1;i<=n;i++) b[p[i]]-=(n-i+1); for (int i=1;i<=n;i++) printf("%d%c",a[i]," \n"[i==n]); for (int i=1;i<=n;i++) printf("%d%c",b[i]," \n"[i==n]); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long cnt(long long l, long long r, vector<vector<long long> > &dp, vector<long long> &a, vector<vector<long long> > &fval) { if (l == r) { return a[l]; } if (dp[l][r] != -1) return dp[l][r]; long long lft = cnt(l + 1, r, dp, a, fval); long long rht = cnt(l, r - 1, dp, a, fval); long long all = fval[l][r]; dp[l][r] = max({lft, rht, all}); return dp[l][r]; } void solve() { long long n; cin >> n; vector<long long> a(n); vector<vector<long long> > fval(n, vector<long long>(n, -1)); vector<vector<long long> > dp(n, vector<long long>(n, -1)); for (long long i = 0; i < n; ++i) cin >> a[i]; for (long long r = 0; r < n; ++r) { fval[r][r] = a[r]; for (long long l = r - 1; l >= 0; --l) { fval[l][r] = fval[l + 1][r] ^ (r - 1 >= 0 ? fval[l][r - 1] : 0); } } long long q; cin >> q; for (long long i = 0; i < q; ++i) { long long l, r; cin >> l >> r; l--, r--; cout << cnt(l, r, dp, a, fval) << "\n"; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; for (long long i = 0; i < t; ++i) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; int T; map<string, int> M; char comando[1024]; char tipo[1024], nome[1024]; int asteriscos(char *s) { int c = 0, n = strlen(s); for (int i = 0; i < n; i++) if (s[i] == '*') c++; return c; } int Es(char *s) { int c = 0, n = strlen(s); for (int i = 0; i < n; i++) if (s[i] == '&') c++; return c; } string seco(char *s) { string r; int n = strlen(s); for (int i = 0; i < n; i++) if (s[i] != '*' and s[i] != '&') r = r + s[i]; return r; } bool valido(char *s) { char est = 'e'; int n = strlen(s); for (int i = 0; i < n; i++) { if (s[i] == '&') { if (est == 'e') continue; else return false; } if ('a' <= s[i] and s[i] <= 'z') { if (est == 'e') est = 'l'; else if (est == 'l') continue; else return false; } if (s[i] == '*') { if (est == 'l') est = 'a'; else if (est == 'a') continue; else return false; } } return est == 'l' or est == 'a'; } int main() { scanf("%d", &T); M.clear(); while (T--) { M["void"] = 0; M["errtype"] = -1; scanf("%s", comando); if (!strcmp(comando, "typedef")) { scanf("%s %s", tipo, nome); string n = nome; int asts = asteriscos(tipo); int es = Es(tipo); int tot = asts - es; string x = seco(tipo); if (M.find(x) == M.end()) tot = -1; else if (M[x] == -1) tot = -1; else tot = tot + M[x]; if (tot < 0) M[n] = -1; else M[n] = tot; } else { scanf("%s", nome); string n = nome; string x = seco(nome); if (M.find(x) == M.end()) { printf("errtype\n"); continue; } int tot = asteriscos(nome) - Es(nome) + M[x]; if (M[x] < 0 or tot < 0) { printf("errtype\n"); } else { printf("void"); int c = tot; for (int i = 0; i < c; i++) printf("*"); printf("\n"); } } } return 0; }
2
#include<bits/stdc++.h> using namespace std; int i,j,a[20],n,b,c; int main() { for(cin>>n;i<n;i++)cin>>a[i]; for(i=1;i<n-1;i++)if((a[i-1]<a[i]&&a[i]<a[i+1])||(a[i-1]>a[i]&&a[i]>a[i+1]))c++; cout<<c; }
0
#include<bits/stdc++.h> using namespace std; int main(){ string s; cin>>s; vector<string> w={"SUN","MON","TUE","WED","THU","FRI","SAT"}; for(int d=0;d<7;d++) if(s==w.at(d)) cout<<7-d<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int x1 = 0, x2 = 0, x3 = 0; cin >> x1 >> x2 >> x3; int ans = abs(x2 - x1) + abs(x3 - x1); if (abs(x1 - x2) + abs(x3 - x2) < ans) { ans = abs(x1 - x2) + abs(x3 - x2); } if (abs(x1 - x3) + abs(x2 - x3) < ans) { ans = abs(x1 - x3) + abs(x2 - x3); } cout << ans; return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; struct Max { vector<int> d; int n; void Init(int n) { this->n = n; d.resize(n * 2, 0); } void Set(int p, int v) { p += n; d[p] = max(d[p], v); for (p >>= 1; p > 0; p >>= 1) { d[p] = max(d[p * 2], d[p * 2 + 1]); } } int Get(int a, int b) { int res = 0; for (a += n, b += n; a < b; a >>= 1, b >>= 1) { if (a & 1) res = max(res, d[a++]); if (b & 1) res = max(res, d[--b]); } return res; } }; struct P { typedef const P& CP; int x, y, z; P() {} friend bool operator<(CP a, CP b) { if (a.x != b.x) return a.x < b.x; if (a.y != b.y) return a.y < b.y; return a.z < b.z; } }; int n; vector<P> pp; vector<int> dify; map<int, int> my; Max mm; int main() { scanf("%d", &n); pp.resize(n); for (int i = 0; i < n; i++) scanf("%d", &pp[i].x); for (int i = 0; i < n; i++) { scanf("%d", &pp[i].y); dify.push_back(pp[i].y); } for (int i = 0; i < n; i++) scanf("%d", &pp[i].z); sort(dify.begin(), dify.end()); dify.erase(unique(dify.begin(), dify.end()), dify.end()); for (int i = 0; i < dify.size(); i++) { my[dify[i]] = i; } int maxy = dify.size() + 10; mm.Init(maxy); sort(pp.begin(), pp.end()); int res = 0; for (int i = n - 1; i >= 0;) { int k = i; while (k > 0 && pp[k - 1].x == pp[i].x) { k--; } for (int j = k; j <= i; j++) { int m = mm.Get(my[pp[j].y] + 1, maxy); if (m > pp[j].z) { res++; } } for (int j = k; j <= i; j++) { mm.Set(my[pp[j].y], pp[j].z); } i = k - 1; } printf("%d\n", res); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; set<string> store; vector<string> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; store.insert(s[i]); } vector<string> left, right; string mid; for (int i = 0; i < n; i++) { string a = s[i]; reverse(a.begin(), a.end()); if (a == s[i]) { mid = a; } else if (store.find(a) != store.end()) { left.push_back(s[i]); right.push_back(a); store.erase(a); store.erase(s[i]); } } cout << left.size() * 2 * m + mid.size() << '\n'; for (auto s : left) { cout << s; } cout << mid; reverse(right.begin(), right.end()); for (auto s : right) { cout << s; } cout << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, x; cin >> n; vector<int> g[n + 1]; for (int i = 0; i < n; i++) { cin >> x; g[i + 1].push_back(x); } long long dp[n + 1][n + 1], mod = 1000000007; for (int i = 1; i < n + 1; i++) { for (int j = i; j < i + 2; j++) { if (i == j) dp[i][j] = 0; else dp[i][j] = 2; } } for (int i = 1; i <= n; i++) { int j = g[i][0]; int k; long long sum = 0; if (j != i) { k = j + 1; while (j != i) { sum = ((sum % mod) + (dp[j][j + 1] % mod)); j++; } } dp[i][i + 1] = ((dp[i][i + 1] % mod) + (sum % mod)) % mod; } long long count = 0; for (int i = 1; i <= n; i++) { count = ((count % mod) + dp[i][i + 1] % mod) % mod; } cout << (count % 1000000007) << endl; }
4
#include <bits/stdc++.h> using namespace std; bool pairCompare1(const std::pair<string, int>& firstElem, const std::pair<string, int>& secondElem) { return firstElem.second > secondElem.second; } bool pairCompare2(const std::pair<int, int>& firstElem, const std::pair<int, int>& secondElem) { return firstElem.first < secondElem.first; } void init(int arr[], int size) { for (long long int i = 0; i < size; ++i) { arr[i] = 0; } } int main() { int n; cin >> n; int count = n / 3; int rem = n % 3; count *= 2; if (rem > 0) { count++; } cout << count << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; inline void read(int &a) { a = 0; int c = getchar(), b = 1; while (c > '9' || c < '0') { if (c == '-') b = -1; c = getchar(); } while (c >= '0' && c <= '9') a = (a << 3) + (a << 1) + c - 48, c = getchar(); a *= b; } inline void write(int x) { if (x > 9) write(x / 10); putchar('0' + x % 10); } inline void W(int x) { if (x < 0) { putchar('-'), x = -x; } write(x); } const int N = 5e4 + 5, M = 5e3 + 5, A = 1e6 + 5; int n, m, a[N], l[M], r[M], sum[A], f[N], ans[M]; int main() { for (int i = 1; i <= A - 5; i++) sum[i] = i ^ sum[i - 1]; read(n); read(m); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= m; i++) read(l[i]), read(r[i]); for (int i = 1; i <= n; i++) { f[i] = a[i]; for (int j = i + 1; j <= n; j++) { f[j] = max(f[j - 1], sum[min(a[i], a[j]) - 1] ^ sum[max(a[i], a[j])]); } for (int j = 1; j <= m; j++) { if (i >= l[j] && i <= r[j]) { ans[j] = max(ans[j], f[r[j]]); } } } for (int i = 1; i <= m; i++) W(ans[i]), putchar('\n'); }
6
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") using namespace std; long long l, r, q; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> q; while (q--) { cin >> l >> r; long long ans = l; for (long long i = 0; i <= 63; i++) { if (!(ans & (1ll << i))) { if (ans + (1ll << i) <= r) { ans += (1ll << i); } else break; } } cout << ans << '\n'; } }
1
#include <bits/stdc++.h> using namespace std; long long M = 1e9 + 7; long long mpe(long long base, long long exponent, long long modulus) { long long result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = result * base % modulus; exponent = exponent >> 1; base = base * base % modulus; } return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); ; long long n; cin >> n; long long a; vector<pair<long long, long long> > vec; for (long long i = 0; i < n; ++i) { cin >> a; vec.push_back(make_pair(a, i + 1)); } sort((vec).begin(), (vec).end()); long long an = 0, ma = 0; for (long long i = 0; i < vec.size(); ++i) { ma = max(ma, vec[i].second); if (ma == i + 1) an++; } cout << an; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int main() { int t; cin >> t; while (t--) { int a, b; cin >> a >> b; int x = a & b; cout << (a ^ x) + (b ^ x) << "\n"; } }
1
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int N = 2e5 + 7; long long modpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b /= 2; } return ans; } int n, Q; long long p[N], q[N], cp[N]; complex<long long> dp[N]; complex<long long> im = {0, 1}; long long f(int i, int j) { if (i == j) return 0; complex<long long> sol = dp[i] + (mod - cp[i] * modpow(cp[j], mod - 2) % mod) * dp[j]; sol = {sol.real() % mod, sol.imag() % mod}; return sol.real() * modpow(1 + mod - sol.imag(), mod - 2) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> Q; for (int i = 0; i < n; i++) { cin >> p[i]; p[i] = p[i] * modpow(100, mod - 2) % mod; q[i] = (1 - p[i] + mod) % mod; } dp[n] = {0, 0}; cp[n] = 1; for (int i = n - 1; i >= 0; i--) { dp[i] = dp[i + 1] * p[i] + im * q[i] + 1LL; dp[i] = {dp[i].real() % mod, dp[i].imag() % mod}; cp[i] = cp[i + 1] * p[i] % mod; } set<int> po; po.insert(0); po.insert(n); long long ans = f(0, n); stringstream ss; for (int _ = 0; _ < Q; _++) { int j; cin >> j; j--; int i = *prev(po.lower_bound(j)), k = *po.upper_bound(j); if (po.count(j)) { po.erase(j); ans = (ans + f(i, k) - f(i, j) - f(j, k) + 2 * mod) % mod; } else { po.insert(j); ans = (ans - f(i, k) + f(i, j) + f(j, k) + mod) % mod; } ss << ans << endl; } cout << ss.str(); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int f[3][4] = { {0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, }; int64_t g(int64_t n) { int cat = (n % 3 - 1 + 3) % 3; int basei; for (basei = 0; (1LL << basei) <= n; basei += 2) ; basei -= 2; n = (n - (1LL << basei)) / 3; vector<int> a; while (n > 0) { a.push_back(n % 4); n /= 4; } int64_t result = (int64_t)(cat + 1) << basei; for (int i = 0; i < a.size(); ++i) { result |= (int64_t)f[cat][a[i]] << i * 2; } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int64_t n; cin >> n; cout << g(n) << endl; } }
5
#include <iostream> #include <iomanip> #include <sstream> #include <vector> #include <string> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <functional> #include <iterator> #include <limits> #include <numeric> #include <utility> #include <cmath> #include <cassert> #include <cstdio> using namespace std; using namespace placeholders; typedef long long LL; typedef unsigned long long ULL; typedef vector< int > VI; typedef vector< vector< int > > VVI; typedef vector< string > VS; typedef stringstream SS; typedef pair< int, int > PII; typedef vector< pair< int, int > > VPII; #define VT( T ) vector< T > #define VVT( T ) vector< vector< T > > #define LIM( T ) numeric_limits< T > template < typename T > inline istream& operator>>( istream &s, vector< T > &v ){ for ( T &t : v ) { s >> t; } return s; } template < typename T > inline ostream& operator<<( ostream &s, const vector< T > &v ){ for ( int i = 0; i < int( v.size() ); ++i ){ s << ( " " + !i ) << v[i]; } return s; } template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; }; template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); }; #define REP2( i, n ) REP3( i, 0, n ) #define REP3( i, m, n ) for ( int i = ( int )( m ); i < ( int )( n ); ++i ) #define GET_REP( a, b, c, F, ... ) F #define REP( ... ) GET_REP( __VA_ARGS__, REP3, REP2 )( __VA_ARGS__ ) #define FOR( e, c ) for ( auto &e : c ) #define ALL( c ) ( c ).begin(), ( c ).end() #define AALL( a, t ) ( t* )a, ( t* )a + sizeof( a ) / sizeof( t ) #define DRANGE( c, p ) ( c ).begin(), ( c ).begin() + p, ( c ).end() #define SZ( v ) ( (int)( v ).size() ) #define PB push_back #define EM emplace #define EB emplace_back #define BI back_inserter #define EXIST( c, e ) ( ( c ).find( e ) != ( c ).end() ) #define MP make_pair #define fst first #define snd second #define DUMP( x ) cerr << #x << " = " << ( x ) << endl int main() { cin.tie( 0 ); ios::sync_with_stdio( false ); int N; cin >> N; VT( LL ) as( N ); cin >> as; int M; cin >> M; VT( LL ) bs( M ), cs( M ); cin >> bs >> cs; sort( ALL( as ) ); VT( LL ) csum( 1 ); partial_sum( ALL( as ), BI( csum ) ); REP( i, M ) { cout << ( cs[i] <= csum[ upper_bound( ALL( as ), bs[i] ) - as.begin() ] ? "Yes" : "No" ) << endl; } return 0; }
0
#include <iostream> #include <string> #define int long long #define INF 1e+17 using namespace std; int n; int C(int x){ if(x == 1) return 1; int tmp = 1,ret = 0,keta = 1; while(tmp <= x){ int three = min(tmp * 10,x) / 3 - tmp / 3,five = min(tmp * 10 - 1,x) / 5 - (tmp - 1) / 5; int tf = min(tmp * 10 - 1,x) / 15 - (tmp - 1) / 15; ret += keta * (min(tmp * 10 - 1,x) - (tmp - 1) - three - five + tf) + (three + five) * 4; tmp *= 10; keta++; } return ret; } signed main(){ cin >> n; int low = 0,up = INF; while(up - low > 1){ int mid = (low + up) / 2; if(C(mid) >= n) up = mid; else low = mid; } int num = C(up - 1); string s = ""; for(int i = 0;i < 20;i++){ if((i + up) % 3 == 0 && (i + up) % 5 == 0) s += "FizzBuzz"; else if((i + up) % 5 == 0) s += "Buzz"; else if((i + up) % 3 == 0) s += "Fizz"; else s += to_string(i + up); } cout << s.substr(n - num - 1,20) << endl; return 0; }
0
#include <iostream> #include <vector> #include <set> using namespace std; int N; vector<int> P,Pd; int main() { cin >> N; P = vector<int>(N + 1); Pd = vector<int>(N + 1); for (int i = 1;i<=N;i++) { cin >> P[i]; Pd[P[i]] =i; } long long ans = 0; multiset<int> s; s.insert(0); s.insert(0); s.insert(N + 1); s.insert(N + 1); for (int i = N; i > 0; i--) { s.insert(Pd[i]); auto il = s.find(Pd[i]); auto ir = il; long long l1 = *(--il); long long l2 = *(--il); long long r1 = *(++ir); long long r2 = *(++ir); long long ls = (l1 - l2) * (r1 - Pd[i]); long long rs = (r2 - r1) * (Pd[i] - l1); ans += (ls + rs) * i; } cout << ans << endl; return 0; }
0
#include<iostream> #include<vector> #include<algorithm> #include<cmath> #include<cassert> #include<iomanip> #include<cstdio> #include<cassert> #include<queue> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define inf (1<<29) #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b)) < EPS) #define MAX 2000 #define pow2(a) ((a)*(a)) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 using namespace std; // class Point { public: double x,y; Point(double x = -inf,double y = -inf): x(x),y(y){} Point operator + (Point p){return Point(x+p.x,y+p.y);} Point operator - (Point p){return Point(x-p.x,y-p.y);} Point operator * (double a){return Point(a*x,a*y);} Point operator / (double a){return Point(x/a,y/a);} bool operator < (const Point& p) const { return !equals(x,p.x)?x<p.x:y<p.y; } bool operator == (const Point& p)const { return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; } }; struct Segment { Point p1,p2; Segment(Point p1 = Point(-inf,-inf),Point p2 = Point(-inf,-inf)):p1(p1),p2(p2){} bool operator == (const Segment& p)const { return p.p1 == p1 && p.p2 == p2; } }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; ostream& operator << (ostream& os,const Point& a) { os << "(" << a.x << "," << a.y << ")"; } ostream& operator << (ostream& os,const Segment& a) { os << "( " << a.p1 << " , " << a.p2 << " )"; } double dot(Point a,Point b){ return a.x*b.x + a.y*b.y; } double cross(Point a,Point b){ return a.x*b.y - a.y*b.x; } double norm(Point a){ return a.x*a.x+a.y*a.y; } double abs(Point a){ return sqrt(norm(a)); } //rad は角度をラジアンで持たせること Point rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); } // 度をラジアンに変換 double toRad(double agl){ return agl*M_PI/180.0; } int ccw(Point p0,Point p1,Point p2) { Point a = p1-p0; Point b = p2-p0; if(cross(a,b) > EPS)return COUNTER_CLOCKWISE; if(cross(a,b) < -EPS)return CLOCKWISE; if(dot(a,b) < -EPS)return ONLINE_BACK; if(norm(a) < norm(b))return ONLINE_FRONT; return ON_SEGMENT; } bool intersectLL(Line l, Line m) { return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line } bool intersectLS(Line l, Line s) { return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l } bool intersectLP(Line l,Point p) { return abs(cross(l.p2-p, l.p1-p)) < EPS; } bool intersectSS(Line s, Line t) { return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 && ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0; } bool intersectSP(Line s, Point p) { return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality } Point projection(Line l,Point p) { double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2); return l.p1 + (l.p1-l.p2)*t; } Point reflection(Line l,Point p) { return p + (projection(l, p) - p) * 2; } double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); } double distanceLL(Line l, Line m) { return intersectLL(l, m) ? 0 : distanceLP(l, m.p1); } double distanceLS(Line l, Line s) { if (intersectLS(l, s)) return 0; return min(distanceLP(l, s.p1), distanceLP(l, s.p2)); } double distanceSP(Line s, Point p) { Point r = projection(s, p); if (intersectSP(s, r)) return abs(r - p); return min(abs(s.p1 - p), abs(s.p2 - p)); } double distanceSS(Line s, Line t) { if (intersectSS(s, t)) return 0; return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)), min(distanceSP(t, s.p1), distanceSP(t, s.p2))); } Point crosspoint(Line l, Line m) { double A = cross(l.p2 - l.p1, m.p2 - m.p1); double B = cross(l.p2 - l.p1, l.p2 - m.p1); if (abs(A) < EPS && abs(B) < EPS) return m.p1; // same line if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!! return m.p1 + (m.p2 - m.p1) * (B / A); } // struct Poxas7 { int cur; double cost; Poxas7(int cur=inf,double cost=inf):cur(cur),cost(cost){} bool operator < (const Poxas7 &a)const { return cost > a.cost; } }; int path[MAX],AN,BN,N; double mincost[MAX]; vector<Point> vec[2]; double dijkstra(vector<Point> &vec,int s,int t,vector<Segment> &ban) { N = vec.size(); rep(i,N)path[i] = -1,mincost[i] = inf; priority_queue<Poxas7> Q; Q.push(Poxas7(s,0)); mincost[s] = 0; while(!Q.empty()) { Poxas7 pox = Q.top(); Q.pop(); if(pox.cur == t) { return pox.cost; } rep(next,N) { if(s == next)continue; Segment seg = Segment(vec[pox.cur],vec[next]); bool out = false; rep(i,ban.size()) if(intersectSS(ban[i],seg)) { out = true; break; } if(out)continue; if(mincost[next] > pox.cost + abs(vec[pox.cur]-vec[next])) { path[next] = pox.cur; mincost[next] = pox.cost + abs(vec[pox.cur]-vec[next]); Q.push(Poxas7(next,mincost[next])); } } } return -1; } int main() { while(scanf("%d %d",&AN,&BN) != EOF) { rep(i,2)vec[i].clear(); vec[0].resize(AN); vec[1].resize(BN); rep(i,AN)cin >> vec[0][i].x >> vec[0][i].y; rep(i,BN)cin >> vec[1][i].x >> vec[1][i].y; double ans = inf; rep(i,2) { vector<Segment> ban; double cost = dijkstra(vec[i],0,1,ban); if(cost == -1)continue; int cur = 1; for(int next=path[cur];cur != 0;cur=next,next=path[cur]) { ban.push_back(Segment(vec[i][cur],vec[i][next])); } double cost2 = dijkstra(vec[(i+1)%2],0,1,ban); if(cost2 == -1)continue; ans = min(ans,cost+cost2); } if(ans == inf)cout << "-1" << endl; else cout << setiosflags(ios::fixed) << setprecision(15) << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int max(const int &a, const int &b) { return a > b ? a : b; } const int N = 2000 + 13; int n, k, f[N]; inline void solve() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) f[i] = -1; f[0] = 0; for (int i = 1, x; i <= n; ++i) { scanf("%d", &x); for (int j = n; j; --j) if (f[j - 1] != -1) f[j] = max(f[j], f[j - 1] + (x == j)); } int ans = n; while (ans && f[ans] < k) --ans; printf("%d\n", !ans ? -1 : n - ans); } int main() { int T; scanf("%d", &T); while (T--) solve(); return 0; }
5
#include <algorithm> #include <iostream> #include <vector> using namespace std; vector<int> get_leq(int k, bool eq = true) { vector<int> res; if (eq) res.emplace_back(k); while (k) res.emplace_back(k - 1), k -= k & -k; return res; } int main() { int n, k; cin >> n >> k; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; long long ans = 0; for (int mask: get_leq(k)) { long long tmp = 0; for (int i = 0; i < n; i++) if ((a[i] | mask) == mask) tmp += b[i]; ans = max(ans, tmp); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<vector<int> > vv; int n, m, h, t; vector<pair<int, int> > edges; vector<int> add; int count(int v1, int v2) { int ans = 0; vector<int> all; int id1 = 0, id2 = 0; while (id1 < vv[v1].size() || id2 < vv[v2].size()) { if (id1 == vv[v1].size()) { all.push_back(vv[v2][id2]); id2++; continue; } if (id2 == vv[v2].size()) { all.push_back(vv[v1][id1]); id1++; continue; } if (vv[v1][id1] < vv[v2][id2]) { all.push_back(vv[v1][id1]); id1++; } else { all.push_back(vv[v2][id2]); id2++; } } add.clear(); for (int i = 1; i < all.size(); i++) { if (all[i] == all[i - 1]) { ans++; add.push_back(all[i]); } } return ans; } bool used[100002]; int main() { cin >> n >> m >> h >> t; vv.resize(n + 1); for (int i = 0; i < m; i++) { int from, to; cin >> from >> to; vv[from].push_back(to); vv[to].push_back(from); edges.push_back(make_pair(from, to)); edges.push_back(make_pair(to, from)); } for (int i = 0; i <= n; i++) { sort(vv[i].begin(), vv[i].end()); } for (int i = 0; i < edges.size(); i++) { int u = edges[i].first; int v = edges[i].second; if ((int)vv[u].size() - 1 < h || (int)vv[v].size() - 1 < t) continue; int cnt_all = count(u, v); int cnt1 = vv[u].size() - 1 - cnt_all; int cnt2 = vv[v].size() - 1 - cnt_all; int need = max(0, h - cnt1); cnt_all -= need; cnt1 += need; need = max(0, t - cnt2); cnt_all -= need; cnt2 += need; if (cnt_all >= 0 && cnt1 >= h && cnt2 >= t) { cout << "YES\n"; cout << u << ' ' << v << '\n'; int cnt = 0; used[u] = true; used[v] = true; for (int j = 0; j < add.size(); j++) used[add[j]] = true; for (int j = 0; j < vv[u].size() && cnt < h; j++) { if (!used[vv[u][j]]) { cout << vv[u][j] << ' '; cnt++; used[vv[u][j]] = true; } } int from = 0; while (cnt < h) { cout << add[from] << ' '; cnt++; from++; } cout << '\n'; cnt = 0; for (int j = 0; j < vv[v].size() && cnt < t; j++) { if (!used[vv[v][j]]) { cout << vv[v][j] << ' '; cnt++; } } while (cnt < t) { cout << add[from] << ' '; cnt++; from++; } cout << '\n'; return 0; } } cout << "NO\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int A[1000001], temp; int main() { int N, t; cin >> N; for (int i = 0; i < N; i++) { scanf("%d", A + i); } cin >> t; for (int i = 0; i < t; i++) { cin >> temp; A[temp - 1] *= -1; } stack<long long int> negative; for (int i = N - 1; i >= 0; i--) { if (A[i] < 0) { negative.push(A[i] * -1); } else { if (negative.empty() || A[i] != negative.top()) { negative.push(A[i]); A[i] *= -1; } else { negative.pop(); } } } if (negative.empty()) { cout << "YES" << endl; for (int i = 0; i < N; i++) printf("%d ", A[i]); cout << endl; } else cout << "NO" << endl; }
5