Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4715 | Accepted: 1590 |
Description
Farmer John has N (1 <= N <= 10,000) cows who are willing to do some cleaning. Because dust falls continuously, the cows require that the farm be continuously cleaned during the workday, which runs from second number M to second number E during the day (0 <= M <= E <= 86,399). Note that the total number of seconds during which cleaning is to take place is E-M+1. During any given second M..E, at least one cow must be cleaning.
Each cow has submitted a job application indicating her willingness to work during a certain interval T1..T2 (where M <= T1 <= T2 <= E) for a certain salary of S (where 0 <= S <= 500,000). Note that a cow who indicated the interval 10..20 would work for 11 seconds, not 10. Farmer John must either accept or reject each individual application; he may NOT ask a cow to work only a fraction of the time it indicated and receive a corresponding fraction of the salary.
Find a schedule in which every second of the workday is covered by at least one cow and which minimizes the total salary that goes to the cows.
Input
Lines 2..N+1: Line i+1 describes cow i's schedule with three space-separated integers: T1, T2, and S.
Output
Sample Input
3 0 4
0 2 3
3 4 2
0 0 1
Sample Output
5
Hint
FJ has three cows, and the barn needs to be cleaned from second 0 to second 4. The first cow is willing to work during seconds 0, 1, and 2 for a total salary of 3, etc.
Farmer John can hire the first two cows.
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
const ll inf = 1e12;
const ll mod = ;
const double epx = 1e-;
const double pi = acos(-1.0);
//head-----------------------------------------------------------------
ll minn[maxn*];
void PushUp(int rt)
{
minn[rt]=min(minn[rt<<],minn[rt<<|]);
}
void update(int x,ll val,int l,int r,int rt)
{
if(l==x&&r==x)
{
minn[rt]=val;
return;
}
int mid=(l+r)>>;
if(x<=mid)
update(x,val,l,mid,rt<<);
else
update(x,val,mid+,r,rt<<|);
PushUp(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return minn[rt];
}
int mid=(l+r)>>;
ll ans=1e12;
if(L<=mid) ans=min(ans,query(L,R,l,mid,rt<<));
if(R>mid) ans=min(ans,query(L,R,mid+,r,rt<<|));
return ans;
}
struct node
{
ll l,r,val;
}qujian[maxn];
bool cmp(node a,node b)
{
return a.r<b.r;
}
int main()
{
ll n,m,e;
scanf("%lld%lld%lld",&n,&m,&e);
m+=,e+=;
for(int i=;i<n;i++)
{
scanf("%lld%lld%lld",&qujian[i].l,&qujian[i].r,&qujian[i].val);
qujian[i].l+=,qujian[i].r+=;
}
sort(qujian,qujian+n,cmp);
for(int i=;i<=1e5;i++)
update(i,inf,,1e5,);
update(m-,,,1e5,);
for(int i=;i<n;i++)
{
if(qujian[i].r>=m)
{
ll tempmin=query(qujian[i].l-,qujian[i].r,,1e5,);
if(tempmin==inf)
continue;
ll temp=tempmin+qujian[i].val;
ll before=query(qujian[i].r,qujian[i].r,,1e5,);
if(before>temp)
update(qujian[i].r,temp,,1e5,);
}
}
ll ans=query(e,1e5,,1e5,);
if(ans==inf)
printf("-1\n");
else
printf("%lld\n",ans);
}