当前位置:网站首页>牛客月赛-分组求对数和
牛客月赛-分组求对数和
2022-07-01 21:45:00 【山中一扶苏】
原题链接
题目描述
牛牛幼稚园的小朋友在做游戏。
幼稚园共有 n 个小朋友,第 i 个小朋友有 s i s_i si个数字,第 i 个小朋友手中的第 jj 个数字记为 a i j ( 1 ≤ j ≤ s i ) a_{ij}(1\leq j \leq s_i) aij(1≤j≤si)。
现在牛牛老师想要知道有多少种不同的方式从两个不同的小朋友手中各取一个数字使得数字的和大于等于 k ?
由于可能的方式可能十分多,所以你只需要告诉牛牛这个方案数模 998244353 之后的结果就可以了(同一个小朋友手中相同的数字分别组成的答案看作是不同的)。
输入样例
3 7
4 2 3 1 5
2 4 1
2 8 2
输出样例
9
算法
(容斥原理思想 + 二分查找)
本题首先想到的是爆搜枚举,一一枚举搜索所有的相加大于 k 的可能,但这显然会超时,是平方级别的复杂度
所以,我们可以想到,本题 k 已经给出,所以我们只要求出每个数字与 k 的差值更大的数有多少个即可,那么就不难想到先排序然后用二分查找。
由于每个数字查找不能包含本组数字,我们就可以每次查找分两个区间,将大区间查找的数减去小区间中的数,最后累加即是答案。
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;const ll mod = 998244353;
int main()
{
ll n,k,cnt;
cin >> n >> k;
vector<ll> v[N],all;
for (int i = 0;i < n;i ++ ) {
int m;
cin >> m;
for (int j = 0;j < m;j ++ ) {
int x;
cin >> x;
v[i].push_back(x);
all.push_back(x);
cnt ++;
}
sort(v[i].begin() , v[i].end());
}
sort(all.begin(),all.end());
ll res = 0,t1 = 0,t2 = 0;
for (int i = 0;i < n;i ++ ) {
for (int j = 0;j < v[i].size();j ++ ) {
t1 = 0,t2 = 0;
t1 = lower_bound(v[i].begin(),v[i].end(),k - v[i][j]) - v[i].begin();
t1 = (ll)v[i].size() - t1;
t2 = lower_bound(all.begin(),all.end(),k - v[i][j]) - all.begin();
t2 = cnt - t2;
//cout << t2 << ' ' << t1 << '\n';
res = (res + t2 - t1) % mod;
}
}
cout << res / 2 << '\n';
return 0;
}
边栏推荐
- 按照功能对Boost库进行分类
- LIS (longest ascending subsequence) problem that can be understood [easy to understand]
- require与import的区别和使用
- 【juc学习之路第9天】屏障衍生工具
- Spark interview questions
- Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
- Training on the device with MIT | 256Kb memory
- JS how to get a list of elements in a collection object
- 【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
- Flume interview questions
猜你喜欢

Spark interview questions

linux下清理系统缓存并释放内存

Sonic云真机学习总结6 - 1.4.1服务端、agent端部署

并发编程系列之FutureTask源码学习笔记

Application of real estate management based on 3D GIS

locust 系列入门

MySQL系列之事务日志Redo log学习笔记

CIO's discussion and Analysis on the definition of high-performance it team

Go - exe corresponding to related dependency

详解JMM
随机推荐
Count the number of each character in the character
Relationship and difference between enterprise architecture and project management
【juc学习之路第9天】屏障衍生工具
并发编程系列之FutureTask源码学习笔记
Unity uses SQLite
信标委云原生专题组组长,任重道远!
Make a three digit number of all daffodils "recommended collection"
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
linux下清理系统缓存并释放内存
月入1W+的自媒体达人都会用到的运营工具
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
[monomer] recommended configuration of streaming information i-bpsv3 server
Redis配置与优化
Basic knowledge of ngnix
Mysql——》索引存储模型推演
MQ learning notes
Can you get a raise? Analysis on gold content of PMP certificate