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 2020.07.28【NOIP提高组】模拟
2020.07.28[NOIP提高组]模拟 考试时状态不好,暴力不想打 结束前勉勉强强骗点分 已经不想说什么了······ \(T1\) 复制&粘贴2 逆推答案,枚举 \(k\),分类讨论 \ ...
- 03#Web 实战:实现下拉菜单
实现过程 实现一个简易的鼠标悬停菜单项显示其子项的下拉框控件.将用到 CSS 绝对定位.流式布局.动画等:JQuery 鼠标移入和移出事件.DOM 查找.效果图如下: HTML 结构: <div ...
- GPS地图生成01之概述
图片来源: Author: Tang Email: jianbo.tang@csu.edu.cn
- chai 3D 之网格对象
推荐:将 NSDT场景编辑器 加入你的3D开发工具链 介绍 网格对象是由三角形和顶点组成的形状 在 CHAI3D 中,多边形网格是定义多面体对象形状的顶点和三角形的集合. 顶点是一个位置以及其 ...
- Supported OPs and DPU Limitations
Currently Supported Operators source:https://www.xilinx.com/html_docs/xilinx2019_2/vitis_doc/zmw1606 ...
- Vue props配置项(属性)
功能:让组件接收外部传过来的数据 1.传递数据: <Demo name="XXX"/> 2.接收数据: (1)第一种方式(只接收): props:['name'] (2 ...
- 回顾——Dom节点类型
DOM 节点 在 HTML DOM 中,所有事物都是节点.DOM 是被视为节点树的 HTML. 根据 W3C 的 HTML DOM 标准,HTML 文档中的所有内容都是节点: 整个文档是一个文档节点 ...
- Laravel 框架根据经纬度计算在一定距离内的数据
$model = DB::table('table_name'); public static function scope_distance($model, $from_latitude, $fro ...
- CSS:盒子_每个元素都有两个盒子(《CSS世界》笔记-块级元素)
CSS:盒子_每个元素都有两个盒子(<CSS世界笔记>-块级元素) 1)CSS世界只有"块级盒子(block-level box)"和"内联盒子(inline ...
- Stanford NLP 在Python环境中安装、介绍及使用
Stanford NLP Stanford NLP提供了一系列自然语言分析工具.它能够给出基本的词形,词性,不管是公司名还是人名等,格式化的日期,时间,量词,并且能够标记句子的结构,语法形式和字词依赖 ...