Content

在一个仅有 \(0,1\) 这两个数的数列上,每次可以选择一段全为1的连续区间将其左移 \(1\) 或者右移 \(1\)。现给出 \(t\) 次询问,每次询问给出一个长度为 \(n\) 的满足上述条件的数列,求出使所有为 \(1\) 的位置在唯一一个连续区间的最小操作次数。

数据范围:\(1\leqslant t\leqslant 200,1\leqslant n\leqslant 50\)。

Solution

每次我们选择全为 \(1\) 的区间向右移,其实就是为了把目标区间中的 \(0\) 给它补齐,所以,我们不难看出,这题目就是让我们求每两个 \(1\) 之间的 \(0\) 的数量的总和。直接扫一遍统计一下即可。

Code

int t, n, a[57];

int main() {
//This program is written in Windows 10 by Eason_AC
getint(t);
while(t--) {
int ans = 0, tmp = 0, flag = 0;
getint(n);
_for(i, 1, n) {
getint(a[i]);
if(a[i] == 0 && flag) {
tmp++;
if(i == n) tmp = 0;
} else if(a[i] == 1) ans += tmp, tmp = 0, flag = 1;
}
writeint(ans), puts("");
}
return 0;
}

CF1433B Yet Another Bookshelf 题解的更多相关文章

  1. Codeforces Round #677 (Div. 3)【ABCDE】

    比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...

  2. POJ 3268 Bookshelf 2 动态规划法题解

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  3. POJ 3628 Bookshelf 2 题解

    本题解法非常多,由于给出的数据特殊性故此能够使用DFS和BFS,也能够使用01背包DP思想来解. 由于一般大家都使用DFS,这里使用非常少人使用的BFS.缺点是比DFS更加耗内存,只是长处是速度比DF ...

  4. poj_3628 Bookshelf 2

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  5. [BZOJ2678][Usaco2012 Open]Bookshelf

    P.S. 偶然间发现可以用 markdown... [BZOJ2678][Usaco2012 Open]Bookshelf 试题描述 When Farmer John isn't milking co ...

  6. Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP

    题目链接:http://codeforces.com/contest/294/problem/B B. Shaass and Bookshelf time limit per test 1 secon ...

  7. [USACO 2012 Open Gold] Bookshelf【优化dp】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=138 传送门2:http://www.lydsy.com/JudgeOn ...

  8. Bookshelf 2 简单DFS

    链接:https://ac.nowcoder.com/acm/contest/993/C来源:牛客网 题目描述 Farmer John recently bought another bookshel ...

  9. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

随机推荐

  1. 基于IDEA Plugin插件开发,撸一个DDD脚手架

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 最近很感兴趣结合 IDEA Plugin 开发能力,扩展各项功能.也基于此使用不同的案例,探索 ...

  2. 《HelloGitHub》第 68 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  3. vue-通过name进行数据过滤

    <template> <div> <h3>搜索列表</h3> <input type="text" placeholder=& ...

  4. ROS机器人开发实践1->SSH远程登录要点记录

    1.有线网卡 设置 找到有线网络,点击设置,修改其中的IPv4的地址和子网掩码. 1 //地址 2 192.168.xxx.xxx 3 //子网掩码 4 255.255.255.0 点击应用 2.配置 ...

  5. c++基础知识02

    1.前置与后置区别 #include<iostream> using namespace std; int main() { //前置和后置区别 //前置递增或递减 先让变量加减1 然后进 ...

  6. Codeforces 983E - NN country(贪心+倍增优化)

    Codeforces 题面传送门 & 洛谷题面传送门 一道(绝对)偏简单的 D1E,但是我怕自己过若干年(大雾)忘了自己的解法了,所以过来水篇题解( 首先考虑怎么暴力地解决这个问题,不难发现我 ...

  7. 什么是GP、LP、PE、VC、FOF?

    GP GP是General Partner的缩写,意思是普通合伙人.投资者经常听到的一些基金.风投等投资公司采用的就是普通合伙人的制度,在美国等发达国家,普通合伙人很常见. 其实,说白了,GP最开始指 ...

  8. LearnPython_week3

    函数说明 1 # -*- coding:utf-8 -*- 2 # Author:Wong Du 3 4 5 ###函数, 6 # 能避免代码重复, 7 # 方便代码修改等操作 8 def wong( ...

  9. JAVA中数组的基本概念与用法

    JAVA中数组的基本概念与用法 1. 数组的定义与特点 数组的一种引用数据类型 数组中可以同时存放多个数据,但是数据的类型必须统一 数组的长度在开始时就需要确定,在程序运行期间是不可改变的 虽然可以使 ...

  10. Can we call an undeclared function in C++?

    Calling an undeclared function is poor style in C (See this) and illegal in C++. So is passing argum ...