about summary refs log tree commit diff
path: root/12/QG-2008/seqgame.cpp
blob: b1751c71b5b8502c33e0c86e640a11dd0bb39ad4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <set>

#define ABS(x) ((x < 0) ? -x : x)

using namespace std;

int
main()
{
  ifstream infile;
  ofstream outfile;
  short n;
  long i, c, min = 2e9, a;
  set<long> b;
  std::set<long>::iterator k;

  infile.open("SEQGAME.INP");
  infile >> n;
  for (i = 0; i < n; i++)
    {
      infile >> c;
      b.insert(c);
    }

  for (; n--;)
    {
      infile >> c;
      k = b.lower_bound(-c);
      if (*k == -c)
        {
          min = 0;
          break;
        }

      if (k != b.end())
        {
          i = ABS(*k + c);
          if (a < min)
            min = i;
        }

      if (k != b.begin())
        {
          k--;
          i = ABS(*k + c);
          if (a < min)
            min = i;
        }
    }
  infile.close();

  outfile.open("SEQGAME.OUT");
  outfile << min << endl;
  outfile.close();

  return 0;
}