solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; vector<int> a(6); int sum = 0; int main() { for (int i = 0; i < 6; i++) { cin >> a[i]; sum += a[i]; } for (int i = 0; i < 6; i++) for (int j = 0; j < 6; j++) if (i != j) for (int k = 0; k < 6; k++) if (k != i && k != j) if ((a[i] + a[j] + a[k]) * 2 == sum) { cout << "YES" << endl; return 0; } cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n; bitset<500069> a; struct val { long long mx = 0, c[2] = {0, 0}; bool ft[2] = {1, 1}, jd = 1; } v0; struct segtree { long long l, r; val ans[2]; bool lz; segtree *p[2]; val mrg(val x, val y) { if (x.mx && y.mx) { long long w; val z; z.mx = max(x.mx, y.mx); z.c[0] = x.c[0]; z.c[1] = y.c[1]; z.ft[0] = x.ft[0]; z.ft[1] = y.ft[1]; z.jd = 0; if (x.ft[1] || y.ft[0]) { w = x.c[1] + y.c[0]; z.mx = max(z.mx, w); if (x.jd) { z.c[0] = w; } if (y.jd) { z.c[1] = w; } z.jd = x.jd && y.jd; } return z; } else if (!x.mx) { return y; } else { return x; } } void bd(long long lh, long long rh) { l = lh; r = rh; lz = 0; if (l == r) { long long ii, jj; for (ii = 0; ii < 2; ii++) { ans[ii].mx = 1; for (jj = 0; jj < 2; jj++) { ans[ii].c[jj] = 1; } ans[ii].ft[!a[l] ^ ii] = 0; } } else { long long ii, md = (l + r) / 2; for (ii = 0; ii < 2; ii++) { p[ii] = new segtree; p[ii]->bd(!ii ? l : md + 1, !ii ? md : r); } for (ii = 0; ii < 2; ii++) { ans[ii] = mrg(p[0]->ans[ii], p[1]->ans[ii]); } } } void blc() { if (lz) { long long ii; for (ii = 0; ii < 2; ii++) { swap(p[ii]->ans[0], p[ii]->ans[1]); p[ii]->lz ^= 1; } lz = 0; } } val qr(long long lh, long long rh) { if (l > rh || r < lh) { return v0; } else if (l >= lh && r <= rh) { swap(ans[0], ans[1]); lz ^= 1; return ans[0]; } else { long long ii; val tmp; blc(); tmp = mrg(p[0]->qr(lh, rh), p[1]->qr(lh, rh)); for (ii = 0; ii < 2; ii++) { ans[ii] = mrg(p[0]->ans[ii], p[1]->ans[ii]); } return tmp; } } } sg; int main() { long long t, rr, i, k, l; char ch; scanf("%lld%lld", &n, &t); for (i = 1; i <= n; i++) { scanf(" %c", &ch); a[i] = ch == '>'; } sg.bd(1, n); for (rr = 0; rr < t; rr++) { scanf("%lld%lld", &k, &l); printf("%lld\n", sg.qr(k, l).mx); } }
20
#include <bits/stdc++.h> using namespace std; char s1[200005], s2[200005]; int N; long double calc(char c) { long double s = 0; for (int i = 0; i < N; i++) if (s1[i] == c) s += (N - i); long double res = 0; long double p = 0; long double sum = s; long double minus = 0; for (int i = 0; i < N; i++) { if (s2[i] == c) res += sum; if (s1[i] == c) { s -= N - i; p = (N - i) * (i + 1); minus += i + 1; } sum += s; sum += -minus; } return res; } int main() { scanf("%d", &N); scanf("%s%s", s1, s2); long double res = 0; for (char c = 'A'; c <= 'Z'; c++) res += calc(c); long long cnt = 0; for (long long i = N; i > 0; i--) cnt += (i * 2 - 1) * (N - i + 1); long double r = res; r /= cnt; printf("%.8lf\n", r); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 26; bitset<1250507> f[N << 1][N]; bool fl[N << 1]; int n, k, g[2][N], w[N << 1], e[N]; inline void solve(int v, int k, int q) { if (v == 0) return; if (f[k][q][v] && !f[k - 1][q][v]) e[++e[0]] = k, solve(v - w[k], k - 1, q - 1); else solve(v, k - 1, q); } int main() { cin >> n; for (int i = 1; i <= 2 * n; i++) cin >> w[i], w[i]++, k += w[i]; sort(w + 1, w + 2 * n + 1), g[0][1] = w[1], g[1][n] = w[2], f[2][0][0] = 1, k -= w[1], k -= w[2]; for (int i = 3; i <= 2 * n; i++) { f[i][0][0] = 1; for (int j = 1; j < n; j++) f[i][j] = f[i - 1][j] | (f[i - 1][j - 1] << w[i]); } int v = 0; for (int i = 0; i <= k / 2; i++) if (f[2 * n][n - 1][i]) v = i; solve(v, 2 * n, n - 1); for (int i = 1; i <= e[0]; i++) fl[e[i]] = 1; fl[1] = 1; for (int i = 1; i <= 2 * n; i++) if (!fl[i]) cout << w[i] - 1 << " "; cout << endl; for (int i = 2 * n; i >= 1; i--) if (fl[i]) cout << w[i] - 1 << " "; cout << endl; return 0; }
23
#include <bits/stdc++.h> using namespace std; int n, m, ans, grid[5001][5001], cnt[5001], tmp; string s; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> s; for (int j = 1; j <= m; j++) { if (s[j - 1] == '1') grid[i][j] = grid[i][j - 1] + 1; } } for (int j = 1; j <= m; j++) { tmp = 0; for (int i = 0; i <= j; i++) cnt[i] = 0; for (int i = 1; i <= n; i++) cnt[grid[i][j]]++; for (int i = j; i >= 1; i--) { tmp += cnt[i]; ans = max(ans, tmp * i); } } cout << ans << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30); const int MX = 201; int a[MX], b[MX], c[MX], d[MX]; int dp[21][1201], n, m, mx, c0, d0; int main() { scanf("%d%d%d%d", &n, &m, &c0, &d0); a[0] = 1000, b[0] = 1; c[0] = c0; d[0] = d0; for (int i = 1; i <= m; i++) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]); for (int i = 0; i <= m; i++) { for (int j = 0; j <= a[i] / b[i]; j++) { for (int x = j * c[i]; x <= n; x++) { dp[i + 1][x] = max(dp[i + 1][x], dp[i][x - j * c[i]] + j * d[i]); } } } for (int i = 0; i <= n; i++) mx = max(mx, dp[m + 1][i]); cout << mx << endl; }
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long double a, b; cin >> a >> b; long double res = 0.5; if (b == 0) { printf("1\n"); continue; } if (4 * b < a) res += 0.5 - b / a; else res += a / 16 / b; printf("%.10lf\n", double(res)); } return 0; }
10
#include <bits/stdc++.h> using namespace std; bool a[205][205]; string f; int i, x = 101, y = 101; int main() { a[x][y] = 1; cin >> f; for (i = 0; i < f.size(); i++) { if (f[i] == 'U') { x--; if (a[x][y] == 1 || a[x - 1][y] == 1 || a[x][y - 1] == 1 || a[x][y + 1] == 1) { cout << "BUG" << endl; return 0; } } else if (f[i] == 'D') { x++; if (a[x][y] == 1 || a[x + 1][y] == 1 || a[x][y - 1] == 1 || a[x][y + 1] == 1) { cout << "BUG" << endl; return 0; } } else if (f[i] == 'L') { y--; if (a[x][y] == 1 || a[x - 1][y] == 1 || a[x][y - 1] == 1 || a[x + 1][y] == 1) { cout << "BUG" << endl; return 0; } } else if (f[i] == 'R') { y++; if (a[x][y] == 1 || a[x - 1][y] == 1 || a[x + 1][y] == 1 || a[x][y + 1] == 1) { cout << "BUG" << endl; return 0; } } a[x][y] = 1; } cout << "OK" << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; bool ok = true; long long n, m, real_n; long long a[100001]; bool in_a[200002]; int not_a[100001]; int checks = 0; unordered_set<int> dict; unordered_set<int> tried; bool reversed = false; bool exists(long long x) { unordered_set<int>::const_iterator got = dict.find(x); return (got != dict.end()); } long long mod(long long a, long long m) { long long out = a % m; if (out < 0) { return out + m; } return out; } pair<int, int> check(long long x, long long y) { checks++; long long ct = 0; long long tmp = max(x, y); x = min(x, y); y = tmp; long long d = y - x; long long cur = 0; while (exists(mod(y + d * cur, m))) { cur++; ct++; } cur = 0; while (exists(mod(x + d * cur, m))) { cur--; ct++; } cur++; if (ct == real_n) { return pair<int, int>(mod(x + d * cur, m), d); } return pair<int, int>(-1, -1); } int main() { srand(18); cin >> m >> n; real_n = n; reversed = (n > m / 2); for (long long i = 0; i < n; i++) { cin >> a[i]; if (!reversed) dict.insert(a[i]); } if (reversed) { real_n = m - n; for (int i = 0; i < n; i++) { in_a[a[i]] = true; } int tmp = 0; for (int i = 0; i < m; i++) { if (!in_a[i]) { not_a[tmp] = i; tmp++; dict.insert(i); } } } if (n == 1) { cout << a[0] << " 1"; return 0; } if (n == m - 1) { cout << (not_a[0] + 1) % m << " 1"; return 0; } if (n == m) { cout << "1 1"; return 0; } long long mx_iters = 1000000; long long r1 = 0; long long r2 = 0; for (long long z = 0; z < mx_iters; z++) { if (n > 2000 && checks > 1000) { cout << "too many checks not gonna work"; return 0; } r2++; r2 %= real_n; if (r1 == r2) continue; int x = a[r1]; int y = a[r2]; if (reversed) { x = not_a[r1]; y = not_a[r2]; } long long d = -1; d = abs(x - y); if (tried.find(d) != tried.end()) { continue; } tried.insert(d); bool prune = false; for (int zz = 0; zz < 30; zz++) { long long k = rand() % (real_n / 2); long long k2 = rand() % (real_n / 2); if (!exists(mod(x + d * k, m)) && !exists(mod(x - d * k2, m))) { prune = true; break; } } if (prune) continue; pair<int, int> res = check(x, y); if (res.first != -1) { if (!reversed) { cout << res.first << " " << res.second; } else { cout << (res.first + (m - n) * d) % m << " " << res.second; } return 0; } } cout << "-1"; }
18
#include <bits/stdc++.h> using namespace std; int minn(int a, int b, int c, int d) { return min(a, min(b, min(c, d))); } int cal(int a, int b, int c, int d) { return abs(a - c) + abs(b - d); } int main() { int n, x1, y1, x2, y2; while (scanf("%d%d%d%d%d", &n, &x1, &y1, &x2, &y2) != EOF) { if (x1 == x2 && (x1 == 0 || x1 == n)) printf("%d\n", abs(y1 - y2)); else if (y1 == y2 && (y1 == 0 || y1 == n)) printf("%d\n", abs(x1 - x2)); else { printf("%d\n", minn(cal(x1, y1, 0, 0) + cal(x2, y2, 0, 0), cal(x1, y1, 0, n) + cal(x2, y2, 0, n), cal(x1, y1, n, 0) + cal(x2, y2, n, 0), cal(x1, y1, n, n) + cal(x2, y2, n, n))); } } }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string s[110]; for (long long i = 0; i < n; i++) cin >> s[i]; map<string, long long> mp; for (long long i = 0; i < n; i++) { string str; for (long long j = 0; j < n; j++) { if (s[i][j] == '0') str.push_back((char)(j + '0')); } mp[str]++; } long long S = 0, mx = 0; for (auto i : mp) { mx = max(i.second, mx); S += i.second; } S = n - S; cout << max(S, mx) << '\n'; return 0; }
4
#include <bits/stdc++.h> const long long N = 200005; long long n, a[N], b[N], pos = -1, m; long long co(long long x) { return (!x) ? n : x; } signed main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]), b[i] = a[i]; a[0] = b[0] = b[n]; for (long long i = 1; i <= n; i++) m = std::max(m, b[i]); for (long long i = 1; i <= n; i++) if (b[i] == m && b[i - 1] != m) { pos = i; break; } if (pos == -1) { if (!m) { puts("YES"); for (long long i = 1; i <= n; i++) printf("%lld ", 1ll); puts(""); } else puts("NO"); return 0; } a[co((pos - 1 + n) % n)] += m; for (long long i = 1; i <= n - 1; i++) a[co((pos - i + n) % n)] += a[co((pos - i + 1 + n) % n)]; puts("YES"); for (long long i = 1; i <= n; i++) printf("%lld ", a[i]); return 0; }
16
#include <bits/stdc++.h> using namespace std; long long int arr[1000001]; char str[100001]; int main() { ios::sync_with_stdio(false); cin.clear(); long long int t, n, i, j = 0, ans = 0, count = 0, sum = 0, k = 0; cin >> n; for (i = 0; i < n; i++) { cin >> arr[i]; } i = n - 2; str[n - 1] = '+'; sum = arr[n - 1]; while (i >= 0) { if (sum > 0) { sum -= arr[i]; str[i] = '-'; } else { sum += arr[i]; str[i] = '+'; } i--; } for (i = 0; i < n; i++) { if (sum < 0) { if (str[i] == '+') cout << '-'; else cout << '+'; } else cout << str[i]; } }
11
#include <bits/stdc++.h> using namespace std; int t; void test_case() { int n_0, n_1, n_2; cin >> n_0 >> n_1 >> n_2; string result; if (n_1 > 0 && n_0 == 0 && n_2 == 0) { n_1++; while (n_1 > 0) { if (result.empty()) result.push_back('0'); else if (result.back() == '1') result.push_back('0'); else result.push_back('1'); n_1--; } cout << result << '\n'; return; } if (n_2 > 0) { for (int i = 0; i < n_2 + 1; i++) result.push_back('1'); } if (n_0 > 0) { if (result.back() == '1') n_1--; for (int i = 0; i < n_0 + 1; i++) result.push_back('0'); } while (n_1 > 0) { if (result.back() == '1') result.push_back('0'); else result.push_back('1'); n_1--; } cout << result << '\n'; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { test_case(); } return 0; }
7
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<ll> vl; typedef pair<ll, ll> pll; #define St first #define Nd second #define Pb push_back #define print_pair(p) "{" << (p).St << ", " << (p).Nd << "} " #define list_format(i, n) (i!=n? " ":"\n") #define endl "\n" #define sz(a) (int)a.size() #define REP(i,a,b) for(int i = a; i <= b; i++) #define REV(i,a,b) for(int i = a; i >= b; i--) #define AritSum(a,d,n) ((n)*(2*(a)+((n)-1)*(d)))/2 #define Log(base, num) log2(num)/log2(base) #define MOD 1000000007 #define INF 1e18 // ******************** Add-on ***************************** // ******************** Add-on ***************************** // ************* MAIN ******************* int A[100000]; int n, x; void solve() { cin >> n >> x; REP(i,0,n-1) cin >> A[i]; int it = 0; bool working = true; ll sum = accumulate(A, A + n, 0ll); while (working) { REP(i,0,n-1) { if (!working) break; if (A[i]%x == 0) sum += (ll)A[i] * (ll)pow(x, it), A[i] /= x; else working = false; } it++; } cout << sum << endl; } /* g++ -std=c++14 -Wall -Wextra -O2 Test.cpp -o Test ./Test < in.txt */ int main() { int _=1; cin>>_; REP(t,1,_) { // cout << "Case #" << t << ": "; solve(); // cout << ((solve())? "YES" : "NO") << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; double x[3], y[3], r[3]; const double t0 = 1e9; const double EPS = 1e-6; inline double sqr(const double x) { return x * x; } double sigma(double dx, double dy) { double ret(0), t[3] = {0}; for (int i = 0; i < 3; ++i) t[i] = sqrt(sqr(dx - x[i]) + sqr(dy - y[i])) / r[i]; for (int i = 0; i < 3; ++i) ret += sqr(t[i] - t[i == 2 ? 0 : i + 1]); return ret; } int main() { srand(69); for (int i = 0; i < 3; i++) { cin >> x[i] >> y[i] >> r[i]; } double xx = (x[0] + x[1] + x[2]) / 3, yy = (y[0] + y[1] + y[2]) / 3; double cur = 1e9; long long cnt = 0; long long picked = 0; for (double k = 1.0; k > EPS; k *= 0.9999) { cnt++; double dx = 0, dy = 0; int p = rand() % 2; int q; if (p) { q = rand() % 2; if (q) { dx = k; } else { dx = -k; } } else { q = rand() % 2; if (q) { dy = k; } else { dy = -k; } } xx += dx; yy += dy; if (sigma(xx, yy) < cur) { cur = sigma(xx, yy); } else if (exp((sigma(xx, yy) - cur) / k) <= 1) { double v = rand() / (double)RAND_MAX; if (v <= exp((sigma(xx, yy) - cur) / k)) { picked++; cur = sigma(xx, yy); } else { xx -= dx; yy -= dy; } } else { xx -= dx; yy -= dy; } } if (sigma(xx, yy) < EPS) { printf("%.10lf %.10lf\n", xx, yy); } return 0; }
18
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, n, m, x; set<long long> st; long long res[200009], pos[200009]; vector<long long> q; int main() { cin >> n; long long cnt = 0; for (i = 1; i <= 2 * n; i++) { char t; cin >> t; if (t == '+') st.insert(++cnt), q.push_back(0); else { cin >> k; pos[k] = cnt; q.push_back(k); } } for (i = 1; i <= n; i++) { auto it = st.upper_bound(pos[i]); if (it == st.begin()) return cout << "NO" << '\n', 0; it--; res[*it] = i; st.erase(it); } st.clear(); cnt = 0; for (auto to : q) { if (to == 0) { st.insert(res[++cnt]); } else { if (*st.begin() != to) return cout << "NO" << '\n', 0; st.erase(to); } } cout << "YES" << '\n'; for (i = 1; i <= n; i++) cout << res[i] << ' '; cout << '\n'; exit(0); }
9
#include<bits/stdc++.h> #define rep(i,x,y) for(auto i=(x);i<=(y);++i) #define dep(i,x,y) for(auto i=(x);i>=(y);--i) #define fr first #define sc second using namespace std; typedef long long ll; typedef pair<int,int>pii; const int N=2e5+10; const int mo=1e9+7; const ll inf=1e18; char s1[N],s2[N]; int lq[N],rq[N],a[N*4],lz[N*4]; void dw(int x,int L,int R){ int m=(L+R)>>1; if(~lz[x]){ lz[x<<1]=lz[x<<1|1]=lz[x]; a[x<<1]=lz[x]*(m-L+1); a[x<<1|1]=lz[x]*(R-m); lz[x]=-1; } } void cg(int x,int l,int r,int y,int L,int R){ if(l<=L&&r>=R){ a[x]=y*(R-L+1);lz[x]=y;return; } int m=(L+R)>>1;dw(x,L,R); if(l<=m)cg(x<<1,l,r,y,L,m); if(r>m)cg(x<<1|1,l,r,y,m+1,R); a[x]=a[x<<1]+a[x<<1|1]; } int q(int x,int l,int r,int L,int R){ if(l<=L&&r>=R)return a[x]; int m=(L+R)>>1;dw(x,L,R); return (l<=m?q(x<<1,l,r,L,m):0)+(r>m?q(x<<1|1,l,r,m+1,R):0); } void sol(){int n,m; scanf("%d%d%s%s",&n,&m,s1+1,s2+1); rep(i,1,n)cg(1,i,i,s2[i]-'0',1,n); rep(i,1,m)scanf("%d%d",&lq[i],&rq[i]); dep(i,m,1){ int x=q(1,lq[i],rq[i],1,n),y=rq[i]-lq[i]+1; if(!x||x==y)continue; if(x*2==y){ printf("NO\n");return; } cg(1,lq[i],rq[i],x*2<y?0:1,1,n); } rep(i,1,n)if(q(1,i,i,1,n)!=s1[i]-'0'){ printf("NO\n");return; } printf("YES\n"); } int main(){int t; scanf("%d",&t); rep(i,1,t)sol(); }
11
#include <bits/stdc++.h> using namespace std; int main() { int n, min, max, cur = 0; cin >> n; vector<pair<int, int>> v(n); set<int> s; s.insert(0); for (int i = 0; i < n - 1; i++) { cin >> v[i].first; } v[0].second = 0; min = v[0].second; max = v[0].second; for (int i = 0; i < n - 1; i++) { cur += v[i].first; v[i + 1].second = cur; s.insert(cur); if (cur < min) { min = cur; } if (cur > max) { max = cur; } } if (s.size() < n || max - min != n - 1) { cout << -1; return 0; } else for (int i = 0; i < n; i++) cout << v[i].second - min + 1 << " "; }
7
#include <bits/stdc++.h> using namespace std; template <typename Tp> inline void read(Tp &x) { static char c; static bool neg; x = 0, c = getchar(), neg = false; for (; !isdigit(c); c = getchar()) { if (c == '-') { neg = true; } } for (; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } if (neg) { x = -x; } } const int N = 100000 + 5; int n, m; int line[N]; int f[N], g[N]; namespace BIT { int t[N]; inline void clear() { memset(t, 0, sizeof(t)); } inline void add(int x, int val) { for (; x < N; x += ((x) & -(x))) { t[x] = std::max(t[x], val); } } inline int query(int x) { int res = 0; for (; x > 0; x -= ((x) & -(x))) { res = std::max(res, t[x]); } return res; } } // namespace BIT int main() { read(n), read(m); for (int i = 1, l, r; i <= n; ++i) { read(l), read(r); ++line[l], --line[r + 1]; } ++line[1]; for (int i = 1; i <= m; ++i) { line[i] += line[i - 1]; } for (int i = 1; i <= m; ++i) { f[i] = BIT::query(line[i]) + 1; BIT::add(line[i], f[i]); } BIT::clear(); for (int i = m; i >= 1; --i) { g[i] = BIT::query(line[i]) + 1; BIT::add(line[i], g[i]); } int ans = 0; for (int i = 1; i <= m; ++i) { ans = std::max(ans, f[i] + g[i] - 1); } printf("%d\n", ans); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { int t, sx, sy, ex, ey, ans; cin >> t; cin >> sx; cin >> sy; cin >> ex; cin >> ey; ans = 0; int q, w; q = ex - sx; w = ey - sy; char one, two; vector<char> wind; int i; char ch; ch = getchar(); for (i = 1; i <= t; i++) { char ch; ch = getchar(); if (ch == 'E') { if (q > 0) { q--; } } else if (ch == 'N') { if (w > 0) { w--; } } else if (ch == 'S') { if (w < 0) { w++; } } else if (ch == 'W') { if (q < 0) { q++; } } if ((w == 0) && (q == 0)) { cout << i; return 0; } } cout << -1; return 0; }
4
#include <bits/stdc++.h> using namespace std; void qmax(int &x, int y) { if (x < y) x = y; } void qmin(int &x, int y) { if (x > y) x = y; } inline int read() { char s; int k = 0, base = 1; while ((s = getchar()) != '-' && s != EOF && !(isdigit(s))) ; if (s == EOF) exit(0); if (s == '-') base = -1, s = getchar(); while (isdigit(s)) k = k * 10 + (s ^ '0'), s = getchar(); return k * base; } inline void write(int x) { static char cnt, num[15]; cnt = 0; if (!x) { putchar('0'); return; } for (; x; x /= 10) num[++cnt] = x % 10; for (; cnt; putchar(num[cnt--] + 48)) ; } const long long maxn = 1e5 + 100; long long n, l, w, X, Y, ans, sa, sb; int ia, ib, L, R; struct node { long long x, y; } z[maxn], a[maxn], b[maxn]; bool cmp(node aa, node bb) { return aa.x < bb.x; } bool check(node x, node y) { if (x.x > y.x) return 0; long long dd = y.x - x.x + l; if (x.x < 0) return y.x * 2 + l * 2 - dd + dd * w > 0; return x.x * 2 + dd - dd * w < 0; } int main() { n = read(); l = read(); w = read(); for (int i = 1; i <= n; i++) { X = read(); Y = read(); if (X < 0 || (X == 0 && Y == -1)) a[++ia].x = X, a[ia].y = Y, sa += (Y == 1); else b[++ib].x = X, b[ib].y = Y, sb += (Y == -1); } ans = (long long)sa * sb; sort(a + 1, a + ia + 1, cmp); R = 0; L = 1; for (int i = 1; i <= ia; i++) if (a[i].y == -1) z[++R] = a[i]; for (int i = 1; i <= ia; i++) if (a[i].y == 1) { while (L <= R && !check(a[i], z[L])) L++; ans += R - L + 1; } sort(b + 1, b + ib + 1, cmp); R = 0; L = 1; for (int i = ib; i >= 1; i--) if (b[i].y == 1) z[++R] = b[i]; for (int i = ib; i >= 1; i--) if (b[i].y == -1) { while (L <= R && !check(z[L], b[i])) L++; ans += R - L + 1; } printf("%lld\n", ans); return 0; }
17
#include <bits/stdc++.h> using namespace std; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; signed main() { map<long long, long long> m; map<long long, long long>::iterator it; long long n; cin >> n; for (long long i = 1; i <= n; i++) { long long x; cin >> x; q.push(make_pair(x, i)); } while (q.size() > 1) { long long x1 = q.top().first; long long y1 = q.top().second; q.pop(); long long x2 = q.top().first; long long y2 = q.top().second; if (x1 == x2) { q.pop(); q.push(make_pair(x1 + x2, y2)); } else m.insert(make_pair(y1, x1)); } m[q.top().second] = q.top().first; cout << m.size() << "\n"; for (it = m.begin(); it != m.end(); it++) { cout << it->second << " "; } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 128; int n, x; double p[N]; struct Mat { static const int N = ::N; double a[N][N]; Mat() {} Mat(double v) { for (int i = (0); i < (N); i++) for (int j = (0); j < (N); j++) a[i][j] = i == j ? v : 0; } void print() { for (int i = (0); i < (4); i++) { for (int j = (0); j < (4); j++) cout << a[i][j] << " "; cout << "\n"; } cout << "\n"; } Mat operator*(Mat &c) { Mat res(0); for (int i = (0); i < (N); i++) for (int j = (0); j < (N); j++) for (int k = (0); k < (N); k++) res.a[i][j] += a[i][k] * c.a[k][j]; return res; } Mat operator^(int b) const { Mat res(1); Mat a = *this; while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } void build() { for (int i = (0); i < (N); i++) for (int j = (0); j < (N); j++) a[i][j] = p[i ^ j]; } } mat; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> n >> x; for (int i = (0); i < (x + 1); i++) cin >> p[i]; mat.build(); mat = mat ^ n; double ans = 1 - mat.a[0][0]; cout << setiosflags(ios::fixed); cout << setprecision(10); cout << ans << "\n"; return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long l, r, m; cin >> l >> r >> m; for (long long a = l; a <= r; a++) { int rem = m % a; if (rem <= r - l) { if (rem - m != 0) { cout << a << " " << r << " " << r - rem << endl; break; } } if (rem >= l - r + a) { if ((m + a - rem) % a == 0) { cout << a << " " << l << " " << l + a - rem << endl; break; } } } } return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<int> all_x, all_y; map<pair<int, int>, int> all_tree; int pos_x[1000006], pos_y[1000006], value_x[1000006], value_y[1000006]; map<int, int> difference; int minus_mod(int a, int b, int m) { return (a - b + m + m) % m; } int main() { int n, m, dx, dy, i; scanf("%d", &n); scanf("%d", &m); scanf("%d", &dx); scanf("%d", &dy); for (i = 0; i < m; i++) { int x, y; scanf("%d", &x); scanf("%d", &y); all_tree[{x, y}]++; } int now_x = 0, now_y = 0; for (i = 0; i < n; i++) { pos_x[now_x] = i; pos_y[now_y] = i; value_x[i] = now_x; value_y[i] = now_y; now_x += dx; now_x %= n; now_y += dy; now_y %= n; } map<pair<int, int>, int>::iterator it; it = all_tree.begin(); while (it != all_tree.end()) { difference[minus_mod(pos_x[(it->first).first], pos_y[(it->first).second], n)] += (it->second); it++; } int maximum_dif, maximum = -1; map<int, int>::iterator p; p = difference.begin(); while (p != difference.end()) { if (maximum < p->second) { maximum_dif = p->first; maximum = p->second; } p++; } for (i = 0; i < n; i++) { if (all_tree[{value_x[i], value_y[minus_mod(i, maximum_dif, n)]}]) { printf("%d %d\n", value_x[i], value_y[minus_mod(i, maximum_dif, n)]); return 0; } } return 0; }
12
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long inf = 0x3f3f3f3f; const long long nax = 0; int n; int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); cin >> n; vector<long long> f(65); f[1] = 0; f[2] = 2; for (int i = 3; i <= n; ++i) { f[i] = 2 * f[i - 2]; } cout << f[n] << '\n'; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long a1, a2, b1, b2, c1, c2; cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2; if (a1 * (b2 - c2) + b1 * (c2 - a2) + c1 * (a2 - b2) == 0) { cout << "No"; return 0; } else if (((a1 - b1) * (a1 - b1) + (a2 - b2) * (a2 - b2)) == ((c1 - b1) * (c1 - b1) + (c2 - b2) * (c2 - b2))) { cout << "Yes"; return 0; } cout << "No"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long int hh, mm; cin >> hh >> mm; long long int h, d, c, n; cin >> h >> d >> c >> n; if (hh >= 20) cout << (h / n + ((h % n > 0) ? 1 : 0)) * c * 0.8 << '\n'; else { cout << min((h / n + ((h % n > 0) ? 1 : 0)) * c * 1.0, ((h + ((20 - hh) * 60 - mm) * d) / n + (((h + ((20 - hh) * 60 - mm) * d) % n > 0) ? 1 : 0)) * c * 0.8) << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int l[4]; for (int i = 0; i < 4; ++i) cin >> l[i]; bool t, s, i; sort(l, l + 4); if ((l[0] + l[1] > l[2] && l[1] + l[2] > l[0] && l[0] + l[2] > l[1]) || (l[1] + l[2] > l[3] && l[2] + l[3] > l[1] && l[1] + l[3] > l[2])) cout << "TRIANGLE" << endl; else if ((l[0] + l[1] == l[2] && l[1] + l[2] > l[0] && l[0] + l[2] > l[1]) || (l[0] + l[1] > l[2] && l[1] + l[2] == l[0] && l[0] + l[2] > l[1]) || (l[0] + l[1] > l[2] && l[1] + l[2] > l[0] && l[0] + l[2] == l[1]) || (l[1] + l[2] == l[3] && l[2] + l[3] > l[1] && l[1] + l[3] > l[2]) || (l[1] + l[2] > l[3] && l[2] + l[3] == l[1] && l[1] + l[3] > l[2]) || (l[1] + l[2] > l[3] && l[2] + l[3] > l[1] && l[1] + l[3] == l[2])) cout << "SEGMENT" << endl; else cout << "IMPOSSIBLE" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> void printRange(T &x) { for (auto y : x) cout << y << ' '; cout << endl; } const int maxn = 7e4 + 10; int n; struct S { int len; long long x; bool operator<(const S &rhs) const { if (len < rhs.len) return true; if (rhs.len < len) return false; return x < rhs.x; } bool operator>(const S &rhs) const { return rhs < *this; } bool operator<=(const S &rhs) const { return !(rhs < *this); } bool operator>=(const S &rhs) const { return !(*this < rhs); } friend ostream &operator<<(ostream &os, const S &s) { long long ub = 9; int rghm = 1; while (s.x > ub) { rghm++; ub *= 10l; ub += 9l; } for (int(_) = (1); (_) <= (s.len - rghm); (_)++) os << 0; os << s.x; return os; } }; map<S, set<int>> buckets; S mn[maxn]; long long sub(long long x, int i, int len) { for (int(_) = (1); (_) <= (9 - i - len); (_)++) x /= 10; long long md = 1; for (int(_) = (1); (_) <= (len); (_)++) md *= 10; return x % md; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int(i) = (1); (i) <= (n); (i)++) { long long x; cin >> x; for (int(len) = (1); (len) <= (9); (len)++) { for (int j = 0; j + len - 1 < 9; j++) { buckets[{len, sub(x, j, len)}].insert(i); } } mn[i] = {100, 0}; } for (auto p : buckets) { if (p.second.size() == 1) { mn[*p.second.begin()] = min(mn[*p.second.begin()], p.first); } } for (int(i) = (1); (i) <= (n); (i)++) cout << mn[i] << endl; }
8
#include <bits/stdc++.h> using namespace std; set<long long int> t; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { int g = 0, s = 0, b; for (int j = 1; j <= m; j++) { char x; cin >> x; x == 'G' ? g = j : x == 'S' ? s = j : b = 0; } if (g > s) { cout << "-1"; return 0; } else if (s - g > 0) t.insert(s - g); } cout << t.size(); return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> struct Matrix { int r, c; T a[4][4]; Matrix() { memset(a, 0, sizeof a); } Matrix(int si) { memset(a, 0, sizeof a); r = c = si; for (int i = 0; i < si; i++) a[i][i] = 1; } Matrix operator*(const Matrix &rhs) { Matrix<T> ret; ret.r = r; ret.c = rhs.c; for (int k = 0; k < c; ++k) for (int i = 0; i < ret.r; ++i) for (int j = 0; j < ret.c; ++j) ret.a[i][j] = (ret.a[i][j] + (long long)a[i][k] * rhs.a[k][j]) % 1000000007; return ret; } Matrix operator^(long long b) { Matrix<T> ret, base = *this; int setret = 0; while (b) { if (b & 1) ret = (setret ? ret * base : base), setret = 1; base = base * base; b >>= 1; } return ret; } }; int Next[1000000]; int KMP(string A, string B) { int l = A.length(); A = A + "#" + B; Next[0] = -1; int x = -1, ret = 0; for (int i = 1; i < A.length(); ++i) { while (x >= 0 && A[x + 1] != A[i]) x = Next[x]; if (A[x + 1] == A[i]) x++; Next[i] = x; if (Next[i] == l - 1) ret++; } return ret; } int main() { Matrix<int> M; string fs[30]; M.r = M.c = 4; M.a[0][1] = M.a[0][2] = M.a[1][0] = M.a[1][1] = M.a[2][3] = M.a[3][2] = 1; fs[1] = "a"; fs[2] = "b"; for (int i = 3; i < 30; ++i) fs[i] = fs[i - 1] + fs[i - 2]; string instr; long long k; int m; cin >> k >> m; while (m--) { cin >> instr; if (k < 30) { cout << KMP(instr, fs[k]) << endl; continue; } string A, B; int I; for (int i = 1; i < 30; i++) { if (fs[i].length() >= instr.length()) { B = fs[i + 1]; A = fs[i]; I = i; break; } } long long CA = KMP(instr, A); long long CB = KMP(instr, B); long long CBA = KMP(instr, B + A) - CB - CA; long long CAB = KMP(instr, A + B) - CA - CB; long long CBB = KMP(instr, B + B) - CB - CB; Matrix<int> cnt; cnt.r = 1; cnt.c = 4; cnt.a[0][1] = 1; cnt = cnt * (M ^ (k - I - 1)); long long ans = (cnt.a[0][0] * (CBA + CA) + cnt.a[0][1] * CB + cnt.a[0][2] * CAB + cnt.a[0][3] * CBB) % 1000000007; cout << ans << endl; } return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, x, y; cin >> n >> m; x = n % m; y = n / m; long long a1 = ((y * (y - 1) / 2 * (m - x) + y * (y + 1) / 2 * x)), a2 = ((n - m) * (n - m + 1) / 2); cout << a1 << " " << a2 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; string s, r1, r2; bool broj() { int i; if (s == "") return false; if (s.size() == 1) { if (s[0] >= '0' && s[0] <= '9') { return true; } else { return false; } } if (s[0] >= '1' && s[0] <= '9') { for (i = 1; i < s.size(); i++) if (!(s[i] >= '0' && s[i] <= '9')) return false; return true; } return false; } char a[333555]; int main() { int i, n; scanf("%s", &a); r1 = ""; r2 = ""; s = ""; n = strlen(a); a[n] = ';'; for (i = 0; i <= n; i++) { if (a[i] == ';' || a[i] == ',') { if (broj()) { r1 += s; r1 += ','; } else { r2 += s; r2 += ','; } s = ""; } else { s += a[i]; } } if (r1 == "") { printf("-\n"); } else { printf("\""); for (i = 0; i < r1.size() - 1; i++) printf("%c", r1[i]); printf("\"\n"); } if (r2 == "") { printf("-\n"); } else { printf("\""); for (i = 0; i < r2.size() - 1; i++) printf("%c", r2[i]); printf("\"\n"); } return 0; }
8
#include <bits/stdc++.h> using namespace std; template <typename T> T getint() { T x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ' '); if (ch == '-') p = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * p; } template <typename T1, typename T2> bool umin(T1 &x, const T2 &y) { if (x > y) return x = y, true; return false; } template <typename T1, typename T2> bool umax(T1 &x, const T2 &y) { if (x < y) return x = y, true; return false; } const int maxn = 2e3 + 10; const int maxe = 4e6; const int inf = (int)1e9 + 5; const int mod = (int)1e9 + 7; const long long llinf = (long long)1e18 + 5; const long double pi = acos(-1.0); int n, m, c; vector<int> g[maxn]; int x[maxe]; int tin[maxn], fup[maxn]; int u[maxn]; bool bridge[maxe]; void dfs(int v, int p) { static int timer = 0; tin[v] = fup[v] = timer++; u[v] = 1; for (int e : g[v]) { int to = x[e]; if (to == p) continue; if (u[to]) { umin(fup[v], tin[to]); } else { dfs(to, v); umin(fup[v], fup[to]); if (fup[to] > fup[v]) { bridge[e] = 1; bridge[e ^ 1] = 1; } } } } int comp[maxn]; int cur; int cnt[maxn]; void findc(int v) { comp[v] = cur; cnt[cur]++; for (int e : g[v]) { if (bridge[e] || comp[x[e]] != 0) continue; findc(x[e]); } } int depth[maxn]; int sz[maxn]; int dp[maxn][maxn]; int val, val2; void dfs2(int v, int par) { depth[v] = depth[par] + cnt[v]; val += cnt[v] * depth[v]; sz[v] = cnt[v]; for (int x : g[v]) { if (x != par) { dfs2(x, v); sz[v] += sz[x]; } } val2 += sz[v] * cnt[v]; } int solve(int v) { memset(depth, 0, sizeof depth); memset(sz, 0, sizeof sz); int c = g[v].size(); for (int i = (0); i < (c + 1); i++) for (int j = (0); j < (n + 1); j++) dp[i][j] = -inf; dp[0][0] = 0; int j = 1; for (int x : g[v]) { val = val2 = 0; dfs2(x, v); for (int c = (0); c < (n + 1); c++) { umax(dp[j][c], dp[j - 1][c] + val); if (c >= sz[x]) umax(dp[j][c], dp[j - 1][c - sz[x]] + val2); } j++; } int ret = 0; for (int i = (0); i < (n + 1); i++) { umax(ret, dp[c][i] + (i + cnt[v]) * (n - i)); } return ret; } int main() { n = getint<int>(); m = getint<int>(); auto addedge = [](int a, int b) -> void { g[a].push_back(c); x[c++] = b; }; for (int i = (0); i < (m); i++) { int a, b; a = getint<int>(); b = getint<int>(); addedge(a, b); addedge(b, a); } dfs(1, -1); for (int i = (1); i < (n + 1); i++) { if (comp[i] != 0) continue; cur++; findc(i); } for (int i = (1); i < (n + 1); i++) g[i].clear(); for (int i = (0); i < (m); i++) { int e = 2 * i; int a = comp[x[e]]; int b = comp[x[e + 1]]; if (a != b) { g[a].push_back(b), g[b].push_back(a); } } int res = 0; for (int i = (1); i < (n + 1); i++) { umax(res, solve(i)); } printf("%d\n", res); return 0; }
19
#include <bits/stdc++.h> using namespace std; int row[1010]; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); memset(row, 0x3f3f3f3f, sizeof row); long long ans = 0; for (int i = (0); i < (n); ++i) { int r, c; scanf("%d %d", &r, &c); row[r] = min(c, row[r]); } for (int i = 1; i <= m; ++i) ans += ((row[i] == 0x3f3f3f3f) ? 0 : row[i]); printf("%lld\n", min(ans, (long long)k)); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n; map<int, int> up, down; int orientation(pair<int, int> a, pair<int, int> b, pair<int, int> c) { pair<int, int> ab = {b.first - a.first, b.second - a.second}; pair<int, int> bc = {c.first - b.first, c.second - b.second}; int64_t cross = 1ll * ab.first * bc.second - 1ll * ab.second * bc.first; if (cross < 0) return -1; if (cross > 0) return 1; return 0; } bool inside(map<int, int>& mp, int x, int y) { if (mp.count(x)) return y <= mp[x]; auto itr = mp.upper_bound(x); auto itl = itr; if (itr == mp.end() || itl == mp.begin()) return false; --itl; return orientation(*itl, make_pair(x, y), *itr) >= 0; } void add(map<int, int>& mp, int x, int y) { if (inside(mp, x, y)) return; mp[x] = y; auto it = mp.upper_bound(x); if (it != mp.end()) while (next(it) != mp.end() && orientation(make_pair(x, y), *it, *next(it)) >= 0) { it = mp.erase(it); } it = mp.find(x); if (it == mp.begin()) return; it = prev(it); while (it != mp.begin() && orientation(*prev(it), *it, make_pair(x, y)) >= 0) { it = prev(mp.erase(it)); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0, inst, x, y; i < n; i++) { cin >> inst >> x >> y; if (inst == 1) { add(up, x, y); add(down, x, -y); } else cout << (inside(up, x, y) && inside(down, x, -y) ? "YES\n" : "NO\n"); } }
19
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a, b; cin >> n >> k; vector<int> v[n + 1]; for (int i = 1; i <= n; i++) v[i].push_back(0); for (int i = 0; i < k; i++) { cin >> a >> b; v[a][0]++; v[b][0]++; v[a].push_back(b); v[b].push_back(a); } int cnt[3] = {0}; for (int i = 1; i <= n; i++) { if (v[i][0] == 1) cnt[0]++; else if (v[i][0] == 2) cnt[1]++; else cnt[2] = v[i][0]; } if (cnt[0] == 2 && cnt[1] == n - 2) cout << "bus topology"; else if (cnt[1] == n) cout << "ring topology"; else if (cnt[2] == n - 1 && cnt[0] == n - 1) cout << "star topology"; else cout << "unknown topology"; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2004; int n, a, b; bool Macierz[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> a >> b; if ((a > 1 && b > 1)) { cout << "NO"; getchar(); getchar(); return 0; } if (n == 2 && a == 1 && b == 1) return cout << "NO", 0; if (n == 3 && a == 1 && b == 1) return cout << "NO", 0; if (b == 1) { for (int i = 2; i <= n - a + 1; i++) { Macierz[i - 1][i] = true; Macierz[i][i - 1] = true; } } else { for (int i = 2; i <= n - b + 1; i++) { Macierz[i - 1][i] = true; Macierz[i][i - 1] = true; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i != j) Macierz[i][j] = !Macierz[i][j]; } } } cout << "YES" << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << Macierz[i][j]; } cout << endl; } getchar(); getchar(); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, a, b; cin >> n >> a >> b; if (a && a > n - 2) { cout << -1 << endl; return 0; } vector<int> ret(n, 1); for (int i = 0; i < (int)(b); i++) ret[1 + i] = ret[i] * 2; if (!b && a) { ret[0] = 10; ret[1] = 10; ++b; } for (int i = 0; i < (int)(a); i++) ret[b + i + 1] = ret[b + i] + 1; for (int i = 0; i < (int)(n); i++) cout << ret[i] << " "; cout << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int x, y; bool odd = false; cin >> x >> y; for (int i = 0; i < x; i++) { if (i % 2 == 0) { for (int j = 0; j < y; j++) cout << "#"; cout << "\n"; } else { if (odd == false) { for (int k = 0; k < y - 1; k++) cout << "."; cout << "#"; odd = true; cout << "\n"; } else { cout << "#"; for (int l = 0; l < y - 1; l++) cout << "."; cout << "\n"; odd = false; } } } }
0
#include <bits/stdc++.h> using namespace std; int N, x[1005], y[1005]; double L, R; int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) scanf("%d%d", &x[i], &y[i]); L = x[1], R = x[2], x[N + 1] = x[1], y[N + 1] = y[1]; if (L > R) swap(L, R); for (int i = 2; i <= N; i++) { double k, b, xx; if (y[i] == y[i + 1]) if ((x[i] < x[i + 1]) ^ (x[1] < x[2])) continue; else return puts("0"), 0; if (x[i] == x[i + 1]) xx = x[i]; else k = 1.0 * (y[i] - y[i + 1]) / (x[i] - x[i + 1]), b = y[i] - k * x[i], xx = (y[1] - b) / k; if (y[i] < y[i + 1]) L = max(L, xx); else R = min(R, xx); } printf("%d\n", L > R ? 0 : int(floor(R) - ceil(L) + 1)); }
17
#include <bits/stdc++.h> using namespace std; long long h, w, p = 1ll, a, b, temp, times = 30ll; int main() { scanf("%lld%lld", &h, &w); while (times--) { if (p <= h && (p << 2ll) <= 5ll * w) { temp = min(w, 5ll * p >> 2ll); if (p * temp > a * b || (p * temp == a * b && a < p)) { a = p; b = temp; } } if (p <= w && (p >> 2ll) <= 5ll * h) { temp = min(h, 5ll * p >> 2ll); if (p * temp > a * b || (p * temp == a * b && a < temp)) { a = temp; b = p; } } p <<= 1ll; } printf("%lld %lld\n", a, b); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; const double INF = 1e6; const double eps = 1e-16; const double pi = acos(-1); template <typename T> void chkmax(T &x, T y) { x = max(x, y); } template <typename T> void chkmin(T &x, T y) { x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } struct point { double x, y; }; struct line { point a, b; double ra, rb; }; point operator+(point a, point b) { return (point){a.x + b.x, a.y + b.y}; } point operator-(point a, point b) { return (point){a.x - b.x, a.y - b.y}; } point operator*(point a, double b) { return (point){a.x * b, a.y * b}; } double operator*(point a, point b) { return a.x * b.y - a.y * b.x; } point intersect(const line &x, const line &y) { double tmp = (y.a - x.a) * (y.b - x.a); double tnp = (y.b - x.b) * (y.a - x.b); return (x.a * tnp + x.b * tmp) * (1 / (tmp + tnp)); } point get(line a, double rad) { return intersect(a, (line){(point){0, 0}, (point){cos(rad), sin(rad)}}); } bool better(line a, line b, double rad) { if (a.ra == a.rb) return false; if (b.ra == b.rb) return true; point ta = get(a, rad); point tb = get(b, rad); return ta.x * ta.x + ta.y * ta.y < tb.x * tb.x + tb.y * tb.y; } struct SegmentTree { struct Node { int lc, rc; double rad; line Max, Nax; } a[MAXN * 2]; int n, root, size; void build(int &root, int l, int r, double *ang) { root = ++size; a[root].rad = (ang[l] + ang[r + 1]) / 2; if (l == r) return; int mid = (l + r) / 2; build(a[root].lc, l, mid, ang); build(a[root].rc, mid + 1, r, ang); } void init(int x, double *ang) { n = x, root = size = 0; build(root, 1, n, ang); } void addline(int root, line x) { if (better(x, a[root].Max, a[root].rad)) { a[root].Nax = a[root].Max; a[root].Max = x; } else if (better(x, a[root].Nax, a[root].rad)) a[root].Nax = x; } void modify(int root, int l, int r, int ql, int qr, line x) { if (l == ql && r == qr) { addline(root, x); return; } int mid = (l + r) / 2; if (mid >= ql) modify(a[root].lc, l, mid, ql, min(mid, qr), x); if (mid + 1 <= qr) modify(a[root].rc, mid + 1, r, max(mid + 1, ql), qr, x); } void modify(int l, int r, line x) { modify(root, 1, n, l, r, x); } void pushdown(int root) { if (a[root].Max.ra != a[root].Max.rb) { addline(a[root].lc, a[root].Max); addline(a[root].rc, a[root].Max); } if (a[root].Nax.ra != a[root].Nax.rb) { addline(a[root].lc, a[root].Nax); addline(a[root].rc, a[root].Nax); } } double res; void getans(int root, int l, int r, double *ang) { if (l == r) { if (a[root].Max.ra == a[root].Max.rb) return; res += get(a[root].Nax, ang[l]) * get(a[root].Nax, ang[r + 1]); res -= get(a[root].Max, ang[l]) * get(a[root].Max, ang[r + 1]); return; } pushdown(root); int mid = (l + r) / 2; getans(a[root].lc, l, mid, ang); getans(a[root].rc, mid + 1, r, ang); } double getans(double *ang) { getans(root, 1, n, ang); return res / 2; } } ST; int lines; line a[MAXN]; int angles; double ang[MAXN]; void AddSegment(point s, point t) { if (s * t == 0) return; if (s * t < 0) swap(s, t); if (s.y > 0 && t.y < 0) { point p = intersect((line){s, t}, (line){(point){0, 0}, (point){-INF, 0}}); p.y = 0, AddSegment(s, p), AddSegment(t, p); } else { double ra = atan2(s.y, s.x); double rb = atan2(t.y, t.x); if (ra > rb) ra = -pi; a[++lines] = (line){s, t, ra, rb}; } } int main() { int T; read(T); while (T--) { int m; read(m); static point p[MAXN]; for (int i = 1; i <= m; i++) read(p[i].x), read(p[i].y); p[0] = p[m]; for (int i = 1; i <= m; i++) AddSegment(p[i - 1], p[i]); } for (int i = 1; i <= lines; i++) { ang[++angles] = a[i].ra; ang[++angles] = a[i].rb; } sort(ang + 1, ang + angles + 1); int tmp = angles; angles = 0; for (int i = 1; i <= tmp; i++) if (i == 1 || ang[i] - ang[angles] > eps) ang[++angles] = ang[i]; ST.init(angles - 1, ang); for (int i = 1; i <= lines; i++) { int l = lower_bound(ang + 1, ang + angles + 1, a[i].ra - eps) - ang; int r = lower_bound(ang + 1, ang + angles + 1, a[i].rb - eps) - ang; ST.modify(l, r - 1, a[i]); } printf("%.10lf\n", ST.getans(ang)); return 0; }
22
#include <bits/stdc++.h> using namespace std; const int nm = 110; int n, a[nm]; string res[nm]; void solve() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } a[n + 1] = 0; res[0] = string(max(a[1], 1), 'a'); for (int i = 1; i <= n; ++i) { res[i] = res[i - 1].substr(0, a[i]); int l = max({a[i], a[i + 1], 1}); if (l > a[i]) { if (a[i] + 1 > res[i - 1].length()) { res[i] += string(l - a[i], 'a'); } else { char x = res[i - 1][a[i]]; x = (x - 'a' + 1) % 26 + 'a'; res[i] += x + string(l - a[i] - 1, 'a'); } } } for (int i = 0; i <= n; ++i) { cout << res[i] << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for (int i = 1; i <= t; ++i) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; char inp[101][101] = {0}; char ans[101][101] = {0}; const int op[][2] = {-1, 0, -1, 1, -1, -1, 1, -1, 1, 0, 1, 1, 0, -1, 0, 1}; int count(int i, int j) { int x = 0; for (int t = 0; t < 8; t++) { if (inp[i + op[t][0]][j + op[t][1]] == '*' && i + op[t][0] >= 0 && j + op[t][1] >= 0) x++; } return x; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> inp[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (inp[i][j] == '*') continue; if (inp[i][j] == '.') { if (count(i, j) == 0) continue; else { cout << "NO"; return 0; } } else if (inp[i][j] > '0' && inp[i][j] <= '9') { if (count(i, j) == inp[i][j] - '0') continue; else { cout << "NO"; return 0; } } } cout << "YES"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int frq[10000]; int main() { int n, x, i, T; scanf("%d", &n); memset(frq, 0, sizeof frq); for (i = 0; i < n; i++) { scanf("%d", &x); frq[x]++; } for (i = 1; i < 10000; i++) frq[i] += frq[i - 1]; scanf("%d", &T); int mx = frq[T]; for (i = 1; i < 10000 - T; i++) { mx = max(mx, frq[i + T] - frq[i - 1]); } printf("%d\n", mx); }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; int mx = *max_element(a.begin(), a.end()); vector<int> vis(mx + 1, -1), cnt(mx + 1), dis(mx + 1, 0); for (int i = 0; i < n; i++) { int s = a[i]; queue<pair<int, int>> q; q.emplace(s, 0); while (!q.empty()) { int x, d; tie(x, d) = q.front(); q.pop(); if (x > mx or vis[x] == i) continue; cnt[x]++; vis[x] = i; dis[x] += d; q.emplace(x * 2, d + 1); q.emplace(x / 2, d + 1); } } int res = 2e9; for (int s = 0; s <= mx; s++) { if (cnt[s] == n and res > dis[s]) res = dis[s]; } cout << res << '\n'; }
11
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30) - 1; const int N = 200005; array<int, 9> id = {inf, inf, inf, inf, inf, inf, inf, inf, inf}; struct node { array<int, 9> a; int best; node() { a = id; best = 2 * inf; } }; vector<node> tree(4 * N, node()); vector<int> val(N, 0); int h(int a) { int x = 0; for (int i = 0; i < 9; i++) { if ((a % 10) > 0) x |= (1 << i); a /= 10; } return x; } node operator+(const node &a, const node &b) { if (a.a == id) return b; if (b.a == id) return a; node c; c.best = min(a.best, b.best); for (int i = 0; i < 9; i++) { if (a.a[i] != inf && b.a[i] != inf) c.best = min(c.best, a.a[i] + b.a[i]); c.a[i] = min(a.a[i], b.a[i]); } return c; } void build(int idx, int l, int r) { if (l == r) { int x = h(val[l]); for (int i = 0; i < 9; i++) if (x & (1 << i)) tree[idx].a[i] = val[l]; } else { int m = (l + r) / 2; build(2 * idx, l, m); build(2 * idx + 1, m + 1, r); tree[idx] = tree[2 * idx] + tree[2 * idx + 1]; } } void update(int idx, int l, int r, int pos) { if (l == r) { tree[idx].a = id; int x = h(val[l]); for (int i = 0; i < 9; i++) if (x & (1 << i)) tree[idx].a[i] = val[l]; } else { int m = (l + r) / 2; if (pos <= m) update(2 * idx, l, m, pos); else update(2 * idx + 1, m + 1, r, pos); tree[idx] = tree[2 * idx] + tree[2 * idx + 1]; } } node query(int idx, int l, int r, int ql, int qr) { if (ql > qr) return node(); if (l == ql && r == qr) return tree[idx]; int m = (l + r) / 2; return query(2 * idx, l, m, ql, min(qr, m)) + query(2 * idx + 1, m + 1, r, max(ql, m + 1), qr); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> val[i]; build(1, 1, n); while (m--) { int t, a, b; cin >> t >> a >> b; if (t == 1) { val[a] = b; update(1, 1, n, a); } else { int res = query(1, 1, n, a, b).best; if (res == 2 * inf) res = -1; cout << res << "\n"; } } return 0; }
15
#include <bits/stdc++.h> template <class T> inline void read(T &res) { char ch; while (ch = getchar(), !isdigit(ch)) ; res = ch ^ 48; while (ch = getchar(), isdigit(ch)) res = res * 10 + ch - 48; } template <class T> inline void put(T x) { if (x > 9) put(x / 10); putchar(x % 10 + 48); } using std::pair; using std::vector; const int N = 1e5 + 5; const int M = 2e5 + 5; int que[N]; bool vis[N]; int n, q, z, tot, qr, opt; struct Edge { int to, cst; Edge *nxt; } p[M], *lst[N], *P = p; inline void Link(int x, int y, int z) { (++P)->nxt = lst[x]; lst[x] = P; P->to = y; P->cst = z; (++P)->nxt = lst[y]; lst[y] = P; P->to = x; P->cst = z; } template <class T> inline T Max(T x, T y) { return x > y ? x : y; } struct tree { pair<int, int> pos[N]; int anc[N][20], dis[N]; int cst[N], pre[N], mx[N], son[N]; int ed[N], _rank[N], id[N], sum[N]; int rt, T; inline void findRoot(int src) { rt = src; for (int i = 1; i <= n; ++i) vis[i] = false; que[qr = 1] = src; vis[src] = true; dis[src] = 0; for (int i = 1, x, y; i <= qr; ++i) { x = que[i]; for (Edge *e = lst[x]; e; e = e->nxt) if (y = e->to, !vis[y]) { que[++qr] = y; vis[y] = true; dis[y] = dis[x] + e->cst; if (dis[y] > dis[rt]) rt = y; } } } inline void dfs1(int x) { for (int i = 0; anc[x][i]; ++i) anc[x][i + 1] = anc[anc[x][i]][i]; for (Edge *e = lst[x]; e; e = e->nxt) { int y = e->to; if (y == anc[x][0]) continue; anc[y][0] = x; cst[y] = e->cst; dis[y] = dis[x] + cst[y]; dfs1(y); int tmp = mx[y] + cst[y]; if (tmp > mx[x]) mx[x] = tmp, son[x] = y; } } inline void dfs2(int x) { if (son[x]) { id[son[x]] = id[x]; sum[id[x]] += cst[son[x]]; dfs2(son[x]); } else ed[id[x]] = x; for (Edge *e = lst[x]; e; e = e->nxt) { int y = e->to; if (y == anc[x][0] || y == son[x]) continue; id[y] = ++T; sum[T] = cst[y]; dfs2(y); } } inline void init() { dis[rt] = 0; dfs1(rt); id[rt] = T = 1; dfs2(rt); for (int i = 1; i <= T; ++i) pos[i] = std::make_pair(sum[i], i); std::sort(pos + 1, pos + T + 1); std::reverse(pos + 1, pos + T + 1); for (int i = 1; i <= T; ++i) pre[i] = pre[i - 1] + pos[i].first; for (int i = 1; i <= T; ++i) _rank[pos[i].second] = i; } inline int query(int x, int y) { int u = x; for (int i = 16; i >= 0; --i) if (anc[u][i] && _rank[id[anc[u][i]]] > y) u = anc[u][i]; u = anc[u][0]; return Max(pre[y - 1], pre[y] - mx[u]) + dis[ed[id[x]]] - dis[u]; } inline int calc(int x, int y) { return _rank[id[x]] <= y ? pre[y] : query(x, y); } } t1, t2; int main() { read(n); read(q); opt = 1; for (int i = 1, x, y, z; i < n; ++i) { read(x); read(y); read(z); Link(x, y, z); tot += z; } t1.findRoot(1); t2.findRoot(t1.rt); t1.init(); t2.init(); int last_ans = 0, x, y; while (q--) { read(x); read(y); if (opt) { x = (x + last_ans - 1) % n + 1; y = (y + last_ans - 1) % n + 1; } y = (y << 1) - 1; put(last_ans = y >= t1.T ? tot : Max(t1.calc(x, y), t2.calc(x, y))), putchar('\n'); } return 0; }
25
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } void __print(long long first) { cerr << first; } void __print(long first) { cerr << first; } void __print(unsigned first) { cerr << first; } void __print(unsigned long first) { cerr << first; } void __print(unsigned long long first) { cerr << first; } void __print(float first) { cerr << first; } void __print(double first) { cerr << first; } void __print(long double first) { cerr << first; } void __print(char first) { cerr << '\'' << first << '\''; } void __print(const char *first) { cerr << '\"' << first << '\"'; } void __print(const string &first) { cerr << '\"' << first << '\"'; } void __print(bool first) { cerr << (first ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &first) { cerr << '{'; __print(first.first); cerr << ','; __print(first.second); cerr << '}'; } template <typename T> void __print(const T &first) { long long f = 0; cerr << '{'; for (auto &i : first) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using cd = complex<double>; const double PI = acos(-1); void fft(vector<cd> &a, bool invert) { long long n = a.size(); for (long long i = 1, j = 0; i < n; i++) { long long bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (long long len = 2; len <= n; len <<= 1) { double ang = 2 * PI / len * (invert ? -1 : 1); cd wlen(cos(ang), sin(ang)); for (long long i = 0; i < n; i += len) { cd w(1); for (long long j = 0; j < len / 2; j++) { cd u = a[i + j], v = a[i + j + len / 2] * w; a[i + j] = u + v; a[i + j + len / 2] = u - v; w *= wlen; } } } if (invert) { for (cd &first : a) first /= n; } } vector<long long> multiply(vector<long long> const &a, vector<long long> const &b) { vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end()); long long n = 1; while (n < a.size() + b.size()) n <<= 1; fa.resize(n); fb.resize(n); fft(fa, false); fft(fb, false); for (long long i = 0; i < n; i++) fa[i] *= fb[i]; fft(fa, true); vector<long long> result(n); for (long long i = 0; i < n; i++) { result[i] = round(fa[i].real()); if (result[i] != 0) result[i] = 1; } return result; } vector<long long> poly_power(vector<long long> a, long long b) { vector<long long> res{1}; while (b) { if (b & 1) { res = multiply(res, a); } a = multiply(a, a); b = b / 2; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed << setprecision(12); long long n, k; cin >> n >> k; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } a.resize(unique(begin(a), end(a)) - begin(a)); long long mi = *min_element(begin(a), end(a)); for (long long &i : a) i -= mi; long long ma = *max_element(begin(a), end(a)); vector<long long> base(ma + 1, 0); for (long long i : a) { base[i] = 1; } vector<long long> res = poly_power(base, k); for (long long i = 0; i < res.size(); i++) { if (res[i] != 0) { cout << i + mi * k << " "; } } return 0; }
16
#include <bits/stdc++.h> using namespace std; struct f { int a; int b; } s[1005]; bool cmp(f x, f y) { if (x.b != y.b) return x.b > y.b; return x.a > y.a; } int main() { int n, i; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d%d", &s[i].a, &s[i].b); sort(s, s + n, cmp); int num = 1, max = 0; for (i = 0; i < n; i++) { if (num == 0) break; max += s[i].a; num += s[i].b; num--; } printf("%d\n", max); } return 0; }
3
#include <bits/stdc++.h> using namespace std; #define N 100100 int id[300], x[N], fst[30], lst[30], cnt[30]; int n, a, b; bool dp[1<<20]; char ch[30], s[N]; inline bool good(int y) { int l = n, r = 0, tot = 0; for (int i = 0; i < 20; i ++) if (y >> i & 1) { if (cnt[i]) { tot += cnt[i]; l = min(l, fst[i]); r = max(r, lst[i]); } } return (r - l + 1) * a <= b * tot; } int main() { for (int i = 'a', j = 0; i <= 'z'; i ++) { if (i == 't' || i == 'r' || i == 'y' || i == 'u' || i == 'b' || i == 'g') continue; id[i] = j, ch[j] = i; j ++; } scanf("%d %d %d", &n, &a, &b); scanf("%s", s + 1); for (int i = 1; i <= n; i ++) x[i] = id[s[i]]; for (int i = 1; i <= n; i ++) { if (!fst[x[i]]) fst[x[i]] = i; lst[x[i]] = i; cnt[x[i]] ++; } int up = 1 << 20; dp[0] = 1; for (int i = 1; i < up; i ++) { if (good(i)) { for (int j = 0; j < 20; j ++) if (i >> j & 1) { if (dp[i^1<<j]) dp[i] = 1; } } if (dp[i]) continue; vector <int> vec; for (int j = 0; j < 20; j ++) if (i >> j & 1) { vec.push_back(j); } sort(vec.begin(), vec.end(), [&](int x, int y) {return fst[x] < fst[y];}); int prv = 0; for (auto x : vec) { if (dp[prv] && dp[i^prv]) { dp[i] = 1; break; } prv ^= 1 << x; } } vector <int> ans; for (int i = 0; i < 20; i ++) if (cnt[i] && dp[up-1^1<<i]) ans.push_back(i); printf("%d", ans.size()); for (auto x : ans) printf(" %c", ch[x]); puts(""); return 0; }
27
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; int a[N], b[N], d[N], f[N], to[N]; bool ban[N]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d %d", &a[i], &b[i]), d[a[i]]++, d[b[i]]++, to[i] = m - i + 1; for (int i = 1; i <= n; i++) f[i] = (d[i] + 1) / 2; swap(to[1], to[(m + 1) / 2]); int cnt = 0; for (int i = 1; i <= m; i++) { if (d[a[to[i]]] > f[a[to[i]]] && d[b[to[i]]] > f[b[to[i]]]) { ban[i] = true, d[a[to[i]]]--, d[b[to[i]]]--, cnt++; } } printf("%d\n", m - cnt); for (int i = 1; i <= m; i++) { if (!ban[i]) printf("%d %d\n", a[to[i]], b[to[i]]); } return 0; }
16
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int N = 1e3 + 5; int n, p; int indeg[N], outdeg[N]; vector<pair<int, int> > adj[N]; vector<pair<int, pair<int, int> > > ans; set<int> tanks, taps; int src; int vis[N]; void dfs(int u, int minn) { vis[u] = 1; for (auto p : adj[u]) { minn = min(minn, p.second); if (taps.find(p.first) != taps.end()) { ans.push_back({src, {p.first, minn}}); } if (!vis[p.first]) dfs(p.first, minn); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n >> p; for (int i = 1; i <= p; i++) { int a, b, d; cin >> a >> b >> d; outdeg[a]++; indeg[b]++; adj[a].push_back({b, d}); } for (int i = 1; i <= n; i++) { if (indeg[i] == 0) { tanks.insert(i); } if (outdeg[i] == 0) { taps.insert(i); } } for (auto i : tanks) { src = i; memset(vis, 0, sizeof vis); dfs(i, INT_MAX); } sort(ans.begin(), ans.end()); cout << ans.size() << "\n"; for (auto ppp : ans) { cout << ppp.first << " " << ppp.second.first << " " << ppp.second.second << "\n"; } return 0; }
6
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") using namespace std; const long long N = 2e5 + 100, OO = 1e9 + 7, T = 1e6 + 10, mod = 1e9 + 7, M = 10301, P = 6151, SQ = 280, lg = 70; long long nxt[N], v[N], b[N], sz[N], res; bool is[N], mark[N]; pair<long long, long long> go(long long x, long long y) { if (x == OO || mark[x]) return {OO, OO}; if (y <= 0) return {x, 0}; if (!is[x]) { is[x] = true; res++; } mark[x] = true; y -= sz[x]; pair<long long, long long> p = go(nxt[x], y); sz[x] += p.second, nxt[x] = p.first; mark[x] = false; return {nxt[x], sz[x]}; } int32_t main() { long long n, m; cin >> n >> m; for (long long i = 1; i <= n; i++) { cin >> nxt[i]; sz[i] = 1; } for (long long i = 1; i <= m; i++) cin >> v[i]; for (long long i = 1; i <= m; i++) { cin >> b[i]; long long x = ((v[i] + res - 1) % n) + 1; res = 0; go(x, b[i]); cout << res << endl; } return 0; }
14
#include <bits/stdc++.h> using namespace std; vector<string> A(8); map<char, int> best; void check(int x, int y) { if (A[x][y] == '.') return; else if (A[x][y] == 'W') { for (int i = 0; i < x; i++) { if (A[i][y] == 'B') return; } best['W'] = min(best['W'], x); } else { for (int i = x + 1; i < 8; i++) { if (A[i][y] == 'W') return; } best['B'] = min(best['B'], 7 - x); } } int main() { ios::sync_with_stdio(0); cin.tie(0); for (int i = 0; i < 8; i++) cin >> A[i]; best['W'] = 1000; best['B'] = 1000; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { check(i, j); } } if (best['W'] <= best['B']) cout << 'A'; else cout << 'B'; cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; char str[100010]; int cnt[3]; int aux; int l, r; int main() { scanf("%s", str); int tam = strlen(str); cnt[0] = cnt[1] = cnt[2] = 0; for (int i = 0; i < tam; i++) { if (str[i] == '?') cnt[2]++; else cnt[str[i] - '0']++; } if (str[tam - 1] == '?') aux = 2; else aux = str[tam - 1] - '0'; int aa = (tam - 1) / 2; int bb = (tam - 2) / 2; if (aa >= cnt[1]) puts("00"); r = aa + 1 - cnt[1]; l = bb + 1 - cnt[0]; if (r >= 0 && l >= 0) { if (aux == 1) puts("01"); else if (aux == 0) puts("10"); else { if (r >= 1) puts("01"); if (l >= 1) puts("10"); } } if (bb >= cnt[0]) puts("11"); return 0; }
11
#include <bits/stdc++.h> using namespace std; const long long UNDEF = -1; const long long INF = 1ULL << 58ULL; template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; } template <typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; } pair<long long, long long> sol[8]; long long x[4], y[4]; long long final; void solve(long long r) { if (r) { for (long long j = 0; j < 4; j++) swap(x[j], y[j]); } bool has = false; set<long long> sy; for (long long i = 0; i < 4; i++) sy.insert(y[i]); vector<long long> vy; for (auto &w : sy) vy.push_back(w); long long d = abs(vy[1] - vy[0]); map<long long, long long> my; for (long long i = 0; i < 2; i++) { my[vy[i]] = i; } vector<pair<long long, long long> > b[2]; for (long long i = 0; i < 4; i++) { long long idx = my[y[i]]; b[idx].push_back(make_pair(x[i], i)); } for (long long idx = 0; idx < 2; idx++) { sort(b[idx].begin(), b[idx].end()); } long long imin = 0; long long imax = 1e9; while (imin < imax) { long long imid = imin + (imax - imin) / 2; set<long long> sx; for (long long idx = 0; idx < 2; idx++) { sx.insert(b[idx][0].first + imid); sx.insert(b[idx][0].first - imid); sx.insert(b[idx][1].first + imid - d); sx.insert(b[idx][1].first - imid - d); } long long best = INF; for (auto &w : sx) { long long ans = 0; for (long long idx = 0; idx < 2; idx++) { chkmax(ans, abs(w - b[idx][0].first)); chkmax(ans, abs(w + d - b[idx][1].first)); } chkmin(best, ans); if (ans < final) { final = ans; has = true; for (long long idx = 0; idx < 2; idx++) { sol[b[idx][0].second] = make_pair(w, vy[idx]); sol[b[idx][1].second] = make_pair(w + d, vy[idx]); } } } if (best > imid) { imin = imid + 1; } else imax = imid; } if (r) { for (long long j = 0; j < 4; j++) swap(x[j], y[j]); if (has) { for (long long j = 0; j < 4; j++) swap(sol[j].first, sol[j].second); } } } int main() { ios_base::sync_with_stdio(false); long long t; cin >> t; for (long long ii = 0; ii < t; ii++) { for (long long j = 0; j < 4; j++) { cin >> x[j] >> y[j]; } map<long long, long long> hx, hy; for (long long j = 0; j < 4; j++) { hx[x[j]]++; hy[y[j]]++; } bool xg = true; for (auto &w : hx) { if (w.second != 2) xg = false; } bool yg = true; for (auto &w : hy) { if (w.second != 2) yg = false; } final = INF; if (xg) { solve(1); } if (yg) { solve(0); } set<long long> sd; sd.insert(0); for (long long i = 0; i < 4; i++) { for (long long j = i + 1; j < 4; j++) { sd.insert(abs(x[i] - x[j])); sd.insert(abs(y[i] - y[j])); } } set<long long> sx, sy; for (long long i = 0; i < 4; i++) { for (auto &w : sd) { sx.insert(x[i] + w); sy.insert(y[i] + w); sx.insert(x[i] - w); sy.insert(y[i] - w); } } for (long long i = 0; i < 4; i++) { sx.erase(x[i]); sy.erase(y[i]); } vector<long long> vx, vy; for (long long i = 0; i < 4; i++) { vx.push_back(x[i]); vy.push_back(y[i]); } for (auto &w : sx) vx.push_back(w); for (auto &w : sy) vy.push_back(w); long long xn = vx.size(), yn = vy.size(); for (long long a = 0; a < 4; a++) { for (long long b = a + 1; b < xn; b++) { for (long long c = 0; c < yn; c++) { for (long long d = c + 1; d < yn; d++) { if (abs(vx[a] - vx[b]) != abs(vy[c] - vy[d])) continue; pair<long long, long long> pt[4]; pt[0] = make_pair(vx[a], vy[c]); pt[1] = make_pair(vx[a], vy[d]); pt[2] = make_pair(vx[b], vy[c]); pt[3] = make_pair(vx[b], vy[d]); long long myints[] = {0, 1, 2, 3}; do { long long ans = 0; for (long long k = 0; k < 4; k++) { pair<long long, long long> p = pt[myints[k]]; pair<long long, long long> q = make_pair(x[k], y[k]); long long best = INF; if (p.first == q.first) { best = abs(p.second - q.second); } else if (p.second == q.second) { best = abs(p.first - q.first); } chkmax(ans, best); if (ans >= final) break; } if (ans < final) { final = ans; for (long long i = 0; i < 4; i++) { sol[i] = pt[myints[i]]; } } } while (std::next_permutation(myints, myints + 4)); } } } } for (long long a = 0; a < xn; a++) { for (long long b = a + 1; b < xn; b++) { for (long long c = 0; c < 4; c++) { for (long long d = c + 1; d < yn; d++) { if (abs(vx[a] - vx[b]) != abs(vy[c] - vy[d])) continue; pair<long long, long long> pt[4]; pt[0] = make_pair(vx[a], vy[c]); pt[1] = make_pair(vx[a], vy[d]); pt[2] = make_pair(vx[b], vy[c]); pt[3] = make_pair(vx[b], vy[d]); long long myints[] = {0, 1, 2, 3}; do { long long ans = 0; for (long long k = 0; k < 4; k++) { pair<long long, long long> p = pt[myints[k]]; pair<long long, long long> q = make_pair(x[k], y[k]); long long best = INF; if (p.first == q.first) { best = abs(p.second - q.second); } else if (p.second == q.second) { best = abs(p.first - q.first); } chkmax(ans, best); if (ans >= final) break; } if (ans < final) { final = ans; for (long long i = 0; i < 4; i++) { sol[i] = pt[myints[i]]; } } } while (std::next_permutation(myints, myints + 4)); } } } } if (final >= INF) { cout << -1 << "\n"; } else { cout << final << "\n"; for (long long i = 0; i < 4; i++) { cout << sol[i].first << " " << sol[i].second << "\n"; } } } }
22
#include <bits/stdc++.h> using namespace std; void solve(long long testcase) { long long n; cin >> n; vector<long long> v(n); map<long long, vector<long long>> mp; for (long long i = 0; i < n; i++) { cin >> v[i]; mp[v[i]].push_back(i); } vector<long long> a, b; long long c = 1; for (auto p : mp) if (p.first != c) { cout << -1 << '\n'; return; } else c++; for (auto p : mp) a.push_back(p.second.size()); b = a; sort(b.rbegin(), b.rend()); if (a != b) { cout << -1 << '\n'; return; } long long ans = 0; vector<long long> Ans(n); for (;;) { long long ok = 0; for (long long i = 0; i < a.size(); i++) { if (a[i] > 0) { a[i]--; ok = 1; Ans[mp[i + 1].back()] = ans; mp[i + 1].pop_back(); } else break; } if (!ok) break; ans++; } cout << ans << '\n'; for (long long x : Ans) cout << x + 1 << ' '; cout << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long T = 1; for (long long t = 1; t <= T; t++) solve(t); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int MX = 2005; const long long MOD = 1000000007; const long long P1 = 998244353; const long long P2 = 1000000009; const long long R = 1331; void add(long long &x, const long long &y) { x = (x + y) % MOD; } char str[2][MX], tar[MX]; int n, m; void input() { scanf("%s", str[0] + 1); scanf("%s", str[1] + 1); scanf("%s", tar + 1); n = strlen(str[0] + 1); m = strlen(tar + 1); } long long *pwr, base[MX * 4]; long long qpow(long long x, long long t, long long p) { long long ret = 1; while (t) { if (t & 1) ret = ret * x % p; x = x * x % p; t >>= 1; } return ret; } long long inv(long long x, long long p) { return qpow(x, p - 2, p); } void init() { pwr = base + MX * 2; pwr[0] = 1; for (int i = 1; i < MX * 2; i++) pwr[i] = pwr[i - 1] * R % P1; long long iv = inv(R, P1); for (int i = -1; i > -MX * 2; i--) pwr[i] = pwr[i + 1] * iv % P1; } long long ans; long long pre_hash[MX], suf_hash[MX]; long long fwd[2][MX], bcw[2][MX]; long long lft[2][MX][MX], tmp[2][MX][MX], rgt[2][MX][MX]; void work() { memset(lft, 0, sizeof(lft)); memset(rgt, 0, sizeof(rgt)); long long h = 0; for (int i = 1; i <= m; i++) { h = (h * R + tar[i]) % P1; pre_hash[i] = h; } long long g = 0; for (int i = m; i >= 1; i--) { g = (g * R + tar[i]) % P1; suf_hash[m - i + 1] = g; } for (int i = 1; i <= n; i++) { fwd[0][i] = (fwd[0][i - 1] + pwr[i] * str[0][i]) % P1; fwd[1][i] = (fwd[1][i - 1] + pwr[i] * str[1][i]) % P1; bcw[0][i] = (bcw[0][i - 1] + pwr[-i] * str[0][i]) % P1; bcw[1][i] = (bcw[1][i - 1] + pwr[-i] * str[1][i]) % P1; } for (int i = 0; i <= n; i++) { for (int j = 4; j <= m; j += 2) { if (j > i * 2) break; long long h0 = ((fwd[0][i] - fwd[0][i - j / 2] + P1) * pwr[j - 1 - i] + (bcw[1][i] - bcw[1][i - j / 2] + P1) * pwr[i]) % P1; long long h1 = ((fwd[1][i] - fwd[1][i - j / 2] + P1) * pwr[j - 1 - i] + (bcw[0][i] - bcw[0][i - j / 2] + P1) * pwr[i]) % P1; if (h0 == pre_hash[j]) lft[1][i][j] = 1; if (h1 == pre_hash[j]) lft[0][i][j] = 1; } } for (int i = 1; i <= n + 1; i++) { for (int j = 4; j <= m; j += 2) { if (j > (n - i + 1) * 2) break; long long h0 = ((fwd[0][i + j / 2 - 1] - fwd[0][i - 1] + P1) * pwr[-i] + (bcw[1][i + j / 2 - 1] - bcw[1][i - 1] + P1) * pwr[i + j - 1]) % P1; long long h1 = ((fwd[1][i + j / 2 - 1] - fwd[1][i - 1] + P1) * pwr[-i] + (bcw[0][i + j / 2 - 1] - bcw[0][i - 1] + P1) * pwr[i + j - 1]) % P1; if (h0 == suf_hash[j]) rgt[0][i][j] = 1; if (h1 == suf_hash[j]) rgt[1][i][j] = 1; } } memmove(tmp, lft, sizeof(tmp)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (str[0][i] == tar[j]) { if (j == 1) tmp[0][i][j] = 1; add(tmp[0][i][j], tmp[0][i - 1][j - 1]); if (j >= 2 && str[1][i] == tar[j - 1]) { if (j == 2) add(tmp[0][i][j], 1); add(tmp[0][i][j], tmp[1][i - 1][j - 2]); } } if (str[1][i] == tar[j]) { if (j == 1) tmp[1][i][j] = 1; add(tmp[1][i][j], tmp[1][i - 1][j - 1]); if (j >= 2 && str[0][i] == tar[j - 1]) { if (j == 2) add(tmp[1][i][j], 1); add(tmp[1][i][j], tmp[0][i - 1][j - 2]); } } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { add(ans, tmp[0][i][j] * rgt[0][i + 1][m - j]); add(ans, tmp[1][i][j] * rgt[1][i + 1][m - j]); } add(ans, tmp[0][i][m]); add(ans, tmp[1][i][m]); add(ans, rgt[0][i][m]); add(ans, rgt[1][i][m]); } } bool pal() { for (int i = 1; i <= m; i++) if (tar[i] != tar[m - i + 1]) return false; return true; } int main() { init(); input(); work(); if (m >= 2) { reverse(tar + 1, tar + m + 1); work(); for (int i = 1; i <= n; i++) ans = (ans + MOD * 4 - (lft[0][i][m] + lft[1][i][m] + rgt[0][i][m] + rgt[1][i][m])) % MOD; } if (m == 2) { for (int i = 1; i <= n; i++) { if (str[0][i] == tar[1] && str[1][i] == tar[2]) ans = (ans + MOD - 1) % MOD; if (str[1][i] == tar[1] && str[0][i] == tar[2]) ans = (ans + MOD - 1) % MOD; } } printf("%lld\n", ans); return 0; }
24
#include <bits/stdc++.h> using namespace std; int n, d, a[2000]; int main() { cin >> d >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long res = 0; for (int i = 0; i < n - 1; i++) res += d - a[i]; cout << res; return 0; }
2
#include <bits/stdc++.h> using namespace std; int c[1000005], d[1000005]; ; int n, q; int main() { cin >> n >> q; int a = 0, b = 1; for (int i = 0; i < q; i++) { int x, y; scanf("%d", &x); if (x == 1) { scanf("%d", &y); a = (a + y + n) % n; b = (b + y + n) % n; } else { a ^= 1; b ^= 1; } } b = (b + n - 1) % n; for (int i = 0; i < n; i++) c[i] = i; for (int i = 0; i < n; i++) { if (i & 1) c[i] = (c[i] + b) % n; else c[i] = (c[i] + a) % n; } for (int i = 0; i < n; i++) { d[c[i]] = i; } for (int i = 0; i < n; i++) printf("%d ", d[i] + 1); }
10
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n, m; cin >> n >> m; cout << gcd(n - 1, m - 1) + 1; return 0; }
13
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.length(); vector<bool> used(26, false); for (int i = 0; i < n; i++) { for (int j = s[i] - 'a' + 1; j < 26; j++) { used[j] = true; } if (used[s[i] - 'a']) { cout << "Ann" << endl; } else { cout << "Mike" << endl; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, x; cin >> n >> x; cout << min(4 * n - x, 3 * n + x - 1) << endl; }
2
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); int dblcmp(double d) { if (fabs(d) < eps) return 0; return d > eps ? 1 : -1; } struct team { string name; int sc; int g, m; team() {} team(string sv) { name = sv; sc = g = m = 0; } bool operator<(const team& gg) const { if (sc == gg.sc) { if (g - m == gg.g - gg.m) { if (g == gg.g) return name > gg.name; return g < gg.g; } return g - m < gg.g - gg.m; } return sc < gg.sc; } }; bool check(vector<string> s) { map<string, team> mm; for (__typeof(s.begin()) e = s.begin(); e != s.end(); ++e) { stringstream ss(*e); string a, b, c; ss >> a >> b >> c; mm[a] = team(a); mm[b] = team(b); } for (__typeof(s.begin()) e = s.begin(); e != s.end(); ++e) { stringstream ss(*e); string a, b, c; ss >> a >> b >> c; for (int i = 0; i < (int)((c).size()); i++) if (c[i] == ':') c[i] = ' '; stringstream ss1(c); int x, y; ss1 >> x >> y; mm[a].g += x; mm[a].m += y; mm[b].g += y; mm[b].m += x; if (x > y) { mm[a].sc += 3; } else if (x == y) { mm[a].sc++; mm[b].sc++; } else { mm[b].sc += 3; } } vector<team> v; for (__typeof(mm.begin()) e = mm.begin(); e != mm.end(); ++e) { v.push_back(e->second); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); for (int i = 0; i < (int)((v).size()); i++) { if (v[i].name == "BERLAND") { return i <= 1; } } return 0; } pair<int, int> less1(pair<int, int> a, pair<int, int> b) { if (a.first - a.second > b.first - b.second) return b; if (a.first - a.second < b.first - b.second) return a; if (a.second > b.second) return b; return a; } int main() { map<string, int> mm; int i, j, k; vector<string> s; for (i = 0; i < 5; i++) { string a, b, c; cin >> a >> b >> c; s.push_back(a + " " + b + " " + c); mm[a]++; mm[b]++; } string e = ""; e += "BERLAND "; for (__typeof(mm.begin()) ee = mm.begin(); ee != mm.end(); ++ee) { if (ee->second == 2 && ee->first != "BERLAND") { e += ee->first; e += " "; break; } } pair<int, int> ans = make_pair(100, 0); bool ok = 0; for (i = 80; i >= 1; i--) { for (j = i - 1; j >= 0; j--) { char sv[33]; sprintf(sv, "%d:%d", i, j); string v = e + (string)sv; s.push_back(v); if (check(s)) { ok = 1; ans = less1(ans, make_pair(i, j)); } s.pop_back(); } } if (!ok) { puts("IMPOSSIBLE"); return 0; } printf("%d:%d\n", ans.first, ans.second); return 0; }
10
#include <bits/stdc++.h> using namespace std; template <typename T> T extgcd(T a, T b, T& x, T& y) { if (b == 0) { x = 1; y = 0; return a; } T q = a / b; T g = extgcd(b, a - q * b, y, x); y -= q * x; return g; } long long a1, b1, a2, b2; long long f(long long N) { long long x, y; long long g = extgcd(a1, a2, x, y); if ((b2 - b1) % g != 0) return 0; long long d = (b2 - b1) / g; cerr << a1 << "*" << x << "+" << a2 << "*" << y << "=" << g << endl; x *= d; y *= d; cerr << a1 << "*" << x << "+" << a2 << "*" << y << "=" << d * g << endl; if (x < 0) { long long t = -x; long long z = a2 / g; long long k = (t + z - 1) / z; x += k * z; y -= k * (a1 / g); } if (y > 0) { long long t = y; long long z = a1 / g; long long k = (t + z - 1) / z; x += k * (a2 / g); y -= k * z; } { long long z = a2 / g; long long w = a1 / g; long long k1 = x / z; long long k2 = -y / w; if (k1 > k2) k1 = k2; x -= k1 * z; y += k2 * w; } cerr << a1 << "*" << x << "+" << a2 << "*" << y << "=" << d * g << endl; long long s = b1 + a1 * x; cerr << a1 / g * a2 << "x+" << s << "<=" << N << endl; if (s > N) return 0; return 1 + (N - s) / (a1 / g * a2); } int main() { long long L, R; cin >> a1 >> b1 >> a2 >> b2 >> L >> R; R -= L; b1 -= L; b2 -= L; if (b1 < 0) b1 %= a1; if (b1 < 0) b1 += a1; if (b2 < 0) b2 %= a2; if (b2 < 0) b2 += a2; if (b1 > b2) swap(b1, b2), swap(a1, a2); b1 += (b2 - b1) / a1 * a1; cerr << a1 << "x+" << b1 << endl; cerr << a2 << "x+" << b2 << endl; cout << f(R) << endl; }
17
#include <bits/stdc++.h> using namespace std; int main() { string s; int w = 0; int b = 0; for (int i = 0; i < 8; i++) { cin >> s; for (int j = 0; j < 8; j++) { char p = s[j]; if (p == 'Q') { w += 9; } else if (p == 'q') { b += 9; } else if (p == 'R') { w += 5; } else if (p == 'r') { b += 5; } else if (p == 'B') { w += 3; } else if (p == 'b') { b += 3; } else if (p == 'N') { w += 3; } else if (p == 'n') { b += 3; } else if (p == 'P') { w += 1; } else if (p == 'p') { b += 1; } } } if (w > b) { cout << "White" << endl; } else if (b > w) { cout << "Black" << endl; } else { cout << "Draw" << endl; } return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}; const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; const int maxn = 1e5 + 10; const int maxx = 600005; const double EPS = 1e-8; const double eps = 1e-8; const int mod = 1e9 + 7; template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline bool scan_d(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) { return 0; } while (c != '-' && (c < '0' || c > '9')) { c = getchar(); } sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'); } ret *= sgn; return 1; } inline bool scan_lf(double &num) { char in; double Dec = 0.1; bool IsN = false, IsD = false; in = getchar(); if (in == EOF) return false; while (in != '-' && in != '.' && (in < '0' || in > '9')) in = getchar(); if (in == '-') { IsN = true; num = 0; } else if (in == '.') { IsD = true; num = 0; } else num = in - '0'; if (!IsD) { while (in = getchar(), in >= '0' && in <= '9') { num *= 10; num += in - '0'; } } if (in != '.') { if (IsN) num = -num; return true; } else { while (in = getchar(), in >= '0' && in <= '9') { num += Dec * (in - '0'); Dec *= 0.1; } } if (IsN) num = -num; return true; } void Out(long long a) { if (a < 0) { putchar('-'); a = -a; } if (a >= 10) Out(a / 10); putchar(a % 10 + '0'); } void print(long long a) { Out(a), puts(""); } long long sum[maxn], sum1[maxn], sum2[maxn]; long long a[maxn], b[maxn]; long long ans = 0; int tot; int n, m, q; long long solve() { int l = 1, r = tot; int len = 100; long long val = LINF; while (l < r) { int mid = l + r + 1 >> 1; if (ans - sum[mid] >= 0) l = mid; else r = mid - 1; } val = min(val, abs(ans - sum[l])); if (l + 1 <= tot) val = min(val, abs(ans - sum[l + 1])); return val; } int main() { while (scanf("%d%d%d", &n, &m, &q) != EOF) { ans = 0; long long cur = 1; for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); ans += cur * a[i]; cur *= -1; } sum1[0] = 0; sum2[0] = 0; for (int i = 1; i <= m; i++) { scanf("%I64d", &b[i]); if (i & 1) { if (i == 1) { sum1[i] = b[i]; } else sum1[i] = sum1[i - 2] + b[i]; sum2[i] = sum2[i - 1]; } else { sum2[i] = sum2[i - 2] + b[i]; sum1[i] = sum1[i - 1]; } } for (int i = 1; i <= m - n + 1; i++) { int r = i + n - 1; if (i & 1) { sum[i] = sum1[r] - sum1[i - 1] - (sum2[r] - sum2[i - 1]); } else { sum[i] = sum2[r] - sum2[i - 1] - (sum1[r] - sum1[i - 1]); } } tot = m - n + 1; sort(sum + 1, sum + 1 + tot); print(solve()); while (q--) { int l, r; long long x; scanf("%d%d", &l, &r); scanf("%I64d", &x); if ((r - l + 1) & 1) { if (l & 1) ans += x; else ans -= x; } print(solve()); } } }
13
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 7, inf = 1e9 + 7; int n, s, m, d; int tm(int a, int b) { return a * 60 + b; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; int ans = inf; vector<int> a(n); for (int i = 0; i < n; i++) { int b, c; cin >> b >> c; a[i] = tm(b, c); if (i && a[i - 1] + 2 * s + 1 < a[i]) { ans = min(a[i - 1] + s + 1, ans); } } if (a[0] > s) ans = 0; ans = min(ans, a.back() + s + 1); cout << ans / 60 << " " << ans % 60 << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e9 + 7; const long long int mod = 998244353; long long int maxi(long long int a, long long int b) { if (a >= b) return a; return b; } long long int mini(long long int a, long long int b) { if (a >= b) return b; return a; } int checkper(vector<long long int> A) { sort(A.begin(), A.end()); for (long long int i = 0; i <= A.size() - 1; i++) if (A[i] != i + 1) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long int n; cin >> n; vector<long long int> A(n); A[0] = 0; long long int ma = -2 * n, mi = 2 * n; for (long long int i = 1; i <= n - 1; i++) { cin >> A[i]; A[i] += A[i - 1]; ma = maxi(A[i], ma); mi = mini(A[i], mi); } long long int x, y; if (ma > 0) { x = n - ma; } if (mi < 0) { x = 1 - mi; } if (x > n || x <= 0) cout << "-1"; else { vector<long long int> ans; ans.push_back(x); for (long long int i = 1; i <= n - 1; i++) ans.push_back(x + A[i]); if (checkper(ans)) { for (auto y : ans) cout << y << " "; } else cout << "-1"; } return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long maxx = 1e6 + 5; bool dd[maxx], check, d1[maxx]; long long tr[maxx], val[maxx]; long long b, n, m, k, q, c, f[maxx], x, y, tong = 0; vector<long long> st[maxx]; bool prime(long long v) { if (v < 2) return false; for (int i = 2; i <= v / i; i++) { if (v % i == 0) return false; } return true; } void sol() { long long a[maxx], t; cin >> t; while (t-- > 0) { cin >> n; cout << n - 1 << " " << 1 << '\n'; } } long long uoc(long long a, long long b) { long long r; while (b != 0) { r = a % b; a = b; b = r; } return a; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); sol(); }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e6; const long long mod = 1e9 + 7; const long long inf = 1e9; long long a[610][610], f[2][610][610]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n; cin >> n; for (int i = 0; i <= 310; i++) for (int j = 0; j <= 310; j++) { a[i][j] = -inf; f[0][i][j] = -inf; f[1][i][j] = -inf; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[0][1][1] = a[1][1]; for (int k = 3; k <= n + n; k++) { for (int i = 1; i <= n && i <= k; i++) { for (int j = 1; j <= n && j <= k; j++) { int x = k % 2, y = 1 - x; long long w = -inf; for (int dx = -1; dx <= 0; dx++) for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]); f[x][i][j] = w; if (i != j) f[x][i][j] += a[i][k - i] + a[j][k - j]; else f[x][i][j] += a[i][k - i]; } } } cout << f[0][n][n]; }
12
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int MAX = 2e5 + 4; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n - 1); vector<int> adj[n + 1]; for (int i = 0; i < n - 1; i++) { cin >> a[i]; adj[a[i]].push_back(i + 2); } for (int i = 1; i <= n - 1; i++) { if (adj[i].empty()) continue; int size = adj[i].size(); for (auto x : adj[i]) { if (adj[x].size() != 0) size--; } if (size <= 2) { cout << "No"; return 0; } } cout << "Yes"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, t1, t2, k; cin >> n >> t1 >> t2 >> k; pair<double, int> arr[n]; arr[0].first = 0; arr[0].second = 0; for (int i = 0; i < n; i++) { long long int u1, u2; cin >> u1 >> u2; arr[i] = make_pair(-max(u1 * t1 * (100 - k) + u2 * t2 * 100, u2 * t1 * (100 - k) + u1 * t2 * 100), i + 1); } sort(arr, arr + n); for (int i = 0; i < n; i++) { printf("%d %.2lf\n", arr[i].second, -0.01 * arr[i].first); } return 0; }
4
#include <bits/stdc++.h> long long dp[11][51], count; int z[51]; char q[51]; int lun; int flag(int k, int *x) { int y = 0; for (int i = 0; i < k - 1; i++) { if (x[i] - x[i + 1] > 1 || x[i] - x[i + 1] < -1) { y = 1; break; } } return y; } long long DFS(int n, int r) { if (dp[n][r] != 0) return dp[n][r]; if (r == lun) return 1; int k = n + z[r]; dp[n][r] = dp[n][r] + DFS(k / 2, r + 1); if (k % 2) dp[n][r] = dp[n][r] + DFS(k / 2 + 1, r + 1); return dp[n][r]; } int main() { scanf("%s", q); lun = strlen(q); for (int i = lun - 1; i >= 0; i--) z[i] = q[i] - '0'; for (int k = 0; k <= 9; ++k) count = count + DFS(k, 1); if (!flag(lun, z)) count--; printf("%lld", count); return 0; }
9
#include <bits/stdc++.h> using namespace std; inline long long readint() { long long a = 0; char c = getchar(), f = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -f; for (; '0' <= c && c <= '9'; c = getchar()) a = (a << 3) + (a << 1) + (c ^ 48); return a * f; } const int MaxN = 300005, infty = 1 << 29; long long c[MaxN]; long long s[MaxN]; long long p[MaxN]; int n, a, d[MaxN]; vector<int> v; int main() { n = readint(), a = readint(); for (int i = 1; i <= n; ++i) { d[i] = readint(); c[i] = a - readint() + c[i - 1]; } long long ans = 0; for (int i = 1; i <= n; ++i) ans = max(ans, c[i] - c[i - 1]); v.push_back(0); d[0] = -infty, p[0] = 1ll * infty * infty; for (int i = 1; i < n; ++i) { long long r = d[i + 1] - d[i]; s[i] = c[i - 1]; while (d[v.back() + 1] - d[v.back()] <= r) { s[i] = min(s[i], s[v.back()]); v.pop_back(); } p[i] = min(p[v.back()], s[i] + r * r); ans = max(ans, c[i + 1] - p[i]); v.push_back(i); } printf("%lld\n", ans); return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a; for (auto i : a) if (isdigit(i)) b += i; sort(b.begin(), b.end()); for (int i = 0; i < b.size(); i++) { if (i != 0) cout << "+"; cout << b[i]; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 410; int n, m; int nxt[N][26]; char s[N], t[N]; void init() { for (int j = 0; j <= 25; j++) { nxt[n][j] = n + 1; nxt[n + 1][j] = n + 1; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= 25; j++) nxt[i][j] = nxt[i + 1][j]; nxt[i][s[i + 1] - 'a'] = i + 1; } } char t1[N], t2[N]; int F[N][N]; bool Calc(int n1, int n2) { for (int i = 1; i <= n1; i++) t1[i] = t[i]; for (int i = 1; i <= n2; i++) t2[i] = t[i + n1]; for (int i = 0; i <= n1; i++) for (int j = 0; j <= n2; j++) F[i][j] = n + 1; F[0][0] = 0; for (int i = 0; i <= n1; i++) for (int j = 0; j <= n2; j++) { if (i > 0) F[i][j] = min(F[i][j], nxt[F[i - 1][j]][t1[i] - 'a']); if (j > 0) F[i][j] = min(F[i][j], nxt[F[i][j - 1]][t2[j] - 'a']); } if (F[n1][n2] <= n) return 1; return 0; } int main() { int tCase; cin >> tCase; while (tCase--) { cin >> (s + 1); n = strlen(s + 1); cin >> (t + 1); m = strlen(t + 1); init(); bool ok = 0; for (int n1 = 1; n1 <= m; n1++) if (Calc(n1, m - n1)) { cout << "YES" << endl; ok = 1; break; } if (!ok) cout << "NO" << endl; } }
14
#include <bits/stdc++.h> using namespace std; int main() { long long int ax, ay, bx, by, cx, cy, q; cin >> ax >> ay >> bx >> by >> cx >> cy; q = (bx - ax) * (cy - by) - (by - ay) * (cx - bx); if (q == 0) cout << "TOWARDS\n"; else if (q > 0) cout << "LEFT\n"; else cout << "RIGHT\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, S; cin >> n >> S; int a[n]; for (int i = 0; i <= n - 1; i++) cin >> a[i]; int left = 1, right = n; int middle = (left + right) / 2; int k = 0, T = 0; while (left <= right) { middle = (left + right) / 2; long long int b[n]; for (int i = 0; i <= n - 1; i++) b[i] = (long long)a[i] + (long long)middle * (i + 1); sort(b, b + n); long long int sum = 0; for (int i = 0; i <= middle - 1; i++) sum += b[i]; if (sum <= S) { k = middle; T = sum; left = middle + 1; } else { right = middle - 1; } } cout << k << " " << T; return 0; }
7
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } template <class T1, class T2, class T3, class T4> void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6 << endl; } const int mod = 1000000007; vector<pair<int, int> > rgt; int ara[200], ini[200], vis[200]; vector<int> tp; int calL(int indx) { vector<pair<int, int> > vc = rgt; int cnt = 0; while (1) { int rr = 0; for (int i = vc.size() - 1; i >= 0; i--) { if (vc[i].first > indx || vis[vc[i].first]) continue; int sum = vc[i].second; int d = vc[i].first; int ck = 1; for (int j = d; j <= indx; j++) { sum += ara[j]; if (sum < 0) { ck = 0; break; } } if (ck == 0) continue; for (int j = d; j <= indx; j++) { ara[j] = 0; } vis[vc[i].first] = 1; rr = 1; cnt++; tp.push_back(vc[i].first); break; } if (rr == 0) return cnt; } return -2147348; } int calR(int indx) { vector<pair<int, int> > vc = rgt; int cnt = 0; while (1) { int rr = 0; for (int i = 0; i < vc.size(); i++) { if (vc[i].first < indx || vis[vc[i].first]) continue; int sum = vc[i].second; int d = vc[i].first; int ck = 1; for (int j = d; j >= indx; j--) { sum += ara[j]; if (sum < 0) { ck = 0; break; } } if (ck == 0) continue; for (int j = d; j >= indx; j--) { ara[j] = 0; } cnt++; rr = 1; vis[vc[i].first] = 1; tp.push_back(vc[i].first); break; } if (rr == 0) return cnt; } return -212261; } void init(int n) { for (int i = 1; i <= n; i++) ara[i] = ini[i]; for (int i = 1; i <= n; i++) vis[i] = 0; tp.clear(); } int hero[200]; int main() { int i, j, k, l, m, n, h; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d", &l, &h); rgt.push_back(make_pair(l, h)); hero[l] = i; } sort(rgt.begin(), rgt.end()); for (int i = 1; i <= n; i++) { scanf("%d", &ara[i]); ini[i] = ara[i]; } int ans = -1, s = -1; for (int i = 1; i <= n; i++) { int c = calL(i); int d = calR(i); if (c + d == m) { ans = i; s = 0; break; } init(n); c = calR(i); d = calL(i); if (c + d == m) { ans = i; s = 1; break; } init(n); } cout << ans << endl; if (ans == -1) { return 0; } for (int i = 0; i < tp.size(); i++) cout << hero[tp[i]] << " "; cout << endl; }
15
#include <bits/stdc++.h> using namespace std; char s[100005]; int main() { int num = 0, ans = 0, len; scanf("%s", s); ans = 1; len = strlen(s); num = s[len - 1] - '0'; if (num % 2 == 0) ans += 1; else ans += 4; if (len >= 2) { num = (s[len - 2] - '0') * 10 + s[len - 1] - '0'; if (num % 4 == 0) ans += 1; if (num % 4 == 1) ans += 2; if (num % 4 == 2) ans += 4; if (num % 4 == 3) ans += 3; if (num % 4 == 0) ans += 1; if (num % 4 == 1) ans += 3; if (num % 4 == 2) ans += 4; if (num % 4 == 3) ans += 2; } else { num = s[len - 1] - '0'; if (num % 4 == 0) ans += 1; if (num % 4 == 1) ans += 2; if (num % 4 == 2) ans += 4; if (num % 4 == 3) ans += 3; if (num % 4 == 0) ans += 1; if (num % 4 == 1) ans += 3; if (num % 4 == 2) ans += 4; if (num % 4 == 3) ans += 2; } ans = ans % 5; printf("%d", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, a, b, c; int input(void) { scanf("%d%d%d%d", &n, &a, &b, &c); return 0; } int solve(void) { int x, ans = 0; for (register int y = 0; y <= b; y++) { for (register int z = 0; z <= c; z++) { x = (n - y - (z << 1)) << 1; if (0 <= x && x <= a) ans++; } } printf("%d\n", ans); return 0; } int main(void) { input(); solve(); return 0; }
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:128777216") using namespace std; struct Basket { int id; int val; void init(int i, int n) { this->id = i + 1; val = abs((n + 1) - (i + 1) * 2); } bool operator<(const Basket &b) const { return val < b.val || val == b.val && id < b.id; } } b[100100]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { b[i].init(i, m); } sort(b, b + m); for (int i = 0; i < n; i++) { printf("%d\n", b[i % m].id); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v1(n), v2(n - 1), v3(n - 2); for (int i = 0; i < n; ++i) cin >> v1[i]; for (int i = 0; i < n - 1; ++i) cin >> v2[i]; for (int i = 0; i < n - 2; ++i) cin >> v3[i]; sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); int x1 = 0, x2 = 0; for (int i = 0; i < n - 1; ++i) if (v1[i] != v2[i]) { x1 = v1[i]; break; } if (!x1) x1 = v1[n - 1]; for (int i = 0; i < n - 2; ++i) if (v3[i] != v2[i]) { x2 = v2[i]; break; } if (!x2) x2 = v2[n - 2]; cout << x1 << endl; cout << x2 << endl; }
3
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > ord; bool comp(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) { return a.first > b.first; } return a.second < b.second; } int main() { string entrada; cin >> entrada; for (int i = 0; i < entrada.size(); i++) { ord.push_back(make_pair(entrada[i], i)); } sort(ord.begin(), ord.end(), comp); int ultimo = -1; for (int i = 0; i < ord.size(); i++) { if (ord[i].second > ultimo) { ultimo = ord[i].second; printf("%c", ord[i].first); } } printf("\n"); return 0; }
3
#include <bits/stdc++.h> using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; const int N = 1e5 + 2; int n, a, b, p[N], cntEdge[N]; vector<int> g[2 * N]; map<int, int> mp; void addEdge(int x1, int x2) { g[x1 ^ 1].emplace_back(x2); g[x2 ^ 1].emplace_back(x1); } bool mark[2 * N]; vector<int> st; bool dfs(int u) { if (mark[u]) return 1; if (mark[u ^ 1]) return 0; mark[u] = 1; st.emplace_back(u); for (int v : g[u]) if (!dfs(v)) return 0; return 1; } bool TSAT() { for (int i = (0); i <= (2 * n - 1); ++i) { if (!mark[i] && !mark[i ^ 1]) { st.clear(); if (!dfs(i)) { for (int v : st) mark[v] = 0; st.clear(); if (!dfs(i ^ 1)) return 0; } } i++; } return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> a >> b; for (int i = (0); i <= (n - 1); ++i) cin >> p[i]; set<pair<int, int> > s; for (int i = (0); i <= (n - 1); ++i) mp[p[i]] = i; for (int i = (0); i <= (n - 1); ++i) { int u1 = -1, u2 = -1; if (mp.find(a - p[i]) != mp.end()) cntEdge[i]++, u1 = mp[a - p[i]]; if (mp.find(b - p[i]) != mp.end()) cntEdge[i]++, u2 = mp[b - p[i]]; if (u1 != -1 && cntEdge[u1]) { bool beBoth = (cntEdge[u1] == 2 && cntEdge[i] == 2); if (beBoth) addEdge(i << 1, (u1 << 1) | 1), addEdge((i << 1) | 1, u1 << 1); else addEdge((i << 1) | 1, (i << 1) | 1), addEdge((u1 << 1) | 1, (u1 << 1) | 1); } if (u2 != -1 && cntEdge[u2]) { bool beBoth = (cntEdge[u2] == 2 && cntEdge[i] == 2); if (beBoth) addEdge(i << 1, (u2 << 1) | 1), addEdge((i << 1) | 1, u2 << 1); else addEdge(i << 1, i << 1), addEdge(u2 << 1, u2 << 1); } } for (int i = (0); i <= (n - 1); ++i) if (!cntEdge[i]) { cout << "NO"; return 0; } if (!TSAT()) cout << "NO"; else { cout << "YES\n"; for (int i = (0); i <= (n - 1); ++i) cout << mark[i << 1] << ' '; } return 0; }
12
#include <bits/stdc++.h> using namespace std; long long v[4005], d[4005], p[4005]; long long ans[4005]; long long flag[4005]; int n; int main() { memset(flag, 0, sizeof(flag)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld%lld%lld", &v[i], &d[i], &p[i]); int tot = 0; for (int i = 1; i <= n; i++) { if (p[i] < 0) continue; ans[tot++] = i; long long sum = 0; long long time = v[i]; for (int j = i + 1; j <= n; j++) { flag[j] = 0; if (p[j] >= 0) { flag[j] = 1; if (time > 0) p[j] -= time; time--; } } for (int j = i + 1; j <= n; j++) { if (p[j] >= 0) p[j] -= sum; if (p[j] < 0 && flag[j]) sum += d[j]; } } printf("%d\n", tot); for (int i = 0; i < tot; i++) printf("%lld ", ans[i]); printf("\n"); }
10
#include <bits/stdc++.h> using namespace std; long long mode(long long x, long long m) { return (x % m + m) % m; } long long mod = 1000000007; long double pi = 2 * acos(0.0); bool tt(long double ar[], long double v[], long long n, long double mid) { long double maxi = INT_MIN; long double mini = INT_MAX; for (long long i = 0; i < n; i++) { long double l = ar[i] - (v[i] * mid); long double r = ar[i] + (v[i] * mid); if (l > maxi) maxi = l; if (r < mini) mini = r; } if (maxi <= mini) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; long double ar[n], v[n]; for (long long i = 0; i < n; i++) cin >> ar[i]; for (long long i = 0; i < n; i++) cin >> v[i]; long double l = 0; long double r = 1e15; long double ans = 0; while (r - l >= 0.0000000001) { long double mid = (l + r) / (2.0); if (tt(ar, v, n, mid)) { ans = mid; r = mid; } else l = mid; } cout << setprecision(10) << fixed << ans; }
8
#include <bits/stdc++.h> using namespace std; template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; namespace input { template <class T> void re(complex<T>& x); template <class T1, class T2> void re(pair<T1, T2>& p); template <class T> void re(vector<T>& a); template <class T, size_t SZ> void re(array<T, SZ>& a); template <class T> void reA(T A[], int sz); template <class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } void re(long double& x) { string t; re(t); x = stold(t); } template <class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& a) { for (int i = 0; i < ((int)a.size()); i++) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ>& a) { for (int i = 0; i < (SZ); i++) re(a[i]); } template <class T> void reA(T A[], int sz) { for (int i = 0; i < (sz); i++) re(A[i]); } void setupIO(const string& PROB = "") { ios::sync_with_stdio(false); cin.tie(nullptr); if (PROB.length() != 0) { ifstream infile(PROB + ".in"); if (infile.good()) { freopen((PROB + ".in").c_str(), "r", stdin); freopen((PROB + ".out").c_str(), "w", stdout); } } } } // namespace input using namespace input; namespace output { template <class T1, class T2> void prD(const pair<T1, T2>& x); template <class T, size_t SZ> void prD(const array<T, SZ>& x); template <class T> void prD(const vector<T>& x); template <class T> void prD(const set<T>& x); template <class T1, class T2> void prD(const map<T1, T2>& x); template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T, size_t SZ> void pr(const array<T, SZ>& x); template <class T> void pr(const vector<T>& x); template <class T> void pr(const set<T>& x); template <class T1, class T2> void pr(const map<T1, T2>& x); template <class T> void prD(const T& x) { cout << x; cout.flush(); } template <class Arg, class... Args> void prD(const Arg& first, const Args&... rest) { prD(first); prD(rest...); } template <class T1, class T2> void prD(const pair<T1, T2>& x) { prD("{", x.first, ", ", x.second, "}"); } template <class T> void prDContain(const T& x) { prD("{"); bool fst = 1; for (const auto& a : x) prD(!fst ? ", " : "", a), fst = 0; prD("}"); } template <class T, size_t SZ> void prD(const array<T, SZ>& x) { prDContain(x); } template <class T> void prD(const vector<T>& x) { prDContain(x); } template <class T> void prD(const set<T>& x) { prDContain(x); } template <class T1, class T2> void prD(const map<T1, T2>& x) { prDContain(x); } void psD() { prD("\n"); } template <class Arg> void psD(const Arg& first) { prD(first); psD(); } template <class Arg, class... Args> void psD(const Arg& first, const Args&... rest) { prD(first, " "); psD(rest...); } template <class T> void pr(const T& x) { cout << x; } template <class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr(x.first, " ", x.second); } template <class T> void prContain(const T& x) { bool fst = 1; for (const auto& a : x) pr(!fst ? " " : "", a), fst = 0; } template <class T, size_t SZ> void pr(const array<T, SZ>& x) { prContain(x); } template <class T> void pr(const vector<T>& x) { prContain(x); } template <class T> void pr(const set<T>& x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2>& x) { prContain(x); } void ps() { pr("\n"); } template <class Arg> void ps(const Arg& first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first, " "); ps(rest...); } } // namespace output using namespace output; int n, m, A[5000], F[5000], H[5000]; int L[5000], R[5000]; int lCows[5000], rCows[5000]; long long ans = 0, ways = 0; void addAns(int num, long long w) { if (num == ans) ways = (ways + w) % (int)(1e+9 + 7); else if (num > ans) { ans = num; ways = w; } } void solve(int firstCowFromLeft) { for (int i = 0; i < 5000; i++) lCows[i] = 0; ; for (int i = 0; i < 5000; i++) rCows[i] = 0; ; int leftMost = (firstCowFromLeft == -1 ? -1 : L[firstCowFromLeft]); for (int i = 0; i < (m); i++) { if (L[i] != -1 && L[i] <= leftMost && (F[i] != F[firstCowFromLeft] || i == firstCowFromLeft)) lCows[F[i]]++; if (i != firstCowFromLeft && R[i] > leftMost) rCows[F[i]]++; } int ans = 0; long long ways = 1; for (int i = 0; i < (n); i++) { if (firstCowFromLeft != -1 && i == F[firstCowFromLeft]) { if (rCows[i] == 0) { ans++; } else { ans += 2; ways = (ways * rCows[i]) % (int)(1e+9 + 7); } } else { long long opt1 = min(lCows[i], rCows[i]) * (max(lCows[i], rCows[i]) - 1); long long opt2 = lCows[i] + rCows[i]; if (opt1 != 0) { ans += 2; ways = (ways * opt1) % (int)(1e+9 + 7); } else if (opt2 != 0) { ans++; ways = (ways * opt2) % (int)(1e+9 + 7); } } } addAns(ans, ways); } int main() { setupIO(); re(n, m); for (int i = 0; i < (n); i++) { int x; re(x); A[i] = x - 1; } for (int i = 0; i < (m); i++) { cin >> F[i] >> H[i]; F[i]--; } for (int i = 0; i < (m); i++) { int ctLeft = H[i]; for (int j = 0; j < (n); j++) { if (A[j] == F[i]) ctLeft--; if (ctLeft == 0) { L[i] = j; break; } } if (ctLeft > 0) L[i] = -1; ctLeft = H[i]; for (int j = (n)-1; j >= 0; j--) { if (A[j] == F[i]) ctLeft--; if (ctLeft == 0) { R[i] = j; break; } } if (ctLeft > 0) R[i] = -1; } solve(-1); for (int i = 0; i < (m); i++) { if (L[i] != -1) { solve(i); } } ps(ans, ways); return 0; }
17
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(0); int n, d, seq[110]; cin >> n >> d; for (int i = 0; i < n; ++i) { cin >> seq[i]; } sort(seq, seq + n); int cnt = 0, start = 0, end = 0; while (end < n) { if (seq[end] - seq[start] <= d) { cnt = max(cnt, end - start + 1); ++end; } else ++start; } cout << n - cnt << endl; return 0; }
4
#include <bits/stdc++.h> struct Show { int l; int r; }; bool order(Show a, Show b) { return a.l < b.l; } struct My { int time; int num; }; bool operator<(const My &a, const My &b) { return a.time > b.time; }; int main() { int n; std::cin >> n; std::vector<Show> shows(n); std::priority_queue<My> heap; for (int i = 0; i < n; ++i) std::cin >> shows[i].l >> shows[i].r; std::sort(shows.begin(), shows.end(), order); for (int i = 0; i < 2; ++i) { My my; my.time = -1; my.num = i; heap.push(my); } for (int i = 0; i < n; ++i) { auto tv = heap.top(); heap.pop(); if (tv.time >= shows[i].l) { std::cout << "NO\n"; return 0; } tv.time = shows[i].r; heap.push(tv); } std::cout << "YES\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; int nxt[3000000][2]; int val[3000000]; int pw[27]; int tot; void upd(int x, int value) { int cur = 0; for (int i = 26; i >= 0; --i) { int d = x & pw[i]; if (d > 0) d = 1; if (!nxt[cur][d]) nxt[cur][d] = ++tot; cur = nxt[cur][d]; val[cur] += value; } } int query(int x, int p) { int cur = 0, ans = 0; for (int i = 26; i >= 0; --i) { int d = x & pw[i], d2 = p & pw[i]; if (d > 0) d = 1; if (d2) ans += val[nxt[cur][1 - d]]; if (!nxt[cur][d]) nxt[cur][d] = ++tot; cur = nxt[cur][d]; } return ans; } int main() { int q; scanf("%d", &q); pw[0] = 1; for (int i = 1; i < 27; ++i) pw[i] = pw[i - 1] << 1; while (q--) { int tag; scanf("%d", &tag); int p, l; scanf("%d", &p); if (tag == 1) upd(p, 1); if (tag == 2) upd(p, -1); if (tag == 3) { scanf("%d", &l); printf("%d\n", query(l ^ p, l)); } } return 0; }
12
#include <bits/stdc++.h> using namespace std; inline int read() { register int x = 0, f = 1; register char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = 0; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch ^ '0'); ch = getchar(); } return f ? x : -x; } const int N = 1e5 + 5, _ = N * 11, mod = 1e9 + 7; int n, m, head[_], cnt, dis[_], vis[_]; vector<int> E[_][10]; vector<vector<int> > q, tmp; inline void add(int u, int v, int w) { while (w > 9) { ++cnt; E[cnt][w % 10].push_back((v)); w /= 10; v = cnt; } E[u][w].push_back((v)); } int main() { n = read(), m = read(); cnt = n; for (int i = (1), _ed = (m); i <= _ed; ++i) { int u = read(), v = read(); add(u, v, i), add(v, u, i); } vis[1] = 1; vector<int> nw; nw.push_back((1)); q.push_back((nw)); while (!q.empty()) { for (vector<int> i : q) for (int c = (0), _ed = (9); c <= _ed; ++c) { vector<int>().swap(nw); for (int u : i) for (int v : E[u][c]) { if (vis[v]) continue; vis[v] = 1; dis[v] = (1ll * dis[u] * 10 + c) % mod; nw.push_back((v)); } if (!nw.empty()) tmp.push_back((nw)); } q.swap(tmp); vector<vector<int> >().swap(tmp); } for (int i = (2), _ed = (n); i <= _ed; ++i) printf("%d\n", dis[i]); return 0; }
18
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 1e5 + 6; const long long mx = 1e9; int dp[410][410]; string s, t; void solve() { int n, m; cin >> s >> t; n = s.length(); m = t.length(); for (int k = 0; k <= m - 1; k++) { int y = m - k; memset(dp, -1, sizeof(dp)); dp[0][0] = 0; for (int i = 0; i <= n - 1; i++) for (int j = 0; j <= k; j++) { if (dp[i][j] == -1) continue; if (j < k && s[i] == t[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]); dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (t[dp[i][j] + k] == s[i])); } if (dp[n][k] == y) { puts("YES"); return; } } puts("NO"); return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int _; cin >> _; while (_--) solve(); return 0; }
14
#include <bits/stdc++.h> using namespace std; const int N = 200002; int n, m, k; bool b[N]; struct node { int id; long long w; } a[N]; inline int read() { int x = 0, k = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') k = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - 48, c = getchar(); return x * k; } bool cmp(const node &p, const node &q) { if (p.w != q.w) return p.w > q.w; return p.id < q.id; } int main() { n = read(), m = read(), k = read(); for (int i = 1; i <= n; ++i) a[i].w = read(), a[i].id = i; sort(a + 1, a + 1 + n, cmp); long long ans = 0; for (int i = 1; i <= m * k; ++i) ans += a[i].w, b[a[i].id] = 1; printf("%lld\n", ans); for (int i = 1, cnt, tot = 0; i <= n; ++i) { cnt = 0; while (i <= n && cnt < m) { if (b[i]) ++cnt; ++i; } ++tot, --i; if (tot != k && i != n) printf("%d ", i); } puts(""); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; for (long long int i = 0; i < n; i++) { long long int a, x; cin >> a; x = a; vector<int> v; for (long long int i = 2; i <= sqrt(a); i++) { if (a % i == 0) { if (a / i == i) v.push_back(i); else { v.push_back(i); v.push_back(a / i); } a = a / i; } } if (v.size() < 2) cout << "NO" << '\n'; else { sort(v.begin(), v.end()); if (x / (v[0] * v[1]) > 1 && x / (v[0] * v[1]) != v[0] && x / (v[0] * v[1]) != v[1]) { cout << "YES" << "\n"; cout << v[0] << ' ' << v[1] << ' ' << x / (v[0] * v[1]) << '\n'; } else cout << "NO" << '\n'; } } }
5