solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; void AIA() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long tin(string s) { long long re = 0; for (int i = 0; i < s.size(); i++) { re *= 10; re += int(s[i] - '0'); } return re; } int main() { AIA(); int t; cin >> t; for (int T = 1; T <= t; T++) { long long a[3], n, m = 0; cin >> a[0] >> a[1] >> a[2] >> n; sort(a, a + 3); m += a[2] - a[1]; m += a[2] - a[0]; n -= m; if (n % 3 || n < 0) { cout << "NO\n"; } else { cout << "YES\n"; } } return 0; }
1
#include<bits/stdc++.h> using namespace std; int main() { long long l,r; scanf("%lld%lld",&l,&r); r=min(r,l+5000); long long ans=2018; for(long long i=l;i<=r;i++) for(long long j=i+1;j<=r;j++) { ans=min(ans,i*j%2019); } printf("%lld\n",ans); }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; const long long mod = 1e9 + 1e9 + 7; char s[MAXN]; int n; long long sum[MAXN]; long long quick_pow(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } int main() { scanf("%s", s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; ++i) sum[i] = (sum[i - 1] * 10 + s[i] - '0') % mod; for (int i = n / 2; i <= n - 1; ++i) { if (s[i + 1] == '0' && i + 1 < n) continue; if (i < n - i) continue; for (int j : {2 * i - n, 2 * i - n + 1, n - i - 1, n - i}) { if (s[1] == '0' && j > 1) continue; if (s[j + 1] == '0' && i - j > 1) continue; long long a = sum[j]; long long b = (sum[i] - sum[j] * quick_pow(10, i - j) % mod + mod) % mod; long long c = (sum[n] - sum[i] * quick_pow(10, n - i) % mod + mod) % mod; if ((a + b) % mod == c) { for (int k = 1; k <= j; ++k) printf("%c", s[k]); printf("+"); for (int k = j + 1; k <= i; ++k) printf("%c", s[k]); printf("="); for (int k = i + 1; k <= n; ++k) printf("%c", s[k]); puts(""); return 0; } } } }
6
#include <bits/stdc++.h> using namespace std; const int MAX = 2005; int cnt[MAX][2]; int main() { int n; scanf("%d", &n); memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); cnt[y - x + 1000][0]++; cnt[x + y][1]++; } int ans = 0; for (int i = 0; i < MAX; ++i) { if (cnt[i][0] >= 2) ans += cnt[i][0] * (cnt[i][0] - 1) / 2; if (cnt[i][1] >= 2) ans += cnt[i][1] * (cnt[i][1] - 1) / 2; } printf("%d\n", ans); return 0; }
2
#include<bits/stdc++.h> using namespace std; typedef long long ll; int x,y,cnt[5],ans; int main() { for(int i=1;i<=3;i++) { scanf("%d%d",&x,&y); cnt[x]++,cnt[y]++; } for(int i=1;i<=4;i++) if(cnt[i]&1) ans++; puts(ans<=2?"YES":"NO"); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; long long a[1111], b[1111]; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; long long ans = 0; for (int i = 1; i <= n; ++i) { long long cur1 = 0, cur2 = 0; for (int j = i; j <= n; ++j) { cur1 = cur1 | a[j]; cur2 = cur2 | b[j]; ans = max(ans, cur1 + cur2); } } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long long loo = 1ll << 62; const int mods = 998244353; const int MAXN = 200005; const int INF = 0x3f3f3f3f; inline int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } char st[MAXN]; struct Segment_Tree { struct segnode { int sum, premx, premn, sufmx, sufmn, pred, sufd, d, ans; } tree[MAXN << 2]; void Update(int y) { segnode &x = tree[y], &l = tree[y << 1], &r = tree[y << 1 | 1]; x.sum = l.sum + r.sum; x.premx = max(l.premx, r.premx + l.sum); x.premn = min(l.premn, r.premn + l.sum); x.sufmx = max(l.sufmx + r.sum, r.sufmx); x.sufmn = min(l.sufmn + r.sum, r.sufmn); x.d = max(l.d + r.sum, r.d - l.sum); x.pred = max(l.pred, max(r.pred - l.sum, l.d + r.premx)); x.sufd = max(r.sufd, max(r.sum + l.sufd, r.d - l.sufmn)); x.ans = max(max(l.ans, r.ans), max(r.pred - l.sufmn, l.sufd + r.premx)); } void build(int x, int l, int r) { if (l == r) { if (st[l] == '(') tree[x] = (segnode){1, 1, 0, 1, 0, 1, 1, 1, 1}; else tree[x] = (segnode){-1, 0, -1, 0, -1, 1, 1, 1, 1}; return; } int mid = (l + r) >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); Update(x); } void change(int x, int l, int r, int y) { if (l == r) { if (st[l] == '(') tree[x] = (segnode){1, 1, 0, 1, 0, 1, 1, 1, 1}; else tree[x] = (segnode){-1, 0, -1, 0, -1, 1, 1, 1, 1}; return; } int mid = (l + r) >> 1; if (y <= mid) change(x << 1, l, mid, y); else change(x << 1 | 1, mid + 1, r, y); Update(x); } } segment; int main() { int n = read() * 2 - 2, Case = read(); scanf("%s", st + 1); segment.build(1, 1, n); printf("%d\n", segment.tree[1].ans); while (Case--) { int x = read(), y = read(); swap(st[x], st[y]); segment.change(1, 1, n, x); segment.change(1, 1, n, y); printf("%d\n", segment.tree[1].ans); } return 0; }
3
#include <bits/stdc++.h> char s[2001]; long long dp[2001][2001]; long long sum[2001]; int main() { int n, k; int i, j, l; scanf("%d %d", &n, &k); scanf(" %s", s); for (i = 0; i <= n; i++) for (j = 0; j <= k; j++) dp[i][j] = 0; for (i = 0; i <= k; i++) sum[i] = 0; dp[0][0] = 1; sum[0]++; for (i = 1; i <= n; i++) { for (j = k; j >= 0; j--) { dp[i][j] = (dp[i][j] + sum[j] * (s[i - 1] - 'a')) % 1000000007; int p = n - i + 1; for (l = i - 1; l >= 0 && j + p * (i - l) <= k; l--) { dp[i][j + p * (i - l)] = (dp[i][j + p * (i - l)] + dp[l][j] * ('z' - s[i - 1])) % 1000000007; } } for (j = 0; j <= k; j++) sum[j] = (sum[j] + dp[i][j]) % 1000000007; } long long ans = 0; for (i = 0; i <= n; i++) ans = (ans + dp[i][k]) % 1000000007; printf("%I64d\n", ans); return 0; }
5
/*author - Kishor Kumar*/ #include<bits/stdc++.h> using namespace std; #define pow2(x) ((x)*(x)); #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) #define ll long long #define ld long double #define eb emplace_back #define pb push_back #define mod 1000000007 #define mp make_pair #define ff first #define ss second #define all(c) (c).begin(),(c).end() #define nl "\n" #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int,int> pi; typedef vector<pair<ll,ll>> vpl; typedef pair<ll,ll> pl; typedef map<ll,ll> ml; typedef map<int,int> mi; typedef unordered_map<int,int> umi; typedef unordered_map<char,int> umc; typedef unordered_set<int> usi; typedef stack<int> st; void solve(){ string s; cin>>s; int c = 0; for(int i=1;i<s.size();i++){ if(s[i-1] == s[i] || (i>1 && s[i-2] == s[i])){ c++; s[i] = '0'; } } cout<<c; } int main() { fast; ll t; cin>>t; while(t--){ solve(); cout<<nl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; inline long long read() { char c = getchar(); long long f = 1, x = 0; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ '0'); c = getchar(); } return x * f; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int maxn = 100100; long long MIN(long long x, long long y) { return x < y ? x : y; } long long MAX(long long x, long long y) { return x > y ? x : y; } long long Gcd(long long x, long long y) { return y == 0 ? x : Gcd(y, x % y); } long long f[4]; int main() { long long n, k, a, b, i, j; n = read(); k = read(); a = read(); b = read(); f[0] = a + b; f[1] = a - b; f[2] = -a + b; f[3] = -a - b; long long x = 1e18 + 5, y = -1; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) { long long c = (f[j] + k) % k; long long s = (i * k + c); x = MIN(x, n * k / Gcd(n * k, s)); y = MAX(y, n * k / Gcd(n * k, s)); } } printf("%lld %lld\n", x, y); return 0; }
1
#include<ctime> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #define p 1000000007 #define file(x)freopen(x".in","r",stdin);freopen(x".out","w",stdout) #define rt register int #define l putchar('\n') #define ll long long #define r read() using namespace std; inline ll read(){ ll x=0;char zf=1;char ch=getchar(); while(ch!='-'&&!isdigit(ch))ch=getchar(); if(ch=='-')zf=-1,ch=getchar(); while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return x*zf; } void write(ll y){if(y<0)putchar('-'),y=-y;if(y>9)write(y/10);putchar(y%10+48);} void writeln(const ll y){write(y);putchar('\n');} int k,m,n,x,y,z,cnt,ans; char c[100010];int a[100010]; int f[100010],la[100010][3]; int main(){ scanf("%s",c+1);n=strlen(c+1); la[0][1]=la[0][2]=0; for(rt i=1;i<=n;i++){ la[i][0]=la[i-1][0],la[i][1]=la[i-1][1],la[i][2]=la[i-1][2]; a[i]=(a[i-1]+(c[i]=='b')+1)%3;la[i][a[i]]=i; } bool fla=0; for(rt i=2;i<=n;i++)if(c[i]==c[i-1])fla=1; if(!fla)return cout<<1,0; bool vis[3]={0,0,0}; for(rt i=1;i<=n;i++){ if(a[i])f[i]=1,vis[a[i]]=1; } for(rt i=1;i<=n;i++){ f[i]+=((ll)f[la[i][0]]+f[la[i][1]]+f[la[i][2]]-f[la[i][a[i]]])%p; (f[i]+=p)%=p; } cout<<f[n]; return 0; }
0
#pragma GCC optimize("trapv") #include<bits/stdc++.h> #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pb push_back using namespace std; using ll = long long; //using arr = array; using pi = pair<int, int>; using vi = vector<ll>; using vvi = vector<vi>; using vpi = vector<pi>; using ld = long double; const int maxn = 1<<18, mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n,m,qsz = 0; vi g[maxn]; vi q[maxn]; int vis[maxn]; vi st; int bip, csz; void dfs(int v, int c = 1) { csz++; vis[v] = c; for(auto &i : g[v]) { if(c == vis[i]) bip = 0; if(!vis[i]) dfs(i, c^3); } } ll count() { ll i, b, u; i = b = u = 0; for(int v = 1; v <= n; v++) { if(vis[v]) continue; bip = 1, csz = 0; dfs(v); if(csz == 1) i++; else if(bip) b++; else u++; } return i * 2 * n - i*i + 2*b*(b+u) + u*u; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m; for(int f, t, i = 0; i < m; i++) { cin >> f >> t; g[f].pb(t); g[t].pb(f); } cout << count(); }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; long long int n, x; cin >> n; long long int b[n]; map<long long int, long long int> mp; for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { mp[(i - b[i])] += b[i]; } long long int maxv = 0; for (auto x : mp) { maxv = max(maxv, x.second); } cout << maxv << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; long long a[N]; long long tree[N << 2]; void build(int i, int l, int r) { if (l == r) return void(tree[i] = 2 * a[l]); int mid = (l + r) >> 1; build(i << 1, l, mid); build(i << 1 | 1, mid + 1, r); tree[i] = min(tree[i << 1], tree[i << 1 | 1]); } int indx; int target; void get(int i, int l, int r, int a, int b) { if (l > b || r < a || indx != -1 || tree[i] >= target) return; if (l == r) { indx = l; return; } int mid = (l + r) >> 1; get(i << 1, l, mid, a, b); get(i << 1 | 1, mid + 1, r, a, b); } int ans[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; long long ma = 0, mn = 2e9; for (int i = 1; i <= n; i++) cin >> a[i], ma = max(ma, a[i]), mn = min(mn, a[i]); if (2 * mn >= ma) { for (int i = 1; i <= n; i++) cout << -1 << " "; return 0; } for (int i = n + 1; i <= 3 * n; i++) a[i] = a[i - n]; build(1, 1, 3 * n); mn = 3 * n; for (int i = 3 * n; i >= 1; i--) { indx = -1; target = a[i]; get(1, 1, 3 * n, i, 3 * n); if (indx == -1) indx = 3 * n; mn = min(mn, 1ll * indx); ans[i] = mn - i; } for (int i = 1; i <= n; i++) cout << ans[i] << " "; return 0; }
4
#include<iostream> using namespace std; int main(){ int n; cin>>n; if(n==10||n==100||n==1000||n==10000||n==100000){ cout<<10<<endl; } else cout<<-9*(n/100000)-9*(n/10000)-9*(n/1000)-9*(n/100)-9*(n/10)+n<<endl; return 0; }
0
#include<bits/stdc++.h> #define int long long using namespace std; int a[100010],n; signed main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); int ans=0; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; if(a[i]&&a[i-1])a[i]--,ans++; ans+=(a[i]>>1); a[i]&=1; } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); int maxi = max(m + (n - 1) / 3 * m + ((n - 1) % 3 > 0 && m < 4 ? (m == 1 || m == 3 ? 1 : 2) : 0), n + (m - 1) / 3 * n + ((m - 1) % 3 > 0 && n < 4 ? (n == 1 || n == 3 ? 1 : 2) : 0)); int maxi_2 = 0; for (int i = 0; i < n; i++) { if (i & 1) { maxi_2 += m / 2; } else { maxi_2 += (m - 1) / 2 + 1; } } int maxi_3 = 0; for (int i = 0; i < n; i++) { if (i & 1) { maxi_3 += (m - 1) / 2 + 1; } else { maxi_3 += m / 2; } } int maxi_4 = 0; int k = (n == 2 ? m : (m == 2 ? n : -1)); for (int i = 0; i < k; i += 4) { if (i + 1 < k) maxi_4 += 4; else maxi_4 += 2; } maxi = max(maxi, max(maxi_2, maxi_3)); maxi = max(maxi, maxi_4); printf("%d", maxi); return 0; }
4
#include <bits/stdc++.h> long double f[100000 + 5]; long double C(int n, int m) { return f[n] - f[m] - f[n - m]; } template <typename Elem_1, typename Elem_2> Elem_1 mn(Elem_1 a, Elem_2 b) { return a < b ? a : b; } int main() { f[0] = 0; for (int i = 1; i <= 100000; i++) { f[i] = f[i - 1] + log(1.0 * i); } int n, m, k; scanf("%d%d%d", &n, &m, &k); long double ans = 0.0; long double tmp; for (int x = 0; x <= n; x++) { for (int y = 0, z; y <= n; y++) { if (n * (x + y) - x * y <= k) { z = n * (x + y) - x * y; tmp = C(n, x) + C(n, y) + C(m - z, k - z) - C(m, k); ans = mn(1e99, ans + exp(tmp)); } } } printf("%.10lf\n", (double)ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t, n, i, j, k; cin >> t; string s; bool hi; while (t--) { cin >> s; n = s.size(); map<char, long long> m; for (i = 0; i < n; i++) m[s[i]]++; map<char, long long>::iterator it = m.begin(); hi = true; char ch = (it->first); if ((it->second) > 1) hi = false; it++; for (; it != m.end(); it++) { j = (it->second); char ch1 = (it->first); if (j > 1) { hi = false; break; } if ((ch1 - 97) == (ch - 97) + 1) ch = ch1; else { hi = false; break; } } if (hi) cout << "YES\n"; else cout << "NO\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int amount; int numbers[200005]; long long cal(int exist, int without) { if (exist < without) return (long long)exist * (without - exist); else return (long long)(amount - exist + 1) * (exist - without); } void solve() { long long res = 0; for (int i = 1; i <= amount; i++) res += cal(numbers[i], numbers[i - 1]); printf("%lld", res); } int main() { scanf("%d", &amount); for (int i = 1; i <= amount; i++) scanf("%d", &numbers[i]); solve(); }
5
#include <bits/stdc++.h> using namespace std; int main() { string st, st2 = "WUB"; cin >> st; vector<char> v; int l = st.size(); int j = 0; for (int i = 0; i < l; i++) { if (st[i] == 'W' && st[i + 1] == 'U' && st[i + 2] == 'B') { if (i > 0 && j == 0) { v.push_back('1'); i += 2; j = 1; } else { i += 2; } } else { j = 0; v.push_back(st[i]); } } for (int i = 0; i < v.size(); i++) { if (v[i] != '1') { cout << v[i]; } else { cout << " "; } } }
1
#include <bits/stdc++.h> using namespace std; const long long maxn = 27; int main() { long long i, j, k, l, m, n, a, b; cin >> n; vector<pair<int, int>> pr; int y = 500000; int x = 0; pr.push_back({x, y}); pr.push_back({x + 1, y}); pr.push_back({x, y - 1}); pr.push_back({x + 1, y - 1}); x++, y--; while (n--) { pr.push_back({x + 1, y}); pr.push_back({x, y - 1}); pr.push_back({x + 1, y - 1}); x++, y--; } cout << pr.size() << "\n"; for (auto it : pr) { cout << it.first << " " << it.second << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int n; cin >> n; string s; cin >> s; vector<long long int> v; long long int j = 0, temp = 1; for (int i = 1; i < n; i++) { if (s[i] == s[j]) temp++; else { j = i; v.push_back(temp); temp = 1; } } if (v.empty()) v.push_back(temp); else { if (s[0] == s[n - 1]) v[0] += temp; else v.push_back(temp); } long long int res = 0; if (int(v.size()) != 1) { for (int i = 0; i < int(v.size()); i++) res += v[i] / 3; cout << res << "\n"; } else { if (v[0] % 3 == 0) cout << v[0] / 3 << "\n"; else cout << v[0] / 3 + 1 << "\n"; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long n, s; cin >> n >> s; vector<long long> nums(n); for (auto &x : nums) { cin >> x; } sort(nums.begin(), nums.end()); long long answ = 0; if (nums[n / 2] > s) { for (long long i = 0; i <= n / 2; i++) { if (nums[i] > s) { answ += nums[i] - s; } } } else if (nums[n / 2] < s) { for (long long i = n / 2; i < n; i++) { if (nums[i] < s) { answ += s - nums[i]; } } } cout << answ; cin >> n; return 0; }
2
#include <bits/stdc++.h> const int N = 100001; using namespace std; long long computeXor(long long x) { long long modFour = x % 4LL; switch (modFour) { case 0: return x; case 1: return 1LL; case 2: return x + 1LL; case 3: return 0LL; } } int main() { int n; long long xorOfAll = 0; long long x, m; scanf("%d", &n); while (n--) { scanf("%I64d %I64d", &x, &m); xorOfAll ^= (computeXor(x + m - 1) ^ computeXor(x - 1)); } printf("%s\n", xorOfAll ? "tolik" : "bolik"); return 0; }
3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; template <typename T = int> inline const T read() { T x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); } return x * f; } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } vector<string> s = { " # # ###### # ", " # ### # # # # ##### ", " ### # # ## # # # ", " #### # # ##### #### # ### # ", " ## # # #### # # # ## ", " # ## ##### ### ### # # ## # # # ", " ### # # # # #### ## ## ## ## ", " ###### ## ## ### ### ### ## # # ### ", " ############ # # # ## ## ## # ### ", " #################### ####### #### ########## ", " ################################################ ", " ################################################## ", " ################################################## ", " #################################################### ", " ###################################################### ", " ###################################################### ", " ######################################################## ", " ########################################################## ", " ######################################################## ", " ############ # ############## # # ############# ", " ## ######### # ############### # ######## ", " #### ##### ## # ############### ## ## # ## ## ", " ####### # ### ### ########### # # ## ###### ", " ######### # # ## ## ######### ## ##### # ######### ", " ############ ## ### ### ####### # # ### ########### ", " ######### # ### ### ######## # ##### # # # ######## ", "############ ### # # # # ###### # # ## ### #########", "########### # # ## ### ### #### # ### #### ### # #########", "########### ###### #### # # # # # # #### ### #########", "########### # # ### ### # # ## ## # # # # #########", "############ ## #### ## ### # ### #### # ###### ##########", "########### ### # # ### # ### #### # ### # #########", "############# ## ### ## ### ## # ## ## # # #########", "############## ## # # # # # # # ## # ## ## ############", "############ #### # # # # #### ## # # ### ### ###########", "############# # # ### #### # ## # ## #############", "################ ### # ## ## ## ## # ### # # ############", "################## # ###### ####### # ################", " ################ # ##### # ########### # ############## ", " ############################## ############################ ", " ########################### # # ############################# ", " ######################### # ################################ ", " ######################## # ############################# ", " ####### ################### ############################### ", " ####### # ################ ##################### ####### ", " ####### # ########### # ########### ##### # ###### ", " ####### # # # # # ## # ## # # # ## ## # ####### ", " ######## ## # ## ## # # ### ######## ", " ####### # #### #### ### ### ### ##### # ######### ", " ########## # # # # #### ### ## # # ######### ", " ######## ## ## # # # # ### # ########## ", " ######### ## ##### ## # # # ## ############# ", " ######### ### # # ### # ### # ########### ", " ######### #### ### ## #### #### ############ ", " ######### ## # # # # ############## ", " ############ # #### ## ## # ## ############ ", " ############# #### # ############ ", " #################### ################# ", " #################################### ", " ################################ ", " ############################## ", " ######################## ", " #################### ", " ############ "}; int main() { int x, y; cin >> x >> y; cout << (s[x][y] == '#' ? "IN" : "OUT") << endl; return 0; }
5
#include <iostream> using namespace std; int main() { int n, m; while(cin >> n >> m ,n){ int ans = 0; for(int i=0;i<n;i++){ int a; cin >> a; ans += min(a, m/n); } cout << ans << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int n,k;cin>>n>>k; string s; cin>>s; int res = 0; for(int i = 0; i < n-1; i++){ if(s[i] == s[i+1]) res++; } int ans = min(n-1,res+2*k); cout<<ans; }
0
#include <bits/stdc++.h> using namespace std; long long n, m, dx, dy; long long maxn = 0, maxx, maxy; long long w[2 * 1000000 + 5]; long long x, y, q; void e_gcd(long long a, long long b) { if (b == 0) { x = 1; y = 0; q = a; return; } e_gcd(b, a % b); long long temp = x; x = y % n; y = temp - a / b * y; y %= n; return; } int main() { long long i; cin >> n >> m >> dx >> dy; for (i = 0; i < m; i++) { long long a, b; scanf("%I64d %I64d", &a, &b); e_gcd(n, dx); w[(n + b - (dy * y) % n * a % n) % n]++; if (maxn < w[(n + b - (dy * y) % n * a % n) % n]) { maxn = w[(n + b - (dy * y) % n * a % n) % n]; maxx = a; maxy = b; } } cout << maxx << " " << maxy << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; bool isPrime[1000005] = {false}; vector<long long> prm; vector<long long> SPF(1000005); void sieve() { isPrime[1] = isPrime[0] = 0; long long i; SPF[1] = SPF[0] = 1; for (i = 2; i < 1000005; ++i) isPrime[i] = 1, SPF[i] = i; for (i = 2; i * i < 1000005; ++i) { if (isPrime[i]) { for (long long j = i * i; j < 1000005; j += i) { isPrime[j] = 0; if (SPF[j] == j) SPF[j] = i; } } } if (!prm.empty()) prm.clear(); for (i = 2; i < 1000005; ++i) { if (isPrime[i]) prm.push_back(i); } } void printa(bool f) { if (f) cout << "YES" << "\n"; else cout << "NO" << "\n"; } bool flip(bool f) { if (f) return 0; return 1; } long long fastPower(long long a, long long b) { if (b == 0) return 1; long long prod = 1; while (b > 0) { if (b & 1) prod = prod * a; b = b >> 1; a = a * a; } return prod; } long long fact(long long n) { long long res = 1; for (long long i = 2; i <= n; i++) { res *= i; } return res; } long long nCr(long long n, long long r) { return (long double)fact(n) / (long double)(fact(r) * fact(n - r)); } void solve() { long long n; cin >> n; if (n < 2 || (n & 1)) cout << 0 << "\n"; else if (!(n & 1)) cout << fastPower(2, n / 2) << "\n"; } int main() { int tc = 1; while (tc--) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int x,a,b; cin>>x>>a>>b; if(abs(a-x)<abs(b-x)) cout<<"A"<<endl; else cout<<"B"<<endl; return 0; }
0
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(); int res = 0; for (int i = 0; i < n; i++) { std::string s; std::cin >> s; if (s == "E869120") res++; } std::cout << res << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[100010]; int n, l, x, y; bool have(int jump) { int lastidx = 0; for (int i = 1; i < n; i++) { while (a[i] - a[lastidx] > jump) lastidx++; if (a[i] - a[lastidx] == jump) { return true; } } return false; } int main() { scanf("%d%d%d%d", &n, &l, &x, &y); for (int i = 0; i < n; i++) scanf("%d", &a[i]); if (have(x) && have(y)) printf("0\n"); else if (have(x)) printf("1\n%d\n", y); else if (have(y)) printf("1\n%d\n", x); else { if (have(x + y)) { int jump = x + y; int lastidx = 0; for (int i = 1; i < n; i++) { while (a[i] - a[lastidx] > jump) lastidx++; if (a[i] - a[lastidx] == jump) { printf("1\n%d\n", a[lastidx] + x); return 0; } } } if (!have(y - x)) printf("2\n%d\n%d\n", x, y); else { int jump = y - x; int lastidx = 0; for (int i = 1; i < n; i++) { while (a[i] - a[lastidx] > jump) lastidx++; if (a[i] - a[lastidx] == jump) { if (a[i] - y >= 0) { printf("1\n%d\n", a[i] - y); return 0; } else if (a[lastidx] + y <= l) { printf("1\n%d\n", a[lastidx] + y); return 0; } } } printf("2\n%d\n%d\n", x, y); } } return 0; }
4
#include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} struct Node{ size_t cnt; Node *p,*l,*r; Node():cnt(0){p=l=r=nullptr;} Node(Node* p):cnt(0),p(p){l=r=nullptr;} }; template<typename T,size_t X> struct BinaryTrie{ T acc; Node *root; Int ptr=0; static vector<Node> vs; BinaryTrie():acc(0),ptr(0){ if(vs.empty()) vs.resize(3e6); root=emplace(nullptr); } inline Node* emplace(Node* p){ return &(vs[ptr++]=Node(p)); } inline size_t count(Node* a){ return a?a->cnt:0; } void add(const T b,size_t k=1){ const T nb=b^acc; Node* a=root; for(Int i=X-1;i>=0;i--){ bool f=(nb>>i)&1; if(!f&&!a->l) a->l=emplace(a); if( f&&!a->r) a->r=emplace(a); a=f?a->r:a->l; } a->cnt+=k; while((a=a->p)) a->cnt=count(a->l)+count(a->r); } inline void update(const T b){acc^=b;} size_t order_of_key(const T b){ Node *a=root; size_t res=0; for(Int i=X-1;i>=0;i--){ Node *l=a->l,*r=a->r; if((acc>>i)&1) swap(l,r); bool f=(b>>i)&1; if(f) res+=count(l); a=f?r:l; if(!a) break; } return res; } }; template<typename T, size_t X> vector<Node> BinaryTrie<T, X>::vs; //INSERT ABOVE HERE signed main(){ Int n,k; scanf("%lld %lld",&n,&k); k=n*(n+1)/2-k; vector<Int> a(n); for(Int i=0;i<n;i++) scanf("%lld",&a[i]); auto calc= [&](Int x)->Int{ Int res=0; BinaryTrie<Int, 20> bt; bt.add(0); for(Int i=0;i<n;i++){ bt.update(a[i]); res+=bt.order_of_key(x); bt.add(0); } return res; }; Int l=0,r=1<<20; while(l+1<r){ Int m=(l+r)>>1; if(calc(m)<=k) l=m; else r=m; } printf("%lld\n",l); return 0; }
0
#include<bits/stdc++.h> using namespace std; long long int flagE,flagW,n,ans=10000000; char prevchar='W',s[300001]; int main() { long long int i,j; cin>>n; for(i=0;i<n;i++) { cin>>s[i]; if((prevchar=='E')&&(s[i]=='E')&&((flagW-flagE-1)<ans)) ans=(flagW-flagE-1); if(s[i]=='W') flagW++; else flagE++; prevchar=s[i]; } if(0<ans) cout<<flagE; else cout<<(ans+flagE)<<'\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, *Z; string s; long long sum = 0, mx = 0, mn = 0, t, x = 0; cin >> n; Z = new int[n]; for (int i = 0; i < n; ++i) { cin >> Z[i]; sum += Z[i]; } cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == 'A') { x -= Z[i]; } else { x += Z[i]; } if (x < mn) mn = x; if (x > mx) mx = x; } if (mx - x > -mn) t = mx - x; else t = -mn; cout << (sum + x + 2 * t) / 2 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; struct node { int x, y; } f[2 * N]; int n, pos, vt[N], a[N], b[N], dd[N]; vector<pair<int, int> > vec; bool check(int x) { if (x == 0) return 0; while (x) { if (x % 10 != 7 && x % 10 != 4) return 0; x /= 10; } return 1; } int cnt = 0; int main() { scanf("%d", &n); pos = -1; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), vec.push_back({a[i], i}); bool ok = 0; for (int i = 1; i < n; ++i) { if (a[i + 1] < a[i]) { ok = 1; break; } } if (!ok) { printf("0"); return 0; } sort(vec.begin(), vec.end()); for (int i = 1; i <= n; ++i) { if (check(a[i])) pos = i; a[i] = b[i] = lower_bound(vec.begin(), vec.end(), pair<int, int>(a[i], i)) - vec.begin(); vt[a[i]] = i; } if (pos == -1) { printf("-1"); return 0; } int x; dd[b[pos]] = 1; sort(b + 1, b + n + 1); for (int i = 1; i <= n; ++i) { if (dd[b[i]]) continue; x = vt[b[i]]; if (x == i) continue; if (i != pos) { vt[a[pos]] = i; vt[a[i]] = pos; swap(a[i], a[pos]); f[++cnt] = {pos, i}; pos = i; } if (x != pos) { vt[a[pos]] = x; vt[a[x]] = pos; swap(a[x], a[pos]); f[++cnt] = {pos, x}; pos = x; } } printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { printf("%d %d\n", f[i].x, f[i].y); } return 0; }
4
#include <bits/stdc++.h> using namespace std; bool vis[100010]; struct note { int v, next; } edge[100010 * 3]; int head[100010], kk; void add(int from, int to) { edge[kk].next = head[from]; edge[kk].v = to; head[from] = kk++; } int cnt; void dfs(int root) { if (vis[root]) return; cnt++; vis[root] = 1; for (int i = head[root]; i != -1; i = edge[i].next) dfs(edge[i].v); } int main() { int n, m, p; cin >> n >> m >> p; int a, b; kk = 1; memset(head, -1, sizeof(head)); for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); add(a, b); add(b, a); } memset(vis, 0, sizeof(vis)); long long sum = 0, re = 1; int k = 0; for (int i = 1; i <= n; i++) { if (vis[i] == 0) { cnt = 0; dfs(i); k++; sum = (sum + cnt) % p; re = (re * cnt) % p; } } for (int i = 1; i <= k - 2; i++) re = (re * sum) % p; if (k == 1) cout << 1 % p << "\n"; else cout << re << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n >> a >> b; int ans = 0; for (int i = 1; i <= n; i++) { if (n - i <= b && i > a) ans++; } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 5; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } vector<long long> Sieve(long long n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (long long i = 2; i * i <= n; i++) { if (prime[i]) { for (long long j = i * i; j <= n; j += i) { prime[j] = false; } } } vector<long long> primes; for (long long i = 2; i <= n; i++) { if (prime[i]) primes.push_back(i); } return primes; } set<long long> primeFactors(long long n) { set<long long> factors; while (n % 2 == 0) { n /= 2; factors.insert(2); } for (long long i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { factors.insert(i); n /= i; } } if (n > 2) factors.insert(n); return factors; } long long power(long long x, unsigned long long y, long long p) { long long res = 1; x = x % p; if (x == 0) return 0; ; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int32_t main() { c_p_c(); long long n, m; cin >> n >> m; long long *arr = new long long[m]; map<long long, long long> occ; long long ans = 0; for (long long i = 0; i < m; i++) { cin >> arr[i]; occ[arr[i]]++; long long t = 0; set<long long> j; for (auto d : occ) { if (d.second > 0) { t++; j.insert(d.first); } } if (t == n) { ans++; for (auto d : j) { occ[d]--; } } } cout << ans << '\n'; }
1
#include <bits/stdc++.h> using namespace std; long long n, k, a, b; struct bitch { long long b[200100]; bitch() { memset(b, 0, sizeof b); } void reset() { memset(b, 0, sizeof b); } void update(int at, int val) { for (int i = at; i < 200100; i += (i & (-i))) b[i] += val; } long long query(int at) { long long ret = 0; for (int i = at; i > 0; i -= (i & (-i))) ret += b[i]; return ret; } long long query(int i, int j) { return query(j) - query(i - 1); } }; bitch A, B; int main() { int q; scanf("%lld %lld %lld %lld %d", &n, &k, &a, &b, &q); A.reset(); B.reset(); while (q--) { int o; scanf("%d", &o); if (o == 1) { long long d, val; scanf("%lld %lld", &d, &val); long long addA = min(a - A.query(d, d), val); A.update(d, addA); long long addB = min(b - B.query(d, d), val); B.update(d, addB); } else { long long d; scanf("%lld", &d); cout << B.query(d - 1) + A.query(d + k, n + 1) << endl; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAX = 405, MOD = 1e9 + 7; int n, k, a[MAX], b[MAX], t[MAX][MAX], dp[MAX][MAX]; int main() { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) scanf("%d", &b[i]); for(int p = 0; p <= 400; p++) { for(int i = 1; i <= 400; i++) { int x = 1; for(int it = 1; it <= p; it++) x = 1LL * x * i % MOD; t[p][i] = (t[p][i - 1] + x) % MOD; } } dp[n + 1][0] = 1; for(int i = n; i > 0; i--) { for(int c = 0; c <= k; c++) { for(int p = 0; p <= c; p++) { int v = (t[p][b[i]] - t[p][a[i] - 1] + MOD) % MOD; v = 1LL * v * dp[i + 1][c - p] % MOD; dp[i][c] = (dp[i][c] + v) % MOD; } } } printf("%d\n", dp[1][k]); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long qpow(long long b, long long e) { if (e == 0) return 1LL; if (e & 1LL) return qpow(b, e - 1LL) * b; return qpow(b * b, e >> 1LL); } int main() { long long n, x, y; cin >> n >> x >> y; long long a = max(n - x, n - y); long long b = max(x - 1, y - 1); cout << (a < b ? "Black" : "White") << endl; return 0; }
1
#include "bits/stdc++.h" using namespace std; const int MX = 1e5 + 5; const int INF = 2e9; int N, A[MX]; void solve() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; sort(A, A + N); int bZ = 0, Z = 0, aZ = 0; for (int i = 0; i < N; i++) { if (A[i] == 0) Z++; if (A[i] > 0) aZ++; if (A[i] < 0) bZ++; } int ans = bZ + Z; if (aZ) { // can we include the min value > 0. bool good = 1; int x = 0; for (int i = 0; i < N; i++) { if (A[i] > 0) { x = A[i]; break; } } for (int i = 1; i < N; i++) if (A[i] < 0) { if (A[i] - A[i - 1] < x) good = 0; } if (good) ans = max(ans, bZ + min(Z, 1) + 1); ans = max(ans, 1); } cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; bool visited[200000]; long long mem[200000]; int a[200000]; int n; long long run23(int x) { if (visited[x]) return mem[x]; visited[x] = true; long long dy = a[x]; int newx = x + a[x]; if (newx >= n) { mem[x] = dy; return dy; } dy += a[newx]; newx -= a[newx]; if (newx < 0) { mem[x] = dy; return dy; } long long r = run23(newx); if (r == -1) mem[x] = -1; else mem[x] = r + dy; return mem[x]; } int main() { cin >> n; for (int i = 1; i < n; i++) { visited[i] = false; mem[i] = -1; cin >> a[i]; } visited[0] = true; mem[0] = -1; for (int i = 1; i < n; i++) { int x = i - a[i]; long long dy; if (x < 0) cout << i + a[i] << endl; else if ((dy = run23(x)) == -1) cout << "-1" << endl; else cout << i + a[i] + dy << endl; } }
2
#include <bits/stdc++.h> using namespace std; const int N = 2000 + 5; const int inf = 2e9 + 5; int n; int a[N]; int first[N]; bool tmp = false; void xuli() { int s = 0; for (int i = 1; i <= n; i++) { if (first[i] == 1) s += a[i]; else s -= a[i]; } if (s % 360 == 0) tmp = true; } void thu(int k) { if (k > n) { xuli(); return; } for (int i = 1; i <= 2; i++) { first[k] = i; thu(k + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; thu(1); if (tmp) cout << "YES"; else cout << "NO"; }
2
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin.exceptions(cin.failbit); int T; cin >> T; while (T--) { long long int t, n, i, j, x, y, m, l, d, k; cin >> n >> m; vector<int> a(n), b(m); for (i = 0; i < (n); ++i) cin >> a[i]; for (i = 0; i < (m); ++i) cin >> b[i]; for (i = 0; i < (n); ++i) { for (j = 0; j < (m); ++j) { if (a[i] == b[j]) { cout << "YES" << "\n" << 1 << " " << a[i] << "\n"; x = 1; break; } } if (x == 1) break; } if (x == 0) cout << "NO" << "\n"; else x = 0; } }
1
#include <bits/stdc++.h> using namespace std; long long k, a[100005]; int n; bool check(long long x) { long long sum = 0; for (int i = 1; i <= n; i++) if (a[i] < x) sum += a[i]; else sum += x - 1; return sum <= k; } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; long long ans, sum; sum = 0; for (int i = 1; i <= n; i++) sum += a[i]; if (sum < k) { cout << "-1" << endl; return 0; } if (sum == k) { return 0; } long long l, r, mid; l = r = a[1]; for (int i = 2; i <= n; i++) l = min(l, a[i]), r = max(r, a[i]); l = 0; while (l <= r) { mid = (l + r) / 2; if (check(mid)) ans = mid, l = mid + 1; else r = mid - 1; } int p; sum = 0; for (int i = 1; i <= n; i++) if (a[i] < ans) sum += a[i]; else sum += ans - 1; for (int i = 1; i <= n; i++) if (a[i] >= ans) { if (sum == k) { p = i; break; } sum++; } for (int i = p; i <= n; i++) if (a[i] >= ans) cout << i << " "; for (int i = 1; i < p; i++) if (a[i] > ans) cout << i << " "; }
2
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int q; cin >> q; vector<int> v; for(int i=0; i<q; i++) { int a; cin >> a; if(a==0) { int b; cin >> b; v.push_back(b); } else if(a==1) { int b; cin >> b; cout << v[b] << endl; } else { v.pop_back(); } } }
0
#include <algorithm> #include <iostream> using namespace std; int main() { int n; cin >> n; int a[100005]; for(int i = 1; i <= n; i++) cin >> a[i]; a[n + 1] = 0; sort(a + 1, a + n + 1, greater<int>()); int f = 0, s = 0; for(int i = 1; i <= n; i++){ if(a[i] > a[i + 1]){ if((i + a[i]) % 2) f = max(f, min(i, a[i])); else s = max(s, min(i, a[i])); } } if(f >= s) cout << "First" << endl; else cout << "Second" << endl; }
0
#include<iostream> using namespace std; int n, p; int dp[101][76]; bool isp(int n){ for (int i = 2; i * i <= n; ++i)if (n % i == 0)return 0; return 1; } int main(){ cin >> n; dp[0][1] = 1; for (int i = 2; i <= n; ++i){ if (isp(i) == 0)continue; int cnt = 0; for (int j = 1; j <= n; ++j){ int a = j; while (a % i == 0)cnt++, a /= i; } for (int j = 0; j <= cnt; ++j){ for (int k = 0; k * (j + 1) <= 75; k++){ dp[i][k * (j + 1)] += dp[p][k]; } } p = i; } cout << dp[p][75] << endl; }
0
#include <bits/stdc++.h> using namespace std; int gi() { int res = 0, w = 1; char ch = getchar(); while (ch != '-' && !isdigit(ch)) ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar(); return res * w; } const int INF = 1e9; const int MAX_N = 2e5 + 5; int N, a[MAX_N], val[MAX_N]; #define lson (o << 1) #define rson (o << 1 | 1) struct SGT { int mx[MAX_N << 2]; void pushup(int o) { mx[o] = max(mx[lson], mx[rson]); } void build(int o = 1, int l = 1, int r = N) { if (l == r) return (void)(mx[o] = -INF); int mid = (l + r) >> 1; build(lson, l, mid), build(rson, mid + 1, r); pushup(o); } void mdf(int pos, int v, int o = 1, int l = 1, int r = N) { if (l == r) return (void)(mx[o] = v); int mid = (l + r) >> 1; if (pos <= mid) mdf(pos, v, lson, l, mid); else mdf(pos, v, rson, mid + 1, r); pushup(o); } int qry(int ql, int qr, int o = 1, int l = 1, int r = N) { if (ql <= l && r <= qr) return mx[o]; int mid = (l + r) >> 1, res = -INF; if (ql <= mid) res = max(res, qry(ql, qr, lson, l, mid)); if (qr > mid) res = max(res, qry(ql, qr, rson, mid + 1, r)); return res; } } t[2]; int cnt[MAX_N]; bool check(int mx, int v) { if (v < 0) return 0; int len = t[v & 1].qry(mx, N); return len >= v; } int main () { N = gi(); for (int i = 1, mx = 0; i <= N; i++) { a[i] = gi(); if (a[i] > mx) mx = a[i], val[i] = 2; else val[i] = 1; } t[1].build(); for (int i = N; i; i--) { int mx0 = t[0].qry(a[i], N), mx1 = t[1].qry(a[i], N); if (val[i] & 1) t[0].mdf(a[i], mx1 + 1), t[1].mdf(a[i], mx0 + 1); else t[0].mdf(a[i], mx0 + 2), t[1].mdf(a[i], mx1 + 2); } for (int i = N; i; i--) cnt[i] = cnt[i + 1] + (val[i] == 2); vector<int> ans; int cntX = 0, cntY = 0, mxX = 0, mxY = 0; for (int i = 1; i <= N; i++) { t[1].mdf(a[i], -INF), t[0].mdf(a[i], 0); int _1 = cntX, _2 = mxX; if (a[i] > mxX) mxX = a[i], ++cntX; if (check(mxY, cntX + cnt[i + 1] - cntY) || check(mxX, cntY + cnt[i + 1] - cntX)) ans.push_back(0); else { cntX = _1, mxX = _2; if (a[i] > mxY) mxY = a[i], ++cntY; ans.push_back(1); } } if (cntX != cntY) return puts("-1") & 0; for (int i = 0; i < (int)ans.size(); i++) putchar(ans[i] + '0'); putchar('\n'); return 0; }
0
#include <iostream> using namespace std; int main() { int N, K; string S; cin >> N >> S >> K; char c = S[K - 1]; for (auto &s : S) { s = (s == c ? s : '*'); } cout << S << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } vector<long long> v; void br(long long m, long long w) { while (m > 0) { v.push_back(m % w); m /= w; } } void prog(int i, long long w) { int crr = 1; while (i < v.size() && crr != 0) { v[i] += crr; crr = (v[i] / w); v[i] %= w; i++; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long w, m; cin >> w >> m; br(m, w); v.push_back(0); for (int i = 0; i < v.size(); i++) { if (v[i] == 0 || v[i] == 1) continue; if (w - v[i] == 1) { prog(i, w); } else { cout << "NO"; return 0; } } cout << "YES"; }
3
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFLL = 0x3f3f3f3f3f3f3f3fLL; const int mod = 1e9 + 7; const int MX = 2e5 + 5; struct Edge { int u, v, w, nxt; } E[MX * 5]; int head[MX], rear; void edge_add(int u, int v, int w = 0) { E[rear] = Edge{u, v, w, head[u]}; head[u] = rear++; } void init() { memset(head, -1, sizeof(head)); rear = 0; } namespace BCC { struct edge { int u, v; edge(int u, int v) : u(u), v(v) {} }; stack<edge> s; struct Edge { int v, nxt; } E[MX * 5]; int n, m; int tot, head[MX]; int pre[MX]; int dfs_clock; int iscut[MX]; int bcc_cnt; int bccno[MX]; vector<int> bcc[MX]; void edge_add(int u, int v) { E[tot] = Edge{v, head[u]}; head[u] = tot++; } void init(int n) { for (int i = 1; i <= n; i++) { pre[i] = iscut[i] = bccno[i] = 0; head[i] = -1; } tot = dfs_clock = bcc_cnt = 0; } int dfs(int u, int fa) { int lowu = pre[u] = ++dfs_clock; int child = 0; for (int i = head[u]; ~i; i = E[i].nxt) { int v = E[i].v; edge e = (edge){u, v}; if (!pre[v]) { s.push(e); child++; int lowv = dfs(v, u); lowu = min(lowu, lowv); if (lowv >= pre[u]) { iscut[u] = 1; bcc_cnt++; bcc[bcc_cnt].clear(); while (1) { edge first = s.top(); s.pop(); if (bccno[first.u] != bcc_cnt) { bcc[bcc_cnt].push_back(first.u); bccno[first.u] = bcc_cnt; } if (bccno[first.v] != bcc_cnt) { bcc[bcc_cnt].push_back(first.v); bccno[first.v] = bcc_cnt; } if (first.u == u && first.v == v) break; } } } else if (pre[v] < pre[u] && v != fa) { s.push(e); lowu = min(lowu, pre[v]); } } if (fa < 0 && child == 1) iscut[u] = 0; return lowu; } } // namespace BCC int par[MX * 2]; int find(int first) { return par[first] == first ? first : (par[first] = find(par[first])); } void rebuild(int n, int m, pair<int, int> Edge[]) { init(); int cnt = BCC::bcc_cnt; for (int i = 1; i < (n + cnt + 1); ++i) par[i] = i; for (int i = 1; i < (cnt + 1); ++i) for (int j = 0; j < (BCC::bcc[i].size()); ++j) { edge_add(BCC::bcc[i][j], n + i, 1); edge_add(n + i, BCC::bcc[i][j], 1); if (j) { int p1 = find(BCC::bcc[i][j]), p2 = find(BCC::bcc[i][j - 1]); if (p1 != p2) par[p1] = p2; } } for (int i = 0; i < (m); ++i) { int u = Edge[i].first, v = Edge[i].second; int p1 = find(u), p2 = find(v); if (p1 != p2) { par[p1] = p2; edge_add(u, v, 2); edge_add(v, u, 2); } } } namespace LCA { int sz, ver[2 * MX], deep[2 * MX], fir[MX], dp[2 * MX][30], vis[MX], dir[MX]; void init(int n) { for (int i = 0; i <= n; i++) dir[i] = vis[i] = 0; sz = 0; } void dfs(int u, int dep) { vis[u] = 1; ver[++sz] = u; fir[u] = sz; deep[sz] = dep; for (int i = head[u]; ~i; i = E[i].nxt) { if (vis[E[i].v]) continue; int v = E[i].v; dir[v] = dir[u] + E[i].w; dfs(v, dep + 1); ver[++sz] = u; deep[sz] = dep; } } void ST(int n) { for (int i = 1; i <= n; i++) dp[i][0] = i; for (int j = 1; (1 << j) <= n; j++) { for (int i = 1; i + (1 << j) - 1 <= n; i++) { int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1]; dp[i][j] = deep[a] < deep[b] ? a : b; } } } int RMQ(int l, int r) { int k = 0; while ((1 << (k + 1)) <= r - l + 1) k++; int a = dp[l][k], b = dp[r - (1 << k) + 1][k]; return deep[a] < deep[b] ? a : b; } int LCA(int u, int v) { int first = fir[u], second = fir[v]; if (first > second) swap(first, second); int ret = RMQ(first, second); return ver[ret]; } void pre_solve(int n) { init(n); dir[1] = 1; dfs(1, 1); ST(sz); } int get_dis(int u, int v) { return dir[u] + dir[v] - 2 * dir[LCA(u, v)]; } } // namespace LCA pair<int, int> p[MX * 5]; int main() { int n, m, q; cin >> n >> m >> q; int u, v; BCC::init(n); for (int i = 0; i < (m); ++i) { scanf("%d%d", &u, &v); p[i] = pair<int, int>(u, v); BCC::edge_add(u, v); BCC::edge_add(v, u); } BCC::dfs(1, -1); rebuild(n, m, p); LCA::pre_solve(n + BCC::bcc_cnt); for (int i = 0; i < (q); ++i) { scanf("%d%d", &u, &v); printf("%d\n", LCA::get_dis(u, v) / 2); } return 0; }
2
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e9+7; ll ksm(ll a,ll b) { ll ans=1; while(b) { if(b&1) ans=ans*a%maxn; a=a*a%maxn; b>>=1; } return ans; } ll kl(ll a,ll b) { ll ca=1,cb=1; if(a<b) { return 0; } else if(a==b) { return 1; } else { for(ll i=0;i<b;i++) { ca=ca*(a-i)%maxn; cb=cb*(b-i)%maxn; } } return ca*ksm(cb,maxn-2)%maxn; } int main() { ll x,y,m,n,k; scanf("%lld%lld",&x,&y); if((x+y)%3!=0) { printf("0\n"); } else { m=(2*y-x)/3; n=(2*x-y)/3; if(m<0||n<0) { printf("0\n"); } else printf("%lld\n",kl(m+n,m)); } return 0; }
0
#include <bits/stdc++.h> using namespace std; string a; int b; int long long c = 1; int long long ans = 0; int main() { cin >> a; b = a.length() - 1; for (int x = 0; x <= b; x++) { int long long t = 1; if (a[x] == '1') { for (int y = 0; y < 2 * b - x; y++) { t *= 2; t %= 1000000007; } ans += t; ans %= 1000000007; } } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a, b, n, m, i; char s[110], c, d; scanf("%d%d", &n, &m); scanf("%s", s + 1); while (m) { scanf("%d%d", &a, &b); cin >> c >> d; for (i = a; i <= b; i++) { if (s[i] == c) s[i] = d; } m--; } for (i = 1; i <= n; i++) printf("%c", s[i]); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<long long int> getPrimes(long long int n) { bool prime[n + 1]; vector<long long int> primes; for (long long int p = 2; p * p <= n; p++) if (!prime[p]) for (long long int i = p * p; i <= n; i += p) prime[i] = true; for (long long int p = 2; p <= n; p++) if (!prime[p]) primes.push_back(p); return primes; } int main() { long long int t, i, j, temp, last, ans, en = 1000000000000000000, g; vector<long long int> primes = getPrimes(100000); long long int l = primes.size(); vector<pair<long long int, pair<long long int, long long int>>> elem; cin >> t; while (t--) { ans = 1; i = 22; j = 0; temp = last = 1; while (i--) { while (j < l && (en / temp) >= primes[j]) temp *= primes[j++]; cout << "? " << temp << "\n" << flush; cin >> g; if (g == last) ans *= g, temp = 1; else temp = g * g; last = g; g = 1; } g *= ans; ans = 1; for (i = 0; i < j; i++) { temp = 1; while ((g % primes[i]) == 0) temp++, g /= primes[i]; ans *= temp; } ans *= 2ll; cout << "! " << ans << "\n" << flush; } return 0; }
6
#include <bits/stdc++.h> using namespace std; struct DS { vector<int> p; vector<int> c; DS(int n) : p(n, 0), c(n, 1) { for (int i = 0; i < n; ++i) p[i] = i; } int SetOf(int x) { return (p[x] == x) ? x : p[x] = SetOf(p[x]); } bool Merge(int x, int y) { x = SetOf(x); y = SetOf(y); if (x == y) return false; if (c[x] < c[y]) swap(x, y); p[y] = x; c[x] += c[y]; return true; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, n, m, k, a, b, s; cin >> n >> m >> k; vector<int> vol(k); for (auto &x : vol) cin >> x; vector<vector<long long> > ady(n + 1); while (m--) { cin >> a >> b; ady[a].push_back(b); ady[b].push_back(a); } cin >> s >> t; vol.push_back(t); int cur = 0; queue<int> q; vector<int> d(n + 1, -1); for (auto &x : vol) { q.push(x); d[x] = 0; } d[s] = 0; DS sets(n + 1); for (; !q.empty(); q.pop()) { int cur = q.front(); for (auto &x : ady[cur]) { if (d[x] == -1) { d[x] = d[cur] + 1; q.push(x); sets.Merge(x, cur); } } } auto solve = [&](int mx) { DS ds = sets; for (int i = 1; i <= n; ++i) { for (auto &x : ady[i]) { if (ds.SetOf(i) != ds.SetOf(x) && d[x] + d[i] + 1 <= mx) { assert(max(d[x], d[i]) <= mx / 2); ds.Merge(i, x); } } } return (ds.SetOf(s) == ds.SetOf(t)); }; int ans = -1; int base = -1, top = n, mid; while (base + 1 < top) { mid = (base + top) / 2; if (solve(mid)) top = mid; else base = mid; } if (solve(top)) ans = top; cout << ans << "\n"; return 0; }
5
#include<bits/stdc++.h> using namespace std; int main(){ int ans=0,a; for(int i=0;i<5;i++){ cin>>a; if(a<40){ a=40; } ans+=a; } cout<<ans/5<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100002; int n, cnt[N]; vector<vector<int> > g(N); long long ans; void dfs(int i, int p) { cnt[i] = 1; long long sum = 0; for (int j : g[i]) if (j != p) { dfs(j, i); sum += 1ll * cnt[j] * cnt[i]; cnt[i] += cnt[j]; } ans -= sum * sum; ans -= sum * cnt[i] * (n - cnt[i]) * 2; } int main() { scanf("%d", &n); for (int i = 2; i <= n; ++i) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } ans = n * (n - 1ll) / 2; ans *= ans; dfs(1, 1); cout << ans; }
4
#include<bits/stdc++.h> using namespace std; #define LL long long #define MOD 998244353 LL N, M, K; LL qpow(LL base, LL pow) { return pow?((pow&1?base:1)*qpow(base*base%MOD, pow/2))%MOD:1; } LL qpow2(LL base, LL pow) { LL res = 1; while(pow) { if(pow&1) { res = res*base%MOD; } base = base*base%MOD; pow /= 2; } return res%MOD; } int main() { cin >> N >> M >>K; LL ans = 0; LL temp = 1; for(int k=0; k<=K; k++) { ans += ((M * temp)%MOD * qpow(M-1, N-1-k))%MOD; ans %= MOD; temp = (temp * (N-k-1))%MOD * qpow(k+1, MOD-2); temp %= MOD; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int dx[8] = {0, -1, 0, 1, -1, -1, 1, 1}, dy[8] = {1, 0, -1, 0, -1, 1, -1, 1}; struct node { int x, y; } a[1000005]; int n, p[1000005]; bool vis[1000005]; bool cmp(node x, node y) { if (x.y == y.y) return x.x < y.x; return x.y < y.y; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].x >> a[i].y; if (a[i].x > a[i].y) swap(a[i].x, a[i].y); } sort(a + 1, a + 1 + n, cmp); int num = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { p[++num] = a[i].y; vis[i] = true; for (int j = i; j <= n; j++) if (a[i].y >= a[j].x) { vis[j] = true; } } } printf("%d\n", num); for (int i = 1; i <= num; i++) printf("%d%c", p[i], i == num ? '\n' : ' '); return 0; }
4
#include <bits/stdc++.h> using namespace std; int rui[2005][2005] = {}, n, m; char first[2005][2005]; int main() { srand((unsigned int)time(NULL)); scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", &first[i]); int lx = 2000000000, ly = 2000000000, rx = -2000000000, ry = -2000000000; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i) rui[i][j] += rui[i - 1][j]; if (j) rui[i][j] += rui[i][j - 1]; if (i && j) rui[i][j] -= rui[i - 1][j - 1]; rui[i][j] += (first[i][j] == 'w'); if (first[i][j] == 'w') { lx = min(i, lx); ly = min(j, ly); rx = max(i, rx); ry = max(j, ry); } } } int unit = max(rx - lx, ry - ly) + 1; if (unit == 1) { for (int i = 0; i < n; i++) printf("%s\n", first[i]); return 0; } for (int i = 0; i <= n - unit; i++) { for (int j = 0; j <= m - unit; j++) { int x = rui[i + unit - 1][j + unit - 1] - rui[i - 1][j + unit - 1] - rui[i + unit - 1][j - 1] + rui[i - 1][j - 1]; int y = rui[i + unit - 2][j + unit - 2] - rui[i][j + unit - 2] - rui[i + unit - 2][j] + rui[i][j]; if (x - y == rui[n - 1][m - 1]) { for (int k = j; k <= j + unit - 1; k++) { if (first[i][k] != 'w') first[i][k] = '+'; if (first[i + unit - 1][k] != 'w') first[i + unit - 1][k] = '+'; } for (int k = i; k <= i + unit - 1; k++) { if (first[k][j] != 'w') first[k][j] = '+'; if (first[k][j + unit - 1] != 'w') first[k][j + unit - 1] = '+'; } for (int i = 0; i < n; i++) printf("%s\n", first[i]); return 0; } } } puts("-1"); }
4
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; int64_t fx[10] = {0, 1, 0, -1, 1, -1, 1, -1}; int64_t fy[10] = {1, 0, -1, 0, 1, -1, -1, 1}; const long long M = 2e5 + 10, M2 = 2e6 + 10, oo = 1e9 + 7, mod = 998244353; const long long MC = (1 << 20) + 5; int64_t pos[M], dp[2][M]; void precomp() {} void solve() { int64_t n; cin >> n; int64_t ara[n + 10]; ara[0] = 0; for (int64_t i = 1; i <= n; i++) { cin >> ara[i]; } ara[n + 1] = 0; long long ans = 0; for (int64_t i = n + 1; i >= 1; i--) { vector<int64_t> all; for (int64_t block = 1; block * block <= ara[i]; block++) { all.push_back(block); if (block < ara[i] / block) all.push_back(ara[i] / block); } if (i > 1) { dp[!(i & 1)][ara[i - 1]] = 1; for (int64_t b : all) { int64_t subs = dp[i & 1][b] * (i - 1), splits = ((ara[i - 1] + b - 1) / b) - 1; int64_t prev = ara[i - 1] / (splits + 1); dp[!(i & 1)][prev] += dp[i & 1][b]; ans = (ans + (subs * splits) % mod) % mod; } } for (int64_t b : all) dp[i & 1][b] = 0; } cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); precomp(); int64_t T; cin >> T; for (int64_t tc = 1; tc <= T; tc++) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 2000; const int dr[] = {3, 2, 1, 0, -3, -2, -1, 0, 2, 1, 0, -3, -2, -1}; const int dc[] = {0, 1, 2, 3, 0, 1, 2, 3, -1, -2, -3, 0, -1, -2}; const int MAXN = 2000; const int INF = 0x3f3f3f3f; long long n, m; int cnt; int g[maxn][maxn]; map<pair<int, int>, int> id; int ID(pair<int, int> p) { if (id.count(p)) return id[p]; return id[p] = ++cnt; } int Nx, Ny; void build() { for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { for (int k = 0; k < 14; ++k) { int ni = i + dr[k], nj = j + dc[k]; if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue; g[ID(pair<int, int>(i, j))][ID(pair<int, int>(ni, nj))] = 1; Ny++; } } } bool flag; int Mx[MAXN], My[MAXN]; int dx[MAXN], dy[MAXN], dis; bool vst[MAXN]; bool searchP(void) { queue<int> Q; dis = INF; memset(dx, -1, sizeof(dx)); memset(dy, -1, sizeof(dy)); for (int i = 1; i <= Nx; i++) if (Mx[i] == -1) { Q.push(i); dx[i] = 0; } while (!Q.empty()) { int u = Q.front(); Q.pop(); if (dx[u] > dis) break; for (int v = 1; v <= Ny; v++) if (g[u][v] && dy[v] == -1) { dy[v] = dx[u] + 1; if (My[v] == -1) dis = dy[v]; else { dx[My[v]] = dy[v] + 1; Q.push(My[v]); } } } return dis != INF; } bool DFS(int u) { for (int v = 1; v <= Ny; v++) if (!vst[v] && g[u][v] && dy[v] == dx[u] + 1) { vst[v] = 1; if (My[v] != -1 && dy[v] == dis) continue; if (My[v] == -1 || DFS(My[v])) { My[v] = u; Mx[u] = v; return 1; } } return 0; } int MaxMatch(void) { int res = 0; memset(Mx, -1, sizeof(Mx)); memset(My, -1, sizeof(My)); while (searchP()) { memset(vst, 0, sizeof(vst)); for (int i = 1; i <= Nx; i++) if (Mx[i] == -1 && DFS(i)) res++; } return res; } void init() { memset(g, 0, sizeof g); id.clear(); memset(vst, 0, sizeof vst); memset(dx, -1, sizeof(dx)); memset(dy, -1, sizeof(dy)); memset(Mx, -1, sizeof(Mx)); memset(My, -1, sizeof(My)); cnt = 0; } int main() { cin >> n >> m; if (n > m) swap(n, m); if (n == 1) { cout << 6 * (m / 6) + 2 * max(m % 6 - 3, 1ll * 0); } else if (n <= 20 && m <= 20) { Nx = n * m; Ny = 0; build(); cout << MaxMatch() << endl; } else { if (n % 2 == 0 && m % 2 == 0) cout << n * m << endl; else if (n % 2 == 0 && m % 2 || n % 2 && m % 2 == 0) cout << n * m << endl; else { cout << n * m - 1 << endl; } } return 0; }
2
#include<iostream> #include<vector> #include<algorithm> using namespace std; bool check(int n,int wei,vector<int> book) { int tmp=wei; n--; for(int i=0;i<book.size();i++) { if(tmp<book[i]) { if(n>0) { n--; tmp=wei; if(tmp<book[i]) return false; } else return false; } // cout<<n<<" "<<tmp<<" "<<book[i]<<endl; tmp-=book[i]; } return true; } int main() { int m,n; for(;cin>>m>>n,m;) { vector<int> book; for(int i=0;i<n;i++) { int tmp; cin>>tmp; book.push_back(tmp); } /* while(true) { int tmp; cin>>tmp; if(check(m,tmp,book)) cout<<"YES"<<endl; else cout<<"NO"<<endl; } */ int l=0; int r=1500000; int ans; while(l<r) { // cout<<l<<" "<<r<<endl; // cout<<(l+r)/2<<endl; if(check(m,(l+r)/2,book)) { ans=(l+r)/2; if(r==(l+r)/2) break; r=(l+r)/2; } else { if(l==(l+r)/2) break; l=(l+r)/2; } } cout<<ans<<endl; } }
0
#include <iostream> using namespace std; typedef long long ll; ll n,a,b,ans=-1,tot; int main(void){ cin>>n; for(int i=0;i<n;i++){ cin>>a>>b; ans+=b; tot+=(a*b); } ans+=(tot-1)/9; cout<<ans<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; int i; for (i = 0; i < n; i++) { if (s[i] == 'A') { break; } } int cnt = 0; int mx = INT_MIN; for (i = i + 1; i < n; i++) { if (s[i] == 'P') { cnt++; } else { mx = max(cnt, mx); cnt = 0; } } mx = max(cnt, mx); cout << mx << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class num> inline void rd(num &x) { char c; while (isspace(c = getchar())) ; bool neg = false; if (!isdigit(c)) neg = (c == '-'), x = 0; else x = c - '0'; while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - '0'; if (neg) x = -x; } vector<pair<int, int> > v; int dp[105][70000]; int solve(int p, int r) { int i = p; if (r < 0) return -1212313; if (p == v.size()) return 0; int &ret = dp[p][r]; if (ret != -1) return ret; if (r >= v[i].first) return ret = max(solve(p + 1, r), solve(p + 1, r - v[i].second) * (r >= v[i].first) + 1); return ret = solve(p + 1, r); } int main(void) { memset(dp, -1, sizeof dp); int n, r; rd(n); rd(r); int ans = 0; vector<pair<int, int> > w; for (int i = 0; i < n; i++) { int r, b; rd(r); rd(b); if (b >= 0) w.push_back({r, b}); else v.push_back({r, -b}); } sort(begin(w), end(w)); for (auto x : w) if (r >= x.first) { ans++; r += x.second; } sort(begin(v), end(v), [](pair<int, int> a, pair<int, int> b) { return a.first - a.second > b.first - b.second; }); cout << ans + solve(0, r) << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 200005; struct Query { int k, pos, id; } q[N]; vector<pair<int, int> > v; int n, m, a[N], res[N], bit[N]; bool cmp(pair<int, int> first, pair<int, int> second) { if (first.first == second.first) return first.second < second.second; return first.first > second.first; } bool cmp2(Query first, Query second) { return first.k < second.k; } struct OrderSet { struct Trie { int cnt; int next[2]; Trie() { memset(next, -1, sizeof(next)); cnt = 0; } }; vector<Trie*> trie; OrderSet() { auto u = new Trie(); trie.push_back(u); } bool find(int first) { int cur = 0; for (int i = 30; i >= 0; i--) { auto u = (first >> i) & 1; if (trie[cur]->next[u] == -1) return 0; else cur = trie[cur]->next[u]; } return 1; } void insert(int first) { int cur = 0; trie[cur]->cnt++; for (int i = 30; i >= 0; i--) { auto u = (first >> i) & 1; if (trie[cur]->next[u] == -1) { auto newTrie = new Trie(); trie.push_back(newTrie); trie[cur]->next[u] = trie.size() - 1; cur = trie[cur]->next[u]; } else { cur = trie[cur]->next[u]; } trie[cur]->cnt++; } } void deletee(int first) { int cur = 0; trie[cur]->cnt--; for (int i = 30; i >= 0; i--) { auto u = (first >> i) & 1; int temp = trie[cur]->next[u]; if (trie[temp]->cnt == 1) trie[cur]->next[u] = -1; cur = temp; trie[cur]->cnt--; } } int order_of_key(int first) { int cur = 0; int res = 0; for (int i = 30; i >= 0; i--) { auto u = (first >> i) & 1; if (u == 1 && trie[cur]->next[0] != -1) { int temp = trie[cur]->next[0]; res += trie[temp]->cnt; } if (trie[cur]->next[u] == -1) return res; cur = trie[cur]->next[u]; } return res; } int find_by_order(int k) { int cur = 0; int res = 0; if (trie[cur]->cnt < k) return -1; for (int i = 30; i >= 0; i--) { int temp = trie[cur]->next[0]; if (temp == -1) { cur = trie[cur]->next[1]; res += (1 << i); continue; } if (trie[temp]->cnt < k) { k -= trie[temp]->cnt; cur = trie[cur]->next[1]; res += (1 << i); } else { cur = trie[cur]->next[0]; } } return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); OrderSet os; cin >> n; v.push_back({1e9 + 2, 1e9}); for (int i = 1; i <= n; i++) { cin >> a[i]; v.push_back({a[i], i}); } sort(v.begin(), v.end(), cmp); cin >> m; for (int i = 1; i <= m; i++) { cin >> q[i].k >> q[i].pos; q[i].id = i; } sort(q + 1, q + m + 1, cmp2); int cur = 0; for (int i = 1; i <= m; i++) { while (cur < q[i].k) { cur++; os.insert(v[cur].second); } res[q[i].id] = os.find_by_order(q[i].pos); } for (int i = 1; i <= m; i++) cout << a[res[i]] << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; int jie[200010], tree[200010], num[200010]; int n; int lowbit(int x) { return x & (-x); } void update(int x, int date) { for (; x <= n; x += lowbit(x)) { tree[x] += date; } } int query(int x) { int sum = 0; for (; x > 0; x -= lowbit(x)) sum += tree[x]; return sum; } int find(int k) { int l = 1, r = n, mi, p; while (l < r) { mi = (l + r) / 2; p = query(mi); if (p < k) l = mi + 1; else r = mi; } update(l, -1); return l; } int main() { int T, t, i, j, u, p; int k, m; scanf("%d", &n); for (i = 1; i <= n; i++) update(i, 1); for (i = n - 1; i >= 0; i--) { scanf("%d", &u); u++; update(u, -1); m = query(u); num[i] += m; } for (i = 1; i <= n; i++) update(i, 1); for (i = n - 1; i >= 0; i--) { scanf("%d", &u); u++; update(u, -1); m = query(u); num[i] += m; } for (i = 1; i < n; i++) { k = num[i] / (i + 1); num[i] %= (i + 1); num[i + 1] += k; } for (i = 1; i <= n; i++) update(i, 1); for (i = n - 1; i >= 1; i--) { p = find(num[i] + 1) - 1; printf("%d ", p); } printf("%d\n", find(1) - 1); }
2
#include <bits/stdc++.h> namespace MyMinMax { template <typename T> inline T mn(T x, T y) { return x < y ? x : y; } template <typename T> inline T mx(T x, T y) { return x > y ? x : y; } template <typename T> inline void chmin(T &x, T y) { (x > y) && (x = y); } template <typename T> inline void chmax(T &x, T y) { (x < y) && (x = y); } } // namespace MyMinMax using namespace MyMinMax; namespace IO { char BB[1 << 22], *SS = BB, *TT = BB; template <typename T> inline void read(T &x) { x = 0; int fu = 1; char c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 22, stdin), SS == TT) ? EOF : *SS++); while (c > 57 || c < 48) { if (c == 45) fu = -1; c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 22, stdin), SS == TT) ? EOF : *SS++); } while (c <= 57 && c >= 48) { x = x * 10 + c - 48; c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 22, stdin), SS == TT) ? EOF : *SS++); } x *= fu; } template <typename T, typename... Args> inline void read(T &x, Args &...args) { read(x); read(args...); } char out[1 << 22], *Out = out; inline void putcha(char x) { *Out++ = x; if (Out - out >= (1 << 22)) fwrite(out, 1, Out - out, stdout), Out = out; } template <typename T> inline void fprint(T x) { if (x < 0) putcha(45), x = -x; if (x > 9) fprint(x / 10); putcha(x % 10 + 48); } template <typename T> inline void print(T x) { fprint(x); putcha(10); } inline void print(const char *ch) { while (*ch != '\0') putcha(*(ch++)); } inline void put(const char *ch) { while (*ch != '\0') putcha(*(ch++)); putcha(10); } template <typename T, typename... Args> inline void print(T x, Args... args) { fprint(x); putcha(32); print(args...); } template <typename... Args> inline void print(const char *ch, Args... args) { while (*ch != '\0') putcha(*(ch++)); putcha(32); print(args...); } template <typename T> inline void sprint(T x) { fprint(x); putcha(32); } template <typename T, typename... Args> inline void sprint(T x, Args... args) { fprint(x); putcha(32); sprint(args...); } class AutoFlush { public: ~AutoFlush() { fwrite(out, 1, Out - out, stdout); } } __AutoFlush; } // namespace IO using namespace IO; int ans[100005]; int tot, n, m; struct NODE { int x, y; long long val; NODE() {} NODE(int x, int y, long long val) : x(x), y(y), val(val) {} inline int num() { return ((x & 1) << 1) | (y & 1); } } e[100005 << 2], g[100005 << 2]; long long BIT[4][1005]; inline int lowbit(int x) { return x & -x; } inline void modify(long long c[], int x, long long y) { for (; x <= n; x += lowbit(x)) c[x] ^= y; } inline long long query(long long c[], int x) { long long ret = 0; for (; x; x -= lowbit(x)) ret ^= c[x]; return ret; } void CDQ(int l, int r) { if (l == r) return; int mid = (l + r) >> 1; CDQ(l, mid); CDQ(mid + 1, r); int i = l, j = mid + 1, k = l; while (i <= mid && j <= r) { if (e[i].x <= e[j].x) { if (e[i].val >= 0) modify(BIT[e[i].num()], e[i].y, e[i].val); g[k++] = e[i++]; } else { if (e[j].val < 0) ans[-e[j].val] ^= query(BIT[e[j].num()], e[j].y); g[k++] = e[j++]; } } while (j <= r) { if (e[j].val < 0) ans[-e[j].val] ^= query(BIT[e[j].num()], e[j].y); g[k++] = e[j++]; } for (int t = l; t < i; ++t) if (e[t].val >= 0) modify(BIT[e[t].num()], e[t].y, e[t].val); while (i <= mid) g[k++] = e[i++]; for (int t = l; t <= r; ++t) e[t] = g[t]; } bool b[100005]; int main() { read(n, m); for (int i = 1; i <= m; ++i) { int opt, x1, y1, x2, y2; long long w; read(opt, x1, y1, x2, y2); if (opt == 2) { read(w); e[++tot] = NODE(x1, y1, w); e[++tot] = NODE(x1, y2 + 1, w); e[++tot] = NODE(x2 + 1, y1, w); e[++tot] = NODE(x2 + 1, y2 + 1, w); } else { b[i] = 1; e[++tot] = NODE(x1 - 1, y1 - 1, -i); e[++tot] = NODE(x1 - 1, y2, -i); e[++tot] = NODE(x2, y1 - 1, -i); e[++tot] = NODE(x2, y2, -i); } } memset(ans, 0, sizeof(ans)); CDQ(1, tot); for (int i = 1; i <= m; ++i) if (b[i]) print(ans[i]); }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:268435456") using namespace std; template <class T> T sqr(T a) { return a * a; } int a[100010]; map<int, int> b; int n, k; int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n; i++) { if (a[i] % k == 0 && b.find(a[i] / k) != b.end()) { b[a[i]] = b[a[i] / k] + 1; b.erase(a[i] / k); } else b[a[i]] = 1; } int result = 0; for (map<int, int>::iterator iter = b.begin(); iter != b.end(); iter++) result += (iter->second + 1) / 2; cout << result; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; void testcase() { int n, m; cin >> n >> m; if (m % 2) { for (int i = 1; i <= n; i++) { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; } cout << "NO" << endl; } else { int check = 0; for (int i = 1; i <= n; i++) { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (a2 == a3) check = 1; } if (check) cout << "YES" << endl; else cout << "NO" << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { testcase(); } }
2
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T, class T1> inline void gn(T &first, T1 &second) { gn(first); gn(second); } template <class T, class T1, class T2> inline void gn(T &first, T1 &second, T2 &z) { gn(first); gn(second); gn(z); } template <class T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <class T> inline void printsp(T first) { print(first); putchar(' '); } template <class T> inline void println(T first) { print(first); putchar('\n'); } template <class T, class U> inline void print(T first, U second) { printsp(first); println(second); } template <class T, class U, class V> inline void print(T first, U second, V z) { printsp(first); printsp(second); println(z); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } char s[202020]; int cnt[2][2][202020], cc[202020]; pair<int, int> ans[202020]; pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { return pair<int, int>(a.first + b.first, a.second + b.second); } int can(int k, int m) { if (m & 1) { if (cnt[k & 1][1][k] - cnt[k & 1][1][k - m - 1] || cnt[~k & 1][0][k - 1] - cnt[~k & 1][0][k - m]) return 0; } else { if (cnt[k & 1][0][k] - cnt[k & 1][0][k - m] || cnt[~k & 1][1][k - 1] - cnt[~k & 1][1][k - m - 1]) return 0; } return 1; } int main() { int n, m; gn(n); scanf("%s", s + 2); gn(m); for (int i = 2; i <= n + 1; i++) { cnt[i & 1][0][i] = cnt[i & 1][0][i - 2] + (s[i] == 'a'); cnt[i & 1][1][i] = cnt[i & 1][1][i - 2] + (s[i] == 'b'); cc[i] = cc[i - 1] + (s[i] == '?'); } for (int i = m + 1; i <= n + 1; i++) { ans[i] = ans[i - 1]; if (can(i, m)) { if (ans[i].first < ans[i - m].first + 1) { ans[i] = ans[i - m] + pair<int, int>(1, cc[i] - cc[i - m]); } else if (ans[i].first == ans[i - m].first + 1) { smin(ans[i], ans[i - m] + pair<int, int>(1, cc[i] - cc[i - m])); } } } println(ans[n + 1].second); return 0; }
5
#include <bits/stdc++.h> using namespace std; void yes() { cout << "YES" << '\n'; } void no() { cout << "NO" << '\n'; } int gcd(unsigned long long a, unsigned long long b) { if (b == 0) return a; return gcd(b, a % b); } bool compare(pair<unsigned long long, int> &a, pair<unsigned long long, int> &b) { return a.first <= b.first; } struct s { int x, y; }; bool check(unsigned long long x) { unsigned long long p = sqrt(x); p = p - 1 >= 0 ? p - 1 : p; while (p * p < x) { p++; } return p * p == x ? true : false; } int pow(int a, int b) { if (b == 0) return 1; unsigned long long y = pow(a, b / 2); if (b % 2) { return (((y * y) % 1000000007) * a) % 1000000007; } return (y * y) % 1000000007; } unsigned long long pow(int x) { if (x == 0) return 1; if (x & 1) return pow(x / 2) * pow(x / 2) * 2; return pow(x / 2) * pow(x / 2); } bool check_prime(int n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } void dfs(vector<vector<int>> &v, int i, bool visited[], stack<int> &s) { visited[i] = true; for (auto x : v[i]) { if (visited[x] == false) { dfs(v, x, visited, s); } } s.push(i); } void dfsutil(vector<vector<int>> &v, bool visited[], int i, vector<int> &ans) { visited[i] = true; ans.push_back(i + 1); for (auto x : v[i]) { if (visited[x] == false) { dfsutil(v, visited, x, ans); } } } const long long maxm = 2 * (long long)(1e5) + 2; void main_() { long long n, m; cin >> n >> m; long long a, b; cin >> a >> b; long long x, y; cin >> x >> y; long long p, q; p = (a <= x) ? (x - a) : (2 * n - a - x); q = (b <= y) ? (y - b) : (2 * m - b - y); cout << min(p, q) << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long test = 1; cin >> test; while (test--) { main_(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1e9; const ll maxn = 610; int a[maxn*maxn], b[maxn*maxn]; double f[maxn], ans; vector <int> vec[maxn]; ll n, m; double DP(double p) { for(int i = 1; i <= n; ++i) { f[i] = 0; } f[n] = 0; for(int i = n - 1; i >= 1; --i){ ll len = vec[i].size(); if(p == i && len == 1) { f[i] = inf; continue; } double t = 1.0 / (len - (p == i)); double mx = 0; for(int j = 0; j < len; ++j) { int v = vec[i][j]; mx = max(mx, (f[v]+1)*t); f[i] += (f[v] + 1) * t; } if(p == i) f[i] -= mx; } return f[1]; } int main () { cin >> n >> m; for(int i = 1; i <= m; ++i) { cin >> a[i] >> b[i]; vec[a[i]].push_back(b[i]); } ans = DP(0); for(int i = 1; i <= n; ++i) { ans = min(ans, DP(i)); } printf("%.9f\n", ans); }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 500 + 10; int T, n, m, b, mod, a[MAXN], dp[2][MAXN][MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); T = 1; while (T--) { cin >> n >> m >> b >> mod; for (int i = 1; i <= n; i++) { cin >> a[i]; } dp[0][0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= m; j++) { for (int k = 0; k <= b; k++) { dp[1][j][k] = dp[0][j][k]; if (j && a[i] <= k) { dp[1][j][k] = (dp[1][j][k] + dp[1][j - 1][k - a[i]]) % mod; } } } for (int j = 0; j <= m; j++) { for (int k = 0; k <= b; k++) { dp[0][j][k] = dp[1][j][k]; } } } int ans = 0; for (int i = 0; i <= b; i++) { ans = (ans + dp[1][m][i]) % mod; } cout << ans << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n]; vector<int> ind; for (int i = 0; i < n; i++) cin >> a[i]; int count = 0; while (k >= 0) { int m = 111; int i, ans; for (i = 0; i < n; i++) { if (a[i] < m) { m = a[i]; ans = i; } } if (m == 111) break; k -= m; a[ans] = 111; if (k >= 0) { count++; ind.push_back(ans); } } cout << count << "\n"; for (auto x : ind) cout << x + 1 << " "; return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using vi = vector<int>; const ld EPS = 1E-9; struct pt { ld x, y; }; struct seg { pt p, q; int id; ld get_y(ld x) const { if (abs(p.x - q.x) < EPS) return p.y; return p.y + (q.y - p.y) * (x - p.x) / (q.x - p.x); } }; inline bool intersect1d(ld l1, ld r1, ld l2, ld r2) { if (l1 > r1) swap(l1, r1); if (l2 > r2) swap(l2, r2); return max(l1, l2) <= min(r1, r2) + EPS; } inline int vec(const pt& a, const pt& b, const pt& c) { ld s = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); return abs(s) < EPS ? 0 : s > 0 ? +1 : -1; } bool intersect(const seg& a, const seg& b) { return intersect1d(a.p.x, a.q.x, b.p.x, b.q.x) && intersect1d(a.p.y, a.q.y, b.p.y, b.q.y) && vec(a.p, a.q, b.p) * vec(a.p, a.q, b.q) <= 0 && vec(b.p, b.q, a.p) * vec(b.p, b.q, a.q) <= 0; } bool operator<(const seg& a, const seg& b) { ld x = max(min(a.p.x, a.q.x), min(b.p.x, b.q.x)); return a.get_y(x) < b.get_y(x) - EPS; } struct event { ld x; int tp, id; event() {} event(ld x, int tp, int id) : x(x), tp(tp), id(id) {} bool operator<(const event& e) const { if (abs(x - e.x) > EPS) return x < e.x; return tp > e.tp; } }; set<seg> s; vector<set<seg>::iterator> where; inline set<seg>::iterator prev(set<seg>::iterator it) { return it == s.begin() ? s.end() : --it; } inline set<seg>::iterator next(set<seg>::iterator it) { return ++it; } pair<int, int> solve(const vector<seg>& a) { int n = (int)a.size(); vector<event> e; for (int i = 0; i < n; ++i) { e.push_back(event(min(a[i].p.x, a[i].q.x), +1, i)); e.push_back(event(max(a[i].p.x, a[i].q.x), -1, i)); } sort(e.begin(), e.end()); s.clear(); where.resize(a.size()); for (size_t i = 0; i < e.size(); ++i) { int id = e[i].id; if (e[i].tp == +1) { set<seg>::iterator nxt = s.lower_bound(a[id]), prv = prev(nxt); if (nxt != s.end() && intersect(*nxt, a[id])) return make_pair(nxt->id, id); if (prv != s.end() && intersect(*prv, a[id])) return make_pair(prv->id, id); where[id] = s.insert(nxt, a[id]); } else { set<seg>::iterator nxt = next(where[id]), prv = prev(where[id]); if (nxt != s.end() && prv != s.end() && intersect(*nxt, *prv)) return make_pair(prv->id, nxt->id); s.erase(where[id]); } } return make_pair(-1, -1); } int main() { ios::sync_with_stdio(0), cin.tie(0); int n; cin >> n; vector<pt> S(n), V(n); vi U(n); for (int i = 0; i < n; i++) { cin >> S[i].x >> S[i].y >> V[i].x >> V[i].y >> U[i]; ld len = sqrt(V[i].x * V[i].x + V[i].y * V[i].y); V[i].x /= len; V[i].y /= len; V[i].x *= U[i]; V[i].y *= U[i]; } ld lf = 0, rg = 1e12; { ld md = rg; vector<seg> a(n); for (int i = 0; i < n; i++) { a[i].id = i; a[i].p = S[i]; ld X = S[i].x + V[i].x * md; ld Y = S[i].y + V[i].y * md; a[i].q = {X, Y}; } if (solve(a) == make_pair(-1, -1)) { cout << "No show :("; return 0; } } const ld _eps = 1e-8; while ((rg - lf) > _eps) { ld md = (lf + rg) / 2; vector<seg> a(n); for (int i = 0; i < n; i++) { a[i].id = i; a[i].p = S[i]; ld X = S[i].x + V[i].x * md; ld Y = S[i].y + V[i].y * md; a[i].q = {X, Y}; } if (solve(a) != make_pair(-1, -1)) { rg = md; } else { lf = md; } } cout.precision(10); cout << fixed << (lf + rg) / 2; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, d; cin >> n >> d; vector<int> vec(n); for (int i = 0; i < n; i++) cin >> vec[i]; long long ans = 0; int l, h, m; for (int i = 0; i <= n - 3; i++) { if ((vec[i + 1] - vec[i]) < d) { l = i + 2; h = n - 1; while (l <= h) { m = l + (h - l) / 2; if ((vec[m] - vec[i]) > d) { h = m - 1; } else { if (m == n - 1 || (vec[m + 1] - vec[i]) > d) { long long x = (m - i); ans += (x * (x - 1)) / 2; break; } l = m + 1; } } } } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; bool check(int mid, int *arr, int m, int n) { int total = 0; for (int i = 0; i < n; i++) { total += max(0, arr[i] - i / mid); if (total >= m) return true; } return false; } int main() { int n, m, ar; cin >> n >> m; vector<int> arr; for (int i = 0; i < n; i++) { cin >> ar; arr.push_back(ar); } sort(arr.begin(), arr.end(), greater<int>()); int l = 1, r = n; int mid, ans = -1; while (l <= r) { mid = (l + r) / 2; if (check(mid, &arr[0], m, n)) { ans = mid; r = mid - 1; } else l = mid + 1; } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, a, s; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a; s += a; } for (int i = 1; i <= n; i++) { cin >> a; s -= a; } if (s < 0) { cout << "No" << endl; } else { cout << "Yes" << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, c = 1, i; cin >> n >> m; long long a[m]; for (i = 0; i < m; i++) cin >> a[i]; for (i = 0; i < m; i++) { if (a[i] <= a[i + 1]) { } else c++; } if (c > 1) cout << (n) * (c - 2) + (n - 1) + a[m - 1]; else cout << a[m - 1] - 1; }
2
#include <bits/stdc++.h> using namespace std; class Timer { clock_t start; string name; public: Timer() { name = ""; start = clock(); } Timer(string s) { name = s; start = clock(); } ~Timer() { fprintf(stderr, "%s: %.3gs\n", name.c_str(), 1.0 * (clock() - start) / CLOCKS_PER_SEC); } }; const double EPS = 1e-9; const long double PI = acos(-1.0L); template <typename dtype> inline dtype sq(dtype a) { return a * a; } template <typename dtype1, typename dtype2> inline pair<dtype1, dtype2> mp(dtype1 a, dtype2 b) { return make_pair(a, b); } template <typename dtype1, typename dtype2> inline dtype1 safeMod(dtype1 a, dtype2 m) { return (a % m + m) % m; } template <typename dtype1, typename dtype2> inline bool isEq(dtype1 a, dtype2 b) { return abs(a - b) < EPS; } template <typename dtype1, typename dtype2, typename dtype3> inline bool isEq(dtype1 a, dtype2 b, dtype3 eps) { return abs(a - b) < eps; } template <typename dtype> inline dtype toRad(dtype deg) { return deg * PI / 180.0; } template <typename dtype> inline dtype toDeg(dtype rad) { return rad * 180.0 / PI; } template <typename dtype> inline bool isKthBitOn(dtype n, int k) { assert(n <= numeric_limits<dtype>::max()); assert(k <= numeric_limits<dtype>::digits); dtype ONE = 1; return bool((n & (ONE << k))); } template <typename dtype> inline void setKthBit(dtype& n, int k) { assert(n <= numeric_limits<dtype>::max()); assert(k <= numeric_limits<dtype>::digits); dtype ONE = 1; n = (n | (ONE << k)); } const int oo = 0x3f3f3f3f; const int MAX = 110; const int MOD = 1000000007; const int precision = 10; int m, s; int ara[MAX]; int lara[MAX]; int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> m >> s; if (m * 9 < s || (s == 0 && m != 1)) { cout << "-1 -1"; return 0; } if (m == 1) { cout << s << " " << s; return 0; } int ss = s - 1; for (int i = m - 1; i > 0; i--) { ara[i] = min(9, ss); ss -= min(9, ss); } ara[0] = max(1, ss + 1); ss = s; for (int i = 0; i < m; i++) { lara[i] = min(9, ss); ss -= min(9, ss); } for (int i = 0; i < m; i++) cout << ara[i]; cout << " "; for (int i = 0; i < m; i++) { cout << lara[i]; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e5 + 10; struct fenwick { long long a[MAXN]; fenwick() { fill(a, a + MAXN, 0); } void update(long long id, long long val) { for (; id < MAXN; id += ((id) & -(id))) { a[id] += val; } } long long get_sum(long long id) { long long sum = 0; for (; id > 0; id -= ((id) & -(id))) { sum += a[id]; } return sum; } }; fenwick fen_1; fenwick fen_2; vector<pair<long long, long long> > query; long long a[MAXN]; int main() { ios_base ::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long double res = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; query.push_back(make_pair(a[i], i)); res += (long double)(n - i + 1) * i * (i - 1) / ((long double)(2) * n * (n + 1)); } sort(query.begin(), query.end()); for (long long i = 0; i < query.size(); i++) { long double e = (fen_1.get_sum(MAXN - 1) - fen_1.get_sum(query[i].second) - (query[i].second * (fen_2.get_sum(MAXN - 1) - fen_2.get_sum(query[i].second)))) * query[i].second; e += ((long double)(n - query[i].second) * (n - query[i].second + 1) * (fen_2.get_sum(MAXN - 1) - fen_2.get_sum(query[i].second))) / (long double)(2); e += ((long double)(query[i].second) * (query[i].second - 1) * (fen_2.get_sum(MAXN - 1) - fen_2.get_sum(query[i].second))) / (long double)(2); res += e * 2 / (n * (n + 1)); fen_1.update(query[i].second, query[i].second); fen_2.update(query[i].second, 1); } cout << fixed << setprecision(15) << res; }
5
#include <bits/stdc++.h> int main() { int a, b; scanf("%d %d", &a, &b); long long x, y, z; scanf("%lld %lld %lld", &x, &y, &z); long long tmpx = x * 2 + y, tmpz = 3 * z + y; if (b > tmpz) b = tmpz; if (a > tmpx) a = tmpx; if (tmpx - a + tmpz - b < 0) printf("0"); else printf("%lld", tmpx - a + tmpz - b); return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename... t> using V = vector<t...>; template <typename t> void print(ostream& os, const t& a) { os << a << '\n'; } template <typename t, typename... v> void print(ostream& os, const t& a, v&&... b) { os << a << ' '; print(os, b...); } template <typename t> void inp(t& a) { cin >> a; } template <typename t, typename... v> void inp(t& a, v&... b) { cin >> a; inp(b...); } constexpr int maxn = 1e5; int n; V<int> ilo; V<pair<int, int> > tb; inline bool by_sec(const pair<int, int>& a, const pair<int, int>& b) { return make_pair(a.second, a.first) < make_pair(b.second, b.first); } inline bool is_sorted(V<pair<int, int> >& tb) { for (int i = 1; i < tb.size(); ++i) if (tb[i].first < tb[i - 1].first) return false; return true; } inline bool is_lucky(int v) { while (v > 0) { if (v % 10 == 4 or v % 10 == 7) { v /= 10; continue; } else return false; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; tb.resize(n); for (pair<int, int>& i : tb) inp(i.first); if (is_sorted(tb)) return print(cout, 0), 0; for (int i = 0; i < n; ++i) tb[i].second = i; int ind = -1; for (int i = 0; i < n; ++i) if (is_lucky(tb[i].first)) ind = i; if (ind == -1) return print(cout, ind), 0; sort(begin(tb), end(tb)); for (int i = 0; i < n; ++i) tb[i].first = i; sort(begin(tb), end(tb), by_sec); ilo.resize(n); for (int i = 0; i < n; ++i) ilo[tb[i].first] = i; ; V<pair<int, int> > odp; for (int i = 0; i < n; ++i) { int v = ilo[i]; if (i != v) { if (ind != i) { odp.push_back({ind + 1, i + 1}); ilo[tb[i].first] = ind; ilo[tb[ind].first] = i; swap(tb[i], tb[ind]); ind = i; } odp.push_back({i + 1, v + 1}); ilo[tb[v].first] = i; ilo[tb[ind].first] = v; swap(tb[i], tb[v]); ind = v; } }; print(cout, odp.size()); for (auto& p : odp) print(cout, p.first, p.second); }
4
/*{{{*/ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<iostream> #include<sstream> #include<set> #include<map> #include<queue> #include<bitset> #include<vector> #include<limits.h> #include<assert.h> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define FOR(I, A, B) for (int I = (A); I <= (B); ++I) #define FORS(I, S) for (int I = 0; S[I]; ++I) #define RS(X) scanf("%s", (X)) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second using namespace std; typedef int64_t LL; typedef uint64_t ULL; typedef long double LD; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PII> VPII; typedef pair<LL,LL> PLL; typedef vector<PLL> VPLL; template<class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); } template<class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);} template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } template<class T, class... U> void DEBUG(const T &head, const U &... tail) { #ifdef HOME _W('#'); _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); #endif } int MOD = 1e9+7; void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;} /*}}}*/ const int SIZE = 2018; int deg[SIZE]; int p[SIZE]; int h[SIZE]; VI e[SIZE]; int a[SIZE]; int N; VI ans; int good[SIZE]; bool game_over(){ REP(i,N) if(a[i]!=i)return 0; return 1; } int bottom[SIZE],choose[SIZE],num; void find_important_vertice(int top){ int x=top; while(x!=-1&&deg[x]<=1){ num++; bottom[x]=top; x=p[x]; } } void push(int x){ ans.PB(x); int now=a[0]; while(x!=-1){ swap(now,a[x]); x=p[x]; } } int bfs[SIZE],cnt[SIZE]; int greedy_choose(){ bfs[0]=0; int rr=1; int ma=0,id=-1; REP(i,rr){ int x=bfs[i]; REP(j,SZ(e[x])){ int y=e[x][j]; if(good[a[y]])continue; bfs[rr++]=y; cnt[y]=cnt[x]+(bottom[a[y]]!=-1); if(cnt[y]>ma){ ma=cnt[y]; id=y; } } } return id; } int main(){ p[0]=-1; R(N); REPP(i,1,N){ R(p[i]); deg[p[i]]++; h[i]=h[p[i]]+1; e[p[i]].PB(i); } REP(i,N)R(a[i]); while(!game_over()){ num=0; MS1(bottom); REP(i,N){ if(!good[i]&&deg[i]==0){ find_important_vertice(i); } } while(num>0){ int me=a[0]; if(bottom[me]!=-1){ num--; good[me]=1; deg[p[me]]--; int x=bottom[me]; while(good[a[x]]&&h[a[x]]>h[me])x=p[x]; push(x); } else{ push(greedy_choose()); } } } W(SZ(ans)); for(int x:ans)W(x); return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int read() { int res = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar(); return res * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + '0'); else { write(x / 10); putchar(x % 10 + '0'); } } int n, m, h, a[110], b[110]; int main() { n = read(); m = read(); h = read(); for (int i = 1; i <= m; i++) a[i] = read(); for (int i = 1; i <= n; i++) b[i] = read(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int x = read(); if (x == 0) putchar('0'); else write(min(a[j], b[i])); putchar(' '); } puts(""); } return 0; }
2
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) using namespace std; #define INF ((1<<30)-1) #define LINF (1LL<<60) #define EPS (1e-10) typedef long long ll; typedef pair<ll, ll> P; const int MOD = 1000000007; const int MOD2 = 998244353; ll a[5050]; int main(){ int n, k; cin >> n >> k; rep(i, n) cin >> a[i]; sort(a, a + n); ll t = 0, cnt = 0, ans = n; rrep(i, n){ if (t + a[i] < k) t += a[i]; else ans = i; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; ifstream fin; ofstream fout; FILE *outt, *inn; const int N = 100009, M = 70; int k, b[M], d[M], k2, y, yy; unsigned long long m, c[M][M], l, r, cur; void find() { for (int i = 0; i <= 64; i++) { for (int j = 0; j <= 64; j++) { if (i == 0) { c[i][j] = 1; continue; } if (i > j) continue; c[i][j] = c[i - 1][j - 1] + c[i][j - 1]; } } } int cnt; bool is; void binary(unsigned long long x) { memset(b, 0, sizeof(b)); is = 0; cnt = 69; while (x > 0) { b[--cnt] = x % 2; x /= 2; } b[0] = cnt; for (int i = b[0] + 1; i <= 68; i++) { if (b[i]) { return; } } is = 1; } unsigned long long solve(unsigned long long x) { cur = 0; binary(2 * x); k2 = k - 1; for (int i = b[0] + 1; i <= 68; i++) { if (b[i] == 1) { y = 68 - i; cur += c[k2][y]; k2--; } if (k2 == 0) { for (int j = i + 1; j <= 68; j++) { if (b[j]) { cur++; break; } } break; } } k2 = 0; for (int i = b[0]; i <= 68; i++) { if (b[i]) k2++; } if (k2 == k) cur++; binary(x + 1); if (!is) { k2 = k - 1; for (int i = b[0] + 1; i <= 68; i++) { if (b[i] == 0) { cur += c[k2 - 1][68 - i]; } if (b[i] == 1) { k2--; } if (k2 == 0) { break; } } k2 = 0; for (int i = b[0]; i <= 68; i++) { if (b[i]) k2++; } if (k2 == k) cur++; } return cur; } int main() { memset(c, 0, sizeof(c)); find(); cin >> m >> k; r = (unsigned long long)1; while (solve(r) < m) { r *= 2; } l = r / 2 + 1; while (l < r) { unsigned long long mid = (l + r) / 2; unsigned long long res = solve(mid); if (res < m) { l = mid + 1; } else if (res > m) { r = mid - 1; } else { cout << mid; return 0; } } cout << l; return 0; }
4
#include<bits/stdc++.h> int main(){long double a,b,x;std::cin>>a>>b>>x;std::cout<<std::fixed<<(b>2*x/(a*a)?atan(b/(2*x/(a*b)))*180/acos(-1):atan((b-x/(a*a))/(a/2))*180/acos(-1));}
0
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; int n, k, m; long long ans; vector<pair<int, pair<int, int> > > ed, edx; vector<pair<int, int> > e[((int)501 * 1000)]; int par[((int)19)][((int)501 * 1000)], dis[((int)501 * 1000)]; int pari[((int)501 * 1000)]; int root(int x) { if (pari[x] < 0) return x; return pari[x] = root(pari[x]); } void pre_dfs(int x) { for (int i = 1; i < ((int)19); i++) par[i][x] = par[i - 1][par[i - 1][x]]; for (auto u : e[x]) if (u.first != par[0][x]) par[0][u.first] = x, dis[u.first] = dis[x] + 1, pre_dfs(u.first); } int iPar(int x, int num) { for (int i = 0; i < ((int)19); i++) if ((num & (1 << i))) x = par[i][x]; return x; } int lca(int x, int y) { if (dis[x] < dis[y]) swap(x, y); x = iPar(x, dis[x] - dis[y]); if (x == y) return x; for (int i = ((int)19) - 1; i >= 0; i--) if (par[i][x] != par[i][y]) x = par[i][x], y = par[i][y]; return par[0][x]; } priority_queue<pair<int, int> > q[((int)501 * 1000)]; vector<pair<int, int> > vec[((int)501 * 1000)]; void dfs(int x) { for (auto u : vec[x]) q[x].push({-u.first, u.second}); for (auto u : e[x]) { if (u.first == par[0][x]) continue; dfs(u.first); if (u.second == 0) { while (q[u.first].size() && q[u.first].top().second >= dis[u.first]) q[u.first].pop(); if (!q[u.first].size()) { printf("-1\n"); exit(0); } ans += -q[u.first].top().first; } if (q[u.first].size() > q[x].size()) q[u.first].swap(q[x]); while (q[u.first].size()) { if (q[u.first].top().second < dis[x]) q[x].push(q[u.first].top()); q[u.first].pop(); } } } int main() { scanf("%d%d%d", &n, &k, &m); for (int i = 0; i < k; i++) { int v, u; scanf("%d%d", &v, &u); ed.push_back({0, {v, u}}); } for (int i = 0; i < m; i++) { int v, u, w; scanf("%d%d%d", &v, &u, &w); ed.push_back({w, {v, u}}); } sort(ed.begin(), ed.end()); for (int i = 1; i <= n; i++) pari[i] = -1; for (auto x : ed) { int v = x.second.first, u = x.second.second; if (root(u) != root(v)) pari[root(v)] = root(u), e[v].push_back({u, x.first}), e[u].push_back({v, x.first}); else edx.push_back(x); } ed.clear(); pre_dfs(1); for (auto x : edx) { int v = x.second.first, u = x.second.second, w = x.first; int p = lca(v, u); if (v != p) vec[v].push_back({w, dis[p]}); if (u != p) vec[u].push_back({w, dis[p]}); } dfs(1); printf("%lld", ans); return 0; }
6
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <sstream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <numeric> #include <cctype> #include <tuple> #ifdef _MSC_VER #include <agents.h> #endif #define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; std::istream &operator>>(std::istream &is, P &p){ return is >> p.first >> p.second; } struct Dice{ private: static const int _roll[4][6], _turn[2][6]; public: vector<int> value; // 1-6 : top, near, right, left, far, bottom Dice(){ value.resize(6); for (int i = 0; i < 6; ++i) value[i] = i+1; } Dice(initializer_list<int> il){ value.resize(6, -1); int cnt = 0; for (auto &e : il){ value[cnt++] = e; if (cnt >= 6) break; } for (int i = 0; i < 6; ++i) if (value[i] < 0) value[i] = 5 - value[5 - i]; } void roll(int dir){ // N, E, W, S vector<int> nxt(6); for (int i = 0; i < 6; ++i){ nxt[i] = value[_roll[dir][i]]; } value = nxt; } void turn(int dir){ // L, R vector<int> nxt(6); for (int i = 0; i < 6; ++i){ nxt[i] = value[_turn[dir][i]]; } value = nxt; } int &operator[](int i){ return value[i]; } }; const int Dice::_roll[4][6] = { { 1, 5, 2, 3, 0, 4 }, // North { 3, 1, 0, 5, 4, 2 }, // East { 2, 1, 5, 0, 4, 3 }, // West { 4, 0, 2, 3, 5, 1 }, // South }; const int Dice::_turn[2][6] = { { 0, 3, 1, 4, 2, 5 }, // Left { 0, 2, 4, 1, 3, 5 }, // Right }; int board[10][10]; struct state{ P first; Dice second; bool operator<(const state &r)const{ if(first != r.first) return first < r.first; rep(i, 6) if (second.value[i] != r.second.value[i]) return second.value[i] < r.second.value[i]; return false; } }; int dx[] = {0, 1, -1, 0}; int dy[] = {-1, 0, 0, 1}; int main(){ int h, w; while (cin >> h >> w, h | w){ rep(i, h) rep(j, w) cin >> board[i][j]; P s, g; cin >> s >> g; map<state, int> m; priority_queue<pair<int, state>, vector<pair<int, state>>, greater<pair<int, state>>> q; q.push(make_pair(0, state{ s, Dice() })); int ans = -1; while (!q.empty()){ int dist = q.top().first; state s = q.top().second; P &pos = s.first; Dice &d = s.second; q.pop(); if (m[s] < dist) continue; if (s.first == g){ ans = dist; break; } rep(i, 4){ int nx = pos.second + dx[i], ny = pos.first + dy[i]; if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue; state nxt = s; nxt.second.roll(i); nxt.first = make_pair(ny, nx); if (m.count(nxt) && m[nxt] <= dist + nxt.second[5] * board[ny][nx]) continue; m[nxt] = dist + nxt.second[5] * board[ny][nx]; q.push(make_pair(dist + nxt.second[5] * board[ny][nx], nxt)); } } //cout << "+++"; cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> #define long long long int using namespace std; // @author: pashka vector<vector<int>> g, gr; vector<bool> z; vector<int> p; void dfs1(int x) { if (z[x]) return; z[x] = true; for (int y : g[x]) dfs1(y); p.push_back(x); } vector<int> c; int cc; void dfs2(int x) { if (z[x]) return; z[x] = true; c[x] = cc; for (int y : gr[x]) dfs2(y); } int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i]; vector<int> a(m); for (int i = 0; i < m; i++) cin >> a[i]; g.resize(n * m); gr.resize(n * m); vector<bool> alive(n * m); z.resize(n * m); for (int j = 0; j < m; j++) { int ii = -1; for (int i = 0; i < n; i++) { if (s[i][j] == '#') { alive[i * m + j] = true; if (ii != -1) { g[ii * m + j].push_back(i * m + j); gr[i * m + j].push_back(ii * m + j); if (i == ii + 1) { g[i * m + j].push_back(ii * m + j); gr[ii * m + j].push_back(i * m + j); } } ii = i; } } } for (int j = 0; j < m - 1; j++) { int ii = 0; for (int i = 0; i < n; i++) { if (s[i][j + 1] == '#') { while (ii < n && (ii < i || s[ii][j] == '.')) ii++; if (ii < n) { g[i * m + j + 1].push_back(ii * m + j); gr[ii * m + j].push_back(i * m + j + 1); } } } } for (int j = 0; j < m - 1; j++) { int ii = 0; for (int i = 0; i < n; i++) { if (s[i][j] == '#') { while (ii < n && (ii < i || s[ii][j + 1] == '.')) ii++; if (ii < n) { g[i * m + j].push_back(ii * m + j + 1); gr[ii * m + j + 1].push_back(i * m + j); } } } } for (int i = 0; i < n * m; i++) { if (alive[i]) dfs1(i); } z.assign(n * m, false); c.resize(n * m); reverse(p.begin(), p.end()); for (int i : p) { if (alive[i] && !z[i]) { dfs2(i); cc++; } } vector<bool> bd(cc); for (int x = 0; x < n * m; x++) { for (int y : g[x]) { if (c[x] != c[y]) bd[c[y]] = true; } } int res = 0; for (int x = 0; x < cc; x++) if (!bd[x]) res++; cout << res << "\n"; return 0; }
6
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define fs first #define sc second using namespace std; typedef pair<int,int> pii; typedef pair<pii,int> tri; int n,m; vector<tri> g[10100]; int dis[10100], cost[10100]; int main(){ while(scanf("%d%d",&n,&m), n){ rep(i,n)g[i].clear(); rep(i,m){ int u,v,d,c; scanf("%d%d%d%d",&u,&v,&d,&c); u--; v--; g[u].push_back(tri(pii(d,c),v)); g[v].push_back(tri(pii(d,c),u)); } fill(dis,dis+n,1e9); dis[0] = cost[0] = 0; priority_queue<pii, vector<pii>, greater<pii> > q; q.push(pii(0,0)); while(q.size()){ pii p = q.top(); q.pop(); if(dis[p.sc]<p.fs)continue; for(tri nxt : g[p.sc]){ int nd = p.fs + nxt.fs.fs, nc = nxt.fs.sc, u = nxt.sc; if(dis[u] > nd){ dis[u] = nd; cost[u] = nc; q.push(pii(nd,u)); }else if(dis[u] == nd && cost[u] > nc)cost[u] = nc; } } cout << accumulate(cost,cost+n,0) << endl; } }
0
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } const int MAXN = 1000010; const int INF = 2147483600; struct data { int x, y, val; } a[MAXN + 1]; int fa[MAXN + 1], s[MAXN + 1], vis[MAXN + 1], N, M, Kf, flag; bool cmp(data x, data y) { return x.val < y.val; } inline int find(int x) { return (x == fa[x] ? x : (fa[x] = find(fa[x]))); } inline void merge(int x, int y) { int X = find(x), Y = find(y); if (X == Y) return; fa[X] = Y; s[Y] += s[X]; if (s[Y] == Kf) flag = 1; } inline void solve() { for (int i = 1; i <= M; i++) { merge(a[i].x, a[i].y); if (flag == 1) { for (int j = 1; j <= Kf; j++) cout << a[i].val << " "; return; } } for (int i = 1; i <= M; i++) { merge(a[i].x, a[i].y); if (flag == 1) { for (int j = 1; j <= Kf; j++) cout << a[i].val << " "; return; } } for (int i = 1; i <= M; i++) { merge(a[i].x, a[i].y); if (flag == 1) { for (int j = 1; j <= Kf; j++) cout << a[i].val << " "; return; } } } int main() { N = read(), M = read(), Kf = read(); for (int i = 1; i <= Kf; i++) { int x = read(); vis[x] = 1; } for (int i = 1; i <= N; i++) { fa[i] = i; if (vis[i]) s[i] = 1; } for (int i = 1; i <= M; i++) a[i].x = read(), a[i].y = read(), a[i].val = read(); sort(a + 1, a + M + 1, cmp); solve(); return 0; }
4