solution
stringlengths
52
181k
difficulty
int64
0
6
#include<bits/stdc++.h> #define endl enjoy_codeforces int main(){ int n;std::cin>>n; std::vector<int>sz={n/3,(n+2)/3,(n+1)/3}; std::vector<std::vector<int>>g(n); for(int i=0;i<n-1;i++){ int u,v;std::cin>>u>>v;u--,v--; g.at(u).push_back(v); g.at(v).push_back(u); } std::vector<int>a(n,-1); std::queue<int>que; que.push(0); a.at(0)=0; while(!que.empty()){ int x=que.front();que.pop(); for(int y:g.at(x)){ if(a.at(y)==-1){ a.at(y)=!a.at(x); que.push(y); } } } std::vector<int>b,c; for(int i=0;i<n;i++)(a.at(i)?b:c).push_back(i); std::vector<int>p(n); for(int x:{1,2}){ if(b.size()<c.size())std::swap(b,c); for(int i=x;i<=n;i+=3){ p.at(b.back())=i; b.pop_back(); } } for(int i=0,j=3;i<n;i++){ if(p.at(i)==0){ p.at(i)=j; j+=3; } } for(int i=0;i<n;i++){ std::cout<<(i?" ":"")<<p.at(i); } std::cout<<'\n'; } /* 二部グラフです。 余りが 1 同士、2 同士が対岸にこないようにします。 大きい方に 1 を入れて、残りの内大きい方に 2 を入れれば良いです。 */
0
#include<iostream> #include<cstdio> using namespace std; int ans,cov[222222],n,m,x,y,a[222222],cnt[222222],pos; int main() { scanf("%d%d",&n,&m); ans=n; for (int i=1;i<=n;i++) { scanf("%d",&a[i]); cnt[a[i]]++; } for (int i=1;i<=n;i++) { pos=i-cnt[i]+1; if (pos<=0) pos=1; for (int j=pos;j<=i;j++) { if (!cov[j]) { ans--; } cov[j]++; } } while(m--) { scanf("%d%d",&x,&y); pos=a[x]-cnt[a[x]]+1; if (pos>0) { cov[pos]--; if (!cov[pos]) ans++; } cnt[a[x]]--; a[x]=y; cnt[a[x]]++; pos=a[x]-cnt[a[x]]+1; if (pos>0) { if (!cov[pos]) ans--; cov[pos]++; } printf("%d\n",ans); } return 0; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:667772160") using namespace std; struct __isoff { __isoff() { if (0) freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); srand(time(0)); } ~__isoff() {} } __osafwf; const unsigned long long p1 = 131797; const unsigned long long p2 = 435493969; const double eps = 1e-7; const double pi = acos(-1.0); const long long inf = 1e18 + 7; const int infi = 1e9 + 7; const long long dd = 2e5 + 7; const long long mod = 1e9 + 7; long long fac[1000]; int n, k, c; long long sd; vector<long long> T; vector<long long> sum; long long res = 0; map<long long, int> D[30]; void dfs(int v, int t, long long sum) { if (sum > sd || t > k) return; if (v == n / 2) { for (long long j = 0; j < (long long)k - t + 1; j++) D[j][sum]++; return; } if (T[v] <= c) dfs(v + 1, t + 1, sum + fac[T[v]]); if (rand() % 2) { dfs(v + 1, t, sum + T[v]); dfs(v + 1, t, sum); } else { dfs(v + 1, t, sum); dfs(v + 1, t, sum + T[v]); } } void dfs2(int v, int t, long long sum) { if (sum > sd || t > k) return; if (v == n) { if (D[t].count(sd - sum)) res += D[t][sd - sum]; return; } if (T[v] <= c) dfs2(v + 1, t + 1, sum + fac[T[v]]); if (rand() % 2) { dfs2(v + 1, t, sum + T[v]); dfs2(v + 1, t, sum); } else { dfs2(v + 1, t, sum); dfs2(v + 1, t, sum + T[v]); } } int main() { fac[0] = 1; for (long long i = (long long)1; i < (long long)1000; i++) { fac[i] = fac[i - 1] * i; c = i; if (fac[i] > 1e16) break; } cin >> n >> k >> sd; T.resize(n); for (long long i = 0; i < (long long)n; i++) { cin >> T[i]; } sort(T.begin(), T.end()); dfs(0, 0, 0); dfs2(n / 2, 0, 0); cout << res; }
5
#include <bits/stdc++.h> using namespace std; const int MAX = 1000001; int main() { int t, n, k; long long int a[1001]; cin >> t; while (t--) { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); long long int s = a[0]; long long int r = 0, ans = 0; for (int i = 1; i < n; i++) { r = k - a[i]; ans += (r / s); } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, sum; int main() { cin >> n; int a[n]; cin >> a[0]; sum = (2 * n) - 1 + a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; sum += abs(a[i] - a[i - 1]); } cout << sum; }
2
#include <bits/stdc++.h> using namespace std; string a; int n; int main() { cin >> a; cin >> n; for (int i = 0; i < a.size(); i++) { a[i] = tolower(a[i]); if (a[i] < 97 + n) a[i] = toupper(a[i]); } cout << a << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, c, t, count = 0; cin >> n >> t >> c; long long int a[n]; for (long long int i = 0; i < n; ++i) { cin >> a[i]; } bool flag = true; long long int l = 0; for (long long int i = 0; i < n; i++) { if (a[i] <= t) { l++; } else { if (l >= c) { count += l - c + 1; } l = 0; } } if (l >= c) { count += l - c + 1; } cout << count << endl; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; mt19937 eng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 2e5 + 4; int n, q; int a[N]; pii pos[N]; bool here; void read() { cin >> n >> q; for (int i = 1; i <= n; ++i) { cin >> a[i]; here = (here || (a[i] == q)); } } void updatePos(int x, int it) { if (pos[x].first == 0) { pos[x] = {it, it}; } else { pos[x].second = it; } } void removeZeros() { for (int i = 1; i <= n; ++i) { if (a[i] == 0) { if (!here) { a[i] = q; here = true; } else { a[i] = max(a[i - 1], 1); } } updatePos(a[i], i); } } bool check(int l, int r, int val) { return !(pos[val].second < l || pos[val].first > r); } bool f(int l, int r, int val) { if (l >= r) { return true; } while (!check(l, r, val)) { --val; } int cl = max(l, pos[val].first); int cr = min(r, pos[val].second); for (int i = cl; i <= cr; ++i) { if (a[i] != val) { return false; } } return f(l, cl - 1, val - 1) && f(cr + 1, r, val - 1); } void solve() { read(); removeZeros(); if (!here) { cout << "NO\n"; return; } if (f(1, n, q)) { cout << "YES\n"; for (int i = 1; i <= n; ++i) { cout << a[i] << " "; } } else { cout << "NO\n"; } } int main() { ios::sync_with_stdio(0); cout.tie(0), cin.tie(0); cout << setprecision(14) << fixed; int z = 1; while (z--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { { ios ::sync_with_stdio(false); cin.tie(0); }; ; long long n, k; cin >> n >> k; if (n >= k) { if (k % 2 == 0) { cout << (k / 2) - 1; return 0; } else { cout << k / 2; return 0; } } else { if (k % 2 == 0) { if (n <= k / 2) { cout << "0"; return 0; } else { cout << (n - (k / 2)); return 0; } } else { if (n < k / 2) { cout << "0"; return 0; } else { cout << n - (k / 2); return 0; } } } }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 1000; const int mod = 1000000000 + 7; char s[maxn]; int n; int a[5]; long long ans; long long pow_q(long long x, int n) { ans = 1; while (n) { if (n & 1) ans = (ans * x) % mod; x = (x * x) % mod; n = n >> 1; } return ans; } int main() { while (~scanf("%d", &n)) { scanf("%s", s); memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) { if (s[i] == 'A') a[0]++; if (s[i] == 'C') a[1]++; if (s[i] == 'G') a[2]++; if (s[i] == 'T') a[3]++; } sort(a, a + 4); int maxc = a[3]; int cou = 0; for (int i = 0; i < 4; i++) if (a[i] == maxc) cou++; long long ans; ans = pow_q(cou, n); cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; double Dist(double x, double y) { return x * x + y * y; } bool Inside(long long x1, long long y1, long long x2, long long y2, long long x, long long y) { return x >= min(x1, x2) && x <= max(x1, x2) && y >= min(y1, y2) && y <= max(y1, y2); } double Nearest_Dist(long long x1, long long y1, long long x2, long long y2) { long long dy, dx, a1, b1, c1, a2, b2, c2; dy = y2 - y1, dx = x2 - x1; a1 = dy, b1 = -dx, c1 = dx * y1 - dy * x1; a2 = dx, b2 = dy, c2 = 0; long long x = b1 * c2 - b2 * c1, y = c1 * a2 - c2 * a1, d = a1 * b2 - a2 * b1; if (Inside(x1 * d, y1 * d, x2 * d, y2 * d, x, y)) return Dist((double)x / (double)d, (double)y / (double)d); return min(Dist(x1, y1), Dist(x2, y2)); } pair<long long, long long> Pair[500007 / 5]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); double Farest = 0, Nearest = 1e16, res; long long n; long long x, y; cin >> n >> x >> y; for (long long i = 1; i <= n; ++i) { cin >> Pair[i].first >> Pair[i].second, Pair[i].first -= x, Pair[i].second -= y; Farest = max(Farest, Dist(Pair[i].first, Pair[i].second)); } for (long long i = 1; i < n; ++i) Nearest = min(Nearest, Nearest_Dist(Pair[i].first, Pair[i].second, Pair[i + 1].first, Pair[i + 1].second)); Nearest = min(Nearest_Dist(Pair[1].first, Pair[1].second, Pair[n].first, Pair[n].second), Nearest); res = acos(-1.0) * (Farest - Nearest); std::cout << std::setprecision(100) << res << '\n'; return 0; }
1
#include<bits/stdc++.h> using namespace std; string a,b,c,d; int i=0,j=0,k=0,l=0,x,y; void reada(){ if(i>=a.size()) return; c=""; while(i<a.size()&&a[i]!='['){ if(a[i]=='(') k++; if(a[i]==')') k--; c+=a[i]; i++; } i++;x=0; if(i>=a.size()) return; while(a[i]!=']') x=x*10+a[i]-'0',i++; i++; } void readb(){ if(j>=b.size()) return; d=""; while(j<b.size()&&b[j]!='['){ if(b[j]=='(') l++; if(b[j]==')') l--; d+=b[j]; j++; } j++;y=0; if(j>=b.size()) return; while(b[j]!=']') y=y*10+b[j]-'0',j++; j++; } int main(){ cin>>a>>b; while(1){ reada(); if(i>=a.size()){ cout << c << endl; break; } readb(); if(j>=b.size()){ cout << d << endl; break; } if(k>l){ while(k>l) reada(); if(i>=a.size()){ cout << d << endl; break; } cout << d << "[" << x+y << "]"; }else{ while(k<l) readb(); if(j>=b.size()){ cout << c << endl; break; } cout << c << "[" << x+y << "]"; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[n]; int t = 0; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; t = min(a[0], b[0]) + 1; if (n >= 2) { for (int i = 1; i < n; i++) if (a[i] != a[i - 1] || b[i] != b[i - 1]) { if (a[i - 1] != b[i - 1]) { if ((min(a[i], b[i]) - max(a[i - 1], b[i - 1])) > 0) t += min(a[i], b[i]) - max(a[i - 1], b[i - 1]) + 1; else if ((min(a[i], b[i]) - max(a[i - 1], b[i - 1])) == 0) { if (min(a[i], b[i]) - min(a[i - 1], b[i - 1]) > 0) t += min(a[i], b[i]) - max(a[i - 1], b[i - 1]) + 1; } } if (a[i - 1] == b[i - 1]) { if ((min(a[i], b[i]) - max(a[i - 1], b[i - 1])) > 0) t += min(a[i], b[i]) - max(a[i - 1], b[i - 1]); else if ((min(a[i], b[i]) - max(a[i - 1], b[i - 1])) == 0) { if (min(a[i], b[i]) - min(a[i - 1], b[i - 1]) > 0) t += min(a[i], b[i]) - max(a[i - 1], b[i - 1]); } } } } cout << t << "\n"; }
2
#include <bits/stdc++.h> using namespace std; long long po[300010]; int arr[300010]; int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &arr[i]); sort(arr, arr + n); vector<int> v; for (int i = 0; i < n - 1; i++) { v.push_back(arr[i + 1] - arr[i]); } po[0] = 1; for (int i = 0; i <= n; i++) { po[i + 1] = ((po[i] * 2LL)) % 1000000007; } long long tot = 0; for (int i = 0; i < (int)v.size(); i++) { long long z = ((po[i + 1] - 1) * (po[(int)v.size() - i] - 1)) % 1000000007; z *= v[i]; z %= 1000000007; tot += z; tot %= 1000000007; } cout << tot; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long double epsilon = 1e-9; int mat2[1 << 18]; int idx2[1 << 18]; int minm[1 << 18]; int maxm[1 << 18]; int main() { int h, m, n; cin >> h >> m >> n; memset(minm, -1, sizeof(minm)); memset(maxm, -1, sizeof(maxm)); int idx = 0; for (int i = 0; i < h; i++) { if (minm[i] == -1) { minm[i] = idx; mat2[idx++] = i; int tmp = (i + m) % h; while (tmp != i) { mat2[idx++] = tmp; tmp = (tmp + m) % h; } maxm[i] = idx - 1; tmp = (i + m) % h; while (tmp != i) { maxm[tmp] = maxm[i]; minm[tmp] = minm[i]; tmp = (tmp + m) % h; } } } for (int i = 0; i < h; i++) { idx2[mat2[i]] = i; } set<int> s; for (int i = 0; i < h; i++) s.insert(i); long long cnt = 0; string str; map<int, int> id_map; set<int>::iterator itr; int x, y; for (int i = 0; i < n; i++) { cin >> str; if (str == "+") { scanf("%d", &x); scanf("%d", &y); int place; itr = s.lower_bound(idx2[y]); if (itr != s.end() && *itr <= maxm[y]) { cnt += (long long)*itr - idx2[y]; place = *itr; } else { cnt += (long long)maxm[y] - idx2[y]; itr = s.lower_bound(minm[y]); cnt += (long long)*itr - minm[y] + 1; place = *itr; } s.erase(place); id_map.insert(make_pair(x, place)); } else { scanf("%d", &x); s.insert(id_map[x]); id_map.erase(x); } } cout << cnt << endl; return 0; }
3
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; if(a*b*c==175){ cout << "YES" << endl; }else{ cout << "NO" << endl; } }
0
#include <bits/stdc++.h> using namespace std; struct task { long long intime; long long starttime; long long runtime; long long endtime; }; int main() { long long i = 0, b, n, currtime = 0, stoptime = -1; queue<long long> z; bool runflg = 0; cin >> n >> b; vector<task> all(n); for (i = 0; i < n; i++) { cin >> all[i].intime >> all[i].runtime; all[i].starttime = -1; all[i].endtime = -2; } i = 0; while (i < n || z.size() > 0) { if (stoptime == currtime) { stoptime = -1; runflg = 0; } if (!runflg && z.size() > 0) { all[z.front()].starttime = currtime; all[z.front()].endtime = currtime + all[z.front()].runtime; stoptime = all[z.front()].endtime; z.pop(); runflg = 1; } if (i < n && all[i].intime == currtime && z.size() < b) { z.push(i); i++; } else if (i < n && all[i].intime == currtime && z.size() >= b) { all[i].endtime = -1; i++; } if (!runflg && z.size() > 0) { all[z.front()].starttime = currtime; all[z.front()].endtime = currtime + all[z.front()].runtime; stoptime = all[z.front()].endtime; z.pop(); runflg = 1; } if (i < n && runflg) currtime = min(stoptime, all[i].intime); if (i >= n && runflg) currtime = stoptime; if (i < n && !runflg) currtime = all[i].intime; if (i >= n && !runflg) currtime++; } for (i = 0; i < n; i++) { cout << all[i].endtime << ' '; } return 0; }
2
/* ---------- STL Libraries ---------- */ // IO library #include <cstdio> #include <fstream> #include <iomanip> #include <ios> #include <iostream> // algorithm library #include <algorithm> #include <cmath> #include <numeric> // container library #include <array> #include <bitset> #include <deque> #include <map> #include <queue> #include <set> #include <string> #include <vector> /* ---------- Namespace ---------- */ using namespace std; /* ---------- Type Abbreviation ---------- */ template <typename T> using V = vector<T>; template <typename T, typename U> using P = pair<T, U>; template <typename T> using PQ = priority_queue<T>; template <typename T> using GPQ = priority_queue<T, vector<T>, greater<T>>; using ll = long long; #define fst first #define snd second #define pb push_back #define mp make_pair #define mt make_tuple /* ---------- conversion ---------- */ #define INT(c) static_cast<int>(c) #define CHAR(n) static_cast<char>(n) #define LL(n) static_cast<ll>(n) #define DOUBLE(n) static_cast<double>(n) /* ---------- container ---------- */ #define ALL(v) (v).begin(), (v).end() #define SIZE(v) (LL((v).size())) #define FIND(v, k) (v).find(k) != (v).end() #define VFIND(v, k) find(ALL(v), k) != (v).end() #define gsort(b, e) sort(b, e, greater<decltype(*b)>()) #define SORT(v) sort(ALL(v)) #define GSORT(v) gsort(ALL(v)) /* ---------- repetition ---------- */ #define FOR(i, a, b) for (ll i = (a); i <= (b); ++i) #define RFOR(i, a, b) for (ll i = (a); i >= (b); --i) /* ----------- debug ---------- */ template <class T> ostream& operator<<(ostream& os, vector<T> v) { os << "["; for (auto vv : v) os << vv << ","; return os << "]"; } template <class T> ostream& operator<<(ostream& os, set<T> v) { os << "["; for (auto vv : v) os << vv << ","; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> p) { return os << "(" << p.fst << "," << p.snd << ")"; } /* ---------- Constants ---------- */ // const ll MOD = 1e9 + 7; // const int INF = 1 << 25; // const ll INF = 1LL << 50; // const double PI = acos(-1); // const double EPS = 1e-10; // const ll dx[4] = {0, -1, 1, 0}; // const ll dy[4] = {-1, 0, 0, 1}; /* ---------- Short Functions ---------- */ template <typename T> T sq(T a) { return a * a; } template <typename T> T gcd(T a, T b) { if (a > b) return gcd(b, a); return a == 0 ? b : gcd(b % a, a); } template <typename T, typename U> T mypow(T b, U n) { if (n == 0) return 1; if (n == 1) return b /* % MOD */; if (n % 2 == 0) { return mypow(sq(b) /* % MOD */, n / 2); } else { return mypow(b, n - 1) * b /* % MOD */; } } ll pcnt(ll b) { return __builtin_popcountll(b); } /* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */ // #define DEBUG // #define DEBUG_CASE_NUM 1 V<string> fromjfen(string S) { S += "/"; V<string> ret; string sub; for (char c : S) { if (c == '/') { ret.pb(sub); sub = ""; } else if (c == 'b') { sub += "b"; } else { sub += string(c - '0', '.'); } } return ret; } string tojfen(V<string> field) { string ret; for (string line : field) { line += "b"; ll cnt = 0; for (char c : line) { if (c == 'b') { if (cnt > 0) { ret += to_string(cnt); cnt = 0; } ret += "b"; } else { ++cnt; } } ret.pop_back(); ret += "/"; } ret.pop_back(); return ret; } void solve() { while (true) { string S; cin >> S; if (S == "#") break; V<string> field = fromjfen(S); // cout << field << endl; ll a, b, c, d; cin >> a >> b >> c >> d; field[a - 1][b - 1] = '.'; field[c - 1][d - 1] = 'b'; cout << tojfen(field) << endl; } return; } int main() { cin.tie(0); ios::sync_with_stdio(false); // cout << fixed << setprecision(10); #ifdef DEBUG freopen("input.txt", "r", stdin); FOR(_, 1, DEBUG_CASE_NUM) { solve(); cout << "++++++++++" << endl; } #else solve(); #endif return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int arr[n + 1]; for (int i = 1; i <= n; ++i) cin >> arr[i]; pair<int, int> p[m + 1]; for (int i = 1; i <= m; ++i) { cin >> p[i].first >> p[i].second; } int maxm = -1e9; vector<int> dmh; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { int ans = arr[i] - arr[j]; vector<int> v; for (int k = 1; k <= m; ++k) { if (p[k].first <= j && p[k].second >= j) { if (p[k].first > i || p[k].second < i) { ans++; v.push_back(k); } } } if (ans >= maxm) { maxm = ans; dmh = v; } } } cout << maxm << endl; cout << dmh.size() << endl; for (auto i : dmh) { cout << i << " "; } cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; double x[2], y[2], R[2], r[2]; double dis(double a, double b, double c, double d) { return sqrt((a - c) * (a - c) + (b - d) * (b - d)); } int doit(int flag) { double d = dis(x[flag], y[flag], x[!flag], y[!flag]); double d1, d2; int ans = 0; if (r[flag] >= d + R[!flag]) ans = 2; else if (R[flag] >= d + R[!flag]) ans = 1; if (ans == 2) return 2; if (d <= R[!flag] && d >= r[!flag]) return ans; d1 = abs(R[!flag] - d); d2 = abs(r[!flag] - d); double dd = min(d1, d2); if (ans == 1) { if (dd >= r[flag]) ans++; return ans; } else { if (dd >= r[flag]) ans++; if (dd >= R[flag]) ans++; return ans; } } int main() { for (int i = 0; i < 2; ++i) cin >> x[i] >> y[i] >> r[i] >> R[i]; int ans = 0; ans += doit(0); ans += doit(1); cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; for (long long i = 1; i <= t; i++) { long long n, k; cin >> n >> k; string s; cin >> s; long long w = 0, z = 0; bool flag = false; for (long long i = 0; i < k; i++) { char e = '?'; for (long long j = i; j < n; j += k) { if (s[j] != '?') { if (e != '?' && e != s[j]) { flag = true; } e = s[j]; } } s[i] = e; if (s[i] == '?') { w++; } else { if (s[i] == '0') { z--; } else { z++; } } } if (!flag && w >= abs(z)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } }
1
#include<iostream> #include<cstdio> #define N 12345 #define ll long long using namespace std; ll n,a[N],b[N],sum1,sum2; int main() { scanf("%lld",&n); for(ll i=1;i<=n;i++) scanf("%lld",a+i); for(ll i=1;i<=n;i++) scanf("%lld",b+i); for(ll i=1;i<=n;i++) { if(a[i] > b[i]) sum1 += (a[i] - b[i]); if(a[i] < b[i]) sum2 += (b[i] - a[i])/2; } if(sum1>sum2) puts("No"); else puts("Yes"); return 0; }
0
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const int MAXN = 2e3 + 5; const int MOD = 1e9 + 7; int n, m; long long dp[MAXN][MAXN][2]; long long sumdp[MAXN][MAXN][2]; int sum[MAXN][MAXN][2]; char s[MAXN]; int Binsearch(int i, int j, int v, int op) { if (op == 0) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; if (sum[mid][j][0] >= v) { r = mid - 1; } else { l = mid + 1; } } return l; } else { int l = 1, r = m; while (l <= r) { int mid = (l + r) / 2; if (sum[i][mid][1] >= v) { r = mid - 1; } else { l = mid + 1; } } return l; } } int main() { while (scanf("%d%d", &n, &m) != EOF) { memset(sum, 0, sizeof sum); memset(sumdp, 0, sizeof sumdp); for (int i = 1; i <= n; i++) { scanf("%s", s); for (int j = 0; j < m; j++) { sum[i][j + 1][0] = sum[i - 1][j + 1][0]; sum[i][j + 1][1] = sum[i][j][1]; if (s[j] == 'R') { sum[i][j + 1][0]++; sum[i][j + 1][1]++; } } } memset(dp, 0, sizeof dp); for (int i = 1; i <= n - sum[n][1][0]; i++) sumdp[i][1][1] = dp[i][1][0] = 1; for (int i = 1; i <= m - sum[1][m][1]; i++) sumdp[1][i][0] = dp[1][i][1] = 1; for (int i = 2; i <= n; i++) for (int j = 2; j <= m; j++) { int k = Binsearch(i, j, sum[n][j][0] - (n - i), 0); dp[i][j][0] = (sumdp[i - 1][j][0] - sumdp[k - 1][j][0] + MOD) % MOD; k = Binsearch(i, j, sum[i][m][1] - (m - j), 1); dp[i][j][1] = (sumdp[i][j - 1][1] - sumdp[i][k - 1][1] + MOD) % MOD; sumdp[i][j][0] = (sumdp[i - 1][j][0] + dp[i][j][1]) % MOD; sumdp[i][j][1] = (sumdp[i][j - 1][1] + dp[i][j][0]) % MOD; } if (n == 1 && m == 1 && sum[1][1][0] == 0) printf("%d\n", 1); else printf("%lld\n", (dp[n][m][1] + dp[n][m][0]) % MOD); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int ms = 100 + 10; long long dp[ms][ms], sum[ms]; pair<long long, long long> a[ms], b[ms]; int n, re[ms]; bool cmp(const pair<long long, long long>& l, const pair<long long, long long>& r) { return l.first < r.first || (l.first == r.first && l.second > r.second); } bool check(long long x) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; ++i) { sum[i] = a[i].first - x * a[i].second; } sum[n] = 0; for (int i = n - 1; i >= 0; --i) sum[i] += sum[i + 1]; for (int i = n - 1; i >= 0; --i) { int r = re[i], k = (r - i + 1); for (int j = 0; j < n; ++j) dp[i][j] = dp[i + 1][j]; for (int j = 0; 2 * j + k < n - r; ++j) { dp[i][j + k] = max(dp[i][j + k], dp[r + 1][j] + sum[i] - sum[r + 1]); } } return sum[0] - *max_element(dp[0], dp[0] + n) <= 0; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i].first, a[i].first *= 1000; for (int i = 0; i < n; ++i) cin >> a[i].second; sort(a, a + n, cmp); for (int i = n - 1; i >= 0; --i) { if (a[i].first == a[i + 1].first) re[i] = re[i + 1]; else re[i] = i; } long long l = 0, r = a[n - 1].first, mid, res = 0; while (l <= r) { mid = (l + r) / 2; if (check(mid)) { res = mid; r = mid - 1; } else { l = mid + 1; } } cout << res; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, dist[200002], p[200002], m, k; vector<int> G[200002]; vector<int> G1[200002]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; G1[y].push_back(x); G[x].push_back(y); } cin >> k; for (int i = 1; i <= k; i++) cin >> p[i]; queue<int> q; q.push(p[k]); while (!q.empty()) { int x = q.front(); q.pop(); for (auto it : G1[x]) { if (it != p[k] && dist[it] == 0) { dist[it] = dist[x] + 1; q.push(it); } } } int ans = 0, ans2 = 0; for (int i = 1; i < k; i++) { int nod = p[i]; int mn = n + 1; for (auto it : G[nod]) mn = min(mn, dist[it]); for (auto it : G[nod]) if (dist[it] == mn && it != p[i + 1]) { ans++; break; } bool ok = 0; for (auto it : G[nod]) if (dist[it] == mn && p[i + 1] == it) ok = 1; if (!ok) ans2++; } cout << ans2 << " " << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; string query(long long m) { cout << m << " 1" << endl; fflush(stdout); string s; cin >> s; return s; } int main() { int n; cin >> n; long long l = 0, r = 1e9; string s = query(l); for (int i = 0; i < n - 1; i++) { long long m = (l + r) / 2; string t = query(m); if (t == s) l = m; else r = m; } long long m = (l + r) / 2; cout << l << " 0 " << r << " 2" << endl; fflush(stdout); return 0; }
3
#include <bits/stdc++.h> template <class T1, class T2> inline bool cmin(T1 &a, const T2 &b) { return b < a ? (a = b, true) : false; } template <class T1, class T2> inline bool cmax(T1 &a, const T2 &b) { return a < b ? (a = b, true) : false; } template <class Type> Type read() { Type a; bool b; unsigned char c; while (c = getchar() - 48, (c > 9) & (c != 253)) ; for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c) ; return b ? -a : a; } int (*rd)() = read<int>; const int P = 4; inline int &inc(int &a, int b) { return (a += b) < P ? a : (a -= P); } inline int &dec(int &a, int b) { return (a -= b) < 0 ? (a += P) : a; } inline int add(int a, int b) { return (a += b) < P ? a : a - P; } inline int sub(int a, int b) { return (a -= b) < 0 ? a + P : a; } unsigned long long power(unsigned long long a, int b) { unsigned long long ans = 1; for (; b; a = a * a % P, b /= 2) if (b & 1) ans = ans * a % P; return ans; } const int N = 6005; int n, x[N], y[N], cnt[64]; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } int main() { n = rd(); for (int i = 1; i <= n; ++i) x[i] = rd(), y[i] = rd(); long long ans = 0; for (int i = 1; i <= n; ++i) { memset(cnt, 0, sizeof(cnt)); for (int j = 1; j <= n; ++j) { if (i == j) continue; int dd = gcd(abs(x[j] - x[i]), abs(y[j] - y[i])) % 4; int xx = (x[j] - x[i]) & 3; int yy = (y[j] - y[i]) & 3; ++cnt[dd << 4 | xx << 2 | yy]; } for (int j = 0; j < 64; ++j) { if (!cnt[j]) continue; int jd = j >> 4, jx = j >> 2 & 3, jy = j & 3; for (int k = j; k < 64; ++k) { if (!cnt[k]) continue; int kd = k >> 4, kx = k >> 2 & 3, ky = k & 3; if (jd % 2 != kd % 2) continue; int dd = gcd(abs(jx - kx), abs(jy - ky)) % 4; if (dd % 2 != 0) continue; if (abs(jx * ky - jy * kx) % 4 == (jd + kd + dd) % 4) ans += (j == k ? cnt[j] * (cnt[j] - 1) / 2 : cnt[j] * cnt[k]) * (jd % 2 == 0 ? 1 : 3); } } } printf("%lld\n", ans / 3); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int a[s.size() + 5]; a[0] = 1; int tmp = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'N') tmp++; } if (tmp == 1) cout << "NO\n"; else cout << "YES\n"; } }
1
#include <bits/stdc++.h> using namespace std; int n; struct bit { int v[200000]; bit() { memset(v, 0, sizeof v); } int query(int p) { int resp = 0; for (int i = p; i > 0; i -= (i & (-i))) resp = max(resp, v[i]); return resp; } void set(int p, int val) { for (int i = p; i < 200000; i += (i & (-i))) v[i] = max(val, v[i]); } }; int main() { scanf("%d", &n); bit bt; int resp = 0; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); bt.set(x, bt.query(x - 1) + 1); } printf("%d\n", bt.query(150000)); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long ans = 1LL * n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120; long long a = 1LL * ans * (n - 5) / 6; long long b = 1LL * a * (n - 6) / 7; ans += a + b; cout << ans << endl; }
6
#include <bits/stdc++.h> using namespace std; bool myfn(int x, int y) { return x > y; } int main() { int n; scanf("%d", &n); int arr[n]; int dp[n]; stack<int> stk; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } sort(arr, arr + n); stk.push(-1); int j = 0; for (int i = 0; i < n; i++) { if (arr[i] != arr[n - 1] && arr[i] == arr[i + 1]) { if (stk.top() != arr[i]) stk.push(arr[i]); } else if (arr[i] != arr[n - 1] && arr[i] != arr[i + 1]) { dp[j] = arr[i]; j++; } } dp[j] = arr[n - 1]; j++; while (stk.top() != -1) { dp[j] = stk.top(); stk.pop(); j++; } cout << j << endl; for (int i = 0; i < j - 1; i++) cout << dp[i] << " "; cout << dp[j - 1] << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int base = 100003; const long long MM = 1ll * 1000000007 * 1000000007; const int maxc = 2e9; int n, p, k, a[200001], dp[200001]; void Solve() { int test; cin >> test; while (test--) { cin >> n >> p >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; sort(a + 1, a + n + 1); memset(dp, 0, sizeof(dp)); if (a[1] > p) { cout << 0 << "\n"; continue; } int ans = 0; for (int i = 1; i <= n; ++i) { if (i < k) dp[i] = dp[i - 1] + a[i]; else dp[i] = dp[i - k] + a[i]; if (dp[i] <= p) ans = max(ans, i); } cout << ans << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); Solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; long long a[2][N]; long long lt[2][N], rt[2][N], both[2][N]; void fun() {} int main() { fun(); ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; int k; cin >> n >> k; int mdarray[k]; memset(mdarray, 0, sizeof(mdarray)); int a; for (int i = 0; i < n; i++) { cin >> a; mdarray[a % k]++; } if (mdarray[0] > 0) { cout << "YES\n"; return 0; } int dp[k + 1]; memset(dp, -1, sizeof(dp)); dp[0] = -1; for (int i = 1; i < k; i++) { int mul = mdarray[i]; while (mul--) { if (dp[i] == -1) { dp[i] = i; } for (int j = 1; j <= k; j++) { int code = i; if (dp[j] > -1 && dp[j] != code) { if (dp[(j + i + k) % k] == -1) { dp[(j + i + k) % k] = code; } } } for (int j = 0; j <= k; j++) { if (dp[j] > -1) { dp[j] = 0; } } if (dp[0] > -1) { cout << "YES\n"; return 0; } } } cout << "NO\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; typedef struct node2 { int d[60 + 1]; } node1; node1 tree[400001]; int a[100001]; void initialise(int node, int b, int e) { if (b > e) { return; } else if (b == e) { for (int(i) = (0); (i) < (60); ++(i)) { tree[node].d[i] = 0; } return; } else { initialise(2 * node, b, (b + e) / 2); initialise(2 * node + 1, (b + e) / 2 + 1, e); for (int(i) = (0); (i) < (60); ++(i)) { tree[node].d[i] = 0; } return; } } void update(int node, int b, int e, int index, int val1) { if (b > e) { return; } else if (b == e && e == index) { for (int(i) = (0); (i) < (60); ++(i)) { tree[node].d[i] = 1; if (i % val1 == 0) { tree[node].d[i] += 1; } } return; } else { if (index <= (b + e) / 2) { update(2 * node, b, (b + e) / 2, index, val1); } else { update(2 * node + 1, (b + e) / 2 + 1, e, index, val1); } for (int(i) = (0); (i) < (60); ++(i)) { tree[node].d[i] = (tree[2 * node].d[i] + tree[2 * node + 1].d[(tree[2 * node].d[i] + i) % 60]); } return; } } void query(node1 &res, int node, int b, int e, int i, int j) { if (b > e || b > j || e < i) { return; } else if (i <= b && e <= j) { res = tree[node]; return; } else { node1 l, r; for (int(i) = (0); (i) < (60); ++(i)) { l.d[i] = 0; r.d[i] = 0; } query(l, 2 * node, b, (b + e) / 2, i, j); query(r, 2 * node + 1, (b + e) / 2 + 1, e, i, j); for (int(i) = (0); (i) < (60); ++(i)) { res.d[i] = (l.d[i] + r.d[(i + l.d[i]) % 60]); } return; } } int main() { int n; scanf("%d", &n); initialise(1, 0, n - 1); for (int(i) = (0); (i) < (n); ++(i)) { int temp; scanf("%d", &temp); update(1, 0, n - 1, i, temp); } int q; scanf("%d", &q); while (q--) { char s[2]; scanf("%s", s); if (s[0] == 'A') { int first, second; scanf("%d%d", &first, &second); first--; second--; second--; node1 res; for (int(i) = (0); (i) < (30); ++(i)) { res.d[i] = 0; } query(res, 1, 0, n - 1, first, second); printf("%d\n", res.d[0]); } else { int first, second; scanf("%d%d", &first, &second); first--; update(1, 0, n - 1, first, second); } } return 0; }
4
#include<bits/stdc++.h> using namespace std; signed main(){ int a,b,c,d; cin>>a>>b>>c>>d; cout << (a!=c) << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { vector<int> a, p; int n, k = 0, n1, ans = 0; cin >> n; n1 = n; while (n1 != 0) { if (n1 % 10 == 4) a.push_back(0); else a.push_back(1); n1 /= 10; k++; } p.push_back(1); for (int i = 0; i < k; i++) p.push_back(p.back() * 2); ans += p.back() - 1; for (int i = 0; i < k; i++) ans += a[i] * p[i]; cout << ans; return 0; }
2
#include<iostream> #include<vector> using namespace std; int main() { long long int t,n,m,sum=0,c=0,x,mn,mx=0,i; cin>>t; while(t--) { cin>>n>>x; vector<int> V(n); for(int i=0;i<n;i++) { cin>>V[i]; sum+=V[i]; } mn=sum%x ? sum/x+1 : sum/x; for(i=0;i<n;i++) mx+=V[i]%x ? V[i]/x+1 : V[i]/x; cout<<mn<<" "<<mx<<endl; mx=0; sum=0; } }
1
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct Pair { int a; int b; }; Pair p[N]; long long pref[N]; long long ans; int n, m, i, L, R, mid, mx; bool cmp(Pair x, Pair y) { return x.a > y.a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { cin >> n >> m; for (i = 0; i < m; i++) cin >> p[i].a >> p[i].b; sort(p, p + m, cmp); for (i = 0; i < m; i++) pref[i + 1] = pref[i] + p[i].a; ans = 0LL; for (i = 0; i < m; i++) { L = -1; R = m; while (R > L + 1) { mid = (L + R) / 2; if (p[mid].a >= p[i].b) L = mid; else R = mid; } L = L + 1; L = min(L, n - 1); if (L <= i) { long long temp = pref[L] + p[i].a + (n - L - 1) * 1LL * p[i].b; ans = max(ans, temp); } } mx = 0; for (i = 0; i < min(n, m); i++) { mx = max(mx, p[i].b); ans = max(ans, pref[i + 1] + (n - i - 1) * 1LL * mx); } cout << ans << endl; } }
3
#include <bits/stdc++.h> using namespace std; const int N = 100500; bool one(int mask, int i) { return (mask & (1ll << i)); } int n, a[N], k = 0, ans[N], first[N], res = 0, b[N]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int pw = 30; pw >= 0; --pw) { bool ok = 1; res = 0; for (int i = 1; i <= n; i++) { if (one(a[i], pw)) { b[++res] = a[i]; } } if (res > 0) { int cur = b[1]; for (int j = 2; j <= res; ++j) { cur &= b[j]; } for (int j = 0; j < pw; ++j) { if (one(cur, j)) { ok = 0; break; } } if (ok) { k = res; for (int i = 1; i <= k; i++) ans[i] = b[i]; break; } } } cout << k << endl; for (int i = 1; i <= k; i++) cout << ans[i] << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; inline long long read() { bool f = false; char ch = getchar(); long long res = 0; while (ch < '0' || '9' < ch) f |= (ch == '-'), ch = getchar(); while ('0' <= ch && ch <= '9') res = 10 * res + ch - '0', ch = getchar(); return f ? -res : res; } const int N = 2e5 + 10; int head[N], ver[N * 2], edge[N * 2], Next[N * 2], tot; void add(int x, int y, int z) { ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot; } long long a[N]; long long dis[N]; bool vis[N]; int fa[N]; int ans[N]; void getdis(int u, int fa) { for (int i = head[u]; i; i = Next[i]) { int v = ver[i], w = edge[i]; if (v == fa) continue; dis[v] = dis[u] + w; getdis(v, u); } } struct node { long long dis, x; node(long long _dis = 0, long long _x = 0) { dis = _dis, x = _x; } bool operator<(const node& b) const { return dis < b.dis; } }; vector<node> path; void solve(int u) { path.push_back(node(dis[u], u)); vis[u] = 1; for (int i = head[u]; i; i = Next[i]) { int v = ver[i]; if (vis[v]) continue; long long temp = dis[v] - a[v]; auto pos = lower_bound(path.begin(), path.end(), temp); if (pos != path.end()) { ans[u]++; ans[fa[pos->x]]--; } solve(v); ans[u] += ans[v]; } path.pop_back(); } int main() { int n = read(); for (int i = 1; i <= n; i++) { a[i] = read(); } for (int i = 2; i <= n; i++) { int x = read(), y = read(); fa[i] = x; add(i, x, y); add(x, i, y); } getdis(1, -1); solve(1); for (int i = 1; i <= n; i++) { printf("%d ", ans[i]); } puts(""); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; long long int a=0,b=0; for(int i=0;i<s.size();i++){ if(s[i]=='0')a++; else b++; } cout << min(a,b)*2 <<endl; }
0
#include<iostream> using namespace std; int main() { int b[3]; cin >> b[0] >> b[1] >> b[2]; if (b[0] + b[1] * 2 + b[2] * 3 >= 3)cout << "Open" << endl; else cout << "Close" << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct point { int x, y; point() {} point(int x_, int y_) { x = x_; y = y_; } bool operator<(const point& b) const { return x != b.x ? x < b.x : y < b.y; } point operator-(const point& b) const { return point(x - b.x, y - b.y); } long long operator*(const point& b) const { return (long long)x * b.y - (long long)y * b.x; } } a[100005]; int s[100005]; int n; double X, Y; int main() { cin >> n >> X >> Y; for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y; sort(a + 1, a + n + 1); int top = 0; for (int i = 1; i <= n; i++) { while (top && a[i].y >= a[s[top]].y) { top--; } while (top >= 2 && ((a[i] - a[s[top - 1]]) * (a[s[top]] - a[s[top - 1]])) <= 0) { top--; } top++; s[top] = i; } double res = max(X / a[s[1]].x, Y / a[s[1]].y); for (int i = 1; i < top; ++i) { point p1 = a[s[i]]; point p2 = a[s[i + 1]]; double l = 0, r = 1, templ, tempr; for (int k = 1; k <= 150; k++) { double lp = (l + l + r) / 3; double rp = (l + r + r) / 3; templ = max(X / (lp * p1.x + (1 - lp) * p2.x), Y / (lp * p1.y + (1 - lp) * p2.y)); tempr = max(X / (rp * p1.x + (1 - rp) * p2.x), Y / (rp * p1.y + (1 - rp) * p2.y)); templ < tempr ? r = rp : l = lp; if (templ < tempr) r = rp; else l = lp; } if (res > templ) res = templ; } printf("%.15f\n", res); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long ans = 0; string str; int n; int main() { ios::sync_with_stdio(false); cin >> n; cin >> str; for (int i = 0; i < n; ++i) ans += (str[i] - '0') & 1 ? 0 : i + 1; cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long mx[5], mi[5]; long long X[5][2], f[7], MX = 1e18; long long get(long long x, long long y) { return x / 2 + y / 2 + (x % 2 + y % 2) / 2; } int pd(long long x) { f[3] = max(X[1][0] - x, X[2][0] + x); f[4] = min(X[1][1] - x, X[2][1] + x); f[5] = max(X[3][0] - x, X[4][0] + x); f[6] = min(X[3][1] - x, X[4][1] + x); if (f[3] > f[4] || f[5] > f[6]) return 0; if (f[4] > f[3] || f[6] > f[5]) return 1; if ((f[3] % 2 + 2) % 2 == (f[5] % 2 + 2) % 2) return 1; return 0; } int check(long long x) { int i; for (i = 1; i < 5; i++) { X[i][0] = mx[i] - x; X[i][1] = mi[i] + x; if (X[i][0] > X[i][1]) return 0; } f[1] = get(X[1][0], -X[2][1]); f[2] = get(X[1][1], -X[2][0]); f[3] = get(X[3][0], -X[4][1]); f[4] = get(X[3][1], -X[4][0]); f[1] = max(f[1], max(f[3], -MX)); f[2] = min(f[2], min(f[4], MX)); if (f[1] > f[2]) return 0; for (long long j = f[1]; j <= f[2]; j++) if (pd(j)) return 1; return 0; } int main() { int i, n, t; long long x, y, z, mxx = 3 * 1e18; scanf("%d", &t); while (t--) { scanf("%d", &n); for (i = 1; i < 5; i++) { mi[i] = mxx; mx[i] = -mxx; } for (i = 1; i <= n; i++) { scanf("%I64d%I64d%I64d", &x, &y, &z); mi[1] = min(mi[1], x + y + z); mx[1] = max(mx[1], x + y + z); mi[2] = min(mi[2], -x + y + z); mx[2] = max(mx[2], -x + y + z); mi[3] = min(mi[3], x + y - z); mx[3] = max(mx[3], x + y - z); mi[4] = min(mi[4], -x + y - z); mx[4] = max(mx[4], -x + y - z); } long long l = -1, r = mxx; while (r - l > 1) { long long mid = (l + r) >> 1; if (check(mid)) { r = mid; } else { l = mid; } } check(r); for (long long j = f[1]; j <= f[2]; j++) if (pd(j)) { x = j; if (f[3] == f[4] || f[5] == f[6]) { if (f[3] == f[4]) { y = f[3]; if ((f[5] % 2 + 2) % 2 == (f[3] % 2 + 2) % 2) z = f[5]; else z = f[5] + 1; } else { z = f[5]; if ((f[5] % 2 + 2) % 2 == (f[3] % 2 + 2) % 2) y = f[3]; else y = f[3] + 1; } } else { y = f[3]; if ((f[5] % 2 + 2) % 2 == (f[3] % 2 + 2) % 2) z = f[5]; else z = f[5] + 1; } break; } printf("%I64d %I64d %I64d\n", x, get(y, z), get(y, -z)); } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int arr[200050]; void swap(long long int *a, long long int *b) { long long int temp = *a; *a = *b; *b = temp; } long long int rev(long long int i, long long int j) { long long int k; long long int temp2 = j; for (k = i; k <= (i + j) / 2; k++) { swap(&arr[k], &arr[temp2]); temp2--; } } int main() { long long int n, i, j, k; scanf("%lld", &n); for (i = 1; i <= n; i++) scanf("%lld", &arr[i]); if (n % 2 == 0) { i = n / 2; j = i + 1; i--; j++; while (i >= 1 && j <= n) { swap(&arr[i], &arr[j]); i -= 2; j += 2; } if ((n / 2) % 2 == 1) rev(1, n); } else { i = j = n / 2 + 1; i--; j++; while (i >= 1 && j <= n) { swap(&arr[i], &arr[j]); i -= 2; j += 2; } if ((n / 2) % 2 == 0) rev(1, n); } for (i = 1; i <= n; i++) printf("%lld ", arr[i]); }
2
#include <stdio.h> #include <string.h> #define NUM 1000 char hitohude_error(char s[]) { int i,j,k; char error=0; for(j=0;s[j+1] != '\n';j++) { if(s[j] == s[j+1]) { error = 1; break; } } for(j=0; s[j+1] != '\n';j++) { if(s[j] == 'A') { if(s[j+1] != 'B' && s[j+1] != 'D') { error = 1; break; } } else if(s[j] == 'B') { if(s[j+1] != 'A' && s[j+1] != 'C' && s[j+1] != 'E') { error = 1; break; } } else if(s[j] == 'C') { if(s[j+1] != 'B' && s[j+1] != 'F') { error = 1; break; } } else if(s[j] == 'D') { if(s[j+1] != 'A' && s[j+1] != 'E' && s[j+1] != 'G') { error = 1; break; } } else if(s[j] == 'E') { if(s[j+1] != 'B' && s[j+1] != 'D' && s[j+1] != 'F' && s[j+1] != 'H') { error = 1; break; } } else if(s[j] == 'F') { if(s[j+1] != 'C' &&s[j+1] != 'E' &&s[j+1] != 'I'){ error = 1; break; } } else if(s[j] == 'G') { if(s[j+1] != 'D' &&s[j+1] != 'H'){ error = 1; break; } } else if(s[j] == 'H') { if(s[j+1] != 'E' &&s[j+1] != 'G' &&s[j+1] != 'I'){ error = 1; break; } } else if(s[j] == 'I') { if(s[j+1] != 'F' &&s[j+1] != 'H'){ error = 1; break; } } } return error; } int main() { int i,j,k; char s[12]; static char print[NUM][12]; int a=0; for(i=0;i<NUM;i++) { fgets(s,15,stdin); if( hitohude_error(s) ) ; else strcpy(print[a++],s); } for(i=0;i<a;i++) printf("%s",print[i]); return 0; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:65777216") using namespace std; int n, m; int x[1111], y[1111], z[1111]; pair<double, double> p[1111]; const double eps = 1e-6; void parse(int A, int B, int C, double &x, double &y, double &z) { double t = (A * x + B * y + C * z) / (A * A + B * B + C * C); x -= A * t; y -= B * t; z -= C * t; } pair<double, double> project(int A, int B, int C, double x, double y, double z) { parse(A, B, C, x, y, z); if (B == 0 && C == 0) return pair<double, double>(y, z); double xx = 1, yy = 0, zz = 0; if (B) yy = (-A) / double(B); else if (C) zz = (-A) / double(C); double d1 = sqrt(x * x + y * y + z * z + 0.); if (d1 < eps) return pair<double, double>(0., 0.); double d2 = sqrt(B * B + C * C + 0.); double d3 = sqrt(x * x + (y - C) * (y - C) + (z + B) * (z + B)); double a = (d1 * d1 + d2 * d2 - d3 * d3) / (2 * d1 * d2); a = acos(a); if (x < 0) a = -a; return pair<double, double>(d1 * cos(a), d1 * sin(a)); } bool good(int A, int B, int C, double r) { double X = p[0].first; double Y = p[0].second; for (int i(1); i < (n); i++) { if ((X - p[i].first) * (X - p[i].first) + (Y - p[i].second) * (Y - p[i].second) <= r * r + eps) continue; bool found = 0; for (int j(0); j < (i); j++) { double d = (p[j].first - p[i].first) * (p[j].first - p[i].first) + (p[j].second - p[i].second) * (p[j].second - p[i].second); if (d > 4 * r * r) return 0; double dx = p[j].first - p[i].first, dy = p[j].second - p[i].second; swap(dx, dy); dx = -dx; double z = sqrt((r * r - d / 4) / (dx * dx + dy * dy)); dx *= z; dy *= z; for (int up(0); up < (2); up++) { X = (p[i].first + p[j].first) / 2 + (up ? -1 : 1) * dx, Y = (p[i].second + p[j].second) / 2 + (up ? -1 : 1) * dy; bool g = 1; for (int k(0); k < (i + 1); k++) if ((X - p[k].first) * (X - p[k].first) + (Y - p[k].second) * (Y - p[k].second) > r * r + eps) { g = 0; break; } if (g) { found = 1; break; } } if (found) break; } if (!found) return 0; } return 1; } double solve(int A, int B, int C) { for (int i(0); i < (n); i++) p[i] = project(A, B, C, x[i], y[i], z[i]); random_shuffle(p, p + n); double a = 0; double b = 1e5; for (int _it(0); _it < (40); _it++) { double s = (a + b) / 2; if (good(A, B, C, s)) b = s; else a = s; } return b; } int main() { cin >> n >> m; for (int i(0); i < (n); i++) scanf("%d %d %d", x + i, y + i, z + i); for (int i(0); i < (m); i++) { int A, B, C; scanf("%d %d %d", &A, &B, &C); printf("%.10lf\n", solve(A, B, C)); } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long digitsum(long long r, long long m = 10) { long long ret = 0; for (long long x = 1; x <= r; x *= m) { long long dig = (r / x) % m; long long le = (r / x) / m; long long ri = r % x; long long cur = 0; for (int i = 1; i < dig; ++i) cur += i; ret += cur * (le + 1) * x; ret += dig * (le * x + ri + 1); cur = 0; for (int i = dig + 1; i < m; ++i) cur += i; ret += cur * le * x; } return ret; } long long d(long long x) { long long res = 0; while (x > 0) { res += x % 10; x /= 10; } return res; } long long bb(long long a) { long long lo = 1; long long hi = 100000000000000000ll; long long mi; long long fmi; while (hi - lo > 2) { mi = (hi + lo) / 2; fmi = digitsum(mi); if (a < fmi) { hi = mi; } else { lo = mi; } } return lo; } int main() { long long n; cin >> n; long long lo = 0; long long hi = bb(n); long long fhi = digitsum(hi), flo = 0; while (fhi - flo != n) { if (fhi - flo < n) { hi++; fhi += d(hi); } else { lo++; flo += d(lo); } } cout << lo + 1 << " " << hi << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 55; int n, m; char a[N][N]; vector<int> row[N]; vector<int> col[N]; bool possible; bool checked[N]; bool same(int idx1, int idx2) { if (row[idx1].size() != row[idx2].size()) return false; for (int i = 0; i < row[idx1].size(); i++) if (row[idx1][i] != row[idx2][i]) return false; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); fopen("input.txt", "r"); cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; if (a[i][j] == '#') { row[i].push_back(j); col[j].push_back(i); } } } possible = true; for (int i = 1; i <= n && possible; i++) { if (!checked[i]) { for (int j = 0; j < row[i].size() && possible; j++) { for (int k = 0; k < col[row[i][j]].size() && possible; k++) { int u = col[row[i][j]][k]; if (!same(i, u)) { possible = false; } else checked[u] = true; } } } checked[i] = true; } if (possible) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.); vector<pair<long long, char> > s, t, temp, temp1; pair<long long, char> p; int n, m; const int MAX_N = 200010; long long b[MAX_N]; void kmpPreprocess() { int i = 0, j = -1; b[0] = -1; while (i < temp.size()) { while (j >= 0 && temp[i] != temp[j]) j = b[j]; i++; j++; b[i] = j; } } int kmpSearch() { int i = 0, j = 0; int res = 0; while (i < t.size()) { while (j >= 0 && t[i] != temp[j]) j = b[j]; i++; j++; if (j == temp.size()) { int x = i - j; int y = x + temp.size() - 1; if (x != 0 && y + 1 < t.size() && t[x - 1].second == s[0].second && t[x - 1].first >= s[0].first && t[y + 1].first >= s.rbegin()->first && t[y + 1].second == s.rbegin()->second) res++; j = b[j]; } } return res; } void fix() { temp1 = vector<pair<long long, char> >(); int i = 0; while (i < temp.size()) { temp1.push_back(temp[i++]); while (i < temp.size() && temp1.rbegin()->second == temp[i].second) temp1.rbegin()->first += temp[i++].first; } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> p.first; char xx; cin >> xx; cin >> p.second; temp.push_back(p); } fix(); t = temp1; temp = vector<pair<long long, char> >(); for (int i = 0; i < m; i++) { cin >> p.first; char xx; cin >> xx; cin >> p.second; temp.push_back(p); } fix(); s = temp1; long long res = 0; if (s.size() == 1) { for (int i = 0; i < t.size(); i++) { if (t[i].first >= s[0].first && s[0].second == t[i].second) res += t[i].first - s[0].first + 1; } cout << res << endl; } else if (s.size() == 2) { for (int i = 1; i < t.size(); i++) { if (t[i].second == s[1].second && t[i - 1].second == s[0].second && t[i].first >= s[1].first && t[i - 1].first >= s[0].first) res++; } cout << res << endl; } else { temp.clear(); for (int i = 1; i < s.size() - 1; i++) temp.push_back(s[i]); kmpPreprocess(); res = kmpSearch(); cout << res << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<char> vec[500005]; int res[500005]; int main() { int n; cin >> n; getchar(); for (int i = 0; i < n; i++) { char x; while (scanf("%c", &x) && (x != '\n')) { vec[i].push_back(x); } } for (int i = n - 1; i > 0; i--) { int tmp1 = vec[i].size(); int tmp2 = vec[i - 1].size(); for (int j = 1; j < min(tmp1, tmp2); j++) { if (vec[i][j] < vec[i - 1][j]) { vec[i - 1][j] = '\n'; break; } else if (vec[i][j] > vec[i - 1][j]) { break; } if (j == tmp1 - 1 && tmp2 > tmp1) { vec[i - 1][j + 1] = '\n'; } } } for (int i = 0; i < n; i++) { int tmp = vec[i].size(); for (int j = 0; j < tmp; j++) { if (vec[i][j] == '\n') break; printf("%c", vec[i][j]); } printf("\n"); } cin >> n; return 0; }
4
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long maxn = 4e5; const long long mod = 1e9 + 7; const long double PI = acos((long double)-1); long long pw(long long a, long long b, long long md = mod) { long long res = 1; while (b) { if (b & 1) { res = (a * res) % md; } a = (a * a) % md; b >>= 1; } return (res); } int n; int q; int a[maxn]; int pos[maxn]; int fen[maxn]; void add(int x, int pos) { for (pos += 5; pos < maxn; pos += pos & -pos) fen[pos] += x; } int get(int pos) { int ans = 0; for (pos += 5; pos; pos -= pos & -pos) ans += fen[pos]; return (ans); } int get(int l, int r) { return (get(r) - get(l - 1)); } void rem(int x) { if (x > 1) if (pos[x] < pos[x - 1] and get(x - 1, x - 1)) add(-1, x - 1); if (x < n) if (pos[x] > pos[x + 1] and get(x, x)) add(-1, x); } void Add(int x) { if (x > 1) if (pos[x] < pos[x - 1] and !get(x - 1, x - 1)) add(1, x - 1); if (x < n) if (pos[x] > pos[x + 1] and !get(x, x)) add(1, x); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]] = i; for (int i = 1; i < n; i++) if (pos[i] > pos[i + 1]) add(1, i); cin >> q; while (q--) { int t, l, r; cin >> t >> l >> r; if (t == 1) cout << get(l, r - 1) + 1 << '\n'; else { rem(a[l]); rem(a[r]); swap(pos[a[l]], pos[a[r]]); swap(a[l], a[r]); Add(a[l]); Add(a[r]); } } return (0); }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; long long a[N], u; int n; long double solve() { long double ans = -1; for (int i = 1; i <= n - 2; i++) { long double ei = (long double)a[i], ej = (long double)a[i + 1]; int idx = -1; int l = i + 2, r = n; while (l <= r) { int mid = (l + r) / 2; if ((a[mid] - a[i]) <= u) l = mid + 1, idx = mid; else r = mid - 1; } if (idx != -1) { long double ek = (long double)a[idx]; long double val = (ek - ej) / (ek - ei); ans = max(ans, val); } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> u; for (int i = 1; i <= n; i++) cin >> a[i]; long double ans = solve(); cout << fixed << setprecision(15) << ans << endl; return 0; }
3
#include <iostream> #include <string> #include <sstream> #include <iomanip> #include <math.h> #include <stdio.h> #include <string.h> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <functional> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <bitset> #include <complex> using namespace std; //#pragma GCC optimize("Ofast") //#pragma GCC optimization("unroll-loops, no-stack-protector") //#pragma GCC target("avx,avx2,fma") int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, i, j, k, x, y, xx, yy, ct = 0; cin >> n >> m; vector<string> s(n); for (i = 0; i < n; i++) cin >> s[i]; x = 0; y = 0; if (s[x][y] == '*') ct = 1; else ct = 0; while (1) { bool found = false; for (i = 1; i <= 10; i++) { for (j = 0; j <= i; j++) { xx = x + j; yy = y + i - j; if ((xx >= 0) && (xx < n) && (yy >= 0) && (yy < m) && (s[xx][yy] == '*')) { found = true; ct++; x = xx; y = yy; break; } } if (found) break; } if (!found) break; } cout << ct << "\n"; return 0; }
5
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <utility> #include <map> using namespace std; void show(vector<int> &v, string s = "") { if (s.size() > 0) { cout << s << endl; } for (auto e : v) { cout << e << " "; } cout << endl; } int main() { int n, m, q; scanf("%d %d %d", &n, &m, &q); vector<int> a(q); for (int i = 0; i < q; i++) { scanf("%d", &a[i]); a[i]--; } reverse(a.begin(), a.end()); vector<int> order; map<int, int> num_pos; vector<int> num_used(m, false); vector<int> fixed_index(n, 0); for (int i = 0; i < q; i++) { // printf("a[%d] = %d\n", i, a[i]); // show(fixed_index, "fixed_index"); if (!num_used[a[i]]) { num_used[a[i]] = true; num_pos[a[i]] = order.size(); order.emplace_back(a[i]); fixed_index.back()++; continue; } int pos = num_pos[a[i]]; // printf("pos %d\n", pos); auto it_bound = upper_bound(fixed_index.begin(), fixed_index.end(), pos); if (it_bound != fixed_index.begin()) { it_bound = prev(it_bound); } // printf("it_bound %d\n", *it_bound); if (*it_bound == pos) { *it_bound += 1; } } // show(order, "order"); // show(fixed_index, "fixed_index"); for (int i = 0; i < m; i++) { if (!num_used[i]) { order.emplace_back(i); } } // show(order, "order"); bool ok = true; for (int i = fixed_index[0]; i < order.size() - 1; i++) { if (order[i] > order[i+1]) { ok = false; break; } } cout << (ok ? "Yes" : "No") << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; set<long long> dp[55][55]; long long pre[55]; long long arr[55]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { scanf("%I64d", &arr[i]); pre[i] = pre[i - 1] + arr[i]; } for (int i = 1; i <= k; i++) { if (i == 1) { for (int j = 1; j <= n; j++) { dp[j][i].insert(pre[j]); } } else { for (int j = i; j <= n; j++) { for (int l = i - 1; l < j; l++) { for (set<long long>::iterator m = dp[l][i - 1].begin(); m != dp[l][i - 1].end(); ++m) { long long x = *m; x &= (pre[j] - pre[l]); dp[j][i].insert(x); } } } } } set<long long>::iterator ans = dp[n][k].end(); ans--; printf("%I64d\n", *ans); }
4
#include<bits/stdc++.h> #define int long long //#include<ext/pb_ds/assoc_container.hpp> using namespace std; //using namespace __gnu_pbds; const int inf=0x3f3f3f3f; const double eps=1e-6; const int mod=1e6+3; typedef long long ll; int modpow(int x,int y,int md=mod){if(y==0)return 1;int ret=modpow(x,y>>1,md);ret=(ll)ret*ret%md;if(y&1)ret=(ll)ret*x%md;return ret;} int q; inline int inv(int x){ return modpow(x,mod-2,mod); } int Mul[1000004]; signed main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>q;//1*2*...*(1e6+2)=1e6+2 Mul[1]=1; for(int i=2;i<=1000002;i++){ Mul[i]=Mul[i-1]*i%mod; } while(q--){ int x,d,n; cin>>x>>d>>n; if(d==0){ cout<<modpow(x,n)<<endl; continue; } int ans=modpow(d,n); x=x*inv(d)%mod; if(!x){ cout<<"0\n"; continue; } if(x+n-1>=1e6+3){ cout<<"0\n"; continue; } cout<<ans*Mul[x+n-1]%mod*inv(Mul[x-1])%mod<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct cmp { bool operator()(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } }; long long modpow(long long base, long long exp, long long mod) { base %= 1000000007; long long result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % 1000000007; base = (base * base) % 1000000007; exp >>= 1; } return result; } long long num[100005], dist[100005]; vector<pair<int, int> > graph[100005]; bool visited[100005]; void dfs2(int i, int &cnt) { for (int j = 0; j < graph[i].size(); j++) { int ind = graph[i][j].first; long long len = graph[i][j].second; if (!visited[ind]) { visited[ind] = 1; dist[ind] = max(len, dist[i] + len); cnt++; dfs2(ind, cnt); } } } void dfs(int i, int &cnt) { for (int j = 0; j < graph[i].size(); j++) { int ind = graph[i][j].first; long long len = graph[i][j].second; if (!visited[ind]) { visited[ind] = 1; dist[ind] = max(len, dist[i] + len); if (dist[ind] > num[ind]) { cnt++; dfs2(ind, cnt); } else dfs(ind, cnt); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> num[i]; visited[i] = 0; } int v; long long w; for (int i = 2; i <= n; i++) { cin >> v >> w; graph[i].push_back(make_pair(v, w)); graph[v].push_back(make_pair(i, w)); } int ans = 0; visited[1] = 1; dfs(1, ans); cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> int main() { int s = 0, n, m, a, b; scanf("%d %d %d %d", &n, &m, &a, &b); if (a * m > b) { s = (n / m) * b + (n % m) * a; if (a > b) { if (n % m != 0) s = ((n / m) + 1) * b; } } else { s += a * n; } if (s > b && m > n) s = b; printf("%d\n", s); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<int> copy1; vector<int> data_1[(int)3e5]; int data_2[(int)3e5]; int n, m, total; int func(int day) { vector<int> remaining = copy1; int t = total; int money = day, cur_money = day; for (int i = day; i >= 1; i--) for (int j = 0; j < data_1[i].size(); j++) { cur_money = min(i, cur_money); while (remaining[data_1[i][j]] and cur_money) remaining[data_1[i][j]]--, t--, money--, cur_money--; } money -= (t * 2); if (money >= 0) return 1; else return 0; } int main() { cin >> n >> m; copy1.resize(n + 1); for (int i = (1); i <= (n); i++) cin >> copy1[i], total += copy1[i]; for (int i = (1); i <= (m); i++) { int day, type; cin >> day >> type; data_1[day].push_back(type); } for (int i = (1); i <= (2000); i++) if (func(i)) return cout << i, 0; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 20005; int n, m; vector<pair<int, int> > ver[MAXN]; int fa[MAXN][15], dep[MAXN], id[MAXN][15], lg[MAXN]; void dfs(int u) { for (pair<int, int> v : ver[u]) if (v.first != fa[u][0]) { fa[v.first][0] = u; dep[v.first] = dep[u] + 1; id[v.first][0] = m + v.second; dfs(v.first); } } int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 14; ~i; i--) if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; if (x == y) return x; for (int i = 14; ~i; i--) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; return fa[x][0]; } const int MAXV = 300005, MAXE = 5000000; const int INF = 0x3f3f3f3f; namespace network_flow { int h[MAXV], tot = 1, ntot, S, T; int val[MAXE], nxt[MAXE], ver[MAXE]; void add(int x, int y, int z) { nxt[++tot] = h[x]; ver[tot] = y; val[tot] = z; h[x] = tot; } void add_edge(int x, int y, int z) { add(x, y, z), add(y, x, 0); } int d[MAXV], cur[MAXV]; bool bfs() { queue<int> q; q.push(S); memset(d + 1, 0, sizeof(int) * ntot); memcpy(cur + 1, h + 1, sizeof(int) * ntot); d[S] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = h[u]; i; i = nxt[i]) { int v = ver[i]; if (!d[v] && val[i]) { d[v] = d[u] + 1; q.push(v); if (v == T) return 1; } } } return 0; } int dfs(int u, int flow) { if (u == T) return flow; int lft = flow; for (int i = cur[u]; i && lft; i = nxt[i]) { cur[u] = i; int v = ver[i]; if (val[i] && d[v] == d[u] + 1) { int tmp = dfs(v, min(val[i], lft)); if (!val[i]) { d[v] = 0; continue; } lft -= tmp; val[i] -= tmp, val[i ^ 1] += tmp; } } return flow - lft; } int dinic() { int ret = 0; while (bfs()) ret += dfs(S, INF); return ret; } bool vis[MAXV]; void make_tag(int u) { if (vis[u]) return; vis[u] = true; for (int i = h[u]; i; i = nxt[i]) if (val[i]) make_tag(ver[i]); } } // namespace network_flow using network_flow::add_edge; using network_flow::dinic; using network_flow::make_tag; using network_flow::ntot; using network_flow::S; using network_flow::T; using network_flow::vis; int jump(int x, int lim) { for (int i = 14; ~i; i--) if (dep[fa[x][i]] >= lim) x = fa[x][i]; return x; } void link(int p, int x, int y) { int tt = lg[dep[x] - dep[y]]; add_edge(p, id[x][tt], INF); if (dep[y] + (1 << tt) != dep[x]) add_edge(p, id[jump(x, dep[y] + (1 << tt))][tt], INF); } vector<int> ans1, ans2; int main() { scanf("%d%d", &n, &m); for (int i = 1, x, y; i < n; i++) { scanf("%d%d", &x, &y); ver[x].push_back(make_pair(y, i)); ver[y].push_back(make_pair(x, i)); } dep[1] = 1, dfs(1); ntot = m + n - 1; for (int j = 1; j <= 14; j++) for (int i = 1; i <= n; i++) { fa[i][j] = fa[fa[i][j - 1]][j - 1]; if (fa[i][j]) { id[i][j] = ++ntot; add_edge(id[i][j], id[i][j - 1], INF); add_edge(id[i][j], id[fa[i][j - 1]][j - 1], INF); } } for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1; S = ++ntot, T = ++ntot; for (int i = 1; i <= m; i++) add_edge(S, i, 1); for (int i = 1; i < n; i++) add_edge(m + i, T, 1); for (int i = 1, x, y; i <= m; i++) { scanf("%d%d", &x, &y); int par = lca(x, y); if (par != x) link(i, x, par); if (par != y) link(i, y, par); } printf("%d\n", dinic()); make_tag(S); for (int i = 1; i <= m; i++) if (!vis[i]) ans1.push_back(i); for (int i = 1; i < n; i++) if (vis[i + m]) ans2.push_back(i); printf("%d", (int)ans1.size()); for (int x : ans1) printf(" %d", x); puts(""); printf("%d", (int)ans2.size()); for (int x : ans2) printf(" %d", x); puts(""); return 0; }
5
#include <bits/stdc++.h> using namespace std; int tot; long long n, m, len; map<long long, long long> cnt; int main() { scanf("%I64d%I64d", &n, &m); if (m <= 2) { printf("%I64d\n", m == 1 ? 1LL : n); return 0; } m -= 2; cnt[n - 2] = 1; while (1) { long long nxt = (cnt.rbegin()->first - 1) >> 1, ctr = 0; while (cnt.size() > 0 && (cnt.rbegin()->first - 1) >> 1 == nxt) { pair<long long, long long> tmp = *cnt.rbegin(); cnt.erase(tmp.first); ctr += tmp.second; if (tmp.first >> 1) { if ((tmp.first - 1) >> 1) cnt[(tmp.first - 1) >> 1] += tmp.second; cnt[tmp.first >> 1] += tmp.second; } } if (ctr < m) { m -= ctr; } else { len = nxt; break; } } long long L = 2, R = n - 1; while (len < (R - L) >> 1) { long long M = (L + R) >> 1, ctr = 0; map<long long, long long>().swap(cnt); cnt[(R - L) >> 1] = 1; while (1) { long long nxt = (cnt.rbegin()->first - 1) >> 1; while (cnt.size() > 0 && (cnt.rbegin()->first - 1) >> 1 == nxt) { pair<long long, long long> tmp = *cnt.rbegin(); cnt.erase(tmp.first); if (nxt == len) ctr += tmp.second; if (tmp.first >> 1) { if ((tmp.first - 1) >> 1) cnt[(tmp.first - 1) >> 1] += tmp.second; cnt[tmp.first >> 1] += tmp.second; } } if (nxt <= len) break; } if (ctr < m) { m -= ctr; L = M + 1; } else { R = M - 1; } } printf("%I64d\n", (L + R + (m - 1)) >> 1); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; char in[MAXN]; long long wynik, wag[26], s[MAXN]; int n; map<long long, long long> pocz[26]; int main() { for (int i = 0; i < 26; i++) scanf("%I64d", &wag[i]); scanf("%s", in + 1); n = strlen(in + 1); for (int i = 1; i <= n; i++) s[i] = s[i - 1] + wag[in[i] - 'a']; for (int i = 1; i <= n; i++) { if (i > 1) wynik += pocz[in[i] - 'a'][s[i - 1]]; pocz[in[i] - 'a'][s[i]]++; } printf("%I64d\n", wynik); return 0; }
4
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long int vis[200005] = {0}; vector<int> v[4005]; vector<set<long long int>> myset(200005); vector<long long int> put; long long int par[200005] = {0}; long long int sz[200005] = {0}; int a[4005][4005]; int main() { int n, m, y, x, i, ans = 99999; cin >> n >> m; while (m--) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); a[x][y] = 1; a[y][x] = 1; } for (i = 1; i <= n; i++) { for (auto pp : v[i]) { for (auto kk : v[i]) { if (a[pp][kk] == 1) { y = v[pp].size() + v[kk].size() + v[i].size(); ans = min(ans, y); } } } } if (ans == 99999) ans = -1; else ans -= 6; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int a, b[6], c, d, e[6], f, g, h; for (a = 0; a < 6; a++) cin >> b[a]; for (a = 0; a < 6; a++) { d = 0; for (c = 0; c < 6; c++) { if (b[a] == b[c]) d = d + 1; } e[a] = d; } f = g = d = h = 0; for (a = 0; a < 6; a++) { if (e[a] == 4) f = 1; else if (e[a] == 2) g = 1; else if (e[a] == 6) d = 1; else if (e[a] == 5) h = 1; } if (f == 1 && g == 1 || d == 1) cout << "Elephant"; else if (f == 1 || h == 1) cout << "Bear"; else cout << "Alien"; return 0; }
1
#include <bits/stdc++.h> #define FI(i,a,b) for(int i=(a);i<=(b);i++) #define FD(i,a,b) for(int i=(a);i>=(b);i--) #define LL long long #define Ldouble long double #define PI 3.1415926535897932384626 #define PII pair<int,int> #define PLL pair<LL,LL> #define mp make_pair #define fi first #define se second using namespace std; int n, l, r; char s[10005][25]; struct name{ char t[25]; int id; bool operator<(const name & k) const{ if(!strcmp(t, k.t)) return id < k.id; else return strcmp(t, k.t) < 0; } }; name u[10005]; int main(){ scanf("%d", &n); FI(i, 1, n + 1) scanf(" %s", s[i]); //first FI(i, 1, n){ int len = strlen(s[i]); FI(j, 0, len - 1){ if(s[i][j] == '?') u[i].t[j] = 'z'; else u[i].t[j] = s[i][j]; } u[i].t[len] = 0; u[i].id = i; } strcpy(u[n + 1].t, s[n + 1]); u[n + 1].id = 0; sort(u + 1, u + n + 2); FI(i, 1, n + 1) if(u[i].id == 0){ l = i; break; } //last FI(i, 1, n){ int len = strlen(s[i]); FI(j, 0, len - 1){ if(s[i][j] == '?') u[i].t[j] = 'a'; else u[i].t[j] = s[i][j]; } u[i].t[len] = 0; u[i].id = i; } strcpy(u[n + 1].t, s[n + 1]); u[n + 1].id = n + 1; sort(u + 1, u + n + 2); FI(i, 1, n + 1) if(u[i].id == n + 1){ r = i; break; } FI(i, l, r) printf("%d%c", i, i == r ? '\n':' '); return 0; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; template <typename T> T sqr(T x) { return x * x; } const int maxn = (int)(1e5) + 10; int n, m, p, q; vector<pair<int, int> > g[maxn]; vector<int> comp[maxn]; long long len[maxn]; bool used[maxn]; int curC, Size[maxn]; void dfs(int v, int pr = -1) { if (used[v]) return; used[v] = true; comp[curC].push_back(v); for (int i = 0; i < g[v].size(); i++) { int to = g[v][i].first; len[curC] += g[v][i].second; dfs(to, v); } } int main() { scanf("%d%d%d%d", &n, &m, &p, &q); while (m--) { int v, u, w; scanf("%d%d%d", &v, &u, &w); --v; --u; g[v].push_back(make_pair(u, w)); g[u].push_back(make_pair(v, w)); } curC = 0; for (int i = 0; i < n; i++) { if (!used[i]) { dfs(i); len[curC] /= 2; curC++; } } bool hmt1 = false; for (int i = 0; i < curC; i++) { Size[i] = (int)((comp[i]).size()); if ((int)((comp[i]).size()) > 1) hmt1 = true; } if (p == 0) { if (curC == q) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; } if (q > curC) { cout << "NO\n"; return 0; } set<pair<long long, int> > st; for (int i = 0; i < curC; i++) { st.insert(make_pair(len[i], i)); } vector<pair<int, int> > ans; int v = -1, u = -1; for (int it = 0; it < curC - q && p >= 0; it++) { pair<long long, int> a = *st.begin(); st.erase(st.begin()); pair<long long, int> b = *st.begin(); st.erase(st.begin()); ans.push_back(make_pair(comp[a.second].back(), comp[b.second].back())); long long addLen = min((long long)(1e9), a.first + b.first + 1); a.first += b.first + addLen; Size[a.second] += Size[b.second]; if (Size[a.second] > 1) { u = comp[b.second].back(); v = comp[a.second].back(); } st.insert(a); p--; } if (p < 0) { cout << "NO\n"; return 0; } if (p > 0) { if (v == -1 && !hmt1) { cout << "NO\n"; return 0; } if (hmt1 && v == -1) { for (int i = 0; i < curC; i++) { if ((int)((comp[i]).size()) > 1) { v = comp[i].front(); u = comp[i].back(); } } } cout << "YES\n"; for (int i = 0; i < (int)((ans).size()); i++) { cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl; } for (int i = 0; i < p; i++) { cout << v + 1 << " " << u + 1 << endl; } } else { cout << "YES\n"; for (int i = 0; i < (int)((ans).size()); i++) { cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 3010; long long dis[N * N], x[N], y[N]; bool g[N][N]; bitset<N> bit[N]; int main() { int n, i, j, m = 0; cin >> n; for (i = 0; i < n; i++) cin >> x[i] >> y[i]; for (i = 0; i < n; i++) for (j = i + 1; j < n; j++) { dis[m] = ((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])) << 32; dis[m++] |= (i << 16) | j; } sort(dis, dis + m); while (m--) { i = dis[m] & ((1 << 16) - 1); j = (dis[m] >> 16) & ((1 << 16) - 1); if ((bit[i] & bit[j]) != 0) break; bit[i][j] = bit[j][i] = 1; } cout << fixed << setprecision(7) << sqrt((dis[m] >> 32) + 0.0) * 0.5 << endl; }
5
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long row = 0, col = 0; vector<pair<long long, long long>> ans; for (long long i = 0; i <= n; i++) { ans.push_back({row, col}); ans.push_back({row + 1, col}); ans.push_back({row, col + 1}); row++; col++; } ans.push_back({row, col}); cout << ans.size() << '\n'; for (auto &i : ans) cout << i.first << " " << i.second << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; int visited[100001] = {0}; int main() { int n, m, ans = 0; cin >> n >> m; if (m >= n) { queue<pair<int, int>> q; q.push({n, 0}); pair<int, int> t; visited[n] = 1; while (q.size()) { t = q.front(); q.pop(); if (t.first == m) { cout << t.second; return 0; } if ((t.first - 1 <= 2 * m && (t.first - 1) != 0) && !visited[t.first - 1]) { visited[t.first - 1] = 1; q.push({t.first - 1, t.second + 1}); } if (t.first * 2 <= 2 * m && !visited[t.first * 2]) { visited[t.first * 2] = 1; q.push({t.first * 2, t.second + 1}); } } } else ans += (n - m); cout << ans; }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; mt19937 rnd(time(0)); const int MAXN = 3 * 100 * 1000 + 17; int n, a[MAXN], q; vector<int> w[MAXN]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) w[a[i]].push_back(i); while (q--) { int l, r, k; cin >> l >> r >> k; r++; int ans = MAXN; for (int i = 1; i <= 80; i++) { int tmp = rnd() % (r - l); if (a[l + tmp] >= ans) continue; int cnt = lower_bound(w[a[l + tmp]].begin(), w[a[l + tmp]].end(), l) - w[a[l + tmp]].begin(); if (w[a[l + tmp]].size() > cnt + (r - l) / k && w[a[l + tmp]][cnt + (r - l) / k] < r) ans = min(ans, a[l + tmp]); } cout << (ans == MAXN ? -1 : ans) << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 5005, mod = 998244353; int qp(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ans; } int n, m, k, f[N][N], ans; int main() { scanf("%d%d%d", &n, &m, &k); f[0][0] = 1; for (int i = 1; i <= k; i++) for (int j = 1; j <= i; j++) f[i][j] = (1ll * f[i - 1][j - 1] * (n + 1 - j) % mod + 1ll * f[i - 1][j] * j % mod) % mod; for (int i = 1; i <= k; i++) ans = (ans + 1ll * f[k][i] * qp(qp(m, mod - 2), i) % mod) % mod; printf("%d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; int a, l[100009], r[100009], z[100009 * 10], b, c; long long sm; vector<int> v; int main() { cin >> a; for (int i = 1; i <= a; i++) cin >> l[i] >> r[i], z[l[i]]++, z[r[i]]--; for (int i = 1; i <= 1000000; i++) z[i] += z[i - 1]; for (int i = 1; i <= 1000000; i++) { if (z[i] > 2) { cout << 0 << endl; return 0; } } for (int i = 1; i <= 1000000; i++) { if (z[i] == 2) { b = i; break; } } if (b == 0) { cout << a << endl; for (int i = 1; i <= a; i++) cout << i << ' '; return 0; } for (int i = 1000000; i >= 1; i--) if (z[i] == 2) { c = i; break; } c++; for (int i = 1; i <= a; i++) if (l[i] <= b and r[i] >= c) sm++, v.push_back(i); cout << sm << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << ' '; return 0; }
3
#include<bits/stdc++.h> using namespace std; const int N = 12345; int n, a[N], b[N]; vector<int> adj[N]; bool visit[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1, x, y; i < n; ++i) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a + 1, a + 1 + n, greater<int>()); queue<int> q; q.push(1); visit[1] = true; b[1] = a[1]; int answer = 0, t = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (auto y : adj[x]) { if (!visit[y]) { visit[y] = true; q.push(y); answer += (b[y] = a[++t]); } } } cout << answer << '\n'; for (int i = 1; i <= n; ++i) { cout << b[i] << " \n"[i == n]; } return 0; }
0
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) int n, p[505][505], q, x, y, ans, dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0}, used[505][505]; void dfs(int x, int y){ p[x][y]--; rep(i,4){ int nx = x + dx[i], ny = y + dy[i]; if(p[nx][ny] + used[nx][ny] == p[x][y] + 2) dfs(nx,ny); } } int main(){ scanf("%d", &n); ans = -n * n; rep(i,n) rep(j,n) p[i+1][j+1] = min({i,n-1-i,j,n-1-j}) + 1; rep(i,n) rep(j,n){ scanf("%d", &q); q--; x = q % n + 1; y = q / n + 1; used[x][y] = 1; ans += p[x][y]; dfs(x, y); } printf("%d\n", ans); }
0
#include <bits/stdc++.h> using namespace std; int n, m, u, v, vk = 0, vv = 0; long long res = 0, se = 0; int visited[1000000], selfE[1000000]; vector<vector<int> > G(1000000, vector<int>()); void dfs(int i) { if (visited[i]) return; visited[i] = 1; vk++; for (int u : G[i]) { dfs(u); } } void solve() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &u, &v); if (u == v) { selfE[v - 1] += 1; se++; } else { G[u - 1].push_back(v - 1); G[v - 1].push_back(u - 1); } } for (int i = 0; i < n; i++) { long long l = G[i].size(); res += l * (l - 1) / 2; } res += se * (se - 1) / 2; res += se * (m - se); memset(visited, 0, sizeof visited); for (int i = 0; i < n; i++) { if (G[i].size()) { dfs(i); break; } } for (int i = 0; i < n; i++) if (G[i].size() || selfE[i]) vv++; if (vk == vv) cout << res << endl; else cout << 0 << endl; } int main() { solve(); return 0; }
4
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define N 1000000007 int main() { long long n, m, k,cost=0,t=1; cin >> n >> m >> k; for (long long i = 0; i < n; i++) cost = (cost + (1 + i) * i / 2 * m % N * m % N) % N; for (long long i = 0; i < m; i++) cost = (cost + (1 + i) * i / 2 * n % N * n % N) % N; for (long long i = 2; i <= n * m - 2; i++) cost = cost * i % N; for (long long i = 2; i <= k - 2; i++) t = t * i % N; for (long long i = 2; i <= (n * m - k); i++) t = t * i % N; for (long long x = N - 2; x > 0; t = t * t % N, x >>= 1) if (x & 1) cost = cost * t % N; cout << cost << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; int n,k,q; int a[2009],b[2009]; int main(){ cin >> n>>k>>q; for(int i = 1; i <= n; i++){ cin >> a[i]; } int ans = 1e9; for(int i = 1; i <= n; i++){ int l = 0, x = a[i]; vector<int> VV; for(int j = 1; j <= n+1; j++){ if(a[j] < x || j == n+1){ vector<int> V; for(int i = l +1; i < j; i ++) { V.push_back(a[i]); } l = j; if(!V.size()) continue; sort(V.begin(),V.end()); for(int i = 0; i < (int)V.size() - k + 1; i++) VV.push_back(V[i]); } } sort(VV.begin(), VV.end()); if(VV.size() < q) continue; ans = min(ans , VV[q-1]-VV[0]); } cout<<ans<<endl; }
0
#include <bits/stdc++.h> using namespace std; void usaco(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } template <class T> void umin(T &a, T b) { if (b < a) a = b; } template <class T> void umax(T &a, T b) { if (a < b) a = b; } int rec(vector<int> &v, int bit) { if (v.size() < 3 || bit < 0) return v.size(); vector<int> vec[2]; for (int x : v) vec[x >> bit & 1].push_back(x); int x = rec(vec[0], bit - 1); int y = rec(vec[1], bit - 1); if (x < y) swap(x, y); return x + !!y; } int main() { int n; scanf("%d", &n); vector<int> v; for (int i = 0; i ^ n; i++) { int x; scanf("%d", &x); v.push_back(x); } printf("%d", n - rec(v, 30)); }
5
#include <bits/stdc++.h> using namespace std; long long a, b, w, x, c; int main() { scanf("%I64d%I64d%I64d%I64d%I64d", &a, &b, &w, &x, &c); long long dif = c - a; if (dif <= 0) printf("0\n"); else { double k = double(dif * x - b) / double(w - x); long long result = dif + ceil(k); printf("%I64d\n", result); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, xx, yy, xx1, yy1, xx2, yy2; cin >> a >> b; for (long long i = 1; i < a; i++) { long long y = a * a - i * i; y = sqrt(y); if (i * i + y * y == a * a) { xx = i; yy = y; xx1 = -y * b; yy1 = i * b; if (xx1 % a == 0 && yy1 % a == 0) { xx1 /= a; yy1 /= a; if (yy != yy1) { puts("YES"); cout << 0 << " " << 0 << endl; cout << xx << " " << yy << endl; cout << xx1 << " " << yy1 << endl; return 0; } } } } puts("NO"); }
3
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; cout << ((a+b)*c)/2; }
0
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::istream& operator>>(std::istream& i, pair<T, U>& p) { i >> p.first >> p.second; return i; } template <typename T> std::istream& operator>>(std::istream& i, vector<T>& t) { for (auto& v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { o << p.first << ' ' << p.second; return o; } template <typename T> std::ostream& operator<<(std::ostream& o, const vector<T>& t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; } unsigned int logceil(long long first) { return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U>& p) const { return t(p.first) ^ (u(p.second) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; class FPassports { public: void solve(istream& cin, ostream& cout) { int N, P; cin >> N >> P; vector<pair<std::pair<int, int>, std::pair<int, int>>> T(N); for (int i = 0; i < N; ++i) { cin >> T[i].first.first >> T[i].first.second >> T[i].second.first; T[i].second.second = i; } sort(T.begin(), T.end()); vector<std::pair<int, int>> ByLength(N); for (int i = 0; i < N; ++i) { ByLength[i] = {T[i].second.first, i}; } sort(ByLength.begin(), ByLength.end()); int inf = 2e9; vector<pair<int, std::pair<int, int>>> W(1 << N, {inf, {-1, 0}}); vector2<int> Bad(N, N, 0); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { for (int k = 0; k < N; ++k) { int apply = T[j].first.first + T[j].first.second; int passBack = apply + T[i].second.first; if (apply <= T[k].first.first && T[k].first.first <= passBack) { Bad[i][j] |= 1 << k; } } } } W[0].first = 1; for (int b = 0; b < (1 << N); ++b) { if (W[b].first == inf) continue; int first = 0; while (first < N && (T[first].first.first + T[first].first.second < W[b].first)) ++first; int bar = first; while (bar < N && T[bar].first.first + T[bar].first.second - 1 < W[b].first) ++bar; bool canApplyRightAway = !(bar < N && T[bar].first.first <= W[b].first); int earliestAfter = first; while (earliestAfter < N && (T[earliestAfter].first.first < W[b].first || ((b & (1 << earliestAfter)) == 0))) ++earliestAfter; int k = first; for (int gX = 0; gX < N; ++gX) { int g = ByLength[gX].second; if (T[g].first.first + T[g].first.second < W[b].first || (b & (1 << g))) continue; int passBackNow = W[b].first + T[g].second.first; if (T[g].first.first <= passBackNow) continue; if (canApplyRightAway && !(earliestAfter != N && W[b].first <= T[earliestAfter].first.first && T[earliestAfter].first.first <= passBackNow)) { W[b | (1 << g)] = min(W[b | (1 << g)], {passBackNow, {g, W[b].first}}); } else { int foo = first; while (k < N) { int apply = T[k].first.first + T[k].first.second; int passBack = apply + T[g].second.first; if (T[g].first.first <= passBack) break; if ((k == N - 1 || T[k + 1].first.first != apply) && !(b & Bad[g][k])) { W[b | (1 << g)] = min(W[b | (1 << g)], {passBack, {g, apply}}); break; } else k++; } } } } vector<std::pair<int, int>> Ans(N); if (P == 1) { if (W[(1 << N) - 1].first == inf) { cout << "NO\n"; return; } else { int m = (1 << N) - 1; while (m != 0) { Ans[T[W[m].second.first].second.second] = {1, W[m].second.second}; m ^= 1 << W[m].second.first; } } } if (P == 2) { bool found = false; for (int m = 0; m < (1 << N); ++m) { int c = ((1 << N) - 1) ^ m; if (W[m].first != inf && W[c].first != inf) { while (m != 0) { Ans[T[W[m].second.first].second.second] = {1, W[m].second.second}; m ^= 1 << W[m].second.first; } while (c != 0) { Ans[T[W[c].second.first].second.second] = {2, W[c].second.second}; c ^= 1 << W[c].second.first; } found = true; break; } } if (!found) { cout << "NO\n"; return; } } cout << "YES\n"; for (std::pair<int, int> ans : Ans) cout << ans << endl; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); FPassports solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MX = 2e5; set<int> adj[MX + 1]; vector<int> leaf; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; adj[u].insert(v); adj[v].insert(u); } for (int i = 1; i <= n; ++i) { if (adj[i].size() == 1) { leaf.push_back(i); } } while (leaf.size() >= 2) { int a = leaf.back(); leaf.pop_back(); int b = leaf.back(); leaf.pop_back(); cout << "? " << a << ' ' << b << endl; int w; cin >> w; if (w == a || w == b) { cout << "! " << w << endl; return 0; } for (auto i : {a, b}) { int par = *adj[i].begin(); adj[par].erase(i); if (adj[par].size() == 1) { leaf.push_back(par); } } } cout << "! " << leaf[0] << endl; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, b[N]; long long m, a[N], tot; pair<long long, int> s[N]; int cnt; long long f(long long x, long long y) { return y - 1 - 3 * x * (x - 1); } int calc(long long x, long long t) { int l = 1, r = x + 1, mid; while (l < r) { mid = l + r >> 1; if (f(mid, x) < t) r = mid; else l = mid + 1; } return l - 1; } bool check(long long x) { tot = 0; for (int i = 1; i <= n && tot <= m; i++) tot += (b[i] = calc(a[i], x)); return tot <= m; } int main() { scanf("%d %lld", &n, &m); long long l = 9e18, r = 0, mid; for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), r = max(r, f(1, a[i])), l = min(l, f(a[i], a[i])); while (l < r) { mid = l + r >> 1; if (check(mid)) r = mid; else l = mid + 1; } check(l); m = m - tot; if (m) for (int i = 1; i <= n; i++) if (b[i] < a[i]) s[++cnt] = make_pair(f(b[i] + 1, a[i]), i); sort(s + 1, s + cnt + 1, greater<pair<long long, int> >()); for (int i = 1; i <= m; i++) b[s[i].second]++; for (int i = 1; i <= n; i++) printf("%d ", b[i]); return 0; }
6
#include<bits/stdc++.h> using namespace std; #define ALL(v) (v).begin(),(v).end() #define REP(i,p,n) for(int i=p;i<(int)(n);++i) #define rep(i,n) REP(i,0,n) #define SZ(x) ((int)(x).size()) #define debug(x) cerr << #x << ": " << x << '\n' #define INF 999999999 typedef long long int Int; typedef pair<int,int> P; using ll = long long; using VI = vector<int>; struct gen{ int time; int basyo; int syozi; }; int main(){ while(1){ int n;cin >> n; if(n==0) break; int dp[4][100]; rep(i,4){ rep(j,100)dp[i][j]=INF; } int bef=0,beft=0; vector<pair<int,int>> v(n); rep(i,n){ int a,b;cin >> a >> b; v[i] = {a,b};//場所,時間 } rep(i,n){ int a,b; a=v[i].first; b=v[i].second; if(i==0){ if(a>b){ cout << "NG 1" << endl; break; } dp[1][i] = a; bef=a; beft=b; continue; } int check = 0; rep(j,4){ if(j==0) continue; if(dp[j][i-1]==INF) continue; if(abs(a-bef)*(j+1)<=b-beft&&j<3){ dp[j+1][i]=dp[j][i-1]+abs(a-bef); } if(bef*(j+1)+a<=b-beft){ dp[1][i]=min(dp[j][i-1]+a+bef,dp[1][i]); } } bef=a; beft=b; rep(j,4){ if(dp[j][i]!=INF) check=1; } if(check==0){ cout << "NG " << i+1 << endl; break; } } int ans = INF; rep(i,3){ ans = min(ans,dp[i+1][n-1]+bef); } if(ans==INF) continue; cout << "OK " << ans << endl; } return 0; }
0
#include <iostream> using namespace std; int a[10]; bool dfs(int i,int l,int r){ if(i>=10)return 1; if(l>a[i]&&r>a[i])return 0; if(l<a[i]){ if(dfs(i+1,a[i],r))return 1; } if(r<a[i]){ return dfs(i+1,l,a[i]); } return 0; } int main(){ int T;cin>>T; while(T--){ for(int i=0;i<10;i++)cin>>a[i]; if(dfs(0,0,0))cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } int dp[int(1e5 + 10)] = {0}, uni[int(1e5 + 10)] = {0}, b[int(1e5 + 10)], nowdp[int(1e5 + 10)]; int BITree[100100] = {0}, n, g[int(1e5 + 10)], N; int getmax(int index) { int mx = 0; while (index > 0) { mx = max(mx, BITree[index]); index -= index & (-index); } return mx; } void updateBIT(int index, int val) { while (index <= N) { if (BITree[index] >= val) return; BITree[index] = val; index += index & (-index); } } int main() { int k, maxb, t; cin >> k >> n >> maxb >> t; N = maxb; for (int u = int(1); u <= (int)k; u++) { for (int i = int(1); i <= (int)n; i++) { scanf("%d", &b[i]); ; } memset(dp, 0, sizeof(dp)); ; memset(BITree, 0, sizeof(BITree)); ; if (t >= n) { int dis = 0; for (int i = int(1); i <= (int)n; i++) if (uni[b[i]] != u) uni[b[i]] = u, dis++; cout << dis << endl; continue; } for (int i = 1; i <= t; ++i) { int cd = 0; for (int j = 1; j <= n; ++j) { if (b[j] > 1) nowdp[b[j]] = 1 + getmax(b[j] - 1); else nowdp[b[j]] = 1; if (nowdp[b[j]] > dp[b[j]]) { dp[b[j]] = nowdp[b[j]]; cd = 1; updateBIT(b[j], dp[b[j]]); } } if (!cd) break; } int ans = 0; for (int i = 1; i <= maxb; ++i) { ans = max(ans, dp[i]); } cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; int ck(string& s, int i, int j, char c) { if (i == j) { if (s[i] == c) return 0; else return 1; } int mid = i + (j - i) / 2; int cnt = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0; for (int ii = i; ii <= mid; ii++) { if (s[ii] == c) cnt++; if (s[ii] == (c + 1)) cnt1++; if (s[ii - i + mid + 1] == c) cnt2++; if (s[ii - i + mid + 1] == (c + 1)) cnt3++; } int ans = 0, ans1 = 0; ans += (mid + 1 - i - cnt2); ans += ck(s, i, mid, c + 1); ans1 += (mid + 1 - i - cnt); ans1 += ck(s, mid + 1, j, c + 1); return min(ans, ans1); } int main() { ios::sync_with_stdio(0), cin.tie(0); ; int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; cout << (ck(s, 0, n - 1, 'a')) << '\n'; } }
4
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+5,M=505,mod=998244353; ll base1=1,base2=0,p[N]; int n,m,tot,vis[N],head[N],nex[N<<1],to[N<<1]; void add(int u,int v){to[++tot]=v;nex[tot]=head[u];head[u]=tot;} vector<ll>res[3]; void solve(int flag) { tot=0;memset(head,0,sizeof(head)); memset(vis,false,sizeof(vis)); m=100000; scanf("%d",&m); for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); //u=v=1;while(u==v) u=rand()*rand()%n+1,v=rand()*rand()%n+1; add(u,v);add(v,u); } priority_queue<int>q[2]; int x=0,tim=0; for(int i=1;i<=n;i++) q[x].push(i); vector<ll>f; while(!q[x].empty()) { tim++; ll ans=0; while(!q[x].empty()) { while(!q[x].empty()&&vis[q[x].top()]==tim) q[x].pop(); if(q[x].empty()) break; int u=q[x].top();q[x].pop(); vis[u]=1e9; ans=(ans+p[u])%mod; for(int i=head[u];i;i=nex[i]) { int v=to[i]; if(vis[v]<tim) { vis[v]=tim; q[x^1].push(v); } } } x^=1; f.push_back(ans); } res[flag]=f; } bool vis1[M][M],vis2[M]; ll solve() { memset(vis2,0,sizeof(vis2)); ll ans=0; for(int i=0;i<res[1].size();i++) { int mx=0; while(mx<res[2].size()&&vis1[i][mx]||vis2[mx]) mx++; if(mx<res[2].size()) { vis1[i][mx]=vis2[mx]=true; ans=(ans+res[1][i]*res[2][mx])%mod; } } return ans; } int main() { srand(time(0)); p[0]=1; for(int i=1;i<N;i++) p[i]=1000000000000000000ll%mod*p[i-1]%mod; scanf("%d",&n); solve(0);solve(1);solve(2); assert(res[0].size()<M); assert(res[1].size()<M); assert(res[2].size()<M); ll ans=0; for(int i=0;i<res[0].size();i++) ans=(ans+res[0][i]*solve())%mod; printf("%lld\n",ans); }
0
#include <bits/stdc++.h> using namespace std; int n, a[100005], dp[100005][20], g[100005][20], lg2[200005]; int pos[100005]; void init() { int i, j; lg2[1] = 0; for (i = 2; i <= n; i++) lg2[i] = lg2[i - 1] + (i & (i - 1) ? 0 : 1); for (i = 1; i <= n; i++) dp[i][0] = a[i], g[i][0] = a[i]; for (j = 1; j <= lg2[n]; j++) { for (i = 1; i + (1 << j) - 1 <= n; i++) { dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]); g[i][j] = min(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]); } } } int rmq(int l, int r) { int m = lg2[r - l + 1]; return max(dp[l][m], dp[r - (1 << m) + 1][m]); } int rmq2(int l, int r) { int m = lg2[r - l + 1]; return min(g[l][m], g[r - (1 << m) + 1][m]); } int main(void) { int i, j; while (~scanf("%d", &n)) { for (i = 1; i <= n; i++) scanf("%d", &a[i]); init(); int ans = 0; for (i = 1; i <= n; i++) { int low = 1, up = n - i + 1, mid; while (low < up) { mid = (low + up + 1) / 2; if (rmq(i, i + mid - 1) - rmq2(i, i + mid - 1) <= 1) low = mid; else up = mid - 1; } ans = max(ans, low); } printf("%d\n", ans); } return 0; }
2
#include <iostream> using namespace std; #define int long long int N, M; int P[100100]; int A[100100]; int B[100100]; int C[100100]; int visit[100100]; signed main() { cin >> N >> M; for (int i=0; i<M; i++) cin >> P[i]; for (int i=0; i<N-1; i++) cin >> A[i] >> B[i] >> C[i]; for (int i=1; i<M; i++) { int from = P[i-1]-1; int to = P[i]-1; for (int j=min(from, to); j<max(from, to); j++) visit[j]++; } int ans = 0; for (int i=0; i<N-1; i++) { int v = visit[i]; if (v != 0) ans += min(v*A[i], C[i] + v*B[i]); } cout << ans << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const double pi = acos(-1.0); int n, i, k; long long pl, pr, vl, vr, minp, maxp, minv, maxv; long long lucky[2000]; long long numerator, denumerator; void generate_lucky(long long me) { lucky[++n] = me; if (me * 10 + 4 <= 1000000000) generate_lucky(10 * me + 4); if (me + 10 + 7 <= 1000000000) generate_lucky(10 * me + 7); } long long intersection(long long l1, long long r1, long long l2, long long r2) { return (max(0LL, min(r1, r2) - max(l1, l2) + 1)); } int main(int argc, char const *argv[]) { scanf("%lld%lld%lld%lld%d", &pl, &pr, &vl, &vr, &k); generate_lucky(4), generate_lucky(7); sort(lucky + 1, lucky + n + 1); numerator = 0; denumerator = (pr - pl + 1) * (vr - vl + 1); for (i = 1; i <= n - k + 1; ++i) { if (lucky[i] < min(pl, vl) || lucky[i + k - 1] > max(pr, vr)) continue; minp = (i == 1 ? pl : lucky[i - 1] + 1); maxp = lucky[i]; minv = lucky[i + k - 1]; maxv = (i == n - k + 1 ? vr : lucky[i + k - 1 + 1] - 1); numerator += intersection(minp, maxp, pl, pr) * intersection(minv, maxv, vl, vr); minv = (i == 1 ? vl : lucky[i - 1] + 1); maxv = lucky[i]; minp = lucky[i + k - 1]; maxp = (i == n - k + 1 ? pr : lucky[i + k - 1 + 1] - 1); numerator += intersection(minp, maxp, pl, pr) * intersection(minv, maxv, vl, vr); if (k == 1 && lucky[i] >= pl && lucky[i] <= pr && lucky[i] >= vl && lucky[i] <= vr) --numerator; } printf("%.15lf\n", (double)numerator / (double)denumerator); return 0; }
4
#include <iostream> #include <map> #include <cstring> using namespace std; int main() { map<char, map<char, string> > data; data['1']['T'] = "a"; data['1']['L'] = "i"; data['1']['U'] = "u"; data['1']['R'] = "e"; data['1']['D'] = "o"; data['2']['T'] = "ka"; data['2']['L'] = "ki"; data['2']['U'] = "ku"; data['2']['R'] = "ke"; data['2']['D'] = "ko"; data['3']['T'] = "sa"; data['3']['L'] = "si"; data['3']['U'] = "su"; data['3']['R'] = "se"; data['3']['D'] = "so"; data['4']['T'] = "ta"; data['4']['L'] = "ti"; data['4']['U'] = "tu"; data['4']['R'] = "te"; data['4']['D'] = "to"; data['5']['T'] = "na"; data['5']['L'] = "ni"; data['5']['U'] = "nu"; data['5']['R'] = "ne"; data['5']['D'] = "no"; data['6']['T'] = "ha"; data['6']['L'] = "hi"; data['6']['U'] = "hu"; data['6']['R'] = "he"; data['6']['D'] = "ho"; data['7']['T'] = "ma"; data['7']['L'] = "mi"; data['7']['U'] = "mu"; data['7']['R'] = "me"; data['7']['D'] = "mo"; data['8']['T'] = "ya"; data['8']['U'] = "yu"; data['8']['D'] = "yo"; data['9']['T'] = "ra"; data['9']['L'] = "ri"; data['9']['U'] = "ru"; data['9']['R'] = "re"; data['9']['D'] = "ro"; data['0']['T'] = "wa"; data['0']['U'] = "nn"; data['0']['D'] = "wo"; string in; cin >> in; for (int i = 0; i < in.size() / 2; i++) { cout << data[in[i * 2]][in[i * 2 + 1]]; } cout << endl; return 0; }
0
#include <stdio.h> #include <string.h> #define ROW_BLOCK 4 #define COL_BLOCK 4 #define ROW_NUM (ROW_BLOCK + 2) * 2 - 1 #define COL_NUM (COL_BLOCK + 2) * 2 - 1 #define UP 0 #define RIGHT 1 #define DOWN 2 #define LEFT 3 const char command[5] = "URDL"; const int dy[4] = {-1,0,1,0}, dx[4] = {0,1,0,-1}; const int gy = 1, gx = 1; int main(void){ int i, j, stage[ROW_NUM][COL_NUM]; memset(stage,0,sizeof(stage)); for(i = 1;i < ROW_NUM - 1;i++){ char str[ROW_BLOCK + 2]; scanf("%s",str); for(j = 1 + i % 2;j < COL_NUM - 1;j+=2){ stage[i][j] = str[(j - 1) / 2] - '0'; } } int nd = RIGHT, nx = 1, ny = 1; do{ if(stage[ny + dy[nd]][nx + dx[nd]] == 0 && stage[ny + dy[(nd + 3) % 4]][nx + dx[(nd + 3) % 4]] == 0){ // 今向いている方向に壁がなく右手にも壁がなければれば nd = (nd + 1) % 4; }else if(stage[ny + dy[(nd + 3) % 4]][nx + dx[(nd + 3) % 4]] == 1){ nd = (nd + 3) % 4; ny += dy[nd] * 2; nx += dx[nd] * 2; printf("%c",command[nd]); }else{ ny += dy[nd] * 2; nx += dx[nd] * 2; printf("%c",command[nd]); } /*for(i = 0;i < ROW_NUM;i++){ for(j = 0;j < COL_NUM;j++){ if(ny == i && nx == j){ printf("# "); }else{ printf("%d ",stage[i][j]); } } printf("\n"); } while(getchar() != '\n');*/ }while(!((nx == gx) && (ny == gy))); printf("\n"); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, u, v, D, B, a[100009]; long long c[100009]; void ins(int x, int y) { for (; x <= n; x += x & -x) c[x] += y; } long long getsum(int x) { long long y = 0; for (; x; x ^= x & -x) y += c[x]; return y; } void update() { for (; u <= n && !a[u]; u++) ; for (; v && !a[v]; v--) ; } int main() { scanf("%d%d%d", &n, &D, &B); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); ins(i, a[i]); } u = 1; v = n; update(); int tx = 0, ty = 0; for (int i = 1, j = n; i <= j;) { int x = min((long long)n, i + (long long)i * D); if (getsum(x) >= B) { int tmp = 0; while (tmp < B) { int da = min(B - tmp, a[u]); a[u] -= da; ins(u, -da); tmp += da; update(); } } else tx++; i++; if (i > j) break; x = max(1ll, j - (long long)(n - j + 1) * D); if (getsum(n) - getsum(x - 1) >= B) { int tmp = 0; while (tmp < B) { int da = min(B - tmp, a[v]); a[v] -= da; ins(v, -da); tmp += da; update(); } } else ty++; j--; } printf("%d\n", max(tx, ty)); return 0; }
6
#include <bits/stdc++.h> using namespace std; template <class T, class U> bool cmax(T& a, const U& b) { return a < b ? a = b, 1 : 0; } template <class T, class U> bool cmin(T& a, const U& b) { return b < a ? a = b, 1 : 0; } void _BG(const char* s) { cerr << s << endl; } template <class T, class... TT> void _BG(const char* s, T a, TT... b) { for (size_t c = 0; *s && (c || *s != ','); cerr << *s++) c += count(begin("([{"), end("([{"), *s) - count(begin(")]}"), end(")]}"), *s); cerr << " = " << a; if (*s) { cerr << ", "; ++s; } _BG(s, b...); } bool RD() { return 1; } bool RD(char& a) { return scanf(" %c", &a) == 1; } bool RD(char* a) { return scanf("%s", a) == 1; } bool RD(double& a) { return scanf("%lf", &a) == 1; } bool RD(int& a) { return scanf("%d", &a) == 1; } bool RD(long long& a) { return scanf("%lld", &a) == 1; } template <class T, class... TT> bool RD(T& a, TT&... b) { return RD(a) && RD(b...); } void PT(const char& a) { putchar(a); } void PT(char const* const& a) { fputs(a, stdout); } void PT(const double& a) { printf("%.16f", a); } void PT(const int& a) { printf("%d", a); } void PT(const long long& a) { printf("%lld", a); } template <char s = ' ', char e = '\n'> void PL() { if (e) PT(e); } template <char s = ' ', char e = '\n', class T, class... TT> void PL(const T& a, const TT&... b) { PT(a); if (sizeof...(b) && s) PT(s); PL<s, e>(b...); } const int N = 1e5 + 87; int n, p, t[N], f[N]; long long ans[N]; void add(int i, int v) { for (; i <= n; i += i & -i) f[i] += v; } int qry(int i) { int r = 0; for (; i > 0; i -= i & -i) r += f[i]; return r; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> p; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq; set<int> rd; for (int i = 1; i <= n; ++i) { cin >> t[i]; add(i, 1); pq.push({t[i], i}); } long long lt = 0; while (pq.size()) { long long x, i; tie(x, i) = pq.top(); while (pq.size() && pq.top().first == x) { tie(x, i) = pq.top(); pq.pop(); if (i < 0) { add(-i, 1); } else { rd.insert(i); } } if (rd.size()) { i = *begin(rd); if (qry(i) == i) { add(i, -1); ans[i] = max(lt, x) + p; lt = ans[i]; pq.push({ans[i], -i}); rd.erase(begin(rd)); } } } for (int i = 1; i <= n; ++i) cout << ans[i] << " \n"[i == n]; }
3
#include <stdio.h> #include <cmath> #include <algorithm> #include <cfloat> #include <stack> #include <queue> #include <vector> #include <string> #include <iostream> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; int N,M; struct Node{ bool operator<(const struct Node &arg) const{ return weight < arg.weight; }; int from,to,weight; }; int parent[1001],height[1001]; Node* nodes; int getParent(int id){ if(parent[id] == id)return id; else{ return parent[id] = getParent(parent[id]); } } bool isSame(int a, int b){ return getParent(a) == getParent(b); } void unite(int a,int b){ int a_parent = getParent(a); int b_parent = getParent(b); if(a_parent == b_parent)return; if(height[a_parent] > height[b_parent]){ parent[b_parent] = a_parent; }else{ if(height[a_parent] == height[b_parent])height[b_parent]++; parent[a_parent] = b_parent; } } void init(){ for(int i = 1; i <= N; i++){ parent[i] = i; height[i] = 0; } } void func(){ for(int i = 0; i < M; i++){ scanf("%d %d %d",&nodes[i].from,&nodes[i].to,&nodes[i].weight); } sort(nodes,nodes+M); int count = 0,ans; init(); for(int i = 0; i < M; i++){ if(!isSame(nodes[i].from,nodes[i].to)){ unite(nodes[i].from,nodes[i].to); if(count == (N-1)/2){ ans = nodes[i].weight; break; } count++; } } printf("%d\n",ans); } int main(){ nodes = (Node*)malloc(sizeof(Node)*10000); while(true){ scanf("%d %d",&N,&M); if(N == 0 && M == 0)break; func(); } }
0
#include <bits/stdc++.h> using namespace std; const long long int M = 4e3 + 5, LG = 30, inf = 1e18, mod = 1e9 + 7; string s; long long int f(string t) { long long int cnt = 0; for (int i = 0; i < ((long long int)s.size()) - ((long long int)t.size()) + 1; i++) { for (int j = 0; j < ((long long int)t.size()); j++) { if (s[i + j] != t[j]) goto here; } cnt++; here: continue; } return cnt; } int main() { ios::sync_with_stdio(0); cin >> s; long long int cnt = f("Danil") + f("Olya") + f("Slava") + f("Ann") + f("Nikita"); cout << (cnt == 1 ? "YES" : "NO"); }
1