UVa 10148 - Advertisement

29 篇文章 0 订阅

一个区间取点问题,按右端点排序。从右端开始标注,保证每段区间都有K个标注点,小于K的区间则保证其区间的点全部被标注,在标注时注意因为范围是-10000~10000,因此需要用一个20000的数组存,需将整体范围往右移动10000(例:标注-10000为a[0] = 1,标注0为a[10000] = 1),保证其全部大于零。

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

struct point
{
    int x, y;
} pot[1002];
int cmp(const void *a, const void *b)
{
    point *aa = (point *)a;
    point *bb = (point *)b;
    if(aa->y != bb->y)
        return aa->y > bb->y ? 1 : -1;
    return aa->x > bb->x ? 1 : -1;
}
int ascend_cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}
const int chr = 10000;
int main()
{
#ifdef test
    freopen("in.txt", "r", stdin);
#endif
    int t, k, n, save[20002], flag[20002];
    scanf("%d", &t);
    while(t--)
    {
        int cct = 0, xx, yy;
        scanf("%d%d", &k, &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &xx, &yy);
            if(xx < yy)  // 保证所有的左端点都小于右端点
            {
                pot[i].x = xx;
                pot[i].y = yy;
            }
            else
            {
                pot[i].x = yy;
                pot[i].y = xx;
            }
        }
        qsort(pot, n, sizeof(pot[0]), cmp);
        memset(flag, 0, sizeof(flag));
        for(int i = 0; i < n; i++)
        {
            int fct = 0;
            for(int j = pot[i].x; j <= pot[i].y; j++) // 检查此段区域内有多少点是已经被标注过的
                if(flag[j + chr])
                {
                    fct++;
                    if(fct == k)
                        break;
                }
            if(fct == k)
                continue;
            for(int j = pot[i].y; j >= pot[i].x; j--)
            {
                if(!flag[j + chr])
                {
                    flag[j + chr] = 1;
                    save[cct++] = j;
                    fct++;
                }
                if(fct == k)
                    break;
            }
        }
        qsort(save, cct, sizeof(save[0]), ascend_cmp);
        printf("%d\n", cct);
        for(int i = 0; i < cct; i++)
            printf("%d\n", save[i]);
        if(t)
            puts("");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值