标签:整数 while return 需要 输入 开始 二次 span 变形
#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <map> #include <algorithm> #define LL long long int #define N 100005 using namespace std; int cmp(int a,int b) { if(a==b) return 0; return a<b?-1:1; } struct node { int num; int ang; }; LL ans[N]; void MergeArr(node num[], int left, int mid, int right) { node AL[N], AR[N]; int lenl=mid-left+1,lenr=right-mid; for (int i = 0;i < lenl;i++) AL[i] = num[left + i]; for (int i = 0;i < lenr;i++) AR[i] = num[mid + 1 + i]; int j = 0, k = 0,pos=left; int tempNum=-1,tempCnt=0; while (j < lenl&&k < lenr) { int fix=cmp(AL[j].num, AR[k].num); if (fix<=0) { num[pos++] = AL[j++],num[pos-1].ang+=k; if(num[pos-1].num==tempNum) num[pos-1].ang-=tempCnt; } //else if(fix==-1) num[pos++] = AL[j++]; else num[pos++] = AR[k++],num[pos-1].ang+=mid-(left+j)+1;//关键步骤 if(fix>0) { if(num[pos-1].num==tempNum) tempCnt++; else tempNum=num[pos-1].num,tempCnt=1; } } while (j < lenl) { num[pos++] = AL[j++]; num[pos-1].ang+=k; if(num[pos-1].num==tempNum) num[pos-1].ang-=tempCnt; } while (k < lenr) num[pos++] = AR[k++]; } void MergeSort(node num[], int left, int right) { if (left < right) { int mid = (right + left) / 2; MergeSort(num, left, mid); MergeSort(num, mid+1, right); MergeArr(num, left, mid, right); } } LL mat(LL num) { return num*(num+1)/2; } node num[N]; int n; int main() { cin.sync_with_stdio(false); while(cin>>n) { LL fuck=0; for(int i=0;i<n;i++) cin>>num[i].num,num[i].ang=0; MergeSort(num,0,n-1); for(int i=0;i<n;i++) fuck+=mat(num[i].ang); cout<<fuck<<endl; } return 0; }
标签:整数 while return 需要 输入 开始 二次 span 变形
原文地址:http://www.cnblogs.com/LukeStepByStep/p/6387507.html