关于区间最小覆盖的问题总结(以zoj 3197 Google Book 为例)

博客探讨了如何解决Google Book问题,即在限制查询次数的情况下获取所有页面的签名。介绍了两种算法,一种时间复杂度为O(nlog(n)),另一种为O(n)。内容包括问题描述、输入输出格式、样例及复杂度为O(nlog(n))的解题思路和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


关于这类问题,现在我就想到两种做法,一种时间复杂度O(nlog(n),一种为O(n)的算法。

先来看一下题目:

Google Book

Time Limit: 1 Second      Memory Limit: 32768 KB

You, the best hacker in the world, want to download the books published on Google Book. After some investigation, you found that the address of each page consists of two parts. The first part is the page number, the second part is the signature which is unique for each page. To get the signature, you can send the query to the server. The query has one parameter, which indicates the page number. The server will return the signature of the required page, and it may also return the signature of some adjacent pages.

To minimize the bytes downloaded from the internet, and also make the server adminstrator hard to notice your "hack", you'd like to minimize the number of queries

Input

The input has multiple cases.
The first line of the input is a single integer T which is the number of test cases. Then T consecutive test cases follow. In each test case, the first line is a number N (1<=N<=5000), indicating the number of pages of the book. Then n lines follows. On the i-th line, there will be two integers ai and bi (ai<=i<=bi). They indicate that the query for the i-th page will return the signatures from page ai to page bi (inclusive)

Output

Results should be directed to standard output. The output of each test case should be a single integer, which is the minimum number of queries to get all the signatures.

Sample Input

2
3
1 1
2 2
3 3
3
1 1
1 3
3 3

Sample Output

3
1

题目大概是这样的:    

有一本书总共有n页,你可以查询n次,而且它告诉你每一次可以查询的页码为

ai <= i <= bi,即从第ai页到第bi页。问你最少可以查询几次能把这本书所有

的页码都可以查询到。

方法一(复杂度为O(nlog(n)))

       首先按左端点排序,再依次找每查询一次能覆盖到的最大的区间,假设还没有看过的书页为(sta , end),每次可以查询的小段区间用(xi , yi) 表示,那么对于没有找过的每段区间,我们都是找 xi<=sta,并且yi > sta的区间中yi最大的区间,直到yi = end为止。最后统计区间的个数,即为最少的查询次数。

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5000+100;
struct node
{
       int a;
       int b;
}edge[maxn];
bool cmp(node x,node y)
{
         return x.a < y.a;
}
int main()
{
        int t,n;
        scanf("%d",&t);
        while(t--)
        {
                  scanf("%d",&n);
                  int x,y;
                  for(int i=0;i<n;++i)
                  {
                           scanf("%d %d",&x,&y);
                           edge[i].a = x;
                           edge[i].b = y;
                  }
                  sort(edge,edge+n,cmp);
                  int end = edge[0].b,start = edge[0].a;
                  int i,j,ans = 1;
                  i = 1;
                  while(i<n && edge[i].a  == start)
                  {
                          if(end < edge[i].b) end = edge[i].b;
                          i++;
                  }
                  while(end!=n)
                  {
                          j = edge[i].b;
                          start = end+1;
                          i++;
                          while(i<n && edge[i].a <= start)
                          {
                                  if(edge[i].b > j) j = edge[i].b;
                                  i++;
                          }
                          if(j > end)
                          {
                                 ans++;
                                 end = j;
                          }
                  }
                  printf("%d\n",ans);
        }
        return 0;
}

方法二(O(n)):

      用一个数组a记录i能覆盖的最大距离,再用一个数组b记录能覆盖这个点的且向右距离最大的点区间。最后再根据b让每个取得查询尽量靠右,统计 取得区间个数即为最小值。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10000+100;
int a[maxn];
int b[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,ans=0;
        scanf("%d",&n);
        int ai,bi;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&ai,&bi);
            if(a[ai]<bi)
            a[ai]=bi;
        }
        b[1]=a[1];
        for(int i=2;i<=n;i++)//记录能覆盖这个点的且向右距离最大的点区间
        {
            if(b[i-1]>a[i])
            {
                b[i]=b[i-1];
            }
            else
            {
                b[i]=a[i];
            }
        }
        for(int i=1;i<=n;)
        {
            i=b[i]+1;
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}




       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值