太坑了
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考虑考虑?