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 相等? ...
随机推荐
- Windows 10系统设置多用户同时远程登录教程 and rdpwrap下载 and Win10多用户同时远程桌面的另类解决方案---支持1809和1909和2004版本V2.0
转载简书: Windows 10系统设置多用户同时远程登录教程 - 简书 (jianshu.com) 转载github: 发布 ·stascorp/rdpwrap ·GitHub 转载csdn: Wi ...
- js 操作符 —— 位操作符详解
这篇文章不讲一元运算符,也就是 + .-. *. /. =. ||. &&. !这些. 位运算符是在数字底层(即表示数字的32个数位)进行操作的. 有符号整数使用 32 位的前 31 ...
- Python ( 高级 第一部)
目录 time 时间模块 Python的内置方法 数字模块 随机模块 序列化模块 pickle 序列化模块 json os 系统模块 os shutil 模块 os,path 模块 文件压缩模块 z ...
- Docker工作管理中实用操作
"build once ,run anywhere" ...
- 优秀PHP程序员技术成长之路
按照了解的很多PHP/LNMP程序员的发展轨迹,结合个人经验体会,抽象出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定 ...
- vue学习笔记:vue.js基础语法
一.VUE 概述 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅 ...
- 去除button默认样式
不加样式之前的: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 实验一C语言开发环境使用和数据类型·运算符·表达式
#include<stdio.h> int main(){ printf(" o \n"); printf("<H>\n"); prin ...
- iPhone添加节假日日历地址
添加苹果节假日日历地址 手动订阅节假日: 打开"设置">"日历">"帐户">"添加帐户">&qu ...
- Java运算符(复习)
运算符 运算符:对字面量或者变量进行操作的符号 表达式:用运算符把字面量或者变量连接起来,符合Java语法的式子就可以称为表达式. 算数运算符 符号 作用 + 加法作用 - 减法作用 * 乘法作用 / ...