题目描述

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. Java日志系列:Log4j使用和原理分析

    目录 一.简介 二.使用 三.日志级别 四.组件说明 Loggers Appenders Layouts 五.配置 加载初始化配置 配置文件加载 查看日志记录器的详细信息 六.Layout的格式 七. ...

  2. LVS专访阿里云席明贤,从视频云2.0到“数能生智”的超长畅谈

    这是一篇人物专访,源自LiveVideoStack与阿里云视频云负责人席明贤(花名右贤)的对话.面对风云变幻的内外环境,阿里云在视频云赛道是坚定向前的,右贤没有回避多媒体当下行业面临的困难以及业务面临 ...

  3. What is TLS ?

    TLS intrduction TLS是一种保障数据传输安全的一种技术(方案),它在传输层工作. 想知道TLS和SSL的关系,先看看他们的历史: 人们遇到数据传输安全问题 1994 SSL 1.0 1 ...

  4. 《Kali渗透基础》12. 无线渗透(二)

    @ 目录 1:无线协议栈 1.1:ifconfig 1.2:iwconfig 1.3:iw 1.4:iwlist 2:无线网卡配置 2.1:查看无线网卡 2.2:查看信道频率 2.3:扫描附近 AP ...

  5. Flink-读Kafka写Hive表

    1. 目标 使用Flink读取Kafka数据并实时写入Hive表. 2. 环境配置 EMR环境:Hadoop 3.3.3, Hive 3.1.3, Flink 1.16.0   根据官网描述: htt ...

  6. stencilJs学习之构建 Drawer 组件

    前言 在之前的学习中,我们已经掌握了 stencilJs 中的一些核心概念和基础知识,如装饰器 Prop.State.Event.Listen.Method.Component 以及生命周期方法.这些 ...

  7. 在 RedHat Enterprise、CentOS 或 Fedora Linux 上安装 MongoDB

    在 RedHat Enterprise.CentOS 或 Fedora Linux 上安装 MongoDB 1.大纲 备注:采用yum安装后,所有进程将自动在/usr/bin下,如下的mongo.mo ...

  8. LeetCode 周赛上分之旅 #44 同余前缀和问题与经典倍增 LCA 算法

    ️ 本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 和 BaguTree Pro 知识星球提问. 学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越 ...

  9. 《Python魔法大冒险》003 两个神奇的魔法工具

    魔法师:小鱼,要开始编写魔法般的Python程序,我们首先需要两个神奇的工具:Python解释器和代码编辑器. 小鱼:这两个工具是做什么的? 魔法师:你可以把Python解释器看作是一个魔法棒,只要你 ...

  10. ORA-01008:并非所有变量都已绑定-解决办法

    近期批量处理数据,后台用JAVA编写,连接Oracle数据库,程序运行报ORA-01008问题.解决这个问题时遇见的坑较多,下面复盘现象.问题提出解决办法,希望能帮到遇见同类问题的你. 调试问题: 后 ...