HDU lines && TIANKENG’s restaurant

6 篇文章 0 订阅
0 篇文章 0 订阅

lines

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 334    Accepted Submission(s): 157


Problem Description
John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A.
 

Input
The first line contains a single integer  T(1T100) (the data for  N>100  less than 11 cases),indicating the number of test cases.
Each test case begins with an integer  N(1N105) ,indicating the number of lines.
Next N lines contains two integers  Xi  and  Yi(1XiYi109) ,describing a line.
 

Output
For each case, output an integer means how many lines cover A.
 

Sample Input
  
  
2 5 1 2 2 2 2 4 3 4 5 1000 5 1 1 2 2 3 3 4 4 5 5
 

Sample Output
  
  
3 1
 

Source
 

Recommend
heyang   |   We have carefully selected several similar problems for you:   5126  5125  5122  5121  5120 
 
题意:
给出一些在X轴上的线段,找出一个点,使这个点被尽量多的线段覆盖,求能够覆盖这个点的线段条数
解析:
首先,数据范围很大,开数组根本就开不了,但是最多能用到的,就是 2*10^5个数据,所以对其进行离散化,把用到的数据放在一个数组中。
需要注意的是:能够出现次数最多的点,一定是线段的端点。(线段与线段覆盖最短的即是端点重合)

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

struct node//存放每一条线段的信息
{
    int xi;
    int yi;
};
node line[100009];

int num[200009],num1;//存储端点值,因为共有10^5个线段,所以有2*10^5个点
int ls[200009],ls1;//存储离散化后的起点终点
int fx[100009];//存储起点进入次数
int fy[100009];//存储终点进入次数

int Find(int a,int shu)//二分查找,找到数据所在数组中的位置
{
    int mid,l = 0,r = shu-1;
    while(l <= r)
    {
        mid = (l + r)/2;
        if(ls[mid] > a)
            r = mid-1;
        else if(ls[mid] < a)
            l = mid+1;
        else if(ls[mid] == a)
            return mid;
    }
    return -1;
}

int main()
{
    int t,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&m);
        num1 = 0;
        for(int i = 0;i < m;i++)
        {
            scanf("%d%d",&line[i].xi,&line[i].yi);
            num[num1++] = line[i].xi;//记录每一个出现的点
            num[num1++] = line[i].yi;
        }
        sort(num,num+num1);//将这些点从小到大排序
        ls[0] = num[0];
        ls1 = 1;
        //将出现的数据按照顺序放在数组中,即离散化
        for(int i = 1;i < num1;i++)//离散化
        {
            if(num[i] == ls[ls1-1])
                continue;
            ls[ls1++] = num[i];
        }
        memset(fx,0,sizeof(fx));
        memset(fy,0,sizeof(fy));
        for(int i = 0;i < m;i++)
        {
            int x = Find(line[i].xi,ls1);//找到所用数据在离散化数组中的位置
            int y = Find(line[i].yi,ls1);
            fx[x]++;//线段的起点代表进入这条线断的区域,所以++;
            fy[y]--;//终点代表走出这条线断,所以--;
        }
        int sum = 0,maxx = 0;//现在开始查询最大线段数
        for(int i = 0;i < ls1;i++)
        {
            sum += fx[i];//说明加上在该点进入的线断数目
            maxx = max(sum,maxx);//判断当前情况下线断的最大值
            sum += fy[i];//说明走到线断的尽头,需要走出线断
        }
        printf("%d\n",maxx);
    }
    return 0;
}



还有一个比较类似的题目:

TIANKENG’s restaurant

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 903    Accepted Submission(s): 399


Problem Description
TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the ith group in sum. Assuming that each customer can own only one chair. Now we know the arriving time STi and departure time EDi of each group. Could you help TIANKENG calculate the minimum chairs he needs to prepare so that every customer can take a seat when arriving the restaurant?
 

Input
The first line contains a positive integer T(T<=100), standing for T test cases in all.

Each cases has a positive integer n(1<=n<=10000), which means n groups of customer. Then following n lines, each line there is a positive integer Xi(1<=Xi<=100), referring to the sum of the number of the ith group people, and the arriving time STi and departure time Edi(the time format is hh:mm, 0<=hh<24, 0<=mm<60), Given that the arriving time must be earlier than the departure time.

Pay attention that when a group of people arrive at the restaurant as soon as a group of people leaves from the restaurant, then the arriving group can be arranged to take their seats if the seats are enough.
 

Output
For each test case, output the minimum number of chair that TIANKENG needs to prepare.
 

Sample Input
   
   
2 2 6 08:00 09:00 5 08:59 09:59 2 6 08:00 09:00 5 09:00 10:00
 

Sample Output
   
   
11 6
 

Source
 

题意:求餐馆需要准备的椅子的数目
解析:需要准备椅子的最少数目就是人最多的时候要让每个人都坐下。
与上一道题目不同之处在于,上一道题的最大数一定出现在端点上,但本题的最大人数不一定出现在端点上,所以本题是现行搜索,不要想得太复杂哦


#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

int cust[2000];
int main()
{
    int t,m,a,b,c,d,e;
    scanf("%d",&t);
    while(t--)
    {
        memset(cust,0,sizeof(cust));
        scanf("%d",&m);
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d:%d %d:%d",&a,&b,&c,&d,&e);
            int s1 = b*60+c;
            int s2 = d*60+e;
            for(int j = s1+1;j <=s2;j++)
                cust[j] += a;
        }
        int maxx = 0;
        for(int i = 0; i < 2000; i++)
        {
            maxx = max(maxx,cust[i]);
        }
        printf("%d\n",maxx);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值