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

Why WA ?

Posted by MyDarling_Na at 2008-05-19 19:54:00 on Problem 2823
#include<stdio.h>
#define MAX 2000001
int maxit[MAX],minit[MAX],ifm[MAX];
int a[2][MAX],cnt;
int n,k,temp=1;
int maxi=-999999999,mini=999999999;
void input()
{
	int i;
	scanf("%d %d",&n,&k);
	for(i=1;i<=n;i++) {
		scanf("%d",&ifm[i]);
	}
	for(i=1;i<=2*n;i++) {
		maxit[i]=-999999999;
		minit[i]=999999999;
	}
}
void setting()
{
	int i,V;
	for(;;) {
		if(temp>=n) break;
		temp*=2;
	}
	for(i=1;i<=n;i++) {
		V=i+temp-1;
		maxit[V]=ifm[i];
		minit[V]=ifm[i];
		while(V>=1) {
			if(V%2==0) V=V/2;
			else V=(V-1)/2;
			if(maxit[V]<ifm[i]) maxit[V]=ifm[i];
			if(minit[V]>ifm[i]) minit[V]=ifm[i];
		}
	}
}
void maxi_search(int l,int r)
{
	while(l<=r) {
		if(l==r) {
			if(maxi<maxit[l]) {
				maxi=maxit[l];
				break;
			}
		}
		if(l%2==1) {
			if(maxi<maxit[l]) {
				maxi=maxit[l];
			}
			l=(l+1)/2;
		}
		else {
			l/=2;
		}
		if(r%2==0) {
			if(maxi<maxit[r]) {
				maxi=maxit[r];
			}
			r=(r-1)/2;
		}
		else {
			r=r/2;
		}
	}
}
void mini_search(int l,int r)
{
	while(l<=r) {
		if(l==r) {
			if(mini>minit[l]) {
				mini=minit[l];
			}
			break;
		}
		if(l%2==1) {
			if(mini>minit[l]) {
				mini=minit[l];
			}
			l=(l+1)/2;
		}
		else {
			if(mini>minit[l]) {
				mini=minit[l];
			}
			l=l/2;
		}
		if(r%2==0) {
			if(mini>minit[r]) {
				mini=minit[r];
			}
			r=(r-1)/2;
		}
		else {
			if(mini>minit[r]) {
				mini=minit[r];
			}
			r=r/2;
		}
	}
}
void indextree()
{
	int i;
	setting();
	for(i=1;i<=n;i++) {
		if(i+k-1>n) break;
		maxi_search(i+temp-1,i+k+temp-2);
		mini_search(i+temp-1,i+k+temp-2);
		a[0][cnt]=mini;
		a[1][cnt++]=maxi;
		mini=99999999;
		maxi=-999999999;
	}
}
void output()
{
	int i,j;
	for(i=0;i<2;i++) {
		for(j=0;j<cnt;j++) {
			printf("%d ",a[i][j]);
		}
		printf("\n");
	}
}
int main()
{
	input();
	indextree();
	output();
	return 1;
}

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