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 ...
随机推荐
- 【前端】【转】JS跨域问题总结
详情见原博客:详解js跨域问题 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 跨域资源共享(CORS) CORS(Cross-Origin Resource Sharing)跨域资源 ...
- R语言基本操作函数---变量的基本操作
1.变量变换 as.array(x),as.data.frame(x),as.numeric(x),as.logical(x),as.complex(x),as.character(x) ...
- 浏览器禁止js打开新窗口
在项目中,有个需求是需要ajax获取新地址,然后去打开该页面地址,这样会被浏览器拦截,可以采取以下方式:1.再ajax请求先前,先创建一个新窗口 var newTab = window.open('' ...
- [转](六)unity4.6Ugui中文教程文档-------概要-UGUI Animation Integration
5.Animation Integration(动画集成) 动画允许控件的所有状态之间相互转换,充分使用unity的动画系统.这是最强大的的转换模式的在处理很多属性的同时可以进行动画. 要使用动画转换 ...
- 后台返回国标码,怎么转化为JSON
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTP ...
- asp.net Forms身份验证
Web.config中的配置<system.web><authentication mode="Forms"> <forms name="K ...
- jQuery size()函数
size() 函数 函数用于返回当前jQuery对象封装的元素个数. 语法 jQueryObject.size( ) 返回值 Number类型 返回该jQuery对象封装的DOM元素的个数. 实例说明 ...
- jsonp的使用
假设我们已经了解什么是同源策略,以及什么是浏览器的同源策略的限制. 现在我们需要在a.demo.com下的某个页面one.html 里加载不同域b.demo.com下的json数据. 我们都知道用&l ...
- Sublime Text 配置记录
sublime userSetting sublime theme sublime plug sublime userSetting 对sublime的配置 { "color_scheme& ...
- linux node安装
安装node0.10.24版本,升级了两个版本/usr/local/src/node/0.10.24/usr/local/n/versions/node/4.4.7/usr/local/n/versi ...