Acdream1174 merge similar items

Source: Internet
Author: User

The question is:

Given n, a [1]... A [n], and M, B [1]... B [m]
Long long ans = 0;
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= m; j ++)
Ans + = ABS (A [I]-B [J]) * (I-j ); NM [] Quickly calculates the Ans 3 seconds. When this multiplication table is drawn, the B [I] is merged, then, after merging the same class items, the efficiency can be achieved. mlogn cannot use the binary of STL, and the card constant is too unreasonable.

#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#include <vector>using namespace std;typedef long long ll;const int max_n =100005;struct point{    int v,num;    point(ll a=0, ll b=0){      v=a; num=b;    }    bool operator <(const point A)const {      return v<A.v||(v==A.v&&num<A.num);    }}P[max_n];int n,m;int B[max_n],A[max_n],K[max_n];ll perAsum[max_n],perAnum[max_n],perAper[max_n];int binser(int n, int v){      int L=0, R =n;      while(L<R){        int mid = (L+R)/2;        if(K[mid]<=v) L=mid+1;        else R=mid;      }      return L;}int main(){     ll two=2,one =1;     int cas=0;      /*freopen("data.in","r",stdin);        freopen("data.out","w",stdout);    */while(scanf("%d%d",&n,&m)==2){          int V;          ll num=one*(n-1)*n/two;         for(int i=0; i<n; ++i){              scanf("%d",&V);              A[i]=V;              P[i]=point(V,i);         }         for(int i=0; i<m; ++i)            scanf("%d",&B[i]);         ll colu=0,per=0;         for(int i =0; i<n; i++){             colu=colu+one*A[i]*i;             per=per+A[i];         }         ll ans=0;         for(int i=0; i<m; ++i){             ans = ans+ colu;             colu= colu - per;             ans = ans - one*B[i]*num;             num = num - n;         }         sort(P,P+n);         perAnum[0]=P[0].num;         perAsum[0]=one*P[0].v*P[0].num;         perAper[0]=P[0].v;         K[0]=P[0].v;         for(int i=1; i<n; ++i){             perAnum[i] = perAnum[i-1]+P[i].num;             perAsum[i] = perAsum[i-1]+one*P[i].num*P[i].v;             perAper[i] = perAper[i-1]+P[i].v;             K[i]=P[i].v;         }         for(int i=0; i<m; ++i){             int loc = binser(n,B[i]);             if(loc<=0)continue;             loc-=1;             ll perA = perAsum[loc]-perAper[loc]*i;             ll perB = B[i]*(perAnum[loc]-one*(loc+1)*i);             ans = ans - ( perA-perB )*two;         }          printf("%I64d\n",ans);    }    return 0;}
View code

 

Acdream1174 merge similar items

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.