func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; long long n, m, a[100005], sum, en; bool cheak(long long mid) { long long sum = 0, cnt = m; for (int i = 1; i <= n; ++i) { if (a[i]) { sum += a[i]; while (sum > mid - i) { sum -= (mid - i); cnt--; if (cnt < 0) return 0; } } } if (!cnt) return sum <= 0; return 1; } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; long long l = 0, r = 1e17, ans; while (l < r) { long long mid = (l + r) / 2; if (cheak(mid)) ans = mid, r = mid; else l = mid + 1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; int a[n], i; for (i = 0; i < n; ++i) cin >> a[i]; cin >> m; int b[m]; for (i = 0; i < m; ++i) cin >> b[i]; sort(a, a + n, greater<int>()); sort(b, b + m, greater<int>()); cout << a[0] << << b[0] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define M 1000000007 int main() { ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0); int t,n,x; cin>>t; string str= 2020 ; while(t--) { int a,b,c,d,e,c1,c2,c3; cin>>c1>>c2>>c3; cin>>a>>b>>c>>d>>e; bool flag=true; int te=min(c1,a); c1-=te; a-=te; te=min(c2,b); c2-=te; b-=te; te=min(c3,c); c3-=te; c-=te; te=min(c1,d); c1-=te; d-=te; te=min(c2,e); c2-=te; e-=te; te=min(c3,d); c3-=te; d-=te; te=min(c3,e); c3-=te; e-=te; if(a||b||c||d||e) cout<< no ; else cout<< yes ; cout<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string s[n]; for (int i = 0; i < n; i++) cin >> s[i]; vector<vector<int>> row(n), col(m); for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < m; j++) { if (s[i][j] == # ) { row[i].push_back(j); col[j].push_back(i); } } } deque<pair<int, int>> q; int rw[n]; int rvis[n]; for (int i = 0; i < n; i++) { rw[i] = INT_MAX; rvis[i] = 0; } int cw[m]; int cvis[m]; for (int i = 0; i < m; i++) { cw[i] = INT_MAX; cvis[i] = 0; } rw[n - 1] = 0; q.push_back(make_pair(0, n - 1)); while (q.size() > 0) { int type = q.front().first; int num = q.front().second; q.pop_front(); if (type == 0) { if (rvis[num] == 1) continue; else rvis[num] = 1; if (num == 0) break; for (int i = 0; i < row[num].size(); i++) { int cn = row[num][i]; if (cvis[cn] == 0 && cw[cn] > rw[num] + 1) { cw[cn] = rw[num] + 1; q.push_back(make_pair(1, cn)); } } } else if (type == 1) { if (cvis[num] == 1) continue; else cvis[num] = 1; for (int i = 0; i < col[num].size(); i++) { int rn = col[num][i]; if (rvis[rn] == 0 && rw[rn] > cw[num] + 1) { rw[rn] = cw[num] + 1; q.push_back(make_pair(0, rn)); } } } } if (rw[0] == INT_MAX) cout << -1; else cout << rw[0]; }
#include <bits/stdc++.h> using namespace std; class Solution { public: long long maximize(vector<int>& nums, int x) { int n = nums.size(); vector<vector<long long>> dp(3, vector<long long>(n + 1, 0)); long long res = 0; for (int i = 0; i < n; ++i) { long long num = nums[i]; dp[0][i + 1] = max(0LL, dp[0][i] + num); dp[1][i + 1] = max(0LL, max(dp[0][i], dp[1][i]) + num * x); dp[2][i + 1] = max(0LL, max(dp[2][i], dp[1][i]) + num); } for (int j = 0; j < 3; ++j) { for (int i = 0; i <= n; ++i) { res = max(res, dp[j][i]); } } return res; } }; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); Solution sol; int n, x; cin >> n >> x; vector<int> nums(n); for (int i = 0; i < n; ++i) { cin >> nums[i]; } cout << sol.maximize(nums, x) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; int main() { int i, n, sum; while (scanf( %d , &n) != EOF) { sum = 0; while (n != 1) { sum += n; for (i = 2; i <= n; i++) { if (n % i == 0) { n /= i; break; } } } cout << sum + 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long xa, ya, xb, yb, xc, yc, a, A; cin >> xa >> ya >> xb >> yb >> xc >> yc; if ((xa == xb) && (ya == yb)) cout << YES ; else if ((xb == ya) && (yb == -xa)) cout << YES ; else if ((xb == -xa) && (yb == -ya)) cout << YES ; else if ((xb == -ya) && (yb == xa)) cout << YES ; else if (((xc * xc) + (yc * yc)) == 0) cout << NO ; else { A = (xc * xc) + (yc * yc); a = (((xb - xa) * xc) + ((yb - ya) * yc)); if (a % A == 0) { a = a / A; if ((yc != 0) && ((xb - xa - (xc * a)) % yc == 0)) { cout << YES ; return 0; } else if ((xc != 0) && ((yb - ya - (yc * a)) % xc == 0)) { cout << YES ; return 0; } } a = (((xb + ya) * xc) + ((yb - xa) * yc)); if (a % A == 0) { a = a / A; if ((yc != 0) && ((xb + ya - (xc * a)) % yc == 0)) { cout << YES ; return 0; } else if ((xc != 0) && ((yb - xa - (yc * a)) % xc == 0)) { cout << YES ; return 0; } } a = (((xb + xa) * xc) + ((yb + ya) * yc)); if (a % A == 0) { a = a / A; if ((yc != 0) && ((xb + xa - (xc * a)) % yc == 0)) { cout << YES ; return 0; } else if ((xc != 0) && ((yb + ya - (yc * a)) % xc == 0)) { cout << YES ; return 0; } } a = (((xb - ya) * xc) + ((yb + xa) * yc)); if (a % A == 0) { a = a / A; if ((yc != 0) && ((xb - ya - (xc * a)) % yc == 0)) { cout << YES ; return 0; } else if ((xc != 0) && ((yb + xa - (yc * a)) % xc == 0)) { cout << YES ; return 0; } } cout << NO ; } return 0; }
#include <bits/stdc++.h> using namespace std; char str[110]; long long ans = 0; int main() { cin >> str; int ll = strlen(str); long long k = 1, l = 1; for (int i = 1; i < ll; i++) { l = (l % 1000000007 * 2) % 1000000007; } k = l; for (int i = ll - 1; i >= 0; i--) { if (str[i] == 1 ) ans = (ans % 1000000007 + k % 1000000007) % 1000000007; k = (k % 1000000007 * 2) % 1000000007; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long array1[n]; for (int i = 0; i < n; i++) { cin >> array1[i]; } long long a, b, c, d, e, sum = 0; cin >> a >> b >> c >> d >> e; map<long long, long long> mp; for (int i = 0; i < n; i++) { sum += array1[i]; mp[5] += sum / e; sum %= e; mp[4] += sum / d; sum %= d; mp[3] += sum / c; sum %= c; mp[2] += sum / b; sum %= b; mp[1] += sum / a; sum %= a; } for (int i = 1; i <= 5; i++) { cout << mp[i]; if (i != 5) cout << ; } cout << endl << sum << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; const int mod = 998244353; const int dx[] = {1, 1, 2, 2, -1, -1, -2, -2}; const int dy[] = {2, -2, 1, -1, 2, -2, 1, -1}; int A[1005][1005], B[1005][1005]; int C[1005][1005][2], D[1005][1005][2]; int main() { int n, m; scanf( %d%d , &n, &m); int x1, y1, x2, y2; scanf( %d%d%d%d , &x1, &y1, &x2, &y2); queue<int> Q; A[n / 2][m / 2] = 1; Q.push(n / 2), Q.push(m / 2); while (!Q.empty()) { int x = Q.front(); Q.pop(); int y = Q.front(); Q.pop(); for (int d = 0; d < 8; d++) { int nx = x + dx[d], ny = y + dy[d]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && A[nx][ny] == 0) { A[nx][ny] = A[x][y] + 1; C[nx][ny][0] = x; C[nx][ny][1] = y; Q.push(nx), Q.push(ny); } } } B[n / 2 + 1][m / 2] = 1; Q.push(n / 2 + 1), Q.push(m / 2); while (!Q.empty()) { int x = Q.front(); Q.pop(); int y = Q.front(); Q.pop(); for (int d = 0; d < 8; d++) { int nx = x + dx[d], ny = y + dy[d]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && B[nx][ny] == 0) { B[nx][ny] = B[x][y] + 1; D[nx][ny][0] = x; D[nx][ny][1] = y; Q.push(nx), Q.push(ny); } } } if ((x1 + y1) % 2 == (x2 + y2) % 2) { int k = A[x1][y1]; if (B[x2][y2] < k) { puts( BLACK ); fflush(stdout); while (x2 != n / 2 + 1 || y2 != m / 2) { scanf( %d%d , &x1, &y1); int nx2 = D[x2][y2][0], ny2 = D[x2][y2][1]; printf( %d %d n , nx2, ny2); fflush(stdout); x2 = nx2, y2 = ny2; if (x2 == x1 && y2 == y1) return 0; } } else if (A[x2][y2] > k) { int nx1 = C[x1][y1][0], ny1 = C[x1][y1][1]; printf( WHITE n%d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; while (x1 != n / 2 || y1 != m / 2) { scanf( %d%d , &x2, &y2); nx1 = C[x1][y1][0], ny1 = C[x1][y1][1]; printf( %d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; } } else { puts( BLACK ); fflush(stdout); while (x2 != n / 2 || y2 != m / 2) { scanf( %d%d , &x1, &y1); int nx2 = C[x2][y2][0], ny2 = C[x2][y2][1]; printf( %d %d n , nx2, ny2); fflush(stdout); x2 = nx2, y2 = ny2; if (x2 == x1 && y2 == y1) return 0; } scanf( %d%d , &x1, &y1); if (A[x1][y1] == 2) { printf( %d %d n , x1, y1); fflush(stdout); return 0; } int nx2 = n / 2 + 1, ny2 = m / 2 + 2; printf( %d %d n , nx2, ny2); fflush(stdout); x2 = nx2, y2 = ny2; if (x2 == x1 && y2 == y1) return 0; scanf( %d%d , &x1, &y1); nx2 = n / 2 + 3, ny2 = m / 2 + 1; printf( %d %d n , nx2, ny2); fflush(stdout); x2 = nx2, y2 = ny2; if (x2 == x1 && y2 == y1) return 0; scanf( %d%d , &x1, &y1); nx2 = n / 2 + 1, ny2 = m / 2; printf( %d %d n , nx2, ny2); fflush(stdout); } } else { int k = B[x2][y2]; if (A[x1][y1] <= k) { int nx1 = C[x1][y1][0], ny1 = C[x1][y1][1]; printf( WHITE n%d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; while (x1 != n / 2 || y1 != m / 2) { scanf( %d%d , &x2, &y2); nx1 = C[x1][y1][0], ny1 = C[x1][y1][1]; printf( %d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; } } else if (B[x1][y1] > k + 1) { puts( BLACK ); fflush(stdout); while (x2 != n / 2 + 1 || y2 != m / 2) { scanf( %d%d , &x1, &y1); int nx2 = D[x2][y2][0], ny2 = D[x2][y2][1]; printf( %d %d n , nx2, ny2); fflush(stdout); x2 = nx2, y2 = ny2; } } else { if (x1 == n / 2 + 1 && y1 == m / 2) { puts( WHITE ); fflush(stdout); if (B[x2][y2] == 2) { printf( %d %d n , x2, y2); fflush(stdout); return 0; } else { int nx1 = n / 2, ny1 = m / 2 + 2; printf( %d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; scanf( %d%d , &x2, &y2); nx1 = n / 2 - 2, ny1 = m / 2 + 1; printf( %d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; scanf( %d%d , &x2, &y2); nx1 = n / 2, ny1 = m / 2; printf( %d %d n , nx1, ny1); fflush(stdout); } return 0; } int nx1 = D[x1][y1][0], ny1 = D[x1][y1][1]; printf( WHITE n%d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; while (x1 != n / 2 + 1 || y1 != m / 2) { scanf( %d%d , &x2, &y2); nx1 = D[x1][y1][0], ny1 = D[x1][y1][1]; printf( %d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; } scanf( %d%d , &x2, &y2); if (B[x2][y2] == 2) { printf( %d %d n , x2, y2); fflush(stdout); return 0; } nx1 = n / 2, ny1 = m / 2 + 2; printf( %d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; scanf( %d%d , &x2, &y2); nx1 = n / 2 - 2, ny1 = m / 2 + 1; printf( %d %d n , nx1, ny1); fflush(stdout); x1 = nx1, y1 = ny1; if (x1 == x2 && y1 == y2) return 0; scanf( %d%d , &x2, &y2); nx1 = n / 2, ny1 = m / 2; printf( %d %d n , nx1, ny1); fflush(stdout); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, a1, a2, b1, b2, c1, c2, s; cin >> n; cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2; s = a1 + b1 + c1; n = n - s; if (n == 0) { cout << a1 << << b1 << << c1 << endl; return 0; } else { a2 = a2 - a1; b2 = b2 - b1; c2 = c2 - c1; if (n <= a2) a1 += n; else { a1 += a2; n = n - a2; if (n == 0) { cout << a1 << << b1 << << c1 << endl; return 0; } if (n <= b2) b1 += n; else { b1 += b2; n = n - b2; if (n == 0) { cout << a1 << << b1 << << c1 << endl; return 0; } if (n <= c2) { c1 += n; } else { c1 += c2; } } } } cout << a1 << << b1 << << c1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, m, i, x, p = 0, kk = 100000000000000000, sum = 0; cin >> n >> k >> m; string me[n]; map<string, long long> fahim; for (i = 0; i < n; i++) { cin >> me[i]; } for (i = 0; i < n; i++) { cin >> fahim[me[i]]; } for (i = 0; i < k; i++) { cin >> x; if (x == 1) { cin >> x; continue; } if (x != 1) { long long d[x]; for (p = 0; p < x; p++) { cin >> d[p]; kk = min(kk, fahim[me[d[p] - 1]]); } for (p = 0; p < x; p++) { fahim[me[d[p] - 1]] = kk; } } kk = 100000000000000000; } string word; for (i = 0; i < m; i++) { cin >> word; sum += fahim[word]; } cout << sum; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int sigma = maxn; const int HASH_CNT = 2; int n; char s[maxn]; unsigned long long Prime_Pool[] = {1998585857ul, 23333333333ul}; unsigned long long Seed_Pool[] = {146527, 19260817}; unsigned long long Mod_Pool[] = {1000000009, 998244353}; unsigned long long has1[26], has2[26]; struct Hash { unsigned long long Seed, Mod; unsigned long long bas[maxn]; unsigned long long sum[26][maxn]; int perm[sigma]; void init(int seedIndex, int modIndex) { Seed = Seed_Pool[seedIndex]; Mod = Mod_Pool[modIndex]; bas[0] = 1; for (int i = 1; i <= n; i++) { bas[i] = bas[i - 1] * Seed % Mod; } for (int i = 1; i <= n; i++) { for (int j = 0; j < 26; j++) { sum[j][i] = (sum[j][i - 1] * Seed % Mod + (s[i] == j + a )) % Mod; } } } unsigned long long getHash(int ch, int l, int r) { return (sum[ch][r] - sum[ch][l - 1] * bas[r - l + 1] % Mod + Mod) % Mod; } } hasher[HASH_CNT]; bool check(int l1, int r1, int l2, int r2) { for (int i = 0; i < HASH_CNT; i++) { for (int j = 0; j < 26; j++) { has1[j] = hasher[i].getHash(j, l1, r1); has2[j] = hasher[i].getHash(j, l2, r2); } sort(has1, has1 + 26); sort(has2, has2 + 26); for (int j = 0; j < 26; j++) if (has1[j] != has2[j]) { return false; } } return true; } int main() { int m, x, y, len; scanf( %d%d , &n, &m); scanf( %s , s + 1); for (int i = 0; i < HASH_CNT; i++) hasher[i].init(i, i); while (m--) { scanf( %d%d%d , &x, &y, &len); if (check(x, x + len - 1, y, y + len - 1)) puts( YES ); else puts( NO ); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } long long f[200009], dp[200009][2]; int main() { string s, t; cin >> s >> t; int m = t.size(); t += # ; t += s; for (int i = 1; i < t.size(); i++) { int j = f[i - 1]; while (j > 0 and t[i] != t[j]) j = f[j - 1]; j += (t[i] == t[j]); f[i] = j; } int d = 0, ind; for (int i = m + 1; i < t.size(); i++) { if (f[i] == m) d = 1, ind = i - m; if (d) { dp[i][0] = ((ind - m + 1) + dp[i - 1][0] + dp[ind][1]) % 1000000007; dp[i][1] = (dp[i][0] + dp[i - 1][1]) % 1000000007; } } cout << dp[t.size() - 1][0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 21; void solve() { int n, x; cin >> n; vector<int> cnt(N, 0); for (int i = 0; i < n; i++) { cin >> x; for (int j = 0; j < N; j++) { if (x >> j & 1) cnt[j]++; } } long long ans = 0; for (int i = 0; i < n; i++) { x = 0; for (int j = N - 1; j >= 0; j--) { if (cnt[j]) { cnt[j]--; x |= (1 << j); } } ans += (x * 1LL * x); } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int b[n]; b[0] = 0; for (int i = 1; i < n; i++) b[i] = abs(a[i] - a[i - 1]); long long int dp[n], dd[n]; dp[0] = 0; dd[0] = 0; for (int i = 1; i < n; i++) { if (i % 2 == 1) { dp[i] = dp[i - 1] + b[i]; dd[i] = max(dd[i - 1] - b[i], 0LL); } else { dp[i] = max(0LL, dp[i - 1] - b[i]); dd[i] = dd[i - 1] + b[i]; } } long long int ans = 0; for (int i = 0; i < n; i++) ans = max(ans, dp[i]); for (int i = 0; i < n; i++) ans = max(ans, dd[i]); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; char str[300005]; vector<int> idx[27]; int main() { int x; scanf( %s , str); int n = strlen(str); for (int i = 0; i < (int)(n); i++) { idx[str[i] - a ].push_back(i); } long long int ans[27] = {0}; int ansmax = 0; for (int i = 0; i < (int)(n); i++) { set<int> fir; for (int j = 0; j < (int)(26); j++) { vector<int>::iterator it = lower_bound(idx[j].begin(), idx[j].end(), i); if (it != idx[j].end()) fir.insert(*it); } int p = i; int c = 0; for (__typeof((fir).begin()) it = (fir).begin(); it != (fir).end(); ++it) { ans[c++] += *it - p; p = *it; } ans[c] += n - p; ansmax = max(ansmax, c); } cout << ansmax << endl; for (int i = 0; i < (int)(ansmax); i++) cout << ans[i + 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool comp(int a, int b) { return (a > b); } int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int main(void) { long long int n, m, k; cin >> n >> m >> k; if (k > (n + m - 2)) { cout << -1 ; return 0; } long long int ans; if (m > n) swap(n, m); if (k <= m - 1) ans = max(m / (k + 1) * n, n / (k + 1) * m); else if (k <= n - 1) ans = n / (k + 1) * m; else ans = m / (k - n + 2); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf( %d , &x); } void _R(long long &x) { scanf( %lld , &x); } void _R(double &x) { scanf( %lf , &x); } void _R(char &x) { scanf( %c , &x); } void _R(char *x) { scanf( %s , x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf( %d , x); } void _W(const long long &x) { printf( %lld , x); } void _W(const double &x) { printf( %.16f , x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf( %s , x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.first); putchar( ); _W(x.second); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar( ); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? : n ); W(tail...); } int MOD = 1e9 + 7; int main() { long long n, a, b, k; R(n, a, b, k); vector<long long> h(n); for (int i = 0; i < (n); ++i) { R(h[i]); } long long res = 0; vector<long long> kk; for (int i = 0; i < (n); ++i) { if ((h[i] % (a + b)) <= a && (h[i] % (a + b)) > 0) { ++res; } else { long long rest = h[i] - (h[i] / (a + b)) * (a + b); rest -= a; if (h[i] % (a + b) == 0) { rest += a + b; } long long r2 = rest; rest /= a; if (r2 % a > 0) { rest += 1; } kk.push_back(rest); } } std::sort(kk.begin(), kk.end()); for (int i = 0; i < (kk.size()); ++i) { if (kk[i] <= k) { ++res; k -= kk[i]; } else { break; } } W(res); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a[200005], b[200005], s[200005], t[200005], dp[200005]; vector<int> v[200005]; bool flag; void dfs(int x, int y) { s[x] = 1; dp[x] = t[x] = b[x]; int best = 0; for (int i = (0); i < (v[x].size()); i++) { int V = v[x][i]; if (V == y) continue; dfs(V, x); s[x] += s[V]; t[x] += t[V]; if (s[V] == t[V]) dp[x] += s[V]; else best = max(best, dp[V]); } dp[x] += best; if (!b[x]) dp[x] = 0; } void dfs2(int x, int y) { int best = 0, ch = -1, sec = 0; dp[x] = t[x] = b[x]; s[x] = 1; for (int i = (0); i < (v[x].size()); i++) { int V = v[x][i]; s[x] += s[V]; t[x] += t[V]; if (s[V] > t[V]) { if (best <= dp[V]) { sec = best; best = dp[V]; ch = V; } else if (dp[V] > sec) sec = dp[V]; } else dp[x] += s[V]; } dp[x] += best; if (!b[x]) dp[x] = 0; if (dp[x] >= m) flag = 1; for (int i = (0); i < (v[x].size()); i++) { int V = v[x][i]; if (V == y) continue; int ori = dp[x], ot = t[x], os = s[x]; if (s[V] == t[V]) dp[x] -= t[V]; else if (ch == V) dp[x] -= best - sec; if (!b[x]) dp[x] = 0; t[x] -= t[V]; s[x] -= s[V]; dfs2(V, x); dp[x] = ori; t[x] = ot; s[x] = os; } } bool ok(int l) { for (int i = (1); i <= (n); i++) b[i] = a[i] >= l; dfs(1, 0); flag = 0; dfs2(1, 0); return flag; } int main() { scanf( %d %d , &n, &m); for (int i = (1); i <= (n); i++) scanf( %d , &a[i]); for (int i = (1); i < (n); i++) { int x, y; scanf( %d %d , &x, &y); v[x].push_back(y); v[y].push_back(x); } int l = 1, h = 1000000; while (l < h) { int m = l + h + 1 >> 1; if (ok(m)) l = m; else h = m - 1; } printf( %d n , l); }
#include <bits/stdc++.h> const int maxn = 5e5 + 5; using namespace std; int a[maxn]; int main() { int n; scanf( %d , &n); long long ans = 0, sum = 0; for (int i = 1; i <= n; ++i) { scanf( %d , &a[i]); int del = min((long long)a[i] >> 1, sum); ans += del; sum -= del; a[i] -= del << 1; ans += a[i] / 3; a[i] %= 3; sum += a[i]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000 + 10; const int maxa = 10000000 + 10; const int inf = (-1u) >> 1; struct disjoint_set { int p[maxn]; void clear(int sz) { for (int i = (1); i <= (sz); ++i) { p[i] = i; } } int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } bool join(int x, int y) { x = find(x), y = find(y); if (x == y) return false; p[x] = y; return true; } }; int id[maxa], n; disjoint_set ds; bool check(int x, int y) { return id[x] && id[y] && ds.join(id[x], id[y]); } int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } int main() { scanf( %d , &n); for (int i = (1); i <= (n); ++i) { int t; scanf( %d , &t); id[t] = i; } ds.clear(n); int ans = n; for (int s = 1; s <= 6000; ++s) { for (int t = (s & 1) + 1; t < s; t += 2) { if (gcd(s, t) != 1) continue; int x = 2 * s * t; int y = s * s - t * t; int z = s * s + t * t; if (x >= maxa || y >= maxa) { continue; } ans -= check(x, y); if (z < maxa) { ans -= check(x, z); ans -= check(y, z); } } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1003; const int xx[4] = {0, 1, 0, -1}; const int yy[4] = {1, 0, -1, 0}; int x[3], y[3]; void pr(int x, int y, int fx, int fy) { while (x < fx) { ++x; printf( %d %d n , x, y); } while (x > fx) { --x; printf( %d %d n , x, y); } while (y < fy) { ++y; printf( %d %d n , x, y); } while (y > fy) { --y; printf( %d %d n , x, y); } } int main() { for (int i = 0; i < 3; ++i) scanf( %d%d , &x[i], &y[i]); int ans = N * N; int ansx, ansy; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { int yans = 0; for (int k = 0; k < 3; ++k) yans += (abs(i - x[k]) + abs(j - y[k]) + 1); yans -= 2; if (yans < ans) { ans = yans; ansx = i; ansy = j; } } } printf( %d n , ans); printf( %d %d n , ansx, ansy); for (int i = 0; i < 3; ++i) pr(ansx, ansy, x[i], y[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int noOfOne(int n) { int c = 0; while (n) { c += (n & 1); n >>= 1; } return c; } unsigned long long int f(unsigned long long int n, unsigned long long int r) { if (n - r > r) { unsigned long long int temp = n, temp2 = r; unsigned long long int ans = 1; while (temp > (n - r)) { ans *= temp; --temp; } while (temp2) { ans /= temp2; --temp2; } return ans; } unsigned long long int temp = n, temp2 = n - r; unsigned long long int ans = 1; while (temp > r) { ans *= temp; --temp; } while (temp2) { ans /= temp2; --temp2; } return ans; } const unsigned long long int N = 1e6 + 5; const unsigned long long int MOD = 1e9 + 7; unsigned long long int n, cnt[N], ans = 0; int32_t main() { cin >> n; unsigned long long int num; cnt[0] = 1; for (int i = 1; i <= n; i++) { cin >> num; vector<unsigned long long int> cur; for (unsigned long long int j = 1; j * j <= num; j++) { if (num % j == 0) { cur.push_back(j); if (j != num / j) cur.push_back(num / j); } } sort(cur.begin(), cur.end()); for (int i = cur.size() - 1; i >= 0; i--) { cnt[cur[i]] += cnt[cur[i] - 1]; cnt[cur[i]] %= MOD; } } for (int i = 1; i <= n && cnt[i] != 0; i++) ans += cnt[i]; ans %= MOD; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const int mod = inf + 7; const double eps = 1e-9; const double pi = acos(-1.0); long double a, b, c, d; pair<long double, long double> get(pair<long double, long double> a, pair<long double, long double> b) { long double x[4] = {a.first * b.first, a.second * b.first, a.first * b.second, a.second * b.second}; sort(x, x + 4); return make_pair(x[0], x[3]); } bool ok(pair<long double, long double> a, pair<long double, long double> b) { long double l = max(a.first, b.first); long double r = min(a.second, b.second); return l < r + 1e-21; } bool ok(long double ans) { pair<long double, long double> x = make_pair(a - ans, a + ans); pair<long double, long double> y = make_pair(b - ans, b + ans); pair<long double, long double> z = make_pair(c - ans, c + ans); pair<long double, long double> t = make_pair(d - ans, d + ans); pair<long double, long double> first = get(x, t); pair<long double, long double> second = get(y, z); return ok(first, second); } long double mx(long double a, long double b, long double c, long double d) { return max(abs(a), max(abs(b), max(abs(c), abs(d)))); } int main() { cin >> a >> b >> c >> d; long double l = 0, r = mx(a, b, c, d); for (int i = 0; i < 20000; i++) { long double mid = (l + r) / 2.0; if (ok(mid)) { r = mid; } else l = mid; } printf( %.10f n , (double)r); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, p, k, pre[100005], f[100005][128]; struct mem { long long p[7], a; bool operator<(const mem &v) const { return a > v.a; } } c[100005]; int main() { scanf( %lld%lld%lld , &n, &p, &k); for (int i = 1; i <= n; i++) scanf( %lld , &c[i].a); for (int i = 1; i <= n; i++) for (int j = 0; j < p; j++) scanf( %lld , &c[i].p[j]); sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + c[i].a; for (int i = 1; i <= n; i++) for (int j = 0; j < 1 << p; j++) { int sz = 0; for (int k = 0; k < p; k++) sz += (j >> k) & 1; f[i][j] = f[i - 1][j]; if (k + sz >= i && sz <= i - 1) f[i][j] += c[i].a; for (int k = 0; k < p; k++) if ((j >> k) & 1) { int q = j ^ (1 << k); f[i][j] = max(f[i][j], f[i - 1][q] + c[i].p[k]); } } cout << f[n][(1 << p) - 1]; return 0; }
#include <bits/stdc++.h> using namespace std; struct __timestamper {}; vector<string> solve(vector<int> a) { int n = a.size(); for (int i = 0; i < n - 1; i++) assert(a[i] >= a[i + 1]); if (n == 0) return vector<string>(); if (a[0] == 1) { for (int x : a) assert(x == 1); vector<string> res; res.push_back(string(n, 1 )); return res; } if (a.back() == n) { vector<string> res(n + 1); for (int i = 0; i <= n; i++) { res[i] = string(n, 1 ); if (i > 0) res[i][i - 1] = 0 ; } return res; } int x = a.back(); assert(x < n); a.pop_back(); string cur(n - 1, 0 ); for (int i = 0; i < n - 1; i++) { if (a[i] == n) { a[i]--; cur[i] = 1 ; } } auto res = solve(a); int left = x - 1; for (int i = 0; i < (int)res.size(); i++) { char c = 0 ; if (left > 0 && res[i] != cur) { left--; c = 1 ; } res[i] += c; } cur += 1 ; res.push_back(cur); return res; } vector<string> fast(vector<int> a) { int n = a.size(); vector<pair<int, int>> b(n); for (int i = 0; i < n; i++) b[i] = {a[i], i}; sort(b.rbegin(), b.rend()); vector<int> c(n); for (int i = 0; i < n; i++) c[i] = b[i].first; auto s = solve(c); vector<string> res; for (auto o : s) { string t(n, . ); for (int j = 0; j < n; j++) { t[b[j].second] = o[j]; } res.push_back(t); } sort(res.begin(), res.end()); for (int i = 0; i < (int)res.size() - 1; i++) assert(res[i] != res[i + 1]); for (int i = 0; i < n; i++) { int cnt = 0; for (int j = 0; j < (int)res.size(); j++) { cnt += res[j][i] == 1 ; } assert(cnt == a[i]); } return res; } void stress() { for (int it = 59;; it++) { mt19937 rnd(it); cerr << it << = << it << endl; int n = 1e3; vector<int> a(n); for (int i = 0; i < n; i++) a[i] = rnd() % n + 1; cerr << a << = ; for (auto xxxx : a) cerr << xxxx << ; cerr << endl; fast(a); } } int main() { int n; while (scanf( %d , &n) == 1) { vector<int> a(n); for (int i = 0; i < n; i++) scanf( %d , &a[i]); auto res = fast(a); printf( %d n , (int)res.size()); for (auto s : res) printf( %s n , s.c_str()); } return 0; }
#include <bits/stdc++.h> using namespace std; int const K = 12; int const N = 5e4 + 123; int n, k; int s[N][K], maxi[N][K], mini[N][K]; int ans[N]; int cnt[N]; struct Player { int id, v; Player(){}; Player(int id, int v) : id(id), v(v){}; bool operator<(const Player &a) const { if (v != a.v) return v < a.v; return id < a.id; }; }; set<Player> t[K]; Player winner = Player(-1, -1); void pushplayer(int id) { for (int i = 0; i < k; ++i) { t[i].insert(Player(id, maxi[id][i])); } } void remove(int id) { for (int i = 0; i < k; ++i) { t[i].erase(Player(id, maxi[id][i])); } } void merge(int ida, int idb) { int idc = min(ida, idb); winner.id = idc; cnt[idc] = cnt[ida] + cnt[idb]; for (int i = 0; i < k; ++i) { mini[idc][i] = min(mini[idb][i], mini[ida][i]); maxi[idc][i] = max(maxi[idb][i], maxi[ida][i]); } } bool canbeat(int ida, int idb) { for (int i = 0; i < k; ++i) if (maxi[ida][i] > mini[idb][i]) return true; return false; } void solve() { ans[0] = 1; winner = Player(0, -1); for (int i = 0; i < n; ++i) for (int j = 0; j < k; ++j) mini[i][j] = maxi[i][j] = s[i][j]; for (int i = 0; i < n; ++i) cnt[i] = 1; for (int i = 1; i < n; ++i) { bool f; if (canbeat(i, winner.id)) { if (canbeat(winner.id, i)) { merge(i, winner.id); f = true; } else { pushplayer(winner.id); winner = Player(i, -1); f = false; } } else { pushplayer(i); f = false; } while (f) { f = false; Player bf = Player(-1, -1); for (int j = 0; j < k; ++j) { if ((int)(t[j].size())) { set<Player>::iterator it = t[j].end(); --it; Player b = (*it); if (canbeat(b.id, winner.id)) { bf = b; f = true; break; } } } if (!f) break; remove(bf.id); merge(winner.id, bf.id); } ans[i] = cnt[winner.id]; } } int main() { scanf( %d %d , &n, &k); for (int i = 0; i < n; ++i) for (int j = 0; j < k; ++j) scanf( %d , &s[i][j]); solve(); for (int i = 0; i < n; ++i) printf( %d n , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n; cout << (((1ll << (n / 2)) - 1) * 2) << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long ara[505][505]; bool row_check(long long val) { for (int i = 0; i < n; i++) { long long tmp = 0; for (int j = 0; j < n; j++) { tmp += ara[i][j]; } if (tmp != val) return false; } return true; } bool col_check(long long val) { for (int i = 0; i < n; i++) { long long tmp = 0; for (int j = 0; j < n; j++) { tmp += ara[j][i]; } if (tmp != val) return false; } return true; } bool diagonal_check(long long val) { long long tmp = 0; for (int i = 0; i < n; i++) tmp += ara[i][i]; if (tmp != val) return false; tmp = 0; for (int i = 0; i < n; i++) { tmp += ara[i][n - 1 - i]; } return (tmp == val); } bool check() { long long row = 0; for (int i = 0; i < n; i++) row += ara[i][0]; return row_check(row) & col_check(row) & diagonal_check(row); } int main() { ios_base::sync_with_stdio(false); while (cin >> n) { int r, c; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> ara[i][j]; if (ara[i][j] == 0) { r = i; c = j; } } } if (n > 1) { long long sum = 0; int x = 0; if (r == 0) x++; for (int i = 0; i < n; i++) sum += ara[x][i]; for (int i = 0; i < n; i++) sum -= ara[r][i]; if (sum > 0) { ara[r][c] = sum; if (check()) { cout << ara[r][c] << endl; } else { printf( -1 n ); } } else { printf( -1 n ); } } else printf( 1 n ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int T, n, i, j, l, m, x, y, z, a, b, c, d, flag, count; cin >> a >> b >> c >> d; long long int sum1, sum2; sum1 = std::max((3 * a / 10), (a - ((a / 250) * c))); sum2 = std::max((3 * b / 10), (b - ((b / 250) * d))); if (sum1 > sum2) cout << Misha << endl; else if (sum1 < sum2) cout << Vasya << endl; else cout << Tie << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { string name; int id; int taxi, pizza, girl; } arr[105]; int cmp1(node a, node b) { if (a.taxi == b.taxi) return a.id < b.id; return a.taxi > b.taxi; } int cmp2(node a, node b) { if (a.pizza == b.pizza) return a.id < b.id; return a.pizza > b.pizza; } int cmp3(node a, node b) { if (a.girl == b.girl) return a.id < b.id; return a.girl > b.girl; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int m; cin >> m >> arr[i].name; arr[i].id = i; for (int j = 0; j < m; j++) { int a, b, c; scanf( %d-%d-%d , &a, &b, &c); if (a % 10 == a / 10 && a == b && b == c) arr[i].taxi++; else if (a / 10 > a % 10 && a % 10 > b / 10 && b / 10 > b % 10 && b % 10 > c / 10 && c / 10 > c % 10) arr[i].pizza++; else arr[i].girl++; } } sort(arr, arr + n, cmp1); printf( If you want to call a taxi, you should call: ); int first = 1; for (int i = 0; i < n; i++) { if (arr[i].taxi != arr[0].taxi) break; if (!first) cout << , ; cout << arr[i].name; first = 0; } cout << . << endl; sort(arr, arr + n, cmp2); printf( If you want to order a pizza, you should call: ); first = 1; for (int i = 0; i < n; i++) { if (arr[i].pizza != arr[0].pizza) break; if (!first) cout << , ; cout << arr[i].name; first = 0; } cout << . << endl; sort(arr, arr + n, cmp3); printf( If you want to go to a cafe with a wonderful girl, you should call: ); first = 1; for (int i = 0; i < n; i++) { if (arr[i].girl != arr[0].girl) break; if (!first) cout << , ; cout << arr[i].name; first = 0; } cout << . << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; const int MOD = 1e9 + 7; void solve() { int n, m; cin >> n >> m; vector<int> a(n), b(m + 1), la(m + 1); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i < m + 1; ++i) cin >> b[i]; int mid = 0, l = 0, r = n - 1, ans = -1; while (l <= r) { mid = (l + r) / 2; for (int i = 0; i < m + 1; ++i) la[i] = -1; for (int i = 0; i < mid + 1; ++i) { if (a[i] != 0) la[a[i]] = i; } int p = 0, ctr = 0; for (int i = 0; i < mid + 1; ++i) { if (la[a[i]] != i) ctr++; else if (ctr >= b[a[i]]) { ctr -= b[a[i]]; p++; } else break; } if (p == m) { ans = mid + 1; r = mid - 1; } else { l = mid + 1; } } cout << ans; } int main() { int t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; t = 1; while (t--) { stack<int> st; int n, x, ans = 0; scanf( %d , &n); for (int i = 0; i < n; ++i) { scanf( %d , &x); while (!st.empty()) { ans = max(ans, st.top() ^ x); if (st.top() > x) break; st.pop(); } st.push(x); } printf( %d n , ans); } }
#include <bits/stdc++.h> unsigned bactery_count(unsigned num) { unsigned res = 0; while (num > 0) { (num & true) ? ++res, --num : num >>= 1; } return res; } int main() { unsigned num; std::cin >> num; std::cout << bactery_count(num) << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; while (cin >> N) { int Store[31][31], i, j, SumR, SumC, Count = 0; vector<int> Row, Col; memset(Store, false, sizeof(Store)); for (i = 0; i < N; i++) for (j = 0; j < N; j++) cin >> Store[i][j]; for (i = 0; i < N; i++) { SumR = 0; SumC = 0; for (j = 0; j < N; j++) { SumR += Store[i][j]; SumC += Store[j][i]; } Row.push_back(SumR); Col.push_back(SumC); } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { if (Row[i] < Col[j]) Count++; } } cout << Count << endl; } }
#include <bits/stdc++.h> using namespace std; const int lim = 400100; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const long long mod = 1000000007; long long a, b; int main() { cin >> a >> b; long long ans; ans = (b * (b - 1)) / 2; ans %= mod; ans *= (((b * (((a * (a + 1)) / 2) % mod)) % mod) + a) % mod; ans %= mod; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m; cin >> n >> m; if (n == 1) { cout << 0 << endl; return; } if (n == 2) { cout << m << endl; return; } if (n > 2) { cout << 2 * m << endl; return; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n = 0; cin >> n; vector<int> ans; int k = 0; if (n == 1) { cout << 1 << endl << 1; return 0; } for (int i = 1; i <= n; i++) { ans.push_back(i); if (n - i >= 0) n -= i; else break; } if (n == 0) { cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i] << ; } else { cout << ans.size() << endl; sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size() - 1; i++) cout << ans[i] << ; cout << ans[ans.size() - 1] + n; return 0; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 6; string lcp(string txt, string pat) { string S = pat + $ + txt; vector<int> Arr(S.size(), 0); int j = 0; for (int i = 1; i < S.size(); i++) { while (j > 0 && S[j] != S[i]) j = Arr[j - 1]; if (S[j] == S[i]) j++; Arr[i] = j; } int MX = 0; for (int i = 0; i < S.size(); i++) MX = max(MX, Arr[i]); if (MX == pat.size()) return txt; return txt + pat.substr(Arr[S.size() - 1]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s[3]; cin >> s[0] >> s[1] >> s[2]; sort(s, s + 3); int Res = 1e9 + 7; do { string s1 = lcp(lcp(s[0], s[1]), s[2]); string s2 = lcp(s[0], lcp(s[1], s[2])); int a = s1.size(), b = s2.size(); Res = min(Res, min(a, b)); } while (next_permutation(s, s + 3)); cout << Res << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int qmin(int a, int b) { return (a < b) ? a : b; } int qmax(int a, int b) { return (a > b) ? a : b; } int n, m, k; int x[555], y[555]; int xl[555], xr[555], yl[555], yr[555]; set<int> stx, sty; map<int, int> mpx; int xpos[1010]; vector<int> l[1010], r[1010]; multiset<pair<int, int> > y_seg_l; bool check(int now) { for (int i = 0; i < 1001; i++) l[i].clear(), r[i].clear(); for (int i = 0; i < k; i++) xl[i] = qmax(x[i] - now, 1), xr[i] = qmin(x[i] + now, n), yl[i] = qmax(y[i] - now, 1), yr[i] = qmin(y[i] + now, m); stx.clear(), sty.clear(); for (int i = 0; i < k; i++) stx.insert(xl[i]), stx.insert(xr[i] + 1); stx.insert(1), stx.insert(n + 1); int cnt = 0; for (set<int>::iterator it = stx.begin(); it != stx.end(); it++, cnt++) mpx[*it] = cnt, xpos[cnt] = *it; for (int i = 0; i < k; i++) l[mpx[xl[i]]].push_back(i), r[mpx[xr[i] + 1]].push_back(i); y_seg_l.clear(); int xxl = n + 1, xxr = 0, yyl = m + 1, yyr = 0; y_seg_l.insert(make_pair(0, 0)); y_seg_l.insert(make_pair(m + 1, m + 1)); for (int i = 0; i < cnt - 1; i++) { for (int j = 0; j < (int)r[i].size(); j++) { multiset<pair<int, int> >::iterator it = y_seg_l.lower_bound(make_pair(yl[r[i][j]], yr[r[i][j]])); y_seg_l.erase(it); } for (int j = 0; j < (int)l[i].size(); j++) y_seg_l.insert(make_pair(yl[l[i][j]], yr[l[i][j]])); int nr = 0; int nyl = m + 1, nyr = 0; for (set<pair<int, int> >::iterator it = y_seg_l.begin(); it != y_seg_l.end(); it++) { if (it->first > nr + 1) nyl = qmin(nyl, nr + 1), nyr = qmax(nyr, it->first - 1); nr = qmax(nr, it->second); } if (nyr > 0) { xxl = qmin(xxl, xpos[i]); xxr = qmax(xxr, xpos[i + 1] - 1); } yyl = qmin(yyl, nyl); yyr = qmax(yyr, nyr); } if (xxr == 0) return 1; return ((xxr - xxl + 1) <= (now + now + 1)) && ((yyr - yyl + 1) <= (now + now + 1)); } int main() { ios::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < k; i++) cin >> x[i] >> y[i]; int lb = 0, ub = 1e9; while (lb <= ub) { int mid = (lb + ub) >> 1; if (check(mid)) ub = mid - 1; else lb = mid + 1; } cout << lb << endl; return 0; }
#include <bits/stdc++.h> namespace LYD { inline int read() { int x = 0, data = 1; char ch = getchar(); while ((ch > 9 || ch < 0 ) && ch != - ) ch = getchar(); if (ch == - ) { ch = getchar(); data = -1; } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * data; } } // namespace LYD int st[105], ans[105]; using namespace LYD; namespace solve {} using namespace solve; int main() { int n = read(), k = read(), tot = 0; for (int i = 1; i <= n; ++i) { int x = read(); if (st[x] == 0) { ++tot; st[x] = i; } } if (tot < k) { printf( NO ); return 0; } int num = 0, ans_num = 0; printf( YES n ); for (int i = 1; i <= k; ++i) { while (st[num] == 0) ++num; ++ans_num; ans[ans_num] = st[num]; ++num; } std ::sort(ans + 1, ans + ans_num + 1); for (int i = 1; i < ans_num; ++i) printf( %d , ans[i]); printf( %d , ans[ans_num]); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> rs_needed[1000000 + 5]; int arr[1000000 + 5]; int main() { int i, j, k, n, idx = 0, rs, lst_pos, cminus = 0, cplus = 0; long long int dv = 0, mn; scanf( %d , &n); for (i = 1; i <= n; i++) { scanf( %d , &arr[i]); dv += abs(arr[i] - i); } mn = dv; for (i = 1; i <= n; i++) { if (arr[i] > i) { rs_needed[arr[i] - i].push_back(arr[i]); cminus++; } else { rs_needed[n - i + arr[i]].push_back(arr[i]); cplus++; } } lst_pos = n; for (rs = 0; rs < n; rs++) { if (dv < mn) mn = dv, idx = rs; for (auto x : rs_needed[rs]) { cminus--; cplus++; } dv -= n - arr[lst_pos]; dv += arr[lst_pos]; lst_pos--; cplus--; cminus++; dv = dv + cplus - cminus; } printf( %lld %d n , mn, idx); return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); struct Item { int c; int x; Item() : c(), x() {} Item(int _c, int _x) : c(_c), x(_x) {} }; const long long INF = (long long)1e16; const int N = 505; int n, m, k; vector<Item> a[N][N]; vector<Item> b[N][N]; long long s[N][N]; long long dp[N][N]; int DX[] = {0, 1, 1, 0}; int DY[] = {0, -1, 0, 1}; int main() { scanf( %d%d%d , &n, &m, &k); while (k--) { int x, y, d, t, c; scanf( %d%d%d%d%d , &x, &y, &d, &t, &c); vector<pair<int, int> > cur; for (int i = 0; i < 4; i++) { int xx = x + d * DX[i], yy = y + d * DY[i]; int tt = xx + yy; if (tt < t || (tt - t) % 4 != i) continue; cur.push_back(make_pair(xx, yy)); } if ((int)cur.size() > 2) throw; for (int i = 0; i < (int)cur.size(); i++) { s[cur[i].first][cur[i].second] += c; if (i == 1) { if (cur[0].first == cur[1].first) a[cur[1].first][cur[1].second].push_back(Item(c, cur[0].second)); else if (cur[0].second == cur[1].second) b[cur[1].first][cur[1].second].push_back(Item(c, cur[0].first)); } } } for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) dp[x][y] = INF; dp[0][0] = 0; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { long long w; w = dp[x][y]; for (int xx = x + 1; xx < n; xx++) { for (Item I : b[xx][y]) { if (I.x >= x) w -= I.c; } w += s[xx][y]; dp[xx][y] = min(dp[xx][y], w); } w = dp[x][y]; for (int yy = y + 1; yy < m; yy++) { for (Item I : a[x][yy]) { if (I.x >= y) w -= I.c; } w += s[x][yy]; dp[x][yy] = min(dp[x][yy], w); } } printf( %lld n , dp[n - 1][m - 1]); return 0; }
#include <bits/stdc++.h> using namespace std; int n, ans = 0, fac[1000010], inv[1000010], sq[1000010], sl[1000010], sr[1000010]; char s[1000010]; template <class T> void read(T &x) { char ch; bool ok; for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == - ) ok = 1; for (x = 0; isdigit(ch); x = x * 10 + ch - 0 , ch = getchar()) ; if (ok) x = -x; } int c(int x, int y) { if (x < y || y < 0) return 0; return 1ll * fac[x] * inv[y] % 998244353 * inv[x - y] % 998244353; } int main() { scanf( %s , s + 1); n = strlen(s + 1); inv[0] = inv[1] = fac[0] = fac[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = 1ll * (998244353 - 998244353 / i) * inv[998244353 % i] % 998244353; for (int i = 2; i <= n; ++i) { inv[i] = 1ll * inv[i - 1] * inv[i] % 998244353; fac[i] = 1ll * fac[i - 1] * i % 998244353; } for (int i = 1; i <= n; ++i) sq[i] = sq[i - 1] + (s[i] == ? ); for (int i = 1; i <= n; ++i) sl[i] = sl[i - 1] + (s[i] == ( ); for (int i = n; i >= 1; --i) sr[i] = sr[i + 1] + (s[i] == ) ); for (int i = 1; i <= n; ++i) ans = (ans + 1ll * sl[i] * c(sq[n], sq[n] - sq[i] + sr[i + 1] - sl[i]) % 998244353 + 1ll * sq[i] * c(sq[n] - 1, sq[n] - sq[i] + sr[i + 1] - sl[i] - 1)) % 998244353; printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 200000; long long mem[MAXN + 1][2]; int visited[MAXN + 1][2]; int b[MAXN + 1][2]; int a[MAXN + 1]; int n; int input() { int i; scanf( %d , &n); for (i = 2; i <= n; i++) scanf( %d , &a[i]); return 0; } int search(int x, int k) { int i, tx; if (visited[x][k] == 1) { mem[x][k] = -1; return 0; } if (visited[x][k] == 2) { return 0; } if (x == 1 && k == 1) { b[x][k] = 1; mem[x][k] = 0; return 0; } mem[x][k] += a[x]; visited[x][k] = 1; tx = x; if (k) tx -= a[x]; else tx += a[x]; if (tx >= 1 && tx <= n) { search(tx, k ^ 1); if (mem[tx][k ^ 1] != -1) { mem[x][k] += mem[tx][k ^ 1]; b[x][k] = b[tx][k ^ 1]; } else mem[x][k] = -1; } visited[x][k] = 2; return 0; } int solve() { int i; memset(mem, 0, sizeof(mem)); memset(b, 0, sizeof(b)); memset(visited, 0, sizeof(visited)); visited[1][0] = 1; for (i = 2; i <= n; i++) { if (!visited[i][1]) search(i, 1); } for (i = 1; i < n; i++) { a[1] = i; a[0] = 0; if (mem[i + 1][1] == -1) printf( -1 n ); else printf( %I64d n , mem[i + 1][1] + i + a[b[i + 1][1]]); } return 0; } int main() { input(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; vector<int> w, cn, c, d, p; vector<bool> used; int find(int x) { return (p[x] == x ? x : p[x] = find(p[x])); } void un(int x, int y) { x = find(x); y = find(y); if (rand() & 1) p[x] = y; else p[y] = x; } int main() { int n, m; scanf( %d %d , &n, &m); used.resize(n, false); cn.resize(n + 1, 0); p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; int a, b, comp = n; for (int i = 0; i < m; ++i) { scanf( %d%d , &a, &b); a--; b--; if (find(a) != find(b)) un(a, b), comp--; } for (int i = 0; i < n; ++i) cn[find(p[i])]++; for (int i = 0; i < n; ++i) if (cn[i] > 0) c.push_back(cn[i]); cn.assign(n + 1, 0); for (int i = 0; i < c.size(); ++i) ++cn[c[i]]; c.clear(); for (int i = 0; i < cn.size(); ++i) if (cn[i] > 0) { int st = 1; while (cn[i] >= st) { c.push_back(st); w.push_back(st * i); cn[i] -= st; st *= 2; } if (cn[i] > 0) { c.push_back(cn[i]); w.push_back(cn[i] * i); } } int N = c.size(); d.resize(n + 1, INF); d[0] = 0; for (int i = 1; i <= N; ++i) for (int j = n; j >= w[i - 1]; --j) if (d[j - w[i - 1]] + c[i - 1] < d[j]) d[j] = d[j - w[i - 1]] + c[i - 1]; int ans = INF, tmp; for (int i = 4; i <= n; ++i) { if (ans <= d[i]) continue; tmp = i; bool good = true; while (tmp > 0 && good) { if (!(tmp % 10 == 4 || tmp % 10 == 7)) good = false; tmp /= 10; } if (good) ans = d[i]; } printf( %d , (ans < INF ? ans - 1 : -1)); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, d, i, j, m, l, t, sz = 1, a; long long cnt(long long p) { long long res = 1; for (i = 0; i < p; i++) res *= 2; return res; } int main() { cin >> t; for (int k = 0; k < t; k++) { long long h1 = 0, h0 = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a; if (a == 1) h1++; if (a == 0) h0++; } cout << h1 * cnt(h0) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1 << 21; const int N = int(1e5); const long long mod = int(1e9) + 7; const long long inf = 1e18; const double PI = 3.1415926536; inline int Set(int n, int pos) { return n = n | 1 << pos; } inline bool check(int n, int pos) { return n & 1 << pos; } inline int Reset(int n, int pos) { return n = n & ~(1 << pos); } inline int lastbit(int n) { return n & (-n); } inline long long mult_mod(long long a, long long b) { a = a % mod; b = b % mod; long long c = (a * b) % mod; return c; } inline long long add_mod(long long a, long long b) { a = (a + mod) % mod; b = (b + mod) % mod; long long c = (a + b) % mod; return c; } inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } inline long long power(long long a, long long b) { long long ans = 1; a = a % mod; while (b) { if (b & 1) ans = (ans * a) % mod; b >>= 1; a = (a * a) % mod; } return ans; } inline long long invmod(long long a) { long long ans = power(a, mod - 2); return ans; } inline unsigned long long mulmod(unsigned long long a, unsigned long long b, unsigned long long c) { unsigned long long x = 0, y = a % c; while (b > 0) { if (b % 2) x = (x + y) % c; y = (y * 2) % c; b >>= 1; } return x % c; } inline unsigned long long Largepower(unsigned long long a, unsigned long long b, unsigned long long c) { unsigned long long x = 1, y = a % c; while (b > 0) { if (b & 1) x = mulmod(x, y, c); y = mulmod(y, y, c); b >>= 1; } return x; } inline long long Egcd(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = Egcd(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } inline int bs(int arr[], int l, int r, int item) { if (l == r) { if (arr[l] == item) return r; else return -1; } if (item <= arr[(l + r) / 2]) return bs(arr, l, (l + r) / 2, item); else return bs(arr, (l + r) / 2 + 1, r, item); } inline long long nCrMod(long long n, long long r) { if (r > n / 2) r = n - r; long long ans = 1; for (long long i = 1; i <= r; i++) { ans = (ans % mod * (n - r + i + mod) % mod) % mod; ans = (ans % mod * invmod(i) % mod) % mod; } return ans % mod; } inline long long nCr(long long n, long long r) { if (r > n / 2) r = n - r; long long ans = 1; for (long long i = 1; i <= r; i++) { ans = (ans * (n - r + i)); ans /= i; } return ans; } inline void fastin(int &x) { x = 0; char c = getchar(); while (c < 0 || c > 9 ) c = getchar(); while (c >= 0 && c <= 9 ) { x = (x << 3) + (x << 1) + c - 0 ; c = getchar(); } return; } int leap[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nleap[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int a[2 * N + 1]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<int> v(2 * n); int odd = 0; int k = 0; for (int i = 1; i <= 2 * n; i += 2) { if (odd == 0) v[k++] = i; else v[k++] = i + 1; odd ^= 1; } odd = 0; for (int i = 2; i <= 2 * n; i += 2) { if (odd == 0) v[k++] = i; else v[k++] = i - 1; odd ^= 1; } int l = n - 1; long long s1 = v[0], s2 = v[0]; int i = 1, j = 2 * n - 1; while (l--) { s1 += v[i]; i++; } l = n - 1; while (l--) { s2 += v[j]; j--; } if (s1 == s2) { cout << YES n ; for (int i = 0; i < 2 * n; i++) cout << v[i] << ; } else cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; int getint() { unsigned int c; int x = 0; while (((c = getchar()) - 0 ) >= 10) { if (c == - ) return -getint(); if (!~c) exit(0); } do { x = (x << 3) + (x << 1) + (c - 0 ); } while (((c = getchar()) - 0 ) < 10); return x; } const int L = 222; const int G = 222; int nTour, needWin, initCap; double winProb[200]; int award[200]; double cache[L][L + G + 1][L]; double solve(int pos, int cap, int won) { double& res = cache[pos][cap][won]; if (res > -1) return res; if (pos == nTour) { if (won >= needWin) { if (cap >= G) { return res = 1; } else { return res = 0; } } else { return res = 0; } } if (award[pos] == -1) { res = winProb[pos] * solve(pos + 1, cap - 1, won + 1); } else { res = winProb[pos] * solve(pos + 1, min(L + G, cap + award[pos]), won + 1); } res += (1 - winProb[pos]) * solve(pos + 1, cap, won); return res; } int main() { int i, j, k, tcc, tc = 1 << 28; for (tcc = 0; tcc < tc; tcc++) { nTour = getint(); needWin = getint(); initCap = getint(); for (i = 0; i < nTour; i++) { winProb[i] = getint() / 100.0; } for (i = 0; i < nTour; i++) { award[i] = getint(); } for (i = 0; i < L; i++) for (j = 0; j < L + G + 1; j++) for (k = 0; k < L; k++) cache[i][j][k] = -1e40; double res = solve(0, initCap + G, 0); printf( %.10lf n , res); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n >> a >> b; int position[21] = {0}; for (int i = 0; i < n; i++) cin >> position[i]; int head = 0, tail = n - 1, cost = 0; while (head <= tail) { if (position[head] != 2 && position[tail] != 2 && (position[head] != position[tail])) { cost = -1; break; } if (position[head] == 2 && position[tail] == 2 && head != tail) { if (a > b) cost += 2 * b; else cost += 2 * a; } else if (position[head] == 2 && position[tail] == 2 && head == tail) { if (a > b) cost += b; else cost += a; } else if (position[head] == 2 || position[tail] == 2) { if (position[head] == 2 && position[tail] == 0) cost += a; else if (position[head] == 2 && position[tail] == 1) cost += b; else if (position[tail] == 2 && position[head] == 0) cost += a; else if (position[tail] == 2 && position[head] == 1) cost += b; } head++; tail--; } cout << cost; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, div = 1, ans = 0; cin >> n; long long int tt = log2(n); vector<long long int> w; if (log2(n) != tt) tt++; for (long long int i = 0; i < n; i++) w.push_back(i + 1); for (long long int j = 0; j < tt; j++) { vector<long long int> v, u; for (long long int i = 0; i < n;) { for (long long int j = i; j < i + div; j++) { if (j >= n) break; v.push_back(w[j]); } i += div; for (long long int j = i; j < i + div; j++) { if (j >= n) break; u.push_back(w[j]); } i += div; } cout << v.size() << << u.size() << ; for (long long int i = 0; i < v.size(); i++) cout << v[i] << ; for (long long int i = 0; i < u.size(); i++) cout << u[i] << ; cout << n ; long long int xxx; cin >> xxx; ans = max(xxx, ans); div *= 2; if (xxx == -1) { cout << ans; exit(0); } } cout << -1 << ans << n ; } }
#include <bits/stdc++.h> using namespace std; const int maxn = 200200; int n, k; int a[maxn]; vector<int> g[maxn]; int sz[maxn]; int dpd[maxn], dpu[maxn]; int mxd[maxn], mxu[maxn]; int mark[maxn], cnt[maxn]; int mxc[maxn], N; int ans; void dfs(int v, int par = -1) { sz[v] = 1; cnt[v] = mark[v]; int c = 0; for (int u : g[v]) if (u != par) { dfs(u, v); cnt[v] += cnt[u]; sz[v] += sz[u]; if (!cnt[u]) { dpd[v] += dpd[u]; mxd[v] += dpd[u]; } else c = max(c, mxd[u]); } if (mark[v]) dpd[v] = 0, mxd[v] = 0; else dpd[v]++, mxd[v] += c + 1; } void gfs(int v, int par = -1) { if (cnt[v] == N) dpu[v] = n - sz[v]; if (!mark[v]) { mxu[v] += mxc[v]; int d, c; if (cnt[v] == N) d = dpu[v] + 1, c = 0; else d = 1, c = mxu[v]; for (int u : g[v]) if (u != par) { mxu[u] += d; mxc[u] = max(mxc[u], c); if (!cnt[u]) d += dpd[u]; else c = max(c, mxd[u]); } reverse(g[v].begin(), g[v].end()); d = 0, c = 0; for (int u : g[v]) if (u != par) { mxu[u] += d; mxc[u] = max(mxc[u], c); if (!cnt[u]) d += dpd[u]; else c = max(c, mxd[u]); } } else dpu[v] = mxu[v] = 0; for (int u : g[v]) if (u != par) gfs(u, v); } bool check(int x) { N = 0; memset(sz, 0, sizeof sz); memset(dpd, 0, sizeof dpd); memset(dpu, 0, sizeof dpu); memset(mxc, 0, sizeof mxc); memset(mxd, 0, sizeof mxd); memset(mxu, 0, sizeof mxu); for (int i = 0; i < n; i++) mark[i] = (a[i] < x); for (int i = 0; i < n; i++) N += mark[i]; dfs(0); gfs(0); for (int i = 0; i < n; i++) if (!mark[i]) if (mxd[i] + dpu[i] >= k or mxu[i] + dpd[i] >= k) return true; return false; } void solve() { int l = 1, r = 1000001; int mid; while (l != r - 1) { mid = (l + r) >> 1; if (check(mid)) l = mid; else r = mid; } ans = l; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; for (int v, u, i = 0; i < n - 1; i++) { cin >> v >> u; v--, u--; g[v].push_back(u); g[u].push_back(v); } solve(); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; const int INF = (int)1e9 + 7; const long long LINF = (long long)9e18 + 7; const long long P1 = 353251; const long long P2 = 239017; const long long MOD = 1e9 + 7; const long long MOD1 = 1e9 + 7; const long long MOD2 = 1e9 + 9; int n, m; long long ans = 0; pair<long long, long long> a[107]; long long t[107]; const bool is_testing = 0; int main() { srand( D + E + N + I + S + S + O + N + time(NULL)); if (is_testing) { freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); } else { } cin >> n; for (int i = 0; i < n; i++) cin >> a[i].second >> a[i].first; cin >> m; for (int i = 0; i < m; i++) cin >> t[i]; long long sum = t[0]; for (int i = 1; i < m; i++) { t[i] -= sum; sum += t[i]; } t[m] = LINF; sort(a, a + n); int uk = 0; for (int i = 0; i < n; i++) { long long now = a[i].second; while (now) { if (now < t[uk]) { ans += now * a[i].first * (uk + 1); t[uk] -= now; now = 0; } else { now -= t[uk]; ans += t[uk] * a[i].first * (uk + 1); uk++; } } } cout << ans; }
#include <bits/stdc++.h> using namespace std; long long int dp[105][105][105]; long long int opti[105][105][105]; long long int lps[105]; string s, s1, s2, ans; long long int n, m, o; long long int rec(long long int i, long long int j, long long int k) { if (k >= o) return -1e16; if (i >= n || j >= m) return 0; if (dp[i][j][k] != -1) return dp[i][j][k]; long long int an = -1e16, x = 0; if (s[i] == s1[j]) { long long int y = k; if (s2[k] != s[i]) { y--; y = max(y, 0ll); y = lps[y]; while (y > 0) { if (s2[y] == s[i]) { y++; break; } y = lps[y - 1]; } if (y == 0 && s2[y] == s[i]) y++; } else y++; x = rec(i + 1, j + 1, y) + 1; if (x > an) { opti[i][j][k] = 1; an = x; } } x = rec(i + 1, j, k); if (x > an) { opti[i][j][k] = 2; an = x; } x = rec(i, j + 1, k); if (x > an) { opti[i][j][k] = 3; an = x; } return dp[i][j][k] = an; } void computeLPSArray() { int len = 0; lps[0] = 0; int i = 1; while (i < o) { if (s2[i] == s2[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } int main() { for (int i = 0; i <= 104; i++) for (int j = 0; j <= 104; j++) for (int k = 0; k <= 104; k++) { dp[i][j][k] = -1; opti[i][j][k] = -1; } cin >> s >> s1 >> s2; n = s.length(); m = s1.length(); o = s2.length(); computeLPSArray(); long long int an = rec(0, 0, 0); if (an <= 0) return cout << 0 << n , 0; long long int i = 0, j = 0, k = 0; ans = ; while (opti[i][j][k] != -1) { if (opti[i][j][k] == 1) { ans.push_back(s[i]); long long int y = k; if (s2[k] != s[i]) { y--; y = max(y, 0ll); y = lps[y]; while (y > 0) { if (s2[y] == s[i]) { y++; break; } y = lps[y - 1]; } if (y == 0 && s2[y] == s[i]) y++; } else y++; k = y; i++, j++; } else if (opti[i][j][k] == 2) i++; else j++; } cout << ans << n ; return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:16777216 ) using namespace std; template <typename T, int N> struct SuffixArray { T data[N + 1]; int result[N + 1], bucket[N + 1], count[N + 1], lcp[N + 1], n; pair<T, int> tmp[N + 1]; SuffixArray() : n(0) {} SuffixArray(T *source, int length) { memcpy(data, source, length * sizeof(T)); n = length; } inline void reset(int length) { n = length; } inline void reset(T *source, int length) { memcpy(data, source, length * sizeof(T)); n = length; } void build(bool cyclic = false, bool char_speedup = false) { bool clean_needed = false; if (!cyclic) { clean_needed = true; data[n++] |= (T)1 << (8 * sizeof(T) - 1); } for (int i = 0; i < n; ++i) result[i] = i; int max_group = 0; for (int current_length = 1; current_length < n * 2; current_length *= 2) { if (current_length == 1) { if (!char_speedup) { for (int i = 0; i < n; ++i) tmp[i] = make_pair(data[i], i); sort(tmp, tmp + n); for (int i = 0; i < n; ++i) result[i] = tmp[i].second; for (int i = 0; i < n; ++i) bucket[i] = data[i]; } else { int char_count[256]; memset(char_count, 0, 256 * sizeof(int)); int minimal_value = 0; if (clean_needed) minimal_value = data[n - 1]; else minimal_value = *min_element(data, data + n); for (int i = 0; i < n; ++i) ++char_count[data[i] - minimal_value]; for (int i = 1; i < 256; ++i) char_count[i] += char_count[i - 1]; for (int i = n - 1; i >= 0; --i) lcp[--char_count[data[i] - minimal_value]] = i; memcpy(result, lcp, n * sizeof(int)); for (int i = 0; i < n; ++i) bucket[i] = data[i]; } int total_count = 0; for (int i = 0; i < n;) { int j = i; int current_bucket = bucket[result[i]]; while (j < n && bucket[result[j]] == current_bucket) ++j; for (int z = i; z < j; ++z) bucket[result[z]] = total_count; ++total_count; i = j; } max_group = total_count; continue; } int prev_length = current_length / 2; for (int i = 0; i < n; ++i) result[i] = (result[i] - prev_length + n) % n; memset(count, 0, max_group * sizeof(int)); for (int i = 0; i < n; ++i) ++count[bucket[i]]; for (int i = 1; i < max_group; ++i) count[i] += count[i - 1]; for (int i = n - 1; i >= 0; --i) lcp[--count[bucket[result[i]]]] = result[i]; memcpy(result, lcp, n * sizeof(int)); int total_count = 0; for (int i = 0; i < n;) { int j = i; int current_bucket_first = bucket[result[i]], current_bucket_second = bucket[(result[i] + prev_length) % n]; while (j < n && bucket[result[j]] == current_bucket_first && bucket[(result[j] + prev_length) % n] == current_bucket_second) ++j; for (int z = i; z < j; ++z) lcp[result[z]] = total_count; ++total_count; i = j; } max_group = total_count; memcpy(bucket, lcp, n * sizeof(int)); } if (clean_needed) { --n; for (int i = 0; i < n; ++i) result[i] = result[i + 1]; } } void buildLCP() { for (int i = 0; i < n; ++i) count[result[i]] = i; int h = 0; lcp[0] = 0; for (int i = 0; i < n; ++i) { if (count[i]) { while (data[i + h] == data[result[count[i] - 1] + h]) ++h; lcp[count[i]] = h--; if (h < 0) h = 0; } } } }; int a, b, c, d, i, j, n, m, k; SuffixArray<char, 100002> q; char str[200202], ans[200002]; long long cs[200002]; inline long long sum(int l, int r) { if (!l) return cs[r]; else return cs[r] - cs[l - 1]; } int main() { scanf( %s , str); scanf( %d , &k); n = (int)strlen(str); if (k > (long long)n * (n + 1) / 2) { puts( No such line. ); exit(0); } --k; q.reset(str, n); q.build(false, true); a = 0; b = n; for (int _n((n)-1), i(0); i <= _n; i++) { cs[i] = n - q.result[i]; if (i) cs[i] += cs[i - 1]; } for (int _n((1000000000) - 1), len(0); len <= _n; len++) { if (k < 0 && len) { ans[len] = 0; puts(ans); exit(0); } for (int _n((26) - 1), i(0); i <= _n; i++) { char ch = a + i; int l = a, r = b; while (r - l > 1) { int xx = (r + l) / 2; if (str[q.result[xx] + len] > ch) r = xx; else l = xx; } int right = l; if (str[q.result[right] + len] > ch) --right; l = a, r = b; while (r - l > 1) { int xx = (r + l) / 2; if (str[q.result[xx] + len] >= ch) r = xx; else l = xx; } int left = l; if (str[q.result[left] + len] < ch) ++left; if (left > right) continue; long long cur = sum(left, right) - (long long)len * (right - left + 1); if (cur <= k) k -= cur; else { ans[len] = ch; a = left; b = right + 1; k -= b - a; break; } } } }
#include <bits/stdc++.h> using namespace std; char A[5] = { a , e , i , o , u }; int main() { int k; cin >> k; int rol = 0, col = 0; for (int i = 5; i <= sqrt(k); ++i) { if ((k % i == 0) && (k / i >= 5)) { rol = i; col = k / i; break; } } if (rol == 0) { cout << -1; } else { for (int i = 1; i <= rol; ++i) { for (int j = 0; j < col; ++j) { cout << A[(i + j) % 5]; } } } }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << << x << ; } void __print(const char *x) { cerr << << x << ; } void __print(const string &x) { cerr << << x << ; } void __print(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ; } template <typename T> void __print(const T &x) { int f = 0; cerr << { ; for (auto &i : x) cerr << (f++ ? , : ), __print(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << , ; _print(v...); } const int maxn = 105; int n; long long a[maxn], k; struct Matrix { long long val[maxn][maxn]; Matrix() { memset(val, 0, sizeof val); } Matrix operator*(const Matrix B) { Matrix C; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { for (int k = 1; k <= n; ++k) { C.val[i][j] = (C.val[i][j] + val[i][k] * B.val[k][j]) % 1000000007; } } } return C; } }; Matrix Mu(Matrix A, long long b) { Matrix C; for (int i = 1; i <= n; ++i) C.val[i][i] = 1; for (; b; b /= 2, A = A * A) { if (b % 2) C = C * A; } return C; } void Solve(int Test) { cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } Matrix T; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { long long x = a[i] ^ a[j]; if (__builtin_popcountll(x) % 3 == 0) T.val[i][j] = 1; } } Matrix B = Mu(T, k - 1); long long res = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { res = (res + B.val[i][j]) % 1000000007; } } cout << res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); if (fopen( CF .inp , r )) { freopen( CF .inp , r , stdin); freopen( CF .out , w , stdout); } int test = 1; for (int i = 1; i <= test; ++i) { Solve(i); } }
#include <bits/stdc++.h> int main(void) { int n; std::cin >> n; std::vector<std::string> a(n); for (int i = 0; i < n; ++i) { std::cin >> a[i]; } std::set<std::string> used; for (int i = n - 1; i >= 0; --i) { if (used.find(a[i]) == used.end()) { used.insert(a[i]); std::cout << a[i] << std::endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, l, r; cin >> s; long long a, b, n; cin >> a >> b; n = s.size(); long long ar[n]; long long br[n]; ar[n - 1] = -1; br[0] = -1; ar[0] = (s[0] - 0 ) % a; br[n - 1] = (s[n - 1] - 0 ) % b; for (int i = 1; i < n - 1; i++) { ar[i] = ((ar[i - 1] * 10) % a + (s[i] - 0 )) % a; } long long x = 10; for (int i = n - 2; i >= 1; i--) { br[i] = (br[i + 1] + ((s[i] - 0 ) * x)) % b; x = (x * 10) % b; } int d = -1; for (int i = 0; i < n; i++) { if (s[i + 1] == 0 ) continue; if (ar[i] == 0 && br[i + 1] == 0) { cout << YES << n ; d = i; break; } } if (d == -1) { cout << NO ; return 0; } cout << s.substr(0, d + 1) << n << s.substr(d + 1, n - d + 1); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int q, w, e, r, t; char a[51]; cin >> a; if (a[0] == h ) { cout << http:// ; w = 4; } else { cout << ftp:// ; w = 3; } cout << a[w]; w++; while ((a[w] != r ) || (a[w + 1] != u )) { cout << a[w]; w++; } cout << .ru ; w += 2; if (a[w]) { cout << / ; while (a[w]) { cout << a[w]; w++; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 3e+5; bool vis[MAX_N] = {0}; char t = . ; int main() { int n, m, ans = 0, cnt = 0; string str; cin >> n >> m >> str; for (int i = 0; i < n; i++) { if (str[i] == t) { ans++; if (i == 0 || str[i - 1] != t) cnt++; vis[i + 1] = 1; } } while (m--) { int i; char ch; cin >> i >> ch; bool a = vis[i], b = ch == t; if (a != b) { if (a) ans--; else ans++; if (vis[i - 1] && vis[i + 1] && !b) cnt++; else if (vis[i - 1] && vis[i + 1] && b) cnt--; else if (!vis[i - 1] && !vis[i + 1] && !b) cnt--; else if (!vis[i - 1] && !vis[i + 1] && b) cnt++; } vis[i] = b; cout << ans - cnt << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string lan1, lan2; map<string, string> link; for (int i = 0; i < m; ++i) { cin >> lan1 >> lan2; link[lan1] = lan2; } for (int j = 0; j < n; ++j) { cin >> lan1; if (lan1.size() <= link[lan1].size()) cout << lan1 << ; else cout << link[lan1] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int Mo = (int)1e9 + 7; vector<int> v[100005]; void add(int x, int y) { v[x].push_back(y); } int i, n, x, y, f[100005][2], A, B, g[2][2]; int inc(int x, int y) { x += y; while (x >= Mo) x -= Mo; while (x < 0) x += Mo; return x; } void dfs(int x) { int i; for (i = 0; i < v[x].size(); ++i) dfs(v[x][i]); f[x][1] = 1; f[x][0] = 0; memset(g, 0, sizeof(g)); g[0][0] = g[0][1] = 1; if (v[x].size() == 0) return; for (i = 0; i < v[x].size(); ++i) { int u = v[x][i]; A = inc(1ll * f[x][1] * f[u][1] % Mo, 1ll * f[x][0] * f[u][0] % Mo); B = inc(1ll * f[x][1] * f[u][0] % Mo, 1ll * f[x][0] * f[u][1] % Mo); f[x][0] = inc(f[x][0], A); f[x][1] = inc(f[x][1], B); } f[x][0] = inc(f[x][0], f[x][0]); f[x][1] = inc(f[x][1], f[x][1]); for (i = 0; i < v[x].size(); ++i) { int u = v[x][i]; A = inc(g[0][0], 1LL * f[u][0] * g[1][0] % Mo); B = inc(g[1][0], 1ll * f[u][0] * g[0][0] % Mo); g[0][0] = A; g[1][0] = B; A = inc(g[0][1], 1ll * f[u][1] * g[1][1] % Mo); B = inc(g[1][1], 1ll * f[u][1] * g[0][1] % Mo); g[0][1] = A; g[1][1] = B; } f[x][1] = inc(f[x][1], inc(-g[0][0], -g[1][0])); f[x][0] = inc(f[x][0], -g[1][1]); } int main() { scanf( %d , &n); for (i = 2; i <= n; ++i) scanf( %d , &x), add(x, i); dfs(1); printf( %d n , (f[1][0] + f[1][1]) % Mo); }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { int n, k; cin >> n >> k; string N = to_string(n); string ans = 99999999999999 ; set<char> S; for (auto c : N) { S.insert(c); } if ((int)S.size() <= k) { cout << N << n ; continue; } for (int d = 0; d < (int)N.size(); d++) { string base = N.substr(0, d); for (int v = N[d] - 0 + 1; v <= 9; v++) { string s = base + to_string(v); set<char> have; for (int i = 0; i < d; i++) { have.insert(N[i]); } have.insert((char)(v + 0 )); if ((int)have.size() > k) { continue; } if ((int)have.size() < k) { have.insert( 0 ); } while (s.size() < N.size()) { s += *have.begin(); } assert(s >= N); ans = min(ans, s); } } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e15 + 88; const int maxn = (int)1e6 + 88; const int mod = 1000000007; long long a[maxn]; long long b[maxn]; long long myabs(long long a) { if (a > 0) return a; else return -a; } int main() { long long n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i <= m; i++) { cin >> b[i]; } sort(b + 1, b + m + 1); b[0] = -inf; b[m + 1] = inf; long long dd; long long Max = 0; for (int i = 0; i < n; i++) { long long l = 0, r = m + 2; while (l <= r) { long long mid = (l + r) / 2; if (a[i] == b[mid]) { l = mid; break; } else if (b[mid] < a[i]) l = mid + 1; else r = mid - 1; } dd = min(myabs(b[l] - a[i]), myabs(b[l - 1] - a[i])); Max = max(Max, dd); } cout << Max; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, w, h; struct Point { int d, p, id; bool operator<(const Point& r) const { return p < r.p; } }; vector<Point> px[N], py[N]; int ansx[N], ansy[N], tmpx[N], tmpy[N]; int main() { scanf( %d%d%d , &n, &w, &h); for (int i = 0; i < n; ++i) { int g, p, t; scanf( %d%d%d , &g, &p, &t); if (g == 1) px[p - t + 100000].push_back({0, p, i}); else py[p - t + 100000].push_back({1, p, i}); } for (int i = 0; i < N; ++i) sort(px[i].begin(), px[i].end()); for (int i = 0; i < N; ++i) sort(py[i].begin(), py[i].end()); for (int i = 0; i < N; ++i) { int cur = 0; for (int j = 0; j < px[i].size(); ++j) tmpx[cur] = px[i][j].p, tmpy[cur] = h, ++cur; for (int j = (int)py[i].size() - 1; j >= 0; --j) tmpx[cur] = w, tmpy[cur] = py[i][j].p, ++cur; cur = 0; for (int j = (int)py[i].size() - 1; j >= 0; --j) ansx[py[i][j].id] = tmpx[cur], ansy[py[i][j].id] = tmpy[cur], ++cur; for (int j = 0; j < px[i].size(); ++j) ansx[px[i][j].id] = tmpx[cur], ansy[px[i][j].id] = tmpy[cur], ++cur; } for (int i = 0; i < n; ++i) printf( %d %d n , ansx[i], ansy[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, t; cin >> t; while (t--) { int count = 0; int ans; cin >> n; for (int i = 1; i <= 1666; i++) { if ((i % 10 != 3) && i % 3 != 0) { count++; } if (count == n) { ans = i; break; } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> Arr; int main() { int N, K; cin >> N >> K; int Num, Acum; int i, j; for (i = 0; i < N; i++) { cin >> Num; Arr.push_back(Num); } sort(Arr.begin(), Arr.end()); j = 0; Acum = 0; for (i = 0; i < K; i++) { if (j >= Arr.size()) { cout << 0 << n ; continue; } Arr[j] -= Acum; cout << Arr[j] << n ; Acum += Arr[j]; Arr[j] = 0; while (Arr[j] <= Acum && j < Arr.size()) j += 1; } }
#include <bits/stdc++.h> using namespace std; int n, P, S; int s, t; vector<int> res1, res2; struct node { int to; int cap; int opp; int cost; }; vector<node> v[3500]; int d[3500]; bool in[3500]; int pre[3500], wh[3500]; void add(int x, int y, int cap, int cost) { node tmp; tmp.to = y; tmp.cap = cap; tmp.cost = cost; tmp.opp = v[y].size(); v[x].push_back(tmp); tmp.to = x; tmp.cap = 0; tmp.cost = -cost; tmp.opp = v[x].size() - 1; v[y].push_back(tmp); } bool spfa() { memset(in, 0, sizeof(in)); for (int i = s; i <= t; i++) d[i] = -199912310; queue<int> q; q.push(s); d[s] = 0; while (!q.empty()) { int x = q.front(); q.pop(); in[x] = false; for (int i = 0; i < (int)v[x].size(); i++) { if (v[x][i].cap <= 0) continue; if (d[v[x][i].to] < d[x] + v[x][i].cost) { d[v[x][i].to] = d[x] + v[x][i].cost; pre[v[x][i].to] = x; wh[v[x][i].to] = i; if (!in[v[x][i].to]) { in[v[x][i].to] = true; q.push(v[x][i].to); } } } } return d[t] != -199912310; } long long max_flow() { long long ret = 0; while (spfa()) { int flow = 199912310; for (int i = t; i; i = pre[i]) { flow = min(flow, v[pre[i]][wh[i]].cap); } ret += 1ll * flow * d[t]; for (int i = t; i; i = pre[i]) { v[pre[i]][wh[i]].cap -= flow; v[i][v[pre[i]][wh[i]].opp].cap += flow; } } return ret; } int main() { scanf( %d , &n); scanf( %d%d , &P, &S); s = 0; t = n + 3; for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); add(i, n + 1, 1, x); } for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); add(i, n + 2, 1, x); add(0, i, 1, 0); } add(n + 1, t, P, 0); add(n + 2, t, S, 0); printf( %d n , max_flow()); for (int i = 1; i <= n; i++) { for (int j = 0; j < (int)v[i].size(); j++) { if (v[i][j].to == n + 1 && v[i][j].cap == 0) { res1.push_back(i); } if (v[i][j].to == n + 2 && v[i][j].cap == 0) { res2.push_back(i); } } } for (int i = 0; i < (int)res1.size(); i++) printf( %I64d , res1[i]); puts( ); for (int i = 0; i < (int)res2.size(); i++) printf( %d , res2[i]); puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a; cin >> b; sort(a.begin(), a.end()); for (int i = 0; i < a.size(); i++) { if (a[i] != 0 && a[0] == 0 ) { swap(a[i], a[0]); break; } } if (a == b) cout << OK << endl; else cout << WRONG_ANSWER << endl; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, x; cin >> n >> x; long long int a[n]; set<long long int> s; long long int sum = 0; for (long long int i = 0; i < n; i++) { cin >> a[i]; s.insert(a[i]); sum += a[i]; } long long int y = *(s.begin()); if (s.size() == 1 && y == x) { cout << 0 << n ; return; } if (s.find(x) != s.end()) { cout << 1 << n ; return; } if (sum == n * x) { cout << 1 << n ; return; } cout << 2 << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cout.precision(20); long long int t = 1; cin >> t; for (long long int i = 1; i <= t; i++) { if (0) cout << Case # << i << : ; solve(); } }
#include <bits/stdc++.h> using namespace std; #pragma optimize( -O3 ) #define f first #define s second #define pb push_back #define endl n map<vector<int>, int>ma; map<vector<int>, bool>che; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); queue<vector<int>>q; vector<int>v; int n; cin>>n; for(int i=1; i<=2*n; i++){ v.pb(i); } q.push(v); ma[v]=0; while(!q.empty()){ vector<int>u=q.front(); q.pop(); if(che[u]){ continue; } che[u]=1; vector<int>v1; vector<int>v2; v1.resize(2*n); v2.resize(2*n); for(int i=0; i<u.size(); i++){ if(i%2==0){ v1[i+1]=u[i]; } else{ v1[i-1]=u[i]; } } for(int i=0; i<n; i++){ v2[i+n]=u[i]; } for(int i=n; i<2*n; i++){ v2[i-n]=u[i]; } int d1=ma[u]; int d2=ma[v1]; int d3=ma[v2]; if(d2>1+d1 || !che[v1]){ ma[v1]=1+d1; q.push(v1); } if(d3>1+d1 || !che[v2]){ ma[v2]=1+d1; q.push(v2); } } vector<int>vec; for(int i=1; i<=2*n; i++){ int x; cin>>x; vec.pb(x); } if(!che[vec]){ cout<<-1; } else{ cout<<ma[vec]; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; int Power(int n, int x) { int mul = n; int res = 1; while (x > 0) { if (x & 1) res = 1ll * res * mul % MOD; mul = 1ll * mul * mul % MOD; x /= 2; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> n; vector<int> a(n + 1), pref(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(begin(a) + 1, end(a), greater<int>()); for (int i = 1; i <= n; i++) { pref[i] = (a[i] + pref[i - 1]) % MOD; } vector<int> ans; for (int k = 1; k <= n; k++) { int res = 0; for (int i = 0, j = 0; i <= n; i += k, j++) { res = (res + (1ll * j * ((pref[min(i + k, n)] - pref[i]) % MOD) % MOD)) % MOD; } ans.emplace_back(1ll * res * Power(n, MOD - 2) % MOD); } for (auto i : ans) { if (i < 0) i += MOD; cout << i << ; } cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; bool odd(int N) { while (N < 0) { N += 2; } return N % 2 == 1 ? true : false; } bool even(int N) { while (N < 0) { N += 2; } return N % 2 == 0 ? true : false; } int main() { string s, t; cin >> s >> t; int N = s.size(); string result = ; int num = 0; for (int i = 0; i != N; ++i) { if (s[i] != t[i]) { ++num; if (num % 2) { result += s[i]; } else { result += t[i]; } } else { result += s[i]; } } if (even(num)) { cout << result; } else { cout << impossible ; } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { long long x, y; } A[100005]; int pos1[100005], pos2[100005], pos3[100005], pos4[100005]; int cmp1(int a, int b) { return A[a].x < A[b].x; } int cmp2(int a, int b) { return A[a].x > A[b].x; } int cmp3(int a, int b) { return A[a].y < A[b].y; } int cmp4(int a, int b) { return A[a].y > A[b].y; } int mark[100005]; int main() { int i, n, k, p1, p2, p3, p4; long long x1, x2, y1, y2; scanf( %d%d , &n, &k); for (i = 1; i <= n; ++i) { scanf( %lld%lld%lld%lld , &x1, &y1, &x2, &y2); A[i].x = x1 + x2; A[i].y = y1 + y2; pos1[i] = pos2[i] = pos3[i] = pos4[i] = i; } sort(pos1 + 1, pos1 + n + 1, cmp1); sort(pos2 + 1, pos2 + n + 1, cmp2); sort(pos3 + 1, pos3 + n + 1, cmp3); sort(pos4 + 1, pos4 + n + 1, cmp4); int now = 0, tot; long long ans = 1LL << 62; for (p1 = 0; p1 <= k; ++p1) { for (p2 = 0; p2 <= k; ++p2) { for (p3 = 0; p3 <= k; ++p3) { for (p4 = 0; p4 <= k; ++p4) { now++; tot = 0; for (i = 1; i <= p1; ++i) if (mark[pos1[i]] != now) mark[pos1[i]] = now, tot++; for (i = 1; i <= p2; ++i) if (mark[pos2[i]] != now) mark[pos2[i]] = now, tot++; for (i = 1; i <= p3; ++i) if (mark[pos3[i]] != now) mark[pos3[i]] = now, tot++; for (i = 1; i <= p4; ++i) if (mark[pos4[i]] != now) mark[pos4[i]] = now, tot++; if (tot != k) continue; long long max_x = -0x7fffffff, min_x = 0x7fffffff, max_y = -0x7fffffff, min_y = 0x7fffffff; for (i = 1; i <= n; ++i) if (mark[i] != now) { max_x = (max_x > A[i].x ? max_x : A[i].x); min_x = (min_x > A[i].x ? A[i].x : min_x); max_y = (max_y > A[i].y ? max_y : A[i].y); min_y = (min_y > A[i].y ? A[i].y : min_y); } long long x = max_x - min_x, y = max_y - min_y; x = (2LL > x ? 2LL : x); y = (2LL > y ? 2LL : y); ans = (ans > x * y ? x * y : ans); } } } } printf( %lld n , ans / 4); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> using v2d = vector<vector<T> >; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int maxN = 1e3 + 10; const int maxK = 1e3 + 10; const long long mod = 998244353; int n, k, a[maxN], lowbound[maxN], upbound[maxN]; long long dp[maxN][maxK], res = 0; inline void normalize(long long &a) { a = (a % mod + mod) % mod; } long long Try(int dist) { long long r = 0; for (int i = 1; i <= (int)(n); ++i) { lowbound[i] = lowbound[i - 1]; while (a[i] - a[lowbound[i] + 1] >= dist) { ++lowbound[i]; } upbound[i] = upbound[i - 1]; while (a[i] - a[upbound[i] + 1] > dist) { ++upbound[i]; } } for (int i = 1; i <= (int)(n); ++i) { dp[i][1] = dp[i - 1][1] + 1; for (int j = (int)(2); j <= (int)(k); ++j) { dp[i][j] = dp[lowbound[i]][j - 1] + dp[i - 1][j]; if (dp[i][j] >= mod) { dp[i][j] -= mod; } } } r = dp[n][k]; for (int i = 1; i <= (int)(n); ++i) { dp[i][1] = dp[i - 1][1] + 1; for (int j = (int)(2); j <= (int)(k); ++j) { dp[i][j] = dp[upbound[i]][j - 1] + dp[i - 1][j]; if (dp[i][j] >= mod) { dp[i][j] -= mod; } } } r -= dp[n][k]; if (r < 0) { r += mod; } return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= (int)(n); ++i) { cin >> a[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= (int)(a[n] / (k - 1)); ++i) { res += i * Try(i) % mod; if (res >= mod) { res -= mod; } } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int maxn = 1e6 + 5; const int mul = 9997; long long h[maxn]; long long p[maxn]; string s; vector<int> Z() { int L = 0, R = 0; vector<int> z(s.size()); z[0] = s.size(); for (int i = 1; i < s.size(); i++) { if (i <= R) { z[i] = z[i - L]; } else { L = i; if (i > R) { R = i; } while (R < z[0] && s[R - L] == s[R]) R++; z[i] = (R--) - L; } } return z; } void go() { int n, k; cin >> n >> k; cin >> s; auto z = Z(); int f = 1; int cnt = 0; for (int i = 0; i < k; i++) { int pos; cin >> pos; cnt += max(0, pos - f); if (pos - f < 0 && z[z.size() - f + pos] < f - pos) { cout << 0 << n ; return; } f = pos + s.size(); } cnt += n + 1 - f; long long ans = 1; for (int i = 0; i < cnt; i++) { ans *= 26; ans %= mod; } cout << ans << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int c = 0; int t; if (!c) { t = 1; } else { cin >> t; } while (t--) { go(); } }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << << x << ; } void __print(const char *x) { cerr << << x << ; } void __print(const string &x) { cerr << << x << ; } void __print(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ; } template <typename T> void __print(const T &x) { int f = 0; cerr << { ; for (auto &i : x) cerr << (f++ ? , : ), __print(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << , ; _print(v...); } string s[105]; long long tot(long long i, long long j) { long long ans = 0; if (s[i][j] == 1 ) ans++; if (s[i + 1][j] == 1 ) ans++; if (s[i][j + 1] == 1 ) ans++; if (s[i + 1][j + 1] == 1 ) ans++; return ans; } vector<long long> calc(long long i, long long j, long long cnt) { vector<pair<long long, long long> > st1, st0; vector<long long> res; if (s[i][j] == 1 ) st1.push_back({i, j}); else st0.push_back({i, j}); if (s[i + 1][j] == 1 ) st1.push_back({i + 1, j}); else st0.push_back({i + 1, j}); if (s[i][j + 1] == 1 ) st1.push_back({i, j + 1}); else st0.push_back({i, j + 1}); if (s[i + 1][j + 1] == 1 ) st1.push_back({i + 1, j + 1}); else st0.push_back({i + 1, j + 1}); if (cnt == 1 || cnt == 2) { for (long long k = 0; k < 1; k++) { long long x = st1[k].first; long long y = st1[k].second; res.push_back(x); res.push_back(y); s[x][y] = 0 ; } for (long long k = 0; k < 2; k++) { long long x = st0[k].first; long long y = st0[k].second; res.push_back(x); res.push_back(y); s[x][y] = 1 ; } } else if (cnt == 3 || cnt == 4) { for (long long k = 0; k < 3; k++) { long long x = st1[k].first; long long y = st1[k].second; res.push_back(x); res.push_back(y); s[x][y] = 0 ; } } return res; } void solve() { long long n, m; cin >> n >> m; for (long long i = 0; i < n; i++) cin >> s[i]; vector<vector<long long> > ans; if (n & 1) { for (long long i = 0; i < m; i += 2) { long long x = (n - 1), y = i; if (i == (m - 1)) y = (i - 1); vector<long long> res; if (s[x][y] == 1 || s[x][y + 1] == 1 ) { if (s[x][y] == 1 && s[x][y + 1] == 1 ) { res.push_back(x); res.push_back(y); s[x][y] = 0 ; res.push_back(x); res.push_back(y + 1); s[x][y + 1] = 0 ; res.push_back(x - 1); res.push_back(y); s[x - 1][y] = (!(s[x - 1][y] - 0 )) + 0 ; } else if (s[x][y] == 1 ) { res.push_back(x); res.push_back(y); s[x][y] = 0 ; res.push_back(x - 1); res.push_back(y); s[x - 1][y] = (!(s[x - 1][y] - 0 )) + 0 ; res.push_back(x - 1); res.push_back(y + 1); s[x - 1][y + 1] = (!(s[x - 1][y + 1] - 0 )) + 0 ; } else if (s[x][y + 1] == 1 ) { res.push_back(x); res.push_back(y + 1); s[x][y + 1] = 0 ; res.push_back(x - 1); res.push_back(y); s[x - 1][y] = (!(s[x - 1][y] - 0 )) + 0 ; res.push_back(x - 1); res.push_back(y + 1); s[x - 1][y + 1] = (!(s[x - 1][y + 1] - 0 )) + 0 ; } } if (res.size() > 0) ans.push_back(res); } n--; } if (m & 1) { for (long long i = 0; i < n; i += 2) { long long x = i, y = (m - 1); if (i == (n - 1)) x = (i - 1); vector<long long> res; if (s[x][y] == 1 || s[x + 1][y] == 1 ) { if (s[x][y] == 1 && s[x + 1][y] == 1 ) { res.push_back(x); res.push_back(y); s[x][y] = 0 ; res.push_back(x + 1); res.push_back(y); s[x + 1][y] = 0 ; res.push_back(x); res.push_back(y - 1); s[x][y - 1] = (!(s[x][y - 1] - 0 )) + 0 ; } else if (s[x][y] == 1 ) { res.push_back(x); res.push_back(y); s[x][y] = 0 ; res.push_back(x); res.push_back(y - 1); s[x][y - 1] = (!(s[x][y - 1] - 0 )) + 0 ; res.push_back(x + 1); res.push_back(y - 1); s[x + 1][y - 1] = (!(s[x + 1][y - 1] - 0 )) + 0 ; } else if (s[x + 1][y] == 1 ) { res.push_back(x + 1); res.push_back(y); s[x + 1][y] = 0 ; res.push_back(x); res.push_back(y - 1); s[x][y - 1] = (!(s[x][y - 1] - 0 )) + 0 ; res.push_back(x + 1); res.push_back(y - 1); s[x + 1][y - 1] = (!(s[x + 1][y - 1] - 0 )) + 0 ; } } if (res.size() > 0) ans.push_back(res); } m--; } for (long long i = 0; i < (n); i += 2) { for (long long j = 0; j < (m); j += 2) { long long cnt = tot(i, j); while (cnt > 0) { vector<long long> res = calc(i, j, cnt); cnt = tot(i, j); ans.push_back(res); } } } cout << ans.size() << endl; for (long long i = 0; i < ans.size(); i++) { for (long long j = 0; j < 6; j++) { cout << ans[i][j] + 1 << ; } cout << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; for (long long i = 0; i < t; i++) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; for (int u = 0; u < t; u++) { long long int n; cin >> n; n = 2 * n; long long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long int b = a[(n / 2) - 1]; long long int c = a[(n / 2)]; cout << abs(b - c) << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; const int D = 200 + 1; int n; pair<int, int> a[N]; int cn[N]; int cd[D]; long long sum, ans; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].first; cn[a[i].first]++; } for (int i = 0; i < n; i++) { cin >> a[i].second; sum += a[i].second; } sort(a, a + n); int it = 0; ans = (long long)1e18; for (int i = 0; i < N && it < n; i++) { if (a[it].first > i) continue; int ln = 0; long long ls = 0; for (int j = 0; j < D; j++) { int cnt = min(cd[j], max((it - ln) - cn[i] + 1, 0)); ln += cnt, ls += j * cnt; } while (it < n && a[it].first == i) { sum -= a[it].second; cd[a[it].second]++; it++; } ans = min(ans, ls + sum); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<int> v[100086]; int x, y; int deg[100086], mxd; int ans[100086 << 3][2], cnt; void dfs(int i, int fa, int val) { int x = deg[i] - (i != 1); int fin = val - 1; ans[++cnt][0] = i, ans[cnt][1] = val; for (int j = 0; j < v[i].size(); j++) { if (v[i][j] == fa) continue; --x; if (val + 1 > mxd) val = fin - x - 1, ans[++cnt][0] = i, ans[cnt][1] = val; dfs(v[i][j], i, ++val); ans[++cnt][0] = i, ans[cnt][1] = val; } if ((i ^ 1) && val != fin) ans[++cnt][0] = i, ans[cnt][1] = fin; } int main() { scanf( %d , &n); for (int i = 1; i < n; i++) { scanf( %d%d , &x, &y); v[x].push_back(y), v[y].push_back(x); deg[x]++, deg[y]++; } for (int i = 1; i <= n; i++) mxd = max(mxd, deg[i]); dfs(1, 0, 0); printf( %d n , cnt); for (int i = 1; i <= cnt; i++) printf( %d %d n , ans[i][0], ans[i][1]); }
#include <bits/stdc++.h> using namespace std; long long n, arr[100003], pre[100003], suf[100003]; int bitidx; bool cmp(long long a, long long b) { return ((a >> bitidx) & 1) < ((b >> bitidx) & 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; pre[0] = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; pre[i + 1] = pre[i] ^ arr[i]; } suf[0] = 0; for (int i = 0; i < n; i++) suf[i + 1] = suf[i] ^ arr[n - i - 1]; sort(suf, suf + n + 1); long long mx = 0; for (int k = 0; k <= n; k += 1) { int st = 0, en = n; for (int i = 40; i >= 0; i -= 1) { int bit = (pre[k] >> i) & 1; bitidx = i; int pos = lower_bound(suf + st, suf + en + 1, (1LL << i), cmp) - suf; if (pos > en) continue; if (!bit) st = pos; else en = pos - 1; } mx = max(mx, pre[k] ^ suf[st]); } cout << mx; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> gra[28]; char s[100005], re[100005]; int main() { int i, l, j, st, l1; while (scanf( %s , &s) == 1) { l = strlen(s); l1 = 0; for (i = 0; i < 26; i++) gra[i].clear(); for (i = 0; i < l; i++) gra[s[i] - a ].push_back(i); st = -1; for (i = 25; i >= 0; i--) if (gra[i].size() > 0) { for (j = 0; j < gra[i].size(); j++) if (gra[i][j] > st) re[l1++] = a + i, st = gra[i][j]; } re[l1] = 0 ; printf( %s n , re); } return 0; }
#include <bits/stdc++.h> using namespace std; int N, M; int A[200], B[200], D[200]; map<int, vector<int>> MM; set<int> S; struct mat { signed long long v[150][3] = {}; }; struct vec { signed long long v[3] = {}; }; void print(mat a, int n) { int x, y; for (x = 0; x < n; x++) { (void)printf( %d: , x); for (y = 0; y < n; y++) (void)printf( %d , (a.v[x][y / 64] & (1LL << (y % 64))) != 0); (void)printf( n ); } } void print(vec a, int n) { int i; for (i = 0; i < n; i++) (void)printf( %d , (a.v[i / 64] & (1LL << (i % 64))) != 0); (void)printf( n ); } mat mult(mat a, mat b, int n) { mat c, d; int x, y, z; for (x = 0; x < n; x++) for (y = 0; y < n; y++) if (b.v[y][x / 64] & (1LL << (x % 64))) c.v[x][y / 64] |= 1LL << (y % 64); for (x = 0; x < n; x++) for (y = 0; y < n; y++) { signed long long v = 0; for (z = 0; z < (n + 63) / 64; z++) v |= a.v[x][z] & c.v[y][z]; v = (v != 0); d.v[x][y / 64] |= v << (y % 64); } return d; } mat init(int n) { mat r; int x; for (x = 0; x < n; x++) r.v[x][x / 64] |= 1LL << (x % 64); return r; } mat powm(mat a, int p, int n) { mat r = init(n); while (p) { if (p % 2) r = mult(r, a, n); a = mult(a, a, n); p /= 2; } return r; } vec mult(mat a, vec b, int n) { vec c; int x, y; for (x = 0; x < n; x++) if (b.v[x / 64] & (1LL << (x % 64))) for (y = 0; y < (n + 63) / 64; y++) c.v[y] |= a.v[x][y]; return c; } bool reachable(mat a, vec b) { int x, y; for (x = 0; x < 150; x++) { vec c = mult(a, b, N); b.v[0] |= c.v[0]; b.v[1] |= c.v[1]; b.v[2] |= c.v[2]; } int n = N - 1; return (b.v[n / 64] & (1LL << (n % 64))) != 0; } void solve() { int i, j, k, l, r, x, y; string s; cin >> N >> M; for (i = 0; i < M; i++) { cin >> A[i] >> B[i] >> D[i]; A[i]--; B[i]--; MM[D[i]].push_back(i); S.insert(D[i]); } int cur = 0; vec v; mat m; v.v[0] = 1; for (auto& r : MM) { v = mult(powm(m, r.first - cur, N), v, N); cur = r.first; for (auto& r2 : r.second) m.v[A[r2]][B[r2] / 64] |= 1LL << (B[r2] % 64); if (reachable(m, v)) break; } if (!reachable(m, v)) return (void)printf( Impossible n ); int n = N - 1; while ((v.v[n / 64] & (1LL << (n % 64))) == 0) { v = mult(m, v, N); cur++; if (S.count(cur)) for (auto& r2 : MM[cur]) m.v[A[r2]][B[r2] / 64] |= 1LL << (B[r2] % 64); } cout << cur << endl; } int main(int argc, char** argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false); for (i = 0; i < argc - 1; i++) s += argv[i + 1], s += n ; for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; bool isPrime(long long int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long long int p = sqrt(n); for (int i = 5; i <= p; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } string d2b(int n) { string s; for (int i = 31; i >= 0; i--) { int k = n >> i; if (k & 1) s += 1 ; else s += 0 ; } return s; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return (a * b) / gcd(a, b); } long long int b2d(string n) { string num = n; long long int dec_value = 0; long long int base = 1; long long int len = num.length(); for (int i = len - 1; i >= 0; i--) { if (num[i] == 1 ) dec_value += base; base = base * 2; } return dec_value; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test_case = 1; cin >> test_case; while (test_case--) { int n, k; cin >> n >> k; int a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (long long i = 1; i < n; i++) { a[i] = k - a[i]; } int temp = a[0]; for (long long i = 1; i < n; i++) { a[i] = a[i] / temp; } int s = 0; for (long long i = 1; i < n; i++) s += a[i]; cout << s << endl; } }
#include <bits/stdc++.h> int *ej[100000], eo[100000]; long long *ew[100000]; void append(int i, int j, long long w) { int o = eo[i]++; if (o >= 2 && (o & o - 1) == 0) { ej[i] = (int *)realloc(ej[i], o * 2 * sizeof *ej[i]); ew[i] = (long long *)realloc(ew[i], o * 2 * sizeof *ew[i]); } ej[i][o] = j, ew[i][o] = w; } long long bb[60], b_; int sz; void add(long long b) { int l; b_ |= b; for (l = 0; l < 60; l++) if (b & 1LL << l) { if (bb[l] & 1LL << l) b ^= bb[l]; else { bb[l] = b, sz++; return; } } } long long xx[100000]; char visited[100000]; int kk[60], cnt; void dfs(int p, int i, long long x) { int o, l; if (visited[i]) { add(xx[i] ^ x); return; } xx[i] = x, visited[i] = 1, cnt++; for (l = 0; l < 60; l++) if (x & 1LL << l) kk[l]++; for (o = eo[i]; o--;) { int j = ej[i][o]; long long w = ew[i][o]; if (j != p) dfs(i, j, x ^ w); } } int main() { int n, m, i, j, ans; scanf( %d%d , &n, &m); for (i = 0; i < n; i++) { ej[i] = (int *)malloc(2 * sizeof *ej[i]); ew[i] = (long long *)malloc(2 * sizeof *ew[i]); } while (m--) { long long w; scanf( %d%d%lld , &i, &j, &w), i--, j--; append(i, j, w), append(j, i, w); } ans = 0; for (i = 0; i < n; i++) if (!visited[i]) { int l, p; memset(bb, 0, 60 * sizeof *bb), b_ = 0, sz = 0; memset(kk, 0, 60 * sizeof *kk); cnt = 0; dfs(-1, i, 0); p = 1; for (l = 0; l < 60; l++) { int k; if (b_ & 1LL << l) k = (long long)cnt * (cnt - 1) / 2 % 1000000007 * ((1LL << sz - 1) % 1000000007) % 1000000007; else k = (long long)kk[l] * (cnt - kk[l]) % 1000000007 * ((1LL << sz) % 1000000007) % 1000000007; ans = (ans + (long long)k * p) % 1000000007; p = p * 2 % 1000000007; } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T min(T a, T b, T c, T d) { return min(a, min(b, min(c, d))); } template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } template <class T> T max(T a, T b, T c, T d) { return max(a, max(b, max(c, d))); } bool cmp(const pair<int, int>& a, const pair<int, int>& b) { return (a.first > b.first || (a.first == b.first && a.second >= b.second)); } long long GCD(long long a, long long b) { return (a % b) ? GCD(b, a % b) : b; } int f[7][100007]; int main() { int n, a, b, c; scanf( %d%d%d%d , &n, &a, &b, &c); --n; f[0][0] = 0; f[1][0] = f[2][0] = 1000000000LL; for (int i = (1); i <= (n); ++i) { f[0][i] = min(f[2][i - 1] + b, f[1][i - 1] + a); f[1][i] = min(f[2][i - 1] + c, f[0][i - 1] + a); f[2][i] = min(f[1][i - 1] + c, f[0][i - 1] + b); } cout << min(f[0][n], f[1][n], f[2][n]) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL); long long tests = 1; cin >> tests; while (tests--) { long long n, k; cin >> n >> k; map<pair<long long, long long>, long long> timers; set<pair<long long, long long> > bombsx, bombsy; for (long long i = 1; i <= n; ++i) { long long x, y, t; cin >> x >> y >> t; bombsx.insert({x, y}); bombsy.insert({y, x}); timers[{x, y}] = t; } vector<long long> min_time; queue<pair<long long, long long> > nodes; while (bombsx.size()) { auto itr = bombsx.begin(); long long min_timer = timers[{itr->first, itr->second}]; nodes.push({itr->first, itr->second}); bombsx.erase({itr->first, itr->second}); bombsy.erase({itr->second, itr->first}); while (nodes.size()) { long long x = nodes.front().first, y = nodes.front().second; nodes.pop(); itr = bombsx.lower_bound({x, y - k}); while (itr != bombsx.end() and itr->first == x and abs(itr->second - y) <= k) { min_timer = min(min_timer, timers[{itr->first, itr->second}]); auto aux = itr; itr++; nodes.push({aux->first, aux->second}); bombsx.erase({aux->first, aux->second}); bombsy.erase({aux->second, aux->first}); } itr = bombsy.lower_bound({y, x - k}); while (itr != bombsy.end() and itr->first == y and abs(itr->second - x) <= k) { min_timer = min(min_timer, timers[{itr->second, itr->first}]); auto aux = itr; itr++; nodes.push({aux->second, aux->first}); bombsy.erase({aux->first, aux->second}); bombsx.erase({aux->second, aux->first}); } } min_time.push_back(min_timer); } sort(min_time.begin(), min_time.end()); long long ans = min_time.size() - 1; n = min_time.size(); for (long long i = 0; i < n; ++i) { ans = min(ans, max(min_time[i], n - 2ll - i)); } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double PI = acos(-1.0); const long long int mod = 1e9 + 7; const int MAXN = 1e6 + 5; void cp() { int n, k; string s; cin >> n >> k >> s; vector<pair<int, int> > group; int l = 0, r = 0; while (l < n) { while (l < n && s[l] == L ) l++; if (l >= n) break; r = l; while (r + 1 < n && s[r + 1] == W ) r++; group.push_back({l, r}); l = r + 1; } vector<pair<int, int> > diff; for (int i = 0; i < ((int)group.size()) - 1; i++) { int d = group[i + 1].first - group[i].second - 1; diff.push_back({d, i}); } sort(diff.rbegin(), diff.rend()); while (k > 0 && !diff.empty()) { pair<int, int> last = diff.back(); diff.pop_back(); int can = min(last.first, k); k -= can; for (int i = group[last.second].second + 1; i < group[last.second + 1].first && can > 0; i++, can--) s[i] = W ; } if (k > 0) { int pos = -2; for (int i = 0; i < n; i++) if (s[i] == W ) pos = i; pos++; if (pos >= 0 && pos < n) for (int i = pos; i < n && k > 0; i++) if (s[i] == L ) s[i] = W , k--; } if (k > 0 && s[0] == L ) { int pos = n; for (int i = 0; i < n && pos == n; i++) if (s[i] == W ) pos = i; pos--; if (pos >= 0) { for (int i = pos; i >= 0 && k > 0; i--) if (s[i] == L ) s[i] = W , k--; } } int score = 0; for (int i = 0; i < n; i++) { score += (s[i] == W ); if (i - 1 >= 0 && s[i] == W && s[i - 1] == W ) score++; } cout << score << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; cin >> t; while (t--) { cp(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int inf = 1e9 + 9; const long long int MOD = 1e9 + 696969; const long long INF = 4LL * 1000000LL * 1000000LL * 1000000LL; template <class T> int size(T &&x) { return int(x.size()); } template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &p) { return out << ( << p.first << , << p.second << ) ; } template <class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << { ; for (auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? : , ); return out << } ; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << ; ; dump(args...); } mt19937_64 rng(0); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } inline void addmod(int &x, int val) { x += val; while (x >= mod) x -= mod; } const int maxn = 300100; int dr[maxn]; int n, m, tab[maxn]; int N; int temp[maxn]; inline void add(int p, int val) { for (; p <= n; p += p & (-p)) dr[p] += val; } inline int get(int p) { if (p == 0) return 0; int res = 0; for (; p > 0; p -= p & (-p)) res += dr[p]; return res; } map<long long, long long> M; vector<pair<int, int> > skal; int cnt = 0; long long inversions(int base) { if (M.find(base) != M.end()) return M[base]; skal.resize(n); ++cnt; for (int i = (1); i <= (n); ++i) skal[i - 1] = make_pair(tab[i] ^ base, i); sort(skal.begin(), skal.end()); for (int i = 0; i < n; ++i) temp[skal[i].second] = i + 1; long long res = 0; fill(dr, dr + n + 1, 0); for (int i = n; i > 0; --i) { res += get(temp[i]); add(temp[i], 1); } M[base] = res; return res; } void solveone() { cin >> n; for (int i = (1); i <= (n); ++i) { cin >> tab[i]; } int b = 0; for (int bit = 29; bit >= 0; --bit) { if (inversions(b) > inversions(b + (1 << bit))) b += (1 << bit); } cout << inversions(b) << << b << n ; } int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; int tests; tests = 1; for (int _ = (1); _ <= (tests); ++_) solveone(); }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, mp; cin >> n >> m >> k; mp = n / 2 + 1; if (n % 2 == 0 || mp > m) { cout << 0; return 0; } int ans = 0, i, a[10005]; for (i = 0; i < n; i++) cin >> a[i]; int min = a[0]; for (i = 0; i < n; i++) if (i % 2 == 0) if (a[i] < min) min = a[i]; for (i = 0; i < k; i++) { int num = m / mp; if (min == 0) break; if (num <= min) { min -= num; ans += num; } else { ans += min; min = 0; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; bool is_Palindrome(string s) { long long int n = s.size(); for (long long int i = 0; i < n / 2; i++) { if (s[i] != s[n - i - 1]) { return 0; } } return 1; } void solve() { string a; cin >> a; long long int n = a.size(); if (is_Palindrome(a)) { cout << 0 << endl; return; } cout << 3 << endl; cout << L << << 2 << endl << R << << 2 << endl << R << << (2 * n - 1); } void testcase() { int t; cin >> t; while (t--) { solve(); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; void solve() { long long int a, b; cin >> a >> b; long long int c = 0; if (a % b == 0) cout << 0 << endl; if (a % b != 0) { long long int ans = b - (a % b); cout << ans << endl; } } int main() { int t; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const long long N = 1000000007; void solve() { string s; cin >> s; long long ans = INT_MAX; for (long long i = 0; i <= 9; i++) { long long count = 0; for (long long j = 0; j < (long long)s.size(); j++) { if (s[j] == (char)( 0 + i)) count++; } ans = min(ans, (long long)s.size() - count); } if (ans == 0) { cout << 0 << endl; return; } for (long long i = 0; i <= 9; i++) { for (long long j = 0; j <= 9; j++) { string temp; if (i == j) continue; temp += (char)( 0 + i); temp += (char)( 0 + j); long long count = 0; long long n = (long long)s.size(); long long curr = 0; for (long long k = 0; k < n; k++) { if (s[k] == temp[curr]) { if (curr == 1) { count++; } curr = (curr + 1) % 2; } } ans = min(ans, (long long)s.size() - 2 * count); } } cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MX_N = (1e3) + 3; int a[MX_N][MX_N]; int main() { int n; scanf( %d , &n); if (n == 4) puts( -1 ); else { for (int i = 0; i < n; ++i) { for (int j = i - 2; j >= 0; j -= 2) a[i][j] = 1; for (int j = i + 1; j < n; j += 2) a[i][j] = 1; } if (!(n & 1)) { a[0][n - 1] = 0; a[n - 1][0] = 1; a[0][2] = 1; a[2][0] = 0; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j) printf( ); printf( %d , a[i][j]); } printf( n ); } } return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <queue> #include <deque> #include <bitset> #include <iterator> #include <list> #include <stack> #include <map> #include <set> #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <climits> #include <iterator> using namespace std; typedef long long ll; const ll mod=1e9+7; const ll N=2e5+1; const ll INF=1e10; vector<vector<int>> adj(N); vector<bool> visited(N); vector<int> parent(N),root(N); pair<int,int> backedge; void dfs(int cur){ visited[cur]=true; for(int next:adj[cur]){ if(!visited[next]){ parent[next]=cur; dfs(next); } else if(parent[cur]!=next) backedge={cur,next}; } } void solve(){ map<int,int> subtree; int n; cin>>n; for(int i=0;i<n;i++){ int u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1); //cout<<backedge.first<< <<backedge.second<< n ;/* for(int i=0;i<adj[backedge.first].size();i++){ if(adj[backedge.first][i]==backedge.second){ adj[backedge.first][i]=adj[backedge.first].back(); adj[backedge.first].pop_back(); //cout<< done ; break; } } for(int i=0;i<adj[backedge.second].size();i++){ if(adj[backedge.second][i]==backedge.first){ adj[backedge.second][i]=adj[backedge.second].back(); adj[backedge.second].pop_back(); //cout<< done ; break; } } //cout<< done ;/* for(int i=1;i<=n;i++) parent[i]=-1,visited[i]=false; parent[backedge.first]=0; dfs(backedge.first); for(int i=1;i<=n;i++) visited[i]=false; queue<int> q; q.push(backedge.second); root[backedge.second]=backedge.second; visited[q.back()]=true; while(parent[q.back()]){ //cout<<q.back()<< ; visited[parent[q.back()]]=true; q.push(parent[q.back()]); root[q.back()]=q.back(); } visited[backedge.first]=true; root[backedge.first]=backedge.first; ll c=q.size(); while(!q.empty()){ int t=q.front(); q.pop(); for(int i:adj[t]){ if(!visited[i]){ root[i]=root[t]; subtree[root[i]]++; visited[i]=true; q.push(i); } } } //for(auto i:subtree) cout<<i.first<< <<i.second<< n ; ll ans1=0,ans2=0,sum=0; for(auto i:subtree){ ans1+=(ll)i.second*(i.second+1)/2; ans2+=i.second*sum; sum+=i.second; } ans1+=2*ans2+2*(c-1)*(n-c)+c*(c-1); cout<<ans1<< n ; for(int i=1;i<=n;i++){ adj[i].clear(); visited[i]=false; parent[i]=0; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); int t; cin>>t; while(t--) solve(); }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, ans = 0; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int a, b; cin >> a >> b; ; if (a == 1 || b == 1) ans++; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, l, r, cnt; long long ans; char s[101][10005]; int dfs(int x, int y, int d) { if (x == n - 1) return 1; if (s[x + 1][y] == . ) { l = y; r = y; ans++; cnt = 0; return dfs(x + 1, y, d); } if (s[x][y + d] == . ) { ans++; r = max(r, y + d); l = min(l, y + d); return dfs(x, y + d, d); } else if (s[x][y + d] == # ) { if (cnt > 2 * m) return 0; ans += 1; cnt++; if (d == 1) { ans += y - l; r = y; return dfs(x, l, -1); } else { ans += r - y; l = y; return dfs(x, r, 1); } } else { s[x][y + d] = . ; ans++; if (d == 1) { ans += y - l; r = y + 1; return dfs(x, l, -1); } else { ans += r - y; l = y - 1; return dfs(x, r, 1); } } } int main() { int i, j, k, t; while (~scanf( %d%d , &n, &m)) { m++; for (i = 0; i < n; i++) scanf( %s , s[i] + 1), s[i][0] = s[i][m] = # ; l = r = 1; ans = 0; j = dfs(0, 1, 1); if (j) printf( %I64d n , ans); else puts( Never ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int arr[3]; cin >> arr[0] >> arr[1] >> arr[2]; sort(arr, arr + 3); int d = arr[1] - arr[0]; d += arr[2] - arr[1]; cout << d; return 0; }