solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int arr[N]; int n; long long solve(int l, int r) { if (l == r) { return 1ll * arr[l] * (n - arr[l] + 1); } int half = (l + r) >> 1; long long res = solve(l, half) + solve(half + 1, r); int L = min(arr[half], arr[half + 1]); int R = max(arr[half], arr[half + 1]); res -= 1ll * L * (n - R + 1); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i]; } cout << solve(0, n - 1) << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; struct node { int h, m; int hehe; } a[110]; int n; char s[10000]; int main() { int i, j; cin >> n; ; getchar(); for (i = 1; i <= n; i++) { gets(s); a[i].h = (s[1] - '0') * 10 + s[2] - '0'; a[i].m = (s[4] - '0') * 10 + s[5] - '0'; if (a[i].h == 12) a[i].h = 0; if (s[7] == 'p') { a[i].h += 12; } a[i].hehe = a[i].m + a[i].h * 60; } int ans = 1, x = a[1].hehe; int siz = 1; for (i = 2; i <= n; i++) { if (a[i].hehe < x) siz = 1, ans++; else if (a[i].hehe == x) siz++; else siz = 1; if (siz > 10) { siz = 1; ans++; } x = a[i].hehe; } printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, i, c = 0, zero = 0, five = 0; cin >> n; while (n--) { cin >> i; if (i == 0) zero++; else five++; } if (zero == 0) { cout << -1; return 0; } five /= 9; five *= 9; if (five <= 0) cout << 0; else { while (five--) cout << 5; while (zero--) cout << 0; } }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); double a, b, c, d, e, f; cin >> a >> b >> c; d = pow(b, 2) - (4 * a * c); if (d < 0) { cout << 0; } else if (a == 0 and b == 0 and c == 0) { cout << -1 << endl; } else if (a == 0 and b == 0 and c != 0) { cout << 0 << endl; } else if (a == 0 and b != 0) { cout << 1 << endl; cout << setprecision(25) << double((-1 * c) / b) << endl; } else { e = ((-1 * b) + pow(d, 0.5)) / (2 * a); f = ((-1 * b) - pow(d, 0.5)) / (2 * a); if (d == 0) { cout << 1 << endl; cout << setprecision(25) << e << endl; } else { cout << 2 << endl; if (e < f) { cout << setprecision(25) << e << endl; cout << setprecision(25) << f << endl; } else { cout << setprecision(25) << f << endl; cout << setprecision(25) << e << endl; } } } }
6
#include <bits/stdc++.h> using namespace std; #define REP(i,a,n) for(int i=(a);i<(n);++i) #define BREP(i,a,n) for(int i=(a);i>(n);--i) #define TRAV(i, v) for(auto &i: v) #define fi first #define se second #define pb push_back #define SZ(x) (int((x).size())) #define ALL(x) (x).begin(), (x).end() #define INDEX(arr,ind) (upper_bound(ALL(arr),ind)-arr.begin()) #define db(x) cerr << #x << "=" << x << endl #define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl #define pow10(n) stoll("1"+string(n,'0')) #define EPS 1e-9 // #define LOCAL 1 typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef pair<ll, ll> pll; typedef vector < int > vi; typedef vector < ll > vll; typedef vector<ii> vii; typedef map<int, int> mii; const long long INF = 1LL << 60; vii v; void rec(int x){ if(x<=2) return; int mid=ceil(sqrt(x)); for (int i = mid+1; i < x; ++i) { v.pb({i,x}); } REP(i,0,2) v.pb({x,mid}); rec(mid); } void solve(){ v.clear(); int n; cin >> n; rec(n); cout << SZ(v); REP(i,0,SZ(v)) cout << endl << v[i].fi << " " << v[i].se; cout << endl; } int main(){ #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif int T; cin >> T; for (int tc = 1; tc <= T; ++tc) { // cout << "Case #" << tc << ": "; solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int arr[n]; for (long long int &i : arr) cin >> i; sort(arr, arr + n); set<long long int> x; set<long long int> y; for (long long int i = 0; i < n; i++) { long long int c = count(arr, arr + n, arr[i]); if (c > 1) { x.insert(arr[i]); y.insert(arr[i]); } else { x.insert(arr[i]); } } long long int temp = 0, ans = 0; for (auto i = 0; i <= 100; i++) { if (x.count(temp) == 0) { ans += temp; break; } temp++; } temp = 0; for (auto i = 0; i <= 100; i++) { if (y.count(temp) == 0) { ans += temp; break; } temp++; } cout << ans << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int r, c, n, k; cin >> r >> c >> n >> k; vector<int> xcords(n); vector<int> ycords(n); for (int i = 0; i < n; i++) { cin >> xcords[i]; cin >> ycords[i]; } int counter = 0; for (int i = 1; i <= r; i++) { for (int j = 1; j <= c; j++) { for (int t = i; t <= r; t++) { for (int l = j; l <= c; l++) { int counter2 = 0; for (int m = 0; m < n; m++) { if (xcords[m] >= i && xcords[m] <= t && ycords[m] >= j && ycords[m] <= l) counter2++; } if (counter2 >= k) counter++; } } } } cout << counter << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { short n, m, i, j; string s; cin >> n >> m; int d[m][n], ans, cv, t; for (i = 0; i < n; i++) { cin >> s; size_t pos = s.find('1'); if (pos != string::npos) { t = 0; for (j = 0; j < m; j++) { if (s[(j + pos) % m] == '1') { t = 0; } else { t++; } d[(j + pos) % m][i] = t; } for (j = m; j > 0; j--) { if (s[(j + pos) % m] == '1') { t = 0; } else { t++; } if (t < d[(j + pos) % m][i]) d[(j + pos) % m][i] = t; } } else { cout << "-1"; return 0; } } ans = INT_MAX; for (i = 0; i < m; i++) { cv = 0; for (j = 0; j < n; j++) { cv += d[i][j]; } if (cv < ans) ans = cv; } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, a, b, x; vector<vector<int> > v; vector<int> u, w, SUM, sum; int g() { int res = 0; for (int i = 0; i < 16; ++i) { int a = i / 4; int b = i % 4; if (a == b) { if (u[a]) res += SUM[i]; } else { if (u[a] || u[b]) { if ((v[i].size() + w[i]) % 2 == 0) res += SUM[i]; else res += sum[i]; } } } return res; } int f(vector<int>& p) { int res = 0; int a, b, c; a = p[0] / 4; b = p[0] % 4; u.assign(4, 0); w.assign(16, 0); u[a] = 1; u[b] = 1; ++w[p[0]]; c = b; for (int i = 1; i < p.size(); ++i) { a = p[i] / 4; b = p[i] % 4; if (b == c) swap(a, b); if (a != c) continue; u[a] = 1; u[b] = 1; ++w[p[i]]; c = b; } res = max(res, g()); a = p[0] / 4; b = p[0] % 4; swap(a, b); u.assign(4, 0); w.assign(16, 0); u[a] = 1; u[b] = 1; ++w[p[0]]; c = b; for (int i = 1; i < p.size(); ++i) { a = p[i] / 4; b = p[i] % 4; if (b == c) swap(a, b); if (a != c) continue; u[b] = 1; ++w[p[i]]; c = b; } res = max(res, g()); return res; } int solve(vector<int> p) { int res = 0; if (p.empty()) return res; if (p.size() > 10) { u.assign(4, 1); w.assign(16, 0); for (int i = 0; i < p.size(); ++i) ++w[p[i]]; res = max(res, g()); return res; } do { res = max(res, f(p)); } while (next_permutation(p.begin(), p.end())); return res; } int main() { ios_base::sync_with_stdio(false); v.resize(16); cin >> n; for (int i = 0; i < n; ++i) { cin >> a >> x >> b; --a; --b; if (a > b) swap(a, b); v[a * 4 + b].push_back(x); } sum.resize(16); SUM.resize(16); int res = 0; for (int i = 0; i < 16; ++i) { sort(v[i].begin(), v[i].end()); SUM[i] = accumulate(v[i].begin(), v[i].end(), 0); if (v[i].empty()) sum[i] = 0; else sum[i] = SUM[i] - v[i][0]; res = max(res, SUM[i]); } vector<vector<int> > p; p.push_back(vector<int>()); for (int i = 0; i < 16; ++i) { if (v[i].size() == 0) continue; if (i / 4 == i % 4) continue; int num = min((int)v[i].size(), 2); if (num > 0) { for (int j = 0; j < p.size(); ++j) p[j].push_back(i); } if (num > 1) { int k = p.size(); for (int j = 0; j < k; ++j) { p.push_back(p[j]); p.back().push_back(i); } } } for (int i = 0; i < p.size(); ++i) res = max(res, solve(p[i])); cout << res; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct rec { long long v; int inc; }; multiset<rec> tr; multiset<rec>::iterator it; bool operator<(rec a, rec b) { return a.v < b.v; } long long tmp, l, r, mid; int a[N], b[N], cnt, n, K, i; void run(int x) { tmp = cnt = 0; tr.clear(); for (i = 1; i <= n; i++) { tr.insert({a[i] - x, 1}); it = tr.begin(); if ((*it).v + b[i] < 0) { tmp += (*it).v + b[i]; cnt += (*it).inc; tr.erase(it); tr.insert({-b[i], 0}); } } } int main() { scanf("%d %d", &n, &K); for (i = 1; i <= n; i++) scanf("%d", a + i); for (i = 1; i <= n; i++) scanf("%d", b + i); l = 0; r = 2e9; while (l < r) { mid = (l + r) / 2; run(mid); if (cnt < K) l = mid + 1; if (cnt > K) r = mid - 1; if (cnt == K) l = r = mid; } run(l); if (cnt != K) tmp += 1ll * (K - cnt) * l; tmp += K * l; printf("%lld", tmp); }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, a[100001], i, j; bool flag; cin >> n >> x >> y; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) { flag = true; for (j = i - 1; j >= i - x && j >= 0; j--) if (a[i] > a[j]) flag = false; for (j = i + 1; j <= i + y && j < n; j++) if (a[i] > a[j]) flag = false; if (flag) break; } cout << i + 1 << endl; }
1
#include <bits/stdc++.h> int main() { int N, M, Q, r, c; int grounds[502], grid[502][502], i, max, mmax, count, j; for (i = 0; i < 502; i++) { for (j = 0; j < 502; j++) { grid[i][j] = 0; } } for (i = 0; i < 502; i++) grounds[i] = 0; scanf("%d%d%d", &N, &M, &Q); for (i = 1; i <= N; i++) { for (j = 1; j <= M; j++) { scanf("%d", &grid[i][j]); } } for (i = 1; i <= N; i++) { grounds[i] = 0; count = 0; max = 0; if (grid[i][1] == 1) ++count; for (j = 2; j <= M; j++) { if (grid[i][j] == grid[i][j - 1] && grid[i][j] == 1) ++count; else { if (count > max) max = count; count = 0; if (grid[i][j] == 1) ++count; } } if (count > max) max = count; grounds[i] = max; } mmax = 0; for (i = 1; i <= N; i++) { if (grounds[i] > mmax) mmax = grounds[i]; } while (Q > 0) { scanf("%d%d", &r, &c); if (grid[r][c] == 1) { grid[r][c] = 0; } else grid[r][c] = 1; count = 0; max = 0; if (grid[r][1] == 1) ++count; for (i = 2; i <= M; i++) { if (grid[r][i] == grid[r][i - 1] && grid[r][i] == 1) ++count; else { if (count > max) max = count; count = 0; if (grid[r][i] == 1) ++count; } } if (count > max) max = count; grounds[r] = max; mmax = 0; for (i = 1; i <= N; i++) { if (grounds[i] > mmax) mmax = grounds[i]; } printf("%d\n", mmax); --Q; } return 0; }
3
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<string> #include<set> using namespace std; #define pb emplace_back #define PII pair<int,int> #define ll long long #define mp make_pair const int maxn = 2e6 + 10; void solve() { long long q, a, b ,c,ans = 0; cin >> q >> a >> b >> c; ans = min((q + a - 1) / a * a - q,min((q + b - 1) / b * b - q, (q + c - 1) / c * c - q)); cout << ans << '\n'; } int main() { cin.sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int MAX = 200009; int a[MAX] = {0}; char s[MAX]; long long pw(long long base, long long expo, long long c) { if (expo == 0) return 1; long long f = pw(base, expo / 2, c); if (expo & 1) return (((f * f) % c) * base) % c; return (f * f) % c; } int main() { int i, j, k; scanf("%s", s); ; int l = strlen(s); long long p = 1LL; long long x = pw(2, l - 1, 1000000007); long long ans = 0; for (i = l - 1; i >= 0; i--) { if (s[i] - '0') { ans = (ans + p) % 1000000007; } p = (p * 2) % 1000000007; } printf("%lld\n", (ans * x) % 1000000007); return 0; }
4
#include <bits/stdc++.h> using namespace std; short n, x[2003], y[2003]; int ans = 0; int main() { scanf("%hd", &n); for (int i = 1; i <= n; ++i) scanf("%hd %hd", x + i, y + i); for (int i = 1; i < n - 1; ++i) for (int j = i + 1; j < n; ++j) for (int k = j + 1; k <= n; ++k) ans += !!(x[i] * (y[j] - y[k]) + x[j] * (y[k] - y[i]) + x[k] * (y[i] - y[j])); printf("%d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; int a[200005], v[200005]; int main() { int n, minm, maxm, k, f; long long int cnt; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } minm = a[0]; for (int i = 1; i < n; i++) { minm = min(minm, a[i]); } cnt = (long long int)minm * (long long int)n; k = 0; for (int i = 0; i < n; i++) { if (a[i] == minm) { k++; } else { v[k]++; } } if (k > 0) v[0] += v[k]; maxm = v[0]; for (int i = 0; i < k; i++) maxm = max(maxm, v[i]); cnt += (long long int)maxm; cout << cnt; return 0; }
2
/* ###################################################################################### # # # ,.--------._ # # / ''. # # ,' \ |"\ /\ /\ # # /"| / \ |__" ( \\ // ) # # "_"| / z#####z \ // \ \\ // / # # \\ ##### ##------". \// \_\\||||//_/ # # \\/-----\ / ". \ \/ _ _ \ # # \| \ | ,,--.. \ \/|(O)(O)| # # | ,.--._ \ ( | ## \) \ \/ | | # # |( ## )/ \ `-....-// |///////////////_\/ \ / # # '--'." \ \ // |____| # # /' / ) --. \ || / \ # # ,..| \.________/ `-.. \ \ \| \ 0 0 / # # _,##/ | ,/ / \ \ \ \ U / \_//_/ # # :###.- | ,/ / \ /' ""\ .\ ( / # # /####| | (.___________,---',/ | |\=._____| |_/ # # /#####| | \__|__|__|__|_,/ |####\ | || # # /######\ \ \__________/ /#####| \ || # # /|#######`. `\ /#######\ | || # # /++\#########\ \ _,' _/#########\ | || # # /++++|#########| \ .---.. ,/ ,'##########.\|_|| # # //++++|#########\. \. ,-/ ,'########,+++++\\_\\ # # /++++++|##########\. '._ _,/ ,'######,''++++++++\ # # |+++++++|###########| -----." _'#######' +++++++++++\ # # |+++++++|############\. \\ // /#######/++++ S@yaN +++\ # # ________________________\\___//______________________________________ # # / ____________________________________________________________________) # # / / _ _ # # | | | | | | # # \ \ | | _ ____ ____ | | _ # # \ \ | || \ / ___) / _ ) | | / ) # # _____) ) | | | | | | ( __ / | |< ( # # (______/ |_| |_| |_| \_____) |_| \_) # # # ###################################################################################### .e@$$$$$$$eeeu=~=._ zd$$$$$$$$$$$$$$' z$b.. _,x, z????$$$$$$$$P",-== matrix""`$$$. .; .e".. `"?$Fu^'e$$$$$be. )$$' 4r'$$nn, _. - ud$$$P zeeeu. ld$$$$$$$$$$$$c`=._,$$ <"$$$$= ' z$$$$$$e ?$$" "FE=e2R$$$$$$$$$$$$bu"h._..d. "`nMb` _ .-J$$$$$$$$,?$$b,.,J",xc3$$$$$$$$$$$$$$$" Mh. " 'MMMMP- ~ ..ze$F R$$$$$$$$$bcccd$$"J(T- "b".nMMP'b. "",c$$$$$$$$P.MMMMMMMb.< )!! >'dMMMMMMM __ .HMMMM "??$$$$$PF"uPF",, umnmnHMMMMMbx.... ''.n'MMMMMCund~ `~ ~ - 'M'HMMMh .e$ee$$?7 MMMMMT"",MMMMMMMMMMMMM.MMMMMMMMM MMMMMMMx -...e$$$P??,nMM "`,nndMMMMMMMMMMMMMMMMk`MMMMMMMP H"MMMMMMMMhx???",nHMMP",nh MMMMMM?MP"xHMMMMMMMMMF-?TMMMM" MMPTMMMMMMMMMMP"u- :n.`"%'MF.xnF.nMMMMMMMMMP".::::."Te 'M".MPTMMMMMP"zeEeP.MMMMM"..4MF,HMMMMMMMMMF'.::::::::`R. " MF dMMMf z$$$$$ MMMF'xMMMr`HMMMMMMMMM".:::::::.::...?_ T M P $$$$$$%dMF'dMMMM"xk'MPJMMMMP ::'.'';i!!!!'`^.xhMmx 'M 4$$$$$P.P,HMMMMM,HMMh TMMMMM> :!!!!!!!'`.xnMMMMMMMMMn J$$$$$ ",MMMMMM,MMMM'h TMMMML'!;!!!`.nHMMMMMMMMMMMMMMMn $$$$$P HMMMMMM,MMMM MMM."MMMM \!i`.HMMMMMMMMMMMMMMMMMMMMr $$$$$"dMMMMMMMMMMMfdMMMM.`MMMh xMMMMMMMMMMMMMMMMMMMMMMM 4$$$$$ MMMMMMMMMMMMMMMMMMMx`MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM' ,4$$$$$.MMMMMMMMMMMMMMMMMMMMh TMMMMMMMMMMMMMMMMMMMMMMMMMMMf ; J$$$$F;MMMMMMMMMMMMMMMMMMMMMM."MMMMMMMMMMMMMMMMMMMMMMP"" .db $$$$$ MMMMMMMMMMMMMMMMMMMMMMMMh."MMMMMMMMMMMMMMPF" , d$b$$$$$ MMMMMMMMMMMMMMMMMMMMMMMMMM u"?f""?= . $$$$$$$$ MMMMMMMMMMMMMMMMMMMMMMMMMfJ$b z$ d$$$$$$$ MMMMMMMMMMMMMMMMMMMMMMMMM P" ` e$$h ?$$$$$$$ MMMMMMMMMMMMMMMMMMMMMMMMf^ zF`. */ // input multiple numbers from single line // string input; // getline(cin, input); // istringstream is(input); // int num; // while(is>>num) arr.push_back(num); //(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS) // #pragma comment(linker, "/stack:200000000") //(UNCOMMENT WHEN NEEDED) #include <bits/stdc++.h> // #include<time.h> using namespace std; #pragma GCC optimize("O3") typedef long long int ll; typedef unsigned long long ull; const ll mod= 1e8; #define endl "\n" #define eps 1e-7 #define PI 3.1415926535897932384626 #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sortall(x) sort(all(x)) #define vpi vector<pi> #define vi vector<ll> #define pi pair<ll,ll> #define mk make_pair #define pb push_back #define ppb pop_back #define umap unordered_map #define uset unordered_set #define vvi vector<vector<ll>> #define minheap priority_queue<ll,vi,greater<ll>> #define maxheap priority_queue<ll> #define minheapi priority_queue<pi,vpi,greater<pi>> #define maxheapi priority_queue<pi> #define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); #define fo(i, n) for(i=0;i<n;i++) #define fox(i,a,b) for(i=a;i<b;i++) #define deb1(x) cout << #x << " = " << x << endl; #define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl #define deblp(i, a, n) fo(i, n){cout << a[i] << " ";} #define clr(x,a) memset(x, a, sizeof(x)) #define fills(a,val) fill_n(a, sizeof(a)/sizeof(a[0]), val) #define sq(x) ((x)*(x)) ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;} ll gcd(ll x,ll y){return y?gcd(y,x%y):x;} ll lcm(ll x,ll y){return x*(y/gcd(x,y)); } ll modd(ll a){return (a%mod + mod)%mod;} double findMod(double a, double b) { double mods; if (a < 0) mods = -a; else mods = a; if (b < 0) b = -b; while (mods >= b) mods = mods - b; if (a < 0) return -mods; return mods; } ll add(ll a,ll b){return modd(modd(a)+modd(b));} ll mul(ll a,ll b){return modd(modd(a)*modd(b));} int smask(int i,int pos){ return (i|(1<<pos)); } int clmask(int i,int pos){return (i&(~(1<<pos))) ; } bool chmask(int i,int pos){return (i&(1<<pos))!=0 ;} double cordist(pair<double,double> a,pair<double,double> b) {return sqrt(((a.first-b.first)*(a.first-b.first))+((a.second-b.second)*(a.second-b.second)));} long long binpow(long long a, long long b) { if (b == 0) return 1; long long res = binpow(a, b / 2); if (b % 2) return mul(mul(res, res), a); else return mul(res,res); } //ll prime[60]; //int posx[10]; //int posy[10]; //int price[30]; //char board[26][26]; // int dx[]={-1,1,0,0};//N,S,w,E // int dy[]={0,0,-1,1}; ll n,m,q,r,i,j; vi arr,brr; ll dp[2505][5000]; ll sol(int i=0,int j=0) { // deb2(i,j); if(i==arr.size()) { return 0; } if(j==brr.size()) return INT_MAX; if(dp[i][j]!=-1) return dp[i][j]; ll temp=INT_MAX; temp=min(temp,abs(arr[i]-brr[j])+sol(i+1,j+1)); temp=min(temp,sol(i,j+1)); return dp[i][j]=temp; } void solve() { cin>>n; arr.clear(); brr.clear(); fo(i,n) { cin>>m; if(m==1) { arr.pb(i); } else brr.pb(i); } clr(dp,-1); cout<<sol()<<endl; } int main() { IOS // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("input.in", "r", stdin); // // for writing output to output.txt // freopen("output.out", "w", stdout); // #endif int k=1; // init(); // cin>>k; //sieve(); // int counter=1; while(k--) { // printf("Case #%d: ",counter++); solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30) - 1; const long long linf = (1ll << 62) - 1; const double eps = 1e-9; const double pi = 4 * atan(1.0); const int N = 1e5 + 100; int n, k; long long a[N], b[N], c[N]; pair<long long, int> inter[N], pinter[N]; int cnt[N]; long long gcd(long long a, long long b) { while (b) { long long y = a % b; a = b; b = y; } return a; } inline bool intersect(int f, int s, long long &res) { const long long r = 28318237214737ll; long long d = a[f] * b[s] - a[s] * b[f]; if (d == 0) { return false; } long long dx = c[f] * b[s] - c[s] * b[f], dy = a[f] * c[s] - a[s] * c[f]; if (d < 0) { d = -d, dx = -dx, dy = -dy; } long long g = gcd(gcd(abs(dx), abs(dy)), abs(d)); d /= g, dx /= g, dy /= g; res = ((d * r + dx) * r + dy) * r; return true; } set<long long> used; int wh[N]; int myrand() { return abs((int)(((long long)rand()) << 16) ^ rand()); } double begins = clock(); inline bool solve(vector<int> cur, int k) { if ((clock() - begins) / CLOCKS_PER_SEC > 1.6) { puts("NO"); exit(0); } static const long long r1 = 218321381232747ll; if (k < 0) { return false; } if (k >= ((int)(cur).size())) { puts("YES"); k -= ((int)(cur).size()); printf("%d\n", ::k - k); for (int i : cur) { printf("%d %d\n", i + 1, -1); } return true; } long long hash = 0; for (int i : cur) { hash = hash * r1 + i + 1; } if (used.find(hash) != used.end()) { return false; } used.insert(hash); for (int it = 0; it < k * 4; it++) { int line = myrand() % ((int)(cur).size()); long long res; int interLen = 0; for (int i = 0; i < ((int)(cur).size()); i++) { if (!intersect(cur[line], cur[i], res)) { continue; } inter[interLen++] = make_pair(res, i); } sort(inter, inter + interLen); int cc = 0; fill_n(wh, ((int)(cur).size()), -1); for (int i = 0; i < interLen; i++) { wh[inter[i].second] = i; if (!i || inter[i].first != inter[i - 1].first) { cnt[cc] = 1; pinter[cc++] = inter[i]; } else { cnt[cc - 1]++; } } for (int i = 0; i < cc; i++) { if ((cnt[i] + 1) * k >= ((int)(cur).size())) { vector<int> nextToTake; for (int j = 0; j < ((int)(cur).size()); j++) { if (j != line && (wh[j] == -1 || inter[wh[j]].first != pinter[i].first)) { nextToTake.push_back(cur[j]); } } int a = cur[line], b = cur[pinter[i].second]; if (solve(nextToTake, k - 1)) { printf("%d %d\n", a + 1, b + 1); return true; } } } } return false; } long long readLong() { int x; scanf("%d", &x); return x; } void gen() { freopen("D.in", "w", stdout); int n = 1e5, k = 5, c = 1000; cout << n << " " << k << endl; vector<pair<int, int> > pt(k); for (int i = 0; i < k; i++) { pt[i] = make_pair(myrand() % c, myrand() % c); } set<pair<pair<int, int>, int> > s; for (int i = 0; i < n; i++) { int p = rand() % k; pair<int, int> pt2; for (;;) { pt2 = make_pair(myrand() % c, myrand() % c); ; if (pt[p] != pt2) { int a = pt2.second - pt[p].second, b = pt[p].first - pt2.first, c = -(pt2.first * a + pt2.second * b); if (a < 0 || (a == 0 && b < 0)) { a = -a, b = -b, c = -c; } int d = gcd(a, b); a /= d, b /= d, c /= d; if (s.find(make_pair(make_pair(a, b), c)) != s.end()) { continue; } printf("%d %d %d\n", a, b, c); s.insert(make_pair(make_pair(a, b), c)); break; } } } exit(0); } int main() { srand(time(NULL)); double begin = clock(); cin >> n >> k; for (int i = 0; i < n; i++) { a[i] = readLong(); b[i] = readLong(); c[i] = -readLong(); } vector<int> cur(n); for (int i = 0; i < n; i++) { cur[i] = i; } if (!solve(cur, k)) { puts("NO"); } 42; return 0; }
10
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > MST1, MST2; int n, m; int w, v; pair<int, int> ans[1000005]; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { cin >> w >> v; if (v) MST1.push_back({w, i}); else MST2.push_back({w, i}); } sort(MST1.begin(), MST1.end()); sort(MST2.begin(), MST2.end()); reverse(MST1.begin(), MST1.end()); reverse(MST2.begin(), MST2.end()); for (int i = 0; i < MST1.size(); i++) ans[MST1[i].second] = {i + 1, i + 2}; for (int i = 0, j = 0, k = 2; i < MST2.size(); i++) { while (j < n - 2 && MST1[j].first > MST2[i].first) j++, k = j + 2; if (j == n - 2) return 0 * puts("-1"); ans[MST2[i].second] = {j + 1, k + 1}; k++; if (k == n) j++, k = j + 2; } for (int i = 0; i < m; i++) cout << ans[i].first << ' ' << ans[i].second << '\n'; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m, j, k, i, a[100], c = 0, l = 0; cin >> n; cin >> m; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (a[i] < a[j]) { k = a[i]; a[i] = a[j]; a[j] = k; } } } while (m > 0) { m = m - a[l]; l++; c++; } cout << c; }
0
#include <bits/stdc++.h> using namespace std; long long n, k, x, y, res; long long a[222222], lvl[222222], d[222222], u[222222]; vector<long long> g[222222]; string s; void dfs1(long long v, long long depth = 0, long long par = -1) { lvl[v] = depth; for (long long to : g[v]) { if (par != to) { dfs1(to, depth + 1, v); d[v] += d[to]; } } d[v] += u[v]; } void dfs2(long long v, long long cnt, long long par = -1, long long rem = 0) { long long target = -1, foo = 0; for (long long to : g[v]) { if (to != par) { if ((d[to] - rem) * 2 > cnt) { target = to; } else { foo += d[to]; } } } if (target == -1) { res -= cnt * lvl[v]; return; } else { foo += u[v]; res -= 2 * foo * lvl[v]; rem += foo; dfs2(target, cnt - 2 * foo, v, rem); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> k; for (long long i = 1; i <= 2 * k; i++) { cin >> a[i]; u[a[i]] = 1; } for (long long i = 1; i <= n - 1; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dfs1(1); dfs2(1, 2 * k); for (long long i = 1; i <= 2 * k; i++) { res += lvl[a[i]]; } cout << res << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, M = 20; int n, m, q; int p[N], arr[N], to[N]; int last[N], ans[N], par[N][M]; int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < n; i++) { scanf("%d", p + i); if (i) to[p[i - 1]] = p[i]; } to[p[n - 1]] = p[0]; for (int i = 0; i < m; i++) scanf("%d", arr + i); memset(last, -1, sizeof last); for (int i = m - 1; i >= 0; i--) { last[arr[i]] = i; par[i][0] = last[to[arr[i]]]; } ans[m] = 1e9; for (int i = m - 1; i >= 0; i--) { for (int j = 1; j < M; j++) { if (~par[i][j - 1]) par[i][j] = par[par[i][j - 1]][j - 1]; else par[i][j] = -1; } int node = i; for (int j = 0; j < M && ~node; j++) if ((n - 1) & (1 << j)) node = par[node][j]; node = node == -1 ? 1e9 : node; ans[i] = min(ans[i + 1], node); } int s, e; while (q--) { scanf("%d%d", &s, &e); putchar(ans[s - 1] < e ? '1' : '0'); } }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; int cnt[maxn * 2]; int a[maxn]; int n; int q; int pos[maxn]; int block = 2000; int cntcnt[maxn]; int b[maxn * 2]; int ans[maxn]; struct node { int l, r; int md; int id; int ans; bool operator<(const node &p) const { if (pos[this->l] == pos[p.l]) { if (pos[this->r] == pos[p.r]) { return this->md < p.md; } return this->r < p.r; } return pos[this->l] < pos[p.l]; } } Q[maxn]; struct node2 { int p; int to; int pre; } modity[maxn]; void add(int va) { cntcnt[cnt[va]]--; cnt[va]++; cntcnt[cnt[va]]++; } void del(int va) { cntcnt[cnt[va]]--; cnt[va]--; cntcnt[cnt[va]]++; } int L, R; void mf(node2 &the) { if (the.p >= L && the.p <= R) { del(the.pre); add(the.to); } a[the.p] = the.to; } void mb(node2 &the) { if (the.p >= L && the.p <= R) { del(the.to); add(the.pre); } a[the.p] = the.pre; } void getpos() { for (int i = 1; i <= n; i++) { pos[i] = i / block; } } int main() { cntcnt[0] = 1e8; int p = 0; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); b[p++] = a[i]; } block = max((int)pow(n, 2 * 1.0 / 3), 1); getpos(); int now = 0; int q1 = 0; int q2 = 1; for (int i = 0; i < q; i++) { int op; scanf("%d", &op); if (op == 1) { scanf("%d%d", &Q[q1].l, &Q[q1].r); Q[q1].ans = 1; Q[q1].md = now; Q[q1].id = q1; q1++; } else { int pp; int va; scanf("%d%d", &pp, &va); now++; modity[q2].p = pp; modity[q2].pre = a[pp]; modity[q2++].to = va; a[pp] = va; b[p++] = va; } } sort(b, b + p); sort(Q, Q + q1); int sz = unique(b, b + p) - b; for (int i = 1; i <= n; i++) { a[i] = lower_bound(b, b + sz, a[i]) - b; } for (int i = 1; i <= q2; i++) { modity[i].pre = lower_bound(b, b + sz, modity[i].pre) - b; modity[i].to = lower_bound(b, b + sz, modity[i].to) - b; } int curt = now; R = 0; L = 1; for (int i = 0; i < q1; i++) { while (R < Q[i].r) add(a[++R]); while (L < Q[i].l) del(a[L++]); while (R > Q[i].r) del(a[R--]); while (L > Q[i].l) add(a[--L]); while (curt < Q[i].md) mf(modity[++curt]); while (curt > Q[i].md) mb(modity[curt--]); while (cntcnt[Q[i].ans] > 0) Q[i].ans++; ans[Q[i].id] = Q[i].ans; } for (int i = 0; i < q1; i++) { printf("%d\n", ans[i]); } }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int test; test = 1; cin >> test; while (test--) { long long int n, k, ans = 0; cin >> n >> k; while (n > 0) { if (n % k) { ans += n % k; n -= n % k; } if (n % k == 0 && n != 0) { n /= k; ans++; } } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; long long s[20001][2]; int main() { int n; cin >> n; for (size_t i = 0; i < 20001; i++) { s[i][0] = 0; s[i][1] = 0; } long long sum = 0; for (int i = 0; i < n; i++) { int a; cin >> a; for (size_t j = 0; j < 20001; j++) { s[j][i % 2] = 0; } for (int j = 0; j < 20001; j++) { if (j + a < 20001) { s[j + a][i % 2] += s[j][(i + 1) % 2]; s[j + a][i % 2] %= 1000000007; } if (j - a >= 0) { s[j - a][i % 2] += s[j][(i + 1) % 2]; s[j - a][i % 2] %= 1000000007; } if (j == a + 10000 || j == 10000 - a) { s[j][i % 2]++; s[j][i % 2] %= 1000000007; } } sum += s[10000][i % 2]; sum %= 1000000007; } cout << sum; }
7
#include <bits/stdc++.h> using namespace std; const int64_t posinf = 9223372036854775807LL; const int64_t neginf = 9223372036854775808LL; const uint64_t ullinf = 18446744073709551615ULL; const uint64_t bigprime = 2305843009213693951ULL; const uint64_t smallprime = 2147483647ULL; const int littleprime = 3571; const int tinyprime = 199; const int64_t modbase = 1000000007LL; const int64_t inv2mod = 500000004LL; const int sneginf = -2147483647; const long double pi = 3.1415926535897932384626433832795; struct vec2 { int x, y; vec2() : x(0), y(0) {} vec2(int x_, int y_) : x(x_), y(y_) {} vec2 operator-(const vec2 &b) const { return vec2(x - b.x, y - b.y); } vec2 operator+(const vec2 &b) const { return vec2(x + b.x, y + b.y); } double length() const { return sqrt(double(x * x + y * y)); } bool operator==(const vec2 &b) const { return x == b.x && y == b.y; } }; int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); cin.tie(0); vector<int> primes; vector<bool> sieve(1000001, false); int x2; cin >> x2; for (int i = 2; i <= (x2 + 1) / 2; i++) { if (sieve[i]) continue; primes.emplace_back(i); for (int j = i + i; j <= (x2 + 1) / 2; j += i) sieve[j] = true; } set<int> x1can; int cnt = 0; for (int p : primes) { if (x2 % p == 0) { int dv = x2 / p; int x1m = (dv - 1) * p + 1; for (int x1 = x1m; x1 <= x2; x1++) { x1can.emplace(x1); cnt++; } } } int x0min = 1000000; for (int p : primes) { for (int mult = 2; mult <= x2 / p; mult++) { int num = p * mult; if (x1can.find(num) != x1can.end()) { int x0 = p * (mult - 1) + 1; x0min = min(x0min, x0); } } } cout << x0min; return 0; }
4
#include <bits/stdc++.h> using namespace std; inline long long ceil(long long a, long long b) { return (a + b - 1) / b; } void func() { long long k; cin >> k; long long n = 3, x = 1; for (long long i = 4; i < 101; i++) { long long temp = (i * (i - 1) * (i - 2)) / 6; if (temp <= k) { n = i; x = temp; } else break; } list<long long> adj[101]; for (long long i = 1; i < n + 1; i++) { for (long long j = 1; j < n + 1; j++) { if (i == j) continue; else adj[i].push_back(j); } } long long cnt = k - x, curr = n + 1; while (cnt) { long long x = 1; for (long long i = 2; i < n + 1; i++) { if (i * (i - 1) / 2 <= cnt) { x = i; } else break; } for (long long i = 1; i < x + 1; i++) { adj[curr].push_back(i); adj[i].push_back(curr); } cnt -= (x * (x - 1)) / 2; curr++; } long long ans[101][101]; for (long long i = 1; i < 101; i++) for (long long j = 1; j < 101; j++) ans[i][j] = 0; for (long long i = 1; i < 101; i++) { for (auto j : adj[i]) ans[i][j] = 1; } cout << "100\n"; for (long long i = 1; i < 101; i++) { for (long long j = 1; j < 101; j++) cout << ans[i][j]; cout << "\n"; } } int main() { { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long ntc = 1; for (long long i = 1; i < ntc + 1; i++) { func(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int x[400000 + 5], y[400000 + 5], l1[400000 + 5], r1[400000 + 5], l2[400000 + 5], r2[400000 + 5]; vector<int> v[400000 + 5]; int main() { int n, i, j; long long ans = 0; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d%d", &x[i], &y[i]); x[i] += 1e5; y[i] += 1e5; x[i] += y[i]; y[i] = x[i] - 2 * y[i]; v[x[i]].push_back(y[i]); } for (i = 0; i <= 400000; ++i) { if (i) l1[i] = l1[i - 1], r1[i] = r1[i - 1]; else l1[i] = 400000 + 1, r1[i] = -400000 - 1; if (i & 1) for (j = 0; j < v[i].size(); ++j) l1[i] = min(l1[i], v[i][j] + 1), r1[i] = max(r1[i], v[i][j] - 1); } for (i = 400000; i; --i) { if (i < 400000) l2[i] = l2[i + 1], r2[i] = r2[i + 1]; else l2[i] = 400000 + 1, r2[i] = -400000 - 1; if (i & 1) for (j = 0; j < v[i].size(); ++j) l2[i] = min(l2[i], v[i][j] + 1), r2[i] = max(r2[i], v[i][j] - 1); else if (max(l1[i], l2[i]) <= min(r1[i], r2[i])) ans += min(r1[i], r2[i]) - max(l1[i], l2[i]) + 2 >> 1; } for (i = 0; i <= 400000; ++i) { if (i) l1[i] = l1[i - 1], r1[i] = r1[i - 1]; else l1[i] = 400000 + 1, r1[i] = -400000 - 1; if (~i & 1) for (j = 0; j < v[i].size(); ++j) l1[i] = min(l1[i], v[i][j] + 1), r1[i] = max(r1[i], v[i][j] - 1); } for (i = 400000; i; --i) { if (i < 400000) l2[i] = l2[i + 1], r2[i] = r2[i + 1]; else l2[i] = 400000 + 1, r2[i] = -400000 - 1; if (~i & 1) for (j = 0; j < v[i].size(); ++j) l2[i] = min(l2[i], v[i][j] + 1), r2[i] = max(r2[i], v[i][j] - 1); else if (max(l1[i], l2[i]) <= min(r1[i], r2[i])) ans += min(r1[i], r2[i]) - max(l1[i], l2[i]) + 2 >> 1; } printf("%I64d", ans); }
8
#include <bits/stdc++.h> using namespace std; int n, m; char s[600]; int dp[501][26][26]; pair<int, int> fr[501][26][26]; void output(int i, int j, int k) { if (!i) return; output(i - 1, fr[i][j][k].first, fr[i][j][k].second); for (int i = 0; i < m; i++) putchar((i % 2 ? k : j) + 'a'); puts(""); } int main() { scanf("%d%d", &n, &m); gets(s); memset(dp, 63, sizeof(dp)); for (int j = 0; j < 26; j++) for (int k = 0; k < 26; k++) dp[0][j][k] = 0; for (int i = 1; i <= n; i++) { gets(s); for (int j = 0; j < 26; j++) for (int k = 0; k < 26; k++) { if (j == k) continue; int cnt = 0; for (int t = 0; t < m; t += 2) if (s[t] - 'a' != j) cnt++; for (int t = 1; t < m; t += 2) if (s[t] - 'a' != k) cnt++; for (int x = 0; x < 26; x++) if (x != j) for (int y = 0; y < 26; y++) if (y != k) { if (x == y) continue; if (dp[i - 1][x][y] + cnt < dp[i][j][k]) { dp[i][j][k] = dp[i - 1][x][y] + cnt; fr[i][j][k] = {x, y}; } } } } int x = 0, y = 0, ans = 1070000000; for (int j = 0; j < 26; j++) for (int k = 0; k < 26; k++) if (dp[n][j][k] < ans) ans = dp[n][x = j][y = k]; printf("%d\n", dp[n][x][y]); output(n, x, y); }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char arr[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } vector<int> v; for (int i = 0; i < n; i++) { short counter = 0; short counter1 = 0; for (int j = 0; j < n; j++) { if (arr[i][j] == 'C') { counter++; } if (arr[j][i] == 'C') { counter1++; } } v.push_back(counter); v.push_back(counter1); } int sum = 0; for (int i = 0; i < v.size(); i++) { if (v[i] != 0 && v[i] != 1) { sum = sum + (v[i] * (v[i] - 1)) / 2; } } cout << sum; }
0
#include <bits/stdc++.h> using namespace std; unordered_map<long long, long long> r; vector<pair<long long, long long> > g[1024]; long long md; int k; int hs(long long a) { return (((a << 30) ^ (a << 17) ^ a) >> 17) & 1023; } long long gt1(long long a) { if (a == 0) return 0; if (a == 1) return 1ll * k * (k + 1) / 2; for (auto v : g[hs(a)]) if (v.first == a) return v.second; long long c = 1ll * (a - 1) * k + 1; long long uu; long long cur = c + c / md - 2; long long ls = -1, lans = 0; if (c <= k) uu = c; else { while (1) { long long gs = (cur + md) / (md + 1); if (gs != ls) ls = gs, lans = gt1(gs); if (lans > cur) gs--; if (cur - gs < c) cur++; else break; } uu = cur; } long long us = 0; long long cc = uu + k - 1; long long ccc = (cc + md) / (md + 1); if (ccc == ls) { if (lans > cc) ccc--, us = gt1(ccc); else us = lans; } else { long long gg = gt1(ccc); if (gg > cc) { ccc--; if (ccc == ls) us = lans; else us = gt1(ccc); } else us = gg; } cur = k * (2 * uu + k - 1) / 2; if (us >= uu) cur += uu + k - us; g[hs(a)].push_back(make_pair(a, cur)); return cur; } long long gt3(long long c, long long m, long long lans) { long long ur = (c + md) / (md + 1); if (ur == m) { if (lans > c) ur--; } else if (gt1(ur) > c) ur--; return ur; } long long rd() { return (rand() * 1ll << 45) + (rand() * 1ll << 30) + (rand() * 1ll << 15) + rand(); } void read(long long &a) { a = 0; char ch = getchar(); while (ch > '9' || ch < '0') ch = getchar(); while (ch >= '0' && ch <= '9') a = a * 10 + ch - '0', ch = getchar(); } void read(int &a) { a = 0; char ch = getchar(); while (ch > '9' || ch < '0') ch = getchar(); while (ch >= '0' && ch <= '9') a = a * 10 + ch - '0', ch = getchar(); } int st[30]; void otp(long long a) { int cnt = 0; while (a) st[cnt++] = a % 10, a /= 10; for (int i = cnt - 1; i >= 0; i--) putchar('0' + st[i]); putchar('\n'); } int main() { int test = 0; if (test) { freopen("1.out", "w", stdout); } int t; if (test) t = 1e5; else read(t); for (int i = 0; i < t; i++) { for (int j = 0; j < 1024; j++) g[j].clear(); long long n; if (test) n = rd(), k = 3; else read(n), read(k); md = 1ll * k * k; long long id = gt3(n, -1, -1); long long ans = 0; if (gt1(id) == n) ans = 1ll * id * (k + 1); else { n -= id; ans = (n - 1) / k * (k + 1) + (n - 1) % k + 1; } otp(ans); } return 0; }
13
#include <bits/stdc++.h> using namespace std; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; const long long MOD = 1e9 + 7; const long long INF = 1e9; string s; vector<int> parent(1000, -1); int nd = 0, r = 0; vector<string> name(1000); int a = 0, b = -1; void add(int prnt) { a = ++b; while (isalpha(s[b])) b++; int ci = nd++; parent[ci] = prnt; name[ci] = s.substr(a, b - a); while (s[b] != '.') add(ci); b++; } int main() { ios::sync_with_stdio(false), cin.tie(0); cin >> s; while (b < (int)s.length()) { a = ++b; while (isalpha(s[b])) b++; int ci = nd++; name[ci] = s.substr(a, b); while (s[b] != '.') add(ci); b++; } for (int i = 0; i < nd; i++) { int cur = parent[i]; while (cur != -1) { if (name[cur] == name[i]) r++; cur = parent[cur]; } } cout << r << endl; cin.ignore(), cin.get(); }
4
#include <bits/stdc++.h> using namespace std; int t; int n; int sol, temp; vector<string> a; void duplicates(int i); int main() { cin >> t; for (; t; --t) { sol = 0; cin >> n; a.resize(n); for (int i = 0; i < n; ++i) { temp = 0; cin >> a[i]; duplicates(i); if (temp) ++sol; } cout << sol << '\n'; for (int i = 0; i < n; ++i) cout << a[i] << '\n'; } return 0; } void duplicates(int i) { bool ok = false; for (int j = 0; j < i; ++j) if (a[i] == a[j]) { ++temp; a[i][1] = (a[j][1] + 1) % 10 + '0'; ok = true; } if (ok) duplicates(i); }
3
#include <bits/stdc++.h> using namespace std; template <class T> struct rge { T b, e; }; template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }; template <class A, class B> ostream &operator<<(ostream &os, pair<A, B> p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream &operator<<(ostream &os, rge<T> d) { os << "["; for (auto it = d.b; it != d.e; it++) os << (it == d.b ? "" : ", ") << *it; return os << "]"; } const int N = 1e4 + 5; const int M = 1e5 + 5; struct prefix_tree { vector<map<char, int> > trie; vector<int> who; prefix_tree() { new_node(); } map<char, int> &operator[](int u) { return trie[u]; } int new_node() { trie.push_back(map<char, int>()); who.push_back(-1); return (int)(trie).size() - 1; } void insert(string &s, int i, int u = 0) { for (char c : s) { c = tolower(c); if (!trie[u][c]) { trie[u][c] = new_node(); } u = trie[u][c]; } who[u] = i; } } trie; struct { int to = -1; int who = -1; } dp[N]; string s, dict[M]; int n, m; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15); cin >> n >> s; cin >> m; for (int i = (0); i <= ((m)-1); i += (+1)) { cin >> dict[i]; trie.insert(dict[i], i); } for (int i = (n - 1); i >= (0); i += (-1)) { if (dp[i + 1].to != -1 || i + 1 == n) { int u = 0; for (int j = (i); j >= (0); j += (-1)) { u = trie[u][s[j]]; if (!u) { break; } if (trie.who[u] != -1) { dp[j].to = i + 1; dp[j].who = trie.who[u]; } } } } for (int i = 0; dp[i].to != -1; i = dp[i].to) { cout << dict[dp[i].who] << " "; } cout << '\n'; return 0; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; template <class T> inline void scan_d(T &ret) { char c; int flag = 0; ret = 0; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') { flag = 1; c = getchar(); } while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar(); if (flag) ret = -ret; } int a[500005], flag[500005]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); flag[i] = 0; } int ans = 0; for (int i = n - 1; i >= 2; i--) { if (a[i - 1] == a[i + 1] && a[i] != a[i - 1]) { flag[i] = flag[i + 1] + 1; ans = max(ans, flag[i]); } } printf("%d\n", ans / 2 + (ans % 2 ? 1 : 0)); printf("%d", a[1]); for (int i = 2; i <= n - 1; i++) { if (flag[i] == 0) printf(" %d", a[i]); else if (flag[i] + 1 != flag[i - 1]) { int p = a[i]; if (flag[i] & 1) { for (int j = 1; j <= flag[i]; j++) printf(" %d", p ^ 1); } else { for (int j = 1; j <= flag[i]; j++) printf(" %d", j <= flag[i] / 2 ? p ^ 1 : p); } } } printf(" %d\n", a[n]); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, t, ans = 1, cost[100100]; set<int> nums; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> t; set<int>::iterator it = nums.upper_bound(t); if (it == nums.end()) cost[t]--; else { int to = *it; it++; if (it == nums.end()) cost[to]++; } nums.insert(t); } for (int i = 2; i <= n; i++) if (cost[i] > cost[ans]) ans = i; cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, *ar, *br; cin >> n; ar = new int[n]; br = new int[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; br[i] = ar[i]; } sort(br, br + n); int sum = 0; for (int i = n - 1; i >= 0; i--) { sum += br[i] * (n - i - 1) + 1; } cout << sum << endl; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < n; j++) { if (br[i] == ar[j]) { cout << j + 1 << " "; ar[j] = 0; continue; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; bool isprime(long long n) { long long i; if (n == 2 || n == 3) return 1; long long flag = 0; for (i = 2; i <= sqrt(n) + 1; i++) { if (n % i == 0) { flag = 1; break; } } if (flag) return 0; else return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; t = 1; while (t--) { long long n, m; cin >> n >> m; long long a, b, c, d; cin >> a >> b >> c >> d; long long arr[n]; if (m < n + 1 || n == 4) { cout << -1; continue; } arr[0] = a; arr[n - 1] = b; arr[1] = c; arr[n - 2] = d; long long idx = 2; for (long long i = 1; i < n + 1; i++) { if (i != a && i != b && i != c && i != d) { arr[idx] = i; idx++; } } for (long long i = 0; i < n; i++) cout << arr[i] << " "; cout << "\n"; cout << c << " " << a << " "; for (long long i = 2; i < n - 2; i++) { cout << arr[i] << " "; } cout << arr[n - 1] << " " << arr[n - 2]; } return 0; }
4
#include <bits/stdc++.h> char s[1005]; int n, m, k, ans[23], G, R, has, oG, oR; int main(void) { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) { has = G = R = 0; scanf("%s", s + 1); for (int j = 1; j <= m; ++j) if (s[j] == '-') has = 1; else if (s[j] == 'G') G = j; else if (s[j] == 'R') R = j; if ((!G && !R) || !has) continue; if (!G) oR = 1; else if (!R) oG = 1; else for (int j = 0; j <= 20; ++j) if ((1 << j) & (abs(R - G) - 1)) ++ans[j]; } if (oR && oG) return puts("Draw") * 0; if (oR) return puts((!1) ? "First" : "Second") * 0; if (oG) return puts((!0) ? "First" : "Second") * 0; for (int i = 0; i <= 20; ++i) if (ans[i] % (k + 1)) return puts((!0) ? "First" : "Second") * 0; return puts((!1) ? "First" : "Second") * 0; }
9
#include <bits/stdc++.h> using namespace std; unordered_map<long long, long long> mp; void mark_current(char a[][100], int i, int j, int r, int c) { if (i < 0 || j < 0 || i >= r || j >= c || a[i][j] != 'B') return; a[i][j] = '.'; mark_current(a, i, j - 1, r, c); mark_current(a, i, j + 1, r, c); mark_current(a, i - 1, j, r, c); mark_current(a, i + 1, j, r, c); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int r, c; cin >> r >> c; char a[100][100]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) cin >> a[i][j]; } int cnt = 0; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (a[i][j] == 'B') { mark_current(a, i, j, r, c); cnt++; } } } cout << cnt; return 0; }
2
#include <bits/stdc++.h> using namespace std; int map[2010][2010]; int map2[2010][2010]; int vis[2010][2010]; int minx, maxx, miny, maxy; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int n; int cnt; vector<pair<int, int> > pts; int que[4000100][2], qs, qe; inline void dfs(int x, int y) { qs = qe = 0; que[qe][0] = x; que[qe][1] = y; qe++; vis[x][y] = 1; while (qs < qe) { int x = que[qs][0], y = que[qs][1]; qs++; pts.push_back(make_pair(x, y)); minx = min(x, minx); miny = min(y, miny); maxx = max(x, maxx); maxy = max(y, maxy); int i; for (i = 0; i < 4; i++) { int xx = x + dx[i], yy = y + dy[i]; if (xx >= 0 && xx < n && yy >= 0 && yy < n && map[xx][yy] && !vis[xx][yy]) { vis[xx][yy] = 1; que[qe][0] = xx; que[qe][1] = yy; qe++; } } cnt++; } } inline bool isg(int x, int y) { return map[x - 1][y] + map[x][y - 1] + map[x + 1][y] + map[x][y + 1] >= 3; } inline bool ise(int x, int y) { return map[x - 1][y] + map[x][y - 1] + map[x + 1][y] + map[x][y + 1] <= 1; } const double pi = acos(-1); const double sq2 = sqrt(2); inline int isc() { int w = maxx - minx + 1, h = maxy - miny + 1; if (w <= 10 || h <= 10) return -1; int i, mt = 0; int cx = minx + w / 2, cy = miny + h / 2; for (i = 0; i < pts.size(); i++) { int x = pts[i].first - cx, y = pts[i].second - cy; { int xx = (int)((x + y) / sq2) + cx; int yy = (int)((x - y) / sq2) + cy; if (xx >= 0 && xx < n && yy >= 0 && yy < n && map[xx][yy]) mt++; } { int xx = (int)((x - y) / sq2) + cx; int yy = (int)((x + y) / sq2) + cy; if (xx >= 0 && xx < n && yy >= 0 && yy < n && map[xx][yy]) mt++; } { int xx = (int)((x + y) / sq2) + cx; int yy = (int)((y - x) / sq2) + cy; if (xx >= 0 && xx < n && yy >= 0 && yy < n && map[xx][yy]) mt++; } { int xx = (int)((y - x) / sq2) + cx; int yy = (int)((x + y) / sq2) + cy; if (xx >= 0 && xx < n && yy >= 0 && yy < n && map[xx][yy]) mt++; } } if (mt >= pts.size() * 4 * 0.88) return 1; return 0; return 0; } int sum[2010][2010]; int main() { int i, j; scanf("%d", &n); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) { scanf("%d", &map2[i][j]); sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + map2[i][j]; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) { int x1 = max(i - 2, 0), x2 = min(i + 3, n), y1 = max(j - 2, 0), y2 = min(j + 3, n); int x = sum[x1][y1] + sum[x2][y2] - sum[x1][y2] - sum[x2][y1]; if (x >= 17) map[i][j] = 1; } int c = 0, s = 0; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (map[i][j] && vis[i][j] == 0) { minx = miny = 10000; maxx = maxy = -10000; cnt = 0; pts.clear(); dfs(i, j); int x = isc(); if (x == 1) c++; else if (x == 0) s++; } printf("%d %d\n", c, s); }
7
#include <bits/stdc++.h> using namespace std; int K, N; int O; void input() { scanf("%d%d", &N, &K); O = 0; for (int i = 0; i < N; i++) { int v; scanf("%d", &v); if (v % 2 == 1) O++; } } void even() { printf("Daenerys\n"); exit(0); } void odd() { printf("Stannis\n"); exit(0); } int main() { input(); int moves = N - K; int movesSt = (moves + 1) / 2, movesDa = (moves) / 2; if (O == 0) { even(); } if (O == N) { if (K % 2 == 0) even(); else odd(); } if (K == N) { if (O % 2 == 0) even(); else odd(); } if (O <= movesDa) { even(); } if (N - O <= movesSt && K % 2 == 1) { odd(); } if (N - O <= movesDa && K % 2 == 0) { even(); } if (moves % 2 == 0) { even(); } else { odd(); } }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> V[maxn]; int d[maxn]; int son[maxn]; long long ans = 0; void dfs(int u, int fa, int k) { son[u] = 1; d[u] = k; for (auto v : V[u]) { if (v == fa) continue; dfs(v, u, k ^ 1); son[u] += son[v]; } } int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); V[x].push_back(y); V[y].push_back(x); } dfs(1, 0, 0); int sum = 0; for (int i = 1; i <= n; i++) { ans += (long long)son[i] * (n - son[i]); sum += d[i]; } ans += (long long)sum * (n - sum); cout << ans / 2 << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int a[111][111][11], n, q, c, x, y, rx, ry, t, v; int main() { cin >> n >> q >> c; for (int i = 1; i <= n; i++) { cin >> x >> y >> t; a[x][y][t]++; } for (int l = 0; l <= c; l++) { for (int i = 1; i <= 100; i++) for (int j = 1; j <= 100; j++) a[i][j][l] += a[i][j - 1][l]; for (int i = 1; i <= 100; i++) for (int j = 1; j <= 100; j++) a[i][j][l] += a[i - 1][j][l]; } for (int i = 1; i <= q; i++) { v = 0; cin >> t >> x >> y >> rx >> ry; for (int j = 0; j <= c; j++) v += (j + t) % (c + 1) * (a[rx][ry][j] - a[rx][y - 1][j] - a[x - 1][ry][j] + a[x - 1][y - 1][j]); cout << v << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1000 + 10; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int k; for (k = 0; k < s.size(); k++) if (s[k] != '0') break; int cnt = 0; for (int i = k; i < s.size(); i++) { if (s[i] == '0') cnt++; } if (cnt >= 6) cout << "yes" << endl; else cout << "no" << endl; }
1
#include <bits/stdc++.h> using namespace std; int i, j, dp[2000][2000], n, k, m; int main() { dp[0][0] = 1; cin >> n >> m; for (int kk = 0; kk < n; kk++) { int x; scanf("%d", &x); for (i = 500; i >= 0; i--) for (j = 500; j >= 0; j--) if (dp[i][j]) dp[i + x][j] = dp[i][j + x] = 1; } for (i = 0; i <= m; i++) k += dp[i][m - i]; printf("%d\n", k); for (i = 0; i <= m; i++) if (dp[i][m - i]) printf("%d ", i); }
5
#include <bits/stdc++.h> using namespace std; int n, m; int e = 1e5; set<pair<int, int> > s1, s2, used1, used2; set<int> p1[200000], p2[200000], p3[200000], p4[200000]; int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1}; void dfs1(int x, int y) { used1.insert(make_pair(x, y)); for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (s1.count(make_pair(nx, ny)) != 0) if (nx >= 1 && nx <= min(n, e) && ny >= 1 && ny <= min(n, e) && used1.count(make_pair(nx, ny)) == 0) dfs1(nx, ny); } set<int>::const_iterator it = p1[x].upper_bound(y); if (it != p1[x].end() && used1.count(make_pair(x, *it)) == 0) dfs1(x, *it); it = p1[x + 1].upper_bound(y); if (it != p1[x + 1].end() && used1.count(make_pair(x + 1, *it)) == 0) dfs1(x + 1, *it); it = p3[y].lower_bound(x); if (it != p3[y].begin()) { it--; if (used1.count(make_pair(*it, y)) == 0) dfs1(*it, y); } it = p3[y - 1].lower_bound(x); if (it != p3[y - 1].begin()) { it--; if (used1.count(make_pair(*it, y - 1)) == 0) dfs1(*it, y - 1); } } void dfs2(int x, int y) { used2.insert(make_pair(x, y)); for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (s2.count(make_pair(nx, ny)) != 0) if (nx >= 1 && nx <= min(n, e) && ny >= 1 && ny <= min(n, e) && used2.count(make_pair(nx, ny)) == 0) dfs2(nx, ny); } set<int>::const_iterator it = p2[y].upper_bound(x); if (it != p2[y].end() && used2.count(make_pair(*it, y)) == 0) dfs2(*it, y); it = p2[y + 1].upper_bound(x); if (it != p2[y + 1].end() && used2.count(make_pair(*it, y + 1)) == 0) dfs2(*it, y + 1); it = p4[x].lower_bound(y); if (it != p4[x].begin()) { it--; if (used2.count(make_pair(x, *it)) == 0) dfs2(x, *it); } it = p4[x - 1].lower_bound(y); if (it != p4[x - 1].begin()) { it--; if (used2.count(make_pair(x - 1, *it)) == 0) dfs2(x - 1, *it); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; if (x < e && y < e) { s1.insert(make_pair(x, y)); p1[x].insert(y); p3[y].insert(x); } if (n - x < e && n - y < e) { s2.insert(make_pair(n - x + 1, n - y + 1)); p2[n - y + 1].insert(n - x + 1); p4[n - x + 1].insert(n - y + 1); } } for (int i = 2; i <= min(n, e); i++) if (s1.count(make_pair(1, i)) != 0) if (used1.count(make_pair(1, i)) == 0) dfs1(1, i); for (int i = 1; i <= min(n, e); i++) if (s2.count(make_pair(i, 1)) != 0) if (used2.count(make_pair(i, 1)) == 0) dfs2(i, 1); for (long long i = 1; i <= min(e, n); i++) if (used1.count(make_pair(i, 1)) != 0 || used2.count(make_pair(1, i)) != 0 || used1.count(make_pair(n, i)) != 0 || used2.count(make_pair(i, n)) != 0) { cout << "-1"; return 0; } cout << 2 * n - 2; }
8
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e6 + 20; const int INF = 0x3f3f3f3f; long long a[MAX_N]; long long diff[MAX_N]; int n, m; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); long long sum = 0; for (int i = 1; i <= n; i++) { diff[i] = a[i] - a[i - 1]; if (diff[i] > 0 && i != 1) sum += diff[i]; } long long k = 0; k = (diff[1] - sum) / 2; printf("%I64d\n", max(diff[1] - k, sum + k)); scanf("%d", &m); while (m--) { long long l, r, x; scanf("%I64d%I64d%I64d", &l, &r, &x); if (l == 1) { diff[1] += x; } else { if (x < 0) { if (diff[l] > 0) sum -= min(diff[l], -x); } else { if (diff[l] + x >= 0) sum += min(diff[l] + x, x); } diff[l] += x; } if (r + 1 <= n) { if (x > 0) { if (diff[r + 1] > 0) sum -= min(diff[r + 1], x); } else { if (diff[r + 1] - x >= 0) sum += min(diff[r + 1] - x, -x); } diff[r + 1] -= x; } k = (diff[1] - sum) / 2; printf("%I64d\n", max(diff[1] - k, sum + k)); } }
7
#include <bits/stdc++.h> using namespace std; int N, K; int ts[5000]; int ds[5000]; int m[5000][5000]; int main() { scanf("%d%d", &N, &K); for (int i = 0; i < (N); ++i) { scanf("%d%d", ts + i, ds + i); --ts[i]; } ts[N] = 86400; m[0][0] = 0; int best = ts[0]; for (int i = (1); i <= (N); ++i) { int p = m[i - 1][0]; if (ts[i - 1] >= p) m[i][0] = ts[i - 1] + ds[i - 1]; else m[i][0] = p + ds[i - 1]; int opt = ts[i] - m[i][0]; best = max(best, opt); } for (int n = (1); n <= (N); ++n) { for (int k = (1); k <= (min(K, n)); ++k) { int o1 = m[n - 1][k - 1]; int o2 = m[n - 1][k]; if (ts[n - 1] >= o2) o2 = ts[n - 1] + ds[n - 1]; else o2 += ds[n - 1]; m[n][k] = min(o1, o2); int opt = ts[n] - m[n][k]; best = max(best, opt); } } if (N == 0) best = 86400; printf("%d\n", best); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k, x, y, z, fi, fr, p[555][555][2], v[555][555][2], qx[660000], qy[660000], qz[660000]; vector<int> g[555], r[2]; int main() { scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } p[1][n][0] = 1; qy[0] = n; qx[0] = fr = 1; qz[0] = fi = 0; while (fi < fr) { x = qx[fi]; y = qy[fi]; z = qz[fi++]; if (z == 0) for (j = 0; j < g[x].size(); j++) { k = g[x][j]; if (p[k][y][1] == 0) { p[k][y][1] = p[x][y][z] + 1; v[k][y][1] = x; qx[fr] = k; qy[fr] = y; qz[fr++] = 1; } } else for (j = 0; j < g[y].size(); j++) { k = g[y][j]; if (p[x][k][0] == 0 && x != k) { p[x][k][0] = p[x][y][z] + 1; v[x][k][0] = y; qx[fr] = x; qy[fr] = k; qz[fr++] = 0; } } } if (p[n][1][0]) { printf("%d\n", (p[n][1][0] - 1) / 2); for (x = n, y = 1, z = 0; p[x][y][z] > 0; z = 1 - z) if (z == 0) { r[1].push_back(y); y = v[x][y][z]; } else { r[0].push_back(x); x = v[x][y][z]; } r[0].push_back(1); for (j = 0; j < 2; j++) for (i = r[j].size() - 1; i >= 0; i--) { printf("%d", r[j][i]); if (i) putchar(' '); else putchar('\n'); } } else puts("-1"); return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int MAX = 2e5 + 6; char ara[MAX]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { int tmp; cin >> tmp; ara[i] = tmp; } int q; cin >> q; while (q--) { int l, r, x, y; cin >> l >> r >> x >> y; char xx = x, yy = y; for (int i = l; i <= r; i++) { ara[i] = (ara[i] == xx ? yy : ara[i]); } } for (int i = 1; i <= n; i++) cout << int(ara[i]) << " "; cout << '\n'; return 0; }
8
#include <bits/stdc++.h> int main() { long long t, i, x, y, a, b; scanf("%lld", &t); for (i = 1; i <= t; i++) { scanf("%lld%lld%lld%lld", &x, &y, &a, &b); if ((y - x) % (a + b) == 0) printf("%lld\n", (y - x) / (a + b)); else printf("-1\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int v[maxn], n, m, ans; int main() { string s; int i, t, p; cin >> m >> s; if (s[0] != '-') s = "+" + s; for (i = 0; i < s.length(); i += 3) { p = s[i++] == '+' ? 1 : -1; for (t = 0; s[i] >= '0' && s[i] <= '9'; ++i) t = t * 10 + s[i] - '0'; if (s[i] == '*') ++i; else t = 1; t *= p; v[++n] = t; ans += (m - (s[i] == 'a')) * t; } sort(v + 1, v + n + 1); for (i = 1; i <= n; ++i) ans += i * v[i]; cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { int a, b, n; cin >> a >> b >> n; vector<int> v(3); v[0] = a; v[1] = b; v[2] = a ^ b; cout << v[n % 3] << '\n'; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; int res = 360; for (int i = 0; i < n; ++i) { int cur = 0; for (int j = 0; j < n; ++j) { cur += v[(i + j) % n]; res = min(res, abs(cur - (360 - cur))); } } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, a[N], suf[2][N], o, e, ans; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { suf[(i % 2)][i] = a[i] + suf[(i % 2)][i + 1]; suf[((i + 1) % 2)][i] = suf[((i + 1) % 2)][i + 1]; } for (int i = 0; i < n; i++) { if (o + suf[0][i + 1] == e + suf[1][i + 1]) ans++; if ((i % 2)) o += a[i]; else e += a[i]; } cout << ans << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; double t1, t2; struct node { double x, y; } c, h, s; double dist(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } double dist(node a, node b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double work(double du) { node x = {s.x * (1 - du) + h.x * du, s.y * (1 - du) + h.y * du}; double ac = dist(x, c), as = dist(x, s), ah = dist(x, h); if (ac + ah <= t2 && ac + as <= t1) return min(t2 - ah, t1 - as); double l = 0, r = 1.0; for (int i = 1; i <= 300; i++) { double mid = (l + r) / 2; node b = {c.x * (1 - mid) + x.x * mid, c.y * (1 - mid) + x.y * mid}; double bc = dist(b, c), bs = dist(b, s), bh = dist(b, h); if (bc + bh <= t2 && bc + bs <= t1) l = mid; else r = mid; } node b = {c.x * (1 - l) + x.x * l, c.y * (1 - l) + x.y * l}; return dist(b, c); } int main() { int i, j, u, v; while (cin >> t1 >> t2) { cin >> c.x >> c.y; cin >> h.x >> h.y; cin >> s.x >> s.y; double a = dist(c, s), b = dist(c, h), c = dist(h, s); if (b + t2 >= a + c) { printf("%.8lf\n", min(a + t1 + c, b + t2)); continue; } t1 += a + 1e-10; t2 += b + 1e-10; double l = 0, r = 1.0, ans = 0; for (i = 1; i <= 300; i++) { double mid1 = (l + r) / 2.0, mid2 = (mid1 + r) / 2.0; double ans1 = work(mid1), ans2 = work(mid2); if (ans1 < ans2) l = mid1; else r = mid2; ans = max(ans, max(ans1, ans2)); } printf("%.8lf\n", ans); } return 0; }
9
#include <bits/stdc++.h> using i64 = long long; const int MAXN = 100010; const double PI = 3.14159265358979323846; struct Pos { double x, y; Pos() = default; Pos(double x_, double y_) : x(x_), y(y_) {} }; int n, k; std::vector<Pos> arr; std::vector<double> pd; std::vector<double> pa; bool check(double d) { std::vector<std::pair<double, double>> vec; vec.reserve(n * 2); for (int i = 0; i < n; ++i) { if (pd[i] < d) continue; double a = pa[i]; double da = std::acos(d / pd[i]); double al = a - da, ar = a + da; if (al < 0) vec.emplace_back(al + 2 * PI, 2 * PI), al = 0; if (ar > 2 * PI) vec.emplace_back(0, ar - 2 * PI), ar = 2 * PI; vec.emplace_back(al, ar); } std::sort(vec.begin(), vec.end()); std::priority_queue<double, std::vector<double>, std::greater<double>> pq; for (auto&& p : vec) { double x = p.first; while (!pq.empty() && pq.top() < x) pq.pop(); pq.push(p.second); if (int(pq.size()) >= k) return true; } return false; } int main() { std::scanf("%d%d", &n, &k); arr.reserve(n); pd.reserve(n); for (int i = 0; i < n; ++i) { int x, y; std::scanf("%d%d", &x, &y); if (x == 0 && y == 0) { --k; continue; } double d = std::sqrt(i64(x) * x + i64(y) * y); double f = 1 / (d * d); arr.emplace_back(x * f, y * f); pd.push_back(1 / d); double a = std::atan2(y, x); if (a < 0) a += 2 * PI; pa.push_back(a); } n = arr.size(); if (k <= 0) std::printf("%.10f\n", 0.0); else { double l = 0, r = 2e5; while (r - l > 1e-5) { double mid = 0.5 * (l + r); if (check(0.5 / mid)) r = mid; else l = mid; } double result = 0.5 * (l + r); std::printf("%.10f\n", result); } }
7
#include <bits/stdc++.h> using namespace std; int n, m; long long a[2][1001][2][1001], v; long long f(int x, int y) { long long r = 0; for (int i = y; i >= 1; i ^= i & -i) for (int j = x; j >= 1; j ^= j & -j) r ^= a[y & 1][i][x & 1][j]; return r; } void g(int x, int y) { for (int i = y; i <= n; i += i & -i) for (int j = x; j <= n; j += j & -j) a[y & 1][i][x & 1][j] ^= v; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; while (m--) { int t, x0, y0, x1, y1; cin >> t >> x0 >> y0 >> x1 >> y1; if (t == 1) { cout << (f(x1, y1) ^ f(x0 - 1, y0 - 1) ^ f(x1, y0 - 1) ^ f(x0 - 1, y1)) << endl; } else { cin >> v; g(x0, y0), g(x1 + 1, y0), g(x0, y1 + 1), g(x1 + 1, y1 + 1); } } }
8
#include <bits/stdc++.h> using namespace std; int t, n, x, a[1010]; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> t; while (t--) { cin >> n >> x; if (n < 3) cout << "1\n"; else cout << ((n - 3) / x) + 2 << '\n'; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; long long A[N], n, p, k, ans; vector<long long> resp; long long fexp(long long x, long long y) { if (y == 0) return 1; else if (y % 2 == 0) return fexp((x * x) % p, y / 2) % p; else return (x * fexp((x * x) % p, y / 2)) % p; } int main() { cin >> n >> p >> k; for (int i = 1; i <= n; i++) { cin >> A[i]; A[i] = (fexp(A[i], 4) - (k * A[i]) % p + p) % p; } sort(A + 1, A + n + 1); int cont = 1; for (int i = 1; i <= n; i++) { if (A[i] == A[i + 1] && i < n) cont++; else resp.push_back(cont), cont = 1; } for (int i = 0; i < resp.size(); i++) if (resp[i] != 1) ans += (resp[i] * (resp[i] - 1)) / 2; cout << ans << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 1000 + 10; int n, m, a; int w[N], b[N], fa[N]; int f[N]; vector<int> adj[N]; int find(int x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } int main() { cin >> n >> m >> a; for (int i = 1; i <= n; i++) cin >> w[i], fa[i] = i; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; fa[find(u)] = find(v); } for (int i = 1; i <= n; i++) adj[find(i)].push_back(i); for (int i = 1; i <= n; i++) { if (i == find(i)) { for (int j = a; j >= 0; j--) { int W, B; W = B = 0; for (int k = 0; k < adj[i].size(); k++) { W += w[adj[i][k]]; B += b[adj[i][k]]; if (j >= w[adj[i][k]]) f[j] = max(f[j], f[j - w[adj[i][k]]] + b[adj[i][k]]); } if (j >= W) f[j] = max(f[j], f[j - W] + B); } } } cout << f[a] << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int maxI = 0, index = 0, k = 0; cin >> n >> m; string s, t; cin >> s; cin >> t; vector<int> count(m - n + 1); for (int i = 0; i < m - n + 1; ++i) { count[i] = 0; for (int j = 0; j < n; ++j) { if (s[j] != t[i + j]) { count[i]++; } } } maxI = count[0]; index = 0; for (int i = 0; i < count.size(); ++i) { if (maxI > count[i]) { maxI = count[i]; index = i; } } cout << maxI << endl; for (int i = 0; i < n; ++i) { if (s[i] != t[i + index]) { if (k == 1) { cout << ' '; } cout << i + 1; k = 1; } } }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, ans = 1000000007; cin >> n; int a[2][2 * n][2 * n], b[4][n][n], c[2 * n][2 * n]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2 * n; j++) { for (int k = 0; k < 2 * n; k++) { a[i][j][k] = (i + j + k) % 2; } } } string s; vector<int> v; for (int i = 0; i < 4; i++) { v.push_back(i); for (int k = 0; k < n; k++) { cin >> s; for (int j = 0; j < n; j++) { b[i][k][j] = s[j] - '0'; } } } do { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c[i][j] = b[v[0]][i][j]; c[i][j + n] = b[v[1]][i][j]; c[i + n][j] = b[v[2]][i][j]; c[i + n][j + n] = b[v[3]][i][j]; } } int c1 = 0, c2 = 0; for (int i = 0; i < 2 * n; i++) { for (int j = 0; j < 2 * n; j++) { if (c[i][j] != a[0][i][j]) c1++; if (c[i][j] != a[1][i][j]) c2++; } } ans = min(ans, min(c1, c2)); } while (next_permutation(v.begin(), v.end())); cout << ans; }
3
#include <bits/stdc++.h> using namespace std; const int N = 200005; vector<int> adj[N], rev[N]; int n, m, k, s, t; int p[N], dist[N]; bool vis[N]; void bfs(int u) { queue<pair<int, int> > q; q.emplace(u, 0); vis[u] = 1; while (!q.empty()) { u = q.front().first; int d = q.front().second; q.pop(); dist[u] = d; for (int v : rev[u]) { if (!vis[v]) { q.emplace(v, d + 1); vis[v] = 1; } } } } int main(void) { scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); adj[u].push_back(v); rev[v].push_back(u); } scanf("%d", &k); for (int i = 0; i < k; i++) { scanf("%d", &p[i]); } s = p[0], t = p[k - 1]; bfs(t); int mn = 0, mx = 0; for (int i = 0; i < k - 1; i++) { u = p[i]; v = p[i + 1]; int cnt = 0; for (int w : adj[u]) { if (dist[u] == dist[w] + 1) cnt++; } if (dist[u] == dist[v] + 1) { if (cnt > 1) { mx++; } } else { mn++; mx++; } } printf("%d %d\n", mn, mx); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007LL; long long k; int n; int dx[] = {2, 2, -2, -2, 1, 1, -1, -1}; int dy[] = {1, -1, 1, -1, 2, -2, 2, -2}; long long arithm(long long a, long long b, long long n) { long long s1 = a + b; long long s2 = n; if (s1 % 2 == 0) s1 /= 2; else s2 /= 2; s1 %= mod; s2 %= mod; return (s1 * s2) % mod; } int main() { cin >> k >> n; set<pair<int, int> > bad; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; bad.insert(make_pair(x, y)); } queue<pair<int, int> > q; map<pair<int, int>, int> d; q.push(make_pair(0, 0)); d[make_pair(0, 0)] = 0; int pdiff = -1; int prev = -1000000; long long total = 0; for (long long i = 0; i <= k; ++i) { long long cur = 0; while (!q.empty()) { pair<int, int> t = q.front(); if (d[t] != i) { break; } ++cur; ++total; q.pop(); for (int j = 0; j < 8; ++j) { int nx = t.first + dx[j], ny = t.second + dy[j]; if (!d.count(make_pair(nx, ny)) && !bad.count(make_pair(nx, ny))) { d[make_pair(nx, ny)] = d[t] + 1; q.push(make_pair(nx, ny)); } } } if (i > 100 && cur - prev == pdiff) { long long buf = (k - i) % mod; long long res = total + arithm(cur + pdiff, cur + buf * pdiff, k - i); cout << res % mod << endl; return 0; } pdiff = cur - prev; prev = cur; } cout << total << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int n, cnt = 0; cin >> n; int a[n], b; vector<int> v; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { scanf("%d", &b); v.push_back(b); } int idx = 0; for (int i = 0; i < n; i++) { auto it = find(v.begin() + idx, v.end(), a[i]); if (it == v.end()) break; idx = it - v.begin() + 1; cnt++; } printf("%d", n - cnt); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MOD(1000000007); const int INF((1 << 30) - 1); const int MAXN(105); int a[MAXN], add[MAXN]; int main() { int n; char c; int numadd = 1, k = 1; add[0] = 1; while (scanf(" %c", &c) == 1) { if (c == '=') { scanf("%d", &n); break; } else if (c != '?') { add[k++] = (c == '+'); numadd += (c == '+'); } } int sum = 0; for (int i = 0; i < k; i++) { if (add[i] == 1) a[i] = n, sum += n; else a[i] = 1, sum--; } if (sum < n) { printf("Impossible"); return 0; } int d; for (int i = 0; i < k; i++) { d = min(sum - n, n - 1); if (add[i] == 1) a[i] -= d; else a[i] += d; sum -= d; } if (sum != n) { printf("Impossible"); return 0; } printf("Possible\n"); for (int i = 0; i < k; i++) { if (i != 0) printf("%c ", add[i] == 1 ? '+' : '-'); printf("%d ", a[i]); } printf("= %d", n); }
5
#include <bits/stdc++.h> using namespace std; struct node { int u, v, len; } edge[100010]; unsigned long long dp[100010]; unsigned long long n; unsigned long long sum[100010]; vector<pair<int, int> > G[100010]; void dfs(int root, int fa) { sum[root] = 1; for (int i = 0; i < G[root].size(); i++) { int son = G[root][i].first; if (son == fa) continue; dfs(son, root); sum[root] += sum[son]; dp[G[root][i].second] = sum[son] * (n - sum[son]) * (n - sum[son] - 1LL) + sum[son] * (sum[son] - 1LL) * (n - sum[son]); } } int main() { ios::sync_with_stdio(false); cin >> n; int u, v, len; for (int i = 1; i < n; i++) { cin >> edge[i].u >> edge[i].v >> edge[i].len; u = edge[i].u; v = edge[i].v; G[u].push_back(make_pair(v, i)); G[v].push_back(make_pair(u, i)); } dfs(1, -1); unsigned long long t = 1.0 * (n) * (n - 1) * (n - 2) / 6; double tmp = 0; for (int i = 1; i < n; i++) { tmp += (double)dp[i] * edge[i].len / t; } int q; cin >> q; while (q--) { cin >> u >> len; tmp -= (double)dp[u] * edge[u].len / t; edge[u].len = len; tmp += (double)dp[u] * edge[u].len / t; cout << fixed << setprecision(12) << (double)tmp << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long L, A[200005], B[200005]; int N, D, Q, Ans, S[200005], F[200005], fa[200005], st[200005], bel[200005]; struct Lin { int v, c, next; } E[200005 * 2]; void Link(int u, int v, int c) { E[++D] = (Lin){v, c, st[u]}; st[u] = D; E[++D] = (Lin){u, c, st[v]}; st[v] = D; } void DFS(int u, int f) { for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) ^ f) { DFS(v, u); long long t = A[v] + E[i].c; if (t > A[u]) B[u] = A[u], A[u] = t; else if (t > B[u]) B[u] = t; } } void DFS(int u, int f, long long x) { A[u] = ((A[u]) > (x) ? (A[u]) : (x)); for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) ^ f) { long long t = A[v] + E[i].c; if (t == A[u]) DFS(v, u, ((x) > (B[u]) ? (x) : (B[u])) + E[i].c); else DFS(v, u, ((x) > (A[u]) ? (x) : (A[u])) + E[i].c); } } void Pre(int u, int f) { bel[u] = f; for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) ^ f) Pre(v, u); } int Get(int u) { return u == fa[u] ? u : fa[u] = Get(fa[u]); } bool Cmp(int a, int b) { return A[a] > A[b]; } int main() { scanf("%d", &N); for (int i = 1, u, v, c; i < N; i++) scanf("%d%d%d", &u, &v, &c), Link(u, v, c); DFS(1, 0); DFS(1, 0, 0); for (int i = 1; i <= N; i++) S[i] = i; sort(S + 1, S + N + 1, Cmp); Pre(S[N], 0); for (scanf("%d", &Q); Q--; Ans = 0) { scanf("%I64d", &L); for (int i = 1; i <= N; i++) F[i] = 1, fa[i] = i; for (int i = 1, ed = 1; i <= N; i++) { int u = S[i]; for (int j = st[u], v; j; j = E[j].next) if ((v = E[j].v) ^ bel[u]) F[u] += F[v], fa[v] = u; for (; ed <= N && A[S[ed]] - A[u] > L; ed++) F[Get(S[ed])]--; Ans = ((Ans) > (F[u]) ? (Ans) : (F[u])); } printf("%d\n", Ans); } }
10
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int maxn = 1e5 + 10; string s; int main() { std::ios::sync_with_stdio(0); ; cin.tie(0); cout.tie(0); cin >> s; if (s.size() == 1) { return cout << 1 << "\n", 0; } bool af = 0; for (int i = 0; i < s.size() - 1; i++) { if (s[i] == 'b' && s[i + 1] == 'a') { cout << 1 << " "; } else if (s[i] == 'a' && s[i + 1] == 'b') { cout << 1 << " "; } else { cout << 0 << " "; } } if (s[s.size() - 1] == 'b') { cout << 0; } else { cout << 1; } }
3
#include <bits/stdc++.h> using namespace std; const int c = 110; int main() { int n; scanf("%d", &n); int arr[c] = {0}; int matr[c][c]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int t; scanf("%d", &matr[i][j]); if (i != j) arr[i] |= matr[i][j]; } for (int i = 0; i < n; i++) printf("%d ", arr[i]); }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 2, maxm = 1e6 + 5; vector<int> seg[4 * maxm]; void add(int id, int L, int R, int idx, int val) { if (idx < L || idx >= R) return; if (R == L + 1) { seg[id].push_back(val); return; } int mid = (L + R) / 2; add(2 * id + 0, L, mid, idx, val); add(2 * id + 1, mid, R, idx, val); } void build(int id, int L, int R) { if (R == L + 1) { sort(seg[id].begin(), seg[id].end()); return; } int mid = (L + R) / 2; build(2 * id + 0, L, mid); build(2 * id + 1, mid, R); seg[id].resize(seg[2 * id].size() + seg[2 * id + 1].size()); int ptr1 = 0, ptr2 = 0; for (int i = 0; i < seg[id].size(); i++) { if (ptr2 == seg[2 * id + 1].size() || (ptr1 != seg[2 * id].size() && seg[2 * id][ptr1] < seg[2 * id + 1][ptr2])) { seg[id][i] = seg[2 * id][ptr1]; ptr1++; } else { seg[id][i] = seg[2 * id + 1][ptr2]; ptr2++; } } } int get(int id, int L, int R, int l, int r, int x) { if (R <= l || r <= L) return 0; if (L >= l && R <= r) return seg[id].end() - lower_bound(seg[id].begin(), seg[id].end(), x); int mid = (L + R) / 2; return get(2 * id, L, mid, l, r, x) + get(2 * id + 1, mid, R, l, r, x); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; l--; r--; add(1, 0, maxm, l, r); } build(1, 0, maxm); for (int i = 0; i < m; i++) { int ans = 0, cnt; cin >> cnt; int prv = -1; for (int i = 0; i < cnt; i++) { int tmp; cin >> tmp; tmp--; ans += get(1, 0, maxm, prv + 1, tmp + 1, tmp); prv = tmp; } cout << ans << '\n'; } }
7
#include <bits/stdc++.h> using namespace std; int inline read() { int num = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num * neg; } const int maxn = 100010, mod = 998244353; int n, deg[maxn], T; vector<int> g[maxn]; vector<pair<int, int> > ans; void dfs(int x, int fa, int t) { int son = deg[x] - (x != 1), now = t; for (int i = 0; i < g[x].size(); i++) { int v = g[x][i]; if (v == fa) continue; if (now == T) { ans.push_back(make_pair(x, t - son - 1)); ans.push_back(make_pair(v, t - son)); dfs(v, x, t - son); ans.push_back(make_pair(x, t - son)); now = t - son; } else { ans.push_back(make_pair(v, now + 1)); dfs(v, x, now + 1); ans.push_back(make_pair(x, now + 1)); now++; } son--; } if (x != 1 && (now != t - 1)) ans.push_back(make_pair(x, t - 1)); } int main() { n = read(); for (int i = 1; i < n; i++) { int x = read(), y = read(); g[x].push_back(y); g[y].push_back(x); deg[x]++; deg[y]++; T = max(T, max(deg[x], deg[y])); } ans.push_back(make_pair(1, 0)); dfs(1, 0, 0); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i].first << " " << ans[i].second << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int m, n, z, s; cin >> n; cin >> m; cin >> z; s = 0; for (int i = 1; i <= z; i++) if (i % n == 0) if (i % m == 0) s++; cout << s << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, w, h; pair<int, int> wyn[N]; vector<pair<pair<int, int>, pair<int, int>>> v[2 * N]; int main() { scanf("%d%d%d", &n, &w, &h); for (int i = 1; i <= n; ++i) { int g, p, t; scanf("%d%d%d", &g, &p, &t); v[p - t + N].push_back(make_pair(make_pair(g, i), make_pair(p, t))); } for (int i = 0; i < 2 * N; ++i) { vector<pair<int, int>> yki, xy; for (auto j : v[i]) if (j.first.first == 1) xy.push_back(make_pair(j.second.first, j.first.second)); else yki.push_back(make_pair(j.second.first, j.first.second)); sort(xy.begin(), xy.end()); sort(yki.begin(), yki.end()); if (yki.empty()) { for (auto j : xy) wyn[j.second] = {j.first, h}; continue; } if (xy.empty()) { for (auto j : yki) wyn[j.second] = {w, j.first}; continue; } vector<pair<int, int>> elo; for (int j = (int)yki.size() - 1; j >= 0; --j) elo.push_back(yki[j]); for (int j = 0; j < (int)xy.size(); ++j) elo.push_back(xy[j]); vector<pair<int, int>> leo; for (int j = 0; j < (int)xy.size(); ++j) leo.push_back(xy[j]); for (int j = (int)yki.size() - 1; j >= 0; --j) leo.push_back(yki[j]); int k = elo.size(); for (int j = 0; j < (int)elo.size(); ++j) { if (k - j - 1 < (int)yki.size()) { wyn[elo[j].second] = {w, leo[j].first}; } else { wyn[elo[j].second] = {leo[j].first, h}; } } } for (int i = 1; i <= n; ++i) printf("%d %d\n", wyn[i].first, wyn[i].second); return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> arr; vector<int> seg; long long ans = 0; long long expo(long long a, long long b) { if (b == 0) { return 1; } long long z = expo(a, b / 2); z *= z; z %= 1000000007; if (b % 2 != 0) { z *= a; z %= 1000000007; } return z; } void Merge(int l, int mid, int r) { int len1 = mid - l + 1; int len2 = r - mid; int A[len1 + 1]; int B[len2 + 1]; for (int i = 0; i < len1; i++) { A[i] = arr[l + i]; } A[len1] = 100000000; for (int i = 0; i < len2; i++) { B[i] = arr[mid + 1 + i]; } B[len2] = 100000000; int a = 0; int b = 0; for (int i = 0; i < (len1 + len2); i++) { if (A[a] > B[b]) { long long ans1 = len1 - a; ans += ans1; arr[l + i] = B[b]; b++; } else { arr[l + i] = A[a]; a++; } } } void Partition(int l, int r) { if (l >= r) { return; } int mid = l + (r - l) / 2; Partition(l, mid); Partition(mid + 1, r); Merge(l, mid, r); } long long query(int node, int s, int e, int l, int r) { if (s > r || e < l) { return 0; } if (s >= l && e <= r) { return seg[node]; } int mid = (s + e) / 2; long long ans1 = query(2 * node + 1, s, mid, l, r) + query(2 * node + 2, mid + 1, e, l, r); return ans1; } void update(int node, int l, int r, int val) { if (l > r) { return; } if (l == r) { if (l == val) { seg[node] = 1; } return; } int mid = (l + r) / 2; if (val <= mid) { update(2 * node + 1, l, mid, val); } else { update(2 * node + 2, mid + 1, r, val); } seg[node] = seg[2 * node + 1] + seg[2 * node + 2]; } void solve() { int n; cin >> n; string str; cin >> str; set<int> v[30]; arr.clear(); arr.resize(n + 2, 0); for (int i = 0; i < n; i++) { int x = str[i] - 'a'; v[x].insert(i); } seg.clear(); seg.resize(4 * n + 5, 0); int i = 0; int j = n - 1; int curr = i; while (i < n) { int x = str[j] - 'a'; int z; for (auto &hk : v[x]) { if (arr[hk] == 0) { z = hk; v[x].erase(hk); break; } } arr[z] = i + 1; i++; j--; } ans = 0; for (int i = 0; i < n; i++) { long long ans1 = query(0, 0, n, arr[i] + 1, n); ans += ans1; update(0, 0, n, arr[i]); } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; t = 1; while (t--) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int cnt[205]; int n, m; int has[205], other[205], hsize, osize, sum; int main() { while (cin >> n >> m) { for (int i = 0; i < n; i++) { cin >> cnt[i]; } int ans = -1005; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { hsize = osize = sum = 0; for (int k = 0; k < n; k++) { if (k >= i && k <= j) { has[hsize++] = cnt[k]; sum += cnt[k]; } else { other[osize++] = cnt[k]; } } sort(has, has + hsize); sort(other, other + osize); int fit = 0; for (int k = 0; k < min(m, hsize); k++) { if (osize - k - 1 >= 0 && other[osize - k - 1] - has[k] >= 0) { fit += other[osize - k - 1] - has[k]; } } ans = max(ans, fit + sum); } } cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 987654321; const long long int INF = 123456789987654321; int N, T; vector<char> V; int last_home, home_cnt = 0, shop_cnt = 0; vector<int> shop_idx; int f(int x) { int last_shop = home_cnt - x - 1 < 0 ? -1 : shop_idx[home_cnt - x - 1]; int last_point = max(last_shop, last_home); int mini = 2 * inf; int sweet = x; int cur_time = 0; int s = -1; for (int i = 0; i < N; i++) { cur_time++; if (V[i] == 'H') { if (sweet == 0) s = i; sweet--; } if (V[i] == 'S') { sweet++; if (sweet == 0) { mini = min(mini, cur_time + (last_point - i) + (last_point - s)); cur_time += 2 * (i - s); s = -1; } } if (i == last_home && s == -1) { mini = min(mini, cur_time); } } return mini; } int main() { scanf("%d %d\n", &N, &T); V.resize(N); for (int i = 0; i < N; i++) { scanf("%c", &V[i]); } for (int i = 0; i < N; i++) { if (V[i] == 'H') { last_home = i; home_cnt++; } if (V[i] == 'S') { shop_idx.push_back(i); shop_cnt++; } } if (T < last_home + 1) { printf("-1"); return 0; } int s = max(home_cnt - shop_cnt, 0), e = home_cnt; int ans = -1; while (s <= e) { int m = (s + e) >> 1; if (f(m) <= T) { ans = m; e = m - 1; } else s = m + 1; } printf("%d", ans); }
7
#include <bits/stdc++.h> using namespace std; int n, m, b[100005], c[100005]; vector<int> a[100005], v2, ar[100005]; vector<pair<int, int> > v[100005]; set<pair<int, int> > st; set<pair<int, int> >::iterator it; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { ar[i].resize(m); for (int j = 0; j < m; j++) { int x; scanf("%d", &x); a[i].push_back(x); if (a[i][j] != -1) v[i].push_back(make_pair(a[i][j], j)); } sort(v[i].begin(), v[i].end()); reverse(v[i].begin(), v[i].end()); } for (int j = 0; j < m; j++) { int sum = 0; for (int i = 0; i < n; i++) { if (a[i][j] != -1) sum++; } b[j] = sum; st.insert(make_pair(sum, j)); } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { if (v[i].empty()) continue; int y = v[i].back().second; if (ar[i][y] == 1) continue; ar[i][y] = 1; if (c[y] == 0) { st.erase(make_pair(b[y], y)); b[y]--; st.insert(make_pair(b[y], y)); } for (int k = v[i].size() - 2; k >= 0; k--) { if (v[i][k].first != v[i][k + 1].first) break; ar[i][v[i][k].second] = 1; y = v[i][k].second; if (c[y] == 0) { st.erase(make_pair(b[y], y)); b[y]--; st.insert(make_pair(b[y], y)); } } } it = st.begin(); pair<int, int> p; p = *it; if (p.first != 0) { printf("-1"); return 0; } v2.push_back(p.second); c[p.second] = 1; st.erase(it); for (int i = 0; i < n; i++) { if (a[i][p.second] != -1) { v[i].pop_back(); } } } for (int i = 0; i < v2.size(); i++) { printf("%d ", v2[i] + 1); } }
7
#include <bits/stdc++.h> using namespace std; void boost() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long cubic_root(long long x) { long long l = 0, r = 2000005; while (l != r) { long long m = (l + r + 1) / 2; if (m * m * m > x) r = m - 1; else l = m; } return l; } int main(void) { boost(); long long t; cin >> t; while (t--) { long long a, b, flag = 0; cin >> a >> b; long long x = a * b; long long n = cubic_root(x); long long t1 = n * n * n; if (t1 != x) { flag = 1; } if (flag) cout << "NO" << "\n"; else { if (a % n == 0 && b % n == 0) { cout << "YES" << "\n"; } else cout << "NO" << "\n"; } } }
4
#include <bits/stdc++.h> using namespace std; vector<pair<long long int, long long int> > ans; deque<long long int> dq; int main() { long long int n, q; scanf("%lld", &n); scanf("%lld", &q); long long int ar[n], mx = -1; for (long long int i = 0; i < n; i++) scanf("%lld", &ar[i]), mx = max(mx, ar[i]); for (long long int i = 0; i < n; i++) dq.push_back(ar[i]); vector<pair<long long int, long long int> > foo, bar; while (1) { long long int a = dq.front(); dq.pop_front(); long long int b = dq.front(); dq.pop_front(); if (a == mx) { dq.push_front(b); dq.push_front(a); break; } foo.push_back(make_pair(a, b)); if (a > b) { dq.push_front(a); dq.push_back(b); } else { dq.push_front(b); dq.push_back(a); } } for (long long int i = 1; i < dq.size(); i++) { long long int x = dq.front(); bar.push_back(make_pair(x, dq[i])); } long long int sz1 = foo.size(), sz2 = bar.size(); while (q--) { long long int idx; scanf("%lld", &idx); idx--; if (idx < sz1) { printf("%lld %lld\n", foo[idx].first, foo[idx].second); } else { idx -= sz1; idx %= sz2; printf("%lld %lld\n", bar[idx].first, bar[idx].second); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; long long a, b, c; while (t--) { cin >> a >> b >> c; printf("%lld\n", a + b + c - 1); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100010; const long long inf = 1ll << 60; int n, tot; int to[N << 1], nex[N << 1], head[N]; long long ans = 0, p[N], sum[N]; bool flag; void SE(int u, int v) { to[++tot] = v; nex[tot] = head[u]; head[u] = tot; return; } long long _GCD(long long x, long long y) { long long r = x % y; while (r) x = y, y = r, r = x % y; return y; } void DFS(int x, int fa) { long long mi = inf, cnt = 0; for (int i = head[x]; i; i = nex[i]) { if (to[i] == fa) continue; DFS(to[i], x); if (flag) return; if (p[to[i]] < mi) mi = p[to[i]]; cnt++; } if (cnt == 0) { sum[x] = 1; return; } long long lcm = 1ll; for (int i = head[x]; i; i = nex[i]) { if (to[i] == fa) continue; if ((double)lcm / _GCD(lcm, sum[to[i]]) * sum[to[i]] > (double)mi) { flag = 1; return; } lcm = lcm / _GCD(lcm, sum[to[i]]) * sum[to[i]]; } p[x] = mi / lcm * lcm * cnt; sum[x] = lcm * cnt; return; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", &p[i]); ans += p[i]; } for (int u, v, i = 1; i < n; i++) { scanf("%d%d", &u, &v); SE(u, v), SE(v, u); } DFS(1, 0); if (!flag) ans -= p[1]; printf("%I64d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; long long findpal(long long n) { long long m = n, m1 = 0, ctr = 0, rem; while (n != 0) { rem = n % 10; m1 = m1 * 10 + rem; ctr++; n /= 10; } m = m * pow(10, ctr) + m1; return m; } int main() { long long k, p, i, rev, sum = 0; cin >> k >> p; i = 0; while (k--) { i++; rev = findpal(i); sum += rev; sum %= p; } cout << sum; return 0; }
2
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } const long long N = 1e5 + 3; void topo(long long id, vector<long long> &deg, vector<long long> *graph, long long *dp) { for (long long child : graph[id]) { --deg[child]; dp[child] = max(dp[child], 1 + dp[id]); if (deg[child] == 0) topo(child, deg, graph, dp); } } bool check(long long mx, vector<pair<long long, long long>> &edges, long long n) { vector<long long> v[n + 1], deg(n + 1, 0); for (long long i = 0; i < mx; ++i) v[edges[i].first].push_back(edges[i].second), deg[edges[i].second]++; long long dp[n + 1]; long long id = -1; long long cnt = 0; for (long long i = 1; i <= n; ++i) { cnt += (deg[i] == 0); if (deg[i] == 0) id = i; } if (cnt != 1) return false; memset(dp, 0, sizeof dp); dp[id] = 1; topo(id, deg, v, dp); for (long long i = 1; i <= n; ++i) { if (dp[i] == n) return true; } return false; } void solve() { vector<pair<long long, long long>> edges; long long n, m; cin >> n >> m; for (long long i = 0; i < m; ++i) { long long x, y; cin >> x >> y; edges.push_back({x, y}); } long long l = 1, r = m; long long ans = -1; while (l <= r) { long long mid = (l + r) / 2; if (check(mid, edges, n)) ans = mid, r = mid - 1; else l = mid + 1; } cout << ans << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); ; long long t = 1; while (t--) solve(); }
5
#include <bits/stdc++.h> using namespace std; const double ef = 0.000000001; double S, ans; struct node { double x, y; } p[305][1005], pp[305][1005], P[305][1005]; int q[1005], qq[1005], Last, A, n, k, i; bool v[1005], V[1005]; int cmp(node i, node j) { return i.x < j.x; } double work(double l, double r, double x) { return l + x * (r - l); } double Find(double L, double R, double l, double r) { double mid = (l + r) / 2.0; while (l <= r) { if (work(Last, A, mid) > work(L, R, mid)) { l = mid + 0.000000001; mid = (l + r) / 2.0; } else { r = mid - 0.000000001; mid = (l + r) / 2.0; } } return mid; } double Find1(double L, double R, double l, double r) { double mid = (l + r) / 2.0; while (l <= r) { if (work(Last, A, mid) < work(L, R, mid)) { l = mid + 0.000000001; mid = (l + r) / 2.0; } else { r = mid - 0.000000001; mid = (l + r) / 2.0; } } return mid; } void add(int t, int l, int r) { int i; for (i = 1; i <= q[t]; i++) v[i] = V[i] = true; qq[t] = 0; for (i = 2; i <= q[t]; i++) { if (p[t][i - 1].y - ef < work(l, r, p[t][i - 1].x) && p[t][i].y - ef < work(l, r, p[t][i].x)) v[i] = v[i - 1] = false; else if (p[t][i - 1].y - ef < work(l, r, p[t][i - 1].x) && p[t][i].y - ef > work(l, r, p[t][i].x)) { v[i - 1] = false; V[i - 1] = false; pp[t][++qq[t]].x = Find(p[t][i - 1].y - (p[t][i].y - p[t][i - 1].y) / (p[t][i].x - p[t][i - 1].x) * p[t][i - 1].x, p[t][i].y + (p[t][i].y - p[t][i - 1].y) / (p[t][i].x - p[t][i - 1].x) * (1 - p[t][i].x), p[t][i - 1].x, p[t][i].x); pp[t][qq[t]].y = work(l, r, pp[t][qq[t]].x); } else if (p[t][i - 1].y - ef > work(l, r, p[t][i - 1].x) && p[t][i].y - ef < work(l, r, p[t][i].x)) { v[i] = false; V[i] = false; pp[t][++qq[t]].x = Find1(p[t][i - 1].y - (p[t][i].y - p[t][i - 1].y) / (p[t][i].x - p[t][i - 1].x) * p[t][i - 1].x, p[t][i].y + (p[t][i].y - p[t][i - 1].y) / (p[t][i].x - p[t][i - 1].x) * (1 - p[t][i].x), p[t][i - 1].x, p[t][i].x); pp[t][qq[t]].y = work(l, r, pp[t][qq[t]].x); } } double Last = 0; for (i = 1; i <= q[t]; i++) if (v[i]) pp[t][++qq[t]] = p[t][i]; sort(pp[t] + 1, pp[t] + qq[t] + 1, cmp); q[t] = 0; if (pp[t][1].x > ef || !qq[t]) { p[t][++q[t]].y = l; p[t][q[t]].x = 0; } for (i = 1; i <= qq[t]; i++) p[t][++q[t]] = pp[t][i]; if (pp[t][qq[t]].x - 1 < -ef || !qq[t]) { p[t][++q[t]].x = 1; p[t][q[t]].y = r; } } double WORK(int t) { double ans = 0; int i; for (i = 2; i <= q[t]; i++) ans += (p[t][i].y + p[t][i - 1].y) * (p[t][i].x - p[t][i - 1].x) / 2.0; return ans; } int main() { scanf("%d%d", &n, &k); for (i = 1; i <= k; i++) { q[i] = 2; p[i][2].x = 1; } while (n--) { scanf("%d", &Last); S = 0; for (i = 1; i <= k; i++) { scanf("%d", &A); add(i, Last, A); Last = A; } for (i = 1; i <= k; i++) S += WORK(i); printf("%.6f\n", S - ans); ans = S; } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int cnt[maxn]; int dp[maxn][3][3]; int main() { int n, m; scanf("%d%d", &n, &m); while (n--) { int x; scanf("%d", &x); cnt[x]++; } memset(dp, -1, sizeof(dp)); dp[0][0][0] = 0; for (int i = 1; i <= m; i++) { for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { for (int c = 0; c < 3; c++) { int tmp = cnt[i] - a - b - c; if (tmp < 0 || dp[i - 1][b][c] < 0) continue; dp[i][a][b] = max(dp[i][a][b], dp[i - 1][b][c] + c + tmp / 3); } } } } int ans = 0; for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { ans = max(ans, dp[m][a][b]); } } printf("%d\n", ans); }
7
#include <bits/stdc++.h> using namespace std; double dist(int x1, int y1, int x2, int y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } int main() { int x, y, n, k; long double sum = 0; bool first = true; int lastX, lastY; cin >> n >> k; while (n--) { cin >> x >> y; if (first) { lastX = x; lastY = y; first = false; continue; } sum += dist(lastX, lastY, x, y); lastX = x; lastY = y; } cout << fixed << setprecision(9) << sum / 50 * k << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = (int)1e9 + 1; 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; } void FileIO() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int main() { int n; cin >> n; string s; cin >> s; string res = ""; for (int i = 0; i < n; i++) { int r = s[i]; if (r == '2' || r == '3' || r == '5' || r == '7') res += string(1, r); else if (r == '4') { res += "223"; } else if (r == '6') { res += "35"; } else if (r == '8') { res += "2227"; } else if (r == '9') res += "3327"; } sort(res.begin(), res.end(), greater<char>()); cout << res; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; struct node { int to, val; }; int n, u, v, w; vector<node> vec[maxn]; int dp[maxn][2]; template <typename T> inline void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x = x * f; } long long solve(int cur, int fa, int v) { long long res = dp[cur][0] + dp[cur][1], sum0 = 0, sum1 = 0; for (int i = 0; i < vec[cur].size(); i++) { auto sun = vec[cur][i]; if (sun.to != fa) { if (sun.val) sum1 += dp[sun.to][0] + 1; else { sum0 += dp[sun.to][0] + 1; for (int j = 0; j < vec[sun.to].size(); j++) { auto son = vec[sun.to][j]; if (son.to != cur && son.val) sum0 -= dp[son.to][0] + 1; } } } } for (int i = 0; i < vec[cur].size(); i++) { auto sun = vec[cur][i]; if (sun.to != fa) { if (sun.val) dp[sun.to][1] = sum0 + sum1 - (dp[sun.to][0] + 1) + dp[cur][1] + 1; else { if (!v) dp[sun.to][1] += dp[cur][1]; long long tot = dp[sun.to][0] + 1; for (int j = 0; j < vec[sun.to].size(); j++) { auto son = vec[sun.to][j]; if (son.to != cur && son.val) tot -= dp[son.to][0] + 1; } dp[sun.to][1] += sum0 - tot + 1; } res += solve(sun.to, cur, sun.val); } } return res; } void dfs(int cur, int fa) { for (int i = 0; i < vec[cur].size(); i++) { if (vec[cur][i].to != fa) { dfs(vec[cur][i].to, cur); } } for (int i = 0; i < vec[cur].size(); i++) { auto sun = vec[cur][i]; if (sun.to != fa) { if (sun.val) dp[cur][0] += dp[sun.to][0] + 1; else { dp[cur][0] += dp[sun.to][0] + 1; for (int j = 0; j < vec[sun.to].size(); j++) { auto grand = vec[sun.to][j]; if (grand.to != cur && grand.val) dp[cur][0] -= dp[grand.to][0] + 1; } } } } } int main() { read(n); for (int i = 1; i < n; i++) { read(u); read(v); read(w); vec[u].push_back(node{v, w}); vec[v].push_back(node{u, w}); } dfs(1, 0); printf("%lld\n", solve(1, 0, 0)); }
7
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, INF = N; int n, L, R, ANS_U, ANS_V, ANS; struct st { int nxt, to, w, dis; } ed[N << 1]; int head[N], num; void add(int from, int to, int w) { ed[++num].to = to, ed[num].w = w; ed[num].nxt = head[from]; head[from] = num; } int sum, rt, siz[N], mx[N]; bool vis[N]; void getrt(int x, int fa) { mx[x] = 0; for (int u, i = head[x]; i; i = ed[i].nxt) { u = ed[i].to; if (u == fa || vis[u]) continue; getrt(u, x), mx[x] = max(mx[x], siz[u]); } mx[x] = max(mx[x], sum - siz[x]); if (mx[x] < mx[rt]) rt = x; } int mxd[N], V[N]; vector<int> E[N]; void dfs_init(int x, int fa) { mxd[x] = 0, siz[x] = 1; for (int u, i = head[x]; i; i = ed[i].nxt) { u = ed[i].to; if (vis[u] || u == fa) continue; dfs_init(u, x), mxd[x] = max(mxd[x], mxd[u] + 1), siz[x] += siz[u]; } } bool cmp(int a, int b) { return mxd[a] < mxd[b]; } void work(int x) { dfs_init(x, 0); int h = 0; for (int i = head[x]; i; i = ed[i].nxt) if (!vis[ed[i].to]) V[++h] = ed[i].to; sort(V + 1, V + 1 + h, cmp); for (int i = 1; i <= h; i++) E[x].push_back(V[i]); } void solve_init(int x) { work(x), vis[x] = 1; for (int u, i = head[x]; i; i = ed[i].nxt) { u = ed[i].to; if (vis[u]) continue; rt = 0, sum = siz[u], getrt(u, x), solve_init(rt); } } int f[N], a[N], s[N], id_a[N], id_s[N], ans_u, ans_v, ans; void dfs(int x, int fa, int dep) { if (f[x] > a[dep]) a[dep] = f[x], id_a[dep] = x; for (int u, i = head[x]; i; i = ed[i].nxt) { u = ed[i].to; if (vis[u] || u == fa) continue; f[u] = f[x] + ed[i].dis, dfs(u, x, dep + 1); } } int q[N], hd, tail; void calc(int x) { dfs_init(x, 0); int len = 0; for (int i = head[x]; i; i = ed[i].nxt) if (!vis[ed[i].to]) f[ed[i].to] = ed[i].dis, ++mxd[ed[i].to]; for (int i = 0; i <= mxd[x]; i++) s[i] = -N; s[0] = 0, id_s[0] = x; for (auto u : E[x]) { if (vis[u]) continue; for (int i = 0; i <= mxd[u]; i++) a[i] = -N; a[0] = 0, id_a[0] = x; dfs(u, x, 1), hd = 1, tail = 0; if (L <= len) for (int i = min(R, len); i > L; i--) { while (hd <= tail && s[q[tail]] <= s[i]) --tail; q[++tail] = i; } for (int i = 0; i <= mxd[u]; i++) { while (hd <= tail && q[hd] > R - i) ++hd; if (len >= L - i && L - i >= 0) { while (hd <= tail && s[L - i] >= s[q[tail]]) --tail; q[++tail] = L - i; } if (hd <= tail && s[q[hd]] + a[i] > ans) ans = s[q[hd]] + a[i], ans_u = id_a[i], ans_v = id_s[q[hd]]; } for (int i = 0; i <= mxd[u]; i++) if (a[i] > s[i]) s[i] = a[i], id_s[i] = id_a[i]; len = max(len, mxd[u]); } } void solve(int x) { calc(x), vis[x] = 1; for (int u, i = head[x]; i; i = ed[i].nxt) { u = ed[i].to; if (vis[u]) continue; rt = 0, sum = siz[u], getrt(u, x), solve(rt); } } int main() { int u, v, w, l = 0, r = 0; scanf("%d%d%d", &n, &L, &R); for (int i = 1; i < n; i++) scanf("%d%d%d", &u, &v, &w), add(u, v, w), add(v, u, w), r = max(r, w); mx[0] = N, rt = 0, sum = n, getrt(1, 0), solve_init(rt); while (l <= r) { int mid = (l + r) >> 1; for (int i = 1; i <= num; i++) ed[i].dis = (ed[i].w >= mid) ? 1 : -1; for (int i = 1; i <= n; i++) vis[i] = 0; mx[0] = N, ans = -N, rt = 0, sum = n, getrt(1, 0), solve(rt); if (ans >= 0) ANS_U = ans_u, ANS_V = ans_v, ANS = mid, l = mid + 1; else r = mid - 1; } printf("%d %d\n", ANS_U, ANS_V); return 0; }
11
#include <bits/stdc++.h> using namespace std; const int maxn = 500; char mat[maxn][maxn]; int main() { int n; cin >> n; for (int i = 0; i < (n); i++) scanf("%s", mat[i]); char a = mat[0][0], b = mat[0][1]; int ok = 1; if (a == b) { cout << "NO" << endl; return 0; } for (int i = 0; i < (n); i++) for (int j = 0; j < (n); j++) if (i == j || j + i == n - 1) { if (mat[i][j] != a) { ok = 0; break; } } else if (mat[i][j] != b) { ok = 0; break; } if (ok) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 102; struct matrix { int arr[MAXN][MAXN]; int n, m; matrix(int x, int y) { n = x, m = y; }; matrix(int x, int y, int v) { n = x, m = y; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = v; }; matrix operator*(const matrix &mat) const { matrix res(n, mat.m, 0); for (int i = 0; i < n; i++) for (int j = 0; j < mat.m; j++) for (int k = 0; k < m; k++) res.arr[i][j] = (arr[i][k] * 1LL * mat.arr[k][j] + res.arr[i][j]) % 1000000007; return res; } bool operator>(const matrix &mat) const { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (arr[i][j] > mat.arr[i][j]) return true; if (arr[i][j] < mat.arr[i][j]) return false; } return true; } void print(int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) printf("%d ", arr[i][j]); printf("\n"); } } }; matrix mat_pow(const matrix &base, int power) { if (power == 1) return base; if (power & 1) return base * mat_pow(base, power - 1); matrix temp = mat_pow(base, power / 2); return temp * temp; } int cnt[105]; int main() { int n, k, x; scanf("%d %d", &n, &k); if (!k) { printf("1"); return 0; } for (int i = 0; i < n; i++) { scanf("%d", &x); cnt[x]++; } matrix m(101, 101, 0); for (int i = 0; i < 100; i++) m.arr[i][0] = cnt[i + 1]; for (int i = 0; i < 99; i++) m.arr[i][i + 1] = 1; m.arr[100][100] = 1; m.arr[100][0] = 1; m = mat_pow(m, k + 1); printf("%d", m.arr[100][0]); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 555; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; int n, k; bool G[N][N]; int mark[N][N]; int m = 0, size[N * N]; inline bool inside(int x, int y) { return x >= 0 && y >= 0 && x < n && y < n; } void floodfill(int x, int y, int m0) { size[m0]++; mark[x][y] = m0; for (int d = 0; d < 4; d++) { int nx = x + dx[d], ny = y + dy[d]; if (inside(nx, ny) && !G[nx][ny] && !mark[nx][ny]) floodfill(nx, ny, m0); } } char buff[N]; int cc[N * N], blocks, fullsize; inline void add(int x, int y, bool core) { if (!inside(x, y)) return; if (core) blocks += G[x][y]; else { if (G[x][y]) return; if (!cc[mark[x][y]]) fullsize += size[mark[x][y]]; cc[mark[x][y]]++; } } inline void del(int x, int y, bool core) { if (!inside(x, y)) return; if (core) blocks -= G[x][y]; else { if (G[x][y]) return; cc[mark[x][y]]--; if (!cc[mark[x][y]]) fullsize -= size[mark[x][y]]; } } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%s", buff); for (int j = 0; j < n; j++) G[i][j] = buff[j] == 'X'; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (!G[i][j] && !mark[i][j]) floodfill(i, j, ++m); int ans = 0; for (int i = 0; i + k - 1 < n; i++) { int j = 0; blocks = fullsize = 0; memset(cc + 1, 0, sizeof(cc[0]) * m); for (int x = i; x < i + k; x++) for (int y = j; y < j + k; y++) add(x, y, 1); for (int x = i - 1; x <= i + k; x++) for (int y = j - 1; y <= j + k; y++) if (((x == i - 1) + (y == j - 1) + (x == i + k) + (y == j + k) < 2)) add(x, y, 0); for (; j + k - 1 < n; j++) { if (fullsize + blocks > ans) ans = fullsize + blocks; if (j + k != n) { for (int x = i; x < i + k; x++) { del(x, j, 1); add(x, j + k, 1); del(x, j - 1, 0); add(x, j + k + 1, 0); } del(i - 1, j, 0); add(i - 1, j + k, 0); del(i + k, j, 0); add(i + k, j + k, 0); } } } printf("%d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.first); putchar(' '); _W(x.second); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } template <class T, class... U> void DEBUG(const T &head, const U &...tail) {} int MOD = 1e9 + 7; void ADD(int64_t &x, int64_t v) { x = (x + v) % MOD; if (x < 0) x += MOD; } const int SIZE = 1e6 + 10; vector<pair<int, int> > e[SIZE]; int lv[SIZE], lt[SIZE]; int64_t c[SIZE]; bool u[SIZE]; bool in_odd_cycle[SIZE]; vector<int> odd_cycle; int64_t an[SIZE]; void dfs(int x) { u[x] = 1; for (int i = 0; i < (((int)(e[x]).size())); ++i) { int y = e[x][i].first; if (u[y]) { if (lv[y] < lv[x] && y != lt[x] && (lv[x] - lv[y]) % 2 == 0 && !((int)(odd_cycle).size())) { int now = x; while (now != y) { odd_cycle.push_back(now); now = lt[now]; } odd_cycle.push_back(y); } } else { lv[y] = lv[x] + 1; lt[y] = x; dfs(y); } } } void dfs_even(int x) { u[x] = 1; for (int i = 0; i < (((int)(e[x]).size())); ++i) { int y = e[x][i].first; if (u[y] || in_odd_cycle[y]) continue; dfs_even(y); an[e[x][i].second] = c[y]; c[x] -= c[y]; } } int main() { int n, m; R(n, m); for (int i = (1); i <= (n); ++i) { int x; R(x); c[i] = x; } for (int i = 0; i < (m); ++i) { int x, y; R(x, y); e[x].push_back(make_pair(y, i)); e[y].push_back(make_pair(x, i)); } dfs(1); if (!((int)(odd_cycle).size())) { memset((u), 0, sizeof((u))); dfs_even(1); if (!c[1]) { puts("YES"); for (int i = 0; i < (m); ++i) W(an[i]); } else puts("NO"); } else { for (int i = 0; i < (((int)(odd_cycle).size())); ++i) { in_odd_cycle[odd_cycle[i]] = 1; } memset((u), 0, sizeof((u))); for (int i = 0; i < (((int)(odd_cycle).size())); ++i) { dfs_even(odd_cycle[i]); } int64_t v = 0; for (int i = 0; i < (((int)(odd_cycle).size())); ++i) { if (!i || i % 2 == 1) v += c[odd_cycle[i]]; else v -= c[odd_cycle[i]]; } if (v % 2 != 0) { puts("NO"); return 0; } v /= 2; odd_cycle.push_back(odd_cycle[0]); for (int i = (1); i < (((int)(odd_cycle).size())); ++i) { int x = odd_cycle[i - 1]; int y = odd_cycle[i]; int eid = -1; for (int j = 0; j < (((int)(e[x]).size())); ++j) { if (e[x][j].first == y) { eid = e[x][j].second; break; } } an[eid] = v; v = c[y] - v; } puts("YES"); for (int i = 0; i < (m); ++i) W(an[i]); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 2005; int n, a[N], m, ans = 0; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (a[i] > a[j]) ++ans; ans = ans & 1; } } for (int i = 1; i <= m; ++i) { int l, r; scanf("%d%d", &l, &r); int len = r - l + 1; int num = len * (len - 1) / 2; if (num & 1) ans = (ans + 1) & 1; if (ans == 1) puts("odd"); else puts("even"); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; cin >> TESTS; while (TESTS--) { long long n, i, j; cin >> n; long long a[n + 1]; for (i = 0; i < n; i++) cin >> a[i]; long long mx = a[0], l = 1, r = -1, f = 0; for (i = 1; i < n; i++) { mx--; if (a[i] < mx) { r = i + 1; f = 1; break; } if (a[i] > mx) { l = i + 1; mx = a[i]; } } if (!f) { long long mn = a[0]; l = 1, r = -1; for (i = 1; i < n; i++) { mn++; if (a[i] > mn) { r = i + 1; f = 1; break; } if (a[i] < mn) { l = i + 1; mn = a[i]; } } if (f) { cout << "YES\n"; cout << l << " " << r << "\n"; } else cout << "NO" << "\n"; } else if (f) { cout << "YES\n"; cout << l << " " << r << "\n"; } } return 0; }
2
#include <bits/stdc++.h> int main() { int t, movesFirst{0}, movesSecond{0}; std::cin >> t; for (int i = 0; i != t; i++) { int number_of_piles, ones{0}; std::cin >> number_of_piles; int stones_on_piles[number_of_piles]; for (int j = 0; j < number_of_piles; j++) { std::cin >> stones_on_piles[j]; } for (int j = 0; j < number_of_piles; j++) { if (stones_on_piles[j] == 1) { ones++; if (ones == number_of_piles) { if (ones % 2 == 1) { std::cout << "First" << '\n'; break; } else { std::cout << "Second" << '\n'; break; } } } else { if (ones % 2 == 0) { std::cout << "First" << '\n'; break; } else { std::cout << "Second" << '\n'; break; } } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int mod = 1e9; int n, m, f[N], pre[N], a[N]; struct mat { int a[3][3]; mat() { memset(a, 0, sizeof(a)); } mat operator*(const mat& b) const { mat ans; int t; for (int i = (1), iend = (2); i <= iend; i++) for (int k = (1), kend = (2); k <= kend; k++) { t = a[i][k]; for (int j = (1), jend = (2); j <= jend; j++) ans.a[i][j] = (ans.a[i][j] + 1ll * t * b.a[k][j] % mod) % mod; } return ans; } } base, h[N]; struct T { int l, r, len, sum1, sum2, tag; } t[N << 2]; inline int read() { int f = 1, x = 0; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } inline mat binpow(int x) { return h[x]; } inline void upd(int rt) { mat tmp = binpow(t[rt << 1].len); t[rt].sum1 = ((t[rt << 1].sum1 + 1ll * t[rt << 1 | 1].sum1 * tmp.a[1][1] % mod) % mod + 1ll * t[rt << 1 | 1].sum2 * tmp.a[1][2] % mod) % mod; t[rt].sum2 = ((t[rt << 1].sum2 + 1ll * t[rt << 1 | 1].sum1 * tmp.a[2][1] % mod) % mod + 1ll * t[rt << 1 | 1].sum2 * tmp.a[2][2] % mod) % mod; } inline void build(int rt, int l, int r) { t[rt].l = l, t[rt].r = r, t[rt].len = r - l + 1; if (l == r) { t[rt].sum1 = t[rt].sum2 = a[l]; return; } int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); upd(rt); } inline void pushdown(int rt) { if (t[rt].tag != 0) { t[rt << 1].tag += t[rt].tag, t[rt << 1].tag %= mod; t[rt << 1 | 1].tag += t[rt].tag, t[rt << 1 | 1].tag %= mod; t[rt << 1].sum1 = (t[rt << 1].sum1 + 1ll * (pre[t[rt << 1].len] - 1) * t[rt].tag % mod) % mod; t[rt << 1].sum2 = (t[rt << 1].sum2 + 1ll * (pre[t[rt << 1].len - 1]) * t[rt].tag % mod) % mod; t[rt << 1 | 1].sum1 = (t[rt << 1 | 1].sum1 + 1ll * (pre[t[rt << 1 | 1].len] - 1) * t[rt].tag % mod) % mod; t[rt << 1 | 1].sum2 = (t[rt << 1 | 1].sum2 + 1ll * (pre[t[rt << 1 | 1].len - 1]) * t[rt].tag % mod) % mod; t[rt].tag = 0; } } inline void add(int rt, int l, int r, int k) { if (l <= t[rt].l && t[rt].r <= r) { t[rt].sum1 = (t[rt].sum1 + 1ll * (pre[t[rt].len] - 1) * k % mod) % mod; t[rt].sum2 = (t[rt].sum2 + 1ll * (pre[t[rt].len - 1]) * k % mod) % mod; t[rt].tag += k; return; } pushdown(rt); if (t[rt << 1].r >= l) add(rt << 1, l, r, k); if (t[rt << 1 | 1].l <= r) add(rt << 1 | 1, l, r, k); upd(rt); } inline void change(int rt, int k, int val) { if (t[rt].l == t[rt].r && t[rt].l == k) { t[rt].tag += val - t[rt].sum1; t[rt].sum1 = t[rt].sum2 = val; return; } pushdown(rt); if (k <= t[rt << 1].r) change(rt << 1, k, val); else change(rt << 1 | 1, k, val); upd(rt); } inline int query(int rt, int l, int r) { if (l <= t[rt].l && t[rt].r <= r) { if (t[rt].l == l) return t[rt].sum2; mat tmp = binpow(t[rt].l - l); return (1ll * t[rt].sum2 * tmp.a[2][2] % mod + 1ll * t[rt].sum1 * tmp.a[2][1] % mod) % mod; } pushdown(rt); int s = 0; if (t[rt << 1].r >= l) s += query(rt << 1, l, r); if (t[rt << 1 | 1].l <= r) s = (s + query(rt << 1 | 1, l, r)) % mod; return s; } int main() { base.a[1][1] = base.a[1][2] = base.a[2][1] = 1; h[1] = base; for (int i = (2), iend = (N - 1); i <= iend; i++) h[i] = h[i - 1] * base; n = read(), m = read(), f[0] = f[1] = 1, pre[0] = 1; for (int i = (2), iend = (n); i <= iend; i++) f[i] = f[i - 1] + f[i - 2], f[i] %= mod; for (int i = (1), iend = (n); i <= iend; i++) pre[i] = pre[i - 1] + f[i], pre[i] %= mod; for (int i = (1), iend = (n); i <= iend; i++) a[i] = read(); build(1, 1, n); for (int i = (1), iend = (m); i <= iend; i++) { int x = read(), y, z, w; if (x == 1) { y = read(), z = read(); change(1, y, z); } if (x == 2) { y = read(), z = read(); printf("%d\n", query(1, y, z)); } if (x == 3) { y = read(), z = read(), w = read(); add(1, y, z, w); } } return 0; }
7