题目链接

题意:给定长度为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的更多相关文章

  1. Luogu 3943 星空

    原题是CF79D Password 很妙的题. 首先我们发现区间操作不太好弄,我们想办法把它转化成单点操作,这样子处理的办法会多一点. 方法当然是差分了. 定义差分数组$b_i = a_i \^ a_ ...

  2. 一句话题解&&总结

    CF79D Password: 差分.两点取反,本质是匹配!最短路+状压DP 取反是套路,匹配是发现可以把操作进行目的化和阶段化,从而第二次转化问题. 且匹配不会影响别的位置答案 sequence 计 ...

  3. 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?

    p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...

  4. your password has expired.to log in you must change it

    今天应用挂了,log提示密码过期.客户端连接不上. 打开mysql,执行sql语句提示密码过期 执行set password=new password('123456'); 提示成功,但客户端仍然连接 ...

  5. MySql Access denied for user 'root'@'localhost' (using password:YES) 解决方案

    关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入"mysql -uroot -pmyadmin"后出现以下错 ...

  6. [上架] iOS "app-specific password" 上架问题

    当你的 Apple ID 改用双重认证密码时,上架 iOS App 需要去建立一个专用密码来登入 Apple ID 才能上架. 如果使用 Application Loader 上传时,得到这个讯息: ...

  7. [LeetCode] Strong Password Checker 密码强度检查器

    A password is considered strong if below conditions are all met: It has at least 6 characters and at ...

  8. 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: 问题原因: ...

  9. phpmyadmin #1045 - Access denied for user 'root'@'localhost' (using password: NO)

    phpmyadmin访问遇到1045问题 #1045 - Access denied for user 'root'@'localhost' (using password: NO) 解决办法 找到p ...

随机推荐

  1. leetcode203

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...

  2. nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}

    Deploying inside Eclipse v3.6 raises the exception. The WEB-INF/classes/ folder in the .war doesn't ...

  3. 使用ES-Hadoop 6.5.4编写MR将数据索引到ES

    目录 1. 开发环境 2. 下载地址 3. 使用示例 4. 参考文献 1. 开发环境 Elasticsearch 6.5.4 ES-Hadoop 6.5.4 Hadoop 2.0.0 2. 下载地址 ...

  4. the difference between fopen&open

    [the difference between fopen&open] fopen是C标准API,open是linux系统调用,层次上fopen基于open,在其之上.fopen有缓存,ope ...

  5. 第七章 二叉搜索树 07B2-1 插入:算法

  6. VS2015解决方案资源管理器空白,不显示内容

    解决方法: 1.先关闭vs: 2.把C:/Users/<users name>/AppData/Local/Microsoft/VisualStudio/14.0/ComponentMod ...

  7. jmeter 使用cookie管理器

    1.jmeter.properties  中 将CookieManager.save.cookies 设置为true 2.添加一个cookie管理器,什么都不用填 3.把需要用到的请求放到登录后面.后 ...

  8. Django基础学习四_数据库的增删改查

    今天主要学习两个东西 1.如何对数据库做增删改查 2.如果将数据库中的数据用html的方式返回到前台 一.对数据库中增删改查操作 1.首先需要先见表,见表的方法我们在“http://www.cnblo ...

  9. HttpClient详解

    HttpClient:是一个接口 首先需要先创建一个DefaultHttpClient的实例 HttpClient httpClient=new DefaultHttpClient(); 发送GET请 ...

  10. Function 对象 & anonymous 匿名函数

    functionName = new Function( [argname1, [... argnameN,]] body ); 例子: var say = new Function("na ...