POJ_3185_The_Water_Bowls_(反转)
描述
http://poj.org/problem?id=3185
20个碗,要全部反转过来.反转某个碗,其相邻的碗(左右两边)也要反转.
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5538 | Accepted: 2172 |
Description
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
Output
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
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_(反转)的更多相关文章
- 简谈百度坐标反转至WGS84的三种思路
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 基于百度地图进行数据展示是目前项目中常见场景,但是因为百度地图 ...
- .Net Core MVC 网站开发(Ninesky) 2.3、项目架构调整-控制反转和依赖注入的使用
再次调整项目架构是因为和群友dezhou的一次聊天,我原来的想法是项目尽量做简单点别搞太复杂了,仅使用了DbContext的注入,其他的也没有写接口耦合度很高.和dezhou聊过之后我仔细考虑了一下, ...
- ASP.NET Core中的依赖注入(1):控制反转(IoC)
ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...
- 控制反转、依赖注入、Unity容器
控制反转原则 依赖注入 Install-Package Unity:https://www.nuget.org/packages/Unity/ Github:https://github.com/un ...
- PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作
PowerDesigner的操作经常忘记,所以把常用的功能记录下来备忘. 1.修改反转过来的字段 PowerDesigner从数据库反转的时候,默认不带注释,需要先进行修改. 输入如下脚本: {OWN ...
- 控制反转(IOC)
对于很多大中型项目为了实现解耦都用到了控制反转. 常用的控制反转有unity,autoface,spring.Net 使用它们的目的归根结底就一个:避免了直接new一个对象. 今天抽时间将三种控制反转 ...
- 链表反转leetcode206
最近准备结束自己的科研生涯,准备要开始找工作了,准备在LEETCODE刷刷题...刷的前40题全部用python刷的,各种调包速度奇快,后被师哥告知这样没意义,于是准备开始回归C++,Python用的 ...
- 回顾Spirng ioc 控制反转
Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的.结合网上对Spring Ioc的理解,回顾一下自 ...
- python的反转(切片)
看下面代码吧,简单来说不如直接看代码.如下: #coding=utf-8 __author__ = 'debude' a = 'python' print a[::-1] #从最后n开始,每走一位都打 ...
随机推荐
- java集合_collection 中的方法 通过Arraylist来体现
import java.util.*; /* Collection定义了集合框架的共性功能.1,添加 add(e); addAll(collection); 2,删除 remove( ...
- (poj)3159 Candies
题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...
- OpenJudge/Poj 1936 All in All
1.链接地址: http://poj.org/problem?id=1936 http://bailian.openjudge.cn/practice/1936 2.题目: All in All Ti ...
- C#拓展练习之模拟键盘录入
摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 使用C#调用Windows API使程序模拟键盘输入,也可模拟按下快捷键.本例中,单击“模拟输入”按钮,可录入字符“ ...
- 2D游戏模型中动态分层的处理 及解决方案 (适用于 webgame 手游等资源控制较严格类型)
文章若非特别注明转载,皆是原创,转载请注明出处. 本文地址:http://www.cnblogs.com/bobolive/p/3537215.html 2D游戏中模型一般都有换装逻辑,特别是联网游戏 ...
- Apose 套打
给web添加一个dll引用:Apose.Words 下载链接:http://yunpan.cn/cA7v6uceM6KVw 提取码 11df 在Global.asax里面的Application_S ...
- ECMAScript 6十大特性
ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...
- mac下搭建react-native环境
1.安装Homebrew 2.安装node(最好安装4.x以上版本这样就自带了一个npm) 3.安装npm(node的包管理工具) 一般高版本的npm在安装node的时候已经具有了 4.安装react ...
- 数据结构与算法C语言实现笔记(1)--表
声明:此一系列博客为阅读<数据结构与算法分析--C语言描述>(Mark Allen Weiss)笔记,部分内容参考自网络:转载请注明出处. 1.表 表是最简单的数据结构,是形如A1.A2. ...
- STM32之定时器
一.定时器简介 1.时钟来源 2.定时器结构(以基本定时器为例) 二.基本定时器的编程方法 1.基本定时器的寄存器 2.例程 /** * @brief 定时器6的初始化,定时周期0.01s * @pa ...