例题

小白月赛 困难卷积

题目

要求一个暴力算是 \(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. js中的for循环,循环次数会多出一次。当循环到最后一个的时候,循环还会继续,并且此时i就变成remove?

    for (i in points) 改成 for(var i = 0; i < points,length; i++)

  2. Vue 禁止按钮多次点击 重复提交数据(指令实现)

    全局定义,方便调用 新建plugins.js export default { install (Vue) { // 防重复点击(指令实现) Vue.directive('preventReClick ...

  3. OpenLayers与百度高德等常见地图坐标系

    1. OpenLayers坐标系 OpenLayers中,创建一个Map,默认的显示(View)的投影坐标系是EPSG:3857,常见的另一个坐标系是 EPSG:4326 参考官方API文档:Open ...

  4. ChatGPT Java客户端,OpenAi的Java版本SDK已完成,请火速接入。

    已经支持OpenAI官方的全部api,有bug欢迎朋友们指出,互相学习. 源码地址:https://github.com/Grt1228/chatgpt-java 不对之处欢迎指正. 注意:由于这个接 ...

  5. 修复gitlab权限(docker方式搭建)

    docker exec -it gitlab update-permissions docker restart gitlab

  6. mybatis动态标签——choose、when、otherwise

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  7. [整合] 解决 Dell T640 安装显卡后风扇转速不降低

    最近实验室的dell T640服务器安装了新的GPU.但是安装后发现,风扇太吵了,于是开始着手解决风扇转速过高的问题. 试过ipmi,但是不好用. 最后发现使用racadm可以让服务器重新安静下来. ...

  8. 对称加密、非对称加密 与 HTTPS

    一.对称加密(Symmetric Cryptography)对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥(secret key). ...

  9. error Unnecessary return statement no-useless-return

    语法错误 原本是 addUser() {       this.$refs.addFormRef.validate((valid) => {         if (!valid) return ...

  10. SQL数据库常用命令

    数据库操作: 创建库:create database 数据库名 删除库:drop database 数据库名 选择库:use 数据库名 Database changed 关闭安全模式:set sql_ ...