solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; int arr[MAXN]; int dp[MAXN]; int main() { int n, m, d; cin >> n >> d; for (int i = 0; i < n; i++) cin >> arr[i]; cin >> m; sort(arr, arr + n); dp[0] = arr[0]; for (int i = 1; i < n; i++) { dp[i] = arr[i] + dp[i - 1]; cerr << dp[i] << " "; } cerr << '\n'; if (m < n) { cout << dp[m - 1] << " " << '\n'; return 0; } if (m == n) { cout << dp[m - 1] << " " << '\n'; return 0; } cout << dp[n - 1] - (m - n) * d << " " << '\n'; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, i, j, k, l, x, y; cin >> n >> m; long long int a[n + 5], b[m + 5]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < m; i++) cin >> b[i]; x = 0, y = 0, k = 0, l = 0; for (i = 0; i < n; i++) { x += a[i]; while (x > y) y += b[l++]; if (x == y) { ++k; x = y = 0; } } cout << k << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> v[2003]; int depth[2003], root[2003]; int ans = 0, n; void dfs(int x, int y) { depth[x] = y; for (int i = 0; i < v[x].size(); i++) { dfs(v[x][i], y + 1); } } int main() { int i, j, a; cin >> n; for (i = 1; i <= n; i++) { cin >> a; if (a == -1) root[i] = 1; else v[a].push_back(i); } for (i = 1; i <= n; i++) { if (root[i]) dfs(i, 1); } for (i = 1; i <= n; i++) { ans = max(ans, depth[i]); } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, a[100005], b[100005], ans[100005], x; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &x), b[x] = i; for (int i = 0; i < n; i++) { ans[b[a[i]]] = i + 1; } for (int i = 0; i < n; i++) printf("%d ", ans[i]); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 5000005; const int oo = 1000000007; int t, l, r, n, prime[N]; long long f[N], p[N]; void calc() { long long ans = 0; for (int i = l; i <= r; ++i) { f[i] %= oo; ans += (p[i - l] * f[i]) % oo; ans %= oo; } cout << ans; } int main() { cin >> t >> l >> r; n = r; for (int i = 2; i <= n; ++i) prime[i] = i; for (int i = 2; i <= n; ++i) if (prime[i] == i) for (int j = i; j <= n; j += i) prime[j] = i; p[0] = 1; for (int i = 1; i <= r - l; ++i) p[i] = (p[i - 1] * t) % oo; for (int i = 2; i <= n; ++i) { f[i] = (long long)oo * oo; if (prime[i] == i) f[i] = (long long)i * (i - 1) / 2; else { int divisor = 0, x = i; while (x != 1) { int j = prime[x]; if (divisor != j) f[i] = min(f[i], (long long)i * (j - 1) / 2 + f[i / j]); x /= j; } } } calc(); }
4
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define pii pair<ll,ll> using namespace std; ll arr[2005],chk[2005]; vector<pii> ans; vector<ll> ch[1000005]; void solve(){ ll i,j,k,a,b,c,n; scanf("%lld",&n); for(i=1;i<=2*n;i++){ scanf("%lld",&arr[i]); } sort(arr+1,arr+n*2+1); for(i=2*n-1;i>=1;i--){ for(j=1;j<=2*n;j++)ch[arr[j]].clear(); for(j=1;j<=2*n;j++){ ch[arr[j]].push_back(j); } for(j=1;j<=2*n;j++)chk[j]=0; chk[i]=1;a=arr[n*2]; ans.clear(); c=0;ans.push_back({arr[2*n],arr[i]}); for(j=2*n-1;j>=1;j--){ if(chk[j])continue; c=1;chk[j]=1; while(ch[a-arr[j]].size()){ b=ch[a-arr[j]].back(); //printf("%lld %lld %lld %lld\n",i,j,b,a-arr[j]); if(chk[b]){ ch[a-arr[j]].pop_back(); } else{ //printf("%lld %lld %lld\n",i,j,b); c=0;break; } } if(c)break; b=ch[a-arr[j]].back(); chk[j]=chk[b]=1; a=arr[j]; ans.push_back({arr[j],arr[b]}); } if(c==0){ printf("YES\n%lld\n",arr[2*n]+arr[i]); for(auto ii : ans){ printf("%lld %lld\n",ii.fi,ii.se); } for(j=1;j<=2*n;j++)ch[arr[j]].clear(); return; } } for(j=1;j<=2*n;j++)ch[arr[j]].clear(); printf("NO\n"); } int main(){ int T; scanf("%d",&T); while(T--){ solve(); } }
3
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int n, m, o, p, answer; int64_t sum, pre; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sum = pre = answer = p = 0; for (int i = 0; i < m; i++) { scanf("%d", &o); sum += o; while (p <= n) { if (pre > sum) { break; } if (pre == sum) { answer++; pre = sum = 0; break; } if (pre < sum) { pre += a[p++]; } } } printf("%d\n", answer); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int Mo = (int)1e9 + 7; long long i, j, m, n, p, k, Mp, Max; long long f[101][10001], c[101][101], G[101][101][2]; long long power(long long x, long long y) { long long sum = 1; if (!x) return 1; for (; y; y >>= 1) { if (y & 1) sum = 1ll * sum * x % Mo; x = 1ll * x * x % Mo; } return sum; } int main() { scanf("%I64d%I64d%I64d", &n, &m, &k); for (i = 1; i <= 100; i++) for (j = 1; j <= i; j++) { c[i][j] = 1; for (p = 2; p <= i; p++) (c[i][j] *= p) %= Mo; for (p = 2; p <= j; p++) (c[i][j] *= power(p, Mo - 2)) %= Mo; for (p = 2; p <= i - j; p++) (c[i][j] *= power(p, Mo - 2)) %= Mo; } for (i = 0; i <= 100; i++) for (j = 0; j <= 100; j++) for (p = 0; p < 2; p++) G[i][j][p] = 1; for (i = 1; i <= 100; i++) for (j = 1; j <= i; j++) G[i][j][0] = power(c[i][j], m / n), G[i][j][1] = power(c[i][j], m / n + 1); f[0][0] = 1; for (i = 1; i <= n; i++) for (j = 0, Mp = min(i * n, k); j <= Mp; j++) for (p = 0, Max = min(j, n); p <= Max; p++) if (f[i - 1][j - p] != 0) (f[i][j] += f[i - 1][j - p] * G[n][p][m % n >= i ? 1 : 0]) %= Mo; printf("%I64d\n", f[n][k]); }
2
#include <bits/stdc++.h> using namespace std; long long bigmod(long long a, long long b) { if (b == 0) return 1; if (!(b & 1)) { long long ret = bigmod(a, b / 2); return ((ret % 1000000007) * (ret % 1000000007)) % 1000000007; } else return ((a % 1000000007) * (bigmod(a, b - 1) % 1000000007)) % 1000000007; } bool cmp(pair<int, int> p1, pair<int, int> p2) { if (p1.first != p2.first) return p1.first < p1.first; return p1.second < p2.second; } bool approximatelyEqual(float a, float b, float epsilon) { return fabs(a - b) <= ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool essentiallyEqual(float a, float b, float epsilon) { return fabs(a - b) <= ((fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool definitelyGreaterThan(float a, float b, float epsilon) { return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool definitelyLessThan(float a, float b, float epsilon) { return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); } int main() { int t, tc = 1, a[100000], c, m, n, i, j, k, p, q; int b[105] = {0}; m = -100; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); b[a[i]]++; m = max(b[a[i]], m); } printf("%d\n", m); return 0; }
1
#include <bits/stdc++.h> using namespace std; inline void R(int &x) { x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } inline void R(long long &x) { x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } void Redirect() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } int a[5050]; int l[5050]; int r[5050]; int ans[5050]; int n; template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } bool is_sqr(long long x, long long y) { if (sgn(x) != sgn(y) && sgn(x) && sgn(y)) return 0; int rt = sqrt((long long)x * y) + 0.5; return (long long)rt * rt == (long long)x * y; } int main() { R(n); for (int i = 1; i <= n; i += 1) { l[i] = -1; R(a[i]); } for (int i = 1; i <= n; ++i) { for (int j = i - 1; j; --j) { if (a[j] == 0) continue; if (is_sqr(a[i], a[j])) { l[i] = j; break; } } } for (int i = 1; i <= n; ++i) { int cur = 0; int fz = 0; bool all_zero = true; for (int j = i; j <= n; ++j) { all_zero &= (a[j] == 0); if (all_zero) cur = 1; else { if (!fz) { fz = j; cur = 1; } else { if (l[j] < fz) cur++; } } ans[cur]++; } } for (int i = 1; i <= n; ++i) { printf("%d ", ans[i]); } puts(""); }
4
#include <bits/stdc++.h> using namespace std; long long t, tt, i, j, x, y, tot; int main() { cin >> t; for (tt = 1; tt <= t; tt++) { cin >> x >> y; j = 1; tot = 0; while ((x != 0) || (y != 0)) { if ((x % 2) != (y % 2)) tot = tot + j; j = j * 2; x = x / 2; y = y / 2; } cout << tot << endl; } }
1
#include <bits/stdc++.h> using namespace std; int pw(int a, int b, int mod) { int r = 1; while (b) { if (b & 1) { r = (long long)r * a % mod; } a = (long long)a * a % mod; b /= 2; } return r; } int dv(int a, int b, int mod) { return (long long)a * pw(b, mod - 2, mod) % mod; } const int C = 2; const int mods[] = {1000000271, 1000000787, 1000000931}; struct T { int a[C]; T() { for (int j = 0; j < C; j++) { a[j] = 0; } } }; bool operator<(T f, T s) { for (int i = 0; i < C; i++) { if (f.a[i] != s.a[i]) { return f.a[i] < s.a[i]; } } return 0; } T operator+(T f, T s) { for (int j = 0; j < C; j++) { f.a[j] += s.a[j]; if (f.a[j] >= mods[j]) { f.a[j] -= mods[j]; } } return f; } T operator-(T f, T s) { for (int j = 0; j < C; j++) { f.a[j] -= s.a[j]; if (f.a[j] < 0) { f.a[j] += mods[j]; } } return f; } T operator*(T f, T s) { for (int j = 0; j < C; j++) { f.a[j] = (long long)f.a[j] * s.a[j] % mods[j]; } return f; } T operator^(T f, int x) { for (int j = 0; j < C; j++) { f.a[j] = pw(f.a[j], x, mods[j]); } return f; } T operator/(T f, T s) { for (int j = 0; j < C; j++) { f.a[j] = dv(f.a[j], s.a[j], mods[j]); } return f; } bool operator==(T f, T s) { for (int j = 0; j < C; j++) { if (f.a[j] != s.a[j]) { return 0; } } return 1; } const int K = (int)1e6 + 7; T kwk[K], iki[K]; struct magma { vector<T> lol; int ln; void init(string s) { ln = (int)s.size(); lol.resize(ln + 1); for (int j = 0; j < ln; j++) { T now; for (int k = 0; k < C; k++) { now.a[k] = s[j] - 'a'; } lol[j + 1] = lol[j] + now * kwk[j + 1]; } } T get(int l, int r) { l++; r++; T sol = lol[r] - lol[l - 1]; sol = sol * iki[l - 1]; return sol; } T get_skip(int skp, int i) { if (i < skp) { return get(0, i); } else { T a = get(0, skp - 1); T b = get(skp + 1, i + 1); return a + b * kwk[skp]; } } T get_pos(int skp, int i) { if (i < skp) { return get(i, i); } else { return get(i + 1, i + 1); } } }; void upd(vector<int> x) { for (auto &it : x) { if (it == -1) { return; } } vector<int> y = x; sort(y.begin(), y.end()); for (int i = 1; i < (int)y.size(); i++) { if (y[i] == y[i - 1]) { return; } } cout << "YES\n"; for (auto &it : x) { cout << it << " "; } cout << "\n"; exit(0); } vector<int> get_sorted(string s) { int n = (int)s.size(); vector<int> nd(n); nd[n - 1] = n - 1; for (int i = n - 2; i >= 0; i--) { if (s[i] == s[i + 1]) { nd[i] = nd[i + 1]; } else { nd[i] = i + 1; } } vector<int> sol(n); int l = 0, r = n - 1; for (int i = 0; i < n; i++) { if (s[nd[i]] <= s[i]) { sol[l++] = i; } else { sol[r--] = i; } } vector<int> ret; for (auto &it : sol) { ret.push_back(it); if (it == n - 1) { ret.push_back(n); } } return ret; } vector<int> get_sorted_smart(string s) { int n = (int)s.size(); vector<pair<string, int>> vec; vec.push_back({s, n}); for (int j = 0; j < n; j++) { string t; for (int i = 0; i < n; i++) { if (i != j) { t += s[i]; } } vec.push_back({t, j}); } sort(vec.begin(), vec.end()); vector<int> sol; for (auto &it : vec) { sol.push_back(it.second); } return sol; } const int N = (int)1e5 + 7; const int MOD = (int)1e9 + 7; int n; string a[N]; magma b[N]; vector<int> so[N]; int comp(int i, int skp1, int skp2) { int ln1 = (int)a[i].size() - (skp1 < (int)a[i].size()); int ln2 = (int)a[i + 1].size() - (skp2 < (int)a[i + 1].size()); int com = 0, lo = 1, hi = min(ln1, ln2); while (lo <= hi) { int mid = (lo + hi) / 2; if (b[i].get_skip(skp1, mid - 1) == b[i + 1].get_skip(skp2, mid - 1)) { com = mid; lo = mid + 1; } else { hi = mid - 1; } } if (com == min(ln1, ln2)) { if (ln1 == ln2) { return 0; } if (ln1 < ln2) { return 1; } else { return 2; } } if (b[i].get_pos(skp1, com) < b[i + 1].get_pos(skp2, com)) { return 1; } else { return 2; } } int main() { ios::sync_with_stdio(0); cin.tie(0); T p26; for (int j = 0; j < C; j++) { p26.a[j] = 26; kwk[0].a[j] = 1; } for (int j = 1; j < K; j++) { kwk[j] = kwk[j - 1] * p26; } iki[K - 1] = kwk[0] / kwk[K - 1]; for (int j = K - 2; j >= 0; j--) { iki[j] = iki[j + 1] * kwk[1]; } cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; so[i] = get_sorted(a[i]); b[i].init(a[i]); } vector<int> dp((int)so[1].size(), 1); for (int i = 2; i <= n; i++) { vector<int> ndp((int)so[i].size(), 0); int pos = 0, sum = 0; for (int k = 0; k < (int)so[i].size(); k++) { while (pos < (int)so[i - 1].size() && comp(i - 1, so[i - 1][pos], so[i][k]) <= 1) { sum = (sum + dp[pos]) % MOD; pos++; } ndp[k] = sum; } dp = ndp; } int sol = 0; for (auto &it : dp) { sol = (sol + it) % MOD; } cout << sol << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int s = 0, p = 0, m = 0, i, j, k, l; string s1, s2, s3; cin >> s1 >> s2 >> s3; if (s1 > s2) swap(s1, s2); if (s2 > s3) swap(s2, s3); if (s1 > s2) swap(s1, s2); if (s1 == s2 && s2 == s3) { cout << "0"; return 0; } if (s1[0] + 1 == s2[0] && s2[0] + 1 == s3[0] && (s1[1] == s2[1] && s2[1] == s3[1])) { cout << "0"; return 0; } if (s1 == s2 || s2 == s3) { cout << "1"; return 0; } if (s1[1] == s2[1] && (s1[0] + 1 == s2[0] || s1[0] + 2 == s2[0])) { cout << "1"; return 0; } if (s2[1] == s3[1] && (s2[0] + 1 == s3[0] || s2[0] + 2 == s3[0])) { cout << "1"; return 0; } if (s1[1] == s3[1] && (s1[0] + 1 == s3[0] || s1[0] + 2 == s3[0])) { cout << "1"; return 0; } cout << "2"; return 0; }
2
#include <bits/stdc++.h> using namespace std; char c[] = {'Q', 'R', 'U', 'O', 'P', 'S', 'D', 'G', 'J', 'C', 'B'}; string s; int main() { cin >> s; int nr = 0; for (int i = 0; s[i]; i++) for (int j = 0; c[j]; j++) if (s[i] == c[j]) { nr++; } if (nr == 0 || nr == s.size()) cout << "YES"; else cout << "NO"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int a[5]; while(cin >> a[0] && a[0]) { for(int i=1; i<5; i++) cin >> a[i]; set<int> s; for(int i=0; i<5; i++) { a[i]--; s.insert(a[i]); } for(int i=0; i<5; i++) { if(s.size()==1||s.size()==3) cout << 3 << endl; else { if(s.count((a[i]+1)%3)) cout << 1 << endl; else cout << 2 << endl; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { long long int n; cin >> n; if (n % 2 == 0) { for (long long int i = 0; i < n / 2; i++) cout << '1'; cout << '\n'; } else { cout << '7'; for (long long int i = 0; i < (n / 2 - 1); i++) cout << '1'; cout << '\n'; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:255000000") bool firstout = 1; template <class T> T &minn(T &a, T b) { if (b < a) a = b; return a; } template <class T> T &maxx(T &a, T b) { if (a < b) a = b; return a; } int &madd(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } int &msub(int &a, int b) { a -= b; if (a < 0) a += 1000000007; return a; } int &mmult(int &a, int b) { return a = (long long)a * b % 1000000007; } int mdiv(long long a, long long b, long long m) { a = (a % m + m) % m; b = (b % m + m) % m; if (a % b == 0) return a / b; return (a + m * mdiv(-a, m, b)) / b; } int n, m, q; int A[3012][3012], H[3012][3012], HH[3012][3012], V[3012][3012], VV[3012][3012]; int X[101234], Y[101234], XX[101234], YY[101234]; bool DD[3012][3012], DU[3012][3012], UD[3012][3012], UU[3012][3012]; int main() { int i, j, k; char c; int a, d; int ts; for (ts = 1; scanf("%" "d", &(n)) > 0; ++ts) { for (i = (0); i < (3012); ++i) for (j = (0); j < (3012); ++j) A[i][j] = H[i][j] = HH[i][j] = V[i][j] = VV[i][j] = DD[i][j] = DU[i][j] = UD[i][j] = UU[i][j] = 0; for (i = (0); i < (n); ++i) scanf( "%" "d", &(X[i])), scanf( "%" "d", &(Y[i])), scanf( "%" "d", &(XX[i])), scanf( "%" "d", &(YY[i])); for (k = (0); k < (n); ++k) { int x, y, xx, yy; x = X[k]; y = Y[k]; xx = XX[k]; yy = YY[k]; for (i = (x); i < (xx); ++i) for (j = (y); j < (yy); ++j) A[i + 1][j + 1] = 1; DD[x][y] = 1; UD[xx][y] = 1; DU[x][yy] = 1; UU[xx][yy] = 1; for (i = (x); i < (xx); ++i) H[i + 1][y] = 1, HH[i + 1][yy] = 1; for (j = (y); j < (yy); ++j) V[x][j + 1] = 1, VV[xx][j + 1] = 1; } for (i = (1); i < (3012); ++i) for (j = (1); j < (3012); ++j) A[i][j] += A[i - 1][j] + A[i][j - 1] - A[i - 1][j - 1]; for (j = (0); j < (3012); ++j) for (i = (1); i < (3012); ++i) H[i][j] += H[i - 1][j], HH[i][j] += HH[i - 1][j]; for (i = (0); i < (3012); ++i) for (j = (1); j < (3012); ++j) V[i][j] += V[i][j - 1], VV[i][j] += VV[i][j - 1]; int ii, jj, l; for (i = (0); i < (3012); ++i) { for (j = (0); j < (3012); ++j) if (DD[i][j]) { for (jj = (j + 1); jj < (3012); ++jj) if (DU[i][jj]) { l = jj - j; ii = i + l; if (UD[ii][j] && UU[ii][jj] && V[i][jj] - V[i][j] == l && VV[ii][jj] - VV[ii][j] == l && H[ii][j] - H[i][j] == l && HH[ii][jj] - HH[i][jj] == l && A[ii][jj] - A[i][jj] - A[ii][j] + A[i][j] == ((l) * (l))) break; } if (jj < 3012) break; } if (j < 3012) break; } if (i == 3012) printf( "%" "s", ("NO")), printf("\n"), firstout = 1; else { printf((firstout) ? "%" "s" : " %" "s", ("YES")), firstout = 0; int res = 0; for (k = (0); k < (n); ++k) if (i <= X[k] && j <= Y[k] && XX[k] <= ii && YY[k] <= jj) ++res; printf((firstout) ? "%" "d" : " %" "d", (res)), firstout = 0; printf("\n"), firstout = 1; for (k = (0); k < (n); ++k) if (i <= X[k] && j <= Y[k] && XX[k] <= ii && YY[k] <= jj) printf((firstout) ? "%" "d" : " %" "d", (k + 1)), firstout = 0; printf("\n"), firstout = 1; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; string s1, s2, s; int n, t, m, ans, res; int main() { cin >> n >> t; cin >> s1 >> s2; for (int i = 0; i < n; i++) if (s1[i] == s2[i]) ans++; else res++; t = n - t; m = 0; for (int i = 0; i < n; i++) { if (s1[i] == s2[i]) { s += s1[i]; continue; } if (t <= ans) { if (s1[i] != 'a' && s2[i] != 'a') s += 'a'; else if (s1[i] != 'b' && s2[i] != 'b') s += 'b'; else if (s1[i] != 'c' && s2[i] != 'c') s += 'c'; } else { if (!m) s += s1[i]; if (m) s += s2[i]; m ^= 1; if (!m) t--; } } if (m % 2 || t > ans) { cout << -1 << endl; return 0; } for (int i = 0; i < n; i++) if (s1[i] == s2[i] && t < ans) { s[i] = ('a' + (s[i] - 'a' + 1) % 26); t++; } cout << s << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long query, i, j; map<long long, long long> mp; map<long long, long long>::iterator itr; vector<long long> v; cin >> query; while (query--) { long long n; mp.clear(); long long ans = 0; v.clear(); long long x; cin >> n; std::vector<long long> v(n + 1); for (i = 0; i < n; i++) { cin >> x; v[x]++; } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); ans = v[0]; long long previous = ans; for (i = 1; i <= n; i++) { if (previous == 0) { break; } if (previous <= v[i]) { ans += previous - 1; previous -= 1; } else { ans += v[i]; previous = v[i]; } } cout << ans << endl; } return 0; }
4
#include<bits/stdc++.h> using namespace std; int main(){ string s; int sum; cin>>s; for(int i=0;i<3;i++){ if(s[i]=='o')sum++; } cout<<700+(sum*100); }
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") using namespace std; void itval(istream_iterator<string> it) {} template <typename T, typename... Args> void itval(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; itval(++it, args...); } const long long int MOD = 1e9 + 7; template <typename T> inline void print(T x) { cout << x << "\n"; } template <typename T> inline void printvec(T x) { for (auto a : x) cout << a << ' '; cout << '\n'; } struct custom { bool operator()( const pair<long long int, pair<long long int, long long int> > &p1, const pair<long long int, pair<long long int, long long int> > &p2) const { return p1.first < p2.first; } }; long long int get_pow(long long int a, long long int b, long long int M = MOD) { long long int res = 1; while (b) { if (b & 1) res = (res * a) % M; a = (a * a) % M; b >>= 1; } return res; } const long long int N = 2e5 + 5, inf = 2e18; int sparse_mat_max[N][22]; int log_value[N]; void compute_sparse() { log_value[1] = 0; for (int i = 2; i < N; i++) { log_value[i] = log_value[i / 2] + 1; } for (int j = 1; j < 22; j++) { for (long long int i = 0; i + (1ll << j) < N; i++) { sparse_mat_max[i][j] = max(sparse_mat_max[i][j - 1], sparse_mat_max[i + (1ll << (j - 1))][j - 1]); } } } inline int query(int L, int R) { int j = log_value[R - L + 1]; return max(sparse_mat_max[L][j], sparse_mat_max[R - (1 << j) + 1][j]); } void solve() { int n, m; cin >> n >> m; if (m > n) { cout << -1; exit(0); } long long int x, y, k; cin >> x >> k >> y; vector<long long> a(n), b(m); for (long long int i = (long long int)0; i < (long long int)(n); i++) { cin >> a[i]; sparse_mat_max[i][0] = a[i]; } for (long long int i = (long long int)0; i < (long long int)(m); i++) { cin >> b[i]; } compute_sparse(); vector<long long> keep = {-1}; int c = 0; for (long long int i = (long long int)0; i < (long long int)(n); i++) { if (c < m && a[i] == b[c]) { keep.push_back(i); c++; } } keep.push_back(n); if (c != m) { cout << -1; return; } long long int ans = 0, len = 0; for (long long int i = (long long int)1; i < (long long int)(keep.size()); i++) { len = keep[i] - keep[i - 1] - 1; if (len > 0) { if (len < k) { int t = query(keep[i - 1] + 1, keep[i] - 1); int l = -1, r = -1; if (keep[i - 1] != -1) l = a[keep[i - 1]]; if (keep[i] != n) r = a[keep[i]]; if (l > t || r > t) { ans += (y * len); } else { cout << -1; return; } } else { if (k * y < x) { int t = query(keep[i - 1] + 1, keep[i] - 1); int l = -1, r = -1; if (keep[i - 1] != -1) l = a[keep[i - 1]]; if (keep[i] != n) r = a[keep[i]]; if (l > t || r > t) { ans += (y * len); continue; } long long int lef = len - k; ans += (x + lef * y); continue; } long long int md = len % k; ans += (x * (len / k) + y * md); } } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test = 1; clock_t z = clock(); for (long long int tes = (long long int)0; tes < (long long int)(test); tes++) { solve(); } fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC), fflush(stderr); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; if (k == n || k == 0) cout << 0 << ' ' << 0; else { cout << 1 << ' ' << min(n - k, 2 * k); } }
2
#include <bits/stdc++.h> using namespace std; const int N = 3500, mod = 1000000007; int qmod1(const int &x) { return x >= mod ? x - mod : x; } int f[N][2][2][N]; char st[N]; int A[N], B[N], a[N], len, lenp, K, p; int dfs(int t, int limn, int limi, int k) { if (t < 0) { return limi && k >= K; } if (f[t][limn][limi][k] != -1) return f[t][limn][limi][k]; int ans = 0; if (limn) { if (limi) { ans = qmod1(ans + dfs(t - 1, 1, 1, k)); ans = (ans + 1ll * a[t] * (dfs(t - 1, 1, 0, k) + dfs(t - 1, 1, 1, k))) % mod; ans = (ans + 1ll * a[t] * dfs(t - 1, 0, 1, k)) % mod; ans = (ans + 1ll * a[t] * (a[t] - 1) / 2 % mod * (dfs(t - 1, 0, 0, k) + dfs(t - 1, 0, 1, k))) % mod; } else { ans = qmod1(ans + dfs(t - 1, 1, 0, k + 1)); ans = (ans + 1ll * (p - 1 - a[t]) * (dfs(t - 1, 1, 0, k + 1) + dfs(t - 1, 1, 1, k + 1))) % mod; ans = (ans + 1ll * a[t] * dfs(t - 1, 0, 0, k + 1)) % mod; ans = (ans + 1ll * (1ll * a[t] * (p - 1) - 1ll * a[t] * (a[t] - 1) / 2) % mod * (dfs(t - 1, 0, 0, k + 1) + dfs(t - 1, 0, 1, k + 1))) % mod; } } else { if (limi) { ans = (ans + 1ll * p * dfs(t - 1, 0, 1, k)) % mod; ans = (ans + 1ll * p * (p - 1) / 2 % mod * (dfs(t - 1, 0, 0, k) + dfs(t - 1, 0, 1, k))) % mod; } else { ans = (ans + 1ll * p * dfs(t - 1, 0, 0, k + 1)) % mod; ans = (ans + 1ll * p * (p - 1) / 2 % mod * (dfs(t - 1, 0, 0, k + 1) + dfs(t - 1, 0, 1, k + 1))) % mod; } } return f[t][limn][limi][k] = ans; } void trans() { while (len) { long long t = 0; for (int j = len - 1; j >= 0; j--) { t = t * 10 + A[j]; B[j] = t / p, t %= p; } a[lenp++] = t; for (int j = len - 1; j >= 0; j--) A[j] = B[j]; while (len && !A[len - 1]) --len; } } int main() { scanf("%d%d", &p, &K); scanf("%s", st + 1); len = strlen(st + 1); for (int i = 0; i < len; i++) A[i] = st[len - i] - 48; trans(); memset(f, -1, sizeof(f)); cout << dfs(lenp - 1, 1, 1, 0) << endl; }
4
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int N=1e6+10; int n,left,right,s0,s1; struct node { int x,y; bool operator <(node b) const { return x-y>b.x-b.y; } } a[N]; char s[N]; int main() { scanf("%d",&n); for(int i=1,l,r;i<=n;i++) { scanf("%s",s+1); l=r=0; for(int j=1;s[j];j++) if(s[j]=='(') l++; else { if(l) l--; else r++; } if(!l&&r) right+=r; else if(!r&&l) left+=l; else a[i]=(node){l,r}; s0+=l;s1+=r; } if(s0^s1) puts("No"),exit(0); sort(a+1,a+n+1); for(int i=1;i<=n;i++) { if(left<a[i].y) puts("No"),exit(0); left-=a[i].y-a[i].x; } if(left^right)puts("No"); else puts("Yes"); return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[30]; int main() { char c; int n,i; long long ans=1; scanf("%d",&n); getchar(); for(i=1;i<=n;i++) { scanf("%c",&c); a[c-'a']++; } for(i=0;i<26;i++) ans=ans*(a[i]+1)%1000000007; printf("%lld",(ans+1000000006)%1000000007); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n; cin >> n; string x; cin >> x; int cnt = 0; for (i = 0; i < n - 1; i += 2) { if (x[i] == 'a' && x[i + 1] != 'b') { cnt++; x[i + 1] = 'b'; } else if (x[i] == 'b' && x[i + 1] != 'a') { cnt++; x[i + 1] = 'a'; } } cout << cnt << endl; cout << x << endl; }
1
#include <bits/stdc++.h> using namespace std; int p[2001], w[2001], z[2001]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= 2 * n; i++) scanf("%d", &p[i]); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); w[u] = v; w[v] = u; } int t; scanf("%d", &t); for (int i = 1; i <= n; i++) if (t == 1) { int o = 0; for (int i = 1; i <= 2 * n; i++) if (!z[i] && ((w[i] && !w[o]) || (!w[i] == !w[o] && p[i] > p[o]))) o = i; printf("%d\n", o); z[o] = 1; fflush(stdout); scanf("%d", &o); z[o] = 1; } else { int o; scanf("%d", &o); z[o] = 1; if (w[o] && !z[w[o]]) { printf("%d\n", w[o]); z[w[o]] = 1; fflush(stdout); continue; } o = 0; for (int i = 1; i <= 2 * n; i++) if (!z[i] && (w[i] && !w[o] || !w[i] == !w[o] && p[i] > p[o])) o = i; printf("%d\n", o); z[o] = 1; fflush(stdout); } }
3
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, d, x; cin >> n >> m >> d; long long int a[n]; unordered_map<long long int, long long int> days; set<long long int> myset; for (long long int i = 0; i < n; i++) { cin >> a[i]; myset.insert(a[i]); } long long int val = 1; while (myset.size()) { auto itr = myset.begin(); while (itr != myset.end()) { days[*itr] = val; long long int temp = *itr; myset.erase(itr); itr = myset.upper_bound((temp + d)); } val++; } cout << val - 1 << '\n'; for (long long int i = 0; i < n; i++) cout << days[a[i]] << ' '; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n, k; map<string, map<int, int> > dict; char s[maxn]; bool is_reverse(const string &t) { for (int i = 0; i < t.size() / 2; ++i) { if (t[i] != t[t.size() - i - 1]) return false; } return true; } string rev(string t) { for (int i = 0; i < t.size() / 2; ++i) { swap(t[i], t[t.size() - 1 - i]); } return t; } int main() { scanf("%d%d", &k, &n); for (int i = 0; i < k; ++i) { int x; scanf("%s%d", s, &x); string tmp = s; if (dict.count(tmp)) { if (dict[tmp].count(x)) { ++dict[tmp][x]; } else { dict[tmp][x] = 1; } } else { dict[tmp][x] = 1; } } int MAX = 0; int ans = 0; for (map<string, map<int, int> >::iterator it1 = dict.begin(); it1 != dict.end(); ++it1) { if (is_reverse(it1->first)) { int cur = 0; for (map<int, int>::reverse_iterator it = (it1->second).rbegin(); it != (it1->second).rend(); ++it) { int x = it->first, num = it->second; if (cur != 0) { if (cur + x <= 0) break; if (x < 0) MAX = max(MAX, -x); ans += cur + x; --num; cur = 0; } if (x <= 0) break; ans += num / 2 * x * 2; cur = x * (num & 1); } MAX = max(MAX, cur); } else { string tmp = rev(it1->first); if (it1->first < tmp && dict.count(tmp)) { map<int, int>::reverse_iterator st = dict[tmp].rbegin(), ed = dict[tmp].rend(); for (map<int, int>::reverse_iterator it = it1->second.rbegin(), it2 = st; it != it1->second.rend() && it2 != ed;) { int x1 = it->first, x2 = it2->first; int num1 = it->second, num2 = it2->second; if (x1 + x2 <= 0) break; int num = min(num1, num2); ans += (x1 + x2) * num; it->second -= num; if (it->second == 0) ++it; it2->second -= num; if (it2->second == 0) ++it2; } } } } printf("%d\n", ans + MAX); }
4
#include <bits/stdc++.h> using namespace std; int read() { int p = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') p = p * 10 + c - '0', c = getchar(); return p; } int head[1000005], ver[1000005 * 2], nxt[1000005 * 2], tot; void add(int a, int b) { tot++; nxt[tot] = head[a]; head[a] = tot; ver[tot] = b; return; } int f[1000005], size[1000005], d[1000005]; int find(int x) { if (x == f[x]) return x; return f[x] = find(f[x]); } int ds[1000005]; int mx, id; void dfs(int x, int fa, int dis) { ds[x] = max(ds[x], dis); if (dis > mx) { mx = dis; id = x; } for (int i = head[x]; i; i = nxt[i]) { if (ver[i] == fa) continue; dfs(ver[i], x, dis + 1); } } vector<int> v[1000005]; vector<pair<int, int> > q[1000005]; int n, m, qq; double ans[1000005]; int num[1000005]; long long sum[1000005]; void solve(int x) { int sz = size[x]; for (int i = 0; i <= sz; i++) num[i] = sum[i] = 0; for (int i = 0; i < v[x].size(); i++) num[v[x][i]]++, sum[v[x][i]] += v[x][i]; for (int i = 0; i <= sz; i++) num[i] += num[i - 1], sum[i] += sum[i - 1]; long long sm = sum[sz]; int pre = 0; double las; for (int i = 0; i < q[x].size(); i++) { int p = q[x][i].first, id = q[x][i].second; if (p == x) { ans[id] = -1; continue; } if (p == pre) { ans[id] = las; continue; } pre = p; long long cnt = 0, as = 0; int tmp = max(d[x], d[p]); for (int j = 0; j < v[p].size(); j++) { int now = tmp - v[p][j] - 1; if (now < 0) { as += 1LL * (v[p][j] + 1) * sz; as += sm; } else { cnt += num[now]; as += 1LL * (v[p][j] + 1) * (sz - num[now]); as += sm - sum[now]; } } ans[id] = las = ((double)(as + cnt * tmp)) / (1LL * sz * size[p]); } } int main() { scanf("%d%d%d", &n, &m, &qq); int t1, t2; for (int i = 1; i <= n; i++) f[i] = i, size[i] = 1; for (int i = 1; i <= m; i++) { scanf("%d%d", &t1, &t2); add(t1, t2); add(t2, t1); int aa = find(t1), bb = find(t2); f[aa] = bb; size[bb] += size[aa]; } for (int i = 1; i <= n; i++) { if (find(i) == i) { mx = -1; id = 0; dfs(i, -1, 0); mx = -1; dfs(id, -1, 0); d[i] = mx; dfs(id, -1, 0); } } for (int i = 1; i <= n; i++) { v[find(i)].push_back(ds[i]); } for (int i = 1; i <= qq; i++) { scanf("%d%d", &t1, &t2); t1 = find(t1); t2 = find(t2); if (size[t1] > size[t2]) swap(t1, t2); q[t2].push_back(make_pair(t1, i)); } for (int i = 1; i <= n; i++) { if (find(i) == i) { sort(v[i].begin(), v[i].end()); sort(q[i].begin(), q[i].end()); } } for (int i = 1; i <= n; i++) { if (find(i) == i) { solve(i); } } for (int i = 1; i <= qq; i++) { printf("%.10lf\n", ans[i]); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main(void) { int n, a[1000005], dp[1000005]; while (scanf("%d", &n) == 1) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int res = 0; for (int i = 0; i < n; i++) { dp[a[i]] = max(dp[a[i]], 1); for (int j = 2; j * a[i] <= a[n - 1]; j++) dp[j * a[i]] = max(dp[j * a[i]], dp[a[i]] + 1); } for (int i = 0; i < n; i++) res = max(res, dp[a[i]]); printf("%d\n", res); } return 0; }
6
#include <bits/stdc++.h> using namespace std; void ga(int N, int *A) { for (int i(0); i < N; i++) scanf("%d", A + i); } int N, M, A[(1 << 18)], D, K, Z, b, e, I, J, B[(1 << 18)], L; long long pw(long long n, long long k) { long long r(1); while (k) { if (k & 1) r *= n, r %= M; n *= n, n %= M; k >>= 1; } return r; } long long inv(long long a) { return pw(a, M - 2); } set<int> S; void go(int d, int *A, int N) { for (int i(0); i < N; i++) S.insert(A[i]); b = e = *A, S.erase(*A); while (S.count((b - d + M) % M)) b = (b - d + M) % M, S.erase(b); while (S.count((e + d) % M)) e = (e + d) % M, S.erase(e); } int main(void) { scanf("%d%d", &M, &N), ga(N, A), Z = abs(A[1] - *A); if (N == 1) return printf("%d 0\n", *A), 0; if (N == M) return printf("1 1\n"), 0; for (int i(0); i < N; i++) S.insert(A[i]); for (int i(0); i < N; i++) K += S.count((A[i] + Z) % M); K = N - K, D = Z * inv(K) % M, go(D, A, N); if (S.empty()) return printf("%d %d\n", b, D), 0; if (M > 2e5) return puts("-1"), 0; for (int i(0); i < N; i++) S.insert(A[i]); for (int i(0); i < M; i++) if (!S.count(i)) B[L++] = i; Z = abs(B[1] - *B), S.clear(), K = 0; for (int i(0); i < L; i++) S.insert(B[i]); for (int i(0); i < L; i++) K += S.count((B[i] + Z) % M); K = L - K, D = Z * inv(K) % M, go(D, B, L); if (!S.empty()) return puts("-1"), 0; printf("%d %d\n", (b + D * (M - N)) % M, D); return 0; }
3
#include <bits/stdc++.h> using namespace std; inline int read() { char c = getchar(); int p = 1, ret = 0; while ((c < '0') || (c > '9')) { if (c == '-') p = -1; c = getchar(); } while ((c >= '0') && (c <= '9')) ret = (ret << 1) + (ret << 3) + c - '0', c = getchar(); return ret * p; } int n, m; int jc[11]; int a[100005]; long long tmp[10]; struct tree { int t[10]; long long sum[10]; void mod(int x, int y) { sum[y] += sum[x]; sum[x] = 0; for (int i = 0; i < 10; i++) if (t[i] == x) t[i] = y; } } t[400005]; void pushup(int index) { for (int i = 0; i < 10; i++) t[index].sum[i] = t[index << 1].sum[i] + t[index << 1 | 1].sum[i]; } void build(int l, int r, int index) { for (int i = 0; i < 10; i++) t[index].t[i] = i; if (l == r) { for (int i = 0; i < 9; i++) if (a[l] >= jc[i]) t[index].sum[a[l] / jc[i] % 10] += jc[i]; return; } build(l, ((l + r) >> 1), index << 1); build(((l + r) >> 1) + 1, r, index << 1 | 1); pushup(index); } void pushdown(int index) { for (int i = 0; i < 10; i++) tmp[i] = t[index << 1].sum[i], t[index << 1].sum[i] = 0; for (int i = 0; i < 10; i++) { t[index << 1].sum[t[index].t[i]] += tmp[i]; t[index << 1].t[i] = t[index].t[t[index << 1].t[i]]; } for (int i = 0; i < 10; i++) tmp[i] = t[index << 1 | 1].sum[i], t[index << 1 | 1].sum[i] = 0; for (int i = 0; i < 10; i++) { t[index << 1 | 1].sum[t[index].t[i]] += tmp[i]; t[index << 1 | 1].t[i] = t[index].t[t[index << 1 | 1].t[i]]; } for (int i = 0; i < 10; i++) t[index].t[i] = i; } void modify(int l, int r, int L, int R, int x, int y, int index) { if ((L <= l) && (R >= r)) { t[index].mod(x, y); return; } pushdown(index); if (L <= ((l + r) >> 1)) modify(l, ((l + r) >> 1), L, R, x, y, index << 1); if (R > ((l + r) >> 1)) modify(((l + r) >> 1) + 1, r, L, R, x, y, index << 1 | 1); pushup(index); } inline tree query(int l, int r, int L, int R, int index) { if ((L <= l) && (R >= r)) return t[index]; pushdown(index); if (R <= ((l + r) >> 1)) return query(l, ((l + r) >> 1), L, R, index << 1); if (L > ((l + r) >> 1)) return query(((l + r) >> 1) + 1, r, L, R, index << 1 | 1); tree tmp, tmp1 = query(l, ((l + r) >> 1), L, R, index << 1), tmp2 = query(((l + r) >> 1) + 1, r, L, R, index << 1 | 1); for (int i = 0; i < 10; i++) tmp.sum[i] = tmp1.sum[i] + tmp2.sum[i]; return tmp; } int main() { jc[0] = 1; for (int i = 1; i < 9; i++) jc[i] = jc[i - 1] * 10; n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); build(1, n, 1); while (m--) { int flag = read(); if (flag == 1) { int l = read(), r = read(), x = read(), y = read(); if (x == y) continue; modify(1, n, l, r, x, y, 1); } else { int l = read(), r = read(); long long ans = 0; tree tmp = query(1, n, l, r, 1); for (int i = 1; i < 10; i++) ans += tmp.sum[i] * i; printf("%I64d\n", ans); } } }
6
#include <bits/stdc++.h> using namespace std; int n, m, l[155], r[155], k = 0, nowx = 1, cnt, maxn; char ch; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { ch = getchar(); while (ch != 'G' && ch != 'W') ch = getchar(); if (ch == 'W') { if (l[i] == 0) l[i] = j; r[i] = j; } } if (r[i]) maxn = i; } for (int i = 1; i <= maxn; i++) { k = 1 - k; if (k == 1) { if (r[i]) { cnt += r[i] - nowx; nowx = r[i]; } if (r[i + 1] && r[i + 1] > nowx) cnt += r[i + 1] - nowx, nowx = r[i + 1]; if (i != maxn) cnt++; } else { if (l[i]) { cnt += nowx - l[i]; nowx = l[i]; } if (l[i + 1] && l[i + 1] < nowx) cnt += nowx - l[i + 1], nowx = l[i + 1]; if (i != maxn) cnt++; } } cout << cnt << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> c[29]; int main() { int at[2]; string s, t; cin >> s >> t; for (int i = 0; i < s.length(); i++) c[s[i] - 'a'].push_back(i); int f = 0; int an = 1; for (int i = 0; i < t.length(); i++) { if (c[t[i] - 'a'].empty()) { cout << -1; return 0; } if (f > c[t[i] - 'a'][c[t[i] - 'a'].size() - 1]) { an++; f = 0; i--; } else f = c[t[i] - 'a'] [lower_bound(c[t[i] - 'a'].begin(), c[t[i] - 'a'].begin() + c[t[i] - 'a'].size(), f) - c[t[i] - 'a'].begin()] + 1; } cout << an; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const int MAXN = 1e6 + 10; const int MOD = 1000000007; long long k; long long x, y, z; int main() { cin >> x >> y >> z >> k; k += 3; while (x + y + z > k) { if (x >= y && x >= z) x--; else if (y >= z && y >= x) y--; else if (z >= x && z >= y) z--; } cout << x * y * z << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { clock_t begin = clock(); ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; pair<int, int> pp[n]; int a; for (int i = 0; i < n; i++) { cin >> a; if (a < 0) pp[i] = {a, i}; else pp[i] = {-a - 1, i}; } sort(pp, pp + n); if (n & 1) { pp[0].first = (-pp[0].first - 1); } vector<int> ans(n); for (int i = 0; i < n; i++) ans[pp[i].second] = pp[i].first; for (int u : ans) cout << u << " "; cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; template <class T> inline bool fs(T &x) { int c = getchar(); int sgn = 1; while (~c && c < '0' || c > '9') { if (c == '-') sgn = -1; c = getchar(); } for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= sgn; return ~c; } struct st { double a; double b; }; double rocket; vector<st> vv; double remained(double r, double f, double need) { double t = (r + f) / need; return (f - t); } bool is_possible(double fuel) { for (int i = 0; i < vv.size() - 1; i++) { fuel = remained(rocket, fuel, vv[i].a); if (fuel < 0.000000000) { return false; } fuel = remained(rocket, fuel, vv[i + 1].b); if (fuel < 0.000000000) { return false; } } fuel = remained(rocket, fuel, vv[vv.size() - 1].a); if (fuel < 0) { return false; } fuel = remained(rocket, fuel, vv[0].b); if (fuel < 0) { return false; } return true; } double bin() { double low = 0.0, high = 2000000000, mid, ans = -1; for (int i = 0; i < 200; i++) { mid = (low + high) / 2.0; if (is_possible(mid)) { high = mid; ans = mid; } else { low = mid; } } return ans; } int main() { st temp; int n; fs(n); scanf("%lf", &rocket); for (int i = 0; i < n; i++) { scanf("%lf", &temp.a); vv.push_back(temp); } for (int i = 0; i < n; i++) { scanf("%lf", &vv[i].b); } double res = bin(); if (res == -1) { cout << -1 << endl; return 0; } printf("%.10f\n", res); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mxN = 1e5 + 5; const long long INF = 1e18; const long long mod = 1e9 + 7; mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); vector<int> adj[mxN]; vector<int> a, b, path, cycle; int pos[mxN]; void bipartite(int v, int col, int par) { if (col == 0) a.push_back(v); else b.push_back(v); for (auto x : adj[v]) { if (x != par) { bipartite(x, col ^ 1, v); } } } bool dfs(int v) { pos[v] = int((path).size()); path.push_back(v); int low = -1; for (auto x : adj[v]) { if (pos[x] != -1 && pos[v] - pos[x] > 1) { low = max(low, pos[x]); } } if (low != -1) { for (int i = low; i <= pos[v]; i++) cycle.push_back(path[i]); return 1; } for (auto x : adj[v]) { if (pos[x] == -1) { if (dfs(x)) return 1; } } path.pop_back(); return 0; } void solve() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } if (m == (n - 1)) { bipartite(1, 0, -1); if (int((a).size()) < int((b).size())) swap(a, b); cout << 1 << '\n'; for (int i = 0; i < (k + 1) / 2; i++) cout << a[i] << " "; cout << '\n'; return; } memset(pos, -1, sizeof(pos)); dfs(1); if (int((cycle).size()) <= k) { cout << 2 << '\n'; cout << int((cycle).size()) << '\n'; for (auto x : cycle) cout << x << " "; cout << '\n'; } else { cout << 1 << '\n'; for (int i = 0; i < (k + 1) / 2; i++) cout << cycle[2 * i] << " "; cout << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; int sz[400010]; void dfs(int u, int par, vector<vector<int> >& adj) { sz[u] = 1; for (auto it : adj[u]) if (it != par) { dfs(it, u, adj); sz[u] += sz[it]; } } int furthest_node; void dfs_centroid(int u, int par, vector<vector<int> >& adj, int N) { bool flg = true; for (auto it : adj[u]) if (it != par) { dfs_centroid(it, u, adj, N); if (sz[it] > N / 2) flg = false; } if (N - sz[u] > N / 2) flg = false; if (flg) furthest_node = u; } int firstCentroid(vector<vector<int> >& adj, int N) { dfs(1, -1, adj); dfs_centroid(1, -1, adj, N); return furthest_node; } int max_sz[] = {-1, -1}, max_e; int res[400010]; void dfs_final(int u, int par, int e, vector<vector<int> >& adj, int N) { if (sz[u] == N || sz[e] == N - (N / 2)) res[u] = true; else if (e == max_e) { res[u] = (N - sz[u] - max_sz[1] <= N / 2); } else { res[u] = (N - sz[u] - max_sz[0] <= N / 2); } for (auto it : adj[u]) if (it != par) dfs_final(it, u, (e == -1 ? it : e), adj, N); } int main() { int N; scanf("%d", &N); vector<vector<int> > adj(N + 1); for (int i = 0; i < N - 1; ++i) { int first, second; scanf("%d%d", &first, &second); adj[first].push_back(second), adj[second].push_back(first); } int root = firstCentroid(adj, N); dfs(root, -1, adj); for (auto it : adj[root]) { if (sz[it] > max_sz[0]) max_sz[1] = max_sz[0], max_sz[0] = sz[it], max_e = it; else if (sz[it] > max_sz[1]) max_sz[1] = sz[it]; } dfs_final(root, -1, -1, adj, N); for (int i = 1; i < N + 1; ++i) printf("%d ", res[i]); printf("\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int x; cin >> x; int now = 0; int qtd = 0; int num = 1, answer = 0; while (now != x) { if (now * 10 + num > 10000) { now = 0; num++; qtd = 0; } now = now * 10 + num; qtd++; answer += qtd; } cout << answer << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 111; const long long H = 100; const long long W = 100 * 1000; const long double EPS = 1e-7; long long n, h1, h2; long long score[N], a[N], b[N], type[N]; long long solve(int runid) { long long res = 0; for (int floor = 0; floor < (n + 1); ++floor) { long long cur = 0; long long start = h1, end = (floor & 1) ? H - h2 : h2; end += floor * H; vector<bool> used(n, 0); for (int i = 0; i < (floor); ++i) { long double h = ((i + 1) * H - start); long double x = 1.0 * W / (end - start) * h; bool done = 0; for (int j = 0; j < (n); ++j) { if (used[j]) continue; if (a[j] > x + EPS || b[j] < x - EPS) continue; if ((i & 1) == type[j]) continue; used[j] = 1; done = 1; cur += score[j]; } if (!done) cur -= 1e15; } res = max(res, cur); } return res; } int main() { ios_base::sync_with_stdio(0); cin >> h1 >> h2 >> n; for (int i = 0; i < (n); ++i) { cin >> score[i]; char ch; cin >> ch; type[i] = (ch == 'T'); cin >> a[i] >> b[i]; } if (h1 > h2) { h1 = H - h1; h2 = H - h2; for (int i = 0; i < (n); ++i) { type[i] ^= 1; } } long long res1 = solve(0); long long res2 = 0; h1 = H - h1; h2 = H - h2; for (int i = 0; i < (n); ++i) { type[i] ^= 1; } res2 = solve(1); cout << max(res1, res2) << endl; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n, h[MAXN], mx[MAXN]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = n; i >= 0; i--) mx[i] = max(h[i + 1], mx[i + 1]); for (int i = 0; i < n; i++) cout << max(h[i], mx[i] + 1) - h[i] << " "; }
2
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long tt; cin >> tt; while (tt--) { long long n, g, b; cin >> n >> g >> b; if (g >= b || g >= (n + 1) / 2) cout << n << '\n'; else { if (((n + 1) / 2) % g == 0) cout << max(n, ((n + 1) / 2) / g * (g + b) - b) << '\n'; else cout << max(n, ((n + 1) / 2) / g * (g + b) + ((n + 1) / 2) % g) << '\n'; } } }
2
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin>>s; s.pop_back(); s.pop_back(); for(int i = s.size(); i >= 0; i-=2){ int t = i/2; if(s.substr(0,t)==s.substr(t,t)){ cout<<i; break; } } }
0
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 322; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { string a; cin >> a; int first = -1, last = -1; for (int i = 0; i < a.size(); i++) { if (a[i] == '1') { first = i; break; } } for (int i = a.size() - 1; i >= 0; i--) { if (a[i] == '1') { last = i; break; } } if (first == -1) { cout << 0 << endl; continue; } int ans = 0; while (first <= last) { if (a[first] == '0') ++ans; ++first; } cout << ans << endl; } return 0; }
1
#include <iostream> int main(){int a,b;std::cin>>a>>b;std::cout<<(a+b>=24?a+b-24:a+b);}
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vvb = vector<vb>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vpll = vector<pll>; using vpdd = vector<pdd>; template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); } template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template <class T1, class T2> void re(pair<T1, T2> &p); template <class T> void re(vector<T> &a); template <class T, size_t SZ> void re(array<T, SZ> &a); template <class T> void re(T &x) { cin >> x; } void re(double &x) { string t; re(t); x = stod(t); } template <class Arg, class... Args> void re(Arg &first, Args &...rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2> &p) { re(p.first, p.second); } template <class T> void re(vector<T> &a) { for (int i = 0; i < int((a).size()); i++) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ> &a) { for (int i = 0; i < SZ; i++) re(a[i]); } } // namespace __input using namespace __input; namespace __output { template <typename T> struct is_outputtable { template <typename C> static constexpr decltype(declval<ostream &>() << declval<const C &>(), bool()) test(int) { return true; } template <typename C> static constexpr bool test(...) { return false; } static constexpr bool value = test<T>(int()); }; template < class T, typename V = decltype(declval<const T &>().begin()), typename S = typename enable_if<!is_outputtable<T>::value, bool>::type> void pr(const T &x); template <class T, typename V = decltype(declval<ostream &>() << declval<const T &>())> void pr(const T &x) { cout << x; } template <class T1, class T2> void pr(const pair<T1, T2> &x); template <class Arg, class... Args> void pr(const Arg &first, const Args &...rest) { pr(first); pr(rest...); } template <class T, bool pretty = true> void prContain(const T &x) { if (pretty) pr("{"); bool fst = 1; for (const auto &a : x) pr(!fst ? pretty ? ", " : " " : "", a), fst = 0; if (pretty) pr("}"); } template <class T> void pc(const T &x) { prContain<T, false>(x); pr("\n"); } template <class T1, class T2> void pr(const pair<T1, T2> &x) { pr("{", x.first, ", ", x.second, "}"); } template <class T, typename V, typename S> void pr(const T &x) { prContain(x); } void ps() { pr("\n"); } template <class Arg> void ps(const Arg &first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg &first, const Args &...rest) { pr(first, " "); ps(rest...); } } // namespace __output using namespace __output; namespace __algorithm { template <typename T> void dedup(vector<T> &v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), v.end()); } template <typename T> typename vector<T>::const_iterator find(const vector<T> &v, const T &x) { auto it = lower_bound((v).begin(), (v).end(), x); return it != v.end() && *it == x ? it : v.end(); } template <typename T> size_t index(const vector<T> &v, const T &x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template <typename I> struct _reversed_struct { I &v_; explicit _reversed_struct(I &v) : v_{v} {} typename I::reverse_iterator begin() const { return v_.rbegin(); } typename I::reverse_iterator end() const { return v_.rend(); } }; template <typename I> _reversed_struct<I> reversed(I &v) { return _reversed_struct<I>(v); } } // namespace __algorithm using namespace __algorithm; namespace __io { void setIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout << setprecision(15); } } // namespace __io using namespace __io; template <typename T, typename U, typename TAssociativeCombineFunction, typename UAssociativeCombineFunction, typename UApplicator> struct segment_tree_lazy { int SZ; T t_identity; U u_identity; TAssociativeCombineFunction TT; UAssociativeCombineFunction UU; UApplicator UT; std::vector<T> data; std::vector<bool> has_update; std::vector<U> updates; segment_tree_lazy() {} segment_tree_lazy(int _SZ, T _t_identity, U _u_identity, TAssociativeCombineFunction _TT, UAssociativeCombineFunction _UU, UApplicator _UT) : SZ(_SZ), t_identity(_t_identity), u_identity(_u_identity), TT(_TT), UU(_UU), UT(_UT) { data.assign(2 * SZ, t_identity); has_update.assign(SZ, false); updates.assign(SZ, u_identity); } template <typename Function> void assign(Function fn) { for (int i = 0; i < SZ; i++) data[SZ + i] = fn(i); for (int i = SZ - 1; i; i--) data[i] = TT(data[2 * i], data[2 * i + 1]); has_update.assign(SZ, false); updates.assign(SZ, u_identity); } private: void apply_update(int i, const U &u) { data[i] = UT(u, data[i]); if (i < SZ) { has_update[i] = true; updates[i] = UU(u, updates[i]); } } void propagate_ancestor_updates(int i) { for (int ht = 31 - __builtin_clz(i); ht > 0; ht--) { int anc = i >> ht; if (has_update[anc]) { apply_update(2 * anc, updates[anc]); apply_update(2 * anc + 1, updates[anc]); has_update[anc] = false; updates[anc] = u_identity; } } } void recompute_ancestors(int i) { for (i /= 2; i; i /= 2) data[i] = UT(updates[i], TT(data[2 * i], data[2 * i + 1])); } void modify_leaf(int i, T v, bool overwrite) { i += SZ; propagate_ancestor_updates(i); data[i] = overwrite ? v : TT(data[i], v); recompute_ancestors(i); } public: void assign(int i, T v) { modify_leaf(i, v, true); } void combine_and_assign(int i, T v) { modify_leaf(i, v, false); } void apply_update(int first, int last, U u) { assert(0 <= first && last <= SZ); first += SZ, last += SZ; propagate_ancestor_updates(first); propagate_ancestor_updates(last - 1); for (int i = first, j = last; i < j; i /= 2, j /= 2) { if (i & 1) apply_update(i++, u); if (j & 1) apply_update(--j, u); } recompute_ancestors(first); recompute_ancestors(last - 1); } T accumulate(int first, int last) { assert(0 <= first && last <= SZ); first += SZ, last += SZ; propagate_ancestor_updates(first); propagate_ancestor_updates(last - 1); T left = t_identity, right = t_identity; for (int i = first, j = last; i < j; i /= 2, j /= 2) { if (i & 1) left = TT(left, data[i++]); if (j & 1) right = TT(data[--j], right); } return TT(left, right); } T read(int i) { i += SZ; propagate_ancestor_updates(i); return data[i]; } }; const string NO = "Hungry", YES = "Full"; const int INF = 1e9; template <typename T, bool maximum_mode> struct monotonic_rmq { deque<pair<T, int>> values; int current_index = 0; bool better(const T &a, const T &b) const { return (a < b) ^ maximum_mode; } int add(const T &x) { while (!values.empty() && !better(values.back().first, x)) values.pop_back(); values.emplace_back(x, current_index); return current_index++; } T query(int index) { while (values.front().second < index) values.pop_front(); assert(!values.empty()); return values.front().first; } }; int main() { setIO(); int n, k; re(n, k); const int S = 2 * n + 1; auto make_dp = [&]() { array<vi, 2> dp{vi(S, INF), vi(S, INF)}; return dp; }; auto dp = make_dp(); dp[1][0] = 0; auto adv = [&](int len) { for (int i = S - 1; i - len >= 0; i--) dp[1][i] = dp[1][i - len]; for (int i = 0; i < len; i++) dp[1][i] = INF; }; auto update = [&](int len) { auto next = make_dp(); next[0] = dp[0]; for (int i = S - 1; i - len >= 0; i--) next[1][i] = dp[1][i - len]; for (int b = 0; b <= 1; b++) { monotonic_rmq<int, false> vals; int removed_count = 0; for (int i = 0; i < S; i++) { vals.add(1 + dp[1 ^ b][i]); vals.add(2 + dp[b][i]); if (i - len - 1 >= 0) removed_count += 2; if (removed_count < vals.current_index) ckmin(next[b][i], vals.query(removed_count)); } } swap(dp, next); }; int t = 0; for (int i = 0; i < k; i++) { int l, r; re(l, r); adv(l - t); t = l; update(r - t); t = r; } adv(2 * n - t); int ans = min(dp[0][n], dp[1][n]); if (ans == INF) { ps(NO); return 0; } ps(YES); ps(ans); return 0; }
6
#include<iostream> using namespace std; const int MAX = 2000000; int H,A[MAX+1]; void maxHeapify(int i){ int l,r,largest; l = 2*i; r= 2*i+1; if(l<=H&&A[l]>A[i])largest = l; else largest = i; if(r<=H&&A[r]>A[largest])largest = r; if(largest!=i){ swap(A[i],A[largest]); maxHeapify(largest); } } int main(){ cin>>H; for(int i =1;i<=H;i++)cin>>A[i]; for(int i = H/2;i>=1;i--)maxHeapify(i); for(int i = 1;i<=H;i++)cout<<" "<<A[i]; cout<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int a, b, n; cin >> a >> b >> n; if (b == 0) { cout << 0 << '\n'; return; } if (a == 0) { if (b) { cout << "No solution" << '\n'; } else { cout << 0 << '\n'; } return; } if (b % a) { cout << "No solution" << '\n'; return; } int second = b / a; if (second < 0 && n % 2 == 0) { cout << "No solution" << '\n'; return; } if (n == 1) { cout << second << '\n'; return; }; for (int first = -40; first <= 40; first++) { long long t = 1; for (int j = 0; j < n; j++) { t *= first; } if (t == second) { cout << first << '\n'; return; } } cout << "No solution" << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, x; cin >> n >> x; long long ans = 0; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; ans += tmp; } if (ans + n - 1 == x) printf("YES\n"); else printf("NO\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d%d", &n, &k); int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); if (k < n + 1 || n == 4) { printf("-1"); return 0; } printf("%d %d", a, c); for (int i = 1; i <= n; i++) { if (i != a && i != b && i != c && i != d) printf(" %d", i); } printf(" %d %d\n", d, b); printf("%d %d", c, a); for (int i = 1; i <= n; i++) { if (i != a && i != b && i != c && i != d) printf(" %d", i); } printf(" %d %d\n", b, d); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 5e3 + 10; const double PI = acos(-1.0); int lcp[N][N]; char s[N]; int dp[N]; void solve() { memset(dp, 0, sizeof dp); int n; scanf("%d%s", &n, s + 1); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { lcp[i][j] = 0; } } for (int i = n; i >= 1; i--) { for (int j = i - 1; j >= 1; j--) { if (s[i] != s[j]) lcp[i][j] = 0; else lcp[i][j] = lcp[i + 1][j + 1] + (s[i] == s[j]); } } int res = 0; for (int i = 1; i <= n; i++) { dp[i] = n - i + 1; for (int j = 1; j < i; j++) { int len = lcp[i][j]; if (s[i + len] > s[j + len]) { dp[i] = max(dp[i], dp[j] + n - (i + len) + 1); } } res = max(res, dp[i]); } printf("%d\n", res); } int main() { int t; cin >> t; while (t--) solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n; int m; int* update(int* tree, int p, int v, bool isor) { tree[p] = v; if (p == 0) return tree; if (p % 2 == 0) { v = isor ? tree[p - 1] | tree[p] : tree[p - 1] ^ tree[p]; p = p / 2 - 1; } else { v = isor ? tree[p] | tree[p + 1] : tree[p] ^ tree[p + 1]; p = p / 2; } isor = isor ? false : true; tree = update(tree, p, v, isor); return tree; } int main() { scanf("%d %d", &n, &m); int input, cp_n = pow(2, n); queue<int> a; while (cp_n--) { scanf("%d", &input); a.push(input); } int total = 0; for (int i = 0; i < n + 1; i++) total += pow(2, i); int tree[total]; bool isor = true; for (int i = n; i > 0; i--) { for (int j = 0; j < pow(2, i); j += 2) { int a1 = a.front(); a.pop(); tree[(int)pow(2, i) - 1 + j] = a1; int a2 = a.front(); a.pop(); tree[(int)pow(2, i) + j] = a2; isor ? a.push(a1 | a2) : a.push(a1 ^ a2); } isor = isor ? false : true; } int a1 = a.front(); a.pop(); tree[0] = a1; while (m--) { int* cpy_tree = tree; int p, v; scanf("%d %d", &p, &v); cpy_tree = update(cpy_tree, (int)pow(2, n) + p - 2, v, true); printf("%d\n", cpy_tree[0]); } }
4
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g; int t[1204020]; int sum[1204020]; bool has_p[1204020]; vector<int> good; void dfs(int v, int p, long long s) { sum[v] = t[v]; for (int(i) = 0; (i) < (int)(g[v].size()); ++(i)) if (g[v][i] != p) { dfs(g[v][i], v, s); sum[v] += sum[g[v][i]]; } if (sum[v] == s && p != -1) good.push_back(v), sum[v] = 0; } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n; cin >> n; long long s = 0; g.resize(n); int root; for (int(i) = 0; (i) < (int)(n); ++(i)) { int p; cin >> p >> t[i]; --p; if (p != -1) g[p].push_back(i); else root = i; s += t[i]; } if (s % 3 != 0) { cout << -1; return 0; } dfs(root, -1, s / 3); if (good.size() < 2) { cout << -1; } else { cout << good[0] + 1 << ' ' << good[1] + 1 << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; int A[2005]; int main() { int n, a, b; scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &A[i]); } sort(A + 1, A + n + 1); int ans = 0; if (A[b + 1] > A[b]) { ans = A[b + 1] - A[b]; } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 2; const long long mod = 1000000007; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); vector<long long> cnt(200); long long n; cin >> n; set<long long> st; vector<long long> age(100); for (int i = 0; i < n; i++) st.insert(i + 1); vector<long long> f, s; set<long long> boro[100], choto[100]; for (int i = 0; i + 1 < n * (n - 1) / 2; i++) { long long x, y; cin >> x >> y; f.push_back(x); s.push_back(y); cnt[x]++, cnt[y]++; if (cnt[x] == n - 1) st.erase(x); if (cnt[y] == n - 1) st.erase(y); boro[x].insert(y); choto[y].insert(x); } long long a = *st.begin(), b = *--st.end(); long long aboro = 0; for (long long e : boro[a]) { for (long long x : boro[e]) { if (x == b) { aboro = 1; } } } if (aboro) cout << a << " " << b; else cout << b << " " << a; }
2
#include <bits/stdc++.h> using namespace std; long long int N = 1e9 + 7; int sum[52][52], mat[52][52], dp[52][52][52][52]; int n; int black(int x1, int y1, int x2, int y2) { return sum[x2][y2] + sum[x1 - 1][y1 - 1] - sum[x2][y1 - 1] - sum[x1 - 1][y2]; } int func(int x1, int y1, int x2, int y2) { if (black(x1, y1, x2, y2) == 0) { return 0; } if (dp[x1][y1][x2][y2] != -1) { return dp[x1][y1][x2][y2]; } int res = max(x2 - x1 + 1, y2 - y1 + 1); for (int i = x1; i < x2; i++) { res = min(res, func(x1, y1, i, y2) + func(i + 1, y1, x2, y2)); } for (int i = y1; i < y2; i++) { res = min(res, func(x1, y1, x2, i) + func(x1, i + 1, x2, y2)); } dp[x1][y1][x2][y2] = res; return res; } void solve() { cin >> n; memset(dp, -1, sizeof(dp)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { char c; cin >> c; mat[i][j] = (c == '.') ? 1 : 0; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (mat[i][j] == 0); } } cout << func(1, 1, n, n) << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const long long int INFLL = 1e18 + 10; const long double EPS = 1e-8; const long double EPSLD = 1e-14; const long long int MOD = 1000000007; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } struct Unionfind { vector<int> size; vector<int> par; Unionfind() {} Unionfind(int n) : size(n, 1), par(n) { for (int i = (0); i < (int)(n); i++) par[i] = i; } void init(int n) { size.resize(n); par.resize(n); for (int i = (0); i < (int)(n); i++) { size[i] = 1; par[i] = i; } } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (size[y] < size[x]) swap(x, y); par[x] = y; size[y] += size[x]; return true; } bool same(int x, int y) { return find(x) == find(y); } }; vector<long long int> compress(vector<long long int> a) { vector<long long int> ord = a, res; sort((ord).begin(), (ord).end()); ord.erase(unique((ord).begin(), (ord).end()), ord.end()); for (auto &w : a) res.emplace_back(lower_bound((ord).begin(), (ord).end(), w) - ord.begin()); return res; } Unionfind uf; struct Segtree { vector<long long int> cnt, an; int size; void init(int n) { size = 1; while (size < n) size *= 2; cnt = vector<long long int>(size * 2 - 1, 0); an = vector<long long int>(size * 2 - 1, -2); } void add(int k, int id) { k += size - 1; cnt[k] = 1; an[k] = id; while (k > 0) { k = (k - 1) / 2; cnt[k] = cnt[k * 2 + 1] + cnt[k * 2 + 2]; if (an[k] == -2) an[k] = id; else an[k] = -1; } } void del(int k) { k += size - 1; cnt[k] = 0; an[k] = -2; while (k > 0) { k = (k - 1) / 2; cnt[k] = cnt[k * 2 + 1] + cnt[k * 2 + 2]; an[k] = -1; if (an[k * 2 + 1] == -2 && an[k * 2 + 2] == -2) an[k] = -2; } } long long int query(int a, int b, int id, int k, int l, int r) { if (an[k] == -2) return 0; if (a <= l && r <= b) { if (an[k] != -1) { if (uf.same(an[k], id)) return cnt[k]; uf.unite(an[k], id); return cnt[k] - 1; } an[k] = id; } else if (b <= l || r <= a) { return 0; } long long int chl = query(a, b, id, k * 2 + 1, l, (l + r) / 2); long long int chr = query(a, b, id, k * 2 + 2, (l + r) / 2, r); return chl + chr; } long long int query(int a, int b, int id) { return query(a, b, id, 0, 0, size); } }; const int MAX_N = 200010; int n; long long int x1[MAX_N], x2[MAX_N]; long long int z1[MAX_N], z2[MAX_N]; vector<long long int> ix, iy; vector<long long int> xs, ys; Segtree seg; vector<pair<pair<long long int, long long int>, pair<long long int, long long int> > > qu; long long int res[MAX_N]; long long int ans; int main() { scanf("%d", &n); for (int i = (0); i < (int)(n); i++) { scanf("%lld %lld %lld %lld", &x1[i], &z1[i], &x2[i], &z2[i]); ix.emplace_back(x1[i]); ix.emplace_back(x2[i]); iy.emplace_back(z1[i]); iy.emplace_back(z2[i]); } xs = compress(ix); ys = compress(iy); for (int i = (0); i < (int)(n); i++) { if (xs[i * 2] == xs[i * 2 + 1]) qu.emplace_back(pair<long long int, long long int>(xs[i * 2], ys[i * 2]), pair<long long int, long long int>(ys[i * 2 + 1], i)); else { qu.emplace_back(pair<long long int, long long int>(xs[i * 2], -1), pair<long long int, long long int>(ys[i * 2], i)); qu.emplace_back(pair<long long int, long long int>(xs[i * 2 + 1], INFLL), pair<long long int, long long int>(ys[i * 2], i)); res[i] = x2[i] - x1[i]; } } sort((qu).begin(), (qu).end()); uf.init(n); seg.init(3 * n); for (auto &q : qu) { int id = q.second.second; if (q.first.second == -1) seg.add(q.second.first, id); else if (q.first.second == INFLL) seg.del(q.second.first); else res[id] = z2[id] - z1[id] - seg.query(q.first.second, q.second.first + 1, id); } for (int i = (0); i < (int)(n); i++) if (uf.find(i) != i) res[uf.find(i)] += res[i]; for (int i = (0); i < (int)(n); i++) if (uf.find(i) == i) chmax(ans, res[i]); cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; void Solve() { string s; cin >> s; int cnt = 0; for (auto i : s) { cnt += (i == 'N'); } cout << (cnt == 1 ? "NO" : "YES") << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) Solve(); }
1
#include <bits/stdc++.h> const long long OO = LLONG_MAX; const long long _OO = LLONG_MIN; const long long MOD = 1e9 + 7; const double PI = acos(-1); const double EPSILON = std::numeric_limits<double>::epsilon(); bool double_comp(double a, double b) { return fabs(a - b) < EPSILON; } using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int d; cin >> d; int n; cin >> n; int prev; int res = 0; for (int i = 0; i < n; i++) { int a; cin >> a; if (i == 0 || prev == d) { prev = a; continue; } else if (prev + 1 < d) res += d - prev; else if (prev + 1 == d) res++; prev = a; } cout << res; }
2
#include <bits/stdc++.h> using namespace std; const int M = 301; const int N = 400010; const int MOD = 1e9 + 7; int n, q; char cmd[69]; vector<long long> pr; long long a[N], mask[M], mul[M]; long long prod[4 * N], orz[4 * N]; long long lzProd[4 * N], lzOrz[4 * N]; long long bigMod(long long a, long long e) { if (e == -1) e = MOD - 2; long long ret = 1; while (e) { if (e & 1) ret = ret * a % MOD; a = a * a % MOD, e >>= 1; } return ret; } bool isPrime(int x) { for (int i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; } void init(int u = 1, int b = 1, int e = n) { lzProd[u] = 1, lzOrz[u] = 0; if (b == e) { prod[u] = a[b], orz[u] = mask[a[b]]; return; } int mid = b + e >> 1; init(u << 1, b, mid), init(u << 1 | 1, mid + 1, e); prod[u] = prod[u << 1] * prod[u << 1 | 1] % MOD; orz[u] = orz[u << 1] | orz[u << 1 | 1]; } void push(int u, int b, int e) { prod[u] *= bigMod(lzProd[u], e - b + 1), prod[u] %= MOD, orz[u] |= lzOrz[u]; if (b ^ e) { lzProd[u << 1] *= lzProd[u], lzProd[u << 1] %= MOD; lzProd[u << 1 | 1] *= lzProd[u], lzProd[u << 1 | 1] %= MOD; lzOrz[u << 1] |= lzOrz[u], lzOrz[u << 1 | 1] |= lzOrz[u]; } lzProd[u] = 1, lzOrz[u] = 0; } void update(int l, int r, int x, int u = 1, int b = 1, int e = n) { if (lzProd[u] > 1 or lzOrz[u]) push(u, b, e); if (b > r or e < l) return; if (b >= l and e <= r) { lzProd[u] *= x, lzProd[u] %= MOD, lzOrz[u] |= mask[x]; return push(u, b, e); } int mid = b + e >> 1; update(l, r, x, u << 1, b, mid), update(l, r, x, u << 1 | 1, mid + 1, e); prod[u] = prod[u << 1] * prod[u << 1 | 1] % MOD; orz[u] = orz[u << 1] | orz[u << 1 | 1]; } pair<long long, long long> query(int l, int r, int u = 1, int b = 1, int e = n) { if (b > r or e < l) return make_pair(1, 0); if (lzProd[u] > 1 or lzOrz[u]) push(u, b, e); if (b >= l and e <= r) return make_pair(prod[u], orz[u]); int mid = b + e >> 1; pair<long long, long long> one = query(l, r, u << 1, b, mid); pair<long long, long long> two = query(l, r, u << 1 | 1, mid + 1, e); return make_pair(one.first * two.first % MOD, one.second | two.second); } int main() { for (int i = 2; i < M; ++i) if (isPrime(i)) pr.emplace_back(i); int sz = pr.size(); for (int i = 1; i < M; ++i) { for (int j = 0; j < sz; ++j) if (i % pr[j] == 0) mask[i] |= 1LL << j; } for (int i = 0; i < sz; ++i) mul[i] = (pr[i] - 1) * bigMod(pr[i], -1) % MOD; cin >> n >> q; for (int i = 1; i <= n; ++i) scanf("%lld", a + i); init(); while (q--) { int l, r, x; scanf("%s %d %d", cmd, &l, &r); if (cmd[0] == 'T') { pair<long long, long long> yo = query(l, r); long long ans = yo.first; for (int i = 0; i < sz; ++i) if (yo.second & 1LL << i) ans *= mul[i], ans %= MOD; printf("%lld\n", ans); } else { scanf("%d", &x); update(l, r, x); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; struct edge { int to, next, w; edge(int _to = 0, int _next = 0, int _w = 0) : to(_to), next(_next), w(_w) {} } e[300010 << 1]; int n, m; int g[300010], nume = 0; int d[300010]; int f[300010][2]; vector<int> ans; namespace LCT { int f[300010]; void init() { for (int i = 1; i <= n; i++) f[i] = i; } int getf(int x) { if (f[x] == x) return x; return f[x] = getf(f[x]); } int merge(int x, int y) { f[f[x]] = f[y]; } } // namespace LCT void addEdge(int u, int v, int w) { e[nume] = edge(v, g[u], w); g[u] = nume++; } void dfs(int x, int p) { for (int i = g[x]; ~i; i = e[i].next) if (e[i].to ^ p) dfs(e[i].to, x); int numch = 0; static int ch[300010][2]; int c0 = 0, c1 = 0; for (int i = g[x]; ~i; i = e[i].next) if (e[i].to ^ p) { ++numch; ch[numch][0] = f[e[i].to][0]; ch[numch][1] = f[e[i].to][1]; if (!ch[numch][0] && !ch[numch][1]) { f[x][0] = f[x][1] = 0; return; } if (ch[numch][0] && ch[numch][1]) { f[x][0] = f[x][1] = 1; return; } if (ch[numch][0]) c0++; if (ch[numch][1]) c1++; } if (d[x] == -1) { f[x][0] = f[x][1] = 1; return; } if (d[x] == 0) { if (c1 & 1) { f[x][1] = 1; f[x][0] = 0; } else { f[x][1] = 0; f[x][0] = 1; } } else { if (c1 & 1) { f[x][1] = 0; f[x][0] = 1; } else { f[x][1] = 1; f[x][0] = 0; } } } void dfs2(int x, int p, int fx) { if (f[x][0] != f[x][1]) { for (int i = g[x]; ~i; i = e[i].next) if (e[i].to ^ p) if (f[e[i].to][0]) { dfs2(e[i].to, x, 0); } else { ans.push_back(e[i].w); dfs2(e[i].to, x, 1); } } else { int temp = -1, c1 = 0; for (int i = g[x]; ~i; i = e[i].next) if (e[i].to ^ p) { if (f[e[i].to][0] == f[e[i].to][1] && temp == -1) { temp = i; } else { if (f[e[i].to][0]) dfs2(e[i].to, x, 0); else { dfs2(e[i].to, x, 1); ans.push_back(e[i].w); c1++; } } } if (temp == -1) return; if ((c1 & 1) == fx ^ d[x]) dfs2(e[temp].to, x, 0); else { dfs2(e[temp].to, x, 1); ans.push_back(e[temp].w); } } } int main() { memset(g, -1, sizeof g); scanf("%d%d", &n, &m); LCT::init(); for (int i = 1; i <= n; i++) scanf("%d", d + i); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); if (LCT::getf(u) ^ LCT::getf(v)) { addEdge(u, v, i); addEdge(v, u, i); LCT::merge(u, v); } } dfs(1, 0); if (!f[1][0]) { puts("-1"); return 0; } dfs2(1, 0, 0); int numans = ans.size(); printf("%d\n", numans); for (int i = 0; i < numans; i++) printf("%d\n", ans[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } int takemod(int x) { return (x % 1000000007 + 1000000007) % 1000000007; } int fpow(int base, int power, int MOD) { int result = 1; base = base % MOD; while (power) { if (power & 1) result = (result * base) % MOD; base = (base * base) % MOD; power = power >> 1; } return result; } int modinv(int x) { return fpow(x, 1000000007 - 2, 1000000007); } bool sortbysec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } void solve() { long long int n, k; cin >> n >> k; vector<pair<long long, long long>> A(n); vector<long long int> v(200005, 0); vector<vector<pair<long long, long long>>> ml(200005); for (long long int i = 0; i < (n); ++i) { long long int l, r; cin >> l >> r; v[l]++; v[r + 1]--; ml[l].push_back({r + 1, i + 1}); } long long int cnt = 0; multiset<pair<long long, long long>> pr; vector<long long int> ans; for (long long int i = 0; i < (v.size()); ++i) { cnt += v[i]; for (auto j : ml[i]) pr.insert(j); while (cnt > k) { cnt--; auto x = *pr.rbegin(); ans.push_back(x.second); v[x.first]++; pr.erase(pr.find(x)); } } cout << ans.size() << "\n"; for (auto i : ans) cout << i << " "; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int T = 1; while (T--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int MAXN = 505; const int MOD = 1000000007; int n, m; char a[MAXN][MAXN]; int id[MAXN][MAXN]; int dp1[MAXN][MAXN]; int dp2[MAXN][MAXN]; int size[2 * MAXN]; pair<int, int> cell[2 * MAXN][MAXN]; void add(int &x, int v) { x = (x + v) % MOD; } int revlvl(int lvl) { return n + m - 2 - lvl; } void solve() { int k = (n + m) / 2; memset(dp1, 0, sizeof(dp1)); if (a[0][0] == a[n - 1][m - 1]) { dp1[0][0] = 1; } memset(size, 0, sizeof(size)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int lvl = i + j; id[i][j] = size[lvl]; cell[lvl][id[i][j]] = make_pair(i, j); size[i + j]++; } } for (int l = 1; l < k; l++) { int sz = size[l]; for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { dp2[i][j] = 0; pair<int, int> p = cell[l][i]; pair<int, int> q = cell[revlvl(l)][j]; if (a[p.first][p.second] == a[q.first][q.second]) { if (p.first > 0) { if (q.first + 1 < n) add(dp2[i][j], dp1[id[p.first - 1][p.second]][id[q.first + 1][q.second]]); if (q.second + 1 < m) add(dp2[i][j], dp1[id[p.first - 1][p.second]][id[q.first][q.second + 1]]); } if (p.second > 0) { if (q.first + 1 < n) add(dp2[i][j], dp1[id[p.first][p.second - 1]][id[q.first + 1][q.second]]); if (q.second + 1 < m) add(dp2[i][j], dp1[id[p.first][p.second - 1]][id[q.first][q.second + 1]]); } } } } for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { dp1[i][j] = dp2[i][j]; } } } k--; int ans = 0; if (k == revlvl(k)) { for (int i = 0; i < size[k]; i++) { add(ans, dp1[i][i]); } } else { for (int i = 0; i < size[k]; i++) { pair<int, int> p = cell[k][i]; if (p.first + 1 < n) add(ans, dp1[i][id[p.first + 1][p.second]]); if (p.second + 1 < m) add(ans, dp1[i][id[p.first][p.second + 1]]); } } printf("%d\n", ans); } int main() { while (scanf("%d %d", &n, &m) == 2) { for (int i = 0; i < n; i++) { scanf("%s", a[i]); } solve(); } }
5
#include <bits/stdc++.h> using namespace std; map<pair<int, long long>, long long> dp; long long nums[512]; long long costs[512]; int n; long long gcd(long long a, long long b) { return a ? (gcd(b % a, a)) : b; } long long calcdp(int i, long long d) { if (dp.count(make_pair(i, d))) return dp[make_pair(i, d)]; dp[make_pair(i, d)] = 1000000000000LL; long long &ans = dp[make_pair(i, d)]; if (i == n) { if (d == 1) { ans = 0; } return ans; } ans = min(ans, calcdp(i + 1, d)); ans = min(ans, calcdp(i + 1, gcd(d, nums[i])) + costs[i]); return ans; } int main() { cin >> n; for (int i = 0; i < (int)(n); ++i) cin >> nums[i]; for (int i = 0; i < (int)(n); ++i) cin >> costs[i]; long long ans = calcdp(0, 0); if (ans < 1000000000000LL) cout << ans << endl; else cout << -1 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int maxcnt = INT_MIN, cnt = 1; for (int i = 0; i < n - 1; i++) { if (a[i] == a[i + 1]) { cnt++; } else { if (maxcnt < cnt) { maxcnt = cnt; } cnt = 1; } } if (maxcnt < cnt) { maxcnt = cnt; } cout << maxcnt; }
3
#include <bits/stdc++.h> using namespace std; class Solution { public: int n; double e[100005]; vector<int> g[100005]; void dfs(int u, int p = 0) { int child = 0; for (int v : g[u]) if (v - p) child++; if (child > 0) { e[u] = 1.0; } else { e[u] = 0.0; } for (int v : g[u]) if (v - p) { dfs(v, u); e[u] += e[v] / child; } } void solve(std::istream& in, std::ostream& out) { in >> n; for (int i = 0; i < n - 1; i++) { int u, v; in >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1); cout << e[1] << endl; } }; void solve(std::istream& in, std::ostream& out) { out << std::setprecision(12) << fixed; Solution solution; solution.solve(in, out); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; ostream& out = cout; solve(in, out); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int m, q, n, a; int mn = 1e9; cin >> m; for (int i = 0; i < m; i++) cin >> q, mn = min(q, mn); cin >> n; int s = 0; vector<int> v(n); for (int j = 0; j < n; j++) cin >> v[j]; sort(v.rbegin(), v.rend()); int t = 0; for (int i = 0; i < v.size(); i++) { t++; s += v[i]; if (t == mn) { i += 2; t = 0; } } cout << s << endl; return 0; }
3
#include <iostream> using namespace std; long long n, m, x; int main(){ int t; cin >> t; while(t--){ cin >> n >> m >> x; long long yy = x / n; yy -= (x % n == 0? 1:0); long long xx = x % n; xx = (xx == 0? n - 1:xx - 1); long long res = (xx * m + yy); cout << res + 1 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; map<string, string> par; map<string, bool> seen; string ans[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m = 0; for (int i = 0; i < n; i++) { string from, to; cin >> from >> to; seen[to] = true; if (!seen.count(from)) ans[m++] = from; par[from] = to; } cout << m << "\n"; for (int i = 0; i < m; i++) { string u = ans[i]; while (par.count(u)) u = par[u]; cout << ans[i] << " " << u << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; const long long MOD = (long long)1e9 + 7; signed main() { long long n, k; cin >> n >> k; string ans; long long cnt = 0; char ch = 'a'; for (long long i = 0; i < n; i++) { ans.push_back(ch + (i % k)); } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; int n, sum, i, j; int a[200010]; priority_queue<pair<int, int> > q; vector<pair<int, int> > ans; int main() { scanf("%d%d", &n, &sum); if (sum % 2 == 1) { printf("No\n"); return 0; } for (i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i]) q.push(make_pair(a[i], i)); } int step = 0; while (q.size()) { pair<int, int> t = q.top(); q.pop(); vector<pair<int, int> > nextiter; nextiter.clear(); for (int i = 0; i < t.first; i++) { if (q.empty()) { printf("No\n"); return 0; } pair<int, int> v = q.top(); q.pop(); ans.push_back(make_pair(t.second, v.second)); v.first--; if (v.first) nextiter.push_back(v); } for (int i = 0; i < nextiter.size(); i++) q.push(nextiter[i]); } printf("Yes\n"); cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) printf("%d %d\n", ans[i].first, ans[i].second); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 55; char m[maxn][maxn]; int main() { int a, b, c, d; for (int i = 0; i < maxn; i++) { fill(m[i], m[i] + maxn, 0); } int idx = 0; scanf("%d %d %d %d", &a, &b, &c, &d); printf("50 50\n"); if (a != 1) { while (a > 1) { for (int i = 0; i < 50; i += 2) { if (a == 1) break; m[idx][i] = 'A'; a--; } idx += 2; } for (int i = 0; i < idx; i++) { for (int j = 0; j < 50; j++) { if (m[i][j] == 0) m[i][j] = 'B'; } } b--; } idx += 1; for (int i = idx; i < 50; i += 2) { for (int j = 0; j < 50; j += 2) { if (b != 0) m[i][j] = 'B', b--; else if (c != 0) m[i][j] = 'C', c--; else if (d != 0) m[i][j] = 'D', d--; if (d == 0) break; } } for (int i = idx - 1; i < 50; i++) { for (int j = 0; j < 50; j++) if (m[i][j] == 0) m[i][j] = 'A'; } for (int i = 0; i < 50; i++) { m[i][50] = 0; puts(m[i]); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long fpow(int a, int n) { long long tmp = a, ans = 1; while (n) { if (n & 1) ans = ans * tmp % 1000000007; tmp = tmp * tmp % 1000000007, n >>= 1; } return ans; } char s[2]; int n, m, c[6][6], t[6][1000000 + 5], lazy[1000000 + 5], sum[6][1000000 + 5], ss[6], a[1000000 + 5], ll, rr, d; void update(int p) { for (int i = 0; i <= 5; i++) t[i][p] = t[i][((p) << 1)] + t[i][((p) << 1 | 1)], t[i][p] %= 1000000007; } void change(int p, int l, int r, int x) { lazy[p] = x; for (int i = 0; i <= 5; i++) t[i][p] = x * 1ll * (sum[i][r] - sum[i][l - 1]) % 1000000007; } void pushdown(int p, int l, int r) { if (lazy[p] != -1) { int mid = l + r >> 1; change(((p) << 1), l, mid, lazy[p]); change(((p) << 1 | 1), mid + 1, r, lazy[p]); lazy[p] = -1; } } void build(int l, int r, int p) { lazy[p] = -1; if (l == r) { for (int i = 0; i <= 5; i++) t[i][p] = i == 0 ? a[l] : a[l] * 1ll * (sum[i][l] - sum[i][l - 1]) % 1000000007; return; } int mid = l + r >> 1; build(l, mid, ((p) << 1)); build(mid + 1, r, ((p) << 1 | 1)); update(p); } void query(int l, int r, int p) { if (ll <= l && r <= rr) { for (int i = 0; i <= 5; i++) ss[i] += t[i][p], ss[i] %= 1000000007; return; } pushdown(p, l, r); int mid = l + r >> 1; if (ll <= mid) query(l, mid, ((p) << 1)); if (rr > mid) query(mid + 1, r, ((p) << 1 | 1)); } void modify(int l, int r, int p) { if (ll <= l && r <= rr) { change(p, l, r, d); return; } pushdown(p, l, r); int mid = l + r >> 1; if (ll <= mid) modify(l, mid, ((p) << 1)); if (rr > mid) modify(mid + 1, r, ((p) << 1 | 1)); update(p); } int main() { for (int i = 0; i <= 5; i++) { for (int j = 0; j <= i; j++) { if (!i) c[i][j] = 1; if (i) c[i][j] += c[i - 1][j]; if (j) c[i][j] += c[i - 1][j - 1]; } } scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) sum[0][i] = 1; for (int i = 1; i <= 5; i++) for (int j = 1; j <= n; j++) sum[i][j] = sum[i - 1][j] * 1ll * j % 1000000007; for (int i = 0; i <= 5; i++) for (int j = 1; j <= n; j++) sum[i][j] += sum[i][j - 1], sum[i][j] %= 1000000007; for (int i = 1; i <= n; i++) scanf("%d", a + i); build(1, n, 1); while (m--) { scanf("%s", s); if (s[0] == '?') { scanf("%d%d%d", &ll, &rr, &d); memset(ss, 0, sizeof(ss)); query(1, n, 1); int ans = 0; for (int i = 0; i <= d; i++) { ans += ss[i] * 1ll * c[d][i] % 1000000007 * fpow(1 - ll, d - i) % 1000000007; ans %= 1000000007; } if (ans < 0) ans += 1000000007; printf("%d\n", ans); } else { scanf("%d%d%d", &ll, &rr, &d); modify(1, n, 1); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T> void __read(T& a) { cin >> a; } template <typename T, typename... Args> void __read(T& a, Args&... args) { cin >> a; __read(args...); } constexpr long long M7 = 1000000007ll; constexpr long long M9 = 1000000009ll; constexpr long long MFFT = 998244353ll; template <class T> void outv(T& a) { for (auto& x : a) cout << x << ' '; } mt19937 rnd(static_cast<unsigned>( chrono::steady_clock::now().time_since_epoch().count())); template <class T> void random_shuffle(T s, T e) { shuffle(s, e, rnd); }; static auto __super_speed__ = (ios_base::sync_with_stdio(0), cin.tie(0)); int32_t main() { long long t; __read(t); while (t--) { long long n; __read(n); map<long long, long long> cnt; for (signed i = 0; i < (n); i++) { long long q; __read(q); cnt[q]++; } long long g = cnt.rbegin()->second; long long s = 0; auto it = ++cnt.rbegin(); while (it != cnt.rend() && s <= g) { s += it->second; it++; } long long b = 0; while (it != cnt.rend() && (g + s + b + it->second) * 2 <= n) { b += it->second; it++; } if (s <= g || b <= g || (s + b + g) * 2 > n) { cout << "0 0 0\n"; } else { cout << g << ' ' << s << ' ' << b << '\n'; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; #define F first #define S second typedef pair<int,int> P; typedef pair<P,P> P2; int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; int d[1<<5][50][50][6],INF=1<<28,n,m,l; bool check(int x, int y) {return x>=0&&x<n&&y>=0&&y<m;} int main() { cin >> n >> m >> l; string s[n]; for(int i=0; i<n; i++) cin >> s[i]; int sx,sy,tx,ty; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(s[i][j]=='S') sx=i,sy=j; if(s[i][j]=='G') tx=i,ty=j; for(int t=0; t<(1<<5); t++) { for(int k=0; k<6; k++) d[t][i][j][k]=INF; } } } d[0][sx][sy][5]=0; int ans=-1; queue<P2> que; que.push(P2(P(sx,sy),P(5,0))); while(!que.empty()) { P2 p=que.front();que.pop(); int nx=p.F.F,ny=p.F.S,k=p.S.F,t=p.S.S; if(nx==tx && ny==ty && k>=4) { ans=d[t][nx][ny][k]; break; } for(int i=0; i<4; i++) { int x,y,z,tt=t; if(!k) { if(!i) x=nx-l,y=ny,z=4; else if(i==1) x=nx,y=ny+1,z=0; else if(i==2) x=nx+1,y=ny,z=5; else x=nx,y=ny-1,z=0; } else if(k==1) { if(!i) x=nx-1,y=ny,z=1; else if(i==1) x=nx,y=ny+l,z=4; else if(i==2) x=nx+1,y=ny,z=1; else x=nx,y=ny-1,z=5; } else if(k==2) { if(!i) x=nx-1,y=ny,z=5; else if(i==1) x=nx,y=ny+1,z=2; else if(i==2) x=nx+l,y=ny,z=4; else x=nx,y=ny-1,z=2; } else if(k==3) { if(!i) x=nx-1,y=ny,z=3; else if(i==1) x=nx,y=ny+1,z=5; else if(i==2) x=nx+1,y=ny,z=3; else x=nx,y=ny-l,z=4; } else if(k==5) { if(!i) x=nx-1,y=ny,z=0; else if(i==1) x=nx,y=ny+1,z=1; else if(i==2) x=nx+1,y=ny,z=2; else x=nx,y=ny-1,z=3; } else if(k==4) { if(!i) x=nx-l,y=ny,z=2; else if(i==1) x=nx,y=ny+l,z=3; else if(i==2) x=nx+l,y=ny,z=0; else x=nx,y=ny-l,z=1; } if(z<4) { bool ck=1; for(int j=0; j<l; j++) { int xx=x+dx[z]*j,yy=y+dy[z]*j; if(!check(xx,yy) || s[xx][yy]=='#') { ck=0; break; } if(s[xx][yy]>='a' && s[xx][yy]<='e' && !(t&(1<<(s[xx][yy]-'a')))) ck=0; } if(!ck) continue; } else { if(!check(x,y)) continue; if(s[x][y]=='^' || s[x][y]=='#') continue; if(s[x][y]>='a' && s[x][y]<='e' && !(t&(1<<(s[x][y]-'a')))) continue; if(s[x][y]>='A' && s[x][y]<='E') tt=tt^(1<<(s[x][y]-'A')); } if(d[tt][x][y][z]<=d[t][nx][ny][k]+1) continue; d[tt][x][y][z]=d[t][nx][ny][k]+1; que.push(P2(P(x,y),P(z,tt))); } } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(void) { long long int n, a, b; cin >> n >> a >> b; if (n % a == 0) cout << "YES" << endl << n / a << " " << 0; else if (n % b == 0) cout << "YES" << endl << 0 << " " << n / b; else { if (n % (a + b) == 0) cout << "YES" << endl << n / (a + b) << " " << n / (a + b); else if ((n % (a + b)) % a == 0) cout << "YES" << endl << (n / (a + b)) + (n % (a + b)) / a << " " << n / (a + b); else if ((n % (a + b)) % b == 0) cout << "YES" << endl << n / (a + b) << " " << (n / (a + b)) + (n % (a + b)) / b; else { for (int i = 1; i <= (n / a); i++) { if ((n - (i * a)) % b == 0) { cout << "YES" << endl << i << " " << (n - (i * a)) / b; return 0; } } cout << "NO"; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<long long> v(n, 0); long long a = 0, b = 0; map<int, int> m; for (long long i = 0; i < n; i++) { cin >> v[i]; m[log10(v[i]) + 1]++; } for (long long i = 0; i < n; i++) { for (auto j = m.begin(); j != m.end(); j++) { long long x = v[i]; long long k = 10; long long y = 0; int s = j->first; while (x > 0) { if (s == 1) { y += x * k; y %= 998244353LL; break; } y += (x % 10) * k; x /= 10; k *= 100; k %= 998244353LL; y %= 998244353LL; s--; } y = y * (j->second); y %= 998244353LL; a += y; a %= 998244353LL; x = v[i]; k = 1; y = 0; s = j->first; while (x > 0) { if (s == 0) { y += x * k; y %= 998244353LL; break; } y += (x % 10) * k; x /= 10; k *= 100; k %= 998244353LL; y %= 998244353LL; s--; } y = y * (j->second); y %= 998244353LL; a += y; a %= 998244353LL; } } cout << (a % 998244353LL); return 0; }
4
#include<bits/stdc++.h> #define f first #define s second #define mp make_pair #define pi M_PI #define inf 1<<30 #define MAX 100010 #define eps (1e-6) #define equals(a,b) (fabs((a)-(b))<eps) using namespace std; class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y){} Point operator+(Point p){ return Point(x+p.x,y+p.y);} Point operator-(Point p){ return Point(x-p.x,y-p.y);} Point operator*(double k){ return Point(x*k,y*k);} Point operator/(double k){ return Point(x/k,y/k);} bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);} bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;} double abs(){ return sqrt(norm());} double norm(){ return (x*x+y*y);} }; typedef Point Vector; typedef vector<Point> Polygon; class Segment{ public: Point p1,p2; Segment(Point p1=Point(),Point p2=Point()):p1(p1),p2(p2){} }; typedef Segment Line; double norm(Vector a){ return (a.x*a.x+a.y*a.y);} double abs(Vector a){ return sqrt(norm(a));} double dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);} double cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);} bool isParallel(Segment s,Segment t){ return equals(cross(s.p1-s.p2,t.p1-t.p2),0.0); } int ccw(Point p0,Point p1,Point p2){ Vector a=p1-p0; Vector b=p2-p0; if(cross(a,b)>eps)return 1; if(cross(a,b)<-eps)return -1; if(dot(a,b)<-eps)return 2; if(a.norm()<b.norm())return -2; return 0; } bool intersect(Point p1,Point p2,Point p3,Point p4){ return (ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0 && ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0); } bool intersect(Segment s1,Segment s2){ return intersect(s1.p1,s1.p2,s2.p1,s2.p2); } Point getCrossPointLL(Line a,Line b){ double A=cross(a.p2-a.p1,b.p2-b.p1); double B=cross(a.p2-a.p1,a.p2-b.p1); if(abs(A)<eps || abs(B)<eps)return b.p1; return b.p1+(b.p2-b.p1)*(B/A); } typedef vector<vector<pair<int,double> > > Graph; Graph SegmentArrangement(vector<Segment> v,vector<Point> &ps){ for(int i=0;i<v.size();i++){ ps.push_back(v[i].p1); ps.push_back(v[i].p2); for(int j=i+1;j<v.size();j++){ if(intersect(v[i],v[j]))ps.push_back(getCrossPointLL(v[i],v[j])); } } sort(ps.begin(),ps.end()); ps.erase(unique(ps.begin(),ps.end()),ps.end()); Graph g(ps.size()); for(int i=0;i<v.size();i++){ vector<pair<double,int> > list; for(int j=0;j<ps.size();j++) if(ccw(v[i].p1,v[i].p2,ps[j])==0) list.push_back(mp(norm(v[i].p1-ps[j]),j)); sort(list.begin(),list.end()); for(int j=0;j<list.size()-1;j++){ int a=list[j].s,b=list[j+1].s; g[a].push_back(mp(b,abs(ps[b]-ps[a]))); g[b].push_back(mp(a,abs(ps[a]-ps[b]))); } } return g; } int n,m; vector<Segment> vs; vector<Point> vp; set<Point> st; Point s,t; int c,d; Graph g; bool checked[MAX]={}; bool visited[MAX]={}; bool valve[MAX]={}; void dfs(int v){ checked[v]=true; if(valve[v])return; for(int i=0;i<g[v].size();i++){ pair<int,double> next=g[v][i]; if(!checked[next.f]){ dfs(next.f); } } return; } double bfs(int v){ double res=0.0; queue<int> q; q.push(v); while(q.size()){ int u=q.front(); q.pop(); if(visited[u])continue; visited[u]=true; for(int i=0;i<g[u].size();i++){ pair<int,double> next=g[u][i]; if(checked[next.f])res+=next.s; else if(!visited[next.f]){ res+=next.s; q.push(next.f); } } } return res; } int main() { cin>>n>>m; for(int i=0;i<n;i++){ int a,b,c,d; cin>>a>>b>>c>>d; vs.push_back(Segment(Point(a,b),Point(c,d))); } for(int i=0;i<m;i++){ int a,b; cin>>a>>b; vp.push_back(Point(a,b)); st.insert(Point(a,b)); } cin>>s.x>>s.y>>t.x>>t.y; vp.push_back(s); vp.push_back(t); g=SegmentArrangement(vs,vp); for(int i=0;i<vp.size();i++){ if(st.find(vp[i])!=st.end())valve[i]=true; if(vp[i]==s)c=i; if(vp[i]==t)d=i; } dfs(d); if(checked[c])cout<<-1<<endl; else { double ans=0.0; for(int i=0;i<g.size();i++){ for(int j=0;j<g[i].size();j++)ans+=g[i][j].s/2.0; } ans-=bfs(c); printf("%.10f\n",ans); } return 0; }
0
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); long long TESTS = 1; while (TESTS--) { long long n, m; cin >> n >> m; long long p = n / m, sum = 0; for (long long i = 1; i < m + 1; i++) { for (long long j = 1; j < m + 1; j++) if ((i * i + j * j) % m == 0) sum++; } long long total = sum * p * p; sum = 0; for (long long i = (n / m) * m + 1; i <= n; i++) { for (long long j = 1; j <= m; j++) if ((i * i + j * j) % m == 0) sum++; } total += sum * (n / m) * 2; for (long long i = (n / m) * m + 1; i <= n; i++) { for (long long j = (n / m) * m + 1; j <= n; j++) { if ((i * i + j * j) % m == 0) total++; } } cout << total; } }
2
#include<bits/stdc++.h> using namespace std; long long mp[305][305],n,m,l; void floyd(){ for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]); } } } } int main(){ cin>>n>>m>>l; memset(mp,0x3f,sizeof(mp)); for(long long i=1;i<=n;i++){ mp[i][i]=0; } for(long long i=1;i<=m;i++){ int x,y,z; cin>>x>>y>>z; mp[x][y]=z; mp[y][x]=z; } floyd(); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(mp[i][j]>l) mp[i][j]=1e17; else mp[i][j]=1; } } floyd(); long long q; cin>>q; while(q--){ long long s,t; cin>>s>>t; if(mp[s][t]>=1e17) cout<<-1<<endl; else cout<<mp[s][t]-1<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int result = 0; int k; while (n >= 2) { k = (int)((-1.0 + sqrt(1 + (double)24 * n)) / 6.0); n -= (k * (3 * k + 1)) / 2; result++; } cout << result << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000; const double pi = 2 * acos(0.0); const int md = 30000001; vector<int> g[300005]; int h[100005]; int q[300005], D[300005], Dist[300005]; bool z[300005]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= k; ++i) cin >> h[i]; int ver = n; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; ver++; g[x].push_back(ver); g[ver].push_back(x); g[ver].push_back(y); g[y].push_back(ver); } int s, t; cin >> s >> t; ++k; h[k] = t; n = ver; int qt, qh; qh = qt = 0; for (int i = 1; i <= n; ++i) D[i] = inf; for (int i = 1; i <= k; ++i) { q[qt++] = h[i]; D[h[i]] = 0; } while (qh < qt) { int v = q[qh++]; for (int i = 0; i <= (int)g[v].size() - 1; ++i) { int to = g[v][i]; if (D[to] > D[v] + 1) { D[to] = D[v] + 1; q[qt++] = to; } } } int l = 1, r = n + 4; while (r > l) { int m = (l + r) / 2; memset(z, false, sizeof(z)); for (int i = 1; i <= n; ++i) { if (D[i] <= m) z[i] = true; Dist[i] = inf; } qh = qt = 0; q[qt++] = s; Dist[s] = 0; while (qh < qt) { int v = q[qh++]; for (int i = 0; i <= (int)g[v].size() - 1; ++i) { int to = g[v][i]; if (z[to] && Dist[to] > Dist[v] + 1) { Dist[to] = Dist[v] + 1; q[qt++] = to; } } } if (Dist[t] >= inf) l = m + 1; else r = m; } if (r > n) cout << "-1" << endl; else cout << r << endl; return 0; }
5
#include <bits/stdc++.h> int a[310], b[310]; int car[30010], r[30010], c[30010]; int tot; int n, m; void movecycle(int value) { car[tot] = a[value]; if (value < n - 1) { r[tot] = 2; c[tot] = value + 2; } if (value == n - 1) { r[tot] = 3; c[tot] = n; } if (value >= n && value < 2 * n - 1) { r[tot] = 3; c[tot] = 2 * n - 1 - value; } if (value == 2 * n - 1) { r[tot] = 2; c[tot] = 1; } tot++; } void movein(int value) { car[tot] = a[value]; if (value > n - 1) { r[tot] = 4; c[tot] = 2 * n - value; } else { r[tot] = 1; c[tot] = value + 1; } tot++; } void putin() { for (int i = 0; i < 2 * n; ++i) { if (a[i] > 0 && a[i] == b[i]) { movein(i); a[i] = 0; } } } void cycle(int e) { int q = e; int p = e - 1; int w = e - 1; if (p < 0) { p = p + 2 * n; } do { if (a[p] > 0) { movecycle(p); a[q] = a[p]; a[p] = 0; } q = p; --p; if (p < 0) { p = p + 2 * n; } } while (p != e); } int foundPositive() { for (int i = 0; i < 2 * n; i++) { if (a[i] > 0) { return 1; } } return 0; } int main() { std::cin >> n >> m; for (int i = 0; i < n; i++) { std::cin >> b[i]; } for (int i = 0; i < n; i++) { std::cin >> a[i]; } for (int i = 0; i < n; i++) { std::cin >> a[2 * n - i - 1]; } for (int i = 0; i < n; i++) { std::cin >> b[2 * n - i - 1]; } while (foundPositive()) { putin(); int ind = -1; for (int i = 0; i < 2 * n; ++i) { if (a[i] == 0) { ind = i; break; } } if (ind < 0) { std::cout << "-1\n"; return 0; } cycle(ind); } std::cout << tot << "\n"; for (int i = 0; i < tot; ++i) { std::cout << car[i] << " " << r[i] << " " << c[i] << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; string s[1005]; int main() { int c = 0, m = 0, l, r; while (getline(cin, s[c])) { l = s[c].length(); m = max(l, m); c++; } for (int i = 0; i <= m + 1; i++) cout << "*"; cout << endl; int f = 0, ll; for (int i = 0; i < c; i++) { ll = m - s[i].length(); if (ll % 2 && s[i].length()) { if (f) { r = ll / 2; l = ll - r; f = 0; } else { l = ll / 2; r = ll - l; f = 1; } } else { l = ll / 2; r = ll - l; } cout << "*"; for (int i = 0; i < l; i++) cout << " "; cout << s[i]; for (int i = 0; i < r; i++) cout << " "; cout << "*" << endl; } for (int i = 0; i <= m + 1; i++) cout << "*"; cout << endl; }
2
#include <queue> #include <iostream> #include <map> #include <vector> #include <string> #include <algorithm> #include <iomanip> using namespace std; #define rep(i,n) for(int i = 0;i < n;i++) #include <complex> typedef double D; typedef complex<D> P; typedef pair<P, P> L; typedef vector<P> VP; #define X real() #define Y imag() const D EPS = 1e-9; const D INF = 1e12; #define EQ(n,m) (abs((n) - (m)) < EPS) #define EX P(INF,INF) D cs(P a, P b) { return (conj(a) * b).Y; } D dt(P a, P b) { return (conj(a) * b).X; } int ccw(P a, P b, P c) { b -= a, c -= a; if (cs(b, c) > EPS)return 1; if (cs(b, c) < -EPS)return -1; if (dt(b, c) < -EPS)return +2; if (norm(b) < norm(c))return -2; return 0; } P cpLL(P a1, P a2, P b1, P b2) { D d1 = cs(b2 - b1, b1 - a1); D d2 = cs(b2 - b1, a2 - a1); if (EQ(d2, 0))return EX; return a1 + d1 / d2 * (a2 - a1); } bool isor(P a1, P a2, P b1, P b2) { return EQ(dt(a1 - a2, b1 - b2), 0.0); } bool ispa(P a1, P a2, P b1, P b2) { return EQ(cs(a1 - a2, b1 - b2), 0.0); } int main() { int n; cin >> n; VP v; D tx = 0.0, ty = 0.0; rep(i, n) { D x, y; cin >> x >> y; v.push_back(P(x, y)); tx += x; ty += y; } tx /= double(n), ty /= double(n); if (n % 2) { cout << "NA" << endl; return 0; } int h = n / 2; rep(i, h) { L l0 = L(v[i], v[i + 1]); L l1 = L(v[(i + h) % n], v[(i + h + 1) % n]); double dist0 = abs(l0.first - l0.second); double dist1 = abs(l1.first - l1.second); if (!EQ(dist0, dist1) || !ispa(l0.first, l0.second, l1.first, l1.second)) { cout << "NA" << endl; return 0; } } cout << fixed << setprecision(30) << tx << " " << ty << endl; return 0; }
0
#include <bits/stdc++.h> int vis[10000], line[10000], head[10000], cnt = 0; struct Edge { int to, next; } edge[10000 * 10]; void addedge(int u, int v) { edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++; } int dfs(int u) { for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (!vis[v]) { vis[v] = 1; if (line[v] == -1 || dfs(line[v]) == 1) { line[v] = u; return 1; } } } return 0; } using namespace std; const int mod = 1e9 + 7; int qpow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = (ans % mod) * (a % mod) % mod; a = (a % mod) * (a % mod) % mod; b >>= 1; } return ans % mod; } char s[1000006]; int p[1000006]; int main() { int t, n; long long k; scanf("%d", &t); while (t--) { int st = 0; scanf("%d%lld%s", &n, &k, s + 1); for (int i = 1; i <= n; i++) if (s[i] == '0') p[++st] = i; int last = 1, q = 1; while (k && q <= st) { int pos = p[q]; if (pos == last) { last++; q++; continue; } else { if (k >= pos - last) { k -= (pos - last); swap(s[last], s[pos]); } else { swap(s[pos - k], s[pos]); k = 0; } last++; q++; } } printf("%s\n", s + 1); } }
4
#include<bits/stdc++.h> using namespace std; vector<pair<int, int>> a[100000]; vector<int> ans(100000); void dfs(int now, int pre, int col){ ans[now] = col; for (auto u:a[now]) if (u.first != pre) dfs(u.first, now, col^(u.second&1)); } int main(){ int i, u, v, w, n; cin >> n; for (i=1; i<n; i++){ cin >> u >> v >> w; a[u].emplace_back(v, w); a[v].emplace_back(u, w); } dfs(1, 0, 0); for (i=1; i<=n; i++) cout << ans[i] << endl; }
0
#include <bits/stdc++.h> using namespace std; long long f(long long lvl, long long n, int t) { if (lvl == 0) return 0; if (t == 0) { if (n <= (1LL << (lvl - 1))) return 1 + f(lvl - 1, n, 1); return (1LL << lvl) + f(lvl - 1, n - (1LL << (lvl - 1)), 0); } if (n <= (1LL << (lvl - 1))) return (1LL << lvl) + f(lvl - 1, n, 1); return 1 + f(lvl - 1, n - (1LL << (lvl - 1)), 0); } int main() { long long lvl, n; while (cin >> lvl >> n) { cout << f(lvl, n, 0) << '\n'; } }
3
#include<iostream> using namespace std; int main(){ int n; cin>>n; int a[n]; float c=0.0; for(int i=0;i<n;i++){ cin>>a[i]; c+=1/float(a[i]); } cout<<1/c<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; int tx[1010], ty[1010]; int xs[1010], ys[1010]; bool field[2020][2020]; bool ng[2020][2020]; queue<pair<int, int> > que; int main() { int N; scanf("%d", &N); int cx = 0, cy = 0; tx[0] = 0, ty[0] = 0; for (int i = 0; i < N; i++) { char ch[10]; int dis; scanf("%s%d", ch, &dis); if (ch[0] == 'R') { cx += dis; tx[i + 1] = cx, ty[i + 1] = cy; } else if (ch[0] == 'L') { cx -= dis; tx[i + 1] = cx, ty[i + 1] = cy; } else if (ch[0] == 'U') { cy += dis; tx[i + 1] = cx, ty[i + 1] = cy; } else { cy -= dis; tx[i + 1] = cx, ty[i + 1] = cy; } } for (int i = 0; i <= N; i++) xs[i] = tx[i], ys[i] = ty[i]; sort(xs, xs + N + 1), sort(ys, ys + N + 1); int xln = unique(xs, xs + N + 1) - xs, yln = unique(ys, ys + N + 1) - ys; memset(field, false, sizeof(field)); for (int i = 0; i < N; i++) { int px = lower_bound(xs, xs + xln, tx[i]) - xs; int py = lower_bound(ys, ys + yln, ty[i]) - ys; int nx = lower_bound(xs, xs + xln, tx[i + 1]) - xs; int ny = lower_bound(ys, ys + yln, ty[i + 1]) - ys; if (px == nx) { int xid = px * 2 + 1; for (int j = min(ny, py) * 2 + 1; j <= max(ny, py) * 2 + 1; j++) { field[xid][j] = true; } } else { int yid = py * 2 + 1; for (int j = min(nx, px) * 2 + 1; j <= max(nx, px) * 2 + 1; j++) { field[j][yid] = true; } } } memset(ng, false, sizeof(ng)); que.push(pair<int, int>(0, 0)); while (!que.empty()) { pair<int, int> p = que.front(); que.pop(); int x = p.first, y = p.second; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || ny < 0 || nx > xln * 2 || ny > yln * 2) continue; int xl, yl; if (nx % 2 == 0 && nx != 0 && nx != xln * 2) xl = xs[nx / 2] - xs[nx / 2 - 1] - 1; else xl = 1; if (ny % 2 == 0 && ny != 0 && ny != yln * 2) yl = ys[ny / 2] - ys[ny / 2 - 1] - 1; else yl = 1; if (xl == 0 || yl == 0) { nx += dx[i], ny += dy[i]; if (nx < 0 || ny < 0 || nx > xln * 2 || ny > yln * 2) continue; } if (field[nx][ny] == true) continue; if (ng[nx][ny] == true) continue; ng[nx][ny] = true; que.push(pair<int, int>(nx, ny)); } } long long ans = 0; for (int i = 1; i < xln * 2; i++) { for (int j = 1; j < yln * 2; j++) { if (ng[i][j]) continue; int x, y; if (i % 2 == 0) x = xs[i / 2] - xs[i / 2 - 1] - 1; else x = 1; if (j % 2 == 0) y = ys[j / 2] - ys[j / 2 - 1] - 1; else y = 1; ans += (long long)x * y; } } cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; long long int i; bool flag = true; for (i = 0; i < s1.length(); ++i) { if ((s2[i] - s1[i]) > 0) { cout << -1 << endl; flag = false; break; } } if (flag == true) { cout << s2 << endl; } }
2
#include<iostream> using namespace std; int main(){ string s,s1,s2; cin>>s>>s1>>s2; cout<<s[0]<<s1[0]<<s2[0]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<string> v; int main() { string x, y; int cnt = 0; char temp; int n; cin >> n; n = n + n - 1; for (int i = 0; i < n; i++) { cin >> x; if (x == "**") continue; cin >> y; x += y; sort(v.begin(), v.end()); if (binary_search(v.begin(), v.end(), x)) continue; cnt++; v.push_back(x); for (int j = 1; j <= 3; j++) { y = ""; if (j == 1) { y += x[2]; y += x[0]; y += x[3]; y += x[1]; v.push_back(y); } else if (j == 2) { y += x[3]; y += x[2]; y += x[1]; y += x[0]; v.push_back(y); } else if (j == 3) { y += x[1]; y += x[3]; y += x[0]; y += x[2]; v.push_back(y); } } } cout << cnt; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int N, M; vector<int> g[maxn]; long long out[maxn], in[maxn]; int main() { cin >> N >> M; for (int i = 1; i <= N; ++i) { g[i].clear(); out[i] = in[i] = 0; } for (int i = 1; i <= M; ++i) { int a, b; cin >> a >> b; int x = max(a, b), y = min(a, b); g[y].push_back(x); ++out[x]; ++in[y]; } long long ret = 0; for (int i = 1; i <= N; ++i) { ret += out[i] * in[i]; } int Q; cin >> Q; cout << ret << endl; for (int q = 1; q <= Q; ++q) { int x; cin >> x; ret -= out[x] * in[x]; for (int i : g[x]) { ret -= out[i] * in[i]; --out[i]; ++in[i]; g[i].push_back(x); ret += out[i] * in[i]; ++out[x]; --in[x]; } ret += out[x] * in[x]; g[x].clear(); cout << ret << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; bool cmp(pair<long long int, pair<long long int, long long int> > a, pair<long long int, pair<long long int, long long int> > b) { if (a.first < b.first) return true; else if (a.first == b.first && a.second.first > b.second.first) return true; return false; } long long int modular_pow(long long int base, long long int exponent) { long long int result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % 1000000007; exponent = exponent >> 1; base = (base * base) % 1000000007; } return result % 1000000007; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, m, n; cin >> n >> k; if (k == 0 || n == 0) { cout << (2 * n) % 1000000007; return 0; } if (k == 1) { cout << ((4 * n) % 1000000007 - 1 % 1000000007 + 1000000007) % 1000000007; return 0; } long long int power = modular_pow(2, k); long long int zz = (power * (2 * n % 1000000007 - 1 % 1000000007 + 1000000007) % 1000000007 + 1) % 1000000007; cout << zz; }
3
#include <bits/stdc++.h> using namespace std; int main() { int a[3] = {2, 5, 8}; long long n, k, ans = 0; cin >> n >> k; for (int i = 0; i < 3; i++) ans += (n * a[i]) / k + ((n * a[i]) % k != 0); cout << ans << endl; }
1
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <cstring> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <list> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <unordered_set> #include <unordered_map> #include <array> #include <cassert> #include <bitset> #include "bits/stdc++.h" using namespace std; using LL = long long; //P型変数(ベクトル)x,yについて、(c=実数) //x+y,x-yはそのまま //x*c,x/cもそのまま //長さはabs(x)、距離はabs(x-y) //単位ベクトルはx/abs(x) //法線ベクトルはx*P(0,±1)(+←→-) //x軸となす角はarg(v)、uとvのなす角はarg(u/v) //uとvを2辺とする三角形の面積はcross(u,v)*0.5 //complexの仕様: //real実部 imag虚部 abs絶対値 arg偏角 norm絶対値の2乗 //conj共役 projリーマン球面への射影(?) polar(r,t)極形式からの変換 using FLOAT = double; const FLOAT EPS = 1e-8; const FLOAT INF = 1e12; typedef complex<FLOAT> P;//点クラス namespace std { bool operator < (const P& a, const P& b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } } //FLOAT同士の等しさ inline bool EQ(FLOAT a, FLOAT b) { return abs(a - b) < EPS; } //P同士の等しさ inline bool EQV(P a, P b) { return EQ(a.real(), b.real()) && EQ(a.imag(), b.imag()); } //外積(a.x*b.y-a.y*b.x) double cross(const P& a, const P& b) { return imag(conj(a)*b); } //内積(a.x*b.x+a.y*b.y) double dot(const P& a, const P& b) { return real(conj(a)*b); } //線クラス(端点は[0],[1]) struct L : public vector<P> { L(const P &a, const P &b) { push_back(a); push_back(b); } }; typedef vector<P> G;//多角形クラス //円クラス struct C { P p; FLOAT r; C(const P &p, FLOAT r) : p(p), r(r) { } }; //謎関数(軽くて色々できる) //なんかEPSを適切につけるとよさそう(FLOAT型側につけるのもいいかも?) int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0) return +1; // counter clockwise if (cross(b, c) < 0) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; } //直線・線分交差系 //直線+直線の交差判定 bool intersectLL(const L &l, const L &m) { return abs(cross(l[1] - l[0], m[1] - m[0])) > EPS || // non-parallel abs(cross(l[1] - l[0], m[0] - l[0])) < EPS; // same line } //直線+線分の交差判定 bool intersectLS(const L &l, const L &s) { return cross(l[1] - l[0], s[0] - l[0])* // s[0] is left of l cross(l[1] - l[0], s[1] - l[0]) < EPS; // s[1] is right of l } //直線+点の交差判定 bool intersectLP(const L &l, const P &p) { return abs(cross(l[1] - p, l[0] - p)) < EPS; } //線分+線分の交差判定 bool intersectSS(const L &s, const L &t) { return ccw(s[0], s[1], t[0])*ccw(s[0], s[1], t[1]) <= 0 && ccw(t[0], t[1], s[0])*ccw(t[0], t[1], s[1]) <= 0; } //線分+点の交差判定 bool intersectSP(const L &s, const P &p) { return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality } //距離・交点系 //線分への点の射影(下ろした垂線の足) P projection(const L &l, const P &p) { FLOAT t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t * (l[0] - l[1]); } //線分と対称な点 P reflection(const L &l, const P &p) { return p + (projection(l, p) - p) * 2.; } //点と直線の距離 FLOAT distanceLP(const L &l, const P &p) { return abs(p - projection(l, p)); } //直線と直線の距離(平行でないときは0) FLOAT distanceLL(const L &l, const L &m) { return intersectLL(l, m) ? 0 : distanceLP(l, m[0]); } //直線と線分の距離(交わるときは0) FLOAT distanceLS(const L &l, const L &s) { if (intersectLS(l, s)) return 0; return min(distanceLP(l, s[0]), distanceLP(l, s[1])); } //線分と点の距離 FLOAT distanceSP(const L &s, const P &p) { const P r = projection(s, p); if (intersectSP(s, r)) return abs(r - p); return min(abs(s[0] - p), abs(s[1] - p)); } //線分と線分の距離(交わるときは0) FLOAT distanceSS(const L &s, const L &t) { if (intersectSS(s, t)) return 0; return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])), min(distanceSP(t, s[0]), distanceSP(t, s[1]))); } // P crosspoint(const L &l, const L &m) { FLOAT A = cross(l[1] - l[0], m[1] - m[0]); FLOAT B = cross(l[1] - l[0], l[1] - m[0]); if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line if (abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!! return m[0] + B / A * (m[1] - m[0]); } //多角形系 //点が多角形の内部/境界/外部のどこにあるかを判定する(O(n)) //凸多角形の場合はより高速な関数がある enum { OUT, ON, IN }; int contains(const G& poly, const P& point) { bool in = false; for (int i = 0; i < poly.size(); ++i) { P a = poly[i] - point, b = poly[(i + 1) % poly.size()] - point; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (cross(a, b) == 0 && dot(a, b) <= 0) return ON; } return in ? IN : OUT; } //多角形の面積 FLOAT areaOfG(const G& poly) { FLOAT A = 0; for (int i = 0; i < poly.size(); ++i) A += cross(poly[i], poly[(i + 1) % poly.size()]); return A / 2; } //凸多角形系 //凸包を構成する点集合をグラハムスキャンの亜種で求める G convexHull(G ps) { int n = ps.size(), k = 0; assert(n >= 3);//頂点が足りない sort(ps.begin(), ps.end()); G ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) // lower-hull while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) // upper-hull while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } //与えられた多角形が凸かを調べる bool isConvex(const G& poly) { for (int i = 0; i < poly.size(); ++i) if (ccw(poly[(i + poly.size() - 1) % poly.size()], poly[i], poly[(i + 1) % poly.size()])) return false; return true; } //凸多角形の直径をキャリパー法で求める(O(n log n)) FLOAT convexDiameter(const G& poly) { const int n = poly.size(); int is = 0, js = 0; for (int i = 1; i < n; ++i) { if (imag(poly[i]) > imag(poly[is]))is = i; if (imag(poly[i]) < imag(poly[js]))js = i; } FLOAT maxd = norm(poly[is] - poly[js]); int i = is, maxi = is; int j = js, maxj = js; auto diff = [](const G&pol, int i) { return pol[(i + 1) % pol.size()] - pol[i]; }; do { if (cross(diff(poly, i), diff(poly, j)) >= 0)j = (j + 1) % n; else i = (i + 1) % n; if (norm(poly[i] - poly[j]) > maxd) { maxd = norm(poly[i] - poly[j]); maxi = i; maxj = j; } } while (i != is || j != js); return maxd; } //凸多角形の頂点のうち直線から一番遠いものを二分探索で求める(O(log n)) //一般的な多角形についてしたい場合は、凸にしたうえで使用するとよい P convexExtreme(const G&poly, const L&l) { const int n = poly.size(); int a = 0, b = n; auto d = [&](int k) { return dot(poly[k], l[1] - l[0]); }; if (d(0) >= d(n - 1) && d(0) >= d(1))return poly[0]; while (a < b) { int c = (a + b) / 2; if (d(c) >= d(c - 1) && d(c) >= d(c + 1))return poly[c]; if (d(a + 1) > d(a)) { if (d(c + 1) <= d(c) || d(a) > d(c))b = c; else a = c; } else { if (d(c + 1) > d(c) || d(a) >= d(c))a = c; else b = c; } } assert(false); } //点が凸多角形の内部/境界/外部のどこにあるかを二分探索を用いて判定する(O(log n)) //enum { OUT, ON, IN }; int convexContains(const G&poly, const P&p) { const int n = poly.size(); P g = (poly[0] + poly[n / 3] + poly[2 * n / 3]) / 3.;//適当な3点の重心をとる int a = 0, b = n; while (a + 1 < b) { int c = (a + b) / 2; if (cross(poly[a] - g, poly[c] - g) > 0) { if (cross(poly[a] - g, p - g) > 0 && cross(poly[c] - g, p - g) < 0)b = c; else a = c; } else { if (cross(poly[a] - g, p - g) < 0 && cross(poly[c] - g, p - g) > 0)a = c; else b = c; } } b %= n; if (cross(poly[a] - p, poly[b] - p) < 0)return OUT; if (cross(poly[a] - p, poly[b] - p) > 0)return IN; return ON; } //相対変換 //点(a,b)と直線(y=a*x-b)を一対一で対応させる //言い換えによって解ける問題がある、らしい //反射性、距離の保存(pとlの距離とl*とp*の距離は等しい)、 //順序の保存(pがlの左/上/右にある⇔l*がp*の左/上/右にある)が成り立つ L dual(const P&p) { return L(P(0, -imag(p)), P(1, real(p) - imag(p))); } P dual(const L&l) { const P&p = l[0], &q = l[1]; return P(imag(p - q) / real(p - q), cross(p, q) / real(q - p)); } int N; LL x[123456], r[123456]; int main(void) { cin >> N; FLOAT lo = 0, hi = 1234567890123456.; for (int i = 0; i < N; ++i) { cin >> x[i] >> r[i]; hi = min(hi, (FLOAT)r[i]); } for (int loop = 0; loop < 50; ++loop) { FLOAT mid = (lo + hi) / 2; FLOAT lef = -1234567890123456.; FLOAT rht = +1234567890123456.; for (int i = 0; i < N; ++i) { FLOAT dif = sqrt(r[i] * r[i] - mid * mid); lef = max(lef,x[i] - dif); rht = min(rht,x[i] + dif); } if (lef > rht) { hi = mid; } else { lo = mid; } } cout << fixed << setprecision(10); cout << lo << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; int a; int main(){ cin>>a; cout<<(a+a*a+a*a*a); return 0; }
0