**A - Serval and Bus **

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 si minutes, and each bus of this route comes di 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≤n≤100, 1≤t≤105) — the number of bus routes and the time Serval goes to the station.

Each of the next n lines contains two space-separated integers si and di (1≤si,di≤105) — 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

正确代码

#include<bits/stdc++.h>
using namespace std;
int main(){
long long s[105],d[105], maxx = INT_MAX, flag = 0, n, t,temp,temp1;
scanf("%lld%lld", &n, &t);
for(int i = 1; i <= n; i++){
scanf("%lld%lld", &s[i], &d[i]);
if(s[i] >= t && maxx>s[i]){
maxx = s[i];
if(maxx == t){
flag = 1;
temp1 = i;
}
temp = i;
}
}
//cout << maxx <<" " <<temp << endl;
if(maxx != INT_MAX &&flag == 1){
printf("%lld\n", temp1);
return 0;
}
for(int i = 1; i <= n; i++){
while(maxx > s[i] && s[i] < t){
s[i] += d[i];
}
}
// for(int i = 1; i <= n; i++){
// printf("%lld ", s[i]);
// }
// printf("\n");
for(int i = 1; i <= n; i++){
if(maxx > s[i]){
maxx = s[i];
temp = i;
}
}
printf("%lld\n", temp);
}

代码理解

nt的意义分别是公交车的数量和主人公到达的时间,每一路段的公交车都和现实生活中一样由多辆公交车有时段间隔的进行行驶,之后n个循环输入的s和d是每段首辆公交车到主人公站点的时间和下一辆车到达时间的间隔,maxx里面存储的是最短时间,即公交车不管是首班还是中途哪班只要间隔和主人公到达时间最短则会保存在maxx变量里面。因为要输入n个数据进入s和d数组,所以maxx里的数据会不停的改变。这里有几个极限判断,即是车到达时间和主人公到达时间相同时,则会跳出循环直接输出车次。temp1是记录时间间隔最短的车次、到最后一个数据输入完毕,计算机的运行也到此结束,因此输出step1,结束程序。

错误及调试

自己进行编写代码的时候运行结果正确但是OJ测试一直WRONG,自己曾百思不得其解,之后参考了AC代码并进行了对比发现了错误。

int tex(int a,int b)
{
if(a<t)
{
a=a+b;
tex(a,b);
}
else if(a>=t)
{
return a-t;
}
}

此判断函数是第一次WRONG的判断函数,首要思路是将全部路线的车次到达时间通过数组h进行记录下来,数组的位次为车辆的位次,数组的数据为车辆到达的时间和主人公到达车站时间的间隔,通过这个函数将所有数据进行统一判断是当时编写的主要思路,有一个小细节当时没有考虑到,即是题意规定如果有相同最小时间间隔的车辆进入车站,即同时进入车站,则主人公会随机选一辆车进入,换言之就是第几辆车的输出是由编写代码的人决定的,因此,在判断出时间间隔为0的时候可以直接进行输出结束程序继续运行,这样可以节省大部分的运行时间。

以下是错误部分源代码:

    for(int i=1;i<=n;i++)
{
if(h[i]>h[i-1])
low=i-1;
}
cout<<low<<"\n";
return 0;
}

在进行判断时,只进行了数组前后的判断,即h1和h2,h2和h3判断,从而或略了对全局的判断,因此我将代码进行修改:

	int minn=h[1];
for(int i=1;i<=n;i++)
{
if(minn>h[i])
{
low=i;
minn=h[i];
}
}

这样就解决了程序判断不准确的问题,并且完成程序输出得到正确答案。

#C++初学记录(算法4)的更多相关文章

  1. #C++初学记录(贪心算法#结构体#贪心算法)

    贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...

  2. #C++初学记录(算法效率与度量)

    时间性能 算法复杂性函数: \[ f(n)=n^2 +1000n+\log_{10}n+1000 \] 当n的数据规模逐渐增大时,f(n)的增长趋势: 当n增大到一定值以后,计算公式中影响最大的就是n ...

  3. #C++初学记录(贪心算法#二分查找)

    D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...

  4. #C++初学记录(算法考试1)

    B - Maximal Continuous Rest Each day in Berland consists of n hours. Polycarp likes time management. ...

  5. #C++初学记录(算法3)

    C - 不要62 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司 ...

  6. #C++初学记录(算法2)

    A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...

  7. #C++初学记录(算法测试2019/5/5)(深度搜索)

    深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...

  8. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  9. #C++初学记录(动态规划(dynamic programming)例题1 钞票)

    浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...

随机推荐

  1. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  2. sencha touch carousel 扩展 CardList 可绑定data/store

    扩展代码: /* *扩展carousel *通过data,tpl,store配置数据 */ Ext.define('ux.CardList', { extend: 'Ext.carousel.Caro ...

  3. Ant的Manifest任务

    建立一个清单文件,他将放入某个jar,作为jar文件的说明书.其中,在清单文件可以指定jar文件的main-class,jar文件将可以直接运行.例子: <manifest >   < ...

  4. 关于JavaScript转义字符('、 " 、\" 、\')【原创】

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  5. C++和Java中枚举enum的用法

    在C++和java中都有枚举enum这个关键字,但是它们之间又不太一样.对于C++来说,枚举是一系列命名了的整型常量,而且从枚举值转化为对应的整型值是在内部进行的.而对于Java来说,枚举更像一个类的 ...

  6. Nginx 安装 --编译模块参数

    公司空出来一些服务器,很久没有来练手了,于是便开始有了这篇博客,记录下过程. Nginx 这个不多说了,名声在外,人们喜爱使用这款软件,主要还是因为它的高并发特性,公司也在用效果还不错,也用了它的一些 ...

  7. 为什么面试要问 hashmap 的原理

    我们都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如此特殊呢?是因为这道题考察的深度很深.这题经常出现在高级或中高级面试中.投资银行更喜欢问这个问题 ...

  8. python数据结构之树(二叉树的遍历)

    树是数据结构中非常重要的一种,主要的用途是用来提高查找效率,对于要重复查找的情况效果更佳,如二叉排序树.FP-树. 本篇学习笔记来自:二叉树及其七种遍历方式.python遍历与非遍历方式实现二叉树 介 ...

  9. iOS多线程编程之线程间的通信(转载)

    一.简单说明 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任务后,转到另1个线程继续执行任务 ...

  10. Catch That Cow--POJ3278

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...