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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "cielib.h"

struct Range
{
  unsigned int left;
  unsigned int right;
};

int main()
{
  unsigned int r = podajR();
  unsigned int d = podajD();
  int r_new = 1;
  while (r_new < (r+1)/2)
    r_new *= 2;
  bool fixed[d];
  Range ranges[d];
  int middle[d];
  unsigned int fixedSum = 0;
  for (unsigned int i = 0; i < d; ++i)
  {
    fixed[i] = false;
    ranges[i].left = 0;
    ranges[i].right = r;
    middle[i] = r/2;
  }
  for (unsigned int i = 0; i < d; ++i)
  {
    if (not fixed[i])
    {
      int mid = (ranges[i].left + ranges[i].right) / 2;
      middle[i] = ranges[i].left;
      czyCieplo(middle);
      middle[i] = ranges[i].right;
      if (czyCieplo(middle))
      {
        if (ranges[i].right == ranges[i].left+1)
        {
          fixed[i] = true;
          ranges[i].left = ranges[i].right;
        }
        else
        {
          ranges[i].left = ranges[i].right - r_new;
        }
      }
      else
      {
        middle[i] = ranges[i].left;
        if (czyCieplo(middle))
        {
          if (ranges[i].right == ranges[i].left+1)
          {
            fixed[i] = true;
            ranges[i].right = ranges[i].left;
          }
          else
          {
            ranges[i].right = ranges[i].left + r_new;
          }
        }
        else
        {
          ranges[i].left = mid - r_new/2;
          ranges[i].right = mid + r_new/2;
        }
      }
      middle[i] = (ranges[i].left + ranges[i].right) / 2;
      if (fixed[i])
        ++fixedSum;
    }
  }
  while ((fixedSum < d) and (r_new >= 2))
  {
    for (unsigned int i = 0; i < d; ++i)
    {
      if (not fixed[i])
      {
        int mid = (ranges[i].left + ranges[i].right) / 2;
        middle[i] = ranges[i].left;
        czyCieplo(middle);
        middle[i] = ranges[i].right;
        if (czyCieplo(middle))
        {
          if (ranges[i].right == ranges[i].left+1)
          {
            fixed[i] = true;
            ranges[i].left = ranges[i].right;
          }
          else
          {
            ranges[i].left = mid;
          }
        }
        else
        {
          middle[i] = ranges[i].left;
          if (czyCieplo(middle))
          {
            if (ranges[i].right == ranges[i].left+1)
            {
              fixed[i] = true;
              ranges[i].right = ranges[i].left;
            }
            else
            {
              ranges[i].right = mid;
            }
          }
          else
          {
            fixed[i] = true;
          }
        }
        middle[i] = (ranges[i].left + ranges[i].right) / 2;
        if (fixed[i])
          ++fixedSum;
      }
    }
    r_new /= 2;
  }
  if (fixedSum < d)
  {
    for (unsigned int i = 0; i < d; ++i)
    {
      if (not fixed[i])
      {
        if (ranges[i].left > 0)
        {
          middle[i] = ranges[i].left-1;
          czyCieplo(middle);
          middle[i] = ranges[i].right;
          if (czyCieplo(middle))
          {
            fixed[i] = true;
            ranges[i].left = ranges[i].right;
          }
          else
          {
            fixed[i] = true;
            ranges[i].right = ranges[i].left;
          }
        }
        else
        {
          middle[i] = ranges[i].right + 1;
          czyCieplo(middle);
          middle[i] = ranges[i].left;
          if (czyCieplo(middle))
          {
            fixed[i] = true;
            ranges[i].right = ranges[i].left;
          }
          else
          {
            fixed[i] = true;
            ranges[i].left = ranges[i].right;
          }
        }
        middle[i] = (ranges[i].left + ranges[i].right) / 2;
        if (fixed[i])
          ++fixedSum;
      }
    }
  }
  znalazlem(middle);
  return 0;
}