"Daily training"woodcutters (codeforces Round 303 Div.2 C)

Source: Internet
Author: User

The problem was caught in the cards. Card for one hours, too true.

Test instructions and analysis (Codeforces 545C)

Test instructions: given \ (n\) tree, in \ (x\) position, high to \ (h\), then can be left to pour right down, and then down will occupy \ ([x-h,x]\) or \ ([x,x+h]\ ) interval, if not felled, occupies \ ([x,x]\) area.
Ask you how many trees you cut down, the condition is that after the fall of the occupied zone can not be occupied by other trees.

Analysis: Under the condition of this topic, this is a silly greedy problem. (then I read the wrong two times, how can not think of greedy strategy ....) )
Very simple strategy: can be left to the left to pour, can be right down to the right. Because the tree's fall will only affect the extra tree!! (In other words, it would be interesting to say that the falling zone doesn't have to take into account the existence of a tree) For example, consider an extreme case: the i\ tree to the n\ tree If it is perfectly covered by the left. So in this case you i-1\ tree If you can go right down so that you can fall to the right, because the fall of each tree affects only another tree, then I am a big deal i\ tree does not fall, my answer will not be worse . This determines the correctness of the greedy strategy.

Code
#include <bits/stdc++.h> #define MP make_pair#define PB push_back#define fi first#define se second#define ZERO (x) Me Mset ((x), 0, sizeof (x)) #define ALL (x) (x). Begin (), (x). End () #define REP (I, A, b) for (Reptype i = (a); I <= (b); ++i) #de      Fine per (I, a, b) for (Reptype i = (a); I >= (b); i.) #define QUICKIO Ios::sync_with_stdio (FALSE);                      Cin.tie (0);    Cout.tie (0); using namespace std;using ll=long long;using reptype=int;int main () {int n; cin>>n;    Pair<int,int> tree[100005];    Rep (I,1,n) cin>>tree[i].fi>>tree[i].se;    int cnt=0;    int npos=0,ntree=1; int cut[100005];    ZERO (cut);        Rep (i,1,n) {int l_boundary=tree[i].fi-tree[i].se, r_boundary=tree[i].fi+tree[i].se;        BOOL Lok=true; Per (j,i-1,1) {if (Tree[j].fi>=l_boundary | |             (cut[j]==2&&tree[j].fi+tree[j].se>=l_boundary))            {lok=false; break;           } if (tree[j].fi+tree[j].se<l_boundary) break;         } if (Lok) {cut[i]=1; continue;}        BOOL Rok=true;            Rep (j,i+1,n) {if (tree[j].fi>r_boundary) break;        else if (tree[j].fi<=r_boundary) {rok=false; break;}    } if (ROK) cut[i]=2;    } int ans=0;    Rep (i,1,n) if (cut[i]) ans++;    cout<<ans<<endl; return 0;}

Daily training "woodcutters (codeforces Round 303 Div.2 C)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.