您当前的位置: 首页 > 

钟钟终

暂无认证

  • 5浏览

    0关注

    233博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

补题。。。。

钟钟终 发布时间:2021-05-30 20:22:11 ,浏览量:5

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

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 a1+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=2, a1=3 and a2=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⋅105) — the number of dormitories and the number of letters.

The second line contains a sequence a1,a2,…,an (1≤ai≤1010), where ai equals to the number of rooms in the i-th dormitory. The third line contains a sequence b1,b2,…,bm (1≤bj≤a1+a2+⋯+an), where bj equals to the room number (among all rooms of all dormitories) for the j-th letter. All bj 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≤af) to deliver the letter.

大意:n个宿舍,每个宿舍有指定的房间,从一个宿舍第一个房间按顺序排。要求给定一个数字,输出所在的宿舍楼和房间号。

思路: 1.刚开始用for循环暴力去做,总在第9组案例超时。 2.然后想到用前缀和去优化,但还是要用到for循环,超时。 3.用lower_bound直接找到前缀和中第一个大于等于指定数所在的下标,代替for循环,过了。

#include 

using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll a[maxn];
ll sum[maxn];
int main()
{
   //freopen("11.txt","r",stdin);
   ios::sync_with_stdio(0);
   cin.tie(0);cout.tie(0);
    int  n,m;
    cin>>n>>m;
    sum[0]=0;
    for(int i=1;i>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    while(m--)
    {
        ll mm;cin>>mm;
        /*for(int i=1;i            
关注
打赏
1664378814
查看更多评论
0.0751s