社区讨论

萌新刚学OI,TLE+WA求调必关

P7614[COCI 2011/2012 #2] NAJBOLJIH 5参与者 4已保存回复 15

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
15 条
当前快照
1 份
快照标识符
@mllq4q0c
此快照首次捕获于
2026/02/14 10:55
5 天前
此快照最后确认于
2026/02/14 11:06
5 天前
查看原帖
1TLE+8WA+1AC
CPP
#include<bits/stdc++.h>
#define ll lon long 
#define ull unsigned long long

using namespace std;

void prin(int);

struct node{
	int ch[2];
	int fa,val,cnt,siz,id;
}t[10];
int root,tot;
void update(int p){
	t[p].siz=t[t[p].ch[0]].siz+t[t[p].ch[1]].siz+t[p].cnt;
}
int get(int p){
	return p==t[t[p].fa].ch[1];
}
void clr(int p){
	t[p].ch[0]=t[p].ch[1]=0;
	t[p].fa=t[p].val=t[p].cnt=t[p].siz=0;
}
void rotate(int x){
	int y=t[x].fa;int z=t[y].fa;
	int chk=get(x);
	t[y].ch[chk]=t[x].ch[chk^1];
	if(t[x].ch[chk^1])t[t[x].ch[chk]].fa=y;
	t[x].ch[chk^1]=y;
	t[y].fa=x;
	t[x].fa=z;
	if(z)t[z].ch[y==t[z].ch[1]]=x;
	update(y);update(x);
}
void splay(int x){
	for(int f=t[x].fa;f=t[x].fa,f;rotate(x)){
		if(t[f].fa)rotate(get(f)==get(x)?f:x);
	}
	root=x;
}
void inser(int k,int id){
	if(!root){
		t[++tot].val=k;
		t[tot].id=id;
		t[tot].cnt++;
		root=tot;
		update(root);
		return;
	}
	int cur=root,f=0;
	while(1){
		if(t[cur].val==k){
			t[cur].cnt++;
			update(cur);
			update(f);
			splay(cur);
			break;
		}
		f=cur;
		cur=t[f].ch[k>t[f].val];
		if(!cur){
			t[++tot].val=k;
			t[tot].id=id;
			t[tot].cnt++;
			t[tot].fa=f;
			t[f].ch[k>t[f].val]=tot;
			update(tot);
			update(f);
			splay(tot);
			break;
		}
	}
}
int rnk(int k){
	int res=0,cur=root;
	while(1){
		if(!cur)return res+1;
		if(k<t[cur].val){
			cur=t[cur].ch[0];
		}else{
			res+=t[t[cur].ch[0]].siz;
			if(t[cur].val==k){
				splay(cur);
				return res+1;
			}
			res+=t[cur].cnt;
			cur=t[cur].ch[1];
		}
	}
}
int kth(int k){
	int cur=root;
	while(1){
		if(t[cur].ch[0]&&t[t[cur].ch[0]].siz>=k){
			cur=t[cur].ch[0];
		}else{
			k-=t[t[cur].ch[0]].siz+t[cur].cnt;
			if(k<=0){
				splay(cur);
				return t[cur].val;
			}
			cur=t[cur].ch[1];
		}
	}
}
int pre(){
	int cur=t[root].ch[0];
	if(!cur)return cur;
	while(t[cur].ch[1])cur=t[cur].ch[1];
	splay(cur);
	return cur;
}
int nxt(){
	int cur=t[root].ch[1];
	if(!cur)return cur;
	while(t[cur].ch[0])cur=t[cur].ch[0];
	splay(cur);
	return cur;
}
void del(int k){
	rnk(k);
	if(t[root].cnt>1){
		t[root].cnt--;
		update(root);
		return;
	}
	if(!t[root].ch[0]&&!t[root].ch[1]){
		clr(root);
		root=0;
		return;
	}
	if(!t[root].ch[0]){
		int cur=root;
		root=t[root].ch[1];
		t[root].fa=0;
		clr(cur);
		return;
	}
	if(!t[root].ch[1]){
		int cur=root;
		root=t[root].ch[0];
		t[root].fa=0;
		clr(cur);
		return;
	}
	int cur=root;
	int x=pre();
	t[t[cur].ch[1]].fa=root;
	t[root].ch[1]=t[cur].ch[1];
	clr(cur);
	update(root);
	return;
}
int getmax(){
	int cur=root;
	while(t[cur].ch[1])cur=t[cur].ch[1];
	return cur;
}
void prin(int p)
{
	if (!p) return;
	printf("id= %d, fa= %d, lc= %d, rc= %d, val= %d, cnt= %d, size= %d\n",\
	p,t[p].fa,t[p].ch[0],t[p].ch[1],t[p].val,t[p].cnt,t[p].siz);
	prin(t[p].ch[0]);
	prin(t[p].ch[1]);
}
int a[10],f[10];
int main(){
	for(int i=1;i<=8;i++){
		cin>>a[i];
		inser(a[i],i);
	}
	int ans=0;
	for(int i=1;i<=5;i++){
		int p=getmax();
		f[t[p].id]=1;
		ans+=t[p].val;
		del(t[p].val);
	}
	cout<<ans<<endl;
	for(int i=1;i<=8;i++){
		if(f[i])cout<<i<<' ';
	}
	return 0;
}

回复

15 条回复,欢迎继续交流。

正在加载回复...