Codeforces 1000c(线段相交求点覆盖问题)

 这道题所实话我是不会做,看了别人的代码也是懵懵懂懂,脑水不行,这里就写一点自己的理解吧。看着别人的代码几行解决了,啥时候咋也可以信手拈来。

先对每个集合的端点排序,并且对左右有要求,不可以笼统的一起排序,要标记那个电视开始端,那个点是结束端。然后通过数组来统计被覆盖的次数,很神奇。哈哈哈;

代码如下,

3
1 3
2 3
1 4
---
1 1
1 1
2 1
4 -1
4 -1
5 -1
---
f==1...0
2---
f==2...1
3---
f==3...2
2---
f==2...1
1---
f==1...1
0---
1 1 2

 

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 500005;
pair<ll,ll>a[maxn];
ll b[maxn],n;
int main()
{
	ios::sync_with_stdio(false);
    ll l,r,cnt=1;
    cin>>n;
    ll t=0;
    for(int i=1;i<=n;i++){
        cin>>l>>r;
        a[++t]={l,1};
        a[++t]={r+1,-1};
    }
    sort(a+1,a+1+t);
   // cout<<"---"<<endl;
   // for(int i=1;i<=t;i++){
   // 	cout<<a[i].first<<" "<<a[i].second<<endl;
//	}
	//cout<<"---"<<endl; 
    for(int i=2;i<=t;i++){
        b[cnt]+=a[i].first-a[i-1].first;	
		//cout<<"b=="<<cnt<<"..."<<f[cnt]<<endl;
        cnt+=a[i].second;//
        //cout<<cnt<<"---"<<endl;   
    }
    cout<<b[1];
    for(int i=2;i<=n;i++)cout<<" "<<b[i]; 
    cout<<endl;
    return 0;
}

 

可以把你们的想法分享一下啊。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值