solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int n, a[26], p, i; string s; int f(int a) { return (a * (a - 1)) / 2; } int main() { for (cin >> n; n--;) cin >> s, a[s[0] - 97]++; for (; i < 26; i++) p += (f(a[i] / 2) + f(a[i] - a[i] / 2)); cout << p; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--){ string gg; vector<char> nn; int n; cin>>n; cin>>gg; nn.push_back(gg[0]); for (int i = 1; i < gg.size(); i++) if (gg[i]!=gg[i-1]) nn.push_back(gg[i]); // for (int i = 0; i < nn.size(); i++) // { // cout<<nn[i]; // } int res=1; for (int i = 0; i < nn.size() && res==1; i++) { for (int j = 0; j < nn.size(); j++) { if(i==j) continue; if(nn[i]==nn[j]) { res=0; } } } if (res==0) cout<<"NO"<<endl; if (res==1) cout<<"YES"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; const int xn = 3e5 + 10; const int xm = 5e5 + 10; const int SQ = 700; const long long inf = 1e18 + 10; int a[xn][8], n, m; int f[300], ff[300]; bool fl[xn]; pair<int, int> ans; void bin(int s) { int ss = s; for (int i = 0; i < 10; i++) fl[i] = 0; int t = 0; while (s) { if (s % 2) fl[t] = 1; t++; s /= 2; } int mx = -1, dx = -1; for (int i = 0; i < n; i++) { int mn = -1; for (int j = 0; j < m; j++) { if (!fl[j]) continue; if (mn == -1) mn = a[i][j]; mn = min(mn, a[i][j]); } if (mx == -1 || mx < mn) dx = i + 1; mx = max(mx, mn); } f[ss] = dx; ff[ss] = mx; } bool check(int x) { for (int i = 1; i < pow(2, m) - 1; i++) { if (ff[i] >= x && ff[int(pow(2, m) - i - 1)] >= x) { ans = {f[i], f[int(pow(2, m) - i - 1)]}; return true; } } int k = pow(2, m) - 1; if (ff[k] >= x) { ans = {f[k], f[k]}; return true; } return false; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; for (int i = 0; i < pow(2, m); i++) bin(i); int l, r, mid; l = 0, r = 1e9 + 10; while (r - l > 1) { mid = (l + r) / 2; if (check(mid)) l = mid; else r = mid; } if (l == 0) { cout << 1 << ' ' << 1 << '\n'; return 0; } cout << ans.first << ' ' << ans.second << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { time_t START = clock(); int n, m, d = 1; cin >> n >> m; int i; for (i = 0; i < 2 * m; i += 2) { cout << d << ' ' << d + 1 << ' '; d += 2; } for (i; i < 2 * n; i += 2) { cout << d + 1 << ' ' << d << ' '; d += 2; } time_t FINISH = clock(); cerr << "Time = " << double(FINISH - START) / CLOCKS_PER_SEC << endl; return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; long long i, j, x, y, z, m, n, k, l, r, f, mi = 1e18, ans, x2, y2, ma, t, a[1010000]; string s, s2; vector<long long> v[410000], v2, v3; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; t = m; ans = 0; if ((n + 1) * n / 2 > m) { cout << -1; return 0; } for (i = 1; i <= n; i++) { a[i] = n - i + 1; ans += max(a[i], i); } if (ans <= m) { cout << ans << endl; for (i = 1; i <= n; i++) cout << i << ' '; cout << endl; for (i = 1; i <= n; i++) cout << a[i] << ' '; return 0; } m -= ((n + 1) * n / 2); l = -1; for (i = 1; i <= n / 2; i++) { if (m >= (a[i] - i)) { m -= (a[i] - i); } else { swap(a[i], a[n - i + 1]); if (l == -1) l = n - i + 1; } } if (m == 1) { if (l != -1) swap(a[l], a[l - 1]); else swap(a[n / 2 + (n % 2)], a[n / 2 + (n % 2) - 1]); } cout << t << endl; for (i = 1; i <= n; i++) cout << i << ' '; cout << endl; for (i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; }
8
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long md) { long long res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x = (x * x) % md; y = y >> 1; } return res % md; } vector<long long> v[100010]; vector<long long> second(100010); vector<long long> a(100010, 0); vector<bool> visit(100010, false); void dfs(long long x, long long presum) { if (visit[x]) return; visit[x] = true; if (second[x] != -1) { second[x] -= presum; } for (long long i = 0; i < v[x].size(); ++i) { if (second[x] != -1) { dfs(v[x][i], second[x] + presum); } else { dfs(v[x][i], presum); } } } int main() { long long n; cin >> n; for (long long i = 0; i < n - 1; ++i) { long long x; cin >> x; v[x].push_back(i + 2); } vector<long long> t(n + 1, 0); for (long long i = 0; i < n; ++i) { cin >> second[i + 1]; t[i + 1] = second[i + 1]; } long long ans = second[1]; dfs(1, 0); bool flag = false; for (long long i = 0; i < n; ++i) { long long mn = 1e15; if (second[i + 1] == -1) { for (long long j = 0; j < v[i + 1].size(); ++j) { mn = min(mn, second[v[i + 1][j]]); } for (long long j = 0; j < v[i + 1].size(); ++j) { ans += (second[v[i + 1][j]] - mn); } if (mn != 1e15) ans += mn; } if (t[i + 1] != -1 && second[i + 1] < 0) { flag = true; break; } } if (flag) ans = -1; cout << ans; }
4
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-11; const int INFINITE = 0x3f3f3f3f; template <class T> inline void checkmin(T &a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, T b) { if (b > a) a = b; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T lowbit(T n) { return (n ^ (n - 1)) & n; } template <class T> inline int countbit(T n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } typedef vector<int> VI; typedef vector<long long> VI64; typedef vector<VI> VII; typedef vector<string> VS; #pragma comment(linker, "/STACK:200000000") void rotateClockwise(VII &b) { int w = b.size(); int h = b[0].size(); VII bb; bb.resize(h); for (int i = (int)(0); i < (int)(h); i++) bb[i].resize(w); for (int i = (int)(0); i < (int)(h); i++) for (int j = (int)(0); j < (int)(w); j++) bb[i][j] = (b[j][i]); b = bb; } void zoomInTwice(VII &b) { int w = b.size(); int h = b[0].size(); VII bb; bb.resize(2 * w); for (int i = (int)(0); i < (int)(2 * w); i++) bb[i].resize(2 * h); int r = 0; for (int i = (int)(0); i < (int)(w); i++) { int c = 0; for (int j = (int)(0); j < (int)(h); j++) { bb[r][c] = b[i][j]; bb[r][c + 1] = b[i][j]; bb[r + 1][c] = b[i][j]; bb[r + 1][c + 1] = b[i][j]; c += 2; } r += 2; } b = bb; } void solve() { int w, h; cin >> h >> w; VS b; b.resize(w); for (int i = (int)(0); i < (int)(w); i++) cin >> b[i]; VII bi; bi.resize(w); for (int i = (int)(0); i < (int)(w); i++) for (int j = (int)(0); j < (int)(h); j++) { bi[i].push_back(b[i][j] == '*' ? 1 : 0); } ::rotateClockwise(bi); ::zoomInTwice(bi); int nw = bi.size(); int nh = bi[0].size(); for (int i = (int)(0); i < (int)(nw); i++) for (int j = (int)(0); j < (int)(nh); j++) { cout << (bi[i][j] == 1 ? '*' : '.'); if (j == nh - 1) cout << "\n"; } } int main() { ios::sync_with_stdio(false); ::solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 5; const int inf = 1e9; int n, m, c; int ans[MAXN]; void insert1(int x) { for (int i = 1; i <= n; ++i) { if (!ans[i] || ans[i] > x) { cout << i << endl; ans[i] = x; return; } } } void insert2(int x) { for (int i = n; i >= 1; --i) { if (!ans[i] || ans[i] < x) { cout << i << endl; ans[i] = x; return; } } } bool check() { for (int i = 1; i <= n; ++i) if (!ans[i]) return 0; return 1; } int main() { scanf("%d%d%d", &n, &m, &c); int mid = (c + 1) / 2; int x; while (cin >> x) { if (x > mid) insert2(x); else insert1(x); if (check()) break; } return 0; }
6
#include <bits/stdc++.h> using namespace std; set<int> s[100005]; int cnt[1000005]; int main() { int n, A; scanf("%d%d", &n, &A); for (int i = 1; i <= 1e6; i++) s[0].insert(i); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); cnt[x]++; if (x == A) { s[cnt[x] - 1].clear(); s[cnt[x]].insert(x); } else if (s[cnt[x] - 1].find(x) != s[cnt[x] - 1].end()) { s[cnt[x] - 1].erase(x); s[cnt[x]].insert(x); } } s[cnt[A]].erase(A); for (int i = 0; i <= 1e5; i++) { if (!s[i].empty()) { printf("%d", *s[i].begin()); return 0; } } printf("-1"); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10, SQ = 500; int n, q, l, r, ans, cnt[MAXN + 5], a[MAXN + 50], dp[MAXN + 50][SQ]; vector<int> v; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> q; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] >= MAXN) continue; cnt[a[i]]++; if (cnt[a[i]] == a[i]) v.push_back(a[i]); } for (int i = 0; i < v.size(); i++) { int u = v[i], tmp = 0; for (int j = 1; j <= n; j++) tmp += (a[j - 1] == u), dp[j][i] = tmp; } for (int i = 0; i < q; i++) { cin >> l >> r; ans = 0; for (int j = 0; j < v.size(); j++) ans += (v[j] == dp[r][j] - dp[l - 1][j]); cout << ans << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { long long x1, y1, x2, y2; scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2); printf("%lld\n", (x2 - x1) * (y2 - y1) + 1); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, k, l, c, d, p, nl, np; cin >> n >> k >> l >> c >> d >> p >> nl >> np; int a = (k * l) / nl; int aa = c * d; int aaa = p / np; std::cout << min({a, aa, aaa}) / n << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct point { int x, y; point() {} point(int a, int b) { x = a, y = b; } }; template <class T> T sqr(T a) { return a * a; } template <class T> T power(T n, T p) { T res = 1; for (int i = 0; i < p; i++) res *= n; return res; } template <class T> double getdist(T a, T b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } template <class T> T extract(string s, T ret) { stringstream ss(s); ss >> ret; return ret; } template <class T> string tostring(T n) { stringstream ss; ss << n; return ss.str(); } template <class T> inline T Mod(T n, T m) { return (n % m + m) % m; } template <class T> void show(T a) { cout << a << endl; } template <class T> void show(T a, T b) { cout << a << ' ' << b << endl; } string arr[1010]; string last[1010]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, i, res = 0, j; cin >> n >> m; for (i = 0; i < n; i++) cin >> arr[i]; for (j = 0; j < m; j++) { int diff = 0, mxpos = 0; vector<string> cur, sorted; set<string> ss; for (i = 0; i < n; i++) { cur.push_back(last[i] + arr[i][j]); sorted.push_back(last[i] + arr[i][j]); ss.insert(last[i] + arr[i][j]); } sort(sorted.begin(), sorted.end()); if (cur != sorted) { res++; continue; } if (cur == sorted && (int)(ss.size()) == n) break; for (i = 0; i < n; i++) last[i] += arr[i][j]; } cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long z[300001]; long long pot[300001]; void po(int n) { int sum = 1; pot[0] = 1; while (n > 0) { pot[sum] = (pot[sum - 1] * 2) % 1000000007; sum++; n--; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); po(300000); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> z[i]; } sort(z, z + n); long long sumamn = 0; long long sumamx = 0; for (int i = 0; i < n; i++) { sumamn = (sumamn + ((pot[n - i - 1] * z[i]) % 1000000007)) % 1000000007; sumamx = (sumamx + (((pot[i]) * z[i]) % 1000000007)) % 1000000007; } cout << ((sumamx - sumamn) + 1000000007) % 1000000007; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; int n, m, w[22][22], dp[1 << 22], same[22][22], cost[22][22]; char g[22][22]; int main() { scanf("%d%d", &n, &m); for (register int i = 0; i <= (1 << n); i++) dp[i] = inf; dp[0] = 0; for (register int i = 0; i < n; ++i) scanf("%s", &g[i]); for (register int i = 0; i < n; ++i) for (register int j = 0; j < m; ++j) scanf("%d", &w[i][j]); for (register int i = 0; i < n; ++i) for (register int j = 0; j < m; ++j) { int sumc = 0, maxc = 0; for (register int k = 0; k < n; ++k) if (g[i][j] == g[k][j]) { sumc += w[k][j]; maxc = max(maxc, w[k][j]); same[i][j] |= 1 << k; } cost[i][j] = sumc - maxc; } for (register int s = 0; s < (1 << n); ++s) for (register int i = 0; i < n; ++i) if (!(s >> i & 1)) for (register int j = 0; j < m; ++j) { dp[s | (1 << i)] = min(dp[s | (1 << i)], dp[s] + w[i][j]); dp[s | same[i][j]] = min(dp[s | same[i][j]], dp[s] + cost[i][j]); } printf("%d\n", dp[(1 << n) - 1]); return 0; }
8
#include <bits/stdc++.h> using namespace std; long long a[100000], b; int n; long long maxSubArraySum() { long long max_so_far = 0, max_ending_here = 0, max_so_far_2 = 0, max_ending_here_2 = 0; for (int i = 0; i < n; i++) { if (i > 0) { max_ending_here += a[i]; if (max_ending_here < 0) max_ending_here = 0; else if (max_so_far < max_ending_here) max_so_far = max_ending_here; } if (i < n - 1) { max_ending_here_2 += a[i]; if (max_ending_here_2 < 0) max_ending_here_2 = 0; else if (max_so_far_2 < max_ending_here_2) max_so_far_2 = max_ending_here_2; } } return max(max_so_far, max_so_far_2); } int main() { int t; scanf("%d", &t); for (; t--;) { scanf("%d", &n); b = 0; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); b += a[i]; } if (maxSubArraySum() < b) puts("YES"); else puts("NO"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; typedef struct node { struct node* nexNode[2]; int cnt; } Node; Node* root; void trie_insert(int num) { int i = 30; string s = ""; while (i > 0) { char digit = '0' + num % 2; s = digit + s; num /= 2; i--; } Node* current = root; while (i < s.size()) { int digit = s[i] - '0'; if (current->nexNode[digit] == NULL) { Node* n_node = new Node; n_node->cnt = 0; n_node->nexNode[0] = n_node->nexNode[1] = NULL; current->nexNode[digit] = n_node; current = n_node; } else current = current->nexNode[digit]; i++; current->cnt++; } } void trie_delete(int num) { int i = 30; string s = ""; while (i > 0) { char digit = '0' + num % 2; s = digit + s; num /= 2; i--; } Node* current = root; while (i < s.size()) { int digit = s[i] - '0'; if (current->nexNode[digit] == NULL) return; else current = current->nexNode[digit]; i++; current->cnt--; } } int find_respectors(int num, int l) { int i = 30, ans = 0; string s = ""; string sl = ""; while (i > 0) { char digit = '0' + num % 2; char digit_l = '0' + l % 2; s = digit + s; sl = digit_l + sl; num /= 2; l /= 2; i--; } Node* current = root; while (i < s.size()) { if (sl[i] == '1') { int digit = s[i] - '0'; if (current->nexNode[digit] != NULL) ans += current->nexNode[digit]->cnt; if (current->nexNode[1 - digit] != NULL && current->nexNode[1 - digit]->cnt > 0) current = current->nexNode[1 - digit]; else return ans; } else { int digit = s[i] - '0'; if (current->nexNode[digit] != NULL && current->nexNode[digit]->cnt > 0) current = current->nexNode[digit]; else return ans; } i++; } return ans; } int main() { ios::sync_with_stdio(false); root = new Node; root->cnt = 0; root->nexNode[0] = root->nexNode[1] = NULL; int q; cin >> q; for (int i = 0; i < q; i++) { int type; cin >> type; int p; cin >> p; if (type == 1) { trie_insert(p); continue; } if (type == 2) { trie_delete(p); continue; } int l; cin >> l; cout << find_respectors(p, l) << "\n"; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = int(3e5) + 9; int n; long long a[N]; int b[N]; int cnt[2]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; b[i] = __builtin_popcountll(a[i]); } long long res = 0; int sufSum = 0; cnt[0] = 1; for (int i = n - 1; i >= 0; --i) { int sum = 0, mx = 0; int add = 0; for (int j = i; j < n && j - i < 65; ++j) { sum += b[j]; mx = max(mx, b[j]); if (mx > sum - mx && sum % 2 == 0) --add; } sufSum += b[i]; add += cnt[sufSum & 1]++; res += add; } cout << res << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int len = s.length(); int n; cin >> n; int a[1][len]; int count = 0; for (int i = 0; i < len - 1; i++) { if (s[i] == s[i + 1]) count++; a[0][i] = count; } while (n--) { int l, r; cin >> l >> r; l--; r--; if (l - 1 >= 0) cout << a[0][r - 1] - a[0][l - 1] << endl; else cout << a[0][r - 1] << endl; } }
1
#include <bits/stdc++.h> using namespace std; int vis[10006]; int colour[10006]; vector<int> adj[10006]; int ans = 0; void dfs(int N, int clr) { stack<int> S; S.push(N); if (colour[N] != clr) { ans++; } while (!S.empty()) { int u = S.top(); S.pop(); int l = adj[u].size(); for (int i = 0; i < l; i++) { if (vis[adj[u][i]] == 0) { vis[adj[u][i]] = 1; colour[adj[u][i]] = clr; S.push(adj[u][i]); } } } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n - 1; i++) { int pi; scanf("%d", &pi); adj[pi].push_back(i + 1); } for (int i = 1; i <= n; i++) { int ci; scanf("%d", &ci); memset(vis, 0, sizeof vis); dfs(i, ci); } cout << ans << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int build[55]; bool cmp(int x, int y) { return x > y; } int main() { ios::sync_with_stdio(0); int n, h, m; int l, r, x, ans = 0; cin >> n >> h >> m; for (int i = 1; i <= n; i++) build[i] = h; for (int i = 1; i <= m; i++) { cin >> l >> r >> x; for (int i = l; i <= r; i++) build[i] = min(build[i], x); } for (int i = 1; i <= n; i++) ans += build[i] * build[i]; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int to_num['z' + 1]; char to_char(int n); void fill_to_num() { for (char c = 'a'; c <= 'z'; c++) { to_num[c] = c - 'a'; } for (char c = '0'; c <= '9'; c++) { to_num[c] = c - '0' + 26; } to_num['\\'] = 36; to_num['.'] = 37; } const int SLASH = 36; const int MAXC = 38; struct Node { Node* paths[MAXC]; bool isLeaf; bool inRoot; Node() { isLeaf = false; inRoot = false; for (int i = 0; i < MAXC; i++) { paths[i] = nullptr; } } }; vector<Node*> folders; struct Trie { Node root; void insert(string key) { Node* node = &root; bool foundSlash = false; for (int i = 3; i < key.size(); i++) { char c = key[i]; if (node->paths[to_num[c]] == nullptr) { node->paths[to_num[c]] = new Node; } node = node->paths[to_num[c]]; if (c == '\\' && !foundSlash) { foundSlash = true; node->inRoot = true; folders.push_back(node); } } node->isLeaf = true; } }; Trie disks[5]; struct Result { int folders; int files; }; int maxfolders, maxfiles; Result dfs(Node* node) { int folders = 0, files = 0; if (node->isLeaf) { files++; } for (int i = 0; i < MAXC; i++) { if (node->paths[i] != nullptr) { Result r = dfs(node->paths[i]); if (!node->paths[i]->inRoot) { if (i == SLASH) { folders++; } folders += r.folders; files += r.files; } } } maxfolders = max(maxfolders, folders); maxfiles = max(maxfiles, files); return {folders, files}; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); fill_to_num(); string path; while (cin >> path) { disks[path[0] - 'C'].insert(path); } for (int disk = 0; disk < 5; disk++) { dfs(&disks[disk].root); } cout << maxfolders << ' ' << maxfiles << '\n'; } char to_char(int n) { if (0 <= n && n <= 'z' - 'a') { return 'a' + (char)n; } else if (26 <= n && n <= '9' - '0' + 26) { return '0' + (char)n - 26; } else if (n == 36) { return '\\'; } else if (n == 37) { return '.'; } }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; pair<int, int> ar[n]; int tot = 0; for (int i = 0; i < n; i++) { cin >> ar[i].first; tot += ar[i].first; ar[i].second = i + 1; } sort(ar, ar + n); int dif = ar[n - 1].first; for (int i = 1; i < n; i++) { ar[i].first += ar[i - 1].first; } int x = ceil(n / 2); for (int i = 0; i + x <= n; i++) { int sum = ar[i + x - 1].first; if (i) sum -= ar[i - 1].first; int left = tot - sum; if (abs(left - sum) <= dif) { cout << x << "\n"; int use[n + 1]; memset(use, 0, sizeof(use)); for (int j = i; j < i + x; j++) { cout << ar[j].second << " "; use[ar[j].second] = 1; } cout << "\n" << n - x << "\n"; for (int i = 1; i <= n; i++) { if (!use[i]) cout << i << " "; } return 0; } } }
3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, n; scanf("%d %d %d", &a, &b, &n); if (b == 0) { printf("0\n"); } else if (a == 0) { printf("No solution\n"); } else { int x = round(pow(1.0 * abs(b) / abs(a), 1.0 / n)); double t = a, u = a; for (int i = 0; i < n; i++) { t *= x; u *= -x; } if (t == b) { printf("%d\n", x); } else if (u == b) { printf("%d\n", -x); } else { printf("No solution\n"); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[100010], num[100010]; int main() { int n, maxx = 0, minn = 1000000000, flag = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); minn = min(a[i], minn); } for (int i = 1; i <= n; i++) { a[i] = a[i] - minn; if (a[i] > n) { flag = 1; break; } num[a[i]]++; maxx = max(a[i], maxx); } if (flag == 1) { printf("NO\n"); return 0; } int l = num[0], x; for (int i = 1; i <= maxx; i++) { if (num[i] < l) { flag = 1; break; } if (l == 0) { flag = 1; break; } l = num[i] - l; } if (l != 0) flag = 1; if (flag == 1) printf("NO\n"); else printf("YES\n"); return 0; }
6
#include <bits/stdc++.h> using namespace std; double p[22], eps = 1e-4; double ans[(1 << 21)], x, a[22]; int main() { int n, k, i, j, y = 0; scanf("%d%d", &n, &k); for (i = 0; i < n; i++) { scanf("%lf", &p[i]); if (p[i] < eps) y++; } ans[0] = 1; k = min(k, n - y); for (i = 0; i < (1 << n); i++) { x = 0; y = 0; for (j = 0; j < n; j++) if ((i & (1 << j)) == 0) { y++; x += p[j]; } if (n - y == k) for (j = 0; j < n; j++) if (i & (1 << j)) a[j] += ans[i]; for (j = 0; j < n; j++) if ((i & (1 << j)) == 0) ans[i | (1 << j)] += ans[i] * p[j] / x; } for (i = 0; i < n; i++) printf("%0.20lf ", a[i]); printf("\n"); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MaxN = 150005; const int MaxS = MaxN * 4; struct Seg { int l, r; long long maxl, maxr, maxs, s; Seg operator+(const Seg& other) const { Seg ret; ret.l = l; ret.r = other.r; ret.maxl = max(maxl, s + other.maxl); ret.maxr = max(other.maxr, other.s + maxr); ret.maxs = max(max(maxs, other.maxs), maxr + other.maxl); ret.s = s + other.s; return ret; } } seg[MaxS]; int x[MaxN], N, M, C; long long value[MaxN]; void init(int k, int l, int r) { if (l == r) { seg[k].l = seg[k].r = l; seg[k].maxl = seg[k].maxr = seg[k].maxs = seg[k].s = value[l]; return; } int mid = (l + r) >> 1; init(k << 1, l, mid); init(k << 1 | 1, mid + 1, r); seg[k] = seg[k << 1] + seg[k << 1 | 1]; } Seg read(int k, int l, int r) { if (seg[k].l == l && seg[k].r == r) return seg[k]; int mid = (seg[k].l + seg[k].r) >> 1; if (r <= mid) return read(k << 1, l, r); else if (l > mid) return read(k << 1 | 1, l, r); else return read(k << 1, l, mid) + read(k << 1 | 1, mid + 1, r); } int main() { int p, l, r; while (scanf("%d%d%d", &N, &M, &C) == 3) { long long ret = 0; for (int i = 1; i <= N; i++) { scanf("%d", &x[i]); } for (int i = 1; i < N; i++) { scanf("%d", &p); value[i] = (x[i + 1] - x[i]) * 50LL - C * p; } init(1, 1, N - 1); for (int i = 0; i < M; i++) { scanf("%d%d", &l, &r); ret += max(0LL, read(1, l, r - 1).maxs); } printf("%.10f\n", ret / 100.0); } return 0; }
7
#include <bits/stdc++.h> int main() { unsigned short length; char line[101]; unsigned short count[101]; unsigned short i, j; unsigned short blacks = 0; char current; scanf("%hu", &length); scanf("%c", &current); for (i = 0; i < length; i++) { scanf("%c", &current); if (current == 'B') { count[++blacks] = 0; while ((current == 'B') && (i < length)) { count[blacks]++; scanf("%c", &current); i++; } } } if (blacks) { printf( "%hu" "\n", blacks); for (i = 1; i <= blacks; i++) { printf( "%hu" " ", count[i]); } printf("\n"); } else { printf("0\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long l, r, f[30]; long long cal(long long r) { int l, s[30]; if (r == 0) return 0; for (l = 0; r != 0; r /= 10) s[++l] = r % 10; if (l == 1) return s[1]; long long ans = 9, p = 0; for (int i = 2; i < l; i++) ans += 9 * f[i - 2]; if (l == 2) { if (s[1] == s[l]) ans += s[1] * f[l - 2]; if (s[1] < s[l]) ans += (s[l] - 1) * f[l - 2]; if (s[1] > s[l]) ans += s[l] * f[l - 2]; return ans; } if (s[1] == s[l]) ans += (s[1] - 1) * f[l - 2]; if (s[1] < s[l]) ans += (s[l] - 1) * f[l - 2]; if (s[1] > s[l]) ans += (s[l] - 1) * f[l - 2]; for (int i = l - 1; i >= 2; i--) p = p * 10 + s[i]; ans += p; if (s[1] >= s[l]) ans++; return ans; } void init() { f[0] = 1; for (int i = 1; i <= 18; i++) f[i] = f[i - 1] * 10; } int main() { cin >> l >> r; init(); cout << cal(r) - cal(l - 1) << endl; }
3
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; const int N=2e5+500; int to[N<<1],fst[N],nxt[N<<1],n,mm; void ade(int u,int v){to[++mm]=v,nxt[mm]=fst[u],fst[u]=mm;} struct HLD { int dep[N],fa[N],tp[N],dfn[N],nid[N],dfs_cnt,son[N],size[N]; void dfs1(int u,int Fa) { fa[u]=Fa;size[u]=1;dep[u]=dep[Fa]+1;int mx=0; for(int i=fst[u];i;i=nxt[i]) { int v=to[i];if(v==Fa)continue; dfs1(v,u);size[u]+=size[v]; if(size[v]>mx)mx=size[v],son[u]=v; } } void dfs2(int u,int top) { tp[u]=top;dfn[u]=++dfs_cnt,nid[dfs_cnt]=u; if(son[u])dfs2(son[u],top); for(int i=fst[u];i;i=nxt[i]) { int v=to[i];if(v==fa[u]||v==son[u])continue; dfs2(v,v); } } int lca(int u,int v) { for(;tp[u]!=tp[v];dep[tp[u]]<dep[tp[v]]?v=fa[tp[v]]:u=fa[tp[u]]); return dep[u]<dep[v]?u:v; } int kthfa(int u,int K) { while(K>dep[u]-dep[tp[u]])K-=dep[u]-dep[tp[u]]+1,u=fa[tp[u]]; return nid[dfn[u]-K]; } int go(int u,int v,int K) { int l=lca(u,v);int d=dep[u]+dep[v]-2*dep[l]; if(K<=dep[u]-dep[l])return kthfa(u,K); else return kthfa(v,d-K); } int dis(int u,int v){return dep[u]+dep[v]-2*dep[lca(u,v)];} void build(){dfs1(1,0),dfs2(1,1);} }ds1; struct DFS { vector<pair<int,int> >f[N],g[N]; int cntf[N],cntg[N],size[N],nn,mxsize,root,vis[N]; long long sf[N],sg[N]; void findroot(int u,int fa) { size[u]=1;int mx=0; for(int i=fst[u];i;i=nxt[i]) { int v=to[i];if(v==fa||vis[v])continue; findroot(v,u),size[u]+=size[v]; mx=max(mx,size[v]); } mx=max(mx,nn-size[u]); if(mx<mxsize)root=u,mxsize=mx; } void dfs1(int u,int anc,int fa,int dep) { f[u].push_back(make_pair(anc,dep)); for(int i=fst[u];i;i=nxt[i]) { int v=to[i];if(v==fa||vis[v])continue; dfs1(v,anc,u,dep+1); } } void dfs2(int u,int anc,int fa,int dep) { g[u].push_back(make_pair(anc,dep)); for(int i=fst[u];i;i=nxt[i]) { int v=to[i];if(v==fa||vis[v])continue; dfs2(v,anc,u,dep+1); } } void solve(int u) { // cout<<u<<endl; vis[u]=1;dfs1(u,u,0,0); for(int i=fst[u];i;i=nxt[i]) { int v=to[i];if(vis[v])continue; nn=size[v],mxsize=1e9,findroot(v,u),dfs2(v,root,u,1),solve(root); } } void build() { nn=n*2-1,mxsize=1e9;findroot(1,0),solve(root); } void upd(int u,int w) { #define IT vector<pair<int,int> >::iterator for(IT it=f[u].begin();it!=f[u].end();++it) cntf[it->first]+=w,sf[it->first]+=w*it->second; for(IT it=g[u].begin();it!=g[u].end();++it) cntg[it->first]+=w,sg[it->first]+=w*it->second; } long long query(int u) { #define IT vector<pair<int,int> >::iterator long long ans=0; for(IT it=f[u].begin();it!=f[u].end();++it) ans+=1ll*cntf[it->first]*it->second+sf[it->first]; for(IT it=g[u].begin();it!=g[u].end();++it) ans-=1ll*cntg[it->first]*it->second+sg[it->first]; return ans; } void print() { #define IT vector<pair<int,int> >::iterator for(int i=1;i<=n;i++) { printf("%d:\n",i); for(IT it=f[i].begin();it!=f[i].end();++it)printf("(%d %d) ",it->first,it->second);puts(""); for(IT it=g[i].begin();it!=g[i].end();++it)printf("(%d %d) ",it->first,it->second);puts(""); puts(""); } } }ds2; struct CIRC { int u,r; CIRC(){u=r=0;} CIRC(const int &_u,const int &_r){u=_u,r=_r;} }c[N]; CIRC operator +(const CIRC &a,const CIRC &b) { int d=ds1.dis(a.u,b.u); if(a.r+d<=b.r)return b; if(b.r+d<=a.r)return a; int r=(a.r+b.r+d)>>1; return CIRC(ds1.go(a.u,b.u,r-a.r),r); } bool operator ==(const CIRC &a,const CIRC &b){return a.u==b.u&&a.r==b.r;} bool operator !=(const CIRC &a,const CIRC &b){return !(a==b);} void print(const CIRC &a){printf("(%d %d)\n",a.u,a.r);} long long ans=0; void solve(int l,int r) { if(l==r)return; int mid=(l+r)>>1; solve(l,mid),solve(mid+1,r); c[mid+1]=CIRC(mid+1,0);for(int i=mid+2;i<=r;i++)c[i]=c[i-1]+CIRC(i,0); c[mid]=CIRC(mid,0);for(int i=mid-1;i>=l;i--)c[i]=c[i+1]+CIRC(i,0); // long long tans=ans; // cout<<l<<" "<<r<<endl; // for(int i=l;i<=mid;i++)print(c[i]);puts(""); // for(int i=mid+1;i<=r;i++)print(c[i]);puts(""); long long s1=0,s2=0; int cnt1=0,cnt2=0,p=mid,q=l-1; while(q<mid&&c[q+1]+c[mid+1]==c[q+1])++q; for(int i=l;i<=q;i++)s2+=c[i].r; for(int i=q+1;i<=p;i++)ds2.upd(c[i].u,1),++cnt1,s1+=c[i].r; for(int i=mid+1;i<=r;i++) { while(q>=l&&c[q]+c[i]!=c[q])s2-=c[q].r,++cnt1,s1+=c[q].r,ds2.upd(c[q--].u,1); while(p>q&&c[p]+c[i]==c[i])++cnt2,--cnt1,s1-=c[p].r,ds2.upd(c[p--].u,-1); ans+=1ll*cnt2*c[i].r+s2+(1ll*cnt1*c[i].r+s1+ds2.query(c[i].u))/2; } while(p>q)ds2.upd(c[p--].u,-1); // cout<<ans-tans<<endl; } int main() { // freopen("data.txt","r",stdin); scanf("%d",&n);int node_cnt=n; for(int i=1,u,v;i<n;i++) { scanf("%d%d",&u,&v);++node_cnt; ade(u,node_cnt),ade(node_cnt,u); ade(v,node_cnt),ade(node_cnt,v); } ds1.build();ds2.build(); // ds2.print(); solve(1,n); cout<<ans<<endl; }
13
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; pair<double, int> c[10010]; int n; struct point { double x, y; } p[600][5]; int dblcmp(double x) { if (fabs(x) < eps) return 0; return x > 0 ? 1 : -1; } double cross(point p1, point p2, point p3) { return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); } double dot(point aa, point bb) { return aa.x * bb.x + aa.y * bb.y; } double segP(point p1, point p2, point p3) { if (dblcmp(p2.x - p3.x)) return (p1.x - p2.x) / (p3.x - p2.x); else return (p1.y - p2.y) / (p3.y - p2.y); } double polyUnion() { int i, j, ii, jj, ta, tb, r, d; double z, w, s, sum = 0, tc, td; point tmp1, tmp2; for (i = 0; i < n; i++) for (ii = 0; ii < 4; ii++) { r = 0; c[r++] = make_pair(0., 0); c[r++] = make_pair(1., 0); for (j = 0; j < n; j++) if (i - j) for (jj = 0; jj < 4; jj++) { ta = dblcmp(cross(p[i][ii], p[i][ii + 1], p[j][jj])); tb = dblcmp(cross(p[i][ii], p[i][ii + 1], p[j][jj + 1])); if (!ta && !tb) { tmp1.x = p[j][jj + 1].x - p[j][jj].x; tmp1.y = p[j][jj + 1].y - p[j][jj].y; tmp2.x = p[i][ii + 1].x - p[i][ii].x; tmp2.y = p[i][ii + 1].y - p[i][ii].y; if (dblcmp(dot(tmp1, tmp2)) > 0 && j < i) { c[r++] = make_pair(segP(p[j][jj], p[i][ii], p[i][ii + 1]), 1); c[r++] = make_pair(segP(p[j][jj + 1], p[i][ii], p[i][ii + 1]), -1); } } else if (ta >= 0 && tb < 0) { tc = cross(p[j][jj], p[j][jj + 1], p[i][ii]); td = cross(p[j][jj], p[j][jj + 1], p[i][ii + 1]); c[r++] = make_pair(tc / (tc - td), 1); } else if (ta < 0 && tb >= 0) { tc = cross(p[j][jj], p[j][jj + 1], p[i][ii]); td = cross(p[j][jj], p[j][jj + 1], p[i][ii + 1]); c[r++] = make_pair(tc / (tc - td), -1); } } sort(c, c + r); z = min(max(c[0].first, 0.), 1.); d = c[0].second; s = 0; for (j = 1; j < r; j++) { w = min(max(c[j].first, 0.), 1.); if (!d) s += w - z; d += c[j].second; z = w; } tmp1.x = tmp1.y = 0; sum += cross(tmp1, p[i][ii], p[i][ii + 1]) * s; } return sum; } int main() { int i, j; double area, tmp; while (~scanf("%d", &n)) { area = 0; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) scanf("%lf%lf", &p[i][j].x, &p[i][j].y); p[i][4] = p[i][0]; tmp = 0; for (j = 1; j <= 4; j++) tmp += p[i][j - 1].x * p[i][j].y - p[i][j - 1].y * p[i][j].x; area += fabs(tmp); if (dblcmp(tmp) < 0) swap(p[i][1], p[i][3]); } printf("%.10lf\n", area / polyUnion()); } return 0; }
9
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; map<int,int> mp; int a[N]; int main(){ int T; scanf("%d",&T); while(T--){ int n,tt=-1,t; scanf("%d",&n); mp.clear(); for(int i=1;i<=n;++i){ scanf("%d",&a[i]); } int l=1,r=n; while(a[l]==a[l+1]&&l<r) ++l; while(a[r]==a[r-1]&&r>l) --r; for(int i=l+1;i<r;++i){ if(i!=l+1&&a[i]==a[i-1]){ continue; } ++mp[a[i]]; } int ans; if(mp.size()){ int t=n; for(int i=l+1;i<r;++i){ t=min(mp[a[i]],t); } ans=t+1; }else{ if(a[1]==a[n]){ ans=0; } else{ ans=1; } } ans=min(ans,mp[a[1]]+1); ans=min(ans,mp[a[n]]+1); printf("%d\n",ans); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, a[1010], ans, x[300010]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) x[a[i] + a[j]]++; for (int i = 0; i < 300000; i++) ans = max(ans, x[i]); cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, q; string s[555]; int U[555][555], D[555][555], L[555][555], R[555][555]; int a[555][555]; int dp1[555][555]; int dp2[555][555]; vector<int> rr[555]; vector<int> cc[555]; int cut(int len) { if (len < 2) return 0; else return (len - 1); } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i <= n + 1; i++) for (int j = 0; j <= m + 1; j++) a[i][j] = 1; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (s[i][j] == '.') a[i + 1][j + 1] = 0; else a[i + 1][j + 1] = 1; } for (int i = 0; i <= n + 1; i++) { for (int j = 0; j <= m + 1; j++) if (a[i][j] == 1) { rr[i].push_back(j); cc[j].push_back(i); } } for (int i = 1; i <= n; i++) { for (int j = 1; j < rr[i].size(); j++) { int u = rr[i][j - 1]; int v = rr[i][j]; dp1[i][v] = dp1[i][u] + cut(v - u - 1); } } for (int i = 1; i <= m; i++) { for (int j = 1; j < cc[i].size(); j++) { int u = cc[i][j - 1]; int v = cc[i][j]; dp2[v][i] = dp2[u][i] + cut(v - u - 1); } } for (int i = 0; i <= n + 1; i++) for (int j = 0; j <= m + 1; j++) if (a[i][j] == 1) { L[i][j] = j; R[i][j] = j; U[i][j] = i; D[i][j] = i; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == 1) L[i][j] = j; else L[i][j] = L[i][j - 1]; if (a[i][j] == 1) U[i][j] = i; else U[i][j] = U[i - 1][j]; } } for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) if (a[i][j] == 1) { R[i][j] = j; D[i][j] = i; } else { R[i][j] = R[i][j + 1]; D[i][j] = D[i + 1][j]; } } cin >> q; int x, y, u, v; for (int i = 1; i <= q; i++) { cin >> x >> y >> u >> v; int ret = 0; for (int j = x; j <= u; j++) { int l = R[j][y]; int r = L[j][v]; if (l > v) ret += cut(v - y + 1); else { ret += (dp1[j][r] - dp1[j][l] + cut(v - r) + cut(l - y)); } } for (int j = y; j <= v; j++) { int l = D[x][j]; int r = U[u][j]; if (l > u) ret += cut(u - x + 1); else { ret += (dp2[r][j] - dp2[l][j] + cut(l - x) + cut(u - r)); } } printf("%d\n", ret); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int a[n], b, rez[n]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) { scanf("%d", &b); rez[(a[i] - 1)] = b; } for (int i = 0; i < n - 1; i++) { printf("%d ", rez[i]); } printf("%d\n", rez[n - 1]); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int N = 2e5 + 1; const long long mod = 1e9 + 7; int arr[32][N], tree[32][4 * N]; void build(int ss, int se, int si, int k) { if (ss == se) { for (int mask = 0; mask < (1 << k); mask++) { tree[mask][si] = arr[mask][ss]; } } else { int mid = (ss + se) / 2; build(ss, mid, 2 * si + 1, k); build(mid + 1, se, 2 * si + 2, k); for (int mask = 0; mask < (1 << k); mask++) { tree[mask][si] = max(tree[mask][2 * si + 1], tree[mask][2 * si + 2]); } } } void update(int qi, int ss, int se, int si, int k) { if (qi < ss || qi > se) return; if (ss == se) { for (int mask = 0; mask < (1 << k); mask++) { tree[mask][si] = arr[mask][ss]; } } else { int mid = (ss + se) / 2; update(qi, ss, mid, 2 * si + 1, k); update(qi, mid + 1, se, 2 * si + 2, k); for (int mask = 0; mask < (1 << k); mask++) { tree[mask][si] = max(tree[mask][2 * si + 1], tree[mask][2 * si + 2]); } } } int query(int qs, int qe, int ss, int se, int si, int mask) { if (qs > se || qe < ss) { return -inf; } if (qs <= ss && se <= qe) { return tree[mask][si]; } int mid = (ss + se) / 2; return max(query(qs, qe, ss, mid, 2 * si + 1, mask), query(qs, qe, mid + 1, se, 2 * si + 2, mask)); } void solve() { int n, k; cin >> n >> k; auto setval = [&](int i) { vector<int> temp(k); for (int j = 0; j < k; j++) { cin >> temp[j]; } for (int mask = 0; mask < (1 << k); mask++) { int val = 0; for (int j = 0; j < k; j++) { if ((mask & (1 << j)) == (1 << j)) { val += temp[j]; } else { val -= temp[j]; } } arr[mask][i] = val; } }; for (int i = 0; i < n; i++) { setval(i); } build(0, n - 1, 0, k); int q, t, pos, l, r; cin >> q; while (q--) { cin >> t; if (t == 1) { cin >> pos; pos--; setval(pos); update(pos, 0, n - 1, 0, k); } else { cin >> l >> r; l--; r--; int ans = 0; vector<int> asked(1 << k); for (int mask = 0; mask < (1 << k); mask++) { if (asked[mask]) continue; int nmask = ((1 << k) - 1) ^ mask; ans = max(ans, query(l, r, 0, n - 1, 0, mask) + query(l, r, 0, n - 1, 0, nmask)); asked[mask] = asked[nmask] = 1; } cout << ans << "\n"; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; std::cout << std::fixed << std::setprecision(20); ; int t = 1; while (t--) { solve(); } }
7
#include <bits/stdc++.h> inline long long read() { char c; long long x; for (c = getchar(); !isdigit(c); c = getchar()) ; for (x = 0; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } return x; } int T, n; long long k, sum; std::vector<std::pair<int, int>> e; std::vector<int> ans; bool check() { for (int i = 0; i < e.size(); i++) { if ((k + 1) / 2 <= sum && sum <= k) { printf("%d\n", (int)e.size() - i); ans.clear(); for (int j = i; j < e.size(); j++) { ans.push_back(e[j].second); } std::sort(ans.begin(), ans.end()); for (auto x : ans) { printf("%d ", x); } printf("\n"); return false; } sum -= e[i].first; } return true; } int main() { for (int T = read(); T; T--) { n = read(); k = read(); e.clear(); int res = -1; sum = 0; for (int i = 0; i < n; i++) { int x = read(); if (x > k) { continue; } e.push_back({x, i + 1}); if ((k + 1) / 2 <= x && x <= k) { res = i + 1; } sum += x; } if (res != -1) { printf("1\n%d\n", res); continue; } std::sort(e.begin(), e.end()); if (check()) { printf("-1\n"); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m; while (cin >> n >> m) { bool flag = 0; int sum = 0; int min = 0; int max = 0; int temp = 0; int arr[1000]; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] > m || arr[i] < -m) { flag = 1; } temp += arr[i]; if (temp < min) min = temp; else if (temp > max) max = temp; } if (min < -m || max > m || flag) { cout << sum << endl; continue; } else { if (min >= 0) sum = m - max + 1; else if (max <= 0) sum = m + min + 1; else sum = m - max + min + 1; sum = sum > 0 ? sum : 0; cout << sum << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[2 * n + 2]; for (int i = 1; i <= 2 * n + 1; i++) { cin >> a[i]; } for (int i = 1; i <= 2 * n + 1; i++) { if (k && i % 2 == 0) { if (a[i - 1] < a[i] - 1 && a[i] - 1 > a[i + 1]) { a[i]--; k--; } } cout << a[i] << " "; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[8]; int main() { int test, d; cin >> test; while (test--) { cin >> d; int s = 0; for (int i = 1; i <= 7; ++i) { cin >> a[i]; s += a[i]; } int ans = (d - 1) / s * 7, res = 2e9 + 5; d = (d - 1) % s; ++d; for (int i = 1; i <= 7; ++i) { if (!a[i]) continue; int tp = d, s = i - 1, cnt = 0; if (s == 0) s = 7; while (tp) { ++s, ++cnt; if (s > 7) s = 1; tp -= a[s]; } res = min(res, cnt); } cout << res + ans << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[] = {0, 4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165}; int main() { int x; cin >> x; cout << a[x]; }
4
#include <bits/stdc++.h> using namespace std; void DFS(int v, vector<vector<int> >& adj, vector<bool>& used) { used[v] = true; for (int i = 0; i < (int)adj[v].size(); ++i) { int u = adj[v][i]; if (!used[u]) { DFS(u, adj, used); } } } class Segment { public: int x; int y; Segment() {} Segment(int _x, int _y) : x(_x), y(_y) {} bool HasTransition(const Segment& a) { if ((a.x < x && x < a.y) || (a.x < y && y < a.y)) { return true; } else { return false; } } }; int main() { int n; cin >> n; vector<Segment> segments; vector<vector<int> > adj; int size = 0; for (int i = 0; i < n; ++i) { int num; cin >> num; if (num == 1) { ++size; int x; int y; cin >> x >> y; Segment seg(x, y); segments.push_back(seg); adj.push_back(vector<int>()); for (int i = 0; i < size - 1; ++i) { Segment& a = segments[i]; if (a.HasTransition(seg)) { adj[i].push_back(size - 1); } if (seg.HasTransition(a)) { adj[size - 1].push_back(i); } } } else { int a; int b; cin >> a >> b; --a; --b; vector<bool> used(size, false); DFS(a, adj, used); if (used[b]) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const double g = 10.0, eps = 1e-7; const int N = 200000 + 10, maxn = 60000 + 10, inf = 0x3f3f3f; int l[N], r[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v1, v2; for (int i = 1; i <= n; i++) { cin >> l[i] >> r[i]; if (l[i] == 0) v1.push_back(i); if (r[i] == 0) v2.push_back(i); } int cnt = 0; int be = v1[cnt], now = be; while (cnt + 1 < v1.size()) { while (r[now] != 0) now = r[now]; ++cnt; r[now] = v1[cnt]; l[v1[cnt]] = now; now = v1[cnt]; } for (int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); long long n, f = 0; cin >> n; while (n--) { string s; long long a, b; cin >> s >> a >> b; if (a >= 2400 && b > a) f = 1; } if (f == 1) cout << "Yes"; else cout << "No"; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct matching { int n, m; vector<int> pa; vector<int> pb; vector<vector<int>> g; vector<int> q; vector<int> was; matching(int n, int m) : n(n), m(m) { pa.resize(n, -1); pb.resize(m, -1); g.resize(n); q.resize(n + 1); was.resize(n); } void add(int u, int v) { assert(u >= 0 && u < n && v >= 0 && v < m); g[u].push_back(v); } bool bfs() { int beg = 0, end = 0; for (int i = 0; i < n; i++) if (pa[i] == -1) { q[end++] = i; was[i] = 1; } else was[i] = 0; bool found = false; while (beg < end) { int v = q[beg++]; for (auto& u : g[v]) { if (pb[u] == -1) { found = true; continue; } if (was[pb[u]]) continue; was[pb[u]] = was[v] + 1; q[end++] = pb[u]; } } return found; } bool dfs(int v, int w = 1) { for (auto& u : g[v]) { if (pb[u] == -1) { pb[u] = v; pa[v] = u; return true; } } for (auto& u : g[v]) { if (was[pb[u]] == w + 1) { was[pb[u]] = 0; if (dfs(pb[u], w + 1)) { pb[u] = v; pa[v] = u; return true; } } } return false; } int solve() { int res = 0; while (bfs()) { int add = 0; for (int i = 0; i < n; i++) if (pa[i] == -1 && dfs(i)) { ++add; } if (!add) break; res += add; } return res; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<tuple<int, int, int>> edges; for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; --u; --v; edges.emplace_back(w, u, v); } sort(edges.begin(), edges.end()); auto test = [&](int val) { matching mat(n, n); for (auto& x : edges) { if (get<0>(x) > val) break; mat.add(get<1>(x), get<2>(x)); } return (mat.solve() == n); }; int l = 0, r = m - 1; while (l <= r) { int mid = l + r >> 1; if (test(get<0>(edges[mid]))) r = mid - 1; else l = mid + 1; } if (l == m) cout << "-1"; else cout << get<0>(edges[l]); }
5
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n, x, y; cin >> n >> x >> y; if (x * 100 >= y * n) { cout << 0; } else { cout << (y * n - x * 100 + 99) / 100; } return 0; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n, x; cin >> n; vector<vector<int>> v(n + 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < 5; ++j) { cin >> x; v[i].push_back(x); } } int cnt = 0, pos = 0, c = 1; for (int i = 1; i < n; ++i) { cnt = 0; for (int j = 0; j < 5; ++j) { if (v[pos][j] < v[i][j]) cnt++; } if (cnt < 3) pos = i; } for (int i = 0; i < n; ++i) { if (i == pos) continue; cnt = 0; for (int j = 0; j < 5; ++j) { if (v[pos][j] < v[i][j]) cnt++; } if (cnt < 3) { pos = -2; break; } } cout << pos + 1 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; cin >> t; while (t--) solve(); }
3
#include <bits/stdc++.h> #ifndef ONLINE_JUDGE #define debug(x) cout << #x << ": " << (x) << endl #else #define debug(x) #endif using namespace std; typedef long long ll; typedef vector<int> vi; const int maxn=1e6+7,inf=0x3f3f3f3f,mod=1e9+7; int main() { ios::sync_with_stdio(false); cin.tie(0); int kase; cin>>kase; while(kase--) { int n; cin>>n; vector<pair<int,int>>ans; if(n==2) { cout<<"1\n"; cout<<"2 1\n"; cout<<"1\n"; cout<<"2 1\n"; } else if(n==3) { cout<<"5\n"; cout<<"2 3 1\n"; cout<<"2\n"; cout<<"1 3\n"; cout<<"3 2\n"; } else { ll sum=0; vi p(n+1); p[1]=n-1; for(int i=2;i<=n/2;++i) p[i]=i+1; p[n/2]=1; p[n/2+1]=n; for(int i=n/2+2;i<=n-1;++i) p[i]=i-1; p[n]=2; vi q=p; for(int i=2;i<=n/2;++i) ans.push_back({i,n}),assert(p[n]==i),swap(p[i],p[n]),sum+=1ll*(n-i)*(n-i); for(int i=n-1;i>=n/2+1;--i) ans.push_back({i,1}),assert(p[1]==i),swap(p[i],p[1]),sum+=1ll*(i-1)*(i-1); ans.push_back({1,n}); sum+=1ll*(n-1)*(n-1); cout<<sum<<'\n'; for(int i=1;i<=n;++i) cout<<q[i]<<' '; cout<<'\n'; cout<<ans.size()<<'\n'; for(auto &v:ans) cout<<v.first<<" "<<v.second<<'\n'; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long N = 200022; int n, ans; string s; int a[10]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { cin >> s; for (int i = 0; i < s.size(); i++) a[i] += s[i] - '0'; } for (int i = 0; i < 10; i++) ans = max(ans, a[i]); cout << ans; }
0
#include <bits/stdc++.h> using namespace std; int n, a, b, arr[2003]; int main(void) { cin >> n >> a >> b; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); cout << arr[b] - arr[b - 1] << endl; }
0
#include <bits/stdc++.h> using namespace std; struct point { long long x; long long y; point(long long _x = 0, long long _y = 0) : x(_x), y(_y) {} point operator+(point rhs) const { return point(x + rhs.x, y + rhs.y); } point operator-(point rhs) const { return point(x - rhs.x, y - rhs.y); } long long operator^(point rhs) const { return x * rhs.y - y * rhs.x; } } p[100100]; long long count(point in) { long long a = ((in.x) < 0 ? -(in.x) : (in.x)), b = ((in.y) < 0 ? -(in.y) : (in.y)), tmp; while (b > 0) { tmp = b; b = a % b; a = tmp; } return a; } int main() { double tta2 = 0, tte = 0, ttc; double na2, ne, nc, num; int n, i, j; scanf("%d", &n); if (n < 60) num = pow(2, n) - 1 - n - 0.5 * n * (n - 1); for (i = 0; i < n; i++) { scanf("%I64d%I64d", &p[i].x, &p[i].y); } for (i = 0; i < n; i++) { tta2 += (double)(p[i] ^ p[(i + 1) % n]); tte += (double)(count(p[(i + 1) % n] - p[i])); } ttc = (tta2 + tte + 2) / 2; for (i = 1; i < min(n, 100); i++) { if (i > 1) { ne = tte * i; na2 = tta2 * i; for (j = 0; j < n; j++) { na2 += (double)(p[(j + i) % n] ^ p[j]); ne += (double)(count(p[(j + i) % n] - p[j])); } nc = (na2 + ne) / 2; } else { nc = tte; } if (n < 60) ttc -= nc * (pow(2, n - i - 1) - 1) / num; else ttc -= nc / pow(2, i + 1); } printf("%.12f\n", ttc); return 0; }
10
#include <bits/stdc++.h> using namespace std; using i64 = long long int; using ii = pair<int, int>; using ii64 = pair<i64, i64>; vector<int> edge[100005]; vector<int> oneWay[100005]; int deg[100005]; int deg2[100005]; bool used[100005]; i64 n, m; bool visit[100005]; void dfs(int root) { visit[root] = true; for (auto& e : edge[root]) { if (visit[e]) continue; oneWay[root].push_back(e); deg[e]++; deg2[e]++; dfs(e); } } void makeGraph() { queue<int> q; for (int i = 1; i <= n; i++) { if (edge[i].size() == n - 1) continue; vector<bool> connect(n + 1); for (auto& e : edge[i]) connect[e] = true; visit[i] = true; q.push(i); for (int j = 1; j <= n; j++) { if (connect[j]) continue; if (i == j) continue; visit[j] = true; q.push(j); break; } break; } while (!q.empty()) { auto now = q.front(); used[now] = true; q.pop(); for (auto& e : edge[now]) { if (!used[e]) { deg[e]++; deg2[e]++; oneWay[now].push_back(e); } if (visit[e]) continue; visit[e] = true; q.push(e); } } for (int i = 1; i <= n; i++) { if (visit[i]) continue; dfs(i); } } void noDup() { queue<int> q; for (int i = 1; i <= n; i++) if (deg[i] == 0) q.push(i); int now = 1; vector<int> ds(n + 1); while (!q.empty()) { int t = q.front(); ds[t] = now; now++; q.pop(); for (auto& e : oneWay[t]) { deg[e]--; if (deg[e] == 0) q.push(e); } } for (int i = 1; i <= n; i++) printf("%d ", ds[i]); printf("\n"); } void dup() { queue<int> q; vector<int> ds(n + 1); for (int i = 1; i <= n; i++) { if (deg2[i] == 0) { q.push(i); ds[i] = 1; } } while (!q.empty()) { int t = q.front(); q.pop(); for (auto& e : oneWay[t]) { deg2[e]--; if (deg2[e] == 0) { q.push(e); ds[e] = ds[t] + 1; } } } for (int i = 1; i <= n; i++) printf("%d ", ds[i]); printf("\n"); } int main() { scanf("%lld %lld", &n, &m); if (n * (n - 1) / 2 == m) { printf("NO\n"); return 0; } for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); edge[a].push_back(b); edge[b].push_back(a); } makeGraph(); printf("YES\n"); noDup(); dup(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long fexp(long long b, long long e, long long m) { long long ans = 1; while (e) { if (e % 2 == 1) ans = (ans * b) % m; b = (b * b) % m; e /= 2; } return ans; } int main() { long long n; cin >> n; if (!n) cout << 1 << endl; else cout << (fexp(2, 2 * n - 1, 1000000007) + fexp(2, n - 1, 1000000007)) % 1000000007 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; struct Dominator { struct min_DSU { int N; vector<int> par, val; vector<int> const &semi; min_DSU(int _N, vector<int> const &semidom) : N(_N), par(N, -1), val(N), semi(semidom) { iota(val.begin(), val.end(), 0); } void comp(int x) { if (par[par[x]] != -1) { comp(par[x]); if (semi[val[par[x]]] < semi[val[x]]) { val[x] = val[par[x]]; } par[x] = par[par[x]]; } } int f(int x) { if (par[x] == -1) { return x; } comp(x); return val[x]; } void link(int x, int p) { par[x] = p; } }; int N; vector<vector<int>> G, rG; vector<int> immediate_dominator, order; vector<vector<int>> dominated; Dominator(int _N) : N(_N), G(N), rG(N) {} void add_edge(int a, int b) { G[a].emplace_back(b); rG[b].emplace_back(a); } vector<int> calc_dominators(int S) { immediate_dominator.assign(N, -1); vector<int> par(N, -1), cur_edge(N, 0), semidom(N, -1); vector<vector<int>> bucket(N); stack<int> s; s.emplace(S); while (!s.empty()) { int a = s.top(); s.pop(); if (semidom[a] == -1) { semidom[a] = order.size(); order.emplace_back(a); } for (int &i = cur_edge[a]; i < (int)G[a].size(); ++i) { if (semidom[G[a][i]] == -1) { s.push(a); par[G[a][i]] = a; s.push(G[a][i]); break; } } } min_DSU uni(N, semidom); for (int i = (int)order.size() - 1; i > 0; --i) { int w = order[i]; for (int f : rG[w]) { int oval = semidom[uni.f(f)]; if (oval >= 0 && semidom[w] > oval) semidom[w] = oval; } bucket[order[semidom[w]]].push_back(w); uni.link(w, par[w]); while (!bucket[par[w]].empty()) { int v = bucket[par[w]].back(); bucket[par[w]].pop_back(); int u = uni.f(v); if (semidom[u] < semidom[v]) immediate_dominator[v] = u; else immediate_dominator[v] = par[w]; } } for (int i = 1; i < (int)order.size(); ++i) { int w = order[i]; if (immediate_dominator[w] != order[semidom[w]]) { immediate_dominator[w] = immediate_dominator[immediate_dominator[w]]; } } immediate_dominator[S] = -1; return immediate_dominator; } }; using ll = long long; vector<ll> dijkstras(vector<vector<pair<int, ll>>> const &g, int s) { vector<ll> ret(g.size(), numeric_limits<ll>::max() / 2); ret[s] = 0; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; pq.emplace(ret[s], s); while (!pq.empty()) { ll curd; int c; tie(curd, c) = pq.top(); pq.pop(); if (curd != ret[c]) continue; for (auto const &e : g[c]) { if (ret[e.first] > ret[c] + e.second) { ret[e.first] = ret[c] + e.second; pq.emplace(ret[e.first], e.first); } } } return ret; } signed CF757F() { int n, m, S; cin >> n >> m >> S; vector<vector<pair<int, ll>>> G(n + 1); while (m--) { int a, b, c; cin >> a >> b >> c; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } vector<ll> dist = dijkstras(G, S); Dominator D(n + 1); for (int i = 0; i <= n; ++i) { for (auto e : G[i]) { if (dist[e.first] == dist[i] + e.second) { D.add_edge(i, e.first); } } } vector<int> id = D.calc_dominators(S); vector<int> hit(n + 1, 0); for (int i = (int)D.order.size() - 1; i > 0; --i) { int e = D.order[i]; hit[id[e]] += ++hit[e]; } hit[S] = 0; int ans = *max_element(hit.begin(), hit.end()); cout << ans << "\n"; return 0; } int main() { return CF757F(); cerr << "go\n"; int N, M, a, b; while (cin >> N >> M && N) { vector<pair<int, int>> ed; Dominator D(N); while (M--) { cin >> a >> b; D.add_edge(a, b); ed.emplace_back(a, b); } vector<int> id = D.calc_dominators(0); copy(id.begin(), id.end(), ostream_iterator<int>(cerr, " ")); cerr << "\n"; } }
10
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int t, n, m, tot, cnt; struct node { int per, to; } e[1000010 << 1]; int head[1000010]; bool tag[1000010], vis[1000010]; int id[1000010], dis[1000010], f[1000010]; void init() { tot = 0, cnt = 0; for (int i = 1; i <= n; ++i) { head[i] = -1; id[i] = dis[i] = tag[i] = f[i] = vis[i] = 0; } } void addedge(int from, int to) { e[++tot].to = to; e[tot].per = head[from]; head[from] = tot; } void dfs(int u, int fa, int len) { vis[u] = 1; f[u] = fa; cnt++; id[cnt] = u; dis[u] = len; for (int i = head[u]; i > 0; i = e[i].per) { int v = e[i].to; if (v == fa || vis[v]) continue; dfs(v, u, len + 1); } } int main() { ios_base::sync_with_stdio(false); cin >> t; while (t--) { cin >> n >> m; init(); for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; addedge(x, y); addedge(y, x); } dfs(1, 0, 1); bool flag = 0; for (int i = 1; i <= n; ++i) { if (dis[i] >= (n + 1) / 2) { cout << "PATH\n"; flag = 1; cout << dis[i] << "\n"; int now = i; while (now) { cout << now << " "; now = f[now]; } cout << "\n"; break; } } if (flag) continue; cout << "PAIRING\n"; int now = id[n - (n + 3) / 4]; while (now) { tag[now] = 1; now = f[now]; } now = 1; cout << (n - (n - (n + 3) / 4 + 1) + 1) << "\n"; for (int i = n - (n + 3) / 4 + 1; i <= n; ++i) { while (tag[id[now]]) now++; cout << id[i] << " " << id[now++] << "\n"; } } return 0; }
9
#include <bits/stdc++.h> using namespace std; int n; int nums[2000000]; int main() { cin >> n; for (int i = 0; i < n; ++i) nums[i] = i + 1; int start = 0; for (int i = 2; i <= n; ++i) { int pos = start; int temp = nums[pos]; ++start; while (true) { pos += i; if (pos - start >= n - 1) { nums[start + n - 1] = temp; break; } int temp2 = nums[pos]; nums[pos] = temp; temp = temp2; } } for (int i = start; i < start + n - 1; ++i) cout << nums[i] << ' '; cout << nums[start + n - 1] << endl; }
7
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; template <typename T> bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; }; template <typename T> bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; }; struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; int main() { int N, Q; cin >> N >> Q; vector<int> ans(N, -1); set<int> s; for (int i = 0; i < N; i++) s.emplace(i); while (Q--) { int L, R; cin >> L >> R; L--; int x; cin >> x; x--; for (auto it = s.lower_bound(L); it != end(s) && *it < R;) { if (*it != x) { ans[*it] = x; it = s.erase(it); } else it++; } } for (int i = 0; i < N; i++) cout << ans[i] + 1 << (i == N - 1 ? '\n' : ' '); }
3
#include <bits/stdc++.h> using namespace std; long long int vis[1000000]; int main() { long long int h, n; cin >> h >> n; long long int rem = pow(2, h); long long int ans = 0; long long int sto = 0; long long int flag = 0; while (rem > 1) { ans++; if (flag == 0) { if (n > rem / 2 + sto) { ans += (rem / 2 + rem / 2 - 1); rem = rem / 2; sto += rem; } else { rem = rem / 2; flag = 1 - flag; } } else { if (n > rem / 2 + sto) { rem = rem / 2; sto += rem; flag = 1 - flag; } else { ans += (rem / 2 + rem / 2 - 1); rem = rem / 2; } } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> int b[110], vis[110]; int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main(void) { int n, i, cur, k, pile; while (scanf("%d%*c", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d", &b[i]); qsort(b, n, 4, cmp); memset(vis, 0, sizeof(vis)); k = 0; pile = 0; while (k < n) { cur = 0; pile++; for (i = 0; i < n; i++) if (!vis[i] && cur <= b[i]) { vis[i] = 1; cur++; k++; } } printf("%d\n", pile); } return 0; }
3
#include <bits/stdc++.h> using namespace std; void solve(); int main() { int t = 1; while (t--) { solve(); } return 0; } namespace input { inline int nextInt() { int res = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar(); return res; } inline long long nextLong() { long long res = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar(); return res; } inline bool isSpace(const char &c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } inline void next(char *str) { char c = getchar(); while (isSpace(c)) c = getchar(); size_t head = 0; while (!isSpace(c)) str[head++] = c, c = getchar(); str[head] = 0; } inline char nextChar() { char c = getchar(); while (isSpace(c)) c = getchar(); return c; } } // namespace input using namespace input; const int N = 1e3 + 5; map<pair<long long, long long>, long long> mp; int higher(long long x, long long y) { for (int i = 62; i >= 0; i--) { if ((x & (1LL << i)) && (y & (1LL << i)) == 0) { return 1; } else if ((x & (1LL << i)) && (y & (1LL << i))) { return 0; } else if ((x & (1LL << i)) == 0 && (y & (1LL << i))) { return -1; } } return 0; } void solve() { int q; scanf("%d", &q); while (q--) { int t; scanf("%d", &t); if (t == 1) { long long v, u, w; scanf( "%lld" "%lld" "%lld", &v, &u, &w); while (v != u) { int h = higher(u, v); if (h == 1) { long long p = u / 2; mp[{p, u}] += w; u /= 2; } else if (h == -1) { long long p = v / 2; mp[{p, v}] += w; v /= 2; } else { long long p1 = v / 2; mp[{p1, v}] += w; v /= 2; long long p2 = u / 2; mp[{p2, u}] += w; u /= 2; } } } else { long long u, v; scanf( "%lld" "%lld", &u, &v); long long ans = 0; while (v != u) { int h = higher(u, v); if (h == 1) { long long p = u / 2; ans += mp[{p, u}]; u /= 2; } else if (h == -1) { long long p = v / 2; ans += mp[{p, v}]; v /= 2; } else { long long p1 = v / 2; ans += mp[{p1, v}]; v /= 2; long long p2 = u / 2; ans += mp[{p2, u}]; u /= 2; } } printf( "%lld" "\n", ans); } } }
3
#include <bits/stdc++.h> using namespace std; inline int read() { int a = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) { a = (a << 3) + (a << 1) + (c ^ '0'); c = getchar(); } return a; } const int MAXN = 300010; struct point { int a, b, ind; } now[MAXN]; int nxt[MAXN], pre[MAXN], S[MAXN], tl; bool can[MAXN]; bool cmp(point a, point b) { if (a.a == b.a) return a.b > b.b; return a.a > b.a; } long double calcK(point a, point b) { return (long double)a.a * b.a * (b.b - a.b) / a.b / b.b / (b.a - a.a); } int main() { int N = read(); for (int i = 1; i <= N; i++) { now[i].a = read(); now[i].b = read(); now[i].ind = i; nxt[i] = i + 1; pre[i] = i - 1; } sort(now + 1, now + N + 1, cmp); int maxB = now[1].b; for (int i = 2; i <= N; i++) if (now[i].b <= maxB) { nxt[pre[i]] = nxt[i]; pre[nxt[i]] = pre[i]; } else maxB = now[i].b; S[0] = 1; tl = 1; for (int i = nxt[1]; i <= N; i = nxt[i]) { while (tl != 1 && calcK(now[i], now[S[tl - 1]]) < calcK(now[S[tl - 2]], now[S[tl - 1]])) tl--; S[tl++] = i; } for (int i = 0; i < tl; i++) { can[now[S[i]].ind] = 1; for (int j = S[i] + 1; j <= N && now[S[i]].a == now[j].a && now[S[i]].b == now[j].b; j++) can[now[j].ind] = 1; } for (int i = 1; i <= N; i++) if (can[i]) printf("%d ", i); return 0; }
9
#include <bits/stdc++.h> template <typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First& first, Rest&... rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } template <typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template <typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } template <class T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } template <class T> using V = std::vector<T>; template <class T> using VV = V<V<T>>; template <typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<ll, ll>; using PAIRLL = std::pair<ll, ll>; constexpr ll INFINT = 1 << 30; constexpr ll INFINT_LIM = (1LL << 31) - 1; constexpr ll INFLL = 1LL << 60; constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); constexpr double EPS = 1e-6; constexpr ll MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template <class T, size_t N> void FILL(T (&a)[N], const T& val) { for (auto& x : a) x = val; } template <class ARY, size_t N, size_t M, class T> void FILL(ARY (&a)[N][M], const T& val) { for (auto& b : a) FILL(b, val); } template <class T> void FILL(std::vector<T>& a, const T& val) { for (auto& x : a) x = val; } template <class ARY, class T> void FILL(std::vector<std::vector<ARY>>& a, const T& val) { for (auto& b : a) FILL(b, val); } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ; ll n, m; MACRO_VAR_Scan(n, m); ; std::vector<ll> a, b; MACRO_VEC_ROW_Init(m, a, b); for (ll w_ = 0; w_ < m; ++w_) { MACRO_VEC_ROW_Scan(w_, a, b); }; std::vector<std::vector<ll>> g(n); for (ll i = 0; i < ll(m); ++i) { --a[i]; --b[i]; g[a[i]].emplace_back(b[i]); } std::vector<ll> min(n, INFLL); for (ll i = 0; i < ll(n); ++i) { for (ll x : g[i]) { ll cost = x - i; if (cost <= 0) cost += n; CHMIN(min[i], cost); } } for (ll i = 0; i < ll(n); ++i) { ll ans = 0; for (ll j = 0; j < ll(n); ++j) { ll J = (i + j) % n; if (g[J].empty()) continue; ll len = g[J].size(); ll sum = (len - 1) * n + j + min[J]; CHMAX(ans, sum); } std::cout << (ans); std::cout << (i + 1 == n ? '\n' : ' '); ; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int T, x, y, k; int cost = 0; int n, a, b; cin >> n >> a >> b; int c[n]; for (int i = 0; i < n; i++) { cin >> c[i]; } int i, j; for (i = 0, j = n - 1; i < j; i++, j--) { if (c[i] == c[j]) { if (c[i] == 2) { cost += (2 * min(a, b)); } } else { if (c[i] == 2 || c[j] == 2) { if (c[i] == 2) { if (c[j] == 1) { cost += b; } else { cost += a; } } else { if (c[i] == 1) { cost += b; } else { cost += a; } } } else { cout << -1; exit(0); } } } if (i == j) { if (c[i] == 2) cost += (min(a, b)); } cout << cost; }
1
#include <bits/stdc++.h> using namespace std; int pp[1000][1000]; int main() { int n, k, i, j; while (cin >> n >> k) { memset(pp, 0, sizeof(pp)); for (i = 1; i <= n; i++) pp[i][i] = 1; if (n * (n - 1) / 2 < k * n) { cout << -1 << endl; continue; } cout << n * k << endl; for (i = 1; i <= n; i++) { int kk = k; for (j = 1; j <= n && kk > 0; j++) if (pp[i][j] == 0) { printf("%d %d\n", i, j); pp[i][j] = pp[j][i] = 1; kk--; } } } }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g = 10.0, eps = 1e-9; const int N = 100000 + 10, maxn = 300, inf = 0x3f3f3f; int a[N]; int d[maxn + 10][N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int k = 1; k <= maxn; k++) { for (int i = n; i >= 1; i--) { if (i + a[i] + k > n) d[k][i] = 1; else d[k][i] = d[k][i + a[i] + k] + 1; } } cin >> m; while (m--) { int p, k, ans = 0; cin >> p >> k; if (k >= maxn) { while (p <= n) { p += (a[p] + k); ans++; } cout << ans << endl; } else cout << d[k][p] << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long int ok = 1000 * 1000 * 1000 + 7; map<long long int, long long int> m; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int n, k; cin >> n >> k; vector<long long int> v; for (long long int i = 0; i < n; i++) { long long int p; cin >> p; if (m[p] == 0) { v.push_back(p); m[p]++; } } sort(v.begin(), v.end()); long long int val = 0; for (long long int i = 0; i < k; i++) { if (i >= v.size()) { cout << 0 << "\n"; } else { if (val > v[i]) cout << 0 << "\n"; else cout << v[i] - val << "\n"; val = v[i]; } } return 0; }
1
#include <bits/stdc++.h> int a[100000]; int b[100000]; int main() { long long m, n; scanf("%lld%lld", &m, &n); for (int i = 0; i < m; i++) scanf("%d", &a[i]); for (int i = 0; i < m; i++) scanf("%d", &b[i]); long long left = 0, right = 2000000000, ans = 0; while (left <= right) { long long t = n; long long mid = (left + right) / 2, i; for (i = 0; i < m; i++) { if (a[i] * mid > b[i]) { if (b[i] + t >= a[i] * mid) { t -= a[i] * mid - b[i]; } else { break; } } } if (i == m) { ans = mid; left = mid + 1; } else right = mid - 1; } printf("%lld\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 5e4 + 10; int book1[500]; int book2[500]; int main() { int t; cin >> t; while (t--) { string s, t, p; cin >> s >> t >> p; int s1 = s.size(); int t1 = t.size(); int p1 = p.size(); int j = 0; int flag = 1; for (int i = 0; i < t1; i++) { if (t[i] == s[j]) j++; } if (j != s1) flag = 0; if (flag == 0) { printf("NO\n"); continue; } memset(book1, 0, sizeof(book1)); memset(book2, 0, sizeof(book2)); for (int i = 0; i < s1; i++) book1[s[i] - 'a']++; for (int i = 0; i < p1; i++) book1[p[i] - 'a']++; for (int i = 0; i < t1; i++) book2[t[i] - 'a']++; for (int i = 0; i < 26; i++) { if (book2[i] > book1[i]) { flag = 0; break; } } if (flag) printf("YES\n"); else printf("NO\n"); } }
2
#include <bits/stdc++.h> using namespace std; static const int MAXN = 1e5 + 10; int a[MAXN]; int n, k; long long sum, tmp, ans; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } if (sum % 3) puts("0"); else { sum /= 3; for (int i = 0; i < n - 1; i++) { tmp = tmp + a[i]; if (tmp == 2 * sum) ans = ans + k; if (tmp == sum) k++; } printf("%lld\n", ans); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int x, y, s; int main() { cin >> x >> y; s = x * x + y * y; for (int i = 10000; i >= 0; --i) if (i * i <= s) { if (0) { cout << "i" << ": " << i << endl; }; if (i * i == s) { puts("black"); } else if (i % 2 == 0) { if (x * y > 0) puts("black"); else puts("white"); } else { if (x * y > 0) puts("white"); else puts("black"); } return 0; } return 0; }
2
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void avneetsng() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int32_t main() { avneetsng(); long long t; cin >> t; while (t--) { string s; cin >> s; long long n = s.size(); long long n1 = 0, n0 = 0; for (auto i : s) { if (i == '0') { n0++; } else n1++; } long long k = min(n1, n0); if (k & 1) cout << "DA" << endl; else cout << "NET" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; using cd = complex<double>; const double PI = acos(-1); const int N = 1e5 + 10; ll dp[N], sum[N]; ll OO = 3e18; ll calc (int n, int mx){ if(dp[n - 1] == OO) return OO; return sum[n - 1] - sum[n - mx - 1]; } void solve(){ int n; ll k; cin>>n>>k; if(dp[n] < k){ cout<<"-1\n"; return; } vector<int> ans; int add = 0; while(n){ int idx = 0; ll take = 0; int L = 1, R = n; while(L <= R){ int mid = (L + R) / 2; if(calc(n, mid) < k){ idx = mid; take = calc(n, mid); L = mid + 1; }else R = mid - 1; } idx++; k -= take; n -= idx; int tmp = idx; while(idx){ ans.push_back(idx + add); idx--; } add += tmp; } for(int x : ans){ cout<<x<<" "; } cout<<"\n"; } int main(){ cin.tie(0); cin.sync_with_stdio(0); dp[0] = sum[0] = 1; for(int i = 1; i <= 60; i++){ dp[i] = sum[i - 1]; sum[i] = dp[i] + sum[i - 1]; if(dp[i] > OO - sum[i - 1]) break; } for(int i = 61; i < N; i++){ dp[i] = sum[i] = OO; } int T; cin>>T; while(T--) solve(); } /* * Think twice, code once * Think of different approaches to tackle a problem: write them down. * Think of different views of the problem. don't look from only one side. * don't get stuck in one approach. * common mistakes: over_flow * - out_of_bound index * -infinite loop * -corner cases * -duplication counting. */
5
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; inline long long read() { long long t = 0, dp = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') dp = -1; c = getchar(); } while (isdigit(c)) t = t * 10 + c - 48, c = getchar(); return t * dp; } inline void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) write(x / 10); putchar(x % 10 + 48); } inline void writeln(long long x) { write(x); puts(""); } inline void write_p(long long x) { write(x); putchar(' '); } const int N = 14, M = (1 << 14) - 1; long long dp[N][M], a[201], b[201], c[201], in[N][M], lin[N][N], n, q, m, mx; inline long long Dfs(int x, int zt) { if (dp[x][zt] != -1) return dp[x][zt]; int tp = 0, tmp = 0; dp[x][zt] = 0; int tzt = zt; zt ^= (1 << x - 1); for (int i = 1; i <= n; ++i) if (in[i][zt]) tp = i; for (int nzt = zt; nzt; nzt = (nzt - 1) & zt) if (in[tp][nzt]) { bool flag = 0; for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) if (i != x && j != x && lin[i][j]) if (in[i][nzt] ^ in[j][nzt]) flag = 1; if (flag) continue; int nrt = 0; for (int i = 1; i <= n; ++i) if (in[i][nzt]) if (lin[x][i]) { if (nrt) flag = 1; else nrt = i; } if (flag) continue; for (int i = 1; i <= q; ++i) { if (c[i] == x && in[a[i]][nzt] && in[b[i]][nzt]) { flag = 1; break; } if (in[c[i]][nzt] && ((!in[a[i]][nzt]) || (!in[b[i]][nzt]))) { flag = 1; break; } } if (flag) continue; if (nrt) dp[x][tzt] += Dfs(nrt, nzt) * Dfs(x, tzt ^ nzt); else for (int nrt = 1; nrt <= n; ++nrt) if (in[nrt][nzt]) { dp[x][tzt] += Dfs(nrt, nzt) * Dfs(x, tzt ^ nzt); } } return dp[x][tzt]; } int main() { n = read(); m = read(); q = read(); mx = (1 << n) - 1; for (int i = 1; i <= n; ++i) for (int zt = 0; zt <= mx; ++zt) dp[i][zt] = -1; for (int i = 1; i <= n; ++i) for (int zt = 0; zt <= mx; ++zt) in[i][zt] = (zt >> (i - 1)) & 1; for (int i = 1; i <= n; ++i) dp[i][1 << i - 1] = 1; for (int i = 1; i <= m; ++i) { int x = read(), y = read(); lin[x][y] = lin[y][x] = 1; } for (int i = 1; i <= q; ++i) { a[i] = read(); b[i] = read(); c[i] = read(); } writeln(Dfs(1, mx)); }
9
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxm = 5e3 + 10; const int inf = 0x3f3f3f3f; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <typename A> inline bool read(A &x) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; else if (c == EOF) return 0; for (x = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read(char &x) { while ((x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) == ' ' || x == '\n' || x == '\r') ; return x != EOF; } inline bool read(char *x) { while ((*x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) == '\n' || *x == ' ' || *x == '\r') ; if (*x == EOF) return 0; while (!(*x == '\n' || *x == ' ' || *x == '\r' || *x == EOF)) *(++x) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); *x = 0; return 1; } template <typename A, typename... B> inline bool read(A &x, B &...y) { return read(x) && read(y...); } template <typename A> inline bool write(A x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); return 0; } inline bool write(char x) { putc(x); return 0; } inline bool write(const char *x) { while (*x) { putc(*x); ++x; } return 0; } inline bool write(char *x) { while (*x) { putc(*x); ++x; } return 0; } template <typename A, typename... B> inline bool write(A x, B... y) { return write(x) || write(y...); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::putc; using io ::read; using io ::write; vector<int> eee[maxm]; int s, t, dis[maxm], h[maxm], fnasiofnoas[maxm], pree[maxm], num[maxm]; struct edge { int u, v, c, w, rev; edge(int a = -1, int b = 0, int cc = 0, int d = 0, int f = 0) : rev(a), u(b), v(cc), c(d), w(f) {} }; vector<edge> ed[maxm]; void addedge(int u, int v, int c, int w) { ed[u].push_back(edge((int)ed[v].size(), u, v, c, w)); ed[v].push_back(edge((int)ed[u].size() - 1, v, u, 0, -w)); } struct node { int id, val; node(int a = 0, int b = 0) : id(a), val(b) {} friend bool operator<(node e1, node e2) { return e1.val > e2.val; } }; priority_queue<node> pq; int costflow() { int res = 0; memset(h, 0, sizeof(h)); int tot = inf; while (tot > 0) { memset(dis, 0x3f, sizeof(dis)); while (!pq.empty()) pq.pop(); dis[s] = 0; pq.push(node(s, 0)); while (!pq.empty()) { node now = pq.top(); pq.pop(); int u = now.id; if (dis[u] < now.val) continue; int len = (int)ed[u].size(); for (int i = 0; i < len; i++) { int v = ed[u][i].v; int f = ed[u][i].c; int w = ed[u][i].w; if (ed[u][i].c && dis[v] > dis[u] + w + h[u] - h[v]) { dis[v] = dis[u] + w + h[u] - h[v]; fnasiofnoas[v] = u; pree[v] = i; pq.push(node(v, dis[v])); } } } if (dis[t] == inf) break; for (int i = 0; i <= t; i++) h[i] += dis[i], h[i] = min(h[i], inf); int flow = inf; for (int i = t; i; i = fnasiofnoas[i]) flow = min(flow, ed[fnasiofnoas[i]][pree[i]].c); tot -= flow; res += flow * h[t]; for (int i = t; i; i = fnasiofnoas[i]) { edge &e = ed[fnasiofnoas[i]][pree[i]]; e.c -= flow; ed[e.v][e.rev].c += flow; } } return res; } int main() { int n, m, k, c, d; read(n, m, k, c, d); for (int i = 1; i <= k; i++) { int x; read(x); num[x]++; } for (int i = 1; i <= m; i++) { int u, v; read(u, v); eee[u].push_back(v); eee[v].push_back(u); } s = 0, t = n * 100 + 1; for (int i = 1; i <= n; i++) addedge(s, i, num[i], 0); for (int i = 1; i <= 99; i++) { int N = (i - 1) * n; for (int j = 1; j <= n; j++) { for (int v : eee[j]) { for (int z = 1; z <= k; z++) { addedge(j + N, v + N + n, 1, (2 * z - 1) * d + c); } } addedge(j + N, j + N + n, inf, c); } addedge(N + 1, t, inf, 0); } addedge(99 * n + 1, t, inf, 0); printf("%d\n", costflow()); return 0; }
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } vector<long long> p; int main() { long long x, y; scanf("%lld%lld", &x, &y); long long t = x; for (long long i = 2; i * i <= t; i++) if (t % i == 0) { p.push_back(i); while (t % i == 0) t /= i; } if (t > 1) p.push_back(t); long long ans = 0; while (y) { long long g = gcd(x, y); x /= g; y /= g; long long mx = 0; for (long long a : p) if (x % a == 0) { mx = max(mx, y / a * a); } ans += y - mx; y = mx; } printf("%lld\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; template <typename... Args> void read(Args &...args) { ((cin >> args), ...); } template <typename... Args> void show(Args... args) { ((cout << args << " "), ...); } void solve() { int64_t n, k; read(n, k); vector<int> v(k); iota(v.begin(), v.end(), 1); int add = (n - (k * (k + 1) / 2)) / k; int rem = (n - (k * (k + 1) / 2)) % k; if (add < 0 || rem < 0) { show("NO"); return; } for (int i = 0; i < k; i++) v[i] += add; for (int i = k - 1; ~i && rem > 0; i--) { if (i) { int64_t mn = min(v[i - 1] * 2, v[i] + rem); rem = rem + v[i] - mn; v[i] = mn; } else { show("NO\n"); return; } } cout << "YES\n"; for (int i = 0; i < k; i++) cout << v[i] << " \n"[i + 1 == k]; } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T = 1; while (T--) solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, x, y; int main() { while (scanf("%d%d%d", &n, &x, &y) == 3) { int a = max(0, n * y - 100 * x), b = 100; printf("%d\n", a % b ? a / b + 1 : a / b); } return 0; }
0
#include <bits/stdc++.h> template <typename T> void read(T& num) { num = 0; int w = 1; char ch = getchar(); if (ch == '-') w = -1; while (ch < '0' || ch > '9') { ch = getchar(); if (ch == '-') w = -1; } while ((ch >= '0' && ch <= '9')) num = num * 10 + ch - '0', ch = getchar(); num = num * w; } template <typename T, typename... Args> void read(T& num, Args&... args) { read(num), read(args...); } const int N = 300010; const int MOD = 19260817; int n; int a[N]; int powmod[N]; struct Segment_Tree { struct Seg { int l, r; int sum, rev; Seg() {} Seg(int _l, int _r, int _sum, int _rev) : l(_l), r(_r), sum(_sum), rev(_rev) {} Seg operator+(const Seg& b) const { if (l == -1) return b; if (b.l == -1) return Seg(l, r, sum, rev); return Seg( l, b.r, ((((((long long)(sum) * (powmod[b.r - b.l + 1]) % (MOD))) + (b.sum)) >= MOD) ? ((((long long)(sum) * (powmod[b.r - b.l + 1]) % (MOD))) + (b.sum) - (MOD)) : ((((long long)(sum) * (powmod[b.r - b.l + 1]) % (MOD))) + (b.sum))), ((((((long long)(b.rev) * (powmod[r - l + 1]) % (MOD))) + (rev)) >= MOD) ? ((((long long)(b.rev) * (powmod[r - l + 1]) % (MOD))) + (rev) - (MOD)) : ((((long long)(b.rev) * (powmod[r - l + 1]) % (MOD))) + (rev)))); } }; Seg t[N << 3]; void push_up(int rt) { t[rt] = t[(rt << 1)] + t[(rt << 1 | 1)]; } void build(int rt, int l, int r) { t[rt] = Seg(l, r, 0, 0); if (l == r) return; int mid = (l + r) >> 1; build((rt << 1), l, mid); build((rt << 1 | 1), mid + 1, r); push_up(rt); } void modify(int rt, int pos, int val) { if (t[rt].l == t[rt].r) { t[rt].sum = t[rt].rev = val; return; } if (pos <= t[(rt << 1)].r) modify((rt << 1), pos, val); else modify((rt << 1 | 1), pos, val); push_up(rt); } Seg query(int rt, int l, int r) { if (t[rt].l > r || t[rt].r < l) return Seg(-1, -1, -1, -1); if (t[rt].l >= l && t[rt].r <= r) return t[rt]; return query((rt << 1), l, r) + query((rt << 1 | 1), l, r); } }; Segment_Tree t; int main() { read(n); for (int i = 1; i <= n; ++i) read(a[i]); powmod[0] = 1; for (int i = 1; i <= n; ++i) powmod[i] = (powmod[i - 1] << 1) % MOD; t.build(1, 1, n); bool flag = false; for (int i = 1; i <= n; ++i) { t.modify(1, a[i], 1); if ((a[i] << 1) - 1 <= n) { if (a[i] > 1 && t.query(1, 1, a[i] - 1).sum != t.query(1, a[i] + 1, (a[i] << 1) - 1).rev) { flag = true; break; } } else { if (a[i] < n && t.query(1, a[i] + 1, n).rev != t.query(1, (a[i] << 1) - n, a[i] - 1).sum) { flag = true; break; } } } if (flag) puts("YES"); else puts("NO"); return 0; }
9
#include <bits/stdc++.h> using namespace std; template <typename T> void chkmax(T &x, T y) { x = x > y ? x : y; } template <typename T> void chkmin(T &x, T y) { x = x > y ? y : x; } template <typename T> void update(T &x, T y, T mod) { x = x + y > mod ? x + y - mod : x + y; } const int INF = (1ll << 30); template <typename T> void read(T &x) { x = 0; bool f = 1; char ch; do { ch = getchar(); if (ch == '-') f = 0; } while (ch > '9' || ch < '0'); do { x = x * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9'); x = f ? x : -x; } template <typename T> void write(T x) { if (x < 0) x = ~x + 1, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int N = 5e5 + 7; const int mod = 1e9 + 7; long long n, m, k, ans, cnt, c[N], fa[N]; vector<long long> v; struct EDGE { long long x, y, w; bool operator<(const EDGE &rhs) const { return w < rhs.w; } } e[N]; inline long long qpow(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ret; } inline long long find(long long x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } inline void merge(long long x, long long y) { x = find(x); y = find(y); if (x == y) return; cnt--; fa[x] = y; } signed main() { read(n); read(m); read(k); for (long long i = 1; i <= n; i++) read(c[i]); for (long long i = 1, x, y; i <= m; i++) { read(e[i].x); read(e[i].y); e[i].w = c[e[i].x] ^ c[e[i].y]; } sort(e + 1, e + m + 1); for (long long i = 1; i <= n; i++) fa[i] = i; long long ret = 0; for (long long i = 1; i <= m + 1; i++) { if (i == 1 || e[i].w != e[i - 1].w) { if (v.size()) ans = (ans + qpow(2, cnt)) % mod; for (long long j = 0; j < v.size(); j++) fa[v[j]] = v[j]; v.clear(); cnt = n; merge(e[i].x, e[i].y); ret += (i != 1); } else { merge(e[i].x, e[i].y); } v.push_back(e[i].x); v.push_back(e[i].y); } printf("%lld\n", (1ll * (qpow(2, k) - ret + mod) % mod * qpow(2, n) % mod + ans) % mod); return 0; }
7
#include <bits/stdc++.h> using namespace std; int a[30][30]; int sum[30][2]; int main() { int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cin >> a[i][j]; sum[j][1] += a[i][j]; sum[i][0] += a[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (sum[j][1] > sum[i][0]) ans++; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-6; const int mod = 1e9 + 7; const int maxn = 1010; int n, d, h; int main() { while (cin >> n >> d >> h) { if (d < h || d > 2 * h) { puts("-1"); } else if (d == 1 && h == 1 && n > 2) { puts("-1"); } else { for (int i = 1; i <= h; i++) { printf("%d %d\n", i, i + 1); } if (h == d) { for (int i = h + 2; i <= n; i++) { printf("%d %d\n", 2, i); } } else if (h + 1 < n) { printf("%d %d\n", 1, h + 2); for (int i = h + 2; i <= d; i++) { printf("%d %d\n", i, i + 1); } if (d + 1 < n) { for (int i = d + 2; i <= n; i++) { printf("%d %d\n", 1, i); } } } } } }
4
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) { out << p.first << ' ' << p.second; } template <class T> istream& operator>>(istream& in, vector<T>& v) { for (auto& x : v) in >> x; return in; } template <class T> ostream& operator<<(ostream& out, vector<T>& v) { for (auto x : v) out << x << ' '; return out; } int n; bool A[5200][5200]; int dp[5200][5200], dp1[5200][5200]; vector<int> v; string s; int maxm = 1; bool check(int x) { bool f = 0; for (long long i = (0); i < (n / x); ++i) { for (long long j = (0); j < (n / x); ++j) { if (dp[(i + 1) * x - 1][(j + 1) * x - 1] < x && dp1[(i + 1) * x - 1][(j + 1) * x - 1] < x) { f = 1; } } } if (f) return 0; return 1; } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> n; for (long long i = (0); i < (n); ++i) { cin >> s; for (long long j = (0); j < ((int)s.size()); ++j) { int nm; if (s[j] >= 'A' && s[j] <= 'F') nm = 10 + (s[j] - 'A'); else nm = s[j] - '0'; while (nm > 0) { v.push_back(nm % 2); nm /= 2; } while ((int)v.size() < 4) v.push_back(0); reverse((v).begin(), (v).end()); int cnt = 0; for (long long k = (0); k < (4); ++k) { A[i][j * 4 + cnt] = v[cnt]; cnt++; } v.clear(); } } for (long long i = (0); i < (n); ++i) { dp[0][i] = A[0][i]; dp1[0][i] = 1 - A[0][i]; } for (long long i = (0); i < (n); ++i) { dp[i][0] = A[i][0]; dp1[i][0] = 1 - A[i][0]; } for (long long i = (1); i < (n); ++i) { for (long long j = (1); j < (n); ++j) { if (A[i][j] == 1) { dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1; } else dp1[i][j] = min(min(dp1[i - 1][j], dp1[i][j - 1]), dp1[i - 1][j - 1]) + 1; } } for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (check(i)) maxm = max(maxm, i); if (i * i != n) if (check(n / i)) maxm = max(maxm, n / i); } } cout << maxm << "\n"; }
5
#include <bits/stdc++.h> using namespace std; template <class T> void chkmin(T &a, T b) { if (b < a) a = b; } template <class T> void chkmax(T &a, T b) { if (a < b) a = b; } int n; int sz[100005]; int sum[100005]; vector<pair<int, int> > con[100005]; long long dfs(int u, int p) { int i, v, w; long long rlt = 0; vector<pair<double, int> > vec; vec.clear(); sz[u] = 1; sum[u] = 0; for (i = con[u].size() - 1; i >= 0; i--) { v = con[u][i].first; w = con[u][i].second; if (v == p) continue; rlt += dfs(v, u); sz[u] += sz[v]; sum[u] += 2 * w + sum[v]; rlt += 1LL * w * sz[v]; vec.push_back(pair<double, int>(1.0 * sz[v] / (sum[v] + 2 * w), i)); } sort(vec.begin(), vec.end()); int t = 0; for (i = vec.size() - 1; i >= 0; i--) { v = con[u][vec[i].second].first; w = con[u][vec[i].second].second; rlt += 1LL * t * sz[v]; t += sum[v] + 2 * w; } return rlt; } int main() { int i, j, k, w; scanf("%d", &n); for (i = 0; i < n - 1; i++) { scanf("%d%d%d", &j, &k, &w); j--, k--; con[j].push_back(pair<int, int>(k, w)); con[k].push_back(pair<int, int>(j, w)); } printf("%.10lf\n", dfs(0, -1) / double(n - 1)); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { queue<pair<int, int> > bfs; queue<int> cekori; pair<int, int> start, kraj; map<pair<int, int>, int> mapa; map<pair<int, int>, int>::iterator it; int n, i, j, one, two, three; cin >> start.first >> start.second >> kraj.first >> kraj.second; cin >> n; for (i = 0; i < n; i++) { cin >> one >> two >> three; for (j = two; j <= three; j++) { pair<int, int> temp = make_pair(one, j); mapa[temp] = 1000000000; } } mapa[start] = 0; bfs.push(start); cekori.push(0); while (!bfs.empty()) { pair<int, int> temp = bfs.front(); int cek = cekori.front(); bfs.pop(); cekori.pop(); pair<int, int> poteg = make_pair(temp.first - 1, temp.second); if ((temp.first - 1 >= 0) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } poteg = make_pair(temp.first + 1, temp.second); if ((temp.first + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } poteg = make_pair(temp.first, temp.second - 1); if ((temp.second - 1 >= 0) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } poteg = make_pair(temp.first, temp.second + 1); if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } poteg = make_pair(temp.first - 1, temp.second + 1); if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } poteg = make_pair(temp.first + 1, temp.second + 1); if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } poteg = make_pair(temp.first + 1, temp.second - 1); if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } poteg = make_pair(temp.first - 1, temp.second - 1); if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) && (mapa[poteg] > cek + 1)) { mapa[poteg] = cek + 1; bfs.push(poteg); cekori.push(cek + 1); } if (mapa[kraj] < 1000000000) { cout << mapa[kraj]; return 0; } } cout << "-1"; return 0; }
5
#include <bits/stdc++.h> using namespace std; pair<long long, long long> m[150]; bool f(pair<long long, long long> a, pair<long long, long long> b) { if (b.second > a.second) return 1; return 0; } int main() { int n; long long a, b, c, k = 0, i = 0; cin >> n; for (a = 0; a < n; a++) { cin >> m[a].first; b = m[a].first; m[a].second = 0; while (b % 2 == 0) { b /= 2; m[a].second++; } while (b % 3 == 0) { b /= 3; m[a].second--; } } sort(m, m + n, f); for (a = 0; a < n; a++) cout << m[a].first << " "; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 1; double p[MAXN][MAXN]; double E[MAXN]; double A[MAXN], B[MAXN]; bool vis[MAXN]; int n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%lf", &p[i][j]); } } for (int i = 0; i < MAXN; i++) { E[i] = 1e18; A[i] = 0; B[i] = 1; } E[n - 1] = 0; for (int iter = 0; iter <= n; iter++) { double minim = 1e18; int idx = -1; for (int i = 0; i < n; i++) { if (minim > E[i] && !vis[i]) { idx = i, minim = E[i]; } } if (idx == 0) { printf("%.10lf\n", E[0]); return 0; } vis[idx] = true; for (int i = 0; i < n; i++) { if (!vis[i]) { A[i] += B[i] * E[idx] * 0.01 * p[i][idx]; B[i] *= (1.0 - 0.01 * p[i][idx]); if ((A[i] + 1.0) / (1.0 - B[i]) < E[i]) { E[i] = (A[i] + 1.0) / (1.0 - B[i]); } } } } assert(false); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> bad; int ar[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; } for (int i = 1; i < n - 1; i++) { if ((ar[i - 1] <= ar[i] && ar[i] <= ar[i + 1]) || (ar[i - 1] >= ar[i] && ar[i] >= ar[i + 1])) { bad.push_back(i); } } cout << n - bad.size() << "\n"; for (int i = 0; i < n; i++) { if (!binary_search(bad.begin(), bad.end(), i)) { cout << ar[i] << " "; } } cout << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; for (int t = 1; t <= q; t++) { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long c = 1; for (int i = 0; i < n - 1; i++) { if (a[i] != a[i + 1]) { c = 0; break; } } if (c == 1) { cout << "NO\n"; } else { cout << "YES\n"; int k = 0; while (a[k] == a[k + 1]) k++; for (int j = 0; j < k; j++) { cout << j + 1 << " " << k + 2 << "\n"; } for (int i = k; i < n - 1; i++) { if (a[i] != a[i + 1]) { cout << i + 1 << " " << i + 2 << "\n"; } else { int j = i; while (a[j] == a[i + 1] and j >= 0) j--; if (j < 0) { j = i + 1; while (a[j] == a[i + 1]) j++; } cout << j + 1 << " " << i + 2 << "\n"; } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; char field[1005][1005]; char test[1005][1005]; char f[1005][1005]; int n, m; int dx[4] = {1, 1, 1, 0}, dy[4] = {-1, 0, 1, 1}; set<char> s[2]; bool check() { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) for (int k = 0; k < 4; ++k) { if (test[i][j] == test[i + dx[k]][j + dy[k]] || test[i][j] == '0') return 0; } } return 1; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%s", field[i] + 1); } bool ok = 1; for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i][j] = 0; s[0].clear(); s[1].clear(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { if (field[i][j] == '0') continue; int I = i % 2, J = j; if (f[I][J] != 0 && f[I][J] != field[i][j]) ok = 0; f[I][J] = field[i][j]; s[j % 2].insert(field[i][j]); } if (s[0].size() > 2 || s[1].size() > 2) ok = 0; if (ok) { for (int i = 0; i < 2; ++i) { while (s[i].size() < 2) { for (char c = '1'; c <= '4'; ++c) { if (s[i].find(c) != s[i].end() || s[1 - i].find(c) != s[1 - i].end()) continue; s[i].insert(c); break; } } } for (int i = 0; i < 2; ++i) for (int j = 1; j <= m; ++j) { if (f[i][j] != '0' && f[i][j] != 0) continue; for (char k = '1'; k <= '4'; ++k) { if (s[j % 2].find(k) == s[j % 2].end() || k == f[1 - i][j]) continue; f[i][j] = k; break; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int I = i % 2, J = j; test[i][j] = f[I][J]; } test[i][m + 1] = 0; } if (ok && check()) { for (int i = 1; i <= n; ++i) printf("%s\n", test[i] + 1); return 0; } } ok = 1; for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j) f[i][j] = 0; s[0].clear(); s[1].clear(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { if (field[i][j] == '0') continue; int I = i, J = j % 2; if (f[I][J] != 0 && f[I][J] != field[i][j]) ok = 0; f[I][J] = field[i][j]; s[i % 2].insert(field[i][j]); } if (s[0].size() > 2 || s[1].size() > 2) ok = 0; if (ok) { for (int i = 0; i < 2; ++i) { while (s[i].size() < 2) { for (char c = '1'; c <= '4'; ++c) { if (s[i].find(c) != s[i].end() || s[1 - i].find(c) != s[1 - i].end()) continue; s[i].insert(c); break; } } } for (int i = 1; i <= n; ++i) for (int j = 0; j < 2; ++j) { if (f[i][j] != '0' && f[i][j] != 0) continue; for (char k = '1'; k <= '4'; ++k) { if (s[i % 2].find(k) == s[i % 2].end() || k == f[i][1 - j]) continue; f[i][j] = k; break; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int I = i, J = j % 2; test[i][j] = f[I][J]; } test[i][m + 1] = 0; } if (ok && check()) { for (int i = 1; i <= n; ++i) printf("%s\n", test[i] + 1); return 0; } } printf("0\n"); return 0; }
8
#include <bits/stdc++.h> void Get(int &T) { char C; bool F = 0; for (; C = getchar(), C < '0' || C > '9';) if (C == '-') F = 1; for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0') ; F && (T = -T); } void Get(long long &T) { char C; bool F = 0; for (; C = getchar(), C < '0' || C > '9';) if (C == '-') F = 1; for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0') ; F && (T = -T); } int N; long long X[100005]; long long T[100005]; int Order[100005]; long long V; int F[100005]; bool Cmp(int A, int B) { if (T[A] != T[B]) return T[A] < T[B]; return X[A] < X[B]; } void Init() { Get(N); for (int i = 1; i <= N; i++) { Get(X[i]); Get(T[i]); Order[i] = i; } Get(V); for (int i = 0; i <= N; i++) { long long NewX = X[i] + T[i] * V; long long NewY = -X[i] + T[i] * V; X[i] = NewX; T[i] = NewY; } std::sort(Order + 1, Order + N + 1, Cmp); } long long Stack[100005]; int StackTop; int BinSrh(int L, int R, long long X) { for (;;) { if (L == R) return L; if (L + 1 == R) { if (X < Stack[L]) return L; return R; } int Mid = L + R; Mid >>= 1; if (X < Stack[Mid]) R = Mid; else L = Mid; } } int Ans1, Ans2; void Work() { F[0] = 0; memset(Stack, 0x2f, sizeof Stack); Stack[0] = X[0]; StackTop = 0; for (int k = 1, i; k <= N; k++) { i = Order[k]; int Pos = BinSrh(0, StackTop + 1, X[i]); if (T[i] < T[0] || X[i] < X[0]) continue; F[i] = Pos; Stack[Pos] = X[i]; if (Pos == StackTop + 1) StackTop++; if (F[i] > Ans1) Ans1 = F[i]; } F[0] = 0; memset(Stack, 0x2f, sizeof Stack); Stack[0] = 0xc0c0c0c0c0c0c0c0ll; StackTop = 0; for (int k = 1, i; k <= N; k++) { i = Order[k]; int Pos = BinSrh(0, StackTop + 1, X[i]); F[i] = Pos; Stack[Pos] = X[i]; if (Pos == StackTop + 1) StackTop++; if (F[i] > Ans2) Ans2 = F[i]; } } void Output() { printf("%d %d\n", Ans1, Ans2); } int main() { Init(); Work(); Output(); return 0; }
7
#include <bits/stdc++.h> using namespace std; long d[100001]; void calc(int k) { d[0] = 1; for (int i = 1; i < 100001; i++) { d[i] += d[i - 1]; if (i >= k) { d[i] = (d[i] + d[i - k]) % 1000000007; } } for (int i = 2; i < 100001; i++) { d[i] = (d[i] + d[i - 1]) % 1000000007; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long n, k; cin >> n >> k; calc(k); long *a1 = new long[n]; d[0] = 0; for (int i = 0; i < n; i++) { long a, b; cin >> a >> b; a1[i] = (d[b] - d[a - 1] + 1000000007) % 1000000007; } for (int i = 0; i < n; i++) cout << a1[i] << endl; }
4
#include <bits/stdc++.h> using namespace std; using vll = vector<long long>; long long mod = 1000000007; void solve() { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; map<long long, vll> cnt; for (long long i = 0; i < n; i++) { cnt[i + 1 - a[i]].push_back(a[i]); } long long mx = -1; for (auto &it : cnt) { long long temp = 0; for (auto &x : cnt[it.first]) { temp += x; } mx = max(temp, mx); } cout << mx << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, 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(); } return x * f; } long long dx[4] = {0, 0, -1, 1}; long long dy[4] = {1, -1, 0, 0}; int n, m, q; char s[11]; long long bz[100010 << 1][52]; int pos[100010 << 1][52]; int head[100010 << 1], nex[100010 << 1], val[100010 << 1], op[100010 << 1], tot; int T[100010 << 2]; void pushdown(int rt) { if (T[rt]) { T[rt << 1] = T[rt << 1 | 1] = T[rt]; T[rt] = 0; } } void insert(int l, int r, int rt, int x, int y, int v) { if (x <= l && r <= y) { T[rt] = v; return; } int mid = l + r >> 1; pushdown(rt); if (mid >= x) insert(l, mid, rt << 1, x, y, v); if (mid < y) insert(mid + 1, r, rt << 1 | 1, x, y, v); } int query(int l, int r, int rt, int v) { if (l == r) return T[rt]; int mid = l + r >> 1; pushdown(rt); if (mid >= v) return query(l, mid, rt << 1, v); else return query(mid + 1, r, rt << 1 | 1, v); } void add(long long x, long long y, int tp) { tot++; nex[tot] = head[x]; head[x] = tot; val[tot] = y; op[tot] = tp; } struct vec { long long x1, x2, y1, y2; int op; void read() { scanf("%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2); if (y1 < y2) op = 0; if (y1 > y2) op = 1; if (x1 > x2) op = 2; if (x1 < x2) op = 3; } } a[100010]; struct node { int op; long long len, x, y; void read() { scanf("%I64d%I64d%s%I64d", &x, &y, s, &len); if (s[0] == 'U') op = 0; if (s[0] == 'D') op = 1; if (s[0] == 'L') op = 2; if (s[0] == 'R') op = 3; } } b[100010]; pair<long long, long long> cal(long long x, long long y, int p) { if (a[p].x1 == a[p].x2) { if (a[p].x1 == x) { if (min(a[p].y1, a[p].y2) <= y && y <= max(a[p].y1, a[p].y2)) return make_pair(x, y); if (y < min(a[p].y1, a[p].y2)) return make_pair(x, min(a[p].y1, a[p].y2)); else return make_pair(x, max(a[p].y1, a[p].y2)); } else return make_pair(a[p].x1, y); } else { if (a[p].y1 == y) { if (min(a[p].x1, a[p].x2) <= x && x <= max(a[p].x1, a[p].x2)) return make_pair(x, y); if (x < min(a[p].x1, a[p].x2)) return make_pair(min(a[p].x1, a[p].x2), y); else return make_pair(max(a[p].x1, a[p].x2), y); } else return make_pair(x, a[p].y1); } } int dis(long long x1, long long y1, long long x2, long long y2) { return abs(x1 - x2) + abs(y1 - y2); } pair<long long, long long> find(long long x, long long y, int tp, long long step) { return make_pair(x + step * dx[tp], y + step * dy[tp]); } void print(pair<long long, long long> p) { long long x = p.first, y = p.second; x = max(x, 0ll); x = min(x, (long long)m); y = max(y, 0ll); y = min(y, (long long)m); printf("%I64d %I64d\n", x, y); } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i].read(); q = read(); for (int i = 1; i <= q; i++) b[i].read(); for (int i = 0; i <= 3; i++) { tot = 0; memset(head, 0, sizeof(head)); memset(T, 0, sizeof(T)); for (int j = 1; j <= q; j++) if (b[j].op == i) { if (i < 2) add(b[j].y, j + n, 1); else add(b[j].x, j + n, 1); } for (int j = 1; j <= n; j++) { if (i == 0) add(max(a[j].y1, a[j].y2), j, 0); if (i == 1) add(min(a[j].y1, a[j].y2), j, 0); if (i == 2) add(min(a[j].x1, a[j].x2), j, 0); if (i == 3) add(max(a[j].x1, a[j].x2), j, 0); } for (int j = 1; j <= n; j++) if (a[j].op == i) { if (i < 2) add(a[j].y2, j, 1); else add(a[j].x2, j, 1); } int now = (i == 0 || i == 3) ? m : 0; while (now != -1 && now != m + 1) { for (int j = head[now]; j; j = nex[j]) { if (!op[j]) { if (i < 2) insert(0, m, 1, min(a[val[j]].x1, a[val[j]].x2), max(a[val[j]].x1, a[val[j]].x2), val[j]); else insert(0, m, 1, min(a[val[j]].y1, a[val[j]].y2), max(a[val[j]].y1, a[val[j]].y2), val[j]); } else { int tmp; if (val[j] <= n) { if (i < 2) tmp = query(0, m, 1, a[val[j]].x1); else tmp = query(0, m, 1, a[val[j]].y1); } else { if (i < 2) tmp = query(0, m, 1, b[val[j] - n].x); else tmp = query(0, m, 1, b[val[j] - n].y); } pos[val[j]][0] = tmp; if (tmp) { if (val[j] <= n) bz[val[j]][0] = abs(a[tmp].x2 - a[val[j]].x2) + abs(a[tmp].y2 - a[val[j]].y2); else bz[val[j]][0] = abs(a[tmp].x2 - b[val[j] - n].x) + abs(a[tmp].y2 - b[val[j] - n].y); } else bz[val[j]][0] = 1ll << 50; } } if (i == 0 || i == 3) now--; else now++; } } for (int i = 1; i <= 50; i++) bz[0][i] = 1ll << 50; for (int i = 1; i <= 50; i++) for (int j = 1; j <= n + q; j++) { pos[j][i] = pos[pos[j][i - 1]][i - 1]; bz[j][i] = bz[j][i - 1] + bz[pos[j][i - 1]][i - 1]; bz[j][i] = min(bz[j][i], 1ll << 50); } for (int i = 1; i <= q; i++) { int tmp = i + n; long long t = b[i].len; for (int j = 50; j >= 0; j--) if (bz[tmp][j] <= t) { t -= bz[tmp][j]; tmp = pos[tmp][j]; } if (tmp == i + n) { if (pos[tmp][0]) { pair<long long, long long> tq = cal(b[i].x, b[i].y, pos[tmp][0]); int tw = dis(tq.first, tq.second, b[i].x, b[i].y); if (tw >= t) print(find(b[i].x, b[i].y, b[i].op, t)); else print(find(tq.first, tq.second, a[pos[tmp][0]].op, t - tw)); } else print(find(b[i].x, b[i].y, b[i].op, t)); } else { if (!pos[tmp][0]) print(find(a[tmp].x2, a[tmp].y2, a[tmp].op, t)); else { pair<long long, long long> tq = cal(a[tmp].x2, a[tmp].y2, pos[tmp][0]); int tw = dis(tq.first, tq.second, a[tmp].x2, a[tmp].y2); if (tw >= t) print(find(a[tmp].x2, a[tmp].y2, a[tmp].op, t)); else print(find(tq.first, tq.second, a[pos[tmp][0]].op, t - tw)); } } } return 0; }
11
#include <bits/stdc++.h> using namespace std; int n, k, x[100010], y; inline double sqr(double x) { return x * x; } inline double dist(int k, int l, int r) { double ret = sqrt(sqr(y) + sqr(x[n] - x[k])); if (l <= r) ret += min(dist(l, 1, 0), dist(r, 1, 0)) + x[r] - x[l]; return ret; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i <= n; i++) scanf("%d", x + i); scanf("%d", &y); int t = x[k - 1]; sort(x, x + n); double ans = 1e100; if (k == n + 1) ans = min(dist(0, 1, 0), dist(n - 1, 1, 0)) + x[n - 1] - x[0]; else { for (int i = 0; i < n; i++) if (x[i] == t) k = i; for (int i = 0; i < n; i++) { if (i == 0 || i > k) ans = min(ans, min(x[k] - x[0] + x[i] - x[0] + dist(i, max(i, k) + 1, n - 1), abs(x[i] - x[k]) + x[i] - x[0] + dist(0, max(i, k) + 1, n - 1))); if (i == n - 1 || i < k) ans = min(ans, min(x[n - 1] - x[k] + x[n - 1] - x[i] + dist(i, 0, min(i, k) - 1), abs(x[k] - x[i]) + x[n - 1] - x[i] + dist(n - 1, 0, min(i, k) - 1))); } } printf("%.12lf\n", ans); }
9
#include <bits/stdc++.h> using namespace std; std::vector<int> numbers; int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; int mask[60]; int best[101][(1 << 16)]; int last[101][(1 << 16)]; int main(int argc, const char* argv[]) { int N; cin >> N; numbers.resize(N); for (int k = 2; k < 60; k++) for (int i = 0; i < 16; i++) if (k % primes[i] == 0) mask[k] |= (1 << i); for (int i = 0; i < N; i++) cin >> numbers[i]; memset(best, 0x7f, sizeof(best)); memset(&best[0][0], 0, (1 << 16) * sizeof(best[0][0])); for (int i = 0; i < N; i++) { for (int j = 0; j < (1 << 16); j++) for (int k = 1; k < 60; k++) { if (j & mask[k]) continue; int cur_best = best[i + 1][j | mask[k]]; int cur = best[i][j] + abs(numbers[i] - k); if (cur < cur_best) { best[i + 1][j | mask[k]] = cur; last[i + 1][j | mask[k]] = k; } } } std::vector<int> ans(N); int best_mask = 0; for (int j = 1; j < (1 << 16); j++) if (best[N][j] < best[N][best_mask]) best_mask = j; for (int i = N; i > 0; i--) { ans[i - 1] = last[i][best_mask]; best_mask &= ~mask[ans[i - 1]]; } for (typeof(ans.end()) it = ans.begin(); it != ans.end(); ++it) cout << *it << ' '; cout << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int tot = 0, cnt = 0; for (auto x : s) { if (x == ')') tot--; if (x == '(') tot++; if (x == '?') cnt++; } if (abs(cnt) % 2 != abs(tot) % 2 || cnt < tot) { cout << ":("; return 0; } int open = (cnt - tot) / 2, close = cnt - open; if (open < 0 || close < 0) { cout << ":("; return 0; }; int cur = 0; string ss = ""; for (auto x : s) if (x == '?') { if (open) { ss += '('; open--; } else ss += ')'; } else ss += x; for (int i = 0; i < n; ++i) { char x = ss[i]; cur += (x == '(' ? 1 : -1); if (cur <= 0 && i + 1 != n) { cout << ":("; return 0; } } if (cur != 0) { cout << ":("; return 0; }; cout << ss; return 0; }
4
#include <bits/stdc++.h> using namespace std; struct node { int F[4]; }; set<node> Set, Set2; bool operator<(node A, node B) { int j = 0; while (A.F[j] == B.F[j] && j + 1 < 4) j++; return (A.F[j] < B.F[j]); } set<node>::iterator it; node T; string s[4] = {"Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"}; string st; int minn, ind, j, n, i; int fix[4]; int main() { cin >> n; cin >> st; st = st + '?'; n++; Set.insert((node){0, 0, 0, 0}); for (i = 0; i < n; i++) { if (st[i] == '?') { Set2.clear(); for (it = Set.begin(); it != Set.end(); it++) { T = *it; minn = 1000000000; for (j = 0; j < 4; j++) minn = min(minn, T.F[j]); for (j = 0; j < 4; j++) if (T.F[j] == minn) { T.F[j]++; if (i == n - 1) fix[j] = 1; Set2.insert(T); T.F[j]--; } } Set = Set2; } else { for (j = 0; j < 4; j++) if (s[j][0] == st[i]) ind = j; Set2.clear(); for (it = Set.begin(); it != Set.end(); it++) { T = *it; T.F[ind]++; Set2.insert(T); } Set = Set2; } } for (i = 0; i < 4; i++) if (fix[i]) cout << s[i] << endl; }
7
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const double Pi = 3.1415926535897932; const int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; int main() { int n, a, b; cin >> n >> a >> b; b *= 5; int ans = n; for (int i = 0; i <= n; i += a) { int now = (n - i) % b; ans = min(ans, now); } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int i = 1; while (pow(n, i) < m) { i++; } if (pow(n, i) == m) cout << "YES\n" << i - 1; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int ans[110][110] = {0}; vector<int> adj[110][110]; int vis[110]; void dfs(int parent, int u, int color) { vis[u] = 1; ans[parent][u]++; for (int v = 0; v < (int)adj[u][color].size(); v++) { if (!vis[adj[u][color][v]]) { dfs(parent, adj[u][color][v], color); } } } int main() { int n, m; cin >> n >> m; set<int> s[110]; for (int i = 0; i < m; i++) { int x, y, c; cin >> x >> y >> c; adj[x][c].push_back(y); adj[y][c].push_back(x); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { memset(vis, 0, sizeof(vis)); if ((int)adj[i][j].size() > 0) { dfs(i, i, j); } } } int q; cin >> q; for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; cout << ans[x][y] << endl; } return 0; }
3