Cod sursa(job #254314)

Utilizator COstel0petrea constantin COstel0 Data 7 februarie 2009 11:08:43
Problema Cuburi2 Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.68 kb
#include<fstream.h>
ifstream f("cuburi2.in");
ofstream g("cuburi2.out");
typedef struct intrebare
  {int x,y;
  };
unsigned long n,m,a[100],v[100];
intrebare b[100];
int max,cost,p;
void main()
  {f>>n>>m;
   for(int i=1;i<=n;i++)
     f>>a[i];
   for(i=1;i<=m;i++)
     f>>b[i].x>>b[i].y;
   for(i=1;i<=m;i++)
     {max=0;cost=0;
      for(int j=1;j<=n;j++)
	v[j]=a[j];
      for(j=b[i].x;j<=b[i].y;j++)
	if(max<v[j])
	  {max=v[j];
	   p=j;
	  }
      for(j=b[i].x;j<p;j++)
	while(v[j])
	  {v[j+1]++;
	   v[j]--;
	   cost++;
	  }
      for(j=b[i].y;j>p;j--)
	while(v[j])
	  {v[j-1]++;
	   v[j]--;
	   cost++;
	  }
      g<<p<<" "<<cost<<endl;
     }
  }