#include <stdio.h>
#include <string.h>
#include "message.h"
#include "maklib.h"
int main(){
int nodesNumber = NumberOfNodes();
int arrSize = Size();
int id = MyNodeId();
int proportion = arrSize / nodesNumber;
int start = 0;
int end = 0;
int element = 0;
long long int sum = 0;
long long int max = 0;
long long int startingNeg = 0;
long long int endingNeg = 0;
long long int recStarting = 0;
long long int recEnding = 0;
long long int recMax = 0;
if( id ){
if( proportion ){
start = id * proportion;
end = ( id + 1 ) * proportion;
end = ( end > arrSize ) ? arrSize : end;
} else {
start = 0;
end = -1;
};
} else {
start = 0;
end = ( proportion ) ? proportion : arrSize;
};
max = ElementAt( start );
if( max >= 0 ){
sum = max;
startingNeg = 0;
} else {
sum = 0;
startingNeg = max;
};
endingNeg = 0;
for(int i = (start+1); i < end; ++i)
{
element = ElementAt( i );
sum += element;
if( sum >= max )
max = sum;
if( sum < 0 )
sum = 0;
if( element < 0 )
{
if( max > 0 ){
if( sum == 0 )
endingNeg += element;
else
endingNeg = 0;
} else
startingNeg += element;
};
};
if( id )
{
if( proportion )
{
PutLL( 0, startingNeg );
Send( 0 );
PutLL( 0, max );
Send( 0 );
PutLL( 0, endingNeg );
Send( 0 );
};
} else {
if( proportion )
{
for( int i = 1; i < nodesNumber; ++i)
{
Receive( i );
recStarting = GetLL( i );
Receive( i );
recMax = GetLL( i );
Receive( i );
recEnding = GetLL( i );
// odebrano
if( ( max + endingNeg + recStarting + recMax ) > max )
{
max += endingNeg + recStarting + recMax;
endingNeg = recEnding;
};
};
};
printf("%lld", max );
};
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 | #include <stdio.h> #include <string.h> #include "message.h" #include "maklib.h" int main(){ int nodesNumber = NumberOfNodes(); int arrSize = Size(); int id = MyNodeId(); int proportion = arrSize / nodesNumber; int start = 0; int end = 0; int element = 0; long long int sum = 0; long long int max = 0; long long int startingNeg = 0; long long int endingNeg = 0; long long int recStarting = 0; long long int recEnding = 0; long long int recMax = 0; if( id ){ if( proportion ){ start = id * proportion; end = ( id + 1 ) * proportion; end = ( end > arrSize ) ? arrSize : end; } else { start = 0; end = -1; }; } else { start = 0; end = ( proportion ) ? proportion : arrSize; }; max = ElementAt( start ); if( max >= 0 ){ sum = max; startingNeg = 0; } else { sum = 0; startingNeg = max; }; endingNeg = 0; for(int i = (start+1); i < end; ++i) { element = ElementAt( i ); sum += element; if( sum >= max ) max = sum; if( sum < 0 ) sum = 0; if( element < 0 ) { if( max > 0 ){ if( sum == 0 ) endingNeg += element; else endingNeg = 0; } else startingNeg += element; }; }; if( id ) { if( proportion ) { PutLL( 0, startingNeg ); Send( 0 ); PutLL( 0, max ); Send( 0 ); PutLL( 0, endingNeg ); Send( 0 ); }; } else { if( proportion ) { for( int i = 1; i < nodesNumber; ++i) { Receive( i ); recStarting = GetLL( i ); Receive( i ); recMax = GetLL( i ); Receive( i ); recEnding = GetLL( i ); // odebrano if( ( max + endingNeg + recStarting + recMax ) > max ) { max += endingNeg + recStarting + recMax; endingNeg = recEnding; }; }; }; printf("%lld", max ); }; return 0; }; |
English