CodeForces 545CWoodcutters (贪心orDP)
来源:互联网 发布:淘宝店铺招牌模板 编辑:程序博客网 时间:2024/05/15 01:16
【题目链接】:click here~~
【题目大意】:
有n棵树,给出每棵树的位置和高度,然后把树砍掉,树可以向左倒也可以向右倒。输出最多能砍几棵树。
【思路】:利用贪心的思想。第一棵树的左边和最后一棵树的右边没树,所以他们向两边倒,然后对于中间的树来说,首先先向左边倒,然后左边距离如果不够的话再向右边倒,向右倒的时候注意更新一下距离。
代码:
/* * Problem: CodeForces 545C* Running time: 46MS * Complier: G++ * Author: herongwei * Create Time: 7:59 2015/9/17 星期四*/ #include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int N=1e5+100;const int inf=0x7f7f7f7f;struct node{ int codx,heg; // coordinate and height bool ck;} arr[N];int main(){ int t,n,m; scanf("%d",&t); for(int i=1; i<=t; ++i){ scanf("%d %d",&arr[i].codx,&arr[i].heg); } arr[0].codx=-inf; // init arr[t+1].codx=inf; int s=0; for(int i=1; i<=t; ++i){ // left if(arr[i].codx-arr[i].heg>arr[i-1].codx){ ++s; continue; } if(arr[i].codx+arr[i].heg<arr[i+1].codx){ //right ++s; arr[i].codx+=arr[i].heg; } } printf("%d\n",s); return 0;}
1 0
- CodeForces 545CWoodcutters (贪心orDP)
- poj Game Prediction 1323 (贪心ordp)
- nyoj248 BUYING FEED(贪心orDP)
- poj2393 Yogurt factory 贪心orDP?
- Codeforces Round #422 (Div. 2) C Hacker, pack your bags! (二分orDP)
- poj 1283(递推ordp)
- CodeForces 545C Woodcutters (贪心)
- Codeforces 545 C Woodcutters(贪心/DP)
- CodeForces 545D贪心
- Largest Rectangle in a Histogram--(单调队列orDP)
- [贪心] Codeforces #545D. Queue
- CodeForceS#276-B(贪心)
- codeforces B. Combination(贪心)
- CodeForces 698AVacations(贪心)
- CodeForces 508C (贪心)
- Codeforces 805C (贪心)
- CodeForces 478C (贪心)
- [贪心]codeforces
- 用NetBeNetBeans
- CodeForces - 545A Toy Cars (模拟)
- SendAnywhere 跨平台传输文件(推荐)
- 详解Android中AsyncTask的使用
- 核心动画基础动画
- CodeForces 545CWoodcutters (贪心orDP)
- 【1】基于ATmega 8A -PU 的蓝牙遥控电源插座设计——【1、总体方案设计】
- Codeforces Round #320 div2 题解
- CodeForces 545BEquidistant String (模拟)
- 一元兑换问题
- 乘法表
- 级数之和
- Android基本View控件
- 小球落地