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

谁帮我看看我的代码呀?

Posted by wuzsh at 2007-07-15 14:47:47 on Problem 3264
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{
	int N;
	int Q;
	while(scanf("%d%d", &N, &Q) != EOF)
	{
    	vector<int> Height(N);
		int K = int(sqrt(N + 0.0));
		if(K*K != N)
		{
			++ K;
		}
		vector<int> LocalMax(K);
		vector<int> LocalMin(K);
		int j = 0;
		int M = N / K;
		int TmpMax = 0;
		int TmpMin = 1000000;
	    for(int i = 0; i < K; ++ i)
	    {
			int TmpMax = 0;
		    int TmpMin = 1000000;
			for(int j = 0; j < M; ++ j)
			{
				cin >> Height[i*M + j];
				if(TmpMax < Height[i*M + j])
				{
					TmpMax = Height[i*M + j];
				}
				if(TmpMin > Height[i*M + j])
				{
					TmpMin = Height[i*M + j];
				}
			}
			LocalMax[i] = TmpMax;
			LocalMin[i] = TmpMin;
	    }
		//copy(LocalMax.begin(), LocalMax.end(), ostream_iterator<int>(cout, " ")), cout << endl;
		//copy(LocalMin.begin(), LocalMin.end(), ostream_iterator<int>(cout, " ")), cout << endl;
	    for(int i = 0; i < Q; ++ i)
    	{
	    	int A;
		    int B;
	    	scanf("%d%d", &A, &B);
		    if(A == B)
	    	{
		    	printf("0\n");
		    }
		    else
	    	{
		    	int m = (A - 1) / M;
				int n = (B - 1) / M ;
				TmpMax = 0;
				TmpMin = 1000000;
                if(n == m)
				{
					for(int j = A - 1; j < B; ++ j)
					{
						if(TmpMax < Height[j])
						{
							TmpMax = Height[j];
						}
						if(TmpMin > Height[j])
						{
							TmpMin = Height[j];
						}
					}
				}
				else
				{
					for(int j = A - 1; j < (m + 1)*M && j < N; ++ j)
					{
						if(TmpMax < Height[j])
						{
							TmpMax = Height[j];
						}
						if(TmpMin > Height[j])
						{
							TmpMin = Height[j];
						}
					}
					for(int j = n*M; j < B && j < N; ++ j)
					{
						if(TmpMax < Height[j])
						{
							TmpMax = Height[j];
						}
						if(TmpMin > Height[j])
						{
							TmpMin = Height[j];
						}
					}
					if(n - m >= 2)
					{
						TmpMax = max(TmpMax, *max_element(LocalMax.begin() + (m + 1), LocalMax.begin() + n));
						TmpMin = min(TmpMin, *min_element(LocalMin.begin() + (m + 1), LocalMin.begin() + n));
					}
				}
				int Max = TmpMax;
				int Min = TmpMin;
		        printf("%d\n", Max - Min);
		    }
	    }
	}
}

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