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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//wersja jednokomp

#include "message.h"
#include "kollib.h"
//#include "myLibrary.cpp" // potem wyrzuciæ!
#include <cstdio>
#include <map>

const int INFTY = 1000000001;
const int MAX_QUERIES = 200;

const int randomNums[] =
{430036318,64058352,507461776,892147588,839106419,458540605,429853206,895596180,253334148,733054598,
 681325725,35187842,795007172,298928801,194524980,513565478,354838710,488021486,460615864,896786401,
 615985596,607196265,991271707,853358562,461958678,838160345,243659780,504470962,551255837,639606922,
 39490952,291726433,338816493,807306132,9521775,768822291,326212348,848841823,94485306,223181860,
 821527757,828760644,634327220,534104435,990173040,341013825,76113163,173192542,478164007,764488663,
 832728050,754051333,388805811,457777643,322855312,36500138,894100773,49958801,724234749,792718284,
 262123478,483199561,470686972,440382092,581987976,963499863,24719993,850184637,966856899,475936156,
 956907865,232947783,517075107,20752587, 862788782,513382367,824213386,226203193,149174475,96346935,
 297280801,494521928,221045565,431867428,92562640, 337778863,99398786,285256509,928342540,58931242,
 2807703,118594928,565141759,435163427,922544023,489913633, 233954894,559190650,723258157,875759148}; //cheat :)
 //kazde dwie ró¿ni¹ siê o min. 10^5, w zwi¹zku z czym dla nStudents \geq 10^4 nie powinno byæ powtórek

int random(int i, int N) // skalowanie do przedzialu 1..N
{
    int d = 1 + ( (long long) N * (long long)randomNums[i])/ 1000000000;   // floor(\alpha N), \alpha \in [0..1]
    if (d>N) d=N; //zabezpieczenie dla ma³ych N
}

struct QueryMember // korzystamy z tego, ¿e MAX_QUERIES < 256!
{
    unsigned char whichQuery;
    unsigned char whichEnd; // 0 = from, 1 = to

    QueryMember(int a, int b)
    {
        whichQuery = (unsigned char) a;
        whichEnd = (unsigned char) b;
    }
};

typedef std::multimap< int, QueryMember> QueryMap;
typedef std::multimap< int, int> MeetingMap;

struct Cursor
{
    int currentPos, oneBefore;
    int stepCounter;
    bool active;

    Cursor(int pos1, int pos0)
    {
        currentPos = pos1;
        oneBefore = pos0;
        stepCounter = 0;
        active = true;
    }

    void go()
    {
        stepCounter++;

        int tmp = FirstNeighbor(currentPos);
        if (tmp == oneBefore) tmp = SecondNeighbor(currentPos);
        oneBefore = currentPos;
        currentPos = tmp;
    }
};

class Searcher
{
    public:

    Searcher(); //konstruktor
    int getNum(); //zwraca numer instancji / = numer komputera na którym pracujê

    void addRequest(int from, int to);
    int answerRequest(int i); //done

    void search();

    int nextNode[2];
    int distToNext[2];

    /******************************************************/

    private:

    int nStudents, myNum, nQueries;
    int startPos;

    int foundDist[MAX_QUERIES+1][2];
    int requestCounter;

    QueryMap queryFields; // na jakiej pozycji; z którego zapytania ; pocz¹tek, czy koniec
    MeetingMap meetingFields; // na jakiej pozycji; z kim spotkanie

    void meetingWith(int num, int dir);
    int getDist(int x);
    void addMeeting(int pos, int who);

};

/********************************************************************************************************************/

int main()
{
    //srand (time(NULL));
    //handleInput(); //usunąć

    int nQueries = NumberOfQueries();
    if (nQueries == 0) return 0;

    Searcher mySearcher; //konstruktor niezupełnie trywialny, zawiera komunikację z pozostałymi

    for (int i=1; i<=nQueries; i++)
        mySearcher.addRequest( QueryFrom(i), QueryTo(i) );

    mySearcher.search();

    if (mySearcher.getNum() == 0)
    {
        for (int i=0; i<nQueries; i++)
            std::printf("%d\n", mySearcher.answerRequest(i));
    }
}

/********************************************************************************
                            IMPLEMENTATION
 *******************************************************************************/

Searcher::Searcher() //done
{
    nStudents = NumberOfStudents();
    myNum = MyNodeId();
    nQueries = NumberOfQueries();

    //startPos = 1 + rand() % nStudents; // bo numerowani od jedynki
    startPos = random(myNum, nStudents);

    requestCounter = 0;

    //std::printf("Start = %d\n", startPos);

    int nComps = NumberOfNodes();

    /** Komunikacja z pozosta³ymi! **/

    /* W zwi¹zku z "nowatorskim podejœciem" ta czêœæ niepotrzebna:
    for (int i=0; i< nComps; i++)
        if (i != myNum)
        {
            PutInt(i, startPos);
            Send(i);
        }

        for (int i=0; i< nComps; i++)
        if (i != myNum)
        {
            Receive(i);
            addMeeting( GetInt(i), i);
        }*/

   for (int i=0; i< nComps; i++)
   if (i != myNum)
   {
      addMeeting( random(i, nStudents), i);
   }

}

int Searcher::getNum() //done
{
    return myNum;
}

void Searcher::addRequest(int from, int to) //done
{
    queryFields.insert( std::pair<int, QueryMember>(from, QueryMember(requestCounter, 0))   );
    queryFields.insert( std::pair<int, QueryMember>(to, QueryMember(requestCounter, 1))   );
    foundDist[requestCounter][0] = foundDist[requestCounter][1] = INFTY;
    requestCounter++;
}

void Searcher::addMeeting(int pos, int who) //done
{
    meetingFields.insert( std::pair<int, int>(pos, who) );
}


int Searcher::answerRequest(int i) //done
{
    //std:printf("Request %d: %d, %d\n", i, foundDist[i][0], foundDist[i][1]);
    int d = (foundDist[i][0] > foundDist[i][1]) ? (foundDist[i][0]-foundDist[i][1]) : (foundDist[i][1]-foundDist[i][0]);
    return ( d < (nStudents-d) ? d : (nStudents-d));
}


void Searcher::search() //To be done
{
    Cursor cursors[] =
        {
            Cursor(startPos, SecondNeighbor(startPos)),
            Cursor(startPos, FirstNeighbor(startPos))
        };

    std::pair <QueryMap::iterator, QueryMap::iterator> ret;
    QueryMap::iterator it;
    MeetingMap::iterator it2;
    int j;

    //std::printf("Cursor %d, poz = %d, step = %d \n", j, cursors[j].currentPos, cursors[j].stepCounter);

    ret = queryFields.equal_range( startPos ); //sprawdzamy pozycje startowa
    for (it=ret.first; it!=ret.second; ++it)
    {
        foundDist[ (it->second).whichQuery ][ (it->second).whichEnd ] = 0;
        //std::printf("Found: %d %d %d \n", it->first, (it->second).whichQuery, (it->second).whichEnd);
    }
    queryFields.erase(ret.first, ret.second);

    if (queryFields.empty()) return; //jeżeli przypadkiem wszystkie zapytania były identyczne i pokrywajace się z startPos

    // meetingPointów - na razie(?) - nie pr ewidujemy

    while (true)
    {
        for (j = 0; j<2 ; j++)
        if (cursors[j].active)
        {
            cursors[j].go();

            //std::printf("Cursor %d, poz = %d, step = %d \n", j, cursors[j].currentPos, cursors[j].stepCounter);

            ret = queryFields.equal_range( cursors[j].currentPos ); // sprawdzamy, czy jest QueryType
            for (it=ret.first; it!=ret.second; ++it)
            {
                foundDist[ (it->second).whichQuery ][ (it->second).whichEnd ] = (2*j-1) * cursors[j].stepCounter; //tzn. +/- dla 0/1
                //std::printf("Found: %d %d %d \n", it->first, (it->second).whichQuery, (it->second).whichEnd);
            }

            queryFields.erase(ret.first, ret.second);

            it2 = meetingFields.find( cursors[j].currentPos );
            if ( it2 != meetingFields.end() )
            {
                cursors[j].active = false;
                nextNode[j] = it2->second;
                distToNext[j] = cursors[j].stepCounter;
            }
                //meetingWith(  it2->second,  j);
                //mo¿liwy bug je¿eli kilka meeting pointów siê pokrywa

           //if (queryFields.empty()) break; //tez niesuper, bo inny moze czekac!

         }

         if (!cursors[0].active && !cursors[1].active) break;
    } //jest problem: przy powtórkach program się zawiesi; tak samo np. przy jednej instancji tylko


    // Drugi etap: łączenie informacji

    if (myNum == 0)
    {
        int currentDist = distToNext[1];  //dystans miedzy current i earlier
        int currentNeighbour = nextNode[1]; // nastepny wierzcholek idziemy w strone 1
        int earlierNeighbour = 0; //dopiero co odwiedzony
        int tmpNext[2], tmpDir, tmpDist[2], a;

        while( currentNeighbour != 0 )
        {
            // przesylka w formacie:
            // ngh0: who ; dist
            // ngh1: who ; dist
            // 400x2 liczb


            Receive(currentNeighbour);

            for (int j=0; j<2; j++)
            {
                tmpNext[j] = GetInt(currentNeighbour); // wczytujemy obu sasiadów i dystansy do nich
                tmpDist[j] = GetInt(currentNeighbour); // dist
                if (tmpNext[j] != earlierNeighbour) tmpDir = j; //tmpdir wskazuje kierunek do obrania w nastepnym kroku
            }

            for (int k=0; k< nQueries; k++)
            {
                for (int j=0;j<2;j++)
                {
                    a = GetInt(currentNeighbour);
                    if (a!=INFTY) foundDist[k][j] = currentDist + a * (2*tmpDir -1);
                }
            }


            earlierNeighbour = currentNeighbour;
            currentNeighbour = tmpNext[tmpDir];
            currentDist += distToNext[ tmpDir];
        }
    }
    else
    {
        PutInt(0, nextNode[0]);
        PutInt(0, distToNext[0]);
        PutInt(0, nextNode[1]);
        PutInt(0, distToNext[1]);
        for (int k=0; k <nQueries; k++)
        {
            PutInt(0, foundDist[k][0]);
            PutInt(0, foundDist[k][1]);
        }
        Send(0);
    }

}

void Searcher::meetingWith(int num, int dir) //to be done
{
    //cursors[dir].active = false;
    //nextNode[dir] = num;
}