p1250 trees greedy

    

Title Description

Side of the street there are several houses. Because environmental reasons residents want to plant some trees on the roadside. Roadside area divided into blocks, and numbers into 1..N. Each part is a unit size and a maximum of a tree species. Each resident would like to plant some trees in front of and designated three numbers B, E, T. This means that the number of three residents want a minimum tree species between B and T E. Of course, B≤E, residents must remember not grow more than the number of trees in the specified area of ​​the block area, so T≤E-B + l. Residents who want to plant trees in their respective regions can cross. Your task is to find the minimum number of trees to meet all requirements.

Write a program to do the following:

Input and output formats

Input formats:

 

The first row contains the number of data N, region (0 <N≤30000);

The second line contains H, the number (0 <H≤5000) of the house;

The following lines describe residents H requires: BET, 0 <B≤E≤30000, T≤E-B + 1.

 

Output formats:

 

Only one line of output file write the number of trees

 

Sample input and output

Input Sample # 1:  Copy
9
4
1 4 2
4 6 2
8 9 2
3 5 2
Output Sample # 1:  Copy
5 

to the right end for the keyword ranking and then if there is overlap interval must be at the right end
so try to arrange the tree on the far right
#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
const int N=3e5;

struct node
{
    int x,y,num;
}s[N];
int vis[N],n,m,ans;

bool cmp(node a,node b)
{
    return a.y<b.y;
}
int main()
{
    RII(n,m);
    rep(i,1,m)
    RIII(s[i].x,s[i].y,s[i].num);
    sort(s+1,s+1+m,cmp);

    rep(i,1,m)
    {
        int cnt=0;
        rep(j,s[i].x,s[i].y)
        if(vis[j])cnt++;

        int temp=s[i].num-cnt;
        if(temp<=0)continue;
        repp(j,s[i].y,s[i].x)
        if(!vis[j])
        {
            vis[j]=1;
            ans++;
            if(--temp==0)break;
        }
    }
    cout<<ans;

    return 0;
}
View Code

 







Guess you like

Origin www.cnblogs.com/bxd123/p/10984770.html