hdu 2600 war

题目如下(给大区间,和n个小区间,求最大的未被小区间覆盖的下标)

Input
For each test case, first input one integer N(1<= N <= 100), then two integers p and q (-6000000 <= p <= q <= 6000000) represent the starting year and the ending year in this case. Followed by N wars.
Each war described as follows:
Ai Bi Ni (A,B is integer, p <= A, B <= q, Ni is a String which is the ith war's name )
Represent that the ith War took place between year A and year B.
 

 

Output
Output one number represent which year there was no war in the range p and q, if there are not only one year, output the maximum year which there was no war. If all the year had war, just output "Badly!". 
 

 

Sample Input
3

100 200

100 120 RtWar

110 140 WeWar

141 199 QqWar

 

1 -600 600

-600 600 Cool War
 

 

Sample Output
200

Badly!
思路:刚刚开始想用差分前缀和做,从后往前为0的便是答案,但是题目给的数据范围太大了,数组开不下,会爆内存,于是就用结构体数组,按左区间排序,然后慢慢找就可以了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[30];
struct node
{
    int x;
    int y;
}a[105];
int cmp(node a,node b)
{
    return a.x<b.x;
}
int main()
{
    int n,p,q,b,c;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%d%d",&p,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            gets(s);
        }
        sort(a+1,a+n+1,cmp);
        int cnt=a[1].x;
        int m=a[1].y;
        for(int i=2;i<=n;i++)
        {
            if(a[i].x>a[i-1].y+1)   /*中间有断开的区间*/
            {
                cnt=a[i].x;     /*断开区间的右端点+1*/
            }
            if(a[i].y>m)    /*记录覆盖到的最大的区间右端*/
            m=a[i].y;
        }
        if(m<q)
            printf("%d\n",q);
        else if(cnt!=p)
        {
                printf("%d\n",cnt-1);
        }
        else
        {
            printf("Badly!\n");
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值