[CF1641D] Two Arrays
题目描述
Sam changed his school and on the first biology lesson he got a very interesting task about genes.
You are given $ n $ arrays, the $ i $ -th of them contains $ m $ different integers — $ a_{i,1}, a_{i,2},\ldots,a_{i,m} $ . Also you are given an array of integers $ w $ of length $ n $ .
Find the minimum value of $ w_i + w_j $ among all pairs of integers $ (i, j) $ ( $ 1 \le i, j \le n $ ), such that the numbers $ a_{i,1}, a_{i,2},\ldots,a_{i,m}, a_{j,1}, a_{j,2},\ldots,a_{j,m} $ are distinct.
输入格式
The first line contains two integers $ n $ , $ m $ ( $ 2 \leq n \leq 10^5 $ , $ 1 \le m \le 5 $ ).
The $ i $ -th of the next $ n $ lines starts with $ m $ distinct integers $ a_{i,1}, a_{i,2}, \ldots, a_{i,m} $ and then $ w_i $ follows ( $ 1\leq a_{i,j} \leq 10^9 $ , $ 1 \leq w_{i} \leq 10^9 $ ).
把所有数按照 \(w\) 排序之后,扫描 \(a_r\),对 \(r\) 去查一个最小的 \(l\) 使得 \(l,r\) 符合条件。
\(r\) 是不断增大的,如果 \(l\) 也跟着变大,答案肯定不如之前算的。所以 \(l\) 是稳定变小的。
我们现在要快速判断 \([1,l]\) 这个区间里是否存在一个和 \(a_r\) 互不相同的数。
互不相同很难计算,考虑容斥,枚举一个集合,统计前 \(l\) 个里面这个集合的出现次数。统计这里可以 map+哈希 统计。然后容斥完判断一下就行了。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=5;
int n,m,a[N][M],w[N],id[N],lsh[N*M],k,ans=2.1e9,pw[N*M],p[N];
mt19937 gen(time(0));
vector<int>g[N*M];
unordered_map<int,int>mp;
int cmp(int x,int y)
{
return w[x]<w[y];
}
int ok(int x)
{
int ret=0;
for(int j=1;j<(1<<m);j++)
{
int ans=0,c=0;
for(int k=0;k<m;k++)
if(j>>k&1)
ans^=pw[a[x][k]],c^=1;
ret+=mp[ans];
}
return ret;
}
void ins(int x,int op)
{
for(int j=1;j<(1<<m);j++)
{
int ans=0,c=0;
for(int k=0;k<m;k++)
if(j>>k&1)
ans^=pw[a[x][k]],c^=1;
mp[ans]+=c? op:-op;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
scanf("%d",a[i]+j),lsh[++k]=a[i][j];
scanf("%d",w+i),id[i]=i;
}
sort(lsh+1,lsh+k+1);
k=unique(lsh+1,lsh+k+1)-lsh-1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
a[i][j]=lower_bound(lsh+1,lsh+k+1,a[i][j])-lsh,g[a[i][j]].push_back(i),pw[a[i][j]]=gen()/2;
}
sort(id+1,id+n+1,cmp);
p[0]=n;
for(int i=1;i<=n;i++)
ins(i,1);
for(int i=1;i<=n;i++)
{
p[i]=p[i-1];
if(ok(id[i])==p[i])
continue;
while(p[i])
{
ins(id[p[i]],-1);
if(ok(id[i])==p[i]-1)
{
ins(id[p[i]],1);
break;
}
else
--p[i];
}
if(p[i])
ans=min(ans,w[id[p[i]]]+w[id[i]]);
}
printf("%d",ans>=2100000000? -1:ans);
}
[CF1641D] Two Arrays的更多相关文章
- Java程序员的日常—— Arrays工具类的使用
这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...
- 使用 Arrays 类操作 Java 中的数组
Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...
- 【转】java.util.Arrays.asList 的用法
DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- 计算机程序的思维逻辑 (31) - 剖析Arrays
数组是存储多个同类型元素的基本数据结构,数组中的元素在内存连续存放,可以通过数组下标直接定位任意元素,相比我们在后续章节介绍的其他容器,效率非常高. 数组操作是计算机程序中的常见基本操作,Java中有 ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Merge K Sorted Arrays
This problem can be solved by using a heap. The time is O(nlog(n)). Given m arrays, the minimum elem ...
随机推荐
- MySQL面试题全解析:准备面试所需的关键知识点和实战经验
MySQL有哪几种数据存储引擎?有什么区别? MySQL支持多种数据存储引擎,其中最常见的是MyISAM和InnoDB引擎.可以通过使用"show engines"命令查看MySQ ...
- 《Web安全基础》01. 基础知识
@ 目录 1:概念名词 1.1:域名 1.2:DNS 1.3:网站开发语言 1.4:后门 1.5:Web 1.6:Web 相关安全漏洞 2:数据包 2.1:HTTP 2.2:HTTPS 2.3:请求数 ...
- QA|ValueError: write to closed file报错怎么debug|IHRM接口自动化测试
unittest生成自动化测试报告时报错ValueError: write to closed file,如下图 代码如下: 原因排查:因为with open打开文件后会自动关闭,也就是上图16行执行 ...
- 每日一库:lumberjack -- 日志轮换和管理
在开发应用程序时,记录日志是一项关键的任务,以便在应用程序运行时追踪问题.监视性能和保留审计记录.Go 语言提供了灵活且强大的日志记录功能,可以通过多种方式配置和使用.其中一个常用的日志记录库是 gi ...
- EtherCAT转Modbus网关做为 MODBUS 从站配置
EtherCAT转Modbus网关做为 MODBUS 从站配置案例 兴达易控EtherCAT转Modbus网关可以用作MODBUS从站的配置.这种网关允许将Modbus协议与EtherCAT协议进行转 ...
- 聊聊JDK19特性之虚拟线程
1.前言 在读<深入理解JVM虚拟机>这本书前两章的时候整理了JDK从1.0到最新版本发展史,其中记录了JDK这么多年来演进过程中的一些趣闻及引人注目的一些特性,在调研JDK19新增特性的 ...
- 时髦称呼:SQL递归"语法糖"的用法
Oracle函数sys_connect_by_path 详解 语法:Oracle函数:sys_connect_by_path 主要用于树查询(层次查询) 以及 多列转行.其语法一般为: s ...
- Gossip in Hyperledger Fabric
1. Gossip协议基础 1.1 什么是分布式系统 分布式系统(Distributed System)是由多台计算机或计算节点组成的计算机系统,这些计算节点通过网络连接在一起,并协同工作以完成共同的 ...
- eclipse使用技巧和插件
eclipse使用技巧和插件 本篇文章只列举了一部分技巧和插件,并没有包括大家都知道的快捷键和技巧,而是一些不经常用但又很方便的功能. 一,技巧 给Eclipse添加更方便的提示功能:Windows– ...
- 【译】为什么命名“它”为依赖属性(DependencyProperty)
当我们创建新的类和成员时,我们花费了大量的时间和精力是它们尽可能的好用,好理解,好发现.通常我们会遵循.Net框架设计指南,尤其是会不断地研究这个新类与其他类,未来计划等内容之间的关系. 当命名依赖属 ...