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 04105330 at 2008-07-06 17:15:12 on Problem 2823
#include<iostream>
#include<queue>
#define MIN 1000000
#define MAX -1000000
using namespace std;
int min2[1000000],max2[1000000];
int main(){
	queue<int> p;
	int n,k ,i,j =1,x,len;
	scanf("%d%d",&n,&k);
	int min1,max1,lenmin = 0,lenmax = 0;
	min1 = MIN;
	max1 = MAX;
	for(i=0;i<k;i++)
	{
		scanf("%d",&x);
		if(x>max1)
			max1 = x;
		if(x<min1)
			min1 = x;
		p.push(x);
	}

	while(i<n){
		min2[lenmin++] = min1;
		max2[lenmax++] = max1;
		x = p.front();
		len = k-1;
		if(min1 == x||max1==x){
			p.pop();
			min1 = MIN;
			max1 = MAX;
			while(len>0){
				x = p.front();
				if(x>max1)
					max1 = x;
				if(x<min1)
					min1 = x;
				len--;
				p.push(x);
				p.pop();
			}
		}
		else 
		p.pop();
		scanf("%d",&x);
		if(x>max1)
			max1 = x;
		if(x<min1)
			min1 = x;
		p.push(x);
		i++;
	}
	min2[lenmin++] = min1;
	max2[lenmax++] = max1;
	for(i = 0;i<lenmin;i++)
		printf("%d ",min2[i]);

	printf("\n");
	for(i = 0;i<lenmax;i++)
		printf("%d ",max2[i]);
 		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