例题

小白月赛 困难卷积

题目

要求一个暴力算是 \(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. JZOJ 5354. 【NOIP2017提高A组模拟9.9】导弹拦截

    题目 如题 分析 第一问很简单, \(dp\) 即可(得先排序) 第二问很经典,最小路径覆盖问题,最大流解决 \(n-Maxflow\) \(Code\) #include<cstdio> ...

  2. reids(2)概述与安装

    前言 redis安装在Linux服务器上,系统为centos7,安装的版本为redis6.2.10 下载与安装 下载地址:https://redis.io/download/#redis-downlo ...

  3. 3D场景建模

    在这个虚拟现实的世界里,我们将会和你一起在云端构建属于自己的世界. 这里有一款3D场景编辑器可以让你在上面进行编辑.设计及渲染. 它是一个非常炫酷的软件,在这里,你可以尽情发挥你的想象力,创造出独一无 ...

  4. CLIP 改进工作串讲(下)学习笔记

    1.图像生成 1.1CLIPasso(semantically-aware object sketching) 将物体的照片变成简笔画的形式,希望即使有最少的线条,也能识别出来物体. 问题定义,在纸上 ...

  5. vue 从后往前循环数组的简洁写法

  6. python 操作配置文件(configparser模块)

    用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser 配置文件信息: [DEFAULT] ServerAliveInterval = 45 Com ...

  7. flink学习总结

    flink学习总结 1.Flink是什么? Apache Flink 是一个框架和分布式处理引擎,用于处理无界和有界数据流的状态计算. 2.为什么选择Flink? 1.流数据更加真实的反映了我们的生活 ...

  8. win10 系统修复IE11方法

    我也是手贱卸载了IE11,启用或关闭Windows功能里也没有Internet Explorer 11,今天意外发现了解决办法. 设置--应用--应用和功能--管理可选功能--添加功能--Intern ...

  9. c# 调用User32.dll

    获取当前窗口句柄:GetForegroundWindow()[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelli ...

  10. 关于sqlsugar二级缓存

    二级缓存 1.优点 (1).维护方便:SqlSugar的 二级缓存 支持单表和多表的 CRUD 自动缓存更新 ,减少了复杂的缓存KEY维护操作 (2).使用方便:可以直接在Queryable.With ...