solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 5; const long long M = maxn; const long long mod = 1e9 + 7; long long n, val[maxn], ans = 1; long long vis[maxn], sz[maxn], mn, siz, rt; long long head[maxn], ecnt; struct edge { long long to, next, w, c; } e[maxn << 1]; struct BIT { long long c[maxn << 1], cnt[maxn << 1], fl[maxn << 1]; inline long long lowbit(long long x) { return x & -x; } void add(long long x, long long val, long long tag) { x += M; for (; x < maxn * 2; x += lowbit(x)) if (fl[x] == tag) c[x] = c[x] * val % mod, cnt[x]++; else c[x] = val, cnt[x] = 1, fl[x] = tag; } void query(long long x, long long tag, long long &p, long long &q) { p = 1, q = 0; x += M; for (; x; x -= lowbit(x)) if (fl[x] == tag) p = p * c[x] % mod, q += cnt[x]; } } T1, T2; struct node { long long s0, s1, w; } st[maxn]; long long idx; void adde(long long u, long long v, long long w, long long c) { e[++ecnt].to = v; e[ecnt].w = w; e[ecnt].c = c; e[ecnt].next = head[u]; head[u] = ecnt; } long long read() { long long res = 0, f = 1; char ch; do { ch = getchar(); if (ch == '-') f = -1; } while (!isdigit(ch)); do { res = res * 10 + ch - '0'; ch = getchar(); } while (isdigit(ch)); return res * f; } long long qpow(long long x, long long k) { long long res = 1; x = (x % mod + mod) % mod; while (k) { if (k & 1) res = res * x % mod; x = x * x % mod; k >>= 1; } return res; } void find(long long u, long long fa) { sz[u] = 1; long long mx = 0; for (long long i = head[u]; i; i = e[i].next) { long long v = e[i].to; if (v == fa || vis[v]) continue; find(v, u); sz[u] += sz[v]; mx = max(mx, sz[v]); } mx = max(mx, siz - sz[u]); if (mx < mn) mn = mx, rt = u; } void dfs(long long u, long long fa, long long s0, long long s1, long long w) { st[++idx] = (node){s0, s1, w}; for (long long i = head[u]; i; i = e[i].next) { long long v = e[i].to; if (v == fa || vis[v]) continue; dfs(v, u, s0 + (e[i].c == 0), s1 + (e[i].c == 1), w * e[i].w % mod); } } long long tag = 0; void calc(long long u, long long fa) { T1.add(0, 1, ++tag); T2.add(0, 1, tag); for (long long i = head[u]; i; i = e[i].next) { long long v = e[i].to; if (v == fa || vis[v]) continue; idx = 0; dfs(v, u, e[i].c == 0, e[i].c != 0, e[i].w); for (long long i = (1); i <= (idx); ++i) { long long x, y; T1.query(2 * st[i].s0 - st[i].s1, tag, x, y); ans = ans * qpow(st[i].w, y) % mod * x % mod; T2.query(st[i].s0 - st[i].s1 * 2 - 1, tag, x, y); ans = ans * qpow(qpow(st[i].w, y) * x % mod, mod - 2) % mod; } for (long long i = (1); i <= (idx); ++i) { T1.add(st[i].s1 - 2 * st[i].s0, st[i].w, tag); T2.add(st[i].s1 * 2 - st[i].s0, st[i].w, tag); } } } void solve(long long u) { vis[u] = 1; calc(u, 0); for (long long i = head[u]; i; i = e[i].next) { long long v = e[i].to; if (vis[v]) continue; mn = 0x3f3f3f3f; siz = sz[v]; find(v, 0); solve(rt); } } signed main() { n = read(); for (long long i = (2); i <= (n); ++i) { long long u = read(), v = read(), w = read(), c = read(); adde(u, v, w, c); adde(v, u, w, c); } mn = 0x3f3f3f3f, siz = n; find(1, 0); solve(rt); printf("%lld\n", ans); }
2,800
CPP
t = int(input()) for i in range(t): s = input() flag = "" d = 1 for j in s: if j == 'L' and flag != "R": flag = 'L' elif j == 'R' and flag != "L": flag = 'R' else: flag = "" break if flag == "L": d = len(s) + 1 elif flag == "R": d = 1 else: count = temp = 0 for k in s: if k == "R": count = max(count, temp) temp = 0 else: temp += 1 count = max(count, temp) d = count + 1 print(d)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX_INF = 0x7fffffff; const int MIN_INF = 0xa0000000; const int N = 1000009; long long ans[N]; inline int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } void init(void) { ans[1] = 1; ans[2] = 2; ans[3] = 6; long long tmp; long long lcm; for (int i = 4; i <= 1000000; i++) { if (i & 1) { ans[i] = i; ans[i] = ans[i] * (i - 1) * (i - 2); continue; } tmp = i; tmp = tmp * (i - 1); lcm = tmp * (i - 2); lcm /= 2; for (int k = i - 3; k >= 1; k -= 2) { if (tmp * k < lcm) break; if (gcd(k, i) == 1 && gcd(k, i - 1) == 1) { if (lcm < k * tmp) lcm = k * tmp; break; } } if (lcm < ans[i - 1]) ans[i] = ans[i - 1]; else ans[i] = lcm; } } int main() { int n; init(); while (cin >> n) { cout << ans[n] << endl; } return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int ans = INT_MAX; for (int i = 0; i < n; i++) { int temp = 0; for (int j = 0; j < n; j++) { int temp1 = 0; temp1 += 2 * abs(j - i); temp1 += 2 * abs(j); temp1 += 2 * abs(i); temp1 *= arr[j]; temp += temp1; } ans = min(ans, temp); } cout << ans; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, b, k, s; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b >> k; if (k % 2 == 0) s = (a * ((k / 2))) - (b * ((k / 2))); else s = (a * ((k / 2) + 1)) - (b * (k / 2)); cout << s << endl; } return 0; }
800
CPP
n = int(input()) i = input().split(' ') if '1' in i: print("HARD") else: print("Easy")
800
PYTHON3
for i in range(int(input())): n = input() a = [int(a) for a in input().split()] a.sort() b = [int(b) for b in input().split()] b.sort() for j in a: print(str(j) , end = ' ') print() for j in b: print(str(j) , end = ' ')
800
PYTHON3
def solve(p,s): dislikes = 0 for i in s: if i == "0": dislikes += 1 likes = len(s) - dislikes if likes == 0 or dislikes == 0: return p dislikedRating = [] likedRating= [] for i in range(len(p)): if s[i] == "0": dislikedRating.append((p[i],i)) else: likedRating.append((p[i],i)) dislikedRating.sort() likedRating.sort() ans = [-1 for i in range(len(p))] curCount = 1 for i in dislikedRating: rating,idx = i ans[idx] = curCount curCount += 1 for i in likedRating: rating,idx = i ans[idx] = curCount curCount += 1 return ans t = int(input()) while t: n = int(input()) a = [int(k) for k in input().split()] s = input() ans = solve(a,s) for i in ans: print(i,end=" ") print() t-=1
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n; long long a[5], b[5], ac[5], p[5]; void score(long long s[5], long long &x, long long &y) { int i; x = 0; y = 0; for (i = 0; i < 5; ++i) { if (a[i] >= 0) x += s[i] * (250 - a[i]) / 250; if (b[i] >= 0) y += s[i] * (250 - b[i]) / 250; } } bool ok(long long m) { int i; long long cac[5]; long long s[5]; for (i = 0; i < 5; ++i) cac[i] = ac[i] + p[i] * m; m += n; for (i = 0; i < 5; ++i) if (m < 2 * cac[i]) s[i] = 500; else if (m < 4 * cac[i]) s[i] = 1000; else if (m < 8 * cac[i]) s[i] = 1500; else if (m < 16 * cac[i]) s[i] = 2000; else if (m < 32 * cac[i]) s[i] = 2500; else s[i] = 3000; long long x, y; score(s, x, y); if (x > y) return true; return false; } int main() { ios::sync_with_stdio(false); int i, j; long long t; cin >> n; for (i = 0; i < 5; ++i) { cin >> a[i]; if (a[i] >= 0) ++ac[i]; } for (i = 0; i < 5; ++i) { cin >> b[i]; if (b[i] >= 0) ++ac[i]; } for (i = 2; i < n; ++i) for (j = 0; j < 5; ++j) { cin >> t; if (t >= 0) ++ac[j]; } for (i = 0; i < 5; ++i) if (a[i] >= 0 && b[i] >= 0 && a[i] > b[i]) p[i] = 1; else p[i] = 0; long long ans; for (ans = 0; ans < 4000; ++ans) if (ok(ans)) break; if (ans < 4000) cout << ans << endl; else cout << -1 << endl; return 0; }
2,000
CPP
n = int(input().split()[0]) LR = input() X = list(map(int, input().split())) if n == 1: print('-1') exit() L = [-1 for i in range(n)] for i, lr in reversed(list(enumerate(LR))): if lr == 'L': L[i] = X[i] else: L[i] = L[(i + 1) % n] mint = float('inf') for i, lr in enumerate(LR): if L[i] < 0: break if lr == 'R': t = L[i] - X[i] if mint > t: mint = t if mint == float('inf'): print('-1') else: print(mint // 2)
1,000
PYTHON3
#include <bits/stdc++.h> template <typename T, typename U, typename V> void impl__matrix_multiplication(const T& lhs, const U& rhs, V& res) { const auto& a = lhs; auto b = rhs.transposed(); for (std::size_t i = 0; i < lhs.rows(); ++i) { for (std::size_t j = 0; j < rhs.columns(); ++j) { for (std::size_t k = 0; k < rhs.rows(); ++k) { res[i][j] += a[i][k] * b[j][k]; } } } } template <typename T, typename Enable = std::true_type> struct impl__IdentityHelper {}; template <typename T> struct impl__IdentityHelper<T, typename std::is_arithmetic<T>::type> { static T identity(const T&) { return 1; } }; template <typename T> T identity(const T& sample) { return impl__IdentityHelper<T>::identity(sample); } template <typename T, typename Container> class Row { public: explicit Row(Container& value) : value(value) {} T& operator[](size_t index) { ; return value[index]; } const T& operator[](size_t index) const { ; return value[index]; } private: Container& value; }; template <typename T> class Matrix { public: Matrix(std::size_t n, std::size_t m, const T& value = T()) : value(n, std::vector<T>(m, value)) {} size_t rows() const { return value.size(); } size_t columns() const { return value[0].size(); } Row<T, std::vector<T>> operator[](std::size_t index) { ; return Row<T, std::vector<T>>(value[index]); } Row<const T, const std::vector<T>> operator[](std::size_t index) const { ; return Row<const T, const std::vector<T>>(value[index]); } Matrix& operator*=(const Matrix& rhs) { ; return *this = *this * rhs; } Matrix& operator+=(const Matrix& rhs) { ; for (size_t i = 0; i < rows(); ++i) { for (size_t j = 0; j < columns(); ++j) { value[i][j] += rhs.value[i][j]; } } return *this; } Matrix operator-() const { Matrix copy = *this; for (int i = 0; i < rows(); ++i) { for (int j = 0; j < columns(); ++j) { copy[i][j] = -copy[i][j]; } } return copy; } Matrix operator-=(const Matrix& rhs) { return *this += -rhs; } Matrix<T> transposed() const { Matrix<T> res(columns(), rows()); for (int i = 0; i < rows(); ++i) for (int j = 0; j < columns(); ++j) res[j][i] = (*this)[i][j]; return res; } private: std::vector<std::vector<T>> value; }; template <typename T> Matrix<T> operator*(const Matrix<T>& lhs, const Matrix<T>& rhs) { ; Matrix<T> res{lhs.rows(), rhs.columns()}; impl__matrix_multiplication(lhs, rhs, res); return res; } template <typename T, typename C1, typename C2> Matrix<T> operator+(Matrix<T> lhs, const Matrix<T>& rhs) { Matrix<T> copy = std::move(lhs); return copy += rhs; } template <typename T, typename C1, typename C2> Matrix<T> operator-(Matrix<T> lhs, const Matrix<T>& rhs) { Matrix<T> copy = std::move(lhs); return copy -= rhs; } template <typename T> struct impl__IdentityHelper<Matrix<T>> { static Matrix<T> identity(const Matrix<T>& sample) { ; Matrix<T> res(sample.rows(), sample.rows()); for (std::size_t i = 0; i < sample.rows(); ++i) { res[i][i] = ::identity(sample[0][0]); } return res; } }; template <typename T> T binpow(T base, long long power) { T result = identity<T>(base); ; while (power > 0) { if (power & 1) { result *= base; } power >>= 1; base *= base; } return result; } using namespace std; class TaskG2 { public: void solve(std::istream& in, std::ostream& out) { int n, k; in >> n >> k; vector<int> v(n); for (int i = 0; i < n; ++i) { in >> v[i]; --v[i]; } vector<int> obr(n); for (int i = 0; i < n; ++i) { obr[v[i]] = i; } Matrix<double> matrix(n * n, n * n); for (int wasi = 0; wasi < n; ++wasi) for (int wasj = 0; wasj < n; ++wasj) { for (int l = 0; l < n; ++l) { for (int r = l + 1; r <= n; ++r) { int newi = wasi; if (newi >= l && newi < r) newi = r + l - 1 - newi; int newj = wasj; if (newj >= l && newj < r) newj = r + l - 1 - newj; matrix[wasi + n * wasj][newi + n * newj] += 2.0 / n / (n + 1); } } } matrix = binpow(matrix, k); double ans = 0; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { int wasid = obr[i] + n * obr[j]; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { ans += matrix[wasid][i + n * j]; } } } out << ans << endl; } }; int main() { std::cin.sync_with_stdio(false); std::cin.tie(0); std::cout << std::fixed; std::cout.precision(20); TaskG2 solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
1,800
CPP
n = int(input()) faces = [] sum_faces = 0 dic = {'Tetrahedron':4,'Cube':6,'Octahedron':8,'Dodecahedron':12,'Icosahedron':20} for i in range(n): face = input() sum_faces+=dic[face] print(sum_faces)
800
PYTHON3
s = input() s = s.lower() s1 = input() s1 = s1.lower() if s < s1: print(-1) elif s == s1: print(0) else: print(1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double PI = acos(-1.0); const long long int mod = 1e9 + 7; const int MAXN = 1e6 + 5; struct edge { int wt, was_included, idx; edge() {} edge(int wt, int was_included, int idx) : wt(wt), was_included(was_included), idx(idx) {} const bool operator<(const edge &e) const { if (wt != e.wt) return wt < e.wt; else return was_included > e.was_included; } }; void cp() { int n, m; cin >> n >> m; vector<edge> edges; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edges.push_back(edge(a, b, i)); } sort(edges.begin(), edges.end()); int u = 2; queue<pair<int, int> > allowed; vector<pair<int, int> > ans(m); bool ok = true; for (edge e : edges) { if (e.was_included) { ans[e.idx] = {1, u}; if (u > 2) allowed.push({2, u}); u++; } else { if (!allowed.empty()) { pair<int, int> avail = allowed.front(); allowed.pop(); ans[e.idx] = avail; avail.first++; if (avail.first < avail.second) allowed.push(avail); } else ok = false; } } if (ok) { for (auto x : ans) cout << x.first << " " << x.second << '\n'; } else cout << -1 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; while (t--) { cp(); } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { char vowel[] = {'a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y'}; string str; getline(cin, str); int len = str.length(); for (int i = len - 1; i >= 0; --i) { if (isalpha(str[i])) { if (strchr(vowel, str[i])) { cout << "YES"; return 0; } else { cout << "NO"; return 0; } } } }
800
CPP
number = int(input()) x = 0 y = 0 z = 0 for i in range(number): t1,t2,t3 = tuple(list(map(int,input().split()))) x+=t1 y+=t2 z+=t3 if x == 0 and y == 0 and z == 0: print('YES') else: print('NO')
1,000
PYTHON3
#line 1 "c.cpp" /* Author : MatsuTaku Date : 04/16/21 Certificate: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDF9T447OEo8XSQ6O1AznN5tKC8mzvYc4Zs3+oOKfMqgLXfOwQnpfcoxKs4MAP1eICBD13PkcIezJ9IlV6nKQZKs1BQmvjSXJ+zKK8YCKvAueNPuNO0Bim43IBaNHNFWcMvcmUvHgRb6hUSO0V8I7OsjiFo20KDBj3gAznu9tir0Q== CompetitiveProgrammingCertification:[email protected] */ #include <bits/stdc++.h> using namespace std; #include <x86intrin.h> #line 4 "/mnt/c/Users/tkmma/competitive_programming/CPBF/library/include/unionfind.hpp" class UnionFind { private: std::vector<int> par_, sz_; public: explicit UnionFind(size_t size) : par_(size), sz_(size, 1) { std::iota(par_.begin(), par_.end(), 0); } int root(int u) { if (u == par_[u]) return u; return par_[u] = root(par_[u]); } bool are_union(int u, int v) { return root(u) == root(v); } int size_of(int u) { return sz_[root(u)]; } bool unite(int u, int v) { if (are_union(u, v)) return false; if (size_of(u) < size_of(v)) std::swap(u, v); sz_[root(u)] += size_of(v); par_[root(v)] = root(u); return true; } }; #line 12 "c.cpp" #define REP(i, f, n) for (auto i = (f), i##_len = (n); i < i##_len; i++) #define rep(i, n) REP(i, (int)0, n) #define RREP(i, f, n) for (auto i = (n)-1, i##_left = (f); i >= i##_left; i--) #define rrep(i, n) RREP(i, (int)0, n) using lint = long long int; using ulint = unsigned long long int; template <typename T> using vvec = vector<vector<T>>; template <typename T> vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); } template <typename T> T &chmax(T &dst, T x) { return dst = max(dst, x); } template <typename T> T &chmin(T &dst, T x) { return dst = min(dst, x); } class Solver { public: Solver(); void solve(); }; Solver::Solver() {} using Edge = tuple<int, int, int>; void Solver::solve() { int n, m; cin >> n >> m; vector<Edge> E(m); vector<unordered_set<int>> G(n); vector<int> D(n); int x = 0; rep(i, m) { int u, v, w; cin >> u >> v >> w; u--; v--; G[u].insert(v); G[v].insert(u); D[u]++; D[v]++; E[i] = {w, u, v}; x ^= w; } UnionFind cdsu(n); unordered_set<int> uc; rep(i, n) uc.insert(i); int rem = 0; while (!uc.empty()) { int i = *uc.begin(); uc.erase(uc.begin()); rem++; // if (D[i] == n-1) continue; queue<int> Q; Q.push(i); while (!Q.empty()) { int q = Q.front(); Q.pop(); vector<int> tmp; for (auto j : uc) { if (j == q) continue; if (G[q].count(j)) continue; if (!cdsu.are_union(q, j)) { tmp.push_back(j); } } for (auto j : tmp) { uc.erase(j); cdsu.unite(q, j); Q.push(j); } } } lint ans = 0; UnionFind dsu(n); sort(E.begin(), E.end()); for (auto [w, u, v] : E) { if (cdsu.are_union(u, v)) { if (!dsu.are_union(u, v)) { chmin(x, w); } } else { cdsu.unite(u, v); dsu.unite(u, v); ans += w; } } if ((lint)n * (n - 1) / 2 - m == n - rem) { ans += x; } cout << ans << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); Solver solver; int t = 1; // cin>>t; while (t--) { solver.solve(); } return 0; }
2,500
CPP
def find_dividor(l, r): print(l, l*2) if __name__ == '__main__': num = int(input()) touples = [] for i in range(0, num): l_r = [int(x) for x in input().split()] touples.append(l_r) for i in touples: find_dividor(i[0], i[1])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; string a, b, x = "R", y = "L"; char c[31] = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/'}; int main() { cin >> b; cin >> a; if (b == x) { for (int i = 0; i < a.size(); ++i) { for (int j = 0; j < 30; ++j) { if (a[i] == c[j]) cout << c[j - 1]; } } } else if (b == y) { for (int i = 0; i < a.size(); ++i) { for (int j = 0; j < 29; ++j) { if (a[i] == c[j]) cout << c[j + 1]; } } } return 0; }
900
CPP
str1 = input().lower() str2 = input().lower() i = 0 a = 0 while i < len(str1) and a == 0: if ord(str1[i]) < ord(str2[i]): a = -1 elif ord(str1[i]) > ord(str2[i]): a = 1 i = i+1 print(a)
800
PYTHON3
n = int(input()) s = list(input()) ans = 0 for i in range(0,n, 2): if s[i] == s[i+1]: if s[i] == "a": s[i] = "b" else: s[i] = "a" ans += 1 print(ans) print("".join(s))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; void read(int &k) { k = 0; char x = getchar(); while (x < '0' || x > '9') x = getchar(); while (x >= '0' && x <= '9') { k = k * 10 - 48 + x; x = getchar(); } } double D, U, yw, xb, yb, r; int main() { cin >> D >> U >> yw >> xb >> yb >> r; yw -= r; yb = 2 * yw - yb; long double k = (yb - (D + r)) / xb; long double b = D + r; long double A = k * k + 1, B = 2 * k * b - 2 * k * U, C = b * b + U * U - 2 * b * U - r * r; if (B * B - 4 * A * C - 1e-10 < 0) printf("%.10lf\n", double((yw - b) / k)); else puts("-1"); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 2003; const long long INF = 1000000009003003; int n; int xl[N], xr[N], y[N]; long long f(int x1, int y1_, int x2, int y2, int x3, int y3) { return (x1 - x3) * 1LL * (y1_ - y2) - (y1_ - y3) * 1LL * (x1 - x2); } long double getx(long double x, long double y, long double a, long double b) { long double k = -y / b; return x + k * a; } long double stg(int a, int b) { long double minu = INF; long double maxu = -INF; for (int i = 1; i <= n; ++i) { long double k = -y[i] / (long double)b; minu = min(minu, xl[i] + k * a); maxu = max(maxu, xr[i] + k * a); } return maxu - minu; } struct ban { int a, b; int z; ban() {} ban(int a, int b, int z) { this->a = a; this->b = b; this->z = z; } }; bool operator<(const ban& a, const ban& b) { if (f(0, 0, a.a, a.b, b.a, b.b) < 0) return true; if (f(0, 0, a.a, a.b, b.a, b.b) > 0) return false; return a.z > b.z; } void solv() { cin >> n; for (int i = 1; i <= n; ++i) cin >> xl[i] >> xr[i] >> y[i]; long double ans = INF; { vector<ban> v; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (y[i] > y[j]) { { int b = (y[j] - y[i]); int a = (xl[j] - xr[i]); v.push_back(ban(a, b, -1)); } { int b = (y[j] - y[i]); int a = (xr[j] - xl[i]); v.push_back(ban(a, b, 1)); } } } } v.push_back(ban(0, -1, 0)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (y[i] > y[j]) { if (getx(xl[i], y[i], -INF, -1) < getx(xl[j], y[j], -INF, -1)) { v.push_back(ban(xl[j] - xl[i], y[j] - y[i], 2 * (i * N + j) + 1)); } else { v.push_back(ban(xl[j] - xl[i], y[j] - y[i], 2 * (j * N + i) + 1)); } if (getx(xr[i], y[i], -INF, -1) > getx(xr[j], y[j], -INF, -1)) { v.push_back(ban(xr[j] - xr[i], y[j] - y[i], 2 * (i * N + j))); } else { v.push_back(ban(xr[j] - xr[i], y[j] - y[i], 2 * (j * N + i))); } } } } sort((v).begin(), (v).end()); int mini = 1, maxi = 1; for (int i = 1; i <= n; ++i) { if (getx(xl[i], y[i], -INF, -1) < getx(xl[mini], y[mini], -INF, -1)) mini = i; if (getx(xr[i], y[i], -INF, -1) > getx(xr[maxi], y[maxi], -INF, -1)) maxi = i; } int q = n; vector<ban> u; for (int i = 0; i < v.size(); ++i) { if (abs(v[i].z) <= 1) { if (max(q, q + v[i].z) == n) { ans = min(ans, getx(xr[maxi], y[maxi], v[i].a, v[i].b) - getx(xl[mini], y[mini], v[i].a, v[i].b)); } q += v[i].z; } else { if (v[i].z % 2 == 1) { v[i].z /= 2; if (v[i].z / N == mini) mini = v[i].z % N; } else { v[i].z /= 2; if (v[i].z / N == maxi) maxi = v[i].z % N; } } } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(9); int tt = 1; while (tt--) { solv(); } return 0; }
2,700
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:66777216") using namespace std; int inf = 1e9 + 7; long long INF = 1e18; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { ios_base::sync_with_stdio(false); int k; cin >> k; vector<long long> a; map<long long, pair<int, int> > sums; for (int i = 0; i < k; i++) { int n; long long sum = 0; cin >> n; a.resize(n); for (int j = 0; j < n; j++) { cin >> a[j]; sum += a[j]; } for (int j = 0; j < n; j++) { if (sums[sum - a[j]] != make_pair(0, 0) && sums[sum - a[j]].first != i + 1) { cout << "YES" << '\n'; cout << sums[sum - a[j]].first << " " << sums[sum - a[j]].second << '\n'; cout << i + 1 << " " << j + 1; return 0; } sums[sum - a[j]] = make_pair(i + 1, j + 1); } } cout << "NO"; return 0; }
1,400
CPP
# -*- coding: utf-8 -*- """ Created on Thu Jan 24 14:22:53 2019 @author: piyus """ n = int(input()) s = input() l = list(s) reps = [] i=0 while i<n: start = i end = i+1 while end<n and l[start]==l[end]: end+=1 if end - start>1: reps.append([start,end-1]) i=end #print(reps) ans = 0 for fix in reps: for j in range(fix[0]+1,fix[1]+1,2): mid = l[j] if j-1>=0: L = l[j-1] else: L = None if j+1<n: R = l[j+1] else: R = None don = {L,R} col = {'R','G','B'} can = col - don for e in can: take = e break l[j] = take ans+=1 print(ans) print(''.join(l))
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; long long v[100010]; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long n, l, r; cin >> n >> l >> r; for (int i = 1; i < n; ++i) v[i] = 2 * (n - i); v[n] = 1; for (int i = 1; i <= n; ++i) v[i] += v[i - 1]; int a = 0, b = n + 1; while (b - a > 1) { int mid = (a + b) / 2; if (v[mid] >= l) b = mid; else a = mid; } long long current = v[a]; long long pos = a + 1; while (current < l) { current += 2; pos++; } bool flag = current != l; long long lim = 1ll * n * (n - 1) + 1; for (long long i = l; i <= min(lim - 1, r); ++i) { if (flag) cout << a + 1 << " "; else cout << pos++ << " "; if (pos > n) { a++; pos = a + 2; } flag ^= 1; } if (lim == r) cout << "1"; cout << "\n"; } return 0; }
1,800
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("fast-math") #pragma GCC target("avx2,tune=native") using namespace std; vector<int> mult(vector<int> a, vector<int> b) { vector<int> c((int)a.size()); for (int i = 0; i < (int)a.size(); i++) c[i] = b[a[i]]; return c; } vector<int> binpow(vector<int> base, int st) { vector<int> res((int)base.size()); iota(res.begin(), res.end(), 0); while (st) { if (st & 1) res = mult(base, res); base = mult(base, base); st >>= 1; } return res; } string s; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s; int q; vector<int> ds((int)s.size()), sd; sd.resize((int)s.size()); iota(sd.begin(), sd.end(), 1); sd[sd.size() - 1] = 0; cin >> q; while (q--) { int k, d; cin >> k >> d; int l = 0; for (int i = 0; i < d; i++) for (int j = i; j < k; j += d) ds[l++] = j; for (int i = k; i < (int)s.size(); i++) ds[l++] = i; int cnt = (int)s.size() - k + 1; vector<int> res = mult(sd, ds); res = binpow(res, (int)s.size() - k + 1); res = mult(binpow(sd, (int)s.size() - cnt), res); string t; for (auto u : res) t += s[u]; cout << (s = t) << "\n"; } return 0; }
2,600
CPP
k = int(input()) l = int(input()) m = int(input()) n = int(input()) d = int(input()) min_number = min(k, l, m, n) if (min_number == 1): print(d) quit() if (min_number > d): print(0) quit() for i in range(1, d + 1): if (i % k != 0 and i % l != 0 and i % m != 0 and i % n != 0): d -= 1 print(d)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; struct node { int tail; int idx; }; node f(int tail, int idx) { node temp; temp.tail = tail; temp.idx = idx; return temp; } vector<node> g[10]; int deg[10] = {0}; int v[105] = {0}; stack<int> s; int all = 0; void dfs(int root) { for (int i = 0; i < g[root].size(); i++) { node temp = g[root][i]; if (!v[abs(temp.idx)]) { v[abs(temp.idx)] = 1; dfs(temp.tail); s.push(temp.idx); all++; } } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); g[a].push_back(f(b, i)); g[b].push_back(f(a, -i)); deg[a]++; deg[b]++; } int from = -1; int to = -1; int sign = 0; for (int i = 0; i <= 6; i++) { if (deg[i] % 2) { if (from == -1) from = i; else if (to == -1) to = i; else { sign = 1; break; } } } if (to == -1 && from != -1) sign = 1; if (sign == 1) printf("No solution\n"); else { if (from == -1) { for (int i = 0; i <= 6; i++) { if (deg[i] != 0) { from = i; break; } } } dfs(from); if (all != n) { printf("No solution\n"); return 0; } while (!s.empty()) { int now = s.top(); s.pop(); if (now < 0) printf("%d -\n", -now); else printf("%d +\n", now); } } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main(int, char**) { string str; cin >> str; vector<int> data; for (auto c : str) { if (c >= '0' && c <= '9') { data.push_back(int(c - '0')); } else if (c >= 'A' && c <= 'Z') { data.push_back(int(c - 'A') + 10); } else if (c >= 'a' && c <= 'z') { data.push_back(int(c - 'a') + 36); } else if (c == '_') { data.push_back(63); } else if (c == '-') { data.push_back(62); } } vector<unsigned long long> map(64, 0); for (int i = 0; i < 64; ++i) for (int j = 0; j < 64; ++j) { map[i & j] += 1; } unsigned long long res = 1; unsigned long long magicN = 1e9 + 7; for (auto d : data) { unsigned long long a = (res % magicN); unsigned long long b = (map[d] % magicN); res = a * b; } res %= magicN; cout << res; return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 10; int n, q, U[N], V[N], W[N], sz[N], h[N]; vector<int> adj[N]; double ans, prop; void dfs(int v = 0, int par = -1) { sz[v] = 1; for (int u : adj[v]) if (u ^ par) h[u] = h[v] + 1, dfs(u, v), sz[v] += sz[u]; } int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cout << setprecision(8) << fixed; cin >> n; prop = 1ll * n * (n - 1) * (n - 2) / 6; for (int i = 0; i < n - 1; i++) cin >> U[i] >> V[i] >> W[i], adj[--U[i]].push_back(--V[i]), adj[V[i]].push_back(U[i]); dfs(); for (int i = 0; i < n - 1; i++) { if (h[U[i]] > h[V[i]]) swap(U[i], V[i]); long long x = n - sz[V[i]]; double sv = sz[V[i]] * x * (x - 1) + x * sz[V[i]] * (sz[V[i]] - 1); ans += W[i] * sv / prop; } int r, w; cin >> q; while (q--) { cin >> r >> w, --r; long long x = n - sz[V[r]]; double sv = sz[V[r]] * x * (x - 1) + x * sz[V[r]] * (sz[V[r]] - 1); ans -= W[r] * sv / prop; ans += w * sv / prop; ; W[r] = w; cout << ans << '\n'; } }
1,900
CPP
n, a = map(int, input().split()) ans = opt = 400 for i in range(3, n + 1): cur = abs(180 * (n - i + 1) - a * n) if cur < opt: ans, opt = i, cur print(1, 2, ans)
1,300
PYTHON3
def Bina(n): s="" while n!=0: s+=str(n%2) n= n//2 return s def Zeros(b): ones=0 for i in range(b.__len__()): if("1"==b[i]): ones+=1 return 6-ones total=1 alphabet= list( '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-') dic = {} num=0 for i in alphabet: dic[i]= Zeros(Bina(alphabet.index(i)) ) M=int(1e9+7) for i in input(): if(i=="_"): continue num = dic[i] total= (total * 3**num )%M print(total)
1,500
PYTHON3
#include <bits/stdc++.h> const double PI = acos(-1.0); using namespace std; int setb(int n, int pos) { return n = n | (1 << pos); } int resb(int n, int pos) { return n = n & ~(1 << pos); } bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); } inline long long bigmod(long long b, long long p, long long m) { long long ret = 1, cur = b % m; while (p) { if (p & 1) ret = (ret * cur) % m; cur = (cur * cur) % m; p = p >> 1; } return ret; } int shit[10000005]; int n; int ara[200005][2], bra[200005][2]; multiset<int> lx, rx, ly, ry; void rid(int i) { int xx1 = ara[i][0], yy1 = ara[i][1]; int xx2 = bra[i][0], yy2 = bra[i][1]; lx.erase(lx.find(xx1)); ly.erase(ly.find(yy1)); rx.erase(rx.find(xx2)); ry.erase(ry.find(yy2)); } void add(int i) { lx.insert(ara[i][0]); ly.insert(ara[i][1]); rx.insert(bra[i][0]); ry.insert(bra[i][1]); } void solve() { multiset<int>::iterator it; it = lx.end(); it--; int xx1 = *it; it = rx.begin(); int xx2 = *it; it = ly.end(); it--; int yy1 = *it; it = ry.begin(); int yy2 = *it; if (xx1 <= xx2 && yy1 <= yy2) { printf("%d %d\n", xx1, yy1); exit(0); } } void super_solve() { int i; for (i = 1; i <= n; i++) { rid(i); solve(); add(i); } int oh = 10000000; while (oh--) { assert(false); assert(false); assert(false); assert(false); assert(false); assert(false); assert(false); assert(false); assert(false); assert(false); assert(false); assert(false); } } int main() { int i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d %d", &ara[i][0], &ara[i][1]); scanf("%d %d", &bra[i][0], &bra[i][1]); add(i); } super_solve(); return 0; }
1,600
CPP
a=int(input()) if (a%2==0 and a>2): print("Yes") else: print("no")
800
PYTHON3
for t in range(int(input())): n=int(input()) f=n//2 f=f*(f+1) ans=f f=1 p=ans for i in range(n//2): ans=ans+(p+f)*2 p=p+f f+=2 print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& dbg_print(ostream& out, const T& dbg_t) { out << "["; for (auto i : dbg_t) out << i << ", "; out << "]"; return out; } template <typename T> ostream& operator<<(ostream& out, const set<T>& dbg_t) { return dbg_print(out, dbg_t); } template <typename T> ostream& operator<<(ostream& out, const vector<T>& dbg_t) { return dbg_print(out, dbg_t); } template <typename S, typename T> ostream& operator<<(ostream& out, const pair<S, T>& rhs) { out << "(" << rhs.first << "," << rhs.second << ")"; return out; } template <typename T> void max_e(T& aaa, const T& bbb) { aaa = max(aaa, bbb); } template <typename T> void min_e(T& aaa, const T& bbb) { aaa = min(aaa, bbb); } int n; long long k; vector<long long> total, taken; long long delta(int i, long long x) { return total[i] - 3 * x * x + 3 * x - 1; } long long threshold(int i, long long A) { long long b = 0; long long e = total[i]; while (b < e) { long long s = (b + e + 1) / 2; if (delta(i, s) >= A) b = s; else e = s - 1; } return b; } long long common_threshold() { long long b = 0; for (int i = 0; i < int(n); ++i) min_e(b, delta(i, total[i])); long long e = 0; for (int i = 0; i < int(n); ++i) max_e(e, delta(i, 1)); while (b < e) { long long s = (b + e + 1) / 2; long long x_sum = 0; for (int i = 0; i < int(n); ++i) x_sum += threshold(i, s); if (x_sum >= k) b = s; else e = s - 1; } return b; } void solve() { long long A = common_threshold(); if (0) cout << "A" << " " << A << " " << endl; long long all_taken = 0; for (int i = 0; i < int(n); ++i) { taken[i] = threshold(i, A); all_taken += taken[i]; } for (int i = 0; i < int(n); ++i) if (all_taken > k && delta(i, taken[i]) == A) { taken[i]--; all_taken--; } assert(all_taken == k); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; total.assign(n, 0); for (int i = 0; i < int(n); ++i) cin >> total[i]; taken.assign(n, 0); solve(); for (int i = 0; i < int(n); ++i) cout << taken[i] << " "; cout << endl; return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; map<string, int> file; map<string, set<string>> folder; int dfs(string src) { if (src == ":)") return 0; int cnt = 0; for (string e : folder[src]) cnt += dfs(e); return cnt + 1; } int main() { int i, j, k; int n, m; string s, t, t2; while (cin >> s) { t = ""; n = s.length(); vector<string> vec; for (i = 0; i < n; i++) { if (s[i] == '\\') vec.push_back(t), t = ""; else t.push_back(s[i]); } t = vec[0]; n = vec.size(); for (i = 1; i < n; i++) { t.push_back(':'); t += vec[i]; file[t]++; } t = vec[0]; for (i = 1; i < n - 1; i++) { t += vec[i]; t.push_back(':'); t2 = t + vec[i + 1] + ":"; folder[t].insert(t2); } t += vec[n - 1]; t += ":"; folder[t].insert(":)"); } int ans2 = 0; auto itr = file.begin(); while (itr != file.end()) { ans2 = max(ans2, itr->second); itr++; } int ans1 = 0; auto itr2 = folder.begin(); while (itr2 != folder.end()) { ans1 = max(ans1, dfs(itr2->first) - 1); itr2++; } cout << ans1 << " " << ans2 << endl; return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int dp[1010][(1 << 8)], ct[9]; bool vis[1010][(1 << 8)], ok; int arr[1010], n, run, quan; vector<int> numbers[9]; set<int> masks; int solve(int ind, int mask) { if (mask == (1 << 8) - 1) return !(ok = true); if (ind == n) return -(1 << 30); masks.insert(mask); if (vis[ind][mask]) return dp[ind][mask]; vis[ind][mask] = true; if ((mask >> arr[ind]) & 1) { return dp[ind][mask] = solve(ind + 1, mask); } else { int pos = lower_bound(numbers[arr[ind]].begin(), numbers[arr[ind]].end(), ind) - numbers[arr[ind]].begin(); int next = solve(ind + 1, mask); int withr = -(1 << 30); int withr1 = -(1 << 30); assert(numbers[arr[ind]][pos] == ind); if (pos + run - 1 < numbers[arr[ind]].size()) withr = run + solve(numbers[arr[ind]][pos + run - 1] + 1, (mask | (1 << arr[ind]))); if (pos + run < numbers[arr[ind]].size()) withr1 = run + 1 + solve(numbers[arr[ind]][pos + run] + 1, (mask | (1 << arr[ind]))); return dp[ind][mask] = max(next, max(withr, withr1)); } } int main() { scanf("%d", &n); for (int e = 0; e < n; e++) scanf("%d", arr + e), arr[e]--; for (int e = 0; e < n; e++) numbers[arr[e]].push_back(e); int ans = 0; for (int e = 0; e < n; e++) ct[arr[e]] = 1; for (int e = 0; e < 9; e++) ans += ct[e]; int lo = 1, hi = n / 8 + 4; while (lo <= hi) { run = (lo + hi) >> 1; memset(vis, 0, sizeof vis); int cur = solve(0, 0); if (cur > 0) ans = cur, lo = run + 1; else hi = run - 1; } printf("%d\n", ans); return 0; }
2,200
CPP
from math import gcd import sys input=sys.stdin.readline from collections import defaultdict as dd t=int(input()) while t: n=int(input()) l=[0]*(n+1) ll=list(map(int,input().split())) for i in ll: l[i]+=1 l.sort() ans=l[-1] #print(l) for i in range(n-1,0,-1): l[i]=min(l[i],l[i+1]) if(l[i]!=l[i+1]): ans+=l[i] #print("lol",i) else: if(l[i]==0): break ans+=(l[i]-1) l[i]=l[i]-1 if(l[i]==0): break #print(l) print(ans) t-=1
1,400
PYTHON3
n=int(input()) c=0 for i in range(n): m=input() if m=="Icosahedron": c=c+20 if m=="Cube": c=c+6 if m=="Tetrahedron": c=c+4 if m=="Dodecahedron": c=c+12 if m=="Octahedron": c=c+8 print(c)
800
PYTHON3
# n=int(input()) # n,k=map(int,input().split()) '''l=0 r=10**13 while l+1<r: mid=(l+r)//2 val=(max(0,b_b*mid-b)*rb+max(0,b_s*mid-s)*rs+max(0,b_c*mid-b)*rc) if val>money: r=mid if val<=money: l=mid''' # arr=list(map(int,input().split())) # n=int(input()) # n,k=map(int,input().split()) # arr=list(map(int,input().split())) st=input() b,s,c=map(int,input().split()) rb,rs,rc=map(int,input().split()) money=int(input()) b_b=st.count('B') b_s=st.count('S') b_c=st.count('C') l,r=0,10000000000000 while 1<r-l: mid=(l+r)//2 val=((max(0,b_b*mid-b)*rb)+(max(0,b_s*mid-s)*rs)+(max(0,b_c*mid-c)*rc)) if val>money: r=mid else: l=mid #print(mid) print(l)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[11], dp[11][11], Cnt[11], ans, pos; inline void Add(int &x, int y) { x += y; if (x >= 1000000007) x -= 1000000007; } int dfs(int p, bool sta, int cnt) { if (p == -1) return !cnt; if (!sta && ~dp[p][cnt]) return dp[p][cnt]; int res = 0; int up = sta ? a[p] : 9; for (int i = (0), i_ = (up); i <= i_; i++) { if (i == 4 || i == 7) Add(res, dfs(p - 1, sta && i == a[p], cnt - 1)); else Add(res, dfs(p - 1, sta && i == a[p], cnt)); } if (!sta) dp[p][cnt] = res; return res; } void Dfs(int x, int mx, int cnt, int now) { if (cnt >= mx) return; if (x == 7) { Add(ans, now); return; } for (int i = (0), i_ = (pos); i <= i_; i++) if (Cnt[i]) { Cnt[i]--; Dfs(x + 1, mx, cnt + i, 1LL * now * (Cnt[i] + 1) % 1000000007); Cnt[i]++; } } void Deal(int x) { while (x) { a[pos++] = x % 10; x /= 10; } for (int i = (0), i_ = (pos); i <= i_; i++) { memset(dp, -1, sizeof dp); Cnt[i] = dfs(pos - 1, 1, i); } Cnt[0]--; for (int i = (1), i_ = (pos); i <= i_; i++) if (Cnt[i]) Dfs(1, i, 0, Cnt[i]); printf("%d\n", ans); } int main() { int m; scanf("%d", &m); memset(dp, -1, sizeof - 1); Deal(m); return 0; }
1,900
CPP
# coding: UTF-8 import sys #sys.setrecursionlimit(n) import heapq import re import bisect import random import math import itertools from collections import defaultdict, deque from copy import deepcopy from decimal import * n = int(input()) s = input() count = 0 for i in list(s): if i == '+': count += 1 else: if count > 0: count -= 1 print(count)
800
PYTHON3
t=int(input()) for i in range(t): l=list(map(int,input().split())) l.sort() a=min(l[2],l[3]) b=min(l[0],l[1]) print(a*b)
800
PYTHON3
#include <bits/stdc++.h> int n, m, su[21], tbl[1048577], gob[21]; char order[21], team[21]; bool check[1048577]; int back(int stat, int k) { if (k == m) return 0; if (check[stat]) return tbl[stat]; check[stat] = 1; int i; if (order[k] == 'p') { for (i = 0; i < n; i++) { if (!(stat & gob[i])) { tbl[stat] = back(stat + gob[i], k + 1); if (team[k]) tbl[stat] -= su[i]; else tbl[stat] += su[i]; return tbl[stat]; } } } else { int min = 0x7fffffff, max = -0x7fffffff; for (i = 0; i < n; i++) { if (!(stat & gob[i])) { back(stat + gob[i], k + 1); if (min > tbl[stat + gob[i]]) min = tbl[stat + gob[i]]; if (max < tbl[stat + gob[i]]) max = tbl[stat + gob[i]]; } } if (team[k]) tbl[stat] = min; else tbl[stat] = max; return tbl[stat]; } } int main() { int i, save[101]; char s[5]; gob[0] = 1; for (i = 1; i < 20; i++) gob[i] = gob[i - 1] << 1; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &save[i]); std::sort(save, save + n); scanf("%d\n", &m); for (i = 0; i < m; i++) { scanf("%s", s); order[i] = s[0]; scanf("%d", &team[i]); team[i]--; } for (i = 1; i <= m; i++) su[i - 1] = save[n - i]; n = m; printf("%d\n", back(0, 0)); return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int sum; scanf("%d", &sum); int A1, A2, A3, B1, B2, B3; cin >> A1 >> A2 >> A3 >> B1 >> B2 >> B3; int ans1 = (min(A1, B1 + B3) + min(A2, B2 + B1) + min(A3, B3 + B2)); int ans2 = (min(A1, B2) + min(A2, B3) + min(A3, B1)); cout << sum - ans1 << ' ' << ans2; return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; long long powermod(long long a, long long b) { if (b == 0) return 1; long long x = powermod(a, b / 2); x = x * x; if (b & 1) x = x * a; return x; } long long i, j, k; void solve() { long long n, x; cin >> n >> x; bool a[300] = {false}; for (i = 1; i <= n; i++) { cin >> k; a[k] = true; } long long ans = 0; for (i = 1; i <= 300 && x >= 0; i++) if (!a[i]) { x--, ans = i; } cout << ans - 1 << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; while (t--) solve(); return 0; }
900
CPP
n, k = map(int,input().split()) a = n//k if a % 2 != 0: print("YES") else: print("NO")
800
PYTHON3
import sys import re '''Abbreviate words over length 10 to <firstLetter><chars betweenn first and last letter><last letter> ''' def abbreviate(word): pattern = re.compile("[A-Za-z]+") if (pattern.fullmatch(word) and len(word)>10): result = word[0] + str(len(word)-2) + word[-1] print (result) elif (pattern.fullmatch(word) and len(word)<=10): result = word print (result) else: return null() def main(): words = [] lines = int(input()) for i in range(0,lines): w = input() words.append(w) for _ in range(0,lines): abbreviate(words[_]) if __name__ == "__main__": main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> bool remin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> bool remax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } int nxt() { int first; scanf("%d", &first); return first; } long long gcd(long long a, long long b) { a = abs(a); b = abs(b); while (b) { a %= b; swap(a, b); } return a; } struct pt { long double first, second; pt() {} pt(long double first, long double second) : first(first), second(second) {} inline pt operator-(const pt &r) const { return pt(first - r.first, second - r.second); } inline pt operator+(const pt &r) const { return pt(first + r.first, second + r.second); } inline pt operator*(const long double &r) const { return pt(first * r, second * r); } inline long double sqlen() const { return abs(first * first + second * second); } pt rot(long double co, long double si) const { return pt(first * co - si * second, si * first + co * second); } pt rot(long double ang) const { return rot(cosl(ang), sinl(ang)); } long double len() const { return sqrtl(sqlen()); } inline bool operator<(const pt &r) const { if (first != r.first) return first < r.first; return second < r.second; } inline bool operator==(const pt &r) const { return first == r.first && second == r.second; } }; ostream &operator<<(ostream &os, const pt &p) { return os << "(" << p.first << "," << p.second << ")"; } inline long double cross(const pt &l, const pt &r) { return l.first * r.second - l.second * r.first; } inline long double dot(const pt &l, const pt &r) { return l.first * r.first + l.second * r.second; } long long pwmod(long long a, long long n, long long mod) { long long ret = 1; while (n) { if (n & 1) ret = ret * a % mod; a = a * a % mod; n >>= 1; } return ret; } template <typename T> inline T sqr(T first) { return first * first; } bool remin(long long &first, long long second) { if (first > second) { first = second; return 1; } return 0; } const long long mod = 1000000007; const int N = 200 * 1000 + 10; pair<int, int> a[N]; long long f[N]; long long fi[N]; long long bin(long long n, long long k) { if (n < 0) return 0; if (k < 0 || k > n) return 0; return f[n] * fi[n - k] % mod * fi[k] % mod; } vector<int> t; int sz; void init(int nn) { sz = nn; t.assign(sz, 0); } int sum(int r) { int result = 0; for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r]; return result; } void inc(int i, int delta) { for (; i < sz; i = (i | (i + 1))) t[i] += delta; } int sum(int l, int r) { return sum(r) - sum(l - 1); } void solve(int test) { f[0] = fi[0] = 1; for (int i = 1; i < N; ++i) { f[i] = f[i - 1] * i % mod; fi[i] = pwmod(f[i], mod - 2, mod); } int n = nxt(); int k = nxt(); for (int i = 0; i < (int)(n); i++) { a[i].first = nxt(); a[i].second = nxt() + 1; } sort(a, a + n); vector<int> q; for (int i = 0; i < (int)(n); i++) { q.push_back(a[i].first); q.push_back(a[i].second); } sort((q).begin(), (q).end()); q.resize(unique((q).begin(), (q).end()) - q.begin()); init(q.size()); long long ans = 0; for (int i = 0, j = 0; i + 1 < q.size(); ++i) { while (j < n && a[j].first <= q[i]) { int ps = lower_bound((q).begin(), (q).end(), a[j].second) - q.begin(); inc(ps, 1); ++j; } long long cur = (q[i + 1] - q[i]) % mod; ans += cur * bin(sum(i + 1, (int)q.size() - 1), k) % mod; } cout << ans % mod << "\n"; } int main() { int t = 1; for (int i = 0; i < (int)(t); i++) { solve(i + 1); } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; vector<int> g[1111111]; double sum; int main() { double n, m; cin >> n >> m; for (int i = 1; i <= n - 1; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= n; i++) { if (g[i].size() == 1) { sum++; } } double ans = (m / sum) * 2; if (n == 2) { cout << m; } else { cout << fixed << setprecision(15) << ans; } }
1,700
CPP
#include <bits/stdc++.h> using namespace std; void getint(int &v) { char ch, fu = 0; for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') fu = 1, ch = getchar(); for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0'; if (fu) v = -v; } pair<int, int> q[111111]; int n, ans, tmpx[5555], tmpy[5555], T, val[5555], ts, a[555][555]; const int dx[] = {2, 0, 0, -2, 1, -1}; const int dy[] = {0, -2, 2, 0, 1, -1}; void pr() { puts("!"); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if ((i + j) % 2 == 1) a[i][j] ^= ans; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) printf("%d", a[i][j]); puts(""); } } void ask(int X1, int Y1, int X2, int Y2) { if (X1 + Y1 > X2 + Y2) swap(X1, X2), swap(Y1, Y2); printf("? %d %d %d %d\n", X1, Y1, X2, Y2); fflush(stdout); cin >> T; } void bfs() { int he = 0, ta = 1; q[1] = make_pair(2, 1); a[2][1] = 0; q[++ta] = make_pair(1, 1); while (he != ta) { pair<int, int> x = q[++he]; for (int d = 0; d <= 5; ++d) { pair<int, int> y = make_pair(x.first + dx[d], x.second + dy[d]); if (y.first < 1 || y.first > n || y.second < 1 || y.second > n) continue; if (a[y.first][y.second] != -1) continue; ask(x.first, x.second, y.first, y.second); if (T == 0) a[y.first][y.second] = a[x.first][x.second] ^ 1; else a[y.first][y.second] = a[x.first][x.second]; q[++ta] = y; } } } int main() { scanf("%d", &n); memset(a, -1, sizeof(a)); a[1][1] = 1; a[n][n] = 0; bfs(); for (int i = 1; i <= n; ++i) ++ts, tmpx[ts] = 1, tmpy[ts] = i, val[ts] = a[tmpx[ts]][tmpy[ts]]; for (int i = 2; i <= n; ++i) ++ts, tmpx[ts] = i, tmpy[ts] = n, val[ts] = a[tmpx[ts]][tmpy[ts]]; for (int i = 1; i <= ts; ++i) for (int j = i + 2; j <= min(i + 2, ts); j += 2) { if (val[i] == val[j]) { if (i - 1 >= 1 && val[i - 1] == val[j - 1]) { ask(tmpx[i - 1], tmpy[i - 1], tmpx[j], tmpy[j]); if (T == 1) ans = val[i - 1] ^ val[i]; else ans = val[i - 1] ^ val[i] ^ 1; pr(); return 0; } if (j + 1 <= ts && val[i + 1] == val[j + 1]) { ask(tmpx[i], tmpy[i], tmpx[j + 1], tmpy[j + 1]); if (T == 1) ans = val[i + 1] ^ val[i]; else ans = val[i + 1] ^ val[i] ^ 1; pr(); return 0; } } else { if (i - 1 >= 1 && val[i - 1] != val[j - 1]) { ask(tmpx[i - 1], tmpy[i - 1], tmpx[j], tmpy[j]); if (T == 1) ans = val[i - 1] ^ val[j]; else ans = val[i - 1] ^ val[j] ^ 1; pr(); return 0; } if (j + 1 <= ts && val[i + 1] != val[j + 1]) { ask(tmpx[i], tmpy[i], tmpx[j + 1], tmpy[j + 1]); if (T == 1) ans = val[i] ^ val[j + 1]; else ans = val[i] ^ val[j + 1] ^ 1; pr(); return 0; } } } return 0; }
2,400
CPP
from functools import reduce n = int(input()) a = list(map(int,input().split())) a = sorted(a,reverse = True) k = 0 t = 0 for i in range(0,n): t += a[i] q = 0 for i in range(0,n+1): if k>t: print(q) break q += 1 t -=a[i] k += a[i]
900
PYTHON3
n,h,m1=map(int,input().split()) m=0 d={} for i in range(m1): l,r,x=map(int,input().split()) for j in range(l,r+1): if j in d: if d[j]>x: d[j]=x else: d[j]=x m=0 for i in range(1,n+1): if i in d: m+=(d[i]*d[i]) else: m+=(h*h) print(m)
800
PYTHON3
n, k = map(int, input().split());count = 0 a = list(map(int, input().split())) k_th = a[k-1] for i in range(0, len(a)): if a[i] > 0 and a[i] >= k_th: count +=1 print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 322; const long long LINF = 2e18 + 474; const long long MAXN = 1e5 + 228; const long long MOD = 1e9 + 7; const double eps = 1e-12; int a[105]; int n, d; void solve() { int A; cin >> A; cout << 2 * A - 1 << " " << 2 << endl; cout << 1 << " " << 2; } int main() { if (!1) { freopen( "474" ".in", "r", stdin); freopen( "474" ".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); setlocale(LC_ALL, "Russian"); int T = 1; while (T--) { solve(); } return 0; }
1,400
CPP
for _ in range(int(input())): s = input() cur,res = 0, len(s) for i in range(len(s)): cur += 1 if s[i] == '+' else -1 if cur < 0: cur = 0 res += i + 1 print(res)
1,300
PYTHON3
for _ in range(int(input())): n,k=map(int,input().split()) s=list(input()) i=0 f=0 for g in range(k): i=g val=-1 while i<len(s): if s[i]!='?': val=s[i] break i+=k if val!=-1: i=g while i<len(s): if s[i]=='?': s[i]=val if s[i]!=val: f=1 break i+=k if f==1: break #print(s) ck=s[:k] if ck.count('1')<=(k//2) and ck.count('0')<=(k//2): pass else: f=1 if f==1: print('NO') else: print('YES')
1,500
PYTHON3
#!/usr/bin/env python # 580B_company.py - Codeforces.com/problemset/problem/580/B by Sergey 2015 import unittest import sys ############################################################################### # Company Class (Main Program) ############################################################################### class Company: """ Company representation """ def __init__(self, test_inputs=None): """ Default constructor """ it = iter(test_inputs.split("\n")) if test_inputs else None def uinput(): return next(it) if it else sys.stdin.readline().rstrip() # Reading single elements [self.n, self.d] = map(int, uinput().split()) # Reading multiple lines of pairs pairs = (" ".join(uinput() for i in range(self.n))).split() self.numa = [int(pairs[i]) for i in range(0, 2*self.n, 2)] self.numb = [int(pairs[i]) for i in range(1, 2*self.n, 2)] self.money = [(self.numa[i], self.numb[i]) for i in range(self.n)] self.money = sorted(self.money) self.partsum = [0] sum = 0 for i in range(self.n): sum += self.money[i][1] self.partsum.append(sum) def calculate(self): """ Main calcualtion function of the class """ result = 0 for i in range(self.n): mlb = self.money[i][0] mrb = mlb + self.d ilb = i irb = lbound(self.money, mrb) sumf = self.partsum[irb] - self.partsum[ilb] result = max(result, sumf) return str(result) def lbound(v, n): b = 0 e = len(v) while b != e: mid = (b + e) // 2 if v[mid][0] < n: b = mid + 1 else: e = mid return b ############################################################################### # Unit Tests ############################################################################### class unitTests(unittest.TestCase): def test_single_test(self): """ Company class testing """ # Constructor test test = "4 5\n75 5\n0 100\n150 20\n75 1" d = Company(test) self.assertEqual(d.n, 4) self.assertEqual(d.d, 5) self.assertEqual(d.numa, [75, 0, 150, 75]) self.assertEqual(d.numb, [5, 100, 20, 1]) # Sample test self.assertEqual(Company(test).calculate(), "100") # Sample test test = "5 100\n0 7\n11 32\n99 10\n46 8\n87 54" self.assertEqual(Company(test).calculate(), "111") # Sample test test = "" # self.assertEqual(Company(test).calculate(), "0") # My tests test = "" # self.assertEqual(Company(test).calculate(), "0") # Time limit test # self.time_limit_test(5000) def time_limit_test(self, nmax): """ Timelimit testing """ import random import timeit # Random inputs test = str(nmax) + " " + str(nmax) + "\n" numnums = [str(i) + " " + str(i+1) for i in range(nmax)] test += "\n".join(numnums) + "\n" nums = [random.randint(1, 10000) for i in range(nmax)] test += " ".join(map(str, nums)) + "\n" # Run the test start = timeit.default_timer() d = Company(test) calc = timeit.default_timer() d.calculate() stop = timeit.default_timer() print("\nTimelimit Test: " + "{0:.3f}s (init {1:.3f}s calc {2:.3f}s)". format(stop-start, calc-start, stop-calc)) if __name__ == "__main__": # Avoiding recursion limitaions sys.setrecursionlimit(100000) if sys.argv[-1] == "-ut": unittest.main(argv=[" "]) # Print the result string sys.stdout.write(Company().calculate())
1,500
PYTHON3
from sys import stdin if __name__ == '__main__': n = int(input()) num = list(map(int, stdin.readline().split())) def solve(n, num): nmax = 1000003 cur = nmax * [0] pt = [] res = 0 for x in range(len(num)): cur[num[x]] += 1 for i in range(nmax - 3, 1, -1): for j in range((((cur[i + 1] % 2) + cur[i]) // 2)): pt.append(i) if cur[i] > 0: cur[i] += cur[i + 1] % 2 for i in range(1, len(pt), 2): res += pt[i - 1] * pt[i] return res if __name__ == '__main__': print(solve(n, num))
1,600
PYTHON3
a=list(map(int, input().strip().split(' '))) if(a[1]==10): if(a[0]==1): print("-1") else: for i in range(0, a[0]-1): print("1", end='', sep='') print("0") else: for i in range(0,a[0]): print(a[1], end='', sep='')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; pair<double, double> p[1011]; double calc(pair<double, double> a, pair<double, double> b, double y) { return a.first + (a.first - b.first) * (y - a.second) / (a.second - b.second); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lf%lf", &p[i].first, &p[i].second); if (p[1].first > p[2].first) { swap(p[1], p[2]); reverse(p + 3, p + n + 1); for (int i = 3; i <= n; ++i) p[i].second = p[1].second + p[1].second - p[i].second; } double L(p[1].first), R(p[2].first); for (int i = 3; i < n; ++i) if (p[i + 1].second > p[i].second) L = max(calc(p[i], p[i + 1], p[1].second), L); else if (p[i + 1].second < p[i].second) R = min(calc(p[i], p[i + 1], p[1].second), R); else if (p[i + 1].first > p[i].first) { puts("0"); return 0; } L += -1e-11 + 1, R += 1e-11 + 1; printf("%d\n", max(int(R) - int(L), 0)); return 0; }
2,500
CPP
s = input() stack = [] for x in s: if len(stack) == 0: stack.append(x) else: if stack[-1] == x: stack.pop(-1) else: stack.append(x) print(''.join(stack))
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m"; void DBG() { cerr << "]" << _reset << endl; } template <class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } const int INF = 1e9 + 7, N = 3e5 + 7; const long long oo = 1e18; long long w[N]; long long dp[N]; vector<pair<int, pair<long long, long long>>> G[N]; pair<long long, long long> pe[N]; int p[N][20], d[N]; struct Data { long long a[2][2]; Data(long long a00 = oo, long long a01 = oo, long long a10 = oo, long long a11 = oo) { a[0][0] = a00, a[0][1] = a01, a[1][0] = a10, a[1][1] = a11; } friend Data operator+(Data lhs, Data rhs) { Data res; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) res.a[i][k] = min(res.a[i][k], lhs.a[i][j] + rhs.a[j][k]); return res; } void transpose() { swap(a[0][1], a[1][0]); } } up[N][20], self[N]; string to_string(Data d) { return "(" + to_string(d.a[0][0]) + ", " + to_string(d.a[0][1]) + ", " + to_string(d.a[1][0]) + ", " + to_string(d.a[1][1]) + ")"; } void dfs(int u, int pa = 0, int depth = 0) { p[u][0] = pa, d[u] = depth; for (int i = 1; i < 20; i++) p[u][i] = p[p[u][i - 1]][i - 1]; dp[u] = w[u]; for (auto [v, ws] : G[u]) if (v != pa) { pe[v] = ws; dfs(v, u, depth + 1); dp[u] = min(dp[u], dp[v] + ws.first + ws.second); } } void dfs2(int u, int pa = 0, long long dp_up = oo) { dp[u] = min(dp[u], dp_up); for (auto [v, ws] : G[u]) if (v != pa) { dfs2(v, u, dp[u] + ws.first + ws.second); } } int lca(int u, int v) { if (d[u] > d[v]) swap(u, v); assert(d[v] >= d[u]); for (int i = 0; i < 20; i++) if ((d[v] - d[u]) >> i & 1) v = p[v][i]; assert(d[v] == d[u]); if (u == v) return u; for (int i = 19; i >= 0; i--) if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i]; assert(p[u][0] == p[v][0]); return p[u][0]; } Data go_up(int u, int step) { Data res = self[u]; for (int i = 0; i < 20; i++) if (step >> i & 1) { res = res + up[u][i]; u = p[u][i]; } return res; } signed main() { std::ios::sync_with_stdio(0); std::cin.tie(0); ; int n; cin >> n; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 0; i < n - 1; i++) { int u, v; long long w1, w2; cin >> u >> v >> w1 >> w2; G[u].emplace_back(v, make_pair(w1, w2)); G[v].emplace_back(u, make_pair(w1, w2)); } dfs(1); dfs2(1); for (int i = 1; i <= n; i++) self[i] = Data(0, dp[i], dp[i], 0); for (int i = 2; i <= n; i++) { up[i][0] = Data(pe[i].first, oo, oo, pe[i].second) + self[p[i][0]]; } for (int j = 1; j < 20; j++) for (int i = 1; i <= n; i++) if (p[i][j]) up[i][j] = up[i][j - 1] + up[p[i][j - 1]][j - 1]; int q; cin >> q; while (q--) { int u, v; cin >> u >> v; assert(u != v); pair<int, int> uu = {(u - 1) / 2 + 1, (u - 1) & 1}; pair<int, int> vv = {(v - 1) / 2 + 1, (v - 1) & 1}; if (uu.first == vv.first) { cout << dp[uu.first] << '\n'; continue; } int l = lca(uu.first, vv.first); Data up = go_up(uu.first, d[uu.first] - d[l]), down = go_up(vv.first, d[vv.first] - d[l]); down.transpose(); Data res = up + down; cout << res.a[uu.second][vv.second] << '\n'; } }
2,700
CPP
#include <bits/stdc++.h> using namespace std; void test() { cerr << "\n"; } template <typename T, typename... Args> void test(T x, Args... args) { cerr << x << " "; test(args...); } const int MAXN = (int)1e5 + 10; const int MOD = 998244353; void Main() { int n; cin >> n; int pos = 0; vector<int> ve; vector<int> ans(n); vector<pair<int, int> > op; for (int i = 0; i < 2 * n; i++) { string s; int x; cin >> s; if (s == "+") { ve.push_back(pos++); op.push_back({0, 0}); } else { cin >> x; if (ve.empty()) { cout << "NO\n"; return; } op.push_back({1, x}); ans[ve.back()] = x; ve.pop_back(); } } set<int> s; pos = 0; for (auto p : op) { if (p.first == 0) { s.insert(ans[pos++]); } else { if (*s.begin() != p.second) { cout << "NO\n"; return; } s.erase(s.begin()); } } cout << "YES\n"; for (auto x : ans) cout << x << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; for (int ca = 1; ca <= T; ca++) { Main(); } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; template <typename X, typename Y> ostream &operator<<(ostream &o, pair<X, Y> &a) { o << "(" << a.first << "," << a.second << ")"; return o; } template <typename X> string join(X &a, const string delimiter) { stringstream ss; int i = 0; for (auto &x : a) { if (i++ > 0) ss << delimiter; ss << x; } return ss.str(); } struct edgew { int u; int v; long long w; edgew(int u, int v, long long w) { this->u = u; this->v = v; this->w = w; } void unpack(int &u, int &v, long long &w) { u = this->u; v = this->v; w = this->w; } }; ostream &operator<<(ostream &o, edgew &a) { o << "(" << a.u << "->" << a.v << "," << a.w << ")"; return o; } struct trees_lca { int T = 1, logn, n; vector<int> tin, tout; vector<long long> weight; vector<vector<int> > gpa; ~trees_lca() { gpa.clear(); } trees_lca(vector<vector<edgew> > &G) { int n = ((int)G.size()); int nx = 1, logn = 0; while (nx <= n) logn++, nx <<= 1; this->n = n, this->logn = logn; gpa.resize(logn); for (int k = 0; k < logn; k++) gpa[k].resize(n); tin.resize(n); tout.resize(n); weight.resize(n); for (int i = 0; i < n; i++) if (tin[i] == 0) dfs(G, i); for (int k = 1; k < logn; k++) for (int i = 0; i < n; i++) { gpa[k][i] = gpa[k - 1][gpa[k - 1][i]]; } } void dfs(vector<vector<edgew> > &G, int u) { tin[u] = T++; for (auto &e : G[u]) if (tin[e.v] == 0) { weight[e.v] = weight[u] + e.w; gpa[0][e.v] = u; dfs(G, e.v); } tout[u] = T; } bool is_ancestor(int an, int x) { return tin[an] <= tin[x] && tout[an] >= tout[x]; } int mth_parent(int i, int m) { for (int k = logn - 1; k >= 0; k--) if (m & (1 << k)) i = gpa[k][i]; return i; } int lca(int a, int b) { if (is_ancestor(a, b)) return a; if (is_ancestor(b, a)) return b; for (int k = logn - 1; k >= 0; k--) if (!is_ancestor(gpa[k][a], b)) a = gpa[k][a]; a = gpa[0][a]; return is_ancestor(a, b) ? a : -1; } long long dist(int a, int b) { int x = lca(a, b); return x != -1 ? weight[a] + weight[b] - 2 * weight[x] : (1LL << 60); } }; struct disjoint_set { vector<int> pa; disjoint_set(int n) { this->pa.resize(n); for (int i = 0; i < n; i++) this->pa[i] = i; } int find(int i) { return i == pa[i] ? i : pa[i] = find(pa[i]); } bool merge(int i, int j) { i = find(i); j = find(j); pa[i] = j; return i != j; } }; vector<long long> dijkstra(vector<vector<edgew> > &G, int src) { int n = ((int)G.size()); vector<long long> d(n); d.assign(n, (1LL << 60)); set<pair<long long, int> > s; s.insert(make_pair(d[src] = 0, src)); while (!s.empty()) { auto top = s.begin(); auto du = top->first; auto u = top->second; s.erase(top); if (d[u] != du) continue; for (auto &e : G[u]) if (du + e.w < d[e.v]) { s.insert(make_pair(d[e.v] = du + e.w, e.v)); } } return d; } int main() { int n, m; while (scanf("%d%d", &n, &m) == 2) { disjoint_set ds(n); vector<int> specials; vector<vector<edgew> > G(n); vector<vector<edgew> > GT(n); for (int i = 0; i < m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w), --u, --v; G[u].push_back(edgew(u, v, w)); G[v].push_back(edgew(v, u, w)); if (ds.merge(u, v)) { GT[u].push_back(edgew(u, v, w)); GT[v].push_back(edgew(v, u, w)); } else { specials.push_back(min(u, v)); } } trees_lca T(GT); vector<vector<long long> > d; for (auto &src : specials) { d.push_back(dijkstra(G, src)); } int nq; scanf("%d", &nq); for (int qtc = 0; qtc < nq; qtc++) { int u, v; scanf("%d%d", &u, &v), --u, --v; long long ans = T.dist(u, v); for (int j = 0; j < ((int)d.size()); j++) ans = min(ans, d[j][u] + d[j][v]); cout << ans << endl; } } return 0; }
2,400
CPP
# IAWT a, b = list(map(int, input().split())) x = min(a, b) y = 1 for i in range(1, x+1): y *= i print(y)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long check, tn, tm, maxn; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return a; } long long d = exgcd(b, a % b, x, y); long long z = x; x = y; y = z - y * (a / b); return d; } long long solve(long long x, long long y) { long long c = x + y, dx, dy; long long d = exgcd(tn, tm, dx, dy); if (c % d) return maxn + 1; long long mod = tm / d; dx *= c / d; if (mod < 0) mod = -mod; dx = (dx % mod + mod) % mod; long long ans = tn * dx - x; if (ans < 0 || ans > maxn) return maxn + 1; return ans; } int main() { long long n, m, k, x, y; cin >> n >> m >> k; maxn = n * m / gcd(n, m); tn = n << 1, tm = -(m << 1); for (int i = 1; i <= k; ++i) { cin >> x >> y; long long ans = maxn + 1; ans = min(ans, solve(x, y)); ans = min(ans, solve(x, -y)); ans = min(ans, solve(-x, y)); ans = min(ans, solve(-x, -y)); if (ans == maxn + 1) cout << -1 << endl; else cout << ans << endl; } }
1,800
CPP
def pal(s): a = s + '.' + s[::-1] n = len(a) j, p = 0, [0]*n for i in range(1, n): while j and a[j] != a[i]: j = p[j - 1] if a[j] == a[i]: j += 1 p[i] = j return s[:j] def longest(s): a, b = pal(s), pal(s[::-1]) return a if len(a) > len(b) else b def f(s): i, j = 0, len(s) - 1 while i < j and s[i] == s[j]: i, j = i + 1, j - 1 if i >= j: return s return s[:i] + longest(s[i:j+1]) + s[j+1:] for _ in range(int(input())): print(f(input()))
1,500
PYTHON3
s=input() f=0 for i in range(len(s)-6): if s[i:i+7]=="0000000" or s[i:i+7]=="1111111": f=1 break if f==0: print("NO") else: print("YES")
900
PYTHON3
a=int(input()) b=list(input()) if a == 24: if int(b[0]) == 2: if int(b[1]) > 3: b[1] = '0' if int(b[0]) > 2: b[0] = '1' if int(b[3]) > 5: b[3] = '5' if a == 12: if int(b[0]) == 1: if int(b[1]) > 2: b[1] = '0' if int(b[0]) > 1: if int(b[1]) > 0: b[0] = '0' if int(b[1]) == 0: b[0] = '1' if int(b[3]) > 5: b[3] = '5' if int(b[0]) == 0 and int(b[1])== 0: b[1] = '1' print(''.join(b))
1,300
PYTHON3
from functools import reduce from math import gcd import collections gcdm = lambda *args: reduce(gcd, args, 0) def pointsToLine2d(p1, p2): if p1 == p2: return 0, 0, 0 p1, p2 = sorted((p1, p2)) a, b, c = p2[1] - p1[1], p1[0] - p2[0], p1[1] * p2[0] - p1[0] * p2[1] g = gcdm(*filter(lambda x: x != 0, (a, b, c))) return a // g, b // g, c // g def main(): from sys import stdin, stdout def read(): return stdin.readline().rstrip('\n') def read_array(sep=None, maxsplit=-1): return read().split(sep, maxsplit) def read_int(): return int(read()) def read_int_array(sep=None, maxsplit=-1): return [int(a) for a in read_array(sep, maxsplit)] def write(*args, **kwargs): sep = kwargs.get('sep', ' ') end = kwargs.get('end', '\n') stdout.write(sep.join(str(a) for a in args) + end) def write_array(array, **kwargs): sep = kwargs.get('sep', ' ') end = kwargs.get('end', '\n') stdout.write(sep.join(str(a) for a in array) + end) n = read_int() p = [] for _ in range(n): p.append(read_int_array()) lines = set() for i in range(n): for j in range(i+1, n): lines.add(pointsToLine2d(p[i], p[j])) k = len(lines) ax_bx = collections.defaultdict(int) out = 0 for a, b, _ in lines: ax_bx[a, b] += 1 for x in ax_bx.values(): out += (k - x) * x write(out // 2) main()
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; long long s(int n) { long long r = 1, a = 4; while (n) { if (n & 1) r *= a; a *= a; n >>= 1; } return r; } int main() { int n; cin >> n; long long a; if (n >= 4) a = s(n - 4); long long ans = 24 * s(n - 3) + (n - 3) * 9 * 4 * a; cout << ans; }
1,700
CPP
k=0 while k<1: y=[int(x) for x in input().split()] k+=1 x=y a=x[0] b=x[1] c=x[2] d=int((a*(c*(c+1)))/2) if d>b: e=d-b else: e=0 print(e)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 10050; const int D = 600050; const int M = 2500050; const int INF = 20000000; int s, t, nds; namespace Maxflow { int dis[D], q[M], f, r, tot = 1; int head[D], to[M], nex[M], fl[M], now[D]; void addedge(int u, int v, int f) { tot++; to[tot] = v, nex[tot] = head[u], head[u] = tot, fl[tot] = f; tot++; to[tot] = u, nex[tot] = head[v], head[v] = tot, fl[tot] = 0; } bool spfa() { int i, j; q[f = r = 1] = s; for (i = 2; i <= nds; i++) dis[i] = INF; while (f <= r) { j = q[f++]; for (i = head[j]; i; i = nex[i]) { if (fl[i] && dis[to[i]] > dis[j] + 1) { dis[to[i]] = dis[j] + 1; q[++r] = to[i]; } } } return dis[t] != INF; } int dinic(int x, int flow) { if (x == t) return flow; for (int i = now[x]; i; now[x] = i = nex[i]) { if (fl[i] && dis[x] + 1 == dis[to[i]]) { int tmp = dinic(to[i], min(fl[i], flow)); if (tmp != 0) { fl[i] -= tmp; fl[i ^ 1] += tmp; return tmp; } } } return 0; } int maxflow() { int i, j, ans = 0; while (spfa()) { for (i = 1; i <= nds; i++) now[i] = head[i]; int tmp = 0; while (tmp = dinic(s, INF)) ans += tmp; } return ans; } } // namespace Maxflow using Maxflow ::addedge; int lc[M], rc[M], root[2 * N], typ; void build(int &p, int l, int r) { p = ++nds; if (l == r) { addedge(p, 2 + l, INF); return; } int md = l + r >> 1; build(lc[p], l, md); build(rc[p], md + 1, r); addedge(p, lc[p], INF); addedge(p, rc[p], INF); } void add(int &p, int las, int one, int l, int r, int x, int y) { p = ++nds; if (l == x && r == y) { if (typ == -1) { if (x == y) addedge(p, 2 + x, INF); else { lc[p] = lc[one]; rc[p] = rc[one]; addedge(p, lc[p], INF); addedge(p, rc[p], INF); } } return; } lc[p] = lc[las]; rc[p] = rc[las]; int md = l + r >> 1; if (y <= md) add(lc[p], lc[las], lc[one], l, md, x, y); else if (x > md) add(rc[p], rc[las], rc[one], md + 1, r, x, y); else { add(lc[p], lc[las], lc[one], l, md, x, md); add(rc[p], rc[las], rc[one], md + 1, r, md + 1, y); } addedge(p, lc[p], INF); addedge(p, rc[p], INF); } namespace io { const int L = (1 << 19) + 1; char ibuf[L], *iS, *iT, c; int f; inline char gc() { if (iS == iT) { iT = (iS = ibuf) + fread(ibuf, 1, L, stdin); return iS == iT ? EOF : *iS++; } return *iS++; } template <class I> void read(I &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; } }; // namespace io using io ::read; struct line { int dw, up, typ, x; line(int xx = 0, int y1 = 0, int y2 = 0, int tp = 0) { x = xx; dw = y1; up = y2; typ = tp; } inline bool operator<(const line &b) const { if (x == b.x) return typ < b.typ; return x < b.x; } } a[2 * N]; int n, m; int main() { int i, j, k, p, q; s = 1, read(n), read(m); t = 2, nds = 2 + n; build(root[0], 1, n); for (i = 1; i <= m; i++) { read(j), read(k), read(p), read(q); a[2 * i - 1] = line(k, j, p, 1); a[2 * i] = line(q + 1, j, p, -1); } sort(a + 1, a + 2 * m + 1); for (i = j = 1; i <= n; i++) { while (j <= 2 * m && a[j].x == i) typ = a[j].typ, add(root[j], root[j - 1], root[0], 1, n, a[j].dw, a[j].up), j++; addedge(2 + i, t, 1), addedge(s, root[j - 1], 1); } printf("%d\n", Maxflow ::maxflow()); return 0; }
3,400
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 100007; int MAXS = 997; const int MAXSS = 1100007; const long double PI = acos(-1.0); const long double EPS = 0.015; const long double RATIO = 0.9; struct cT { cT() : x(0), y(0) {} cT(long double _x, long double _y) : x(_x), y(_y) {} cT operator+(const cT &a) const { return cT(x + a.x, y + a.y); } void operator+=(const cT &a) { (*this) = (*this) + a; } cT operator-(const cT &a) const { return cT(x - a.x, y - a.y); } void operator-=(const cT &a) { (*this) = (*this) - a; } long double ang() { return atan2(y, x); } long double abs() { return x * x + y * y; } long double len() { return sqrt(abs()); } cT rot(long double ang) { long double ca = cos(ang), sa = sin(ang); return cT(x * ca - y * sa, x * sa + y * ca); } bool operator<(const cT &a) const { return x < a.x; } long double x, y; } c1[MAXN], c2[MAXN]; int n, n1, n2; pair<cT, int> s1[MAXN]; pair<long double, pair<int, int> > s2[MAXSS]; int ans2[MAXN]; bool vis1[MAXN]; void init(); void input(); void work(); void check(int a11, int a12, int a21, int a22); int main() { init(); input(); work(); } void init() { ios::sync_with_stdio(false); srand((unsigned)time(NULL)); } void input() { scanf("%d", &n); scanf("%d", &n1); double tx, ty; for (int i = 1; i <= n1; ++i) scanf("%lf%lf", &tx, &ty), c1[i].x = tx, c1[i].y = ty; scanf("%d", &n2); for (int i = 1; i <= n2; ++i) scanf("%lf%lf", &tx, &ty), c2[i].x = tx, c2[i].y = ty; } void work() { MAXS = (((MAXS) < (n)) ? (MAXS) : (n)); for (int i = 1; i <= n1; ++i) s1[i] = make_pair(c1[i], i); sort(s1 + 1, s1 + 1 + n1); int k2 = 0; for (int i = 1; i <= MAXS; ++i) { for (int j = i + 1; j <= MAXS; ++j) { s2[++k2] = make_pair((c2[j] - c2[i]).len(), make_pair(i, j)); } } sort(s2 + 1, s2 + 1 + k2); long double nd; int np; while (1) { int i = rand() % MAXS + 1, j = rand() % MAXS + 1; if (i ^ j) { nd = (c1[j] - c1[i]).len(); np = lower_bound(s2 + 1, s2 + 1 + k2, make_pair(nd, make_pair(0, 0))) - s2; for (int k = np; k <= k2; ++k) { if (s2[k].first - nd < EPS) check(i, j, s2[k].second.first, s2[k].second.second); else break; } for (int k = np - 1; k > 0; --k) { if (nd - s2[k].first < EPS) check(i, j, s2[k].second.first, s2[k].second.second); else break; } } } } void check(int a11, int a12, int a21, int a22) { long double ang1 = (c1[a12] - c1[a11]).ang(), ang2 = (c2[a22] - c2[a21]).ang(); long double ang = ang1 - ang2; cT ra21 = c2[a21].rot(ang); cT d = c1[a11] - ra21; int np, cc = 0; cT nr; for (int i = 1; i <= n2; ++i) { nr = c2[i].rot(ang) + d; np = lower_bound(s1 + 1, s1 + 1 + n1, make_pair(nr, 0)) - s1; for (int k = np; k <= n1; ++k) { if (s1[k].first.x - nr.x < EPS) { if ((((s1[k].first.y - nr.y) >= 0) ? (s1[k].first.y - nr.y) : (-(s1[k].first.y - nr.y))) < EPS && !vis1[s1[k].second]) { ans2[i] = s1[k].second; vis1[s1[k].second] = true; break; } } else break; } if (!ans2[i]) { for (int k = np - 1; k > 0; --k) { if (nr.x - s1[k].first.x < EPS) { if ((((s1[k].first.y - nr.y) >= 0) ? (s1[k].first.y - nr.y) : (-(s1[k].first.y - nr.y))) < EPS && !vis1[s1[k].second]) { ans2[i] = s1[k].second; vis1[s1[k].second] = true; break; } } else break; } } if (ans2[i]) ++cc; if (i >= 100 && cc * 5 < i) { for (int j = 1; j <= i; ++j) { vis1[ans2[j]] = false; ans2[j] = 0; } return; } } if (cc > RATIO * n) { int nc = 0; for (int i = 1; i <= n2; ++i) { if (ans2[i]) { printf("%d %d\n", ans2[i], i); if (++nc == n) break; } } if (nc < n) { int i1 = 1; for (int i = 1; i <= n2; ++i) { if (!ans2[i]) { while (vis1[i1]) ++i1; printf("%d %d\n", i1, i); vis1[i1] = true; if (++nc == n) break; } } } exit(0); } memset(ans2, 0, sizeof(ans2)); memset(vis1, 0, sizeof(vis1)); }
3,100
CPP
n = int(input()) a = [int(i) for i in input().split()] res = 0; for i in range(n): if a[i] == 1: res += 1 print(res, end='\n') for i in range(n): if a[i] == 1: j = i while j < n and a[j] - a[i] == j - i : j += 1 print(j-i,end=' '); i = j - 1
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a[50], count = 0, i; cin >> n >> k; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) { if (a[i] >= a[k - 1] && a[i] > 0) count++; } cout << count; }
800
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } const int maxn = 4e5 + 5; const long long mod = 1000000007; string s; long long pow4[105]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); pow4[0] = 1; for (int i = 1; i < 105; i++) { pow4[i] = 4 * pow4[i - 1]; pow4[i] %= mod; } cin >> s; reverse(s.begin(), s.end()); int n = s.length(); long long cur = 0; if (s[0] == '1') cur = 1; for (int i = 1; i < n; i++) { if (s[i] == '0') { cur *= 2; cur %= mod; } else { cur *= 2; cur %= mod; cur += pow4[i]; cur %= mod; } } cout << cur << endl; return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; const int mod = 1e9 + 7; char s[N], t[N]; long long f[N], invf[N], nnf[N]; int num[30]; int num2[30]; int n; long long qpow(int x, int b) { long long sum = 1; long long now = x; while (b) { if (b & 1) sum = sum * now % mod; now = now * now % mod; b >>= 1; } return sum; } void init() { f[0] = 1; for (int i = 1; i < N; i++) { f[i] = f[i - 1] * i % mod; } for (int i = 0; i < N; i++) { invf[i] = qpow(f[i], mod - 2); } for (int i = 0; i < N; i++) { nnf[i] = qpow(invf[i], mod - 2); } } void add(int &x, int y) { x += y; if (x > mod) x -= mod; } int solve(char *st, int len) { int ret = 0; for (int i = 0; i < len; i++) { int now = st[i] - 'a'; long long cns = 1; for (int j = 0; j < 26; j++) cns = cns * invf[num[j]] % mod; for (int j = now + 1; j < 26; j++) { if (num[j] == 0) continue; int res = len - i - 1; int tmp = f[res] * cns % mod; tmp = tmp * nnf[num[j]] % mod; tmp = tmp * invf[num[j] - 1] % mod; add(ret, tmp); } if (num[now] == 0) return ret; num[now]--; } return ret; } int main() { init(); scanf("%s %s", s, t); n = strlen(s); for (int i = 0; i < n; i++) { num[s[i] - 'a']++; } for (int i = 0; i < 26; i++) num2[i] = num[i]; int ans1 = solve(s, n); for (int i = 0; i < 26; i++) num[i] = num2[i]; int ans2 = solve(t, n); bool tmp = true; for (int i = 0; i < n; i++) num2[t[i] - 'a']--; for (int i = 0; i < 26; i++) if (num2[i] != 0) tmp = false; if (tmp) ans2++; cout << (ans1 - ans2 + mod) % mod << endl; return 0; }
2,100
CPP
n, b, a = map(int, input().split()) s = list(map(int, input().split())) b1, a1 = b, a k = 0 for c in s: if a1 == 0 and b1 == 0: break if c == 0: if a1 > 0: a1 -= 1 elif b1 > 0: b1 -= 1 else: break else: if a1 != a: if b1 > 0: b1 -= 1 a1 += 1 elif a1 > 0: a1 -= 1 else: break else: a1 -= 1 k += 1 print(k)
1,500
PYTHON3
n = int(input()) s = input().split(" "); for i in range(n): s[i] = int(s[i]) s = sorted(s) for i in range(n): print(s[i], end=" ")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } const int maxn = 5e5 + 7; int head[maxn], nxt[maxn], go[maxn], cnt, dep[maxn]; void add(int u, int v) { go[++cnt] = v; nxt[cnt] = head[u]; head[u] = cnt; } void dfs(int x, int f) { dep[x] = dep[f] + 1; for (int i = head[x]; i; i = nxt[i]) { if (go[i] == f) continue; dfs(go[i], x); } } int n; int main() { int u, v; n = read(); for (int i = 1; i <= n - 1; i++) { u = read(), v = read(); add(u, v); add(v, u); } dep[1] = 1; dfs(1, 0); double ans = (double)0; for (int i = 1; i <= n; i++) { ans += 1.0 / dep[i]; } printf("%.7lf", ans); return 0; }
2,200
CPP
def int_lst_input(): return [int(val) for val in input().split(' ')] def int_input(): return int(input()) def print_lst(lst): print(' '.join([str(val) for val in lst])) def solve(): n, m, z = int_lst_input() n_min, m_min = set(), set() # n_min = {n * i for i in range(1, z + 1) if n * i <= z} # m_min = {m * i for i in range(1, z + 1) if m * i <= z} i = 1 while n * i <= z: n_min.add(n * i) i += 1 i = 1 while m * i <= z: m_min.add(m * i) i += 1 print(len(n_min.intersection(m_min))) if __name__ == '__main__': solve()
800
PYTHON3
import copy def main(inp): t = int(inp()) for __ in range(t): def move(i, j, d, arr, commands): arr[i-1] -= d arr[j-1] += d commands.append((i, j, d//i)) assert arr[i-1] >= 0 def f(): n = int(inp()) arr = split_inp_int(inp) # calculate average total = sum(arr) if total % n != 0: print(-1) return avg = total//n commands = [] for i in range(1, n+1): if arr[i-1] % i: move(1, i, i - (arr[i-1] % i), arr, commands) move(i, 1, arr[i-1], arr, commands) for i in range(2, n+1): if arr[i-1] < avg: move(1, i, avg-arr[i-1], arr, commands) print(len(commands)) for command in commands: print(*command) for i in range(1, n): assert arr[i-1] == avg f() def split_inp_int(inp): return list(map(int, inp().split())) def use_fast_io(): import sys class InputStorage: def __init__(self, lines): lines.reverse() self.lines = lines def input_func(self): if self.lines: return self.lines.pop() else: return "" input_storage_obj = InputStorage(sys.stdin.readlines()) return input_storage_obj.input_func from collections import Counter, defaultdict from functools import reduce import operator import math def product(arr_): return reduce(operator.mul, arr_, 1) if __name__ == "__main__": main(input)
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; bool Primes[3001]; void CalcPrimes(int n) { int sqrtn = sqrt(n); Primes[0] = Primes[1] = 0; for (int i = 2; i <= sqrtn; i++) { if (Primes[i] == true) { for (int j = 2; i * j <= n; j++) { Primes[i * j] = false; } } } } int main() { int n; scanf("%d", &n); fill(Primes, Primes + n, true); CalcPrimes(n); int ans = 0; int cntr; for (int i = 1; i <= n; i++) { cntr = 0; for (int j = 1; j <= i; j++) { if (Primes[j] && (i % j) == 0) cntr++; } if (cntr == 2) ans++; } printf("%d\n", ans); return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int sg(long long x) { if (x < 4) return 0; if (x < 16) return 1; if (x < 82) return 2; if (x < 6724) return 0; if (x < 50626) return 3; if (x < 2562991876LL) return 1; return 2; } int main() { int n, ans = 0; long long x; scanf("%d", &n); for (int i = 1; i <= n; i++) cin >> x, ans ^= sg(x); if (ans) puts("Furlo"); else puts("Rublo"); return 0; }
2,200
CPP
def solve(): N = int(input()) taken = [False] * N princess = -1 for i in range(N): x = list(map(int, input().split())) who = -1 for j in range(x[0]): if not taken[ x[j + 1] - 1 ]: who = x[j + 1] - 1 taken[ x[j + 1] - 1 ] = True break if who == -1: princess = i prince = -1 for i in range(N): if not taken[i]: prince = i break if princess == -1 or prince == -1: print("OPTIMAL") else: princess += 1 prince += 1 print("IMPROVE") print(princess, end=" ") print(prince) T = int(input()) for i in range(T): solve()
1,200
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("03") using namespace std; signed main() { iostream::sync_with_stdio(0); cin.tie(0); long long tt; cin >> tt; for (; tt > 0; tt--) { long long n, k, l1, r1, l2, r2, ans; cin >> n >> k >> l1 >> r1 >> l2 >> r2; if (l1 > l2) { swap(l1, l2); swap(r1, r2); } if (r1 <= l2) { if (r2 - l1 >= k) { ans = l2 - r1 + k; k = 0; } else { ans = l2 - l1 + r2 - r1; k -= (r2 - l1); } if ((n - 1) * (r2 - l1) >= k) { ans += (k / (r2 - l1)) * (l2 - l1 + r2 - r1); k -= (k / (r2 - l1)) * (r2 - l1); ans += min(2 * k, l2 - r1 + k); } else { ans += (n - 1) * (l2 - l1 + r2 - r1); k -= (n - 1) * (r2 - l1); ans += 2 * k; } } else if (r1 <= r2) { if (n * (r1 - l2) >= k) ans = 0; else if (n * (r2 - l1) >= k) ans = k - n * (r1 - l2); else { ans = n * (l2 - l1 + r2 - r1); k -= n * (r2 - l1); ans += 2 * k; } } else { if (n * (r2 - l2) >= k) ans = 0; else if (n * (r1 - l1) >= k) ans = k - n * (r2 - l2); else { ans = n * (l2 - l1 + r1 - r2); k -= n * (r1 - l1); ans += 2 * k; } } cout << ans << "\n"; } return 0; }
2,100
CPP
import sys grid = [[int(x) for x in i.split(' ')] for i in sys.stdin.read().split('\n')[:5]] row = 0 for line in grid: if 1 in line: sys.stdout.write(str(abs(row - 2) + abs(line.index(1) - 2))) break row += 1
800
PYTHON3
from bisect import * n=int(input()) a=[int(x) for x in input().split()] a.sort() for _ in range(int(input())): m=int(input()) print(bisect_right(a,m))
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000010; int _w; int n; namespace Prelude { bool isp[MAXN]; int prm[MAXN], pcnt; int phi[MAXN]; int upc[MAXN]; int smallp[MAXN]; void solve() { memset(isp, 1, sizeof isp); isp[0] = isp[1] = 0; for (int i = 2; i <= n; ++i) { if (isp[i]) { prm[pcnt++] = i; phi[i] = i - 1; upc[i] = 1; smallp[i] = i; } for (int j = 0; j < pcnt; ++j) { int k = i * prm[j]; if (k > n) break; isp[k] = 0; smallp[k] = prm[j]; if (i % prm[j]) { phi[k] = phi[i] * phi[prm[j]]; upc[k] = upc[i] + 1; } else { phi[k] = phi[i] * prm[j]; upc[k] = upc[i]; break; } } } } } // namespace Prelude using namespace Prelude; namespace Solve0 { long long solve() { int g = n - 1; for (int i = n; i > n / 2; --i) if (isp[i]) --g; return (long long)n * (n - 1) / 2 - (long long)g * (g - 1) / 2; } } // namespace Solve0 namespace Solve1 { long long solve() { long long ans = 0; for (int i = 1; i <= n; ++i) ans += i - phi[i] - 1; return ans; } } // namespace Solve1 namespace Solve2 { long long part1() { long long ans = 0; int sump = 1; for (int i = 2; i <= n; ++i) { if (!isp[i]) ans += phi[i] - sump + upc[i]; sump += isp[i]; } return ans; } int ssp[MAXN]; long long part2() { for (int i = 2; i <= n; ++i) if (!(i > n / 2 && isp[i])) ++ssp[smallp[i]]; for (int i = 3; i <= n; ++i) ssp[i] += ssp[i - 1]; long long ans = 0; for (int i = 2; i <= n / 2; ++i) if (isp[i]) { ans += ssp[n / i]; for (int j = i; j <= n; j += i) if (smallp[j] <= n / i) --ans; } return ans; } int spc[MAXN]; long long part3() { for (int i = 2; i <= n / 2; ++i) spc[i] = spc[i - 1] + isp[i]; long long ans = 0; int sump = 0; for (int i = 2; i <= n / 2; ++i) if (isp[i]) ans += min(spc[n / i], sump++); return ans; } long long solve() { return part1() + part2() - part3(); } } // namespace Solve2 namespace Solve { void solve() { long long ans = 0, tot = (long long)n * (n - 1) / 2; tot -= Solve0::solve(); long long c1 = Solve1::solve(); ans += c1, tot -= c1; ans += tot * 3 - Solve2::solve(); printf("%lld\n", ans); } } // namespace Solve int main() { _w = scanf("%d", &n); Prelude::solve(); Solve::solve(); return 0; }
2,700
CPP
n, m = map(int, input().split()) num = [] used = [] usedstr = [0] * n fl = True usedstolb = [0] * m for i in range(n): num.append(input()) for i in range(n): if usedstr[i] == 0: stroki = [] stolbi = [] for j in range(m): if num[i][j] == '#': stolbi.append(j) for x in range(n): if num[x][j] == '#' and x not in stroki: stroki.append(x) for x in range(len(stroki)): for y in range(m): if num[stroki[x]][y] == '#' and y not in stolbi: stolbi.append(y) for x in range(len(stolbi)): if usedstolb[stolbi[x]]: fl = False break else: usedstolb[stolbi[x]] = 1 for x in range(len(stroki)): if usedstr[stroki[x]]: fl = False break else: usedstr[stroki[x]] = 1 for x in range(len(stroki)): for y in range(len(stolbi)): if num[stroki[x]][stolbi[y]] == '.': fl = False break if not fl: break if fl: print('Yes') else: print('No')
1,300
PYTHON3
def main(): num, len_r = (map(int, input().split())) lan = list(map(int, input().split())) lan.sort() max_d = 0 for i in range(1, len(lan)): max_d = max(max_d, (lan[i]-lan[i-1])/2) if lan[0] != 0: max_d = max(max_d, (lan[0] - 0)) if lan[-1] != len_r: max_d = max(max_d, (len_r - lan[-1])) print(max_d) if __name__ == '__main__': main()
1,200
PYTHON3
n = int(input()) arr = [] for i in range(n): arr.append(tuple(map(int,input().split()))) arr = list(enumerate(arr)) while(len(arr)>0): p = arr[0] x,y,z = p[1] q = sorted(arr, key = lambda i: abs(i[1][0]-x)+abs(i[1][1]-y)+abs(i[1][2]-z))[1] print(p[0]+1,q[0]+1) arr.remove(q) del arr[0]
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; bool comp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } int main() { int n; cin >> n; pair<int, int> a[n]; for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i + 1; } sort(a, a + n, comp); long long int sum1 = 0, sum2 = 0; vector<int> t1, t2; int ct = 1, ct1 = 1; int g; if (n % 2 == 0) g = n / 2; else g = n / 2 + 1; for (int j = n - 1; j >= 0; j--) { if (abs(sum1 - sum2 + a[j].first) <= a[n - 1].first && ct <= g) { if (ct == g && n % 2 == 1) g--; sum1 += a[j].first; t1.push_back(a[j].second); ct++; } else if (ct1 <= g) { if (ct1 == g && n % 2 == 1) g--; sum2 += a[j].first; t2.push_back(a[j].second); ct1++; } } cout << t1.size() << endl; for (auto i : t1) cout << i << " "; cout << endl << t2.size() << endl; for (auto i : t2) cout << i << " "; return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL); map<string, long long int> hash{{"C", 0}, {"C#", 1}, {"D", 2}, {"D#", 3}, {"E", 4}, {"F", 5}, {"F#", 6}, {"G", 7}, {"G#", 8}, {"A", 9}, {"B", 10}, {"H", 11}}; string a, b, c; cin >> a >> b >> c; vector<long long int> arr({hash[a], hash[b], hash[c]}); sort(arr.begin(), arr.end()); bool ans = false; if (arr[1] - arr[0] == 3 && arr[2] - arr[1] == 4) { ans = true; } else if (arr[2] - arr[1] == 3 && arr[0] + 12 - arr[2] == 4) { ans = true; } else if (arr[0] + 12 - arr[2] == 3 && arr[1] - arr[0] == 4) { ans = true; } bool major = false; if (arr[1] - arr[0] == 4 && arr[2] - arr[1] == 3) { major = true; } else if (arr[2] - arr[1] == 4 && arr[0] + 12 - arr[2] == 3) { major = true; } else if (arr[0] + 12 - arr[2] == 4 && arr[1] - arr[0] == 3) { major = true; } if (ans) cout << "minor"; else if (major) cout << "major"; else cout << "strange"; }
1,200
CPP
""" instagram : essipoortahmasb2018 telegram channel : essi_python """ i = input n = int(i()) l = [*map(int,i().split())] l.sort() c = 0 for j in range(n-1): c+= l[j+1]-l[j]-1 print(c) #8 10 12 13
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, a[200000], d[200000]; int main() { string s; cin >> n >> s; for (long long int i = 0; i < n; i++) { cin >> a[i]; } long long i = 0; do { if (d[i] == 1) { cout << "INFINITE"; return 0; } d[i] = 1; if (s[i] == '<') i -= a[i]; else i += a[i]; } while (i >= 0 && i < n); cout << "FINITE"; }
1,000
CPP