sábado, 31 de marzo de 2012

413 - Up and Down Sequences, UVA

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#define db(a) cout << #a << " = " << a << endl
#define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
#define listar(lista) for (int i = 0; i < lista.size(); i++) db(lista[i])
using namespace std;
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
 #endif
 int aveup = 0, avedown = 0;
 double  sumup = 0, sumdown = 0;
 int creciente = 0, decreciente = 0;
 bool first = true, crece = true, decrece = true;
 int d, leido, cont = 0;
 while (scanf("%d", &d) != EOF) {
  if (d != 0) {
   cont++;
   if (first) {
    leido = d;
    first = false;
    continue;
   }
   if (leido == d) {
    if (crece)
     creciente++;
    if (decrece)
     decreciente++;
    leido = d;
    continue;
   }
   if (leido < d) {
    if (decrece) {
     if (creciente != decreciente) {
      sumdown += decreciente;
      avedown++;
     }
     decrece = false;
     decreciente = 0;
    }
    crece = true;
    creciente++;
    leido = d;
    continue;
   }
   if (leido > d) {
    if (crece) {
     if(creciente != decreciente) {
      sumup += creciente;
      aveup++;
     }
     crece = false;
     creciente = 0;
    }
    decrece = true;
    decreciente++;
    leido = d;
   }
  }
  else {
   if (cont == 0) break;
   if (creciente == decreciente)
    printf("Nr values = %d:  %.6f %.6f\n", cont, 0. , 0.);
   else {
    if (crece) sumup += creciente, aveup++;
    if (decrece) sumdown += decreciente, avedown++;
    printf("Nr values = %d:  %.6f %.6f\n",cont , (aveup != 0) ? sumup / aveup : 0, (avedown != 0) ? sumdown / avedown : 0);
   }
   creciente = decreciente = leido = cont = 0;
   first = crece = decrece = true;
   sumdown = sumup = aveup = avedown = 0;
  }
 }
 return 0;
}

No hay comentarios:

Publicar un comentario