题目描述

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. 4、Mybatis核心配置文件详解

    4.1.environments <!-- environments标签:配置多个连接数据库的环境 default属性:设置默认使用的环境的id --> <environments ...

  2. 浅谈 Linux 下 vim 的使用

    Vim 是从 vi 发展出来的一个文本编辑器,其代码补全.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. Vi 是老式的字处理器,功能虽然已经很齐全了,但还有可以进步的地方.Vim 可 ...

  3. SSMS 显示行号

    SSMS 显示行号 SSMS2022--工具--选项--文本编辑器--所有语言--常规--勾选"行号"--确定.

  4. 「hdu - 5780」gcd

    link. 钦定 \(i>j\),研究得 \((x^i-1,x^j-1)\rightleftharpoons(x^i-x^j,x^j-1)\rightleftharpoons(x^j(x^{i- ...

  5. Solution -「CSP 2019」Partition

    Description Link. 给出一个数列,要求将其分成几段,使每段的和非严格递增,求最小的每段的和的平方和. Solution 注意到 \(a_{i}\) 为正,对于正数有这样的结论: \[a ...

  6. PowerDotNet平台化软件架构设计与实现系列(16):财务平台

    不同行业基本都会有自己独特的业务,甚至同行的不同企业之间的业务逻辑也会相差千里,只有最大程度抽象出通用性.标准性和普适性的系统才能够成为平台系统,平台系统开发的成本和难度可想而知. 个人深度参与或独立 ...

  7. JUC并发编程(1)—CompletableFuture详解

    @ 目录 CompletableFuture介绍 1.创建异步任务 2.CompletableFuture API ①. 获得结果和触发计算(get.getNow.join.complete) ②. ...

  8. 10.4 认识Capstone反汇编引擎

    Capstone 是一款开源的反汇编框架,目前该引擎支持的CPU架构包括x86.x64.ARM.MIPS.POWERPC.SPARC等,Capstone 的特点是快速.轻量级.易于使用,它可以良好地处 ...

  9. 使用playwright爬取魔笔小说网站并下载轻小说资源

    一.安装python 官网 下载python3.9及以上版本 二.安装playwright playwright是微软公司2020年初发布的新一代自动化测试工具,相较于目前最常用的Selenium,它 ...

  10. oracle RAC redhat

    RAC比较严格,如果操作系统不纯净,容易失败: 装备第一台VM:chkconfig sendmail offchkconfig iptables offchkconfig ip6talbes offs ...