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| ≤ 105, 1 ≤ 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=;
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=;i<=n;i++)
{
scanf("%I64d",&a[i].x);
a[i].no=(ll)i;
}
for(int i=;i<=m;i++)
{
scanf("%I64d",&b[i].x);
b[i].no=(ll)i;
}
sort(a+,a+n+,cmp);
sort(b+,b+m+,cmp);
tot=;
int tmp=;
for(int i=;i<=m;i++)
{
sum[i]=sum[i-]+b[i].no;
if(b[i].x!=b[i+].x)
{
c[tot].no=sum[i]-sum[tmp];
c[tot].p=(ll)i-tmp;
c[tot].x=b[i].x;
tmp=i;
tot++;
}
}
cnt=;sum[]=;tmp=;
for(int i=;i<=n;i++)
{
sum[i]=sum[i-]+a[i].no;
if(a[i].x!=a[i+].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=;i<cnt;i++)
{
for(int j=;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=;i<;i++)
{
ret+=ans[i]*i;
}
printf("%I64d\n",ret);
}

Codeforces Gym 100418B 暴力的更多相关文章

  1. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  2. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  3. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  4. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  5. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  6. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  7. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  8. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  9. Codeforces Gym 100803F There is No Alternative 暴力Kruskal

    There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...

随机推荐

  1. db file scattered read 等待事件

    db file scattered read 等待事件: 我们经常会见到db file scattered read  等待事件,在生产环境中,这个等待事件可能更为常见.这个事件表明用户进程正在读数据 ...

  2. 【转】MegaSAS RAID卡 BBU Learn Cycle周期的影响

    http://ju.outofmemory.cn/entry/140 背景 最近遇到有些带MegaSAS RAID卡的服务器,在业务高峰时突然IO负载飚升得很高,IO性能急剧下降,查了日志及各种设置最 ...

  3. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(5)

    数据操作与REST API(Data manipulation with REST API) ElasticSearch REST API可用于各种任务.多亏了它,我们可以管理索引,更改实例参数,检查 ...

  4. HDU5772 String problem 最大权闭合图+巧妙建图

    题意:自己看吧(不是很好说) 分析: 网络流:最大权闭合子图. 思路如下: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得 ...

  5. 标准Web系统的架构分层[转]

    标准Web系统的架构分层 – 转载请注明出处 1.架构体系分层图 在上图中我们描述了Web系统架构中的组成部分.并且给出了每一层常用的技术组件/服务实现.需要注意以下几点: 系统架构是灵活的,根据需求 ...

  6. 关于ShareSDK接入的各种问题,以及解决方案

    随着社交网络的流行,游戏接入分享已经是必然.毕竟这是非常好的一种推广方式.ShareSDK是一个非常好的内分享提供商!但是接入后发生的各种问题,下面给大家提供几个本人遇到的问题,以及解决方法: 1)微 ...

  7. 踩刹车——regularization

    从一个问题说起: 当我们使用H10去拟合曲线的时候,其实我们只想要H2的结果.如果从H10变回到H2呢? 所以我们只需要添加上限制条件:w3=...=w10=0即可.现在呢,我们可以放宽一点条件:任意 ...

  8. Python学习笔记——正则表达式

    今天把之前学的正则表达式好好总结总结. 一.元字符 . :  .表示可以匹配任意一个字符 \d  :  \d表示可以匹配任意一个数字 \D  : \D表示可以匹配任意一个非数字 \s  :  \s表示 ...

  9. Android版本判断

    尽管Android向下兼容不好,但是一个程序还是可以在多个平台上跑的.向下兼容不好,接口改变,新的平台上不能用旧的API,旧的平台更不可能用新的API,不等于一个平台需要一个APK.可以在高版本的SD ...

  10. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...