描述


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. java Springmvc ajax上传

    ajax上传方式相对于普通的form上传方式要便捷,在更多的时候都会使用ajax (简单的小示例) 1.要先去下载一个 jquery.ajaxfileupload.js(基于jquery.js上的js ...

  2. ThreadPoolExecutor 线程池的实现

    ThreadPoolExecutor继承自 AbstractExecutorService.AbstractExecutorService实现了 ExecutorService 接口. 顾名思义,线程 ...

  3. dialog的传值

    A页面 等待B页面返回值的textBox:<asp:TextBox ID="TextBox1" runat="server"></asp:Te ...

  4. AJAX安全-Session做Token

    个人思路,请大神看到了指点 个人理解token是防止扫号机或者恶意注册.恶意发表灌水,有些JS写的token算法,也会被抓出来被利用,个人感觉还是用会过期的Session做token更好,服务器存储, ...

  5. [C#]判断字符串中是否包含中文

    关键代码: /// <summary> /// 判断字符串中是否包含中文 /// </summary> /// <param name="str"&g ...

  6. Linux中的文件上传下载

    1.部署ftp服务器 2.安装bypy python 客户端(还没试过,先记录一下) https://www.v2ex.com/t/124886

  7. 正则表达式的秘籍-b

    一. 正则表达式和其他方法的比较 1.我们一般将谓词和正则表达式配合使用,这是最常用的方法. - (BOOL)validateNumber:(NSString *) textString {     ...

  8. 解析Android消息处理机制:Handler/Thread/Looper & MessageQueue

    解析Android消息处理机制 ——Handler/Thread/Looper & MessageQueue Keywords: Android Message HandlerThread L ...

  9. C#变成数据导入Excel和导出Excel

    excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...

  10. spoj LCS

    初识后缀自动机: 推荐学习:http://blog.sina.com.cn/s/blog_7812e98601012dfv.html #include<cstdio> #include&l ...