2020 Multi-University Training Contest 2 1001,1006,1010

224 team0691
北京林业大学
3

1001:

每次显然极大连通量。正着做最优是n^2。

正难则反。

倒着做发现:每次把最大值降到其周围相邻的最小值,知道降到0;

我没每次加入一个点(从点权大到小),把它与其相连的点,且已被加入的点并到一个集合里,代表这些点在一个连通快内可以一起缩小。缩小到当前节点的权值。设有nm个连通快,则执行到当前步的花费为:nm*(a[i]-a[i-1]).

加入n个点后的花费即最终花费。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;

vector<int>g[M];
int a[M],vs[M],fa[M];
int gt(int x)
{
	if(x==fa[x])return fa[x];
	return fa[x]=gt(fa[x]);
}
struct node{
	int x,id;
	bool operator < (const node &r)const{
		return x<r.x;
	}
}p[M];
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
  	int t;
  	cin>>t;
  	while(t--)
  	{
  		int n,m;
  		cin>>n>>m;
  		for(int i=1;i<=n;i++)cin>>a[i],g[i].clear(),p[i]=node{a[i],i};
  		for(int i=1;i<=n;i++)fa[i]=i,vs[i]=0;
  		for(int i=1;i<=m;i++)
  		{
  			int u,v;
  			cin>>u>>v;
  			g[u].pb(v);
  			g[v].pb(u);
		}
		sort(p+1,p+1+n);
		int nm=0;
		ll ans=0;
		for(int i=n;i>=1;i--)
		{
			int x=p[i].id;
			nm++;
			vs[x]=1;
			int gx=gt(x);
			for(auto y:g[x])
			{
				if(!vs[y])continue;
				int gy=gt(y);
				if(gx!=gy)nm--,fa[gy]=gx;
			}
		//	cout<<i<<" "<<nm<<" "<<p[i].x<<" "<<p[i-1].x<<endl;
			ans+=(ll)nm*(p[i].x-p[i-1].x);
		}
		cout<<ans<<endl;
	  }
	return 0;
}

1004:

如果这题结果不爆ll,那么这题就是一个简单的思维题。

既然会爆,我们直接用hash一下即可。。注意可以多选几个质数防止出问题

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e6+7;
const int up=5;
ll p[up]={19260817,131,1331,1000000009,1000000007};
ll c[M*2]; 
ll f[M*2][up];
ll A[up]={0},B[up]={0},C[up]={0},Z[up]={0},x;
int main()
{
  	int T;
	scanf("%d",&T); 
	for(int j=0;j<up;j++)f[1][j]=1,f[2][j]=2;
	for(int i=3;i<=2000007;i++)
		for(int j=0;j<up;j++)f[i][j]=(f[i-1][j]+f[i-2][j])%p[j];
	while(T--)
	{
		int n;
		for(int i=0;i<up;i++)A[i]=B[i]=C[i]=Z[i]=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			if(x)for(int j=0;j<up;j++)A[j]=(A[j]+f[i][j])%p[j];
		}
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			if(x)for(int j=0;j<up;j++)B[j]=(B[j]+f[i][j])%p[j];
		}
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&c[i]);
			if(c[i])for(int j=0;j<up;j++)C[j]=(C[j]+f[i][j])%p[j];
		}
		
		for(int j=0;j<up;j++){
			Z[j]=(A[j]*B[j])%p[j];
			Z[j]=(Z[j]+p[j]-C[j])%p[j];
		}
		for(int i=1;i<=n;i++){
			if(c[i]==0){
				if(c[i-1]||c[i+1])continue;
				bool fg=true;
				for(int j=0;j<up;j++)if(f[i][j]!=Z[j])fg=false;
				if(fg)
				{
					printf("%d\n",i);
					break;
				}
			}
		}
	}
	return 0;
}

1010:

简单的dfs。

注意把0给跳过可以优化一个大常数。

这里我比赛时没有优化上面,但我说把最大的块不进行dfs,最后用for处理,可以少很多dfs。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
	int a,b,c,d;
}p[57][57];
int nm[57];
ll mx=0;
int n,k;
int a,b,c,d;
void dfs(int x)
{
	if(x==k+1){
		for(int i=1;i<=nm[1];i++)
		{
			node y=p[1][i];
			mx=max(mx,(ll)(100+a+y.a)*(100+b+y.b)*(100+c+y.c)*(100+d+y.d));
		}
		return;
	}
	if(nm[x]==0){
		dfs(x+1);
		return ;
	}
	for(int i=1;i<=nm[x];i++)
	{
		node y=p[x][i];
		a+=y.a;b+=y.b;c+=y.c;d+=y.d;
		dfs(x+1);
		a-=y.a;b-=y.b;c-=y.c;d-=y.d;	
	}
}

int main()
{
  	int T;
	scanf("%d",&T);
	while(T--)
	{
		mx=0;
		scanf("%d%d",&n,&k);
		memset(p,0,sizeof(p));
		memset(nm,0,sizeof(nm));
		for(int i=0;i<=k;i++)nm[i]=0;
		for(int i=1;i<=n;i++)
		{
			int ty,A,B,C,D;
			scanf("%d%d%d%d%d",&ty,&A,&B,&C,&D);
			p[ty][++nm[ty]]=(node{A,B,C,D});
		}
		int id=1;
		for(int i=1;i<=k;i++)
			if(nm[id]<nm[i])id=i;
		swap(p[id],p[1]);
		swap(nm[id],nm[1]);
		a=b=c=d=0;
		dfs(2);
		printf("%lld\n",mx);
	}
	return 0;
}
/*
6 4
1 17 25 10 0
2 0 0 25 14
4 17 0 21 0
1 5 22 0 10
2 0 16 20 0
4 37 0 0 0
*/

猜你喜欢

转载自blog.csdn.net/bjfu170203101/article/details/107547025