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 ...
随机推荐
- Delphi 函数参数修饰中的var 、out和const
(1)var修饰符 添加var 是地址传递,会修改原有的变量 var s: string; begin S := 'Hello'; ChangeSVar(s); ShowMessage(S); e ...
- $( ).focus()与$( )[0].focus()区别
$( #id).focus()与$( #id)[0].focus()没有区别,因为id必须是唯一的.如果同一页面出现多个相同的ID(这是不符合w3c规范的),$(#id)也只会拿到第一个该ID,后面的 ...
- Android 打勾显示输入的密码
main.xml: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:and ...
- 抽屉显示控件SlidingDrawer入门
SlidingDrawer是一个抽屉控件,代码具体路径为:android.widget.SlidingDrawer,该控件从API Level3引入,在API 17及之后的版本将不再被支持.具体效果 ...
- 手势解锁自定义View
package com.rxx.view; import java.util.ArrayList; import java.util.List; import java.util.Timer; imp ...
- 【原】Storm实战
3.Storm实战 如何新建一个Storm 项目 本文简要概括如何新建一个Storm项目,步骤如下: 1.添加Storm 相关jar添加到类路径上. 2.如果使用多语言特性,将多语言实现的目录加到cl ...
- 直接调用系统Camera
关键思路: 初始化 组件: 创建并启动拍照intent: 使用回调函数onActivityResult()处理图像. 关键代码: 初始化 组件: takePicBtn = (Button) findV ...
- 配置youcompleteme碰到的问题
Q1: 进入vim里面后,可以使用ycm的相关命令来看到底出现啦什么问题? :Ycm YcmCompleter YcmForceCompileAndDiagnostics YcmToggleLogs ...
- Nginx和Tengine解决高并发和高可用,而非推荐Apache
什么是Nginx 什么是Tengine 看看国内大公司在用Nginx和Tengine吗? 步骤一:进入 https://www.taobao.com/,按F12.可看到 有很多APP对淘宝进行请求. ...
- Session.Abandon, Session.Clear和Session.Remove的区别
Session.Clear()就是把Session对象中的所有项目都删除了, Session对象里面啥都没有.但是Session对象还保留. Session.Abandon()就是把当前Session ...