bzoj1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 663  Solved: 291
[Submit][Status][Discuss]

Description

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.

    约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群有洁癖的奶牛满意,他不得不雇佣她们中的一些来清扫牛棚, 约翰的奶牛中有N(1≤N≤10000)头愿意通过清扫牛棚来挣一些零花钱.由于在某个时段中奶牛们会在牛棚里随时随地地乱扔垃圾,自然地,她们要求在这段时间里,无论什么时候至少要有一头奶牛正在打扫.需要打扫的时段从某一天的第M秒开始,到第E秒结束f0≤M≤E≤86399).注意这里的秒是指时间段而不是时间点,也就是说,每天需要打扫的总时间是E-M+I秒. 约翰已经从每头牛那里得到了她们愿意接受的工作计划:对于某一头牛,她每天都愿意在笫Ti,.T2秒的时间段内工作(M≤Ti≤马≤E),所要求的报酬是S美元(0≤S≤500000).与需打扫时段的描述一样,如果一头奶牛愿意工作的时段是每天的第10_20秒,那她总共工作的时间是11秒,而不是10秒.约翰一旦决定雇佣某一头奶牛,就必须付给她全额的工资,而不能只让她工作一段时间,然后再按这段时间在她愿意工作的总时间中所占的百分比来决定她的工资.现在请你帮约翰决定该雇佣哪些奶牛以保持牛棚的清洁,当然,在能让奶牛们满意的前提下,约翰希望使总花费尽量小.

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.

    第1行:3个正整数N,M,E,用空格隔开.
    第2到N+1行:第i+l行给出了编号为i的奶牛的工作计划,即3个用空格隔开的正整数Ti,T2,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.

    输出一个整数,表示约翰需要为牛棚清理工作支付的最少费用.如果清理工作不可能完成,
那么输出-1.

Sample Input

3 0 4 //三头牛,要打扫从0到4号stall
0 2 3 //一号牛,从0号stall打扫到2号,工资为3
3 4 2
0 0 1

INPUT DETAILS:

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.

Sample Output

5

HINT

约翰有3头牛,牛棚在第0秒到第4秒之间需要打扫.第1头牛想要在第0,1,2秒内工作,为此她要求的报酬是3美元.其余的依此类推.    约翰雇佣前两头牛清扫牛棚,可以只花5美元就完成一整天的清扫.


Source

题解:

正解是线段树?用dp水过。

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
#define N 10005
#define inf 1000000000
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
struct Node{
	int t1,t2,s;
}a[N];
bool cmp(Node a,Node b)
{   
    return (a.t1!=b.t1)?(a.t1<b.t1):((a.t2!=b.t2)?(a.t2<b.t2):(a.s<b.s));}
int n,m,e,ans,f[N];
int main()
{
    n=read();m=read();e=read();
    for(int i=1;i<=n;i++)a[i].t1=read(),a[i].t2=read(),a[i].s=read();
    sort(a+1,a+1+n,cmp);
    ans=inf;
    for(int i=1;i<=n;i++)f[i]=inf;
    for(int i=1;i<=n;i++)if(a[i].t1<=m)f[i]=a[i].s;
    for(int i=2;i<=n;i++)
     for(int j=1;j<i;j++)
     if(a[i].t1<=a[j].t2+1)
     {
     	f[i]=min(f[i],f[j]+a[i].s);
     	if(a[i].t2>=e)ans=min(ans,f[i]);
     }
     ans=(ans==inf)?-1:ans;
     cout<<ans<<endl;
     return 0;
}
</span>

好吧,来段zyf写的正解。%%%。

<span style="font-size:18px;">#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 100000000000
#define maxn 80000+2000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    return x*f;
}
struct rec{int l,r;ll w;}a[maxn];
struct seg{int l,r;ll mi,tag;}t[4*maxn];
int n,x,y;
inline bool cmp(rec a,rec b)
{
    return a.l<b.l||(a.l==b.l&&a.r<b.r);
}
inline void pushup(int k)
{
    t[k].mi=min(t[k<<1].mi,t[k<<1|1].mi);
}
inline void update(int k,ll z)
{
    t[k].mi=min(t[k].mi,z);
    t[k].tag=min(t[k].tag,z);
}
inline void pushdown(int k)
{
    if(t[k].tag==inf)return ;
    update(k<<1,t[k].tag);
    update(k<<1|1,t[k].tag);
    t[k].tag=inf;
}
inline void build(int k,int x,int y)
{
    int l=t[k].l=x,r=t[k].r=y,mid=(l+r)>>1;t[k].tag=inf;
    if(l==r){t[k].mi=l==0?0:inf;return ;}
    build(k<<1,l,mid);build(k<<1|1,mid+1,r);
    pushup(k);
}
inline ll query(int k,int x)
{
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==r)return t[k].mi;
    pushdown(k);
    if(x<=mid)return query(k<<1,x);else return query(k<<1|1,x);
}
inline void change(int k,int x,int y,ll z)
{
    int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
    if(l==x&&r==y){update(k,z);return;}
    pushdown(k);
    if(y<=mid)change(k<<1,x,y,z);
    else if(x>mid)change(k<<1|1,x,y,z);
    else change(k<<1,x,mid,z),change(k<<1|1,mid+1,y,z);
    pushup(k);
}
int main()
{
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
    n=read();x=read();y=read();
    for1(i,n)
     {
      a[i].l=read()-x+1;a[i].r=read()-x+1;a[i].w=read();
      if(a[i].l<=0)a[i].l=1;
     }
    sort(a+1,a+n+1,cmp);
    build(1,0,y-x+1);
    for1(i,n)
    {
        if(a[i].r<a[i].l)continue;
        if(a[i].r<=0)continue;
        if(a[i].l>y-x+1)continue;
        ll t=query(1,a[i].l-1);
        if(t!=inf)change(1,a[i].l,min(a[i].r,y-x+1),t+a[i].w);
    }
    //for1(i,n)cout<<a[i].l<<' '<<a[i].r<<' '<<a[i].w<<endl;
    //for1(k,8*n)cout<<t[k].l<<' '<<t[k].r<<' '<<t[k].mi<<' '<<t[k].tag<<endl;
    ll ans=query(1,y-x+1);
    printf("%lld\n",ans==inf?-1:ans);
    return 0;
}</span>



相关推荐
<p> 欢迎参加英特尔® OpenVINO™工具套件初级课程 !本课程面向零基础学员,将从AI的基本概念开始,介绍人工智能与视觉应用的相关知识,并且帮助您快速理解英特尔® OpenVINO™工具套件的基本概念以及应用场景。整个课程包含了视频的处理,深度学习的相关知识,人工智能应用的推理加速,以及英特尔® OpenVINO™工具套件的Demo演示。通过本课程的学习,将帮助您快速上手计算机视觉的基本知识和英特尔® OpenVINO™ 工具套件的相关概念。 </p> <p> 为保证您顺利收听课程参与测试获取证书,还请您于<strong>电脑端</strong>进行课程收听学习! </p> <p> 为了便于您更好的学习本次课程,推荐您免费<strong>下载英特尔® OpenVINO™工具套件</strong>,下载地址:https://t.csdnimg.cn/yOf5 </p> <p> 收听课程并完成章节测试,可获得本课程<strong>专属定制证书</strong>,还可参与<strong>福利抽奖</strong>,活动详情:https://bss.csdn.net/m/topic/intel_openvino </p> <p> 8月1日-9月30日,学习完成【初级课程】的小伙伴,可以<span style="color:#FF0000;"><strong>免费学习【中级课程】</strong></span>,中级课程免费学习优惠券将在学完初级课程后的7个工作日内发送至您的账户,您可以在:<a href="https://i.csdn.net/#/wallet/coupon">https://i.csdn.net/#/wallet/coupon</a>查询优惠券情况,请大家报名初级课程后尽快学习哦~ </p> <p> <span style="font-size:12px;">请注意:点击报名即表示您确认您已年满18周岁,并且同意CSDN基于商务需求收集并使用您的个人信息,用于注册OpenVINO™工具套件及其课程。CSDN和英特尔会为您定制最新的科学技术和行业信息,将通过邮件或者短信的形式推送给您,您也可以随时取消订阅不再从CSDN或Intel接收此类信息。 查看更多详细信息请点击CSDN“<a href="https://passport.csdn.net/service">用户服务协议</a>”,英特尔“<a href="https://www.intel.cn/content/www/cn/zh/privacy/intel-privacy-notice.html?_ga=2.83783126.1562103805.1560759984-1414337906.1552367839&elq_cid=1761146&erpm_id=7141654/privacy/us/en/">隐私声明</a>”和“<a href="https://www.intel.cn/content/www/cn/zh/legal/terms-of-use.html?_ga=2.84823001.1188745750.1560759986-1414337906.1552367839&elq_cid=1761146&erpm_id=7141654/privacy/us/en/">使用条款</a>”。</span> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页