题目描述

There are n lights aligned in a row. These lights are numbered 1 to n from left to right.
Initially some of the lights are turned on. Chiaki would like to turn off all the lights.
Chiaki starts from the p-th light. Each time she can go left or right (i.e. if Chiaki is at x, then she can go to x−1 or x+1) and then press the switch of the light in that position (i.e. if the light is turned on before, it will be turned off and vise versa).
For each p=1,2,…,n, Chiaki would like to know the minimum steps needed to turn off all the lights.

///一排中有n个灯,从左到右依次标号1到n,起初有些灯是亮着的。千明小姐想要关掉所有的灯。千明小姐从第p盏灯开始,每次她可以左右横跳一格来改变那个格子中灯的状态(关上的就打开,开着的就关上)。对于每个p=1,2,…,n,千明小姐想知道关闭所有灯所需的最小步骤。///

输入

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains an integer n (2≤n≤106) – the number of lights.
The second line contains a binary string s where si=1 means the i-th light is turned on and si=0 means i-th light is turned off.
It is guaranteed that the sum of all n does not exceed 107.

///有多个测试用例。输入的第一行是一个整数T,表示测试用例的数量。
对于每个测试用例:第一行包含整数n(2≤n≤106)——灯光数。
第二行包含一个二进制字符串s,其中si=1表示第i个灯打开,si=0表示第i个灯关闭。 题中会保证n<107。///

输出

最终的结果需要计算i*zi (i为1~字符串的长度的范围,zi为p在i的最小步骤),从1到n的和
输出和%(1e9+7)

举例:对于测试用例的111来说,p=1,则需要经过111>101>001>011>010>000 5个步骤;p=2,需经过111>011>001>000 3个步骤;p=3,需经过跟p=1一样的5个步骤,只不过倒过来。则结果为:15+23+3*5=26

样例输入

3
3
000
3
111
8
01010101

样例输出

0
26
432

你说这题该怎么办?我是真的不会。
你们自己去看大佬写的吧:运用了异或运算等知识

https://blog.csdn.net/BinGoo0o0o/article/details/81190093

https://blog.csdn.net/qq_18869763/article/details/81193622

最终代码:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
#define N 1010000
const int mod = 1e9+7;
int n,T;
bool f[N],g[N],preg[N],preg2[N];
char s[N];int ret[N],ret2[N],cnt[N],cnt2[N];
void work(bool *g,int *ret,int n)
{   for(int i = 1; i <= n; i++)         {ret[i]=1<<30;}int fo=n+1,lo=-1;    int i=1;    while(i<=n)    {   if(g[i])         {if (fo==n+1) fo=i;lo=i;}        i++;    }    if (fo>lo)     {   i=1;        while(i<=n)        {   ret[i]=0;           i++;        }         return;    }    for(i = 1; i <= n; i++)     {      preg[i]=preg[i-1]^g[i]^1;        preg2[i]=preg2[i-1]^g[i];        cnt[i]=cnt[i-1]+preg[i];        cnt2[i]=cnt2[i-1]+preg2[i];    }    for( i = 1; i <= lo; i++)     {      if (i<=fo)        {   int fw=i,lw=lo;            if (i==lo)             {   ret[i]=min(ret[i],3);                continue;            }            int ans=lw-fw;            if (preg[fw-1])ans+=2*(cnt[lw-1]-cnt[fw-1]);            else ans+=2*(lw-fw-cnt[lw-1]+cnt[fw-1]);            if (preg[lw-1]^preg[fw-1]^1) ans--;            ret[i]=min(ret[i],ans);        }         else        {      int fw=fo,lw=lo;            int ans=i-fo+lw-fw;            if (preg2[fw-1])                 ans += 2*(cnt2[i-1]-cnt2[fw-1]);            else    ans+=2*(i-fw-cnt2[i-1]+cnt2[fw-1]);            int x=preg2[i-1]^preg2[fw-1]^1;            if (x==preg[i-1])                 ans+=2*(cnt[lw-1]-cnt[i-1]);            else  ans+=2*(lw-i-cnt[lw-1]+cnt[i-1]);            if (x^preg[i-1]^preg[lw-1])   ans--;            ret[i]=min(ret[i],ans);        }    }
}
int main()
{       scanf("%d",&T);    while(T--)    {    scanf("%d",&n);        scanf("%s",s+1);        int i=1;        while(i<=n)        {       g[i]=(s[i]=='1');            i++;        }        work(g,ret,n);        reverse(g+1,g+n+1);        work(g,ret2,n);        LL ans=0;        for(i = 1; i <= n; i++)         {      ret[i]=min(ret[i],ret2[n+1-i]);            ans=(ans+(LL)i*ret[i])%mod;        }        printf("%lld\n",ans);    }    return 0;
}

数据结构作业3-4(周)问题F:Turn off the light(关下灯)相关推荐

  1. 20172310 2017-2018-2 《程序设计与数据结构》第十一周学习总结

    20172310 2017-2018-2 <程序设计与数据结构>第十一周学习总结 作业要求 1.教材第23-26章(Java和Android开发学习指南) 代码托管到git@OSC,参考一 ...

  2. 20162305《程序设计与数据结构》第1周学习总结

    学号 20162305 2016-2017-2<程序设计与数据结构>第1周学习总结 教材学习内容总结 本周,我们主要学习了Java程序设计的第一章内容,通过对教材的学习,我对Java的用途 ...

  3. 20172328 2018-2019《Java软件结构与数据结构》第八周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第八周学习总结 概述 Generalization 本周学习了二叉树的另一种有序扩展?是什么呢?你猜对了!ヾ(◍°∇°◍) ...

  4. 20172307 2018-2019-1 《程序设计与数据结构》第3周学习总结

    20172307 2018-2019-1 <程序设计与数据结构>第3周学习总结 教材学习内容总结 队列 1.队列的元素是按FIFO方式处理的. 2.队列是一种可存储重复编码密钥的便利集合. ...

  5. 学号 20172326 《程序设计与数据结构》第三周学习总结

    学号 20172326 <程序设计与数据结构>第三周学习总结 教材学习内容总结 队列是先进先出的数据结构(FIFO)与栈不同,队列的两端可分别进行操作 first与front相同,返回首段 ...

  6. 20172303 2017-2018-2 《程序设计与数据结构》第4周学习总结

    20172303 2017-2018-2 <程序设计与数据结构>第4周学习总结 教材学习内容总结 第四章: 学会了简单的编写类 了解了UML类图(真的很有用!!!!) 了解了return语 ...

  7. 王彪20162321 2016-2017-2 《程序设计与数据结构》第7周学习总结

    学号20162321 2016-2017-2 <程序设计与数据结构>第七周学习总结 教材学习内容总结 关键概念 多态引用在不同的时候可以指向不同类型的对象 多态引用在运行时才将方法调用与它 ...

  8. 20162303 2016-2017-2 《程序设计与数据结构》第五周学习总结

    20162303 2016-2017-2 <程序设计与数据结构>第五周学习总结 教材学习内容总结 类可能包含许多对象,对象有一个状态由属性来定义,对象的行为由相关的操作来定义. 每个类代表 ...

  9. 20172313 2018-2019-1 《程序设计与数据结构》第六周学习总结

    20172313 2018-2019-1 <程序设计与数据结构>第六周学习总结 教材学习内容总结 概述 树是一种非线性结构,其中的元素被组织成一个层次结构. 树由一个包含结点(node)和 ...

  10. 20172329 2017-2018-2 《程序设计与数据结构》第五周学习总结

    20172329 2017-2018-2 <程序设计与数据结构>第五周学习总结 教材学习内容总结 第五章 条件判断与循环: 一.条件语句 1.条件语句的内容:if语句.if-else语句和 ...

最新文章

  1. php怎么批量转码,网站文件批量转码_PHP教程
  2. 【JVM】jstack和dump线程分析(2)
  3. 一致性代码段和非一致性代码段
  4. spring 多数据源 总结
  5. Java dom4j解析RESTFull风格发布的WebService的xml文件
  6. redhat 6.5 mysql rpm_CentOS6.5和RedHat6.5下以rpm方式安装mysql-5.6.20
  7. 关于sqlmap无法打开的问题解决办法
  8. oracle18cscott,Oracle 18c 数据库中scott用户不存在的解决方法
  9. 3D体验平台(3DExperience)介绍---达索系统
  10. cocostudio 1.6
  11. 无传感器永磁同步电机电机自适应自抗扰ADRC控制策略
  12. python节日贺卡图片_节日贺卡图片制作手工
  13. 哪些学校不让用matlab,新一轮制裁?部分高校被禁止使用matlab,科学无国界就是一句笑话...
  14. 2012年2月4日汇报Axure RP Pro 6.5 Beta简体中文加强测试版进展
  15. python下载地址到迅雷qq旋风下载
  16. 计算机基础(16)——Office和WPS(3)——什么查看自己的WPS是哪个版本
  17. Rock Pi开发笔记(二):入手Rock Pi 4B plus(基于瑞星微RK3399)板子并制作系统运行
  18. ZWAVE Door Lock Logging Records
  19. td 内容自动换行 table表格td设置宽度后文字太多自动换行
  20. linux 恢复手机照片,Remini照片修复

热门文章

  1. java编程培训都学习哪些内容
  2. pip 安装库出错:Defaulting to user installation because normal site-packages is not writeable
  3. 划痕分析_如何保护相机和镜头不受损坏,灰尘和划痕
  4. 模拟电路设计(23)---模数和数模转换器概述
  5. Apache FOP 将Java对象转换为pdf文件
  6. 汽车暖风系统操作步骤
  7. 华为鸿蒙跑了个“hello world”!跑通后,我特么开始怀疑人生....
  8. 从键盘交互式输入-一个人的18位的身份证号,以类似于“2001年09月12日”的形式输出该人的出生日期。
  9. dlang语法的简单整理
  10. 外汇期货市场的组织结构