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

和题目要求一样,结果也一样,为什么总是wrong answer呢?

Posted by windy0128 at 2014-01-27 19:01:35 on Problem 1009
#include <iostream>
#include <math.h>
using namespace std;

struct block
{
	int value;
	int runlength;
};
int cmp(const void *a,const void *b)
{
	int *x=(int *)a;
    int *y=(int *)b;
	return *x-*y;
}
class image
{
	block *inblock;
	int numberofinblock;
	int numofoutblock;
	int piexlnum;
	int pointnumber;
	block *outblock;
	int *point;
	int linelength;
public:
	image()
	{linelength=0;inblock=outblock=NULL;numberofinblock=0;point=NULL;}
	bool input()
	{
		cin>>linelength;
		if(linelength ==0)
			return false;
		inblock=new block[1001];
		int v,r;
		int i=0;
		cin>>v>>r;
		while(v!=0)
		{
			inblock[i].value=v;
			inblock[i].runlength=r;
			i++;
			cin>>v>>r;
		}
		numberofinblock=i;
		cout<<linelength<<endl;
		return true;
	}
	void exchangeblock()//将runlength转化为分界
	{
		int dex=0;
		for(int i=0;i<numberofinblock;i++)
		{
			dex+=inblock[i].runlength;
			inblock[i].runlength=dex;
		}
		piexlnum=dex;
	}
	void setpoint()
	{
		int now=0;
		int sum=0;//存储分界点的个数
		point=new int[numberofinblock*6];
		for(int i=0;i<numberofinblock;i++)
		{
			point[sum]=now;sum++;
			if(now-linelength>-1)
			{
				point[sum]=now-linelength;//记录当前点的上面一个点的位置
				sum++;
			}
			if(now+linelength<piexlnum)
			{
				point[sum]=now+linelength;//记录当前点的下面一个点的位置
				sum++;
			}
			now=inblock[i].runlength-1;//定位到块后面一个点
			point[sum]=now;sum++;
			if(now-linelength>-1)
			{
				point[sum]=now-linelength;//记录当前点的下面一个点的位置
				sum++;
			}
			if(now+linelength<piexlnum)
			{
				point[sum]=now+linelength;//记录当前点的上面一个点的位置
				sum++;
			}
			now++;//定位到下一个块的开始一个点
		}
		pointnumber=sum;
		qsort(point,pointnumber,sizeof(int),cmp);
		int temp=point[0];
		int realnumber=1;
		for(int i=1;i<pointnumber;i++)
		{
			if(point[i]!=temp)
			{
				temp=point[i];
				point[realnumber]=point[i];
				realnumber++;
			}
		}
		pointnumber=realnumber;
	}
	void getoutblock()
	{
		outblock=new block[1001];
		int bnumber=0;
		for(int i=0;i<pointnumber-1;i++)
		{
			outblock[bnumber].runlength=1;
			outblock[bnumber].value=getads(point[i]);
			bnumber++;
			if(point[i+1]-point[i]>1)
			{
				outblock[bnumber].runlength=point[i+1]-point[i]-1;
			    outblock[bnumber].value=getads(point[i]+1);
			    bnumber++;
			}
		}
		outblock[bnumber].runlength=1;
		outblock[bnumber].value=getads(point[pointnumber-1]);
		bnumber++;
		int temp=outblock[0].value;
		numofoutblock=0;
		for(int i=1;i<bnumber;i++)
		{
			if(outblock[i].value!=temp)
			{
				temp=outblock[i].value;
				numofoutblock++;
				outblock[numofoutblock]=outblock[i];
			}
			else
			{
				outblock[numofoutblock].runlength+=outblock[i].runlength;
			}
		}
		numofoutblock++;
	}
	int getads(int pos)
	{
		int posvalue=getvalue(pos);
		int ads[8];
		int surroundnumbers=0;
		if(pos-linelength>-1)
		{
			ads[surroundnumbers]=abs(posvalue-getvalue(pos-linelength));
			surroundnumbers++;
			int flag=pos%linelength;
			if(flag!=0)
			{
				ads[surroundnumbers]=abs(posvalue-getvalue(pos-linelength-1));
			    surroundnumbers++;
			}
			if(flag!=linelength-1)
			{
				ads[surroundnumbers]=abs(posvalue-getvalue(pos-linelength+1));
			    surroundnumbers++;
			}
		}
		if(pos+linelength<piexlnum)
		{
			ads[surroundnumbers]=abs(posvalue-getvalue(pos+linelength));
			surroundnumbers++;
			int flag=pos%linelength;
			if(flag!=0)
			{
				ads[surroundnumbers]=abs(posvalue-getvalue(pos+linelength-1));
			    surroundnumbers++;
				ads[surroundnumbers]=abs(posvalue-getvalue(pos-1));
			    surroundnumbers++;
			}
			if(flag!=linelength-1)
			{
				ads[surroundnumbers]=abs(posvalue-getvalue(pos+linelength+1));
			    surroundnumbers++;
				ads[surroundnumbers]=abs(posvalue-getvalue(pos+1));
			    surroundnumbers++;
			}
		}
		int max=ads[0];
		for(int i=1;i<surroundnumbers;i++)
		{
			if(ads[i]>max)
				max=ads[i];
		}
		return max;
	}
	int getvalue(int pos)
	{
		int posvalue;
		for(int i=0;i<numberofinblock;i++)
		{
			if(pos<inblock[i].runlength)
			{
				posvalue=inblock[i].value;
				break;
			}
		}
		return posvalue;
	}
	void output()
	{
		for(int i=0;i<numofoutblock;i++)
			cout<<outblock[i].value<<" "<<outblock[i].runlength<<endl;
		cout<<0<<" "<<0<<endl;
	}
};

int main()
{
	image picture;
	while(picture.input())
	{
		picture.exchangeblock();
		picture.setpoint();
		picture.getoutblock();
		picture.output();
	}
	cout<<0<<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