题目描述

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的更多相关文章

  1. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  2. 使用 Arrays 类操作 Java 中的数组

    Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...

  3. 【转】java.util.Arrays.asList 的用法

    DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...

  4. System.arraycopy()和Arrays.copyOf()的区别

    先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...

  5. 计算机程序的思维逻辑 (31) - 剖析Arrays

    数组是存储多个同类型元素的基本数据结构,数组中的元素在内存连续存放,可以通过数组下标直接定位任意元素,相比我们在后续章节介绍的其他容器,效率非常高. 数组操作是计算机程序中的常见基本操作,Java中有 ...

  6. 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 ...

  7. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  8. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. mybatis系列: 简介以及使用

    目录 一.简介 二.简单使用 一.简介 MyBatis本质上就是对JDBC的封装,通过MyBatis完成CRUD. MyBatis在三层架构中负责持久层的,属于持久层框架. MyBatis的发展历程: ...

  2. Trino418版本动态加载catalog不需要重启集群修改思路及实现

          熟悉Trino 的同学应该都知道Trino新增.删除 catalog 都需要重启集群,这个生产环境里如果需要频繁增加数据源的场景是非常不友好的操作.     网上关于动态加载Catalog ...

  3. Mybatis插件功能

    1 插件的作用 在Mybatis执行SQL的生命周期中,会使用插件进行埋点,主要包括Executor.StatementHandler.ParameterHandler和ResultSetHandle ...

  4. XV6中的锁:MIT6.s081/6.828 lectrue10:Locking 以及 Lab8 locks Part1 心得

    这节课程的内容是锁(本节只讨论最基础的锁).其实锁本身就是一个很简单的概念,这里的简单包括 3 点: 概念简单,和实际生活中的锁可以类比,不像学习虚拟内存时,现实世界中几乎没有可以类比的对象,所以即使 ...

  5. api接口对接如何实现商品数据采集的

    在当前互联网行业中,快速准确地采集和处理大量数据是非常重要的一项任务.而实现商品数据采集则是许多企业和电商平台必须完成的任务之一.使用API接口对接进行商品数据采集可以大大提高数据采集效率和准确性.下 ...

  6. python 自定义排序

    需求:根据自定义的顺序就行排序 实现方法: res = [ {'name': 'RE', 'value': 2}, {'name': 'aa', 'value': 3}, {'name': 'RFM' ...

  7. 基于间隔密度的概念漂移检测算法mdm-DDM

    概念漂移 ​ 概念漂移是数据流挖掘领域中一个重要的研究点.传统的机器学习算法在操作时通常假设数据是静态的,其数据分布不会随着时间发生变化.然而对于真实的数据流来说,由于数据流天生的时间性,到达的数据的 ...

  8. linux中的sar命令

    linux中的sar命令 sar命令的安装 [root@localhost test]# yum install sysstat 安装成功! sar命令说明 语法格式 sar [ 选项 ] [ < ...

  9. 前端三件套系例之CSS——CSS3基础样式

    文章目录 1.宽和高 案例 2.字体属性 2-1 文字字体 2-2 字体大小 2-3 字重(粗细) 2-4 文本颜色 2-5 总结 2-6 案例 文字属性 3-1 文字对齐 3-2 文字装饰 3-3 ...

  10. linux常用命令(八) tar 打包、压缩、解包、解压缩

    Linux 常用的压缩与解压缩命令有:tar.gzip.gunzip.bzip2.bunzip2.compress .uncompress. zip. unzip.rar.unrar 等. 首先要弄清 ...