#include "message.h" #include "maklib.h" #include <algorithm> #include <iostream> using namespace std; int main() { int size = Size(); int n = NumberOfNodes(); int m = MyNodeId(); int k = (size-1)/n + 1; long long all = 0; long long min_sum = 0; long long max_sum = 0; long long left_min_sum = 0; long long right_max_sum = 0; for(int i=1; i<=k && i+k*m<=size; ++i) { int elem = ElementAt(i + m*k); all += elem; if(min_sum > all) { min_sum = all; right_max_sum = all; } if(max_sum < all) { max_sum = all; left_min_sum = min_sum; } if(right_max_sum < all) { right_max_sum = all; } } long long left_max_value = max_sum; long long right_min_value = 0; if(max_sum>0) { right_min_value = all - max_sum; } long long max_value = 0; if(max_sum - left_min_sum > right_max_sum-min_sum) { max_value = max_sum - left_min_sum; } else { max_value = right_max_sum-min_sum; } if(m == 0) { long long *max_value_tab; long long *left_max_value_tab; long long *right_min_value_tab; long long *all_tab; max_value_tab = new long long[n]; left_max_value_tab = new long long[n]; right_min_value_tab = new long long[n]; all_tab = new long long[n]; max_value_tab[0] = max_value; left_max_value_tab[0] = left_max_value; right_min_value_tab[0] = right_min_value; all_tab[0] = all; for(int i=1; i<n; ++i) { int source = Receive(-1); max_value_tab[source] = GetLL(source); left_max_value_tab[source] = GetLL(source); right_min_value_tab[source] = GetLL(source); all_tab[source] = GetLL(source); } all = 0; min_sum = 0; max_sum = 0; left_min_sum = 0; right_max_sum = 0; for(int i=0; i<n; ++i) { all += all_tab[i]; long long all_min = all; long long all_max = all; if(i>0) { all_min += right_min_value_tab[i-1]; } else if(i< m-1) { all_max += left_max_value_tab[i+1]; } if(min_sum > all_min) { min_sum = all_min; right_max_sum = all_max; } if(max_sum < all_max) { max_sum = all_max; left_min_sum = min_sum; } if(right_max_sum < all_max) { right_max_sum = all_max; } } if(max_sum - left_min_sum > right_max_sum-min_sum) { max_value = max_sum - left_min_sum; } else { max_value = right_max_sum-min_sum; } for(int i=0;i<n;++i) { if(max_value < max_value_tab[i]) { max_value = max_value_tab[i]; } } delete [] max_value_tab; delete [] left_max_value_tab; delete [] right_min_value_tab; delete [] all_tab; cout << max_value; } else { PutLL(0,max_value); PutLL(0,left_max_value); PutLL(0,right_min_value); PutLL(0,all); Send(0); } 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 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 | #include "message.h" #include "maklib.h" #include <algorithm> #include <iostream> using namespace std; int main() { int size = Size(); int n = NumberOfNodes(); int m = MyNodeId(); int k = (size-1)/n + 1; long long all = 0; long long min_sum = 0; long long max_sum = 0; long long left_min_sum = 0; long long right_max_sum = 0; for(int i=1; i<=k && i+k*m<=size; ++i) { int elem = ElementAt(i + m*k); all += elem; if(min_sum > all) { min_sum = all; right_max_sum = all; } if(max_sum < all) { max_sum = all; left_min_sum = min_sum; } if(right_max_sum < all) { right_max_sum = all; } } long long left_max_value = max_sum; long long right_min_value = 0; if(max_sum>0) { right_min_value = all - max_sum; } long long max_value = 0; if(max_sum - left_min_sum > right_max_sum-min_sum) { max_value = max_sum - left_min_sum; } else { max_value = right_max_sum-min_sum; } if(m == 0) { long long *max_value_tab; long long *left_max_value_tab; long long *right_min_value_tab; long long *all_tab; max_value_tab = new long long[n]; left_max_value_tab = new long long[n]; right_min_value_tab = new long long[n]; all_tab = new long long[n]; max_value_tab[0] = max_value; left_max_value_tab[0] = left_max_value; right_min_value_tab[0] = right_min_value; all_tab[0] = all; for(int i=1; i<n; ++i) { int source = Receive(-1); max_value_tab[source] = GetLL(source); left_max_value_tab[source] = GetLL(source); right_min_value_tab[source] = GetLL(source); all_tab[source] = GetLL(source); } all = 0; min_sum = 0; max_sum = 0; left_min_sum = 0; right_max_sum = 0; for(int i=0; i<n; ++i) { all += all_tab[i]; long long all_min = all; long long all_max = all; if(i>0) { all_min += right_min_value_tab[i-1]; } else if(i< m-1) { all_max += left_max_value_tab[i+1]; } if(min_sum > all_min) { min_sum = all_min; right_max_sum = all_max; } if(max_sum < all_max) { max_sum = all_max; left_min_sum = min_sum; } if(right_max_sum < all_max) { right_max_sum = all_max; } } if(max_sum - left_min_sum > right_max_sum-min_sum) { max_value = max_sum - left_min_sum; } else { max_value = right_max_sum-min_sum; } for(int i=0;i<n;++i) { if(max_value < max_value_tab[i]) { max_value = max_value_tab[i]; } } delete [] max_value_tab; delete [] left_max_value_tab; delete [] right_min_value_tab; delete [] all_tab; cout << max_value; } else { PutLL(0,max_value); PutLL(0,left_max_value); PutLL(0,right_min_value); PutLL(0,all); Send(0); } return 0; } |