Codeforces Round #647 (Div. 1) - Thanks, Algo Muse! A,B

A:

从主题1的开始遍历。

与主题i相连的点的主题一定都小于i,且从主题1到i-1都至少一个点与其相连。

我们记录每个点最后一次与其相连的点的主题是多少。

然后从主题1的所有点开始遍历。记录每个点被不同主题访问的次数。

只有当某个点:主题为x,最后一个点访问他的主题为x-1,且被x-1个不同点访问时才满足条件。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int M = 5e5+7;
 
int head[M],cnt;
void init(){cnt=0,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
vector<int>v[M];
int t[M],p[M],vs[M],nm[M];
int main()
{
  	int n,m;
  	scanf("%d%d",&n,&m);
  	for(int i=1;i<=m;i++)
  	{
  		int x,y;
  		scanf("%d%d",&x,&y);
  		add(x,y,1),add(y,x,1);
	}
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		t[i]=x;
		v[x].pb(i);
	}
	//cout<<"OKKK"<<endl;
	int sz=0;
	bool f=true;
	for(int z=1;z<=n;z++)
	{
		for(auto x:v[z])
		{
			if(!(nm[x]==z-1&&vs[x]==z-1))
			{
				f=false;
				break;
			}
			p[++sz]=x;
			for(int i=head[x];i;i=ee[i].nxt)
			{
				int y=ee[i].to;
				if(vs[y]!=z)
				{
					vs[y]=z;
					nm[y]++;
				}
			}
		}
		if(!f)break;
	}
	if(!f)cout<<-1<<endl;
	else 
	{
		for(int i=1;i<=n;i++)printf("%d ",p[i]);
		puts("");
	}
	return 0;
}

B:

按k大小排序

显然:最大的p^k[i], 一定是两两配对,然后剩一个,用前面的p^k[j]来抵消影响。

然后往前访问,到j,如果p^k[j]能抵消p^k[i]则抵消后,把当前的k[j]当成最大的来处理。

否则,全部抵消后,接着往前遍历。

重复上述过程即可。

显然每个种类的k最多访问一次 复杂度O val_k*logn  即k的取值范围

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int M = 1e6+7;
const int mod =1e9+7;
struct node{
	ll x,nm;
}p[M];
int a[M];
ll qpow(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return ans;
}
int main()
{
  	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		sort(a+1,a+1+n);a[0]=-1;int sz=0;
		//cout<<"okkk "<<endl;
		if(m==1)
		{
			printf("%d\n",n&1);
			continue;
		}
		for(int i=1;i<=n;i++)
		{
			if(a[i]!=a[i-1])p[++sz]=(node){a[i],1};
			else p[sz].nm++;
		}
		ll tp=1,up=n+1;
		for(int i=1;i<=100;i++)
		{
			tp*=m;
			if(tp>1e6)
			{
				up=i;
				break;
			}
		}
		ll ans=0;
		while(sz>=1)
		{
			//cout<<sz<<" - "<<endl;
			if(p[sz].nm%2==0){
				sz--;
				continue;
			}
			ans=(ans+qpow(m,p[sz].x))%mod;
			sz--;ll nm=1;
		//	cout<< ans<<"  "<<sz<<endl; 
			while(sz>=1)
			{
				int cz=p[sz+1].x-p[sz].x;
				if(cz<up)nm*=qpow(m,cz);
			//	cout<< cz<<"   "<<sz<<"  =  "<<ans<<endl;
				if(cz>=up||nm>1e6)
				{
					for(int i=sz;i>=1;i--)ans=(ans+mod-qpow(m,p[i].x)*p[i].nm%mod)%mod;
					sz=0;
					break;
				}
				else
				{
					if(nm>p[sz].nm)ans=(ans+mod-qpow(m,p[sz].x)*p[sz].nm%mod)%mod,nm-=p[sz].nm;
					else
					{
						ans=(ans+mod-qpow(m,p[sz].x)*nm%mod)%mod;
						p[sz].nm-=nm;
						break;
					}
				}
				sz--;
			}
		}
		printf("%lld\n",ans);
	} 
	return 0;
}
/*
10 5
3 3 3 2 2 2 2 2 2 2
*/ 

我们可以用双hash判断ans是否为0

 

#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 = 1e6+7;
ll a[M];
ll qpow(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b)
	{
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return ans;
}
ll pw[M];
int main()
{
  	int t;
  	scanf("%d",&t);
  	for(int T=1;T<=t;T++)
  	{
  		ll n,m;
  		scanf("%lld%lld",&n,&m);
  		for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
  		sort(a+1,a+1+n);
  		ll ans1=0,ans2=0;
  		int mod1=1e9+7,mod2=19270817;
  		for(int i=n;i;i--)
  		{
  			ll tp1=qpow(m,a[i],mod1);
  			ll tp2=qpow(m,a[i],mod2);
  			if(!ans1&&!ans2)ans1=(tp1+ans1)%mod1,ans2=(tp2+ans2)%mod2;
  			else ans1=(ans1+mod1-tp1)%mod1,ans2=(ans2+mod2-tp2)%mod2;
		}
		printf("%lld\n",ans1);
	}
	return 0;
}
/*
7
10 5
8 6 8 8 10 10 10 8 8 2 
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值