当前位置:   article > 正文

CodeForces978C Letters(模拟)_there are nn dormitories in berland state universi

there are nn dormitories in berland state university they are numbered with integers from 11 to nn.

There are  dormitories in Berland State University, they are numbered with integers from  to . Each dormitory consists of rooms, there are  rooms in -th dormitory. The rooms in -th dormitory are numbered from  to .

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 dormitories is written on an envelope. In this case, assume that all the rooms are numbered from  to  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 ,  and  an envelope can have any integer from to  written on it. If the number  is written on an envelope, it means that the letter should be delivered to the room number  of the second dormitory.

For each of  letters by the room number among all  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  and   — the number of dormitories and the number of letters.

The second line contains a sequence  , where  equals to the number of rooms in the -th dormitory. The third line contains a sequence  , where  equals to the room number (among all rooms of all dormitories) for the -th letter. All  are given in increasing order.

Output

Print  lines. For each letter print two integers  and  — the dormitory number  and the room number  in this dormitory  to deliver the letter.

Examples
Input
3 6
10 15 12
1 9 12 23 26 37
Output
1 1
1 9
2 2
2 13
3 1
3 12
Input
2 3
5 10000000000
5 6 9999999999
Output
1 5
2 1
2 9999999994
Note

In the first example letters should be delivered in the following order:

  • the first letter in room  of the first dormitory
  • the second letter in room  of the first dormitory
  • the third letter in room  of the second dormitory
  • the fourth letter in room  of the second dormitory
  • the fifth letter in room  of the third dormitory
  • the sixth letter in room  of the third dormitory


题意:

直接分析数据吧。

给出n,m。有n个宿舍楼以及每个宿舍的房间数,把所有房间号按1到a1+a2+...+an编号。再给出m次询问,每次询问一个编号,问这个编号是第几个宿舍楼的第几个宿舍。m个询问按递增顺序给出。


解题思路:

因为按递增顺序给出,所以每找一次就可以存下当前找到的sum和对应的下标,下次找直接在这个基础上继续找下去就行了。


AC代码:

  1. #include<stdio.h>
  2. typedef long long ll;
  3. int main()
  4. {
  5. ll n,m;
  6. while(~scanf("%lld%lld",&n,&m))
  7. {
  8. ll a[n];
  9. for(ll i=0;i<n;i++)
  10. {
  11. scanf("%lld",&a[i]);
  12. }
  13. ll sum=0,x;
  14. int f=0,i;//记录下标避免重复查找
  15. while(m--)
  16. {
  17. scanf("%lld",&x);//按递增顺序给出
  18. x-=sum;
  19. for(i=f;i<n;i++)
  20. {
  21. if(x-a[i]>0)
  22. {
  23. x-=a[i];
  24. sum+=a[i];
  25. }
  26. else
  27. {
  28. printf("%d %lld\n",i+1,x);
  29. f=i;
  30. break;
  31. }
  32. }
  33. }
  34. }
  35. return 0;
  36. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/471348
推荐阅读
相关标签
  

闽ICP备14008679号