Educational Codeforces Round 98 (Rated for Div. 2)
直接跳
#include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <stack> #include <sstream> #include <set> // #pragma GCC optimize(2) //#define int long long #define ls u<<1 #define rs u<<1|1 #define rep(i,a,n) for(int i=a;i<=n;i++) #define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define mm(i,v) memset(i,v,sizeof i); #define mp(a, b) make_pair(a, b) #define pi acos(-1) #define fi first #define se second using namespace std; typedef long long ll; typedef double db; typedef pair<int, int > PII; priority_queue< PII, vector<PII>, greater<PII> > que; stringstream ssin; // ssin << string while ( ssin >> int) const ll LINF = 0x7fffffffffffffffll; const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f; int _, x, y, ans; inline ll read() { char c=getchar();ll x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif _ = read(); while (_--) { ans = 0; x = read(); y = read(); ans += min(x, y) * 2; int t = min(x, y); x -= t; y -= t; if (x) { ans += 2 * x - 1; } else if (y) { ans += 2 * y - 1; } cout << ans << '\n'; } // #ifndef ONLINE_JUDGE // system("pause"); // #endif }
比较一下最大值和剩下n - 1个即可
#include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <stack> #include <sstream> #include <set> // #pragma GCC optimize(2) //#define int long long #define ls u<<1 #define rs u<<1|1 #define rep(i,a,n) for(int i=a;i<=n;i++) #define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define mm(i,v) memset(i,v,sizeof i); #define mp(a, b) make_pair(a, b) #define pi acos(-1) #define fi first #define se second using namespace std; typedef long long ll; typedef double db; typedef pair<int, int > PII; priority_queue< PII, vector<PII>, greater<PII> > que; stringstream ssin; // ssin << string while ( ssin >> int) const ll LINF = 0x7fffffffffffffffll; const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f; ll _, n, sum, max1, max2; ll a[N]; inline ll read() { char c=getchar();ll x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif _ = read(); while (_--) { n = read(); sum = 0; max1 = 0; max2 = 0; rep(i, 1, n) { a[i] = read(); if (a[i] > max1) { max2 = max1; max1 = a[i]; } else if (a[i] == max1) { max2 = a[i]; } sum += a[i]; } ll ans = 0; ll tmp = max1 * (n - 1); if (sum >= tmp) { printf("%lld\n",sum%(n-1)==0? 0: (n-1)-sum%(n-1)); } else printf("%lld\n",tmp-sum); } // #ifndef ONLINE_JUDGE // system("pause"); // #endif }
括号匹配
#include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <stack> #include <sstream> #include <set> // #pragma GCC optimize(2) //#define int long long #define ls u<<1 #define rs u<<1|1 #define rep(i,a,n) for(int i=a;i<=n;i++) #define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define mm(i,v) memset(i,v,sizeof i); #define mp(a, b) make_pair(a, b) #define pi acos(-1) #define fi first #define se second using namespace std; typedef long long ll; typedef double db; typedef pair<int, int > PII; priority_queue< PII, vector<PII>, greater<PII> > que; stringstream ssin; // ssin << string while ( ssin >> int) const ll LINF = 0x7fffffffffffffffll; const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f; int _; char s[N]; stack<char>s1, s2; inline ll read() { char c=getchar();ll x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif _ = read(); while (_--) { while (!s1.empty()) s1.pop(); while (!s2.empty()) s2.pop(); scanf("%s", s + 1); int len = strlen(s + 1); int ans = 0; rep(i, 1, len) { char x = s[i]; if (x == '(') s1.push(x); if (x == '[') s2.push(x); if (x == ')') { if (s1.empty()) { s1.push(x); continue; } char y = s1.top(); if (y == '(') { ans++; s1.pop(); } else s1.push(x); } if (x == ']') { if (s2.empty()) { s2.push(x); continue; } char y = s2.top(); if (y == '[') { ans++; s2.pop(); } else s2.push(x); } } cout << ans << '\n'; } // #ifndef ONLINE_JUDGE // system("pause"); // #endif }
分子斐波那契,分母2的幂次
#include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <stack> #include <sstream> #include <set> // #pragma GCC optimize(2) //#define int long long #define ls u<<1 #define rs u<<1|1 #define rep(i,a,n) for(int i=a;i<=n;i++) #define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define mm(i,v) memset(i,v,sizeof i); #define mp(a, b) make_pair(a, b) #define pi acos(-1) #define fi first #define se second using namespace std; typedef long long ll; typedef double db; typedef pair<int, int > PII; priority_queue< PII, vector<PII>, greater<PII> > que; stringstream ssin; // ssin << string while ( ssin >> int) const ll LINF = 0x7fffffffffffffffll; const int N = 4e5 + 5, M = 4e5 + 5, mod = 998244353, INF = 0x3f3f3f3f; ll n; ll fab[N], fac[N]; inline ll read() { char c=getchar();ll x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } ll qp(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) { ans = ans * a % mod; } a = a * a % mod; b = b / 2; } return ans % mod; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif n = read(); fab[1] = 1; fab[2] = 1; fac[0] = 1; fac[1] = 2; fac[2] = 4; for (int i = 3; i <= n; ++i) { fac[i] = fac[i - 1] * 2 % mod; fab[i] = fab[i - 1] + fab[i - 2]; fab[i] %= mod; } // cout << fab[n - 1] << '\n'; // cout << fac[n] << '\n'; cout << fab[n] % mod * qp(fac[n], mod - 2) % mod << '\n'; // #ifndef ONLINE_JUDGE // system("pause"); // #endif }
先固定第一个k1,枚举所有区间,可以通过二维差分约束O(1)预处理出第二个范围k2在每个位置的贡献,具体如下
add(L + len, 1);
add(R - len + 1 + k, 1);
add(min(L + k, R + 1), -1);
add(max(L + r, R + 1), -1);
#include <stdio.h> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <stack> #include <sstream> #include <set> // #pragma GCC optimize(2) //#define int long long #define ls u<<1 #define rs u<<1|1 #define rep(i,a,n) for(int i=a;i<=n;i++) #define REP(i,a,n) for(int i=a;i>=n;--i) #define rush() int T;scanf("%d",&T);for(int Ti=1;Ti<=T;++Ti) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define mm(i,v) memset(i,v,sizeof i); #define mp(a, b) make_pair(a, b) #define pi acos(-1) #define fi first #define se second using namespace std; typedef long long ll; typedef double db; typedef pair<int, int > PII; priority_queue< PII, vector<PII>, greater<PII> > que; stringstream ssin; // ssin << string while ( ssin >> int) const ll LINF = 0x7fffffffffffffffll; const int N = 4e5 + 5, M = 4e5 + 5, mod = 1e9 + 7, INF = 0x3f3f3f3f; int n, m, k, ans; int d2[N]; PII a[N]; inline ll read() { char c=getchar();ll x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f; } int f(int l1, int r1, int l2, int r2) { if (l1 > l2) { swap(l1, l2); swap(r1, r2); } if (r1 < l2) return 0; return min(r1, r2) - l2 + 1; } void add(int x, int v) { d2[x] += v; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif n = read(); m = read(); k = read(); rep(i, 1, m) a[i].fi = read(), a[i].se = read(); rep(i, k, n) { rep(j, 0, n) d2[j] = 0; int now = 0, res = 0; rep(j, 1, m) { int L = a[j].fi, R = a[j].se; now = f(L, R, i - k + 1, i); res += now; if (now == k || now == R - L + 1) continue; add(L + now, 1); add(R - now + 1 + k, 1); add(min(L + k, R + 1), -1); add(max(L + k, R + 1), -1); } rep(j, 1, n) { d2[j] += d2[j - 1]; } rep(j, 1, n) { d2[j] += d2[j - 1]; if (j >= k) ans = max(ans, res + d2[j]); } } cout << ans << '\n'; // #ifndef ONLINE_JUDGE // system("pause"); // #endif }