978C - Letters - CodeForces Solution


binary search implementation two pointers *1000

Please click on ads to support us..

Python Code:

n, m = input().split()
n = int(n)
m = int(m)
dormitories = input().split()
roomIndices = [0] + [None] * (n - 1)
roomIndex = 0
for i in range(n-1):
    roomIndex += int(dormitories[i])
    roomIndices[i + 1] = roomIndex
def binSearch(number, start, end):
    if start == end:
        return str(start + 1) + " " + str(number - roomIndices[start])
    if number > roomIndices[(start + end + 1) // 2]:
        return binSearch(number, (start + end + 1) // 2, end)
    return binSearch(number, start, (start + end) // 2)
for i in input().split():
    print(binSearch(int(i), 0, n - 1))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;*/

// file I/O
/*ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs*/

/* ------------------------------------ */

template <typename T>
ostream& operator <<(ostream& output, const vector<T>& data) {
    for (const T& x : data)
        output << x <<" ";
    return output;
}
 
template<typename T>
istream& operator>>(istream& input,vector<T>& data) {
    for (auto& item : data)
        input >> item;
    return input;
}

/* ------------------------------------ */

typedef long long ll;
typedef long double ld;

#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))

#define endl "\n"

const ll inf = 1e18;
const ll mod = 1e9 + 7;
const int MAX = 2e5+5;

/////////////////////////////////////////////

void solve() {
    ll n,m; 
    cin>>n>>m;
    vector<ll> v(n+1);
    for(ll i=1; i<=n; ++i) {
    	ll x; cin>>x;
    	v[i] = x + v[i-1];
    }
    while(m--) {
    	ll x; cin>>x;
    	auto it = lower_bound(all(v),x);
    	cout<<it - v.begin()<<" ";
    	--it;
    	cout<<x - *it<<endl;
    }
    return;
}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
    ll tc=1; 
    // cin>>tc;
    for(ll i=1; i<=tc; ++i) {
    	solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign