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 相等? ...
随机推荐
- JZOJ 3889
\(\text{Problem}\) 小H是个善于思考的学生,她正在思考一个有关序列的问题. 她的面前浮现出了一个长度为 \(n\) 的序列 \({ai}\),她想找出两个非空的集合 \(S.T\). ...
- .Net 6 miniAPI
启动:1.双击 WebApplication1.exe文件 2.dotnet WebApplication1.dll --urls "http://localhost:5403;http ...
- Blender减面修改器
推荐:使用 NSDT场景设计器 快速搭建 3D场景. 使用Decimate修改器的目的是减少雕刻或 3D 扫描模型的面数. 要使用抽取修改器,请转到对象模式并选择要减少面数的任何模型. 在对象模式中选 ...
- 微信APP支付后台配置
1.申请商户号 ,申请成功后去配置证书APIv2 https://pay.weixin.qq.com/index.php/core/cert/api_cert#/ --MchKey 和MchI ...
- mysql8使用tmpfs内存磁盘当内存数据库的配置方法
序: 内存关系数据库没有找到开源好用的,很多都是商用.虽然mysql有memory引擎,但写是整体锁表,没法用. 一直想将mysql放入内存中,搜索n次资料,没找到合适的,可能之前思路不对. 最近在测 ...
- 布尔类型:boolean
布尔类型 基本介绍 布尔类型也叫boolean类型,其数据只允许取值true和false,无null boolean类型占1个字节 boolean类型适于逻辑运算,一般用于程序流程控制: if条件控制 ...
- KMP 算法实现
# coding=utf-8 def get_next_list(findding_str): # O(m) # 求一个字符串序列每个位置的最长相等前.后缀 j = 0 # 最长相等前缀的末位 nex ...
- P1002 [NOIP2002 普及组] 过河卒
P1002 [NOIP2002 普及组] 过河卒 题目见上. 一个经典的递推题 递推不会的看下面: https://www.cnblogs.com/haoningdeboke-2022/p/16247 ...
- 一次eureka的事故
本地起了一个微服务(不知道怎么起的),导致注册到微服务上了,不知所措.. 想了下,杀死对应微服务的进程id就可以了(记不住啊!!)
- 【易语言】exui超级列表框使用方法
优秀例程1 黑鸟 https://jiucaiwl.lanzoum.com/iLq8B0oswkuf