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
#include "cielib.h"
#include <cstdio>
#include <algorithm>
#include <iostream>

int begin[501];
int end[501];
int d;
int r;
int count = 0;
int max_diff = 0;
int tryNr(int i, int *t) {
  int diff = std::min(begin[i], r - end[i]);
  t[i] = begin[i]-diff;
  czyCieplo(t);
  t[i] = end[i]+diff;
  int e = czyCieplo(t);
  t[i] = begin[i]-diff;
  
  //int b = czyCieplo(t);
  int mid = (begin[i] + end[i])/2;
  count+=2;
  if(e == 1) begin[i] = (begin[i] + end[i])/2 + 1;
  else if(czyCieplo(t) == 1) end[i] = (begin[i] + end[i])/2; 
  else {
    t[i] = mid;
    count++;
    int c = czyCieplo(t);
    if(c == 1) t[i] = end[i] = begin[i] = mid;
    else if(max_diff == 3 && begin[i] + 3 == end[i]) {
      begin[i]++; end[i]--;
    } else if(max_diff == 4 && begin[i] + 4 == end[i]) {
      begin[i]++; end[i]--;
    }
  } 
  t[i] = (begin[i] + end[i])/2;
  //count += 3;
}
void findMin(int nr, int* t) {
  int bg = begin[nr];
  int ed = end[nr];
  int diff = ed - bg;
  bg -= diff;
  ed += diff;
  t[nr] = bg;
  czyCieplo(t);
  t[nr] = ed;
  int e = czyCieplo(t);
  t[nr] = bg;
  int b = czyCieplo(t);
  if(e == 1) {
    int st = bg;
    int nd = begin[nr];
    while(st < nd) {
      int mid = (st + nd)/2;
      t[nr] = mid;
      czyCieplo(t);
      t[nr] = ed;
      int c = czyCieplo(t);
      if(c == 1) st = mid+1;
      else nd = mid;
      count+=2;
    }   
    t[nr] = begin[nr] = end[nr] = (st + ed)/2;
    
  } else if(b == 1) {
    int st = end[nr];
    int nd = ed;
    while(st < nd) {
      int mid = (st + nd + 1)/2;
      t[nr] = mid;
      czyCieplo(t);
      t[nr] = bg;
      int c = czyCieplo(t);
      if(c == 1) nd = mid - 1;
      else st = mid;
      count+=2;
    }    
    t[nr] = begin[nr] = end[nr] = (bg + st)/2;
  } else {
    tryNr(nr, t);
  }
}
void tryOneDiff(int nr, int* t) {
  int bg = begin[nr]; int bg0 = begin[nr];
  int ed = end[nr]; int ed0 = end[nr]; 
  if(bg +1 == ed) {
    if(bg > 0) bg--;
    else ed++;
  }
  int mid = bg + 1;
  t[nr] = bg;
  czyCieplo(t);
  t[nr] = ed;
  int e = czyCieplo(t);
  count++;
  if(e == 1) {
    t[nr] = begin[nr] = end[nr] = ed;
    return;
  }
  t[nr] = bg;
  int b = czyCieplo(t);
  count++;
  if(b == 1) {
    t[nr] = begin[nr] = end[nr] = bg;
    return;
  }
  t[nr] = mid;
  int c = czyCieplo(t);
  count++;
  if(c == 1) {
    t[nr] = begin[nr] = end[nr] = mid;
    return;
  } 
  begin[nr] = bg0;
  end[nr] = ed0; 
}
int main() {
	d = podajD();
	int *t = new int[d];
        max_diff = r = podajR();
        for(int i = 0; i < d; i++) { begin[i] = 0; end[i] = r;}
  
        bool diff = true;
        int it = 0;
        int maximum = 0;
        do {
          for(int i = 0; i < d; i++)
            t[i] = (begin[i] + end[i]+1)/2;

          for(int i = 0; i < d; i++) if(begin[i] < end[i]){
            int eb = end[i] - begin[i];
            //if (eb < max_diff) continue;
            if(eb <= 2){
              tryOneDiff(i, t);
            } else if(eb + end[i] <= r && 0 <= begin[i] - eb && r > 10) {
              findMin(i, t);
            } else {
              tryNr(i, t);
            }
          }
          diff = false;
          maximum = 0;
          for(int i = 0; i < d; i++) {
            if(begin[i] < end[i]) diff = true;
            if(end[i] - begin[i] > maximum) maximum = end[i] - begin[i];
            //printf("%d %d\n", begin[i], end[i]);
          }
          max_diff = maximum;
          //printf("%d\n", count);
          it++;
        } while(diff);

        znalazlem(begin);
}