solution
stringlengths
52
181k
difficulty
int64
0
6
#include <cstdio> using namespace std; int main(){ double a,b,c,d,e,f; double x,y; while(scanf("%lf %lf %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f) != EOF){ y = (f - (d * c) / a) / ((d * -1 * b) / a + e); x = (c - b * y) / a; printf("%.3f %.3f\n",x,y); } return(0); }
0
#include <bits/stdc++.h> using namespace std; template <class T> void printvec(vector<T>& x, int a, int b) { for (int i = (a); i <= (b); i++) cout << x[i] << " "; cout << "\n"; } const double PI = acos((double)-1); const double EPSILON = 1e-7; const int N = 1e6 + 1; const int NN = 1e3 + 1; const long long MOD = 1e9 + 7; const long long oo = LLONG_MAX; const int BASE = 10000; const int di[4] = {-1, 0, 1, 0}; const int dj[4] = {0, 1, 0, -1}; void query() { int n, k; cin >> n >> k; vector<int> a(n + 1), c(k + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= k; i++) { cin >> c[i]; } sort(1 + (a).begin(), (a).end()); vector<vector<int>> res(n + 1); vector<int> cnt(n + 1); for (int i = n; i; i--) { int l = 1, r = n; while (l < r) { int mid = (l + r) / 2; if (cnt[mid] >= c[a[i]]) { l = mid + 1; } else { r = mid; } } res[l].push_back(a[i]); cnt[l]++; } int ans = 0; for (int i = 1; i <= n; i++) { ans += (cnt[i] > 0); } cout << ans << "\n"; for (int i = 1; i <= n; i++) { if (!(int)(res[i]).size()) { continue; } cout << (int)(res[i]).size() << " "; for (int x : res[i]) { cout << x << " "; } cout << "\n"; } } void solve() { int t = 1; while (t--) { query(); } } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cout << fixed << setprecision(4); solve(); cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int solve(int a, int b) { if (a - b == 0) return printf("infinity\n"); if (a - b < 0 || b > a - b) return printf("0\n"); int i = 2, n = a - b, res = 1; while (i < n && i * i <= (a - b) && n > 1) { int rr = 0; while (n % i == 0) { rr++; n /= i; } res *= (rr + 1); i++; } if (n > 1) res *= 2; if (b > 0) res--; i = 2; n = a - b; while (i * i <= n && i <= b) { if (n % i == 0) { res--; res -= (n / i) <= b && n / i != i; } i++; } cout << res << endl; } int main() { int a, b; cin >> a >> b; solve(a, b); return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") using namespace std; const int maxn = 1000 + 5; const int mod = 998244353; int dp[maxn][maxn], a[maxn], ans, n, k; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", a + i); sort(a + 1, a + n + 1); auto solve = [&](int x) { dp[0][0] = 1; int tmp = 0; for (int i = 1; i <= n; i++) { while (tmp + 1 <= i && a[i] - a[tmp + 1] >= x) ++tmp; for (int j = 0; j <= k; j++) dp[i][j] = dp[i - 1][j]; for (int j = 1; j <= k; j++) dp[i][j] = (dp[i][j] + dp[tmp][j - 1]) % mod; } return dp[n][k]; }; for (int i = 1; i <= (1e5 / (k - 1)); i++) ans = (ans + solve(i)) % mod; printf("%d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; int64_t n, a, b, da, db, c, d; vector<int> v[100005]; bool dis(int x, int p, int d) { if (x == b) return d <= da; bool ans = false; for (auto i : v[x]) if (i != p) ans |= dis(i, x, d + 1); return ans; } int maxDis(int x, int p) { int d = 0; for (auto i : v[x]) if (i != p) d = max(d, maxDis(i, x)); return d + 1; } int findNode(int x, int p, int d, int req) { if (d == req) return x; int found; for (auto i : v[x]) if (i != p) { found = findNode(i, x, d + 1, req); if (found != -1) return found; } return -1; } int diameter() { int w = maxDis(1, -1); int node = findNode(1, -1, 0, w - 1); return maxDis(node, -1) - 1; } int main() { int t; cin >> t; while (t--) { cin >> n >> a >> b >> da >> db; for (int i = 1; i < n; i++) { cin >> c >> d; v[d].push_back(c); v[c].push_back(d); } if (db < 2 * da + 1) cout << "Alice\n"; else if (dis(a, -1, 0)) cout << "Alice\n"; else if (diameter() < 2 * da + 1) cout << "Alice\n"; else cout << "Bob\n"; for (int i = 1; i <= n; i++) v[i].clear(); } return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2,fma") using namespace std; const long long sz = 3e5 + 10; pair<long long, long long> ara[sz], lim; multiset<long long> lst; int main() { long long n; cin >> n; for (long long i = 1; i <= n; i++) { if (i == 1) { scanf("%lld", &lim.first), scanf("%lld", &lim.second); lim.second = lim.second - lim.first + 1; } else { scanf("%lld", &ara[i].first), scanf("%lld", &ara[i].second); ara[i].second = ara[i].second - ara[i].first + 1; } } sort(ara + 2, ara + n + 1, greater<pair<long long, long long> >()); long long ans = (1LL << 62), ptr = 2, chk = 1; while (chk) { for (; ptr <= n; ptr++) { if (ara[ptr].first <= lim.first) break; lst.insert(ara[ptr].second); } ans = min(ans, (long long)lst.size() + 1); chk = 0; if (lst.empty()) continue; long long f = *(lst.begin()); if (f <= lim.first) { lim.first -= f; lst.erase(lst.find(f)); chk = 1; } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; ostream cnull(NULL); template <class TH> void _dbg(const char *sdbg, TH h) { cnull << sdbg << "=" << h << "\n"; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cnull << *sdbg++; cnull << "=" << h << ","; _dbg(sdbg + 1, a...); } const int MX = 100007, D = 17, BLEP = 1 << D; vector<pair<int, int> > in, ska; map<pair<int, int>, int> mapa; vector<int> pyt[MX]; vector<tuple<int, int, int> > queries[BLEP << 1]; stack<tuple<int, int, int, int, int> > rollback; pair<int, int> kraw[MX]; bool odp[MX], F; struct dsu { int rep[MX], roz[MX], dis[MX]; void init() { for (int i = (0); i <= ((int)(MX)-1); ++i) rep[i] = i, roz[i] = 1; } int Find(int x, int &d) { d += dis[x]; if (x != rep[x]) return Find(rep[x], d); return x; } bool Union(int a, int b) { int dA = 0, dB = 0; if ((a = Find(a, dA)) == (b = Find(b, dB))) { if ((dA + dB) % 2 == 0) F = 1; return 0; } if (roz[a] < roz[b]) swap(a, b); rollback.push({a, roz[a], b, dis[b], rep[b]}); rep[b] = a; roz[a] += roz[b]; dis[b] = dA + dB + 1; return 1; } } FU; void dnc(int v, int l, int r) { int m = (l + r) / 2; int ops = 0; bool cykl = F; for (auto q : queries[v]) { int a = get<0>(q), b = get<1>(q), nrk = get<2>(q); if (l >= a && r <= b) { int x = kraw[nrk].first, y = kraw[nrk].second; ops += FU.Union(x, y); } else if (l < r) { if (a <= m) queries[2 * v].push_back(q); if (b > m) queries[2 * v + 1].push_back(q); } } if (l == r && l < MX) odp[l] = F; else if (l < r) { dnc(2 * v, l, m); dnc(2 * v + 1, m + 1, r); } for (int i = (0); i <= ((int)(ops)-1); ++i) { int a = get<0>(rollback.top()), rozA = get<1>(rollback.top()), b = get<2>(rollback.top()), kolB = get<3>(rollback.top()), repB = get<4>(rollback.top()); FU.rep[b] = repB; FU.roz[a] = rozA; FU.dis[b] = kolB; rollback.pop(); } F = cykl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; for (int i = (1); i <= (q); ++i) { int a, b; cin >> a >> b; if (a > b) swap(a, b); in.push_back({a, b}); } ska = in; sort((ska).begin(), (ska).end()); ska.resize(distance(ska.begin(), unique((ska).begin(), (ska).end()))); for (int i = (0); i <= ((int)(((int)(ska).size())) - 1); ++i) { mapa[ska[i]] = i + 1; kraw[i + 1] = ska[i]; } for (int i = (0); i <= ((int)(q)-1); ++i) pyt[mapa[in[i]]].push_back(i + 1); int m = ((int)(ska).size()); for (int i = (1); i <= (m); ++i) { if (((int)(pyt[i]).size()) % 2 == 1) pyt[i].push_back(q + 1); for (int j = (0); j <= ((int)(((int)(pyt[i]).size())) - 1); ++j) if (j % 2 == 0) queries[1].push_back({pyt[i][j], pyt[i][j + 1] - 1, i}); } FU.init(); dnc(1, 0, BLEP - 1); for (int i = (1); i <= (q); ++i) cout << (odp[i] ? "NO" : "YES") << '\n'; }
6
#include <iostream> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <utility> #include <functional> #include <stack> #include <queue> #include <map> #include <set> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <deque> #include <ctime> using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define pb push_back #define mp make_pair #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() #define fi first #define se second #define println(X) cout<<X<<endl; #define DBG(X) cout<<#X<<" : "<<X<<endl; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; const ll MOD = 1e9 + 7; double EPS = 1e-8; const double PI = acos(-1); struct Vector2{ double x, y; Vector2(){}; Vector2(double x, double y) : x(x), y(y){}; double cross(const Vector2& p){ return x*p.y - y*p.x; } }; int main(){ int n, c = 0; while(cin>>n && n && ++c){ vector<Vector2> v; rep(i, n){ double x, y; cin>>x>>y; v.pb({x, y}); } double sum = 0.0; v.pb(v[0]); rep(i, n){ sum += v[i].cross(v[i+1]); } printf("%d %.1lf\n", c, -sum/ 2.0); } }
0
#include <bits/stdc++.h> using namespace std; int n; int box[101]; int aox[101]; int minn = 2147480000; int maxn = -1; int gcd(int a, int b) { while (b != 0) { int r = b; b = a % b; a = r; } return a; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> box[i]; maxn = max(box[i], maxn); } sort(box + 1, box + 1 + n); for (int i = 1; i <= n; i++) { aox[i] = box[i] - box[i - 1]; } int a = gcd(aox[1], aox[2]); for (int i = 3; i <= n; i++) { a = gcd(a, aox[i]); } int b = maxn - a; b = b / a + 1; int c = b - n; if (c % 2 == 0) cout << "Bob" << endl; else cout << "Alice" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; char c = getchar(); int sgn = 1; while (c < '0' || c > '9') { if (c == '-') sgn = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); x *= sgn; } template <typename T> void out(T x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) out(x / 10); putchar(x % 10 + '0'); } const int N = 1e5 + 5; int a[N]; int sum[11][N]; char s[N]; int main() { int n, k, w; read(n); read(k); read(w); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { a[i] = a[i - 1] + (s[i] - '0'); sum[i % k][i] = (s[i] - '0'); } for (int i = 0; i < k; i++) for (int j = 1; j <= n; j++) sum[i][j] += sum[i][j - 1]; while (w--) { int l, r; read(l); read(r); int ans = a[r] - a[l - 1] + (r - l + 1) / k - 2 * (sum[(l - 1) % k][r] - sum[(l - 1) % k][l - 1]); out(ans); puts(""); } return 0; }
3
#include<bits/stdc++.h> using namespace std; using ll=long long; static const ll mod=998244353; ll modpow(ll x,ll y){ if(y==0) return 1; else if(y%2==0){ ll z=modpow(x,y/2); return (z*z)%mod; }else{ ll z=modpow(x,y/2); return (x*((z*z)%mod))%mod; } } ll N; ll fac[10000005]; ll rev[10000005]; ll inf[10000005]; int main(){ cin>>N;N/=2; fac[0]=1;rev[0]=1; for(ll i=1;i<=N;i++){ fac[i]=(i*fac[i-1])%mod; rev[i]=modpow(fac[i],mod-2); }ll a=1; inf[0]=1; for(ll i=1;i<=N;i++){ a=(a*2)%mod; inf[i]=(a*((fac[N]*((rev[N-i]*rev[i])%mod))%mod))%mod; }ll ans=0;ll sum=0; for(ll i=0;i<N;i++){ sum=(sum+inf[i])%mod; ans=(ans+((inf[N-1-i]*sum)%mod))%mod; } ll all=modpow(9,N); ans=(all-2*ans+2*mod)%mod; cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct Query { int a, t, x, n; Query() {} bool operator<(const Query &b) const { return x < b.x; } }; int n; vector<Query> queries; vector<int> ans; int ft[100005]; void add(int i, int a) { for (; i < n; i |= i + 1) ft[i] += a; } int sum(int i) { int ret = 0; for (; i >= 0; i = (i & (i + 1)) - 1) ret += ft[i]; return ret; } void read() { cin >> n; queries.resize(n); int t, s = 0; vector<pair<int, int> > ts; for (int i = 0; i < n; ++i) { cin >> queries[i].a >> t >> queries[i].x; ts.emplace_back(t, i); if (queries[i].a == 3) { queries[i].n = s++; ans.emplace_back(0); } } sort(ts.begin(), ts.end()); queries[ts[0].second].t = 0; for (int i = 1; i < n; ++i) queries[ts[i].second].t = queries[ts[i - 1].second].t + int(ts[i].first != ts[i - 1].first); } void print() { for (auto &q : queries) { cout << q.a << " " << q.t << " " << q.x << " " << q.n << endl; } } int main() { ios::sync_with_stdio(0); read(); stable_sort(queries.begin(), queries.end()); int pre = -1; for (int i = 0; i < n; ++i) { if (queries[i].x != pre) { memset(ft, 0, sizeof ft); pre = queries[i].x; } if (queries[i].a == 1) add(queries[i].t, 1); else if (queries[i].a == 2) add(queries[i].t, -1); else if (queries[i].a == 3) ans[queries[i].n] = sum(queries[i].t); } for (auto x : ans) cout << x << endl; }
5
#include <bits/stdc++.h> using namespace std; long long n, k[111], c[111], t, p[111]; pair<long long, long long> q[111]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> q[i].second >> q[i].first; cin >> t; for (int i = 0; i < t; i++) cin >> p[i]; sort(q, q + n); p[t] = 1000000007LL * 1000000007LL; long long ans = 0, cur = 0, B = 0; for (int i = 0; i < t + 1; i++) { if (cur == n) break; if (B + q[cur].second <= p[i]) { ans += q[cur].second * q[cur].first * (i + 1); B += q[cur].second; cur++; i--; } else { ans += (p[i] - B) * q[cur].first * (i + 1); q[cur].second -= p[i] - B; B = p[i]; } } cout << ans; }
3
#include <bits/stdc++.h> using namespace std; namespace io { const int __SIZE = (1 << 21) + 1; char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; } inline void gc(char &x) { x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); } inline void pc(char x) { *oS++ = x; if (oS == oT) flush(); } inline void pstr(const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc(s[__f]); } inline void gstr(char *s) { for (__c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); __c < 32 || __c > 126 || __c == ' ';) __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); for (; __c > 31 && __c < 127 && __c != ' '; ++s, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) *s = __c; *s = 0; } template <class I> inline bool gi(I &x) { _eof = 0; for (__f = 1, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); (__c < '0' || __c > '9') && !_eof; __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) { if (__c == '-') __f = -1; _eof |= __c == EOF; } for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; } template <class I> inline void print(I x) { if (!x) pc('0'); if (x < 0) pc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) pc(qu[qr--]); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io::gc; using io::gi; using io::gstr; using io::pc; using io::print; using io::pstr; using ll = long long; using pii = pair<int, int>; const ll MOD = 1000000007; ll R[500005], D[500005], _D[500005], cnt[500005]; bool has[500005]; vector<pii> elist[500005]; void link(ll fa, ll ch, ll w) { if (cnt[ch]) { cnt[fa] += cnt[ch]; ll c = D[ch] - w; if (c < D[fa]) { _D[fa] = D[fa]; D[fa] = c; } else _D[fa] = min(_D[fa], c); R[fa] += R[ch] + 2 * w; } } void cut(ll fa, ll ch, ll w) { if (cnt[ch]) { cnt[fa] -= cnt[ch]; ll c = D[ch] - w; if (c == D[fa]) D[fa] = _D[fa]; R[fa] -= R[ch] + 2 * w; } } void dfs(ll u, ll fa) { cnt[u] = has[u]; R[u] = D[u] = _D[u] = 0; for (pii &v : elist[u]) { if (v.first == fa) continue; dfs(v.first, u); link(u, v.first, v.second); } } ll ans[500005]; void reroot(ll u, ll fa) { ll pr = R[u], pd = D[u], p_d = _D[u], pcnt = cnt[u]; ans[u] = R[u] + D[u]; for (pii &v : elist[u]) { if (v.first == fa) continue; cut(u, v.first, v.second); link(v.first, u, v.second); reroot(v.first, u); R[u] = pr, D[u] = pd, _D[u] = p_d, cnt[u] = pcnt; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); ll N, K; gi(N), gi(K); for (ll i = (0); i < (N - 1); i++) { ll u, v; gi(u), gi(v); elist[u].push_back({v, 1}); elist[v].push_back({u, 1}); } for (ll i = (0); i < (K); i++) { ll v; gi(v); has[v] = 1; } dfs(1, -1); reroot(1, -1); ll stest = 0xFFFFFFF, at = 0; for (ll i = (1); i < ((N) + 1); i++) { if (ans[i] < stest) { stest = ans[i]; at = i; } } print(at); pc('\n'); print(stest); }
4
#include <bits/stdc++.h> using namespace std; using ii = long long; using pi = pair<int, int>; int main() { int t; cin >> t; while (t--) { int a, b, c; cin >> c >> a >> b; int x, y; cin >> x >> y; pi A = {x, a}; pi B = {y, b}; if (B > A) swap(A, B); int r = 0; r += A.first * min(c / 2, A.second); c -= 2 * min(c / 2, A.second); r += B.first * min(c / 2, B.second); cout << r << "\n"; } }
1
#include<bits/stdc++.h> using namespace std; #define vi vector<int> #define vii vector<pair<int, int> > #define fi first #define se second int x[100005]; int sps[100005][20]; signed main() { int n, q, L; cin>>n; for(int i=1; i<=n; i++) { cin>>x[i]; // for(int d=0; d<=19; d++) sps[i][d]=n+1; } cin>>L>>q; int p=n; for(int i=n; i>=1; i--) { while(x[p]-x[i]>L) p--; sps[i][0]=p; // cout<<i<<" "<<p<<endl; } for(int d=1; d<=19; d++) { for(int i=1; i+(1<<d)<=n; i++) sps[i][d]=sps[sps[i][d-1]][d-1]; } while(q--) { int l, r, ans=0; cin>>l>>r; if(l>r) swap(l, r); for(int i=19; i>=0; i--) { if(sps[l][i]<r and sps[l][i]) { l=sps[l][i]; ans+=(1<<i); } } ans++; cout<<ans<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int const mod = (1e9) + 7; int const N = (1e5) + 5; long long power(long long a, long long deg) { long long res = 1; while (deg) { if (deg % 2 == 0) { a = (a * a) % mod; deg /= 2; } else { res = (res * a) % mod; deg--; } } return res; } int main() { int k; cin >> k; int even = 1; long long deg = 2; for (int i = 0; i < k; ++i) { long long a; scanf("%I64d", &a); even *= (a % 2 == 0 ? 0 : 1); deg = (power(deg, a)) % mod; } deg = (deg * power(2, mod - 2)) % mod; long long p = deg; if (!even) p = (p + 1) % mod; else p = (p - 1) % mod; p = (p * power(3, mod - 2)) % mod; long long q = deg; cout << p << "/" << q; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; struct P { long long int x, y, z; }; void solve() { long long int n; cin >> n; vector<P> v(n); vector<bool> flag(n); for (long long int i = 0; i < n; i++) { cin >> v[i].x >> v[i].y >> v[i].z; } for (long long int i = 0; i < n; i++) { while (i < n && flag[i]) i++; if (i == n) break; long long int an1 = i, an2 = -1; for (long long int j = i + 1; j < n; j++) { if (!flag[j]) { if (an2 == -1) { an2 = j; } else { if (min(v[an1].x, v[an2].x) <= v[j].x && (v[j].x <= max(v[an1].x, v[an2].x))) { if (min(v[an1].y, v[an2].y) <= v[j].y && (v[j].y <= max(v[an1].y, v[an2].y))) { if (min(v[an1].z, v[an2].z) <= v[j].z && (v[j].z <= max(v[an1].z, v[an2].z))) { an2 = j; } } } } } } cout << an1 + 1 << " " << an2 + 1 << "\n"; flag[an1] = 1; flag[an2] = 1; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; clock_t launch = clock(); while (t--) { solve(); } clog << ((long double)(clock() - launch) / CLOCKS_PER_SEC) << "\n"; }
3
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); return f ? -x : x; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } template <typename T> void print(T x, char let) { print(x), putchar(let); } int main() { int n = read(), m = 1; if (m) print((n + 1) / 2, '\n'); else { if (n & 1) print(n / 2, '\n'); else { int x = 1; while (x <= n) x <<= 1; x >>= 1; print((n - x) / 2, '\n'); } } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> par; UnionFind(int n) : par(n) { for (int i = 0; i < n; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { if (root(x) == root(y)) return; par[root(x)] = root(y); } bool same(int x, int y) { return root(x) == root(y); } }; int main() { int n, m; cin >> n >> m; vector<int> p(n); for (int i = 0; i < n; i++) cin >> p[i]; UnionFind tree(n); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; tree.unite(x-1, y-1); } int cnt = 0; for (int i = 0; i < n; i++) { if (tree.same(i, p[i]-1)) cnt++; } cout << cnt << endl; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; #define debug(x) cerr << "[(" << __LINE__ << ") " << #x << "]: " << (x) << endl; int sz; void upd(vector<int>& seg, int l, int r, int val) { for (l+=sz, r+=sz; l<r; l/=2, r/=2) { if (l&1) seg[l] = min(seg[l], val), l++; if (r&1) --r, seg[r] = min(seg[r], val); } } int query(vector<int>& seg, int i) { int ans; for (ans=seg[i+=sz]; i/=2; ) ans = min(ans, seg[i]); return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, q; cin >> n >> q; sz = 1 << (32-__builtin_clz(n-1)); vector<int> seg(2*sz, n-1), seg2(2*sz, n-1); ll ans = (n-2) * (n-2); for (int i=0; i<q; i++) { int x, y; cin >> x >> y; if (x == 1) { int duh = query(seg, y-1); ans -= duh-1; upd(seg2, 0, duh, y-1); } else { int duh = query(seg2, y-1); ans -= duh-1; upd(seg, 0, duh, y-1); } } cout << ans << "\n"; }
0
#include <bits/stdc++.h> using namespace std; const long long int M = 998244353; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res % p; } long long int modinver(long long int a, long long int b) { return (power(a, 1, M) * power(b, M - 2, M)) % M; } void SieveOfEratosthenes(int n) { bool prime[10005]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } std::vector<long long int> v[300002]; long long int visited[300002]; long long int oddev[300002]; long long int odd = 0, even = 0; long long int flag = 0; void dfs(long long int x, long long int ty) { visited[x] = 1; oddev[x] = ty; if (ty == 0) even++; else odd++; for (int i = 0; i < v[x].size(); ++i) { if (visited[v[x][i]] == 0) { dfs(v[x][i], 1 - ty); } else { if (oddev[v[x][i]] == oddev[x]) flag = 1; } } } long long int a[300002], b[300002]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n, m; cin >> n >> m; for (int i = 0; i <= n; ++i) { v[i].clear(); visited[i] = 0; oddev[i] = -1; } for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; v[a[i]].push_back(b[i]); v[b[i]].push_back(a[i]); } long long int ans = 1; flag = 0; for (int i = 1; i <= n; ++i) { if (visited[i] == 0) { dfs(i, 0); ans = (ans % M * (power(2, odd, M) + power(2, even, M)) % M) % M; odd = 0; even = 0; } } if (flag) { cout << "0\n"; continue; } cout << ans % M << '\n'; } }
4
#include <bits/stdc++.h> #define base 950527 #define base2 1777771 #define N 1001 using namespace std; typedef unsigned long long ull; ull a[N][N],b[N][N],A[N][N],tmp[N][N],ans; void Reverse(int p){for(int i=0;i<p;i++)reverse(b[i],b[i]+p);} void rol_90(int p){ for(int i=0;i<p;i++) for(int j=0;j<p;j++) tmp[p-j-1][i]=b[i][j]; for(int i=0;i<p;i++) for(int j=0;j<p;j++) b[i][j]=tmp[i][j]; } ull ctoi(char ch){ if('A'<=ch&&ch<='Z') return ch-'A'; if('a'<=ch&&ch<='z') return 26+ch-'a'; if(isdigit(ch)) return 52+ch-'0'; if(ch=='+') return 62; return 63; } void in(int w,int h,ull tmp[1001][1001]){ string str[1001]; for(int i=0;i<h;i++) cin>>str[i]; for(int i=0;i<h;i++) for(int j=0;j<(int)str[i].size();j++) for(int k=0;k<6;k++)tmp[i][6*j+5-k]=(ctoi(str[i][j])>>k)&1; } set <ull> S; void roll_hs(int w,int h,int p){ ull target=0; for(int i=0;i<p;i++){ ull k=0; for(int j=0;j<p;j++)k=k*base+b[i][j]; target=target*base2+k; } if(S.count(target))return; S.insert(target); for(int i=0;i<h;i++){ ull k=0,l=1; for(int j=0;j<w;j++){ k=k*base+a[i][j]; if(j-p>=0) k-=l*a[i][j-p]; else l*=base; A[i][j]=k; } } for(int j=p-1;j<w;j++){ ull k=0,l=1; for(int i=0;i<h;i++){ k=k*base2+A[i][j]; if(i-p>=0) k-=l*A[i-p][j]; else l*=base2; if(target==k&&i>=p-1)ans++; } } } int main(){ while(1){ int w,h,p; cin>>w>>h>>p; if(!w&&!h&&!p)break; in(w,h,a),in(p,p,b); ans=0,S.clear(); for(int i=0;i<4;i++){ rol_90(p),roll_hs(w,h,p); Reverse(p),roll_hs(w,h,p),Reverse(p); } cout <<ans<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; int A[300005], P[300005]; int idx = 1; int Path[300005 * 30 + 5][2]; int Count[300005 * 30 + 5]; void insert(int v) { int cur = 1; Count[cur]++; for (int i = 29; i >= 0; i--) { int p = (v >> i) & 1; if (Path[cur][p] == 0) { Path[cur][p] = ++idx; } cur = Path[cur][p]; Count[cur]++; } } int solve(int cur, int a, int i) { Count[cur]--; if (i == -1) return 0; int p = (a >> i) & 1; if (Path[cur][p] && Count[Path[cur][p]]) return solve(Path[cur][p], a, i - 1); return solve(Path[cur][p ^ 1], a, i - 1) | (1 << i); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &A[i]); for (int i = 0; i < n; i++) { scanf("%d", &P[i]); insert(P[i]); } for (int i = 0; i < n; i++) { printf("%d", solve(1, A[i], 29)); if (i < n - 1) printf(" "); else printf("\n"); } return 0; }
3
#include <iostream> #include <cmath> using namespace std; typedef long long ll; typedef pair<ll,ll> P; ll N,x; ll A[2001]; ll C[2001][2001]; P D[2001][2001]; int main(){ cin >> N >> x; for(int i=0;i<N;i++){ cin >> A[i]; C[0][i] = A[i]; } for(int k=1;k<N;k++){ for(int i=0;i<N;i++){ C[k][i] = min(A[(i-k+N)%N],C[k-1][i]); } } ll ans = 1e18; for(int k=0;k<N;k++){ ll now = 0; for(int i=0;i<N;i++){ now += C[k][i]; } ans = min(ans,x*k + now); } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; long long int mod = 1e9 + 7; long long int dp[101000][3]; long long int n; map<long long int, long long int> mp; long long int rec(long long int k, long long int par, long long int ct) { if (k == 0) { if (par == 1) { return 1; } return 0; } if (dp[k][par] != -1) return dp[k][par]; if (par == 1) { dp[k][par] = ((((n - ct) * rec(k - 1, 0, ct) % mod) % mod) + (((ct - 1) * rec(k - 1, 1, ct) % mod) % mod)) % mod; } else { dp[k][par] = ((((n - ct - 1) * rec(k - 1, 0, ct) % mod) % mod) + (((ct)*rec(k - 1, 1, ct) % mod) % mod)) % mod; } return dp[k][par]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string st, en; cin >> st >> en; long long int k; cin >> k; n = st.size(); long long int ct = 0; memset(dp, -1, sizeof(dp)); for (long long int i = 0; i < n; i++) { long long int temp = i, flag = 0; for (long long int j = 0; j < n; j++) { if (en[j] != st[temp]) { flag = 1; break; } temp++; temp = temp % n; } if (flag == 0) { mp[i] = 1; ct++; } else { mp[i] = 0; } } long long int ans; if (mp[0] == 1) { ans = rec(k, 1, ct) % mod; } else { ans = rec(k, 0, ct) % mod; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; void _print(long long int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void print(const T *const arr, long long int count) { for (long long int i = 0; i < count; ++i) { _print(arr[i]); cerr << " "; } cerr << '\n'; } void Solve() { long long ll n, x; cin >> n; long long ll sq = sqrt(n); long long ll cb = cbrt(n); set<long long ll> second; for (long long ll i = 1; i <= sq; i++) { x = i * i; second.insert(x); } for (long long ll i = 1; i <= cb; i++) { x = (i * i * i); second.insert(x); } cout << second.size(); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int T = 1; cin >> T; for (long long int i = 1; i < T + 1; i++) { Solve(); cout << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; class N { public: long id, value; N(long _id, long _value); }; N::N(long _id, long _value) { id = _id; value = _value; } class H { private: vector<N> elem; void swap(long a, long b); public: bool empty(); H(); void modify(long value); void pop(); N top(); void push(long id, long value); } B, W; void H::modify(long value) { if (value == 0) { pop(); return; } elem[1].value = value; } bool H::empty() { if (elem.size() <= 1) return true; return false; } N H::top() { return elem[1]; } void H::pop() { elem[1] = elem.back(); elem.pop_back(); bool finished; long maxChild; long n = 1; do { finished = true; if (2 * n >= elem.size()) return; if (2 * n + 1 >= elem.size()) maxChild = 2 * n; if (2 * n + 1 < elem.size()) maxChild = elem[2 * n].value > elem[2 * n + 1].value ? 2 * n : 2 * n + 1; if (elem[n].value < elem[maxChild].value) { swap(n, maxChild); n = maxChild; finished = false; } } while (!finished); } void H::swap(long a, long b) { long t_id = elem[a].id; long t_value = elem[a].value; elem[a].id = elem[b].id; elem[a].value = elem[b].value; elem[b].id = t_id; elem[b].value = t_value; } H::H() { elem.push_back(N(0, 2000000000)); } void H::push(long id, long value) { elem.push_back(N(id, value)); long n = elem.size() - 1; while (elem[n].value > elem[n / 2].value) { swap(n, n / 2); n /= 2; } } int Color[100005]; long N; vector<long> Child[100005]; void in() { scanf("%ld", &N); long value; for (long i = 1; i <= N; i++) { scanf("%d %ld", &Color[i], &value); if (Color[i] == 0) B.push(i, value); else W.push(i, value); } } void makeConnected() { bool visited[100005]; long v = 1; for (long i = 0; i < 100005; i++) visited[i] = false; long n; queue<long> q; visited[1] = true; q.push(1); while (v < N) { if (q.empty()) { for (long i = 1; i <= N; i++) if (!visited[i]) { for (long j = 1; j <= N; j++) if (visited[j] && Color[i] != Color[j]) { printf("%ld %ld 0\n", i, j); visited[i] = true; v++; Child[i].push_back(j); Child[j].push_back(i); q.push(i); i = j = N + 1; } } } n = q.front(); q.pop(); for (long i = 0; i < Child[n].size(); i++) if (!visited[Child[n][i]]) { visited[Child[n][i]] = true; v++; q.push(Child[n][i]); } } } void solve() { while (!B.empty() && !W.empty()) { long value = (B.top().value < W.top().value ? B.top().value : W.top().value); printf("%ld %ld %ld\n", B.top().id, W.top().id, value); Child[B.top().id].push_back(W.top().id); Child[W.top().id].push_back(B.top().id); B.modify(B.top().value - value); W.modify(W.top().value - value); } makeConnected(); } int main() { in(); solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long x[105], k[105], dp[1 << 21], ind[105]; int pb[105]; bool comp(int id1, int id2) { return k[id1] > k[id2]; } int main() { int n, m; long long b; cin >> n >> m >> b; for (int i = 0; i < n; i++) { int mp; cin >> x[i] >> k[i] >> mp; ind[i] = i; int bit = 0; for (int j = 0; j < mp; j++) { int xpb; cin >> xpb; xpb--; bit |= (1 << xpb); } pb[i] = bit; } sort(ind, ind + n, comp); for (int i = 0; i < (1 << m); i++) dp[i] = LLONG_MAX; dp[(1 << m) - 1] = 0; long long resp = LLONG_MAX; for (int i = n - 1; i >= 0; i--) { int id = ind[i]; for (int j = 0; j < (1 << m); j++) { int bit = (j | pb[id]); if (dp[bit] != LLONG_MAX) dp[j] = min(dp[j], dp[bit] + x[id]); } if (dp[0] != LLONG_MAX) resp = min(resp, dp[0] + k[id] * b); } if (resp == LLONG_MAX) cout << "-1" << endl; else cout << resp << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, k; cin >> n >> m >> k; if (k <= n - 1) { cout << 1 + k << " " << 1; return 0; } long long magic = m - 1; k -= n - 1; if (k == 1) { cout << n << " " << 2; return 0; } k -= 1; long long mg = k % magic; long long level = k / magic; if (level % 2) { cout << n - level << " " << m - mg << endl; } else { cout << n - level << " " << mg + 2; } return 0; }
2
#include<bits/stdc++.h> using namespace std; using Int = long long; struct BiMatch{ Int n; vector<vector<Int> > G; vector<Int> match,used; BiMatch(){} BiMatch(Int sz):n(sz),G(sz),match(sz),used(sz){} void add_edge(Int u,Int v){ G[u].push_back(v); G[v].push_back(u); } bool dfs(Int v){ used[v]=true; for(Int i=0;i<(Int)G[v].size();i++){ Int u=G[v][i],w=match[u]; if(w<0||(!used[w]&&dfs(w))){ match[v]=u; match[u]=v; return true; } } return false; } Int build(){ Int res=0; fill(match.begin(),match.end(),-1); for(Int v=0;v<n;v++){ if(match[v]<0){ fill(used.begin(),used.end(),0); if(dfs(v)){ res++; } } } return res; } }; Int isprime(Int x){ if(x<=2) return 0; for(Int i=2;i*i<=x;i++) if(x%i==0) return 0; return 1; } //INSERT ABOVE HERE signed main(){ Int n; cin>>n; vector<Int> x(n); for(Int i=0;i<n;i++) cin>>x[i]; vector<Int> a(1e7+100,0); for(Int i=0;i<n;i++) a[x[i]]=1; vector<Int> b; for(Int i=1;i<(Int)a.size();i++) if(a[i]^a[i-1]) b.emplace_back(i); Int m=b.size(); BiMatch bm(m); for(Int i=0;i<m;i++) for(Int j=0;j<i;j++) if(isprime(b[i]-b[j])) bm.add_edge(i,j); Int k=bm.build(); Int o=0,e=0; for(Int x:b) if(x&1) o++;else e++; //cout<<k<<" "<<o<<" "<<e<<endl; Int ans=k+((o-k)/2*2)+((e-k)/2*2)+((o-k)&1?3:0); cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> #define F first #define S second using namespace std; int mem[3005][3005]; pair<int,int> arr[3005]; int n,t; int go(int x,int y) { if(y>=t) return 0; if(x>=n) return 0; if(mem[x][y]!=-1) return mem[x][y]; return mem[x][y]=max(go(x+1,y),arr[x].S+go(x+1,y+arr[x].F)); } int main() { cin >> n >> t; for(int i=0;i<n;i++) cin >> arr[i].F >> arr[i].S; sort(arr,arr+n); memset(mem,-1,sizeof mem); cout << go(0,0) << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { for (int i = 0; i < 10; i++) { cout << i << endl; string r; getline(cin, r); if (r == "terrible" || r == "worse" || r == "no way" || r == "are you serious" || r == "do die in a hole") { cout << "grumpy"; return 0; } if (r == "cool" || r == "not bad" || r == "don't think so" || r == "don't touch me'" || r == "great") { cout << "normal"; return 0; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100, INFTY = 0x3f3f3f3f; int A[MAXN], B[MAXN]; vector<int> p[MAXN]; int posb[330]; int resp[330]; int main() { int N, M, S, E; posb[0] = 0; resp[0] = 0; for (int i = 1; i <= 300; i++) { posb[i] = MAXN; resp[i] = INFTY; } for (int i = 1; i <= MAXN - 100; i++) p[i].clear(); scanf("%d %d %d %d", &N, &M, &S, &E); for (int i = 1; i <= N; i++) scanf("%d", &A[i]); for (int i = 1; i <= M; i++) { scanf("%d", &B[i]); p[B[i]].push_back(i); } for (int i = 1; i <= 10000; i++) { int tam = p[i].size(); if (tam == 0) continue; } int limite = 0; for (int k = 1; k <= N; k++) { int x = A[k]; for (int i = limite; i >= 0; i--) { int ini = posb[i] + 1; int tam = p[x].size(); int prox = (lower_bound(&p[x][0], &p[x][tam], ini) - &p[x][0]); if (prox == tam) continue; posb[i + 1] = min(posb[i + 1], p[x][prox]); resp[i + 1] = min(resp[i + 1], p[x][prox] + k); if (i == limite && i < 299) limite++; } } int ans = 0; for (int i = 0; i <= 300; i++) { long long int qq = E, rr = S; qq *= i; qq += resp[i]; if (qq > rr) break; ans = i; } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; scanf("%lld%lld", &n, &m); long long npair = n / 5; long long mpair = m / 5; long long nextra = n % 5; long long mext = m % 5; long long solve = npair * mpair * 5 + nextra * mpair + mext * npair; if (nextra + mext > 4) solve += (nextra + mext) - 4; printf("%lld\n", solve); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; struct state { int pos[3]; int who[3]; bool candrop[3], canmove[3], canpick[3]; }; bool operator<(const state &s1, const state &s2) { int i = 0; while (i < 3 && s1.pos[i] == s2.pos[i]) ++i; if (i < 3) return s1.pos[i] < s2.pos[i]; i = 0; while (i < 3 && s1.who[i] == s2.who[i]) ++i; if (i < 3) return s1.who[i] < s2.who[i]; i = 0; while (i < 3 && s1.candrop[i] == s2.candrop[i]) ++i; if (i < 3) return s1.candrop[i] < s2.candrop[i]; i = 0; while (i < 3 && s1.canmove[i] == s2.canmove[i]) ++i; if (i < 3) return s1.canmove[i] < s2.canmove[i]; i = 0; while (i < 3 && s1.canpick[i] == s2.canpick[i]) ++i; if (i < 3) return s1.canpick[i] < s2.canpick[i]; return false; } set<state> used; int lenmove[3], lendrop[3]; int ans = -1; int ts; int iter = 0; void rec(state cur) { iter++; if (iter == 10000) { iter = 0; if (clock() - ts > 1000) printf("%d\n", ans), exit(0); } if (used.count(cur)) return; used.insert(cur); ans = max(ans, max(max(cur.pos[0], cur.pos[1]), cur.pos[2])); int ubound = 0; for (int i = 0; i < 3; ++i) ubound = max(cur.pos[i], ubound); for (int i = 0; i < 3; ++i) { if (cur.canmove[i]) ubound += lenmove[i]; if (cur.candrop[i]) ubound += lendrop[i]; } if (ubound <= ans) return; int d[3] = {-1, -1, -1}; for (int i = 0; i < 3; ++i) if (cur.who[i] != -1) d[cur.who[i]] = i; for (int i = 0; i < 3; ++i) { if (d[i] != -1) continue; if (cur.who[i] == -1) { if (cur.canmove[i]) { for (int dist = -lenmove[i]; dist <= lenmove[i]; ++dist) { int newpos = cur.pos[i] + dist; if (cur.pos[0] == newpos || cur.pos[1] == newpos || cur.pos[2] == newpos) continue; state to = cur; to.pos[i] = newpos; to.canmove[i] = false; rec(to); } } if (cur.canpick[i]) { for (int j = 0; j < 3; ++j) if (i != j) { if (abs(cur.pos[i] - cur.pos[j]) == 1 && d[j] == -1) { state to = cur; to.canpick[i] = false; to.who[i] = j; to.pos[j] = cur.pos[i]; if (cur.who[j] != -1) to.pos[cur.who[j]] = cur.pos[i]; rec(to); } } } } else { if (cur.candrop[i]) { for (int dist = -lendrop[i]; dist <= lendrop[i]; ++dist) { int newpos = dist + cur.pos[i]; if (cur.pos[0] == newpos || cur.pos[1] == newpos || cur.pos[2] == newpos) continue; state to = cur; to.candrop[i] = false; to.who[i] = -1; to.pos[cur.who[i]] = newpos; if (cur.who[cur.who[i]] != -1) { to.pos[cur.who[cur.who[i]]] = newpos; } rec(to); } } } } } int main() { ts = clock(); state start; memset(start.candrop, 1, sizeof(start.candrop)); memset(start.canpick, 1, sizeof(start.canpick)); memset(start.canmove, 1, sizeof(start.canmove)); memset(start.who, -1, sizeof(start.who)); for (int i = 0; i < 3; ++i) { scanf("%d%d%d", &start.pos[i], &lenmove[i], &lendrop[i]); } rec(start); printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 5; int a[N]; int get(int x, int y) { return (x >> y) & 1; } bool pal(string s) { for (int i = 0; i < s.size(); i++) { if (s[i] != s[s.size() - i - 1]) { return false; } } return true; } bool lucky(int x) { while (x) { if (x % 10 != 7 && x % 10 != 4) { return false; } x /= 10; } return true; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < n; i++) { cout << a[i] << " "; } }
1
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; const double pi = acos(-1); vector<long long> ft(1, 1LL); vector<long long> graph[500005]; void solve() { long long n, m; cin >> n >> m; vector<pair<long long, long long> > x(n); for (long long i = 0; i < m; i++) { long long node1, node2; cin >> node1 >> node2; graph[node1].push_back(node2), graph[node2].push_back(node1); } for (long long i = 0; i < n; i++) cin >> x[i].first, x[i].second = i; for (long long i = 0; i < n; i++) { set<long long> s; for (auto j : graph[i + 1]) { if (x[j - 1].first == x[i].first) { cout << -1 << '\n'; return; } s.insert(x[j - 1].first); } for (long long j = 1; j < x[i].first; j++) { if (s.find(j) == s.end()) { cout << -1 << '\n'; return; } } } sort(x.begin(), x.end()); for (auto i : x) cout << i.second + 1 << " "; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; int n, m, tot; map<int, int> id; multiset<int> to[2 * 100000 + 5]; int ans[10 * 100000 + 5], tp, d[2 * 100000 + 5], cnt, p[10 * 100000 + 5]; int b[100000 + 5], c[100000 + 5], val[2 * 100000 + 5]; void dfs(int u) { for (auto i = to[u].begin(); i != to[u].end(); i = to[u].begin()) { auto v = *i; to[u].erase(i), to[v].erase(to[v].lower_bound(u)); dfs(v); } ans[++tp] = u; } int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { scanf("%d", &b[i]); if (!id.count(b[i])) id[b[i]] = ++tot, val[tot] = b[i]; } for (int i = 1; i < n; ++i) { scanf("%d", &c[i]); if (b[i] > c[i]) { printf("-1\n"); return 0; } if (!id.count(c[i])) id[c[i]] = ++tot, val[tot] = c[i]; to[id[c[i]]].insert(id[b[i]]), to[id[b[i]]].insert(id[c[i]]); d[id[c[i]]]++, d[id[b[i]]]++; } for (int i = 1; i <= tot; ++i) if (d[i] & 1) p[++cnt] = i; if (cnt != 0 && cnt != 2) printf("-1\n"); else { if (cnt == 0) dfs(1); else dfs(p[1]); if (tp != n) printf("-1\n"); else { while (tp) printf("%d ", val[ans[tp--]]); printf("\n"); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; string s; int now, ans; int main() { cin >> s; for (int i = 0; i < s.length(); i++) if (s[i] == '(') now++; else if (s[i] == ')' && now > 0) now--, ans += 2; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int a[] = {0, 1, 3, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1}; int x, y; scanf("%d%d", &x, &y); cout << (a[x]==a[y]? "Yes" : "No") << endl; return 0; }
0
#include<bits/stdc++.h> #define F(i,a,b) for(int i=(a);i<=(b);++i) #define F2(i,a,b) for(int i=(a);i<(b);++i) #define dF(i,a,b) for(int i=(a);i>=(b);--i) #define dF2(i,a,b) for(int i=(a);i>(b);--i) #define dF3(i,a,b) for(int i=(a)-1;i>=(b);--i) using namespace std;typedef long long ll;typedef double ld;int INF=0x3f3f3f3f;int INF2=0x7fffffff;ll LNF=0x3f3f3f3f3f3f3f3f;ll LNF2=0x7fffffffffffffff; int n; int t[4001],a[4001]; int b[4001]; int f[2001][2001]; int bit[4001]; inline void Add(int p,int x){for(;p<=n+n;p+=p&-p)bit[p]+=x;} inline int Qur(int p){int A=0;for(;p;p-=p&-p)A+=bit[p];return A;} int main(){ char s[3]; scanf("%d",&n); F(i,1,n+n) scanf("%s%d",s,a+i), t[i]=*s=='B', b[t[i]*n+a[i]]=i; f[0][0]=0; F(i,0,n){ memset(bit,0,sizeof bit); F(j,1,n+n) Add(j,1); F(j,1,i) Add(b[j],-1); F(j,i?0:1,n){ if(j) Add(b[n+j],-1); f[i][j]=INF; if(i) f[i][j]=min(f[i][j],f[i-1][j]+Qur(b[i])); if(j) f[i][j]=min(f[i][j],f[i][j-1]+Qur(b[n+j])); } } printf("%d",f[n][n]); return 0; }
0
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) int main(){ int n,p; while(cin>>n>>p, n!=0 || p!=0){ int A[n]; memset(A, 0, sizeof(A)); int t=0; while(1){ if(p==0){ p=A[t]; A[t]=0; } else { p-=1; A[t]+=1; } int f=1; rep(i,n) { if(t!=i && A[i]!=0) f=0; } if(p==0 && f) break; t=(t+1)%n; } cout<<t<<endl; } return 0; }
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> e[200005]; vector<int> pick[200005]; int pre[200005]; int dp[200005][2]; void dfs(int u, int fa) { for (auto v : e[u]) { if (v == fa) continue; dfs(v, u); } // dp[i][0] root i is not on side // dp[i][1] root i is one of the side vector<pair<int, int> > tmp; int sum = 0; for (auto v : e[u]) { if (v == fa) continue; sum += dp[v][0] + 1; tmp.emplace_back(make_pair(-dp[v][0] - 1 + dp[v][1], v)); } sort(tmp.begin(), tmp.end()); if (tmp.size() == 0) { dp[u][0] = 0; dp[u][1] = 0; } else if (tmp.size() == 1) { dp[u][0] = dp[u][1] = min(sum, sum + tmp[0].first); if (tmp[0].first < 0) pre[u] = tmp[0].second; else pre[u] = -1; } else { dp[u][1] = min(sum, sum + tmp[0].first); if (tmp[0].first < 0) pre[u] = tmp[0].second; else pre[u] = -1; dp[u][0] = sum + tmp[0].first + tmp[1].first; pick[u].emplace_back(tmp[0].second); pick[u].emplace_back(tmp[1].second); } //cout << u << " " << dp[u][0] << " " << dp[u][1] << endl; } pair<int, int> find_ans(int u, int state, int fa) { int sz = 0; for (auto v : e[u]) { if (v == fa) continue; sz++; } pair<int, int> pa = make_pair(u, u); if (sz == 1) { int node = pre[u]; if (node != -1) { for (auto v : e[u]) { if (v == fa) continue; if (v == node) { pa = find_ans(v, 1, u); if (pa.first == v) pa.first = u; else pa.second = u; } } } else { for (auto v : e[u]) { if (v == fa) continue; if (v == node) continue; else { pair<int, int> new_pa = find_ans(v, 0, u); printf("%d %d %d %d\n", u, v, pa.first, new_pa.first); pa = make_pair(pa.second, new_pa.second); } } } } else if (sz > 1) { if (state == 1) { int node = pre[u]; for (auto v : e[u]) { if (v == fa) continue; if (v == node) { pa = find_ans(v, 1, u); if (pa.second == v) pa.second = u; else pa.first = u; if (pa.second != u) swap(pa.first, pa.second); } } for (auto v : e[u]) { if (v == fa) continue; if (v == node) continue; pair<int, int> new_pa = find_ans(v, 0, u); printf("%d %d %d %d\n", u, v, pa.first, new_pa.first); pa = make_pair(pa.second, new_pa.second); if (pa.second != u) swap(pa.first, pa.second); } } else { for (auto v : e[u]) { if (v == fa) continue; if (v == pick[u][0] || v == pick[u][1]) { pair<int, int> pa1 = find_ans(v, 1, u); int node = pa1.first; if (pa1.first == v) node = pa1.second; if (pa.first == u) pa.first = node; else pa.second = node; } } for (auto v : e[u]) { if (v == fa) continue; if (v == pick[u][0] || v == pick[u][1]) continue; pair<int, int> new_pa = find_ans(v, 0, u); printf("%d %d %d %d\n", u, v, pa.first, new_pa.first); pa = make_pair(pa.second, new_pa.second); } } } //cout << u << " " << state << " " << pa.first << " " << pa.second << endl; return pa; } int main(void) { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 1;i <= n;i++) { e[i].clear(); pick[i].clear(); } for (int i = 0;i < n - 1;i++) { int u, v; scanf("%d %d", &u, &v); e[u].emplace_back(v); e[v].emplace_back(u); } dfs(1, -1); printf("%d\n", min(dp[1][0], dp[1][1])); if (dp[1][0] > dp[1][1]) find_ans(1, 1, -1); else find_ans(1, 0, -1); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, r, c; scanf("%lld%lld", &n, &m); if (n % 2 == 0) { r = 1; c = 1; while (r != n / 2 + 1 || c != 1) { printf("%lld %lld\n", r, c); if (r <= n / 2) { r = n - r + 1; c = m - c + 1; } else if (c != 1) { r = n - r + 1; c = m - c + 2; } else { r = n - r + 2; c = 1; } } printf("%lld %lld\n", r, c); return 0; } r = 1; c = 1; while (r != n / 2 + 1 || c != 1) { printf("%lld %lld\n", r, c); if (r <= n / 2) { r = n - r + 1; c = m - c + 1; } else if (c != 1) { r = n - r + 1; c = m - c + 2; } else { r = n - r + 2; c = 1; } } while (c != m / 2 + 1) { printf("%lld %lld\n", r, c); if (c <= m / 2) { c = m - c + 1; } else { c = m - c + 2; } } printf("%lld %lld\n", r, c); return 0; }
4
#include <bits/stdc++.h> using namespace std; map<int, int> segs; int main() { int n, k, a; scanf("%d %d %d", &n, &k, &a); segs[0] = n; int s = (n + 1) / (a + 1); int m; scanf("%d", &m); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); x--; map<int, int>::iterator t = segs.upper_bound(x); t--; int l = t->second; s -= (l + 1) / (a + 1); int l1 = x - t->first; int l2 = l - l1 - 1; s += (l1 + 1) / (a + 1); s += (l2 + 1) / (a + 1); segs[t->first] = l1; segs[x + 1] = l2; if (s < k) { printf("%d\n", i + 1); return 0; } } printf("-1\n"); return 0; }
4
#include <iostream> int main() { int n; std::cin >> n; std::cout << ((n%2==0) ? n/2 : n/2+1) << "\n"; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 1000 * 1000 * 1000; const long long INF64 = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; string s; int n; void Load() { cin >> s; n = (int)s.size(); } int dp[2100][2100]; const int MOD = 1000 * 1000 + 3; int rec(int pos, int br) { if (br < 0) return 0; if (pos == n) return (br == 0 ? 1 : 0); if (dp[pos][br] != -1) return dp[pos][br]; int t = pos; if (s[pos] >= '0' && s[pos] <= '9') { while (t < n && s[t] >= '0' && s[t] <= '9') t++; t--; dp[pos][br] = (rec(t, br - 1) + rec(t + 1, br)) % MOD; } else dp[pos][br] = rec(pos + 1, br + 1); return dp[pos][br]; } bool check() { if (s[0] == '*' || s[0] == '/') return false; if (s[n - 1] == '*' || s[n - 1] == '/' || s[n - 1] == '+' || s[n - 1] == '-') return false; for (int i = 1; i < n; i++) { if (s[i] == '*' || s[i] == '/') if (!(s[i - 1] >= '0' && s[i - 1] <= '9')) return false; } return true; } void Solve() { memset(dp, 0xFF, sizeof(dp)); if (check()) cout << rec(0, 0); else cout << 0; } int main() { Load(); Solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, k; int main() { cin >> n; if (n % 2 == 0) cout << n * n / 2 << endl; if (n % 2 == 1) cout << n * (n - 1) / 2 + n / 2 + 1 << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if ((i + j) % 2 == 0) { cout << "C"; } else { cout << "."; } } cout << endl; } }
1
#include <iostream> using namespace std; int main(){ int A, B, C; cin >> A >> B >> C; if(A==B) cout<< C; else if(A==C) cout << B; else cout << A; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s; char ch; int flag = 0; cin >> s; int count = 0; for (int i = 0; i < s.size(); i++) { if (isupper(s[i])) { if (i == 0) { flag = 1; } count++; } } if (flag == 0 && count == s.size() - 1) { for (int i = 0; i < s.size(); i++) { if (i == 0) { ch = toupper(s[i]); cout << ch; } else { ch = tolower(s[i]); cout << ch; } } } else if (count == s.size()) { for (int i = 0; i < s.size(); i++) { ch = tolower(s[i]); cout << ch; } } else { cout << s; } return 0; }
1
#include <bits/stdc++.h> void solve() { long long n; std::cin >> n; std::cout << -1 * (n - 1) << " " << n << "\n"; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); long long t = 0; std::cin >> t; while (t--) { solve(); } }
1
#include<bits/stdc++.h> using namespace std; using Int = long long; #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b)) < EPS) #define PI 3.141592653589793238 // COUNTER CLOCKWISE static const Int CCW_COUNTER_CLOCKWISE = 1; static const Int CCW_CLOCKWISE = -1; static const Int CCW_ONLINE_BACK = 2; static const Int CCW_ONLINE_FRONT = -2; static const Int CCW_ON_SEGMENT = 0; //Intercsect Circle & Circle static const Int ICC_SEPERATE = 4; static const Int ICC_CIRCUMSCRIBE = 3; static const Int ICC_INTERSECT = 2; static const Int ICC_INSCRIBE = 1; static const Int ICC_CONTAIN = 0; struct Point{ double x,y; Point(){} Point(double x,double y) :x(x),y(y){} Point operator+(Point p) {return Point(x+p.x,y+p.y);} Point operator-(Point p) {return Point(x-p.x,y-p.y);} Point operator*(double k){return Point(x*k,y*k);} Point operator/(double k){return Point(x/k,y/k);} double norm(){return x*x+y*y;} double abs(){return sqrt(norm());} bool operator < (const Point &p) const{ return x!=p.x?x<p.x:y<p.y; //grid-point only //return !equals(x,p.x)?x<p.x:!equals(y,p.y)?y<p.y:0; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; istream &operator >> (istream &is,Point &p){ is>>p.x>>p.y; return is; } ostream &operator << (ostream &os,Point p){ os<<fixed<<setprecision(12)<<p.x<<" "<<p.y; return os; } bool sort_x(Point a,Point b){ return a.x!=b.x?a.x<b.x:a.y<b.y; } bool sort_y(Point a,Point b){ return a.y!=b.y?a.y<b.y:a.x<b.x; } typedef Point Vector; typedef vector<Point> Polygon; istream &operator >> (istream &is,Polygon &p){ for(Int i=0;i<(Int)p.size();i++) cin>>p[i]; return is; } struct Segment{ Point p1,p2; Segment(){} Segment(Point p1, Point p2):p1(p1),p2(p2){} }; typedef Segment Line; istream &operator >> (istream &is,Segment &s){ is>>s.p1>>s.p2; return is; } struct Circle{ Point c; double r; Circle(){} Circle(Point c,double r):c(c),r(r){} }; istream &operator >> (istream &is,Circle &c){ is>>c.c>>c.r; return is; } double norm(Vector a){ return a.x*a.x+a.y*a.y; } double abs(Vector a){ return sqrt(norm(a)); } double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } Point orth(Point p){return Point(-p.y,p.x);} bool isOrthogonal(Vector a,Vector b){ return equals(dot(a,b),0.0); } bool isOrthogonal(Point a1,Point a2,Point b1,Point b2){ return isOrthogonal(a1-a2,b1-b2); } bool isOrthogonal(Segment s1,Segment s2){ return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0); } bool isParallel(Vector a,Vector b){ return equals(cross(a,b),0.0); } bool isParallel(Point a1,Point a2,Point b1,Point b2){ return isParallel(a1-a2,b1-b2); } bool isParallel(Segment s1,Segment s2){ return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0); } Point project(Segment s,Point p){ Vector base=s.p2-s.p1; double r=dot(p-s.p1,base)/norm(base); return s.p1+base*r; } Point reflect(Segment s,Point p){ return p+(project(s,p)-p)*2.0; } double arg(Vector p){ return atan2(p.y,p.x); } Vector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a); } Int ccw(Point p0,Point p1,Point p2); bool intersectSS(Point p1,Point p2,Point p3,Point p4); bool intersectSS(Segment s1,Segment s2); bool intersectPS(Polygon p,Segment l); Int intersectCC(Circle c1,Circle c2); bool intersectSC(Segment s,Circle c); double getDistanceLP(Line l,Point p); double getDistanceSP(Segment s,Point p); double getDistanceSS(Segment s1,Segment s2); Point getCrossPointSS(Segment s1,Segment s2); Point getCrossPointLL(Line l1,Line l2); Polygon getCrossPointCL(Circle c,Line l); Polygon getCrossPointCC(Circle c1,Circle c2); Int contains(Polygon g,Point p); Polygon andrewScan(Polygon s); Polygon convex_hull(Polygon ps); double diameter(Polygon s); bool isConvex(Polygon p); double area(Polygon s); Polygon convexCut(Polygon p,Line l); Line bisector(Point p1,Point p2); Vector translate(Vector v,double theta); vector<Line> corner(Line l1,Line l2); vector<vector<pair<Int, double> > > segmentArrangement(vector<Segment> &ss, Polygon &ps); Int ccw(Point p0,Point p1,Point p2){ Vector a = p1-p0; Vector b = p2-p0; if(cross(a,b) > EPS) return CCW_COUNTER_CLOCKWISE; if(cross(a,b) < -EPS) return CCW_CLOCKWISE; if(dot(a,b) < -EPS) return CCW_ONLINE_BACK; if(a.norm()<b.norm()) return CCW_ONLINE_FRONT; return CCW_ON_SEGMENT; } bool intersectSS(Point p1,Point p2,Point p3,Point p4){ return (ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 && ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0 ); } bool intersectSS(Segment s1,Segment s2){ return intersectSS(s1.p1,s1.p2,s2.p1,s2.p2); } bool intersectPS(Polygon p,Segment l){ Int n=p.size(); for(Int i=0;i<n;i++) if(intersectSS(Segment(p[i],p[(i+1)%n]),l)) return 1; return 0; } Int intersectCC(Circle c1,Circle c2){ if(c1.r<c2.r) swap(c1,c2); double d=abs(c1.c-c2.c); double r=c1.r+c2.r; if(equals(d,r)) return ICC_CIRCUMSCRIBE; if(d>r) return ICC_SEPERATE; if(equals(d+c2.r,c1.r)) return ICC_INSCRIBE; if(d+c2.r<c1.r) return ICC_CONTAIN; return ICC_INTERSECT; } bool intersectSC(Segment s,Circle c){ return getDistanceSP(s,c.c)<=c.r; } double getDistanceLP(Line l,Point p){ return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1)); } double getDistanceSP(Segment s,Point p){ if(dot(s.p2-s.p1,p-s.p1) < 0.0 ) return abs(p-s.p1); if(dot(s.p1-s.p2,p-s.p2) < 0.0 ) return abs(p-s.p2); return getDistanceLP(s,p); } double getDistanceSS(Segment s1,Segment s2){ if(intersectSS(s1,s2)) return 0.0; return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)), min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2))); } Point getCrossPointSS(Segment s1,Segment s2){ for(Int k=0;k<2;k++){ if(getDistanceSP(s1,s2.p1)<EPS) return s2.p1; if(getDistanceSP(s1,s2.p2)<EPS) return s2.p2; swap(s1,s2); } Vector base=s2.p2-s2.p1; double d1=abs(cross(base,s1.p1-s2.p1)); double d2=abs(cross(base,s1.p2-s2.p1)); double t=d1/(d1+d2); return s1.p1+(s1.p2-s1.p1)*t; } Point getCrossPointLL(Line l1,Line l2){ double a=cross(l1.p2-l1.p1,l2.p2-l2.p1); double b=cross(l1.p2-l1.p1,l1.p2-l2.p1); if(abs(a)<EPS&&abs(b)<EPS) return l2.p1; return l2.p1+(l2.p2-l2.p1)*(b/a); } Polygon getCrossPointCL(Circle c,Line l){ Polygon ps; Point pr=project(l,c.c); Vector e=(l.p2-l.p1)/abs(l.p2-l.p1); if(equals(getDistanceLP(l,c.c),c.r)){ ps.emplace_back(pr); return ps; } double base=sqrt(c.r*c.r-norm(pr-c.c)); ps.emplace_back(pr+e*base); ps.emplace_back(pr-e*base); return ps; } Polygon getCrossPointCC(Circle c1,Circle c2){ Polygon p(2); double d=abs(c1.c-c2.c); double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d)); double t=arg(c2.c-c1.c); p[0]=c1.c+polar(c1.r,t+a); p[1]=c1.c+polar(c1.r,t-a); return p; } // IN:2 ON:1 OUT:0 Int contains(Polygon g,Point p){ Int n=g.size(); bool x=false; for(Int i=0;i<n;i++){ Point a=g[i]-p,b=g[(i+1)%n]-p; if(fabs(cross(a,b)) < EPS && dot(a,b) < EPS) return 1; if(a.y>b.y) swap(a,b); if(a.y < EPS && EPS < b.y && cross(a,b) > EPS ) x = !x; } return (x?2:0); } Polygon andrewScan(Polygon s){ Polygon u,l; if(s.size()<3) return s; sort(s.begin(),s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size()-1]); l.push_back(s[s.size()-2]); for(Int i=2;i<(Int)s.size();i++){ for(Int n=u.size();n>=2&&ccw(u[n-2],u[n-1],s[i])!=CCW_CLOCKWISE;n--){ u.pop_back(); } u.push_back(s[i]); } for(Int i=s.size()-3;i>=0;i--){ for(Int n=l.size();n>=2&&ccw(l[n-2],l[n-1],s[i])!=CCW_CLOCKWISE;n--){ l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(),l.end()); for(Int i=u.size()-2;i>=1;i--) l.push_back(u[i]); return l; } Polygon convex_hull(Polygon ps){ Int n=ps.size(); sort(ps.begin(),ps.end(),sort_y); Int k=0; Polygon qs(n*2); for(Int i=0;i<n;i++){ while(k>1&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0) k--; qs[k++]=ps[i]; } for(Int i=n-2,t=k;i>=0;i--){ while(k>t&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0) k--; qs[k++]=ps[i]; } qs.resize(k-1); return qs; } double diameter(Polygon s){ Polygon p=s; Int n=p.size(); if(n==2) return abs(p[0]-p[1]); Int i=0,j=0; for(Int k=0;k<n;k++){ if(p[i]<p[k]) i=k; if(!(p[j]<p[k])) j=k; } double res=0; Int si=i,sj=j; while(i!=sj||j!=si){ res=max(res,abs(p[i]-p[j])); if(cross(p[(i+1)%n]-p[i],p[(j+1)%n]-p[j])<0.0){ i=(i+1)%n; }else{ j=(j+1)%n; } } return res; } bool isConvex(Polygon p){ bool f=1; Int n=p.size(); for(Int i=0;i<n;i++){ Int t=ccw(p[(i+n-1)%n],p[i],p[(i+1)%n]); f&=t!=CCW_CLOCKWISE; } return f; } double area(Polygon s){ double res=0; for(Int i=0;i<(Int)s.size();i++){ res+=cross(s[i],s[(i+1)%s.size()])/2.0; } return abs(res); } double area(Circle c1,Circle c2){ double d=abs(c1.c-c2.c); if(c1.r+c2.r<=d+EPS) return 0; if(d<=abs(c1.r-c2.r)){ double r=min(c1.r,c2.r); return PI*r*r; } double rc=(d*d+c1.r*c1.r-c2.r*c2.r)/(2*d); double th=acos(rc/c1.r); double ph=acos((d-rc)/c2.r); return c1.r*c1.r*th+c2.r*c2.r*ph-d*c1.r*sin(th); } Polygon convexCut(Polygon p,Line l){ Polygon q; for(Int i=0;i<(Int)p.size();i++){ Point a=p[i],b=p[(i+1)%p.size()]; if(ccw(l.p1,l.p2,a)!=-1) q.push_back(a); if(ccw(l.p1,l.p2,a)*ccw(l.p1,l.p2,b)<0) q.push_back(getCrossPointLL(Line(a,b),l)); } return q; } Line bisector(Point p1,Point p2){ Circle c1=Circle(p1,abs(p1-p2)),c2=Circle(p2,abs(p1-p2)); Polygon p=getCrossPointCC(c1,c2); if(cross(p2-p1,p[0]-p1)>0) swap(p[0],p[1]); return Line(p[0],p[1]); } Vector translate(Vector v,double theta){ Vector res; res.x=cos(theta)*v.x-sin(theta)*v.y; res.y=sin(theta)*v.x+cos(theta)*v.y; return res; } vector<Line> corner(Line l1,Line l2){ vector<Line> res; if(isParallel(l1,l2)){ double d=getDistanceLP(l1,l2.p1)/2.0; Vector v1=l1.p2-l1.p1; v1=v1/v1.abs()*d; Point p=l2.p1+translate(v1,90.0*(PI/180.0)); double d1=getDistanceLP(l1,p); double d2=getDistanceLP(l2,p); if(abs(d1-d2)>d){ p=l2.p1+translate(v1,-90.0*(PI/180.0)); } res.push_back(Line(p,p+v1)); }else{ Point p=getCrossPointLL(l1,l2); Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1; v1=v1/v1.abs(); v2=v2/v2.abs(); res.push_back(Line(p,p+(v1+v2))); res.push_back(Line(p,p+translate(v1+v2,90.0*(PI/180.0)))); } return res; } Polygon tangent(Circle c1,Point p2){ Circle c2=Circle(p2,sqrt(norm(c1.c-p2)-c1.r*c1.r)); Polygon p=getCrossPointCC(c1,c2); sort(p.begin(),p.end()); return p; } vector<Line> tangent(Circle c1,Circle c2){ vector<Line> ls; if(c1.r<c2.r) swap(c1,c2); double g=norm(c1.c-c2.c); if(equals(g,0)) return ls; Point u=(c2.c-c1.c)/sqrt(g); Point v=orth(u); for(Int s=1;s>=-1;s-=2){ double h=(c1.r+s*c2.r)/sqrt(g); if(equals(1-h*h,0)){ ls.emplace_back(c1.c+u*c1.r,c1.c+(u+v)*c1.r); }else if(1-h*h>0){ Point uu=u*h,vv=v*sqrt(1-h*h); ls.emplace_back(c1.c+(uu+vv)*c1.r,c2.c-(uu+vv)*c2.r*s); ls.emplace_back(c1.c+(uu-vv)*c1.r,c2.c-(uu-vv)*c2.r*s); } } return ls; } double closest_pair(Polygon &a,Int l=0,Int r=-1){ if(r<0){ r=a.size(); sort(a.begin(),a.end(),sort_x); } if(r-l<=1) return abs(a[0]-a[1]); Int m=(l+r)>>1; double x=a[m].x; double d=min(closest_pair(a,l,m),closest_pair(a,m,r)); inplace_merge(a.begin()+l,a.begin()+m,a.begin()+r,sort_y); Polygon b; for(Int i=l;i<r;i++){ if(fabs(a[i].x-x)>=d) continue; for(Int j=0;j<(Int)b.size();j++){ double dy=a[i].y-next(b.rbegin(),j)->y; if(dy>=d) break; d=min(d,abs(a[i]-*next(b.rbegin(),j))); } b.emplace_back(a[i]); } return d; } vector<vector<pair<Int, double> > > segmentArrangement(vector<Segment> &ss, Polygon &ps){ Int n=ss.size(); for(Int i=0;i<n;i++){ ps.emplace_back(ss[i].p1); ps.emplace_back(ss[i].p2); for(Int j=i+1;j<n;j++) if(intersectSS(ss[i],ss[j])) ps.emplace_back(getCrossPointSS(ss[i],ss[j])); } sort(ps.begin(),ps.end()); ps.erase(unique(ps.begin(),ps.end()),ps.end()); vector<vector<pair<Int, double> > > G(ps.size()); for(Int i=0;i<n;i++){ vector<pair<double,Int> > ls; for(Int j=0;j<(Int)ps.size();j++) if(getDistanceSP(ss[i],ps[j])<EPS) ls.emplace_back(make_pair(norm(ss[i].p1-ps[j]),j)); sort(ls.begin(),ls.end()); for(Int j=0;j+1<(Int)ls.size();j++){ Int a=ls[j].second,b=ls[j+1].second; G[a].emplace_back(b,abs(ps[a]-ps[b])); G[b].emplace_back(a,abs(ps[a]-ps[b])); } } return G; } Polygon volonoi(Polygon &b,Polygon &ps,Int k){ Polygon r(b); for(Int i=0;i<(Int)ps.size();i++){ if(i==k) continue; Line l=bisector(ps[k],ps[i]); r=convexCut(r,l); } return r; } double calc(Point a,Point b){ Vector v=b-a; if(abs(v.x)>EPS){ double th=-atan2(v.y,v.x)+PI/2; a=translate(a,th); b=translate(b,th); } double x=a.x,y=b.y-a.y; double s=a.y/a.x,t=b.y/b.x; return x*x*x/3.0*y + x*x*x*x/12.0*(-s*s*s+s+t*t*t-t); }; struct Precision{ Precision(){ cout<<fixed<<setprecision(12); } }precision_beet; //INSERT ABOVE HERE signed main(){ Int m,n; cin>>m>>n; Polygon ps(m),ts(n); cin>>ps>>ts; double res=0,sum=area(ps); for(Int i=0;i<n;i++){ Polygon xs=volonoi(ps,ts,i); for(auto &p:xs) p=p-ts[i]; Int s=xs.size(); for(Int j=0;j<s;j++){ Int k=(j+1)%s; res+=calc(xs[j],xs[k]); } } cout<<res/sum<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; ifstream putin; struct point { int x, y; }; int n, i, k; vector<point> a; int main() { cin >> n; a.resize(n); for (i = 0; i < n; i++) cin >> a[i].x >> a[i].y; for (i = 0; i < n; i++) { if (a[(i + 1) % n].x == a[i].x) if (a[(i + 1) % n].y > a[i].y) { if (a[(i + 2) % n].x < a[i].x) k++; } else { if (a[(i + 2) % n].x > a[i].x) k++; } else if (a[(i + 1) % n].x < a[i].x) { if (a[(i + 2) % n].y < a[i].y) k++; } else if (a[(i + 2) % n].y > a[i].y) k++; } cout << k; return 0; }
4
#include <bits/stdc++.h> using namespace std; int tab[10000]; bool Perfect(int a) { int sum = 0; while (a > 0) { sum = sum + a % 10; a = a / 10; if (sum > 10) break; } if (sum == 10) return (true); return (false); } int main() { tab[0] = 19; int k; cin >> k; bool found = false; int count = 1, num = 20; while (k > 1 && !found) { if (Perfect(num) == false) num++; else { tab[count] = num; num++; count++; found = (count == k); } } cout << tab[k - 1]; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; { long long int n = 0, k = 0, i = 0, j = 0; cin >> n; vector<long long int> a(n); for (i = 0; i < (n); i++) cin >> (a)[i]; ; vector<long long int> pre(n, 0); pre[0] = a[0]; for (i = 1; i < n; i++) { pre[i] = pre[i - 1] ^ a[i]; } long long int mx = 0, v = 0; for (i = 0; i < n; i++) { for (j = i; j < n; j++) { if (i == 0) v = pre[j]; else v = pre[j] ^ pre[i - 1]; mx = max(mx, v); } } cout << mx; cout << "\n"; ; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long int sz = 100005; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { long long int n, k; cin >> n >> k; if (n == 1) { cout << "0"; return 0; } long long int val = ((long long int)sqrt(9 + 8 * (n + k)) - 3LL) / 2LL; cout << n - val << "\n"; ; } }
2
#include <bits/stdc++.h> using namespace std; string get_only_alpha(string s) { string res = ""; for (int i = 0; i < s.length(); i++) if (isalpha(s[i])) res += s[i]; return res; } pair<int, pair<string, string> > parse_it(string s, int t) { int st = -1, ed = -1; for (int i = 0; i < s.length(); i++) if (s[i] == '(') st = i; for (int i = 0; i < s.length(); i++) if (s[i] == ')') ed = i; if (t == 1) { string rt = s.substr(st + 1, ed - st - 1); return make_pair(0, make_pair(get_only_alpha(rt), "")); } else { int cm = -1; for (int i = 0; i < s.length(); i++) { if (s[i] == ',') cm = i; } string rt = s.substr(st + 1, cm - st - 1); int lc = 10000, uc = -1; for (int i = 0; i < s.length(); i++) if (s[i] == '"') lc = min(lc, i), uc = max(uc, i); string st = s.substr(lc + 1, uc - lc - 1); return make_pair(-1, make_pair(get_only_alpha(rt), st)); } } string get_first_two(string s) { int mk = 0; string res = ""; for (int i = 0; i < s.length(); i++) { if (isalpha(s[i]) && mk < 2) { mk++; res += s[i]; } } return res; } int mat[100010]; int main() { ios::sync_with_stdio(false); int n; cin >> n; cin.ignore(); vector<pair<int, pair<string, string> > > dp; int x = n; while (x--) { string s = " "; char x[60]; getline(cin, s); if (get_first_two(s) == "tr") dp.push_back(make_pair(1, make_pair("", ""))); else if (get_first_two(s) == "th") dp.push_back(parse_it(s, 1)); else if (get_first_two(s) == "ca") dp.push_back(parse_it(s, 2)); } stack<int> ry; for (int i = 0; i < dp.size(); i++) { if (dp[i].first == 1) ry.push(i); else if (dp[i].first == -1) mat[i] = ry.top(), ry.pop(); } int mk = 0; for (int i = 0; i < dp.size(); i++) { if (dp[i].first == 0) { for (int j = i + 1; j < dp.size(); j++) { if (dp[j].first == -1 && mat[j] < i && dp[j].second.first == dp[i].second.first) { cout << dp[j].second.second << '\n'; mk = 1; break; } } } } if (!mk) cout << "Unhandled Exception\n"; }
3
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); pair<int, int> fib(int n) { if (n == 0) return {0, 1}; auto p = fib(n >> 1); int c = p.first * (2 * p.second - p.first); int d = p.first * p.first + p.second * p.second; if (n & 1) return {d, c + d}; else return {c, d}; } int fact(int n) { int res = 1; for (int i = n; i >= 1; i--) (res *= i) %= mod; return (res % mod); } int pow(int a, int b, int m) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } bool isPrime(long long n) { if (n <= 1) return false; for (long long i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } vector<long long> segmentedSieve(long long L, long long R) { long long lim = sqrt(R); vector<bool> mark(lim + 1, false); vector<long long> primes; for (long long i = 2; i <= lim; ++i) { if (!mark[i]) { primes.emplace_back(i); for (long long j = i * i; j <= lim; j += i) mark[j] = true; } } vector<bool> isPrime(R - L + 1, true); for (long long i : primes) for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i) isPrime[j - L] = false; if (L == 1) isPrime[0] = false; vector<long long> realprime; for (int i = 0; i <= R - L; i++) { if (isPrime[i]) { realprime.emplace_back(i + L); } } return realprime; } bool isPalindrome(string s) { int l = 0; int h = s.size() - 1; while (h > l) { if (s[l++] != s[h--]) { return false; } } return true; } int ternarySearch(int l, int r, int key, int ar[]) { while (r >= l) { int mid1 = l + (r - l) / 3; int mid2 = r - (r - l) / 3; if (ar[mid1] == key) { return mid1; } if (ar[mid2] == key) { return mid2; } if (key < ar[mid1]) { r = mid1 - 1; } else if (key > ar[mid2]) { l = mid2 + 1; } else { l = mid1 + 1; r = mid2 - 1; } } return -1; } int binarySearch(int arr[], int l, int r, int x) { while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } long long maxsubarraysum(long long *array, long long n) { long long best = 0, sum = 0; for (long long k = 0; k < n; k++) { sum = max(array[k], sum + array[k]); best = max(best, sum); } return best; } string findSum(string str1, string str2) { if (str1.length() > str2.length()) swap(str1, str2); string str = ""; int n1 = str1.length(), n2 = str2.length(); reverse(str1.begin(), str1.end()); reverse(str2.begin(), str2.end()); int carry = 0; for (int i = 0; i < n1; i++) { int sum = ((str1[i] - '0') + (str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } for (int i = n1; i < n2; i++) { int sum = ((str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } if (carry) str.push_back(carry + '0'); reverse(str.begin(), str.end()); return str; } string smin(string a, string b) { if (a.size() < b.size()) return a; if (a.size() > b.size()) return b; if (a <= b) return a; else return b; } bool so(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int getsum(const vector<int> &s, int l, int r) { return (l == 0) ? s[r] : (s[r] - s[l - 1]); } long long maxPrimeFactors(long long n) { long long maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n >>= 1; } for (int i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { maxPrime = i; n = n / i; } } if (n > 2) maxPrime = n; return maxPrime; } long long int nextp(long long int m) { while (!isPrime(m)) { m++; } return m; } int par[10001]; int find(int a) { if (par[a] < 0) return a; else return par[a] = find(par[a]); } void unionab(int a, int b) { par[a] = b; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) par[i] = -1; for (int i = 1; i <= n; i++) { int a; cin >> a; int b = find(i); a = find(a); if (a != b) unionab(a, b); } int k = 0; for (int i = 1; i <= n; i++) { if (par[i] < 0) k++; } cout << k << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, k; long long fac[1000007]; long long inv[1000007]; long long fastpow(long long a, long long x) { long long r = 1; while (x != 0) { if (x % 2 == 0) { a = (a * a) % 1000003; x /= 2; } else { r = (r * a) % 1000003; x--; } } return r; } void input() { cin >> n >> k; } void solve() { fac[0] = 1; int i; for (i = 1; i <= 1000000; i++) { fac[i] = (i * fac[i - 1]) % 1000003; } long long ans = 0; for (i = 1; i <= k; i++) { long long cur = 1; cur *= fac[n + i - 1]; cur %= 1000003; long long inv = (fac[i] * fac[(n - 1)]) % 1000003; cur *= fastpow(inv, 1000003 - 2); cur %= 1000003; ans += cur; ans %= 1000003; } cout << ans << "\n"; } int main() { input(); solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2100; int n, fa[N], s[N], root, size[N], ans[N]; vector<int> to[N], a[N]; template <class T> inline void read(T &x) { x = 0; char ch = getchar(), w = 0; while (!isdigit(ch)) w = (ch == '-'), ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x = w ? -x : x; return; } void dfs1(int now) { size[now] = 1; if (!s[now]) a[now].push_back(now); for (auto &v : to[now]) { dfs1(v), size[now] += size[v]; for (auto &k : a[v]) { a[now].push_back(k); if (a[now].size() == s[now]) a[now].push_back(now); } } if (size[now] <= s[now]) { printf("NO\n"); exit(0); } return; } int main() { read(n); for (register int i = 1; i <= n; ++i) { read(fa[i]), read(s[i]); if (fa[i]) to[fa[i]].push_back(i); else root = i; } dfs1(root); printf("YES\n"); for (register int i = 0; i < n; ++i) ans[a[root][i]] = i + 1; for (register int i = 1; i <= n; ++i) printf("%d ", ans[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, b, d; cin >> n >> b >> d; long long a[n + 3], tm = 0, cnt = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] > b) continue; tm = tm + a[i]; if (tm > d) { cnt++; tm = 0; } } cout << cnt << endl; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6; vector<vector<int>> G(N); int odw[N]; char odp[N]; int rozm[N]; int centroid; void dfs(int start, int przodek = 0) { rozm[start] = 1; for (auto u : G[start]) if (odw[u] == 0 && u != przodek) { dfs(u, start); rozm[start] += rozm[u]; } } void find_centroid(int start, int rozmiar, int przodek = -1) { if (rozm[start] * 2 >= rozmiar) { bool good = 1; for (auto u : G[start]) { if (u == przodek || odw[u]) continue; if (rozm[u] * 2 > rozmiar) { good = 0; break; } } if (good) centroid = start; } for (auto u : G[start]) { if (u == przodek || odw[u]) continue; find_centroid(u, rozmiar, start); } } void centroid_decomp(int start = 1, char level = 'A') { dfs(start); find_centroid(start, rozm[start]); odw[centroid] = 1; odp[centroid] = level; for (auto v : G[centroid]) if (!odw[v]) centroid_decomp(v, level + 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } centroid_decomp(1); for (int i = 1; i <= n; i++) cout << odp[i] << ' '; }
3
#include <bits/stdc++.h> using namespace std; inline long long read() { long long Hashimoto = 0; bool Kanna = 1; char I_Love = getchar(); while (I_Love < '0' || I_Love > '9') { if (I_Love == '-') Kanna = 0; I_Love = getchar(); } while (I_Love >= '0' && I_Love <= '9') { Hashimoto = Hashimoto * 10 + I_Love - '0'; I_Love = getchar(); } return (Kanna ? Hashimoto : -Hashimoto); } template <typename T1, typename T2> inline void Umax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T1, typename T2> inline void Umin(T1 &a, T2 b) { if (a > b) a = b; } struct mat { long long a[77][77]; mat() { memset(a, 0x3f, sizeof(a)); } mat operator*(mat &b) { mat ret; for (int i = 0; i < (77); ++i) for (int j = 0; j < (77); ++j) for (int k = 0; k < (77); ++k) { Umin(ret.a[i][j], a[i][k] + b.a[k][j]); } return ret; } }; int x, k, n, q; int maskid[333]; long long w[11]; int tot; mat pw[30]; long long dp[77]; long long ndp[77]; vector<pair<int, pair<long long, int> > > magic; long long delta[11] = {}; mat dodo() { mat ret; for (int i = 0; i < (1 << k); ++i) { if (maskid[i]) { if (i & 1) { for (int j = 1; j < (k + 1); ++j) { if (i & (1 << j)) continue; ret.a[maskid[i]][maskid[((i | (1 << j)) >> 1)]] = w[j] + delta[j]; } } else { ret.a[maskid[i]][maskid[i >> 1]] = 0; } } } return ret; } void fuck(mat a) { memcpy(ndp, dp, sizeof(dp)); memset(dp, 0x3f, sizeof(dp)); for (int i = 0; i < (77); ++i) for (int j = 0; j < (77); ++j) Umin(dp[i], ndp[j] + a.a[j][i]); } void fuck(int x) { for (int i = 0; i < (29); ++i) { if (x & (1 << i)) { fuck(pw[i]); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; memset(dp, 0x3f, sizeof(dp)); dp[1] = 0; cin >> x >> k >> n >> q; for (int i = 1; i < (k + 1); ++i) cin >> w[i]; for (int i = 0; i < (1 << k); ++i) if (__builtin_popcount(i) == x) { maskid[i] = ++tot; } pw[0] = dodo(); for (int i = 0; i < (28); ++i) pw[i + 1] = pw[i] * pw[i]; for (int i = 0; i < (q); ++i) { int p, w; cin >> p >> w; p--; for (int j = 1; j < (k + 1); ++j) { int f = p - j; if (f < 0 || f >= n - x) continue; magic.push_back({f, {w, j}}); } } sort(magic.begin(), magic.end()); ; int pre = -1; int l = 0; while (l < magic.size()) { fuck(magic[l].first - pre - 1); pre = magic[l].first; int r = l; while (r < magic.size() && magic[r].first == pre) r++; memset(delta, 0, sizeof(delta)); for (int i = l; i < (r); ++i) { delta[magic[i].second.second] += magic[i].second.first; } fuck(dodo()); l = r; } fuck(n - x - pre - 1); cout << dp[1] << endl; ; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 100010; int mx; vector<int> G[N]; vector<pair<int, int>> ans; void dfs(int u, int fa, int t) { ans.push_back({u, t}); int tmp = t; int cnt = G[u].size(); if (u != 1) cnt--; for (int v : G[u]) { if (v == fa) continue; if (t == mx) { ans.push_back({u, tmp - 1 - cnt}); t = tmp - 1 - cnt; } dfs(v, u, t + 1); ans.push_back({u, t + 1}); t++; cnt--; } if (u != 1 && t != tmp - 1) ans.push_back({u, tmp - 1}); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n - 1; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } if (n == 1) { printf("1\n"); printf("1 0\n"); return 0; } for (int i = 1; i <= n; i++) mx = max(mx, (int)G[i].size()); dfs(1, 0, 0); printf("%d\n", ans.size()); for (auto p : ans) printf("%d %d\n", p.first, p.second); return 0; }
4
#include <iostream> #include <fstream> #include <string> #include <iomanip> #include <math.h> #include <stdbool.h> #include <algorithm> #include <vector> #include <map> using namespace std; class Point { public: double x, y; Point(double x = 0, double y = 0): x(x), y(y) {} Point operator + (Point p) { return Point(x+p.x, y+p.y); } Point operator - (Point p) { return Point(x-p.x, y-p.y); } Point operator * (double a) { return Point(a*x, a*y); } Point operator / (double a) { return Point(x/a, y/a); } double abs() {return sqrt(norm()); } double norm() {return x*x + y*y; } }; typedef Point Vector; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x; } class Circle { public: double r; Point c; Circle(double r = 0, Point c = Point()): r(r) {} }; //ポイントpがベクトルp1-p0からみて反時計回りにいるかどうか //CCW: 1, CW: -1, ON:0 int isCCW(Point p, Point p0, Point p1){ Vector v01 = p1 - p0; Vector v02 = p - p0; double Cross = cross(v01, v02); int out = 2; if (Cross>0){out = 1;} else if(Cross<0){out = -1;} else{out = 0;} return out; } Point projection(Point p1, Point p2, Point p) { Vector v = p2 - p1; Vector u = p - p1; Point t = p1 + v*dot(u,v)/v.norm(); return t; } void print(Point p0, Point p1); int main(){ #if 0 std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); #endif Circle K; cin >> K.c.x >> K.c.y >> K.r; int q; cin >> q; for (int i=0; i<q; i++) { Point p0, p1, proj, cross0, cross1; cin >> p0.x >> p0.y >> p1.x >> p1.y; if ( isCCW(K.c, p0, p1) == 0 ) { Vector v1 = p1 - p0; v1 = v1 * K.r / v1.abs(); cross0 = K.c + v1; cross1 = K.c - v1; print(cross0, cross1); continue; } proj = projection(p0, p1, K.c); double d = ( proj - K.c ).abs(); if ( d > K.r ) { cout << "Invalid Input" << endl; continue; } if ( d == K.r ) { print(proj, proj); continue; } Vector v0 = proj - K.c; Vector v1; v1.x = v0.y; v1.y = - v0.x; v1 = v1 * sqrt( K.r*K.r - d*d ) / v1.abs(); cross0 = proj + v1; cross1 = proj - v1; print(cross0, cross1); } return 0; } void print(Point p0, Point p1) { double x0, y0, x1, y1; if ( p0.x < p1.x ) { x0 = p0.x; y0 = p0.y; x1 = p1.x; y1 = p1.y; } else if ( p0.x > p1.x ) { x0 = p1.x; y0 = p1.y; x1 = p0.x; y1 = p0.y; } else { if ( p0.y < p1.y ){ x0 = p0.x; y0 = p0.y; x1 = p1.x; y1 = p1.y; } else{ x0 = p1.x; y0 = p1.y; x1 = p0.x; y1 = p0.y; } } cout << fixed << setprecision(11) << x0 << " " << y0 << " " << x1 << " " << y1 << endl; }
0
#include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a);i<(b);i++) using pii=pair<int, int>; using Weight =int; struct Edge { int s, d; Weight w; Edge() {}; Edge(int s, int d, Weight w) :s(s), d(d), w(w) {}; }; using Array=vector<Weight>; using Edges = vector<Edge>; using Graph = vector<Edges>; void addArc(Graph& g, int s, int d, Weight w = 1) { g[s].emplace_back(s, d, w); } void addEdge(Graph& g, int s, int d, Weight w = 1) { addArc(g, s, d, w); addArc(g, s, d, w); } const int INF = 1e7; //vector<int>dijkstra(const Graph& g, int s, Array& dist) { // int n = g.size(); // enum {WHITE,GRAY,BLACK}; // assert(s < n); // vector<int>color(n, WHITE); color[s] = GRAY; // vector<int>prev(n, -1); // dist.assign(n, INF); dist[s] = 0; // using State=tuple<Weight, int, int>; // priority_queue<State, vector<State>, greater<State>>pq; pq.emplace(0, s, -1); // //} void solve(int N, int M) { vector<string>c(N); rep(i, 0, N) { cin >> c[i]; } auto inrange = [&](int x, int y) {return 0 <= x and x < N and 0 <= y and y < M; }; vector<int>dx = { 0,-1,0,1 }, dy = { 1,0,-1,0 }; auto check = [&](int x, int y, int dir) { int xx = x + dx[dir]; int yy = y + dy[dir]; if (not inrange(xx, yy))return false; return c[xx][yy] == '.'; }; int x = 0, y = 0, dir = 0; auto idx = [&](int x, int y) {return x * M + y; }; vector<int>visited; while (1) { int left = (dir + 1) % 4; if (check(x, y, left)) { dir = left; x += dx[dir]; y += dy[dir]; } else if (check(x, y, dir)) { x += dx[dir]; y += dy[dir]; } else { (dir += 3) %= 4; } visited.emplace_back(idx(x, y)); if (x == 0 && y == 0)break; } if (visited.back() == 0)visited.pop_back(); int cnt = 0; rep(i, 0, visited.size()) { int xx = visited[i] / M; int yy = visited[i] % M; if ((xx == 0 && yy == M - 1) || (xx == N - 1 && yy == 0) || (xx == N - 1 && yy == M - 1)) cnt++; for (int j = visited.size() - 1; j > i; j--) { if (visited[i] == visited[j]) { i = j; break; } } } if (cnt == 3) { cout << "YES" << endl; } else { cout << "NO" << endl; } } signed main(void) { int N, M; while (cin >> N >> M, N && M) { solve(N, M); } }
0
#include <bits/stdc++.h> using namespace std; void solve() { std::vector<long long> arr(4); long long a, b; for (int i = 0; i < 4; ++i) { cin >> arr[i]; } cin >> a >> b; sort(arr.begin(), arr.end()); if (arr[0] > a) { cout << min(arr[0] - a, b - a + 1) << endl; } else { cout << 0 << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int tt = 1; while (tt--) { solve(); } return 0; }
1
#include <iostream> #include <vector> #include <algorithm> #define rep(i,n) for(int i=0;i<(n);i++) #define ALL(A) A.begin(), A.end() using namespace std; int main() { vector <int> n2; for (int i = 1; i <= 10000; i++ ){ n2.push_back (i*i ); } // end for int n; while (cin >> n && n ){ vector <int> m (n-1, 0 ); rep (i, n-1 ){ m[i] = n2[i] % n; } // end rep sort (ALL (m ) ); m.erase (unique (ALL(m) ), m.end() ); int size = m.size(); vector<int> cnt ((n-1)/2+1, 0 ); int maxd = 0; rep (i, size ){ rep (j, size ){ if (i == j ) continue; int d = m[i] - m[j]; if (d < 0 ){ d += n; }// end if if (d > (n-1)/2 ){ d = n - d; } // end if cnt[d]++; maxd = max (d, maxd ); } // end for } // end for for (int i = 1; i <= maxd; i++ ){ cout << cnt[i] << endl; } // end for } // end loop return 0; }
0
#include <bits/stdc++.h> using namespace std; void IO() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; } int32_t main() { IO(); long long int t = 1; while (t--) { string s; cin >> s; string ans = ""; long long int n = s.size(), ct = 0; for (long long int i = 0; i < n; i++) { if (s[i] == '1') ct++; } long long int i = 0; for (; i < n; i++) { if (s[i] == '2') break; if (s[i] == '0') ans += '0'; } ans += string(ct, '1'); for (; i < n; i++) { if (s[i] != '1') ans += s[i]; } cout << ans << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; int n, m, t, head[100005]; int getHead(int p) { if (head[p] == p) return p; return head[p] = getHead(head[p]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) head[i] = i; for (int i = 0, a, b; i < m; ++i) { scanf("%d%d", &a, &b), --a, --b; if (getHead(a) != getHead(b)) { head[getHead(a)] = b; } else { t *= 2, t++, t %= 1000000009; } printf("%d\n", t); } }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; long long int len = s.length(); long long int cnt = 0; for (long long int i = 0; i < len; ++i) { if (s[i] == '1') { cnt++; } } long long int pw = len - 1; long long int ans = 0; if (cnt >= 1) { ans = pw / 2; ans++; } if (cnt == 1) { if (pw % 2 == 0) { ans--; } } cout << ans << endl; return 0; }
1
#include <cstdio> #include <cctype> #include <cstring> #include <algorithm> #include <vector> #define debug(...) fprintf(stderr, __VA_ARGS__) #ifndef AT_HOME #define getchar() IO::myGetchar() #define putchar(x) IO::myPutchar(x) #endif namespace IO { static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23; inline char myGetchar() { static char buf[IN_BUF], *ps = buf, *pt = buf; if (ps == pt) { ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin); } return ps == pt ? EOF : *ps++; } template<typename T> inline bool read(T &x) { bool op = 0; char ch = getchar(); x = 0; for (; !isdigit(ch) && ch != EOF; ch = getchar()) { op ^= (ch == '-'); } if (ch == EOF) { return false; } for (; isdigit(ch); ch = getchar()) { x = x * 10 + (ch ^ '0'); } if (op) { x = -x; } return true; } inline int readStr(char *s) { int n = 0; char ch = getchar(); for (; isspace(ch) && ch != EOF; ch = getchar()) ; for (; !isspace(ch) && ch != EOF; ch = getchar()) { s[n++] = ch; } s[n] = '\0'; return n; } inline void myPutchar(char x) { static char pbuf[OUT_BUF], *pp = pbuf; struct _flusher { ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); } }; static _flusher outputFlusher; if (pp == pbuf + OUT_BUF) { fwrite(pbuf, 1, OUT_BUF, stdout); pp = pbuf; } *pp++ = x; } template<typename T> inline void print_(T x) { if (x == 0) { putchar('0'); return; } std::vector<int> num; if (x < 0) { putchar('-'); x = -x; } for (; x; x /= 10) { num.push_back(x % 10); } while (!num.empty()) { putchar(num.back() ^ '0'); num.pop_back(); } } template<typename T> inline void print(T x, char ch = '\n') { print_(x); putchar(ch); } inline void printStr_(const char *s, int n = -1) { if (n == -1) { n = strlen(s); } for (int i = 0; i < n; ++i) { putchar(s[i]); } } inline void printStr(const char *s, int n = -1, char ch = '\n') { printStr_(s, n); putchar(ch); } } using namespace IO; const int N = 105, M = 1000005; const int P = 1000000007, Inv2 = (P + 1) / 2; const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int n, m, k, x[N], y[N]; int cntx[M], idx[M], wx[M], cnty[M], idy[M], wy[M]; int a[N][N], ans; void solve(int *cnt, int *id, int *w, int n, int m) { int now = 0, idx = 0; for (int i = 0; i < n - 1; ++i) { now = (now + m - cnt[i]) % P; if (cnt[i] || cnt[i + 1]) { id[i] = idx++; continue; } int t = (1ll * n * m - k - now) % P; ans = (ans + 2ll * now * t) % P; id[i] = idx; } id[n - 1] = idx++; for (int i = 0; i < n; ++i) { ++w[id[i]]; } } int bfs(int x, int y) { static int dis[N][N]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { dis[i][j] = P; } } std::vector<std::pair<int, int>> Q; int res = 0; dis[x][y] = 0, Q.push_back({x, y}); for (int i = 0; i < (int)Q.size(); ++i) { int x = Q[i].first, y = Q[i].second; res = (res + 1ll * a[x][y] * dis[x][y]) % P; for (int k = 0; k < 4; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) { continue; } if (a[nx][ny] == -1 || dis[nx][ny] < P) { continue; } dis[nx][ny] = dis[x][y] + 1; Q.push_back({nx, ny}); } } return res; } int main() { read(n), read(m), read(k); for (int i = 0; i < k; ++i) { read(x[i]), read(y[i]); ++cntx[x[i]], ++cnty[y[i]]; } solve(cntx, idx, wx, n, m); solve(cnty, idy, wy, m, n); n = idx[n - 1] + 1; m = idy[m - 1] + 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { a[i][j] = 1ll * wx[i] * wy[j] % P; } } for (int i = 0; i < k; ++i) { a[idx[x[i]]][idy[y[i]]] = -1; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] != -1) { ans = (ans + 1ll * a[i][j] * bfs(i, j)) % P; } } } print(1ll * ans * Inv2 % P); }
0
#include <bits/stdc++.h> using namespace std; template <int __> struct ia { int a[__], n; ia(int n = 0) : n(n) {} void rev() { reverse(a + 1, a + n + 1); } void hp() { make_heap(a + 1, a + n + 1); } int& operator[](int x) { return a[x]; } void sf() { for (int i = (1); i <= (n); i++) scanf("%d", a + i); } void mem(int x) { memset(a, x, sizeof(a)); } void sup() { sort(a + 1, a + n + 1, less<int>()); } void sdn() { sort(a + 1, a + n + 1, greater<int>()); } void uq() { this->sup(), n = unique(a + 1, a + n + 1) - a - 1; } bool nexp() { return next_permutation(a + 1, a + n + 1); } int lb(int x) { return lower_bound(a + 1, a + n + 1, x) - a; } int ub(int x) { return upper_bound(a + 1, a + n + 1, x) - a; } void pf() { for (int i = (1); i <= (n); i++) printf("%d%c", a[i], i == n ? '\n' : ' '); } ia operator+(const ia& b) { ia<__> c(max(n, b.n)); c.mem(0); for (int i = 1; i <= (c.n); i++) c[i] = a[i] + b.a[i]; return c; } ia operator-(const ia& b) { ia<__> c(max(n, b.n)); c.mem(0); for (int i = 1; i <= (c.n); i++) c[i] = a[i] - b.a[i]; return c; } ia operator*(const ia& b) { ia<__> c; c.mem(0); for (int i = 1; i <= ((*this).n); i++) for (int j = 1; j <= (b.n); j++) c.n = max(c.n, i + j - 1), c[i + j - 1] += a[i] * b.a[j]; return c; } }; struct node { int x, y; node(int x, int y) : x(x), y(y) {} }; const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; char a[55][55]; char op[105]; ia<5> b(4); int main() { for (int i = 1; i <= (b.n); i++) b[i] = i - 1; int n, m; scanf("%d%d", &n, &m); for (int i = (1); i <= (n); i++) scanf("%s", a[i] + 1); scanf("%s", op + 1); int len = strlen(op + 1); node st(0, 0), ed(0, 0); for (int i = (1); i <= (n); i++) for (int j = (1); j <= (m); j++) if (a[i][j] == 'S') st = node(i, j); else if (a[i][j] == 'E') ed = node(i, j); int ans = 0; while (1) { node now = st; bool flag = false; for (int i = (1); i <= (len); i++) { int x = op[i] - '0'; for (int j = (1); j <= (4); j++) if (x == b[j]) { now.x = now.x + dx[j]; now.y = now.y + dy[j]; break; } if (now.x < 1 || now.x > n) { flag = false; break; } if (now.y < 1 || now.y > m) { flag = false; break; } if (a[now.x][now.y] == '#') { flag = false; break; } if (a[now.x][now.y] == 'E') { flag = true; break; } } if (flag) ans++; if (!b.nexp()) break; } printf("%d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; #define SZ(x) (int)(x.size()) #define REP(i, n) for(int i=0;i<(n);++i) #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define RREP(i, n) for(int i=(int)(n)-1;i>=0;--i) #define RFOR(i, a, b) for(int i=(int)(b)-1;i>=(a);--i) #define ALL(a) a.begin(),a.end() #define DUMP(x) cerr<<#x<<" = "<<(x)<<endl #define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<int, int>; const double eps = 1e-8; const ll MOD = 1000000007; const int INF = INT_MAX / 2; const ll LINF = LLONG_MAX / 2; template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) { if (a < b) { a = b; return true; } return false; } template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) { if (a > b) { a = b; return true; } return false; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; REP(i, SZ(v)) { if (i) os << ", "; os << v[i]; } return os << "]"; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { os << "{"; int a = 0; for (auto &tp : mp) { if (a) os << ", "; a = 1; os << tp; } return os << "}"; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << ":" << p.second; return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); const int di[] = {0, 1, 0, -1, 1, 1, -1, -1}; const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1}; for (;;) { int N; cin >> N; if (N == 0) break; vll L(N); vi S(N); REP(i, N) { int m; cin >> m >> L[i]; REP(j, m) { int s, e; cin >> s >> e; FOR(t, s, e) { S[i] |= 1<<t-6; } } } vll dp(1<<16); REP(i, N) { REP(s, 1<<16) { if ((s & S[i]) == 0) { chmax(dp[s | S[i]], dp[s] + L[i]); } } } ll ans = *max_element(ALL(dp)); cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch; while (!isdigit(ch = getchar())) ; long long sum = ch ^ 48; while (isdigit(ch = getchar())) sum = (sum << 1) + (sum << 3) + (ch ^ 48); return sum; } int n, m, d_in[610], d_ou[610]; int tops, fst[610], to[100010], nxt[100010]; int ntops, top; long long p, dp[610][610]; queue<int> Q; long long mat[610][610], ans; inline long long fst_pow(long long x, long long p, long long mod) { long long re = 1; while (p) { if (p & 1) re = re * x % mod; x = x * x % mod, p >>= 1; } return re; } inline void add_edge(int u, int v) { to[tops] = v, nxt[tops] = fst[u], fst[u] = tops++; } inline void det() { ans = 1; int tmp, t; for (int i = 0; i < ntops; i++) { if (mat[i][i] == 0) { for (int j = i + 1; j < ntops; j++) if (mat[j][i] != 0) { for (int k = i; k < ntops; k++) mat[i][k] = (mat[i][k] + mat[j][k]) % p; break; } if (mat[i][i] == 0) { ans = 0; return; } } ans = 1ll * ans * mat[i][i] % p; tmp = (p - fst_pow(mat[i][i], p - 2, p)) % p; for (int j = i + 1; j < ntops; j++) if (mat[j][i] != 0) { t = 1ll * tmp * mat[j][i] % p; for (int k = i; k < ntops; k++) mat[j][k] = (mat[j][k] + 1ll * mat[i][k] * t % p) % p; } } } int main() { int u, v; n = read(), m = read(), p = read(); memset(fst, -1, sizeof(fst)); for (int i = 0; i < m; i++) { u = read(), v = read(); d_in[v]++, d_ou[u]++; add_edge(u, v); } for (int i = 1; i <= n; i++) if (d_in[i] == 0) { Q.push(i); dp[i][++ntops] = 1; } int now; while (!Q.empty()) { now = Q.front(), Q.pop(); for (int i = fst[now]; i != -1; i = nxt[i]) { if (--d_in[to[i]] == 0) Q.push(to[i]); for (int j = 1; j <= ntops; j++) dp[to[i]][j] = (dp[to[i]][j] + dp[now][j]) % p; } } for (int i = 1; i <= n; i++) if (d_ou[i] == 0) { top++; for (int j = 1; j <= ntops; j++) { mat[top - 1][j - 1] = dp[i][j]; } } det(); printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int OO = (int)2e9; const double eps = 1e-9; int daysInMonths[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int di[] = {-1, 0, 1, 0}; int dj[] = {0, 1, 0, -1}; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; if (n == 0 || n % 10 == 0) cout << n; else { int steps = 0, steps2 = 0; int n1, n2; int i, j; for (i = 0; i < 10; i++) { if ((n + i) % 10 == 0) break; steps++; } for (j = 0; j < 10; j++) { if ((n - j) % 10 == 0) { steps2++; break; } steps2++; } if (steps2 < steps) cout << n - j; else cout << n + i; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, OO = 1e8; int dp[100001]; int solve(int x) { if (x <= 0 || x > 1e5) return OO; if (x == m) return 0; int &ret = dp[x]; if (ret != -1) return ret; ret = OO; if (x > m) ret = min(ret, 1 + solve(x - 1)); else { ret = min(ret, 1 + solve(x * 2)); ret = min(ret, 1 + solve(x - 1)); } return ret; } int main() { cin >> n >> m; memset(dp, -1, sizeof(dp)); cout << solve(n) << endl; return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") using namespace std; const int N = 1e5 + 2; map<int, pair<long long, long long> > vec; vector<int> v; int a[N], b[N]; void add(int p, int i, int j) { pair<long long, long long> &x = vec[p]; x = pair<long long, long long>(x.first | (1LL << i), x.second | (1LL << j)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = int(0); i < int(n); ++i) cin >> a[i], a[i] *= 2; for (int j = int(0); j < int(m); ++j) cin >> b[j], b[j] *= 2; for (int i = int(0); i < int(n); ++i) { for (int j = int(0); j < int(m); ++j) { int y = (a[i] + b[j]) / 2; add(y, i, j); v.push_back(y); } } sort(v.begin(), v.end()); long long ans = 0; for (int i = int(0); i < int(int(v.size())); ++i) { for (int j = int(i); j < int(int(v.size())); ++j) { pair<long long, long long> foo = vec[v[i]], bar = vec[v[j]]; long long ret = __builtin_popcountll(foo.first | bar.first) + __builtin_popcountll(foo.second | bar.second); ans = max(ans, ret); } } cout << ans << endl; return 0; }
5
#include<bits/stdc++.h> #define ll long long #define mod 1000000007 #define N 100009 using namespace std; int n,a[N],fst[N],nxt[N],dgr[N],vis[N],num[N],f[N]; int main(){ scanf("%d",&n); int i,j,k,sz,tmp,rst; for (i=1; i<=n; i++){ scanf("%d",&a[i]); nxt[i]=fst[a[i]]; fst[a[i]]=i; dgr[a[i]]++; } int ans=1; bool flag; for (i=1; i<=n; i++) if (!vis[i]){ for (j=i; !vis[j]; j=a[j]) vis[j]=1; sz=0; flag=1; for (; vis[j]!=2; j=a[j]){ sz++; vis[j]=2; if (dgr[j]>2){ puts("0"); return 0; } else if (dgr[j]>1) flag=0; } if (flag){ num[sz]++; continue; } for (; dgr[j]==1; j=a[j]); j=a[j]; for (rst=0; vis[j]!=3; j=a[j]){ vis[j]=3; rst++; for (k=fst[j]; k; k=nxt[k]) if (vis[k]<2){ for (tmp=1; dgr[k]==1; k=fst[k]){ vis[k]=1; tmp++; } vis[k]=1; if (dgr[k] || tmp>rst){ puts("0"); return 0; } if (rst>tmp) ans=(ans<<1)%mod; rst=0; } } } for (i=1; i<=n; i++){ k=i>1 && (i&1)?2:1; for (j=f[0]=1; j<=num[i]; j++){ f[j]=f[j-1]*k%mod; if (j>1) f[j]=(f[j]+(ll)f[j-2]*(j-1)%mod*i)%mod; } ans=(ll)ans*f[num[i]]%mod; } printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long n, m; cin >> n >> m; long long arr[n][m]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> arr[i][j]; } } long long a[n][m], b[n][m], c[n][m], d[n][m]; a[0][0] = arr[0][0], b[n - 1][m - 1] = arr[n - 1][m - 1], c[n - 1][0] = arr[n - 1][0], d[0][m - 1] = arr[0][m - 1]; for (int i = 1; i < n; ++i) { a[i][0] = a[i - 1][0] + arr[i][0]; b[n - i - 1][m - 1] = b[n - i][m - 1] + arr[n - i - 1][m - 1]; c[n - i - 1][0] = c[n - i][0] + arr[n - i - 1][0]; d[i][m - 1] = d[i - 1][m - 1] + arr[i][m - 1]; } for (int i = 1; i < m; ++i) { a[0][i] = a[0][i - 1] + arr[0][i]; b[n - 1][m - i - 1] = b[n - 1][m - i] + arr[n - 1][m - i - 1]; c[n - 1][i] = c[n - 1][i - 1] + arr[n - 1][i]; d[0][m - i - 1] = d[0][m - i] + arr[0][m - i - 1]; } for (int i = 1; i < n; ++i) { for (int j = 1; j < m; ++j) { a[i][j] = arr[i][j] + max(a[i - 1][j], a[i][j - 1]); } } for (int i = n - 2; i >= 0; --i) { for (int j = m - 2; j >= 0; --j) { b[i][j] = arr[i][j] + max(b[i + 1][j], b[i][j + 1]); } } for (int i = n - 2; i >= 0; --i) { for (int j = 1; j < m; ++j) { c[i][j] = arr[i][j] + max(c[i + 1][j], c[i][j - 1]); } } for (int i = 1; i < n; ++i) { for (int j = m - 2; j >= 0; --j) { d[i][j] = arr[i][j] + max(d[i - 1][j], d[i][j + 1]); } } long long ans = 0; for (int i = 1; i < n - 1; ++i) { for (int j = 1; j < m - 1; ++j) { ans = max(ans, a[i - 1][j] + b[i + 1][j] + c[i][j - 1] + d[i][j + 1]); ans = max(ans, a[i][j - 1] + b[i][j + 1] + c[i + 1][j] + d[i - 1][j]); } } cout << ans << "\n"; return 0; }
4
#include <iostream> #include <cstdio> #include <cstdlib> #include <climits> using namespace std; #define N 8 //メリーゴーランドの台数 int M[N] = {4, 1, 4, 1, 2, 1, 2, 1}; int check_M[N] = {41412121, 14121214, 41212141, 12121414, 21214141,12141412, 21414121, 14141212}; int main(void) { int p[N]; //客の人数 int min; //乗り損ねた客の最小人数 while(cin >> p[0]) { for(int i = 1; i < N; ++i) { cin >> p[i]; } int ans; //p[0]の客が乗るメリーゴーランドの番号 min = INT_MAX; for(int i = 0; i < N; ++i) { //p[0]の客が乗るメリーゴーランドをM[i]とする int n = 0; for(int j = 0; j < N; ++j) { if(M[(i + j) % N] < p[j % N]) { n += p[j % N] - M[(i + j) % N]; } } if(n < min) { min = n; ans = i; } else if(n == min) { if(check_M[i] < check_M[ans]) ans = i; } } cout << M[ans % N]; for(int i = 1; i < N; ++i) { cout << ' ' << M[(ans + i) % N]; } cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; cin >> TESTS; while (TESTS--) { int n; cin >> n; set<long long int> s; for (int i = 0, a; i < n; i++) { cin >> a; s.insert(a); } cout << s.size() << endl; } return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops") using namespace std; const int MAXN = 100010; struct Node { int mn = 0, cnt = 0, sz = 0; }; Node Merge(Node x, Node y) { Node z; z.mn = min(x.mn, y.mn); if (z.mn == x.mn) z.cnt ^= x.cnt; if (z.mn == y.mn) z.cnt ^= y.cnt; z.sz = (x.sz ^ y.sz); return z; } int n, m, k, u, v, x, y, t, a, b, ans; int A[MAXN], B[MAXN], C[MAXN], D[MAXN]; Node seg[MAXN << 2]; int lazy[MAXN << 2]; vector<pair<pair<int, int>, int>> vec[MAXN]; vector<int> compx, compy; Node Build(int id, int tl, int tr) { lazy[id] = 0; if (tr - tl == 1) { seg[id].mn = 0; seg[id].sz = seg[id].cnt = (compx[tr] - compx[tl]) % 2; return seg[id]; } int mid = (tl + tr) >> 1; return seg[id] = Merge(Build(id << 1, tl, mid), Build(id << 1 | 1, mid, tr)); } inline void add_lazy(int id, int val) { seg[id].mn += val; lazy[id] += val; } inline void shift(int id) { if (!lazy[id]) return; add_lazy(id << 1, lazy[id]); add_lazy(id << 1 | 1, lazy[id]); lazy[id] = 0; } void Add(int id, int tl, int tr, int l, int r, int val) { if (r <= tl || tr <= l) return; if (l <= tl && tr <= r) { add_lazy(id, val); return; } shift(id); int mid = (tl + tr) >> 1; Add(id << 1, tl, mid, l, r, val); Add(id << 1 | 1, mid, tr, l, r, val); seg[id] = Merge(seg[id << 1], seg[id << 1 | 1]); } inline int f(int x, int bit) { return (x + (1 << bit) - 1) >> bit; } bool Solve(int bit) { compx.clear(); compy.clear(); for (int i = 0; i < MAXN; i++) vec[i].clear(); for (int i = 1; i <= m; i++) { int x = f(A[i], bit), y = f(B[i], bit), xx = f(C[i], bit), yy = f(D[i], bit); compx.push_back(x); compx.push_back(xx); compy.push_back(y); compy.push_back(yy); } sort(compx.begin(), compx.end()); sort(compy.begin(), compy.end()); compx.resize(unique(compx.begin(), compx.end()) - compx.begin()); compy.resize(unique(compy.begin(), compy.end()) - compy.begin()); compx.push_back(1000000001); compy.push_back(1000000001); for (int i = 1; i <= m; i++) { int x = f(A[i], bit), y = f(B[i], bit), xx = f(C[i], bit), yy = f(D[i], bit); x = lower_bound(compx.begin(), compx.end(), x) - compx.begin(); y = lower_bound(compy.begin(), compy.end(), y) - compy.begin(); xx = lower_bound(compx.begin(), compx.end(), xx) - compx.begin(); yy = lower_bound(compy.begin(), compy.end(), yy) - compy.begin(); vec[y].push_back({{x, xx}, +1}); vec[yy].push_back({{x, xx}, -1}); } int res = 0; n = compx.size() - 1; Build(1, 0, n); for (int i = 0; i + 1 < compy.size(); i++) { for (auto p : vec[i]) Add(1, 0, n, p.first.first, p.first.second, p.second); if ((compy[i + 1] - compy[i]) % 2 && seg[1].mn == 0) res ^= seg[1].sz ^ seg[1].cnt; } return res % 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) cin >> A[i] >> B[i] >> C[i] >> D[i], C[i]++, D[i]++; for (int i = 0; (1 << i) <= k; i++) if (Solve(i)) { cout << "Hamed\n"; return 0; } cout << "Malek\n"; return 0; }
5
#include <bits/stdc++.h> const double pi = 3.1415926535897932384626433832795; double EPS = 10e-6; const int INF = 2000000000; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } using namespace std; void ifd() {} void tme() {} int xx, yy, ans, cur_x = 350, cur_y = 350, cur_d = 0, len = 1, cnt = 0; int d[777][777]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { ifd(); d[cur_x][cur_y] = 0; while (len < 300) { for (int i = (0); i < (2); ++i) { for (int j = (0); j < (len); ++j) { cur_x += dx[cur_d]; cur_y += dy[cur_d]; d[cur_x][cur_y] = cnt; } cur_d++; cur_d %= 4; cnt++; } len++; } scanf("%d %d", &xx, &yy); xx += 350; yy += 350; printf("%d\n", d[xx][yy]); tme(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1 + 2e5; long long low[MAXN], num[MAXN], id[MAXN], vis[MAXN], vtime, nscc, n, m, f[MAXN], ans, x, sz[MAXN], sum[MAXN], dp[MAXN][2], res, kt; stack<int> s; vector<int> g[MAXN], p[MAXN]; long long dfs(int u) { vis[u] = 1; low[u] = num[u] = ++vtime; s.push(u); int v; for (int i = 0; i < g[u].size(); ++i) { v = g[u][i]; if (id[v] != -1) continue; for (int j = 0; j < g[v].size(); ++j) if (g[v][j] == u) g[v][j] = 0; if (!vis[v]) low[u] = min(low[u], dfs(v)); else low[u] = min(low[u], num[v]); } if (low[u] == num[u]) { while (1) { v = s.top(); s.pop(); id[v] = nscc; sz[nscc]++; if (v == u) break; } putchar('\n'); ++nscc; } return low[u]; } void dfs1(int u) { vis[u] = 1; long long d = sum[u]; long long kt = 0, ok = 0; for (int i = 0; i < p[u].size(); ++i) { int v = p[u][i]; if (vis[v] == 0) { dfs1(v); if (dp[v][0] != -1) { d += dp[v][0]; kt = max(kt, dp[v][1] - dp[v][0]); ok = 1; } else kt = max(kt, dp[v][1]); } } if (ok) dp[u][0] = d; else { dp[u][0] = sum[u]; if (sz[u] == 1) dp[u][0] = -1; } dp[u][1] = max(sum[u], d + kt); return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> f[i]; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } cin >> x; vtime = 0; nscc = 0; memset(id, 0xff, sizeof(id)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; ++i) if (!vis[i]) dfs(i); for (int i = 1; i <= n; ++i) sum[id[i]] += f[i]; for (int i = 1; i <= n; ++i) { for (int j = 0; j < g[i].size(); ++j) { int v = g[i][j]; if (id[i] != id[v] && v != 0) { p[id[i]].push_back(id[v]); p[id[v]].push_back(id[i]); } } } memset(vis, 0, sizeof vis); dfs1(id[x]); ans = max(ans, max(dp[id[x]][0], dp[id[x]][1])); cout << ans; return 0; }
5
#include<stdio.h> int main(){ int n=0, x; scanf("%d", &x); for(n=0;n<x;n+=1000){ n; } printf("%d", n-x); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, l; scanf("%d", &n); stack<int> s; for (int i = 1; i <= n; i++) { scanf("%d", &l); while (!s.empty() && s.top() >= i - l) s.pop(); s.push(i); } printf("%d", s.size()); return 0; }
2
#include <bits/stdc++.h> using namespace std; const double pi = acos(0) * 2; template <class T> inline T abs1(T a) { return a < 0 ? -a : a; } template <class T> inline T max1(T a, T b) { return a > b ? a : b; } template <class T> inline T min1(T a, T b) { return a < b ? a : b; } template <class T> inline T gcd1(T a, T b) { if (a < b) swap(a, b); if (a % b == 0) return b; return gcd1(b, a % b); } template <class T> inline T lb(T num) { return num & (-num); } template <class T> inline int bitnum(T num) { int ans = 0; while (num) { num -= lb(num); ans++; } return ans; } long long pow(long long n, long long m, long long mod = 0) { long long ans = 1; long long k = n; while (m) { if (m & 1) { ans *= k; if (mod) ans %= mod; } k *= k; if (mod) k %= mod; m >>= 1; } return ans; } int main() { int n, m; scanf("%d%d", &n, &m); cout << (pow(3, n, m) - 1 + m) % m << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; void execution(); int main() { ios_base::sync_with_stdio(0); execution(); return 0; } void execution() { long long n, m, sum = 0; cin >> n >> m; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; if (sum < m) { cout << -1; return; } long long l = 1, r = n; sort(a.rbegin(), a.rend()); while (r > l) { long long sr = (l + r) / 2, lans = 0; for (int i = 0, cnt = -1; i < n; i++) { if (i % sr == 0) cnt++; lans += max(0LL, (a[i] - cnt)); } if (lans >= m) r = sr; else l = sr + 1; } cout << l; }
4
#include <cstdio> using namespace std; int v,k; int main(){ scanf("%d", &v); if(v==0) return 0; for(int i=0;i<9;i++){ scanf("%d", &k); v -= k; } printf("%d\n", v); int main(); }
0
#include <bits/stdc++.h> using namespace std; long long dp[2][20][48][2520]; int id[2521], totid; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } int lcm(int a, int b) { if (b == 0) return a; return a * b / gcd(a, b); } char l[20]; int len; long long DP(int pos, int u, int lcmnow, int left) { if (pos == len) { return left % lcmnow == 0; } if (dp[u][pos][id[lcmnow]][left] == -1) { dp[u][pos][id[lcmnow]][left] = 0; int ed = (u == 0) ? 9 : (l[pos] - '0'); for (int i = 0; i <= ed; i++) { dp[u][pos][id[lcmnow]][left] += DP(pos + 1, (u == 1 && i == (l[pos] - '0')), lcm(lcmnow, i), (left * 10 + i) % 2520); } } return dp[u][pos][id[lcmnow]][left]; } long long Gao(long long num) { sprintf(l, "%020I64d", num); len = strlen(l); memset(dp[1], -1, sizeof(dp[1])); return DP(0, 1, 1, 0); } int main() { totid = 0; for (int i = 1; i <= 2520; i++) if (2520 % i == 0) id[i] = totid++; memset(dp[0], -1, sizeof(dp[0])); int t; scanf("%d", &t); for (int ft = 1; ft <= t; ft++) { long long l, r; scanf("%I64d%I64d", &r, &l); printf("%I64d\n", Gao(l) - Gao(r - 1)); } return 0; }
4
#include <bits/stdc++.h> using namespace std; double log2(double n) { return log(n) / log(2); } double ln(double n) { return log(n) / log(2.71828182846); } void Heap(signed long long int A[], signed long long int n, signed long long int l, signed long long int r) { signed long long int x, i, j; x = A[l]; i = l; while (1) { j = 2 * i; if (j > r) break; if (j < r && A[j + 1] >= A[j]) j++; if (x >= A[j]) break; A[i] = A[j]; i = j; } A[i] = x; } void HeapSort(signed long long int A[], signed long long int n) { signed long long int l, r, temp; l = n / 2; while (l >= 0) { Heap(A, n, l, n - 1); l--; } r = n - 1; while (r >= 1) { temp = A[0]; A[0] = A[r]; A[r] = temp; r--; Heap(A, n, 0, r); } } int mini(int a, int b) { if (a > b) return b; return a; } int maxi(int a, int b) { if (a > b) return a; return b; } bool issimple(unsigned long long n) { for (unsigned long long i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } unsigned long long nod(unsigned long long a, unsigned long long b) { unsigned long long temp; while (b) { a %= b; temp = a; a = b; b = temp; } return a; } unsigned long long nok(unsigned long long a, unsigned long long b, unsigned long long nod) { return a / nod * b; } int main() { long long int count, n, k, i, t = 1, j, m, x, y; char last; long long int max = 0, min = 0; bool yes, flag; cin >> t; int a[300003]; while (t--) { cin >> n; count = -1; min = 1000000010; max = -1; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] < min) { min = a[i]; x = i; } if (a[i] >= max) { max = a[i]; y = i; } } if (n > 2) { count = -1; for (i = 0; i < n; i++) { if (a[i] == max) { if (i == 0) { if (a[1] < a[0]) { count = 1; break; } } else if (i == n - 1) { if (a[n - 1] > a[n - 2]) { count = n; break; } } else { if (a[i] > a[i - 1] || a[i] > a[i + 1]) { count = i + 1; break; } } } } } else { if (a[0] == a[1]) count = -1; else if (a[0] > a[1]) count = 1; else count = 2; } cout << count << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-11; int main() { int N, M; int a, b, r, si, ei, ti; scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) { scanf("%d %d %d", &si, &ei, &ti); si--; ei--; if (si == ei) { printf("%d\n", ti); continue; } r = ti; a = ti % (2 * (M - 1)); if (a <= M - 1) { if (si >= a) { r += si - a; if (ei >= si) { r += ei - si; } else { r += (M - 1 - si) + (M - 1 - ei); } } else { r += (M - 1 - a) + (M - 1 - si); if (ei <= si) { r += si - ei; } else { r += si + ei; } } } else { a = 2 * (M - 1) - a; if (si <= a) { r += a - si; if (ei <= si) { r += si - ei; } else { r += si + ei; } } else { r += a + si; if (ei >= si) { r += ei - si; } else { r += (M - 1 - si) + (M - 1 - ei); } } } printf("%d\n", r); } return 0; }
1
#include <bits/stdc++.h> using namespace std; char s[1000005]; int n = 0, m; char s1[2000005]; int nex[1000005]; struct node { int ch[26], pre, l; } t[2000005]; int ndtot = 1; int num[2000005] = {0}; int cnt[1000005] = {0}, seq[2000005]; int rt = 1, las = 1; void add(int c) { int p = las, np = ++ndtot; las = np; num[np] = 1; cnt[t[np].l = t[p].l + 1]++; for (; p && !t[p].ch[c]; p = t[p].pre) t[p].ch[c] = np; if (!p) t[np].pre = rt; else { int q = t[p].ch[c]; if (t[q].l == t[p].l + 1) t[np].pre = q; else { int nq = ++ndtot; t[nq] = t[q]; cnt[t[nq].l = t[p].l + 1]++; t[q].pre = t[np].pre = nq; for (; p && t[p].ch[c] == q; p = t[p].pre) t[p].ch[c] = nq; } } } int main() { scanf("%s", s + 1); for (int i = 1; s[i]; i++) { n++; add(s[i] - 'a'); } cnt[0] = 1; for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; for (int i = 1; i <= ndtot; i++) seq[cnt[t[i].l]--] = i; for (int i = ndtot; i >= 2; i--) num[t[seq[i]].pre] += num[seq[i]]; int que; scanf("%d", &que); while (que--) { scanf("%s", s1 + 1); m = strlen(s1 + 1); nex[1] = 0; for (int i = 2, j = 0; i <= m; i++) { while (j && s1[j + 1] != s1[i]) j = nex[j]; if (s1[j + 1] == s1[i]) j++; nex[i] = j; } int r; int cy = m - nex[m]; if (m % cy == 0) r = cy; else r = m; for (int i = m + 1; i <= m + r; i++) s1[i] = s1[i - m]; int p = rt; int ans = 0; for (int i = 0, j = 1; i < r; j++) { while (p && !t[p].ch[s1[j] - 'a']) { p = t[p].pre; i = j - 1 - t[p].l; } if (t[p].ch[s1[j] - 'a']) p = t[p].ch[s1[j] - 'a']; else p = rt, i = j; if (j == i + m) { ans += num[p]; if (t[t[p].pre].l == m - 1) p = t[p].pre; i++; } } printf("%d\n", ans); } return 0; }
3
#include <bits/stdc++.h> using namespace std; struct abc { long long int x, y; }; bool compare(abc a1, abc a2) { if (a1.x == a2.x) return a1.y < a2.y; return a1.x < a2.x; } const long long int mod = 1e9; long long int a[100005]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n, k, m; cin >> n >> k >> m; long long int a[n + 1]; for (long long int i = 1; i <= n; i++) { if (k > 0) a[i] = m; else a[i] = 1; k = a[i] - k; } a[n] -= k; if (a[n] <= 0 || a[n] > m) cout << -1; else { for (long long int i = 1; i <= n; i++) cout << a[i] << ' '; } }
2
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } const char lf = '\n'; namespace ae86 { const int bufl = 1 << 15; char buf[bufl], *s = buf, *t = buf; inline int fetch() { if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; } return *s++; } inline int ty() { int a = 0; int b = 1, c = fetch(); while (!isdigit(c)) b ^= c == '-', c = fetch(); while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); return b ? a : -a; } } // namespace ae86 using ae86::ty; const int _ = 207, __ = 30007; inline void failure() { cout << -1 << lf; exit(0); } int n, m, loc[_] = {0}, dis[_][__] = {0}, ps[_][__] = {0}, ed[__] = {0}, las[__] = {0}; vector<pair<int, int>> has[__]; int main() { ios::sync_with_stdio(0), cout.tie(nullptr); n = ty(), m = ty(); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { dis[i][j] = ty(); if (!dis[i][j]) loc[i] = j; } if (!loc[i]) failure(); } for (int i = 2; i <= m; i++) { for (int j = 1; j <= n; j++) if (dis[1][j] + dis[i][j] == dis[1][loc[i]]) ps[i][dis[1][j]] = j, ed[j] = 1; for (int j = dis[1][loc[i]]; j >= 1; j--) { if (!ps[i][j]) failure(); las[ps[i][j]] = ps[i][j - 1]; } } ed[loc[1]] = 1; for (int i = 1; i <= n; i++) { if (ed[i]) continue; int mx = 0, mxloc = loc[1]; for (int j = 2; j <= m; j++) { int a = (dis[1][loc[j]] + dis[1][i] - dis[j][i]) >> 1; if (a >= mx) mx = a, mxloc = ps[j][a]; } has[mxloc].emplace_back(dis[1][i], i); } for (int j = 1; j <= n; j++) { sort(has[j].begin(), has[j].end()); for (int i = 0, pre = dis[1][j], now = j; i < ((int)((has[j]).size()));) { pre++; if (pre != has[j][i].first) failure(); while (i < ((int)((has[j]).size())) - 1 && has[j][i + 1].first == pre) las[has[j][i].second] = now, i++; las[has[j][i].second] = now, now = has[j][i].second, i++; } } for (int i = 1; i <= n; i++) if (las[i]) cout << las[i] << ' ' << i << lf; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MX = 5e5 + 5; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); template <class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); 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 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 T, class... Ts> void re(T& t, Ts&... ts) { re(t); re(ts...); } template <class T> void re(complex<T>& x) { T a, b; re(a, b); x = cd(a, b); } 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]); } } // namespace input using namespace input; namespace output { void pr(int x) { cout << x; } void pr(long x) { cout << x; } void pr(long long x) { cout << x; } void pr(unsigned x) { cout << x; } void pr(unsigned long x) { cout << x; } void pr(unsigned long long x) { cout << x; } void pr(float x) { cout << x; } void pr(double x) { cout << x; } void pr(long double x) { cout << x; } void pr(char x) { cout << x; } void pr(const char* x) { cout << x; } void pr(const string& x) { cout << x; } void pr(bool x) { pr(x ? "true" : "false"); } template <class T> void pr(const complex<T>& x) { cout << x; } template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T> void pr(const T& x); template <class T, class... Ts> void pr(const T& t, const Ts&... ts) { pr(t); pr(ts...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr("{", x.first, ", ", x.second, "}"); } template <class T> void pr(const T& x) { pr("{"); bool fst = 1; for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0; pr("}"); } void ps() { pr("\n"); } template <class T, class... Ts> void ps(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); } void pc() { pr("]\n"); } template <class T, class... Ts> void pc(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(", "); pc(ts...); } } // namespace output using namespace output; namespace io { void setIn(string second) { freopen(second.c_str(), "r", stdin); } void setOut(string second) { freopen(second.c_str(), "w", stdout); } void setIO(string second = "") { cin.sync_with_stdio(0); cin.tie(0); if ((int)second.size()) { setIn(second + ".in"), setOut(second + ".out"); } } } // namespace io using namespace io; typedef decay<decltype(MOD)>::type T; struct mi { T val; explicit operator T() const { return val; } mi() { val = 0; } mi(const long long& v) { val = (-MOD <= v && v <= MOD) ? v : v % MOD; if (val < 0) val += MOD; } friend void pr(const mi& a) { pr(a.val); } friend void re(mi& a) { long long x; re(x); a = mi(x); } friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; } friend bool operator!=(const mi& a, const mi& b) { return !(a == b); } friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; } mi operator-() const { return mi(-val); } mi& operator+=(const mi& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; } mi& operator-=(const mi& m) { if ((val -= m.val) < 0) val += MOD; return *this; } mi& operator*=(const mi& m) { val = (long long)val * m.val % MOD; return *this; } friend mi pow(mi a, long long p) { mi ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend mi inv(const mi& a) { assert(a != 0); return pow(a, MOD - 2); } mi& operator/=(const mi& m) { return (*this) *= inv(m); } friend mi operator+(mi a, const mi& b) { return a += b; } friend mi operator-(mi a, const mi& b) { return a -= b; } friend mi operator*(mi a, const mi& b) { return a *= b; } friend mi operator/(mi a, const mi& b) { return a /= b; } }; template <int SZ> struct BCC { int N; vector<pair<int, int> > adj[SZ], ed; void addEdge(int u, int v) { adj[u].push_back({v, (int)ed.size()}), adj[v].push_back({u, (int)ed.size()}); ed.push_back({u, v}); } int disc[SZ]; vector<int> st; vector<vector<int> > bccs; int bcc(int u, int p = -1) { static int ti = 0; disc[u] = ++ti; int low = disc[u]; int child = 0; for (auto& i : adj[u]) if (i.second != p) { if (!disc[i.first]) { child++; st.push_back(i.second); int LOW = bcc(i.first, i.second); ckmin(low, LOW); if (disc[u] <= LOW) { bccs.emplace_back(); vector<int>& tmp = bccs.back(); for (bool done = 0; !done; tmp.push_back(st.back()), st.pop_back()) done |= st.back() == i.second; } } else if (disc[i.first] < disc[u]) { ckmin(low, disc[i.first]); st.push_back(i.second); } } return low; } void init(int _N) { N = _N; for (int i = (0); i < (N); ++i) disc[i] = 0; for (int i = (0); i < (N); ++i) if (!disc[i]) bcc(i); } }; BCC<MX> B; int n, m, cnt[MX], label[MX], dp[MX], DP[MX]; vector<pair<int, int> > tmp[MX]; vector<pair<int, int> > v; int dfs(int x, int y) { for (auto& t : tmp[x]) if (t.second < y) return dfs(t.first, t.second); return y; } int main() { cin.sync_with_stdio(0); cin.tie(0); re(n, m); v.resize(m); re(v); reverse(begin(v), end(v)); for (auto& t : v) B.addEdge(t.first, t.second); B.init(n + 1); for (int i = (0); i < ((int)B.bccs.size()); ++i) for (auto& j : B.bccs[i]) label[j] = i; for (int i = (1); i < (n + 1); ++i) dp[i] = 1; for (int i = (0); i < ((int)v.size()); ++i) { int L = label[i]; cnt[L]++; long long sum = dp[v[i].first] + dp[v[i].second]; if (cnt[L] > 1 && cnt[L] == (int)B.bccs[L].size()) { for (auto& t : B.bccs[L]) if (t != i) { int u = B.ed[t].first, v = B.ed[t].second; tmp[u].push_back({v, t}), tmp[v].push_back({u, t}); } int a = dfs(v[i].first, MOD); int b = dfs(v[i].second, MOD); if (a == b) sum -= DP[a]; for (auto& t : B.bccs[L]) if (t != i) { int u = B.ed[t].first, v = B.ed[t].second; tmp[u].clear(), tmp[v].clear(); } } dp[v[i].first] = dp[v[i].second] = DP[i] = sum; } for (int i = (1); i < (n + 1); ++i) pr(dp[i] - 1, ' '); }
5
#include<stdio.h> const int maxn=1000005; int i,j,k,m,n,l,r; int a[maxn]; int check(int x){ for(int i=n,j=n;i>1&&j<m;i--,j++){ if((a[i]<=x&&a[i-1]<=x)||(a[j]<=x&&a[j+1]<=x)) return 1; if((a[i]>x&&a[i-1]>x)||(a[j]>x&&a[j+1]>x)) return 0; } return a[1]<=x; } int main(){ scanf("%d",&n); m=(n<<1)-1; for(i=1;i<=m;i++) scanf("%d",&a[i]); l=0,r=m+1; while(l+1<r){ int mid=(l+r)>>1; if(check(mid)) r=mid; else l=mid; } printf("%d\n",r); return 0; }
0
#include <set> #include <map> #include <list> #include <queue> #include <stack> #include <cmath> #include <ctype.h> #include <ctime> #include <cstdio> #include <vector> #include <string> #include <bitset> #include <cctype> #include <cstdlib> #include <cstring> #include <utility> #include <numeric> #include <complex> #include <sstream> #include <fstream> #include <iomanip> #include <cassert> #include <iostream> #include <iterator> #include <algorithm> using namespace std; #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define lengthof(x) (sizeof(x) / sizeof(*(x))) #define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value)) template <typename T> void FILL_(void * ptr, size_t size, T value){ std::fill((T*)ptr, (T*)ptr+size, value); } //4方向ベクトル→↑←↓ int dx[] ={1,0,-1,0}; int dy[] ={0,-1,0,1}; int main() { double xa,xb,xc,xd,ya,yb,yc,yd; while(scanf("%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",&xa,&ya,&xb,&yb,&xc,&yc,&xd,&yd)!=EOF){ double td =(xa-xc)*(yd-ya)+(ya-yc)*(xa-xd); double tb =(xa-xc)*(yb-ya)+(ya-yc)*(xa-xb); double ta =(xb-xd)*(ya-yb)+(yb-yd)*(xb-xa); double tc =(xb-xd)*(yc-yb)+(yb-yd)*(xb-xc); if(td*tb >0.0||ta*tc>0.0) cout <<"NO"<<endl; else cout <<"YES"<<endl; } return 0; }
0