say_yas's blog

By say_yas, history, 4 years ago, In English

Hi, i tried to solved this problem Below is the code.

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main(){
    int n,k;cin>>n>>k;
    vector<ll> a(n);
    cin>>a[0];
    for(int i=1;i<n;i++){cin>>a[i];a[i]+=a[i-1];}
    for(int i=0;i<k;i++){ll temp;cin>>temp;
        ll j=0;
        while(temp > a[j]){j++;}
        cout<<j+1<<" ";
        j==0?cout<<temp<<endl:cout<<temp-a[j-1]<<endl;
    }
         
} 

But i am getting TLE. I saw a similar approach(link to that solution) that got ac . Can anyone point out the difference between the two solutions. Also I saw the tag of binary search but i'm unable to figure out how to apply binary search on this. Can someone please discuss it.Thanks!!

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it