#include "maklib.h"
#include "message.h"
#include <iostream>
using namespace std;
int main()
{
long long biggestSum;
long long tempSum;
long long otherBiggestSum;
long long otherTempSum;
int startIdx;
int endIdx;
int startRetest=0;
int endRetest=0;
startIdx=(MyNodeId()*(Size()/NumberOfNodes()))+1;
endIdx=((MyNodeId()+1)*(Size()/NumberOfNodes()))+1;
if((MyNodeId()==(NumberOfNodes()-1))&&(Size()%NumberOfNodes())!=0){
endIdx=Size()+1;
}
tempSum=(long long)ElementAt(startIdx);
biggestSum=tempSum;
for(int i=(startIdx+1);i<endIdx;i++){
tempSum+=(long long)ElementAt(i);
if(tempSum<0){
tempSum=0;
}
if(tempSum>biggestSum){
biggestSum=tempSum;
}
}
if(tempSum==biggestSum){
PutLL(0,biggestSum);
PutLL(0,biggestSum);
}
else{
PutLL(0,0);
PutLL(0,biggestSum);
}
Send(0);
if(MyNodeId()==0){
for(int i=0;i<NumberOfNodes();i++){
Receive(i);
otherTempSum=GetLL(i);
otherBiggestSum=GetLL(i);
if(otherBiggestSum>biggestSum){
biggestSum=otherBiggestSum;
}
if(otherTempSum!=0){
if(i!=(NumberOfNodes()-1)){
endRetest=i+1;
}
}
else{
tempSum=(long long)ElementAt((startRetest*(Size()/NumberOfNodes()))+1);
for(int i=((startRetest*(Size()/NumberOfNodes()))+2);i<((endRetest+1)*(Size()/NumberOfNodes()))+1;i++){
tempSum+=(long long)ElementAt(i);
if(tempSum<0){
tempSum=0;
}
if(tempSum>biggestSum){
biggestSum=tempSum;
}
}
startRetest=i+1;
}
}
cout << biggestSum;
}
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 | #include "maklib.h" #include "message.h" #include <iostream> using namespace std; int main() { long long biggestSum; long long tempSum; long long otherBiggestSum; long long otherTempSum; int startIdx; int endIdx; int startRetest=0; int endRetest=0; startIdx=(MyNodeId()*(Size()/NumberOfNodes()))+1; endIdx=((MyNodeId()+1)*(Size()/NumberOfNodes()))+1; if((MyNodeId()==(NumberOfNodes()-1))&&(Size()%NumberOfNodes())!=0){ endIdx=Size()+1; } tempSum=(long long)ElementAt(startIdx); biggestSum=tempSum; for(int i=(startIdx+1);i<endIdx;i++){ tempSum+=(long long)ElementAt(i); if(tempSum<0){ tempSum=0; } if(tempSum>biggestSum){ biggestSum=tempSum; } } if(tempSum==biggestSum){ PutLL(0,biggestSum); PutLL(0,biggestSum); } else{ PutLL(0,0); PutLL(0,biggestSum); } Send(0); if(MyNodeId()==0){ for(int i=0;i<NumberOfNodes();i++){ Receive(i); otherTempSum=GetLL(i); otherBiggestSum=GetLL(i); if(otherBiggestSum>biggestSum){ biggestSum=otherBiggestSum; } if(otherTempSum!=0){ if(i!=(NumberOfNodes()-1)){ endRetest=i+1; } } else{ tempSum=(long long)ElementAt((startRetest*(Size()/NumberOfNodes()))+1); for(int i=((startRetest*(Size()/NumberOfNodes()))+2);i<((endRetest+1)*(Size()/NumberOfNodes()))+1;i++){ tempSum+=(long long)ElementAt(i); if(tempSum<0){ tempSum=0; } if(tempSum>biggestSum){ biggestSum=tempSum; } } startRetest=i+1; } } cout << biggestSum; } return 0; } |
English