Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

暴力优化??自底向上??我已经先按k排序了,还是TLE的,看看我程序吧

Posted by Jin_j_y at 2005-04-06 22:41:31 on Problem 2201
In Reply To:Re:tle说明算法应该对了,然后就是暴力优化-_-b....(其实可以自底向上的) Posted by:ArXoR at 2005-04-05 23:31:34
先说一个问题:开始我只开到5000,TLE了,后来我发现问题的规模是50000。为什么5000的时候没有RTE呢?是不是测试数据小于5000的啊?如何这样都TLE,那么我没想法了。
//#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;

class Node{
public:
	int p,a,k;
	Node(){}
	Node(int pp,int aa,int kk):p(pp),a(aa),k(kk){}
	friend bool operator<(const Node& aa,const Node& bb){
		return aa.k<bb.k;
	}
};

Node * nodes;
int tree[50000][3];

int DC(int low,int high,int father){
	if(low==high)return 0;
	int mina=40000,mink,minp,mini;
	if(high-low==1){
		mini=nodes[low].p;
		tree[mini-1][1]=0;
		tree[mini-1][2]=0;
		tree[mini-1][0]=father;
		return mini;
	}
	//找到含有最小a的元素
	int i;
	for(i=low;i<high;++i)
		if(mina>nodes[i].a){
			minp=i;
			mina=nodes[i].a;
			mink=nodes[i].k;
			mini=nodes[i].p;
		}
	//递归处理左右子树,排序之后已经保证left<root<right了
	int left=DC(low,minp,mini);
	int right=DC(minp+1,high,mini);
	tree[mini-1][1]=left;
	tree[mini-1][2]=right;
	tree[mini-1][0]=father;
	return mini;
}


int main(){
	//ifstream cin("in.txt");
	int n;
	cin>>n;
	nodes=new Node[n];
	int i,a,k;
	for(i=0;i<n;i++){
		cin>>k>>a;
		nodes[i]=Node(i+1,a,k);
	}
	sort(nodes,nodes+n);//nlog(n)
	DC(0,n,0);
	cout<<"YES"<<endl;
	for(i=0;i<n;i++){
		cout<<tree[i][0]<<" "<<tree[i][1]<<" "<<tree[i][2]<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator