BZOJ 2501: [usaco2010 Oct]Soda Machine 差分 离散化

2501: [usaco2010 Oct]Soda Machine

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 216  Solved: 149
[Submit][Status][Discuss]

Description

To meet the ever-growing demands of his N (1 <= N <= 50,000) cows, 
Farmer John has bought them a new soda machine. He wants to figure 
out the perfect place to install the machine. 

The field in which the cows graze can be represented as a one-dimensional 
number line. Cow i grazes in the range A_i..B_i (1 <= A_i <= B_i; 
A_i <= B_i <= 1,000,000,000) (a range that includes its endpoints), 
and FJ can place the soda machine at any integer point in the range 
1..1,000,000,000. Since cows are extremely lazy and try to move 
as little as possible, each cow would like to have the soda machine 
installed within her grazing range. 

Sadly, it is not always possible to satisfy every cow's desires. 
Thus FJ would like to know the largest number of cows that can be 
satisfied. 

To demonstrate the issue, consider four cows with grazing ranges 
3..5, 4..8, 1..2, and 5..10; below is a schematic of their grazing 
ranges: 

         1   2   3   4   5   6   7   8   9  10  11  12  13

         |---|---|---|---|---|---|---|---|---|---|---|---|-...

                 aaaaaaaaa

                     bbbbbbbbbbbbbbbbb

         ccccc           ddddddddddddddddddddd

Sample Output


As can be seen, the first, second and fourth cows share the point 5, 
but the third cow's grazing range is disjoint. Thus, a maximum of 
3 cows can have the soda machine within their grazing range. 
有N个人要去膜拜JZ,他们不知道JZ会出现在哪里,因此每个人有一个活动范围,只要JZ出现在这个范围内就能被膜拜, 
伟大的JZ当然希望膜拜他的人越多越好,但是JZ不能分身,因此只能选择一个位置出现,他最多可以被多少人膜拜呢, 
这个简单的问题JZ当然交给你了 


3

OUTPUT DETAILS:

If the soda machine is placed at location 5, cows 1, 2, and 4 can be
satisfied. It is impossible to satisfy all four cows.

Input

 

* Line 1: A single integer: N 

* Lines 2..N+1: Line i+1 contains two space-separated integers: A_i 
and B_i 

Output

* Line 1: A single integer representing the largest number of cows 
whose grazing intervals can all contain the soda machine. 

Sample Input

4
3 5
4 8
1 2
5 10


Sample Output

3

离散化完差分一下 扫一遍坐标 完事了


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>

using namespace std;

typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=100100;

struct node{int l,r;}a[N];

int b[N];

map<int,int>mp;
int res[N];
int main()
{
	register int i,n=read(),cnt=0,ans=0;
	for(i=1;i<=n;++i)a[i].l=read(),a[i].r=read(),b[(i<<1)-1]=a[i].l,b[i<<1]=a[i].r;
	sort(b+1,b+1+(n<<1));b[0]=-0X3f3f3f3f;
	for(i=1;i<=n<<1;++i)
		if(b[i]!=b[i-1])
			mp[b[i]]=++cnt;
	for(i=1;i<=n;++i)
	{res[mp[a[i].l]]++;res[mp[a[i].r]+1]--;}
	for(i=1;i<=cnt;++i)
	{res[i]+=res[i-1];ans=max(ans,res[i]);}
	print(ans);puts("");return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值