Time limit 1000 ms
Memory limit 65536 kB
Farmer John’s cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most obliging of farmers, has no choice but hire some of the cows to clean the barn.
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
Line 1: Three space-separated integers: N, M, and E.
Lines 2…N+1: Line i+1 describes cow i’s schedule with three space-separated integers: T1, T2, and S.
Output
Line 1: a single integer that is either the minimum total salary to get the barn cleaned or else -1 if it is impossible to clean the barn.
题目分析
将奶牛按工作结束时间升序排序
dp[i]dp[i]dp[i]表示MMM~i−1i-1i−1时间段都有奶牛打扫所需最小费用
初始化dp[M]=0dp[M]=0dp[M]=0,其余为INF
O(n2)O(n^2)O(n2)的dp方程很显然 dp[Ti2+1]=min(dp[j])+Si(j∈[Ti1,Ti2])dp[T_{i2}+1]=min(dp[j])+S_i(j\in[T_{i1},T_{i2}])dp[Ti2+1]=min(dp[j])+Si(j∈[Ti1,Ti2])
但是时间复杂度肯定无法承受
于是考虑线段树保存dp数组的区间最小值优化DP
最后判断dp[E+1]dp[E+1]dp[E+1]是否为INF,若是则输出-1,否则输出其值
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
typedef long long lt;
int read()
{
int f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const lt inf=1e12;
const int maxn=100010;
int n,M,E;
struct node{int st,ed,cost;}rem[maxn];
lt mi[maxn<<2],dp[maxn];
bool cmp(node a,node b){return (a.ed==b.ed)?a.st<b.st:a.ed<b.ed;}
void build(int s,int t,int p)
{
if(s==t){ mi[p]=dp[s]; return;}
int mid=s+t>>1;
build(s,mid,p<<1); build(mid+1,t,p<<1|1);
mi[p]=min(mi[p<<1],mi[p<<1|1]);
}
void update(int u,int s,int t,int p,lt val)
{
if(s==t){ mi[p]=val; return;}
int mid=s+t>>1;
if(u<=mid) update(u,s,mid,p<<1,val);
else update(u,mid+1,t,p<<1|1,val);
mi[p]=min(mi[p<<1],mi[p<<1|1]);
}
lt qmin(int ll,int rr,int s,int t,int p)
{
if(ll<=s&&t<=rr) return mi[p];
int mid=s+t>>1;lt ans=inf;
if(ll<=mid) ans=min(ans,qmin(ll,rr,s,mid,p<<1));
if(rr>mid) ans=min(ans,qmin(ll,rr,mid+1,t,p<<1|1));
return ans;
}
int main()
{
n=read();M=read()+1;E=read()+1;
for(int i=1;i<=n;++i)
rem[i].st=read()+1,rem[i].ed=read()+1,rem[i].cost=read();
sort(rem+1,rem+1+n,cmp);
for(int i=1;i<=E+1;++i)dp[i]=inf; dp[M]=0;
build(1,E+1,1);
for(int i=1;i<=n;++i)
{
dp[rem[i].ed+1]=min(dp[rem[i].ed+1],qmin(rem[i].st,rem[i].ed,1,E+1,1)+rem[i].cost);
update(rem[i].ed+1,1,E+1,1,dp[rem[i].ed+1]);
}
if(dp[E+1]==inf) printf("-1");
else printf("%lld",dp[E+1]);
return 0;
}
本文介绍了一种使用动态规划和线段树优化的算法,解决农场清扫任务中如何以最低成本覆盖整个工作时段的问题。通过将奶牛的工作时间排序并利用线段树存储动态规划状态的最小值,该算法能在大规模数据下高效找到最优解。

406

被折叠的 条评论
为什么被折叠?



