812B(状态压缩)

Dijkstra 与 TSP 算法对比:在 16 节点校园导航系统中的性能与适用性分析 本文对比了Dijkstra与TSP算法在16节点校园导航系统中的性能与适用性。通过时间复杂度实测和场景化分析,揭示了Dijkstra在单点路径规划中的高效性,以及TSP在多点遍历优化中的局限性,为校园导航系统的算法选型提供实践指导。 阅读详情

B. Sagheer, the Hausmeister
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.

The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.

Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.

Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

Input
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively.

The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.

The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Output
Print a single integer — the minimum total time needed to turn off all the lights.

Examples
input
2 2
0010
0100
output
5
input
3 4
001000
000010
000010
output
12
input
4 3
01110
01110
01110
01110
output
18
Note
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.

In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.

In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.

解题思路:每次关完当前楼的所有灯之后,有两个选择,从左边上楼或者从右边上楼,N最大15,我们们可以枚举在每一楼向左或者向右,一共就2^15次方种选择,然后取最优的那个就行

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200;
int n, m;
char Matrix[maxn][maxn];
int l[maxn],lc[maxn];
int r[maxn],rc[maxn];
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", Matrix[i] + 1);
            lc[i] = 1;
            for(int j = 2; j <= m + 1; j++)
            {
                if(Matrix[i][j] == '1') lc[i] = j;
            }
            l[i] = lc[i] - 1;
            rc[i] = m + 2;
            for(int j = m + 1; j >= 2; j--)
            {
                if(Matrix[i][j] == '1') rc[i] = j;
            }
            r[i] = m + 2 - rc[i];
        }
        int flag = -1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m + 2; j++)
            {
                if(Matrix[i][j] == '1')
                {
                    flag = i;
                    break;
                }
            }
            if(flag != -1) break;
        }
        if(flag == -1)
        {
            printf("0\n");
            continue;
        }
        int ans = 0;
        int loc = 1;
        for(int i = 2; i <= m + 1; i++)
        {
            if(Matrix[n][i] == '1') loc = i;
        }
        ans += loc - 1;
        int d = n - 1 - flag + 1;
        if(d == 0)
        {
            printf("%d\n", ans);
            continue;
        }
        int Min = 1e8;
        int x = 0;
        for(int i = 0; i < d; i++)
        {
            x += (1<<i);
        }
        for(int i = 0; i <= x; i++)
        {
            int term = i;
            int res = loc;
            int sum = 0;
            for(int j = n - 1; j >= flag; j--)
            {
                int num = term&1;
                term >>= 1;
                if(num == 1)//左
                {
                    sum += res - 1;
                    sum++;
                    sum += l[j];
                    res = lc[j];
                }
                else
                {
                    sum += m + 2 - res;
                    sum++;
                    sum += r[j];
                    res = rc[j];
                }
            }
            Min = min(Min, sum);
        }
        printf("%d\n", ans + Min);
    }
    return 0;
}
自适应粒子群优化(Adaptive Particle Swarm Optimization,APSO 下面是一个关于自适应粒子群优化(Adaptive Particle Swarm Optimization,APSO)的博客,希望可以帮助您。提示:以下是本篇文章正文内容,下面案例可供参考在上面的代码中,表示粒子的数量,表示解空间的维度,表示迭代次数,表示优化的目标函数。在__init__方法中,我们初始化了粒子群的位置和速度,并将每个粒子的位置作为其最优解,将其适应度值初始化为正无穷。我们还初始化了学习因子和惯性权重的参数。在run方法中,我们迭代了次,计算每个粒子的适应度值和最优解。 阅读详情

相关推荐

基于STM32与FPGA Cyclone IV的FSMC高速数据通信实战解析

本文详细解析了基于STM32的FSMC接口与FPGA Cyclone IV实现高速并行数据通信的实战方案。通过将FPGA模拟为SRAM设备,构建了MCU与FPGA之间的高速数据通道,并深入探讨了硬件连接、STM32软件配置、FPGA逻辑设计及性能优化等关键步骤,为图像处理、高速采集等需要大数据量交换的嵌入式应用提供了稳定高效的通信解决方案。

swift5iosmith的博客 299

codefoces 812B——Sagheer, the Hausmeister(动态规划)

B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Some people leave the lights at

say_c_box的博客 766

实用软件工程(第2版)吕云翔编简答题部分答案(自用整理)

整理自用

m0_61217768的博客 4131

CodeForces 812B-Sagheer, the Hausmeister

CodeForces 812B-Sagheer, the Hausmeister 题目链接:B. Sagheer, the Hausmeister 思路: 题目大意是n层楼,每层楼有m个房间,该楼有两部楼梯,位于房间的两侧,从当前楼梯到相邻层楼梯口,当前房间到相邻房间或相邻楼梯口都需要一分钟,问起点位于底层左楼梯口,最少多长时间才能把灯全部关掉,不用回到起点。 从最底层...

[SZU_Crayon] 404

保姆级教程:用YOLOv8/9/10搞定仪表盘指针识别,附完整数据集和Python环境配置

本文提供了一份基于YOLOv8/v9/v10的仪表盘指针检测全流程指南,涵盖环境配置、数据集构建、模型选型、训练调优和部署落地等关键步骤。通过详细的代码示例和实战经验,帮助开发者高效解决工业视觉中的仪表盘检测难题,特别适合电力巡检、智能监控等应用场景。

weixin_29031057的博客 1346

手把手教你学Simulink——基于核能发电系统的场景实例:核电站汽轮机与发电机协调控制仿真

在核电机组中,汽轮机-发电机系统是将核能最终转化为电能的关键环节。与火电机组不同,核反应堆的热惯性大、功率调节慢,因此汽轮机的控制策略需与反应堆特性相匹配,实现安全、稳定、高效的能量转换。维持发电机输出功率/频率稳定(响应电网调度)调节汽轮机进汽压力(匹配一回路蒸汽供应)实现“堆-机协调控制”保障机组在启停、变负荷、甩负荷等工况下的安全性本教程将带你使用构建一个核电站汽轮机与发电机协调控制系统汽轮机调速系统(调门控制)建模发电机与电网接口建模(同步机)

MHD0815的博客 219

codeforce刷题 1

1.Sequence Pair Weight 题目大意 给定一个长度为 n 的数组 a,规定 i < j 且 ai = aj 为一个数对,求数组 a 的 所有连续子序列中有多少个这样的数对,输出总数。 解题思路 易得如果一个一个子序列去遍历的话,那么大约有 n*n/2 个子序列,会超时所以要考虑更简单的方法,采用动态规划的思想,dp[i] 代表以第 i 个数为结尾的所有子序列的答案总数,同时用 map 储存每一个数字出现的次数 #include<iostream> #include<

jojo_it_journey的博客 746

低照度与极端天气环境中的YOLOv8优化:CPA-Enhancer网络的应用与分析

为了克服上述问题,本文提出了一种名为的网络改进方案。CPA-Enhancer通过引入链式思考机制和改进的注意力机制,有效地提升了YOLOv8在低照度和复杂天气下的表现。本文提出了针对低照度和恶劣天气条件下目标检测的YOLOv8改进方案——CPA-Enhancer链式思考网络。通过引入低照度图像增强和多尺度特征融合技术,CPA-Enhancer显著提高了YOLOv8在低光照、雾霾、雨雪等环境下的检测性能。实验结果表明,CPA-Enhancer相较于原始YOLOv8,在多个指标上都有显著的提升,尤其是在mAP。

博客 1574

Codeforces 812B-Sagheer, the Hausmeister

Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Some people leave the lights at the

wang_128的博客 1146

大模型系列——Ollama-OCR 简明教程

Llama 3.2-Vision 是一个多模态大型语言模型,有 11B 和 90B 两种大小,能够处理文本和图像输入以生成文本输出。该模型在视觉识别、图像推理、图像描述和回答与图像相关的问题方面表现出色,在多个行业基准测试中优于现有的开源和闭源多模态模型。在本文中,我将介绍如何调用由 Ollama 运行的 Llama 3.2-Vision 11B 建模服务并使用实现图像文本识别 (OCR) 功能。

2401_84052244的博客 1万+

Java SSH框架学习

Java SSH框架学习

私信可回答各种问题~ 1798

Sagheer, the Hausmeister CodeForces - 812B dp

题目链接 思路: 只说一下这个题dp的思想吧,我们对于每一行都记录最左端未关灯的位置,和最右端未关灯的位置,那么对于左端和右端的楼梯维护一下是将本层楼灯全部关闭原路返回花的时间少,还是从右端过来时间少,对于右端也一样,然后维护一个最优解决就可以了;    坑点:在dp的时候左端被更新了会影响下面所以要记录一下初始的值.特判只有最后一行未关灯的情况 #include #inc

Marcus-Bao的个人主页 921

【重识云原生】第四章云网络4.7.5节vDPA方案——virtio的半硬件虚拟化实现

vDPA(vhost Data Path Acceleration)即是让virtio数据平面不需主机干预的解决方案。该框架由Redhat提出,实现了virtio数据平面的硬件卸载。控制平面仍然采用原来的控制平面协议,当控制信息被传递到硬件中,硬件完成数据平面的配置之后,数据通信过程由硬件设备(智能网卡)完成,虚拟机与网卡之间直通。中断信息也由网卡直接发送至虚拟机不需要主机的干预。......

junbaozi的专栏 2192

poj 2411 Mondriaan's Dream(状态压缩DP)

Mondriaan's Dream \(solution:\) 这道题 \(wch\) 首先想到的是搜索和状态压缩,因为这道题的方块可以竖着也就是产生一个凸起(这里可以状态压缩),我们可以枚举它i行j列上下端凸起的情况产生的方案数,然后进行合并。但是这种方法需要维护的东西太多了,复杂度也很难预测。所以 $wch $ 想着忽略上端凸起,直接从第一行开始向下递推,然后只需要用状态研所记录下端竖着...

weixin_30546933的博客 89

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

cf812 C 二分

C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output On his trip to Luxor and Aswan, S...

all00747的博客 199

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

基本算法——位运算

快速幂算法通过二进制拆分和倍增思想,将幂运算的时间复杂度从O(n)优化到O(logn),特别适用于大数幂或模幂运算。其核心在于利用指数的二进制表示,逐步计算幂次并减少乘法次数。动态规划则通过分解问题为子问题,并存储子问题的解以优化计算,适用于具有重叠子问题和最优子结构的问题。状态压缩则通过位运算等技术减少动态规划的空间复杂度。贪心算法通过每一步选择局部最优解,不进行回退或全组合尝试,适用于求取最优解的问题。这些算法在计算效率和资源优化方面具有重要意义,广泛应用于加密、路径优化和资源分配等领域。

2301_80065652的博客 1544

codeforces round 417 div2 补题 CF 812 A-E

ASagheer and Crossroads 水题略过(然而被Hack了 以后要更加谨慎) #include<bits/stdc++.h> using namespace std; int main() { //freopen("t.txt","r",stdin); int a[4][4]; //memset(a,0,sizeof(a)...

weixin_30525825的博客 163

poj 1185 炮兵阵地(状压DP)

炮兵阵地 \(solution:\) 这一道题限制条件有点多,我们需要逐个击破: 首先要判断这一格是否为山地,这个可以状态压缩 然后我们的左右一定距离不能有炮兵,这是一个突破口,因为我们看数据发现每行不超过十个格子!这样的话我们完全可以预处理出来每一行的填充方案而且这些方案肯定很少! 某一格的前面两格也不能有炮兵。这个限制最麻烦,他涉及了前两行,我们如果要从第一行开始向下推,那就必须知道...

weixin_30896763的博客 92

UVALive 5794 (UVA 12361) File Retrieval 后缀数组 + 分治dfs

题目大意: 就是现在给出F(1 大致思路: 首先不难想到将F个串中间用未出现的不同字符隔开连接起来, 然后处理出后缀数组, 然后我们可以用后缀树的样子来看这些后缀之间的关系(后缀树组作为工具), 不能发现如果某一段连续的极大区间[L, R]中的height值都 >= h(也就是这个区间旁边的 < h),那么我们对于后缀sa[L - 1], sa[L], .... sa[R]的前 <

Gatevin的专栏 998

2019牛客暑期多校训练营 第五场

应该是最简单的一场了。 题目链接:https://ac.nowcoder.com/acm/contest/885#question A: 显然输出n个n串在一起是答案。 1 /* basic header */ 2 #include <bits/stdc++.h> 3 /* define */ 4 #define ll long long 5 #d...

weixin_30497527的博客 74

状压dp-A Simple Task

A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Given a simple graph, output the number of...

weixin_30468137的博客 81

网络流24题12. 软件补丁问题

spfa,最短路,位运算

Circus 1027

【NOI2007】 生成树计数

题解 解法1:行列式 1 (* 2 Problem: NOI2007 生成树计数 3 Author : Chen Yang 4 Time : 2012.5.30 8:00 am 5 State : 40分 6 Memo : 行列式求生成树个数 7 *)...

186

笔记(动态规划(引入)1)

关于动态规划LIS模型的笔记

HaPpYhApPYher的博客 1059

网络流24题之T12 软件补丁问题

网络流24题之T12 软件补丁问题

物是人非因果循环的博客 439

[HAOI2008]移动玩具(状压&带权二分图)

题目描述 • 一个 4 × 4 的 0/1 矩阵 • 每次可以交换相邻两个元素 • 求从初始状态到目标状态的最小交换次数 输入格式 前四行,每行一个长为 4 的 0/1 字符串,描述初始状态。 后四行,每行一个长为 4 的 0/1 字符串,描述目标状态。 输出格式 一行一个数,表示最小交换次数。 样例输入 1111 0000 1110 0010 1010 0101 1010 0101 样例输出 4...

weixin_30600503的博客 81

HDU 4405 Aeroplane chess

Aeroplane chess 今年的网络赛题目。那个时候还没写过期望概率DP的题目。之前苏牛说让我做一下期望概率DP的专题,但是没做,结果就出了一道期望DP的题目。后悔啊。。。 最近学了一下期望DP,然后做到了这一题。现在发现这题真的是很水。方程很容易写出来。dp[i] = sum(dp[i+j]) + 1 , 1= /* author : csuchenan PRO

BootStar 520
上一篇: HDU6026(最短路)
下一篇: 812C(二分)
creatorx
博客等级 码龄10年 127粉丝 145原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值