solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() {int a,b,c; cin>>a>>b>>c; swap(a,b); swap(a,c); cout<<a<<' '<<b<<' '<<c;}
0
#include <bits/stdc++.h> using namespace std; void check(int a[], int n) { int i = 0; while (i < n) { if (a[i] == 1) { cout << "HARD"; return; } i++; } cout << "EASY"; return; } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } check(a, n); }
1
#include <bits/stdc++.h> const int N = 12005; struct line { double l, r, k, b; void move(int x) { l += x, r += x, b -= k * x; } double f(double x) { return k * x + b; } void add(double x, double y) { k += x, b += y; } } t[N]; int n, q, a, b, top, x[N]; double y[N]; double get() { double now = 1e100, res; for (int i = 1; i <= top; ++i) { double x = -t[i].b / t[i].k; x = std::min(std::max(x, t[i].l), t[i].r); double y = t[i].f(x); if (std::fabs(y) < now) now = std::fabs(y), res = x; } return res; } int main() { scanf("%d%d%d%d", &n, &q, &a, &b); for (int i = 1; i <= n; ++i) scanf("%d", &x[i]); t[1] = (line){1, 1.0 * q, 2, -2.0 * x[1]}, top = 1; y[1] = get(); for (int i = 2; i <= n; ++i) { int k; for (k = 1; k <= top; ++k) if (t[k].r > y[i - 1]) break; top += 2; for (int j = top; j >= k + 2; --j) t[j] = t[j - 2]; t[k].r = y[i - 1], t[k + 2].l = y[i - 1]; for (int j = 1; j <= k; ++j) t[j].move(a); for (int j = k + 2; j <= top; ++j) t[j].move(b); t[k + 1] = (line){t[k].r, t[k + 2].l, 0, 0}; for (int j = 1; j <= top; ++j) t[j].add(2, -2 * x[i]); while (t[top].l > q) --top; t[top].r = q; y[i] = get(); } for (int i = n - 1; i >= 1; --i) { if (y[i] < y[i + 1] - b) y[i] = y[i + 1] - b; else if (y[i] > y[i + 1] - a) y[i] = y[i + 1] - a; } double ans = 0; for (int i = 1; i <= n; ++i) printf("%.9lf ", y[i]), ans += (x[i] - y[i]) * (x[i] - y[i]); printf("\n%.9lf\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const long long N = 2e2 + 7; const long long mod = 1e9 + 7; const long double eps = 1E-7; long long a, b, n, cnt, mx, ans, c; long long used[N]; string s[12] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "B", "H"}; string s1, s2, s3; int main() { ios_base::sync_with_stdio(0); cin >> s1 >> s2 >> s3; for (int i = 0; i < 12; i++) { if (s[i] == s1) a = i; if (s[i] == s2) b = i; if (s[i] == s3) c = i; } long long arr[3] = {a, b, c}; sort(arr, arr + 3); do { if (arr[0] > arr[1]) cnt += 11 - arr[0] + arr[1] + 1; else cnt += arr[1] - arr[0]; if (arr[1] > arr[2]) ans += 11 - arr[1] + arr[2] + 1; else ans += arr[2] - arr[1]; if (cnt == 4 and ans == 3) return cout << "major", 0; if (cnt == 3 and ans == 4) return cout << "minor", 0; cnt = 0, ans = 0; } while (next_permutation(arr, arr + 3)); cout << "strange"; }
1
#include <bits/stdc++.h> using namespace std; int n, m, cnta, cntb, cntc; int ans[1010][1010]; int main() { scanf("%d%d%d%d%d", &n, &m, &cnta, &cntb, &cntc); if ((n * m) & 1) { puts("IMPOSSIBLE"); return 0; } else if (((n & 1) == 0) && ((m & 1) == 0)) { for (int i = 1; i <= n; i += 2) for (int j = 1; j <= m; j += 2) { if ((i + j) % 4 == 0) ans[i][j] = ans[i + 1][j] = ans[i][j + 1] = ans[i + 1][j + 1] = 2; else ans[i][j] = ans[i + 1][j] = ans[i][j + 1] = ans[i + 1][j + 1] = 1; --cntc; } int nowx = 1, nowy = 1; while (cnta >= 2 && cntc < 0) { if ((nowx + nowy) % 4 != 0) { if ((nowx >> 1) & 1) { ans[nowx][nowy] = ans[nowx][nowy + 1] = 3; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 1; cnta -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx][nowy + 1] = 1; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 3; cnta -= 2; cntc++; } } else { if ((nowx >> 1) & 1) { ans[nowx][nowy] = ans[nowx][nowy + 1] = 2; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 3; cnta -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx][nowy + 1] = 3; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 2; cnta -= 2; cntc++; } } if (nowy + 2 > m) nowx += 2, nowy = 1; else nowy += 2; } nowx = n - 1; nowy = m - 1; while (cntb >= 2 && cntc < 0) { if (((nowx - 1) >> 1) & 1) { ans[nowx][nowy] = ans[nowx + 1][nowy] = 4; ans[nowx][nowy + 1] = ans[nowx + 1][nowy + 1] = 5; cntb -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx + 1][nowy] = 5; ans[nowx][nowy + 1] = ans[nowx + 1][nowy + 1] = 4; cntb -= 2; cntc++; } if (nowy - 2 <= 0) nowx -= 2, nowy = m - 1; else nowy -= 2; } if (cntc < 0) puts("IMPOSSIBLE"); else for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) putchar('a' + ans[i][j] - 1); putchar('\n'); } } else if (((n & 1) == 1) && ((m & 1) == 0)) { for (int i = 1; i <= m; i += 2) if ((i >> 1) & 1) ans[n][i] = ans[n][i + 1] = 6, cnta--; else ans[n][i] = ans[n][i + 1] = 7, cnta--; if (cnta < 0) { puts("IMPOSSIBLE"); return 0; } n--; for (int i = 1; i <= n; i += 2) for (int j = 1; j <= m; j += 2) { if ((i + j) % 4 == 0) ans[i][j] = ans[i + 1][j] = ans[i][j + 1] = ans[i + 1][j + 1] = 2; else ans[i][j] = ans[i + 1][j] = ans[i][j + 1] = ans[i + 1][j + 1] = 1; --cntc; } int nowx = 1, nowy = 1; while (cnta >= 2 && cntc < 0) { if ((nowx + nowy) % 4 != 0) { if ((nowx >> 1) & 1) { ans[nowx][nowy] = ans[nowx][nowy + 1] = 3; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 1; cnta -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx][nowy + 1] = 1; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 3; cnta -= 2; cntc++; } } else { if ((nowx >> 1) & 1) { ans[nowx][nowy] = ans[nowx][nowy + 1] = 2; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 3; cnta -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx][nowy + 1] = 3; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 2; cnta -= 2; cntc++; } } if (nowy + 2 > m) nowx += 2, nowy = 1; else nowy += 2; } nowx = n - 1; nowy = m - 1; while (cntb >= 2 && cntc < 0) { if (((nowx - 1) >> 1) & 1) { ans[nowx][nowy] = ans[nowx + 1][nowy] = 4; ans[nowx][nowy + 1] = ans[nowx + 1][nowy + 1] = 5; cntb -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx + 1][nowy] = 5; ans[nowx][nowy + 1] = ans[nowx + 1][nowy + 1] = 4; cntb -= 2; cntc++; } if (nowy - 2 <= 0) nowx -= 2, nowy = m - 1; else nowy -= 2; } if (cntc < 0) puts("IMPOSSIBLE"); else for (int i = 1; i <= n + 1; ++i) { for (int j = 1; j <= m; ++j) putchar('a' + ans[i][j] - 1); putchar('\n'); } } else if (((n & 1) == 0) && ((m & 1) == 1)) { for (int i = 1; i <= n; i += 2) if ((i >> 1) & 1) ans[i][m] = ans[i + 1][m] = 6, cntb--; else ans[i][m] = ans[i + 1][m] = 7, cntb--; if (cntb < 0) { puts("IMPOSSIBLE"); return 0; } m--; for (int i = 1; i <= n; i += 2) for (int j = 1; j <= m; j += 2) { if ((i + j) % 4 == 0) ans[i][j] = ans[i + 1][j] = ans[i][j + 1] = ans[i + 1][j + 1] = 2; else ans[i][j] = ans[i + 1][j] = ans[i][j + 1] = ans[i + 1][j + 1] = 1; --cntc; } int nowx = 1, nowy = 1; while (cnta >= 2 && cntc < 0) { if ((nowx + nowy) % 4 != 0) { if ((nowx >> 1) & 1) { ans[nowx][nowy] = ans[nowx][nowy + 1] = 3; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 1; cnta -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx][nowy + 1] = 1; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 3; cnta -= 2; cntc++; } } else { if ((nowx >> 1) & 1) { ans[nowx][nowy] = ans[nowx][nowy + 1] = 2; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 3; cnta -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx][nowy + 1] = 3; ans[nowx + 1][nowy] = ans[nowx + 1][nowy + 1] = 2; cnta -= 2; cntc++; } } if (nowy + 2 > m) nowx += 2, nowy = 1; else nowy += 2; } nowx = n - 1; nowy = m - 1; while (cntb >= 2 && cntc < 0) { if (((nowx - 1) >> 1) & 1) { ans[nowx][nowy] = ans[nowx + 1][nowy] = 4; ans[nowx][nowy + 1] = ans[nowx + 1][nowy + 1] = 5; cntb -= 2; cntc++; } else { ans[nowx][nowy] = ans[nowx + 1][nowy] = 5; ans[nowx][nowy + 1] = ans[nowx + 1][nowy + 1] = 4; cntb -= 2; cntc++; } if (nowy - 2 <= 0) nowx -= 2, nowy = m - 1; else nowy -= 2; } if (cntc < 0) puts("IMPOSSIBLE"); else for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m + 1; ++j) putchar('a' + ans[i][j] - 1); putchar('\n'); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; constexpr int N = 1e3 + 5; int n, m; char c; int field[N][N]; template <typename P> struct Dsu { vector<P> pr; vector<int> sz; Dsu(int s) { pr.resize(s + 1); sz.resize(s + 1); } int toid(const pii& a) { return a.first * N + a.second; } P get(const P& a) { if (toid(pr[toid(a)]) == toid(a)) return a; return pr[toid(a)] = get(pr[toid(a)]); } void make(const P& a) { if (pr[toid(a)] == P()) { pr[toid(a)] = a; sz[toid(a)] = 1; } } void uni(P a, P b) { a = get(a); b = get(b); if (a != b) { if (sz[toid(a)] < sz[toid(b)]) swap(a, b); sz[toid(a)] += sz[toid(b)]; pr[toid(b)] = a; } } }; Dsu<pii> d(N* N); void bfs(int i, int j) { d.make({i, j}); queue<pii> q; q.push({i, j}); while (q.size()) { auto [x, y] = q.front(); q.pop(); if (x == 0 || y == 0 || x == n + 1 || y == m + 1 || field[x][y]) continue; d.make({x, y}); d.uni({i, j}, {x, y}); field[x][y] = -1; q.push({x + 1, y}); q.push({x, y + 1}); q.push({x - 1, y}); q.push({x, y - 1}); } } void tr(int i, int j) { field[i][j] = 1; set<int> st; st.insert(d.toid(d.get({i - 1, j}))); st.insert(d.toid(d.get({i, j - 1}))); st.insert(d.toid(d.get({i + 1, j}))); st.insert(d.toid(d.get({i, j + 1}))); for (auto el : st) field[i][j] += d.sz[el]; } void Solve() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> c; field[i][j] = (c == '*'); } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (!field[i][j]) bfs(i, j); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (field[i][j] != -1) tr(i, j); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (field[i][j] > 0) cout << field[i][j] % 10; else cout << '.'; } cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); Solve(); return 0; }
3
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int l,n; cin >> l >> n; int b = 0; for(int i=0; i<n; i++){ int x,w; cin >> x >> w; b += x*w; } if(b == 0){ cout << 0 << endl; }else{ cout << 1 << endl; cout << -b/abs(b) << " " << abs(b) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 10; int ch[30][MAXN], t[MAXN]; bool win[MAXN], lose[MAXN]; int node = 1; void dfs(int v) { int tc = 0; for (int i = 0; i < 26; i++) if (ch[i][v] != 0) { dfs(ch[i][v]); if (!win[ch[i][v]]) win[v] = true; if (!lose[ch[i][v]]) lose[v] = true; tc++; } if (tc == 0) lose[v] = true; } void insert(string &s) { int v = 0, i = 0; while (i < (int)s.size()) { if (ch[s[i] - 'a'][v] == 0) ch[s[i] - 'a'][v] = node++; v = ch[s[i] - 'a'][v]; i++; } t[v]++; } int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { string s; cin >> s; insert(s); } dfs(0); if (win[0] && lose[0]) cout << "First\n"; else if (!win[0] && !lose[0]) cout << "Second\n"; else if (win[0]) cout << ((k % 2) ? "First\n" : "Second\n"); else cout << "Second\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; int answer = -1; map<int, int> mp; for (int j = 1; j <= t; j++) { int k; cin >> k; if (mp.count(k)) { int tmp = j - mp[k] + 1; if (answer == -1 || answer > tmp) { answer = tmp; } } mp[k] = j; } cout << answer << endl; } }
3
#include <bits/stdc++.h> using namespace std; template <class T> T chmin(T& a, T& b) { if (a > b) return a; } template <class T> T chmax(T& a, T& b) { if (a < b) return b; } const long long INF = 1e18; const int MOD = 3; const int N = 7000; bool v[N]; vector<long long> adj[N]; int main() { long long l, r; cin >> l >> r; vector<long long> v; v.push_back(0); v.push_back(4); v.push_back(7); long long cnt = 1; while (cnt != 10) { for (int i = v.size() - pow(2, cnt), j = v.size(); i < j; i++) { v.push_back(4 * pow(10, cnt) + v[i]); v.push_back(7 * pow(10, cnt) + v[i]); } cnt++; } sort(v.begin(), v.end()); long long lo, up; for (int i = 0; v[i] < l; i++) lo = i; for (int i = v.size() - 1; v[i] >= r; i--) up = i; lo++; unsigned long long ans = 0; ans += v[lo] * (min(v[lo] - l + 1, r - l + 1)); for (int i = lo + 1; i <= up; i++) { ans += (v[i] * (min(min(r, v[i]) - v[i - 1], r - l))); } cout << ans; }
3
#include <bits/stdc++.h> using namespace std; long long t, m, n; vector<int> A; long long solve() { int ans = 0; for (int i = 0; i < n; i++) { int j = i; while (j + 1 < n && A[j + 1] <= 2 * A[j]) j++; ans = max(ans, j - i + 1); i = j; } return ans; } int main() { cin >> n; A.resize(n); for (int i = 0; i < n; i++) cin >> A[i]; cout << solve() << endl; return 0; }
2
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int,int> pii; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define all(a) (a).begin(),(a).end() #define pb push_back #define INF (1e9+1) //#define INF (1LL<<59) //0-index ??????????????°?????????(????????´??°or?????? , ????°????or?????§???or???) verified AOJ DSL_2,DSL_3 template<class T> class segtree{ private: vector<T> dat; vector<T> lazy; int _size; //the number of leaves T _init; public: segtree(int __size=0 , T init=numeric_limits<T>::max() ):_size(__size), _init(init), dat(4*__size,init), lazy(4*__size,init){ if(__size==0)return ; int x=1; while(x<_size)x*=2; _size = x; } void update(int s,int t, T x ,int num=1,int a=0,int b=-1){ if(b==-1)b=_size-1; //I couldn't "int b=_size". if(b<s||t<a)return ; if(s<=a&&b<=t){ lazy[num]+=x; //addition // lazy[num]=x; //initialize // dat[num] = min(dat[num],x); //minimum // dat[num] = max(dat[num],x); //maximum dat[num]+=x*(b-a+1); //sum return ; } update(s,t,x,num*2+0,a,(a+b)/2); update(s,t,x,num*2+1,(a+b)/2+1,b); // dat[num] = min(min(dat[num*2],dat[num*2+1]),lazy[num]); //minimum // dat[num] = max(max(dat[num*2],dat[num*2+1]),lazy[num]); //maximum dat[num] = dat[num*2]+dat[num*2+1]+lazy[num]*(b-a+1); //sum } // call find(s,t) T find(int s,int t,int num=1,int a=0,int b=-1){ if(b==-1)b=_size-1; //I couldn't "int b=_size". if(b<s||t<a)return _init; if(s<=a&&b<=t)return dat[num]; ll l = find(s,t,num*2,a,(a+b)/2); ll r = find(s,t,num*2+1,(a+b)/2+1,b); // return min(min(l,r),lazy[num]); //minimum // return max(max(l,r),lazy[num]); //maximum return l+r+lazy[num]*(min(b,t)-max(a,s)+1); //sum } int size(){return _size;} }; int main(){ int n,q; cin>>n>>q; segtree<ll> st(n,0); rep(i,q){ int p; cin>>p; if(!p){ int s,t,x; cin>>s>>t>>x; s--,t--; st.update(s,t,x); // rep(i,n)cout<<st.find(i,i)<<" ";cout<<endl; }else{ int s,t; cin>>s>>t; s--,t--; cout<<st.find(s,t)<<endl; } } }
0
#include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #include <string> #include <string.h> using namespace std; const int MAX_N=10000; int n,y; int main(){ cin >> y >> n; if(y==0){ if(n>=1989) cout << "H" << n-1989+1 << endl; else if(n>=1926) cout << "S" << n-1926+1 << endl; else if(n>=1912) cout << "T" << n-1912+1 << endl; else if(n>=1868) cout << "M" << n-1868+1 << endl; }else if(y==1){ cout << n+1868-1 << endl; }else if(y==2){ cout << n+1912-1 << endl; }else if(y==3){ cout << n+1926-1 << endl; }else if(y==4){ cout << n+1989-1 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b; cin >> a >> b; int h = 23 - a; int m = 60 - b; cout << (h * 60) + m << endl; } return 0; }
1
#include <bits/stdc++.h> char s[((int)1e6 + 5)]; int main(void) { int len; int p = -1; int first = -1; int last = -1; int exp = 0; scanf(" %s", s); len = strlen(s); for (int i = 0; i < len; i++) { if (s[i] == '.') { p = i; } if (isdigit(s[i]) && s[i] != '0' && first == -1) { first = i; } if (isdigit(s[i])) { last = i; } } if (p != -1) { exp = p - first; if (exp > 0) { exp--; } } else { exp = last - first; } while (last >= first && (s[last] == '0' || s[last] == '.')) { last--; } for (int i = first; i <= last; i++) { if (i == first + 1) { printf("."); } if (isdigit(s[i])) { printf("%c", s[i]); } } if (exp != 0) { printf("E%d\n", exp); } return 0; }
3
#include <bits/stdc++.h> namespace FastIO { bool IOerror = 0; inline char nc() { static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; if (p1 == pend) { p1 = buf; pend = buf + fread(buf, 1, 100000, stdin); if (pend == p1) { IOerror = 1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } template <class T> inline bool read(T& x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()) ; if (IOerror) return false; if (ch == '-') sign = 1, ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0'; if (sign) x = -x; return true; } template <class T, class... U> bool read(T& h, U&... t) { return read(h) && read(t...); } }; // namespace FastIO using namespace std; using namespace FastIO; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int INF = 0x3f3f3f3f; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int cnt = 0; for (auto i : s) { if (cnt == 0 || i == 'A') cnt++; else cnt--; } cout << cnt << endl; } return 0; }
3
#include <iostream> #include<string> using namespace std; int main(){ int n, a = 0; string s; cin >> n >> s; for (int i = 0; i < n - 2; i++) if (s[i] == 'A'&&s[i + 1] == 'B'&&s[i + 2] == 'C') a++; cout << a; }
0
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; const double PI = acos(-1); void fft(vector<cd>& a, bool invert) { int n = a.size(); if (n == 1) return; vector<cd> a0(n / 2), a1(n / 2); for (int i = 0; 2 * i < n; i++) { a0[i] = a[2 * i]; a1[i] = a[2 * i + 1]; } fft(a0, invert); fft(a1, invert); double ang = 2 * PI / n * (invert ? -1 : 1); cd w(1), wn(cos(ang), sin(ang)); for (int i = 0; 2 * i < n; i++) { a[i] = a0[i] + w * a1[i]; a[i + n / 2] = a0[i] - w * a1[i]; if (invert) { a[i] /= 2; a[i + n / 2] /= 2; } w *= wn; } } vector<int> multiply(vector<int> const& a, vector<int> const& b) { vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end()); int n = 1; while (n < a.size() + b.size()) n <<= 1; fa.resize(n); fb.resize(n); fft(fa, false); fft(fb, false); for (int i = 0; i < n; i++) fa[i] *= fb[i]; fft(fa, true); vector<int> result(n); for (int i = 0; i < n; i++) result[i] = round(fa[i].real()); return result; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool pali(string s) { long long l = s.length(); for (int i = 0; i < l / 2; i++) { if (s[i] != s[l - i - 1]) return false; } return true; } int main() { ios_base::sync_with_stdio(false); long long t; cin >> t; while (t--) { long long n, k; cin >> n; long long* a = new long long[n]; long long* m = new long long[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]] = i; } long long* dp = new long long[n](); long long s = 1; bool ans = true; while (s <= n) { long long id = m[s]; if (dp[id] != 0) { ans = false; break; } while (id < n && dp[id] == 0) { dp[id] = s; s++; id++; } } for (int i = 0; i < n; i++) { if (dp[i] != a[i]) ans = false; } if (ans) cout << "Yes" << endl; else cout << "No" << endl; } }
1
#include<bits/stdc++.h> using namespace std; #define int long long typedef vector<int>vint; typedef pair<int,int>pint; typedef vector<pint>vpint; #define rep(i,n) for(int i=0;i<(n);i++) #define reps(i,f,n) for(int i=(f);i<(n);i++) #define all(v) (v).begin(),(v).end() #define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++) #define pb push_back #define fi first #define se second template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} const int INF=1001001001; int dy[]={-1,0,1,0}; int dx[]={0,-1,0,1}; int H,W,N; char fld[55][55]; int D[50][50][50][50]; void bfs(int sy,int sx){ fill_n(*D[sy][sx],2500,INF); D[sy][sx][sy][sx]=0; queue<pint>que; que.push(pint(sy,sx)); while(que.size()){ int y,x; tie(y,x)=que.front(); que.pop(); rep(i,4){ int ny=y+dy[i],nx=x+dx[i]; if(ny<0||ny>=H||nx<0||nx>=W||fld[ny][nx]=='#'||D[sy][sx][ny][nx]!=INF)continue; D[sy][sx][ny][nx]=D[sy][sx][y][x]+1; que.push(pint(ny,nx)); } } } int r[50][50],n[50][50],f[50][50]; vint ev[50][50]; int dp[1111][2]; int calc(vint vec,int r,int n,int f){ fill_n(*dp,1111*2,INF); dp[0][0]=0; rep(i,vec.size()){ int t=min(dp[i][0]+n,dp[i][1]+vec[i]*r); dp[i+1][0]=t+f; dp[i+1][1]=t-r*vec[i]; } return dp[vec.size()][0]; } signed main(){ cin>>H>>W>>N; rep(i,H)cin>>fld[i]; rep(i,H)rep(j,W)if(fld[i][j]=='.')bfs(i,j); rep(i,H)rep(j,W)cin>>r[i][j]; rep(i,H)rep(j,W)cin>>n[i][j]; rep(i,H)rep(j,W)cin>>f[i][j]; int y,x;cin>>y>>x; ev[y][x].pb(0); int t=0; rep(i,N-1){ int yy,xx; cin>>yy>>xx; while(y!=yy||x!=xx){ int d=-1; rep(i,4){ int ny=y+dy[i],nx=x+dx[i]; if(ny<0||ny>=H||nx<0||nx>=W||fld[ny][nx]=='#')continue; if(D[y][x][yy][xx]==D[ny][nx][yy][xx]+1)d=i; } y+=dy[d];x+=dx[d];t++; ev[y][x].pb(t); } } int ans=0; rep(i,H)rep(j,W)ans+=calc(ev[i][j],r[i][j],n[i][j],f[i][j]); cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long int; string s, t; int mp[28], tempMap[28]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i, j, len1, len2, can = -1; cin >> s >> t; len1 = (int)s.size(), len2 = (int)t.size(); for (i = 0; i < len1; i++) { mp[s[i] - 'a']++; } int rng = min(len1, len2) - 1; if (len1 > len2) rng++; for (i = 0; i < rng; i++) { if (mp[t[i] - 'a']) { mp[t[i] - 'a']--; for (j = 0; j < 26; j++) { tempMap[j] = mp[j]; } bool flag = false; if (i == len2 - 1) { if (len1 > len2 && i == rng - 1) flag = true, can = i; } else { for (j = t[i + 1] - 'a' + 1; j < 26; j++) { if (mp[j]) { flag = true; break; } } if (flag) can = i; } } else { break; } } memset(mp, 0, sizeof mp); for (i = 0; i < len1; i++) mp[s[i] - 'a']++; if (can == -1) { int found = -1; for (i = t[0] - 'a' + 1; i < 26; i++) { if (mp[i]) { found = i; break; } } if (found == -1) cout << -1 << endl; else { cout << (char)(found + 'a'); mp[found]--; for (i = 0; i < 26; i++) { for (j = 0; j < mp[i]; j++) { cout << (char)(i + 'a'); } } cout << endl; } } else { for (i = 0; i <= can; i++) { cout << t[i]; mp[t[i] - 'a']--; } if (len1 > len2 && can == len2 - 1) { for (i = 0; i < 26; i++) { for (j = 0; j < mp[i]; j++) { cout << (char)(i + 'a'); } } cout << endl; } else { for (i = t[can + 1] - 'a' + 1; i < 26; i++) { if (mp[i]) { cout << (char)(i + 'a'); mp[i]--; break; } } for (i = 0; i < 26; i++) { for (j = 0; j < mp[i]; j++) { cout << (char)(i + 'a'); } } cout << endl; } } return 0; }
4
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #define rep(x, a, b) for(int x = (a); x <= (b); ++x) #define rop(x, a, b) for(int x = (a); x < (b); ++x) #define per(x, a, b) for(int x = (a); x >= (b); --x) using namespace std; typedef long long LL; typedef double DB; const int mod = 1000000007; LL f[4005][2005][2], S[2005], as; inline LL MOD(LL x) { return (x % mod + mod) % mod; } int n, m, K; int main() { scanf("%d%d%d", &n, &m, &K); if(n > m) swap(n, m); int lm = n + m; f[0][0][1] = 1; rep(i, 1, lm) { S[0] = MOD(f[i - 1][0][0] + f[i - 1][0][1]); rep(k, 1, n) S[k] = MOD(S[k - 1] + f[i - 1][k][0] + f[i - 1][k][1]); rep(k, 0, n) { f[i][k][1] = MOD(f[i - 1][k][0] + f[i - 1][k][1]); f[i][k][0] = MOD(S[k - 1] - (k >= K ? S[k - K] : 0)); } rep(k, 0, n) { if(k % (K - 1) == n % (K - 1)) { int lsbl = i * (K - 1) - k + 1; if(lsbl <= m && lsbl % (K - 1) == m % (K - 1)) as = MOD(as + f[i][k][0]); } } } cout << as << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; void solve() { long long n, q; cin >> n >> q; long long v[n], c[n]; for (long long i = 0; i < n; i++) cin >> v[i]; for (long long i = 0; i < n; i++) { cin >> c[i]; c[i]--; } while (q--) { long long a, b; cin >> a >> b; vector<long long> dp(n + 1, -1e18); dp[n] = 0; long long ma = 0, m1 = n, m2 = n; for (long long i = 0; i < n; i++) { long long g; if (m1 == c[i]) g = m2; else g = m1; if (dp[c[i]] != -1e18) { long long x = dp[g] + v[i] * b; long long y = max(dp[c[i]], dp[c[i]] + v[i] * a); x = max(x, y); ma = max(x, ma); dp[c[i]] = x; if (x > dp[m1]) { m2 = m1; m1 = c[i]; } else if (x > dp[m2] && c[i] != m1) { m2 = c[i]; } } else { long long x = dp[g] + v[i] * b; ma = max(x, ma); dp[c[i]] = x; if (x > dp[m1]) { m2 = m1; m1 = c[i]; } else if (x > dp[m2] && c[i] != m1) { m2 = c[i]; } } } cout << ma << endl; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(x); return v; } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; err(++it, args...); } const int INF = 0x3f3f3f3f; const int N = 200010; int p[N], sz[N]; int flag[N]; int find(int x) { while (x != p[x]) p[x] = p[p[x]], x = p[x]; return x; } int n, m; int s, t, ds, dt; int hasdircon; int g[N << 1][2]; int fgs[N], fgt[N]; vector<int> can_s, can_t; vector<pair<int, int> > ans; int main() { while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) { p[i] = i; sz[i] = 1; flag[i] = 0; } for (int i = 1; i <= m; i++) { scanf("%d%d", &g[i][0], &g[i][1]); if (g[i][0] > g[i][1]) swap(g[i][0], g[i][1]); } scanf("%d%d%d%d", &s, &t, &ds, &dt); if (s > t) { swap(s, t); swap(ds, dt); } hasdircon = false; ans.clear(); for (int i = 1; i <= m; i++) { if (g[i][0] == s && g[i][1] == t) hasdircon = true; else if (g[i][0] == s) continue; else if (g[i][1] == s) continue; else if (g[i][0] == t) continue; else if (g[i][1] == t) continue; else { int fx = find(g[i][0]); int fy = find(g[i][1]); if (fx == fy) continue; sz[fx] += sz[fy]; sz[fy] = 0; p[fy] = fx; ans.push_back({g[i][0], g[i][1]}); } } for (int i = 1; i <= m; i++) { if (g[i][0] == s && g[i][1] == t) continue; else if (g[i][0] == s) { int v = g[i][1]; int fv = find(v); flag[fv] |= 1; fgs[fv] = v; } else if (g[i][1] == s) { int v = g[i][0]; int fv = find(v); flag[fv] |= 1; fgs[fv] = v; } else if (g[i][0] == t) { int v = g[i][1]; int fv = find(v); flag[fv] |= 2; fgt[fv] = v; } else if (g[i][1] == t) { int v = g[i][0]; int fv = find(v); flag[fv] |= 2; fgt[fv] = v; } else { continue; } } int ca = 0, cb = 0, cc = 0; for (int i = 1; i <= n; i++) { if (p[i] != i) continue; if (i == s || i == t) continue; if (flag[i] == 1) ca++; if (flag[i] == 2) cb++; if (flag[i] == 3) cc++; } if (cc == 0 && hasdircon == false) { puts("No"); continue; } if (ds < 1 || dt < 1) { puts("No"); continue; } if (cc == 0) { ans.push_back({s, t}); ds--, dt--; } else { for (int i = 1; i <= n; i++) { if (flag[i] == 3) { ans.push_back({s, fgs[i]}); ans.push_back({t, fgt[i]}); ds--, dt--; flag[i] = 0; cc--; break; } } } if (ds < ca || dt < cb) { puts("No"); continue; } for (int i = 1; i <= n; i++) { if (flag[i] == 1) { ans.push_back({s, fgs[i]}); ds--; } if (flag[i] == 2) { ans.push_back({t, fgt[i]}); dt--; } } if (ds + dt < cc) { puts("No"); continue; } for (int i = 1; i <= n; i++) { if (flag[i] == 3) { if (ds > 0) { ans.push_back({s, fgs[i]}); ds--; } else if (dt > 0) { ans.push_back({t, fgt[i]}); dt--; } } } puts("Yes"); for (auto i = ans.begin(); i != ans.end(); i++) { printf("%d %d\n", i->first, i->second); } } return 0; }
6
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define REP(i,n) for(int i=1;i<=(n);i++) #define mp make_pair #define pb push_back #define fst first #define snd second typedef long long ll; typedef pair<int,int> pii; const int maxn=200005; const int mlog=20; int n; vector<int>edge[maxn]; int dep[maxn],par[maxn][mlog]; int sum1[maxn],sum2[maxn],val[maxn],f[maxn]; void prework(int x,int p,int d,int mx){ dep[x]=d; par[x][0]=p; REP(j,mlog-1)par[x][j]=par[par[x][j-1]][j-1]; val[x]=mx; mx=max(mx,x); rep(i,int(edge[x].size())){ int y=edge[x][i]; if(y==p)continue; prework(y,x,d+1,mx); } } void work(int rt,int x,int p,bool en){ sum1[x]=1; if(x!=rt&&x>val[rt])en=true; sum2[x]=!en; rep(i,int(edge[x].size())){ int y=edge[x][i]; if(y==p||y>rt)continue; work(rt,y,x,en); sum1[x]+=sum1[y]; sum2[x]+=sum2[y]; } } void calc(int x,int p){ if(x==1)f[x]=0; else{ int y=val[x],z=x; for(int i=mlog-1;i>=0;i--){ if(par[z][i]&&dep[par[z][i]]>dep[y])z=par[z][i]; } f[x]=f[y]; if(z<y)f[x]+=sum1[z]; if(z<val[y])f[x]-=sum2[z]; if(x>y)f[x]+=sum2[x]; } rep(i,int(edge[x].size())) if(edge[x][i]!=p)calc(edge[x][i],x); } int main(){ scanf("%d",&n); REP(i,n-1){ int u,v; scanf("%d%d",&u,&v); edge[u].pb(v); edge[v].pb(u); } prework(1,0,0,0); for(int i=n;i>=1;i--)if(!sum1[i])work(i,i,par[i][0],false); calc(1,0); for(int i=2;i<=n;i++)printf("%d%c",f[i],i==n?'\n':' '); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { map<long long, bool> mp; long long n; cin >> n; long long k = n; for (int i = 1; i <= n; i++) { long long a; cin >> a; mp[a] = 1; while (mp[k] == 1) { cout << k << " "; k--; } cout << '\n'; } }
1
#include <bits/stdc++.h> using namespace std; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; void init() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long Gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } long long Lcm(long long a, long long b) { return (a * b) / Gcd(a, b); } int main() { long long t; cin >> t; while (t--) { int alpha[26] = {0}; string s; cin >> s; for (int i = 0; i < s.size(); i++) { alpha[s[i] - 'a']++; } int a = 0, b = 0; for (int i = 0; i < 26; i++) { if (alpha[i] >= 2) a++; else if (alpha[i] == 1) b++; } cout << a + b / 2 << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; long long finMin(long long x, long long y, long long a, long long l, long long r) { if (l == r) return l; if (l + 1 == r) { if (y + l * 2 * a > x) return l; return r; } long long m = (l + r) / 2; if (y + m * 2 * a > x) return finMin(x, y, a, l, m); return finMin(x, y, a, m, r); } long long finMax(long long x, long long y, long long a, long long l, long long r) { if (l == r) return l; if (l + 1 == r) { if (y + r * 2 * a < x) return r; return l; } long long m = (l + r) / 2; if (y + m * 2 * a < x) return finMax(x, y, a, m, r); return finMax(x, y, a, l, m); } void swap(long long &a, long long &b) { long long t = a; a = b; b = t; } int main() { long long a, b, x1, y1, x2, y2, k = 0; cin >> a >> b >> x1 >> y1 >> x2 >> y2; long long k1 = finMin(x1, y1, b, -1000000000, 1000000000); long long k2 = finMax(x2, y2, b, -1000000000, 1000000000); if (k2 >= k1) k = max(k, k2 - k1 + 1); k1 = finMin(x2, y2, b, -1000000000, 1000000000); k2 = finMax(x1, y1, b, -1000000000, 1000000000); if (k2 >= k1) k = max(k, k2 - k1 + 1); k1 = finMin(x1, -y1, a, -1000000000, 1000000000); k2 = finMax(x2, -y2, a, -1000000000, 1000000000); if (k2 >= k1) k = max(k, k2 - k1 + 1); k1 = finMin(x2, -y2, a, -1000000000, 1000000000); k2 = finMax(x1, -y1, a, -1000000000, 1000000000); if (k2 >= k1) k = max(k, k2 - k1 + 1); cout << k; return 0; }
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1000000000") #pragma GCC optimize("Ofast,no-stack-protector") using namespace std; map<long long, map<int, long long> > ed; int nx[500][500]; bool bad[500][500]; long long go(long long mask, int x) { if (ed[mask].count(x)) { return ed[mask][x]; } long long to = mask; for (int i = 0; i < (1 << 5); i++) { if ((mask >> (long long)i) & 1) { to |= (1ll << (long long)(i ^ x)); } } return ed[mask][x] = to; } vector<long long> g; map<long long, int> num; const int maxn = (int)1e5 + 10; bool used[maxn]; vector<pair<int, int> > red[maxn]; int com[maxn]; int up[maxn]; int pr[maxn]; long long cur[maxn]; bool ch[maxn]; long long gl[maxn]; int h[maxn]; void dfs(int v, int par, int c, int val, int hh = 1) { up[v] = val; used[v] = true; com[v] = c; pr[v] = par; h[v] = hh; for (auto [u, x] : red[v]) { if (u == par) { continue; } if (!used[u]) { dfs(u, v, c, val ^ x, hh + 1); } else if (u == 1) { gl[c] = go(1, val ^ x); } else if (h[v] > h[u]) { int y = val ^ x ^ up[u]; if ((cur[c] >> (long long)y) & 1) { ch[c] = false; } else { cur[c] = go(cur[c], y); } } } } vector<long long> cyc; vector<long long> ms; const int mod = (int)1e9 + 7; int add(int x, int y) { x += y; if (x >= mod) { x -= mod; } return x; } int mul(int x, int y) { return (long long)x * y % mod; } int dp[2][500]; int main() { queue<long long> q; q.push(1); ed[1] = map<int, long long>(); while (!q.empty()) { long long mask = q.front(); q.pop(); for (int i = 0; i < (1 << 5); i++) { long long to = go(mask, i); if (!ed.count(to)) { q.push(to); ed[to] = map<int, long long>(); } } } int ps = 0; for (auto f : ed) { g.push_back(f.first); num[f.first] = ps++; } for (int i = 0; i < ps; i++) { for (int j = 0; j < ps; j++) { long long to = g[i]; bool ok = true; for (int k = 1; k < (1 << 5); k++) { if ((g[j] >> (long long)k) & 1) { to = go(to, k); } if ((g[j] >> (long long)k) & 1) { if ((g[i] >> (long long)k) & 1) { ok = false; } } } bad[i][j] = !ok; nx[i][j] = num[to]; } } int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y, w; scanf("%d %d %d", &x, &y, &w); red[x].push_back(make_pair(y, w)); red[y].push_back(make_pair(x, w)); } used[1] = true; for (auto [u, w] : red[1]) { if (!used[u]) { cur[u] = 1; gl[u] = -1; ch[u] = true; dfs(u, 1, u, w); if (ch[u]) { cyc.push_back(gl[u]); ms.push_back(cur[u]); } } } dp[0][0] = 1; int it = 0; for (int i = 0; i < (int)cyc.size(); i++) { memset(dp[it ^ 1], 0, sizeof dp[it ^ 1]); int numb = num[ms[i]]; int numbgl = -1; bool ok = false; int un = numb; int cf = 1; if (cyc[i] != -1) { cf = 2; } if (cyc[i] != -1 && cyc[i] != 1) { numbgl = num[cyc[i]]; ok = !bad[numb][numbgl]; un = nx[numb][numbgl]; } for (int j = 0; j < (int)g.size(); j++) { dp[it ^ 1][j] = add(dp[it ^ 1][j], dp[it][j]); if (!bad[j][numb]) { int to = nx[j][numb]; dp[it ^ 1][to] = add(dp[it ^ 1][to], mul(dp[it][j], cf)); } if (ok && !bad[j][un]) { int to = nx[j][un]; dp[it ^ 1][to] = add(dp[it ^ 1][to], dp[it][j]); } } it ^= 1; } int ans = 0; for (int i = 0; i < (int)g.size(); i++) { ans = add(ans, dp[it][i]); } cout << ans << '\n'; }
4
#include <bits/stdc++.h> using namespace std; int main() { iostream::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> pos(n + 1); vector<int> toplace(n, n - 1); for (int i = 0; i < n; i++) { int a; cin >> a; pos[a] = i; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a = pos[a]; b = pos[b]; if (a > b) swap(a, b); toplace[a] = min(toplace[a], b - 1); } long long ans = 0; int cur = n - 1; for (int i = n - 1; i >= 0; i--) { cur = min(cur, toplace[i]); ans += cur - i + 1; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 200000; long long t[3][4 * MAXN + 1]; int a, b; void upd(int num, int pos, int delta, int v = 1, int l = 1, int r = MAXN) { if (l == r) { if (num == 1 && t[num][v] + delta >= b) t[num][v] = b; else if (num == 1) t[num][v] += delta; if (num == 2 && t[num][v] + delta >= a) t[num][v] = a; else if (num == 2) t[num][v] += delta; } else { int m = (l + r) / 2; if (pos <= m) upd(num, pos, delta, v * 2, l, m); else upd(num, pos, delta, v * 2 + 1, m + 1, r); t[num][v] = t[num][v * 2] + t[num][v * 2 + 1]; } } long long get(int num, int l, int r, int tv = 1, int tl = 1, int tr = MAXN) { if (l > r) return 0; else if (l == tl && r == tr) return t[num][tv]; else { int tm = (tl + tr) / 2; return get(num, l, min(tm, r), tv * 2, tl, tm) + get(num, max(tm + 1, l), r, tv * 2 + 1, tm + 1, tr); } } int main() { int n, k, q; cin >> n >> k >> a >> b >> q; for (int i = 0; i < q; i++) { int type; scanf("%d", &type); if (type == 1) { int d, a; scanf("%d%d", &d, &a); upd(1, d, a); upd(2, d, a); } else { int p; scanf("%d", &p); long long ans = 0; ans += get(1, 1, p - 1); ans += get(2, p + k, n); cout << ans << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long ans, n; int main() { cin >> n; ans = 1; ans += 3ll * (n - 3ll); ans += (n - 3ll) * (n - 4ll); cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int perm[N]; pair<int, int> v[N]; vector<pair<int, int>> s[N]; signed main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i].first >> v[i].second; s[v[i].first].push_back({v[i].second, i}); } set<pair<int, int>> best; vector<pair<int, int>> ch; for (int i = 1; i <= n; i++) { for (auto elem : s[i]) best.insert(elem); assert(!best.empty()); int p = (*best.begin()).second; perm[p] = i; best.erase(best.begin()); if (!best.empty()) { ch.push_back({p, (*best.begin()).second}); } } pair<int, int> res = {-1, -1}; for (auto [a, b] : ch) { if (v[b].first <= perm[a] && perm[a] <= v[b].second) { if (v[a].first <= perm[b] && perm[b] <= v[a].second) { res = {a, b}; break; } } } if (res.first == -1) { cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << perm[i] << ' '; cout << endl; } else { cout << "NO" << endl; for (int i = 1; i <= n; i++) cout << perm[i] << ' '; cout << endl; swap(perm[res.first], perm[res.second]); for (int i = 1; i <= n; i++) cout << perm[i] << ' '; cout << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int t1[100005] = {}; for (int i = 1; i <= n; i++) { int c, t; cin >> c >> t; t1[i] = t1[i - 1] + c * t; } int i0 = 0; for (int i = 0; i < m; i++) { int v; cin >> v; while (t1[i0] < v && i0 < n) i0++; cout << i0 << endl; } return 0; }
2
#include<iostream> #include<cstdio> #include<math.h> #include<algorithm> #define Lens 250 #define Nil -1 using namespace std; struct Node { int p, l, r; }; int N; Node Tr[Lens]; int D[Lens], C[Lens], H[Lens]; bool V[Lens]; //建树 void maketree() { int m, l, r; for (int i = 0; i < N; i++) { cin >> m >> l >> r; Tr[m].l = l; Tr[l].p = m; Tr[m].r = r; Tr[r].p = m; } } //深度 void dns(int i, int p) { D[i] = p; if (Tr[i].l != Nil) { C[i]++; dns(Tr[i].l, p + 1); } if (Tr[i].r != Nil) { C[i]++; dns(Tr[i].r, p + 1); } } int hns(int i) { int h1 = 0, h2 = 0; if (Tr[i].l != Nil) h1 = hns(Tr[i].l)+1; if (Tr[i].r != Nil) h2 = hns(Tr[i].r)+ 1; H[i] = max(h1, h2); return H[i]; } void printtree() { int root; for (int i = 0; i < N; i++) { if (Tr[i].p == Nil) root = i; } dns(root, 0); hns(root); for (int i = 0; i < N; i++) { cout << "node " << i << ": parent = " << Tr[i].p << ", sibling = "; if (i == root) cout << Nil; else if (i == Tr[Tr[i].p].l) cout << Tr[Tr[i].p].r; else cout << Tr[Tr[i].p].l; cout << ", degree = " << C[i] << ", depth = " << D[i] << ", height = " << H[i] << ", "; if (i == root) cout << "root"; else if (i != root&&C[i] != 0) cout << "internal node"; else cout << "leaf"; cout << endl;; } } int main() { cin >> N; for (int i = 0; i < N; i++) { Tr[i].p = Tr[i].l = Tr[i].r = Nil; V[i] = false; } maketree(); printtree(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long N; cin >> N; long long n = N; long long logi = 0; while (n >= 10) { n /= 10; logi++; } long long zahl = 0; for (int i = 0; i < logi; i++) { zahl *= 10; zahl += 9; } N -= zahl; long long QS = 0; string S = to_string(N); for (long long i = 0; i < (long long)S.length(); i++) { QS += S[i] - '0'; } cout << logi * 9 + QS; }
2
#include <bits/stdc++.h> long long mu[20], f[40][20][20]; long next[40][20][20]; char xau[40], kq[40]; int main() { long i, j, k, l; scanf("%ld\n", &l); gets(xau + 1); mu[1] = 1; for (i = 2; i <= l; i++) mu[i] = mu[i - 1] * 10; for (i = 0; i <= 2 * l; i++) for (j = 0; j <= l; j++) for (k = 0; k <= l; k++) f[i][j][k] = -9000000; f[2 * l + 1][0][0] = 0; for (i = 2 * l; i >= 1; i--) for (j = 0; j <= l; j++) for (k = 0; k <= l; k++) { if (j > 0) { f[i][j][k] = f[i + 1][j - 1][k] + mu[j] * (xau[i] - 48); next[i][j][k] = 0; } if (k > 0 && f[i + 1][j][k - 1] + mu[k] * (xau[i] - 48) > f[i][j][k]) { f[i][j][k] = f[i + 1][j][k - 1] + mu[k] * (xau[i] - 48); next[i][j][k] = 1; } } j = k = l; for (i = 1; i <= 2 * l; i++) if (next[i][j][k]) { printf("H"); k--; } else { printf("M"); j--; } printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; struct PT { int x, y; PT(int x = 0, int y = 0) : x(x), y(y) {} void in() { scanf("%d%d", &x, &y); } PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); } PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); } long long operator*(const PT &p) const { return 1LL * x * p.y - 1LL * y * p.x; } }; bool cmp(PT a, PT b) { if (a.y >= 0 && b.y < 0) return 1; if (a.y < 0 && b.y >= 0) return 0; if (a.y == 0 && b.y == 0) { if (a.x >= 0 && b.x < 0) return 1; else return 0; } return a * b > 0; } const int N = 440000; int n; PT A, B[N + N]; int main() { scanf("%d", &n); A.in(); for (int i = 0; i < n; i++) { B[i].in(); B[i] = B[i] - A; } sort(B, B + n, cmp); for (int i = n; i < n + n; i++) B[i] = B[i - n]; long long ans = 0; int j = 0, pr = 0; for (int i = 0; i < n; i++) { if (B[i] * B[pr] != 0 || (B[i] * B[pr] == 0 && 1LL * B[i].x * B[j].x <= 0 && 1LL * B[i].y * B[j].y <= 0)) pr = i; while (B[i] * B[j] >= 0 && j < pr + n) j++; int cnt = j - i; ans += 1LL * (cnt - 1) * (cnt - 2) / 2; } j = 0; while (j < n) { int i = j; while (j < n && B[i] * B[j] == 0 && 1LL * B[i].x * B[j].x >= 0 && 1LL * B[i].y * B[j].y >= 0) ++j; int cnt = j - i; ans += 1LL * cnt * (cnt - 1) * (cnt - 2) / 6; } for (int i = 0; i < n; i++) { if (B[i].x < 0 || (B[i].x == 0 && B[i].y < 0)) B[i] = PT(-B[i].x, -B[i].y); } sort(B, B + n, cmp); int i = 0; while (i < n) { int j = i; while (i < n && B[i] * B[j] == 0) ++i; int cnt = i - j; ans -= 1LL * cnt * (cnt - 1) * (cnt - 2) / 6; } cout << 1LL * n * (n - 1) * (n - 2) / 6 - ans << endl; }
5
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long N; int M; struct Matrix { const static int maxn = 103; long long v[maxn][maxn]; int n; Matrix(int _n) : n(_n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { v[i][j] = 0; } } } Matrix operator*(const Matrix &o) const { Matrix res(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { res.v[i][j] = (res.v[i][j] + v[i][k] * o.v[k][j] + mod) % mod; } } } return res; } static Matrix getIdentity(int n) { Matrix res(n); for (int i = 0; i < n; i++) { res.v[i][i] = 1; } return res; } Matrix operator^(long long k) const { Matrix res = Matrix::getIdentity(n); Matrix a = *this; while (k) { if (k & 1) res = res * a; a = a * a; k /= 2; } return res; } }; int main() { cin >> N >> M; if (N < M) { cout << 1 << endl; return 0; } long long start_vector[M]; Matrix c(M); c.v[0][0] = 1; c.v[0][M - 1] = 1; for (int i = 1; i < M; i++) { c.v[i][i - 1] = 1; } Matrix res = c ^ (N - M + 1); long long sum = 0; for (int i = 0; i < M; i++) { sum = (sum + res.v[0][i] + mod) % mod; } cout << sum << endl; }
4
#include<iostream> #include<cstdio> #include<cassert> #include<set> #include<algorithm> using namespace std; const int N=100005; int n; int a[N],b[N]; pair<int,int> p[N]; int q[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),p[i]=make_pair(a[i],i); sort(p+1,p+n+1,greater<pair<int,int> >()); set<pair<int,int> >S; for(int i=1;i<=n;i++) scanf("%d",&b[i]),q[i]=i,S.insert(make_pair(b[i],i)); int ans=0; for(int i=1;i<=n;i++) { int u=p[i].second; if(a[u]<=b[q[u]]) { S.erase(S.lower_bound(make_pair(b[q[u]],u))); continue; } set<pair<int,int> >::iterator A=S.lower_bound(make_pair(b[q[u]],u)),B=S.lower_bound(make_pair(a[u],0)); if(B==S.end()) { // cerr<<"now"<<u<<" "<<"\n"; printf("No"); return 0; } assert(A!=S.end()); int v=B->second; // cerr<<"swap"<<u<<" "<<v<<"\n"; S.erase(A); S.erase(B); swap(q[u],q[v]); S.insert(make_pair(b[q[v]],v)); ans++; } if(ans<=n-2) printf("Yes"); else printf("No"); // cout<<ans<<"\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long maxAns = 3000000000; long long x, y, n, c, t1, t2, t3, t4; long long mk2(long long x) { if (x <= 0) return 0; return ((x + 1) * x) / 2; } long long mk(long long x) { if (x <= 0) return 0; return x * x; } long long count(long long time) { long long ans = mk(time - t1) + mk(time - t2) + mk(time - t3) + mk(time - t4) - mk2(time - (t1 + t2 + 1)) - mk2(time - (t2 + t3 + 1)) - mk2(time - (t3 + t4 + 1)) - mk2(time - (t4 + t1 + 1)); ans = ((time + 1) * (time)) * 2 + 1 - ans; return ans; } long long bin(long long l, long long r) { while (r - l > 1) { long long m = (l + r) / 2; if (count(m) < c) l = m; else r = m; } return r; } int main() { cin >> n >> x >> y >> c; if (c == 1) { cout << 0; return 0; } t1 = x - 1; t2 = n - y; t3 = n - x; t4 = y - 1; cout << bin(0, maxAns); return 0; }
2
#include <bits/stdc++.h> using namespace std; set<int> s; vector<int> v, r; map<int, int> ans; bool check[2 * int(1e5 + 10)]; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n, m; cin >> n >> m; for (int i = 0; i < n + m; i++) { int x; cin >> x; v.push_back(x); } for (int i = 0; i < n + m; i++) { bool x; cin >> x; check[i] = x; if (x) s.insert(v[i]); } int l = -1; for (int i = 0; i < n + m; i++) { if (check[i]) { l = v[i]; } else { if (s.lower_bound(v[i]) != s.end()) { if (l == -1) ans[*s.lower_bound(v[i])]++; else { if (abs(v[i] - *s.lower_bound(v[i])) < abs(v[i] - l)) ans[*s.lower_bound(v[i])]++; else if (abs(v[i] - *s.lower_bound(v[i])) == abs(v[i] - l)) ans[l]++; else ans[l]++; } } else ans[l]++; } } for (int i = 0; i < n + m; i++) if (check[i]) cout << ans[v[i]] << " "; }
2
#include <bits/stdc++.h> using namespace std; long long dp[1000007]; int main() { string s; cin >> s; long long N = s.size(); long long nxt = 0, idx = 0; for (long long i = (s.size() - 1); i > (-1); i--) { if (s[i] == 'a') { dp[idx++] = nxt; nxt = 0; } else { nxt++; } } long long ans = dp[0]; for (long long i = (1); i < (idx); i++) { dp[i] = (dp[i] + 2 * dp[i - 1]) % 1000000007; ans = (ans + dp[i]) % 1000000007; } printf("%lld\n", ans % 1000000007); }
2
#include <bits/stdc++.h> using namespace std; inline void beg(); inline void fin(); const int N = 2e5 + 5; string t, p; int a[N]; bool check(int num) { string q = t; for (int i = 0; i < num; i++) { q[a[i] - 1] = '#'; } int j = 0; for (int i = 0; i < (int)q.size() && j < (int)p.size(); i++) { if (q[i] == p[j]) j++; } return j == (int)p.size(); } int main() { beg(); cin >> t >> p; for (int i = 0; i < (int)t.size(); i++) cin >> a[i]; int lo = 0, hi = (int)t.size(), mid; while (lo < hi) { mid = lo + (hi - lo + 1) / 2; if (check(mid)) lo = mid; else hi = mid - 1; } cout << lo << endl; fin(); return 0; } inline void beg() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; } inline void fin() {}
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int r, c; char ch[9][9]; for (int i = 0; i < 9; ++i) for (int j = 0; j < 9; ++j) cin >> ch[i][j]; cin >> r >> c; int pr = r - 1 - (r - 1) / 3 * 3, pc = c - 1 - (c - 1) / 3 * 3; int chk = false; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) if (ch[3 * pr + i][3 * pc + j] == '.') ch[3 * pr + i][3 * pc + j] = '!', chk = true; if (!chk) for (int i = 0; i < 9; ++i) for (int j = 0; j < 9; ++j) if (ch[i][j] == '.') ch[i][j] = '!'; for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { cout << ch[i][j]; if (j % 3 == 2) cout << " "; } cout << endl; if (i % 3 == 2) cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, cnt, f[100005], fa[100005]; long long size[100005]; long long ans; struct node { int x, y, v; } a[100005]; bool operator<(node x, node y) { return x.v > y.v; } int find(int x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &f[i]); fa[i] = i; size[i] = 1; } for (int i = 1; i <= m; i++) { scanf("%d%d", &a[i].x, &a[i].y); a[i].v = min(f[a[i].x], f[a[i].y]); } sort(a + 1, a + m + 1); for (int i = 1; i <= m; i++) { int x = find(a[i].x), y = find(a[i].y); if (x != y) { ans += a[i].v * size[x] * size[y]; size[x] += size[y]; fa[y] = x; } } long long tmp = (long long)n * (n - 1) / 2; double ans2 = (double)ans / tmp; printf("%.6lf", ans2); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], i; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); cout << "1" << " " << a[0] << "\n"; if (a[n - 1] > 0) { cout << "1" << " " << a[n - 1] << "\n"; cout << n - 2 << " "; for (i = 1; i < n - 1; i++) { cout << a[i] << " "; } } else { cout << "2" << " " << a[1] << " " << a[2] << "\n"; cout << n - 3 << " "; for (i = 3; i < n; i++) { cout << a[i] << " "; } } }
1
#include <bits/stdc++.h> using namespace std; typedef double D; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PII pair <int, int> const int N = 1e6 + 7; const int MX = 1e9 + 7; const LL INF = 1LL * MX * MX; int n; char in[N]; int limit[N]; int check(int v){ int zeroes = 0, ones = 0, mx = 0; for(int i = 1; i <= n; ++i){ char c = in[i]; if(c == '0'){ ++zeroes; } else if(c == '1'){ ++ones; mx = max(mx, ones - zeroes); } else{ if(zeroes - ones + limit[i] + 1 <= v) ++zeroes; else{ ++ones; mx = max(mx, ones - zeroes); } } } return mx + v; } int main(){ scanf("%s", in + 1); n = strlen(in + 1); int mx = 0; for(int i = n; i >= 1; --i){ limit[i] = mx; if(in[i] == '0'){ ++mx; } else{ --mx; if(mx < 0) mx = 0; } } int ans = N; int s = mx, e = n; while(e - s >= 5){ int mid = (s + e) / 2; int Left = min(check(mid - 1), check(mid)); int Right = min(check(mid + 1), check(mid + 2)); ans = min(ans, min(Left, Right)); if(Left < Right) e = mid; else s = mid + 1; } for(int i = s; i <= e; ++i) ans = min(ans, check(i)); printf("%d\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 65; bool f[N][N][N * N], g[N][N][N * N], ans[N][N]; int a0[N], d[N], m, n = -1; void dfs(int i, int j, int k) { if (!j) return; d[j] = a0[i]; if (g[i][j][k]) dfs(i - 1, j - 1, k - a0[i]); else dfs(i, j - 1, k - a0[i]); } int p[N]; bool cmp(int a, int b) { return d[a] < d[b]; } int main() { scanf("%d", &m); for (register int i = 1; i <= m; ++i) scanf("%d", &a0[i]); sort(a0 + 1, a0 + 1 + m); f[0][0][0] = true; for (int i = 0; i <= m; i++) for (register int j = 0; j <= 62; ++j) for (register int k = 0; k <= j * 30; ++k) if (f[i][j][k]) { if (i && k + a0[i] >= (j + 1) * j / 2) f[i][j + 1][k + a0[i]] = true; if (i != m && k + a0[i + 1] >= (j + 1) * j / 2) { f[i + 1][j + 1][k + a0[i + 1]] = g[i + 1][j + 1][k + a0[i + 1]] = true; } } for (register int i = 1; i <= 62; ++i) if (f[m][i][i * (i - 1) / 2]) { n = i; break; } if (n == -1) { printf("=(\n"); return 0; } printf("%d\n", n); dfs(m, n, n * (n - 1) / 2); for (register int i = 1; i <= n; ++i) p[i] = i; for (register int i = 1; i <= n; ++i) { sort(p + i + 1, p + n + 1, cmp); for (int j = i + 1; j <= i + d[p[i]]; j++) ans[p[i]][p[j]] = true; for (int j = i + d[p[i]] + 1; j <= n; j++) d[p[j]]--, ans[p[j]][p[i]] = true; } for (register int i = 1; i <= n; ++i) { for (register int j = 1; j <= n; ++j) printf("%d", ans[i][j] ? 1 : 0); printf("\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int tc; scanf("%d", &tc); while (tc--) { int a, b, x, y; scanf("%d%d%d%d", &a, &b, &x, &y); int d[5], maxN = -1; d[1] = x * b, d[2] = max((a - x - 1), 0) * b, d[3] = a * y, d[4] = a * max((b - y - 1), 0); for (int i = 1; i <= 4; i++) { if (d[i] > maxN) maxN = d[i]; } printf("%d\n", maxN); } return 0; }
1
#include <bits/stdc++.h> using namespace std; class doi { public: void endline(char edl) { _edl = edl; } template <typename T> void o_ans(T ans[], int a, int b) { for (int i = a; i < b; i++) cout << ans[i] << _edl; } template <typename T> void o_bol(T ans[], int a, int b) { for (int i = a; i < b; i++) { if (ans[i]) cout << "YES" << _edl; else cout << "NO" << _edl; } } template <typename T> void i_get(T arr[], int a, int b) { for (int i = a; i < b; i++) cin >> arr[i]; } template <typename T> void d_reval(T arr[], T val, int a, int b) { for (int i = a; i < b; i++) arr[i] = val; } private: char _edl = '\n'; } doi; class alg { public: int find_max(int arr[], int a, int b) { int tmp = -10000, ans = a; for (int i = a; i < b; i++) { if (arr[i] > tmp) { tmp = arr[i]; ans = i; } } return ans; } int find_min(int arr[], int a, int b) { int tmp = 0xffff, ans = a; for (int i = a; i < b; i++) { if (arr[i] < tmp) { tmp = arr[i]; ans = i; } } return ans; } int find_nchar(string str, char c) { int tmp = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == c) { tmp++; } } return tmp; } string conv_lowcase(string str) { for (int i = 0; i < str.size(); i++) { if (str[i] < 96) str[i] += 32; } return str; } string conv_upcase(string str) { for (int i = 0; i < str.size(); i++) { if (str[i] > 96) str[i] -= 32; } return str; } template <typename T> void invert(T str[], int a) { int n = a / 2; for (int i = 0; i <= n; i++) swap(str[i], str[a - 1 - i]); } string invert(string str) { int n = str.size() / 2; for (int i = 0; i <= n; i++) swap(str[i], str[str.size() - 1 - i]); return str; } int base(unsigned long long int n, int k) { long long int tmp = 0; while (n >= k) { n /= k; tmp++; } return tmp; } } alg; int main() { ios::sync_with_stdio(false); cin.tie(0); ; int n = 0, m = 0, k = 0, t = 0; doi.endline(' '); string str; cin >> t; while (t--) { cin >> str; int ans[n], tmp = 0, st = 1, po = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == 'L') { po++; if (i == str.size() - 1) { st = 0; if (po + 1 >= tmp) tmp = po + 1; po = 0; } } else { st = 0; if (po + 1 >= tmp) tmp = po + 1; po = 0; } } if (st == 1) tmp = str.size() + 1; if (tmp == 0) tmp++; cout << tmp << endl; } return 0; }
3
#include <iostream> #include <string> #include <cmath> #include <vector> #include <algorithm> #include <utility> #include <bitset> #include <climits> #include <set> #include <map> #include <iomanip> #include <queue> #include <cstring> using namespace std; #define ll long long #define INF LONG_LONG_MAX #define pll pair<ll,ll> #define F first #define S second #define mp make_pair #define pb push_back using namespace std; const ll N=998244353; void extgcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1; y = 0; return; } else { extgcd(b, a % b, x, y); ll nx = y; ll ny = x - (a / b) * y; x = nx; y = ny; return; } } int main(){ std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); ll t; cin >> t; while(t--){ ll n; string s; cin >> n; cin >> s; ll start=0,end=n-1; while(start<n && s[start]!='0') start++; while(end>-1 && s[end]!='0') end--; start%=2; if((n-1-end)%2==1) end=n-2; else end=n-1; ll cntz=0,cnt=0; for(int i=start;i<=end;i++){ if(s[i]=='0') cntz++; if(s[i]=='1' && s[i+1]=='1'){ cnt++; i++; } } //cout << cntz << " " << cnt << endl; ll ans=1; for(int i=cntz+1;i<=cnt+cntz;i++){ ans*=i; ans%=N; } for(int i=1;i<=cnt;i++){ ll x,y; extgcd(i, N, x, y); if(x<0) x+=N; ans*=x; ans%=N; } cout << ans << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5, SQRT = 320; struct query { long long l, r, id; bool operator<(const query& rem) const { if (l / SQRT != rem.l / SQRT) return l / SQRT < rem.l / SQRT; else return r < rem.r; } } que[N]; long long n, K, q, t[N], a[N], up[N], down[N], l = 0, r = -1, ans[N], cnt[N], res; vector<long long> vec; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> K; for (long long i = 1; i <= n; ++i) cin >> t[i]; for (long long i = 1; i <= n; ++i) { long long _a; cin >> _a; a[i] = a[i - 1] + _a - 2 * (t[i] == 2) * _a; vec.push_back(a[i]); } vec.push_back(0); sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); for (long long i = 0; i <= n; ++i) { long long pos; pos = lower_bound(vec.begin(), vec.end(), a[i] - K) - vec.begin(); if (pos == vec.size() || vec[pos] != a[i] - K) down[i] = -1; else down[i] = pos; pos = lower_bound(vec.begin(), vec.end(), a[i] + K) - vec.begin(); if (pos == vec.size() || vec[pos] != a[i] + K) up[i] = -1; else up[i] = pos; pos = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin(); a[i] = pos; } cin >> q; for (long long i = 1; i <= q; ++i) cin >> que[i].l >> que[i].r, que[i].id = i; sort(que + 1, que + q + 1); for (long long i = 1; i <= q; ++i) { long long L = que[i].l, R = que[i].r, ID = que[i].id; L--; if (r < L && l <= r) { res = 0; while (l <= r) cnt[a[l]]--, l++; l = L - 1; r = L - 2; } while (l < L) { cnt[a[l]]--; if (up[l] != -1) res -= cnt[up[l]]; l++; } while (R < r) { cnt[a[r]]--; if (down[r] != -1) res -= cnt[down[r]]; r--; } while (L < l) { l--; if (up[l] != -1) res += cnt[up[l]]; cnt[a[l]]++; } while (r < R) { r++; if (down[r] != -1) res += cnt[down[r]]; cnt[a[r]]++; } ans[ID] = res; } for (long long i = 1; i <= q; ++i) cout << ans[i] << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const int md = 1e9 + 7; const int N = 100 + 10; const int M = 1e3 + 10; int n, m; int T; int ar[N]; double dp[N][M * N]; double sum[N][M * N]; int main() { int al = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &ar[i]), al += ar[i]; sum[0][0] = dp[0][0] = 1; for (int j = 0; j <= m * n; j++) sum[0][j] = 1; for (int i = 1; i <= n; i++) { for (int j = i; j <= m * n; j++) { int t = max(j - m, 0); double p = sum[i - 1][j - 1]; if (t) p -= sum[i - 1][t - 1]; if (t <= j - ar[i] && j - ar[i] <= j - 1) p -= dp[i - 1][j - ar[i]]; dp[i][j] = p / (m - 1); sum[i][j] = sum[i][j - 1] + dp[i][j]; } } double ans = 0; for (int i = 0; i < al; i++) ans += dp[n][i]; ans *= m - 1; ans += 1; printf("%.11lf\n", ans); return 0; }
5
#include<cstdio> int main() { int a,b,c,i; scanf("%d%d%d",&a,&b,&c); for(i=b;i<=c;i++) if(i%a==0) break; if(i>c) printf("NG"); else printf("OK"); }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, x, tmp, count = 0, i = 0; map<int, int> m; cin >> n >> x; for (int j = 0; j < n; j++) { cin >> tmp; m[tmp] = 1; } for (; i < x; ++i) { if (i < x && m.find(i) == m.end()) { count++; } } if (i == x && m.find(i) != m.end()) { count++; } cout << count; return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> dp_in, dp_out; vector<vector<pair<int, int>>> g; vector<pair<int, int>> color; void dfs1(int u, int p) { dp_in[u] = color[u]; int v; for (auto& row : g[u]) { v = row.first; if (v ^ p) { dfs1(v, u); if ((dp_in[u].first - dp_in[u].second) < (dp_in[u].first - dp_in[u].second + dp_in[v].first - dp_in[v].second)) { dp_in[u].first += dp_in[v].first; dp_in[u].second += dp_in[v].second; row.second = 1; } } } } void dfs2(int u, int p) { int v; for (auto& row : g[u]) { v = row.first; if (v ^ p) { dp_out[v] = color[v]; auto t = dp_in[u]; if (row.second) { t.first -= dp_in[v].first; t.second -= dp_in[v].second; } if ((dp_out[v].first - dp_out[v].second) < (dp_out[v].first - dp_out[v].second + t.first - t.second + dp_out[u].first - dp_out[u].second - color[u].first + color[u].second)) { dp_out[v].first += t.first + dp_out[u].first - color[u].first; dp_out[v].second += t.second + dp_out[u].second - color[u].second; } dfs2(v, u); } } } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n; cin >> n; color = vector<pair<int, int>>(n, {0, 0}); int x, y; for (int i = 0; i < n; ++i) { cin >> x; if (x) color[i].first = 1; else color[i].second = 1; } g = vector<vector<pair<int, int>>>(n, vector<pair<int, int>>()); for (int i = 1; i < n; ++i) { cin >> x >> y; g[x - 1].emplace_back(y - 1, 0); g[y - 1].emplace_back(x - 1, 0); } dp_in = vector<pair<int, int>>(n + 1, {0, 0}); dp_out = vector<pair<int, int>>(n + 1, {0, 0}); dfs1(0, -1); dp_out[0] = color[0]; dfs2(0, -1); int result; for (int i = 0; i < n; ++i) { auto t = color[i]; result = max({dp_in[i].first - dp_in[i].second, dp_out[i].first - dp_out[i].second, dp_in[i].first - dp_in[i].second + dp_out[i].first - dp_out[i].second - t.first + t.second}); cout << result << " "; } return 0; }
6
#include<iostream> int main (void){ int k,x; scanf("%d",&k); scanf("%d",&x); for(int i=x-k+1;i<=x+k-1;i++) { printf("%d ",i); } }
0
#include <bits/stdc++.h> using namespace std; long long n, k, a, b, q, binary[2][1000000], no[1000000]; void update(long long s, long long k, long long l) { for (long long i = s; i <= n; i += i & -i) binary[l][i] += k; } long long query(long long s, long long l) { long long ans = 0; for (long long i = s; i > 0; i -= i & -i) ans += binary[l][i]; return ans; } int main() { cin >> n >> k >> a >> b >> q; for (long long i = 0; i < q; i++) { long long x; cin >> x; if (x == 1) { long long d, hi; cin >> d >> hi; long long nul = 0; update(d, min(max(b - no[d], nul), hi), 0); update(d, min(max(a - no[d], nul), hi), 1); no[d] += hi; } else { long long p; cin >> p; long long fin = query(p - 1, 0) + query(n, 1) - query(p + k - 1, 1); cout << fin << '\n'; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1000005; string s; int R[N]; int k, n; bool p(int pos, int x, int l) { if (l > k) return false; if (pos == n) return true; int y = pos; int in; while (y - pos <= x) { in = y; y = R[y]; } return p(in, x, l + 1); } int main() { cin >> k; cin.ignore(); getline(cin, s); n = s.size(); R[n] = 1000000000; R[n - 1] = n; for (int i = n - 2; i >= 0; --i) if (s[i] == ' ' or s[i] == '-') R[i] = i + 1; else R[i] = R[i + 1]; int lo = 1, hi = n; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (not p(0, mid, 0)) lo = mid + 1; else hi = mid; } cout << lo << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const short maxN = 400 + 2; short N, K; vector<pair<short, short> > adj[maxN]; vector<short> dp[maxN]; vector<short> fat; short rec[maxN][maxN][maxN]; vector<short> final; void dfs(short u, short pre) { dp[u] = vector<short>(K + 1, 0x03ff); dp[u][1] = 0; fat[u] = pre; for (short i = 0; i < (short)adj[u].size(); i++) { short v = adj[u][i].first; if (v == pre) continue; dfs(v, u); for (short j = K; j >= 1; j--) { dp[u][j]++; rec[u][v][j] = 0; for (short k = 1; k < j; k++) { short w = dp[u][j - k] + dp[v][k]; if (dp[u][j] > w) { dp[u][j] = w; rec[u][v][j] = k; } } } } } void get_final(short u, short pre, short t) { for (short i = (short)adj[u].size() - 1; i >= 0; i--) { short v = adj[u][i].first; short d = adj[u][i].second; if (v == pre) continue; if (rec[u][v][t]) { get_final(v, u, rec[u][v][t]); t -= rec[u][v][t]; } else final.push_back(d); } } int main() { cin >> N >> K; short u, v; pair<short, short> o; for (short i = 1; i < N; i++) { cin >> u >> v; o.first = v, o.second = i; adj[u].push_back(o); o.first = u, o.second = i; adj[v].push_back(o); } for (short i = 1; i <= N; i++) dp[i].clear(); fat = vector<short>(N + 1, -1); dfs(1, -1); short res = dp[1][K], rt = 1; for (short i = 2; i <= N; i++) if (res > dp[i][K] + 1) { res = dp[i][K] + 1; rt = i; } cout << res << endl; final.clear(); for (short j = 0; j < (short)adj[rt].size(); j++) if (fat[rt] == adj[rt][j].first) { final.push_back(adj[rt][j].second); break; } get_final(rt, fat[rt], K); for (short i = 0; i < (short) final.size(); i++) { if (i) cout << " "; cout << final[i]; } cout << endl; return 0; }
4
#include<stdio.h> int main(void) { int n,s,ss,i; scanf("%d",&n); s=100000; for(i=0;i<n;i++){ ss=s*0.05; s+=ss; if(s%1000!=0){ s=s/1000; s=(s+1)*1000; } } printf("%d\n",s); return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<pair<int, bool> > enemy; vector<int> my; int n, m; bool comp(const pair<int, bool> &a, const pair<int, bool> &b) { return a.second > b.second || ((a.second == b.second) && (a.first < b.first)); } int main(int argc, char **argv) { scanf("%d %d\n", &n, &m); for (int i = 0; i < n; i++) { char type; int str; scanf("%c%*c%*c %d\n", &type, &str); enemy.push_back(make_pair(str, type == 'A')); } for (int i = 0; i < m; i++) { int str; scanf("%d\n", &str); my.push_back(str); } int ans = 0; multiset<int> smy; multiset<pair<bool, int> > sen; for (int i = 0; i < n; i++) sen.insert(make_pair(enemy[i].second, enemy[i].first)); for (int i = 0; i < m; i++) smy.insert(my[i]); int curr_ans = 0; while (!sen.empty()) { int need = sen.begin()->second; bool type = sen.begin()->first; set<int>::iterator it; if (type) it = smy.lower_bound(need); else it = smy.upper_bound(need); if (it != smy.end()) { if (type) curr_ans += *it - need; smy.erase(it); set<pair<bool, int> >::iterator it2 = sen.find(*sen.begin()); sen.erase(it2); } else { break; } } if (sen.empty()) { for (set<int>::iterator it = smy.begin(); it != smy.end(); it++) curr_ans += *it; ans = curr_ans; } sen.clear(); smy.clear(); curr_ans = 0; sort(my.begin(), my.end()); sort(enemy.begin(), enemy.end(), comp); int j = 0; int cnt = 0; int i; for (i = int(my.size()) - 1; i >= 0 && j < enemy.size(); i--) { bool good = false; while (j < enemy.size()) { if (enemy[j].second) { if (my[i] >= enemy[j].first) { curr_ans += my[i] - enemy[j].first; j++; cnt++; good = true; break; } else j++; } else { if (my[i] > enemy[j].first) { j++; cnt++; good = true; break; } else j++; } } if (!good) break; } if (cnt == n) { for (; i >= 0; i--) { curr_ans += my[i]; } } ans = max(ans, curr_ans); cout << ans; return 0; }
2
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int>pii; const int mn=200+10; const ll inf=0x3f3f3f3f3f3f3f3f; const ll mod=1e9+7; ll basis[64]; ll a[mn]; bool add(ll x){ for(int i=60;i>=0;i--)if((x>>i)&1){ if(basis[i])x^=basis[i]; else{ basis[i]=x; return 1; } } return 0; } int main(){ cin.tie(0); cin.sync_with_stdio(0); int tc; cin>>tc; while(tc--){ memset(basis,0,sizeof(basis)); int n; cin>>n; for(int i=0;i<n;i++)cin>>a[i]; string s; cin>>s; bool f=0; for(int i=n-1;i>=0;i--){ if(s[i]-'0'){ if(add(a[i])){ f=1; } } else add(a[i]); } printf("%d\n",(int)f); } }
0
#include<iostream> #include<algorithm> using namespace std; long n,h,a[1<<17],b[1<<17]; int main() { cin>>n>>h; for(int i=0;i<n;i++)cin>>a[i]>>b[i]; sort(a,a+n);sort(b,b+n); long cnt=0; for(int i=n;i--;) { if(b[i]<a[n-1])break; cnt++; h-=b[i]; if(h<=0)break; } cout<<(cnt+(h>0?(h+a[n-1]-1)/a[n-1]:0))<<endl; }
0
#include<iostream> #include<algorithm> using namespace std; int main(){ int n, a[100], sum=0; cin>>n; for(int i=0;i<n;i++) cin>>a[i]; sort(a, a+n, greater<int>()); for(int i=0;i<n;i++){ if(i%2==0) sum+=a[i]; else sum-=a[i]; } cout<<sum<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; unsigned long long gcd(unsigned long long a, unsigned long long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b % a, a); } unsigned long long lcm(unsigned long long a, unsigned long long b) { return (a * b) / gcd(a, b); } unsigned long long nCr(unsigned long long n, unsigned long long r) { unsigned long long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; unsigned long long m = gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p; } long long power(long long b, long long p, long long m) { if (!p) return 1; if (p % 2) return (b * power(b, p - 1, m)) % m; unsigned long long x = power(b, p / 2, m); return (x * x) % m; } struct dsu { int n; vector<int> rank; vector<int> parent; vector<int> sz; dsu(int x) { n = x; for (int i = 0; i <= n; i++) { parent.push_back(i); rank.push_back(1); sz.push_back(1); } } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } void link(int x, int y) { int px = find(x), py = find(y); if (px == py) return; if (rank[px] > rank[py]) { parent[py] = px; sz[px] += sz[py]; } else if (rank[py] > rank[px]) { parent[px] = py; sz[py] += sz[px]; } else { rank[px]++; sz[px] += sz[py]; parent[py] = px; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long n; cin >> n; if (n % 4 == 0) cout << "YES"; else cout << "NO"; cout << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; long long factorial = 1; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long ans = 0; ans += n / 1 * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5; ans += n / 1 * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5 * (n - 5) / 6; ans += n / 1 * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5 * (n - 5) / 6 * (n - 6) / 7; cout << ans << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; if ((d - c == c - b) && (c - b == b - a)) { cout << d + (d - c); } else if (((c * c == b * d) && (b * b == c * a)) && ((d * d) % c == 0)) { cout << (d * d) / c; } else cout << "42"; return 0; }
1
#include <bits/stdc++.h> using namespace std; inline int S() { int ret = 0, neg = 0; char c; for (c = getchar(); c < '0' || c > '9'; c = getchar()) if (c == '-') neg = 1; for (; c >= '0' && c <= '9'; c = getchar()) ret = (ret << 3) + (ret << 1) + c - '0'; return neg ? -ret : ret; } const int N = 3e5 + 10; int n, k; int a[N], b[N]; long long dp[2][5010][2], sum[N]; long long cal(int st, int ed) { long long ret = 0; for (int i = st + 1; i <= ed; ++i) { ret += a[i] - a[i - 1]; } return ret; } int main() { n = S(); k = S(); for (int i = 1; i <= n; ++i) { a[i] = S(); } sort(a + 1, a + 1 + n); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 5010; ++j) { for (int k = 0; k < 2; ++k) { dp[i][j][k] = 1LL << 60; } } } if (n % k) { int b = n / k, a = b + 1, ta = n % k, tb = k - ta; dp[1][0][1] = cal(1, b); dp[1][1][0] = cal(1, a); for (int i = 2; i <= k; ++i) { for (int j = 0; j <= ta; ++j) { if (i < j) { break; } int curI = i & 1, lastI = curI ^ 1; int st = a * (j - 1) + b * (i - j) + 1; int ed = st + a - 1; long long val = cal(st, ed); if (j) { dp[curI][j][0] = min(dp[lastI][j - 1][0], dp[lastI][j - 1][1]) + val; } st = a * j + b * (i - j - 1) + 1; ed = st + b - 1; long long val1 = cal(st, ed); dp[curI][j][1] = min(dp[lastI][j][0], dp[lastI][j][1]) + val1; } } long long ans = min(dp[k & 1][ta][0], dp[k & 1][ta][1]); cout << ans << endl; } else { int cnt = 0, group = 1, pos = 1; while (1) { if (cnt == n) { break; } if (pos <= n) { b[pos] = a[++cnt]; pos += k; } else { group++; pos = group; } } long long ans = 0; for (int i = k + 1; i <= n; ++i) { ans += abs(b[i] - b[i - k]); } cout << ans << endl; } return 0; }
2
#include <iostream> using namespace std; int main(){ int A,B,C=0; cin>>A>>B; int D[A]; for(int i=0;i<A;i++){ cin>>D[i]; if(D[i]>=B) C++; } cout<<C; return 0; }
0
#include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const int INF = 1 << 29; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; int mulmd(long long a, long long b) { long long ret = (a * b) % 1000000007; return (int)ret; } inline int power(long long x, long long y, int p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = mulmd(res, x); y = y >> 1; x = mulmd(x, x); } return (int)res; } int submd(long long a, long long b) { long long ret = a - b; if (ret < 0) { ret += 1000000007; } return (int)ret; } int addmd(long long a, long long b) { long long ret = (a + b) % 1000000007; return (int)ret; } int phi(int n) { int result = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) { n = n / i; } result -= result / i; } } if (n > 1) result -= result / n; return result; } int gcd(int a, int b, int& x, int& y) { if (a == 0) { x = 0; y = 1; return b; } int x1, y1; int d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } vector<int> adj[100005]; int c[100005]; bool visited[100005]; set<int> ind; int dfs(int v) { visited[v] = true; int cnt = 0; for (int u : adj[v]) { if (!visited[u]) { cnt += dfs(u); } } if (cnt == adj[v].size() - 1 && c[v - 1] == 1) ind.insert(v); if (c[v - 1] == 1) return 1; else return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; memset(visited, false, sizeof visited); int root = -1; for (int i = 0; i < (n); i++) { int x; cin >> x >> c[i]; if (x == -1) { root = i + 1; continue; } adj[i + 1].push_back(x); adj[x].push_back(i + 1); } dfs(root); if (ind.size() == 0) { cout << -1; return 0; } for (auto it : ind) { cout << it << " "; } }
3
#include<iostream> #include<cstdio> #include<complex> #include<vector> using namespace std; #define X real() #define Y imag() typedef long double D; typedef complex<D> P; struct L{P a, b;}; const D EPS = 1e-8; int sig(D a, D b = 0) {return a < b - EPS ? -1 : a > b + EPS ? 1 : 0;} bool near(P a, P b) {return !sig(norm(a - b));} namespace std { bool operator<(P a, P b) {return sig(a.X, b.X) ? a.X < b.X : a.Y < b.Y;} } D sr(D a) {return sqrt(max(a, (D)0));} D dot(P a, P b) {return a.X * b.X + a.Y * b.Y;} D det(P a, P b) {return a.X * b.Y - a.Y * b.X;} P vec(L a) {return a.b - a.a;} enum CCW{FRONT = 1, RIGHT = 2, BACK = 4, LEFT = 8, ON = 16}; int ccw(P a, P b, P c) { if (near(a, c) || near(b, c)) return ON; int s = sig(det(b - a, c - a)); if (s) return s > 0 ? LEFT : RIGHT; return (a < b) == (b < c) ? FRONT : (c < a) == (a < b) ? BACK : ON; } P proj(P a, P b) {return a * dot(a, b) / norm(a);} P perp(L l, P p) {return l.a + proj(vec(l), p - l.a);} bool iLLs(L a, L b) {return sig(det(vec(a), vec(b)));} P pLL(L a, L b) {return a.a + vec(a) * (det(vec(b), b.a - a.a) / det(vec(b), vec(a)));} D dLP(L l, P p) {return abs(det(vec(l), p - l.a) / vec(l));} D dSP(L s, P p) { if (dot(vec(s), p - s.a) < 0) return abs(p - s.a); if (dot(vec(s), p - s.b) > 0) return abs(p - s.b); return dLP(s, p); } struct C{P c; D r;}; bool iCSs(C c, L s) {return sig(c.r, dSP(s, c.c)) > 0;} pair<P, P> pCL(C c, L l) { P x = perp(l, c.c); P y = vec(l) / abs(vec(l)) * sr(c.r * c.r - norm(x - c.c)); return make_pair(x - y, x + y); } pair<P, P> tCP(C c, P p) { D d2 = norm(p - c.c); D x = sqrt(max(d2 - c.r * c.r, (D)0)); P h = c.c + (p - c.c) * (c.r * c.r / d2); P w = (p - c.c) * (x * c.r / d2) * P(0, 1); return make_pair(h - w, h + w); } #define rep(i, n) for (int i = 0; i < int(n); ++i) int main() { int n, d; cin >> n >> d; C c[n]; rep (i, n) { D x, y, r; cin >> x >> y >> r; c[i] = (C){P(x, y), r}; } L l; l.a = P(25, 0); l.b = P(25, 94); int cnt = 0; rep (i, n) if (iCSs(c[i], l)) ++cnt; if (cnt <= d) { puts("94"); return 0; } vector<L> vl1, vl2; rep (i, n) { pair<P, P> pp = tCP(c[i], l.a); vl1.push_back((L){l.a, pp.first}); vl1.push_back((L){l.a, pp.second}); } rep (i, n) { pair<P, P> pp = tCP(c[i], l.b); vl2.push_back((L){l.b, pp.first}); vl2.push_back((L){l.b, pp.second}); } D res = 1000; rep (i, vl1.size()) rep (j, vl2.size()) if (iLLs(vl1[i], vl2[j])) { P p = pLL(vl1[i], vl2[j]); if (sig(p.X) < 0 || sig(p.X, 50) > 0 || sig(p.Y) < 0 || sig(p.Y, 94) > 0) continue; cnt = 0; rep (i, n) if (iCSs(c[i], (L){l.a, p})) { pair<P, P> pp = pCL(c[i], (L){l.a, p}); if (ccw(l.a, p, pp.first) == ON) ++cnt; if (ccw(l.a, p, pp.second) == ON) ++cnt; } rep (i, n) if (iCSs(c[i], (L){l.b, p})) { pair<P, P> pp = pCL(c[i], (L){l.b, p}); if (ccw(l.b, p, pp.first) == ON) ++cnt; if (ccw(l.b, p, pp.second) == ON) ++cnt; } if (cnt / 2 <= d) { res = min(res, abs(l.a - p) + abs(l.b - p)); } } if (sig(res, 1000) == 0) { puts("-1"); } else { printf("%.12Lf\n", res); } }
0
#include <bits/stdc++.h> using namespace std; const int MAX = 5001; struct node { pair<int, int> l, r, u, d; node() {} void assign(int id, pair<int, int> hh) { if (id == 0) l = hh; if (id == 1) r = hh; if (id == 2) u = hh; if (id == 3) d = hh; } }; vector<vector<node> > bb; vector<vector<node> > xi; char mm[MAX][MAX]; vector<vector<int> > ans; int n, m; int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; inline bool core(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } int main() { scanf("%d%d", &n, &m); bb.resize(n), xi.resize(n), ans.resize(n); for (int i = 0; i < n; i++) bb[i].resize(m), xi[i].resize(m), ans[i].resize(m); for (int i = 0; i < n; i++) scanf("%s", mm[i]); for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) { if (mm[r][c] == '.') continue; for (int d = 0; d < 4; d++) { for (int k = 1; k <= max(n, m); k++) { int x1 = r + k * dx[d], y1 = c + k * dy[d]; if (!core(x1, y1)) { xi[r][c].assign(d, {-1, -1}); break; } else { if (mm[x1][y1] != '.') { xi[r][c].assign(d, {x1, y1}); break; } } } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mm[i][j] == '.') continue; for (int r = 0; r < n; r++) { for (int c = 0; c < m; c++) bb[r][c] = xi[r][c]; } int cnt = 0; int rs = i, cs = j; while (rs != -1) { cnt++; pair<int, int> l = bb[rs][cs].l, r = bb[rs][cs].r, u = bb[rs][cs].u, d = bb[rs][cs].d; if (l.first != -1) { bb[l.first][l.second].r = r; } if (r.first != -1) { bb[r.first][r.second].l = l; } if (u.first != -1) { bb[u.first][u.second].d = d; } if (d.first != -1) { bb[d.first][d.second].u = u; } if (mm[rs][cs] == 'L') { pair<int, int> l = bb[rs][cs].l; rs = l.first, cs = l.second; } else if (mm[rs][cs] == 'R') { pair<int, int> l = bb[rs][cs].r; rs = l.first, cs = l.second; } else if (mm[rs][cs] == 'D') { pair<int, int> l = bb[rs][cs].d; rs = l.first, cs = l.second; } else if (mm[rs][cs] == 'U') { pair<int, int> l = bb[rs][cs].u; rs = l.first, cs = l.second; } } ans[i][j] = cnt; } } int mx = INT_MIN; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mm[i][j] == '.') continue; mx = max(mx, ans[i][j]); } } int cc = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mm[i][j] == '.') continue; if (ans[i][j] == mx) cc++; } } printf("%d %d\n", mx, cc); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int Max_num_names = 110; int num_names; string names[Max_num_names]; const int Max_N = 40; int n = 26; vector<int> graph[Max_N]; vector<int> ans; bool visited[Max_N]; void dfs(int curr) { visited[curr] = true; for (int i = 0; i < graph[curr].size(); i++) { if (!visited[graph[curr][i]]) { dfs(graph[curr][i]); } } ans.push_back(curr); return; } void top_sort() { for (int i = 0; i < n; i++) { if (!visited[i]) { dfs(i); } } reverse(ans.begin(), ans.end()); } bool is_loop = false; int col[Max_N]; void sec_dfs(int curr) { col[curr] = 1; for (int i = 0; i < graph[curr].size(); i++) { if (col[graph[curr][i]] == 0) { sec_dfs(graph[curr][i]); } if (col[graph[curr][i]] == 1) { is_loop = true; return; } } col[curr] = 2; return; } void check_cycle() { for (int i = 0; i < n; i++) { if (!is_loop) { sec_dfs(i); } else { return; } } } int lett_to_int(char a) { return (((int)a) - 97); } int main() { ios_base::sync_with_stdio(false); cin >> num_names; for (int i = 0; i < num_names; i++) { cin >> names[i]; } for (int i = 0; i < num_names - 1; i++) { for (int j = 0; j < min(names[i].length(), names[i + 1].length()); j++) { if (names[i].at(j) != names[i + 1].at(j)) { graph[lett_to_int(names[i].at(j))].push_back( lett_to_int(names[i + 1].at(j))); goto end_first_for; } } if (names[i + 1].length() < names[i].length()) { cout << "Impossible"; return 0; } end_first_for:; } check_cycle(); if (is_loop) { cout << "Impossible"; return 0; } top_sort(); for (int i = 0; i < ans.size(); i++) { cout << (char)(ans[i] + 97); } return 0; }
3
#include <bits/stdc++.h> using namespace std; void qmax(int &x, int y) { if (x < y) x = y; } void qmin(int &x, int y) { if (x > y) x = y; } inline int read() { char s; int k = 0, base = 1; while ((s = getchar()) != '-' && s != EOF && !(isdigit(s))) ; if (s == EOF) exit(0); if (s == '-') base = -1, s = getchar(); while (isdigit(s)) k = k * 10 + (s ^ '0'), s = getchar(); return k * base; } inline void write(long long x) { static char cnt, num[20]; cnt = 0; if (!x) { putchar('0'); return; } for (; x; x /= 10) num[++cnt] = x % 10; for (; cnt; putchar(num[cnt--] + 48)) ; } const long long mod = 1e9 + 7; const int maxn = 1e5 + 100; char ch[maxn]; int n, q; int cnt[52]; long long dp[maxn], ny, jie; long long f[2][maxn], ans[52][52]; int Map[129]; void Mod(long long &x) { if (x < 0) x += mod; } void Mod1(long long &x) { if (x >= mod) x -= mod; } long long ksm(long long x, int y) { long long sum = 1; while (y) { if (y & 1) sum = sum * x % mod; x = x * x % mod; y >>= 1; } return sum; } long long qwq[maxn]; int main() { scanf("%s", ch + 1); n = strlen(ch + 1); for (char i = 'a'; i <= 'z'; i++) Map[i] = i - 'a'; for (char i = 'A'; i <= 'Z'; i++) Map[i] = i - 'A' + 26; qwq[0] = 1; for (int i = 1; i <= n; i++) { qwq[i] = qwq[i - 1] * i % mod; cnt[Map[ch[i]]]++; } jie = 1; for (int i = 1; i + i <= n; i++) jie = jie * i % mod; dp[0] = 1; ny = 1; for (int i = 0; i < 52; i++) { if (!cnt[i]) continue; ny = ny * qwq[cnt[i]] % mod; for (int j = n; j >= cnt[i]; j--) Mod1(dp[j] += dp[j - cnt[i]]); } ny = ksm(ny, mod - 2); int X, Y, m = n / 2; int flag = 1, p; for (int x = 0; x < 52; x++) { if (m - cnt[x] < 0) { flag = 0; break; } if (!cnt[x]) continue; memcpy(f[0], dp, sizeof(f[0])); for (int i = cnt[x]; i <= n; i++) { Mod(f[0][i] -= f[0][i - cnt[x]]); } ans[x][x] = 2 * f[0][m - cnt[x]] % mod * jie % mod * jie % mod * ny % mod; for (int y = x + 1; y < 52; y++) { X = cnt[x], Y = cnt[y]; if (!Y) continue; if (X + Y <= m) { p = (m - X) % Y; f[1][p] = f[0][p]; while (p <= m - Y - X) { Mod(f[1][p + Y] = f[0][p + Y] - f[1][p]); p += Y; } ans[x][y] = f[1][m - Y - X] * jie % mod * jie % mod * 2 % mod * ny % mod; } else { ans[x][y] = 0; } } } q = read(); while (q--) { X = read(), Y = read(); if (Map[ch[Y]] < Map[ch[X]]) swap(X, Y); printf("%lld\n", flag * ans[Map[ch[X]]][Map[ch[Y]]]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long x, y, p, q; int main() { int T; cin >> T; while (T-- && cin >> x >> y >> p >> q) { if (x * q == p * y) { puts("0"); continue; } if (p == q || p == 0 || q == 0) { puts("-1"); continue; } long long t = 0; t = max(t, y / q + 1); t = max(t, (x - 1) / p + 1); t = max(t, (y - x - 1) / (q - p) + 1); cout << t * q - y << endl; } }
1
#include <bits/stdc++.h> using namespace std; int c[128]; int main() { int n, m, ai; memset(c, 0x00, sizeof(c)); scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d", &ai); c[ai]++; } for (int d = 128; d > 0; d--) { int k = 0; for (int i = 1; i <= 128; i++) k += c[i] / d; if (k >= n) { printf("%d\n", d); return 0; } } printf("0\n"); }
2
#include <bits/stdc++.h> using namespace std; const bool DEBUG = false; template <class T> void print1D(T v) { if (DEBUG) { for (auto e : v) cout << e << " "; cout << endl; } } template <class T> void print1D(T v, int min_incl, int max_incl) { if (DEBUG) { for (int i = min_incl; i <= max_incl; i++) cout << v[i] << " "; cout << endl; } } template <class T> void print2D(T v) { if (DEBUG) for (auto a : v) print1D(a); } template <class T> void print2D(T v, int min_incl, int max_incl, bool square) { if (DEBUG) { for (int i = min_incl; i <= max_incl; i++) { if (square) print1D(v[i], min_incl, max_incl); else print1D(v[i]); } } } template <class T> void printPair(T p) { if (DEBUG) cout << "(" << p.first << ", " << p.second << ")" << endl; } vector<bool> decToBinary(int n) { vector<bool> binaryNum(32); int i = 0; while (n > 0) { binaryNum[32 - i - 1] = n % 2; n = n / 2; i++; } return binaryNum; } int main() { int n = 0; cin >> n; vector<vector<bool> > a(n); vector<int> orig(n); for (int i = 0; i < n; i++) { int in = 0; cin >> in; orig[i] = in; a[i] = decToBinary(in); } print2D(a); vector<int> digits_kept(32, -1); for (int j = 0; j < 32; j++) { for (int i = 0; i < n; i++) { if (a[i][j] && digits_kept[j] == -1) { digits_kept[j] = i; } else if (a[i][j] && digits_kept[j] != -1) { digits_kept[j] = -2; } } } print1D(digits_kept); vector<bool> used(n, false); vector<int> order; for (int i : digits_kept) { if (i >= 0 && !used[i]) { order.push_back(i); used[i] = true; } } for (int i = 0; i < n; i++) { if (!used[i]) { order.push_back(i); } } for (int i : order) { cout << orig[i] << " "; } cout << endl; }
3
// D - Coloring Edges on Tree #include <bits/stdc++.h> using namespace std; #define rp(i,s,e) for(int i=(s);i<(e);++i) int main(){ int N; cin>>N; vector<int> G(N), C(N-1); rp(i, 0, N-1){ int p; cin>>p>>C[i]; C[i]--; G[C[i]] = p-1; } vector<int> colors(N), pre_colors(N); rp(c, 1, N){ int p = G[c]; int color = pre_colors[p] + 1; colors[c] = color==colors[p]? ++color: color; pre_colors[p] = colors[c]; } int K = 1; for(int k:pre_colors) K = max(K, k); cout<< K <<endl; for(int c:C) cout<< colors[c] <<endl; }
0
/****Bismillahir rahmanir rahim****/ #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; template <typename T> void get_vector(T &a) { for(auto &e: a) cin >> e; } template <typename T> void put_vector(T a) { for(auto e :a)cout << e << " "; cout << endl; } void solve() { ll n; cin >> n; ll sum= 0; ll inp; bool ok = true; for(ll i = 0 ; i< n; i++) { cin >> inp; sum+= inp; if(sum<i*(i+1)/2) ok = false; } cout << (ok?"YES":"NO") << endl; return; } int main() { int T; cin >> T; while(T--) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { map<int, pair<int, int> > mp; map<int, pair<int, int> >::iterator it; pair<int, int> pa; int n; scanf("%d", &n); int c[n]; int p[54][54]; int sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &c[i]); sum += c[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < c[i]; j++) { scanf("%d", &p[i][j]); pa = make_pair(i, j); mp[p[i][j]] = pa; } } int k = 1; int count = 0; vector<int> a1, a2, a3, a4; for (int i = 0; i < n; i++) { for (int j = 0; j < c[i]; j++) { pa = mp.find(k)->second; if (!(i == pa.first && j == pa.second)) { a1.push_back(i + 1); a2.push_back(j + 1); a3.push_back(pa.first + 1); a4.push_back(pa.second + 1); int temp = p[i][j]; p[i][j] = p[pa.first][pa.second]; p[pa.first][pa.second] = temp; mp[k] = make_pair(i, j); mp[p[pa.first][pa.second]] = pa; count++; } k++; } } cout << count << endl; for (int i = 0; i < a1.size(); i++) { cout << a1[i] << " " << a2[i] << " " << a3[i] << " " << a4[i] << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 5; long long ex_gcd(long long a, long long b, long long& x, long long& y) { long long tem = a; if (!b) x = 1, y = 0; else { tem = ex_gcd(b, a % b, y, x); y -= a / b * x; } return tem; } long long qpow(long long base, long long n) { long long ans = 1; while (n) { if (n & 1) ans = (ans % 1000000007) * (base % 1000000007) % 1000000007; base = (base % 1000000007) * (base % 1000000007) % 1000000007; n >>= 1; } return ans; } long long gcdl(long long a, long long b) { return b == 0 ? a : gcdl(b, a % b); } int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } int mi(int a, int b) { return a < b ? a : b; } int ma(int a, int b) { return a > b ? a : b; } int abs(int a) { return a > 0 ? a : -a; } void prep() { int ckt = 0; for (int i = 1; i <= 2; i++) ckt++; } int vis[202][1000]; int a[222]; int b[222]; int main() { int m, n; scanf("%d %d", &m, &n); int ans = 0; for (int i = 1; i <= m; i++) scanf("%d", &a[i]); for (int j = 1; j <= n; j++) scanf("%d", &b[j]); for (int i = 0; i < 1024; i++) { int flag = 1; for (int j = 1; j <= m; j++) { int f = 1; for (int k = 1; k <= n; k++) { if ((i & (a[j] & b[k])) == (a[j] & b[k])) { f = 0; break; } } if (f) { flag = 0; break; } } if (flag) { ans = i; break; } } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; typedef struct { int id; int fq; } onl; int main() { int n, k, q; cin >> n >> k >> q; int arn[n + 1]; for (int i = 1; i <= n; i++) cin >> arn[i]; deque<onl> online; for (int i = 1; i <= q; i++) { int a, id; cin >> a >> id; if (a == 1) { onl temp; temp.id = id; temp.fq = arn[id]; if (online.size() < k) { online.push_back(temp); } else { int min = temp.fq, ix = 0; for (int j = 0; j < online.size(); j++) { if (online.at(j).fq < min) { min = online.at(j).fq; ix = j; } } if (min != temp.fq) { online.at(ix) = temp; } } } else if (a == 2) { bool x = false; int k = 0; while (!x && k < online.size()) { if (online.at(k).id == id) x = true; k++; } if (x) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long x, y, n, m; long long h[1000005]; void solve() { cin >> n >> m; vector<pair<long long, long long>> v, t; for (long long i = 0; i < n; i += 1) { cin >> x >> y; v.push_back({x, y}); } for (long long i = 0; i < m; i += 1) { cin >> x >> y; t.push_back({x, y}); } for (long long i = 0; i < n; i += 1) { long long x = 0, y = 0; for (long long j = 0; j < m; j += 1) { x = max(t[j].first - v[i].first + 1, 0ll); y = max(t[j].second - v[i].second + 1, 0ll); h[x] = max(h[x], y); } } long long ans = INT64_MAX, mx = 0; for (long long i = 1000004; i >= 0; i--) { ans = min(ans, mx + i); mx = max(mx, h[i]); } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long tin = 1; while (tin--) { solve(); } return 0; }
4
#include<iostream> #include<cstdio> #define MN 100000 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; } int n,head[MN+5],cnt=0,f[MN+5],v[MN+5],ans=0; struct edge{int to,next;}e[MN*2+5]; inline void ins(int f,int t) { e[++cnt]=(edge){t,head[f]};head[f]=cnt; e[++cnt]=(edge){f,head[t]};head[t]=cnt; } void dfs(int x,int fa) { int has=0,bad=0; for(int i=head[x];i;i=e[i].next) if(e[i].to!=fa) { dfs(e[i].to,x); bad|=(has&f[e[i].to]); has|=f[e[i].to]; } for(v[x]=16;~v[x];--v[x]) if(bad&(1<<v[x])) break; for(++v[x];has&(1<<v[x]);++v[x]); f[x]=(has&((1<<17)-(1<<v[x])))|(1<<v[x]); ans=max(ans,v[x]); } int main() { n=read(); for(int i=1;i<n;++i) ins(read(),read()); dfs(1,0);cout<<ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int count = 0; int count1 = 0; cin >> m >> n; if (n >= 1 && n <= 16 && m <= n) { int k = n; if (m == 1) { while (n > 1) { n = n - 2; count1++; } cout << count1; return 0; } while (n > 0) { n = n - 1; count++; } m = m - 2; while (m > 1) { m = m - 2; count1++; } count = count * count1 + count; if (m == 1) { while (k > 1) { k = k - 2; count++; } } cout << count; } }
1
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const int nmax = 1e5 + 10; const int eps = 1e-8; const long long infll = 1e15; int main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q; while (q--) { int n, r; cin >> n >> r; set<int> s; for (int i = 0; i < n; i++) { int c; cin >> c; s.insert(c); } int ans = 0; while (1) { if (s.empty()) break; int x = *s.rbegin(); x -= (ans * r); if (x <= 0) break; auto it = prev(s.end()); s.erase(it); ans++; } cout << ans << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int f[100]; vector<string> es = { "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og"}; int main() { for (auto &c : es) { if (c.size() == 2) c[1] = toupper(c[1]); } char s[100]; cin >> s + 1; for (int i = 1; i <= strlen(s + 1); i++) { f[0] = 1; for (int j = 0; j < es.size(); j++) { int len = es[j].size(); if (len == 1 && f[i - 1] && es[j][0] == s[i]) f[i] = 1; if (len == 2 && i >= 2 && f[i - 2] && es[j][0] == s[i - 1] && es[j][1] == s[i]) f[i] = 1; } } puts(f[strlen(s + 1)] ? "YES" : "NO"); return 0; }
6
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") const int MOD = 1e9 + 7; const long double PI = 3.1415926535898; const long long INF = LLONG_MAX; int n; int a[(int)1e6 + 5]; vector<int> elements; int ans(int pos) { if (pos < 0) return 0; vector<int> on, off; for (int i = 0; i < elements.size(); i++) { if (elements[i] & (1 << pos)) on.push_back(elements[i]); else off.push_back(elements[i]); } if (on.size() == 0 || on.size() == elements.size()) { return ans(pos - 1); } elements.clear(); for (auto v : on) elements.push_back(v); int v1 = ans(pos - 1); elements.clear(); for (auto v : off) elements.push_back(v); int v2 = ans(pos - 1); return (1 << pos) + min(v1, v2); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; elements.push_back(a[i]); } cout << ans(31) << "\n"; }
4
#include <bits/stdc++.h> using namespace std; const int MaxN = 110000; double ans[MaxN]; double pos[MaxN * 2]; void calc(int n, double len, double l) { int ff = 0, rr; double st = 0, en = len; for (rr = 0; pos[rr] + 1e-9 < en; rr++) ; for (; st + 1e-9 < 2 * l;) { double dt = min(pos[rr] - en, pos[ff] - st); if (st + dt > 2 * l) dt = 2 * l - st; ans[rr - ff] += dt; if (pos[rr] - en < pos[ff] - st) rr++; else ff++; st += dt; en += dt; } for (int i = 0; i <= n; i++) printf("%.20lf\n", ans[i] / 2 / l); } int main() { int n; double l, v1, v2; cin >> n >> l >> v1 >> v2; for (int i = 0; i < n; i++) scanf("%lf", pos + i); for (int i = 0; i < n; i++) pos[i + n] = pos[i] + 2 * l; pos[2 * n] = (4 * l + 1e-7); calc(n, v2 / (v1 + v2) * l, l); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long int mxn = 1e5 + 1; string s; char cs[1000][1000]; int main() { long long int n, m, i, z, j, a = 0, r = 1, ans = 0, k = 0, cnt = 0, b = 0; long long int c = 1, x, sm, q, l = 1, y, d = 0, sum = 0, mx = 0, mn = 0; string ss, strn; char ch; cin >> n; vector<long long int> vct; vector<long long int>::iterator vit; set<long long int> st; set<long long int>::iterator sit; map<long long int, long long int> mp; map<long long int, long long int>::iterator it; long long int ara[n + 1][n + 1], taj[n + 1], bra[n + 1]; memset(taj, 0, sizeof(taj)); memset(bra, 0, sizeof(bra)); for (i = 0; i < n; i++) { mx = 0; for (j = 0; j < n; j++) { cin >> ara[i][j]; mx = max(ara[i][j], mx); } mp[mx]++; if (mp[mx] == 1) { taj[i] = mx; bra[mx] = 1; } } for (i = 0; i < n; i++) { if (taj[i]) { vct.push_back(taj[i]); continue; } for (j = 1; j <= n; j++) { if (bra[j] == 0) { bra[j] = 1; break; } } vct.push_back(j); } for (i = 0; i < n; i++) cout << vct[i] << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; class trio { public: long long first, second, third; }; class quad { public: long long first, second, third, fourth; }; class DSU { public: long long *par, *size; DSU(long long n) { par = new long long[n + 1]; size = new long long[n + 1]; for (long long i = 0; i < n + 1; ++i) { par[i] = i; size[i] = 0; } } long long find(long long a) { long long p = a, third; while (par[p] != p) p = par[p]; while (p != a) { third = par[a]; par[a] = p; a = third; } return a; } void union_(long long a, long long b) { long long u1 = find(a); long long u2 = find(b); if (size[u1] < size[u2]) { par[u1] = u2; } else { if (size[u2] < size[u1]) { par[u2] = u1; } else { par[u1] = u2; size[u1]++; } } } }; bool mycom(quad a, quad b) { return a.first < b.first; } long long *intime, *outime, timt = 1; bool inacestor(long long a, long long b) { return (intime[a] <= intime[b] && outime[a] >= outime[b]); } long long tree[100010][20]; long long dp[100010][20]; void dfs(long long i, list<pair<long long, long long> > *adj) { intime[i] = timt; timt++; for (auto it : adj[i]) { if (it.first != tree[i][0]) { tree[it.first][0] = i; dp[it.first][0] = it.second; dfs(it.first, adj); } } outime[i] = timt; timt++; } long long func(long long a, long long b) { long long ans = 0; for (long long j = 19; j >= 0; --j) { if (inacestor(tree[a][j], b)) continue; ans = max(ans, dp[a][j]); a = tree[a][j]; } for (long long j = 19; j >= 0; --j) { if (inacestor(tree[b][j], a)) continue; ans = max(ans, dp[b][j]); b = tree[b][j]; } if (!inacestor(a, b) && !inacestor(b, a)) { ans = max(ans, dp[b][0]); ans = max(ans, dp[a][0]); } else { if (inacestor(a, b)) { ans = max(ans, dp[b][0]); } else ans = max(ans, dp[a][0]); } return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, m; cin >> n >> m; quad edges[m + 1]; bool select[m + 1]; memset(select, 0, sizeof(select)); for (long long i = 0; i < m; ++i) { cin >> edges[i].second >> edges[i].third >> edges[i].first; edges[i].fourth = i; } sort(edges, edges + m, mycom); DSU dsu1(n); long long ans[m + 1]; for (long long i = 0; i < m; ++i) { ans[i] = -1; } list<pair<long long, long long> > adj[n + 1]; for (long long i = 0; i < m; ++i) { if (dsu1.find(edges[i].second) != dsu1.find(edges[i].third)) { dsu1.union_(edges[i].second, edges[i].third); select[i] = 1; adj[edges[i].second].push_back({edges[i].third, edges[i].first}); adj[edges[i].third].push_back({edges[i].second, edges[i].first}); } } intime = new long long[n + 1]; outime = new long long[n + 1]; tree[1][0] = -1; dfs(1, adj); tree[1][0] = 1; dp[1][0] = 0; for (long long j = 1; j < 20; ++j) { for (long long i = 1; i < n + 1; ++i) { tree[i][j] = tree[tree[i][j - 1]][j - 1]; dp[i][j] = max(dp[tree[i][j - 1]][j - 1], dp[i][j - 1]); } } for (long long j = 0; j < m; ++j) { if (select[j] == 0) { ans[edges[j].fourth] = func(edges[j].second, edges[j].third); } } for (long long i = 0; i < m; ++i) { if (ans[i] != -1) { cout << ans[i] << endl; } } }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const long long inf = 1000000000000000000LL; long long getB(long long x, long long y, long long b) { long long c = x - y; long long d = c / b; long long mn = -inf; long long mx = inf; for (long long i = d - 2; i <= d + 2; ++i) { if (i * b >= c) { mx = min(i, mx); } if (i * b <= c) { mn = max(mn, i); } } return mn; } long long getA(long long x, long long y, long long a) { long long c = x + y; long long d = c / a; long long mn = -inf; long long mx = inf; for (long long i = d - 2; i <= d + 2; ++i) { if (i * a >= c) { mx = min(i, mx); } if (i * a <= c) { mn = max(mn, i); } } return mn; } int main() { long long a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; long long res = 0; long long u1 = getA(x1, y1, 2LL * a); long long v1 = getB(x1, y1, 2LL * b); long long u2 = getA(x2, y2, 2LL * a); long long v2 = getB(x2, y2, 2LL * b); long long dx = u1 - u2; long long dy = v1 - v2; if (dx < 0) { dx = -dx; } if (dy < 0) { dy = -dy; } cout << (dx > dy ? dx : dy) << endl; return 0; }
2
#include <bits/stdc++.h> #pragma 03 using namespace std; const int mod = 1e9 + 7; const int mx = INT_MAX / 10; const long double pi = 4 * atan((long double)1); int n; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; int arr[n << 1]; for (int i = 1; i <= n; i++) arr[i] = i; for (int i = 2; i <= n; i++) { int temp = i - 1; arr[temp + n] = arr[temp + (n / i) * i]; for (int j = temp + (n / i) * i; j >= temp; j -= i) arr[j] = arr[j - i]; } for (int i = 1; i <= n; i++) arr[i] = arr[n + i - 1], cout << arr[i] << " "; }
2
#include <bits/stdc++.h> using namespace std; int sl[200]; int main() { int i, j, n, m; string s; cin >> s; n = s.size(); for (i = 0; i < n; i++) sl[s[i]]++; int l = 0, r = 200; while (l < r) { while (l < r && sl[l] % 2 == 0) l++; while (l < r && sl[r] % 2 == 0) r--; if (l == r) break; else { sl[l]++; sl[r]--; } } s = ""; char ch; for (i = 0; i < 200; i++) { if (sl[i] % 2 != 0) ch = i; for (j = 0; j < sl[i] / 2; j++) s += i; } string str; if (n % 2 == 1) { str = s; reverse(s.begin(), s.end()); str = str + ch + s; } else { str = s; reverse(s.begin(), s.end()); str = str + s; } cout << str; return 0; }
3
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fll; using namespace std; bool isp(string str, int len) { for (int i = 0; i < len / 2; i++) { if (str[i] != str[len - i - 1]) return false; } return true; } string judge(char str[]) { string tmp = str; int len = strlen(str); int l = 0, r = len - 1; int pos = -1; while (l < r) { if (str[l] != str[r]) { if (str[l + 1] == str[r]) { int tl = l, tr = r; l++; while (l < r) { if (str[l] != str[r]) { break; } l++, r--; } if (l >= r) { return tmp.substr(0, tr + 1) + tmp.substr(tl, 1) + tmp.substr(tr + 1, len - tr - 1); } l = tl, r = tr; } if (str[l] == str[r - 1]) { int tl = l, tr = r; r--; while (l < r) { if (str[l] != str[r]) { break; } l++, r--; } if (l >= r) { return tmp.substr(0, tl) + tmp.substr(tr, 1) + tmp.substr(tl, len - tl); } } return "NA"; } l++, r--; } if (len % 2) return tmp.substr(0, len / 2 + 1) + tmp.substr(len / 2, 1) + tmp.substr(len / 2 + 1, len / 2); return tmp.substr(0, len / 2) + tmp.substr(len / 2, 1) + tmp.substr(len / 2, len / 2); } int main() { char str[100]; scanf("%s", str); cout << judge(str) << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, a[100020]; int ans, t; int lg(int x) { if (x == 1) { return 0; } return lg(x / 2) + 1; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); int c = lg(x); int d = 0; if (x != (1 << c)) { d = 1; } if (t < c) { ans += c - t; t = c; } if (t > c) { t = c; d = 0; } if (d) { ans++; t++; } } printf("%d\n", ans); }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n; char a[maxn], b[maxn]; int cnt(char a1, char a2, char b1, char b2) { return (a1 != a2) + (a1 != b1) + (a1 != b2) + (a2 != b1) + (a2 != b2) + (b1 != b2); } int main() { while (scanf("%d", &n) != EOF) { scanf("%s%s", a + 1, b + 1); int i = 1, j = n; int ans = 0; while (i < j) { int t = cnt(a[i], a[j], b[i], b[j]); if (t == 3 || t == 5 && a[i] != a[j]) ans++; if (t == 6 || t == 5 && a[i] == a[j]) ans += 2; i++, j--; } if (i == j && a[i] != b[i]) ans++; printf("%d\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, a[N], id[N], mx[N]; int solve(int L, int R) { if (R - L == 1) return 0; int mid = L + R >> 1, ans = solve(L, mid) + solve(mid, R); mx[mid - 1] = a[mid - 1], mx[mid] = a[mid]; for (int i = mid + 1; i < R; i++) mx[i] = max(mx[i - 1], a[i]); for (int i = mid - 2; i >= L; i--) mx[i] = max(mx[i + 1], a[i]); for (int i = L, j = id[mx[i] - a[i]]; i < R; ++i, j = id[mx[i] - a[i]]) ans += (L <= min(i, j) && min(i, j) < mid && mid <= max(i, j) && max(i, j) < R && mx[i] > mx[j]); return ans; } int main() { cin >> n, id[0] = n; for (int i = 0; i < n; i++) cin >> a[i], id[a[i]] = i; cout << solve(0, n); }
5
#include <bits/stdc++.h> using namespace std; const int N = 900010; int a, b, c, d; int main() { cin >> a >> b; cin >> c >> d; if (b == d) { return 0 * printf("%d\n", b); } else { for (int i = 0; i <= 1000; i++) for (int j = 0; j <= 1000; j++) { if (b + i * a == d + j * c) { return 0 * printf("%d\n", b + i * a); } } } return 0 * printf("-1\n"); }
1