Codeforces 1153 A Serval and Bus

http://codeforces.com/problemset/problem/1153/A

It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.

Serval will go to the bus station at time $$$t$$$, and there are $$$n$$$ bus routes which stop at this station. For the $$$i$$$-th bus route, the first bus arrives at time $$$s_i$$$ minutes, and each bus of this route comes $$$d_i$$$ minutes later than the previous one.

As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.

Input

The first line contains two space-separated integers $$$n$$$ and $$$t$$$ ($$$1\leq n\leq 100$$$, $$$1\leq t\leq 10^5$$$) — the number of bus routes and the time Serval goes to the station.

Each of the next $$$n$$$ lines contains two space-separated integers $$$s_i$$$ and $$$d_i$$$ ($$$1\leq s_i,d_i\leq 10^5$$$) — the time when the first bus of this route arrives and the interval between two buses of this route.

Output

Print one number — what bus route Serval will use. If there are several possible answers, you can print any of them.

Examples

Input

2 2
6 4
9 5

Output

1

Input

5 5
3 3
2 5
5 6
4 9
6 1

Output

3

Input

3 7
2 2
2 3
2 4

Output

1

Note

In the first example, the first bus of the first route arrives at time $$$6$$$, and the first bus of the second route arrives at time $$$9$$$, so the first route is the answer.

In the second example, a bus of the third route arrives at time $$$5$$$, so it is the answer.

In the third example, buses of the first route come at times $$$2$$$, $$$4$$$, $$$6$$$, $$$8$$$, and so fourth, buses of the second route come at times $$$2$$$, $$$5$$$, $$$8$$$, and so fourth and buses of the third route come at times $$$2$$$, $$$6$$$, $$$10$$$, and so on, so $$$1$$$ and $$$2$$$ are both acceptable answers while $$$3$$$ is not.

题目大意:小明在车站等车,他总会坐上第一辆到来的车。(如果有多个选择 任意一个都可以)现给出n辆车的到达时间和时间间隔t以及小明到达车站的时间,请输入小明坐上的第一辆车的编号。

思路:水题,对n辆车按到达时间从小到大排序,二分查找到第一个满足到达时间>=小明到达时间的位置,然后从该位置向前遍历,记录差值的最小值即可。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std ;

int n,t;

struct node
{
    int s,d,id;
};
node a[105];

bool cmp(node aa,node bb)
{
    return aa.s<bb.s;
}

int main()
{
    scanf("%d %d",&n,&t);
    for(int i=0;i<n;i++)
    {
        scanf("%d %d",&a[i].s,&a[i].d);
        a[i].id=i+1;
    }
    sort(a,a+n,cmp);
    if(a[0].s>=t)
    {
        printf("%d\n",a[0].id);
        return 0;
    }
    node c;
    c.s=t;
    int len=upper_bound(a,a+n,c,cmp)-a;
    int MIN=INF;
    int idx=0;
    if(len!=n)
    {
        MIN=a[len].s-t;
        idx=len;
    }
    --len;
    int temp;
    while(len>=0)
    {
        temp=a[len].s+a[len].d*ceil((t-a[len].s)*1.0/a[len].d)-t;
        if(temp<MIN)
        {
            MIN=temp;
            idx=len;
        }
        --len;
    }
    printf("%d\n",a[idx].id);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/89355250
今日推荐