lightoj 1389 - Scarecrow 贪心
来源:互联网 发布:正装衬衫 知乎 编辑:程序博客网 时间:2024/05/23 21:24
给定一个1*n的图,#代表坏的地,点代表庄稼,要放置稻草人防止乌鸦,一个稻草人可以保护本身和左右两端,问最少需要多少个稻草人。
三个点为一组。遇到一个#可能的情况是前面的点数量是三的倍数,刚好不用管,余数为2,那么就必须要放置一个啦。余数为1..有可能的情况是.#.或者.##,其实不管什么情况都是要放置一个的,直接跳过下个点,不管是啥都行。
我的代码最后要处理边界,其实在最后面加个#就可以不考虑边界了...
我看到题目的第一想法是DP....应该可解。
#include<bits/stdc++.h>using namespace std;#define ll long long#define ull unsigned long long#define mod 1000007#define inf 0x3f3f3f3f#define N 100100using namespace std;char s[200];int main(){ int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++) { int n; scanf("%d",&n); scanf("%s",s); int ans=0,cnt=0; for(int i=0;i<n;i++) { if(s[i]=='.') cnt++; else { ans+=cnt/3; cnt=cnt%3; if(cnt==2) ans++; else if(cnt==1) { i++; ans++; } cnt=0; } } if(cnt) ans+=cnt/3+(cnt%3?1:0);//剩余有cnt点没考虑 printf("Case %d: %d\n",cas,ans); } return 0;}
0 0
- LightOJ - 1389 Scarecrow(贪心)
- lightoj 1389 - Scarecrow 贪心
- LightOJ 1389 - Scarecrow(贪心啊)
- LightOJ 1389 - Scarecrow【模拟】
- LightOJ - 1389 Scarecrow (水模拟)
- UVA12405 Scarecrow 贪心
- Lightoj 1106 贪心
- lightoj 1219 - Mafia 贪心
- lightoj 1198(贪心)
- lightoj 1016 水贪心
- lightoj 1034 贪心 拓扑排序
- LightOJ - 1166 Old Sorting(贪心)
- lightoj 1016 - Brush (II) 贪心
- lightoj 1198 - Karate Competition 贪心
- lightoj 1301 - Monitoring Processes 贪心
- lightoj 1016 - Brush (II) 【贪心】
- lightoj 1198 - Karate Competition 【贪心】
- LightOJ - 1198 Karate Competition 【贪心】
- ( ̄︶ ̄)↗ 涨姿势
- C++类中的static数据成员,static成员函数
- putty&TeamViewer
- 几种 angular 循环。。。。
- 搭建一个php框架
- lightoj 1389 - Scarecrow 贪心
- Spring框架 基于@Aspect的AOP配置(六)
- 复杂链表的复制
- 调用摄像头拍照
- STM32 的 I/O 口默认都是 3.3V的,如何输出5V ? 这里介绍个好用的方法!
- C++作业3
- “平台化思维”如火如荼的今天,该如何借鉴?
- 查找-插值查找
- RxJava/RxAndroid 入门初体验