A - Ice Tea Store

算一下每种零售最少的钱就行,然后优先买2,零头买1

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 100005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int Q,H,S,D,N;
int64 ans;
int main() {
read(Q);read(H);read(S);read(D);
read(N);
H = min(2 * Q,H);
S = min(2 * H,S);
D = min(2 * S,D);
ans = 1LL * (N / 2) * D + (N & 1) * S;
out(ans);enter;
}

B - Reverse and Compare

对于两个相同的字符,在这个字符之间的翻转肯定能取代以这两个字符为端点的翻转

那么如果认为一个翻转会形成一个字符串,那么我们删掉左右两端点相同的翻转

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int64 ans;
int N,L,cnt[30];
char s[MAXN];
void Solve() {
scanf("%s",s + 1);
N = strlen(s + 1);
ans = 1 + 1LL * N * (N - 1) / 2;
for(int i = 1 ; i <= N ; ++i) {
cnt[s[i] - 'a']++;
}
for(int i = 0 ; i <= 25 ; ++i) {
ans -= 1LL * cnt[i] * (cnt[i] - 1) / 2;
}
out(ans);enter;
} int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

C - Fountain Walk

以横坐标上升对y求一个最长上升子序列

设长度为\(k\)

如果\(k < min(x_2 - x_1,y_2 - y_1) + 1\)

那么我就可以拐角k次,减少\((20 - 5\pi)k\)

否则我只能拐角k - 1次,不得不走一个半圆,减少\((20 - 5\pi)(k - 1)\)加上\((10\pi - 20)\)

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const double PI = acos(-1.0);
int64 x[MAXN],y[MAXN],id[MAXN];
int64 X1,X2,Y1,Y2,num[MAXN],val[MAXN];
int N,tot,c;
void Solve() {
read(X1);read(Y1);read(X2);read(Y2);
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(x[i]);read(y[i]);
}
if(X1 > X2) {
swap(X1,X2);swap(Y1,Y2);
}
if(Y1 > Y2) {
swap(Y1,Y2);
for(int i = 1 ; i <= N ; ++i) {
y[i] = Y1 + Y2 - y[i];
}
}
for(int i = 1 ; i <= N ; ++i) {
id[i] = i;
}
sort(id + 1,id + N + 1,[](int a,int b){return y[a] < y[b];});
for(int i = 1 ; i <= N ; ++i) {
int u = id[i];
if(y[u] >= Y1 && y[u] <= Y2 && x[u] >= X1 && x[u] <= X2) {
num[++tot] = x[u];
}
}
c = 0;
val[c] = -1;
for(int i = 1 ; i <= tot ; ++i) {
if(num[i] > val[c]) val[++c] = num[i];
else {
int t = upper_bound(val + 1,val + c + 1,num[i]) - val;
val[t] = num[i];
}
}
if(c < min(X2 - X1,Y2 - Y1) + 1) {
double ans = 100.0 * (X2 - X1 + Y2 - Y1) - (20 - PI * 5) * c;
printf("%.12lf\n",ans);
}
else {
double ans = 100.0 * (X2 - X1 + Y2 - Y1) - (20 - PI * 5) * (c - 1) + (10 * PI - 20);
printf("%.12lf\n",ans);
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

D - Shift and Flip

枚举B的第一位和A的哪一位对齐

然后对于A的每一位1,如果对着的B不是1,那么我们想办法把它消成0,这找这个1最近的两个1,这分别对应着两个和B第一位对齐的位置,放在数轴上

然后用twopoints,枚举当左边选择数轴上的点在这的时候,右边最远选到哪,计算两种走法,然后计算端点到目标点的距离

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 4005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int L[MAXN],R[MAXN],N;
char a[MAXN],b[MAXN];
vector<int> v[MAXN];
int cnt[MAXN];
bool checkpos(int x) {
if(x >= N) return false;
for(auto t : v[x]) {
cnt[t]--;
}
bool flag = 1;
for(auto t : v[x]) if(!cnt[t]) flag = 0;
for(auto t : v[x]) cnt[t]++;
return flag;
}
void Solve() {
scanf("%s%s",a,b);
N = strlen(a);
int ca = 0,cb = 0;
for(int i = 0 ; i < N ; ++i) {
if(a[i] == '1') ++ca;
if(b[i] == '1') ++cb;
}
if(!cb) {
if(!ca) puts("0");
else puts("-1");
return;
}
for(int i = 0 ; i < N ; ++i) {
if(a[i] == '1') {
int t = i;
int cnt = 0;
while(b[t] == '0') {
t = (t - 1 + N) % N;
++cnt;
}
L[i] = cnt % N;
t = i,cnt = 0;
while(b[t] == '0') {
t = (t + 1) % N;
++cnt;
}
R[i] = (N - cnt) % N;
}
}
for(int i = 0 ; i < N ; ++i) a[i + N] = a[i];
int ans = 1e9;
for(int i = 0 ; i < N ; ++i) {
int now = 0;
for(int j = 0 ; j < N ; ++j) v[j].clear();
memset(cnt,0,sizeof(cnt));
for(int j = 0 ; j < N ; ++j) {
if(b[j] != a[i + j]) {
++now;
if(a[i + j] == '1') {
int t = (i + j) % N;
v[L[t]].pb(t);v[R[t]].pb(t);
cnt[t] += 2;
}
}
}
int p = 0,q = 1;
while(p < N) {
while(checkpos(q)) {
for(auto t : v[q]) cnt[t]--;
++q;
}
int tmp = p + 2 * (N - q);
tmp += min(abs(p - i),N - abs(p - i));
ans = min(ans,tmp + now);
tmp = N - q + 2 * p;
tmp += min(abs(q - i),N - abs(q - i));
ans = min(ans,tmp + now);
++p;
for(auto t : v[p]) cnt[t]++;
}
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

E - Shuffle and Swap

就是对于01 和 10这两种序列配对,然后可以插任意个11进去

假如有d个01,总共k个1

这个方案数就是\((\frac{1}{1!}x^{1} + \frac{1}{2!}x^{2}.... + \frac{1}{k!}x^{k})^d\)

然后记\(f(i)\)是系数,那么方案数就是\(f(i) d!(k - d)!k!\),对于每个i都累加一下

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 10005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353,MAXL = (1 << 16);
char a[MAXN],b[MAXN];
int N,k,d,fac[MAXN],invfac[MAXN],W[MAXL];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int C(int n,int m) {
if(n < m) return 0;
return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
struct poly {
vector<int> v;
void limit(int n = -1) {
if(n != -1) v.resize(n);
while(v.size() > 1 && (v.back()) == 0) v.pop_back();
}
friend void NTT(poly &f,int L,int on) {
f.v.resize(L);
for(int i = 1, j = L >> 1 ; i < L - 1 ; ++i) {
if(i < j) swap(f.v[i],f.v[j]);
int k = L >> 1;
while(j >= k) {
j -= k;
k >>= 1;
}
j += k;
}
for(int h = 2 ; h <= L ; h <<= 1) {
int wn = W[(MAXL + on * MAXL / h) % MAXL];
for(int k = 0 ; k < L ; k += h) {
int w = 1;
for(int j = k ; j < k + h / 2 ; ++j) {
int u = f.v[j],v = mul(w,f.v[j + h / 2]);
f.v[j] = inc(u,v);
f.v[j + h / 2] = inc(u,MOD - v);
w = mul(w,wn);
}
}
}
if(on == -1) {
int invL = fpow(L,MOD - 2);
for(int i = 0 ; i < L ; ++i) {
f.v[i] = mul(f.v[i],invL);
}
}
}
friend poly operator * (poly a,poly b) {
poly c;c.v.clear();
int s = a.v.size() + b.v.size();
int L = 1;
while(L <= s) L <<= 1;
NTT(a,L,1);NTT(b,L,1);
for(int i = 0 ; i < L ; ++i) {
c.v.pb(mul(a.v[i],b.v[i]));
}
NTT(c,L,-1);
return c;
}
friend poly fpow(const poly &a,int c,int n) {
poly res,t = a;
res.v.clear();res.v.pb(1);
while(c) {
if(c & 1) {
res = res * t;
res.limit(n);
}
t = t * t;t.limit(n);
c >>= 1;
}
return res;
}
}f;
void Solve() {
scanf("%s%s",a + 1,b + 1);
N = strlen(a + 1);
for(int i = 1 ; i <= N ; ++i) {
if(a[i] == '1') {
++k;
if(b[i] == '0') ++d;
}
}
fac[0] = 1; for(int i = 1 ; i <= N ; ++i) {
fac[i] = mul(fac[i - 1],i);
}
invfac[N] = fpow(fac[N],MOD - 2);
for(int i = N - 1 ; i >= 0 ; --i) {
invfac[i] = mul(invfac[i + 1],i + 1);
}
W[0] = 1;W[1] = fpow(3,(MOD - 1) / MAXL);
for(int i = 2 ; i < MAXL ; ++i) W[i] = mul(W[i - 1],W[1]);
f.v.clear();
f.v.resize(N);
for(int i = 1 ; i <= k ; ++i) {
f.v[i] = invfac[i];
}
f = fpow(f,d,N);
int ans = 0;
for(int i = d ; i <= k ; ++i) {
if(i > f.v.size() - 1) break;
int t = mul(f.v[i],mul(fac[i],fac[d]));
t = mul(t,C(k,i));
t = mul(t,mul(fac[k - d],fac[k - i]));
ans = inc(ans,t);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

F - Yes or No

本来是个组合数前缀和,但是根据递推只需要改一项???

这坐标算的也太难受了,不想说话了。。。

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 1000005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353;
int N,M;
int fac[MAXN],invfac[MAXN],f[MAXN],v[MAXN],h[MAXN],inv[MAXN];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int C(int n,int m) {
if(n < m) return 0;
return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int way(int x1,int y1,int x2,int y2) {
return C(x2 - x1 + y2 - y1,y2 - y1);
}
void update(int &x,int y) {
x = inc(x,y);
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Solve() {
read(N);read(M);
fac[0] = 1;
inv[1] = 1;
for(int i = 1 ; i <= N + M ; ++i) {
fac[i] = mul(fac[i - 1],i);
if(i > 1) inv[i] = mul(inv[MOD % i],MOD - MOD / i);
}
invfac[N + M] = fpow(fac[N + M],MOD - 2);
for(int i = N + M - 1 ; i >= 0 ; --i) {
invfac[i] = mul(invfac[i + 1],i + 1);
}
if(M > N) h[0] = 1;
if(N > M) v[0] = 1;
for(int i = 1 ; i <= N + M ; ++i) {
h[i] = h[i - 1];
int r = min(i - 1,N) - h[i - 1] + 1,c = i - 1 - r + 1;
if(i <= N && M - (N - r) >= c + 1) ++h[i];
if(i > N && M - (N - r) <= c) --h[i];
v[i] = v[i - 1];
c = min(i - 1,M) - v[i - 1] + 1,r = i - 1 - c + 1;
if(i <= M && N - (M - c) >= r + 1) ++v[i];
if(i > M && N - (M - c) <= r) --v[i];
}
f[0] = mul(C(N + M, N),max(N,M));
int valh = 0,valv = 0;
if(h[0]) valh = C(N + M - 1, M - 1);
if(v[0]) valv = C(N + M - 1, N - 1);
for(int i = 1 ; i < N + M ; ++i) {
f[i] = f[i - 1];
if(h[i - 1]) f[i] = inc(f[i],MOD - valh);
if(v[i - 1]) f[i] = inc(f[i],MOD - valv);
if(!h[i - 1] && h[i]) valh = way(i,0,N,M - 1);
if(!v[i - 1] && v[i]) valv = way(0,i,N - 1,M);
int r1 = min(i - 1,N) - h[i - 1] + 1,c1 = i - 1 - r1;
int r2 = min(i,N) - h[i] + 1,c2 = i - r2;
if(h[i - 1]) {
if(r2 > r1) {
valh = inc(valh, MOD - mul(way(0,0,r1,c1),way(r1,c1 + 1,N,M - 1)));
}
else {
if(r2 >= 1) valh = inc(valh,mul(way(0,0,r2 - 1,c2),way(r2,c2,N,M - 1)));
}
}
c1 = min(i - 1,M) - v[i - 1] + 1,r1 = i - 1 - c1;
c2 = min(i,M) - v[i] + 1,r2 = i - c2;
if(v[i - 1]) {
if(c2 > c1) {
valv = inc(valv, MOD - mul(way(0,0,r1,c1),way(r1 + 1,c1,N - 1,M)));
}
else {
if(c2 >= 1) valv = inc(valv,mul(way(0,0,r2,c2 - 1),way(r2,c2,N - 1,M)));
}
}
}
int ans = 0;
for(int i = 0 ; i < N + M ; ++i) {
ans = inc(ans,mul(f[i],inv[N + M - i]));
}
ans = mul(ans,fpow(C(N + M,N),MOD - 2));
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

【AtCoder】AGC019的更多相关文章

  1. 【AtCoder】ARC092 D - Two Sequences

    [题目]AtCoder Regular Contest 092 D - Two Sequences [题意]给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1<=i,j<=n ...

  2. 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring

    [题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...

  3. 【AtCoder】ARC 081 E - Don't Be a Subsequence

    [题意]给定长度为n(<=2*10^5)的字符串,求最短的字典序最小的非子序列字符串. http://arc081.contest.atcoder.jp/tasks/arc081_c [算法]字 ...

  4. 【AtCoder】AGC022 F - Leftmost Ball 计数DP

    [题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...

  5. 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT

    [题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...

  6. 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分

    [题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...

  7. 【AtCoder】ARC095 E - Symmetric Grid 模拟

    [题目]E - Symmetric Grid [题意]给定n*m的小写字母矩阵,求是否能通过若干行互换和列互换使得矩阵中心对称.n,m<=12. [算法]模拟 [题解]首先行列操作独立,如果已确 ...

  8. 【Atcoder】AGC022 C - Remainder Game 搜索

    [题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...

  9. 【Atcoder】AGC 020 B - Ice Rink Game 递推

    [题意]n个人进行游戏,每轮只保留最大的a[i]倍数的人,最后一轮过后剩余2人,求最小和最大的n,或-1.n<=10^5. [算法]递推||二分 [题解]令L(i),R(i)表示第i轮过后的最小 ...

随机推荐

  1. 两年.net码农总结

    一直都是在博客园看文章,几乎每个两三天都会来,不管是看技术分享还是看经验总结,我觉得这真是个好地方. 工作两年,24.5岁,目前达到8.5K(即10W)的.net web. 文章水平不好,各位见谅了, ...

  2. Java开发中各种集合框架简介

    在大数据MapReduce作业开发中,我们经常会遇到一些大小表的join,这是如果这个小表足够“小”的话,我们可以使用进行“map-join-side”,这要就可以有效的降低reduce端的压力,但是 ...

  3. android SQLiteOpenHelper 使用

    1.实体 package mydemo.mycom.demo2.entity; public class UserInfo { private int id; private String usern ...

  4. 把ui界面加入到工程中

    第一步   from untitled import Ui_Form untitled是ui转化成py的文件名:Ui_Form是转换后的类名 第二步  把Ui_Form做为工程的父类    class ...

  5. 关于windows下的虚拟机Homestead在推送代码上github 步骤

    1.ssh 秘钥登录配置 使用以下命令检查主机是否生成SSH Key: > ls -al ~/.ssh 2.如果有秘钥,那就跳过这个步骤,如果没有秘钥,则运行以下命令来生成秘钥: ssh-key ...

  6. Activity Window View WindowManager关系&Touch事件分发机制

    http://www.cnblogs.com/linjzong/p/4191891.html https://www.cnblogs.com/kest/p/5141817.html https://b ...

  7. [转]python 装饰器

    以前你有没有这样一段经历:很久之前你写过一个函数,现在你突然有了个想法就是你想看看,以前那个函数在你数据集上的运行时间是多少,这时候你可以修改之前代码为它加上计时的功能,但是这样的话是不是还要大体读读 ...

  8. Flex布局和Bootstrap布局两者的比较

    在Bootstrap中采用12栅格的布局,12份随意分配,但是不能解决5等分,7等分的问题.所以flex布局来协助. bootstrap的布局方式 <div class="row&qu ...

  9. centos6.5生产环境编译安装nginx-1.11.3并增加第三方模块ngx_cache_purge、nginx_upstream_check、ngx_devel_kit、lua-nginx

    1.安装依赖包 yum install -y gcc gcc-c++ pcre-devel openssl-devel geoip-devel 2.下载需要的安装包 LuaJIT-2.0.4.zip ...

  10. HTTP基础知识1

    HTTP 简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准,它是基于TCP/IP ...