第k小团+bitset优化——牛客多校第2场D
模拟bfs,以空团为起点,用堆维护当前最小的团,然后进行加点更新
在加入新点时要注意判重,并且用bitset来加速判断和转移构造
#include<bits/stdc++.h>
#include<bitset>
#include<queue>
using namespace std;
#define ll long long
char mp[][];
int n,k;
ll a[];
bitset<>bit[];
struct Node{
ll w;
bitset<>b;//团内点的状态
Node(){w=;b.reset();}
Node(ll w,bitset<>b):w(w),b(b){}
};
bool operator<(Node a,Node b){
return a.w>b.w;
}
priority_queue<Node> pq;//团按权值升序排列 int main(){
cin>>n>>k;
for(int i=;i<=n;i++)scanf("%lld",&a[i]);
for(int i=;i<=n;i++)scanf("%s",mp[i]+);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(mp[i][j]=='')bit[i][j]=;
else bit[i][j]=;
}
pq.push(Node());
int cnt=;ll ans=;
while(pq.size()){
Node cur=pq.top();pq.pop();
//for(int i=1;i<=n;i++)cout<<cur.b[i];
//puts(""); cnt++;
if(cnt==k){
ans=cur.w;
break;
}
//要保证所有在pq里出现过的团都是唯一的,即不能重复加入状态相同的团,
//更新时只加比当前团最大的点编号大的点
int start=;
for(int i=n;i>=;i--)if(cur.b[i]){
start=i+;break;
} for(int i=start;i<=n;i++)if(!cur.b[i]){//第i个点没在团里
//for(int j=1;j<=n;j++)cout<<bit[i][j];
//puts("");
if((cur.b&bit[i])==cur.b){
Node tmp=cur;
tmp.w+=a[i];
tmp.b[i]=;
pq.push(tmp);
}
}
} if(cnt==k)cout<<ans<<endl;
else cout<<-<<endl;
}
第k小团+bitset优化——牛客多校第2场D的更多相关文章
- 2020牛客多校第八场K题
__int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- 牛客多校第三场 F Planting Trees
牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...
- 牛客多校第四场sequence C (线段树+单调栈)
牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- 牛客多校第三场 G Removing Stones(分治+线段树)
牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...
- Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 找第\(k\)小团. 思路 用\(bitset\)来标记每个结点与哪些结点直接有边,然后进行\(bfs\),在判断新加入的点与现在有的点是否都 ...
- 牛客多校第七场 C Bit Compression 思维
链接:https://www.nowcoder.com/acm/contest/145/C来源:牛客网 A binary string s of length N = 2n is given. You ...
- 牛客多校第五场 F take
链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...
随机推荐
- string参考
#include <iostream> #include <string.h> class string { private: char *data; public: stri ...
- Django之template操作
一.模板渲染的原理 (一)使用 模板渲染首先有一个模板对象Template,然后有一个上下文对象Context,通过render方法进行渲染,最后返回字符串,render方法的本质还是调用了HttpR ...
- redis集群添加新节点
一.创建节点(接上文) 1.在H1服务器/root/soft目录下创建7002目录 2.将7001目录的配置文件redis.conf拷贝到7002,并修改配置文件的端口 3.进入 redis-5.0. ...
- 每天一个Linux命令:ls(1)
ls ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录). 格式 ls [-alrtAFR] [name...] 参数选项 参数 备注 -a 列出目录下的所有文件,包括以 . ...
- 【Nacos】本地集群部署
关于Nacos已经展开了四篇入门文章: 初探Nacos(一)-- 单机模式启动 初探Nacos(二)-- SpringCloud使用Nacos的服务注册与发现 初探Nacos(三)-- SpringB ...
- Spring Cloud Alibaba 从孵化到 "挂牌" 之旅
背景 2014 年,Spring Boot 1.0 发布.Spring Boot 的发布绝对是 Pivotal 历史上具有里程碑意义的事件,它让我们能够非常简便地开发 Spring 应用,屏蔽了各种配 ...
- PHP ftp_close() 函数
定义和用法 ftp_close() 函数关闭 FTP 连接. 语法 ftp_close(ftp_connection) 参数 描述 ftp_connection 必需.规定要关闭的 FTP 连接. 实 ...
- 【LeetCode 26】删除排序数组中的重复项
题目链接 [题解] 沙比提 [代码] class Solution { public: int removeDuplicates(vector<int>& nums) { if ( ...
- 排序算法(一) 插入排序及Java实现
代码实现: public void insertionSort(List<T> list, Comparator<T> comparator) { for (int i=1; ...
- CSS:CSS Positioning(定位)
ylbtech-CSS:CSS Positioning(定位) 1.返回顶部 1. CSS Positioning(定位) position 属性指定了元素的定位类型. position 属性的四个值 ...