2019.12.4 trees

Plant trees

Title Description

A street classified as \ (n \) road sections, which \ (n \) road sections are numbered \ (1 \ the n-DOTS \) . Each segment can grow up to a tree. Residents now given \ (H \) Group recommended, recommendation each comprising three integers \ (B, E, T \) , represents residents want to link \ (B \) to \ (E \) between at least to seed \ (t \) tree. These recommendations may cross section to section. I ask you: If you want to meet recommended that all residents, at least how many trees to plant.

Input Format

The first line \ (the n-\) , represents the number of sections.

The second line \ (H \) , represents the number of proposals.

Here \ (H \) row describes a recommendation: \ (B, E, T \) , separated by a space.

Output Format

Output is only a number, in order to meet recommended that all residents, the minimum number of trees needs.

Sample input and output

Sample input
9
4
1 4 2
4 6 2
8 9 2
3 5 2
Sample Output
5

Data range and tips

\ (30 \% \) data satisfies \ (0 <n-\ Le 1000 \) , \ (0 <H \ 500 Le \) ;

\ (100 \% \) data satisfies \ (0 <n-\ Le. 3 \ Times 10 ^. 4 \) , \ (H \ Le 5000 \) , \ (0 <B \ Le E \ Le. 3 \ Times 10 ^ . 4 \) , \ (T \ Le + E-B. 1 \) .

The contribution of each tree as much as possible to answer in as much as possible, so we follow the right end of the sort, and then as far as possible to the right species. Every time this interval scans which have been planted with trees, the rest can be filled from right to left.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#define int long long
#define rep(i,a,n) for(register int i=a;i<=n;++i)
#define dwn(i,n,a) for(register int i=n;i>=a;--i)
using namespace std;
int h,n,book[100050];
struct node
{
    int b,e,t;
}a[100050];
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    if(x==0)return;
    write(x/10);
    putchar(x%10+'0');
}
bool cmp(node a,node b)
{
    if(a.e==b.e)return a.b<b.b;
    return a.e<b.e;
}
signed main()
{
    n=read(),h=read();
    rep(i,1,h)a[i].b=read(),a[i].e=read(),a[i].t=read();
    sort(a+1,a+h+1,cmp);
    rep(i,1,h)
    {
        int cnt=0;
        rep(j,a[i].b,a[i].e)cnt+=book[j];
        if(cnt>=a[i].t)continue;
        dwn(j,a[i].e,a[i].b)
        {
            if(!book[j])
            {
                book[j]=1;
                ++cnt;
            }
            if(cnt==a[i].t)break;
        }
    }
    int cnt=0;
    rep(i,1,n)cnt+=book[i];
    if(cnt)write(cnt);
    else putchar('0');
    return 0;
}
/*
9
4
1 4 2
4 6 2
8 9 2
3 5 2
*/

Guess you like

Origin www.cnblogs.com/qxds/p/11984411.html