直接贪心,我们把线段按照右端点从小到大排序,然后一个个尝试插入即可。。。

来证明贪心的正确性:

不妨设贪心得到的答案集合为$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 农场分配的更多相关文章

  1. BZOJ_1828_[Usaco2010 Mar]balloc 农场分配_线段树

    BZOJ_1828_[Usaco2010 Mar]balloc 农场分配_线段树 Description Input 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数 ...

  2. 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 ...

  3. 【BZOJ】1828: [Usaco2010 Mar]balloc 农场分配(经典贪心)

    [算法]贪心+线段树 [题意]给定n个数字ci,m个区间[a,b](1<=a,b<=10^5),每个位置最多被ci个区间覆盖,求最多选择多少区间. 附加退化问题:全部ci=1,即求最多的不 ...

  4. BZOJ 1828 [Usaco2010 Mar]balloc 农场分配(贪心+线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1828 [题目大意] 现在有一些线段[l,r]的需求需要满足,i位置最多允许a[i]条线 ...

  5. bzoj 1828: [Usaco2010 Mar]balloc 农场分配【贪心+线段树】

    长得挺唬人的贪心,按照右端点排序,用最小值线段树的询问判断当前牛是否能放进去,能的话更新线段树,ans++ 来自https://www.cnblogs.com/rausen/p/4529245.htm ...

  6. [Usaco2010 Mar]gather 奶牛大集会

    [Usaco2010 Mar]gather 奶牛大集会 题目 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 ...

  7. 【BZOJ1827】[Usaco2010 Mar]gather 奶牛大集会 树形DP

    [BZOJ][Usaco2010 Mar]gather 奶牛大集会 Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来 ...

  8. BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会 树形DP

    [Usaco2010 Mar]gather 奶牛大集会 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1 ...

  9. 【树形DP/搜索】BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会

    1827: [Usaco2010 Mar]gather 奶牛大集会 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 793  Solved: 354[Sub ...

随机推荐

  1. J2EE开发时的包命名规则

    http://www.blogjava.net/paulwong/archive/2012/04/15/374675.html 转一个J2EE开发时的包命名规则,养成良好的开发习惯 代码编写规范目的: ...

  2. 一个简单的loading,纯属自娱自乐

    /// <reference path="/scripts/js/jquery.min.js" /> var zsw = { loading: function (im ...

  3. dom节点的操作

    dom节点的操作 -- jQuery 内部插入 1.(结尾)append 方法 . appendto方法(为了方便链式操作) (开头)prepend方法           $('#div1').ap ...

  4. Oracle(创建视图)

    概念: 视图:所谓视图就是提取一张或者多张表的数据生成一个映射,管理视图可以同样达到操作原表的效果,方便数据的管理以及安全操作. 视图其实就是一条查询sql语句,用于显示一个或多个表或其他视图中的相关 ...

  5. dm9000网口收发控制以及mac地址过滤设置

    目的 :完成网口收发调试   过程 :      1.网口初始化,根据芯片数据手册配置   2.网口发数,先向DM9000中的TX FIFO存入数据,然后出发发送寄存器完成发送:   3.网口接收 . ...

  6. test homework ~ coverage about method printPrimes

    /******************************************************* * Finds and prints n prime integers * Jeff ...

  7. Java开发中经典的小实例-(打印输入重复的值)

    import java.util.ArrayList;import java.util.Scanner;public class Test8 {    public static void main( ...

  8. java回调初步学习

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢以前不理解什 ...

  9. hdu 4585 Shaolin treap

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

  10. 《OOAD与UML那点儿事》目录索引

    关键字:OOAD.UML.设计模式 各位园友,大家好,我是Bobby,在学习OOAD和开发的项目的过程中有一些感悟和想法,整理和编写了一些学习资料 [内容简介]掌握某种开发语言,让你实现了由零到一的脱 ...