0519模擬賽

在這裏插入圖片描述
考試時沒做這題。。。結果是最水的。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 100005;
int n, k;
struct node {
	LL x, y;
	node(LL x=0, LL y=0):x(x), y(y){}
	inline node operator +(const node &o)const { return node(x+o.x, y+o.y); }
	inline node operator /(const int &o)const { return node(x/o, y/o); }
	inline bool operator ==(const node &o)const { return x == o.x && y == o.y; }
	inline bool operator <(const node &o)const { return x == o.x ? y < o.y : x < o.x; }
}p[MAXN];
int ans;
map<node, bool>mp;
void chk(node mid) {
	if(mp.count(mid)) return;
	mp[mid] = 1;
	int l = 1, r = n, left = k;
	while(l <= r) {
		if((p[l]+p[r])/2 == mid) ++l, --r;
		else {
			if(p[l] == mid) ++l;
			else if(p[r] == mid) --r;
			else if(left) {
				if((p[l]+p[r])/2 < mid) ++l, --left;
				else --r, --left;
			}
			else return;
		}
	}
	++ans;
}
int main () {
	scanf("%d%d", &n, &k);
	for(int i =1; i <= n; ++i) scanf("%lld%lld", &p[i].x, &p[i].y), p[i].x *= 2, p[i].y *= 2;
	sort(p + 1, p + n + 1);
	if(k >= n) puts("-1");
	else {
		for(int i = 1; i <= k+1; ++i)
			for(int j = n; j >= n-k; --j)
				chk((p[i]+p[j])/2);
		printf("%d\n", ans);
	}
}

在這裏插入圖片描述
注意DP兩邊是對稱的,所以dpldp_l沒必要求,直接用對應的dprdp_r

考試時大概想出做法 沒調出來…

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, m, k, a, b;
inline int qpow(int a, int b) {
	int re = 1;
	for(; b; b>>=1, a=1ll*a*a%mod)if(b&1)re = 1ll*re*a%mod;
	return re;
}
const int MAXN = 1505;
int f[MAXN], sum[MAXN], sump[MAXN], pp[MAXN], fac[100005], inv[100005], p, q, ps[MAXN];
inline int C(int N, int M) { return N < M ? 0 : 1ll * fac[N] * inv[M] % mod * inv[N-M] % mod; }
int main () {
	scanf("%d%d%d%d%d", &n, &m, &a, &b, &k);
	p = 1ll * a * qpow(b, mod-2) % mod;
	q = (mod + 1 - p) % mod;
	fac[0] = fac[1] = inv[0] = inv[1] = 1;
	for(int i = 2; i <= k; ++i) fac[i] = 1ll * fac[i-1] * i % mod, inv[i] = 1ll * (mod-mod/i) * inv[mod%i] % mod;
	for(int i = 2; i <= k; ++i) inv[i] = 1ll * inv[i-1] * inv[i] % mod;
	for(int i = 0; i <= m && i <= k; ++i) {
		ps[i] = pp[i] = 1ll * C(k, i) * qpow(p, i) % mod * qpow(q, k-i) % mod;
	}
	for(int i = 1; i <= m; ++i) (ps[i] += ps[i-1]) %= mod;
	sum[m] = 1;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j)
			f[j] = 1ll * pp[m-j] * (1ll * ps[j-1] * (sum[m]-sum[m-j]) % mod - sump[j-1]) % mod;
		for(int j = 1; j <= m; ++j) {
			sum[j] = (sum[j-1] + f[j]) % mod;
			sump[j] = (sump[j-1] + 1ll*sum[j]*pp[j]) % mod;
		}
	}
	printf("%d\n", (sum[m] + mod) % mod);
}

在這裏插入圖片描述
考試時莫名MLE,就很難受。

#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
	char ch; int flg=1; while(!isdigit(ch=getchar()))if(ch=='-')flg=-flg;
	for(x=ch-'0';isdigit(ch=getchar());x=x*10+ch-'0'); x*=flg;
}
typedef long long LL;
const int MAXN = 200005;
const int MAXM = 200005;
const int inf = 1e9 + 5;
int n, A, B, q, qv[MAXN], fa[MAXN], id[MAXN]; LL ans[MAXN];
struct edge {
	int u, v, w;
	inline bool operator <(const edge& o)const {
		return w < o.w;
	}
}Ea[MAXM], Eb[MAXM];
int ch[MAXN][2], Fa[MAXN], eid[MAXN]; bool rev[MAXN];
struct node {
	int v, id;
	node(int v=-inf, int id=0):v(v), id(id){}
	inline node operator +(const node &o)const {
		if(v < o.v) return o;
		return *this;
	}
}val[MAXN], mx[MAXN];
inline bool isr(int x) { return ch[Fa[x]][0] != x && ch[Fa[x]][1] != x; }
inline bool get(int x) { return ch[Fa[x]][1] == x; }
inline void upd(int x) { mx[x] = mx[ch[x][0]] + mx[ch[x][1]] + val[x]; }
inline void rot(int x) {
	int y = Fa[x], z = Fa[y]; bool o = get(x);
	if(!isr(y)) ch[z][get(y)] = x; Fa[x] = z;
	Fa[ch[y][o] = ch[x][o^1]] = y;
	Fa[ch[x][o^1] = y] = x;
	upd(y), upd(x);
}
inline void mt(int x) { if(rev[x])swap(ch[x][0], ch[x][1]), rev[x]^=1, rev[ch[x][0]]^=1, rev[ch[x][1]]^=1; }
inline void mtp(int x) { if(!isr(x)) mtp(Fa[x]); mt(x); }
inline void splay(int x) {
	mtp(x);
	for(; !isr(x); rot(x))
		if(!isr(Fa[x])) rot(get(Fa[x]) == get(x) ? Fa[x] : x);
}
inline void access(int x) {
	for(int y = 0; x; x = Fa[y=x]) splay(x), ch[x][1] = y, upd(x);
}
inline int findrt(int x) { int X = x;
	access(x); splay(x);
	while(ch[x][0]) x = ch[x][0];
	splay(x), splay(X);
	return x;
}
inline void bert(int x) {
	access(x); splay(x); rev[x] ^= 1;
}
inline void link(int u, int v) {
	bert(u); access(v), splay(v); Fa[u] = v;
}
inline node qry(int u, int v) {
	bert(u); access(v), splay(v); return mx[v];
}
inline void cut(int u, int v) {
	bert(u); access(v), splay(v); ch[v][0] = Fa[u] = 0; upd(v);
}
int ff[MAXN];
int find(int x) { return ff[x] == x ? x : ff[x] = find(ff[x]); }
int pos[MAXN], v[MAXN], ii[MAXN], qid[MAXN];
inline bool cmp1(int i, int j) { return qv[i] < qv[j]; }
inline bool cmp2(int i, int j) { return pos[i] < pos[j]; }
int main () {
	read(n), read(A), read(B), read(q);
	for(int i = 1; i <= A; ++i) read(Ea[i].u), read(Ea[i].v), read(Ea[i].w);
	for(int i = 1; i <= B; ++i) read(Eb[i].u), read(Eb[i].v), read(Eb[i].w);
	for(int i = 1; i <= q; ++i) read(qv[i]), qid[i] = i;
	int tot = n;
	sort(Ea + 1, Ea + A + 1);
	sort(Eb + 1, Eb + B + 1);
	LL sum = 0, cf = 0;
	for(int i = 1; i <= A; ++i)
		if(findrt(Ea[i].u) != findrt(Ea[i].v)) {
			eid[++tot] = i;
			val[tot] = node(Ea[i].w, tot);
			link(tot, Ea[i].u), link(tot, Ea[i].v);
			sum += Ea[i].w, ++cf;
			if(cf == n-1) break;
		}
	for(int i = 1; i <= n; ++i) ff[i] = i;
	for(int i = 1, now = 0; i <= B; ++i)
		if(find(Eb[i].u) != find(Eb[i].v)) {
			ff[find(Eb[i].v)] = find(Eb[i].u);
			node Mx = qry(Eb[i].u, Eb[i].v);
			int x = (Eb[i].w - Mx.v + 1) >> 1; ++now;
			pos[now] = x, v[now] = Eb[i].w - Mx.v; ii[now] = now;
			cut(Mx.id, Ea[eid[Mx.id]].u);
			cut(Mx.id, Ea[eid[Mx.id]].v);
			val[Mx.id] = node(-inf, 0);
			//ch[Mx.id][0] = ch[Mx.id][1] = Fa[Mx.id] = rev[Mx.id] = 0;
			link(Mx.id, Eb[i].u);
			link(Mx.id, Eb[i].v);
			if(now == n-1) break;
		}
	sort(qid + 1, qid + q + 1, cmp1);
	sort(ii + 1, ii + n, cmp2);
	for(int i = 1, j = 1; i <= q; ++i) {
		while(j < n && pos[ii[j]] <= qv[qid[i]]) {
			sum += v[ii[j]]; cf -= 2; ++j;
		}
		ans[qid[i]] = sum + cf*qv[qid[i]];
	}
	for(int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章