solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int a[50][50], row[50], col[50], n, ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[j][i]; row[i] += a[j][i]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { col[i] +=...
1
// Problem: F. Christmas Game // Contest: Codeforces - CodeCraft-21 and Codeforces Round #711 (Div. 2) // URL: https://codeforces.com/contest/1498/problem/F // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("a...
6
#include <bits/stdc++.h> using namespace std; template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out...
5
#include <bits/stdc++.h> using namespace std; const int Max = 3e5 + 10; const int Mod = 1e9 + 7; const int N = 1e6; int D[N + 10]; void Cin(int &num) { num = 0; char ch = getchar(); int flag = 0; while (!((ch >= '0' & ch <= '9') || ch == '-')) { ch = getchar(); } if (ch == '-') { flag = 1; ch = ...
6
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= 2 * k; i++) { int sx, sy; cin >> sx >> sy; } for (int i = 1; i <= m - 1; i++) s += 'L'; for (int i = 1; i <= n - 1; i++) s += 'U'; for (int i = 1; i <= n; i++) { if (i %...
3
#include <bits/stdc++.h> using namespace std; const int N=234567; int n; int a[N],b[N]; int check(int x){ for(int i=0; i<2*n-1; i++) b[i]=0; for(int i=0; i<2*n-1; i++) if(a[i]>=x) b[i]=1; int r=b[n-1],ri=N; for(int i=n; i<2*n-1; i++){ if(r==b[i]){ ri=i-(n-1)-1; break; } r=b[i]; } int l=b[n-1],li=...
0
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #define MV 51 #define MX 101 #define MX2 202 #define MOD 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> pii; template <typename T> void read(T& x) { x = 0; char c = getchar(); while(!isdigi...
0
#include <bits/stdc++.h> using namespace std; int n, k, f[10], mn = 1 << 29; string str, res; string calc(int b) { int rem = k - f[b]; vector<int> F(f, f + 10); int l = b - 1, r = b + 1; int cost = 0; vector<int> change(10, 0); while (rem) { if (r < 10 && !F[r]) { ++r; continue; } if...
3
#include <bits/stdc++.h> using namespace std; int main() { string s; int i, j, cntb = 0, totb = 0, sum = 1e6, cnt; cin >> s; int a[5005]; if (s[0] == 'a') a[0] = 1; else a[0] = 0, totb++; for (i = 1; s[i]; i++) { if (s[i] == 'a') a[i] = a[i - 1] + 1; else a[i] = a[i - 1], totb+...
2
#include <bits/stdc++.h> using namespace std; int main() { int N, M, tmp; long long Hest = 0; cin >> N >> M; vector<long long> party[M + 1]; for (int i = 0; i < N; i++) { long long r, c; cin >> r >> c; party[r].push_back(c); } for (int i = 2; i <= M; i++) { sort(party[i].begin(), party[i]....
1
#include <bits/stdc++.h> using namespace std; long long int findSum(vector<long long int> vect, int x, int y) { return vect[y] - vect[x]; } void compute() { int n, m, x, y; cin >> n >> m >> x >> y; vector<string> str(n + 1); for (int i = 1; i <= n; i++) { cin >> str[i]; } vector<long long int> ones(m ...
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e4 + 5; const int MaxN = 1e8 + 5, d = 5e7; struct XXX { int check, l, r, x; } s[MAXN + 5]; long long Max[MAXN + 5], p[MAXN + 5]; int n, m; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d %d %d %d", &s[i].check, &s[i].l...
1
#include <bits/stdc++.h> using namespace std; string s; int n; int main() { getline(cin, s); n = (int)s.size(); long long ans = 0; for (int i = 0; i < n; i++) { bool found = false; for (int j = i + 2; j < n; j++) { for (int k = 1; j - 2 * k >= i; k++) { if (s[j] == s[j - k] && s[j] == s[j ...
2
#include <bits/stdc++.h> using namespace std; int n; int f[10005], c[10005]; int main() { while (cin >> n) { for (int i = 2; i <= n; i++) cin >> f[i]; for (int i = 1; i <= n; i++) cin >> c[i]; int ans = 1; for (int i = 2; i <= n; i++) if (c[i] != c[f[i]]) ans++; cout << ans << endl; } re...
2
#include <bits/stdc++.h> using namespace std; int main() { int n, p, q, ans, i, j; string str; while (scanf("%d%d%d\n", &n, &p, &q) != EOF) { int a = -1, b = -1; cin >> str; for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { ans = i * p + j * q; if (ans == n) { a = i...
1
#include <bits/stdc++.h> using namespace std; void ECHO(string _s) { cout << endl; (void)_s; } template <typename T, typename... Args> void ECHO(string _s, T x, Args... args) { unsigned _i; string _s2 = ""; for (_i = 0; _i < _s.length(); ++_i) { if (_s[_i] == ',') break; if (_s[_i] != ' ') _s2 += _s[_...
3
#include <bits/stdc++.h> inline long long read() { long long x = 0; char c = getchar(), f = 1; for (; c < '0' || '9' < c; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return x * f; } inline void write(long long x) { static char buf[20]; int len...
4
#include <bits/stdc++.h> using namespace std; int n, m, xc, yc, zc; int f[305][305], a[305][305]; int main() { scanf("%d %d", &n, &m); memset(f, 127, sizeof(f)); memset(a, 127, sizeof(a)); for (int i = 1; i <= m; i++) { scanf("%d %d %d", &xc, &yc, &zc); f[xc][yc] = zc; f[yc][xc] = zc; a[xc][yc] ...
4
#include <bits/stdc++.h> using namespace std; vector<long long> divs; long long g(long long a, long long b) { if (a > b) { swap(a, b); } if (a == 0) { return b; } return g(b % a, a); } int main() { long long n, m, gcd = 1; cin >> n >> m; for (long long i = 0; i < n; i++) { long long a; c...
5
#include <bits/stdc++.h> using namespace std; void sol() { long long int n; cin >> n; string a; cin >> a; int cnt = 0; for (int i = 0; i < n; i++) { if (a[i] == '2') { cnt++; } } if (cnt > 0 && cnt < 3) { cout << "NO" << endl; return; } char ans[n][n]; for (int i = 0; i < n; ...
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL<<62 #define inf 1000000007 ll f,od; bool ev; int main() { ll n; cin>>n; for(int i=0;i<n;i++){ ll x; cin>>x; if(x%4==0){ f++; } else if(x%2==0){ ev=true; } else{ od++; } } if(ev){ od++; } cout << (od-1<...
0
#include<iostream> using namespace std; int main(){ int N,M,P; cin>>N>>M>>P; int u[123],v[123]; for(int i=0;i<M;i++){ cin>>u[i]>>v[i]; u[i]--; v[i]--; } double dp[1<<14]={0,1}; for(int i=2;i<1<<N;i++){ if(!(i&1))continue; dp[i]=1; for(int j=1;j<i;j=(j+1+~i)&i){ double d=dp[...
0
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } string s; cin >> s; vector<long long> b; for (int i = n - 1; i >= 0; i--) { long long v = a[i]; for (auto x : b) { v = min(v, v ^ x); ...
0
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-10; const long long mod = 1e9 + 7; const long long INF = (long long)1e14 + 5; ; const int maxn = 1e5 + 5; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '...
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 15; const int MAXM = 10; const int MAXS = (1 << MAXN); const int MAXLEN = 500000; const int INF = 0x3f3f3f3f; template <class T> void Read(T &x) { x = 0; char c = getchar(); bool flag = 0; while (c < '0' || '9' < c) { if (c == '-') flag = 1; ...
4
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { long long int a[3]; cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[0] != a[1] && a[2] != a[1]) { a[0]++; a[2]--; } else if (a[0] == a[1] && a[0] != a[2]) { a[0]++; a[1]++; ...
1
#include<bits/stdc++.h> using namespace std; int main() { int i,j,k,l; while(cin>>l){ j=l/100; k=l%100; if(j<=12&&k<=12&&j!=0&&k!=0) cout<<"AMBIGUOUS"<<endl; else if(j<=12&&j!=0) cout<<"MMYY"<<endl; else if(k<=12&&k!=0) cout<<"YYMM"<<endl; else cout<<"NA"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 1; int h[maxn]; int main() { int n; cin >> n; h[0] = 0; for (int(i) = (1); (i) <= (n); ++(i)) cin >> h[i]; long long sum = 0; long long minSum = 0; for (int(i) = (0); (i) < (n); ++(i)) sum += h[i] - h[i + 1], minSum = min(minSum, sum...
2
#include <bits/stdc++.h> using namespace std; bool compare(string &s1, string &s2) { return s1.size() < s2.size(); } bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) { if (a.first < b.first) return true; return false; } void solve() { long long int n, op; cin >> n >> op; string s; cin >> ...
4
#include<bits/stdc++.h> using namespace std; int main(){ int n,k,sum=0,x,y; cin>>n>>k>>x>>y; for(int i=0;i<n;i++){ sum+=(i<k)?x:y; } cout<<sum; }
0
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define mod 998244353 #define int long long using namespace std; const int N=3*1e5+100; int n,cnt[5],ans,vi[N],id[30]; char s[N]; vector <int> p[N]; signed main() { id['R'-'A']=1;id['B'-'A']=2;id['G'-'A']=3; scanf("%lld",&n); scanf("%s",s+1); for (int i=1;i<=3*n;i++) ...
0
#include <bits/stdc++.h> using namespace std; long long n, m, u, v, edges, ans; vector<long long> adj[100001], nodes; bool visit[100001]; void dfs(int node) { nodes.push_back(node); visit[node] = 1; for (long long i = 0; i < adj[node].size(); i++) { if (!visit[adj[node][i]]) dfs(adj[node][i]); } } int main(...
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 50 + 2; int N; char chess[MAXN][MAXN]; char ans[MAXN * 2 + 1][MAXN * 2 + 1]; char new_chess[MAXN][MAXN]; char get() { char ch = getchar(); while (ch != 'o' && ch != 'x' && ch != '.') ch = getchar(); return ch; } int main() { scanf("%d", &N); for (...
4
#include <map> #include <algorithm> #include <set> #include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); map<int, set<int>> b; int h, w, n; cin >> h >> w >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; b[y].insert(x); } i...
0
#include <bits/stdc++.h> using namespace std; int dp[503][503], n, c[503]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> c[i]; for (int l = 0; l < n; l++) for (int i = 0; i < n - l; i++) { int j = i + l; if (i == j) { dp[i][j] = 1; continue; } dp[i][j] = dp[...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 7; int n, m, head[maxn], depth[maxn], fa[maxn][50], num = 0; int a[maxn]; struct node { int next, to; }; struct node edge[maxn]; void add(int u, int v) { edge[num].next = head[u]; edge[num].to = v; head[u] = num++; } void dfs(int u, int pre, i...
5
#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int i=0;i<n;++i) typedef long long ll; typedef vector<ll> vll; ll mod = 1e9+7; int main() { ll n;cin >> n; vll a(n), t(n); rep(i,n) cin>>a[i]; rep(i,n) cin>>t[i]; vll x(n,-1); if(a[n-1] != t[0]) { puts("0"); return 0; } x[0]=a...
0
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void chkmax(T& x, U y) { if (x < y) x = y; } template <typename T, typename U> inline void chkmin(T& x, U y) { if (y < x) x = y; } const int INF = 1e9; int main() { int n; scanf("%d", &n); int L = -INF, R = INF; for (int ...
3
#include <bits/stdc++.h> using namespace std; const int MAX = 500009; int n, m, k, x, y; int a[MAX]; int ji[MAX]; int ji2[MAX]; int flag, max1, sum1, bj, max2, sum2; vector<int> g[MAX]; int main() { memset(ji2, 0, sizeof(ji2)); memset(ji, 0, sizeof(ji)); memset(a, 0, sizeof(a)); scanf("%d", &n); max1 = -20000...
3
#include <bits/stdc++.h> using namespace std; template <typename T> bool scan(T &n) { n = 0; bool got = false; bool negative = false; char c = getchar(); if (c == EOF) return false; while (c<'0' | c> '9') { if (c == '-') negative = true; c = getchar(); } while (c >= '0' && c <= '9') { got = ...
3
#include "bits/stdc++.h" using namespace std; int main() { int n; int m; int d; while (~scanf("%d%d%d", &n, &m, &d)) { double ans = (d == 0 ? n : (n - d) * 2) / ((double)n * n); ans *= m - 1; printf("%.10f\n", ans); } }
0
#include <bits/stdc++.h> using namespace std; long long S[1200008]; long long A[300002]; int n; void add(int l, int r, int num, int id = 1, int sl = 0, int sr = n + 1) { if (r <= sl or sr <= l) return; if (l <= sl and sr <= r) { S[id] += num; return; } int mid = (sl + sr) / 2; S[2 * id] += S[id]; S[...
5
#include <bits/stdc++.h> constexpr int N = 100, P = 1000000007; int n, k; int dp[N + 1][N * N + 1]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> n >> k; dp[n][0] = 1; for (int i = n; i > 0; --i) { for (int j = 0; j <= n * n; ++j) { ...
0
#include <bits/stdc++.h> using namespace std; int main() { int N; scanf("%d", &N); for (int i = 0; i < (N); i++) { int R = N - 4 * i; if (R >= 0 && R % 7 == 0) { for (int j = 0; j < (i); j++) putchar('4'); for (int j = 0; j < (R / 7); j++) putchar('7'); putchar('\n'); return 0; ...
1
#include <bits/stdc++.h> using namespace std; struct ww { int x, y; } an[(1 << 20)], tt[(1 << 20)]; int i, j, k, n, m, s, u, t, re; int a[30], b[30], c[30]; inline void Do(int n, int x, int y) { int i; for (i = 1; i <= n; i++) an[++re] = (ww){x + 1, y + 1}; } void dfs(int n, int x, int y, int ty) { if (n > s) r...
4
#include <bits/stdc++.h> using namespace std; long long a[100005]; int par[100005]; priority_queue<pair<long long, int> > pq; long long tim(long long carrot, int parts) { long long w = carrot / parts; return (w) * (w) * (parts - carrot % parts) + (w + 1) * (w + 1) * (carrot % parts); } int main() { ios_b...
5
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); //e int n; cin >> n; vector<int> pos(n); for (int i = 0; i < n; i++) { int a; cin >> a; pos[--a] = i; } long long ans = 0; set<int> st; st.insert(-1); st.in...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 500005; string s; int n, a[maxn]; struct node { int x1, y1, x2, y2; } p[5]; bool cov(node a, node b) { if (a.x1 >= b.x1 && a.y1 >= b.y1 && a.x2 <= b.x2 && a.y2 <= b.y2) return true; return false; } int main() { cin >> p[1].x1 >> p[1].y1 >> p[1].x2 >...
3
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> int print_value(A title, B val) { cout << title << " = " << val << "\n"; return 1; } int break_point() { char c; while ((c = getchar()) != '\n') ; return 0; } template <typename A> int logg(A v) { cout << v << "\n"; return...
3
#include <bits/stdc++.h> using namespace std; const int Imx = 2147483647; const long long Lbig = 2e18; const int mod = 1e9 + 7; struct fastio { char s[100000]; int it, len; fastio() { it = len = 0; } inline char get() { if (it < len) return s[it++]; it = 0; len = fread(s, 1, 100000, stdin); if (...
4
#include <bits/stdc++.h> #pragma warning(disable : 6031) #pragma warning(disable : 4101) using namespace std; const int INF = 1e9; const long long LINF = 1e18; const double PI = acos(-1); const int MAXN = 200005; int n, m, l, r, j, best, ans, A[MAXN]; vector<pair<int, int> > B; void print_res(int ind) { if (ind == l ...
6
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") void scan() {} template <typename F, typename... R> void scan(F& f, R&... r) { cin >> f; scan(r...); } int di_; string dnms_, co_ = ","; void debug_() { cout << endl; } template <typename F, typename... R> void debug_(F& f, R&... r) { whi...
3
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int...
0
#include <bits/stdc++.h> using namespace std; char op(char a) { return (a == 'U' ? 'R' : 'U'); } int main() { int n; string s; cin >> n >> s; stack<char> t; for (int i = 0; i < n; i++) { if (t.size() == 0) t.push(s[i]); else { if (t.top() == op(s[i])) { t.pop(); t.push('D')...
1
#include <bits/stdc++.h> using namespace std; char s[100]; int toInt(char *p) { if (p[0] == 'A') return 0; if (p[0] == 'C' && p[1] == 'h') return 1; if (p[0] == 'C' && p[1] == 'l') return 2; if (p[0] == 'T') return 3; if (p[0] == 'D') return 4; if (p[0] == 'S') return 5; return 6; } int l[7][7]; int a[7];...
1