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

O(n^2) 竟然 TLE OAQ

Posted by tigercosmos at 2013-04-21 00:23:20 on Problem 3494
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#define N 3000+10
using namespace std;
int a[N][N];
struct d{
	int h;//height
	int p;//position
	d(int _a,int _b):h(_a),p(_b)
	{
	}
};
int main()
{
	//freopen("test.cpp","r",stdin);
	int m,n;
	while(~scanf("%d %d",&m,&n)){
			if(m==0&&n==0)
				break;
			memset(a,-1,sizeof(a));
			for(int i=0;i<m;i++)
				for(int j=0;j<n;j++){
					scanf("%d",&a[i][j]);
				}

			for(int i=0;i<n;i++)
				for(int j=1;j<m;j++){
					if(a[j][i]!=0)
						a[j][i]+=a[j-1][i];
				}
				
			int max=0,sum;
			for(int i=0;i<m;i++){
				stack<d> s;
				sum=0;
				s.push(d(-1,0));				
				for(int j=0;j<=n;j++){
					int k=0,flag=1;
					while(s.top().h>a[i][j]){
						sum=s.top().h*(j-s.top().p);
						if(sum>max) max=sum;
						k = s.top().p;
						s.pop();
						flag=0;
					}
					if(!flag)
						s.push(d(a[i][j],k));
					else	
						s.push(d(a[i][j],j));
				}
			}

			printf("%d\n",max);	
		
	}
	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