Cod sursa(job #254689)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 7 februarie 2009 13:45:28
Problema Cuburi2 Scor 0
Compilator c Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 2.15 kb
#include <stdio.h>

int A[250001],N,M,x,y,poz,i,j,maxim,nr,ok,z,suma,B[250001],maxx,nrx,nry,a,b;

int abs(int a)
{
    if (a>=0) return a;
         return -a;
}

int main()
{
    freopen("cuburi2.in","r",stdin);
    scanf("%d %d", &N,&M);
    suma=0;
    for (i=1;i<=N;++i)
          {
                scanf("%d", &A[i]);
                B[i]=A[i];
                suma+=A[i];
          }
    
    freopen("cuburi2.out","w",stdout);
    M--; 
    scanf("%d %d", &x,&y);
    maxim=0;
    for (i=1;i<=N;++i)
         if (A[i]>maxim)
             {
              maxim=A[i];
              poz=i;
             }
    nr=0;
    z=maxim;
    for (i=1;i<=N;++i)
         if (i!=poz)
             {
               if (A[i]>=x && A[i]<=y)
                    {
                        nr+=(abs(poz-i))*A[i];
                        A[poz]+=A[i];
                        A[i]=0;
                    }
                    else
               if (A[i]>=x && A[i]>y)
                    {
                        nr+=(abs(poz-i))*y;
                        A[poz]+=y;
                        A[i]-=y;
                    }
            }
    printf("%ld %ld\n", poz,nr);
    a=nr;
    while(M--)
    {
        scanf("%d %d",&x,&y);
        if (A[poz]==suma && y<=z/2 && y-x!=1)
           {
              maxx=0;  
              for (j=x;j<=y;++j)
              {
                    nr=0;
                  for (i=1;i<=N;++i)
                       if (B[i]>=j)
                            nr++;
                  if (nr>maxx) maxx=nr;
              }
              printf("%ld %ld\n", poz,maxx);
              b=maxx;
            }      
        else
        if (y-x==1)
            {
              poz=y;
              for (i=1;i<=N;++i)
                    if (B[i]>=x) nrx++;   
              for (i=1;i<=N;++i)
                    if (B[i]>=y) nry++; 
              printf("%ld %ld\n", poz,abs(nry-nrx));
              b=maxx;
            }             
        else
        {
            poz++;
            printf("%ld %ld\n", poz,abs(b-a));
            a=b;
            b=abs(b-a);
        }
    }
    return 0;
}