CF79D Password
题意:给定长度为n的0/1序列,初始值都为0。你每次可以在给定的l个长度中的\(a_i\)并将序列中长度为\(a_i\)的部分取反。使得最终状态为\(x_1\)~\(x_k\),求最少取反次数。
分析:看到这种区间整体变化的题,我们应该直觉想到差分(也可能想到线段树
先求出差分后的数组,然后每次操作相当于对距离为\(a_i\)的两点取反。
我们可以用类似求最短路的方法求出取反i,j位的最小操作次数。
因为最后为1的位置 \(\leq\) 10,所以操作的次数 \(\leq\) 20。
所以就可以大力状压了,方程见代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int dp[1<<20], num[1<<20], low[1<<20], f[10005], dis[10005], mn[22][22], len[105], cnt[22];
int n, k, l, tot=0;
queue<int> q;
inline int read()
{
int x=0,f=1; char ch=getchar();
for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
void Get_Shortest_Path(int rt)
{
memset(dis, 0x3f, sizeof(dis)); dis[cnt[rt]]=0;
q.push(cnt[rt]);
while (!q.empty())
{
int u=q.front(); q.pop();
for (int i=1; i<=l; i++)
{
if (u+len[i]<=n+1 && dis[u+len[i]]>dis[u]+1)
{dis[u+len[i]]=dis[u]+1; q.push(u+len[i]);}
if (u-len[i]>0 && dis[u-len[i]]>dis[u]+1)
{dis[u-len[i]]=dis[u]+1; q.push(u-len[i]);}
}
}
for (int i=1; i<=tot; i++) mn[rt][i]=dis[cnt[i]];
}
int main()
{
n=read(); k=read(); l=read();
for (int i=1; i<=k; i++) {int x=read(); f[x]=1;}
for (int i=1; i<=n+1; i++) if (f[i]^f[i-1]) cnt[++tot]=i;//差分
for (int i=1; i<=l; i++) len[i]=read();
for (int i=1; i<=tot; i++) Get_Shortest_Path(i);//预处理取反i,j位的最小操作次数
low[1]=0; for (int i=2; i<(1<<tot); i++) low[i]=low[i>>1]+1;//预处理log_2
for (int i=1; i<(1<<tot); i++) num[i]=num[i-(i&-i)]+1;//预处理二进制位数
memset(dp, 0x3f, sizeof(dp)); dp[0]=0;
for (int i=0; i<(1<<tot); i++)
{
if (num[i]&1) continue;
int j=((1<<tot)-1)^i; j=low[j&-j]+1;
for (int k=j+1; k<=tot; k++)
dp[i|(1<<(j-1))|(1<<(k-1))]=
min(dp[i|(1<<(j-1))|(1<<(k-1))],
dp[i]+mn[j][k]);
}//状压
printf("%d\n",dp[(1<<tot)-1]==inf?-1:dp[(1<<tot)-1]);
return 0;
}
CF79D Password的更多相关文章
- Luogu 3943 星空
原题是CF79D Password 很妙的题. 首先我们发现区间操作不太好弄,我们想办法把它转化成单点操作,这样子处理的办法会多一点. 方法当然是差分了. 定义差分数组$b_i = a_i \^ a_ ...
- 一句话题解&&总结
CF79D Password: 差分.两点取反,本质是匹配!最短路+状压DP 取反是套路,匹配是发现可以把操作进行目的化和阶段化,从而第二次转化问题. 且匹配不会影响别的位置答案 sequence 计 ...
- 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?
p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...
- your password has expired.to log in you must change it
今天应用挂了,log提示密码过期.客户端连接不上. 打开mysql,执行sql语句提示密码过期 执行set password=new password('123456'); 提示成功,但客户端仍然连接 ...
- MySql Access denied for user 'root'@'localhost' (using password:YES) 解决方案
关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入"mysql -uroot -pmyadmin"后出现以下错 ...
- [上架] iOS "app-specific password" 上架问题
当你的 Apple ID 改用双重认证密码时,上架 iOS App 需要去建立一个专用密码来登入 Apple ID 才能上架. 如果使用 Application Loader 上传时,得到这个讯息: ...
- [LeetCode] Strong Password Checker 密码强度检查器
A password is considered strong if below conditions are all met: It has at least 6 characters and at ...
- mysql 错误 ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number 解决办法
MySQL创建用户(包括密码)时,会提示ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number: 问题原因: ...
- phpmyadmin #1045 - Access denied for user 'root'@'localhost' (using password: NO)
phpmyadmin访问遇到1045问题 #1045 - Access denied for user 'root'@'localhost' (using password: NO) 解决办法 找到p ...
随机推荐
- #region 常量和静态变量静态类readonly
#region 常量和静态变量静态类readonly //---------------------------------------------------------------------- ...
- 迷你MVVM框架 avalonjs 0.91发布
本版本修了一些BUG与不合理的地方,感谢感谢ztz, 民工精髓, 姚立, qiangtou等人指正. 处理AMD加载 旧式IE下移除script节点内存泄漏的问题 fix firefox 全系列vis ...
- 判断单选框选中不成功,$("#xx").attr("checked")undefined
判断单选框选中状态,各种都不行,受到https://www.cnblogs.com/yxwkf/p/4853014.html 的启发,相关引用: 原来.在jquery1.6版本号便对此做出了改动: [ ...
- Implement Trie (Prefix Tree)实现字典树
[抄题]: Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inp ...
- CiteSpace安装使用简介
一.简介 CiteSpaceⅡ基于JAVA平台的信息可视化软,是美国Drexel大学陈超美(Chaomei Chen)教授开发的,用于文献引文网络分析的信息,作为文献计量学方面最先进的分析工具之一,是 ...
- ecshop适配php
https://www.cnblogs.com/xiwang6428/p/5460155.html
- Laravel中Trait的用法实例详解
本文实例讲述了Laravel中Trait的用法.分享给大家供大家参考,具体如下: 看看PHP官方手册对Trait的定义: 自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 trait ...
- ResourceUtils读取properties文件
注意: properties文件要放在classPath下面,也就是与src下. path.properties(在properties文件中\代表着没有完,下行同本行是一个内容) perfectMa ...
- python 中feedParser
转载于https://www.cnblogs.com/bbn0111/p/7056366.html.学习使用 参考链接:http://blog.csdn.net/lanchunhui/article/ ...
- SpringBoot里的一些注解
Spring不仅可以通过xml配置获取*.properties,还可以通过@Value注解的方式来获取,将properties配置文件中的属性值注入到java成员变量. 如果不想每次都写private ...