题目描述

Farmer John recently opened up a new barn and is now accepting stall allocation requests from the cows since some of the stalls have a better view of the pastures.

The barn comprises N (1 <= N <= 100,000) stalls conveniently numbered 1..N; stall i has capacity C_i cows (1 <= C_i <= 100,000). Cow i may request a contiguous interval of stalls (A_i, B_i) in which to roam (1 <= A_i <= N; A_i <= B_i <= N), i.e., the cow would like to wander among all the stalls in the range A_i..B_i (and the stalls must always have the capacity for her to wander).

Given M (1 <= M <= 100,000) stall requests, determine the maximum number of them that can be satisfied without exceeding stall

capacities.

农夫约翰最近开了一个新的牲口棚屋,并且现在接受来自奶牛的分配畜栏请求因为其中的一些畜栏有更好风景。

畜栏包括N个畜栏(1 ≤ N ≤ 100,000),方便起见,我们把它们编号为1..N,畜栏i能容纳Ci只牛(1 ≤ Ci ≤ 100,000),第i只牛需要连续编号畜栏(从Ai到Bi)来漫步其中,

(1 ≤ Ai ≤ N; Ai ≤ Bi ≤ N),换言之,这只牛想要在编号范围为Ai..Bi的畜栏漫步(所有它想要畜栏必须实施为它空出位置来供它散步)

给出M个畜栏分配请求(1 ≤ M ≤ 100,000),回答最多能满足多少只牛的要求(不增加另外畜栏)

考虑以下例子:

畜栏号:    1   2   3   4   5
+---+---+---+---+---+
容纳空间: | 1 | 3 | 2 | 1 | 3 |
+---+---+---+---+---+
Cow 1 XXXXXXXXXXX (1, 3)
Cow 2 XXXXXXXXXXXXXXX (2, 5)
Cow 3 XXXXXXX (2, 3)
Cow 4 XXXXXXX (4, 5)

约翰显然不能满足所有的牛,因为畜栏3,4请求太多了

经过试验,我们发现,我们能满足牛1,3,4需要,所以这组数据答案为3

输入输出格式

输入格式:

第一行包括两个以空格隔开的正整数:N,M

第二行到第N+1行:第i+1行包括一个整数:Ci

第N+2到第N+M+1行:第i+N+1 包括两个整数Ai、Bi

输出格式:

仅一行:能满足的最大需要

输入输出样例

输入样例#1:

5 4
1
3
2
1
3
1 3
2 5
2 3
4 5
输出样例#1:

3

说明

Source: USACO 2010 March Gold

Translator: @chrome01

分析:这道题其实和借教室那道题差不多,可以考虑用线段树来维护,我们考虑一个区间是只用考虑它的最小值的,如果最小值都能满足条件,那么肯定是能够满足条件的,那么怎么样才能让题目给定的区间不重复呢?考虑贪心,我们先按照右端点从小到大排序,再按照左端点从大到小排序,这样可以保证区间之间尽量不要互相影响,最后先查询最小值,再修改就好了.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int inf = 0x7ffffff; int n,m,minn[],flag[],ans;
bool flag2 = false; struct node
{
int a,b;
}e[]; bool cmp(node x,node y)
{
if (x.b != y.b)
return x.b < y.b;
else
return x.a > y.a;
} void pushup(int o)
{
minn[o] = min(minn[o * ],minn[o * + ]);
} void pushdown(int o)
{
if (flag[o])
{
flag[o * ] += flag[o];
flag[o * + ] += flag[o];
minn[o * ] -= flag[o];
minn[o * + ] -= flag[o];
}
flag[o] = ;
} void build(int l,int r,int o)
{
if (l == r)
{
scanf("%d",&minn[o]);
return;
}
int mid = (l + r) >> ;
build(l,mid,o * );
build(mid + ,r,o * + );
pushup(o);
} void update(int l,int r,int o,int x,int y)
{
if (x <= l && r <= y)
{
flag[o]++;
minn[o]--;
return;
}
pushdown(o);
int mid = (l + r) >> ;
if (x <= mid)
update(l,mid,o * ,x,y);
if (y > mid)
update(mid + ,r,o * + ,x,y);
pushup(o);
} int query(int l,int r,int o,int x,int y)
{
if (x <= l && r <= y)
return minn[o];
pushdown(o);
int mid = (l + r) >> ,res = inf;
if (x <= mid)
res = min(query(l,mid,o * ,x,y),res);
if (y > mid)
res = min(query(mid + ,r,o * + ,x,y),res);
return res;
} int main()
{
scanf("%d%d",&n,&m);
build(,n,);
for (int i = ; i <= m; i++)
scanf("%d%d",&e[i].a,&e[i].b);
sort(e + ,e + + m,cmp);
for (int i = ; i <= m; i++)
{
if (query(,n,,e[i].a,e[i].b) <= )
continue;
update(,n,,e[i].a,e[i].b);
ans++;
}
printf("%d\n",ans); return ;
}

洛谷P1937 [USACO10MAR]仓配置Barn Allocation的更多相关文章

  1. 洛谷 1937 [USACO10MAR]仓配置Barn Allocation

    [题解] 贪心. 把区间按照右端点从小到大排序,右端点相同的按照长度从小到大排序,然后按顺序考虑,能放就放下去. 维护能不能放下去用线段树即可. #include<cstdio> #inc ...

  2. LUOGU P1937 [USACO10MAR]仓配置Barn Allocation

    传送门 解题思路 扫了一眼觉得是贪心+线段树,结果贪心的时候刚开始按区间长度排的序..这还有82分,后来叉了自己,换成按右端点排序过了. 代码 #include<iostream> #in ...

  3. AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937

    [USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; ...

  4. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  5. 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)

    P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...

  6. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  7. 洛谷P3066 [USACO12DEC]逃跑的Barn (线段树合并)

    题目描述It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to ro ...

  8. 洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]

    题目传送门 逃跑的Barn 题目描述 It's milking time at Farmer John's farm, but the cows have all run away! Farmer J ...

  9. Java实现 洛谷 P6183 [USACO10MAR]The Rock Game S(DFS)

    P6183 [USACO10MAR]The Rock Game S 输入输出样例 输入 3 输出 OOO OXO OXX OOX XOX XXX XXO XOO OOO PS: 因为每一位只有两种可能 ...

随机推荐

  1. Select 、Poll 和 Epoll

    作用 Epoll 和 Select 的作用都是为了多I/O同步复用的问题,利用Epoll.Poll或Select函数指定内核监听多个I/O的读.写.异常事件,避免每一个I/O都指定一个处理线程,导致开 ...

  2. IO多路复用(一)-- Select、Poll、Epoll

    在上一篇博文中提到了五种IO模型,关于这五种IO模型可以参考博文IO模型浅析-阻塞.非阻塞.IO复用.信号驱动.异步IO.同步IO,本篇主要介绍IO多路复用的使用和编程. IO多路复用的概念 多路复用 ...

  3. 基于C#的机器学习--机器学习的基本知识

    机器学习的基本知识 ,…用n个观测值测量.但我们不再对Y的预测感兴趣,因为我们不再有Y了,我们唯一感兴趣的是在已有的特征上发现数据模式: ​ 在前面的图中,我们可以看到这样的数据本身更适合于非线性方法 ...

  4. 17 Tips For Writing An Excellent Email Subject Line

    Out of the billions of emails that are sent every day, how can you make sure that yours stands out? ...

  5. Python爬虫入门(7):正则表达式

    下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的一种公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串” ...

  6. <力荐>非常好的正则表达式的详解<力荐>

    正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 列目录时, dir *.t ...

  7. Java中的抽象类abstract

    abstract定义抽象类 abstract定义抽象方法,只需要声明,不需要实现 包含抽象方法的类是抽象类 抽象类中可以包含抽象方法,也可以包含普通方法 抽象类不能直接创建,可以定义父类引用变量指向子 ...

  8. linux +redis 安装 +mongo 安装

    Linux 下redis安装 本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://download.redis.io/releases/redis-2.8.17.tar ...

  9. Xcode 6添加模板无效

    最近发现从Xcode 5拷贝来的模板在Xcode 6上是OK的,但是自己自定义的却不行,一直使用的是自定义的基类模板,最后发现原因是没有在 TemplateInfo.plist 中注册自定义的模板,注 ...

  10. Ubuntu中Google Chrome安装

    转载自博客 1. 方法一   1.在ubuntu中启动终端   2.在终端中,输入以下命令: sudo wget http://www.linuxidc.com/files/repo/google-c ...