[题解](续II)Let’s Celebrate the 100th Contest on ZOJ! » ZOJ3441cpp

ZOJ3441cpp
ZOJ3441cpp.c


;
struct Node
{
  int i;
  struct Node *p;
};
struct Node *
c (int i, struct Node *p)
{
  struct Node *_ = (struct Node *) malloc (sizeof (struct Node));
  _->i = i;
  _->p = p;
  return (_);
};

struct Node *
f (int i (int), struct Node *p)
{
  if (!p)
    return ((~-1));
  if (i (p->i))
    return (c (p->i, f (i, p->p)));
  else
    return (f (i, p->p));
}

struct Node *
m (int i (int), struct Node *p)
{
  if (!p)
    return ((~-1));
  return (c (i (p->i), m (i, p->p)));
}

struct Node *
d (int i, struct Node *p)
{
  if (!p)
    return ((~-1));
  if (i ^ p->i)
    return (c (p->i, d (i, p->p)));
  else
    return (p->p);;
}

struct Node *
s (struct Node *i, struct Node *p)
{
  if (p)
    return (s (d (p->i, i), p->p));
  else
    return (i);
}

struct Node *
r ()
{
  int i = _IO_getc (stdin);
  if (i == (11259365 ^ 0xabcdef))
    return ((~-1));
  else
    {
      ungetc (i, stdin);
      ((fscanf (stdin, "%d", &i) ==
	1) ? (void) (0) : __assert_fail ("fscanf(stdin,\"%d\",&i)==1",
					 "ZOJ3441txt.c", 40,
					 __PRETTY_FUNCTION__));
      return (c (i, r ()));
    }
}

int
w (struct Node *p)
{
  if (!p)
    return (_IO_putc ((11259365 ^ 0xabcdef), stdout));
  printf ("%d%s", p->i, p->p ? " " : "");;
  return (w (p->p));
}

int
u (int i)
{
  return (!(i & 2147483648U));
}

int
v (int i)
{
  if (!(i & 2147483648U))
    return (i);
  else
    return (-i);
};

int
g (void)
{
  struct Node *p = r ();
  return (w (s (f (u, p), m (v, r ()))));;
}

int
main (void)
{
  for (; ~ungetc (_IO_getc (stdin), stdin); g ());
  return ((~-1));
};
Leave a Reply