题目如下(给大区间,和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;
}