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 相等? ...
随机推荐
- LG P4717 【模板】快速莫比乌斯/沃尔什变换 (FMT/FWT)
\[C_k = \sum_{i|j=k}A_i B_j \] 这样的或卷积可以做一次 \(\text{FWT}\),把数组变为 \(\widehat{A}_i = \sum_{j\subseteq i ...
- VueRouter 报错:inject() can only be used inside setup() or functional components
单独创建的一个文件,封装了登录函数的业务逻辑,出现了一个警告,紧接着就是报错:说不能读取到路由的 push 函数.路由必须在组件里面使用,不能在 ts 或 js 文件中使用. 还要注意的点是,在使用路 ...
- Linux操作命令(九)1.comm命令 2.diff命令 3.patch命令
1.comm 命令 比较文本文件的内容 comm 命令将逐行比较已经排序的两个文件.显示结果包括 3 列:第 1 列为只在第一个文件中找到的行,第 2 列为只在第二个文件中找到的行,第 3 列为两个文 ...
- Qt实现简单的TCP协议(客户端的实现)
1.QT提供了QTcpSocket类,可以直接实例化一个客户端.需要在pro文件中添加 QT += network 2.连接服务端 connect(connectbutton,SIGNAL(cli ...
- angular - 另一个ID为#######的进程当前正在运行ngcc
angular项目启动时报错 首先尝试npm install再次运行,看看它是否可以解决您的问题. 尝试ngcc手动运行. 删除这个文件 node_modules/@angular/compiler- ...
- staticmethod() 函数
title: staticmethod() 函数 author: 杨晓东 permalink: staticmethod date: 2021-10-02 11:27:04 categories: - ...
- Deer_GF之图片
Hi,今天介绍一下Deer_Gf里的图片组件. 框架介绍请移步[Deer_GF之框架介绍] 接下来为大家介绍一下框架里用到的图片组件及加载流程. 目录 大图(Texture)存 ...
- 客户端发送信息给服务器以及服务器接收客户端发来的信息(socket)
服务器端: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...
- Linux服务器监控性能测试
1.进程与线程的定义与区别 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,是系统进行资源分配和调度的一个独立单位[例如电脑上的不同程序] 线程是进程的一个实体,是cpu调度和分派的基本 ...
- Spring随意总结
Spring框架的优点 1.使用Spring的IOC容器,将对象之间的依赖关系交给Spring,降低组件之间的耦合性,让我们更专注于应用逻辑 2.可以提供众多服务,事务管理,WS等. 3.AOP的很好 ...