概述
There are nn dormitories in Berland State University, they are numbered with integers from 11 to nn. Each dormitory consists of rooms, there are aiai rooms in ii-th dormitory. The rooms in ii-th dormitory are numbered from 11 to aiai.
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 nndormitories is written on an envelope. In this case, assume that all the rooms are numbered from 11 to a1+a2+⋯+ana1+a2+⋯+an 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=2n=2, a1=3a1=3 and a2=5a2=5 an envelope can have any integer from 11to 88 written on it. If the number 77 is written on an envelope, it means that the letter should be delivered to the room number 44 of the second dormitory.
For each of mm letters by the room number among all nn dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.
InputThe first line contains two integers nn and mm (1≤n,m≤2⋅105)(1≤n,m≤2⋅105) — the number of dormitories and the number of letters.
The second line contains a sequence a1,a2,…,ana1,a2,…,an (1≤ai≤1010)(1≤ai≤1010), where aiai equals to the number of rooms in the ii-th dormitory. The third line contains a sequence b1,b2,…,bmb1,b2,…,bm (1≤bj≤a1+a2+⋯+an)(1≤bj≤a1+a2+⋯+an), where bjbj equals to the room number (among all rooms of all dormitories) for the jj-th letter. All bjbj are given in increasing order.
OutputPrint mm lines. For each letter print two integers ff and kk — the dormitory number ff(1≤f≤n)(1≤f≤n) and the room number kk in this dormitory (1≤k≤af)(1≤k≤af) to deliver the letter.
Examples3 6 10 15 12 1 9 12 23 26 37
1 1 1 9 2 2 2 13 3 1 3 12
2 3 5 10000000000 5 6 9999999999
1 5 2 1 2 9999999994
In the first example letters should be delivered in the following order:
- the first letter in room 11 of the first dormitory
- the second letter in room 99 of the first dormitory
- the third letter in room 22 of the second dormitory
- the fourth letter in room 1313 of the second dormitory
- the fifth letter in room 11 of the third dormitory
- the sixth letter in room 1212 of the third dormitory
题意:
直接分析数据吧。
给出n,m。有n个宿舍楼以及每个宿舍的房间数,把所有房间号按1到a1+a2+...+an编号。再给出m次询问,每次询问一个编号,问这个编号是第几个宿舍楼的第几个宿舍。m个询问按递增顺序给出。
解题思路:
因为按递增顺序给出,所以每找一次就可以存下当前找到的sum和对应的下标,下次找直接在这个基础上继续找下去就行了。
AC代码:
#include<stdio.h>
typedef long long ll;
int main()
{
ll n,m;
while(~scanf("%lld%lld",&n,&m))
{
ll a[n];
for(ll i=0;i<n;i++)
{
scanf("%lld",&a[i]);
}
ll sum=0,x;
int f=0,i;//记录下标避免重复查找
while(m--)
{
scanf("%lld",&x);//按递增顺序给出
x-=sum;
for(i=f;i<n;i++)
{
if(x-a[i]>0)
{
x-=a[i];
sum+=a[i];
}
else
{
printf("%d %lldn",i+1,x);
f=i;
break;
}
}
}
}
return 0;
}
最后
以上就是冷傲犀牛为你收集整理的CodeForces978C Letters(模拟)的全部内容,希望文章能够帮你解决CodeForces978C Letters(模拟)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复