func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int getint() { int s = 0, o = 1; char c; for (c = getchar(); c < 0 || c > 9 ; c = getchar()) if (c == - ) o = -1; for (; c >= 0 && c <= 9 ; c = getchar()) s *= 10, s += c - 0 ; return s * o; } const int maxn = 30005; int gem[maxn]; int a[maxn]; int dp[maxn][601], all, len, n, x; inline void up(int &a, int b) { if (a < b) a = b; } int main(int argc, char const *argv[]) { n = getint(), len = getint(); for (int i = 0; i < n; i++) { x = getint(); gem[x]++; all = max(all, x); } int r = 0; dp[len][0 + 300] = gem[len] + 1; for (int i = 0; i <= all; ++i) for (int j = -300; j < 300; ++j) if (dp[i][j + 300]) { r = max(r, dp[i][j + 300] - 1); if (len + j > 0 && i + len + j - 1 <= all) up(dp[i + len + j - 1][j - 1 + 300], dp[i][j + 300] + gem[i + len + j - 1]); if (i + len + j <= all) up(dp[i + len + j][j + 300], dp[i][j + 300] + gem[i + len + j]); if (i + len + j + 1 <= all) up(dp[i + len + j + 1][j + 1 + 300], dp[i][j + 300] + gem[i + len + j + 1]); } printf( %d n , r); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline T read() { T f = 0, x = 0; char c = getchar(); while (!isdigit(c)) f = c == - , c = getchar(); while (isdigit(c)) x = x * 10 + c - 48, c = getchar(); return f ? -x : x; } namespace run { inline char getc() { char c = getchar(); while (!isalpha(c)) c = getchar(); return c; } const int N = 2e5 + 9; int head[N], nex[N], to[N], cnt, deg[N]; inline void add(int u, int v) { deg[v]++, nex[++cnt] = head[u], head[u] = cnt, to[cnt] = v; } int n, m, bel[N], tot, dfn[N], num, dwn[N]; inline void dfs(int u) { dfn[u] = ++num; for (int i = head[u]; i; i = nex[i]) dfs(to[i]); dwn[u] = num; } struct node { int x, l, r, v; explicit node() {} node(int _x, int _l, int _r, int _v) : x(_x), l(_l), r(_r), v(_v) {} inline bool operator<(const node &A) const { return x < A.x; } } q[N << 1]; namespace Seg { int sum[N << 2], num[N << 2]; inline void pushup(int k, int l, int r) { if (l == r) return num[k] = sum[k] > 0, void(); if (sum[k]) num[k] = r - l + 1; else num[k] = num[(k << 1)] + num[(k << 1 | 1)]; } inline void modify(int k, int l, int r, int _l, int _r, int v) { if (_l <= l && r <= _r) return sum[k] += v, pushup(k, l, r); if (_l <= ((l + r) >> 1)) modify((k << 1), l, ((l + r) >> 1), _l, _r, v); if (_r > ((l + r) >> 1)) modify((k << 1 | 1), ((l + r) >> 1) + 1, r, _l, _r, v); pushup(k, l, r); } } // namespace Seg inline int id(int x, int y) { return (x - 1) * m + y; } int main() { n = read<int>(), m = read<int>(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { char c = getc(); if (c == D ) { bel[id(i - 1, j)] = id(i, j), bel[id(i, j)] = id(i - 1, j); if (i > 2) add(id(i - 2, j), id(i, j)); if (i + 1 <= n) add(id(i + 1, j), id(i - 1, j)); } if (c == R ) { bel[id(i, j - 1)] = id(i, j), bel[id(i, j)] = id(i, j - 1); if (j > 2) add(id(i, j - 2), id(i, j)); if (j + 1 <= m) add(id(i, j + 1), id(i, j - 1)); } } for (int i = 1; i <= n * m; i++) if (!deg[i]) dfs(i); for (int i = 1; i <= n * m; i++) { int l = dfn[bel[i]], r = dwn[bel[i]]; q[++tot] = node(dfn[i], l, r, 1), q[++tot] = node(dwn[i] + 1, l, r, -1); } sort(q + 1, q + tot + 1); long long ans = 0; for (int i = 1, j, las = 0; i <= tot; i = j + 1) { j = i; while (j + 1 <= tot && q[j + 1].x == q[i].x) j++; ans += 1LL * (q[i].x - las) * Seg::num[1], las = q[i].x; for (int k = i; k <= j; k++) Seg::modify(1, 1, n * m, q[k].l, q[k].r, q[k].v); } printf( %lld n , ans >> 1); return 0; } } // namespace run int main() { return run::main(); }
#include <bits/stdc++.h> using namespace std; int ans1 = 0, ans2 = 0, ans3 = 0, a[100]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (i % 3 == 1) ans1 += a[i]; if (i % 3 == 2) ans2 += a[i]; if (i % 3 == 0) ans3 += a[i]; } if (ans1 >= ans2 && ans1 >= ans3) { cout << chest << endl; return 0; } if (ans2 >= ans1 && ans2 >= ans3) { cout << biceps << endl; return 0; } if (ans3 >= ans1 && ans3 >= ans2) { cout << back << endl; return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; inline const int getint() { int r = 0, k = 1; char c = getchar(); for (; c < 0 || c > 9 ; c = getchar()) if (c == - ) k = -1; for (; c >= 0 && c <= 9 ; c = getchar()) r = r * 10 + c - 0 ; return k * r; } const int N = 1e5 + 10; char s[N]; int n; int main() { scanf( %s , s + 1); n = strlen(s + 1); int sum = 0, x = 0, y = 0, z = 0, need = 0; for (int i = (1); i <= (n); ++i) if (s[i] == ( ) ++x; else if (s[i] == ) ) ++y; else ++z; need = x - y - (z - 1); if (need <= 0 && z) { puts( -1 ); return 0; } int now = z; for (int i = (n); i >= (1); --i) { if (s[i] == ( ) ++sum; else if (s[i] == ) ) --sum; else { if (now == z) { sum -= need; } else { sum--; } --now; } if (sum > 0) { puts( -1 ); return 0; } } if (sum != 0) { puts( -1 ); return 0; } now = 0; for (int i = (1); i <= (n); ++i) { if (s[i] == # ) { ++now; if (now == z) printf( %d n , need); else puts( 1 ); } } return 0; }
#include<bits/stdc++.h> #define inf 0x3f3f3f using namespace std; typedef long long ll; const int maxn = 3e5+5; const int mod = 1e9+7; int a[maxn],b[maxn]; int main() { int T; cin>>T; while(T--){ int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int j=1;j<=n;j++) cin>>b[j]; int cnt1=0,cnt2=0; vector<int> v1,v2; for(int i=1;i<=n;i++){ if(a[i]>b[i]){ cnt1+=a[i]-b[i]; for(int j=0;j<a[i]-b[i];j++) v1.push_back(i); } else{ cnt2+=b[i]-a[i]; for(int j=0;j<b[i]-a[i];j++) v2.push_back(i); } } if(cnt1!=cnt2){ cout<< -1 n ; continue; } cout<<v1.size()<< n ; for(int i=0;i<v1.size();i++){ cout<<v1[i]<< <<v2[i]<< n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, n, m, z, ck = 0; cin >> n >> m >> z; for (i = 1; i <= z; i++) { if (n * i > z) break; else if ((n * i) % m == 0) { ck++; } } cout << ck << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1700; int n, m, tim, res[N * N], num[N * N], a[N][N], s[N][N], id[N][N]; bool ray[N][N]; bool check(int x, int y) { return s[x + 3][y + 3] - s[x - 4][y + 3] - s[x + 3][y - 4] + s[x - 4][y - 4] <= 15; } void dfs1(int x, int y) { if (id[x][y] || !a[x][y]) return; id[x][y] = *res; dfs1(x - 1, y); dfs1(x + 1, y); dfs1(x, y - 1); dfs1(x, y + 1); } void dfs2(int x, int y) { if (!a[x][y] || ray[x][y]) return; if (!check(x, y)) { if (num[id[x][y]] != tim) { num[id[x][y]] = tim; res[id[x][y]]++; } return; } ray[x][y] = true; dfs2(x - 1, y); dfs2(x + 1, y); dfs2(x, y - 1); dfs2(x, y + 1); } int main() { scanf( %d%d , &n, &m); n += 5; m += 5; for (int i = 6; i <= n; i++) for (int j = 6; j <= m; j++) scanf( %d , &a[i][j]); n += 5; m += 5; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] && !id[i][j]) { ++*res; dfs1(i, j); } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] && !ray[i][j] && check(i, j)) { ++tim; dfs2(i, j); } sort(res + 1, res + *res + 1); printf( %d n , *res); for (int i = 1; i <= *res; i++) printf( %d , res[i]); }
#include <bits/stdc++.h> using namespace std; template <typename T> void join(vector<T>& arr, string sep) { bool first = true; for (auto t : arr) { if (first) { first = false; cout << t; } else { cout << sep << t; } } cout << n ; } struct Euler { vector<vector<pair<long long, long long>>> g; long long n; vector<long long> path; long long edge_count; Euler(long long n) : n(n) { g.resize(n); edge_count = 0; } void addEdge(long long u, long long v) { long long w = edge_count++; g[u].push_back({v, w}); g[v].push_back({u, w}); } bool compute() { int first = 0; while (first < n && g[first].empty()) first++; if (first == n) return true; long long v1 = -1, v2 = -1; for (long long u = 0; u < (long long)(n); ++u) { if (g[u].size() & 1) { return false; } } if (v1 != -1) { assert(v2 != -1); } vector<long long> visited(edge_count); vector<long long> s = {first}; while (!s.empty()) { long long u = s.back(); long long v; while (true) { if (g[u].empty()) { v = -1; path.push_back(u); s.pop_back(); break; } long long v = g[u].back().first; long long w = g[u].back().second; g[u].pop_back(); if (visited[w]) { continue; } visited[w] = true; s.push_back(v); break; } } if (v1 != -1) { for (long long i = 0; i < (long long)(path.size() - 1); ++i) { if ((path[i] == v1 && path[i + 1] == v2) || (path[i] == v2 && path[i + 1] == v1)) { vector<long long> res; for (long long j = (long long)(i + 1); j < (long long)(path.size()); ++j) { res.push_back(path[j]); } for (long long j = (long long)(1); j < (long long)(i + 1); ++j) { res.push_back(path[j]); } path = res; break; } } } for (long long u = 0; u < (long long)(n); ++u) { if (!g[u].empty()) { return false; } } return true; } }; long long getPref(long long c, long long i) { return ((1 << i) - 1) & c; } struct PairHash { int operator()(const pair<long long, long long>& pp) const { const int p = 31; const int m = 1e9 + 9; long long l = pp.first; long long r = (pp.second * p) % m; return (l + r) % m; } }; void solve() { long long n; cin >> n; vector<pair<long long, long long>> colors(n); for (long long i = 0; i < (long long)(n); ++i) { long long a, b; cin >> a >> b; colors[i] = {a, b}; } vector<long long> res; for (long long i = (long long)(1); i < (long long)(2 * n + 1); ++i) { res.push_back(i); } long long lowest = 0; long long l = 1; long long r = 20; while (l <= r) { long long i = l + (r - l) / 2; unordered_map<pair<long long, long long>, vector<pair<long long, long long>>, PairHash> nodes2Idx; Euler e(1 << i); for (long long j = 0; j < (long long)(n); ++j) { long long pref1 = getPref(colors[j].first, i); long long pref2 = getPref(colors[j].second, i); long long idx1 = 2 * j + 1; long long idx2 = 2 * j + 2; if (pref1 > pref2) { swap(pref1, pref2); swap(idx1, idx2); } nodes2Idx[{pref1, pref2}].push_back({idx1, idx2}); e.addEdge(pref1, pref2); } if (!e.compute()) { r = i - 1; continue; } l = i + 1; lowest = i; assert(e.path.size() == n + 1); for (long long j = 0; j < (long long)(n); ++j) { long long pref1 = e.path[j]; long long pref2 = e.path[j + 1]; bool swaped = false; if (pref1 > pref2) { swap(pref1, pref2); swaped = true; } auto it = nodes2Idx.find({pref1, pref2}); pair<long long, long long> idxPair = it->second.back(); it->second.pop_back(); long long idx1 = idxPair.first; long long idx2 = idxPair.second; if (swaped) swap(idx1, idx2); res[2 * j] = idx1; res[2 * j + 1] = idx2; } } cout << lowest << n ; join(res, ); } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int n, k, a[111000], s, ans[111000]; int main() { cin >> n >> k; for (register int i = 1; i <= n; i++) { cin >> a[i]; s += a[i]; } if (s % k != 0) { cout << No ; return 0; } int t = s / k; int cnt = 0; for (register int i = 1; i <= n;) { int sum = 0, tmp = 0; while (sum < t) { sum += a[i]; i++; tmp++; } if (sum != t) { cout << No ; return 0; } ans[++cnt] = tmp; } cout << Yes << endl; for (register int i = 1; i <= cnt; i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; int w[100001]; int main() { map<string, int> mp; int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> w[i]; } sort(w, w + n); string s; for (int i = 0; i < m; i++) { cin >> s; mp[s]++; } int a[101], k = 0; for (map<string, int>::iterator i = mp.begin(); i != mp.end(); i++) { a[k++] = (*i).second; } sort(a, a + k); long long s1 = 0, p = 0; for (int i = 0; i < k; i++) { s1 += a[k - i - 1] * w[i]; p += a[k - i - 1] * w[n - i - 1]; } cout << s1 << << p; }
#include <bits/stdc++.h> using namespace std; const int Max = 2e6 + 10; const int Mod = 1e9 + 7; const int MAX = 2e18; int main() { long long int x, j = 0, mx = 0, mx2 = 0, cnt = 0; bool f = 0, f2 = 0, f3 = 0; string s; cin >> x >> s; long long int ar[x + 5]; memset(ar, 0, sizeof(ar)); ; for (int i = 0; i < x; i++) { if (s[i] == G ) { ar[j]++; f2 = 1; cnt++; if (f == 1) { f3 = 1; } } else { f = 1; if (i != x - 1) { j++; } } } if (f == 0) { cout << x << endl; ; return 0; } if (f2 == 0) { cout << 0 << endl; ; return 0; } if (f3 == 0) { cout << cnt << endl; ; return 0; } for (int i = 0; i < j + 1; i++) { if (ar[i + 1] == 0) { mx2 = max(mx2, ar[i]); ar[i] = 0; i++; continue; } mx2 = max(mx2, ar[i]); ar[i] = ar[i] + ar[i + 1]; mx = max(mx, ar[i]); } if (mx2 > mx) { if (mx2 < cnt) { cout << mx2 + 1 << endl; ; } else { cout << mx2 << endl; ; } } else if (cnt > mx) { cout << mx + 1 << endl; ; } else { cout << mx << endl; ; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; if (n <= 30) cout << NO n ; else { cout << YES n ; long long d = n - 30; long long a = 6, b = 10, c = 14; if (d == 6 || d == 10 || d == 14) cout << 6 10 15 << d - 1 << endl; else cout << 6 10 14 << d << endl; } } signed main() { long long t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; const ll mod = 1000000007; const ll M = 1000000000000000000ll; inline ll add(ll a, ll b) { return ((a % mod + b % mod) % mod); } inline ll sub(ll a, ll b) { return ((a % mod - b % mod + mod) % mod); } inline ll mult(ll a, ll b) { return (((a % mod) * (b % mod)) % mod); } inline ll power(ll n, ll x) { ll p = 1; while (x != 0) { if (x % 2 == 0) { n = mult(n, n); x = x / 2; } p = mult(p, n); x--; } return p; } inline ll recurmult(ll a, ll b) { ll r = 0; while (b) { if (b & 1) r = add(r, a); a = mult(2, a); b >>= 1; } return r % mod; } int main() { ios::sync_with_stdio(0); cin.tie(0); ll n, ma = 0; cin >> n; ll arr[n + 1]; ll ans[n + 1]; for (ll i = 0; i < n; i++) cin >> arr[i]; ans[0] = arr[0]; ma = ans[0]; for (ll i = 1; i < n; i++) { ans[i] = ma + arr[i]; ma = max(ma, ans[i]); } for (ll i = 0; i < n; i++) cout << ans[i] << ; }
#include <bits/stdc++.h> using namespace std; int main() { int x1, x2, x3, n1, n2, n3; scanf( %d %d %d , &x1, &x2, &x3); n3 = (x1 - x2 + x3) / 2; n2 = x3 - n3; n1 = x1 - n3; if (n1 < 0 || n2 < 0 || n3 < 0 || 2 * (n1 + n2 + n3) != x1 + x2 + x3) printf( Impossible n ); else printf( %d %d %d n , n1, n2, n3); return 0; }
#include <bits/stdc++.h> using namespace std; struct pp { long long int len, v; }; bool cmp(pp& a, pp& b) { if (a.v == b.v) return a.len > b.len; return a.v > b.v; } int main() { long long int n, k; cin >> n; cout << (n - 1) * n * (2 * n - 1) / 6 - 1 - 1 + n * (n - 1) / 2 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<db, db>; using vi = vector<int>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<string>; using vii = vector<pii>; using vll = vector<pll>; using vdd = vector<pdd>; template <class T> bool ckmin(T& a, const T& b) { return (b < a) ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return (a < b) ? a = b, 1 : 0; } const ll MOD = 1e9 + 7; const ll MOD2 = 998244353; const int kx[] = {0, 1, 0, -1}, ky[] = {1, 0, -1, 0}; void fileIO(string filename) { freopen((filename + .in ).c_str(), r , stdin); freopen((filename + .out ).c_str(), w , stdout); } int solve(string s) { int n = (int)s.size(), tot = count((s).begin(), (s).end(), 1 ), cmin = tot, cur = 0; vi dp(n); for (int(i) = 0; i < n; ++i) { cur += s[i] == 1 ; dp[i] = s[i] == 0 ; if (i) dp[i] += min(dp[i - 1], cur - (s[i] == 1 )); ckmin(cmin, dp[i] + tot - cur); } return cmin; } int main(void) { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, k, tot, ans = INT_MAX; cin >> n >> k; string ss; cin >> ss; tot = count((ss).begin(), (ss).end(), 1 ); vs s(k); vi cnt(k); for (int(i) = 0; i < n; ++i) s[i % k] += ss[i]; for (int(i) = 0; i < k; ++i) cnt[i] = count((s[i]).begin(), (s[i]).end(), 1 ); for (int(i) = 0; i < k; ++i) ckmin(ans, solve(s[i]) + tot - cnt[i]); cout << ans << n ; } cerr << Time elapsed: << 1.0 * clock() / CLOCKS_PER_SEC << s n ; }
#include<bits/stdc++.h> using namespace std; #define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define PB(x, v) x.push_back(v); #define M_P(a, b) make_pair(a, b) #define pll pair<ll, ll> #define ll long long int #define mll map<ll, ll> #define vl vector<ll> #define fa(x, v) for(auto x: v) #define fr(i, a, b, step) for(auto i= a;i <=b; i+=step) #define TEST Int test; cin >> test; while(test--) #define FTEST Int TesT; cin >> TesT; for(Int test = 1; test<=TesT; test++) /**So..a**/ #define B_ begin() #define E_ end() #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define NL_ n #define F first #define S second #define FF first.first #define FS first.second #define SF second.first #define SS second.second #include <iomanip> #define __lcm(a, b) (a/__gcd(a, b)*b) #define PI acos(-1) #define EXP 10e-6 ll prime(ll a,ll b){ ll ans=0; return ans; } int main() { ll n,m,k,i,j,sum=0,cnt=0,x; string s,ss,str,st; bool sign=1,ok=true,cheak=1; ll t,tc; ll a,b,c; ll ans=0; ok=1; sum=0; ll fac=1; cin>>t; while(t--) { cnt=0; ok=1;sign=0; int first=0; int p=0; int fst=0,scnd=0; cin>>n>>k; int mid=(n+1)/2; vector<ll>vv,v; for(i=1;i<=mid;i++){ vv.push_back(i); } for(i=mid+1;i<=n;i++){ v.push_back(i); } /* for(auto i:vv)cout<<i<< ; cout<<endl; for(auto i:v)cout<<i<< ; cout<<endl; */ if(k==0){ for(i=1;i<=n;i++)cout<<i<< ; cout<<endl; continue; } if((k+k+1) > n)cout<<-1<<endl; else { for(i=1;i<=n;i++) { if(ok){ cout<<vv[first++]<< ; ok=0; sign=1; fst++; continue; } if(sign){ ok=1; sign=0; cout<<v[p++]<< ; scnd++; cnt++; if(cnt==k)break; } } for(i=fst;i<vv.size();i++)cout<<vv[i]<< ; for(i=scnd;i<v.size();i++)cout<<v[i]<< ; cout<<endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; void read(long long a = 0) { freopen( in , r , stdin); if (a) freopen( out , w , stdout); } int main() { long long n, m, k; cin >> n >> m >> k; long long done = 0; while (k--) { long long x, y; cin >> x >> y; long long mi = 1e15; mi = min(mi, x - 1); mi = min(mi, n - x); mi = min(mi, y - 1); mi = min(mi, m - y); done |= (mi <= 4); } if (done) cout << YES ; else cout << NO ; }
#include <bits/stdc++.h> const int inf = 1 << 30; const long long INF = 1ll << 61; using namespace std; int n, m; int c[1000000 + 55]; int nnum[1000000 + 55]; int aa[1000000 + 55]; int sum[1000000 + 55]; int len; void init() { memset(c, 0, sizeof(c)); memset(aa, -1, sizeof(aa)); } bool input() { while (cin >> n >> m) { for (int i = 0; i < m; i++) cin >> nnum[i]; return false; } return true; } int lowbit(int x) { return x & (-x); } void add(int i, int val) { while (i <= n) { c[i] += val; i += lowbit(i); } } int get_sum(int i) { int sum = 0; while (i > 0) { sum += c[i]; i -= lowbit(i); } return sum; } void binary_find(int pos) { for (int i = 0; i <= pos; i++) { int l = 1; int r = n; while (l <= r) { int mid = (l + r) >> 1; int ret = get_sum(mid); if (aa[mid] == -1) { if (ret >= nnum[i] - i) r = mid - 1; else l = mid + 1; } else if (ret == nnum[i] - i) { add(mid, -1); aa[mid] = -1; len--; break; } else if (ret > nnum[i] - i) r = mid - 1; else if (ret < nnum[i] - i) l = mid + 1; } } } void cal() { int q = n; len = 0; int cnt = 1; while (q--) { int type; cin >> type; if (type == -1) { if (len == 0) continue; if (nnum[0] > len) continue; int now = lower_bound(nnum, nnum + m, len) - nnum; binary_find(now); } else { aa[cnt] = type; add(cnt, 1); cnt++; len++; } } if (len <= 0) { puts( Poor stack! ); return; } for (int i = 1; i <= n; i++) if (aa[i] != -1) printf( %d , aa[i]); puts( ); } void output() {} int main() { while (true) { init(); if (input()) return 0; cal(); output(); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int e, d, c, x, aux, aux2; priority_queue<pair<long long int, long long int> > q; void res(long long int n) { long long int k = n; e = 0; d = 1; while (k > 0) { d = d * 10 + 1; e++; k = k / 10; } c = (d - 1) / 10; if (c > n) { d = c; c = (d - 1) / 10; e--; } } void dis(long long v) { q.push(make_pair(0, v)); aux = q.top().second; while (aux != 0) { res(aux); aux2 = q.top().first; q.pop(); if (d - aux < aux) q.push(make_pair(aux2 - (e + 1), d - aux)); q.push(make_pair(aux2 - e, aux - c)); aux = q.top().second; } x = q.top().first; } int main() { long long int a; cin >> a; dis(a); cout << x * -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; int a[111111], b[111111], ra[111111]; set<pair<int, int> > s; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0, _n = (int)(n); i < _n; i++) cin >> a[i], a[i]--, ra[a[i]] = i; for (int i = 0, _n = (int)(n); i < _n; i++) cin >> b[i], b[i]--; for (int i = 0, _n = (int)(n); i < _n; i++) s.insert(pair<int, int>(ra[b[i]] - i, i)); for (int i = 0, _n = (int)(n); i < _n; i++) { int ans = 999999; auto x = s.upper_bound(pair<int, int>(-i, 0)); if (x != s.end()) ans = min(ans, abs(x->first + i)); if (x != s.begin()) x--, ans = min(ans, abs(x->first + i)); cout << ans << endl; x = s.lower_bound(pair<int, int>(ra[b[i]] - i, i)); s.erase(x); s.insert(pair<int, int>(x->first - n, i)); } cout << endl; { int _; cin >> _; } return 0; }
#include <bits/stdc++.h> int x[100010]; int main() { int a, b, c, i; scanf( %d%d%d , &a, &b, &c); for (i = 1; i <= a; i++) scanf( %d , &x[i]); int s = 0, t = 1; long long int count = 0; while (1) { if (t > a && s == 0) { printf( %I64d , count); return 0; } while (t <= a && s + x[t] <= b) s += x[t], t++; if (s < c) { count++; s = 0; } else { count += (s / c); s = s % c; } } }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, t; cin >> n >> t; string s; cin >> s; for (long long int i = 0; i < t; i++) { for (long long int j = 0; j < n - 1; j++) { if (s[j] == B && s[j + 1] == G ) { s[j] = G ; s[j + 1] = B ; j = j + 1; } } } for (long long int i = 0; i < n; i++) { cout << s[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> const int inf = 0xfffffff; const long long INF = 1ll << 61; using namespace std; int n, s, k; int aa[100 + 55][100 + 55]; int ans; void init() { memset(aa, 0, sizeof(aa)); } bool input() { while (scanf( %d %d %d , &n, &s, &k) == 3) { for (int i = 0; i < n; i++) for (int j = 0; j < s; j++) scanf( %d , &aa[i][j]); return false; } return true; } void cal() { ans = 11; if (n < s) { for (int i = 0; i < s; i++) for (int j = i + 1; j < s; j++) swap(aa[i][j], aa[j][i]); swap(n, s); } if (n <= k) { for (int i = 0; i < (1 << s); i++) { int sum = 0; for (int j = 0; j < n; j++) { int now = 0; for (int l = 0; l < s; l++) { if (i & (1 << l)) { if (!aa[j][l]) now++; } else { if (aa[j][l]) now++; } } sum += min(now, s - now); } ans = min(ans, sum); } } else { for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < n; j++) { int now = 0; for (int l = 0; l < s; l++) { if (aa[i][l] != aa[j][l]) now++; } sum += min(now, s - now); } ans = min(ans, sum); } } } void output() { if (ans <= k) cout << ans << endl; else puts( -1 ); } int main() { while (true) { init(); if (input()) return 0; cal(); output(); } }
#include <bits/stdc++.h> using namespace std; int main() { int number; cin >> number; long long ans = number; while (1) { int x = 100000000; for (int i = 2; i <= sqrt(number); i++) { if (number % i == 0) { x = min(x, i); } } if (x == 100000000) break; ans += number / x; number = number / x; } cout << ans + 1; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, sum; cin >> n; long long int arr[n]; sum = 0; for (long long int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } sort(arr, arr + n); long long int curr = 0; long long int cnt = 0; long long int ind = n - 1; while (curr <= sum) { ++cnt; curr += arr[ind]; sum -= arr[ind]; --ind; } cout << cnt << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int T, n, x; int main() { scanf( %d , &T); while (T--) { scanf( %d%d , &n, &x); printf( %d n , 2 * x); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, s[1000005], t[1000005]; int bef[1000005][2]; long long ss, st, cs, ct; bool sw; int main() { memset(bef, -1, sizeof(bef)); bef[0][0] = bef[0][1] = 0; scanf( %d , &n); for (int i = (1); i <= (n); i++) scanf( %d , &s[i]), ss += s[i]; for (int i = (1); i <= (n); i++) scanf( %d , &t[i]), st += t[i]; if (ss < st) { swap(ss, st); for (int i = (1); i <= (n); i++) swap(s[i], t[i]); sw = true; } int p = 0; for (int i = (1); i <= (n); i++) { ct += t[i]; while (p < n && cs + s[p + 1] <= ct) cs += s[++p]; long long d = ct - cs; assert(d >= 0 && d < n); if (bef[d][0] == -1) { bef[d][0] = p; bef[d][1] = i; } else { if (sw) { printf( %d n , i - bef[d][1]); for (int j = (bef[d][1] + 1); j <= (i); j++) printf( %d%c , j, j == i ? n : ); } printf( %d n , p - bef[d][0]); for (int j = (bef[d][0] + 1); j <= (p); j++) printf( %d%c , j, j == p ? n : ); if (!sw) { printf( %d n , i - bef[d][1]); for (int j = (bef[d][1] + 1); j <= (i); j++) printf( %d%c , j, j == i ? n : ); } return 0; } } }
#include <bits/stdc++.h> using namespace std; const int M = 998244353; int main() { long long n; cin >> n; map<long long, long long> mp; long long a[200005]; for (long long i = 0; i < n; i++) { cin >> a[i]; mp[a[i]] = i; } long long ans = 1, w = mp[a[0]]; for (int i = 0; i < n; i++) { if (i > w) { ans = (ans * 2) % M; } w = max(w, mp[a[i]]); } cout << ans; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int ans = 0; while (n % 3 == 0 || n % 6 == 0) { if (n % 6 == 0) { n /= 6; ans++; } else if (n % 3 == 0) { n /= 3; ans += 2; } } if (n != 1) { cout << -1 << endl; return; } cout << ans << endl; } int main() { int t = 1, i = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 9, inf = 1e18, mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { int a, b, c, d, k; cin >> a >> b >> c >> d >> k; int x = (a + c - 1) / c; int y = k - x; if (y * d >= b) cout << x << << y << n ; else cout << -1 << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string str; int j = 0; cin >> str; int len = str.length(); for (int i = 0; i < len - 1; i = i + 2) { cout << str[i]; } cout << str[len - 1]; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } long long mod = 1000000007; int main() { ios::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; while (t--) { long long p, q; cin >> p >> q; std::vector<long long> factors; long long r = p; while (r % q == 0) { r /= q; } long long Q = q; long long i = 2; while (q > 1 and i * i <= Q) { if (q % i == 0) { factors.push_back(i); while (q % i == 0) { q /= i; } } i++; } if (q != 1) { factors.push_back(q); } std::vector<long long> v; for (auto it : factors) { long long P = p; while (P % Q == 0) { P /= it; } v.push_back(P); } std::cout << *max_element(v.begin(), v.end()) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, n, moves = 0, flag = 1, gears; cin >> n; cin >> gears; moves = n - gears; for (i = 1; i < n; i++) { cin >> gears; if (i % 2 == 1) gears = (gears + (n - moves)) % n; else gears = (gears + moves) % n; if (gears != i) { cout << No << endl; return 0; } } cout << Yes << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[3000]; bool apare[1010]; int n, k, c; int main() { ios_base ::sync_with_stdio(0); cin >> n >> k; while (k--) { cin >> c; apare[c] = 1; } queue<int> q; vector<int> lungimi; for (int i(0); i < 1010; i++) { if (!apare[i]) continue; int x(n - i + 1500); dp[x] = 1; q.push(x); lungimi.push_back(x - 1500); } while (!q.empty()) { int x(q.front()), t(dp[x]); q.pop(); for (auto i : lungimi) { if (x + i >= 0 && x + i < 3000 && !dp[x + i]) { dp[x + i] = 1 + t; q.push(x + i); } } } cout << (dp[1500] ? dp[1500] : -1); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; using namespace std; const int maxn = 404; int n, m, k; int s[103], t[103]; int c[103][103], d[103][103], e[103][103], f[103][103]; struct Edge { int u, v, c, w, next; Edge(int u, int v, int c, int w, int next) : u(u), v(v), c(c), w(w), next(next) {} Edge() {} } edge[100005]; int head[maxn], E; void init() { memset(head, -1, sizeof(head)); E = 0; } void add(int s, int t, int c, int w) { edge[E] = Edge(s, t, c, w, head[s]); head[s] = E++; edge[E] = Edge(t, s, 0, -w, head[t]); head[t] = E++; } int S, T; int pre[maxn], dis[maxn]; bool vis[maxn]; bool spfa(int s, int t, int n) { int i, u, v; for (i = 0; i <= n; i++) dis[i] = inf, pre[i] = -1, vis[i] = false; queue<int> q; vis[s] = true; dis[s] = 0; q.push(s); while (!q.empty()) { u = q.front(); q.pop(); vis[u] = false; for (i = head[u]; ~i; i = edge[i].next) { v = edge[i].v; if (edge[i].c && dis[v] > dis[u] + edge[i].w) { dis[v] = dis[u] + edge[i].w; pre[v] = i; if (!vis[v]) { vis[v] = true; q.push(v); } } } } if (dis[t] == inf) return 0; return 1; } int ans; pair<int, int> mcmf(int s, int t, int n) { int aug, minc = 0, i, maxf = 0; while (spfa(s, t, n)) { aug = inf + 1; for (i = pre[t]; ~i; i = pre[edge[i].u]) aug = min(aug, edge[i].c); for (i = pre[t]; ~i; i = pre[edge[i].u]) { edge[i].c -= aug; edge[i ^ 1].c += aug; } maxf += aug; minc += dis[t] * aug; } return make_pair(maxf, minc); } int Map[55][55]; void build(int m) { int i, j; init(); for (i = 0; i < n; i++) for (j = 0; j < n; j++) { if (Map[i][j]) { add(i, j, Map[i][j], 0); add(i, j, k, 1); } } add(n, 0, m, 0); } int main() { int i, j; scanf( %d%d , &n, &k); for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf( %d , &Map[i][j]); int l = 0, r = 1e9; while (l <= r) { int m = (l + r) >> 1; build(m); pair<int, int> t = mcmf(n, n - 1, n + 1); if (t.first == m && t.second <= k) { l = m + 1; ans = m; } else r = m - 1; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; vector<int> way(s.size()); way[0] = 0; for (int i = 1; i < (int)s.size(); i++) { if (s[i] == 0 ) way[i] = i; else way[i] = way[i - 1]; } int l = 0, r = n - 1; while (r - l > 1) { int m = (l + r) / 2; int cnt = k - 1; int now = 0; int last = 0; while (now < n && cnt > 0) { now += m; now = min(now, n - 1); now = way[now]; cnt--; last = max(last, now); } if (last == n - 1) { r = m; } else { l = m; } } cout << l << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s = ; for (int i = 0; i < k; i++) s += char(i + 97); for (int i = 0; i < n - k; i++) { s += char(i % k + 97); } cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int i = 0; vector<int> ans; ans.push_back(v[0]); ans.push_back(v[n - 1]); while (i < n - 3) { int k = v[i + 1]; int l = v[i + 2]; if (l >= k) { ans.push_back(l); i = i + 2; } else { ans.push_back(k); i++; } } int out = *min_element(ans.begin(), ans.end()); cout << out; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m; struct pt { long long x, y; pt() {} pt(long long x, long long y) : x(x), y(y) {} }; vector<vector<long long>> used; vector<vector<long long>> g; void parasha(pt q, pt p, long long weight, deque<pt>& bfs, vector<vector<long long>>& w) { if (q.x < 0 || q.y < 0 || q.x >= n || q.y >= m) return; if (g[q.x][q.y] == * ) return; if (w[q.x][q.y] > w[p.x][p.y] + weight) { w[q.x][q.y] = w[p.x][p.y] + weight; if (weight == 1) bfs.push_back(q); else bfs.push_front(q); } } int32_t main() { deque<pt> bfs; cin >> n >> m; vector<vector<long long>> w(n, vector<long long>(m, 3e9)); g.assign(n, vector<long long>(m)); used.assign(n, vector<long long>(m, 0)); long long r, c; cin >> r >> c; --r; --c; long long x, y; cin >> x >> y; for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { char x; cin >> x; g[i][j] = x; } } bfs.push_back(pt(r, c)); w[r][c] = 0; while (!bfs.empty()) { pt q = bfs.front(); bfs.pop_front(); if (used[q.x][q.y] == 1) { continue; } used[q.x][q.y] = 1; parasha(pt(q.x, q.y + 1), q, 0, bfs, w); parasha(pt(q.x + 1, q.y), q, 0, bfs, w); parasha(pt(q.x - 1, q.y), q, 0, bfs, w); parasha(pt(q.x, q.y - 1), q, 1, bfs, w); } long long ans = 0; for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) { if (w[i][j] <= x && w[i][j] - (c - j) <= y) ++ans; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = pair<lint, lint>; const int MAXN = 100005; int n, m, sum; int cnt[MAXN]; set<int> s[MAXN]; int p[MAXN]; vector<tuple<int, int, int>> dap; void MOVE(int dst, int src, int v) { vector<int> mutation; for (auto &i : s[dst]) { if (s[src].find(i) == s[src].end()) { continue; } s[src].erase(i); mutation.push_back(i); } while (v--) { auto x = *s[src].begin(); s[src].erase(x); s[dst].insert(x); dap.emplace_back(src + 1, dst + 1, x); } for (auto &i : mutation) s[src].insert(i); } int main() { scanf( %d %d , &n, &m); for (int i = 0; i < n; i++) { int x; scanf( %d , &x); sum += x; p[i] = i; for (int j = 0; j < x; j++) { int v; scanf( %d , &v); s[i].insert(v); } } for (int i = 0; i < sum; i++) { cnt[n - 1 - i % n]++; } sort(p, p + n, [&](const int &a, const int &b) { return s[a].size() < s[b].size(); }); int lx = -1, ly = 1e9; for (int i = 0; i < n; i++) { if (s[p[i]].size() < cnt[i]) { lx = i; } if (s[p[i]].size() > cnt[i]) ly = min(ly, i); } if (lx == -1) { puts( 0 ); return 0; } while (true) { while (lx >= 0 && s[p[lx]].size() == cnt[lx]) lx--; while (ly < n && s[p[ly]].size() == cnt[ly]) ly++; if (lx == -1) break; int thres0 = cnt[lx] - s[p[lx]].size(); int thres1 = s[p[ly]].size() - cnt[ly]; MOVE(p[lx], p[ly], min(thres0, thres1)); } printf( %d n , dap.size()); for (auto &i : dap) { int x, y, z; tie(x, y, z) = i; printf( %d %d %d n , x, y, z); } }
#include <bits/stdc++.h> using namespace std; long long n, p, su; long long a[100005], b[100005]; long double ex[100005]; bool check(long double mid) { long double need = 0; for (int i = 0; i < n; i++) { long double ne = max(0.0L, a[i] * (mid - ex[i])); need += ne; } return (mid * p >= need); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> p; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; su += a[i]; } if (p >= su) { cout << -1; } else { for (int i = 0; i < n; i++) { ex[i] = (b[i] * 1.0L) / a[i]; } long double l = 0; long double r = 1e18; for (int i = 0; i < 120; i++) { long double mid = (l + r) / 2; if (check(mid)) { l = mid; } else { r = mid; } } if (r > 1e18 - 100) { cout << -1; } else { cout << setprecision(9) << l; } } return 0; }
#include <bits/stdc++.h> using namespace std; int W, H, N, num; char ch; multiset<int> maxHorSize, maxVertSize; set<int> horiz, vert; long long max_area; long long find_max(set<int> &setCut, char cut) { auto med = setCut.find(num); auto big = med; auto sml = med; ++big; --sml; if (cut == h ) { maxHorSize.erase(maxHorSize.find(*big - *sml)); maxHorSize.insert(*med - *sml); maxHorSize.insert(*big - *med); } else if (cut == v ) { maxVertSize.erase(maxVertSize.find(*big - *sml)); maxVertSize.insert(*med - *sml); maxVertSize.insert(*big - *med); } return (long long)(*maxHorSize.rbegin()) * (long long)(*maxVertSize.rbegin()); } int main() { scanf( %d%d%d , &W, &H, &N); horiz = {0, H}; vert = {0, W}; maxHorSize = {H}; maxVertSize = {W}; for (auto i = 1; i != N + 1 - 2 * (1 > N); i += 1 - 2 * (1 > N)) { cin >> ch >> num; if (ch == H ) { horiz.insert(num); max_area = find_max(horiz, h ); } else if (ch == V ) { vert.insert(num); max_area = find_max(vert, v ); } printf( %I64d n , max_area); } return 0; }
#include <bits/stdc++.h> using namespace std; set<pair<long long, long long> > S; set<pair<long long, long long> >::iterator it; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n; for (cin >> t; t--;) { cin >> n; S.clear(); long long a = 1, b = 0, ans = 0, u; for (int i = 0; i < n; i++) { cin >> u; long long s = a * (1 - b), e = a * (u - 1 - b); if (a < 0) swap(s, e); while (!S.empty()) { pair<long long, long long> v = *S.begin(); if (v.first >= s) break; else { S.erase(S.begin()); if (v.second >= s) S.insert(pair<long long, long long>(s, v.second)); } } while (!S.empty()) { it = S.end(); it--; pair<long long, long long> v = *it; if (v.second > e) S.erase(it); else break; if (v.first <= e) S.insert(pair<long long, long long>(v.first, e)); } if (S.empty()) { if (a > 0) S.insert(pair<long long, long long>(a * (1 - b), a * (u - b))); else S.insert(pair<long long, long long>(a * (u - b), a * (1 - b))); } else ans++; a *= -1, b = u - b; if (u % 2 == 0) { it = S.upper_bound( pair<long long, long long>(a * (u / 2 - b), 1000000000000000000ll)); if (it == S.begin()) S.insert( pair<long long, long long>(a * (u / 2 - b), a * (u / 2 - b))); else { it--; pair<long long, long long> v = *it; if (v.first <= a * (u / 2 - b) && v.second >= a * (u / 2 - b)) { S.clear(); a = 1; b = 0; ans++; } S.insert( pair<long long, long long>(a * (u / 2 - b), a * (u / 2 - b))); } } } cout << n * 2 - ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int A[26] = {0}, count = 0; string s; getline(cin, s); for (int i = 1; i < s.size(); i += 3) { A[int(s[i]) - 97] = 1; } for (int i = 0; i < 26; i++) { if (A[i] == 1) { count++; } } cout << count; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast,unroll-loops,fast-math ) using namespace std; long long poww(long long a, long long b, long long md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const int maxn = 2000 * 100 + 5; const long long inf = 9223372036854775807; const long long mod = 1e9 + 7; const int lg = 20; const int SQ = 6000; const int sq = maxn / SQ + 2; int cnt[sq][maxn], fen[sq][maxn], type[maxn], v[maxn], p[maxn], res, n, q, a[maxn], num[maxn], mark[maxn], ans2, ans3, ans, c[maxn]; vector<int> comp; unordered_map<int, int> to; void add(int pos, int i, int val) { for (i++; i < maxn; i += i & -i) fen[pos][i] += val; } int get(int pos, int i) { res = 0; for (i++; i > 0; i -= i & -i) res += fen[pos][i]; return res; } void build() { for (int i = 1; i <= n; i++) { cnt[i / SQ][a[i]]++; } for (int i = 1; i <= n; i++) { if (mark[a[i]]) continue; mark[a[i]] = 1; for (int j = 1; j < sq; j++) { cnt[j][a[i]] += cnt[j - 1][a[i]]; } } for (int i = 0; i < sq; i++) { for (int j = 1; j <= (long long)(comp).size(); j++) { add(i, cnt[i][j], 1); } } } void del(int x) { for (int i = x / SQ; i < sq; i++) { add(i, cnt[i][a[x]], -1); add(i, --cnt[i][a[x]], 1); } } void Add(int x) { for (int i = x / SQ; i < sq; i++) { add(i, cnt[i][a[x]], -1); add(i, ++cnt[i][a[x]], 1); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; comp.push_back(a[i]); } cin >> q; for (int i = 1; i <= q; i++) { cin >> type[i] >> v[i] >> p[i]; if (type[i] == 1) comp.push_back(v[i]); if (type[i] == 2) { if (v[i] > 3) { if (v[i] & 1) v[i] = 3; else v[i] = 2; } } } sort((comp).begin(), (comp).end()); comp.resize(unique((comp).begin(), (comp).end()) - comp.begin()); for (int i = 0; i < (long long)(comp).size(); i++) { to[comp[i]] = i + 1; num[i + 1] = comp[i]; } for (int i = 1; i <= n; i++) a[i] = to[a[i]]; for (int i = 1; i <= q; i++) { if (type[i] == 1) v[i] = to[v[i]]; } build(); int x, y, t, ct = 0; for (int i = 1; i <= q; i++) { ct += (type[i] == 2); if (type[i] == 1) { del(p[i]); a[p[i]] = v[i]; Add(p[i]); continue; } x = v[i], y = p[i]; if (x == 1) { cout << num[a[y]] << n ; continue; } ans = a[y]; if (y / SQ > 0) ans2 = cnt[y / SQ - 1][ans]; else ans2 = 0; for (int j = y - (y % SQ); j <= y; j++) { if (a[j] == a[y]) ans2++; } if (x == 2) { cout << ans2 << n ; continue; } if (y / SQ > 0) ans3 = get(y / SQ - 1, maxn / 2) - get(y / SQ - 1, ans2 - 1); else { ans3 = 0; for (int j = 1; j <= y; j++) { c[a[j]]++; if (c[a[j]] == ans2) ans3++; } for (int j = 1; j <= y; j++) c[a[j]]--; cout << ans3 << n ; continue; } t = y / SQ - 1; for (int j = y - (y % SQ); j <= y; j++) { cnt[t][a[j]]++; if (cnt[t][a[j]] == ans2) ans3++; } for (int j = y - (y % SQ); j <= y; j++) cnt[t][a[j]]--; cout << ans3 << n ; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long mx = 1e5 + 5; long long middle(long long a, long long b, long long c) { if ((a <= b && b <= c) || (c <= b && b <= a)) return b; else if ((b <= a && a <= c) || (c <= a && a <= b)) return a; else return c; } bool ispalindrom(string s) { transform(s.begin(), s.end(), s.begin(), ::tolower); return (s[0] == s[s.size() - 1] ? 1 : 0); } bool as_second(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.second < b.second); } bool ds_first(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.first > b.first); } bool ds_second(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second > b.second; } template <typename KeyType, typename ValueType> std::pair<KeyType, ValueType> get_max(const std::map<KeyType, ValueType> &x) { using pairtype = std::pair<KeyType, ValueType>; return *std::max_element(x.begin(), x.end(), [](const pairtype &p1, const pairtype &p2) { return p1.second < p2.second; }); } static inline bool isvowel(char ch) { static const std::string vowels( aeiouEAIOU ); return vowels.end() != std::find(vowels.begin(), vowels.end(), ch); } template <typename Map> bool map_compare(Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); } long long nCr(long long n, long long k) { long long ans = 1; k = k > n - k ? n - k : k; long long j = 1; for (; j <= k; j++, n--) { if (n % j == 0) { ans *= n / j; } else if (ans % j == 0) { ans = ans / j * n; } else { ans = (ans * n) / j; } } return ans; } string str, str1, str2, str3, str4, str5, str6, str7; char ch, ch1, ch2, ch3; bool flag = true, flag1 = true, flag2 = true; long long a, b, c, d, e, f, g, h, l, i, j, k, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, f1 = 0, f2 = 0, cnt = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, sum = 0, sum1 = 0, sum2 = 0, sum3 = 0, ma = 0, ma1 = 0, mi = INT_MAX, mi1 = INT_MAX, temp = 0, temp1 = 0, temp2 = 0; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) { cin >> v[i]; sum += v[i]; } if (sum != 0) { cout << YES n1 n ; cout << 1 << << n << endl; } else { p = 0; for (long long i = 0; i < n; i++) { if (v[i] != 0) { p = i + 1; break; } } if (p == 0) { return cout << NO n << endl, 0; } else { cout << YES n2 n ; cout << 1 << << p << endl; cout << p + 1 << << n << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000009; const int N = 100005; const double PI = 4 * atan(1); const long long MAX = 9223372036854775807; int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } bool isprime(long long u) { for (long long i = 2; i <= (int)sqrt(u); i++) { if (u % i == 0) return 0; } return 1; } long long mod(long long to_mod) { to_mod %= MOD; while (to_mod < 0) to_mod += MOD; return to_mod % MOD; } long long moduloMultiplication(long long a, long long b, long long mod) { long long res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; a = (2 * a) % mod; b >>= 1; } return res; } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { ; if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long n, m, x, y, res, t, k, ans = 0, b, q, z, a, l; int main() { ios::sync_with_stdio(0); cin >> n >> m >> k; res = n / k; ans = n % k; m -= ans; a = res * (k - 1); if (a >= m) { ans += m; ans = mod(ans); cout << ans; return 0; } b = m - a; y = (power(2, b + 1, MOD) - 2); ans += moduloMultiplication(y, k, MOD); ans = mod(ans); ans += moduloMultiplication(k - 1, res - b, MOD); ans = mod(ans); cout << ans << endl; return 0; }
#pragma GCC optimize( Ofast ) #include bits/stdc++.h typedef long long ll; using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { // #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << ( << d.first << , << d.second << ) ; } sim dor(rge<c> d) { *this << [ ; for (auto it = d.b; it != d.e; ++it) *this << , + 2 * (it == d.b) << *it; ris << ] ; } // sim dor(const c&) { ris; } }; #ifdef ASJKD } #endif #define imie(...) [ << #__VA_ARGS__ : << (__VA_ARGS__) << ] #define read(x) for(auto &i : (x)){cin>>i;} #define all(x) begin(x), end(x) #define pb push_back #define pii pair<int,int> #define maxn 100005 ll mod = 1e9+7; int INF = 1e9+1e8+1e7; ll INF64 = 1e18; long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1){ res = res * a % m; } a = a * a % m; b >>= 1; } return res; } vector<vector<int>> pfs(maxn); vector<bool> isprime(maxn, 1); void sieve(int mx){ for(int i=2;i<mx;i++){ if(isprime[i]){ for(int j=i;j<mx;j+=i){ isprime[j]=0; pfs[j].pb(i); } } } } void test_case(int tnum){ int n,q; cin>>n>>q; vector<int> a(n); read(a); vector<int> go(n+1, n); vector<int> last(maxn, n); for(int i=n-1;i>=0;i--){ for(int j : pfs[a[i]]){ go[i]=min(go[i], last[j]); last[j] = min(last[j], i); } go[i] = min(go[i], go[i+1]); } // debug() << imie(go); vector<vector<int>> lift(20, vector<int>(n+1, n)); for(int i=0;i<n;i++){ lift[0][i] = go[i]; } for(int i=1;i<20;i++){ for(int j=0;j<n;j++){ lift[i][j]=lift[i-1][lift[i-1][j]]; } } for(int rep=0;rep<q;rep++){ int l, r; cin>>l>>r; --l,--r; int cur=l; int ans =0; while(true){ if(go[cur] > r){ ans++; break; } for(int i=19;i>=0;i--){ if(lift[i][cur]<=r){ ans += 1<<(i); cur = lift[i][cur]; break; } } } cout<<ans<< n ; } } int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int t=1; sieve(maxn); // cin>>t; for(int test=1;test<=t;test++){ test_case(test); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string table; string hand; getline(cin, table, n ); transform(table.begin(), table.end(), table.begin(), ::tolower); getline(cin, hand, n ); transform(hand.begin(), hand.end(), hand.begin(), ::tolower); if (hand.find(table[0]) != -1 || hand.find(table[1]) != -1) { cout << YES ; } else cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 820; typedef struct node { int l, r, x; } node; node a[N * N]; bool cmp(node x, node y) { return x.x > y.x; } int n, m, k, t, tot = 0; int v[N], p[N], ans[N]; int main() { cin >> n; n = n * 2; for (int i = 1; i <= n; i++) for (int j = 1; j <= i - 1; j++) { scanf( %d , &a[++tot].x); a[tot].l = i; a[tot].r = j; } sort(a + 1, a + tot + 1, cmp); for (int i = 1; i <= tot; i++) { int l = a[i].l; int r = a[i].r; if (v[l] || v[r]) continue; v[l] = v[r] = 1; ans[l] = r; ans[r] = l; } for (int i = 1; i <= n; i++) printf( %d , ans[i]); cout << endl; }
#include <bits/stdc++.h> const long long N = 1e6 + 10; const long long mod = 1e9 + 7; long long dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; using namespace std; struct node { long long x, y, id; node() {} node(long long x, long long y, long long i) : x(x), y(y), id(i) {} }; long long ans[N]; stack<node> q; long long ok(node a, node b) { long long x = a.x + b.x, y = a.y + b.y; if (x * x + y * y <= 1e12) return 1; x = a.x - b.x; y = a.y - b.y; if (x * x + y * y <= 1e12) return 2; return 0; } long long be[N], dp[N]; long long dfs(long long x) { if (dp[x] != -2) return dp[x]; if (be[x] == 0) return dp[x] = ans[x]; dp[x] = ans[x] * dfs(be[x]); return dp[x]; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long x, y; for (long long i = 1; i <= n; i++) cin >> x >> y, q.push(node(x, y, i)); if (n == 1) return cout << 1, 0; long long cc = n; while (q.size() > 2) { node t1 = q.top(); q.pop(); node t2 = q.top(); q.pop(); node t3 = q.top(); q.pop(); long long t; if (t = ok(t1, t2)) { if (t == 1) { ans[t1.id] = 1, ans[t2.id] = 1, q.push(node(t1.x + t2.x, t1.y + t2.y, ++cc)), q.push(t3); be[t1.id] = be[t2.id] = cc; } else { ans[t1.id] = 1, ans[t2.id] = -1, q.push(node(t1.x - t2.x, t1.y - t2.y, ++cc)), q.push(t3); be[t1.id] = be[t2.id] = cc; } } else if (t = ok(t1, t3)) { if (t == 1) { ans[t1.id] = 1, ans[t3.id] = 1, q.push(node(t1.x + t3.x, t1.y + t3.y, ++cc)), q.push(t2); be[t1.id] = be[t3.id] = cc; } else { ans[t1.id] = 1, ans[t3.id] = -1, q.push(node(t1.x - t3.x, t1.y - t3.y, ++cc)), q.push(t2); be[t1.id] = be[t3.id] = cc; } } else if (t = ok(t3, t2)) { if (t == 1) { ans[t3.id] = 1, ans[t2.id] = 1, q.push(node(t3.x + t2.x, t3.y + t2.y, ++cc)), q.push(t1); be[t3.id] = be[t2.id] = cc; } else { ans[t3.id] = 1, ans[t2.id] = -1, q.push(node(t3.x - t2.x, t3.y - t2.y, ++cc)), q.push(t1); be[t3.id] = be[t2.id] = cc; } } } { node t1 = q.top(); q.pop(); node t2 = q.top(); long long xx = t1.x + t2.x, yy = t1.y + t2.y; if (xx * xx + yy * yy <= 2.25e12) ans[t1.id] = ans[t2.id] = 1; else ans[t1.id] = 1, ans[t2.id] = -1; } for (long long i = 1; i <= cc; i++) dp[i] = -2; for (long long i = 1; i <= n; i++) if (dp[i] == -2) dp[i] = dfs(i); for (long long i = 1; i <= n; i++) cout << dp[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long NR = 5e5 + 100; const long long oo = 2e9 + 100; const long long MOD = 998244353; long long n; string s; long long a[NR]; long long dp[5]; signed main() { ios::sync_with_stdio(0); cin.tie(0); long long i; cin >> n; cin >> s; for (i = 0; i < n; ++i) { cin >> a[i]; } for (i = 0; i < s.size(); ++i) { if (s[i] == h ) { dp[0] += a[i]; } if (s[i] == a ) { dp[1] = min(dp[1] + a[i], dp[0]); } if (s[i] == r ) { dp[2] = min(a[i] + dp[2], dp[1]); } if (s[i] == d ) { dp[3] = min(a[i] + dp[3], dp[2]); } } cout << dp[3] << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long A[100010]; long long B[100010]; priority_queue<long long> pq; set<int> S; int P[100010]; long long ans[100010]; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %I64d , &A[i]); B[0] = 0LL; for (int i = 1; i <= n; i++) B[i] = B[i - 1] + A[i]; for (int i = 0; i < n; i++) scanf( %d , &P[i]); ans[n - 1] = 0; for (int i = 1; i <= n; i++) S.insert(i); S.insert(0); S.insert(n + 1); set<int>::iterator L, R; long long best = 0; for (int i = n - 2; i >= 0; i--) { int p = P[i + 1]; S.erase(p); R = S.lower_bound(p); L = R; L--; long long v = B[(*R) - 1] - B[*L]; best = max(best, v); ans[i] = best; } for (int i = 0; i < n; i++) printf( %I64d n , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; template <class X, class Y> bool minimize(X &x, const Y &y) { X eps = 1e-9; if (x > y + eps) { x = y; return true; } else return false; } template <class X, class Y> bool maximize(X &x, const Y &y) { X eps = 1e-9; if (x + eps < y) { x = y; return true; } else return false; } template <class T> T Abs(const T &x) { return (x < 0 ? -x : x); } char s[300300]; int n; bool ok(int l, int r) { for (int k = (1), _b = ((r - l + 1) / 2); k <= _b; k++) for (int i = (l + k), _b = (r - k); i <= _b; i++) if (s[i] == s[i - k] && s[i] == s[i + k]) return true; return false; } void process(void) { scanf( %s , s + 1); n = strlen(s + 1); long long res = 0; for (int i = (1), _b = (n); i <= _b; i++) { if (i + 11 <= n) res += n - (i + 11) + 1; for (int j = (i), _b = (n); j <= _b; j++) { if (j > i + 10) break; if (ok(i, j)) res++; } } cout << res << endl; } int main(void) { process(); return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; vector<long long> pref(n + 1); for (long long i = 0; i < n; i++) pref[i + 1] = pref[i] + a[i]; long long ans = lower_bound(pref.begin(), pref.end(), (pref[n] + 2 - 1) / 2) - pref.begin(); cout << ans << n ; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::string s; std::cin >> s; std::string_view view = s; size_t n = 0; while (!view.empty()) { const size_t id = view.find( > ) + 1; const auto tag = view.substr(0, id); view.remove_prefix(id); if (tag[1] == / ) { n -= 2; } std::cout << std::string(n, ) << tag << n ; if (tag[1] != / ) { n += 2; } } }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; if (n > m) { swap(n, m); } if (n > 3) { cout << ((n * m / 2) * 2); } else { if (n == 3) { cout << m * n / 2 * 2; return 0; } else { if (n == 2) { if (m == 2) { cout << 0; return 0; } if (m == 3) { cout << 4; return 0; } if (m == 7) { cout << 12; return 0; } cout << n * m; } else { long long x = m % 6; long long c = m / 6 * 6; if (x == 4) { c += 2; } if (x == 5) { c += 4; } cout << c; return 0; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int a[100010], b[100010]; int main() { int n, k; scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) { scanf( %1d , &a[i]); b[i] = b[i - 1] + a[i]; } for (int i = 1; i + k - 1 <= n; i++) { int sum = b[i - 1] + b[n] - b[i + k - 1]; if (sum == 0 || sum == (i - 1 + n - (i + k - 1))) { puts( tokitsukaze ); return 0; } } if (2 * k >= n && k != 1) { bool flag = true; for (int i = 2; i + k - 1 < n; i++) { int sum1 = b[i - 1]; int sum2 = b[n] - b[i + k - 1]; if (sum1 == 0 && sum2 == (n - (i + k - 1))) { continue; } if (sum2 == 0 && sum1 == (i - 1)) { continue; } flag = false; } if (flag) { puts( quailty ); return 0; } } puts( once again ); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, j, s; int k[100000], a[100000], b[100000]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> k[i]; for (int i = 0; i < m; i++) { int x, y, z; cin >> x >> y; if (x == 1) { cin >> z; k[y - 1] = z; a[y - 1] = s; } if (x == 2) s += y; if (x == 3) { b[j] = k[y - 1] + s - a[y - 1]; j++; } } for (int i = 0; i < j; i++) cout << b[i] << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; char s[N]; int a[N], sta[N], tp; int main() { scanf( %s , s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; ++i) if (s[i] == 0 ) { if (tp) a[sta[tp--]] = 1; } else sta[++tp] = i; for (int i = 1; i <= n; ++i) printf( %d , a[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, n1, n2; cin >> n >> n1 >> n2; int A[n], i; for (i = 0; i < n; ++i) cin >> A[i]; sort(A, A + n, std::greater<int>()); int a, b; if (n1 < n2) { a = n1; b = n2; } else { a = n2; b = n1; } double ans1 = 0, ans2 = 0; for (i = 0; i < a; ++i) ans1 += A[i]; ans1 = ans1 / (double)a; for (; i < a + b; ++i) ans2 += A[i]; ans2 = ans2 / (double)b; printf( %.8lf n , ans1 + ans2); }
#include <bits/stdc++.h> using namespace std; int main() { string x; cin >> x; long long step[101]; step[0] = 1; for (int i = 1; i < 101; i++) step[i] = (step[i - 1] * 2) % ((int)1e9 + 7); long long ans = 0; for (int i = 0; i < x.size(); i++) if (x[i] == 1 ) ans = (ans + step[x.size() - i - 1]) % ((int)1e9 + 7); for (int i = 0; i < x.size() - 1; i++) ans = (ans * 2) % ((int)1e9 + 7); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /STACK:1024000000,1024000000 ) const int INF = 0x3f3f3f; const long long mod = 1e9 + 7; const int MAXN = 1055; const double eps = 1e-6; const double pi = acos(-1.0); using namespace std; int main() { long long n, t; cin >> n >> t; double x = 1.000000011; double ans = 1.0; for (long long i = t; i; i = i >> 1, x *= x) if (i & 1) ans *= x; printf( %.6f n , ans * n); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast,inline,unroll-loops,fast-math ) #pragma GCC optimize( O3 ) using namespace std; using namespace std::chrono; long long mod_exp(long long x, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = (res * x) % 1000000007; x = (x * x) % 1000000007; n >>= 1; } return res; } void inout() { freopen( in.txt , r , stdin); freopen( out.txt , w , stdout); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long k; cin >> k; long long x = 9; for (long long i = 1;; i++) { if (k <= (i * x)) { x = (x / 9) + (--k / i); while (++k % i) x /= 10; cout << x % 10; exit(0); } k -= (i * x); x *= 10; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; map<int, int> uchs; for (int i = 0; i < n; i++) { int x; cin >> x; if (uchs.find(x) != uchs.end()) { (uchs.at(x))++; } else uchs.insert(pair<int, int>(x, 1)); } cin >> m; vector<int> films; films.reserve(m); int max = 0; for (int j = 0; j < m; j++) { int x; cin >> x; if (uchs.find(x) != uchs.end()) { if (uchs.at(x) > max) { films.clear(); max = uchs.at(x); films.push_back(j); } else if (uchs.at(x) == max) { films.push_back(j); } } else if (max == 0) { films.push_back(j); } } int kinco = films[0] + 1; int newmax = 0; int r = 0; for (int j = 0; j < m; j++) { int x; cin >> x; if (films[r] == j) { r++; if (uchs.find(x) != uchs.end()) { if (uchs.find(x)->second > newmax) { kinco = j + 1; newmax = uchs.find(x)->second; } } if (r == films.size()) break; } } cout << kinco; return 0; }
#include <bits/stdc++.h> using namespace std; int T, n, head[300005], cnt, in[300005], ans, rt; struct edge { int to, nxt; } e[300005 << 1]; void add(int u, int v) { e[++cnt] = (edge){v, head[u]}; head[u] = cnt; } void dfs(int u, int fa, int tot) { if (tot > ans) ans = tot, rt = u; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (v == fa) continue; dfs(v, u, tot + in[v]); } } int main() { scanf( %d , &T); while (T--) { scanf( %d , &n); cnt = 0; for (int i = 1; i <= n; i++) head[i] = 0, in[i] = -1; for (int i = 1, u, v; i < n; i++) { scanf( %d%d , &u, &v); add(u, v); add(v, u); in[u]++; in[v]++; } ans = -1; dfs(1, 0, in[1]); ans = -1; dfs(rt, 0, in[rt]); printf( %d n , ans + 2); } }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long sum = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) { sum = (sum << 1) + (sum << 3) + (c ^ 48); c = getchar(); } return sum; } long long const maxn = 1e5 + 5; long long const p = 1e9 + 7; long long n, q, a[maxn]; long long father[maxn], Dep[maxn], size[maxn], son[maxn], top[maxn], seg[maxn], rev[maxn << 2]; long long sum_seg[maxn << 2]; long long f[maxn], sum[maxn]; struct Edge { long long next, to; } E[maxn << 1]; long long cnt, H[maxn]; inline void AADDDD(long long a, long long b) { E[++cnt] = (Edge){H[a], b}; H[a] = cnt; } void dfs1(long long x, long long fa) { Dep[x] = Dep[fa] + 1; father[x] = fa; size[x] = 1; for (long long i = H[x]; i; i = E[i].next) { long long y = E[i].to; if (y == fa) continue; dfs1(y, x); size[x] += size[y]; if (size[y] > size[son[x]]) son[x] = y; } } void dfs2(long long x, long long fa) { if (son[x]) { seg[son[x]] = ++seg[0]; rev[seg[0]] = son[x]; top[son[x]] = top[x]; dfs2(son[x], x); } for (long long i = H[x]; i; i = E[i].next) { long long y = E[i].to; if (!top[y]) { seg[y] = ++seg[0]; rev[seg[0]] = y; top[y] = y; dfs2(y, x); } } } void change(long long k, long long l, long long r, long long x, long long v) { if (l == r && l == x) { sum_seg[k] += v; return; } if (r < x || l > x) return; long long mid = l + r >> 1; change(k << 1, l, mid, x, v); change(k << 1 | 1, mid + 1, r, x, v); sum_seg[k] = sum_seg[k << 1] + sum_seg[k << 1 | 1]; } long long query(long long k, long long l, long long r, long long x, long long y) { if (l >= x && r <= y) return sum_seg[k]; if (r < x || l > y) return 0; long long mid = l + r >> 1; return query(k << 1, l, mid, x, y) + query(k << 1 | 1, mid + 1, r, x, y); } inline long long ask(long long x, long long y) { long long res = 0; while (top[x] != top[y]) { if (Dep[top[x]] < Dep[top[y]]) swap(x, y); res += query(1, 1, seg[0], seg[top[x]], seg[x]); x = father[top[x]]; } if (Dep[x] > Dep[y]) swap(x, y); res += query(1, 1, seg[0], seg[x], seg[y]); return res; } signed main() { n = read(), q = read(); for (long long i = 1; i < n; i++) { long long x = read(), y = read(); AADDDD(x, y), AADDDD(y, x); } dfs1(1, 0); seg[0] = seg[1] = 1; top[1] = 1, rev[1] = 1; dfs2(1, 0); while (q--) { long long k = read(), m = read(), r = read(); for (long long i = 1; i <= k; i++) { a[i] = read(); change(1, 1, seg[0], seg[a[i]], 1); } for (long long i = 1; i <= k; i++) sum[i] = ask(a[i], r) - 1; bool wujie = false; for (long long i = 1; i <= sum[i]; i++) if (sum[i] >= m) wujie = true; sort(sum + 1, sum + k + 1); for (long long i = 1; i <= k; i++) f[i] = 0; f[0] = 1; for (long long i = 1; i <= k; i++) for (long long j = min(i, m); j >= 0; j--) { if (j <= sum[i]) f[j] = 0; else f[j] = (f[j - 1] + f[j] * (j - sum[i]) % p) % p; } long long ans = 0; for (long long i = 1; i <= m; i++) ans = (ans + f[i]) % p; if (wujie) printf( 0 n ); else printf( %lld n , ans); for (long long i = 1; i <= k; i++) change(1, 1, seg[0], seg[a[i]], -1); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MX = 1e5 + 10; int n, a[MX], out[MX], mn[MX]; int bs(int L, int R, int val) { if (R - L == 1) return L; int mid = (R + L) / 2; if (val <= mn[mid]) return bs(L, mid, val); else return bs(mid, R, val); } int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; mn[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; --i) mn[i] = min(mn[i + 1], a[i]); for (int i = n - 1; i >= 0; --i) { if (mn[i] == a[i]) out[i] = -1; else { int x = bs(i + 1, n, a[i]); out[i] = x - i - 1; } } for (int i = 0; i < n; ++i) cout << out[i] << ; }
#include <bits/stdc++.h> int main() { int i, j, l, n, k, good; static int a[6][1001], dp0[1001], dp1[1001], pp[6][1001]; scanf( %d%d , &n, &k); for (i = 1; i <= k; i++) for (j = 1; j <= n; j++) { scanf( %d , &a[i][j]); if (i == 1) pp[1][a[i][j]] = j; else { a[i][j] = pp[1][a[i][j]]; pp[i][a[i][j]] = j; } } dp0[1] = 0; dp1[1] = 1; for (i = 2; i <= n; i++) { dp0[i] = dp0[i - 1] > dp1[i - 1] ? dp0[i - 1] : dp1[i - 1]; dp1[i] = 1; for (j = 0; j < i; j++) { good = 1; for (l = 2; l <= k; l++) if (pp[l][j] > pp[l][i]) { good = 0; break; } if (good && dp1[i] < dp1[j] + 1) dp1[i] = dp1[j] + 1; } } printf( %d n , dp0[n] > dp1[n] ? dp0[n] : dp1[n]); return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long dx[] = {-1, 0, 1, 0}; long long dy[] = {0, -1, 0, 1}; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; vector<pair<long long, long long> > v(m); for (long long i = 0; i < m; i++) { cin >> v[i].second >> v[i].first; } sort(v.rbegin(), v.rend()); long long ans = v[0].first; for (long long k = 2; k <= m; k++) { if (max((k * (k - 1)) / 2 + 1, k * (k / 2)) <= n) ans += v[k - 1].first; else break; } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 3; const long long INFF = (long long)1e18; int cnt[N]; bool ck[N]; int main() { for (int i = 2; i < N; i++) { if (ck[i]) continue; for (int j = i; j < N; j += i) ck[j] = true; cnt[i]++; if (i <= 1000) { cnt[i * i]--; } } cnt[1] = 1; for (int i = 1; i < N; i++) cnt[i] += cnt[i - 1]; int t; scanf( %d , &t); while (t--) { int n; scanf( %d , &n); printf( %d n , cnt[n]); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string b; cin >> b; if (b.length() < 4) { cout << b << n ; continue; } cout << b[0]; for (int i = 1; i < b.length(); i += 2) { cout << b[i]; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { string s; int n, s2 = 0, ans = 0, s1 = 0; cin >> s; n = s.size() - 1; for (int i = n; i >= 0; i--) if (s[i] == 1 ) { s2 = i; break; } for (int i = 0; i <= n; i++) if (s[i] == 1 ) { s1 = i; break; } for (int i = 0; i <= n; i++) { if (i > s1 && i < s2 && s[i] == 0 ) ans++; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long infinity = 1000000000000000000; const int inf = 1e9 + 5; bool do_debug = false; const int maxn = 3e5 + 5, maxjump = 19; int n_nodes, n_edges, nq; vector<int> adj[maxn]; int depth[maxn] = {0}; long long inverse_dp[maxn] = {0}, subtree_dp[maxn] = {0}, root_dp[maxn] = {0}; int vals[maxn]; vector<array<int, 3>> edges; unordered_map<int, int> edge_wt[maxn]; int par[maxn]; struct lca { int n, tl; vector<int> t; vector<int> first; vector<int> euler; vector<int> dv; void init(int N) { n = N; euler.clear(); euler.reserve(2 * n); first.clear(); first.resize(n); dv.clear(); dv.resize(n); dv[0] = 0; dfs(0, 0); tl = euler.size(); t.clear(); t.resize(tl << 2, -1); build(1, 0, tl - 1); } int get_lca(int u, int v) { int l = first[u], r = first[v]; if (l > r) swap(l, r); return query(1, 0, tl - 1, l, r); } void dfs(int node, int parent) { first[node] = euler.size(); euler.push_back(node); for (int next : adj[node]) { if (next == parent) continue; dv[next] = dv[node] + 1; dfs(next, node); euler.push_back(node); } } int task(int child1, int child2) { if (child1 == -1) return child2; if (child2 == -1) return child1; if (dv[child1] < dv[child2]) { return child1; } else { return child2; } } void build(int node, int l, int r) { if (l == r) { t[node] = euler[l]; } else { int mid = (l + r) >> 1; build(node << 1, l, mid); build((node << 1) + 1, mid + 1, r); t[node] = task(t[node << 1], t[(node << 1) + 1]); } } int query(int node, int l, int r, int lq, int rq) { if (lq > rq) { return -1; } else if (lq <= l && rq >= r) { return t[node]; } else { int mid = (l + r) >> 1; return task(query(node << 1, l, mid, lq, min(rq, mid)), query((node << 1) + 1, mid + 1, r, max(lq, mid + 1), rq)); } } }; lca lca; struct binlift { int n, jump; vector<vector<int>> parent_table; vector<int> logtable; vector<vector<long long>> data; void init(int N, int JUMP) { n = N; jump = JUMP; parent_table = vector<vector<int>>(n, vector<int>(jump, -1)); data = vector<vector<long long>>(n, vector<long long>(jump, 0)); logtable = vector<int>(n + 1, 0); for (int i = 2; i < n + 1; i++) { logtable[i] = logtable[i >> 1] + 1; } } void add_node(int v, int p) { parent_table[v][0] = v; data[v][0] = subtree_dp[v] - inverse_dp[v] - edge_wt[v][par[v]]; parent_table[v][1] = p; data[v][1] = data[v][0] + subtree_dp[p] - inverse_dp[p] - edge_wt[p][par[p]]; } void build() { for (int j = 2; j < jump; j++) { for (int v = 0; v < n; v++) { if (parent_table[v][j - 1] != -1 && parent_table[parent_table[v][j - 1]][1] != -1) { parent_table[v][j] = parent_table[parent_table[parent_table[v][j - 1]][1]][j - 1]; data[v][j] = data[v][j - 1] + data[parent_table[parent_table[v][j - 1]][1]][j - 1]; } else { parent_table[v][j] = -1; } } } } long long lift(int v, int dist) { if (dist == 0) { return 0; } int opt = logtable[dist]; if (dist == (1 << opt)) return data[v][opt]; return data[v][opt] + lift(parent_table[parent_table[v][opt]][1], dist - (1 << opt)); } int lift_ancestor(int v, int dist) { if (dist == 1) { return v; } int opt = logtable[dist]; if (dist == (1 << opt)) { return parent_table[v][opt]; } return lift_ancestor(parent_table[parent_table[v][opt]][1], dist - (1 << opt)); } }; binlift b; void terraform(int v, int p, long long temp) { for (int u : adj[v]) { if (u == p) continue; long long T = temp - inverse_dp[u]; root_dp[u] += max(0LL, T - 2 * edge_wt[u][v]); terraform(u, v, root_dp[u]); } } void dfs(int v, int p) { subtree_dp[v] = vals[v]; for (int u : adj[v]) { if (u == p) continue; par[u] = v; depth[u] = depth[v] + 1; dfs(u, v); inverse_dp[u] = max(0LL, subtree_dp[u] - 2 * edge_wt[u][v]); subtree_dp[v] += inverse_dp[u]; } root_dp[v] = subtree_dp[v]; } void add_dfs(int v, int p) { for (int u : adj[v]) { if (u == p) continue; b.add_node(u, v); add_dfs(u, v); } } void solve() { cin >> n_nodes >> nq; n_edges = n_nodes - 1; for (int i = 0; i < n_nodes; i++) { cin >> vals[i]; } for (int i = 0; i < n_edges; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; adj[u].push_back(v); adj[v].push_back(u); edges.push_back({u, v, w}); edge_wt[u][v] = w; edge_wt[v][u] = w; } b.init(n_nodes, maxjump); dfs(0, 0); terraform(0, 0, root_dp[0]); add_dfs(0, 0); b.build(); lca.init(n_nodes); for (int q = 0; q < nq; q++) { int u, v; cin >> u >> v; u--; v--; int LCA = lca.get_lca(u, v); long long ans = 0; if (u != LCA) { int inbw = depth[u] - depth[LCA]; ans += b.lift(u, inbw); } if (v != LCA) { int inbw = depth[v] - depth[LCA]; ans += b.lift(v, inbw); } ans += root_dp[LCA]; cout << ans << n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int q = 1; while (q-- > 0) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; queue<int> que; vector<bool> init, goal; vector<bool> visited; vector<vector<int>> nodes; void calculate(int root, bool flip1, bool flip2) { const vector<int> &children = nodes[root]; visited[root] = true; if (init[root] ^ flip1 != goal[root]) { que.push(root + 1); for (auto c : children) { if (visited[c]) continue; calculate(c, flip2, !flip1); } } else { for (auto c : children) { if (visited[c]) continue; calculate(c, flip2, flip1); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; nodes.resize(n); for (int i = 0; i < n - 1; ++i) { int p, c; cin >> c >> p; nodes[p - 1].push_back(c - 1); nodes[c - 1].push_back(p - 1); } init.resize(n); for (int i = 0; i < n; ++i) { int b; cin >> b; init[i] = b; } goal.resize(n); for (int i = 0; i < n; ++i) { int b; cin >> b; goal[i] = b; } visited.resize(n, false); calculate(0, false, false); cout << que.size() << endl; while (!que.empty()) { cout << que.front() << endl; que.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } int n, m; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; vector<vector<int> > board(n, vector<int>(m)); for (auto& it : board) for (auto& jt : it) cin >> jt; long long ans = 0; for (int j = 0; j < m; j++) { map<int, int> mp; for (int i = 0; i < n; i++) { if (board[i][j] % m != (j + 1) % m) continue; if (board[i][j] > n * m) continue; int orgin = m * i + j + 1; int dif = orgin - board[i][j]; if (dif < 0) dif += n * m; dif /= m; mp[dif]++; } int minVal = n; for (auto& it : mp) { minVal = min(minVal, it.first + n - it.second); } ans += minVal; } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int k, n; cin >> n >> k; if (n == 2) return cout << 2 << << 1 << << 2 << endl, 0; int ans1, ans2; int l = 1, r = n; while (l < r) { int mid = (l + r) / 2; cout << 1 << << mid << << mid + 1 << endl; string s; cin >> s; if (s == NIE ) l = mid + 1; else r = mid; } ans1 = l; if (ans1 != n) { l = ans1 + 1, r = n; bool good = 0; while (l < r) { int mid = (l + r) / 2; cout << 1 << << mid << << mid + 1 << endl; string s; cin >> s; if (s == NIE ) l = mid + 1; else { r = mid; } } ans2 = r; cout << 1 << << ans2 << << ans1 << endl; string s; cin >> s; if (s != NIE ) return cout << 2 << << ans1 << << ans2 << endl, 0; } l = 1, r = ans1 - 1; while (l < r) { int mid = (l + r) / 2; cout << 1 << << mid << << mid + 1 << endl; string s; cin >> s; if (s == NIE ) l = mid + 1; else { r = mid; } } ans2 = r; cout << 2 << << ans1 << << ans2 << endl; }
#include <bits/stdc++.h> using namespace std; int const sz = 2 * 1e5; int const oo = 1e9; bool comp(int i, int j) { return i < j; } long long num[1000000], kk[101]; void fill(int n, int m) { long long arr[4] = {0, 1, 2, 3}; int l = 0; num[0] = 1; num[1] = 2; num[2] = 3; for (int i = 3; i < n; i++) { arr[(l % 3) + 1] = ((arr[1] + arr[2] + arr[3]) % m) + 1; num[i] = arr[(l++ % 3) + 1]; if (num[i] < 101) kk[num[i]]++; } } long long check(long long m, int k) { long long a = 1; long long sum = 0; while (m / a > 0) { sum = sum + (m / a); a *= k; } return sum; } void solve() { long long n, k; cin >> n >> k; long long l = 0, r = n; while (l < r - 1) { long long m = (l + r) / 2; if (check(m, k) >= n) r = m; else l = m; } cout << r; } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; int f[100005], s[100005]; int main() { int n, k, inp; scanf( %d %d , &n, &k); for (int i = 1; i < k + 1; i++) { scanf( %d , &inp); s[inp] = i; if (!f[inp]) f[inp] = i; } int ans = 0; for (int i = 1; i < n + 1; i++) { if (!f[i]) { ans += 3; if (i == 1 || i == n) ans--; continue; } int b = i - 1; if (b >= 1 && s[b] < f[i]) ans++; b = i + 1; if (b <= n && s[b] < f[i]) ans++; } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); long long t; cin >> t; while (t--) { long long n, m, k; cin >> n >> m >> k; long long cnt = n / k; if (m <= cnt) cout << m << n ; else { long long cntt = (m - cnt + k - 2) / (k - 1); cout << (cnt - cntt) << n ; } } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:66777216 ) using namespace std; const int N = 1001002; int a, b, c, d, i, j, n, m, k, tot, now, last; int nx[N][26], sib[N], pr[N], fir[N]; int cnt[N], en[N]; char str[502]; int num[2][N]; int dp[2][2002][2002]; vector<int> lev[503]; int dfs(int v, int h = 0) { lev[h].push_back(v); for (int _n((26) - 1), i(0); i <= _n; i++) { if (nx[v][i] != -1) cnt[v] += dfs(nx[v][i], h + 1); } return cnt[v]; } inline int g(int v, int c) { if (c < 0) return -1000000000; if (v == -1 && !c) return 0; if (v == -1) return -1000000000; if (!c) return 0; return dp[last][num[last][v]][c]; } inline int f(int v, int c) { if (c < 0) return -1000000000; if (v == -1 && !c) return 0; if (v == -1) return -1000000000; if (!c) return 0; return dp[now][num[now][v]][c]; } int rec(int v, int c) { if (c < 0) return -1000000000; if (v == -1 && !c) return 0; if (v == -1) return -1000000000; if (!c) return 0; int ans = 0, fr = cnt[v] - en[v]; for (int i = (min(cnt[v], c)), _b = (0); i >= _b; i--) { int lf = c - i; if (lf && sib[v] == -1) break; int v1 = g(fir[v], min(fr, i)); if (v1 < 0) continue; int v2 = f(sib[v], lf); if (v2 < 0) continue; ans = max(ans, i * (i - 1) / 2 + v1 + v2); } return dp[now][num[now][v]][c] = ans; } int main() { memset(nx, -1, sizeof(nx)); pr[0] = -1; k = 1; scanf( %d%d , &n, &tot); for (int _n((n)-1), i(0); i <= _n; i++) { scanf( %s , str); m = (int)strlen(str); a = 0; for (int _n((m)-1), i(0); i <= _n; i++) { if (nx[a][str[i] - a ] == -1) { nx[a][str[i] - a ] = k++; pr[k - 1] = a; } a = nx[a][str[i] - a ]; } ++cnt[a]; ++en[a]; } for (int _n((k)-1), i(0); i <= _n; i++) { fir[i] = -1; for (int j = (25), _b = (0); j >= _b; j--) { if (nx[i][j] == -1) continue; sib[nx[i][j]] = fir[i]; fir[i] = nx[i][j]; } } dfs(0); memset((dp), 0, sizeof(dp)); now = 0, last = 1; for (int h = (500), _b = (1); h >= _b; h--) { if (lev[h].empty()) continue; now ^= 1; last ^= 1; for (int _n(((int)((lev[h]).size())) - 1), i(0); i <= _n; i++) num[now][lev[h][i]] = i; for (int i = ((int)((lev[h]).size()) - 1), _b = (0); i >= _b; i--) { int v = lev[h][i], p = pr[v]; for (int _n((k + 1) - 1), j(0); j <= _n; j++) { if (j > cnt[p]) break; rec(v, j); } } } int ans = dp[now][0][tot]; printf( %d n , ans); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string n; int k; cin >> n >> k; int ans = 0; int p = 0; for (auto it = n.rbegin(); p < k && it != n.rend();) { if (*it != 0 ) { ++ans; n.erase((++it).base()); } else { ++it; ++p; } } for (auto it = n.begin(); it != n.end() && it + 1 != n.end(); ++it) { if (*it == 0 ) { ++ans; } else { break; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) using namespace std; using ll = int64_t; const size_t kBuben = 10; const int kNone = -1; struct Pair { int value, count; }; struct ByValue { inline bool operator()(const Pair& a, const Pair& b) const { return a.value < b.value; } }; struct ByCount { inline bool operator()(const Pair& a, const Pair& b) const { if (a.count != b.count) return a.count > b.count; return a.value < b.value; } }; struct Segment { Segment* l; Segment* r; array<Pair, kBuben> top; int len, middle, value_to_push; Segment(int length) { l = r = nullptr; len = length; clear(); } inline void clear() { value_to_push = kNone; top.fill(Pair{kNone, 0}); } inline void assign(int value) { clear(); top[0] = Pair{value, len}; value_to_push = value; } inline void rebuild(const Segment& a, const Segment& b) { array<Pair, 2 * kBuben> result; array<Pair, kBuben> add; size_t add_count = 0; result.fill(Pair{kNone, 0}); add.fill(Pair{kNone, 0}); copy(a.top.begin(), a.top.end(), result.begin()); sort(result.begin(), result.end(), ByValue()); for (const auto& segment : b.top) { auto it = lower_bound(result.begin(), result.end(), segment, ByValue()); if (it == result.end() || it->value != segment.value) { add[add_count++] = segment; } else { it->count += segment.count; } } copy(add.begin(), add.begin() + add_count, result.begin()); sort(result.begin(), result.end(), ByCount()); copy(result.begin(), result.begin() + kBuben, top.begin()); } }; Segment* root; inline bool push(Segment* current) { if (current->value_to_push != kNone) { current->l->assign(current->value_to_push); current->r->assign(current->value_to_push); current->value_to_push = kNone; return true; } else { return false; } } inline int get_rand(int l, int r) { return l + (rand() % (r - l + 1)); } Segment* build(int l, int r, const vector<int>& values) { Segment* current = new Segment(r - l + 1); if (l == r) { current->assign(values[l]); } else { int m = (l + r) / 2; current->middle = m; current->l = build(l, m, values); current->r = build(m + 1, r, values); current->rebuild(*current->l, *current->r); } return current; } void assign(Segment* current, int l, int r, int tl, int tr, int value) { if (l == tl && r == tr) { current->assign(value); } else { push(current); int m = current->middle; if (tl <= m) assign(current->l, l, m, tl, min(m, tr), value); if (tr > m) assign(current->r, m + 1, r, max(m + 1, tl), tr, value); current->rebuild(*current->l, *current->r); } } bool get(Segment* current, int l, int r, int tl, int tr, vector<Pair>& bests) { if (l == tl && r == tr) { for (const auto& p : current->top) { bests.push_back(p); } return false; } else { bool pushed = push(current); int m = current->middle; if (tl <= m) pushed |= get(current->l, l, m, tl, min(m, tr), bests); if (tr > m) pushed |= get(current->r, m + 1, r, max(m + 1, tl), tr, bests); if (pushed) current->rebuild(*current->l, *current->r); return pushed; } } int main() { srand(1224); ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, P; cin >> n >> m >> P; int k = 100 / P; vector<int> values(n); for (int i = 0; i < n; i++) { cin >> values[i]; } root = build(0, n - 1, values); vector<Pair> bests; vector<Pair> result; for (int i = 0; i < m; i++) { int type, l, r, id; cin >> type >> l >> r; --l, --r; if (type == 1) { cin >> id; assign(root, 0, n - 1, l, r, id); } else { bests.clear(); get(root, 0, n - 1, l, r, bests); sort(bests.begin(), bests.end(), ByValue()); result.clear(); int i = 0; while (i < bests.size()) { int j = i, cnt = 0; while (j < bests.size() && bests[i].value == bests[j].value) { cnt += bests[j].count; ++j; } result.push_back(Pair{bests[i].value, cnt}); i = j; } sort(result.begin(), result.end(), ByCount()); result.resize(k); cout << k; for (const auto& x : result) { cout << << x.value; } cout << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; namespace flyinthesky { const int MAXN = 500000 + 5; struct edge { int u, v, w; bool operator<(const edge &b) const { return w < b.w; } } ed[MAXN], ed2[MAXN]; struct whw { int id, fr, t; }; struct ask { int id; bool operator<(const ask &b) const { return ed[id].w < ed[b.id].w; } }; int n, m, q, ans[MAXN], maxw = 0, f[MAXN], sz[MAXN]; vector<whw> a[MAXN]; vector<ask> que[MAXN]; stack<pair<int, int> > s; int find(int x) { return x == f[x] ? x : find(f[x]); } void mg(int a, int b) { int x = a, y = b; if (sz[x] > sz[y]) swap(x, y); f[x] = y, sz[y] += sz[x], s.push(make_pair(x, y)); } void cc(int tms) { while (tms-- && !s.empty()) { pair<int, int> p = s.top(); s.pop(); int x = p.first, y = p.second; f[x] = x, sz[y] -= sz[x]; } } void clean() { memset(ans, 0, sizeof ans); } int solve() { cin >> n >> m; clean(); for (int i = 1; i <= m; ++i) scanf( %d%d%d , &ed[i].u, &ed[i].v, &ed[i].w), maxw = max(maxw, ed[i].w), ed2[i] = ed[i]; sort(ed2 + 1, ed2 + 1 + m); cin >> q; for (int i = 1; i <= q; ++i) { int Ki; scanf( %d , &Ki); for (int x, j = 1; j <= Ki; ++j) scanf( %d , &x), que[i].push_back((ask){x}); sort(que[i].begin(), que[i].end()); int lst = 0; for (int j = 1; j < (int)que[i].size(); ++j) { int id = que[i][j].id, lid = que[i][j - 1].id; if (ed[id].w != ed[lid].w) { a[ed[lid].w].push_back((whw){i, lst, j - 1}); lst = j; } } a[ed[que[i][(int)que[i].size() - 1].id].w].push_back( (whw){i, lst, (int)que[i].size() - 1}); } for (int i = 0; i <= n; ++i) f[i] = i, sz[i] = 1; int now = 1; for (int i = 1; i <= maxw; ++i) { for (int j = 0; j < (int)a[i].size(); ++j) { int fl = 0, tms = 0; whw &ct = a[i][j]; for (int k = ct.fr; k <= ct.t; ++k) { edge &e = ed[que[ct.id][k].id]; int x = find(e.u), y = find(e.v); if (x == y) { fl = 1; break; } else mg(x, y), ++tms; } if (fl) ans[ct.id] = 1; cc(tms); } while (ed2[now].w == i) { int x = find(ed2[now].u), y = find(ed2[now].v); if (x != y) mg(x, y); ++now; } } for (int i = 1; i <= q; ++i) printf( %s , ans[i] == 1 ? NO n : YES n ); return 0; } } // namespace flyinthesky int main() { flyinthesky::solve(); return 0; }
#include <bits/stdc++.h> using namespace std; bool cmp(long long int a, long long int b) { return b < a; } int main() { long long int n; cin >> n; long long int a[n + 1]; for (int i = 1; i <= n; i++) { scanf( %lld , &a[i]); } sort(a + 1, a + n + 1, cmp); long long int ans = 0; a[0] = 0; for (int i = 1; i <= n / 4; i++) { a[i] += a[i - 1]; } for (int i = 1; i <= n / 4; i = i * 4) { ans += a[i]; } for (int i = n / 4; i <= n; i++) { ans += a[i]; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, b, d; int a[100000 + 10]; cin >> n >> b >> d; for (int i = 0; i < n; i++) cin >> a[i]; int p = 0, emp = 0; int temp = 0; for (int i = 0; i < n; i++) { if (a[i] > b) continue; temp += a[i]; if (temp > d) { emp++; temp = 0; } } cout << emp; }
#include <bits/stdc++.h> const int N = 100000 + 10; int n; std::vector<int> adj[N]; bool flag[N]; void dfs(int a, int fa = -1) { if (adj[a].size() > 2) return; flag[a] = true; for (int i = 0; i < adj[a].size(); ++i) { int b = adj[a][i]; if (b != fa) dfs(b, a); } } int main() { scanf( %d , &n); for (int i = n - 1; i--;) { int a, b; scanf( %d%d , &a, &b); adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; ++i) if (adj[i].size() == 1) dfs(i); static int cnt[N]; for (int i = 1; i <= n; ++i) for (int j = 0; j < adj[i].size(); ++j) cnt[i] += flag[adj[i][j]]; for (int i = 1; i <= n; ++i) { if (flag[i]) continue; int cur = 0; for (int j = 0; j < adj[i].size(); ++j) { int t = adj[i][j]; if (!flag[t] && adj[t].size() - std::min(cnt[t], 2) > 1) ++cur; } if (cur > 2) return puts( No ), 0; } puts( Yes ); return 0; }
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433; const long long M = 1e9; map<long long, set<long long>> ms; long long a[200100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, m, x, t = 0, s = 0, p, r; cin >> n >> m; for (long long i = 0; i < n; i++) { cin >> a[i]; ms[a[i] % m].insert(i); } r = n / m; for (auto i : ms) { if (i.first == m - 1) break; if (i.second.size() > r) { p = i.second.size() - r; for (auto j : i.second) { p--; while ((ms[t].size() >= r || t <= i.first) && t < m - 1) t++; ms[t].insert(j); a[j] += (t - i.first); s += (t - i.first); ms[i.first].erase(j); if (p == 0) break; } } } t = 0; bool q = true; for (auto i : ms[m - 1]) { while (q && ms[t].size() >= r && t < m - 1) t++; if (t >= m - 1) break; if (q) { p = ms[t].size(); q = false; } if (p < r) { a[i] += t + 1; s += t + 1; p++; if (p == r) { q = true; t++; } } } cout << s << endl; for (long long i = 0; i < n; i++) cout << a[i] << ; }
#include <bits/stdc++.h> using namespace std; const int N = 300009; vector<long long> visited[N]; map<pair<int, int>, int> mp; set<int> s; int n, m; void dfs(int j) { set<int> q; for (auto i : s) { if (mp[{j, i}] != 1) { q.insert(i); } } for (auto p : q) { s.erase(p); } for (auto p : q) { dfs(p); } } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; mp[{x, y}] = 1; mp[{y, x}] = 1; } for (int i = 1; i <= n; i++) { s.insert(i); } int cnt = 0; while (!s.empty()) { dfs(*s.begin()); cnt++; } cout << cnt - 1 << endl; }
#include <bits/stdc++.h> using namespace std; int n, a[300300], q, p, x, y, l[300300], b[300300]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> q; for (int i = 1; i <= q; i++) { cin >> p; if (p == 1) { cin >> x >> y; a[x] = y; l[x] = i; } if (p == 2) { cin >> x; b[i] = x; } } for (int i = q - 1; i >= 0; i--) b[i] = max(b[i], b[i + 1]); for (int i = 1; i <= n; i++) cout << max(a[i], b[l[i]]) << ; return 0; }
#include <bits/stdc++.h> using namespace std; int N; char buf[20]; set<int> B, S, Q; const int INF = 1e9; inline int get_b() { return ((int)(B).size()) ? *B.rbegin() : -INF; } inline int get_s() { return ((int)(S).size()) ? *S.begin() : INF; } int main() { scanf( %d , &N); long long ans = 1; const int MOD = 1e9 + 7; for (int a = 0; a < N; a++) { scanf( %s , buf); int x; scanf( %d , &x); if (buf[1] == D ) { int bv = get_b(); int sv = get_s(); if (bv < x && x < sv) Q.insert(x); else if (x < bv) B.insert(x); else S.insert(x); } else { if (B.find(x) != B.end()) { if (get_b() != x) ans = 0; B.erase(x); for (int e : Q) S.insert(e); Q.clear(); } else if (S.find(x) != S.end()) { if (get_s() != x) ans = 0; S.erase(x); for (int e : Q) B.insert(e); Q.clear(); } else { ans = (ans * 2) % MOD; for (int e : Q) { if (e < x) B.insert(e); else if (e > x) S.insert(e); } Q.clear(); } } } ans = ans * (int)(Q.size() + 1) % MOD; printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using std::pair; using std::string; using std::vector; template <typename T> bool chkmax(T& a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> bool chkmin(T& a, T b) { return a > b ? a = b, 1 : 0; } const int oo = 0x3f3f3f3f; string procStatus() { std::ifstream t( /proc/self/status ); return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>()); } template <typename T> T read(T& x) { int f = 1; x = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == - ) f = -1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48; return x *= f; } const int N = 10000; struct point { int x, y; point(int _x = 0, int _y = 0) : x(_x), y(_y) {} point operator+(const point& rhs) const { return point(x + rhs.x, y + rhs.y); } point operator-(const point& rhs) const { return point(x - rhs.x, y - rhs.y); } bool operator<(const point& rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y); } }; inline int dot(const point& a, const point& b) { return a.x * b.x + a.y * b.y; } inline int cross(const point& a, const point& b) { return a.x * b.y - a.y * b.x; } vector<point> convex_hull(vector<point> a) { vector<point> h; std::sort(a.begin(), a.end()); for (auto p : a) { while ((int)h.size() > 1 && cross(p - *h.rbegin(), p - *(h.rbegin() + 1)) >= 0) { h.pop_back(); } h.push_back(p); } int m = h.size(); h.pop_back(); std::reverse(a.begin(), a.end()); for (auto p : a) { while ((int)h.size() > m && cross(p - *h.rbegin(), p - *(h.rbegin() + 1)) >= 0) { h.pop_back(); } h.push_back(p); } h.pop_back(); return h; } bool solve(vector<point> a, vector<point> b) { vector<pair<int, int> > edge; std::function<void(int, int)> get = [&](int l, int r) { edge.push_back(std::make_pair(l, r)); if (r - l < 2) return; int p = -1; double mx = -DBL_MAX; for (int i = l + 1; i < r; ++i) { double temp = 1. * dot(a[r] - a[i], a[l] - a[i]) / cross(a[r] - a[i], a[l] - a[i]); if (chkmax(mx, temp)) p = i; } get(l, p); get(p, r); }; a = convex_hull(a); get(0, a.size() - 1); for (auto e : edge) { int x = e.first, y = e.second; double l = -DBL_MAX, r = DBL_MAX; for (int i = 0; i < (int)a.size() && l < r; ++i) { int p = dot(a[y] - a[i], a[x] - a[i]); int q = cross(a[y] - a[i], a[x] - a[i]); if (!q) continue; if (q < 0) { chkmin(r, double(p) / q); } else { chkmax(l, double(p) / q); } } for (int i = 0; i < (int)b.size() && l < r; ++i) { int p = dot(a[y] - b[i], a[x] - b[i]); int q = cross(a[y] - b[i], a[x] - b[i]); if (!q) { if (p > 0) continue; l = DBL_MAX; break; } if (q < 0) { chkmax(l, double(p) / q); } else { chkmin(r, double(p) / q); } } if (l < r) return true; } return false; } int n, m; vector<point> a, b; int main() { read(n), read(m); for (int i = 0, x, y; i < n; ++i) read(x), read(y), a.push_back(point(x, y)); for (int i = 0, x, y; i < m; ++i) read(x), read(y), b.push_back(point(x, y)); if (n == 1 || m == 1) return puts( YES ), 0; puts((solve(a, b) || solve(b, a)) ? YES : NO ); return 0; }
//#define _USE_MATH_DEFINES #include<bits/stdc++.h> //#pragma GCC optimize ( O3 ) //#pragma GCC target ( sse4 ) using namespace std; #define ll long long #define endl n #define f first #define s second #define ar array #define pb push_back #define eb emplace_back #define mp make_pair #define sz(X) ((int)(X).size()) #define rsz resize #define pcnt __builtin_popcount #define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define get_pos(c, x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define all(X) (X).begin(), (X).end() #define rall(X) (X).rbegin(), (X).rend() #define ms(c, x) memset(c,x,sizeof c) #define No(x) cout<<(x? NO : No )<<endl #define Yes(x) cout<<(x? YES : Yes )<<endl #define nl cout<<endl; #define forn(i, n) for(int i = 0; i < int(n); i++) #define fore(i, l, r) for(int i = l; i <r; i++) #define fored(i, l, r) for(int i = r-1; i >=l; i--) #define ford(i, n) for (int i = n - 1; i >= 0; --i) using ld = long double; using ul = unsigned long long; using db = double; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; template<typename T, size_t size> using va = vector<ar<T, size>>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template<typename A, typename B> istream &operator>>(istream &in, pair<A, B> &p) { return in >> p.first >> p.second; } template<typename T> istream &operator>>(istream &in, vector<T> &vec) { for (auto &x : vec) { in >> x; } return in; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const auto &x : v) os << x << ; return os; } template<typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { for (const auto &x : arr) os << x << ; return os; } template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << << p.second; } template<class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } void debug() { cerr << endl; } template<typename Head, typename... Tail> void debug(Head H, Tail... T) { cerr << << H; debug(T...); } #ifdef LOCAL #define dbg(...) cerr <<__LINE__<< [[DEBUG]] << ( << #__VA_ARGS__ << ): , debug(__VA_ARGS__) #else #define dbg(...) #endif const ll mod = 1e9 + 7; //const int mod = 998244353; const ll INF = 2e18 + 6; inline ll modPow(ll x, ll y, ll mod) { ll res = 1; x = x % mod;; while (y > 0) { if (y & 1) { res = (res * x) % mod; } y = y >> 1; x = (x * x) % mod; } return res; } int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; char dirs[4] = { R , D , L , U }; unordered_map<char, int> rdirs = {{ R , 0}, { D , 1}, { L , 2}, { U , 3}}; void solve() { ll a, b, n; cin >> a >> b >> n; vpi in(n); forn(i,n)cin>>in[i].f; forn(i,n)cin>>in[i].s; sort(all(in)); forn(i, n) { ll times = (in[i].s+a-1) / a; b -= (times-1) * in[i].f; if(b <=0) { No(1); return; } b-=in[i].f; if(b<0&&i!=n-1){ No(1); return; } } Yes(1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(6); #ifdef LOCAL freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif int t = 1, tc = 1; cin >> t; while (t--) { // cout<< Case # <<tc<< : ; solve(); tc++; } #ifdef LOCAL cerr << endl << Time elapsed : << clock() * 1000.0 / CLOCKS_PER_SEC << ms << n ; #endif return 0; } //NOTES //look if it requires ll // read the statement carefully // check array bounds and cases for n=1
#include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const int INF = 1 << 29; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, k, cnt = 0; cin >> n >> m >> k; vector<char> V; for (int i = 0; i < n - 1; i++) { ++cnt; V.push_back( U ); } for (int i = 0; i < m - 1; i++) { ++cnt; V.push_back( L ); } int flag = 1; for (int i = 0; i < m - 1; i++) V.push_back( R ), ++cnt; for (int i = 0; i < n - 1; i++) { ++cnt; V.push_back( D ); if (flag == 1) { for (int i = 0; i < m - 1; i++) V.push_back( L ), ++cnt; } else { for (int i = 0; i < m - 1; i++) V.push_back( R ), ++cnt; } flag *= -1; } cout << cnt; cout << endl; for (auto u : V) cout << u; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n, m, len[maxn << 2]; bool first[maxn << 2]; long long mm[maxn], rr[maxn], ss[maxn], last[maxn << 2]; vector<long long> summ[maxn << 2], sumr[maxn << 2]; vector<int> tree[maxn << 2]; bool cmp(int a, int b) { return mm[a] * rr[b] < mm[b] * rr[a]; } void build(int cur, int l, int r) { len[cur] = 0; first[cur] = true, last[cur] = -1; tree[cur].clear(); tree[cur].push_back(0); for (int i = l; i <= r; i++) if (mm[i] != 0 && rr[i] != 0) tree[cur].push_back(i), len[cur]++; sort(tree[cur].begin() + 1, tree[cur].end(), cmp); summ[cur].clear(); sumr[cur].clear(); summ[cur].push_back(0); sumr[cur].push_back(0); long long sr = 0, sm = 0; for (int i = 1; i <= len[cur]; i++) { sr += rr[tree[cur][i]]; sm += mm[tree[cur][i]]; sumr[cur].push_back(sr); summ[cur].push_back(sm); } if (l != r) { build((cur << 1), l, ((l + r) >> 1)); build((cur << 1 | 1), ((l + r) >> 1) + 1, r); } } int find(int cur, long long t) { int l = 1, r = len[cur]; while (l <= r) { if (t * rr[tree[cur][((l + r) >> 1)]] >= mm[tree[cur][((l + r) >> 1)]]) l = ((l + r) >> 1) + 1; else r = ((l + r) >> 1) - 1; } return l - 1; } long long ask(int cur, int l, int r, long long t) { if (l == r && first[cur]) { long long ret = min(mm[l], t * rr[l] + ss[l]); ss[l] = 0, first[cur] = false; return ret; } if (last[cur] != -1) { long long delta = t - last[cur]; int mid = find(cur, delta); long long ret = 0; ret += summ[cur][mid]; ret += (sumr[cur][len[cur]] - sumr[cur][mid]) * delta; return ret; } long long ret = 0; ret += ask((cur << 1), l, ((l + r) >> 1), t); ret += ask((cur << 1 | 1), ((l + r) >> 1) + 1, r, t); return ret; } void down(int cur) { if (last[cur] != -1) { first[(cur << 1)] = first[(cur << 1 | 1)] = false; last[(cur << 1)] = last[(cur << 1 | 1)] = last[cur]; last[cur] = -1; } } long long query(int cur, int l, int r, int ql, int qr, long long t) { if (ql <= l && r <= qr) { long long ret = ask(cur, l, r, t); first[cur] = false, last[cur] = t; return ret; } down(cur); long long ret = 0; if (ql <= ((l + r) >> 1)) ret += query((cur << 1), l, ((l + r) >> 1), ql, qr, t); if (qr > ((l + r) >> 1)) ret += query((cur << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, t); return ret; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %I64d%I64d%I64d , &ss[i], &mm[i], &rr[i]); build(1, 1, n); scanf( %d , &m); int qq = 0; while (m--) { ++qq; int t, l, r; scanf( %d%d%d , &t, &l, &r); printf( %I64d n , query(1, 1, n, l, r, t)); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e6; const long long INF = 1e18; const long long mod = 1e9 + 7; long long y; long long n, m; vector<long long> v; void rec(long long x) { if (x > m) { return; } if (x == m) { cout << YES << endl; y = 1; } rec(x * 2); rec(x * 10 + 1); if (y) { v.push_back(x); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; rec(n); if (!y) { cout << NO ; return 0; } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); for (int i = 0; i < v.size() - 1; i++) { if (v[i] / 2 == v[i + 1] || v[i] / 10 == v[i + 1]) { continue; } v.erase(v.begin() + i + 1); i--; } cout << v.size() << endl; for (int i = v.size() - 1; i >= 0; i--) { cout << v[i] << ; } }
#include <bits/stdc++.h> using namespace std; vector<int> a; vector<pair<pair<int, int>, int> > b; vector<int> k; vector<int> ans; signed main() { ios_base::sync_with_stdio(0); int n, m; cin >> n; a.resize(n); b.resize(n); ans.resize(n); for (int i = 0; i < n; i++) { cin >> b[i].first.first >> b[i].second; } cin >> m; b.resize(n + m); k.resize(m); for (int i = 0; i < n; i++) { b[i].first.second = i + m; } for (int i = n; i < m + n; i++) { cin >> b[i].first.first >> b[i].second >> k[i - n]; b[i].first.second = i - n; } sort(b.begin(), b.end()); set<pair<int, int> > d; bool poss = true; for (int i = 0; i < n + m; i++) { if (b[i].first.second < m) d.insert(make_pair(b[i].second, b[i].first.second)); else { set<pair<int, int> >::iterator it = d.upper_bound(make_pair(b[i].second - 1, m)); if (it == d.end()) { poss = false; continue; } k[it->second]--; ans[b[i].first.second - m] = it->second + 1; if (k[it->second] == 0) d.erase(it); } } if (poss) { cout << YES << endl; for (int i = 0; i < n; i++) { cout << ans[i] << ; } cout << endl; } else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[100010], cnt[100010]; int main() { int max1 = 0, max2 = 0; cin >> n; ; for (int i = 1; i <= n; i++) { cin >> a[i]; ; } for (int i = 1; i <= n; i++) { if (a[i] > max1) { max2 = max1; max1 = a[i]; cnt[a[i]]--; } else { if (a[i] > max2) { cnt[max1]++; max2 = a[i]; } } } int ans, max3 = -100000; for (int i = 1; i <= n; i++) { if (cnt[i] > max3) { max3 = cnt[i]; ans = i; } } cout << ans; ; }
#include <bits/stdc++.h> using namespace std; class Solution { public: Solution(int _n) : N(_n) {} void guess(long long n, int k) { long long l = 1; long long r = n; int M = min(4LL * k, n); mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); string ans; for (int i = 0; i < N; ++i) { if (r - l + 1 <= M) { uniform_int_distribution<long long> dist(l, r); long long p = dist(gen); cout << p << << p << endl; fflush(stdout); cin >> ans; if (ans[0] == y || ans[0] == Y ) { return; } } else { long long m = l + (r - l) / 2; cout << l << << m << endl; fflush(stdout); cin >> ans; if (ans[0] == y || ans[0] == Y ) { r = m; } else { l = m + 1; } } l = max(1LL, l - k); r = min(n, r + k); } } private: int N; }; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); long long n; int k; long long p; cin >> n >> k; Solution sol(4500); sol.guess(n, k); return 0; }