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 lq_kaixin at 2010-07-27 10:46:55 on Problem 2201
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Node
{
	int k,a;
	int par;
	int index;
	int l,r;
};
vector<Node> p(50001);
bool Cmp(Node a, Node b)
{
	return a.k<b.k;
}
bool Cmp1(Node a, Node b)
{
	return a.index<b.index;
}
void MakeTree(int l, int h)
{
	for(int i=l+1;i<=h;i++)
	{
		int cur = i-1, son = 0;
		while(p[cur].a > p[i].a)
			son = cur, cur = p[cur].par;//从最右找到合适位置
									//父节点(par),左右结点暂时记录按k排序后
									//相应结点的数组下标;
		if(son == 0)
		{
			p[cur].r = i;
			p[i].par = cur;
		}
		else if(cur == 0)
		{
			p[i].l = son;
			p[son].par = i;
		}
		else 
		{
			p[cur].r = i;
			p[i].par = cur;
			p[son].par = i;
			p[i].l = son;
		}
	}
	for(int i=l;i<=h;i++)
	{
		//将数组下标转化为index
		p[i].par = p[p[i].par].index;
		p[i].l = p[p[i].l].index;
		p[i].r = p[p[i].r].index;
	}
}
int main()
{
	int n;
	scanf("%d", &n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d %d", &p[i].k, &p[i].a);
		p[i].index = i;
	}
	sort(p.begin()+1, p.begin()+n+1, Cmp);//按k排序
	MakeTree(1, n);
	sort(p.begin()+1, p.begin()+n+1, Cmp1);//按index排序
	printf("YES\n");
	for(int i=1;i<=n;i++)
		printf("%d %d %d\n", p[i].par, p[i].l, p[i].r);
}

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