http://www.lydsy.com/JudgeOnline/problem.php?id=1651

很奇妙。。

我们发现,每一时刻的重叠数选最大的就是答案。。。。

orz

那么我们可以线段树维护每个点的次数。。。

然后就ok了。。

第二种做法:用前缀和来维护即可。。。

线段树:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
#define lc x<<1
#define rc x<<1|1
#define lson l, m, lc
#define rson m+1, r, rc
#define MID (l+r)>>1
struct nod { int mx, tag; }t[4000005];
int n;
void pushdown(int x) {
int g=t[x].tag; t[x].tag=0;
t[lc].mx+=g; t[lc].tag+=g;
t[rc].mx+=g; t[rc].tag+=g;
}
void pushup(int x) { t[x].mx=max(t[lc].mx, t[rc].mx); }
void update(int l, int r, int x, int L, int R) {
if(L<=l && r<=R) { ++t[x].mx; ++t[x].tag; return; }
pushdown(x);
int m=MID;
if(L<=m) update(lson, L, R); if(m<R) update(rson, L, R);
pushup(x);
}
int main() {
read(n);
for1(i, 1, n) {
int l=getint(), r=getint();
update(1, 1000000, 1, l, r);
}
print(t[1].mx);
return 0;
}

前缀和:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
const int M=1000005;
int n, t[M], sum, mx, ans;
int main() {
read(n);
for1(i, 1, n) {
int x=getint(), y=getint();
++t[x]; --t[y+1]; mx=max(mx, y);
}
for1(i, 1, mx) {
sum+=t[i];
if(sum>ans) ans=sum;
}
print(ans);
return 0;
}

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. Help FJ by determining: * The minimum number of stalls required in the barn so that each cow can have her private milking period * An assignment of cows to these stalls over time

有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall 现在给出每头牛的喝水时间段,问至少要多少个Stall才能满足它们的要求

Input

* Line 1: A single integer, N

* Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.

Output

* Line 1: The minimum number of stalls the barn must have.

* Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4

OUTPUT DETAILS:

Here's a graphical schedule for this output:

Time 1 2 3 4 5 6 7 8 9 10
Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..

Other outputs using the same number of stalls are possible.

HINT

不妨试下这个数据,对于按结束点SORT,再GREEDY的做法 1 3 5 7 6 9 10 11 8 12 4 13 正确的输出应该是3

Source

最新文章

  1. Objective-C三方库: ZXEasyCoding
  2. JetBrains激活
  3. 关于Winform发布时,资源文件缺失的解决方案
  4. 【HDOJ】4358 Boring counting
  5. HDFS Architecture--官方文档
  6. 【转】那些好用的iOS开发工具
  7. 0130——UIScrollView
  8. Mysql常用命令记录
  9. (17)IO中的异常处理
  10. java的三大特性,封装,继承,多态
  11. centOS7服务管理与启动流程
  12. Linux CentOS设置定时重启:crontab
  13. nginx: [emerg] unknown directive &quot;stub_status&quot; in /usr/local/openresty/nginx/conf/conf.d/ngx_metric.conf:19
  14. 关于Android Studio开发环境变量的设置(avd启动黑屏)
  15. Jenkins job 之间实现带参数触发
  16. 数链剖分(Aragorn&#39;s Story )
  17. Oracle数据库sql语句
  18. bzoj 2597 [Wc2007]剪刀石头布——费用流
  19. MS Project 如何删除前置任务
  20. Qt中切换窗口功能的实现

热门文章

  1. 算法笔记_137:二分图的最大匹配(Java)
  2. 小米电视2S加量不加价,你还会买吗?
  3. js改变下拉框内容
  4. Linux异步IO操作
  5. screen 命令使用及示例
  6. 零基础小白怎么用Python做表格?
  7. Ubuntu下设置redis让其他服务器访问
  8. mysql和redis之间互相备份
  9. ASP.NET Web API 控制器执行过程
  10. winform通过网络获取用户信息