solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; string Input; bool judgeResult[8][5]; void solve() { int j, k, len; string tmp; len = Input.size(); tmp = ""; for (int i = 0; i < 5 - len; i++) { tmp += "0"; } Input = tmp + Input; len = Input.size(); //cout << Input << endl; for (int i = len - 1; i > -1; i...
0
#include <bits/stdc++.h> using namespace std; void Add(string &s, int i) { string tmp1 = ""; string tmp2 = ""; for (int k = 0; k <= i + 1; k++) tmp1 += s[k]; if (i == s.length() - 2) s = tmp1; else { for (int k = i + 2; k < s.length(); k++) tmp2 += s[k]; s = tmp1 + ',' + tmp2; } } void Sub(strin...
2
#include <bits/stdc++.h> using namespace std; void FIO() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); } inline int D() { int t; scanf("%d", &t); return t; } inline long long LLD() { long long t; scanf("%lld", &t); return t; } long long gcd(long long a, long long b) { return b ? gcd(b, a %...
6
#include <bits/stdc++.h> using namespace std; long long bigmod(long long b, long long p, long long md) { if (p == 0) return 1; if (p % 2 == 1) { return ((b % md) * bigmod(b, p - 1, md)) % md; } else { long long y = bigmod(b, p / 2, md); return (y * y) % md; } } int main() { long long k, a, b; ci...
3
#include <bits/stdc++.h> using namespace std; pair<long long int, long long int> count_white(long long int n, long long int m) { return {(long long int)(ceil((n * m) / 2.)), (long long int)(floor((n * m) / 2.))}; } pair<long long int, long long int> count_black...
3
#include<cstdio> #include<algorithm> using namespace std; int n; long long c,ans,sum[1000005],val[1000005],v[1000005],id[1000005]; void calc(){ for (int i=n; i>=1; i--){ sum[i]=sum[i+1]+v[i]; val[i]=sum[i]-(c-id[i]); } for (int i=n; i>=1; i--) val[i]=max(val[i],val[i+1]); long long ss=0; for (int i=1; i<=n; ...
0
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; bool judge(string s, bool head) { if (s.empty()) return true; char c = s[0]; bool ff = true, fff = true, ffff = true; string ss = s, sss = s, ssss = s; for (int i = 0; i < 3; i++){ if (ss.find(c) == -1){ ff = f...
0
#include <bits/stdc++.h> using namespace std; const int N_MAX = 4000000 + 10; int N, M, nxt[N_MAX], pre[N_MAX]; long long a[N_MAX]; namespace Ninit { void init() { scanf("%d%d", &N, &M); for (int i = 1; i <= N; ++i) scanf("%I64d", &a[i]); } } // namespace Ninit namespace Nwork { long long A[N_MAX]; int tmp, num, u...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 200; int n, a[maxn], l[maxn], r[maxn]; vector<int> ans; int main() { while (scanf("%d", &n) != EOF) { for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]); for (int i = (1); i <= (n); ++i) { int cur = i; while (cur > 1 && a[cur - ...
4
#include <bits/stdc++.h> using namespace std; priority_queue<long long int, vector<long long int>, greater<long long int> > ti; vector<long long int> p[200005], y[300005], f(2000005, 0); long long int vis[100005], b[100005]; map<pair<long long int, long long int>, long long int> mp; set<pair<long long int, long lon...
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i, c; string s; string ss = "ogo"; cin >> n; cin >> s; c = 0; for (i = 0; i < n; i++) { if (s.substr(i, 3) == ss) { c++; i++; } else { if (c > 0) { cout << "***"; c = 0; } else { cout ...
1
#include <bits/stdc++.h> using namespace std; template <int m> struct modint { unsigned x; modint() : x(0) {} modint(long long arg) { arg %= m; if (arg < 0) { x = arg + m; } else { x = arg; } } modint& operator+=(const modint& other) { x += other.x; if (x >= m) { x -=...
6
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; scanf("%lld", &n); scanf("%lld", &k); long long ans[n]; vector<pair<long long, pair<long long, long long>>> v(n); for (long long i = 0; i < n; i++) { long long x; scanf("%lld", &x); v[i].first = x; } for (long long i ...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1100; const int mod = 1e9 + 7; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int n, m, D[maxn][maxn], DD[maxn][maxn]; pair<int, int> st, e; vector<string> grid; bool valid(int first, int second) { return first >= 0 && first < n && second >= 0 && sec...
2
#include<cstdio> #include<vector> #include<queue> #include<string> #include<algorithm> #include<functional> #include<map> #include<cstring> #include<iostream> using namespace std; typedef pair<int,int> P; typedef pair<int,P> PP; int fie[21][21]; int dp[21][21][1<<10]; int w,h; int rx,ry; int sx[4]={1,0,-1,0}; int sy[4...
0
#include <bits/stdc++.h> using namespace std; int n, m, k, x; vector<int> adj[100010]; long long dp[100005][12][3]; void dfs(int src, int prv) { dp[src][0][0] = (k - 1); dp[src][1][1] = 1; dp[src][0][2] = (m - k); for (int i = 0; i < adj[src].size(); i++) { if (adj[src][i] == prv) continue; int tmp = ad...
3
#include <bits/stdc++.h> using namespace std; int n, a; int main() { scanf("%d %d", &n, &a); if (a % 2 == 1) { printf("%d", (a - 1) / 2 + 1); return 0; } if (a % 2 == 0) { printf("%d", n / 2 + 1 - a / 2); return 0; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = (int)1205; struct P { int l, r; }; int n; int dp[N][N]; int st[N][N]; int lb[N], rb[N]; char res[N]; int solve(int l, int r) { if (l > r) return 1; if (dp[l][r] != -1) return dp[l][r]; for (int md = lb[l] / 2; md <= min((rb[l] - 1) / 2, r - l); md++) {...
5
#include <bits/stdc++.h> using namespace std; void Do(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; } else { int dx, dy; Do(b, a % b, dx, dy); x = dy; y = dx - a / b * dy; } } int main() { int N; scanf("%d", &N); map<int, int> Mx; int tn = N; for (int i = 2; i * i <...
6
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const int MOD = 998244353; const int N = 2e5 + 5; int t, n, a[N], f[N]; vector<int> p; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 0; i <= n; ++i) f[i] = 0; p....
5
#include <bits/stdc++.h> using namespace std; long long n, d, b, i, j, k, t1, t2, x1, x2; long long a[100005], s[100005]; int main() { scanf("%I64d%I64d%I64d", &n, &d, &b); for (i = 1; i <= n; i++) { scanf("%I64d", &a[i]); s[i] = s[i - 1] + a[i]; } for (i = 1; i <= n / 2; i++) { x1 = s[min(n, i + d ...
6
#include <bits/stdc++.h> using namespace std; const int MN = 1e3 + 3, mod = 1e9 + 7; int dp[MN], i, j, x, n, tot, ans; int main() { for (scanf("%d", &n), i = 1; i <= n; i++) { scanf("%d", &x); tot = 0; dp[i] = 1; for (j = i; j >= x; j--) tot = (tot + dp[j]) % mod; dp[i] = (dp[i] + tot) % mod; ...
4
#include<bits/stdc++.h> const int N = 200010; typedef long long ll; typedef std::pair <ll, ll> pii; struct jewel{ ll value, id; bool ispair; bool operator < (const jewel &j)const{ if (value != j.value){ return value > j.value; } return ispair > j.ispair; } }; ...
0
#include<stdio.h> int main(void) { int n,m,A[100001],B,C[100001],sum=1,sum1=0,i; scanf("%d%d",&n,&m); for(i=1;i<n;i++){ C[i]=0; } for(i=1;i<n;i++){ scanf("%d",&A[i]); C[i+1]=C[i]+A[i]; } for(i=1;i<=m;i++){ scanf("%d",&B); if(sum<sum+B){ sum1+=C[sum+B]-C[sum]; } else{ sum1+=C[sum]-C[sum+B]; ...
0
#include <iostream> #include <algorithm> #include <cassert> #include <cctype> #include <cstdio> #include <math.h> #include <map> #include <queue> #include <string> using namespace std; int n[100000],s[100000],m; int main(){ int c=0; while(scanf("%d,%d",&n[c],&s[c])){ if(n[c]+s[c]==0)break; c++; } int p=1; fo...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int sum = 0; string s; while (n--) { int x, y; cin >> x >> y; if (sum + x <= 500) { sum += x; s += 'A'; } else { sum -= y; s += 'G'; } } cout << s; return 0; }
2
#include <bits/stdc++.h> int main() { int n; while (~scanf("%d", &n)) { if (n <= 10 || n > 21) printf("0\n"); else if (n == 20) printf("15\n"); else printf("4\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, res[111]; pair<int, int> a[111]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { int x; cin >> x; a[i] = make_pair(x, i); } sort(a, a + n); for (int i = 0; i < n; i++) res[a[i].second] = i % 2; for (int i = 0; i < n; i++) cout << ...
1
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int a[N], b[N]; int main() { int n, m; scanf("%d", &n); unsigned long long sum1 = 0, sum2 = 0; for (int i = 0; i < n; i++) scanf("%d", a + i), sum1 += a[i]; scanf("%d", &m); for (int i = 0; i < m; i++) scanf("%d", b + i), sum2 += b[i]; i...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, t, i; int a[111][2]; scanf("%d", &n); scanf("%d", &t); for (i = 0; i < n; i++) scanf("%d %d", &a[i][0], &a[i][1]); t = 7 - t; for (i = 1; i <= n - 1; i++) { if (t == a[i][0] or t == a[i][1] or t == 7 - a[i][0] or t == 7 - a[i][1]) ...
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; set<int> st; for(int i = 0;i < n;i++) { int x; cin >> x; st.insert(x); } if(st.size() % 2 == 0) { cout << st.size() - 1; } else { cout << st.size(); } ...
0
#include <bits/stdc++.h> using namespace std; long long pw(long long x, long long y) { long long ret = 1; while (y) { if (y % 2) ret = (ret * x) % 1000000007; x = (x * x) % 1000000007; y /= 2; } return ret; } long long s[100005] = {0}, e[100005] = {0}, cur = 0; vector<pair<long long, long long> > rn...
5
#include<map> #include<string> #include<iostream> using namespace std; int main() { map<char,string> en; en[' ']="101"; en['\'']="000000"; en[',']="000011"; en['-']="10010001"; en['.']="010001"; en['?']="000001"; en['A']="100101"; en['B']="10011010"; en['C']="0101"; en['D']="0001"; en['E']="110"; en['F']="010...
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int t; cin >> t; int n; n = s.size(); if (n % t == 0) { char a[1001]; for (int i = 1; i <= n; i++) { a[i] = s[i - 1]; } bool flag; flag = true; int j; int k; for (int i = 1; i < n; i = i + ...
1
#include<iostream> using namespace std; class Dice{ public: int f[6]; Dice(){} void roll_x(){roll(1,4,6,3);} // to E void roll_y(){roll(1,2,6,5);} // to N void roll_z(){roll(2,3,5,4);} // 上から見て時計回り void roll(int i,int j,int k,int l){ int t=f[i]; f[i]=f[j]; f[j]=f[k]; f[k]=f[l]; f[l]...
0
#include <bits/stdc++.h> using namespace std; long long flag; char s, s1, s2, s3, s4, s5, x, x1, x2, x3, x4, x5; int main() { cin >> x >> s; cin >> x1 >> s1 >> x2 >> s2 >> x3 >> s3 >> x4 >> s4 >> x5 >> s5; flag = 0; if (x1 == x || x2 == x || x3 == x || x4 == x || x5 == x) flag++; if (s1 == s || s2 == s || s3 ...
1
#include <bits/stdc++.h> using namespace std; const int N = 2 * 1000 * 100 + 9; double mas[N], sum[N]; int main() { ios::sync_with_stdio(false); int n, m, T; double c; int a; cin >> n >> T >> c; for (int i = 1; i <= n; ++i) { cin >> a; mas[i] = (mas[i - 1] + a * 1.0 / T) / c; sum[i] = sum[i - 1]...
2
#include <bits/stdc++.h> using namespace std; using nagai = long long; #define sz(x) int((x).size()) const int mod=1e6+3; int su(int a,int b){ if((a+=b)>=mod)a-=mod; return a; } int mu(int a,int b){ return (nagai)a*b%mod; } int pw(int a,int b,int c=1){ for(;b;b>>=1,a=mu(a,a))if(b&1)c=mu(c,a); return c; } in...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; struct node { long long a; int b; bool operator<(const node &y) const { return a < y.a; } } f[maxn]; long long a[maxn]; int n; int judge1() { if (a[1] + a[3] != a[2] * 2) return -1; int c = a[2] - a[1], t = 0, ind = 1, i; for (i = 4; i...
4
#include <bits/stdc++.h> using namespace std; const long double PI = acosl(-1.0); bool isSubsetSum(vector<int> set, int n, int sum) { bool subset[n + 1][sum + 1]; for (int i = 0; i <= n; i++) subset[i][0] = true; for (int i = 1; i <= sum; i++) subset[0][i] = false; for (int i = 1; i <= n; i++) { for (int j ...
2
#include <bits/stdc++.h> using namespace std; const int N = 5005; int n, k; char ch[N], m; bool ok[N][N]; queue<pair<int, int> > q; int main() { int i, j, l, o, t; scanf("%s", ch + 1); ch[0] = '_'; n = strlen(ch) - 1; i = 2; while (i <= n) { k++; i <<= 1; } l = n - (1 << k) + 1; ok[0][0] = 1; ...
6
#include <bits/stdc++.h> using namespace std; long long INV2 = 500000004; long long INV6 = 166666668; long long power(long long a, long long b, long long c) { long long x = 1, y = a; while (b > 0) { if (b & 1) x = (x * y) % c; y = (y * y) % c; b /= 2; } return x % c; } int dx[] = {0, -1, 0, 1}; int ...
1
#include <bits/stdc++.h> const int MAXN = 2e5 + 5; using namespace std; struct info { int coord; bool taxi; } v[MAXN]; int n, m, dist[MAXN], man[MAXN], fr[MAXN]; int main() { cin >> n >> m; for (int i = 1; i <= n + m; i++) { cin >> v[i].coord; dist[i] = 2e9; } for (int i = 1; i <= n + m; i++) cin >>...
2
#include<iostream> #include<map> #include<queue> using namespace std; map<int,int> m1; priority_queue<int> q1; int t[200005]; int check(int a){ for(int i=0;;i++){ if((1<<i)>a)return ((1<<i)-a); } } int main(){ int n; int i,j,k; int a,b,c; int s=0; cin>>n; for(i=0;i<n;i++){ cin>>t[i]; m1[t[i]]++; q1.push...
0
#include <bits/stdc++.h> using namespace std; int x[510][510], row[510][510], col[510][510]; int n, m, ans; void calc_ans(int sx, int sy) { int up = sx - 1, down = sx + 1, left = sy - 1, right = sy + 1; int t = x[sx][sy], sum = x[sx][sy]; while (up >= 1 && down <= n && left >= 1 && right <= m) { int add = row...
3
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main() { int n; cin >> n; vector<int> a(n+1); a[n] = 1000000001; vector<int> b(n+1); b[n] = 1000000001; vector<int> c(2 * n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[i] = x; b[i] = y; c[2...
0
#include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <vector> #include <map> #include <set> using namespace std; typedef pair<int , int> P2; typedef pair<pair<int , int> , int> P3; typedef pair<pair<int , int> , pair<int , int> > P4; #define Fst first #define Snd second #define PB(a) push...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, int> > v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(make_pair(x, i + 1)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int cost = 1; for (int i = 1; i < n; i++) { c...
2
#include <iostream> #include <set> using namespace std; int main() { int n; set<string> st; cin >> n; bool flg=true; for (int i=0; i<n; i++) { char t; string s; cin >> s; if (i>0 && s.front()!=t) flg=false; t=s.back(); st.insert(s); } if (st.size()!=n) flg=false; if (flg) co...
0
#include <bits//stdc++.h> using namespace std; #define int long long signed main(){ int a,b,c,k; cin>>a>>b>>c>>k; if(b>a){ swap(a,b); } if(c>a){ swap(a,c); } cout<<(1<<k)*a+b+c<<endl; }
0
#include<stdio.h> int main() { int a[4][3][10]={0},b,f,r,v,n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d %d %d %d",&b,&f,&r,&v); a[b-1][f-1][r-1]=a[b-1][f-1][r-1]+v; } for(int x=0;x<4;x++) { for(int y=0;y<3;y++) { for(int z=0;z<10;z++) { printf(" %d",a[x][y][z]); } printf("\n"); ...
0
#include<bits/stdc++.h> using namespace std; int main(void){ int n; cin>>n; string s; cin>>s; vector<int> e(n+1,0),w(n+1,0); for(int i=1; i<n+1; ++i){ e[i]=e[i-1]; w[i]=w[i-1]; if(s[i-1]=='E') ++e[i]; else ++w[i]; } int res=2147483647; for(int i=1; i<n+1; ++i) res=min(res,w[i-1]+e[n]-e[i]); cout...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, i, j; cin >> n >> m; char img[55][55]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> img[i][j]; } } int cnt = 0; for (i = 0; i < n - 1; i++) { for (j = 0; j < m...
1
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 29; const int mod = (int)1e9 + 7; const double eps = 1e-8; const double pi = acos(-1.0); int n, m, k; char a[2000010], b[1000010]; int nxt[2000010]; int main() { scanf("%d", &n); scanf("%s%s", a, b); n--; for (int i = 0; i < n; i++) { if (b[...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1, r, n, a, d, c, b, x, y, sum, cnt, i, j, k, m, l, q, maxx, minn, temp, ans, flag, g, ts; char ch, chh; double dans; string str, ttr; cin >> t; for (ts = 1; ts <...
3
#include <bits/stdc++.h> using namespace std; const long long inf = 2e18; struct LiChao_min { struct line { int64_t a, b; line() { a = 0; b = 0; } line(int64_t _a, int64_t _b) { a = _a; b = _b; } int64_t eval(int x) { return a * x + b; } }; struct node { node *l...
3
#include <bits/stdc++.h> using namespace std; int a[10][10]; int main() { int r, c, n, k, x, y, m = 0; cin >> r >> c >> n >> k; long long count = 0; for (int i = 0; i < n; i++) { cin >> x >> y; a[x - 1][y - 1] = 1; } for (int x1 = 0; x1 < r; x1++) for (int y1 = 0; y1 < c; y1++) for (int x2...
1
#include<iostream> using namespace std; int main() { int a; int ans; ans = 0; for (int i = 1; i <= 5; i++) { cin >> a; if (a <= 40) { a = 40; } ans = ans + a; } cout << ans / 5 << endl; }
0
#include <iostream> #define REP(i, a, n) for(int i = (a); i <= (n); i++) using namespace std; typedef pair<int, int> pii; int P, Q, A, N; int dfs(int p, int q, int n, int t) { if(P * q == p * Q) return 1; if(P * q < p * Q) return 0; if(N == n) return 0; int ans = 0; REP(i, t, A / q) ans += dfs(p * i + q, ...
0
#include <bits/stdc++.h> using namespace std; template <class T> void chmax(T &a, T b) { if (a < b) a = b; } template <class T> void chmin(T &a, T b) { if (a > b) a = b; } constexpr long long INF = 1000000000000000000; constexpr long long mod = 1000000007; constexpr double eps = 1e-8; long long dx[] = {0, 1, 0, -1}...
2
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int n, m, a[111111]; void fileOpen() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); ios ::sync_with_stdio(false); } int main() { scanf("%d", &(n)); for (int i = (int)(1); i <= (int)(n); ++i) scanf("%d", &(a[i])); ...
4
#include <bits/stdc++.h> using namespace std; vector<vector<int> > conections; vector<int> selected; vector<bool> visited; vector<int> init; vector<int> goal; void visit(int index, int level, int count0, int count1) { if (visited[index]) return; visited[index] = true; if (level % 2 == 0) { if ((init[index] + ...
3
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long a, b, c, d[4]; for (long long i = 0; i < 4; i++) { cin >> d[i]; } sort(d, d + 4); a = d[0] + d[1] - d[3]; b = d[1] + d[2] - d[3]; c = d[0] + ...
1
#include <bits/stdc++.h> using namespace std; const int N = 20; bool adj[N][N]; long long dp[1 << N][N]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; adj[a][b] = adj[b][a] = true; } for (int i = 0; i < n; i++) dp[1 << i][i] = 1; l...
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> k; while (cin >> n) cout << (n < 4 ? 4 - n : n % 2) << endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n, m, r; cin >> n >> m >> r; ll dp[n][n]; for(ll i = 0; i < n; ++i) for(ll j = 0; j < n; ++j)dp[i][j] = (i == j ? 0 : 1145141919); vector<ll> rr; for(ll i = 0; i < r; ++i){ ll a; cin >> a; rr.push_back(--a); } sort(rr.begi...
0
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30) - 1; const long long linf = (1ll << 62) - 1; int q; map<int, long long> parent, value; inline int getLevel(long long x) { int ans = 0; while (x > 1) { x >>= 1; ans++; } return ans; } int main() { cin >> q; for (int i = 0; i < q;...
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { long long A, B; cin >> A >> B; long long hb = (floor(log10(B + 1)) == log10(B + 1)) ? (floor(log10(B)) + 1) :...
2
#include <bits/stdc++.h> using namespace std; long long a[6]; const long long maxn = 1e5; long long b[6][maxn]; long long n; long long get(long long T, long long val) { auto it = lower_bound(b[T], b[T] + n, val); return it - b[T]; } long long calc(long long pos, long long v) { long long last = pos; long long mx...
3
#include<bits/stdc++.h> using namespace std; int n,t[2],a[5][100010],to[100010]; char buffer[10000010],*hed; inline char Getchar(){ return *hed++; } inline int rd(){ register int x=0; char c; do c=Getchar(); while(!isdigit(c)); do{ x=(x<<1)+(x<<3)+(c^48); c=Getchar(); }while(...
0
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); const long long SZ = 1e5 + 4; const double EPS = 1e-7; const long long mod = 1e9 + 7; const long long hashpow = 2; const long long inf = 2e9; struct custom_hash { static uint64_t splitmix64(uint64_t x) {...
1
#include<bits/stdc++.h> using namespace std; int main(){ int n,m; double cost[22][22]; double ans; cin>>n>>m; double l[22],a[22],b[22]; for(int i=0;i<n;i++) cin>>l[i]>>a[i]>>b[i]; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++){ double x=l[i]-l[j]; double y=a[i]-a[j]; double z=b[i]-...
0
#include <bits/stdc++.h> using namespace std; void doin() { cin.tie(); cout.tie(); ios::sync_with_stdio(0); } template <typename X, typename Y> istream& operator>>(istream& in, pair<X, Y>& a) { in >> a.first >> a.second; return in; } template <typename T> void getv(T& i) { cin >> i; } template <typename T, ...
1
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); } template<class T> using V = vector<T>; template<class T> using VV = V<V<T>>; using D = double; const D PI = acos(D(-1)), EPS = 1e-...
0
#include <bits/stdc++.h> using namespace std; inline int setBit(int N, int pos) { return N = N | (1 << pos); } inline int resetBit(int N, int pos) { return N = N & ~(1 << pos); } inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); } long long n, m; string s, t; int hour_dig, min_dig; void gen() { s ...
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int N; cin >> N; int A[N], i, j, tot = 0, max = 0; for (i = 0; i < N; ++i) { cin >> A[i]; } sort(A, A + N, greater<int>()); int l, b; for (i = 0; i < N; ++i) { l = A[i]; b = ...
1
#include <bits/stdc++.h> using namespace std; int n, m; int ans = 0; int deg[1013], ged[1013]; pair<int, int> has[2][1013][1013]; int color[100013]; int c[2]; void dfs(int x, int p) { auto i = has[p][x][c[p ^ 1]]; if (has[p ^ 1][i.first][c[p]].second) dfs(i.first, p ^ 1); else has[p ^ 1][i.first][c[p ^ 1]...
6
#include <bits/stdc++.h> using namespace std; int read() { int x = 0; bool f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return f ? -x : x; } const int N = 3e6 + 10; const int mod = 1e9 + 7; const int inv3 = (mod ...
3
#include <bits/stdc++.h> using namespace std; int N, K; string s; int main() { cin >> N >> K >> s; for (int i = 0; i < N; i++) { if (K == 0) break; if ((i + 1) % 2 != 0) { if (i < N - 1 && s[i] == '4' && s[i + 1] == '7') { s[i] = s[i + 1] = '4'; K--; } } else { if (i < ...
4
#include <bits/stdc++.h> using namespace std; const long long INF = (int)1e9 + 7; const int mod = (int)1e9 + 7; const int N = (int)1e5 + 2; int n, degree[N]; int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int x, y; scanf("%d %d", &x, &y); degree[x]++, degree[y]++; } for (int i = 1; i <=...
1
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int g[16][16],n,x,y,now; void func2(void); void func1(void){ if(x>=n)x=0; if(x<0)x=n-1; if(y>=n)y=0; if(g[y][x]>0)func2(); } void func2(void){ if(g[y][x]>0) x--,y++; if(x>=n || x<0 || y>=n)func1(); } int main(void){ while...
0
#include <bits/stdc++.h> using namespace std; int main() { int r, c, n; char b; cin >> r >> c; vector<vector<int> > v(101); bool wrong = 0; for (int i = 0; i < r; i++) { bool at_least_once = 0; for (int j = 0; j < c; j++) { cin >> b; n = b - '0'; if (n == 1) { at_least_once...
1
#include <bits/stdc++.h> using namespace std; const int N = 1000005; const int mod = 998244353; void inc(int &x, int y) { x += y; if (x >= mod) x -= mod; } int qpow(int a, int b) { int tmp = 1; while (b) { if (b & 1) tmp = 1ll * tmp * a % mod; a = 1ll * a * a % mod; b >>= 1; } return tmp; } int ...
4
#include <bits/stdc++.h> using namespace std; const int MN = 5e5 + 100; int t[MN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> t[i]; } int b = 0; int ma = 0; for (; b < n; ++b) { if (b == n - 1 || t[b] != t[b + 1]) break; } w...
1
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; for (int i = 0; i < str.length(); i++) { if (i == 0 && str[i] == '9') continue; if (str[i] >= '5') str[i] = '9' - str[i] + '0'; } cout << str << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long nax = 100005, mod = 1e9 + 7; vector<long long> g[nax]; long long c[nax], dp[nax][2] = {{0}}; long long mod_expo(long long a, long long b) { long long tans = 1; a = a % mod; while (b != 0) { if (b & 1) tans = (1LL * tans * a) % mod; a = (1LL * a...
2
#include <bits/stdc++.h> using namespace std; void online_judge() {} void ISO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } int main() { ISO(); online_judge(); int t; cin >> t; while (t--) { long long int n; long long y; cin >> n >> y; long long ans = 0; long long int a[n]; ...
1
#include <bits/stdc++.h> using namespace std; const int N = 5 * 1e5 + 10; char str[N]; bool st[N]; char s[2][10] = {"Ann", "Mike"}; int main() { scanf("%s", str + 1); int len = strlen(str + 1); char min_ch; for (int i = 1; i <= len; i++) { if (i == 1) { min_ch = str[i]; st[i] = true; } else ...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); vector<int> vec; int count = 0; int N; cin >> N; int arr[N - 1][2]; for (int i = 0; i < (N - 1); i++) { for (int j = 0; j < 2; j++) { cin >> arr[i][j]; } } for (int search_item = 1;...
2
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T& t) { cin >> t; } template <typename T, typename... U> void read(T& t, U&... u) { read(t); read(u...); } template <typename T> void write(T t) { cout << t; } template <typename T, typename... U> void write(T t, U... u) { write(t);...
6
#include "bits/stdc++.h" using namespace std; using ll = long long; using P = pair<int, int>; #define PI acos(-1) #define Mod (int)1000000007 #define INFTY (int)INT_MAX #define Rep(i, n) for (int i = 0; i < (int)(n); i++) #define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++) #define All(vec) vec.begin(), vec.end...
0
#include <bits/stdc++.h> using namespace std; struct state { short int len, link; map<char, int> next; }; state st[12000]; short int vis[12000][5001]; int dp[5005]; int sz, last; void sa_init() { sz = last = 0; st[0].len = 0; st[0].link = -1; ++sz; } char str[5005]; void sa_extend(char c) { int cur = sz++...
6
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int mod = 1e9 + 7; int n, m, t, q; vector<long long> G; map<int, int> M; set<int> S; struct node { long long x, y; } a[N]; bool cmp(node aa, node bb) { return aa.x < bb.x; } int main() { scanf("%d", &n); for (int i = (1); i <= (n); i++) sc...
2
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y, long long int p = 1e9 + 7) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } x = (x * x) % p; y = y >> 1; } return res; } int main...
4
#include <bits/stdc++.h> using namespace std; template <typename T> void show(const T& v) { cerr << v << ' '; } template <typename T1, typename... T2> void show(const T1& first, const T2&... rest) { show(first); show(rest...); } template <typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S>& ...
4
#include <bits/stdc++.h> using namespace std; const int N = 3e6 + 20; const long long inf = 2e15; long long d[N], inq[N]; queue<int> q; long long n, m, k; struct node { long long x, y; } p[N]; long long SPFA() { for (int i = 1; i <= k; i++) d[i] = inf, inq[i] = 0; while (!q.empty()) q.pop(); q.push(1), inq[1] =...
4
#include<bits/stdc++.h> using namespace std; map<pair<int, int> , int> ans; int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); } void solve() { int n; scanf("%d", &n); int D = 0, K = 0; for(int i = 1; i <= n; i++) { char cur; scanf(" %c", &cur); if(cur == 'D') D++; else K++; int div = gcd(D,...
3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double pii = 2 * pi; const double eps = 1e-6; const long long MOD = 1e9 + 7; void solve() { int n, t; cin >> n >> t; string x, y; cin >> x >> y; int ctr = 0; for (int i = 0; i < n; i++) if (x[i] != y[i]) ctr++; if (t > c...
3
#include <bits/stdc++.h> using namespace std; int main() { int t, sum = 0; cin >> t; while (t--) { string s; cin >> s; if (s == "Tetrahedron") { sum += 4; } if (s == "Cube") { sum += 6; } if (s == "Octahedron") { sum += 8; } if (s == "Dodecahedron") { su...
1
#include <bits/stdc++.h> using namespace std; int n, m, vis[2001], Color[2001], par[2001], d[2001]; vector<vector<int> > G(2001), G1(2001); queue<int> q; void make_orientation(int); void set_color(int, int); void find_bridge(int); void print_colors(); void print_graph(); int main() { scanf("%d%d", &n, &m); for (int...
2