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

按《算法竞赛进阶指南》的思路写的,为什么会WA啊。。。求大佬看下

Posted by forestsea at 2018-09-15 17:56:48 on Problem 2442
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200005;
int a[105][2005];
int ans[2005];
typedef struct Node{
	int i;
	int j;
	bool last;
}node;
node heap[maxn];
int M,N,T,p,n;
void up(int v)
{
	while(v>1)
	{
		if((a[p][heap[v].i]+a[p+1][heap[v].j]) < (a[p][heap[v/2].i]+a[p+1][heap[v/2].j]))
		{
			swap(heap[v],heap[v/2]);
			v /= 2;
		}
		else
		break;
	}
}
void Insert(node v)
{
	heap[++n] = v;
	up(n); 
}
node GetTop()
{
	return heap[1];
}
void down(int v)
{
	int s = v*2;//左节点
	while(s <= n)
	{
		if(s<n && (a[p][heap[s].i]+a[p+1][heap[s].j]) > (a[p][heap[s+1].i]+a[p+1][heap[s+1].j])) s++;//子节点中选最xiao
		if((a[p][heap[s].i]+a[p+1][heap[s].j]) < (a[p][heap[v].i]+a[p+1][heap[v].j]))
		{
			swap(heap[s],heap[v]);
			v = s;
			s = v*2;
		}
		else
		break;
	}
}
void Extract()//将堆顶移除
{
	heap[1] = heap[n--];
	down(1);
}
void Remove(int k)//移除k位置的节点
{
	heap[k]=heap[n--];
	up(k);
	down(k);
}
void show()
{
	int i,j;
	for(i=0;i<M;i++)
	{
		for(j=1;j<=N;j++)
		cout<<a[i][j]<<' ';
		cout<<endl;
	}
	cout<<"ans:"<<endl;
	for(i=0;i<N;i++)
	cout<<ans[i]<<' ';
	cout<<endl;
	cout<<"p: "<<p<<endl;
}
int main()
{
	int i,j;
	cin>>T;
	while(T--)
	{
		//Read
		scanf("%d%d",&M,&N);
		for(i=0;i<M;i++)
		for(j=1;j<=N;j++)
		scanf("%d",&a[i][j]);
		//Init
		memset(heap,0,sizeof(heap));
		n=0;
		p=0;
		//Solve
		for(i=0;i<M;i++) 
		sort(a[i]+1,a[i]+N+1);
		for(j=0;j<M-1;j++)
		{
			node t;
			t.i=1,t.j=1,t.last=false;
			Insert(t);
			for(i=0;i<N;i++)
			{
				//init
				t = GetTop();
				Extract();
				node t1,t2;
				ans[i] = a[p][t.i]+a[p+1][t.j];
				//update
				t1.i = t.i;
				t1.j = t.j+1;
				t1.last = true;
				Insert(t1);
				if(t.last == false)
				{
					t2.i = t.i+1;
					t2.j = t.j;
					t2.last = false;
					Insert(t2);
				}
			}
			p++;
			memset(heap,0,sizeof(heap));
			n=0;
			for(int k=1;k<=N;k++)
			a[p][k]=ans[k-1];
			
//			cout<<endl;
//			show();
//			system("pause");
		}
		for(int k=0;k<N-1;k++)
		cout<<ans[k]<<' ';
		cout<<ans[N-1]<<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