#12【BZOJ3003】LED BFS+状压DP
题解:
看到区间修改先想一下差分
这题用差分是为了分析问题
现在的问题就变成了
原序列全为0,要使得特定的k个点变为1,每个操作改变x,y+1
然后我们会发现
对于二元组a,b我们要修改它,实际上是在找连续的区间相连,所以实质上是最短路
为什么要差分了才能这么做呢
因为原来的区间修改可能中间涉及了有效点而变得复杂
现在每次有效操作不会影响到中间的有效点
接下来状压dp这是显然的
对于每一个状态,枚举二元组(确定其中一个值,枚举另一个值)
这是因为被确定的这个值一定是其中一个二元组
在做这个之前,还需要证明的是三元组,4元组。。。是无效的
首先奇数是不可能的
看一下四元组,一定是可以拆分成两个无关的二元组
非常的智障的是我写最短路的时候只往一边走。。 应该要往两边扩展
#include <bits/stdc++.h>
using namespace std;
int T,n,m,k;
int len[],s[];
#define rg register
#define IL inline
#define N 10100
#define N2 1100000
#define INF 1e9
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
char ss[<<],*A=ss,*B=ss;
inline char gc(){return A==B&&(B=(A=ss)+fread(ss,,<<,stdin),A==B)?EOF:*A++;}
template<class T>void read(T&x){
rg int f=,c;while(c=gc(),c<||<c)if(c=='-')f=-;x=c^;
while(c=gc(),<c&&c<)x=(x<<)+(x<<)+(c^);x*=f;
}
bool ff[N],f[N];
queue<int> q;
int dis[][N],dp[N2];
int main()
{
freopen("noi.in","r",stdin);
freopen("noi.out","w",stdout);
std::ios::sync_with_stdio(false);
read(T);
for (rg int TT=;TT<=T;TT++)
{
memset(ff,,sizeof(ff));
int kk=;
read(n); read(m); read(k);
rg int x,y;
for (rg int i=;i<=m;i++)
read(x),ff[x]^=,ff[x+]^=;
for (rg int i=;i<=n+;i++)
if (ff[i]) s[++kk]=i;
for (rg int i=;i<=kk;i++)
for (rg int j=;j<=n+;j++)
dis[i][j]=INF;
for (rg int i=;i<=k;i++) read(len[i]);
for (rg int i=;i<N2;i++) dp[i]=INF;
for (rg int i=;i<=kk;i++)
{
memset(f,,sizeof(f));
q.push(s[i]); dis[i][s[i]]=;
while (!q.empty())
{
rg int x=q.front(); q.pop();
for (rg int j=;j<=k;j++)
if (x-len[j]>&&f[x-len[j]])
{
dis[i][x-len[j]]=dis[i][x]+;
f[x-len[j]]=;
q.push(x-len[j]);
}
for (rg int j=;j<=k;j++)
if (x+len[j]<=n+&&f[x+len[j]])
{
dis[i][x+len[j]]=dis[i][x]+;
f[x+len[j]]=;
q.push(x+len[j]);
}
}
}
/* for (int i=1;i<=kk;i++)
{
cout<<endl<<endl;
for (int j=1;j<=n;j++)
cout<<dis[i][j]<<" ";
} */
for (rg int i=;i<=kk;i++)
for (rg int j=;j<i;j++)
{
int x=(<<(i-))|(<<(j-));
dp[x]=dis[i][s[j]];
}
for (rg int i=;i<=(<<kk)-;i++)
{
rg int x;
for (rg int j=;j<kk;j++)
if ((i>>j)&)
{
x=j+; break;
}
for (rg int j=;j<kk;j++)
if ((i>>j)&&&j+!=x)
{
y=(<<j)|(<<(x-));
dp[i]=min(dp[i],dp[y]+dp[i^y]);
}
}
/* for (rg int i=1;i<=(1<<kk)-1;i++)
for (rg int j=i;j;j=i&(j-1))
{
for
dp[i]=min(dp[i],dp[j]+dp[i^j]);
} */
dp[(<<kk)-]==INF?cout<<-<<endl:cout<<dp[(<<kk)-]<<endl;
}
return ;
}
#12【BZOJ3003】LED BFS+状压DP的更多相关文章
- 【BZOJ3003】LED BFS+状压DP
		
[BZOJ3003]LED Description LED屏是由一个庞大的点阵小灯泡组成的,一开始每个小灯泡都不发光.每一行一共有N个小灯泡,依次标号为1~n.现在给定K个点,要求这K个点发光,其余点 ...
 - hdu 4856  Tunnels (bfs + 状压dp)
		
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
 - HDU-4856 Tunnels (BFS+状压DP)
		
Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...
 - 孤岛营救问题(BFS+状压DP)
		
孤岛营救问题 https://www.luogu.org/problemnew/show/P4011 用状压DP标记拿到钥匙的数量 #include<iostream> #include& ...
 - QDUOJ 来自xjy的签到题(bfs+状压dp)
		
来自xjy的签到题 Description 爱丽丝冒险来到了红皇后一个n*n大小的花园,每个格子由'.'或'#'表示,'.'表示爱丽丝可以到达这个格子,‘#’表示爱丽丝不能到达这个格子,爱丽丝每1 ...
 - HDU-3681-Prison Break(BFS+状压DP+二分)
		
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
 - 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP
		
这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...
 - CodeForces 907E Party(bfs+状压DP)
		
Arseny likes to organize parties and invite people to it. However, not only friends come to his part ...
 - HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)
		
题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...
 
随机推荐
- Python复习笔记(二)变量进阶
			
02. 可变和不可变类型 不可变类型,内存中的数据不允许被修改: 数字类型 int , bool , float , complex , long(2.x) 字符串 str 元组 tuple 可变类型 ...
 - postgresql行转列
			
问:怎么分页&&按条件&&按顺序&&姓名不重复查出数据? 答:其实就是行转列,那么,postgresql怎么进行转列呢,百度了下,大概有三种写法 写法1 ...
 - C#自绘蒙版控件,带延时隐藏显示,拷贝底图功能
			
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...
 - python(nmap模块、多线程模块)
			
http://xael.org/pages/python-nmap-en.html nmap模块 http://www.tutorialspoint.com/python/python_m ...
 - Java开发中各种集合框架简介
			
在大数据MapReduce作业开发中,我们经常会遇到一些大小表的join,这是如果这个小表足够“小”的话,我们可以使用进行“map-join-side”,这要就可以有效的降低reduce端的压力,但是 ...
 - Linux cache 缓存过大
			
linux cache 缓存过大 : 除重启服务 之外:直接释放内存方式之一: 修改配置释放cached内存: echo > /proc/sys/vm/drop_caches
 - 使用Jupyter lab前应该读的几篇文章
			
知乎上的一篇文章: 如何优雅的使用Jupyter? Jupyter Lab原来还有如下使用方式: 执行Shell命令 Hintland(提示命令).snippets(插入代码段).一键美化代码等功能( ...
 - jQuery - 字符串与json对象之间的转换
			
将字符串转换为json 在js中,我们是这样写的 var _data = eval('(' + data + ')'); 原理:eval() 函数可计算某个字符串,并执行其中的的 JavaScript ...
 - adb shell dumpsys meminfo [packagename] 输出内容的含义
			
Private Dirty:私有的脏内存页(还在使用中)的大小: Private Clean:私有的干净内存页(现在未使用了)的大小: 以上这二者相加,便是应用曾经申请过的内存空间大小.Priva ...
 - Android NDK编程
			
1.首先需要声明native方法: public native String helloWorldNdk(); public native String hello_World_Ndk(); 2.然后 ...