solution
stringlengths
52
181k
difficulty
int64
0
6
#include<iostream> #define C std::cin>> int main(){int n,c=0,j,l[100];C n>>l[0];for(j=1;j<n;j++)C l[j],l[j-1]==l[j]?c++,l[j]=0:0;std::cout<<c<<"\n";}
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, mod = 1e9 + 7; char s[maxn], t[maxn]; int bita[maxn], bitb[maxn], ans[maxn], pa[maxn], pb[maxn]; int lowbit(int x) { return x & -x; } void update(int b[], int v, int val, int up) { while (v <= up) { b[v] += val; v += lowbit(v); } } int sum(int b[], int v) { int res = 0; while (v) { res += b[v]; v -= lowbit(v); } return res; } void init() { int n = strlen(s + 1); int m = strlen(t + 1); for (int i = 1; i <= n; i++) { update(bita, i, s[i] != 'A', n); if (s[i] == 'A') pa[i] = pa[i - 1] + 1; else pa[i] = 0; } for (int i = 1; i <= m; i++) { update(bitb, i, t[i] != 'A', m); if (t[i] == 'A') pb[i] = pb[i - 1] + 1; else pb[i] = 0; } } int main() { scanf("%s%s", s + 1, t + 1); init(); int T; scanf("%d", &T); int cnt = 0; while (T--) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); int p = sum(bita, b) - sum(bita, a - 1); int q = sum(bitb, d) - sum(bitb, c - 1); int k1 = min(pa[b], b - a + 1); int k2 = min(pb[d], d - c + 1); if (p < q) { if ((q - p) & 1) ans[cnt] = 0; else { if (p == 0) { ans[cnt] = (k1 > k2); } else { if (k1 >= k2) ans[cnt] = 1; else ans[cnt] = 0; } } } else if (p > q) ans[cnt] = 0; else { if (k1 >= k2 && (k1 - k2) % 3 == 0) ans[cnt] = 1; else ans[cnt] = 0; } cnt++; } for (int i = 0; i < cnt; i++) printf("%d", ans[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979; const double PI2 = 6.28318530717958; const double PId2 = 1.570796326794895; template <typename T> T sqr(T x) { return x * x; } template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); } long long pw(long long a, long long p, int mod) { long long res = 1; while (p) { if (p & 1) { --p; res = (res * a) % mod; } else { p >>= 1; a = (a * a) % mod; } } return res; } inline vector<int> ReadVI(int count) { vector<int> arrayname(count); for (int i = 0; i < count; i++) cin >> arrayname[i]; return arrayname; } inline vector<long long> ReadVlong(int count) { vector<long long> arrayname(count); for (int i = 0; i < count; i++) cin >> arrayname[i]; return arrayname; } const int MOD = 1000000007; const int MAXVALUE = 10000001; vector<int> factor(int k) { vector<int> res; for (int i = 2; i * i <= k; i++) { if (k % i == 0) { res.push_back(i); while (k % i == 0) k /= i; } } if (k != 1) res.push_back(k); return res; } int main() { int n, m, k; char c; cin >> n >> m; vector<int> on(n + 1); vector<int> f(n + 1); for (int i = 0; i < m; i++) { cin >> c >> k; vector<int> p = factor(k); if (c == '-') { if (on[k]) { on[k] = 0; for (int j = 0; j < p.size(); j++) f[p[j]] = 0; cout << "Success" << endl; } else cout << "Already off" << endl; } else { if (on[k]) cout << "Already on" << endl; else { bool flag = 1; for (int j = 0; j < p.size(); j++) if (p[j] > 1 && f[p[j]] != 0) { cout << "Conflict with " << f[p[j]] << endl; flag = 0; break; } if (flag) { for (int j = 0; j < p.size(); j++) f[p[j]] = k; on[k] = 1; cout << "Success" << endl; } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } inline long long isqrt(long long k) { long long r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline long long icbrt(long long k) { long long r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(long double x, long double y) { return sign(x - y); } const int maxn = 5e5 + 5; int n; int a[maxn]; int b[maxn]; vector<int> adj[maxn]; int ans[maxn]; int vis[maxn]; vector<int> ver; pair<int, int> st[maxn << 1]; void upd(int p, int val) { int q = p; for (st[p += n] = make_pair(val, q); p > 1;) p >>= 1, st[p] = max(st[p << 1], st[p << 1 | 1]); } pair<int, int> query(int l, int r) { pair<int, int> res = make_pair(-1, -1); for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) chkmax(res, st[l++]); if (r & 1) chkmax(res, st[--r]); } return res; } void dfs(int u) { vis[u] = 1; upd(u, -1); for (int i = (0); i < (int((adj[u]).size())); i++) { int v = adj[u][i]; if (!vis[v]) { dfs(v); } } while (1) { if (a[u] == -1) a[u] = n; pair<int, int> mx = query(0, a[u] - 1); if (mx.first <= u) break; int v = mx.second; if (!vis[v]) { dfs(v); } } ver.push_back(u); } void solve() { cin >> n; fill_n(b, n, n); for (int i = (0); i < (n); i++) { cin >> a[i]; if (a[i] > 0) { a[i]--; b[a[i]] = i; adj[a[i]].push_back(i); } } for (int i = (0); i < (n); i++) upd(i, b[i]); for (int i = (0); i < (n); i++) if (!vis[i]) dfs(i); for (int i = (0); i < (int((ver).size())); i++) ans[ver[i]] = i; for (int i = (0); i < (n); i++) cout << ans[i] + 1 << " \n"[i == n - 1]; } int main() { int JUDGE_ONLINE = 1; if (fopen("in.txt", "r")) { JUDGE_ONLINE = 0; assert(freopen("in.txt", "r", stdin)); } else { ios_base::sync_with_stdio(0), cin.tie(0); } solve(); if (!JUDGE_ONLINE) { } return 0; }
5
#include <iostream> using namespace std; #define ll long long int main(){ int n,x,y; cin>>n; ll s=0; int a[n+1]; for(int i=0;i<=n;i++){ cin>>a[i]; } for(int i=0;i<n;i++){ cin>>x; y=min(x,a[i]); s+=y; x-=y; y=min(x,a[i+1]); s+=y; a[i+1]-=y; } cout<<s<<"\n"; }
0
#include <bits/stdc++.h> using namespace std; vector<int> primes; void primegen(int ub) { bool w[1001] = {false}; for (int p = 2; p <= ub; p++) if (!w[p]) { for (int i = p; i <= ub; i += p) w[i] = true; primes.push_back(p); } } int main() { string s; cin >> s; int len = int((s).size()); primegen(len); int c[26] = {0}; bool mul[len]; for (int i = (0); i < (len); i++) { mul[i] = false; c[s[i] - 'a']++; } for (int i = (0); i < (int((primes).size())); i++) { if (primes[i] * 2 > len) break; for (int p = primes[i]; p <= len; p += primes[i]) mul[p - 1] = true; } int req = 0, provider = 0; for (int i = (0); i < (len); i++) req += mul[i]; for (int i = (0); i < (26); i++) if (c[provider] < c[i]) provider = i; if (c[provider] < req) cout << "NO"; else { c[provider] -= req; for (int i = (0); i < (len); i++) { if (mul[i]) s[i] = 'a' + provider; else for (int j = (0); j < (26); j++) if (c[j]) { s[i] = 'a' + j; c[j]--; break; } } cout << "YES" << endl << s; } return 0; }
3
#include<iostream> #include<cstdio> #include<algorithm> typedef long long ll; const int N=1e5+5,INF=1e9+7; int n,m,par[N],id[N],a[N]; void init(){ for(int i=0;i<n;i++) par[i]=i,id[i]=i; } int fd(int x) { return par[x]==x?x:par[x]=fd(par[x]); } int main() { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%d",a+i);init(); int x,y; while(m--) { scanf("%d%d",&x,&y); x=fd(x);y=fd(y); if(x==y) continue; if(a[id[y]]>a[id[x]]) id[y]=id[x]; par[x]=y; } ll ans=0;int tot=0; for(int i=0;i<n;i++) if(fd(i)==i) tot++,ans+=a[id[i]],a[id[i]]=INF; if(tot==1) printf("0\n"); else { std::sort(a,a+n);bool ok=true; for(int i=0;i<tot-2;i++) { ans+=a[i]; if(a[i]>=INF) ok=false; } if(ok) printf("%lld\n",ans); else printf("Impossible\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m; int main() { cin >> n >> m; int a = n / m; int b = n % m; for (int i = 1; i <= m; ++i) { if (b > 0) { cout << a + 1 << " "; b--; continue; } cout << a << " "; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 105; const int mod = 1e9 + 7; int n, m, k, ans; int dp[maxn][maxn][maxn], ndp[maxn][maxn][maxn], binom[maxn][maxn]; void Add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int main() { scanf("%d%d%d", &n, &m, &k); binom[0][0] = 1; for (int i = 1; i <= n; i++) { binom[i][0] = binom[i][i] = 1; for (int j = 1; j < i; j++) binom[i][j] = min(k + 1, (binom[i - 1][j - 1] + binom[i - 1][j])); } dp[0][1][1] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= n; j++) for (int p = 1; p <= n; p++) for (int q = 1; q <= k; q++) { int cur = dp[j][p][q]; if (!cur) continue; for (int jj = p; jj <= n - j; jj++) { if (q * binom[jj - 1][p - 1] <= k) Add(ndp[j + jj][jj - p + (j == 0)][q * binom[jj - 1][p - 1]], cur); else break; } } for (int j = 0; j <= n; j++) for (int p = 0; p <= n; p++) for (int q = 1; q <= k; q++) { dp[j][p][q] = ndp[j][p][q]; ndp[j][p][q] = 0; if (p == 0) Add(ans, dp[j][p][q]); } Add(dp[0][1][1], 1); } printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int w,h,n;cin>>w>>h>>n; int x1=0,x2=w,y1=0,y2=h; for(int i=0;i<n;i++) { int x,y,a;cin>>x>>y>>a; if(a==1)x1=max(x1,x); if(a==2)x2=min(x2,x); if(a==3)y1=max(y1,y); if(a==4)y2=min(y2,y); } int ans=max(x2-x1,0)*max(y2-y1,0); cout<<ans<<"\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MaxN = 1005; int cnt[30], n; char s[MaxN]; bool bio[MaxN]; void add(int p) { for (int i = p; i <= n; i += p) bio[i] = true; } int main() { scanf("%s", s + 1); n = strlen(s + 1); if (n <= 2) { puts("YES"); puts(s + 1); return 0; } for (int i = 1; i <= n; i++) cnt[s[i] - 'a']++; add(2); for (int i = 3; i <= n; i++) if (2 * i <= n) add(i); int bigset = count(bio + 1, bio + n + 1, true); int c = max_element(cnt, cnt + 30) - cnt; if (bigset > cnt[c]) { puts("NO"); return 0; } for (int i = 1; i <= n; i++) if (bio[i]) { s[i] = (char)(c + 'a'); cnt[c]--; } for (int i = 1; i <= n; i++) if (!bio[i]) { c = max_element(cnt, cnt + 30) - cnt; s[i] = (char)(c + 'a'); cnt[c]--; } puts("YES"); puts(s + 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; int compareAscending(const void *a, const void *b) { return (*(int *)a - *(int *)b); } int compareDecending(const void *a, const void *b) { return (*(int *)b - *(int *)a); } int compareFirst(const void *a, const void *b) { if (((pair<int, int> *)a)->first == ((pair<int, int> *)b)->first) return ((pair<int, int> *)a)->second - ((pair<int, int> *)b)->second; else return ((pair<int, int> *)a)->first - ((pair<int, int> *)b)->first; } int compareSecond(const void *a, const void *b) { if (((pair<int, int> *)a)->second == ((pair<int, int> *)b)->second) return ((pair<int, int> *)a)->first - ((pair<int, int> *)b)->first; else return ((pair<int, int> *)a)->second - ((pair<int, int> *)b)->second; } bool isPrime(int a) { if (a < 2) return false; if (a < 4) return true; if (a % 2 == 0) return false; int s = (int)sqrt(a); for (int i = 3; i <= s; i += 2) { if (a % i == 0) return false; } return true; } int bsearch(int *a, int value, int l, int r) { if (l == r) return l; if (l + 1 == r) return a[l] >= value ? l : r; int m = (l + r) / 2; if (a[m] >= value) return bsearch(a, value, l, m); else return bsearch(a, value, m + 1, r); } int main() { long long a, b, res = 0, t; scanf("%lld %lld", &a, &b); while (a != 0 && b != 0) { res += a / b; t = a % b; a = b; b = t; } printf("%lld", res); return 0; }
1
#include <bits/stdc++.h> using namespace std; int pre[100005][20], suf[100005][20], mn[100005][20]; int a[100005], lg[100005], n, ans; int getmn(int x, int y) { if (x > y) return 1e9; int k = lg[y - x + 1]; return min(mn[x][k], mn[y - (1 << k) + 1][k]); } int getmn1(int x, int y) { if (x > y) return 1e9; int k = lg[y - x + 1]; return min(pre[x][k], pre[y - (1 << k) + 1][k]); } int getmn2(int x, int y) { if (x > y) return 1e9; int k = lg[y - x + 1]; return min(suf[x][k], suf[y - (1 << k) + 1][k]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); mn[i][0] = a[i]; pre[i][0] = a[i] - i; suf[i][0] = a[i] + i; } for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1; for (int j = 1; j < 17; j++) { int bit = 1 << (j - 1); for (int i = 1; i + (1 << j) - 1 <= n; i++) { mn[i][j] = min(mn[i][j - 1], mn[i + bit][j - 1]); pre[i][j] = min(pre[i][j - 1], pre[i + bit][j - 1]); suf[i][j] = min(suf[i][j - 1], suf[i + bit][j - 1]); } } int L = 1; ans = 1e9; for (int T = 1; T <= n; T++) { for (; L <= T && T >= 2 * L - a[L]; L++) ; int j = -1; if (L <= T) j = L; else { int l = T, r = n; while (l <= r) { int mid = (l + r) / 2; if (getmn(T, mid) < T) j = mid, r = mid - 1; else l = mid + 1; } if (j == -1) continue; } int x, y; if (j <= T) { x = getmn1(1, j - 1) + T; y = min(getmn1(j, T) + T, getmn2(T + 1, n) - T); } else { x = min(getmn1(1, T) + T, getmn2(T + 1, j - 1) - T); y = getmn2(j, n) - T; } ans = min(ans, max(x, y)); } if (ans > 1e8) puts("-1"); else printf("%d", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<long long int> v[1000007]; vector<long long int> vv[2]; map<long long int, long long int> mp; long long int a[1000007]; int aa[2]; int vis[1000007]; long long int dfs(int pos, int id) { vis[pos] = 1; int i; long long int ans = 0; int lt = 0; for (i = 0; i < v[pos].size(); i++) if (!vis[v[pos][i]]) ans = (ans ^ dfs(v[pos][i], id + 1)), lt = 1; if (!lt) aa[id % 2] = 1, aa[(id + 1) % 2] = 0; if (aa[id % 2]) vv[0].push_back(a[pos]), mp[a[pos]]++, ans = (ans ^ a[pos]); else vv[1].push_back(a[pos]); return ans; } int main() { int m; scanf("%d", &m); int i; int tem; for (i = 1; i <= m; i++) scanf("%lld", a + i); for (i = 1; i < m; i++) scanf("%d", &tem), v[tem].push_back(i + 1); long long odd = dfs(1, 0); long long int ans = 0; if (!odd) ans = ans + (vv[0].size() * (vv[0].size() - 1)) / 2 + (vv[1].size() * (vv[1].size() - 1)) / 2; for (i = 0; i < vv[1].size(); i++) ans = ans + mp[odd ^ vv[1][i]]; printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; inline bool isvowel(char c) { c = tolower(c); if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u') return 1; return 0; } const double eps = 0.000001; const long double pi = acos(-1); const int maxn = 1e7 + 9; const int mod = 1e9 + 7; const long long MOD = 1e18 + 9; const long long INF = 1e18 + 123; const int inf = 2e9 + 11; const int mxn = 1e6 + 9; const int N = 6e5 + 123; const int M = 22; const int pri = 997; const int Magic = 2101; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; int n, m, k; int a[N], b[N]; int cnt[N]; set<pair<int, int> > all; long long cur = 0, ans = 0; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b + 1, b + n + 1); for (long long i = 1; i <= n; i++) { a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; cnt[a[i]]++; } for (long long i = 1; i <= n; i++) { all.insert(make_pair(cnt[a[i]], a[i])); } for (int i = 1; i <= n; i++) { int it = i; cur = 0; vector<pair<int, int> > del; while (all.size()) { auto v = all.lower_bound(make_pair(it, -1)); if (v == all.end()) break; cur += it; del.push_back(*v); all.erase(v); it += it; } ans = max(ans, cur); for (auto to : del) all.insert(to); } cout << ans << '\n'; return 0; }
5
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define MOD2 1000000007 #define MOD 998244353 #define PRIM 3 #define INF (1 << 29) #define LINF (1LL << 60) #define EPS (1e-10) #define PI 3.1415926535897932384626433832795028 typedef long long Int; typedef pair<Int, Int> P; typedef long double Real; typedef complex<Real> CP; bool ok[330][330][330]; // pos one two Int cnt[330][330][330]; Int exact[330][330][330]; string s; Int ans; int main() { cin >> s; int n = s.size(); for (int i = 0; i <= n; i++) { for (int j = n; j >= 0; j--) { for (int k = n; k >= 0; k--) { if (i == 0 && j == 0 && k == 0) ok[i][j][k] = true; if (i + 1 < n && (s[i] == '0' || s[i + 1] == '0')) { ok[i + 2][j + 1][k] |= ok[i][j][k]; } if (i + 1 < n && (s[i] == '1' || s[i + 1] == '1')) { ok[i + 2][j][k + 1] |= ok[i][j][k]; } if (i < n && s[i] == '0' && k > 0) { ok[i + 1][j + 1][k - 1] |= ok[i][j][k]; } if (i < n && s[i] == '1' && j > 0) { ok[i + 1][j - 1][k + 1] |= ok[i][j][k]; } ok[i + 1][j][k] |= ok[i][j][k]; if (j) ok[i][j - 1][k] |= ok[i][j][k]; if (k) ok[i][j][k - 1] |= ok[i][j][k]; } } } exact[n][0][0] = 1; for (int one = 0; one <= n; one++) { for (int zero = 0; zero <= n; zero++) { int zero_cnt = 0; int one_cnt = 0; for (int pos = n; pos >= 0; pos--) { cnt[pos][one][zero] = 0; if (one) cnt[pos][one][zero] += cnt[pos][one - 1][zero] + exact[pos][one - 1][zero]; if (zero) cnt[pos][one][zero] += cnt[pos][one][zero - 1] + exact[pos][one][zero - 1]; if (pos + 1 <= n) { exact[pos][one][zero] = exact[pos + 1][one][zero]; if (s[pos] == '0' && one > 0) { exact[pos][one][zero] += exact[pos][one - 1][zero]; } if (s[pos] == '1' && zero > 0) { exact[pos][one][zero] += exact[pos][one][zero - 1]; } } cnt[pos][one][zero] %= MOD; exact[pos][one][zero] %= MOD; } if (one == 0 && zero == 0) continue; if (ok[n][zero][one]) { ans += exact[n][one][zero]; ans += cnt[n][one][zero]; ans %= MOD; // cout << zero << " " << one << " " << ans << // endl; continue; } for (int pos = n - 1; pos >= 0; pos--) { if (s[pos] == '0') zero_cnt++; else one_cnt++; int need_one = one - one_cnt; int need_zero = zero - zero_cnt; if (need_one < 0 || need_zero < 0) break; if (ok[pos][need_zero][need_one]) { ans += exact[pos][need_one][need_zero]; ans += cnt[pos][need_one][need_zero]; ans %= MOD; // cout << pos << " " << need_zero << " " // << need_one << " " // << ans << endl; break; } } } } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 52; int Nextint() { char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); int x = 0; while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x; } int A[maxn][maxn], S[maxn], N, lt, B[maxn][maxn], C[maxn][maxn], D1[maxn], D2[maxn]; long long ts[maxn]; void qck(long long y) { if (y == 1) { memcpy(B, A, sizeof(A)); return; } qck(y / 2); memcpy(C, B, sizeof(B)); memset(B, 0, sizeof(B)); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = 1; k <= N; k++) (B[i][k] += C[i][j] * C[j][k]) %= 2; if (y & 1) { memcpy(C, B, sizeof(B)); memset(B, 0, sizeof(B)); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = 1; k <= N; k++) (B[i][k] += C[i][j] * A[j][k]) %= 2; } } bool E() { for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) if ((B[i][j] == 1) ^ (i == j)) return false; return true; } bool find() { for (int i = 1; i <= N; i++) S[i] = rand() % 2; S[1] = 1; S[2] = 1; S[3] = 0; memset(A, 0, sizeof(A)); for (int i = 1; i < N; i++) A[i + 1][i] = 1; for (int i = 1; i <= N; i++) A[i][N] = S[i]; long long tmp = 1; tmp <<= N; tmp--; qck(tmp); if (!E()) return false; for (int t = 1; t <= lt; t++) { qck(tmp / ts[t]); if (E()) return false; } return true; } void Sm() { long long tmp = 1; tmp = (tmp << N); tmp--; long long sum = tmp; long long tts = 2; while (tts * tts <= tmp) { if (tmp % tts == 0) { lt++; ts[lt] = tts; while (tmp % tts == 0) tmp /= tts; } tts++; } if (tmp != sum && tmp != 1) { lt++; ts[lt] = tmp; } } int main() { srand(111); scanf("%d", &N); Sm(); while (!find()) { } for (int i = 1; i <= N; i++) { printf("%d", S[N - i + 1]); if (i == N) printf("\n"); else printf(" "); } for (int i = 1; i <= N; i++) { if (i == N) printf("1\n"); else printf("0 "); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, inf = 1e9 + 7; const long double eps = 1e-12; const long long linf = (long long)1e18 + 7, mod = 1e14; int main() { string s; cin >> s; long long n = (int)(s).size(), ans = 0; for (int i = 0; i < n; i++) { stack<char> st; long long cnt = 0; for (int j = i; j < n; j++) { if (s[j] == '?') { if ((int)(st).size() == 0) { st.push('('); } else { cnt++; st.pop(); } } else if (s[j] == '(') { st.push('('); } else if (s[j] == ')') { if ((int)(st).size() == 0) { if (cnt == 0) { break; } cnt--; st.push('('); } else { st.pop(); } } if ((int)(st).size() == 0) ans++; } } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; struct Tar { int x, y, t; double p; void read() { scanf("%d%d%d%lf", &x, &y, &t, &p); } } r[1010]; bool cmp(Tar a, Tar b) { return a.t < b.t; } double dis(double x, double y) { return sqrt(x * x + y * y); } bool good(int a, int b) { return dis(r[a].x - r[b].x, r[a].y - r[b].y) < r[b].t - r[a].t + 1e-8; } double dp[1010]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) r[i].read(); sort(r, r + n, cmp); dp[0] = r[0].p; for (int i = 1; i < n; i++) { dp[i] = r[i].p; for (int j = 0; j < i; j++) { if (good(j, i)) { dp[i] = max(dp[i], dp[j] + r[i].p); } } } double mx = 0; for (int i = 0; i < n; i++) mx = max(mx, dp[i]); printf("%.9lf\n", mx); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long b[7]; const long long M = 6 * 1e5 + 7; struct data1 { long long x; long long val; }; data1 a[M]; bool cmp(data1 u, data1 v) { return (u.x < v.x) || (u.x == v.x && u.val < v.val); } int main() { for (long long i = 1; i <= 6; i++) { cin >> b[i]; } long long dem = 0; long long n; cin >> n; for (long long i = 1; i <= n; i++) { long long x; cin >> x; for (long long j = 1; j <= 6; j++) { dem++; a[dem] = data1{x - b[j], i}; } } sort(a + 1, a + dem + 1, cmp); long long r = 0; long long kq = 1e9; long long dem2 = 0; map<long long, long long> check; for (long long i = 1; i <= dem; i++) { for (; r < dem && dem2 != n;) { r++; check[a[r].val]++; if (check[a[r].val] == 1) { dem2++; } } if (dem2 != n) { break; } kq = min(kq, abs(a[r].x - a[i].x)); check[a[i].val]--; if (check[a[i].val] == 0) { dem2--; } } cout << kq; }
3
#include <bits/stdc++.h> using namespace std; int n, d, a[100005], b[100005], idx[100005]; long long x; int s = 30; vector<int> ones; long long getNextX() { x = (x * 37 + 10007) % 1000000007; return x; } void initAB() { int i; for (i = 0; i < n; i = i + 1) { a[i] = i + 1; } for (i = 0; i < n; i = i + 1) { swap(a[i], a[getNextX() % (i + 1)]); } for (i = 0; i < n; i = i + 1) { if (i < d) b[i] = 1; else b[i] = 0; } for (i = 0; i < n; i = i + 1) { swap(b[i], b[getNextX() % (i + 1)]); } } int main() { cin >> n >> d >> x; initAB(); for (int i = 0; i < n; i++) { idx[a[i]] = i; if (b[i]) ones.push_back(i); } for (int i = 0; i < n; i++) { int ans = 0; for (int j = 0; j < s; j++) { int x = n - j; if (x <= 0) break; if (idx[x] <= i && b[i - idx[x]] == 1) { ans = x; break; } } if (ans == 0) { for (int k = 0; k < (int)(ones).size(); k++) { if (ones[k] > i) break; ans = max(ans, a[i - ones[k]]); } } cout << ans << endl; } }
4
#include<bits/stdc++.h> #define INF 0x3f3f3f3f #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef pair<int, int>P; int c[6], cost[110][1 << 6], m, n, k, d; struct st { int a, b, c; }; int num(string s) { if (s == "H")return m + n; if (s == "D")return m + n + 1; if (s[0] == 'C')return s[1] - '1'; return stoi(s.substr(1)) - 1 + m; } int main() { while (scanf("%d%d%d%d", &m, &n, &k, &d), m) { rep(i, m)scanf("%d", &c[i]); vector<st>v; rep(i, d) { string s, t; int e; cin >> s >> t >> e; e *= k; v.push_back({ num(s),num(t),e }); v.push_back({ num(t),num(s),e }); } memset(cost, 0x3f, sizeof(cost)); cost[m + n][0] = 0; bool update = true; while (update) { update = false; for (st e : v)rep(i, 1 << m) { if (cost[e.a][i] == INF)continue; if (e.b < m) { if (!(i >> e.b & 1) && cost[e.b][i | 1 << e.b] > cost[e.a][i] + e.c - c[e.b]) cost[e.b][i | 1 << e.b] = cost[e.a][i] + e.c - c[e.b], update = true; } else if (cost[e.b][i] > cost[e.a][i] + e.c) cost[e.b][i] = cost[e.a][i] + e.c, update = true; } } int Min = INF; rep(i, 1 << m)Min = min(Min, cost[m + n + 1][i]); printf("%d\n", Min); } }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } int main() { long long m, n; cin >> n >> m; cout << binpow((binpow(2, m) - 1), n) << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c = 1000; for (cin >> a >> b; cin >> a; b = a) if (a > b) c += c / b * (a - b); cout << c; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> three(N); vector<int> four(N); for (int i = 0; i < N; i++) { cin >> three[i]; } for (int i = 0; i < N; i++) { cin >> four[i]; } vector<int> revind_t(N + 1); vector<int> revind_f(N + 1); for (int i = 0; i < N; i++) { revind_t[three[i]] = i; revind_f[four[i]] = i; } vector<int> transformation(N); for (int i = 0; i < N; i++) { transformation[i] = revind_t[four[i]]; } for (int i = 0; i < N; i++) { cout << transformation[i] + 1 << " \n"[i == N - 1]; } }
6
#include <bits/stdc++.h> using namespace std; const int N = 2005; int n, m; int f[N][N], to[N]; char s[N], p[N]; int main() { scanf("%s%s", s + 1, p + 1); n = strlen(s + 1), m = strlen(p + 1); for (int i = 1; i <= n; i++) { to[i] = n + 2; int k = 1; for (int j = i; j <= n; j++) { if (s[j] == p[k]) { k++; if (k > m) { to[i] = j + 1; break; } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { f[i + 1][j] = max(f[i + 1][j], f[i][j]); f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]); if (to[i] != n + 2) { int nex = j + to[i] - m - i; f[to[i]][nex] = max(f[to[i]][nex], f[i][j] + 1); } } } for (int i = 1; i <= n + 1; i++) { printf("%d%c", f[n + 1][i], i == n + 1 ? '\n' : ' '); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 2; vector<int> g[N]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; ++i) sort(g[i].begin(), g[i].end()); vector<int> tmp(n); iota(tmp.begin(), tmp.end(), 1); set<int> unreached(tmp.begin(), tmp.end()); int res = -1; for (int start = 1; start <= n; ++start) if (!unreached.empty() && start == *unreached.begin()) { queue<int> q; q.push(start); unreached.erase(unreached.begin()); while (!q.empty()) { int u = q.front(); q.pop(); vector<int> new_reached; set_difference(unreached.begin(), unreached.end(), g[u].begin(), g[u].end(), back_inserter(new_reached)); for (int v : new_reached) { q.push(v); unreached.erase(v); } } ++res; } cout << res; return 0; }
2
#include<bits/stdc++.h> using namespace std; #define Uni All right #define REP(i,a,b) for(int i=(a),i##_end_=(b);i<i##_end_;++i) #define DREP(i,a,b) for(int i=(a),i##_end_=(b);i>i##_end_;--i) #define LREP(i,a) for(int i=Head[a];i;i=Next[i]) #define LL long long #define Mod 1000000007 static const int M=(1<<17)+4; int n,A,B; int Cnt[M]; void Work(int f,int a,int b){ if(Cnt[f]==1)printf("%d %d ",a,b); else{ int p=1,q=1; while(!(f&p) || !((a^b)&p))p<<=1; while(!(f&q) || p==q)q<<=1; Work(f^p,a,a^q); Work(f^p,a^q^p,b); } } int main(){ scanf("%d%d%d",&n,&A,&B); int t=0; REP(i,0,1<<n)Cnt[i]=Cnt[i>>1]+(i&1); if(!(Cnt[A^B]&1))return puts("NO"),0; puts("YES"); Work((1<<n)-1,A,B); puts(""); return 0; }
0
#include <bits/stdc++.h> using namespace std; inline long long mulmod(long long x, long long n, long long _mod) { long long res = 0; while (n) { if (n & 1) res = (res + x) % _mod; x = (x + x) % _mod; n >>= 1; } return res; } inline long long powmod(long long x, long long n, long long _mod) { long long res = 1; while (n) { if (n & 1) res = (res * x) % _mod; x = (x * x) % _mod; n >>= 1; } return res; } inline long long gcd(long long a, long long b) { long long t; while (b) { a = a % b; t = a; a = b; b = t; } return a; } inline int gcd(int a, int b) { int t; while (b) { a = a % b; t = a; a = b; b = t; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long gcd(long long a, long long b, long long c) { return gcd(gcd(a, b), c); } inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); } int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; long long mod = 1e9 + 7; std::vector<int> v1[100005], v2[100005]; bool vis[100005]; std::vector<pair<int, int> > f; int a[100005], n, m; int t = 0; void dfs(int u) { vis[u] = 1; for (int x : v1[u]) { if (!vis[x]) dfs(x); } t++; f.push_back(make_pair(t, u)); } int cnt = 0, sum = 1e9 + 5; void dfs2(int u) { vis[u] = 1; if (a[u] < sum) { sum = a[u]; cnt = 1; } else if (a[u] == sum) cnt++; for (int x : v2[u]) { if (!vis[x]) dfs2(x); } } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v1[x].push_back(y); v2[y].push_back(x); } memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { if (!vis[i]) dfs(i); } memset(vis, 0, sizeof(vis)); sort(f.begin(), f.end()); long long ans1 = 0, ans2 = 1; for (int i = f.size() - 1; i >= 0; i--) { int u = f[i].second; if (!vis[u]) { dfs2(u); ans1 += sum; ans2 = (1ll * ans2 * cnt) % mod; cnt = 0, sum = 1e9 + 5; } } cout << ans1 << " " << ans2 << "\n"; }
3
#include <bits/stdc++.h> using namespace std; const long long int cons = 100005; const long long int MOD1 = 1000000007; const long long int MOD2 = 998244353; const long long int const_INT_MAX = 100000000000000000; const long long int const_INT_MIN = -1 * 100000000000000000; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { long long int n, m; cin >> n >> m; long long int a[n]; long long int sum = 0; for (long long int i = 0; i < n; i++) { cin >> a[i]; } for (long long int i = 0; i < n; i++) { sum += a[i]; } if (sum == m) { cout << "YES\n"; } else { cout << "NO\n"; } } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1123456; char s[maxn], t[maxn]; int dp[maxn][8]; long long ans; int main() { scanf("%s%s", s, t); int n = strlen(s), m = strlen(t); for (int(i) = (0); (i) <= ((n)-1); (i)++) s[i] %= 3; for (int(i) = (0); (i) <= ((m)-1); (i)++) t[i] %= 3; for (int(i) = (1); (i) <= (n - 1); (i)++) { for (int(j) = (0); (j) <= ((8) - 1); (j)++) dp[i][j] = dp[i - 1][j]; if (s[i - 1] != s[i]) dp[i][(s[i - 1] > s[i]) * 4 + s[i - 1] + s[i]]++; } for (int(i) = (0); (i) <= ((8) - 1); (i)++) dp[n][i] = dp[n - 1][i]; int lo = 0, hi = 0; for (int(i) = (0); (i) <= ((m)-1); (i)++) { if (i && t[i - 1] != t[i]) { int j = (t[i - 1] < t[i]) * 4 + t[i - 1] + t[i]; ans += dp[lo][j] - dp[hi][j]; } while (hi < n && s[hi] != t[i]) hi++; ans += hi - lo + 1 - (hi == n); if (hi < n) hi++; if (lo < hi && s[lo] == t[i]) lo++; } printf("%lld", ans); }
4
#include <bits/stdc++.h> using namespace std; struct node { long long start; long long end; long long count; }; node arr[1000010]; long long ans[1000010]; long long maxi = -1; long long mini = 10000000; int main() { for (long long i = 1; i <= 1000000; i++) arr[i].count = 0; long long n; cin >> n; for (long long i = 0; i < n; i++) { long long x; cin >> x; if (arr[x].count == 0) { arr[x].count++; arr[x].start = i + 1; arr[x].end = i + 1; } else { arr[x].count++; arr[x].end = i + 1; } maxi = max(maxi, arr[x].count); } long long ans1, ans2; for (long long i = 1; i <= 1000000; i++) { if (arr[i].count == maxi) { long long x1 = arr[i].start; long long x2 = arr[i].end; if ((x2 - x1) < mini) { mini = x2 - x1; ans1 = x1; ans2 = x2; } } } cout << ans1 << " " << ans2 << endl; }
2
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main(){ int d,n,m,ans,In; vector<int> vec; while(cin >> d && d){ ans = 0; vec.clear(); vec.push_back(0); cin >> n >> m; for(int i=0;i<n-1;i++){ cin >> In; vec.push_back(In); } vec.push_back(d); sort(vec.begin(),vec.end()); for(int i=0;i<m;i++){ cin >> In; vector<int>::iterator it = lower_bound(vec.begin(),vec.end(),In); //cout << (*it) << " " << (*(it-1)) << endl; ans += abs((*it)-In)<abs((*(it-1))-In)?abs((*it)-In):abs((*(it-1))-In); } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int Maxn = 500005; int n, m, q, a[Maxn], cnt[Maxn], ct, out[Maxn], tot, rk[Maxn]; long long tmp[Maxn], now[Maxn], all; vector<int> Ve[Maxn]; struct quest { int id; long long x; bool operator<(const quest& tmp) const { return x < tmp.x; } } Q[Maxn]; struct node { int val, siz, cnt; node *son[2], *fa; } * root; void create(node*& t) { t = new (node); t->val = t->siz = 0; t->son[0] = t->son[1] = t->fa = NULL; } bool get_son(node* t) { return t->fa->son[1] == t; } void update(node* t) { t->siz = t->cnt; if (t->son[0] != NULL) t->siz += t->son[0]->siz; if (t->son[1] != NULL) t->siz += t->son[1]->siz; } void rotate(node* t) { node *fa = t->fa, *gf = fa->fa; if (gf != NULL) { bool s = get_son(fa); gf->son[s] = t; } else root = t; bool ss = get_son(t); fa->son[ss] = t->son[!ss]; if (t->son[!ss] != NULL) t->son[!ss]->fa = fa; t->son[!ss] = fa; fa->fa = t; t->fa = gf; if (gf != NULL) update(gf); update(fa), update(t); } void splay(node* t, node* goal = NULL) { while (t->fa != goal) { node *fa = t->fa, *gf = fa->fa; if (gf == goal) rotate(t); else { if (get_son(fa) ^ get_son(t)) rotate(t), rotate(t); else rotate(fa), rotate(t); } } } node* insert(int val) { node* tmp; if (root == NULL) { create(root); root->val = val; root->cnt = root->siz = 1; return root; } else { for (node* t = root; t != NULL; t = t->son[val >= t->val]) { if (t->val == val) { t->cnt++; if (t->fa != NULL) update(t->fa); splay(t); update(t); tmp = t; break; } if (t->son[val >= t->val] == NULL) { bool s = val >= t->val; create(t->son[s]); t->son[s]->val = val; t->son[s]->cnt = 1; t->son[s]->siz = 1; t->son[s]->fa = t; update(t); tmp = t->son[s]; break; } } } splay(tmp); return tmp; } node* find_kth(int k) { for (node* t = root; t != NULL;) { int tmp = 0; if (t->son[0] != NULL) tmp += t->son[0]->siz; if (k > tmp && k <= tmp + t->cnt) return t; if (tmp < k && t->son[1] != NULL) k -= tmp + t->cnt, t = t->son[1]; else t = t->son[0]; } } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), cnt[a[i]]++; for (int i = 1; i <= m; i++) rk[i] = i; sort(rk + 1, rk + 1 + m, [](int x, int y) { return cnt[x] < cnt[y]; }); for (int i = 1; i <= q; i++) scanf("%lld", &Q[i].x), Q[i].id = i; sort(Q + 1, Q + 1 + q); insert(rk[1]); tot = 1; for (int i = 1; i <= q; i++) { Q[i].x -= n + 1; while (tot < m && all + (long long)tot * (cnt[rk[tot + 1]] - cnt[rk[tot]]) <= Q[i].x) { all += (long long)tot * (cnt[rk[tot + 1]] - cnt[rk[tot]]); insert(rk[++tot]); } Q[i].x = (Q[i].x - all) % tot + 1; out[Q[i].id] = find_kth(Q[i].x)->val; } for (int i = 1; i <= q; i++) printf("%d\n", out[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<long long> g; long long a[300100]; long long now, tmp, k; int b, l, i, j, ans, n; int main() { cin >> n >> k; for (i = 0; i <= n; i++) { scanf("%d", &b); a[i] = b; g.push_back(a[i]); } for (i = 0; i < g.size(); i++) { tmp = g[i]; if (abs(tmp) <= 1) continue; if (i + 1 == g.size()) g.push_back(0); g[i + 1] += tmp / 2; g[i] %= 2; } while (l < g.size() && g[l] == 0) l++; for (i = g.size() - 1; i >= 0; i--) { now = now * 2 + g[i]; if (abs(now) > (1LL << 35)) break; if (i > l) continue; if (i > n) continue; if (i == n && a[i] == now) continue; if (abs(1LL * a[i] - now) <= k) ans++; } cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; bool a[256] = {}; string s; cin >> n; cin >> s; int sl = s.length(); int t = 0; for (int i = 0; i < sl; i++) { t += a[s[i]] == false; a[s[i]] = true; } if (t >= n) { cout << "YES\n"; bool first = true; for (int i = 'a'; i <= 'z'; i++) a[i] = false; t = 0; for (int i = 0; i < sl; i++) { if (t >= n) { cout << s[i]; } else { if (a[s[i]]) cout << s[i]; else { cout << (first ? "" : "\n") << s[i]; first = false; a[s[i]] = true; t++; } } } } else { cout << "NO\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int b = 0, c = 0; string a; cin >> a; for (int i = 0; i < a.size(); i++) { if (a[i] != a[a.size() - 1 - i]) b++; } if (b == 0) { for (int i = 0; i < a.size(); i++) { if (a[i] == 'A') c++; else if (a[i] == 'H') c++; else if (a[i] == 'I') c++; else if (a[i] == 'M') c++; else if (a[i] == 'O') c++; else if (a[i] == 'T') c++; else if (a[i] == 'U') c++; else if (a[i] == 'V') c++; else if (a[i] == 'W') c++; else if (a[i] == 'X') c++; else if (a[i] == 'Y') c++; } if (c == a.size()) cout << "YES" << endl; else cout << "NO" << endl; } else cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int ON(int N, int pos) { return N = N | (1 << pos); } int OFF(int N, int pos) { return N = N & ~(1 << pos); } bool isON(int N, int pos) { return (bool)(N & (1 << pos)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc, cs; int i, j, k; int n; while (cin >> n >> k) { int a[105]; for (i = 0; i < n; i++) cin >> a[i]; int ans = 0; for (i = 0; i < k; i++) { int o = 0, t = 0; for (j = i; j < n; j += k) { if (a[j] == 1) o++; else t++; } ans += min(o, t); } cout << ans << endl; } return 0; }
1
#include "bits/stdc++.h" using namespace std; using VS = vector<string>; using LL = long long; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using PLL = pair<LL, LL>; using VL = vector<LL>; using VVL = vector<VL>; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++) #define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--) #define debug(x) cerr << #x << ": " << x << endl const int INF = 1e9; const LL LINF = 1e16; const LL MOD = 1000000007; const double PI = acos(-1.0); int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 }; /* ----- 2017/09/20 Problem: ACPC2017_day3_a / Link: ----- */ /* ------??????------ -----?????????????????§----- */ /* -----?§£??¬???----- ----?§£??¬???????????§---- */ LL N; LL ans = 0LL; int main() { cin.tie(0); ios_base::sync_with_stdio(false); string s; cin >> s; int now = 0; FOR(i, 0, SZ(s)) { int p = s[i] - 'A'; if (now >= p)ans++; now = p; } cout << ans << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; class Solution { public: void solve(std::istream& in, std::ostream& out) { int n; in >> n; std::vector<int> known; std::vector<string> s(n); std::vector<int> info; int target_size = 0; for (int i = 0; i < n; ++i) { std::string t; in >> t; int k; in >> k; s[i] = t; for (int j = 0; j < k; ++j) { int x; in >> x; --x; target_size = std::max(target_size, x + (int)t.length()); if (info.size() < x + 1) info.resize(x + 1, -1); if (info[x] == -1) info[x] = i; else if (t.length() > s[info[x]].length()) { info[x] = i; } } } int last_valid_i = -1; std::string res(target_size, 'a'); for (int i = 0; i < target_size; ++i) { if (i < info.size() && info[i] != -1 && (last_valid_i == -1 || i + s[info[i]].length() > last_valid_i + s[info[last_valid_i]].length())) last_valid_i = i; if (last_valid_i != -1 && i < last_valid_i + s[info[last_valid_i]].length()) res[i] = s[info[last_valid_i]][i - last_valid_i]; } out << res << '\n'; } }; void solve(std::istream& in, std::ostream& out) { out << std::setprecision(12); Solution solution; solution.solve(in, out); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; ostream& out = cout; solve(in, out); return 0; }
3
#include <cstdio> int main() { int a,b; scanf("%d %d", &a, &b); printf("%d", a * a - b); }
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") using namespace std; bool is_prime(long long n) { for (long long i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } vector<long long> fact(long long n) { vector<long long> ans; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { ans.push_back(i); while (n % i == 0) { n /= i; } } } if (n > 1) ans.push_back(n); return ans; } long long getPow(long long a, long long b) { long long res = 1ll, tp = a; while (b) { if (b & 1ll) { res *= tp; } tp *= tp; b >>= 1ll; } return res; } long long vec_mult(long long x1, long long y1, long long x2, long long y2, long long x3, long long y3) { return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)); } void ok() { cout << "YES" << endl; exit(0); } void no() { cout << "NO" << endl; exit(0); } inline long long nxt() { long long x; cin >> x; return x; } const long long N = 3e3 + 10, inf = 2e16; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long n = nxt(), a = nxt(), b = nxt(); vector<long long> mas(n + 1); for (int i = 1; i <= n; i++) { mas[i] = nxt(); } vector<long long> arr = {mas[1], mas[1] + 1, mas[1] - 1, mas[n], mas[n] - 1, mas[n] + 1}; vector<long long> factors; for (auto t : arr) { vector<long long> cur = fact(t); for (auto x : cur) { factors.push_back(x); } } sort((factors).begin(), (factors).end()); (factors).resize(unique((factors).begin(), (factors).end()) - (factors).begin()); long long ans = inf; vector<vector<long long>> dp(n + 1, vector<long long>(3)); for (int j = 0; j < 3; j++) { dp[0][j] = 0; } for (int i = 0; i < factors.size(); i++) { long long p = factors[i]; for (int j = 1; j <= n; j++) { long long type; if (mas[j] % p == 0) type = 0; else if ((mas[j] - 1) % p == 0 || (mas[j] + 1) % p == 0) type = 1; else type = 2; if (type == 0) { dp[j][0] = dp[j - 1][0]; dp[j][1] = min(dp[j - 1][0], dp[j - 1][1]) + a; dp[j][2] = min(dp[j - 1][1], dp[j - 1][2]); } if (type == 1) { dp[j][0] = dp[j - 1][0] + b; dp[j][1] = min(dp[j - 1][0], dp[j - 1][1]) + a; dp[j][2] = min(dp[j - 1][1], dp[j - 1][2]) + b; } if (type == 2) { for (int t = 0; t < 3; t++) { dp[j][t] = inf; } dp[j][1] = min(dp[j - 1][1], dp[j - 1][0]) + a; } } for (auto x : dp[n]) { ans = min(ans, x); } } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long inf = 1e9; const long long ninf = LLONG_MIN; const double eps = 1e-12; const long long N = 1000005; const long long LOGN = 19; const double PI = 3.14159265358979323846; long long n, m; long long d[505][505], is[505][505], ini[505][505], last[505][505]; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < 505; i++) for (int j = 0; j < 505; j++) d[i][j] = inf, ini[i][j] = inf; for (int i = 0; i < m; i++) { long long x, y, l; cin >> x >> y >> l; d[x][y] = l; d[y][x] = l; ini[x][y] = l; ini[y][x] = l; is[x][y] = 1; is[y][x] = 1; } for (int i = 1; i <= n; i++) d[i][i] = 0; for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } for (int i = 1; i <= n; i++) { for (int k = 1; k <= n; k++) { for (int j = 1; j <= n; j++) { if (d[i][j] == inf) continue; if (d[i][k] + ini[k][j] == d[i][j]) { last[i][j]++; } } } } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { long long ans = 0; for (int k = 1; k <= n; k++) { if (d[i][k] + d[k][j] == d[i][j]) { ans += last[i][k]; } } cout << ans << " "; } } cout << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[123456], b[123456], c[123456]; int main(void) { int n; scanf("%d", &n); int cnt = 99999999; int ans = 0; for (int i = 0; i < n; i++) { scanf("%d%d", &a[i], &b[i]); cnt = min(cnt, b[i]); ans += a[i] * cnt; } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> int poss(char ch) { if (ch == '3' || ch == '4') return 3; if (ch == '0' || ch == '6' || ch == '9' || ch == '2') return 2; if (ch == '1') return 7; if (ch == '5') return 4; if (ch == '8') return 1; if (ch == '7') return 5; } int main() { char str[3]; gets(str); printf("%d\n", poss(str[0]) * poss(str[1])); return 0; }
1
#include <iostream> #include <cstdio> using namespace std; int main(){ int price,num,sump=0,sum=0,i=0; double ave; while(scanf("%d,%d",&price,&num)!=EOF){ sump += price*num; sum += num; i++; } ave = sum/(double)i; ave += 0.5; cout<<sump<<endl<<(int)ave<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e3 + 5, inf = 2e9, mod = 1e9 + 7; vector<int> prime_Numbers; bool primes[1000005]; double dot(complex<double> a, complex<double> b) { return (conj(a) * b).real(); } double cross(complex<double> a, complex<double> b) { return (conj(a) * b).imag(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; long long acc1[n + 2], acc2[n + 2], acc3[n + 2]; memset(acc3, 0, sizeof acc3); string s, t = "RGB"; cin >> s; for (int j = 0; j < 3; j++) { for (int i = 0; i < s.size(); i++) { if (s[i] != t[(j + i) % 3]) { if (j == 0) { if (i == 0) acc1[i] = 1; else acc1[i] = acc1[i - 1] + 1; } if (j == 1) { if (i == 0) acc2[i] = 1; else acc2[i] = acc2[i - 1] + 1; } if (j == 2) { if (i == 0) acc3[i] = 1; else acc3[i] = acc3[i - 1] + 1; } } else { if (j == 0) { if (i == 0) acc1[i] = 0; else acc1[i] = acc1[i - 1]; } if (j == 1) { if (i == 0) acc2[i] = 0; else acc2[i] = acc2[i - 1]; } if (j == 2) { if (i == 0) acc3[i] = 0; else acc3[i] = acc3[i - 1]; } } } } long long ans = 1e9; acc1[-1] = 0; acc2[-1] = 0; acc3[-1] = 0; for (int i = 0; i <= n - k; i++) { ans = min(acc1[i + k - 1] - acc1[i - 1], min(acc2[i + k - 1] - acc2[i - 1], min(acc3[i + k - 1] - acc3[i - 1], ans))); } cout << ans << endl; } return 0; }
4
#include <stdio.h> #include <algorithm> using namespace std; int main() { int a, b; bool first = true; while (scanf("%d %d", &a, &b), a|b) { if (!first) { puts(""); } first = false; if (a > b) { swap(a, b); } bool output = false; for (int i = a; i <= b; i++) { if (i % 400 == 0 || (i % 100 != 0 && i % 4 == 0)) { output = true; printf("%d\n", i); } } if (!output) { puts("NA"); } } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int d, h, v, e; cin >> d >> h >> v >> e; double vw = (double)v / (3.14159265359L * d * d / 4); vw -= (double)e; double ret = double(h) / (vw); if (ret >= 0) { cout << "YES\n"; cout << ret; } else cout << "NO\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int max(int a, int b, int c) { int x = c + b, y = a + c, z = a + b; if (x <= y && x <= z) cout << x << endl; else if (z <= y && x >= z) cout << z << endl; else if (x >= y && y <= z) cout << y << endl; return 0; } int main() { int n, a = 0, b = 0, c = 0; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; if (x == 1) a++; else if (x == 2) b++; else if (x == 3) c++; } max(a, b, c); return 0; }
1
#include<iostream> #include<map> using namespace std; map<char,char> go,back; string S,T; int main () { cin >> S >> T; bool flag = true; for(int i=0; i<S.size(); i++) { if(go[S[i]] && go[S[i]] != T[i]) flag = false; if(back[T[i]] && back[T[i]] != S[i]) flag = false; go[S[i]] = T[i]; back[T[i]] = S[i]; } cout << ( flag ? "Yes" : "No" ) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, q; int a[1005], d[1005], mark[1005]; vector<int> v[1005]; void solve() { int i, x, y, da, vt, ln, maxx, gt; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= q; i++) { cin >> x >> y; d[x] += a[y]; d[y] += a[x]; v[x].push_back(y); v[y].push_back(x); } da = 0; bool si = true; while (si) { si = false; maxx = 0; vt = -1; for (i = 1; i <= n; i++) { if (mark[i] == 0 && (a[i] > maxx || a[i] == maxx && d[i] < gt)) { si = true; maxx = a[i]; vt = i; gt = d[vt]; } } if (vt > 0) da += d[vt], mark[vt] = 1; ln = v[vt].size(); for (i = 0; i < ln; i++) { if (mark[v[vt][i]] == 0) { d[v[vt][i]] -= a[vt]; } } } cout << da << endl; } int main() { while (cin >> n >> q) solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; char in[200005]; long long bignum[212345], l; long long bigmod(long long c) { long long ret = 0; for (long long i = l; i >= 1; i--) { ret *= 10; ret += bignum[i]; ret %= c; } return ret; } signed main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long c, p, q, ans; cin >> in >> c; l = strlen(in); bignum[l] = 1; p = bigmod(c); for (long long i = 0; i < l; i++) bignum[l - i] = in[i] - '0'; ans = q = bigmod(c); for (long long i = 0; i < l - 1 && ans != 0; i++) { if (in[i] == '0') { q *= 10; q %= c; } else { q -= ((in[i] - '0') * p) % c; q = (q + c) % c; q = (q * 10 + (in[i] - '0')) % c; } if (in[i + 1] != '0') ans = min(q, ans); } cout << ans << "\n"; return 0; }
5
#include <iostream> #include <complex> #include <sstream> #include <string> #include <algorithm> #include <deque> #include <list> #include <map> #include <numeric> #include <queue> #include <vector> #include <set> #include <limits> #include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; #define REP(i, j) for(int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) typedef complex<double> P; int main() { int T; cin >>T; REP(t, T){ string s; cin >>s; int h = atoi(s.substr(0, 2).c_str()); int m = atoi(s.substr(3).c_str()); double d = fabs((h * 30.0 + m * 0.5) - m * 6.0); d = min(d, 360.0 - d); cout <<(d >= 0.0 && d < 30.0 ? "alert" : (d >= 90.0 && d <= 180.0 ? "safe" : "warning")) <<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; double dp[101][101010]; int main() { int a, b; cin >> a >> b; if (b == 1) { cout << 1 << endl; return 0; } vector<int> ms; for (int i = 0; i < a; ++i) { int s; cin >> s; ms.push_back(s); } int msum = accumulate(ms.begin(), ms.end(), 0); vector<vector<double>> dp(a + 1, vector<double>((a + 1) * (b + 1))); dp[0][0] = 1; vector<double> sas((a + 1) * (b + 1)); for (int solved = 0; solved < a; ++solved) { int ams = ms[solved]; fill(sas.begin(), sas.end(), 0); for (int ps = 0; ps < solved * (b) + 1; ++ps) { sas[ps + 1] += (1.0 / (b - 1)) * dp[solved][ps]; sas[ps + ams] += (-1.0 / (b - 1)) * dp[solved][ps]; sas[ps + ams + 1] += (1.0 / (b - 1)) * dp[solved][ps]; sas[ps + b + 1] += (-1.0 / (b - 1)) * dp[solved][ps]; } dp[solved + 1][0] = 0; for (int ps = 0; ps < a * b + 1; ++ps) { dp[solved + 1][ps + 1] = dp[solved + 1][ps] + sas[ps + 1]; } } double ans = 0; for (int s = msum - 1; s > 0; --s) { ans += dp[a][s]; } ans = 1 + ans * (b - 1); cout << setprecision(29) << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, x, y, i, ans = 0; cin >> n >> x >> y; string s; cin >> s; reverse(s.begin(), s.end()); for (i = 0; i < x; ++i) ans += s[i] == (i == y ? '0' : '1'); cout << ans; return 0; }
1
#include <cstdio> #include <iostream> #include <vector> #include <list> #include <cmath> #include <fstream> #include <algorithm> #include <string> #include <queue> #include <set> #include <map> #include <complex> #include <iterator> #include <cstdlib> #include <cstring> #include <sstream> #include <stack> using namespace std; typedef long long ll; typedef pair<int,int> pii; double EPS=1e-10; const int MAX_N=200001; double EQ(double a,double b){ return abs(a-b)<EPS; } void fast_stream(){ std::ios_base::sync_with_stdio(0); } ll A,B,P; // 1baseでs1,...,snの和を計算 class BIT{ private: ll bit[MAX_N]; int n; public: BIT(int sz){ n=sz; memset(bit,0,sizeof(bit)); } ll sum(int i){ ll s=0; while(i>0){ s+=bit[i]; s%=P; i-=i&-i; } return s; } void add(int i,ll x){ while(i<=n){ bit[i]+=x; bit[i]%=P; i+=i&-i; } } }; ll dp[MAX_N]; int main(){ while(cin>>A>>B>>P&&(A|B|P)){ memset(dp,0,sizeof(dp)); vector<pair<string,ll> > v; for(ll i=A;i<=B;i++){ stringstream ss; ss<<i; v.push_back(make_pair(ss.str(),i)); } sort(v.begin(),v.end()); BIT bit(B-A+1); // 辞書順で小さいものから順番に計算 for(int i=0;i<v.size();i++){ ll curNum=v[i].second; // 今回の数字以下のものの和を計算 dp[i]=(bit.sum(curNum-A+1)+1)%P; bit.add(curNum-A+1,dp[i]); } ll res=0; for(int i=0;i<v.size();i++) res=(res+dp[i])%P; cout<<res<<endl; } return 0; }
0
#include <cstdio> #define MAXN 14 int main(void) { long long i,c0,c1,c2,N,m[5]={0}; long long s; char name[MAXN]; const char sign[7]="MARCH"; scanf("%lld",&N); for(i=0;i<N;i++) { scanf("%s",name); for(int j=0;j<5;j++) if(name[0]==sign[j]) m[j]++; } s=0; for(c0=0;c0<5;c0++) for(c1=c0+1;c1<5;c1++) for(c2=c1+1;c2<5;c2++) s+=m[c0]*m[c1]*m[c2]; printf("%lld\n",s); return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[111], b[111], n, m; int gcd(int a, int b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } int main() { while (~scanf("%d%d", &n, &m)) { n += 1; m += 1; for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < m; i++) scanf("%d", &b[i]); int posa, posb; for (posa = 0; a[posa] == 0 && posa < n; posa++) ; for (posb = 0; b[posb] == 0 && posb < m; posb++) ; int ans; if (n - posa < m - posb) { printf("0/1\n"); continue; } if (n - posa > m - posb) { if (a[posa] * b[posb] > 0) printf("Infinity\n"); else printf("-Infinity\n"); continue; } int flag; if (a[posa] * b[posb] > 0) flag = 0; else flag = 1; a[posa] = fabs(a[posa]); b[posb] = fabs(b[posb]); int xx = gcd(a[posa], b[posb]); a[posa] /= xx; b[posb] /= xx; if (flag) printf("-"); printf("%d/%d\n", a[posa], b[posb]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int mxn = 1e5 + 5; const int mod = 1e9 + 7; const int mod1 = 998244353; int main() { ios_base::sync_with_stdio(false); string s, t, x; cin >> s >> t; int n = s.size(), m = t.size(), i; x = t + "#" + s; vector<bool> v(n); vector<int> pi(x.size()); for (i = 1; i < x.size(); i++) { int j = pi[i - 1]; while (j && x[i] - x[j]) j = pi[j - 1]; pi[i] = j + (x[i] == x[j]); if (pi[i] == m) v[i - m - 1] = 1; } vector<int> a(n), p(n), pp(n); for (i = 0; i < n; i++) { if (!v[i]) a[i] = (i ? a[i - 1] : 0); else a[i] = ((i >= m ? pp[i - m] : 0) + i - m + 2) % mod; p[i] = ((i ? p[i - 1] : 0) + a[i]) % mod; pp[i] = ((i ? pp[i - 1] : 0) + p[i]) % mod; } cout << p[n - 1] << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int arr[n]; for (long long int i = 0; i < n; i++) { cin >> arr[i]; } set<long long int> s; for (long long int i = 0; i < n; i++) { if (arr[i] == 0) { continue; } s.insert(arr[i]); } cout << s.size(); }
1
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int sum = 7; for(int i = 1; i<=n; i++){ if(sum%n==0){ cout<<i; return 0; } else sum = (sum*10+7)%n; } cout<<-1; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; while (t--) { int n; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<long long int> turn = a; for (int i = 0; i < n; i++) turn[i]--; for (int i = 1; i < n; i++) turn[i] = turn[i - 1] + turn[i]; for (int i = 0; i < n; i++) { if (turn[i] % 2 == 1) cout << 1 << '\n'; else cout << 2 << '\n'; } } }
2
#include <bits/stdc++.h> using namespace std; int compare(const void *a, const void *b) { if (*(int *)a > *(int *)b) return -1; if (*(int *)a < *(int *)b) return 1; return 0; } int main() { int x = 1; int n, m, *d, *a, l, i, j, *con, up, down, flag, niz, prev; cin >> n >> m; d = new int[n]; for (i = 0; i < n; i++) { cin >> d[i]; } a = new int[m]; con = new int[m]; for (i = 0; i < m; i++) { cin >> a[i]; } l = m; for (i = 0; i < m; i++) { l = l + a[i]; } if (l > n) { cout << -1; return 0; } up = n; down = l; l = (l + n) / 2; prev = -1; flag = 1; while (flag) { niz = n + 1; for (i = 0; i < m; i++) { con[i] = 0; } for (i = 0; i < l; i++) { if (d[i]) con[d[i] - 1] = i; } qsort(con, m, sizeof(int), compare); for (j = 0; j < m; j++) { niz = min(niz - 1, con[j]) - a[j]; } if (l == n && niz < 0) break; if (niz < 0) { down = l; l = (l + up + 1) / 2; } else { prev = l; up = l; l = (l + down) / 2; } if (l == prev) flag = 0; } if (flag == 1) cout << -1 << endl; else { cout << l << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int tree[800001]; pair<int, int> arr[200001]; void build(int node, int start, int end) { if (start == end) { tree[node] = 1; } else { int mid; mid = (start + end) / 2; build(2 * node, start, mid); build(2 * node + 1, mid + 1, end); tree[node] = tree[2 * node] + tree[2 * node + 1]; } } void update(int node, int start, int end, int idx) { if (start == end) { tree[node] = 0; } else { int mid; mid = (start + end) / 2; if (idx <= mid) { update(2 * node, start, mid, idx); } else { update(2 * node + 1, mid + 1, end, idx); } tree[node] = tree[2 * node] + tree[2 * node + 1]; } } int query(int node, int start, int end, int l, int r) { if (l > end or r < start) { return 0; } else if (l <= start and end <= r) { return tree[node]; } else { int mid; mid = (start + end) / 2; int p1 = query(2 * node, start, mid, l, r); int p2 = query(2 * node + 1, mid + 1, end, l, r); return p1 + p2; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; pair<int, int> le[n + 1]; for (int i = 1; i <= n; i++) { cin >> le[i].first >> arr[i].first; le[i].second = i; arr[i].second = i; } sort(arr + 1, arr + n + 1); int loc[n + 1]; for (int i = 1; i <= n; i++) { loc[arr[i].second] = i; } build(1, 1, n); sort(le + 1, le + n + 1); int ans[n + 1]; for (int i = 1; i <= n; i++) { ans[le[i].second] = query(1, 1, n, 1, loc[le[i].second]) - 1; update(1, 1, n, loc[le[i].second]); } for (int i = 1; i <= n; i++) cout << ans[i] << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int x[10001], y[10001], z[10001]; int main() { int n; int x0, y0, z0, x1, y1, z1; scanf("%d", &n); for (int i = 0; i <= n; i++) scanf("%d %d %d", &x[i], &y[i], &z[i]); int vs, vp; int px, py, pz; scanf("%d %d", &vp, &vs); scanf("%d %d %d", &px, &py, &pz); double t = 0; for (int i = 1; i <= n; i++) { double dx = x[i] - x[i - 1], dy = y[i] - y[i - 1], dz = z[i] - z[i - 1]; double t2 = t + sqrt(dx * dx + dy * dy + dz * dz) / vs; dx = px - x[i]; dy = py - y[i]; dz = pz - z[i]; double tp = sqrt(dx * dx + dy * dy + dz * dz) / vp; if (tp > t2 + 1e-11) { t = t2; continue; } double tm = 0, tM = 1, ts; double xs, ys, zs; for (int it = 0; it <= 1000; it++) { double tt = (tm + tM) / 2; xs = x[i - 1] + (x[i] - x[i - 1]) * tt; ys = y[i - 1] + (y[i] - y[i - 1]) * tt; zs = z[i - 1] + (z[i] - z[i - 1]) * tt; dx = px - xs; dy = py - ys; dz = pz - zs; tp = sqrt(dx * dx + dy * dy + dz * dz) / vp; dx = x[i - 1] - xs; dy = y[i - 1] - ys; dz = z[i - 1] - zs; ts = t + sqrt(dx * dx + dy * dy + dz * dz) / vs; if (tp < ts + 1e-11) tM = tt; else tm = tt; } printf("YES\n"); printf("%.9lf\n", tp); printf("%.9lf %.9lf %.9lf\n", xs, ys, zs); return 0; } printf("NO\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; struct nodo { string t; int l, r; }; int N; nodo a[2000005]; int ori[2000005]; int ans[2000005]; void llena(int x, int v) { if (!x) return; if (a[x].t == "IN") { ans[x] = v; return; } llena(a[x].l, v); llena(a[x].r, v); } void dfs2(int x, int v) { if (a[x].t == "IN") { if (a[x].l != v) ans[x] = 1; else ans[x] = 0; return; } if (a[x].t == "OR") { if (v == 1) { if (ori[a[x].l]) llena(a[x].r, 1); else dfs2(a[x].r, 1); if (ori[a[x].r]) llena(a[x].l, 1); else dfs2(a[x].l, 1); } else { if (ori[a[x].l]) llena(a[x].r, 0); else dfs2(a[x].r, 0); if (ori[a[x].r]) llena(a[x].l, 0); else dfs2(a[x].l, 0); } } else if (a[x].t == "XOR") { if (v == 1) { dfs2(a[x].l, !ori[a[x].r]); dfs2(a[x].r, !ori[a[x].l]); } else { dfs2(a[x].l, ori[a[x].r]); dfs2(a[x].r, ori[a[x].l]); } } else if (a[x].t == "AND") { if (v == 1) { if (!ori[a[x].l]) llena(a[x].r, 0); else dfs2(a[x].r, 1); if (!ori[a[x].r]) llena(a[x].l, 0); else dfs2(a[x].l, 1); } else { if (ori[a[x].l]) dfs2(a[x].r, 0); else llena(a[x].r, 1); if (ori[a[x].r]) dfs2(a[x].l, 0); else llena(a[x].l, 1); } } else if (a[x].t == "NOT") dfs2(a[x].l, !v); } void dfs(int x) { if (a[x].t == "IN") { ori[x] = a[x].l; return; } if (a[x].t == "NOT") { dfs(a[x].l); ori[x] = !ori[a[x].l]; } else { dfs(a[x].l); dfs(a[x].r); if (a[x].t == "XOR") ori[x] = (ori[a[x].l] ^ ori[a[x].r]); if (a[x].t == "AND") ori[x] = (ori[a[x].l] & ori[a[x].r]); if (a[x].t == "OR") ori[x] = (ori[a[x].l] | ori[a[x].r]); } } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> N; for (int i = 1; i <= N; i++) { cin >> a[i].t; if (a[i].t == "IN" or a[i].t == "NOT") cin >> a[i].l; else cin >> a[i].l >> a[i].r; } dfs(1); dfs2(1, 1); for (int i = 2; i <= N; i++) if (a[i].t == "IN") cout << ans[i]; return 0; }
4
#include <bits/stdc++.h> using namespace std; inline int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); } int main() { int n, k, g; cin >> n >> k >> g; int maxi = g; n--; while (n--) { int a; cin >> a; g = gcd(g, a); maxi = max(maxi, a); } cout << ((k % g == 0 && k <= maxi) ? "POSSIBLE" : "IMPOSSIBLE") << '\n'; return 0; }
0
#include <iostream> #include <vector> #include <set> using namespace std; int n, m; vector<pair<int, int> > target; set<pair<int, int> > stars; bool containsAll(int dx, int dy) { for (int i = 0; i < m; i++) { pair<int, int>& p = target[i]; if (stars.find(make_pair(p.first + dx, p.second + dy)) == stars.end()) { return false; } } return true; } void solve() { for (int i = 0; i < m; i++) { for (set<pair<int, int> >::iterator it = stars.begin(); it != stars.end(); ++it) { int dx = it->first - target[i].first; int dy = it->second - target[i].second; if (containsAll(dx, dy)) { cout << dx << ' ' << dy << endl; return; } } } } int main() { while (cin >> m, m) { target = vector<pair<int, int> >(m); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; target[i] = make_pair(x, y); } stars.clear(); cin >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; stars.insert(make_pair(x, y)); } solve(); } }
0
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, n, aux, sq; int x; scanf("%d", &x); n = x; long long menor = 999999999999999LL, maior = 0; long long ate = sqrt(n) + 1; for (a = 1; a <= ate; a++) { if (n % a != 0) continue; sq = sqrt(n / a) + 1; for (b = 1; b <= sq; ++b) { if (n % (a * b) != 0) continue; c = n / (a * b); aux = b * c + (a + 1) * c * 2 + ((a + 1) * (b + 2) << 1); if (aux < menor) menor = aux; if (aux > maior) maior = aux; } b = n / a; if (n % (a * b) != 0) continue; c = n / (a * b); aux = b * c + (a + 1) * c * 2 + ((a + 1) * (b + 2) << 1); if (aux < menor) menor = aux; if (aux > maior) maior = aux; } a = n; b = 1; c = 1; aux = b * c + (a + 1) * c * 2 + ((a + 1) * (b + 2) << 1); if (aux < menor) menor = aux; if (aux > maior) maior = aux; printf("%I64d %I64d\n", menor, maior); return 0; }
3
#include <cstdio> #define max(a,b) ((a>b)?a:b) using namespace std; int ops[]={1,-1}; char sop[]={'+','-'},N[4]; bool add(int op){ int num=N[0]-'0'; for(int i=0;i<3;i++){ num+=(N[i+1]-'0')*ops[op%2]; op/=2; } return num==7; } void print(int op){ for(int i=0;i<3;i++){ printf("%c%c",N[i],sop[op%2]); op/=2; } printf("%c=7\n",N[3]); } int main() { scanf("%s",N); for(int i=0;i<8;i++) if(add(i)){ print(i); break; } return 0; }
0
#include <iostream> using namespace std; int main() { int k; while(cin >> k, k) { int sum; sum = 0; for(int i = 0; i < k*(k-1)/2; i++) { int a; cin >> a; sum += a; } cout << sum / (k-1) << endl; } }
0
#include <cstdio> #include <cstring> const int N=500010; char c[N]; int q,l,r,f[N][30],d[N][25],n,j,h,i; int main(){ scanf("%s",c+1); n=strlen(c+1); for(i=1;i<=n;++i)f[i][c[i]-'a']=i; for(i=n;i;--i){ for(j=c[i]+1-'a';j<=26;++j){ if(f[i][j-1])f[i][j]=f[f[i][j-1]+1][j-1]; //printf("f[%d][%d]=%d\n",i,j,f[i][j]); } if(f[i][26])for(j=0;j<c[i]-'a';++j)f[i][j]=f[f[i][26]+1][j]; d[i][0]=f[i][26]; for(h=1;(1<<h)<n;++h)if(d[i][h-1])d[i][h]=d[d[i][h-1]+1][h-1]; } scanf("%d",&q); while(q--){ scanf("%d%d",&l,&r); if(l==r){ puts("No"); continue; } for(i=h;i>=0;--i) if(d[l][i] && d[l][i]<=r)l=d[l][i]+1; if(l==r+1)puts("Yes"); else puts("No"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { vector<int> x(4); cin >> x[0] >> x[1] >> x[2] >> x[3]; if (x[1] - x[0] == x[2] - x[1] && x[3] - x[2] == x[2] - x[1]) { cout << x[3] + (x[1] - x[0]); return 0; } double r = double(x[1]) / double(x[0]); if (r == double(double(x[2]) / double(x[1])) && r == double(double(x[3]) / double(x[2]))) { int res = x[3] * r; if (double(res) == double(x[3] * r)) { cout << res; return 0; } } cout << 42; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct indexed_tree { private: int N; std::vector<long long int> tree; void update_tree(int x, long long int cnt, long long int f(long long int, long long int)) { if (x == 0) return; tree[x] = f(tree[x], cnt); update_tree(x >> 1, cnt, f); } long long int find_tree(int left, int right, long long int f(long long int, long long int)) { int L = left, R = right; long long int res = DEFAULT; while (L <= R) { if (L & 1) res = f(res, tree[L++]); if (!(R & 1)) res = f(res, tree[R--]); L >>= 1, R >>= 1; } return res; } public: long long int DEFAULT; indexed_tree(){}; indexed_tree(long long int size, long long int _DEFAULT) { N = size; DEFAULT = _DEFAULT; tree.resize(size * 2 + 2); for (int i = 1; i <= size * 2 + 1; i++) tree[i] = DEFAULT; } void update(int x, long long int cnt, long long int f(long long int, long long int)) { update_tree(x + N, cnt, f); } long long int find(int left, int right, long long int f(long long int, long long int)) { return find_tree(left + N, right + N, f); } }; int cnt[100050]; long long int max(long long int x, long long int y) { return x > y ? x : y; } int main() { int n, m, a, b; fscanf(stdin, "%d", &n), fscanf(stdin, "%d", &m); for (int i = 0; i < n; i++) fscanf(stdin, "%d", &a), fscanf(stdin, "%d", &b), cnt[a]++, cnt[b + 1]--; for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; int lis[100050]; int lis_rev[100050]; indexed_tree T(100000, 0); for (int i = 1; i <= m; i++) { lis[i] = T.find(0, cnt[i], max) + 1; T.update(cnt[i], lis[i], max); } indexed_tree T_rev(100000, 0); for (int i = m; i >= 1; i--) { lis_rev[i] = T_rev.find(0, cnt[i], max) + 1; T_rev.update(cnt[i], lis_rev[i], max); } int ans = 0; for (int i = 1; i <= m; i++) { if (ans < lis[i] + lis_rev[i] - 1) ans = lis[i] + lis_rev[i] - 1; } printf("%d\n", ans); }
6
#include <bits/stdc++.h> using namespace std; #define PI 4*atan(1) #define INF 1e8 int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; int main(){ int N, A, B, C, X; while(cin >> N >> A >> B >> C >> X, N||A||B||C||X){ vector<int> Y(N); for(int i = 0; i < N; i++){ cin >> Y[i]; } int x = X, index = 0, frame = -1; for(int i = 0; i <= 10000; i++){ if(x == Y[index])index++; if(index == N){ frame = i; break; } x = (A * x + B) % C; } cout << frame << endl; } }
0
#include <bits/stdc++.h> using namespace std; signed main(signed argc, const char *argv[]) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; string ans(n, '0'); vector<pair<long long, long long>> ab(n); map<pair<long long, long long>, long long> ind; for (long long i = 0; i < n; i++) { cin >> ab[i].first; } for (long long i = 0; i < n; i++) { cin >> ab[i].second; } for (long long i = 0; i < n; i++) { ind[ab[i]] = i; } sort((ab).begin(), (ab).end()); vector<long long> pmax(n); vector<long long> smin(n + 1); smin[n] = LLONG_MAX; for (long long i = 0; i < n; i++) { pmax[i] = ab[i].second; if (i) pmax[i] = max(pmax[i], pmax[i - 1]); } for (long long i = n - 1; i >= 0; i--) { smin[i] = min(smin[i + 1], ab[i].second); } long long l = n - 1; for (long long i = n - 2; i >= 0; --i) { if (pmax[i] > smin[i + 1]) { l = i; } else { break; } } for (long long i = l; i < n; i++) { ans[ind[ab[i]]] = '1'; } cout << ans << '\n'; } }
3
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=200005; const int INF=1061109567; int n; int p[N],q[N]; int ida[N],cnta; int idb[N],cntb; struct Edge { int to,flow; int next; }edge[N<<1]; int head[N],cnt=1; void add_edge(int u,int v,int flow) { cnt++; edge[cnt].to=v; edge[cnt].flow=flow; edge[cnt].next=head[u]; head[u]=cnt; return; } void add(int u,int v,int flow) { add_edge(u,v,flow); add_edge(v,u,0); return; } int s,t; int dep[N]; bool bfs() { memset(dep,-1,sizeof(dep)); queue<int>q; q.push(s); dep[s]=0; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i;i=edge[i].next) { int v=edge[i].to; if(dep[v]!=-1||edge[i].flow<=0) continue; dep[v]=dep[u]+1; q.push(v); } } return dep[t]!=-1; } int cur[N]; int dfs(int u,int flow) { if(u==t) return flow; int used=0; for(int &i=cur[u];i;i=edge[i].next) { int v=edge[i].to; if(dep[v]!=dep[u]+1||edge[i].flow<=0) continue; int now=dfs(v,min(flow,edge[i].flow)); flow-=now; edge[i].flow-=now; edge[i^1].flow+=now; used+=now; if(flow==0) break; } if(used==0) dep[u]=-1; return used; } int dinic() { int res=0; while(bfs()) { memcpy(cur,head,sizeof(head)); res+=dfs(s,INF); } return res; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&p[i]),p[i]++; for(int i=1;i<=n;i++) scanf("%d",&q[i]),q[i]++; for(int i=1;i<=n;i++) if(!ida[i]) { cnta++; ida[i]=cnta; int u=p[i]; while(u!=i) ida[u]=cnta,u=p[u]; } cntb=cnta; for(int i=1;i<=n;i++) if(!idb[i]) { cntb++; idb[i]=cntb; int u=q[i]; while(u!=i) idb[u]=cntb,u=q[u]; } s=0,t=cntb+1; int ans=n; for(int i=1;i<=n;i++) if(p[i]==i&&q[i]==i) ans--; else if(p[i]==i&&q[i]!=i) add(s,idb[i],1); else if(p[i]!=i&&q[i]==i) add(ida[i],t,1); else if(p[i]==q[i]&&p[i]!=i) add(ida[i],idb[i],1),add(idb[i],ida[i],1); else add(ida[i],idb[i],1); ans-=dinic(); printf("%d",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, L[400010], R[400010], D[400010], Head[400010], Next[400010], Go[400010], Cnt = 0; double Val[400010], O[400010]; vector<int> big[400010]; void addedge(int x, int y) { Go[++Cnt] = y; Next[Cnt] = Head[x]; Head[x] = Cnt; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf", &Val[i]); for (int i = 1; i < n; i++) { scanf("%d%d", &L[i], &R[i]); D[L[i]]++; D[R[i]]++; addedge(L[i], R[i]); addedge(R[i], L[i]); } for (int i = 0; i < n; i++) if (D[i] * D[i] > n) { for (int T = Head[i]; T; T = Next[T]) big[Go[T]].push_back(i); } int q; scanf("%d", &q); double ans = 1; for (int j = 0; j < n; j++) { ans += (D[j] - 1) * Val[j]; } for (int j = 1; j < n; j++) ans -= Val[L[j]] * Val[R[j]]; for (int i = 0; i < n; i++) if (D[i] * D[i] <= n) for (int T = Head[i]; T; T = Next[T]) O[Go[T]] -= Val[i]; for (int i = 1; i <= q; i++) { int a; double b; scanf("%d%lf", &a, &b); if (D[a] * D[a] <= n) { for (int T = Head[a]; T; T = Next[T]) O[Go[T]] += Val[a]; } for (int j = 0; j < big[a].size(); j++) ans += Val[a] * Val[big[a][j]]; ans -= O[a] * Val[a]; ans -= (D[a] - 1) * Val[a]; Val[a] = b; ans += O[a] * Val[a]; ans += (D[a] - 1) * Val[a]; if (D[a] * D[a] <= n) { for (int T = Head[a]; T; T = Next[T]) O[Go[T]] -= Val[a]; } for (int j = 0; j < big[a].size(); j++) ans -= Val[a] * Val[big[a][j]]; printf("%.5lf\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 22; char ss[N][N]; int n, m; int check(int x, int y) { if (x < 0 || y < 0 || x >= n || y >= m) return 0; return 1; } int mp[N * 3][N * 3][N * 3][N * 3], ed = 20; int que(int x1, int x2, int y1, int y2) { return mp[x1][x2][y1][y2]; } void up(int x1, int x2, int y1, int y2, int w) { mp[x1][x2][y1][y2] = w; } int dfs(int x1, int x2, int y1, int y2) { int sta[N * N], top = 0; if (x1 > x2 || y1 > y2) return 0; int tmp = que(x1, x2, y1 + ed, y2 + ed); if (tmp != -1) return tmp; for (int i = x1; i <= x2; i += 2) { for (int j = y1; j <= y2; j += 2) if ((i + j) % 2 == 0) { int x = (i + j) / 2, y = (i - j) / 2; if (!check(x, y)) continue; if (ss[x][y] == 'L') { sta[top++] = dfs(x1, i - 2, y1, y2) ^ dfs(i + 2, x2, y1, y2); } else if (ss[x][y] == 'R') { sta[top++] = dfs(x1, x2, y1, j - 2) ^ dfs(x1, x2, j + 2, y2); } else { sta[top++] = dfs(x1, i - 2, y1, j - 2) ^ dfs(i + 2, x2, y1, j - 2) ^ dfs(x1, i - 2, j + 2, y2) ^ dfs(i + 2, x2, j + 2, y2); } } } sort(sta, sta + top); top = unique(sta, sta + top) - sta; sta[top] = top + 1; for (int i = 0; i <= top; i++) if (sta[i] != i) { up(x1, x2, y1 + ed, y2 + ed, i); return i; } } int main() { while (scanf("%d%d", &n, &m) != EOF) { for (int i = 0; i < n; i++) { scanf("%s", ss[i]); } int x1 = 0, x2 = n - 1 + m - 1; int y1 = 0 - (m - 1), y2 = n - 1 - 0; memset(mp, -1, sizeof(mp)); int a, b; a = dfs(x1, x2 - x2 % 2, y1 - y1 % 2, y2 - y2 % 2); b = dfs(1, x2 - (x2 + 1) % 2, y1 - (y1 - 1) % 2, y2 - (y2 + 1) % 2); if (a ^ b) { printf("WIN\n"); } else { printf("LOSE\n"); } } return 0; }
4
#include <cstdio> const int N=41; int a[N],mx=-1,t; long long n; int main(){ scanf("%lld",&n); for(int i=40;~i;--i) if((n>>i)&1) if(~mx)a[i]=mx+(++t); else mx=i; printf("%d\n",mx+mx+2+t+t); if(a[0])printf("%d ",a[0]); for(int i=1;i<=mx;++i){ printf("%d ",i); if(a[i])printf("%d ",a[i]); } for(int i=1;i<=mx+t;++i)printf("%d ",i); printf("%d %d\n",mx+t+1,mx+t+1); }
0
#include <bits/stdc++.h> using namespace std; int n, pre[1000001]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) pre[i] = i ^ pre[i - 1]; int res = 0; for (int i = n; i > 0; --i) if ((n - i) % 2) res ^= i; for (int i = 1; i <= n; ++i) { if ((n - i) / i % 2) res ^= pre[i - 1]; res ^= pre[(n - i) % i]; } for (int i = 0, x; i < n; ++i) { scanf("%d", &x); res ^= x; } printf("%d\n", res); return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m; int main() { while (cin >> n >> m) { int tt = min(n + 1, m + 1); cout << tt << endl; for (int i = 0; i < tt; ++i, --m) { cout << i << " " << m << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 2e2 + 10; int t, n, a[N], ans[N]; void solve() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { int k = i, t = a[i], cnt = 0; while (t - 1 != i) { cnt++; k = t - 1; t = a[k]; } ans[i] = ++cnt; } for (int i = 0; i < n; i++) cout << ans[i] << ' '; cout << endl; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> t; while (t--) solve(); }
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int maxn = 110; int main() { int n, d; int a[maxn], x[maxn], y[maxn]; int dist[maxn][maxn], cost[maxn][maxn]; scanf("%d%d", &n, &d); a[1] = a[n] = 0; for (int i = 2; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dist[i][j] = dist[j][i] = abs(x[i] - x[j]) + abs(y[i] - y[j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cost[i][j] = d * dist[i][j] - a[j]; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i == j) continue; if (i == k) continue; if (j == k) continue; if (cost[i][k] + cost[k][j] < cost[i][j]) cost[i][j] = cost[i][k] + cost[k][j]; } int ans = cost[1][n]; if (ans < 0) ans = 0; printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int arr[101][101]; int main() { int n; cin >> n; int plus = 0; int minus = 0; for (int i = 0; i < n; i++) { int x; int y; cin >> x; cin >> y; if (x > 0) plus++; else minus++; if (plus > 1 && minus > 1) { cout << "No"; return 0; } } cout << "Yes"; return 0; }
1
#include <bits/stdc++.h> using namespace std; bool cmpa(pair<int, int>& a, pair<int, int>& b) { return a.first < b.first; } bool cmpd(pair<int, int>& a, pair<int, int>& b) { return a.first > b.first; } const int T = 1005; int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<vector<pair<int, int>>> v(T); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v[a / 1000].emplace_back(make_pair(b, i)); } bool b = true; for (int i = 0; i < T; i++) { if (b) sort(v[i].begin(), v[i].end(), cmpa); else sort(v[i].begin(), v[i].end(), cmpd); b = !b; } for (int i = 0; i < T; i++) { for (int j = 0; j < v[i].size(); j++) { cout << v[i][j].second + 1 << ' '; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; set<string> Set; int main() { string s; cin >> s; int n = s.size(); for (int i = 0; i < n; i++) { s = s.substr(1, n - 1) + s.substr(0, 1); Set.insert(s); } printf("%d\n", Set.size()); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<int> a; vector<int> b; set<int> c; int n, m; pair<int, int> dichSeach(int e) { int i = lower_bound(a.begin(), a.end(), e) - a.begin(); int p = lower_bound(b.begin(), b.end(), e) - b.begin(); return make_pair((n - i) * 3 + i * 2, p * 2 + (m - p) * 3); } int main() { int ax, bx; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &ax); a.push_back(ax); c.insert(ax); c.insert(ax + 1); } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &bx); b.push_back(bx); c.insert(bx); c.insert(bx + 1); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); set<int>::iterator si = c.begin(); pair<int, int> pi = dichSeach(*si), Maxpi = pi; int sub = pi.first - pi.second; while (si != c.end()) { pi = dichSeach(*si); if (pi.first - pi.second > sub) { sub = pi.first - pi.second; Maxpi = pi; } si++; } printf("%d:%d", Maxpi.first, Maxpi.second); return 0; }
3
#include <iostream> #include <string> #include <queue> using namespace std; struct cha{ string name; queue<int> q; int toku; }; int main() { int n; while (cin >> n) { if (n == 0) { break; } int d[30]; for (int i = 0; i < 30; i++) { d[i] = n+1; } cha c[20]; int a, b; for (int i = 0; i < n; i++) { cin >> c[i].name; cin >> a; for (int j = 0; j < a; j++) { cin >> b; d[b]--; (c[i].q).push(b); } } cha max; for (int i = 0; i < n; i++) { c[i].toku = 0; int v; while (!(c[i].q).empty()) { v = (c[i].q).front(); (c[i].q).pop(); c[i].toku += d[v]; } if (i == 0) { max.name = c[i].name; max.toku = c[i].toku; } else if (max.toku > c[i].toku) { max.name = c[i].name; max.toku = c[i].toku; } else if (max.toku == c[i].toku) { if (max.name > c[i].name) { max.name = c[i].name; } } } cout << max.toku << " " << max.name << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return f * x; } const int maxn = 2e5 + 7; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; int n; int p[maxn], a[maxn]; long long node[maxn << 2], lazy[maxn << 2]; void push_down(int l, int r, int now) { int m = (l + r) / 2; long long &d = lazy[now]; node[now << 1] += d, node[now << 1 | 1] += d; lazy[now << 1] += d, lazy[now << 1 | 1] += d; d = 0; } void update(int x, int y, int d, int l, int r, int now) { if (x <= l && r <= y) { node[now] += d, lazy[now] += d; return; } if (lazy[now]) push_down(l, r, now); int m = (l + r) / 2; if (x <= m) update(x, y, d, l, m, now << 1); if (y > m) update(x, y, d, m + 1, r, now << 1 | 1); node[now] = min(node[now << 1], node[now << 1 | 1]); } int main() { n = read(); for (int i = 1; i <= n; ++i) p[i] = read() + 1; for (int i = 1; i <= n; ++i) a[i] = read(); for (int i = 1; i <= n; ++i) update(p[i], n + 1, a[i], 1, n + 1, 1); long long ans = 9e18; for (int i = 1; i < n; ++i) { update(p[i], n + 1, -a[i], 1, n + 1, 1); update(1, p[i] - 1, a[i], 1, n + 1, 1); ans = min(ans, node[1]); } printf("%lld\n", ans); }
5
#include <bits/stdc++.h> using namespace std; long long a[505][505]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%lld", &a[i][j]); } } if (n == 1) { printf("1\n"); return 0; } int fg = 0; long long sm = 0; for (int i = 0; i < n; i++) { if (a[0][i] == 0) { fg = 1; break; } sm += a[0][i]; } if (fg == 1) { sm = 0; for (int i = 0; i < n; i++) { sm += a[1][i]; } } int x, y; fg = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (a[i][j] == 0) { x = i; y = j; fg = 1; i = n; break; } } } if (fg == 0) { printf("-1\n"); return 0; } long long fll1, fll2; long long sm1 = 0, sm2 = 0; for (int i = 0; i < n; i++) { sm1 += a[i][y]; } for (int i = 0; i < n; i++) { sm2 += a[x][i]; } fll1 = sm - sm1; fll2 = sm - sm2; if (fll1 != fll2) { printf("-1\n"); return 0; } a[x][y] = fll1; int flg = 0; long long smd1 = 0, smd2 = 0; for (int i = 0; i < n; i++) { smd1 += a[i][i]; smd2 += a[i][n - i - 1]; } if (smd1 != sm || smd2 != sm) flg = 1; for (int i = 0; i < n; i++) { long long sum1 = 0, sum2 = 0; for (int j = 0; j < n; j++) { sum1 += a[i][j]; sum2 += a[j][i]; } if (sum1 != sm || sum2 != sm) { flg = 1; break; } } if (flg == 1) printf("-1\n"); else { if (fll1 <= 0) fll1 = -1; printf("%lld\n", fll1); } return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; const int mod = 998244353; const int maxn = 2e5 + 10; struct edge { int st, ed; int w; bool operator<(const edge &b) const { return w < b.w; } edge() {} edge(int st, int ed, int w) : st(st), ed(ed), w(w) {} }; edge e[maxn]; int vet[805]; ll dis[805][805]; int temp[805], cnt; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; ++i) { int x, y, w; scanf("%d %d %d", &x, &y, &w); e[i] = edge(x, y, w); } sort(e, e + m); for (int i = 0; i < min(m, k); ++i) { temp[cnt++] = e[i].st; temp[cnt++] = e[i].ed; } sort(temp, temp + cnt); cnt = unique(temp, temp + cnt) - temp; memset(dis, 0x3f, sizeof(dis)); for (int i = 0; i < min(m, k); ++i) { int x, y; x = lower_bound(temp, temp + cnt, e[i].st) - temp; y = lower_bound(temp, temp + cnt, e[i].ed) - temp; dis[x][y] = min(dis[x][y], (ll)e[i].w); dis[y][x] = dis[x][y]; } for (int a = 0; a < cnt; ++a) { for (int i = 0; i < cnt; ++i) { for (int j = i + 1; j < cnt; ++j) { dis[i][j] = min(dis[i][j], dis[i][a] + dis[a][j]); dis[j][i] = dis[i][j]; } } } vector<ll> ans; for (int i = 0; i < cnt; ++i) { for (int j = i + 1; j < cnt; ++j) ans.push_back(dis[i][j]); } sort(ans.begin(), ans.end()); printf("%lld\n", ans[k - 1]); }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int k; string s; cin >> k >> s; int n = s.size(); set<char> se; for (char c = 'a'; c < char('a' + k); c++) se.insert(c); bool flag = true; vector<int> a; for (int i = 0; i < n / 2; i++) { se.erase(s[i]); se.erase(s[n - i - 1]); if (s[i] == '?' && s[n - i - 1] == '?') a.emplace_back(i); if (s[i] != s[n - i - 1] && (s[i] == '?' || s[n - i - 1] == '?')) { (s[i] == '?' ? (s[i] = s[n - i - 1]) : (s[n - i - 1] = s[i])); } flag &= s[i] == s[n - i - 1]; } if (n & 1) { se.erase(s[n / 2]); if (s[n / 2] == '?') a.emplace_back(n / 2); } flag &= a.size() >= se.size(); int cnt = a.size(); for (int i : a) { s[i] = s[n - i - 1] = (cnt > se.size() ? 'a' : *begin(se)); if (cnt <= se.size()) se.erase(begin(se)); cnt--; } int d = 0; bool bar = true; for (int i = 0; i <= n / 2; i++) { bar &= s[i] == s[n - i - 1]; d |= 1 << (s[i] - 'a'); d |= 1 << (s[n - i - 1] - 'a'); } bar &= d == (1 << k) - 1; flag &= bar; cout << (flag ? s : "IMPOSSIBLE"); return 0; }
3
#include <bits/stdc++.h> struct node { int v, len; node *next; } * head[10100], tree[10100]; int n, m, dp[10100][2], cnt; int high, low, mid, len; int ptr, vis[10100], ans, num[10100]; void Initial() { ans = 10000001, len = 0, ptr = 1; memset(head, NULL, sizeof(head)); } void AddEdge(int a, int b) { tree[ptr].v = b, tree[ptr].len = 0; tree[ptr].next = head[a], head[a] = &tree[ptr++]; tree[ptr].v = a, tree[ptr].len = 1; tree[ptr].next = head[b], head[b] = &tree[ptr++]; } int Dfs_Up(int v, int pa) { int u, sum = 0; node *p = head[v]; while (p != NULL) { u = p->v; if (u != pa) sum += Dfs_Up(u, v) + p->len; p = p->next; } return sum; } void Tree_DP(int v, int fa, int cur, int up) { int i, j, k, u, tpk; node *p = head[v]; for (j = 0; j <= 1; ++j) for (k = 0; k <= 1; ++k) { if (j == 0 && k == 1) continue; dp[v][j] = (dp[v][j]) < (dp[fa][k] + (cur != j)) ? (dp[v][j]) : (dp[fa][k] + (cur != j)); tpk = dp[v][j] + cnt - up; ans = (ans) < (tpk) ? (ans) : (tpk); } while (p != NULL) { u = p->v; if (u != fa) Tree_DP(u, v, p->len, up + p->len); p = p->next; } } int main() { int i, j, k, a, b, c; while (scanf("%d", &n) != EOF) { Initial(); for (i = 1; i < n; ++i) { scanf("%d%d", &a, &b); AddEdge(a, b); } for (i = 1; i <= n; ++i) { cnt = Dfs_Up(i, -1); ans = (ans) < (cnt) ? (ans) : (cnt); for (j = 1; j <= n; ++j) dp[j][0] = dp[j][1] = 10000001; dp[i][0] = dp[i][1] = 0; node *p = head[i]; while (p != NULL) { int u = p->v; Tree_DP(u, i, p->len, p->len); p = p->next; } } printf("%d\n", ans); } return 0; }
5
#include <bits/stdc++.h> bool dfs_topo(std::vector<std::vector<int> > &adjacent, std::vector<int> &des, int node, std::list<int> &topo_sort) { des[node] = 0; for (int x : adjacent[node]) { if (des[x] == 0) return false; else if (des[x] == -1) { dfs_topo(adjacent, des, x, topo_sort); } } topo_sort.push_front(node); des[node] = 1; return true; } bool dfs(std::vector<std::vector<int> > &adjacent, std::list<int> &topo_sort) { int n = adjacent.size(); std::vector<int> des(n); for (int i = 0; i < n; i++) des[i] = -1; for (int i = 0; i < n; i++) { if (des[i] == -1) { if (dfs_topo(adjacent, des, i, topo_sort) == false) return false; } } return true; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); long long int n; std::cin >> n; std::vector<std::string> array(n); for (int i = 0; i < n; i++) std::cin >> array[i]; std::vector<std::vector<int> > adjacent(26); std::vector<std::vector<int> > adjacent_2(26); bool done = true; for (int i = 1; i < n; i++) { int j = 0; done = false; while (j < array[i - 1].size() && j < array[i].size()) { if (array[i - 1][j] != array[i][j]) { adjacent[(int)(array[i - 1][j]) - 97].push_back(((int)array[i][j]) - 97); for (int k = 0; k < adjacent_2[(int)(array[i - 1][j]) - 97].size(); k++) { adjacent[adjacent_2[(int)(array[i - 1][j]) - 97][k]].push_back( ((int)array[i][j]) - 97); } for (int k = 0; k < adjacent[(int)(array[i][j]) - 97].size(); k++) { adjacent[(int)(array[i - 1][j]) - 97].push_back( adjacent[(int)(array[i][j]) - 97][k]); } adjacent_2[((int)array[i][j]) - 97].push_back((int)(array[i - 1][j]) - 97); done = true; break; } j++; } if (done == false && array[i - 1].size() > array[i].size()) { break; } else if (done == false) done = true; } if (done) { std::list<int> array_123; bool ok = dfs(adjacent, array_123); if (ok) for (int x : array_123) std::cout << ((char)(x + 97)); else std::cout << "Impossible"; std::cout << '\n'; } else { std::cout << "Impossible\n"; } }
3
#include <bits/stdc++.h> using namespace std; int n, m, s, t; vector<int> g[1005]; int dis_s[1005], dis_t[1005]; bool vis[1005]; bool adj[1005][1005]; void bfs(int v, int dis[]) { queue<int> q; vis[v] = 1; dis[v] = 0; q.push(v); while (q.size()) { v = q.front(); q.pop(); for (int u : g[v]) { if (vis[u]) continue; dis[u] = dis[v] + 1; vis[u] = 1; q.push(u); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0), cout.precision(15); cin >> n >> m >> s >> t; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); adj[u][v] = 1; adj[v][u] = 1; } memset(vis, 0, sizeof(vis)); bfs(s, dis_s); memset(vis, 0, sizeof(vis)); bfs(t, dis_t); int ans = 0; for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (!adj[i][j] && min(dis_s[i] + dis_t[j] + 1, dis_s[j] + dis_t[i] + 1) >= dis_s[t]) ++ans; } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long n; cin >> n; long a[n]; for (long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); a[n - 1] = (a[n - 1] == 1) ? 2 : 1; sort(a, a + n); for (long i = 0; i < n; i++) cout << a[i] << " "; }
1
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(const T &a) { return a * a; } template <class T> inline int len(const T &c) { return static_cast<int>(c.size()); } template <class T> inline bool maximize(T &r, const T c) { if (r < c) { r = c; return true; } return false; } template <class T> inline bool minimize(T &r, const T c) { if (r > c) { r = c; return true; } return false; } const long double EPS = 1e-9; const long double PI = 2 * acos(0.0); const int N = 100; void Add(map<pair<pair<int, int>, pair<int, int>>, vector<int>> &m, pair<pair<int, int>, pair<int, int>> from, int to) { if (m.count(from)) { m[from].push_back(to); } else { m[from] = {to}; } } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); map<pair<pair<int, int>, pair<int, int>>, vector<int>> dia; vector<pair<int, int>> sensors; for (int i = 0; i < k; ++i) { int x, y; scanf("%d%d", &x, &y); sensors.push_back({x, y}); int lf = max(0, x - y); int rg = min(n, n - ((n - x) - (m - y))); int ly = y - (x - lf); int ry = y + (rg - x); Add(dia, {{lf, ly}, {rg, ry}}, i); lf = max(0, x - (m - y)); rg = min(n, n - ((n - x) - y)); ly = y + (x - lf); ry = y - (rg - x); Add(dia, {{lf, ly}, {rg, ry}}, i); } set<pair<int, int>> used; pair<int, int> cur{0, 0}; pair<int, int> dir{1, 1}; vector<long long> ans(k, -1); int found = 0; long long timer = 0; while (!used.count(cur) && found < k) { used.insert(cur); int dt = 1e9; if (dir.first < 0) { minimize(dt, cur.first); } else { minimize(dt, n - cur.first); } if (dir.second < 0) { minimize(dt, cur.second); } else { minimize(dt, m - cur.second); } pair<int, int> next{cur.first + dir.first * dt, cur.second + dir.second * dt}; pair<int, int> lf = min(cur, next); pair<int, int> rg = max(cur, next); for (int sid : dia[{lf, rg}]) { if (ans[sid] == -1) { int dx = cur.first - sensors[sid].first; int dy = cur.second - sensors[sid].second; ans[sid] = timer + min(abs(dx), abs(dy)); ++found; } } bool y_refl = next.second == m || next.second == 0; bool x_refl = next.first == 0 || next.first == n; if (x_refl && y_refl) { break; } if (x_refl) dir.first *= -1; if (y_refl) dir.second *= -1; timer += abs(next.first - cur.first); cur = next; } for (long long x : ans) { printf("%lld\n", x); } return 0; }
3
#include <bits/stdc++.h> using namespace std; char arr[150] = {'a'}; int main() { int n; scanf("%d", &n); int a = 0, b = 0, c = 0, d = 0, k = 0, count = 0; scanf("%s", arr); while (k < n) { if (arr[k] == 'L') a += 1; else if (arr[k] == 'R') b += 1; else if (arr[k] == 'U') c += 1; else if (arr[k] == 'D') d += 1; ++k; if (k == n) { count = min(a, b); a = min(c, d); } } cout << 2 * count + 2 * a << endl; return 0; }
2