例题

小白月赛 困难卷积

题目

要求一个暴力算是 \(O(n^2)\) 的东西

同时题目保证 \(\sum a[i] \leq 10^7\)

题解

\(\sum a[i] \leq 10^7\) 的含义是 \(a[i]\) 的值的种类数不超过 \(sqrt(10^7)\) 的意思

这样就可以用pair存储每个值出现的次数,并在 \(O(n * sqrt(n))\) 内可以求出答案

// #pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pb push_back
using namespace std;
const int N = 3e6 + 10;
int n, a[N], b[N];
map<int, int> ma, mb; inline int cal(int a, int b) {
return floor(sqrt(abs(a - b)));
} int main(){
cin >> n;
for(int i = 1; i <= n; i++) scanf("%d", &a[i]), ma[a[i]]++;
for(int i = 1; i <= n; i++) scanf("%d", &b[i]), mb[b[i]]++;
ll ans = 0;
for(auto i : ma) {
for(auto j : mb) {
ans += 1ll * i.second * j.second * cal(i.first, j.first);
}
}
printf("%lld\n", ans);
system("pause");
return 0;
}

a[i]之和小于N的含义的更多相关文章

  1. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  2. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  3. LeetCode 259. 3Sum Smaller (三数之和较小值) $

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  4. LeetCode 167. 两数之和 II - 输入有序数组

    题目: 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...

  5. LeetCode(15):三数之和

    Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...

  6. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  7. LeetCode15. 三数之和

    15. 三数之和 描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  8. 15. 3Sum[M]三数之和

    题目 Given an array nums of n integers, are three elements a, b, c in nums such that a+b+c=0? Find all ...

  9. [LeetCode] 15. 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  10. LeetCode 18. 四数之和(4Sum)

    题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...

随机推荐

  1. 【CTO变形记】整体系统思维-从现象到本质

    前言:我们的⼤脑⾥的认知不是⼀块⽩板,⽽是写满着密密麻麻对这个世界形成的各种观念.信念.塞满了对事物的各个表象,我们脑中的表象世界,对应着外部世界的各种事物. 如果感觉本篇看起来有点不适应,可以看看之 ...

  2. CSP2022 S游记

    9.26:开坑. 没报 J 组主要是因为 J 比较垃圾,去抢小朋友的一等没什么意思. 初赛 刚拿到试卷就直接懵了,这 tm 是给人做的题?宇宙射线是什么奇妙东西,还有基数排序我根本不会啊,这个阅读程序 ...

  3. AI 能多强「GitHub 热点速览」

    不知道 AI 在你那边是什么样的具象,在我这就是各种搞图:从给线稿图上色,到直接给你生成一张小色图,AI 最近是真出风头,本周热点速览也收录了 2 个 AI 项目,也和图像有关.还有一个和 AI 相关 ...

  4. angular 封装http请求、解决跨域问题

    第一部分:封装http请求 1.定义 ResultDataModel export interface ResultDataModel<T> { success: boolean; err ...

  5. Django-drf-序列化器高级用法之SerializerMethodField

    在Drf框架中的serializers.py序列化中, SerializerMethodField字段是一个只读字段.它通过调用附加到的序列化程序类上的方法来获取其值.它可用于将任何类型的数据添加到对 ...

  6. jQuery使用与语法

    jQuery安装 1.从官网Download jQuery | jQuery下载安装: 2.CDN在线加载: statistic CDN:https://cdn.staticfile.org/jque ...

  7. vue+html5实现上传图片

    原理:dispatchEvent 自定义触发事件,常用于自定义鼠标事件或点击事件 ,原生控件input打开上传文件方案:vue项目,点击自己的上传文件图标,通过dispatchEvent主动触发一个自 ...

  8. How to present a paper 怎么讲好一篇文献

    Author : 如果在冬夜一个旅人 Date : 2022/05/24 目录 背景说明 1 读文献 1.1 读文献的层次 1.2 论文阅读的首轮次序 2 讲文献 2.1 The Problem to ...

  9. IT之软件公司组织架构

    总结一下软件企业的组织架构,软件公司大部分都很年轻,整个行业还在调整期,一般规模都在300人以内,现在国内大型的软件产品公司都不是靠软件起家的,国内软件三强:华为.中信.海尔都是从硬件甚至是家电做起的 ...

  10. wpa_supplicant 交叉编译

    交叉编译 wpa_supplicant 指定交叉编译环境: CC=arm-linux-xxxxx-gcc 运行错误 :  wlan0: Unsupported driver 'nl80211' 在.c ...