#include "message.h"
#include "kanapka.h"
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second
int NODES, ID;
int N;
long long minp[111], maxp[111], s[111];
int main() {
NODES = NumberOfNodes();
ID = MyNodeId();
N = GetN();
// podzial
int first = 1LL*N*ID/NODES;
int last = 1LL*N*(ID+1)/NODES;
// kazdy osobno
long long minpref = 0, maxpref = 0, sum = 0, minsum = 0;
int all = last-first;
FOR(i,all) {
sum += GetTaste(first+i);
minpref = min(minpref, sum);
maxpref = max(maxpref, sum);
minsum = min(minsum, sum-maxpref);
}
//printf("%d -- %lld %lld %lld %lld\n", ID, sum, minpref, maxpref, minsum);
// wyslanie
if (ID>0) {
PutLL(0, sum);
PutLL(0, minpref);
PutLL(0, maxpref);
PutLL(0, minsum);
Send(0);
return 0;
}
// polaczenie
long long res = 0;
s[0] = sum;
minp[0] = minpref;
maxp[0] = maxpref;
FORI(i,NODES-1) {
Receive(i);
s[i] = GetLL(i);
minp[i] = GetLL(i);
maxp[i] = GetLL(i);
res = min(res, GetLL(i));
}
sum = 0;
maxpref = 0;
FOR(i,NODES) {
//printf("%lld %lld %lld\n", s[i], minp[i], maxp[i]);
res = min(res, sum+minp[i]-maxpref);
maxpref = max(maxpref, sum+maxp[i]);
sum += s[i];
}
res = -res;
FOR(i,NODES) res += s[i];
printf("%lld\n", res);
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 | #include "message.h" #include "kanapka.h" #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second int NODES, ID; int N; long long minp[111], maxp[111], s[111]; int main() { NODES = NumberOfNodes(); ID = MyNodeId(); N = GetN(); // podzial int first = 1LL*N*ID/NODES; int last = 1LL*N*(ID+1)/NODES; // kazdy osobno long long minpref = 0, maxpref = 0, sum = 0, minsum = 0; int all = last-first; FOR(i,all) { sum += GetTaste(first+i); minpref = min(minpref, sum); maxpref = max(maxpref, sum); minsum = min(minsum, sum-maxpref); } //printf("%d -- %lld %lld %lld %lld\n", ID, sum, minpref, maxpref, minsum); // wyslanie if (ID>0) { PutLL(0, sum); PutLL(0, minpref); PutLL(0, maxpref); PutLL(0, minsum); Send(0); return 0; } // polaczenie long long res = 0; s[0] = sum; minp[0] = minpref; maxp[0] = maxpref; FORI(i,NODES-1) { Receive(i); s[i] = GetLL(i); minp[i] = GetLL(i); maxp[i] = GetLL(i); res = min(res, GetLL(i)); } sum = 0; maxpref = 0; FOR(i,NODES) { //printf("%lld %lld %lld\n", s[i], minp[i], maxp[i]); res = min(res, sum+minp[i]-maxpref); maxpref = max(maxpref, sum+maxp[i]); sum += s[i]; } res = -res; FOR(i,NODES) res += s[i]; printf("%lld\n", res); return 0; } |
English