codeforces 545C Woodcutters

题目:Woodcutters

传送门:

http://codeforces.com/contest/545/problem/C

题目简介:给n棵树的在一维数轴上的坐标,以及它们的高度。现在要你砍倒这些树,树可以向左倒也可以向右倒,砍倒的树不能重合、当然也不能覆盖其他的树原来的位置,现在求最大可以砍倒的树的数目。

分析:

(1)对于第i棵树,可以不砍,可以砍了向左倒,可以砍了向右倒。我们得到三种状态,用f[i][0]表示不砍,f[i][1]表示砍了向左倒,f[i][2]表示砍了向右倒;

  (1.1)对于第i棵不砍的情况:显然f[i][0]可以由第(i-1)棵不砍、第(i-1)棵砍了向左倒这两种情况转移而来;如果第(i-1)棵砍了向右倒不会压到第i棵,则f[i][0]可以由第(i-1)棵砍了向右倒转移而来。

  (1.2)对于第i棵砍了向左倒的情况:如果第i棵砍了不会压到第(i-1)棵,则f[i][1]可以由第(i-1)棵不砍、第(i-1)棵砍了向左倒则两种情况转移而来;如果第(i-1)棵砍了向右倒且第i棵砍了向左倒不会互相压到,则f[i][1]可以由第(i-1)棵砍了向右倒转移而来。

  (1.3)对于第i棵砍了向右倒的情况:显然f[i][2]可以由第(i-1)棵不砍、第(i-1)棵砍了向左倒这两种情况转移而来;如果第(i-1)棵砍了向右倒不会压到第i棵,则f[i][2]可以由第(i-1)棵砍了向右倒转移而来。

(2)在原题中有一句话“The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.”,可以在线做这道题;f[i]只与f[i-1]有关,可以滚动数组。

(3)事实上,这道题有贪心的解法。第一棵树向左边倒;最后一棵树向右边倒;对于中间的树来说,优先向左边倒,如果左边距离不够的话才向右边倒,并更新一下距离。

注意:

(1)本题数据范围很大,设置-INF时不要设得太大。

(2)本题数据范围很大,a+b+c<d会算数上溢,改为a+b<d-c就好了。

代码:

1)

 1 #include <cstdio>
 2 int n,x[100005],h[100005],f[100005][3];
 3 int max(int x,int y){x-=y;return y+(x&(~(x>>31)));}
 4 int main(){
 5     //freopen("in.txt","r",stdin);
 6     //freopen("out.txt","w",stdout);
 7     scanf("%d",&n);x[0]=-2e9;
 8     for(int i=1;i<=n;++i){
 9         scanf("%d%d",&x[i],&h[i]);
10         f[i][0]=max(f[i-1][0],f[i-1][1]);
11         if(x[i-1]+h[i-1]<x[i])f[i][0]=max(f[i][0],f[i-1][2]);
12         if(x[i-1]<x[i]-h[i])f[i][1]=max(f[i-1][0],f[i-1][1])+1;
13         if(x[i-1]+h[i-1]<x[i]-h[i])f[i][1]=max(f[i][1],f[i-1][2]+1);
14         f[i][2]=max(f[i-1][0],f[i-1][1])+1;
15         if(x[i-1]+h[i-1]<x[i])f[i][2]=max(f[i][2],f[i-1][2]+1);
16     }
17     printf("%d",max(max(f[n][0],f[n][1]),f[n][2]));
18     //fclose(stdin);fclose(stdout);
19     return 0;
20 }

 2)

#include <cstdio>
int max(int x,int y){x-=y;return y+(x&(~(x>>31)));}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;scanf("%d",&n);
    int x0=-2e9,x1,h0=0,h1;
    int f00=0,f01=0,f02=0,f10,f11,f12;
    for(int i=1;i<=n;++i){
        scanf("%d%d",&x1,&h1);
        f10=max(f00,f01);if(x0+h0<x1)f10=max(f10,f02);
        f11=0;if(x0<x1-h1)f11=max(f00,f01)+1;if(x0+h0<x1-h1)f11=max(f11,f02+1);
        f12=max(f00,f01);if(x0+h0<x1)f12=max(f12,f02);++f12;
        x0=x1;h0=h1;f00=f10;f01=f11;f02=f12;
    }
    printf("%d",max(max(f00,f01),f02));
    //fclose(stdin);fclose(stdout);
    return 0;
}

 3)

#include <cstdio>
int main(){
    int ans=0;
    scanf("%*d");
    for(int x1=-(1<<30),h1=0,x,h;~scanf("%d %d",&x,&h);++ans,x1=x,h1=h){
        if(x1+h1>=x){--ans;h1=0;}
        if(x-h>x1+h1)h=0;
    }
    printf("%d",ans);
}

 

转载于:https://www.cnblogs.com/hjj1871984569/p/5621984.html

### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值