solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; bool checkPrime(long long int x) { if (x == 1) { return false; } if (x <= 3) { return true; } long long int t = 2; if (x % 2 == 0) { return false; } t = 3; for (t; t <= sqrt(x); t += 2) { if (x % t == 0) { return false; } } return true; } unsigned int reverse(unsigned int A) { unsigned int B = 0; for (int i = 0; i < 32; i++) { int k = A & 1; B <<= 1; B |= k; A >>= 1; } return B; } long long int fact(long long int n) { long long int res = 1; for (long long int i = 2; i <= n; i++) res = res * i; return res; } long long int nCr(long long int n, long long int r) { return fact(n) / (fact(r) * fact(n - r)); } void allSumadj(int arr[], int n, vector<long long int>* v, int* it) { if (n == 0) { return; } allSumadj(arr + 1, n - 1, v, it); long long int size = v->size(); v->push_back(arr[0]); for (long long int k = (*it); k < size; k++) { v->push_back(v->at(k) + arr[0]); } *it = size; } void allSum(long long int arr[], long long int n, vector<long long int>* v) { if (n == 0) { return; } allSum(arr + 1, n - 1, v); long long int size = v->size(); for (long long int k = 0; k < size; k++) { v->push_back(v->at(k) + arr[0]); } v->push_back(arr[0]); } bool checkPow(long long int x, long long int k) { while (abs(x) > 1) { if (x % k != 0) return false; x = x / k; } return true; } string reversee(string s) { if (s.length() == 0) return ""; string ans = reversee(s.substr(1)) + s.substr(0, 1); return ans; } string gives(string s, int i) { if (i == s.length()) return reversee(s); string dum, ss = ""; dum = reversee(s.substr(0, i)); dum = dum + s.substr(i); string fs = gives(dum.substr(1), i); return dum.substr(0, 1) + fs; } int no_of_ones(int x, int y) { int count = 0; for (int i = 31; i >= 0; i--) { bool b1 = x & (1 << i); bool b2 = y & (1 << i); int num = (b1 & b2) ? 0 : (b1 | b2); if (num == 1) count++; } return count; } long long int givestringvalue(string s) { long long int sum = 0, p = 0; long long int l = s.length(); while (p < l) { long long int n = s[p] == '1' ? 1 : 0; if (n) sum = sum + pow(2, l - 1 - p); p++; } return sum; } void givesubstrings(string s, vector<string>* v) { long long int l = s.length(); for (long long int sl = 1; sl <= l; sl++) for (long long int i = 0; i <= l - sl; i++) v->push_back(s.substr(i, sl)); } void givesubsequence(string s, vector<string>* v) { if (s.length() == 1) { v->push_back(s); return; } givesubsequence(s.substr(1), v); long long int a = v->size(); for (long long int i = 0; i < a; i++) { string p = s[0] + v->at(i); v->push_back(p); } v->push_back(s.substr(0, 1)); } long long int gcd(long long int a, long long int b) { if (a == 0) return b; if (a > b) return gcd(a % b, b); else { return gcd(b % a, a); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int x, y, cnt = 0; cin >> x >> y; while (x > 0 && y > 0) { if (x <= y && y >= 2) { x--; y -= 2; cnt++; } else if (y < x) { y--; x -= 2; cnt++; } if (x == 1 && y == 1) break; } cout << cnt << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int l[6], r[6]; int n; double ans; inline int wei(int x, int y) { return (x >> y) & 1; } inline int cnt(int x) { int pa = 0; for (int i = (0); i <= (int)n - 1; i++) pa += wei(x, i); return pa; } int main() { scanf("%d", &n); for (int i = (1); i <= (int)n; i++) scanf("%d%d", &l[i], &r[i]); for (int j = (1); j <= (int)(1 << n) - 1; j++) if (cnt(j) > 1) { int lp = 0; int rp = 100000; for (int i = (1); i <= (int)n; i++) if (wei(j, i - 1)) { lp = max(lp, l[i]); rp = min(rp, r[i]); } for (int hi = (lp); hi <= (int)rp; hi++) { double ep = 1; for (int k = (1); k <= (int)n; k++) if (!wei(j, k - 1)) { if (l[k] >= hi) ep = 0; else ep *= 1. * min(hi - l[k], r[k] - l[k] + 1) / (r[k] - l[k] + 1); } for (int k = (1); k <= (int)n; k++) if (wei(j, k - 1)) ep /= (r[k] - l[k] + 1); ans += ep * hi; } } for (int j = (1); j <= (int)n; j++) for (int mid = (1); mid <= (int)r[j] - 1; mid++) for (int k = (1 << n) - 1 - (1 << (j - 1)); k; k = (k - 1) & ((1 << n) - 1 - (1 << (j - 1)))) { double ep = 1; for (int u = (1); u <= (int)n; u++) if (wei(k, u - 1) && (r[u] < mid || mid < l[u])) ep = 0; for (int u = (1); u <= (int)n; u++) if (wei(k, u - 1)) ep /= (r[u] - l[u] + 1); ep *= 1. * min(r[j] - mid, r[j] - l[j] + 1) / (r[j] - l[j] + 1); for (int u = (1); u <= (int)n; u++) if (wei(k, u - 1) == 0 && u != j) { if (l[u] >= mid) ep = 0; else ep *= 1. * min(r[u] - l[u] + 1, mid - l[u]) / (r[u] - l[u] + 1); } ans += ep * mid; } printf("%.12lf\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[1005], b[1005]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } int kk, tt = 1005; for (int t = 1; t <= 1000; t++) { int s = t; int op = 0; for (int i = 1; i <= n; i++) { if (a[i] != s) { op++; } s += k; } if (op < tt) { kk = t; tt = op; } if (kk == 0) break; } cout << tt << endl; for (int i = 1; i <= n; i++) { if (a[i] - kk != 0) { if (kk > a[i]) cout << "+ " << i << ' ' << kk - a[i] << endl; else cout << "- " << i << ' ' << a[i] - kk << endl; } kk += k; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const constexpr int INF = 1e9; const constexpr long long MOD = 1e9 + 7; const int BITSIZE = 100010; struct Less { bool operator()(const pair<int, int>& x, const pair<int, int>& y) const { return x.first < y.first; } }; struct More { bool operator()(const pair<int, int>& x, const pair<int, int>& y) const { return x.first > y.first; } }; int bit[100010]; int bit_query(int i) { int res = 0; for (; i >= 1; i -= i & -i) res = max(res, bit[i]); return res; } void bit_set(int x, int v) { for (; x <= BITSIZE; x += x & -x) { bit[x] = max(bit[x], v); } } long long pow_mod(long long a, long long b) { long long sum = 1; while (b > 0) { if (b & 1) { sum *= a % MOD; } a = a * a % MOD; b >>= 1; } return sum; } int N, M; char a[51][51]; vector<pair<int, int> > vp; int main() { int s, n; cin >> s >> n; for (long long i = (long long)0; i < (long long)n; ++i) { int x, y; cin >> x >> y; vp.push_back({x, y}); } sort(vp.begin(), vp.end(), Less()); bool f = true; for (int i = 0; i < n; ++i) { if (s > vp[i].first) { s += vp[i].second; } else { f = false; break; } } if (f) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> int main(int argc, char** argv) { std::cin.tie(0); std::ios_base::sync_with_stdio(0); std::cout << std::fixed << std::setprecision(6); std::cerr << std::fixed << std::setprecision(6); int n, m[111111], o[111111]; std::cin >> n; for (int i = 0; i < n; i++) std::cin >> m[i]; o[0] = 0; for (int i = 1; i < n; i++) o[i] = std::max(m[i], o[i - 1]); for (int i = n - 2; i >= 0; i--) if (o[i] < o[i + 1] - 1) o[i] = o[i + 1] - 1; for (int i = 0; i < n; i++) o[i]++; long long c = 0; for (int i = 0; i < n; i++) c += o[i] - m[i] - 1; std::cout << c << std::endl; return 0; }
4
#include <queue> #include <vector> #include <iostream> using namespace std; typedef long long ll; struct edge { int to; ll cost; }; struct state { int pos; ll cost; }; bool operator<(const state& s1, const state& s2) { return s1.cost > s2.cost; } int dx[] = { 0, 1, 0, -1 }; int dy[] = { 1, 0, -1, 0 }; int H, W, A, B, C, N, x[100009], y[100009], xl[100009], xr[100009], yl[100009], yr[100009], ds[509][509]; vector<vector<edge> > g; void add_edge(int v1, int v2, ll c) { g[v1].push_back(edge{ v2, c }); } void add_edge2(int v1, int v2, ll c) { add_edge(v1, v2, c); add_edge(v2, v1, c); } ll shortest_path(int start, int goal) { vector<ll> dist(g.size(), 1LL << 60); dist[start] = 0; priority_queue<state> que; que.push(state{ start, 0 }); while (!que.empty()) { int u = que.top().pos; que.pop(); for (edge e : g[u]) { ll nd = dist[u] + e.cost; if (dist[e.to] > nd) { dist[e.to] = nd; que.push(state{ e.to, nd }); } } } return dist[goal]; } int main() { ios::sync_with_stdio(false); cin >> H >> W >> A >> B >> C >> N; H++; W++; g.resize(3 * H * W); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (j >= 1) { add_edge2(i * W + j, i * W + j - 1, C); add_edge2(H * W + i * W + j, H * W + i * W + j - 1, A); } if (i >= 1) { add_edge2(i * W + j, i * W + j - W, C); add_edge2(2 * H * W + i * W + j, 2 * H * W + i * W + j - W, A); } add_edge(i * W + j, H * W + i * W + j, B); add_edge(i * W + j, 2 * H * W + i * W + j, B); ds[i][j] = -1; } } fill(xl, xl + H, W + 1); fill(xr, xr + H, -1); fill(yl, yl + W, H + 1); fill(yr, yr + W, -1); queue<pair<int, int> > qs; for (int i = 0; i < N; i++) { cin >> x[i] >> y[i]; xl[x[i]] = min(xl[x[i]], y[i]); xr[x[i]] = max(xr[x[i]], y[i]); yl[y[i]] = min(yl[y[i]], x[i]); yr[y[i]] = max(yr[y[i]], x[i]); ds[x[i]][y[i]] = 0; add_edge(H * W + x[i] * W + y[i], x[i] * W + y[i], 0); add_edge(2 * H * W + x[i] * W + y[i], x[i] * W + y[i], 0); qs.push(make_pair(x[i], y[i])); } while (!qs.empty()) { pair<int, int> u = qs.front(); qs.pop(); for (int i = 0; i < 4; i++) { int px = u.first + dx[i], py = u.second + dy[i]; if (0 <= px && px < H && 0 <= py && py < W && ds[px][py] == -1) { ds[px][py] = ds[u.first][u.second] + 1; add_edge(H * W + px * W + py, px * W + py, 1LL * ds[px][py] * C); add_edge(2 * H * W + px * W + py, px * W + py, 1LL * ds[px][py] * C); qs.push(make_pair(px, py)); } } } cout << shortest_path(x[0] * W + y[0], x[N - 1] * W + y[N - 1]) << '\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n,t,mx,cnt,mn=(1ll<<30); int main(){ ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n>>t; for(ll i=0,a;i<n;i++){ cin>>a; mn=min(mn,a); if(a-mn>mx)mx=a-mn,cnt=1; else if(a-mn==mx)cnt++; } cout<<cnt; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 100500; vector<int> gen(int n) { int s = 1; vector<int> res; for (int i = 0; i < (int)(n); ++i) { res.push_back(s); res.push_back(s + 1); res.push_back(s + 2); res.push_back(s + 4); s += 6; } return res; } int main() { int n, k; cin >> n >> k; vector<int> res = gen(n); cout << *max_element((res).begin(), (res).end()) * k << "\n"; for (int i = 0; i < (int)(n * 4); ++i) { cout << res[i] * k << " \n"[i % 4 == 3]; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int INF = 1e9; int const N = 5e4 + 16; int const M = 1e9 + 7; int const M2 = 999999983; int n; int main() { scanf("%d", &n); bool found = false; for (int i = 1; i < 44722; ++i) { long long t1 = i * (i + 1) / 2; if (t1 > n) break; int l = i + 1, r = 44722; while (l < r) { int m = (l + r) / 2; int t2 = m * (m + 1) / 2; if (t1 + t2 <= n) l = m + 1; else r = m; } if (t1 + l * (l - 1) / 2 == n || n == t1 + t1) { found = true; break; } } puts(found ? "YES" : "NO"); }
1
#include <bits/stdc++.h> int n, a, d, i, j, top; double t, s[100010]; int v[100010], st[100010], r[100010]; int main() { scanf("%d%d%d", &n, &a, &d); for (i = 1; i <= n; ++i) { scanf("%d%d", &st[i], &v[i]); t = v[i] * 1.0 / a; if (v[i] / 2 * t > d) t = sqrt(2 * (double)a * d * 1.0) / a; else t += d * 1.0 / v[i] - t / 2; t += st[i]; if (t > s[top]) s[++top] = t; r[top] = i; } for (i = 1; i <= top; ++i) { for (j = r[i - 1] + 1; j <= r[i]; ++j) printf("%.10lf\n", s[i]); } }
1
#include <bits/stdc++.h> using namespace std; int main() { int m; cin >> m; int j = 0; vector<int> v(100005, -1); for (int i = 1;; i++) { if (j > m) break; if (i % 5 == 0) { int k = i; while (k % 5 == 0) { k = k / 5; j++; } v[j] = i; } } if (v[m] == -1) { cout << "0"; return 0; } cout << "5" << endl; int k = v[m]; for (int i = 0; i < 5; i++) { cout << k++ << " "; } }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; struct Point { long long x, y; Point() {} Point(long long _x, long long _y) : x(_x), y(_y) {} Point operator+(const Point &t) const { return Point(x + t.x, y + t.y); } Point operator-(const Point &t) const { return Point(x - t.x, y - t.y); } bool operator<(const Point &t) const { return x == t.x ? y < t.y : x < t.x; } bool operator==(const Point &t) const { return x == t.x && y == t.y; } } p[MAXN], q[MAXN], t[MAXN]; vector<Point> res; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%I64d%I64d", &p[i].x, &p[i].y); if (k >= n) return 0 * printf("-1\n"); sort(p, p + n); for (int i = 0; i < n; i++) q[i] = Point(-p[i].x, -p[i].y); sort(q, q + n); for (int i = 0; i <= k; i++) for (int j = 0; j <= k; j++) { Point v = p[i] - q[j]; int mis = 0; for (int _ = 0; _ < n; _++) t[_] = q[_] + v; for (int l = 0, r = 0; l < n; l++) { while (r < n && t[r] < p[l]) r++; mis += !(r < n && t[r] == p[l]); } if (mis <= k) res.push_back(v); } sort(res.begin(), res.end()); res.erase(unique(res.begin(), res.end()), res.end()); printf("%d\n", (int)res.size()); for (int i = 0; i < (int)res.size(); i++) printf("%.12f %.12f\n", res[i].x / 2.0, res[i].y / 2.0); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long int n, num, max = 0, diff, sum = 0; cin >> n; for (long long int i = 0; i < n; ++i) { cin >> num; sum += num; if (num > max) { max = num; } } diff = sum - max; diff = max - diff; cout << diff + 1 << endl; return 0; }
2
#include <bits/stdc++.h> const int MAX = 200005; long long n, t, a[MAX], x[MAX], b[MAX], i; int main() { scanf("%lld %lld", &n, &t); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) scanf("%lld", &x[i]); for (i = 1; i <= n; i++) { if (x[i] < x[i - 1] || x[i] < i) { if (n == 200000 && t == 1) printf("hehe "); printf("No"); return 0; } } b[n] = a[n] + t + 1000000; for (i = n - 1; i >= 1; i--) b[i] = a[i + 1] + t - (i == x[i]); for (i = 1; i < n; i++) { if ((x[i] < x[i + 1] && x[i] != i) || b[i] == b[i + 1]) { printf("No"); return 0; } } printf("Yes\n"); for (i = 1; i <= n; i++) printf("%lld ", b[i]); return 0; }
1
#include <iostream> using namespace std; void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } int partition(int *a, int p, int r){ int x, i; x = a[r]; i = p - 1; for(int j=p;j<r;++j){ if(a[j]<=x){ ++i; swap(&(a[i]),&(a[j])); } } swap(&(a[i+1]),&(a[r])); return i+1; } int main() { int n, *a, p; cin>>n; a = new int[n]; for(int i=0;i<n;++i){ cin>>a[i]; } p = partition(a,0,n-1); for(int i=0;i<n;++i){ if(i==p)cout<<(i?" ":"")<<"["<<a[i]<<"]"; else cout<<(i?" ":"")<<a[i]; } cout<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool SR(int &x) { return scanf("%d", &x) == 1; } bool SR(long long &x) { return scanf("%lld", &x) == 1; } bool SR(double &x) { return scanf("%lf", &x) == 1; } bool SR(char *s) { return scanf("%s", s) == 1; } bool RI() { return true; } template <typename I, typename... T> bool RI(I &x, T &...tail) { return SR(x) && RI(tail...); } void SP(const int x) { printf("%d", x); } void SP(const long long x) { printf("%lld", x); } void SP(const double x) { printf("%.16lf", x); } void SP(const char *s) { printf("%s", s); } void PL() { puts(""); } template <typename I, typename... T> void PL(const I x, const T... tail) { SP(x); if (sizeof...(tail)) putchar(' '); PL(tail...); } vector<pair<int, int> > get(int l, int r, int i) { r++; i++; int lc = l >> i, rc = r >> i; if (lc == rc) return vector<pair<int, int> >(1, make_pair(l % (1 << i), r % (1 << i))); else if (lc + 1 == rc) return vector<pair<int, int> >{make_pair(l % (1 << i), (1 << i)), make_pair(0, r % (1 << i))}; else return vector<pair<int, int> >(1, make_pair(0, (1 << i))); } int main() { int l1, r1, l2, r2; RI(l1, r1, l2, r2); int ans = 0; for (int i = 0; i < int(30); i++) { auto a = get(l1, r1, i), b = get(l2, r2, i); ; for (auto x : a) for (auto y : b) ans = max(ans, min(x.second, y.second) - max(1, max(x.first, y.first))); } PL(ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, a1[4], a2[4], a3[4], a4[4]; cin >> n; for (int i = 0; i < 4; i++) cin >> a1[i]; for (int i = 0; i < 4; i++) cin >> a2[i]; for (int i = 0; i < 4; i++) cin >> a3[i]; for (int i = 0; i < 4; i++) cin >> a4[i]; if (a1[0] + a1[2] <= n) cout << "1 " << a1[0] << " " << n - a1[0] << endl; else if (a1[0] + a1[3] <= n) cout << "1 " << a1[0] << " " << n - a1[0] << endl; else if (a1[1] + a1[2] <= n) cout << "1 " << a1[1] << " " << n - a1[1] << endl; else if (a1[1] + a1[3] <= n) cout << "1 " << a1[1] << " " << n - a1[1] << endl; else if (a2[0] + a2[2] <= n) cout << "2 " << a2[0] << " " << n - a2[0] << endl; else if (a2[0] + a2[3] <= n) cout << "2 " << a2[0] << " " << n - a2[0] << endl; else if (a2[1] + a2[2] <= n) cout << "2 " << a2[1] << " " << n - a2[1] << endl; else if (a2[1] + a2[3] <= n) cout << "2 " << a2[1] << " " << n - a2[1] << endl; else if (a3[0] + a3[2] <= n) cout << "3 " << a3[0] << " " << n - a3[0] << endl; else if (a3[0] + a3[3] <= n) cout << "3 " << a3[0] << " " << n - a3[0] << endl; else if (a3[1] + a3[2] <= n) cout << "3 " << a3[1] << " " << n - a3[1] << endl; else if (a3[1] + a3[3] <= n) cout << "2 " << a3[1] << " " << n - a3[1] << endl; else if (a4[0] + a4[2] <= n) cout << "4 " << a4[0] << " " << n - a4[0] << endl; else if (a4[0] + a4[3] <= n) cout << "4 " << a4[0] << " " << n - a4[0] << endl; else if (a4[1] + a4[2] <= n) cout << "4 " << a4[1] << " " << n - a4[1] << endl; else if (a4[1] + a4[3] <= n) cout << "4 " << a4[1] << " " << n - a4[1] << endl; else cout << "-1\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[100001]; int main() { int n, k = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int m = n + 1, mm = -1; for (int i = n - 1; 0 <= i; i--) { if ((a[i] > m) || (a[i] < mm)) { k++; if (a[i] > mm) mm = a[i]; } if (a[i] < m) m = a[i]; } cout << k; return 0; }
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:128000000") using namespace std; void solve(); void precalc(); clock_t start; bool doing = true; int main() { int t = 1; cout.sync_with_stdio(0); cin.tie(0); precalc(); cout.precision(10); cout << fixed; start = clock(); int testNum = 1; while (t--) { solve(); } return 0; } void precalc() {} int binpow(int q, int w, int mod) { if (!w) return 1; if (w & 1) return q * binpow(q, w - 1, mod) % mod; return binpow(q * q % mod, w / 2, mod); } int gcd(int q, int w) { while (w) { q %= w; swap(q, w); } return q; } const long long INF = 100100000000; struct treap { typedef struct _node { long long key; long long prior; long long cnt; _node* l; _node* r; long long val; long long mx; _node(long long key, long long val) : key(key), val(val), mx(mx) { prior = (rand() << 16) | rand(); l = 0; r = 0; cnt = 1; } void recalc() { mx = val; if (l) { mx = max(mx, l->mx); } if (r) { mx = max(mx, r->mx); } cnt = 1 + Cnt(l) + Cnt(r); } } * node; static long long Cnt(node v) { if (!v) return 0; return v->cnt; } node root; size_t Size; node merge(node l, node r) { if (!l) return r; if (!r) return l; if (l->prior < r->prior) { l->r = merge(l->r, r); l->recalc(); return l; } else { r->l = merge(l, r->l); r->recalc(); return r; } } void split(node v, long long key, node& l, node& r) { l = r = 0; if (!v) return; if (v->key < key) { l = v; split(l->r, key, l->r, r); l->recalc(); } else { r = v; split(r->l, key, l, r->l); r->recalc(); } } treap() { root = 0; } void insert(long long key, long long val) { node l, r; split(root, key, l, r); root = merge(l, merge(new _node(key, val), r)); } long long getVal(long long key) { node l, r; split(root, key, l, r); long long res = Cnt(r); root = merge(l, r); return res; } long long getMax(long long L) { node l, r; split(root, L, l, r); long long res = r ? r->mx : -INF; root = merge(l, r); return res; } }; void solve() { long long n; cin >> n; n += 2; vector<long long> x(n), d(n); for (long long i = 0; i < n; ++i) { cin >> x[i]; } for (long long i = 1; i + 1 < n; ++i) { cin >> d[i]; } d[0] = d[n - 1] = INF; long long len = x[n - 1]; vector<long long> borders[2]; for (long long w = 0; w < 2; ++w) { treap dps; dps.insert(0, INF); vector<long long> rightBorder(n); rightBorder[0] = INF; for (long long i = 1; i < n; ++i) { long long l = 0; long long r = i; while (l + 1 < r) { long long m = (l + r) / 2; if (dps.getMax(x[m]) >= x[i]) { l = m; } else { r = m; } } rightBorder[i] = 2 * d[i] - (x[i] - x[l]) + x[i]; if (rightBorder[i] <= x[i]) { rightBorder[i] = -INF; } else { dps.insert(x[i], rightBorder[i]); } } reverse(d.begin(), d.end()); reverse(x.begin(), x.end()); for (long long i = 0; i < n; ++i) { x[i] = len - x[i]; } if (w) { reverse(rightBorder.begin(), rightBorder.end()); for (long long i = 0; i < n; ++i) { rightBorder[i] = len - rightBorder[i]; } } borders[w] = rightBorder; } long long ans = INF; treap dp; for (long long i = 0; i < n; ++i) { if (i > 0 && borders[1][i] <= x[i]) { long long l = 0; long long r = i; while (l + 1 < r) { long long m = (l + r) / 2; if (dp.getMax(x[m]) >= borders[1][i]) { l = m; } else { r = m; } } if (x[l] >= borders[1][i] && x[i] <= borders[0][l] && (i != n - 1 || l != 0)) { ans = 0; } else { if (borders[1][i] <= borders[0][l]) { ans = min(ans, x[i] - x[l]); } } } if (borders[0][i] >= x[i]) { dp.insert(x[i], borders[0][i]); } } double res = ans / 2.0; cout << res << "\n"; }
4
#include <bits/stdc++.h> using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; const int N = 1e5 + 4; int n, p; ll ans[N]; pair<int, int> a[N]; bool done[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> p; for (int i = (1); i <= (n); ++i) cin >> a[i].first, a[i].second = i; sort(a + 1, a + 1 + n); queue<int> q; priority_queue<int, vector<int>, greater<int> > pq, pq1; int ptr = 1; ll cur = -1; while (ptr <= n) { cur = a[ptr].first; while (ptr <= n && a[ptr].first <= cur) pq.push(a[ptr].second), ptr++; int mn = pq.top(); q.push(mn); pq1.push(mn); pq.pop(); while (!q.empty()) { int u = q.front(); q.pop(); cur += p; ans[u] = cur; done[u] = 1; int lst = -1, mn1 = N; vector<int> tem; while (ptr <= n && a[ptr].first <= cur) { if (lst == -1) lst = a[ptr].first, mn1 = a[ptr].second, tem = vector<int>(1, a[ptr].second); else { if (lst != a[ptr].first) { if (mn1 < mn) q.push(mn1), pq1.push(mn1), mn = mn1; for (int v : tem) if (v != mn1 || mn1 > mn) pq.push(v); tem = vector<int>(1, a[ptr].second); lst = a[ptr].first; mn1 = a[ptr].second; } else { tem.emplace_back(a[ptr].second); mn1 = min(mn1, a[ptr].second); } } ptr++; } if (mn1 < mn) q.push(mn1), pq1.push(mn1), mn = mn1; for (int v : tem) if (v != mn1 || mn1 > mn) pq.push(v); if (u == mn) { while (!pq1.empty() && done[pq1.top()]) pq1.pop(); mn = (pq1.empty() ? N : pq1.top()); } if (!pq.empty() && pq.top() < mn) { int tem = pq.top(); pq.pop(); q.push(tem); pq1.push(tem); mn = tem; } } } for (int i = (1); i <= (n); ++i) cout << ans[i] << ' '; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, s; pair<int, int> d[200005]; long long int fon(int k) { long long int ret = 0; int say = 0; vector<int> v, v2; for (int i = 0; i < n; i++) if (d[i].first <= k - 2 || d[i].first > k) { v.push_back(d[i].second); if (d[i].first <= k - 2) say++; } else v2.push_back(d[i].second); sort(v2.begin(), v2.end()); int qw = (n - s + 1) - say; if (qw < 0) qw = 0; if (qw > k) return -1; for (int i = 0; i < qw; i++) { if (i == v2.size()) return -1; ret += v2[i]; } for (int i = qw; i < v2.size(); i++) v.push_back(v2[i]); sort(v.begin(), v.end()); for (int i = 0; i < k - qw; i++) if (i == v.size()) return -1; else ret += v[i]; return ret; } int main() { scanf("%d %d", &n, &s); for (int i = 0; i < n; i++) { scanf("%d %d", &d[i].first, &d[i].second); } sort(d, d + n); int a = 0, b = n; while (a != b) { if (fon(((a + b) / 2)) == -1) a = ((a + b) / 2) + 1; else b = ((a + b) / 2); } long long int mn = fon(a); if (mn == -1) return printf("-1\n"), 0; for (int i = a; i <= n && i < a + 100; i++) mn = min(fon(i), mn); printf("%lld\n", mn); }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, ans = 0; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { if (a[i] <= 0) break; if (i == 0) { if (a[i] > 0) ans += a[i]; break; } ans += a[i]; if (a[i] <= a[i - 1]) a[i - 1] = a[i] - 1; } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; const int mo = 1e9 + 7; const int iv2 = 5e8 + 4; const int jzm = 2333; const int orG = 3, invG = 332748118; const double Pi = acos(-1.0); const double PI = acos(-1.0); template <typename _T> _T Fabs(_T x) { return x < 0 ? -x : x; } template <typename _T> void read(_T &x) { _T f = 1; x = 0; char s = getchar(); while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while ('0' <= s && s <= '9') { x = (x << 3) + (x << 1) + (s ^ 48); s = getchar(); } x *= f; } template <typename _T> void print(_T x) { if (x < 0) { x = (~x) + 1; putchar('-'); } if (x > 9) print(x / 10); putchar(x % 10 + '0'); } int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int add(int x, int y) { return x + y < mo ? x + y : x + y - mo; } int K, d, n, a[200005], nxt[200005], lim[200005], ans, ansk; int stamx[200005], stkmx, stamn[200005], stkmn; map<int, int> head; class SegmentTree { public: int sum[200005 << 2], lzy[200005 << 2]; bool clr[200005 << 2]; void pushup(int rt) { sum[rt] = max(sum[(rt << 1)], sum[(rt << 1 | 1)]); } void pushdown(int rt) { if (lzy[rt]) { lzy[(rt << 1)] += lzy[rt]; sum[(rt << 1)] += lzy[rt]; lzy[(rt << 1 | 1)] += lzy[rt]; sum[(rt << 1 | 1)] += lzy[rt]; lzy[rt] = 0; } if (clr[rt]) { sum[(rt << 1)] = sum[(rt << 1 | 1)] = -INF; lzy[(rt << 1)] = lzy[(rt << 1 | 1)] = 0; clr[(rt << 1)] = clr[(rt << 1 | 1)] = 1; clr[rt] = 1; } } void build(int rt, int l, int r) { sum[rt] = -INF; lzy[rt] = 0; if (l == r) return; int mid = l + r >> 1; build((rt << 1), l, mid); build((rt << 1 | 1), mid + 1, r); } void insert(int rt, int l, int r, int ai) { if (l > r || l > ai || r < ai) return; int mid = l + r >> 1; if (l == r) { sum[rt] = ai; lzy[rt] = 0; return; } pushdown(rt); if (ai <= mid) insert((rt << 1), l, mid, ai); else insert((rt << 1 | 1), mid + 1, r, ai); pushup(rt); } void modify(int rt, int l, int r, int al, int ar, int aw) { if (al > ar || l > r || al > r || ar < l) return; if (al <= l && r <= ar) { sum[rt] += aw; lzy[rt] += aw; return; } int mid = l + r >> 1; pushdown(rt); if (al <= mid) modify((rt << 1), l, mid, al, ar, aw); if (ar > mid) modify((rt << 1 | 1), mid + 1, r, al, ar, aw); pushup(rt); } void clear(int rt, int l, int r, int al, int ar) { if (al > ar || l > r || l > ar || r < al) return; if (al <= l && r <= ar) { sum[rt] = -INF; lzy[rt] = 0; clr[rt] = 1; return; } int mid = l + r >> 1; pushdown(rt); if (al <= mid) clear((rt << 1), l, mid, al, ar); if (ar > mid) clear((rt << 1 | 1), mid + 1, r, al, ar); pushup(rt); } int query(int rt, int l, int r, int k) { if (l > r || sum[rt] < k) return 0; if (l == r) return l; int mid = l + r >> 1; pushdown(rt); if (sum[(rt << 1 | 1)] >= k) return query((rt << 1 | 1), mid + 1, r, k); return query((rt << 1), l, mid, k); } } T; signed main() { read(n); read(K); read(d); for (int i = 1; i <= n; i++) read(a[i]); T.build(1, 1, n); if (!d) { int tmp = 0; a[0] = -INF; for (int i = 1; i <= n; i++) { if (a[i] == a[i - 1]) tmp++; else tmp = 1; if (tmp > ans) ansk = i - tmp + 1, ans = tmp; } printf("%d %d\n", ansk, ansk + ans - 1); return 0; } for (int i = 1, j; i <= n; i = j + 1) { j = i; while (j < n && Fabs(a[j + 1] - a[j]) % d == 0) j++; for (int k = i; k <= j; k++) a[k] = (int)floor(1.0 * a[k] / d); head.clear(); int tmp = j + 1; stkmx = stkmn = 0; for (int k = j; k >= i; k--) { if (!head[a[k]]) nxt[k] = j + 1; else nxt[k] = head[a[k]]; tmp = min(nxt[k], tmp); head[a[k]] = k; lim[k] = tmp - 1; } for (int k = j, r; k >= i; k--) { while (stkmx && a[stamx[stkmx]] < a[k]) { r = stkmx > 1 ? stamx[stkmx - 1] - 1 : j; T.modify(1, 1, n, stamx[stkmx], r, a[stamx[stkmx]]); stkmx--; } r = stkmx > 0 ? stamx[stkmx] - 1 : j; T.modify(1, 1, n, k + 1, r, -a[k]); stamx[++stkmx] = k; while (stkmn && a[stamn[stkmn]] > a[k]) { r = stkmn > 1 ? stamn[stkmn - 1] - 1 : j; T.modify(1, 1, n, stamn[stkmn], r, -a[stamn[stkmn]]); stkmn--; } r = stkmn > 0 ? stamn[stkmn] - 1 : j; T.modify(1, 1, n, k + 1, r, a[k]); stamn[++stkmn] = k; T.insert(1, 1, n, k); T.clear(1, 1, n, lim[k] + 1, j); int tmp = T.query(1, 1, n, k - K) - k + 1; if (ans < tmp || (ans == tmp && ansk > k)) ans = tmp, ansk = k; } T.clear(1, 1, n, i, j); } printf("%d %d\n", ansk, ansk + ans - 1); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { long long n; cin >> n; long long tong = n * (n + 1) / 2; long long tongam = 0; int k = 1; while (n >= pow(2, k)) { tongam = tongam + pow(2, k); k++; } cout << tong - 2 * tongam - 2 << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, array[10]; cin >> n >> m; int ansc = m; int ans = 0; for (i = 0; i < n; i++) { array[i] = i + 1; ans += (i + 1) * (n - i); } do { int sum = 0, minn = 2e9; for (i = 0; i < n; i++) { minn = 2e9; for (j = i; j < n; j++) { minn = min(minn, array[j]); sum += minn; } } if (sum == ans) m--; if (m != 0) next_permutation(array, array + n); } while (m != 0); for (i = 0; i < n; i++) cout << array[i] << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, k; int a[100005]; int b[100005]; int main() { while (~scanf("%d%d", &n, &k)) { for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) scanf("%d", b + i); sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); int rk = n; for (int i = 1; i <= n; i++) if (a[i] + b[rk] >= k) rk--; int ans = n - rk; rk = n; for (int i = 1; i <= n; i++) if (b[i] + a[rk] >= k) rk--; printf("1 %d\n", max(ans, n - rk)); } return 0; }
4
#include<iostream> #include<algorithm> using namespace std; int main(){ int t,n; string str; bool book[2][50000]; int v[3][20000],a[3][20000]; cin >> t; while(t--){ cin >> n >> str; for(int i=0;i<2;i++){ for(int j=0;j<2*n;j++)book[i][j+1] = (str[i*2*n+j]=='Y'); book[i][0] = book[i][2*n+1] = false; } for(int i=0;i<3;i++) for(int j=0;j<=n;j++){ v[i][j] = a[i][j] = 100000000; } a[0][0] = 0; for(int i=0;i<=n;i++){ if(book[1][2*i] || book[1][2*i+1]){ v[0][i] = min(min(v[0][i],a[0][i] + 3),min(a[1][i] + 2,a[2][i] + 2)); if(book[0][2*i] || book[0][2*i+1]){ v[1][i] = min(min(v[1][i],a[0][i] + 2),min(a[1][i] + 2,a[2][i] + 2)); v[2][i] = min(min(v[2][i],a[0][i] + 2),min(a[1][i] + 2,a[2][i] + 3)); }else{ v[1][i] = min(min(v[1][i],a[0][i] + 2),min(a[1][i] + 1,a[2][i] + 1)); v[2][i] = min(min(v[2][i],a[0][i] + 2),min(a[1][i] + 1,a[2][i] + 1)); } }else if(book[0][2*i] || book[0][2*i+1]){ v[0][i] = min(min(v[0][i],a[0][i] + 1),min(a[1][i] + 1,a[2][i] + 2)); v[1][i] = min(min(v[1][i],a[0][i] + 1),min(a[1][i] + 1,a[2][i] + 2)); v[2][i] = min(min(v[2][i],a[0][i] + 2),min(a[1][i] + 2,a[2][i] + 3)); }else{ v[0][i] = min(min(v[0][i],a[0][i]),min(a[1][i] + 1,a[2][i] + 2)); v[1][i] = min(min(v[1][i],a[0][i] + 1),min(a[1][i],a[2][i] + 1)); v[2][i] = min(min(v[2][i],a[0][i] + 2),min(a[1][i] + 1,a[2][i])); } if(i==n)break; for(int j=0;j<3;j++)a[j][i+1] = v[j][i] + 1; } cout << v[0][n] << endl; } }
0
#include <bits/stdc++.h> using namespace std; const long long MXN = 3e5 + 1; const long long MAXN = 1e3 + 1; const long long MOD = 1e9 + 7; const long long INF = 1e18; inline void f_math(bool ok = 0) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (ok) { freopen("aquarium.in", "r", stdin); freopen("aquarium.out", "w", stdout); ; } } long long n, sum[MXN], sz[MXN], mx[MXN], num[MXN], ans[MXN], c[MXN]; vector<int> g[MXN]; map<int, int> cnt[MXN]; void go(int v, int c, int s) { cnt[v][c] += s; if (cnt[v][c] > mx[v]) { sum[v] = c; mx[v] = cnt[v][c]; } else if (cnt[v][c] == mx[v]) { sum[v] += c; } } void dfs(int v, int pr = -1) { int res = -1; sz[v] = 1; for (auto to : g[v]) { if (to == pr) continue; dfs(to, v); sz[v] += sz[to]; if (res == -1 || sz[to] > sz[res]) res = to; } if (res != -1) { int w = res; res = num[res]; num[v] = res; go(res, c[v], 1); for (auto to : g[v]) { if (to != pr && to != w) { for (auto c : cnt[num[to]]) { go(res, c.first, c.second); } } } } else { num[v] = v; go(v, c[v], 1); } ans[v] = sum[num[v]]; } int main() { f_math(); cin >> n; for (int i = 1; i <= n; i++) { cin >> c[i]; } for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); for (int i = 1; i <= n; i++) { cout << ans[i] << ' '; } return 0; }
5
#include <iostream> using namespace std; int main(){ int s; cin>>s; cout<<s/3600<<":"<<(s%3600)/60<<":"<<s%60<< endl; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 20; pair<int, int> a[MAXN], b[MAXN]; inline int match(pair<int, int> x, pair<int, int> y) { if (x.first == y.first && x.second != y.second || x.first == y.second && x.second != y.first) return x.first; if (x.second == y.first && x.first != y.second || x.second == y.second && x.first != y.first) return x.second; return 0; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second); for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].first, &b[i].second); int ans = -1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int t = match(a[i], b[j]); if (t) { if (ans == -1) ans = t; else if (t != ans) ans = 0; } } if (ans > 0) printf("%d\n", ans); else { for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (!match(a[i], b[j])) continue; int f1 = -1, f2 = -1; for (int k = 1; k <= m; k++) { int t = match(a[i], b[k]); if (t) { if (f1 == -1) f1 = t; else if (t != f1) f1 = 0; } } for (int k = 1; k <= n; k++) { int t = match(a[k], b[j]); if (t) { if (f2 == -1) f2 = t; else if (t != f2) f2 = 0; } } if (f1 <= 0 || f2 <= 0) { ans = -1; break; } } printf("%d\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; int arr[2000]; int arr1[2000]; int count = 0; int n1 = 0; cin >> n; for (int i = 0; i < 2000; i++) { arr[i] = 0; arr1[i] = 0; } for (int i = 0; i < n; i++) { cin >> arr[i]; cout << " "; } while (n != n1) { for (int i = 0; i < n; i++) { if (arr[i + 1] != '/0') { count = 0; for (int j = 0; j < n; j++) { if (arr[i] < arr[j]) { count++; } } } arr1[i] = count + 1; n1++; } } for (int i = 0; i < n1; i++) { cout << arr1[i]; cout << " "; } }
1
#include <bits/stdc++.h> using namespace std; long long ar[10000000]; int main() { long long n; while (cin >> n) { long long i, j, a, b, c, p = 0; for (i = 1; i <= n; i++) cin >> ar[i]; sort(ar + 1, ar + n + 1); for (j = 1; j <= n - 2; j++) { a = ar[j], b = ar[j + 1], c = ar[j + 2]; if (a + b > c && b + c > a && a + c > b) { p++; break; } } if (p > 0) printf("YES\n"); else printf("NO\n"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; bool chon[101]; vector<int> card[101]; vector<int> b; int a[101]; double val[101]; double lon, nho; int n, k, q; bool diff(vector<int> a, vector<int> b) { for (int i = 0; i < a.size(); i++) if (a[i] != b[i]) return true; return false; } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> q; for (int i = 0; i < q; i++) { for (int j = 0; j < n / k; j++) { int tam; cin >> tam; chon[tam] = true; card[i].push_back(tam); } sort(card[i].begin(), card[i].end()); } if (q == 0) { for (int i = 1; i <= n; i++) b.push_back(a[i]); sort(b.begin(), b.end()); for (int i = 0; i < n / k; i++) { nho += b[i]; lon += b[int(b.size()) - 1 - i]; } nho /= (n / k); lon /= (n / k); printf("%0.9lf %0.9lf", nho, lon); return 0; } for (int i = 0; i < card[0].size(); i++) val[0] += a[card[0][i]]; val[0] /= card[0].size(); lon = val[0]; nho = val[0]; for (int i = 1; i < q; i++) if (diff(card[i], card[i - 1])) { for (int j = 0; j < card[i].size(); j++) val[i] += a[card[i][j]]; val[i] /= card[i].size(); nho = min(nho, val[i]); lon = max(lon, val[i]); } for (int i = 1; i <= n; i++) if (chon[i] == false) b.push_back(a[i]); sort(b.begin(), b.end()); if (b.size() > n - k * (n / k)) { double canNho = 0; double canLon = 0; for (int i = 0; i < n / k; i++) { canNho += b[i]; canLon += b[int(b.size()) - 1 - i]; } canNho /= (n / k); canLon /= (n / k); nho = min(nho, canNho); lon = max(lon, canLon); } printf("%0.9lf %0.9lf", nho, lon); }
2
#include <bits/stdc++.h> using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; const int N = 15; int K, nxt[N * 5001], nmp, idBox[N * 5001], par[N * 5001], val[N * 5001], track[1 << N]; ll tot[N], atot; vector<int> ele[N]; map<int, int> mp; bool vis[N * 5001], in[N * 5001], dp[1 << N]; struct Move { int c, to, from; }; int curMask = 0; vector<Move> cur, ofDp[1 << N]; void dfs(int u) { in[u] = 1; vis[u] = 1; if (nxt[u]) { int v = nxt[u]; if (in[v]) { int x = u, lst = v; curMask |= (1 << idBox[x]); cur.push_back({val[lst], idBox[x], idBox[lst]}); while (x != v) { lst = x; x = par[x]; if (curMask >> (idBox[x]) & 1) { curMask = 0; break; } curMask |= (1 << idBox[x]); cur.push_back({val[lst], idBox[x], idBox[lst]}); } } else if (!vis[v]) { par[v] = u; dfs(v); } } in[u] = 0; } pair<int, int> ans[N]; void solve(int m) { if (!ofDp[m].empty()) { for (Move M : ofDp[m]) ans[M.from] = {M.c, M.to}; return; } if (m == 0) return; solve(track[m]); solve(m ^ track[m]); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> K; for (int i = (0); i <= (K - 1); ++i) { int len; cin >> len; ele[i].assign(len, 0); for (int j = (0); j <= (len - 1); ++j) { int x; cin >> x; ele[i][j] = x; tot[i] += x; mp[x] = ++nmp; idBox[nmp] = i; val[nmp] = x; } atot += tot[i]; } if (atot % K != 0) { cout << "No"; return 0; } atot /= K; for (int i = (0); i <= (K - 1); ++i) { for (int v : ele[i]) { int id = mp[v]; ll to = atot - (tot[i] - v); if (abs(to) <= 1e9 && mp.find(to) != mp.end()) nxt[id] = mp[to]; } } for (int i = (0); i <= (K - 1); ++i) { for (int v : ele[i]) { int u = mp[v]; if (!vis[u]) { curMask = 0; cur.clear(); dfs(u); if (curMask && !dp[curMask]) { dp[curMask] = 1; ofDp[curMask] = cur; } } } } dp[0] = 1; for (int x = (1); x <= ((1 << K) - 1); ++x) if (!dp[x]) for (int subx = x; subx; subx = (subx - 1) & x) if (dp[subx] && dp[x ^ subx]) { dp[x] = 1; track[x] = subx; break; } if (!dp[(1 << K) - 1]) cout << "No"; else { cout << "Yes\n"; solve((1 << K) - 1); for (int i = (0); i <= (K - 1); ++i) cout << ans[i].first << ' ' << ans[i].second + 1 << '\n'; } return 0; }
5
#include<stdio.h> int main (void) { char inp[1024]; scanf("AtCoder %s Contest", &inp); printf("A%cC\n", inp[0]); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; struct segtree { struct node { long long int min, max, sum; }; vector<node> st; long long int ans = 0; int n; template <typename M> segtree(vector<M> &a) { n = (int)a.size(); st.resize(4 * n); } void combine(node &a, node &b, node &c) { a.sum = b.sum + c.sum; a.min = min(b.min, c.min); a.max = max(b.max, c.max); } template <typename M> void build(int v, int l, int r, vector<M> &vec) { if (l == r) { st[v].max = vec[l]; st[v].min = vec[l]; st[v].sum = vec[l]; } else { int m = (l + r) / 2; build(2 * v, l, m, vec); build(2 * v + 1, m + 1, r, vec); combine(st[v], st[2 * v], st[2 * v + 1]); } } node query(int v, int l, int r, int l_, int r_) { if (l == l_ && r == r_) { return st[v]; } int m = (l + r) / 2; if (r_ <= m) { return query(2 * v, l, m, l_, r_); } else if (l_ > m) { return query(2 * v + 1, m + 1, r, l_, r_); } else { node a, b, c; b = query(2 * v, l, m, l_, m); c = query(2 * v + 1, m + 1, r, m + 1, r_); combine(a, b, c); return a; } } template <typename M> void update(int v, int l, int r, int id, M val) { if (l == r) { st[v].min += val; st[v].max += val; st[v].sum += val; return; } int m = (l + r) / 2; if (id <= m) { update(2 * v, l, m, id, val); } else { update(2 * v + 1, m + 1, r, id, val); } combine(st[v], st[2 * v], st[2 * v + 1]); } long long int query(int l, int r) { return query(1, 0, n - 1, l, r).sum; } template <typename M> void update(int id, M val) { update(1, 0, n - 1, id, val); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t, n, m, k, q, a, b, c, d, x, y; cin >> n >> k >> a >> b >> q; x = a; y = b; vector<long long> ff(n + 2), rr(n + 2); for (int i = 1; i <= n; i++) { ff[i] = b; rr[i] = a; } segtree aa(ff); segtree bb(ff); for (int i = 0; i < q; i++) { cin >> a; if (a == 1) { cin >> a >> b; if (ff[a] >= b) { ff[a] -= b; aa.update((int)a, b); } else { aa.update((int)a, ff[a]); ff[a] = 0; } if (rr[a] >= b) { rr[a] -= b; bb.update((int)a, b); } else { bb.update((int)a, rr[a]); rr[a] = 0; } } else { cin >> a; cout << aa.query(0, (int)a - 1) + bb.query((int)a + (int)k, n + 1) << '\n'; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, i, a[100010], j, k; long long f[1010][1010], g[1010][1010]; int main() { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); if (n == 1) { printf("1"); return 0; } n--; for (i = 1; i <= n; i++) { g[i][i] = 1; f[i][i] = 1; } for (i = 2; i <= n; i++) for (j = 1; j <= n - i + 1; j++) { f[j][j + i - 1] = g[j + 1][j + i - 1]; g[j][j + i - 1] = f[j][j + i - 1]; for (k = j; k < j + i - 1; k++) if (a[j] < a[k + 1]) g[j][j + i - 1] = (g[j][j + i - 1] + f[j][k] * g[k + 1][j + i - 1]) % 1000000007; } printf("%I64d", g[1][n]); }
6
#include <bits/stdc++.h> using namespace std; void true_main() { int n, r, i, j; cin >> n >> r; vector<double> x(n), y(n); for (i = 0; i < n; i++) { cin >> x[i]; y[i] = r; for (j = 0; j < i; j++) { int t = x[i] - x[j]; y[i] = max(y[i], y[j] + sqrt(4 * r * r - t * t)); } cout << fixed << setprecision(7) << y[i] << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { true_main(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; using pi = pair<int, pair<int, int> >; const int N = 2e5 + 5; pi p[N]; int a[N]; deque<int> dq; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d", a + i); } for (int i = 0; i < m; ++i) { int t, r; scanf("%d %d", &t, &r); p[i] = {r, {i, t}}; } sort(p, p + m, greater<pi>()); sort(a, a + p[0].first); for (int i = 0; i < p[0].first; ++i) { dq.emplace_back(a[i]); } int prev = 0; for (int i = 1; i < m; ++i) { if (p[i].second.first > p[prev].second.first) { int num = p[prev].first - p[i].first; int idx = p[prev].first - 1; if (p[prev].second.second == 1) { while (num--) { a[idx--] = dq.back(); dq.pop_back(); } } else { while (num--) { a[idx--] = dq.front(); dq.pop_front(); } } prev = i; } } int idx = p[prev].first - 1; if (p[prev].second.second == 1) { while (!dq.empty()) { a[idx--] = dq.back(); dq.pop_back(); } } else { while (!dq.empty()) { a[idx--] = dq.front(); dq.pop_front(); } } for (int i = 0; i < n; ++i) { printf("%d ", a[i]); } return 0; }
3
// 基本テンプレート #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> using namespace std; #define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++) #define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++) #define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--) #define debug(...) fprintf(stderr, __VA_ARGS__) #define int long long int template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const ll INF = 1001001001001001LL; const ll MOD = 1000000007LL; int N, R; const int SIZE = 1 << 17; struct Segtree { int node[2 * SIZE]; Segtree () { memset(node, 0, sizeof(node)); } void reset() { memset(node, 0, sizeof(node)); } void add(int x, int val) { x += SIZE - 1; node[x] += val; while(x > 0) { x = (x - 1) / 2; node[x] = (node[2*x+1] + node[2*x+2]); } } int query(int a, int b, int l=0, int r=SIZE, int k=0) { if(b <= l || r <= a) return 0; if(a <= l && r <= b) return node[k]; int mid = (l + r) / 2; int vl = query(a, b, l, mid, 2*k+1); int vr = query(a, b, mid, r, 2*k+2); return vl + vr; } }; int fact[100010]; Segtree treeone; signed main() { fact[0] = 1; for(int i=1; i<=100005; i++) { fact[i] = (fact[i-1] * i) % MOD; } while(cin >> N, N) { cin >> R; vector<int> perm(N); iota(perm.begin(), perm.end(), 0); for(int i=0; i<R; i++) { int s, t; cin >> s >> t; s--; t--; swap(perm[s], perm[t]); } treeone.reset(); int ans = 0; for(int i=0; i<N; i++) { int mul = fact[N - i - 1]; int val = perm[i] - treeone.query(0, perm[i]); (ans += mul * val) %= MOD; treeone.add(perm[i], 1); } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int p; cin >> p; while (p--) { string s, t; cin >> s >> t; int n = s.length(); int m = t.length(); int i = 0, j = 0, f = 0; char c; while (i < n && j < m) { if (n > m) { f = 1; break; } while (s[i] == t[j] && s[i] == s[i + 1]) { i++; j++; } if (s[i] != t[j]) { f = 1; break; } while (s[i] == t[j]) j++; i++; } if (i == n && j == m) cout << "YES"; else cout << "NO"; cout << "\n"; } return 0; }
2
#include <bits/stdc++.h> namespace util { template <typename T, typename DataT> struct Event { T time; char type; DataT data; Event(char type_, T time_, DataT data_) : time(time_), type(type_), data(data_) {} bool operator<(const Event<T, DataT> &other) const { return time < other.time; } bool operator>(const Event<T, DataT> &other) const { return time > other.time; } }; template <typename T, typename DataT> struct EventQueue { std::priority_queue<Event<T, DataT>, std::vector<Event<T, DataT>>, std::greater<Event<T, DataT>>> q; std::function<void(DataT)> handlers[256]; EventQueue() {} void add(Event<T, DataT> e) { q.push(e); } void handle(char c, std::function<void(DataT)> handler) { handlers[c] = handler; } void advance_until(T time) { while (!q.empty()) { const Event<T, DataT> &e = q.top(); if (e.time > time) break; handlers[e.type](e.data); q.pop(); } } }; long long search_min(long long lower, long long upper, std::function<bool(long long)> is_valid) { long long orig_upper = upper; while (lower < upper) { int mid = (lower + upper) / 2; if (is_valid(mid)) upper = mid; else lower = mid + 1; } if (lower == orig_upper && !is_valid(lower)) return orig_upper + 1; return lower; } } // namespace util using namespace std; const int K_MAX = 505; long long N, M, K; long long X[K_MAX]; long long Y[K_MAX]; vector<long long> get_dividers(long long pos[], long long max_val, long long t) { set<long long> temp; temp.insert(0); temp.insert(max_val); for (int i = 0; i < K; i++) { temp.insert(pos[i] - t - 1); temp.insert(pos[i] + t); } vector<long long> ret; for (long long x : temp) { if (x >= 0 && x <= max_val) ret.push_back(x); } return ret; } bool can_engulf(long long t) { vector<long long> x_dividers = get_dividers(X, N, t); vector<long long> y_dividers = get_dividers(Y, M, t); int n = x_dividers.size() - 1; int m = y_dividers.size() - 1; vector<int> count(m, 0); util::EventQueue<long long, pair<long long, int>> eqx; for (int i = 0; i < K; i++) { eqx.add(util::Event<long long, pair<long long, int>>( '.', max(X[i] - t - 1, 0LL), pair<long long, int>(Y[i], 1))); ; eqx.add(util::Event<long long, pair<long long, int>>( '.', min(X[i] + t, N), pair<long long, int>(Y[i], -1))); } eqx.handle('.', [&](pair<long long, int> data) { long long y = data.first; int amt = data.second; for (int i = 0; i < m; i++) { if (y_dividers[i] >= y - t - 1 && y_dividers[i] < y + t) { count[i] += amt; } } }); long long x_min = N, x_max = 0; for (int i = 0; i < n; i++) { eqx.advance_until(x_dividers[i]); for (int j = 0; j < m; j++) { if (count[j] == 0) { x_min = min(x_min, x_dividers[i]); x_max = max(x_max, x_dividers[i + 1]); break; } } } count.assign(n, 0); util::EventQueue<long long, pair<long long, int>> eqy; for (int i = 0; i < K; i++) { eqy.add(util::Event<long long, pair<long long, int>>( '.', max(Y[i] - t - 1, 0LL), pair<long long, int>(X[i], 1))); ; eqy.add(util::Event<long long, pair<long long, int>>( '.', min(Y[i] + t, M), pair<long long, int>(X[i], -1))); } eqy.handle('.', [&](pair<long long, int> data) { long long x = data.first; int amt = data.second; for (int i = 0; i < n; i++) { if (x_dividers[i] >= x - t - 1 && x_dividers[i] < x + t) count[i] += amt; } }); long long y_min = M, y_max = 0; for (int i = 0; i < m; i++) { eqy.advance_until(y_dividers[i]); for (int j = 0; j < n; j++) { if (count[j] == 0) { y_min = min(y_min, y_dividers[i]); y_max = max(y_max, y_dividers[i + 1]); break; } } } return (x_max - x_min <= 2 * t + 1) && (y_max - y_min <= 2 * t + 1); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> N >> M >> K; for (int i = 0; i < K; i++) { cin >> X[i] >> Y[i]; } long long answer = util::search_min(0, max(N, M), can_engulf); cout << answer << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int n4, n7, n47, n74; bool solve() { if (abs(n47 - n74) > 1) return false; for (int fst = 0, _n = (10); fst < _n; fst++) if (fst == 4 || fst == 7) { if (fst == 4 && n47 < n74) continue; if (fst == 7 && n47 > n74) continue; int mn4 = (fst == 4 ? 1 : 0) + n74; int mn7 = (fst == 7 ? 1 : 0) + n47; if (mn4 > n4 || mn7 > n7) continue; vector<int> order; int groups = n47 + n74 + 1; int e4 = n4 - mn4; int e7 = n7 - mn7; for (int i = 0, _n = (groups); i < _n; i++) { int cur = ((fst == 4) ^ (i % 2 == 0) ^ 1) ? 4 : 7; printf("%d", cur); if (i <= 1 && cur == 4) for (int i = 0, _n = (e4); i < _n; i++) printf("4"); if (i >= groups - 2 && cur == 7) for (int i = 0, _n = (e7); i < _n; i++) printf("7"); } printf("\n"); return true; } return false; } int main() { scanf("%d%d%d%d", &n4, &n7, &n47, &n74); if (!solve()) printf("-1\n"); return 0; }
2
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; enum Type{ YOKO, TATE, }; int H,W,change_limit,base_num; int table[10][10]; bool FLG; void recursive(Type type,int changed_count,int base_row,int base_col){ if(FLG)return; if(type == YOKO){ //その行に、超過シーケンスがあるかどうか調べる //行 int left = -1,right,tmp; queue<int> START; START.push(0); int col,start; while(!START.empty()){ start = START.front(); START.pop(); tmp = 0; for(col = start; col < W;){ tmp += table[base_row][col]; if(tmp < 0){ //start~colまでは、超過シーケンスなし if(col <= W-2){ START.push(col+1); } break; }else if(tmp <= base_num){ col++; }else if(tmp > base_num){ while(col+1 <= W-1 && tmp+table[base_row][col+1] > base_num){ col++; tmp += table[base_row][col]; } left = start,right = col; break; } } if(left != -1)break; } //なければ次の行へ if(left == -1){ if(base_row == H-1){ recursive(TATE,changed_count,base_row,0); }else{ recursive(YOKO,changed_count,base_row+1,base_col); } }else{ //あれば、少なくとも、その区間の1つを変更 if(changed_count == change_limit)return; for(int i = left; i <= right; i++){ if(table[base_row][i] <= 0)continue; table[base_row][i] *= -1; recursive(YOKO,changed_count+1,base_row,base_col); table[base_row][i] *= -1; } } }else{ //type == TATE //その列に、超過シーケンスがあるかどうか調べる int top = -1,under,tmp; queue<int> START; START.push(0); int row,start; while(!START.empty()){ start = START.front(); START.pop(); tmp = 0; for(row = start; row < H;){ tmp += table[row][base_col]; if(tmp < 0){ //start~rowまでは、超過シーケンスなし if(row <= H-2){ START.push(row+1); } break; }else if(tmp <= base_num){ row++; }else if(tmp > base_num){ while(row+1 <= H-1 && tmp+table[row+1][base_col] > base_num){ row++; tmp += table[row][base_col]; } top = start,under = row; break; } } if(top != -1)break; } //なければ次の列へ if(top == -1){ if(base_col == W-1){ FLG = true; return; }else{ recursive(TATE,changed_count,base_row,base_col+1); } }else{ //あれば、少なくとも、その区間の1つを変更 if(changed_count == change_limit)return; for(int i = top; i <= under; i++){ if(table[i][base_col] <= 0)continue; table[i][base_col] *= -1; recursive(TATE,changed_count+1,base_row,base_col); table[i][base_col] *= -1; } } } } int main(){ scanf("%d %d %d %d",&H,&W,&change_limit,&base_num); for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++)scanf("%d",&table[row][col]); } //初期状態での、行の超過シーケンス数を数える //行 int over_row_num = 0,tmp; queue<int> START; for(int row = 0; row < H; row++){ START.push(0); int col,start; while(!START.empty()){ start = START.front(); START.pop(); tmp = 0; for(col = start; col < W;){ tmp += table[row][col]; if(tmp < 0){ //start~colまでは、超過シーケンスなし if(col <= W-2){ START.push(col+1); } break; }else if(tmp <= base_num){ col++; }else if(tmp > base_num){ over_row_num++; while(col+1 <= W-1 && tmp+table[row][col+1] > base_num){ col++; tmp += table[row][col]; } if(col <= W-2){ START.push(col+1); } break; } } } } //列 int over_col_num = 0; for(int col = 0; col < W; col++){ START.push(0); int row,start; while(!START.empty()){ start = START.front(); START.pop(); tmp = 0; for(row = start; row < H;){ tmp += table[row][col]; if(tmp < 0){ //start~rowまでは、超過シーケンスなし if(row <= H-2){ START.push(row+1); } break; }else if(tmp <= base_num){ row++; }else if(tmp > base_num){ over_col_num++; while(row+1 <= H-1 && tmp+table[row+1][col] > base_num){ row++; tmp += table[row][col]; } if(row <= H-2){ START.push(row+1); } break; } } } } if(over_row_num == 0 && over_col_num == 0){ printf("Yes\n"); return 0; } if(change_limit < max(over_row_num,over_col_num)){ //反転回数の上限が、反転が必要な行数または列数より少ない printf("No\n"); return 0; } FLG = false; recursive(YOKO,0,0,0); if(FLG){ printf("Yes\n"); }else{ printf("No\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[202]; int main() { int i, j, n, t1, t2, x, y; while (~scanf("%d", &n)) { t1 = 0; t2 = 0; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); t2 += a[i]; } scanf("%d%d", &x, &y); for (i = 1; i <= n; i++) { t1 += a[i]; t2 -= a[i]; if (t1 <= y && t1 >= x && t2 <= y && t2 >= x) break; } if (i == n + 1) { printf("0\n"); } else printf("%d\n", i + 1); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 22; int d[N], spos[N], n, m; vector<pair<int, int> > res; void solveRev(int id, int a, int b, int c) { if (id > m) return; solveRev(id + 1, a, c, b); for (int i = spos[id]; i < spos[id + 1]; ++i) res.push_back(pair<int, int>(a, c)); solveRev(id + 1, b, a, c); } void solve(int id, int a, int b, int c) { if (id == m) { for (int i = spos[id]; i + 1 < spos[id + 1]; ++i) res.push_back(pair<int, int>(a, b)); res.push_back(pair<int, int>(a, c)); for (int i = spos[id]; i + 1 < spos[id + 1]; ++i) res.push_back(pair<int, int>(b, c)); return; } if (spos[id] + 1 == spos[id + 1]) { solveRev(id, a, b, c); return; } solveRev(id + 1, a, b, c); for (int i = spos[id]; i < spos[id + 1]; ++i) res.push_back(pair<int, int>(a, b)); solveRev(id + 1, c, b, a); for (int i = spos[id]; i < spos[id + 1]; ++i) res.push_back(pair<int, int>(b, c)); solve(id + 1, a, b, c); } int main() { scanf("%d", &n); m = 0; for (int i = 1; i <= n; ++i) { scanf("%d", &d[i]); if (d[i] != d[i - 1]) spos[++m] = i; } spos[m + 1] = n + 1; solve(1, 1, 2, 3); printf("%d\n", (int)res.size()); for (pair<int, int> y : res) printf("%d %d\n", y.first, y.second); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 5; const long long M = 1e6 + 5; long long ft[N], D[M], a[N]; void update(long long id, long long val) { while (id < N) { ft[id] += val; id += (id & -id); } } long long query(long long id) { long long res = 0; while (id > 0) { res += ft[id]; id -= (id & -id); } return res; } int32_t main() { ios::sync_with_stdio(false), cin.tie(nullptr); ; for (long long i = 1; i < M; i++) { for (long long j = i; j < M; j += i) { D[j]++; } } long long n, q; cin >> n >> q; set<long long> active; for (long long i = 1; i <= n; i++) { cin >> a[i]; update(i, a[i]); active.insert(i); } for (long long _i = 1; _i <= q; _i++) { long long type, l, r; cin >> type >> l >> r; if (type == 1) { set<long long> for_del; auto it = active.lower_bound(l); while (it != active.end() && *it <= r) { if (D[a[*it]] != a[*it]) { update(*it, D[a[*it]] - a[*it]); a[*it] = D[a[*it]]; } else { for_del.insert(*it); } it++; } for (auto it : for_del) active.erase(it); } else { cout << query(r) - query(l - 1) << "\n"; } } }
6
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define N 100007 #define fo(i,a,b) for(i=a;i<=b;i++) using namespace std; int i,j,k,n,x,y,gx,gy,cnt,ans; int a[N],b[N],c[N],d[N]; int fa[N]; bool bz[N]; int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);} int read(){ int fh=1,res=0;char ch; while((ch<'0'||ch>'9')&&(ch^'0'))ch=getchar(); if(ch=='-')fh=-1,ch=getchar(); while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+(ch^'0'),ch=getchar(); return fh*res; } int main(){ n=read(); fo(i,1,n)a[i]=read(),a[n+1]^=a[i];fo(i,1,n)b[i]=read(),b[n+1]^=b[i]; memcpy(c,a,sizeof(c));memcpy(d,b,sizeof(d));n++; sort(c+1,c+n+1);sort(d+1,d+n+1); fo(i,1,n)if(c[i]!=d[i]){printf("-1");return 0;} cnt=unique(c+1,c+n+1)-c-1; fo(i,1,cnt)fa[i]=i; fo(i,1,n)a[i]=lower_bound(c+1,c+cnt+1,a[i])-c,b[i]=lower_bound(c+1,c+cnt+1,b[i])-c; fo(i,1,n){ if(a[i]==b[i]&&i<n)continue; ans+=(i<n); x=a[i],y=b[i]; gx=get(x);gy=get(y); if(gx!=gy)fa[gy]=gx; bz[x]=bz[y]=1; } fo(i,1,cnt)fa[i]=get(fa[i]); fo(i,1,cnt)if(bz[i]&&fa[i]==i)ans++; if(ans)ans--; printf("%d",ans); return 0; }
0
#include <bits/stdc++.h> int main() { long n, k; scanf("%ld %ld", &n, &k); long m = 2 * k + 1; if (n <= m) { printf("1\n%ld\n", (n + 1) / 2); } else if (n % m == 0) { printf("%ld\n", n / m); for (long p = k + 1; p <= n; p += m) { printf("%ld ", p); } puts(""); } else if (n % m > k) { long a = n / m; long u = n % m; printf("%ld\n", a + 1); for (long p = u - k; p < n; p += m) { printf("%ld ", p); } puts(""); } else if (n % m <= k) { long a = -1 + (n / m); long u = m + n % m; long x = u / 2; u -= u / 2; long y = n + 1 - u + k; printf("%ld\n", a + 2); for (long p = x - k; p < y; p += m) { printf("%ld ", p); } printf("%ld\n", y); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; vector<int> a, b; vector<pair<int, int> > cost; struct Item { long long cost; int id, day; bool operator<(const Item &t) const { return cost < t.cost; } }; int n, m, k, s; bool solve(int days, bool print = false) { int a_best = MOD, b_best = MOD, a_day, b_day; for (int i = 0; i < days; ++i) { if (a_best > a[i]) { a_best = a[i]; a_day = i + 1; } if (b_best > b[i]) { b_best = b[i]; b_day = i + 1; } } vector<Item> items; for (int i = 0; i < m; ++i) items.push_back( {(long long)cost[i].first * a_best + (long long)cost[i].second * b_best, i + 1, cost[i].first == 0 ? b_day : a_day}); sort(items.begin(), items.end()); long long total = 0; for (int i = 0; i < k; ++i) total += items[i].cost; if (print) { for (int i = 0; i < k; ++i) printf("%d %d\n", items[i].id, items[i].day); } return total <= s; } int main() { cin >> n >> m >> k >> s; a.resize(n); b.resize(n); cost.resize(m); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 0; i < n; ++i) scanf("%d", &b[i]); for (int i = 0; i < m; ++i) { scanf("%d %d", &cost[i].first, &cost[i].second); if (cost[i].first == 1) { swap(cost[i].second, cost[i].first); cost[i].second = 0; } else cost[i].first = 0; } cost.push_back({0, 0}); int lo = 1, hi = n + 1, mid; while (lo < hi) { mid = (lo + hi) / 2; if (solve(mid)) hi = mid; else lo = mid + 1; } if (lo > n) { puts("-1"); return 0; } cout << lo << '\n'; solve(lo, 1); }
4
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; string s; string t; cin >> s; k -= 1; int c = 0; while (k--) { t.push_back('('); t.push_back(')'); c += 2; } c = n - c; c /= 2; for (int i = 0; i < c; i++) t.push_back('('); for (int i = 0; i < c; i++) t.push_back(')'); cout << n << '\n'; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (s[j] == t[i]) { cout << i + 1 << " " << j + 1 << '\n'; reverse(s.begin() + i, s.begin() + j + 1); break; } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> int main() { char a[15], b[15]; int c, d, s; scanf("%s%s", a, b); if (strcmp(a, "monday") == 0) c = 1; if (strcmp(a, "tuesday") == 0) c = 2; if (strcmp(a, "wednesday") == 0) c = 3; if (strcmp(a, "thursday") == 0) c = 4; if (strcmp(a, "friday") == 0) c = 5; if (strcmp(a, "saturday") == 0) c = 6; if (strcmp(a, "sunday") == 0) c = 7; if (strcmp(b, "monday") == 0) d = 1; if (strcmp(b, "tuesday") == 0) d = 2; if (strcmp(b, "wednesday") == 0) d = 3; if (strcmp(b, "thursday") == 0) d = 4; if (strcmp(b, "friday") == 0) d = 5; if (strcmp(b, "saturday") == 0) d = 6; if (strcmp(b, "sunday") == 0) d = 7; s = d - c; if (s < 0) s = 7 + s; if (s == 2 || s == 3 || s == 0) printf("YES"); else printf("NO"); return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I) int main(){ int q,k,x; cin >> q; set<int> se; while(q--){ cin >> k >> x; if(k){ cout << se.count(x) << "\n"; }else{ se.insert(x); cout << se.size() << "\n"; } } }
0
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long large = 2000000000000000000LL; struct Vector { long long x, y; Vector(long long x = 0, long long y = 0) : x(x), y(y) {} }; Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } bool operator<(const Vector &A, const Vector &B) { return A.x < B.x || (A.x == B.x && A.y < B.y); } bool operator==(const Vector &A, const Vector &B) { return A.x - B.x == 0 && A.y - B.y == 0; } long long dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; } long long cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } int getConvexHull(vector<Vector> p, vector<Vector> &ch) { sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end()); int n = (int)p.size(); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) { ch.pop_back(); m--; } ch.push_back(p[i]); m++; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) { ch.pop_back(); m--; } ch.push_back(p[i]); m++; } if (n > 1) { ch.pop_back(); m--; } return m; } int main() { int n; cin >> n; vector<Vector> p(n, Vector()); for (int i = 0; i < n; i++) scanf("%lld%lld", &p[i].x, &p[i].y); map<Vector, int> comp; for (int i = 0; i < n; i++) comp[p[i]] = i; string s; cin >> s; int cur = -1; vector<bool> vis(n, false); vector<Vector> ch; getConvexHull(p, ch); cur = comp[ch[0]]; vector<int> ans(1, cur); for (int i = 0; i < (int)s.length(); i++) { vector<Vector> t; for (int j = 0; j < n; j++) if (!vis[j]) t.push_back(p[j]); ch.clear(); getConvexHull(t, ch); int ne = -1; for (int j = 0; j < (int)ch.size(); j++) { if (comp[ch[j]] == cur) { ne = j; break; } } assert(ne != -1); if (s[i] == 'L') ne++; else ne--; ne += (int)ch.size(); ne %= (int)ch.size(); vis[cur] = true; cur = comp[ch[ne]]; ans.push_back(cur); } vis[cur] = true; for (int i = 0; i < n; i++) if (!vis[i]) ans.push_back(i); for (int i = 0; i < n; i++) printf("%d ", ans[i] + 1); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, m; pair<int, int> tl = {-100, -100}, br = {-100, -100}; cin >> n >> m; char paper[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> paper[i][j]; if (paper[i][j] == '*' && (tl.first == -100 && tl.second == -100)) tl = {i, j}; else if (j < tl.second && paper[i][j] == '*') tl = {tl.first, j}; if (paper[i][j] == '*' && (i >= br.first && j > br.second)) br = {i, j}; else if (paper[i][j] == '*' && (i > br.first)) br = {i, br.second}; else if (paper[i][j] == '*' && (j > br.second)) br = {br.first, j}; } } for (int i = tl.first; i <= br.first; i++) { for (int j = tl.second; j <= br.second; j++) { cout << paper[i][j]; } cout << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 105; int n; char s[N]; int a[N]; ll dp[N][N][N]; ll f(int l, int r, int pref) { if (l > r) return 0; ll &ans = dp[l][r][pref]; if (~ans) return ans; ans = f(l + 1, r, 1) + a[pref]; for (int k = l + 1; k <= r; k++) if (s[l] == s[k]) { ans = max(ans, f(k, r, pref + 1) + f(l + 1, k - 1, 1)); } return ans; } int main() { memset(dp, -1, sizeof dp); scanf("%d %s", &n, s); for (int i = 1; i <= n; i++) scanf("%d", a + i); printf("%lld\n", f(0, n - 1, 1)); }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; template <typename T> bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; }; template <typename T> bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; }; struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; template <typename Monoid> struct Segment_Tree { using F = function<Monoid(Monoid, Monoid)>; int n; vector<Monoid> seg; const F f; const Monoid e1; Segment_Tree(const vector<Monoid> &v, const F &f, const Monoid &e1) : f(f), e1(e1) { int m = (int)v.size(); n = 1; while (n < m) n <<= 1; seg.assign(2 * n, e1); copy(v.begin(), v.end(), seg.begin() + n); for (int i = n - 1; i > 0; i--) seg[i] = f(seg[2 * i], seg[2 * i + 1]); } void change(int i, const Monoid &x) { seg[i += n] = x; while (i >>= 1) { seg[i] = f(seg[2 * i], seg[2 * i + 1]); } } Monoid query(int l, int r) const { Monoid L = e1, R = e1; l += n, r += n; while (l < r) { if (l & 1) L = f(L, seg[l++]); if (r & 1) R = f(seg[--r], R); l >>= 1, r >>= 1; } return f(L, R); } Monoid operator[](int i) const { return seg[n + i]; } template <typename C> int find_subtree(int i, const C &check, const Monoid &x, Monoid &M, bool type) const { while (i < n) { Monoid nxt = type ? f(seg[2 * i + type], M) : f(M, seg[2 * i + type]); if (check(nxt, x)) i = 2 * i + type; else M = nxt, i = 2 * i + (type ^ 1); } return i - n; } template <typename C> int find_first(int l, const C &check, const Monoid &x) const { Monoid L = e1; int a = l + n, b = n + n; while (a < b) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt, x)) return find_subtree(a, check, x, L, false); L = nxt, a++; } a >>= 1, b >>= 1; } return n; } template <typename C> int find_last(int r, const C &check, const Monoid &x) const { Monoid R = e1; int a = n, b = r + n; while (a < b) { if (b & 1 || a == 1) { Monoid nxt = f(seg[--b], R); if (check(nxt, x)) return find_subtree(b, check, x, R, true); R = nxt; } a >>= 1, b >>= 1; } return -1; } }; int main() { int N; cin >> N; vector<int> a(N), b(N); vector<vector<int>> ids(N); for (int i = 0; i < N; i++) { cin >> a[i] >> b[i]; a[i]--, b[i]--; ids[a[i]].emplace_back(i); } vector<int> ans(N), p(N); priority_queue<pii, vector<pii>, greater<pii>> que; for (int i = 0; i < N; i++) { for (auto &e : ids[i]) { que.emplace(b[e], e); } ans[que.top().second] = i; que.pop(); } for (int i = 0; i < N; i++) p[ans[i]] = i; auto f = [](int a, int b) { return min(a, b); }; vector<int> v(N); for (int i = 0; i < N; i++) v[i] = a[p[i]]; Segment_Tree<int> seg(v, f, inf); for (int i = 0; i < N; i++) { int L = i + 1, R = b[p[i]] + 1; if (seg.query(L, R) <= i) { for (int j = L; j <= R - 1; j++) { if (a[p[j]] <= i) { cout << "NO\n"; for (auto &e : ans) cout << e + 1 << ' '; cout << '\n'; swap(ans[p[i]], ans[p[j]]); for (auto &e : ans) cout << e + 1 << ' '; cout << '\n'; return 0; } } } } cout << "YES\n"; for (auto &e : ans) cout << e + 1 << ' '; cout << '\n'; }
6
#include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef pair<int, int> pii; namespace io { const int maxb = 1 << 15; char b[maxb], *s = b, *t = b; bool Getchar(char &ch) { return ch = s == t && (t = (s = b) + fread(b, 1, maxb, stdin)) == b ? 0 : *s ++; } int Getint() { char ch; while (Getchar(ch) && (ch < '0' || ch > '9')); int s = ch - '0'; while (Getchar(ch) && ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } } using io::Getint; template<typename ty> bool Chkmax(ty &x, const ty &y) { return x < y ? x = y, 1 : 0; } template<typename ty> bool Chkmin(ty &x, const ty &y) { return x > y ? x = y, 1 : 0; } const int maxn = 2e5 + 10; const int inf = 1e9 + 10; int M, Mn[maxn * 4]; void Init(int n) { for (M = 1; M < (n + 2); M <<= 1); for (int i = M + M; i; -- i) Mn[i] = inf; } void Modify(int t, int val) { for (t += M; t; t >>= 1) Chkmin(Mn[t], val); } int Query(int s, int t) { int res = inf; for (s += M - 1, t += M + 1; s ^ t ^ 1; s >>= 1, t >>= 1) { if (~ s & 1) Chkmin(res, Mn[s ^ 1]); if ( t & 1) Chkmin(res, Mn[t ^ 1]); } return res; } bool b[maxn]; vector<int> opt[maxn]; int f[maxn]; int main() { int n = Getint(), ans = 0; for (int i = 1; i <= n; ++ i) { b[i] = Getint(); ans += !b[i]; } int Q = Getint(); for (int i = 1; i <= Q; ++ i) { int l = Getint(), r = Getint(); opt[l].push_back(r); } Init(n); for (int i = 0; i <= n; ++ i) { if (i) f[i] = min(f[i - 1] + (b[i] ? 1 : -1), Query(i, i)); int size = opt[i + 1].size(); sort(opt[i + 1].begin(), opt[i + 1].end()); for (int j = size - 1; ~ j; -- j) Modify(opt[i + 1][j], min(Query(i + 1, opt[i + 1][j]), f[i])); } ans += f[n]; printf("%d\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int b[10], c[10]; int m[4] = {0}; int i = 0; int d = 0; for (int a = 0; a < 10; a++) { cout << a << a << a << a << endl; cin >> b[a] >> c[a]; if (b[a] > 0) { m[i] = a; i++; } } cout << m[0] << m[1] << m[2] << m[3] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[0] << m[1] << m[3] << m[2] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[0] << m[2] << m[1] << m[3] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[0] << m[2] << m[3] << m[1] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[0] << m[3] << m[1] << m[2] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[0] << m[3] << m[2] << m[1] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[1] << m[0] << m[2] << m[3] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[1] << m[0] << m[3] << m[2] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[1] << m[2] << m[0] << m[3] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[1] << m[2] << m[3] << m[0] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[1] << m[3] << m[0] << m[2] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[1] << m[3] << m[2] << m[0] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[2] << m[1] << m[0] << m[3] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[2] << m[1] << m[3] << m[0] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[2] << m[0] << m[1] << m[3] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[2] << m[0] << m[3] << m[1] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[2] << m[3] << m[1] << m[0] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[2] << m[3] << m[0] << m[1] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[3] << m[1] << m[2] << m[0] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[3] << m[1] << m[0] << m[2] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[3] << m[2] << m[1] << m[0] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[3] << m[2] << m[0] << m[1] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[3] << m[0] << m[1] << m[2] << endl; cin >> i >> d; if (i == 4) return 0; cout << m[3] << m[0] << m[2] << m[1] << endl; cin >> i >> d; if (i == 4) return 0; }
2
#include <bits/stdc++.h> using namespace std; int k; struct sptman { int minstr[10], maxstr[10]; int sz; sptman() { sz = 1; int t; for (int i = 0; i < k; i++) { scanf("%d", &t); minstr[i] = maxstr[i] = t; } } void merge(const sptman &a) { for (int i = 0; i < k; i++) { minstr[i] = min(minstr[i], a.minstr[i]); maxstr[i] = max(maxstr[i], a.maxstr[i]); } sz += a.sz; } }; struct cmp { bool operator()(const sptman &a, const sptman &b) { for (int i = 0; i < k; i++) if (b.minstr[i] < a.maxstr[i]) return false; return true; } }; set<sptman, cmp> all; int main(int argc, char *argv[]) { int n; set<sptman, cmp>::iterator lp, hp; scanf("%d%d", &n, &k); while (n--) { sptman sm = sptman(); lp = all.lower_bound(sm); hp = all.upper_bound(sm); while (lp != hp) { sm.merge(*lp); all.erase(lp++); } all.insert(sm); printf("%d\n", all.rbegin()->sz); } return 0; }
3
#include <iostream> #include <cstdio> #include <map> using namespace std; int main() { int n; char b[2]; map<string,int> m; m["A"]=0; m["B"]=0; m["AB"]=0; m["O"]=0; while(~scanf("%d,%s",&n,b)){ m[b]++; } printf("%d\n",m["A"]); printf("%d\n",m["B"]); printf("%d\n",m["AB"]); printf("%d\n",m["O"]); return 0; }
0
#include <iostream> #include <algorithm> using namespace std; #define ll long long int main() { int t; cin >> t; while (t--) { int x, y; cin >> x >> y; ll ans = 0; for (int i = 2; (i - 1) * (i + 1) <= x && i <= y; i++) { ll cnt = min(x / (i - 1), (y + 1)) - i; ans += cnt; } cout << ans << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; void Fast() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void File() {} using ll = long long; using ull = unsigned long long; ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } int dx[] = {0, 0, -1, 1, 1, 1, -1, -1}; int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; int n, m, k, shovel[200005], offer[2005]; ll res[2005]; ll dp[2005]; int main() { Fast(), File(); cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> shovel[i]; sort(shovel, shovel + n + 1); for (int i = 2; i <= k; i++) { shovel[i] += shovel[i - 1]; } int x, y; for (int i = 0; i < m; i++) { cin >> x >> y; if (x <= k) { offer[x] = max(offer[x], y); } } for (int i = k; i > 0; i--) { dp[i] = 1e18; for (int j = 1; j + i <= k + 1; j++) dp[i] = min(dp[i], dp[i + j] + shovel[i + j - 1] - shovel[i + offer[j] - 1]); } cout << dp[1] << "\n"; }
6
#include <bits/stdc++.h> using namespace std; map<int, long long int> dp[200005]; int closest[500005][2]; vector<int> tr[500005]; vector<int> safe; void update(int i, int j, long long int val) { if (dp[i + 1].find(j) == dp[i + 1].end()) dp[i + 1][j] = val; else dp[i + 1][j] = min(dp[i + 1][j], val); } int main() { int n, m, k, q; scanf("%d %d %d %d", &n, &m, &k, &q); for (int i = 1; i <= k; i++) { int r, c; scanf("%d %d", &r, &c); tr[r].push_back(c); } for (int i = 1; i <= n; i++) { sort(tr[i].begin(), tr[i].end()); } for (int i = 1; i <= q; i++) { int sf; scanf("%d", &sf); safe.push_back(sf); } sort(safe.begin(), safe.end()); for (int i = 1; i <= safe[0]; i++) { closest[i][0] = safe[0]; closest[i][1] = safe[0]; } for (int i = safe.back(); i <= m; i++) closest[i][0] = closest[i][1] = safe.back(); for (int i = 1; i < safe.size(); i++) { for (int j = safe[i - 1] + 1; j < safe[i]; j++) { closest[j][0] = safe[i - 1]; closest[j][1] = safe[i]; } closest[safe[i]][0] = safe[i]; closest[safe[i]][1] = safe[i]; } for (int i = n; i >= 1; i--) { if (tr[i].size() != 0) { n = i; break; } } dp[1][1] = 0; long long int ans = 2000000000000000000LL; for (int i = 1; i <= n; i++) { for (auto pr : dp[i]) { int col = pr.first; long long int val = pr.second; if (tr[i].size() == 0 || (tr[i].size() == 1 && col == tr[i][0])) { long long int x = val + abs(col - closest[col][0]); if (i == n) ans = min(ans, x); update(i, closest[col][0], x); x = val + abs(col - closest[col][1]); if (i == n) ans = min(ans, x); update(i, closest[col][1], x); continue; } int toCol; if (col == tr[i].back()) toCol = tr[i][(int)tr[i].size() - 2]; else toCol = tr[i].back(); long long int x = val + abs(tr[i][0] - col) + abs(toCol - tr[i][0]); if (i == n) ans = min(ans, x); int lastCol = tr[i].back(); update(i, closest[toCol][0], x + abs(toCol - closest[toCol][0])); update(i, closest[toCol][1], x + abs(toCol - closest[toCol][1])); if (col == tr[i][0]) toCol = tr[i][1]; else toCol = tr[i][0]; x = val + abs(tr[i].back() - col) + abs(tr[i].back() - toCol); if (i == n) ans = min(ans, x); update(i, closest[toCol][0], x + abs(toCol - closest[toCol][0])); update(i, closest[toCol][1], x + abs(toCol - closest[toCol][1])); } } ans += (n - 1); printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; typedef pair<int,char> P; bool ok(P a,P b,P c){ return a.second == b.second && b.second == c.second && ((a.first-b.first == 1 && b.first-c.first == 1) || (a.first == b.first && b.first == c.first)); } bool comp(P a,P b){ return a.first + (int)a.second * 10 < b.first + (int)b.second * 10; } int main(){ int T; cin >> T; for(int i=0; i<T; i++){ vector<P> v(9); for(auto &t:v) cin>>t.first; for(auto &t:v) cin>>t.second; bool flag=false; do{ flag = (ok(v[0],v[1],v[2]) && ok(v[3],v[4],v[5]) && ok(v[6],v[7],v[8])); }while(!flag && next_permutation(v.begin(),v.end())); cout << (flag ? 1 : 0) << endl; } }
0
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &o, vector<T> v) { for (auto u : v) o << u << " "; return o; } template <typename T> ostream &operator<<(ostream &o, unordered_set<T> v) { for (auto u : v) o << u << " "; return o; } int n, h, g, m; std::vector<int> a; template <bool dry> bool f(int i) { int l = 2 * i, r = 2 * i + 1; if (l >= n || (!a[l] && !a[r])) { if constexpr (!dry) a[i] = 0; return i >= m; } if (a[l] > a[r]) { if constexpr (!dry) a[i] = a[l]; return f<dry>(l); } else { if constexpr (!dry) a[i] = a[r]; return f<dry>(r); } } void solve() { cin >> h >> g; n = 1 << h; m = 1 << g; a = std::vector<int>(n); for (int i = (1); i < (n); i++) cin >> a[i]; std::vector<int> op; for (int i = (1); i < (n); i++) { while (a[i] != 0 && f<true>(i)) { f<false>(i); op.push_back(i); } } long long sum = 0; for (int i = (1); i < (m); i++) sum += a[i]; std::cout << sum << std::endl; std::cout << op << std::endl; } int main(void) { ios::sync_with_stdio(false); int T; cin >> T; while (T--) solve(); }
3
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long a[N], b[N]; vector<long long> v; int32_t main() { long long t; cin >> t; while (t--) { long long a, b, c, q; cin >> a >> b >> c; if (b > a) swap(a, b); if (b > a / 2) cout << "-1\n"; else { long long as = a - b; if (c > as * 2) cout << "-1\n"; else if ((c - as <= 0) && (c + as > as * 2)) cout << "-1\n"; else if (c - as > 0) cout << c - as << "\n"; else if (c + as <= as * 2) cout << c + as << "\n"; } } }
2
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define mp make_pair #define pb push_back #define lb(v,n) lower_bound(v.begin(),v.end(),n) #define ub(v,n) upper_bound(v.begin(),v.end(),n) #define srt(v) sort(v.begin(),v.end()) #define fas ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define PIE 3.14159265358979323846 //----------------------seive--------------------------// /*vector<bool> seive(int n) { vector<bool>prime(n+1,1); for(int i=2;i*i<=n;i++) { if(prime[i]) { for(int j=i;j*i<=n;j++) { prime[j*i]=0; } } } return prime; } */ //----------------------prime factoisation of n-------------------------// map<ll,ll> primefactorisation(ll n) { map<ll,ll> factors; for(ll i=2;i*i<=n;i++) { if(n%i==0) { factors.insert(make_pair(i,0)); while(n%i==0) { factors[i]++; n=n/i; } } } if(n>1) { factors.insert(make_pair(n,1)); } return factors; } //--------------------------------------ncr-----------------------------------// /* ll ncr(ll n,ll r){ if(n<r) { return 0; } if(n-r<r) { r=n-r; } if(r==0) { return 1; } ll num=1; ll den=1; while(r) { num=num*n; den=den*r; ll hcf=__gcd(num,den); num=num/hcf; den=den/hcf; n--; r--; } return num/den; }*/ //--------------------------------power modulo-------------------------------// /*ll powermod(ll base,ll power,ll mod) { if(power==1) { return base%mod; } if(power==0) { return 1%m; } ll shortoutput; if(power%2==0) { shortoutput=powermod((base%mod)*(base%mod)%mod,power/2,mod); return shortoutput%mod; } else { shortoutput=powermod((base%mod)*(base%mod)%mod,(power-1)/2,mod); return ((base%mod)*(shortoutput%mod))%mod; } }*/ //----------------------------------------------------------------------------// /*void dfs(vector<vector<int>>&list,int node,vector<int>&visited) { visited[node]=1; cout<<node<<"->"; for(int i=0;i<list[node].size();i++) { if(!visited[list[node][i]]) { dfs(list,list[node][i],visited); } } }*/ //----------------------------sum of digits-----------------------------------// ll sumofdigits(ll n) { ll temp=n; ll sum=0; while(temp>0) { sum=sum+(temp%10); temp=temp/10; } return sum; } bool checkstring(string s) { int f1,f2,f3; f1=0;f2=0;f3=0; for(int i=0;i<s.size();i++) { if(s[i]>='a'&&s[i]<='z') { f1++; } else if(s[i]>='A'&&s[i]<='B') { f2++; } else if(s[i]>='0'&&s[i]<='9') { f3++; } } if(f1>0&&f2>0&&f3>0) { return 1; } else { return 0; } } vector<char> getvalues(char c) { vector<char>s(3); s[0]=c; if(c>='0'&&c<='9') { s[1]=c+'a'; s[2]=c+'A'; return s; } else if(c>='a'&&c<='z') { s[1]=c-'a'+'A'; s[2]=((c-'a')%10)+'0'; return s; } else if(c>='A'&&c<='Z') { s[1]=c-'A'+'a'; s[2]=((c-'A')%10)+'0'; return s; } } //------------------- solve-------------------------------------------------// void solve() { int n; cin>>n; vector<ll>arr(n); int si=-1; for(int i=0;i<n;i++) { cin>>arr[i]; if(arr[i]>=0&&si==-1) { si=i; } } if(si==-1) { cout<<0<<endl; } else { ll sum=0; ll num=n-si; multiset<ll>m; for(int i=si;i<n;i++) { sum=sum+arr[i]; if(arr[i]<0) { m.insert(arr[i]); } if(sum>=0) { continue; } else { ll temp=*(m.begin()); sum=sum-temp; auto it=m.begin(); m.erase(it); num--; } } cout<<num<<endl; } } //---------------------------------------------------------------------------// int main() { fas /* #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif*/ ll t=1; // cin>>t; while(t--) solve(); }
3
#include <bits/stdc++.h> using namespace std; map<string, pair<int, int> > ma; int main() { ma["ABU"] = make_pair(1, 3); ma["AIDS"] = make_pair(3, 4); ma["Action"] = make_pair(3, 6); ma["Agreement"] = make_pair(3, 4); ma["Agriculture"] = make_pair(3, 7); ma["American"] = make_pair(3, 27); ma["Arab"] = make_pair(1, 13); ma["Armacost"] = make_pair(3, 3); ma["Assets"] = make_pair(2, 3); ma["Bahrain"] = make_pair(1, 7); ma["Baldrige"] = make_pair(3, 9); ma["Bangemann"] = make_pair(1, 5); ma["Bond"] = make_pair(1, 3); ma["Both"] = make_pair(3, 3); ma["Bouldikian"] = make_pair(1, 3); ma["Brien"] = make_pair(3, 4); ma["Broadly"] = make_pair(2, 3); ma["Byrd"] = make_pair(3, 5); ma["CDs"] = make_pair(2, 7); ma["Cable"] = make_pair(3, 3); ma["Chartered"] = make_pair(2, 6); ma["Chicago"] = make_pair(2, 3); ma["Chirac"] = make_pair(3, 10); ma["Clark"] = make_pair(3, 3); ma["Clayton"] = make_pair(3, 4); ma["Clercq"] = make_pair(3, 8); ma["Commerce"] = make_pair(3, 5); ma["Commission"] = make_pair(3, 3); ma["Conable"] = make_pair(3, 5); ma["Congress"] = make_pair(3, 20); ma["Congressmen"] = make_pair(3, 5); ma["Croo"] = make_pair(3, 5); ma["DHABI"] = make_pair(1, 3); ma["DRAM"] = make_pair(3, 5); ma["Dattel"] = make_pair(1, 3); ma["Dealers"] = make_pair(1, 11); ma["Democrat"] = make_pair(3, 3); ma["Democratic"] = make_pair(3, 11); ma["Department"] = make_pair(3, 4); ma["Deputy"] = make_pair(3, 3); ma["Development"] = make_pair(3, 3); ma["ECGD"] = make_pair(3, 3); ma["EEP"] = make_pair(3, 7); ma["Economic"] = make_pair(3, 8); ma["Edwards"] = make_pair(3, 5); ma["Electric"] = make_pair(3, 3); ma["Electronic"] = make_pair(3, 3); ma["Europe"] = make_pair(3, 8); ma["FECs"] = make_pair(1, 6); ma["FOMC"] = make_pair(2, 8); ma["FSO"] = make_pair(3, 6); ma["Faced"] = make_pair(3, 3); ma["Fitzwater"] = make_pair(3, 3); ma["Fleming"] = make_pair(3, 3); ma["GATT"] = make_pair(3, 17); ma["General"] = make_pair(3, 5); ma["Giordano"] = make_pair(1, 8); ma["Gulf"] = make_pair(1, 4); ma["Heller"] = make_pair(1, 4); ma["Herstatt"] = make_pair(1, 4); ma["Hitachi"] = make_pair(3, 3); ma["Horner"] = make_pair(1, 5); ma["IDB"] = make_pair(3, 4); ma["India"] = make_pair(3, 10); ma["Indian"] = make_pair(3, 5); ma["Industries"] = make_pair(3, 3); ma["Islamic"] = make_pair(3, 4); ma["Jardine"] = make_pair(3, 3); ma["Johnson"] = make_pair(2, 3); ma["July"] = make_pair(3, 4); ma["Kyodo"] = make_pair(3, 4); ma["LDP"] = make_pair(3, 11); ma["Leader"] = make_pair(3, 3); ma["Lebanese"] = make_pair(1, 5); ma["Liberal"] = make_pair(3, 3); ma["Local"] = make_pair(2, 3); ma["London"] = make_pair(3, 11); ma["Lyng"] = make_pair(3, 3); ma["MITI"] = make_pair(3, 17); ma["MMTC"] = make_pair(3, 3); ma["MPs"] = make_pair(3, 4); ma["Malcolm"] = make_pair(3, 4); ma["Margaret"] = make_pair(3, 3); ma["Matsunaga"] = make_pair(3, 5); ma["McCarthy"] = make_pair(2, 3); ma["Melzer"] = make_pair(2, 3); ma["Miller"] = make_pair(3, 3); ma["Most"] = make_pair(3, 3); ma["NEC"] = make_pair(3, 6); ma["Nakasone"] = make_pair(3, 47); ma["Nashashibi"] = make_pair(1, 3); ma["News"] = make_pair(3, 4); ma["Notes"] = make_pair(2, 3); ma["November"] = make_pair(2, 22); ma["OTTAWA"] = make_pair(2, 4); ma["Officials"] = make_pair(3, 5); ma["PHLX"] = make_pair(1, 10); ma["PSL"] = make_pair(2, 3); ma["Partly"] = make_pair(1, 3); ma["Party"] = make_pair(3, 3); ma["Philadelphia"] = make_pair(1, 4); ma["Plaza"] = make_pair(1, 4); ma["Policy"] = make_pair(3, 8); ma["Prime"] = make_pair(3, 17); ma["Representative"] = make_pair(3, 3); ma["Republican"] = make_pair(3, 7); ma["Rich"] = make_pair(3, 3); ma["Richard"] = make_pair(3, 4); ma["Ronald"] = make_pair(3, 4); ma["Rostenkowski"] = make_pair(3, 3); ma["Russell"] = make_pair(2, 4); ma["Ryzhkov"] = make_pair(3, 4); ma["Saba"] = make_pair(3, 6); ma["Savings"] = make_pair(2, 3); ma["Senate"] = make_pair(3, 9); ma["Service"] = make_pair(3, 3); ma["Shiratori"] = make_pair(3, 3); ma["Shoichi"] = make_pair(3, 3); ma["Smart"] = make_pair(3, 5); ma["Stephens"] = make_pair(3, 5); ma["Sumita"] = make_pair(1, 6); ma["Tamura"] = make_pair(3, 4); ma["Tariffs"] = make_pair(3, 8); ma["Thatcher"] = make_pair(3, 4); ma["Their"] = make_pair(3, 4); ma["Third"] = make_pair(3, 5); ma["Though"] = make_pair(1, 3); ma["Tian"] = make_pair(1, 4); ma["Toshiba"] = make_pair(3, 4); ma["Vice"] = make_pair(1, 5); ma["Volcker"] = make_pair(2, 3); ma["Washington"] = make_pair(3, 38); ma["While"] = make_pair(3, 7); ma["White"] = make_pair(3, 12); ma["Willy"] = make_pair(3, 3); ma["Wireless"] = make_pair(3, 3); ma["Wolf"] = make_pair(3, 5); ma["World"] = make_pair(3, 9); ma["Wright"] = make_pair(3, 3); ma["Yasuhiro"] = make_pair(3, 12); ma["Yeutter"] = make_pair(3, 11); ma["abide"] = make_pair(3, 3); ma["able"] = make_pair(1, 3); ma["acceptances"] = make_pair(2, 10); ma["acquisition"] = make_pair(3, 3); ma["act"] = make_pair(3, 8); ma["add"] = make_pair(1, 4); ma["adverse"] = make_pair(3, 3); ma["aggregate"] = make_pair(2, 4); ma["aggregates"] = make_pair(2, 6); ma["agricultural"] = make_pair(3, 3); ma["alleged"] = make_pair(3, 13); ma["amendment"] = make_pair(3, 3); ma["amounts"] = make_pair(1, 4); ma["anger"] = make_pair(3, 4); ma["angry"] = make_pair(3, 4); ma["announcement"] = make_pair(3, 3); ma["annualised"] = make_pair(2, 5); ma["annualized"] = make_pair(2, 5); ma["anti"] = make_pair(3, 3); ma["application"] = make_pair(3, 5); ma["areas"] = make_pair(3, 5); ma["assistance"] = make_pair(1, 14); ma["attempt"] = make_pair(3, 4); ma["auto"] = make_pair(3, 3); ma["averaged"] = make_pair(2, 4); ma["band"] = make_pair(1, 9); ma["bankers"] = make_pair(1, 5); ma["barriers"] = make_pair(3, 6); ma["bearing"] = make_pair(2, 4); ma["bilateral"] = make_pair(3, 12); ma["booked"] = make_pair(2, 4); ma["books"] = make_pair(2, 5); ma["borrowings"] = make_pair(2, 8); ma["bringing"] = make_pair(2, 3); ma["broader"] = make_pair(2, 3); ma["car"] = make_pair(3, 4); ma["carefully"] = make_pair(1, 4); ma["cause"] = make_pair(3, 3); ma["cent"] = make_pair(3, 3); ma["certain"] = make_pair(3, 5); ma["chance"] = make_pair(3, 5); ma["changed"] = make_pair(2, 3); ma["chartered"] = make_pair(2, 7); ma["chequable"] = make_pair(2, 4); ma["chip"] = make_pair(3, 30); ma["chips"] = make_pair(3, 21); ma["circular"] = make_pair(1, 3); ma["claims"] = make_pair(3, 3); ma["closely"] = make_pair(1, 4); ma["coins"] = make_pair(2, 4); ma["come"] = make_pair(3, 14); ma["comment"] = make_pair(1, 4); ma["committed"] = make_pair(1, 4); ma["commodity"] = make_pair(1, 11); ma["common"] = make_pair(1, 3); ma["company"] = make_pair(3, 7); ma["compares"] = make_pair(1, 5); ma["computer"] = make_pair(3, 16); ma["concerned"] = make_pair(3, 4); ma["concerns"] = make_pair(3, 3); ma["conciliatory"] = make_pair(3, 4); ma["confident"] = make_pair(3, 3); ma["considering"] = make_pair(3, 3); ma["consultations"] = make_pair(3, 4); ma["contract"] = make_pair(1, 4); ma["contractionary"] = make_pair(2, 3); ma["contracts"] = make_pair(1, 5); ma["controls"] = make_pair(1, 8); ma["convince"] = make_pair(3, 5); ma["cooperate"] = make_pair(1, 3); ma["counter"] = make_pair(1, 4); ma["countertrade"] = make_pair(3, 7); ma["cross"] = make_pair(3, 3); ma["curbs"] = make_pair(3, 6); ma["currencies"] = make_pair(1, 24); ma["customer"] = make_pair(1, 3); ma["date"] = make_pair(3, 3); ma["deal"] = make_pair(3, 3); ma["dealers"] = make_pair(1, 16); ma["deals"] = make_pair(3, 4); ma["defined"] = make_pair(2, 9); ma["defuse"] = make_pair(3, 3); ma["developed"] = make_pair(3, 3); ma["difficult"] = make_pair(3, 5); ma["dinar"] = make_pair(1, 3); ma["directive"] = make_pair(2, 3); ma["discuss"] = make_pair(3, 3); ma["dispute"] = make_pair(3, 24); ma["doing"] = make_pair(3, 4); ma["dollars"] = make_pair(1, 8); ma["downwards"] = make_pair(2, 3); ma["dumping"] = make_pair(3, 16); ma["duties"] = make_pair(3, 6); ma["eased"] = make_pair(1, 3); ma["efforts"] = make_pair(3, 7); ma["elected"] = make_pair(1, 3); ma["elections"] = make_pair(3, 3); ma["electronic"] = make_pair(3, 14); ma["electronics"] = make_pair(3, 8); ma["emphasis"] = make_pair(3, 3); ma["ensure"] = make_pair(3, 4); ma["estimate"] = make_pair(1, 7); ma["exchequer"] = make_pair(1, 3); ma["excluding"] = make_pair(2, 6); ma["expanding"] = make_pair(3, 5); ma["expectations"] = make_pair(2, 3); ma["export"] = make_pair(3, 10); ma["expressed"] = make_pair(3, 3); ma["extended"] = make_pair(2, 4); ma["fallen"] = make_pair(1, 3); ma["fear"] = make_pair(3, 3); ma["figure"] = make_pair(2, 5); ma["final"] = make_pair(3, 6); ma["fire"] = make_pair(3, 3); ma["float"] = make_pair(2, 6); ma["focus"] = make_pair(3, 5); ma["forced"] = make_pair(3, 4); ma["foster"] = make_pair(1, 5); ma["foundation"] = make_pair(3, 3); ma["friction"] = make_pair(3, 4); ma["fund"] = make_pair(1, 4); ma["futures"] = make_pair(1, 4); ma["gold"] = make_pair(1, 5); ma["goods"] = make_pair(3, 33); ma["group"] = make_pair(3, 3); ma["guilders"] = make_pair(2, 3); ma["hand"] = make_pair(1, 4); ma["hard"] = make_pair(3, 5); ma["hearing"] = make_pair(3, 5); ma["heated"] = make_pair(3, 4); ma["hedging"] = make_pair(1, 6); ma["hope"] = make_pair(3, 6); ma["hoped"] = make_pair(3, 3); ma["house"] = make_pair(1, 3); ma["how"] = make_pair(3, 3); ma["huge"] = make_pair(3, 8); ma["implement"] = make_pair(3, 3); ma["import"] = make_pair(3, 7); ma["important"] = make_pair(3, 3); ma["impose"] = make_pair(3, 15); ma["imposed"] = make_pair(3, 4); ma["imposing"] = make_pair(3, 3); ma["include"] = make_pair(3, 10); ma["index"] = make_pair(1, 3); ma["industrialized"] = make_pair(1, 3); ma["industries"] = make_pair(3, 4); ma["intend"] = make_pair(1, 3); ma["interbank"] = make_pair(1, 3); ma["interests"] = make_pair(3, 3); ma["investigation"] = make_pair(3, 4); ma["irrational"] = make_pair(3, 3); ma["island"] = make_pair(1, 3); ma["issue"] = make_pair(3, 11); ma["join"] = make_pair(3, 4); ma["joint"] = make_pair(3, 9); ma["keep"] = make_pair(3, 3); ma["kind"] = make_pair(3, 3); ma["leaders"] = make_pair(3, 5); ma["legislation"] = make_pair(3, 8); ma["liabilities"] = make_pair(2, 3); ma["licences"] = make_pair(3, 4); ma["list"] = make_pair(3, 4); ma["live"] = make_pair(3, 10); ma["look"] = make_pair(3, 4); ma["loss"] = make_pair(3, 3); ma["makers"] = make_pair(3, 12); ma["manufacturers"] = make_pair(3, 3); ma["maturing"] = make_pair(1, 3); ma["memory"] = make_pair(3, 4); ma["microchip"] = make_pair(3, 7); ma["microchips"] = make_pair(3, 11); ma["minimum"] = make_pair(1, 3); ma["minus"] = make_pair(2, 3); ma["minutes"] = make_pair(2, 4); ma["missiles"] = make_pair(3, 3); ma["morning"] = make_pair(1, 7); ma["moves"] = make_pair(3, 4); ma["narrowly"] = make_pair(2, 4); ma["newly"] = make_pair(1, 3); ma["normally"] = make_pair(1, 3); ma["offers"] = make_pair(1, 3); ma["offsetting"] = make_pair(1, 3); ma["opened"] = make_pair(3, 4); ma["opposition"] = make_pair(3, 4); ma["output"] = make_pair(3, 8); ma["outstanding"] = make_pair(2, 9); ma["package"] = make_pair(3, 4); ma["packages"] = make_pair(3, 4); ma["parliament"] = make_pair(3, 3); ma["parties"] = make_pair(3, 5); ma["parts"] = make_pair(3, 6); ma["passed"] = make_pair(3, 5); ma["per"] = make_pair(3, 5); ma["persuade"] = make_pair(3, 5); ma["pesos"] = make_pair(2, 9); ma["planned"] = make_pair(3, 4); ma["pledged"] = make_pair(3, 4); ma["plus"] = make_pair(2, 19); ma["polled"] = make_pair(2, 4); ma["power"] = make_pair(3, 4); ma["practices"] = make_pair(3, 6); ma["predicted"] = make_pair(3, 3); ma["pressing"] = make_pair(1, 3); ma["products"] = make_pair(3, 17); ma["programme"] = make_pair(1, 3); ma["projections"] = make_pair(2, 4); ma["proposal"] = make_pair(3, 3); ma["propose"] = make_pair(3, 7); ma["provided"] = make_pair(1, 9); ma["provisional"] = make_pair(2, 22); ma["purchased"] = make_pair(1, 4); ma["quickly"] = make_pair(3, 4); ma["ranges"] = make_pair(1, 3); ma["react"] = make_pair(3, 4); ma["ready"] = make_pair(3, 3); ma["recommendation"] = make_pair(3, 3); ma["recommendations"] = make_pair(3, 5); ma["reform"] = make_pair(3, 3); ma["regret"] = make_pair(3, 3); ma["relations"] = make_pair(3, 3); ma["relationship"] = make_pair(3, 4); ma["replied"] = make_pair(3, 4); ma["report"] = make_pair(3, 10); ma["request"] = make_pair(3, 3); ma["resist"] = make_pair(3, 3); ma["resolve"] = make_pair(3, 4); ma["restraint"] = make_pair(2, 4); ma["restrictions"] = make_pair(3, 3); ma["retaliate"] = make_pair(3, 8); ma["retaliation"] = make_pair(3, 19); ma["retaliatory"] = make_pair(3, 8); ma["round"] = make_pair(1, 4); ma["row"] = make_pair(3, 7); ma["ruling"] = make_pair(3, 3); ma["run"] = make_pair(3, 3); ma["sanctions"] = make_pair(3, 26); ma["says"] = make_pair(1, 7); ma["seasonal"] = make_pair(2, 3); ma["secretary"] = make_pair(3, 3); ma["see"] = make_pair(3, 4); ma["sell"] = make_pair(3, 4); ma["semiconductor"] = make_pair(3, 33); ma["semiconductors"] = make_pair(3, 23); ma["send"] = make_pair(3, 5); ma["separate"] = make_pair(3, 5); ma["serious"] = make_pair(3, 3); ma["serve"] = make_pair(1, 6); ma["services"] = make_pair(3, 9); ma["sessions"] = make_pair(1, 3); ma["settle"] = make_pair(3, 6); ma["settlement"] = make_pair(3, 6); ma["she"] = make_pair(3, 8); ma["shipments"] = make_pair(3, 3); ma["sight"] = make_pair(2, 5); ma["signed"] = make_pair(3, 10); ma["significantly"] = make_pair(3, 3); ma["slash"] = make_pair(3, 3); ma["smallest"] = make_pair(2, 3); ma["something"] = make_pair(3, 4); ma["soon"] = make_pair(3, 5); ma["source"] = make_pair(3, 3); ma["specific"] = make_pair(3, 5); ma["speech"] = make_pair(3, 3); ma["spokeswoman"] = make_pair(3, 3); ma["stability"] = make_pair(1, 6); ma["steady"] = make_pair(2, 3); ma["steep"] = make_pair(1, 3); ma["step"] = make_pair(3, 4); ma["steps"] = make_pair(3, 3); ma["stop"] = make_pair(3, 11); ma["stopped"] = make_pair(3, 5); ma["strengthen"] = make_pair(1, 3); ma["studying"] = make_pair(3, 3); ma["success"] = make_pair(3, 4); ma["summer"] = make_pair(3, 3); ma["sure"] = make_pair(3, 6); ma["survey"] = make_pair(1, 3); ma["technology"] = make_pair(3, 5); ma["television"] = make_pair(3, 3); ma["tell"] = make_pair(3, 3); ma["test"] = make_pair(3, 4); ma["textile"] = make_pair(3, 6); ma["textiles"] = make_pair(3, 3); ma["thus"] = make_pair(2, 3); ma["tied"] = make_pair(1, 3); ma["totalled"] = make_pair(2, 6); ma["tough"] = make_pair(3, 3); ma["trader"] = make_pair(1, 3); ma["transactions"] = make_pair(1, 6); ma["tried"] = make_pair(3, 3); ma["trip"] = make_pair(3, 8); ma["try"] = make_pair(3, 11); ma["trying"] = make_pair(3, 4); ma["unadjusted"] = make_pair(2, 5); ma["unchanged"] = make_pair(2, 6); ma["underlying"] = make_pair(1, 4); ma["unfair"] = make_pair(3, 7); ma["urge"] = make_pair(3, 3); ma["violating"] = make_pair(3, 3); ma["visit"] = make_pair(3, 14); ma["volume"] = make_pair(3, 3); ma["voted"] = make_pair(2, 3); ma["ward"] = make_pair(3, 3); ma["warned"] = make_pair(3, 3); ma["warning"] = make_pair(3, 3); ma["weakened"] = make_pair(3, 3); ma["willing"] = make_pair(3, 4); ma["yield"] = make_pair(1, 3); ma["yields"] = make_pair(1, 3); string s; int a[4]; memset(a, 0, sizeof a); while (cin >> s) { if (ma.find(s) != ma.end()) { a[ma[s].first] += ma[s].second; } } int m = max(max(a[1], a[2]), a[3]); if (a[1] == m) { cout << 1 << endl; } else { if (a[2] == m) { cout << 2 << endl; } else { cout << 3 << endl; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; char ch[4][4]; int pic[4][4]; int cal() { int i, j; for (i = 0; i < 4; i++) { for (j = 0; j <= 1; j++) { if (ch[i][j] == 'x' && ch[i][j + 1] == 'x' && ch[i][j + 2] == 'x') { return 1; } } } for (j = 0; j <= 1; j++) { for (i = 0; i < 4; i++) { if (ch[j][i] == 'x' && ch[j + 1][i] == 'x' && ch[j + 2][i] == 'x') { return 1; } } } for (i = 0; i <= 1; i++) { for (j = 0; j <= 1; j++) { if (ch[i][j] == 'x' && ch[i + 1][j + 1] == 'x' && ch[i + 2][j + 2] == 'x') { return 1; } } } for (i = 0; i <= 1; i++) { for (j = 2; j < 4; j++) { if (ch[i][j] == 'x' && ch[i + 1][j - 1] == 'x' && ch[i + 2][j - 2] == 'x') { return 1; } } } return 0; } int main() { for (int i = 0; i < 4; i++) cin >> ch[i]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (ch[i][j] == '.') { ch[i][j] = 'x'; if (cal() == 1) { cout << "Yes" << endl; return 0; } ch[i][j] = '.'; } } } cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int N, M, K; cin >> N >> M >> K; long long int a, b, c, i, j, k, start, end, mid; if ((2 * N * M) % K != 0) { cout << "NO"; return 0; } c = N * M * 2; c /= K; for (i = 1; i * i <= N; i++) { if (N % i == 0) { if (c % i == 0) { a = c / i; if (a <= M) { cout << "YES\n"; cout << "0 0\n"; cout << i << " 0\n"; cout << "0 " << a << "\n"; return 0; } } a = N / i; if (c % a == 0) { b = c / a; if (b <= M) { cout << "YES\n"; cout << "0 0\n"; cout << a << " 0\n"; cout << "0 " << b << "\n"; return 0; } } } } for (i = 1; i * i <= M; i++) { if (M % i == 0) { if (c % i == 0) { a = c / i; if (a <= N) { cout << "YES\n"; cout << "0 0\n"; cout << a << " 0\n"; cout << "0 " << i << "\n"; return 0; } } a = M / i; if (c % a == 0) { b = c / a; if (b <= N) { cout << "YES\n"; cout << "0 0\n"; cout << b << " 0\n"; cout << "0 " << a << "\n"; return 0; } } } } cout << "NO"; return 0; }
4
#include <cstdio> #pragma warning(disable : 4996) int N, Q, v[1048588], vx[1048588], vy[1048588], pc[75]; char s[1048588], qs[29]; int main() { scanf("%d%d%s", &N, &Q, s); for (int i = 0; i < 1 << N; ++i) { v[i] = s[i] - '0'; vx[i] = v[i]; vy[i] = v[i]; } for (int i = 0; i < N; ++i) { for (int j = 0; j < 1 << N; ++j) { if ((j >> i) & 1) { vx[j] += vx[j - (1 << i)]; vy[j - (1 << i)] += vy[j]; } } } pc[0] = 1; for (int i = 1; i < 1 << (N / 3); i++) { pc[i] = pc[i >> 1] ^ (i & 1); } for (int i = 0; i < Q; ++i) { scanf("%s", qs); int cx = 0, cy = 0, cz = 0, sx = 0, sy = 0, sz = 0; for (int j = 0; j < N; ++j) { if (qs[N - j - 1] == '0') ++cx, sx |= 1 << j; if (qs[N - j - 1] == '1') ++cy, sy |= 1 << j; if (qs[N - j - 1] == '?') ++cz, sz |= 1 << j; } int ret = 0; if (3 * cz <= N) { for (int j = sz; j >= 0; --j) { j &= sz; ret += v[j + sy]; } } else if (3 * cy <= N) { for (int j = sy, k = 0; j >= 0; --j, ++k) { j &= sy; if(pc[k]) ret += vx[j + sz]; else ret -= vx[j + sz]; } } else { for (int j = sx, k = 0; j >= 0; --j, ++k) { j &= sx; if (pc[k]) ret += vy[sx + sy - j]; else ret -= vy[sx + sy - j]; } } printf("%d\n", ret); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int r[25][6] = { {0, 0, 0, 0, 0, 0}, {1, 2, 3, 4, 5, 6}, {1, 2, 4, 5, 6, 3}, {1, 2, 5, 6, 3, 4}, {1, 2, 6, 3, 4, 5}, {2, 1, 5, 4, 3, 6}, {2, 1, 4, 3, 6, 5}, {2, 1, 3, 6, 5, 4}, {2, 1, 6, 5, 4, 3}, {5, 3, 1, 4, 2, 6}, {5, 3, 4, 2, 6, 1}, {5, 3, 2, 6, 1, 4}, {5, 3, 6, 1, 4, 2}, {3, 5, 2, 4, 1, 6}, {3, 5, 4, 1, 6, 2}, {3, 5, 1, 6, 2, 4}, {3, 5, 6, 2, 4, 1}, {4, 6, 3, 2, 5, 1}, {4, 6, 2, 5, 1, 3}, {4, 6, 5, 1, 3, 2}, {4, 6, 1, 3, 2, 5}, {6, 4, 1, 5, 2, 3}, {6, 4, 5, 2, 3, 1}, {6, 4, 2, 3, 1, 5}, {6, 4, 3, 1, 5, 2}, }; struct Node { int a[7]; } node[500], now; char ch[8]; int a[8]; int ans; int cnt; int get(char c) { if (c == 'R') return 1; if (c == 'O') return 2; if (c == 'Y') return 3; if (c == 'G') return 4; if (c == 'B') return 5; return 6; } bool slove2(int k) { for (int i = 1; i <= 24; i++) { int f = 0; for (int j = 0; j < 6; j++) { if (node[k].a[r[i][j]] != now.a[j + 1]) { f = 1; break; } } if (f == 0) return true; } return false; } bool judge() { for (int i = 1; i <= ans; i++) { if (slove2(i)) return false; } for (int i = 1; i <= 6; i++) { node[ans + 1].a[i] = now.a[i]; } return true; } void slove() { int b[7]; for (int i = 1; i <= 6; i++) { b[i] = i; } do { for (int i = 1; i <= 6; i++) { now.a[b[i]] = a[i]; } if (judge()) ans++; } while (next_permutation(b + 1, b + 7)); } int main() { scanf("%s", ch); for (int i = 0; i <= 5; i++) { a[i + 1] = get(ch[i]); } ans = 0; slove(); printf("%d\n", ans); }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, mn, mx, x; bool I = 0; scanf("%d %d %d %d", &n, &m, &mn, &mx); for (int i = 0; i < m; i++) { cin >> x; if (x < mn || x > mx) { printf("Incorrect"); return 0; } if (x == mn || x == mx) I = 1; } if (I || n - m > 1) printf("Correct"); else printf("Incorrect"); }
1
#include <bits/stdc++.h> using namespace std; int a[200100], n, m, s, d, run[200100], jump[200100], l = 0; int main() { scanf("%d%d%d%d", &n, &m, &s, &d); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); if (a[0] - 1 < s) { printf("IMPOSSIBLE"); return 0; } for (int i = 0; i < n; i++) { int t = i; while (i < n - 1 && a[i + 1] - a[i] - 2 < s) i++; jump[l] = a[i] - a[t] + 2; run[l] = (i + 1 < n) ? (a[i + 1] - a[i] - 2) : (m - a[i] - 1); if (jump[l] > d) { printf("IMPOSSIBLE"); return 0; } l++; } printf("RUN %d\n", a[0] - 1); for (int i = 0; i < l; i++) { printf("JUMP %d\n", jump[i]); if (run[i]) printf("RUN %d\n", run[i]); } }
4
#include <iostream> using namespace std; int main() { int x; unsigned long long int ret=1; cin >> x; for(int i=1;i<=x;i++) ret*=i; if(!x)ret=0; cout << ret << endl; }
0
#include <bits/stdc++.h> using namespace std; const long long M = 1000000009, N = 300010; long long F1[N], F2[N], f1[N], f2[N], Fib[N], fib[N], st[N * 4], lazy[N * 4][3], l, r, n, m, flag, a[N], s[N]; inline long long P(long long x) { return x >= M ? x % M : x; } long long get(long long x, long long y, long long z) { return x * f1[z] + y * f2[z]; } void pushdown(long long ind, long long be, long long en) { long long mid = (be + en) >> 1; st[ind << 1] = P(st[ind << 1] + P(F1[mid - be + 1] * lazy[ind][1]) + P(F2[mid - be + 1] * lazy[ind][2])); st[ind << 1 | 1] = P(st[ind << 1 | 1] + P(P(F1[en - be + 1] - F1[mid - be + 1] + M) * lazy[ind][1]) + P(P(F2[en - be + 1] - F2[mid - be + 1] + M) * lazy[ind][2])); lazy[ind << 1][1] = P(lazy[ind << 1][1] + lazy[ind][1]); lazy[ind << 1][2] = P(lazy[ind << 1][2] + lazy[ind][2]); lazy[ind << 1 | 1][1] = P(lazy[ind << 1 | 1][1] + get(lazy[ind][1], lazy[ind][2], mid - be + 2)); lazy[ind << 1 | 1][2] = P(lazy[ind << 1 | 1][2] + get(lazy[ind][1], lazy[ind][2], mid - be + 3)); lazy[ind][1] = lazy[ind][2] = 0; } void update(long long ind, long long be, long long en) { if (l <= be && en <= r) { st[ind] = P(st[ind] + P(Fib[en - l + 1] - Fib[be - l] + M)); lazy[ind][1] = P(lazy[ind][1] + fib[be - l + 1]); lazy[ind][2] = P(lazy[ind][2] + fib[be - l + 2]); return; } if (be < en) pushdown(ind, be, en); long long mid = (be + en) >> 1; if (l <= mid) update(ind << 1, be, mid); if (r >= mid + 1) update(ind << 1 | 1, mid + 1, en); st[ind] = P(st[ind << 1] + st[ind << 1 | 1]); } long long ask(long long ind, long long be, long long en) { if (l <= be && en <= r) return st[ind]; if (be < en) pushdown(ind, be, en); long long mid = (be + en) >> 1, res = 0; if (l <= mid) res = P(res + ask(ind << 1, be, mid)); if (r >= mid + 1) res = P(res + ask(ind << 1 | 1, mid + 1, en)); st[ind] = P(st[ind << 1] + st[ind << 1 | 1]); return res; } void prework() { f1[1] = 1; for (long long i = 3; i <= 300000; i++) f1[i] = P(f1[i - 1] + f1[i - 2]); f2[2] = 1; for (long long i = 3; i <= 300000; i++) f2[i] = P(f2[i - 1] + f2[i - 2]); for (int i = 1; i <= 300000; i++) fib[i] = P(f1[i] + f2[i]); for (long long i = 1; i <= 300000; i++) F1[i] = P(f1[i] + F1[i - 1]); for (long long i = 1; i <= 300000; i++) F2[i] = P(f2[i] + F2[i - 1]); for (long long i = 1; i <= 300000; i++) Fib[i] = P(F1[i] + F2[i]); } int main() { prework(); scanf("%lld%lld", &n, &m); for (long long i = 1; i <= n; i++) { scanf("%lld", &a[i]); s[i] = P(s[i - 1] + a[i]); } for (long long i = 1; i <= m; i++) { scanf("%lld%lld%lld", &flag, &l, &r); if (flag == 1) update(1, 1, n); else printf("%lld\n", P(P(s[r] - s[l - 1] + M) + ask(1, 1, n))); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1 * 1000 + 10; int n, a[maxn], p; vector<pair<int, int>> vec, ans; vector<pair<int, bool>> idk; vector<int> loc; void solve(int l, int r) { if (r - l == 1) return; int mid = (l + r) / 2; solve(l, mid); solve(mid, r); loc.clear(); idk.clear(); for (int i = l; i < mid; i++) { loc.push_back(vec[i].second); idk.push_back({vec[i].second, 0}); } sort((loc).begin(), (loc).end()); for (int i = mid; i < r; i++) idk.push_back({vec[i].second, 1}); sort((idk).begin(), (idk).end()); p = 0; for (int i = 0; i < (((int)idk.size())); i++) { if (idk[i].second) { for (int j = (((int)loc.size())) - 1; j >= p; j--) ans.push_back({idk[i].first + 1, loc[j] + 1}); } else p++; } } int main() { ios::sync_with_stdio(0); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); vec.push_back({a[i], i}); } sort((vec).begin(), (vec).end()); solve(0, n); printf("%d\n", (((int)ans.size()))); for (auto &w : ans) printf("%d %d\n", w.first, w.second); return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[200001], l[200001], r[200001], ans[200001]; int main() { int n, x; while (cin >> n) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) { x = i - 1; while (x >= 0 && a[x] >= a[i]) x = l[x]; l[i] = x; } for (int i = n - 1; i >= 0; i--) { x = i + 1; while (x < n && a[x] >= a[i]) x = r[x]; r[i] = x; } memset(ans, 0, sizeof(ans)); for (int i = 0; i < n; i++) { x = r[i] - l[i] - 1; ans[x] = max(ans[x], a[i]); } for (int i = n; i >= 1; i--) ans[i] = max(ans[i], ans[i + 1]); for (int i = 1; i <= n; i++) if (i == 1) printf("%d", ans[i]); else printf(" %d", ans[i]); cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, s = 0, i = 1, k = 0, m, p = 0; cin >> n; cout << (n / 2) + 1; return 0; }
1
#include <bits/stdc++.h> using namespace std; int A[200200]; deque<int> DQ; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(); int N; cin >> N; for (int I = 0; I < N; I++) { cin >> A[I]; } if (N & 1) { DQ.push_front(A[N / 2]); } for (int I = N / 2 - 1; I >= 0; I--) { if (I & 1) { DQ.push_front(A[I]); DQ.push_back(A[N - I - 1]); } else { DQ.push_back(A[I]); DQ.push_front(A[N - I - 1]); } } for (int I = 0; I < N; I++) { cout << DQ[I] << ' '; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, games; cin >> n >> m; int c[n]; int a[m]; for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 0; i < m; i++) cin >> a[i]; i = 0; j = 0; games = 0; while (i < n && j < m) { if (c[i] <= a[j]) { games++; i++; j++; } else i++; } cout << games; }
1
#include <bits/stdc++.h> using namespace std; struct yy { int id, val; } f[8000]; int a[8000], p[8000][2], d[8000]; bool b[8000]; bool cmp(yy x, yy y) { return x.val > y.val; } int main() { int i, j, n, m, t, x; bool tm; scanf("%d%d", &n, &m); for (i = 1; i <= 2 * n; ++i) { scanf("%d", &f[i].val); f[i].id = i; a[i] = f[i].val; } sort(f + 1, f + 1 + 2 * n, cmp); for (i = 1; i <= m; ++i) { scanf("%d%d", &p[i][0], &p[i][1]); d[p[i][0]] = p[i][1]; d[p[i][1]] = p[i][0]; } scanf("%d", &t); if (t == 1) { for (i = 1; i <= m; ++i) { if (a[p[i][0]] > a[p[i][1]]) { printf("%d\n", p[i][0]); fflush(stdout); b[p[i][0]] = 1; } else { printf("%d\n", p[i][1]); fflush(stdout); b[p[i][1]] = 1; } scanf("%d", &x); b[x] = 1; } for (i = 1; i <= 2 * n; ++i) { if (!b[f[i].id]) { printf("%d\n", f[i].id); fflush(stdout); b[f[i].id] = 1; scanf("%d", &x); b[x] = 1; } } } else { for (i = 1; i <= n; ++i) { scanf("%d", &x); b[x] = 1; if (d[x] && !b[d[x]]) { printf("%d\n", d[x]); fflush(stdout); b[d[x]] = 1; } else { tm = 0; for (j = 1; j <= m; ++j) if (!b[p[j][0]]) { if (a[p[j][0]] > a[p[j][1]]) { printf("%d\n", p[j][0]); fflush(stdout); b[p[j][0]] = 1; tm = 1; break; } else { printf("%d\n", p[j][1]); fflush(stdout); b[p[j][1]] = 1; tm = 1; break; } } if (!tm) for (j = 1; j <= 2 * n; ++j) if (!b[f[j].id]) { printf("%d\n", f[j].id); fflush(stdout); b[f[j].id] = 1; break; } } } } return 0; }
3
#include <iostream> using namespace std; int n,m,a,b,c,dp[(1<<12)],L; int main(void){ for(int i=1;i<(1<<12);i++)dp[i]=1e9; cin>>n>>m; for(int i=0;i<m;i++){ cin>>a>>b; L=0; for(int j=0;j<b;j++){ cin>>c; c--; L|=(1<<c); } for(int i=0;i<(1<<n);i++){ dp[i|L]=min(dp[i|L],dp[i]+a); //cout<<dp[i]<<" "<<a<<endl; } } if(dp[(1<<n)-1]==1e9)cout<<-1<<endl; else cout<<dp[(1<<n)-1]<<endl; }
0
#include <cstdio> int main(){ int l; while(scanf("%d", &l), l){ int cm = -1; for(int i=1;i<=12;i++){ int m, n; scanf("%d%d", &m, &n); l -= m - n; if(l <= 0 && cm < 0) cm = i; } if(cm<0)puts("NA"); else printf("%d\n", cm); } }
0
#include"bits/stdc++.h" using namespace std; int main() { string S; cin >> S; if (S.size() == 3) { reverse(S.begin(), S.end()); } cout << S << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> v[100004]; int main() { cin>>n>>m; for(int i = 0;i<n;i++){ int a, b; cin>>a>>b; v[a].push_back(b); } priority_queue<int> pq; int ans = 0; for(int i = 1; i<=m; i++){ for(int val : v[i]) pq.push(val); if(!pq.empty()){ ans += pq.top(); pq.pop(); } } cout<<ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, a; set<int> s; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a; s.insert(a); } for (auto i : s) if (i == 0) { cout << s.size() - 1; return 0; } cout << s.size(); }
1
#include <bits/stdc++.h> using namespace std; int main() { string s[100]; int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } vector<char> v; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int t = 0; for (int k = 0; k < n; k++) { if (s[k][j] == s[i][j]) { t++; } } for (int k = 0; k < m; k++) { if (s[i][k] == s[i][j]) { t++; } } if (t == 2) { v.push_back(s[i][j]); } } } int l = (int)v.size(); for (int i = 0; i < l; i++) { printf("%c", v[i]); } printf("\n"); }
2
#include <bits/stdc++.h> using namespace std; template <class T> T min3(T a, T b, T c) { return min(a, min(b, c)); }; template <class T> T max3(T a, T b, T c) { return max(a, max(b, c)); }; void openFile() { freopen("/home/khaihanhdk/MyWorkplace/cpp file/a.inp", "r", stdin); } bool con7(int h, int k) { return (k % 10 == 7 || k / 10 == 7 || h % 10 == 7 || h / 10 == 7); } bool findTime(int h, int k, int x, int y) { int l = x * y; int m = l / 60; m %= 24; h = (h - m + 24) % 24; l %= 60; if (l > k) { h = h - 1; if (h == -1) h = 23; k = 60 + k - l; } else k -= l; return con7(h, k); } int main() { int x, h, k; cin >> x; cin >> h >> k; for (int i = 0; i <= 1000000000; i++) { if (findTime(h, k, x, i)) { cout << i; return 0; } } }
1
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <cfloat> #include <ctime> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <numeric> #include <list> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define popcount __builtin_popcount #define rep(i, n) for (int i = 0; i < n; ++i) template <class T> void max_swap(T& a, const T& b) { a = max(a, b); } template <class T> void min_swap(T& a, const T& b) { a = min(a, b); } const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> pint; #include <cassert> struct Dice { int bottom, east, south; Dice(int bottom, int east, int south) : bottom(bottom), east(east), south(south) { int t[3] = { bottom, east, south }; for (int i = 0; i < 3; ++i) if (t[i] < 0 || t[i] > 6) cout << t[i] << endl; } Dice roll(int dx, int dy) { if (dy == 0) { if (dx == 1) return Dice(east, 7 - bottom, south); else if (dx == -1) return Dice(7 - east, bottom, south); else exit(1); } else if (dx == 0) { if (dy == 1) return Dice(7 - south, east, bottom); else if (dy == -1) return Dice(south, east, 7 - bottom); else exit(1); } else exit(1); } int hash() const { return bottom*100 + east*10 + south; } }; bool operator<(const Dice& a, const Dice& b) { return a.hash() < b.hash(); } struct Node { int pena, x, y; Dice dice; }; bool operator<(const Node& a, const Node& b) { return a.pena > b.pena; } int main() { int h, w, sx, sy, gx, gy; while (cin >> h >> w, h | w) { int cell[16][16]; rep (y, h) rep (x, w) cin >> cell[h-1 - y][x]; cin >> sy >> sx >> gy >> gx; sy = h-1 - sy; gy = h-1 - gy; map<Dice, int> dp[16][16]; priority_queue<Node> q; q.push((Node){ 0, sx, sy, Dice(6, 3, 2) }); dp[sy][sx][q.top().dice] = 0; while (!q.empty()) { Node cur = q.top(); q.pop(); if (cur.pena > dp[cur.y][cur.x][cur.dice]) continue; static const int dx[] = { 0, 1, 0, -1 }; static const int dy[] = { 1, 0, -1, 0 }; for (int i = 0; i < 4; ++i) { int tx = cur.x + dx[i], ty = cur.y + dy[i]; if (tx < 0 || ty < 0 || tx >= w || ty >= h) continue; Dice d = cur.dice.roll(dx[i], dy[i]); int pena = cur.pena + d.bottom * cell[ty][tx]; if (!dp[ty][tx].count(d) || pena < dp[ty][tx][d]) { dp[ty][tx][d] = pena; q.push((Node){ pena, tx, ty, d }); } } } int res = 1e9; foreach (it, dp[gy][gx]) min_swap(res, it->second); cout << res << endl; } }
0
#include <bits/stdc++.h> using namespace std; int dp[1000005]; int leftt = 100000000; int rightt = 0; int le[5005]; int ri[5005]; vector<int> ans; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; r--; le[i] = l; ri[i] = r; dp[l]++; dp[r + 1]--; } for (int i = 1; i <= 1000000; i++) { dp[i] += dp[i - 1]; if (dp[i] > 2) { cout << 0; return 0; } } for (int i = 0; i <= 1000000; i++) { if (dp[i] == 2) { leftt = min(leftt, i); rightt = max(rightt, i); } } for (int i = 0; i < n; i++) { if (le[i] <= leftt && rightt <= ri[i]) { ans.push_back(i); } } cout << ans.size() << endl; for (auto i : ans) cout << i + 1 << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int T = 1; cin >> T; while (T--) { int n, x, ans = INT_MAX, f = 0; cin >> n >> x; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] == x) f = 1; ans = min(ans, x / a[i] + (x % a[i] > 0)); } if (f) printf("%d\n", ans); else printf("%d\n", max(ans, 2)); } return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int n, m; pair<int, int> nt[100002]; bool cmp(int a, int b) { if (nt[a].first == nt[b].first) return nt[a].second < nt[b].second; return nt[a].first < nt[b].first; } int wh[100002], v[100002], lft[100002]; struct actor { int x, y, k, pi; }; actor va[100002]; bool cmp2(actor a, actor b) { return a.x < b.x; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) { cin >> nt[i].first >> nt[i].second; v[i] = i; } sort(v + 1, v + n + 1, cmp); cin >> m; for (int i = 1; i <= m; ++i) { cin >> va[i].x >> va[i].y >> va[i].k; lft[i] = va[i].k; va[i].pi = i; } sort(va + 1, va + m + 1, cmp2); set<pair<int, int> > s; int pz = 1; for (int i = 1; i <= n; ++i) { while (pz <= m && va[pz].x <= nt[v[i]].first) s.insert({va[pz].y, va[pz].pi}), ++pz; while (!s.empty() && (*s.begin()).first < nt[v[i]].first) s.erase(s.begin()); set<pair<int, int> >::iterator it = s.lower_bound({nt[v[i]].second, 0}); if (it == s.end()) { cout << "NO"; return 0; } pair<int, int> xx = *it; s.erase(xx); --lft[xx.second]; wh[v[i]] = xx.second; if (lft[xx.second]) s.insert(xx); } cout << "YES\n"; for (int i = 1; i <= n; ++i) cout << wh[i] << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, i, max; cin >> n; int A[n]; int ctr[100] = {0}; for (i = 0; i < n; i++) { cin >> A[i]; ctr[A[i] - 1]++; } max = *max_element(ctr, ctr + 100); cout << max; return 0; }
1
#include <bits/stdc++.h> int n, ans, S; char a[111111], b[111111], a2[111111], b2[111111]; short z[1111111]; void mul() { for (int i = 0; i < S; i++) z[i] *= 25; for (int i = 0; i < S; i++) z[i + 1] += z[i] / 10, z[i] %= 10; for (int i = S; z[i] > 0; i++) z[i + 1] += z[i] / 10, z[i] %= 10, S++; } void f() { z[0] = 6; z[1] = 2; S = 2; for (int i = 0; i < n; i++) { mul(); } for (int i = S - 1; i >= 0; i--) printf("%d", z[i]); } int main() { scanf("%d ", &n); scanf("%s %s", a, b); int cnt = 0; int l = 0, r = 0, sz = 0; while (l < n || r < n) { if (a[l] != b[r]) { cnt++; if (cnt == 1) { a2[sz++] = a[l]; l++; } else if (cnt == 2) { a2[sz++] = b[r]; r++; } else { break; } } else { a2[sz++] = a[l]; l++; r++; } } if (cnt == 0) { f(); return 0; } else if (cnt == 2) { ans++; } l = r = sz = cnt = 0; while (l <= n && r <= n) { if (a[l] != b[r]) { cnt++; if (cnt == 1) { b2[sz++] = b[r]; r++; } else if (cnt == 2) { b2[sz++] = a[l]; l++; } else { break; } } else { b2[sz++] = a[l]; l++; r++; } } if (cnt == 3) { printf("%d", ans); return 0; } if (ans == 0 && cnt == 2) { puts("1"); return 0; } for (int i = 0; i < sz; i++) if (a2[i] != b2[i]) { ans++; break; } printf("%d", ans); }
5
#include <bits/stdc++.h> using namespace std; long long x, y, z, w; int main() { cin >> x >> y >> z >> w; cout << (max(x, z) + y + w) * 2 + 4 << endl; return 0; }
1
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) #define repb(i,a,b) for(int i=a;i>=b;i--) #define all(a) a.begin(),a.end() #define o(a) cout<<a<<endl #define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; int calc(int n){ int res = 0; for(int i = 1; i*i <= n; i++){ if(n % i == 0){ res += i; if(i*i != n) res += n / i; } } return res - n; } signed main(){ int n; while(cin >> n, n){ int sum = calc(n); //o(sum); if(sum == n) cout << "perfect number" << endl; if(sum < n) cout << "deficient number" << endl; if(sum > n) cout << "abundant number" << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k, l, s, m, a, b, c; cin >> n; string s1, s2; cin >> s1; cin >> s2; int p, q; n = s1.size(); n = s2.size(); int cot = 0; for (int i = 0; i < s1.size(); i++) { if (s1[i] > s2[i]) { a = abs(s2[i] - s1[i]); b = s2[i] + 10; c = b - s1[i]; if (c < a) { cot = cot + c; } else { cot = cot + a; } } else { k = abs(s1[i] - s2[i]); l = s1[i] + 10; m = l - s2[i]; if (m < k) { cot = cot + m; } else { cot = cot + k; } } } cout << cot << endl; }
1
#include <bits/stdc++.h> using namespace std; struct state { int x, y, cost; }; int sx, sy, fs, fy, n, r, a, b; map<pair<int, int>, int> mp, vis; queue<state> q; int dxx[] = {1, 0, -1, 0, -1, 1, 1, -1}; int dyy[] = {0, 1, 0, -1, -1, 1, -1, 1}; bool check(int x, int y) { return x > 0 && y > 0; } int bfs() { state s; s.x = sx; s.y = sy; s.cost = 0; q.push(s); while (!q.empty()) { state cur = q.front(); int ox = cur.x, oy = cur.y, cc = cur.cost; q.pop(); if (cur.x == fs && cur.y == fy) return cur.cost; if (vis.count(make_pair(cur.x, cur.y))) continue; vis[make_pair(cur.x, cur.y)] = 1; for (int i = 0; i < 8; i++) { int nx = ox + dxx[i], ny = oy + dyy[i]; if (check(nx, ny) && vis.count(make_pair(nx, ny)) == 0 && mp.count(make_pair(nx, ny))) { state next; next.x = nx; next.y = ny; next.cost = cur.cost + 1; q.push(next); } } } return -1; } int main() { cin >> sx >> sy >> fs >> fy >> n; for (int i = 0; i < n; i++) { cin >> r >> a >> b; for (int j = a; j <= b; j++) mp[make_pair(r, j)] = 1; } printf("%d\n", bfs()); }
3