C. Letters

C. Letters
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are
n
dormitories in Berland State University, they are numbered with integers from
1
to
n
. Each dormitory consists of rooms, there are
a
i
rooms in
i
-th dormitory. The rooms in
i
-th dormitory are numbered from
1
to
a
i
.

A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all
n
dormitories is written on an envelope. In this case, assume that all the rooms are numbered from
1
to
a
1
+
a
2
+

+
a
n
and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.

For example, in case

n

2
,
a

1

3
and
a

2

5
an envelope can have any integer from
1
to
8
written on it. If the number
7
is written on an envelope, it means that the letter should be delivered to the room number
4
of the second dormitory.

For each of
m
letters by the room number among all
n
dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.

Input
The first line contains two integers
n
and
m

(
1

n
,
m

2

10
5
)
— the number of dormitories and the number of letters.

The second line contains a sequence
a
1
,
a
2
,

,
a
n

(
1

a
i

10
10
)
, where
a
i
equals to the number of rooms in the
i
-th dormitory. The third line contains a sequence
b
1
,
b
2
,

,
b
m

(
1

b
j

a
1
+
a
2
+

+
a
n
)
, where
b
j
equals to the room number (among all rooms of all dormitories) for the
j
-th letter. All
b
j
are given in increasing order.

Output
Print
m
lines. For each letter print two integers
f
and
k
— the dormitory number
f

(
1

f

n
)
and the room number
k
in this dormitory
(
1

k

a
f
)
to deliver the letter.

Examples
inputCopy
3 6
10 15 12
1 9 12 23 26 37
outputCopy
1 1
1 9
2 2
2 13
3 1
3 12
inputCopy
2 3
5 10000000000
5 6 9999999999
outputCopy
1 5
2 1
2 9999999994
Note
In the first example letters should be delivered in the following order:

the first letter in room
1
of the first dormitory
the second letter in room
9
of the first dormitory
the third letter in room
2
of the second dormitory
the fourth letter in room
13
of the second dormitory
the fifth letter in room
1
of the third dormitory
the sixth letter in room
12
of the third dormitory

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>//使用下面的lower_bound函数的头文件
#include<string.h>
using namespace std;
long long int a[200010];
int main()
{
    long long int n,m,t,p,su;
    scanf("%lld%lld",&n,&m);
    memset(a,0,sizeof(a));
    int i;
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&t);
        a[i]=a[i-1]+t;
    }
    while(m--)
    {
        scanf("%lld",&p);
        su=lower_bound(a+1,a+1+n,p)-a;
        printf("%lld %lld\n",su,p-a[su-1]);
    }
    return 0;
}

题意:告诉你几个宿舍,然后每个宿舍有多少个房间,每个房间的编号是一次从第一个加起来的,现在给你一些信封,信封上只有房间号,现在让我们判断这个房间是属于哪个宿舍,在这个宿舍里他是第几个房间。
lower_bound函数和upper_bound函数的区别就是,upper_bound函数是大于查找值的最小的指针,lower_bound函数是大于等于查找值的最小的指针,然后我们输入房间号之后,就会返回这个找到的这个地址,由于是地址,所以得剪掉a的地址,输出的就是宿舍号。房间号的话,因为a[1]里面存放的最后一个数就是第一个宿舍的最后一个房间号,所以我们用当前信封上的房间号减到上一个宿舍的最后一个房间号就是当前的第几个房间了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值