描述


http://poj.org/problem?id=3185

20个碗,要全部反转过来.反转某个碗,其相邻的碗(左右两边)也要反转.

The Water Bowls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5538   Accepted: 2172

Description

The cows have a line of 20 water bowls from which they drink. The bowls can be either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which holds no water). They want all 20 water bowls to be right-side-up and thus use their wide snouts to flip bowls.

Their snouts, though, are so wide that they flip not only one bowl
but also the bowls on either side of that bowl (a total of three or --
in the case of either end bowl -- two bowls).

Given the initial state of the bowls (1=undrinkable, 0=drinkable --
it even looks like a bowl), what is the minimum number of bowl flips
necessary to turn all the bowls right-side-up?

Input

Line 1: A single line with 20 space-separated integers

Output

Line
1: The minimum number of bowl flips necessary to flip all the bowls
right-side-up (i.e., to 0). For the inputs given, it will always be
possible to find some combination of flips that will manipulate the
bowls to 20 0's.

Sample Input

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

Sample Output

3

Hint

Explanation of the sample:

Flip bowls 4, 9, and 11 to make them all drinkable:

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state]

0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 4]

0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9]

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11]

Source

分析


反转(开关问题).

这一类问题的一个小变形.唯一的不同之处在于在区间边缘可以用长度为2的区间,这就让问题不能裸着做了= =.由于区间只存在转和不转,并且没有先后顺序,所以对于左边的长度为2的区间,只有转/不转两种情况,右同.所以共有2*2=4中情况,于是我们可以分这4种情况,把每一种情况中两个特殊区间的都先处理完,然后剩下的区间就是模板问题了.再来思考右边的那个长度为2的区间,对于转或不转可以在最后进行判断,故只要讨论2种情况即可.

注意点:

1.对于右边的长度为2的区间,如果要转不能忘了res++.

2.这道题n=20,k=3,做起来方便.可以思考任取的n与k.

 #include<cstdio>
#include<cstring>
#include<algorithm>
using std :: min; const int maxn=,n=,k=,INF=0x7fffffff;
bool b[maxn],f[maxn];
int ans=INF; void solve(int res)
{
bool sum=false;
memset(f,false,sizeof(f));
for(int i=;i<=n-;i++)
{
if(b[i]^sum)
{
f[i]=true;
sum^=true;
res++;
}
if(i->) sum^=f[i-];
}
bool l2=b[n-]^sum;
sum^=f[n-];
bool l1=b[n]^sum;
if(l2!=l1) return;
if(l2==true) res++;
ans=min(ans,res);
} void init()
{
for(int i=;i<=n;i++)
{
int a;
scanf("%d",&a);
b[i]=a== ? true : false;
}
solve();
b[]=!b[];
b[]=!b[];
solve();
printf("%d\n",ans);
} int main()
{
freopen("water.in","r",stdin);
freopen("water.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return ;
}

POJ_3185_The_Water_Bowls_(反转)的更多相关文章

  1. 简谈百度坐标反转至WGS84的三种思路

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 基于百度地图进行数据展示是目前项目中常见场景,但是因为百度地图 ...

  2. .Net Core MVC 网站开发(Ninesky) 2.3、项目架构调整-控制反转和依赖注入的使用

    再次调整项目架构是因为和群友dezhou的一次聊天,我原来的想法是项目尽量做简单点别搞太复杂了,仅使用了DbContext的注入,其他的也没有写接口耦合度很高.和dezhou聊过之后我仔细考虑了一下, ...

  3. ASP.NET Core中的依赖注入(1):控制反转(IoC)

    ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...

  4. 控制反转、依赖注入、Unity容器

    控制反转原则 依赖注入 Install-Package Unity:https://www.nuget.org/packages/Unity/ Github:https://github.com/un ...

  5. PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作

    PowerDesigner的操作经常忘记,所以把常用的功能记录下来备忘. 1.修改反转过来的字段 PowerDesigner从数据库反转的时候,默认不带注释,需要先进行修改. 输入如下脚本: {OWN ...

  6. 控制反转(IOC)

    对于很多大中型项目为了实现解耦都用到了控制反转. 常用的控制反转有unity,autoface,spring.Net 使用它们的目的归根结底就一个:避免了直接new一个对象. 今天抽时间将三种控制反转 ...

  7. 链表反转leetcode206

    最近准备结束自己的科研生涯,准备要开始找工作了,准备在LEETCODE刷刷题...刷的前40题全部用python刷的,各种调包速度奇快,后被师哥告知这样没意义,于是准备开始回归C++,Python用的 ...

  8. 回顾Spirng ioc 控制反转

    Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的.结合网上对Spring Ioc的理解,回顾一下自 ...

  9. python的反转(切片)

    看下面代码吧,简单来说不如直接看代码.如下: #coding=utf-8 __author__ = 'debude' a = 'python' print a[::-1] #从最后n开始,每走一位都打 ...

随机推荐

  1. 12_ServletConfig对象

    [ServletConfig对象简述] 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为Servlet配置一些初始化参数. 当Servlet配置了初始化参数后 ...

  2. OpenJudge/Poj 1226 Substrings

    1.链接地址: http://bailian.openjudge.cn/practice/1226/ http://poj.org/problem?id=1226 2.题目: 总时间限制: 1000m ...

  3. 软件测试之 LoadRunner安装\破解\汉化

    一.下载 LoadRunner下载地址:http://kuai.xunlei.com/d/QRNIUASALOIE 二. 安装 1.启动安装程序 运行setup.exe,点击“LoadRunner完整 ...

  4. 在Mac OS X中搭建STM32开发环境(1)

    本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 本文方法必须好用!绝不坑爹!看了N多英文资料才搞明白的,适用于STM32F4DISCOVERY评估板,带 ...

  5. js模拟苹果菜单

    模拟苹果菜单的js代码是从网上看到的,用来做导航菜单还是蛮好看的.这里借鉴一下. 效果描述:当鼠标移动离哪个图片最近的时候,这个图片最大,鼠标离的图片越远,则图片越小: 原理:主要用到了三角形的勾股定 ...

  6. C#关于编码、解码相关问题

    编码.解码技术是我们在程序中开发中经常使用到的,对一些敏感信息的存储,比如密码之类的,我们一般是不会直接以明文直接存储到数据库的,而是会通过各种算法,可以是现成的MD5(一种散列算法).或者是Hash ...

  7. 遍历 TextBox控件

    foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Form ...

  8. MVC文件上传-使用jQuery.FileUpload和Backload组件实现文件上传

    本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: 处理文件上传的服务端组件Backload 用于处理文件上传的服务端组件 ...

  9. 我们都忽略了Html5的力量,如果只看成一种技术就大错特错了!

    第一部分:Html5市场的力量 我们太忽略Html5的市场力量了.如果你把Html5当作一种技术,就大错特错了!忘掉你的产品,忘掉你的技术,想想移动时代的信息传播和消费场景.作为2B,我们实在是没有重 ...

  10. python 魔法方法

    I am not a creator, I just a porter. Note: Everything is object in python. 对于Python来说一切都是对象,也就是函数的参数 ...