#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; #include "message.h" #include "kanapka.h" typedef long long ll; ll N; ll NoN; ll me; ll start; ll finish; ll size; ll size2; ll last; ll *aA; ll *aL; ll *aR; ll *aM; ll getA(ll i) { if (i<size2) return aA[i]; i-=size2; if (i<size) return GetTaste(start+i); return 0ll; } void setA(ll i, ll v) { aA[i]=v; } ll getL(ll i) { if (i<size2) return aL[i]; i-=size2; if (i<size) return min(0ll,GetTaste(start+i)); return 0ll; } void setL(ll i, ll v) { aL[i]=v; } ll getR(ll i) { if (i<size2) return aR[i]; i-=size2; if (i<size) return min(0ll,GetTaste(start+i)); return 0ll; } void setR(ll i, ll v) { aR[i]=v; } ll getM(ll i) { if (i<size2) return aM[i]; i-=size2; if (i<size) return min(0ll,GetTaste(start+i)); return 0ll; } void setM(ll i, ll v) { aM[i]=v; } int main() { bool parallel = true; NoN = NumberOfNodes(); me = MyNodeId(); N = GetN(); if (NoN*10>N) parallel = false; if (!parallel) if (me>0) return 0; if (parallel) { start = N*me/NoN; finish = N*(me+1)/NoN; } else { start = 0; finish = N; } size = finish-start; size2 = 1; while (size2<size) size2 *= 2; last = size2*2-2; aA = new ll[size2]; aL = new ll[size2]; aR = new ll[size2]; aM = new ll[size2]; for (ll i=size2-1; i>0; i--) { setA(i, getA(i*2)+getA(i*2+1)); } for (ll i=size2-1; i>0; i--) { setL(i, min(getL(i*2+1), getA(i*2+1)+getL(i*2))); } for (ll i=size2-1; i>0; i--) { setR(i, min(getR(i*2), getA(i*2)+getR(i*2+1))); } for (ll i=size2-1; i>0; i--) { setM(i, min(min(getM(i*2),getM(i*2+1)),getL(i*2)+getR(i*2+1))); } if (!parallel) { cout << getA(1)-getM(1) << endl; return 0; } PutLL(0, getA(1)); PutLL(0, getL(1)); PutLL(0, getR(1)); PutLL(0, getM(1)); Send(0); if (me>0) return 0; delete aA; delete aL; delete aR; delete aM; size2 = 1; while (size2<NoN) size2 *= 2; aA = new ll[size2*2]; aL = new ll[size2*2]; aR = new ll[size2*2]; aM = new ll[size2*2]; for (int i=0; i<NoN; i++) { int j = Receive(-1); aA[size2+j] = GetLL(j); aL[size2+j] = GetLL(j); aR[size2+j] = GetLL(j); aM[size2+j] = GetLL(j); } for (int j=NoN; j<size2; j++) { aA[size2+j] = 0ll; aL[size2+j] = 0ll; aR[size2+j] = 0ll; aM[size2+j] = 0ll; } for (ll i=size2-1; i>0; i--) { aA[i] = aA[i*2]+aA[i*2+1]; } for (ll i=size2-1; i>0; i--) { aL[i] = min(aL[i*2+1], aA[i*2+1]+aL[i*2]); } for (ll i=size2-1; i>0; i--) { aR[i] = min(aR[i*2], aA[i*2]+aR[i*2+1]); } for (ll i=size2-1; i>0; i--) { aM[i] = min(min(aM[i*2],aM[i*2+1]),aL[i*2]+aR[i*2+1]); } cout << aA[1]-aM[1] << 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 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 | #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; #include "message.h" #include "kanapka.h" typedef long long ll; ll N; ll NoN; ll me; ll start; ll finish; ll size; ll size2; ll last; ll *aA; ll *aL; ll *aR; ll *aM; ll getA(ll i) { if (i<size2) return aA[i]; i-=size2; if (i<size) return GetTaste(start+i); return 0ll; } void setA(ll i, ll v) { aA[i]=v; } ll getL(ll i) { if (i<size2) return aL[i]; i-=size2; if (i<size) return min(0ll,GetTaste(start+i)); return 0ll; } void setL(ll i, ll v) { aL[i]=v; } ll getR(ll i) { if (i<size2) return aR[i]; i-=size2; if (i<size) return min(0ll,GetTaste(start+i)); return 0ll; } void setR(ll i, ll v) { aR[i]=v; } ll getM(ll i) { if (i<size2) return aM[i]; i-=size2; if (i<size) return min(0ll,GetTaste(start+i)); return 0ll; } void setM(ll i, ll v) { aM[i]=v; } int main() { bool parallel = true; NoN = NumberOfNodes(); me = MyNodeId(); N = GetN(); if (NoN*10>N) parallel = false; if (!parallel) if (me>0) return 0; if (parallel) { start = N*me/NoN; finish = N*(me+1)/NoN; } else { start = 0; finish = N; } size = finish-start; size2 = 1; while (size2<size) size2 *= 2; last = size2*2-2; aA = new ll[size2]; aL = new ll[size2]; aR = new ll[size2]; aM = new ll[size2]; for (ll i=size2-1; i>0; i--) { setA(i, getA(i*2)+getA(i*2+1)); } for (ll i=size2-1; i>0; i--) { setL(i, min(getL(i*2+1), getA(i*2+1)+getL(i*2))); } for (ll i=size2-1; i>0; i--) { setR(i, min(getR(i*2), getA(i*2)+getR(i*2+1))); } for (ll i=size2-1; i>0; i--) { setM(i, min(min(getM(i*2),getM(i*2+1)),getL(i*2)+getR(i*2+1))); } if (!parallel) { cout << getA(1)-getM(1) << endl; return 0; } PutLL(0, getA(1)); PutLL(0, getL(1)); PutLL(0, getR(1)); PutLL(0, getM(1)); Send(0); if (me>0) return 0; delete aA; delete aL; delete aR; delete aM; size2 = 1; while (size2<NoN) size2 *= 2; aA = new ll[size2*2]; aL = new ll[size2*2]; aR = new ll[size2*2]; aM = new ll[size2*2]; for (int i=0; i<NoN; i++) { int j = Receive(-1); aA[size2+j] = GetLL(j); aL[size2+j] = GetLL(j); aR[size2+j] = GetLL(j); aM[size2+j] = GetLL(j); } for (int j=NoN; j<size2; j++) { aA[size2+j] = 0ll; aL[size2+j] = 0ll; aR[size2+j] = 0ll; aM[size2+j] = 0ll; } for (ll i=size2-1; i>0; i--) { aA[i] = aA[i*2]+aA[i*2+1]; } for (ll i=size2-1; i>0; i--) { aL[i] = min(aL[i*2+1], aA[i*2+1]+aL[i*2]); } for (ll i=size2-1; i>0; i--) { aR[i] = min(aR[i*2], aA[i*2]+aR[i*2+1]); } for (ll i=size2-1; i>0; i--) { aM[i] = min(min(aM[i*2],aM[i*2+1]),aL[i*2]+aR[i*2+1]); } cout << aA[1]-aM[1] << endl; return 0; } |