Codeforces Round #149 (Div. 2)——B

B. Big Segment
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A coordinate line has n segments, the i-th segment starts at the position li and ends at the position ri. We will denote such a segment as [li, ri].

You have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want to test your assumption. Find in the given set the segment which covers all other segments, and print its number. If such a segment doesn't exist, print -1.

Formally we will assume that segment [a, b] covers segment [c, d], if they meet this condition a ≤ c ≤ d ≤ b.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ 109) — the borders of the i-th segment.

It is guaranteed that no two segments coincide.

Output

Print a single integer — the number of the segment that covers all other segments in the set. If there's no solution, print -1.

The segments are numbered starting from 1 in the order in which they appear in the input.

Sample test(s)
input
3
1 1
2 2
3 3
output
-1
input
6
1 5
2 3
1 10
7 10
7 7
10 10
output
3

这是自己写的:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 100005
struct anpai{
  int start;
  int end;
}s[maxn];
int main()
{
    int n,idx=1;
    scanf("%d",&n);
    scanf("%d%d",&s[1].start,&s[1].end);
    int max=s[1].end,min=s[1].start;
    for(int i=2;i<=n;i++)
        {
          scanf("%d%d",&s[i].start,&s[i].end);
          //找到开始时间小,结束时间大的那个,此处注意,是同时满足,不一定是开始时间和结束时间的最小值
          if(min>=s[i].start&&max<=s[i].end)
          {
              max=s[i].end;
              min=s[i].start;
              idx=i;
          }
        }
    for(int i=1;i<=n;i++)
    {
      if(max<s[i].end||min>s[i].start)//判断找的那个是否满足开始时间最小且结束时间最大
      {
          printf("-1\n");
          return 0;
      }
    }
    printf("%d\n",idx);
    return 0;
}

这是神牛的代码 生气

#include<iostream>
using namespace std;

int main(){
int n,a,b,c,d,ans=-1;
cin>>n;
for(int i=0;i<n;i++){
cin>>a>>b;
if(i==0){c=a;d=b;}
if(a<c){c=a; ans=-1;}
if(d<b){d=b; ans=-1;}
if(a==c&&b==d)ans=i+1;}
cout<<ans<<endl;}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值