标签:ash ace names print 下界 结束 预处理 air size
签到题,两种情况分别计算然后取个min
#include<vector> #include<map> #include<cstdio> #define min(x, y) (x < y ? x : y) #define max(x, y) (x < y ? y : x) #define abs(x) (x < 0 ? - x : x) #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; inline int read() { char c = getchar(); int 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 N; int a[MAXN]; int x = INF, y = INF, z = INF; int main() { #ifdef WIN32 freopen("a.in", "r", stdin); #endif N = read(); for(int i = 1; i <= N; i++) a[i] = read(); for(int i = 1; i <= N; i++) { int opt = read(); if(opt == 1) x = min(x, a[i]); else if(opt == 2) y = min(y, a[i]); else if(opt == 3) z = min(z, a[i]); } printf("%d", min(x + y, z)); return 0; }
不会QWQ....
首先一个很显然的性质就是当$w >8$或$w < -9$的时候是无解的
否则,我们令$D_1=x$,$D_N = x +W$,这样其它的数就可以任意取了,有$10^{N - 2}$种方案
然后把$x$的取值乘上
具体见代码吧
#include<cstdio> #include<cstring> #include<vector> #include<map> #include<cmath> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> #define LL long long using namespace __gnu_pbds; using namespace std; const LL MAXN = 1e6 + 10, INF = 1e9 + 10, mod = 1e9 + 7; 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 T, N, W; LL fastpow(LL a, LL p) { LL base = 1; while(p) { if(p & 1) base = (base * a) % mod; p >>= 1; a = (a * a ) % mod; } return base % mod; } main() { #ifdef WIN32 freopen("a.in", "r", stdin); //freopen("b.out", "w", stdout); #endif T = read(); while(T--) { N = read(), W = read(); if(W > 8 || W < -9 ) {printf("0\n"); continue;} printf("%lld\n", (W >= 0 ? (9 - W) * fastpow(10, N - 2) : (10 + W) * fastpow(10, N - 2) ) % mod); } }
考虑到值域很小,首先预处理出每个值的出现次数
然后枚举所有值,再枚举一个断点,根据这个数拆开后的两个数的出现次数算贡献
两个数相同的时候需要特判
存值域可以让每个数加上下界,也可以直接用cc_hash_table,可以卡过
#include<cstdio> #include<cstring> #include<vector> #include<map> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> #define min(x, y) (x < y ? x : y) #define max(x, y) (x < y ? y : x) #define abs(x) (x < 0 ? - x : x) #define Pair pair<int, int> #define MP(x, y) make_pair(x, y) #define int long long using namespace __gnu_pbds; using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; inline int read() { char c = getchar(); int 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 T, N; int a[MAXN]; cc_hash_table<int,int>vis; cc_hash_table<int,bool>happen; main() { #ifdef WIN32 freopen("a.in", "r", stdin); #endif T = read(); while(T--) { N = read(); int mx = -INF, mi = INF; vis.clear(); happen.clear(); for(int i = 1; i <= N; i++) a[i] = read(), mx = max(a[i], mx), mi = min(a[i], mi); for(int i = 1; i <= N; i++) vis[a[i]]++; int ans = 0; for(int i = 1; i <= N; i++) { int limit = 2 * a[i]; if(happen[limit]) continue; for(int j = mi; j <= mx; j++) { if(vis[j] && vis[limit - j]) { if(j == (limit - j)) ans += (vis[j] - 1) * vis[j]; else ans += vis[j] * vis[limit - j]; } } happen[limit] = 1; } printf("%lld\n", ans / 2); } }
没啥可说的
大力特判,细节巨多
#include<cstdio> #include<cstring> #include<vector> #include<map> #include<cmath> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> #define int long long using namespace __gnu_pbds; using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; inline int read() { char c = getchar(); int 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 T, N; char s[MAXN]; int a[MAXN], b[MAXN]; main() { #ifdef WIN32 freopen("a.in", "r", stdin); freopen("b.out", "w", stdout); #endif scanf("%d", &T); while(T--) { scanf("%s", s + 1); scanf("%d", &N); int L = strlen(s + 1); for(int i = 1; i <= L; i++) a[i] = a[i - 1], b[i] = b[i - 1], s[i] == ‘a‘ ? a[i]++ : b[i]++; int ans = 0; if(a[L] > b[L]) { for(int i = 1; i <= L; i++) { if(a[i] > b[i]) ans = ans + N; else if(a[i] == b[i]) ans = ans + N - 1; else { ans = ans + max((int)0, N - 1 - (b[i] - a[i]) / (a[L] - b[L])); } } } else if(a[L] == b[L]) { for(int i = 1; i <= L; i++) { if(a[i] > b[i]) ans = ans + N; else continue; } } else { for(int i = 1; i <= L; i++) { if(a[i] > b[i]) ans = ans + min(N, (a[i] - b[i]) / (b[L] - a[L]) + (bool)((a[i] - b[i]) % (b[L] - a[L]))); else continue; } } printf("%lld\n", ans); } }
剩下的还没做
等APIO结束再说吧(估计也做不动了)
codechef April Challenge 2018 Div2(1-4)
标签:ash ace names print 下界 结束 预处理 air size
原文地址:https://www.cnblogs.com/zwfymqz/p/9026630.html