20180713 考试记录

T1 [Codeforces Round #250 Div1 B] The Child and Zoo

Solution

分析性质后,发现与最大生成树相关,然后就直接套就行了

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
typedef long long LL;

LL rd() {
	LL x=0,f=1; char c=getchar();
	while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
	while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
	return x*f;
}

const int N=200010,M=2000010;
LL n,m;
LL da[N],fa[N],siz[N];//1

struct Ed{
	int fr,to,w;
	Ed(int a=0,int b=0,int c=0):fr(a),to(b),w(c) {}
	bool operator < (const Ed oth) {return w>oth.w;}
}edg[M];

int getf(int x) {return fa[x]==x?x:getf(fa[x]);}

int main() {
//	freopen("zoo.in","r",stdin);
//	freopen("zoo.out","w",stdout);
	n=rd(),m=rd();
	F(i,1,n) da[i]=rd(),fa[i]=i,siz[i]=1;
	F(i,1,m) {
		int a=rd(),b=rd();
		edg[i]=Ed(a,b,min(da[a],da[b]));
	}
	sort(edg+1,edg+1+m);
	LL ans=0;
	F(i,1,m) {
		int u=getf(edg[i].fr),v=getf(edg[i].to);
		if(u!=v) {
			ans+=siz[u]*siz[v]*edg[i].w;
			if(siz[u]>siz[v]) swap(u,v);
			fa[u]=v; siz[v]+=siz[u];
		}
	}
	printf("%.6lf",(double)(ans<<1)/(n*(n-1)));
	return 0;
}

T2 segment

利用每次插入的长度单调递增的规律,用右端点<=当前右端点的数目-左端点<当前左端点数目
具体代码就是先离散化然后两个树状数组维护每个值存在的个数
代码丢了QAQ

T3 Codeforces 40E

Solution

当我们选完前n-1列后最后一列一定只有一种情况,同理在一列上选前n-1个数后最后一个数只有一种情况满足题意
那么可知每一列的情况数为(2^{n-s-1}) n为行数 s为预先填上的数目
然后利用乘法原理吧每一列乘起来
PS:有一些特判要注意 如要挑没有预先填数的一列把它的情况数看做1

Code

//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
typedef long long LL;

LL rd() {
	LL x=0,f=1; char c=getchar();
	while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
	while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
	return x*f;
}

const int N=1000010;
LL n,m,k,MOD,flag;
LL cnt[N],len[N],num[N],fla[N];
LL ans=1;

LL qpow(LL a,LL b) {
	LL t=1;
	while(b) {
		if(b&1) t=t*a%MOD;
		a=a*a%MOD;
		b>>=1;
	}
	return t;
}

int main() {
	n=rd(),m=rd();k=rd();
	if((n+m)&1) {puts("0");return 0;}
	if(n<m) swap(n,m),flag=1;
	F(i,1,k) {
		int a=rd(),b=rd(),c=rd();
		if(flag) swap(a,b);
		num[a]++;
		if(c==-1) fla[a]^=1;
		if(num[a]==m) if(!fla[a]) {puts("0");return 0;}
	}
	MOD=rd();
	flag=0;
	F(i,1,n) {
		if(!num[i]&&!flag) {flag=1;continue;}
		if(num[i]==m) continue;//1
		ans=ans*qpow(2ll,m-num[i]-1)%MOD;
	}
	printf("%lld",ans);
	return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 博主:https://www.cnblogs.com/Menteur-Hxy/
原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9307411.html