// Błędne rozwiązanie do zadania Działka 2.
// Na pojedynczym węźle wypisuje liczbę dostępnych kwadracików.
#include "dzialka.h"
#include "message.h"
#include <iostream>
#include <vector>
using namespace std;
const int infty=99999;
int fst[75000];
int arr[75000];
struct rect{
int left;
int depth;
rect( int l , int d ): left(l),depth(d) {}
};
long long binom(long long b) {
return b*(b-1)/2;
}
int main() {
const int NumRows = GetFieldHeight();
const int NumCols = GetFieldWidth();
const int N=NumberOfNodes();
const int ID=MyNodeId();
const int firstRow=(NumRows/N)*ID;
const int lastRow=(ID < N-1) ? firstRow+(NumRows/N)-1 : NumRows-1;
const int noRows=lastRow+1-firstRow;
const int msgSize=10000;
for(int col=0; col<NumCols; col++) arr[col]=(ID==0) ? 0 : infty;
for(int row=1; row<=noRows; row++)
for(int col=0; col<NumCols; col++)
if(IsUsableCell(firstRow+row-1, col))
arr[col] = (arr[col]==infty) ? arr[col] : arr[col]+1;
else arr[col]=0;
for(int col=0; col < NumCols; col++) {
if(ID > 0) {
if(!(col % msgSize)) Receive(ID-1);
fst[col]=GetInt(ID-1);
if(arr[col]==infty && fst[col] < infty)
arr[col]=fst[col]+noRows;
}
if(ID < N-1) {
PutInt(ID+1,arr[col]);
if(!((col + 1) % msgSize)) Send(ID+1);
}
}
if(ID < N-1 && (NumCols % msgSize)) Send(ID+1);
// DYNAMIK
long long myNumber=0;
for(int col=0; col < NumCols; col++) arr[col]=fst[col];
for(int row=1; row<=noRows; row++) {
vector<rect> rlist;
rlist.push_back(rect(-1, 0));
for(int col=0; col < NumCols; col++) {
if(!IsUsableCell(firstRow+row-1,col)) arr[col]=0;
else arr[col]++;
int dist=arr[col];
int my_col = col;
while(dist < rlist.back().depth) {
rect r=rlist.back();
rlist.pop_back();
my_col = r.left;
myNumber+=(r.depth-max(dist, rlist.back().depth))*binom(col-r.left+1);
}
if(dist > rlist.back().depth)
rlist.push_back(rect(my_col,dist));
}
while(rlist.size() > 1) {
rect r=rlist.back();
rlist.pop_back();
myNumber+=(r.depth-rlist.back().depth)*binom(NumCols-r.left+1);
}
}
if(ID > 0) {
PutLL(0,myNumber);
Send(0);
} else {
for(int i=1; i<N; ++i) {
Receive(i);
long long num=GetLL(i);
myNumber+=num;
}
cout << myNumber << endl;
}
return 0;
}
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 | // Błędne rozwiązanie do zadania Działka 2. // Na pojedynczym węźle wypisuje liczbę dostępnych kwadracików. #include "dzialka.h" #include "message.h" #include <iostream> #include <vector> using namespace std; const int infty=99999; int fst[75000]; int arr[75000]; struct rect{ int left; int depth; rect( int l , int d ): left(l),depth(d) {} }; long long binom(long long b) { return b*(b-1)/2; } int main() { const int NumRows = GetFieldHeight(); const int NumCols = GetFieldWidth(); const int N=NumberOfNodes(); const int ID=MyNodeId(); const int firstRow=(NumRows/N)*ID; const int lastRow=(ID < N-1) ? firstRow+(NumRows/N)-1 : NumRows-1; const int noRows=lastRow+1-firstRow; const int msgSize=10000; for(int col=0; col<NumCols; col++) arr[col]=(ID==0) ? 0 : infty; for(int row=1; row<=noRows; row++) for(int col=0; col<NumCols; col++) if(IsUsableCell(firstRow+row-1, col)) arr[col] = (arr[col]==infty) ? arr[col] : arr[col]+1; else arr[col]=0; for(int col=0; col < NumCols; col++) { if(ID > 0) { if(!(col % msgSize)) Receive(ID-1); fst[col]=GetInt(ID-1); if(arr[col]==infty && fst[col] < infty) arr[col]=fst[col]+noRows; } if(ID < N-1) { PutInt(ID+1,arr[col]); if(!((col + 1) % msgSize)) Send(ID+1); } } if(ID < N-1 && (NumCols % msgSize)) Send(ID+1); // DYNAMIK long long myNumber=0; for(int col=0; col < NumCols; col++) arr[col]=fst[col]; for(int row=1; row<=noRows; row++) { vector<rect> rlist; rlist.push_back(rect(-1, 0)); for(int col=0; col < NumCols; col++) { if(!IsUsableCell(firstRow+row-1,col)) arr[col]=0; else arr[col]++; int dist=arr[col]; int my_col = col; while(dist < rlist.back().depth) { rect r=rlist.back(); rlist.pop_back(); my_col = r.left; myNumber+=(r.depth-max(dist, rlist.back().depth))*binom(col-r.left+1); } if(dist > rlist.back().depth) rlist.push_back(rect(my_col,dist)); } while(rlist.size() > 1) { rect r=rlist.back(); rlist.pop_back(); myNumber+=(r.depth-rlist.back().depth)*binom(NumCols-r.left+1); } } if(ID > 0) { PutLL(0,myNumber); Send(0); } else { for(int i=1; i<N; ++i) { Receive(i); long long num=GetLL(i); myNumber+=num; } cout << myNumber << endl; } return 0; } |
English