CF115B Lawnmower

$ solution: $

很明显的一道贪心题,奇数行只能向左走,偶数行只能向右走,每一行的起点应该在上一行就已确定,而这一行的终点只和(这一行最后一棵草(相对于你走的方向),以及下一行第一颗草的位置)。所以我们最开始保留每一行中最左边杂草的位置及最右边杂草的位置,这样枚举行时可以简单一些:(只需要将上一行的起点与这一行终点取差即可)

特殊情况:(某一行没有草时)(后面几行都没有草)(第一行如何开始)(最后一行如何结束)

  1. 没有草:只需要将这一行最左边杂草的位置及最右边杂草的位置全部赋值成上一行终点的位置即可。
  2. 后面几行都没有草:要特殊处理(详见代码中的\(f\)数组(记录每一行有没有草))
  3. $ l[0]=1; $ 即可
  4. 最后一行如何结束:用 $ f $ 数组记录终点行即可

$ code: $

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set> #define ll long long
#define db double
#define inf 0x7fffffff
#define rg register int using namespace std; char ch;
int n,m,s=1,k,ff=1,ans;
int l[161];//
int r[161];//最左边的杂草
bool f[161];//最右边的杂草 inline int qr(){
while((ch=getchar())<'0'||ch>'9');
int res=ch^48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+(ch^48);
return res;
} inline void init(){
n=qr(),m=qr(); l[0]=1;//初始化
for(rg i=1;i<=n;++i)l[i]=160;
for(rg i=1;i<=n;++i){
for(rg j=1;j<=m;++j){
while((ch=getchar())!='G'&&ch!='W');
if(ch=='W'){l[i]=min(l[i],j);r[i]=j;f[i]=1;}
}if(!f[i])l[i]=r[i]=(i&1?l[i-1]:r[i-1]);//这一行没草,直接搬上一层的!
}l[n+1]=l[n];r[n+1]=r[n];//特殊初始化
} inline void work(){
for(rg i=n;i;--i)
if(f[i]){ff=i;break;}
for(rg i=1;i<=ff;++i,s=k){
if(i&1)ans+=(k=max(r[i],r[i+1]))-s+1;
else ans+=s-(k=min(l[i],l[i+1]))+1;
}printf("%d\n",--ans);
} int main(){
//freopen("Lawnmower.in","r",stdin);
//freopen("Lawnmower.out","w",stdout);
init(); work(); return 0;
}

CF115B Lawnmower(贪心)的更多相关文章

  1. CF115B Lawnmower

    题目描述 You have a garden consisting entirely of grass and weeds. Your garden is described by an n×mn×m ...

  2. CF 115B Lawnmower(贪心)

    题目链接: 传送门 Lawnmower time limit per test:2 second     memory limit per test:256 megabytes Description ...

  3. Lawnmower(洛谷 CF115B)

    题目看这里 题目大意 简单来讲就是从(1,1)向左或右或下走,经过所有草坪的最短路程 思路: 由于在第一行只能向右走,那么我们就知道,在单数行和双数行分别是向右走和向左走,那么我们在单数行就只需要统计 ...

  4. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  5. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

随机推荐

  1. Hibernate 查询技术

    转载: http://blog.csdn.net/u014078192/article/details/24986475 一.Hibernate的三种查询方式(掌握) Hibernate中提供了三种查 ...

  2. IDEA 快捷键修改(长期更新)

    最近误操作,导致idea的快捷键没了. 痛定思痛,打算记录一下,以前修改过的key map 搜索一下就好了: 1  代码格式化 -- reformat code:Ctrl+Alt+L(如果按了没反应, ...

  3. EF 更新 删除

    为了避免先查询后更新或删除的问题 可以使用如下语句 Entities db = new Entities(); Orders o = new Orders(); o.id = 6; o.name = ...

  4. day26 多继承

    class A(object): def test(self): print('from A') class B(A): def test(self): print('from B') class C ...

  5. 【CodeForces 624D/623B】Array GCD

    题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...

  6. 【BZOJ1822】[JSOI2010]冷冻波(二分,网络流)

    [BZOJ1822][JSOI2010]冷冻波(二分,网络流) 题面 BZOJ 洛谷 题解 先预处理每个巫妖可以打到哪些小精灵,然后二分答案,网络流判定即可. #include<iostream ...

  7. 将一个字符串中的空格替换成“%20”(C、Python)

    将一个字符串中的空格替换成“%20” C语言: /* ----------------------------------- 通过函数调用,传地址来操作字符串 1.先计算出替换后的字符串的长度 2.从 ...

  8. Java8的Stream流(一) --- 基础用法

    Java8中的Stream Stream使用一种类似用SQL语句从数据库查询数据的直观方式来提供一种对Java集合运算和表达的高阶抽象. Stream的特性及优点: 无存储. Stream不是一种数据 ...

  9. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

  10. ZOJ_3950_How Many Nines 解题报告及如何对程序进行测试修改

    The 17th Zhejiang University Programming Contest Sponsored by TuSimple Solution: #include <stdio. ...