【NOIP模拟赛】密码锁
题目描述
输入
输出
样例输入
样例输出
提示
首先最终状态是1 1 1 0 1 1 1 1 1 0 0
差分后为 1 0 0 1 1 0 0 0 0 1 0
这个差分结果可以换成括号序列的思想来理解
四个1分别出现在1 4 5 10四个位置,这是一个半闭半开区间,也就是说[1,4) [5,10)这两个区间内的数都必须是1
怎么办。
我们先处理出每一段区间全部变成1所需要的最少操作数
初始时所有的位置都是0,所以我们的任务是让[1,4) [5,10)这两个区间内的数变成1,而且操作数最少
所以考虑所有让这两个内的数变成1的情况,算出每种情况的操作数,然后取最小
把[1,4) [5,10)这两个区间内的数变成1就是把这两个区间内的元素取反
我们发现,取反这两个区间和取反[1,6) [4,10)这两个区间是等价的,所以这些数可以随机两两组合来进行变换,每次变换的加起来就是这种方案的操作数
k辣么小很明显用到状态压缩
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define inf 1000000000
#define N 10005
#define M 2000005
#define T 45
queue<int>q;
int n,k,m,num[N],x[N],sz[N],a[N],cnt,dis[N],d[][],f[M];
bool vis[N],mark[M];
void bfs(int x){
while(!q.empty())q.pop();
memset(vis,,sizeof(vis));
q.push(x);
vis[x]=;dis[x]=;
while(!q.empty()){
int now=q.front();q.pop();
for(int i=;i<=m;i++){
if(now+sz[i]<=n&&(!vis[now+sz[i]])){
vis[now+sz[i]]=;
dis[now+sz[i]]=dis[now]+;
q.push(now+sz[i]);
}
if(now-sz[i]>&&(!vis[now-sz[i]])){
vis[now-sz[i]]=;
dis[now-sz[i]]=dis[now]+;
q.push(now-sz[i]);
}
}
}
for(int i=;i<=n;i++)
if(num[i]){
if(!vis[i])d[num[x]][num[i]]=inf;
else d[num[x]][num[i]]=dis[i];
}
}
int dp(int x){
if(!x)return ;
if(mark[x])return f[x];
mark[x]=;f[x]=inf;
int st=;
for(int i=;i<=cnt;i++){
if(x&(<<(i-))){
if(!st)st=i;
else{
if(d[st][i]!=inf)
f[x]=min(f[x],dp(x^(<<(st-))^(<<(i-)))+d[st][i]);
}
}
}
return f[x];
}
int main(){
freopen("password.in","r",stdin);
freopen("password.out","w",stdout);
//freopen("Cola.txt","r",stdin);
scanf("%d%d%d",&n,&k,&m);
for(int i=;i<=k;i++){scanf("%d",&x[i]);a[x[i]]=;}
for(int i=;i<=m;i++)scanf("%d",&sz[i]);
for(int i=n+;i;i--)a[i]^=a[i-];
n++;
for(int i=;i<=n;i++){if(a[i])cnt++,num[i]=cnt;}
for(int i=;i<=n;i++)if(a[i])bfs(i);
dp((<<cnt)-);
if(f[(<<cnt)-]==inf)printf("-1");
else printf("%d",f[(<<cnt)-]);
return ;
}
【NOIP模拟赛】密码锁的更多相关文章
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
- CH Round #49 - Streaming #4 (NOIP模拟赛Day2)
A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...
随机推荐
- GstAppSink简介
Description Appsink is a sink plugin that supports many different methods for making the application ...
- angularJs-未加载完成的页面显示混乱ng-bind
ng-bind 未初始化完成不加载数据,避免产生{{}}导致页面混乱 还是上边的例子,当前页面没有加载完成的时候,页面显示是不完整的会频繁的出现{{}}这样子的表达式语句,给用户的感觉很不和谐,所以使 ...
- history显示历史操作记录,并显示操作时间
在查看历史的操作记录有两种方式1.在用户的目录下的.bash_history文件中[root@node1 ~]# vi ~/.bash_history rebootvi /etc/sysconfig/ ...
- Win8+VS2012 配置OpenGL SuperBible5 环境
(1)glew: 版本:1.7.0-win32 下载地址:https://sourceforge.net/projects/glew/files/glew/ 安装步骤: 将include文件夹下的.h ...
- Algorithm: dynamic programming
1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...
- css三角形实现的几种方法的区别
演变: .triangle{ height: 30px; width: 30px; display: inline-block; border: 30px solid; border-color: # ...
- GCC的-wl,-rpath=参数
使用GCC编译动态链接库的项目时,在其他目录下执行很可以出现找不到动态链接库的问题. 这种情况多发生在动态链接库是自己开发的情况下,原因就是程序运行时找不到去何处加载动态链接库. 可能会说在编译时指定 ...
- RStudio安装package时出现错误
cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/src/contrib/PACKAGES' 提示是打不开链接,你切换为国内的源试试Rstudio ...
- 延时加载 lazyload使用技巧
html <img class="lazy" src="images/src_unit.png" data-src="images/index/ ...
- 淘宝双十一页面(Flexible)demo
下面的代码是看了大漠 使用Flexible实现手淘H5页面的终端适配 做的一个demo. <!DOCTYPE html> <html lang="en" ng-a ...