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. ASP.NET 经典60道面试题

    转:http://bbs.chinaunix.net/thread-4065577-1-1.html ASP.NET 经典60道面试题 1. 简述 private. protected. public ...

  2. 2.Linq实用性技巧篇

    在论坛上经常会看到别人问,linq怎么实现递归,如何求笛卡尔积等问题..都可以用linq快速方便的解决..下面我就来个总的归纳 1.)递归 我们经常会遇到一个情况,就是想获取当前节点下的所有子节点.比 ...

  3. [Everyday Mathematics]20150118

    设 $X$ 是线性空间, $\phi_1,\cdots,\phi_n,\phi$ 是 $X$ 上的线性泛函, 试证: $$\bex \phi\in \span\sed{\phi_1,\cdots,\p ...

  4. ylbtech-Model-Account(通用账户模块设计)

    ylbtech-DatabaseDesgin:ylbtech-Model-Account(通用账户模块设计) ylbtech-Model-Account(通用账户模块设计) 1.A,数据库关系图(Da ...

  5. 《Python核心编程》 第三章 Python基础 - 练习

    创建文件: # -*- coding: gbk -*- #! /auto/ERP/python_core/chapter ''' Created on 2014年5月21日 @author: user ...

  6. 全栈工程师眼中的HTTP

    HTTP,是Web工程师每天打交道最多的一个基本协议.很多工作流程.性能优化都围绕HTTP协议来进行,但是我们对HTTP的理解是否全面呢?如果前端工程师和后台工程师坐在一起玩捉鬼游戏,他们对HTTP的 ...

  7. 差分信号(Differential Signal)

    差分信号(Differential Signal)在高速电路设计中的应用越来越广泛,电路中最关键的信号往往都要采用差分结构设计,什么另它这么倍受青睐呢?在 PCB 设计中又如何能保证其良好的性能呢?  ...

  8. 我的EC-final总结

    by.Max EC-final正式结束,也预示着我大学ICPC旅程的结束.回来睡了一天,现在也可以总结一下了 被告知参赛: 本来以为就会这样告别ACM-ICPC,没想到半个月前徐老师告诉我们SHU给我 ...

  9. 【原】Storm实战

    3.Storm实战 如何新建一个Storm 项目 本文简要概括如何新建一个Storm项目,步骤如下: 1.添加Storm 相关jar添加到类路径上. 2.如果使用多语言特性,将多语言实现的目录加到cl ...

  10. mybatis系列-09-订单商品数据模型

    9.1     数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空字段.外键字段 3.数据库级别表与 ...