DP Codeforces Round #303 (Div. 2) C. Woodcutters

题目传送门

 1 /*
 2     题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒
 3     问最多能砍到多少棵树
 4     DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树
 5     分情况讨论,若符合就取最大值更新,线性dp,自己做出来了:)
 6 */
 7 #include <cstdio>
 8 #include <algorithm>
 9 #include <cstring>
10 #include <cmath>
11 #include <iostream>
12 using namespace std;
13
14 typedef long long LL;
15
16 const int MAXN = 1e5 + 10;
17 const int INF = 0x3f3f3f3f;
18 LL x[MAXN], h[MAXN];
19 LL dp[MAXN][3];        //0-l 1-no 2-r
20
21 int main(void)        //Codeforces Round #303 (Div. 2) C. Woodcutters
22 {
23     //freopen ("C.in", "r", stdin);
24
25     int n;
26     while (scanf ("%d", &n) == 1)
27     {
28         for (int i=1; i<=n; ++i)
29         {
30             scanf ("%I64d%I64d", &x[i], &h[i]);
31         }
32         memset (dp, 0, sizeof (dp));
33         dp[1][0] = dp[1][1] = 1;
34         if (x[1] + h[1] < x[2])    dp[1][2] = 1;
35         for (int i=2; i<=n; ++i)
36         {
37             LL tmp;
38             tmp = max (dp[i-1][0], dp[i-1][1]);
39             tmp = max (tmp, dp[i-1][2]);
40             dp[i][1] = tmp;
41             if (x[i] - h[i] > x[i-1])
42             {
43                 dp[i][0] = max (dp[i-1][0], dp[i-1][1]) + 1;
44             }
45             if (x[i] - x[i-1] > h[i-1] + h[i])
46             {
47                 dp[i][0] = max (dp[i][0], dp[i-1][2] + 1);
48             }
49             if (i < n && x[i] + h[i] < x[i+1])
50             {
51                 dp[i][2] = tmp + 1;
52             }
53             if (i == n)    dp[i][2] = tmp + 1;
54         }
55
56         LL ans;
57         ans = max (dp[n][0], dp[n][1]);
58         ans = max (ans, dp[n][2]);
59         printf ("%I64d\n", ans);
60     }
61
62     return 0;
63 }
时间: 2024-08-06 03:45:17

DP Codeforces Round #303 (Div. 2) C. Woodcutters的相关文章

水题 Codeforces Round #303 (Div. 2) D. Queue

题目传送门 1 /* 2 比C还水... 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 #include <iostream> 9 using namespace std; 10 11 typedef long long ll; 12 13 const int MAXN = 1e5 + 10; 14 const i

贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

题目传送门 1 /* 2 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 3 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <iostream> 10 using namespace std; 11 12 const int MAX

水题 Codeforces Round #303 (Div. 2) A. Toy Cars

题目传送门 1 /* 2 题意:5种情况对应对应第i或j辆车翻了没 3 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 4 3: if both cars turned over during the collision. 5 是指i,j两辆车,而不是全部 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #

DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + 1 2. l[i-1] + 1 3. r[i+1] + 1 //修改a[i] */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN =

递推DP Codeforces Round #260 (Div. 1) A. Boredom

题目传送门 1 /* 2 DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 1e5 + 10; 12 const int INF

树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

题目传送门 1 /* 2 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 3 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计算dp[v], 4 此时可以从上个节点的信息递推出来 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 using name

DP Codeforces Round #260 (Div. 1) A. Boredom

题目传送门 1 /* 2 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 3 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) i * cnt[i]); 4 只和x-1,x-2有关,和顺序无关,x-1不取,x-2取那么累加相同的值,ans = dp[mx] 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring&

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)

这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x]\)或者\([x,x+h]\)区间,如果不砍伐,占据\([x,x]\)区域. 问你最多砍多少棵树,砍树的条件是倒下去后占有的区间不能被其他树占据. 分析:在这条题目的条件下,这是一个傻逼贪心题.(然后我读错两次题目,怎么也想不出来贪心策略....) 很简单的策略:能往左倒往左倒,能往右倒往右倒.因