Codeforces Gym 100418B 暴力

Sum of sequences
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86686#problem/B

Description

You finished RIUSB ACBJSO university few years ago and started working hard and growing your carrier. At the some moment you tried to pass an interview at the XEDNAY company. You successfully answered all tricky questions about advanced algorithms and data structures and got the last one. Given two sequences A, B you need to find the following sum: 

Input

Input contains three lines. First contains two numbers lengths of sequences |A|, |B|. Second and third line contains |A| and |B| numbers separated by spaces (1 ≤ |A|, |B| ≤ 1051 ≤ Ai, Bi ≤ 104).

Output

Single line containing answer to the task.

Sample Input

5 4
3 4 5 4 4
1 2 3 4

Sample Output

42

HINT

题意

题解

发现i和j的取值范围都是1e5,所以不可能是n^2的,但是ai和bi的取值范围是1e4,所以把i,j和a[i],b[j]换一下就好了

然后就1e4的n^2直接暴力过去就好了

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;
const int N=100010;
int n,m,tot,cnt;
ll ans[N],ret,sum[N];
struct node
{
    ll x,no,p;
}a[N],b[N],c[N],d[N];
bool cmp(node n1,node n2)
{
    return n1.x<n2.x;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i].x);
        a[i].no=(ll)i;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%I64d",&b[i].x);
        b[i].no=(ll)i;
    }
    sort(a+1,a+n+1,cmp);
    sort(b+1,b+m+1,cmp);
    tot=1;
    int tmp=0;
    for(int i=1;i<=m;i++)
    {
        sum[i]=sum[i-1]+b[i].no;
        if(b[i].x!=b[i+1].x)
        {
            c[tot].no=sum[i]-sum[tmp];
            c[tot].p=(ll)i-tmp;
            c[tot].x=b[i].x;
            tmp=i;
            tot++;
        }
    }
    cnt=1;sum[0]=0;tmp=0;
    for(int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]+a[i].no;
        if(a[i].x!=a[i+1].x)
        {
            d[cnt].no=sum[i]-sum[tmp];
            d[cnt].p=(ll)i-tmp;
            d[cnt].x=a[i].x;
            tmp=i;
            cnt++;
        }
    }
 //   for(int i=1;i<=n;i++) cout<<a[i].x<<" "<<a[i].no<<endl;
 //   for(int i=1;i<tot;i++) cout<<c[i].x<<" "<<c[i].no<<" "<<c[i].p<<endl;
 //   for(int i=1;i<cnt;i++) cout<<d[i].x<<" "<<d[i].no<<" "<<d[i].p<<endl;
    for(int i=1;i<cnt;i++)
    {
        for(int j=1;j<tot;j++)
        {
            ans[abs(d[i].x-c[j].x)]+=d[i].no*c[j].p-c[j].no*d[i].p;
        }
    }
    for(int i=0;i<10010;i++)
    {
        ret+=ans[i]*i;
    }
    printf("%I64d
",ret);
}
原文地址:https://www.cnblogs.com/qscqesze/p/4711679.html