solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> char mp[100][100]; int main() { int n, m, i, j, k; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf(" %s", mp[i]); k = 0; for (i = 1; i <= n; i++) for (j = 0; j < m; j++) { if (mp[i][j] == 'P') { if ((i - 1) && (mp[i - 1][j] == 'W')) { k++; mp[i - 1][j] = ' '; } if ((i < n) && (mp[i + 1][j] == 'W')) { k++; mp[i + 1][j] = ' '; } if ((j) && (mp[i][j - 1] == 'W')) { k++; mp[i][j - 1] = ' '; } if ((j < m - 1) && (mp[i][j + 1] == 'W')) { k++; mp[i][j + 1] = ' '; } } } printf("%d\n", k); return 0; }
2
//test https://blog.csdn.net/rising_shit/article/details/80739593 #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #define maxn 2005 #define _x1 x1 #define _x2 x2 using namespace std; typedef long long ll; const ll mod=1e9+7; int n,m; ll v; int fa[maxn],id[maxn],x[maxn],y[maxn]; ll w[maxn]; int getfa(int now) { return fa[now]==now ? now : fa[now]=getfa(fa[now]); } int cmp(int i,int j) { return w[i]<w[j]; } ll kruskal(int now) { for(int i=1;i<=n;i++) fa[i]=i; ll ans=0; if(now) fa[x[now]]=y[now],ans+=w[now]; for(int i=1;i<=m;i++) { int pos=id[i]; int x1=getfa(x[pos]),y1=getfa(y[pos]); if(x1!=y1) { ans+=w[pos]; fa[x1]=y1; } } return ans; } ll quickpow(ll p,ll k) { ll ans=1; while(k) { if(k&1) ans=(ans*p)%mod; p=(p*p)%mod; k>>=1; } return ans; } int main() { scanf("%d%d%lld",&n,&m,&v); for(int i=1;i<=m;i++) scanf("%d%d%lld",&x[i],&y[i],&w[i]),id[i]=i; sort(id+1,id+1+m,cmp); ll temp=kruskal(0); if(temp>v) return printf("0\n"),0; if(temp==v) { int cnt1=0,cnt2=0; for(int i=1;i<=m;i++) { if(kruskal(i)==temp) cnt1++; else cnt2++; } ll res1=(quickpow(2,cnt1)-2+mod)%mod; ll res2=quickpow(2,cnt2)%mod; return printf("%lld\n",res1*res2%mod),0; } else { int cnt1=0,cnt2=0; for(int i=1;i<=m;i++) { ll res1=kruskal(i); if(res1==v) cnt1++; else if(res1>v) cnt2++; } ll res1=2ll*((quickpow(2,cnt1)-1+mod)%mod)%mod; ll res2=quickpow(2,cnt2); printf("%lld\n",res1*res2%mod); } }
0
#include<iostream> using namespace std; bool find(int a){ for(int i=2;i*i<=a;i++){ if(a % i == 0){ return false; } } return true; } int main(){ int a; cin>>a; while(1){ if(find(a) == true){ cout<<a; return 0; } a++; } }
0
#include <bits/stdc++.h> using namespace std; int cnt1 = 0; const int maxn = 1e5; int fac[maxn + 5], vis[maxn + 5]; vector<int> prime; void init() { memset(vis, 0, sizeof(vis)); for (int i = 2; i <= maxn; i++) { if (!vis[i]) prime.push_back(i); for (int j = 0; j < prime.size() && i * prime[j] <= maxn; j++) { vis[i * prime[j]] = 1; if (i % prime[j] == 0) break; } } } void cal(int n) { int m = sqrt(n + 0.5); int cnt2 = 0; for (int i = 2; i <= m; i++) { if (n % i == 0) { fac[i]++; while (n % i == 0) n /= i; } } if (n > 1) fac[n]++; } int main() { int n, x; init(); memset(fac, 0, sizeof(fac)); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x); cal(x); } int ans = 1; for (int i = 0; i < prime.size(); i++) ans = max(ans, fac[prime[i]]); printf("%d", ans); return 0; }
2
#include<iostream> using namespace std; int main(){ int a, b, c; cin >> a >> b >> c; cout << min(min(a+b, b+c), c+a) << endl; }
0
#include <bits/stdc++.h> using namespace std; void adjust(int &k, int n) { if (k < 1) k = 1; if (k > n) k = n; } void solve() { int n, S, C; cin >> n >> S >> C; string sdir; cin >> sdir; cin >> sdir; int dir = (sdir == "head") ? -1 : 1; string train; cin >> train; int min = 0; for (int i = 0; i < int(train.size()) - 1; ++i) { assert(C != S); if (train[i] == '0') { if (dir * (S - C) > 0) { S += dir; adjust(S, n); } else { S -= dir; adjust(S, n); } C += dir; } else { C += dir; if (C == 1) S = n; else if (C == n) S = 1; else if (dir > 0) S = 1; else S = n; } if (C == 1 || C == n) dir = -dir; if (S == C) { min = i + 1; break; } } if (S == C) cout << "Controller " << min << endl; else cout << "Stowaway\n"; } int main() { solve(); }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n <= 10 || n > 21) { cout << 0 << endl; } else if (n == 20) { cout << 15 << endl; } else if (n <= 21) { cout << 4 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int max_n = 500; pair<bool, int> dp[max_n][max_n]; int dp2[max_n][max_n]; int pref[max_n]; int a[max_n]; signed main() { cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) dp[i][i] = {true, a[i]}; for (int len = 2; len <= n; len++) { for (int l = 0; l + len - 1 < n; l++) { int r = l + len - 1; for (int k = l + 1; k <= r; k++) { if (dp[l][k - 1].first && dp[l][k - 1] == dp[k][r]) { dp[l][r] = {true, dp[l][k - 1].second + 1}; } } } } for (int l = 0; l < n; l++) for (int r = 0; r < n; r++) dp2[l][r] = max_n; for (int i = 0; i < n; i++) dp2[i][i] = 1; for (int len = 2; len <= n; len++) { for (int l = 0; l + len - 1 < n; l++) { int r = l + len - 1; if (dp[l][r].first) dp2[l][r] = 1; for (int k = l + 1; k <= r; k++) { if (dp[l][k - 1].first) dp2[l][r] = min(dp2[l][r], dp2[k][r] + 1); } } } cout << dp2[0][n - 1]; }
5
#include<iostream> using namespace std; string s; int main() { cin>>s; for(int i=0;i<3;i++)cout<<s[i]; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, unb, sum; int zero[100003]; string s[100003]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { cin >> s[i]; int cnt = 0; int len = s[i].length(); for (int j = 0; j < len; j++) { zero[i] += (s[i][j] == '0'); cnt += (s[i][j] == '1'); if (s[i][j] != '0' && s[i][j] != '1') unb = i; } if (len == 1 && s[i][0] == '0') { puts("0"); exit(0); } if (cnt > 1) unb = i; } for (int i = 1; i <= n; i++) if (unb != i) sum += zero[i]; if (unb) cout << s[unb]; else printf("1"); for (int i = 0; i < sum; i++) printf("0"); }
2
#include <bits/stdc++.h> using namespace std; int solve(int x) { if (x % 2) { const int y = x - 1; for (int n = 1;; n += 2) { const int h = n / 2; if (y % 4 == 2 && h <= 1) continue; const int m = h * (h + 1) / 2; if (m * 4 >= y) return n; } } else { for (int n = 1;; n++) { if (n % 2) { const int h = n / 2; const int m = h * (h + 1) / 2; if (m * 4 >= x) return n; } else { const int h = n / 2 - 1; const int m = h * h / 2 + h % 2; if (x % 4 != 0) continue; if (m * 4 >= x) return n; } } } return -1; } int main() { int x; scanf("%d", &x); printf("%d\n", solve(x)); return 0; }
1
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 1e3; int a[maxn+10]; int main() { int n,X,Y; scanf("%d%d%d",&n,&X,&Y); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int ans; if(n==0) ans=abs(X-Y); if(n==1) ans=abs(a[n]-Y); if(n>=2) ans=max(abs(Y-a[n]),abs(a[n-1]-a[n])); printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; long long fact[110], inv[110]; long long ksm(long long x, long long y) { long long ret = 1; while (y > 0) { if (y & 1) { ret = (ret * x) % 1000000007; } x = (x * x) % 1000000007; y >>= 1; } return ret; } long long GetC(long long x, long long y) { if (x < y || x < 0 || y < 0) { return 0; } return (fact[x] * ((inv[y] * inv[x - y]) % 1000000007)) % 1000000007; } long long n, m, k, dp[2][52][52][110], ans = 0; int main() { long long i, j, l, p, x; fact[0] = inv[0] = 1; for (i = 1; i < 110; i++) { fact[i] = (fact[i - 1] * i) % 1000000007; inv[i] = ksm(fact[i], 1000000007 - 2); } cin >> n >> m >> k; memset(dp, 0, sizeof(dp)); for (i = 1; i * 2 <= n; i++) { dp[0][i][i][1] = 1; } for (i = 2; i <= min(m, n / 2 + 1); i++) { for (j = 1; j * 2 <= n; j++) { for (l = 1; l * 2 <= n; l++) { for (p = 1; p <= k; p++) { if (dp[i & 1][j][l][p] == 0) { continue; } ans = (ans + (m - i + 1) * dp[i & 1][j][l][p]) % 1000000007; for (x = 1; (x + l) * 2 <= n; x++) { if (p * GetC(j + x - 1, x) > k) { continue; } dp[(i & 1) ^ 1][x][l + x][p * GetC(j + x - 1, x)] = (dp[(i & 1) ^ 1][x][l + x][p * GetC(j + x - 1, x)] + dp[i & 1][j][l][p]); } } } } memset(dp[i & 1], 0, sizeof(dp[i & 1])); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline long long read() { char c = getchar(); long long tot = 1; while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') { tot = -1; c = getchar(); } long long sum = 0; while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return sum * tot; } inline void wr(long long x) { if (x < 0) { putchar('-'); wr(-x); return; } if (x >= 10) wr(x / 10); putchar(x % 10 + '0'); } inline void wrn(long long x) { wr(x); putchar('\n'); } inline void wri(long long x) { wr(x); putchar(' '); } inline void wrn(long long x, long long y) { wri(x); wrn(y); } inline void wrn(long long a, long long b, long long c) { wri(a); wrn(b, c); } long long n, m, ans, q, z[1000055], b[1000055], tot, num; char str[1000055]; signed main() { scanf("%s", str + 1); n = strlen(str + 1); for (long long i = (1); i <= (n); i++) { if (i == 1 || str[i] != str[i - 1]) z[++tot] = 1; else z[tot] = 0; } if (str[n] == str[1]) z[1] = 0, tot--; long long t = 1; for (long long i = (1); i <= (tot); i++) { if (z[i] == 1) { t = i; break; } } for (long long i = (t); i <= (tot); i++) b[++num] = z[i]; for (long long i = (1); i <= (t - 1); i++) b[++num] = z[i]; for (long long i = (1); i <= (num); i++) { if (b[i] == 1) q++; else { ans += (q >> 1) + 1; q = 0; } } wrn(ans + (q >> 1)); return 0; }
5
#include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <climits> #include <ctime> #include <cassert> using namespace std; #define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++) #define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++) #define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--) #define all(v) begin(v), end(v) #define pb(a) push_back(a) #define fr first #define sc second #define INF 2000000000 #define X real() #define Y imag() #define EPS (1e-10) #define EQ(a,b) (abs((a) - (b)) < EPS) #define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) ) #define LE(n, m) ((n) < (m) + EPS) #define LEQ(n, m) ((n) <= (m) + EPS) #define GE(n, m) ((n) + EPS > (m)) #define GEQ(n, m) ((n) + EPS >= (m)) typedef vector<int> VI; typedef vector<VI> MAT; typedef pair<int, int> pii; typedef long long ll; typedef complex<double> P; typedef pair<P, P> L; typedef pair<P, double> C; int dy[]={0, 0, 1, -1}; int dx[]={1, -1, 0, 0}; int const MOD = 1000000007; namespace std { bool operator<(const P& a, const P& b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } // ?§?????????¨????????¨???????????????????¨???????????§??????? template <typename T> struct Edge { int from, to; T cost; Edge(int s, T d) : to(s), cost(d) {} Edge(int f, int s, T d) : from(f), to(s), cost(d) {} bool operator<(const Edge &e) const { return cost < e.cost; } bool operator>(const Edge &e) const { return cost > e.cost; } }; #define Graph(type) vector< vector< Edge<type> > > template <typename T> vector<T> dijkstra(vector< vector< Edge<T> > > &G, int s) { int V = G.size(); vector<T> d(V); fill(d.begin(), d.end(), INF); priority_queue<pii, vector<pii>, greater<pii> > que; d[s] = 0; que.push(pii(0,s)); while(!que.empty()) { pii p = que.top(); que.pop(); int v = p.second; if(d[v] < p.first) continue; for(int i=0; i<G[v].size(); i++) { Edge<T> e = G[v][i]; if(d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.push(pii(d[e.to], e.to)); } } } return d; } template <typename T> vector<T> treeHeight(vector< vector< Edge<T> > > &G) { int n = G.size(); vector<T> v1, v2, v3, ret(n); int p1, p2; v1 = dijkstra<T>(G, 0); p1 = max_element(v1.begin(), v1.end()) - v1.begin(); v2 = dijkstra<T>(G, p1); p2 = max_element(v2.begin(), v2.end()) - v2.begin(); v3 = dijkstra<T>(G, p2); for(int i=0; i<n; i++) ret[i] = max(v2[i], v3[i]); return ret; } signed main() { int n; cin >> n; Graph(int) G(n); int s, t, w; rep(i,0,n-1) { cin >> s >> t >> w; G[s].pb(Edge<int>(t,w)); G[t].pb(Edge<int>(s,w)); } vector<int> ans = treeHeight<int> (G); rep(i,0,ans.size()) cout << ans[i] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int M; int dp[255][255][255]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); M = 100005; int n, i, j, k, q; int sa[] = {0, 0, 0, 0}; vector<char> ca[4]; cin >> n >> q; string w; cin >> w; int lca[n + 1][26]; for (i = 0; i < 26; i++) { lca[n][i] = M; } for (i = n - 1; i >= 0; i--) { for (j = 0; j < 26; j++) { if (w[i] == ('a' + j)) lca[i][j] = i + 1; else lca[i][j] = lca[i + 1][j]; } } dp[0][0][0] = 0; for (i = 0; i < q; i++) { char a, ch; int c; cin >> a >> c; if (a == '-') { ca[c].pop_back(); sa[c]--; } else { cin >> ch; sa[c]++; ca[c].push_back(ch); if (c == 1) { for (j = 0; j <= sa[2]; j++) { for (k = 0; k <= sa[3]; k++) { dp[sa[1]][j][k] = M; if (dp[sa[1] - 1][j][k] < M) dp[sa[1]][j][k] = lca[dp[sa[1] - 1][j][k]][ch - 'a']; if (j > 0 && dp[sa[1]][j - 1][k] < M) dp[sa[1]][j][k] = min(dp[sa[1]][j][k], lca[dp[sa[1]][j - 1][k]][ca[2][j - 1] - 'a']); if (k > 0 && dp[sa[1]][j][k - 1] < M) dp[sa[1]][j][k] = min(dp[sa[1]][j][k], lca[dp[sa[1]][j][k - 1]][ca[3][k - 1] - 'a']); } } } else if (c == 2) { for (j = 0; j <= sa[1]; j++) { for (k = 0; k <= sa[3]; k++) { dp[j][sa[2]][k] = M; if (dp[j][sa[2] - 1][k] < M) dp[j][sa[2]][k] = lca[dp[j][sa[2] - 1][k]][ch - 'a']; if (j > 0 && dp[j - 1][sa[2]][k] < M) dp[j][sa[2]][k] = min(dp[j][sa[2]][k], lca[dp[j - 1][sa[2]][k]][ca[1][j - 1] - 'a']); if (k > 0 && dp[j][sa[2]][k - 1] < M) dp[j][sa[2]][k] = min(dp[j][sa[2]][k], lca[dp[j][sa[2]][k - 1]][ca[3][k - 1] - 'a']); } } } else { for (j = 0; j <= sa[1]; j++) { for (k = 0; k <= sa[2]; k++) { dp[j][k][sa[3]] = M; if (dp[j][k][sa[3] - 1] < M) dp[j][k][sa[3]] = lca[dp[j][k][sa[3] - 1]][ch - 'a']; if (j > 0 && dp[j - 1][k][sa[3]] < M) dp[j][k][sa[3]] = min(dp[j][k][sa[3]], lca[dp[j - 1][k][sa[3]]][ca[1][j - 1] - 'a']); if (k > 0 && dp[j][k - 1][sa[3]] < M) dp[j][k][sa[3]] = min(dp[j][k][sa[3]], lca[dp[j][k - 1][sa[3]]][ca[2][k - 1] - 'a']); } } } } if (dp[sa[1]][sa[2]][sa[3]] < M) cout << "YES"; else cout << "NO"; cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; int l[500], r[500], d[500], ans[500]; for (int i = 0; i < q; i++) { cin >> l[i] >> r[i] >> d[i]; int quo; if (d[i] < l[i]) { ans[i] = d[i]; } else { quo = int(r[i] / d[i]); ans[i] = d[i] * (quo + 1); } } for (int i = 0; i < q; i++) { cout << ans[i] << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[n], p[n]; for (int i = 0; i < n; i++) cin >> p[i]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; int m; cin >> m; set<int> s[4]; for (int i = 0; i < n; i++) { s[a[i]].insert(p[i]); s[b[i]].insert(p[i]); } int d; while (m--) { cin >> d; if (s[d].empty() == true) cout << "-1" << " "; else { int ans = *s[d].begin(); for (int i = 1; i < 4; i++) { s[i].erase(ans); } cout << ans << " "; } } }
2
// #define _GLIBCXX_DEBUG // for STL debug (optional) #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; #define debug(...) fprintf(stderr, __VA_ARGS__) #define int long long int template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const ll INF = 1001001001001001LL; const ll MOD = 1000000007LL; // Dinic を信じろ template <typename T> struct Edge { int to; T cap, cost; int rev; Edge(int t, T c, int r) : to(t), cap(c), rev(r) {} Edge(int t, T ca, T co, int r) : to(t), cap(ca), cost(co), rev(r) {} }; template <typename T> using Graph = vector< vector< Edge<T> > >; template<typename T> struct Dinic { int N, M; Graph<T> G; vector<int> level, iter; Dinic(int n, int m = 0) : N(n), M(m) { G.resize(N); level.resize(N); iter.resize(N); } void add_edge(int from, int to, T cost) { G[from].push_back(Edge<T>(to , cost, G[to ].size() )); G[to ].push_back(Edge<T>(from, 0.0 , G[from].size() - 1)); } void readGraph() { for(int i=0; i<M; i++) { int a, b; T c; cin >> a >> b >> c; // a--; b--; add_edge(a, b, c); } } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; que.push(s); level[s] = 0; while(!que.empty()) { int temp = que.front(); que.pop(); for(size_t i=0; i<G[temp].size(); i++) { Edge<T> &e = G[temp][i]; if(e.cap > 0 && level[e.to] < 0) { level[e.to] = level[temp] + 1; que.push(e.to); } } } } T dfs(int v, int t, T f) { if(v == t) return f; for(int &i = iter[v]; i<(int)G[v].size(); i++) { Edge<T> &e = G[v][i]; if(e.cap > 0 && level[v] < level[e.to]) { T d = dfs(e.to, t, min(f, e.cap)); if(d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } T max_flow(int s, int t) { T flow = 0.0, f; while(1) { bfs(s); if(level[t] < 0) return flow; fill(iter.begin(), iter.end(), 0); while( (f = dfs(s, t, INF)) > 0 ) flow += f; } } }; // 最小流量制約付きフロー // u から v に最小 lb, 最大 ub 流す template<typename flow_type> struct Dinic_lr { Dinic<flow_type> fl; int S, T; flow_type sum_lb; Dinic_lr() {} Dinic_lr(int N) : fl(N+2, 0), S(N), T(N+1), sum_lb(0) {} void add_edge(int u, int v, flow_type lb, flow_type ub) { assert(0 <= lb && lb <= ub); if(u == v || ub == 0) return; fl.add_edge(u, v, ub - lb); fl.add_edge(S, v, lb); fl.add_edge(u, T, lb); sum_lb += lb; } flow_type max_flow(int s, int t) { flow_type a = fl.max_flow(S, T); flow_type b = fl.max_flow(s, T); flow_type c = fl.max_flow(S, t); flow_type d = fl.max_flow(s, t); return (a + c == sum_lb && a + b == sum_lb) ? b + d : -1; } }; signed main() { int N, M; cin >> N >> M; vector< pair<int, int> > edges; for(int i=0; i<M; i++) { int a, b; cin >> a >> b; a--; b--; Dinic<int> fl(2 + 2 * N); int source = 2*N, sink = source + 1; fl.add_edge(source, 2*a, 1); fl.add_edge(source, 2*b, 1); fl.add_edge(1, sink, 2); for(int i=0; i<N; i++) { fl.add_edge(2*i, 2*i + 1, 1 + (i == 0)); } for(auto e : edges) { int u, v; tie(u, v) = e; fl.add_edge(2*u + 1, 2*v, 1); fl.add_edge(2*v + 1, 2*u, 1); } edges.emplace_back(a, b); int f = fl.max_flow(source, sink); if(f == 2) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long n, k, a, b; int main() { cin >> n >> k >> a >> b; long long mn, mx; mn = min(a, b); mx = max(a, b); if (mx > ((k) * (mn + 1))) { cout << "NO" << endl; return 0; } char c = 'G', d = 'B'; if (a == mn) { swap(a, b); swap(c, d); } long long q = a / (b + 1), r = a % (b + 1); while (r > 0 && n > 0) { for (int i = 1; i <= min(a, min(k, q + 1)); i++) cout << c; n -= (q + 1); a -= (q + 1); if (n > 0) cout << d; b--; n--; r--; } while (n > 0) { for (int i = 1; i <= min(a, min(q, n)); i++) cout << c; n -= q; a -= q; if (n > 0 && b > 0) cout << d; b--; } cout << endl; return 0; }
4
#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> a(100, 0); int d = 0; int g = 0; bool c = false; for (int i = 0; i < 4; i++) { cin >> a[i]; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i != j) { if (a[i] == a[j]) { d = i; g = j; break; } } } } for (int i = 0; i < 4; i++) { if (i == d || i == g) { continue; } for (int j = 0; j < 4; j++) { if (j == d || j == g) { continue; } if (i != j) { if (a[i] == a[j]) { c = true; } } } } if (c) { cout << "yes" << endl; } else { cout << "no" << endl; } return 0; }
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; int main(){ ll N; cin >> N; vector<ll> A(N),B(N+1); for(int i=0;i<N;i++){ cin >> A[i]; B[A[i]]++; } vector<ll> cnt(N+1,0),D(N+1,0); for(int i=1;i<=N;i++) cnt[B[i]]++; vector<ll> sum(N+1,0),sum2(N+1,0); for(ll i=1;i<=N;i++){ sum[i] = sum[i-1]+i*cnt[i]; sum2[i] = sum2[i-1]+cnt[i]; } vector<ll> f(N+1),ans(N+1,0); for(ll i=1;i<=N;i++){ f[i] = (sum[i]+i*(sum2[N]-sum2[i]))/i; } int now = 1; for(int i=N;i>=1;i--){ while(now<=N && now<=f[i]){ ans[now] = i; now++; } } for(int i=1;i<=N;i++) cout << ans[i] << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int k; scanf("%d", &k); int pos = 0; for (int i = 0; i < 20; ++i) { if ((k >> i) & 1) { pos = i; } } int incre = k + (1 << (pos + 1)), incre_no_k = 1 << (pos + 1); printf("%d %d\n", 2, 3); printf("%d %d %d\n%d %d %d\n", incre, incre_no_k, incre_no_k, k, incre, k); }
4
#include<bits/stdc++.h> using namespace std; template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } typedef long long ll; typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<long long> vlong; typedef vector<string> vstring; #define vpush(a,x) a.push_back(x); #define rep(i, n) REP(i, 0, n) #define all(v) v.begin(), v.end() #define REP(i, x, n) for(int i = x; i < n; i++) const int INF = 1 << 30; const int dx[] = {1,0,-1,0,1,1,-1,-1}; const int dy[] = {0,-1,0,1,1,-1,-1,1}; #define stp(x) setprecision(x) int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int a,b,x,sum=0; cin>>a>>b>>x; if(a>b*2){ sum=(x/500)*b; x%=500; } else{ sum=(x/1000)*a; x%=1000; } if(x>500){ b*=2; } if(x==0){ cout<<sum<<'\n'; return(0); } if(a>b){ sum+=b; } else{ sum+=a; } cout<<sum<<'\n'; return(0); }
0
#include <bits/stdc++.h> int main() { long n; scanf("%ld\n", &n); if (n % 3 == 1) { --n; } else if (n % 3 == 2) { ++n; } long feet = n / 36; long inches = (n - 36 * feet) / 3; printf("%ld %ld\n", feet, inches); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; if (n == 2) { cout << "2\n1 2\n"; continue; } cout << "2\n"; cout << n << " " << n - 2 << "\n"; cout << n - 1 << " " << n - 1 << "\n"; int pos = n - 1, next = n - 3; for (long long i = (0); i < (long long)(n - 3); i++) { cout << pos << " " << next << "\n"; pos--; next--; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k, a[2005], res, temp, s; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a, a + n + 1); s = 1; for (int i = a[1]; i <= a[n]; i++) { temp = 0; for (int j = 1; j <= n; j++) if (a[j] >= i) temp++; if (temp == 0) break; int x = temp / k; if (temp % k != 0) x++; res += 2 * x * (i - s) - (i - s); s = i; } res += a[n] - 1; cout << res; cin >> n; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1000 + 10, inf = 1e-10; int main() { int book[40], n, m; scanf("%d%d", &m, &n); bool flag = 1; for (int i = 1; i <= n; i++) { printf("1\n"); fflush(stdout); scanf("%d", &book[i]); if (!book[i]) { flag = 0; break; } } if (flag) { int _begin = 1, _end = m, mid = (m + 1) / 2; int cnt = 0, temp; while (_begin <= _end) { cnt++; printf("%d\n", mid); fflush(stdout); scanf("%d", &temp); if (temp * book[cnt] == 1) { _begin = mid + 1; } else if (temp * book[cnt] == -1) { _end = mid; } else { break; } mid = (_begin + _end) / 2; if (cnt == n) cnt = 0; } } }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); int mx = 0; for (auto &it : a) { cin >> it; mx = max(mx, it); } int idx = -1; for (int i = 0; i < n; ++i) { if (a[i] != mx) continue; if (i > 0 && a[i - 1] != mx) idx = i + 1; if (i < n - 1 && a[i + 1] != mx) idx = i + 1; } cout << idx << endl; } return 0; }
3
#include <bits/stdc++.h> enum Opcodes { A = 'A', B = 'B', C = 'C', MAX }; uint32_t askFor(Opcodes opcode, uint32_t num) { assert(opcode >= Opcodes::A && opcode < Opcodes::MAX); assert(num > 0); assert(opcode != Opcodes::B || num > 1); printf("%c %u\n", (char)opcode, num); fflush(stdout); uint32_t res = 0; if (opcode != Opcodes::C) { scanf("%u", &res); } return res; } uint32_t minSieve(bool *sieve, uint32_t last, uint32_t limit) { assert(sieve && last > 0); uint32_t res = 0; for (uint32_t i = last + 1; i < limit; i++) { if (sieve[i]) { res = i; break; } } return res; } uint32_t calc(uint32_t n) { constexpr size_t MAX_N = 100000; constexpr size_t SIEVE_SIZE = MAX_N + 1; static bool sieve[SIEVE_SIZE]; for (size_t i = 0; i < SIEVE_SIZE; i++) { sieve[i] = true; } sieve[0] = sieve[1] = false; uint32_t min_sieve = 1; uint32_t x = 1; uint32_t limit = n; uint32_t remaining = n; const uint32_t group = (uint32_t)pow(n, 0.4); while (min_sieve > 0) { std::list<uint32_t> primes; for (uint32_t g = 0; g < group; g++) { min_sieve = minSieve(sieve, min_sieve, limit + 1); uint32_t p = min_sieve; if (p) { primes.push_back(p); sieve[p] = false; for (uint32_t i = p * p; i <= limit; i += p) { sieve[i] = false; } remaining -= askFor(Opcodes::B, p); } else { break; } } if (primes.size() > 0) { uint32_t real_count = askFor(Opcodes::A, 1); if (real_count != remaining) { remaining = real_count; for (auto p : primes) { uint32_t multiplier = 1; for (uint32_t power = 0; power < (uint32_t)(log(limit) / log(p)); power++) { uint32_t num = pow(p, power + 1); if (askFor(Opcodes::A, num) > 0) { multiplier = num; } else { break; } } x *= multiplier; limit /= multiplier; } } } } return x; } int main() { uint32_t n, x = 1; scanf("%u", &n); if (n > 1) { x = calc(n); } askFor(Opcodes::C, x); return 0; }
5
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { std::string s; std::cin >> s; if (s.length() % 2 == 0 && s[0] != ')' && s.back() != '(') { std::cout << "YES\n"; } else { std::cout << "NO\n"; } } return 0; }
1
#include <bits/stdc++.h> int main() { int n; char cad[15]; scanf("%s", cad); n = strlen(cad); int i = 0, j = n - 1, cont = 0; while (i < j) { if (cad[i] != cad[j] && cont == 0) { cont++; } else if (cad[i] != cad[j] && cont == 1) { printf("NO\n"); return 0; } i++; j--; } if (n % 2 == 1 && cont == 0) { printf("YES\n"); return 0; } if (cont == 1) { printf("YES\n"); return 0; } else printf("NO\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, q, x, y; int connect[100001]; vector<int> g[100001]; void dfs(int v, int c) { connect[v] = c; int size_ = g[v].size(); for (int i = 0; i < size_; ++i) { if (connect[g[v][i]] == 0) { dfs(g[v][i], c); } } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } int c = 1; for (int i = 0; i < n; ++i) { if (connect[i] == 0) { dfs(i, c); c++; } } cin >> q; for (int i = 0; i < q; ++i) { cin >> x >> y; if (connect[x] == connect[y]) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/stack:64000000") using namespace std; struct Lesson { long long L, R; int D, N; Lesson() {} Lesson(long long L, long long R, int D, int N) : L(L), R(R), D(D), N(N) {} bool operator<(const Lesson& lesson) const { return D < lesson.D; } }; struct Prev { int num, exc; Prev() {} Prev(int num, int exc) : num(num), exc(exc) {} }; long long dp[55][55][111]; Prev pr[55][55][111]; int main() { int need, m, k; scanf("%d %d %d", &need, &m, &k); vector<Lesson> v(m); for (int(i) = 0; (i) < (m); (i)++) { long long L, R; int D; cin >> L >> R >> D; v[i] = Lesson(L, R, D, i + 1); } sort((v).begin(), (v).end()); memset(dp, 0, sizeof(dp)); memset(pr, -1, sizeof(pr)); for (int(i) = 0; (i) < (m); (i)++) for (int(j) = 0; (j) < ((int)(v[i].R - v[i].L) + 1); (j)++) dp[1][i][j] = v[i].L + j; for (int done = 1; done < need; done++) { for (int cur = 0; cur < m; cur++) { for (int exc = 0; exc <= (int)(v[cur].R - v[cur].L); exc++) { long long did = v[cur].L + exc; if (dp[done][cur][exc] == 0) continue; for (int nxt = cur + 1; nxt < m; nxt++) { if (v[nxt].D > v[cur].D) { long long val[2]; val[0] = did + k; if (did > (long long)(1e16 + 10)) { val[1] = -1; } else { val[1] = did * k; } for (int(j) = 0; (j) < (2); (j)++) { if (v[nxt].L <= val[j] && val[j] <= v[nxt].R) { int t = (int)(val[j] - v[nxt].L); if (dp[done + 1][nxt][t] < dp[done][cur][exc] + val[j]) { dp[done + 1][nxt][t] = dp[done][cur][exc] + val[j]; pr[done + 1][nxt][t] = Prev(cur, exc); } } } } } } } } int B = -1, C = -1; for (int(i) = 0; (i) < (111); (i)++) for (int(j) = 0; (j) < (111); (j)++) { if (dp[need][i][j] > 0 && (B == -1 || dp[need][i][j] > dp[need][B][C])) { B = i, C = j; } } if (B != -1) { vector<pair<int, long long> > res(need); for (int A = need; A >= 1; A--) { res[A - 1] = make_pair(v[B].N, v[B].L + C); int b = B, c = C; B = pr[A][b][c].num; C = pr[A][b][c].exc; } cout << "YES\n"; for (int(i) = 0; (i) < ((int)(res).size()); (i)++) { cout << res[i].first << " " << res[i].second << "\n"; } } else { cout << "NO\n"; } return 0; }
3
#include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define sc second typedef pair<int,int> P; int N,K; int C[5003],R[5003]; int A,B; vector<int>G[5003];int s1[5003];bool f[5003]; vector<P>G2[5003]; int main() { scanf("%d%d",&N,&K); for(int i=0;i<N;i++){ scanf("%d%d",&C[i],&R[i]); } for(int i=0;i<K;i++){ scanf("%d%d",&A,&B);A--;B--; G[A].pb(B); G[B].pb(A); } for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ s1[j]=N+1;f[j]=false; } vector<int>ver; ver.pb(i);int ss=0; while((int)ver.size()!=0){ vector<int>ver1; for(int j=0;j<(int)ver.size();j++){ if(f[ver[j]])continue; //printf("%d ",ver[j]); f[ver[j]]=true;s1[ver[j]]=ss; for(int k=0;k<(int)G[ver[j]].size();k++){ ver1.pb(G[ver[j]][k]); } } ver.erase(ver.begin(),ver.end()); for(int j=0;j<(int)ver1.size();j++){ ver.pb(ver1[j]); } //printf("\n"); ss++; } for(int j=0;j<N;j++){ if(i==j)continue; if(R[i]>=s1[j]){ //printf("%d %d\n",i,j); G2[i].pb(P(j,C[i])); } } } for(int j=0;j<N;j++){ s1[j]=10000*N+1; } priority_queue<P,vector<P>,greater<P> >que;que.push(P(0,0)); while(!que.empty()){ P p=que.top();que.pop(); if(s1[p.sc]<=p.fi)continue; s1[p.sc]=p.fi; for(int i=0;i<(int)G2[p.sc].size();i++){ if(s1[G2[p.sc][i].fi]>p.fi+G2[p.sc][i].sc){ que.push(P(p.fi+G2[p.sc][i].sc,G2[p.sc][i].fi)); } } } printf("%d\n",s1[N-1]); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAX = 100000 + 10; const int Mod = (int)1e9 + 7; const int Mod1 = 239482903; const int Mult = 142341; const int Mult1 = 238478989; int n; char str[MAX]; long long sum[MAX]; long long sum1[MAX]; long long P[MAX], P1[MAX]; void init() { int i; for ((i) = (1); (i) <= (n); ++(i)) sum[i] = (sum[i - 1] * Mult + str[i]) % Mod; for ((i) = (1); (i) <= (n); ++(i)) sum1[i] = (sum1[i - 1] * Mult1 + str[i]) % Mod1; } long long getHash(int l, int r) { long long a = (sum[r] - sum[l - 1] * P[r - l + 1] % Mod + Mod) % Mod; long long b = (sum1[r] - sum1[l - 1] * P1[r - l + 1] % Mod1 + Mod1) % Mod1; return a * Mod + b; } int lcs(int a, int b) { if (a <= 0 || b <= 0 || a > n || b > n) return -Mod; if (str[a] != str[b]) return 0; int left = 1, right = min(n - b, n - a) + 1; while (left < right) { int mid = (left + right + 1) / 2; if (getHash(a, a + mid - 1) == getHash(b, b + mid - 1)) left = mid; else right = mid - 1; } return left; } int check(int Len) { int i; for (i = Len + 1; i <= n; i += Len) { int l = lcs(i, i - Len); if (l >= Len) return 1; if (!l) continue; int need = Len - l; int can = lcs(i - need, i - Len - need); if (can >= need) return 1; } return 0; } int fa[MAX]; char nstr[MAX]; void doit(int Len) { int now; int top = 0; int j; for (now = 1; now <= n;) { int next = now + Len; if (next + Len - 1 > n || getHash(next, next + Len - 1) != getHash(now, now + Len - 1)) { nstr[++top] = str[now]; ++now; continue; } while (next + Len - 1 <= n && getHash(next, next + Len - 1) == getHash(now, now + Len - 1)) next += Len; nstr[++top] = str[now]; now = next - Len + 1; } n = top; for ((j) = (1); (j) <= (n); ++(j)) str[j] = nstr[j]; } int main() { int i; scanf("%s", str + 1); n = strlen(str + 1); P[0] = 1; P1[0] = 1; for ((i) = (1); (i) <= (n); ++(i)) { P[i] = P[i - 1] * Mult % Mod; P1[i] = P1[i - 1] * Mult1 % Mod1; } init(); for ((i) = (1); (i) <= (n); ++(i)) if (check(i)) { doit(i); init(); } for ((i) = (1); (i) <= (n); ++(i)) cout << str[i]; cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int n; long long f[MAXN], a[MAXN]; int main() { scanf("%d", &n); for (int i = n; i; i--) scanf("%lld", &a[i]); for (int i = n - 1; i; i--) a[i] = min(a[i], a[i + 1]); a[0] = 1; for (int i = 1; i <= n; i++) { for (long long j = a[i], k, l = 0; j; j %= a[k]) { k = lower_bound(a + 1, a + n + 1, j) - a - 1; f[i] = max(f[i], f[k] + (n - k) * (j - j % a[k] - a[k]) + l); l += (n - k) * (j - j % a[k]); } } printf("%lld\n", f[n]); }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; inline int gcd(int a, int b) { if (a < b) { a ^= b; b ^= a; a ^= b; } return b ? ((a - b) ? gcd(a % b, b) : a) : a; } void solve() { int n, a, b, cur = 1; cin >> n; vector<pair<int, int> > v(n); for (int i = 0; i < n; i++) { cin >> a >> b; v[i] = make_pair(a, b); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) if (v[i].second >= cur) cur = v[i].second; else cur = v[i].first; cout << cur; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
1
#include<iostream> #include<stack> #include<vector> using namespace std; typedef long long ll; typedef vector<ll> V; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) //グラフの隣接リスト vector<ll> g[100010]; //頂点の入次数の管理 ll h[100010]={0}; //頂点の番号 ll num[100010]; int main(){ ll v,e; cin >> v>>e; REP(i,e){ ll s,t; cin >> s >> t; g[s].push_back(t); h[t]++; } //入字数0の頂点の集合 stack<ll> st; REP(i,v)if(h[i]==0)st.push(i); V ans; while(st.size()){ ll s=st.top();st.pop(); ans.push_back(s); REP(i,g[s].size()){ //隣接する頂点の入次数をマイナス1 h[g[s][i]]--; //入次数が0ならstackに追加 if(h[g[s][i]]==0)st.push(g[s][i]); } } if(ans.size()==v)cout <<"0"<<endl; else cout <<"1"<<endl; }
0
#include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> given(n); // the given sequence vector<int> prev(n); // the previous permutation vector<int> next(n); // the next permutation for_each(given.begin(),given.end(),[](int &_e){cin >> _e;}); prev.assign(given.begin(),given.end()); next.assign(given.begin(),given.end()); if (prev_permutation(prev.begin(),prev.end())) { int i = 1; for_each(prev.begin(),prev.end(),[&i,n](int _e){cout << _e << (i++ == n ? "\n" : " ");}); } int j = 1; for_each(given.begin(),given.end(),[&j,n](int _e){cout << _e << (j++ == n ? "\n" : " ");}); if (next_permutation(next.begin(),next.end())) { int i = 1; for_each(next.begin(),next.end(),[&i,n](int _e){cout << _e << (i++ == n ? "\n" : " ");}); } }
0
#include <bits/stdc++.h> int main() { static const int MOD = 998244353; int n; std::cin >> n; std::vector<int> a(n * 2); std::vector<int64_t> fact(n * 2 + 5); std::vector<int64_t> inv(n * 2 + 5); std::vector<int64_t> inv_fact(n * 2 + 5); fact[0] = inv[0] = inv_fact[0] = 1; fact[1] = inv[1] = inv_fact[1] = 1; for (int i = 2; i <= n * 2; ++i) { fact[i] = (fact[i - 1] * i) % MOD; inv[i] = (inv[MOD % i] * (MOD - MOD / i)) % MOD; inv_fact[i] = (inv_fact[i - 1] * inv[i]) % MOD; } auto C = [&](int k, int n) { return (((fact[n] * inv_fact[n - k]) % MOD) * inv_fact[k]) % MOD; }; for (int i = 0; i < 2 * n; ++i) { std::cin >> a[i]; } std::sort(a.begin(), a.end()); int64_t sum = 0; for (int i = 0; i < n * 2; ++i) { sum += i < n ? -a[i] : a[i]; } sum = ((sum % MOD) + MOD) % MOD; std::cout << ((sum * C(n, n * 2)) % MOD); }
2
#include <bits/stdc++.h> using namespace std; string s; class node { public: string s; vector<node *> next; node *f; node() { s = ""; next.clear(); f = NULL; } }; node *root; vector<string> p; int dfs(node *cur, int k) { int ret = 0; if (k == p.size() - 1) { for (int i = 0; i < cur->next.size(); i++) { if (cur->next[i]->s == p[p.size() - 1]) ret++; ret += dfs(cur->next[i], k); } return ret; } for (int i = 0; i < cur->next.size(); i++) { if (cur->next[i]->s == p[k]) ret += dfs(cur->next[i], k + 1); else ret += dfs(cur->next[i], k); } return ret; } int main() { while (cin >> s) { string str = ""; int st = 1, en; root = new node; node *cur = root, *next; while (1) { if (st >= s.length()) break; bool flag = 0, self = 0; for (en = st; s[en] != '>'; en++) ; if (s[st] == '/') { flag = 1; st++; } if (s[en - 1] == '/') { self = 1; en--; } for (int i = st; i < en; i++) str += s[i]; if (self) { next = new node; next->s = str; cur->next.push_back(next); next->f = cur; } else if (!flag) { next = new node; next->s = str; cur->next.push_back(next); next->f = cur; cur = next; } else cur = cur->f; str = ""; if (self) en++; st = en + 2; } int n; cin >> n; getchar(); while (n--) { getline(cin, s); stringstream sin; sin << s; p.clear(); while (sin >> s) p.push_back(s); cout << dfs(root, 0) << endl; } } return 0; }
5
/* _____ _ _ ____ __ __ _ / ____| | | | | | _ \ _ \ \ / /(_) | | ___ __| | ___ __| | | |_) | _ _ (_) \ \ /\ / / _ ____ | | / _ \ / _` | / _ \ / _` | | _ < | | | | \ \/ \/ / | ||_ / | |____| (_) || (_| || __/| (_| | | |_) || |_| | _ \ /\ / | | / / \_____|\___/ \__,_| \___| \__,_| |____/ \__, | (_) \/ \/ |_|/___| __/ | |___/ */ #include<bits/stdc++.h> using namespace std; #define ff first #define ss second #define ll long long #define ld long double #define vll vector<ll> #define vvl vector<vector<ll>> #define pll pair<ll,ll> #define vpll vector<pair<ll,ll>> #define uset unordered_set #define pb push_back #define show(x) cout <<(#x)<<" : "<<x<<endl #define all(x) x.begin(),x.end() #define sortall(x) sort(all(x)) #define sortrev(x) sort(all(x), greater<ll>()) #define op(x) cout<<x<<endl #define opl(x) cout<<x<<" " #define MOD 1073741824 #define MAX 1e6 void solve(){ ll n,cnt = 0; cin>>n; for(int i=1;i<10;i++){ ll curr = i; while(curr <= n){ cnt++; curr *=10; curr+=i; } } op(cnt); } int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t=1; cin>>t; while(t--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; int N; vector<pair<int, int>> red(101); vector<pair<int, int>> blue(101); int main(void){ cin >> N; for(int i = 0; i < N; ++i) cin >> red[i].first >> red[i].second; for(int i = 0; i < N; ++i) cin >> blue[i].first >> blue[i].second; sort(blue.begin(), blue.begin() + N); bitset<101> bit; for(int i = 0; i < N; ++i){ int sub = -1, y = -1; for(int j = 0; j < N; ++j){ if(!bit[j] && blue[i].first > red[j].first && blue[i].second > red[j].second){ if(red[j].second > y) sub = j, y = red[j].second; } } if(y != -1) bit.set(sub); } cout << bit.count() << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct Shoe { int cost; int sz; int idx; Shoe() {} Shoe(int cost, int sz, int idx) : cost(cost), sz(sz), idx(idx) {} }; struct Customer { int money; int sz; int idx; Customer() {} Customer(int money, int sz, int idx) : money(money), sz(sz), idx(idx) {} }; const int MAXN = (int)1e5 + 5; int n, m; vector<Shoe> shoes; vector<Customer> customers; long long int dp[MAXN][4]; int best_mask[MAXN][4]; int bought[MAXN][4]; map<int, Shoe> mapa; bool operator<(const Customer& a, const Customer& b) { return a.sz < b.sz || (a.sz == b.sz && a.idx < b.idx); } int get_mask(int pos, int mask) { if (pos < m - 1) { if (customers[pos + 1].sz == customers[pos].sz) { ; } else if (customers[pos + 1].sz == customers[pos].sz + 1) { mask >>= 1; } else { mask = 0; } } return mask; } long long int solve(int pos, int mask) { if (pos >= m) { return 0; } if (dp[pos][mask] != -1) { return dp[pos][mask]; } int n_mask = get_mask(pos, mask); long long int aux = solve(pos + 1, n_mask); best_mask[pos][mask] = n_mask; bought[pos][mask] = -1; for (int i = 0; i <= 1; i++) { int bit = (mask >> i) & 1; if (bit == 0) { int sz = customers[pos].sz + i; int money = customers[pos].money; if (mapa.find(sz) != mapa.end()) { int cost = mapa[sz].cost; int idx = mapa[sz].idx; if (money >= cost) { int n_mask = get_mask(pos, mask | (1 << i)); long long int temp = cost + solve(pos + 1, n_mask); if (temp > aux) { aux = temp; bought[pos][mask] = idx; best_mask[pos][mask] = n_mask; } } } } } dp[pos][mask] = aux; return aux; } int main(void) { int c, s; scanf(" %d", &n); for (int i = 1; i <= n; i++) { scanf(" %d %d", &c, &s); shoes.push_back(Shoe(c, s, i)); mapa[s] = shoes.back(); } scanf(" %d", &m); for (int i = 1; i <= m; i++) { scanf(" %d %d", &c, &s); customers.push_back(Customer(c, s, i)); } sort(customers.begin(), customers.end()); for (int i = 0; i < m; i++) { for (int mask = 0; mask < 4; mask++) { dp[i][mask] = -1; } } long long int ans = solve(0, 0); int mask = 0; vector<pair<int, int> > orders; for (int i = 0; i < m; i++) { if (bought[i][mask] != -1) { orders.push_back({customers[i].idx, bought[i][mask]}); } mask = best_mask[i][mask]; } printf("%lld\n", ans); printf("%d\n", (int)orders.size()); for (int i = 0; i < (int)orders.size(); i++) { printf("%d %d\n", orders[i].first, orders[i].second); } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> ans; void gogo(int n, int pre) { for (int offset = 1; offset < n; offset *= 2) for (int i = 1; i + offset <= n; i += offset << 1) for (int j = 0; j < offset; j++) ans.emplace_back(pre + i + j, pre + i + j + offset); } void test_case() { int n, k; cin >> n; k = n - 1; k |= k >> 1; k |= k >> 2; k |= k >> 4; k |= k >> 8; k |= k >> 16; ++k; k >>= 1; gogo(k, 0); gogo(k, n - k); int len = ans.size(); cout << len << '\n'; for (int i = 0; i < len; i++) cout << ans[i].first << ' ' << ans[i].second << '\n'; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); int Case = 1; for (int i = 0; i < Case; i++) { test_case(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long int n, a, b; cin >> n; a = 9 * n; b = 8 * n; cout << a << " " << b; }
1
#include <bits/stdc++.h> using namespace std; int main() { string s = "12345"; int x; int G[5][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cin >> x; G[i][j] = x; } } int maxima = 0; do { int si = 0, sum = 0; while (si < 4) { for (int i = si; i < 4; i += 2) { int X = s[i] - '0'; int Y = s[i + 1] - '0'; sum += (G[X - 1][Y - 1] + G[Y - 1][X - 1]); } si++; } if (sum > maxima) maxima = sum; } while (std::next_permutation(s.begin(), s.end())); cout << maxima << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, a[5]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (a[i] < a[j]) swap(a[i], a[j]); cout << a[n / 2]; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int k; cin >> k; int a, b; b = 2000 - (k % 2000); while (1999 * b <= k) b += 2000; a = (k + b) / 2000 - b; cout << "2000\n"; for (int i = 0; i < 1998; ++i) { cout << "0 "; } cout << a << ' ' << b << '\n'; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[2 * n]; for (int i = 0; i < 2 * n; i++) { cin >> a[i]; } sort(a, a + 2 * n); cout << (a[n - 1] == a[n] ? "NO" : "YES") << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int x; cin>>x; cout<<((x>=30)?"Yes":"No")<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char str[n + 1]; cin >> str; int res = 0; for (int i = 1; i < n; i++) { if (str[i] == str[i - 1]) res++; } cout << res; return 0; }
1
#include <bits/stdc++.h> using namespace std; int dx[5] = {-1, 0, 1, 0}; int dy[5] = {0, -1, 0, 1}; int dp[1001][1001][41]; bool colour[45]; vector<pair<int, int>> v1[45]; queue<pair<int, int>> q1; int n, m, K; bool check(int r, int c) { if (r <= 0 || r > n || c <= 0 || c > m) { return false; } return true; } void bfs(int col) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (dp[i][j][0] == col) { q1.push(make_pair(i, j)); } } } for (int i = 1; i <= K; i++) { colour[i] = false; } while (!q1.empty()) { auto hold = q1.front(); q1.pop(); int x = hold.first; int y = hold.second; if (!colour[dp[x][y][0]]) { colour[dp[x][y][0]] = true; for (auto i : v1[dp[x][y][0]]) { if (dp[i.first][i.second][col] > dp[x][y][col] + 1) { dp[i.first][i.second][col] = dp[x][y][col] + 1; q1.push(make_pair(i.first, i.second)); } } } for (int i = 0; i < 4; i++) { int nextx = x + dx[i]; int nexty = y + dy[i]; if (check(nextx, nexty) && dp[nextx][nexty][col] > dp[x][y][col] + 1) { dp[nextx][nexty][col] = dp[x][y][col] + 1; q1.push(make_pair(nextx, nexty)); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> K; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 1; k <= K; k++) { dp[i][j][k] = 1e9; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> dp[i][j][0]; v1[dp[i][j][0]].push_back(make_pair(i, j)); dp[i][j][dp[i][j][0]] = 0; } } for (int i = 1; i <= K; i++) { bfs(i); } int q; cin >> q; while (q--) { int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; int res = abs(r1 - r2) + abs(c1 - c2); for (int i = 1; i <= K; i++) { res = min(res, dp[r1][c1][i] + 1 + dp[r2][c2][i]); } cout << res << '\n'; } }
6
#include <bits/stdc++.h> using namespace std; const int md = 1000003; long long binpow(long long n, long long k) { long long ans = 1; while (k != 0) { if (k % 2 == 1) ans = (ans * n) % md; n = (n * n) % md; k >>= 1; } return ans; } int main() { long long n, k; long long ed = 1; cin >> n >> k; if (n <= 63 && k > (ed << n)) { cout << 1 << " " << 1; return 0; } int b = __builtin_popcountll(k - 1); long long d = k - b - 1, nd = n % (md - 1), kd = k % (md - 1), dd = d % (md - 1), st = (nd * (kd - 1) - dd) % (md - 1); if (st < 0) st += (md - 1); long long z = binpow(2, st), c = 0; if (k - 1 >= md) { c = 0; } else { long long c1 = 1, ns = binpow(2, nd); c1 = binpow(2, dd); c1 = binpow(c1, md - 2); ; for (long long y = 1; y <= k - 1; y++) { c1 = (c1 * (ns - y)) % md; } c = c1; } long long ans = (z - c) % md; if (ans < 0) ans += md; ans %= md; z %= md; cout << ans << " " << z; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long int INF = 98769876987698889LL; const int MOD = 1e9 + 7; const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dx4[] = {1, -1, 0, 0}; const int dy4[] = {0, 0, 1, -1}; const int N = 2e5 + 5; long long int dp1[N], a[N], f[N], dp2[N], s[N]; int n; vector<int> G[N]; void dfs(int x, int pa) { if (G[x].size() == 1 and G[x][0] == pa) { s[x] = a[x]; return; } s[x] = a[x]; for (auto k : G[x]) { if (k == pa) continue; dfs(k, x); s[x] += s[k]; } } void dfs2(int x, int pa) { if (G[x].size() == 1 and G[x][0] == pa) { f[x] = 0; return; } f[x] = 0; vector<long long int> v; for (auto k : G[x]) { if (k == pa) continue; dfs2(k, x); if (s[k] >= dp1[k]) { v.push_back(s[k]); } else { v.push_back(dp1[k]); } } sort(v.begin(), v.end()); dp1[x] = v[v.size() - 1]; if (v.size() > 1) { f[x] = 1; dp2[x] = v[v.size() - 2]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < N; i++) { dp1[i] = -INF; dp2[i] = -INF; } for (int i = 2; i <= n; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } if (n == 1) { cout << "Impossible" << endl; return 0; } dfs(1, 0); dfs2(1, 0); int fl = 0; for (int i = 1; i <= n; i++) { if (f[i]) fl = 1; } if (!fl) { cout << "Impossible" << endl; return 0; } long long int ans = -INF; for (int i = 1; i <= n; i++) { if (f[i]) { ans = max(ans, dp1[i] + dp2[i]); } } cout << ans; }
4
#include <bits/stdc++.h> int main() { char s[110]; scanf("%s", s); int len = strlen(s); if (len == 1) { if (s[0] >= 'A' && s[0] <= 'Z') s[0] = tolower(s[0]); else s[0] = toupper(s[0]); puts(s); return 0; } int acnt = 0, bcnt = 0; if (s[0] >= 'A' && s[0] <= 'Z') acnt++; for (int i = 1; i < len; i++) { if (s[i] >= 'A' && s[i] <= 'Z') acnt++, bcnt++; } if (acnt == len) { for (int i = 0; i < len; i++) s[i] = tolower(s[i]); } else if (bcnt == len - 1) { s[0] = toupper(s[0]); for (int i = 1; i < len; i++) s[i] = tolower(s[i]); } puts(s); }
1
#include <bits/stdc++.h> using namespace std; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } int board[8][8]; bool valid(int x, int y) { return 0 <= x && x < 8 && 0 <= y && y < 8; } void Input(int t) { string s; cin >> s; int x = s[0] - 'a'; int y = s[1] - '1'; board[x][y] = t; } void print(int board[8][8]) { for (int i = 0; i < (int)(8); ++i) { for (int j = 0; j < (int)(8); ++j) cout << board[i][j] << ' '; cout << endl; } } int sub[8][8]; void rook(int x, int y) { int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; for (int k = 0; k < (int)(4); ++k) { int nx = x + dx[k]; int ny = y + dy[k]; while (valid(nx, ny) && (board[nx][ny] == 0 || board[nx][ny] == 'K')) { sub[nx][ny]++; nx += dx[k]; ny += dy[k]; } if (valid(nx, ny)) sub[nx][ny]++; } } void king(int x, int y) { for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) if (dx || dy) if (valid(x + dx, y + dy)) sub[x + dx][y + dy]++; } int solve() { memset(sub, 0, sizeof(sub)); for (int i = 0; i < (int)(8); ++i) for (int j = 0; j < (int)(8); ++j) { if (board[i][j] == '+') { rook(i, j); } if (board[i][j] == 'k') { king(i, j); } } for (int i = 0; i < (int)(8); ++i) for (int j = 0; j < (int)(8); ++j) { if (board[i][j] == 'K') { if (sub[i][j] == 0) return 0; for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) { int nx = i + dx; int ny = j + dy; if (valid(nx, ny) && sub[nx][ny] == 0) return 0; } } } return 1; } int main() { memset(board, 0, sizeof(board)); Input('+'); Input('+'); Input('k'); Input('K'); int res = solve(); if (res == 1) cout << "CHECKMATE" << endl; else cout << "OTHER" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int t, n, i, j, k, f, b[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}, a[1008], c[1008]; int main() { for (scanf("%d", &t); t--; printf("\n"), i = 0) { for (scanf("%d", &n); i++ < n; scanf("%d", a + i), c[i] = 0) ; for (k = 1, j = 11; j--; k += !!f) for (i = f = 0; i++ < n; c[i] || a[i] % b[j] ? 0 : c[i] = f = k) ; for (printf("%d\n", k - 1), i = 0; i++ < n; printf("%d ", c[i])) ; } exit(0); }
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool comp(string s, string t){ for(int i = 0; i < min(s.size(), t.size()); i++){ if(s[i] < t[i]) return true; if(s[i] > t[i]) return false; } return s.size() <= t.size(); } int getDist(const string& s, const string& t){ int dp[20][20]; for(int i = 0; i <= s.size(); i++) dp[i][0] = i; for(int i = 0; i <= t.size(); i++) dp[0][i] = i; for(int i = 1; i <= s.size(); i++) for(int j = 1; j <= t.size(); j++) dp[i][j] = min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + (s[i - 1] != t[j - 1])); return (dp[s.size()][t.size()]); } string name[256]; bool f(string s, string t, int d){ if(s == t) return true; if(getDist(s, t) <= d) return true; if(d >= 1){ // 1??????????????? if(s.size() == t.size()){ for(int i = 0; i < s.size() - 1; i++){ swap(s[i], s[i + 1]); if(s == t) return true; swap(s[i], s[i + 1]); } } } if(d == 2){ // 2??????????????? if(s.size() == t.size()){ for(int i = 0; i < s.size() - 1; i++){ swap(s[i], s[i + 1]); for(int j = 0; j < s.size() - 1; j++){ if(i == j) continue; swap(s[j], s[j + 1]); if(s == t) return true; swap(s[j], s[j + 1]); } swap(s[i], s[i + 1]); } } if(s.size() - 1 == t.size()){ // ???????????£??????????????? for(int i = 0; i < s.size() - 2; i++){ swap(s[i], s[i + 2]); int l = i + 1, r = s.size() - 1 - l; string ss = s.substr(0, i + 1) + s.substr(i + 2, r); if(ss == t) return true; swap(s[i], s[i + 2]); } } if(t.size() - 1 == t.size()){ // ???????????£??????????????? for(int i = 0; i < t.size() - 2; i++){ swap(t[i], t[i + 2]); int l = i + 1, r = t.size() - 1 - l; string tt = t.substr(0, i + 1) + t.substr(i + 2, r); if(tt == s) return true; swap(t[i], t[i + 2]); } } // 1?????????????????§??¨????????¢1 for(int i = 0; i < s.size() - 1; i++){ swap(s[i], s[i + 1]); if(getDist(s, t) <= 1) return true; swap(s[i], s[i + 1]); } } return false; } int main(){ cin.tie(0); ios::sync_with_stdio(false); #ifdef LOCAL std::ifstream in("in"); std::cin.rdbuf(in.rdbuf()); #endif int n, d; while(cin >> n >> d, n){ for(int i = 0; i < n; i++){ cin >> name[i]; } vector<pair<string, string>> ans; for(int i = 0; i < n; i++){ for(int j = i + 1; j < n; j++){ if(f(name[i], name[j], d)){ string s1 = min(name[i], name[j], comp); string s2 = max(name[i], name[j], comp); ans.push_back({ s1, s2 }); } } } sort(ans.begin(), ans.end()); for(auto v : ans){ cout << v.first + "," + v.second << endl; } cout << ans.size() << endl; } }
0
#include <stdio.h> #include <algorithm> #include <utility> #include <set> #include <queue> using namespace std; typedef long long ll; int main(void) { ll i, j, k, n, m, ans = 0, a, b, t, nowx, nowy; scanf("%lld%lld", &n, &m); set<ll> x, y; set<ll>::iterator it; pair<ll, pair<ll, ll> > vx[m], vy[n]; for(i = 0; i < n; ++i) { scanf("%lld%lld%lld", &vy[i].second.first, &vy[i].second.second, &vy[i].first); x.insert(vy[i].second.first), x.insert(vy[i].second.second); y.insert(vy[i].first); } for(i = 0; i < m; ++i) { scanf("%lld%lld%lld", &vx[i].first, &vx[i].second.first, &vx[i].second.second); y.insert(vx[i].second.first), y.insert(vx[i].second.second); x.insert(vx[i].first); } a = x.size(), b = y.size(); ll px[a], py[b]; ll lx[a][b], ly[a][b], flg[a][b]; for(i = 0, it = x.begin(); it != x.end(); ++i, ++it) px[i] = *it; for(i = 0, it = y.begin(); it != y.end(); ++i, ++it) py[i] = *it; for(i = 0; i < a; ++i) for(j = 0; j < b; ++j) lx[i][j] = ly[i][j] = flg[i][j] = true; for(i = 0; i < m; ++i) { t = lower_bound(px, px + a, vx[i].first) - px; for(j = lower_bound(py, py + b, vx[i].second.first) - py; j < b && py[j] < vx[i].second.second; ++j) lx[t][j] = false; } for(i = 0; i < n; ++i) { t = lower_bound(py, py + b, vy[i].first) - py; for(j = lower_bound(px, px + a, vy[i].second.first) - px; j < a && px[j] < vy[i].second.second; ++j) ly[j][t] = false; } queue< pair<ll, ll> > q; nowx = lower_bound(px, px + a, 0) - px - 1, nowy = lower_bound(py, py + b, 0) - py - 1; q.push(make_pair(nowx, nowy)); while(q.size()) { nowx = q.front().first, nowy = q.front().second; q.pop(); if(nowx < 0 || nowx > a - 2 || nowy < 0 || nowy > b - 2) { printf("INF"); return 0; } if(!flg[nowx][nowy]) continue; flg[nowx][nowy] = false; ans += (px[nowx + 1] - px[nowx]) * (py[nowy + 1] - py[nowy]); if(lx[nowx][nowy]) q.push(make_pair(nowx - 1, nowy)); if(ly[nowx][nowy]) q.push(make_pair(nowx, nowy - 1)); if(lx[nowx + 1][nowy]) q.push(make_pair(nowx + 1, nowy)); if(ly[nowx][nowy + 1]) q.push(make_pair(nowx, nowy + 1)); } printf("%lld", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<set<int>> segs, cur; vector<int> ans; set<int> ins; void init() { segs.clear(); } void solve() { init(); int n, cnt = 0; scanf("%d", &n); for (int i = 1; i < n; ++i) { ins.clear(); cnt = 0; int a = 0; scanf("%d", &cnt); for (int j = 1; j <= cnt; ++j) { scanf("%d", &a); ins.insert(a); } segs.push_back(ins); } for (cnt = 1; cnt <= n; ++cnt) { ans.clear(); ans.push_back(cnt); cur = segs; for (auto &s : cur) if (s.count(cnt)) s.erase(cnt); bool flag = 1; for (int i = 1; i < n; ++i) { int cnt1 = 0; int nxt = -1; for (auto &s : cur) if (s.size() == 1) { ++cnt1; nxt = *s.begin(); } if (cnt1 != 1) { flag = 0; break; } for (auto &s : cur) if (s.count(nxt)) s.erase(nxt); ans.push_back(nxt); } if (flag) { set<set<int>> chks(segs.begin(), segs.end()); for (int i = 1; i < n; ++i) { ins.clear(); ins.insert(ans[i]); bool f = 0; for (int j = i - 1; j >= 0; --j) { ins.insert(ans[j]); if (chks.count(ins)) { chks.erase(ins); f = 1; break; } } if (!f) flag = 0; } } if (flag) { for (auto &i : ans) printf("%d ", i); printf("\n"); break; } } } int main() { int T; scanf("%d", &T); while (T--) solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; bool check(string s1,string s2){ int l=0,r=0; bool flag=false; int cnt=0; while(l<s1.size() || r<s2.size()){ if(s1.size()==l || s2.size()==r)return false; if(s1[l]!=s2[r])return false; if(s1[l]=='"'){ l++; string ss=""; while(s1[l]!='"'){ ss+=s1[l]; l++; } l++; r++; string ss2=""; while(s2[r]!='"'){ ss2+=s2[r]; r++; } r++; if(ss!=ss2){ cnt++; } }else{ l++; r++; } } return (l==s1.size() && r==s2.size() && cnt<=1); } int main(void){ while(1){ string s1,s2; cin >> s1; if(s1==".")break; cin >> s2; if(s1==s2){ printf("IDENTICAL\n"); }else{ printf("%s\n",check(s1,s2)?"CLOSE":"DIFFERENT"); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 333; const long long linf = 1e18 + inf; const int N = 1e5 + 5; const int SQRT = 100; int n, q, ptr; char str[N]; string s[N]; long long ans[N]; int fin[N], fail[N], trie[N][26]; vector<int> v[N], ___ends[N]; int x[N], y[N], k[N]; vector<int> ord; vector<int> qu[N]; void build() { for (int i = 1; i <= n; i++) { int x = 0; for (int j = 0; j < s[i].size(); j++) { int u = s[i][j]; if (!trie[x][u]) { trie[x][u] = ++ptr; } x = trie[x][u]; } fin[i] = x; ___ends[x].push_back(i); } ord.push_back(0); for (int it = 0; it < ord.size(); it++) { int x = ord[it]; for (int i = 0; i < 26; i++) { if (trie[x][i]) { int u = trie[x][i]; int q = fail[x]; while (q and !trie[q][i]) { q = fail[q]; } if (q != x and trie[q][i]) q = trie[q][i]; v[q].push_back(u); fail[u] = q; ord.push_back(u); } } } } vector<pair<pair<int, int>, int> > sq[N]; void get(int l, int r, int x, int id) { int q = 0; for (int i = 0; i < s[x].size(); i++) { int u = s[x][i]; while (q and !trie[q][u]) { q = fail[q]; } if (trie[q][u]) q = trie[q][u]; sq[q].push_back(make_pair(pair<int, int>(l, r), id)); } } int fen[N]; void up(int x, int k) { for (; x < N; x += x & -x) fen[x] += k; } int get(int x, int y) { int res = 0; for (; y; y -= y & -y) res += fen[y]; for (x--; x; x -= x & -x) res -= fen[x]; return res; } void dfs(int x) { for (__typeof((___ends[x]).begin()) it = (___ends[x]).begin(); it != (___ends[x]).end(); it++) { int u = *it; up(u, 1); } for (__typeof((sq[x]).begin()) it = (sq[x]).begin(); it != (sq[x]).end(); it++) { int l = it->first.first; int r = it->first.second; int i = it->second; ans[i] += get(l, r); } for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) { int u = *it; dfs(u); } for (__typeof((___ends[x]).begin()) it = (___ends[x]).begin(); it != (___ends[x]).end(); it++) { int u = *it; up(u, -1); } } long long dp[N], cnt[N]; void solve(int x) { memset(dp, 0, sizeof(dp)); memset(cnt, 0, sizeof(cnt)); int q = 0; for (int i = 0; i < s[x].size(); i++) { int u = s[x][i]; while (q and !trie[q][u]) { q = fail[q]; } if (trie[q][u]) q = trie[q][u]; cnt[q]++; } for (int i = (int)ord.size() - 1; i >= 0; i--) { int x = ord[i]; cnt[fail[x]] += cnt[x]; } for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1] + cnt[fin[i]]; } for (__typeof((qu[x]).begin()) it = (qu[x]).begin(); it != (qu[x]).end(); it++) { int i = *it; int l = ::x[i]; int r = ::y[i]; ans[i] = dp[r] - dp[l - 1]; } } int main() { scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%s", str); s[i] = str; for (int j = 0; j < s[i].size(); j++) s[i][j] -= 'a'; } build(); for (int i = 1; i <= q; i++) { scanf("%d %d %d", x + i, y + i, k + i); if (s[k[i]].size() <= SQRT) { get(x[i], y[i], k[i], i); } else { qu[k[i]].push_back(i); } } dfs(0); for (int i = 1; i <= n; i++) { if (qu[i].size()) { solve(i); } } for (int i = 1; i <= q; i++) { printf("%lld\n", ans[i]); } return 0; }
6
#include<bits/stdc++.h> using namespace std; class range { private: struct Iterator { int val; int operator*() {return val;} bool operator!=(Iterator &itr) {return val < itr.val;} void operator++() {++val;} }; Iterator i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} Iterator &begin() {return i;} Iterator &end() {return n;} }; template<class T> T at(vector<T> v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];} int main() { int n, res = 0; cin >> n; for (int i : range(n)) { long long a; cin >> a; bool ok = false; for (long long x = 1, y = 1e9; x < y; ++x) { y = (a - x) / (2 * x + 1); if (2 * x * y + x + y == a) { ok = true; break; } } if (!ok) ++res; } cout << res << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int upper[n], lower[n], u = 0, l = 0, ans = 0; for (int i = 0; i < n; i++) { cin >> upper[i] >> lower[i]; u += upper[i]; l += lower[i]; } if (u % 2 == 0 && l % 2 == 0) cout << "0"; else { for (int i = 0; i < n; i++) { if (upper[i] % 2 != 0 && lower[i] % 2 == 0 || upper[i] % 2 == 0 && lower[i] % 2 != 0) { u -= upper[i]; u += lower[i]; l -= lower[i]; l += upper[i]; ans++; if (u % 2 == 0 && l % 2 == 0) { cout << ans; return 0; } } } cout << "-1"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m, s, t; int co[501], si[501], L[501], R[501]; long long dis[501][501]; short f[501][501][501], g[501][501]; vector<int> zi[501]; vector<pair<int, int> > a[501]; void sou(int x, int y, int id) { int i; for (i = 0; i < a[x].size(); i++) { pair<int, int> A = a[x][i]; if (A.first == y) continue; dis[id][A.first] = dis[id][x] + A.second; sou(A.first, x, id); } } void Dfs(int x, int y) { int i, j, k, u, r; zi[x].push_back(x); for (i = 0; i < a[x].size(); i++) { pair<int, int> A = a[x][i]; if (A.first == y) continue; Dfs(A.first, x); for (j = 0; j < zi[A.first].size(); j++) zi[x].push_back(zi[A.first][j]); } for (j = 1; j <= n; j++) { if (dis[x][j] > m) continue; si[x] = 1; f[x][j][j == x] = j == x && !co[x]; for (i = 0; i < a[x].size(); i++) { pair<int, int> A = a[x][i]; int w = A.first; if (w == y) continue; int h[501]; memset(h, 120, sizeof(h)); for (u = si[x]; u >= 0; u--) if (f[x][j][u] < 501) for (r = si[w]; r >= 0; r--) { short zhi = f[w][j][r]; if (dis[j][w] > dis[j][x]) zhi = min(zhi, g[w][r]); if (zhi < 501) h[u + r] = min(h[u + r], f[x][j][u] + zhi); } si[x] += si[w]; for (k = 0; k <= si[x]; k++) f[x][j][k] = h[k]; } } for (j = 0; j < zi[x].size(); j++) { int A = zi[x][j]; for (i = 0; i <= si[x]; i++) g[x][i] = min(g[x][i], f[x][A][i]); } } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%d", &co[i]), s += co[i]; for (i = 1; i <= n - 1; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); a[x].push_back(make_pair(y, z)); a[y].push_back(make_pair(x, z)); } for (i = 1; i <= n; i++) sou(i, 0, i); memset(f, 120, sizeof(f)); memset(g, 120, sizeof(g)); Dfs(1, 0); short an = 501; for (i = 1; i <= n; i++) for (j = 0; j <= s; j++) an = min(an, f[1][i][j]); printf("%d\n", an == 501 ? -1 : an); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 2020; int n, k, c[N][N] = {}, d[N] = {}; int main() { scanf("%d%d", &n, &k); long long sum = 0; for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) scanf("%d", &c[i][j]); if (k == 1 || k == n - 1) { for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) if (c[i][j] != -1) sum += c[i][j] + c[i][j]; printf("%I64d\n", sum / n); } else { for (int i = 1; i <= n; ++i) for (int j = 1; j < i; ++j) c[i][j] = c[j][i]; for (int i = 1; i <= n; ++i) c[i][i] = -1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) d[i] += c[i][j] != -1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (c[i][j] != -1) sum += (long long)c[i][j] * (d[i] - 1); printf("%I64d\n", sum / (n * (n - 1) / 2)); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, i, s1 = 10e10, s2 = 10e10, ans = 0; cin >> n; long long int a[n], b[n]; for (i = 0; i < n; i++) { cin >> a[i]; s1 = min(s1, a[i]); } for (i = 0; i < n; i++) { cin >> b[i]; s2 = min(s2, b[i]); } for (i = 0; i < n; i++) { if (a[i] > s1 && b[i] > s2) { long long int x = min(a[i] - s1, b[i] - s2); ans += x; a[i] -= x; b[i] -= x; } if (a[i] > s1) ans = ans + a[i] - s1; if (b[i] > s2) ans = ans + b[i] - s2; } cout << ans << endl; } }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = -1; const long long INF = (long long)1e18 + 50; map<long long, int> F; void fact(long long n) { F.clear(); for (long long p = 2; p * p <= n; p++) { while (n % p == 0) F[p]++, n /= p; } if (n > 1) F[n]++; } long long countp(long long n, long long p) { long long ans = 0; while (n >= p) ans += n / p, n /= p; return ans; } long long n, b; int main() { while (scanf("%lld %lld", &n, &b) >= 1) { fact(b); long long ans = INF; for (auto t : F) { long long p = t.first, e = t.second; long long c = countp(n, p); ans = min(ans, c / e); } printf("%lld\n", ans); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 20; template <typename T> struct Treap { T key; int f, maxf; int height, s; int addk, addf; Treap *left, *right, *parent; Treap(T a, int b = 0, Treap *parent = 0) : key(a), f(b), parent(parent) { height = rand(); maxf = f, s = 1; addk = addf = 0; left = right = 0; } void push() { if (!addk && !addf) { return; } } void maintain() { maxf = f, s = 1; if (left) { maxf = std::max(maxf, left->maxf); s += left->s; } if (right) { maxf = std::max(maxf, right->maxf); s += right->s; } } }; template <typename T> Treap<T> *merge(Treap<T> *x, Treap<T> *y) { if (!x) return y; if (!y) return x; if (x->height < y->height) { x->push(); x->right = merge(x->right, y); if (x->right) x->right->parent = x; x->maintain(); return x; } else { y->push(); y->left = merge(x, y->left); if (y->left) y->left->parent = y; y->maintain(); return y; } } template <typename T> Treap<T> *get_left(Treap<T> *x) { if (!x) { return x; } if (!x->left) { return x; } x->push(); return get_left(x->left); } template <typename T> Treap<T> *get_right(Treap<T> *x) { if (!x) { return x; } if (!x->right) { return x; } x->push(); return get_right(x->right); } template <typename T> Treap<T> *prev(Treap<T> *cur) { cur->push(); if (cur->left) { return get_right(cur->left); } else { Treap<T> *pa = cur->parent, *prev = cur; while (pa) { if (prev == pa->right) break; else prev = pa, pa = pa->parent; } return pa; } } template <typename T> Treap<T> *next(Treap<T> *cur) { cur->push(); if (cur->right) { return get_left(cur->right); } else { Treap<T> *pa = cur->parent, *prev = cur; while (pa) { if (prev == pa->left) break; else prev = pa, pa = pa->parent; } return pa; } } template <typename T> void split(Treap<T> *t, Treap<T> *&l, Treap<T> *&r, T x) { if (!t) { l = r = 0; return; } t->push(); if (t->key >= x) { split(t->left, l, t->left, x); if (t->left) t->left->parent = t; if (l) l->parent = 0; r = t; } else { split(t->right, t->right, r, x); if (t->right) t->right->parent = t; if (r) r->parent = 0; l = t; } t->maintain(); } template <typename T> Treap<T> *access(Treap<T> *r, T key) { if (!r) return r; if (r->key == key) return r; r->push(); if (key < r->key) return access(r->left, key); else return access(r->right, key); } template <typename T> void insert(Treap<T> *&r, T key, int f) { Treap<T> *ne = new Treap<T>(key, f); Treap<T> *t1, *t2; split(r, t1, t2, key); r = merge(t1, ne); r = merge(r, t2); } template <typename T> Treap<T> *remove(Treap<T> *&r, T key) { Treap<T> *x = access(r, key); if (x) { x->push(); Treap<T> *xx = merge(x->left, x->right); if (x->parent == NULL) { r = xx; if (r) r->parent = 0; } else { if (x == x->parent->left) x->parent->left = xx; else x->parent->right = xx; if (xx) xx->parent = x->parent; } Treap<T> *tmp = x->parent; if (xx) xx->maintain(); while (tmp) tmp->maintain(), tmp = tmp->parent; } return x; } template <typename T> int count(Treap<T> *root, T l, T r) { int ans = 0; while (root) { root->push(); if (root->key >= l && root->key <= r) break; else if (root->key < l) root = root->right; else root = root->left; } if (root) { root->push(); ++ans; Treap<T> *z1 = root->left, *z2 = root->right; while (z1) { z1->push(); if (z1->key >= l) { ++ans; if (z1->right) z1->right->push(), ans += z1->right->s; z1 = z1->left; } else z1 = z1->right; } while (z2) { z2->push(); if (z2->key <= r) { ++ans; if (z2->left) z2->left->push(), ans += z2->left->s; z2 = z2->right; } else z2 = z2->left; } } return ans; } template <typename T> int findAssoMax(Treap<T> *root, T l, T r) { int ret = INT_MIN; while (root) { root->push(); if (root->key >= l && root->key <= r) break; else if (root->key < l) root = root->right; else root = root->left; } if (root) { root->push(); ret = std::max(ret, root->f); Treap<T> *z1 = root->left, *z2 = root->right; while (z1) { z1->push(); if (z1->key >= l) { ret = std::max(ret, z1->f); if (z1->right) { z1->right->push(); ret = std::max(ret, z1->right->maxf); } z1 = z1->left; } else z1 = z1->right; } while (z2) { z2->push(); if (z2->key <= r) { ret = std::max(ret, z2->f); if (z2->left) { z2->left->push(); ret = std::max(ret, z2->left->maxf); } z2 = z2->right; } else z2 = z2->left; } } return ret; } template <typename T> void dfs(Treap<T> *v, int depth) { if (!v) return; dfs(v->left, depth + 1); for (int i = 0; i < depth; i++) putchar(' '); printf("key=%d, s = %d addmask=%d\n", v->key, v->s, v->addk); dfs(v->right, depth + 1); } template <typename T> void print(void *p) { dfs((Treap<T> *)p, 0); } int num, k, q; struct peo { int r, ag, idx, size; void rr() { scanf("%d%d", &r, &ag); } void print() { printf("r = %d, ag = %d, idx = %d, size = %d\n", r, ag, idx, size); } }; int r[MAX], ag[MAX]; peo ps[MAX]; map<int, vector<peo> > mm; Treap<pair<int, int> > *root; int sz[MAX], pos[MAX], ans[MAX]; struct as { int left, right, idx; as(int a = 0, int b = 0, int c = 0) : left(a), right(b), idx(c) {} }; map<int, vector<as> > rec; int main() { scanf("%d%d", &num, &k); for (int i = 1; i <= num; i++) { scanf("%d", &r[i]); } for (int i = 1; i <= num; i++) { scanf("%d", &ag[i]); } for (int i = 1; i <= num; i++) { ps[i].r = r[i], ps[i].ag = ag[i], ps[i].idx = i; } for (int i = 1; i <= num; i++) { mm[ps[i].r].push_back(ps[i]); } root = 0; for (auto &ele : mm) { auto &ve = ele.second; for (int i = 0; i < ve.size(); i++) { auto &pe = ve[i]; insert(root, {pe.ag, pe.idx}, 0); } for (int i = 0; i < ve.size(); i++) { auto &pe = ve[i]; remove(root, {pe.ag, pe.idx}); int idx = pe.idx; int left = pe.ag - k, right = pe.ag + k; ps[idx].size = count(root, {left, INT_MIN}, {right, INT_MAX}) + 1; insert(root, {pe.ag, pe.idx}, 0); } } scanf("%d", &q); for (int i = 1; i <= q; i++) { int x, y; scanf("%d%d", &x, &y); int df = abs(ps[x].ag - ps[y].ag); if (df > 2 * k) ans[i] = -1; else { int llimit = max(ps[x].ag, ps[y].ag) - k, rlimit = min(ps[x].ag, ps[y].ag) + k; rec[max(ps[x].r, ps[y].r)].push_back(as(llimit, rlimit, i)); } } sort(ps + 1, ps + num + 1, [](const peo &a, const peo &b) { return a.r < b.r; }); int idx = num; root = 0; for (auto ite = rec.rbegin(); ite != rec.rend(); ++ite) { auto &ele = *ite; int thres = ele.first; while (idx >= 1 && ps[idx].r >= thres) { insert(root, {ps[idx].ag, ps[idx].idx}, ps[idx].size); idx--; } auto &ve = ele.second; for (int i = 0; i < ve.size(); i++) { as &que = ve[i]; int left = que.left, right = que.right, idx = que.idx; int tmp = findAssoMax(root, {left, INT_MIN}, {right, INT_MAX}); if (tmp != INT_MIN) ans[idx] = tmp; else ans[idx] = -1; } } for (int i = 1; i <= q; i++) printf("%d\n", ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; pair<int, int> comb(pair<int, int> a, pair<int, int> b) { pair<int, int> res; if (a.first < b.first) res.first = b.first; else res.first = a.first; if (a.second > b.second) res.second = b.second; else res.second = a.second; if (res.first <= res.second) return res; else return pair<int, int>(-1, -1); } int main() { int n, c; cin >> c >> n; int** li; li = new int*[2]; li[0] = new int[500000]; li[1] = new int[500000]; int l[2]; cin >> l[0]; for (int i = 0; i < l[0]; i++) { int x; cin >> li[0][i]; } int index = 1; queue<pair<int, int> > qu; while (!qu.empty()) qu.pop(); qu.push(pair<int, int>(0, n - 1)); for (int i = 1; i < c; i++) { cin >> l[index]; for (int j = 0; j < l[index]; j++) { int x; scanf(" %d", &(li[index][j])); } int left, right, check; int le = l[index]; if (l[1 - index] < le) le = l[1 - index]; check = 0; for (int j = 0; j < le; j++) if (li[index][j] != li[1 - index][j]) { left = (n - li[1 - index][j] + 1) % n; right = n - li[index][j]; check = 1; break; } if (check == 0) if (l[index] >= l[1 - index]) { left = 0; right = n - 1; } else { cout << -1 << endl; return 0; } int tn = qu.size(); if (tn == 0) { cout << -1 << endl; return 0; } while (tn > 0) { tn--; pair<int, int> tch, res; tch = qu.front(); qu.pop(); if (left <= right) { res = comb(tch, pair<int, int>(left, right)); if (res.first != -1) { qu.push(res); } } else { res = comb(tch, pair<int, int>(left, n - 1)); if (res.first != -1) { qu.push(res); } res = comb(tch, pair<int, int>(0, right)); if (res.first != -1) { qu.push(res); } } } index = 1 - index; } if (qu.size() == 0) cout << -1 << endl; else cout << qu.front().first << endl; return 0; }
4
#include <iostream> #include <iomanip> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <string> #define shosu(x) fixed<<setprecision(x) using namespace std; typedef pair<int,int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,P> pip; typedef long long ll; const int inf=(1<<31)-1; const double pi=acos(-1); int a; vi n(5); char t; int main(){ while(cin>>a){ vi vec,tmp; n[0]=a; vec.push_back(a); for(int i=1;i<5;i++){ cin>>t>>n[i]; bool used=1; for(int j=0;j<vec.size();j++){ if(n[i]==vec[j]) used=0; } if(used) vec.push_back(n[i]); else tmp.push_back(n[i]); } if(vec.size()==4){ cout<<"one pair"<<endl;; continue; } if(vec.size()==3){ if(tmp[0]==tmp[1]) cout<<"three card"<<endl; else cout<<"two pair"<<endl; continue; } if(vec.size()==2){ if(tmp[0]==tmp[1]&&tmp[1]==tmp[2]) cout<<"four card"<<endl; else cout<<"full house"<<endl; continue; } sort(n.begin(),n.end()); int tmpn=0; for(int i=1;i<4;i++){ if(n[i]+1==n[i+1]) tmpn++; } if((n[0]==1&&n[1]==10)||n[0]+1==n[1]) tmpn++; if(tmpn==4){ cout<<"straight"<<endl; continue; } cout<<"null"<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int c[100100], visi[100100]; set<int> gr[100100]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 2; ++i) { int x, y, z; cin >> x >> y >> z; gr[x].insert(y); gr[x].insert(z); gr[y].insert(x); gr[y].insert(z); gr[z].insert(x); gr[z].insert(y); c[x]++; c[y]++; c[z]++; } vector<int> ans; int sta; int x, y; for (int i = 1; i <= n; ++i) { if (c[i] == 1) { sta = i; int mi = 10; for (int l : gr[sta]) { if (c[l] < mi) { y = x; x = l; mi = c[l]; } else y = l; } break; } } if (n == 4) { for (int l : gr[sta]) ans.push_back(l); ans.push_back(10 - (ans[0] + ans[1] + ans[2])); for (int l : ans) cout << l << " "; cout << endl; return 0; } for (int i = 0; i < n; ++i) { ans.push_back(sta); gr[x].erase(sta); gr[y].erase(sta); gr[y].erase(x); visi[sta] = 1; visi[x] = 1; sta = x; x = y; gr[y].erase(x); for (int l : gr[x]) { if (!visi[l] && gr[l].find(sta) != gr[l].end()) { y = l; } } } for (int l : ans) cout << l << " "; cout << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; #define F first #define S second typedef pair<int,int> P; typedef pair<P,P> PP; int n,m,INF=1<<29,dxx[4]={-1,0,1,0},dyy[4]={0,-1,0,1}; int dx[8]={-1,-1,0,1,2,2,0,1},dy[8]={0,1,-1,-1,0,1,2,2}; string s[55]; bool check(int x,int y) {return x>=0&&x<n&&y>=0&&y<m;} bool check(int x,int y,int xx,int yy) { if(x>=0&&x<n&&y>=0&&y<m) { if(s[x][y]=='*') return 0; if(x==xx&&(y==yy||y==yy+1)) return 0; if(x==xx+1&&(y==yy||y==yy+1)) return 0; return 1; } return 0; } int calc(int sx,int sy,int tx,int ty,int xx,int yy) { if(!check(sx,sy,xx,yy)||!check(tx,ty,xx,yy)) return INF; int d[n][m]; for(int i=0;i<n;i++)for(int j=0;j<m;j++) d[i][j]=INF; d[sx][sy]=0; queue<P> que; que.push(P(sx,sy)); while(!que.empty()) { P p=que.front();que.pop(); for(int i=0; i<4; i++) { int x=p.F+dxx[i],y=p.S+dyy[i]; if(!check(x,y,xx,yy)||d[x][y]<=d[p.F][p.S]+1) continue; d[x][y]=d[p.F][p.S]+1; que.push(P(x,y)); } } return d[tx][ty]; } int main() { while(cin >> n >> m && n) { for(int i=0; i<n; i++) cin >> s[i]; int d[n][m][4]; for(int i=0;i<n;i++)for(int j=0;j<m;j++)for(int k=0;k<4;k++)d[i][j][k]=INF; vector<P> v; P p=P(-1,-1); for(int i=0;i<n;i++)for(int j=0;j<m;j++) { if(s[i][j]=='.')v.push_back(P(i,j)); if(s[i][j]=='X'&&p.F==-1) p=P(i,j); } if(p==P(0,0)) { cout << 0 << endl; continue; } priority_queue<PP,vector<PP>,greater<PP> > que; for(int i=0; i<4; i++) { int b[2]={0,1}; do { int cost=calc(v[0].F,v[0].S,p.F+dx[i*2+b[0]],p.S+dy[i*2+b[0]],p.F,p.S); cost+=calc(v[1].F,v[1].S,p.F+dx[i*2+b[1]],p.S+dy[i*2+b[1]],p.F,p.S); d[p.F][p.S][i]=min(d[p.F][p.S][i],cost); } while(next_permutation(b,b+2)); que.push(PP(P(0,i),p)); } while(!que.empty()) { PP pp=que.top();que.pop(); int x=pp.S.F,y=pp.S.S,k=pp.F.S,c=pp.F.F; if(d[x][y][k]<c) continue; for(int i=0; i<4; i++) { int xx=x+dxx[i],yy=y+dyy[i]; int cost=INF; int b[2]={0,1}; do { int sum=calc(x+dx[k*2],y+dy[k*2],x+dx[i*2+b[0]],y+dy[i*2+b[0]],x,y); sum+=calc(x+dx[k*2+1],y+dy[k*2+1],x+dx[i*2+b[1]],y+dy[i*2+b[1]],x,y); cost=min(cost,sum); }while(next_permutation(b,b+2)); if(!check(xx,yy)||d[xx][yy][(i+2)%4]<=d[x][y][k]+cost+1) continue; d[xx][yy][(i+2)%4]=d[x][y][k]+cost+1; que.push(PP(P(d[xx][yy][(i+2)%4],(i+2)%4),P(xx,yy))); } } int ans=INF; for(int i=0; i<4; i++) ans=min(ans,d[0][0][i]); if(ans==INF) ans=-1; cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::istream& operator>>(std::istream& i, pair<T, U>& p) { i >> p.first >> p.second; return i; } template <typename T> std::istream& operator>>(std::istream& i, vector<T>& t) { for (auto& v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { o << p.first << ' ' << p.second; return o; } template <typename T> std::ostream& operator<<(std::ostream& o, const vector<T>& t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; } unsigned int logceil(int first) { return 8 * sizeof(int) - __builtin_clz(first); } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U>& p) const { return t(p.first) ^ (u(p.second) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; template <typename T> struct bounded_priority_queue { inline bounded_priority_queue(unsigned int X) : A(X), B(0), s(0) {} inline void push(unsigned int L, T V) { B = max(B, L); A[L].push(V); ++s; } inline const T& top() const { return A[B].front(); } inline void pop() { --s; A[B].pop(); while (B > 0 && A[B].empty()) --B; } inline bool empty() const { return A[B].empty(); } inline void clear() { s = B = 0; for (auto& a : A) a = queue<T>(); } inline unsigned int size() const { return s; } private: vector<queue<T>> A; unsigned int B; int s; }; struct UnionFind { UnionFind(size_t N) : P(N, -1), comp(N) {} int find(int first) { while (P[first] > 0) { first = P[first] - 1; } return first; } bool united(int first, int second) { return find(first) == find(second); } bool unite(int first, int second) { first = find(first); second = find(second); if (first == second) { return false; } --comp; if (P[first] > P[second]) { swap(first, second); } P[first] += P[second]; P[second] = first + 1; return true; } vector<int> P; size_t comp; }; unsigned long long mulull(unsigned long long a, unsigned long long z, unsigned long long mod) { __int128 result = ((__int128)a * (__int128)z) % (__int128)mod; return (unsigned long long)result; } pair<unsigned long long, unsigned long long> mulull(unsigned long long a, unsigned long long b) { __int128 result = (__int128)a * (__int128)b; return {(unsigned long long)(result >> 64), (unsigned long long)result}; } bool fractionGreaterOrEqual(long long a, long long b, long long c, long long d) { if (b < 0) { b = -b; a = -a; } if (d < 0) { d = -d; c = -c; } return a < 0 && c < 0 ? mulull(-a, d) <= mulull(-c, b) : (a >= 0) && (c < 0 || mulull(a, d) >= mulull(c, b)); } bool fractionGreaterOrEqual(pair<long long, long long> a, pair<long long, long long> b) { return fractionGreaterOrEqual(a.first, a.second, b.first, b.second); } bool fractionGreaterOrEqual(double a, double b, double c, double d) { return a / b >= c / d; } namespace Envelope { template <typename T> struct Line { T m, b; int id; mutable function<const Line<T>*()> succ; bool operator<(const Line& rhs) const { if (rhs.b != numeric_limits<T>::min()) return m < rhs.m; const Line* s = succ(); if (!s) return 0; return b - s->b < (s->m - m) * rhs.m; } }; template <typename T> struct Upper : public multiset<Envelope::Line<T>> { using parent = multiset<Line<T>>; bool bad(typename parent::iterator second) { auto z = next(second); if (second == parent::begin()) { return z != parent::end() && second->m == z->m && second->b <= z->b; } else { auto first = prev(second); if (z == parent::end()) { return second->m == first->m && second->b <= first->b; } else { return fractionGreaterOrEqual(first->b - second->b, second->m - first->m, second->b - z->b, z->m - second->m); } } } void insert_line(T m, T b, int i = 0) { auto second = parent::insert({m, b, i}); second->succ = [=] { return next(second) == parent::end() ? 0 : &*next(second); }; if (bad(second)) { parent::erase(second); return; } while (next(second) != parent::end() && bad(next(second))) parent::erase(next(second)); while (second != parent::begin() && bad(prev(second))) parent::erase(prev(second)); } T operator[](T first) const { auto l = parent::lower_bound({first, numeric_limits<T>::min()}); return l == parent::end() ? numeric_limits<T>::min() : l->m * first + l->b; } int best(T first) const { auto l = parent::lower_bound({first, numeric_limits<T>::min()}); return l == parent::end() ? -1 : l->id; } }; template <typename T> struct Lower : public Upper<T> { void insert_line(T m, T b, int i = 0) { Upper<T>::insert_line(-m, -b, i); } T operator[](T first) const { return -Upper<T>::operator[](first); } }; } // namespace Envelope namespace LinearEnvelope { template <typename T> struct Line { T m, b; int id; }; template <typename T> struct Upper : public vector<Line<T>> { using parent = vector<Line<T>>; using parent::at; using parent::back; using parent::size; T t; int i; Upper() : t(0), i(0) {} void insert_line(T m, T b, int i = 0) { assert(t == 0); while (size() > 0 && ((back().b < b) || (back().b == b && back().m < m))) parent::pop_back(); while (size() >= 2 && fractionGreaterOrEqual(at(size() - 2).b - back().b, back().m - at(size() - 2).m, back().b - b, m - back().m)) parent::pop_back(); parent::push_back({m, b, i}); } pair<T, int> advance(T first) { assert(first >= 0); t += first; while (i + 1 < size() && at(i).m * t + at(i).b < at(i + 1).m * t + at(i + 1).b) ++i; return {at(i).m * t + at(i).b, at(i).id}; } }; }; // namespace LinearEnvelope struct Sieve : public std::vector<bool> { explicit Sieve(unsigned int n) : vector<bool>(n + 1, true), n(n) { at(0) = false; if (n != 0) at(1) = false; for (unsigned int i = 2; i * i <= n; ++i) { if (at(i)) for (int j = i * i; j <= n; j += i) (*this)[j] = false; } } vector<int> primes() const { vector<int> ans; for (int i = 2; i <= n; ++i) if (at(i)) ans.push_back(i); return ans; } private: int n; }; struct Moebius : public std::vector<std::pair<int, int>> { explicit Moebius(unsigned int n) : Moebius(n, Sieve{n}.primes()) {} Moebius(unsigned int n, const vector<int>& primes) { search(-1, 1, 1, n, primes); std::sort(begin(), end()); } private: void search(int i, int first, int second, int n, const vector<int>& primes) { push_back({first, second}); while (++i < primes.size() && (long long)first * primes[i] <= n) search(i, first * primes[i], -second, n, primes); } }; struct LargePrimes : public vector<int> { LargePrimes() : vector<int>{ 7, 9, 21, 33, 87, 93, 97, 103, 123, 181, 207, 223, 241, 271, 289, 297, 321, 349, 363, 403, 409, 411, 427, 433, 439, 447, 453, 459, 483, 513, 531, 579, 607, 613, 637, 663, 711, 753, 787, 801, 829, 861, 871, 891, 901, 919, 931, 933, 993, 1011, 1021, 1053, 1087, 1099, 1137, 1161, 1203, 1213, 1237, 1263, 1269, 1273, 1279, 1311, 1329, 1333, 1351, 1371, 1393, 1413, 1447, 1449, 1491, 1501, 1531, 1537, 1539, 1581, 1617, 1621, 1633, 1647, 1663, 1677, 1699, 1759, 1773, 1789, 1791, 1801, 1803, 1819, 1857, 1887, 1917, 1927, 1957, 1963, 1969, 2043, 2089, 2103, 2139, 2149, 2161, 2173, 2187, 2193, 2233, 2239, 2277, 2307, 2359, 2361, 2431, 2449, 2457, 2499, 2571, 2581, 2607, 2631, 2637, 2649, 2667, 2727, 2791, 2803, 2821, 2823, 2827, 2907, 2937, 2989, 3009, 3013, 3051, 3057, 3097, 3111, 3133, 3153, 3157, 3163, 3211, 3241, 3247, 3253, 3267, 3271, 3273, 3283, 3309, 3337, 3351, 3357, 3373, 3379, 3397, 3469, 3471, 3513, 3519, 3559, 3577, 3579, 3601, 3621, 3643, 3651, 3663, 3679, 3709, 3747, 3751, 3769, 3777, 3787, 3793, 3843, 3853, 3871, 3889, 3891, 3909, 3919, 3931, 3951, 3957, 3967, 3987, 3999, 4023, 4059, 4099, 4119, 4123, 4207, 4233, 4249, 4251, 4263, 4321, 4329, 4381, 4389, 4437, 4449, 4459, 4497, 4507, 4519, 4539, 4567, 4569, 4581, 4609, 4611, 4627, 4633, 4647, 4693, 4699, 4717, 4771, 4777, 4783, 4791, 4807, 4839, 4843, 4849, 4857, 4867, 4869, 4891, 4893, 4897, 4927, 4933, 4977, 4981, 5001, 5029, 5053, 5067, 5103, 5133, 5187, 5197, 5203, 5233, 5277, 5287, 5299, 5317, 5329, 5341, 5353, 5361, 5403, 5407, 5437, 5443, 5449, 5451, 5469, 5491, 5527, 5541, 5547, 5583, 5631, 5647, 5683, 5731, 5737, 5751, 5763, 5791, 5823, 5827, 5833, 5847, 5863, 5899, 5907, 5947, 5953, 5959, 5971, 5973, 5991, 5997, 6019, 6027, 6037, 6039, 6061, 6093, 6099, 6127, 6129, 6177, 6193, 6211, 6223, 6229, 6307, 6313, 6331, 6349, 6379, 6393, 6417, 6421, 6457, 6459, 6477, 6541, 6571, 6577, 6583, 6607, 6621, 6661, 6663, 6697, 6717, 6751, 6781, 6837, 6867, 6901, 6957, 6961, 6967, 6981, 7003, 7023, 7027, 7089, 7117, 7137, 7147, 7159, 7191, 7209, 7237, 7243, 7257, 7279, 7321, 7347, 7383, 7389, 7417, 7429, 7447, 7453, 7467, 7479, 7497, 7513, 7521, 7531, 7537, 7557, 7633, 7647, 7651, 7653, 7681, 7707, 7719, 7741, 7759, 7773, 7803, 7819, 7837, 7873, 7887, 7909, 7923, 7927, 7929, 7941, 8041, 8083, 8089, 8101, 8109, 8127, 8173, 8181, 8223, 8257, 8259, 8271, 8277, 8311, 8313, 8343, 8349, 8397, 8403, 8419, 8431, 8439, 8487, 8511, 8557, 8593, 8617, 8637, 8649, 8661, 8671, 8679, 8719, 8727, 8761, 8773, 8791, 8797, 8803, 8811, 8829, 8853, 8899, 8917, 8937, 8967, 9009, 9013, 9063, 9081, 9093, 9099, 9123, 9133, 9163, 9183, 9211, 9223, 9259, 9277, 9279, 9289, 9301, 9321, 9331, 9363, 9399, 9403, 9421, 9441, 9457, 9469, 9487, 9519, 9529, 9531, 9541, 9559, 9561, 9567, 9573, 9579, 9597, 9601, 9609, 9631, 9651, 9667, 9679, 9711, 9733, 9739, 9757, 9789, 9831, 9859, 9867, 9961, 9999, 10029, 10051, 10069, 10101, 10153, 10173, 10189, 10197, 10233, 10243, 10251, 10267, 10281, 10303, 10321, 10327, 10351, 10357, 10381, 10449, 10467, 10483, 10503, 10513, 10549, 10593, 10597, 10611, 10633, 10659, 10699, 10707, 10723, 10747, 10749, 10761, 10773, 10777, 10801, 10833, 10903, 10953, 10969, 10971, 10981, 10987, 11007, 11011, 11071, 11083, 11091, 11107, 11109, 11137, 11149, 11161, 11223, 11239, 11253, 11269, 11277, 11283, 11289, 11301, 11317, 11329, 11371, 11377, 11391, 11421, 11479, 11487, 11497, 11517, 11533, 11539, 11559, 11583, 11601, 11619, 11631, 11659, 11673, 11679, 11707, 11763, 11767, 11769, 11773, 11799, 11811, 11821, 11823, 11847, 11869, 11967, 11983, 11989, 12019, 12037, 12121, 12157, 12177, 12187, 12217, 12219, 12231, 12241, 12253, 12297, 12309, 12333, 12337, 12339, 12361, 12367, 12399, 12411, 12459, 12463, 12483, 12501, 12513, 12523, 12547, 12553, 12577, 12609, 12631, 12649, 12679, 12681, 12703, 12723, 12733, 12747, 12753, 12759, 12799, 12847, 12877, 12891, 12901, 12903, 12927, 12939, 12963, 13011, 13017, 13029, 13039, 13081, 13087, 13089, 13101, 13107, 13137, 13141, 13143, 13153, 13191, 13207, 13213, 13237, 13323, 13369, 13419, 13437, 13447, 13477, 13519, 13537, 13561, 13563, 13587, 13591, 13723, 13741, 13759, 13789, 13801, 13857, 13881, 13897, 13921, 13923, 13939, 13999, 14011, 14073, 14077, 14083, 14097, 14121, 14131, 14143, 14157, 14161, 14167, 14181, 14193, 14209, 14241, 14259, 14271, 14293, 14361, 14391, 14397, 14437, 14529, 14551, 14581, 14583, 14599, 14607, 14611, 14643, 14727, 14761, 14773, 14779, 14791, 14823, 14853, 14881, 14889, 14901, 14931, 14937, 14997, 15031, 15043, 15057, 15063, 15069, 15097, 15103, 15109, 15117, 15141, 15147, 15187, 15199, 15217, 15231, 15253, 15271, 15279, 15283, 15307, 15339, 15349, 15351, 15363, 15369, 15409, 15427, 15447, 15477, 15487, 15553, 15579, 15591, 15649, 15651, 15673, 15693, 15703, 15717, 15727, 15747, 15799, 15811, 15823, 15859, 15889, 15963, 15981, 16009, 16021, 16029, 16047, 16063, 16089, 16093, 16107, 16153, 16167, 16191, 16201, 16231, 16233, 16261, 16267, 16273, 16317, 16323, 16411, 16419, 16431, 16489, 16491, 16513, 16539, 16617, 16621, 16623, 16627, 16651, 16659, 16671, 16711} { for (int& i : *this) i += 1000000000; } }; vector<int> SmallPrimes{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257}; bool isPrime(long long n) { for (int p : SmallPrimes) if (p < n && n % p == 0) return false; long long d = n - 1, r = 0; while (d % 2 == 0) { d /= 2; r += 1; } auto composite = [=](int a) { long long first = 1, p = a, e = d; while (e > 0) { if (e % 2 > 0) { first = mulull(first, p, n); } e /= 2; p = mulull(p, p, n); } if (first == 1 || first == n - 1) return false; for (int i = 1; i < r; ++i) { first = mulull(first, first, n); if (first == 1) return true; if (first == n - 1) return false; } return true; }; return !(composite(2)) && (n < 2047 || !composite(3)) && (n < 1373653 || !composite(5)) && (n < 25326001 || !composite(7)) && (n < 3215031751LL || !composite(11)) && (n < 2152302898747LL || !composite(13)) && (n < 3474749660383LL || !composite(17)) && (n < 341550071728321LL || (!composite(19) && !composite(23))) && (n < 3825123056546413051LL || (!composite(29) && !composite(31) && !composite(37))); } class TaskB { public: void solve(istream& cin, ostream& cout) { int N; cin >> N; vector<int> A(N); cin >> A; auto W = Sieve{55}.primes(); vector<int> P(59, 0); for (int i = 1; i < 59; ++i) { for (int w = 0; w < W.size(); ++w) { if (i % W[w] == 0) { P[i] |= (1 << w); } } } int infty = 1e9; vector2<std::pair<int, int>> D(N + 1, 1 << W.size(), {infty, -1}); D[0][0] = {0, -1}; for (int i = 0; i < N; ++i) { for (int m = 0; m < (1 << W.size()); ++m) { if (D[i][m].first == infty) continue; for (int z = 1; z < 59; ++z) { if ((m & P[z]) == 0) { D[i + 1][m | P[z]] = min(D[i + 1][m | P[z]], {D[i][m].first + abs(A[i] - z), z}); } } } } std::pair<int, int> lo{infty, 0}; for (int m = 0; m < (1 << W.size()); ++m) { lo = min(lo, {D[N][m].first, m}); } vector<int> B(N); int m = lo.second; for (int i = N; i > 0; --i) { B[i - 1] = D[i][m].second; m ^= P[D[i][m].second]; } cout << B; }; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); TaskB solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { set<int>x; for(int i=0;i<3;i++){ int y; cin>>y; x.insert(y); } cout<<x.size(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int z = n / 3; int x = n % 3; int ans1 = z; int ans2 = z; if (x == 1) ans1++; if (x == 2) ans2++; cout << ans1 << " " << ans2 << endl; } return 0; }
1
#include <bits/stdc++.h> const long long N = 2e5 + 1; using namespace std; clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } void init_code() {} signed main() { startTime = clock(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long T = 1; cin >> T; while (T--) { long long n; cin >> n; vector<long long> v(n); map<long long, long long> mp; for (long long i = 0; i < n; i++) { cin >> v[i]; mp[v[i]] = i; } if (mp.size() < n) { cout << "YES" << "\n"; continue; } vector<long long> temp = v; sort(temp.begin(), temp.end()); long long swaps = 0; for (long long i = 0; i < n; i++) { if (temp[i] != v[i]) { swaps++; long long pos = mp[temp[i]]; mp[temp[i]] = i; mp[v[i]] = pos; swap(v[i], v[pos]); } } if (swaps % 2) cout << "NO"; else cout << "YES"; cout << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; long long int calDis(long long int x1, long long int y1, long long int x2, long long int y2) { return abs(x1 - x2) + abs(y1 - y2); } void solve() { long long int x0, y0, ax, ay, bx, by; cin >> x0 >> y0 >> ax >> ay >> bx >> by; long long int xs, ys, t; cin >> xs >> ys >> t; vector<pair<long long int, long long int>> dataPoints; dataPoints.push_back(make_pair(x0, y0)); for (int i = 1;; ++i) { long long int tempx, tempy; if (dataPoints[i - 1].first < ((1LL << 62) - 1 - bx) / ax && dataPoints[i - 1].second < ((1LL << 62) - 1 - by) / ay) { tempx = dataPoints[i - 1].first * ax + bx; tempy = dataPoints[i - 1].second * ay + by; dataPoints.push_back(make_pair(tempx, tempy)); } else { break; } } long long int ans = 0; for (int i = 0; i < (long long int)dataPoints.size(); ++i) { for (int j = i; j < (long long int)dataPoints.size(); ++j) { long long int dis1 = calDis(dataPoints[i].first, dataPoints[i].second, dataPoints[j].first, dataPoints[j].second); long long int dis2 = calDis(dataPoints[i].first, dataPoints[i].second, xs, ys); if (dis1 <= t - dis2) { ans = ((ans) > (j - i + 1) ? (ans) : (j - i + 1)); } dis1 = calDis(dataPoints[i].first, dataPoints[i].second, dataPoints[j].first, dataPoints[j].second); dis2 = calDis(dataPoints[j].first, dataPoints[j].second, xs, ys); if (dis1 <= t - dis2) { ans = ((ans) > (j - i + 1) ? (ans) : (j - i + 1)); } } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); string H, A; int n; cin >> H >> A >> n; int Hc[101] = {}, Ac[101] = {}; while (n--) { int t, nr; char team, type; cin >> t >> team >> nr >> type; if (team == 'h') { if (Hc[nr] < 2) { Hc[nr] += (type == 'y' ? 1 : 2); if (Hc[nr] >= 2) cout << H << " " << nr << " " << t << "\n"; } } else { if (Ac[nr] < 2) { Ac[nr] += (type == 'y' ? 1 : 2); if (Ac[nr] >= 2) cout << A << " " << nr << " " << t << "\n"; } } } }
1
#include <bits/stdc++.h> using namespace std; using namespace std; int a[100000 + 5]; vector<int> v; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int inv = 0; int n; cin >> n; int a[100000 + 5]; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) if (a[i] > a[j]) ++inv; inv %= 2; int q; cin >> q; for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; inv += ((y - x) * (y - x + 1)) / 2; inv %= 2; cout << (inv % 2 ? "odd" : "even") << endl; } }
4
#include <cstdio> #include <vector> #include <algorithm> #include <iostream> using namespace std; unsigned long xor128() { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } static const int INF = 0x3f3f3f3f; struct node_t { node_t *l = NULL, *r = NULL; int val, first, ma; int pri; bool monotone; node_t(int val) : val(val), pri(xor128() % INF) { first = ma = val; monotone = true; } void reset() { first = ma = val; monotone = true; } void update() { first = (l ? l->first : val); int tmpma = val; bool tmpmonotone = true; if (l) { tmpma = max(tmpma, l->ma); if (!l->monotone || l->ma > val) tmpmonotone = false; } if (r) { tmpma = max(tmpma, r->ma); if (tmpmonotone && (!r->monotone || val > r->first)) tmpmonotone = false; } ma = tmpma; monotone = tmpmonotone; } void update2() { first = (l ? l->first : val); ma = (r ? r->ma : val); monotone = true; } }; inline node_t* merge(node_t *l, node_t *r) { if (!l) return r; if (!r) return l; if (l->pri > r->pri) { l->r = l->r ? merge(l->r, r) : r; l->update(); return l; } else { r->l = r->l ? merge(l, r->l) : l; r->update(); return r; } } inline node_t* merge2(node_t *l, node_t *r) { if (!l) return r; if (!r) return l; if (l->pri > r->pri) { l->r = l->r ? merge2(l->r, r) : r; l->update2(); return l; } else { r->l = r->l ? merge2(l, r->l) : l; r->update2(); return r; } } pair<node_t*, node_t*> split(node_t *tree, int lb) { if (!tree) return { NULL, NULL }; if (tree->ma < lb) return { NULL, tree }; if (tree->monotone) { if (lb < tree->first) return { tree, NULL }; else if (!tree->l) { if (!tree->r) return { NULL, tree }; auto sub = split(tree->r, lb); tree->r = NULL; tree->reset(); sub.second = merge(tree, sub.second); return sub; } else if (lb < tree->val) { auto sub = split(tree->l, lb); tree->l = NULL; tree->update(); sub.first = merge2(sub.first, tree); return sub; } else { auto sub = split(tree->r, lb); tree->r = NULL; tree->update(); sub.second = merge(tree, sub.second); return sub; } } else { node_t *left = tree->l, *right = tree->r; tree->l = tree->r = NULL; tree->update(); auto sub = split(left, lb); if (sub.first) { lb = sub.first->ma; } if (tree->val > lb) { sub.first = merge2(sub.first, tree); lb = tree->val; } else { sub.second = merge(sub.second, tree); } auto sub2 = split(right, lb); return { merge2(sub.first, sub2.first), merge(sub.second, sub2.second) }; } } /* void dfs(node_t* u) { if (u->l) dfs(u->l); cerr << u->val << ' '; if (u->r) dfs(u->r); } */ int main() { int n; scanf("%d", &n); vector<node_t> nodes; for (int i = 0; i < n; i ++) { int p; scanf("%d", &p); nodes.emplace_back(p); } node_t* tree = NULL; for (int i = 0; i < n; i ++) { tree = merge(tree, &nodes[i]); } int ans = 0; /* dfs(tree); cerr << endl; */ while (!tree->monotone) { ans ++; auto sub = split(tree, 0); tree = merge(sub.second, sub.first); /* dfs(tree); cerr << endl; */ } printf("%d\n", ans); return 0; }
0
#include<iostream> using namespace std; int main(){ ios::sync_with_stdio(false); int n,a,b,c,d;cin >> n >> a >> b >> c >> d; string s; cin >> s;s='?'+s; for(int i=a; i<c ;i++){ if(s[i]=='#' && s[i+1]=='#') return cout << "No\n",0; } for(int i=b; i<d ;i++){ if(s[i]=='#' && s[i+1]=='#') return cout << "No\n",0; } if(c<d) return cout << "Yes\n",0; for(int i=b-1; i<d ;i++){ if(s[i]=='.' && s[i+1]=='.' && s[i+2]=='.') return cout << "Yes\n",0; } return cout << "No\n",0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, n, y, z; scanf("%d", &n); int a[n], x[n + 2]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); if (n % 2 == 0) { j = n - 1; for (i = 0; i < n / 2; i++) { x[i] = a[j]; j = j - 2; } j++; for (i = i; i < n; i++) { x[i] = a[j]; j = j + 2; } x[n] = x[0]; x[n + 1] = x[1]; for (i = 0; i < n; i++) { if (x[i] + x[i + 2] <= x[i + 1]) { printf("NO"); return 0; } } printf("YES\n"); for (i = 0; i < n; i++) { printf("%d ", x[i]); } } else { j = n - 1; for (i = 0; i <= n / 2; i++) { x[i] = a[j]; j = j - 2; } j = 1; for (i = i; i < n; i++) { x[i] = a[j]; j = j + 2; } x[n] = x[0]; x[n + 1] = x[1]; for (i = 0; i < n; i++) { if (x[i] + x[i + 2] <= x[i + 1]) { printf("NO"); return 0; } } printf("YES\n"); for (i = 0; i < n; i++) { printf("%d ", x[i]); } } }
2
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; map<string,int>m; for(int i=0;i<n;i++) { string s; cin>>s; m[s]++; } cout<<"AC x "<<m["AC"]<<endl; cout<<"WA x "<<m["WA"]<<endl; cout<<"TLE x "<<m["TLE"]<<endl; cout<<"RE x "<<m["RE"]<<endl; }
0
#include <bits/stdc++.h> using namespace std; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; const long long MOD = 1e9 + 7; const int lim = 2e5 + 10; int h[lim]; int main() { int n, k; cin >> n >> k; memset(h, 0, sizeof(h)); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); h[a]++; } int ans = 0; int sum = 0; for (int i = lim - 1; i >= 0; --i) { if (i != lim - 1) { h[i] += h[i + 1]; } if (h[i] == n) break; sum += h[i]; if (sum == k) { sum = 0; ans++; } else if (sum > k) { ans++; sum = h[i]; } } if (sum > 0) { ans++; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 2005; const int inf = 0x3f3f3f3f; bool vis[maxn]; int n, m, s, t, x, y, z, f, dis[maxn], pre[maxn], last[maxn], flow[maxn], maxflow, mincost; struct Edge { int u, to, next, flow, dis; } edge[3000005]; int head[maxn], num_edge; queue<int> q; struct node { int s, t, c, id; } g[1005]; void add(int from, int to, int flow, int dis) { edge[++num_edge].next = head[from]; edge[num_edge].u = from; edge[num_edge].to = to; edge[num_edge].flow = flow; edge[num_edge].dis = dis; head[from] = num_edge; flow = 0; dis = -dis; edge[++num_edge].next = head[to]; edge[num_edge].u = to; edge[num_edge].to = from; edge[num_edge].flow = flow; edge[num_edge].dis = dis; head[to] = num_edge; } bool spfa(int s, int t) { memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow)); memset(vis, 0, sizeof(vis)); q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].next) { if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = dis[now] + edge[i].dis; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now], edge[i].flow); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1; } void MCMF() { while (spfa(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } } } bool cmp(node a, node b) { return a.s < b.s; } bool ans[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset(head, -1, sizeof(head)); num_edge = -1; int k; cin >> n >> k; s = 0, t = n * 2 + 2; int tmp = n * 2 + 1; add(tmp, t, k, 0); for (int i = 1; i <= n; i++) { cin >> g[i].s >> g[i].t >> g[i].c; g[i].t = g[i].s + g[i].t - 1; g[i].id = i; add(0, i, 1, 0); add(i, i + n, 1, -g[i].c); add(i + n, tmp, inf, 0); } sort(g + 1, g + 1 + n, cmp); for (int i = 1; i <= n - 1; i++) { add(g[i].id, g[i + 1].id, inf, 0); for (int j = i + 1; j <= n; j++) if (g[j].s > g[i].t) { add(g[i].id + n, g[j].id, inf, 0); break; } } MCMF(); for (int i = 0; i < num_edge; i += 2) { if (!edge[i].flow) { if (edge[i].u >= 1 && edge[i].u <= n) ans[edge[i].u] = 1; } } for (int i = 1; i <= n; i++) cout << ans[i] << ' '; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const double PI = acos(-1.); const double eps = 1e-9; char s[1000005]; char t[1000005]; int fs[11]; int ft[11]; int fx[11]; char y[1000005]; int main() { int i, j, k, _T; while (~scanf("%s%s", s, t)) { if (strcmp(s, "01") == 0 || strcmp(s, "10") == 0) { printf("0\n"); continue; } memset(fs, 0, sizeof fs); memset(ft, 0, sizeof ft); for (i = 0; s[i]; i++) fs[s[i] - '0']++; for (i = 0; t[i]; i++) ft[t[i] - '0']++; int ls = strlen(s); int lt = strlen(t); for (int len = lt; len <= ls; len++) { char x[15]; memset(fx, 0, sizeof fx); memset(x, 0, sizeof x); sprintf(x, "%d", len); int lx = strlen(x); for (i = 0; x[i]; i++) fx[x[i] - '0']++; int fail = 0; for (i = 0; i <= 9; i++) if (fs[i] < ft[i] + fx[i]) { fail = 1; } if (fail || ls - lx != len) continue; int flag = 0; for (i = 1; i <= 9; i++) flag |= fs[i] > fx[i] + ft[i]; if (t[0] == '0' && !flag) continue; if (!flag) { printf("%s", t); for (i = 0; i <= 9; i++) for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) putchar('0' + i); putchar('\n'); break; } int id = 0, fi = -1; for (i = 1; i <= 9; i++) if (fs[i] > fx[i] + ft[i]) { fi = i; break; } fs[fi]--; y[id++] = '0' + fi; for (i = 0; i <= 9; i++) { if (t[0] == '0' + i) { int big = 0; for (j = 1; j < lt; j++) { if (t[j] != t[0]) { if (t[j] > t[0]) big = 1; break; } } if (!big) { for (j = 0; j < lt; j++) y[id++] = t[j]; } for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) y[id++] = '0' + i; if (big) { for (j = 0; j < lt; j++) y[id++] = t[j]; } } else for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) y[id++] = '0' + i; } int ly = id; fs[fi]++; id = lt; for (i = 0; i <= 9; i++) for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) t[id++] = '0' + i; int uset = 0; for (i = 0; i < ly; i++) { if (y[i] != t[i]) { if (y[i] > t[i]) uset = 1; break; } } if (uset && t[0] != '0') { for (i = 0; i < ly; i++) putchar(t[i]); putchar('\n'); } else { for (i = 0; i < ly; i++) putchar(y[i]); putchar('\n'); } break; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); double w, h, alpha; double a, b, c, d, e, f; double x, y; int main() { ios::sync_with_stdio(0); cin >> w >> h >> alpha; if (w < h) swap(w, h); alpha = min(alpha, 180 - alpha); if (alpha == 0) { return printf("%.9f\n", 1.0 * w * h), 0; } double Sin = sin(alpha * pi / 180); double Cos = cos(alpha * pi / 180); double Tan = tan(alpha * pi / 180); double tmp = 1.0 + 1 / Cos; y = (h - Tan * w / tmp) / (tmp - Tan * Tan / tmp); x = h - tmp * y; if (tan((alpha * pi / 180) / 2) < h / w) printf("%.9f\n", (w * h - x * x / Tan - y * y * Tan)); else { printf("%.9f\n", (w * h - (w - h / Sin) * h)); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1001; int a[N]; int vis[N]; int n; struct edge { int from, to, w; bool operator<(const edge& e) const { return w > e.w; } }; vector<edge> adj[N]; priority_queue<edge> q; int MST() { int mstCost = 0; int edges = 0; while (!q.empty()) { edge e = q.top(); q.pop(); if (vis[e.to]) continue; vis[e.to] = 1; mstCost += e.w; ++edges; for (int j = 0; j < adj[e.to].size(); j++) { edge ne = adj[e.to][j]; if (!vis[ne.to]) q.push(ne); } } if (edges != n - 1) return -1; return mstCost; } int main() { int m; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 0; i < m; i++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); --u, --v; adj[u].push_back(edge{u, v, w}); q.push(edge{u, v, w}); } printf("%d\n", MST()); }
2
#include<bits/stdc++.h> using namespace std; #define inf INT_MAX #define INF LLONG_MAX #define ll long long #define ull unsigned long long #define M (int)(1e9+7) #define P pair<int,int> #define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++) #define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--) #define rep(i,n) FOR(i,0,n) #define rrep(i,n) RFOR(i,n,0) #define all(a) a.begin(),a.end() const int vx[4] = {0,1,0,-1}; const int vy[4] = {1,0,-1,0}; #define F first #define S second #define PB push_back #define EB emplace_back #define int ll #define vi vector<int> #define IP pair<int,P> #define PI pair<P,int> #define PP pair<P,P> #define Yes(f){cout<<(f?"Yes":"No")<<endl;} #define YES(f){cout<<(f?"YES":"NO")<<endl;} int Madd(int x,int y) {return (x+y)%M;} int Msub(int x,int y) {return (x-y+M)%M;} int Mmul(int x,int y) {return (x*y)%M;} signed main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20); int x; cin>>x; bitset<32> bit(x); cout<<bit<<endl; cout<<~bit<<endl; cout<<(bit<<1)<<endl; cout<<(bit>>1)<<endl; }
0
#include <bits/stdc++.h> using namespace std; vector<set<pair<int,int>>> X(100000); vector<int> Bl(100000,5); void dfs(int x,int y){ auto it=X[x].begin(); while(it!=X[x].end()){ int a=(*it).first; int c=(*it).second; it++; if(a==y) continue; Bl[a]=(Bl[x]+c)%2; dfs(a,x); } return; } int main(){ int N; cin>>N; int a,b,c; for(int i=0;i<N-1;i++){ cin>>a>>b>>c; c%=2; a--; b--; X[a].insert({b,c}); X[b].insert({a,c}); } Bl[0]=0; dfs(0,512411); for(int i=0;i<N;i++){ cout<<Bl[i]<<endl; } }
0
#include <bits/stdc++.h> using namespace std; map<vector<string>, bool> state; vector<string> matr; void printm(int n) { for (int i = 0; i <= n - 1; i++) { for (int j = 0; j <= n - 1; j++) cout << matr[i][j] << ' '; cout << '\n'; } cout << '\n'; } void compute_states(int li) { for (int q = 0; q <= li + 1; q++) { vector<string> aux; state[matr] = 1; for (int j = li; j >= 0; j--) { string foo; for (int i = 0; i <= li; i++) { foo.push_back(matr[i][j]); } aux.push_back(foo); } matr = aux; } } int main() { int n; cin >> n; for (int i = 0; i <= n - 1; i++) { string x; cin >> x; matr.push_back(x); } compute_states(n - 1); for (int i = 0; i <= n - 1; i++) reverse(matr[i].begin(), matr[i].end()); compute_states(n - 1); reverse(matr.begin(), matr.end()); compute_states(n - 1); for (int i = 0; i <= n - 1; i++) reverse(matr[i].begin(), matr[i].end()); compute_states(n - 1); vector<string> query; for (int i = 0; i <= n - 1; i++) { string x; cin >> x; query.push_back(x); } cout << ((state[query]) ? "Yes" : "No"); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<int> v[101]; int n, m, a, b, len; char dp[33][101][101], c; int g[101][101]; inline char winner(int cost, int x, int y) { if (isalpha(dp[cost][x][y])) return dp[cost][x][y]; else { for (int i = 0; i < v[x].size(); ++i) { if (g[x][v[x][i]] >= cost && winner(g[x][v[x][i]], y, v[x][i]) == 'B') return dp[cost][x][y] = 'A'; } } return dp[cost][x][y] = 'B'; } int main() { cin >> n >> m; for (int i = 1; i <= m; ++i) { scanf("%d%d %c", &a, &b, &c); v[a].push_back(b); g[a][b] = c - 'a'; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { winner(0, i, j); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i == j) printf("B"); else printf("%c", dp[0][i][j]); } cout << endl; } return 0; }
2
# include <cstdio> # include <iostream> # include <string> using namespace std; int main () { string s; int n,a=0,flag=1; cin>>s; n=s.length(); for (int i=0;i<n;++i) { if (isupper(s[i])) a++; } if (s[0]=='A'&&s[1]!='C'&&s[n-1]!='C'&&a==2) printf ("AC\n"); else printf ("WA\n"); }
0
#include <bits/stdc++.h> bool a[100001]; class op { public: int first, delta; inline op(int first, int delta) : first(first), delta(delta) { a[first] ^= 1, a[first + delta] ^= 1, a[first + (delta << 1)] ^= 1; } }; std::vector<op> ans; int main() { std::ios_base::sync_with_stdio(0), std::cin.tie(0); int n; std::cin >> n; for (int i = 1; i <= n; ++i) std::cin >> a[i]; for (int i = 1; i + 2 <= n; ++i) if (a[i]) ans.emplace_back(i, i + 10 < n ? a[i + 1] ? a[i + 2] ? 1 : a[i + 3] ? 3 + a[i + 4] : 4 | !a[i + 4] : 2 | !a[i + 2] : 1); if (a[n - 1]) if (n >= 8) ans.emplace_back(n - 5, 1), ans.emplace_back(n - 7, 2), ans.emplace_back(n - 7, 3); else do { std::cout << "NO\n"; return 0; } while (0); if (a[n]) if (n >= 7) ans.emplace_back(n - 4, 1), ans.emplace_back(n - 6, 2), ans.emplace_back(n - 6, 3); else do { std::cout << "NO\n"; return 0; } while (0); std::cout << "YES\n" << ans.size() << '\n'; for (const op& i : ans) std::cout << i.first << ' ' << i.first + i.delta << ' ' << i.first + (i.delta << 1) << '\n'; }
5
#include<iostream> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<vector> #include<cmath> #include<cstdio> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define mp make_pair #define it ::iterator #define all(in) in.begin(),in.end() const double PI=acos(-1); const double ESP=1e-10; using namespace std; int main(){ int a,b; while(cin>>a>>b,a||b){ vector<vector<double> >in(a,vector<double>(a)); rep(i,a)rep(j,a)cin>>in[i][j]; vector<vector<double> >out(b,vector<double>(a)); rep(i,a)out[0][i]=1.0; rep(i,b-1){ rep(j,a){ double max1=0; rep(k,a)if(max1<out[i][k]*in[k][j])max1=out[i][k]*in[k][j]; out[i+1][j]=max1; } } double max1=0; rep(i,a)if(max1<out[b-1][i])max1=out[b-1][i]; //double tmp=max1*100-(int)(max1*100); //printf("%.3f\n",tmp); //if(tmp-0.50>=-ESP)max1+=0.01; printf("%.2f\n",max1); } }
0
#include <cstdio> int N, M, a, b, l, r, z = 1 << 29, q[5009], c[5009], t[5009], s[5009], d[5009], g[5009][5009], v[5009][5009]; int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; i++) scanf("%d%d", &c[i], &t[i]); for (int i = 0; i < M; i++) scanf("%d%d", &a, &b), a--, b--, g[a][s[a]++] = b, g[b][s[b]++] = a; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) d[j] = v[i][j] = z; for (l = r = d[i] = 0, q[r++] = i; l != r; l++) { for (int j = 0; j < s[q[l]]; j++) { if (d[g[q[l]][j]] == z) d[g[q[l]][j]] = d[q[l]] + 1, q[r++] = g[q[l]][j]; } } for (int j = 0; j < N; j++) { if (d[j] <= t[i]) v[i][j] = c[i]; } } for (int i = 0; i < N; i++) d[i] = t[i] = z; d[0] = 0; for (int i = 0; i < N; i++) { int u = z; for (int j = 0; j < N; j++) { if (t[j] && (u == z || d[u] > d[j])) u = j; } t[u] = 0; for (int j = 0; j < N; j++) { if (d[j] > d[u] + v[u][j]) d[j] = d[u] + v[u][j]; } } printf("%d\n", d[N - 1]); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = (int)2e5 + 2; const int INF = (int)1e9 + 7; const long double pi = acos(-1); const long long linf = (long long)1e18; int w, h, k, ans; int main() { cin >> w >> h >> k; for (int i = 1; i <= k; ++i) { ans += ((w * 2) + (h * 2) - 4); h -= 4; w -= 4; } cout << ans << "\n"; return 0; }
1