CodeForces - 612D 【扫描线+线段排序】

题目链接:http://codeforces.com/problemset/problem/612/D


题目大意:有 n 条线断,问被任意 k 条线段覆盖的区间有多少个并输出;

思路:把每个左端点标记为0,右端点标记为1,按值从小到大排序,值相同的标记为0的在前面;

           定义 cut 为当前被多少线段覆盖,遇到标记为0的 cut++,当cut==k时,就是答案区间的左端点,
          遇到标记为1 就cut--,当cut==k-1,就是答案区间的右端点,用个数组保存就行了;

#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<math.h>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);
const int mod=1e9;
const int N=2e6+10;

struct node
{
    int a,f;
} pos[N];
int ans[N][2];

bool cmp(node x,node y)
{
    if(x.a!=y.a)
        return x.a<y.a;
    return x.f<y.f;
}

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    for(int i=0; i<n; i++)
    {
        scanf("%d %d",&pos[i].a,&pos[i+n].a);
        pos[i].f=0;
        pos[i+n].f=1;
    }
    sort(pos,pos+2*n,cmp);
    int cut=0,e=0;
    for(int i=0; i<2*n; i++)
    {
        if(pos[i].f==0)
        {
            cut++;
            if(cut==k)
                ans[e][0]=pos[i].a;
        }
        else
        {
            cut--;
            if(cut==k-1)
                ans[e++][1]=pos[i].a;
        }
    }
    printf("%d\n",e);
    for(int i=0; i<e; i++)
        printf("%d %d\n",ans[i][0],ans[i][1]);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值