ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1126. Magnetic Storms

Why I get WA?Here is my program.
Posted by blue dog 4 Apr 2002 19:07
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<assert.h>

FILE *inp,*outp;
long a[14101];
long b[14101];
long g[25101];
long m,i;
void Change(long t1,long t2){
    long k=a[t1];a[t1]=a[t2];a[t2]=k;
    k=b[t1];b[t1]=b[t2];b[t2]=k;
    g[b[t1]]=t1;g[b[t2]]=t2;
}
void Move(long i){
    if (i>1&&a[i]>a[i/2]){
        Change(i,i/2);
        Move(i/2);
        return;
    }else
    if (i*2<=m)
    if (a[i]<a[i*2]||a[i]<a[i*2+1])
        if (a[i*2]>a[i*2+1]){
            Change(i,i*2);
            Move(i*2);
        }else{
            Change(i,i*2+1);
            Move(i*2+1);
        };
}
main(){
    inp=fopen("input.004","r");assert(inp);
    outp=fopen("output.004","w");assert(outp);
    memset(a,255,sizeof(a));
    fscanf(inp,"%ld",&m);
    for (i=1;i<=m;i++){
      fscanf(inp,"%ld",&a[i]);
      if (a[i]==-1){
          fprintf(outp,"%ld\n",a[1]);
          exit(-1);
      };
      g[i]=i;b[g[i]]=i;
      Move(i);
    }
    i=m;
    while(true){
      fprintf(outp,"%ld\n",a[1]);

      i++;fscanf(inp,"%ld",&a[g[i-m]]);

      if (a[g[i-m]]==-1) exit(-1);
      g[i]=g[i-m];b[g[i]]=i;
      Move(g[i]);
    }
    fclose(outp);
}