Codeforces Round #237 (Div. 2) E. Maze 1D
来源:互联网 发布:ios日本漫画软件 编辑:程序博客网 时间:2024/04/29 21:22
题意:每次可以向左或右走一格,遇到障碍物这步就不走,求放最少的障碍物的时候方障碍物的方法有多少种
分析:看起来好像很难的样子,又要最少障碍物,又要求多少种方法,其实在一端放了障碍物的时候后面再放就不起作用了,其次最后一步向左走的话放在左边也是不能起到作用的,就是说如果最后一步向左走,要实现题目要求,肯定要走到左边一个没有走过的端点,那么在右边放障碍物才能使它比起不放障碍物再往左边走,所以障碍物最多放1个,所以解决的时候先判断不放障碍物的时候能不能满足要求,若不满足,二分求解;又因为如果障碍物放在+x处可行的话,放在+(x-1)处必定也可行,所以方案数为(l-1)
代码:
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;string str;int n;bool solve(int x){ int i,now=0,left=0; for(i=0;i<n;i++){ if(str[i]=='L'){ now--; if(i==n-1){ if(now<left) return true; return false; } if(now<left) left=now; }else { if(now+1<x) now++; } }}int main(){ int i,l,r,m; while(cin>>str){ n=str.size(); if(str[n-1]=='R'){ for(i=0;i<n;i++){ if(str[i]=='L')str[i]='R'; else str[i]='L'; } } if(solve(n)){ printf("1\n"); continue; } l=0;r=n-1; while(l<r){ m=(l+r+1)/2; if(solve(m)) l=m; else r=m-1; } cout<<l<<endl; } return 0;}
0 0
- Codeforces Round #237 (Div. 2) E. Maze 1D
- Codeforces Round #184 (Div. 2) D、E
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #306 (Div. 2) D-E
- Codeforces Round #345 (Div. 2) D,E
- Codeforces Round #368 (Div. 2) D &E
- Codeforces Round #443 (Div. 2) D,E
- Codeforces Round #124 (Div. 2)D. Infinite Maze
- CodeForces 413E Maze 2D
- CodeForces 404E Maze 1D
- Codeforces Round #222 (Div. 1) A. Maze
- Codeforces Round #186 (Div. 2)A、B、C、D、E
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
- Codeforces Round #261 (Div. 2) A,B,C,D,E
- Codeforces Round #263 (Div. 2) A,B,C,D,E
- Codeforces Round #264 (Div. 2) A,B,C,D,E
- Codeforces Round #274 (Div. 2) 解题报告 (C D E)
- Codeforces Round #277 (Div. 2) A,B,C,D,E
- 服务器端配置MySQL
- 请求生命周期回调(Controllers)
- c++模板两个数的加法
- Java对象池
- 用户界面设计的七大法则:给你一个受人欢迎的界面设计
- Codeforces Round #237 (Div. 2) E. Maze 1D
- 软件公司研发职位
- 快速幂&&取余(NYOJ420、105、205)
- 微生物增殖
- poj 3126 Prime Path (bfs+筛素数)
- poj 1258 Agri-Net(最小生成树)
- android adt 使用图标
- ORACLE日期时间函数大全
- 《APUE》Chapter 4 Files and directories (学习笔记加上自己的代码)