zoukankan      html  css  js  c++  java
  • [USACO09FEB]庙会班车Fair Shuttle

    题目描述

    Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his cows are not in such good shape; a full day of walking around the fair leaves them exhausted. To help them enjoy the fair, FJ has arranged for a shuttle truck to take the cows from place to place in the fairgrounds.

    FJ couldn't afford a really great shuttle, so the shuttle he rented traverses its route only once (!) and makes N (1 <= N <= 20,000) stops (conveniently numbered 1..N) along its path. A total of K (1 <= K <= 50,000) groups of cows conveniently numbered 1..K wish to use the shuttle, each of the M_i (1 <= M_i <= N) cows in group i wanting to ride from one stop S_i (1 <= S_i < E_i) to another stop E_i (S_i < E_i <= N) farther along the route.

    The shuttle might not be able to pick up an entire group of cows (since it has limited capacity) but can pick up partial groups as appropriate.

    Given the capacity C (1 <= C <= 100) of the shuttle truck and the descriptions of the groups of cows that want to visit various sites at the fair, determine the maximum number of cows that can ride the shuttle during the fair.

    逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的。所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛们在集市上以车代步。但是,约翰木有钱,他租来的班车只能在集市上沿直线跑一次,而且只能停靠N(1 ≤N≤20000)个地点(所有地点都以1到N之间的一个数字来表示)。现在奶牛们分成K(1≤K≤50000)个小组,第i 组有Mi(1 ≤Mi≤N)头奶牛,他们希望从Si跑到Ti(1 ≤Si<Ti≤N)。

    由于班车容量有限,可能载不下所有想乘车的奶牛们,此时也允许小里的一部分奶牛分开乘坐班车。约翰经过调查得知班车的容量是C(1≤C≤100),请你帮助约翰计划一个尽可能满足更多奶牛愿望的方案。

    输入输出格式

    输入格式:
    【输入】

    第一行:包括三个整数:K,N和C,彼此用空格隔开。

    第二行到K+1行:在第i+1行,将会告诉你第i组奶牛的信息:Si,Ei和Mi,彼

    此用空格隔开。

    输出格式:
    【输出】

    第一行:可以坐班车的奶牛的最大头数。

    输入输出样例

    输入样例#1:
    8 15 3
    1 5 2
    13 14 1
    5 8 3
    8 14 2
    14 15 1
    输出样例#1:
    10

    解题报告:

    正解:贪心

    比较好想吧,但我也走了次弯路,首先原则是优先选结束位置最小的,能选就选,然后就是怎么判断是否可以选,那么可以选的条件是这个组的[L,R]之间的最大值加上这个组的人不会超过最大容量C,所以维护一个区间最大值就好,如果塞不进,能塞多少是多少,本蒟蒻用的是线段树,然后每一次选完后就在线段树中[L,R-1] 加上这个组的人数即可

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define ls (node<<1)
    #define rs (node<<1|1)
    using namespace std;
    const int N=50005;
    int n,m,c;
    struct node{
    	int l,r,val;
    	bool operator <(const node &pp)const{
    		if(pp.r!=r)return r<pp.r;
    		return l>pp.l;
    	}
    }a[N];
    int Tree[N<<2],mark[N<<2];
    void pushdown(int node){
    	if(!mark[node])return ;
    	int k=mark[node];
    	Tree[ls]+=k;Tree[rs]+=k;
    	mark[ls]+=k;mark[rs]+=k;
    	mark[node]=0;
    }
    void updata(int l,int r,int node,int sa,int se,int to){
    	if(l>se || r<sa)return ;
    	if(sa<=l && r<=se){
    		mark[node]+=to;Tree[node]+=to;
    		return ;
    	}
    	pushdown(node);
    	int mid=(l+r)>>1;
    	updata(l,mid,ls,sa,se,to);updata(mid+1,r,rs,sa,se,to);
    	Tree[node]=Max(Tree[ls],Tree[rs]);
    }
    int query(int l,int r,int node,int sa,int se){
    	if(l>se || r<sa)return 0;
    	if(sa<=l && r<=se)return Tree[node];
    	int mid=(l+r)>>1;
    	pushdown(node);
    	int q1=query(l,mid,ls,sa,se),q2=query(mid+1,r,rs,sa,se);
    	Tree[node]=Max(Tree[ls],Tree[rs]);
    	return Max(q1,q2);
    }
    void work()
    {
    	scanf("%d%d%d",&m,&n,&c);
    	for(int i=1;i<=m;i++){
    		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].val);
    	}
    	int ans=0,tmp,t;
    	sort(a+1,a+m+1);
    	for(int i=1;i<=m;i++){
    		tmp=query(1,n,1,a[i].l,a[i].r);
    		if(tmp>=c)continue;
    		if(tmp+a[i].val<=c)t=a[i].val;
    		else t=c-tmp;
    		ans+=t;
    		updata(1,n,1,a[i].l,a[i].r-1,t);
    	}
    	printf("%d
    ",ans);
    }
    
    int main()
    {
    	freopen("pp.in","r",stdin);
    	freopen("pp.out","w",stdout);
    	work();
    	return 0;
    }
    
  • 相关阅读:
    高斯消元算法
    Fermat小定理的证明
    Pollard Rho 算法简介
    做一些无聊的题
    永远不要相信自己的傲慢
    笔记-数据结构进阶
    笔记-区间问题
    线段树板子
    [DarkBZOJ3694] 最短路
    [CF321D] Ciel and Flipboard
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7435869.html
Copyright © 2011-2022 走看看