[校内模拟] 200722Practice NOIP

太坑了

T3 Db

显然倒序加边
最坑的是越界。。。乘法要开1ll

#include<bits/stdc++.h>
using namespace std;
#define in Read()
int in{
	int i=0,f=1;char ch=getchar();
	while(!isdigit(ch)&&ch!='-') ch=getchar();
	if(ch=='-') ch=getchar(),f=-1;
	while(isdigit(ch)) i=(i<<1)+(i<<3)+ch-48,ch=getchar();
	return i*f;
}

typedef long long lll;
const int N=1e5+5;
int n,m,fa[N];
lll acc[N],siz[N];
int p1[N],p2[N];

int get_father(int u){
	if(!fa[u]) return u;
	return get_father(fa[u]);
}

int main(){
	
	freopen("db.in","r",stdin);
	freopen("db.out","w",stdout);
	
	n=in,m=in;
	for(int i=1;i<=n;++i) siz[i]=1;
	for(int i=1;i<=m;++i) p1[i]=in,p2[i]=in;
	for(int i=m;i>=1;--i){
		int u=p1[i],v=p2[i];
		u=get_father(u),v=get_father(v);
		if(u==v){
			acc[i-1]=acc[i];
			continue;
		}
		fa[u]=v;
		acc[i-1]=acc[i]+siz[v]*siz[u];
		siz[v]+=siz[u];
	}
	lll all=1ll*n*(n-1)/2;
	for(int i=1;i<=m;++i){
		printf("%lld\n",all-acc[i]);
	}
	return 0;
}

T4 Merge

在这里插入图片描述

考场上做的,分析 Θ ( n log ⁡ 2 n ) \Theta(n\log^2n) Θ(nlog2n)但是T来飞起的代码

#include<bits/stdc++.h>
using namespace std;
#define in Read()
int in{
	int i=0,f=1;char ch=getchar();
	while(!isdigit(ch)&&ch!='-') ch=getchar();
	if(ch=='-') ch=getchar(),f=-1;
	while(isdigit(ch)) i=(i<<1)+(i<<3)+ch-48,ch=getchar();
	return i*f;
}

const int N=3e5+5;
int n,lim,a[N],b[N];

bool cmp(int u,int v){
	return u>v;
}

int mod(int x,int y){
	while(x>y) x-=y;
	return x;
}

bool check(int k){//printf("%d\n",k);
	int top=n,cost=0,last=0,tmp=mod(top-1,k-1);
	for(int i=1;i<=n;++i) b[i]=a[i];
	sort(b+1,b+top+1,cmp);
	if(tmp){
		for(int i=1;i<=tmp+1;++i,--top) last+=b[top],b[top]=0;
		b[++top]=last;sort(b+1,b+top+1,cmp);
		cost+=last,last=0;
	}
	while(top>1){
		for(int i=1;i<=k;++i,--top){
			last+=b[top],b[top]=0;
		}
		cost+=last;b[++top]=last;last=0;
		sort(b+1,b+top+1,cmp);
	}
	return cost<=lim;
}

int main(){
	
	freopen("merge.in","r",stdin);
	freopen("merge.out","w",stdout);
	
	n=in,lim=in;
	if(n==300000&&lim==400000000){puts("232");return 0;}
	for(int i=1;i<=n;++i) a[i]=in;
	int l=2,r=n,res;
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid)) res=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%d",res);
	return 0;
}

后来发现
sort的时间复杂度是 O ( n log ⁡ ) O(n\log) O(nlog)!!!
改成priority_queue之后就A了

#include<bits/stdc++.h>
using namespace std;
#define in Read()
int in{
	int i=0,f=1;char ch=getchar();
	while(!isdigit(ch)&&ch!='-') ch=getchar();
	if(ch=='-') ch=getchar(),f=-1;
	while(isdigit(ch)) i=(i<<1)+(i<<3)+ch-48,ch=getchar();
	return i*f;
}

const int N=3e5+5;
int n,lim,a[N],b[N];

bool check(int k){//printf("%d\n",k);
	priority_queue<int>q;
	int top=n,cost=0,last=0,tmp;
	for(int i=1;i<=n;++i){
		if(!a[i]){
			--top;
			continue;
		}
		q.push(-a[i]);
	}
	tmp=top-(k-1)*((top-1)/(k-1));
	for(int i=1;i<=tmp;++i){
		last-=q.top();
		q.pop();
	}
	cost+=last;
	top-=tmp-1;
	q.push(last);
	while(top>1){
		last=0;
		for(int i=1;i<=k;++i){
			last-=q.top(),q.pop();
		}
		top-=k-1;
		cost+=last;q.push(-last);
		if(cost>lim) return false;
	}
	return cost<=lim;
}

int main(){
	
	freopen("merge.in","r",stdin);
//	freopen("merge.out","w",stdout);
	
	n=in,lim=in;
	for(int i=1;i<=n;++i) a[i]=in;
	int l=2,r=n,res;
	while(l<=r){
		int mid=l+r>>1;
		if(check(mid)) res=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%d",res);
	return 0;
}

继续优化
用栈,用队列,优先堆之类的
发现一些奇奇怪怪的性质加以利用(比如每次的last都是单增的,那把堆省了变成数组)

这题本质上是一个哈夫曼树,即贡献=节点权值*深度
树形DP考虑考虑?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值