a[i]之和小于N的含义
例题
题目
要求一个暴力算是 \(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的含义的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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 ...
- 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 < ...
- LeetCode 167. 两数之和 II - 输入有序数组
题目: 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...
- LeetCode(15):三数之和
Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- LeetCode15. 三数之和
15. 三数之和 描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
- 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 ...
- [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 ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
随机推荐
- C语言初学习
首先我们先看一下什么是编译型语言和解释型语言: 解释:借助一个程序,那个程序能试图理解你的程序,然后按照你的要求执行 编译:借助一个程序,就像一个翻译,把你的程序翻译成计算机真正能懂得语言--机器语言 ...
- Vulhub 漏洞学习之:Couchdb
Vulhub 漏洞学习之:Couchdb 目录 Vulhub 漏洞学习之:Couchdb 1 Couchdb 垂直权限绕过漏洞(CVE-2017-12635) 1.1 漏洞利用过程 2 Couchdb ...
- Express 项目 Cookie 不能保存在 Application 中
在前后端分离项目中,因为跨域策略,所以 Cookie 不能存储在浏览器的 Application 中.只能在请求的响应头处看到有 Set-Cookie: 1️⃣前端发送请求添加 withCredent ...
- 09 安装虚拟机:Ubuntu Server 20.04
09 安装虚拟机:Ubuntu Server 20.04 9.1 取得安装映像档 9.2 建立虚拟机客体 请至Proxmox VE管理界面点选右上方的[建立VM],来到建立虚拟机客体的引导程序.引导程 ...
- 野火FreeRTOS计数信号量实验意外处理
编译的时候,一直说xSemaphoreCreateCounting这个函数没有定义. 最后发现,是FreeRTOSConfig.h文件中,没有将使能计数信号量的宏打开. 解决办法:在FreeRTOSC ...
- 概率生成函数(PGF)简记
基本搬运自<浅谈生成函数在掷骰子问题上的应用>. 对于定义在非负整数上的离散随机变量 \(X\),级数 \(F(z) = \sum\limits_{i\ge 0} \operatornam ...
- 数据每三位增加一个逗号(即千分符) js
使用 toLocaleString() 另一种使用场景
- vue2 项目引入Fontawesome
官网: https://fontawesome.com/ 1.安装 `` `powershell npm i --save @fortawesome/fontawesome-svg-core Usin ...
- centos6 编译安装 mysql5.6----------centos7编译安装MySQL5.7
centos6 编译安装 mysql5.6 安装依赖包 yum install -y ncurses-devel libaio-devel 安装cmake编译工具 cmake 定制功能:存储引擎.字 ...
- express的使用:数据库相关(三)
1.定义:用来组织,存储和管理数据的仓库 2.分类 a.MySQL(最广泛,流行度最高,community+enterprise),关系数据库,SQL数据库 b.Oracle (收费),关系数据库,S ...