#include "teatr.h"
#include "message.h"
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
/*int GetN() {
return 5;
}
int GetElement(int x) {
if (x==0) return 5;
if (x==1) return 2;
if (x==2) return 4;
if (x==3) return 4;
if (x==4) return 3;
return -1;
}
int MyNodeId() {
return 0;
}
int NumberOfNodes() {
return 1;
}
void Receive(int a){
printf("Receive from %d: \n",a);
}
void Send(int a) {
printf("Send to %d\n",a);
}
void PutLL(int target,ll a) {
printf("Put %lld to %d\n",a,target);
}
ll GetLL(int source) {
printf("Get LL from %d: ",source);
ll a;
scanf("%lld",&a)?:0;
return a;
}*/
int id=MyNodeId();
int num=NumberOfNodes();
int n=GetN();
int ile[1000001],kek[1000001],pom[1000001];
long long licznik=0;
void merge(int a,int b,int c) {
int i=a,j=b+1,k=a;
while (i<=b && j<=c) {
if (kek[i]>kek[j]) licznik+=c-j+1,pom[k++]=kek[i++];
else pom[k++]=kek[j++];
}
while (i<=b) pom[k++]=kek[i++];
while (j<=c) pom[k++]=kek[j++];
memcpy(kek+a,pom+a,sizeof(int)*(c-a+1));
}
void mergesort(int a,int b) {
if (a==b) return;
int sr=(a+b)/2;
mergesort(a,sr);
mergesort(sr+1,b);
merge(a,sr,b);
}
int main() {
int zakres=(n-1)/num+1;
int poczatek=id*zakres;
int koniec=min(n,(id+1)*zakres);
int len=koniec-poczatek;
if (poczatek>=n) {
PutLL(0,0);
Send(0);
return 0;
}
for (int i=poczatek; i<koniec; i++) {
kek[i-poczatek]=GetElement(i);
ile[kek[i-poczatek]]++;
}
mergesort(0,len-1);
for (int i=1; i<=1000000; i++) ile[i]+=ile[i-1];
for (int i=0; i<poczatek; i++) licznik+=ile[GetElement(i)-1];
if (id>0) {
PutLL(0,licznik);
Send(0);
} else {
for (int i=1; i<num; i++) {
Receive(i);
licznik+=GetLL(i);
}
printf("%lld\n",licznik);
}
}
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 | #include "teatr.h" #include "message.h" #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long ll; /*int GetN() { return 5; } int GetElement(int x) { if (x==0) return 5; if (x==1) return 2; if (x==2) return 4; if (x==3) return 4; if (x==4) return 3; return -1; } int MyNodeId() { return 0; } int NumberOfNodes() { return 1; } void Receive(int a){ printf("Receive from %d: \n",a); } void Send(int a) { printf("Send to %d\n",a); } void PutLL(int target,ll a) { printf("Put %lld to %d\n",a,target); } ll GetLL(int source) { printf("Get LL from %d: ",source); ll a; scanf("%lld",&a)?:0; return a; }*/ int id=MyNodeId(); int num=NumberOfNodes(); int n=GetN(); int ile[1000001],kek[1000001],pom[1000001]; long long licznik=0; void merge(int a,int b,int c) { int i=a,j=b+1,k=a; while (i<=b && j<=c) { if (kek[i]>kek[j]) licznik+=c-j+1,pom[k++]=kek[i++]; else pom[k++]=kek[j++]; } while (i<=b) pom[k++]=kek[i++]; while (j<=c) pom[k++]=kek[j++]; memcpy(kek+a,pom+a,sizeof(int)*(c-a+1)); } void mergesort(int a,int b) { if (a==b) return; int sr=(a+b)/2; mergesort(a,sr); mergesort(sr+1,b); merge(a,sr,b); } int main() { int zakres=(n-1)/num+1; int poczatek=id*zakres; int koniec=min(n,(id+1)*zakres); int len=koniec-poczatek; if (poczatek>=n) { PutLL(0,0); Send(0); return 0; } for (int i=poczatek; i<koniec; i++) { kek[i-poczatek]=GetElement(i); ile[kek[i-poczatek]]++; } mergesort(0,len-1); for (int i=1; i<=1000000; i++) ile[i]+=ile[i-1]; for (int i=0; i<poczatek; i++) licznik+=ile[GetElement(i)-1]; if (id>0) { PutLL(0,licznik); Send(0); } else { for (int i=1; i<num; i++) { Receive(i); licznik+=GetLL(i); } printf("%lld\n",licznik); } } |
English