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| ≤ 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 暴力的更多相关文章
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- 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 ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces Gym 100803F There is No Alternative 暴力Kruskal
There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...
随机推荐
- kettle连接hadoop&hdfs图文详解
1 引言: 项目最近要引入大数据技术,使用其处理加工日上网话单数据,需要kettle把源系统的文本数据load到hadoop环境中 2 准备工作: 1 首先 要了解支持hadoop的Kettle版本情 ...
- 使用jQuery Mobile实现通讯录
jQuery Mobile 通讯录 拨打电话作者:方倍工作室 地址: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/ ...
- Emmet:HTML/CSS代码快速编写神器(转)
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- Ubuntu 安装 Sun JDK
1. 下载 Oracle网站下载JDK7 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1 ...
- mysql 错误解决
1. Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...
- POJ2763-Housewife Wind(树链剖分)
也是入门题,和上一题不一样的是权值在边上. 调了半天后来发现线段树写错了,build的时候没有pushup...蠢哭了好吗.... 做题还是不专心,太慢辣.. #include <algorit ...
- ocp 1Z0-051 106-140题解析
106. Examine the data inthe LIST_PRICE and MIN_PRICE columns of the PRODUCTS table: LIST_PRICE MIN_P ...
- nyoj 118 修路方案(最小生成树删边求多个最小生成树)
修路方案 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 南将军率领着许多部队,它们分别驻扎在N个不同的城市里,这些城市分别编号1~N,由于交通不太便利,南将军准备修 ...
- Android MuPDF 部署
MuPDF是一款轻量级的开源软件,可以用来阅读PDF文件.下载完源代码以后,想要运行成功,除了Android SDK之外,还需要Android NDK环境,因此有点麻烦. 但是一旦安装完必须的环境以后 ...
- Caused by: Cannot locate the chosen ObjectFactory implementation: spring - [unknown location] 的解决方式
1.添加网上所说的struts2 plugin jar包 2. <!-- Struts2配置 --> <filter> <filter-name>struts2&l ...