BZOJ1828 [Usaco2010 Mar]balloc 农场分配
直接贪心,我们把线段按照右端点从小到大排序,然后一个个尝试插入即可。。。
来证明贪心的正确性:
不妨设贪心得到的答案集合为$S$,最优解的答案集合为$T$
若$S$不是最优解,那么$S \not= T$,不妨设按照右端点排序后,第一个不同的位置为$i$
则$S_i \not= T_i$,分情况讨论:
(1)$S_i$的左端点在$T_i$的右端点后,由于贪心的步骤这是不可能的
(2)$S_i$的右端点在$T_i$的右端点之前:
(2.1)$S_i$的右端点在$T_i$的左端点之前,即$S_i$、$T_i$不相交,我们发现存在$S' = \{S_1, S_2, ..., S_i\} \cup \{T_i, T_{i + 1}, ..., T_n\}$,且$|S'| = |T| + 1$,矛盾
(2.2)$S_i$的右端点在$T_i$的左端点之后,即$S_i$、$T_i$相交,我们直接令$S' = T - \{T_i\} + \{S_i\}$,于是有$|S'| = |T|$,即$S'$也是最优解
故可以证明贪心的正确性
于是每次模拟的时候利用线段树即可,时间复杂度$O(mlogm + mlogn)$
/**************************************************************
Problem: 1828
User: rausen
Language: C++
Result: Accepted
Time:772 ms
Memory:5708 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
const int N = 1e5 + ;
const int M = 1e5 + ;
const int inf = 1e9; int read(); struct data {
int l, r; inline void get() {
l = read(), r = read();
}
inline bool operator < (const data &d) const {
return r < d.r;
}
} a[M]; struct seg {
seg *ls, *rs;
int mn, tag; #define Len (1 << 16)
inline void* operator new(size_t) {
static seg *mempool, *c;
if (mempool == c)
mempool = (c = new seg[Len]) + Len;
c -> ls = c -> rs = NULL, c -> mn = c -> tag = ;
return c++;
}
#undef Len inline void update() {
mn = min(ls -> mn, rs -> mn);
}
inline void push() {
ls -> tag += tag, rs -> tag += tag;
ls -> mn -= tag, rs -> mn -= tag;
tag = ;
} #define mid (l + r >> 1)
void build(int l, int r) {
if (l == r) {
mn = read();
return;
}
(ls = new()seg) -> build(l, mid), (rs = new()seg) -> build(mid + , r);
update();
} void modify(int l, int r, int L, int R) {
if (L <= l && r <= R) {
++tag, --mn;
return;
}
push();
if (L <= mid) ls -> modify(l, mid, L, R);
if (mid < R) rs -> modify(mid + , r, L, R);
update();
} int query(int l, int r, int L, int R) {
if (L <= l && r <= R) return mn;
push();
int res = inf;
if (L <= mid) res = min(res, ls -> query(l, mid, L, R));
if (mid < R) res = min(res, rs -> query(mid + , r, L, R));
update();
return res;
}
#undef mid
} *T; int n, m, ans; int main() {
int i;
n = read(), m = read();
(T = new()seg) -> build(, n);
for (i = ; i <= m; ++i) a[i].get();
sort(a + , a + m + );
for (i = ; i <= m; ++i)
if (T -> query(, n, a[i].l, a[i].r))
T -> modify(, n, a[i].l, a[i].r), ++ans;
printf("%d\n", ans);
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}
BZOJ1828 [Usaco2010 Mar]balloc 农场分配的更多相关文章
- BZOJ_1828_[Usaco2010 Mar]balloc 农场分配_线段树
BZOJ_1828_[Usaco2010 Mar]balloc 农场分配_线段树 Description Input 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数 ...
- BZOJ 1828: [Usaco2010 Mar]balloc 农场分配
Description Input 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i ...
- 【BZOJ】1828: [Usaco2010 Mar]balloc 农场分配(经典贪心)
[算法]贪心+线段树 [题意]给定n个数字ci,m个区间[a,b](1<=a,b<=10^5),每个位置最多被ci个区间覆盖,求最多选择多少区间. 附加退化问题:全部ci=1,即求最多的不 ...
- BZOJ 1828 [Usaco2010 Mar]balloc 农场分配(贪心+线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1828 [题目大意] 现在有一些线段[l,r]的需求需要满足,i位置最多允许a[i]条线 ...
- bzoj 1828: [Usaco2010 Mar]balloc 农场分配【贪心+线段树】
长得挺唬人的贪心,按照右端点排序,用最小值线段树的询问判断当前牛是否能放进去,能的话更新线段树,ans++ 来自https://www.cnblogs.com/rausen/p/4529245.htm ...
- [Usaco2010 Mar]gather 奶牛大集会
[Usaco2010 Mar]gather 奶牛大集会 题目 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 ...
- 【BZOJ1827】[Usaco2010 Mar]gather 奶牛大集会 树形DP
[BZOJ][Usaco2010 Mar]gather 奶牛大集会 Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来 ...
- BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会 树形DP
[Usaco2010 Mar]gather 奶牛大集会 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1 ...
- 【树形DP/搜索】BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会
1827: [Usaco2010 Mar]gather 奶牛大集会 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 793 Solved: 354[Sub ...
随机推荐
- [mysql]throw exception
CREATE PROCEDURE pro_throwException (errorCode char(5), errorMessage text) BEGIN SIGNAL SQLSTATE err ...
- React Native for Android 学习笔记
C:\Users\Vic Lee\AwesomeProject>react-native run-android Starting JS server... Running D:\Android ...
- Android BinderService 过程
步骤:建立服务器端服务,暴露接口 1.BinderService /** * @Title BinderService.java * @package cn.boxai.binderservice * ...
- UVALive 3401 彩色立方体
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- kali Linux添加add-apt-repository
Debian让用户可以通过一个名为add-apt-repository的应用程序,添加和使用PPA软件库,不过Kali Linux在其默认的程序包列表中并不含有该应用程序.就Kali而言,由于这是个特 ...
- 利用scrapy-splash爬取JS生成的动态页面
目前,为了加速页面的加载速度,页面的很多部分都是用JS生成的,而对于用scrapy爬虫来说就是一个很大的问题,因为scrapy没有JS engine,所以爬取的都是静态页面,对于JS生成的动态页面都无 ...
- mac下安装 xampp 无法启动apache (转,留用)
1.查看端口是否被占用 sudo lsof -i -n 2.用终端运行xampp,查看具体的错误 sudo su /Applications/XAMPP/xamppfiles/xampp star ...
- 修改CMD字符编码
1.参考网址: 1.1.http://blog.useasp.net/archive/2012/04/24/how_to_use_UTF8_encoding_in_Windows_CMD.aspx 1 ...
- SwitchyOmega 在线调试
1,chrome 安装 Proxy SwitchyOmega 扩展程序 2,新建情景模式,输入模式名称"例如:new proxy1",选择"请选择情景模式的类型:代理服务 ...
- go并发
Go语言从语言层面上就支持了并发,这与其他语言大不一样,不像以前我们要用Thread库 来新建线程,还要用线程安全的队列库来共享数据. 以下是我入门的学习笔记. 首先,并行!=并发, 两者是不同的,可 ...